aoptcpu.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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).loadreg(0,taicpu(movp).oper[0]^.reg);
  266. asml.remove(movp);
  267. asml.Remove(dealloc);
  268. asml.InsertAfter(dealloc,p);
  269. movp.free;
  270. end;
  271. end;
  272. end;
  273. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  274. var
  275. hp1,hp2: tai;
  276. i, i2: longint;
  277. TmpUsedRegs: TAllUsedRegs;
  278. tempop: tasmop;
  279. function IsPowerOf2(const value: DWord): boolean; inline;
  280. begin
  281. Result:=(value and (value - 1)) = 0;
  282. end;
  283. begin
  284. result := false;
  285. case p.typ of
  286. ait_instruction:
  287. begin
  288. (* optimization proved not to be safe, see tw4768.pp
  289. {
  290. change
  291. <op> reg,x,y
  292. cmp reg,#0
  293. into
  294. <op>s reg,x,y
  295. }
  296. { this optimization can applied only to the currently enabled operations because
  297. the other operations do not update all flags and FPC does not track flag usage }
  298. 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
  299. (taicpu(p).oppostfix = PF_None) and
  300. (taicpu(p).condition = C_None) and
  301. GetNextInstruction(p, hp1) and
  302. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  303. (taicpu(hp1).oper[1]^.typ = top_const) and
  304. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  305. (taicpu(hp1).oper[1]^.val = 0) { and
  306. GetNextInstruction(hp1, hp2) and
  307. (tai(hp2).typ = ait_instruction) and
  308. // be careful here, following instructions could use other flags
  309. // however after a jump fpc never depends on the value of flags
  310. (taicpu(hp2).opcode = A_B) and
  311. (taicpu(hp2).condition in [C_EQ,C_NE,C_MI,C_PL])} then
  312. begin
  313. taicpu(p).oppostfix:=PF_S;
  314. asml.remove(hp1);
  315. hp1.free;
  316. end
  317. else
  318. *)
  319. case taicpu(p).opcode of
  320. A_STR:
  321. begin
  322. { change
  323. str reg1,ref
  324. ldr reg2,ref
  325. into
  326. str reg1,ref
  327. mov reg2,reg1
  328. }
  329. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  330. (taicpu(p).oppostfix=PF_None) and
  331. GetNextInstruction(p,hp1) and
  332. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [PF_None]) and
  333. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  334. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  335. begin
  336. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  337. begin
  338. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 1 done')), hp1);
  339. asml.remove(hp1);
  340. hp1.free;
  341. end
  342. else
  343. begin
  344. taicpu(hp1).opcode:=A_MOV;
  345. taicpu(hp1).oppostfix:=PF_None;
  346. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  347. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrLdr2StrMov 2 done')), hp1);
  348. end;
  349. result := true;
  350. end
  351. { change
  352. str reg1,ref
  353. str reg2,ref
  354. into
  355. strd reg1,ref
  356. }
  357. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  358. (taicpu(p).oppostfix=PF_None) and
  359. (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  360. GetNextInstruction(p,hp1) and
  361. MatchInstruction(hp1, A_STR, [taicpu(p).condition, C_None], [PF_None]) and
  362. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  363. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  364. { str ensures that either base or index contain no register, else ldr wouldn't
  365. use an offset either
  366. }
  367. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  368. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  369. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  370. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  371. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  372. begin
  373. asml.insertbefore(tai_comment.Create(strpnew('Peephole StrStr2Strd done')), p);
  374. taicpu(p).oppostfix:=PF_D;
  375. asml.remove(hp1);
  376. hp1.free;
  377. end;
  378. end;
  379. A_LDR:
  380. begin
  381. { change
  382. ldr reg1,ref
  383. ldr reg2,ref
  384. into ...
  385. }
  386. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  387. GetNextInstruction(p,hp1) and
  388. { ldrd is not allowed here }
  389. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix,PF_None]-[PF_D]) then
  390. begin
  391. {
  392. ...
  393. ldr reg1,ref
  394. mov reg2,reg1
  395. }
  396. if RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  397. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  398. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  399. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  400. begin
  401. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  402. begin
  403. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2Ldr done')), hp1);
  404. asml.remove(hp1);
  405. hp1.free;
  406. end
  407. else
  408. begin
  409. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2LdrMov done')), hp1);
  410. taicpu(hp1).opcode:=A_MOV;
  411. taicpu(hp1).oppostfix:=PF_None;
  412. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  413. end;
  414. result := true;
  415. end
  416. {
  417. ...
  418. ldrd reg1,ref
  419. }
  420. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  421. { ldrd does not allow any postfixes ... }
  422. (taicpu(p).oppostfix=PF_None) and
  423. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  424. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  425. { ldr ensures that either base or index contain no register, else ldr wouldn't
  426. use an offset either
  427. }
  428. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  429. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  430. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  431. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  432. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  433. begin
  434. asml.insertbefore(tai_comment.Create(strpnew('Peephole LdrLdr2Ldrd done')), p);
  435. taicpu(p).oppostfix:=PF_D;
  436. asml.remove(hp1);
  437. hp1.free;
  438. end;
  439. end;
  440. { Remove superfluous mov after ldr
  441. changes
  442. ldr reg1, ref
  443. mov reg2, reg1
  444. to
  445. ldr reg2, ref
  446. conditions are:
  447. * no ldrd usage
  448. * reg1 must be released after mov
  449. * mov can not contain shifterops
  450. * ldr+mov have the same conditions
  451. * mov does not set flags
  452. }
  453. if (taicpu(p).oppostfix<>PF_D) and GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  454. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr');
  455. end;
  456. A_MOV:
  457. begin
  458. { fold
  459. mov reg1,reg0, shift imm1
  460. mov reg1,reg1, shift imm2
  461. }
  462. if (taicpu(p).ops=3) and
  463. (taicpu(p).oper[2]^.typ = top_shifterop) and
  464. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  465. getnextinstruction(p,hp1) and
  466. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  467. (taicpu(hp1).ops=3) and
  468. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  469. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  470. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  471. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  472. begin
  473. { fold
  474. mov reg1,reg0, lsl 16
  475. mov reg1,reg1, lsr 16
  476. strh reg1, ...
  477. dealloc reg1
  478. to
  479. strh reg1, ...
  480. dealloc reg1
  481. }
  482. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  483. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  484. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  485. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  486. getnextinstruction(hp1,hp2) and
  487. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  488. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  489. begin
  490. CopyUsedRegs(TmpUsedRegs);
  491. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  492. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  493. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  494. begin
  495. asml.insertbefore(tai_comment.Create(strpnew('Peephole optimizer removed superfluous 16 Bit zero extension')), hp1);
  496. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  497. asml.remove(p);
  498. asml.remove(hp1);
  499. p.free;
  500. hp1.free;
  501. p:=hp2;
  502. end;
  503. ReleaseUsedRegs(TmpUsedRegs);
  504. end
  505. { fold
  506. mov reg1,reg0, shift imm1
  507. mov reg1,reg1, shift imm2
  508. to
  509. mov reg1,reg0, shift imm1+imm2
  510. }
  511. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) or
  512. { asr makes no use after a lsr, the asr can be foled into the lsr }
  513. ((taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSR) and (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_ASR) ) then
  514. begin
  515. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  516. { avoid overflows }
  517. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  518. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  519. SM_ROR:
  520. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  521. SM_ASR:
  522. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  523. SM_LSR,
  524. SM_LSL:
  525. begin
  526. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  527. InsertLLItem(p.previous, p.next, hp1);
  528. p.free;
  529. p:=hp1;
  530. end;
  531. else
  532. internalerror(2008072803);
  533. end;
  534. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShift2Shift 1 done')), p);
  535. asml.remove(hp1);
  536. hp1.free;
  537. result := true;
  538. end
  539. { fold
  540. mov reg1,reg0, shift imm1
  541. mov reg1,reg1, shift imm2
  542. mov reg1,reg1, shift imm3 ...
  543. }
  544. else if getnextinstruction(hp1,hp2) and
  545. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  546. (taicpu(hp2).ops=3) and
  547. MatchOperand(taicpu(hp2).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  548. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  549. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  550. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  551. begin
  552. { mov reg1,reg0, lsl imm1
  553. mov reg1,reg1, lsr/asr imm2
  554. mov reg1,reg1, lsl imm3 ...
  555. if imm3<=imm1 and imm2>=imm3
  556. to
  557. mov reg1,reg0, lsl imm1
  558. mov reg1,reg1, lsr/asr imm2-imm3
  559. }
  560. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  561. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  562. (taicpu(hp2).oper[2]^.shifterop^.shiftimm<=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  563. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(hp2).oper[2]^.shifterop^.shiftimm) then
  564. begin
  565. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  566. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShiftShift2ShiftShift 1 done')), p);
  567. asml.remove(hp2);
  568. hp2.free;
  569. result := true;
  570. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  571. begin
  572. asml.remove(hp1);
  573. hp1.free;
  574. end;
  575. end
  576. { mov reg1,reg0, lsr/asr imm1
  577. mov reg1,reg1, lsl imm2
  578. mov reg1,reg1, lsr/asr imm3 ...
  579. if imm3>=imm1 and imm2>=imm1
  580. to
  581. mov reg1,reg0, lsl imm2-imm1
  582. mov reg1,reg1, lsr/asr imm3 ...
  583. }
  584. 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
  585. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  586. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  587. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  588. begin
  589. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  590. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  591. asml.insertbefore(tai_comment.Create(strpnew('Peephole ShiftShiftShift2ShiftShift 2 done')), p);
  592. asml.remove(p);
  593. p.free;
  594. p:=hp2;
  595. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  596. begin
  597. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  598. asml.remove(hp1);
  599. hp1.free;
  600. p:=hp2;
  601. end;
  602. result := true;
  603. end;
  604. end;
  605. end;
  606. { Change the common
  607. mov r0, r0, lsr #24
  608. and r0, r0, #255
  609. and remove the superfluous and
  610. This could be extended to handle more cases.
  611. }
  612. if (taicpu(p).ops=3) and
  613. (taicpu(p).oper[2]^.typ = top_shifterop) and
  614. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  615. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  616. (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  617. getnextinstruction(p,hp1) and
  618. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  619. (taicpu(hp1).ops=3) and
  620. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  621. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  622. (taicpu(hp1).oper[2]^.typ = top_const) and
  623. { Check if the AND actually would only mask out bits beeing already zero because of the shift
  624. For LSR #25 and an AndConst of 255 that whould go like this:
  625. 255 and ((2 shl (32-25))-1)
  626. which results in 127, which is one less a power-of-2, meaning all lower bits are set.
  627. LSR #25 and AndConst of 254:
  628. 254 and ((2 shl (32-25))-1) = 126 -> lowest bit is clear, so we can't remove it.
  629. }
  630. ispowerof2((taicpu(hp1).oper[2]^.val and ((2 shl (32-taicpu(p).oper[2]^.shifterop^.shiftimm))-1))+1) then
  631. begin
  632. asml.insertbefore(tai_comment.Create(strpnew('Peephole LsrAnd2Lsr done')), hp1);
  633. asml.remove(hp1);
  634. hp1.free;
  635. end;
  636. {
  637. optimize
  638. mov rX, yyyy
  639. ....
  640. }
  641. if (taicpu(p).ops = 2) and
  642. GetNextInstruction(p,hp1) and
  643. (tai(hp1).typ = ait_instruction) then
  644. begin
  645. {
  646. This changes the very common
  647. mov r0, #0
  648. str r0, [...]
  649. mov r0, #0
  650. str r0, [...]
  651. and removes all superfluous mov instructions
  652. }
  653. if (taicpu(p).oper[1]^.typ = top_const) and
  654. (taicpu(hp1).opcode=A_STR) then
  655. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], [PF_None]) and
  656. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  657. GetNextInstruction(hp1, hp2) and
  658. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  659. (taicpu(hp2).ops = 2) and
  660. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  661. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  662. begin
  663. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovStrMov done')), hp2);
  664. GetNextInstruction(hp2,hp1);
  665. asml.remove(hp2);
  666. hp2.free;
  667. if not assigned(hp1) then break;
  668. end
  669. {
  670. This removes the first mov from
  671. mov rX,...
  672. mov rX,...
  673. }
  674. else if taicpu(hp1).opcode=A_MOV then
  675. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  676. (taicpu(hp1).ops = 2) and
  677. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  678. { don't remove the first mov if the second is a mov rX,rX }
  679. not(MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)) do
  680. begin
  681. asml.insertbefore(tai_comment.Create(strpnew('Peephole MovMov done')), p);
  682. asml.remove(p);
  683. p.free;
  684. p:=hp1;
  685. GetNextInstruction(hp1,hp1);
  686. if not assigned(hp1) then
  687. break;
  688. end;
  689. end;
  690. {
  691. change
  692. mov r1, r0
  693. add r1, r1, #1
  694. to
  695. add r1, r0, #1
  696. Todo: Make it work for mov+cmp too
  697. CAUTION! If this one is successful p might not be a mov instruction anymore!
  698. }
  699. if (taicpu(p).ops = 2) and
  700. (taicpu(p).oper[1]^.typ = top_reg) and
  701. (taicpu(p).oppostfix = PF_NONE) and
  702. GetNextInstruction(p, hp1) and
  703. (tai(hp1).typ = ait_instruction) and
  704. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  705. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN]) and
  706. {MOV and MVN might only have 2 ops}
  707. (taicpu(hp1).ops = 3) and
  708. (taicpu(hp1).condition in [C_NONE, taicpu(hp1).condition]) and
  709. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  710. (taicpu(hp1).oper[1]^.typ = top_reg) and
  711. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop]) then
  712. begin
  713. { When we get here we still don't know if the registers match}
  714. for I:=1 to 2 do
  715. {
  716. If the first loop was successful p will be replaced with hp1.
  717. The checks will still be ok, because all required information
  718. will also be in hp1 then.
  719. }
  720. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  721. begin
  722. asml.insertbefore(tai_comment.Create(strpnew('Peephole RedundantMovProcess done')), hp1);
  723. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  724. if p<>hp1 then
  725. begin
  726. asml.remove(p);
  727. p.free;
  728. p:=hp1;
  729. end;
  730. end;
  731. end;
  732. { This folds shifterops into following instructions
  733. mov r0, r1, lsl #8
  734. add r2, r3, r0
  735. to
  736. add r2, r3, r1, lsl #8
  737. CAUTION! If this one is successful p might not be a mov instruction anymore!
  738. }
  739. if (taicpu(p).opcode = A_MOV) and
  740. (taicpu(p).ops = 3) and
  741. (taicpu(p).oper[1]^.typ = top_reg) and
  742. (taicpu(p).oper[2]^.typ = top_shifterop) and
  743. (taicpu(p).oppostfix = PF_NONE) and
  744. GetNextInstruction(p, hp1) and
  745. (tai(hp1).typ = ait_instruction) and
  746. (taicpu(hp1).ops >= 2) and {Currently we can't fold into another shifterop}
  747. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  748. (taicpu(hp1).oppostfix = PF_NONE) and
  749. (taicpu(hp1).condition = taicpu(p).condition) and
  750. (taicpu(hp1).opcode in [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  751. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  752. A_CMP, A_CMN]) and
  753. (
  754. {Only ONE of the two src operands is allowed to match}
  755. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  756. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  757. ) then
  758. begin
  759. CopyUsedRegs(TmpUsedRegs);
  760. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  761. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  762. I2:=0
  763. else
  764. I2:=1;
  765. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp1,TmpUsedRegs)) then
  766. for I:=I2 to taicpu(hp1).ops-1 do
  767. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  768. begin
  769. { If the parameter matched on the second op from the RIGHT
  770. we have to switch the parameters, this will not happen for CMP
  771. were we're only evaluating the most right parameter
  772. }
  773. if I <> taicpu(hp1).ops-1 then
  774. begin
  775. {The SUB operators need to be changed when we swap parameters}
  776. case taicpu(hp1).opcode of
  777. A_SUB: tempop:=A_RSB;
  778. A_SBC: tempop:=A_RSC;
  779. A_RSB: tempop:=A_SUB;
  780. A_RSC: tempop:=A_SBC;
  781. else tempop:=taicpu(hp1).opcode;
  782. end;
  783. if taicpu(hp1).ops = 3 then
  784. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  785. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  786. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  787. else
  788. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  789. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  790. taicpu(p).oper[2]^.shifterop^);
  791. end
  792. else
  793. if taicpu(hp1).ops = 3 then
  794. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  795. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  796. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  797. else
  798. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  799. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  800. taicpu(p).oper[2]^.shifterop^);
  801. asml.insertbefore(hp2, p);
  802. asml.remove(p);
  803. asml.remove(hp1);
  804. p.free;
  805. hp1.free;
  806. p:=hp2;
  807. GetNextInstruction(p,hp1);
  808. asml.insertbefore(tai_comment.Create(strpnew('Peephole FoldShiftProcess done')), p);
  809. break;
  810. end;
  811. ReleaseUsedRegs(TmpUsedRegs);
  812. end;
  813. {
  814. Often we see shifts and then a superfluous mov to another register
  815. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  816. }
  817. if (taicpu(p).opcode = A_MOV) and
  818. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  819. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  820. end;
  821. A_ADD,
  822. A_ADC,
  823. A_RSB,
  824. A_RSC,
  825. A_SUB,
  826. A_SBC,
  827. A_AND,
  828. A_BIC,
  829. A_EOR,
  830. A_ORR,
  831. A_MLA,
  832. A_MUL:
  833. begin
  834. {
  835. change
  836. and reg2,reg1,const1
  837. and reg2,reg2,const2
  838. to
  839. and reg2,reg1,(const1 and const2)
  840. }
  841. if (taicpu(p).opcode = A_AND) and
  842. (taicpu(p).oper[1]^.typ = top_reg) and
  843. (taicpu(p).oper[2]^.typ = top_const) and
  844. GetNextInstruction(p, hp1) and
  845. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  846. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  847. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  848. (taicpu(hp1).oper[2]^.typ = top_const) then
  849. begin
  850. asml.insertbefore(tai_comment.Create(strpnew('Peephole AndAnd2And done')), p);
  851. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  852. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  853. asml.remove(hp1);
  854. hp1.free;
  855. end;
  856. {
  857. change
  858. add reg1, ...
  859. mov reg2, reg1
  860. to
  861. add reg2, ...
  862. }
  863. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  864. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  865. end;
  866. A_CMP:
  867. begin
  868. {
  869. change
  870. cmp reg,const1
  871. moveq reg,const1
  872. movne reg,const2
  873. to
  874. cmp reg,const1
  875. movne reg,const2
  876. }
  877. if (taicpu(p).oper[1]^.typ = top_const) and
  878. GetNextInstruction(p, hp1) and
  879. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  880. (taicpu(hp1).oper[1]^.typ = top_const) and
  881. GetNextInstruction(hp1, hp2) and
  882. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  883. (taicpu(hp1).oper[1]^.typ = top_const) then
  884. begin
  885. RemoveRedundantMove(p, hp1, asml);
  886. RemoveRedundantMove(p, hp2, asml);
  887. end;
  888. end;
  889. end;
  890. end;
  891. end;
  892. end;
  893. { instructions modifying the CPSR can be only the last instruction }
  894. function MustBeLast(p : tai) : boolean;
  895. begin
  896. Result:=(p.typ=ait_instruction) and
  897. ((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
  898. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  899. (taicpu(p).oppostfix=PF_S));
  900. end;
  901. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  902. var
  903. p,hp1,hp2: tai;
  904. l : longint;
  905. condition : tasmcond;
  906. hp3: tai;
  907. WasLast: boolean;
  908. { UsedRegs, TmpUsedRegs: TRegSet; }
  909. begin
  910. p := BlockStart;
  911. { UsedRegs := []; }
  912. while (p <> BlockEnd) Do
  913. begin
  914. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  915. case p.Typ Of
  916. Ait_Instruction:
  917. begin
  918. case taicpu(p).opcode Of
  919. A_B:
  920. if taicpu(p).condition<>C_None then
  921. begin
  922. { check for
  923. Bxx xxx
  924. <several instructions>
  925. xxx:
  926. }
  927. l:=0;
  928. WasLast:=False;
  929. GetNextInstruction(p, hp1);
  930. while assigned(hp1) and
  931. (l<=4) and
  932. CanBeCond(hp1) and
  933. { stop on labels }
  934. not(hp1.typ=ait_label) do
  935. begin
  936. inc(l);
  937. if MustBeLast(hp1) then
  938. begin
  939. WasLast:=True;
  940. GetNextInstruction(hp1,hp1);
  941. break;
  942. end
  943. else
  944. GetNextInstruction(hp1,hp1);
  945. end;
  946. if assigned(hp1) then
  947. begin
  948. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  949. begin
  950. if (l<=4) and (l>0) then
  951. begin
  952. condition:=inverse_cond(taicpu(p).condition);
  953. hp2:=p;
  954. GetNextInstruction(p,hp1);
  955. p:=hp1;
  956. repeat
  957. if hp1.typ=ait_instruction then
  958. taicpu(hp1).condition:=condition;
  959. if MustBeLast(hp1) then
  960. begin
  961. GetNextInstruction(hp1,hp1);
  962. break;
  963. end
  964. else
  965. GetNextInstruction(hp1,hp1);
  966. until not(assigned(hp1)) or
  967. not(CanBeCond(hp1)) or
  968. (hp1.typ=ait_label);
  969. { wait with removing else GetNextInstruction could
  970. ignore the label if it was the only usage in the
  971. jump moved away }
  972. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  973. asml.remove(hp2);
  974. hp2.free;
  975. continue;
  976. end;
  977. end
  978. else
  979. { do not perform further optimizations if there is inctructon
  980. in block #1 which can not be optimized.
  981. }
  982. if not WasLast then
  983. begin
  984. { check further for
  985. Bcc xxx
  986. <several instructions 1>
  987. B yyy
  988. xxx:
  989. <several instructions 2>
  990. yyy:
  991. }
  992. { hp2 points to jmp yyy }
  993. hp2:=hp1;
  994. { skip hp1 to xxx }
  995. GetNextInstruction(hp1, hp1);
  996. if assigned(hp2) and
  997. assigned(hp1) and
  998. (l<=3) and
  999. (hp2.typ=ait_instruction) and
  1000. (taicpu(hp2).is_jmp) and
  1001. (taicpu(hp2).condition=C_None) and
  1002. { real label and jump, no further references to the
  1003. label are allowed }
  1004. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  1005. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1006. begin
  1007. l:=0;
  1008. { skip hp1 to <several moves 2> }
  1009. GetNextInstruction(hp1, hp1);
  1010. while assigned(hp1) and
  1011. CanBeCond(hp1) do
  1012. begin
  1013. inc(l);
  1014. GetNextInstruction(hp1, hp1);
  1015. end;
  1016. { hp1 points to yyy: }
  1017. if assigned(hp1) and
  1018. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  1019. begin
  1020. condition:=inverse_cond(taicpu(p).condition);
  1021. GetNextInstruction(p,hp1);
  1022. hp3:=p;
  1023. p:=hp1;
  1024. repeat
  1025. if hp1.typ=ait_instruction then
  1026. taicpu(hp1).condition:=condition;
  1027. GetNextInstruction(hp1,hp1);
  1028. until not(assigned(hp1)) or
  1029. not(CanBeCond(hp1));
  1030. { hp2 is still at jmp yyy }
  1031. GetNextInstruction(hp2,hp1);
  1032. { hp2 is now at xxx: }
  1033. condition:=inverse_cond(condition);
  1034. GetNextInstruction(hp1,hp1);
  1035. { hp1 is now at <several movs 2> }
  1036. repeat
  1037. taicpu(hp1).condition:=condition;
  1038. GetNextInstruction(hp1,hp1);
  1039. until not(assigned(hp1)) or
  1040. not(CanBeCond(hp1)) or
  1041. (hp1.typ=ait_label);
  1042. {
  1043. asml.remove(hp1.next)
  1044. hp1.next.free;
  1045. asml.remove(hp1);
  1046. hp1.free;
  1047. }
  1048. { remove Bcc }
  1049. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  1050. asml.remove(hp3);
  1051. hp3.free;
  1052. { remove jmp }
  1053. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1054. asml.remove(hp2);
  1055. hp2.free;
  1056. continue;
  1057. end;
  1058. end;
  1059. end;
  1060. end;
  1061. end;
  1062. end;
  1063. end;
  1064. end;
  1065. p := tai(p.next)
  1066. end;
  1067. end;
  1068. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  1069. begin
  1070. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  1071. Result:=true
  1072. else
  1073. Result:=inherited RegInInstruction(Reg, p1);
  1074. end;
  1075. const
  1076. { set of opcode which might or do write to memory }
  1077. { TODO : extend armins.dat to contain r/w info }
  1078. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  1079. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  1080. { adjust the register live information when swapping the two instructions p and hp1,
  1081. they must follow one after the other }
  1082. procedure TCpuPreRegallocScheduler.SwapRegLive(p,hp1 : taicpu);
  1083. procedure CheckLiveEnd(reg : tregister);
  1084. var
  1085. supreg : TSuperRegister;
  1086. regtype : TRegisterType;
  1087. begin
  1088. if reg=NR_NO then
  1089. exit;
  1090. regtype:=getregtype(reg);
  1091. supreg:=getsupreg(reg);
  1092. if (cg.rg[regtype].live_end[supreg]=hp1) and
  1093. RegInInstruction(reg,p) then
  1094. cg.rg[regtype].live_end[supreg]:=p;
  1095. end;
  1096. procedure CheckLiveStart(reg : TRegister);
  1097. var
  1098. supreg : TSuperRegister;
  1099. regtype : TRegisterType;
  1100. begin
  1101. if reg=NR_NO then
  1102. exit;
  1103. regtype:=getregtype(reg);
  1104. supreg:=getsupreg(reg);
  1105. if (cg.rg[regtype].live_start[supreg]=p) and
  1106. RegInInstruction(reg,hp1) then
  1107. cg.rg[regtype].live_start[supreg]:=hp1;
  1108. end;
  1109. var
  1110. i : longint;
  1111. r : TSuperRegister;
  1112. begin
  1113. { assumption: p is directly followed by hp1 }
  1114. { if live of any reg used by p starts at p and hp1 uses this register then
  1115. set live start to hp1 }
  1116. for i:=0 to p.ops-1 do
  1117. case p.oper[i]^.typ of
  1118. Top_Reg:
  1119. CheckLiveStart(p.oper[i]^.reg);
  1120. Top_Ref:
  1121. begin
  1122. CheckLiveStart(p.oper[i]^.ref^.base);
  1123. CheckLiveStart(p.oper[i]^.ref^.index);
  1124. end;
  1125. Top_Shifterop:
  1126. CheckLiveStart(p.oper[i]^.shifterop^.rs);
  1127. Top_RegSet:
  1128. for r:=RS_R0 to RS_R15 do
  1129. if r in p.oper[i]^.regset^ then
  1130. CheckLiveStart(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1131. end;
  1132. { if live of any reg used by hp1 ends at hp1 and p uses this register then
  1133. set live end to p }
  1134. for i:=0 to hp1.ops-1 do
  1135. case hp1.oper[i]^.typ of
  1136. Top_Reg:
  1137. CheckLiveEnd(hp1.oper[i]^.reg);
  1138. Top_Ref:
  1139. begin
  1140. CheckLiveEnd(hp1.oper[i]^.ref^.base);
  1141. CheckLiveEnd(hp1.oper[i]^.ref^.index);
  1142. end;
  1143. Top_Shifterop:
  1144. CheckLiveStart(hp1.oper[i]^.shifterop^.rs);
  1145. Top_RegSet:
  1146. for r:=RS_R0 to RS_R15 do
  1147. if r in hp1.oper[i]^.regset^ then
  1148. CheckLiveEnd(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1149. end;
  1150. end;
  1151. function TCpuPreRegallocScheduler.SchedulerPass1Cpu(var p: tai): boolean;
  1152. { TODO : schedule also forward }
  1153. { TODO : schedule distance > 1 }
  1154. var
  1155. hp1,hp2,hp3,hp4,hp5 : tai;
  1156. list : TAsmList;
  1157. begin
  1158. result:=true;
  1159. list:=TAsmList.Create;
  1160. p:=BlockStart;
  1161. while p<>BlockEnd Do
  1162. begin
  1163. if (p.typ=ait_instruction) and
  1164. GetNextInstruction(p,hp1) and
  1165. (hp1.typ=ait_instruction) and
  1166. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  1167. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  1168. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  1169. not(RegModifiedByInstruction(NR_PC,p))
  1170. ) or
  1171. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  1172. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  1173. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  1174. (taicpu(hp1).oper[1]^.ref^.offset=0)
  1175. )
  1176. ) or
  1177. { try to prove that the memory accesses don't overlapp }
  1178. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  1179. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  1180. (taicpu(p).oppostfix=PF_None) and
  1181. (taicpu(hp1).oppostfix=PF_None) and
  1182. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  1183. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1184. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  1185. (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)]))
  1186. )
  1187. )
  1188. ) and
  1189. GetNextInstruction(hp1,hp2) and
  1190. (hp2.typ=ait_instruction) and
  1191. { loaded register used by next instruction? }
  1192. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  1193. { loaded register not used by previous instruction? }
  1194. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  1195. { same condition? }
  1196. (taicpu(p).condition=taicpu(hp1).condition) and
  1197. { first instruction might not change the register used as base }
  1198. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  1199. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  1200. ) and
  1201. { first instruction might not change the register used as index }
  1202. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  1203. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  1204. ) then
  1205. begin
  1206. hp3:=tai(p.Previous);
  1207. hp5:=tai(p.next);
  1208. asml.Remove(p);
  1209. { if there is a reg. dealloc instruction associated with p, move it together with p }
  1210. { before the instruction? }
  1211. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  1212. begin
  1213. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  1214. RegInInstruction(tai_regalloc(hp3).reg,p) then
  1215. begin
  1216. hp4:=hp3;
  1217. hp3:=tai(hp3.Previous);
  1218. asml.Remove(hp4);
  1219. list.Concat(hp4);
  1220. end
  1221. else
  1222. hp3:=tai(hp3.Previous);
  1223. end;
  1224. list.Concat(p);
  1225. SwapRegLive(taicpu(p),taicpu(hp1));
  1226. { after the instruction? }
  1227. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  1228. begin
  1229. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  1230. RegInInstruction(tai_regalloc(hp5).reg,p) then
  1231. begin
  1232. hp4:=hp5;
  1233. hp5:=tai(hp5.next);
  1234. asml.Remove(hp4);
  1235. list.Concat(hp4);
  1236. end
  1237. else
  1238. hp5:=tai(hp5.Next);
  1239. end;
  1240. asml.Remove(hp1);
  1241. {$ifdef DEBUG_PREREGSCHEDULER}
  1242. asml.InsertBefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  1243. {$endif DEBUG_PREREGSCHEDULER}
  1244. asml.InsertBefore(hp1,hp2);
  1245. asml.InsertListBefore(hp2,list);
  1246. p:=tai(p.next)
  1247. end
  1248. else if p.typ=ait_instruction then
  1249. p:=hp1
  1250. else
  1251. p:=tai(p.next);
  1252. end;
  1253. list.Free;
  1254. end;
  1255. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  1256. begin
  1257. { TODO: Add optimizer code }
  1258. end;
  1259. begin
  1260. casmoptimizer:=TCpuAsmOptimizer;
  1261. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  1262. End.