aoptcpu.pas 161 KB

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