aoptcpu.pas 131 KB

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