aoptx86.pas 268 KB

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