aoptcpu.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. cgutils,
  36. aasmbase,aasmcpu;
  37. function CanBeCond(p : tai) : boolean;
  38. begin
  39. result:=
  40. (p.typ=ait_instruction) and
  41. (taicpu(p).condition=C_None) and
  42. ((taicpu(p).opcode<>A_BLX) or
  43. (taicpu(p).oper[0]^.typ=top_reg));
  44. end;
  45. function RefsEqual(const r1, r2: treference): boolean;
  46. begin
  47. refsequal :=
  48. (r1.offset = r2.offset) and
  49. (r1.base = r2.base) and
  50. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  51. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  52. (r1.relsymbol = r2.relsymbol) and
  53. (r1.signindex = r2.signindex) and
  54. (r1.shiftimm = r2.shiftimm) and
  55. (r1.addressmode = r2.addressmode) and
  56. (r1.shiftmode = r2.shiftmode);
  57. end;
  58. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  59. var
  60. next1: tai;
  61. hp1,hp2: tai;
  62. begin
  63. result := false;
  64. case p.typ of
  65. ait_instruction:
  66. begin
  67. {
  68. change
  69. <op> reg,x,y
  70. cmp reg,#0
  71. into
  72. <op>s reg,x,y
  73. }
  74. { this optimization can applied only to the currently enabled operations because
  75. the other operations do not update all flags and FPC does not track flag usage }
  76. if (taicpu(p).opcode in [A_ADC,A_ADD,A_SUB {A_UDIV,A_SDIV,A_MUL,A_MVN,A_MOV,A_ORR,A_EOR,A_AND}]) and
  77. (taicpu(p).oper[0]^.typ = top_reg) and
  78. (taicpu(p).oppostfix = PF_None) and
  79. (taicpu(p).condition = C_None) and
  80. GetNextInstruction(p, hp1) and
  81. (tai(hp1).typ = ait_instruction) and
  82. (taicpu(hp1).opcode = A_CMP) and
  83. (taicpu(hp1).oppostfix = PF_None) and
  84. (taicpu(hp1).condition = C_None) and
  85. (taicpu(hp1).oper[0]^.typ = top_reg) and
  86. (taicpu(hp1).oper[1]^.typ = top_const) and
  87. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  88. (taicpu(hp1).oper[1]^.val = 0) { and
  89. GetNextInstruction(hp1, hp2) and
  90. (tai(hp2).typ = ait_instruction) and
  91. // be careful here, following instructions could use other flags
  92. // however after a jump fpc never depends on the value of flags
  93. (taicpu(hp2).opcode = A_B) and
  94. (taicpu(hp2).condition in [C_EQ,C_NE,C_MI,C_PL])} then
  95. begin
  96. taicpu(p).oppostfix:=PF_S;
  97. asml.remove(hp1);
  98. hp1.free;
  99. end
  100. else
  101. case taicpu(p).opcode of
  102. A_STR:
  103. begin
  104. { change
  105. str reg1,ref
  106. ldr reg2,ref
  107. into
  108. str reg1,ref
  109. mov reg2,reg1
  110. }
  111. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  112. getnextinstruction(p,hp1) and
  113. (hp1.typ = ait_instruction) and
  114. (taicpu(hp1).opcode = A_LDR) and
  115. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  116. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  117. begin
  118. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  119. begin
  120. asml.remove(hp1);
  121. hp1.free;
  122. end
  123. else
  124. begin
  125. taicpu(hp1).opcode:=A_MOV;
  126. taicpu(hp1).oppostfix:=PF_None;
  127. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  128. end;
  129. result := true;
  130. end;
  131. end;
  132. A_MOV:
  133. begin
  134. { fold
  135. mov reg1,reg0, shift imm1
  136. mov reg1,reg1, shift imm2
  137. to
  138. mov reg1,reg0, shift imm1+imm2
  139. }
  140. if (taicpu(p).ops=3) and
  141. (taicpu(p).oper[0]^.typ = top_reg) and
  142. (taicpu(p).oper[2]^.typ = top_shifterop) and
  143. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  144. getnextinstruction(p,next1) and
  145. (next1.typ = ait_instruction) and
  146. (taicpu(next1).opcode = A_MOV) and
  147. (taicpu(p).condition=taicpu(next1).condition) and
  148. (taicpu(next1).ops=3) and
  149. (taicpu(next1).oper[0]^.typ = top_reg) and
  150. (taicpu(p).oper[0]^.reg=taicpu(next1).oper[0]^.reg) and
  151. (taicpu(next1).oper[1]^.typ = top_reg) and
  152. (taicpu(p).oper[0]^.reg=taicpu(next1).oper[1]^.reg) and
  153. (taicpu(next1).oper[2]^.typ = top_shifterop) and
  154. (taicpu(next1).oper[2]^.shifterop^.rs = NR_NO) and
  155. (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(next1).oper[2]^.shifterop^.shiftmode) then
  156. begin
  157. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(next1).oper[2]^.shifterop^.shiftimm);
  158. { avoid overflows }
  159. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  160. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  161. SM_ROR:
  162. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  163. SM_ASR:
  164. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  165. SM_LSR,
  166. SM_LSL:
  167. begin
  168. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  169. InsertLLItem(p.previous, p.next, hp1);
  170. p.free;
  171. p:=hp1;
  172. end;
  173. else
  174. internalerror(2008072803);
  175. end;
  176. asml.remove(next1);
  177. next1.free;
  178. result := true;
  179. end;
  180. end;
  181. A_AND:
  182. begin
  183. {
  184. change
  185. and reg2,reg1,const1
  186. and reg2,reg2,const2
  187. to
  188. and reg2,reg1,(const1 and const2)
  189. }
  190. if (taicpu(p).oper[0]^.typ = top_reg) and
  191. (taicpu(p).oper[1]^.typ = top_reg) and
  192. (taicpu(p).oper[2]^.typ = top_const) and
  193. GetNextInstruction(p, hp1) and
  194. (tai(hp1).typ = ait_instruction) and
  195. (taicpu(hp1).opcode = A_AND) and
  196. (taicpu(p).condition=taicpu(hp1).condition) and
  197. (taicpu(p).oppostfix=PF_None) and
  198. (taicpu(hp1).oper[0]^.typ = top_reg) and
  199. (taicpu(hp1).oper[1]^.typ = top_reg) and
  200. (taicpu(hp1).oper[2]^.typ = top_const) and
  201. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  202. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) then
  203. begin
  204. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  205. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  206. asml.remove(hp1);
  207. hp1.free;
  208. end;
  209. end;
  210. end;
  211. end;
  212. end;
  213. end;
  214. { instructions modifying the CPSR can be only the last instruction }
  215. function MustBeLast(p : tai) : boolean;
  216. begin
  217. Result:=(p.typ=ait_instruction) and
  218. ((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
  219. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  220. (taicpu(p).oppostfix=PF_S));
  221. end;
  222. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  223. var
  224. p,hp1,hp2: tai;
  225. l : longint;
  226. condition : tasmcond;
  227. hp3: tai;
  228. WasLast: boolean;
  229. { UsedRegs, TmpUsedRegs: TRegSet; }
  230. begin
  231. p := BlockStart;
  232. { UsedRegs := []; }
  233. while (p <> BlockEnd) Do
  234. begin
  235. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  236. case p.Typ Of
  237. Ait_Instruction:
  238. begin
  239. case taicpu(p).opcode Of
  240. A_B:
  241. if taicpu(p).condition<>C_None then
  242. begin
  243. { check for
  244. Bxx xxx
  245. <several instructions>
  246. xxx:
  247. }
  248. l:=0;
  249. WasLast:=False;
  250. GetNextInstruction(p, hp1);
  251. while assigned(hp1) and
  252. (l<=4) and
  253. CanBeCond(hp1) and
  254. { stop on labels }
  255. not(hp1.typ=ait_label) do
  256. begin
  257. inc(l);
  258. if MustBeLast(hp1) then
  259. begin
  260. WasLast:=True;
  261. GetNextInstruction(hp1,hp1);
  262. break;
  263. end
  264. else
  265. GetNextInstruction(hp1,hp1);
  266. end;
  267. if assigned(hp1) then
  268. begin
  269. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  270. begin
  271. if (l<=4) and (l>0) then
  272. begin
  273. condition:=inverse_cond(taicpu(p).condition);
  274. hp2:=p;
  275. GetNextInstruction(p,hp1);
  276. p:=hp1;
  277. repeat
  278. if hp1.typ=ait_instruction then
  279. taicpu(hp1).condition:=condition;
  280. if MustBeLast(hp1) then
  281. begin
  282. GetNextInstruction(hp1,hp1);
  283. break;
  284. end
  285. else
  286. GetNextInstruction(hp1,hp1);
  287. until not(assigned(hp1)) or
  288. not(CanBeCond(hp1)) or
  289. (hp1.typ=ait_label);
  290. { wait with removing else GetNextInstruction could
  291. ignore the label if it was the only usage in the
  292. jump moved away }
  293. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  294. asml.remove(hp2);
  295. hp2.free;
  296. continue;
  297. end;
  298. end
  299. else
  300. { do not perform further optimizations if there is inctructon
  301. in block #1 which can not be optimized.
  302. }
  303. if not WasLast then
  304. begin
  305. { check further for
  306. Bcc xxx
  307. <several instructions 1>
  308. B yyy
  309. xxx:
  310. <several instructions 2>
  311. yyy:
  312. }
  313. { hp2 points to jmp yyy }
  314. hp2:=hp1;
  315. { skip hp1 to xxx }
  316. GetNextInstruction(hp1, hp1);
  317. if assigned(hp2) and
  318. assigned(hp1) and
  319. (l<=3) and
  320. (hp2.typ=ait_instruction) and
  321. (taicpu(hp2).is_jmp) and
  322. (taicpu(hp2).condition=C_None) and
  323. { real label and jump, no further references to the
  324. label are allowed }
  325. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  326. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  327. begin
  328. l:=0;
  329. { skip hp1 to <several moves 2> }
  330. GetNextInstruction(hp1, hp1);
  331. while assigned(hp1) and
  332. CanBeCond(hp1) do
  333. begin
  334. inc(l);
  335. GetNextInstruction(hp1, hp1);
  336. end;
  337. { hp1 points to yyy: }
  338. if assigned(hp1) and
  339. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  340. begin
  341. condition:=inverse_cond(taicpu(p).condition);
  342. GetNextInstruction(p,hp1);
  343. hp3:=p;
  344. p:=hp1;
  345. repeat
  346. if hp1.typ=ait_instruction then
  347. taicpu(hp1).condition:=condition;
  348. GetNextInstruction(hp1,hp1);
  349. until not(assigned(hp1)) or
  350. not(CanBeCond(hp1));
  351. { hp2 is still at jmp yyy }
  352. GetNextInstruction(hp2,hp1);
  353. { hp2 is now at xxx: }
  354. condition:=inverse_cond(condition);
  355. GetNextInstruction(hp1,hp1);
  356. { hp1 is now at <several movs 2> }
  357. repeat
  358. taicpu(hp1).condition:=condition;
  359. GetNextInstruction(hp1,hp1);
  360. until not(assigned(hp1)) or
  361. not(CanBeCond(hp1)) or
  362. (hp1.typ=ait_label);
  363. {
  364. asml.remove(hp1.next)
  365. hp1.next.free;
  366. asml.remove(hp1);
  367. hp1.free;
  368. }
  369. { remove Bcc }
  370. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  371. asml.remove(hp3);
  372. hp3.free;
  373. { remove jmp }
  374. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  375. asml.remove(hp2);
  376. hp2.free;
  377. continue;
  378. end;
  379. end;
  380. end;
  381. end;
  382. end;
  383. end;
  384. end;
  385. end;
  386. p := tai(p.next)
  387. end;
  388. end;
  389. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  390. begin
  391. { TODO: Add optimizer code }
  392. end;
  393. begin
  394. casmoptimizer:=TCpuAsmOptimizer;
  395. End.