aoptcpu.pas 134 KB

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