aoptcpu.pas 15 KB

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