aoptcpu.pas 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906
  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. GetNextInstruction(p, hp1);
  1106. asml.remove(p);
  1107. p.free;
  1108. p:=hp1;
  1109. result:=true;
  1110. end;
  1111. { This folds shifterops into following instructions
  1112. mov r0, r1, lsl #8
  1113. add r2, r3, r0
  1114. to
  1115. add r2, r3, r1, lsl #8
  1116. CAUTION! If this one is successful p might not be a mov instruction anymore!
  1117. }
  1118. if (taicpu(p).opcode = A_MOV) and
  1119. (taicpu(p).ops = 3) and
  1120. (taicpu(p).oper[1]^.typ = top_reg) and
  1121. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1122. (taicpu(p).oppostfix = PF_NONE) and
  1123. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1124. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  1125. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  1126. A_CMP, A_CMN],
  1127. [taicpu(p).condition], [PF_None]) and
  1128. (not ((GenerateThumb2Code) and
  1129. (taicpu(hp1).opcode in [A_SBC]) and
  1130. (((taicpu(hp1).ops=3) and
  1131. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^.reg)) or
  1132. ((taicpu(hp1).ops=2) and
  1133. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg))))) and
  1134. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) and
  1135. (taicpu(hp1).ops >= 2) and
  1136. {Currently we can't fold into another shifterop}
  1137. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  1138. {Folding rrx is problematic because of the C-Flag, as we currently can't check
  1139. NR_DEFAULTFLAGS for modification}
  1140. (
  1141. {Everything is fine if we don't use RRX}
  1142. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) or
  1143. (
  1144. {If it is RRX, then check if we're just accessing the next instruction}
  1145. GetNextInstruction(p, hp2) and
  1146. (hp1 = hp2)
  1147. )
  1148. ) and
  1149. { reg1 might not be modified inbetween }
  1150. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1151. { The shifterop can contain a register, might not be modified}
  1152. (
  1153. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) or
  1154. not(RegModifiedBetween(taicpu(p).oper[2]^.shifterop^.rs, p, hp1))
  1155. ) and
  1156. (
  1157. {Only ONE of the two src operands is allowed to match}
  1158. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  1159. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  1160. ) then
  1161. begin
  1162. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  1163. I2:=0
  1164. else
  1165. I2:=1;
  1166. for I:=I2 to taicpu(hp1).ops-1 do
  1167. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  1168. begin
  1169. { If the parameter matched on the second op from the RIGHT
  1170. we have to switch the parameters, this will not happen for CMP
  1171. were we're only evaluating the most right parameter
  1172. }
  1173. if I <> taicpu(hp1).ops-1 then
  1174. begin
  1175. {The SUB operators need to be changed when we swap parameters}
  1176. case taicpu(hp1).opcode of
  1177. A_SUB: tempop:=A_RSB;
  1178. A_SBC: tempop:=A_RSC;
  1179. A_RSB: tempop:=A_SUB;
  1180. A_RSC: tempop:=A_SBC;
  1181. else tempop:=taicpu(hp1).opcode;
  1182. end;
  1183. if taicpu(hp1).ops = 3 then
  1184. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  1185. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  1186. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1187. else
  1188. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  1189. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1190. taicpu(p).oper[2]^.shifterop^);
  1191. end
  1192. else
  1193. if taicpu(hp1).ops = 3 then
  1194. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  1195. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  1196. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1197. else
  1198. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  1199. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1200. taicpu(p).oper[2]^.shifterop^);
  1201. asml.insertbefore(hp2, hp1);
  1202. GetNextInstruction(p, hp2);
  1203. asml.remove(p);
  1204. asml.remove(hp1);
  1205. p.free;
  1206. hp1.free;
  1207. p:=hp2;
  1208. DebugMsg('Peephole FoldShiftProcess done', p);
  1209. break;
  1210. end;
  1211. end;
  1212. {
  1213. Fold
  1214. mov r1, r1, lsl #2
  1215. ldr/ldrb r0, [r0, r1]
  1216. to
  1217. ldr/ldrb r0, [r0, r1, lsl #2]
  1218. XXX: This still needs some work, as we quite often encounter something like
  1219. mov r1, r2, lsl #2
  1220. add r2, r3, #imm
  1221. ldr r0, [r2, r1]
  1222. which can't be folded because r2 is overwritten between the shift and the ldr.
  1223. We could try to shuffle the registers around and fold it into.
  1224. add r1, r3, #imm
  1225. ldr r0, [r1, r2, lsl #2]
  1226. }
  1227. if (not(GenerateThumbCode)) and
  1228. (taicpu(p).opcode = A_MOV) and
  1229. (taicpu(p).ops = 3) and
  1230. (taicpu(p).oper[1]^.typ = top_reg) and
  1231. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1232. { RRX is tough to handle, because it requires tracking the C-Flag,
  1233. it is also extremly unlikely to be emitted this way}
  1234. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) and
  1235. (taicpu(p).oper[2]^.shifterop^.shiftimm <> 0) and
  1236. { thumb2 allows only lsl #0..#3 }
  1237. (not(GenerateThumb2Code) or
  1238. ((taicpu(p).oper[2]^.shifterop^.shiftimm in [0..3]) and
  1239. (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL)
  1240. )
  1241. ) and
  1242. (taicpu(p).oppostfix = PF_NONE) and
  1243. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1244. {Only LDR, LDRB, STR, STRB can handle scaled register indexing}
  1245. MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition],
  1246. [PF_None, PF_B]) and
  1247. (
  1248. {If this is address by offset, one of the two registers can be used}
  1249. ((taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1250. (
  1251. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) xor
  1252. (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg)
  1253. )
  1254. ) or
  1255. {For post and preindexed only the index register can be used}
  1256. ((taicpu(hp1).oper[1]^.ref^.addressmode in [AM_POSTINDEXED, AM_PREINDEXED]) and
  1257. (
  1258. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) and
  1259. (taicpu(hp1).oper[1]^.ref^.base <> taicpu(p).oper[0]^.reg)
  1260. )
  1261. )
  1262. ) and
  1263. { Only fold if there isn't another shifterop already. }
  1264. (taicpu(hp1).oper[1]^.ref^.shiftmode = SM_None) and
  1265. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1266. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  1267. begin
  1268. { If the register we want to do the shift for resides in base, we need to swap that}
  1269. if (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg) then
  1270. taicpu(hp1).oper[1]^.ref^.base := taicpu(hp1).oper[1]^.ref^.index;
  1271. taicpu(hp1).oper[1]^.ref^.index := taicpu(p).oper[1]^.reg;
  1272. taicpu(hp1).oper[1]^.ref^.shiftmode := taicpu(p).oper[2]^.shifterop^.shiftmode;
  1273. taicpu(hp1).oper[1]^.ref^.shiftimm := taicpu(p).oper[2]^.shifterop^.shiftimm;
  1274. DebugMsg('Peephole FoldShiftLdrStr done', hp1);
  1275. GetNextInstruction(p, hp1);
  1276. asml.remove(p);
  1277. p.free;
  1278. p:=hp1;
  1279. end;
  1280. {
  1281. Often we see shifts and then a superfluous mov to another register
  1282. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  1283. }
  1284. if (taicpu(p).opcode = A_MOV) and
  1285. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1286. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  1287. end;
  1288. A_ADD,
  1289. A_ADC,
  1290. A_RSB,
  1291. A_RSC,
  1292. A_SUB,
  1293. A_SBC,
  1294. A_AND,
  1295. A_BIC,
  1296. A_EOR,
  1297. A_ORR,
  1298. A_MLA,
  1299. A_MUL:
  1300. begin
  1301. {
  1302. optimize
  1303. and reg2,reg1,const1
  1304. ...
  1305. }
  1306. if (taicpu(p).opcode = A_AND) and
  1307. (taicpu(p).ops>2) and
  1308. (taicpu(p).oper[1]^.typ = top_reg) and
  1309. (taicpu(p).oper[2]^.typ = top_const) then
  1310. begin
  1311. {
  1312. change
  1313. and reg2,reg1,const1
  1314. ...
  1315. and reg3,reg2,const2
  1316. to
  1317. and reg3,reg1,(const1 and const2)
  1318. }
  1319. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1320. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  1321. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1322. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1323. (taicpu(hp1).oper[2]^.typ = top_const) then
  1324. begin
  1325. if not(RegUsedBetween(taicpu(hp1).oper[0]^.reg,p,hp1)) then
  1326. begin
  1327. DebugMsg('Peephole AndAnd2And done', p);
  1328. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1329. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  1330. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1331. asml.remove(hp1);
  1332. hp1.free;
  1333. Result:=true;
  1334. end
  1335. else if not(RegUsedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1336. begin
  1337. DebugMsg('Peephole AndAnd2And done', hp1);
  1338. taicpu(hp1).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1339. taicpu(hp1).oppostfix:=taicpu(p).oppostfix;
  1340. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1341. GetNextInstruction(p, hp1);
  1342. asml.remove(p);
  1343. p.free;
  1344. p:=hp1;
  1345. Result:=true;
  1346. end;
  1347. end
  1348. {
  1349. change
  1350. and reg2,reg1,$xxxxxxFF
  1351. strb reg2,[...]
  1352. dealloc reg2
  1353. to
  1354. strb reg1,[...]
  1355. }
  1356. else if ((taicpu(p).oper[2]^.val and $FF) = $FF) and
  1357. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1358. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1359. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1360. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1361. { the reference in strb might not use reg2 }
  1362. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1363. { reg1 might not be modified inbetween }
  1364. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1365. begin
  1366. DebugMsg('Peephole AndStrb2Strb done', p);
  1367. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1368. GetNextInstruction(p, hp1);
  1369. asml.remove(p);
  1370. p.free;
  1371. p:=hp1;
  1372. result:=true;
  1373. end
  1374. {
  1375. change
  1376. and reg2,reg1,255
  1377. uxtb/uxth reg3,reg2
  1378. dealloc reg2
  1379. to
  1380. and reg3,reg1,x
  1381. }
  1382. else if (taicpu(p).oper[2]^.val = $FF) and
  1383. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1384. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1385. MatchInstruction(hp1, [A_UXTB,A_UXTH], [C_None], [PF_None]) and
  1386. (taicpu(hp1).ops = 2) and
  1387. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1388. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1389. { reg1 might not be modified inbetween }
  1390. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1391. begin
  1392. DebugMsg('Peephole AndUxt2And done', p);
  1393. taicpu(hp1).opcode:=A_AND;
  1394. taicpu(hp1).ops:=3;
  1395. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1396. taicpu(hp1).loadconst(2,255);
  1397. GetNextInstruction(p,hp1);
  1398. asml.remove(p);
  1399. p.Free;
  1400. p:=hp1;
  1401. result:=true;
  1402. end
  1403. {
  1404. from
  1405. and reg1,reg0,2^n-1
  1406. mov reg2,reg1, lsl imm1
  1407. (mov reg3,reg2, lsr/asr imm1)
  1408. remove either the and or the lsl/xsr sequence if possible
  1409. }
  1410. else if cutils.ispowerof2(taicpu(p).oper[2]^.val+1,i) and
  1411. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1412. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  1413. (taicpu(hp1).ops=3) and
  1414. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1415. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  1416. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) and
  1417. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  1418. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) then
  1419. begin
  1420. {
  1421. and reg1,reg0,2^n-1
  1422. mov reg2,reg1, lsl imm1
  1423. mov reg3,reg2, lsr/asr imm1
  1424. =>
  1425. and reg1,reg0,2^n-1
  1426. if lsr and 2^n-1>=imm1 or asr and 2^n-1>imm1
  1427. }
  1428. if GetNextInstructionUsingReg(hp1,hp2,taicpu(p).oper[0]^.reg) and
  1429. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  1430. (taicpu(hp2).ops=3) and
  1431. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  1432. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  1433. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) and
  1434. (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  1435. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=taicpu(hp2).oper[2]^.shifterop^.shiftimm) and
  1436. RegEndOfLife(taicpu(hp1).oper[0]^.reg,taicpu(hp2)) and
  1437. ((i<32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) or
  1438. ((i=32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) and
  1439. (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSR))) then
  1440. begin
  1441. DebugMsg('Peephole AndLslXsr2And done', p);
  1442. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  1443. asml.Remove(hp1);
  1444. asml.Remove(hp2);
  1445. hp1.free;
  1446. hp2.free;
  1447. result:=true;
  1448. end
  1449. {
  1450. and reg1,reg0,2^n-1
  1451. mov reg2,reg1, lsl imm1
  1452. =>
  1453. mov reg2,reg1, lsl imm1
  1454. if imm1>i
  1455. }
  1456. else if i>32-taicpu(hp1).oper[2]^.shifterop^.shiftimm then
  1457. begin
  1458. DebugMsg('Peephole AndLsl2Lsl done', p);
  1459. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[0]^.reg;
  1460. GetNextInstruction(p, hp1);
  1461. asml.Remove(p);
  1462. p.free;
  1463. p:=hp1;
  1464. result:=true;
  1465. end
  1466. end;
  1467. end;
  1468. {
  1469. change
  1470. add/sub reg2,reg1,const1
  1471. str/ldr reg3,[reg2,const2]
  1472. dealloc reg2
  1473. to
  1474. str/ldr reg3,[reg1,const2+/-const1]
  1475. }
  1476. if (taicpu(p).opcode in [A_ADD,A_SUB]) and
  1477. (taicpu(p).ops>2) and
  1478. (taicpu(p).oper[1]^.typ = top_reg) and
  1479. (taicpu(p).oper[2]^.typ = top_const) then
  1480. begin
  1481. hp1:=p;
  1482. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) and
  1483. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  1484. MatchInstruction(hp1, [A_LDR, A_STR], [C_None], []) and
  1485. (taicpu(hp1).oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  1486. { don't optimize if the register is stored/overwritten }
  1487. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[1]^.reg) and
  1488. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1489. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1490. { new offset must be valid: either in the range of 8 or 12 bit, depend on the
  1491. ldr postfix }
  1492. (((taicpu(p).opcode=A_ADD) and
  1493. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset+taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1494. ) or
  1495. ((taicpu(p).opcode=A_SUB) and
  1496. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset-taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1497. )
  1498. ) do
  1499. begin
  1500. { neither reg1 nor reg2 might be changed inbetween }
  1501. if RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1) or
  1502. RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1) then
  1503. break;
  1504. { reg2 must be either overwritten by the ldr or it is deallocated afterwards }
  1505. if ((taicpu(hp1).opcode=A_LDR) and (taicpu(p).oper[0]^.reg=taicpu(hp1).oper[0]^.reg)) or
  1506. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  1507. begin
  1508. { remember last instruction }
  1509. hp2:=hp1;
  1510. DebugMsg('Peephole Add/SubLdr2Ldr done', p);
  1511. hp1:=p;
  1512. { fix all ldr/str }
  1513. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) do
  1514. begin
  1515. taicpu(hp1).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  1516. if taicpu(p).opcode=A_ADD then
  1517. inc(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val)
  1518. else
  1519. dec(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val);
  1520. if hp1=hp2 then
  1521. break;
  1522. end;
  1523. GetNextInstruction(p,hp1);
  1524. asml.remove(p);
  1525. p.free;
  1526. p:=hp1;
  1527. break;
  1528. end;
  1529. end;
  1530. end;
  1531. {
  1532. change
  1533. add reg1, ...
  1534. mov reg2, reg1
  1535. to
  1536. add reg2, ...
  1537. }
  1538. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1539. begin
  1540. if (taicpu(p).ops=3) then
  1541. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  1542. end;
  1543. if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  1544. LookForPreindexedPattern(taicpu(p)) then
  1545. begin
  1546. GetNextInstruction(p,hp1);
  1547. DebugMsg('Peephole Add/Sub to Preindexed done', p);
  1548. asml.remove(p);
  1549. p.free;
  1550. p:=hp1;
  1551. end;
  1552. end;
  1553. {$ifdef dummy}
  1554. A_MVN:
  1555. begin
  1556. {
  1557. change
  1558. mvn reg2,reg1
  1559. and reg3,reg4,reg2
  1560. dealloc reg2
  1561. to
  1562. bic reg3,reg4,reg1
  1563. }
  1564. if (taicpu(p).oper[1]^.typ = top_reg) and
  1565. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1566. MatchInstruction(hp1,A_AND,[],[]) and
  1567. (((taicpu(hp1).ops=3) and
  1568. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1569. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  1570. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) or
  1571. ((taicpu(hp1).ops=2) and
  1572. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1573. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1574. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1575. { reg1 might not be modified inbetween }
  1576. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1577. begin
  1578. DebugMsg('Peephole MvnAnd2Bic done', p);
  1579. taicpu(hp1).opcode:=A_BIC;
  1580. if taicpu(hp1).ops=3 then
  1581. begin
  1582. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1583. taicpu(hp1).loadReg(1,taicpu(hp1).oper[2]^.reg); // Swap operands
  1584. taicpu(hp1).loadReg(2,taicpu(p).oper[1]^.reg);
  1585. end
  1586. else
  1587. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1588. GetNextInstruction(p, hp1);
  1589. asml.remove(p);
  1590. p.free;
  1591. p:=hp1;
  1592. end;
  1593. end;
  1594. {$endif dummy}
  1595. A_UXTB:
  1596. begin
  1597. {
  1598. change
  1599. uxtb reg2,reg1
  1600. strb reg2,[...]
  1601. dealloc reg2
  1602. to
  1603. strb reg1,[...]
  1604. }
  1605. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1606. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1607. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1608. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1609. { the reference in strb might not use reg2 }
  1610. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1611. { reg1 might not be modified inbetween }
  1612. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1613. begin
  1614. DebugMsg('Peephole UxtbStrb2Strb done', p);
  1615. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1616. GetNextInstruction(p,hp2);
  1617. asml.remove(p);
  1618. p.free;
  1619. p:=hp2;
  1620. result:=true;
  1621. end
  1622. {
  1623. change
  1624. uxtb reg2,reg1
  1625. uxth reg3,reg2
  1626. dealloc reg2
  1627. to
  1628. uxtb reg3,reg1
  1629. }
  1630. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1631. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1632. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1633. (taicpu(hp1).ops = 2) and
  1634. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1635. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1636. { reg1 might not be modified inbetween }
  1637. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1638. begin
  1639. DebugMsg('Peephole UxtbUxth2Uxtb done', p);
  1640. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1641. asml.remove(hp1);
  1642. hp1.free;
  1643. result:=true;
  1644. end
  1645. {
  1646. change
  1647. uxtb reg2,reg1
  1648. uxtb reg3,reg2
  1649. dealloc reg2
  1650. to
  1651. uxtb reg3,reg1
  1652. }
  1653. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1654. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1655. MatchInstruction(hp1, A_UXTB, [C_None], [PF_None]) and
  1656. (taicpu(hp1).ops = 2) and
  1657. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1658. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1659. { reg1 might not be modified inbetween }
  1660. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1661. begin
  1662. DebugMsg('Peephole UxtbUxtb2Uxtb done', p);
  1663. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1664. asml.remove(hp1);
  1665. hp1.free;
  1666. result:=true;
  1667. end
  1668. {
  1669. change
  1670. uxtb reg2,reg1
  1671. and reg3,reg2,#0x*FF
  1672. dealloc reg2
  1673. to
  1674. uxtb reg3,reg1
  1675. }
  1676. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1677. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1678. (taicpu(p).ops=2) and
  1679. MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  1680. (taicpu(hp1).ops=3) and
  1681. (taicpu(hp1).oper[2]^.typ=top_const) and
  1682. ((taicpu(hp1).oper[2]^.val and $FF)=$FF) and
  1683. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1684. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1685. { reg1 might not be modified inbetween }
  1686. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1687. begin
  1688. DebugMsg('Peephole UxtbAndImm2Uxtb done', p);
  1689. taicpu(hp1).opcode:=A_UXTB;
  1690. taicpu(hp1).ops:=2;
  1691. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1692. GetNextInstruction(p,hp2);
  1693. asml.remove(p);
  1694. p.free;
  1695. p:=hp2;
  1696. result:=true;
  1697. end
  1698. else if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1699. begin
  1700. //if (taicpu(p).ops=3) then
  1701. RemoveSuperfluousMove(p, hp1, 'UxtbMov2Data');
  1702. end;
  1703. end;
  1704. A_UXTH:
  1705. begin
  1706. {
  1707. change
  1708. uxth reg2,reg1
  1709. strh reg2,[...]
  1710. dealloc reg2
  1711. to
  1712. strh reg1,[...]
  1713. }
  1714. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1715. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1716. MatchInstruction(hp1, A_STR, [C_None], [PF_H]) and
  1717. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1718. { the reference in strb might not use reg2 }
  1719. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1720. { reg1 might not be modified inbetween }
  1721. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1722. begin
  1723. DebugMsg('Peephole UXTHStrh2Strh done', p);
  1724. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1725. GetNextInstruction(p, hp1);
  1726. asml.remove(p);
  1727. p.free;
  1728. p:=hp1;
  1729. result:=true;
  1730. end
  1731. {
  1732. change
  1733. uxth reg2,reg1
  1734. uxth reg3,reg2
  1735. dealloc reg2
  1736. to
  1737. uxth reg3,reg1
  1738. }
  1739. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1740. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1741. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1742. (taicpu(hp1).ops=2) and
  1743. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1744. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1745. { reg1 might not be modified inbetween }
  1746. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1747. begin
  1748. DebugMsg('Peephole UxthUxth2Uxth done', p);
  1749. taicpu(hp1).opcode:=A_UXTH;
  1750. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1751. GetNextInstruction(p, hp1);
  1752. asml.remove(p);
  1753. p.free;
  1754. p:=hp1;
  1755. result:=true;
  1756. end
  1757. {
  1758. change
  1759. uxth reg2,reg1
  1760. and reg3,reg2,#65535
  1761. dealloc reg2
  1762. to
  1763. uxth reg3,reg1
  1764. }
  1765. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1766. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1767. MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  1768. (taicpu(hp1).ops=3) and
  1769. (taicpu(hp1).oper[2]^.typ=top_const) and
  1770. ((taicpu(hp1).oper[2]^.val and $FFFF)=$FFFF) and
  1771. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1772. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1773. { reg1 might not be modified inbetween }
  1774. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1775. begin
  1776. DebugMsg('Peephole UxthAndImm2Uxth done', p);
  1777. taicpu(hp1).opcode:=A_UXTH;
  1778. taicpu(hp1).ops:=2;
  1779. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1780. GetNextInstruction(p, hp1);
  1781. asml.remove(p);
  1782. p.free;
  1783. p:=hp1;
  1784. result:=true;
  1785. end
  1786. else if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1787. begin
  1788. //if (taicpu(p).ops=3) then
  1789. RemoveSuperfluousMove(p, hp1, 'UxthMov2Data');
  1790. end;
  1791. end;
  1792. A_CMP:
  1793. begin
  1794. {
  1795. change
  1796. cmp reg,const1
  1797. moveq reg,const1
  1798. movne reg,const2
  1799. to
  1800. cmp reg,const1
  1801. movne reg,const2
  1802. }
  1803. if (taicpu(p).oper[1]^.typ = top_const) and
  1804. GetNextInstruction(p, hp1) and
  1805. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1806. (taicpu(hp1).oper[1]^.typ = top_const) and
  1807. GetNextInstruction(hp1, hp2) and
  1808. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1809. (taicpu(hp1).oper[1]^.typ = top_const) then
  1810. begin
  1811. RemoveRedundantMove(p, hp1, asml);
  1812. RemoveRedundantMove(p, hp2, asml);
  1813. end;
  1814. end;
  1815. A_STM:
  1816. begin
  1817. {
  1818. change
  1819. stmfd r13!,[r14]
  1820. sub r13,r13,#4
  1821. bl abc
  1822. add r13,r13,#4
  1823. ldmfd r13!,[r15]
  1824. into
  1825. b abc
  1826. }
  1827. if not(ts_thumb_interworking in current_settings.targetswitches) and
  1828. MatchInstruction(p, A_STM, [C_None], [PF_FD]) and
  1829. GetNextInstruction(p, hp1) and
  1830. GetNextInstruction(hp1, hp2) and
  1831. SkipEntryExitMarker(hp2, hp2) and
  1832. GetNextInstruction(hp2, hp3) and
  1833. SkipEntryExitMarker(hp3, hp3) and
  1834. GetNextInstruction(hp3, hp4) and
  1835. (taicpu(p).oper[0]^.typ = top_ref) and
  1836. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1837. (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  1838. (taicpu(p).oper[0]^.ref^.offset=0) and
  1839. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1840. (taicpu(p).oper[1]^.typ = top_regset) and
  1841. (taicpu(p).oper[1]^.regset^ = [RS_R14]) and
  1842. MatchInstruction(hp1, A_SUB, [C_None], [PF_NONE]) and
  1843. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1844. (taicpu(hp1).oper[0]^.reg = NR_STACK_POINTER_REG) and
  1845. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) and
  1846. (taicpu(hp1).oper[2]^.typ = top_const) and
  1847. MatchInstruction(hp3, A_ADD, [C_None], [PF_NONE]) and
  1848. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[0]^) and
  1849. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[1]^) and
  1850. MatchOperand(taicpu(hp1).oper[2]^,taicpu(hp3).oper[2]^) and
  1851. MatchInstruction(hp2, [A_BL,A_BLX], [C_None], [PF_NONE]) and
  1852. (taicpu(hp2).oper[0]^.typ = top_ref) and
  1853. MatchInstruction(hp4, A_LDM, [C_None], [PF_FD]) and
  1854. MatchOperand(taicpu(p).oper[0]^,taicpu(hp4).oper[0]^) and
  1855. (taicpu(hp4).oper[1]^.typ = top_regset) and
  1856. (taicpu(hp4).oper[1]^.regset^ = [RS_R15]) then
  1857. begin
  1858. asml.Remove(p);
  1859. asml.Remove(hp1);
  1860. asml.Remove(hp3);
  1861. asml.Remove(hp4);
  1862. taicpu(hp2).opcode:=A_B;
  1863. p.free;
  1864. hp1.free;
  1865. hp3.free;
  1866. hp4.free;
  1867. p:=hp2;
  1868. DebugMsg('Peephole Bl2B done', p);
  1869. end;
  1870. end;
  1871. end;
  1872. end;
  1873. end;
  1874. end;
  1875. { instructions modifying the CPSR can be only the last instruction }
  1876. function MustBeLast(p : tai) : boolean;
  1877. begin
  1878. Result:=(p.typ=ait_instruction) and
  1879. ((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
  1880. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  1881. (taicpu(p).oppostfix=PF_S));
  1882. end;
  1883. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  1884. var
  1885. p,hp1,hp2: tai;
  1886. l : longint;
  1887. condition : tasmcond;
  1888. hp3: tai;
  1889. WasLast: boolean;
  1890. { UsedRegs, TmpUsedRegs: TRegSet; }
  1891. begin
  1892. p := BlockStart;
  1893. { UsedRegs := []; }
  1894. while (p <> BlockEnd) Do
  1895. begin
  1896. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  1897. case p.Typ Of
  1898. Ait_Instruction:
  1899. begin
  1900. case taicpu(p).opcode Of
  1901. A_B:
  1902. if (taicpu(p).condition<>C_None) and
  1903. not(GenerateThumbCode) then
  1904. begin
  1905. { check for
  1906. Bxx xxx
  1907. <several instructions>
  1908. xxx:
  1909. }
  1910. l:=0;
  1911. WasLast:=False;
  1912. GetNextInstruction(p, hp1);
  1913. while assigned(hp1) and
  1914. (l<=4) and
  1915. CanBeCond(hp1) and
  1916. { stop on labels }
  1917. not(hp1.typ=ait_label) do
  1918. begin
  1919. inc(l);
  1920. if MustBeLast(hp1) then
  1921. begin
  1922. WasLast:=True;
  1923. GetNextInstruction(hp1,hp1);
  1924. break;
  1925. end
  1926. else
  1927. GetNextInstruction(hp1,hp1);
  1928. end;
  1929. if assigned(hp1) then
  1930. begin
  1931. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1932. begin
  1933. if (l<=4) and (l>0) then
  1934. begin
  1935. condition:=inverse_cond(taicpu(p).condition);
  1936. hp2:=p;
  1937. GetNextInstruction(p,hp1);
  1938. p:=hp1;
  1939. repeat
  1940. if hp1.typ=ait_instruction then
  1941. taicpu(hp1).condition:=condition;
  1942. if MustBeLast(hp1) then
  1943. begin
  1944. GetNextInstruction(hp1,hp1);
  1945. break;
  1946. end
  1947. else
  1948. GetNextInstruction(hp1,hp1);
  1949. until not(assigned(hp1)) or
  1950. not(CanBeCond(hp1)) or
  1951. (hp1.typ=ait_label);
  1952. { wait with removing else GetNextInstruction could
  1953. ignore the label if it was the only usage in the
  1954. jump moved away }
  1955. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1956. asml.remove(hp2);
  1957. hp2.free;
  1958. continue;
  1959. end;
  1960. end
  1961. else
  1962. { do not perform further optimizations if there is inctructon
  1963. in block #1 which can not be optimized.
  1964. }
  1965. if not WasLast then
  1966. begin
  1967. { check further for
  1968. Bcc xxx
  1969. <several instructions 1>
  1970. B yyy
  1971. xxx:
  1972. <several instructions 2>
  1973. yyy:
  1974. }
  1975. { hp2 points to jmp yyy }
  1976. hp2:=hp1;
  1977. { skip hp1 to xxx }
  1978. GetNextInstruction(hp1, hp1);
  1979. if assigned(hp2) and
  1980. assigned(hp1) and
  1981. (l<=3) and
  1982. (hp2.typ=ait_instruction) and
  1983. (taicpu(hp2).is_jmp) and
  1984. (taicpu(hp2).condition=C_None) and
  1985. { real label and jump, no further references to the
  1986. label are allowed }
  1987. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  1988. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1989. begin
  1990. l:=0;
  1991. { skip hp1 to <several moves 2> }
  1992. GetNextInstruction(hp1, hp1);
  1993. while assigned(hp1) and
  1994. CanBeCond(hp1) do
  1995. begin
  1996. inc(l);
  1997. GetNextInstruction(hp1, hp1);
  1998. end;
  1999. { hp1 points to yyy: }
  2000. if assigned(hp1) and
  2001. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  2002. begin
  2003. condition:=inverse_cond(taicpu(p).condition);
  2004. GetNextInstruction(p,hp1);
  2005. hp3:=p;
  2006. p:=hp1;
  2007. repeat
  2008. if hp1.typ=ait_instruction then
  2009. taicpu(hp1).condition:=condition;
  2010. GetNextInstruction(hp1,hp1);
  2011. until not(assigned(hp1)) or
  2012. not(CanBeCond(hp1));
  2013. { hp2 is still at jmp yyy }
  2014. GetNextInstruction(hp2,hp1);
  2015. { hp2 is now at xxx: }
  2016. condition:=inverse_cond(condition);
  2017. GetNextInstruction(hp1,hp1);
  2018. { hp1 is now at <several movs 2> }
  2019. repeat
  2020. taicpu(hp1).condition:=condition;
  2021. GetNextInstruction(hp1,hp1);
  2022. until not(assigned(hp1)) or
  2023. not(CanBeCond(hp1)) or
  2024. (hp1.typ=ait_label);
  2025. {
  2026. asml.remove(hp1.next)
  2027. hp1.next.free;
  2028. asml.remove(hp1);
  2029. hp1.free;
  2030. }
  2031. { remove Bcc }
  2032. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  2033. asml.remove(hp3);
  2034. hp3.free;
  2035. { remove jmp }
  2036. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2037. asml.remove(hp2);
  2038. hp2.free;
  2039. continue;
  2040. end;
  2041. end;
  2042. end;
  2043. end;
  2044. end;
  2045. end;
  2046. end;
  2047. end;
  2048. p := tai(p.next)
  2049. end;
  2050. end;
  2051. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  2052. begin
  2053. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  2054. Result:=true
  2055. else If MatchInstruction(p1, [A_LDR, A_STR], [], [PF_D]) and
  2056. (getsupreg(taicpu(p1).oper[0]^.reg)+1=getsupreg(reg)) then
  2057. Result:=true
  2058. else
  2059. Result:=inherited RegInInstruction(Reg, p1);
  2060. end;
  2061. const
  2062. { set of opcode which might or do write to memory }
  2063. { TODO : extend armins.dat to contain r/w info }
  2064. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  2065. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  2066. { adjust the register live information when swapping the two instructions p and hp1,
  2067. they must follow one after the other }
  2068. procedure TCpuPreRegallocScheduler.SwapRegLive(p,hp1 : taicpu);
  2069. procedure CheckLiveEnd(reg : tregister);
  2070. var
  2071. supreg : TSuperRegister;
  2072. regtype : TRegisterType;
  2073. begin
  2074. if reg=NR_NO then
  2075. exit;
  2076. regtype:=getregtype(reg);
  2077. supreg:=getsupreg(reg);
  2078. if (cg.rg[regtype].live_end[supreg]=hp1) and
  2079. RegInInstruction(reg,p) then
  2080. cg.rg[regtype].live_end[supreg]:=p;
  2081. end;
  2082. procedure CheckLiveStart(reg : TRegister);
  2083. var
  2084. supreg : TSuperRegister;
  2085. regtype : TRegisterType;
  2086. begin
  2087. if reg=NR_NO then
  2088. exit;
  2089. regtype:=getregtype(reg);
  2090. supreg:=getsupreg(reg);
  2091. if (cg.rg[regtype].live_start[supreg]=p) and
  2092. RegInInstruction(reg,hp1) then
  2093. cg.rg[regtype].live_start[supreg]:=hp1;
  2094. end;
  2095. var
  2096. i : longint;
  2097. r : TSuperRegister;
  2098. begin
  2099. { assumption: p is directly followed by hp1 }
  2100. { if live of any reg used by p starts at p and hp1 uses this register then
  2101. set live start to hp1 }
  2102. for i:=0 to p.ops-1 do
  2103. case p.oper[i]^.typ of
  2104. Top_Reg:
  2105. CheckLiveStart(p.oper[i]^.reg);
  2106. Top_Ref:
  2107. begin
  2108. CheckLiveStart(p.oper[i]^.ref^.base);
  2109. CheckLiveStart(p.oper[i]^.ref^.index);
  2110. end;
  2111. Top_Shifterop:
  2112. CheckLiveStart(p.oper[i]^.shifterop^.rs);
  2113. Top_RegSet:
  2114. for r:=RS_R0 to RS_R15 do
  2115. if r in p.oper[i]^.regset^ then
  2116. CheckLiveStart(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2117. end;
  2118. { if live of any reg used by hp1 ends at hp1 and p uses this register then
  2119. set live end to p }
  2120. for i:=0 to hp1.ops-1 do
  2121. case hp1.oper[i]^.typ of
  2122. Top_Reg:
  2123. CheckLiveEnd(hp1.oper[i]^.reg);
  2124. Top_Ref:
  2125. begin
  2126. CheckLiveEnd(hp1.oper[i]^.ref^.base);
  2127. CheckLiveEnd(hp1.oper[i]^.ref^.index);
  2128. end;
  2129. Top_Shifterop:
  2130. CheckLiveStart(hp1.oper[i]^.shifterop^.rs);
  2131. Top_RegSet:
  2132. for r:=RS_R0 to RS_R15 do
  2133. if r in hp1.oper[i]^.regset^ then
  2134. CheckLiveEnd(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2135. end;
  2136. end;
  2137. function TCpuPreRegallocScheduler.SchedulerPass1Cpu(var p: tai): boolean;
  2138. { TODO : schedule also forward }
  2139. { TODO : schedule distance > 1 }
  2140. var
  2141. hp1,hp2,hp3,hp4,hp5,insertpos : tai;
  2142. list : TAsmList;
  2143. begin
  2144. result:=true;
  2145. list:=TAsmList.create_without_marker;
  2146. p:=BlockStart;
  2147. while p<>BlockEnd Do
  2148. begin
  2149. if (p.typ=ait_instruction) and
  2150. GetNextInstruction(p,hp1) and
  2151. (hp1.typ=ait_instruction) and
  2152. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  2153. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  2154. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  2155. not(RegModifiedByInstruction(NR_PC,p))
  2156. ) or
  2157. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  2158. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  2159. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  2160. (taicpu(hp1).oper[1]^.ref^.offset=0)
  2161. )
  2162. ) or
  2163. { try to prove that the memory accesses don't overlapp }
  2164. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  2165. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  2166. (taicpu(p).oppostfix=PF_None) and
  2167. (taicpu(hp1).oppostfix=PF_None) and
  2168. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  2169. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  2170. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  2171. (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)]))
  2172. )
  2173. )
  2174. ) and
  2175. GetNextInstruction(hp1,hp2) and
  2176. (hp2.typ=ait_instruction) and
  2177. { loaded register used by next instruction? }
  2178. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  2179. { loaded register not used by previous instruction? }
  2180. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  2181. { same condition? }
  2182. (taicpu(p).condition=taicpu(hp1).condition) and
  2183. { first instruction might not change the register used as base }
  2184. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  2185. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  2186. ) and
  2187. { first instruction might not change the register used as index }
  2188. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  2189. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  2190. ) then
  2191. begin
  2192. hp3:=tai(p.Previous);
  2193. hp5:=tai(p.next);
  2194. asml.Remove(p);
  2195. { if there is a reg. dealloc instruction associated with p, move it together with p }
  2196. { before the instruction? }
  2197. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  2198. begin
  2199. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  2200. RegInInstruction(tai_regalloc(hp3).reg,p) then
  2201. begin
  2202. hp4:=hp3;
  2203. hp3:=tai(hp3.Previous);
  2204. asml.Remove(hp4);
  2205. list.Concat(hp4);
  2206. end
  2207. else
  2208. hp3:=tai(hp3.Previous);
  2209. end;
  2210. list.Concat(p);
  2211. SwapRegLive(taicpu(p),taicpu(hp1));
  2212. { after the instruction? }
  2213. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  2214. begin
  2215. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  2216. RegInInstruction(tai_regalloc(hp5).reg,p) then
  2217. begin
  2218. hp4:=hp5;
  2219. hp5:=tai(hp5.next);
  2220. asml.Remove(hp4);
  2221. list.Concat(hp4);
  2222. end
  2223. else
  2224. hp5:=tai(hp5.Next);
  2225. end;
  2226. asml.Remove(hp1);
  2227. { if there are address labels associated with hp2, those must
  2228. stay with hp2 (e.g. for GOT-less PIC) }
  2229. insertpos:=hp2;
  2230. while assigned(hp2.previous) and
  2231. (tai(hp2.previous).typ<>ait_instruction) do
  2232. begin
  2233. hp2:=tai(hp2.previous);
  2234. if (hp2.typ=ait_label) and
  2235. (tai_label(hp2).labsym.typ=AT_ADDR) then
  2236. insertpos:=hp2;
  2237. end;
  2238. {$ifdef DEBUG_PREREGSCHEDULER}
  2239. asml.insertbefore(tai_comment.Create(strpnew('Rescheduled')),insertpos);
  2240. {$endif DEBUG_PREREGSCHEDULER}
  2241. asml.InsertBefore(hp1,insertpos);
  2242. asml.InsertListBefore(insertpos,list);
  2243. p:=tai(p.next)
  2244. end
  2245. else if p.typ=ait_instruction then
  2246. p:=hp1
  2247. else
  2248. p:=tai(p.next);
  2249. end;
  2250. list.Free;
  2251. end;
  2252. procedure DecrementPreceedingIT(list: TAsmList; p: tai);
  2253. var
  2254. hp : tai;
  2255. l : longint;
  2256. begin
  2257. hp := tai(p.Previous);
  2258. l := 1;
  2259. while assigned(hp) and
  2260. (l <= 4) do
  2261. begin
  2262. if hp.typ=ait_instruction then
  2263. begin
  2264. if (taicpu(hp).opcode>=A_IT) and
  2265. (taicpu(hp).opcode <= A_ITTTT) then
  2266. begin
  2267. if (taicpu(hp).opcode = A_IT) and
  2268. (l=1) then
  2269. list.Remove(hp)
  2270. else
  2271. case taicpu(hp).opcode of
  2272. A_ITE:
  2273. if l=2 then taicpu(hp).opcode := A_IT;
  2274. A_ITT:
  2275. if l=2 then taicpu(hp).opcode := A_IT;
  2276. A_ITEE:
  2277. if l=3 then taicpu(hp).opcode := A_ITE;
  2278. A_ITTE:
  2279. if l=3 then taicpu(hp).opcode := A_ITT;
  2280. A_ITET:
  2281. if l=3 then taicpu(hp).opcode := A_ITE;
  2282. A_ITTT:
  2283. if l=3 then taicpu(hp).opcode := A_ITT;
  2284. A_ITEEE:
  2285. if l=4 then taicpu(hp).opcode := A_ITEE;
  2286. A_ITTEE:
  2287. if l=4 then taicpu(hp).opcode := A_ITTE;
  2288. A_ITETE:
  2289. if l=4 then taicpu(hp).opcode := A_ITET;
  2290. A_ITTTE:
  2291. if l=4 then taicpu(hp).opcode := A_ITTT;
  2292. A_ITEET:
  2293. if l=4 then taicpu(hp).opcode := A_ITEE;
  2294. A_ITTET:
  2295. if l=4 then taicpu(hp).opcode := A_ITTE;
  2296. A_ITETT:
  2297. if l=4 then taicpu(hp).opcode := A_ITET;
  2298. A_ITTTT:
  2299. if l=4 then taicpu(hp).opcode := A_ITTT;
  2300. end;
  2301. break;
  2302. end;
  2303. {else if (taicpu(hp).condition<>taicpu(p).condition) or
  2304. (taicpu(hp).condition<>inverse_cond(taicpu(p).condition)) then
  2305. break;}
  2306. inc(l);
  2307. end;
  2308. hp := tai(hp.Previous);
  2309. end;
  2310. end;
  2311. function TCpuThumb2AsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  2312. var
  2313. hp : taicpu;
  2314. hp1,hp2 : tai;
  2315. oldreg : TRegister;
  2316. begin
  2317. result:=false;
  2318. if inherited PeepHoleOptPass1Cpu(p) then
  2319. result:=true
  2320. else if (p.typ=ait_instruction) and
  2321. MatchInstruction(p, A_STM, [C_None], [PF_FD,PF_DB]) and
  2322. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  2323. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  2324. ((taicpu(p).oper[1]^.regset^*[8..13,15])=[]) then
  2325. begin
  2326. DebugMsg('Peephole Stm2Push done', p);
  2327. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  2328. AsmL.InsertAfter(hp, p);
  2329. asml.Remove(p);
  2330. p:=hp;
  2331. result:=true;
  2332. end
  2333. {else if (p.typ=ait_instruction) and
  2334. MatchInstruction(p, A_STR, [C_None], [PF_None]) and
  2335. (taicpu(p).oper[1]^.ref^.addressmode=AM_PREINDEXED) and
  2336. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  2337. (taicpu(p).oper[1]^.ref^.offset=-4) and
  2338. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,14]) then
  2339. begin
  2340. DebugMsg('Peephole Str2Push done', p);
  2341. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  2342. asml.InsertAfter(hp, p);
  2343. asml.Remove(p);
  2344. p.Free;
  2345. p:=hp;
  2346. result:=true;
  2347. end}
  2348. else if (p.typ=ait_instruction) and
  2349. MatchInstruction(p, A_LDM, [C_None], [PF_FD,PF_IA]) and
  2350. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  2351. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  2352. ((taicpu(p).oper[1]^.regset^*[8..14])=[]) then
  2353. begin
  2354. DebugMsg('Peephole Ldm2Pop done', p);
  2355. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  2356. asml.InsertBefore(hp, p);
  2357. asml.Remove(p);
  2358. p.Free;
  2359. p:=hp;
  2360. result:=true;
  2361. end
  2362. {else if (p.typ=ait_instruction) and
  2363. MatchInstruction(p, A_LDR, [C_None], [PF_None]) and
  2364. (taicpu(p).oper[1]^.ref^.addressmode=AM_POSTINDEXED) and
  2365. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  2366. (taicpu(p).oper[1]^.ref^.offset=4) and
  2367. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,15]) then
  2368. begin
  2369. DebugMsg('Peephole Ldr2Pop done', p);
  2370. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  2371. asml.InsertBefore(hp, p);
  2372. asml.Remove(p);
  2373. p.Free;
  2374. p:=hp;
  2375. result:=true;
  2376. end}
  2377. else if (p.typ=ait_instruction) and
  2378. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2379. (taicpu(p).ops = 2) and
  2380. (taicpu(p).oper[1]^.typ=top_const) and
  2381. ((taicpu(p).oper[1]^.val=255) or
  2382. (taicpu(p).oper[1]^.val=65535)) then
  2383. begin
  2384. DebugMsg('Peephole AndR2Uxt done', p);
  2385. if taicpu(p).oper[1]^.val=255 then
  2386. taicpu(p).opcode:=A_UXTB
  2387. else
  2388. taicpu(p).opcode:=A_UXTH;
  2389. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  2390. result := true;
  2391. end
  2392. else if (p.typ=ait_instruction) and
  2393. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2394. (taicpu(p).ops = 3) and
  2395. (taicpu(p).oper[2]^.typ=top_const) and
  2396. ((taicpu(p).oper[2]^.val=255) or
  2397. (taicpu(p).oper[2]^.val=65535)) then
  2398. begin
  2399. DebugMsg('Peephole AndRR2Uxt done', p);
  2400. if taicpu(p).oper[2]^.val=255 then
  2401. taicpu(p).opcode:=A_UXTB
  2402. else
  2403. taicpu(p).opcode:=A_UXTH;
  2404. taicpu(p).ops:=2;
  2405. result := true;
  2406. end
  2407. {
  2408. Turn
  2409. mul reg0, z,w
  2410. sub/add x, y, reg0
  2411. dealloc reg0
  2412. into
  2413. mls/mla x,z,w,y
  2414. }
  2415. else if (p.typ=ait_instruction) and
  2416. MatchInstruction(p, [A_MUL], [C_None], [PF_None]) and
  2417. (taicpu(p).ops=3) and
  2418. (taicpu(p).oper[0]^.typ = top_reg) and
  2419. (taicpu(p).oper[1]^.typ = top_reg) and
  2420. (taicpu(p).oper[2]^.typ = top_reg) and
  2421. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  2422. MatchInstruction(hp1,[A_ADD,A_SUB],[C_None],[PF_None]) and
  2423. (((taicpu(hp1).ops=3) and
  2424. (taicpu(hp1).oper[2]^.typ=top_reg) and
  2425. ((MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) and
  2426. (not RegModifiedBetween(taicpu(hp1).oper[1]^.reg, p, hp1))) or
  2427. ((MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  2428. (taicpu(hp1).opcode=A_ADD) and
  2429. (not RegModifiedBetween(taicpu(hp1).oper[2]^.reg, p, hp1)))))) or
  2430. ((taicpu(hp1).ops=2) and
  2431. (taicpu(hp1).oper[1]^.typ=top_reg) and
  2432. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  2433. (RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1))) then
  2434. begin
  2435. if taicpu(hp1).opcode=A_ADD then
  2436. begin
  2437. taicpu(hp1).opcode:=A_MLA;
  2438. if taicpu(hp1).ops=3 then
  2439. begin
  2440. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^) then
  2441. oldreg:=taicpu(hp1).oper[2]^.reg
  2442. else
  2443. oldreg:=taicpu(hp1).oper[1]^.reg;
  2444. end
  2445. else
  2446. oldreg:=taicpu(hp1).oper[0]^.reg;
  2447. taicpu(hp1).loadreg(1,taicpu(p).oper[1]^.reg);
  2448. taicpu(hp1).loadreg(2,taicpu(p).oper[2]^.reg);
  2449. taicpu(hp1).loadreg(3,oldreg);
  2450. DebugMsg('MulAdd2MLA done', p);
  2451. taicpu(hp1).ops:=4;
  2452. asml.remove(p);
  2453. p.free;
  2454. p:=hp1;
  2455. end
  2456. else
  2457. begin
  2458. taicpu(hp1).opcode:=A_MLS;
  2459. if taicpu(hp1).ops=2 then
  2460. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  2461. taicpu(hp1).loadreg(2,taicpu(p).oper[1]^.reg);
  2462. taicpu(hp1).loadreg(3,taicpu(p).oper[2]^.reg);
  2463. DebugMsg('MulSub2MLS done', p);
  2464. taicpu(hp1).ops:=4;
  2465. asml.remove(p);
  2466. p.free;
  2467. p:=hp1;
  2468. end;
  2469. result:=true;
  2470. end
  2471. {else if (p.typ=ait_instruction) and
  2472. MatchInstruction(p, [A_CMP], [C_None], [PF_None]) and
  2473. (taicpu(p).oper[1]^.typ=top_const) and
  2474. (taicpu(p).oper[1]^.val=0) and
  2475. GetNextInstruction(p,hp1) and
  2476. (taicpu(hp1).opcode=A_B) and
  2477. (taicpu(hp1).condition in [C_EQ,C_NE]) then
  2478. begin
  2479. if taicpu(hp1).condition = C_EQ then
  2480. hp2:=taicpu.op_reg_ref(A_CBZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^)
  2481. else
  2482. hp2:=taicpu.op_reg_ref(A_CBNZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^);
  2483. taicpu(hp2).is_jmp := true;
  2484. asml.InsertAfter(hp2, hp1);
  2485. asml.Remove(hp1);
  2486. hp1.Free;
  2487. asml.Remove(p);
  2488. p.Free;
  2489. p := hp2;
  2490. result := true;
  2491. end}
  2492. end;
  2493. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  2494. var
  2495. p,hp1,hp2: tai;
  2496. l,l2 : longint;
  2497. condition : tasmcond;
  2498. hp3: tai;
  2499. WasLast: boolean;
  2500. { UsedRegs, TmpUsedRegs: TRegSet; }
  2501. begin
  2502. p := BlockStart;
  2503. { UsedRegs := []; }
  2504. while (p <> BlockEnd) Do
  2505. begin
  2506. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  2507. case p.Typ Of
  2508. Ait_Instruction:
  2509. begin
  2510. case taicpu(p).opcode Of
  2511. A_B:
  2512. if taicpu(p).condition<>C_None then
  2513. begin
  2514. { check for
  2515. Bxx xxx
  2516. <several instructions>
  2517. xxx:
  2518. }
  2519. l:=0;
  2520. GetNextInstruction(p, hp1);
  2521. while assigned(hp1) and
  2522. (l<=4) and
  2523. CanBeCond(hp1) and
  2524. { stop on labels }
  2525. not(hp1.typ=ait_label) do
  2526. begin
  2527. inc(l);
  2528. if MustBeLast(hp1) then
  2529. begin
  2530. //hp1:=nil;
  2531. GetNextInstruction(hp1,hp1);
  2532. break;
  2533. end
  2534. else
  2535. GetNextInstruction(hp1,hp1);
  2536. end;
  2537. if assigned(hp1) then
  2538. begin
  2539. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2540. begin
  2541. if (l<=4) and (l>0) then
  2542. begin
  2543. condition:=inverse_cond(taicpu(p).condition);
  2544. hp2:=p;
  2545. GetNextInstruction(p,hp1);
  2546. p:=hp1;
  2547. repeat
  2548. if hp1.typ=ait_instruction then
  2549. taicpu(hp1).condition:=condition;
  2550. if MustBeLast(hp1) then
  2551. begin
  2552. GetNextInstruction(hp1,hp1);
  2553. break;
  2554. end
  2555. else
  2556. GetNextInstruction(hp1,hp1);
  2557. until not(assigned(hp1)) or
  2558. not(CanBeCond(hp1)) or
  2559. (hp1.typ=ait_label);
  2560. { wait with removing else GetNextInstruction could
  2561. ignore the label if it was the only usage in the
  2562. jump moved away }
  2563. asml.InsertAfter(tai_comment.create(strpnew('Collapsed')), hp2);
  2564. DecrementPreceedingIT(asml, hp2);
  2565. case l of
  2566. 1: asml.InsertAfter(taicpu.op_cond(A_IT,condition), hp2);
  2567. 2: asml.InsertAfter(taicpu.op_cond(A_ITT,condition), hp2);
  2568. 3: asml.InsertAfter(taicpu.op_cond(A_ITTT,condition), hp2);
  2569. 4: asml.InsertAfter(taicpu.op_cond(A_ITTTT,condition), hp2);
  2570. end;
  2571. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2572. asml.remove(hp2);
  2573. hp2.free;
  2574. continue;
  2575. end;
  2576. end;
  2577. end;
  2578. end;
  2579. end;
  2580. end;
  2581. end;
  2582. p := tai(p.next)
  2583. end;
  2584. end;
  2585. function TCpuThumb2AsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
  2586. begin
  2587. result:=false;
  2588. if p.typ = ait_instruction then
  2589. begin
  2590. if MatchInstruction(p, A_MOV, [C_None], [PF_None]) and
  2591. (taicpu(p).oper[1]^.typ=top_const) and
  2592. (taicpu(p).oper[1]^.val >= 0) and
  2593. (taicpu(p).oper[1]^.val < 256) and
  2594. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2595. begin
  2596. DebugMsg('Peephole Mov2Movs done', p);
  2597. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2598. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2599. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2600. taicpu(p).oppostfix:=PF_S;
  2601. result:=true;
  2602. end
  2603. else if MatchInstruction(p, A_MVN, [C_None], [PF_None]) and
  2604. (taicpu(p).oper[1]^.typ=top_reg) and
  2605. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2606. begin
  2607. DebugMsg('Peephole Mvn2Mvns done', p);
  2608. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2609. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2610. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2611. taicpu(p).oppostfix:=PF_S;
  2612. result:=true;
  2613. end
  2614. else if MatchInstruction(p, A_RSB, [C_None], [PF_None]) and
  2615. (taicpu(p).ops = 3) and
  2616. (taicpu(p).oper[2]^.typ=top_const) and
  2617. (taicpu(p).oper[2]^.val=0) and
  2618. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2619. begin
  2620. DebugMsg('Peephole Rsb2Rsbs done', p);
  2621. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2622. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2623. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2624. taicpu(p).oppostfix:=PF_S;
  2625. result:=true;
  2626. end
  2627. else if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  2628. (taicpu(p).ops = 3) and
  2629. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2630. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  2631. (taicpu(p).oper[2]^.typ=top_const) and
  2632. (taicpu(p).oper[2]^.val >= 0) and
  2633. (taicpu(p).oper[2]^.val < 256) and
  2634. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2635. begin
  2636. DebugMsg('Peephole AddSub2*s done', p);
  2637. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2638. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2639. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2640. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  2641. taicpu(p).oppostfix:=PF_S;
  2642. taicpu(p).ops := 2;
  2643. result:=true;
  2644. end
  2645. else if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  2646. (taicpu(p).ops = 2) and
  2647. (taicpu(p).oper[1]^.typ=top_reg) and
  2648. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2649. begin
  2650. DebugMsg('Peephole AddSub2*s done', p);
  2651. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2652. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2653. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2654. taicpu(p).oppostfix:=PF_S;
  2655. result:=true;
  2656. end
  2657. else if MatchInstruction(p, [A_ADD], [C_None], [PF_None]) and
  2658. (taicpu(p).ops = 3) and
  2659. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2660. (taicpu(p).oper[2]^.typ=top_reg) then
  2661. begin
  2662. DebugMsg('Peephole AddRRR2AddRR done', p);
  2663. taicpu(p).ops := 2;
  2664. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2665. result:=true;
  2666. end
  2667. 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
  2668. (taicpu(p).ops = 3) and
  2669. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2670. (taicpu(p).oper[2]^.typ=top_reg) and
  2671. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2672. begin
  2673. DebugMsg('Peephole opXXY2opsXY done', p);
  2674. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2675. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2676. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2677. taicpu(p).ops := 2;
  2678. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2679. taicpu(p).oppostfix:=PF_S;
  2680. result:=true;
  2681. end
  2682. 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
  2683. (taicpu(p).ops = 3) and
  2684. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2685. (taicpu(p).oper[2]^.typ in [top_reg,top_const]) then
  2686. begin
  2687. DebugMsg('Peephole opXXY2opXY done', p);
  2688. taicpu(p).ops := 2;
  2689. if taicpu(p).oper[2]^.typ=top_reg then
  2690. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg)
  2691. else
  2692. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  2693. result:=true;
  2694. end
  2695. else if MatchInstruction(p, [A_ADD,A_AND,A_ORR,A_EOR], [C_None], [PF_None,PF_S]) and
  2696. (taicpu(p).ops = 3) and
  2697. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[2]^) and
  2698. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2699. begin
  2700. DebugMsg('Peephole opXYX2opsXY done', p);
  2701. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2702. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2703. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2704. taicpu(p).oppostfix:=PF_S;
  2705. taicpu(p).ops := 2;
  2706. result:=true;
  2707. end
  2708. else if MatchInstruction(p, [A_MOV], [C_None], [PF_None]) and
  2709. (taicpu(p).ops=3) and
  2710. (taicpu(p).oper[2]^.typ=top_shifterop) and
  2711. (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_LSL,SM_LSR,SM_ASR,SM_ROR]) and
  2712. //MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2713. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2714. begin
  2715. DebugMsg('Peephole Mov2Shift done', p);
  2716. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2717. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2718. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2719. taicpu(p).oppostfix:=PF_S;
  2720. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  2721. SM_LSL: taicpu(p).opcode:=A_LSL;
  2722. SM_LSR: taicpu(p).opcode:=A_LSR;
  2723. SM_ASR: taicpu(p).opcode:=A_ASR;
  2724. SM_ROR: taicpu(p).opcode:=A_ROR;
  2725. end;
  2726. if taicpu(p).oper[2]^.shifterop^.rs<>NR_NO then
  2727. taicpu(p).loadreg(2, taicpu(p).oper[2]^.shifterop^.rs)
  2728. else
  2729. taicpu(p).loadconst(2, taicpu(p).oper[2]^.shifterop^.shiftimm);
  2730. result:=true;
  2731. end
  2732. end;
  2733. end;
  2734. begin
  2735. casmoptimizer:=TCpuAsmOptimizer;
  2736. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  2737. End.