aoptcpu.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_SRWI:
  41. begin
  42. if getnextinstruction(p,next1) and
  43. (next1.typ = ait_instruction) and
  44. ((taicpu(next1).opcode = A_SLWI) or
  45. (taicpu(next1).opcode = A_RLWINM)) and
  46. (taicpu(next1).oper[0]^.reg = taicpu(p).oper[0]^.reg) and
  47. (taicpu(next1).oper[1]^.reg = taicpu(p).oper[0]^.reg) then
  48. case taicpu(next1).opcode of
  49. A_SLWI:
  50. begin
  51. taicpu(p).opcode := A_RLWINM;
  52. taicpu(p).ops := 5;
  53. taicpu(p).loadconst(2,taicpu(next1).oper[2]^.val-taicpu(p).oper[2]^.val);
  54. if (taicpu(p).oper[2]^.val < 0) then
  55. begin
  56. taicpu(p).loadconst(3,-taicpu(p).oper[2]^.val);
  57. taicpu(p).loadconst(4,31-taicpu(next1).oper[2]^.val);
  58. inc(taicpu(p).oper[2]^.val,32);
  59. end
  60. else
  61. begin
  62. taicpu(p).loadconst(3,0);
  63. taicpu(p).loadconst(4,31-taicpu(next1).oper[2]^.val);
  64. end;
  65. asml.remove(next1);
  66. next1.free;
  67. result := true;
  68. end;
  69. A_RLWINM:
  70. begin
  71. if (taicpu(next1).oper[2]^.val = 0) then
  72. begin
  73. { convert srwi to rlwinm and see if the rlwinm }
  74. { optimization can do something with it }
  75. taicpu(p).opcode := A_RLWINM;
  76. taicpu(p).ops := 5;
  77. taicpu(p).loadconst(3,taicpu(p).oper[2]^.val);
  78. taicpu(p).loadconst(4,31);
  79. taicpu(p).loadconst(2,(32-taicpu(p).oper[2]^.val) and 31);
  80. result := true;
  81. end;
  82. end;
  83. end;
  84. end;
  85. A_RLWINM:
  86. begin
  87. if getnextinstruction(p,next1) and
  88. (next1.typ = ait_instruction) and
  89. (taicpu(next1).opcode = A_RLWINM) and
  90. (taicpu(next1).oper[0]^.reg = taicpu(p).oper[0]^.reg) and
  91. // both source and target of next1 must equal target of p
  92. (taicpu(next1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  93. (taicpu(next1).oper[2]^.val = 0) then
  94. begin
  95. l1 := taicpu(p).oper[4]^.val;
  96. if (l1 < taicpu(p).oper[3]^.val) then
  97. inc(l1,32);
  98. l2 := taicpu(next1).oper[4]^.val;
  99. if (l2 < taicpu(next1).oper[3]^.val) then
  100. inc(l2,32);
  101. if (taicpu(p).oper[3]^.val > l2) or
  102. (taicpu(next1).oper[3]^.val > l1) then
  103. begin
  104. // masks have no bits in common
  105. taicpu(p).opcode := A_LI;
  106. taicpu(p).loadconst(1,0);
  107. taicpu(p).clearop(2);
  108. taicpu(p).clearop(3);
  109. taicpu(p).clearop(4);
  110. taicpu(p).ops := 2;
  111. taicpu(p).opercnt := 2;
  112. asml.remove(next1);
  113. next1.free;
  114. end
  115. else
  116. // some of the cases with l1>32 or l2>32 can be
  117. // optimized, but others can't (like 19,17 and 25,23)
  118. if (l1 < 32) and
  119. (l2 < 32) then
  120. begin
  121. taicpu(p).oper[3]^.val := max(taicpu(p).oper[3]^.val,taicpu(next1).oper[3]^.val);
  122. taicpu(p).oper[4]^.val := min(taicpu(p).oper[4]^.val,taicpu(next1).oper[4]^.val);
  123. asml.remove(next1);
  124. next1.free;
  125. result := true;
  126. end;
  127. end;
  128. end;
  129. end;
  130. end;
  131. end;
  132. end;
  133. begin
  134. casmoptimizer:=TCpuAsmOptimizer;
  135. End.