aoptx86.pas 193 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Jonas Maebe
  3. This unit contains the peephole optimizer.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aoptx86;
  18. {$i fpcdefs.inc}
  19. {$define DEBUG_AOPTCPU}
  20. interface
  21. uses
  22. globtype,
  23. cpubase,
  24. aasmtai,aasmcpu,
  25. cgbase,cgutils,
  26. aopt,aoptobj;
  27. type
  28. TX86AsmOptimizer = class(TAsmOptimizer)
  29. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  30. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  31. function RegReadByInstruction(reg : TRegister; hp : tai) : boolean;
  32. function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  33. function GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  34. protected
  35. { checks whether loading a new value in reg1 overwrites the entirety of reg2 }
  36. function Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  37. { checks whether reading the value in reg1 depends on the value of reg2. This
  38. is very similar to SuperRegisterEquals, except it takes into account that
  39. R_SUBH and R_SUBL are independendent (e.g. reading from AL does not
  40. depend on the value in AH). }
  41. function Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  42. procedure DebugMsg(const s : string; p : tai);inline;
  43. class function IsExitCode(p : tai) : boolean;
  44. class function isFoldableArithOp(hp1 : taicpu; reg : tregister) : boolean;
  45. procedure RemoveLastDeallocForFuncRes(p : tai);
  46. function DoSubAddOpt(var p : tai) : Boolean;
  47. function PrePeepholeOptSxx(var p : tai) : boolean;
  48. function PrePeepholeOptIMUL(var p : tai) : boolean;
  49. function OptPass1AND(var p : tai) : boolean;
  50. function OptPass1_V_MOVAP(var p : tai) : boolean;
  51. function OptPass1VOP(var p : tai) : boolean;
  52. function OptPass1MOV(var p : tai) : boolean;
  53. function OptPass1Movx(var p : tai) : boolean;
  54. function OptPass1MOVXX(var p : tai) : boolean;
  55. function OptPass1OP(var p : tai) : boolean;
  56. function OptPass1LEA(var p : tai) : boolean;
  57. function OptPass1Sub(var p : tai) : boolean;
  58. function OptPass1SHLSAL(var p : tai) : boolean;
  59. function OptPass1SETcc(var p : tai) : boolean;
  60. function OptPass1FSTP(var p : tai) : boolean;
  61. function OptPass1FLD(var p : tai) : boolean;
  62. function OptPass1Cmp(var p : tai) : boolean;
  63. function OptPass2MOV(var p : tai) : boolean;
  64. function OptPass2Imul(var p : tai) : boolean;
  65. function OptPass2Jmp(var p : tai) : boolean;
  66. function OptPass2Jcc(var p : tai) : boolean;
  67. function OptPass2Lea(var p: tai): Boolean;
  68. function PostPeepholeOptMov(var p : tai) : Boolean;
  69. {$ifdef x86_64} { These post-peephole optimisations only affect 64-bit registers. [Kit] }
  70. function PostPeepholeOptMovzx(var p : tai) : Boolean;
  71. function PostPeepholeOptXor(var p : tai) : Boolean;
  72. {$endif}
  73. function PostPeepholeOptCmp(var p : tai) : Boolean;
  74. function PostPeepholeOptTestOr(var p : tai) : Boolean;
  75. function PostPeepholeOptCall(var p : tai) : Boolean;
  76. function PostPeepholeOptLea(var p : tai) : Boolean;
  77. procedure OptReferences;
  78. end;
  79. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  80. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  81. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  82. function MatchInstruction(const instr: tai; const ops: array of TAsmOp; const opsize: topsizes): boolean;
  83. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  84. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  85. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  86. function RefsEqual(const r1, r2: treference): boolean;
  87. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  88. { returns true, if ref is a reference using only the registers passed as base and index
  89. and having an offset }
  90. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  91. implementation
  92. uses
  93. cutils,verbose,
  94. globals,
  95. cpuinfo,
  96. procinfo,
  97. aasmbase,
  98. aoptutils,
  99. symconst,symsym,
  100. cgx86,
  101. itcpugas;
  102. {$ifdef DEBUG_AOPTCPU}
  103. const
  104. SPeepholeOptimization: shortstring = 'Peephole Optimization: ';
  105. {$else DEBUG_AOPTCPU}
  106. { Empty strings help the optimizer to remove string concatenations that won't
  107. ever appear to the user on release builds. [Kit] }
  108. const
  109. SPeepholeOptimization = '';
  110. {$endif DEBUG_AOPTCPU}
  111. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  112. begin
  113. result :=
  114. (instr.typ = ait_instruction) and
  115. (taicpu(instr).opcode = op) and
  116. ((opsize = []) or (taicpu(instr).opsize in opsize));
  117. end;
  118. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  119. begin
  120. result :=
  121. (instr.typ = ait_instruction) and
  122. ((taicpu(instr).opcode = op1) or
  123. (taicpu(instr).opcode = op2)
  124. ) and
  125. ((opsize = []) or (taicpu(instr).opsize in opsize));
  126. end;
  127. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  128. begin
  129. result :=
  130. (instr.typ = ait_instruction) and
  131. ((taicpu(instr).opcode = op1) or
  132. (taicpu(instr).opcode = op2) or
  133. (taicpu(instr).opcode = op3)
  134. ) and
  135. ((opsize = []) or (taicpu(instr).opsize in opsize));
  136. end;
  137. function MatchInstruction(const instr : tai;const ops : array of TAsmOp;
  138. const opsize : topsizes) : boolean;
  139. var
  140. op : TAsmOp;
  141. begin
  142. result:=false;
  143. for op in ops do
  144. begin
  145. if (instr.typ = ait_instruction) and
  146. (taicpu(instr).opcode = op) and
  147. ((opsize = []) or (taicpu(instr).opsize in opsize)) then
  148. begin
  149. result:=true;
  150. exit;
  151. end;
  152. end;
  153. end;
  154. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  155. begin
  156. result := (oper.typ = top_reg) and (oper.reg = reg);
  157. end;
  158. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  159. begin
  160. result := (oper.typ = top_const) and (oper.val = a);
  161. end;
  162. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  163. begin
  164. result := oper1.typ = oper2.typ;
  165. if result then
  166. case oper1.typ of
  167. top_const:
  168. Result:=oper1.val = oper2.val;
  169. top_reg:
  170. Result:=oper1.reg = oper2.reg;
  171. top_ref:
  172. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  173. else
  174. internalerror(2013102801);
  175. end
  176. end;
  177. function RefsEqual(const r1, r2: treference): boolean;
  178. begin
  179. RefsEqual :=
  180. (r1.offset = r2.offset) and
  181. (r1.segment = r2.segment) and (r1.base = r2.base) and
  182. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  183. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  184. (r1.relsymbol = r2.relsymbol) and
  185. (r1.volatility=[]) and
  186. (r2.volatility=[]);
  187. end;
  188. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  189. begin
  190. Result:=(ref.offset=0) and
  191. (ref.scalefactor in [0,1]) and
  192. (ref.segment=NR_NO) and
  193. (ref.symbol=nil) and
  194. (ref.relsymbol=nil) and
  195. ((base=NR_INVALID) or
  196. (ref.base=base)) and
  197. ((index=NR_INVALID) or
  198. (ref.index=index)) and
  199. (ref.volatility=[]);
  200. end;
  201. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  202. begin
  203. Result:=(ref.scalefactor in [0,1]) and
  204. (ref.segment=NR_NO) and
  205. (ref.symbol=nil) and
  206. (ref.relsymbol=nil) and
  207. ((base=NR_INVALID) or
  208. (ref.base=base)) and
  209. ((index=NR_INVALID) or
  210. (ref.index=index)) and
  211. (ref.volatility=[]);
  212. end;
  213. function InstrReadsFlags(p: tai): boolean;
  214. begin
  215. InstrReadsFlags := true;
  216. case p.typ of
  217. ait_instruction:
  218. if InsProp[taicpu(p).opcode].Ch*
  219. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  220. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  221. Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc,Ch_All]<>[] then
  222. exit;
  223. ait_label:
  224. exit;
  225. else
  226. ;
  227. end;
  228. InstrReadsFlags := false;
  229. end;
  230. function TX86AsmOptimizer.GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  231. begin
  232. Next:=Current;
  233. repeat
  234. Result:=GetNextInstruction(Next,Next);
  235. until not (Result) or
  236. not(cs_opt_level3 in current_settings.optimizerswitches) or
  237. (Next.typ<>ait_instruction) or
  238. RegInInstruction(reg,Next) or
  239. is_calljmp(taicpu(Next).opcode);
  240. end;
  241. function TX86AsmOptimizer.InstructionLoadsFromReg(const reg: TRegister;const hp: tai): boolean;
  242. begin
  243. Result:=RegReadByInstruction(reg,hp);
  244. end;
  245. function TX86AsmOptimizer.RegReadByInstruction(reg: TRegister; hp: tai): boolean;
  246. var
  247. p: taicpu;
  248. opcount: longint;
  249. begin
  250. RegReadByInstruction := false;
  251. if hp.typ <> ait_instruction then
  252. exit;
  253. p := taicpu(hp);
  254. case p.opcode of
  255. A_CALL:
  256. regreadbyinstruction := true;
  257. A_IMUL:
  258. case p.ops of
  259. 1:
  260. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  261. (
  262. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  263. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  264. );
  265. 2,3:
  266. regReadByInstruction :=
  267. reginop(reg,p.oper[0]^) or
  268. reginop(reg,p.oper[1]^);
  269. end;
  270. A_MUL:
  271. begin
  272. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  273. (
  274. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  275. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  276. );
  277. end;
  278. A_IDIV,A_DIV:
  279. begin
  280. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  281. (
  282. (getregtype(reg)=R_INTREGISTER) and
  283. (
  284. (getsupreg(reg)=RS_EAX) or ((getsupreg(reg)=RS_EDX) and (p.opsize<>S_B))
  285. )
  286. );
  287. end;
  288. else
  289. begin
  290. if (p.opcode=A_LEA) and is_segment_reg(reg) then
  291. begin
  292. RegReadByInstruction := false;
  293. exit;
  294. end;
  295. for opcount := 0 to p.ops-1 do
  296. if (p.oper[opCount]^.typ = top_ref) and
  297. RegInRef(reg,p.oper[opcount]^.ref^) then
  298. begin
  299. RegReadByInstruction := true;
  300. exit
  301. end;
  302. { special handling for SSE MOVSD }
  303. if (p.opcode=A_MOVSD) and (p.ops>0) then
  304. begin
  305. if p.ops<>2 then
  306. internalerror(2017042702);
  307. regReadByInstruction := reginop(reg,p.oper[0]^) or
  308. (
  309. (p.oper[1]^.typ=top_reg) and (p.oper[0]^.typ=top_reg) and reginop(reg, p.oper[1]^)
  310. );
  311. exit;
  312. end;
  313. with insprop[p.opcode] do
  314. begin
  315. if getregtype(reg)=R_INTREGISTER then
  316. begin
  317. case getsupreg(reg) of
  318. RS_EAX:
  319. if [Ch_REAX,Ch_RWEAX,Ch_MEAX]*Ch<>[] then
  320. begin
  321. RegReadByInstruction := true;
  322. exit
  323. end;
  324. RS_ECX:
  325. if [Ch_RECX,Ch_RWECX,Ch_MECX]*Ch<>[] then
  326. begin
  327. RegReadByInstruction := true;
  328. exit
  329. end;
  330. RS_EDX:
  331. if [Ch_REDX,Ch_RWEDX,Ch_MEDX]*Ch<>[] then
  332. begin
  333. RegReadByInstruction := true;
  334. exit
  335. end;
  336. RS_EBX:
  337. if [Ch_REBX,Ch_RWEBX,Ch_MEBX]*Ch<>[] then
  338. begin
  339. RegReadByInstruction := true;
  340. exit
  341. end;
  342. RS_ESP:
  343. if [Ch_RESP,Ch_RWESP,Ch_MESP]*Ch<>[] then
  344. begin
  345. RegReadByInstruction := true;
  346. exit
  347. end;
  348. RS_EBP:
  349. if [Ch_REBP,Ch_RWEBP,Ch_MEBP]*Ch<>[] then
  350. begin
  351. RegReadByInstruction := true;
  352. exit
  353. end;
  354. RS_ESI:
  355. if [Ch_RESI,Ch_RWESI,Ch_MESI]*Ch<>[] then
  356. begin
  357. RegReadByInstruction := true;
  358. exit
  359. end;
  360. RS_EDI:
  361. if [Ch_REDI,Ch_RWEDI,Ch_MEDI]*Ch<>[] then
  362. begin
  363. RegReadByInstruction := true;
  364. exit
  365. end;
  366. end;
  367. end;
  368. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  369. begin
  370. if (Ch_RFLAGScc in Ch) and not(getsubreg(reg) in [R_SUBW,R_SUBD,R_SUBQ]) then
  371. begin
  372. case p.condition of
  373. C_A,C_NBE, { CF=0 and ZF=0 }
  374. C_BE,C_NA: { CF=1 or ZF=1 }
  375. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY,R_SUBFLAGZERO];
  376. C_AE,C_NB,C_NC, { CF=0 }
  377. C_B,C_NAE,C_C: { CF=1 }
  378. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY];
  379. C_NE,C_NZ, { ZF=0 }
  380. C_E,C_Z: { ZF=1 }
  381. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO];
  382. C_G,C_NLE, { ZF=0 and SF=OF }
  383. C_LE,C_NG: { ZF=1 or SF<>OF }
  384. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO,R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  385. C_GE,C_NL, { SF=OF }
  386. C_L,C_NGE: { SF<>OF }
  387. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  388. C_NO, { OF=0 }
  389. C_O: { OF=1 }
  390. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGOVERFLOW];
  391. C_NP,C_PO, { PF=0 }
  392. C_P,C_PE: { PF=1 }
  393. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGPARITY];
  394. C_NS, { SF=0 }
  395. C_S: { SF=1 }
  396. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN];
  397. else
  398. internalerror(2017042701);
  399. end;
  400. if RegReadByInstruction then
  401. exit;
  402. end;
  403. case getsubreg(reg) of
  404. R_SUBW,R_SUBD,R_SUBQ:
  405. RegReadByInstruction :=
  406. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  407. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  408. Ch_RDirFlag,Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc]*Ch<>[];
  409. R_SUBFLAGCARRY:
  410. RegReadByInstruction:=[Ch_RCarryFlag,Ch_RWCarryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  411. R_SUBFLAGPARITY:
  412. RegReadByInstruction:=[Ch_RParityFlag,Ch_RWParityFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  413. R_SUBFLAGAUXILIARY:
  414. RegReadByInstruction:=[Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  415. R_SUBFLAGZERO:
  416. RegReadByInstruction:=[Ch_RZeroFlag,Ch_RWZeroFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  417. R_SUBFLAGSIGN:
  418. RegReadByInstruction:=[Ch_RSignFlag,Ch_RWSignFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  419. R_SUBFLAGOVERFLOW:
  420. RegReadByInstruction:=[Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  421. R_SUBFLAGINTERRUPT:
  422. RegReadByInstruction:=[Ch_RFlags,Ch_RWFlags]*Ch<>[];
  423. R_SUBFLAGDIRECTION:
  424. RegReadByInstruction:=[Ch_RDirFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  425. else
  426. internalerror(2017042601);
  427. end;
  428. exit;
  429. end;
  430. if (Ch_NoReadIfEqualRegs in Ch) and (p.ops=2) and
  431. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  432. (p.oper[0]^.reg=p.oper[1]^.reg) then
  433. exit;
  434. if ([CH_RWOP1,CH_ROP1,CH_MOP1]*Ch<>[]) and reginop(reg,p.oper[0]^) then
  435. begin
  436. RegReadByInstruction := true;
  437. exit
  438. end;
  439. if ([Ch_RWOP2,Ch_ROP2,Ch_MOP2]*Ch<>[]) and reginop(reg,p.oper[1]^) then
  440. begin
  441. RegReadByInstruction := true;
  442. exit
  443. end;
  444. if ([Ch_RWOP3,Ch_ROP3,Ch_MOP3]*Ch<>[]) and reginop(reg,p.oper[2]^) then
  445. begin
  446. RegReadByInstruction := true;
  447. exit
  448. end;
  449. if ([Ch_RWOP4,Ch_ROP4,Ch_MOP4]*Ch<>[]) and reginop(reg,p.oper[3]^) then
  450. begin
  451. RegReadByInstruction := true;
  452. exit
  453. end;
  454. end;
  455. end;
  456. end;
  457. end;
  458. function TX86AsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  459. begin
  460. result:=false;
  461. if p1.typ<>ait_instruction then
  462. exit;
  463. if (Ch_All in insprop[taicpu(p1).opcode].Ch) then
  464. exit(true);
  465. if (getregtype(reg)=R_INTREGISTER) and
  466. { change information for xmm movsd are not correct }
  467. ((taicpu(p1).opcode<>A_MOVSD) or (taicpu(p1).ops=0)) then
  468. begin
  469. case getsupreg(reg) of
  470. { RS_EAX = RS_RAX on x86-64 }
  471. RS_EAX:
  472. result:=([Ch_REAX,Ch_RRAX,Ch_WEAX,Ch_WRAX,Ch_RWEAX,Ch_RWRAX,Ch_MEAX,Ch_MRAX]*insprop[taicpu(p1).opcode].Ch)<>[];
  473. RS_ECX:
  474. result:=([Ch_RECX,Ch_RRCX,Ch_WECX,Ch_WRCX,Ch_RWECX,Ch_RWRCX,Ch_MECX,Ch_MRCX]*insprop[taicpu(p1).opcode].Ch)<>[];
  475. RS_EDX:
  476. result:=([Ch_REDX,Ch_RRDX,Ch_WEDX,Ch_WRDX,Ch_RWEDX,Ch_RWRDX,Ch_MEDX,Ch_MRDX]*insprop[taicpu(p1).opcode].Ch)<>[];
  477. RS_EBX:
  478. result:=([Ch_REBX,Ch_RRBX,Ch_WEBX,Ch_WRBX,Ch_RWEBX,Ch_RWRBX,Ch_MEBX,Ch_MRBX]*insprop[taicpu(p1).opcode].Ch)<>[];
  479. RS_ESP:
  480. result:=([Ch_RESP,Ch_RRSP,Ch_WESP,Ch_WRSP,Ch_RWESP,Ch_RWRSP,Ch_MESP,Ch_MRSP]*insprop[taicpu(p1).opcode].Ch)<>[];
  481. RS_EBP:
  482. result:=([Ch_REBP,Ch_RRBP,Ch_WEBP,Ch_WRBP,Ch_RWEBP,Ch_RWRBP,Ch_MEBP,Ch_MRBP]*insprop[taicpu(p1).opcode].Ch)<>[];
  483. RS_ESI:
  484. result:=([Ch_RESI,Ch_RRSI,Ch_WESI,Ch_WRSI,Ch_RWESI,Ch_RWRSI,Ch_MESI,Ch_MRSI,Ch_RMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  485. RS_EDI:
  486. result:=([Ch_REDI,Ch_RRDI,Ch_WEDI,Ch_WRDI,Ch_RWEDI,Ch_RWRDI,Ch_MEDI,Ch_MRDI,Ch_WMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  487. else
  488. ;
  489. end;
  490. if result then
  491. exit;
  492. end
  493. else if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  494. begin
  495. if ([Ch_RFlags,Ch_WFlags,Ch_RWFlags,Ch_RFLAGScc]*insprop[taicpu(p1).opcode].Ch)<>[] then
  496. exit(true);
  497. case getsubreg(reg) of
  498. R_SUBFLAGCARRY:
  499. Result:=([Ch_RCarryFlag,Ch_RWCarryFlag,Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  500. R_SUBFLAGPARITY:
  501. Result:=([Ch_RParityFlag,Ch_RWParityFlag,Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  502. R_SUBFLAGAUXILIARY:
  503. Result:=([Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  504. R_SUBFLAGZERO:
  505. Result:=([Ch_RZeroFlag,Ch_RWZeroFlag,Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  506. R_SUBFLAGSIGN:
  507. Result:=([Ch_RSignFlag,Ch_RWSignFlag,Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  508. R_SUBFLAGOVERFLOW:
  509. Result:=([Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  510. R_SUBFLAGINTERRUPT:
  511. Result:=([Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  512. R_SUBFLAGDIRECTION:
  513. Result:=([Ch_RDirFlag,Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  514. else
  515. ;
  516. end;
  517. if result then
  518. exit;
  519. end
  520. else if (getregtype(reg)=R_FPUREGISTER) and (Ch_FPU in insprop[taicpu(p1).opcode].Ch) then
  521. exit(true);
  522. Result:=inherited RegInInstruction(Reg, p1);
  523. end;
  524. {$ifdef DEBUG_AOPTCPU}
  525. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);
  526. begin
  527. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  528. end;
  529. function debug_tostr(i: tcgint): string; inline;
  530. begin
  531. Result := tostr(i);
  532. end;
  533. function debug_regname(r: TRegister): string; inline;
  534. begin
  535. Result := '%' + std_regname(r);
  536. end;
  537. { Debug output function - creates a string representation of an operator }
  538. function debug_operstr(oper: TOper): string;
  539. begin
  540. case oper.typ of
  541. top_const:
  542. Result := '$' + debug_tostr(oper.val);
  543. top_reg:
  544. Result := debug_regname(oper.reg);
  545. top_ref:
  546. begin
  547. if oper.ref^.offset <> 0 then
  548. Result := debug_tostr(oper.ref^.offset) + '('
  549. else
  550. Result := '(';
  551. if (oper.ref^.base <> NR_INVALID) and (oper.ref^.base <> NR_NO) then
  552. begin
  553. Result := Result + debug_regname(oper.ref^.base);
  554. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  555. Result := Result + ',' + debug_regname(oper.ref^.index);
  556. end
  557. else
  558. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  559. Result := Result + debug_regname(oper.ref^.index);
  560. if (oper.ref^.scalefactor > 1) then
  561. Result := Result + ',' + debug_tostr(oper.ref^.scalefactor) + ')'
  562. else
  563. Result := Result + ')';
  564. end;
  565. else
  566. Result := '[UNKNOWN]';
  567. end;
  568. end;
  569. function debug_op2str(opcode: tasmop): string; inline;
  570. begin
  571. Result := std_op2str[opcode];
  572. end;
  573. function debug_opsize2str(opsize: topsize): string; inline;
  574. begin
  575. Result := gas_opsize2str[opsize];
  576. end;
  577. {$else DEBUG_AOPTCPU}
  578. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  579. begin
  580. end;
  581. function debug_tostr(i: tcgint): string; inline;
  582. begin
  583. Result := '';
  584. end;
  585. function debug_regname(r: TRegister): string; inline;
  586. begin
  587. Result := '';
  588. end;
  589. function debug_operstr(oper: TOper): string; inline;
  590. begin
  591. Result := '';
  592. end;
  593. function debug_op2str(opcode: tasmop): string; inline;
  594. begin
  595. Result := '';
  596. end;
  597. function debug_opsize2str(opsize: topsize): string; inline;
  598. begin
  599. Result := '';
  600. end;
  601. {$endif DEBUG_AOPTCPU}
  602. function TX86AsmOptimizer.Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  603. begin
  604. if not SuperRegistersEqual(reg1,reg2) then
  605. exit(false);
  606. if getregtype(reg1)<>R_INTREGISTER then
  607. exit(true); {because SuperRegisterEqual is true}
  608. case getsubreg(reg1) of
  609. { A write to R_SUBL doesn't change R_SUBH and if reg2 is R_SUBW or
  610. higher, it preserves the high bits, so the new value depends on
  611. reg2's previous value. In other words, it is equivalent to doing:
  612. reg2 := (reg2 and $ffffff00) or byte(reg1); }
  613. R_SUBL:
  614. exit(getsubreg(reg2)=R_SUBL);
  615. { A write to R_SUBH doesn't change R_SUBL and if reg2 is R_SUBW or
  616. higher, it actually does a:
  617. reg2 := (reg2 and $ffff00ff) or (reg1 and $ff00); }
  618. R_SUBH:
  619. exit(getsubreg(reg2)=R_SUBH);
  620. { If reg2 is R_SUBD or larger, a write to R_SUBW preserves the high 16
  621. bits of reg2:
  622. reg2 := (reg2 and $ffff0000) or word(reg1); }
  623. R_SUBW:
  624. exit(getsubreg(reg2) in [R_SUBL,R_SUBH,R_SUBW]);
  625. { a write to R_SUBD always overwrites every other subregister,
  626. because it clears the high 32 bits of R_SUBQ on x86_64 }
  627. R_SUBD,
  628. R_SUBQ:
  629. exit(true);
  630. else
  631. internalerror(2017042801);
  632. end;
  633. end;
  634. function TX86AsmOptimizer.Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  635. begin
  636. if not SuperRegistersEqual(reg1,reg2) then
  637. exit(false);
  638. if getregtype(reg1)<>R_INTREGISTER then
  639. exit(true); {because SuperRegisterEqual is true}
  640. case getsubreg(reg1) of
  641. R_SUBL:
  642. exit(getsubreg(reg2)<>R_SUBH);
  643. R_SUBH:
  644. exit(getsubreg(reg2)<>R_SUBL);
  645. R_SUBW,
  646. R_SUBD,
  647. R_SUBQ:
  648. exit(true);
  649. else
  650. internalerror(2017042802);
  651. end;
  652. end;
  653. function TX86AsmOptimizer.PrePeepholeOptSxx(var p : tai) : boolean;
  654. var
  655. hp1 : tai;
  656. l : TCGInt;
  657. begin
  658. result:=false;
  659. { changes the code sequence
  660. shr/sar const1, x
  661. shl const2, x
  662. to
  663. either "sar/and", "shl/and" or just "and" depending on const1 and const2 }
  664. if GetNextInstruction(p, hp1) and
  665. MatchInstruction(hp1,A_SHL,[]) and
  666. (taicpu(p).oper[0]^.typ = top_const) and
  667. (taicpu(hp1).oper[0]^.typ = top_const) and
  668. (taicpu(hp1).opsize = taicpu(p).opsize) and
  669. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[1]^.typ) and
  670. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) then
  671. begin
  672. if (taicpu(p).oper[0]^.val > taicpu(hp1).oper[0]^.val) and
  673. not(cs_opt_size in current_settings.optimizerswitches) then
  674. begin
  675. { shr/sar const1, %reg
  676. shl const2, %reg
  677. with const1 > const2 }
  678. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  679. taicpu(hp1).opcode := A_AND;
  680. l := (1 shl (taicpu(hp1).oper[0]^.val)) - 1;
  681. case taicpu(p).opsize Of
  682. S_B: taicpu(hp1).loadConst(0,l Xor $ff);
  683. S_W: taicpu(hp1).loadConst(0,l Xor $ffff);
  684. S_L: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffff));
  685. S_Q: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffffffffffff));
  686. else
  687. Internalerror(2017050703)
  688. end;
  689. end
  690. else if (taicpu(p).oper[0]^.val<taicpu(hp1).oper[0]^.val) and
  691. not(cs_opt_size in current_settings.optimizerswitches) then
  692. begin
  693. { shr/sar const1, %reg
  694. shl const2, %reg
  695. with const1 < const2 }
  696. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val-taicpu(p).oper[0]^.val);
  697. taicpu(p).opcode := A_AND;
  698. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  699. case taicpu(p).opsize Of
  700. S_B: taicpu(p).loadConst(0,l Xor $ff);
  701. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  702. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  703. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  704. else
  705. Internalerror(2017050702)
  706. end;
  707. end
  708. else if (taicpu(p).oper[0]^.val = taicpu(hp1).oper[0]^.val) then
  709. begin
  710. { shr/sar const1, %reg
  711. shl const2, %reg
  712. with const1 = const2 }
  713. taicpu(p).opcode := A_AND;
  714. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  715. case taicpu(p).opsize Of
  716. S_B: taicpu(p).loadConst(0,l Xor $ff);
  717. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  718. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  719. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  720. else
  721. Internalerror(2017050701)
  722. end;
  723. asml.remove(hp1);
  724. hp1.free;
  725. end;
  726. end;
  727. end;
  728. function TX86AsmOptimizer.PrePeepholeOptIMUL(var p : tai) : boolean;
  729. var
  730. opsize : topsize;
  731. hp1 : tai;
  732. tmpref : treference;
  733. ShiftValue : Cardinal;
  734. BaseValue : TCGInt;
  735. begin
  736. result:=false;
  737. opsize:=taicpu(p).opsize;
  738. { changes certain "imul const, %reg"'s to lea sequences }
  739. if (MatchOpType(taicpu(p),top_const,top_reg) or
  740. MatchOpType(taicpu(p),top_const,top_reg,top_reg)) and
  741. (opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) then
  742. if (taicpu(p).oper[0]^.val = 1) then
  743. if (taicpu(p).ops = 2) then
  744. { remove "imul $1, reg" }
  745. begin
  746. hp1 := tai(p.Next);
  747. asml.remove(p);
  748. DebugMsg(SPeepholeOptimization + 'Imul2Nop done',p);
  749. p.free;
  750. p := hp1;
  751. result:=true;
  752. end
  753. else
  754. { change "imul $1, reg1, reg2" to "mov reg1, reg2" }
  755. begin
  756. hp1 := taicpu.Op_Reg_Reg(A_MOV, opsize, taicpu(p).oper[1]^.reg,taicpu(p).oper[2]^.reg);
  757. InsertLLItem(p.previous, p.next, hp1);
  758. DebugMsg(SPeepholeOptimization + 'Imul2Mov done',p);
  759. p.free;
  760. p := hp1;
  761. end
  762. else if
  763. ((taicpu(p).ops <= 2) or
  764. (taicpu(p).oper[2]^.typ = Top_Reg)) and
  765. not(cs_opt_size in current_settings.optimizerswitches) and
  766. (not(GetNextInstruction(p, hp1)) or
  767. not((tai(hp1).typ = ait_instruction) and
  768. ((taicpu(hp1).opcode=A_Jcc) and
  769. (taicpu(hp1).condition in [C_O,C_NO])))) then
  770. begin
  771. {
  772. imul X, reg1, reg2 to
  773. lea (reg1,reg1,Y), reg2
  774. shl ZZ,reg2
  775. imul XX, reg1 to
  776. lea (reg1,reg1,YY), reg1
  777. shl ZZ,reg2
  778. This optimziation makes sense for pretty much every x86, except the VIA Nano3000: it has IMUL latency 2, lea/shl pair as well,
  779. it does not exist as a separate optimization target in FPC though.
  780. This optimziation can be applied as long as only two bits are set in the constant and those two bits are separated by
  781. at most two zeros
  782. }
  783. reference_reset(tmpref,1,[]);
  784. if (PopCnt(QWord(taicpu(p).oper[0]^.val))=2) and (BsrQWord(taicpu(p).oper[0]^.val)-BsfQWord(taicpu(p).oper[0]^.val)<=3) then
  785. begin
  786. ShiftValue:=BsfQWord(taicpu(p).oper[0]^.val);
  787. BaseValue:=taicpu(p).oper[0]^.val shr ShiftValue;
  788. TmpRef.base := taicpu(p).oper[1]^.reg;
  789. TmpRef.index := taicpu(p).oper[1]^.reg;
  790. if not(BaseValue in [3,5,9]) then
  791. Internalerror(2018110101);
  792. TmpRef.ScaleFactor := BaseValue-1;
  793. if (taicpu(p).ops = 2) then
  794. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[1]^.reg)
  795. else
  796. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[2]^.reg);
  797. AsmL.InsertAfter(hp1,p);
  798. DebugMsg(SPeepholeOptimization + 'Imul2LeaShl done',p);
  799. AsmL.Remove(p);
  800. taicpu(hp1).fileinfo:=taicpu(p).fileinfo;
  801. p.free;
  802. p := hp1;
  803. if ShiftValue>0 then
  804. AsmL.InsertAfter(taicpu.op_const_reg(A_SHL, opsize, ShiftValue, taicpu(hp1).oper[1]^.reg),hp1);
  805. end;
  806. end;
  807. end;
  808. function TX86AsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  809. var
  810. p: taicpu;
  811. begin
  812. if not assigned(hp) or
  813. (hp.typ <> ait_instruction) then
  814. begin
  815. Result := false;
  816. exit;
  817. end;
  818. p := taicpu(hp);
  819. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  820. with insprop[p.opcode] do
  821. begin
  822. case getsubreg(reg) of
  823. R_SUBW,R_SUBD,R_SUBQ:
  824. Result:=
  825. RegLoadedWithNewValue(NR_CARRYFLAG,hp) and
  826. RegLoadedWithNewValue(NR_PARITYFLAG,hp) and
  827. RegLoadedWithNewValue(NR_AUXILIARYFLAG,hp) and
  828. RegLoadedWithNewValue(NR_ZEROFLAG,hp) and
  829. RegLoadedWithNewValue(NR_SIGNFLAG,hp) and
  830. RegLoadedWithNewValue(NR_OVERFLOWFLAG,hp);
  831. R_SUBFLAGCARRY:
  832. Result:=[Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag,Ch_WFlags]*Ch<>[];
  833. R_SUBFLAGPARITY:
  834. Result:=[Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag,Ch_WFlags]*Ch<>[];
  835. R_SUBFLAGAUXILIARY:
  836. Result:=[Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag,Ch_WFlags]*Ch<>[];
  837. R_SUBFLAGZERO:
  838. Result:=[Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag,Ch_WFlags]*Ch<>[];
  839. R_SUBFLAGSIGN:
  840. Result:=[Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag,Ch_WFlags]*Ch<>[];
  841. R_SUBFLAGOVERFLOW:
  842. Result:=[Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag,Ch_WFlags]*Ch<>[];
  843. R_SUBFLAGINTERRUPT:
  844. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*Ch<>[];
  845. R_SUBFLAGDIRECTION:
  846. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*Ch<>[];
  847. else
  848. begin
  849. writeln(getsubreg(reg));
  850. internalerror(2017050501);
  851. end;
  852. end;
  853. exit;
  854. end;
  855. Result :=
  856. (((p.opcode = A_MOV) or
  857. (p.opcode = A_MOVZX) or
  858. (p.opcode = A_MOVSX) or
  859. (p.opcode = A_LEA) or
  860. (p.opcode = A_VMOVSS) or
  861. (p.opcode = A_VMOVSD) or
  862. (p.opcode = A_VMOVAPD) or
  863. (p.opcode = A_VMOVAPS) or
  864. (p.opcode = A_VMOVQ) or
  865. (p.opcode = A_MOVSS) or
  866. (p.opcode = A_MOVSD) or
  867. (p.opcode = A_MOVQ) or
  868. (p.opcode = A_MOVAPD) or
  869. (p.opcode = A_MOVAPS) or
  870. {$ifndef x86_64}
  871. (p.opcode = A_LDS) or
  872. (p.opcode = A_LES) or
  873. {$endif not x86_64}
  874. (p.opcode = A_LFS) or
  875. (p.opcode = A_LGS) or
  876. (p.opcode = A_LSS)) and
  877. (p.ops=2) and { A_MOVSD can have zero operands, so this check is needed }
  878. (p.oper[1]^.typ = top_reg) and
  879. (Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg)) and
  880. ((p.oper[0]^.typ = top_const) or
  881. ((p.oper[0]^.typ = top_reg) and
  882. not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  883. ((p.oper[0]^.typ = top_ref) and
  884. not RegInRef(reg,p.oper[0]^.ref^)))) or
  885. ((p.opcode = A_POP) and
  886. (Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg))) or
  887. ((p.opcode = A_IMUL) and
  888. (p.ops=3) and
  889. (Reg1WriteOverwritesReg2Entirely(p.oper[2]^.reg,reg)) and
  890. (((p.oper[1]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg))) or
  891. ((p.oper[1]^.typ=top_ref) and not(RegInRef(reg,p.oper[1]^.ref^))))) or
  892. ((((p.opcode = A_IMUL) or
  893. (p.opcode = A_MUL)) and
  894. (p.ops=1)) and
  895. (((p.oper[0]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  896. ((p.oper[0]^.typ=top_ref) and not(RegInRef(reg,p.oper[0]^.ref^)))) and
  897. (((p.opsize=S_B) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  898. ((p.opsize=S_W) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  899. ((p.opsize=S_L) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg))
  900. {$ifdef x86_64}
  901. or ((p.opsize=S_Q) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg))
  902. {$endif x86_64}
  903. )) or
  904. ((p.opcode = A_CWD) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  905. ((p.opcode = A_CDQ) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg)) or
  906. {$ifdef x86_64}
  907. ((p.opcode = A_CQO) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg)) or
  908. {$endif x86_64}
  909. ((p.opcode = A_CBW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  910. {$ifndef x86_64}
  911. ((p.opcode = A_LDS) and (reg=NR_DS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  912. ((p.opcode = A_LES) and (reg=NR_ES) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  913. {$endif not x86_64}
  914. ((p.opcode = A_LFS) and (reg=NR_FS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  915. ((p.opcode = A_LGS) and (reg=NR_GS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  916. ((p.opcode = A_LSS) and (reg=NR_SS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  917. {$ifndef x86_64}
  918. ((p.opcode = A_AAM) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  919. {$endif not x86_64}
  920. ((p.opcode = A_LAHF) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  921. ((p.opcode = A_LODSB) and Reg1WriteOverwritesReg2Entirely(NR_AL,reg)) or
  922. ((p.opcode = A_LODSW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg)) or
  923. ((p.opcode = A_LODSD) and Reg1WriteOverwritesReg2Entirely(NR_EAX,reg)) or
  924. {$ifdef x86_64}
  925. ((p.opcode = A_LODSQ) and Reg1WriteOverwritesReg2Entirely(NR_RAX,reg)) or
  926. {$endif x86_64}
  927. ((p.opcode = A_SETcc) and (p.oper[0]^.typ=top_reg) and Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  928. (((p.opcode = A_FSTSW) or
  929. (p.opcode = A_FNSTSW)) and
  930. (p.oper[0]^.typ=top_reg) and
  931. Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  932. (((p.opcode = A_XOR) or (p.opcode = A_SUB) or (p.opcode = A_SBB)) and
  933. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  934. (p.oper[0]^.reg=p.oper[1]^.reg) and
  935. Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg));
  936. end;
  937. class function TX86AsmOptimizer.IsExitCode(p : tai) : boolean;
  938. var
  939. hp2,hp3 : tai;
  940. begin
  941. { some x86-64 issue a NOP before the real exit code }
  942. if MatchInstruction(p,A_NOP,[]) then
  943. GetNextInstruction(p,p);
  944. result:=assigned(p) and (p.typ=ait_instruction) and
  945. ((taicpu(p).opcode = A_RET) or
  946. ((taicpu(p).opcode=A_LEAVE) and
  947. GetNextInstruction(p,hp2) and
  948. MatchInstruction(hp2,A_RET,[S_NO])
  949. ) or
  950. (((taicpu(p).opcode=A_LEA) and
  951. MatchOpType(taicpu(p),top_ref,top_reg) and
  952. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  953. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  954. ) and
  955. GetNextInstruction(p,hp2) and
  956. MatchInstruction(hp2,A_RET,[S_NO])
  957. ) or
  958. ((((taicpu(p).opcode=A_MOV) and
  959. MatchOpType(taicpu(p),top_reg,top_reg) and
  960. (taicpu(p).oper[0]^.reg=current_procinfo.framepointer) and
  961. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)) or
  962. ((taicpu(p).opcode=A_LEA) and
  963. MatchOpType(taicpu(p),top_ref,top_reg) and
  964. (taicpu(p).oper[0]^.ref^.base=current_procinfo.framepointer) and
  965. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  966. )
  967. ) and
  968. GetNextInstruction(p,hp2) and
  969. MatchInstruction(hp2,A_POP,[reg2opsize(current_procinfo.framepointer)]) and
  970. MatchOpType(taicpu(hp2),top_reg) and
  971. (taicpu(hp2).oper[0]^.reg=current_procinfo.framepointer) and
  972. GetNextInstruction(hp2,hp3) and
  973. MatchInstruction(hp3,A_RET,[S_NO])
  974. )
  975. );
  976. end;
  977. class function TX86AsmOptimizer.isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  978. begin
  979. isFoldableArithOp := False;
  980. case hp1.opcode of
  981. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  982. isFoldableArithOp :=
  983. ((taicpu(hp1).oper[0]^.typ = top_const) or
  984. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  985. (taicpu(hp1).oper[0]^.reg <> reg))) and
  986. (taicpu(hp1).oper[1]^.typ = top_reg) and
  987. (taicpu(hp1).oper[1]^.reg = reg);
  988. A_INC,A_DEC,A_NEG,A_NOT:
  989. isFoldableArithOp :=
  990. (taicpu(hp1).oper[0]^.typ = top_reg) and
  991. (taicpu(hp1).oper[0]^.reg = reg);
  992. else
  993. ;
  994. end;
  995. end;
  996. procedure TX86AsmOptimizer.RemoveLastDeallocForFuncRes(p: tai);
  997. procedure DoRemoveLastDeallocForFuncRes( supreg: tsuperregister);
  998. var
  999. hp2: tai;
  1000. begin
  1001. hp2 := p;
  1002. repeat
  1003. hp2 := tai(hp2.previous);
  1004. if assigned(hp2) and
  1005. (hp2.typ = ait_regalloc) and
  1006. (tai_regalloc(hp2).ratype=ra_dealloc) and
  1007. (getregtype(tai_regalloc(hp2).reg) = R_INTREGISTER) and
  1008. (getsupreg(tai_regalloc(hp2).reg) = supreg) then
  1009. begin
  1010. asml.remove(hp2);
  1011. hp2.free;
  1012. break;
  1013. end;
  1014. until not(assigned(hp2)) or regInInstruction(newreg(R_INTREGISTER,supreg,R_SUBWHOLE),hp2);
  1015. end;
  1016. begin
  1017. case current_procinfo.procdef.returndef.typ of
  1018. arraydef,recorddef,pointerdef,
  1019. stringdef,enumdef,procdef,objectdef,errordef,
  1020. filedef,setdef,procvardef,
  1021. classrefdef,forwarddef:
  1022. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1023. orddef:
  1024. if current_procinfo.procdef.returndef.size <> 0 then
  1025. begin
  1026. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1027. { for int64/qword }
  1028. if current_procinfo.procdef.returndef.size = 8 then
  1029. DoRemoveLastDeallocForFuncRes(RS_EDX);
  1030. end;
  1031. else
  1032. ;
  1033. end;
  1034. end;
  1035. function TX86AsmOptimizer.OptPass1_V_MOVAP(var p : tai) : boolean;
  1036. var
  1037. hp1,hp2 : tai;
  1038. begin
  1039. result:=false;
  1040. if MatchOpType(taicpu(p),top_reg,top_reg) then
  1041. begin
  1042. { vmova* reg1,reg1
  1043. =>
  1044. <nop> }
  1045. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  1046. begin
  1047. GetNextInstruction(p,hp1);
  1048. asml.Remove(p);
  1049. p.Free;
  1050. p:=hp1;
  1051. result:=true;
  1052. end
  1053. else if GetNextInstruction(p,hp1) then
  1054. begin
  1055. if MatchInstruction(hp1,[taicpu(p).opcode],[S_NO]) and
  1056. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  1057. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1058. begin
  1059. { vmova* reg1,reg2
  1060. vmova* reg2,reg3
  1061. dealloc reg2
  1062. =>
  1063. vmova* reg1,reg3 }
  1064. TransferUsedRegs(TmpUsedRegs);
  1065. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1066. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1067. begin
  1068. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1069. asml.Remove(hp1);
  1070. hp1.Free;
  1071. result:=true;
  1072. end
  1073. { special case:
  1074. vmova* reg1,reg2
  1075. vmova* reg2,reg1
  1076. =>
  1077. vmova* reg1,reg2 }
  1078. else if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) then
  1079. begin
  1080. asml.Remove(hp1);
  1081. hp1.Free;
  1082. result:=true;
  1083. end
  1084. end
  1085. else if MatchInstruction(hp1,[A_VFMADDPD,
  1086. A_VFMADD132PD,
  1087. A_VFMADD132PS,
  1088. A_VFMADD132SD,
  1089. A_VFMADD132SS,
  1090. A_VFMADD213PD,
  1091. A_VFMADD213PS,
  1092. A_VFMADD213SD,
  1093. A_VFMADD213SS,
  1094. A_VFMADD231PD,
  1095. A_VFMADD231PS,
  1096. A_VFMADD231SD,
  1097. A_VFMADD231SS,
  1098. A_VFMADDSUB132PD,
  1099. A_VFMADDSUB132PS,
  1100. A_VFMADDSUB213PD,
  1101. A_VFMADDSUB213PS,
  1102. A_VFMADDSUB231PD,
  1103. A_VFMADDSUB231PS,
  1104. A_VFMSUB132PD,
  1105. A_VFMSUB132PS,
  1106. A_VFMSUB132SD,
  1107. A_VFMSUB132SS,
  1108. A_VFMSUB213PD,
  1109. A_VFMSUB213PS,
  1110. A_VFMSUB213SD,
  1111. A_VFMSUB213SS,
  1112. A_VFMSUB231PD,
  1113. A_VFMSUB231PS,
  1114. A_VFMSUB231SD,
  1115. A_VFMSUB231SS,
  1116. A_VFMSUBADD132PD,
  1117. A_VFMSUBADD132PS,
  1118. A_VFMSUBADD213PD,
  1119. A_VFMSUBADD213PS,
  1120. A_VFMSUBADD231PD,
  1121. A_VFMSUBADD231PS,
  1122. A_VFNMADD132PD,
  1123. A_VFNMADD132PS,
  1124. A_VFNMADD132SD,
  1125. A_VFNMADD132SS,
  1126. A_VFNMADD213PD,
  1127. A_VFNMADD213PS,
  1128. A_VFNMADD213SD,
  1129. A_VFNMADD213SS,
  1130. A_VFNMADD231PD,
  1131. A_VFNMADD231PS,
  1132. A_VFNMADD231SD,
  1133. A_VFNMADD231SS,
  1134. A_VFNMSUB132PD,
  1135. A_VFNMSUB132PS,
  1136. A_VFNMSUB132SD,
  1137. A_VFNMSUB132SS,
  1138. A_VFNMSUB213PD,
  1139. A_VFNMSUB213PS,
  1140. A_VFNMSUB213SD,
  1141. A_VFNMSUB213SS,
  1142. A_VFNMSUB231PD,
  1143. A_VFNMSUB231PS,
  1144. A_VFNMSUB231SD,
  1145. A_VFNMSUB231SS],[S_NO]) and
  1146. { we mix single and double opperations here because we assume that the compiler
  1147. generates vmovapd only after double operations and vmovaps only after single operations }
  1148. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[2]^) and
  1149. GetNextInstruction(hp1,hp2) and
  1150. MatchInstruction(hp2,[A_VMOVAPD,A_VMOVAPS,A_MOVAPD,A_MOVAPS],[S_NO]) and
  1151. MatchOperand(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) then
  1152. begin
  1153. TransferUsedRegs(TmpUsedRegs);
  1154. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1155. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1156. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs))
  1157. then
  1158. begin
  1159. taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
  1160. asml.Remove(p);
  1161. p.Free;
  1162. asml.Remove(hp2);
  1163. hp2.Free;
  1164. p:=hp1;
  1165. end;
  1166. end
  1167. else if (hp1.typ = ait_instruction) and
  1168. GetNextInstruction(hp1, hp2) and
  1169. MatchInstruction(hp2,taicpu(p).opcode,[]) and
  1170. OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  1171. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  1172. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  1173. (((taicpu(p).opcode=A_MOVAPS) and
  1174. ((taicpu(hp1).opcode=A_ADDSS) or (taicpu(hp1).opcode=A_SUBSS) or
  1175. (taicpu(hp1).opcode=A_MULSS) or (taicpu(hp1).opcode=A_DIVSS))) or
  1176. ((taicpu(p).opcode=A_MOVAPD) and
  1177. ((taicpu(hp1).opcode=A_ADDSD) or (taicpu(hp1).opcode=A_SUBSD) or
  1178. (taicpu(hp1).opcode=A_MULSD) or (taicpu(hp1).opcode=A_DIVSD)))
  1179. ) then
  1180. { change
  1181. movapX reg,reg2
  1182. addsX/subsX/... reg3, reg2
  1183. movapX reg2,reg
  1184. to
  1185. addsX/subsX/... reg3,reg
  1186. }
  1187. begin
  1188. TransferUsedRegs(TmpUsedRegs);
  1189. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1190. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1191. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1192. begin
  1193. DebugMsg(SPeepholeOptimization + 'MovapXOpMovapX2Op ('+
  1194. debug_op2str(taicpu(p).opcode)+' '+
  1195. debug_op2str(taicpu(hp1).opcode)+' '+
  1196. debug_op2str(taicpu(hp2).opcode)+') done',p);
  1197. { we cannot eliminate the first move if
  1198. the operations uses the same register for source and dest }
  1199. if not(OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp1).oper[0]^)) then
  1200. begin
  1201. asml.remove(p);
  1202. p.Free;
  1203. end;
  1204. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1205. asml.remove(hp2);
  1206. hp2.Free;
  1207. p:=hp1;
  1208. result:=true;
  1209. end;
  1210. end;
  1211. end;
  1212. end;
  1213. end;
  1214. function TX86AsmOptimizer.OptPass1VOP(var p : tai) : boolean;
  1215. var
  1216. hp1 : tai;
  1217. begin
  1218. result:=false;
  1219. { replace
  1220. V<Op>X %mreg1,%mreg2,%mreg3
  1221. VMovX %mreg3,%mreg4
  1222. dealloc %mreg3
  1223. by
  1224. V<Op>X %mreg1,%mreg2,%mreg4
  1225. ?
  1226. }
  1227. if GetNextInstruction(p,hp1) and
  1228. { we mix single and double operations here because we assume that the compiler
  1229. generates vmovapd only after double operations and vmovaps only after single operations }
  1230. MatchInstruction(hp1,A_VMOVAPD,A_VMOVAPS,[S_NO]) and
  1231. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  1232. (taicpu(hp1).oper[1]^.typ=top_reg) then
  1233. begin
  1234. TransferUsedRegs(TmpUsedRegs);
  1235. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1236. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)
  1237. ) then
  1238. begin
  1239. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  1240. DebugMsg(SPeepholeOptimization + 'VOpVmov2VOp done',p);
  1241. asml.Remove(hp1);
  1242. hp1.Free;
  1243. result:=true;
  1244. end;
  1245. end;
  1246. end;
  1247. function TX86AsmOptimizer.OptPass1MOV(var p : tai) : boolean;
  1248. var
  1249. hp1, hp2: tai;
  1250. GetNextInstruction_p: Boolean;
  1251. PreMessage, RegName1, RegName2, InputVal, MaskNum: string;
  1252. NewSize: topsize;
  1253. begin
  1254. Result:=false;
  1255. GetNextInstruction_p:=GetNextInstruction(p, hp1);
  1256. { remove mov reg1,reg1? }
  1257. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^)
  1258. then
  1259. begin
  1260. DebugMsg(SPeepholeOptimization + 'Mov2Nop done',p);
  1261. { take care of the register (de)allocs following p }
  1262. UpdateUsedRegs(tai(p.next));
  1263. asml.remove(p);
  1264. p.free;
  1265. p:=hp1;
  1266. Result:=true;
  1267. exit;
  1268. end;
  1269. if GetNextInstruction_p and
  1270. MatchInstruction(hp1,A_AND,[]) and
  1271. (taicpu(p).oper[1]^.typ = top_reg) and
  1272. MatchOpType(taicpu(hp1),top_const,top_reg) then
  1273. begin
  1274. if MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) then
  1275. begin
  1276. case taicpu(p).opsize of
  1277. S_L:
  1278. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  1279. begin
  1280. { Optimize out:
  1281. mov x, %reg
  1282. and ffffffffh, %reg
  1283. }
  1284. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 1 done',p);
  1285. asml.remove(hp1);
  1286. hp1.free;
  1287. Result:=true;
  1288. exit;
  1289. end;
  1290. S_Q: { TODO: Confirm if this is even possible }
  1291. if (taicpu(hp1).oper[0]^.val = $ffffffffffffffff) then
  1292. begin
  1293. { Optimize out:
  1294. mov x, %reg
  1295. and ffffffffffffffffh, %reg
  1296. }
  1297. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 2 done',p);
  1298. asml.remove(hp1);
  1299. hp1.free;
  1300. Result:=true;
  1301. exit;
  1302. end;
  1303. else
  1304. ;
  1305. end;
  1306. end
  1307. else if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(hp1).oper[1]^.typ = top_reg) and
  1308. (taicpu(p).oper[0]^.typ <> top_const) and { MOVZX only supports registers and memory, not immediates (use MOV for that!) }
  1309. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  1310. then
  1311. begin
  1312. InputVal := debug_operstr(taicpu(p).oper[0]^);
  1313. MaskNum := debug_tostr(taicpu(hp1).oper[0]^.val);
  1314. case taicpu(p).opsize of
  1315. S_B:
  1316. if (taicpu(hp1).oper[0]^.val = $ff) then
  1317. begin
  1318. { Convert:
  1319. movb x, %regl movb x, %regl
  1320. andw ffh, %regw andl ffh, %regd
  1321. To:
  1322. movzbw x, %regd movzbl x, %regd
  1323. (Identical registers, just different sizes)
  1324. }
  1325. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 8-bit register name }
  1326. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 16/32-bit register name }
  1327. case taicpu(hp1).opsize of
  1328. S_W: NewSize := S_BW;
  1329. S_L: NewSize := S_BL;
  1330. {$ifdef x86_64}
  1331. S_Q: NewSize := S_BQ;
  1332. {$endif x86_64}
  1333. else
  1334. InternalError(2018011510);
  1335. end;
  1336. end
  1337. else
  1338. NewSize := S_NO;
  1339. S_W:
  1340. if (taicpu(hp1).oper[0]^.val = $ffff) then
  1341. begin
  1342. { Convert:
  1343. movw x, %regw
  1344. andl ffffh, %regd
  1345. To:
  1346. movzwl x, %regd
  1347. (Identical registers, just different sizes)
  1348. }
  1349. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 16-bit register name }
  1350. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 32-bit register name }
  1351. case taicpu(hp1).opsize of
  1352. S_L: NewSize := S_WL;
  1353. {$ifdef x86_64}
  1354. S_Q: NewSize := S_WQ;
  1355. {$endif x86_64}
  1356. else
  1357. InternalError(2018011511);
  1358. end;
  1359. end
  1360. else
  1361. NewSize := S_NO;
  1362. else
  1363. NewSize := S_NO;
  1364. end;
  1365. if NewSize <> S_NO then
  1366. begin
  1367. PreMessage := 'mov' + debug_opsize2str(taicpu(p).opsize) + ' ' + InputVal + ',' + RegName1;
  1368. { The actual optimization }
  1369. taicpu(p).opcode := A_MOVZX;
  1370. taicpu(p).changeopsize(NewSize);
  1371. taicpu(p).oper[1]^ := taicpu(hp1).oper[1]^;
  1372. { Safeguard if "and" is followed by a conditional command }
  1373. TransferUsedRegs(TmpUsedRegs);
  1374. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  1375. if (RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  1376. begin
  1377. { At this point, the "and" command is effectively equivalent to
  1378. "test %reg,%reg". This will be handled separately by the
  1379. Peephole Optimizer. [Kit] }
  1380. DebugMsg(SPeepholeOptimization + PreMessage +
  1381. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  1382. end
  1383. else
  1384. begin
  1385. DebugMsg(SPeepholeOptimization + PreMessage + '; and' + debug_opsize2str(taicpu(hp1).opsize) + ' $' + MaskNum + ',' + RegName2 +
  1386. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  1387. asml.Remove(hp1);
  1388. hp1.Free;
  1389. end;
  1390. Result := True;
  1391. Exit;
  1392. end;
  1393. end;
  1394. end;
  1395. { Next instruction is also a MOV ? }
  1396. if GetNextInstruction_p and
  1397. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) then
  1398. begin
  1399. if (taicpu(p).oper[1]^.typ = top_reg) and
  1400. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1401. begin
  1402. TransferUsedRegs(TmpUsedRegs);
  1403. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  1404. { we have
  1405. mov x, %treg
  1406. mov %treg, y
  1407. }
  1408. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^)) and
  1409. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  1410. { we've got
  1411. mov x, %treg
  1412. mov %treg, y
  1413. with %treg is not used after }
  1414. case taicpu(p).oper[0]^.typ Of
  1415. top_reg:
  1416. begin
  1417. { change
  1418. mov %reg, %treg
  1419. mov %treg, y
  1420. to
  1421. mov %reg, y
  1422. }
  1423. if taicpu(hp1).oper[1]^.typ=top_reg then
  1424. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1425. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  1426. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 2 done',p);
  1427. asml.remove(hp1);
  1428. hp1.free;
  1429. Result:=true;
  1430. Exit;
  1431. end;
  1432. top_const:
  1433. begin
  1434. { change
  1435. mov const, %treg
  1436. mov %treg, y
  1437. to
  1438. mov const, y
  1439. }
  1440. if (taicpu(hp1).oper[1]^.typ=top_reg) or
  1441. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  1442. begin
  1443. if taicpu(hp1).oper[1]^.typ=top_reg then
  1444. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1445. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  1446. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 5 done',p);
  1447. asml.remove(hp1);
  1448. hp1.free;
  1449. Result:=true;
  1450. Exit;
  1451. end;
  1452. end;
  1453. top_ref:
  1454. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  1455. begin
  1456. { change
  1457. mov mem, %treg
  1458. mov %treg, %reg
  1459. to
  1460. mov mem, %reg"
  1461. }
  1462. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1463. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3 done',p);
  1464. asml.remove(hp1);
  1465. hp1.free;
  1466. Result:=true;
  1467. Exit;
  1468. end;
  1469. else
  1470. ;
  1471. end;
  1472. end;
  1473. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  1474. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  1475. { mov reg1, mem1 or mov mem1, reg1
  1476. mov mem2, reg2 mov reg2, mem2}
  1477. begin
  1478. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  1479. { mov reg1, mem1 or mov mem1, reg1
  1480. mov mem2, reg1 mov reg2, mem1}
  1481. begin
  1482. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  1483. { Removes the second statement from
  1484. mov reg1, mem1/reg2
  1485. mov mem1/reg2, reg1 }
  1486. begin
  1487. if taicpu(p).oper[0]^.typ=top_reg then
  1488. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1489. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 1',p);
  1490. asml.remove(hp1);
  1491. hp1.free;
  1492. Result:=true;
  1493. exit;
  1494. end
  1495. else
  1496. begin
  1497. TransferUsedRegs(TmpUsedRegs);
  1498. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1499. if (taicpu(p).oper[1]^.typ = top_ref) and
  1500. { mov reg1, mem1
  1501. mov mem2, reg1 }
  1502. (taicpu(hp1).oper[0]^.ref^.refaddr = addr_no) and
  1503. GetNextInstruction(hp1, hp2) and
  1504. MatchInstruction(hp2,A_CMP,[taicpu(p).opsize]) and
  1505. OpsEqual(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  1506. OpsEqual(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) and
  1507. not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs)) then
  1508. { change to
  1509. mov reg1, mem1 mov reg1, mem1
  1510. mov mem2, reg1 cmp reg1, mem2
  1511. cmp mem1, reg1
  1512. }
  1513. begin
  1514. asml.remove(hp2);
  1515. hp2.free;
  1516. taicpu(hp1).opcode := A_CMP;
  1517. taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
  1518. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  1519. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  1520. DebugMsg(SPeepholeOptimization + 'MovMovCmp2MovCmp done',hp1);
  1521. end;
  1522. end;
  1523. end
  1524. else if (taicpu(p).oper[1]^.typ=top_ref) and
  1525. OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  1526. begin
  1527. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  1528. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  1529. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov1 done',p);
  1530. end
  1531. else
  1532. begin
  1533. TransferUsedRegs(TmpUsedRegs);
  1534. if GetNextInstruction(hp1, hp2) and
  1535. MatchOpType(taicpu(p),top_ref,top_reg) and
  1536. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  1537. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1538. MatchInstruction(hp2,A_MOV,[taicpu(p).opsize]) and
  1539. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  1540. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  1541. if not RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^) and
  1542. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,tmpUsedRegs)) then
  1543. { mov mem1, %reg1
  1544. mov %reg1, mem2
  1545. mov mem2, reg2
  1546. to:
  1547. mov mem1, reg2
  1548. mov reg2, mem2}
  1549. begin
  1550. AllocRegBetween(taicpu(hp2).oper[1]^.reg,p,hp2,usedregs);
  1551. DebugMsg(SPeepholeOptimization + 'MovMovMov2MovMov 1 done',p);
  1552. taicpu(p).loadoper(1,taicpu(hp2).oper[1]^);
  1553. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  1554. asml.remove(hp2);
  1555. hp2.free;
  1556. end
  1557. {$ifdef i386}
  1558. { this is enabled for i386 only, as the rules to create the reg sets below
  1559. are too complicated for x86-64, so this makes this code too error prone
  1560. on x86-64
  1561. }
  1562. else if (taicpu(p).oper[1]^.reg <> taicpu(hp2).oper[1]^.reg) and
  1563. not(RegInRef(taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.ref^)) and
  1564. not(RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^)) then
  1565. { mov mem1, reg1 mov mem1, reg1
  1566. mov reg1, mem2 mov reg1, mem2
  1567. mov mem2, reg2 mov mem2, reg1
  1568. to: to:
  1569. mov mem1, reg1 mov mem1, reg1
  1570. mov mem1, reg2 mov reg1, mem2
  1571. mov reg1, mem2
  1572. or (if mem1 depends on reg1
  1573. and/or if mem2 depends on reg2)
  1574. to:
  1575. mov mem1, reg1
  1576. mov reg1, mem2
  1577. mov reg1, reg2
  1578. }
  1579. begin
  1580. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  1581. taicpu(hp1).loadReg(1,taicpu(hp2).oper[1]^.reg);
  1582. taicpu(hp2).loadRef(1,taicpu(hp2).oper[0]^.ref^);
  1583. taicpu(hp2).loadReg(0,taicpu(p).oper[1]^.reg);
  1584. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1585. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  1586. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1587. AllocRegBetween(taicpu(p).oper[0]^.ref^.base,p,hp2,usedregs);
  1588. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and
  1589. (getsupreg(taicpu(p).oper[0]^.ref^.index) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1590. AllocRegBetween(taicpu(p).oper[0]^.ref^.index,p,hp2,usedregs);
  1591. end
  1592. else if (taicpu(hp1).Oper[0]^.reg <> taicpu(hp2).Oper[1]^.reg) then
  1593. begin
  1594. taicpu(hp2).loadReg(0,taicpu(hp1).Oper[0]^.reg);
  1595. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1596. end
  1597. else
  1598. begin
  1599. asml.remove(hp2);
  1600. hp2.free;
  1601. end
  1602. {$endif i386}
  1603. ;
  1604. end;
  1605. end;
  1606. (* { movl [mem1],reg1
  1607. movl [mem1],reg2
  1608. to
  1609. movl [mem1],reg1
  1610. movl reg1,reg2
  1611. }
  1612. else if (taicpu(p).oper[0]^.typ = top_ref) and
  1613. (taicpu(p).oper[1]^.typ = top_reg) and
  1614. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1615. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1616. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1617. RefsEqual(TReference(taicpu(p).oper[0]^^),taicpu(hp1).oper[0]^^.ref^) and
  1618. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.base) and
  1619. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.index) then
  1620. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg)
  1621. else*)
  1622. { movl const1,[mem1]
  1623. movl [mem1],reg1
  1624. to
  1625. movl const1,reg1
  1626. movl reg1,[mem1]
  1627. }
  1628. if MatchOpType(Taicpu(p),top_const,top_ref) and
  1629. MatchOpType(Taicpu(hp1),top_ref,top_reg) and
  1630. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1631. RefsEqual(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.ref^) and
  1632. not(RegInRef(taicpu(hp1).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^)) then
  1633. begin
  1634. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1635. taicpu(hp1).loadReg(0,taicpu(hp1).oper[1]^.reg);
  1636. taicpu(hp1).loadRef(1,taicpu(p).oper[1]^.ref^);
  1637. taicpu(p).loadReg(1,taicpu(hp1).oper[0]^.reg);
  1638. taicpu(hp1).fileinfo := taicpu(p).fileinfo;
  1639. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 1',p);
  1640. Result:=true;
  1641. exit;
  1642. end;
  1643. {
  1644. mov* x,reg1
  1645. mov* y,reg1
  1646. to
  1647. mov* y,reg1
  1648. }
  1649. if (taicpu(p).oper[1]^.typ=top_reg) and
  1650. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  1651. not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^)) then
  1652. begin
  1653. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 4 done',p);
  1654. { take care of the register (de)allocs following p }
  1655. UpdateUsedRegs(tai(p.next));
  1656. asml.remove(p);
  1657. p.free;
  1658. p:=hp1;
  1659. Result:=true;
  1660. exit;
  1661. end;
  1662. end;
  1663. { search further than the next instruction for a mov }
  1664. if (cs_opt_level3 in current_settings.optimizerswitches) and
  1665. { check as much as possible before the expensive GetNextInstructionUsingReg call }
  1666. (taicpu(p).oper[1]^.typ = top_reg) and
  1667. (taicpu(p).oper[0]^.typ in [top_reg,top_const]) and
  1668. { we work with hp2 here, so hp1 can be still used later on when
  1669. checking for GetNextInstruction_p }
  1670. GetNextInstructionUsingReg(p,hp2,taicpu(p).oper[1]^.reg) and
  1671. MatchInstruction(hp2,A_MOV,[]) and
  1672. MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  1673. ((taicpu(p).oper[0]^.typ=top_const) or
  1674. ((taicpu(p).oper[0]^.typ=top_reg) and
  1675. not(RegUsedBetween(taicpu(p).oper[0]^.reg, p, hp2))
  1676. )
  1677. ) then
  1678. begin
  1679. TransferUsedRegs(TmpUsedRegs);
  1680. { we have
  1681. mov x, %treg
  1682. mov %treg, y
  1683. }
  1684. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp2).oper[1]^)) and
  1685. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs)) then
  1686. { we've got
  1687. mov x, %treg
  1688. mov %treg, y
  1689. with %treg is not used after }
  1690. case taicpu(p).oper[0]^.typ Of
  1691. top_reg:
  1692. begin
  1693. { change
  1694. mov %reg, %treg
  1695. mov %treg, y
  1696. to
  1697. mov %reg, y
  1698. }
  1699. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp2,usedregs);
  1700. taicpu(hp2).loadOper(0,taicpu(p).oper[0]^);
  1701. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 6 done',p);
  1702. { take care of the register (de)allocs following p }
  1703. UpdateUsedRegs(tai(p.next));
  1704. asml.remove(p);
  1705. p.free;
  1706. p:=hp1;
  1707. Result:=true;
  1708. Exit;
  1709. end;
  1710. top_const:
  1711. begin
  1712. { change
  1713. mov const, %treg
  1714. mov %treg, y
  1715. to
  1716. mov const, y
  1717. }
  1718. if (taicpu(hp2).oper[1]^.typ=top_reg) or
  1719. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  1720. begin
  1721. taicpu(hp2).loadOper(0,taicpu(p).oper[0]^);
  1722. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 7 done',p);
  1723. { take care of the register (de)allocs following p }
  1724. UpdateUsedRegs(tai(p.next));
  1725. asml.remove(p);
  1726. p.free;
  1727. p:=hp1;
  1728. Result:=true;
  1729. Exit;
  1730. end;
  1731. end;
  1732. else
  1733. Internalerror(2019103001);
  1734. end;
  1735. end;
  1736. { Change
  1737. mov %reg1, %reg2
  1738. xxx %reg2, ???
  1739. to
  1740. mov %reg1, %reg2
  1741. xxx %reg1, ???
  1742. to avoid a write/read penalty
  1743. }
  1744. if GetNextInstruction_p and
  1745. MatchOpType(taicpu(p),top_reg,top_reg) and
  1746. MatchInstruction(hp1,A_OR,A_AND,A_TEST,[]) and
  1747. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  1748. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1749. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) then
  1750. { we have
  1751. mov %reg1, %reg2
  1752. test/or/and %reg2, %reg2
  1753. }
  1754. begin
  1755. TransferUsedRegs(TmpUsedRegs);
  1756. { reg1 will be used after the first instruction,
  1757. so update the allocation info }
  1758. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1759. if GetNextInstruction(hp1, hp2) and
  1760. (hp2.typ = ait_instruction) and
  1761. taicpu(hp2).is_jmp and
  1762. not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, hp1, TmpUsedRegs)) then
  1763. { change
  1764. mov %reg1, %reg2
  1765. test/or/and %reg2, %reg2
  1766. jxx
  1767. to
  1768. test %reg1, %reg1
  1769. jxx
  1770. }
  1771. begin
  1772. taicpu(hp1).loadoper(0,taicpu(p).oper[0]^);
  1773. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  1774. DebugMsg(SPeepholeOptimization + 'MovTestJxx2TestMov done',p);
  1775. asml.remove(p);
  1776. p.free;
  1777. p := hp1;
  1778. Exit;
  1779. end
  1780. else
  1781. { change
  1782. mov %reg1, %reg2
  1783. test/or/and %reg2, %reg2
  1784. to
  1785. mov %reg1, %reg2
  1786. test/or/and %reg1, %reg1
  1787. }
  1788. begin
  1789. taicpu(hp1).loadoper(0,taicpu(p).oper[0]^);
  1790. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  1791. DebugMsg(SPeepholeOptimization + 'MovTestJxx2MovTestJxx done',p);
  1792. end;
  1793. end;
  1794. { leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  1795. x >= RetOffset) as it doesn't do anything (it writes either to a
  1796. parameter or to the temporary storage room for the function
  1797. result)
  1798. }
  1799. if GetNextInstruction_p and
  1800. IsExitCode(hp1) and
  1801. MatchOpType(taicpu(p),top_reg,top_ref) and
  1802. (taicpu(p).oper[1]^.ref^.base = current_procinfo.FramePointer) and
  1803. not(assigned(current_procinfo.procdef.funcretsym) and
  1804. (taicpu(p).oper[1]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  1805. (taicpu(p).oper[1]^.ref^.index = NR_NO) then
  1806. begin
  1807. asml.remove(p);
  1808. p.free;
  1809. p:=hp1;
  1810. DebugMsg(SPeepholeOptimization + 'removed deadstore before leave/ret',p);
  1811. RemoveLastDeallocForFuncRes(p);
  1812. Result:=true;
  1813. exit;
  1814. end;
  1815. if GetNextInstruction_p and
  1816. MatchOpType(taicpu(p),top_reg,top_ref) and
  1817. MatchInstruction(hp1,A_CMP,A_TEST,[taicpu(p).opsize]) and
  1818. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1819. RefsEqual(taicpu(p).oper[1]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  1820. begin
  1821. { change
  1822. mov reg1, mem1
  1823. test/cmp x, mem1
  1824. to
  1825. mov reg1, mem1
  1826. test/cmp x, reg1
  1827. }
  1828. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  1829. DebugMsg(SPeepholeOptimization + 'MovTestCmp2MovTestCmp 1',hp1);
  1830. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1831. exit;
  1832. end;
  1833. if GetNextInstruction_p and
  1834. (taicpu(p).oper[1]^.typ = top_reg) and
  1835. (hp1.typ = ait_instruction) and
  1836. GetNextInstruction(hp1, hp2) and
  1837. MatchInstruction(hp2,A_MOV,[]) and
  1838. (SuperRegistersEqual(taicpu(hp2).oper[0]^.reg,taicpu(p).oper[1]^.reg)) and
  1839. (IsFoldableArithOp(taicpu(hp1), taicpu(p).oper[1]^.reg) or
  1840. ((taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and (taicpu(hp2).opsize=S_L) and
  1841. IsFoldableArithOp(taicpu(hp1), newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[1]^.reg),R_SUBQ)))
  1842. ) then
  1843. begin
  1844. if OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  1845. (taicpu(hp2).oper[0]^.typ=top_reg) then
  1846. { change movsX/movzX reg/ref, reg2
  1847. add/sub/or/... reg3/$const, reg2
  1848. mov reg2 reg/ref
  1849. dealloc reg2
  1850. to
  1851. add/sub/or/... reg3/$const, reg/ref }
  1852. begin
  1853. TransferUsedRegs(TmpUsedRegs);
  1854. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1855. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1856. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1857. begin
  1858. { by example:
  1859. movswl %si,%eax movswl %si,%eax p
  1860. decl %eax addl %edx,%eax hp1
  1861. movw %ax,%si movw %ax,%si hp2
  1862. ->
  1863. movswl %si,%eax movswl %si,%eax p
  1864. decw %eax addw %edx,%eax hp1
  1865. movw %ax,%si movw %ax,%si hp2
  1866. }
  1867. DebugMsg(SPeepholeOptimization + 'MovOpMov2Op ('+
  1868. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  1869. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  1870. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize),p);
  1871. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  1872. {
  1873. ->
  1874. movswl %si,%eax movswl %si,%eax p
  1875. decw %si addw %dx,%si hp1
  1876. movw %ax,%si movw %ax,%si hp2
  1877. }
  1878. case taicpu(hp1).ops of
  1879. 1:
  1880. begin
  1881. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  1882. if taicpu(hp1).oper[0]^.typ=top_reg then
  1883. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1884. end;
  1885. 2:
  1886. begin
  1887. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1888. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  1889. (taicpu(hp1).opcode<>A_SHL) and
  1890. (taicpu(hp1).opcode<>A_SHR) and
  1891. (taicpu(hp1).opcode<>A_SAR) then
  1892. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1893. end;
  1894. else
  1895. internalerror(2008042701);
  1896. end;
  1897. {
  1898. ->
  1899. decw %si addw %dx,%si p
  1900. }
  1901. asml.remove(hp2);
  1902. hp2.Free;
  1903. RemoveCurrentP(p);
  1904. Result:=True;
  1905. Exit;
  1906. end;
  1907. end;
  1908. if MatchOpType(taicpu(hp2),top_reg,top_reg) and
  1909. not(SuperRegistersEqual(taicpu(hp1).oper[0]^.reg,taicpu(hp2).oper[1]^.reg)) and
  1910. ((topsize2memsize[taicpu(hp1).opsize]<= topsize2memsize[taicpu(hp2).opsize]) or
  1911. { opsize matters for these opcodes, we could probably work around this, but it is not worth the effort }
  1912. ((taicpu(hp1).opcode<>A_SHL) and (taicpu(hp1).opcode<>A_SHR) and (taicpu(hp1).opcode<>A_SAR))
  1913. )
  1914. {$ifdef i386}
  1915. { byte registers of esi, edi, ebp, esp are not available on i386 }
  1916. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  1917. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(p).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  1918. {$endif i386}
  1919. then
  1920. { change movsX/movzX reg/ref, reg2
  1921. add/sub/or/... regX/$const, reg2
  1922. mov reg2, reg3
  1923. dealloc reg2
  1924. to
  1925. movsX/movzX reg/ref, reg3
  1926. add/sub/or/... reg3/$const, reg3
  1927. }
  1928. begin
  1929. TransferUsedRegs(TmpUsedRegs);
  1930. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1931. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1932. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1933. begin
  1934. { by example:
  1935. movswl %si,%eax movswl %si,%eax p
  1936. decl %eax addl %edx,%eax hp1
  1937. movw %ax,%si movw %ax,%si hp2
  1938. ->
  1939. movswl %si,%eax movswl %si,%eax p
  1940. decw %eax addw %edx,%eax hp1
  1941. movw %ax,%si movw %ax,%si hp2
  1942. }
  1943. DebugMsg(SPeepholeOptimization + 'MovOpMov2MovOp ('+
  1944. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  1945. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  1946. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  1947. { limit size of constants as well to avoid assembler errors, but
  1948. check opsize to avoid overflow when left shifting the 1 }
  1949. if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=4) then
  1950. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl (topsize2memsize[taicpu(hp2).opsize]*8))-1);
  1951. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  1952. taicpu(p).changeopsize(taicpu(hp2).opsize);
  1953. if taicpu(p).oper[0]^.typ=top_reg then
  1954. setsubreg(taicpu(p).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1955. taicpu(p).loadoper(1, taicpu(hp2).oper[1]^);
  1956. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,usedregs);
  1957. {
  1958. ->
  1959. movswl %si,%eax movswl %si,%eax p
  1960. decw %si addw %dx,%si hp1
  1961. movw %ax,%si movw %ax,%si hp2
  1962. }
  1963. case taicpu(hp1).ops of
  1964. 1:
  1965. begin
  1966. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  1967. if taicpu(hp1).oper[0]^.typ=top_reg then
  1968. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1969. end;
  1970. 2:
  1971. begin
  1972. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1973. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  1974. (taicpu(hp1).opcode<>A_SHL) and
  1975. (taicpu(hp1).opcode<>A_SHR) and
  1976. (taicpu(hp1).opcode<>A_SAR) then
  1977. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1978. end;
  1979. else
  1980. internalerror(2018111801);
  1981. end;
  1982. {
  1983. ->
  1984. decw %si addw %dx,%si p
  1985. }
  1986. asml.remove(hp2);
  1987. hp2.Free;
  1988. end;
  1989. end;
  1990. end;
  1991. if GetNextInstruction_p and
  1992. MatchInstruction(hp1,A_BTS,A_BTR,[Taicpu(p).opsize]) and
  1993. GetNextInstruction(hp1, hp2) and
  1994. MatchInstruction(hp2,A_OR,[Taicpu(p).opsize]) and
  1995. MatchOperand(Taicpu(p).oper[0]^,0) and
  1996. (Taicpu(p).oper[1]^.typ = top_reg) and
  1997. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp1).oper[1]^) and
  1998. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp2).oper[1]^) then
  1999. { mov reg1,0
  2000. bts reg1,operand1 --> mov reg1,operand2
  2001. or reg1,operand2 bts reg1,operand1}
  2002. begin
  2003. Taicpu(hp2).opcode:=A_MOV;
  2004. asml.remove(hp1);
  2005. insertllitem(hp2,hp2.next,hp1);
  2006. asml.remove(p);
  2007. p.free;
  2008. p:=hp1;
  2009. Result:=true;
  2010. exit;
  2011. end;
  2012. if GetNextInstruction_p and
  2013. MatchInstruction(hp1,A_LEA,[S_L]) and
  2014. MatchOpType(Taicpu(p),top_ref,top_reg) and
  2015. ((MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(hp1).oper[1]^.reg,Taicpu(p).oper[1]^.reg) and
  2016. (Taicpu(hp1).oper[0]^.ref^.base<>Taicpu(p).oper[1]^.reg)
  2017. ) or
  2018. (MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(p).oper[1]^.reg,Taicpu(hp1).oper[1]^.reg) and
  2019. (Taicpu(hp1).oper[0]^.ref^.index<>Taicpu(p).oper[1]^.reg)
  2020. )
  2021. ) then
  2022. { mov reg1,ref
  2023. lea reg2,[reg1,reg2]
  2024. to
  2025. add reg2,ref}
  2026. begin
  2027. TransferUsedRegs(TmpUsedRegs);
  2028. { reg1 may not be used afterwards }
  2029. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  2030. begin
  2031. Taicpu(hp1).opcode:=A_ADD;
  2032. Taicpu(hp1).oper[0]^.ref^:=Taicpu(p).oper[0]^.ref^;
  2033. DebugMsg(SPeepholeOptimization + 'MovLea2Add done',hp1);
  2034. asml.remove(p);
  2035. p.free;
  2036. p:=hp1;
  2037. result:=true;
  2038. exit;
  2039. end;
  2040. end;
  2041. end;
  2042. function TX86AsmOptimizer.OptPass1MOVXX(var p : tai) : boolean;
  2043. var
  2044. hp1 : tai;
  2045. begin
  2046. Result:=false;
  2047. if taicpu(p).ops <> 2 then
  2048. exit;
  2049. if GetNextInstruction(p,hp1) and
  2050. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  2051. (taicpu(hp1).ops = 2) then
  2052. begin
  2053. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  2054. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  2055. { movXX reg1, mem1 or movXX mem1, reg1
  2056. movXX mem2, reg2 movXX reg2, mem2}
  2057. begin
  2058. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  2059. { movXX reg1, mem1 or movXX mem1, reg1
  2060. movXX mem2, reg1 movXX reg2, mem1}
  2061. begin
  2062. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2063. begin
  2064. { Removes the second statement from
  2065. movXX reg1, mem1/reg2
  2066. movXX mem1/reg2, reg1
  2067. }
  2068. if taicpu(p).oper[0]^.typ=top_reg then
  2069. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2070. { Removes the second statement from
  2071. movXX mem1/reg1, reg2
  2072. movXX reg2, mem1/reg1
  2073. }
  2074. if (taicpu(p).oper[1]^.typ=top_reg) and
  2075. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)) then
  2076. begin
  2077. asml.remove(p);
  2078. p.free;
  2079. GetNextInstruction(hp1,p);
  2080. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2Nop 1 done',p);
  2081. end
  2082. else
  2083. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
  2084. asml.remove(hp1);
  2085. hp1.free;
  2086. Result:=true;
  2087. exit;
  2088. end
  2089. end;
  2090. end;
  2091. end;
  2092. end;
  2093. function TX86AsmOptimizer.OptPass1OP(var p : tai) : boolean;
  2094. var
  2095. hp1 : tai;
  2096. begin
  2097. result:=false;
  2098. { replace
  2099. <Op>X %mreg1,%mreg2 // Op in [ADD,MUL]
  2100. MovX %mreg2,%mreg1
  2101. dealloc %mreg2
  2102. by
  2103. <Op>X %mreg2,%mreg1
  2104. ?
  2105. }
  2106. if GetNextInstruction(p,hp1) and
  2107. { we mix single and double opperations here because we assume that the compiler
  2108. generates vmovapd only after double operations and vmovaps only after single operations }
  2109. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  2110. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2111. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  2112. (taicpu(p).oper[0]^.typ=top_reg) then
  2113. begin
  2114. TransferUsedRegs(TmpUsedRegs);
  2115. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2116. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2117. begin
  2118. taicpu(p).loadoper(0,taicpu(hp1).oper[0]^);
  2119. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  2120. DebugMsg(SPeepholeOptimization + 'OpMov2Op done',p);
  2121. asml.Remove(hp1);
  2122. hp1.Free;
  2123. result:=true;
  2124. end;
  2125. end;
  2126. end;
  2127. function TX86AsmOptimizer.OptPass1LEA(var p : tai) : boolean;
  2128. var
  2129. hp1, hp2, hp3: tai;
  2130. l : ASizeInt;
  2131. ref: Integer;
  2132. saveref: treference;
  2133. begin
  2134. Result:=false;
  2135. { removes seg register prefixes from LEA operations, as they
  2136. don't do anything}
  2137. taicpu(p).oper[0]^.ref^.Segment:=NR_NO;
  2138. { changes "lea (%reg1), %reg2" into "mov %reg1, %reg2" }
  2139. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  2140. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  2141. { do not mess with leas acessing the stack pointer }
  2142. (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) and
  2143. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  2144. begin
  2145. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  2146. (taicpu(p).oper[0]^.ref^.offset = 0) then
  2147. begin
  2148. hp1:=taicpu.op_reg_reg(A_MOV,taicpu(p).opsize,taicpu(p).oper[0]^.ref^.base,
  2149. taicpu(p).oper[1]^.reg);
  2150. InsertLLItem(p.previous,p.next, hp1);
  2151. DebugMsg(SPeepholeOptimization + 'Lea2Mov done',hp1);
  2152. p.free;
  2153. p:=hp1;
  2154. Result:=true;
  2155. exit;
  2156. end
  2157. else if (taicpu(p).oper[0]^.ref^.offset = 0) then
  2158. begin
  2159. DebugMsg(SPeepholeOptimization + 'Lea2Nop done',p);
  2160. RemoveCurrentP(p);
  2161. Result:=true;
  2162. exit;
  2163. end
  2164. { continue to use lea to adjust the stack pointer,
  2165. it is the recommended way, but only if not optimizing for size }
  2166. else if (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) or
  2167. (cs_opt_size in current_settings.optimizerswitches) then
  2168. with taicpu(p).oper[0]^.ref^ do
  2169. if (base = taicpu(p).oper[1]^.reg) then
  2170. begin
  2171. l:=offset;
  2172. if (l=1) and UseIncDec then
  2173. begin
  2174. taicpu(p).opcode:=A_INC;
  2175. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  2176. taicpu(p).ops:=1;
  2177. DebugMsg(SPeepholeOptimization + 'Lea2Inc done',p);
  2178. end
  2179. else if (l=-1) and UseIncDec then
  2180. begin
  2181. taicpu(p).opcode:=A_DEC;
  2182. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  2183. taicpu(p).ops:=1;
  2184. DebugMsg(SPeepholeOptimization + 'Lea2Dec done',p);
  2185. end
  2186. else
  2187. begin
  2188. if (l<0) and (l<>-2147483648) then
  2189. begin
  2190. taicpu(p).opcode:=A_SUB;
  2191. taicpu(p).loadConst(0,-l);
  2192. DebugMsg(SPeepholeOptimization + 'Lea2Sub done',p);
  2193. end
  2194. else
  2195. begin
  2196. taicpu(p).opcode:=A_ADD;
  2197. taicpu(p).loadConst(0,l);
  2198. DebugMsg(SPeepholeOptimization + 'Lea2Add done',p);
  2199. end;
  2200. end;
  2201. Result:=true;
  2202. exit;
  2203. end;
  2204. end;
  2205. if GetNextInstruction(p,hp1) and
  2206. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  2207. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2208. MatchOpType(Taicpu(hp1),top_reg,top_reg) and
  2209. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) then
  2210. begin
  2211. TransferUsedRegs(TmpUsedRegs);
  2212. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2213. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2214. begin
  2215. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  2216. DebugMsg(SPeepholeOptimization + 'LeaMov2Lea done',p);
  2217. asml.Remove(hp1);
  2218. hp1.Free;
  2219. result:=true;
  2220. end;
  2221. end;
  2222. { changes
  2223. lea offset1(regX), reg1
  2224. lea offset2(reg1), reg1
  2225. to
  2226. lea offset1+offset2(regX), reg1 }
  2227. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) and
  2228. MatchInstruction(hp1,A_LEA,[S_L]) and
  2229. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  2230. (taicpu(hp1).oper[0]^.ref^.base=taicpu(p).oper[1]^.reg) and
  2231. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  2232. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  2233. (taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) and
  2234. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  2235. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  2236. (taicpu(p).oper[0]^.ref^.index=taicpu(hp1).oper[0]^.ref^.index) and
  2237. (taicpu(p).oper[0]^.ref^.relsymbol=taicpu(hp1).oper[0]^.ref^.relsymbol) and
  2238. (taicpu(p).oper[0]^.ref^.scalefactor=taicpu(hp1).oper[0]^.ref^.scalefactor) and
  2239. (taicpu(p).oper[0]^.ref^.segment=taicpu(hp1).oper[0]^.ref^.segment) and
  2240. (taicpu(p).oper[0]^.ref^.symbol=taicpu(hp1).oper[0]^.ref^.symbol) then
  2241. begin
  2242. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea done',p);
  2243. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  2244. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  2245. RemoveCurrentP(p);
  2246. result:=true;
  2247. exit;
  2248. end;
  2249. { changes
  2250. lea <ref1>, reg1
  2251. <op> ...,<ref. with reg1>,...
  2252. to
  2253. <op> ...,<ref1>,... }
  2254. if (taicpu(p).oper[1]^.reg<>current_procinfo.framepointer) and
  2255. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) and
  2256. GetNextInstruction(p,hp1) and
  2257. (hp1.typ=ait_instruction) and
  2258. not(MatchInstruction(hp1,A_LEA,[])) then
  2259. begin
  2260. { find a reference which uses reg1 }
  2261. if (taicpu(hp1).ops>=1) and (taicpu(hp1).oper[0]^.typ=top_ref) and RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^) then
  2262. ref:=0
  2263. else if (taicpu(hp1).ops>=2) and (taicpu(hp1).oper[1]^.typ=top_ref) and RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^) then
  2264. ref:=1
  2265. else
  2266. ref:=-1;
  2267. if (ref<>-1) and
  2268. { reg1 must be either the base or the index }
  2269. ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) xor (taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg)) then
  2270. begin
  2271. { reg1 can be removed from the reference }
  2272. saveref:=taicpu(hp1).oper[ref]^.ref^;
  2273. if taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg then
  2274. taicpu(hp1).oper[ref]^.ref^.base:=NR_NO
  2275. else if taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg then
  2276. taicpu(hp1).oper[ref]^.ref^.index:=NR_NO
  2277. else
  2278. Internalerror(2019111201);
  2279. { check if the can insert all data of the lea into the second instruction }
  2280. if ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) or (taicpu(hp1).oper[ref]^.ref^.scalefactor in [0,1])) and
  2281. ((taicpu(p).oper[0]^.ref^.base=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.base=NR_NO)) and
  2282. ((taicpu(p).oper[0]^.ref^.index=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.index=NR_NO)) and
  2283. ((taicpu(p).oper[0]^.ref^.symbol=nil) or (taicpu(hp1).oper[ref]^.ref^.symbol=nil)) and
  2284. ((taicpu(p).oper[0]^.ref^.relsymbol=nil) or (taicpu(hp1).oper[ref]^.ref^.relsymbol=nil)) and
  2285. ((taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) or (taicpu(hp1).oper[ref]^.ref^.scalefactor in [0,1])) and
  2286. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.segment=NR_NO)
  2287. {$ifdef x86_64}
  2288. and (abs(taicpu(hp1).oper[ref]^.ref^.offset+taicpu(p).oper[0]^.ref^.offset)<=$7fffffff)
  2289. and (((taicpu(p).oper[0]^.ref^.base<>NR_RIP) and (taicpu(p).oper[0]^.ref^.index<>NR_RIP)) or
  2290. ((taicpu(hp1).oper[ref]^.ref^.base=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.index=NR_NO))
  2291. )
  2292. {$endif x86_64}
  2293. then
  2294. begin
  2295. { reg1 might not used by the second instruction after it is remove from the reference }
  2296. if not(RegInInstruction(taicpu(p).oper[1]^.reg,taicpu(hp1))) then
  2297. begin
  2298. TransferUsedRegs(TmpUsedRegs);
  2299. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2300. { reg1 is not updated so it might not be used afterwards }
  2301. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2302. begin
  2303. DebugMsg(SPeepholeOptimization + 'LeaOp2Op done',p);
  2304. if taicpu(p).oper[0]^.ref^.base<>NR_NO then
  2305. taicpu(hp1).oper[ref]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  2306. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  2307. taicpu(hp1).oper[ref]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  2308. if taicpu(p).oper[0]^.ref^.symbol<>nil then
  2309. taicpu(hp1).oper[ref]^.ref^.symbol:=taicpu(p).oper[0]^.ref^.symbol;
  2310. if taicpu(p).oper[0]^.ref^.relsymbol<>nil then
  2311. taicpu(hp1).oper[ref]^.ref^.relsymbol:=taicpu(p).oper[0]^.ref^.relsymbol;
  2312. if not(taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) then
  2313. taicpu(hp1).oper[ref]^.ref^.scalefactor:=taicpu(p).oper[0]^.ref^.scalefactor;
  2314. inc(taicpu(hp1).oper[ref]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  2315. RemoveCurrentP(p);
  2316. result:=true;
  2317. exit;
  2318. end
  2319. end;
  2320. end;
  2321. { recover }
  2322. taicpu(hp1).oper[ref]^.ref^:=saveref;
  2323. end;
  2324. end;
  2325. { replace
  2326. lea x(stackpointer),stackpointer
  2327. call procname
  2328. lea -x(stackpointer),stackpointer
  2329. ret
  2330. by
  2331. jmp procname
  2332. this should never hurt except when pic is used, not sure
  2333. how to handle it then
  2334. but do it only on level 4 because it destroys stack back traces
  2335. }
  2336. if (cs_opt_level4 in current_settings.optimizerswitches) and
  2337. not(cs_create_pic in current_settings.moduleswitches) and
  2338. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  2339. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  2340. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  2341. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  2342. (taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) and
  2343. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  2344. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  2345. GetNextInstruction(p, hp1) and
  2346. MatchInstruction(hp1,A_CALL,[S_NO]) and
  2347. GetNextInstruction(hp1, hp2) and
  2348. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  2349. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  2350. (taicpu(p).oper[0]^.ref^.base=taicpu(hp2).oper[0]^.ref^.base) and
  2351. (taicpu(p).oper[0]^.ref^.index=taicpu(hp2).oper[0]^.ref^.index) and
  2352. (taicpu(p).oper[0]^.ref^.offset=-taicpu(hp2).oper[0]^.ref^.offset) and
  2353. (taicpu(p).oper[0]^.ref^.relsymbol=taicpu(hp2).oper[0]^.ref^.relsymbol) and
  2354. (taicpu(p).oper[0]^.ref^.scalefactor=taicpu(hp2).oper[0]^.ref^.scalefactor) and
  2355. (taicpu(p).oper[0]^.ref^.segment=taicpu(hp2).oper[0]^.ref^.segment) and
  2356. (taicpu(p).oper[0]^.ref^.symbol=taicpu(hp2).oper[0]^.ref^.symbol) and
  2357. GetNextInstruction(hp2, hp3) and
  2358. MatchInstruction(hp3,A_RET,[S_NO]) and
  2359. (taicpu(hp3).ops=0) then
  2360. begin
  2361. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  2362. taicpu(hp1).opcode:=A_JMP;
  2363. taicpu(hp1).is_jmp:=true;
  2364. asml.remove(p);
  2365. asml.remove(hp2);
  2366. asml.remove(hp3);
  2367. p.free;
  2368. hp2.free;
  2369. hp3.free;
  2370. p:=hp1;
  2371. Result:=true;
  2372. end;
  2373. end;
  2374. function TX86AsmOptimizer.DoSubAddOpt(var p: tai): Boolean;
  2375. var
  2376. hp1 : tai;
  2377. begin
  2378. DoSubAddOpt := False;
  2379. if GetLastInstruction(p, hp1) and
  2380. (hp1.typ = ait_instruction) and
  2381. (taicpu(hp1).opsize = taicpu(p).opsize) then
  2382. case taicpu(hp1).opcode Of
  2383. A_DEC:
  2384. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  2385. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2386. begin
  2387. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
  2388. asml.remove(hp1);
  2389. hp1.free;
  2390. end;
  2391. A_SUB:
  2392. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  2393. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  2394. begin
  2395. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
  2396. asml.remove(hp1);
  2397. hp1.free;
  2398. end;
  2399. A_ADD:
  2400. begin
  2401. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  2402. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  2403. begin
  2404. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  2405. asml.remove(hp1);
  2406. hp1.free;
  2407. if (taicpu(p).oper[0]^.val = 0) then
  2408. begin
  2409. hp1 := tai(p.next);
  2410. asml.remove(p);
  2411. p.free;
  2412. if not GetLastInstruction(hp1, p) then
  2413. p := hp1;
  2414. DoSubAddOpt := True;
  2415. end
  2416. end;
  2417. end;
  2418. else
  2419. ;
  2420. end;
  2421. end;
  2422. function TX86AsmOptimizer.OptPass1Sub(var p : tai) : boolean;
  2423. {$ifdef i386}
  2424. var
  2425. hp1 : tai;
  2426. {$endif i386}
  2427. begin
  2428. Result:=false;
  2429. { * change "subl $2, %esp; pushw x" to "pushl x"}
  2430. { * change "sub/add const1, reg" or "dec reg" followed by
  2431. "sub const2, reg" to one "sub ..., reg" }
  2432. if MatchOpType(taicpu(p),top_const,top_reg) then
  2433. begin
  2434. {$ifdef i386}
  2435. if (taicpu(p).oper[0]^.val = 2) and
  2436. (taicpu(p).oper[1]^.reg = NR_ESP) and
  2437. { Don't do the sub/push optimization if the sub }
  2438. { comes from setting up the stack frame (JM) }
  2439. (not(GetLastInstruction(p,hp1)) or
  2440. not(MatchInstruction(hp1,A_MOV,[S_L]) and
  2441. MatchOperand(taicpu(hp1).oper[0]^,NR_ESP) and
  2442. MatchOperand(taicpu(hp1).oper[0]^,NR_EBP))) then
  2443. begin
  2444. hp1 := tai(p.next);
  2445. while Assigned(hp1) and
  2446. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  2447. not RegReadByInstruction(NR_ESP,hp1) and
  2448. not RegModifiedByInstruction(NR_ESP,hp1) do
  2449. hp1 := tai(hp1.next);
  2450. if Assigned(hp1) and
  2451. MatchInstruction(hp1,A_PUSH,[S_W]) then
  2452. begin
  2453. taicpu(hp1).changeopsize(S_L);
  2454. if taicpu(hp1).oper[0]^.typ=top_reg then
  2455. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  2456. hp1 := tai(p.next);
  2457. asml.remove(p);
  2458. p.free;
  2459. p := hp1;
  2460. Result:=true;
  2461. exit;
  2462. end;
  2463. end;
  2464. {$endif i386}
  2465. if DoSubAddOpt(p) then
  2466. Result:=true;
  2467. end;
  2468. end;
  2469. function TX86AsmOptimizer.OptPass1SHLSAL(var p : tai) : boolean;
  2470. var
  2471. TmpBool1,TmpBool2 : Boolean;
  2472. tmpref : treference;
  2473. hp1,hp2: tai;
  2474. begin
  2475. Result:=false;
  2476. if MatchOpType(taicpu(p),top_const,top_reg) and
  2477. (taicpu(p).opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  2478. (taicpu(p).oper[0]^.val <= 3) then
  2479. { Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement }
  2480. begin
  2481. { should we check the next instruction? }
  2482. TmpBool1 := True;
  2483. { have we found an add/sub which could be
  2484. integrated in the lea? }
  2485. TmpBool2 := False;
  2486. reference_reset(tmpref,2,[]);
  2487. TmpRef.index := taicpu(p).oper[1]^.reg;
  2488. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  2489. while TmpBool1 and
  2490. GetNextInstruction(p, hp1) and
  2491. (tai(hp1).typ = ait_instruction) and
  2492. ((((taicpu(hp1).opcode = A_ADD) or
  2493. (taicpu(hp1).opcode = A_SUB)) and
  2494. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  2495. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  2496. (((taicpu(hp1).opcode = A_INC) or
  2497. (taicpu(hp1).opcode = A_DEC)) and
  2498. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  2499. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg)) or
  2500. ((taicpu(hp1).opcode = A_LEA) and
  2501. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  2502. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg))) and
  2503. (not GetNextInstruction(hp1,hp2) or
  2504. not instrReadsFlags(hp2)) Do
  2505. begin
  2506. TmpBool1 := False;
  2507. if taicpu(hp1).opcode=A_LEA then
  2508. begin
  2509. if (TmpRef.base = NR_NO) and
  2510. (taicpu(hp1).oper[0]^.ref^.symbol=nil) and
  2511. (taicpu(hp1).oper[0]^.ref^.relsymbol=nil) and
  2512. (taicpu(hp1).oper[0]^.ref^.segment=NR_NO) and
  2513. ((taicpu(hp1).oper[0]^.ref^.scalefactor=0) or
  2514. (taicpu(hp1).oper[0]^.ref^.scalefactor*tmpref.scalefactor<=8)) then
  2515. begin
  2516. TmpBool1 := True;
  2517. TmpBool2 := True;
  2518. inc(TmpRef.offset, taicpu(hp1).oper[0]^.ref^.offset);
  2519. if taicpu(hp1).oper[0]^.ref^.scalefactor<>0 then
  2520. tmpref.scalefactor:=tmpref.scalefactor*taicpu(hp1).oper[0]^.ref^.scalefactor;
  2521. TmpRef.base := taicpu(hp1).oper[0]^.ref^.base;
  2522. asml.remove(hp1);
  2523. hp1.free;
  2524. end
  2525. end
  2526. else if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  2527. begin
  2528. TmpBool1 := True;
  2529. TmpBool2 := True;
  2530. case taicpu(hp1).opcode of
  2531. A_ADD:
  2532. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  2533. A_SUB:
  2534. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  2535. else
  2536. internalerror(2019050536);
  2537. end;
  2538. asml.remove(hp1);
  2539. hp1.free;
  2540. end
  2541. else
  2542. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  2543. (((taicpu(hp1).opcode = A_ADD) and
  2544. (TmpRef.base = NR_NO)) or
  2545. (taicpu(hp1).opcode = A_INC) or
  2546. (taicpu(hp1).opcode = A_DEC)) then
  2547. begin
  2548. TmpBool1 := True;
  2549. TmpBool2 := True;
  2550. case taicpu(hp1).opcode of
  2551. A_ADD:
  2552. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  2553. A_INC:
  2554. inc(TmpRef.offset);
  2555. A_DEC:
  2556. dec(TmpRef.offset);
  2557. else
  2558. internalerror(2019050535);
  2559. end;
  2560. asml.remove(hp1);
  2561. hp1.free;
  2562. end;
  2563. end;
  2564. if TmpBool2
  2565. {$ifndef x86_64}
  2566. or
  2567. ((current_settings.optimizecputype < cpu_Pentium2) and
  2568. (taicpu(p).oper[0]^.val <= 3) and
  2569. not(cs_opt_size in current_settings.optimizerswitches))
  2570. {$endif x86_64}
  2571. then
  2572. begin
  2573. if not(TmpBool2) and
  2574. (taicpu(p).oper[0]^.val=1) then
  2575. begin
  2576. hp1:=taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  2577. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  2578. end
  2579. else
  2580. hp1:=taicpu.op_ref_reg(A_LEA, taicpu(p).opsize, TmpRef,
  2581. taicpu(p).oper[1]^.reg);
  2582. DebugMsg(SPeepholeOptimization + 'ShlAddLeaSubIncDec2Lea',p);
  2583. InsertLLItem(p.previous, p.next, hp1);
  2584. p.free;
  2585. p := hp1;
  2586. end;
  2587. end
  2588. {$ifndef x86_64}
  2589. else if (current_settings.optimizecputype < cpu_Pentium2) and
  2590. MatchOpType(taicpu(p),top_const,top_reg) then
  2591. begin
  2592. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  2593. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  2594. (unlike shl, which is only Tairable in the U pipe) }
  2595. if taicpu(p).oper[0]^.val=1 then
  2596. begin
  2597. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  2598. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  2599. InsertLLItem(p.previous, p.next, hp1);
  2600. p.free;
  2601. p := hp1;
  2602. end
  2603. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  2604. "shl $3, %reg" to "lea (,%reg,8), %reg }
  2605. else if (taicpu(p).opsize = S_L) and
  2606. (taicpu(p).oper[0]^.val<= 3) then
  2607. begin
  2608. reference_reset(tmpref,2,[]);
  2609. TmpRef.index := taicpu(p).oper[1]^.reg;
  2610. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  2611. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  2612. InsertLLItem(p.previous, p.next, hp1);
  2613. p.free;
  2614. p := hp1;
  2615. end;
  2616. end
  2617. {$endif x86_64}
  2618. ;
  2619. end;
  2620. function TX86AsmOptimizer.OptPass1SETcc(var p: tai): boolean;
  2621. var
  2622. hp1,hp2,next: tai; SetC, JumpC: TAsmCond;
  2623. begin
  2624. Result:=false;
  2625. if MatchOpType(taicpu(p),top_reg) and
  2626. GetNextInstruction(p, hp1) and
  2627. MatchInstruction(hp1, A_TEST, [S_B]) and
  2628. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2629. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  2630. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  2631. GetNextInstruction(hp1, hp2) and
  2632. MatchInstruction(hp2, A_Jcc, []) then
  2633. { Change from: To:
  2634. set(C) %reg j(~C) label
  2635. test %reg,%reg
  2636. je label
  2637. set(C) %reg j(C) label
  2638. test %reg,%reg
  2639. jne label
  2640. }
  2641. begin
  2642. next := tai(p.Next);
  2643. TransferUsedRegs(TmpUsedRegs);
  2644. UpdateUsedRegs(TmpUsedRegs, next);
  2645. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2646. asml.Remove(hp1);
  2647. hp1.Free;
  2648. JumpC := taicpu(hp2).condition;
  2649. if conditions_equal(JumpC, C_E) then
  2650. SetC := inverse_cond(taicpu(p).condition)
  2651. else if conditions_equal(JumpC, C_NE) then
  2652. SetC := taicpu(p).condition
  2653. else
  2654. InternalError(2018061400);
  2655. if SetC = C_NONE then
  2656. InternalError(2018061401);
  2657. taicpu(hp2).SetCondition(SetC);
  2658. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
  2659. begin
  2660. asml.Remove(p);
  2661. UpdateUsedRegs(next);
  2662. p.Free;
  2663. Result := True;
  2664. p := hp2;
  2665. end;
  2666. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> Jcc',p);
  2667. end;
  2668. end;
  2669. function TX86AsmOptimizer.OptPass1FSTP(var p: tai): boolean;
  2670. { returns true if a "continue" should be done after this optimization }
  2671. var
  2672. hp1, hp2: tai;
  2673. begin
  2674. Result := false;
  2675. if MatchOpType(taicpu(p),top_ref) and
  2676. GetNextInstruction(p, hp1) and
  2677. (hp1.typ = ait_instruction) and
  2678. (((taicpu(hp1).opcode = A_FLD) and
  2679. (taicpu(p).opcode = A_FSTP)) or
  2680. ((taicpu(p).opcode = A_FISTP) and
  2681. (taicpu(hp1).opcode = A_FILD))) and
  2682. MatchOpType(taicpu(hp1),top_ref) and
  2683. (taicpu(hp1).opsize = taicpu(p).opsize) and
  2684. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  2685. begin
  2686. { replacing fstp f;fld f by fst f is only valid for extended because of rounding }
  2687. if (taicpu(p).opsize=S_FX) and
  2688. GetNextInstruction(hp1, hp2) and
  2689. (hp2.typ = ait_instruction) and
  2690. IsExitCode(hp2) and
  2691. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  2692. not(assigned(current_procinfo.procdef.funcretsym) and
  2693. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  2694. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  2695. begin
  2696. asml.remove(p);
  2697. asml.remove(hp1);
  2698. p.free;
  2699. hp1.free;
  2700. p := hp2;
  2701. RemoveLastDeallocForFuncRes(p);
  2702. Result := true;
  2703. end
  2704. (* can't be done because the store operation rounds
  2705. else
  2706. { fst can't store an extended value! }
  2707. if (taicpu(p).opsize <> S_FX) and
  2708. (taicpu(p).opsize <> S_IQ) then
  2709. begin
  2710. if (taicpu(p).opcode = A_FSTP) then
  2711. taicpu(p).opcode := A_FST
  2712. else taicpu(p).opcode := A_FIST;
  2713. asml.remove(hp1);
  2714. hp1.free;
  2715. end
  2716. *)
  2717. end;
  2718. end;
  2719. function TX86AsmOptimizer.OptPass1FLD(var p : tai) : boolean;
  2720. var
  2721. hp1, hp2: tai;
  2722. begin
  2723. result:=false;
  2724. if MatchOpType(taicpu(p),top_reg) and
  2725. GetNextInstruction(p, hp1) and
  2726. (hp1.typ = Ait_Instruction) and
  2727. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2728. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  2729. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  2730. { change to
  2731. fld reg fxxx reg,st
  2732. fxxxp st, st1 (hp1)
  2733. Remark: non commutative operations must be reversed!
  2734. }
  2735. begin
  2736. case taicpu(hp1).opcode Of
  2737. A_FMULP,A_FADDP,
  2738. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  2739. begin
  2740. case taicpu(hp1).opcode Of
  2741. A_FADDP: taicpu(hp1).opcode := A_FADD;
  2742. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  2743. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  2744. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  2745. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  2746. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  2747. else
  2748. internalerror(2019050534);
  2749. end;
  2750. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  2751. taicpu(hp1).oper[1]^.reg := NR_ST;
  2752. asml.remove(p);
  2753. p.free;
  2754. p := hp1;
  2755. Result:=true;
  2756. exit;
  2757. end;
  2758. else
  2759. ;
  2760. end;
  2761. end
  2762. else
  2763. if MatchOpType(taicpu(p),top_ref) and
  2764. GetNextInstruction(p, hp2) and
  2765. (hp2.typ = Ait_Instruction) and
  2766. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  2767. (taicpu(p).opsize in [S_FS, S_FL]) and
  2768. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  2769. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  2770. if GetLastInstruction(p, hp1) and
  2771. MatchInstruction(hp1,A_FLD,A_FST,[taicpu(p).opsize]) and
  2772. MatchOpType(taicpu(hp1),top_ref) and
  2773. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  2774. if ((taicpu(hp2).opcode = A_FMULP) or
  2775. (taicpu(hp2).opcode = A_FADDP)) then
  2776. { change to
  2777. fld/fst mem1 (hp1) fld/fst mem1
  2778. fld mem1 (p) fadd/
  2779. faddp/ fmul st, st
  2780. fmulp st, st1 (hp2) }
  2781. begin
  2782. asml.remove(p);
  2783. p.free;
  2784. p := hp1;
  2785. if (taicpu(hp2).opcode = A_FADDP) then
  2786. taicpu(hp2).opcode := A_FADD
  2787. else
  2788. taicpu(hp2).opcode := A_FMUL;
  2789. taicpu(hp2).oper[1]^.reg := NR_ST;
  2790. end
  2791. else
  2792. { change to
  2793. fld/fst mem1 (hp1) fld/fst mem1
  2794. fld mem1 (p) fld st}
  2795. begin
  2796. taicpu(p).changeopsize(S_FL);
  2797. taicpu(p).loadreg(0,NR_ST);
  2798. end
  2799. else
  2800. begin
  2801. case taicpu(hp2).opcode Of
  2802. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  2803. { change to
  2804. fld/fst mem1 (hp1) fld/fst mem1
  2805. fld mem2 (p) fxxx mem2
  2806. fxxxp st, st1 (hp2) }
  2807. begin
  2808. case taicpu(hp2).opcode Of
  2809. A_FADDP: taicpu(p).opcode := A_FADD;
  2810. A_FMULP: taicpu(p).opcode := A_FMUL;
  2811. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  2812. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  2813. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  2814. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  2815. else
  2816. internalerror(2019050533);
  2817. end;
  2818. asml.remove(hp2);
  2819. hp2.free;
  2820. end
  2821. else
  2822. ;
  2823. end
  2824. end
  2825. end;
  2826. function TX86AsmOptimizer.OptPass1Cmp(var p: tai): boolean;
  2827. var
  2828. v: QWord;
  2829. hp1, hp2, hp3, hp4: tai;
  2830. begin
  2831. Result:=false;
  2832. { cmp register,$8000 neg register
  2833. je target --> jo target
  2834. .... only if register is deallocated before jump.}
  2835. case Taicpu(p).opsize of
  2836. S_B: v:=$80;
  2837. S_W: v:=$8000;
  2838. S_L: v:=qword($80000000);
  2839. S_Q : v:=qword($8000000000000000);
  2840. else
  2841. internalerror(2013112905);
  2842. end;
  2843. if MatchOpType(taicpu(p),Top_const,top_reg) and
  2844. (taicpu(p).oper[0]^.val=v) and
  2845. GetNextInstruction(p, hp1) and
  2846. MatchInstruction(hp1,A_Jcc,[]) and
  2847. (Taicpu(hp1).condition in [C_E,C_NE]) then
  2848. begin
  2849. TransferUsedRegs(TmpUsedRegs);
  2850. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  2851. if not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, TmpUsedRegs)) then
  2852. begin
  2853. DebugMsg(SPeepholeOptimization + 'CmpJe2NegJo done',p);
  2854. Taicpu(p).opcode:=A_NEG;
  2855. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  2856. Taicpu(p).clearop(1);
  2857. Taicpu(p).ops:=1;
  2858. if Taicpu(hp1).condition=C_E then
  2859. Taicpu(hp1).condition:=C_O
  2860. else
  2861. Taicpu(hp1).condition:=C_NO;
  2862. Result:=true;
  2863. exit;
  2864. end;
  2865. end;
  2866. {
  2867. @@2: @@2:
  2868. .... ....
  2869. cmp operand1,0
  2870. jle/jbe @@1
  2871. dec operand1 --> sub operand1,1
  2872. jmp @@2 jge/jae @@2
  2873. @@1: @@1:
  2874. ... ....}
  2875. if (taicpu(p).oper[0]^.typ = top_const) and
  2876. (taicpu(p).oper[1]^.typ in [top_reg,top_ref]) and
  2877. (taicpu(p).oper[0]^.val = 0) and
  2878. GetNextInstruction(p, hp1) and
  2879. MatchInstruction(hp1,A_Jcc,[]) and
  2880. (taicpu(hp1).condition in [C_LE,C_BE]) and
  2881. GetNextInstruction(hp1,hp2) and
  2882. MatchInstruction(hp1,A_DEC,[]) and
  2883. OpsEqual(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  2884. GetNextInstruction(hp2, hp3) and
  2885. MatchInstruction(hp1,A_JMP,[]) and
  2886. GetNextInstruction(hp3, hp4) and
  2887. FindLabel(tasmlabel(taicpu(hp1).oper[0]^.ref^.symbol),hp4) then
  2888. begin
  2889. DebugMsg(SPeepholeOptimization + 'CmpJxxDecJmp2SubJcc done',p);
  2890. taicpu(hp2).Opcode := A_SUB;
  2891. taicpu(hp2).loadoper(1,taicpu(hp2).oper[0]^);
  2892. taicpu(hp2).loadConst(0,1);
  2893. taicpu(hp2).ops:=2;
  2894. taicpu(hp3).Opcode := A_Jcc;
  2895. case taicpu(hp1).condition of
  2896. C_LE: taicpu(hp3).condition := C_GE;
  2897. C_BE: taicpu(hp3).condition := C_AE;
  2898. else
  2899. internalerror(2019050903);
  2900. end;
  2901. asml.remove(p);
  2902. asml.remove(hp1);
  2903. p.free;
  2904. hp1.free;
  2905. p := hp2;
  2906. Result:=true;
  2907. exit;
  2908. end;
  2909. end;
  2910. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  2911. var
  2912. hp1,hp2: tai;
  2913. {$ifdef x86_64}
  2914. hp3: tai;
  2915. {$endif x86_64}
  2916. begin
  2917. Result:=false;
  2918. if MatchOpType(taicpu(p),top_reg,top_reg) and
  2919. GetNextInstruction(p, hp1) and
  2920. {$ifdef x86_64}
  2921. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  2922. {$else x86_64}
  2923. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  2924. {$endif x86_64}
  2925. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2926. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  2927. { mov reg1, reg2 mov reg1, reg2
  2928. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  2929. begin
  2930. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  2931. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  2932. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  2933. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  2934. TransferUsedRegs(TmpUsedRegs);
  2935. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2936. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  2937. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  2938. then
  2939. begin
  2940. asml.remove(p);
  2941. p.free;
  2942. p := hp1;
  2943. Result:=true;
  2944. end;
  2945. exit;
  2946. end
  2947. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  2948. GetNextInstruction(p, hp1) and
  2949. {$ifdef x86_64}
  2950. MatchInstruction(hp1,[A_MOV,A_MOVZX,A_MOVSX,A_MOVSXD],[]) and
  2951. {$else x86_64}
  2952. MatchInstruction(hp1,A_MOV,A_MOVZX,A_MOVSX,[]) and
  2953. {$endif x86_64}
  2954. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  2955. ((taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg)
  2956. or
  2957. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg)
  2958. ) and
  2959. (getsupreg(taicpu(hp1).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) then
  2960. { mov reg1, reg2
  2961. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  2962. begin
  2963. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) then
  2964. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[0]^.reg;
  2965. if (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) then
  2966. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  2967. DebugMsg(SPeepholeOptimization + 'MovMovXX2MoVXX 1 done',p);
  2968. asml.remove(p);
  2969. p.free;
  2970. p := hp1;
  2971. Result:=true;
  2972. exit;
  2973. end
  2974. else if (taicpu(p).oper[0]^.typ = top_ref) and
  2975. GetNextInstruction(p,hp1) and
  2976. (hp1.typ = ait_instruction) and
  2977. { while the GetNextInstruction(hp1,hp2) call could be factored out,
  2978. doing it separately in both branches allows to do the cheap checks
  2979. with low probability earlier }
  2980. ((IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  2981. GetNextInstruction(hp1,hp2) and
  2982. MatchInstruction(hp2,A_MOV,[])
  2983. ) or
  2984. ((taicpu(hp1).opcode=A_LEA) and
  2985. GetNextInstruction(hp1,hp2) and
  2986. MatchInstruction(hp2,A_MOV,[]) and
  2987. ((MatchReference(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  2988. (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg)
  2989. ) or
  2990. (MatchReference(taicpu(hp1).oper[0]^.ref^,NR_INVALID,
  2991. taicpu(p).oper[1]^.reg) and
  2992. (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg)) or
  2993. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_NO)) or
  2994. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,NR_NO,taicpu(p).oper[1]^.reg))
  2995. ) and
  2996. ((MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^)) or not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)))
  2997. )
  2998. ) and
  2999. MatchOperand(taicpu(hp1).oper[taicpu(hp1).ops-1]^,taicpu(hp2).oper[0]^) and
  3000. (taicpu(hp2).oper[1]^.typ = top_ref) then
  3001. begin
  3002. TransferUsedRegs(TmpUsedRegs);
  3003. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  3004. UpdateUsedRegs(TmpUsedRegs,tai(hp1.next));
  3005. if (RefsEqual(taicpu(hp2).oper[1]^.ref^,taicpu(p).oper[0]^.ref^) and
  3006. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,TmpUsedRegs))) then
  3007. { change mov (ref), reg
  3008. add/sub/or/... reg2/$const, reg
  3009. mov reg, (ref)
  3010. # release reg
  3011. to add/sub/or/... reg2/$const, (ref) }
  3012. begin
  3013. case taicpu(hp1).opcode of
  3014. A_INC,A_DEC,A_NOT,A_NEG :
  3015. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  3016. A_LEA :
  3017. begin
  3018. taicpu(hp1).opcode:=A_ADD;
  3019. if (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.index<>NR_NO) then
  3020. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.index)
  3021. else if (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.base<>NR_NO) then
  3022. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.base)
  3023. else
  3024. taicpu(hp1).loadconst(0,taicpu(hp1).oper[0]^.ref^.offset);
  3025. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  3026. DebugMsg(SPeepholeOptimization + 'FoldLea done',hp1);
  3027. end
  3028. else
  3029. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  3030. end;
  3031. asml.remove(p);
  3032. asml.remove(hp2);
  3033. p.free;
  3034. hp2.free;
  3035. p := hp1
  3036. end;
  3037. Exit;
  3038. {$ifdef x86_64}
  3039. end
  3040. else if (taicpu(p).opsize = S_L) and
  3041. (taicpu(p).oper[1]^.typ = top_reg) and
  3042. (
  3043. GetNextInstruction(p, hp1) and
  3044. MatchInstruction(hp1, A_MOV,[]) and
  3045. (taicpu(hp1).opsize = S_L) and
  3046. (taicpu(hp1).oper[1]^.typ = top_reg)
  3047. ) and (
  3048. GetNextInstruction(hp1, hp2) and
  3049. (tai(hp2).typ=ait_instruction) and
  3050. (taicpu(hp2).opsize = S_Q) and
  3051. (
  3052. (
  3053. MatchInstruction(hp2, A_ADD,[]) and
  3054. (taicpu(hp2).opsize = S_Q) and
  3055. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  3056. (
  3057. (
  3058. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  3059. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  3060. ) or (
  3061. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3062. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  3063. )
  3064. )
  3065. ) or (
  3066. MatchInstruction(hp2, A_LEA,[]) and
  3067. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  3068. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  3069. (
  3070. (
  3071. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  3072. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  3073. ) or (
  3074. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3075. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  3076. )
  3077. ) and (
  3078. (
  3079. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  3080. ) or (
  3081. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  3082. )
  3083. )
  3084. )
  3085. )
  3086. ) and (
  3087. GetNextInstruction(hp2, hp3) and
  3088. MatchInstruction(hp3, A_SHR,[]) and
  3089. (taicpu(hp3).opsize = S_Q) and
  3090. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  3091. (taicpu(hp3).oper[0]^.val = 1) and
  3092. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  3093. ) then
  3094. begin
  3095. { Change movl x, reg1d movl x, reg1d
  3096. movl y, reg2d movl y, reg2d
  3097. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  3098. shrq $1, reg1q shrq $1, reg1q
  3099. ( reg1d and reg2d can be switched around in the first two instructions )
  3100. To movl x, reg1d
  3101. addl y, reg1d
  3102. rcrl $1, reg1d
  3103. This corresponds to the common expression (x + y) shr 1, where
  3104. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  3105. smaller code, but won't account for x + y causing an overflow). [Kit]
  3106. }
  3107. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  3108. { Change first MOV command to have the same register as the final output }
  3109. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  3110. else
  3111. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  3112. { Change second MOV command to an ADD command. This is easier than
  3113. converting the existing command because it means we don't have to
  3114. touch 'y', which might be a complicated reference, and also the
  3115. fact that the third command might either be ADD or LEA. [Kit] }
  3116. taicpu(hp1).opcode := A_ADD;
  3117. { Delete old ADD/LEA instruction }
  3118. asml.remove(hp2);
  3119. hp2.free;
  3120. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  3121. taicpu(hp3).opcode := A_RCR;
  3122. taicpu(hp3).changeopsize(S_L);
  3123. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  3124. {$endif x86_64}
  3125. end;
  3126. end;
  3127. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  3128. var
  3129. hp1 : tai;
  3130. begin
  3131. Result:=false;
  3132. if (taicpu(p).ops >= 2) and
  3133. ((taicpu(p).oper[0]^.typ = top_const) or
  3134. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  3135. (taicpu(p).oper[1]^.typ = top_reg) and
  3136. ((taicpu(p).ops = 2) or
  3137. ((taicpu(p).oper[2]^.typ = top_reg) and
  3138. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  3139. GetLastInstruction(p,hp1) and
  3140. MatchInstruction(hp1,A_MOV,[]) and
  3141. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3142. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3143. begin
  3144. TransferUsedRegs(TmpUsedRegs);
  3145. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) or
  3146. ((taicpu(p).ops = 3) and (taicpu(p).oper[1]^.reg=taicpu(p).oper[2]^.reg)) then
  3147. { change
  3148. mov reg1,reg2
  3149. imul y,reg2 to imul y,reg1,reg2 }
  3150. begin
  3151. taicpu(p).ops := 3;
  3152. taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
  3153. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  3154. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  3155. asml.remove(hp1);
  3156. hp1.free;
  3157. result:=true;
  3158. end;
  3159. end;
  3160. end;
  3161. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  3162. var
  3163. hp1 : tai;
  3164. begin
  3165. {
  3166. change
  3167. jmp .L1
  3168. ...
  3169. .L1:
  3170. ret
  3171. into
  3172. ret
  3173. }
  3174. result:=false;
  3175. if (taicpu(p).oper[0]^.typ=top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full) and (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  3176. (taicpu(p).oper[0]^.ref^.index=NR_NO) then
  3177. begin
  3178. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  3179. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and
  3180. MatchInstruction(hp1,A_RET,[S_NO]) then
  3181. begin
  3182. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  3183. taicpu(p).opcode:=A_RET;
  3184. taicpu(p).is_jmp:=false;
  3185. taicpu(p).ops:=taicpu(hp1).ops;
  3186. case taicpu(hp1).ops of
  3187. 0:
  3188. taicpu(p).clearop(0);
  3189. 1:
  3190. taicpu(p).loadconst(0,taicpu(hp1).oper[0]^.val);
  3191. else
  3192. internalerror(2016041301);
  3193. end;
  3194. result:=true;
  3195. end;
  3196. end;
  3197. end;
  3198. function CanBeCMOV(p : tai) : boolean;
  3199. begin
  3200. CanBeCMOV:=assigned(p) and
  3201. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  3202. { we can't use cmov ref,reg because
  3203. ref could be nil and cmov still throws an exception
  3204. if ref=nil but the mov isn't done (FK)
  3205. or ((taicpu(p).oper[0]^.typ = top_ref) and
  3206. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  3207. }
  3208. (MatchOpType(taicpu(p),top_reg,top_reg) or
  3209. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  3210. it is not expected that this can cause a seg. violation }
  3211. (MatchOpType(taicpu(p),top_ref,top_reg) and
  3212. (((taicpu(p).oper[0]^.ref^.base=NR_NO) and (taicpu(p).oper[0]^.ref^.refaddr=addr_no)){$ifdef x86_64} or
  3213. ((taicpu(p).oper[0]^.ref^.base=NR_RIP) and (taicpu(p).oper[0]^.ref^.refaddr=addr_pic)){$endif x86_64}
  3214. ) and
  3215. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  3216. (taicpu(p).oper[0]^.ref^.offset=0)
  3217. )
  3218. );
  3219. end;
  3220. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  3221. var
  3222. hp1,hp2,hp3,hp4,hpmov2: tai;
  3223. carryadd_opcode : TAsmOp;
  3224. l : Longint;
  3225. condition : TAsmCond;
  3226. symbol: TAsmSymbol;
  3227. begin
  3228. result:=false;
  3229. symbol:=nil;
  3230. if GetNextInstruction(p,hp1) then
  3231. begin
  3232. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  3233. if (hp1.typ=ait_instruction) and
  3234. GetNextInstruction(hp1,hp2) and (hp2.typ=ait_label) and
  3235. (Tasmlabel(symbol) = Tai_label(hp2).labsym) then
  3236. { jb @@1 cmc
  3237. inc/dec operand --> adc/sbb operand,0
  3238. @@1:
  3239. ... and ...
  3240. jnb @@1
  3241. inc/dec operand --> adc/sbb operand,0
  3242. @@1: }
  3243. begin
  3244. carryadd_opcode:=A_NONE;
  3245. if Taicpu(p).condition in [C_NAE,C_B] then
  3246. begin
  3247. if Taicpu(hp1).opcode=A_INC then
  3248. carryadd_opcode:=A_ADC;
  3249. if Taicpu(hp1).opcode=A_DEC then
  3250. carryadd_opcode:=A_SBB;
  3251. if carryadd_opcode<>A_NONE then
  3252. begin
  3253. Taicpu(p).clearop(0);
  3254. Taicpu(p).ops:=0;
  3255. Taicpu(p).is_jmp:=false;
  3256. Taicpu(p).opcode:=A_CMC;
  3257. Taicpu(p).condition:=C_NONE;
  3258. Taicpu(hp1).ops:=2;
  3259. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  3260. Taicpu(hp1).loadconst(0,0);
  3261. Taicpu(hp1).opcode:=carryadd_opcode;
  3262. result:=true;
  3263. exit;
  3264. end;
  3265. end;
  3266. if Taicpu(p).condition in [C_AE,C_NB] then
  3267. begin
  3268. if Taicpu(hp1).opcode=A_INC then
  3269. carryadd_opcode:=A_ADC;
  3270. if Taicpu(hp1).opcode=A_DEC then
  3271. carryadd_opcode:=A_SBB;
  3272. if carryadd_opcode<>A_NONE then
  3273. begin
  3274. asml.remove(p);
  3275. p.free;
  3276. Taicpu(hp1).ops:=2;
  3277. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  3278. Taicpu(hp1).loadconst(0,0);
  3279. Taicpu(hp1).opcode:=carryadd_opcode;
  3280. p:=hp1;
  3281. result:=true;
  3282. exit;
  3283. end;
  3284. end;
  3285. end;
  3286. { Detect the following:
  3287. jmp<cond> @Lbl1
  3288. jmp @Lbl2
  3289. ...
  3290. @Lbl1:
  3291. ret
  3292. Change to:
  3293. jmp<inv_cond> @Lbl2
  3294. ret
  3295. }
  3296. if MatchInstruction(hp1,A_JMP,[]) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  3297. begin
  3298. hp2:=getlabelwithsym(TAsmLabel(symbol));
  3299. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  3300. MatchInstruction(hp2,A_RET,[S_NO]) then
  3301. begin
  3302. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  3303. { Change label address to that of the unconditional jump }
  3304. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  3305. TAsmLabel(symbol).DecRefs;
  3306. taicpu(hp1).opcode := A_RET;
  3307. taicpu(hp1).is_jmp := false;
  3308. taicpu(hp1).ops := taicpu(hp2).ops;
  3309. DebugMsg(SPeepholeOptimization+'JccJmpRet2J!ccRet',p);
  3310. case taicpu(hp2).ops of
  3311. 0:
  3312. taicpu(hp1).clearop(0);
  3313. 1:
  3314. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  3315. else
  3316. internalerror(2016041302);
  3317. end;
  3318. end;
  3319. end;
  3320. end;
  3321. {$ifndef i8086}
  3322. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  3323. begin
  3324. { check for
  3325. jCC xxx
  3326. <several movs>
  3327. xxx:
  3328. }
  3329. l:=0;
  3330. GetNextInstruction(p, hp1);
  3331. while assigned(hp1) and
  3332. CanBeCMOV(hp1) and
  3333. { stop on labels }
  3334. not(hp1.typ=ait_label) do
  3335. begin
  3336. inc(l);
  3337. GetNextInstruction(hp1,hp1);
  3338. end;
  3339. if assigned(hp1) then
  3340. begin
  3341. if FindLabel(tasmlabel(symbol),hp1) then
  3342. begin
  3343. if (l<=4) and (l>0) then
  3344. begin
  3345. condition:=inverse_cond(taicpu(p).condition);
  3346. GetNextInstruction(p,hp1);
  3347. repeat
  3348. if not Assigned(hp1) then
  3349. InternalError(2018062900);
  3350. taicpu(hp1).opcode:=A_CMOVcc;
  3351. taicpu(hp1).condition:=condition;
  3352. UpdateUsedRegs(hp1);
  3353. GetNextInstruction(hp1,hp1);
  3354. until not(CanBeCMOV(hp1));
  3355. { Remember what hp1 is in case there's multiple aligns to get rid of }
  3356. hp2 := hp1;
  3357. repeat
  3358. if not Assigned(hp2) then
  3359. InternalError(2018062910);
  3360. case hp2.typ of
  3361. ait_label:
  3362. { What we expected - break out of the loop (it won't be a dead label at the top of
  3363. a cluster because that was optimised at an earlier stage) }
  3364. Break;
  3365. ait_align:
  3366. { Go to the next entry until a label is found (may be multiple aligns before it) }
  3367. begin
  3368. hp2 := tai(hp2.Next);
  3369. Continue;
  3370. end;
  3371. else
  3372. begin
  3373. { Might be a comment or temporary allocation entry }
  3374. if not (hp2.typ in SkipInstr) then
  3375. InternalError(2018062911);
  3376. hp2 := tai(hp2.Next);
  3377. Continue;
  3378. end;
  3379. end;
  3380. until False;
  3381. { Now we can safely decrement the reference count }
  3382. tasmlabel(symbol).decrefs;
  3383. DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
  3384. { Remove the original jump }
  3385. asml.Remove(p);
  3386. p.Free;
  3387. GetNextInstruction(hp2, p); { Instruction after the label }
  3388. { Remove the label if this is its final reference }
  3389. if (tasmlabel(symbol).getrefs=0) then
  3390. StripLabelFast(hp1);
  3391. if Assigned(p) then
  3392. begin
  3393. UpdateUsedRegs(p);
  3394. result:=true;
  3395. end;
  3396. exit;
  3397. end;
  3398. end
  3399. else
  3400. begin
  3401. { check further for
  3402. jCC xxx
  3403. <several movs 1>
  3404. jmp yyy
  3405. xxx:
  3406. <several movs 2>
  3407. yyy:
  3408. }
  3409. { hp2 points to jmp yyy }
  3410. hp2:=hp1;
  3411. { skip hp1 to xxx (or an align right before it) }
  3412. GetNextInstruction(hp1, hp1);
  3413. if assigned(hp2) and
  3414. assigned(hp1) and
  3415. (l<=3) and
  3416. (hp2.typ=ait_instruction) and
  3417. (taicpu(hp2).is_jmp) and
  3418. (taicpu(hp2).condition=C_None) and
  3419. { real label and jump, no further references to the
  3420. label are allowed }
  3421. (tasmlabel(symbol).getrefs=1) and
  3422. FindLabel(tasmlabel(symbol),hp1) then
  3423. begin
  3424. l:=0;
  3425. { skip hp1 to <several moves 2> }
  3426. if (hp1.typ = ait_align) then
  3427. GetNextInstruction(hp1, hp1);
  3428. GetNextInstruction(hp1, hpmov2);
  3429. hp1 := hpmov2;
  3430. while assigned(hp1) and
  3431. CanBeCMOV(hp1) do
  3432. begin
  3433. inc(l);
  3434. GetNextInstruction(hp1, hp1);
  3435. end;
  3436. { hp1 points to yyy (or an align right before it) }
  3437. hp3 := hp1;
  3438. if assigned(hp1) and
  3439. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  3440. begin
  3441. condition:=inverse_cond(taicpu(p).condition);
  3442. GetNextInstruction(p,hp1);
  3443. repeat
  3444. taicpu(hp1).opcode:=A_CMOVcc;
  3445. taicpu(hp1).condition:=condition;
  3446. UpdateUsedRegs(hp1);
  3447. GetNextInstruction(hp1,hp1);
  3448. until not(assigned(hp1)) or
  3449. not(CanBeCMOV(hp1));
  3450. condition:=inverse_cond(condition);
  3451. hp1 := hpmov2;
  3452. { hp1 is now at <several movs 2> }
  3453. while Assigned(hp1) and CanBeCMOV(hp1) do
  3454. begin
  3455. taicpu(hp1).opcode:=A_CMOVcc;
  3456. taicpu(hp1).condition:=condition;
  3457. UpdateUsedRegs(hp1);
  3458. GetNextInstruction(hp1,hp1);
  3459. end;
  3460. hp1 := p;
  3461. { Get first instruction after label }
  3462. GetNextInstruction(hp3, p);
  3463. if assigned(p) and (hp3.typ = ait_align) then
  3464. GetNextInstruction(p, p);
  3465. { Don't dereference yet, as doing so will cause
  3466. GetNextInstruction to skip the label and
  3467. optional align marker. [Kit] }
  3468. GetNextInstruction(hp2, hp4);
  3469. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
  3470. { remove jCC }
  3471. asml.remove(hp1);
  3472. hp1.free;
  3473. { Now we can safely decrement it }
  3474. tasmlabel(symbol).decrefs;
  3475. { Remove label xxx (it will have a ref of zero due to the initial check }
  3476. StripLabelFast(hp4);
  3477. { remove jmp }
  3478. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  3479. asml.remove(hp2);
  3480. hp2.free;
  3481. { As before, now we can safely decrement it }
  3482. tasmlabel(symbol).decrefs;
  3483. { Remove label yyy (and the optional alignment) if its reference falls to zero }
  3484. if tasmlabel(symbol).getrefs = 0 then
  3485. StripLabelFast(hp3);
  3486. if Assigned(p) then
  3487. begin
  3488. UpdateUsedRegs(p);
  3489. result:=true;
  3490. end;
  3491. exit;
  3492. end;
  3493. end;
  3494. end;
  3495. end;
  3496. end;
  3497. {$endif i8086}
  3498. end;
  3499. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  3500. var
  3501. hp1,hp2: tai;
  3502. begin
  3503. result:=false;
  3504. if (taicpu(p).oper[1]^.typ = top_reg) and
  3505. GetNextInstruction(p,hp1) and
  3506. (hp1.typ = ait_instruction) and
  3507. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  3508. GetNextInstruction(hp1,hp2) and
  3509. MatchInstruction(hp2,A_MOV,[]) and
  3510. (taicpu(hp2).oper[0]^.typ = top_reg) and
  3511. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  3512. {$ifdef i386}
  3513. { not all registers have byte size sub registers on i386 }
  3514. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  3515. {$endif i386}
  3516. (((taicpu(hp1).ops=2) and
  3517. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  3518. ((taicpu(hp1).ops=1) and
  3519. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  3520. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  3521. begin
  3522. { change movsX/movzX reg/ref, reg2
  3523. add/sub/or/... reg3/$const, reg2
  3524. mov reg2 reg/ref
  3525. to add/sub/or/... reg3/$const, reg/ref }
  3526. { by example:
  3527. movswl %si,%eax movswl %si,%eax p
  3528. decl %eax addl %edx,%eax hp1
  3529. movw %ax,%si movw %ax,%si hp2
  3530. ->
  3531. movswl %si,%eax movswl %si,%eax p
  3532. decw %eax addw %edx,%eax hp1
  3533. movw %ax,%si movw %ax,%si hp2
  3534. }
  3535. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  3536. {
  3537. ->
  3538. movswl %si,%eax movswl %si,%eax p
  3539. decw %si addw %dx,%si hp1
  3540. movw %ax,%si movw %ax,%si hp2
  3541. }
  3542. case taicpu(hp1).ops of
  3543. 1:
  3544. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  3545. 2:
  3546. begin
  3547. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  3548. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  3549. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  3550. end;
  3551. else
  3552. internalerror(2008042701);
  3553. end;
  3554. {
  3555. ->
  3556. decw %si addw %dx,%si p
  3557. }
  3558. DebugMsg(SPeepholeOptimization + 'var3',p);
  3559. asml.remove(p);
  3560. asml.remove(hp2);
  3561. p.free;
  3562. hp2.free;
  3563. p:=hp1;
  3564. end
  3565. else if taicpu(p).opcode=A_MOVZX then
  3566. begin
  3567. { removes superfluous And's after movzx's }
  3568. if (taicpu(p).oper[1]^.typ = top_reg) and
  3569. GetNextInstruction(p, hp1) and
  3570. (tai(hp1).typ = ait_instruction) and
  3571. (taicpu(hp1).opcode = A_AND) and
  3572. (taicpu(hp1).oper[0]^.typ = top_const) and
  3573. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3574. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3575. begin
  3576. case taicpu(p).opsize Of
  3577. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  3578. if (taicpu(hp1).oper[0]^.val = $ff) then
  3579. begin
  3580. DebugMsg(SPeepholeOptimization + 'var4',p);
  3581. asml.remove(hp1);
  3582. hp1.free;
  3583. end;
  3584. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  3585. if (taicpu(hp1).oper[0]^.val = $ffff) then
  3586. begin
  3587. DebugMsg(SPeepholeOptimization + 'var5',p);
  3588. asml.remove(hp1);
  3589. hp1.free;
  3590. end;
  3591. {$ifdef x86_64}
  3592. S_LQ:
  3593. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  3594. begin
  3595. if (cs_asm_source in current_settings.globalswitches) then
  3596. asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
  3597. asml.remove(hp1);
  3598. hp1.Free;
  3599. end;
  3600. {$endif x86_64}
  3601. else
  3602. ;
  3603. end;
  3604. end;
  3605. { changes some movzx constructs to faster synonims (all examples
  3606. are given with eax/ax, but are also valid for other registers)}
  3607. if (taicpu(p).oper[1]^.typ = top_reg) then
  3608. if (taicpu(p).oper[0]^.typ = top_reg) then
  3609. case taicpu(p).opsize of
  3610. S_BW:
  3611. begin
  3612. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3613. not(cs_opt_size in current_settings.optimizerswitches) then
  3614. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  3615. begin
  3616. taicpu(p).opcode := A_AND;
  3617. taicpu(p).changeopsize(S_W);
  3618. taicpu(p).loadConst(0,$ff);
  3619. DebugMsg(SPeepholeOptimization + 'var7',p);
  3620. end
  3621. else if GetNextInstruction(p, hp1) and
  3622. (tai(hp1).typ = ait_instruction) and
  3623. (taicpu(hp1).opcode = A_AND) and
  3624. (taicpu(hp1).oper[0]^.typ = top_const) and
  3625. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3626. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3627. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  3628. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  3629. begin
  3630. DebugMsg(SPeepholeOptimization + 'var8',p);
  3631. taicpu(p).opcode := A_MOV;
  3632. taicpu(p).changeopsize(S_W);
  3633. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  3634. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3635. end;
  3636. end;
  3637. S_BL:
  3638. begin
  3639. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3640. not(cs_opt_size in current_settings.optimizerswitches) then
  3641. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  3642. begin
  3643. taicpu(p).opcode := A_AND;
  3644. taicpu(p).changeopsize(S_L);
  3645. taicpu(p).loadConst(0,$ff)
  3646. end
  3647. else if GetNextInstruction(p, hp1) and
  3648. (tai(hp1).typ = ait_instruction) and
  3649. (taicpu(hp1).opcode = A_AND) and
  3650. (taicpu(hp1).oper[0]^.typ = top_const) and
  3651. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3652. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3653. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  3654. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  3655. begin
  3656. DebugMsg(SPeepholeOptimization + 'var10',p);
  3657. taicpu(p).opcode := A_MOV;
  3658. taicpu(p).changeopsize(S_L);
  3659. { do not use R_SUBWHOLE
  3660. as movl %rdx,%eax
  3661. is invalid in assembler PM }
  3662. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3663. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3664. end
  3665. end;
  3666. {$ifndef i8086}
  3667. S_WL:
  3668. begin
  3669. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3670. not(cs_opt_size in current_settings.optimizerswitches) then
  3671. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  3672. begin
  3673. DebugMsg(SPeepholeOptimization + 'var11',p);
  3674. taicpu(p).opcode := A_AND;
  3675. taicpu(p).changeopsize(S_L);
  3676. taicpu(p).loadConst(0,$ffff);
  3677. end
  3678. else if GetNextInstruction(p, hp1) and
  3679. (tai(hp1).typ = ait_instruction) and
  3680. (taicpu(hp1).opcode = A_AND) and
  3681. (taicpu(hp1).oper[0]^.typ = top_const) and
  3682. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3683. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3684. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  3685. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  3686. begin
  3687. DebugMsg(SPeepholeOptimization + 'var12',p);
  3688. taicpu(p).opcode := A_MOV;
  3689. taicpu(p).changeopsize(S_L);
  3690. { do not use R_SUBWHOLE
  3691. as movl %rdx,%eax
  3692. is invalid in assembler PM }
  3693. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3694. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  3695. end;
  3696. end;
  3697. {$endif i8086}
  3698. else
  3699. ;
  3700. end
  3701. else if (taicpu(p).oper[0]^.typ = top_ref) then
  3702. begin
  3703. if GetNextInstruction(p, hp1) and
  3704. (tai(hp1).typ = ait_instruction) and
  3705. (taicpu(hp1).opcode = A_AND) and
  3706. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3707. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3708. begin
  3709. //taicpu(p).opcode := A_MOV;
  3710. case taicpu(p).opsize Of
  3711. S_BL:
  3712. begin
  3713. DebugMsg(SPeepholeOptimization + 'var13',p);
  3714. taicpu(hp1).changeopsize(S_L);
  3715. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3716. end;
  3717. S_WL:
  3718. begin
  3719. DebugMsg(SPeepholeOptimization + 'var14',p);
  3720. taicpu(hp1).changeopsize(S_L);
  3721. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  3722. end;
  3723. S_BW:
  3724. begin
  3725. DebugMsg(SPeepholeOptimization + 'var15',p);
  3726. taicpu(hp1).changeopsize(S_W);
  3727. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3728. end;
  3729. {$ifdef x86_64}
  3730. S_BQ:
  3731. begin
  3732. DebugMsg(SPeepholeOptimization + 'var16',p);
  3733. taicpu(hp1).changeopsize(S_Q);
  3734. taicpu(hp1).loadConst(
  3735. 0, taicpu(hp1).oper[0]^.val and $ff);
  3736. end;
  3737. S_WQ:
  3738. begin
  3739. DebugMsg(SPeepholeOptimization + 'var17',p);
  3740. taicpu(hp1).changeopsize(S_Q);
  3741. taicpu(hp1).loadConst(0, taicpu(hp1).oper[0]^.val and $ffff);
  3742. end;
  3743. S_LQ:
  3744. begin
  3745. DebugMsg(SPeepholeOptimization + 'var18',p);
  3746. taicpu(hp1).changeopsize(S_Q);
  3747. taicpu(hp1).loadConst(
  3748. 0, taicpu(hp1).oper[0]^.val and $ffffffff);
  3749. end;
  3750. {$endif x86_64}
  3751. else
  3752. Internalerror(2017050704)
  3753. end;
  3754. end;
  3755. end;
  3756. end;
  3757. end;
  3758. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  3759. var
  3760. hp1 : tai;
  3761. MaskLength : Cardinal;
  3762. begin
  3763. Result:=false;
  3764. if GetNextInstruction(p, hp1) then
  3765. begin
  3766. if MatchOpType(taicpu(p),top_const,top_reg) and
  3767. MatchInstruction(hp1,A_AND,[]) and
  3768. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3769. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3770. { the second register must contain the first one, so compare their subreg types }
  3771. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  3772. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  3773. { change
  3774. and const1, reg
  3775. and const2, reg
  3776. to
  3777. and (const1 and const2), reg
  3778. }
  3779. begin
  3780. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  3781. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  3782. asml.remove(p);
  3783. p.Free;
  3784. p:=hp1;
  3785. Result:=true;
  3786. exit;
  3787. end
  3788. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3789. MatchInstruction(hp1,A_MOVZX,[]) and
  3790. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3791. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3792. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3793. (((taicpu(p).opsize=S_W) and
  3794. (taicpu(hp1).opsize=S_BW)) or
  3795. ((taicpu(p).opsize=S_L) and
  3796. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3797. {$ifdef x86_64}
  3798. or
  3799. ((taicpu(p).opsize=S_Q) and
  3800. (taicpu(hp1).opsize in [S_BQ,S_WQ]))
  3801. {$endif x86_64}
  3802. ) then
  3803. begin
  3804. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3805. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  3806. ) or
  3807. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3808. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  3809. then
  3810. begin
  3811. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  3812. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  3813. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  3814. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  3815. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  3816. }
  3817. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  3818. asml.remove(hp1);
  3819. hp1.free;
  3820. Exit;
  3821. end;
  3822. end
  3823. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3824. MatchInstruction(hp1,A_SHL,[]) and
  3825. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3826. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  3827. begin
  3828. {$ifopt R+}
  3829. {$define RANGE_WAS_ON}
  3830. {$R-}
  3831. {$endif}
  3832. { get length of potential and mask }
  3833. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  3834. { really a mask? }
  3835. {$ifdef RANGE_WAS_ON}
  3836. {$R+}
  3837. {$endif}
  3838. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  3839. { unmasked part shifted out? }
  3840. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  3841. begin
  3842. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  3843. { take care of the register (de)allocs following p }
  3844. UpdateUsedRegs(tai(p.next));
  3845. asml.remove(p);
  3846. p.free;
  3847. p:=hp1;
  3848. Result:=true;
  3849. exit;
  3850. end;
  3851. end
  3852. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3853. MatchInstruction(hp1,A_MOVSX{$ifdef x86_64},A_MOVSXD{$endif x86_64},[]) and
  3854. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3855. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3856. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3857. (((taicpu(p).opsize=S_W) and
  3858. (taicpu(hp1).opsize=S_BW)) or
  3859. ((taicpu(p).opsize=S_L) and
  3860. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3861. {$ifdef x86_64}
  3862. or
  3863. ((taicpu(p).opsize=S_Q) and
  3864. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_LQ]))
  3865. {$endif x86_64}
  3866. ) then
  3867. begin
  3868. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3869. ((taicpu(p).oper[0]^.val and $7f)=taicpu(p).oper[0]^.val)
  3870. ) or
  3871. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3872. ((taicpu(p).oper[0]^.val and $7fff)=taicpu(p).oper[0]^.val))
  3873. {$ifdef x86_64}
  3874. or
  3875. (((taicpu(hp1).opsize)=S_LQ) and
  3876. ((taicpu(p).oper[0]^.val and $7fffffff)=taicpu(p).oper[0]^.val)
  3877. )
  3878. {$endif x86_64}
  3879. then
  3880. begin
  3881. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  3882. asml.remove(hp1);
  3883. hp1.free;
  3884. Exit;
  3885. end;
  3886. end
  3887. else if (taicpu(p).oper[1]^.typ = top_reg) and
  3888. (hp1.typ = ait_instruction) and
  3889. (taicpu(hp1).is_jmp) and
  3890. (taicpu(hp1).opcode<>A_JMP) and
  3891. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  3892. begin
  3893. { change
  3894. and x, reg
  3895. jxx
  3896. to
  3897. test x, reg
  3898. jxx
  3899. if reg is deallocated before the
  3900. jump, but only if it's a conditional jump (PFV)
  3901. }
  3902. taicpu(p).opcode := A_TEST;
  3903. Exit;
  3904. end;
  3905. end;
  3906. { Lone AND tests }
  3907. if MatchOpType(taicpu(p),top_const,top_reg) then
  3908. begin
  3909. {
  3910. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  3911. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  3912. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  3913. }
  3914. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  3915. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  3916. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  3917. begin
  3918. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg)
  3919. end;
  3920. end;
  3921. end;
  3922. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  3923. begin
  3924. Result:=false;
  3925. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3926. MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  3927. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  3928. begin
  3929. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  3930. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  3931. taicpu(p).opcode:=A_ADD;
  3932. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  3933. result:=true;
  3934. end
  3935. else if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3936. MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  3937. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  3938. begin
  3939. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  3940. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  3941. taicpu(p).opcode:=A_ADD;
  3942. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  3943. result:=true;
  3944. end;
  3945. end;
  3946. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  3947. function SkipSimpleInstructions(var hp1 : tai) : Boolean;
  3948. begin
  3949. { we can skip all instructions not messing with the stack pointer }
  3950. while assigned(hp1) and {MatchInstruction(taicpu(hp1),[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  3951. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  3952. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  3953. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  3954. ({(taicpu(hp1).ops=0) or }
  3955. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  3956. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  3957. ) and }
  3958. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  3959. )
  3960. ) do
  3961. GetNextInstruction(hp1,hp1);
  3962. Result:=assigned(hp1);
  3963. end;
  3964. var
  3965. hp1, hp2, hp3: tai;
  3966. begin
  3967. Result:=false;
  3968. { replace
  3969. leal(q) x(<stackpointer>),<stackpointer>
  3970. call procname
  3971. leal(q) -x(<stackpointer>),<stackpointer>
  3972. ret
  3973. by
  3974. jmp procname
  3975. but do it only on level 4 because it destroys stack back traces
  3976. }
  3977. if (cs_opt_level4 in current_settings.optimizerswitches) and
  3978. MatchOpType(taicpu(p),top_ref,top_reg) and
  3979. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  3980. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  3981. { the -8 or -24 are not required, but bail out early if possible,
  3982. higher values are unlikely }
  3983. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  3984. (taicpu(p).oper[0]^.ref^.offset=-24)) and
  3985. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  3986. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  3987. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  3988. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  3989. GetNextInstruction(p, hp1) and
  3990. { trick to skip label }
  3991. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  3992. SkipSimpleInstructions(hp1) and
  3993. MatchInstruction(hp1,A_CALL,[S_NO]) and
  3994. GetNextInstruction(hp1, hp2) and
  3995. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  3996. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  3997. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  3998. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  3999. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  4000. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  4001. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  4002. (taicpu(hp2).oper[0]^.ref^.segment=NR_NO) and
  4003. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  4004. GetNextInstruction(hp2, hp3) and
  4005. { trick to skip label }
  4006. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  4007. MatchInstruction(hp3,A_RET,[S_NO]) and
  4008. (taicpu(hp3).ops=0) then
  4009. begin
  4010. taicpu(hp1).opcode := A_JMP;
  4011. taicpu(hp1).is_jmp := true;
  4012. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  4013. RemoveCurrentP(p);
  4014. AsmL.Remove(hp2);
  4015. hp2.free;
  4016. AsmL.Remove(hp3);
  4017. hp3.free;
  4018. Result:=true;
  4019. end;
  4020. end;
  4021. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  4022. var
  4023. Value, RegName: string;
  4024. begin
  4025. Result:=false;
  4026. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  4027. begin
  4028. case taicpu(p).oper[0]^.val of
  4029. 0:
  4030. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  4031. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  4032. begin
  4033. { change "mov $0,%reg" into "xor %reg,%reg" }
  4034. taicpu(p).opcode := A_XOR;
  4035. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  4036. Result := True;
  4037. end;
  4038. $1..$FFFFFFFF:
  4039. begin
  4040. { Code size reduction by J. Gareth "Kit" Moreton }
  4041. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  4042. case taicpu(p).opsize of
  4043. S_Q:
  4044. begin
  4045. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  4046. Value := debug_tostr(taicpu(p).oper[0]^.val);
  4047. { The actual optimization }
  4048. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  4049. taicpu(p).changeopsize(S_L);
  4050. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  4051. Result := True;
  4052. end;
  4053. else
  4054. ;
  4055. end;
  4056. end;
  4057. end;
  4058. end;
  4059. end;
  4060. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  4061. begin
  4062. Result:=false;
  4063. { change "cmp $0, %reg" to "test %reg, %reg" }
  4064. if MatchOpType(taicpu(p),top_const,top_reg) and
  4065. (taicpu(p).oper[0]^.val = 0) then
  4066. begin
  4067. taicpu(p).opcode := A_TEST;
  4068. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  4069. Result:=true;
  4070. end;
  4071. end;
  4072. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  4073. var
  4074. IsTestConstX : Boolean;
  4075. hp1,hp2 : tai;
  4076. begin
  4077. Result:=false;
  4078. { removes the line marked with (x) from the sequence
  4079. and/or/xor/add/sub/... $x, %y
  4080. test/or %y, %y | test $-1, %y (x)
  4081. j(n)z _Label
  4082. as the first instruction already adjusts the ZF
  4083. %y operand may also be a reference }
  4084. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  4085. MatchOperand(taicpu(p).oper[0]^,-1);
  4086. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  4087. GetLastInstruction(p, hp1) and
  4088. (tai(hp1).typ = ait_instruction) and
  4089. GetNextInstruction(p,hp2) and
  4090. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  4091. case taicpu(hp1).opcode Of
  4092. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  4093. begin
  4094. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  4095. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  4096. { and in case of carry for A(E)/B(E)/C/NC }
  4097. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  4098. ((taicpu(hp1).opcode <> A_ADD) and
  4099. (taicpu(hp1).opcode <> A_SUB))) then
  4100. begin
  4101. hp1 := tai(p.next);
  4102. asml.remove(p);
  4103. p.free;
  4104. p := tai(hp1);
  4105. Result:=true;
  4106. end;
  4107. end;
  4108. A_SHL, A_SAL, A_SHR, A_SAR:
  4109. begin
  4110. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  4111. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  4112. { therefore, it's only safe to do this optimization for }
  4113. { shifts by a (nonzero) constant }
  4114. (taicpu(hp1).oper[0]^.typ = top_const) and
  4115. (taicpu(hp1).oper[0]^.val <> 0) and
  4116. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  4117. { and in case of carry for A(E)/B(E)/C/NC }
  4118. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  4119. begin
  4120. hp1 := tai(p.next);
  4121. asml.remove(p);
  4122. p.free;
  4123. p := tai(hp1);
  4124. Result:=true;
  4125. end;
  4126. end;
  4127. A_DEC, A_INC, A_NEG:
  4128. begin
  4129. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  4130. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  4131. { and in case of carry for A(E)/B(E)/C/NC }
  4132. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  4133. begin
  4134. case taicpu(hp1).opcode of
  4135. A_DEC, A_INC:
  4136. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  4137. begin
  4138. case taicpu(hp1).opcode Of
  4139. A_DEC: taicpu(hp1).opcode := A_SUB;
  4140. A_INC: taicpu(hp1).opcode := A_ADD;
  4141. else
  4142. ;
  4143. end;
  4144. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  4145. taicpu(hp1).loadConst(0,1);
  4146. taicpu(hp1).ops:=2;
  4147. end;
  4148. else
  4149. ;
  4150. end;
  4151. hp1 := tai(p.next);
  4152. asml.remove(p);
  4153. p.free;
  4154. p := tai(hp1);
  4155. Result:=true;
  4156. end;
  4157. end
  4158. else
  4159. { change "test $-1,%reg" into "test %reg,%reg" }
  4160. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  4161. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  4162. end { case }
  4163. { change "test $-1,%reg" into "test %reg,%reg" }
  4164. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  4165. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  4166. end;
  4167. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  4168. var
  4169. hp1 : tai;
  4170. {$ifndef x86_64}
  4171. hp2 : taicpu;
  4172. {$endif x86_64}
  4173. begin
  4174. Result:=false;
  4175. {$ifndef x86_64}
  4176. { don't do this on modern CPUs, this really hurts them due to
  4177. broken call/ret pairing }
  4178. if (current_settings.optimizecputype < cpu_Pentium2) and
  4179. not(cs_create_pic in current_settings.moduleswitches) and
  4180. GetNextInstruction(p, hp1) and
  4181. MatchInstruction(hp1,A_JMP,[S_NO]) and
  4182. MatchOpType(taicpu(hp1),top_ref) and
  4183. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  4184. begin
  4185. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  4186. InsertLLItem(p.previous, p, hp2);
  4187. taicpu(p).opcode := A_JMP;
  4188. taicpu(p).is_jmp := true;
  4189. asml.remove(hp1);
  4190. hp1.free;
  4191. Result:=true;
  4192. end
  4193. else
  4194. {$endif x86_64}
  4195. { replace
  4196. call procname
  4197. ret
  4198. by
  4199. jmp procname
  4200. but do it only on level 4 because it destroys stack back traces
  4201. }
  4202. if (cs_opt_level4 in current_settings.optimizerswitches) and
  4203. GetNextInstruction(p, hp1) and
  4204. MatchInstruction(hp1,A_RET,[S_NO]) and
  4205. (taicpu(hp1).ops=0) then
  4206. begin
  4207. taicpu(p).opcode := A_JMP;
  4208. taicpu(p).is_jmp := true;
  4209. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  4210. asml.remove(hp1);
  4211. hp1.free;
  4212. Result:=true;
  4213. end;
  4214. end;
  4215. {$ifdef x86_64}
  4216. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  4217. var
  4218. PreMessage: string;
  4219. begin
  4220. Result := False;
  4221. { Code size reduction by J. Gareth "Kit" Moreton }
  4222. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  4223. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  4224. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  4225. then
  4226. begin
  4227. { Has 64-bit register name and opcode suffix }
  4228. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  4229. { The actual optimization }
  4230. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  4231. if taicpu(p).opsize = S_BQ then
  4232. taicpu(p).changeopsize(S_BL)
  4233. else
  4234. taicpu(p).changeopsize(S_WL);
  4235. DebugMsg(SPeepholeOptimization + PreMessage +
  4236. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  4237. end;
  4238. end;
  4239. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  4240. var
  4241. PreMessage, RegName: string;
  4242. begin
  4243. { Code size reduction by J. Gareth "Kit" Moreton }
  4244. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  4245. as this removes the REX prefix }
  4246. Result := False;
  4247. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  4248. Exit;
  4249. if taicpu(p).oper[0]^.typ <> top_reg then
  4250. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  4251. InternalError(2018011500);
  4252. case taicpu(p).opsize of
  4253. S_Q:
  4254. begin
  4255. if (getsupreg(taicpu(p).oper[0]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP]) then
  4256. begin
  4257. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  4258. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  4259. { The actual optimization }
  4260. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  4261. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  4262. taicpu(p).changeopsize(S_L);
  4263. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  4264. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  4265. end;
  4266. end;
  4267. else
  4268. ;
  4269. end;
  4270. end;
  4271. {$endif}
  4272. procedure TX86AsmOptimizer.OptReferences;
  4273. var
  4274. p: tai;
  4275. i: Integer;
  4276. begin
  4277. p := BlockStart;
  4278. while (p <> BlockEnd) Do
  4279. begin
  4280. if p.typ=ait_instruction then
  4281. begin
  4282. for i:=0 to taicpu(p).ops-1 do
  4283. if taicpu(p).oper[i]^.typ=top_ref then
  4284. optimize_ref(taicpu(p).oper[i]^.ref^,false);
  4285. end;
  4286. p:=tai(p.next);
  4287. end;
  4288. end;
  4289. end.