aoptx86.pas 260 KB

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