aoptx86.pas 456 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632
  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. {$ifdef EXTDEBUG}
  20. {$define DEBUG_AOPTCPU}
  21. {$endif EXTDEBUG}
  22. interface
  23. uses
  24. globtype,
  25. cpubase,
  26. aasmtai,aasmcpu,
  27. cgbase,cgutils,
  28. aopt,aoptobj;
  29. type
  30. TOptsToCheck = (
  31. aoc_MovAnd2Mov_3
  32. );
  33. TX86AsmOptimizer = class(TAsmOptimizer)
  34. { some optimizations are very expensive to check, so the
  35. pre opt pass can be used to set some flags, depending on the found
  36. instructions if it is worth to check a certain optimization }
  37. OptsToCheck : set of TOptsToCheck;
  38. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  39. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  40. function RegReadByInstruction(reg : TRegister; hp : tai) : boolean;
  41. function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  42. function GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  43. { This version of GetNextInstructionUsingReg will look across conditional jumps,
  44. potentially allowing further optimisation (although it might need to know if
  45. it crossed a conditional jump. }
  46. function GetNextInstructionUsingRegCond(Current: tai; out Next: tai; reg: TRegister; var CrossJump: Boolean): Boolean;
  47. {
  48. In comparison with GetNextInstructionUsingReg, GetNextInstructionUsingRegTrackingUse tracks
  49. the use of a register by allocs/dealloc, so it can ignore calls.
  50. In the following example, GetNextInstructionUsingReg will return the second movq,
  51. GetNextInstructionUsingRegTrackingUse won't.
  52. movq %rdi,%rax
  53. # Register rdi released
  54. # Register rdi allocated
  55. movq %rax,%rdi
  56. While in this example:
  57. movq %rdi,%rax
  58. call proc
  59. movq %rdi,%rax
  60. GetNextInstructionUsingRegTrackingUse will return the second instruction while GetNextInstructionUsingReg
  61. won't.
  62. }
  63. function GetNextInstructionUsingRegTrackingUse(Current: tai; out Next: tai; reg: TRegister): Boolean;
  64. function RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean; override;
  65. private
  66. function SkipSimpleInstructions(var hp1: tai): Boolean;
  67. protected
  68. class function IsMOVZXAcceptable: Boolean; static; inline;
  69. { Attempts to allocate a volatile integer register for use between p and hp,
  70. using AUsedRegs for the current register usage information. Returns NR_NO
  71. if no free register could be found }
  72. function GetIntRegisterBetween(RegSize: TSubRegister; var AUsedRegs: TAllUsedRegs; p, hp: tai): TRegister;
  73. { Attempts to allocate a volatile MM register for use between p and hp,
  74. using AUsedRegs for the current register usage information. Returns NR_NO
  75. if no free register could be found }
  76. function GetMMRegisterBetween(RegSize: TSubRegister; var AUsedRegs: TAllUsedRegs; p, hp: tai): TRegister;
  77. { checks whether loading a new value in reg1 overwrites the entirety of reg2 }
  78. function Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  79. { checks whether reading the value in reg1 depends on the value of reg2. This
  80. is very similar to SuperRegisterEquals, except it takes into account that
  81. R_SUBH and R_SUBL are independendent (e.g. reading from AL does not
  82. depend on the value in AH). }
  83. function Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  84. { Replaces all references to AOldReg in a memory reference to ANewReg }
  85. class function ReplaceRegisterInRef(var ref: TReference; const AOldReg, ANewReg: TRegister): Boolean; static;
  86. { Replaces all references to AOldReg in an operand to ANewReg }
  87. class function ReplaceRegisterInOper(const p: taicpu; const OperIdx: Integer; const AOldReg, ANewReg: TRegister): Boolean; static;
  88. { Replaces all references to AOldReg in an instruction to ANewReg,
  89. except where the register is being written }
  90. function ReplaceRegisterInInstruction(const p: taicpu; const AOldReg, ANewReg: TRegister): Boolean;
  91. { Returns true if the reference only refers to ESP or EBP (or their 64-bit equivalents),
  92. or writes to a global symbol }
  93. class function IsRefSafe(const ref: PReference): Boolean; static; inline;
  94. { Returns true if the given MOV instruction can be safely converted to CMOV }
  95. class function CanBeCMOV(p : tai) : boolean; static;
  96. { Converts the LEA instruction to ADD/INC/SUB/DEC. Returns True if the
  97. conversion was successful }
  98. function ConvertLEA(const p : taicpu): Boolean;
  99. function DeepMOVOpt(const p_mov: taicpu; const hp: taicpu): Boolean;
  100. procedure DebugMsg(const s : string; p : tai);inline;
  101. class function IsExitCode(p : tai) : boolean; static;
  102. class function isFoldableArithOp(hp1 : taicpu; reg : tregister) : boolean; static;
  103. procedure RemoveLastDeallocForFuncRes(p : tai);
  104. function DoSubAddOpt(var p : tai) : Boolean;
  105. function PrePeepholeOptSxx(var p : tai) : boolean;
  106. function PrePeepholeOptIMUL(var p : tai) : boolean;
  107. function PrePeepholeOptAND(var p : tai) : boolean;
  108. function OptPass1Test(var p: tai): boolean;
  109. function OptPass1Add(var p: tai): boolean;
  110. function OptPass1AND(var p : tai) : boolean;
  111. function OptPass1_V_MOVAP(var p : tai) : boolean;
  112. function OptPass1VOP(var p : tai) : boolean;
  113. function OptPass1MOV(var p : tai) : boolean;
  114. function OptPass1Movx(var p : tai) : boolean;
  115. function OptPass1MOVXX(var p : tai) : boolean;
  116. function OptPass1OP(var p : tai) : boolean;
  117. function OptPass1LEA(var p : tai) : boolean;
  118. function OptPass1Sub(var p : tai) : boolean;
  119. function OptPass1SHLSAL(var p : tai) : boolean;
  120. function OptPass1FSTP(var p : tai) : boolean;
  121. function OptPass1FLD(var p : tai) : boolean;
  122. function OptPass1Cmp(var p : tai) : boolean;
  123. function OptPass1PXor(var p : tai) : boolean;
  124. function OptPass1VPXor(var p: tai): boolean;
  125. function OptPass1Imul(var p : tai) : boolean;
  126. function OptPass1Jcc(var p : tai) : boolean;
  127. function OptPass1SHXX(var p: tai): boolean;
  128. function OptPass1VMOVDQ(var p: tai): Boolean;
  129. function OptPass1_V_Cvtss2sd(var p: tai): boolean;
  130. function OptPass2Movx(var p : tai): Boolean;
  131. function OptPass2MOV(var p : tai) : boolean;
  132. function OptPass2Imul(var p : tai) : boolean;
  133. function OptPass2Jmp(var p : tai) : boolean;
  134. function OptPass2Jcc(var p : tai) : boolean;
  135. function OptPass2Lea(var p: tai): Boolean;
  136. function OptPass2SUB(var p: tai): Boolean;
  137. function OptPass2ADD(var p : tai): Boolean;
  138. function OptPass2SETcc(var p : tai) : boolean;
  139. function CheckMemoryWrite(var first_mov, second_mov: taicpu): Boolean;
  140. function PostPeepholeOptMov(var p : tai) : Boolean;
  141. function PostPeepholeOptMovzx(var p : tai) : Boolean;
  142. {$ifdef x86_64} { These post-peephole optimisations only affect 64-bit registers. [Kit] }
  143. function PostPeepholeOptXor(var p : tai) : Boolean;
  144. {$endif x86_64}
  145. function PostPeepholeOptAnd(var p : tai) : boolean;
  146. function PostPeepholeOptMOVSX(var p : tai) : boolean;
  147. function PostPeepholeOptCmp(var p : tai) : Boolean;
  148. function PostPeepholeOptTestOr(var p : tai) : Boolean;
  149. function PostPeepholeOptCall(var p : tai) : Boolean;
  150. function PostPeepholeOptLea(var p : tai) : Boolean;
  151. function PostPeepholeOptPush(var p: tai): Boolean;
  152. function PostPeepholeOptShr(var p : tai) : boolean;
  153. function PostPeepholeOptVPXOR(var p: tai): Boolean;
  154. procedure ConvertJumpToRET(const p: tai; const ret_p: tai);
  155. function CheckJumpMovTransferOpt(var p: tai; hp1: tai; LoopCount: Integer; out Count: Integer): Boolean;
  156. procedure SwapMovCmp(var p, hp1: tai);
  157. { Processor-dependent reference optimisation }
  158. class procedure OptimizeRefs(var p: taicpu); static;
  159. end;
  160. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  161. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  162. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  163. function MatchInstruction(const instr: tai; const ops: array of TAsmOp; const opsize: topsizes): boolean;
  164. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  165. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  166. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  167. {$if max_operands>2}
  168. function MatchOperand(const oper1: TOper; const oper2: TOper; const oper3: TOper): boolean;
  169. {$endif max_operands>2}
  170. function RefsEqual(const r1, r2: treference): boolean;
  171. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  172. { returns true, if ref is a reference using only the registers passed as base and index
  173. and having an offset }
  174. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  175. implementation
  176. uses
  177. cutils,verbose,
  178. systems,
  179. globals,
  180. cpuinfo,
  181. procinfo,
  182. paramgr,
  183. aasmbase,
  184. aoptbase,aoptutils,
  185. symconst,symsym,
  186. cgx86,
  187. itcpugas;
  188. {$ifdef DEBUG_AOPTCPU}
  189. const
  190. SPeepholeOptimization: shortstring = 'Peephole Optimization: ';
  191. {$else DEBUG_AOPTCPU}
  192. { Empty strings help the optimizer to remove string concatenations that won't
  193. ever appear to the user on release builds. [Kit] }
  194. const
  195. SPeepholeOptimization = '';
  196. {$endif DEBUG_AOPTCPU}
  197. LIST_STEP_SIZE = 4;
  198. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  199. begin
  200. result :=
  201. (instr.typ = ait_instruction) and
  202. (taicpu(instr).opcode = op) and
  203. ((opsize = []) or (taicpu(instr).opsize in opsize));
  204. end;
  205. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  206. begin
  207. result :=
  208. (instr.typ = ait_instruction) and
  209. ((taicpu(instr).opcode = op1) or
  210. (taicpu(instr).opcode = op2)
  211. ) and
  212. ((opsize = []) or (taicpu(instr).opsize in opsize));
  213. end;
  214. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  215. begin
  216. result :=
  217. (instr.typ = ait_instruction) and
  218. ((taicpu(instr).opcode = op1) or
  219. (taicpu(instr).opcode = op2) or
  220. (taicpu(instr).opcode = op3)
  221. ) and
  222. ((opsize = []) or (taicpu(instr).opsize in opsize));
  223. end;
  224. function MatchInstruction(const instr : tai;const ops : array of TAsmOp;
  225. const opsize : topsizes) : boolean;
  226. var
  227. op : TAsmOp;
  228. begin
  229. result:=false;
  230. if (instr.typ <> ait_instruction) or
  231. ((opsize <> []) and not(taicpu(instr).opsize in opsize)) then
  232. exit;
  233. for op in ops do
  234. begin
  235. if taicpu(instr).opcode = op then
  236. begin
  237. result:=true;
  238. exit;
  239. end;
  240. end;
  241. end;
  242. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  243. begin
  244. result := (oper.typ = top_reg) and (oper.reg = reg);
  245. end;
  246. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  247. begin
  248. result := (oper.typ = top_const) and (oper.val = a);
  249. end;
  250. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  251. begin
  252. result := oper1.typ = oper2.typ;
  253. if result then
  254. case oper1.typ of
  255. top_const:
  256. Result:=oper1.val = oper2.val;
  257. top_reg:
  258. Result:=oper1.reg = oper2.reg;
  259. top_ref:
  260. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  261. else
  262. internalerror(2013102801);
  263. end
  264. end;
  265. function MatchOperand(const oper1: TOper; const oper2: TOper; const oper3: TOper): boolean;
  266. begin
  267. result := (oper1.typ = oper2.typ) and (oper1.typ = oper3.typ);
  268. if result then
  269. case oper1.typ of
  270. top_const:
  271. Result:=(oper1.val = oper2.val) and (oper1.val = oper3.val);
  272. top_reg:
  273. Result:=(oper1.reg = oper2.reg) and (oper1.reg = oper3.reg);
  274. top_ref:
  275. Result:=RefsEqual(oper1.ref^, oper2.ref^) and RefsEqual(oper1.ref^, oper3.ref^);
  276. else
  277. internalerror(2020052401);
  278. end
  279. end;
  280. function RefsEqual(const r1, r2: treference): boolean;
  281. begin
  282. RefsEqual :=
  283. (r1.offset = r2.offset) and
  284. (r1.segment = r2.segment) and (r1.base = r2.base) and
  285. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  286. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  287. (r1.relsymbol = r2.relsymbol) and
  288. (r1.volatility=[]) and
  289. (r2.volatility=[]);
  290. end;
  291. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  292. begin
  293. Result:=(ref.offset=0) and
  294. (ref.scalefactor in [0,1]) and
  295. (ref.segment=NR_NO) and
  296. (ref.symbol=nil) and
  297. (ref.relsymbol=nil) and
  298. ((base=NR_INVALID) or
  299. (ref.base=base)) and
  300. ((index=NR_INVALID) or
  301. (ref.index=index)) and
  302. (ref.volatility=[]);
  303. end;
  304. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  305. begin
  306. Result:=(ref.scalefactor in [0,1]) and
  307. (ref.segment=NR_NO) and
  308. (ref.symbol=nil) and
  309. (ref.relsymbol=nil) and
  310. ((base=NR_INVALID) or
  311. (ref.base=base)) and
  312. ((index=NR_INVALID) or
  313. (ref.index=index)) and
  314. (ref.volatility=[]);
  315. end;
  316. function InstrReadsFlags(p: tai): boolean;
  317. begin
  318. InstrReadsFlags := true;
  319. case p.typ of
  320. ait_instruction:
  321. if InsProp[taicpu(p).opcode].Ch*
  322. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  323. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  324. Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc,Ch_All]<>[] then
  325. exit;
  326. ait_label:
  327. exit;
  328. else
  329. ;
  330. end;
  331. InstrReadsFlags := false;
  332. end;
  333. function TX86AsmOptimizer.GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  334. begin
  335. Next:=Current;
  336. repeat
  337. Result:=GetNextInstruction(Next,Next);
  338. until not (Result) or
  339. not(cs_opt_level3 in current_settings.optimizerswitches) or
  340. (Next.typ<>ait_instruction) or
  341. RegInInstruction(reg,Next) or
  342. is_calljmp(taicpu(Next).opcode);
  343. end;
  344. function TX86AsmOptimizer.GetNextInstructionUsingRegCond(Current: tai; out Next: tai; reg: TRegister; var CrossJump: Boolean): Boolean;
  345. begin
  346. { Note, CrossJump keeps its input value if a conditional jump is not found - it doesn't get set to False }
  347. Next := Current;
  348. repeat
  349. Result := GetNextInstruction(Next,Next);
  350. if Result and (Next.typ=ait_instruction) and is_calljmp(taicpu(Next).opcode) then
  351. if is_calljmpuncondret(taicpu(Next).opcode) then
  352. begin
  353. Result := False;
  354. Exit;
  355. end
  356. else
  357. CrossJump := True;
  358. until not Result or
  359. not (cs_opt_level3 in current_settings.optimizerswitches) or
  360. (Next.typ <> ait_instruction) or
  361. RegInInstruction(reg,Next);
  362. end;
  363. function TX86AsmOptimizer.GetNextInstructionUsingRegTrackingUse(Current: tai; out Next: tai; reg: TRegister): Boolean;
  364. begin
  365. if not(cs_opt_level3 in current_settings.optimizerswitches) then
  366. begin
  367. Result:=GetNextInstruction(Current,Next);
  368. exit;
  369. end;
  370. Next:=tai(Current.Next);
  371. Result:=false;
  372. while assigned(Next) do
  373. begin
  374. if ((Next.typ=ait_instruction) and is_calljmp(taicpu(Next).opcode) and not(taicpu(Next).opcode=A_CALL)) or
  375. ((Next.typ=ait_regalloc) and (getsupreg(tai_regalloc(Next).reg)=getsupreg(reg))) or
  376. ((Next.typ=ait_label) and not(labelCanBeSkipped(Tai_Label(Next)))) then
  377. exit
  378. else if (Next.typ=ait_instruction) and RegInInstruction(reg,Next) and not(taicpu(Next).opcode=A_CALL) then
  379. begin
  380. Result:=true;
  381. exit;
  382. end;
  383. Next:=tai(Next.Next);
  384. end;
  385. end;
  386. function TX86AsmOptimizer.InstructionLoadsFromReg(const reg: TRegister;const hp: tai): boolean;
  387. begin
  388. Result:=RegReadByInstruction(reg,hp);
  389. end;
  390. function TX86AsmOptimizer.RegReadByInstruction(reg: TRegister; hp: tai): boolean;
  391. var
  392. p: taicpu;
  393. opcount: longint;
  394. begin
  395. RegReadByInstruction := false;
  396. if hp.typ <> ait_instruction then
  397. exit;
  398. p := taicpu(hp);
  399. case p.opcode of
  400. A_CALL:
  401. regreadbyinstruction := true;
  402. A_IMUL:
  403. case p.ops of
  404. 1:
  405. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  406. (
  407. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  408. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  409. );
  410. 2,3:
  411. regReadByInstruction :=
  412. reginop(reg,p.oper[0]^) or
  413. reginop(reg,p.oper[1]^);
  414. else
  415. InternalError(2019112801);
  416. end;
  417. A_MUL:
  418. begin
  419. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  420. (
  421. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  422. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  423. );
  424. end;
  425. A_IDIV,A_DIV:
  426. begin
  427. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  428. (
  429. (getregtype(reg)=R_INTREGISTER) and
  430. (
  431. (getsupreg(reg)=RS_EAX) or ((getsupreg(reg)=RS_EDX) and (p.opsize<>S_B))
  432. )
  433. );
  434. end;
  435. else
  436. begin
  437. if (p.opcode=A_LEA) and is_segment_reg(reg) then
  438. begin
  439. RegReadByInstruction := false;
  440. exit;
  441. end;
  442. for opcount := 0 to p.ops-1 do
  443. if (p.oper[opCount]^.typ = top_ref) and
  444. RegInRef(reg,p.oper[opcount]^.ref^) then
  445. begin
  446. RegReadByInstruction := true;
  447. exit
  448. end;
  449. { special handling for SSE MOVSD }
  450. if (p.opcode=A_MOVSD) and (p.ops>0) then
  451. begin
  452. if p.ops<>2 then
  453. internalerror(2017042702);
  454. regReadByInstruction := reginop(reg,p.oper[0]^) or
  455. (
  456. (p.oper[1]^.typ=top_reg) and (p.oper[0]^.typ=top_reg) and reginop(reg, p.oper[1]^)
  457. );
  458. exit;
  459. end;
  460. with insprop[p.opcode] do
  461. begin
  462. case getregtype(reg) of
  463. R_INTREGISTER:
  464. begin
  465. case getsupreg(reg) of
  466. RS_EAX:
  467. if [Ch_REAX,Ch_RWEAX,Ch_MEAX]*Ch<>[] then
  468. begin
  469. RegReadByInstruction := true;
  470. exit
  471. end;
  472. RS_ECX:
  473. if [Ch_RECX,Ch_RWECX,Ch_MECX]*Ch<>[] then
  474. begin
  475. RegReadByInstruction := true;
  476. exit
  477. end;
  478. RS_EDX:
  479. if [Ch_REDX,Ch_RWEDX,Ch_MEDX]*Ch<>[] then
  480. begin
  481. RegReadByInstruction := true;
  482. exit
  483. end;
  484. RS_EBX:
  485. if [Ch_REBX,Ch_RWEBX,Ch_MEBX]*Ch<>[] then
  486. begin
  487. RegReadByInstruction := true;
  488. exit
  489. end;
  490. RS_ESP:
  491. if [Ch_RESP,Ch_RWESP,Ch_MESP]*Ch<>[] then
  492. begin
  493. RegReadByInstruction := true;
  494. exit
  495. end;
  496. RS_EBP:
  497. if [Ch_REBP,Ch_RWEBP,Ch_MEBP]*Ch<>[] then
  498. begin
  499. RegReadByInstruction := true;
  500. exit
  501. end;
  502. RS_ESI:
  503. if [Ch_RESI,Ch_RWESI,Ch_MESI]*Ch<>[] then
  504. begin
  505. RegReadByInstruction := true;
  506. exit
  507. end;
  508. RS_EDI:
  509. if [Ch_REDI,Ch_RWEDI,Ch_MEDI]*Ch<>[] then
  510. begin
  511. RegReadByInstruction := true;
  512. exit
  513. end;
  514. end;
  515. end;
  516. R_MMREGISTER:
  517. begin
  518. case getsupreg(reg) of
  519. RS_XMM0:
  520. if [Ch_RXMM0,Ch_RWXMM0,Ch_MXMM0]*Ch<>[] then
  521. begin
  522. RegReadByInstruction := true;
  523. exit
  524. end;
  525. end;
  526. end;
  527. else
  528. ;
  529. end;
  530. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  531. begin
  532. if (Ch_RFLAGScc in Ch) and not(getsubreg(reg) in [R_SUBW,R_SUBD,R_SUBQ]) then
  533. begin
  534. case p.condition of
  535. C_A,C_NBE, { CF=0 and ZF=0 }
  536. C_BE,C_NA: { CF=1 or ZF=1 }
  537. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY,R_SUBFLAGZERO];
  538. C_AE,C_NB,C_NC, { CF=0 }
  539. C_B,C_NAE,C_C: { CF=1 }
  540. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY];
  541. C_NE,C_NZ, { ZF=0 }
  542. C_E,C_Z: { ZF=1 }
  543. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO];
  544. C_G,C_NLE, { ZF=0 and SF=OF }
  545. C_LE,C_NG: { ZF=1 or SF<>OF }
  546. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO,R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  547. C_GE,C_NL, { SF=OF }
  548. C_L,C_NGE: { SF<>OF }
  549. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  550. C_NO, { OF=0 }
  551. C_O: { OF=1 }
  552. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGOVERFLOW];
  553. C_NP,C_PO, { PF=0 }
  554. C_P,C_PE: { PF=1 }
  555. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGPARITY];
  556. C_NS, { SF=0 }
  557. C_S: { SF=1 }
  558. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN];
  559. else
  560. internalerror(2017042701);
  561. end;
  562. if RegReadByInstruction then
  563. exit;
  564. end;
  565. case getsubreg(reg) of
  566. R_SUBW,R_SUBD,R_SUBQ:
  567. RegReadByInstruction :=
  568. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  569. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  570. Ch_RDirFlag,Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc]*Ch<>[];
  571. R_SUBFLAGCARRY:
  572. RegReadByInstruction:=[Ch_RCarryFlag,Ch_RWCarryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  573. R_SUBFLAGPARITY:
  574. RegReadByInstruction:=[Ch_RParityFlag,Ch_RWParityFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  575. R_SUBFLAGAUXILIARY:
  576. RegReadByInstruction:=[Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  577. R_SUBFLAGZERO:
  578. RegReadByInstruction:=[Ch_RZeroFlag,Ch_RWZeroFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  579. R_SUBFLAGSIGN:
  580. RegReadByInstruction:=[Ch_RSignFlag,Ch_RWSignFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  581. R_SUBFLAGOVERFLOW:
  582. RegReadByInstruction:=[Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  583. R_SUBFLAGINTERRUPT:
  584. RegReadByInstruction:=[Ch_RFlags,Ch_RWFlags]*Ch<>[];
  585. R_SUBFLAGDIRECTION:
  586. RegReadByInstruction:=[Ch_RDirFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  587. else
  588. internalerror(2017042601);
  589. end;
  590. exit;
  591. end;
  592. if (Ch_NoReadIfEqualRegs in Ch) and (p.ops=2) and
  593. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  594. (p.oper[0]^.reg=p.oper[1]^.reg) then
  595. exit;
  596. if ([CH_RWOP1,CH_ROP1,CH_MOP1]*Ch<>[]) and reginop(reg,p.oper[0]^) then
  597. begin
  598. RegReadByInstruction := true;
  599. exit
  600. end;
  601. if ([Ch_RWOP2,Ch_ROP2,Ch_MOP2]*Ch<>[]) and reginop(reg,p.oper[1]^) then
  602. begin
  603. RegReadByInstruction := true;
  604. exit
  605. end;
  606. if ([Ch_RWOP3,Ch_ROP3,Ch_MOP3]*Ch<>[]) and reginop(reg,p.oper[2]^) then
  607. begin
  608. RegReadByInstruction := true;
  609. exit
  610. end;
  611. if ([Ch_RWOP4,Ch_ROP4,Ch_MOP4]*Ch<>[]) and reginop(reg,p.oper[3]^) then
  612. begin
  613. RegReadByInstruction := true;
  614. exit
  615. end;
  616. end;
  617. end;
  618. end;
  619. end;
  620. function TX86AsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  621. begin
  622. result:=false;
  623. if p1.typ<>ait_instruction then
  624. exit;
  625. if (Ch_All in insprop[taicpu(p1).opcode].Ch) then
  626. exit(true);
  627. if (getregtype(reg)=R_INTREGISTER) and
  628. { change information for xmm movsd are not correct }
  629. ((taicpu(p1).opcode<>A_MOVSD) or (taicpu(p1).ops=0)) then
  630. begin
  631. case getsupreg(reg) of
  632. { RS_EAX = RS_RAX on x86-64 }
  633. RS_EAX:
  634. result:=([Ch_REAX,Ch_RRAX,Ch_WEAX,Ch_WRAX,Ch_RWEAX,Ch_RWRAX,Ch_MEAX,Ch_MRAX]*insprop[taicpu(p1).opcode].Ch)<>[];
  635. RS_ECX:
  636. result:=([Ch_RECX,Ch_RRCX,Ch_WECX,Ch_WRCX,Ch_RWECX,Ch_RWRCX,Ch_MECX,Ch_MRCX]*insprop[taicpu(p1).opcode].Ch)<>[];
  637. RS_EDX:
  638. result:=([Ch_REDX,Ch_RRDX,Ch_WEDX,Ch_WRDX,Ch_RWEDX,Ch_RWRDX,Ch_MEDX,Ch_MRDX]*insprop[taicpu(p1).opcode].Ch)<>[];
  639. RS_EBX:
  640. result:=([Ch_REBX,Ch_RRBX,Ch_WEBX,Ch_WRBX,Ch_RWEBX,Ch_RWRBX,Ch_MEBX,Ch_MRBX]*insprop[taicpu(p1).opcode].Ch)<>[];
  641. RS_ESP:
  642. result:=([Ch_RESP,Ch_RRSP,Ch_WESP,Ch_WRSP,Ch_RWESP,Ch_RWRSP,Ch_MESP,Ch_MRSP]*insprop[taicpu(p1).opcode].Ch)<>[];
  643. RS_EBP:
  644. result:=([Ch_REBP,Ch_RRBP,Ch_WEBP,Ch_WRBP,Ch_RWEBP,Ch_RWRBP,Ch_MEBP,Ch_MRBP]*insprop[taicpu(p1).opcode].Ch)<>[];
  645. RS_ESI:
  646. result:=([Ch_RESI,Ch_RRSI,Ch_WESI,Ch_WRSI,Ch_RWESI,Ch_RWRSI,Ch_MESI,Ch_MRSI,Ch_RMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  647. RS_EDI:
  648. result:=([Ch_REDI,Ch_RRDI,Ch_WEDI,Ch_WRDI,Ch_RWEDI,Ch_RWRDI,Ch_MEDI,Ch_MRDI,Ch_WMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  649. else
  650. ;
  651. end;
  652. if result then
  653. exit;
  654. end
  655. else if getregtype(reg)=R_MMREGISTER then
  656. begin
  657. case getsupreg(reg) of
  658. RS_XMM0:
  659. result:=([Ch_RXMM0,Ch_WXMM0,Ch_RWXMM0,Ch_MXMM0]*insprop[taicpu(p1).opcode].Ch)<>[];
  660. else
  661. ;
  662. end;
  663. if result then
  664. exit;
  665. end
  666. else if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  667. begin
  668. if ([Ch_RFlags,Ch_WFlags,Ch_RWFlags,Ch_RFLAGScc]*insprop[taicpu(p1).opcode].Ch)<>[] then
  669. exit(true);
  670. case getsubreg(reg) of
  671. R_SUBFLAGCARRY:
  672. Result:=([Ch_RCarryFlag,Ch_RWCarryFlag,Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  673. R_SUBFLAGPARITY:
  674. Result:=([Ch_RParityFlag,Ch_RWParityFlag,Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  675. R_SUBFLAGAUXILIARY:
  676. Result:=([Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  677. R_SUBFLAGZERO:
  678. Result:=([Ch_RZeroFlag,Ch_RWZeroFlag,Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  679. R_SUBFLAGSIGN:
  680. Result:=([Ch_RSignFlag,Ch_RWSignFlag,Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  681. R_SUBFLAGOVERFLOW:
  682. Result:=([Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  683. R_SUBFLAGINTERRUPT:
  684. Result:=([Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  685. R_SUBFLAGDIRECTION:
  686. Result:=([Ch_RDirFlag,Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  687. else
  688. ;
  689. end;
  690. if result then
  691. exit;
  692. end
  693. else if (getregtype(reg)=R_FPUREGISTER) and (Ch_FPU in insprop[taicpu(p1).opcode].Ch) then
  694. exit(true);
  695. Result:=inherited RegInInstruction(Reg, p1);
  696. end;
  697. function TX86AsmOptimizer.RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean;
  698. begin
  699. Result := False;
  700. if p1.typ <> ait_instruction then
  701. exit;
  702. with insprop[taicpu(p1).opcode] do
  703. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  704. begin
  705. case getsubreg(reg) of
  706. R_SUBW,R_SUBD,R_SUBQ:
  707. Result :=
  708. [Ch_WCarryFlag,Ch_WParityFlag,Ch_WAuxiliaryFlag,Ch_WZeroFlag,Ch_WSignFlag,Ch_WOverflowFlag,
  709. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  710. Ch_W0DirFlag,Ch_W1DirFlag,Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  711. R_SUBFLAGCARRY:
  712. Result:=[Ch_WCarryFlag,Ch_RWCarryFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  713. R_SUBFLAGPARITY:
  714. Result:=[Ch_WParityFlag,Ch_RWParityFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  715. R_SUBFLAGAUXILIARY:
  716. Result:=[Ch_WAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  717. R_SUBFLAGZERO:
  718. Result:=[Ch_WZeroFlag,Ch_RWZeroFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  719. R_SUBFLAGSIGN:
  720. Result:=[Ch_WSignFlag,Ch_RWSignFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  721. R_SUBFLAGOVERFLOW:
  722. Result:=[Ch_WOverflowFlag,Ch_RWOverflowFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  723. R_SUBFLAGINTERRUPT:
  724. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  725. R_SUBFLAGDIRECTION:
  726. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  727. else
  728. internalerror(2017042602);
  729. end;
  730. exit;
  731. end;
  732. case taicpu(p1).opcode of
  733. A_CALL:
  734. { We could potentially set Result to False if the register in
  735. question is non-volatile for the subroutine's calling convention,
  736. but this would require detecting the calling convention in use and
  737. also assuming that the routine doesn't contain malformed assembly
  738. language, for example... so it could only be done under -O4 as it
  739. would be considered a side-effect. [Kit] }
  740. Result := True;
  741. A_MOVSD:
  742. { special handling for SSE MOVSD }
  743. if (taicpu(p1).ops>0) then
  744. begin
  745. if taicpu(p1).ops<>2 then
  746. internalerror(2017042703);
  747. Result := (taicpu(p1).oper[1]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[1]^);
  748. end;
  749. { VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  750. so fix it here (FK)
  751. }
  752. A_VMOVSS,
  753. A_VMOVSD:
  754. begin
  755. Result := (taicpu(p1).ops=3) and (taicpu(p1).oper[2]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[2]^);
  756. exit;
  757. end;
  758. A_IMUL:
  759. Result := (taicpu(p1).oper[taicpu(p1).ops-1]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[taicpu(p1).ops-1]^);
  760. else
  761. ;
  762. end;
  763. if Result then
  764. exit;
  765. with insprop[taicpu(p1).opcode] do
  766. begin
  767. if getregtype(reg)=R_INTREGISTER then
  768. begin
  769. case getsupreg(reg) of
  770. RS_EAX:
  771. if [Ch_WEAX,Ch_RWEAX,Ch_MEAX]*Ch<>[] then
  772. begin
  773. Result := True;
  774. exit
  775. end;
  776. RS_ECX:
  777. if [Ch_WECX,Ch_RWECX,Ch_MECX]*Ch<>[] then
  778. begin
  779. Result := True;
  780. exit
  781. end;
  782. RS_EDX:
  783. if [Ch_WEDX,Ch_RWEDX,Ch_MEDX]*Ch<>[] then
  784. begin
  785. Result := True;
  786. exit
  787. end;
  788. RS_EBX:
  789. if [Ch_WEBX,Ch_RWEBX,Ch_MEBX]*Ch<>[] then
  790. begin
  791. Result := True;
  792. exit
  793. end;
  794. RS_ESP:
  795. if [Ch_WESP,Ch_RWESP,Ch_MESP]*Ch<>[] then
  796. begin
  797. Result := True;
  798. exit
  799. end;
  800. RS_EBP:
  801. if [Ch_WEBP,Ch_RWEBP,Ch_MEBP]*Ch<>[] then
  802. begin
  803. Result := True;
  804. exit
  805. end;
  806. RS_ESI:
  807. if [Ch_WESI,Ch_RWESI,Ch_MESI]*Ch<>[] then
  808. begin
  809. Result := True;
  810. exit
  811. end;
  812. RS_EDI:
  813. if [Ch_WEDI,Ch_RWEDI,Ch_MEDI]*Ch<>[] then
  814. begin
  815. Result := True;
  816. exit
  817. end;
  818. end;
  819. end;
  820. if ([CH_RWOP1,CH_WOP1,CH_MOP1]*Ch<>[]) and reginop(reg,taicpu(p1).oper[0]^) then
  821. begin
  822. Result := true;
  823. exit
  824. end;
  825. if ([Ch_RWOP2,Ch_WOP2,Ch_MOP2]*Ch<>[]) and reginop(reg,taicpu(p1).oper[1]^) then
  826. begin
  827. Result := true;
  828. exit
  829. end;
  830. if ([Ch_RWOP3,Ch_WOP3,Ch_MOP3]*Ch<>[]) and reginop(reg,taicpu(p1).oper[2]^) then
  831. begin
  832. Result := true;
  833. exit
  834. end;
  835. if ([Ch_RWOP4,Ch_WOP4,Ch_MOP4]*Ch<>[]) and reginop(reg,taicpu(p1).oper[3]^) then
  836. begin
  837. Result := true;
  838. exit
  839. end;
  840. end;
  841. end;
  842. {$ifdef DEBUG_AOPTCPU}
  843. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);
  844. begin
  845. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  846. end;
  847. function debug_tostr(i: tcgint): string; inline;
  848. begin
  849. Result := tostr(i);
  850. end;
  851. function debug_regname(r: TRegister): string; inline;
  852. begin
  853. Result := '%' + std_regname(r);
  854. end;
  855. { Debug output function - creates a string representation of an operator }
  856. function debug_operstr(oper: TOper): string;
  857. begin
  858. case oper.typ of
  859. top_const:
  860. Result := '$' + debug_tostr(oper.val);
  861. top_reg:
  862. Result := debug_regname(oper.reg);
  863. top_ref:
  864. begin
  865. if oper.ref^.offset <> 0 then
  866. Result := debug_tostr(oper.ref^.offset) + '('
  867. else
  868. Result := '(';
  869. if (oper.ref^.base <> NR_INVALID) and (oper.ref^.base <> NR_NO) then
  870. begin
  871. Result := Result + debug_regname(oper.ref^.base);
  872. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  873. Result := Result + ',' + debug_regname(oper.ref^.index);
  874. end
  875. else
  876. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  877. Result := Result + debug_regname(oper.ref^.index);
  878. if (oper.ref^.scalefactor > 1) then
  879. Result := Result + ',' + debug_tostr(oper.ref^.scalefactor) + ')'
  880. else
  881. Result := Result + ')';
  882. end;
  883. else
  884. Result := '[UNKNOWN]';
  885. end;
  886. end;
  887. function debug_op2str(opcode: tasmop): string; inline;
  888. begin
  889. Result := std_op2str[opcode];
  890. end;
  891. function debug_opsize2str(opsize: topsize): string; inline;
  892. begin
  893. Result := gas_opsize2str[opsize];
  894. end;
  895. {$else DEBUG_AOPTCPU}
  896. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  897. begin
  898. end;
  899. function debug_tostr(i: tcgint): string; inline;
  900. begin
  901. Result := '';
  902. end;
  903. function debug_regname(r: TRegister): string; inline;
  904. begin
  905. Result := '';
  906. end;
  907. function debug_operstr(oper: TOper): string; inline;
  908. begin
  909. Result := '';
  910. end;
  911. function debug_op2str(opcode: tasmop): string; inline;
  912. begin
  913. Result := '';
  914. end;
  915. function debug_opsize2str(opsize: topsize): string; inline;
  916. begin
  917. Result := '';
  918. end;
  919. {$endif DEBUG_AOPTCPU}
  920. class function TX86AsmOptimizer.IsMOVZXAcceptable: Boolean; inline;
  921. begin
  922. {$ifdef x86_64}
  923. { Always fine on x86-64 }
  924. Result := True;
  925. {$else x86_64}
  926. Result :=
  927. {$ifdef i8086}
  928. (current_settings.cputype >= cpu_386) and
  929. {$endif i8086}
  930. (
  931. { Always accept if optimising for size }
  932. (cs_opt_size in current_settings.optimizerswitches) or
  933. { From the Pentium II onwards, MOVZX only takes 1 cycle. [Kit] }
  934. (current_settings.optimizecputype >= cpu_Pentium2)
  935. );
  936. {$endif x86_64}
  937. end;
  938. { Attempts to allocate a volatile integer register for use between p and hp,
  939. using AUsedRegs for the current register usage information. Returns NR_NO
  940. if no free register could be found }
  941. function TX86AsmOptimizer.GetIntRegisterBetween(RegSize: TSubRegister; var AUsedRegs: TAllUsedRegs; p, hp: tai): TRegister;
  942. var
  943. RegSet: TCPURegisterSet;
  944. CurrentSuperReg: Integer;
  945. CurrentReg: TRegister;
  946. Currentp: tai;
  947. Breakout: Boolean;
  948. begin
  949. { TODO: Currently, only the volatile registers are checked - can this be extended to use any register the procedure has preserved? }
  950. Result := NR_NO;
  951. RegSet := paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  952. for CurrentSuperReg in RegSet do
  953. begin
  954. CurrentReg := newreg(R_INTREGISTER, TSuperRegister(CurrentSuperReg), RegSize);
  955. if not AUsedRegs[R_INTREGISTER].IsUsed(CurrentReg) then
  956. begin
  957. Currentp := p;
  958. Breakout := False;
  959. while not Breakout and GetNextInstruction(Currentp, Currentp) and (Currentp <> hp) do
  960. begin
  961. case Currentp.typ of
  962. ait_instruction:
  963. begin
  964. if RegInInstruction(CurrentReg, Currentp) then
  965. begin
  966. Breakout := True;
  967. Break;
  968. end;
  969. { Cannot allocate across an unconditional jump }
  970. if is_calljmpuncondret(taicpu(Currentp).opcode) then
  971. Exit;
  972. end;
  973. ait_marker:
  974. { Don't try anything more if a marker is hit }
  975. Exit;
  976. ait_regalloc:
  977. if (tai_regalloc(Currentp).ratype <> ra_dealloc) and SuperRegistersEqual(CurrentReg, tai_regalloc(Currentp).reg) then
  978. begin
  979. Breakout := True;
  980. Break;
  981. end;
  982. else
  983. ;
  984. end;
  985. end;
  986. if Breakout then
  987. { Try the next register }
  988. Continue;
  989. { We have a free register available }
  990. Result := CurrentReg;
  991. AllocRegBetween(CurrentReg, p, hp, AUsedRegs);
  992. Exit;
  993. end;
  994. end;
  995. end;
  996. { Attempts to allocate a volatile MM register for use between p and hp,
  997. using AUsedRegs for the current register usage information. Returns NR_NO
  998. if no free register could be found }
  999. function TX86AsmOptimizer.GetMMRegisterBetween(RegSize: TSubRegister; var AUsedRegs: TAllUsedRegs; p, hp: tai): TRegister;
  1000. var
  1001. RegSet: TCPURegisterSet;
  1002. CurrentSuperReg: Integer;
  1003. CurrentReg: TRegister;
  1004. Currentp: tai;
  1005. Breakout: Boolean;
  1006. begin
  1007. { TODO: Currently, only the volatile registers are checked - can this be extended to use any register the procedure has preserved? }
  1008. Result := NR_NO;
  1009. RegSet := paramanager.get_volatile_registers_mm(current_procinfo.procdef.proccalloption);
  1010. for CurrentSuperReg in RegSet do
  1011. begin
  1012. CurrentReg := newreg(R_MMREGISTER, TSuperRegister(CurrentSuperReg), RegSize);
  1013. if not AUsedRegs[R_MMREGISTER].IsUsed(CurrentReg) then
  1014. begin
  1015. Currentp := p;
  1016. Breakout := False;
  1017. while not Breakout and GetNextInstruction(Currentp, Currentp) and (Currentp <> hp) do
  1018. begin
  1019. case Currentp.typ of
  1020. ait_instruction:
  1021. begin
  1022. if RegInInstruction(CurrentReg, Currentp) then
  1023. begin
  1024. Breakout := True;
  1025. Break;
  1026. end;
  1027. { Cannot allocate across an unconditional jump }
  1028. if is_calljmpuncondret(taicpu(Currentp).opcode) then
  1029. Exit;
  1030. end;
  1031. ait_marker:
  1032. { Don't try anything more if a marker is hit }
  1033. Exit;
  1034. ait_regalloc:
  1035. if (tai_regalloc(Currentp).ratype <> ra_dealloc) and SuperRegistersEqual(CurrentReg, tai_regalloc(Currentp).reg) then
  1036. begin
  1037. Breakout := True;
  1038. Break;
  1039. end;
  1040. else
  1041. ;
  1042. end;
  1043. end;
  1044. if Breakout then
  1045. { Try the next register }
  1046. Continue;
  1047. { We have a free register available }
  1048. Result := CurrentReg;
  1049. AllocRegBetween(CurrentReg, p, hp, AUsedRegs);
  1050. Exit;
  1051. end;
  1052. end;
  1053. end;
  1054. function TX86AsmOptimizer.Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  1055. begin
  1056. if not SuperRegistersEqual(reg1,reg2) then
  1057. exit(false);
  1058. if getregtype(reg1)<>R_INTREGISTER then
  1059. exit(true); {because SuperRegisterEqual is true}
  1060. case getsubreg(reg1) of
  1061. { A write to R_SUBL doesn't change R_SUBH and if reg2 is R_SUBW or
  1062. higher, it preserves the high bits, so the new value depends on
  1063. reg2's previous value. In other words, it is equivalent to doing:
  1064. reg2 := (reg2 and $ffffff00) or byte(reg1); }
  1065. R_SUBL:
  1066. exit(getsubreg(reg2)=R_SUBL);
  1067. { A write to R_SUBH doesn't change R_SUBL and if reg2 is R_SUBW or
  1068. higher, it actually does a:
  1069. reg2 := (reg2 and $ffff00ff) or (reg1 and $ff00); }
  1070. R_SUBH:
  1071. exit(getsubreg(reg2)=R_SUBH);
  1072. { If reg2 is R_SUBD or larger, a write to R_SUBW preserves the high 16
  1073. bits of reg2:
  1074. reg2 := (reg2 and $ffff0000) or word(reg1); }
  1075. R_SUBW:
  1076. exit(getsubreg(reg2) in [R_SUBL,R_SUBH,R_SUBW]);
  1077. { a write to R_SUBD always overwrites every other subregister,
  1078. because it clears the high 32 bits of R_SUBQ on x86_64 }
  1079. R_SUBD,
  1080. R_SUBQ:
  1081. exit(true);
  1082. else
  1083. internalerror(2017042801);
  1084. end;
  1085. end;
  1086. function TX86AsmOptimizer.Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  1087. begin
  1088. if not SuperRegistersEqual(reg1,reg2) then
  1089. exit(false);
  1090. if getregtype(reg1)<>R_INTREGISTER then
  1091. exit(true); {because SuperRegisterEqual is true}
  1092. case getsubreg(reg1) of
  1093. R_SUBL:
  1094. exit(getsubreg(reg2)<>R_SUBH);
  1095. R_SUBH:
  1096. exit(getsubreg(reg2)<>R_SUBL);
  1097. R_SUBW,
  1098. R_SUBD,
  1099. R_SUBQ:
  1100. exit(true);
  1101. else
  1102. internalerror(2017042802);
  1103. end;
  1104. end;
  1105. function TX86AsmOptimizer.PrePeepholeOptSxx(var p : tai) : boolean;
  1106. var
  1107. hp1 : tai;
  1108. l : TCGInt;
  1109. begin
  1110. result:=false;
  1111. { changes the code sequence
  1112. shr/sar const1, x
  1113. shl const2, x
  1114. to
  1115. either "sar/and", "shl/and" or just "and" depending on const1 and const2 }
  1116. if GetNextInstruction(p, hp1) and
  1117. MatchInstruction(hp1,A_SHL,[]) and
  1118. (taicpu(p).oper[0]^.typ = top_const) and
  1119. (taicpu(hp1).oper[0]^.typ = top_const) and
  1120. (taicpu(hp1).opsize = taicpu(p).opsize) and
  1121. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[1]^.typ) and
  1122. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) then
  1123. begin
  1124. if (taicpu(p).oper[0]^.val > taicpu(hp1).oper[0]^.val) and
  1125. not(cs_opt_size in current_settings.optimizerswitches) then
  1126. begin
  1127. { shr/sar const1, %reg
  1128. shl const2, %reg
  1129. with const1 > const2 }
  1130. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  1131. taicpu(hp1).opcode := A_AND;
  1132. l := (1 shl (taicpu(hp1).oper[0]^.val)) - 1;
  1133. case taicpu(p).opsize Of
  1134. S_B: taicpu(hp1).loadConst(0,l Xor $ff);
  1135. S_W: taicpu(hp1).loadConst(0,l Xor $ffff);
  1136. S_L: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffff));
  1137. S_Q: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffffffffffff));
  1138. else
  1139. Internalerror(2017050703)
  1140. end;
  1141. end
  1142. else if (taicpu(p).oper[0]^.val<taicpu(hp1).oper[0]^.val) and
  1143. not(cs_opt_size in current_settings.optimizerswitches) then
  1144. begin
  1145. { shr/sar const1, %reg
  1146. shl const2, %reg
  1147. with const1 < const2 }
  1148. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val-taicpu(p).oper[0]^.val);
  1149. taicpu(p).opcode := A_AND;
  1150. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  1151. case taicpu(p).opsize Of
  1152. S_B: taicpu(p).loadConst(0,l Xor $ff);
  1153. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  1154. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  1155. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  1156. else
  1157. Internalerror(2017050702)
  1158. end;
  1159. end
  1160. else if (taicpu(p).oper[0]^.val = taicpu(hp1).oper[0]^.val) then
  1161. begin
  1162. { shr/sar const1, %reg
  1163. shl const2, %reg
  1164. with const1 = const2 }
  1165. taicpu(p).opcode := A_AND;
  1166. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  1167. case taicpu(p).opsize Of
  1168. S_B: taicpu(p).loadConst(0,l Xor $ff);
  1169. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  1170. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  1171. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  1172. else
  1173. Internalerror(2017050701)
  1174. end;
  1175. RemoveInstruction(hp1);
  1176. end;
  1177. end;
  1178. end;
  1179. function TX86AsmOptimizer.PrePeepholeOptIMUL(var p : tai) : boolean;
  1180. var
  1181. opsize : topsize;
  1182. hp1 : tai;
  1183. tmpref : treference;
  1184. ShiftValue : Cardinal;
  1185. BaseValue : TCGInt;
  1186. begin
  1187. result:=false;
  1188. opsize:=taicpu(p).opsize;
  1189. { changes certain "imul const, %reg"'s to lea sequences }
  1190. if (MatchOpType(taicpu(p),top_const,top_reg) or
  1191. MatchOpType(taicpu(p),top_const,top_reg,top_reg)) and
  1192. (opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) then
  1193. if (taicpu(p).oper[0]^.val = 1) then
  1194. if (taicpu(p).ops = 2) then
  1195. { remove "imul $1, reg" }
  1196. begin
  1197. DebugMsg(SPeepholeOptimization + 'Imul2Nop done',p);
  1198. Result := RemoveCurrentP(p);
  1199. end
  1200. else
  1201. { change "imul $1, reg1, reg2" to "mov reg1, reg2" }
  1202. begin
  1203. hp1 := taicpu.Op_Reg_Reg(A_MOV, opsize, taicpu(p).oper[1]^.reg,taicpu(p).oper[2]^.reg);
  1204. InsertLLItem(p.previous, p.next, hp1);
  1205. DebugMsg(SPeepholeOptimization + 'Imul2Mov done',p);
  1206. p.free;
  1207. p := hp1;
  1208. end
  1209. else if ((taicpu(p).ops <= 2) or
  1210. (taicpu(p).oper[2]^.typ = Top_Reg)) and
  1211. not(cs_opt_size in current_settings.optimizerswitches) and
  1212. (not(GetNextInstruction(p, hp1)) or
  1213. not((tai(hp1).typ = ait_instruction) and
  1214. ((taicpu(hp1).opcode=A_Jcc) and
  1215. (taicpu(hp1).condition in [C_O,C_NO])))) then
  1216. begin
  1217. {
  1218. imul X, reg1, reg2 to
  1219. lea (reg1,reg1,Y), reg2
  1220. shl ZZ,reg2
  1221. imul XX, reg1 to
  1222. lea (reg1,reg1,YY), reg1
  1223. shl ZZ,reg2
  1224. This optimziation makes sense for pretty much every x86, except the VIA Nano3000: it has IMUL latency 2, lea/shl pair as well,
  1225. it does not exist as a separate optimization target in FPC though.
  1226. This optimziation can be applied as long as only two bits are set in the constant and those two bits are separated by
  1227. at most two zeros
  1228. }
  1229. reference_reset(tmpref,1,[]);
  1230. if (PopCnt(QWord(taicpu(p).oper[0]^.val))=2) and (BsrQWord(taicpu(p).oper[0]^.val)-BsfQWord(taicpu(p).oper[0]^.val)<=3) then
  1231. begin
  1232. ShiftValue:=BsfQWord(taicpu(p).oper[0]^.val);
  1233. BaseValue:=taicpu(p).oper[0]^.val shr ShiftValue;
  1234. TmpRef.base := taicpu(p).oper[1]^.reg;
  1235. TmpRef.index := taicpu(p).oper[1]^.reg;
  1236. if not(BaseValue in [3,5,9]) then
  1237. Internalerror(2018110101);
  1238. TmpRef.ScaleFactor := BaseValue-1;
  1239. if (taicpu(p).ops = 2) then
  1240. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[1]^.reg)
  1241. else
  1242. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[2]^.reg);
  1243. AsmL.InsertAfter(hp1,p);
  1244. DebugMsg(SPeepholeOptimization + 'Imul2LeaShl done',p);
  1245. taicpu(hp1).fileinfo:=taicpu(p).fileinfo;
  1246. RemoveCurrentP(p, hp1);
  1247. if ShiftValue>0 then
  1248. AsmL.InsertAfter(taicpu.op_const_reg(A_SHL, opsize, ShiftValue, taicpu(hp1).oper[1]^.reg),hp1);
  1249. end;
  1250. end;
  1251. end;
  1252. function TX86AsmOptimizer.PrePeepholeOptAND(var p : tai) : boolean;
  1253. begin
  1254. Result := False;
  1255. if MatchOperand(taicpu(p).oper[0]^, 0) and
  1256. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) then
  1257. begin
  1258. DebugMsg(SPeepholeOptimization + 'AND 0 -> MOV 0', p);
  1259. taicpu(p).opcode := A_MOV;
  1260. Result := True;
  1261. end;
  1262. end;
  1263. function TX86AsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  1264. var
  1265. p: taicpu absolute hp;
  1266. i: Integer;
  1267. begin
  1268. Result := False;
  1269. if not assigned(hp) or
  1270. (hp.typ <> ait_instruction) then
  1271. Exit;
  1272. // p := taicpu(hp);
  1273. Prefetch(insprop[p.opcode]);
  1274. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  1275. with insprop[p.opcode] do
  1276. begin
  1277. case getsubreg(reg) of
  1278. R_SUBW,R_SUBD,R_SUBQ:
  1279. Result:=
  1280. RegLoadedWithNewValue(NR_CARRYFLAG,hp) and
  1281. RegLoadedWithNewValue(NR_PARITYFLAG,hp) and
  1282. RegLoadedWithNewValue(NR_AUXILIARYFLAG,hp) and
  1283. RegLoadedWithNewValue(NR_ZEROFLAG,hp) and
  1284. RegLoadedWithNewValue(NR_SIGNFLAG,hp) and
  1285. RegLoadedWithNewValue(NR_OVERFLOWFLAG,hp);
  1286. R_SUBFLAGCARRY:
  1287. Result:=[Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag,Ch_WFlags]*Ch<>[];
  1288. R_SUBFLAGPARITY:
  1289. Result:=[Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag,Ch_WFlags]*Ch<>[];
  1290. R_SUBFLAGAUXILIARY:
  1291. Result:=[Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag,Ch_WFlags]*Ch<>[];
  1292. R_SUBFLAGZERO:
  1293. Result:=[Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag,Ch_WFlags]*Ch<>[];
  1294. R_SUBFLAGSIGN:
  1295. Result:=[Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag,Ch_WFlags]*Ch<>[];
  1296. R_SUBFLAGOVERFLOW:
  1297. Result:=[Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag,Ch_WFlags]*Ch<>[];
  1298. R_SUBFLAGINTERRUPT:
  1299. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*Ch<>[];
  1300. R_SUBFLAGDIRECTION:
  1301. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*Ch<>[];
  1302. else
  1303. begin
  1304. writeln(getsubreg(reg));
  1305. internalerror(2017050501);
  1306. end;
  1307. end;
  1308. exit;
  1309. end;
  1310. { Handle special cases first }
  1311. case p.opcode of
  1312. A_MOV, A_MOVZX, A_MOVSX, A_LEA, A_VMOVSS, A_VMOVSD, A_VMOVAPD,
  1313. A_VMOVAPS, A_VMOVQ, A_MOVSS, A_MOVSD, A_MOVQ, A_MOVAPD, A_MOVAPS:
  1314. begin
  1315. Result :=
  1316. (p.ops=2) and { A_MOVSD can have zero operands, so this check is needed }
  1317. (p.oper[1]^.typ = top_reg) and
  1318. (Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg)) and
  1319. (
  1320. (p.oper[0]^.typ = top_const) or
  1321. (
  1322. (p.oper[0]^.typ = top_reg) and
  1323. not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))
  1324. ) or (
  1325. (p.oper[0]^.typ = top_ref) and
  1326. not RegInRef(reg,p.oper[0]^.ref^)
  1327. )
  1328. );
  1329. end;
  1330. A_MUL, A_IMUL:
  1331. Result :=
  1332. (
  1333. (p.ops=3) and { IMUL only }
  1334. (Reg1WriteOverwritesReg2Entirely(p.oper[2]^.reg,reg)) and
  1335. (
  1336. (
  1337. (p.oper[1]^.typ=top_reg) and
  1338. not Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg)
  1339. ) or (
  1340. (p.oper[1]^.typ=top_ref) and
  1341. not RegInRef(reg,p.oper[1]^.ref^)
  1342. )
  1343. )
  1344. ) or (
  1345. (
  1346. (p.ops=1) and
  1347. (
  1348. (
  1349. (
  1350. (p.oper[0]^.typ=top_reg) and
  1351. not Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg)
  1352. )
  1353. ) or (
  1354. (p.oper[0]^.typ=top_ref) and
  1355. not RegInRef(reg,p.oper[0]^.ref^)
  1356. )
  1357. ) and (
  1358. (
  1359. (p.opsize=S_B) and
  1360. Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and
  1361. not Reg1ReadDependsOnReg2(NR_AL,reg)
  1362. ) or (
  1363. (p.opsize=S_W) and
  1364. Reg1WriteOverwritesReg2Entirely(NR_DX,reg)
  1365. ) or (
  1366. (p.opsize=S_L) and
  1367. Reg1WriteOverwritesReg2Entirely(NR_EDX,reg)
  1368. {$ifdef x86_64}
  1369. ) or (
  1370. (p.opsize=S_Q) and
  1371. Reg1WriteOverwritesReg2Entirely(NR_RDX,reg)
  1372. {$endif x86_64}
  1373. )
  1374. )
  1375. )
  1376. );
  1377. A_CBW:
  1378. Result := Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg));
  1379. {$ifndef x86_64}
  1380. A_LDS:
  1381. Result := (reg=NR_DS) and not(RegInRef(reg,p.oper[0]^.ref^));
  1382. A_LES:
  1383. Result := (reg=NR_ES) and not(RegInRef(reg,p.oper[0]^.ref^));
  1384. {$endif not x86_64}
  1385. A_LFS:
  1386. Result := (reg=NR_FS) and not(RegInRef(reg,p.oper[0]^.ref^));
  1387. A_LGS:
  1388. Result := (reg=NR_GS) and not(RegInRef(reg,p.oper[0]^.ref^));
  1389. A_LSS:
  1390. Result := (reg=NR_SS) and not(RegInRef(reg,p.oper[0]^.ref^));
  1391. A_LAHF{$ifndef x86_64}, A_AAM{$endif not x86_64}:
  1392. Result := Reg1WriteOverwritesReg2Entirely(NR_AH,reg);
  1393. A_LODSB:
  1394. Result := Reg1WriteOverwritesReg2Entirely(NR_AL,reg);
  1395. A_LODSW:
  1396. Result := Reg1WriteOverwritesReg2Entirely(NR_AX,reg);
  1397. {$ifdef x86_64}
  1398. A_LODSQ:
  1399. Result := Reg1WriteOverwritesReg2Entirely(NR_RAX,reg);
  1400. {$endif x86_64}
  1401. A_LODSD:
  1402. Result := Reg1WriteOverwritesReg2Entirely(NR_EAX,reg);
  1403. A_FSTSW, A_FNSTSW:
  1404. Result := (p.oper[0]^.typ=top_reg) and Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg);
  1405. else
  1406. begin
  1407. with insprop[p.opcode] do
  1408. begin
  1409. if (
  1410. { xor %reg,%reg etc. is classed as a new value }
  1411. (([Ch_NoReadIfEqualRegs]*Ch)<>[]) and
  1412. MatchOpType(p, top_reg, top_reg) and
  1413. (p.oper[0]^.reg = p.oper[1]^.reg) and
  1414. Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg)
  1415. ) then
  1416. begin
  1417. Result := True;
  1418. Exit;
  1419. end;
  1420. { Make sure the entire register is overwritten }
  1421. if (getregtype(reg) = R_INTREGISTER) then
  1422. begin
  1423. if (p.ops > 0) then
  1424. begin
  1425. if RegInOp(reg, p.oper[0]^) then
  1426. begin
  1427. if (p.oper[0]^.typ = top_ref) then
  1428. begin
  1429. if RegInRef(reg, p.oper[0]^.ref^) then
  1430. begin
  1431. Result := False;
  1432. Exit;
  1433. end;
  1434. end
  1435. else if (p.oper[0]^.typ = top_reg) then
  1436. begin
  1437. if ([Ch_ROp1, Ch_RWOp1, Ch_MOp1]*Ch<>[]) then
  1438. begin
  1439. Result := False;
  1440. Exit;
  1441. end
  1442. else if ([Ch_WOp1]*Ch<>[]) then
  1443. begin
  1444. if Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg, reg) then
  1445. Result := True
  1446. else
  1447. begin
  1448. Result := False;
  1449. Exit;
  1450. end;
  1451. end;
  1452. end;
  1453. end;
  1454. if (p.ops > 1) then
  1455. begin
  1456. if RegInOp(reg, p.oper[1]^) then
  1457. begin
  1458. if (p.oper[1]^.typ = top_ref) then
  1459. begin
  1460. if RegInRef(reg, p.oper[1]^.ref^) then
  1461. begin
  1462. Result := False;
  1463. Exit;
  1464. end;
  1465. end
  1466. else if (p.oper[1]^.typ = top_reg) then
  1467. begin
  1468. if ([Ch_ROp2, Ch_RWOp2, Ch_MOp2]*Ch<>[]) then
  1469. begin
  1470. Result := False;
  1471. Exit;
  1472. end
  1473. else if ([Ch_WOp2]*Ch<>[]) then
  1474. begin
  1475. if Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg, reg) then
  1476. Result := True
  1477. else
  1478. begin
  1479. Result := False;
  1480. Exit;
  1481. end;
  1482. end;
  1483. end;
  1484. end;
  1485. if (p.ops > 2) then
  1486. begin
  1487. if RegInOp(reg, p.oper[2]^) then
  1488. begin
  1489. if (p.oper[2]^.typ = top_ref) then
  1490. begin
  1491. if RegInRef(reg, p.oper[2]^.ref^) then
  1492. begin
  1493. Result := False;
  1494. Exit;
  1495. end;
  1496. end
  1497. else if (p.oper[2]^.typ = top_reg) then
  1498. begin
  1499. if ([Ch_ROp3, Ch_RWOp3, Ch_MOp3]*Ch<>[]) then
  1500. begin
  1501. Result := False;
  1502. Exit;
  1503. end
  1504. else if ([Ch_WOp3]*Ch<>[]) then
  1505. begin
  1506. if Reg1WriteOverwritesReg2Entirely(p.oper[2]^.reg, reg) then
  1507. Result := True
  1508. else
  1509. begin
  1510. Result := False;
  1511. Exit;
  1512. end;
  1513. end;
  1514. end;
  1515. end;
  1516. if (p.ops > 3) and RegInOp(reg, p.oper[3]^) then
  1517. begin
  1518. if (p.oper[3]^.typ = top_ref) then
  1519. begin
  1520. if RegInRef(reg, p.oper[3]^.ref^) then
  1521. begin
  1522. Result := False;
  1523. Exit;
  1524. end;
  1525. end
  1526. else if (p.oper[3]^.typ = top_reg) then
  1527. begin
  1528. if ([Ch_ROp4, Ch_RWOp4, Ch_MOp4]*Ch<>[]) then
  1529. begin
  1530. Result := False;
  1531. Exit;
  1532. end
  1533. else if ([Ch_WOp4]*Ch<>[]) then
  1534. begin
  1535. if Reg1WriteOverwritesReg2Entirely(p.oper[3]^.reg, reg) then
  1536. Result := True
  1537. else
  1538. begin
  1539. Result := False;
  1540. Exit;
  1541. end;
  1542. end;
  1543. end;
  1544. end;
  1545. end;
  1546. end;
  1547. end;
  1548. { Don't do these ones first in case an input operand is equal to an explicit output registers }
  1549. case getsupreg(reg) of
  1550. RS_EAX:
  1551. if ([Ch_WEAX{$ifdef x86_64},Ch_WRAX{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EAX, reg) then
  1552. begin
  1553. Result := True;
  1554. Exit;
  1555. end;
  1556. RS_ECX:
  1557. if ([Ch_WECX{$ifdef x86_64},Ch_WRCX{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_ECX, reg) then
  1558. begin
  1559. Result := True;
  1560. Exit;
  1561. end;
  1562. RS_EDX:
  1563. if ([Ch_REDX{$ifdef x86_64},Ch_WRDX{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EDX, reg) then
  1564. begin
  1565. Result := True;
  1566. Exit;
  1567. end;
  1568. RS_EBX:
  1569. if ([Ch_WEBX{$ifdef x86_64},Ch_WRBX{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EBX, reg) then
  1570. begin
  1571. Result := True;
  1572. Exit;
  1573. end;
  1574. RS_ESP:
  1575. if ([Ch_WESP{$ifdef x86_64},Ch_WRSP{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_ESP, reg) then
  1576. begin
  1577. Result := True;
  1578. Exit;
  1579. end;
  1580. RS_EBP:
  1581. if ([Ch_WEBP{$ifdef x86_64},Ch_WRBP{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EBP, reg) then
  1582. begin
  1583. Result := True;
  1584. Exit;
  1585. end;
  1586. RS_ESI:
  1587. if ([Ch_WESI{$ifdef x86_64},Ch_WRSI{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_ESI, reg) then
  1588. begin
  1589. Result := True;
  1590. Exit;
  1591. end;
  1592. RS_EDI:
  1593. if ([Ch_WEDI{$ifdef x86_64},Ch_WRDI{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EDI, reg) then
  1594. begin
  1595. Result := True;
  1596. Exit;
  1597. end;
  1598. else
  1599. ;
  1600. end;
  1601. end;
  1602. end;
  1603. end;
  1604. end;
  1605. end;
  1606. class function TX86AsmOptimizer.IsExitCode(p : tai) : boolean;
  1607. var
  1608. hp2,hp3 : tai;
  1609. begin
  1610. { some x86-64 issue a NOP before the real exit code }
  1611. if MatchInstruction(p,A_NOP,[]) then
  1612. GetNextInstruction(p,p);
  1613. result:=assigned(p) and (p.typ=ait_instruction) and
  1614. ((taicpu(p).opcode = A_RET) or
  1615. ((taicpu(p).opcode=A_LEAVE) and
  1616. GetNextInstruction(p,hp2) and
  1617. MatchInstruction(hp2,A_RET,[S_NO])
  1618. ) or
  1619. (((taicpu(p).opcode=A_LEA) and
  1620. MatchOpType(taicpu(p),top_ref,top_reg) and
  1621. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  1622. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  1623. ) and
  1624. GetNextInstruction(p,hp2) and
  1625. MatchInstruction(hp2,A_RET,[S_NO])
  1626. ) or
  1627. ((((taicpu(p).opcode=A_MOV) and
  1628. MatchOpType(taicpu(p),top_reg,top_reg) and
  1629. (taicpu(p).oper[0]^.reg=current_procinfo.framepointer) and
  1630. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)) or
  1631. ((taicpu(p).opcode=A_LEA) and
  1632. MatchOpType(taicpu(p),top_ref,top_reg) and
  1633. (taicpu(p).oper[0]^.ref^.base=current_procinfo.framepointer) and
  1634. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  1635. )
  1636. ) and
  1637. GetNextInstruction(p,hp2) and
  1638. MatchInstruction(hp2,A_POP,[reg2opsize(current_procinfo.framepointer)]) and
  1639. MatchOpType(taicpu(hp2),top_reg) and
  1640. (taicpu(hp2).oper[0]^.reg=current_procinfo.framepointer) and
  1641. GetNextInstruction(hp2,hp3) and
  1642. MatchInstruction(hp3,A_RET,[S_NO])
  1643. )
  1644. );
  1645. end;
  1646. class function TX86AsmOptimizer.isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  1647. begin
  1648. isFoldableArithOp := False;
  1649. case hp1.opcode of
  1650. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  1651. isFoldableArithOp :=
  1652. ((taicpu(hp1).oper[0]^.typ = top_const) or
  1653. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  1654. (taicpu(hp1).oper[0]^.reg <> reg))) and
  1655. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1656. (taicpu(hp1).oper[1]^.reg = reg);
  1657. A_INC,A_DEC,A_NEG,A_NOT:
  1658. isFoldableArithOp :=
  1659. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1660. (taicpu(hp1).oper[0]^.reg = reg);
  1661. else
  1662. ;
  1663. end;
  1664. end;
  1665. procedure TX86AsmOptimizer.RemoveLastDeallocForFuncRes(p: tai);
  1666. procedure DoRemoveLastDeallocForFuncRes( supreg: tsuperregister);
  1667. var
  1668. hp2: tai;
  1669. begin
  1670. hp2 := p;
  1671. repeat
  1672. hp2 := tai(hp2.previous);
  1673. if assigned(hp2) and
  1674. (hp2.typ = ait_regalloc) and
  1675. (tai_regalloc(hp2).ratype=ra_dealloc) and
  1676. (getregtype(tai_regalloc(hp2).reg) = R_INTREGISTER) and
  1677. (getsupreg(tai_regalloc(hp2).reg) = supreg) then
  1678. begin
  1679. RemoveInstruction(hp2);
  1680. break;
  1681. end;
  1682. until not(assigned(hp2)) or regInInstruction(newreg(R_INTREGISTER,supreg,R_SUBWHOLE),hp2);
  1683. end;
  1684. begin
  1685. case current_procinfo.procdef.returndef.typ of
  1686. arraydef,recorddef,pointerdef,
  1687. stringdef,enumdef,procdef,objectdef,errordef,
  1688. filedef,setdef,procvardef,
  1689. classrefdef,forwarddef:
  1690. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1691. orddef:
  1692. if current_procinfo.procdef.returndef.size <> 0 then
  1693. begin
  1694. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1695. { for int64/qword }
  1696. if current_procinfo.procdef.returndef.size = 8 then
  1697. DoRemoveLastDeallocForFuncRes(RS_EDX);
  1698. end;
  1699. else
  1700. ;
  1701. end;
  1702. end;
  1703. function TX86AsmOptimizer.OptPass1_V_MOVAP(var p : tai) : boolean;
  1704. var
  1705. hp1,hp2 : tai;
  1706. begin
  1707. result:=false;
  1708. if MatchOpType(taicpu(p),top_reg,top_reg) then
  1709. begin
  1710. { vmova* reg1,reg1
  1711. =>
  1712. <nop> }
  1713. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  1714. begin
  1715. RemoveCurrentP(p);
  1716. result:=true;
  1717. exit;
  1718. end
  1719. else if GetNextInstruction(p,hp1) then
  1720. begin
  1721. if MatchInstruction(hp1,[taicpu(p).opcode],[S_NO]) and
  1722. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1723. begin
  1724. { vmova* reg1,reg2
  1725. vmova* reg2,reg3
  1726. dealloc reg2
  1727. =>
  1728. vmova* reg1,reg3 }
  1729. TransferUsedRegs(TmpUsedRegs);
  1730. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1731. if MatchOpType(taicpu(hp1),top_reg,top_reg) and
  1732. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1733. begin
  1734. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 1',p);
  1735. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1736. RemoveInstruction(hp1);
  1737. result:=true;
  1738. exit;
  1739. end
  1740. { special case:
  1741. vmova* reg1,<op>
  1742. vmova* <op>,reg1
  1743. =>
  1744. vmova* reg1,<op> }
  1745. else if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  1746. ((taicpu(p).oper[0]^.typ<>top_ref) or
  1747. (not(vol_read in taicpu(p).oper[0]^.ref^.volatility))
  1748. ) then
  1749. begin
  1750. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 2',p);
  1751. RemoveInstruction(hp1);
  1752. result:=true;
  1753. exit;
  1754. end
  1755. end
  1756. else if ((MatchInstruction(p,[A_MOVAPS,A_VMOVAPS],[S_NO]) and
  1757. MatchInstruction(hp1,[A_MOVSS,A_VMOVSS],[S_NO])) or
  1758. ((MatchInstruction(p,[A_MOVAPD,A_VMOVAPD],[S_NO]) and
  1759. MatchInstruction(hp1,[A_MOVSD,A_VMOVSD],[S_NO])))
  1760. ) and
  1761. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1762. begin
  1763. { vmova* reg1,reg2
  1764. vmovs* reg2,<op>
  1765. dealloc reg2
  1766. =>
  1767. vmovs* reg1,reg3 }
  1768. TransferUsedRegs(TmpUsedRegs);
  1769. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1770. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1771. begin
  1772. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVS*2(V)MOVS* 1',p);
  1773. taicpu(p).opcode:=taicpu(hp1).opcode;
  1774. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1775. RemoveInstruction(hp1);
  1776. result:=true;
  1777. exit;
  1778. end
  1779. end;
  1780. end;
  1781. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) then
  1782. begin
  1783. if MatchInstruction(hp1,[A_VFMADDPD,
  1784. A_VFMADD132PD,
  1785. A_VFMADD132PS,
  1786. A_VFMADD132SD,
  1787. A_VFMADD132SS,
  1788. A_VFMADD213PD,
  1789. A_VFMADD213PS,
  1790. A_VFMADD213SD,
  1791. A_VFMADD213SS,
  1792. A_VFMADD231PD,
  1793. A_VFMADD231PS,
  1794. A_VFMADD231SD,
  1795. A_VFMADD231SS,
  1796. A_VFMADDSUB132PD,
  1797. A_VFMADDSUB132PS,
  1798. A_VFMADDSUB213PD,
  1799. A_VFMADDSUB213PS,
  1800. A_VFMADDSUB231PD,
  1801. A_VFMADDSUB231PS,
  1802. A_VFMSUB132PD,
  1803. A_VFMSUB132PS,
  1804. A_VFMSUB132SD,
  1805. A_VFMSUB132SS,
  1806. A_VFMSUB213PD,
  1807. A_VFMSUB213PS,
  1808. A_VFMSUB213SD,
  1809. A_VFMSUB213SS,
  1810. A_VFMSUB231PD,
  1811. A_VFMSUB231PS,
  1812. A_VFMSUB231SD,
  1813. A_VFMSUB231SS,
  1814. A_VFMSUBADD132PD,
  1815. A_VFMSUBADD132PS,
  1816. A_VFMSUBADD213PD,
  1817. A_VFMSUBADD213PS,
  1818. A_VFMSUBADD231PD,
  1819. A_VFMSUBADD231PS,
  1820. A_VFNMADD132PD,
  1821. A_VFNMADD132PS,
  1822. A_VFNMADD132SD,
  1823. A_VFNMADD132SS,
  1824. A_VFNMADD213PD,
  1825. A_VFNMADD213PS,
  1826. A_VFNMADD213SD,
  1827. A_VFNMADD213SS,
  1828. A_VFNMADD231PD,
  1829. A_VFNMADD231PS,
  1830. A_VFNMADD231SD,
  1831. A_VFNMADD231SS,
  1832. A_VFNMSUB132PD,
  1833. A_VFNMSUB132PS,
  1834. A_VFNMSUB132SD,
  1835. A_VFNMSUB132SS,
  1836. A_VFNMSUB213PD,
  1837. A_VFNMSUB213PS,
  1838. A_VFNMSUB213SD,
  1839. A_VFNMSUB213SS,
  1840. A_VFNMSUB231PD,
  1841. A_VFNMSUB231PS,
  1842. A_VFNMSUB231SD,
  1843. A_VFNMSUB231SS],[S_NO]) and
  1844. { we mix single and double opperations here because we assume that the compiler
  1845. generates vmovapd only after double operations and vmovaps only after single operations }
  1846. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[2]^) and
  1847. GetNextInstruction(hp1,hp2) and
  1848. MatchInstruction(hp2,[A_VMOVAPD,A_VMOVAPS,A_MOVAPD,A_MOVAPS],[S_NO]) and
  1849. MatchOperand(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) then
  1850. begin
  1851. TransferUsedRegs(TmpUsedRegs);
  1852. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1853. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1854. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1855. begin
  1856. taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
  1857. RemoveCurrentP(p, hp1); // <-- Is this actually safe? hp1 is not necessarily the next instruction. [Kit]
  1858. RemoveInstruction(hp2);
  1859. end;
  1860. end
  1861. else if (hp1.typ = ait_instruction) and
  1862. GetNextInstruction(hp1, hp2) and
  1863. MatchInstruction(hp2,taicpu(p).opcode,[]) and
  1864. OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  1865. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  1866. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  1867. (((taicpu(p).opcode=A_MOVAPS) and
  1868. ((taicpu(hp1).opcode=A_ADDSS) or (taicpu(hp1).opcode=A_SUBSS) or
  1869. (taicpu(hp1).opcode=A_MULSS) or (taicpu(hp1).opcode=A_DIVSS))) or
  1870. ((taicpu(p).opcode=A_MOVAPD) and
  1871. ((taicpu(hp1).opcode=A_ADDSD) or (taicpu(hp1).opcode=A_SUBSD) or
  1872. (taicpu(hp1).opcode=A_MULSD) or (taicpu(hp1).opcode=A_DIVSD)))
  1873. ) then
  1874. { change
  1875. movapX reg,reg2
  1876. addsX/subsX/... reg3, reg2
  1877. movapX reg2,reg
  1878. to
  1879. addsX/subsX/... reg3,reg
  1880. }
  1881. begin
  1882. TransferUsedRegs(TmpUsedRegs);
  1883. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1884. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1885. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1886. begin
  1887. DebugMsg(SPeepholeOptimization + 'MovapXOpMovapX2Op ('+
  1888. debug_op2str(taicpu(p).opcode)+' '+
  1889. debug_op2str(taicpu(hp1).opcode)+' '+
  1890. debug_op2str(taicpu(hp2).opcode)+') done',p);
  1891. { we cannot eliminate the first move if
  1892. the operations uses the same register for source and dest }
  1893. if not(OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp1).oper[0]^)) then
  1894. RemoveCurrentP(p, nil);
  1895. p:=hp1;
  1896. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1897. RemoveInstruction(hp2);
  1898. result:=true;
  1899. end;
  1900. end;
  1901. end;
  1902. end;
  1903. end;
  1904. function TX86AsmOptimizer.OptPass1VOP(var p : tai) : boolean;
  1905. var
  1906. hp1 : tai;
  1907. begin
  1908. result:=false;
  1909. { replace
  1910. V<Op>X %mreg1,%mreg2,%mreg3
  1911. VMovX %mreg3,%mreg4
  1912. dealloc %mreg3
  1913. by
  1914. V<Op>X %mreg1,%mreg2,%mreg4
  1915. ?
  1916. }
  1917. if GetNextInstruction(p,hp1) and
  1918. { we mix single and double operations here because we assume that the compiler
  1919. generates vmovapd only after double operations and vmovaps only after single operations }
  1920. MatchInstruction(hp1,A_VMOVAPD,A_VMOVAPS,[S_NO]) and
  1921. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  1922. (taicpu(hp1).oper[1]^.typ=top_reg) then
  1923. begin
  1924. TransferUsedRegs(TmpUsedRegs);
  1925. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1926. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  1927. begin
  1928. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  1929. DebugMsg(SPeepholeOptimization + 'VOpVmov2VOp done',p);
  1930. RemoveInstruction(hp1);
  1931. result:=true;
  1932. end;
  1933. end;
  1934. end;
  1935. { Replaces all references to AOldReg in a memory reference to ANewReg }
  1936. class function TX86AsmOptimizer.ReplaceRegisterInRef(var ref: TReference; const AOldReg, ANewReg: TRegister): Boolean;
  1937. begin
  1938. Result := False;
  1939. { For safety reasons, only check for exact register matches }
  1940. { Check base register }
  1941. if (ref.base = AOldReg) then
  1942. begin
  1943. ref.base := ANewReg;
  1944. Result := True;
  1945. end;
  1946. { Check index register }
  1947. if (ref.index = AOldReg) then
  1948. begin
  1949. ref.index := ANewReg;
  1950. Result := True;
  1951. end;
  1952. end;
  1953. { Replaces all references to AOldReg in an operand to ANewReg }
  1954. class function TX86AsmOptimizer.ReplaceRegisterInOper(const p: taicpu; const OperIdx: Integer; const AOldReg, ANewReg: TRegister): Boolean;
  1955. var
  1956. OldSupReg, NewSupReg: TSuperRegister;
  1957. OldSubReg, NewSubReg: TSubRegister;
  1958. OldRegType: TRegisterType;
  1959. ThisOper: POper;
  1960. begin
  1961. ThisOper := p.oper[OperIdx]; { Faster to access overall }
  1962. Result := False;
  1963. if (AOldReg = NR_NO) or (ANewReg = NR_NO) then
  1964. InternalError(2020011801);
  1965. OldSupReg := getsupreg(AOldReg);
  1966. OldSubReg := getsubreg(AOldReg);
  1967. OldRegType := getregtype(AOldReg);
  1968. NewSupReg := getsupreg(ANewReg);
  1969. NewSubReg := getsubreg(ANewReg);
  1970. if OldRegType <> getregtype(ANewReg) then
  1971. InternalError(2020011802);
  1972. if OldSubReg <> NewSubReg then
  1973. InternalError(2020011803);
  1974. case ThisOper^.typ of
  1975. top_reg:
  1976. if (
  1977. (ThisOper^.reg = AOldReg) or
  1978. (
  1979. (OldRegType = R_INTREGISTER) and
  1980. (getsupreg(ThisOper^.reg) = OldSupReg) and
  1981. (getregtype(ThisOper^.reg) = R_INTREGISTER) and
  1982. (
  1983. (getsubreg(ThisOper^.reg) <= OldSubReg)
  1984. {$ifndef x86_64}
  1985. and (
  1986. { Under i386 and i8086, ESI, EDI, EBP and ESP
  1987. don't have an 8-bit representation }
  1988. (getsubreg(ThisOper^.reg) >= R_SUBW) or
  1989. not (NewSupReg in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  1990. )
  1991. {$endif x86_64}
  1992. )
  1993. )
  1994. ) then
  1995. begin
  1996. ThisOper^.reg := newreg(getregtype(ANewReg), NewSupReg, getsubreg(p.oper[OperIdx]^.reg));
  1997. Result := True;
  1998. end;
  1999. top_ref:
  2000. if ReplaceRegisterInRef(ThisOper^.ref^, AOldReg, ANewReg) then
  2001. Result := True;
  2002. else
  2003. ;
  2004. end;
  2005. end;
  2006. { Replaces all references to AOldReg in an instruction to ANewReg }
  2007. function TX86AsmOptimizer.ReplaceRegisterInInstruction(const p: taicpu; const AOldReg, ANewReg: TRegister): Boolean;
  2008. const
  2009. ReadFlag: array[0..3] of TInsChange = (Ch_Rop1, Ch_Rop2, Ch_Rop3, Ch_Rop4);
  2010. var
  2011. OperIdx: Integer;
  2012. begin
  2013. Result := False;
  2014. for OperIdx := 0 to p.ops - 1 do
  2015. if (ReadFlag[OperIdx] in InsProp[p.Opcode].Ch) and
  2016. { The shift and rotate instructions can only use CL }
  2017. not (
  2018. (OperIdx = 0) and
  2019. { This second condition just helps to avoid unnecessarily
  2020. calling MatchInstruction for 10 different opcodes }
  2021. (p.oper[0]^.reg = NR_CL) and
  2022. MatchInstruction(p, [A_RCL, A_RCR, A_ROL, A_ROR, A_SAL, A_SAR, A_SHL, A_SHLD, A_SHR, A_SHRD], [])
  2023. ) then
  2024. Result := ReplaceRegisterInOper(p, OperIdx, AOldReg, ANewReg) or Result;
  2025. end;
  2026. class function TX86AsmOptimizer.IsRefSafe(const ref: PReference): Boolean; inline;
  2027. begin
  2028. Result :=
  2029. (ref^.index = NR_NO) and
  2030. (
  2031. {$ifdef x86_64}
  2032. (
  2033. (ref^.base = NR_RIP) and
  2034. (ref^.refaddr in [addr_pic, addr_pic_no_got])
  2035. ) or
  2036. {$endif x86_64}
  2037. (ref^.base = NR_STACK_POINTER_REG) or
  2038. (ref^.base = current_procinfo.framepointer)
  2039. );
  2040. end;
  2041. function TX86AsmOptimizer.ConvertLEA(const p: taicpu): Boolean;
  2042. var
  2043. l: asizeint;
  2044. begin
  2045. Result := False;
  2046. { Should have been checked previously }
  2047. if p.opcode <> A_LEA then
  2048. InternalError(2020072501);
  2049. { do not mess with the stack point as adjusting it by lea is recommend, except if we optimize for size }
  2050. if (p.oper[1]^.reg=NR_STACK_POINTER_REG) and
  2051. not(cs_opt_size in current_settings.optimizerswitches) then
  2052. exit;
  2053. with p.oper[0]^.ref^ do
  2054. begin
  2055. if (base <> p.oper[1]^.reg) or
  2056. (index <> NR_NO) or
  2057. assigned(symbol) then
  2058. exit;
  2059. l:=offset;
  2060. if (l=1) and UseIncDec then
  2061. begin
  2062. p.opcode:=A_INC;
  2063. p.loadreg(0,p.oper[1]^.reg);
  2064. p.ops:=1;
  2065. DebugMsg(SPeepholeOptimization + 'Lea2Inc done',p);
  2066. end
  2067. else if (l=-1) and UseIncDec then
  2068. begin
  2069. p.opcode:=A_DEC;
  2070. p.loadreg(0,p.oper[1]^.reg);
  2071. p.ops:=1;
  2072. DebugMsg(SPeepholeOptimization + 'Lea2Dec done',p);
  2073. end
  2074. else
  2075. begin
  2076. if (l<0) and (l<>-2147483648) then
  2077. begin
  2078. p.opcode:=A_SUB;
  2079. p.loadConst(0,-l);
  2080. DebugMsg(SPeepholeOptimization + 'Lea2Sub done',p);
  2081. end
  2082. else
  2083. begin
  2084. p.opcode:=A_ADD;
  2085. p.loadConst(0,l);
  2086. DebugMsg(SPeepholeOptimization + 'Lea2Add done',p);
  2087. end;
  2088. end;
  2089. end;
  2090. Result := True;
  2091. end;
  2092. function TX86AsmOptimizer.DeepMOVOpt(const p_mov: taicpu; const hp: taicpu): Boolean;
  2093. var
  2094. CurrentReg, ReplaceReg: TRegister;
  2095. begin
  2096. Result := False;
  2097. ReplaceReg := taicpu(p_mov).oper[0]^.reg;
  2098. CurrentReg := taicpu(p_mov).oper[1]^.reg;
  2099. case hp.opcode of
  2100. A_FSTSW, A_FNSTSW,
  2101. A_IN, A_INS, A_OUT, A_OUTS,
  2102. A_CMPS, A_LODS, A_MOVS, A_SCAS, A_STOS:
  2103. { These routines have explicit operands, but they are restricted in
  2104. what they can be (e.g. IN and OUT can only read from AL, AX or
  2105. EAX. }
  2106. Exit;
  2107. A_IMUL:
  2108. begin
  2109. { The 1-operand version writes to implicit registers
  2110. The 2-operand version reads from the first operator, and reads
  2111. from and writes to the second (equivalent to Ch_ROp1, ChRWOp2).
  2112. the 3-operand version reads from a register that it doesn't write to
  2113. }
  2114. case hp.ops of
  2115. 1:
  2116. if (
  2117. (
  2118. (hp.opsize = S_B) and (getsupreg(CurrentReg) <> RS_EAX)
  2119. ) or
  2120. not (getsupreg(CurrentReg) in [RS_EAX, RS_EDX])
  2121. ) and ReplaceRegisterInOper(hp, 0, CurrentReg, ReplaceReg) then
  2122. begin
  2123. Result := True;
  2124. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 1)', hp);
  2125. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  2126. end;
  2127. 2:
  2128. { Only modify the first parameter }
  2129. if ReplaceRegisterInOper(hp, 0, CurrentReg, ReplaceReg) then
  2130. begin
  2131. Result := True;
  2132. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 2)', hp);
  2133. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  2134. end;
  2135. 3:
  2136. { Only modify the second parameter }
  2137. if ReplaceRegisterInOper(hp, 1, CurrentReg, ReplaceReg) then
  2138. begin
  2139. Result := True;
  2140. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 3)', hp);
  2141. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  2142. end;
  2143. else
  2144. InternalError(2020012901);
  2145. end;
  2146. end;
  2147. else
  2148. if (hp.ops > 0) and
  2149. ReplaceRegisterInInstruction(hp, CurrentReg, ReplaceReg) then
  2150. begin
  2151. Result := True;
  2152. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovXXX2MovXXX)', hp);
  2153. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  2154. end;
  2155. end;
  2156. end;
  2157. function TX86AsmOptimizer.OptPass1MOV(var p : tai) : boolean;
  2158. var
  2159. hp1, hp2, hp3: tai;
  2160. DoOptimisation, TempBool: Boolean;
  2161. procedure convert_mov_value(signed_movop: tasmop; max_value: tcgint); inline;
  2162. begin
  2163. if taicpu(hp1).opcode = signed_movop then
  2164. begin
  2165. if taicpu(p).oper[0]^.val > max_value shr 1 then
  2166. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val - max_value - 1 { Convert to signed }
  2167. end
  2168. else
  2169. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and max_value; { Trim to unsigned }
  2170. end;
  2171. var
  2172. GetNextInstruction_p, TempRegUsed, CrossJump: Boolean;
  2173. PreMessage, RegName1, RegName2, InputVal, MaskNum: string;
  2174. NewSize: topsize;
  2175. CurrentReg, ActiveReg: TRegister;
  2176. SourceRef, TargetRef: TReference;
  2177. MovAligned, MovUnaligned: TAsmOp;
  2178. begin
  2179. Result:=false;
  2180. GetNextInstruction_p:=GetNextInstruction(p, hp1);
  2181. { remove mov reg1,reg1? }
  2182. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^)
  2183. then
  2184. begin
  2185. DebugMsg(SPeepholeOptimization + 'Mov2Nop 1 done',p);
  2186. { take care of the register (de)allocs following p }
  2187. RemoveCurrentP(p, hp1);
  2188. Result:=true;
  2189. exit;
  2190. end;
  2191. { All the next optimisations require a next instruction }
  2192. if not GetNextInstruction_p or (hp1.typ <> ait_instruction) then
  2193. Exit;
  2194. { Look for:
  2195. mov %reg1,%reg2
  2196. ??? %reg2,r/m
  2197. Change to:
  2198. mov %reg1,%reg2
  2199. ??? %reg1,r/m
  2200. }
  2201. if MatchOpType(taicpu(p), top_reg, top_reg) then
  2202. begin
  2203. CurrentReg := taicpu(p).oper[1]^.reg;
  2204. if RegReadByInstruction(CurrentReg, hp1) and
  2205. DeepMOVOpt(taicpu(p), taicpu(hp1)) then
  2206. begin
  2207. TransferUsedRegs(TmpUsedRegs);
  2208. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  2209. if not RegUsedAfterInstruction(CurrentReg, hp1, TmpUsedRegs) and
  2210. { Just in case something didn't get modified (e.g. an
  2211. implicit register) }
  2212. not RegReadByInstruction(CurrentReg, hp1) then
  2213. begin
  2214. { We can remove the original MOV }
  2215. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3 done',p);
  2216. RemoveCurrentp(p, hp1);
  2217. { UsedRegs got updated by RemoveCurrentp }
  2218. Result := True;
  2219. Exit;
  2220. end;
  2221. { If we know a MOV instruction has become a null operation, we might as well
  2222. get rid of it now to save time. }
  2223. if (taicpu(hp1).opcode = A_MOV) and
  2224. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2225. SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[0]^.reg) and
  2226. { Just being a register is enough to confirm it's a null operation }
  2227. (taicpu(hp1).oper[0]^.typ = top_reg) then
  2228. begin
  2229. Result := True;
  2230. { Speed-up to reduce a pipeline stall... if we had something like...
  2231. movl %eax,%edx
  2232. movw %dx,%ax
  2233. ... the second instruction would change to movw %ax,%ax, but
  2234. given that it is now %ax that's active rather than %eax,
  2235. penalties might occur due to a partial register write, so instead,
  2236. change it to a MOVZX instruction when optimising for speed.
  2237. }
  2238. if not (cs_opt_size in current_settings.optimizerswitches) and
  2239. IsMOVZXAcceptable and
  2240. (taicpu(hp1).opsize < taicpu(p).opsize)
  2241. {$ifdef x86_64}
  2242. { operations already implicitly set the upper 64 bits to zero }
  2243. and not ((taicpu(hp1).opsize = S_L) and (taicpu(p).opsize = S_Q))
  2244. {$endif x86_64}
  2245. then
  2246. begin
  2247. CurrentReg := taicpu(hp1).oper[1]^.reg;
  2248. DebugMsg(SPeepholeOptimization + 'Zero-extension to minimise pipeline stall (Mov2Movz)',hp1);
  2249. case taicpu(p).opsize of
  2250. S_W:
  2251. if taicpu(hp1).opsize = S_B then
  2252. taicpu(hp1).opsize := S_BL
  2253. else
  2254. InternalError(2020012911);
  2255. S_L{$ifdef x86_64}, S_Q{$endif x86_64}:
  2256. case taicpu(hp1).opsize of
  2257. S_B:
  2258. taicpu(hp1).opsize := S_BL;
  2259. S_W:
  2260. taicpu(hp1).opsize := S_WL;
  2261. else
  2262. InternalError(2020012912);
  2263. end;
  2264. else
  2265. InternalError(2020012910);
  2266. end;
  2267. taicpu(hp1).opcode := A_MOVZX;
  2268. taicpu(hp1).oper[1]^.reg := newreg(getregtype(CurrentReg), getsupreg(CurrentReg), R_SUBD)
  2269. end
  2270. else
  2271. begin
  2272. GetNextInstruction_p := GetNextInstruction(hp1, hp2);
  2273. DebugMsg(SPeepholeOptimization + 'Mov2Nop 4 done',hp1);
  2274. RemoveInstruction(hp1);
  2275. { The instruction after what was hp1 is now the immediate next instruction,
  2276. so we can continue to make optimisations if it's present }
  2277. if not GetNextInstruction_p or (hp2.typ <> ait_instruction) then
  2278. Exit;
  2279. hp1 := hp2;
  2280. end;
  2281. end;
  2282. end;
  2283. end;
  2284. { Depending on the DeepMOVOpt above, it may turn out that hp1 completely
  2285. overwrites the original destination register. e.g.
  2286. movl ###,%reg2d
  2287. movslq ###,%reg2q (### doesn't have to be the same as the first one)
  2288. In this case, we can remove the MOV (Go to "Mov2Nop 5" below)
  2289. }
  2290. if (taicpu(p).oper[1]^.typ = top_reg) and
  2291. MatchInstruction(hp1, [A_LEA, A_MOV, A_MOVSX, A_MOVZX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}], []) and
  2292. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2293. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[1]^.reg) then
  2294. begin
  2295. if RegInOp(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[0]^) then
  2296. begin
  2297. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  2298. case taicpu(p).oper[0]^.typ of
  2299. top_const:
  2300. { We have something like:
  2301. movb $x, %regb
  2302. movzbl %regb,%regd
  2303. Change to:
  2304. movl $x, %regd
  2305. }
  2306. begin
  2307. case taicpu(hp1).opsize of
  2308. S_BW:
  2309. begin
  2310. convert_mov_value(A_MOVSX, $FF);
  2311. setsubreg(taicpu(p).oper[1]^.reg, R_SUBW);
  2312. taicpu(p).opsize := S_W;
  2313. end;
  2314. S_BL:
  2315. begin
  2316. convert_mov_value(A_MOVSX, $FF);
  2317. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  2318. taicpu(p).opsize := S_L;
  2319. end;
  2320. S_WL:
  2321. begin
  2322. convert_mov_value(A_MOVSX, $FFFF);
  2323. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  2324. taicpu(p).opsize := S_L;
  2325. end;
  2326. {$ifdef x86_64}
  2327. S_BQ:
  2328. begin
  2329. convert_mov_value(A_MOVSX, $FF);
  2330. setsubreg(taicpu(p).oper[1]^.reg, R_SUBQ);
  2331. taicpu(p).opsize := S_Q;
  2332. end;
  2333. S_WQ:
  2334. begin
  2335. convert_mov_value(A_MOVSX, $FFFF);
  2336. setsubreg(taicpu(p).oper[1]^.reg, R_SUBQ);
  2337. taicpu(p).opsize := S_Q;
  2338. end;
  2339. S_LQ:
  2340. begin
  2341. convert_mov_value(A_MOVSXD, $FFFFFFFF); { Note it's MOVSXD, not MOVSX }
  2342. setsubreg(taicpu(p).oper[1]^.reg, R_SUBQ);
  2343. taicpu(p).opsize := S_Q;
  2344. end;
  2345. {$endif x86_64}
  2346. else
  2347. { If hp1 was a MOV instruction, it should have been
  2348. optimised already }
  2349. InternalError(2020021001);
  2350. end;
  2351. DebugMsg(SPeepholeOptimization + 'MovMovXX2MovXX 2 done',p);
  2352. RemoveInstruction(hp1);
  2353. Result := True;
  2354. Exit;
  2355. end;
  2356. top_ref:
  2357. { We have something like:
  2358. movb mem, %regb
  2359. movzbl %regb,%regd
  2360. Change to:
  2361. movzbl mem, %regd
  2362. }
  2363. if (taicpu(p).oper[0]^.ref^.refaddr<>addr_full) and (IsMOVZXAcceptable or (taicpu(hp1).opcode<>A_MOVZX)) then
  2364. begin
  2365. DebugMsg(SPeepholeOptimization + 'MovMovXX2MovXX 1 done',p);
  2366. taicpu(hp1).loadref(0,taicpu(p).oper[0]^.ref^);
  2367. RemoveCurrentP(p, hp1);
  2368. Result:=True;
  2369. Exit;
  2370. end;
  2371. else
  2372. if (taicpu(hp1).opcode <> A_MOV) and (taicpu(hp1).opcode <> A_LEA) then
  2373. { Just to make a saving, since there are no more optimisations with MOVZX and MOVSX/D }
  2374. Exit;
  2375. end;
  2376. end
  2377. { The RegInOp check makes sure that movl r/m,%reg1l; movzbl (%reg1l),%reg1l"
  2378. and "movl r/m,%reg1; leal $1(%reg1,%reg2),%reg1" etc. are not incorrectly
  2379. optimised }
  2380. else
  2381. begin
  2382. DebugMsg(SPeepholeOptimization + 'Mov2Nop 5 done',p);
  2383. RemoveCurrentP(p, hp1);
  2384. Result := True;
  2385. Exit;
  2386. end;
  2387. end;
  2388. if (taicpu(hp1).opcode = A_AND) and
  2389. (taicpu(p).oper[1]^.typ = top_reg) and
  2390. MatchOpType(taicpu(hp1),top_const,top_reg) then
  2391. begin
  2392. if MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) then
  2393. begin
  2394. case taicpu(p).opsize of
  2395. S_L:
  2396. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  2397. begin
  2398. { Optimize out:
  2399. mov x, %reg
  2400. and ffffffffh, %reg
  2401. }
  2402. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 1 done',p);
  2403. RemoveInstruction(hp1);
  2404. Result:=true;
  2405. exit;
  2406. end;
  2407. S_Q: { TODO: Confirm if this is even possible }
  2408. if (taicpu(hp1).oper[0]^.val = $ffffffffffffffff) then
  2409. begin
  2410. { Optimize out:
  2411. mov x, %reg
  2412. and ffffffffffffffffh, %reg
  2413. }
  2414. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 2 done',p);
  2415. RemoveInstruction(hp1);
  2416. Result:=true;
  2417. exit;
  2418. end;
  2419. else
  2420. ;
  2421. end;
  2422. if ((taicpu(p).oper[0]^.typ=top_reg) or
  2423. ((taicpu(p).oper[0]^.typ=top_ref) and (taicpu(p).oper[0]^.ref^.refaddr<>addr_full))) and
  2424. GetNextInstruction(hp1,hp2) and
  2425. MatchInstruction(hp2,A_TEST,[taicpu(p).opsize]) and
  2426. MatchOperand(taicpu(hp1).oper[1]^,taicpu(hp2).oper[1]^) and
  2427. (MatchOperand(taicpu(hp2).oper[0]^,taicpu(hp2).oper[1]^) or
  2428. MatchOperand(taicpu(hp2).oper[0]^,-1)) and
  2429. GetNextInstruction(hp2,hp3) and
  2430. MatchInstruction(hp3,A_Jcc,A_Setcc,[]) and
  2431. (taicpu(hp3).condition in [C_E,C_NE]) then
  2432. begin
  2433. TransferUsedRegs(TmpUsedRegs);
  2434. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  2435. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  2436. if not(RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp2, TmpUsedRegs)) then
  2437. begin
  2438. DebugMsg(SPeepholeOptimization + 'MovAndTest2Test done',p);
  2439. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  2440. taicpu(hp1).opcode:=A_TEST;
  2441. RemoveInstruction(hp2);
  2442. RemoveCurrentP(p, hp1);
  2443. Result:=true;
  2444. exit;
  2445. end;
  2446. end;
  2447. end
  2448. else if IsMOVZXAcceptable and
  2449. (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(hp1).oper[1]^.typ = top_reg) and
  2450. (taicpu(p).oper[0]^.typ <> top_const) and { MOVZX only supports registers and memory, not immediates (use MOV for that!) }
  2451. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2452. then
  2453. begin
  2454. InputVal := debug_operstr(taicpu(p).oper[0]^);
  2455. MaskNum := debug_tostr(taicpu(hp1).oper[0]^.val);
  2456. case taicpu(p).opsize of
  2457. S_B:
  2458. if (taicpu(hp1).oper[0]^.val = $ff) then
  2459. begin
  2460. { Convert:
  2461. movb x, %regl movb x, %regl
  2462. andw ffh, %regw andl ffh, %regd
  2463. To:
  2464. movzbw x, %regd movzbl x, %regd
  2465. (Identical registers, just different sizes)
  2466. }
  2467. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 8-bit register name }
  2468. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 16/32-bit register name }
  2469. case taicpu(hp1).opsize of
  2470. S_W: NewSize := S_BW;
  2471. S_L: NewSize := S_BL;
  2472. {$ifdef x86_64}
  2473. S_Q: NewSize := S_BQ;
  2474. {$endif x86_64}
  2475. else
  2476. InternalError(2018011510);
  2477. end;
  2478. end
  2479. else
  2480. NewSize := S_NO;
  2481. S_W:
  2482. if (taicpu(hp1).oper[0]^.val = $ffff) then
  2483. begin
  2484. { Convert:
  2485. movw x, %regw
  2486. andl ffffh, %regd
  2487. To:
  2488. movzwl x, %regd
  2489. (Identical registers, just different sizes)
  2490. }
  2491. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 16-bit register name }
  2492. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 32-bit register name }
  2493. case taicpu(hp1).opsize of
  2494. S_L: NewSize := S_WL;
  2495. {$ifdef x86_64}
  2496. S_Q: NewSize := S_WQ;
  2497. {$endif x86_64}
  2498. else
  2499. InternalError(2018011511);
  2500. end;
  2501. end
  2502. else
  2503. NewSize := S_NO;
  2504. else
  2505. NewSize := S_NO;
  2506. end;
  2507. if NewSize <> S_NO then
  2508. begin
  2509. PreMessage := 'mov' + debug_opsize2str(taicpu(p).opsize) + ' ' + InputVal + ',' + RegName1;
  2510. { The actual optimization }
  2511. taicpu(p).opcode := A_MOVZX;
  2512. taicpu(p).changeopsize(NewSize);
  2513. taicpu(p).oper[1]^ := taicpu(hp1).oper[1]^;
  2514. { Safeguard if "and" is followed by a conditional command }
  2515. TransferUsedRegs(TmpUsedRegs);
  2516. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  2517. if (RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  2518. begin
  2519. { At this point, the "and" command is effectively equivalent to
  2520. "test %reg,%reg". This will be handled separately by the
  2521. Peephole Optimizer. [Kit] }
  2522. DebugMsg(SPeepholeOptimization + PreMessage +
  2523. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  2524. end
  2525. else
  2526. begin
  2527. DebugMsg(SPeepholeOptimization + PreMessage + '; and' + debug_opsize2str(taicpu(hp1).opsize) + ' $' + MaskNum + ',' + RegName2 +
  2528. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  2529. RemoveInstruction(hp1);
  2530. end;
  2531. Result := True;
  2532. Exit;
  2533. end;
  2534. end;
  2535. end;
  2536. if (taicpu(hp1).opcode = A_OR) and
  2537. (taicpu(p).oper[1]^.typ = top_reg) and
  2538. MatchOperand(taicpu(p).oper[0]^, 0) and
  2539. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) then
  2540. begin
  2541. { mov 0, %reg
  2542. or ###,%reg
  2543. Change to (only if the flags are not used):
  2544. mov ###,%reg
  2545. }
  2546. TransferUsedRegs(TmpUsedRegs);
  2547. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  2548. DoOptimisation := True;
  2549. { Even if the flags are used, we might be able to do the optimisation
  2550. if the conditions are predictable }
  2551. if RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) then
  2552. begin
  2553. { Only perform if ### = %reg (the same register) or equal to 0,
  2554. so %reg is guaranteed to still have a value of zero }
  2555. if MatchOperand(taicpu(hp1).oper[0]^, 0) or
  2556. MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^.reg) then
  2557. begin
  2558. hp2 := hp1;
  2559. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  2560. while RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) and
  2561. GetNextInstruction(hp2, hp3) do
  2562. begin
  2563. { Don't continue modifying if the flags state is getting changed }
  2564. if RegModifiedByInstruction(NR_DEFAULTFLAGS, hp3) then
  2565. Break;
  2566. UpdateUsedRegs(TmpUsedRegs, tai(hp3.Next));
  2567. if MatchInstruction(hp3, A_Jcc, A_SETcc, A_CMOVcc, []) then
  2568. begin
  2569. if condition_in(C_E, taicpu(hp3).condition) or (taicpu(hp3).condition in [C_NC, C_NS, C_NO]) then
  2570. begin
  2571. { Condition is always true }
  2572. case taicpu(hp3).opcode of
  2573. A_Jcc:
  2574. begin
  2575. DebugMsg(SPeepholeOptimization + 'Condition is always true (jump made unconditional)', hp3);
  2576. { Check for jump shortcuts before we destroy the condition }
  2577. DoJumpOptimizations(hp3, TempBool);
  2578. MakeUnconditional(taicpu(hp3));
  2579. Result := True;
  2580. end;
  2581. A_CMOVcc:
  2582. begin
  2583. DebugMsg(SPeepholeOptimization + 'Condition is always true (CMOVcc -> MOV)', hp3);
  2584. taicpu(hp3).opcode := A_MOV;
  2585. taicpu(hp3).condition := C_None;
  2586. Result := True;
  2587. end;
  2588. A_SETcc:
  2589. begin
  2590. DebugMsg(SPeepholeOptimization + 'Condition is always true (changed to MOV 1)', hp3);
  2591. { Convert "set(c) %reg" instruction to "movb 1,%reg" }
  2592. taicpu(hp3).opcode := A_MOV;
  2593. taicpu(hp3).ops := 2;
  2594. taicpu(hp3).condition := C_None;
  2595. taicpu(hp3).opsize := S_B;
  2596. taicpu(hp3).loadreg(1,taicpu(hp3).oper[0]^.reg);
  2597. taicpu(hp3).loadconst(0, 1);
  2598. Result := True;
  2599. end;
  2600. else
  2601. InternalError(2021090701);
  2602. end;
  2603. end
  2604. else if (taicpu(hp3).condition in [C_A, C_B, C_C, C_G, C_L, C_NE, C_NZ, C_O, C_S]) then
  2605. begin
  2606. { Condition is always false }
  2607. case taicpu(hp3).opcode of
  2608. A_Jcc:
  2609. begin
  2610. DebugMsg(SPeepholeOptimization + 'Condition is always false (jump removed)', hp3);
  2611. TAsmLabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  2612. RemoveInstruction(hp3);
  2613. Result := True;
  2614. { Since hp3 was deleted, hp2 must not be updated }
  2615. Continue;
  2616. end;
  2617. A_CMOVcc:
  2618. begin
  2619. DebugMsg(SPeepholeOptimization + 'Condition is always false (conditional load removed)', hp3);
  2620. RemoveInstruction(hp3);
  2621. Result := True;
  2622. { Since hp3 was deleted, hp2 must not be updated }
  2623. Continue;
  2624. end;
  2625. A_SETcc:
  2626. begin
  2627. DebugMsg(SPeepholeOptimization + 'Condition is always false (changed to MOV 0)', hp3);
  2628. { Convert "set(c) %reg" instruction to "movb 0,%reg" }
  2629. taicpu(hp3).opcode := A_MOV;
  2630. taicpu(hp3).ops := 2;
  2631. taicpu(hp3).condition := C_None;
  2632. taicpu(hp3).opsize := S_B;
  2633. taicpu(hp3).loadreg(1,taicpu(hp3).oper[0]^.reg);
  2634. taicpu(hp3).loadconst(0, 0);
  2635. Result := True;
  2636. end;
  2637. else
  2638. InternalError(2021090702);
  2639. end;
  2640. end
  2641. else
  2642. { Uncertain what to do - don't optimise (although optimise other conditional statements if present) }
  2643. DoOptimisation := False;
  2644. end;
  2645. hp2 := hp3;
  2646. end;
  2647. { Flags are still in use - don't optimise }
  2648. if DoOptimisation and RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) then
  2649. DoOptimisation := False;
  2650. end
  2651. else
  2652. DoOptimisation := False;
  2653. end;
  2654. if DoOptimisation then
  2655. begin
  2656. {$ifdef x86_64}
  2657. { OR only supports 32-bit sign-extended constants for 64-bit
  2658. instructions, so compensate for this if the constant is
  2659. encoded as a value greater than or equal to 2^31 }
  2660. if (taicpu(hp1).opsize = S_Q) and
  2661. (taicpu(hp1).oper[0]^.typ = top_const) and
  2662. (taicpu(hp1).oper[0]^.val >= $80000000) then
  2663. taicpu(hp1).oper[0]^.val := taicpu(hp1).oper[0]^.val or $FFFFFFFF00000000;
  2664. {$endif x86_64}
  2665. DebugMsg(SPeepholeOptimization + 'MOV 0 / OR -> MOV', p);
  2666. taicpu(hp1).opcode := A_MOV;
  2667. RemoveCurrentP(p, hp1);
  2668. Result := True;
  2669. Exit;
  2670. end;
  2671. end;
  2672. { Next instruction is also a MOV ? }
  2673. if MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) then
  2674. begin
  2675. if (taicpu(p).oper[1]^.typ = top_reg) and
  2676. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  2677. begin
  2678. CurrentReg := taicpu(p).oper[1]^.reg;
  2679. TransferUsedRegs(TmpUsedRegs);
  2680. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  2681. { we have
  2682. mov x, %treg
  2683. mov %treg, y
  2684. }
  2685. if not(RegInOp(CurrentReg, taicpu(hp1).oper[1]^)) then
  2686. if not(RegUsedAfterInstruction(CurrentReg, hp1, TmpUsedRegs)) then
  2687. { we've got
  2688. mov x, %treg
  2689. mov %treg, y
  2690. with %treg is not used after }
  2691. case taicpu(p).oper[0]^.typ Of
  2692. { top_reg is covered by DeepMOVOpt }
  2693. top_const:
  2694. begin
  2695. { change
  2696. mov const, %treg
  2697. mov %treg, y
  2698. to
  2699. mov const, y
  2700. }
  2701. if (taicpu(hp1).oper[1]^.typ=top_reg) or
  2702. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  2703. begin
  2704. if taicpu(hp1).oper[1]^.typ=top_reg then
  2705. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  2706. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  2707. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 5 done',p);
  2708. RemoveInstruction(hp1);
  2709. Result:=true;
  2710. Exit;
  2711. end;
  2712. end;
  2713. top_ref:
  2714. case taicpu(hp1).oper[1]^.typ of
  2715. top_reg:
  2716. begin
  2717. { change
  2718. mov mem, %treg
  2719. mov %treg, %reg
  2720. to
  2721. mov mem, %reg"
  2722. }
  2723. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  2724. taicpu(p).loadreg(1, taicpu(hp1).oper[1]^.reg);
  2725. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3 done',p);
  2726. RemoveInstruction(hp1);
  2727. Result:=true;
  2728. Exit;
  2729. end;
  2730. top_ref:
  2731. begin
  2732. {$ifdef x86_64}
  2733. { Look for the following to simplify:
  2734. mov x(mem1), %reg
  2735. mov %reg, y(mem2)
  2736. mov x+8(mem1), %reg
  2737. mov %reg, y+8(mem2)
  2738. Change to:
  2739. movdqu x(mem1), %xmmreg
  2740. movdqu %xmmreg, y(mem2)
  2741. }
  2742. SourceRef := taicpu(p).oper[0]^.ref^;
  2743. TargetRef := taicpu(hp1).oper[1]^.ref^;
  2744. if (taicpu(p).opsize = S_Q) and
  2745. GetNextInstruction(hp1, hp2) and
  2746. MatchInstruction(hp2, A_MOV, [taicpu(p).opsize]) and
  2747. MatchOpType(taicpu(hp2), top_ref, top_reg) then
  2748. begin
  2749. { Delay calling GetNextInstruction(hp2, hp3) for as long as possible }
  2750. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  2751. Inc(SourceRef.offset, 8);
  2752. if UseAVX then
  2753. begin
  2754. MovAligned := A_VMOVDQA;
  2755. MovUnaligned := A_VMOVDQU;
  2756. end
  2757. else
  2758. begin
  2759. MovAligned := A_MOVDQA;
  2760. MovUnaligned := A_MOVDQU;
  2761. end;
  2762. if RefsEqual(SourceRef, taicpu(hp2).oper[0]^.ref^) then
  2763. begin
  2764. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  2765. Inc(TargetRef.offset, 8);
  2766. if GetNextInstruction(hp2, hp3) and
  2767. MatchInstruction(hp3, A_MOV, [taicpu(p).opsize]) and
  2768. MatchOpType(taicpu(hp3), top_reg, top_ref) and
  2769. (taicpu(hp2).oper[1]^.reg = taicpu(hp3).oper[0]^.reg) and
  2770. RefsEqual(TargetRef, taicpu(hp3).oper[1]^.ref^) and
  2771. not RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp3, TmpUsedRegs) then
  2772. begin
  2773. CurrentReg := GetMMRegisterBetween(R_SUBMMX, UsedRegs, p, hp3);
  2774. if CurrentReg <> NR_NO then
  2775. begin
  2776. { Remember that the offsets are 8 ahead }
  2777. if ((SourceRef.offset mod 16) = 8) and
  2778. (
  2779. { Base pointer is always aligned (stack pointer won't be if there's no stack frame) }
  2780. (SourceRef.base = current_procinfo.framepointer) or
  2781. ((SourceRef.alignment >= 16) and ((SourceRef.alignment mod 16) = 0))
  2782. ) then
  2783. taicpu(p).opcode := MovAligned
  2784. else
  2785. taicpu(p).opcode := MovUnaligned;
  2786. taicpu(p).opsize := S_XMM;
  2787. taicpu(p).oper[1]^.reg := CurrentReg;
  2788. if ((TargetRef.offset mod 16) = 8) and
  2789. (
  2790. { Base pointer is always aligned (stack pointer won't be if there's no stack frame) }
  2791. (TargetRef.base = current_procinfo.framepointer) or
  2792. ((TargetRef.alignment >= 16) and ((TargetRef.alignment mod 16) = 0))
  2793. ) then
  2794. taicpu(hp1).opcode := MovAligned
  2795. else
  2796. taicpu(hp1).opcode := MovUnaligned;
  2797. taicpu(hp1).opsize := S_XMM;
  2798. taicpu(hp1).oper[0]^.reg := CurrentReg;
  2799. DebugMsg(SPeepholeOptimization + 'Used ' + debug_regname(CurrentReg) + ' to merge a pair of memory moves (MovMovMovMov2MovdqMovdq 1)', p);
  2800. RemoveInstruction(hp2);
  2801. RemoveInstruction(hp3);
  2802. Result := True;
  2803. Exit;
  2804. end;
  2805. end;
  2806. end
  2807. else
  2808. begin
  2809. { See if the next references are 8 less rather than 8 greater }
  2810. Dec(SourceRef.offset, 16); { -8 the other way }
  2811. if RefsEqual(SourceRef, taicpu(hp2).oper[0]^.ref^) then
  2812. begin
  2813. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  2814. Dec(TargetRef.offset, 8); { Only 8, not 16, as it wasn't incremented unlike SourceRef }
  2815. if GetNextInstruction(hp2, hp3) and
  2816. MatchInstruction(hp3, A_MOV, [taicpu(p).opsize]) and
  2817. MatchOpType(taicpu(hp3), top_reg, top_ref) and
  2818. (taicpu(hp2).oper[1]^.reg = taicpu(hp3).oper[0]^.reg) and
  2819. RefsEqual(TargetRef, taicpu(hp3).oper[1]^.ref^) and
  2820. not RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp3, TmpUsedRegs) then
  2821. begin
  2822. CurrentReg := GetMMRegisterBetween(R_SUBMMX, UsedRegs, p, hp3);
  2823. if CurrentReg <> NR_NO then
  2824. begin
  2825. { hp2 and hp3 are the starting offsets, so mod = 0 this time }
  2826. if ((SourceRef.offset mod 16) = 0) and
  2827. (
  2828. { Base pointer is always aligned (stack pointer won't be if there's no stack frame) }
  2829. (SourceRef.base = current_procinfo.framepointer) or
  2830. ((SourceRef.alignment >= 16) and ((SourceRef.alignment mod 16) = 0))
  2831. ) then
  2832. taicpu(hp2).opcode := MovAligned
  2833. else
  2834. taicpu(hp2).opcode := MovUnaligned;
  2835. taicpu(hp2).opsize := S_XMM;
  2836. taicpu(hp2).oper[1]^.reg := CurrentReg;
  2837. if ((TargetRef.offset mod 16) = 0) and
  2838. (
  2839. { Base pointer is always aligned (stack pointer won't be if there's no stack frame) }
  2840. (TargetRef.base = current_procinfo.framepointer) or
  2841. ((TargetRef.alignment >= 16) and ((TargetRef.alignment mod 16) = 0))
  2842. ) then
  2843. taicpu(hp3).opcode := MovAligned
  2844. else
  2845. taicpu(hp3).opcode := MovUnaligned;
  2846. taicpu(hp3).opsize := S_XMM;
  2847. taicpu(hp3).oper[0]^.reg := CurrentReg;
  2848. DebugMsg(SPeepholeOptimization + 'Used ' + debug_regname(CurrentReg) + ' to merge a pair of memory moves (MovMovMovMov2MovdqMovdq 2)', p);
  2849. RemoveInstruction(hp1);
  2850. RemoveCurrentP(p, hp2);
  2851. Result := True;
  2852. Exit;
  2853. end;
  2854. end;
  2855. end;
  2856. end;
  2857. end;
  2858. {$endif x86_64}
  2859. end;
  2860. else
  2861. { The write target should be a reg or a ref }
  2862. InternalError(2021091601);
  2863. end;
  2864. else
  2865. ;
  2866. end
  2867. else
  2868. { %treg is used afterwards, but all eventualities
  2869. other than the first MOV instruction being a constant
  2870. are covered by DeepMOVOpt, so only check for that }
  2871. if (taicpu(p).oper[0]^.typ = top_const) and
  2872. (
  2873. { For MOV operations, a size saving is only made if the register/const is byte-sized }
  2874. not (cs_opt_size in current_settings.optimizerswitches) or
  2875. (taicpu(hp1).opsize = S_B)
  2876. ) and
  2877. (
  2878. (taicpu(hp1).oper[1]^.typ = top_reg) or
  2879. ((taicpu(p).oper[0]^.val >= low(longint)) and (taicpu(p).oper[0]^.val <= high(longint)))
  2880. ) then
  2881. begin
  2882. DebugMsg(SPeepholeOptimization + debug_operstr(taicpu(hp1).oper[0]^) + ' = $' + debug_tostr(taicpu(p).oper[0]^.val) + '; changed to minimise pipeline stall (MovMov2Mov 6b)',hp1);
  2883. taicpu(hp1).loadconst(0, taicpu(p).oper[0]^.val);
  2884. end;
  2885. end;
  2886. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  2887. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  2888. { mov reg1, mem1 or mov mem1, reg1
  2889. mov mem2, reg2 mov reg2, mem2}
  2890. begin
  2891. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  2892. { mov reg1, mem1 or mov mem1, reg1
  2893. mov mem2, reg1 mov reg2, mem1}
  2894. begin
  2895. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2896. { Removes the second statement from
  2897. mov reg1, mem1/reg2
  2898. mov mem1/reg2, reg1 }
  2899. begin
  2900. if taicpu(p).oper[0]^.typ=top_reg then
  2901. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2902. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 1',p);
  2903. RemoveInstruction(hp1);
  2904. Result:=true;
  2905. exit;
  2906. end
  2907. else
  2908. begin
  2909. TransferUsedRegs(TmpUsedRegs);
  2910. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2911. if (taicpu(p).oper[1]^.typ = top_ref) and
  2912. { mov reg1, mem1
  2913. mov mem2, reg1 }
  2914. (taicpu(hp1).oper[0]^.ref^.refaddr = addr_no) and
  2915. GetNextInstruction(hp1, hp2) and
  2916. MatchInstruction(hp2,A_CMP,[taicpu(p).opsize]) and
  2917. OpsEqual(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  2918. OpsEqual(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) and
  2919. not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs)) then
  2920. { change to
  2921. mov reg1, mem1 mov reg1, mem1
  2922. mov mem2, reg1 cmp reg1, mem2
  2923. cmp mem1, reg1
  2924. }
  2925. begin
  2926. RemoveInstruction(hp2);
  2927. taicpu(hp1).opcode := A_CMP;
  2928. taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
  2929. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  2930. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  2931. DebugMsg(SPeepholeOptimization + 'MovMovCmp2MovCmp done',hp1);
  2932. end;
  2933. end;
  2934. end
  2935. else if (taicpu(p).oper[1]^.typ=top_ref) and
  2936. OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2937. begin
  2938. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  2939. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  2940. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov1 done',p);
  2941. end
  2942. else
  2943. begin
  2944. TransferUsedRegs(TmpUsedRegs);
  2945. if GetNextInstruction(hp1, hp2) and
  2946. MatchOpType(taicpu(p),top_ref,top_reg) and
  2947. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2948. (taicpu(hp1).oper[1]^.typ = top_ref) and
  2949. MatchInstruction(hp2,A_MOV,[taicpu(p).opsize]) and
  2950. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  2951. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  2952. if not RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^) and
  2953. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,tmpUsedRegs)) then
  2954. { mov mem1, %reg1
  2955. mov %reg1, mem2
  2956. mov mem2, reg2
  2957. to:
  2958. mov mem1, reg2
  2959. mov reg2, mem2}
  2960. begin
  2961. AllocRegBetween(taicpu(hp2).oper[1]^.reg,p,hp2,usedregs);
  2962. DebugMsg(SPeepholeOptimization + 'MovMovMov2MovMov 1 done',p);
  2963. taicpu(p).loadoper(1,taicpu(hp2).oper[1]^);
  2964. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  2965. RemoveInstruction(hp2);
  2966. Result := True;
  2967. end
  2968. {$ifdef i386}
  2969. { this is enabled for i386 only, as the rules to create the reg sets below
  2970. are too complicated for x86-64, so this makes this code too error prone
  2971. on x86-64
  2972. }
  2973. else if (taicpu(p).oper[1]^.reg <> taicpu(hp2).oper[1]^.reg) and
  2974. not(RegInRef(taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.ref^)) and
  2975. not(RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^)) then
  2976. { mov mem1, reg1 mov mem1, reg1
  2977. mov reg1, mem2 mov reg1, mem2
  2978. mov mem2, reg2 mov mem2, reg1
  2979. to: to:
  2980. mov mem1, reg1 mov mem1, reg1
  2981. mov mem1, reg2 mov reg1, mem2
  2982. mov reg1, mem2
  2983. or (if mem1 depends on reg1
  2984. and/or if mem2 depends on reg2)
  2985. to:
  2986. mov mem1, reg1
  2987. mov reg1, mem2
  2988. mov reg1, reg2
  2989. }
  2990. begin
  2991. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  2992. taicpu(hp1).loadReg(1,taicpu(hp2).oper[1]^.reg);
  2993. taicpu(hp2).loadRef(1,taicpu(hp2).oper[0]^.ref^);
  2994. taicpu(hp2).loadReg(0,taicpu(p).oper[1]^.reg);
  2995. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  2996. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  2997. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  2998. AllocRegBetween(taicpu(p).oper[0]^.ref^.base,p,hp2,usedregs);
  2999. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and
  3000. (getsupreg(taicpu(p).oper[0]^.ref^.index) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  3001. AllocRegBetween(taicpu(p).oper[0]^.ref^.index,p,hp2,usedregs);
  3002. end
  3003. else if (taicpu(hp1).Oper[0]^.reg <> taicpu(hp2).Oper[1]^.reg) then
  3004. begin
  3005. taicpu(hp2).loadReg(0,taicpu(hp1).Oper[0]^.reg);
  3006. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  3007. end
  3008. else
  3009. begin
  3010. RemoveInstruction(hp2);
  3011. end
  3012. {$endif i386}
  3013. ;
  3014. end;
  3015. end
  3016. { movl [mem1],reg1
  3017. movl [mem1],reg2
  3018. to
  3019. movl [mem1],reg1
  3020. movl reg1,reg2
  3021. }
  3022. else if MatchOpType(taicpu(p),top_ref,top_reg) and
  3023. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  3024. (taicpu(p).opsize = taicpu(hp1).opsize) and
  3025. RefsEqual(taicpu(p).oper[0]^.ref^,taicpu(hp1).oper[0]^.ref^) and
  3026. (taicpu(p).oper[0]^.ref^.volatility=[]) and
  3027. (taicpu(hp1).oper[0]^.ref^.volatility=[]) and
  3028. not(SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^.base)) and
  3029. not(SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^.index)) then
  3030. begin
  3031. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 2',p);
  3032. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  3033. end;
  3034. { movl const1,[mem1]
  3035. movl [mem1],reg1
  3036. to
  3037. movl const1,reg1
  3038. movl reg1,[mem1]
  3039. }
  3040. if MatchOpType(Taicpu(p),top_const,top_ref) and
  3041. MatchOpType(Taicpu(hp1),top_ref,top_reg) and
  3042. (taicpu(p).opsize = taicpu(hp1).opsize) and
  3043. RefsEqual(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.ref^) and
  3044. not(RegInRef(taicpu(hp1).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^)) then
  3045. begin
  3046. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  3047. taicpu(hp1).loadReg(0,taicpu(hp1).oper[1]^.reg);
  3048. taicpu(hp1).loadRef(1,taicpu(p).oper[1]^.ref^);
  3049. taicpu(p).loadReg(1,taicpu(hp1).oper[0]^.reg);
  3050. taicpu(hp1).fileinfo := taicpu(p).fileinfo;
  3051. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 1',p);
  3052. Result:=true;
  3053. exit;
  3054. end;
  3055. { mov x,reg1; mov y,reg1 -> mov y,reg1 is handled by the Mov2Nop 5 optimisation }
  3056. end;
  3057. { search further than the next instruction for a mov (as long as it's not a jump) }
  3058. if not is_calljmpuncondret(taicpu(hp1).opcode) and
  3059. { check as much as possible before the expensive GetNextInstructionUsingRegCond call }
  3060. (taicpu(p).oper[1]^.typ = top_reg) and
  3061. (taicpu(p).oper[0]^.typ in [top_reg,top_const]) and
  3062. not RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp1) then
  3063. begin
  3064. { we work with hp2 here, so hp1 can be still used later on when
  3065. checking for GetNextInstruction_p }
  3066. hp3 := hp1;
  3067. { Initialise CrossJump (if it becomes True at any point, it will remain True) }
  3068. CrossJump := (taicpu(hp1).opcode = A_Jcc);
  3069. { Saves on a large number of dereferences }
  3070. ActiveReg := taicpu(p).oper[1]^.reg;
  3071. while GetNextInstructionUsingRegCond(hp3,hp2,ActiveReg,CrossJump) and
  3072. { GetNextInstructionUsingRegCond only searches one instruction ahead unless -O3 is specified }
  3073. (hp2.typ=ait_instruction) do
  3074. begin
  3075. case taicpu(hp2).opcode of
  3076. A_MOV:
  3077. if MatchOperand(taicpu(hp2).oper[0]^,ActiveReg) and
  3078. ((taicpu(p).oper[0]^.typ=top_const) or
  3079. ((taicpu(p).oper[0]^.typ=top_reg) and
  3080. not(RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp2))
  3081. )
  3082. ) then
  3083. begin
  3084. { we have
  3085. mov x, %treg
  3086. mov %treg, y
  3087. }
  3088. TransferUsedRegs(TmpUsedRegs);
  3089. TmpUsedRegs[R_INTREGISTER].Update(tai(p.Next));
  3090. { We don't need to call UpdateUsedRegs for every instruction between
  3091. p and hp2 because the register we're concerned about will not
  3092. become deallocated (otherwise GetNextInstructionUsingReg would
  3093. have stopped at an earlier instruction). [Kit] }
  3094. TempRegUsed :=
  3095. CrossJump { Assume the register is in use if it crossed a conditional jump } or
  3096. RegUsedAfterInstruction(ActiveReg, hp2, TmpUsedRegs) or
  3097. RegReadByInstruction(ActiveReg, hp1);
  3098. case taicpu(p).oper[0]^.typ Of
  3099. top_reg:
  3100. begin
  3101. { change
  3102. mov %reg, %treg
  3103. mov %treg, y
  3104. to
  3105. mov %reg, y
  3106. }
  3107. CurrentReg := taicpu(p).oper[0]^.reg; { Saves on a handful of pointer dereferences }
  3108. RegName1 := debug_regname(taicpu(hp2).oper[0]^.reg);
  3109. if taicpu(hp2).oper[1]^.reg = CurrentReg then
  3110. begin
  3111. { %reg = y - remove hp2 completely (doing it here instead of relying on
  3112. the "mov %reg,%reg" optimisation might cut down on a pass iteration) }
  3113. if TempRegUsed then
  3114. begin
  3115. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + RegName1 + '; removed unnecessary instruction (MovMov2MovNop 6b}',hp2);
  3116. AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
  3117. { Set the start of the next GetNextInstructionUsingRegCond search
  3118. to start at the entry right before hp2 (which is about to be removed) }
  3119. hp3 := tai(hp2.Previous);
  3120. RemoveInstruction(hp2);
  3121. { See if there's more we can optimise }
  3122. Continue;
  3123. end
  3124. else
  3125. begin
  3126. RemoveInstruction(hp2);
  3127. { We can remove the original MOV too }
  3128. DebugMsg(SPeepholeOptimization + 'MovMov2NopNop 6b done',p);
  3129. RemoveCurrentP(p, hp1);
  3130. Result:=true;
  3131. Exit;
  3132. end;
  3133. end
  3134. else
  3135. begin
  3136. AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
  3137. taicpu(hp2).loadReg(0, CurrentReg);
  3138. if TempRegUsed then
  3139. begin
  3140. { Don't remove the first instruction if the temporary register is in use }
  3141. DebugMsg(SPeepholeOptimization + RegName1 + ' = ' + debug_regname(CurrentReg) + '; changed to minimise pipeline stall (MovMov2Mov 6a}',hp2);
  3142. { No need to set Result to True. If there's another instruction later on
  3143. that can be optimised, it will be detected when the main Pass 1 loop
  3144. reaches what is now hp2 and passes it through OptPass1MOV. [Kit] };
  3145. end
  3146. else
  3147. begin
  3148. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 6 done',p);
  3149. RemoveCurrentP(p, hp1);
  3150. Result:=true;
  3151. Exit;
  3152. end;
  3153. end;
  3154. end;
  3155. top_const:
  3156. if not (cs_opt_size in current_settings.optimizerswitches) or (taicpu(hp2).opsize = S_B) then
  3157. begin
  3158. { change
  3159. mov const, %treg
  3160. mov %treg, y
  3161. to
  3162. mov const, y
  3163. }
  3164. if (taicpu(hp2).oper[1]^.typ=top_reg) or
  3165. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  3166. begin
  3167. RegName1 := debug_regname(taicpu(hp2).oper[0]^.reg);
  3168. taicpu(hp2).loadOper(0,taicpu(p).oper[0]^);
  3169. if TempRegUsed then
  3170. begin
  3171. { Don't remove the first instruction if the temporary register is in use }
  3172. DebugMsg(SPeepholeOptimization + RegName1 + ' = ' + debug_tostr(taicpu(p).oper[0]^.val) + '; changed to minimise pipeline stall (MovMov2Mov 7a)',hp2);
  3173. { No need to set Result to True. If there's another instruction later on
  3174. that can be optimised, it will be detected when the main Pass 1 loop
  3175. reaches what is now hp2 and passes it through OptPass1MOV. [Kit] };
  3176. end
  3177. else
  3178. begin
  3179. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 7 done',p);
  3180. RemoveCurrentP(p, hp1);
  3181. Result:=true;
  3182. Exit;
  3183. end;
  3184. end;
  3185. end;
  3186. else
  3187. Internalerror(2019103001);
  3188. end;
  3189. end
  3190. else
  3191. if MatchOperand(taicpu(hp2).oper[1]^, ActiveReg) then
  3192. begin
  3193. if not CrossJump and
  3194. not RegUsedBetween(ActiveReg, p, hp2) and
  3195. not RegReadByInstruction(ActiveReg, hp2) then
  3196. begin
  3197. { Register is not used before it is overwritten }
  3198. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3a done',p);
  3199. RemoveCurrentp(p, hp1);
  3200. Result := True;
  3201. Exit;
  3202. end;
  3203. if (taicpu(p).oper[0]^.typ = top_const) and
  3204. (taicpu(hp2).oper[0]^.typ = top_const) then
  3205. begin
  3206. if taicpu(p).oper[0]^.val = taicpu(hp2).oper[0]^.val then
  3207. begin
  3208. { Same value - register hasn't changed }
  3209. DebugMsg(SPeepholeOptimization + 'Mov2Nop 2 done', hp2);
  3210. RemoveInstruction(hp2);
  3211. Result := True;
  3212. { See if there's more we can optimise }
  3213. Continue;
  3214. end;
  3215. end;
  3216. end;
  3217. A_MOVZX, A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}:
  3218. if MatchOpType(taicpu(hp2), top_reg, top_reg) and
  3219. MatchOperand(taicpu(hp2).oper[0]^, ActiveReg) and
  3220. SuperRegistersEqual(taicpu(hp2).oper[1]^.reg, ActiveReg) then
  3221. begin
  3222. {
  3223. Change from:
  3224. mov ###, %reg
  3225. ...
  3226. movs/z %reg,%reg (Same register, just different sizes)
  3227. To:
  3228. movs/z ###, %reg (Longer version)
  3229. ...
  3230. (remove)
  3231. }
  3232. DebugMsg(SPeepholeOptimization + 'MovMovs/z2Mov/s/z done', p);
  3233. taicpu(p).oper[1]^.reg := taicpu(hp2).oper[1]^.reg;
  3234. { Keep the first instruction as mov if ### is a constant }
  3235. if taicpu(p).oper[0]^.typ = top_const then
  3236. taicpu(p).opsize := reg2opsize(taicpu(hp2).oper[1]^.reg)
  3237. else
  3238. begin
  3239. taicpu(p).opcode := taicpu(hp2).opcode;
  3240. taicpu(p).opsize := taicpu(hp2).opsize;
  3241. end;
  3242. DebugMsg(SPeepholeOptimization + 'Removed movs/z instruction and extended earlier write (MovMovs/z2Mov/s/z)', hp2);
  3243. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp2, UsedRegs);
  3244. RemoveInstruction(hp2);
  3245. Result := True;
  3246. Exit;
  3247. end;
  3248. else
  3249. if MatchOpType(taicpu(p), top_reg, top_reg) then
  3250. begin
  3251. TransferUsedRegs(TmpUsedRegs);
  3252. TmpUsedRegs[R_INTREGISTER].Update(tai(p.Next));
  3253. if
  3254. not RegModifiedByInstruction(taicpu(p).oper[0]^.reg, hp1) and
  3255. not RegModifiedBetween(taicpu(p).oper[0]^.reg, hp1, hp2) and
  3256. DeepMovOpt(taicpu(p), taicpu(hp2)) then
  3257. begin
  3258. { Just in case something didn't get modified (e.g. an
  3259. implicit register) }
  3260. if not RegReadByInstruction(ActiveReg, hp2) and
  3261. { If a conditional jump was crossed, do not delete
  3262. the original MOV no matter what }
  3263. not CrossJump then
  3264. begin
  3265. TransferUsedRegs(TmpUsedRegs);
  3266. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3267. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3268. if
  3269. { Make sure the original register isn't still present
  3270. and has been written to (e.g. with SHRX) }
  3271. RegLoadedWithNewValue(ActiveReg, hp2) or
  3272. not RegUsedAfterInstruction(ActiveReg, hp2, TmpUsedRegs) then
  3273. begin
  3274. RegUsedAfterInstruction(ActiveReg, hp2, TmpUsedRegs);
  3275. { We can remove the original MOV }
  3276. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3b done',p);
  3277. RemoveCurrentp(p, hp1);
  3278. Result := True;
  3279. Exit;
  3280. end
  3281. else
  3282. begin
  3283. { See if there's more we can optimise }
  3284. hp3 := hp2;
  3285. Continue;
  3286. end;
  3287. end;
  3288. end;
  3289. end;
  3290. end;
  3291. { Break out of the while loop under normal circumstances }
  3292. Break;
  3293. end;
  3294. end;
  3295. if (aoc_MovAnd2Mov_3 in OptsToCheck) and
  3296. (taicpu(p).oper[1]^.typ = top_reg) and
  3297. (taicpu(p).opsize = S_L) and
  3298. GetNextInstructionUsingRegTrackingUse(p,hp2,taicpu(p).oper[1]^.reg) and
  3299. (taicpu(hp2).opcode = A_AND) and
  3300. (MatchOpType(taicpu(hp2),top_const,top_reg) or
  3301. (MatchOpType(taicpu(hp2),top_reg,top_reg) and
  3302. MatchOperand(taicpu(hp2).oper[0]^,taicpu(hp2).oper[1]^))
  3303. ) then
  3304. begin
  3305. if SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp2).oper[1]^.reg) then
  3306. begin
  3307. if ((taicpu(hp2).oper[0]^.typ=top_const) and (taicpu(hp2).oper[0]^.val = $ffffffff)) or
  3308. ((taicpu(hp2).oper[0]^.typ=top_reg) and (taicpu(hp2).opsize=S_L)) then
  3309. begin
  3310. { Optimize out:
  3311. mov x, %reg
  3312. and ffffffffh, %reg
  3313. }
  3314. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 3 done',p);
  3315. RemoveInstruction(hp2);
  3316. Result:=true;
  3317. exit;
  3318. end;
  3319. end;
  3320. end;
  3321. { leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  3322. x >= RetOffset) as it doesn't do anything (it writes either to a
  3323. parameter or to the temporary storage room for the function
  3324. result)
  3325. }
  3326. if IsExitCode(hp1) and
  3327. (taicpu(p).oper[1]^.typ = top_ref) and
  3328. (taicpu(p).oper[1]^.ref^.index = NR_NO) and
  3329. (
  3330. (
  3331. (taicpu(p).oper[1]^.ref^.base = current_procinfo.FramePointer) and
  3332. not (
  3333. assigned(current_procinfo.procdef.funcretsym) and
  3334. (taicpu(p).oper[1]^.ref^.offset <= tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)
  3335. )
  3336. ) or
  3337. { Also discard writes to the stack that are below the base pointer,
  3338. as this is temporary storage rather than a function result on the
  3339. stack, say. }
  3340. (
  3341. (taicpu(p).oper[1]^.ref^.base = NR_STACK_POINTER_REG) and
  3342. (taicpu(p).oper[1]^.ref^.offset < current_procinfo.final_localsize)
  3343. )
  3344. ) then
  3345. begin
  3346. RemoveCurrentp(p, hp1);
  3347. DebugMsg(SPeepholeOptimization + 'removed deadstore before leave/ret',p);
  3348. RemoveLastDeallocForFuncRes(p);
  3349. Result:=true;
  3350. exit;
  3351. end;
  3352. if MatchInstruction(hp1,A_CMP,A_TEST,[taicpu(p).opsize]) then
  3353. begin
  3354. if MatchOpType(taicpu(p),top_reg,top_ref) and
  3355. (taicpu(hp1).oper[1]^.typ = top_ref) and
  3356. RefsEqual(taicpu(p).oper[1]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  3357. begin
  3358. { change
  3359. mov reg1, mem1
  3360. test/cmp x, mem1
  3361. to
  3362. mov reg1, mem1
  3363. test/cmp x, reg1
  3364. }
  3365. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  3366. DebugMsg(SPeepholeOptimization + 'MovTestCmp2MovTestCmp 1',hp1);
  3367. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  3368. Result := True;
  3369. Exit;
  3370. end;
  3371. if MatchOpType(taicpu(p),top_ref,top_reg) and
  3372. { The x86 assemblers have difficulty comparing values against absolute addresses }
  3373. (taicpu(p).oper[0]^.ref^.refaddr in [addr_no, addr_pic, addr_pic_no_got]) and
  3374. (taicpu(hp1).oper[0]^.typ <> top_ref) and
  3375. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  3376. (
  3377. (
  3378. (taicpu(hp1).opcode = A_TEST)
  3379. ) or (
  3380. (taicpu(hp1).opcode = A_CMP) and
  3381. { A sanity check more than anything }
  3382. not MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg)
  3383. )
  3384. ) then
  3385. begin
  3386. { change
  3387. mov mem, %reg
  3388. cmp/test x, %reg / test %reg,%reg
  3389. (reg deallocated)
  3390. to
  3391. cmp/test x, mem / cmp 0, mem
  3392. }
  3393. TransferUsedRegs(TmpUsedRegs);
  3394. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3395. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) then
  3396. begin
  3397. { Convert test %reg,%reg or test $-1,%reg to cmp $0,mem }
  3398. if (taicpu(hp1).opcode = A_TEST) and
  3399. (
  3400. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) or
  3401. MatchOperand(taicpu(hp1).oper[0]^, -1)
  3402. ) then
  3403. begin
  3404. taicpu(hp1).opcode := A_CMP;
  3405. taicpu(hp1).loadconst(0, 0);
  3406. end;
  3407. taicpu(hp1).loadref(1, taicpu(p).oper[0]^.ref^);
  3408. DebugMsg(SPeepholeOptimization + 'MOV/CMP -> CMP (memory check)', p);
  3409. RemoveCurrentP(p, hp1);
  3410. Result := True;
  3411. Exit;
  3412. end;
  3413. end;
  3414. end;
  3415. if MatchInstruction(hp1,A_LEA,[S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  3416. { If the flags register is in use, don't change the instruction to an
  3417. ADD otherwise this will scramble the flags. [Kit] }
  3418. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) then
  3419. begin
  3420. if MatchOpType(Taicpu(p),top_ref,top_reg) and
  3421. ((MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(hp1).oper[1]^.reg,Taicpu(p).oper[1]^.reg) and
  3422. (Taicpu(hp1).oper[0]^.ref^.base<>Taicpu(p).oper[1]^.reg)
  3423. ) or
  3424. (MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(p).oper[1]^.reg,Taicpu(hp1).oper[1]^.reg) and
  3425. (Taicpu(hp1).oper[0]^.ref^.index<>Taicpu(p).oper[1]^.reg)
  3426. )
  3427. ) then
  3428. { mov reg1,ref
  3429. lea reg2,[reg1,reg2]
  3430. to
  3431. add reg2,ref}
  3432. begin
  3433. TransferUsedRegs(TmpUsedRegs);
  3434. { reg1 may not be used afterwards }
  3435. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  3436. begin
  3437. Taicpu(hp1).opcode:=A_ADD;
  3438. Taicpu(hp1).oper[0]^.ref^:=Taicpu(p).oper[0]^.ref^;
  3439. DebugMsg(SPeepholeOptimization + 'MovLea2Add done',hp1);
  3440. RemoveCurrentp(p, hp1);
  3441. result:=true;
  3442. exit;
  3443. end;
  3444. end;
  3445. { If the LEA instruction can be converted into an arithmetic instruction,
  3446. it may be possible to then fold it in the next optimisation, otherwise
  3447. there's nothing more that can be optimised here. }
  3448. if not ConvertLEA(taicpu(hp1)) then
  3449. Exit;
  3450. end;
  3451. if (taicpu(p).oper[1]^.typ = top_reg) and
  3452. (hp1.typ = ait_instruction) and
  3453. GetNextInstruction(hp1, hp2) and
  3454. MatchInstruction(hp2,A_MOV,[]) and
  3455. (SuperRegistersEqual(taicpu(hp2).oper[0]^.reg,taicpu(p).oper[1]^.reg)) and
  3456. (topsize2memsize[taicpu(hp1).opsize]>=topsize2memsize[taicpu(hp2).opsize]) and
  3457. (
  3458. IsFoldableArithOp(taicpu(hp1), taicpu(p).oper[1]^.reg)
  3459. {$ifdef x86_64}
  3460. or
  3461. (
  3462. (taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and (taicpu(hp2).opsize=S_L) and
  3463. IsFoldableArithOp(taicpu(hp1), newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[1]^.reg),R_SUBQ))
  3464. )
  3465. {$endif x86_64}
  3466. ) then
  3467. begin
  3468. if OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  3469. (taicpu(hp2).oper[0]^.typ=top_reg) then
  3470. { change movsX/movzX reg/ref, reg2
  3471. add/sub/or/... reg3/$const, reg2
  3472. mov reg2 reg/ref
  3473. dealloc reg2
  3474. to
  3475. add/sub/or/... reg3/$const, reg/ref }
  3476. begin
  3477. TransferUsedRegs(TmpUsedRegs);
  3478. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3479. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  3480. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  3481. begin
  3482. { by example:
  3483. movswl %si,%eax movswl %si,%eax p
  3484. decl %eax addl %edx,%eax hp1
  3485. movw %ax,%si movw %ax,%si hp2
  3486. ->
  3487. movswl %si,%eax movswl %si,%eax p
  3488. decw %eax addw %edx,%eax hp1
  3489. movw %ax,%si movw %ax,%si hp2
  3490. }
  3491. DebugMsg(SPeepholeOptimization + 'MovOpMov2Op ('+
  3492. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  3493. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  3494. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  3495. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  3496. {
  3497. ->
  3498. movswl %si,%eax movswl %si,%eax p
  3499. decw %si addw %dx,%si hp1
  3500. movw %ax,%si movw %ax,%si hp2
  3501. }
  3502. case taicpu(hp1).ops of
  3503. 1:
  3504. begin
  3505. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  3506. if taicpu(hp1).oper[0]^.typ=top_reg then
  3507. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  3508. end;
  3509. 2:
  3510. begin
  3511. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  3512. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  3513. (taicpu(hp1).opcode<>A_SHL) and
  3514. (taicpu(hp1).opcode<>A_SHR) and
  3515. (taicpu(hp1).opcode<>A_SAR) then
  3516. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  3517. end;
  3518. else
  3519. internalerror(2008042701);
  3520. end;
  3521. {
  3522. ->
  3523. decw %si addw %dx,%si p
  3524. }
  3525. RemoveInstruction(hp2);
  3526. RemoveCurrentP(p, hp1);
  3527. Result:=True;
  3528. Exit;
  3529. end;
  3530. end;
  3531. if MatchOpType(taicpu(hp2),top_reg,top_reg) and
  3532. not(SuperRegistersEqual(taicpu(hp1).oper[0]^.reg,taicpu(hp2).oper[1]^.reg)) and
  3533. ((topsize2memsize[taicpu(hp1).opsize]<= topsize2memsize[taicpu(hp2).opsize]) or
  3534. { opsize matters for these opcodes, we could probably work around this, but it is not worth the effort }
  3535. ((taicpu(hp1).opcode<>A_SHL) and (taicpu(hp1).opcode<>A_SHR) and (taicpu(hp1).opcode<>A_SAR))
  3536. )
  3537. {$ifdef i386}
  3538. { byte registers of esi, edi, ebp, esp are not available on i386 }
  3539. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  3540. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(p).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  3541. {$endif i386}
  3542. then
  3543. { change movsX/movzX reg/ref, reg2
  3544. add/sub/or/... regX/$const, reg2
  3545. mov reg2, reg3
  3546. dealloc reg2
  3547. to
  3548. movsX/movzX reg/ref, reg3
  3549. add/sub/or/... reg3/$const, reg3
  3550. }
  3551. begin
  3552. TransferUsedRegs(TmpUsedRegs);
  3553. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3554. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  3555. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  3556. begin
  3557. { by example:
  3558. movswl %si,%eax movswl %si,%eax p
  3559. decl %eax addl %edx,%eax hp1
  3560. movw %ax,%si movw %ax,%si hp2
  3561. ->
  3562. movswl %si,%eax movswl %si,%eax p
  3563. decw %eax addw %edx,%eax hp1
  3564. movw %ax,%si movw %ax,%si hp2
  3565. }
  3566. DebugMsg(SPeepholeOptimization + 'MovOpMov2MovOp ('+
  3567. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  3568. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  3569. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  3570. { limit size of constants as well to avoid assembler errors, but
  3571. check opsize to avoid overflow when left shifting the 1 }
  3572. if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=63) then
  3573. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl topsize2memsize[taicpu(hp2).opsize])-1);
  3574. {$ifdef x86_64}
  3575. { Be careful of, for example:
  3576. movl %reg1,%reg2
  3577. addl %reg3,%reg2
  3578. movq %reg2,%reg4
  3579. This will cause problems if the upper 32-bits of %reg3 or %reg4 are non-zero
  3580. }
  3581. if (taicpu(hp1).opsize = S_L) and (taicpu(hp2).opsize = S_Q) then
  3582. begin
  3583. taicpu(hp2).changeopsize(S_L);
  3584. setsubreg(taicpu(hp2).oper[0]^.reg, R_SUBD);
  3585. setsubreg(taicpu(hp2).oper[1]^.reg, R_SUBD);
  3586. end;
  3587. {$endif x86_64}
  3588. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  3589. taicpu(p).changeopsize(taicpu(hp2).opsize);
  3590. if taicpu(p).oper[0]^.typ=top_reg then
  3591. setsubreg(taicpu(p).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  3592. taicpu(p).loadoper(1, taicpu(hp2).oper[1]^);
  3593. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,usedregs);
  3594. {
  3595. ->
  3596. movswl %si,%eax movswl %si,%eax p
  3597. decw %si addw %dx,%si hp1
  3598. movw %ax,%si movw %ax,%si hp2
  3599. }
  3600. case taicpu(hp1).ops of
  3601. 1:
  3602. begin
  3603. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  3604. if taicpu(hp1).oper[0]^.typ=top_reg then
  3605. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  3606. end;
  3607. 2:
  3608. begin
  3609. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  3610. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  3611. (taicpu(hp1).opcode<>A_SHL) and
  3612. (taicpu(hp1).opcode<>A_SHR) and
  3613. (taicpu(hp1).opcode<>A_SAR) then
  3614. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  3615. end;
  3616. else
  3617. internalerror(2018111801);
  3618. end;
  3619. {
  3620. ->
  3621. decw %si addw %dx,%si p
  3622. }
  3623. RemoveInstruction(hp2);
  3624. end;
  3625. end;
  3626. end;
  3627. if MatchInstruction(hp1,A_BTS,A_BTR,[Taicpu(p).opsize]) and
  3628. GetNextInstruction(hp1, hp2) and
  3629. MatchInstruction(hp2,A_OR,[Taicpu(p).opsize]) and
  3630. MatchOperand(Taicpu(p).oper[0]^,0) and
  3631. (Taicpu(p).oper[1]^.typ = top_reg) and
  3632. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp1).oper[1]^) and
  3633. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp2).oper[1]^) then
  3634. { mov reg1,0
  3635. bts reg1,operand1 --> mov reg1,operand2
  3636. or reg1,operand2 bts reg1,operand1}
  3637. begin
  3638. Taicpu(hp2).opcode:=A_MOV;
  3639. DebugMsg(SPeepholeOptimization + 'MovBtsOr2MovBts done',hp1);
  3640. asml.remove(hp1);
  3641. insertllitem(hp2,hp2.next,hp1);
  3642. RemoveCurrentp(p, hp1);
  3643. Result:=true;
  3644. exit;
  3645. end;
  3646. {
  3647. mov ref,reg0
  3648. <op> reg0,reg1
  3649. dealloc reg0
  3650. to
  3651. <op> ref,reg1
  3652. }
  3653. if MatchOpType(taicpu(p),top_ref,top_reg) and
  3654. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3655. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  3656. MatchInstruction(hp1,[A_AND,A_OR,A_XOR,A_ADD,A_SUB,A_CMP],[Taicpu(p).opsize]) and
  3657. not(MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^)) and
  3658. RegEndOfLife(taicpu(p).oper[1]^.reg,taicpu(hp1)) then
  3659. begin
  3660. taicpu(hp1).loadoper(0,taicpu(p).oper[0]^);
  3661. DebugMsg(SPeepholeOptimization + 'MovOp2Op done',hp1);
  3662. RemoveCurrentp(p, hp1);
  3663. Result:=true;
  3664. exit;
  3665. end;
  3666. {$ifdef x86_64}
  3667. { Convert:
  3668. movq x(ref),%reg64
  3669. shrq y,%reg64
  3670. To:
  3671. movq x+4(ref),%reg32
  3672. shrq y-32,%reg32 (Remove if y = 32)
  3673. }
  3674. if (taicpu(p).opsize = S_Q) and
  3675. (taicpu(p).oper[0]^.typ = top_ref) and { Second operand will be a register }
  3676. (taicpu(p).oper[0]^.ref^.offset <= $7FFFFFFB) and
  3677. MatchInstruction(hp1, A_SHR, [taicpu(p).opsize]) and
  3678. MatchOpType(taicpu(hp1), top_const, top_reg) and
  3679. (taicpu(hp1).oper[0]^.val >= 32) and
  3680. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3681. begin
  3682. RegName1 := debug_regname(taicpu(hp1).oper[1]^.reg);
  3683. PreMessage := 'movq ' + debug_operstr(taicpu(p).oper[0]^) + ',' + RegName1 + '; ' +
  3684. 'shrq $' + debug_tostr(taicpu(hp1).oper[0]^.val) + ',' + RegName1 + ' -> movl ';
  3685. { Convert to 32-bit }
  3686. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3687. taicpu(p).opsize := S_L;
  3688. Inc(taicpu(p).oper[0]^.ref^.offset, 4);
  3689. PreMessage := PreMessage + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg);
  3690. if (taicpu(hp1).oper[0]^.val = 32) then
  3691. begin
  3692. DebugMsg(SPeepholeOptimization + PreMessage + ' (MovShr2Mov)', p);
  3693. RemoveInstruction(hp1);
  3694. end
  3695. else
  3696. begin
  3697. { This will potentially open up more arithmetic operations since
  3698. the peephole optimizer now has a big hint that only the lower
  3699. 32 bits are currently in use (and opcodes are smaller in size) }
  3700. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  3701. taicpu(hp1).opsize := S_L;
  3702. Dec(taicpu(hp1).oper[0]^.val, 32);
  3703. DebugMsg(SPeepholeOptimization + PreMessage +
  3704. '; shrl $' + debug_tostr(taicpu(hp1).oper[0]^.val) + ',' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' (MovShr2MovShr)', p);
  3705. end;
  3706. Result := True;
  3707. Exit;
  3708. end;
  3709. {$endif x86_64}
  3710. end;
  3711. function TX86AsmOptimizer.OptPass1MOVXX(var p : tai) : boolean;
  3712. var
  3713. hp1 : tai;
  3714. begin
  3715. Result:=false;
  3716. if taicpu(p).ops <> 2 then
  3717. exit;
  3718. if GetNextInstruction(p,hp1) then
  3719. begin
  3720. if MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  3721. (taicpu(hp1).ops = 2) then
  3722. begin
  3723. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  3724. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  3725. { movXX reg1, mem1 or movXX mem1, reg1
  3726. movXX mem2, reg2 movXX reg2, mem2}
  3727. begin
  3728. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  3729. { movXX reg1, mem1 or movXX mem1, reg1
  3730. movXX mem2, reg1 movXX reg2, mem1}
  3731. begin
  3732. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  3733. begin
  3734. { Removes the second statement from
  3735. movXX reg1, mem1/reg2
  3736. movXX mem1/reg2, reg1
  3737. }
  3738. if taicpu(p).oper[0]^.typ=top_reg then
  3739. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  3740. { Removes the second statement from
  3741. movXX mem1/reg1, reg2
  3742. movXX reg2, mem1/reg1
  3743. }
  3744. if (taicpu(p).oper[1]^.typ=top_reg) and
  3745. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)) then
  3746. begin
  3747. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2Nop 1 done',p);
  3748. RemoveInstruction(hp1);
  3749. RemoveCurrentp(p); { p will now be equal to the instruction that follows what was hp1 }
  3750. end
  3751. else
  3752. begin
  3753. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
  3754. RemoveInstruction(hp1);
  3755. end;
  3756. Result:=true;
  3757. exit;
  3758. end
  3759. end;
  3760. end;
  3761. end;
  3762. end;
  3763. end;
  3764. function TX86AsmOptimizer.OptPass1OP(var p : tai) : boolean;
  3765. var
  3766. hp1 : tai;
  3767. begin
  3768. result:=false;
  3769. { replace
  3770. <Op>X %mreg1,%mreg2 // Op in [ADD,MUL]
  3771. MovX %mreg2,%mreg1
  3772. dealloc %mreg2
  3773. by
  3774. <Op>X %mreg2,%mreg1
  3775. ?
  3776. }
  3777. if GetNextInstruction(p,hp1) and
  3778. { we mix single and double opperations here because we assume that the compiler
  3779. generates vmovapd only after double operations and vmovaps only after single operations }
  3780. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  3781. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  3782. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  3783. (taicpu(p).oper[0]^.typ=top_reg) then
  3784. begin
  3785. TransferUsedRegs(TmpUsedRegs);
  3786. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3787. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  3788. begin
  3789. taicpu(p).loadoper(0,taicpu(hp1).oper[0]^);
  3790. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  3791. DebugMsg(SPeepholeOptimization + 'OpMov2Op done',p);
  3792. RemoveInstruction(hp1);
  3793. result:=true;
  3794. end;
  3795. end;
  3796. end;
  3797. function TX86AsmOptimizer.OptPass1Test(var p: tai) : boolean;
  3798. var
  3799. hp1, p_label, p_dist, hp1_dist: tai;
  3800. JumpLabel, JumpLabel_dist: TAsmLabel;
  3801. begin
  3802. Result := False;
  3803. if GetNextInstruction(p, hp1) and
  3804. MatchInstruction(hp1,A_MOV,[]) and
  3805. (
  3806. (taicpu(p).oper[0]^.typ <> top_reg) or
  3807. not RegInInstruction(taicpu(p).oper[0]^.reg, hp1)
  3808. ) and
  3809. (
  3810. (taicpu(p).oper[1]^.typ <> top_reg) or
  3811. not RegInInstruction(taicpu(p).oper[1]^.reg, hp1)
  3812. ) and
  3813. (
  3814. { Make sure the register written to doesn't appear in the
  3815. test instruction (in a reference, say) }
  3816. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  3817. not RegInInstruction(taicpu(hp1).oper[1]^.reg, p)
  3818. ) then
  3819. begin
  3820. { If we have something like:
  3821. test %reg1,%reg1
  3822. mov 0,%reg2
  3823. And no registers are shared (the two %reg1's can be different, as
  3824. long as neither of them are also %reg2), move the MOV command to
  3825. before the comparison as this means it can be optimised without
  3826. worrying about the FLAGS register. (This combination is generated
  3827. by "J(c)Mov1JmpMov0 -> Set(~c)", among other things).
  3828. }
  3829. SwapMovCmp(p, hp1);
  3830. Result := True;
  3831. Exit;
  3832. end;
  3833. { Search for:
  3834. test %reg,%reg
  3835. j(c1) @lbl1
  3836. ...
  3837. @lbl:
  3838. test %reg,%reg (same register)
  3839. j(c2) @lbl2
  3840. If c2 is a subset of c1, change to:
  3841. test %reg,%reg
  3842. j(c1) @lbl2
  3843. (@lbl1 may become a dead label as a result)
  3844. }
  3845. if (taicpu(p).oper[1]^.typ = top_reg) and
  3846. (taicpu(p).oper[0]^.typ = top_reg) and
  3847. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  3848. MatchInstruction(hp1, A_JCC, []) and
  3849. IsJumpToLabel(taicpu(hp1)) then
  3850. begin
  3851. JumpLabel := TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol);
  3852. p_label := nil;
  3853. if Assigned(JumpLabel) then
  3854. p_label := getlabelwithsym(JumpLabel);
  3855. if Assigned(p_label) and
  3856. GetNextInstruction(p_label, p_dist) and
  3857. MatchInstruction(p_dist, A_TEST, []) and
  3858. { It's fine if the second test uses smaller sub-registers }
  3859. (taicpu(p_dist).opsize <= taicpu(p).opsize) and
  3860. MatchOpType(taicpu(p_dist), top_reg, top_reg) and
  3861. SuperRegistersEqual(taicpu(p_dist).oper[0]^.reg, taicpu(p).oper[0]^.reg) and
  3862. SuperRegistersEqual(taicpu(p_dist).oper[1]^.reg, taicpu(p).oper[1]^.reg) and
  3863. GetNextInstruction(p_dist, hp1_dist) and
  3864. MatchInstruction(hp1_dist, A_JCC, []) then { This doesn't have to be an explicit label }
  3865. begin
  3866. JumpLabel_dist := TAsmLabel(taicpu(hp1_dist).oper[0]^.ref^.symbol);
  3867. if JumpLabel = JumpLabel_dist then
  3868. { This is an infinite loop }
  3869. Exit;
  3870. { Best optimisation when the first condition is a subset (or equal) of the second }
  3871. if condition_in(taicpu(hp1).condition, taicpu(hp1_dist).condition) then
  3872. begin
  3873. { Any registers used here will already be allocated }
  3874. if Assigned(JumpLabel_dist) then
  3875. JumpLabel_dist.IncRefs;
  3876. if Assigned(JumpLabel) then
  3877. JumpLabel.DecRefs;
  3878. DebugMsg(SPeepholeOptimization + 'TEST/Jcc/@Lbl/TEST/Jcc -> TEST/Jcc, redirecting first jump', hp1);
  3879. taicpu(hp1).loadref(0, taicpu(hp1_dist).oper[0]^.ref^);
  3880. Result := True;
  3881. Exit;
  3882. end;
  3883. end;
  3884. end;
  3885. end;
  3886. function TX86AsmOptimizer.OptPass1Add(var p : tai) : boolean;
  3887. var
  3888. hp1 : tai;
  3889. begin
  3890. result:=false;
  3891. { replace
  3892. addX const,%reg1
  3893. leaX (%reg1,%reg1,Y),%reg2 // Base or index might not be equal to reg1
  3894. dealloc %reg1
  3895. by
  3896. leaX const+const*Y(%reg1,%reg1,Y),%reg2
  3897. }
  3898. if MatchOpType(taicpu(p),top_const,top_reg) and
  3899. GetNextInstruction(p,hp1) and
  3900. MatchInstruction(hp1,A_LEA,[taicpu(p).opsize]) and
  3901. ((taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.base) or
  3902. (taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.index)) then
  3903. begin
  3904. TransferUsedRegs(TmpUsedRegs);
  3905. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3906. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  3907. begin
  3908. DebugMsg(SPeepholeOptimization + 'AddLea2Lea done',p);
  3909. if taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.base then
  3910. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.val);
  3911. if taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.index then
  3912. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.val*max(taicpu(hp1).oper[0]^.ref^.scalefactor,1));
  3913. RemoveCurrentP(p);
  3914. result:=true;
  3915. end;
  3916. end;
  3917. end;
  3918. function TX86AsmOptimizer.OptPass1LEA(var p : tai) : boolean;
  3919. var
  3920. hp1: tai;
  3921. ref: Integer;
  3922. saveref: treference;
  3923. TempReg: TRegister;
  3924. Multiple: TCGInt;
  3925. begin
  3926. Result:=false;
  3927. { removes seg register prefixes from LEA operations, as they
  3928. don't do anything}
  3929. taicpu(p).oper[0]^.ref^.Segment:=NR_NO;
  3930. { changes "lea (%reg1), %reg2" into "mov %reg1, %reg2" }
  3931. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  3932. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  3933. (
  3934. { do not mess with leas accessing the stack pointer
  3935. unless it's a null operation }
  3936. (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) or
  3937. (
  3938. (taicpu(p).oper[0]^.ref^.base = NR_STACK_POINTER_REG) and
  3939. (taicpu(p).oper[0]^.ref^.offset = 0)
  3940. )
  3941. ) and
  3942. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  3943. begin
  3944. if (taicpu(p).oper[0]^.ref^.offset = 0) then
  3945. begin
  3946. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) then
  3947. begin
  3948. hp1:=taicpu.op_reg_reg(A_MOV,taicpu(p).opsize,taicpu(p).oper[0]^.ref^.base,
  3949. taicpu(p).oper[1]^.reg);
  3950. InsertLLItem(p.previous,p.next, hp1);
  3951. DebugMsg(SPeepholeOptimization + 'Lea2Mov done',hp1);
  3952. p.free;
  3953. p:=hp1;
  3954. end
  3955. else
  3956. begin
  3957. DebugMsg(SPeepholeOptimization + 'Lea2Nop done',p);
  3958. RemoveCurrentP(p);
  3959. end;
  3960. Result:=true;
  3961. exit;
  3962. end
  3963. else if (
  3964. { continue to use lea to adjust the stack pointer,
  3965. it is the recommended way, but only if not optimizing for size }
  3966. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) or
  3967. (cs_opt_size in current_settings.optimizerswitches)
  3968. ) and
  3969. { If the flags register is in use, don't change the instruction
  3970. to an ADD otherwise this will scramble the flags. [Kit] }
  3971. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  3972. ConvertLEA(taicpu(p)) then
  3973. begin
  3974. Result:=true;
  3975. exit;
  3976. end;
  3977. end;
  3978. if GetNextInstruction(p,hp1) and
  3979. (hp1.typ=ait_instruction) then
  3980. begin
  3981. if MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  3982. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  3983. MatchOpType(Taicpu(hp1),top_reg,top_reg) and
  3984. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) then
  3985. begin
  3986. TransferUsedRegs(TmpUsedRegs);
  3987. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3988. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  3989. begin
  3990. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  3991. DebugMsg(SPeepholeOptimization + 'LeaMov2Lea done',p);
  3992. RemoveInstruction(hp1);
  3993. result:=true;
  3994. exit;
  3995. end;
  3996. end;
  3997. { changes
  3998. lea <ref1>, reg1
  3999. <op> ...,<ref. with reg1>,...
  4000. to
  4001. <op> ...,<ref1>,... }
  4002. if (taicpu(p).oper[1]^.reg<>current_procinfo.framepointer) and
  4003. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) and
  4004. not(MatchInstruction(hp1,A_LEA,[])) then
  4005. begin
  4006. { find a reference which uses reg1 }
  4007. 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
  4008. ref:=0
  4009. 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
  4010. ref:=1
  4011. else
  4012. ref:=-1;
  4013. if (ref<>-1) and
  4014. { reg1 must be either the base or the index }
  4015. ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) xor (taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg)) then
  4016. begin
  4017. { reg1 can be removed from the reference }
  4018. saveref:=taicpu(hp1).oper[ref]^.ref^;
  4019. if taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg then
  4020. taicpu(hp1).oper[ref]^.ref^.base:=NR_NO
  4021. else if taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg then
  4022. taicpu(hp1).oper[ref]^.ref^.index:=NR_NO
  4023. else
  4024. Internalerror(2019111201);
  4025. { check if the can insert all data of the lea into the second instruction }
  4026. if ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) or (taicpu(hp1).oper[ref]^.ref^.scalefactor <= 1)) and
  4027. ((taicpu(p).oper[0]^.ref^.base=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.base=NR_NO)) and
  4028. ((taicpu(p).oper[0]^.ref^.index=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.index=NR_NO)) and
  4029. ((taicpu(p).oper[0]^.ref^.symbol=nil) or (taicpu(hp1).oper[ref]^.ref^.symbol=nil)) and
  4030. ((taicpu(p).oper[0]^.ref^.relsymbol=nil) or (taicpu(hp1).oper[ref]^.ref^.relsymbol=nil)) and
  4031. ((taicpu(p).oper[0]^.ref^.scalefactor <= 1) or (taicpu(hp1).oper[ref]^.ref^.scalefactor <= 1)) and
  4032. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.segment=NR_NO)
  4033. {$ifdef x86_64}
  4034. and (abs(taicpu(hp1).oper[ref]^.ref^.offset+taicpu(p).oper[0]^.ref^.offset)<=$7fffffff)
  4035. and (((taicpu(p).oper[0]^.ref^.base<>NR_RIP) and (taicpu(p).oper[0]^.ref^.index<>NR_RIP)) or
  4036. ((taicpu(hp1).oper[ref]^.ref^.base=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.index=NR_NO))
  4037. )
  4038. {$endif x86_64}
  4039. then
  4040. begin
  4041. { reg1 might not used by the second instruction after it is remove from the reference }
  4042. if not(RegInInstruction(taicpu(p).oper[1]^.reg,taicpu(hp1))) then
  4043. begin
  4044. TransferUsedRegs(TmpUsedRegs);
  4045. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4046. { reg1 is not updated so it might not be used afterwards }
  4047. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  4048. begin
  4049. DebugMsg(SPeepholeOptimization + 'LeaOp2Op done',p);
  4050. if taicpu(p).oper[0]^.ref^.base<>NR_NO then
  4051. taicpu(hp1).oper[ref]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  4052. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  4053. taicpu(hp1).oper[ref]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  4054. if taicpu(p).oper[0]^.ref^.symbol<>nil then
  4055. taicpu(hp1).oper[ref]^.ref^.symbol:=taicpu(p).oper[0]^.ref^.symbol;
  4056. if taicpu(p).oper[0]^.ref^.relsymbol<>nil then
  4057. taicpu(hp1).oper[ref]^.ref^.relsymbol:=taicpu(p).oper[0]^.ref^.relsymbol;
  4058. if taicpu(p).oper[0]^.ref^.scalefactor > 1 then
  4059. taicpu(hp1).oper[ref]^.ref^.scalefactor:=taicpu(p).oper[0]^.ref^.scalefactor;
  4060. inc(taicpu(hp1).oper[ref]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  4061. RemoveCurrentP(p, hp1);
  4062. result:=true;
  4063. exit;
  4064. end
  4065. end;
  4066. end;
  4067. { recover }
  4068. taicpu(hp1).oper[ref]^.ref^:=saveref;
  4069. end;
  4070. end;
  4071. end;
  4072. { for now, we do not mess with the stack pointer, thought it might be usefull to remove
  4073. unneeded lea sequences on the stack pointer, it needs to be tested in detail }
  4074. if (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) and
  4075. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) then
  4076. begin
  4077. { Check common LEA/LEA conditions }
  4078. if MatchInstruction(hp1,A_LEA,[taicpu(p).opsize]) and
  4079. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
  4080. (taicpu(p).oper[0]^.ref^.relsymbol = nil) and
  4081. (taicpu(p).oper[0]^.ref^.segment = NR_NO) and
  4082. (taicpu(p).oper[0]^.ref^.symbol = nil) and
  4083. (taicpu(hp1).oper[0]^.ref^.relsymbol = nil) and
  4084. (taicpu(hp1).oper[0]^.ref^.segment = NR_NO) and
  4085. (taicpu(hp1).oper[0]^.ref^.symbol = nil) and
  4086. (
  4087. (taicpu(p).oper[0]^.ref^.base = NR_NO) or { Don't call RegModifiedBetween unnecessarily }
  4088. not(RegModifiedBetween(taicpu(p).oper[0]^.ref^.base,p,hp1))
  4089. ) and (
  4090. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) or { Don't call RegModifiedBetween unnecessarily }
  4091. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  4092. not(RegModifiedBetween(taicpu(p).oper[0]^.ref^.index,p,hp1))
  4093. ) then
  4094. begin
  4095. { changes
  4096. lea (regX,scale), reg1
  4097. lea offset(reg1,reg1), reg1
  4098. to
  4099. lea offset(regX,scale*2), reg1
  4100. and
  4101. lea (regX,scale1), reg1
  4102. lea offset(reg1,scale2), reg1
  4103. to
  4104. lea offset(regX,scale1*scale2), reg1
  4105. ... so long as the final scale does not exceed 8
  4106. (Similarly, allow the first instruction to be "lea (regX,regX),reg1")
  4107. }
  4108. if (taicpu(p).oper[0]^.ref^.offset = 0) and
  4109. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  4110. (
  4111. (
  4112. (taicpu(p).oper[0]^.ref^.base = NR_NO)
  4113. ) or (
  4114. (taicpu(p).oper[0]^.ref^.scalefactor <= 1) and
  4115. (
  4116. (taicpu(p).oper[0]^.ref^.base = taicpu(p).oper[0]^.ref^.index) and
  4117. not(RegUsedBetween(taicpu(p).oper[0]^.ref^.index, p, hp1))
  4118. )
  4119. )
  4120. ) and (
  4121. (
  4122. { lea (reg1,scale2), reg1 variant }
  4123. (taicpu(hp1).oper[0]^.ref^.base = NR_NO) and
  4124. (
  4125. (
  4126. (taicpu(p).oper[0]^.ref^.base = NR_NO) and
  4127. (taicpu(hp1).oper[0]^.ref^.scalefactor * taicpu(p).oper[0]^.ref^.scalefactor <= 8)
  4128. ) or (
  4129. { lea (regX,regX), reg1 variant }
  4130. (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  4131. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 4)
  4132. )
  4133. )
  4134. ) or (
  4135. { lea (reg1,reg1), reg1 variant }
  4136. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  4137. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1)
  4138. )
  4139. ) then
  4140. begin
  4141. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea 2 done',p);
  4142. { Make everything homogeneous to make calculations easier }
  4143. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) then
  4144. begin
  4145. if taicpu(p).oper[0]^.ref^.index <> NR_NO then
  4146. { Convert lea (regX,regX),reg1 to lea (regX,2),reg1 }
  4147. taicpu(p).oper[0]^.ref^.scalefactor := 2
  4148. else
  4149. taicpu(p).oper[0]^.ref^.index := taicpu(p).oper[0]^.ref^.base;
  4150. taicpu(p).oper[0]^.ref^.base := NR_NO;
  4151. end;
  4152. if (taicpu(hp1).oper[0]^.ref^.base = NR_NO) then
  4153. begin
  4154. { Just to prevent miscalculations }
  4155. if (taicpu(hp1).oper[0]^.ref^.scalefactor = 0) then
  4156. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(p).oper[0]^.ref^.scalefactor
  4157. else
  4158. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(hp1).oper[0]^.ref^.scalefactor * taicpu(p).oper[0]^.ref^.scalefactor;
  4159. end
  4160. else
  4161. begin
  4162. taicpu(hp1).oper[0]^.ref^.base := NR_NO;
  4163. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(p).oper[0]^.ref^.scalefactor * 2;
  4164. end;
  4165. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.ref^.index;
  4166. RemoveCurrentP(p);
  4167. result:=true;
  4168. exit;
  4169. end
  4170. { changes
  4171. lea offset1(regX), reg1
  4172. lea offset2(reg1), reg1
  4173. to
  4174. lea offset1+offset2(regX), reg1 }
  4175. else if
  4176. (
  4177. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  4178. (taicpu(p).oper[0]^.ref^.index = NR_NO)
  4179. ) or (
  4180. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  4181. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1) and
  4182. (
  4183. (
  4184. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  4185. (taicpu(p).oper[0]^.ref^.base = NR_NO)
  4186. ) or (
  4187. (taicpu(p).oper[0]^.ref^.scalefactor <= 1) and
  4188. (
  4189. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  4190. (
  4191. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) and
  4192. (
  4193. (taicpu(hp1).oper[0]^.ref^.index = NR_NO) or
  4194. (taicpu(hp1).oper[0]^.ref^.base = NR_NO)
  4195. )
  4196. )
  4197. )
  4198. )
  4199. )
  4200. ) then
  4201. begin
  4202. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea 1 done',p);
  4203. if taicpu(hp1).oper[0]^.ref^.index=taicpu(p).oper[1]^.reg then
  4204. begin
  4205. taicpu(hp1).oper[0]^.ref^.index:=taicpu(p).oper[0]^.ref^.base;
  4206. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset*max(taicpu(hp1).oper[0]^.ref^.scalefactor,1));
  4207. { if the register is used as index and base, we have to increase for base as well
  4208. and adapt base }
  4209. if taicpu(hp1).oper[0]^.ref^.base=taicpu(p).oper[1]^.reg then
  4210. begin
  4211. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  4212. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  4213. end;
  4214. end
  4215. else
  4216. begin
  4217. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  4218. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  4219. end;
  4220. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  4221. begin
  4222. taicpu(hp1).oper[0]^.ref^.base:=taicpu(hp1).oper[0]^.ref^.index;
  4223. taicpu(hp1).oper[0]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  4224. taicpu(hp1).oper[0]^.ref^.scalefactor:=taicpu(p).oper[0]^.ref^.scalefactor;
  4225. end;
  4226. RemoveCurrentP(p);
  4227. result:=true;
  4228. exit;
  4229. end;
  4230. end;
  4231. { Change:
  4232. leal/q $x(%reg1),%reg2
  4233. ...
  4234. shll/q $y,%reg2
  4235. To:
  4236. leal/q $(x+2^y)(%reg1,2^y),%reg2 (if y <= 3)
  4237. }
  4238. if MatchInstruction(hp1, A_SHL, [taicpu(p).opsize]) and
  4239. MatchOpType(taicpu(hp1), top_const, top_reg) and
  4240. (taicpu(hp1).oper[0]^.val <= 3) then
  4241. begin
  4242. Multiple := 1 shl taicpu(hp1).oper[0]^.val;
  4243. TransferUsedRegs(TmpUsedRegs);
  4244. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  4245. TempReg := taicpu(hp1).oper[1]^.reg; { Store locally to reduce the number of dereferences }
  4246. if
  4247. { This allows the optimisation in some circumstances even if the lea instruction already has a scale factor
  4248. (this works even if scalefactor is zero) }
  4249. ((Multiple * taicpu(p).oper[0]^.ref^.scalefactor) <= 8) and
  4250. { Ensure offset doesn't go out of bounds }
  4251. (abs(taicpu(p).oper[0]^.ref^.offset * Multiple) <= $7FFFFFFF) and
  4252. not (RegInUsedRegs(NR_DEFAULTFLAGS,TmpUsedRegs)) and
  4253. MatchOperand(taicpu(p).oper[1]^, TempReg) and
  4254. (
  4255. (
  4256. not SuperRegistersEqual(taicpu(p).oper[0]^.ref^.base, TempReg) and
  4257. (
  4258. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  4259. (taicpu(p).oper[0]^.ref^.index = NR_INVALID) or
  4260. (
  4261. { Check for lea $x(%reg1,%reg1),%reg2 and treat as it it were lea $x(%reg1,2),%reg2 }
  4262. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) and
  4263. (taicpu(p).oper[0]^.ref^.scalefactor <= 1)
  4264. )
  4265. )
  4266. ) or (
  4267. (
  4268. (taicpu(p).oper[0]^.ref^.base = NR_NO) or
  4269. (taicpu(p).oper[0]^.ref^.base = NR_INVALID)
  4270. ) and
  4271. not SuperRegistersEqual(taicpu(p).oper[0]^.ref^.index, TempReg)
  4272. )
  4273. ) then
  4274. begin
  4275. repeat
  4276. with taicpu(p).oper[0]^.ref^ do
  4277. begin
  4278. { Convert lea $x(%reg1,%reg1),%reg2 to lea $x(%reg1,2),%reg2 }
  4279. if index = base then
  4280. begin
  4281. if Multiple > 4 then
  4282. { Optimisation will no longer work because resultant
  4283. scale factor will exceed 8 }
  4284. Break;
  4285. base := NR_NO;
  4286. scalefactor := 2;
  4287. DebugMsg(SPeepholeOptimization + 'lea $x(%reg1,%reg1),%reg2 -> lea $x(%reg1,2),%reg2 for following optimisation', p);
  4288. end
  4289. else if (base <> NR_NO) and (base <> NR_INVALID) then
  4290. begin
  4291. { Scale factor only works on the index register }
  4292. index := base;
  4293. base := NR_NO;
  4294. end;
  4295. { For safety }
  4296. if scalefactor <= 1 then
  4297. begin
  4298. DebugMsg(SPeepholeOptimization + 'LeaShl2Lea 1', p);
  4299. scalefactor := Multiple;
  4300. end
  4301. else
  4302. begin
  4303. DebugMsg(SPeepholeOptimization + 'LeaShl2Lea 2', p);
  4304. scalefactor := scalefactor * Multiple;
  4305. end;
  4306. offset := offset * Multiple;
  4307. end;
  4308. RemoveInstruction(hp1);
  4309. Result := True;
  4310. Exit;
  4311. { This repeat..until loop exists for the benefit of Break }
  4312. until True;
  4313. end;
  4314. end;
  4315. end;
  4316. end;
  4317. function TX86AsmOptimizer.DoSubAddOpt(var p: tai): Boolean;
  4318. var
  4319. hp1 : tai;
  4320. begin
  4321. DoSubAddOpt := False;
  4322. if GetLastInstruction(p, hp1) and
  4323. (hp1.typ = ait_instruction) and
  4324. (taicpu(hp1).opsize = taicpu(p).opsize) then
  4325. case taicpu(hp1).opcode Of
  4326. A_DEC:
  4327. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  4328. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  4329. begin
  4330. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
  4331. RemoveInstruction(hp1);
  4332. end;
  4333. A_SUB:
  4334. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  4335. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  4336. begin
  4337. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
  4338. RemoveInstruction(hp1);
  4339. end;
  4340. A_ADD:
  4341. begin
  4342. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  4343. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  4344. begin
  4345. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  4346. RemoveInstruction(hp1);
  4347. if (taicpu(p).oper[0]^.val = 0) then
  4348. begin
  4349. hp1 := tai(p.next);
  4350. RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
  4351. if not GetLastInstruction(hp1, p) then
  4352. p := hp1;
  4353. DoSubAddOpt := True;
  4354. end
  4355. end;
  4356. end;
  4357. else
  4358. ;
  4359. end;
  4360. end;
  4361. function TX86AsmOptimizer.OptPass1Sub(var p : tai) : boolean;
  4362. {$ifdef i386}
  4363. var
  4364. hp1 : tai;
  4365. {$endif i386}
  4366. begin
  4367. Result:=false;
  4368. { * change "subl $2, %esp; pushw x" to "pushl x"}
  4369. { * change "sub/add const1, reg" or "dec reg" followed by
  4370. "sub const2, reg" to one "sub ..., reg" }
  4371. if MatchOpType(taicpu(p),top_const,top_reg) then
  4372. begin
  4373. {$ifdef i386}
  4374. if (taicpu(p).oper[0]^.val = 2) and
  4375. (taicpu(p).oper[1]^.reg = NR_ESP) and
  4376. { Don't do the sub/push optimization if the sub }
  4377. { comes from setting up the stack frame (JM) }
  4378. (not(GetLastInstruction(p,hp1)) or
  4379. not(MatchInstruction(hp1,A_MOV,[S_L]) and
  4380. MatchOperand(taicpu(hp1).oper[0]^,NR_ESP) and
  4381. MatchOperand(taicpu(hp1).oper[0]^,NR_EBP))) then
  4382. begin
  4383. hp1 := tai(p.next);
  4384. while Assigned(hp1) and
  4385. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  4386. not RegReadByInstruction(NR_ESP,hp1) and
  4387. not RegModifiedByInstruction(NR_ESP,hp1) do
  4388. hp1 := tai(hp1.next);
  4389. if Assigned(hp1) and
  4390. MatchInstruction(hp1,A_PUSH,[S_W]) then
  4391. begin
  4392. taicpu(hp1).changeopsize(S_L);
  4393. if taicpu(hp1).oper[0]^.typ=top_reg then
  4394. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  4395. hp1 := tai(p.next);
  4396. RemoveCurrentp(p, hp1);
  4397. Result:=true;
  4398. exit;
  4399. end;
  4400. end;
  4401. {$endif i386}
  4402. if DoSubAddOpt(p) then
  4403. Result:=true;
  4404. end;
  4405. end;
  4406. function TX86AsmOptimizer.OptPass1SHLSAL(var p : tai) : boolean;
  4407. var
  4408. TmpBool1,TmpBool2 : Boolean;
  4409. tmpref : treference;
  4410. hp1,hp2: tai;
  4411. mask: tcgint;
  4412. begin
  4413. Result:=false;
  4414. { All these optimisations work on "shl/sal const,%reg" }
  4415. if not MatchOpType(taicpu(p),top_const,top_reg) then
  4416. Exit;
  4417. if (taicpu(p).opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  4418. (taicpu(p).oper[0]^.val <= 3) then
  4419. { Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement }
  4420. begin
  4421. { should we check the next instruction? }
  4422. TmpBool1 := True;
  4423. { have we found an add/sub which could be
  4424. integrated in the lea? }
  4425. TmpBool2 := False;
  4426. reference_reset(tmpref,2,[]);
  4427. TmpRef.index := taicpu(p).oper[1]^.reg;
  4428. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  4429. while TmpBool1 and
  4430. GetNextInstruction(p, hp1) and
  4431. (tai(hp1).typ = ait_instruction) and
  4432. ((((taicpu(hp1).opcode = A_ADD) or
  4433. (taicpu(hp1).opcode = A_SUB)) and
  4434. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  4435. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  4436. (((taicpu(hp1).opcode = A_INC) or
  4437. (taicpu(hp1).opcode = A_DEC)) and
  4438. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  4439. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg)) or
  4440. ((taicpu(hp1).opcode = A_LEA) and
  4441. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  4442. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg))) and
  4443. (not GetNextInstruction(hp1,hp2) or
  4444. not instrReadsFlags(hp2)) Do
  4445. begin
  4446. TmpBool1 := False;
  4447. if taicpu(hp1).opcode=A_LEA then
  4448. begin
  4449. if (TmpRef.base = NR_NO) and
  4450. (taicpu(hp1).oper[0]^.ref^.symbol=nil) and
  4451. (taicpu(hp1).oper[0]^.ref^.relsymbol=nil) and
  4452. (taicpu(hp1).oper[0]^.ref^.segment=NR_NO) and
  4453. ((taicpu(hp1).oper[0]^.ref^.scalefactor=0) or
  4454. (taicpu(hp1).oper[0]^.ref^.scalefactor*tmpref.scalefactor<=8)) then
  4455. begin
  4456. TmpBool1 := True;
  4457. TmpBool2 := True;
  4458. inc(TmpRef.offset, taicpu(hp1).oper[0]^.ref^.offset);
  4459. if taicpu(hp1).oper[0]^.ref^.scalefactor<>0 then
  4460. tmpref.scalefactor:=tmpref.scalefactor*taicpu(hp1).oper[0]^.ref^.scalefactor;
  4461. TmpRef.base := taicpu(hp1).oper[0]^.ref^.base;
  4462. RemoveInstruction(hp1);
  4463. end
  4464. end
  4465. else if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  4466. begin
  4467. TmpBool1 := True;
  4468. TmpBool2 := True;
  4469. case taicpu(hp1).opcode of
  4470. A_ADD:
  4471. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  4472. A_SUB:
  4473. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  4474. else
  4475. internalerror(2019050536);
  4476. end;
  4477. RemoveInstruction(hp1);
  4478. end
  4479. else
  4480. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  4481. (((taicpu(hp1).opcode = A_ADD) and
  4482. (TmpRef.base = NR_NO)) or
  4483. (taicpu(hp1).opcode = A_INC) or
  4484. (taicpu(hp1).opcode = A_DEC)) then
  4485. begin
  4486. TmpBool1 := True;
  4487. TmpBool2 := True;
  4488. case taicpu(hp1).opcode of
  4489. A_ADD:
  4490. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  4491. A_INC:
  4492. inc(TmpRef.offset);
  4493. A_DEC:
  4494. dec(TmpRef.offset);
  4495. else
  4496. internalerror(2019050535);
  4497. end;
  4498. RemoveInstruction(hp1);
  4499. end;
  4500. end;
  4501. if TmpBool2
  4502. {$ifndef x86_64}
  4503. or
  4504. ((current_settings.optimizecputype < cpu_Pentium2) and
  4505. (taicpu(p).oper[0]^.val <= 3) and
  4506. not(cs_opt_size in current_settings.optimizerswitches))
  4507. {$endif x86_64}
  4508. then
  4509. begin
  4510. if not(TmpBool2) and
  4511. (taicpu(p).oper[0]^.val=1) then
  4512. begin
  4513. hp1:=taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  4514. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  4515. end
  4516. else
  4517. hp1:=taicpu.op_ref_reg(A_LEA, taicpu(p).opsize, TmpRef,
  4518. taicpu(p).oper[1]^.reg);
  4519. DebugMsg(SPeepholeOptimization + 'ShlAddLeaSubIncDec2Lea',p);
  4520. InsertLLItem(p.previous, p.next, hp1);
  4521. p.free;
  4522. p := hp1;
  4523. end;
  4524. end
  4525. {$ifndef x86_64}
  4526. else if (current_settings.optimizecputype < cpu_Pentium2) then
  4527. begin
  4528. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  4529. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  4530. (unlike shl, which is only Tairable in the U pipe) }
  4531. if taicpu(p).oper[0]^.val=1 then
  4532. begin
  4533. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  4534. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  4535. InsertLLItem(p.previous, p.next, hp1);
  4536. p.free;
  4537. p := hp1;
  4538. end
  4539. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  4540. "shl $3, %reg" to "lea (,%reg,8), %reg }
  4541. else if (taicpu(p).opsize = S_L) and
  4542. (taicpu(p).oper[0]^.val<= 3) then
  4543. begin
  4544. reference_reset(tmpref,2,[]);
  4545. TmpRef.index := taicpu(p).oper[1]^.reg;
  4546. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  4547. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  4548. InsertLLItem(p.previous, p.next, hp1);
  4549. p.free;
  4550. p := hp1;
  4551. end;
  4552. end
  4553. {$endif x86_64}
  4554. else if
  4555. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  4556. (
  4557. (
  4558. MatchInstruction(hp1, A_AND, [taicpu(p).opsize]) and
  4559. SetAndTest(hp1, hp2)
  4560. {$ifdef x86_64}
  4561. ) or
  4562. (
  4563. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  4564. GetNextInstruction(hp1, hp2) and
  4565. MatchInstruction(hp2, A_AND, [taicpu(p).opsize]) and
  4566. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  4567. (taicpu(hp1).oper[1]^.reg = taicpu(hp2).oper[0]^.reg)
  4568. {$endif x86_64}
  4569. )
  4570. ) and
  4571. (taicpu(p).oper[1]^.reg = taicpu(hp2).oper[1]^.reg) then
  4572. begin
  4573. { Change:
  4574. shl x, %reg1
  4575. mov -(1<<x), %reg2
  4576. and %reg2, %reg1
  4577. Or:
  4578. shl x, %reg1
  4579. and -(1<<x), %reg1
  4580. To just:
  4581. shl x, %reg1
  4582. Since the and operation only zeroes bits that are already zero from the shl operation
  4583. }
  4584. case taicpu(p).oper[0]^.val of
  4585. 8:
  4586. mask:=$FFFFFFFFFFFFFF00;
  4587. 16:
  4588. mask:=$FFFFFFFFFFFF0000;
  4589. 32:
  4590. mask:=$FFFFFFFF00000000;
  4591. 63:
  4592. { Constant pre-calculated to prevent overflow errors with Int64 }
  4593. mask:=$8000000000000000;
  4594. else
  4595. begin
  4596. if taicpu(p).oper[0]^.val >= 64 then
  4597. { Shouldn't happen realistically, since the register
  4598. is guaranteed to be set to zero at this point }
  4599. mask := 0
  4600. else
  4601. mask := -(Int64(1 shl taicpu(p).oper[0]^.val));
  4602. end;
  4603. end;
  4604. if taicpu(hp1).oper[0]^.val = mask then
  4605. begin
  4606. { Everything checks out, perform the optimisation, as long as
  4607. the FLAGS register isn't being used}
  4608. TransferUsedRegs(TmpUsedRegs);
  4609. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4610. {$ifdef x86_64}
  4611. if (hp1 <> hp2) then
  4612. begin
  4613. { "shl/mov/and" version }
  4614. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  4615. { Don't do the optimisation if the FLAGS register is in use }
  4616. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)) then
  4617. begin
  4618. DebugMsg(SPeepholeOptimization + 'ShlMovAnd2Shl', p);
  4619. { Don't remove the 'mov' instruction if its register is used elsewhere }
  4620. if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp2, TmpUsedRegs)) then
  4621. begin
  4622. RemoveInstruction(hp1);
  4623. Result := True;
  4624. end;
  4625. { Only set Result to True if the 'mov' instruction was removed }
  4626. RemoveInstruction(hp2);
  4627. end;
  4628. end
  4629. else
  4630. {$endif x86_64}
  4631. begin
  4632. { "shl/and" version }
  4633. { Don't do the optimisation if the FLAGS register is in use }
  4634. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  4635. begin
  4636. DebugMsg(SPeepholeOptimization + 'ShlAnd2Shl', p);
  4637. RemoveInstruction(hp1);
  4638. Result := True;
  4639. end;
  4640. end;
  4641. Exit;
  4642. end
  4643. else {$ifdef x86_64}if (hp1 = hp2) then{$endif x86_64}
  4644. begin
  4645. { Even if the mask doesn't allow for its removal, we might be
  4646. able to optimise the mask for the "shl/and" version, which
  4647. may permit other peephole optimisations }
  4648. {$ifdef DEBUG_AOPTCPU}
  4649. mask := taicpu(hp1).oper[0]^.val and mask;
  4650. if taicpu(hp1).oper[0]^.val <> mask then
  4651. begin
  4652. DebugMsg(
  4653. SPeepholeOptimization +
  4654. 'Changed mask from $' + debug_tostr(taicpu(hp1).oper[0]^.val) +
  4655. ' to $' + debug_tostr(mask) +
  4656. 'based on previous instruction (ShlAnd2ShlAnd)', hp1);
  4657. taicpu(hp1).oper[0]^.val := mask;
  4658. end;
  4659. {$else DEBUG_AOPTCPU}
  4660. { If debugging is off, just set the operand even if it's the same }
  4661. taicpu(hp1).oper[0]^.val := taicpu(hp1).oper[0]^.val and mask;
  4662. {$endif DEBUG_AOPTCPU}
  4663. end;
  4664. end;
  4665. {
  4666. change
  4667. shl/sal const,reg
  4668. <op> ...(...,reg,1),...
  4669. into
  4670. <op> ...(...,reg,1 shl const),...
  4671. if const in 1..3
  4672. }
  4673. if MatchOpType(taicpu(p), top_const, top_reg) and
  4674. (taicpu(p).oper[0]^.val in [1..3]) and
  4675. GetNextInstruction(p, hp1) and
  4676. MatchInstruction(hp1,A_MOV,A_LEA,[]) and
  4677. MatchOpType(taicpu(hp1), top_ref, top_reg) and
  4678. (taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.index) and
  4679. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^.ref^.base) and
  4680. (taicpu(hp1).oper[0]^.ref^.scalefactor in [0,1]) then
  4681. begin
  4682. TransferUsedRegs(TmpUsedRegs);
  4683. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4684. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  4685. begin
  4686. taicpu(hp1).oper[0]^.ref^.scalefactor:=1 shl taicpu(p).oper[0]^.val;
  4687. DebugMsg(SPeepholeOptimization + 'ShlOp2Op', p);
  4688. RemoveCurrentP(p);
  4689. Result:=true;
  4690. end;
  4691. end;
  4692. end;
  4693. function TX86AsmOptimizer.CheckMemoryWrite(var first_mov, second_mov: taicpu): Boolean;
  4694. var
  4695. CurrentRef: TReference;
  4696. FullReg: TRegister;
  4697. hp1, hp2: tai;
  4698. begin
  4699. Result := False;
  4700. if (first_mov.opsize <> S_B) or (second_mov.opsize <> S_B) then
  4701. Exit;
  4702. { We assume you've checked if the operand is actually a reference by
  4703. this point. If it isn't, you'll most likely get an access violation }
  4704. CurrentRef := first_mov.oper[1]^.ref^;
  4705. { Memory must be aligned }
  4706. if (CurrentRef.offset mod 4) <> 0 then
  4707. Exit;
  4708. Inc(CurrentRef.offset);
  4709. CurrentRef.alignment := 1; { Otherwise references_equal will return False }
  4710. if MatchOperand(second_mov.oper[0]^, 0) and
  4711. references_equal(second_mov.oper[1]^.ref^, CurrentRef) and
  4712. GetNextInstruction(second_mov, hp1) and
  4713. (hp1.typ = ait_instruction) and
  4714. (taicpu(hp1).opcode = A_MOV) and
  4715. MatchOpType(taicpu(hp1), top_const, top_ref) and
  4716. (taicpu(hp1).oper[0]^.val = 0) then
  4717. begin
  4718. Inc(CurrentRef.offset);
  4719. CurrentRef.alignment := taicpu(hp1).oper[1]^.ref^.alignment; { Otherwise references_equal might return False }
  4720. FullReg := newreg(R_INTREGISTER,getsupreg(first_mov.oper[0]^.reg), R_SUBD);
  4721. if references_equal(taicpu(hp1).oper[1]^.ref^, CurrentRef) then
  4722. begin
  4723. case taicpu(hp1).opsize of
  4724. S_B:
  4725. if GetNextInstruction(hp1, hp2) and
  4726. MatchInstruction(taicpu(hp2), A_MOV, [S_B]) and
  4727. MatchOpType(taicpu(hp2), top_const, top_ref) and
  4728. (taicpu(hp2).oper[0]^.val = 0) then
  4729. begin
  4730. Inc(CurrentRef.offset);
  4731. CurrentRef.alignment := 1; { Otherwise references_equal will return False }
  4732. if references_equal(taicpu(hp2).oper[1]^.ref^, CurrentRef) and
  4733. (taicpu(hp2).opsize = S_B) then
  4734. begin
  4735. RemoveInstruction(hp1);
  4736. RemoveInstruction(hp2);
  4737. first_mov.opsize := S_L;
  4738. if first_mov.oper[0]^.typ = top_reg then
  4739. begin
  4740. DebugMsg(SPeepholeOptimization + 'MOVb/MOVb/MOVb/MOVb -> MOVZX/MOVl', first_mov);
  4741. { Reuse second_mov as a MOVZX instruction }
  4742. second_mov.opcode := A_MOVZX;
  4743. second_mov.opsize := S_BL;
  4744. second_mov.loadreg(0, first_mov.oper[0]^.reg);
  4745. second_mov.loadreg(1, FullReg);
  4746. first_mov.oper[0]^.reg := FullReg;
  4747. asml.Remove(second_mov);
  4748. asml.InsertBefore(second_mov, first_mov);
  4749. end
  4750. else
  4751. { It's a value }
  4752. begin
  4753. DebugMsg(SPeepholeOptimization + 'MOVb/MOVb/MOVb/MOVb -> MOVl', first_mov);
  4754. RemoveInstruction(second_mov);
  4755. end;
  4756. Result := True;
  4757. Exit;
  4758. end;
  4759. end;
  4760. S_W:
  4761. begin
  4762. RemoveInstruction(hp1);
  4763. first_mov.opsize := S_L;
  4764. if first_mov.oper[0]^.typ = top_reg then
  4765. begin
  4766. DebugMsg(SPeepholeOptimization + 'MOVb/MOVb/MOVw -> MOVZX/MOVl', first_mov);
  4767. { Reuse second_mov as a MOVZX instruction }
  4768. second_mov.opcode := A_MOVZX;
  4769. second_mov.opsize := S_BL;
  4770. second_mov.loadreg(0, first_mov.oper[0]^.reg);
  4771. second_mov.loadreg(1, FullReg);
  4772. first_mov.oper[0]^.reg := FullReg;
  4773. asml.Remove(second_mov);
  4774. asml.InsertBefore(second_mov, first_mov);
  4775. end
  4776. else
  4777. { It's a value }
  4778. begin
  4779. DebugMsg(SPeepholeOptimization + 'MOVb/MOVb/MOVw -> MOVl', first_mov);
  4780. RemoveInstruction(second_mov);
  4781. end;
  4782. Result := True;
  4783. Exit;
  4784. end;
  4785. else
  4786. ;
  4787. end;
  4788. end;
  4789. end;
  4790. end;
  4791. function TX86AsmOptimizer.OptPass1FSTP(var p: tai): boolean;
  4792. { returns true if a "continue" should be done after this optimization }
  4793. var
  4794. hp1, hp2: tai;
  4795. begin
  4796. Result := false;
  4797. if MatchOpType(taicpu(p),top_ref) and
  4798. GetNextInstruction(p, hp1) and
  4799. (hp1.typ = ait_instruction) and
  4800. (((taicpu(hp1).opcode = A_FLD) and
  4801. (taicpu(p).opcode = A_FSTP)) or
  4802. ((taicpu(p).opcode = A_FISTP) and
  4803. (taicpu(hp1).opcode = A_FILD))) and
  4804. MatchOpType(taicpu(hp1),top_ref) and
  4805. (taicpu(hp1).opsize = taicpu(p).opsize) and
  4806. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  4807. begin
  4808. { replacing fstp f;fld f by fst f is only valid for extended because of rounding or if fastmath is on }
  4809. if ((taicpu(p).opsize=S_FX) or (cs_opt_fastmath in current_settings.optimizerswitches)) and
  4810. GetNextInstruction(hp1, hp2) and
  4811. (hp2.typ = ait_instruction) and
  4812. IsExitCode(hp2) and
  4813. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  4814. not(assigned(current_procinfo.procdef.funcretsym) and
  4815. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  4816. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  4817. begin
  4818. RemoveInstruction(hp1);
  4819. RemoveCurrentP(p, hp2);
  4820. RemoveLastDeallocForFuncRes(p);
  4821. Result := true;
  4822. end
  4823. else
  4824. { we can do this only in fast math mode as fstp is rounding ...
  4825. ... still disabled as it breaks the compiler and/or rtl }
  4826. if ({ (cs_opt_fastmath in current_settings.optimizerswitches) or }
  4827. { ... or if another fstp equal to the first one follows }
  4828. (GetNextInstruction(hp1,hp2) and
  4829. (hp2.typ = ait_instruction) and
  4830. (taicpu(p).opcode=taicpu(hp2).opcode) and
  4831. (taicpu(p).opsize=taicpu(hp2).opsize))
  4832. ) and
  4833. { fst can't store an extended/comp value }
  4834. (taicpu(p).opsize <> S_FX) and
  4835. (taicpu(p).opsize <> S_IQ) then
  4836. begin
  4837. if (taicpu(p).opcode = A_FSTP) then
  4838. taicpu(p).opcode := A_FST
  4839. else
  4840. taicpu(p).opcode := A_FIST;
  4841. DebugMsg(SPeepholeOptimization + 'FstpFld2Fst',p);
  4842. RemoveInstruction(hp1);
  4843. end;
  4844. end;
  4845. end;
  4846. function TX86AsmOptimizer.OptPass1FLD(var p : tai) : boolean;
  4847. var
  4848. hp1, hp2: tai;
  4849. begin
  4850. result:=false;
  4851. if MatchOpType(taicpu(p),top_reg) and
  4852. GetNextInstruction(p, hp1) and
  4853. (hp1.typ = Ait_Instruction) and
  4854. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  4855. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  4856. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  4857. { change to
  4858. fld reg fxxx reg,st
  4859. fxxxp st, st1 (hp1)
  4860. Remark: non commutative operations must be reversed!
  4861. }
  4862. begin
  4863. case taicpu(hp1).opcode Of
  4864. A_FMULP,A_FADDP,
  4865. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  4866. begin
  4867. case taicpu(hp1).opcode Of
  4868. A_FADDP: taicpu(hp1).opcode := A_FADD;
  4869. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  4870. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  4871. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  4872. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  4873. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  4874. else
  4875. internalerror(2019050534);
  4876. end;
  4877. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  4878. taicpu(hp1).oper[1]^.reg := NR_ST;
  4879. RemoveCurrentP(p, hp1);
  4880. Result:=true;
  4881. exit;
  4882. end;
  4883. else
  4884. ;
  4885. end;
  4886. end
  4887. else
  4888. if MatchOpType(taicpu(p),top_ref) and
  4889. GetNextInstruction(p, hp2) and
  4890. (hp2.typ = Ait_Instruction) and
  4891. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  4892. (taicpu(p).opsize in [S_FS, S_FL]) and
  4893. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  4894. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  4895. if GetLastInstruction(p, hp1) and
  4896. MatchInstruction(hp1,A_FLD,A_FST,[taicpu(p).opsize]) and
  4897. MatchOpType(taicpu(hp1),top_ref) and
  4898. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  4899. if ((taicpu(hp2).opcode = A_FMULP) or
  4900. (taicpu(hp2).opcode = A_FADDP)) then
  4901. { change to
  4902. fld/fst mem1 (hp1) fld/fst mem1
  4903. fld mem1 (p) fadd/
  4904. faddp/ fmul st, st
  4905. fmulp st, st1 (hp2) }
  4906. begin
  4907. RemoveCurrentP(p, hp1);
  4908. if (taicpu(hp2).opcode = A_FADDP) then
  4909. taicpu(hp2).opcode := A_FADD
  4910. else
  4911. taicpu(hp2).opcode := A_FMUL;
  4912. taicpu(hp2).oper[1]^.reg := NR_ST;
  4913. end
  4914. else
  4915. { change to
  4916. fld/fst mem1 (hp1) fld/fst mem1
  4917. fld mem1 (p) fld st}
  4918. begin
  4919. taicpu(p).changeopsize(S_FL);
  4920. taicpu(p).loadreg(0,NR_ST);
  4921. end
  4922. else
  4923. begin
  4924. case taicpu(hp2).opcode Of
  4925. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  4926. { change to
  4927. fld/fst mem1 (hp1) fld/fst mem1
  4928. fld mem2 (p) fxxx mem2
  4929. fxxxp st, st1 (hp2) }
  4930. begin
  4931. case taicpu(hp2).opcode Of
  4932. A_FADDP: taicpu(p).opcode := A_FADD;
  4933. A_FMULP: taicpu(p).opcode := A_FMUL;
  4934. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  4935. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  4936. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  4937. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  4938. else
  4939. internalerror(2019050533);
  4940. end;
  4941. RemoveInstruction(hp2);
  4942. end
  4943. else
  4944. ;
  4945. end
  4946. end
  4947. end;
  4948. function IsCmpSubset(cond1, cond2: TAsmCond): Boolean; inline;
  4949. begin
  4950. Result := condition_in(cond1, cond2) or
  4951. { Not strictly subsets due to the actual flags checked, but because we're
  4952. comparing integers, E is a subset of AE and GE and their aliases }
  4953. ((cond1 in [C_E, C_Z]) and (cond2 in [C_AE, C_NB, C_NC, C_GE, C_NL]));
  4954. end;
  4955. function TX86AsmOptimizer.OptPass1Cmp(var p: tai): boolean;
  4956. var
  4957. v: TCGInt;
  4958. hp1, hp2, p_dist, p_jump, hp1_dist, p_label, hp1_label: tai;
  4959. FirstMatch: Boolean;
  4960. JumpLabel, JumpLabel_dist, JumpLabel_far: TAsmLabel;
  4961. begin
  4962. Result:=false;
  4963. { All these optimisations need a next instruction }
  4964. if not GetNextInstruction(p, hp1) then
  4965. Exit;
  4966. { Search for:
  4967. cmp ###,###
  4968. j(c1) @lbl1
  4969. ...
  4970. @lbl:
  4971. cmp ###.### (same comparison as above)
  4972. j(c2) @lbl2
  4973. If c1 is a subset of c2, change to:
  4974. cmp ###,###
  4975. j(c2) @lbl2
  4976. (@lbl1 may become a dead label as a result)
  4977. }
  4978. { Also handle cases where there are multiple jumps in a row }
  4979. p_jump := hp1;
  4980. while Assigned(p_jump) and MatchInstruction(p_jump, A_JCC, []) do
  4981. begin
  4982. if IsJumpToLabel(taicpu(p_jump)) then
  4983. begin
  4984. JumpLabel := TAsmLabel(taicpu(p_jump).oper[0]^.ref^.symbol);
  4985. p_label := nil;
  4986. if Assigned(JumpLabel) then
  4987. p_label := getlabelwithsym(JumpLabel);
  4988. if Assigned(p_label) and
  4989. GetNextInstruction(p_label, p_dist) and
  4990. MatchInstruction(p_dist, A_CMP, []) and
  4991. MatchOperand(taicpu(p_dist).oper[0]^, taicpu(p).oper[0]^) and
  4992. MatchOperand(taicpu(p_dist).oper[1]^, taicpu(p).oper[1]^) and
  4993. GetNextInstruction(p_dist, hp1_dist) and
  4994. MatchInstruction(hp1_dist, A_JCC, []) then { This doesn't have to be an explicit label }
  4995. begin
  4996. JumpLabel_dist := TAsmLabel(taicpu(hp1_dist).oper[0]^.ref^.symbol);
  4997. if JumpLabel = JumpLabel_dist then
  4998. { This is an infinite loop }
  4999. Exit;
  5000. { Best optimisation when the first condition is a subset (or equal) of the second }
  5001. if IsCmpSubset(taicpu(p_jump).condition, taicpu(hp1_dist).condition) then
  5002. begin
  5003. { Any registers used here will already be allocated }
  5004. if Assigned(JumpLabel_dist) then
  5005. JumpLabel_dist.IncRefs;
  5006. if Assigned(JumpLabel) then
  5007. JumpLabel.DecRefs;
  5008. DebugMsg(SPeepholeOptimization + 'CMP/Jcc/@Lbl/CMP/Jcc -> CMP/Jcc, redirecting first jump', p_jump);
  5009. taicpu(p_jump).condition := taicpu(hp1_dist).condition;
  5010. taicpu(p_jump).loadref(0, taicpu(hp1_dist).oper[0]^.ref^);
  5011. Result := True;
  5012. { Don't exit yet. Since p and p_jump haven't actually been
  5013. removed, we can check for more on this iteration }
  5014. end
  5015. else if IsCmpSubset(taicpu(hp1_dist).condition, inverse_cond(taicpu(p_jump).condition)) and
  5016. GetNextInstruction(hp1_dist, hp1_label) and
  5017. SkipAligns(hp1_label, hp1_label) and
  5018. (hp1_label.typ = ait_label) then
  5019. begin
  5020. JumpLabel_far := tai_label(hp1_label).labsym;
  5021. if (JumpLabel_far = JumpLabel_dist) or (JumpLabel_far = JumpLabel) then
  5022. { This is an infinite loop }
  5023. Exit;
  5024. if Assigned(JumpLabel_far) then
  5025. begin
  5026. { In this situation, if the first jump branches, the second one will never,
  5027. branch so change the destination label to after the second jump }
  5028. DebugMsg(SPeepholeOptimization + 'CMP/Jcc/@Lbl/CMP/Jcc/@Lbl -> CMP/Jcc, redirecting first jump to 2nd label', p_jump);
  5029. if Assigned(JumpLabel) then
  5030. JumpLabel.DecRefs;
  5031. JumpLabel_far.IncRefs;
  5032. taicpu(p_jump).oper[0]^.ref^.symbol := JumpLabel_far;
  5033. Result := True;
  5034. { Don't exit yet. Since p and p_jump haven't actually been
  5035. removed, we can check for more on this iteration }
  5036. Continue;
  5037. end;
  5038. end;
  5039. end;
  5040. end;
  5041. { Search for:
  5042. cmp ###,###
  5043. j(c1) @lbl1
  5044. cmp ###,### (same as first)
  5045. Remove second cmp
  5046. }
  5047. if GetNextInstruction(p_jump, hp2) and
  5048. (
  5049. (
  5050. MatchInstruction(hp2, A_CMP, []) and
  5051. (
  5052. (
  5053. MatchOpType(taicpu(p), top_const, top_reg) and
  5054. (taicpu(hp2).oper[0]^.val = taicpu(p).oper[0]^.val) and
  5055. SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp2).oper[1]^.reg)
  5056. ) or (
  5057. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  5058. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^)
  5059. )
  5060. )
  5061. ) or (
  5062. { Also match cmp $0,%reg; jcc @lbl; test %reg,%reg }
  5063. MatchOperand(taicpu(p).oper[0]^, 0) and
  5064. (taicpu(p).oper[1]^.typ = top_reg) and
  5065. MatchInstruction(hp2, A_TEST, []) and
  5066. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  5067. (taicpu(hp2).oper[0]^.reg = taicpu(hp2).oper[1]^.reg) and
  5068. SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp2).oper[1]^.reg)
  5069. )
  5070. ) then
  5071. begin
  5072. DebugMsg(SPeepholeOptimization + 'CMP/Jcc/CMP; removed superfluous CMP', hp2);
  5073. RemoveInstruction(hp2);
  5074. Result := True;
  5075. { Continue the while loop in case "Jcc/CMP" follows the second CMP that was just removed }
  5076. end;
  5077. GetNextInstruction(p_jump, p_jump);
  5078. end;
  5079. if taicpu(p).oper[0]^.typ = top_const then
  5080. begin
  5081. if (taicpu(p).oper[0]^.val = 0) and
  5082. (taicpu(p).oper[1]^.typ = top_reg) and
  5083. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) then
  5084. begin
  5085. hp2 := p;
  5086. FirstMatch := True;
  5087. { When dealing with "cmp $0,%reg", only ZF and SF contain
  5088. anything meaningful once it's converted to "test %reg,%reg";
  5089. additionally, some jumps will always (or never) branch, so
  5090. evaluate every jump immediately following the
  5091. comparison, optimising the conditions if possible.
  5092. Similarly with SETcc... those that are always set to 0 or 1
  5093. are changed to MOV instructions }
  5094. while FirstMatch or { Saves calling GetNextInstruction unnecessarily }
  5095. (
  5096. GetNextInstruction(hp2, hp1) and
  5097. MatchInstruction(hp1,A_Jcc,A_SETcc,[])
  5098. ) do
  5099. begin
  5100. FirstMatch := False;
  5101. case taicpu(hp1).condition of
  5102. C_B, C_C, C_NAE, C_O:
  5103. { For B/NAE:
  5104. Will never branch since an unsigned integer can never be below zero
  5105. For C/O:
  5106. Result cannot overflow because 0 is being subtracted
  5107. }
  5108. begin
  5109. if taicpu(hp1).opcode = A_Jcc then
  5110. begin
  5111. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (jump removed)', hp1);
  5112. TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
  5113. RemoveInstruction(hp1);
  5114. { Since hp1 was deleted, hp2 must not be updated }
  5115. Continue;
  5116. end
  5117. else
  5118. begin
  5119. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (set -> mov 0)', hp1);
  5120. { Convert "set(c) %reg" instruction to "movb 0,%reg" }
  5121. taicpu(hp1).opcode := A_MOV;
  5122. taicpu(hp1).ops := 2;
  5123. taicpu(hp1).condition := C_None;
  5124. taicpu(hp1).opsize := S_B;
  5125. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  5126. taicpu(hp1).loadconst(0, 0);
  5127. end;
  5128. end;
  5129. C_BE, C_NA:
  5130. begin
  5131. { Will only branch if equal to zero }
  5132. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition BE/NA --> E', hp1);
  5133. taicpu(hp1).condition := C_E;
  5134. end;
  5135. C_A, C_NBE:
  5136. begin
  5137. { Will only branch if not equal to zero }
  5138. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition A/NBE --> NE', hp1);
  5139. taicpu(hp1).condition := C_NE;
  5140. end;
  5141. C_AE, C_NB, C_NC, C_NO:
  5142. begin
  5143. { Will always branch }
  5144. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition AE/NB/NC/NO --> Always', hp1);
  5145. if taicpu(hp1).opcode = A_Jcc then
  5146. begin
  5147. MakeUnconditional(taicpu(hp1));
  5148. { Any jumps/set that follow will now be dead code }
  5149. RemoveDeadCodeAfterJump(taicpu(hp1));
  5150. Break;
  5151. end
  5152. else
  5153. begin
  5154. { Convert "set(c) %reg" instruction to "movb 1,%reg" }
  5155. taicpu(hp1).opcode := A_MOV;
  5156. taicpu(hp1).ops := 2;
  5157. taicpu(hp1).condition := C_None;
  5158. taicpu(hp1).opsize := S_B;
  5159. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  5160. taicpu(hp1).loadconst(0, 1);
  5161. end;
  5162. end;
  5163. C_None:
  5164. InternalError(2020012201);
  5165. C_P, C_PE, C_NP, C_PO:
  5166. { We can't handle parity checks and they should never be generated
  5167. after a general-purpose CMP (it's used in some floating-point
  5168. comparisons that don't use CMP) }
  5169. InternalError(2020012202);
  5170. else
  5171. { Zero/Equality, Sign, their complements and all of the
  5172. signed comparisons do not need to be converted };
  5173. end;
  5174. hp2 := hp1;
  5175. end;
  5176. { Convert the instruction to a TEST }
  5177. taicpu(p).opcode := A_TEST;
  5178. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  5179. Result := True;
  5180. Exit;
  5181. end
  5182. else if (taicpu(p).oper[0]^.val = 1) and
  5183. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  5184. (taicpu(hp1).condition in [C_L, C_NGE]) then
  5185. begin
  5186. { Convert; To:
  5187. cmp $1,r/m cmp $0,r/m
  5188. jl @lbl jle @lbl
  5189. }
  5190. DebugMsg(SPeepholeOptimization + 'Cmp1Jl2Cmp0Jle', p);
  5191. taicpu(p).oper[0]^.val := 0;
  5192. taicpu(hp1).condition := C_LE;
  5193. { If the instruction is now "cmp $0,%reg", convert it to a
  5194. TEST (and effectively do the work of the "cmp $0,%reg" in
  5195. the block above)
  5196. If it's a reference, we can get away with not setting
  5197. Result to True because he haven't evaluated the jump
  5198. in this pass yet.
  5199. }
  5200. if (taicpu(p).oper[1]^.typ = top_reg) then
  5201. begin
  5202. taicpu(p).opcode := A_TEST;
  5203. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  5204. Result := True;
  5205. end;
  5206. Exit;
  5207. end
  5208. else if (taicpu(p).oper[1]^.typ = top_reg)
  5209. {$ifdef x86_64}
  5210. and (taicpu(p).opsize <> S_Q) { S_Q will never happen: cmp with 64 bit constants is not possible }
  5211. {$endif x86_64}
  5212. then
  5213. begin
  5214. { cmp register,$8000 neg register
  5215. je target --> jo target
  5216. .... only if register is deallocated before jump.}
  5217. case Taicpu(p).opsize of
  5218. S_B: v:=$80;
  5219. S_W: v:=$8000;
  5220. S_L: v:=qword($80000000);
  5221. else
  5222. internalerror(2013112905);
  5223. end;
  5224. if (taicpu(p).oper[0]^.val=v) and
  5225. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  5226. (Taicpu(hp1).condition in [C_E,C_NE]) then
  5227. begin
  5228. TransferUsedRegs(TmpUsedRegs);
  5229. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  5230. if not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, TmpUsedRegs)) then
  5231. begin
  5232. DebugMsg(SPeepholeOptimization + 'CmpJe2NegJo done',p);
  5233. Taicpu(p).opcode:=A_NEG;
  5234. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  5235. Taicpu(p).clearop(1);
  5236. Taicpu(p).ops:=1;
  5237. if Taicpu(hp1).condition=C_E then
  5238. Taicpu(hp1).condition:=C_O
  5239. else
  5240. Taicpu(hp1).condition:=C_NO;
  5241. Result:=true;
  5242. exit;
  5243. end;
  5244. end;
  5245. end;
  5246. end;
  5247. if MatchInstruction(hp1,A_MOV,[]) and
  5248. (
  5249. (taicpu(p).oper[0]^.typ <> top_reg) or
  5250. not RegInInstruction(taicpu(p).oper[0]^.reg, hp1)
  5251. ) and
  5252. (
  5253. (taicpu(p).oper[1]^.typ <> top_reg) or
  5254. not RegInInstruction(taicpu(p).oper[1]^.reg, hp1)
  5255. ) and
  5256. (
  5257. { Make sure the register written to doesn't appear in the
  5258. cmp instruction (in a reference, say) }
  5259. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  5260. not RegInInstruction(taicpu(hp1).oper[1]^.reg, p)
  5261. ) then
  5262. begin
  5263. { If we have something like:
  5264. cmp ###,%reg1
  5265. mov 0,%reg2
  5266. And no registers are shared, move the MOV command to before the
  5267. comparison as this means it can be optimised without worrying
  5268. about the FLAGS register. (This combination is generated by
  5269. "J(c)Mov1JmpMov0 -> Set(~c)", among other things).
  5270. }
  5271. SwapMovCmp(p, hp1);
  5272. Result := True;
  5273. Exit;
  5274. end;
  5275. end;
  5276. function TX86AsmOptimizer.OptPass1PXor(var p: tai): boolean;
  5277. var
  5278. hp1: tai;
  5279. begin
  5280. {
  5281. remove the second (v)pxor from
  5282. pxor reg,reg
  5283. ...
  5284. pxor reg,reg
  5285. }
  5286. Result:=false;
  5287. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  5288. MatchOpType(taicpu(p),top_reg,top_reg) and
  5289. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  5290. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  5291. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  5292. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) then
  5293. begin
  5294. DebugMsg(SPeepholeOptimization + 'PXorPXor2PXor done',hp1);
  5295. RemoveInstruction(hp1);
  5296. Result:=true;
  5297. Exit;
  5298. end
  5299. {
  5300. replace
  5301. pxor reg1,reg1
  5302. movapd/s reg1,reg2
  5303. dealloc reg1
  5304. by
  5305. pxor reg2,reg2
  5306. }
  5307. else if GetNextInstruction(p,hp1) and
  5308. { we mix single and double opperations here because we assume that the compiler
  5309. generates vmovapd only after double operations and vmovaps only after single operations }
  5310. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  5311. MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  5312. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  5313. (taicpu(p).oper[0]^.typ=top_reg) then
  5314. begin
  5315. TransferUsedRegs(TmpUsedRegs);
  5316. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5317. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  5318. begin
  5319. taicpu(p).loadoper(0,taicpu(hp1).oper[1]^);
  5320. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  5321. DebugMsg(SPeepholeOptimization + 'PXorMovapd2PXor done',p);
  5322. RemoveInstruction(hp1);
  5323. result:=true;
  5324. end;
  5325. end;
  5326. end;
  5327. function TX86AsmOptimizer.OptPass1VPXor(var p: tai): boolean;
  5328. var
  5329. hp1: tai;
  5330. begin
  5331. {
  5332. remove the second (v)pxor from
  5333. (v)pxor reg,reg
  5334. ...
  5335. (v)pxor reg,reg
  5336. }
  5337. Result:=false;
  5338. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^,taicpu(p).oper[2]^) and
  5339. MatchOpType(taicpu(p),top_reg,top_reg,top_reg) and
  5340. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  5341. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  5342. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  5343. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^,taicpu(hp1).oper[2]^) then
  5344. begin
  5345. DebugMsg(SPeepholeOptimization + 'VPXorVPXor2PXor done',hp1);
  5346. RemoveInstruction(hp1);
  5347. Result:=true;
  5348. Exit;
  5349. end
  5350. else
  5351. Result:=OptPass1VOP(p);
  5352. end;
  5353. function TX86AsmOptimizer.OptPass1Imul(var p: tai): boolean;
  5354. var
  5355. hp1 : tai;
  5356. begin
  5357. result:=false;
  5358. { replace
  5359. IMul const,%mreg1,%mreg2
  5360. Mov %reg2,%mreg3
  5361. dealloc %mreg3
  5362. by
  5363. Imul const,%mreg1,%mreg23
  5364. }
  5365. if (taicpu(p).ops=3) and
  5366. GetNextInstruction(p,hp1) and
  5367. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  5368. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  5369. (taicpu(hp1).oper[1]^.typ=top_reg) then
  5370. begin
  5371. TransferUsedRegs(TmpUsedRegs);
  5372. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5373. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  5374. begin
  5375. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  5376. DebugMsg(SPeepholeOptimization + 'ImulMov2Imul done',p);
  5377. RemoveInstruction(hp1);
  5378. result:=true;
  5379. end;
  5380. end;
  5381. end;
  5382. function TX86AsmOptimizer.OptPass1SHXX(var p: tai): boolean;
  5383. var
  5384. hp1 : tai;
  5385. begin
  5386. result:=false;
  5387. { replace
  5388. IMul %reg0,%reg1,%reg2
  5389. Mov %reg2,%reg3
  5390. dealloc %reg2
  5391. by
  5392. Imul %reg0,%reg1,%reg3
  5393. }
  5394. if GetNextInstruction(p,hp1) and
  5395. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  5396. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  5397. (taicpu(hp1).oper[1]^.typ=top_reg) then
  5398. begin
  5399. TransferUsedRegs(TmpUsedRegs);
  5400. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5401. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  5402. begin
  5403. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  5404. DebugMsg(SPeepholeOptimization + 'SHXXMov2SHXX done',p);
  5405. RemoveInstruction(hp1);
  5406. result:=true;
  5407. end;
  5408. end;
  5409. end;
  5410. function TX86AsmOptimizer.OptPass1_V_Cvtss2sd(var p: tai): boolean;
  5411. var
  5412. hp1: tai;
  5413. begin
  5414. Result:=false;
  5415. { get rid of
  5416. (v)cvtss2sd reg0,<reg1,>reg2
  5417. (v)cvtss2sd reg2,<reg2,>reg0
  5418. }
  5419. if GetNextInstruction(p,hp1) and
  5420. (((taicpu(p).opcode=A_CVTSS2SD) and MatchInstruction(hp1,A_CVTSD2SS,[taicpu(p).opsize]) and
  5421. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^)) or
  5422. ((taicpu(p).opcode=A_VCVTSS2SD) and MatchInstruction(hp1,A_VCVTSD2SS,[taicpu(p).opsize]) and
  5423. MatchOpType(taicpu(p),top_reg,top_reg,top_reg) and
  5424. MatchOpType(taicpu(hp1),top_reg,top_reg,top_reg) and
  5425. (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  5426. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  5427. (getsupreg(taicpu(p).oper[2]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg))
  5428. )
  5429. ) then
  5430. begin
  5431. if getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[2]^.reg) then
  5432. begin
  5433. DebugMsg(SPeepholeOptimization + '(V)Cvtss2CvtSd(V)Cvtsd2ss2Nop done',p);
  5434. RemoveCurrentP(p);
  5435. RemoveInstruction(hp1);
  5436. end
  5437. else
  5438. begin
  5439. DebugMsg(SPeepholeOptimization + '(V)Cvtss2CvtSd(V)Cvtsd2ss2Vmovaps done',p);
  5440. taicpu(p).loadreg(1,taicpu(hp1).oper[2]^.reg);
  5441. taicpu(p).ops:=2;
  5442. taicpu(p).opcode:=A_VMOVAPS;
  5443. RemoveInstruction(hp1);
  5444. end;
  5445. Result:=true;
  5446. Exit;
  5447. end;
  5448. end;
  5449. function TX86AsmOptimizer.OptPass1Jcc(var p : tai) : boolean;
  5450. var
  5451. hp1, hp2, hp3, hp4, hp5: tai;
  5452. ThisReg: TRegister;
  5453. begin
  5454. Result := False;
  5455. if not GetNextInstruction(p,hp1) or (hp1.typ <> ait_instruction) then
  5456. Exit;
  5457. {
  5458. convert
  5459. j<c> .L1
  5460. mov 1,reg
  5461. jmp .L2
  5462. .L1
  5463. mov 0,reg
  5464. .L2
  5465. into
  5466. mov 0,reg
  5467. set<not(c)> reg
  5468. take care of alignment and that the mov 0,reg is not converted into a xor as this
  5469. would destroy the flag contents
  5470. Use MOVZX if size is preferred, since while mov 0,reg is bigger, it can be
  5471. executed at the same time as a previous comparison.
  5472. set<not(c)> reg
  5473. movzx reg, reg
  5474. }
  5475. if MatchInstruction(hp1,A_MOV,[]) and
  5476. (taicpu(hp1).oper[0]^.typ = top_const) and
  5477. (
  5478. (
  5479. (taicpu(hp1).oper[1]^.typ = top_reg)
  5480. {$ifdef i386}
  5481. { Under i386, ESI, EDI, EBP and ESP
  5482. don't have an 8-bit representation }
  5483. and not (getsupreg(taicpu(hp1).oper[1]^.reg) in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  5484. {$endif i386}
  5485. ) or (
  5486. {$ifdef i386}
  5487. (taicpu(hp1).oper[1]^.typ <> top_reg) and
  5488. {$endif i386}
  5489. (taicpu(hp1).opsize = S_B)
  5490. )
  5491. ) and
  5492. GetNextInstruction(hp1,hp2) and
  5493. MatchInstruction(hp2,A_JMP,[]) and (taicpu(hp2).oper[0]^.ref^.refaddr=addr_full) and
  5494. GetNextInstruction(hp2,hp3) and
  5495. SkipAligns(hp3, hp3) and
  5496. (hp3.typ=ait_label) and
  5497. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol)=tai_label(hp3).labsym) and
  5498. GetNextInstruction(hp3,hp4) and
  5499. MatchInstruction(hp4,A_MOV,[taicpu(hp1).opsize]) and
  5500. (taicpu(hp4).oper[0]^.typ = top_const) and
  5501. (
  5502. ((taicpu(hp1).oper[0]^.val = 0) and (taicpu(hp4).oper[0]^.val = 1)) or
  5503. ((taicpu(hp1).oper[0]^.val = 1) and (taicpu(hp4).oper[0]^.val = 0))
  5504. ) and
  5505. MatchOperand(taicpu(hp1).oper[1]^,taicpu(hp4).oper[1]^) and
  5506. GetNextInstruction(hp4,hp5) and
  5507. SkipAligns(hp5, hp5) and
  5508. (hp5.typ=ait_label) and
  5509. (tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol)=tai_label(hp5).labsym) then
  5510. begin
  5511. if (taicpu(hp1).oper[0]^.val = 1) and (taicpu(hp4).oper[0]^.val = 0) then
  5512. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  5513. tai_label(hp3).labsym.DecRefs;
  5514. { If this isn't the only reference to the middle label, we can
  5515. still make a saving - only that the first jump and everything
  5516. that follows will remain. }
  5517. if (tai_label(hp3).labsym.getrefs = 0) then
  5518. begin
  5519. if (taicpu(hp1).oper[0]^.val = 1) and (taicpu(hp4).oper[0]^.val = 0) then
  5520. DebugMsg(SPeepholeOptimization + 'J(c)Mov1JmpMov0 -> Set(~c)',p)
  5521. else
  5522. DebugMsg(SPeepholeOptimization + 'J(c)Mov0JmpMov1 -> Set(c)',p);
  5523. { remove jump, first label and second MOV (also catching any aligns) }
  5524. repeat
  5525. if not GetNextInstruction(hp2, hp3) then
  5526. InternalError(2021040810);
  5527. RemoveInstruction(hp2);
  5528. hp2 := hp3;
  5529. until hp2 = hp5;
  5530. { Don't decrement reference count before the removal loop
  5531. above, otherwise GetNextInstruction won't stop on the
  5532. the label }
  5533. tai_label(hp5).labsym.DecRefs;
  5534. end
  5535. else
  5536. begin
  5537. if (taicpu(hp1).oper[0]^.val = 1) and (taicpu(hp4).oper[0]^.val = 0) then
  5538. DebugMsg(SPeepholeOptimization + 'J(c)Mov1JmpMov0 -> Set(~c) (partial)',p)
  5539. else
  5540. DebugMsg(SPeepholeOptimization + 'J(c)Mov0JmpMov1 -> Set(c) (partial)',p);
  5541. end;
  5542. taicpu(p).opcode:=A_SETcc;
  5543. taicpu(p).opsize:=S_B;
  5544. taicpu(p).is_jmp:=False;
  5545. if taicpu(hp1).opsize=S_B then
  5546. begin
  5547. taicpu(p).loadoper(0, taicpu(hp1).oper[1]^);
  5548. if taicpu(hp1).oper[1]^.typ = top_reg then
  5549. AllocRegBetween(taicpu(hp1).oper[1]^.reg, p, hp2, UsedRegs);
  5550. RemoveInstruction(hp1);
  5551. end
  5552. else
  5553. begin
  5554. { Will be a register because the size can't be S_B otherwise }
  5555. ThisReg := newreg(R_INTREGISTER,getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBL);
  5556. taicpu(p).loadreg(0, ThisReg);
  5557. AllocRegBetween(ThisReg, p, hp2, UsedRegs);
  5558. if (cs_opt_size in current_settings.optimizerswitches) and IsMOVZXAcceptable then
  5559. begin
  5560. case taicpu(hp1).opsize of
  5561. S_W:
  5562. taicpu(hp1).opsize := S_BW;
  5563. S_L:
  5564. taicpu(hp1).opsize := S_BL;
  5565. {$ifdef x86_64}
  5566. S_Q:
  5567. begin
  5568. taicpu(hp1).opsize := S_BL;
  5569. { Change the destination register to 32-bit }
  5570. taicpu(hp1).loadreg(1, newreg(R_INTREGISTER,getsupreg(ThisReg), R_SUBD));
  5571. end;
  5572. {$endif x86_64}
  5573. else
  5574. InternalError(2021040820);
  5575. end;
  5576. taicpu(hp1).opcode := A_MOVZX;
  5577. taicpu(hp1).loadreg(0, ThisReg);
  5578. end
  5579. else
  5580. begin
  5581. AllocRegBetween(NR_FLAGS,p,hp1,UsedRegs);
  5582. { hp1 is already a MOV instruction with the correct register }
  5583. taicpu(hp1).loadconst(0, 0);
  5584. { Inserting it right before p will guarantee that the flags are also tracked }
  5585. asml.Remove(hp1);
  5586. asml.InsertBefore(hp1, p);
  5587. end;
  5588. end;
  5589. Result:=true;
  5590. exit;
  5591. end
  5592. end;
  5593. function TX86AsmOptimizer.OptPass1VMOVDQ(var p: tai): Boolean;
  5594. var
  5595. hp1, hp2, hp3: tai;
  5596. SourceRef, TargetRef: TReference;
  5597. CurrentReg: TRegister;
  5598. begin
  5599. { VMOVDQU/CMOVDQA shouldn't have even been generated }
  5600. if not UseAVX then
  5601. InternalError(2021100501);
  5602. Result := False;
  5603. { Look for the following to simplify:
  5604. vmovdqa/u x(mem1), %xmmreg
  5605. vmovdqa/u %xmmreg, y(mem2)
  5606. vmovdqa/u x+16(mem1), %xmmreg
  5607. vmovdqa/u %xmmreg, y+16(mem2)
  5608. Change to:
  5609. vmovdqa/u x(mem1), %ymmreg
  5610. vmovdqa/u %ymmreg, y(mem2)
  5611. vpxor %ymmreg, %ymmreg, %ymmreg
  5612. ( The VPXOR instruction is to zero the upper half, thus removing the
  5613. need to call the potentially expensive VZEROUPPER instruction. Other
  5614. peephole optimisations can remove VPXOR if it's unnecessary )
  5615. }
  5616. TransferUsedRegs(TmpUsedRegs);
  5617. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  5618. { NOTE: In the optimisations below, if the references dictate that an
  5619. aligned move is possible (i.e. VMOVDQA), the existing instructions
  5620. should already be VMOVDQA because if (x mod 32) = 0, then (x mod 16) = 0 }
  5621. if (taicpu(p).opsize = S_XMM) and
  5622. MatchOpType(taicpu(p), top_ref, top_reg) and
  5623. GetNextInstruction(p, hp1) and
  5624. MatchInstruction(hp1, A_VMOVDQA, A_VMOVDQU, [S_XMM]) and
  5625. MatchOpType(taicpu(hp1), top_reg, top_ref) and
  5626. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) then
  5627. begin
  5628. SourceRef := taicpu(p).oper[0]^.ref^;
  5629. TargetRef := taicpu(hp1).oper[1]^.ref^;
  5630. if GetNextInstruction(hp1, hp2) and
  5631. MatchInstruction(hp2, A_VMOVDQA, A_VMOVDQU, [S_XMM]) and
  5632. MatchOpType(taicpu(hp2), top_ref, top_reg) then
  5633. begin
  5634. { Delay calling GetNextInstruction(hp2, hp3) for as long as possible }
  5635. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  5636. Inc(SourceRef.offset, 16);
  5637. { Reuse the register in the first block move }
  5638. CurrentReg := newreg(R_MMREGISTER, getsupreg(taicpu(p).oper[1]^.reg), R_SUBMMY);
  5639. if RefsEqual(SourceRef, taicpu(hp2).oper[0]^.ref^) then
  5640. begin
  5641. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  5642. Inc(TargetRef.offset, 16);
  5643. if GetNextInstruction(hp2, hp3) and
  5644. MatchInstruction(hp3, A_VMOVDQA, A_VMOVDQU, [S_XMM]) and
  5645. MatchOpType(taicpu(hp3), top_reg, top_ref) and
  5646. (taicpu(hp2).oper[1]^.reg = taicpu(hp3).oper[0]^.reg) and
  5647. RefsEqual(TargetRef, taicpu(hp3).oper[1]^.ref^) and
  5648. not RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp3, TmpUsedRegs) then
  5649. begin
  5650. { Update the register tracking to the new size }
  5651. AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
  5652. { Remember that the offsets are 16 ahead }
  5653. { Switch to unaligned if the memory isn't on a 32-byte boundary }
  5654. if not (
  5655. ((SourceRef.offset mod 32) = 16) and
  5656. (SourceRef.alignment >= 32) and ((SourceRef.alignment mod 32) = 0)
  5657. ) then
  5658. taicpu(p).opcode := A_VMOVDQU;
  5659. taicpu(p).opsize := S_YMM;
  5660. taicpu(p).oper[1]^.reg := CurrentReg;
  5661. if not (
  5662. ((TargetRef.offset mod 32) = 16) and
  5663. (TargetRef.alignment >= 32) and ((TargetRef.alignment mod 32) = 0)
  5664. ) then
  5665. taicpu(hp1).opcode := A_VMOVDQU;
  5666. taicpu(hp1).opsize := S_YMM;
  5667. taicpu(hp1).oper[0]^.reg := CurrentReg;
  5668. DebugMsg(SPeepholeOptimization + 'Used ' + debug_regname(CurrentReg) + ' to merge a pair of memory moves (VmovdqxVmovdqxVmovdqxVmovdqx2VmovdqyVmovdqy 1)', p);
  5669. { If pi_uses_ymm is set, VZEROUPPER is present to do this for us }
  5670. if (pi_uses_ymm in current_procinfo.flags) then
  5671. RemoveInstruction(hp2)
  5672. else
  5673. begin
  5674. taicpu(hp2).opcode := A_VPXOR;
  5675. taicpu(hp2).opsize := S_YMM;
  5676. taicpu(hp2).loadreg(0, CurrentReg);
  5677. taicpu(hp2).loadreg(1, CurrentReg);
  5678. taicpu(hp2).loadreg(2, CurrentReg);
  5679. taicpu(hp2).ops := 3;
  5680. end;
  5681. RemoveInstruction(hp3);
  5682. Result := True;
  5683. Exit;
  5684. end;
  5685. end
  5686. else
  5687. begin
  5688. { See if the next references are 16 less rather than 16 greater }
  5689. Dec(SourceRef.offset, 32); { -16 the other way }
  5690. if RefsEqual(SourceRef, taicpu(hp2).oper[0]^.ref^) then
  5691. begin
  5692. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  5693. Dec(TargetRef.offset, 16); { Only 16, not 32, as it wasn't incremented unlike SourceRef }
  5694. if GetNextInstruction(hp2, hp3) and
  5695. MatchInstruction(hp3, A_MOV, [taicpu(p).opsize]) and
  5696. MatchOpType(taicpu(hp3), top_reg, top_ref) and
  5697. (taicpu(hp2).oper[1]^.reg = taicpu(hp3).oper[0]^.reg) and
  5698. RefsEqual(TargetRef, taicpu(hp3).oper[1]^.ref^) and
  5699. not RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp3, TmpUsedRegs) then
  5700. begin
  5701. { Update the register tracking to the new size }
  5702. AllocRegBetween(CurrentReg, hp2, hp3, UsedRegs);
  5703. { hp2 and hp3 are the starting offsets, so mod = 0 this time }
  5704. { Switch to unaligned if the memory isn't on a 32-byte boundary }
  5705. if not(
  5706. ((SourceRef.offset mod 32) = 0) and
  5707. (SourceRef.alignment >= 32) and ((SourceRef.alignment mod 32) = 0)
  5708. ) then
  5709. taicpu(hp2).opcode := A_VMOVDQU;
  5710. taicpu(hp2).opsize := S_YMM;
  5711. taicpu(hp2).oper[1]^.reg := CurrentReg;
  5712. if not (
  5713. ((TargetRef.offset mod 32) = 0) and
  5714. (TargetRef.alignment >= 32) and ((TargetRef.alignment mod 32) = 0)
  5715. ) then
  5716. taicpu(hp3).opcode := A_VMOVDQU;
  5717. taicpu(hp3).opsize := S_YMM;
  5718. taicpu(hp3).oper[0]^.reg := CurrentReg;
  5719. DebugMsg(SPeepholeOptimization + 'Used ' + debug_regname(CurrentReg) + ' to merge a pair of memory moves (VmovdqxVmovdqxVmovdqxVmovdqx2VmovdqyVmovdqy 2)', p);
  5720. { If pi_uses_ymm is set, VZEROUPPER is present to do this for us }
  5721. if (pi_uses_ymm in current_procinfo.flags) then
  5722. RemoveInstruction(hp1)
  5723. else
  5724. begin
  5725. taicpu(hp1).opcode := A_VPXOR;
  5726. taicpu(hp1).opsize := S_YMM;
  5727. taicpu(hp1).loadreg(0, CurrentReg);
  5728. taicpu(hp1).loadreg(1, CurrentReg);
  5729. taicpu(hp1).loadreg(2, CurrentReg);
  5730. taicpu(hp1).ops := 3;
  5731. Asml.Remove(hp1);
  5732. Asml.InsertAfter(hp1, hp3); { Register deallocations will be after hp3 }
  5733. end;
  5734. RemoveCurrentP(p, hp2);
  5735. Result := True;
  5736. Exit;
  5737. end;
  5738. end;
  5739. end;
  5740. end;
  5741. end;
  5742. end;
  5743. function TX86AsmOptimizer.CheckJumpMovTransferOpt(var p: tai; hp1: tai; LoopCount: Integer; out Count: Integer): Boolean;
  5744. var
  5745. hp2, hp3, first_assignment: tai;
  5746. IncCount, OperIdx: Integer;
  5747. OrigLabel: TAsmLabel;
  5748. begin
  5749. Count := 0;
  5750. Result := False;
  5751. first_assignment := nil;
  5752. if (LoopCount >= 20) then
  5753. begin
  5754. { Guard against infinite loops }
  5755. Exit;
  5756. end;
  5757. if (taicpu(p).oper[0]^.typ <> top_ref) or
  5758. (taicpu(p).oper[0]^.ref^.refaddr <> addr_full) or
  5759. (taicpu(p).oper[0]^.ref^.base <> NR_NO) or
  5760. (taicpu(p).oper[0]^.ref^.index <> NR_NO) or
  5761. not (taicpu(p).oper[0]^.ref^.symbol is TAsmLabel) then
  5762. Exit;
  5763. OrigLabel := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  5764. {
  5765. change
  5766. jmp .L1
  5767. ...
  5768. .L1:
  5769. mov ##, ## ( multiple movs possible )
  5770. jmp/ret
  5771. into
  5772. mov ##, ##
  5773. jmp/ret
  5774. }
  5775. if not Assigned(hp1) then
  5776. begin
  5777. hp1 := GetLabelWithSym(OrigLabel);
  5778. if not Assigned(hp1) or not SkipLabels(hp1, hp1) then
  5779. Exit;
  5780. end;
  5781. hp2 := hp1;
  5782. while Assigned(hp2) do
  5783. begin
  5784. if Assigned(hp2) and (hp2.typ in [ait_label, ait_align]) then
  5785. SkipLabels(hp2,hp2);
  5786. if not Assigned(hp2) or (hp2.typ <> ait_instruction) then
  5787. Break;
  5788. case taicpu(hp2).opcode of
  5789. A_MOVSS:
  5790. begin
  5791. if taicpu(hp2).ops = 0 then
  5792. { Wrong MOVSS }
  5793. Break;
  5794. Inc(Count);
  5795. if Count >= 5 then
  5796. { Too many to be worthwhile }
  5797. Break;
  5798. GetNextInstruction(hp2, hp2);
  5799. Continue;
  5800. end;
  5801. A_MOV,
  5802. A_MOVD,
  5803. A_MOVQ,
  5804. A_MOVSX,
  5805. {$ifdef x86_64}
  5806. A_MOVSXD,
  5807. {$endif x86_64}
  5808. A_MOVZX,
  5809. A_MOVAPS,
  5810. A_MOVUPS,
  5811. A_MOVSD,
  5812. A_MOVAPD,
  5813. A_MOVUPD,
  5814. A_MOVDQA,
  5815. A_MOVDQU,
  5816. A_VMOVSS,
  5817. A_VMOVAPS,
  5818. A_VMOVUPS,
  5819. A_VMOVSD,
  5820. A_VMOVAPD,
  5821. A_VMOVUPD,
  5822. A_VMOVDQA,
  5823. A_VMOVDQU:
  5824. begin
  5825. Inc(Count);
  5826. if Count >= 5 then
  5827. { Too many to be worthwhile }
  5828. Break;
  5829. GetNextInstruction(hp2, hp2);
  5830. Continue;
  5831. end;
  5832. A_JMP:
  5833. begin
  5834. { Guard against infinite loops }
  5835. if taicpu(hp2).oper[0]^.ref^.symbol = OrigLabel then
  5836. Exit;
  5837. { Analyse this jump first in case it also duplicates assignments }
  5838. if CheckJumpMovTransferOpt(hp2, nil, LoopCount + 1, IncCount) then
  5839. begin
  5840. { Something did change! }
  5841. Result := True;
  5842. Inc(Count, IncCount);
  5843. if Count >= 5 then
  5844. begin
  5845. { Too many to be worthwhile }
  5846. Exit;
  5847. end;
  5848. if MatchInstruction(hp2, [A_JMP, A_RET], []) then
  5849. Break;
  5850. end;
  5851. Result := True;
  5852. Break;
  5853. end;
  5854. A_RET:
  5855. begin
  5856. Result := True;
  5857. Break;
  5858. end;
  5859. else
  5860. Break;
  5861. end;
  5862. end;
  5863. if Result then
  5864. begin
  5865. { A count of zero can happen when CheckJumpMovTransferOpt is called recursively }
  5866. if Count = 0 then
  5867. begin
  5868. Result := False;
  5869. Exit;
  5870. end;
  5871. hp3 := p;
  5872. DebugMsg(SPeepholeOptimization + 'Duplicated ' + debug_tostr(Count) + ' assignment(s) and redirected jump', p);
  5873. while True do
  5874. begin
  5875. if Assigned(hp1) and (hp1.typ in [ait_label, ait_align]) then
  5876. SkipLabels(hp1,hp1);
  5877. if (hp1.typ <> ait_instruction) then
  5878. InternalError(2021040720);
  5879. case taicpu(hp1).opcode of
  5880. A_JMP:
  5881. begin
  5882. { Change the original jump to the new destination }
  5883. OrigLabel.decrefs;
  5884. taicpu(hp1).oper[0]^.ref^.symbol.increfs;
  5885. taicpu(p).loadref(0, taicpu(hp1).oper[0]^.ref^);
  5886. { Set p to the first duplicated assignment so it can get optimised if needs be }
  5887. if not Assigned(first_assignment) then
  5888. InternalError(2021040810)
  5889. else
  5890. p := first_assignment;
  5891. Exit;
  5892. end;
  5893. A_RET:
  5894. begin
  5895. { Now change the jump into a RET instruction }
  5896. ConvertJumpToRET(p, hp1);
  5897. { Set p to the first duplicated assignment so it can get optimised if needs be }
  5898. if not Assigned(first_assignment) then
  5899. InternalError(2021040811)
  5900. else
  5901. p := first_assignment;
  5902. Exit;
  5903. end;
  5904. else
  5905. begin
  5906. { Duplicate the MOV instruction }
  5907. hp3:=tai(hp1.getcopy);
  5908. if first_assignment = nil then
  5909. first_assignment := hp3;
  5910. asml.InsertBefore(hp3, p);
  5911. { Make sure the compiler knows about any final registers written here }
  5912. for OperIdx := 0 to taicpu(hp3).ops - 1 do
  5913. with taicpu(hp3).oper[OperIdx]^ do
  5914. begin
  5915. case typ of
  5916. top_ref:
  5917. begin
  5918. if (ref^.base <> NR_NO) and
  5919. (getsupreg(ref^.base) <> RS_ESP) and
  5920. (getsupreg(ref^.base) <> RS_EBP)
  5921. {$ifdef x86_64} and (ref^.base <> NR_RIP) {$endif x86_64}
  5922. then
  5923. AllocRegBetween(ref^.base, hp3, tai(p.Next), UsedRegs);
  5924. if (ref^.index <> NR_NO) and
  5925. (getsupreg(ref^.index) <> RS_ESP) and
  5926. (getsupreg(ref^.index) <> RS_EBP)
  5927. {$ifdef x86_64} and (ref^.index <> NR_RIP) {$endif x86_64} and
  5928. (ref^.index <> ref^.base) then
  5929. AllocRegBetween(ref^.index, hp3, tai(p.Next), UsedRegs);
  5930. end;
  5931. top_reg:
  5932. AllocRegBetween(reg, hp3, tai(p.Next), UsedRegs);
  5933. else
  5934. ;
  5935. end;
  5936. end;
  5937. end;
  5938. end;
  5939. if not GetNextInstruction(hp1, hp1) then
  5940. { Should have dropped out earlier }
  5941. InternalError(2021040710);
  5942. end;
  5943. end;
  5944. end;
  5945. procedure TX86AsmOptimizer.SwapMovCmp(var p, hp1: tai);
  5946. var
  5947. hp2: tai;
  5948. X: Integer;
  5949. begin
  5950. asml.Remove(hp1);
  5951. { Try to insert after the last instructions where the FLAGS register is not yet in use }
  5952. if not GetLastInstruction(p, hp2) then
  5953. asml.InsertBefore(hp1, p)
  5954. else
  5955. asml.InsertAfter(hp1, hp2);
  5956. DebugMsg(SPeepholeOptimization + 'Swapped ' + debug_op2str(taicpu(p).opcode) + ' and mov instructions to improve optimisation potential', hp1);
  5957. for X := 0 to 1 do
  5958. case taicpu(hp1).oper[X]^.typ of
  5959. top_reg:
  5960. AllocRegBetween(taicpu(hp1).oper[X]^.reg, hp1, p, UsedRegs);
  5961. top_ref:
  5962. begin
  5963. if taicpu(hp1).oper[X]^.ref^.base <> NR_NO then
  5964. AllocRegBetween(taicpu(hp1).oper[X]^.ref^.base, hp1, p, UsedRegs);
  5965. if taicpu(hp1).oper[X]^.ref^.index <> NR_NO then
  5966. AllocRegBetween(taicpu(hp1).oper[X]^.ref^.index, hp1, p, UsedRegs);
  5967. end;
  5968. else
  5969. ;
  5970. end;
  5971. end;
  5972. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  5973. function IsXCHGAcceptable: Boolean; inline;
  5974. begin
  5975. { Always accept if optimising for size }
  5976. Result := (cs_opt_size in current_settings.optimizerswitches) or
  5977. (
  5978. {$ifdef x86_64}
  5979. { XCHG takes 3 cycles on AMD Athlon64 }
  5980. (current_settings.optimizecputype >= cpu_core_i)
  5981. {$else x86_64}
  5982. { From the Pentium M onwards, XCHG only has a latency of 2 rather
  5983. than 3, so it becomes a saving compared to three MOVs with two of
  5984. them able to execute simultaneously. [Kit] }
  5985. (current_settings.optimizecputype >= cpu_PentiumM)
  5986. {$endif x86_64}
  5987. );
  5988. end;
  5989. var
  5990. NewRef: TReference;
  5991. hp1, hp2, hp3, hp4: Tai;
  5992. {$ifndef x86_64}
  5993. OperIdx: Integer;
  5994. {$endif x86_64}
  5995. NewInstr : Taicpu;
  5996. NewAligh : Tai_align;
  5997. DestLabel: TAsmLabel;
  5998. begin
  5999. Result:=false;
  6000. { This optimisation adds an instruction, so only do it for speed }
  6001. if not (cs_opt_size in current_settings.optimizerswitches) and
  6002. MatchOpType(taicpu(p), top_const, top_reg) and
  6003. (taicpu(p).oper[0]^.val = 0) then
  6004. begin
  6005. { To avoid compiler warning }
  6006. DestLabel := nil;
  6007. if (p.typ <> ait_instruction) or (taicpu(p).oper[1]^.typ <> top_reg) then
  6008. InternalError(2021040750);
  6009. if not GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[1]^.reg) then
  6010. Exit;
  6011. case hp1.typ of
  6012. ait_label:
  6013. begin
  6014. { Change:
  6015. mov $0,%reg mov $0,%reg
  6016. @Lbl1: @Lbl1:
  6017. test %reg,%reg / cmp $0,%reg test %reg,%reg / mov $0,%reg
  6018. je @Lbl2 jne @Lbl2
  6019. To: To:
  6020. mov $0,%reg mov $0,%reg
  6021. jmp @Lbl2 jmp @Lbl3
  6022. (align) (align)
  6023. @Lbl1: @Lbl1:
  6024. test %reg,%reg / cmp $0,%reg test %reg,%reg / cmp $0,%reg
  6025. je @Lbl2 je @Lbl2
  6026. @Lbl3: <-- Only if label exists
  6027. (Not if it's optimised for size)
  6028. }
  6029. if not GetNextInstruction(hp1, hp2) then
  6030. Exit;
  6031. if not (cs_opt_size in current_settings.optimizerswitches) and
  6032. (hp2.typ = ait_instruction) and
  6033. (
  6034. { Register sizes must exactly match }
  6035. (
  6036. (taicpu(hp2).opcode = A_CMP) and
  6037. MatchOperand(taicpu(hp2).oper[0]^, 0) and
  6038. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^.reg)
  6039. ) or (
  6040. (taicpu(hp2).opcode = A_TEST) and
  6041. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  6042. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^.reg)
  6043. )
  6044. ) and GetNextInstruction(hp2, hp3) and
  6045. (hp3.typ = ait_instruction) and
  6046. (taicpu(hp3).opcode = A_JCC) and
  6047. (taicpu(hp3).oper[0]^.typ=top_ref) and (taicpu(hp3).oper[0]^.ref^.refaddr=addr_full) and (taicpu(hp3).oper[0]^.ref^.base=NR_NO) and
  6048. (taicpu(hp3).oper[0]^.ref^.index=NR_NO) and (taicpu(hp3).oper[0]^.ref^.symbol is tasmlabel) then
  6049. begin
  6050. { Check condition of jump }
  6051. { Always true? }
  6052. if condition_in(C_E, taicpu(hp3).condition) then
  6053. begin
  6054. { Copy label symbol and obtain matching label entry for the
  6055. conditional jump, as this will be our destination}
  6056. DestLabel := tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol);
  6057. DebugMsg(SPeepholeOptimization + 'Mov0LblCmp0Je -> Mov0JmpLblCmp0Je', p);
  6058. Result := True;
  6059. end
  6060. { Always false? }
  6061. else if condition_in(C_NE, taicpu(hp3).condition) and GetNextInstruction(hp3, hp2) then
  6062. begin
  6063. { This is only worth it if there's a jump to take }
  6064. case hp2.typ of
  6065. ait_instruction:
  6066. begin
  6067. if taicpu(hp2).opcode = A_JMP then
  6068. begin
  6069. DestLabel := tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol);
  6070. { An unconditional jump follows the conditional jump which will always be false,
  6071. so use this jump's destination for the new jump }
  6072. DebugMsg(SPeepholeOptimization + 'Mov0LblCmp0Jne -> Mov0JmpLblCmp0Jne (with JMP)', p);
  6073. Result := True;
  6074. end
  6075. else if taicpu(hp2).opcode = A_JCC then
  6076. begin
  6077. DestLabel := tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol);
  6078. if condition_in(C_E, taicpu(hp2).condition) then
  6079. begin
  6080. { A second conditional jump follows the conditional jump which will always be false,
  6081. while the second jump is always True, so use this jump's destination for the new jump }
  6082. DebugMsg(SPeepholeOptimization + 'Mov0LblCmp0Jne -> Mov0JmpLblCmp0Jne (with second Jcc)', p);
  6083. Result := True;
  6084. end;
  6085. { Don't risk it if the jump isn't always true (Result remains False) }
  6086. end;
  6087. end;
  6088. else
  6089. { If anything else don't optimise };
  6090. end;
  6091. end;
  6092. if Result then
  6093. begin
  6094. { Just so we have something to insert as a paremeter}
  6095. reference_reset(NewRef, 1, []);
  6096. NewInstr := taicpu.op_ref(A_JMP, S_NO, NewRef);
  6097. { Now actually load the correct parameter }
  6098. NewInstr.loadsymbol(0, DestLabel, 0);
  6099. { Get instruction before original label (may not be p under -O3) }
  6100. if not GetLastInstruction(hp1, hp2) then
  6101. { Shouldn't fail here }
  6102. InternalError(2021040701);
  6103. DestLabel.increfs;
  6104. AsmL.InsertAfter(NewInstr, hp2);
  6105. { Add new alignment field }
  6106. (* AsmL.InsertAfter(
  6107. cai_align.create_max(
  6108. current_settings.alignment.jumpalign,
  6109. current_settings.alignment.jumpalignskipmax
  6110. ),
  6111. NewInstr
  6112. ); *)
  6113. end;
  6114. Exit;
  6115. end;
  6116. end;
  6117. else
  6118. ;
  6119. end;
  6120. end;
  6121. if not GetNextInstruction(p, hp1) then
  6122. Exit;
  6123. if MatchInstruction(hp1, A_JMP, [S_NO]) then
  6124. begin
  6125. { Sometimes the MOVs that OptPass2JMP produces can be improved
  6126. further, but we can't just put this jump optimisation in pass 1
  6127. because it tends to perform worse when conditional jumps are
  6128. nearby (e.g. when converting CMOV instructions). [Kit] }
  6129. if OptPass2JMP(hp1) then
  6130. { call OptPass1MOV once to potentially merge any MOVs that were created }
  6131. Result := OptPass1MOV(p)
  6132. { OptPass2MOV will now exit but will be called again if OptPass1MOV
  6133. returned True and the instruction is still a MOV, thus checking
  6134. the optimisations below }
  6135. { If OptPass2JMP returned False, no optimisations were done to
  6136. the jump and there are no further optimisations that can be done
  6137. to the MOV instruction on this pass }
  6138. end
  6139. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  6140. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  6141. MatchInstruction(hp1,A_ADD,A_SUB,[taicpu(p).opsize]) and
  6142. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6143. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) and
  6144. { be lazy, checking separately for sub would be slightly better }
  6145. (abs(taicpu(hp1).oper[0]^.val)<=$7fffffff) then
  6146. begin
  6147. { Change:
  6148. movl/q %reg1,%reg2 movl/q %reg1,%reg2
  6149. addl/q $x,%reg2 subl/q $x,%reg2
  6150. To:
  6151. leal/q x(%reg1),%reg2 leal/q -x(%reg1),%reg2
  6152. }
  6153. TransferUsedRegs(TmpUsedRegs);
  6154. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  6155. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  6156. if not GetNextInstruction(hp1, hp2) or
  6157. (
  6158. { The FLAGS register isn't always tracked properly, so do not
  6159. perform this optimisation if a conditional statement follows }
  6160. not RegReadByInstruction(NR_DEFAULTFLAGS, hp2) and
  6161. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)
  6162. ) then
  6163. begin
  6164. reference_reset(NewRef, 1, []);
  6165. NewRef.base := taicpu(p).oper[0]^.reg;
  6166. NewRef.scalefactor := 1;
  6167. if taicpu(hp1).opcode = A_ADD then
  6168. begin
  6169. DebugMsg(SPeepholeOptimization + 'MovAdd2Lea', p);
  6170. NewRef.offset := taicpu(hp1).oper[0]^.val;
  6171. end
  6172. else
  6173. begin
  6174. DebugMsg(SPeepholeOptimization + 'MovSub2Lea', p);
  6175. NewRef.offset := -taicpu(hp1).oper[0]^.val;
  6176. end;
  6177. taicpu(p).opcode := A_LEA;
  6178. taicpu(p).loadref(0, NewRef);
  6179. RemoveInstruction(hp1);
  6180. Result := True;
  6181. Exit;
  6182. end;
  6183. end
  6184. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  6185. {$ifdef x86_64}
  6186. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  6187. {$else x86_64}
  6188. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  6189. {$endif x86_64}
  6190. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  6191. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  6192. { mov reg1, reg2 mov reg1, reg2
  6193. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  6194. begin
  6195. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  6196. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  6197. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  6198. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  6199. TransferUsedRegs(TmpUsedRegs);
  6200. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  6201. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  6202. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  6203. then
  6204. begin
  6205. RemoveCurrentP(p, hp1);
  6206. Result:=true;
  6207. end;
  6208. exit;
  6209. end
  6210. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  6211. IsXCHGAcceptable and
  6212. { XCHG doesn't support 8-byte registers }
  6213. (taicpu(p).opsize <> S_B) and
  6214. MatchInstruction(hp1, A_MOV, []) and
  6215. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  6216. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  6217. GetNextInstruction(hp1, hp2) and
  6218. MatchInstruction(hp2, A_MOV, []) and
  6219. { Don't need to call MatchOpType for hp2 because the operand matches below cover for it }
  6220. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  6221. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) then
  6222. begin
  6223. { mov %reg1,%reg2
  6224. mov %reg3,%reg1 -> xchg %reg3,%reg1
  6225. mov %reg2,%reg3
  6226. (%reg2 not used afterwards)
  6227. Note that xchg takes 3 cycles to execute, and generally mov's take
  6228. only one cycle apiece, but the first two mov's can be executed in
  6229. parallel, only taking 2 cycles overall. Older processors should
  6230. therefore only optimise for size. [Kit]
  6231. }
  6232. TransferUsedRegs(TmpUsedRegs);
  6233. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  6234. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  6235. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs) then
  6236. begin
  6237. DebugMsg(SPeepholeOptimization + 'MovMovMov2XChg', p);
  6238. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp1, UsedRegs);
  6239. taicpu(hp1).opcode := A_XCHG;
  6240. RemoveCurrentP(p, hp1);
  6241. RemoveInstruction(hp2);
  6242. Result := True;
  6243. Exit;
  6244. end;
  6245. end
  6246. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  6247. MatchInstruction(hp1, A_SAR, []) then
  6248. begin
  6249. if MatchOperand(taicpu(hp1).oper[0]^, 31) then
  6250. begin
  6251. { the use of %edx also covers the opsize being S_L }
  6252. if MatchOperand(taicpu(hp1).oper[1]^, NR_EDX) then
  6253. begin
  6254. { Note it has to be specifically "movl %eax,%edx", and those specific sub-registers }
  6255. if (taicpu(p).oper[0]^.reg = NR_EAX) and
  6256. (taicpu(p).oper[1]^.reg = NR_EDX) then
  6257. begin
  6258. { Change:
  6259. movl %eax,%edx
  6260. sarl $31,%edx
  6261. To:
  6262. cltd
  6263. }
  6264. DebugMsg(SPeepholeOptimization + 'MovSar2Cltd', p);
  6265. RemoveInstruction(hp1);
  6266. taicpu(p).opcode := A_CDQ;
  6267. taicpu(p).opsize := S_NO;
  6268. taicpu(p).clearop(1);
  6269. taicpu(p).clearop(0);
  6270. taicpu(p).ops:=0;
  6271. Result := True;
  6272. end
  6273. else if (cs_opt_size in current_settings.optimizerswitches) and
  6274. (taicpu(p).oper[0]^.reg = NR_EDX) and
  6275. (taicpu(p).oper[1]^.reg = NR_EAX) then
  6276. begin
  6277. { Change:
  6278. movl %edx,%eax
  6279. sarl $31,%edx
  6280. To:
  6281. movl %edx,%eax
  6282. cltd
  6283. Note that this creates a dependency between the two instructions,
  6284. so only perform if optimising for size.
  6285. }
  6286. DebugMsg(SPeepholeOptimization + 'MovSar2MovCltd', p);
  6287. taicpu(hp1).opcode := A_CDQ;
  6288. taicpu(hp1).opsize := S_NO;
  6289. taicpu(hp1).clearop(1);
  6290. taicpu(hp1).clearop(0);
  6291. taicpu(hp1).ops:=0;
  6292. end;
  6293. {$ifndef x86_64}
  6294. end
  6295. { Don't bother if CMOV is supported, because a more optimal
  6296. sequence would have been generated for the Abs() intrinsic }
  6297. else if not(CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and
  6298. { the use of %eax also covers the opsize being S_L }
  6299. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) and
  6300. (taicpu(p).oper[0]^.reg = NR_EAX) and
  6301. (taicpu(p).oper[1]^.reg = NR_EDX) and
  6302. GetNextInstruction(hp1, hp2) and
  6303. MatchInstruction(hp2, A_XOR, [S_L]) and
  6304. MatchOperand(taicpu(hp2).oper[0]^, NR_EAX) and
  6305. MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) and
  6306. GetNextInstruction(hp2, hp3) and
  6307. MatchInstruction(hp3, A_SUB, [S_L]) and
  6308. MatchOperand(taicpu(hp3).oper[0]^, NR_EAX) and
  6309. MatchOperand(taicpu(hp3).oper[1]^, NR_EDX) then
  6310. begin
  6311. { Change:
  6312. movl %eax,%edx
  6313. sarl $31,%eax
  6314. xorl %eax,%edx
  6315. subl %eax,%edx
  6316. (Instruction that uses %edx)
  6317. (%eax deallocated)
  6318. (%edx deallocated)
  6319. To:
  6320. cltd
  6321. xorl %edx,%eax <-- Note the registers have swapped
  6322. subl %edx,%eax
  6323. (Instruction that uses %eax) <-- %eax rather than %edx
  6324. }
  6325. TransferUsedRegs(TmpUsedRegs);
  6326. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  6327. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  6328. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  6329. if not RegUsedAfterInstruction(NR_EAX, hp3, TmpUsedRegs) then
  6330. begin
  6331. if GetNextInstruction(hp3, hp4) and
  6332. not RegModifiedByInstruction(NR_EDX, hp4) and
  6333. not RegUsedAfterInstruction(NR_EDX, hp4, TmpUsedRegs) then
  6334. begin
  6335. DebugMsg(SPeepholeOptimization + 'abs() intrinsic optimisation', p);
  6336. taicpu(p).opcode := A_CDQ;
  6337. taicpu(p).clearop(1);
  6338. taicpu(p).clearop(0);
  6339. taicpu(p).ops:=0;
  6340. RemoveInstruction(hp1);
  6341. taicpu(hp2).loadreg(0, NR_EDX);
  6342. taicpu(hp2).loadreg(1, NR_EAX);
  6343. taicpu(hp3).loadreg(0, NR_EDX);
  6344. taicpu(hp3).loadreg(1, NR_EAX);
  6345. AllocRegBetween(NR_EAX, hp3, hp4, TmpUsedRegs);
  6346. { Convert references in the following instruction (hp4) from %edx to %eax }
  6347. for OperIdx := 0 to taicpu(hp4).ops - 1 do
  6348. with taicpu(hp4).oper[OperIdx]^ do
  6349. case typ of
  6350. top_reg:
  6351. if getsupreg(reg) = RS_EDX then
  6352. reg := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  6353. top_ref:
  6354. begin
  6355. if getsupreg(reg) = RS_EDX then
  6356. ref^.base := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  6357. if getsupreg(reg) = RS_EDX then
  6358. ref^.index := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  6359. end;
  6360. else
  6361. ;
  6362. end;
  6363. end;
  6364. end;
  6365. {$else x86_64}
  6366. end;
  6367. end
  6368. else if MatchOperand(taicpu(hp1).oper[0]^, 63) and
  6369. { the use of %rdx also covers the opsize being S_Q }
  6370. MatchOperand(taicpu(hp1).oper[1]^, NR_RDX) then
  6371. begin
  6372. { Note it has to be specifically "movq %rax,%rdx", and those specific sub-registers }
  6373. if (taicpu(p).oper[0]^.reg = NR_RAX) and
  6374. (taicpu(p).oper[1]^.reg = NR_RDX) then
  6375. begin
  6376. { Change:
  6377. movq %rax,%rdx
  6378. sarq $63,%rdx
  6379. To:
  6380. cqto
  6381. }
  6382. DebugMsg(SPeepholeOptimization + 'MovSar2Cqto', p);
  6383. RemoveInstruction(hp1);
  6384. taicpu(p).opcode := A_CQO;
  6385. taicpu(p).opsize := S_NO;
  6386. taicpu(p).clearop(1);
  6387. taicpu(p).clearop(0);
  6388. taicpu(p).ops:=0;
  6389. Result := True;
  6390. end
  6391. else if (cs_opt_size in current_settings.optimizerswitches) and
  6392. (taicpu(p).oper[0]^.reg = NR_RDX) and
  6393. (taicpu(p).oper[1]^.reg = NR_RAX) then
  6394. begin
  6395. { Change:
  6396. movq %rdx,%rax
  6397. sarq $63,%rdx
  6398. To:
  6399. movq %rdx,%rax
  6400. cqto
  6401. Note that this creates a dependency between the two instructions,
  6402. so only perform if optimising for size.
  6403. }
  6404. DebugMsg(SPeepholeOptimization + 'MovSar2MovCqto', p);
  6405. taicpu(hp1).opcode := A_CQO;
  6406. taicpu(hp1).opsize := S_NO;
  6407. taicpu(hp1).clearop(1);
  6408. taicpu(hp1).clearop(0);
  6409. taicpu(hp1).ops:=0;
  6410. {$endif x86_64}
  6411. end;
  6412. end;
  6413. end
  6414. else if MatchInstruction(hp1, A_MOV, []) and
  6415. (taicpu(hp1).oper[1]^.typ = top_reg) then
  6416. { Though "GetNextInstruction" could be factored out, along with
  6417. the instructions that depend on hp2, it is an expensive call that
  6418. should be delayed for as long as possible, hence we do cheaper
  6419. checks first that are likely to be False. [Kit] }
  6420. begin
  6421. if (
  6422. (
  6423. MatchOperand(taicpu(p).oper[1]^, NR_EDX) and
  6424. (taicpu(hp1).oper[1]^.reg = NR_EAX) and
  6425. (
  6426. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  6427. MatchOperand(taicpu(hp1).oper[0]^, NR_EDX)
  6428. )
  6429. ) or
  6430. (
  6431. MatchOperand(taicpu(p).oper[1]^, NR_EAX) and
  6432. (taicpu(hp1).oper[1]^.reg = NR_EDX) and
  6433. (
  6434. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  6435. MatchOperand(taicpu(hp1).oper[0]^, NR_EAX)
  6436. )
  6437. )
  6438. ) and
  6439. GetNextInstruction(hp1, hp2) and
  6440. MatchInstruction(hp2, A_SAR, []) and
  6441. MatchOperand(taicpu(hp2).oper[0]^, 31) then
  6442. begin
  6443. if MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) then
  6444. begin
  6445. { Change:
  6446. movl r/m,%edx movl r/m,%eax movl r/m,%edx movl r/m,%eax
  6447. movl %edx,%eax or movl %eax,%edx or movl r/m,%eax or movl r/m,%edx
  6448. sarl $31,%edx sarl $31,%edx sarl $31,%edx sarl $31,%edx
  6449. To:
  6450. movl r/m,%eax <- Note the change in register
  6451. cltd
  6452. }
  6453. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCltd', p);
  6454. AllocRegBetween(NR_EAX, p, hp1, UsedRegs);
  6455. taicpu(p).loadreg(1, NR_EAX);
  6456. taicpu(hp1).opcode := A_CDQ;
  6457. taicpu(hp1).clearop(1);
  6458. taicpu(hp1).clearop(0);
  6459. taicpu(hp1).ops:=0;
  6460. RemoveInstruction(hp2);
  6461. (*
  6462. {$ifdef x86_64}
  6463. end
  6464. else if MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) and
  6465. { This code sequence does not get generated - however it might become useful
  6466. if and when 128-bit signed integer types make an appearance, so the code
  6467. is kept here for when it is eventually needed. [Kit] }
  6468. (
  6469. (
  6470. (taicpu(hp1).oper[1]^.reg = NR_RAX) and
  6471. (
  6472. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  6473. MatchOperand(taicpu(hp1).oper[0]^, NR_RDX)
  6474. )
  6475. ) or
  6476. (
  6477. (taicpu(hp1).oper[1]^.reg = NR_RDX) and
  6478. (
  6479. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  6480. MatchOperand(taicpu(hp1).oper[0]^, NR_RAX)
  6481. )
  6482. )
  6483. ) and
  6484. GetNextInstruction(hp1, hp2) and
  6485. MatchInstruction(hp2, A_SAR, [S_Q]) and
  6486. MatchOperand(taicpu(hp2).oper[0]^, 63) and
  6487. MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) then
  6488. begin
  6489. { Change:
  6490. movq r/m,%rdx movq r/m,%rax movq r/m,%rdx movq r/m,%rax
  6491. movq %rdx,%rax or movq %rax,%rdx or movq r/m,%rax or movq r/m,%rdx
  6492. sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx
  6493. To:
  6494. movq r/m,%rax <- Note the change in register
  6495. cqto
  6496. }
  6497. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCqto', p);
  6498. AllocRegBetween(NR_RAX, p, hp1, UsedRegs);
  6499. taicpu(p).loadreg(1, NR_RAX);
  6500. taicpu(hp1).opcode := A_CQO;
  6501. taicpu(hp1).clearop(1);
  6502. taicpu(hp1).clearop(0);
  6503. taicpu(hp1).ops:=0;
  6504. RemoveInstruction(hp2);
  6505. {$endif x86_64}
  6506. *)
  6507. end;
  6508. end;
  6509. {$ifdef x86_64}
  6510. end
  6511. else if (taicpu(p).opsize = S_L) and
  6512. (taicpu(p).oper[1]^.typ = top_reg) and
  6513. (
  6514. MatchInstruction(hp1, A_MOV,[]) and
  6515. (taicpu(hp1).opsize = S_L) and
  6516. (taicpu(hp1).oper[1]^.typ = top_reg)
  6517. ) and (
  6518. GetNextInstruction(hp1, hp2) and
  6519. (tai(hp2).typ=ait_instruction) and
  6520. (taicpu(hp2).opsize = S_Q) and
  6521. (
  6522. (
  6523. MatchInstruction(hp2, A_ADD,[]) and
  6524. (taicpu(hp2).opsize = S_Q) and
  6525. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  6526. (
  6527. (
  6528. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  6529. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  6530. ) or (
  6531. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  6532. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  6533. )
  6534. )
  6535. ) or (
  6536. MatchInstruction(hp2, A_LEA,[]) and
  6537. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  6538. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  6539. (
  6540. (
  6541. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  6542. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  6543. ) or (
  6544. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  6545. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  6546. )
  6547. ) and (
  6548. (
  6549. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  6550. ) or (
  6551. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  6552. )
  6553. )
  6554. )
  6555. )
  6556. ) and (
  6557. GetNextInstruction(hp2, hp3) and
  6558. MatchInstruction(hp3, A_SHR,[]) and
  6559. (taicpu(hp3).opsize = S_Q) and
  6560. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  6561. (taicpu(hp3).oper[0]^.val = 1) and
  6562. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  6563. ) then
  6564. begin
  6565. { Change movl x, reg1d movl x, reg1d
  6566. movl y, reg2d movl y, reg2d
  6567. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  6568. shrq $1, reg1q shrq $1, reg1q
  6569. ( reg1d and reg2d can be switched around in the first two instructions )
  6570. To movl x, reg1d
  6571. addl y, reg1d
  6572. rcrl $1, reg1d
  6573. This corresponds to the common expression (x + y) shr 1, where
  6574. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  6575. smaller code, but won't account for x + y causing an overflow). [Kit]
  6576. }
  6577. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  6578. { Change first MOV command to have the same register as the final output }
  6579. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  6580. else
  6581. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  6582. { Change second MOV command to an ADD command. This is easier than
  6583. converting the existing command because it means we don't have to
  6584. touch 'y', which might be a complicated reference, and also the
  6585. fact that the third command might either be ADD or LEA. [Kit] }
  6586. taicpu(hp1).opcode := A_ADD;
  6587. { Delete old ADD/LEA instruction }
  6588. RemoveInstruction(hp2);
  6589. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  6590. taicpu(hp3).opcode := A_RCR;
  6591. taicpu(hp3).changeopsize(S_L);
  6592. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  6593. {$endif x86_64}
  6594. end;
  6595. end;
  6596. function TX86AsmOptimizer.OptPass2Movx(var p : tai) : boolean;
  6597. var
  6598. ThisReg: TRegister;
  6599. MinSize, MaxSize, TrySmaller, TargetSize: TOpSize;
  6600. TargetSubReg: TSubRegister;
  6601. hp1, hp2: tai;
  6602. RegInUse, RegChanged, p_removed: Boolean;
  6603. { Store list of found instructions so we don't have to call
  6604. GetNextInstructionUsingReg multiple times }
  6605. InstrList: array of taicpu;
  6606. InstrMax, Index: Integer;
  6607. UpperLimit, TrySmallerLimit: TCgInt;
  6608. PreMessage: string;
  6609. { Data flow analysis }
  6610. TestValMin, TestValMax: TCgInt;
  6611. SmallerOverflow: Boolean;
  6612. begin
  6613. Result := False;
  6614. p_removed := False;
  6615. { This is anything but quick! }
  6616. if not(cs_opt_level2 in current_settings.optimizerswitches) then
  6617. Exit;
  6618. SetLength(InstrList, 0);
  6619. InstrMax := -1;
  6620. ThisReg := taicpu(p).oper[1]^.reg;
  6621. case taicpu(p).opsize of
  6622. S_BW, S_BL:
  6623. begin
  6624. {$if defined(i386) or defined(i8086)}
  6625. { If the target size is 8-bit, make sure we can actually encode it }
  6626. if not (GetSupReg(ThisReg) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX]) then
  6627. Exit;
  6628. {$endif i386 or i8086}
  6629. UpperLimit := $FF;
  6630. MinSize := S_B;
  6631. if taicpu(p).opsize = S_BW then
  6632. MaxSize := S_W
  6633. else
  6634. MaxSize := S_L;
  6635. end;
  6636. S_WL:
  6637. begin
  6638. UpperLimit := $FFFF;
  6639. MinSize := S_W;
  6640. MaxSize := S_L;
  6641. end
  6642. else
  6643. InternalError(2020112301);
  6644. end;
  6645. TestValMin := 0;
  6646. TestValMax := UpperLimit;
  6647. TrySmallerLimit := UpperLimit;
  6648. TrySmaller := S_NO;
  6649. SmallerOverflow := False;
  6650. RegChanged := False;
  6651. hp1 := p;
  6652. while GetNextInstructionUsingReg(hp1, hp1, ThisReg) and
  6653. (hp1.typ = ait_instruction) and
  6654. (
  6655. { Under -O1 and -O2, GetNextInstructionUsingReg may return an
  6656. instruction that doesn't actually contain ThisReg }
  6657. (cs_opt_level3 in current_settings.optimizerswitches) or
  6658. RegInInstruction(ThisReg, hp1)
  6659. ) do
  6660. begin
  6661. case taicpu(hp1).opcode of
  6662. A_INC,A_DEC:
  6663. begin
  6664. { Has to be an exact match on the register }
  6665. if not MatchOperand(taicpu(hp1).oper[0]^, ThisReg) then
  6666. Break;
  6667. if taicpu(hp1).opcode = A_INC then
  6668. begin
  6669. Inc(TestValMin);
  6670. Inc(TestValMax);
  6671. end
  6672. else
  6673. begin
  6674. Dec(TestValMin);
  6675. Dec(TestValMax);
  6676. end;
  6677. end;
  6678. A_CMP:
  6679. begin
  6680. if (taicpu(hp1).oper[1]^.typ <> top_reg) or
  6681. { Has to be an exact match on the register }
  6682. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  6683. (taicpu(hp1).oper[0]^.typ <> top_const) or
  6684. { Make sure the comparison value is not smaller than the
  6685. smallest allowed signed value for the minimum size (e.g.
  6686. -128 for 8-bit) }
  6687. not (
  6688. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  6689. { Is it in the negative range? }
  6690. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val))
  6691. ) then
  6692. Break;
  6693. TestValMin := TestValMin - taicpu(hp1).oper[0]^.val;
  6694. TestValMax := TestValMax - taicpu(hp1).oper[0]^.val;
  6695. if (TestValMin < TrySmallerLimit) or (TestValMax < TrySmallerLimit) or
  6696. (TestValMin > UpperLimit) or (TestValMax > UpperLimit) then
  6697. { Overflow }
  6698. Break;
  6699. { Check to see if the active register is used afterwards }
  6700. TransferUsedRegs(TmpUsedRegs);
  6701. IncludeRegInUsedRegs(ThisReg, TmpUsedRegs);
  6702. if not RegUsedAfterInstruction(ThisReg, hp1, TmpUsedRegs) then
  6703. begin
  6704. case MinSize of
  6705. S_B:
  6706. TargetSubReg := R_SUBL;
  6707. S_W:
  6708. TargetSubReg := R_SUBW;
  6709. else
  6710. InternalError(2021051002);
  6711. end;
  6712. { Update the register to its new size }
  6713. setsubreg(ThisReg, TargetSubReg);
  6714. taicpu(hp1).oper[1]^.reg := ThisReg;
  6715. taicpu(hp1).opsize := MinSize;
  6716. { Convert the input MOVZX to a MOV }
  6717. if (taicpu(p).oper[0]^.typ = top_reg) and
  6718. SuperRegistersEqual(taicpu(p).oper[0]^.reg, ThisReg) then
  6719. begin
  6720. { Or remove it completely! }
  6721. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 1a', p);
  6722. RemoveCurrentP(p);
  6723. p_removed := True;
  6724. end
  6725. else
  6726. begin
  6727. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 1a', p);
  6728. taicpu(p).opcode := A_MOV;
  6729. taicpu(p).oper[1]^.reg := ThisReg;
  6730. taicpu(p).opsize := MinSize;
  6731. end;
  6732. if (InstrMax >= 0) then
  6733. begin
  6734. for Index := 0 to InstrMax do
  6735. begin
  6736. { If p_removed is true, then the original MOV/Z was removed
  6737. and removing the AND instruction may not be safe if it
  6738. appears first }
  6739. if (InstrList[Index].oper[InstrList[Index].ops - 1]^.typ <> top_reg) then
  6740. InternalError(2020112311);
  6741. if InstrList[Index].oper[0]^.typ = top_reg then
  6742. InstrList[Index].oper[0]^.reg := ThisReg;
  6743. InstrList[Index].oper[InstrList[Index].ops - 1]^.reg := ThisReg;
  6744. InstrList[Index].opsize := MinSize;
  6745. end;
  6746. end;
  6747. Result := True;
  6748. Exit;
  6749. end;
  6750. end;
  6751. { OR and XOR are not included because they can too easily fool
  6752. the data flow analysis (they can cause non-linear behaviour) }
  6753. A_ADD,A_SUB,A_AND,A_SHL,A_SHR:
  6754. begin
  6755. if
  6756. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  6757. { Has to be an exact match on the register }
  6758. (taicpu(hp1).oper[1]^.reg <> ThisReg) or not
  6759. (
  6760. (
  6761. (taicpu(hp1).oper[0]^.typ = top_const) and
  6762. (
  6763. (
  6764. (taicpu(hp1).opcode = A_SHL) and
  6765. (
  6766. ((MinSize = S_B) and (taicpu(hp1).oper[0]^.val < 8)) or
  6767. ((MinSize = S_W) and (taicpu(hp1).oper[0]^.val < 16)) or
  6768. ((MinSize = S_L) and (taicpu(hp1).oper[0]^.val < 32))
  6769. )
  6770. ) or (
  6771. (taicpu(hp1).opcode <> A_SHL) and
  6772. (
  6773. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  6774. { Is it in the negative range? }
  6775. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val))
  6776. )
  6777. )
  6778. )
  6779. ) or (
  6780. MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^.reg) and
  6781. ((taicpu(hp1).opcode = A_ADD) or (taicpu(hp1).opcode = A_AND) or (taicpu(hp1).opcode = A_SUB))
  6782. )
  6783. ) then
  6784. Break;
  6785. case taicpu(hp1).opcode of
  6786. A_ADD:
  6787. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  6788. begin
  6789. TestValMin := TestValMin * 2;
  6790. TestValMax := TestValMax * 2;
  6791. end
  6792. else
  6793. begin
  6794. TestValMin := TestValMin + taicpu(hp1).oper[0]^.val;
  6795. TestValMax := TestValMax + taicpu(hp1).oper[0]^.val;
  6796. end;
  6797. A_SUB:
  6798. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  6799. begin
  6800. TestValMin := 0;
  6801. TestValMax := 0;
  6802. end
  6803. else
  6804. begin
  6805. TestValMin := TestValMin - taicpu(hp1).oper[0]^.val;
  6806. TestValMax := TestValMax - taicpu(hp1).oper[0]^.val;
  6807. end;
  6808. A_AND:
  6809. if (taicpu(hp1).oper[0]^.typ = top_const) then
  6810. begin
  6811. { we might be able to go smaller if AND appears first }
  6812. if InstrMax = -1 then
  6813. case MinSize of
  6814. S_B:
  6815. ;
  6816. S_W:
  6817. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  6818. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  6819. begin
  6820. TrySmaller := S_B;
  6821. TrySmallerLimit := $FF;
  6822. end;
  6823. S_L:
  6824. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  6825. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  6826. begin
  6827. TrySmaller := S_B;
  6828. TrySmallerLimit := $FF;
  6829. end
  6830. else if ((taicpu(hp1).oper[0]^.val and $FFFF) = taicpu(hp1).oper[0]^.val) or
  6831. ((not(taicpu(hp1).oper[0]^.val) and $7FFF) = (not taicpu(hp1).oper[0]^.val)) then
  6832. begin
  6833. TrySmaller := S_W;
  6834. TrySmallerLimit := $FFFF;
  6835. end;
  6836. else
  6837. InternalError(2020112320);
  6838. end;
  6839. TestValMin := TestValMin and taicpu(hp1).oper[0]^.val;
  6840. TestValMax := TestValMax and taicpu(hp1).oper[0]^.val;
  6841. end;
  6842. A_SHL:
  6843. begin
  6844. TestValMin := TestValMin shl taicpu(hp1).oper[0]^.val;
  6845. TestValMax := TestValMax shl taicpu(hp1).oper[0]^.val;
  6846. end;
  6847. A_SHR:
  6848. begin
  6849. { we might be able to go smaller if SHR appears first }
  6850. if InstrMax = -1 then
  6851. case MinSize of
  6852. S_B:
  6853. ;
  6854. S_W:
  6855. if (taicpu(hp1).oper[0]^.val >= 8) then
  6856. begin
  6857. TrySmaller := S_B;
  6858. TrySmallerLimit := $FF;
  6859. end;
  6860. S_L:
  6861. if (taicpu(hp1).oper[0]^.val >= 24) then
  6862. begin
  6863. TrySmaller := S_B;
  6864. TrySmallerLimit := $FF;
  6865. end
  6866. else if (taicpu(hp1).oper[0]^.val >= 16) then
  6867. begin
  6868. TrySmaller := S_W;
  6869. TrySmallerLimit := $FFFF;
  6870. end;
  6871. else
  6872. InternalError(2020112321);
  6873. end;
  6874. TestValMin := TestValMin shr taicpu(hp1).oper[0]^.val;
  6875. TestValMax := TestValMax shr taicpu(hp1).oper[0]^.val;
  6876. end;
  6877. else
  6878. InternalError(2020112303);
  6879. end;
  6880. end;
  6881. (*
  6882. A_IMUL:
  6883. case taicpu(hp1).ops of
  6884. 2:
  6885. begin
  6886. if not MatchOpType(hp1, top_reg, top_reg) or
  6887. { Has to be an exact match on the register }
  6888. (taicpu(hp1).oper[0]^.reg <> ThisReg) or
  6889. (taicpu(hp1).oper[1]^.reg <> ThisReg) then
  6890. Break;
  6891. TestValMin := TestValMin * TestValMin;
  6892. TestValMax := TestValMax * TestValMax;
  6893. end;
  6894. 3:
  6895. begin
  6896. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  6897. { Has to be an exact match on the register }
  6898. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  6899. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  6900. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  6901. { Is it in the negative range? }
  6902. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  6903. Break;
  6904. TestValMin := TestValMin * taicpu(hp1).oper[0]^.val;
  6905. TestValMax := TestValMax * taicpu(hp1).oper[0]^.val;
  6906. end;
  6907. else
  6908. Break;
  6909. end;
  6910. A_IDIV:
  6911. case taicpu(hp1).ops of
  6912. 3:
  6913. begin
  6914. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  6915. { Has to be an exact match on the register }
  6916. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  6917. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  6918. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  6919. { Is it in the negative range? }
  6920. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  6921. Break;
  6922. TestValMin := TestValMin div taicpu(hp1).oper[0]^.val;
  6923. TestValMax := TestValMax div taicpu(hp1).oper[0]^.val;
  6924. end;
  6925. else
  6926. Break;
  6927. end;
  6928. *)
  6929. A_MOVZX:
  6930. begin
  6931. if not MatchOpType(taicpu(hp1), top_reg, top_reg) then
  6932. Break;
  6933. if not SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, ThisReg) then
  6934. begin
  6935. { Because hp1 was obtained via GetNextInstructionUsingReg
  6936. and ThisReg doesn't appear in the first operand, it
  6937. must appear in the second operand and hence gets
  6938. overwritten }
  6939. if (InstrMax = -1) and
  6940. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, ThisReg) then
  6941. begin
  6942. { The two MOVZX instructions are adjacent, so remove the first one }
  6943. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 5', p);
  6944. RemoveCurrentP(p);
  6945. Result := True;
  6946. Exit;
  6947. end;
  6948. Break;
  6949. end;
  6950. { The objective here is to try to find a combination that
  6951. removes one of the MOV/Z instructions. }
  6952. case taicpu(hp1).opsize of
  6953. S_WL:
  6954. if (MinSize in [S_B, S_W]) then
  6955. begin
  6956. TargetSize := S_L;
  6957. TargetSubReg := R_SUBD;
  6958. end
  6959. else if ((TrySmaller in [S_B, S_W]) and not SmallerOverflow) then
  6960. begin
  6961. TargetSize := TrySmaller;
  6962. if TrySmaller = S_B then
  6963. TargetSubReg := R_SUBL
  6964. else
  6965. TargetSubReg := R_SUBW;
  6966. end
  6967. else
  6968. Break;
  6969. S_BW:
  6970. if (MinSize in [S_B, S_W]) then
  6971. begin
  6972. TargetSize := S_W;
  6973. TargetSubReg := R_SUBW;
  6974. end
  6975. else if ((TrySmaller = S_B) and not SmallerOverflow) then
  6976. begin
  6977. TargetSize := S_B;
  6978. TargetSubReg := R_SUBL;
  6979. end
  6980. else
  6981. Break;
  6982. S_BL:
  6983. if (MinSize in [S_B, S_W]) then
  6984. begin
  6985. TargetSize := S_L;
  6986. TargetSubReg := R_SUBD;
  6987. end
  6988. else if ((TrySmaller = S_B) and not SmallerOverflow) then
  6989. begin
  6990. TargetSize := S_B;
  6991. TargetSubReg := R_SUBL;
  6992. end
  6993. else
  6994. Break;
  6995. else
  6996. InternalError(2020112302);
  6997. end;
  6998. { Update the register to its new size }
  6999. setsubreg(ThisReg, TargetSubReg);
  7000. if TargetSize = MinSize then
  7001. begin
  7002. { Convert the input MOVZX to a MOV }
  7003. if (taicpu(p).oper[0]^.typ = top_reg) and
  7004. SuperRegistersEqual(taicpu(p).oper[0]^.reg, ThisReg) then
  7005. begin
  7006. { Or remove it completely! }
  7007. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 1', p);
  7008. RemoveCurrentP(p);
  7009. p_removed := True;
  7010. end
  7011. else
  7012. begin
  7013. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 1', p);
  7014. taicpu(p).opcode := A_MOV;
  7015. taicpu(p).oper[1]^.reg := ThisReg;
  7016. taicpu(p).opsize := TargetSize;
  7017. end;
  7018. Result := True;
  7019. end
  7020. else if TargetSize <> MaxSize then
  7021. begin
  7022. case MaxSize of
  7023. S_L:
  7024. if TargetSize = S_W then
  7025. begin
  7026. DebugMsg(SPeepholeOptimization + 'movzbl2movzbw', p);
  7027. taicpu(p).opsize := S_BW;
  7028. taicpu(p).oper[1]^.reg := ThisReg;
  7029. Result := True;
  7030. end
  7031. else
  7032. InternalError(2020112341);
  7033. S_W:
  7034. if TargetSize = S_L then
  7035. begin
  7036. DebugMsg(SPeepholeOptimization + 'movzbw2movzbl', p);
  7037. taicpu(p).opsize := S_BL;
  7038. taicpu(p).oper[1]^.reg := ThisReg;
  7039. Result := True;
  7040. end
  7041. else
  7042. InternalError(2020112342);
  7043. else
  7044. ;
  7045. end;
  7046. end;
  7047. if (MaxSize = TargetSize) or
  7048. ((TargetSize = S_L) and (taicpu(hp1).opsize in [S_L, S_BL, S_WL])) or
  7049. ((TargetSize = S_W) and (taicpu(hp1).opsize in [S_W, S_BW])) then
  7050. begin
  7051. { Convert the output MOVZX to a MOV }
  7052. if SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) then
  7053. begin
  7054. { Or remove it completely! }
  7055. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 2', hp1);
  7056. { Be careful; if p = hp1 and p was also removed, p
  7057. will become a dangling pointer }
  7058. if p = hp1 then
  7059. RemoveCurrentp(p) { p = hp1 and will then become the next instruction }
  7060. else
  7061. RemoveInstruction(hp1);
  7062. end
  7063. else
  7064. begin
  7065. taicpu(hp1).opcode := A_MOV;
  7066. taicpu(hp1).oper[0]^.reg := ThisReg;
  7067. taicpu(hp1).opsize := TargetSize;
  7068. { Check to see if the active register is used afterwards;
  7069. if not, we can change it and make a saving. }
  7070. RegInUse := False;
  7071. TransferUsedRegs(TmpUsedRegs);
  7072. { The target register may be marked as in use to cross
  7073. a jump to a distant label, so exclude it }
  7074. ExcludeRegFromUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs);
  7075. hp2 := p;
  7076. repeat
  7077. UpdateUsedRegs(TmpUsedRegs, tai(hp2.next));
  7078. { Explicitly check for the excluded register (don't include the first
  7079. instruction as it may be reading from here }
  7080. if ((p <> hp2) and (RegInInstruction(taicpu(hp1).oper[1]^.reg, hp2))) or
  7081. RegInUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs) then
  7082. begin
  7083. RegInUse := True;
  7084. Break;
  7085. end;
  7086. if not GetNextInstruction(hp2, hp2) then
  7087. InternalError(2020112340);
  7088. until (hp2 = hp1);
  7089. if not RegInUse and not RegUsedAfterInstruction(ThisReg, hp1, TmpUsedRegs) then
  7090. begin
  7091. DebugMsg(SPeepholeOptimization + 'Simplified register usage so ' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' = ' + debug_regname(taicpu(p).oper[1]^.reg), p);
  7092. ThisReg := taicpu(hp1).oper[1]^.reg;
  7093. RegChanged := True;
  7094. TransferUsedRegs(TmpUsedRegs);
  7095. AllocRegBetween(ThisReg, p, hp1, TmpUsedRegs);
  7096. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 3', hp1);
  7097. if p = hp1 then
  7098. RemoveCurrentp(p) { p = hp1 and will then become the next instruction }
  7099. else
  7100. RemoveInstruction(hp1);
  7101. { Instruction will become "mov %reg,%reg" }
  7102. if not p_removed and (taicpu(p).opcode = A_MOV) and
  7103. MatchOperand(taicpu(p).oper[0]^, ThisReg) then
  7104. begin
  7105. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 6', p);
  7106. RemoveCurrentP(p);
  7107. p_removed := True;
  7108. end
  7109. else
  7110. taicpu(p).oper[1]^.reg := ThisReg;
  7111. Result := True;
  7112. end
  7113. else
  7114. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 2', hp1);
  7115. end;
  7116. end
  7117. else
  7118. InternalError(2020112330);
  7119. { Now go through every instruction we found and change the
  7120. size. If TargetSize = MaxSize, then almost no changes are
  7121. needed and Result can remain False if it hasn't been set
  7122. yet.
  7123. If RegChanged is True, then the register requires changing
  7124. and so the point about TargetSize = MaxSize doesn't apply. }
  7125. if ((TargetSize <> MaxSize) or RegChanged) and (InstrMax >= 0) then
  7126. begin
  7127. for Index := 0 to InstrMax do
  7128. begin
  7129. { If p_removed is true, then the original MOV/Z was removed
  7130. and removing the AND instruction may not be safe if it
  7131. appears first }
  7132. if (InstrList[Index].oper[InstrList[Index].ops - 1]^.typ <> top_reg) then
  7133. InternalError(2020112310);
  7134. if InstrList[Index].oper[0]^.typ = top_reg then
  7135. InstrList[Index].oper[0]^.reg := ThisReg;
  7136. InstrList[Index].oper[InstrList[Index].ops - 1]^.reg := ThisReg;
  7137. InstrList[Index].opsize := TargetSize;
  7138. end;
  7139. Result := True;
  7140. end;
  7141. Exit;
  7142. end;
  7143. else
  7144. { This includes ADC, SBB, IDIV and SAR }
  7145. Break;
  7146. end;
  7147. if (TestValMin < 0) or (TestValMax < 0) or
  7148. (TestValMin > UpperLimit) or (TestValMax > UpperLimit) then
  7149. { Overflow }
  7150. Break
  7151. else if not SmallerOverflow and (TrySmaller <> S_NO) and
  7152. ((TestValMin > TrySmallerLimit) or (TestValMax > TrySmallerLimit)) then
  7153. SmallerOverflow := True;
  7154. { Contains highest index (so instruction count - 1) }
  7155. Inc(InstrMax);
  7156. if InstrMax > High(InstrList) then
  7157. SetLength(InstrList, InstrMax + LIST_STEP_SIZE);
  7158. InstrList[InstrMax] := taicpu(hp1);
  7159. end;
  7160. end;
  7161. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  7162. var
  7163. hp1 : tai;
  7164. begin
  7165. Result:=false;
  7166. if (taicpu(p).ops >= 2) and
  7167. ((taicpu(p).oper[0]^.typ = top_const) or
  7168. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  7169. (taicpu(p).oper[1]^.typ = top_reg) and
  7170. ((taicpu(p).ops = 2) or
  7171. ((taicpu(p).oper[2]^.typ = top_reg) and
  7172. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  7173. GetLastInstruction(p,hp1) and
  7174. MatchInstruction(hp1,A_MOV,[]) and
  7175. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  7176. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  7177. begin
  7178. TransferUsedRegs(TmpUsedRegs);
  7179. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) or
  7180. ((taicpu(p).ops = 3) and (taicpu(p).oper[1]^.reg=taicpu(p).oper[2]^.reg)) then
  7181. { change
  7182. mov reg1,reg2
  7183. imul y,reg2 to imul y,reg1,reg2 }
  7184. begin
  7185. taicpu(p).ops := 3;
  7186. taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
  7187. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  7188. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  7189. RemoveInstruction(hp1);
  7190. result:=true;
  7191. end;
  7192. end;
  7193. end;
  7194. procedure TX86AsmOptimizer.ConvertJumpToRET(const p: tai; const ret_p: tai);
  7195. var
  7196. ThisLabel: TAsmLabel;
  7197. begin
  7198. ThisLabel := tasmlabel(taicpu(p).oper[0]^.ref^.symbol);
  7199. ThisLabel.decrefs;
  7200. taicpu(p).opcode := A_RET;
  7201. taicpu(p).is_jmp := false;
  7202. taicpu(p).ops := taicpu(ret_p).ops;
  7203. case taicpu(ret_p).ops of
  7204. 0:
  7205. taicpu(p).clearop(0);
  7206. 1:
  7207. taicpu(p).loadconst(0,taicpu(ret_p).oper[0]^.val);
  7208. else
  7209. internalerror(2016041301);
  7210. end;
  7211. { If the original label is now dead, it might turn out that the label
  7212. immediately follows p. As a result, everything beyond it, which will
  7213. be just some final register configuration and a RET instruction, is
  7214. now dead code. [Kit] }
  7215. { NOTE: This is much faster than introducing a OptPass2RET routine and
  7216. running RemoveDeadCodeAfterJump for each RET instruction, because
  7217. this optimisation rarely happens and most RETs appear at the end of
  7218. routines where there is nothing that can be stripped. [Kit] }
  7219. if not ThisLabel.is_used then
  7220. RemoveDeadCodeAfterJump(p);
  7221. end;
  7222. function TX86AsmOptimizer.OptPass2SETcc(var p: tai): boolean;
  7223. var
  7224. hp1,hp2,next: tai; SetC, JumpC: TAsmCond;
  7225. Unconditional, PotentialModified: Boolean;
  7226. OperPtr: POper;
  7227. NewRef: TReference;
  7228. InstrList: array of taicpu;
  7229. InstrMax, Index: Integer;
  7230. const
  7231. {$ifdef DEBUG_AOPTCPU}
  7232. SNoFlags: shortstring = ' so the flags aren''t modified';
  7233. {$else DEBUG_AOPTCPU}
  7234. SNoFlags = '';
  7235. {$endif DEBUG_AOPTCPU}
  7236. begin
  7237. Result:=false;
  7238. if MatchOpType(taicpu(p),top_reg) and GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  7239. begin
  7240. if MatchInstruction(hp1, A_TEST, [S_B]) and
  7241. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  7242. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  7243. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  7244. GetNextInstruction(hp1, hp2) and
  7245. MatchInstruction(hp2, A_Jcc, A_SETcc, []) then
  7246. { Change from: To:
  7247. set(C) %reg j(~C) label
  7248. test %reg,%reg/cmp $0,%reg
  7249. je label
  7250. set(C) %reg j(C) label
  7251. test %reg,%reg/cmp $0,%reg
  7252. jne label
  7253. (Also do something similar with sete/setne instead of je/jne)
  7254. }
  7255. begin
  7256. { Before we do anything else, we need to check the instructions
  7257. in between SETcc and TEST to make sure they don't modify the
  7258. FLAGS register - if -O2 or under, there won't be any
  7259. instructions between SET and TEST }
  7260. TransferUsedRegs(TmpUsedRegs);
  7261. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  7262. if (cs_opt_level3 in current_settings.optimizerswitches) then
  7263. begin
  7264. next := p;
  7265. SetLength(InstrList, 0);
  7266. InstrMax := -1;
  7267. PotentialModified := False;
  7268. { Make a note of every instruction that modifies the FLAGS
  7269. register }
  7270. while GetNextInstruction(next, next) and (next <> hp1) do
  7271. begin
  7272. if next.typ <> ait_instruction then
  7273. { GetNextInstructionUsingReg should have returned False }
  7274. InternalError(2021051701);
  7275. if RegModifiedByInstruction(NR_DEFAULTFLAGS, next) then
  7276. begin
  7277. case taicpu(next).opcode of
  7278. A_SETcc,
  7279. A_CMOVcc,
  7280. A_Jcc:
  7281. begin
  7282. if PotentialModified then
  7283. { Not safe because the flags were modified earlier }
  7284. Exit
  7285. else
  7286. { Condition is the same as the initial SETcc, so this is safe
  7287. (don't add to instruction list though) }
  7288. Continue;
  7289. end;
  7290. A_ADD:
  7291. begin
  7292. if (taicpu(next).opsize = S_B) or
  7293. { LEA doesn't support 8-bit operands }
  7294. (taicpu(next).oper[1]^.typ <> top_reg) or
  7295. { Must write to a register }
  7296. (taicpu(next).oper[0]^.typ = top_ref) then
  7297. { Require a constant or a register }
  7298. Exit;
  7299. PotentialModified := True;
  7300. end;
  7301. A_SUB:
  7302. begin
  7303. if (taicpu(next).opsize = S_B) or
  7304. { LEA doesn't support 8-bit operands }
  7305. (taicpu(next).oper[1]^.typ <> top_reg) or
  7306. { Must write to a register }
  7307. (taicpu(next).oper[0]^.typ <> top_const) or
  7308. (taicpu(next).oper[0]^.val = $80000000) then
  7309. { Can't subtract a register with LEA - also
  7310. check that the value isn't -2^31, as this
  7311. can't be negated }
  7312. Exit;
  7313. PotentialModified := True;
  7314. end;
  7315. A_SAL,
  7316. A_SHL:
  7317. begin
  7318. if (taicpu(next).opsize = S_B) or
  7319. { LEA doesn't support 8-bit operands }
  7320. (taicpu(next).oper[1]^.typ <> top_reg) or
  7321. { Must write to a register }
  7322. (taicpu(next).oper[0]^.typ <> top_const) or
  7323. (taicpu(next).oper[0]^.val < 0) or
  7324. (taicpu(next).oper[0]^.val > 3) then
  7325. Exit;
  7326. PotentialModified := True;
  7327. end;
  7328. A_IMUL:
  7329. begin
  7330. if (taicpu(next).ops <> 3) or
  7331. (taicpu(next).oper[1]^.typ <> top_reg) or
  7332. { Must write to a register }
  7333. (taicpu(next).oper[2]^.val in [2,3,4,5,8,9]) then
  7334. { We can convert "imul x,%reg1,%reg2" (where x = 2, 4 or 8)
  7335. to "lea (%reg1,x),%reg2". If x = 3, 5 or 9, we can
  7336. change this to "lea (%reg1,%reg1,(x-1)),%reg2" }
  7337. Exit
  7338. else
  7339. PotentialModified := True;
  7340. end;
  7341. else
  7342. { Don't know how to change this, so abort }
  7343. Exit;
  7344. end;
  7345. { Contains highest index (so instruction count - 1) }
  7346. Inc(InstrMax);
  7347. if InstrMax > High(InstrList) then
  7348. SetLength(InstrList, InstrMax + LIST_STEP_SIZE);
  7349. InstrList[InstrMax] := taicpu(next);
  7350. end;
  7351. UpdateUsedRegs(TmpUsedRegs, tai(next.next));
  7352. end;
  7353. if not Assigned(next) or (next <> hp1) then
  7354. { It should be equal to hp1 }
  7355. InternalError(2021051702);
  7356. { Cycle through each instruction and check to see if we can
  7357. change them to versions that don't modify the flags }
  7358. if (InstrMax >= 0) then
  7359. begin
  7360. for Index := 0 to InstrMax do
  7361. case InstrList[Index].opcode of
  7362. A_ADD:
  7363. begin
  7364. DebugMsg(SPeepholeOptimization + 'ADD -> LEA' + SNoFlags, InstrList[Index]);
  7365. InstrList[Index].opcode := A_LEA;
  7366. reference_reset(NewRef, 1, []);
  7367. NewRef.base := InstrList[Index].oper[1]^.reg;
  7368. if InstrList[Index].oper[0]^.typ = top_reg then
  7369. begin
  7370. NewRef.index := InstrList[Index].oper[0]^.reg;
  7371. NewRef.scalefactor := 1;
  7372. end
  7373. else
  7374. NewRef.offset := InstrList[Index].oper[0]^.val;
  7375. InstrList[Index].loadref(0, NewRef);
  7376. end;
  7377. A_SUB:
  7378. begin
  7379. DebugMsg(SPeepholeOptimization + 'SUB -> LEA' + SNoFlags, InstrList[Index]);
  7380. InstrList[Index].opcode := A_LEA;
  7381. reference_reset(NewRef, 1, []);
  7382. NewRef.base := InstrList[Index].oper[1]^.reg;
  7383. NewRef.offset := -InstrList[Index].oper[0]^.val;
  7384. InstrList[Index].loadref(0, NewRef);
  7385. end;
  7386. A_SHL,
  7387. A_SAL:
  7388. begin
  7389. DebugMsg(SPeepholeOptimization + 'SHL -> LEA' + SNoFlags, InstrList[Index]);
  7390. InstrList[Index].opcode := A_LEA;
  7391. reference_reset(NewRef, 1, []);
  7392. NewRef.index := InstrList[Index].oper[1]^.reg;
  7393. NewRef.scalefactor := 1 shl (InstrList[Index].oper[0]^.val);
  7394. InstrList[Index].loadref(0, NewRef);
  7395. end;
  7396. A_IMUL:
  7397. begin
  7398. DebugMsg(SPeepholeOptimization + 'IMUL -> LEA' + SNoFlags, InstrList[Index]);
  7399. InstrList[Index].opcode := A_LEA;
  7400. reference_reset(NewRef, 1, []);
  7401. NewRef.index := InstrList[Index].oper[1]^.reg;
  7402. case InstrList[Index].oper[0]^.val of
  7403. 2, 4, 8:
  7404. NewRef.scalefactor := InstrList[Index].oper[0]^.val;
  7405. else {3, 5 and 9}
  7406. begin
  7407. NewRef.scalefactor := InstrList[Index].oper[0]^.val - 1;
  7408. NewRef.base := InstrList[Index].oper[1]^.reg;
  7409. end;
  7410. end;
  7411. InstrList[Index].loadref(0, NewRef);
  7412. end;
  7413. else
  7414. InternalError(2021051710);
  7415. end;
  7416. end;
  7417. { Mark the FLAGS register as used across this whole block }
  7418. AllocRegBetween(NR_DEFAULTFLAGS, p, hp1, UsedRegs);
  7419. end;
  7420. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  7421. JumpC := taicpu(hp2).condition;
  7422. Unconditional := False;
  7423. if conditions_equal(JumpC, C_E) then
  7424. SetC := inverse_cond(taicpu(p).condition)
  7425. else if conditions_equal(JumpC, C_NE) then
  7426. SetC := taicpu(p).condition
  7427. else
  7428. { We've got something weird here (and inefficent) }
  7429. begin
  7430. DebugMsg('DEBUG: Inefficient jump - check code generation', p);
  7431. SetC := C_NONE;
  7432. { JAE/JNB will always branch (use 'condition_in', since C_AE <> C_NB normally) }
  7433. if condition_in(C_AE, JumpC) then
  7434. Unconditional := True
  7435. else
  7436. { Not sure what to do with this jump - drop out }
  7437. Exit;
  7438. end;
  7439. RemoveInstruction(hp1);
  7440. if Unconditional then
  7441. MakeUnconditional(taicpu(hp2))
  7442. else
  7443. begin
  7444. if SetC = C_NONE then
  7445. InternalError(2018061402);
  7446. taicpu(hp2).SetCondition(SetC);
  7447. end;
  7448. { as hp2 is a jump, we cannot use RegUsedAfterInstruction but we have to check if it is included in
  7449. TmpUsedRegs }
  7450. if not TmpUsedRegs[getregtype(taicpu(p).oper[0]^.reg)].IsUsed(taicpu(p).oper[0]^.reg) then
  7451. begin
  7452. RemoveCurrentp(p, hp2);
  7453. if taicpu(hp2).opcode = A_SETcc then
  7454. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/SETcc -> SETcc',p)
  7455. else
  7456. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> Jcc',p);
  7457. end
  7458. else
  7459. if taicpu(hp2).opcode = A_SETcc then
  7460. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/SETcc -> SETcc/SETcc',p)
  7461. else
  7462. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> SETcc/Jcc',p);
  7463. Result := True;
  7464. end
  7465. else if
  7466. { Make sure the instructions are adjacent }
  7467. (
  7468. not (cs_opt_level3 in current_settings.optimizerswitches) or
  7469. GetNextInstruction(p, hp1)
  7470. ) and
  7471. MatchInstruction(hp1, A_MOV, [S_B]) and
  7472. { Writing to memory is allowed }
  7473. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) then
  7474. begin
  7475. {
  7476. Watch out for sequences such as:
  7477. set(c)b %regb
  7478. movb %regb,(ref)
  7479. movb $0,1(ref)
  7480. movb $0,2(ref)
  7481. movb $0,3(ref)
  7482. Much more efficient to turn it into:
  7483. movl $0,%regl
  7484. set(c)b %regb
  7485. movl %regl,(ref)
  7486. Or:
  7487. set(c)b %regb
  7488. movzbl %regb,%regl
  7489. movl %regl,(ref)
  7490. }
  7491. if (taicpu(hp1).oper[1]^.typ = top_ref) and
  7492. GetNextInstruction(hp1, hp2) and
  7493. MatchInstruction(hp2, A_MOV, [S_B]) and
  7494. (taicpu(hp2).oper[1]^.typ = top_ref) and
  7495. CheckMemoryWrite(taicpu(hp1), taicpu(hp2)) then
  7496. begin
  7497. { Don't do anything else except set Result to True }
  7498. end
  7499. else
  7500. begin
  7501. if taicpu(p).oper[0]^.typ = top_reg then
  7502. begin
  7503. TransferUsedRegs(TmpUsedRegs);
  7504. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  7505. end;
  7506. { If it's not a register, it's a memory address }
  7507. if (taicpu(p).oper[0]^.typ <> top_reg) or RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp1, TmpUsedRegs) then
  7508. begin
  7509. { Even if the register is still in use, we can minimise the
  7510. pipeline stall by changing the MOV into another SETcc. }
  7511. taicpu(hp1).opcode := A_SETcc;
  7512. taicpu(hp1).condition := taicpu(p).condition;
  7513. if taicpu(hp1).oper[1]^.typ = top_ref then
  7514. begin
  7515. { Swapping the operand pointers like this is probably a
  7516. bit naughty, but it is far faster than using loadoper
  7517. to transfer the reference from oper[1] to oper[0] if
  7518. you take into account the extra procedure calls and
  7519. the memory allocation and deallocation required }
  7520. OperPtr := taicpu(hp1).oper[1];
  7521. taicpu(hp1).oper[1] := taicpu(hp1).oper[0];
  7522. taicpu(hp1).oper[0] := OperPtr;
  7523. end
  7524. else
  7525. taicpu(hp1).oper[0]^.reg := taicpu(hp1).oper[1]^.reg;
  7526. taicpu(hp1).clearop(1);
  7527. taicpu(hp1).ops := 1;
  7528. DebugMsg(SPeepholeOptimization + 'SETcc/Mov -> SETcc/SETcc',p);
  7529. end
  7530. else
  7531. begin
  7532. if taicpu(hp1).oper[1]^.typ = top_reg then
  7533. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,UsedRegs);
  7534. taicpu(p).loadoper(0, taicpu(hp1).oper[1]^);
  7535. RemoveInstruction(hp1);
  7536. DebugMsg(SPeepholeOptimization + 'SETcc/Mov -> SETcc',p);
  7537. end
  7538. end;
  7539. Result := True;
  7540. end;
  7541. end;
  7542. end;
  7543. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  7544. var
  7545. hp1: tai;
  7546. Count: Integer;
  7547. OrigLabel: TAsmLabel;
  7548. begin
  7549. result := False;
  7550. { Sometimes, the optimisations below can permit this }
  7551. RemoveDeadCodeAfterJump(p);
  7552. 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
  7553. (taicpu(p).oper[0]^.ref^.index=NR_NO) and (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) then
  7554. begin
  7555. OrigLabel := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  7556. { Also a side-effect of optimisations }
  7557. if CollapseZeroDistJump(p, OrigLabel) then
  7558. begin
  7559. Result := True;
  7560. Exit;
  7561. end;
  7562. hp1 := GetLabelWithSym(OrigLabel);
  7563. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and (hp1.typ = ait_instruction) then
  7564. begin
  7565. case taicpu(hp1).opcode of
  7566. A_RET:
  7567. {
  7568. change
  7569. jmp .L1
  7570. ...
  7571. .L1:
  7572. ret
  7573. into
  7574. ret
  7575. }
  7576. begin
  7577. ConvertJumpToRET(p, hp1);
  7578. result:=true;
  7579. end;
  7580. { Check any kind of direct assignment instruction }
  7581. A_MOV,
  7582. A_MOVD,
  7583. A_MOVQ,
  7584. A_MOVSX,
  7585. {$ifdef x86_64}
  7586. A_MOVSXD,
  7587. {$endif x86_64}
  7588. A_MOVZX,
  7589. A_MOVAPS,
  7590. A_MOVUPS,
  7591. A_MOVSD,
  7592. A_MOVAPD,
  7593. A_MOVUPD,
  7594. A_MOVDQA,
  7595. A_MOVDQU,
  7596. A_VMOVSS,
  7597. A_VMOVAPS,
  7598. A_VMOVUPS,
  7599. A_VMOVSD,
  7600. A_VMOVAPD,
  7601. A_VMOVUPD,
  7602. A_VMOVDQA,
  7603. A_VMOVDQU:
  7604. if ((current_settings.optimizerswitches * [cs_opt_level3, cs_opt_size]) <> [cs_opt_size]) and
  7605. CheckJumpMovTransferOpt(p, hp1, 0, Count) then
  7606. begin
  7607. Result := True;
  7608. Exit;
  7609. end;
  7610. else
  7611. ;
  7612. end;
  7613. end;
  7614. end;
  7615. end;
  7616. class function TX86AsmOptimizer.CanBeCMOV(p : tai) : boolean;
  7617. begin
  7618. CanBeCMOV:=assigned(p) and
  7619. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  7620. { we can't use cmov ref,reg because
  7621. ref could be nil and cmov still throws an exception
  7622. if ref=nil but the mov isn't done (FK)
  7623. or ((taicpu(p).oper[0]^.typ = top_ref) and
  7624. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  7625. }
  7626. (taicpu(p).oper[1]^.typ = top_reg) and
  7627. (
  7628. (taicpu(p).oper[0]^.typ = top_reg) or
  7629. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  7630. it is not expected that this can cause a seg. violation }
  7631. (
  7632. (taicpu(p).oper[0]^.typ = top_ref) and
  7633. IsRefSafe(taicpu(p).oper[0]^.ref)
  7634. )
  7635. );
  7636. end;
  7637. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  7638. var
  7639. hp1,hp2: tai;
  7640. {$ifndef i8086}
  7641. hp3,hp4,hpmov2, hp5: tai;
  7642. l : Longint;
  7643. condition : TAsmCond;
  7644. {$endif i8086}
  7645. carryadd_opcode : TAsmOp;
  7646. symbol: TAsmSymbol;
  7647. reg: tsuperregister;
  7648. increg, tmpreg: TRegister;
  7649. begin
  7650. result:=false;
  7651. if GetNextInstruction(p,hp1) and (hp1.typ=ait_instruction) then
  7652. begin
  7653. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  7654. if (
  7655. (
  7656. ((Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB)) and
  7657. MatchOptype(Taicpu(hp1),top_const,top_reg) and
  7658. (Taicpu(hp1).oper[0]^.val=1)
  7659. ) or
  7660. ((Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC))
  7661. ) and
  7662. GetNextInstruction(hp1,hp2) and
  7663. SkipAligns(hp2, hp2) and
  7664. (hp2.typ = ait_label) and
  7665. (Tasmlabel(symbol) = Tai_label(hp2).labsym) then
  7666. { jb @@1 cmc
  7667. inc/dec operand --> adc/sbb operand,0
  7668. @@1:
  7669. ... and ...
  7670. jnb @@1
  7671. inc/dec operand --> adc/sbb operand,0
  7672. @@1: }
  7673. begin
  7674. if Taicpu(p).condition in [C_NAE,C_B,C_C] then
  7675. begin
  7676. case taicpu(hp1).opcode of
  7677. A_INC,
  7678. A_ADD:
  7679. carryadd_opcode:=A_ADC;
  7680. A_DEC,
  7681. A_SUB:
  7682. carryadd_opcode:=A_SBB;
  7683. else
  7684. InternalError(2021011001);
  7685. end;
  7686. Taicpu(p).clearop(0);
  7687. Taicpu(p).ops:=0;
  7688. Taicpu(p).is_jmp:=false;
  7689. Taicpu(p).opcode:=A_CMC;
  7690. Taicpu(p).condition:=C_NONE;
  7691. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2CmcAdc/Sbb',p);
  7692. Taicpu(hp1).ops:=2;
  7693. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  7694. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  7695. else
  7696. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  7697. Taicpu(hp1).loadconst(0,0);
  7698. Taicpu(hp1).opcode:=carryadd_opcode;
  7699. result:=true;
  7700. exit;
  7701. end
  7702. else if Taicpu(p).condition in [C_AE,C_NB,C_NC] then
  7703. begin
  7704. case taicpu(hp1).opcode of
  7705. A_INC,
  7706. A_ADD:
  7707. carryadd_opcode:=A_ADC;
  7708. A_DEC,
  7709. A_SUB:
  7710. carryadd_opcode:=A_SBB;
  7711. else
  7712. InternalError(2021011002);
  7713. end;
  7714. Taicpu(hp1).ops:=2;
  7715. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2Adc/Sbb',p);
  7716. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  7717. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  7718. else
  7719. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  7720. Taicpu(hp1).loadconst(0,0);
  7721. Taicpu(hp1).opcode:=carryadd_opcode;
  7722. RemoveCurrentP(p, hp1);
  7723. result:=true;
  7724. exit;
  7725. end
  7726. {
  7727. jcc @@1 setcc tmpreg
  7728. inc/dec/add/sub operand -> (movzx tmpreg)
  7729. @@1: add/sub tmpreg,operand
  7730. While this increases code size slightly, it makes the code much faster if the
  7731. jump is unpredictable
  7732. }
  7733. else if not(cs_opt_size in current_settings.optimizerswitches) then
  7734. begin
  7735. { search for an available register which is volatile }
  7736. for reg in tcpuregisterset do
  7737. begin
  7738. if
  7739. {$if defined(i386) or defined(i8086)}
  7740. { Only use registers whose lowest 8-bits can Be accessed }
  7741. (reg in [RS_EAX,RS_EBX,RS_ECX,RS_EDX]) and
  7742. {$endif i386 or i8086}
  7743. (reg in paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption)) and
  7744. not(reg in UsedRegs[R_INTREGISTER].GetUsedRegs)
  7745. { We don't need to check if tmpreg is in hp1 or not, because
  7746. it will be marked as in use at p (if not, this is
  7747. indictive of a compiler bug). }
  7748. then
  7749. begin
  7750. TAsmLabel(symbol).decrefs;
  7751. increg := newreg(R_INTREGISTER,reg,R_SUBL);
  7752. Taicpu(p).clearop(0);
  7753. Taicpu(p).ops:=1;
  7754. Taicpu(p).is_jmp:=false;
  7755. Taicpu(p).opcode:=A_SETcc;
  7756. DebugMsg(SPeepholeOptimization+'JccAdd2SetccAdd',p);
  7757. Taicpu(p).condition:=inverse_cond(Taicpu(p).condition);
  7758. Taicpu(p).loadreg(0,increg);
  7759. if getsubreg(Taicpu(hp1).oper[1]^.reg)<>R_SUBL then
  7760. begin
  7761. case getsubreg(Taicpu(hp1).oper[1]^.reg) of
  7762. R_SUBW:
  7763. begin
  7764. tmpreg := newreg(R_INTREGISTER,reg,R_SUBW);
  7765. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BW,increg,tmpreg);
  7766. end;
  7767. R_SUBD:
  7768. begin
  7769. tmpreg := newreg(R_INTREGISTER,reg,R_SUBD);
  7770. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,tmpreg);
  7771. end;
  7772. {$ifdef x86_64}
  7773. R_SUBQ:
  7774. begin
  7775. { MOVZX doesn't have a 64-bit variant, because
  7776. the 32-bit version implicitly zeroes the
  7777. upper 32-bits of the destination register }
  7778. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,
  7779. newreg(R_INTREGISTER,reg,R_SUBD));
  7780. tmpreg := newreg(R_INTREGISTER,reg,R_SUBQ);
  7781. end;
  7782. {$endif x86_64}
  7783. else
  7784. Internalerror(2020030601);
  7785. end;
  7786. taicpu(hp2).fileinfo:=taicpu(hp1).fileinfo;
  7787. asml.InsertAfter(hp2,p);
  7788. end
  7789. else
  7790. tmpreg := increg;
  7791. if (Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC) then
  7792. begin
  7793. Taicpu(hp1).ops:=2;
  7794. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^)
  7795. end;
  7796. Taicpu(hp1).loadreg(0,tmpreg);
  7797. AllocRegBetween(tmpreg,p,hp1,UsedRegs);
  7798. Result := True;
  7799. { p is no longer a Jcc instruction, so exit }
  7800. Exit;
  7801. end;
  7802. end;
  7803. end;
  7804. end;
  7805. { Detect the following:
  7806. jmp<cond> @Lbl1
  7807. jmp @Lbl2
  7808. ...
  7809. @Lbl1:
  7810. ret
  7811. Change to:
  7812. jmp<inv_cond> @Lbl2
  7813. ret
  7814. }
  7815. if MatchInstruction(hp1,A_JMP,[]) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  7816. begin
  7817. hp2:=getlabelwithsym(TAsmLabel(symbol));
  7818. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  7819. MatchInstruction(hp2,A_RET,[S_NO]) then
  7820. begin
  7821. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  7822. { Change label address to that of the unconditional jump }
  7823. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  7824. TAsmLabel(symbol).DecRefs;
  7825. taicpu(hp1).opcode := A_RET;
  7826. taicpu(hp1).is_jmp := false;
  7827. taicpu(hp1).ops := taicpu(hp2).ops;
  7828. DebugMsg(SPeepholeOptimization+'JccJmpRet2J!ccRet',p);
  7829. case taicpu(hp2).ops of
  7830. 0:
  7831. taicpu(hp1).clearop(0);
  7832. 1:
  7833. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  7834. else
  7835. internalerror(2016041302);
  7836. end;
  7837. end;
  7838. {$ifndef i8086}
  7839. end
  7840. {
  7841. convert
  7842. j<c> .L1
  7843. mov 1,reg
  7844. jmp .L2
  7845. .L1
  7846. mov 0,reg
  7847. .L2
  7848. into
  7849. mov 0,reg
  7850. set<not(c)> reg
  7851. take care of alignment and that the mov 0,reg is not converted into a xor as this
  7852. would destroy the flag contents
  7853. }
  7854. else if MatchInstruction(hp1,A_MOV,[]) and
  7855. MatchOpType(taicpu(hp1),top_const,top_reg) and
  7856. {$ifdef i386}
  7857. (
  7858. { Under i386, ESI, EDI, EBP and ESP
  7859. don't have an 8-bit representation }
  7860. not (getsupreg(taicpu(hp1).oper[1]^.reg) in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  7861. ) and
  7862. {$endif i386}
  7863. (taicpu(hp1).oper[0]^.val=1) and
  7864. GetNextInstruction(hp1,hp2) and
  7865. MatchInstruction(hp2,A_JMP,[]) and (taicpu(hp2).oper[0]^.ref^.refaddr=addr_full) and
  7866. GetNextInstruction(hp2,hp3) and
  7867. { skip align }
  7868. ((hp3.typ<>ait_align) or GetNextInstruction(hp3,hp3)) and
  7869. (hp3.typ=ait_label) and
  7870. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol)=tai_label(hp3).labsym) and
  7871. (tai_label(hp3).labsym.getrefs=1) and
  7872. GetNextInstruction(hp3,hp4) and
  7873. MatchInstruction(hp4,A_MOV,[]) and
  7874. MatchOpType(taicpu(hp4),top_const,top_reg) and
  7875. (taicpu(hp4).oper[0]^.val=0) and
  7876. MatchOperand(taicpu(hp1).oper[1]^,taicpu(hp4).oper[1]^) and
  7877. GetNextInstruction(hp4,hp5) and
  7878. (hp5.typ=ait_label) and
  7879. (tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol)=tai_label(hp5).labsym) and
  7880. (tai_label(hp5).labsym.getrefs=1) then
  7881. begin
  7882. AllocRegBetween(NR_FLAGS,p,hp4,UsedRegs);
  7883. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2MovSetcc',p);
  7884. { remove last label }
  7885. RemoveInstruction(hp5);
  7886. { remove second label }
  7887. RemoveInstruction(hp3);
  7888. { if align is present remove it }
  7889. if GetNextInstruction(hp2,hp3) and (hp3.typ=ait_align) then
  7890. RemoveInstruction(hp3);
  7891. { remove jmp }
  7892. RemoveInstruction(hp2);
  7893. if taicpu(hp1).opsize=S_B then
  7894. RemoveInstruction(hp1)
  7895. else
  7896. taicpu(hp1).loadconst(0,0);
  7897. taicpu(hp4).opcode:=A_SETcc;
  7898. taicpu(hp4).opsize:=S_B;
  7899. taicpu(hp4).condition:=inverse_cond(taicpu(p).condition);
  7900. taicpu(hp4).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(hp4).oper[1]^.reg),R_SUBL));
  7901. taicpu(hp4).opercnt:=1;
  7902. taicpu(hp4).ops:=1;
  7903. taicpu(hp4).freeop(1);
  7904. RemoveCurrentP(p);
  7905. Result:=true;
  7906. exit;
  7907. end
  7908. else if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  7909. begin
  7910. { check for
  7911. jCC xxx
  7912. <several movs>
  7913. xxx:
  7914. }
  7915. l:=0;
  7916. while assigned(hp1) and
  7917. CanBeCMOV(hp1) and
  7918. { stop on labels }
  7919. not(hp1.typ=ait_label) do
  7920. begin
  7921. inc(l);
  7922. GetNextInstruction(hp1,hp1);
  7923. end;
  7924. if assigned(hp1) then
  7925. begin
  7926. if FindLabel(tasmlabel(symbol),hp1) then
  7927. begin
  7928. if (l<=4) and (l>0) then
  7929. begin
  7930. condition:=inverse_cond(taicpu(p).condition);
  7931. GetNextInstruction(p,hp1);
  7932. repeat
  7933. if not Assigned(hp1) then
  7934. InternalError(2018062900);
  7935. taicpu(hp1).opcode:=A_CMOVcc;
  7936. taicpu(hp1).condition:=condition;
  7937. UpdateUsedRegs(hp1);
  7938. GetNextInstruction(hp1,hp1);
  7939. until not(CanBeCMOV(hp1));
  7940. { Remember what hp1 is in case there's multiple aligns to get rid of }
  7941. hp2 := hp1;
  7942. repeat
  7943. if not Assigned(hp2) then
  7944. InternalError(2018062910);
  7945. case hp2.typ of
  7946. ait_label:
  7947. { What we expected - break out of the loop (it won't be a dead label at the top of
  7948. a cluster because that was optimised at an earlier stage) }
  7949. Break;
  7950. ait_align:
  7951. { Go to the next entry until a label is found (may be multiple aligns before it) }
  7952. begin
  7953. hp2 := tai(hp2.Next);
  7954. Continue;
  7955. end;
  7956. else
  7957. begin
  7958. { Might be a comment or temporary allocation entry }
  7959. if not (hp2.typ in SkipInstr) then
  7960. InternalError(2018062911);
  7961. hp2 := tai(hp2.Next);
  7962. Continue;
  7963. end;
  7964. end;
  7965. until False;
  7966. { Now we can safely decrement the reference count }
  7967. tasmlabel(symbol).decrefs;
  7968. DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
  7969. { Remove the original jump }
  7970. RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
  7971. GetNextInstruction(hp2, p); { Instruction after the label }
  7972. { Remove the label if this is its final reference }
  7973. if (tasmlabel(symbol).getrefs=0) then
  7974. StripLabelFast(hp1);
  7975. if Assigned(p) then
  7976. begin
  7977. UpdateUsedRegs(p);
  7978. result:=true;
  7979. end;
  7980. exit;
  7981. end;
  7982. end
  7983. else
  7984. begin
  7985. { check further for
  7986. jCC xxx
  7987. <several movs 1>
  7988. jmp yyy
  7989. xxx:
  7990. <several movs 2>
  7991. yyy:
  7992. }
  7993. { hp2 points to jmp yyy }
  7994. hp2:=hp1;
  7995. { skip hp1 to xxx (or an align right before it) }
  7996. GetNextInstruction(hp1, hp1);
  7997. if assigned(hp2) and
  7998. assigned(hp1) and
  7999. (l<=3) and
  8000. (hp2.typ=ait_instruction) and
  8001. (taicpu(hp2).is_jmp) and
  8002. (taicpu(hp2).condition=C_None) and
  8003. { real label and jump, no further references to the
  8004. label are allowed }
  8005. (tasmlabel(symbol).getrefs=1) and
  8006. FindLabel(tasmlabel(symbol),hp1) then
  8007. begin
  8008. l:=0;
  8009. { skip hp1 to <several moves 2> }
  8010. if (hp1.typ = ait_align) then
  8011. GetNextInstruction(hp1, hp1);
  8012. GetNextInstruction(hp1, hpmov2);
  8013. hp1 := hpmov2;
  8014. while assigned(hp1) and
  8015. CanBeCMOV(hp1) do
  8016. begin
  8017. inc(l);
  8018. GetNextInstruction(hp1, hp1);
  8019. end;
  8020. { hp1 points to yyy (or an align right before it) }
  8021. hp3 := hp1;
  8022. if assigned(hp1) and
  8023. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  8024. begin
  8025. condition:=inverse_cond(taicpu(p).condition);
  8026. GetNextInstruction(p,hp1);
  8027. repeat
  8028. taicpu(hp1).opcode:=A_CMOVcc;
  8029. taicpu(hp1).condition:=condition;
  8030. UpdateUsedRegs(hp1);
  8031. GetNextInstruction(hp1,hp1);
  8032. until not(assigned(hp1)) or
  8033. not(CanBeCMOV(hp1));
  8034. condition:=inverse_cond(condition);
  8035. hp1 := hpmov2;
  8036. { hp1 is now at <several movs 2> }
  8037. while Assigned(hp1) and CanBeCMOV(hp1) do
  8038. begin
  8039. taicpu(hp1).opcode:=A_CMOVcc;
  8040. taicpu(hp1).condition:=condition;
  8041. UpdateUsedRegs(hp1);
  8042. GetNextInstruction(hp1,hp1);
  8043. end;
  8044. hp1 := p;
  8045. { Get first instruction after label }
  8046. GetNextInstruction(hp3, p);
  8047. if assigned(p) and (hp3.typ = ait_align) then
  8048. GetNextInstruction(p, p);
  8049. { Don't dereference yet, as doing so will cause
  8050. GetNextInstruction to skip the label and
  8051. optional align marker. [Kit] }
  8052. GetNextInstruction(hp2, hp4);
  8053. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
  8054. { remove jCC }
  8055. RemoveInstruction(hp1);
  8056. { Now we can safely decrement it }
  8057. tasmlabel(symbol).decrefs;
  8058. { Remove label xxx (it will have a ref of zero due to the initial check }
  8059. StripLabelFast(hp4);
  8060. { remove jmp }
  8061. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  8062. RemoveInstruction(hp2);
  8063. { As before, now we can safely decrement it }
  8064. tasmlabel(symbol).decrefs;
  8065. { Remove label yyy (and the optional alignment) if its reference falls to zero }
  8066. if tasmlabel(symbol).getrefs = 0 then
  8067. StripLabelFast(hp3);
  8068. if Assigned(p) then
  8069. begin
  8070. UpdateUsedRegs(p);
  8071. result:=true;
  8072. end;
  8073. exit;
  8074. end;
  8075. end;
  8076. end;
  8077. end;
  8078. {$endif i8086}
  8079. end;
  8080. end;
  8081. end;
  8082. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  8083. var
  8084. hp1,hp2: tai;
  8085. reg_and_hp1_is_instr: Boolean;
  8086. begin
  8087. result:=false;
  8088. reg_and_hp1_is_instr:=(taicpu(p).oper[1]^.typ = top_reg) and
  8089. GetNextInstruction(p,hp1) and
  8090. (hp1.typ = ait_instruction);
  8091. if reg_and_hp1_is_instr and
  8092. (
  8093. (taicpu(hp1).opcode <> A_LEA) or
  8094. { If the LEA instruction can be converted into an arithmetic instruction,
  8095. it may be possible to then fold it. }
  8096. (
  8097. { If the flags register is in use, don't change the instruction
  8098. to an ADD otherwise this will scramble the flags. [Kit] }
  8099. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  8100. ConvertLEA(taicpu(hp1))
  8101. )
  8102. ) and
  8103. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  8104. GetNextInstruction(hp1,hp2) and
  8105. MatchInstruction(hp2,A_MOV,[]) and
  8106. (taicpu(hp2).oper[0]^.typ = top_reg) and
  8107. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  8108. ((taicpu(p).opsize in [S_BW,S_BL]) and (taicpu(hp2).opsize=S_B) or
  8109. (taicpu(p).opsize in [S_WL]) and (taicpu(hp2).opsize=S_W)) and
  8110. {$ifdef i386}
  8111. { not all registers have byte size sub registers on i386 }
  8112. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  8113. {$endif i386}
  8114. (((taicpu(hp1).ops=2) and
  8115. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  8116. ((taicpu(hp1).ops=1) and
  8117. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  8118. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  8119. begin
  8120. { change movsX/movzX reg/ref, reg2
  8121. add/sub/or/... reg3/$const, reg2
  8122. mov reg2 reg/ref
  8123. to add/sub/or/... reg3/$const, reg/ref }
  8124. { by example:
  8125. movswl %si,%eax movswl %si,%eax p
  8126. decl %eax addl %edx,%eax hp1
  8127. movw %ax,%si movw %ax,%si hp2
  8128. ->
  8129. movswl %si,%eax movswl %si,%eax p
  8130. decw %eax addw %edx,%eax hp1
  8131. movw %ax,%si movw %ax,%si hp2
  8132. }
  8133. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  8134. {
  8135. ->
  8136. movswl %si,%eax movswl %si,%eax p
  8137. decw %si addw %dx,%si hp1
  8138. movw %ax,%si movw %ax,%si hp2
  8139. }
  8140. case taicpu(hp1).ops of
  8141. 1:
  8142. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  8143. 2:
  8144. begin
  8145. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  8146. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  8147. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  8148. end;
  8149. else
  8150. internalerror(2008042702);
  8151. end;
  8152. {
  8153. ->
  8154. decw %si addw %dx,%si p
  8155. }
  8156. DebugMsg(SPeepholeOptimization + 'var3',p);
  8157. RemoveCurrentP(p, hp1);
  8158. RemoveInstruction(hp2);
  8159. end
  8160. else if reg_and_hp1_is_instr and
  8161. (taicpu(hp1).opcode = A_MOV) and
  8162. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  8163. (MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^)
  8164. {$ifdef x86_64}
  8165. { check for implicit extension to 64 bit }
  8166. or
  8167. ((taicpu(p).opsize in [S_BL,S_WL]) and
  8168. (taicpu(hp1).opsize=S_Q) and
  8169. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg)
  8170. )
  8171. {$endif x86_64}
  8172. )
  8173. then
  8174. begin
  8175. { change
  8176. movx %reg1,%reg2
  8177. mov %reg2,%reg3
  8178. dealloc %reg2
  8179. into
  8180. movx %reg,%reg3
  8181. }
  8182. TransferUsedRegs(TmpUsedRegs);
  8183. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  8184. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  8185. begin
  8186. DebugMsg(SPeepholeOptimization + 'MovxMov2Movx',p);
  8187. {$ifdef x86_64}
  8188. if (taicpu(p).opsize in [S_BL,S_WL]) and
  8189. (taicpu(hp1).opsize=S_Q) then
  8190. taicpu(p).loadreg(1,newreg(R_INTREGISTER,getsupreg(taicpu(hp1).oper[1]^.reg),R_SUBD))
  8191. else
  8192. {$endif x86_64}
  8193. taicpu(p).loadreg(1,taicpu(hp1).oper[1]^.reg);
  8194. RemoveInstruction(hp1);
  8195. end;
  8196. end
  8197. else if reg_and_hp1_is_instr and
  8198. ((taicpu(hp1).opcode=A_MOV) or
  8199. (taicpu(hp1).opcode=A_ADD) or
  8200. (taicpu(hp1).opcode=A_SUB) or
  8201. (taicpu(hp1).opcode=A_CMP) or
  8202. (taicpu(hp1).opcode=A_OR) or
  8203. (taicpu(hp1).opcode=A_XOR) or
  8204. (taicpu(hp1).opcode=A_AND)
  8205. ) and
  8206. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  8207. (((taicpu(p).opsize in [S_BW,S_BL,S_WL{$ifdef x86_64},S_BQ,S_WQ,S_LQ{$endif x86_64}]) and
  8208. (taicpu(hp1).opsize=S_B)) or
  8209. ((taicpu(p).opsize in [S_WL{$ifdef x86_64},S_WQ,S_LQ{$endif x86_64}]) and
  8210. (taicpu(hp1).opsize=S_W))
  8211. {$ifdef x86_64}
  8212. or ((taicpu(p).opsize=S_LQ) and
  8213. (taicpu(hp1).opsize=S_L))
  8214. {$endif x86_64}
  8215. ) and
  8216. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg) then
  8217. begin
  8218. { change
  8219. movx %reg1,%reg2
  8220. mov %reg2,%reg3
  8221. dealloc %reg2
  8222. into
  8223. mov %reg1,%reg3
  8224. if the second mov accesses only the bits stored in reg1
  8225. }
  8226. TransferUsedRegs(TmpUsedRegs);
  8227. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  8228. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  8229. begin
  8230. DebugMsg(SPeepholeOptimization + 'MovxOp2Op',p);
  8231. if taicpu(p).oper[0]^.typ=top_reg then
  8232. begin
  8233. case taicpu(hp1).opsize of
  8234. S_B:
  8235. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBL));
  8236. S_W:
  8237. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBW));
  8238. S_L:
  8239. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBD));
  8240. else
  8241. Internalerror(2020102301);
  8242. end;
  8243. AllocRegBetween(taicpu(hp1).oper[0]^.reg,p,hp1,UsedRegs);
  8244. end
  8245. else
  8246. taicpu(hp1).loadref(0,taicpu(p).oper[0]^.ref^);
  8247. RemoveCurrentP(p);
  8248. result:=true;
  8249. exit;
  8250. end;
  8251. end
  8252. else if reg_and_hp1_is_instr and
  8253. (taicpu(p).oper[0]^.typ = top_reg) and
  8254. (
  8255. (taicpu(hp1).opcode = A_SHL) or (taicpu(hp1).opcode = A_SAL)
  8256. ) and
  8257. (taicpu(hp1).oper[0]^.typ = top_const) and
  8258. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  8259. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  8260. { Minimum shift value allowed is the bit difference between the sizes }
  8261. (taicpu(hp1).oper[0]^.val >=
  8262. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  8263. 8 * (
  8264. tcgsize2size[reg_cgsize(taicpu(p).oper[1]^.reg)] -
  8265. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  8266. )
  8267. ) then
  8268. begin
  8269. { For:
  8270. movsx/movzx %reg1,%reg1 (same register, just different sizes)
  8271. shl/sal ##, %reg1
  8272. Remove the movsx/movzx instruction if the shift overwrites the
  8273. extended bits of the register (e.g. movslq %eax,%rax; shlq $32,%rax
  8274. }
  8275. DebugMsg(SPeepholeOptimization + 'MovxShl2Shl',p);
  8276. RemoveCurrentP(p, hp1);
  8277. Result := True;
  8278. Exit;
  8279. end
  8280. else if reg_and_hp1_is_instr and
  8281. (taicpu(p).oper[0]^.typ = top_reg) and
  8282. (
  8283. ((taicpu(hp1).opcode = A_SHR) and (taicpu(p).opcode = A_MOVZX)) or
  8284. ((taicpu(hp1).opcode = A_SAR) and (taicpu(p).opcode <> A_MOVZX))
  8285. ) and
  8286. (taicpu(hp1).oper[0]^.typ = top_const) and
  8287. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  8288. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  8289. { Minimum shift value allowed is the bit size of the smallest register - 1 }
  8290. (taicpu(hp1).oper[0]^.val <
  8291. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  8292. 8 * (
  8293. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  8294. )
  8295. ) then
  8296. begin
  8297. { For:
  8298. movsx %reg1,%reg1 movzx %reg1,%reg1 (same register, just different sizes)
  8299. sar ##, %reg1 shr ##, %reg1
  8300. Move the shift to before the movx instruction if the shift value
  8301. is not too large.
  8302. }
  8303. asml.Remove(hp1);
  8304. asml.InsertBefore(hp1, p);
  8305. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  8306. case taicpu(p).opsize of
  8307. s_BW, S_BL{$ifdef x86_64}, S_BQ{$endif}:
  8308. taicpu(hp1).opsize := S_B;
  8309. S_WL{$ifdef x86_64}, S_WQ{$endif}:
  8310. taicpu(hp1).opsize := S_W;
  8311. {$ifdef x86_64}
  8312. S_LQ:
  8313. taicpu(hp1).opsize := S_L;
  8314. {$endif}
  8315. else
  8316. InternalError(2020112401);
  8317. end;
  8318. if (taicpu(hp1).opcode = A_SHR) then
  8319. DebugMsg(SPeepholeOptimization + 'MovzShr2ShrMovz', hp1)
  8320. else
  8321. DebugMsg(SPeepholeOptimization + 'MovsSar2SarMovs', hp1);
  8322. Result := True;
  8323. end
  8324. else if taicpu(p).opcode=A_MOVZX then
  8325. begin
  8326. { removes superfluous And's after movzx's }
  8327. if reg_and_hp1_is_instr and
  8328. (taicpu(hp1).opcode = A_AND) and
  8329. MatchOpType(taicpu(hp1),top_const,top_reg) and
  8330. ((taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)
  8331. {$ifdef x86_64}
  8332. { check for implicit extension to 64 bit }
  8333. or
  8334. ((taicpu(p).opsize in [S_BL,S_WL]) and
  8335. (taicpu(hp1).opsize=S_Q) and
  8336. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg)
  8337. )
  8338. {$endif x86_64}
  8339. )
  8340. then
  8341. begin
  8342. case taicpu(p).opsize Of
  8343. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  8344. if (taicpu(hp1).oper[0]^.val = $ff) then
  8345. begin
  8346. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz1',p);
  8347. RemoveInstruction(hp1);
  8348. Result:=true;
  8349. exit;
  8350. end;
  8351. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  8352. if (taicpu(hp1).oper[0]^.val = $ffff) then
  8353. begin
  8354. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz2',p);
  8355. RemoveInstruction(hp1);
  8356. Result:=true;
  8357. exit;
  8358. end;
  8359. {$ifdef x86_64}
  8360. S_LQ:
  8361. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  8362. begin
  8363. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz3',p);
  8364. RemoveInstruction(hp1);
  8365. Result:=true;
  8366. exit;
  8367. end;
  8368. {$endif x86_64}
  8369. else
  8370. ;
  8371. end;
  8372. { we cannot get rid of the and, but can we get rid of the movz ?}
  8373. if SuperRegistersEqual(taicpu(p).oper[0]^.reg,taicpu(p).oper[1]^.reg) then
  8374. begin
  8375. case taicpu(p).opsize Of
  8376. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  8377. if (taicpu(hp1).oper[0]^.val and $ff)=taicpu(hp1).oper[0]^.val then
  8378. begin
  8379. DebugMsg(SPeepholeOptimization + 'MovzAnd2And1',p);
  8380. RemoveCurrentP(p,hp1);
  8381. Result:=true;
  8382. exit;
  8383. end;
  8384. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  8385. if (taicpu(hp1).oper[0]^.val and $ffff)=taicpu(hp1).oper[0]^.val then
  8386. begin
  8387. DebugMsg(SPeepholeOptimization + 'MovzAnd2And2',p);
  8388. RemoveCurrentP(p,hp1);
  8389. Result:=true;
  8390. exit;
  8391. end;
  8392. {$ifdef x86_64}
  8393. S_LQ:
  8394. if (taicpu(hp1).oper[0]^.val and $ffffffff)=taicpu(hp1).oper[0]^.val then
  8395. begin
  8396. DebugMsg(SPeepholeOptimization + 'MovzAnd2And3',p);
  8397. RemoveCurrentP(p,hp1);
  8398. Result:=true;
  8399. exit;
  8400. end;
  8401. {$endif x86_64}
  8402. else
  8403. ;
  8404. end;
  8405. end;
  8406. end;
  8407. { changes some movzx constructs to faster synonyms (all examples
  8408. are given with eax/ax, but are also valid for other registers)}
  8409. if MatchOpType(taicpu(p),top_reg,top_reg) then
  8410. begin
  8411. case taicpu(p).opsize of
  8412. { Technically, movzbw %al,%ax cannot be encoded in 32/64-bit mode
  8413. (the machine code is equivalent to movzbl %al,%eax), but the
  8414. code generator still generates that assembler instruction and
  8415. it is silently converted. This should probably be checked.
  8416. [Kit] }
  8417. S_BW:
  8418. begin
  8419. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  8420. (
  8421. not IsMOVZXAcceptable
  8422. { and $0xff,%ax has a smaller encoding but risks a partial write penalty }
  8423. or (
  8424. (cs_opt_size in current_settings.optimizerswitches) and
  8425. (taicpu(p).oper[1]^.reg = NR_AX)
  8426. )
  8427. ) then
  8428. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  8429. begin
  8430. DebugMsg(SPeepholeOptimization + 'var7',p);
  8431. taicpu(p).opcode := A_AND;
  8432. taicpu(p).changeopsize(S_W);
  8433. taicpu(p).loadConst(0,$ff);
  8434. Result := True;
  8435. end
  8436. else if not IsMOVZXAcceptable and
  8437. GetNextInstruction(p, hp1) and
  8438. (tai(hp1).typ = ait_instruction) and
  8439. (taicpu(hp1).opcode = A_AND) and
  8440. MatchOpType(taicpu(hp1),top_const,top_reg) and
  8441. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  8442. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  8443. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  8444. begin
  8445. DebugMsg(SPeepholeOptimization + 'var8',p);
  8446. taicpu(p).opcode := A_MOV;
  8447. taicpu(p).changeopsize(S_W);
  8448. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  8449. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  8450. Result := True;
  8451. end;
  8452. end;
  8453. {$ifndef i8086} { movzbl %al,%eax cannot be encoded in 16-bit mode (the machine code is equivalent to movzbw %al,%ax }
  8454. S_BL:
  8455. begin
  8456. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  8457. (
  8458. not IsMOVZXAcceptable
  8459. { and $0xff,%eax has a smaller encoding but risks a partial write penalty }
  8460. or (
  8461. (cs_opt_size in current_settings.optimizerswitches) and
  8462. (taicpu(p).oper[1]^.reg = NR_EAX)
  8463. )
  8464. ) then
  8465. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  8466. begin
  8467. DebugMsg(SPeepholeOptimization + 'var9',p);
  8468. taicpu(p).opcode := A_AND;
  8469. taicpu(p).changeopsize(S_L);
  8470. taicpu(p).loadConst(0,$ff);
  8471. Result := True;
  8472. end
  8473. else if not IsMOVZXAcceptable and
  8474. GetNextInstruction(p, hp1) and
  8475. (tai(hp1).typ = ait_instruction) and
  8476. (taicpu(hp1).opcode = A_AND) and
  8477. MatchOpType(taicpu(hp1),top_const,top_reg) and
  8478. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  8479. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  8480. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  8481. begin
  8482. DebugMsg(SPeepholeOptimization + 'var10',p);
  8483. taicpu(p).opcode := A_MOV;
  8484. taicpu(p).changeopsize(S_L);
  8485. { do not use R_SUBWHOLE
  8486. as movl %rdx,%eax
  8487. is invalid in assembler PM }
  8488. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  8489. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  8490. Result := True;
  8491. end;
  8492. end;
  8493. {$endif i8086}
  8494. S_WL:
  8495. if not IsMOVZXAcceptable then
  8496. begin
  8497. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) then
  8498. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  8499. begin
  8500. DebugMsg(SPeepholeOptimization + 'var11',p);
  8501. taicpu(p).opcode := A_AND;
  8502. taicpu(p).changeopsize(S_L);
  8503. taicpu(p).loadConst(0,$ffff);
  8504. Result := True;
  8505. end
  8506. else if GetNextInstruction(p, hp1) and
  8507. (tai(hp1).typ = ait_instruction) and
  8508. (taicpu(hp1).opcode = A_AND) and
  8509. (taicpu(hp1).oper[0]^.typ = top_const) and
  8510. (taicpu(hp1).oper[1]^.typ = top_reg) and
  8511. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  8512. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  8513. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  8514. begin
  8515. DebugMsg(SPeepholeOptimization + 'var12',p);
  8516. taicpu(p).opcode := A_MOV;
  8517. taicpu(p).changeopsize(S_L);
  8518. { do not use R_SUBWHOLE
  8519. as movl %rdx,%eax
  8520. is invalid in assembler PM }
  8521. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  8522. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  8523. Result := True;
  8524. end;
  8525. end;
  8526. else
  8527. InternalError(2017050705);
  8528. end;
  8529. end
  8530. else if not IsMOVZXAcceptable and (taicpu(p).oper[0]^.typ = top_ref) then
  8531. begin
  8532. if GetNextInstruction(p, hp1) and
  8533. (tai(hp1).typ = ait_instruction) and
  8534. (taicpu(hp1).opcode = A_AND) and
  8535. MatchOpType(taicpu(hp1),top_const,top_reg) and
  8536. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  8537. begin
  8538. //taicpu(p).opcode := A_MOV;
  8539. case taicpu(p).opsize Of
  8540. S_BL:
  8541. begin
  8542. DebugMsg(SPeepholeOptimization + 'var13',p);
  8543. taicpu(hp1).changeopsize(S_L);
  8544. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  8545. end;
  8546. S_WL:
  8547. begin
  8548. DebugMsg(SPeepholeOptimization + 'var14',p);
  8549. taicpu(hp1).changeopsize(S_L);
  8550. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  8551. end;
  8552. S_BW:
  8553. begin
  8554. DebugMsg(SPeepholeOptimization + 'var15',p);
  8555. taicpu(hp1).changeopsize(S_W);
  8556. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  8557. end;
  8558. else
  8559. Internalerror(2017050704)
  8560. end;
  8561. Result := True;
  8562. end;
  8563. end;
  8564. end;
  8565. end;
  8566. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  8567. var
  8568. hp1, hp2 : tai;
  8569. MaskLength : Cardinal;
  8570. MaskedBits : TCgInt;
  8571. begin
  8572. Result:=false;
  8573. { There are no optimisations for reference targets }
  8574. if (taicpu(p).oper[1]^.typ <> top_reg) then
  8575. Exit;
  8576. while GetNextInstruction(p, hp1) and
  8577. (hp1.typ = ait_instruction) do
  8578. begin
  8579. if (taicpu(p).oper[0]^.typ = top_const) then
  8580. begin
  8581. case taicpu(hp1).opcode of
  8582. A_AND:
  8583. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  8584. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  8585. { the second register must contain the first one, so compare their subreg types }
  8586. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  8587. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  8588. { change
  8589. and const1, reg
  8590. and const2, reg
  8591. to
  8592. and (const1 and const2), reg
  8593. }
  8594. begin
  8595. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  8596. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  8597. RemoveCurrentP(p, hp1);
  8598. Result:=true;
  8599. exit;
  8600. end;
  8601. A_CMP:
  8602. if (PopCnt(DWord(taicpu(p).oper[0]^.val)) = 1) and { Only 1 bit set }
  8603. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.val) and
  8604. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  8605. { Just check that the condition on the next instruction is compatible }
  8606. GetNextInstruction(hp1, hp2) and
  8607. (hp2.typ = ait_instruction) and
  8608. (taicpu(hp2).condition in [C_Z, C_E, C_NZ, C_NE])
  8609. then
  8610. { change
  8611. and 2^n, reg
  8612. cmp 2^n, reg
  8613. j(c) / set(c) / cmov(c) (c is equal or not equal)
  8614. to
  8615. and 2^n, reg
  8616. test reg, reg
  8617. j(~c) / set(~c) / cmov(~c)
  8618. }
  8619. begin
  8620. { Keep TEST instruction in, rather than remove it, because
  8621. it may trigger other optimisations such as MovAndTest2Test }
  8622. taicpu(hp1).loadreg(0, taicpu(hp1).oper[1]^.reg);
  8623. taicpu(hp1).opcode := A_TEST;
  8624. DebugMsg(SPeepholeOptimization + 'AND/CMP/J(c) -> AND/J(~c) with power of 2 constant', p);
  8625. taicpu(hp2).condition := inverse_cond(taicpu(hp2).condition);
  8626. Result := True;
  8627. Exit;
  8628. end;
  8629. A_MOVZX:
  8630. if MatchOpType(taicpu(hp1),top_reg,top_reg) and
  8631. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg) and
  8632. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  8633. (
  8634. (
  8635. (taicpu(p).opsize=S_W) and
  8636. (taicpu(hp1).opsize=S_BW)
  8637. ) or
  8638. (
  8639. (taicpu(p).opsize=S_L) and
  8640. (taicpu(hp1).opsize in [S_WL,S_BL{$ifdef x86_64},S_BQ,S_WQ{$endif x86_64}])
  8641. )
  8642. {$ifdef x86_64}
  8643. or
  8644. (
  8645. (taicpu(p).opsize=S_Q) and
  8646. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_BL,S_WL])
  8647. )
  8648. {$endif x86_64}
  8649. ) then
  8650. begin
  8651. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  8652. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  8653. ) or
  8654. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  8655. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  8656. then
  8657. begin
  8658. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  8659. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  8660. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  8661. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  8662. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  8663. }
  8664. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  8665. RemoveInstruction(hp1);
  8666. { See if there are other optimisations possible }
  8667. Continue;
  8668. end;
  8669. end;
  8670. A_SHL:
  8671. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  8672. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  8673. begin
  8674. {$ifopt R+}
  8675. {$define RANGE_WAS_ON}
  8676. {$R-}
  8677. {$endif}
  8678. { get length of potential and mask }
  8679. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  8680. { really a mask? }
  8681. {$ifdef RANGE_WAS_ON}
  8682. {$R+}
  8683. {$endif}
  8684. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  8685. { unmasked part shifted out? }
  8686. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  8687. begin
  8688. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  8689. RemoveCurrentP(p, hp1);
  8690. Result:=true;
  8691. exit;
  8692. end;
  8693. end;
  8694. A_SHR:
  8695. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  8696. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
  8697. (taicpu(hp1).oper[0]^.val <= 63) then
  8698. begin
  8699. { Does SHR combined with the AND cover all the bits?
  8700. e.g. for "andb $252,%reg; shrb $2,%reg" - the "and" can be removed }
  8701. MaskedBits := taicpu(p).oper[0]^.val or ((TCgInt(1) shl taicpu(hp1).oper[0]^.val) - 1);
  8702. if ((taicpu(p).opsize = S_B) and ((MaskedBits and $FF) = $FF)) or
  8703. ((taicpu(p).opsize = S_W) and ((MaskedBits and $FFFF) = $FFFF)) or
  8704. ((taicpu(p).opsize = S_L) and ((MaskedBits and $FFFFFFFF) = $FFFFFFFF)) then
  8705. begin
  8706. DebugMsg(SPeepholeOptimization + 'AndShrToShr done', p);
  8707. RemoveCurrentP(p, hp1);
  8708. Result := True;
  8709. Exit;
  8710. end;
  8711. end;
  8712. A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}:
  8713. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  8714. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg) then
  8715. begin
  8716. if SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  8717. (
  8718. (
  8719. (taicpu(hp1).opsize in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  8720. ((taicpu(p).oper[0]^.val and $7F) = taicpu(p).oper[0]^.val)
  8721. ) or (
  8722. (taicpu(hp1).opsize in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  8723. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val)
  8724. {$ifdef x86_64}
  8725. ) or (
  8726. (taicpu(hp1).opsize = S_LQ) and
  8727. ((taicpu(p).oper[0]^.val and $7fffffff) = taicpu(p).oper[0]^.val)
  8728. {$endif x86_64}
  8729. )
  8730. ) then
  8731. begin
  8732. if (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg){$ifdef x86_64} or (taicpu(hp1).opsize = S_LQ){$endif x86_64} then
  8733. begin
  8734. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  8735. RemoveInstruction(hp1);
  8736. { See if there are other optimisations possible }
  8737. Continue;
  8738. end;
  8739. { The super-registers are the same though.
  8740. Note that this change by itself doesn't improve
  8741. code speed, but it opens up other optimisations. }
  8742. {$ifdef x86_64}
  8743. { Convert 64-bit register to 32-bit }
  8744. case taicpu(hp1).opsize of
  8745. S_BQ:
  8746. begin
  8747. taicpu(hp1).opsize := S_BL;
  8748. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  8749. end;
  8750. S_WQ:
  8751. begin
  8752. taicpu(hp1).opsize := S_WL;
  8753. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  8754. end
  8755. else
  8756. ;
  8757. end;
  8758. {$endif x86_64}
  8759. DebugMsg(SPeepholeOptimization + 'AndMovsxToAndMovzx', hp1);
  8760. taicpu(hp1).opcode := A_MOVZX;
  8761. { See if there are other optimisations possible }
  8762. Continue;
  8763. end;
  8764. end;
  8765. else
  8766. ;
  8767. end;
  8768. end;
  8769. if (taicpu(hp1).is_jmp) and
  8770. (taicpu(hp1).opcode<>A_JMP) and
  8771. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  8772. begin
  8773. { change
  8774. and x, reg
  8775. jxx
  8776. to
  8777. test x, reg
  8778. jxx
  8779. if reg is deallocated before the
  8780. jump, but only if it's a conditional jump (PFV)
  8781. }
  8782. taicpu(p).opcode := A_TEST;
  8783. Exit;
  8784. end;
  8785. Break;
  8786. end;
  8787. { Lone AND tests }
  8788. if (taicpu(p).oper[0]^.typ = top_const) then
  8789. begin
  8790. {
  8791. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  8792. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  8793. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  8794. }
  8795. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  8796. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  8797. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  8798. begin
  8799. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg);
  8800. if taicpu(p).opsize = S_L then
  8801. begin
  8802. Include(OptsToCheck,aoc_MovAnd2Mov_3);
  8803. Result := True;
  8804. end;
  8805. end;
  8806. end;
  8807. { Backward check to determine necessity of and %reg,%reg }
  8808. if (taicpu(p).oper[0]^.typ = top_reg) and
  8809. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  8810. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  8811. GetLastInstruction(p, hp2) and
  8812. RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp2) and
  8813. { Check size of adjacent instruction to determine if the AND is
  8814. effectively a null operation }
  8815. (
  8816. (taicpu(p).opsize = taicpu(hp2).opsize) or
  8817. { Note: Don't include S_Q }
  8818. ((taicpu(p).opsize = S_L) and (taicpu(hp2).opsize in [S_BL, S_WL])) or
  8819. ((taicpu(p).opsize = S_W) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_L])) or
  8820. ((taicpu(p).opsize = S_B) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_W, S_L]))
  8821. ) then
  8822. begin
  8823. DebugMsg(SPeepholeOptimization + 'And2Nop', p);
  8824. { If GetNextInstruction returned False, hp1 will be nil }
  8825. RemoveCurrentP(p, hp1);
  8826. Result := True;
  8827. Exit;
  8828. end;
  8829. end;
  8830. function TX86AsmOptimizer.OptPass2ADD(var p : tai) : boolean;
  8831. var
  8832. hp1: tai; NewRef: TReference;
  8833. { This entire nested function is used in an if-statement below, but we
  8834. want to avoid all the used reg transfers and GetNextInstruction calls
  8835. until we really have to check }
  8836. function MemRegisterNotUsedLater: Boolean; inline;
  8837. var
  8838. hp2: tai;
  8839. begin
  8840. TransferUsedRegs(TmpUsedRegs);
  8841. hp2 := p;
  8842. repeat
  8843. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  8844. until not GetNextInstruction(hp2, hp2) or (hp2 = hp1);
  8845. Result := not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs);
  8846. end;
  8847. begin
  8848. Result := False;
  8849. if not GetNextInstruction(p, hp1) or (hp1.typ <> ait_instruction) then
  8850. Exit;
  8851. if (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif}]) then
  8852. begin
  8853. { Change:
  8854. add %reg2,%reg1
  8855. mov/s/z #(%reg1),%reg1 (%reg1 superregisters must be the same)
  8856. To:
  8857. mov/s/z #(%reg1,%reg2),%reg1
  8858. }
  8859. if MatchOpType(taicpu(p), top_reg, top_reg) and
  8860. MatchInstruction(hp1, [A_MOV, A_MOVZX, A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif}], []) and
  8861. MatchOpType(taicpu(hp1), top_ref, top_reg) and
  8862. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1) and
  8863. (
  8864. (
  8865. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  8866. (taicpu(hp1).oper[0]^.ref^.index = NR_NO) and
  8867. { r/esp cannot be an index }
  8868. (taicpu(p).oper[0]^.reg<>NR_STACK_POINTER_REG)
  8869. ) or (
  8870. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  8871. (taicpu(hp1).oper[0]^.ref^.base = NR_NO)
  8872. )
  8873. ) and (
  8874. Reg1WriteOverwritesReg2Entirely(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) or
  8875. (
  8876. { If the super registers ARE equal, then this MOV/S/Z does a partial write }
  8877. not SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  8878. MemRegisterNotUsedLater
  8879. )
  8880. ) then
  8881. begin
  8882. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[1]^.reg;
  8883. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  8884. DebugMsg(SPeepholeOptimization + 'AddMov2Mov done', p);
  8885. RemoveCurrentp(p, hp1);
  8886. Result := True;
  8887. Exit;
  8888. end;
  8889. { Change:
  8890. addl/q $x,%reg1
  8891. movl/q %reg1,%reg2
  8892. To:
  8893. leal/q $x(%reg1),%reg2
  8894. addl/q $x,%reg1 (can be removed if %reg1 or the flags are not used afterwards)
  8895. Breaks the dependency chain.
  8896. }
  8897. if MatchOpType(taicpu(p),top_const,top_reg) and
  8898. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  8899. (taicpu(hp1).oper[1]^.typ = top_reg) and
  8900. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) and
  8901. (
  8902. { Don't do AddMov2LeaAdd under -Os, but do allow AddMov2Lea }
  8903. not (cs_opt_size in current_settings.optimizerswitches) or
  8904. (
  8905. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) and
  8906. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)
  8907. )
  8908. ) then
  8909. begin
  8910. { Change the MOV instruction to a LEA instruction, and update the
  8911. first operand }
  8912. reference_reset(NewRef, 1, []);
  8913. NewRef.base := taicpu(p).oper[1]^.reg;
  8914. NewRef.scalefactor := 1;
  8915. NewRef.offset := taicpu(p).oper[0]^.val;
  8916. taicpu(hp1).opcode := A_LEA;
  8917. taicpu(hp1).loadref(0, NewRef);
  8918. TransferUsedRegs(TmpUsedRegs);
  8919. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  8920. if RegUsedAfterInstruction(NewRef.base, hp1, TmpUsedRegs) or
  8921. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs) then
  8922. begin
  8923. { Move what is now the LEA instruction to before the SUB instruction }
  8924. Asml.Remove(hp1);
  8925. Asml.InsertBefore(hp1, p);
  8926. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  8927. DebugMsg(SPeepholeOptimization + 'AddMov2LeaAdd', p);
  8928. p := hp1;
  8929. end
  8930. else
  8931. begin
  8932. { Since %reg1 or the flags aren't used afterwards, we can delete p completely }
  8933. RemoveCurrentP(p, hp1);
  8934. DebugMsg(SPeepholeOptimization + 'AddMov2Lea', p);
  8935. end;
  8936. Result := True;
  8937. end;
  8938. end;
  8939. end;
  8940. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  8941. begin
  8942. Result:=false;
  8943. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  8944. begin
  8945. if MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  8946. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  8947. begin
  8948. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  8949. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  8950. taicpu(p).opcode:=A_ADD;
  8951. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  8952. result:=true;
  8953. end
  8954. else if MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  8955. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  8956. begin
  8957. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  8958. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  8959. taicpu(p).opcode:=A_ADD;
  8960. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  8961. result:=true;
  8962. end;
  8963. end;
  8964. end;
  8965. function TX86AsmOptimizer.OptPass2SUB(var p: tai): Boolean;
  8966. var
  8967. hp1: tai; NewRef: TReference;
  8968. begin
  8969. { Change:
  8970. subl/q $x,%reg1
  8971. movl/q %reg1,%reg2
  8972. To:
  8973. leal/q $-x(%reg1),%reg2
  8974. subl/q $x,%reg1 (can be removed if %reg1 or the flags are not used afterwards)
  8975. Breaks the dependency chain and potentially permits the removal of
  8976. a CMP instruction if one follows.
  8977. }
  8978. Result := False;
  8979. if (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  8980. MatchOpType(taicpu(p),top_const,top_reg) and
  8981. GetNextInstruction(p, hp1) and
  8982. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  8983. (taicpu(hp1).oper[1]^.typ = top_reg) and
  8984. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) and
  8985. (
  8986. { Don't do SubMov2LeaSub under -Os, but do allow SubMov2Lea }
  8987. not (cs_opt_size in current_settings.optimizerswitches) or
  8988. (
  8989. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) and
  8990. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)
  8991. )
  8992. ) then
  8993. begin
  8994. { Change the MOV instruction to a LEA instruction, and update the
  8995. first operand }
  8996. reference_reset(NewRef, 1, []);
  8997. NewRef.base := taicpu(p).oper[1]^.reg;
  8998. NewRef.scalefactor := 1;
  8999. NewRef.offset := -taicpu(p).oper[0]^.val;
  9000. taicpu(hp1).opcode := A_LEA;
  9001. taicpu(hp1).loadref(0, NewRef);
  9002. TransferUsedRegs(TmpUsedRegs);
  9003. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  9004. if RegUsedAfterInstruction(NewRef.base, hp1, TmpUsedRegs) or
  9005. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs) then
  9006. begin
  9007. { Move what is now the LEA instruction to before the SUB instruction }
  9008. Asml.Remove(hp1);
  9009. Asml.InsertBefore(hp1, p);
  9010. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  9011. DebugMsg(SPeepholeOptimization + 'SubMov2LeaSub', p);
  9012. p := hp1;
  9013. end
  9014. else
  9015. begin
  9016. { Since %reg1 or the flags aren't used afterwards, we can delete p completely }
  9017. RemoveCurrentP(p, hp1);
  9018. DebugMsg(SPeepholeOptimization + 'SubMov2Lea', p);
  9019. end;
  9020. Result := True;
  9021. end;
  9022. end;
  9023. function TX86AsmOptimizer.SkipSimpleInstructions(var hp1 : tai) : Boolean;
  9024. begin
  9025. { we can skip all instructions not messing with the stack pointer }
  9026. while assigned(hp1) and {MatchInstruction(hp1,[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  9027. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  9028. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  9029. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  9030. ({(taicpu(hp1).ops=0) or }
  9031. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  9032. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  9033. ) and }
  9034. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  9035. )
  9036. ) do
  9037. GetNextInstruction(hp1,hp1);
  9038. Result:=assigned(hp1);
  9039. end;
  9040. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  9041. var
  9042. hp1, hp2, hp3, hp4, hp5: tai;
  9043. begin
  9044. Result:=false;
  9045. hp5:=nil;
  9046. { replace
  9047. leal(q) x(<stackpointer>),<stackpointer>
  9048. call procname
  9049. leal(q) -x(<stackpointer>),<stackpointer>
  9050. ret
  9051. by
  9052. jmp procname
  9053. but do it only on level 4 because it destroys stack back traces
  9054. }
  9055. if (cs_opt_level4 in current_settings.optimizerswitches) and
  9056. MatchOpType(taicpu(p),top_ref,top_reg) and
  9057. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  9058. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  9059. { the -8 or -24 are not required, but bail out early if possible,
  9060. higher values are unlikely }
  9061. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  9062. (taicpu(p).oper[0]^.ref^.offset=-24)) and
  9063. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  9064. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  9065. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  9066. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  9067. GetNextInstruction(p, hp1) and
  9068. { Take a copy of hp1 }
  9069. SetAndTest(hp1, hp4) and
  9070. { trick to skip label }
  9071. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  9072. SkipSimpleInstructions(hp1) and
  9073. MatchInstruction(hp1,A_CALL,[S_NO]) and
  9074. GetNextInstruction(hp1, hp2) and
  9075. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  9076. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  9077. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  9078. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  9079. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  9080. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  9081. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  9082. (taicpu(hp2).oper[0]^.ref^.segment=NR_NO) and
  9083. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  9084. GetNextInstruction(hp2, hp3) and
  9085. { trick to skip label }
  9086. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  9087. (MatchInstruction(hp3,A_RET,[S_NO]) or
  9088. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  9089. SetAndTest(hp3,hp5) and
  9090. GetNextInstruction(hp3,hp3) and
  9091. MatchInstruction(hp3,A_RET,[S_NO])
  9092. )
  9093. ) and
  9094. (taicpu(hp3).ops=0) then
  9095. begin
  9096. taicpu(hp1).opcode := A_JMP;
  9097. taicpu(hp1).is_jmp := true;
  9098. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  9099. RemoveCurrentP(p, hp4);
  9100. RemoveInstruction(hp2);
  9101. RemoveInstruction(hp3);
  9102. if Assigned(hp5) then
  9103. begin
  9104. AsmL.Remove(hp5);
  9105. ASmL.InsertBefore(hp5,hp1)
  9106. end;
  9107. Result:=true;
  9108. end;
  9109. end;
  9110. function TX86AsmOptimizer.PostPeepholeOptPush(var p : tai) : Boolean;
  9111. {$ifdef x86_64}
  9112. var
  9113. hp1, hp2, hp3, hp4, hp5: tai;
  9114. {$endif x86_64}
  9115. begin
  9116. Result:=false;
  9117. {$ifdef x86_64}
  9118. hp5:=nil;
  9119. { replace
  9120. push %rax
  9121. call procname
  9122. pop %rcx
  9123. ret
  9124. by
  9125. jmp procname
  9126. but do it only on level 4 because it destroys stack back traces
  9127. It depends on the fact, that the sequence push rax/pop rcx is used for stack alignment as rcx is volatile
  9128. for all supported calling conventions
  9129. }
  9130. if (cs_opt_level4 in current_settings.optimizerswitches) and
  9131. MatchOpType(taicpu(p),top_reg) and
  9132. (taicpu(p).oper[0]^.reg=NR_RAX) and
  9133. GetNextInstruction(p, hp1) and
  9134. { Take a copy of hp1 }
  9135. SetAndTest(hp1, hp4) and
  9136. { trick to skip label }
  9137. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  9138. SkipSimpleInstructions(hp1) and
  9139. MatchInstruction(hp1,A_CALL,[S_NO]) and
  9140. GetNextInstruction(hp1, hp2) and
  9141. MatchInstruction(hp2,A_POP,[taicpu(p).opsize]) and
  9142. MatchOpType(taicpu(hp2),top_reg) and
  9143. (taicpu(hp2).oper[0]^.reg=NR_RCX) and
  9144. GetNextInstruction(hp2, hp3) and
  9145. { trick to skip label }
  9146. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  9147. (MatchInstruction(hp3,A_RET,[S_NO]) or
  9148. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  9149. SetAndTest(hp3,hp5) and
  9150. GetNextInstruction(hp3,hp3) and
  9151. MatchInstruction(hp3,A_RET,[S_NO])
  9152. )
  9153. ) and
  9154. (taicpu(hp3).ops=0) then
  9155. begin
  9156. taicpu(hp1).opcode := A_JMP;
  9157. taicpu(hp1).is_jmp := true;
  9158. DebugMsg(SPeepholeOptimization + 'PushCallPushRet2Jmp done',p);
  9159. RemoveCurrentP(p, hp4);
  9160. RemoveInstruction(hp2);
  9161. RemoveInstruction(hp3);
  9162. if Assigned(hp5) then
  9163. begin
  9164. AsmL.Remove(hp5);
  9165. ASmL.InsertBefore(hp5,hp1)
  9166. end;
  9167. Result:=true;
  9168. end;
  9169. {$endif x86_64}
  9170. end;
  9171. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  9172. var
  9173. Value, RegName: string;
  9174. begin
  9175. Result:=false;
  9176. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  9177. begin
  9178. case taicpu(p).oper[0]^.val of
  9179. 0:
  9180. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  9181. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  9182. begin
  9183. { change "mov $0,%reg" into "xor %reg,%reg" }
  9184. taicpu(p).opcode := A_XOR;
  9185. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  9186. Result := True;
  9187. {$ifdef x86_64}
  9188. end
  9189. else if (taicpu(p).opsize = S_Q) then
  9190. begin
  9191. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  9192. { The actual optimization }
  9193. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  9194. taicpu(p).changeopsize(S_L);
  9195. DebugMsg(SPeepholeOptimization + 'movq $0,' + RegName + ' -> movl $0,' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  9196. Result := True;
  9197. end;
  9198. $1..$FFFFFFFF:
  9199. begin
  9200. { Code size reduction by J. Gareth "Kit" Moreton }
  9201. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  9202. case taicpu(p).opsize of
  9203. S_Q:
  9204. begin
  9205. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  9206. Value := debug_tostr(taicpu(p).oper[0]^.val);
  9207. { The actual optimization }
  9208. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  9209. taicpu(p).changeopsize(S_L);
  9210. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  9211. Result := True;
  9212. end;
  9213. else
  9214. { Do nothing };
  9215. end;
  9216. {$endif x86_64}
  9217. end;
  9218. -1:
  9219. { Don't make this optimisation if the CPU flags are required, since OR scrambles them }
  9220. if (cs_opt_size in current_settings.optimizerswitches) and
  9221. (taicpu(p).opsize <> S_B) and
  9222. not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  9223. begin
  9224. { change "mov $-1,%reg" into "or $-1,%reg" }
  9225. { NOTES:
  9226. - No size saving is made when changing a Word-sized assignment unless the register is AX (smaller encoding)
  9227. - This operation creates a false dependency on the register, so only do it when optimising for size
  9228. - 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
  9229. }
  9230. taicpu(p).opcode := A_OR;
  9231. Result := True;
  9232. end;
  9233. else
  9234. { Do nothing };
  9235. end;
  9236. end;
  9237. end;
  9238. function TX86AsmOptimizer.PostPeepholeOptAnd(var p : tai) : boolean;
  9239. var
  9240. hp1: tai;
  9241. begin
  9242. { Detect:
  9243. andw x, %ax (0 <= x < $8000)
  9244. ...
  9245. movzwl %ax,%eax
  9246. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  9247. }
  9248. Result := False; if MatchOpType(taicpu(p), top_const, top_reg) and
  9249. (taicpu(p).oper[1]^.reg = NR_AX) and { This is also enough to determine that opsize = S_W }
  9250. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val) and
  9251. GetNextInstructionUsingReg(p, hp1, NR_EAX) and
  9252. MatchInstruction(hp1, A_MOVZX, [S_WL]) and
  9253. MatchOperand(taicpu(hp1).oper[0]^, NR_AX) and
  9254. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) then
  9255. begin
  9256. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via AndMovz2AndCwtl)', hp1);
  9257. taicpu(hp1).opcode := A_CWDE;
  9258. taicpu(hp1).clearop(0);
  9259. taicpu(hp1).clearop(1);
  9260. taicpu(hp1).ops := 0;
  9261. { A change was made, but not with p, so move forward 1 }
  9262. p := tai(p.Next);
  9263. Result := True;
  9264. end;
  9265. end;
  9266. function TX86AsmOptimizer.PostPeepholeOptMOVSX(var p : tai) : boolean;
  9267. begin
  9268. Result := False;
  9269. if not MatchOpType(taicpu(p), top_reg, top_reg) then
  9270. Exit;
  9271. { Convert:
  9272. movswl %ax,%eax -> cwtl
  9273. movslq %eax,%rax -> cdqe
  9274. NOTE: Don't convert movswl %al,%ax to cbw, because cbw and cwde
  9275. refer to the same opcode and depends only on the assembler's
  9276. current operand-size attribute. [Kit]
  9277. }
  9278. with taicpu(p) do
  9279. case opsize of
  9280. S_WL:
  9281. if (oper[0]^.reg = NR_AX) and (oper[1]^.reg = NR_EAX) then
  9282. begin
  9283. DebugMsg(SPeepholeOptimization + 'Converted movswl %ax,%eax to cwtl', p);
  9284. opcode := A_CWDE;
  9285. clearop(0);
  9286. clearop(1);
  9287. ops := 0;
  9288. Result := True;
  9289. end;
  9290. {$ifdef x86_64}
  9291. S_LQ:
  9292. if (oper[0]^.reg = NR_EAX) and (oper[1]^.reg = NR_RAX) then
  9293. begin
  9294. DebugMsg(SPeepholeOptimization + 'Converted movslq %eax,%rax to cltq', p);
  9295. opcode := A_CDQE;
  9296. clearop(0);
  9297. clearop(1);
  9298. ops := 0;
  9299. Result := True;
  9300. end;
  9301. {$endif x86_64}
  9302. else
  9303. ;
  9304. end;
  9305. end;
  9306. function TX86AsmOptimizer.PostPeepholeOptShr(var p : tai) : boolean;
  9307. var
  9308. hp1: tai;
  9309. begin
  9310. { Detect:
  9311. shr x, %ax (x > 0)
  9312. ...
  9313. movzwl %ax,%eax
  9314. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  9315. }
  9316. Result := False;
  9317. if MatchOpType(taicpu(p), top_const, top_reg) and
  9318. (taicpu(p).oper[1]^.reg = NR_AX) and { This is also enough to determine that opsize = S_W }
  9319. (taicpu(p).oper[0]^.val > 0) and
  9320. GetNextInstructionUsingReg(p, hp1, NR_EAX) and
  9321. MatchInstruction(hp1, A_MOVZX, [S_WL]) and
  9322. MatchOperand(taicpu(hp1).oper[0]^, NR_AX) and
  9323. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) then
  9324. begin
  9325. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via ShrMovz2ShrCwtl)', hp1);
  9326. taicpu(hp1).opcode := A_CWDE;
  9327. taicpu(hp1).clearop(0);
  9328. taicpu(hp1).clearop(1);
  9329. taicpu(hp1).ops := 0;
  9330. { A change was made, but not with p, so move forward 1 }
  9331. p := tai(p.Next);
  9332. Result := True;
  9333. end;
  9334. end;
  9335. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  9336. begin
  9337. Result:=false;
  9338. { change "cmp $0, %reg" to "test %reg, %reg" }
  9339. if MatchOpType(taicpu(p),top_const,top_reg) and
  9340. (taicpu(p).oper[0]^.val = 0) then
  9341. begin
  9342. taicpu(p).opcode := A_TEST;
  9343. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  9344. Result:=true;
  9345. end;
  9346. end;
  9347. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  9348. var
  9349. IsTestConstX : Boolean;
  9350. hp1,hp2 : tai;
  9351. begin
  9352. Result:=false;
  9353. { removes the line marked with (x) from the sequence
  9354. and/or/xor/add/sub/... $x, %y
  9355. test/or %y, %y | test $-1, %y (x)
  9356. j(n)z _Label
  9357. as the first instruction already adjusts the ZF
  9358. %y operand may also be a reference }
  9359. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  9360. MatchOperand(taicpu(p).oper[0]^,-1);
  9361. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  9362. GetLastInstruction(p, hp1) and
  9363. (tai(hp1).typ = ait_instruction) and
  9364. GetNextInstruction(p,hp2) and
  9365. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  9366. case taicpu(hp1).opcode Of
  9367. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  9368. begin
  9369. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  9370. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  9371. { and in case of carry for A(E)/B(E)/C/NC }
  9372. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  9373. ((taicpu(hp1).opcode <> A_ADD) and
  9374. (taicpu(hp1).opcode <> A_SUB))) then
  9375. begin
  9376. RemoveCurrentP(p, hp2);
  9377. Result:=true;
  9378. Exit;
  9379. end;
  9380. end;
  9381. A_SHL, A_SAL, A_SHR, A_SAR:
  9382. begin
  9383. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  9384. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  9385. { therefore, it's only safe to do this optimization for }
  9386. { shifts by a (nonzero) constant }
  9387. (taicpu(hp1).oper[0]^.typ = top_const) and
  9388. (taicpu(hp1).oper[0]^.val <> 0) and
  9389. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  9390. { and in case of carry for A(E)/B(E)/C/NC }
  9391. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  9392. begin
  9393. RemoveCurrentP(p, hp2);
  9394. Result:=true;
  9395. Exit;
  9396. end;
  9397. end;
  9398. A_DEC, A_INC, A_NEG:
  9399. begin
  9400. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  9401. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  9402. { and in case of carry for A(E)/B(E)/C/NC }
  9403. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  9404. begin
  9405. RemoveCurrentP(p, hp2);
  9406. Result:=true;
  9407. Exit;
  9408. end;
  9409. end
  9410. else
  9411. ;
  9412. end; { case }
  9413. { change "test $-1,%reg" into "test %reg,%reg" }
  9414. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  9415. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  9416. { Change "or %reg,%reg" to "test %reg,%reg" as OR generates a false dependency }
  9417. if MatchInstruction(p, A_OR, []) and
  9418. { Can only match if they're both registers }
  9419. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) then
  9420. begin
  9421. DebugMsg(SPeepholeOptimization + 'or %reg,%reg -> test %reg,%reg to remove false dependency (Or2Test)', p);
  9422. taicpu(p).opcode := A_TEST;
  9423. { No need to set Result to True, as we've done all the optimisations we can }
  9424. end;
  9425. end;
  9426. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  9427. var
  9428. hp1,hp3 : tai;
  9429. {$ifndef x86_64}
  9430. hp2 : taicpu;
  9431. {$endif x86_64}
  9432. begin
  9433. Result:=false;
  9434. hp3:=nil;
  9435. {$ifndef x86_64}
  9436. { don't do this on modern CPUs, this really hurts them due to
  9437. broken call/ret pairing }
  9438. if (current_settings.optimizecputype < cpu_Pentium2) and
  9439. not(cs_create_pic in current_settings.moduleswitches) and
  9440. GetNextInstruction(p, hp1) and
  9441. MatchInstruction(hp1,A_JMP,[S_NO]) and
  9442. MatchOpType(taicpu(hp1),top_ref) and
  9443. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  9444. begin
  9445. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  9446. InsertLLItem(p.previous, p, hp2);
  9447. taicpu(p).opcode := A_JMP;
  9448. taicpu(p).is_jmp := true;
  9449. RemoveInstruction(hp1);
  9450. Result:=true;
  9451. end
  9452. else
  9453. {$endif x86_64}
  9454. { replace
  9455. call procname
  9456. ret
  9457. by
  9458. jmp procname
  9459. but do it only on level 4 because it destroys stack back traces
  9460. else if the subroutine is marked as no return, remove the ret
  9461. }
  9462. if ((cs_opt_level4 in current_settings.optimizerswitches) or
  9463. (po_noreturn in current_procinfo.procdef.procoptions)) and
  9464. GetNextInstruction(p, hp1) and
  9465. (MatchInstruction(hp1,A_RET,[S_NO]) or
  9466. (MatchInstruction(hp1,A_VZEROUPPER,[S_NO]) and
  9467. SetAndTest(hp1,hp3) and
  9468. GetNextInstruction(hp1,hp1) and
  9469. MatchInstruction(hp1,A_RET,[S_NO])
  9470. )
  9471. ) and
  9472. (taicpu(hp1).ops=0) then
  9473. begin
  9474. if (cs_opt_level4 in current_settings.optimizerswitches) and
  9475. { we might destroy stack alignment here if we do not do a call }
  9476. (target_info.stackalign<=sizeof(SizeUInt)) then
  9477. begin
  9478. taicpu(p).opcode := A_JMP;
  9479. taicpu(p).is_jmp := true;
  9480. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  9481. end
  9482. else
  9483. DebugMsg(SPeepholeOptimization + 'CallRet2Call done',p);
  9484. RemoveInstruction(hp1);
  9485. if Assigned(hp3) then
  9486. begin
  9487. AsmL.Remove(hp3);
  9488. AsmL.InsertBefore(hp3,p)
  9489. end;
  9490. Result:=true;
  9491. end;
  9492. end;
  9493. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  9494. function ConstInRange(const Val: TCGInt; const OpSize: TOpSize): Boolean;
  9495. begin
  9496. case OpSize of
  9497. S_B, S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  9498. Result := (Val <= $FF) and (Val >= -128);
  9499. S_W, S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  9500. Result := (Val <= $FFFF) and (Val >= -32768);
  9501. S_L{$ifdef x86_64}, S_LQ{$endif x86_64}:
  9502. Result := (Val <= $FFFFFFFF) and (Val >= -2147483648);
  9503. else
  9504. Result := True;
  9505. end;
  9506. end;
  9507. var
  9508. hp1, hp2 : tai;
  9509. SizeChange: Boolean;
  9510. PreMessage: string;
  9511. begin
  9512. Result := False;
  9513. if (taicpu(p).oper[0]^.typ = top_reg) and
  9514. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  9515. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) then
  9516. begin
  9517. { Change (using movzbl %al,%eax as an example):
  9518. movzbl %al, %eax movzbl %al, %eax
  9519. cmpl x, %eax testl %eax,%eax
  9520. To:
  9521. cmpb x, %al testb %al, %al (Move one back to avoid a false dependency)
  9522. movzbl %al, %eax movzbl %al, %eax
  9523. Smaller instruction and minimises pipeline stall as the CPU
  9524. doesn't have to wait for the register to get zero-extended. [Kit]
  9525. Also allow if the smaller of the two registers is being checked,
  9526. as this still removes the false dependency.
  9527. }
  9528. if
  9529. (
  9530. (
  9531. (taicpu(hp1).opcode = A_CMP) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  9532. ConstInRange(taicpu(hp1).oper[0]^.val, taicpu(p).opsize)
  9533. ) or (
  9534. { If MatchOperand returns True, they must both be registers }
  9535. (taicpu(hp1).opcode = A_TEST) and MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)
  9536. )
  9537. ) and
  9538. (reg2opsize(taicpu(hp1).oper[1]^.reg) <= reg2opsize(taicpu(p).oper[1]^.reg)) and
  9539. SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) then
  9540. begin
  9541. PreMessage := debug_op2str(taicpu(hp1).opcode) + debug_opsize2str(taicpu(hp1).opsize) + ' ' + debug_operstr(taicpu(hp1).oper[0]^) + ',' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' -> ' + debug_op2str(taicpu(hp1).opcode);
  9542. asml.Remove(hp1);
  9543. asml.InsertBefore(hp1, p);
  9544. { Swap instructions in the case of cmp 0,%reg or test %reg,%reg }
  9545. if (taicpu(hp1).opcode = A_TEST) or (taicpu(hp1).oper[0]^.val = 0) then
  9546. begin
  9547. taicpu(hp1).opcode := A_TEST;
  9548. taicpu(hp1).loadreg(0, taicpu(p).oper[0]^.reg);
  9549. end;
  9550. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  9551. case taicpu(p).opsize of
  9552. S_BW, S_BL:
  9553. begin
  9554. SizeChange := taicpu(hp1).opsize <> S_B;
  9555. taicpu(hp1).changeopsize(S_B);
  9556. end;
  9557. S_WL:
  9558. begin
  9559. SizeChange := taicpu(hp1).opsize <> S_W;
  9560. taicpu(hp1).changeopsize(S_W);
  9561. end
  9562. else
  9563. InternalError(2020112701);
  9564. end;
  9565. UpdateUsedRegs(tai(p.Next));
  9566. { Check if the register is used aferwards - if not, we can
  9567. remove the movzx instruction completely }
  9568. if not RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, p, UsedRegs) then
  9569. begin
  9570. { Hp1 is a better position than p for debugging purposes }
  9571. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4a', hp1);
  9572. RemoveCurrentp(p, hp1);
  9573. Result := True;
  9574. end;
  9575. if SizeChange then
  9576. DebugMsg(SPeepholeOptimization + PreMessage +
  9577. debug_opsize2str(taicpu(hp1).opsize) + ' ' + debug_operstr(taicpu(hp1).oper[0]^) + ',' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' (smaller and minimises pipeline stall - MovzxCmp2CmpMovzx)', hp1)
  9578. else
  9579. DebugMsg(SPeepholeOptimization + 'MovzxCmp2CmpMovzx', hp1);
  9580. Exit;
  9581. end;
  9582. { Change (using movzwl %ax,%eax as an example):
  9583. movzwl %ax, %eax
  9584. movb %al, (dest) (Register is smaller than read register in movz)
  9585. To:
  9586. movb %al, (dest) (Move one back to avoid a false dependency)
  9587. movzwl %ax, %eax
  9588. }
  9589. if (taicpu(hp1).opcode = A_MOV) and
  9590. (taicpu(hp1).oper[0]^.typ = top_reg) and
  9591. not RegInOp(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^) and
  9592. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(p).oper[0]^.reg) and
  9593. (reg2opsize(taicpu(hp1).oper[0]^.reg) <= reg2opsize(taicpu(p).oper[0]^.reg)) then
  9594. begin
  9595. DebugMsg(SPeepholeOptimization + 'MovzxMov2MovMovzx', hp1);
  9596. hp2 := tai(hp1.Previous); { Effectively the old position of hp1 }
  9597. asml.Remove(hp1);
  9598. asml.InsertBefore(hp1, p);
  9599. if taicpu(hp1).oper[1]^.typ = top_reg then
  9600. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, hp2, UsedRegs);
  9601. { Check if the register is used aferwards - if not, we can
  9602. remove the movzx instruction completely }
  9603. if not RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, p, UsedRegs) then
  9604. begin
  9605. { Hp1 is a better position than p for debugging purposes }
  9606. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4b', hp1);
  9607. RemoveCurrentp(p, hp1);
  9608. Result := True;
  9609. end;
  9610. Exit;
  9611. end;
  9612. end;
  9613. end;
  9614. {$ifdef x86_64}
  9615. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  9616. var
  9617. PreMessage, RegName: string;
  9618. begin
  9619. { Code size reduction by J. Gareth "Kit" Moreton }
  9620. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  9621. as this removes the REX prefix }
  9622. Result := False;
  9623. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  9624. Exit;
  9625. if taicpu(p).oper[0]^.typ <> top_reg then
  9626. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  9627. InternalError(2018011500);
  9628. case taicpu(p).opsize of
  9629. S_Q:
  9630. begin
  9631. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  9632. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  9633. { The actual optimization }
  9634. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  9635. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  9636. taicpu(p).changeopsize(S_L);
  9637. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  9638. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (32-bit register recommended when zeroing 64-bit counterpart)', p);
  9639. end;
  9640. else
  9641. ;
  9642. end;
  9643. end;
  9644. {$endif}
  9645. function TX86AsmOptimizer.PostPeepholeOptVPXOR(var p : tai) : Boolean;
  9646. var
  9647. XReg: TRegister;
  9648. begin
  9649. Result := False;
  9650. { Turn "vpxor %ymmreg2,%ymmreg2,%ymmreg1" to "vpxor %xmmreg2,%xmmreg2,%xmmreg1"
  9651. Smaller encoding and slightly faster on some platforms (also works for
  9652. ZMM-sized registers) }
  9653. if (taicpu(p).opsize in [S_YMM, S_ZMM]) and
  9654. MatchOpType(taicpu(p), top_reg, top_reg, top_reg) then
  9655. begin
  9656. XReg := taicpu(p).oper[0]^.reg;
  9657. if (taicpu(p).oper[1]^.reg = XReg) then
  9658. begin
  9659. taicpu(p).changeopsize(S_XMM);
  9660. setsubreg(taicpu(p).oper[2]^.reg, R_SUBMMX);
  9661. if (cs_opt_size in current_settings.optimizerswitches) then
  9662. begin
  9663. { Change input registers to %xmm0 to reduce size. Note that
  9664. there's a risk of a false dependency doing this, so only
  9665. optimise for size here }
  9666. XReg := NR_XMM0;
  9667. DebugMsg(SPeepholeOptimization + 'Changed zero-setting vpxor from Y/ZMM to XMM and changed input registers to %xmm0 to reduce size', p);
  9668. end
  9669. else
  9670. begin
  9671. setsubreg(XReg, R_SUBMMX);
  9672. DebugMsg(SPeepholeOptimization + 'Changed zero-setting vpxor from Y/ZMM to XMM to reduce size and increase efficiency', p);
  9673. end;
  9674. taicpu(p).oper[0]^.reg := XReg;
  9675. taicpu(p).oper[1]^.reg := XReg;
  9676. Result := True;
  9677. end;
  9678. end;
  9679. end;
  9680. class procedure TX86AsmOptimizer.OptimizeRefs(var p: taicpu);
  9681. var
  9682. OperIdx: Integer;
  9683. begin
  9684. for OperIdx := 0 to p.ops - 1 do
  9685. if p.oper[OperIdx]^.typ = top_ref then
  9686. optimize_ref(p.oper[OperIdx]^.ref^, False);
  9687. end;
  9688. end.