aoptcpu.pas 29 KB

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