aoptcpu.pas 32 KB

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