aoptx86.pas 278 KB

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