aoptx86.pas 296 KB

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