aoptcpu.pas 104 KB

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