aoptcpu.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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 cgbase, cpubase, aasmtai, aopt, aoptcpub, aoptobj;
  22. Type
  23. { TCpuAsmOptimizer }
  24. TCpuAsmOptimizer = class(TAsmOptimizer)
  25. { uses the same constructor as TAopObj }
  26. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  27. procedure PeepHoleOptPass2;override;
  28. Function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  29. procedure RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  30. function RegUsedAfterInstruction(reg: Tregister; p: tai;
  31. var AllUsedRegs: TAllUsedRegs): Boolean;
  32. End;
  33. TCpuPreRegallocScheduler = class(TAsmOptimizer)
  34. function PeepHoleOptPass1Cpu(var p: tai): boolean;override;
  35. end;
  36. TCpuThumb2AsmOptimizer = class(TCpuAsmOptimizer)
  37. { uses the same constructor as TAopObj }
  38. procedure PeepHoleOptPass2;override;
  39. End;
  40. Implementation
  41. uses
  42. cutils,
  43. verbose,
  44. cgutils,
  45. aasmbase,aasmdata,aasmcpu;
  46. function CanBeCond(p : tai) : boolean;
  47. begin
  48. result:=
  49. (p.typ=ait_instruction) and
  50. (taicpu(p).condition=C_None) and
  51. ((taicpu(p).opcode<>A_BLX) or
  52. (taicpu(p).oper[0]^.typ=top_reg));
  53. end;
  54. function RefsEqual(const r1, r2: treference): boolean;
  55. begin
  56. refsequal :=
  57. (r1.offset = r2.offset) and
  58. (r1.base = r2.base) and
  59. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  60. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  61. (r1.relsymbol = r2.relsymbol) and
  62. (r1.signindex = r2.signindex) and
  63. (r1.shiftimm = r2.shiftimm) and
  64. (r1.addressmode = r2.addressmode) and
  65. (r1.shiftmode = r2.shiftmode);
  66. end;
  67. function MatchInstruction(const instr: tai; const op: TAsmOp; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  68. begin
  69. result :=
  70. (instr.typ = ait_instruction) and
  71. (taicpu(instr).opcode = op) and
  72. ((cond = []) or (taicpu(instr).condition in cond)) and
  73. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  74. end;
  75. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  76. begin
  77. result := (oper1.typ = oper2.typ) and
  78. (
  79. ((oper1.typ = top_const) and (oper1.val = oper2.val)) or
  80. ((oper1.typ = top_reg) and (oper1.reg = oper2.reg)) or
  81. ((oper1.typ = top_conditioncode) and (oper1.cc = oper2.cc))
  82. );
  83. end;
  84. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  85. begin
  86. result := (oper.typ = top_reg) and (oper.reg = reg);
  87. end;
  88. procedure RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList);
  89. begin
  90. if (taicpu(movp).condition = C_EQ) and
  91. (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and
  92. (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
  93. begin
  94. asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
  95. asml.remove(movp);
  96. movp.free;
  97. end;
  98. end;
  99. function regLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  100. var
  101. p: taicpu;
  102. begin
  103. p := taicpu(hp);
  104. regLoadedWithNewValue :=
  105. (assigned(hp)) and
  106. (hp.typ = ait_instruction) and
  107. (not(p.opcode in [A_STR, A_STRB, A_STRH, A_CMP, A_CMN, A_TST, A_TEQ,
  108. A_B, A_BL, A_BX, A_BLX])) and
  109. (p.oper[0]^.typ = top_reg) and
  110. (p.oper[0]^.reg = reg);
  111. end;
  112. function instructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  113. var
  114. p: taicpu;
  115. i: longint;
  116. begin
  117. instructionLoadsFromReg := false;
  118. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  119. exit;
  120. p:=taicpu(hp);
  121. i:=1;
  122. {For these instructions we have to start on oper[0]}
  123. if (p.opcode in [A_STR, A_STRB, A_STRH, A_CMP, A_CMN, A_TST, A_TEQ,
  124. A_B, A_BL, A_BX, A_BLX]) then i:=0;
  125. while(i<p.ops) do
  126. begin
  127. case p.oper[I]^.typ of
  128. top_reg:
  129. instructionLoadsFromReg := p.oper[I]^.reg = reg;
  130. top_regset:
  131. instructionLoadsFromReg := (getsupreg(reg) in p.oper[I]^.regset^);
  132. top_shifterop:
  133. instructionLoadsFromReg := p.oper[I]^.shifterop^.rs = reg;
  134. top_ref:
  135. instructionLoadsFromReg :=
  136. (p.oper[I]^.ref^.base = reg) or
  137. (p.oper[I]^.ref^.index = reg);
  138. end;
  139. if instructionLoadsFromReg then exit; {Bailout if we found something}
  140. Inc(I);
  141. end;
  142. end;
  143. function TCpuAsmOptimizer.RegUsedAfterInstruction(reg: Tregister; p: tai;
  144. var AllUsedRegs: TAllUsedRegs): Boolean;
  145. begin
  146. AllUsedRegs[getregtype(reg)].Update(tai(p.Next));
  147. RegUsedAfterInstruction :=
  148. (AllUsedRegs[getregtype(reg)].IsUsed(reg)) and
  149. (not(getNextInstruction(p,p)) or
  150. instructionLoadsFromReg(reg,p) or
  151. not(regLoadedWithNewValue(reg,p)));
  152. end;
  153. procedure TCpuAsmOptimizer.RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  154. var
  155. TmpUsedRegs: TAllUsedRegs;
  156. begin
  157. if MatchInstruction(movp, A_MOV, [taicpu(p).condition], [PF_None]) and
  158. (taicpu(movp).ops=2) and {We can't optimize if there is a shiftop}
  159. MatchOperand(taicpu(movp).oper[1]^, taicpu(p).oper[0]^.reg) then
  160. begin
  161. CopyUsedRegs(TmpUsedRegs);
  162. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  163. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,movp,TmpUsedRegs)) then
  164. begin
  165. asml.insertbefore(tai_comment.Create(strpnew('Peephole '+optimizer+' removed superfluous mov')), movp);
  166. taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
  167. asml.remove(movp);
  168. movp.free;
  169. end;
  170. ReleaseUsedRegs(TmpUsedRegs);
  171. end;
  172. end;
  173. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  174. var
  175. hp1,hp2: tai;
  176. i: longint;
  177. begin
  178. result := false;
  179. case p.typ of
  180. ait_instruction:
  181. begin
  182. (* optimization proved not to be safe, see tw4768.pp
  183. {
  184. change
  185. <op> reg,x,y
  186. cmp reg,#0
  187. into
  188. <op>s reg,x,y
  189. }
  190. { this optimization can applied only to the currently enabled operations because
  191. the other operations do not update all flags and FPC does not track flag usage }
  192. 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
  193. (taicpu(p).oppostfix = PF_None) and
  194. (taicpu(p).condition = C_None) and
  195. GetNextInstruction(p, hp1) and
  196. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  197. (taicpu(hp1).oper[1]^.typ = top_const) and
  198. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  199. (taicpu(hp1).oper[1]^.val = 0) { and
  200. GetNextInstruction(hp1, hp2) and
  201. (tai(hp2).typ = ait_instruction) and
  202. // be careful here, following instructions could use other flags
  203. // however after a jump fpc never depends on the value of flags
  204. (taicpu(hp2).opcode = A_B) and
  205. (taicpu(hp2).condition in [C_EQ,C_NE,C_MI,C_PL])} then
  206. begin
  207. taicpu(p).oppostfix:=PF_S;
  208. asml.remove(hp1);
  209. hp1.free;
  210. end
  211. else
  212. *)
  213. case taicpu(p).opcode of
  214. A_STR,
  215. A_STRH,
  216. A_STRB:
  217. begin
  218. { change
  219. str reg1,ref
  220. ldr reg2,ref
  221. into
  222. str reg1,ref
  223. mov reg2,reg1
  224. }
  225. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  226. GetNextInstruction(p,hp1) and
  227. (
  228. ( (taicpu(p).opcode = A_STR) and
  229. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [PF_None])
  230. ) or
  231. ( (taicpu(p).opcode = A_STRH) and
  232. MatchInstruction(hp1, A_LDRH, [taicpu(p).condition, C_None], [PF_None])
  233. ) or
  234. ( (taicpu(p).opcode = A_STRB) and
  235. MatchInstruction(hp1, A_LDRB, [taicpu(p).condition, C_None], [PF_None])
  236. )
  237. ) and
  238. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  239. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  240. begin
  241. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  242. begin
  243. asml.remove(hp1);
  244. hp1.free;
  245. end
  246. else
  247. begin
  248. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov done')), hp1);
  249. taicpu(hp1).opcode:=A_MOV;
  250. taicpu(hp1).oppostfix:=PF_None;
  251. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  252. end;
  253. result := true;
  254. end;
  255. end;
  256. A_LDR,
  257. A_LDRH,
  258. A_LDRB,
  259. A_LDRSH,
  260. A_LDRSB:
  261. begin
  262. { change
  263. ldr reg1,ref
  264. ldr reg2,ref
  265. into
  266. ldr reg1,ref
  267. mov reg2,reg1
  268. }
  269. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  270. GetNextInstruction(p,hp1) and
  271. MatchInstruction(hp1, taicpu(p).opcode, [taicpu(p).condition, C_None], [PF_None]) and
  272. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  273. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  274. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  275. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  276. begin
  277. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  278. begin
  279. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2Ldr done')), hp1);
  280. asml.remove(hp1);
  281. hp1.free;
  282. end
  283. else
  284. begin
  285. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2LdrMov done')), hp1);
  286. taicpu(hp1).opcode:=A_MOV;
  287. taicpu(hp1).oppostfix:=PF_None;
  288. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  289. end;
  290. result := true;
  291. end;
  292. { Remove superfluous mov after ldr
  293. changes
  294. ldr reg1, ref
  295. mov reg2, reg1
  296. to
  297. ldr reg2, ref
  298. conditions are:
  299. * reg1 must be released after mov
  300. * mov can not contain shifterops
  301. * ldr+mov have the same conditions
  302. * mov does not set flags
  303. }
  304. if GetNextInstruction(p, hp1) then
  305. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr');
  306. end;
  307. A_MOV:
  308. begin
  309. { fold
  310. mov reg1,reg0, shift imm1
  311. mov reg1,reg1, shift imm2
  312. to
  313. mov reg1,reg0, shift imm1+imm2
  314. }
  315. if (taicpu(p).ops=3) and
  316. (taicpu(p).oper[2]^.typ = top_shifterop) and
  317. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  318. getnextinstruction(p,hp1) and
  319. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  320. (taicpu(hp1).ops=3) and
  321. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  322. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  323. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  324. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) and
  325. (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) then
  326. begin
  327. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  328. { avoid overflows }
  329. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  330. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  331. SM_ROR:
  332. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  333. SM_ASR:
  334. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  335. SM_LSR,
  336. SM_LSL:
  337. begin
  338. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  339. InsertLLItem(p.previous, p.next, hp1);
  340. p.free;
  341. p:=hp1;
  342. end;
  343. else
  344. internalerror(2008072803);
  345. end;
  346. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShift2Shift done')), p);
  347. asml.remove(hp1);
  348. hp1.free;
  349. result := true;
  350. end;
  351. {
  352. This changes the very common
  353. mov r0, #0
  354. str r0, [...]
  355. mov r0, #0
  356. str r0, [...]
  357. and removes all superfluous mov instructions
  358. }
  359. if (taicpu(p).ops = 2) and
  360. (taicpu(p).oper[1]^.typ = top_const) and
  361. GetNextInstruction(p,hp1) then
  362. begin
  363. while (tai(p).typ = ait_instruction) and
  364. (taicpu(p).opcode in [A_STR, A_STRH, A_STRB]) and
  365. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) and
  366. GetNextInstruction(hp1, hp2) and
  367. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  368. (taicpu(hp2).ops = 2) and
  369. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  370. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  371. begin
  372. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovStrMov done')), hp2);
  373. GetNextInstruction(hp2,hp1);
  374. asml.remove(hp2);
  375. hp2.free;
  376. if not assigned(hp1) then break;
  377. end;
  378. end;
  379. {
  380. change
  381. mov r1, r0
  382. add r1, r1, #1
  383. to
  384. add r1, r0, #1
  385. Todo: Make it work for mov+cmp too
  386. }
  387. if (taicpu(p).ops = 2) and
  388. (taicpu(p).oper[1]^.typ = top_reg) and
  389. (taicpu(p).oppostfix = PF_NONE) and
  390. GetNextInstruction(p, hp1) and
  391. (tai(hp1).typ = ait_instruction) and
  392. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  393. A_AND, A_BIC, A_EOR, A_ORR]) and
  394. (taicpu(hp1).condition in [C_NONE, taicpu(hp1).condition]) and
  395. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  396. (taicpu(hp1).oper[1]^.typ = top_reg) and
  397. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const]) then
  398. begin
  399. { When we get here we still don't know if the registers match}
  400. for I:=1 to 2 do
  401. {
  402. If the first loop was successful p will be replaced with hp1.
  403. The checks will still be ok, because all required information
  404. will also be in hp1 then.
  405. }
  406. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  407. begin
  408. asml.insertbefore(tai_comment.Create(strpnew('Peephole RedundantMovProcess done ')), hp1);
  409. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  410. if p<>hp1 then
  411. begin
  412. asml.remove(p);
  413. p.free;
  414. p:=hp1;
  415. end;
  416. end;
  417. end;
  418. end;
  419. A_ADD,
  420. A_ADC,
  421. A_RSB,
  422. A_RSC,
  423. A_SUB,
  424. A_SBC,
  425. A_AND,
  426. A_BIC,
  427. A_EOR,
  428. A_ORR:
  429. begin
  430. {
  431. change
  432. and reg2,reg1,const1
  433. and reg2,reg2,const2
  434. to
  435. and reg2,reg1,(const1 and const2)
  436. }
  437. if (taicpu(p).opcode = A_AND) and
  438. (taicpu(p).oper[1]^.typ = top_reg) and
  439. (taicpu(p).oper[2]^.typ = top_const) and
  440. GetNextInstruction(p, hp1) and
  441. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  442. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  443. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  444. (taicpu(hp1).oper[2]^.typ = top_const) then
  445. begin
  446. asml.insertbefore(tai_comment.Create(strpnew('Peephole AndAnd2And done')), p);
  447. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  448. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  449. asml.remove(hp1);
  450. hp1.free;
  451. end;
  452. {
  453. change
  454. add reg1, ...
  455. mov reg2, reg1
  456. to
  457. add reg2, ...
  458. }
  459. if GetNextInstruction(p, hp1) then
  460. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  461. end;
  462. A_CMP:
  463. begin
  464. {
  465. change
  466. cmp reg,const1
  467. moveq reg,const1
  468. movne reg,const2
  469. to
  470. cmp reg,const1
  471. movne reg,const2
  472. }
  473. if (taicpu(p).oper[1]^.typ = top_const) and
  474. GetNextInstruction(p, hp1) and
  475. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  476. (taicpu(hp1).oper[1]^.typ = top_const) and
  477. GetNextInstruction(hp1, hp2) and
  478. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  479. (taicpu(hp1).oper[1]^.typ = top_const) then
  480. begin
  481. RemoveRedundantMove(p, hp1, asml);
  482. RemoveRedundantMove(p, hp2, asml);
  483. end;
  484. end;
  485. end;
  486. end;
  487. end;
  488. end;
  489. { instructions modifying the CPSR can be only the last instruction }
  490. function MustBeLast(p : tai) : boolean;
  491. begin
  492. Result:=(p.typ=ait_instruction) and
  493. ((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
  494. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  495. (taicpu(p).oppostfix=PF_S));
  496. end;
  497. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  498. var
  499. p,hp1,hp2: tai;
  500. l : longint;
  501. condition : tasmcond;
  502. hp3: tai;
  503. WasLast: boolean;
  504. { UsedRegs, TmpUsedRegs: TRegSet; }
  505. begin
  506. p := BlockStart;
  507. { UsedRegs := []; }
  508. while (p <> BlockEnd) Do
  509. begin
  510. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  511. case p.Typ Of
  512. Ait_Instruction:
  513. begin
  514. case taicpu(p).opcode Of
  515. A_B:
  516. if taicpu(p).condition<>C_None then
  517. begin
  518. { check for
  519. Bxx xxx
  520. <several instructions>
  521. xxx:
  522. }
  523. l:=0;
  524. WasLast:=False;
  525. GetNextInstruction(p, hp1);
  526. while assigned(hp1) and
  527. (l<=4) and
  528. CanBeCond(hp1) and
  529. { stop on labels }
  530. not(hp1.typ=ait_label) do
  531. begin
  532. inc(l);
  533. if MustBeLast(hp1) then
  534. begin
  535. WasLast:=True;
  536. GetNextInstruction(hp1,hp1);
  537. break;
  538. end
  539. else
  540. GetNextInstruction(hp1,hp1);
  541. end;
  542. if assigned(hp1) then
  543. begin
  544. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  545. begin
  546. if (l<=4) and (l>0) then
  547. begin
  548. condition:=inverse_cond(taicpu(p).condition);
  549. hp2:=p;
  550. GetNextInstruction(p,hp1);
  551. p:=hp1;
  552. repeat
  553. if hp1.typ=ait_instruction then
  554. taicpu(hp1).condition:=condition;
  555. if MustBeLast(hp1) then
  556. begin
  557. GetNextInstruction(hp1,hp1);
  558. break;
  559. end
  560. else
  561. GetNextInstruction(hp1,hp1);
  562. until not(assigned(hp1)) or
  563. not(CanBeCond(hp1)) or
  564. (hp1.typ=ait_label);
  565. { wait with removing else GetNextInstruction could
  566. ignore the label if it was the only usage in the
  567. jump moved away }
  568. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  569. asml.remove(hp2);
  570. hp2.free;
  571. continue;
  572. end;
  573. end
  574. else
  575. { do not perform further optimizations if there is inctructon
  576. in block #1 which can not be optimized.
  577. }
  578. if not WasLast then
  579. begin
  580. { check further for
  581. Bcc xxx
  582. <several instructions 1>
  583. B yyy
  584. xxx:
  585. <several instructions 2>
  586. yyy:
  587. }
  588. { hp2 points to jmp yyy }
  589. hp2:=hp1;
  590. { skip hp1 to xxx }
  591. GetNextInstruction(hp1, hp1);
  592. if assigned(hp2) and
  593. assigned(hp1) and
  594. (l<=3) and
  595. (hp2.typ=ait_instruction) and
  596. (taicpu(hp2).is_jmp) and
  597. (taicpu(hp2).condition=C_None) and
  598. { real label and jump, no further references to the
  599. label are allowed }
  600. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  601. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  602. begin
  603. l:=0;
  604. { skip hp1 to <several moves 2> }
  605. GetNextInstruction(hp1, hp1);
  606. while assigned(hp1) and
  607. CanBeCond(hp1) do
  608. begin
  609. inc(l);
  610. GetNextInstruction(hp1, hp1);
  611. end;
  612. { hp1 points to yyy: }
  613. if assigned(hp1) and
  614. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  615. begin
  616. condition:=inverse_cond(taicpu(p).condition);
  617. GetNextInstruction(p,hp1);
  618. hp3:=p;
  619. p:=hp1;
  620. repeat
  621. if hp1.typ=ait_instruction then
  622. taicpu(hp1).condition:=condition;
  623. GetNextInstruction(hp1,hp1);
  624. until not(assigned(hp1)) or
  625. not(CanBeCond(hp1));
  626. { hp2 is still at jmp yyy }
  627. GetNextInstruction(hp2,hp1);
  628. { hp2 is now at xxx: }
  629. condition:=inverse_cond(condition);
  630. GetNextInstruction(hp1,hp1);
  631. { hp1 is now at <several movs 2> }
  632. repeat
  633. taicpu(hp1).condition:=condition;
  634. GetNextInstruction(hp1,hp1);
  635. until not(assigned(hp1)) or
  636. not(CanBeCond(hp1)) or
  637. (hp1.typ=ait_label);
  638. {
  639. asml.remove(hp1.next)
  640. hp1.next.free;
  641. asml.remove(hp1);
  642. hp1.free;
  643. }
  644. { remove Bcc }
  645. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  646. asml.remove(hp3);
  647. hp3.free;
  648. { remove jmp }
  649. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  650. asml.remove(hp2);
  651. hp2.free;
  652. continue;
  653. end;
  654. end;
  655. end;
  656. end;
  657. end;
  658. end;
  659. end;
  660. end;
  661. p := tai(p.next)
  662. end;
  663. end;
  664. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  665. begin
  666. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  667. Result:=true
  668. else
  669. Result:=inherited RegInInstruction(Reg, p1);
  670. end;
  671. const
  672. { set of opcode which might or do write to memory }
  673. { TODO : extend armins.dat to contain r/w info }
  674. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  675. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  676. function TCpuPreRegallocScheduler.PeepHoleOptPass1Cpu(var p: tai): boolean;
  677. { TODO : schedule also forward }
  678. { TODO : schedule distance > 1 }
  679. var
  680. hp1,hp2,hp3,hp4,hp5 : tai;
  681. list : TAsmList;
  682. begin
  683. result:=true;
  684. list:=TAsmList.Create;
  685. p := BlockStart;
  686. { UsedRegs := []; }
  687. while (p <> BlockEnd) Do
  688. begin
  689. if (p.typ=ait_instruction) and
  690. GetNextInstruction(p,hp1) and
  691. (hp1.typ=ait_instruction) and
  692. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  693. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  694. not(RegModifiedByInstruction(NR_PC,p)) and
  695. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH])
  696. ) or
  697. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  698. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  699. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  700. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  701. (taicpu(hp1).oper[1]^.ref^.offset=0)
  702. )
  703. ) or
  704. { try to prove that the memory accesses don't overlapp }
  705. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  706. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  707. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  708. (taicpu(p).oppostfix=PF_None) and
  709. (taicpu(hp1).oppostfix=PF_None) and
  710. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  711. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  712. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  713. (abs(taicpu(p).oper[1]^.ref^.offset-taicpu(hp1).oper[1]^.ref^.offset)>=max(tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)],tcgsize2size[reg_cgsize(taicpu(hp1).oper[0]^.reg)]))
  714. )
  715. )
  716. ) and
  717. GetNextInstruction(hp1,hp2) and
  718. (hp2.typ=ait_instruction) and
  719. { loaded register used by next instruction? }
  720. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  721. { loaded register not used by previous instruction? }
  722. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  723. { same condition? }
  724. (taicpu(p).condition=taicpu(hp1).condition) and
  725. { first instruction might not change the register used as base }
  726. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  727. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  728. ) and
  729. { first instruction might not change the register used as index }
  730. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  731. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  732. ) then
  733. begin
  734. hp3:=tai(p.Previous);
  735. hp5:=tai(p.next);
  736. asml.Remove(p);
  737. { if there is a reg. dealloc instruction associated with p, move it together with p }
  738. { before the instruction? }
  739. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  740. begin
  741. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  742. RegInInstruction(tai_regalloc(hp3).reg,p) then
  743. begin
  744. hp4:=hp3;
  745. hp3:=tai(hp3.Previous);
  746. asml.Remove(hp4);
  747. list.Concat(hp4);
  748. end
  749. else
  750. hp3:=tai(hp3.Previous);
  751. end;
  752. list.Concat(p);
  753. { after the instruction? }
  754. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  755. begin
  756. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  757. RegInInstruction(tai_regalloc(hp5).reg,p) then
  758. begin
  759. hp4:=hp5;
  760. hp5:=tai(hp5.next);
  761. asml.Remove(hp4);
  762. list.Concat(hp4);
  763. end
  764. else
  765. hp5:=tai(hp5.Next);
  766. end;
  767. asml.Remove(hp1);
  768. {$ifdef DEBUG_PREREGSCHEDULER}
  769. asml.InsertBefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  770. {$endif DEBUG_PREREGSCHEDULER}
  771. asml.InsertBefore(hp1,hp2);
  772. asml.InsertListBefore(hp2,list);
  773. end;
  774. p := tai(p.next)
  775. end;
  776. list.Free;
  777. end;
  778. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  779. begin
  780. { TODO: Add optimizer code }
  781. end;
  782. begin
  783. casmoptimizer:=TCpuAsmOptimizer;
  784. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  785. End.