aoptcpu.pas 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  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. {$define DEBUG_AOPTCPU}
  22. Interface
  23. uses cgbase, cpubase, aasmtai, aasmcpu,aopt, aoptcpub, aoptobj, cclasses;
  24. Type
  25. TCpuAsmOptimizer = class(TAsmOptimizer)
  26. { uses the same constructor as TAopObj }
  27. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  28. procedure PeepHoleOptPass2;override;
  29. Function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  30. procedure RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  31. function RegUsedAfterInstruction(reg: Tregister; p: tai;
  32. var AllUsedRegs: TAllUsedRegs): Boolean;
  33. { gets the next tai object after current that contains info relevant
  34. to the optimizer in p1 which used the given register or does a
  35. change in program flow.
  36. If there is none, it returns false and
  37. sets p1 to nil }
  38. Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
  39. { outputs a debug message into the assembler file }
  40. procedure DebugMsg(const s: string; p: tai);
  41. protected
  42. function LookForPostindexedPattern(p: taicpu): boolean;
  43. End;
  44. TCpuPreRegallocScheduler = class(TAsmScheduler)
  45. function SchedulerPass1Cpu(var p: tai): boolean;override;
  46. procedure SwapRegLive(p, hp1: taicpu);
  47. end;
  48. TCpuThumb2AsmOptimizer = class(TCpuAsmOptimizer)
  49. { uses the same constructor as TAopObj }
  50. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  51. procedure PeepHoleOptPass2;override;
  52. End;
  53. function MustBeLast(p : tai) : boolean;
  54. Implementation
  55. uses
  56. cutils,verbose,globals,
  57. systems,
  58. cpuinfo,
  59. cgobj,cgutils,procinfo,
  60. aasmbase,aasmdata;
  61. function CanBeCond(p : tai) : boolean;
  62. begin
  63. result:=
  64. (p.typ=ait_instruction) and
  65. (taicpu(p).condition=C_None) and
  66. ((taicpu(p).opcode<A_IT) or (taicpu(p).opcode>A_ITTTT)) and
  67. (taicpu(p).opcode<>A_CBZ) and
  68. (taicpu(p).opcode<>A_CBNZ) and
  69. (taicpu(p).opcode<>A_PLD) and
  70. ((taicpu(p).opcode<>A_BLX) or
  71. (taicpu(p).oper[0]^.typ=top_reg));
  72. end;
  73. function RefsEqual(const r1, r2: treference): boolean;
  74. begin
  75. refsequal :=
  76. (r1.offset = r2.offset) and
  77. (r1.base = r2.base) and
  78. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  79. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  80. (r1.relsymbol = r2.relsymbol) and
  81. (r1.signindex = r2.signindex) and
  82. (r1.shiftimm = r2.shiftimm) and
  83. (r1.addressmode = r2.addressmode) and
  84. (r1.shiftmode = r2.shiftmode);
  85. end;
  86. function MatchInstruction(const instr: tai; const op: TCommonAsmOps; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  87. begin
  88. result :=
  89. (instr.typ = ait_instruction) and
  90. ((op = []) or ((ord(taicpu(instr).opcode)<256) and (taicpu(instr).opcode in op))) and
  91. ((cond = []) or (taicpu(instr).condition in cond)) and
  92. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  93. end;
  94. function MatchInstruction(const instr: tai; const op: TAsmOp; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  95. begin
  96. result :=
  97. (instr.typ = ait_instruction) and
  98. (taicpu(instr).opcode = op) and
  99. ((cond = []) or (taicpu(instr).condition in cond)) and
  100. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  101. end;
  102. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  103. begin
  104. result := oper1.typ = oper2.typ;
  105. if result then
  106. case oper1.typ of
  107. top_const:
  108. Result:=oper1.val = oper2.val;
  109. top_reg:
  110. Result:=oper1.reg = oper2.reg;
  111. top_conditioncode:
  112. Result:=oper1.cc = oper2.cc;
  113. top_ref:
  114. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  115. else Result:=false;
  116. end
  117. end;
  118. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  119. begin
  120. result := (oper.typ = top_reg) and (oper.reg = reg);
  121. end;
  122. procedure RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList);
  123. begin
  124. if (taicpu(movp).condition = C_EQ) and
  125. (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and
  126. (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
  127. begin
  128. asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
  129. asml.remove(movp);
  130. movp.free;
  131. end;
  132. end;
  133. function regLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  134. var
  135. p: taicpu;
  136. begin
  137. p := taicpu(hp);
  138. regLoadedWithNewValue := false;
  139. if not ((assigned(hp)) and (hp.typ = ait_instruction)) then
  140. exit;
  141. case p.opcode of
  142. { These operands do not write into a register at all }
  143. A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX, A_SWI, A_MSR, A_PLD:
  144. exit;
  145. {Take care of post/preincremented store and loads, they will change their base register}
  146. A_STR, A_LDR:
  147. begin
  148. regLoadedWithNewValue :=
  149. (taicpu(p).oper[1]^.typ=top_ref) and
  150. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  151. (taicpu(p).oper[1]^.ref^.base = reg);
  152. {STR does not load into it's first register}
  153. if p.opcode = A_STR then exit;
  154. end;
  155. { These four are writing into the first 2 register, UMLAL and SMLAL will also read from them }
  156. A_UMLAL, A_UMULL, A_SMLAL, A_SMULL:
  157. regLoadedWithNewValue :=
  158. (p.oper[1]^.typ = top_reg) and
  159. (p.oper[1]^.reg = reg);
  160. {Loads to oper2 from coprocessor}
  161. {
  162. MCR/MRC is currently not supported in FPC
  163. A_MRC:
  164. regLoadedWithNewValue :=
  165. (p.oper[2]^.typ = top_reg) and
  166. (p.oper[2]^.reg = reg);
  167. }
  168. {Loads to all register in the registerset}
  169. A_LDM:
  170. regLoadedWithNewValue := (getsupreg(reg) in p.oper[1]^.regset^);
  171. end;
  172. if regLoadedWithNewValue then
  173. exit;
  174. case p.oper[0]^.typ of
  175. {This is the case}
  176. top_reg:
  177. regLoadedWithNewValue := (p.oper[0]^.reg = reg) or
  178. { LDRD }
  179. (p.opcode=A_LDR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg));
  180. {LDM/STM might write a new value to their index register}
  181. top_ref:
  182. regLoadedWithNewValue :=
  183. (taicpu(p).oper[0]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  184. (taicpu(p).oper[0]^.ref^.base = reg);
  185. end;
  186. end;
  187. function AlignedToQWord(const ref : treference) : boolean;
  188. begin
  189. { (safe) heuristics to ensure alignment }
  190. result:=(target_info.abi in [abi_eabi,abi_armeb,abi_eabihf]) and
  191. (((ref.offset>=0) and
  192. ((ref.offset mod 8)=0) and
  193. ((ref.base=NR_R13) or
  194. (ref.index=NR_R13))
  195. ) or
  196. ((ref.offset<=0) and
  197. { when using NR_R11, it has always a value of <qword align>+4 }
  198. ((abs(ref.offset+4) mod 8)=0) and
  199. (current_procinfo.framepointer=NR_R11) and
  200. ((ref.base=NR_R11) or
  201. (ref.index=NR_R11))
  202. )
  203. );
  204. end;
  205. function instructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  206. var
  207. p: taicpu;
  208. i: longint;
  209. begin
  210. instructionLoadsFromReg := false;
  211. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  212. exit;
  213. p:=taicpu(hp);
  214. i:=1;
  215. {For these instructions we have to start on oper[0]}
  216. if (p.opcode in [A_STR, A_LDM, A_STM, A_PLD,
  217. A_CMP, A_CMN, A_TST, A_TEQ,
  218. A_B, A_BL, A_BX, A_BLX,
  219. A_SMLAL, A_UMLAL]) then i:=0;
  220. while(i<p.ops) do
  221. begin
  222. case p.oper[I]^.typ of
  223. top_reg:
  224. instructionLoadsFromReg := (p.oper[I]^.reg = reg) or
  225. { STRD }
  226. ((i=0) and (p.opcode=A_STR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg)));
  227. top_regset:
  228. instructionLoadsFromReg := (getsupreg(reg) in p.oper[I]^.regset^);
  229. top_shifterop:
  230. instructionLoadsFromReg := p.oper[I]^.shifterop^.rs = reg;
  231. top_ref:
  232. instructionLoadsFromReg :=
  233. (p.oper[I]^.ref^.base = reg) or
  234. (p.oper[I]^.ref^.index = reg);
  235. end;
  236. if instructionLoadsFromReg then exit; {Bailout if we found something}
  237. Inc(I);
  238. end;
  239. end;
  240. function isValidConstLoadStoreOffset(const aoffset: longint; const pf: TOpPostfix) : boolean;
  241. begin
  242. if current_settings.cputype in cpu_thumb2 then
  243. result := (aoffset<4096) and (aoffset>-256)
  244. else
  245. result := ((pf in [PF_None,PF_B]) and
  246. (abs(aoffset)<4096)) or
  247. (abs(aoffset)<256);
  248. end;
  249. function TCpuAsmOptimizer.RegUsedAfterInstruction(reg: Tregister; p: tai;
  250. var AllUsedRegs: TAllUsedRegs): Boolean;
  251. begin
  252. AllUsedRegs[getregtype(reg)].Update(tai(p.Next),true);
  253. RegUsedAfterInstruction :=
  254. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  255. not(regLoadedWithNewValue(reg,p)) and
  256. (
  257. not(GetNextInstruction(p,p)) or
  258. instructionLoadsFromReg(reg,p) or
  259. not(regLoadedWithNewValue(reg,p))
  260. );
  261. end;
  262. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  263. var Next: tai; reg: TRegister): Boolean;
  264. begin
  265. Next:=Current;
  266. repeat
  267. Result:=GetNextInstruction(Next,Next);
  268. until not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  269. (is_calljmp(taicpu(Next).opcode)) or (RegInInstruction(NR_PC,Next));
  270. end;
  271. {$ifdef DEBUG_AOPTCPU}
  272. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  273. begin
  274. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  275. end;
  276. {$else DEBUG_AOPTCPU}
  277. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  278. begin
  279. end;
  280. {$endif DEBUG_AOPTCPU}
  281. procedure TCpuAsmOptimizer.RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  282. var
  283. alloc,
  284. dealloc : tai_regalloc;
  285. hp1 : tai;
  286. begin
  287. if MatchInstruction(movp, A_MOV, [taicpu(p).condition], [PF_None]) and
  288. (taicpu(movp).ops=2) and {We can't optimize if there is a shiftop}
  289. MatchOperand(taicpu(movp).oper[1]^, taicpu(p).oper[0]^.reg) and
  290. { don't mess with moves to pc }
  291. (taicpu(movp).oper[0]^.reg<>NR_PC) and
  292. { don't mess with moves to lr }
  293. (taicpu(movp).oper[0]^.reg<>NR_R14) and
  294. { the destination register of the mov might not be used beween p and movp }
  295. not(RegUsedBetween(taicpu(movp).oper[0]^.reg,p,movp)) and
  296. { cb[n]z are thumb instructions which require specific registers, with no wide forms }
  297. (taicpu(p).opcode<>A_CBZ) and
  298. (taicpu(p).opcode<>A_CBNZ) and
  299. {There is a special requirement for MUL and MLA, oper[0] and oper[1] are not allowed to be the same}
  300. not (
  301. (taicpu(p).opcode in [A_MLA, A_MUL]) and
  302. (taicpu(p).oper[1]^.reg = taicpu(movp).oper[0]^.reg) and
  303. (current_settings.cputype < cpu_armv6)
  304. ) and
  305. { Take care to only do this for instructions which REALLY load to the first register.
  306. Otherwise
  307. str reg0, [reg1]
  308. mov reg2, reg0
  309. will be optimized to
  310. str reg2, [reg1]
  311. }
  312. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, p) then
  313. begin
  314. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(movp.Next));
  315. if assigned(dealloc) then
  316. begin
  317. DebugMsg('Peephole '+optimizer+' removed superfluous mov', movp);
  318. { taicpu(p).oper[0]^.reg is not used anymore, try to find its allocation
  319. and remove it if possible }
  320. GetLastInstruction(p,hp1);
  321. asml.Remove(dealloc);
  322. alloc:=FindRegAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  323. if assigned(alloc) then
  324. begin
  325. asml.Remove(alloc);
  326. alloc.free;
  327. dealloc.free;
  328. end
  329. else
  330. asml.InsertAfter(dealloc,p);
  331. { try to move the allocation of the target register }
  332. GetLastInstruction(movp,hp1);
  333. alloc:=FindRegAlloc(taicpu(movp).oper[0]^.reg,tai(hp1.Next));
  334. if assigned(alloc) then
  335. begin
  336. asml.Remove(alloc);
  337. asml.InsertBefore(alloc,p);
  338. { adjust used regs }
  339. IncludeRegInUsedRegs(taicpu(movp).oper[0]^.reg,UsedRegs);
  340. end;
  341. { finally get rid of the mov }
  342. taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
  343. asml.remove(movp);
  344. movp.free;
  345. end;
  346. end;
  347. end;
  348. {
  349. optimize
  350. ldr/str regX,[reg1]
  351. ...
  352. add/sub reg1,reg1,regY/const
  353. into
  354. ldr/str regX,[reg1], regY/const
  355. }
  356. function TCpuAsmOptimizer.LookForPostindexedPattern(p: taicpu) : boolean;
  357. var
  358. hp1 : tai;
  359. begin
  360. Result:=false;
  361. if (p.oper[1]^.ref^.addressmode=AM_OFFSET) and
  362. (p.oper[1]^.ref^.index=NR_NO) and
  363. (p.oper[1]^.ref^.offset=0) and
  364. GetNextInstructionUsingReg(p, hp1, p.oper[1]^.ref^.base) and
  365. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  366. MatchInstruction(hp1, [A_ADD, A_SUB], [C_None], [PF_None]) and
  367. (taicpu(hp1).oper[0]^.reg=p.oper[1]^.ref^.base) and
  368. (taicpu(hp1).oper[1]^.reg=p.oper[1]^.ref^.base) and
  369. (
  370. (taicpu(hp1).oper[2]^.typ=top_reg) or
  371. { valid offset? }
  372. ((taicpu(hp1).oper[2]^.typ=top_const) and
  373. ((abs(taicpu(hp1).oper[2]^.val)<256) or
  374. ((abs(taicpu(hp1).oper[2]^.val)<4096) and (p.oppostfix in [PF_None,PF_B]))
  375. )
  376. )
  377. ) and
  378. { don't apply the optimization if the base register is loaded }
  379. (p.oper[0]^.reg<>p.oper[1]^.ref^.base) and
  380. not(RegModifiedBetween(taicpu(hp1).oper[0]^.reg,p,hp1)) and
  381. { don't apply the optimization if the (new) index register is loaded }
  382. (p.oper[0]^.reg<>taicpu(hp1).oper[2]^.reg) and
  383. not(RegModifiedBetween(taicpu(hp1).oper[2]^.reg,p,hp1)) then
  384. begin
  385. DebugMsg('Peephole Str/LdrAdd/Sub2Str/Ldr Postindex done', p);
  386. p.oper[1]^.ref^.addressmode:=AM_POSTINDEXED;
  387. if taicpu(hp1).oper[2]^.typ=top_const then
  388. begin
  389. if taicpu(hp1).opcode=A_ADD then
  390. p.oper[1]^.ref^.offset:=taicpu(hp1).oper[2]^.val
  391. else
  392. p.oper[1]^.ref^.offset:=-taicpu(hp1).oper[2]^.val;
  393. end
  394. else
  395. begin
  396. p.oper[1]^.ref^.index:=taicpu(hp1).oper[2]^.reg;
  397. if taicpu(hp1).opcode=A_ADD then
  398. p.oper[1]^.ref^.signindex:=1
  399. else
  400. p.oper[1]^.ref^.signindex:=-1;
  401. end;
  402. asml.Remove(hp1);
  403. hp1.Free;
  404. Result:=true;
  405. end;
  406. end;
  407. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  408. var
  409. hp1,hp2: tai;
  410. i, i2: longint;
  411. TmpUsedRegs: TAllUsedRegs;
  412. tempop: tasmop;
  413. function IsPowerOf2(const value: DWord): boolean; inline;
  414. begin
  415. Result:=(value and (value - 1)) = 0;
  416. end;
  417. begin
  418. result := false;
  419. case p.typ of
  420. ait_instruction:
  421. begin
  422. {
  423. change
  424. <op> reg,x,y
  425. cmp reg,#0
  426. into
  427. <op>s reg,x,y
  428. }
  429. { this optimization can applied only to the currently enabled operations because
  430. the other operations do not update all flags and FPC does not track flag usage }
  431. if MatchInstruction(p, [A_ADC,A_ADD,A_BIC,A_SUB,A_MUL,A_MVN,A_MOV,A_ORR,A_EOR,A_AND,
  432. A_RSB,A_RSC,A_SBC,A_MLA], [C_None], [PF_None]) and
  433. GetNextInstruction(p, hp1) and
  434. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  435. (taicpu(hp1).oper[1]^.typ = top_const) and
  436. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  437. (taicpu(hp1).oper[1]^.val = 0) and
  438. GetNextInstruction(hp1, hp2) and
  439. { be careful here, following instructions could use other flags
  440. however after a jump fpc never depends on the value of flags }
  441. { All above instructions set Z and N according to the following
  442. Z := result = 0;
  443. N := result[31];
  444. EQ = Z=1; NE = Z=0;
  445. MI = N=1; PL = N=0; }
  446. MatchInstruction(hp2, A_B, [C_EQ,C_NE,C_MI,C_PL], []) and
  447. assigned(FindRegDealloc(NR_DEFAULTFLAGS,tai(hp2.Next))) then
  448. begin
  449. DebugMsg('Peephole OpCmp2OpS done', p);
  450. taicpu(p).oppostfix:=PF_S;
  451. { move flag allocation if possible }
  452. GetLastInstruction(hp1, hp2);
  453. hp2:=FindRegAlloc(NR_DEFAULTFLAGS,tai(hp2.Next));
  454. if assigned(hp2) then
  455. begin
  456. asml.Remove(hp2);
  457. asml.insertbefore(hp2, p);
  458. end;
  459. asml.remove(hp1);
  460. hp1.free;
  461. end
  462. else
  463. case taicpu(p).opcode of
  464. A_STR:
  465. begin
  466. { change
  467. str reg1,ref
  468. ldr reg2,ref
  469. into
  470. str reg1,ref
  471. mov reg2,reg1
  472. }
  473. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  474. (taicpu(p).oppostfix=PF_None) and
  475. GetNextInstruction(p,hp1) and
  476. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [PF_None]) and
  477. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  478. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  479. begin
  480. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  481. begin
  482. DebugMsg('Peephole StrLdr2StrMov 1 done', hp1);
  483. asml.remove(hp1);
  484. hp1.free;
  485. end
  486. else
  487. begin
  488. taicpu(hp1).opcode:=A_MOV;
  489. taicpu(hp1).oppostfix:=PF_None;
  490. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  491. DebugMsg('Peephole StrLdr2StrMov 2 done', hp1);
  492. end;
  493. result := true;
  494. end
  495. { change
  496. str reg1,ref
  497. str reg2,ref
  498. into
  499. strd reg1,ref
  500. }
  501. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  502. (taicpu(p).oppostfix=PF_None) and
  503. (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  504. GetNextInstruction(p,hp1) and
  505. MatchInstruction(hp1, A_STR, [taicpu(p).condition, C_None], [PF_None]) and
  506. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  507. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  508. { str ensures that either base or index contain no register, else ldr wouldn't
  509. use an offset either
  510. }
  511. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  512. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  513. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  514. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  515. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  516. begin
  517. DebugMsg('Peephole StrStr2Strd done', p);
  518. taicpu(p).oppostfix:=PF_D;
  519. asml.remove(hp1);
  520. hp1.free;
  521. end;
  522. LookForPostindexedPattern(taicpu(p));
  523. end;
  524. A_LDR:
  525. begin
  526. { change
  527. ldr reg1,ref
  528. ldr reg2,ref
  529. into ...
  530. }
  531. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  532. GetNextInstruction(p,hp1) and
  533. { ldrd is not allowed here }
  534. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix,PF_None]-[PF_D]) then
  535. begin
  536. {
  537. ...
  538. ldr reg1,ref
  539. mov reg2,reg1
  540. }
  541. if RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  542. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  543. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  544. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  545. begin
  546. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  547. begin
  548. DebugMsg('Peephole LdrLdr2Ldr done', hp1);
  549. asml.remove(hp1);
  550. hp1.free;
  551. end
  552. else
  553. begin
  554. DebugMsg('Peephole LdrLdr2LdrMov done', hp1);
  555. taicpu(hp1).opcode:=A_MOV;
  556. taicpu(hp1).oppostfix:=PF_None;
  557. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  558. end;
  559. result := true;
  560. end
  561. {
  562. ...
  563. ldrd reg1,ref
  564. }
  565. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  566. { ldrd does not allow any postfixes ... }
  567. (taicpu(p).oppostfix=PF_None) and
  568. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  569. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  570. { ldr ensures that either base or index contain no register, else ldr wouldn't
  571. use an offset either
  572. }
  573. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  574. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  575. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  576. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  577. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  578. begin
  579. DebugMsg('Peephole LdrLdr2Ldrd done', p);
  580. taicpu(p).oppostfix:=PF_D;
  581. asml.remove(hp1);
  582. hp1.free;
  583. end;
  584. end;
  585. LookForPostindexedPattern(taicpu(p));
  586. { Remove superfluous mov after ldr
  587. changes
  588. ldr reg1, ref
  589. mov reg2, reg1
  590. to
  591. ldr reg2, ref
  592. conditions are:
  593. * no ldrd usage
  594. * reg1 must be released after mov
  595. * mov can not contain shifterops
  596. * ldr+mov have the same conditions
  597. * mov does not set flags
  598. }
  599. if (taicpu(p).oppostfix<>PF_D) and GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  600. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr');
  601. end;
  602. A_MOV:
  603. begin
  604. { fold
  605. mov reg1,reg0, shift imm1
  606. mov reg1,reg1, shift imm2
  607. }
  608. if (taicpu(p).ops=3) and
  609. (taicpu(p).oper[2]^.typ = top_shifterop) and
  610. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  611. getnextinstruction(p,hp1) and
  612. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  613. (taicpu(hp1).ops=3) and
  614. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  615. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  616. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  617. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  618. begin
  619. { fold
  620. mov reg1,reg0, lsl 16
  621. mov reg1,reg1, lsr 16
  622. strh reg1, ...
  623. dealloc reg1
  624. to
  625. strh reg1, ...
  626. dealloc reg1
  627. }
  628. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  629. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  630. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  631. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  632. getnextinstruction(hp1,hp2) and
  633. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  634. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  635. begin
  636. CopyUsedRegs(TmpUsedRegs);
  637. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  638. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  639. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  640. begin
  641. DebugMsg('Peephole optimizer removed superfluous 16 Bit zero extension', hp1);
  642. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  643. asml.remove(p);
  644. asml.remove(hp1);
  645. p.free;
  646. hp1.free;
  647. p:=hp2;
  648. end;
  649. ReleaseUsedRegs(TmpUsedRegs);
  650. end
  651. { fold
  652. mov reg1,reg0, shift imm1
  653. mov reg1,reg1, shift imm2
  654. to
  655. mov reg1,reg0, shift imm1+imm2
  656. }
  657. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) or
  658. { asr makes no use after a lsr, the asr can be foled into the lsr }
  659. ((taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSR) and (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_ASR) ) then
  660. begin
  661. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  662. { avoid overflows }
  663. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  664. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  665. SM_ROR:
  666. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  667. SM_ASR:
  668. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  669. SM_LSR,
  670. SM_LSL:
  671. begin
  672. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  673. InsertLLItem(p.previous, p.next, hp1);
  674. p.free;
  675. p:=hp1;
  676. end;
  677. else
  678. internalerror(2008072803);
  679. end;
  680. DebugMsg('Peephole ShiftShift2Shift 1 done', p);
  681. asml.remove(hp1);
  682. hp1.free;
  683. result := true;
  684. end
  685. { fold
  686. mov reg1,reg0, shift imm1
  687. mov reg1,reg1, shift imm2
  688. mov reg1,reg1, shift imm3 ...
  689. }
  690. else if getnextinstruction(hp1,hp2) and
  691. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  692. (taicpu(hp2).ops=3) and
  693. MatchOperand(taicpu(hp2).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  694. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  695. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  696. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  697. begin
  698. { mov reg1,reg0, lsl imm1
  699. mov reg1,reg1, lsr/asr imm2
  700. mov reg1,reg1, lsl imm3 ...
  701. if imm3<=imm1 and imm2>=imm3
  702. to
  703. mov reg1,reg0, lsl imm1
  704. mov reg1,reg1, lsr/asr imm2-imm3
  705. }
  706. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  707. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  708. (taicpu(hp2).oper[2]^.shifterop^.shiftimm<=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  709. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(hp2).oper[2]^.shifterop^.shiftimm) then
  710. begin
  711. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  712. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1 done', p);
  713. asml.remove(hp2);
  714. hp2.free;
  715. result := true;
  716. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  717. begin
  718. asml.remove(hp1);
  719. hp1.free;
  720. end;
  721. end
  722. { mov reg1,reg0, lsr/asr imm1
  723. mov reg1,reg1, lsl imm2
  724. mov reg1,reg1, lsr/asr imm3 ...
  725. if imm3>=imm1 and imm2>=imm1
  726. to
  727. mov reg1,reg0, lsl imm2-imm1
  728. mov reg1,reg1, lsr/asr imm3 ...
  729. }
  730. 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
  731. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  732. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  733. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  734. begin
  735. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  736. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  737. DebugMsg('Peephole ShiftShiftShift2ShiftShift 2 done', p);
  738. asml.remove(p);
  739. p.free;
  740. p:=hp2;
  741. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  742. begin
  743. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  744. asml.remove(hp1);
  745. hp1.free;
  746. p:=hp2;
  747. end;
  748. result := true;
  749. end;
  750. end;
  751. end;
  752. { Change the common
  753. mov r0, r0, lsr #24
  754. and r0, r0, #255
  755. and remove the superfluous and
  756. This could be extended to handle more cases.
  757. }
  758. if (taicpu(p).ops=3) and
  759. (taicpu(p).oper[2]^.typ = top_shifterop) and
  760. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  761. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  762. (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  763. GetNextInstructionUsingReg(p,hp1, taicpu(p).oper[0]^.reg) and
  764. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  765. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp1)) and
  766. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  767. (taicpu(hp1).ops=3) and
  768. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  769. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  770. (taicpu(hp1).oper[2]^.typ = top_const) and
  771. { Check if the AND actually would only mask out bits beeing already zero because of the shift
  772. For LSR #25 and an AndConst of 255 that whould go like this:
  773. 255 and ((2 shl (32-25))-1)
  774. which results in 127, which is one less a power-of-2, meaning all lower bits are set.
  775. LSR #25 and AndConst of 254:
  776. 254 and ((2 shl (32-25))-1) = 126 -> lowest bit is clear, so we can't remove it.
  777. }
  778. ispowerof2((taicpu(hp1).oper[2]^.val and ((2 shl (32-taicpu(p).oper[2]^.shifterop^.shiftimm))-1))+1) then
  779. begin
  780. DebugMsg('Peephole LsrAnd2Lsr done', hp1);
  781. asml.remove(hp1);
  782. hp1.free;
  783. end;
  784. {
  785. optimize
  786. mov rX, yyyy
  787. ....
  788. }
  789. if (taicpu(p).ops = 2) and
  790. GetNextInstruction(p,hp1) and
  791. (tai(hp1).typ = ait_instruction) then
  792. begin
  793. {
  794. This changes the very common
  795. mov r0, #0
  796. str r0, [...]
  797. mov r0, #0
  798. str r0, [...]
  799. and removes all superfluous mov instructions
  800. }
  801. if (taicpu(p).oper[1]^.typ = top_const) and
  802. (taicpu(hp1).opcode=A_STR) then
  803. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], []) and
  804. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  805. GetNextInstruction(hp1, hp2) and
  806. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  807. (taicpu(hp2).ops = 2) and
  808. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  809. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  810. begin
  811. DebugMsg('Peephole MovStrMov done', hp2);
  812. GetNextInstruction(hp2,hp1);
  813. asml.remove(hp2);
  814. hp2.free;
  815. if not assigned(hp1) then break;
  816. end
  817. {
  818. This removes the first mov from
  819. mov rX,...
  820. mov rX,...
  821. }
  822. else if taicpu(hp1).opcode=A_MOV then
  823. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  824. (taicpu(hp1).ops = 2) and
  825. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  826. { don't remove the first mov if the second is a mov rX,rX }
  827. not(MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)) do
  828. begin
  829. DebugMsg('Peephole MovMov done', p);
  830. asml.remove(p);
  831. p.free;
  832. p:=hp1;
  833. GetNextInstruction(hp1,hp1);
  834. if not assigned(hp1) then
  835. break;
  836. end;
  837. end;
  838. {
  839. change
  840. mov r1, r0
  841. add r1, r1, #1
  842. to
  843. add r1, r0, #1
  844. Todo: Make it work for mov+cmp too
  845. CAUTION! If this one is successful p might not be a mov instruction anymore!
  846. }
  847. if (taicpu(p).ops = 2) and
  848. (taicpu(p).oper[1]^.typ = top_reg) and
  849. (taicpu(p).oppostfix = PF_NONE) and
  850. GetNextInstruction(p, hp1) and
  851. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  852. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN],
  853. [taicpu(p).condition], []) and
  854. {MOV and MVN might only have 2 ops}
  855. (taicpu(hp1).ops >= 2) and
  856. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  857. (taicpu(hp1).oper[1]^.typ = top_reg) and
  858. (
  859. (taicpu(hp1).ops = 2) or
  860. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop])
  861. ) then
  862. begin
  863. { When we get here we still don't know if the registers match}
  864. for I:=1 to 2 do
  865. {
  866. If the first loop was successful p will be replaced with hp1.
  867. The checks will still be ok, because all required information
  868. will also be in hp1 then.
  869. }
  870. if (taicpu(hp1).ops > I) and
  871. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  872. begin
  873. DebugMsg('Peephole RedundantMovProcess done', hp1);
  874. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  875. if p<>hp1 then
  876. begin
  877. asml.remove(p);
  878. p.free;
  879. p:=hp1;
  880. end;
  881. end;
  882. end;
  883. { This folds shifterops into following instructions
  884. mov r0, r1, lsl #8
  885. add r2, r3, r0
  886. to
  887. add r2, r3, r1, lsl #8
  888. CAUTION! If this one is successful p might not be a mov instruction anymore!
  889. }
  890. if (taicpu(p).opcode = A_MOV) and
  891. (taicpu(p).ops = 3) and
  892. (taicpu(p).oper[1]^.typ = top_reg) and
  893. (taicpu(p).oper[2]^.typ = top_shifterop) and
  894. (taicpu(p).oppostfix = PF_NONE) and
  895. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  896. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  897. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  898. A_CMP, A_CMN],
  899. [taicpu(p).condition], [PF_None]) and
  900. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  901. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp1)) and
  902. (taicpu(hp1).ops >= 2) and
  903. {Currently we can't fold into another shifterop}
  904. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  905. {Folding rrx is problematic because of the C-Flag, as we currently can't check
  906. NR_DEFAULTFLAGS for modification}
  907. (
  908. {Everything is fine if we don't use RRX}
  909. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) or
  910. (
  911. {If it is RRX, then check if we're just accessing the next instruction}
  912. GetNextInstruction(p, hp2) and
  913. (hp1 = hp2)
  914. )
  915. ) and
  916. { reg1 might not be modified inbetween }
  917. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  918. { The shifterop can contain a register, might not be modified}
  919. (
  920. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) or
  921. not(RegModifiedBetween(taicpu(p).oper[2]^.shifterop^.rs, p, hp1))
  922. ) and
  923. (
  924. {Only ONE of the two src operands is allowed to match}
  925. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  926. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  927. ) then
  928. begin
  929. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  930. I2:=0
  931. else
  932. I2:=1;
  933. for I:=I2 to taicpu(hp1).ops-1 do
  934. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  935. begin
  936. { If the parameter matched on the second op from the RIGHT
  937. we have to switch the parameters, this will not happen for CMP
  938. were we're only evaluating the most right parameter
  939. }
  940. if I <> taicpu(hp1).ops-1 then
  941. begin
  942. {The SUB operators need to be changed when we swap parameters}
  943. case taicpu(hp1).opcode of
  944. A_SUB: tempop:=A_RSB;
  945. A_SBC: tempop:=A_RSC;
  946. A_RSB: tempop:=A_SUB;
  947. A_RSC: tempop:=A_SBC;
  948. else tempop:=taicpu(hp1).opcode;
  949. end;
  950. if taicpu(hp1).ops = 3 then
  951. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  952. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  953. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  954. else
  955. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  956. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  957. taicpu(p).oper[2]^.shifterop^);
  958. end
  959. else
  960. if taicpu(hp1).ops = 3 then
  961. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  962. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  963. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  964. else
  965. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  966. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  967. taicpu(p).oper[2]^.shifterop^);
  968. asml.insertbefore(hp2, hp1);
  969. asml.remove(p);
  970. asml.remove(hp1);
  971. p.free;
  972. hp1.free;
  973. p:=hp2;
  974. GetNextInstruction(p,hp1);
  975. DebugMsg('Peephole FoldShiftProcess done', p);
  976. break;
  977. end;
  978. end;
  979. {
  980. Often we see shifts and then a superfluous mov to another register
  981. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  982. }
  983. if (taicpu(p).opcode = A_MOV) and
  984. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  985. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  986. end;
  987. A_ADD,
  988. A_ADC,
  989. A_RSB,
  990. A_RSC,
  991. A_SUB,
  992. A_SBC,
  993. A_AND,
  994. A_BIC,
  995. A_EOR,
  996. A_ORR,
  997. A_MLA,
  998. A_MUL:
  999. begin
  1000. {
  1001. optimize
  1002. and reg2,reg1,const1
  1003. ...
  1004. }
  1005. if (taicpu(p).opcode = A_AND) and
  1006. (taicpu(p).oper[1]^.typ = top_reg) and
  1007. (taicpu(p).oper[2]^.typ = top_const) then
  1008. begin
  1009. {
  1010. change
  1011. and reg2,reg1,const1
  1012. and reg3,reg2,const2
  1013. to
  1014. and reg3,reg1,(const1 and const2)
  1015. }
  1016. if GetNextInstruction(p, hp1) and
  1017. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  1018. { either reg3 and reg2 are equal or reg2 is deallocated after the and }
  1019. (MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) or
  1020. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next)))) and
  1021. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1022. (taicpu(hp1).oper[2]^.typ = top_const) then
  1023. begin
  1024. DebugMsg('Peephole AndAnd2And done', p);
  1025. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1026. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  1027. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1028. asml.remove(hp1);
  1029. hp1.free;
  1030. end
  1031. {
  1032. change
  1033. and reg2,reg1,255
  1034. strb reg2,[...]
  1035. dealloc reg2
  1036. to
  1037. strb reg1,[...]
  1038. }
  1039. else if (taicpu(p).oper[2]^.val = 255) and
  1040. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1041. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1042. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1043. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1044. { the reference in strb might not use reg2 }
  1045. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1046. { reg1 might not be modified inbetween }
  1047. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1048. begin
  1049. DebugMsg('Peephole AndStrb2Strb done', p);
  1050. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1051. asml.remove(p);
  1052. p.free;
  1053. p:=hp1;
  1054. end;
  1055. end;
  1056. {
  1057. change
  1058. add/sub reg2,reg1,const1
  1059. str/ldr reg3,[reg2,const2]
  1060. dealloc reg2
  1061. to
  1062. str/ldr reg3,[reg1,const2+/-const1]
  1063. }
  1064. if (taicpu(p).opcode in [A_ADD,A_SUB]) and
  1065. (taicpu(p).oper[1]^.typ = top_reg) and
  1066. (taicpu(p).oper[2]^.typ = top_const) then
  1067. begin
  1068. hp1:=p;
  1069. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) and
  1070. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  1071. MatchInstruction(hp1, [A_LDR, A_STR], [C_None], []) and
  1072. (taicpu(hp1).oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  1073. { don't optimize if the register is stored/overwritten }
  1074. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[1]^.reg) and
  1075. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1076. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1077. { new offset must be valid: either in the range of 8 or 12 bit, depend on the
  1078. ldr postfix }
  1079. (((taicpu(p).opcode=A_ADD) and
  1080. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset+taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1081. ) or
  1082. ((taicpu(p).opcode=A_SUB) and
  1083. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset-taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1084. )
  1085. ) do
  1086. begin
  1087. { neither reg1 nor reg2 might be changed inbetween }
  1088. if RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1) or
  1089. RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1) then
  1090. break;
  1091. { reg2 must be either overwritten by the ldr or it is deallocated afterwards }
  1092. if ((taicpu(hp1).opcode=A_LDR) and (taicpu(p).oper[0]^.reg=taicpu(hp1).oper[0]^.reg)) or
  1093. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  1094. begin
  1095. { remember last instruction }
  1096. hp2:=hp1;
  1097. DebugMsg('Peephole Add/SubLdr2Ldr done', p);
  1098. hp1:=p;
  1099. { fix all ldr/str }
  1100. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) do
  1101. begin
  1102. taicpu(hp1).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  1103. if taicpu(p).opcode=A_ADD then
  1104. inc(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val)
  1105. else
  1106. dec(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val);
  1107. if hp1=hp2 then
  1108. break;
  1109. end;
  1110. GetNextInstruction(p,hp1);
  1111. asml.remove(p);
  1112. p.free;
  1113. p:=hp1;
  1114. break;
  1115. end;
  1116. end;
  1117. end;
  1118. {
  1119. change
  1120. add reg1, ...
  1121. mov reg2, reg1
  1122. to
  1123. add reg2, ...
  1124. }
  1125. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1126. begin
  1127. if (taicpu(p).ops=3) then
  1128. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  1129. end;
  1130. end;
  1131. {$ifdef dummy}
  1132. A_MVN:
  1133. begin
  1134. {
  1135. change
  1136. mvn reg2,reg1
  1137. and reg3,reg4,reg2
  1138. dealloc reg2
  1139. to
  1140. bic reg3,reg4,reg1
  1141. }
  1142. if (taicpu(p).oper[1]^.typ = top_reg) and
  1143. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1144. MatchInstruction(hp1,A_AND,[],[]) and
  1145. (((taicpu(hp1).ops=3) and
  1146. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1147. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  1148. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) or
  1149. ((taicpu(hp1).ops=2) and
  1150. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1151. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1152. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1153. { reg1 might not be modified inbetween }
  1154. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1155. begin
  1156. DebugMsg('Peephole MvnAnd2Bic done', p);
  1157. taicpu(hp1).opcode:=A_BIC;
  1158. if taicpu(hp1).ops=3 then
  1159. begin
  1160. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1161. taicpu(hp1).loadReg(1,taicpu(hp1).oper[2]^.reg); // Swap operands
  1162. taicpu(hp1).loadReg(2,taicpu(p).oper[1]^.reg);
  1163. end
  1164. else
  1165. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1166. asml.remove(p);
  1167. p.free;
  1168. p:=hp1;
  1169. end;
  1170. end;
  1171. {$endif dummy}
  1172. A_UXTB:
  1173. begin
  1174. {
  1175. change
  1176. uxtb reg2,reg1
  1177. strb reg2,[...]
  1178. dealloc reg2
  1179. to
  1180. strb reg1,[...]
  1181. }
  1182. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1183. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1184. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1185. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1186. { the reference in strb might not use reg2 }
  1187. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1188. { reg1 might not be modified inbetween }
  1189. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1190. begin
  1191. DebugMsg('Peephole UxtbStrb2Strb done', p);
  1192. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1193. asml.remove(p);
  1194. p.free;
  1195. p:=hp1;
  1196. end
  1197. {
  1198. change
  1199. uxtb reg2,reg1
  1200. uxth reg3,reg2
  1201. dealloc reg2
  1202. to
  1203. uxtb reg3,reg1
  1204. }
  1205. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1206. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1207. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1208. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1209. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1210. { reg1 might not be modified inbetween }
  1211. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1212. begin
  1213. DebugMsg('Peephole UxtbUxth2Uxtb done', p);
  1214. taicpu(hp1).opcode:=A_UXTB;
  1215. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1216. asml.remove(p);
  1217. p.free;
  1218. p:=hp1;
  1219. end
  1220. {
  1221. change
  1222. uxtb reg2,reg1
  1223. uxtb reg3,reg2
  1224. dealloc reg2
  1225. to
  1226. uxtb reg3,reg1
  1227. }
  1228. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1229. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1230. MatchInstruction(hp1, A_UXTB, [C_None], [PF_None]) and
  1231. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1232. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1233. { reg1 might not be modified inbetween }
  1234. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1235. begin
  1236. DebugMsg('Peephole UxtbUxtb2Uxtb done', p);
  1237. taicpu(hp1).opcode:=A_UXTB;
  1238. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1239. asml.remove(p);
  1240. p.free;
  1241. p:=hp1;
  1242. end
  1243. {
  1244. change
  1245. uxth reg2,reg1
  1246. uxth reg3,reg2
  1247. dealloc reg2
  1248. to
  1249. uxth reg3,reg1
  1250. }
  1251. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1252. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1253. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1254. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1255. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1256. { reg1 might not be modified inbetween }
  1257. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1258. begin
  1259. DebugMsg('Peephole UxthUxth2Uxth done', p);
  1260. taicpu(hp1).opcode:=A_UXTH;
  1261. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1262. asml.remove(p);
  1263. p.free;
  1264. p:=hp1;
  1265. end;
  1266. end;
  1267. A_UXTH:
  1268. begin
  1269. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1270. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1271. MatchInstruction(hp1, A_STR, [C_None], [PF_H]) and
  1272. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1273. { the reference in strb might not use reg2 }
  1274. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1275. { reg1 might not be modified inbetween }
  1276. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1277. begin
  1278. DebugMsg('Peephole UXTHStrh2Strh done', p);
  1279. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1280. asml.remove(p);
  1281. p.free;
  1282. p:=hp1;
  1283. end;
  1284. end;
  1285. A_CMP:
  1286. begin
  1287. {
  1288. change
  1289. cmp reg,const1
  1290. moveq reg,const1
  1291. movne reg,const2
  1292. to
  1293. cmp reg,const1
  1294. movne reg,const2
  1295. }
  1296. if (taicpu(p).oper[1]^.typ = top_const) and
  1297. GetNextInstruction(p, hp1) and
  1298. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1299. (taicpu(hp1).oper[1]^.typ = top_const) and
  1300. GetNextInstruction(hp1, hp2) and
  1301. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1302. (taicpu(hp1).oper[1]^.typ = top_const) then
  1303. begin
  1304. RemoveRedundantMove(p, hp1, asml);
  1305. RemoveRedundantMove(p, hp2, asml);
  1306. end;
  1307. end;
  1308. end;
  1309. end;
  1310. end;
  1311. end;
  1312. { instructions modifying the CPSR can be only the last instruction }
  1313. function MustBeLast(p : tai) : boolean;
  1314. begin
  1315. Result:=(p.typ=ait_instruction) and
  1316. ((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
  1317. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  1318. (taicpu(p).oppostfix=PF_S));
  1319. end;
  1320. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  1321. var
  1322. p,hp1,hp2: tai;
  1323. l : longint;
  1324. condition : tasmcond;
  1325. hp3: tai;
  1326. WasLast: boolean;
  1327. { UsedRegs, TmpUsedRegs: TRegSet; }
  1328. begin
  1329. p := BlockStart;
  1330. { UsedRegs := []; }
  1331. while (p <> BlockEnd) Do
  1332. begin
  1333. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  1334. case p.Typ Of
  1335. Ait_Instruction:
  1336. begin
  1337. case taicpu(p).opcode Of
  1338. A_B:
  1339. if taicpu(p).condition<>C_None then
  1340. begin
  1341. { check for
  1342. Bxx xxx
  1343. <several instructions>
  1344. xxx:
  1345. }
  1346. l:=0;
  1347. WasLast:=False;
  1348. GetNextInstruction(p, hp1);
  1349. while assigned(hp1) and
  1350. (l<=4) and
  1351. CanBeCond(hp1) and
  1352. { stop on labels }
  1353. not(hp1.typ=ait_label) do
  1354. begin
  1355. inc(l);
  1356. if MustBeLast(hp1) then
  1357. begin
  1358. WasLast:=True;
  1359. GetNextInstruction(hp1,hp1);
  1360. break;
  1361. end
  1362. else
  1363. GetNextInstruction(hp1,hp1);
  1364. end;
  1365. if assigned(hp1) then
  1366. begin
  1367. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1368. begin
  1369. if (l<=4) and (l>0) then
  1370. begin
  1371. condition:=inverse_cond(taicpu(p).condition);
  1372. hp2:=p;
  1373. GetNextInstruction(p,hp1);
  1374. p:=hp1;
  1375. repeat
  1376. if hp1.typ=ait_instruction then
  1377. taicpu(hp1).condition:=condition;
  1378. if MustBeLast(hp1) then
  1379. begin
  1380. GetNextInstruction(hp1,hp1);
  1381. break;
  1382. end
  1383. else
  1384. GetNextInstruction(hp1,hp1);
  1385. until not(assigned(hp1)) or
  1386. not(CanBeCond(hp1)) or
  1387. (hp1.typ=ait_label);
  1388. { wait with removing else GetNextInstruction could
  1389. ignore the label if it was the only usage in the
  1390. jump moved away }
  1391. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1392. asml.remove(hp2);
  1393. hp2.free;
  1394. continue;
  1395. end;
  1396. end
  1397. else
  1398. { do not perform further optimizations if there is inctructon
  1399. in block #1 which can not be optimized.
  1400. }
  1401. if not WasLast then
  1402. begin
  1403. { check further for
  1404. Bcc xxx
  1405. <several instructions 1>
  1406. B yyy
  1407. xxx:
  1408. <several instructions 2>
  1409. yyy:
  1410. }
  1411. { hp2 points to jmp yyy }
  1412. hp2:=hp1;
  1413. { skip hp1 to xxx }
  1414. GetNextInstruction(hp1, hp1);
  1415. if assigned(hp2) and
  1416. assigned(hp1) and
  1417. (l<=3) and
  1418. (hp2.typ=ait_instruction) and
  1419. (taicpu(hp2).is_jmp) and
  1420. (taicpu(hp2).condition=C_None) and
  1421. { real label and jump, no further references to the
  1422. label are allowed }
  1423. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  1424. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1425. begin
  1426. l:=0;
  1427. { skip hp1 to <several moves 2> }
  1428. GetNextInstruction(hp1, hp1);
  1429. while assigned(hp1) and
  1430. CanBeCond(hp1) do
  1431. begin
  1432. inc(l);
  1433. GetNextInstruction(hp1, hp1);
  1434. end;
  1435. { hp1 points to yyy: }
  1436. if assigned(hp1) and
  1437. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  1438. begin
  1439. condition:=inverse_cond(taicpu(p).condition);
  1440. GetNextInstruction(p,hp1);
  1441. hp3:=p;
  1442. p:=hp1;
  1443. repeat
  1444. if hp1.typ=ait_instruction then
  1445. taicpu(hp1).condition:=condition;
  1446. GetNextInstruction(hp1,hp1);
  1447. until not(assigned(hp1)) or
  1448. not(CanBeCond(hp1));
  1449. { hp2 is still at jmp yyy }
  1450. GetNextInstruction(hp2,hp1);
  1451. { hp2 is now at xxx: }
  1452. condition:=inverse_cond(condition);
  1453. GetNextInstruction(hp1,hp1);
  1454. { hp1 is now at <several movs 2> }
  1455. repeat
  1456. taicpu(hp1).condition:=condition;
  1457. GetNextInstruction(hp1,hp1);
  1458. until not(assigned(hp1)) or
  1459. not(CanBeCond(hp1)) or
  1460. (hp1.typ=ait_label);
  1461. {
  1462. asml.remove(hp1.next)
  1463. hp1.next.free;
  1464. asml.remove(hp1);
  1465. hp1.free;
  1466. }
  1467. { remove Bcc }
  1468. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  1469. asml.remove(hp3);
  1470. hp3.free;
  1471. { remove jmp }
  1472. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1473. asml.remove(hp2);
  1474. hp2.free;
  1475. continue;
  1476. end;
  1477. end;
  1478. end;
  1479. end;
  1480. end;
  1481. end;
  1482. end;
  1483. end;
  1484. p := tai(p.next)
  1485. end;
  1486. end;
  1487. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  1488. begin
  1489. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  1490. Result:=true
  1491. else
  1492. Result:=inherited RegInInstruction(Reg, p1);
  1493. end;
  1494. const
  1495. { set of opcode which might or do write to memory }
  1496. { TODO : extend armins.dat to contain r/w info }
  1497. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  1498. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  1499. { adjust the register live information when swapping the two instructions p and hp1,
  1500. they must follow one after the other }
  1501. procedure TCpuPreRegallocScheduler.SwapRegLive(p,hp1 : taicpu);
  1502. procedure CheckLiveEnd(reg : tregister);
  1503. var
  1504. supreg : TSuperRegister;
  1505. regtype : TRegisterType;
  1506. begin
  1507. if reg=NR_NO then
  1508. exit;
  1509. regtype:=getregtype(reg);
  1510. supreg:=getsupreg(reg);
  1511. if (cg.rg[regtype].live_end[supreg]=hp1) and
  1512. RegInInstruction(reg,p) then
  1513. cg.rg[regtype].live_end[supreg]:=p;
  1514. end;
  1515. procedure CheckLiveStart(reg : TRegister);
  1516. var
  1517. supreg : TSuperRegister;
  1518. regtype : TRegisterType;
  1519. begin
  1520. if reg=NR_NO then
  1521. exit;
  1522. regtype:=getregtype(reg);
  1523. supreg:=getsupreg(reg);
  1524. if (cg.rg[regtype].live_start[supreg]=p) and
  1525. RegInInstruction(reg,hp1) then
  1526. cg.rg[regtype].live_start[supreg]:=hp1;
  1527. end;
  1528. var
  1529. i : longint;
  1530. r : TSuperRegister;
  1531. begin
  1532. { assumption: p is directly followed by hp1 }
  1533. { if live of any reg used by p starts at p and hp1 uses this register then
  1534. set live start to hp1 }
  1535. for i:=0 to p.ops-1 do
  1536. case p.oper[i]^.typ of
  1537. Top_Reg:
  1538. CheckLiveStart(p.oper[i]^.reg);
  1539. Top_Ref:
  1540. begin
  1541. CheckLiveStart(p.oper[i]^.ref^.base);
  1542. CheckLiveStart(p.oper[i]^.ref^.index);
  1543. end;
  1544. Top_Shifterop:
  1545. CheckLiveStart(p.oper[i]^.shifterop^.rs);
  1546. Top_RegSet:
  1547. for r:=RS_R0 to RS_R15 do
  1548. if r in p.oper[i]^.regset^ then
  1549. CheckLiveStart(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1550. end;
  1551. { if live of any reg used by hp1 ends at hp1 and p uses this register then
  1552. set live end to p }
  1553. for i:=0 to hp1.ops-1 do
  1554. case hp1.oper[i]^.typ of
  1555. Top_Reg:
  1556. CheckLiveEnd(hp1.oper[i]^.reg);
  1557. Top_Ref:
  1558. begin
  1559. CheckLiveEnd(hp1.oper[i]^.ref^.base);
  1560. CheckLiveEnd(hp1.oper[i]^.ref^.index);
  1561. end;
  1562. Top_Shifterop:
  1563. CheckLiveStart(hp1.oper[i]^.shifterop^.rs);
  1564. Top_RegSet:
  1565. for r:=RS_R0 to RS_R15 do
  1566. if r in hp1.oper[i]^.regset^ then
  1567. CheckLiveEnd(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1568. end;
  1569. end;
  1570. function TCpuPreRegallocScheduler.SchedulerPass1Cpu(var p: tai): boolean;
  1571. { TODO : schedule also forward }
  1572. { TODO : schedule distance > 1 }
  1573. var
  1574. hp1,hp2,hp3,hp4,hp5 : tai;
  1575. list : TAsmList;
  1576. begin
  1577. result:=true;
  1578. list:=TAsmList.Create;
  1579. p:=BlockStart;
  1580. while p<>BlockEnd Do
  1581. begin
  1582. if (p.typ=ait_instruction) and
  1583. GetNextInstruction(p,hp1) and
  1584. (hp1.typ=ait_instruction) and
  1585. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  1586. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  1587. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  1588. not(RegModifiedByInstruction(NR_PC,p))
  1589. ) or
  1590. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  1591. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  1592. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  1593. (taicpu(hp1).oper[1]^.ref^.offset=0)
  1594. )
  1595. ) or
  1596. { try to prove that the memory accesses don't overlapp }
  1597. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  1598. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  1599. (taicpu(p).oppostfix=PF_None) and
  1600. (taicpu(hp1).oppostfix=PF_None) and
  1601. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  1602. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1603. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  1604. (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)]))
  1605. )
  1606. )
  1607. ) and
  1608. GetNextInstruction(hp1,hp2) and
  1609. (hp2.typ=ait_instruction) and
  1610. { loaded register used by next instruction? }
  1611. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  1612. { loaded register not used by previous instruction? }
  1613. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  1614. { same condition? }
  1615. (taicpu(p).condition=taicpu(hp1).condition) and
  1616. { first instruction might not change the register used as base }
  1617. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  1618. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  1619. ) and
  1620. { first instruction might not change the register used as index }
  1621. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  1622. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  1623. ) then
  1624. begin
  1625. hp3:=tai(p.Previous);
  1626. hp5:=tai(p.next);
  1627. asml.Remove(p);
  1628. { if there is a reg. dealloc instruction associated with p, move it together with p }
  1629. { before the instruction? }
  1630. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  1631. begin
  1632. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  1633. RegInInstruction(tai_regalloc(hp3).reg,p) then
  1634. begin
  1635. hp4:=hp3;
  1636. hp3:=tai(hp3.Previous);
  1637. asml.Remove(hp4);
  1638. list.Concat(hp4);
  1639. end
  1640. else
  1641. hp3:=tai(hp3.Previous);
  1642. end;
  1643. list.Concat(p);
  1644. SwapRegLive(taicpu(p),taicpu(hp1));
  1645. { after the instruction? }
  1646. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  1647. begin
  1648. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  1649. RegInInstruction(tai_regalloc(hp5).reg,p) then
  1650. begin
  1651. hp4:=hp5;
  1652. hp5:=tai(hp5.next);
  1653. asml.Remove(hp4);
  1654. list.Concat(hp4);
  1655. end
  1656. else
  1657. hp5:=tai(hp5.Next);
  1658. end;
  1659. asml.Remove(hp1);
  1660. {$ifdef DEBUG_PREREGSCHEDULER}
  1661. asml.insertbefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  1662. {$endif DEBUG_PREREGSCHEDULER}
  1663. asml.InsertBefore(hp1,hp2);
  1664. asml.InsertListBefore(hp2,list);
  1665. p:=tai(p.next)
  1666. end
  1667. else if p.typ=ait_instruction then
  1668. p:=hp1
  1669. else
  1670. p:=tai(p.next);
  1671. end;
  1672. list.Free;
  1673. end;
  1674. procedure DecrementPreceedingIT(list: TAsmList; p: tai);
  1675. var
  1676. hp : tai;
  1677. l : longint;
  1678. begin
  1679. hp := tai(p.Previous);
  1680. l := 1;
  1681. while assigned(hp) and
  1682. (l <= 4) do
  1683. begin
  1684. if hp.typ=ait_instruction then
  1685. begin
  1686. if (taicpu(hp).opcode>=A_IT) and
  1687. (taicpu(hp).opcode <= A_ITTTT) then
  1688. begin
  1689. if (taicpu(hp).opcode = A_IT) and
  1690. (l=1) then
  1691. list.Remove(hp)
  1692. else
  1693. case taicpu(hp).opcode of
  1694. A_ITE:
  1695. if l=2 then taicpu(hp).opcode := A_IT;
  1696. A_ITT:
  1697. if l=2 then taicpu(hp).opcode := A_IT;
  1698. A_ITEE:
  1699. if l=3 then taicpu(hp).opcode := A_ITE;
  1700. A_ITTE:
  1701. if l=3 then taicpu(hp).opcode := A_ITT;
  1702. A_ITET:
  1703. if l=3 then taicpu(hp).opcode := A_ITE;
  1704. A_ITTT:
  1705. if l=3 then taicpu(hp).opcode := A_ITT;
  1706. A_ITEEE:
  1707. if l=4 then taicpu(hp).opcode := A_ITEE;
  1708. A_ITTEE:
  1709. if l=4 then taicpu(hp).opcode := A_ITTE;
  1710. A_ITETE:
  1711. if l=4 then taicpu(hp).opcode := A_ITET;
  1712. A_ITTTE:
  1713. if l=4 then taicpu(hp).opcode := A_ITTT;
  1714. A_ITEET:
  1715. if l=4 then taicpu(hp).opcode := A_ITEE;
  1716. A_ITTET:
  1717. if l=4 then taicpu(hp).opcode := A_ITTE;
  1718. A_ITETT:
  1719. if l=4 then taicpu(hp).opcode := A_ITET;
  1720. A_ITTTT:
  1721. if l=4 then taicpu(hp).opcode := A_ITTT;
  1722. end;
  1723. break;
  1724. end;
  1725. {else if (taicpu(hp).condition<>taicpu(p).condition) or
  1726. (taicpu(hp).condition<>inverse_cond(taicpu(p).condition)) then
  1727. break;}
  1728. inc(l);
  1729. end;
  1730. hp := tai(hp.Previous);
  1731. end;
  1732. end;
  1733. function TCpuThumb2AsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  1734. var
  1735. hp : taicpu;
  1736. hp1,hp2 : tai;
  1737. begin
  1738. if (p.typ=ait_instruction) and
  1739. MatchInstruction(p, A_STM, [C_None], [PF_FD,PF_DB]) and
  1740. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1741. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1742. ((taicpu(p).oper[1]^.regset^*[8..13,15])=[]) then
  1743. begin
  1744. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  1745. AsmL.InsertAfter(hp, p);
  1746. asml.Remove(p);
  1747. p:=hp;
  1748. result:=true;
  1749. end
  1750. else if (p.typ=ait_instruction) and
  1751. MatchInstruction(p, A_STR, [C_None], [PF_None]) and
  1752. (taicpu(p).oper[1]^.ref^.addressmode=AM_PREINDEXED) and
  1753. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  1754. (taicpu(p).oper[1]^.ref^.offset=-4) and
  1755. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,14]) then
  1756. begin
  1757. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  1758. asml.InsertAfter(hp, p);
  1759. asml.Remove(p);
  1760. p.Free;
  1761. p:=hp;
  1762. result:=true;
  1763. end
  1764. else if (p.typ=ait_instruction) and
  1765. MatchInstruction(p, A_LDM, [C_None], [PF_FD,PF_IA]) and
  1766. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1767. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1768. ((taicpu(p).oper[1]^.regset^*[8..14])=[]) then
  1769. begin
  1770. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  1771. asml.InsertBefore(hp, p);
  1772. asml.Remove(p);
  1773. p.Free;
  1774. p:=hp;
  1775. result:=true;
  1776. end
  1777. else if (p.typ=ait_instruction) and
  1778. MatchInstruction(p, A_LDR, [C_None], [PF_None]) and
  1779. (taicpu(p).oper[1]^.ref^.addressmode=AM_POSTINDEXED) and
  1780. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  1781. (taicpu(p).oper[1]^.ref^.offset=4) and
  1782. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,15]) then
  1783. begin
  1784. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  1785. asml.InsertBefore(hp, p);
  1786. asml.Remove(p);
  1787. p.Free;
  1788. p:=hp;
  1789. result:=true;
  1790. end
  1791. else if (p.typ=ait_instruction) and
  1792. MatchInstruction(p, A_MOV, [C_None], [PF_None]) and
  1793. (taicpu(p).oper[1]^.typ=top_const) and
  1794. (taicpu(p).oper[1]^.val >= 0) and
  1795. (taicpu(p).oper[1]^.val < 256) and
  1796. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  1797. begin
  1798. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  1799. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  1800. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  1801. taicpu(p).oppostfix:=PF_S;
  1802. result:=true;
  1803. end
  1804. else if (p.typ=ait_instruction) and
  1805. MatchInstruction(p, A_MVN, [C_None], [PF_None]) and
  1806. (taicpu(p).oper[1]^.typ=top_reg) and
  1807. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  1808. begin
  1809. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  1810. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  1811. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  1812. taicpu(p).oppostfix:=PF_S;
  1813. result:=true;
  1814. end
  1815. else if (p.typ=ait_instruction) and
  1816. MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  1817. (taicpu(p).ops = 3) and
  1818. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  1819. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  1820. (taicpu(p).oper[2]^.typ=top_const) and
  1821. (taicpu(p).oper[2]^.val >= 0) and
  1822. (taicpu(p).oper[2]^.val < 256) and
  1823. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  1824. begin
  1825. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  1826. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  1827. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  1828. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  1829. taicpu(p).oppostfix:=PF_S;
  1830. taicpu(p).ops := 2;
  1831. result:=true;
  1832. end
  1833. else if (p.typ=ait_instruction) and
  1834. MatchInstruction(p, [A_ADD], [C_None], [PF_None]) and
  1835. (taicpu(p).ops = 3) and
  1836. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  1837. (taicpu(p).oper[2]^.typ=top_reg) then
  1838. begin
  1839. taicpu(p).ops := 2;
  1840. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  1841. result:=true;
  1842. end
  1843. else if (p.typ=ait_instruction) and
  1844. MatchInstruction(p, [A_AND,A_ORR,A_EOR,A_BIC,A_LSL,A_LSR,A_ASR,A_ROR], [C_None], [PF_None]) and
  1845. (taicpu(p).ops = 3) and
  1846. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  1847. (taicpu(p).oper[2]^.typ=top_reg) and
  1848. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  1849. begin
  1850. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  1851. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  1852. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  1853. taicpu(p).ops := 2;
  1854. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  1855. taicpu(p).oppostfix:=PF_S;
  1856. result:=true;
  1857. end
  1858. else if (p.typ=ait_instruction) and
  1859. MatchInstruction(p, [A_AND,A_ORR,A_EOR], [C_None], [PF_None,PF_S]) and
  1860. (taicpu(p).ops = 3) and
  1861. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[2]^) and
  1862. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  1863. begin
  1864. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  1865. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  1866. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  1867. taicpu(p).oppostfix:=PF_S;
  1868. taicpu(p).ops := 2;
  1869. result:=true;
  1870. end
  1871. else if (p.typ=ait_instruction) and
  1872. MatchInstruction(p, [A_MOV], [C_None], [PF_None]) and
  1873. (taicpu(p).ops=3) and
  1874. (taicpu(p).oper[2]^.typ=top_shifterop) and
  1875. (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_LSL,SM_LSR,SM_ASR,SM_ROR]) and
  1876. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  1877. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  1878. begin
  1879. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  1880. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  1881. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  1882. taicpu(p).oppostfix:=PF_S;
  1883. taicpu(p).ops := 2;
  1884. if taicpu(p).oper[2]^.shifterop^.rs<>NR_NO then
  1885. taicpu(p).loadreg(1, taicpu(p).oper[2]^.shifterop^.rs)
  1886. else
  1887. taicpu(p).loadconst(1, taicpu(p).oper[2]^.shifterop^.shiftimm);
  1888. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  1889. SM_LSL: taicpu(p).opcode:=A_LSL;
  1890. SM_LSR: taicpu(p).opcode:=A_LSR;
  1891. SM_ASR: taicpu(p).opcode:=A_ASR;
  1892. SM_ROR: taicpu(p).opcode:=A_ROR;
  1893. end;
  1894. result:=true;
  1895. end
  1896. else if (p.typ=ait_instruction) and
  1897. MatchInstruction(p, [A_AND], [], [PF_None]) and
  1898. (taicpu(p).ops = 2) and
  1899. (taicpu(p).oper[1]^.typ=top_const) and
  1900. ((taicpu(p).oper[1]^.val=255) or
  1901. (taicpu(p).oper[1]^.val=65535)) then
  1902. begin
  1903. if taicpu(p).oper[1]^.val=255 then
  1904. taicpu(p).opcode:=A_UXTB
  1905. else
  1906. taicpu(p).opcode:=A_UXTH;
  1907. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  1908. result := true;
  1909. end
  1910. else if (p.typ=ait_instruction) and
  1911. MatchInstruction(p, [A_AND], [], [PF_None]) and
  1912. (taicpu(p).ops = 3) and
  1913. (taicpu(p).oper[2]^.typ=top_const) and
  1914. ((taicpu(p).oper[2]^.val=255) or
  1915. (taicpu(p).oper[2]^.val=65535)) then
  1916. begin
  1917. if taicpu(p).oper[2]^.val=255 then
  1918. taicpu(p).opcode:=A_UXTB
  1919. else
  1920. taicpu(p).opcode:=A_UXTH;
  1921. taicpu(p).ops:=2;
  1922. result := true;
  1923. end
  1924. {
  1925. Turn
  1926. mul reg0, z,w
  1927. sub/add x, y, reg0
  1928. dealloc reg0
  1929. into
  1930. mls/mla x,y,z,w
  1931. }
  1932. {
  1933. According to Jeppe Johansen this currently uses operands in the wrong order.
  1934. else if (p.typ=ait_instruction) and
  1935. MatchInstruction(p, [A_MUL], [C_None], [PF_None]) and
  1936. (taicpu(p).ops=3) and
  1937. (taicpu(p).oper[0]^.typ = top_reg) and
  1938. (taicpu(p).oper[1]^.typ = top_reg) and
  1939. (taicpu(p).oper[2]^.typ = top_reg) and
  1940. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1941. MatchInstruction(hp1,[A_ADD,A_SUB],[C_None],[PF_None]) and
  1942. (((taicpu(hp1).ops=3) and
  1943. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1944. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  1945. (MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1946. (taicpu(hp1).opcode=A_ADD)))) or
  1947. ((taicpu(hp1).ops=2) and
  1948. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1949. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1950. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1951. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1952. not(RegModifiedBetween(taicpu(p).oper[2]^.reg,p,hp1)) then
  1953. begin
  1954. if taicpu(hp1).opcode=A_ADD then
  1955. begin
  1956. taicpu(hp1).opcode:=A_MLA;
  1957. if taicpu(hp1).ops=3 then
  1958. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^) then
  1959. taicpu(hp1).loadreg(1,taicpu(hp1).oper[2]^.reg);
  1960. taicpu(hp1).loadreg(2,taicpu(p).oper[1]^.reg);
  1961. taicpu(hp1).loadreg(3,taicpu(p).oper[2]^.reg);
  1962. DebugMsg('MulAdd2MLA done', p);
  1963. taicpu(hp1).ops:=4;
  1964. asml.remove(p);
  1965. p.free;
  1966. p:=hp1;
  1967. end
  1968. else
  1969. begin
  1970. taicpu(hp1).opcode:=A_MLS;
  1971. if taicpu(hp1).ops=2 then
  1972. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  1973. taicpu(hp1).loadreg(2,taicpu(p).oper[1]^.reg);
  1974. taicpu(hp1).loadreg(3,taicpu(p).oper[2]^.reg);
  1975. DebugMsg('MulSub2MLS done', p);
  1976. taicpu(hp1).ops:=4;
  1977. asml.remove(p);
  1978. p.free;
  1979. p:=hp1;
  1980. end;
  1981. result:=true;
  1982. end
  1983. }
  1984. {else if (p.typ=ait_instruction) and
  1985. MatchInstruction(p, [A_CMP], [C_None], [PF_None]) and
  1986. (taicpu(p).oper[1]^.typ=top_const) and
  1987. (taicpu(p).oper[1]^.val=0) and
  1988. GetNextInstruction(p,hp1) and
  1989. (taicpu(hp1).opcode=A_B) and
  1990. (taicpu(hp1).condition in [C_EQ,C_NE]) then
  1991. begin
  1992. if taicpu(hp1).condition = C_EQ then
  1993. hp2:=taicpu.op_reg_ref(A_CBZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^)
  1994. else
  1995. hp2:=taicpu.op_reg_ref(A_CBNZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^);
  1996. taicpu(hp2).is_jmp := true;
  1997. asml.InsertAfter(hp2, hp1);
  1998. asml.Remove(hp1);
  1999. hp1.Free;
  2000. asml.Remove(p);
  2001. p.Free;
  2002. p := hp2;
  2003. result := true;
  2004. end}
  2005. else
  2006. Result := inherited PeepHoleOptPass1Cpu(p);
  2007. end;
  2008. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  2009. var
  2010. p,hp1,hp2: tai;
  2011. l,l2 : longint;
  2012. condition : tasmcond;
  2013. hp3: tai;
  2014. WasLast: boolean;
  2015. { UsedRegs, TmpUsedRegs: TRegSet; }
  2016. begin
  2017. p := BlockStart;
  2018. { UsedRegs := []; }
  2019. while (p <> BlockEnd) Do
  2020. begin
  2021. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  2022. case p.Typ Of
  2023. Ait_Instruction:
  2024. begin
  2025. case taicpu(p).opcode Of
  2026. A_B:
  2027. if taicpu(p).condition<>C_None then
  2028. begin
  2029. { check for
  2030. Bxx xxx
  2031. <several instructions>
  2032. xxx:
  2033. }
  2034. l:=0;
  2035. GetNextInstruction(p, hp1);
  2036. while assigned(hp1) and
  2037. (l<=4) and
  2038. CanBeCond(hp1) and
  2039. { stop on labels }
  2040. not(hp1.typ=ait_label) do
  2041. begin
  2042. inc(l);
  2043. if MustBeLast(hp1) then
  2044. begin
  2045. //hp1:=nil;
  2046. GetNextInstruction(hp1,hp1);
  2047. break;
  2048. end
  2049. else
  2050. GetNextInstruction(hp1,hp1);
  2051. end;
  2052. if assigned(hp1) then
  2053. begin
  2054. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2055. begin
  2056. if (l<=4) and (l>0) then
  2057. begin
  2058. condition:=inverse_cond(taicpu(p).condition);
  2059. hp2:=p;
  2060. GetNextInstruction(p,hp1);
  2061. p:=hp1;
  2062. repeat
  2063. if hp1.typ=ait_instruction then
  2064. taicpu(hp1).condition:=condition;
  2065. if MustBeLast(hp1) then
  2066. begin
  2067. GetNextInstruction(hp1,hp1);
  2068. break;
  2069. end
  2070. else
  2071. GetNextInstruction(hp1,hp1);
  2072. until not(assigned(hp1)) or
  2073. not(CanBeCond(hp1)) or
  2074. (hp1.typ=ait_label);
  2075. { wait with removing else GetNextInstruction could
  2076. ignore the label if it was the only usage in the
  2077. jump moved away }
  2078. asml.InsertAfter(tai_comment.create(strpnew('Collapsed')), hp2);
  2079. DecrementPreceedingIT(asml, hp2);
  2080. case l of
  2081. 1: asml.InsertAfter(taicpu.op_cond(A_IT,condition), hp2);
  2082. 2: asml.InsertAfter(taicpu.op_cond(A_ITT,condition), hp2);
  2083. 3: asml.InsertAfter(taicpu.op_cond(A_ITTT,condition), hp2);
  2084. 4: asml.InsertAfter(taicpu.op_cond(A_ITTTT,condition), hp2);
  2085. end;
  2086. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2087. asml.remove(hp2);
  2088. hp2.free;
  2089. continue;
  2090. end;
  2091. end;
  2092. end;
  2093. end;
  2094. end;
  2095. end;
  2096. end;
  2097. p := tai(p.next)
  2098. end;
  2099. end;
  2100. begin
  2101. casmoptimizer:=TCpuAsmOptimizer;
  2102. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  2103. End.