aoptx86.pas 267 KB

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