aoptx86.pas 253 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973
  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. function RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean; override;
  35. protected
  36. class function IsMOVZXAcceptable: Boolean; static; inline;
  37. { checks whether loading a new value in reg1 overwrites the entirety of reg2 }
  38. function Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  39. { checks whether reading the value in reg1 depends on the value of reg2. This
  40. is very similar to SuperRegisterEquals, except it takes into account that
  41. R_SUBH and R_SUBL are independendent (e.g. reading from AL does not
  42. depend on the value in AH). }
  43. function Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  44. { Replaces all references to AOldReg in a memory reference to ANewReg }
  45. class function ReplaceRegisterInRef(var ref: TReference; const AOldReg, ANewReg: TRegister): Boolean; static;
  46. { Replaces all references to AOldReg in an operand to ANewReg }
  47. class function ReplaceRegisterInOper(const p: taicpu; const OperIdx: Integer; const AOldReg, ANewReg: TRegister): Boolean; static;
  48. { Replaces all references to AOldReg in an instruction to ANewReg,
  49. except where the register is being written }
  50. function ReplaceRegisterInInstruction(const p: taicpu; const AOldReg, ANewReg: TRegister): Boolean;
  51. { Returns true if the reference only refers to ESP or EBP (or their 64-bit equivalents),
  52. or writes to a global symbol }
  53. class function IsRefSafe(const ref: PReference): Boolean; static; inline;
  54. { Returns true if the given MOV instruction can be safely converted to CMOV }
  55. class function CanBeCMOV(p : tai) : boolean; static;
  56. function DeepMOVOpt(const p_mov: taicpu; const hp: taicpu): Boolean;
  57. procedure DebugMsg(const s : string; p : tai);inline;
  58. class function IsExitCode(p : tai) : boolean; static;
  59. class function isFoldableArithOp(hp1 : taicpu; reg : tregister) : boolean; static;
  60. procedure RemoveLastDeallocForFuncRes(p : tai);
  61. function DoSubAddOpt(var p : tai) : Boolean;
  62. function PrePeepholeOptSxx(var p : tai) : boolean;
  63. function PrePeepholeOptIMUL(var p : tai) : boolean;
  64. function OptPass1AND(var p : tai) : boolean;
  65. function OptPass1_V_MOVAP(var p : tai) : boolean;
  66. function OptPass1VOP(var p : tai) : boolean;
  67. function OptPass1MOV(var p : tai) : boolean;
  68. function OptPass1Movx(var p : tai) : boolean;
  69. function OptPass1MOVXX(var p : tai) : boolean;
  70. function OptPass1OP(var p : tai) : boolean;
  71. function OptPass1LEA(var p : tai) : boolean;
  72. function OptPass1Sub(var p : tai) : boolean;
  73. function OptPass1SHLSAL(var p : tai) : boolean;
  74. function OptPass1SETcc(var p : tai) : boolean;
  75. function OptPass1FSTP(var p : tai) : boolean;
  76. function OptPass1FLD(var p : tai) : boolean;
  77. function OptPass1Cmp(var p : tai) : boolean;
  78. function OptPass2MOV(var p : tai) : boolean;
  79. function OptPass2Imul(var p : tai) : boolean;
  80. function OptPass2Jmp(var p : tai) : boolean;
  81. function OptPass2Jcc(var p : tai) : boolean;
  82. function OptPass2Lea(var p: tai): Boolean;
  83. function OptPass2SUB(var p: tai): Boolean;
  84. function PostPeepholeOptMov(var p : tai) : Boolean;
  85. {$ifdef x86_64} { These post-peephole optimisations only affect 64-bit registers. [Kit] }
  86. function PostPeepholeOptMovzx(var p : tai) : Boolean;
  87. function PostPeepholeOptXor(var p : tai) : Boolean;
  88. {$endif}
  89. function PostPeepholeOptMOVSX(var p : tai) : boolean;
  90. function PostPeepholeOptCmp(var p : tai) : Boolean;
  91. function PostPeepholeOptTestOr(var p : tai) : Boolean;
  92. function PostPeepholeOptCall(var p : tai) : Boolean;
  93. function PostPeepholeOptLea(var p : tai) : Boolean;
  94. procedure ConvertJumpToRET(const p: tai; const ret_p: tai);
  95. { Processor-dependent reference optimisation }
  96. class procedure OptimizeRefs(var p: taicpu); static;
  97. end;
  98. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  99. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  100. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  101. function MatchInstruction(const instr: tai; const ops: array of TAsmOp; const opsize: topsizes): boolean;
  102. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  103. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  104. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  105. function RefsEqual(const r1, r2: treference): boolean;
  106. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  107. { returns true, if ref is a reference using only the registers passed as base and index
  108. and having an offset }
  109. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  110. implementation
  111. uses
  112. cutils,verbose,
  113. globals,
  114. cpuinfo,
  115. procinfo,
  116. aasmbase,
  117. aoptutils,
  118. symconst,symsym,
  119. cgx86,
  120. itcpugas;
  121. {$ifdef DEBUG_AOPTCPU}
  122. const
  123. SPeepholeOptimization: shortstring = 'Peephole Optimization: ';
  124. {$else DEBUG_AOPTCPU}
  125. { Empty strings help the optimizer to remove string concatenations that won't
  126. ever appear to the user on release builds. [Kit] }
  127. const
  128. SPeepholeOptimization = '';
  129. {$endif DEBUG_AOPTCPU}
  130. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  131. begin
  132. result :=
  133. (instr.typ = ait_instruction) and
  134. (taicpu(instr).opcode = op) and
  135. ((opsize = []) or (taicpu(instr).opsize in opsize));
  136. end;
  137. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  138. begin
  139. result :=
  140. (instr.typ = ait_instruction) and
  141. ((taicpu(instr).opcode = op1) or
  142. (taicpu(instr).opcode = op2)
  143. ) and
  144. ((opsize = []) or (taicpu(instr).opsize in opsize));
  145. end;
  146. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  147. begin
  148. result :=
  149. (instr.typ = ait_instruction) and
  150. ((taicpu(instr).opcode = op1) or
  151. (taicpu(instr).opcode = op2) or
  152. (taicpu(instr).opcode = op3)
  153. ) and
  154. ((opsize = []) or (taicpu(instr).opsize in opsize));
  155. end;
  156. function MatchInstruction(const instr : tai;const ops : array of TAsmOp;
  157. const opsize : topsizes) : boolean;
  158. var
  159. op : TAsmOp;
  160. begin
  161. result:=false;
  162. for op in ops do
  163. begin
  164. if (instr.typ = ait_instruction) and
  165. (taicpu(instr).opcode = op) and
  166. ((opsize = []) or (taicpu(instr).opsize in opsize)) then
  167. begin
  168. result:=true;
  169. exit;
  170. end;
  171. end;
  172. end;
  173. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  174. begin
  175. result := (oper.typ = top_reg) and (oper.reg = reg);
  176. end;
  177. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  178. begin
  179. result := (oper.typ = top_const) and (oper.val = a);
  180. end;
  181. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  182. begin
  183. result := oper1.typ = oper2.typ;
  184. if result then
  185. case oper1.typ of
  186. top_const:
  187. Result:=oper1.val = oper2.val;
  188. top_reg:
  189. Result:=oper1.reg = oper2.reg;
  190. top_ref:
  191. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  192. else
  193. internalerror(2013102801);
  194. end
  195. end;
  196. function RefsEqual(const r1, r2: treference): boolean;
  197. begin
  198. RefsEqual :=
  199. (r1.offset = r2.offset) and
  200. (r1.segment = r2.segment) and (r1.base = r2.base) and
  201. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  202. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  203. (r1.relsymbol = r2.relsymbol) and
  204. (r1.volatility=[]) and
  205. (r2.volatility=[]);
  206. end;
  207. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  208. begin
  209. Result:=(ref.offset=0) and
  210. (ref.scalefactor in [0,1]) and
  211. (ref.segment=NR_NO) and
  212. (ref.symbol=nil) and
  213. (ref.relsymbol=nil) and
  214. ((base=NR_INVALID) or
  215. (ref.base=base)) and
  216. ((index=NR_INVALID) or
  217. (ref.index=index)) and
  218. (ref.volatility=[]);
  219. end;
  220. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  221. begin
  222. Result:=(ref.scalefactor in [0,1]) and
  223. (ref.segment=NR_NO) and
  224. (ref.symbol=nil) and
  225. (ref.relsymbol=nil) and
  226. ((base=NR_INVALID) or
  227. (ref.base=base)) and
  228. ((index=NR_INVALID) or
  229. (ref.index=index)) and
  230. (ref.volatility=[]);
  231. end;
  232. function InstrReadsFlags(p: tai): boolean;
  233. begin
  234. InstrReadsFlags := true;
  235. case p.typ of
  236. ait_instruction:
  237. if InsProp[taicpu(p).opcode].Ch*
  238. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  239. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  240. Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc,Ch_All]<>[] then
  241. exit;
  242. ait_label:
  243. exit;
  244. else
  245. ;
  246. end;
  247. InstrReadsFlags := false;
  248. end;
  249. function TX86AsmOptimizer.GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  250. begin
  251. Next:=Current;
  252. repeat
  253. Result:=GetNextInstruction(Next,Next);
  254. until not (Result) or
  255. not(cs_opt_level3 in current_settings.optimizerswitches) or
  256. (Next.typ<>ait_instruction) or
  257. RegInInstruction(reg,Next) or
  258. is_calljmp(taicpu(Next).opcode);
  259. end;
  260. function TX86AsmOptimizer.InstructionLoadsFromReg(const reg: TRegister;const hp: tai): boolean;
  261. begin
  262. Result:=RegReadByInstruction(reg,hp);
  263. end;
  264. function TX86AsmOptimizer.RegReadByInstruction(reg: TRegister; hp: tai): boolean;
  265. var
  266. p: taicpu;
  267. opcount: longint;
  268. begin
  269. RegReadByInstruction := false;
  270. if hp.typ <> ait_instruction then
  271. exit;
  272. p := taicpu(hp);
  273. case p.opcode of
  274. A_CALL:
  275. regreadbyinstruction := true;
  276. A_IMUL:
  277. case p.ops of
  278. 1:
  279. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  280. (
  281. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  282. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  283. );
  284. 2,3:
  285. regReadByInstruction :=
  286. reginop(reg,p.oper[0]^) or
  287. reginop(reg,p.oper[1]^);
  288. else
  289. InternalError(2019112801);
  290. end;
  291. A_MUL:
  292. begin
  293. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  294. (
  295. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  296. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  297. );
  298. end;
  299. A_IDIV,A_DIV:
  300. begin
  301. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  302. (
  303. (getregtype(reg)=R_INTREGISTER) and
  304. (
  305. (getsupreg(reg)=RS_EAX) or ((getsupreg(reg)=RS_EDX) and (p.opsize<>S_B))
  306. )
  307. );
  308. end;
  309. else
  310. begin
  311. if (p.opcode=A_LEA) and is_segment_reg(reg) then
  312. begin
  313. RegReadByInstruction := false;
  314. exit;
  315. end;
  316. for opcount := 0 to p.ops-1 do
  317. if (p.oper[opCount]^.typ = top_ref) and
  318. RegInRef(reg,p.oper[opcount]^.ref^) then
  319. begin
  320. RegReadByInstruction := true;
  321. exit
  322. end;
  323. { special handling for SSE MOVSD }
  324. if (p.opcode=A_MOVSD) and (p.ops>0) then
  325. begin
  326. if p.ops<>2 then
  327. internalerror(2017042702);
  328. regReadByInstruction := reginop(reg,p.oper[0]^) or
  329. (
  330. (p.oper[1]^.typ=top_reg) and (p.oper[0]^.typ=top_reg) and reginop(reg, p.oper[1]^)
  331. );
  332. exit;
  333. end;
  334. with insprop[p.opcode] do
  335. begin
  336. if getregtype(reg)=R_INTREGISTER then
  337. begin
  338. case getsupreg(reg) of
  339. RS_EAX:
  340. if [Ch_REAX,Ch_RWEAX,Ch_MEAX]*Ch<>[] then
  341. begin
  342. RegReadByInstruction := true;
  343. exit
  344. end;
  345. RS_ECX:
  346. if [Ch_RECX,Ch_RWECX,Ch_MECX]*Ch<>[] then
  347. begin
  348. RegReadByInstruction := true;
  349. exit
  350. end;
  351. RS_EDX:
  352. if [Ch_REDX,Ch_RWEDX,Ch_MEDX]*Ch<>[] then
  353. begin
  354. RegReadByInstruction := true;
  355. exit
  356. end;
  357. RS_EBX:
  358. if [Ch_REBX,Ch_RWEBX,Ch_MEBX]*Ch<>[] then
  359. begin
  360. RegReadByInstruction := true;
  361. exit
  362. end;
  363. RS_ESP:
  364. if [Ch_RESP,Ch_RWESP,Ch_MESP]*Ch<>[] then
  365. begin
  366. RegReadByInstruction := true;
  367. exit
  368. end;
  369. RS_EBP:
  370. if [Ch_REBP,Ch_RWEBP,Ch_MEBP]*Ch<>[] then
  371. begin
  372. RegReadByInstruction := true;
  373. exit
  374. end;
  375. RS_ESI:
  376. if [Ch_RESI,Ch_RWESI,Ch_MESI]*Ch<>[] then
  377. begin
  378. RegReadByInstruction := true;
  379. exit
  380. end;
  381. RS_EDI:
  382. if [Ch_REDI,Ch_RWEDI,Ch_MEDI]*Ch<>[] then
  383. begin
  384. RegReadByInstruction := true;
  385. exit
  386. end;
  387. end;
  388. end;
  389. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  390. begin
  391. if (Ch_RFLAGScc in Ch) and not(getsubreg(reg) in [R_SUBW,R_SUBD,R_SUBQ]) then
  392. begin
  393. case p.condition of
  394. C_A,C_NBE, { CF=0 and ZF=0 }
  395. C_BE,C_NA: { CF=1 or ZF=1 }
  396. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY,R_SUBFLAGZERO];
  397. C_AE,C_NB,C_NC, { CF=0 }
  398. C_B,C_NAE,C_C: { CF=1 }
  399. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY];
  400. C_NE,C_NZ, { ZF=0 }
  401. C_E,C_Z: { ZF=1 }
  402. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO];
  403. C_G,C_NLE, { ZF=0 and SF=OF }
  404. C_LE,C_NG: { ZF=1 or SF<>OF }
  405. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO,R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  406. C_GE,C_NL, { SF=OF }
  407. C_L,C_NGE: { SF<>OF }
  408. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  409. C_NO, { OF=0 }
  410. C_O: { OF=1 }
  411. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGOVERFLOW];
  412. C_NP,C_PO, { PF=0 }
  413. C_P,C_PE: { PF=1 }
  414. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGPARITY];
  415. C_NS, { SF=0 }
  416. C_S: { SF=1 }
  417. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN];
  418. else
  419. internalerror(2017042701);
  420. end;
  421. if RegReadByInstruction then
  422. exit;
  423. end;
  424. case getsubreg(reg) of
  425. R_SUBW,R_SUBD,R_SUBQ:
  426. RegReadByInstruction :=
  427. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  428. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  429. Ch_RDirFlag,Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc]*Ch<>[];
  430. R_SUBFLAGCARRY:
  431. RegReadByInstruction:=[Ch_RCarryFlag,Ch_RWCarryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  432. R_SUBFLAGPARITY:
  433. RegReadByInstruction:=[Ch_RParityFlag,Ch_RWParityFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  434. R_SUBFLAGAUXILIARY:
  435. RegReadByInstruction:=[Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  436. R_SUBFLAGZERO:
  437. RegReadByInstruction:=[Ch_RZeroFlag,Ch_RWZeroFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  438. R_SUBFLAGSIGN:
  439. RegReadByInstruction:=[Ch_RSignFlag,Ch_RWSignFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  440. R_SUBFLAGOVERFLOW:
  441. RegReadByInstruction:=[Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  442. R_SUBFLAGINTERRUPT:
  443. RegReadByInstruction:=[Ch_RFlags,Ch_RWFlags]*Ch<>[];
  444. R_SUBFLAGDIRECTION:
  445. RegReadByInstruction:=[Ch_RDirFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  446. else
  447. internalerror(2017042601);
  448. end;
  449. exit;
  450. end;
  451. if (Ch_NoReadIfEqualRegs in Ch) and (p.ops=2) and
  452. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  453. (p.oper[0]^.reg=p.oper[1]^.reg) then
  454. exit;
  455. if ([CH_RWOP1,CH_ROP1,CH_MOP1]*Ch<>[]) and reginop(reg,p.oper[0]^) then
  456. begin
  457. RegReadByInstruction := true;
  458. exit
  459. end;
  460. if ([Ch_RWOP2,Ch_ROP2,Ch_MOP2]*Ch<>[]) and reginop(reg,p.oper[1]^) then
  461. begin
  462. RegReadByInstruction := true;
  463. exit
  464. end;
  465. if ([Ch_RWOP3,Ch_ROP3,Ch_MOP3]*Ch<>[]) and reginop(reg,p.oper[2]^) then
  466. begin
  467. RegReadByInstruction := true;
  468. exit
  469. end;
  470. if ([Ch_RWOP4,Ch_ROP4,Ch_MOP4]*Ch<>[]) and reginop(reg,p.oper[3]^) then
  471. begin
  472. RegReadByInstruction := true;
  473. exit
  474. end;
  475. end;
  476. end;
  477. end;
  478. end;
  479. function TX86AsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  480. begin
  481. result:=false;
  482. if p1.typ<>ait_instruction then
  483. exit;
  484. if (Ch_All in insprop[taicpu(p1).opcode].Ch) then
  485. exit(true);
  486. if (getregtype(reg)=R_INTREGISTER) and
  487. { change information for xmm movsd are not correct }
  488. ((taicpu(p1).opcode<>A_MOVSD) or (taicpu(p1).ops=0)) then
  489. begin
  490. case getsupreg(reg) of
  491. { RS_EAX = RS_RAX on x86-64 }
  492. RS_EAX:
  493. result:=([Ch_REAX,Ch_RRAX,Ch_WEAX,Ch_WRAX,Ch_RWEAX,Ch_RWRAX,Ch_MEAX,Ch_MRAX]*insprop[taicpu(p1).opcode].Ch)<>[];
  494. RS_ECX:
  495. result:=([Ch_RECX,Ch_RRCX,Ch_WECX,Ch_WRCX,Ch_RWECX,Ch_RWRCX,Ch_MECX,Ch_MRCX]*insprop[taicpu(p1).opcode].Ch)<>[];
  496. RS_EDX:
  497. result:=([Ch_REDX,Ch_RRDX,Ch_WEDX,Ch_WRDX,Ch_RWEDX,Ch_RWRDX,Ch_MEDX,Ch_MRDX]*insprop[taicpu(p1).opcode].Ch)<>[];
  498. RS_EBX:
  499. result:=([Ch_REBX,Ch_RRBX,Ch_WEBX,Ch_WRBX,Ch_RWEBX,Ch_RWRBX,Ch_MEBX,Ch_MRBX]*insprop[taicpu(p1).opcode].Ch)<>[];
  500. RS_ESP:
  501. result:=([Ch_RESP,Ch_RRSP,Ch_WESP,Ch_WRSP,Ch_RWESP,Ch_RWRSP,Ch_MESP,Ch_MRSP]*insprop[taicpu(p1).opcode].Ch)<>[];
  502. RS_EBP:
  503. result:=([Ch_REBP,Ch_RRBP,Ch_WEBP,Ch_WRBP,Ch_RWEBP,Ch_RWRBP,Ch_MEBP,Ch_MRBP]*insprop[taicpu(p1).opcode].Ch)<>[];
  504. RS_ESI:
  505. result:=([Ch_RESI,Ch_RRSI,Ch_WESI,Ch_WRSI,Ch_RWESI,Ch_RWRSI,Ch_MESI,Ch_MRSI,Ch_RMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  506. RS_EDI:
  507. result:=([Ch_REDI,Ch_RRDI,Ch_WEDI,Ch_WRDI,Ch_RWEDI,Ch_RWRDI,Ch_MEDI,Ch_MRDI,Ch_WMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  508. else
  509. ;
  510. end;
  511. if result then
  512. exit;
  513. end
  514. else if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  515. begin
  516. if ([Ch_RFlags,Ch_WFlags,Ch_RWFlags,Ch_RFLAGScc]*insprop[taicpu(p1).opcode].Ch)<>[] then
  517. exit(true);
  518. case getsubreg(reg) of
  519. R_SUBFLAGCARRY:
  520. Result:=([Ch_RCarryFlag,Ch_RWCarryFlag,Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  521. R_SUBFLAGPARITY:
  522. Result:=([Ch_RParityFlag,Ch_RWParityFlag,Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  523. R_SUBFLAGAUXILIARY:
  524. Result:=([Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  525. R_SUBFLAGZERO:
  526. Result:=([Ch_RZeroFlag,Ch_RWZeroFlag,Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  527. R_SUBFLAGSIGN:
  528. Result:=([Ch_RSignFlag,Ch_RWSignFlag,Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  529. R_SUBFLAGOVERFLOW:
  530. Result:=([Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  531. R_SUBFLAGINTERRUPT:
  532. Result:=([Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  533. R_SUBFLAGDIRECTION:
  534. Result:=([Ch_RDirFlag,Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  535. else
  536. ;
  537. end;
  538. if result then
  539. exit;
  540. end
  541. else if (getregtype(reg)=R_FPUREGISTER) and (Ch_FPU in insprop[taicpu(p1).opcode].Ch) then
  542. exit(true);
  543. Result:=inherited RegInInstruction(Reg, p1);
  544. end;
  545. function TX86AsmOptimizer.RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean;
  546. begin
  547. Result := False;
  548. if p1.typ <> ait_instruction then
  549. exit;
  550. with insprop[taicpu(p1).opcode] do
  551. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  552. begin
  553. case getsubreg(reg) of
  554. R_SUBW,R_SUBD,R_SUBQ:
  555. Result :=
  556. [Ch_WCarryFlag,Ch_WParityFlag,Ch_WAuxiliaryFlag,Ch_WZeroFlag,Ch_WSignFlag,Ch_WOverflowFlag,
  557. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  558. Ch_W0DirFlag,Ch_W1DirFlag,Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  559. R_SUBFLAGCARRY:
  560. Result:=[Ch_WCarryFlag,Ch_RWCarryFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  561. R_SUBFLAGPARITY:
  562. Result:=[Ch_WParityFlag,Ch_RWParityFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  563. R_SUBFLAGAUXILIARY:
  564. Result:=[Ch_WAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  565. R_SUBFLAGZERO:
  566. Result:=[Ch_WZeroFlag,Ch_RWZeroFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  567. R_SUBFLAGSIGN:
  568. Result:=[Ch_WSignFlag,Ch_RWSignFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  569. R_SUBFLAGOVERFLOW:
  570. Result:=[Ch_WOverflowFlag,Ch_RWOverflowFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  571. R_SUBFLAGINTERRUPT:
  572. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  573. R_SUBFLAGDIRECTION:
  574. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  575. else
  576. internalerror(2017042602);
  577. end;
  578. exit;
  579. end;
  580. case taicpu(p1).opcode of
  581. A_CALL:
  582. { We could potentially set Result to False if the register in
  583. question is non-volatile for the subroutine's calling convention,
  584. but this would require detecting the calling convention in use and
  585. also assuming that the routine doesn't contain malformed assembly
  586. language, for example... so it could only be done under -O4 as it
  587. would be considered a side-effect. [Kit] }
  588. Result := True;
  589. A_MOVSD:
  590. { special handling for SSE MOVSD }
  591. if (taicpu(p1).ops>0) then
  592. begin
  593. if taicpu(p1).ops<>2 then
  594. internalerror(2017042703);
  595. Result := (taicpu(p1).oper[1]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[1]^);
  596. end;
  597. { VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  598. so fix it here (FK)
  599. }
  600. A_VMOVSS,
  601. A_VMOVSD:
  602. begin
  603. Result := (taicpu(p1).ops=3) and (taicpu(p1).oper[2]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[2]^);
  604. exit;
  605. end;
  606. A_IMUL:
  607. Result := (taicpu(p1).oper[taicpu(p1).ops-1]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[taicpu(p1).ops-1]^);
  608. else
  609. ;
  610. end;
  611. if Result then
  612. exit;
  613. with insprop[taicpu(p1).opcode] do
  614. begin
  615. if getregtype(reg)=R_INTREGISTER then
  616. begin
  617. case getsupreg(reg) of
  618. RS_EAX:
  619. if [Ch_WEAX,Ch_RWEAX,Ch_MEAX]*Ch<>[] then
  620. begin
  621. Result := True;
  622. exit
  623. end;
  624. RS_ECX:
  625. if [Ch_WECX,Ch_RWECX,Ch_MECX]*Ch<>[] then
  626. begin
  627. Result := True;
  628. exit
  629. end;
  630. RS_EDX:
  631. if [Ch_WEDX,Ch_RWEDX,Ch_MEDX]*Ch<>[] then
  632. begin
  633. Result := True;
  634. exit
  635. end;
  636. RS_EBX:
  637. if [Ch_WEBX,Ch_RWEBX,Ch_MEBX]*Ch<>[] then
  638. begin
  639. Result := True;
  640. exit
  641. end;
  642. RS_ESP:
  643. if [Ch_WESP,Ch_RWESP,Ch_MESP]*Ch<>[] then
  644. begin
  645. Result := True;
  646. exit
  647. end;
  648. RS_EBP:
  649. if [Ch_WEBP,Ch_RWEBP,Ch_MEBP]*Ch<>[] then
  650. begin
  651. Result := True;
  652. exit
  653. end;
  654. RS_ESI:
  655. if [Ch_WESI,Ch_RWESI,Ch_MESI]*Ch<>[] then
  656. begin
  657. Result := True;
  658. exit
  659. end;
  660. RS_EDI:
  661. if [Ch_WEDI,Ch_RWEDI,Ch_MEDI]*Ch<>[] then
  662. begin
  663. Result := True;
  664. exit
  665. end;
  666. end;
  667. end;
  668. if ([CH_RWOP1,CH_WOP1,CH_MOP1]*Ch<>[]) and reginop(reg,taicpu(p1).oper[0]^) then
  669. begin
  670. Result := true;
  671. exit
  672. end;
  673. if ([Ch_RWOP2,Ch_WOP2,Ch_MOP2]*Ch<>[]) and reginop(reg,taicpu(p1).oper[1]^) then
  674. begin
  675. Result := true;
  676. exit
  677. end;
  678. if ([Ch_RWOP3,Ch_WOP3,Ch_MOP3]*Ch<>[]) and reginop(reg,taicpu(p1).oper[2]^) then
  679. begin
  680. Result := true;
  681. exit
  682. end;
  683. if ([Ch_RWOP4,Ch_WOP4,Ch_MOP4]*Ch<>[]) and reginop(reg,taicpu(p1).oper[3]^) then
  684. begin
  685. Result := true;
  686. exit
  687. end;
  688. end;
  689. end;
  690. {$ifdef DEBUG_AOPTCPU}
  691. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);
  692. begin
  693. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  694. end;
  695. function debug_tostr(i: tcgint): string; inline;
  696. begin
  697. Result := tostr(i);
  698. end;
  699. function debug_regname(r: TRegister): string; inline;
  700. begin
  701. Result := '%' + std_regname(r);
  702. end;
  703. { Debug output function - creates a string representation of an operator }
  704. function debug_operstr(oper: TOper): string;
  705. begin
  706. case oper.typ of
  707. top_const:
  708. Result := '$' + debug_tostr(oper.val);
  709. top_reg:
  710. Result := debug_regname(oper.reg);
  711. top_ref:
  712. begin
  713. if oper.ref^.offset <> 0 then
  714. Result := debug_tostr(oper.ref^.offset) + '('
  715. else
  716. Result := '(';
  717. if (oper.ref^.base <> NR_INVALID) and (oper.ref^.base <> NR_NO) then
  718. begin
  719. Result := Result + debug_regname(oper.ref^.base);
  720. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  721. Result := Result + ',' + debug_regname(oper.ref^.index);
  722. end
  723. else
  724. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  725. Result := Result + debug_regname(oper.ref^.index);
  726. if (oper.ref^.scalefactor > 1) then
  727. Result := Result + ',' + debug_tostr(oper.ref^.scalefactor) + ')'
  728. else
  729. Result := Result + ')';
  730. end;
  731. else
  732. Result := '[UNKNOWN]';
  733. end;
  734. end;
  735. function debug_op2str(opcode: tasmop): string; inline;
  736. begin
  737. Result := std_op2str[opcode];
  738. end;
  739. function debug_opsize2str(opsize: topsize): string; inline;
  740. begin
  741. Result := gas_opsize2str[opsize];
  742. end;
  743. {$else DEBUG_AOPTCPU}
  744. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  745. begin
  746. end;
  747. function debug_tostr(i: tcgint): string; inline;
  748. begin
  749. Result := '';
  750. end;
  751. function debug_regname(r: TRegister): string; inline;
  752. begin
  753. Result := '';
  754. end;
  755. function debug_operstr(oper: TOper): string; inline;
  756. begin
  757. Result := '';
  758. end;
  759. function debug_op2str(opcode: tasmop): string; inline;
  760. begin
  761. Result := '';
  762. end;
  763. function debug_opsize2str(opsize: topsize): string; inline;
  764. begin
  765. Result := '';
  766. end;
  767. {$endif DEBUG_AOPTCPU}
  768. class function TX86AsmOptimizer.IsMOVZXAcceptable: Boolean; inline;
  769. begin
  770. {$ifdef x86_64}
  771. { Always fine on x86-64 }
  772. Result := True;
  773. {$else x86_64}
  774. Result :=
  775. {$ifdef i8086}
  776. (current_settings.cputype >= cpu_386) and
  777. {$endif i8086}
  778. (
  779. { Always accept if optimising for size }
  780. (cs_opt_size in current_settings.optimizerswitches) or
  781. { From the Pentium II onwards, MOVZX only takes 1 cycle. [Kit] }
  782. (current_settings.optimizecputype >= cpu_Pentium2)
  783. );
  784. {$endif x86_64}
  785. end;
  786. function TX86AsmOptimizer.Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  787. begin
  788. if not SuperRegistersEqual(reg1,reg2) then
  789. exit(false);
  790. if getregtype(reg1)<>R_INTREGISTER then
  791. exit(true); {because SuperRegisterEqual is true}
  792. case getsubreg(reg1) of
  793. { A write to R_SUBL doesn't change R_SUBH and if reg2 is R_SUBW or
  794. higher, it preserves the high bits, so the new value depends on
  795. reg2's previous value. In other words, it is equivalent to doing:
  796. reg2 := (reg2 and $ffffff00) or byte(reg1); }
  797. R_SUBL:
  798. exit(getsubreg(reg2)=R_SUBL);
  799. { A write to R_SUBH doesn't change R_SUBL and if reg2 is R_SUBW or
  800. higher, it actually does a:
  801. reg2 := (reg2 and $ffff00ff) or (reg1 and $ff00); }
  802. R_SUBH:
  803. exit(getsubreg(reg2)=R_SUBH);
  804. { If reg2 is R_SUBD or larger, a write to R_SUBW preserves the high 16
  805. bits of reg2:
  806. reg2 := (reg2 and $ffff0000) or word(reg1); }
  807. R_SUBW:
  808. exit(getsubreg(reg2) in [R_SUBL,R_SUBH,R_SUBW]);
  809. { a write to R_SUBD always overwrites every other subregister,
  810. because it clears the high 32 bits of R_SUBQ on x86_64 }
  811. R_SUBD,
  812. R_SUBQ:
  813. exit(true);
  814. else
  815. internalerror(2017042801);
  816. end;
  817. end;
  818. function TX86AsmOptimizer.Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  819. begin
  820. if not SuperRegistersEqual(reg1,reg2) then
  821. exit(false);
  822. if getregtype(reg1)<>R_INTREGISTER then
  823. exit(true); {because SuperRegisterEqual is true}
  824. case getsubreg(reg1) of
  825. R_SUBL:
  826. exit(getsubreg(reg2)<>R_SUBH);
  827. R_SUBH:
  828. exit(getsubreg(reg2)<>R_SUBL);
  829. R_SUBW,
  830. R_SUBD,
  831. R_SUBQ:
  832. exit(true);
  833. else
  834. internalerror(2017042802);
  835. end;
  836. end;
  837. function TX86AsmOptimizer.PrePeepholeOptSxx(var p : tai) : boolean;
  838. var
  839. hp1 : tai;
  840. l : TCGInt;
  841. begin
  842. result:=false;
  843. { changes the code sequence
  844. shr/sar const1, x
  845. shl const2, x
  846. to
  847. either "sar/and", "shl/and" or just "and" depending on const1 and const2 }
  848. if GetNextInstruction(p, hp1) and
  849. MatchInstruction(hp1,A_SHL,[]) and
  850. (taicpu(p).oper[0]^.typ = top_const) and
  851. (taicpu(hp1).oper[0]^.typ = top_const) and
  852. (taicpu(hp1).opsize = taicpu(p).opsize) and
  853. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[1]^.typ) and
  854. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) then
  855. begin
  856. if (taicpu(p).oper[0]^.val > taicpu(hp1).oper[0]^.val) and
  857. not(cs_opt_size in current_settings.optimizerswitches) then
  858. begin
  859. { shr/sar const1, %reg
  860. shl const2, %reg
  861. with const1 > const2 }
  862. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  863. taicpu(hp1).opcode := A_AND;
  864. l := (1 shl (taicpu(hp1).oper[0]^.val)) - 1;
  865. case taicpu(p).opsize Of
  866. S_B: taicpu(hp1).loadConst(0,l Xor $ff);
  867. S_W: taicpu(hp1).loadConst(0,l Xor $ffff);
  868. S_L: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffff));
  869. S_Q: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffffffffffff));
  870. else
  871. Internalerror(2017050703)
  872. end;
  873. end
  874. else if (taicpu(p).oper[0]^.val<taicpu(hp1).oper[0]^.val) and
  875. not(cs_opt_size in current_settings.optimizerswitches) then
  876. begin
  877. { shr/sar const1, %reg
  878. shl const2, %reg
  879. with const1 < const2 }
  880. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val-taicpu(p).oper[0]^.val);
  881. taicpu(p).opcode := A_AND;
  882. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  883. case taicpu(p).opsize Of
  884. S_B: taicpu(p).loadConst(0,l Xor $ff);
  885. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  886. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  887. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  888. else
  889. Internalerror(2017050702)
  890. end;
  891. end
  892. else if (taicpu(p).oper[0]^.val = taicpu(hp1).oper[0]^.val) then
  893. begin
  894. { shr/sar const1, %reg
  895. shl const2, %reg
  896. with const1 = const2 }
  897. taicpu(p).opcode := A_AND;
  898. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  899. case taicpu(p).opsize Of
  900. S_B: taicpu(p).loadConst(0,l Xor $ff);
  901. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  902. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  903. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  904. else
  905. Internalerror(2017050701)
  906. end;
  907. asml.remove(hp1);
  908. hp1.free;
  909. end;
  910. end;
  911. end;
  912. function TX86AsmOptimizer.PrePeepholeOptIMUL(var p : tai) : boolean;
  913. var
  914. opsize : topsize;
  915. hp1 : tai;
  916. tmpref : treference;
  917. ShiftValue : Cardinal;
  918. BaseValue : TCGInt;
  919. begin
  920. result:=false;
  921. opsize:=taicpu(p).opsize;
  922. { changes certain "imul const, %reg"'s to lea sequences }
  923. if (MatchOpType(taicpu(p),top_const,top_reg) or
  924. MatchOpType(taicpu(p),top_const,top_reg,top_reg)) and
  925. (opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) then
  926. if (taicpu(p).oper[0]^.val = 1) then
  927. if (taicpu(p).ops = 2) then
  928. { remove "imul $1, reg" }
  929. begin
  930. hp1 := tai(p.Next);
  931. DebugMsg(SPeepholeOptimization + 'Imul2Nop done',p);
  932. RemoveCurrentP(p);
  933. result:=true;
  934. end
  935. else
  936. { change "imul $1, reg1, reg2" to "mov reg1, reg2" }
  937. begin
  938. hp1 := taicpu.Op_Reg_Reg(A_MOV, opsize, taicpu(p).oper[1]^.reg,taicpu(p).oper[2]^.reg);
  939. InsertLLItem(p.previous, p.next, hp1);
  940. DebugMsg(SPeepholeOptimization + 'Imul2Mov done',p);
  941. p.free;
  942. p := hp1;
  943. end
  944. else if ((taicpu(p).ops <= 2) or
  945. (taicpu(p).oper[2]^.typ = Top_Reg)) and
  946. not(cs_opt_size in current_settings.optimizerswitches) and
  947. (not(GetNextInstruction(p, hp1)) or
  948. not((tai(hp1).typ = ait_instruction) and
  949. ((taicpu(hp1).opcode=A_Jcc) and
  950. (taicpu(hp1).condition in [C_O,C_NO])))) then
  951. begin
  952. {
  953. imul X, reg1, reg2 to
  954. lea (reg1,reg1,Y), reg2
  955. shl ZZ,reg2
  956. imul XX, reg1 to
  957. lea (reg1,reg1,YY), reg1
  958. shl ZZ,reg2
  959. This optimziation makes sense for pretty much every x86, except the VIA Nano3000: it has IMUL latency 2, lea/shl pair as well,
  960. it does not exist as a separate optimization target in FPC though.
  961. This optimziation can be applied as long as only two bits are set in the constant and those two bits are separated by
  962. at most two zeros
  963. }
  964. reference_reset(tmpref,1,[]);
  965. if (PopCnt(QWord(taicpu(p).oper[0]^.val))=2) and (BsrQWord(taicpu(p).oper[0]^.val)-BsfQWord(taicpu(p).oper[0]^.val)<=3) then
  966. begin
  967. ShiftValue:=BsfQWord(taicpu(p).oper[0]^.val);
  968. BaseValue:=taicpu(p).oper[0]^.val shr ShiftValue;
  969. TmpRef.base := taicpu(p).oper[1]^.reg;
  970. TmpRef.index := taicpu(p).oper[1]^.reg;
  971. if not(BaseValue in [3,5,9]) then
  972. Internalerror(2018110101);
  973. TmpRef.ScaleFactor := BaseValue-1;
  974. if (taicpu(p).ops = 2) then
  975. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[1]^.reg)
  976. else
  977. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[2]^.reg);
  978. AsmL.InsertAfter(hp1,p);
  979. DebugMsg(SPeepholeOptimization + 'Imul2LeaShl done',p);
  980. taicpu(hp1).fileinfo:=taicpu(p).fileinfo;
  981. RemoveCurrentP(p);
  982. if ShiftValue>0 then
  983. AsmL.InsertAfter(taicpu.op_const_reg(A_SHL, opsize, ShiftValue, taicpu(hp1).oper[1]^.reg),hp1);
  984. end;
  985. end;
  986. end;
  987. function TX86AsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  988. var
  989. p: taicpu;
  990. begin
  991. if not assigned(hp) or
  992. (hp.typ <> ait_instruction) then
  993. begin
  994. Result := false;
  995. exit;
  996. end;
  997. p := taicpu(hp);
  998. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  999. with insprop[p.opcode] do
  1000. begin
  1001. case getsubreg(reg) of
  1002. R_SUBW,R_SUBD,R_SUBQ:
  1003. Result:=
  1004. RegLoadedWithNewValue(NR_CARRYFLAG,hp) and
  1005. RegLoadedWithNewValue(NR_PARITYFLAG,hp) and
  1006. RegLoadedWithNewValue(NR_AUXILIARYFLAG,hp) and
  1007. RegLoadedWithNewValue(NR_ZEROFLAG,hp) and
  1008. RegLoadedWithNewValue(NR_SIGNFLAG,hp) and
  1009. RegLoadedWithNewValue(NR_OVERFLOWFLAG,hp);
  1010. R_SUBFLAGCARRY:
  1011. Result:=[Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag,Ch_WFlags]*Ch<>[];
  1012. R_SUBFLAGPARITY:
  1013. Result:=[Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag,Ch_WFlags]*Ch<>[];
  1014. R_SUBFLAGAUXILIARY:
  1015. Result:=[Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag,Ch_WFlags]*Ch<>[];
  1016. R_SUBFLAGZERO:
  1017. Result:=[Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag,Ch_WFlags]*Ch<>[];
  1018. R_SUBFLAGSIGN:
  1019. Result:=[Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag,Ch_WFlags]*Ch<>[];
  1020. R_SUBFLAGOVERFLOW:
  1021. Result:=[Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag,Ch_WFlags]*Ch<>[];
  1022. R_SUBFLAGINTERRUPT:
  1023. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*Ch<>[];
  1024. R_SUBFLAGDIRECTION:
  1025. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*Ch<>[];
  1026. else
  1027. begin
  1028. writeln(getsubreg(reg));
  1029. internalerror(2017050501);
  1030. end;
  1031. end;
  1032. exit;
  1033. end;
  1034. Result :=
  1035. (((p.opcode = A_MOV) or
  1036. (p.opcode = A_MOVZX) or
  1037. (p.opcode = A_MOVSX) or
  1038. (p.opcode = A_LEA) or
  1039. (p.opcode = A_VMOVSS) or
  1040. (p.opcode = A_VMOVSD) or
  1041. (p.opcode = A_VMOVAPD) or
  1042. (p.opcode = A_VMOVAPS) or
  1043. (p.opcode = A_VMOVQ) or
  1044. (p.opcode = A_MOVSS) or
  1045. (p.opcode = A_MOVSD) or
  1046. (p.opcode = A_MOVQ) or
  1047. (p.opcode = A_MOVAPD) or
  1048. (p.opcode = A_MOVAPS) or
  1049. {$ifndef x86_64}
  1050. (p.opcode = A_LDS) or
  1051. (p.opcode = A_LES) or
  1052. {$endif not x86_64}
  1053. (p.opcode = A_LFS) or
  1054. (p.opcode = A_LGS) or
  1055. (p.opcode = A_LSS)) and
  1056. (p.ops=2) and { A_MOVSD can have zero operands, so this check is needed }
  1057. (p.oper[1]^.typ = top_reg) and
  1058. (Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg)) and
  1059. ((p.oper[0]^.typ = top_const) or
  1060. ((p.oper[0]^.typ = top_reg) and
  1061. not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  1062. ((p.oper[0]^.typ = top_ref) and
  1063. not RegInRef(reg,p.oper[0]^.ref^)))) or
  1064. ((p.opcode = A_POP) and
  1065. (Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg))) or
  1066. ((p.opcode = A_IMUL) and
  1067. (p.ops=3) and
  1068. (Reg1WriteOverwritesReg2Entirely(p.oper[2]^.reg,reg)) and
  1069. (((p.oper[1]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg))) or
  1070. ((p.oper[1]^.typ=top_ref) and not(RegInRef(reg,p.oper[1]^.ref^))))) or
  1071. ((((p.opcode = A_IMUL) or
  1072. (p.opcode = A_MUL)) and
  1073. (p.ops=1)) and
  1074. (((p.oper[0]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  1075. ((p.oper[0]^.typ=top_ref) and not(RegInRef(reg,p.oper[0]^.ref^)))) and
  1076. (((p.opsize=S_B) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  1077. ((p.opsize=S_W) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  1078. ((p.opsize=S_L) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg))
  1079. {$ifdef x86_64}
  1080. or ((p.opsize=S_Q) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg))
  1081. {$endif x86_64}
  1082. )) or
  1083. ((p.opcode = A_CWD) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  1084. ((p.opcode = A_CDQ) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg)) or
  1085. {$ifdef x86_64}
  1086. ((p.opcode = A_CQO) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg)) or
  1087. {$endif x86_64}
  1088. ((p.opcode = A_CBW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  1089. {$ifndef x86_64}
  1090. ((p.opcode = A_LDS) and (reg=NR_DS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1091. ((p.opcode = A_LES) and (reg=NR_ES) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1092. {$endif not x86_64}
  1093. ((p.opcode = A_LFS) and (reg=NR_FS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1094. ((p.opcode = A_LGS) and (reg=NR_GS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1095. ((p.opcode = A_LSS) and (reg=NR_SS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1096. {$ifndef x86_64}
  1097. ((p.opcode = A_AAM) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  1098. {$endif not x86_64}
  1099. ((p.opcode = A_LAHF) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  1100. ((p.opcode = A_LODSB) and Reg1WriteOverwritesReg2Entirely(NR_AL,reg)) or
  1101. ((p.opcode = A_LODSW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg)) or
  1102. ((p.opcode = A_LODSD) and Reg1WriteOverwritesReg2Entirely(NR_EAX,reg)) or
  1103. {$ifdef x86_64}
  1104. ((p.opcode = A_LODSQ) and Reg1WriteOverwritesReg2Entirely(NR_RAX,reg)) or
  1105. {$endif x86_64}
  1106. ((p.opcode = A_SETcc) and (p.oper[0]^.typ=top_reg) and Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  1107. (((p.opcode = A_FSTSW) or
  1108. (p.opcode = A_FNSTSW)) and
  1109. (p.oper[0]^.typ=top_reg) and
  1110. Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  1111. (((p.opcode = A_XOR) or (p.opcode = A_SUB) or (p.opcode = A_SBB)) and
  1112. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  1113. (p.oper[0]^.reg=p.oper[1]^.reg) and
  1114. Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg));
  1115. end;
  1116. class function TX86AsmOptimizer.IsExitCode(p : tai) : boolean;
  1117. var
  1118. hp2,hp3 : tai;
  1119. begin
  1120. { some x86-64 issue a NOP before the real exit code }
  1121. if MatchInstruction(p,A_NOP,[]) then
  1122. GetNextInstruction(p,p);
  1123. result:=assigned(p) and (p.typ=ait_instruction) and
  1124. ((taicpu(p).opcode = A_RET) or
  1125. ((taicpu(p).opcode=A_LEAVE) and
  1126. GetNextInstruction(p,hp2) and
  1127. MatchInstruction(hp2,A_RET,[S_NO])
  1128. ) or
  1129. (((taicpu(p).opcode=A_LEA) and
  1130. MatchOpType(taicpu(p),top_ref,top_reg) and
  1131. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  1132. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  1133. ) and
  1134. GetNextInstruction(p,hp2) and
  1135. MatchInstruction(hp2,A_RET,[S_NO])
  1136. ) or
  1137. ((((taicpu(p).opcode=A_MOV) and
  1138. MatchOpType(taicpu(p),top_reg,top_reg) and
  1139. (taicpu(p).oper[0]^.reg=current_procinfo.framepointer) and
  1140. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)) or
  1141. ((taicpu(p).opcode=A_LEA) and
  1142. MatchOpType(taicpu(p),top_ref,top_reg) and
  1143. (taicpu(p).oper[0]^.ref^.base=current_procinfo.framepointer) and
  1144. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  1145. )
  1146. ) and
  1147. GetNextInstruction(p,hp2) and
  1148. MatchInstruction(hp2,A_POP,[reg2opsize(current_procinfo.framepointer)]) and
  1149. MatchOpType(taicpu(hp2),top_reg) and
  1150. (taicpu(hp2).oper[0]^.reg=current_procinfo.framepointer) and
  1151. GetNextInstruction(hp2,hp3) and
  1152. MatchInstruction(hp3,A_RET,[S_NO])
  1153. )
  1154. );
  1155. end;
  1156. class function TX86AsmOptimizer.isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  1157. begin
  1158. isFoldableArithOp := False;
  1159. case hp1.opcode of
  1160. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  1161. isFoldableArithOp :=
  1162. ((taicpu(hp1).oper[0]^.typ = top_const) or
  1163. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  1164. (taicpu(hp1).oper[0]^.reg <> reg))) and
  1165. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1166. (taicpu(hp1).oper[1]^.reg = reg);
  1167. A_INC,A_DEC,A_NEG,A_NOT:
  1168. isFoldableArithOp :=
  1169. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1170. (taicpu(hp1).oper[0]^.reg = reg);
  1171. else
  1172. ;
  1173. end;
  1174. end;
  1175. procedure TX86AsmOptimizer.RemoveLastDeallocForFuncRes(p: tai);
  1176. procedure DoRemoveLastDeallocForFuncRes( supreg: tsuperregister);
  1177. var
  1178. hp2: tai;
  1179. begin
  1180. hp2 := p;
  1181. repeat
  1182. hp2 := tai(hp2.previous);
  1183. if assigned(hp2) and
  1184. (hp2.typ = ait_regalloc) and
  1185. (tai_regalloc(hp2).ratype=ra_dealloc) and
  1186. (getregtype(tai_regalloc(hp2).reg) = R_INTREGISTER) and
  1187. (getsupreg(tai_regalloc(hp2).reg) = supreg) then
  1188. begin
  1189. asml.remove(hp2);
  1190. hp2.free;
  1191. break;
  1192. end;
  1193. until not(assigned(hp2)) or regInInstruction(newreg(R_INTREGISTER,supreg,R_SUBWHOLE),hp2);
  1194. end;
  1195. begin
  1196. case current_procinfo.procdef.returndef.typ of
  1197. arraydef,recorddef,pointerdef,
  1198. stringdef,enumdef,procdef,objectdef,errordef,
  1199. filedef,setdef,procvardef,
  1200. classrefdef,forwarddef:
  1201. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1202. orddef:
  1203. if current_procinfo.procdef.returndef.size <> 0 then
  1204. begin
  1205. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1206. { for int64/qword }
  1207. if current_procinfo.procdef.returndef.size = 8 then
  1208. DoRemoveLastDeallocForFuncRes(RS_EDX);
  1209. end;
  1210. else
  1211. ;
  1212. end;
  1213. end;
  1214. function TX86AsmOptimizer.OptPass1_V_MOVAP(var p : tai) : boolean;
  1215. var
  1216. hp1,hp2 : tai;
  1217. begin
  1218. result:=false;
  1219. if MatchOpType(taicpu(p),top_reg,top_reg) then
  1220. begin
  1221. { vmova* reg1,reg1
  1222. =>
  1223. <nop> }
  1224. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  1225. begin
  1226. GetNextInstruction(p,hp1);
  1227. asml.Remove(p);
  1228. p.Free;
  1229. p:=hp1;
  1230. result:=true;
  1231. exit;
  1232. end
  1233. else if GetNextInstruction(p,hp1) then
  1234. begin
  1235. if MatchInstruction(hp1,[taicpu(p).opcode],[S_NO]) and
  1236. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  1237. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1238. begin
  1239. { vmova* reg1,reg2
  1240. vmova* reg2,reg3
  1241. dealloc reg2
  1242. =>
  1243. vmova* reg1,reg3 }
  1244. TransferUsedRegs(TmpUsedRegs);
  1245. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1246. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1247. begin
  1248. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 1',p);
  1249. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1250. asml.Remove(hp1);
  1251. hp1.Free;
  1252. result:=true;
  1253. exit;
  1254. end
  1255. { special case:
  1256. vmova* reg1,reg2
  1257. vmova* reg2,reg1
  1258. =>
  1259. vmova* reg1,reg2 }
  1260. else if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) then
  1261. begin
  1262. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 2',p);
  1263. asml.Remove(hp1);
  1264. hp1.Free;
  1265. result:=true;
  1266. exit;
  1267. end
  1268. end
  1269. end;
  1270. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) then
  1271. begin
  1272. if MatchInstruction(hp1,[A_VFMADDPD,
  1273. A_VFMADD132PD,
  1274. A_VFMADD132PS,
  1275. A_VFMADD132SD,
  1276. A_VFMADD132SS,
  1277. A_VFMADD213PD,
  1278. A_VFMADD213PS,
  1279. A_VFMADD213SD,
  1280. A_VFMADD213SS,
  1281. A_VFMADD231PD,
  1282. A_VFMADD231PS,
  1283. A_VFMADD231SD,
  1284. A_VFMADD231SS,
  1285. A_VFMADDSUB132PD,
  1286. A_VFMADDSUB132PS,
  1287. A_VFMADDSUB213PD,
  1288. A_VFMADDSUB213PS,
  1289. A_VFMADDSUB231PD,
  1290. A_VFMADDSUB231PS,
  1291. A_VFMSUB132PD,
  1292. A_VFMSUB132PS,
  1293. A_VFMSUB132SD,
  1294. A_VFMSUB132SS,
  1295. A_VFMSUB213PD,
  1296. A_VFMSUB213PS,
  1297. A_VFMSUB213SD,
  1298. A_VFMSUB213SS,
  1299. A_VFMSUB231PD,
  1300. A_VFMSUB231PS,
  1301. A_VFMSUB231SD,
  1302. A_VFMSUB231SS,
  1303. A_VFMSUBADD132PD,
  1304. A_VFMSUBADD132PS,
  1305. A_VFMSUBADD213PD,
  1306. A_VFMSUBADD213PS,
  1307. A_VFMSUBADD231PD,
  1308. A_VFMSUBADD231PS,
  1309. A_VFNMADD132PD,
  1310. A_VFNMADD132PS,
  1311. A_VFNMADD132SD,
  1312. A_VFNMADD132SS,
  1313. A_VFNMADD213PD,
  1314. A_VFNMADD213PS,
  1315. A_VFNMADD213SD,
  1316. A_VFNMADD213SS,
  1317. A_VFNMADD231PD,
  1318. A_VFNMADD231PS,
  1319. A_VFNMADD231SD,
  1320. A_VFNMADD231SS,
  1321. A_VFNMSUB132PD,
  1322. A_VFNMSUB132PS,
  1323. A_VFNMSUB132SD,
  1324. A_VFNMSUB132SS,
  1325. A_VFNMSUB213PD,
  1326. A_VFNMSUB213PS,
  1327. A_VFNMSUB213SD,
  1328. A_VFNMSUB213SS,
  1329. A_VFNMSUB231PD,
  1330. A_VFNMSUB231PS,
  1331. A_VFNMSUB231SD,
  1332. A_VFNMSUB231SS],[S_NO]) and
  1333. { we mix single and double opperations here because we assume that the compiler
  1334. generates vmovapd only after double operations and vmovaps only after single operations }
  1335. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[2]^) and
  1336. GetNextInstruction(hp1,hp2) and
  1337. MatchInstruction(hp2,[A_VMOVAPD,A_VMOVAPS,A_MOVAPD,A_MOVAPS],[S_NO]) and
  1338. MatchOperand(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) then
  1339. begin
  1340. TransferUsedRegs(TmpUsedRegs);
  1341. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1342. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1343. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1344. begin
  1345. taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
  1346. asml.Remove(p);
  1347. p.Free;
  1348. asml.Remove(hp2);
  1349. hp2.Free;
  1350. p:=hp1;
  1351. end;
  1352. end
  1353. else if (hp1.typ = ait_instruction) and
  1354. GetNextInstruction(hp1, hp2) and
  1355. MatchInstruction(hp2,taicpu(p).opcode,[]) and
  1356. OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  1357. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  1358. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  1359. (((taicpu(p).opcode=A_MOVAPS) and
  1360. ((taicpu(hp1).opcode=A_ADDSS) or (taicpu(hp1).opcode=A_SUBSS) or
  1361. (taicpu(hp1).opcode=A_MULSS) or (taicpu(hp1).opcode=A_DIVSS))) or
  1362. ((taicpu(p).opcode=A_MOVAPD) and
  1363. ((taicpu(hp1).opcode=A_ADDSD) or (taicpu(hp1).opcode=A_SUBSD) or
  1364. (taicpu(hp1).opcode=A_MULSD) or (taicpu(hp1).opcode=A_DIVSD)))
  1365. ) then
  1366. { change
  1367. movapX reg,reg2
  1368. addsX/subsX/... reg3, reg2
  1369. movapX reg2,reg
  1370. to
  1371. addsX/subsX/... reg3,reg
  1372. }
  1373. begin
  1374. TransferUsedRegs(TmpUsedRegs);
  1375. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1376. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1377. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1378. begin
  1379. DebugMsg(SPeepholeOptimization + 'MovapXOpMovapX2Op ('+
  1380. debug_op2str(taicpu(p).opcode)+' '+
  1381. debug_op2str(taicpu(hp1).opcode)+' '+
  1382. debug_op2str(taicpu(hp2).opcode)+') done',p);
  1383. { we cannot eliminate the first move if
  1384. the operations uses the same register for source and dest }
  1385. if not(OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp1).oper[0]^)) then
  1386. begin
  1387. asml.remove(p);
  1388. p.Free;
  1389. end;
  1390. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1391. asml.remove(hp2);
  1392. hp2.Free;
  1393. p:=hp1;
  1394. result:=true;
  1395. end;
  1396. end;
  1397. end;
  1398. end;
  1399. end;
  1400. function TX86AsmOptimizer.OptPass1VOP(var p : tai) : boolean;
  1401. var
  1402. hp1 : tai;
  1403. begin
  1404. result:=false;
  1405. { replace
  1406. V<Op>X %mreg1,%mreg2,%mreg3
  1407. VMovX %mreg3,%mreg4
  1408. dealloc %mreg3
  1409. by
  1410. V<Op>X %mreg1,%mreg2,%mreg4
  1411. ?
  1412. }
  1413. if GetNextInstruction(p,hp1) and
  1414. { we mix single and double operations here because we assume that the compiler
  1415. generates vmovapd only after double operations and vmovaps only after single operations }
  1416. MatchInstruction(hp1,A_VMOVAPD,A_VMOVAPS,[S_NO]) and
  1417. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  1418. (taicpu(hp1).oper[1]^.typ=top_reg) then
  1419. begin
  1420. TransferUsedRegs(TmpUsedRegs);
  1421. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1422. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  1423. begin
  1424. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  1425. DebugMsg(SPeepholeOptimization + 'VOpVmov2VOp done',p);
  1426. asml.Remove(hp1);
  1427. hp1.Free;
  1428. result:=true;
  1429. end;
  1430. end;
  1431. end;
  1432. { Replaces all references to AOldReg in a memory reference to ANewReg }
  1433. class function TX86AsmOptimizer.ReplaceRegisterInRef(var ref: TReference; const AOldReg, ANewReg: TRegister): Boolean;
  1434. var
  1435. OldSupReg: TSuperRegister;
  1436. OldSubReg, MemSubReg: TSubRegister;
  1437. begin
  1438. Result := False;
  1439. { For safety reasons, only check for exact register matches }
  1440. { Check base register }
  1441. if (ref.base = AOldReg) then
  1442. begin
  1443. ref.base := ANewReg;
  1444. Result := True;
  1445. end;
  1446. { Check index register }
  1447. if (ref.index = AOldReg) then
  1448. begin
  1449. ref.index := ANewReg;
  1450. Result := True;
  1451. end;
  1452. end;
  1453. { Replaces all references to AOldReg in an operand to ANewReg }
  1454. class function TX86AsmOptimizer.ReplaceRegisterInOper(const p: taicpu; const OperIdx: Integer; const AOldReg, ANewReg: TRegister): Boolean;
  1455. var
  1456. OldSupReg, NewSupReg: TSuperRegister;
  1457. OldSubReg, NewSubReg, MemSubReg: TSubRegister;
  1458. OldRegType: TRegisterType;
  1459. ThisOper: POper;
  1460. begin
  1461. ThisOper := p.oper[OperIdx]; { Faster to access overall }
  1462. Result := False;
  1463. if (AOldReg = NR_NO) or (ANewReg = NR_NO) then
  1464. InternalError(2020011801);
  1465. OldSupReg := getsupreg(AOldReg);
  1466. OldSubReg := getsubreg(AOldReg);
  1467. OldRegType := getregtype(AOldReg);
  1468. NewSupReg := getsupreg(ANewReg);
  1469. NewSubReg := getsubreg(ANewReg);
  1470. if OldRegType <> getregtype(ANewReg) then
  1471. InternalError(2020011802);
  1472. if OldSubReg <> NewSubReg then
  1473. InternalError(2020011803);
  1474. case ThisOper^.typ of
  1475. top_reg:
  1476. if (
  1477. (ThisOper^.reg = AOldReg) or
  1478. (
  1479. (OldRegType = R_INTREGISTER) and
  1480. (getsupreg(ThisOper^.reg) = OldSupReg) and
  1481. (getregtype(ThisOper^.reg) = R_INTREGISTER) and
  1482. (
  1483. (getsubreg(ThisOper^.reg) <= OldSubReg)
  1484. {$ifndef x86_64}
  1485. and (
  1486. { Under i386 and i8086, ESI, EDI, EBP and ESP
  1487. don't have an 8-bit representation }
  1488. (getsubreg(ThisOper^.reg) >= R_SUBW) or
  1489. not (NewSupReg in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  1490. )
  1491. {$endif x86_64}
  1492. )
  1493. )
  1494. ) then
  1495. begin
  1496. ThisOper^.reg := newreg(getregtype(ANewReg), NewSupReg, getsubreg(p.oper[OperIdx]^.reg));;
  1497. Result := True;
  1498. end;
  1499. top_ref:
  1500. if ReplaceRegisterInRef(ThisOper^.ref^, AOldReg, ANewReg) then
  1501. Result := True;
  1502. else
  1503. ;
  1504. end;
  1505. end;
  1506. { Replaces all references to AOldReg in an instruction to ANewReg }
  1507. function TX86AsmOptimizer.ReplaceRegisterInInstruction(const p: taicpu; const AOldReg, ANewReg: TRegister): Boolean;
  1508. const
  1509. ReadFlag: array[0..3] of TInsChange = (Ch_Rop1, Ch_Rop2, Ch_Rop3, Ch_Rop4);
  1510. var
  1511. OperIdx: Integer;
  1512. begin
  1513. Result := False;
  1514. for OperIdx := 0 to p.ops - 1 do
  1515. if (ReadFlag[OperIdx] in InsProp[p.Opcode].Ch) and
  1516. { The shift and rotate instructions can only use CL }
  1517. not (
  1518. (OperIdx = 0) and
  1519. { This second condition just helps to avoid unnecessarily
  1520. calling MatchInstruction for 10 different opcodes }
  1521. (p.oper[0]^.reg = NR_CL) and
  1522. MatchInstruction(p, [A_RCL, A_RCR, A_ROL, A_ROR, A_SAL, A_SAR, A_SHL, A_SHLD, A_SHR, A_SHRD], [])
  1523. ) then
  1524. Result := ReplaceRegisterInOper(p, OperIdx, AOldReg, ANewReg) or Result;
  1525. end;
  1526. class function TX86AsmOptimizer.IsRefSafe(const ref: PReference): Boolean; inline;
  1527. begin
  1528. Result :=
  1529. (ref^.index = NR_NO) and
  1530. (
  1531. {$ifdef x86_64}
  1532. (
  1533. (ref^.base = NR_RIP) and
  1534. (ref^.refaddr in [addr_pic, addr_pic_no_got])
  1535. ) or
  1536. {$endif x86_64}
  1537. (ref^.base = NR_STACK_POINTER_REG) or
  1538. (ref^.base = current_procinfo.framepointer)
  1539. );
  1540. end;
  1541. function TX86AsmOptimizer.DeepMOVOpt(const p_mov: taicpu; const hp: taicpu): Boolean;
  1542. var
  1543. CurrentReg, ReplaceReg: TRegister;
  1544. SubReg: TSubRegister;
  1545. begin
  1546. Result := False;
  1547. ReplaceReg := taicpu(p_mov).oper[0]^.reg;
  1548. CurrentReg := taicpu(p_mov).oper[1]^.reg;
  1549. case hp.opcode of
  1550. A_FSTSW, A_FNSTSW,
  1551. A_IN, A_INS, A_OUT, A_OUTS,
  1552. A_CMPS, A_LODS, A_MOVS, A_SCAS, A_STOS:
  1553. { These routines have explicit operands, but they are restricted in
  1554. what they can be (e.g. IN and OUT can only read from AL, AX or
  1555. EAX. }
  1556. Exit;
  1557. A_IMUL:
  1558. begin
  1559. { The 1-operand version writes to implicit registers
  1560. The 2-operand version reads from the first operator, and reads
  1561. from and writes to the second (equivalent to Ch_ROp1, ChRWOp2).
  1562. the 3-operand version reads from a register that it doesn't write to
  1563. }
  1564. case hp.ops of
  1565. 1:
  1566. if (
  1567. (
  1568. (hp.opsize = S_B) and (getsupreg(CurrentReg) <> RS_EAX)
  1569. ) or
  1570. not (getsupreg(CurrentReg) in [RS_EAX, RS_EDX])
  1571. ) and ReplaceRegisterInOper(hp, 0, CurrentReg, ReplaceReg) then
  1572. begin
  1573. Result := True;
  1574. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 1)', hp);
  1575. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  1576. end;
  1577. 2:
  1578. { Only modify the first parameter }
  1579. if ReplaceRegisterInOper(hp, 0, CurrentReg, ReplaceReg) then
  1580. begin
  1581. Result := True;
  1582. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 2)', hp);
  1583. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  1584. end;
  1585. 3:
  1586. { Only modify the second parameter }
  1587. if ReplaceRegisterInOper(hp, 1, CurrentReg, ReplaceReg) then
  1588. begin
  1589. Result := True;
  1590. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 3)', hp);
  1591. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  1592. end;
  1593. else
  1594. InternalError(2020012901);
  1595. end;
  1596. end;
  1597. else
  1598. if (hp.ops > 0) and
  1599. ReplaceRegisterInInstruction(hp, CurrentReg, ReplaceReg) then
  1600. begin
  1601. Result := True;
  1602. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovXXX2MovXXX)', hp);
  1603. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  1604. end;
  1605. end;
  1606. end;
  1607. function TX86AsmOptimizer.OptPass1MOV(var p : tai) : boolean;
  1608. var
  1609. hp1, hp2: tai;
  1610. GetNextInstruction_p, TempRegUsed: Boolean;
  1611. PreMessage, RegName1, RegName2, InputVal, MaskNum: string;
  1612. NewSize: topsize;
  1613. CurrentReg: TRegister;
  1614. begin
  1615. Result:=false;
  1616. GetNextInstruction_p:=GetNextInstruction(p, hp1);
  1617. { remove mov reg1,reg1? }
  1618. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^)
  1619. then
  1620. begin
  1621. DebugMsg(SPeepholeOptimization + 'Mov2Nop 1 done',p);
  1622. { take care of the register (de)allocs following p }
  1623. UpdateUsedRegs(tai(p.next));
  1624. asml.remove(p);
  1625. p.free;
  1626. p:=hp1;
  1627. Result:=true;
  1628. exit;
  1629. end;
  1630. { All the next optimisations require a next instruction }
  1631. if not GetNextInstruction_p or (hp1.typ <> ait_instruction) then
  1632. Exit;
  1633. { Look for:
  1634. mov %reg1,%reg2
  1635. ??? %reg2,r/m
  1636. Change to:
  1637. mov %reg1,%reg2
  1638. ??? %reg1,r/m
  1639. }
  1640. if MatchOpType(taicpu(p), top_reg, top_reg) then
  1641. begin
  1642. CurrentReg := taicpu(p).oper[1]^.reg;
  1643. if RegReadByInstruction(CurrentReg, hp1) and
  1644. DeepMOVOpt(taicpu(p), taicpu(hp1)) then
  1645. begin
  1646. TransferUsedRegs(TmpUsedRegs);
  1647. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  1648. if not RegUsedAfterInstruction(CurrentReg, hp1, TmpUsedRegs) and
  1649. { Just in case something didn't get modified (e.g. an
  1650. implicit register) }
  1651. not RegReadByInstruction(CurrentReg, hp1) then
  1652. begin
  1653. { We can remove the original MOV }
  1654. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3 done',p);
  1655. Asml.Remove(p);
  1656. p.Free;
  1657. p := hp1;
  1658. { TmpUsedRegs contains the results of "UpdateUsedRegs(tai(p.Next))" already,
  1659. so just restore it to UsedRegs instead of calculating it again }
  1660. RestoreUsedRegs(TmpUsedRegs);
  1661. Result := True;
  1662. Exit;
  1663. end;
  1664. { If we know a MOV instruction has become a null operation, we might as well
  1665. get rid of it now to save time. }
  1666. if (taicpu(hp1).opcode = A_MOV) and
  1667. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1668. SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[0]^.reg) and
  1669. { Just being a register is enough to confirm it's a null operation }
  1670. (taicpu(hp1).oper[0]^.typ = top_reg) then
  1671. begin
  1672. Result := True;
  1673. { Speed-up to reduce a pipeline stall... if we had something like...
  1674. movl %eax,%edx
  1675. movw %dx,%ax
  1676. ... the second instruction would change to movw %ax,%ax, but
  1677. given that it is now %ax that's active rather than %eax,
  1678. penalties might occur due to a partial register write, so instead,
  1679. change it to a MOVZX instruction when optimising for speed.
  1680. }
  1681. if not (cs_opt_size in current_settings.optimizerswitches) and
  1682. IsMOVZXAcceptable and
  1683. (taicpu(hp1).opsize < taicpu(p).opsize)
  1684. {$ifdef x86_64}
  1685. { operations already implicitly set the upper 64 bits to zero }
  1686. and not ((taicpu(hp1).opsize = S_L) and (taicpu(p).opsize = S_Q))
  1687. {$endif x86_64}
  1688. then
  1689. begin
  1690. CurrentReg := taicpu(hp1).oper[1]^.reg;
  1691. DebugMsg(SPeepholeOptimization + 'Zero-extension to minimise pipeline stall (Mov2Movz)',hp1);
  1692. case taicpu(p).opsize of
  1693. S_W:
  1694. if taicpu(hp1).opsize = S_B then
  1695. taicpu(hp1).opsize := S_BL
  1696. else
  1697. InternalError(2020012911);
  1698. S_L{$ifdef x86_64}, S_Q{$endif x86_64}:
  1699. case taicpu(hp1).opsize of
  1700. S_B:
  1701. taicpu(hp1).opsize := S_BL;
  1702. S_W:
  1703. taicpu(hp1).opsize := S_WL;
  1704. else
  1705. InternalError(2020012912);
  1706. end;
  1707. else
  1708. InternalError(2020012910);
  1709. end;
  1710. taicpu(hp1).opcode := A_MOVZX;
  1711. taicpu(hp1).oper[1]^.reg := newreg(getregtype(CurrentReg), getsupreg(CurrentReg), R_SUBD)
  1712. end
  1713. else
  1714. begin
  1715. GetNextInstruction_p := GetNextInstruction(hp1, hp2);
  1716. DebugMsg(SPeepholeOptimization + 'Mov2Nop 4 done',hp1);
  1717. asml.remove(hp1);
  1718. hp1.free;
  1719. { The instruction after what was hp1 is now the immediate next instruction,
  1720. so we can continue to make optimisations if it's present }
  1721. if not GetNextInstruction_p or (hp2.typ <> ait_instruction) then
  1722. Exit;
  1723. hp1 := hp2;
  1724. end;
  1725. end;
  1726. end;
  1727. end;
  1728. { Depending on the DeepMOVOpt above, it may turn out that hp1 completely
  1729. overwrites the original destination register. e.g.
  1730. movl %reg1d,%reg2d
  1731. movslq %reg1d,%reg2q
  1732. In this case, we can remove the MOV
  1733. }
  1734. if (taicpu(p).oper[1]^.typ = top_reg) and
  1735. MatchInstruction(hp1, [A_LEA, A_MOV, A_MOVSX, A_MOVZX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}], []) and
  1736. { The RegInOp check makes sure that movb r/m,%reg1b; movzbl %reg1b,%reg1l"
  1737. and "movl r/m,%reg1; leal $1(%reg1,%reg2),%reg1" etc. are not incorrectly
  1738. optimised }
  1739. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1740. not RegInOp(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[0]^) and
  1741. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[1]^.reg) then
  1742. begin
  1743. DebugMsg(SPeepholeOptimization + 'Mov2Nop 5 done',p);
  1744. { take care of the register (de)allocs following p }
  1745. UpdateUsedRegs(tai(p.next));
  1746. asml.remove(p);
  1747. p.free;
  1748. p:=hp1;
  1749. Result := True;
  1750. Exit;
  1751. end;
  1752. if (taicpu(hp1).opcode = A_AND) and
  1753. (taicpu(p).oper[1]^.typ = top_reg) and
  1754. MatchOpType(taicpu(hp1),top_const,top_reg) then
  1755. begin
  1756. if MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) then
  1757. begin
  1758. case taicpu(p).opsize of
  1759. S_L:
  1760. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  1761. begin
  1762. { Optimize out:
  1763. mov x, %reg
  1764. and ffffffffh, %reg
  1765. }
  1766. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 1 done',p);
  1767. asml.remove(hp1);
  1768. hp1.free;
  1769. Result:=true;
  1770. exit;
  1771. end;
  1772. S_Q: { TODO: Confirm if this is even possible }
  1773. if (taicpu(hp1).oper[0]^.val = $ffffffffffffffff) then
  1774. begin
  1775. { Optimize out:
  1776. mov x, %reg
  1777. and ffffffffffffffffh, %reg
  1778. }
  1779. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 2 done',p);
  1780. asml.remove(hp1);
  1781. hp1.free;
  1782. Result:=true;
  1783. exit;
  1784. end;
  1785. else
  1786. ;
  1787. end;
  1788. end
  1789. else if IsMOVZXAcceptable and
  1790. (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(hp1).oper[1]^.typ = top_reg) and
  1791. (taicpu(p).oper[0]^.typ <> top_const) and { MOVZX only supports registers and memory, not immediates (use MOV for that!) }
  1792. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  1793. then
  1794. begin
  1795. InputVal := debug_operstr(taicpu(p).oper[0]^);
  1796. MaskNum := debug_tostr(taicpu(hp1).oper[0]^.val);
  1797. case taicpu(p).opsize of
  1798. S_B:
  1799. if (taicpu(hp1).oper[0]^.val = $ff) then
  1800. begin
  1801. { Convert:
  1802. movb x, %regl movb x, %regl
  1803. andw ffh, %regw andl ffh, %regd
  1804. To:
  1805. movzbw x, %regd movzbl x, %regd
  1806. (Identical registers, just different sizes)
  1807. }
  1808. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 8-bit register name }
  1809. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 16/32-bit register name }
  1810. case taicpu(hp1).opsize of
  1811. S_W: NewSize := S_BW;
  1812. S_L: NewSize := S_BL;
  1813. {$ifdef x86_64}
  1814. S_Q: NewSize := S_BQ;
  1815. {$endif x86_64}
  1816. else
  1817. InternalError(2018011510);
  1818. end;
  1819. end
  1820. else
  1821. NewSize := S_NO;
  1822. S_W:
  1823. if (taicpu(hp1).oper[0]^.val = $ffff) then
  1824. begin
  1825. { Convert:
  1826. movw x, %regw
  1827. andl ffffh, %regd
  1828. To:
  1829. movzwl x, %regd
  1830. (Identical registers, just different sizes)
  1831. }
  1832. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 16-bit register name }
  1833. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 32-bit register name }
  1834. case taicpu(hp1).opsize of
  1835. S_L: NewSize := S_WL;
  1836. {$ifdef x86_64}
  1837. S_Q: NewSize := S_WQ;
  1838. {$endif x86_64}
  1839. else
  1840. InternalError(2018011511);
  1841. end;
  1842. end
  1843. else
  1844. NewSize := S_NO;
  1845. else
  1846. NewSize := S_NO;
  1847. end;
  1848. if NewSize <> S_NO then
  1849. begin
  1850. PreMessage := 'mov' + debug_opsize2str(taicpu(p).opsize) + ' ' + InputVal + ',' + RegName1;
  1851. { The actual optimization }
  1852. taicpu(p).opcode := A_MOVZX;
  1853. taicpu(p).changeopsize(NewSize);
  1854. taicpu(p).oper[1]^ := taicpu(hp1).oper[1]^;
  1855. { Safeguard if "and" is followed by a conditional command }
  1856. TransferUsedRegs(TmpUsedRegs);
  1857. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  1858. if (RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  1859. begin
  1860. { At this point, the "and" command is effectively equivalent to
  1861. "test %reg,%reg". This will be handled separately by the
  1862. Peephole Optimizer. [Kit] }
  1863. DebugMsg(SPeepholeOptimization + PreMessage +
  1864. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  1865. end
  1866. else
  1867. begin
  1868. DebugMsg(SPeepholeOptimization + PreMessage + '; and' + debug_opsize2str(taicpu(hp1).opsize) + ' $' + MaskNum + ',' + RegName2 +
  1869. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  1870. asml.Remove(hp1);
  1871. hp1.Free;
  1872. end;
  1873. Result := True;
  1874. Exit;
  1875. end;
  1876. end;
  1877. end;
  1878. { Next instruction is also a MOV ? }
  1879. if MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) then
  1880. begin
  1881. if (taicpu(p).oper[1]^.typ = top_reg) and
  1882. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1883. begin
  1884. CurrentReg := taicpu(p).oper[1]^.reg;
  1885. TransferUsedRegs(TmpUsedRegs);
  1886. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  1887. { we have
  1888. mov x, %treg
  1889. mov %treg, y
  1890. }
  1891. if not(RegInOp(CurrentReg, taicpu(hp1).oper[1]^)) then
  1892. if not(RegUsedAfterInstruction(CurrentReg, hp1, TmpUsedRegs)) then
  1893. { we've got
  1894. mov x, %treg
  1895. mov %treg, y
  1896. with %treg is not used after }
  1897. case taicpu(p).oper[0]^.typ Of
  1898. top_reg:
  1899. begin
  1900. { change
  1901. mov %reg, %treg
  1902. mov %treg, y
  1903. to
  1904. mov %reg, y
  1905. }
  1906. if taicpu(hp1).oper[1]^.typ=top_reg then
  1907. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1908. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  1909. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 2 done',p);
  1910. asml.remove(hp1);
  1911. hp1.free;
  1912. Result:=true;
  1913. Exit;
  1914. end;
  1915. top_const:
  1916. begin
  1917. { change
  1918. mov const, %treg
  1919. mov %treg, y
  1920. to
  1921. mov const, y
  1922. }
  1923. if (taicpu(hp1).oper[1]^.typ=top_reg) or
  1924. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  1925. begin
  1926. if taicpu(hp1).oper[1]^.typ=top_reg then
  1927. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1928. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  1929. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 5 done',p);
  1930. asml.remove(hp1);
  1931. hp1.free;
  1932. Result:=true;
  1933. Exit;
  1934. end;
  1935. end;
  1936. top_ref:
  1937. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  1938. begin
  1939. { change
  1940. mov mem, %treg
  1941. mov %treg, %reg
  1942. to
  1943. mov mem, %reg"
  1944. }
  1945. taicpu(p).loadreg(1, taicpu(hp1).oper[1]^.reg);
  1946. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3 done',p);
  1947. asml.remove(hp1);
  1948. hp1.free;
  1949. Result:=true;
  1950. Exit;
  1951. end;
  1952. else
  1953. { Do nothing };
  1954. end
  1955. else
  1956. { %treg is used afterwards }
  1957. case taicpu(p).oper[0]^.typ of
  1958. top_const:
  1959. if
  1960. (
  1961. not (cs_opt_size in current_settings.optimizerswitches) or
  1962. (taicpu(hp1).opsize = S_B)
  1963. ) and
  1964. (
  1965. (taicpu(hp1).oper[1]^.typ = top_reg) or
  1966. ((taicpu(p).oper[0]^.val >= low(longint)) and (taicpu(p).oper[0]^.val <= high(longint)))
  1967. ) then
  1968. begin
  1969. DebugMsg(SPeepholeOptimization + debug_operstr(taicpu(hp1).oper[0]^) + ' = $' + debug_tostr(taicpu(p).oper[0]^.val) + '; changed to minimise pipeline stall (MovMov2Mov 6b)',hp1);
  1970. taicpu(hp1).loadconst(0, taicpu(p).oper[0]^.val);
  1971. end;
  1972. top_reg:
  1973. begin
  1974. DebugMsg(SPeepholeOptimization + debug_operstr(taicpu(hp1).oper[0]^) + ' = ' + debug_regname(taicpu(p).oper[0]^.reg) + '; changed to minimise pipeline stall (MovMov2Mov 6c)',hp1);
  1975. AllocRegBetween(taicpu(p).oper[0]^.reg, p, hp1, UsedRegs);
  1976. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1977. begin
  1978. DebugMsg(SPeepholeOptimization + 'Mov2Nop 2 done',hp1);
  1979. asml.remove(hp1);
  1980. hp1.free;
  1981. Result := True;
  1982. Exit;
  1983. end;
  1984. taicpu(hp1).loadreg(0, taicpu(p).oper[0]^.reg);
  1985. end;
  1986. else
  1987. { Do nothing };
  1988. end;
  1989. end;
  1990. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  1991. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  1992. { mov reg1, mem1 or mov mem1, reg1
  1993. mov mem2, reg2 mov reg2, mem2}
  1994. begin
  1995. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  1996. { mov reg1, mem1 or mov mem1, reg1
  1997. mov mem2, reg1 mov reg2, mem1}
  1998. begin
  1999. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2000. { Removes the second statement from
  2001. mov reg1, mem1/reg2
  2002. mov mem1/reg2, reg1 }
  2003. begin
  2004. if taicpu(p).oper[0]^.typ=top_reg then
  2005. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2006. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 1',p);
  2007. asml.remove(hp1);
  2008. hp1.free;
  2009. Result:=true;
  2010. exit;
  2011. end
  2012. else
  2013. begin
  2014. TransferUsedRegs(TmpUsedRegs);
  2015. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2016. if (taicpu(p).oper[1]^.typ = top_ref) and
  2017. { mov reg1, mem1
  2018. mov mem2, reg1 }
  2019. (taicpu(hp1).oper[0]^.ref^.refaddr = addr_no) and
  2020. GetNextInstruction(hp1, hp2) and
  2021. MatchInstruction(hp2,A_CMP,[taicpu(p).opsize]) and
  2022. OpsEqual(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  2023. OpsEqual(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) and
  2024. not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs)) then
  2025. { change to
  2026. mov reg1, mem1 mov reg1, mem1
  2027. mov mem2, reg1 cmp reg1, mem2
  2028. cmp mem1, reg1
  2029. }
  2030. begin
  2031. asml.remove(hp2);
  2032. hp2.free;
  2033. taicpu(hp1).opcode := A_CMP;
  2034. taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
  2035. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  2036. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  2037. DebugMsg(SPeepholeOptimization + 'MovMovCmp2MovCmp done',hp1);
  2038. end;
  2039. end;
  2040. end
  2041. else if (taicpu(p).oper[1]^.typ=top_ref) and
  2042. OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2043. begin
  2044. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  2045. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  2046. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov1 done',p);
  2047. end
  2048. else
  2049. begin
  2050. TransferUsedRegs(TmpUsedRegs);
  2051. if GetNextInstruction(hp1, hp2) and
  2052. MatchOpType(taicpu(p),top_ref,top_reg) and
  2053. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2054. (taicpu(hp1).oper[1]^.typ = top_ref) and
  2055. MatchInstruction(hp2,A_MOV,[taicpu(p).opsize]) and
  2056. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  2057. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  2058. if not RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^) and
  2059. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,tmpUsedRegs)) then
  2060. { mov mem1, %reg1
  2061. mov %reg1, mem2
  2062. mov mem2, reg2
  2063. to:
  2064. mov mem1, reg2
  2065. mov reg2, mem2}
  2066. begin
  2067. AllocRegBetween(taicpu(hp2).oper[1]^.reg,p,hp2,usedregs);
  2068. DebugMsg(SPeepholeOptimization + 'MovMovMov2MovMov 1 done',p);
  2069. taicpu(p).loadoper(1,taicpu(hp2).oper[1]^);
  2070. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  2071. asml.remove(hp2);
  2072. hp2.free;
  2073. end
  2074. {$ifdef i386}
  2075. { this is enabled for i386 only, as the rules to create the reg sets below
  2076. are too complicated for x86-64, so this makes this code too error prone
  2077. on x86-64
  2078. }
  2079. else if (taicpu(p).oper[1]^.reg <> taicpu(hp2).oper[1]^.reg) and
  2080. not(RegInRef(taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.ref^)) and
  2081. not(RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^)) then
  2082. { mov mem1, reg1 mov mem1, reg1
  2083. mov reg1, mem2 mov reg1, mem2
  2084. mov mem2, reg2 mov mem2, reg1
  2085. to: to:
  2086. mov mem1, reg1 mov mem1, reg1
  2087. mov mem1, reg2 mov reg1, mem2
  2088. mov reg1, mem2
  2089. or (if mem1 depends on reg1
  2090. and/or if mem2 depends on reg2)
  2091. to:
  2092. mov mem1, reg1
  2093. mov reg1, mem2
  2094. mov reg1, reg2
  2095. }
  2096. begin
  2097. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  2098. taicpu(hp1).loadReg(1,taicpu(hp2).oper[1]^.reg);
  2099. taicpu(hp2).loadRef(1,taicpu(hp2).oper[0]^.ref^);
  2100. taicpu(hp2).loadReg(0,taicpu(p).oper[1]^.reg);
  2101. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  2102. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  2103. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  2104. AllocRegBetween(taicpu(p).oper[0]^.ref^.base,p,hp2,usedregs);
  2105. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and
  2106. (getsupreg(taicpu(p).oper[0]^.ref^.index) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  2107. AllocRegBetween(taicpu(p).oper[0]^.ref^.index,p,hp2,usedregs);
  2108. end
  2109. else if (taicpu(hp1).Oper[0]^.reg <> taicpu(hp2).Oper[1]^.reg) then
  2110. begin
  2111. taicpu(hp2).loadReg(0,taicpu(hp1).Oper[0]^.reg);
  2112. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  2113. end
  2114. else
  2115. begin
  2116. asml.remove(hp2);
  2117. hp2.free;
  2118. end
  2119. {$endif i386}
  2120. ;
  2121. end;
  2122. end;
  2123. (* { movl [mem1],reg1
  2124. movl [mem1],reg2
  2125. to
  2126. movl [mem1],reg1
  2127. movl reg1,reg2
  2128. }
  2129. else if (taicpu(p).oper[0]^.typ = top_ref) and
  2130. (taicpu(p).oper[1]^.typ = top_reg) and
  2131. (taicpu(hp1).oper[0]^.typ = top_ref) and
  2132. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2133. (taicpu(p).opsize = taicpu(hp1).opsize) and
  2134. RefsEqual(TReference(taicpu(p).oper[0]^^),taicpu(hp1).oper[0]^^.ref^) and
  2135. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.base) and
  2136. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.index) then
  2137. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg)
  2138. else*)
  2139. { movl const1,[mem1]
  2140. movl [mem1],reg1
  2141. to
  2142. movl const1,reg1
  2143. movl reg1,[mem1]
  2144. }
  2145. if MatchOpType(Taicpu(p),top_const,top_ref) and
  2146. MatchOpType(Taicpu(hp1),top_ref,top_reg) and
  2147. (taicpu(p).opsize = taicpu(hp1).opsize) and
  2148. RefsEqual(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.ref^) and
  2149. not(RegInRef(taicpu(hp1).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^)) then
  2150. begin
  2151. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  2152. taicpu(hp1).loadReg(0,taicpu(hp1).oper[1]^.reg);
  2153. taicpu(hp1).loadRef(1,taicpu(p).oper[1]^.ref^);
  2154. taicpu(p).loadReg(1,taicpu(hp1).oper[0]^.reg);
  2155. taicpu(hp1).fileinfo := taicpu(p).fileinfo;
  2156. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 1',p);
  2157. Result:=true;
  2158. exit;
  2159. end;
  2160. {
  2161. mov* x,reg1
  2162. mov* y,reg1
  2163. to
  2164. mov* y,reg1
  2165. }
  2166. if (taicpu(p).oper[1]^.typ=top_reg) and
  2167. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  2168. not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^)) then
  2169. begin
  2170. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 4 done',p);
  2171. { take care of the register (de)allocs following p }
  2172. UpdateUsedRegs(tai(p.next));
  2173. asml.remove(p);
  2174. p.free;
  2175. p:=hp1;
  2176. Result:=true;
  2177. exit;
  2178. end;
  2179. end;
  2180. { search further than the next instruction for a mov }
  2181. if
  2182. { check as much as possible before the expensive GetNextInstructionUsingReg call }
  2183. (taicpu(p).oper[1]^.typ = top_reg) and
  2184. (taicpu(p).oper[0]^.typ in [top_reg,top_const]) and
  2185. not RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp1) and
  2186. { we work with hp2 here, so hp1 can be still used later on when
  2187. checking for GetNextInstruction_p }
  2188. { GetNextInstructionUsingReg only searches one instruction ahead unless -O3 is specified }
  2189. GetNextInstructionUsingReg(hp1,hp2,taicpu(p).oper[1]^.reg) and
  2190. MatchInstruction(hp2,A_MOV,[]) and
  2191. MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  2192. ((taicpu(p).oper[0]^.typ=top_const) or
  2193. ((taicpu(p).oper[0]^.typ=top_reg) and
  2194. not(RegUsedBetween(taicpu(p).oper[0]^.reg, p, hp2))
  2195. )
  2196. ) then
  2197. begin
  2198. { we have
  2199. mov x, %treg
  2200. mov %treg, y
  2201. }
  2202. TransferUsedRegs(TmpUsedRegs);
  2203. TmpUsedRegs[R_INTREGISTER].Update(tai(p.Next));
  2204. { We don't need to call UpdateUsedRegs for every instruction between
  2205. p and hp2 because the register we're concerned about will not
  2206. become deallocated (otherwise GetNextInstructionUsingReg would
  2207. have stopped at an earlier instruction). [Kit] }
  2208. TempRegUsed :=
  2209. RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs) or
  2210. RegReadByInstruction(taicpu(p).oper[1]^.reg, hp1);
  2211. case taicpu(p).oper[0]^.typ Of
  2212. top_reg:
  2213. begin
  2214. { change
  2215. mov %reg, %treg
  2216. mov %treg, y
  2217. to
  2218. mov %reg, y
  2219. }
  2220. CurrentReg := taicpu(p).oper[0]^.reg; { Saves on a handful of pointer dereferences }
  2221. RegName1 := debug_regname(taicpu(hp2).oper[0]^.reg);
  2222. if taicpu(hp2).oper[1]^.reg = CurrentReg then
  2223. begin
  2224. { %reg = y - remove hp2 completely (doing it here instead of relying on
  2225. the "mov %reg,%reg" optimisation might cut down on a pass iteration) }
  2226. if TempRegUsed then
  2227. begin
  2228. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + RegName1 + '; removed unnecessary instruction (MovMov2MovNop 6b}',hp2);
  2229. AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
  2230. asml.remove(hp2);
  2231. hp2.Free;
  2232. end
  2233. else
  2234. begin
  2235. asml.remove(hp2);
  2236. hp2.Free;
  2237. { We can remove the original MOV too }
  2238. DebugMsg(SPeepholeOptimization + 'MovMov2NopNop 6b done',p);
  2239. { take care of the register (de)allocs following p }
  2240. UpdateUsedRegs(tai(p.next));
  2241. asml.remove(p);
  2242. p.free;
  2243. p:=hp1;
  2244. Result:=true;
  2245. Exit;
  2246. end;
  2247. end
  2248. else
  2249. begin
  2250. AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
  2251. taicpu(hp2).loadReg(0, CurrentReg);
  2252. if TempRegUsed then
  2253. begin
  2254. { Don't remove the first instruction if the temporary register is in use }
  2255. DebugMsg(SPeepholeOptimization + RegName1 + ' = ' + debug_regname(CurrentReg) + '; changed to minimise pipeline stall (MovMov2Mov 6a}',hp2);
  2256. { No need to set Result to True. If there's another instruction later on
  2257. that can be optimised, it will be detected when the main Pass 1 loop
  2258. reaches what is now hp2 and passes it through OptPass1MOV. [Kit] };
  2259. end
  2260. else
  2261. begin
  2262. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 6 done',p);
  2263. { take care of the register (de)allocs following p }
  2264. UpdateUsedRegs(tai(p.next));
  2265. asml.remove(p);
  2266. p.free;
  2267. p:=hp1;
  2268. Result:=true;
  2269. Exit;
  2270. end;
  2271. end;
  2272. end;
  2273. top_const:
  2274. if not (cs_opt_size in current_settings.optimizerswitches) or (taicpu(hp2).opsize = S_B) then
  2275. begin
  2276. { change
  2277. mov const, %treg
  2278. mov %treg, y
  2279. to
  2280. mov const, y
  2281. }
  2282. if (taicpu(hp2).oper[1]^.typ=top_reg) or
  2283. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  2284. begin
  2285. RegName1 := debug_regname(taicpu(hp2).oper[0]^.reg);
  2286. taicpu(hp2).loadOper(0,taicpu(p).oper[0]^);
  2287. if TempRegUsed then
  2288. begin
  2289. { Don't remove the first instruction if the temporary register is in use }
  2290. DebugMsg(SPeepholeOptimization + RegName1 + ' = ' + debug_tostr(taicpu(p).oper[0]^.val) + '; changed to minimise pipeline stall (MovMov2Mov 7a)',hp2);
  2291. { No need to set Result to True. If there's another instruction later on
  2292. that can be optimised, it will be detected when the main Pass 1 loop
  2293. reaches what is now hp2 and passes it through OptPass1MOV. [Kit] };
  2294. end
  2295. else
  2296. begin
  2297. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 7 done',p);
  2298. { take care of the register (de)allocs following p }
  2299. UpdateUsedRegs(tai(p.next));
  2300. asml.remove(p);
  2301. p.free;
  2302. p:=hp1;
  2303. Result:=true;
  2304. Exit;
  2305. end;
  2306. end;
  2307. end;
  2308. else
  2309. Internalerror(2019103001);
  2310. end;
  2311. end;
  2312. { Change
  2313. mov %reg1, %reg2
  2314. xxx %reg2, ???
  2315. to
  2316. mov %reg1, %reg2
  2317. xxx %reg1, ???
  2318. to avoid a write/read penalty
  2319. }
  2320. if MatchOpType(taicpu(p),top_reg,top_reg) and
  2321. ((MatchInstruction(hp1,A_OR,A_AND,A_TEST,[]) and
  2322. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2323. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^)) or
  2324. (MatchInstruction(hp1,A_CMP,[]) and
  2325. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  2326. MatchOpType(taicpu(hp1),top_const,top_reg)
  2327. )
  2328. ) then
  2329. { we have
  2330. mov %reg1, %reg2
  2331. test/or/and %reg2, %reg2
  2332. }
  2333. begin
  2334. TransferUsedRegs(TmpUsedRegs);
  2335. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2336. { reg1 will be used after the first instruction,
  2337. so update the allocation info }
  2338. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2339. if GetNextInstruction(hp1, hp2) and
  2340. (hp2.typ = ait_instruction) and
  2341. taicpu(hp2).is_jmp and
  2342. not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp1, TmpUsedRegs)) then
  2343. { change
  2344. mov %reg1, %reg2
  2345. test/or/and %reg2, %reg2
  2346. jxx
  2347. to
  2348. test %reg1, %reg1
  2349. jxx
  2350. }
  2351. begin
  2352. if taicpu(hp1).opcode<>A_CMP then
  2353. taicpu(hp1).loadoper(0,taicpu(p).oper[0]^);
  2354. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  2355. DebugMsg(SPeepholeOptimization + 'MovTest/Cmp/Or/AndJxx2Test/Cmp/Or/AndJxx done',p);
  2356. RemoveCurrentP(p);
  2357. Exit;
  2358. end
  2359. else
  2360. { change
  2361. mov %reg1, %reg2
  2362. test/or/and %reg2, %reg2
  2363. to
  2364. mov %reg1, %reg2
  2365. test/or/and %reg1, %reg1
  2366. }
  2367. begin
  2368. if taicpu(hp1).opcode<>A_CMP then
  2369. taicpu(hp1).loadoper(0,taicpu(p).oper[0]^);
  2370. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  2371. DebugMsg(SPeepholeOptimization + 'MovTest/Cmp/Or/AndJxx2MovTest/Cmp/Or/AndJxx done',p);
  2372. end;
  2373. end;
  2374. { leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  2375. x >= RetOffset) as it doesn't do anything (it writes either to a
  2376. parameter or to the temporary storage room for the function
  2377. result)
  2378. }
  2379. if IsExitCode(hp1) and
  2380. (taicpu(p).oper[1]^.typ = top_ref) and
  2381. (taicpu(p).oper[1]^.ref^.index = NR_NO) and
  2382. (
  2383. (
  2384. (taicpu(p).oper[1]^.ref^.base = current_procinfo.FramePointer) and
  2385. not (
  2386. assigned(current_procinfo.procdef.funcretsym) and
  2387. (taicpu(p).oper[1]^.ref^.offset <= tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)
  2388. )
  2389. ) or
  2390. { Also discard writes to the stack that are below the base pointer,
  2391. as this is temporary storage rather than a function result on the
  2392. stack, say. }
  2393. (
  2394. (taicpu(p).oper[1]^.ref^.base = NR_STACK_POINTER_REG) and
  2395. (taicpu(p).oper[1]^.ref^.offset < current_procinfo.final_localsize)
  2396. )
  2397. ) then
  2398. begin
  2399. asml.remove(p);
  2400. p.free;
  2401. p:=hp1;
  2402. DebugMsg(SPeepholeOptimization + 'removed deadstore before leave/ret',p);
  2403. RemoveLastDeallocForFuncRes(p);
  2404. Result:=true;
  2405. exit;
  2406. end;
  2407. if MatchOpType(taicpu(p),top_reg,top_ref) and
  2408. MatchInstruction(hp1,A_CMP,A_TEST,[taicpu(p).opsize]) and
  2409. (taicpu(hp1).oper[1]^.typ = top_ref) and
  2410. RefsEqual(taicpu(p).oper[1]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  2411. begin
  2412. { change
  2413. mov reg1, mem1
  2414. test/cmp x, mem1
  2415. to
  2416. mov reg1, mem1
  2417. test/cmp x, reg1
  2418. }
  2419. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  2420. DebugMsg(SPeepholeOptimization + 'MovTestCmp2MovTestCmp 1',hp1);
  2421. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2422. exit;
  2423. end;
  2424. if (taicpu(p).oper[1]^.typ = top_reg) and
  2425. (hp1.typ = ait_instruction) and
  2426. GetNextInstruction(hp1, hp2) and
  2427. MatchInstruction(hp2,A_MOV,[]) and
  2428. (SuperRegistersEqual(taicpu(hp2).oper[0]^.reg,taicpu(p).oper[1]^.reg)) and
  2429. (IsFoldableArithOp(taicpu(hp1), taicpu(p).oper[1]^.reg) or
  2430. ((taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and (taicpu(hp2).opsize=S_L) and
  2431. IsFoldableArithOp(taicpu(hp1), newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[1]^.reg),R_SUBQ)))
  2432. ) then
  2433. begin
  2434. if OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  2435. (taicpu(hp2).oper[0]^.typ=top_reg) then
  2436. { change movsX/movzX reg/ref, reg2
  2437. add/sub/or/... reg3/$const, reg2
  2438. mov reg2 reg/ref
  2439. dealloc reg2
  2440. to
  2441. add/sub/or/... reg3/$const, reg/ref }
  2442. begin
  2443. TransferUsedRegs(TmpUsedRegs);
  2444. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2445. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2446. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  2447. begin
  2448. { by example:
  2449. movswl %si,%eax movswl %si,%eax p
  2450. decl %eax addl %edx,%eax hp1
  2451. movw %ax,%si movw %ax,%si hp2
  2452. ->
  2453. movswl %si,%eax movswl %si,%eax p
  2454. decw %eax addw %edx,%eax hp1
  2455. movw %ax,%si movw %ax,%si hp2
  2456. }
  2457. DebugMsg(SPeepholeOptimization + 'MovOpMov2Op ('+
  2458. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  2459. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  2460. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  2461. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  2462. {
  2463. ->
  2464. movswl %si,%eax movswl %si,%eax p
  2465. decw %si addw %dx,%si hp1
  2466. movw %ax,%si movw %ax,%si hp2
  2467. }
  2468. case taicpu(hp1).ops of
  2469. 1:
  2470. begin
  2471. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  2472. if taicpu(hp1).oper[0]^.typ=top_reg then
  2473. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2474. end;
  2475. 2:
  2476. begin
  2477. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  2478. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  2479. (taicpu(hp1).opcode<>A_SHL) and
  2480. (taicpu(hp1).opcode<>A_SHR) and
  2481. (taicpu(hp1).opcode<>A_SAR) then
  2482. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2483. end;
  2484. else
  2485. internalerror(2008042701);
  2486. end;
  2487. {
  2488. ->
  2489. decw %si addw %dx,%si p
  2490. }
  2491. asml.remove(hp2);
  2492. hp2.Free;
  2493. RemoveCurrentP(p);
  2494. Result:=True;
  2495. Exit;
  2496. end;
  2497. end;
  2498. if MatchOpType(taicpu(hp2),top_reg,top_reg) and
  2499. not(SuperRegistersEqual(taicpu(hp1).oper[0]^.reg,taicpu(hp2).oper[1]^.reg)) and
  2500. ((topsize2memsize[taicpu(hp1).opsize]<= topsize2memsize[taicpu(hp2).opsize]) or
  2501. { opsize matters for these opcodes, we could probably work around this, but it is not worth the effort }
  2502. ((taicpu(hp1).opcode<>A_SHL) and (taicpu(hp1).opcode<>A_SHR) and (taicpu(hp1).opcode<>A_SAR))
  2503. )
  2504. {$ifdef i386}
  2505. { byte registers of esi, edi, ebp, esp are not available on i386 }
  2506. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  2507. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(p).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  2508. {$endif i386}
  2509. then
  2510. { change movsX/movzX reg/ref, reg2
  2511. add/sub/or/... regX/$const, reg2
  2512. mov reg2, reg3
  2513. dealloc reg2
  2514. to
  2515. movsX/movzX reg/ref, reg3
  2516. add/sub/or/... reg3/$const, reg3
  2517. }
  2518. begin
  2519. TransferUsedRegs(TmpUsedRegs);
  2520. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2521. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2522. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  2523. begin
  2524. { by example:
  2525. movswl %si,%eax movswl %si,%eax p
  2526. decl %eax addl %edx,%eax hp1
  2527. movw %ax,%si movw %ax,%si hp2
  2528. ->
  2529. movswl %si,%eax movswl %si,%eax p
  2530. decw %eax addw %edx,%eax hp1
  2531. movw %ax,%si movw %ax,%si hp2
  2532. }
  2533. DebugMsg(SPeepholeOptimization + 'MovOpMov2MovOp ('+
  2534. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  2535. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  2536. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  2537. { limit size of constants as well to avoid assembler errors, but
  2538. check opsize to avoid overflow when left shifting the 1 }
  2539. if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=63) then
  2540. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl topsize2memsize[taicpu(hp2).opsize])-1);
  2541. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  2542. taicpu(p).changeopsize(taicpu(hp2).opsize);
  2543. if taicpu(p).oper[0]^.typ=top_reg then
  2544. setsubreg(taicpu(p).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2545. taicpu(p).loadoper(1, taicpu(hp2).oper[1]^);
  2546. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,usedregs);
  2547. {
  2548. ->
  2549. movswl %si,%eax movswl %si,%eax p
  2550. decw %si addw %dx,%si hp1
  2551. movw %ax,%si movw %ax,%si hp2
  2552. }
  2553. case taicpu(hp1).ops of
  2554. 1:
  2555. begin
  2556. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  2557. if taicpu(hp1).oper[0]^.typ=top_reg then
  2558. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2559. end;
  2560. 2:
  2561. begin
  2562. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  2563. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  2564. (taicpu(hp1).opcode<>A_SHL) and
  2565. (taicpu(hp1).opcode<>A_SHR) and
  2566. (taicpu(hp1).opcode<>A_SAR) then
  2567. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2568. end;
  2569. else
  2570. internalerror(2018111801);
  2571. end;
  2572. {
  2573. ->
  2574. decw %si addw %dx,%si p
  2575. }
  2576. asml.remove(hp2);
  2577. hp2.Free;
  2578. end;
  2579. end;
  2580. end;
  2581. if MatchInstruction(hp1,A_BTS,A_BTR,[Taicpu(p).opsize]) and
  2582. GetNextInstruction(hp1, hp2) and
  2583. MatchInstruction(hp2,A_OR,[Taicpu(p).opsize]) and
  2584. MatchOperand(Taicpu(p).oper[0]^,0) and
  2585. (Taicpu(p).oper[1]^.typ = top_reg) and
  2586. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp1).oper[1]^) and
  2587. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp2).oper[1]^) then
  2588. { mov reg1,0
  2589. bts reg1,operand1 --> mov reg1,operand2
  2590. or reg1,operand2 bts reg1,operand1}
  2591. begin
  2592. Taicpu(hp2).opcode:=A_MOV;
  2593. asml.remove(hp1);
  2594. insertllitem(hp2,hp2.next,hp1);
  2595. asml.remove(p);
  2596. p.free;
  2597. p:=hp1;
  2598. Result:=true;
  2599. exit;
  2600. end;
  2601. if MatchInstruction(hp1,A_LEA,[S_L]) and
  2602. MatchOpType(Taicpu(p),top_ref,top_reg) and
  2603. ((MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(hp1).oper[1]^.reg,Taicpu(p).oper[1]^.reg) and
  2604. (Taicpu(hp1).oper[0]^.ref^.base<>Taicpu(p).oper[1]^.reg)
  2605. ) or
  2606. (MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(p).oper[1]^.reg,Taicpu(hp1).oper[1]^.reg) and
  2607. (Taicpu(hp1).oper[0]^.ref^.index<>Taicpu(p).oper[1]^.reg)
  2608. )
  2609. ) then
  2610. { mov reg1,ref
  2611. lea reg2,[reg1,reg2]
  2612. to
  2613. add reg2,ref}
  2614. begin
  2615. TransferUsedRegs(TmpUsedRegs);
  2616. { reg1 may not be used afterwards }
  2617. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  2618. begin
  2619. Taicpu(hp1).opcode:=A_ADD;
  2620. Taicpu(hp1).oper[0]^.ref^:=Taicpu(p).oper[0]^.ref^;
  2621. DebugMsg(SPeepholeOptimization + 'MovLea2Add done',hp1);
  2622. asml.remove(p);
  2623. p.free;
  2624. p:=hp1;
  2625. result:=true;
  2626. exit;
  2627. end;
  2628. end;
  2629. end;
  2630. function TX86AsmOptimizer.OptPass1MOVXX(var p : tai) : boolean;
  2631. var
  2632. hp1 : tai;
  2633. begin
  2634. Result:=false;
  2635. if taicpu(p).ops <> 2 then
  2636. exit;
  2637. if GetNextInstruction(p,hp1) and
  2638. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  2639. (taicpu(hp1).ops = 2) then
  2640. begin
  2641. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  2642. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  2643. { movXX reg1, mem1 or movXX mem1, reg1
  2644. movXX mem2, reg2 movXX reg2, mem2}
  2645. begin
  2646. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  2647. { movXX reg1, mem1 or movXX mem1, reg1
  2648. movXX mem2, reg1 movXX reg2, mem1}
  2649. begin
  2650. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2651. begin
  2652. { Removes the second statement from
  2653. movXX reg1, mem1/reg2
  2654. movXX mem1/reg2, reg1
  2655. }
  2656. if taicpu(p).oper[0]^.typ=top_reg then
  2657. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2658. { Removes the second statement from
  2659. movXX mem1/reg1, reg2
  2660. movXX reg2, mem1/reg1
  2661. }
  2662. if (taicpu(p).oper[1]^.typ=top_reg) and
  2663. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)) then
  2664. begin
  2665. asml.remove(p);
  2666. p.free;
  2667. GetNextInstruction(hp1,p);
  2668. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2Nop 1 done',p);
  2669. end
  2670. else
  2671. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
  2672. asml.remove(hp1);
  2673. hp1.free;
  2674. Result:=true;
  2675. exit;
  2676. end
  2677. end;
  2678. end;
  2679. end;
  2680. end;
  2681. function TX86AsmOptimizer.OptPass1OP(var p : tai) : boolean;
  2682. var
  2683. hp1 : tai;
  2684. begin
  2685. result:=false;
  2686. { replace
  2687. <Op>X %mreg1,%mreg2 // Op in [ADD,MUL]
  2688. MovX %mreg2,%mreg1
  2689. dealloc %mreg2
  2690. by
  2691. <Op>X %mreg2,%mreg1
  2692. ?
  2693. }
  2694. if GetNextInstruction(p,hp1) and
  2695. { we mix single and double opperations here because we assume that the compiler
  2696. generates vmovapd only after double operations and vmovaps only after single operations }
  2697. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  2698. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2699. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  2700. (taicpu(p).oper[0]^.typ=top_reg) then
  2701. begin
  2702. TransferUsedRegs(TmpUsedRegs);
  2703. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2704. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2705. begin
  2706. taicpu(p).loadoper(0,taicpu(hp1).oper[0]^);
  2707. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  2708. DebugMsg(SPeepholeOptimization + 'OpMov2Op done',p);
  2709. asml.Remove(hp1);
  2710. hp1.Free;
  2711. result:=true;
  2712. end;
  2713. end;
  2714. end;
  2715. function TX86AsmOptimizer.OptPass1LEA(var p : tai) : boolean;
  2716. var
  2717. hp1, hp2, hp3: tai;
  2718. l : ASizeInt;
  2719. ref: Integer;
  2720. saveref: treference;
  2721. begin
  2722. Result:=false;
  2723. { removes seg register prefixes from LEA operations, as they
  2724. don't do anything}
  2725. taicpu(p).oper[0]^.ref^.Segment:=NR_NO;
  2726. { changes "lea (%reg1), %reg2" into "mov %reg1, %reg2" }
  2727. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  2728. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  2729. { do not mess with leas acessing the stack pointer }
  2730. (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) and
  2731. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  2732. begin
  2733. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  2734. (taicpu(p).oper[0]^.ref^.offset = 0) then
  2735. begin
  2736. hp1:=taicpu.op_reg_reg(A_MOV,taicpu(p).opsize,taicpu(p).oper[0]^.ref^.base,
  2737. taicpu(p).oper[1]^.reg);
  2738. InsertLLItem(p.previous,p.next, hp1);
  2739. DebugMsg(SPeepholeOptimization + 'Lea2Mov done',hp1);
  2740. p.free;
  2741. p:=hp1;
  2742. Result:=true;
  2743. exit;
  2744. end
  2745. else if (taicpu(p).oper[0]^.ref^.offset = 0) then
  2746. begin
  2747. DebugMsg(SPeepholeOptimization + 'Lea2Nop done',p);
  2748. RemoveCurrentP(p);
  2749. Result:=true;
  2750. exit;
  2751. end
  2752. { continue to use lea to adjust the stack pointer,
  2753. it is the recommended way, but only if not optimizing for size }
  2754. else if (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) or
  2755. (cs_opt_size in current_settings.optimizerswitches) then
  2756. with taicpu(p).oper[0]^.ref^ do
  2757. if (base = taicpu(p).oper[1]^.reg) then
  2758. begin
  2759. l:=offset;
  2760. if (l=1) and UseIncDec then
  2761. begin
  2762. taicpu(p).opcode:=A_INC;
  2763. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  2764. taicpu(p).ops:=1;
  2765. DebugMsg(SPeepholeOptimization + 'Lea2Inc done',p);
  2766. end
  2767. else if (l=-1) and UseIncDec then
  2768. begin
  2769. taicpu(p).opcode:=A_DEC;
  2770. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  2771. taicpu(p).ops:=1;
  2772. DebugMsg(SPeepholeOptimization + 'Lea2Dec done',p);
  2773. end
  2774. else
  2775. begin
  2776. if (l<0) and (l<>-2147483648) then
  2777. begin
  2778. taicpu(p).opcode:=A_SUB;
  2779. taicpu(p).loadConst(0,-l);
  2780. DebugMsg(SPeepholeOptimization + 'Lea2Sub done',p);
  2781. end
  2782. else
  2783. begin
  2784. taicpu(p).opcode:=A_ADD;
  2785. taicpu(p).loadConst(0,l);
  2786. DebugMsg(SPeepholeOptimization + 'Lea2Add done',p);
  2787. end;
  2788. end;
  2789. Result:=true;
  2790. exit;
  2791. end;
  2792. end;
  2793. if GetNextInstruction(p,hp1) and
  2794. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  2795. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2796. MatchOpType(Taicpu(hp1),top_reg,top_reg) and
  2797. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) then
  2798. begin
  2799. TransferUsedRegs(TmpUsedRegs);
  2800. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2801. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2802. begin
  2803. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  2804. DebugMsg(SPeepholeOptimization + 'LeaMov2Lea done',p);
  2805. asml.Remove(hp1);
  2806. hp1.Free;
  2807. result:=true;
  2808. end;
  2809. end;
  2810. { changes
  2811. lea offset1(regX), reg1
  2812. lea offset2(reg1), reg1
  2813. to
  2814. lea offset1+offset2(regX), reg1 }
  2815. { for now, we do not mess with the stack pointer, thought it might be usefull to remove
  2816. unneeded lea sequences on the stack pointer, it needs to be tested in detail }
  2817. if (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) and
  2818. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) and
  2819. MatchInstruction(hp1,A_LEA,[taicpu(p).opsize]) and
  2820. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  2821. (taicpu(hp1).oper[0]^.ref^.base=taicpu(p).oper[1]^.reg) and
  2822. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  2823. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  2824. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  2825. (((taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) and
  2826. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  2827. (taicpu(p).oper[0]^.ref^.index=taicpu(hp1).oper[0]^.ref^.index) and
  2828. (taicpu(p).oper[0]^.ref^.scalefactor=taicpu(hp1).oper[0]^.ref^.scalefactor)
  2829. ) or
  2830. ((taicpu(hp1).oper[0]^.ref^.scalefactor in [0,1]) and
  2831. (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  2832. not(RegUsedBetween(taicpu(p).oper[0]^.ref^.index,p,hp1)))
  2833. ) and
  2834. not(RegUsedBetween(taicpu(p).oper[0]^.ref^.base,p,hp1)) and
  2835. (taicpu(p).oper[0]^.ref^.relsymbol=taicpu(hp1).oper[0]^.ref^.relsymbol) and
  2836. (taicpu(p).oper[0]^.ref^.segment=taicpu(hp1).oper[0]^.ref^.segment) and
  2837. (taicpu(p).oper[0]^.ref^.symbol=taicpu(hp1).oper[0]^.ref^.symbol) then
  2838. begin
  2839. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea done',p);
  2840. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  2841. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  2842. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  2843. begin
  2844. taicpu(hp1).oper[0]^.ref^.base:=taicpu(hp1).oper[0]^.ref^.index;
  2845. taicpu(hp1).oper[0]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  2846. taicpu(hp1).oper[0]^.ref^.scalefactor:=taicpu(p).oper[0]^.ref^.scalefactor;
  2847. end;
  2848. RemoveCurrentP(p);
  2849. result:=true;
  2850. exit;
  2851. end;
  2852. { changes
  2853. lea <ref1>, reg1
  2854. <op> ...,<ref. with reg1>,...
  2855. to
  2856. <op> ...,<ref1>,... }
  2857. if (taicpu(p).oper[1]^.reg<>current_procinfo.framepointer) and
  2858. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) and
  2859. GetNextInstruction(p,hp1) and
  2860. (hp1.typ=ait_instruction) and
  2861. not(MatchInstruction(hp1,A_LEA,[])) then
  2862. begin
  2863. { find a reference which uses reg1 }
  2864. 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
  2865. ref:=0
  2866. 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
  2867. ref:=1
  2868. else
  2869. ref:=-1;
  2870. if (ref<>-1) and
  2871. { reg1 must be either the base or the index }
  2872. ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) xor (taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg)) then
  2873. begin
  2874. { reg1 can be removed from the reference }
  2875. saveref:=taicpu(hp1).oper[ref]^.ref^;
  2876. if taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg then
  2877. taicpu(hp1).oper[ref]^.ref^.base:=NR_NO
  2878. else if taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg then
  2879. taicpu(hp1).oper[ref]^.ref^.index:=NR_NO
  2880. else
  2881. Internalerror(2019111201);
  2882. { check if the can insert all data of the lea into the second instruction }
  2883. if ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) or (taicpu(hp1).oper[ref]^.ref^.scalefactor in [0,1])) and
  2884. ((taicpu(p).oper[0]^.ref^.base=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.base=NR_NO)) and
  2885. ((taicpu(p).oper[0]^.ref^.index=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.index=NR_NO)) and
  2886. ((taicpu(p).oper[0]^.ref^.symbol=nil) or (taicpu(hp1).oper[ref]^.ref^.symbol=nil)) and
  2887. ((taicpu(p).oper[0]^.ref^.relsymbol=nil) or (taicpu(hp1).oper[ref]^.ref^.relsymbol=nil)) and
  2888. ((taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) or (taicpu(hp1).oper[ref]^.ref^.scalefactor in [0,1])) and
  2889. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.segment=NR_NO)
  2890. {$ifdef x86_64}
  2891. and (abs(taicpu(hp1).oper[ref]^.ref^.offset+taicpu(p).oper[0]^.ref^.offset)<=$7fffffff)
  2892. and (((taicpu(p).oper[0]^.ref^.base<>NR_RIP) and (taicpu(p).oper[0]^.ref^.index<>NR_RIP)) or
  2893. ((taicpu(hp1).oper[ref]^.ref^.base=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.index=NR_NO))
  2894. )
  2895. {$endif x86_64}
  2896. then
  2897. begin
  2898. { reg1 might not used by the second instruction after it is remove from the reference }
  2899. if not(RegInInstruction(taicpu(p).oper[1]^.reg,taicpu(hp1))) then
  2900. begin
  2901. TransferUsedRegs(TmpUsedRegs);
  2902. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2903. { reg1 is not updated so it might not be used afterwards }
  2904. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2905. begin
  2906. DebugMsg(SPeepholeOptimization + 'LeaOp2Op done',p);
  2907. if taicpu(p).oper[0]^.ref^.base<>NR_NO then
  2908. taicpu(hp1).oper[ref]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  2909. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  2910. taicpu(hp1).oper[ref]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  2911. if taicpu(p).oper[0]^.ref^.symbol<>nil then
  2912. taicpu(hp1).oper[ref]^.ref^.symbol:=taicpu(p).oper[0]^.ref^.symbol;
  2913. if taicpu(p).oper[0]^.ref^.relsymbol<>nil then
  2914. taicpu(hp1).oper[ref]^.ref^.relsymbol:=taicpu(p).oper[0]^.ref^.relsymbol;
  2915. if not(taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) then
  2916. taicpu(hp1).oper[ref]^.ref^.scalefactor:=taicpu(p).oper[0]^.ref^.scalefactor;
  2917. inc(taicpu(hp1).oper[ref]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  2918. RemoveCurrentP(p);
  2919. result:=true;
  2920. exit;
  2921. end
  2922. end;
  2923. end;
  2924. { recover }
  2925. taicpu(hp1).oper[ref]^.ref^:=saveref;
  2926. end;
  2927. end;
  2928. { replace
  2929. lea x(stackpointer),stackpointer
  2930. call procname
  2931. lea -x(stackpointer),stackpointer
  2932. ret
  2933. by
  2934. jmp procname
  2935. this should never hurt except when pic is used, not sure
  2936. how to handle it then
  2937. but do it only on level 4 because it destroys stack back traces
  2938. }
  2939. if (cs_opt_level4 in current_settings.optimizerswitches) and
  2940. not(cs_create_pic in current_settings.moduleswitches) and
  2941. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  2942. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  2943. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  2944. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  2945. (taicpu(p).oper[0]^.ref^.scalefactor in [0,1]) and
  2946. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  2947. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  2948. GetNextInstruction(p, hp1) and
  2949. MatchInstruction(hp1,A_CALL,[S_NO]) and
  2950. GetNextInstruction(hp1, hp2) and
  2951. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  2952. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  2953. (taicpu(p).oper[0]^.ref^.base=taicpu(hp2).oper[0]^.ref^.base) and
  2954. (taicpu(p).oper[0]^.ref^.index=taicpu(hp2).oper[0]^.ref^.index) and
  2955. (taicpu(p).oper[0]^.ref^.offset=-taicpu(hp2).oper[0]^.ref^.offset) and
  2956. (taicpu(p).oper[0]^.ref^.relsymbol=taicpu(hp2).oper[0]^.ref^.relsymbol) and
  2957. (taicpu(p).oper[0]^.ref^.scalefactor=taicpu(hp2).oper[0]^.ref^.scalefactor) and
  2958. (taicpu(p).oper[0]^.ref^.segment=taicpu(hp2).oper[0]^.ref^.segment) and
  2959. (taicpu(p).oper[0]^.ref^.symbol=taicpu(hp2).oper[0]^.ref^.symbol) and
  2960. GetNextInstruction(hp2, hp3) and
  2961. MatchInstruction(hp3,A_RET,[S_NO]) and
  2962. (taicpu(hp3).ops=0) then
  2963. begin
  2964. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  2965. taicpu(hp1).opcode:=A_JMP;
  2966. taicpu(hp1).is_jmp:=true;
  2967. asml.remove(p);
  2968. asml.remove(hp2);
  2969. asml.remove(hp3);
  2970. p.free;
  2971. hp2.free;
  2972. hp3.free;
  2973. p:=hp1;
  2974. Result:=true;
  2975. end;
  2976. end;
  2977. function TX86AsmOptimizer.DoSubAddOpt(var p: tai): Boolean;
  2978. var
  2979. hp1 : tai;
  2980. begin
  2981. DoSubAddOpt := False;
  2982. if GetLastInstruction(p, hp1) and
  2983. (hp1.typ = ait_instruction) and
  2984. (taicpu(hp1).opsize = taicpu(p).opsize) then
  2985. case taicpu(hp1).opcode Of
  2986. A_DEC:
  2987. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  2988. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2989. begin
  2990. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
  2991. asml.remove(hp1);
  2992. hp1.free;
  2993. end;
  2994. A_SUB:
  2995. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  2996. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  2997. begin
  2998. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
  2999. asml.remove(hp1);
  3000. hp1.free;
  3001. end;
  3002. A_ADD:
  3003. begin
  3004. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  3005. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  3006. begin
  3007. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  3008. asml.remove(hp1);
  3009. hp1.free;
  3010. if (taicpu(p).oper[0]^.val = 0) then
  3011. begin
  3012. hp1 := tai(p.next);
  3013. asml.remove(p);
  3014. p.free;
  3015. if not GetLastInstruction(hp1, p) then
  3016. p := hp1;
  3017. DoSubAddOpt := True;
  3018. end
  3019. end;
  3020. end;
  3021. else
  3022. ;
  3023. end;
  3024. end;
  3025. function TX86AsmOptimizer.OptPass1Sub(var p : tai) : boolean;
  3026. {$ifdef i386}
  3027. var
  3028. hp1 : tai;
  3029. {$endif i386}
  3030. begin
  3031. Result:=false;
  3032. { * change "subl $2, %esp; pushw x" to "pushl x"}
  3033. { * change "sub/add const1, reg" or "dec reg" followed by
  3034. "sub const2, reg" to one "sub ..., reg" }
  3035. if MatchOpType(taicpu(p),top_const,top_reg) then
  3036. begin
  3037. {$ifdef i386}
  3038. if (taicpu(p).oper[0]^.val = 2) and
  3039. (taicpu(p).oper[1]^.reg = NR_ESP) and
  3040. { Don't do the sub/push optimization if the sub }
  3041. { comes from setting up the stack frame (JM) }
  3042. (not(GetLastInstruction(p,hp1)) or
  3043. not(MatchInstruction(hp1,A_MOV,[S_L]) and
  3044. MatchOperand(taicpu(hp1).oper[0]^,NR_ESP) and
  3045. MatchOperand(taicpu(hp1).oper[0]^,NR_EBP))) then
  3046. begin
  3047. hp1 := tai(p.next);
  3048. while Assigned(hp1) and
  3049. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  3050. not RegReadByInstruction(NR_ESP,hp1) and
  3051. not RegModifiedByInstruction(NR_ESP,hp1) do
  3052. hp1 := tai(hp1.next);
  3053. if Assigned(hp1) and
  3054. MatchInstruction(hp1,A_PUSH,[S_W]) then
  3055. begin
  3056. taicpu(hp1).changeopsize(S_L);
  3057. if taicpu(hp1).oper[0]^.typ=top_reg then
  3058. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  3059. hp1 := tai(p.next);
  3060. asml.remove(p);
  3061. p.free;
  3062. p := hp1;
  3063. Result:=true;
  3064. exit;
  3065. end;
  3066. end;
  3067. {$endif i386}
  3068. if DoSubAddOpt(p) then
  3069. Result:=true;
  3070. end;
  3071. end;
  3072. function TX86AsmOptimizer.OptPass1SHLSAL(var p : tai) : boolean;
  3073. var
  3074. TmpBool1,TmpBool2 : Boolean;
  3075. tmpref : treference;
  3076. hp1,hp2: tai;
  3077. begin
  3078. Result:=false;
  3079. if MatchOpType(taicpu(p),top_const,top_reg) and
  3080. (taicpu(p).opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  3081. (taicpu(p).oper[0]^.val <= 3) then
  3082. { Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement }
  3083. begin
  3084. { should we check the next instruction? }
  3085. TmpBool1 := True;
  3086. { have we found an add/sub which could be
  3087. integrated in the lea? }
  3088. TmpBool2 := False;
  3089. reference_reset(tmpref,2,[]);
  3090. TmpRef.index := taicpu(p).oper[1]^.reg;
  3091. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  3092. while TmpBool1 and
  3093. GetNextInstruction(p, hp1) and
  3094. (tai(hp1).typ = ait_instruction) and
  3095. ((((taicpu(hp1).opcode = A_ADD) or
  3096. (taicpu(hp1).opcode = A_SUB)) and
  3097. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  3098. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  3099. (((taicpu(hp1).opcode = A_INC) or
  3100. (taicpu(hp1).opcode = A_DEC)) and
  3101. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  3102. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg)) or
  3103. ((taicpu(hp1).opcode = A_LEA) and
  3104. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  3105. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg))) and
  3106. (not GetNextInstruction(hp1,hp2) or
  3107. not instrReadsFlags(hp2)) Do
  3108. begin
  3109. TmpBool1 := False;
  3110. if taicpu(hp1).opcode=A_LEA then
  3111. begin
  3112. if (TmpRef.base = NR_NO) and
  3113. (taicpu(hp1).oper[0]^.ref^.symbol=nil) and
  3114. (taicpu(hp1).oper[0]^.ref^.relsymbol=nil) and
  3115. (taicpu(hp1).oper[0]^.ref^.segment=NR_NO) and
  3116. ((taicpu(hp1).oper[0]^.ref^.scalefactor=0) or
  3117. (taicpu(hp1).oper[0]^.ref^.scalefactor*tmpref.scalefactor<=8)) then
  3118. begin
  3119. TmpBool1 := True;
  3120. TmpBool2 := True;
  3121. inc(TmpRef.offset, taicpu(hp1).oper[0]^.ref^.offset);
  3122. if taicpu(hp1).oper[0]^.ref^.scalefactor<>0 then
  3123. tmpref.scalefactor:=tmpref.scalefactor*taicpu(hp1).oper[0]^.ref^.scalefactor;
  3124. TmpRef.base := taicpu(hp1).oper[0]^.ref^.base;
  3125. asml.remove(hp1);
  3126. hp1.free;
  3127. end
  3128. end
  3129. else if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  3130. begin
  3131. TmpBool1 := True;
  3132. TmpBool2 := True;
  3133. case taicpu(hp1).opcode of
  3134. A_ADD:
  3135. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  3136. A_SUB:
  3137. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  3138. else
  3139. internalerror(2019050536);
  3140. end;
  3141. asml.remove(hp1);
  3142. hp1.free;
  3143. end
  3144. else
  3145. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  3146. (((taicpu(hp1).opcode = A_ADD) and
  3147. (TmpRef.base = NR_NO)) or
  3148. (taicpu(hp1).opcode = A_INC) or
  3149. (taicpu(hp1).opcode = A_DEC)) then
  3150. begin
  3151. TmpBool1 := True;
  3152. TmpBool2 := True;
  3153. case taicpu(hp1).opcode of
  3154. A_ADD:
  3155. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  3156. A_INC:
  3157. inc(TmpRef.offset);
  3158. A_DEC:
  3159. dec(TmpRef.offset);
  3160. else
  3161. internalerror(2019050535);
  3162. end;
  3163. asml.remove(hp1);
  3164. hp1.free;
  3165. end;
  3166. end;
  3167. if TmpBool2
  3168. {$ifndef x86_64}
  3169. or
  3170. ((current_settings.optimizecputype < cpu_Pentium2) and
  3171. (taicpu(p).oper[0]^.val <= 3) and
  3172. not(cs_opt_size in current_settings.optimizerswitches))
  3173. {$endif x86_64}
  3174. then
  3175. begin
  3176. if not(TmpBool2) and
  3177. (taicpu(p).oper[0]^.val=1) then
  3178. begin
  3179. hp1:=taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  3180. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  3181. end
  3182. else
  3183. hp1:=taicpu.op_ref_reg(A_LEA, taicpu(p).opsize, TmpRef,
  3184. taicpu(p).oper[1]^.reg);
  3185. DebugMsg(SPeepholeOptimization + 'ShlAddLeaSubIncDec2Lea',p);
  3186. InsertLLItem(p.previous, p.next, hp1);
  3187. p.free;
  3188. p := hp1;
  3189. end;
  3190. end
  3191. {$ifndef x86_64}
  3192. else if (current_settings.optimizecputype < cpu_Pentium2) and
  3193. MatchOpType(taicpu(p),top_const,top_reg) then
  3194. begin
  3195. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  3196. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  3197. (unlike shl, which is only Tairable in the U pipe) }
  3198. if taicpu(p).oper[0]^.val=1 then
  3199. begin
  3200. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  3201. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  3202. InsertLLItem(p.previous, p.next, hp1);
  3203. p.free;
  3204. p := hp1;
  3205. end
  3206. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  3207. "shl $3, %reg" to "lea (,%reg,8), %reg }
  3208. else if (taicpu(p).opsize = S_L) and
  3209. (taicpu(p).oper[0]^.val<= 3) then
  3210. begin
  3211. reference_reset(tmpref,2,[]);
  3212. TmpRef.index := taicpu(p).oper[1]^.reg;
  3213. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  3214. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  3215. InsertLLItem(p.previous, p.next, hp1);
  3216. p.free;
  3217. p := hp1;
  3218. end;
  3219. end
  3220. {$endif x86_64}
  3221. ;
  3222. end;
  3223. function TX86AsmOptimizer.OptPass1SETcc(var p: tai): boolean;
  3224. var
  3225. hp1,hp2,next: tai; SetC, JumpC: TAsmCond; Unconditional: Boolean;
  3226. begin
  3227. Result:=false;
  3228. if MatchOpType(taicpu(p),top_reg) and
  3229. GetNextInstruction(p, hp1) and
  3230. ((MatchInstruction(hp1, A_TEST, [S_B]) and
  3231. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3232. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg)) or
  3233. (MatchInstruction(hp1, A_CMP, [S_B]) and
  3234. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3235. (taicpu(hp1).oper[0]^.val=0))
  3236. ) and
  3237. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  3238. GetNextInstruction(hp1, hp2) and
  3239. MatchInstruction(hp2, A_Jcc, []) then
  3240. { Change from: To:
  3241. set(C) %reg j(~C) label
  3242. test %reg,%reg/cmp $0,%reg
  3243. je label
  3244. set(C) %reg j(C) label
  3245. test %reg,%reg/cmp $0,%reg
  3246. jne label
  3247. }
  3248. begin
  3249. next := tai(p.Next);
  3250. TransferUsedRegs(TmpUsedRegs);
  3251. UpdateUsedRegs(TmpUsedRegs, next);
  3252. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  3253. JumpC := taicpu(hp2).condition;
  3254. Unconditional := False;
  3255. if conditions_equal(JumpC, C_E) then
  3256. SetC := inverse_cond(taicpu(p).condition)
  3257. else if conditions_equal(JumpC, C_NE) then
  3258. SetC := taicpu(p).condition
  3259. else
  3260. { We've got something weird here (and inefficent) }
  3261. begin
  3262. DebugMsg('DEBUG: Inefficient jump - check code generation', p);
  3263. SetC := C_NONE;
  3264. { JAE/JNB will always branch (use 'condition_in', since C_AE <> C_NB normally) }
  3265. if condition_in(C_AE, JumpC) then
  3266. Unconditional := True
  3267. else
  3268. { Not sure what to do with this jump - drop out }
  3269. Exit;
  3270. end;
  3271. asml.Remove(hp1);
  3272. hp1.Free;
  3273. if Unconditional then
  3274. MakeUnconditional(taicpu(hp2))
  3275. else
  3276. begin
  3277. if SetC = C_NONE then
  3278. InternalError(2018061401);
  3279. taicpu(hp2).SetCondition(SetC);
  3280. end;
  3281. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
  3282. begin
  3283. asml.Remove(p);
  3284. UpdateUsedRegs(next);
  3285. p.Free;
  3286. Result := True;
  3287. p := hp2;
  3288. end;
  3289. DebugMsg(SPeepholeOptimization + 'SETcc/TESTCmp/Jcc -> Jcc',p);
  3290. end;
  3291. end;
  3292. function TX86AsmOptimizer.OptPass1FSTP(var p: tai): boolean;
  3293. { returns true if a "continue" should be done after this optimization }
  3294. var
  3295. hp1, hp2: tai;
  3296. begin
  3297. Result := false;
  3298. if MatchOpType(taicpu(p),top_ref) and
  3299. GetNextInstruction(p, hp1) and
  3300. (hp1.typ = ait_instruction) and
  3301. (((taicpu(hp1).opcode = A_FLD) and
  3302. (taicpu(p).opcode = A_FSTP)) or
  3303. ((taicpu(p).opcode = A_FISTP) and
  3304. (taicpu(hp1).opcode = A_FILD))) and
  3305. MatchOpType(taicpu(hp1),top_ref) and
  3306. (taicpu(hp1).opsize = taicpu(p).opsize) and
  3307. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  3308. begin
  3309. { replacing fstp f;fld f by fst f is only valid for extended because of rounding }
  3310. if (taicpu(p).opsize=S_FX) and
  3311. GetNextInstruction(hp1, hp2) and
  3312. (hp2.typ = ait_instruction) and
  3313. IsExitCode(hp2) and
  3314. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  3315. not(assigned(current_procinfo.procdef.funcretsym) and
  3316. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  3317. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  3318. begin
  3319. asml.remove(p);
  3320. asml.remove(hp1);
  3321. p.free;
  3322. hp1.free;
  3323. p := hp2;
  3324. RemoveLastDeallocForFuncRes(p);
  3325. Result := true;
  3326. end
  3327. (* can't be done because the store operation rounds
  3328. else
  3329. { fst can't store an extended value! }
  3330. if (taicpu(p).opsize <> S_FX) and
  3331. (taicpu(p).opsize <> S_IQ) then
  3332. begin
  3333. if (taicpu(p).opcode = A_FSTP) then
  3334. taicpu(p).opcode := A_FST
  3335. else taicpu(p).opcode := A_FIST;
  3336. asml.remove(hp1);
  3337. hp1.free;
  3338. end
  3339. *)
  3340. end;
  3341. end;
  3342. function TX86AsmOptimizer.OptPass1FLD(var p : tai) : boolean;
  3343. var
  3344. hp1, hp2: tai;
  3345. begin
  3346. result:=false;
  3347. if MatchOpType(taicpu(p),top_reg) and
  3348. GetNextInstruction(p, hp1) and
  3349. (hp1.typ = Ait_Instruction) and
  3350. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3351. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  3352. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  3353. { change to
  3354. fld reg fxxx reg,st
  3355. fxxxp st, st1 (hp1)
  3356. Remark: non commutative operations must be reversed!
  3357. }
  3358. begin
  3359. case taicpu(hp1).opcode Of
  3360. A_FMULP,A_FADDP,
  3361. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  3362. begin
  3363. case taicpu(hp1).opcode Of
  3364. A_FADDP: taicpu(hp1).opcode := A_FADD;
  3365. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  3366. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  3367. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  3368. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  3369. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  3370. else
  3371. internalerror(2019050534);
  3372. end;
  3373. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  3374. taicpu(hp1).oper[1]^.reg := NR_ST;
  3375. asml.remove(p);
  3376. p.free;
  3377. p := hp1;
  3378. Result:=true;
  3379. exit;
  3380. end;
  3381. else
  3382. ;
  3383. end;
  3384. end
  3385. else
  3386. if MatchOpType(taicpu(p),top_ref) and
  3387. GetNextInstruction(p, hp2) and
  3388. (hp2.typ = Ait_Instruction) and
  3389. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  3390. (taicpu(p).opsize in [S_FS, S_FL]) and
  3391. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  3392. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  3393. if GetLastInstruction(p, hp1) and
  3394. MatchInstruction(hp1,A_FLD,A_FST,[taicpu(p).opsize]) and
  3395. MatchOpType(taicpu(hp1),top_ref) and
  3396. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  3397. if ((taicpu(hp2).opcode = A_FMULP) or
  3398. (taicpu(hp2).opcode = A_FADDP)) then
  3399. { change to
  3400. fld/fst mem1 (hp1) fld/fst mem1
  3401. fld mem1 (p) fadd/
  3402. faddp/ fmul st, st
  3403. fmulp st, st1 (hp2) }
  3404. begin
  3405. asml.remove(p);
  3406. p.free;
  3407. p := hp1;
  3408. if (taicpu(hp2).opcode = A_FADDP) then
  3409. taicpu(hp2).opcode := A_FADD
  3410. else
  3411. taicpu(hp2).opcode := A_FMUL;
  3412. taicpu(hp2).oper[1]^.reg := NR_ST;
  3413. end
  3414. else
  3415. { change to
  3416. fld/fst mem1 (hp1) fld/fst mem1
  3417. fld mem1 (p) fld st}
  3418. begin
  3419. taicpu(p).changeopsize(S_FL);
  3420. taicpu(p).loadreg(0,NR_ST);
  3421. end
  3422. else
  3423. begin
  3424. case taicpu(hp2).opcode Of
  3425. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  3426. { change to
  3427. fld/fst mem1 (hp1) fld/fst mem1
  3428. fld mem2 (p) fxxx mem2
  3429. fxxxp st, st1 (hp2) }
  3430. begin
  3431. case taicpu(hp2).opcode Of
  3432. A_FADDP: taicpu(p).opcode := A_FADD;
  3433. A_FMULP: taicpu(p).opcode := A_FMUL;
  3434. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  3435. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  3436. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  3437. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  3438. else
  3439. internalerror(2019050533);
  3440. end;
  3441. asml.remove(hp2);
  3442. hp2.free;
  3443. end
  3444. else
  3445. ;
  3446. end
  3447. end
  3448. end;
  3449. function TX86AsmOptimizer.OptPass1Cmp(var p: tai): boolean;
  3450. var
  3451. v: TCGInt;
  3452. hp1, hp2: tai;
  3453. begin
  3454. Result:=false;
  3455. if taicpu(p).oper[0]^.typ = top_const then
  3456. begin
  3457. { Though GetNextInstruction can be factored out, it is an expensive
  3458. call, so delay calling it until we have first checked cheaper
  3459. conditions that are independent of it. }
  3460. if (taicpu(p).oper[0]^.val = 0) and
  3461. (taicpu(p).oper[1]^.typ = top_reg) and
  3462. GetNextInstruction(p, hp1) and
  3463. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) then
  3464. begin
  3465. hp2 := p;
  3466. { When dealing with "cmp $0,%reg", only ZF and SF contain
  3467. anything meaningful once it's converted to "test %reg,%reg";
  3468. additionally, some jumps will always (or never) branch, so
  3469. evaluate every jump immediately following the
  3470. comparison, optimising the conditions if possible.
  3471. Similarly with SETcc... those that are always set to 0 or 1
  3472. are changed to MOV instructions }
  3473. while GetNextInstruction(hp2, hp1) and
  3474. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) do
  3475. begin
  3476. case taicpu(hp1).condition of
  3477. C_B, C_C, C_NAE, C_O:
  3478. { For B/NAE:
  3479. Will never branch since an unsigned integer can never be below zero
  3480. For C/O:
  3481. Result cannot overflow because 0 is being subtracted
  3482. }
  3483. begin
  3484. if taicpu(hp1).opcode = A_Jcc then
  3485. begin
  3486. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (jump removed)', hp1);
  3487. TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
  3488. AsmL.Remove(hp1);
  3489. hp1.Free;
  3490. { Since hp1 was deleted, hp2 must not be updated }
  3491. Continue;
  3492. end
  3493. else
  3494. begin
  3495. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (set -> mov 0)', hp1);
  3496. { Convert "set(c) %reg" instruction to "movb 0,%reg" }
  3497. taicpu(hp1).opcode := A_MOV;
  3498. taicpu(hp1).condition := C_None;
  3499. taicpu(hp1).opsize := S_B;
  3500. taicpu(hp1).allocate_oper(2);
  3501. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  3502. taicpu(hp1).loadconst(0, 0);
  3503. end;
  3504. end;
  3505. C_BE, C_NA:
  3506. begin
  3507. { Will only branch if equal to zero }
  3508. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition BE/NA --> E', hp1);
  3509. taicpu(hp1).condition := C_E;
  3510. end;
  3511. C_A, C_NBE:
  3512. begin
  3513. { Will only branch if not equal to zero }
  3514. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition A/NBE --> NE', hp1);
  3515. taicpu(hp1).condition := C_NE;
  3516. end;
  3517. C_AE, C_NB, C_NC, C_NO:
  3518. begin
  3519. { Will always branch }
  3520. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition AE/NB/NC/NO --> Always', hp1);
  3521. if taicpu(hp1).opcode = A_Jcc then
  3522. begin
  3523. MakeUnconditional(taicpu(hp1));
  3524. { Any jumps/set that follow will now be dead code }
  3525. RemoveDeadCodeAfterJump(taicpu(hp1));
  3526. Break;
  3527. end
  3528. else
  3529. begin
  3530. { Convert "set(c) %reg" instruction to "movb 1,%reg" }
  3531. taicpu(hp1).opcode := A_MOV;
  3532. taicpu(hp1).condition := C_None;
  3533. taicpu(hp1).opsize := S_B;
  3534. taicpu(hp1).allocate_oper(2);
  3535. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  3536. taicpu(hp1).loadconst(0, 1);
  3537. end;
  3538. end;
  3539. C_None:
  3540. InternalError(2020012201);
  3541. C_P, C_PE, C_NP, C_PO:
  3542. { We can't handle parity checks and they should never be generated
  3543. after a general-purpose CMP (it's used in some floating-point
  3544. comparisons that don't use CMP) }
  3545. InternalError(2020012202);
  3546. else
  3547. { Zero/Equality, Sign, their complements and all of the
  3548. signed comparisons do not need to be converted };
  3549. end;
  3550. hp2 := hp1;
  3551. end;
  3552. { Convert the instruction to a TEST }
  3553. taicpu(p).opcode := A_TEST;
  3554. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  3555. Result := True;
  3556. Exit;
  3557. end
  3558. else if (taicpu(p).oper[0]^.val = 1) and
  3559. GetNextInstruction(p, hp1) and
  3560. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  3561. (taicpu(hp1).condition in [C_L, C_NGE]) then
  3562. begin
  3563. { Convert; To:
  3564. cmp $1,r/m cmp $0,r/m
  3565. jl @lbl jle @lbl
  3566. }
  3567. DebugMsg(SPeepholeOptimization + 'Cmp1Jl2Cmp0Jle', p);
  3568. taicpu(p).oper[0]^.val := 0;
  3569. taicpu(hp1).condition := C_LE;
  3570. { If the instruction is now "cmp $0,%reg", convert it to a
  3571. TEST (and effectively do the work of the "cmp $0,%reg" in
  3572. the block above)
  3573. If it's a reference, we can get away with not setting
  3574. Result to True because he haven't evaluated the jump
  3575. in this pass yet.
  3576. }
  3577. if (taicpu(p).oper[1]^.typ = top_reg) then
  3578. begin
  3579. taicpu(p).opcode := A_TEST;
  3580. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  3581. Result := True;
  3582. end;
  3583. Exit;
  3584. end
  3585. else if (taicpu(p).oper[1]^.typ = top_reg) then
  3586. begin
  3587. { cmp register,$8000 neg register
  3588. je target --> jo target
  3589. .... only if register is deallocated before jump.}
  3590. case Taicpu(p).opsize of
  3591. S_B: v:=$80;
  3592. S_W: v:=$8000;
  3593. S_L: v:=qword($80000000);
  3594. { S_Q will never happen: cmp with 64 bit constants is not possible }
  3595. S_Q:
  3596. Exit;
  3597. else
  3598. internalerror(2013112905);
  3599. end;
  3600. if (taicpu(p).oper[0]^.val=v) and
  3601. GetNextInstruction(p, hp1) and
  3602. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  3603. (Taicpu(hp1).condition in [C_E,C_NE]) then
  3604. begin
  3605. TransferUsedRegs(TmpUsedRegs);
  3606. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  3607. if not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, TmpUsedRegs)) then
  3608. begin
  3609. DebugMsg(SPeepholeOptimization + 'CmpJe2NegJo done',p);
  3610. Taicpu(p).opcode:=A_NEG;
  3611. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  3612. Taicpu(p).clearop(1);
  3613. Taicpu(p).ops:=1;
  3614. if Taicpu(hp1).condition=C_E then
  3615. Taicpu(hp1).condition:=C_O
  3616. else
  3617. Taicpu(hp1).condition:=C_NO;
  3618. Result:=true;
  3619. exit;
  3620. end;
  3621. end;
  3622. end;
  3623. end;
  3624. end;
  3625. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  3626. function IsXCHGAcceptable: Boolean; inline;
  3627. begin
  3628. { Always accept if optimising for size }
  3629. Result := (cs_opt_size in current_settings.optimizerswitches) or
  3630. (
  3631. {$ifdef x86_64}
  3632. { XCHG takes 3 cycles on AMD Athlon64 }
  3633. (current_settings.optimizecputype >= cpu_core_i)
  3634. {$else x86_64}
  3635. { From the Pentium M onwards, XCHG only has a latency of 2 rather
  3636. than 3, so it becomes a saving compared to three MOVs with two of
  3637. them able to execute simultaneously. [Kit] }
  3638. (current_settings.optimizecputype >= cpu_PentiumM)
  3639. {$endif x86_64}
  3640. );
  3641. end;
  3642. var
  3643. NewRef: TReference;
  3644. hp1,hp2,hp3: tai;
  3645. {$ifndef x86_64}
  3646. hp4: tai;
  3647. OperIdx: Integer;
  3648. {$endif x86_64}
  3649. begin
  3650. Result:=false;
  3651. if not GetNextInstruction(p, hp1) then
  3652. Exit;
  3653. if MatchInstruction(hp1, A_JMP, [S_NO]) then
  3654. begin
  3655. { Sometimes the MOVs that OptPass2JMP produces can be improved
  3656. further, but we can't just put this jump optimisation in pass 1
  3657. because it tends to perform worse when conditional jumps are
  3658. nearby (e.g. when converting CMOV instructions). [Kit] }
  3659. if OptPass2JMP(hp1) then
  3660. { call OptPass1MOV once to potentially merge any MOVs that were created }
  3661. Result := OptPass1MOV(p)
  3662. { OptPass2MOV will now exit but will be called again if OptPass1MOV
  3663. returned True and the instruction is still a MOV, thus checking
  3664. the optimisations below }
  3665. { If OptPass2JMP returned False, no optimisations were done to
  3666. the jump and there are no further optimisations that can be done
  3667. to the MOV instruction on this pass }
  3668. end
  3669. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3670. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  3671. MatchInstruction(hp1,A_ADD,A_SUB,[taicpu(p).opsize]) and
  3672. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3673. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) and
  3674. { be lazy, checking separately for sub would be slightly better }
  3675. (abs(taicpu(hp1).oper[0]^.val)<=$7fffffff) then
  3676. begin
  3677. { Change:
  3678. movl/q %reg1,%reg2 movl/q %reg1,%reg2
  3679. addl/q $x,%reg2 subl/q $x,%reg2
  3680. To:
  3681. leal/q x(%reg1),%reg2 leal/q -x(%reg1),%reg2
  3682. }
  3683. TransferUsedRegs(TmpUsedRegs);
  3684. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3685. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3686. if not GetNextInstruction(hp1, hp2) or
  3687. (
  3688. { The FLAGS register isn't always tracked properly, so do not
  3689. perform this optimisation if a conditional statement follows }
  3690. not RegReadByInstruction(NR_DEFAULTFLAGS, hp2) and
  3691. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)
  3692. ) then
  3693. begin
  3694. reference_reset(NewRef, 1, []);
  3695. NewRef.base := taicpu(p).oper[0]^.reg;
  3696. NewRef.scalefactor := 1;
  3697. if taicpu(hp1).opcode = A_ADD then
  3698. begin
  3699. DebugMsg(SPeepholeOptimization + 'MovAdd2Lea', p);
  3700. NewRef.offset := taicpu(hp1).oper[0]^.val;
  3701. end
  3702. else
  3703. begin
  3704. DebugMsg(SPeepholeOptimization + 'MovSub2Lea', p);
  3705. NewRef.offset := -taicpu(hp1).oper[0]^.val;
  3706. end;
  3707. taicpu(p).opcode := A_LEA;
  3708. taicpu(p).loadref(0, NewRef);
  3709. Asml.Remove(hp1);
  3710. hp1.Free;
  3711. Result := True;
  3712. Exit;
  3713. end;
  3714. end
  3715. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3716. {$ifdef x86_64}
  3717. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  3718. {$else x86_64}
  3719. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  3720. {$endif x86_64}
  3721. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3722. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  3723. { mov reg1, reg2 mov reg1, reg2
  3724. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  3725. begin
  3726. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  3727. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  3728. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  3729. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  3730. TransferUsedRegs(TmpUsedRegs);
  3731. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3732. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  3733. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  3734. then
  3735. begin
  3736. asml.remove(p);
  3737. p.free;
  3738. p := hp1;
  3739. Result:=true;
  3740. end;
  3741. exit;
  3742. end
  3743. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3744. IsXCHGAcceptable and
  3745. { XCHG doesn't support 8-byte registers }
  3746. (taicpu(p).opsize <> S_B) and
  3747. MatchInstruction(hp1, A_MOV, []) and
  3748. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3749. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  3750. GetNextInstruction(hp1, hp2) and
  3751. MatchInstruction(hp2, A_MOV, []) and
  3752. { Don't need to call MatchOpType for hp2 because the operand matches below cover for it }
  3753. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  3754. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) then
  3755. begin
  3756. { mov %reg1,%reg2
  3757. mov %reg3,%reg1 -> xchg %reg3,%reg1
  3758. mov %reg2,%reg3
  3759. (%reg2 not used afterwards)
  3760. Note that xchg takes 3 cycles to execute, and generally mov's take
  3761. only one cycle apiece, but the first two mov's can be executed in
  3762. parallel, only taking 2 cycles overall. Older processors should
  3763. therefore only optimise for size. [Kit]
  3764. }
  3765. TransferUsedRegs(TmpUsedRegs);
  3766. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3767. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3768. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs) then
  3769. begin
  3770. DebugMsg(SPeepholeOptimization + 'MovMovMov2XChg', p);
  3771. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp1, UsedRegs);
  3772. taicpu(hp1).opcode := A_XCHG;
  3773. asml.Remove(p);
  3774. asml.Remove(hp2);
  3775. p.Free;
  3776. hp2.Free;
  3777. p := hp1;
  3778. Result := True;
  3779. Exit;
  3780. end;
  3781. end
  3782. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3783. {$ifdef x86_64}
  3784. MatchInstruction(hp1,[A_MOV,A_MOVZX,A_MOVSX,A_MOVSXD],[]) and
  3785. {$else x86_64}
  3786. MatchInstruction(hp1,A_MOV,A_MOVZX,A_MOVSX,[]) and
  3787. {$endif x86_64}
  3788. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  3789. ((taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg)
  3790. or
  3791. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg)
  3792. ) and
  3793. (getsupreg(taicpu(hp1).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) then
  3794. { mov reg1, reg2
  3795. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  3796. begin
  3797. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) then
  3798. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[0]^.reg;
  3799. if (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) then
  3800. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  3801. DebugMsg(SPeepholeOptimization + 'MovMovXX2MoVXX 1 done',p);
  3802. asml.remove(p);
  3803. p.free;
  3804. p := hp1;
  3805. Result:=true;
  3806. exit;
  3807. end
  3808. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3809. MatchInstruction(hp1, A_SAR, []) then
  3810. begin
  3811. if MatchOperand(taicpu(hp1).oper[0]^, 31) then
  3812. begin
  3813. { the use of %edx also covers the opsize being S_L }
  3814. if MatchOperand(taicpu(hp1).oper[1]^, NR_EDX) then
  3815. begin
  3816. { Note it has to be specifically "movl %eax,%edx", and those specific sub-registers }
  3817. if (taicpu(p).oper[0]^.reg = NR_EAX) and
  3818. (taicpu(p).oper[1]^.reg = NR_EDX) then
  3819. begin
  3820. { Change:
  3821. movl %eax,%edx
  3822. sarl $31,%edx
  3823. To:
  3824. cltd
  3825. }
  3826. DebugMsg(SPeepholeOptimization + 'MovSar2Cltd', p);
  3827. Asml.Remove(hp1);
  3828. hp1.Free;
  3829. taicpu(p).opcode := A_CDQ;
  3830. taicpu(p).opsize := S_NO;
  3831. taicpu(p).clearop(1);
  3832. taicpu(p).clearop(0);
  3833. taicpu(p).ops:=0;
  3834. Result := True;
  3835. end
  3836. else if (cs_opt_size in current_settings.optimizerswitches) and
  3837. (taicpu(p).oper[0]^.reg = NR_EDX) and
  3838. (taicpu(p).oper[1]^.reg = NR_EAX) then
  3839. begin
  3840. { Change:
  3841. movl %edx,%eax
  3842. sarl $31,%edx
  3843. To:
  3844. movl %edx,%eax
  3845. cltd
  3846. Note that this creates a dependency between the two instructions,
  3847. so only perform if optimising for size.
  3848. }
  3849. DebugMsg(SPeepholeOptimization + 'MovSar2MovCltd', p);
  3850. taicpu(hp1).opcode := A_CDQ;
  3851. taicpu(hp1).opsize := S_NO;
  3852. taicpu(hp1).clearop(1);
  3853. taicpu(hp1).clearop(0);
  3854. taicpu(hp1).ops:=0;
  3855. end;
  3856. {$ifndef x86_64}
  3857. end
  3858. { Don't bother if CMOV is supported, because a more optimal
  3859. sequence would have been generated for the Abs() intrinsic }
  3860. else if not(CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and
  3861. { the use of %eax also covers the opsize being S_L }
  3862. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) and
  3863. (taicpu(p).oper[0]^.reg = NR_EAX) and
  3864. (taicpu(p).oper[1]^.reg = NR_EDX) and
  3865. GetNextInstruction(hp1, hp2) and
  3866. MatchInstruction(hp2, A_XOR, [S_L]) and
  3867. MatchOperand(taicpu(hp2).oper[0]^, NR_EAX) and
  3868. MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) and
  3869. GetNextInstruction(hp2, hp3) and
  3870. MatchInstruction(hp3, A_SUB, [S_L]) and
  3871. MatchOperand(taicpu(hp3).oper[0]^, NR_EAX) and
  3872. MatchOperand(taicpu(hp3).oper[1]^, NR_EDX) then
  3873. begin
  3874. { Change:
  3875. movl %eax,%edx
  3876. sarl $31,%eax
  3877. xorl %eax,%edx
  3878. subl %eax,%edx
  3879. (Instruction that uses %edx)
  3880. (%eax deallocated)
  3881. (%edx deallocated)
  3882. To:
  3883. cltd
  3884. xorl %edx,%eax <-- Note the registers have swapped
  3885. subl %edx,%eax
  3886. (Instruction that uses %eax) <-- %eax rather than %edx
  3887. }
  3888. TransferUsedRegs(TmpUsedRegs);
  3889. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3890. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3891. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  3892. if not RegUsedAfterInstruction(NR_EAX, hp3, TmpUsedRegs) then
  3893. begin
  3894. if GetNextInstruction(hp3, hp4) and
  3895. not RegModifiedByInstruction(NR_EDX, hp4) and
  3896. not RegUsedAfterInstruction(NR_EDX, hp4, TmpUsedRegs) then
  3897. begin
  3898. DebugMsg(SPeepholeOptimization + 'abs() intrinsic optimisation', p);
  3899. taicpu(p).opcode := A_CDQ;
  3900. taicpu(p).clearop(1);
  3901. taicpu(p).clearop(0);
  3902. taicpu(p).ops:=0;
  3903. AsmL.Remove(hp1);
  3904. hp1.Free;
  3905. taicpu(hp2).loadreg(0, NR_EDX);
  3906. taicpu(hp2).loadreg(1, NR_EAX);
  3907. taicpu(hp3).loadreg(0, NR_EDX);
  3908. taicpu(hp3).loadreg(1, NR_EAX);
  3909. AllocRegBetween(NR_EAX, hp3, hp4, TmpUsedRegs);
  3910. { Convert references in the following instruction (hp4) from %edx to %eax }
  3911. for OperIdx := 0 to taicpu(hp4).ops - 1 do
  3912. with taicpu(hp4).oper[OperIdx]^ do
  3913. case typ of
  3914. top_reg:
  3915. if reg = NR_EDX then
  3916. reg := NR_EAX;
  3917. top_ref:
  3918. begin
  3919. if ref^.base = NR_EDX then
  3920. ref^.base := NR_EAX;
  3921. if ref^.index = NR_EDX then
  3922. ref^.index := NR_EAX;
  3923. end;
  3924. else
  3925. ;
  3926. end;
  3927. end;
  3928. end;
  3929. {$else x86_64}
  3930. end;
  3931. end
  3932. else if MatchOperand(taicpu(hp1).oper[0]^, 63) and
  3933. { the use of %rdx also covers the opsize being S_Q }
  3934. MatchOperand(taicpu(hp1).oper[1]^, NR_RDX) then
  3935. begin
  3936. { Note it has to be specifically "movq %rax,%rdx", and those specific sub-registers }
  3937. if (taicpu(p).oper[0]^.reg = NR_RAX) and
  3938. (taicpu(p).oper[1]^.reg = NR_RDX) then
  3939. begin
  3940. { Change:
  3941. movq %rax,%rdx
  3942. sarq $63,%rdx
  3943. To:
  3944. cqto
  3945. }
  3946. DebugMsg(SPeepholeOptimization + 'MovSar2Cqto', p);
  3947. Asml.Remove(hp1);
  3948. hp1.Free;
  3949. taicpu(p).opcode := A_CQO;
  3950. taicpu(p).opsize := S_NO;
  3951. taicpu(p).clearop(1);
  3952. taicpu(p).clearop(0);
  3953. taicpu(p).ops:=0;
  3954. Result := True;
  3955. end
  3956. else if (cs_opt_size in current_settings.optimizerswitches) and
  3957. (taicpu(p).oper[0]^.reg = NR_RDX) and
  3958. (taicpu(p).oper[1]^.reg = NR_RAX) then
  3959. begin
  3960. { Change:
  3961. movq %rdx,%rax
  3962. sarq $63,%rdx
  3963. To:
  3964. movq %rdx,%rax
  3965. cqto
  3966. Note that this creates a dependency between the two instructions,
  3967. so only perform if optimising for size.
  3968. }
  3969. DebugMsg(SPeepholeOptimization + 'MovSar2MovCqto', p);
  3970. taicpu(hp1).opcode := A_CQO;
  3971. taicpu(hp1).opsize := S_NO;
  3972. taicpu(hp1).clearop(1);
  3973. taicpu(hp1).clearop(0);
  3974. taicpu(hp1).ops:=0;
  3975. {$endif x86_64}
  3976. end;
  3977. end;
  3978. end
  3979. else if MatchInstruction(hp1, A_MOV, []) and
  3980. (taicpu(hp1).oper[1]^.typ = top_reg) then
  3981. { Though "GetNextInstruction" could be factored out, along with
  3982. the instructions that depend on hp2, it is an expensive call that
  3983. should be delayed for as long as possible, hence we do cheaper
  3984. checks first that are likely to be False. [Kit] }
  3985. begin
  3986. if MatchOperand(taicpu(p).oper[1]^, NR_EDX) and
  3987. (
  3988. (
  3989. (taicpu(hp1).oper[1]^.reg = NR_EAX) and
  3990. (
  3991. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  3992. MatchOperand(taicpu(hp1).oper[0]^, NR_EDX)
  3993. )
  3994. ) or
  3995. (
  3996. (taicpu(hp1).oper[1]^.reg = NR_EDX) and
  3997. (
  3998. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  3999. MatchOperand(taicpu(hp1).oper[0]^, NR_EAX)
  4000. )
  4001. )
  4002. ) and
  4003. GetNextInstruction(hp1, hp2) and
  4004. MatchInstruction(hp2, A_SAR, []) and
  4005. MatchOperand(taicpu(hp2).oper[0]^, 31) then
  4006. begin
  4007. if MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) then
  4008. begin
  4009. { Change:
  4010. movl r/m,%edx movl r/m,%eax movl r/m,%edx movl r/m,%eax
  4011. movl %edx,%eax or movl %eax,%edx or movl r/m,%eax or movl r/m,%edx
  4012. sarl $31,%edx sarl $31,%edx sarl $31,%edx sarl $31,%edx
  4013. To:
  4014. movl r/m,%eax <- Note the change in register
  4015. cltd
  4016. }
  4017. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCltd', p);
  4018. AllocRegBetween(NR_EAX, p, hp1, UsedRegs);
  4019. taicpu(p).loadreg(1, NR_EAX);
  4020. taicpu(hp1).opcode := A_CDQ;
  4021. taicpu(hp1).clearop(1);
  4022. taicpu(hp1).clearop(0);
  4023. taicpu(hp1).ops:=0;
  4024. AsmL.Remove(hp2);
  4025. hp2.Free;
  4026. (*
  4027. {$ifdef x86_64}
  4028. end
  4029. else if MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) and
  4030. { This code sequence does not get generated - however it might become useful
  4031. if and when 128-bit signed integer types make an appearance, so the code
  4032. is kept here for when it is eventually needed. [Kit] }
  4033. (
  4034. (
  4035. (taicpu(hp1).oper[1]^.reg = NR_RAX) and
  4036. (
  4037. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4038. MatchOperand(taicpu(hp1).oper[0]^, NR_RDX)
  4039. )
  4040. ) or
  4041. (
  4042. (taicpu(hp1).oper[1]^.reg = NR_RDX) and
  4043. (
  4044. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4045. MatchOperand(taicpu(hp1).oper[0]^, NR_RAX)
  4046. )
  4047. )
  4048. ) and
  4049. GetNextInstruction(hp1, hp2) and
  4050. MatchInstruction(hp2, A_SAR, [S_Q]) and
  4051. MatchOperand(taicpu(hp2).oper[0]^, 63) and
  4052. MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) then
  4053. begin
  4054. { Change:
  4055. movq r/m,%rdx movq r/m,%rax movq r/m,%rdx movq r/m,%rax
  4056. movq %rdx,%rax or movq %rax,%rdx or movq r/m,%rax or movq r/m,%rdx
  4057. sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx
  4058. To:
  4059. movq r/m,%rax <- Note the change in register
  4060. cqto
  4061. }
  4062. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCqto', p);
  4063. AllocRegBetween(NR_RAX, p, hp1, UsedRegs);
  4064. taicpu(p).loadreg(1, NR_RAX);
  4065. taicpu(hp1).opcode := A_CQO;
  4066. taicpu(hp1).clearop(1);
  4067. taicpu(hp1).clearop(0);
  4068. taicpu(hp1).ops:=0;
  4069. AsmL.Remove(hp2);
  4070. hp2.Free;
  4071. {$endif x86_64}
  4072. *)
  4073. end;
  4074. end;
  4075. end
  4076. else if (taicpu(p).oper[0]^.typ = top_ref) and
  4077. (hp1.typ = ait_instruction) and
  4078. { while the GetNextInstruction(hp1,hp2) call could be factored out,
  4079. doing it separately in both branches allows to do the cheap checks
  4080. with low probability earlier }
  4081. ((IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  4082. GetNextInstruction(hp1,hp2) and
  4083. MatchInstruction(hp2,A_MOV,[])
  4084. ) or
  4085. ((taicpu(hp1).opcode=A_LEA) and
  4086. GetNextInstruction(hp1,hp2) and
  4087. MatchInstruction(hp2,A_MOV,[]) and
  4088. ((MatchReference(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  4089. (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg)
  4090. ) or
  4091. (MatchReference(taicpu(hp1).oper[0]^.ref^,NR_INVALID,
  4092. taicpu(p).oper[1]^.reg) and
  4093. (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg)) or
  4094. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_NO)) or
  4095. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,NR_NO,taicpu(p).oper[1]^.reg))
  4096. ) and
  4097. ((MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^)) or not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)))
  4098. )
  4099. ) and
  4100. MatchOperand(taicpu(hp1).oper[taicpu(hp1).ops-1]^,taicpu(hp2).oper[0]^) and
  4101. (taicpu(hp2).oper[1]^.typ = top_ref) then
  4102. begin
  4103. TransferUsedRegs(TmpUsedRegs);
  4104. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  4105. UpdateUsedRegs(TmpUsedRegs,tai(hp1.next));
  4106. if (RefsEqual(taicpu(hp2).oper[1]^.ref^,taicpu(p).oper[0]^.ref^) and
  4107. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,TmpUsedRegs))) then
  4108. { change mov (ref), reg
  4109. add/sub/or/... reg2/$const, reg
  4110. mov reg, (ref)
  4111. # release reg
  4112. to add/sub/or/... reg2/$const, (ref) }
  4113. begin
  4114. case taicpu(hp1).opcode of
  4115. A_INC,A_DEC,A_NOT,A_NEG :
  4116. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  4117. A_LEA :
  4118. begin
  4119. taicpu(hp1).opcode:=A_ADD;
  4120. if (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.index<>NR_NO) then
  4121. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.index)
  4122. else if (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.base<>NR_NO) then
  4123. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.base)
  4124. else
  4125. taicpu(hp1).loadconst(0,taicpu(hp1).oper[0]^.ref^.offset);
  4126. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  4127. DebugMsg(SPeepholeOptimization + 'FoldLea done',hp1);
  4128. end
  4129. else
  4130. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  4131. end;
  4132. asml.remove(p);
  4133. asml.remove(hp2);
  4134. p.free;
  4135. hp2.free;
  4136. p := hp1
  4137. end;
  4138. Exit;
  4139. {$ifdef x86_64}
  4140. end
  4141. else if (taicpu(p).opsize = S_L) and
  4142. (taicpu(p).oper[1]^.typ = top_reg) and
  4143. (
  4144. MatchInstruction(hp1, A_MOV,[]) and
  4145. (taicpu(hp1).opsize = S_L) and
  4146. (taicpu(hp1).oper[1]^.typ = top_reg)
  4147. ) and (
  4148. GetNextInstruction(hp1, hp2) and
  4149. (tai(hp2).typ=ait_instruction) and
  4150. (taicpu(hp2).opsize = S_Q) and
  4151. (
  4152. (
  4153. MatchInstruction(hp2, A_ADD,[]) and
  4154. (taicpu(hp2).opsize = S_Q) and
  4155. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4156. (
  4157. (
  4158. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4159. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4160. ) or (
  4161. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4162. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4163. )
  4164. )
  4165. ) or (
  4166. MatchInstruction(hp2, A_LEA,[]) and
  4167. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  4168. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  4169. (
  4170. (
  4171. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4172. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4173. ) or (
  4174. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4175. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  4176. )
  4177. ) and (
  4178. (
  4179. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4180. ) or (
  4181. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4182. )
  4183. )
  4184. )
  4185. )
  4186. ) and (
  4187. GetNextInstruction(hp2, hp3) and
  4188. MatchInstruction(hp3, A_SHR,[]) and
  4189. (taicpu(hp3).opsize = S_Q) and
  4190. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4191. (taicpu(hp3).oper[0]^.val = 1) and
  4192. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  4193. ) then
  4194. begin
  4195. { Change movl x, reg1d movl x, reg1d
  4196. movl y, reg2d movl y, reg2d
  4197. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  4198. shrq $1, reg1q shrq $1, reg1q
  4199. ( reg1d and reg2d can be switched around in the first two instructions )
  4200. To movl x, reg1d
  4201. addl y, reg1d
  4202. rcrl $1, reg1d
  4203. This corresponds to the common expression (x + y) shr 1, where
  4204. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  4205. smaller code, but won't account for x + y causing an overflow). [Kit]
  4206. }
  4207. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  4208. { Change first MOV command to have the same register as the final output }
  4209. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  4210. else
  4211. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  4212. { Change second MOV command to an ADD command. This is easier than
  4213. converting the existing command because it means we don't have to
  4214. touch 'y', which might be a complicated reference, and also the
  4215. fact that the third command might either be ADD or LEA. [Kit] }
  4216. taicpu(hp1).opcode := A_ADD;
  4217. { Delete old ADD/LEA instruction }
  4218. asml.remove(hp2);
  4219. hp2.free;
  4220. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  4221. taicpu(hp3).opcode := A_RCR;
  4222. taicpu(hp3).changeopsize(S_L);
  4223. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  4224. {$endif x86_64}
  4225. end;
  4226. end;
  4227. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  4228. var
  4229. hp1 : tai;
  4230. begin
  4231. Result:=false;
  4232. if (taicpu(p).ops >= 2) and
  4233. ((taicpu(p).oper[0]^.typ = top_const) or
  4234. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  4235. (taicpu(p).oper[1]^.typ = top_reg) and
  4236. ((taicpu(p).ops = 2) or
  4237. ((taicpu(p).oper[2]^.typ = top_reg) and
  4238. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  4239. GetLastInstruction(p,hp1) and
  4240. MatchInstruction(hp1,A_MOV,[]) and
  4241. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  4242. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4243. begin
  4244. TransferUsedRegs(TmpUsedRegs);
  4245. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) or
  4246. ((taicpu(p).ops = 3) and (taicpu(p).oper[1]^.reg=taicpu(p).oper[2]^.reg)) then
  4247. { change
  4248. mov reg1,reg2
  4249. imul y,reg2 to imul y,reg1,reg2 }
  4250. begin
  4251. taicpu(p).ops := 3;
  4252. taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
  4253. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  4254. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  4255. asml.remove(hp1);
  4256. hp1.free;
  4257. result:=true;
  4258. end;
  4259. end;
  4260. end;
  4261. procedure TX86AsmOptimizer.ConvertJumpToRET(const p: tai; const ret_p: tai);
  4262. var
  4263. ThisLabel: TAsmLabel;
  4264. begin
  4265. ThisLabel := tasmlabel(taicpu(p).oper[0]^.ref^.symbol);
  4266. ThisLabel.decrefs;
  4267. taicpu(p).opcode := A_RET;
  4268. taicpu(p).is_jmp := false;
  4269. taicpu(p).ops := taicpu(ret_p).ops;
  4270. case taicpu(ret_p).ops of
  4271. 0:
  4272. taicpu(p).clearop(0);
  4273. 1:
  4274. taicpu(p).loadconst(0,taicpu(ret_p).oper[0]^.val);
  4275. else
  4276. internalerror(2016041301);
  4277. end;
  4278. { If the original label is now dead, it might turn out that the label
  4279. immediately follows p. As a result, everything beyond it, which will
  4280. be just some final register configuration and a RET instruction, is
  4281. now dead code. [Kit] }
  4282. { NOTE: This is much faster than introducing a OptPass2RET routine and
  4283. running RemoveDeadCodeAfterJump for each RET instruction, because
  4284. this optimisation rarely happens and most RETs appear at the end of
  4285. routines where there is nothing that can be stripped. [Kit] }
  4286. if not ThisLabel.is_used then
  4287. RemoveDeadCodeAfterJump(p);
  4288. end;
  4289. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  4290. var
  4291. hp1, hp2, hp3: tai;
  4292. OperIdx: Integer;
  4293. begin
  4294. result:=false;
  4295. 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
  4296. (taicpu(p).oper[0]^.ref^.index=NR_NO) and (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) then
  4297. begin
  4298. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  4299. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and (hp1.typ = ait_instruction) then
  4300. begin
  4301. case taicpu(hp1).opcode of
  4302. A_RET:
  4303. {
  4304. change
  4305. jmp .L1
  4306. ...
  4307. .L1:
  4308. ret
  4309. into
  4310. ret
  4311. }
  4312. begin
  4313. ConvertJumpToRET(p, hp1);
  4314. result:=true;
  4315. end;
  4316. A_MOV:
  4317. {
  4318. change
  4319. jmp .L1
  4320. ...
  4321. .L1:
  4322. mov ##, ##
  4323. ret
  4324. into
  4325. mov ##, ##
  4326. ret
  4327. }
  4328. { This optimisation tends to increase code size if the pass 1 MOV optimisations aren't
  4329. re-run, so only do this particular optimisation if optimising for speed or when
  4330. optimisations are very in-depth. [Kit] }
  4331. if (current_settings.optimizerswitches * [cs_opt_level3, cs_opt_size]) <> [cs_opt_size] then
  4332. begin
  4333. GetNextInstruction(hp1, hp2);
  4334. if not Assigned(hp2) then
  4335. Exit;
  4336. if (hp2.typ in [ait_label, ait_align]) then
  4337. SkipLabels(hp2,hp2);
  4338. if Assigned(hp2) and MatchInstruction(hp2, A_RET, [S_NO]) then
  4339. begin
  4340. { Duplicate the MOV instruction }
  4341. hp3:=tai(hp1.getcopy);
  4342. asml.InsertBefore(hp3, p);
  4343. { Make sure the compiler knows about any final registers written here }
  4344. for OperIdx := 0 to 1 do
  4345. with taicpu(hp3).oper[OperIdx]^ do
  4346. begin
  4347. case typ of
  4348. top_ref:
  4349. begin
  4350. if (ref^.base <> NR_NO) {$ifdef x86_64} and (ref^.base <> NR_RIP) {$endif x86_64} then
  4351. AllocRegBetween(ref^.base, hp3, tai(p.Next), UsedRegs);
  4352. if (ref^.index <> NR_NO) {$ifdef x86_64} and (ref^.index <> NR_RIP) {$endif x86_64} then
  4353. AllocRegBetween(ref^.index, hp3, tai(p.Next), UsedRegs);
  4354. end;
  4355. top_reg:
  4356. AllocRegBetween(reg, hp3, tai(p.Next), UsedRegs);
  4357. else
  4358. ;
  4359. end;
  4360. end;
  4361. { Now change the jump into a RET instruction }
  4362. ConvertJumpToRET(p, hp2);
  4363. result:=true;
  4364. end;
  4365. end;
  4366. else
  4367. ;
  4368. end;
  4369. end;
  4370. end;
  4371. end;
  4372. class function TX86AsmOptimizer.CanBeCMOV(p : tai) : boolean;
  4373. begin
  4374. CanBeCMOV:=assigned(p) and
  4375. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  4376. { we can't use cmov ref,reg because
  4377. ref could be nil and cmov still throws an exception
  4378. if ref=nil but the mov isn't done (FK)
  4379. or ((taicpu(p).oper[0]^.typ = top_ref) and
  4380. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  4381. }
  4382. (taicpu(p).oper[1]^.typ = top_reg) and
  4383. (
  4384. (taicpu(p).oper[0]^.typ = top_reg) or
  4385. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  4386. it is not expected that this can cause a seg. violation }
  4387. (
  4388. (taicpu(p).oper[0]^.typ = top_ref) and
  4389. IsRefSafe(taicpu(p).oper[0]^.ref)
  4390. )
  4391. );
  4392. end;
  4393. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  4394. var
  4395. hp1,hp2,hp3,hp4,hpmov2: tai;
  4396. carryadd_opcode : TAsmOp;
  4397. l : Longint;
  4398. condition : TAsmCond;
  4399. symbol: TAsmSymbol;
  4400. begin
  4401. result:=false;
  4402. symbol:=nil;
  4403. if GetNextInstruction(p,hp1) then
  4404. begin
  4405. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  4406. if (hp1.typ=ait_instruction) and
  4407. GetNextInstruction(hp1,hp2) and (hp2.typ=ait_label) and
  4408. (Tasmlabel(symbol) = Tai_label(hp2).labsym) then
  4409. { jb @@1 cmc
  4410. inc/dec operand --> adc/sbb operand,0
  4411. @@1:
  4412. ... and ...
  4413. jnb @@1
  4414. inc/dec operand --> adc/sbb operand,0
  4415. @@1: }
  4416. begin
  4417. carryadd_opcode:=A_NONE;
  4418. if Taicpu(p).condition in [C_NAE,C_B] then
  4419. begin
  4420. if Taicpu(hp1).opcode=A_INC then
  4421. carryadd_opcode:=A_ADC;
  4422. if Taicpu(hp1).opcode=A_DEC then
  4423. carryadd_opcode:=A_SBB;
  4424. if carryadd_opcode<>A_NONE then
  4425. begin
  4426. Taicpu(p).clearop(0);
  4427. Taicpu(p).ops:=0;
  4428. Taicpu(p).is_jmp:=false;
  4429. Taicpu(p).opcode:=A_CMC;
  4430. Taicpu(p).condition:=C_NONE;
  4431. Taicpu(hp1).ops:=2;
  4432. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  4433. Taicpu(hp1).loadconst(0,0);
  4434. Taicpu(hp1).opcode:=carryadd_opcode;
  4435. result:=true;
  4436. exit;
  4437. end;
  4438. end;
  4439. if Taicpu(p).condition in [C_AE,C_NB] then
  4440. begin
  4441. if Taicpu(hp1).opcode=A_INC then
  4442. carryadd_opcode:=A_ADC;
  4443. if Taicpu(hp1).opcode=A_DEC then
  4444. carryadd_opcode:=A_SBB;
  4445. if carryadd_opcode<>A_NONE then
  4446. begin
  4447. asml.remove(p);
  4448. p.free;
  4449. Taicpu(hp1).ops:=2;
  4450. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  4451. Taicpu(hp1).loadconst(0,0);
  4452. Taicpu(hp1).opcode:=carryadd_opcode;
  4453. p:=hp1;
  4454. result:=true;
  4455. exit;
  4456. end;
  4457. end;
  4458. end;
  4459. { Detect the following:
  4460. jmp<cond> @Lbl1
  4461. jmp @Lbl2
  4462. ...
  4463. @Lbl1:
  4464. ret
  4465. Change to:
  4466. jmp<inv_cond> @Lbl2
  4467. ret
  4468. }
  4469. if MatchInstruction(hp1,A_JMP,[]) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  4470. begin
  4471. hp2:=getlabelwithsym(TAsmLabel(symbol));
  4472. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  4473. MatchInstruction(hp2,A_RET,[S_NO]) then
  4474. begin
  4475. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  4476. { Change label address to that of the unconditional jump }
  4477. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  4478. TAsmLabel(symbol).DecRefs;
  4479. taicpu(hp1).opcode := A_RET;
  4480. taicpu(hp1).is_jmp := false;
  4481. taicpu(hp1).ops := taicpu(hp2).ops;
  4482. DebugMsg(SPeepholeOptimization+'JccJmpRet2J!ccRet',p);
  4483. case taicpu(hp2).ops of
  4484. 0:
  4485. taicpu(hp1).clearop(0);
  4486. 1:
  4487. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  4488. else
  4489. internalerror(2016041302);
  4490. end;
  4491. end;
  4492. end;
  4493. end;
  4494. {$ifndef i8086}
  4495. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  4496. begin
  4497. { check for
  4498. jCC xxx
  4499. <several movs>
  4500. xxx:
  4501. }
  4502. l:=0;
  4503. GetNextInstruction(p, hp1);
  4504. while assigned(hp1) and
  4505. CanBeCMOV(hp1) and
  4506. { stop on labels }
  4507. not(hp1.typ=ait_label) do
  4508. begin
  4509. inc(l);
  4510. GetNextInstruction(hp1,hp1);
  4511. end;
  4512. if assigned(hp1) then
  4513. begin
  4514. if FindLabel(tasmlabel(symbol),hp1) then
  4515. begin
  4516. if (l<=4) and (l>0) then
  4517. begin
  4518. condition:=inverse_cond(taicpu(p).condition);
  4519. GetNextInstruction(p,hp1);
  4520. repeat
  4521. if not Assigned(hp1) then
  4522. InternalError(2018062900);
  4523. taicpu(hp1).opcode:=A_CMOVcc;
  4524. taicpu(hp1).condition:=condition;
  4525. UpdateUsedRegs(hp1);
  4526. GetNextInstruction(hp1,hp1);
  4527. until not(CanBeCMOV(hp1));
  4528. { Remember what hp1 is in case there's multiple aligns to get rid of }
  4529. hp2 := hp1;
  4530. repeat
  4531. if not Assigned(hp2) then
  4532. InternalError(2018062910);
  4533. case hp2.typ of
  4534. ait_label:
  4535. { What we expected - break out of the loop (it won't be a dead label at the top of
  4536. a cluster because that was optimised at an earlier stage) }
  4537. Break;
  4538. ait_align:
  4539. { Go to the next entry until a label is found (may be multiple aligns before it) }
  4540. begin
  4541. hp2 := tai(hp2.Next);
  4542. Continue;
  4543. end;
  4544. else
  4545. begin
  4546. { Might be a comment or temporary allocation entry }
  4547. if not (hp2.typ in SkipInstr) then
  4548. InternalError(2018062911);
  4549. hp2 := tai(hp2.Next);
  4550. Continue;
  4551. end;
  4552. end;
  4553. until False;
  4554. { Now we can safely decrement the reference count }
  4555. tasmlabel(symbol).decrefs;
  4556. DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
  4557. { Remove the original jump }
  4558. asml.Remove(p);
  4559. p.Free;
  4560. GetNextInstruction(hp2, p); { Instruction after the label }
  4561. { Remove the label if this is its final reference }
  4562. if (tasmlabel(symbol).getrefs=0) then
  4563. StripLabelFast(hp1);
  4564. if Assigned(p) then
  4565. begin
  4566. UpdateUsedRegs(p);
  4567. result:=true;
  4568. end;
  4569. exit;
  4570. end;
  4571. end
  4572. else
  4573. begin
  4574. { check further for
  4575. jCC xxx
  4576. <several movs 1>
  4577. jmp yyy
  4578. xxx:
  4579. <several movs 2>
  4580. yyy:
  4581. }
  4582. { hp2 points to jmp yyy }
  4583. hp2:=hp1;
  4584. { skip hp1 to xxx (or an align right before it) }
  4585. GetNextInstruction(hp1, hp1);
  4586. if assigned(hp2) and
  4587. assigned(hp1) and
  4588. (l<=3) and
  4589. (hp2.typ=ait_instruction) and
  4590. (taicpu(hp2).is_jmp) and
  4591. (taicpu(hp2).condition=C_None) and
  4592. { real label and jump, no further references to the
  4593. label are allowed }
  4594. (tasmlabel(symbol).getrefs=1) and
  4595. FindLabel(tasmlabel(symbol),hp1) then
  4596. begin
  4597. l:=0;
  4598. { skip hp1 to <several moves 2> }
  4599. if (hp1.typ = ait_align) then
  4600. GetNextInstruction(hp1, hp1);
  4601. GetNextInstruction(hp1, hpmov2);
  4602. hp1 := hpmov2;
  4603. while assigned(hp1) and
  4604. CanBeCMOV(hp1) do
  4605. begin
  4606. inc(l);
  4607. GetNextInstruction(hp1, hp1);
  4608. end;
  4609. { hp1 points to yyy (or an align right before it) }
  4610. hp3 := hp1;
  4611. if assigned(hp1) and
  4612. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  4613. begin
  4614. condition:=inverse_cond(taicpu(p).condition);
  4615. GetNextInstruction(p,hp1);
  4616. repeat
  4617. taicpu(hp1).opcode:=A_CMOVcc;
  4618. taicpu(hp1).condition:=condition;
  4619. UpdateUsedRegs(hp1);
  4620. GetNextInstruction(hp1,hp1);
  4621. until not(assigned(hp1)) or
  4622. not(CanBeCMOV(hp1));
  4623. condition:=inverse_cond(condition);
  4624. hp1 := hpmov2;
  4625. { hp1 is now at <several movs 2> }
  4626. while Assigned(hp1) and CanBeCMOV(hp1) do
  4627. begin
  4628. taicpu(hp1).opcode:=A_CMOVcc;
  4629. taicpu(hp1).condition:=condition;
  4630. UpdateUsedRegs(hp1);
  4631. GetNextInstruction(hp1,hp1);
  4632. end;
  4633. hp1 := p;
  4634. { Get first instruction after label }
  4635. GetNextInstruction(hp3, p);
  4636. if assigned(p) and (hp3.typ = ait_align) then
  4637. GetNextInstruction(p, p);
  4638. { Don't dereference yet, as doing so will cause
  4639. GetNextInstruction to skip the label and
  4640. optional align marker. [Kit] }
  4641. GetNextInstruction(hp2, hp4);
  4642. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
  4643. { remove jCC }
  4644. asml.remove(hp1);
  4645. hp1.free;
  4646. { Now we can safely decrement it }
  4647. tasmlabel(symbol).decrefs;
  4648. { Remove label xxx (it will have a ref of zero due to the initial check }
  4649. StripLabelFast(hp4);
  4650. { remove jmp }
  4651. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  4652. asml.remove(hp2);
  4653. hp2.free;
  4654. { As before, now we can safely decrement it }
  4655. tasmlabel(symbol).decrefs;
  4656. { Remove label yyy (and the optional alignment) if its reference falls to zero }
  4657. if tasmlabel(symbol).getrefs = 0 then
  4658. StripLabelFast(hp3);
  4659. if Assigned(p) then
  4660. begin
  4661. UpdateUsedRegs(p);
  4662. result:=true;
  4663. end;
  4664. exit;
  4665. end;
  4666. end;
  4667. end;
  4668. end;
  4669. end;
  4670. {$endif i8086}
  4671. end;
  4672. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  4673. var
  4674. hp1,hp2: tai;
  4675. reg_and_hp1_is_instr: Boolean;
  4676. begin
  4677. result:=false;
  4678. reg_and_hp1_is_instr:=(taicpu(p).oper[1]^.typ = top_reg) and
  4679. GetNextInstruction(p,hp1) and
  4680. (hp1.typ = ait_instruction);
  4681. if reg_and_hp1_is_instr and
  4682. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  4683. GetNextInstruction(hp1,hp2) and
  4684. MatchInstruction(hp2,A_MOV,[]) and
  4685. (taicpu(hp2).oper[0]^.typ = top_reg) and
  4686. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  4687. {$ifdef i386}
  4688. { not all registers have byte size sub registers on i386 }
  4689. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  4690. {$endif i386}
  4691. (((taicpu(hp1).ops=2) and
  4692. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  4693. ((taicpu(hp1).ops=1) and
  4694. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  4695. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  4696. begin
  4697. { change movsX/movzX reg/ref, reg2
  4698. add/sub/or/... reg3/$const, reg2
  4699. mov reg2 reg/ref
  4700. to add/sub/or/... reg3/$const, reg/ref }
  4701. { by example:
  4702. movswl %si,%eax movswl %si,%eax p
  4703. decl %eax addl %edx,%eax hp1
  4704. movw %ax,%si movw %ax,%si hp2
  4705. ->
  4706. movswl %si,%eax movswl %si,%eax p
  4707. decw %eax addw %edx,%eax hp1
  4708. movw %ax,%si movw %ax,%si hp2
  4709. }
  4710. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  4711. {
  4712. ->
  4713. movswl %si,%eax movswl %si,%eax p
  4714. decw %si addw %dx,%si hp1
  4715. movw %ax,%si movw %ax,%si hp2
  4716. }
  4717. case taicpu(hp1).ops of
  4718. 1:
  4719. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  4720. 2:
  4721. begin
  4722. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  4723. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  4724. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  4725. end;
  4726. else
  4727. internalerror(2008042701);
  4728. end;
  4729. {
  4730. ->
  4731. decw %si addw %dx,%si p
  4732. }
  4733. DebugMsg(SPeepholeOptimization + 'var3',p);
  4734. asml.remove(p);
  4735. asml.remove(hp2);
  4736. p.free;
  4737. hp2.free;
  4738. p:=hp1;
  4739. end
  4740. else if taicpu(p).opcode=A_MOVZX then
  4741. begin
  4742. { removes superfluous And's after movzx's }
  4743. if reg_and_hp1_is_instr and
  4744. (taicpu(hp1).opcode = A_AND) and
  4745. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4746. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4747. begin
  4748. case taicpu(p).opsize Of
  4749. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  4750. if (taicpu(hp1).oper[0]^.val = $ff) then
  4751. begin
  4752. DebugMsg(SPeepholeOptimization + 'var4',p);
  4753. asml.remove(hp1);
  4754. hp1.free;
  4755. end;
  4756. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  4757. if (taicpu(hp1).oper[0]^.val = $ffff) then
  4758. begin
  4759. DebugMsg(SPeepholeOptimization + 'var5',p);
  4760. asml.remove(hp1);
  4761. hp1.free;
  4762. end;
  4763. {$ifdef x86_64}
  4764. S_LQ:
  4765. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  4766. begin
  4767. if (cs_asm_source in current_settings.globalswitches) then
  4768. asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
  4769. asml.remove(hp1);
  4770. hp1.Free;
  4771. end;
  4772. {$endif x86_64}
  4773. else
  4774. ;
  4775. end;
  4776. end;
  4777. { changes some movzx constructs to faster synonyms (all examples
  4778. are given with eax/ax, but are also valid for other registers)}
  4779. if MatchOpType(taicpu(p),top_reg,top_reg) then
  4780. begin
  4781. case taicpu(p).opsize of
  4782. { Technically, movzbw %al,%ax cannot be encoded in 32/64-bit mode
  4783. (the machine code is equivalent to movzbl %al,%eax), but the
  4784. code generator still generates that assembler instruction and
  4785. it is silently converted. This should probably be checked.
  4786. [Kit] }
  4787. S_BW:
  4788. begin
  4789. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  4790. (
  4791. not IsMOVZXAcceptable
  4792. { and $0xff,%ax has a smaller encoding but risks a partial write penalty }
  4793. or (
  4794. (cs_opt_size in current_settings.optimizerswitches) and
  4795. (taicpu(p).oper[1]^.reg = NR_AX)
  4796. )
  4797. ) then
  4798. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  4799. begin
  4800. DebugMsg(SPeepholeOptimization + 'var7',p);
  4801. taicpu(p).opcode := A_AND;
  4802. taicpu(p).changeopsize(S_W);
  4803. taicpu(p).loadConst(0,$ff);
  4804. Result := True;
  4805. end
  4806. else if not IsMOVZXAcceptable and
  4807. GetNextInstruction(p, hp1) and
  4808. (tai(hp1).typ = ait_instruction) and
  4809. (taicpu(hp1).opcode = A_AND) and
  4810. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4811. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4812. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  4813. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  4814. begin
  4815. DebugMsg(SPeepholeOptimization + 'var8',p);
  4816. taicpu(p).opcode := A_MOV;
  4817. taicpu(p).changeopsize(S_W);
  4818. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  4819. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  4820. Result := True;
  4821. end;
  4822. end;
  4823. {$ifndef i8086} { movzbl %al,%eax cannot be encoded in 16-bit mode (the machine code is equivalent to movzbw %al,%ax }
  4824. S_BL:
  4825. begin
  4826. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  4827. (
  4828. not IsMOVZXAcceptable
  4829. { and $0xff,%eax has a smaller encoding but risks a partial write penalty }
  4830. or (
  4831. (cs_opt_size in current_settings.optimizerswitches) and
  4832. (taicpu(p).oper[1]^.reg = NR_EAX)
  4833. )
  4834. ) then
  4835. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  4836. begin
  4837. DebugMsg(SPeepholeOptimization + 'var9',p);
  4838. taicpu(p).opcode := A_AND;
  4839. taicpu(p).changeopsize(S_L);
  4840. taicpu(p).loadConst(0,$ff);
  4841. Result := True;
  4842. end
  4843. else if not IsMOVZXAcceptable and
  4844. GetNextInstruction(p, hp1) and
  4845. (tai(hp1).typ = ait_instruction) and
  4846. (taicpu(hp1).opcode = A_AND) and
  4847. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4848. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4849. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  4850. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  4851. begin
  4852. DebugMsg(SPeepholeOptimization + 'var10',p);
  4853. taicpu(p).opcode := A_MOV;
  4854. taicpu(p).changeopsize(S_L);
  4855. { do not use R_SUBWHOLE
  4856. as movl %rdx,%eax
  4857. is invalid in assembler PM }
  4858. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  4859. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  4860. Result := True;
  4861. end;
  4862. end;
  4863. {$endif i8086}
  4864. S_WL:
  4865. if not IsMOVZXAcceptable then
  4866. begin
  4867. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) then
  4868. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  4869. begin
  4870. DebugMsg(SPeepholeOptimization + 'var11',p);
  4871. taicpu(p).opcode := A_AND;
  4872. taicpu(p).changeopsize(S_L);
  4873. taicpu(p).loadConst(0,$ffff);
  4874. Result := True;
  4875. end
  4876. else if GetNextInstruction(p, hp1) and
  4877. (tai(hp1).typ = ait_instruction) and
  4878. (taicpu(hp1).opcode = A_AND) and
  4879. (taicpu(hp1).oper[0]^.typ = top_const) and
  4880. (taicpu(hp1).oper[1]^.typ = top_reg) and
  4881. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4882. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  4883. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  4884. begin
  4885. DebugMsg(SPeepholeOptimization + 'var12',p);
  4886. taicpu(p).opcode := A_MOV;
  4887. taicpu(p).changeopsize(S_L);
  4888. { do not use R_SUBWHOLE
  4889. as movl %rdx,%eax
  4890. is invalid in assembler PM }
  4891. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  4892. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  4893. Result := True;
  4894. end;
  4895. end;
  4896. else
  4897. InternalError(2017050705);
  4898. end;
  4899. end
  4900. else if not IsMOVZXAcceptable and (taicpu(p).oper[0]^.typ = top_ref) then
  4901. begin
  4902. if GetNextInstruction(p, hp1) and
  4903. (tai(hp1).typ = ait_instruction) and
  4904. (taicpu(hp1).opcode = A_AND) and
  4905. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4906. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4907. begin
  4908. //taicpu(p).opcode := A_MOV;
  4909. case taicpu(p).opsize Of
  4910. S_BL:
  4911. begin
  4912. DebugMsg(SPeepholeOptimization + 'var13',p);
  4913. taicpu(hp1).changeopsize(S_L);
  4914. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  4915. end;
  4916. S_WL:
  4917. begin
  4918. DebugMsg(SPeepholeOptimization + 'var14',p);
  4919. taicpu(hp1).changeopsize(S_L);
  4920. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  4921. end;
  4922. S_BW:
  4923. begin
  4924. DebugMsg(SPeepholeOptimization + 'var15',p);
  4925. taicpu(hp1).changeopsize(S_W);
  4926. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  4927. end;
  4928. else
  4929. Internalerror(2017050704)
  4930. end;
  4931. Result := True;
  4932. end;
  4933. end;
  4934. end;
  4935. end;
  4936. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  4937. var
  4938. hp1 : tai;
  4939. MaskLength : Cardinal;
  4940. begin
  4941. Result:=false;
  4942. if GetNextInstruction(p, hp1) then
  4943. begin
  4944. if MatchOpType(taicpu(p),top_const,top_reg) and
  4945. MatchInstruction(hp1,A_AND,[]) and
  4946. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4947. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4948. { the second register must contain the first one, so compare their subreg types }
  4949. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  4950. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  4951. { change
  4952. and const1, reg
  4953. and const2, reg
  4954. to
  4955. and (const1 and const2), reg
  4956. }
  4957. begin
  4958. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  4959. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  4960. asml.remove(p);
  4961. p.Free;
  4962. p:=hp1;
  4963. Result:=true;
  4964. exit;
  4965. end
  4966. else if MatchOpType(taicpu(p),top_const,top_reg) and
  4967. MatchInstruction(hp1,A_MOVZX,[]) and
  4968. (taicpu(hp1).oper[0]^.typ = top_reg) and
  4969. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  4970. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4971. (((taicpu(p).opsize=S_W) and
  4972. (taicpu(hp1).opsize=S_BW)) or
  4973. ((taicpu(p).opsize=S_L) and
  4974. (taicpu(hp1).opsize in [S_WL,S_BL]))
  4975. {$ifdef x86_64}
  4976. or
  4977. ((taicpu(p).opsize=S_Q) and
  4978. (taicpu(hp1).opsize in [S_BQ,S_WQ]))
  4979. {$endif x86_64}
  4980. ) then
  4981. begin
  4982. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  4983. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  4984. ) or
  4985. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  4986. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  4987. then
  4988. begin
  4989. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  4990. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  4991. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  4992. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  4993. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  4994. }
  4995. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  4996. asml.remove(hp1);
  4997. hp1.free;
  4998. Exit;
  4999. end;
  5000. end
  5001. else if MatchOpType(taicpu(p),top_const,top_reg) and
  5002. MatchInstruction(hp1,A_SHL,[]) and
  5003. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5004. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  5005. begin
  5006. {$ifopt R+}
  5007. {$define RANGE_WAS_ON}
  5008. {$R-}
  5009. {$endif}
  5010. { get length of potential and mask }
  5011. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  5012. { really a mask? }
  5013. {$ifdef RANGE_WAS_ON}
  5014. {$R+}
  5015. {$endif}
  5016. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  5017. { unmasked part shifted out? }
  5018. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  5019. begin
  5020. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  5021. { take care of the register (de)allocs following p }
  5022. UpdateUsedRegs(tai(p.next));
  5023. asml.remove(p);
  5024. p.free;
  5025. p:=hp1;
  5026. Result:=true;
  5027. exit;
  5028. end;
  5029. end
  5030. else if MatchOpType(taicpu(p),top_const,top_reg) and
  5031. MatchInstruction(hp1,A_MOVSX{$ifdef x86_64},A_MOVSXD{$endif x86_64},[]) and
  5032. (taicpu(hp1).oper[0]^.typ = top_reg) and
  5033. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  5034. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  5035. (((taicpu(p).opsize=S_W) and
  5036. (taicpu(hp1).opsize=S_BW)) or
  5037. ((taicpu(p).opsize=S_L) and
  5038. (taicpu(hp1).opsize in [S_WL,S_BL]))
  5039. {$ifdef x86_64}
  5040. or
  5041. ((taicpu(p).opsize=S_Q) and
  5042. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_LQ]))
  5043. {$endif x86_64}
  5044. ) then
  5045. begin
  5046. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  5047. ((taicpu(p).oper[0]^.val and $7f)=taicpu(p).oper[0]^.val)
  5048. ) or
  5049. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  5050. ((taicpu(p).oper[0]^.val and $7fff)=taicpu(p).oper[0]^.val))
  5051. {$ifdef x86_64}
  5052. or
  5053. (((taicpu(hp1).opsize)=S_LQ) and
  5054. ((taicpu(p).oper[0]^.val and $7fffffff)=taicpu(p).oper[0]^.val)
  5055. )
  5056. {$endif x86_64}
  5057. then
  5058. begin
  5059. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  5060. asml.remove(hp1);
  5061. hp1.free;
  5062. Exit;
  5063. end;
  5064. end
  5065. else if (taicpu(p).oper[1]^.typ = top_reg) and
  5066. (hp1.typ = ait_instruction) and
  5067. (taicpu(hp1).is_jmp) and
  5068. (taicpu(hp1).opcode<>A_JMP) and
  5069. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  5070. begin
  5071. { change
  5072. and x, reg
  5073. jxx
  5074. to
  5075. test x, reg
  5076. jxx
  5077. if reg is deallocated before the
  5078. jump, but only if it's a conditional jump (PFV)
  5079. }
  5080. taicpu(p).opcode := A_TEST;
  5081. Exit;
  5082. end;
  5083. end;
  5084. { Lone AND tests }
  5085. if MatchOpType(taicpu(p),top_const,top_reg) then
  5086. begin
  5087. {
  5088. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  5089. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  5090. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  5091. }
  5092. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  5093. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  5094. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  5095. begin
  5096. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg)
  5097. end;
  5098. end;
  5099. end;
  5100. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  5101. begin
  5102. Result:=false;
  5103. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  5104. MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  5105. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  5106. begin
  5107. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  5108. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  5109. taicpu(p).opcode:=A_ADD;
  5110. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  5111. result:=true;
  5112. end
  5113. else if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  5114. MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  5115. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  5116. begin
  5117. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  5118. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  5119. taicpu(p).opcode:=A_ADD;
  5120. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  5121. result:=true;
  5122. end;
  5123. end;
  5124. function TX86AsmOptimizer.OptPass2SUB(var p: tai): Boolean;
  5125. var
  5126. hp1: tai; NewRef: TReference;
  5127. begin
  5128. { Change:
  5129. subl/q $x,%reg1
  5130. movl/q %reg1,%reg2
  5131. To:
  5132. leal/q $-x(%reg1),%reg2
  5133. subl/q $x,%reg1
  5134. Breaks the dependency chain and potentially permits the removal of
  5135. a CMP instruction if one follows.
  5136. }
  5137. Result := False;
  5138. if not (cs_opt_size in current_settings.optimizerswitches) and
  5139. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  5140. MatchOpType(taicpu(p),top_const,top_reg) and
  5141. GetNextInstruction(p, hp1) and
  5142. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  5143. (taicpu(hp1).oper[1]^.typ = top_reg) and
  5144. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) then
  5145. begin
  5146. { Change the MOV instruction to a LEA instruction, and update the
  5147. first operand }
  5148. reference_reset(NewRef, 1, []);
  5149. NewRef.base := taicpu(p).oper[1]^.reg;
  5150. NewRef.scalefactor := 1;
  5151. NewRef.offset := -taicpu(p).oper[0]^.val;
  5152. taicpu(hp1).opcode := A_LEA;
  5153. taicpu(hp1).loadref(0, NewRef);
  5154. { Move what is now the LEA instruction to before the SUB instruction }
  5155. Asml.Remove(hp1);
  5156. Asml.InsertBefore(hp1, p);
  5157. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  5158. DebugMsg(SPeepholeOptimization + 'SubMov2LeaSub', p);
  5159. Result := True;
  5160. end;
  5161. end;
  5162. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  5163. function SkipSimpleInstructions(var hp1 : tai) : Boolean;
  5164. begin
  5165. { we can skip all instructions not messing with the stack pointer }
  5166. while assigned(hp1) and {MatchInstruction(taicpu(hp1),[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  5167. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  5168. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  5169. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  5170. ({(taicpu(hp1).ops=0) or }
  5171. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  5172. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  5173. ) and }
  5174. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  5175. )
  5176. ) do
  5177. GetNextInstruction(hp1,hp1);
  5178. Result:=assigned(hp1);
  5179. end;
  5180. var
  5181. hp1, hp2, hp3: tai;
  5182. begin
  5183. Result:=false;
  5184. { replace
  5185. leal(q) x(<stackpointer>),<stackpointer>
  5186. call procname
  5187. leal(q) -x(<stackpointer>),<stackpointer>
  5188. ret
  5189. by
  5190. jmp procname
  5191. but do it only on level 4 because it destroys stack back traces
  5192. }
  5193. if (cs_opt_level4 in current_settings.optimizerswitches) and
  5194. MatchOpType(taicpu(p),top_ref,top_reg) and
  5195. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  5196. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  5197. { the -8 or -24 are not required, but bail out early if possible,
  5198. higher values are unlikely }
  5199. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  5200. (taicpu(p).oper[0]^.ref^.offset=-24)) and
  5201. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  5202. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  5203. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  5204. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  5205. GetNextInstruction(p, hp1) and
  5206. { trick to skip label }
  5207. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  5208. SkipSimpleInstructions(hp1) and
  5209. MatchInstruction(hp1,A_CALL,[S_NO]) and
  5210. GetNextInstruction(hp1, hp2) and
  5211. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  5212. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  5213. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  5214. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  5215. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  5216. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  5217. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  5218. (taicpu(hp2).oper[0]^.ref^.segment=NR_NO) and
  5219. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  5220. GetNextInstruction(hp2, hp3) and
  5221. { trick to skip label }
  5222. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  5223. MatchInstruction(hp3,A_RET,[S_NO]) and
  5224. (taicpu(hp3).ops=0) then
  5225. begin
  5226. taicpu(hp1).opcode := A_JMP;
  5227. taicpu(hp1).is_jmp := true;
  5228. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  5229. RemoveCurrentP(p);
  5230. AsmL.Remove(hp2);
  5231. hp2.free;
  5232. AsmL.Remove(hp3);
  5233. hp3.free;
  5234. Result:=true;
  5235. end;
  5236. end;
  5237. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  5238. var
  5239. Value, RegName: string;
  5240. begin
  5241. Result:=false;
  5242. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  5243. begin
  5244. case taicpu(p).oper[0]^.val of
  5245. 0:
  5246. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  5247. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  5248. begin
  5249. { change "mov $0,%reg" into "xor %reg,%reg" }
  5250. taicpu(p).opcode := A_XOR;
  5251. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  5252. Result := True;
  5253. end;
  5254. $1..$FFFFFFFF:
  5255. begin
  5256. { Code size reduction by J. Gareth "Kit" Moreton }
  5257. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  5258. case taicpu(p).opsize of
  5259. S_Q:
  5260. begin
  5261. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  5262. Value := debug_tostr(taicpu(p).oper[0]^.val);
  5263. { The actual optimization }
  5264. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5265. taicpu(p).changeopsize(S_L);
  5266. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  5267. Result := True;
  5268. end;
  5269. else
  5270. { Do nothing };
  5271. end;
  5272. end;
  5273. -1:
  5274. { Don't make this optimisation if the CPU flags are required, since OR scrambles them }
  5275. if (cs_opt_size in current_settings.optimizerswitches) and
  5276. (taicpu(p).opsize <> S_B) and
  5277. not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  5278. begin
  5279. { change "mov $-1,%reg" into "or $-1,%reg" }
  5280. { NOTES:
  5281. - No size saving is made when changing a Word-sized assignment unless the register is AX (smaller encoding)
  5282. - This operation creates a false dependency on the register, so only do it when optimising for size
  5283. - It is possible to set memory operands using this method, but this creates an even greater false dependency, so don't do this at all
  5284. }
  5285. taicpu(p).opcode := A_OR;
  5286. Result := True;
  5287. end;
  5288. end;
  5289. end;
  5290. end;
  5291. function TX86AsmOptimizer.PostPeepholeOptMOVSX(var p : tai) : boolean;
  5292. begin
  5293. Result := False;
  5294. if not MatchOpType(taicpu(p), top_reg, top_reg) then
  5295. Exit;
  5296. { Convert:
  5297. movswl %ax,%eax -> cwtl
  5298. movslq %eax,%rax -> cdqe
  5299. NOTE: Don't convert movswl %al,%ax to cbw, because cbw and cwde
  5300. refer to the same opcode and depends only on the assembler's
  5301. current operand-size attribute. [Kit]
  5302. }
  5303. with taicpu(p) do
  5304. case opsize of
  5305. S_WL:
  5306. if (oper[0]^.reg = NR_AX) and (oper[1]^.reg = NR_EAX) then
  5307. begin
  5308. DebugMsg(SPeepholeOptimization + 'Converted movswl %ax,%eax to cwtl', p);
  5309. opcode := A_CWDE;
  5310. clearop(0);
  5311. clearop(1);
  5312. ops := 0;
  5313. Result := True;
  5314. end;
  5315. {$ifdef x86_64}
  5316. S_LQ:
  5317. if (oper[0]^.reg = NR_EAX) and (oper[1]^.reg = NR_RAX) then
  5318. begin
  5319. DebugMsg(SPeepholeOptimization + 'Converted movslq %eax,%rax to cltq', p);
  5320. opcode := A_CDQE;
  5321. clearop(0);
  5322. clearop(1);
  5323. ops := 0;
  5324. Result := True;
  5325. end;
  5326. {$endif x86_64}
  5327. else
  5328. ;
  5329. end;
  5330. end;
  5331. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  5332. begin
  5333. Result:=false;
  5334. { change "cmp $0, %reg" to "test %reg, %reg" }
  5335. if MatchOpType(taicpu(p),top_const,top_reg) and
  5336. (taicpu(p).oper[0]^.val = 0) then
  5337. begin
  5338. taicpu(p).opcode := A_TEST;
  5339. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  5340. Result:=true;
  5341. end;
  5342. end;
  5343. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  5344. var
  5345. IsTestConstX : Boolean;
  5346. hp1,hp2 : tai;
  5347. begin
  5348. Result:=false;
  5349. { removes the line marked with (x) from the sequence
  5350. and/or/xor/add/sub/... $x, %y
  5351. test/or %y, %y | test $-1, %y (x)
  5352. j(n)z _Label
  5353. as the first instruction already adjusts the ZF
  5354. %y operand may also be a reference }
  5355. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  5356. MatchOperand(taicpu(p).oper[0]^,-1);
  5357. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  5358. GetLastInstruction(p, hp1) and
  5359. (tai(hp1).typ = ait_instruction) and
  5360. GetNextInstruction(p,hp2) and
  5361. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  5362. case taicpu(hp1).opcode Of
  5363. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  5364. begin
  5365. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  5366. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  5367. { and in case of carry for A(E)/B(E)/C/NC }
  5368. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  5369. ((taicpu(hp1).opcode <> A_ADD) and
  5370. (taicpu(hp1).opcode <> A_SUB))) then
  5371. begin
  5372. hp1 := tai(p.next);
  5373. asml.remove(p);
  5374. p.free;
  5375. p := tai(hp1);
  5376. Result:=true;
  5377. end;
  5378. end;
  5379. A_SHL, A_SAL, A_SHR, A_SAR:
  5380. begin
  5381. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  5382. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  5383. { therefore, it's only safe to do this optimization for }
  5384. { shifts by a (nonzero) constant }
  5385. (taicpu(hp1).oper[0]^.typ = top_const) and
  5386. (taicpu(hp1).oper[0]^.val <> 0) and
  5387. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  5388. { and in case of carry for A(E)/B(E)/C/NC }
  5389. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  5390. begin
  5391. hp1 := tai(p.next);
  5392. asml.remove(p);
  5393. p.free;
  5394. p := tai(hp1);
  5395. Result:=true;
  5396. end;
  5397. end;
  5398. A_DEC, A_INC, A_NEG:
  5399. begin
  5400. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  5401. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  5402. { and in case of carry for A(E)/B(E)/C/NC }
  5403. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  5404. begin
  5405. case taicpu(hp1).opcode of
  5406. A_DEC, A_INC:
  5407. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  5408. begin
  5409. case taicpu(hp1).opcode Of
  5410. A_DEC: taicpu(hp1).opcode := A_SUB;
  5411. A_INC: taicpu(hp1).opcode := A_ADD;
  5412. else
  5413. ;
  5414. end;
  5415. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  5416. taicpu(hp1).loadConst(0,1);
  5417. taicpu(hp1).ops:=2;
  5418. end;
  5419. else
  5420. ;
  5421. end;
  5422. hp1 := tai(p.next);
  5423. asml.remove(p);
  5424. p.free;
  5425. p := tai(hp1);
  5426. Result:=true;
  5427. end;
  5428. end
  5429. else
  5430. { change "test $-1,%reg" into "test %reg,%reg" }
  5431. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  5432. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  5433. end { case }
  5434. { change "test $-1,%reg" into "test %reg,%reg" }
  5435. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  5436. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  5437. end;
  5438. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  5439. var
  5440. hp1 : tai;
  5441. {$ifndef x86_64}
  5442. hp2 : taicpu;
  5443. {$endif x86_64}
  5444. begin
  5445. Result:=false;
  5446. {$ifndef x86_64}
  5447. { don't do this on modern CPUs, this really hurts them due to
  5448. broken call/ret pairing }
  5449. if (current_settings.optimizecputype < cpu_Pentium2) and
  5450. not(cs_create_pic in current_settings.moduleswitches) and
  5451. GetNextInstruction(p, hp1) and
  5452. MatchInstruction(hp1,A_JMP,[S_NO]) and
  5453. MatchOpType(taicpu(hp1),top_ref) and
  5454. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  5455. begin
  5456. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  5457. InsertLLItem(p.previous, p, hp2);
  5458. taicpu(p).opcode := A_JMP;
  5459. taicpu(p).is_jmp := true;
  5460. asml.remove(hp1);
  5461. hp1.free;
  5462. Result:=true;
  5463. end
  5464. else
  5465. {$endif x86_64}
  5466. { replace
  5467. call procname
  5468. ret
  5469. by
  5470. jmp procname
  5471. but do it only on level 4 because it destroys stack back traces
  5472. }
  5473. if (cs_opt_level4 in current_settings.optimizerswitches) and
  5474. GetNextInstruction(p, hp1) and
  5475. MatchInstruction(hp1,A_RET,[S_NO]) and
  5476. (taicpu(hp1).ops=0) then
  5477. begin
  5478. taicpu(p).opcode := A_JMP;
  5479. taicpu(p).is_jmp := true;
  5480. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  5481. asml.remove(hp1);
  5482. hp1.free;
  5483. Result:=true;
  5484. end;
  5485. end;
  5486. {$ifdef x86_64}
  5487. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  5488. var
  5489. PreMessage: string;
  5490. begin
  5491. Result := False;
  5492. { Code size reduction by J. Gareth "Kit" Moreton }
  5493. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  5494. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  5495. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  5496. then
  5497. begin
  5498. { Has 64-bit register name and opcode suffix }
  5499. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  5500. { The actual optimization }
  5501. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5502. if taicpu(p).opsize = S_BQ then
  5503. taicpu(p).changeopsize(S_BL)
  5504. else
  5505. taicpu(p).changeopsize(S_WL);
  5506. DebugMsg(SPeepholeOptimization + PreMessage +
  5507. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  5508. end;
  5509. end;
  5510. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  5511. var
  5512. PreMessage, RegName: string;
  5513. begin
  5514. { Code size reduction by J. Gareth "Kit" Moreton }
  5515. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  5516. as this removes the REX prefix }
  5517. Result := False;
  5518. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  5519. Exit;
  5520. if taicpu(p).oper[0]^.typ <> top_reg then
  5521. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  5522. InternalError(2018011500);
  5523. case taicpu(p).opsize of
  5524. S_Q:
  5525. begin
  5526. 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
  5527. begin
  5528. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  5529. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  5530. { The actual optimization }
  5531. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  5532. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5533. taicpu(p).changeopsize(S_L);
  5534. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  5535. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  5536. end;
  5537. end;
  5538. else
  5539. ;
  5540. end;
  5541. end;
  5542. {$endif}
  5543. class procedure TX86AsmOptimizer.OptimizeRefs(var p: taicpu);
  5544. var
  5545. OperIdx: Integer;
  5546. begin
  5547. for OperIdx := 0 to p.ops - 1 do
  5548. if p.oper[OperIdx]^.typ = top_ref then
  5549. optimize_ref(p.oper[OperIdx]^.ref^, False);
  5550. end;
  5551. end.