aoptcpu.pas 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  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. {$define DEBUG_PREREGSCHEDULER}
  21. Interface
  22. uses cgbase, cpubase, aasmtai, aasmcpu,aopt, aoptcpub, aoptobj;
  23. Type
  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. { gets the next tai object after current that contains info relevant
  33. to the optimizer in p1 which used the given register or does a
  34. change in program flow.
  35. If there is none, it returns false and
  36. sets p1 to nil }
  37. Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
  38. End;
  39. TCpuPreRegallocScheduler = class(TAsmScheduler)
  40. function SchedulerPass1Cpu(var p: tai): boolean;override;
  41. procedure SwapRegLive(p, hp1: taicpu);
  42. end;
  43. TCpuThumb2AsmOptimizer = class(TCpuAsmOptimizer)
  44. { uses the same constructor as TAopObj }
  45. procedure PeepHoleOptPass2;override;
  46. End;
  47. Implementation
  48. uses
  49. cutils,verbose,globals,
  50. systems,
  51. cpuinfo,
  52. cgobj,cgutils,procinfo,
  53. aasmbase,aasmdata;
  54. function CanBeCond(p : tai) : boolean;
  55. begin
  56. result:=
  57. (p.typ=ait_instruction) and
  58. (taicpu(p).condition=C_None) and
  59. (taicpu(p).opcode<>A_PLD) and
  60. ((taicpu(p).opcode<>A_BLX) or
  61. (taicpu(p).oper[0]^.typ=top_reg));
  62. end;
  63. function RefsEqual(const r1, r2: treference): boolean;
  64. begin
  65. refsequal :=
  66. (r1.offset = r2.offset) and
  67. (r1.base = r2.base) and
  68. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  69. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  70. (r1.relsymbol = r2.relsymbol) and
  71. (r1.signindex = r2.signindex) and
  72. (r1.shiftimm = r2.shiftimm) and
  73. (r1.addressmode = r2.addressmode) and
  74. (r1.shiftmode = r2.shiftmode);
  75. end;
  76. function MatchInstruction(const instr: tai; const op: TAsmOp; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  77. begin
  78. result :=
  79. (instr.typ = ait_instruction) and
  80. (taicpu(instr).opcode = op) and
  81. ((cond = []) or (taicpu(instr).condition in cond)) and
  82. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  83. end;
  84. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  85. begin
  86. result := oper1.typ = oper2.typ;
  87. if result then
  88. case oper1.typ of
  89. top_const:
  90. Result:=oper1.val = oper2.val;
  91. top_reg:
  92. Result:=oper1.reg = oper2.reg;
  93. top_conditioncode:
  94. Result:=oper1.cc = oper2.cc;
  95. top_ref:
  96. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  97. else Result:=false;
  98. end
  99. end;
  100. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  101. begin
  102. result := (oper.typ = top_reg) and (oper.reg = reg);
  103. end;
  104. procedure RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList);
  105. begin
  106. if (taicpu(movp).condition = C_EQ) and
  107. (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and
  108. (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
  109. begin
  110. asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
  111. asml.remove(movp);
  112. movp.free;
  113. end;
  114. end;
  115. function regLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  116. var
  117. p: taicpu;
  118. begin
  119. p := taicpu(hp);
  120. regLoadedWithNewValue := false;
  121. if not ((assigned(hp)) and (hp.typ = ait_instruction)) then
  122. exit;
  123. case p.opcode of
  124. { These operands do not write into a register at all }
  125. A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX, A_SWI, A_MSR, A_PLD:
  126. exit;
  127. {Take care of post/preincremented store and loads, they will change their base register}
  128. A_STR, A_LDR:
  129. regLoadedWithNewValue :=
  130. (taicpu(p).oper[1]^.typ=top_ref) and
  131. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  132. (taicpu(p).oper[1]^.ref^.base = reg);
  133. { These four are writing into the first 2 register, UMLAL and SMLAL will also read from them }
  134. A_UMLAL, A_UMULL, A_SMLAL, A_SMULL:
  135. regLoadedWithNewValue :=
  136. (p.oper[1]^.typ = top_reg) and
  137. (p.oper[1]^.reg = reg);
  138. {Loads to oper2 from coprocessor}
  139. {
  140. MCR/MRC is currently not supported in FPC
  141. A_MRC:
  142. regLoadedWithNewValue :=
  143. (p.oper[2]^.typ = top_reg) and
  144. (p.oper[2]^.reg = reg);
  145. }
  146. {Loads to all register in the registerset}
  147. A_LDM:
  148. regLoadedWithNewValue := (getsupreg(reg) in p.oper[1]^.regset^);
  149. end;
  150. if regLoadedWithNewValue then
  151. exit;
  152. case p.oper[0]^.typ of
  153. {This is the case}
  154. top_reg:
  155. regLoadedWithNewValue := (p.oper[0]^.reg = reg) or
  156. { LDRD }
  157. (p.opcode=A_LDR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg));
  158. {LDM/STM might write a new value to their index register}
  159. top_ref:
  160. regLoadedWithNewValue :=
  161. (taicpu(p).oper[0]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  162. (taicpu(p).oper[0]^.ref^.base = reg);
  163. end;
  164. end;
  165. function AlignedToQWord(const ref : treference) : boolean;
  166. begin
  167. { (safe) heuristics to ensure alignment }
  168. result:=(target_info.abi in [abi_eabi,abi_armeb,abi_eabihf]) and
  169. (((ref.offset>=0) and
  170. ((ref.offset mod 8)=0) and
  171. ((ref.base=NR_R13) or
  172. (ref.index=NR_R13))
  173. ) or
  174. ((ref.offset<=0) and
  175. { when using NR_R11, it has always a value of <qword align>+4 }
  176. ((abs(ref.offset+4) mod 8)=0) and
  177. (current_procinfo.framepointer=NR_R11) and
  178. ((ref.base=NR_R11) or
  179. (ref.index=NR_R11))
  180. )
  181. );
  182. end;
  183. function instructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  184. var
  185. p: taicpu;
  186. i: longint;
  187. begin
  188. instructionLoadsFromReg := false;
  189. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  190. exit;
  191. p:=taicpu(hp);
  192. i:=1;
  193. {For these instructions we have to start on oper[0]}
  194. if (p.opcode in [A_STR, A_LDM, A_STM, A_PLD,
  195. A_CMP, A_CMN, A_TST, A_TEQ,
  196. A_B, A_BL, A_BX, A_BLX,
  197. A_SMLAL, A_UMLAL]) then i:=0;
  198. while(i<p.ops) do
  199. begin
  200. case p.oper[I]^.typ of
  201. top_reg:
  202. instructionLoadsFromReg := (p.oper[I]^.reg = reg) or
  203. { STRD }
  204. ((i=0) and (p.opcode=A_STR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg)));
  205. top_regset:
  206. instructionLoadsFromReg := (getsupreg(reg) in p.oper[I]^.regset^);
  207. top_shifterop:
  208. instructionLoadsFromReg := p.oper[I]^.shifterop^.rs = reg;
  209. top_ref:
  210. instructionLoadsFromReg :=
  211. (p.oper[I]^.ref^.base = reg) or
  212. (p.oper[I]^.ref^.index = reg);
  213. end;
  214. if instructionLoadsFromReg then exit; {Bailout if we found something}
  215. Inc(I);
  216. end;
  217. end;
  218. function TCpuAsmOptimizer.RegUsedAfterInstruction(reg: Tregister; p: tai;
  219. var AllUsedRegs: TAllUsedRegs): Boolean;
  220. begin
  221. AllUsedRegs[getregtype(reg)].Update(tai(p.Next),true);
  222. RegUsedAfterInstruction :=
  223. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  224. not(regLoadedWithNewValue(reg,p)) and
  225. (
  226. not(GetNextInstruction(p,p)) or
  227. instructionLoadsFromReg(reg,p) or
  228. not(regLoadedWithNewValue(reg,p))
  229. );
  230. end;
  231. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  232. var Next: tai; reg: TRegister): Boolean;
  233. begin
  234. Next:=Current;
  235. repeat
  236. Result:=GetNextInstruction(Next,Next);
  237. until not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  238. (is_calljmp(taicpu(Next).opcode)) or (RegInInstruction(NR_PC,Next));
  239. end;
  240. procedure TCpuAsmOptimizer.RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  241. var
  242. alloc,
  243. dealloc : tai_regalloc;
  244. hp1 : tai;
  245. begin
  246. if MatchInstruction(movp, A_MOV, [taicpu(p).condition], [PF_None]) and
  247. (taicpu(movp).ops=2) and {We can't optimize if there is a shiftop}
  248. MatchOperand(taicpu(movp).oper[1]^, taicpu(p).oper[0]^.reg) and
  249. { don't mess with moves to pc }
  250. (taicpu(movp).oper[0]^.reg<>NR_PC) and
  251. { don't mess with moves to lr }
  252. (taicpu(movp).oper[0]^.reg<>NR_R14) and
  253. { the destination register of the mov might not be used beween p and movp }
  254. not(RegUsedBetween(taicpu(movp).oper[0]^.reg,p,movp)) and
  255. {There is a special requirement for MUL and MLA, oper[0] and oper[1] are not allowed to be the same}
  256. not (
  257. (taicpu(p).opcode in [A_MLA, A_MUL]) and
  258. (taicpu(p).oper[1]^.reg = taicpu(movp).oper[0]^.reg)
  259. ) then
  260. begin
  261. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(movp.Next));
  262. if assigned(dealloc) then
  263. begin
  264. asml.insertbefore(tai_comment.Create(strpnew('Peephole '+optimizer+' removed superfluous mov')), movp);
  265. { taicpu(p).oper[0]^.reg is not used anymore, try to find its allocation
  266. and remove it if possible }
  267. GetLastInstruction(p,hp1);
  268. asml.Remove(dealloc);
  269. alloc:=FindRegAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  270. if assigned(alloc) then
  271. begin
  272. asml.Remove(alloc);
  273. alloc.free;
  274. dealloc.free;
  275. end
  276. else
  277. asml.InsertAfter(dealloc,p);
  278. { try to move the allocation of the target register }
  279. GetLastInstruction(movp,hp1);
  280. alloc:=FindRegAlloc(taicpu(movp).oper[0]^.reg,tai(hp1.Next));
  281. if assigned(alloc) then
  282. begin
  283. asml.Remove(alloc);
  284. asml.InsertBefore(alloc,p);
  285. { adjust used regs }
  286. IncludeRegInUsedRegs(taicpu(movp).oper[0]^.reg,UsedRegs);
  287. end;
  288. { finally get rid of the mov }
  289. taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
  290. asml.remove(movp);
  291. movp.free;
  292. end;
  293. end;
  294. end;
  295. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  296. var
  297. hp1,hp2: tai;
  298. i, i2: longint;
  299. TmpUsedRegs: TAllUsedRegs;
  300. tempop: tasmop;
  301. function IsPowerOf2(const value: DWord): boolean; inline;
  302. begin
  303. Result:=(value and (value - 1)) = 0;
  304. end;
  305. begin
  306. result := false;
  307. case p.typ of
  308. ait_instruction:
  309. begin
  310. {
  311. change
  312. <op> reg,x,y
  313. cmp reg,#0
  314. into
  315. <op>s reg,x,y
  316. }
  317. { this optimization can applied only to the currently enabled operations because
  318. the other operations do not update all flags and FPC does not track flag usage }
  319. if ((taicpu(p).opcode in [A_ADC,A_ADD,A_SUB,A_MUL,A_MVN,A_MOV,A_ORR,A_EOR,A_AND]) or
  320. { those two values are > 255 so check separately }
  321. (taicpu(p).opcode=A_UDIV) or
  322. (taicpu(p).opcode=A_SDIV)
  323. ) and
  324. (taicpu(p).oppostfix = PF_None) and
  325. (taicpu(p).condition = C_None) and
  326. GetNextInstruction(p, hp1) and
  327. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  328. (taicpu(hp1).oper[1]^.typ = top_const) and
  329. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  330. (taicpu(hp1).oper[1]^.val = 0) and
  331. GetNextInstruction(hp1, hp2) and
  332. (tai(hp2).typ = ait_instruction) and
  333. { be careful here, following instructions could use other flags
  334. however after a jump fpc never depends on the value of flags }
  335. (taicpu(hp2).opcode = A_B) and
  336. (((taicpu(p).opcode in [A_ADC,A_ADD,A_SUB]) and
  337. (taicpu(hp2).condition in [C_EQ,C_NE,C_MI,C_PL])) or
  338. (taicpu(hp2).condition in [C_EQ,C_NE])) and
  339. assigned(FindRegDealloc(NR_DEFAULTFLAGS,tai(hp2.Next))) then
  340. begin
  341. asml.insertbefore(tai_comment.Create(strpnew('Peephole OpCmp2OpS done')), p);
  342. taicpu(p).oppostfix:=PF_S;
  343. { move flag allocation if possible }
  344. GetLastInstruction(hp1, hp2);
  345. hp2:=FindRegAlloc(NR_DEFAULTFLAGS,tai(hp2.Next));
  346. if assigned(hp2) then
  347. begin
  348. asml.Remove(hp2);
  349. asml.insertbefore(hp2, p);
  350. end;
  351. asml.remove(hp1);
  352. hp1.free;
  353. end
  354. else
  355. case taicpu(p).opcode of
  356. A_STR:
  357. begin
  358. { change
  359. str reg1,ref
  360. ldr reg2,ref
  361. into
  362. str reg1,ref
  363. mov reg2,reg1
  364. }
  365. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  366. (taicpu(p).oppostfix=PF_None) and
  367. GetNextInstruction(p,hp1) and
  368. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [PF_None]) and
  369. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  370. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  371. begin
  372. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  373. begin
  374. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 1 done')), hp1);
  375. asml.remove(hp1);
  376. hp1.free;
  377. end
  378. else
  379. begin
  380. taicpu(hp1).opcode:=A_MOV;
  381. taicpu(hp1).oppostfix:=PF_None;
  382. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  383. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 2 done')), hp1);
  384. end;
  385. result := true;
  386. end
  387. { change
  388. str reg1,ref
  389. str reg2,ref
  390. into
  391. strd reg1,ref
  392. }
  393. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  394. (taicpu(p).oppostfix=PF_None) and
  395. (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  396. GetNextInstruction(p,hp1) and
  397. MatchInstruction(hp1, A_STR, [taicpu(p).condition, C_None], [PF_None]) and
  398. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  399. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  400. { str ensures that either base or index contain no register, else ldr wouldn't
  401. use an offset either
  402. }
  403. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  404. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  405. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  406. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  407. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  408. begin
  409. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrStr2Strd done')), p);
  410. taicpu(p).oppostfix:=PF_D;
  411. asml.remove(hp1);
  412. hp1.free;
  413. end;
  414. end;
  415. A_LDR:
  416. begin
  417. { change
  418. ldr reg1,ref
  419. ldr reg2,ref
  420. into ...
  421. }
  422. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  423. GetNextInstruction(p,hp1) and
  424. { ldrd is not allowed here }
  425. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix,PF_None]-[PF_D]) then
  426. begin
  427. {
  428. ...
  429. ldr reg1,ref
  430. mov reg2,reg1
  431. }
  432. if RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  433. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  434. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  435. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  436. begin
  437. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  438. begin
  439. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2Ldr done')), hp1);
  440. asml.remove(hp1);
  441. hp1.free;
  442. end
  443. else
  444. begin
  445. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2LdrMov done')), hp1);
  446. taicpu(hp1).opcode:=A_MOV;
  447. taicpu(hp1).oppostfix:=PF_None;
  448. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  449. end;
  450. result := true;
  451. end
  452. {
  453. ...
  454. ldrd reg1,ref
  455. }
  456. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  457. { ldrd does not allow any postfixes ... }
  458. (taicpu(p).oppostfix=PF_None) and
  459. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  460. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  461. { ldr ensures that either base or index contain no register, else ldr wouldn't
  462. use an offset either
  463. }
  464. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  465. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  466. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  467. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  468. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  469. begin
  470. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2Ldrd done')), p);
  471. taicpu(p).oppostfix:=PF_D;
  472. asml.remove(hp1);
  473. hp1.free;
  474. end;
  475. end;
  476. { Remove superfluous mov after ldr
  477. changes
  478. ldr reg1, ref
  479. mov reg2, reg1
  480. to
  481. ldr reg2, ref
  482. conditions are:
  483. * no ldrd usage
  484. * reg1 must be released after mov
  485. * mov can not contain shifterops
  486. * ldr+mov have the same conditions
  487. * mov does not set flags
  488. }
  489. if (taicpu(p).oppostfix<>PF_D) and GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  490. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr');
  491. end;
  492. A_MOV:
  493. begin
  494. { fold
  495. mov reg1,reg0, shift imm1
  496. mov reg1,reg1, shift imm2
  497. }
  498. if (taicpu(p).ops=3) and
  499. (taicpu(p).oper[2]^.typ = top_shifterop) and
  500. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  501. getnextinstruction(p,hp1) and
  502. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  503. (taicpu(hp1).ops=3) and
  504. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  505. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  506. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  507. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  508. begin
  509. { fold
  510. mov reg1,reg0, lsl 16
  511. mov reg1,reg1, lsr 16
  512. strh reg1, ...
  513. dealloc reg1
  514. to
  515. strh reg1, ...
  516. dealloc reg1
  517. }
  518. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  519. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  520. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  521. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  522. getnextinstruction(hp1,hp2) and
  523. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  524. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  525. begin
  526. CopyUsedRegs(TmpUsedRegs);
  527. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  528. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  529. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  530. begin
  531. asml.insertbefore(tai_comment.Create(strpnew('Peephole optimizer removed superfluous 16 Bit zero extension')), hp1);
  532. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  533. asml.remove(p);
  534. asml.remove(hp1);
  535. p.free;
  536. hp1.free;
  537. p:=hp2;
  538. end;
  539. ReleaseUsedRegs(TmpUsedRegs);
  540. end
  541. { fold
  542. mov reg1,reg0, shift imm1
  543. mov reg1,reg1, shift imm2
  544. to
  545. mov reg1,reg0, shift imm1+imm2
  546. }
  547. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) or
  548. { asr makes no use after a lsr, the asr can be foled into the lsr }
  549. ((taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSR) and (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_ASR) ) then
  550. begin
  551. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  552. { avoid overflows }
  553. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  554. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  555. SM_ROR:
  556. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  557. SM_ASR:
  558. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  559. SM_LSR,
  560. SM_LSL:
  561. begin
  562. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  563. InsertLLItem(p.previous, p.next, hp1);
  564. p.free;
  565. p:=hp1;
  566. end;
  567. else
  568. internalerror(2008072803);
  569. end;
  570. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShift2Shift 1 done')), p);
  571. asml.remove(hp1);
  572. hp1.free;
  573. result := true;
  574. end
  575. { fold
  576. mov reg1,reg0, shift imm1
  577. mov reg1,reg1, shift imm2
  578. mov reg1,reg1, shift imm3 ...
  579. }
  580. else if getnextinstruction(hp1,hp2) and
  581. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  582. (taicpu(hp2).ops=3) and
  583. MatchOperand(taicpu(hp2).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  584. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  585. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  586. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  587. begin
  588. { mov reg1,reg0, lsl imm1
  589. mov reg1,reg1, lsr/asr imm2
  590. mov reg1,reg1, lsl imm3 ...
  591. if imm3<=imm1 and imm2>=imm3
  592. to
  593. mov reg1,reg0, lsl imm1
  594. mov reg1,reg1, lsr/asr imm2-imm3
  595. }
  596. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  597. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  598. (taicpu(hp2).oper[2]^.shifterop^.shiftimm<=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  599. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(hp2).oper[2]^.shifterop^.shiftimm) then
  600. begin
  601. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  602. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShiftShift2ShiftShift 1 done')), p);
  603. asml.remove(hp2);
  604. hp2.free;
  605. result := true;
  606. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  607. begin
  608. asml.remove(hp1);
  609. hp1.free;
  610. end;
  611. end
  612. { mov reg1,reg0, lsr/asr imm1
  613. mov reg1,reg1, lsl imm2
  614. mov reg1,reg1, lsr/asr imm3 ...
  615. if imm3>=imm1 and imm2>=imm1
  616. to
  617. mov reg1,reg0, lsl imm2-imm1
  618. mov reg1,reg1, lsr/asr imm3 ...
  619. }
  620. 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
  621. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  622. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  623. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  624. begin
  625. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  626. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  627. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShiftShift2ShiftShift 2 done')), p);
  628. asml.remove(p);
  629. p.free;
  630. p:=hp2;
  631. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  632. begin
  633. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  634. asml.remove(hp1);
  635. hp1.free;
  636. p:=hp2;
  637. end;
  638. result := true;
  639. end;
  640. end;
  641. end;
  642. { Change the common
  643. mov r0, r0, lsr #24
  644. and r0, r0, #255
  645. and remove the superfluous and
  646. This could be extended to handle more cases.
  647. }
  648. if (taicpu(p).ops=3) and
  649. (taicpu(p).oper[2]^.typ = top_shifterop) and
  650. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  651. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  652. (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  653. getnextinstruction(p,hp1) and
  654. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  655. (taicpu(hp1).ops=3) and
  656. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  657. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  658. (taicpu(hp1).oper[2]^.typ = top_const) and
  659. { Check if the AND actually would only mask out bits beeing already zero because of the shift
  660. For LSR #25 and an AndConst of 255 that whould go like this:
  661. 255 and ((2 shl (32-25))-1)
  662. which results in 127, which is one less a power-of-2, meaning all lower bits are set.
  663. LSR #25 and AndConst of 254:
  664. 254 and ((2 shl (32-25))-1) = 126 -> lowest bit is clear, so we can't remove it.
  665. }
  666. ispowerof2((taicpu(hp1).oper[2]^.val and ((2 shl (32-taicpu(p).oper[2]^.shifterop^.shiftimm))-1))+1) then
  667. begin
  668. asml.insertbefore(tai_comment.Create(strpnew('Peephole LsrAnd2Lsr done')), hp1);
  669. asml.remove(hp1);
  670. hp1.free;
  671. end;
  672. {
  673. optimize
  674. mov rX, yyyy
  675. ....
  676. }
  677. if (taicpu(p).ops = 2) and
  678. GetNextInstruction(p,hp1) and
  679. (tai(hp1).typ = ait_instruction) then
  680. begin
  681. {
  682. This changes the very common
  683. mov r0, #0
  684. str r0, [...]
  685. mov r0, #0
  686. str r0, [...]
  687. and removes all superfluous mov instructions
  688. }
  689. if (taicpu(p).oper[1]^.typ = top_const) and
  690. (taicpu(hp1).opcode=A_STR) then
  691. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], [PF_None]) and
  692. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  693. GetNextInstruction(hp1, hp2) and
  694. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  695. (taicpu(hp2).ops = 2) and
  696. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  697. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  698. begin
  699. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovStrMov done')), hp2);
  700. GetNextInstruction(hp2,hp1);
  701. asml.remove(hp2);
  702. hp2.free;
  703. if not assigned(hp1) then break;
  704. end
  705. {
  706. This removes the first mov from
  707. mov rX,...
  708. mov rX,...
  709. }
  710. else if taicpu(hp1).opcode=A_MOV then
  711. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  712. (taicpu(hp1).ops = 2) and
  713. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  714. { don't remove the first mov if the second is a mov rX,rX }
  715. not(MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)) do
  716. begin
  717. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovMov done')), p);
  718. asml.remove(p);
  719. p.free;
  720. p:=hp1;
  721. GetNextInstruction(hp1,hp1);
  722. if not assigned(hp1) then
  723. break;
  724. end;
  725. end;
  726. {
  727. change
  728. mov r1, r0
  729. add r1, r1, #1
  730. to
  731. add r1, r0, #1
  732. Todo: Make it work for mov+cmp too
  733. CAUTION! If this one is successful p might not be a mov instruction anymore!
  734. }
  735. if (taicpu(p).ops = 2) and
  736. (taicpu(p).oper[1]^.typ = top_reg) and
  737. (taicpu(p).oppostfix = PF_NONE) and
  738. GetNextInstruction(p, hp1) and
  739. (tai(hp1).typ = ait_instruction) and
  740. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  741. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN]) and
  742. {MOV and MVN might only have 2 ops}
  743. (taicpu(hp1).ops = 3) and
  744. (taicpu(hp1).condition in [C_NONE, taicpu(hp1).condition]) and
  745. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  746. (taicpu(hp1).oper[1]^.typ = top_reg) and
  747. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop]) then
  748. begin
  749. { When we get here we still don't know if the registers match}
  750. for I:=1 to 2 do
  751. {
  752. If the first loop was successful p will be replaced with hp1.
  753. The checks will still be ok, because all required information
  754. will also be in hp1 then.
  755. }
  756. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  757. begin
  758. asml.insertbefore(tai_comment.Create(strpnew('Peephole RedundantMovProcess done')), hp1);
  759. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  760. if p<>hp1 then
  761. begin
  762. asml.remove(p);
  763. p.free;
  764. p:=hp1;
  765. end;
  766. end;
  767. end;
  768. { This folds shifterops into following instructions
  769. mov r0, r1, lsl #8
  770. add r2, r3, r0
  771. to
  772. add r2, r3, r1, lsl #8
  773. CAUTION! If this one is successful p might not be a mov instruction anymore!
  774. }
  775. if (taicpu(p).opcode = A_MOV) and
  776. (taicpu(p).ops = 3) and
  777. (taicpu(p).oper[1]^.typ = top_reg) and
  778. (taicpu(p).oper[2]^.typ = top_shifterop) and
  779. (taicpu(p).oppostfix = PF_NONE) and
  780. GetNextInstruction(p, hp1) and
  781. (tai(hp1).typ = ait_instruction) and
  782. (taicpu(hp1).ops >= 2) and {Currently we can't fold into another shifterop}
  783. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  784. (taicpu(hp1).oppostfix = PF_NONE) and
  785. (taicpu(hp1).condition = taicpu(p).condition) and
  786. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  787. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  788. A_CMP, A_CMN]) and
  789. (
  790. {Only ONE of the two src operands is allowed to match}
  791. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  792. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  793. ) then
  794. begin
  795. CopyUsedRegs(TmpUsedRegs);
  796. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  797. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  798. I2:=0
  799. else
  800. I2:=1;
  801. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp1,TmpUsedRegs)) then
  802. for I:=I2 to taicpu(hp1).ops-1 do
  803. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  804. begin
  805. { If the parameter matched on the second op from the RIGHT
  806. we have to switch the parameters, this will not happen for CMP
  807. were we're only evaluating the most right parameter
  808. }
  809. if I <> taicpu(hp1).ops-1 then
  810. begin
  811. {The SUB operators need to be changed when we swap parameters}
  812. case taicpu(hp1).opcode of
  813. A_SUB: tempop:=A_RSB;
  814. A_SBC: tempop:=A_RSC;
  815. A_RSB: tempop:=A_SUB;
  816. A_RSC: tempop:=A_SBC;
  817. else tempop:=taicpu(hp1).opcode;
  818. end;
  819. if taicpu(hp1).ops = 3 then
  820. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  821. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  822. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  823. else
  824. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  825. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  826. taicpu(p).oper[2]^.shifterop^);
  827. end
  828. else
  829. if taicpu(hp1).ops = 3 then
  830. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  831. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  832. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  833. else
  834. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  835. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  836. taicpu(p).oper[2]^.shifterop^);
  837. asml.insertbefore(hp2, p);
  838. asml.remove(p);
  839. asml.remove(hp1);
  840. p.free;
  841. hp1.free;
  842. p:=hp2;
  843. GetNextInstruction(p,hp1);
  844. asml.insertbefore(tai_comment.Create(strpnew('Peephole FoldShiftProcess done')), p);
  845. break;
  846. end;
  847. ReleaseUsedRegs(TmpUsedRegs);
  848. end;
  849. {
  850. Often we see shifts and then a superfluous mov to another register
  851. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  852. }
  853. if (taicpu(p).opcode = A_MOV) and
  854. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  855. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  856. end;
  857. A_ADD,
  858. A_ADC,
  859. A_RSB,
  860. A_RSC,
  861. A_SUB,
  862. A_SBC,
  863. A_AND,
  864. A_BIC,
  865. A_EOR,
  866. A_ORR,
  867. A_MLA,
  868. A_MUL:
  869. begin
  870. {
  871. change
  872. and reg2,reg1,const1
  873. and reg2,reg2,const2
  874. to
  875. and reg2,reg1,(const1 and const2)
  876. }
  877. if (taicpu(p).opcode = A_AND) and
  878. (taicpu(p).oper[1]^.typ = top_reg) and
  879. (taicpu(p).oper[2]^.typ = top_const) and
  880. GetNextInstruction(p, hp1) and
  881. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  882. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  883. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  884. (taicpu(hp1).oper[2]^.typ = top_const) then
  885. begin
  886. asml.insertbefore(tai_comment.Create(strpnew('Peephole AndAnd2And done')), p);
  887. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  888. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  889. asml.remove(hp1);
  890. hp1.free;
  891. end;
  892. {
  893. change
  894. add reg1, ...
  895. mov reg2, reg1
  896. to
  897. add reg2, ...
  898. }
  899. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  900. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  901. end;
  902. A_CMP:
  903. begin
  904. {
  905. change
  906. cmp reg,const1
  907. moveq reg,const1
  908. movne reg,const2
  909. to
  910. cmp reg,const1
  911. movne reg,const2
  912. }
  913. if (taicpu(p).oper[1]^.typ = top_const) and
  914. GetNextInstruction(p, hp1) and
  915. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  916. (taicpu(hp1).oper[1]^.typ = top_const) and
  917. GetNextInstruction(hp1, hp2) and
  918. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  919. (taicpu(hp1).oper[1]^.typ = top_const) then
  920. begin
  921. RemoveRedundantMove(p, hp1, asml);
  922. RemoveRedundantMove(p, hp2, asml);
  923. end;
  924. end;
  925. end;
  926. end;
  927. end;
  928. end;
  929. { instructions modifying the CPSR can be only the last instruction }
  930. function MustBeLast(p : tai) : boolean;
  931. begin
  932. Result:=(p.typ=ait_instruction) and
  933. ((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
  934. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  935. (taicpu(p).oppostfix=PF_S));
  936. end;
  937. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  938. var
  939. p,hp1,hp2: tai;
  940. l : longint;
  941. condition : tasmcond;
  942. hp3: tai;
  943. WasLast: boolean;
  944. { UsedRegs, TmpUsedRegs: TRegSet; }
  945. begin
  946. p := BlockStart;
  947. { UsedRegs := []; }
  948. while (p <> BlockEnd) Do
  949. begin
  950. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  951. case p.Typ Of
  952. Ait_Instruction:
  953. begin
  954. case taicpu(p).opcode Of
  955. A_B:
  956. if taicpu(p).condition<>C_None then
  957. begin
  958. { check for
  959. Bxx xxx
  960. <several instructions>
  961. xxx:
  962. }
  963. l:=0;
  964. WasLast:=False;
  965. GetNextInstruction(p, hp1);
  966. while assigned(hp1) and
  967. (l<=4) and
  968. CanBeCond(hp1) and
  969. { stop on labels }
  970. not(hp1.typ=ait_label) do
  971. begin
  972. inc(l);
  973. if MustBeLast(hp1) then
  974. begin
  975. WasLast:=True;
  976. GetNextInstruction(hp1,hp1);
  977. break;
  978. end
  979. else
  980. GetNextInstruction(hp1,hp1);
  981. end;
  982. if assigned(hp1) then
  983. begin
  984. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  985. begin
  986. if (l<=4) and (l>0) then
  987. begin
  988. condition:=inverse_cond(taicpu(p).condition);
  989. hp2:=p;
  990. GetNextInstruction(p,hp1);
  991. p:=hp1;
  992. repeat
  993. if hp1.typ=ait_instruction then
  994. taicpu(hp1).condition:=condition;
  995. if MustBeLast(hp1) then
  996. begin
  997. GetNextInstruction(hp1,hp1);
  998. break;
  999. end
  1000. else
  1001. GetNextInstruction(hp1,hp1);
  1002. until not(assigned(hp1)) or
  1003. not(CanBeCond(hp1)) or
  1004. (hp1.typ=ait_label);
  1005. { wait with removing else GetNextInstruction could
  1006. ignore the label if it was the only usage in the
  1007. jump moved away }
  1008. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1009. asml.remove(hp2);
  1010. hp2.free;
  1011. continue;
  1012. end;
  1013. end
  1014. else
  1015. { do not perform further optimizations if there is inctructon
  1016. in block #1 which can not be optimized.
  1017. }
  1018. if not WasLast then
  1019. begin
  1020. { check further for
  1021. Bcc xxx
  1022. <several instructions 1>
  1023. B yyy
  1024. xxx:
  1025. <several instructions 2>
  1026. yyy:
  1027. }
  1028. { hp2 points to jmp yyy }
  1029. hp2:=hp1;
  1030. { skip hp1 to xxx }
  1031. GetNextInstruction(hp1, hp1);
  1032. if assigned(hp2) and
  1033. assigned(hp1) and
  1034. (l<=3) and
  1035. (hp2.typ=ait_instruction) and
  1036. (taicpu(hp2).is_jmp) and
  1037. (taicpu(hp2).condition=C_None) and
  1038. { real label and jump, no further references to the
  1039. label are allowed }
  1040. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  1041. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1042. begin
  1043. l:=0;
  1044. { skip hp1 to <several moves 2> }
  1045. GetNextInstruction(hp1, hp1);
  1046. while assigned(hp1) and
  1047. CanBeCond(hp1) do
  1048. begin
  1049. inc(l);
  1050. GetNextInstruction(hp1, hp1);
  1051. end;
  1052. { hp1 points to yyy: }
  1053. if assigned(hp1) and
  1054. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  1055. begin
  1056. condition:=inverse_cond(taicpu(p).condition);
  1057. GetNextInstruction(p,hp1);
  1058. hp3:=p;
  1059. p:=hp1;
  1060. repeat
  1061. if hp1.typ=ait_instruction then
  1062. taicpu(hp1).condition:=condition;
  1063. GetNextInstruction(hp1,hp1);
  1064. until not(assigned(hp1)) or
  1065. not(CanBeCond(hp1));
  1066. { hp2 is still at jmp yyy }
  1067. GetNextInstruction(hp2,hp1);
  1068. { hp2 is now at xxx: }
  1069. condition:=inverse_cond(condition);
  1070. GetNextInstruction(hp1,hp1);
  1071. { hp1 is now at <several movs 2> }
  1072. repeat
  1073. taicpu(hp1).condition:=condition;
  1074. GetNextInstruction(hp1,hp1);
  1075. until not(assigned(hp1)) or
  1076. not(CanBeCond(hp1)) or
  1077. (hp1.typ=ait_label);
  1078. {
  1079. asml.remove(hp1.next)
  1080. hp1.next.free;
  1081. asml.remove(hp1);
  1082. hp1.free;
  1083. }
  1084. { remove Bcc }
  1085. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  1086. asml.remove(hp3);
  1087. hp3.free;
  1088. { remove jmp }
  1089. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1090. asml.remove(hp2);
  1091. hp2.free;
  1092. continue;
  1093. end;
  1094. end;
  1095. end;
  1096. end;
  1097. end;
  1098. end;
  1099. end;
  1100. end;
  1101. p := tai(p.next)
  1102. end;
  1103. end;
  1104. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  1105. begin
  1106. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  1107. Result:=true
  1108. else
  1109. Result:=inherited RegInInstruction(Reg, p1);
  1110. end;
  1111. const
  1112. { set of opcode which might or do write to memory }
  1113. { TODO : extend armins.dat to contain r/w info }
  1114. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  1115. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  1116. { adjust the register live information when swapping the two instructions p and hp1,
  1117. they must follow one after the other }
  1118. procedure TCpuPreRegallocScheduler.SwapRegLive(p,hp1 : taicpu);
  1119. procedure CheckLiveEnd(reg : tregister);
  1120. var
  1121. supreg : TSuperRegister;
  1122. regtype : TRegisterType;
  1123. begin
  1124. if reg=NR_NO then
  1125. exit;
  1126. regtype:=getregtype(reg);
  1127. supreg:=getsupreg(reg);
  1128. if (cg.rg[regtype].live_end[supreg]=hp1) and
  1129. RegInInstruction(reg,p) then
  1130. cg.rg[regtype].live_end[supreg]:=p;
  1131. end;
  1132. procedure CheckLiveStart(reg : TRegister);
  1133. var
  1134. supreg : TSuperRegister;
  1135. regtype : TRegisterType;
  1136. begin
  1137. if reg=NR_NO then
  1138. exit;
  1139. regtype:=getregtype(reg);
  1140. supreg:=getsupreg(reg);
  1141. if (cg.rg[regtype].live_start[supreg]=p) and
  1142. RegInInstruction(reg,hp1) then
  1143. cg.rg[regtype].live_start[supreg]:=hp1;
  1144. end;
  1145. var
  1146. i : longint;
  1147. r : TSuperRegister;
  1148. begin
  1149. { assumption: p is directly followed by hp1 }
  1150. { if live of any reg used by p starts at p and hp1 uses this register then
  1151. set live start to hp1 }
  1152. for i:=0 to p.ops-1 do
  1153. case p.oper[i]^.typ of
  1154. Top_Reg:
  1155. CheckLiveStart(p.oper[i]^.reg);
  1156. Top_Ref:
  1157. begin
  1158. CheckLiveStart(p.oper[i]^.ref^.base);
  1159. CheckLiveStart(p.oper[i]^.ref^.index);
  1160. end;
  1161. Top_Shifterop:
  1162. CheckLiveStart(p.oper[i]^.shifterop^.rs);
  1163. Top_RegSet:
  1164. for r:=RS_R0 to RS_R15 do
  1165. if r in p.oper[i]^.regset^ then
  1166. CheckLiveStart(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1167. end;
  1168. { if live of any reg used by hp1 ends at hp1 and p uses this register then
  1169. set live end to p }
  1170. for i:=0 to hp1.ops-1 do
  1171. case hp1.oper[i]^.typ of
  1172. Top_Reg:
  1173. CheckLiveEnd(hp1.oper[i]^.reg);
  1174. Top_Ref:
  1175. begin
  1176. CheckLiveEnd(hp1.oper[i]^.ref^.base);
  1177. CheckLiveEnd(hp1.oper[i]^.ref^.index);
  1178. end;
  1179. Top_Shifterop:
  1180. CheckLiveStart(hp1.oper[i]^.shifterop^.rs);
  1181. Top_RegSet:
  1182. for r:=RS_R0 to RS_R15 do
  1183. if r in hp1.oper[i]^.regset^ then
  1184. CheckLiveEnd(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1185. end;
  1186. end;
  1187. function TCpuPreRegallocScheduler.SchedulerPass1Cpu(var p: tai): boolean;
  1188. { TODO : schedule also forward }
  1189. { TODO : schedule distance > 1 }
  1190. var
  1191. hp1,hp2,hp3,hp4,hp5 : tai;
  1192. list : TAsmList;
  1193. begin
  1194. result:=true;
  1195. list:=TAsmList.Create;
  1196. p:=BlockStart;
  1197. while p<>BlockEnd Do
  1198. begin
  1199. if (p.typ=ait_instruction) and
  1200. GetNextInstruction(p,hp1) and
  1201. (hp1.typ=ait_instruction) and
  1202. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  1203. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  1204. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  1205. not(RegModifiedByInstruction(NR_PC,p))
  1206. ) or
  1207. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  1208. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  1209. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  1210. (taicpu(hp1).oper[1]^.ref^.offset=0)
  1211. )
  1212. ) or
  1213. { try to prove that the memory accesses don't overlapp }
  1214. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  1215. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  1216. (taicpu(p).oppostfix=PF_None) and
  1217. (taicpu(hp1).oppostfix=PF_None) and
  1218. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  1219. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1220. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  1221. (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)]))
  1222. )
  1223. )
  1224. ) and
  1225. GetNextInstruction(hp1,hp2) and
  1226. (hp2.typ=ait_instruction) and
  1227. { loaded register used by next instruction? }
  1228. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  1229. { loaded register not used by previous instruction? }
  1230. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  1231. { same condition? }
  1232. (taicpu(p).condition=taicpu(hp1).condition) and
  1233. { first instruction might not change the register used as base }
  1234. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  1235. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  1236. ) and
  1237. { first instruction might not change the register used as index }
  1238. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  1239. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  1240. ) then
  1241. begin
  1242. hp3:=tai(p.Previous);
  1243. hp5:=tai(p.next);
  1244. asml.Remove(p);
  1245. { if there is a reg. dealloc instruction associated with p, move it together with p }
  1246. { before the instruction? }
  1247. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  1248. begin
  1249. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  1250. RegInInstruction(tai_regalloc(hp3).reg,p) then
  1251. begin
  1252. hp4:=hp3;
  1253. hp3:=tai(hp3.Previous);
  1254. asml.Remove(hp4);
  1255. list.Concat(hp4);
  1256. end
  1257. else
  1258. hp3:=tai(hp3.Previous);
  1259. end;
  1260. list.Concat(p);
  1261. SwapRegLive(taicpu(p),taicpu(hp1));
  1262. { after the instruction? }
  1263. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  1264. begin
  1265. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  1266. RegInInstruction(tai_regalloc(hp5).reg,p) then
  1267. begin
  1268. hp4:=hp5;
  1269. hp5:=tai(hp5.next);
  1270. asml.Remove(hp4);
  1271. list.Concat(hp4);
  1272. end
  1273. else
  1274. hp5:=tai(hp5.Next);
  1275. end;
  1276. asml.Remove(hp1);
  1277. {$ifdef DEBUG_PREREGSCHEDULER}
  1278. asml.InsertBefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  1279. {$endif DEBUG_PREREGSCHEDULER}
  1280. asml.InsertBefore(hp1,hp2);
  1281. asml.InsertListBefore(hp2,list);
  1282. p:=tai(p.next)
  1283. end
  1284. else if p.typ=ait_instruction then
  1285. p:=hp1
  1286. else
  1287. p:=tai(p.next);
  1288. end;
  1289. list.Free;
  1290. end;
  1291. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  1292. begin
  1293. { TODO: Add optimizer code }
  1294. end;
  1295. begin
  1296. casmoptimizer:=TCpuAsmOptimizer;
  1297. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  1298. End.