aoptcpu.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the PowerPC optimizer object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit aoptcpu;
  19. Interface
  20. {$i fpcdefs.inc}
  21. uses cpubase, aoptobj, aoptcpub, aopt, aasmtai;
  22. Type
  23. TCpuAsmOptimizer = class(TAsmOptimizer)
  24. { uses the same constructor as TAopObj }
  25. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  26. End;
  27. Implementation
  28. uses
  29. cutils, aasmcpu;
  30. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  31. var
  32. next1, next2: tai;
  33. l1, l2: longint;
  34. begin
  35. result := false;
  36. case p.typ of
  37. ait_instruction:
  38. begin
  39. case taicpu(p).opcode of
  40. A_RLWINM:
  41. begin
  42. if getnextinstruction(p,next1) and
  43. (next1.typ = ait_instruction) and
  44. (taicpu(next1).opcode = A_RLWINM) and
  45. (taicpu(next1).oper[0]^.reg = taicpu(p).oper[0]^.reg) and
  46. // both source and target of next1 must equal target of p
  47. (taicpu(next1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  48. (taicpu(next1).oper[2]^.val = 0) then
  49. begin
  50. l1 := taicpu(p).oper[4]^.val;
  51. if (l1 < taicpu(p).oper[3]^.val) then
  52. inc(l1,32);
  53. l2 := taicpu(next1).oper[4]^.val;
  54. if (l2 < taicpu(next1).oper[3]^.val) then
  55. inc(l2,32);
  56. if (taicpu(p).oper[3]^.val > l2) or
  57. (taicpu(next1).oper[3]^.val > l1) then
  58. begin
  59. // masks have no bits in common
  60. taicpu(p).opcode := A_LI;
  61. taicpu(p).loadconst(1,0);
  62. taicpu(p).clearop(2);
  63. taicpu(p).clearop(3);
  64. taicpu(p).clearop(4);
  65. taicpu(p).ops := 2;
  66. asml.remove(next1);
  67. next1.free;
  68. end
  69. else
  70. // some of the cases with l1>32 or l2>32 can be
  71. // optimized, but others can't (like 19,17 and 25,23)
  72. if (l1 < 32) and
  73. (l2 < 32) then
  74. begin
  75. taicpu(p).oper[3]^.val := max(taicpu(p).oper[3]^.val,taicpu(next1).oper[3]^.val);
  76. taicpu(p).oper[4]^.val := min(taicpu(p).oper[4]^.val,taicpu(next1).oper[4]^.val);
  77. asml.remove(next1);
  78. next1.free;
  79. result := true;
  80. end;
  81. end;
  82. end;
  83. end;
  84. end;
  85. end;
  86. end;
  87. begin
  88. casmoptimizer:=TCpuAsmOptimizer;
  89. End.