aoptcpu.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. { mov reg1,reg0, lsr/asr imm1
  473. mov reg1,reg1, lsl imm2
  474. mov reg1,reg1, lsr/asr imm3 ...
  475. if imm3>=imm1 and imm2>=imm1
  476. to
  477. mov reg1,reg0, lsl imm2-imm1
  478. mov reg1,reg1, lsr/asr imm3 ...
  479. }
  480. else if (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  481. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  482. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  483. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  484. begin
  485. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  486. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  487. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShiftShift2ShiftShift 2 done')), p);
  488. asml.remove(p);
  489. p.free;
  490. p:=hp2;
  491. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  492. begin
  493. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  494. asml.remove(hp1);
  495. hp1.free;
  496. p:=hp2;
  497. end;
  498. result := true;
  499. end;
  500. end;
  501. end;
  502. { Change the common
  503. mov r0, r0, lsr #24
  504. and r0, r0, #255
  505. and remove the superfluous and
  506. This could be extended to handle more cases.
  507. }
  508. if (taicpu(p).ops=3) and
  509. (taicpu(p).oper[2]^.typ = top_shifterop) and
  510. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  511. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  512. (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  513. getnextinstruction(p,hp1) and
  514. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  515. (taicpu(hp1).ops=3) and
  516. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  517. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  518. (taicpu(hp1).oper[2]^.typ = top_const) and
  519. { Check if the AND actually would only mask out bits beeing already zero because of the shift
  520. For LSR #25 and an AndConst of 255 that whould go like this:
  521. 255 and ((2 shl (32-25))-1)
  522. which results in 127, which is one less a power-of-2, meaning all lower bits are set.
  523. LSR #25 and AndConst of 254:
  524. 254 and ((2 shl (32-25))-1) = 126 -> lowest bit is clear, so we can't remove it.
  525. }
  526. ispowerof2((taicpu(hp1).oper[2]^.val and ((2 shl (32-taicpu(p).oper[2]^.shifterop^.shiftimm))-1))+1) then
  527. begin
  528. asml.insertbefore(tai_comment.Create(strpnew('Peephole LsrAnd2Lsr done')), hp1);
  529. asml.remove(hp1);
  530. hp1.free;
  531. end;
  532. {
  533. optimize
  534. mov rX, yyyy
  535. ....
  536. }
  537. if (taicpu(p).ops = 2) and
  538. GetNextInstruction(p,hp1) and
  539. (tai(hp1).typ = ait_instruction) then
  540. begin
  541. {
  542. This changes the very common
  543. mov r0, #0
  544. str r0, [...]
  545. mov r0, #0
  546. str r0, [...]
  547. and removes all superfluous mov instructions
  548. }
  549. if (taicpu(p).oper[1]^.typ = top_const) and
  550. (taicpu(hp1).opcode=A_STR) then
  551. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], []) and
  552. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  553. GetNextInstruction(hp1, hp2) and
  554. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  555. (taicpu(hp2).ops = 2) and
  556. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  557. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  558. begin
  559. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovStrMov done')), hp2);
  560. GetNextInstruction(hp2,hp1);
  561. asml.remove(hp2);
  562. hp2.free;
  563. if not assigned(hp1) then break;
  564. end
  565. {
  566. This removes the first mov from
  567. mov rX,...
  568. mov rX,...
  569. }
  570. else if taicpu(hp1).opcode=A_MOV then
  571. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  572. (taicpu(hp1).ops = 2) and
  573. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) do
  574. begin
  575. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovMov done')), p);
  576. asml.remove(p);
  577. p.free;
  578. p:=hp1;
  579. GetNextInstruction(hp1,hp1);
  580. if not assigned(hp1) then
  581. break;
  582. end;
  583. end;
  584. {
  585. change
  586. mov r1, r0
  587. add r1, r1, #1
  588. to
  589. add r1, r0, #1
  590. Todo: Make it work for mov+cmp too
  591. CAUTION! If this one is successful p might not be a mov instruction anymore!
  592. }
  593. if (taicpu(p).ops = 2) and
  594. (taicpu(p).oper[1]^.typ = top_reg) and
  595. (taicpu(p).oppostfix = PF_NONE) and
  596. GetNextInstruction(p, hp1) and
  597. (tai(hp1).typ = ait_instruction) and
  598. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  599. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN]) and
  600. {MOV and MVN might only have 2 ops}
  601. (taicpu(hp1).ops = 3) and
  602. (taicpu(hp1).condition in [C_NONE, taicpu(hp1).condition]) and
  603. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  604. (taicpu(hp1).oper[1]^.typ = top_reg) and
  605. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop]) then
  606. begin
  607. { When we get here we still don't know if the registers match}
  608. for I:=1 to 2 do
  609. {
  610. If the first loop was successful p will be replaced with hp1.
  611. The checks will still be ok, because all required information
  612. will also be in hp1 then.
  613. }
  614. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  615. begin
  616. asml.insertbefore(tai_comment.Create(strpnew('Peephole RedundantMovProcess done')), hp1);
  617. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  618. if p<>hp1 then
  619. begin
  620. asml.remove(p);
  621. p.free;
  622. p:=hp1;
  623. end;
  624. end;
  625. end;
  626. { This folds shifterops into following instructions
  627. mov r0, r1, lsl #8
  628. add r2, r3, r0
  629. to
  630. add r2, r3, r1, lsl #8
  631. CAUTION! If this one is successful p might not be a mov instruction anymore!
  632. }
  633. if (taicpu(p).opcode = A_MOV) and
  634. (taicpu(p).ops = 3) and
  635. (taicpu(p).oper[1]^.typ = top_reg) and
  636. (taicpu(p).oper[2]^.typ = top_shifterop) and
  637. (taicpu(p).oppostfix = PF_NONE) and
  638. GetNextInstruction(p, hp1) and
  639. (tai(hp1).typ = ait_instruction) and
  640. (taicpu(hp1).ops = 3) and {Currently we can't fold into another shifterop}
  641. (taicpu(hp1).oper[2]^.typ = top_reg) and
  642. (taicpu(hp1).oppostfix = PF_NONE) and
  643. (taicpu(hp1).condition = taicpu(p).condition) and
  644. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  645. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST]) and
  646. (
  647. {Only ONE of the two src operands is allowed to match}
  648. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) xor
  649. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[2]^)
  650. ) then
  651. begin
  652. CopyUsedRegs(TmpUsedRegs);
  653. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  654. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp1,TmpUsedRegs)) then
  655. for I:=1 to 2 do
  656. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  657. begin
  658. if I = 1 then
  659. begin
  660. {The SUB operators need to be changed when we swap parameters}
  661. case taicpu(hp1).opcode of
  662. A_SUB: tempop:=A_RSB;
  663. A_SBC: tempop:=A_RSC;
  664. A_RSB: tempop:=A_SUB;
  665. A_RSC: tempop:=A_SBC;
  666. else tempop:=taicpu(hp1).opcode;
  667. end;
  668. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  669. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  670. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^);
  671. end
  672. else
  673. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  674. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  675. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^);
  676. asml.insertbefore(hp2, p);
  677. asml.remove(p);
  678. asml.remove(hp1);
  679. p.free;
  680. hp1.free;
  681. p:=hp2;
  682. GetNextInstruction(p,hp1);
  683. asml.insertbefore(tai_comment.Create(strpnew('Peephole FoldShiftProcess done')), p);
  684. break;
  685. end;
  686. ReleaseUsedRegs(TmpUsedRegs);
  687. end;
  688. {
  689. Often we see shifts and then a superfluous mov to another register
  690. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  691. }
  692. if (taicpu(p).opcode = A_MOV) and
  693. GetNextInstruction(p, hp1) then
  694. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  695. end;
  696. A_ADD,
  697. A_ADC,
  698. A_RSB,
  699. A_RSC,
  700. A_SUB,
  701. A_SBC,
  702. A_AND,
  703. A_BIC,
  704. A_EOR,
  705. A_ORR,
  706. A_MLA,
  707. A_MUL:
  708. begin
  709. {
  710. change
  711. and reg2,reg1,const1
  712. and reg2,reg2,const2
  713. to
  714. and reg2,reg1,(const1 and const2)
  715. }
  716. if (taicpu(p).opcode = A_AND) and
  717. (taicpu(p).oper[1]^.typ = top_reg) and
  718. (taicpu(p).oper[2]^.typ = top_const) and
  719. GetNextInstruction(p, hp1) and
  720. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  721. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  722. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  723. (taicpu(hp1).oper[2]^.typ = top_const) then
  724. begin
  725. asml.insertbefore(tai_comment.Create(strpnew('Peephole AndAnd2And done')), p);
  726. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  727. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  728. asml.remove(hp1);
  729. hp1.free;
  730. end;
  731. {
  732. change
  733. add reg1, ...
  734. mov reg2, reg1
  735. to
  736. add reg2, ...
  737. }
  738. if GetNextInstruction(p, hp1) then
  739. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  740. end;
  741. A_CMP:
  742. begin
  743. {
  744. change
  745. cmp reg,const1
  746. moveq reg,const1
  747. movne reg,const2
  748. to
  749. cmp reg,const1
  750. movne reg,const2
  751. }
  752. if (taicpu(p).oper[1]^.typ = top_const) and
  753. GetNextInstruction(p, hp1) and
  754. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  755. (taicpu(hp1).oper[1]^.typ = top_const) and
  756. GetNextInstruction(hp1, hp2) and
  757. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  758. (taicpu(hp1).oper[1]^.typ = top_const) then
  759. begin
  760. RemoveRedundantMove(p, hp1, asml);
  761. RemoveRedundantMove(p, hp2, asml);
  762. end;
  763. end;
  764. end;
  765. end;
  766. end;
  767. end;
  768. { instructions modifying the CPSR can be only the last instruction }
  769. function MustBeLast(p : tai) : boolean;
  770. begin
  771. Result:=(p.typ=ait_instruction) and
  772. ((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
  773. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  774. (taicpu(p).oppostfix=PF_S));
  775. end;
  776. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  777. var
  778. p,hp1,hp2: tai;
  779. l : longint;
  780. condition : tasmcond;
  781. hp3: tai;
  782. WasLast: boolean;
  783. { UsedRegs, TmpUsedRegs: TRegSet; }
  784. begin
  785. p := BlockStart;
  786. { UsedRegs := []; }
  787. while (p <> BlockEnd) Do
  788. begin
  789. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  790. case p.Typ Of
  791. Ait_Instruction:
  792. begin
  793. case taicpu(p).opcode Of
  794. A_B:
  795. if taicpu(p).condition<>C_None then
  796. begin
  797. { check for
  798. Bxx xxx
  799. <several instructions>
  800. xxx:
  801. }
  802. l:=0;
  803. WasLast:=False;
  804. GetNextInstruction(p, hp1);
  805. while assigned(hp1) and
  806. (l<=4) and
  807. CanBeCond(hp1) and
  808. { stop on labels }
  809. not(hp1.typ=ait_label) do
  810. begin
  811. inc(l);
  812. if MustBeLast(hp1) then
  813. begin
  814. WasLast:=True;
  815. GetNextInstruction(hp1,hp1);
  816. break;
  817. end
  818. else
  819. GetNextInstruction(hp1,hp1);
  820. end;
  821. if assigned(hp1) then
  822. begin
  823. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  824. begin
  825. if (l<=4) and (l>0) then
  826. begin
  827. condition:=inverse_cond(taicpu(p).condition);
  828. hp2:=p;
  829. GetNextInstruction(p,hp1);
  830. p:=hp1;
  831. repeat
  832. if hp1.typ=ait_instruction then
  833. taicpu(hp1).condition:=condition;
  834. if MustBeLast(hp1) then
  835. begin
  836. GetNextInstruction(hp1,hp1);
  837. break;
  838. end
  839. else
  840. GetNextInstruction(hp1,hp1);
  841. until not(assigned(hp1)) or
  842. not(CanBeCond(hp1)) or
  843. (hp1.typ=ait_label);
  844. { wait with removing else GetNextInstruction could
  845. ignore the label if it was the only usage in the
  846. jump moved away }
  847. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  848. asml.remove(hp2);
  849. hp2.free;
  850. continue;
  851. end;
  852. end
  853. else
  854. { do not perform further optimizations if there is inctructon
  855. in block #1 which can not be optimized.
  856. }
  857. if not WasLast then
  858. begin
  859. { check further for
  860. Bcc xxx
  861. <several instructions 1>
  862. B yyy
  863. xxx:
  864. <several instructions 2>
  865. yyy:
  866. }
  867. { hp2 points to jmp yyy }
  868. hp2:=hp1;
  869. { skip hp1 to xxx }
  870. GetNextInstruction(hp1, hp1);
  871. if assigned(hp2) and
  872. assigned(hp1) and
  873. (l<=3) and
  874. (hp2.typ=ait_instruction) and
  875. (taicpu(hp2).is_jmp) and
  876. (taicpu(hp2).condition=C_None) and
  877. { real label and jump, no further references to the
  878. label are allowed }
  879. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  880. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  881. begin
  882. l:=0;
  883. { skip hp1 to <several moves 2> }
  884. GetNextInstruction(hp1, hp1);
  885. while assigned(hp1) and
  886. CanBeCond(hp1) do
  887. begin
  888. inc(l);
  889. GetNextInstruction(hp1, hp1);
  890. end;
  891. { hp1 points to yyy: }
  892. if assigned(hp1) and
  893. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  894. begin
  895. condition:=inverse_cond(taicpu(p).condition);
  896. GetNextInstruction(p,hp1);
  897. hp3:=p;
  898. p:=hp1;
  899. repeat
  900. if hp1.typ=ait_instruction then
  901. taicpu(hp1).condition:=condition;
  902. GetNextInstruction(hp1,hp1);
  903. until not(assigned(hp1)) or
  904. not(CanBeCond(hp1));
  905. { hp2 is still at jmp yyy }
  906. GetNextInstruction(hp2,hp1);
  907. { hp2 is now at xxx: }
  908. condition:=inverse_cond(condition);
  909. GetNextInstruction(hp1,hp1);
  910. { hp1 is now at <several movs 2> }
  911. repeat
  912. taicpu(hp1).condition:=condition;
  913. GetNextInstruction(hp1,hp1);
  914. until not(assigned(hp1)) or
  915. not(CanBeCond(hp1)) or
  916. (hp1.typ=ait_label);
  917. {
  918. asml.remove(hp1.next)
  919. hp1.next.free;
  920. asml.remove(hp1);
  921. hp1.free;
  922. }
  923. { remove Bcc }
  924. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  925. asml.remove(hp3);
  926. hp3.free;
  927. { remove jmp }
  928. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  929. asml.remove(hp2);
  930. hp2.free;
  931. continue;
  932. end;
  933. end;
  934. end;
  935. end;
  936. end;
  937. end;
  938. end;
  939. end;
  940. p := tai(p.next)
  941. end;
  942. end;
  943. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  944. begin
  945. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  946. Result:=true
  947. else
  948. Result:=inherited RegInInstruction(Reg, p1);
  949. end;
  950. const
  951. { set of opcode which might or do write to memory }
  952. { TODO : extend armins.dat to contain r/w info }
  953. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  954. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  955. function TCpuPreRegallocScheduler.PeepHoleOptPass1Cpu(var p: tai): boolean;
  956. { TODO : schedule also forward }
  957. { TODO : schedule distance > 1 }
  958. var
  959. hp1,hp2,hp3,hp4,hp5 : tai;
  960. list : TAsmList;
  961. begin
  962. result:=true;
  963. list:=TAsmList.Create;
  964. p := BlockStart;
  965. { UsedRegs := []; }
  966. while (p <> BlockEnd) Do
  967. begin
  968. if (p.typ=ait_instruction) and
  969. GetNextInstruction(p,hp1) and
  970. (hp1.typ=ait_instruction) and
  971. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  972. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  973. not(RegModifiedByInstruction(NR_PC,p)) and
  974. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH])
  975. ) or
  976. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  977. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  978. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  979. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  980. (taicpu(hp1).oper[1]^.ref^.offset=0)
  981. )
  982. ) or
  983. { try to prove that the memory accesses don't overlapp }
  984. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  985. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  986. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  987. (taicpu(p).oppostfix=PF_None) and
  988. (taicpu(hp1).oppostfix=PF_None) and
  989. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  990. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  991. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  992. (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)]))
  993. )
  994. )
  995. ) and
  996. GetNextInstruction(hp1,hp2) and
  997. (hp2.typ=ait_instruction) and
  998. { loaded register used by next instruction? }
  999. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  1000. { loaded register not used by previous instruction? }
  1001. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  1002. { same condition? }
  1003. (taicpu(p).condition=taicpu(hp1).condition) and
  1004. { first instruction might not change the register used as base }
  1005. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  1006. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  1007. ) and
  1008. { first instruction might not change the register used as index }
  1009. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  1010. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  1011. ) then
  1012. begin
  1013. hp3:=tai(p.Previous);
  1014. hp5:=tai(p.next);
  1015. asml.Remove(p);
  1016. { if there is a reg. dealloc instruction associated with p, move it together with p }
  1017. { before the instruction? }
  1018. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  1019. begin
  1020. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  1021. RegInInstruction(tai_regalloc(hp3).reg,p) then
  1022. begin
  1023. hp4:=hp3;
  1024. hp3:=tai(hp3.Previous);
  1025. asml.Remove(hp4);
  1026. list.Concat(hp4);
  1027. end
  1028. else
  1029. hp3:=tai(hp3.Previous);
  1030. end;
  1031. list.Concat(p);
  1032. { after the instruction? }
  1033. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  1034. begin
  1035. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  1036. RegInInstruction(tai_regalloc(hp5).reg,p) then
  1037. begin
  1038. hp4:=hp5;
  1039. hp5:=tai(hp5.next);
  1040. asml.Remove(hp4);
  1041. list.Concat(hp4);
  1042. end
  1043. else
  1044. hp5:=tai(hp5.Next);
  1045. end;
  1046. asml.Remove(hp1);
  1047. {$ifdef DEBUG_PREREGSCHEDULER}
  1048. asml.InsertBefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  1049. {$endif DEBUG_PREREGSCHEDULER}
  1050. asml.InsertBefore(hp1,hp2);
  1051. asml.InsertListBefore(hp2,list);
  1052. end;
  1053. p := tai(p.next)
  1054. end;
  1055. list.Free;
  1056. end;
  1057. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  1058. begin
  1059. { TODO: Add optimizer code }
  1060. end;
  1061. begin
  1062. casmoptimizer:=TCpuAsmOptimizer;
  1063. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  1064. End.