aoptcpu.pas 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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_PLD) and
  52. ((taicpu(p).opcode<>A_BLX) or
  53. (taicpu(p).oper[0]^.typ=top_reg));
  54. end;
  55. function RefsEqual(const r1, r2: treference): boolean;
  56. begin
  57. refsequal :=
  58. (r1.offset = r2.offset) and
  59. (r1.base = r2.base) and
  60. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  61. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  62. (r1.relsymbol = r2.relsymbol) and
  63. (r1.signindex = r2.signindex) and
  64. (r1.shiftimm = r2.shiftimm) and
  65. (r1.addressmode = r2.addressmode) and
  66. (r1.shiftmode = r2.shiftmode);
  67. end;
  68. function MatchInstruction(const instr: tai; const op: TAsmOp; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  69. begin
  70. result :=
  71. (instr.typ = ait_instruction) and
  72. (taicpu(instr).opcode = op) and
  73. ((cond = []) or (taicpu(instr).condition in cond)) and
  74. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  75. end;
  76. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  77. begin
  78. result := oper1.typ = oper2.typ;
  79. if result then
  80. case oper1.typ of
  81. top_const:
  82. Result:=oper1.val = oper2.val;
  83. top_reg:
  84. Result:=oper1.reg = oper2.reg;
  85. top_conditioncode:
  86. Result:=oper1.cc = oper2.cc;
  87. top_ref:
  88. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  89. else Result:=false;
  90. end
  91. end;
  92. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  93. begin
  94. result := (oper.typ = top_reg) and (oper.reg = reg);
  95. end;
  96. procedure RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList);
  97. begin
  98. if (taicpu(movp).condition = C_EQ) and
  99. (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and
  100. (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
  101. begin
  102. asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
  103. asml.remove(movp);
  104. movp.free;
  105. end;
  106. end;
  107. function regLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  108. var
  109. p: taicpu;
  110. begin
  111. p := taicpu(hp);
  112. regLoadedWithNewValue := false;
  113. if not ((assigned(hp)) and (hp.typ = ait_instruction)) then
  114. exit;
  115. case p.opcode of
  116. { These operands do not write into a register at all }
  117. A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX, A_SWI, A_MSR, A_PLD:
  118. exit;
  119. {Take care of post/preincremented store and loads, they will change their base register}
  120. A_STR, A_LDR:
  121. regLoadedWithNewValue :=
  122. (taicpu(p).oper[1]^.typ=top_ref) and
  123. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  124. (taicpu(p).oper[1]^.ref^.base = reg);
  125. { These four are writing into the first 2 register, UMLAL and SMLAL will also read from them }
  126. A_UMLAL, A_UMULL, A_SMLAL, A_SMULL:
  127. regLoadedWithNewValue :=
  128. (p.oper[1]^.typ = top_reg) and
  129. (p.oper[1]^.reg = reg);
  130. {Loads to oper2 from coprocessor}
  131. {
  132. MCR/MRC is currently not supported in FPC
  133. A_MRC:
  134. regLoadedWithNewValue :=
  135. (p.oper[2]^.typ = top_reg) and
  136. (p.oper[2]^.reg = reg);
  137. }
  138. {Loads to all register in the registerset}
  139. A_LDM:
  140. regLoadedWithNewValue := (getsupreg(reg) in p.oper[1]^.regset^);
  141. end;
  142. if regLoadedWithNewValue then
  143. exit;
  144. case p.oper[0]^.typ of
  145. {This is the case}
  146. top_reg:
  147. regLoadedWithNewValue := (p.oper[0]^.reg = reg);
  148. {LDM/STM might write a new value to their index register}
  149. top_ref:
  150. regLoadedWithNewValue :=
  151. (taicpu(p).oper[0]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  152. (taicpu(p).oper[0]^.ref^.base = reg);
  153. end;
  154. end;
  155. function instructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  156. var
  157. p: taicpu;
  158. i: longint;
  159. begin
  160. instructionLoadsFromReg := false;
  161. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  162. exit;
  163. p:=taicpu(hp);
  164. i:=1;
  165. {For these instructions we have to start on oper[0]}
  166. if (p.opcode in [A_STR, A_LDM, A_STM, A_PLD,
  167. A_CMP, A_CMN, A_TST, A_TEQ,
  168. A_B, A_BL, A_BX, A_BLX,
  169. A_SMLAL, A_UMLAL]) then i:=0;
  170. while(i<p.ops) do
  171. begin
  172. case p.oper[I]^.typ of
  173. top_reg:
  174. instructionLoadsFromReg := p.oper[I]^.reg = reg;
  175. top_regset:
  176. instructionLoadsFromReg := (getsupreg(reg) in p.oper[I]^.regset^);
  177. top_shifterop:
  178. instructionLoadsFromReg := p.oper[I]^.shifterop^.rs = reg;
  179. top_ref:
  180. instructionLoadsFromReg :=
  181. (p.oper[I]^.ref^.base = reg) or
  182. (p.oper[I]^.ref^.index = reg);
  183. end;
  184. if instructionLoadsFromReg then exit; {Bailout if we found something}
  185. Inc(I);
  186. end;
  187. end;
  188. function TCpuAsmOptimizer.RegUsedAfterInstruction(reg: Tregister; p: tai;
  189. var AllUsedRegs: TAllUsedRegs): Boolean;
  190. begin
  191. AllUsedRegs[getregtype(reg)].Update(tai(p.Next));
  192. RegUsedAfterInstruction :=
  193. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  194. not(regLoadedWithNewValue(reg,p)) and
  195. (
  196. not(GetNextInstruction(p,p)) or
  197. instructionLoadsFromReg(reg,p) or
  198. not(regLoadedWithNewValue(reg,p))
  199. );
  200. end;
  201. procedure TCpuAsmOptimizer.RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  202. var
  203. TmpUsedRegs: TAllUsedRegs;
  204. begin
  205. if MatchInstruction(movp, A_MOV, [taicpu(p).condition], [PF_None]) and
  206. (taicpu(movp).ops=2) and {We can't optimize if there is a shiftop}
  207. MatchOperand(taicpu(movp).oper[1]^, taicpu(p).oper[0]^.reg) and
  208. {There is a special requirement for MUL and MLA, oper[0] and oper[1] are not allowed to be the same}
  209. not (
  210. (taicpu(p).opcode in [A_MLA, A_MUL]) and
  211. (taicpu(p).oper[1]^.reg = taicpu(movp).oper[0]^.reg)
  212. ) then
  213. begin
  214. CopyUsedRegs(TmpUsedRegs);
  215. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  216. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,movp,TmpUsedRegs)) then
  217. begin
  218. asml.insertbefore(tai_comment.Create(strpnew('Peephole '+optimizer+' removed superfluous mov')), movp);
  219. taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
  220. asml.remove(movp);
  221. movp.free;
  222. end;
  223. ReleaseUsedRegs(TmpUsedRegs);
  224. end;
  225. end;
  226. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  227. var
  228. hp1,hp2: tai;
  229. i: longint;
  230. TmpUsedRegs: TAllUsedRegs;
  231. tempop: tasmop;
  232. function IsPowerOf2(const value: DWord): boolean; inline;
  233. begin
  234. Result:=(value and (value - 1)) = 0;
  235. end;
  236. begin
  237. result := false;
  238. case p.typ of
  239. ait_instruction:
  240. begin
  241. (* optimization proved not to be safe, see tw4768.pp
  242. {
  243. change
  244. <op> reg,x,y
  245. cmp reg,#0
  246. into
  247. <op>s reg,x,y
  248. }
  249. { this optimization can applied only to the currently enabled operations because
  250. the other operations do not update all flags and FPC does not track flag usage }
  251. 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
  252. (taicpu(p).oppostfix = PF_None) and
  253. (taicpu(p).condition = C_None) and
  254. GetNextInstruction(p, hp1) and
  255. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  256. (taicpu(hp1).oper[1]^.typ = top_const) and
  257. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  258. (taicpu(hp1).oper[1]^.val = 0) { and
  259. GetNextInstruction(hp1, hp2) and
  260. (tai(hp2).typ = ait_instruction) and
  261. // be careful here, following instructions could use other flags
  262. // however after a jump fpc never depends on the value of flags
  263. (taicpu(hp2).opcode = A_B) and
  264. (taicpu(hp2).condition in [C_EQ,C_NE,C_MI,C_PL])} then
  265. begin
  266. taicpu(p).oppostfix:=PF_S;
  267. asml.remove(hp1);
  268. hp1.free;
  269. end
  270. else
  271. *)
  272. case taicpu(p).opcode of
  273. A_STR:
  274. begin
  275. { change
  276. str reg1,ref
  277. ldr reg2,ref
  278. into
  279. str reg1,ref
  280. mov reg2,reg1
  281. }
  282. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  283. (taicpu(p).oppostfix=PF_None) and
  284. GetNextInstruction(p,hp1) and
  285. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [PF_None]) and
  286. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  287. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  288. begin
  289. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  290. begin
  291. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 1 done')), hp1);
  292. asml.remove(hp1);
  293. hp1.free;
  294. end
  295. else
  296. begin
  297. taicpu(hp1).opcode:=A_MOV;
  298. taicpu(hp1).oppostfix:=PF_None;
  299. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  300. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 2 done')), hp1);
  301. end;
  302. result := true;
  303. end;
  304. end;
  305. A_LDR:
  306. begin
  307. { change
  308. ldr reg1,ref
  309. ldr reg2,ref
  310. into
  311. ldr reg1,ref
  312. mov reg2,reg1
  313. }
  314. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  315. GetNextInstruction(p,hp1) and
  316. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix]) and
  317. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  318. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  319. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  320. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  321. begin
  322. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  323. begin
  324. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2Ldr done')), hp1);
  325. asml.remove(hp1);
  326. hp1.free;
  327. end
  328. else
  329. begin
  330. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2LdrMov done')), hp1);
  331. taicpu(hp1).opcode:=A_MOV;
  332. taicpu(hp1).oppostfix:=PF_None;
  333. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  334. end;
  335. result := true;
  336. end;
  337. { Remove superfluous mov after ldr
  338. changes
  339. ldr reg1, ref
  340. mov reg2, reg1
  341. to
  342. ldr reg2, ref
  343. conditions are:
  344. * reg1 must be released after mov
  345. * mov can not contain shifterops
  346. * ldr+mov have the same conditions
  347. * mov does not set flags
  348. }
  349. if GetNextInstruction(p, hp1) then
  350. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr');
  351. end;
  352. A_MOV:
  353. begin
  354. { fold
  355. mov reg1,reg0, shift imm1
  356. mov reg1,reg1, shift imm2
  357. }
  358. if (taicpu(p).ops=3) and
  359. (taicpu(p).oper[2]^.typ = top_shifterop) and
  360. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  361. getnextinstruction(p,hp1) and
  362. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  363. (taicpu(hp1).ops=3) and
  364. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  365. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  366. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  367. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  368. begin
  369. { fold
  370. mov reg1,reg0, lsl 16
  371. mov reg1,reg1, lsr 16
  372. strh reg1, ...
  373. dealloc reg1
  374. to
  375. strh reg1, ...
  376. dealloc reg1
  377. }
  378. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  379. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  380. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  381. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  382. getnextinstruction(hp1,hp2) and
  383. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  384. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  385. begin
  386. CopyUsedRegs(TmpUsedRegs);
  387. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  388. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  389. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  390. begin
  391. asml.insertbefore(tai_comment.Create(strpnew('Peephole optimizer removed superfluous 16 Bit zero extension')), hp1);
  392. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  393. asml.remove(p);
  394. asml.remove(hp1);
  395. p.free;
  396. hp1.free;
  397. p:=hp2;
  398. end;
  399. ReleaseUsedRegs(TmpUsedRegs);
  400. end
  401. { fold
  402. mov reg1,reg0, shift imm1
  403. mov reg1,reg1, shift imm2
  404. to
  405. mov reg1,reg0, shift imm1+imm2
  406. }
  407. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) or
  408. { asr makes no use after a lsr, the asr can be foled into the lsr }
  409. ((taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSR) and (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_ASR) ) then
  410. begin
  411. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  412. { avoid overflows }
  413. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  414. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  415. SM_ROR:
  416. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  417. SM_ASR:
  418. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  419. SM_LSR,
  420. SM_LSL:
  421. begin
  422. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  423. InsertLLItem(p.previous, p.next, hp1);
  424. p.free;
  425. p:=hp1;
  426. end;
  427. else
  428. internalerror(2008072803);
  429. end;
  430. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShift2Shift 1 done')), p);
  431. asml.remove(hp1);
  432. hp1.free;
  433. result := true;
  434. end
  435. { fold
  436. mov reg1,reg0, shift imm1
  437. mov reg1,reg1, shift imm2
  438. mov reg1,reg1, shift imm3 ...
  439. }
  440. else if getnextinstruction(hp1,hp2) and
  441. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  442. (taicpu(hp2).ops=3) and
  443. MatchOperand(taicpu(hp2).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  444. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  445. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  446. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  447. begin
  448. { mov reg1,reg0, lsl imm1
  449. mov reg1,reg1, lsr/asr imm2
  450. mov reg1,reg1, lsl imm3 ...
  451. if imm3<=imm1 and imm2>=imm3
  452. to
  453. mov reg1,reg0, lsl imm1
  454. mov reg1,reg1, lsr/asr imm2-imm3
  455. }
  456. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  457. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  458. (taicpu(hp2).oper[2]^.shifterop^.shiftimm<=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  459. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(hp2).oper[2]^.shifterop^.shiftimm) then
  460. begin
  461. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  462. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShiftShift2ShiftShift 1 done')), p);
  463. asml.remove(hp2);
  464. hp2.free;
  465. result := true;
  466. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  467. begin
  468. asml.remove(hp1);
  469. hp1.free;
  470. end;
  471. end
  472. end;
  473. end;
  474. { Change the common
  475. mov r0, r0, lsr #24
  476. and r0, r0, #255
  477. and remove the superfluous and
  478. This could be extended to handle more cases.
  479. }
  480. if (taicpu(p).ops=3) and
  481. (taicpu(p).oper[2]^.typ = top_shifterop) and
  482. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  483. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  484. (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  485. getnextinstruction(p,hp1) and
  486. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  487. (taicpu(hp1).ops=3) and
  488. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  489. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  490. (taicpu(hp1).oper[2]^.typ = top_const) and
  491. { Check if the AND actually would only mask out bits beeing already zero because of the shift
  492. For LSR #25 and an AndConst of 255 that whould go like this:
  493. 255 and ((2 shl (32-25))-1)
  494. which results in 127, which is one less a power-of-2, meaning all lower bits are set.
  495. LSR #25 and AndConst of 254:
  496. 254 and ((2 shl (32-25))-1) = 126 -> lowest bit is clear, so we can't remove it.
  497. }
  498. ispowerof2((taicpu(hp1).oper[2]^.val and ((2 shl (32-taicpu(p).oper[2]^.shifterop^.shiftimm))-1))+1) then
  499. begin
  500. asml.insertbefore(tai_comment.Create(strpnew('Peephole LsrAnd2Lsr done')), hp1);
  501. asml.remove(hp1);
  502. hp1.free;
  503. end;
  504. {
  505. optimize
  506. mov rX, yyyy
  507. ....
  508. }
  509. if (taicpu(p).ops = 2) and
  510. GetNextInstruction(p,hp1) and
  511. (tai(hp1).typ = ait_instruction) then
  512. begin
  513. {
  514. This changes the very common
  515. mov r0, #0
  516. str r0, [...]
  517. mov r0, #0
  518. str r0, [...]
  519. and removes all superfluous mov instructions
  520. }
  521. if (taicpu(p).oper[1]^.typ = top_const) and
  522. (taicpu(hp1).opcode=A_STR) then
  523. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], []) and
  524. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  525. GetNextInstruction(hp1, hp2) and
  526. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  527. (taicpu(hp2).ops = 2) and
  528. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  529. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  530. begin
  531. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovStrMov done')), hp2);
  532. GetNextInstruction(hp2,hp1);
  533. asml.remove(hp2);
  534. hp2.free;
  535. if not assigned(hp1) then break;
  536. end
  537. {
  538. This removes the first mov from
  539. mov rX,...
  540. mov rX,...
  541. }
  542. else if taicpu(hp1).opcode=A_MOV then
  543. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  544. (taicpu(hp1).ops = 2) and
  545. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) do
  546. begin
  547. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovMov done')), p);
  548. asml.remove(p);
  549. p.free;
  550. p:=hp1;
  551. GetNextInstruction(hp1,hp1);
  552. if not assigned(hp1) then
  553. break;
  554. end;
  555. end;
  556. {
  557. change
  558. mov r1, r0
  559. add r1, r1, #1
  560. to
  561. add r1, r0, #1
  562. Todo: Make it work for mov+cmp too
  563. CAUTION! If this one is successful p might not be a mov instruction anymore!
  564. }
  565. if (taicpu(p).ops = 2) and
  566. (taicpu(p).oper[1]^.typ = top_reg) and
  567. (taicpu(p).oppostfix = PF_NONE) and
  568. GetNextInstruction(p, hp1) and
  569. (tai(hp1).typ = ait_instruction) and
  570. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  571. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN]) and
  572. {MOV and MVN might only have 2 ops}
  573. (taicpu(hp1).ops = 3) and
  574. (taicpu(hp1).condition in [C_NONE, taicpu(hp1).condition]) and
  575. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  576. (taicpu(hp1).oper[1]^.typ = top_reg) and
  577. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop]) then
  578. begin
  579. { When we get here we still don't know if the registers match}
  580. for I:=1 to 2 do
  581. {
  582. If the first loop was successful p will be replaced with hp1.
  583. The checks will still be ok, because all required information
  584. will also be in hp1 then.
  585. }
  586. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  587. begin
  588. asml.insertbefore(tai_comment.Create(strpnew('Peephole RedundantMovProcess done')), hp1);
  589. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  590. if p<>hp1 then
  591. begin
  592. asml.remove(p);
  593. p.free;
  594. p:=hp1;
  595. end;
  596. end;
  597. end;
  598. { This folds shifterops into following instructions
  599. mov r0, r1, lsl #8
  600. add r2, r3, r0
  601. to
  602. add r2, r3, r1, lsl #8
  603. CAUTION! If this one is successful p might not be a mov instruction anymore!
  604. }
  605. if (taicpu(p).opcode = A_MOV) and
  606. (taicpu(p).ops = 3) and
  607. (taicpu(p).oper[1]^.typ = top_reg) and
  608. (taicpu(p).oper[2]^.typ = top_shifterop) and
  609. (taicpu(p).oppostfix = PF_NONE) and
  610. GetNextInstruction(p, hp1) and
  611. (tai(hp1).typ = ait_instruction) and
  612. (taicpu(hp1).ops = 3) and {Currently we can't fold into another shifterop}
  613. (taicpu(hp1).oper[2]^.typ = top_reg) and
  614. (taicpu(hp1).oppostfix = PF_NONE) and
  615. (taicpu(hp1).condition = taicpu(p).condition) and
  616. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  617. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST]) and
  618. (
  619. {Only ONE of the two src operands is allowed to match}
  620. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) xor
  621. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[2]^)
  622. ) then
  623. begin
  624. CopyUsedRegs(TmpUsedRegs);
  625. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  626. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp1,TmpUsedRegs)) then
  627. for I:=1 to 2 do
  628. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  629. begin
  630. if I = 1 then
  631. begin
  632. {The SUB operators need to be changed when we swap parameters}
  633. case taicpu(hp1).opcode of
  634. A_SUB: tempop:=A_RSB;
  635. A_SBC: tempop:=A_RSC;
  636. A_RSB: tempop:=A_SUB;
  637. A_RSC: tempop:=A_SBC;
  638. else tempop:=taicpu(hp1).opcode;
  639. end;
  640. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  641. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  642. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^);
  643. end
  644. else
  645. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  646. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  647. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^);
  648. asml.insertbefore(hp2, p);
  649. asml.remove(p);
  650. asml.remove(hp1);
  651. p.free;
  652. hp1.free;
  653. p:=hp2;
  654. GetNextInstruction(p,hp1);
  655. asml.insertbefore(tai_comment.Create(strpnew('Peephole FoldShiftProcess done')), p);
  656. break;
  657. end;
  658. ReleaseUsedRegs(TmpUsedRegs);
  659. end;
  660. {
  661. Often we see shifts and then a superfluous mov to another register
  662. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  663. }
  664. if (taicpu(p).opcode = A_MOV) and
  665. GetNextInstruction(p, hp1) then
  666. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  667. end;
  668. A_ADD,
  669. A_ADC,
  670. A_RSB,
  671. A_RSC,
  672. A_SUB,
  673. A_SBC,
  674. A_AND,
  675. A_BIC,
  676. A_EOR,
  677. A_ORR,
  678. A_MLA,
  679. A_MUL:
  680. begin
  681. {
  682. change
  683. and reg2,reg1,const1
  684. and reg2,reg2,const2
  685. to
  686. and reg2,reg1,(const1 and const2)
  687. }
  688. if (taicpu(p).opcode = A_AND) and
  689. (taicpu(p).oper[1]^.typ = top_reg) and
  690. (taicpu(p).oper[2]^.typ = top_const) and
  691. GetNextInstruction(p, hp1) and
  692. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  693. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  694. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  695. (taicpu(hp1).oper[2]^.typ = top_const) then
  696. begin
  697. asml.insertbefore(tai_comment.Create(strpnew('Peephole AndAnd2And done')), p);
  698. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  699. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  700. asml.remove(hp1);
  701. hp1.free;
  702. end;
  703. {
  704. change
  705. add reg1, ...
  706. mov reg2, reg1
  707. to
  708. add reg2, ...
  709. }
  710. if GetNextInstruction(p, hp1) then
  711. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  712. end;
  713. A_CMP:
  714. begin
  715. {
  716. change
  717. cmp reg,const1
  718. moveq reg,const1
  719. movne reg,const2
  720. to
  721. cmp reg,const1
  722. movne reg,const2
  723. }
  724. if (taicpu(p).oper[1]^.typ = top_const) and
  725. GetNextInstruction(p, hp1) and
  726. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  727. (taicpu(hp1).oper[1]^.typ = top_const) and
  728. GetNextInstruction(hp1, hp2) and
  729. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  730. (taicpu(hp1).oper[1]^.typ = top_const) then
  731. begin
  732. RemoveRedundantMove(p, hp1, asml);
  733. RemoveRedundantMove(p, hp2, asml);
  734. end;
  735. end;
  736. end;
  737. end;
  738. end;
  739. end;
  740. { instructions modifying the CPSR can be only the last instruction }
  741. function MustBeLast(p : tai) : boolean;
  742. begin
  743. Result:=(p.typ=ait_instruction) and
  744. ((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
  745. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  746. (taicpu(p).oppostfix=PF_S));
  747. end;
  748. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  749. var
  750. p,hp1,hp2: tai;
  751. l : longint;
  752. condition : tasmcond;
  753. hp3: tai;
  754. WasLast: boolean;
  755. { UsedRegs, TmpUsedRegs: TRegSet; }
  756. begin
  757. p := BlockStart;
  758. { UsedRegs := []; }
  759. while (p <> BlockEnd) Do
  760. begin
  761. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  762. case p.Typ Of
  763. Ait_Instruction:
  764. begin
  765. case taicpu(p).opcode Of
  766. A_B:
  767. if taicpu(p).condition<>C_None then
  768. begin
  769. { check for
  770. Bxx xxx
  771. <several instructions>
  772. xxx:
  773. }
  774. l:=0;
  775. WasLast:=False;
  776. GetNextInstruction(p, hp1);
  777. while assigned(hp1) and
  778. (l<=4) and
  779. CanBeCond(hp1) and
  780. { stop on labels }
  781. not(hp1.typ=ait_label) do
  782. begin
  783. inc(l);
  784. if MustBeLast(hp1) then
  785. begin
  786. WasLast:=True;
  787. GetNextInstruction(hp1,hp1);
  788. break;
  789. end
  790. else
  791. GetNextInstruction(hp1,hp1);
  792. end;
  793. if assigned(hp1) then
  794. begin
  795. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  796. begin
  797. if (l<=4) and (l>0) then
  798. begin
  799. condition:=inverse_cond(taicpu(p).condition);
  800. hp2:=p;
  801. GetNextInstruction(p,hp1);
  802. p:=hp1;
  803. repeat
  804. if hp1.typ=ait_instruction then
  805. taicpu(hp1).condition:=condition;
  806. if MustBeLast(hp1) then
  807. begin
  808. GetNextInstruction(hp1,hp1);
  809. break;
  810. end
  811. else
  812. GetNextInstruction(hp1,hp1);
  813. until not(assigned(hp1)) or
  814. not(CanBeCond(hp1)) or
  815. (hp1.typ=ait_label);
  816. { wait with removing else GetNextInstruction could
  817. ignore the label if it was the only usage in the
  818. jump moved away }
  819. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  820. asml.remove(hp2);
  821. hp2.free;
  822. continue;
  823. end;
  824. end
  825. else
  826. { do not perform further optimizations if there is inctructon
  827. in block #1 which can not be optimized.
  828. }
  829. if not WasLast then
  830. begin
  831. { check further for
  832. Bcc xxx
  833. <several instructions 1>
  834. B yyy
  835. xxx:
  836. <several instructions 2>
  837. yyy:
  838. }
  839. { hp2 points to jmp yyy }
  840. hp2:=hp1;
  841. { skip hp1 to xxx }
  842. GetNextInstruction(hp1, hp1);
  843. if assigned(hp2) and
  844. assigned(hp1) and
  845. (l<=3) and
  846. (hp2.typ=ait_instruction) and
  847. (taicpu(hp2).is_jmp) and
  848. (taicpu(hp2).condition=C_None) and
  849. { real label and jump, no further references to the
  850. label are allowed }
  851. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  852. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  853. begin
  854. l:=0;
  855. { skip hp1 to <several moves 2> }
  856. GetNextInstruction(hp1, hp1);
  857. while assigned(hp1) and
  858. CanBeCond(hp1) do
  859. begin
  860. inc(l);
  861. GetNextInstruction(hp1, hp1);
  862. end;
  863. { hp1 points to yyy: }
  864. if assigned(hp1) and
  865. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  866. begin
  867. condition:=inverse_cond(taicpu(p).condition);
  868. GetNextInstruction(p,hp1);
  869. hp3:=p;
  870. p:=hp1;
  871. repeat
  872. if hp1.typ=ait_instruction then
  873. taicpu(hp1).condition:=condition;
  874. GetNextInstruction(hp1,hp1);
  875. until not(assigned(hp1)) or
  876. not(CanBeCond(hp1));
  877. { hp2 is still at jmp yyy }
  878. GetNextInstruction(hp2,hp1);
  879. { hp2 is now at xxx: }
  880. condition:=inverse_cond(condition);
  881. GetNextInstruction(hp1,hp1);
  882. { hp1 is now at <several movs 2> }
  883. repeat
  884. taicpu(hp1).condition:=condition;
  885. GetNextInstruction(hp1,hp1);
  886. until not(assigned(hp1)) or
  887. not(CanBeCond(hp1)) or
  888. (hp1.typ=ait_label);
  889. {
  890. asml.remove(hp1.next)
  891. hp1.next.free;
  892. asml.remove(hp1);
  893. hp1.free;
  894. }
  895. { remove Bcc }
  896. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  897. asml.remove(hp3);
  898. hp3.free;
  899. { remove jmp }
  900. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  901. asml.remove(hp2);
  902. hp2.free;
  903. continue;
  904. end;
  905. end;
  906. end;
  907. end;
  908. end;
  909. end;
  910. end;
  911. end;
  912. p := tai(p.next)
  913. end;
  914. end;
  915. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  916. begin
  917. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  918. Result:=true
  919. else
  920. Result:=inherited RegInInstruction(Reg, p1);
  921. end;
  922. const
  923. { set of opcode which might or do write to memory }
  924. { TODO : extend armins.dat to contain r/w info }
  925. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  926. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  927. function TCpuPreRegallocScheduler.PeepHoleOptPass1Cpu(var p: tai): boolean;
  928. { TODO : schedule also forward }
  929. { TODO : schedule distance > 1 }
  930. var
  931. hp1,hp2,hp3,hp4,hp5 : tai;
  932. list : TAsmList;
  933. begin
  934. result:=true;
  935. list:=TAsmList.Create;
  936. p := BlockStart;
  937. { UsedRegs := []; }
  938. while (p <> BlockEnd) Do
  939. begin
  940. if (p.typ=ait_instruction) and
  941. GetNextInstruction(p,hp1) and
  942. (hp1.typ=ait_instruction) and
  943. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  944. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  945. not(RegModifiedByInstruction(NR_PC,p)) and
  946. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH])
  947. ) or
  948. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  949. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  950. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  951. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  952. (taicpu(hp1).oper[1]^.ref^.offset=0)
  953. )
  954. ) or
  955. { try to prove that the memory accesses don't overlapp }
  956. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  957. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  958. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  959. (taicpu(p).oppostfix=PF_None) and
  960. (taicpu(hp1).oppostfix=PF_None) and
  961. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  962. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  963. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  964. (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)]))
  965. )
  966. )
  967. ) and
  968. GetNextInstruction(hp1,hp2) and
  969. (hp2.typ=ait_instruction) and
  970. { loaded register used by next instruction? }
  971. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  972. { loaded register not used by previous instruction? }
  973. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  974. { same condition? }
  975. (taicpu(p).condition=taicpu(hp1).condition) and
  976. { first instruction might not change the register used as base }
  977. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  978. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  979. ) and
  980. { first instruction might not change the register used as index }
  981. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  982. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  983. ) then
  984. begin
  985. hp3:=tai(p.Previous);
  986. hp5:=tai(p.next);
  987. asml.Remove(p);
  988. { if there is a reg. dealloc instruction associated with p, move it together with p }
  989. { before the instruction? }
  990. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  991. begin
  992. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  993. RegInInstruction(tai_regalloc(hp3).reg,p) then
  994. begin
  995. hp4:=hp3;
  996. hp3:=tai(hp3.Previous);
  997. asml.Remove(hp4);
  998. list.Concat(hp4);
  999. end
  1000. else
  1001. hp3:=tai(hp3.Previous);
  1002. end;
  1003. list.Concat(p);
  1004. { after the instruction? }
  1005. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  1006. begin
  1007. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  1008. RegInInstruction(tai_regalloc(hp5).reg,p) then
  1009. begin
  1010. hp4:=hp5;
  1011. hp5:=tai(hp5.next);
  1012. asml.Remove(hp4);
  1013. list.Concat(hp4);
  1014. end
  1015. else
  1016. hp5:=tai(hp5.Next);
  1017. end;
  1018. asml.Remove(hp1);
  1019. {$ifdef DEBUG_PREREGSCHEDULER}
  1020. asml.InsertBefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  1021. {$endif DEBUG_PREREGSCHEDULER}
  1022. asml.InsertBefore(hp1,hp2);
  1023. asml.InsertListBefore(hp2,list);
  1024. end;
  1025. p := tai(p.next)
  1026. end;
  1027. list.Free;
  1028. end;
  1029. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  1030. begin
  1031. { TODO: Add optimizer code }
  1032. end;
  1033. begin
  1034. casmoptimizer:=TCpuAsmOptimizer;
  1035. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  1036. End.