aoptcpu.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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: tai;
  38. begin
  39. result := false;
  40. case p.typ of
  41. ait_instruction:
  42. begin
  43. case taicpu(p).opcode of
  44. A_MOV:
  45. begin
  46. { fold
  47. mov reg1,reg0, shift imm1
  48. mov reg1,reg1, shift imm2
  49. to
  50. mov reg1,reg0, shift imm1+imm2
  51. }
  52. if (taicpu(p).ops=3) and
  53. (taicpu(p).oper[0]^.typ = top_reg) and
  54. (taicpu(p).oper[2]^.typ = top_shifterop) and
  55. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  56. getnextinstruction(p,next1) and
  57. (next1.typ = ait_instruction) and
  58. (taicpu(next1).opcode = A_MOV) and
  59. (taicpu(next1).ops=3) and
  60. (taicpu(next1).oper[0]^.typ = top_reg) and
  61. (taicpu(p).oper[0]^.reg=taicpu(next1).oper[0]^.reg) and
  62. (taicpu(next1).oper[1]^.typ = top_reg) and
  63. (taicpu(p).oper[0]^.reg=taicpu(next1).oper[1]^.reg) and
  64. (taicpu(next1).oper[2]^.typ = top_shifterop) and
  65. (taicpu(next1).oper[2]^.shifterop^.rs = NR_NO) and
  66. (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(next1).oper[2]^.shifterop^.shiftmode) then
  67. begin
  68. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(next1).oper[2]^.shifterop^.shiftimm);
  69. asml.remove(next1);
  70. next1.free;
  71. result := true;
  72. end;
  73. end;
  74. end;
  75. end;
  76. end;
  77. end;
  78. { instructions modifying the CPSR can be only the last instruction }
  79. function MustBeLast(p : tai) : boolean;
  80. begin
  81. Result:=(p.typ=ait_instruction) and
  82. ((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
  83. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  84. (taicpu(p).oppostfix=PF_S));
  85. end;
  86. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  87. var
  88. p,hp1,hp2: tai;
  89. l : longint;
  90. condition : tasmcond;
  91. hp3: tai;
  92. WasLast: boolean;
  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. WasLast:=False;
  114. GetNextInstruction(p, hp1);
  115. while assigned(hp1) and
  116. (l<=4) and
  117. CanBeCond(hp1) and
  118. { stop on labels }
  119. not(hp1.typ=ait_label) do
  120. begin
  121. inc(l);
  122. if MustBeLast(hp1) then
  123. begin
  124. WasLast:=True;
  125. GetNextInstruction(hp1,hp1);
  126. break;
  127. end
  128. else
  129. GetNextInstruction(hp1,hp1);
  130. end;
  131. if assigned(hp1) then
  132. begin
  133. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  134. begin
  135. if (l<=4) and (l>0) then
  136. begin
  137. condition:=inverse_cond(taicpu(p).condition);
  138. hp2:=p;
  139. GetNextInstruction(p,hp1);
  140. p:=hp1;
  141. repeat
  142. if hp1.typ=ait_instruction then
  143. taicpu(hp1).condition:=condition;
  144. if MustBeLast(hp1) then
  145. begin
  146. GetNextInstruction(hp1,hp1);
  147. break;
  148. end
  149. else
  150. GetNextInstruction(hp1,hp1);
  151. until not(assigned(hp1)) or
  152. not(CanBeCond(hp1)) or
  153. (hp1.typ=ait_label);
  154. { wait with removing else GetNextInstruction could
  155. ignore the label if it was the only usage in the
  156. jump moved away }
  157. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  158. asml.remove(hp2);
  159. hp2.free;
  160. continue;
  161. end;
  162. end
  163. else
  164. { do not perform further optimizations if there is inctructon
  165. in block #1 which can not be optimized.
  166. }
  167. if not WasLast then
  168. begin
  169. { check further for
  170. Bcc xxx
  171. <several instructions 1>
  172. B yyy
  173. xxx:
  174. <several instructions 2>
  175. yyy:
  176. }
  177. { hp2 points to jmp yyy }
  178. hp2:=hp1;
  179. { skip hp1 to xxx }
  180. GetNextInstruction(hp1, hp1);
  181. if assigned(hp2) and
  182. assigned(hp1) and
  183. (l<=3) and
  184. (hp2.typ=ait_instruction) and
  185. (taicpu(hp2).is_jmp) and
  186. (taicpu(hp2).condition=C_None) and
  187. { real label and jump, no further references to the
  188. label are allowed }
  189. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  190. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  191. begin
  192. l:=0;
  193. { skip hp1 to <several moves 2> }
  194. GetNextInstruction(hp1, hp1);
  195. while assigned(hp1) and
  196. CanBeCond(hp1) do
  197. begin
  198. inc(l);
  199. GetNextInstruction(hp1, hp1);
  200. end;
  201. { hp1 points to yyy: }
  202. if assigned(hp1) and
  203. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  204. begin
  205. condition:=inverse_cond(taicpu(p).condition);
  206. GetNextInstruction(p,hp1);
  207. hp3:=p;
  208. p:=hp1;
  209. repeat
  210. if hp1.typ=ait_instruction then
  211. taicpu(hp1).condition:=condition;
  212. GetNextInstruction(hp1,hp1);
  213. until not(assigned(hp1)) or
  214. not(CanBeCond(hp1));
  215. { hp2 is still at jmp yyy }
  216. GetNextInstruction(hp2,hp1);
  217. { hp2 is now at xxx: }
  218. condition:=inverse_cond(condition);
  219. GetNextInstruction(hp1,hp1);
  220. { hp1 is now at <several movs 2> }
  221. repeat
  222. taicpu(hp1).condition:=condition;
  223. GetNextInstruction(hp1,hp1);
  224. until not(assigned(hp1)) or
  225. not(CanBeCond(hp1)) or
  226. (hp1.typ=ait_label);
  227. {
  228. asml.remove(hp1.next)
  229. hp1.next.free;
  230. asml.remove(hp1);
  231. hp1.free;
  232. }
  233. { remove Bcc }
  234. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  235. asml.remove(hp3);
  236. hp3.free;
  237. { remove jmp }
  238. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  239. asml.remove(hp2);
  240. hp2.free;
  241. continue;
  242. end;
  243. end;
  244. end;
  245. end;
  246. end;
  247. end;
  248. end;
  249. end;
  250. p := tai(p.next)
  251. end;
  252. end;
  253. begin
  254. casmoptimizer:=TCpuAsmOptimizer;
  255. End.