aoptcpu.pas 15 KB

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