aoptx86.pas 252 KB

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