aoptcpu.pas 13 KB

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