aoptcpu.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the ARM 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. {$i fpcdefs.inc}
  20. Interface
  21. uses cpubase, aasmtai, aopt, aoptcpub;
  22. Type
  23. TCpuAsmOptimizer = class(TAsmOptimizer)
  24. { uses the same constructor as TAopObj }
  25. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  26. procedure PeepHoleOptPass2;override;
  27. End;
  28. Implementation
  29. uses
  30. aasmbase,aasmcpu;
  31. function CanBeCond(p : tai) : boolean;
  32. begin
  33. result:=(p.typ=ait_instruction) and (taicpu(p).condition=C_None);
  34. end;
  35. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  36. var
  37. next1, next2: tai;
  38. l1, l2, shlcount: longint;
  39. begin
  40. result := false;
  41. case p.typ of
  42. ait_instruction:
  43. begin
  44. case taicpu(p).opcode of
  45. A_MOV:
  46. begin
  47. { fold
  48. mov reg1,reg0, shift imm1
  49. mov reg1,reg1, shift imm2
  50. to
  51. mov reg1,reg0, shift imm1+imm2
  52. }
  53. if (taicpu(p).ops=3) and
  54. (taicpu(p).oper[0]^.typ = top_reg) and
  55. (taicpu(p).oper[2]^.typ = top_shifterop) and
  56. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  57. getnextinstruction(p,next1) and
  58. (next1.typ = ait_instruction) and
  59. (taicpu(next1).opcode = A_MOV) and
  60. (taicpu(next1).ops=3) and
  61. (taicpu(next1).oper[0]^.typ = top_reg) and
  62. (taicpu(p).oper[0]^.reg=taicpu(next1).oper[0]^.reg) and
  63. (taicpu(next1).oper[1]^.typ = top_reg) and
  64. (taicpu(p).oper[0]^.reg=taicpu(next1).oper[1]^.reg) and
  65. (taicpu(next1).oper[2]^.typ = top_shifterop) and
  66. (taicpu(next1).oper[2]^.shifterop^.rs = NR_NO) and
  67. (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(next1).oper[2]^.shifterop^.shiftmode) then
  68. begin
  69. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(next1).oper[2]^.shifterop^.shiftimm);
  70. asml.remove(next1);
  71. next1.free;
  72. result := true;
  73. end;
  74. end;
  75. end;
  76. end;
  77. end;
  78. end;
  79. { instructions modifying the CPSR can be only the last instruction }
  80. function MustBeLast(p : tai) : boolean;
  81. begin
  82. Result:=(p.typ=ait_instruction) and
  83. ((taicpu(p).opcode in [A_BL,A_BLX,A_CMP,A_CMN,A_SWI,A_TEQ,A_TST,A_CMF,A_CMFE {,A_MSR}]) or
  84. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  85. (taicpu(p).oppostfix=PF_S));
  86. end;
  87. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  88. var
  89. p,hp1,hp2: tai;
  90. l : longint;
  91. condition : tasmcond;
  92. hp3: tai;
  93. { UsedRegs, TmpUsedRegs: TRegSet; }
  94. begin
  95. p := BlockStart;
  96. { UsedRegs := []; }
  97. while (p <> BlockEnd) Do
  98. begin
  99. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  100. case p.Typ Of
  101. Ait_Instruction:
  102. begin
  103. case taicpu(p).opcode Of
  104. A_B:
  105. if taicpu(p).condition<>C_None then
  106. begin
  107. { check for
  108. Bxx xxx
  109. <several instructions>
  110. xxx:
  111. }
  112. l:=0;
  113. GetNextInstruction(p, hp1);
  114. while assigned(hp1) and
  115. (l<=4) and
  116. CanBeCond(hp1) and
  117. { stop on labels }
  118. not(hp1.typ=ait_label) do
  119. begin
  120. inc(l);
  121. if MustBeLast(hp1) then
  122. begin
  123. GetNextInstruction(hp1,hp1);
  124. break;
  125. end
  126. else
  127. GetNextInstruction(hp1,hp1);
  128. end;
  129. if assigned(hp1) then
  130. begin
  131. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  132. begin
  133. if (l<=4) and (l>0) then
  134. begin
  135. condition:=inverse_cond(taicpu(p).condition);
  136. hp2:=p;
  137. GetNextInstruction(p,hp1);
  138. p:=hp1;
  139. repeat
  140. if hp1.typ=ait_instruction then
  141. taicpu(hp1).condition:=condition;
  142. if MustBeLast(hp1) then
  143. begin
  144. GetNextInstruction(hp1,hp1);
  145. break;
  146. end
  147. else
  148. GetNextInstruction(hp1,hp1);
  149. until not(assigned(hp1)) or
  150. not(CanBeCond(hp1)) or
  151. (hp1.typ=ait_label);
  152. { wait with removing else GetNextInstruction could
  153. ignore the label if it was the only usage in the
  154. jump moved away }
  155. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  156. asml.remove(hp2);
  157. hp2.free;
  158. continue;
  159. end;
  160. end
  161. else
  162. begin
  163. { check further for
  164. Bcc xxx
  165. <several movs 1>
  166. B yyy
  167. xxx:
  168. <several movs 2>
  169. yyy:
  170. }
  171. { hp2 points to jmp yyy }
  172. hp2:=hp1;
  173. { skip hp1 to xxx }
  174. GetNextInstruction(hp1, hp1);
  175. if assigned(hp2) and
  176. assigned(hp1) and
  177. (l<=3) and
  178. (hp2.typ=ait_instruction) and
  179. (taicpu(hp2).is_jmp) and
  180. (taicpu(hp2).condition=C_None) and
  181. { real label and jump, no further references to the
  182. label are allowed }
  183. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  184. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  185. begin
  186. l:=0;
  187. { skip hp1 to <several moves 2> }
  188. GetNextInstruction(hp1, hp1);
  189. while assigned(hp1) and
  190. CanBeCond(hp1) do
  191. begin
  192. inc(l);
  193. GetNextInstruction(hp1, hp1);
  194. end;
  195. { hp1 points to yyy: }
  196. if assigned(hp1) and
  197. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  198. begin
  199. condition:=inverse_cond(taicpu(p).condition);
  200. GetNextInstruction(p,hp1);
  201. hp3:=p;
  202. p:=hp1;
  203. repeat
  204. if hp1.typ=ait_instruction then
  205. taicpu(hp1).condition:=condition;
  206. GetNextInstruction(hp1,hp1);
  207. until not(assigned(hp1)) or
  208. not(CanBeCond(hp1));
  209. { hp2 is still at jmp yyy }
  210. GetNextInstruction(hp2,hp1);
  211. { hp2 is now at xxx: }
  212. condition:=inverse_cond(condition);
  213. GetNextInstruction(hp1,hp1);
  214. { hp1 is now at <several movs 2> }
  215. repeat
  216. taicpu(hp1).condition:=condition;
  217. GetNextInstruction(hp1,hp1);
  218. until not(assigned(hp1)) or
  219. not(CanBeCond(hp1)) or
  220. (hp1.typ=ait_label);
  221. {
  222. asml.remove(hp1.next)
  223. hp1.next.free;
  224. asml.remove(hp1);
  225. hp1.free;
  226. }
  227. { remove Bcc }
  228. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  229. asml.remove(hp3);
  230. hp3.free;
  231. { remove jmp }
  232. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  233. asml.remove(hp2);
  234. hp2.free;
  235. continue;
  236. end;
  237. end;
  238. end;
  239. end;
  240. end;
  241. end;
  242. end;
  243. end;
  244. p := tai(p.next)
  245. end;
  246. end;
  247. begin
  248. casmoptimizer:=TCpuAsmOptimizer;
  249. End.