aoptx86.pas 275 KB

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