aoptx86.pas 327 KB

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