aoptx86.pas 274 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446
  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. mask: tcgint;
  3155. begin
  3156. Result:=false;
  3157. { All these optimisations work on "shl/sal const,%reg" }
  3158. if not MatchOpType(taicpu(p),top_const,top_reg) then
  3159. Exit;
  3160. if (taicpu(p).opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  3161. (taicpu(p).oper[0]^.val <= 3) then
  3162. { Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement }
  3163. begin
  3164. { should we check the next instruction? }
  3165. TmpBool1 := True;
  3166. { have we found an add/sub which could be
  3167. integrated in the lea? }
  3168. TmpBool2 := False;
  3169. reference_reset(tmpref,2,[]);
  3170. TmpRef.index := taicpu(p).oper[1]^.reg;
  3171. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  3172. while TmpBool1 and
  3173. GetNextInstruction(p, hp1) and
  3174. (tai(hp1).typ = ait_instruction) and
  3175. ((((taicpu(hp1).opcode = A_ADD) or
  3176. (taicpu(hp1).opcode = A_SUB)) and
  3177. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  3178. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  3179. (((taicpu(hp1).opcode = A_INC) or
  3180. (taicpu(hp1).opcode = A_DEC)) and
  3181. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  3182. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg)) or
  3183. ((taicpu(hp1).opcode = A_LEA) and
  3184. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  3185. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg))) and
  3186. (not GetNextInstruction(hp1,hp2) or
  3187. not instrReadsFlags(hp2)) Do
  3188. begin
  3189. TmpBool1 := False;
  3190. if taicpu(hp1).opcode=A_LEA then
  3191. begin
  3192. if (TmpRef.base = NR_NO) and
  3193. (taicpu(hp1).oper[0]^.ref^.symbol=nil) and
  3194. (taicpu(hp1).oper[0]^.ref^.relsymbol=nil) and
  3195. (taicpu(hp1).oper[0]^.ref^.segment=NR_NO) and
  3196. ((taicpu(hp1).oper[0]^.ref^.scalefactor=0) or
  3197. (taicpu(hp1).oper[0]^.ref^.scalefactor*tmpref.scalefactor<=8)) then
  3198. begin
  3199. TmpBool1 := True;
  3200. TmpBool2 := True;
  3201. inc(TmpRef.offset, taicpu(hp1).oper[0]^.ref^.offset);
  3202. if taicpu(hp1).oper[0]^.ref^.scalefactor<>0 then
  3203. tmpref.scalefactor:=tmpref.scalefactor*taicpu(hp1).oper[0]^.ref^.scalefactor;
  3204. TmpRef.base := taicpu(hp1).oper[0]^.ref^.base;
  3205. asml.remove(hp1);
  3206. hp1.free;
  3207. end
  3208. end
  3209. else if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  3210. begin
  3211. TmpBool1 := True;
  3212. TmpBool2 := True;
  3213. case taicpu(hp1).opcode of
  3214. A_ADD:
  3215. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  3216. A_SUB:
  3217. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  3218. else
  3219. internalerror(2019050536);
  3220. end;
  3221. asml.remove(hp1);
  3222. hp1.free;
  3223. end
  3224. else
  3225. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  3226. (((taicpu(hp1).opcode = A_ADD) and
  3227. (TmpRef.base = NR_NO)) or
  3228. (taicpu(hp1).opcode = A_INC) or
  3229. (taicpu(hp1).opcode = A_DEC)) then
  3230. begin
  3231. TmpBool1 := True;
  3232. TmpBool2 := True;
  3233. case taicpu(hp1).opcode of
  3234. A_ADD:
  3235. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  3236. A_INC:
  3237. inc(TmpRef.offset);
  3238. A_DEC:
  3239. dec(TmpRef.offset);
  3240. else
  3241. internalerror(2019050535);
  3242. end;
  3243. asml.remove(hp1);
  3244. hp1.free;
  3245. end;
  3246. end;
  3247. if TmpBool2
  3248. {$ifndef x86_64}
  3249. or
  3250. ((current_settings.optimizecputype < cpu_Pentium2) and
  3251. (taicpu(p).oper[0]^.val <= 3) and
  3252. not(cs_opt_size in current_settings.optimizerswitches))
  3253. {$endif x86_64}
  3254. then
  3255. begin
  3256. if not(TmpBool2) and
  3257. (taicpu(p).oper[0]^.val=1) then
  3258. begin
  3259. hp1:=taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  3260. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  3261. end
  3262. else
  3263. hp1:=taicpu.op_ref_reg(A_LEA, taicpu(p).opsize, TmpRef,
  3264. taicpu(p).oper[1]^.reg);
  3265. DebugMsg(SPeepholeOptimization + 'ShlAddLeaSubIncDec2Lea',p);
  3266. InsertLLItem(p.previous, p.next, hp1);
  3267. p.free;
  3268. p := hp1;
  3269. end;
  3270. end
  3271. {$ifndef x86_64}
  3272. else if (current_settings.optimizecputype < cpu_Pentium2) then
  3273. begin
  3274. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  3275. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  3276. (unlike shl, which is only Tairable in the U pipe) }
  3277. if taicpu(p).oper[0]^.val=1 then
  3278. begin
  3279. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  3280. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  3281. InsertLLItem(p.previous, p.next, hp1);
  3282. p.free;
  3283. p := hp1;
  3284. end
  3285. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  3286. "shl $3, %reg" to "lea (,%reg,8), %reg }
  3287. else if (taicpu(p).opsize = S_L) and
  3288. (taicpu(p).oper[0]^.val<= 3) then
  3289. begin
  3290. reference_reset(tmpref,2,[]);
  3291. TmpRef.index := taicpu(p).oper[1]^.reg;
  3292. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  3293. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  3294. InsertLLItem(p.previous, p.next, hp1);
  3295. p.free;
  3296. p := hp1;
  3297. end;
  3298. end
  3299. {$endif x86_64}
  3300. else if
  3301. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  3302. (
  3303. (
  3304. MatchInstruction(hp1, A_AND, [taicpu(p).opsize]) and
  3305. SetAndTest(hp1, hp2)
  3306. {$ifdef x86_64}
  3307. ) or
  3308. (
  3309. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  3310. GetNextInstruction(hp1, hp2) and
  3311. MatchInstruction(hp2, A_AND, [taicpu(p).opsize]) and
  3312. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  3313. (taicpu(hp1).oper[1]^.reg = taicpu(hp2).oper[0]^.reg)
  3314. {$endif x86_64}
  3315. )
  3316. ) and
  3317. (taicpu(p).oper[1]^.reg = taicpu(hp2).oper[1]^.reg) then
  3318. begin
  3319. { Change:
  3320. shl x, %reg1
  3321. mov -(1<<x), %reg2
  3322. and %reg2, %reg1
  3323. Or:
  3324. shl x, %reg1
  3325. and -(1<<x), %reg1
  3326. To just:
  3327. shl x, %reg1
  3328. Since the and operation only zeroes bits that are already zero from the shl operation
  3329. }
  3330. case taicpu(p).oper[0]^.val of
  3331. 8:
  3332. mask:=$FFFFFFFFFFFFFF00;
  3333. 16:
  3334. mask:=$FFFFFFFFFFFF0000;
  3335. 32:
  3336. mask:=$FFFFFFFF00000000;
  3337. 63:
  3338. { Constant pre-calculated to prevent overflow errors with Int64 }
  3339. mask:=$8000000000000000;
  3340. else
  3341. begin
  3342. if taicpu(p).oper[0]^.val >= 64 then
  3343. { Shouldn't happen realistically, since the register
  3344. is guaranteed to be set to zero at this point }
  3345. mask := 0
  3346. else
  3347. mask := -(Int64(1 shl taicpu(p).oper[0]^.val));
  3348. end;
  3349. end;
  3350. if taicpu(hp1).oper[0]^.val = mask then
  3351. begin
  3352. { Everything checks out, perform the optimisation, as long as
  3353. the FLAGS register isn't being used}
  3354. TransferUsedRegs(TmpUsedRegs);
  3355. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3356. {$ifdef x86_64}
  3357. if (hp1 <> hp2) then
  3358. begin
  3359. { "shl/mov/and" version }
  3360. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  3361. { Don't do the optimisation if the FLAGS register is in use }
  3362. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)) then
  3363. begin
  3364. DebugMsg(SPeepholeOptimization + 'ShlMovAnd2Shl', p);
  3365. { Don't remove the 'mov' instruction if its register is used elsewhere }
  3366. if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp2, TmpUsedRegs)) then
  3367. begin
  3368. asml.Remove(hp1);
  3369. hp1.Free;
  3370. Result := True;
  3371. end;
  3372. { Only set Result to True if the 'mov' instruction was removed }
  3373. asml.Remove(hp2);
  3374. hp2.Free;
  3375. end;
  3376. end
  3377. else
  3378. {$endif x86_64}
  3379. begin
  3380. { "shl/and" version }
  3381. { Don't do the optimisation if the FLAGS register is in use }
  3382. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  3383. begin
  3384. DebugMsg(SPeepholeOptimization + 'ShlAnd2Shl', p);
  3385. asml.Remove(hp1);
  3386. hp1.Free;
  3387. Result := True;
  3388. end;
  3389. end;
  3390. Exit;
  3391. end
  3392. else {$ifdef x86_64}if (hp1 = hp2) then{$endif x86_64}
  3393. begin
  3394. { Even if the mask doesn't allow for its removal, we might be
  3395. able to optimise the mask for the "shl/and" version, which
  3396. may permit other peephole optimisations }
  3397. {$ifdef DEBUG_AOPTCPU}
  3398. mask := taicpu(hp1).oper[0]^.val and mask;
  3399. if taicpu(hp1).oper[0]^.val <> mask then
  3400. begin
  3401. DebugMsg(
  3402. SPeepholeOptimization +
  3403. 'Changed mask from $' + debug_tostr(taicpu(hp1).oper[0]^.val) +
  3404. ' to $' + debug_tostr(mask) +
  3405. 'based on previous instruction (ShlAnd2ShlAnd)', hp1);
  3406. taicpu(hp1).oper[0]^.val := mask;
  3407. end;
  3408. {$else DEBUG_AOPTCPU}
  3409. { If debugging is off, just set the operand even if it's the same }
  3410. taicpu(hp1).oper[0]^.val := taicpu(hp1).oper[0]^.val and mask;
  3411. {$endif DEBUG_AOPTCPU}
  3412. end;
  3413. end;
  3414. end;
  3415. function TX86AsmOptimizer.OptPass1SETcc(var p: tai): boolean;
  3416. var
  3417. hp1,hp2,next: tai; SetC, JumpC: TAsmCond; Unconditional: Boolean;
  3418. begin
  3419. Result:=false;
  3420. if MatchOpType(taicpu(p),top_reg) and
  3421. GetNextInstruction(p, hp1) and
  3422. ((MatchInstruction(hp1, A_TEST, [S_B]) and
  3423. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3424. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg)) or
  3425. (MatchInstruction(hp1, A_CMP, [S_B]) and
  3426. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3427. (taicpu(hp1).oper[0]^.val=0))
  3428. ) and
  3429. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  3430. GetNextInstruction(hp1, hp2) and
  3431. MatchInstruction(hp2, A_Jcc, []) then
  3432. { Change from: To:
  3433. set(C) %reg j(~C) label
  3434. test %reg,%reg/cmp $0,%reg
  3435. je label
  3436. set(C) %reg j(C) label
  3437. test %reg,%reg/cmp $0,%reg
  3438. jne label
  3439. }
  3440. begin
  3441. next := tai(p.Next);
  3442. TransferUsedRegs(TmpUsedRegs);
  3443. UpdateUsedRegs(TmpUsedRegs, next);
  3444. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  3445. JumpC := taicpu(hp2).condition;
  3446. Unconditional := False;
  3447. if conditions_equal(JumpC, C_E) then
  3448. SetC := inverse_cond(taicpu(p).condition)
  3449. else if conditions_equal(JumpC, C_NE) then
  3450. SetC := taicpu(p).condition
  3451. else
  3452. { We've got something weird here (and inefficent) }
  3453. begin
  3454. DebugMsg('DEBUG: Inefficient jump - check code generation', p);
  3455. SetC := C_NONE;
  3456. { JAE/JNB will always branch (use 'condition_in', since C_AE <> C_NB normally) }
  3457. if condition_in(C_AE, JumpC) then
  3458. Unconditional := True
  3459. else
  3460. { Not sure what to do with this jump - drop out }
  3461. Exit;
  3462. end;
  3463. asml.Remove(hp1);
  3464. hp1.Free;
  3465. if Unconditional then
  3466. MakeUnconditional(taicpu(hp2))
  3467. else
  3468. begin
  3469. if SetC = C_NONE then
  3470. InternalError(2018061401);
  3471. taicpu(hp2).SetCondition(SetC);
  3472. end;
  3473. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
  3474. begin
  3475. asml.Remove(p);
  3476. UpdateUsedRegs(next);
  3477. p.Free;
  3478. Result := True;
  3479. p := hp2;
  3480. end;
  3481. DebugMsg(SPeepholeOptimization + 'SETcc/TESTCmp/Jcc -> Jcc',p);
  3482. end;
  3483. end;
  3484. function TX86AsmOptimizer.OptPass1FSTP(var p: tai): boolean;
  3485. { returns true if a "continue" should be done after this optimization }
  3486. var
  3487. hp1, hp2: tai;
  3488. begin
  3489. Result := false;
  3490. if MatchOpType(taicpu(p),top_ref) and
  3491. GetNextInstruction(p, hp1) and
  3492. (hp1.typ = ait_instruction) and
  3493. (((taicpu(hp1).opcode = A_FLD) and
  3494. (taicpu(p).opcode = A_FSTP)) or
  3495. ((taicpu(p).opcode = A_FISTP) and
  3496. (taicpu(hp1).opcode = A_FILD))) and
  3497. MatchOpType(taicpu(hp1),top_ref) and
  3498. (taicpu(hp1).opsize = taicpu(p).opsize) and
  3499. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  3500. begin
  3501. { replacing fstp f;fld f by fst f is only valid for extended because of rounding }
  3502. if (taicpu(p).opsize=S_FX) and
  3503. GetNextInstruction(hp1, hp2) and
  3504. (hp2.typ = ait_instruction) and
  3505. IsExitCode(hp2) and
  3506. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  3507. not(assigned(current_procinfo.procdef.funcretsym) and
  3508. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  3509. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  3510. begin
  3511. asml.remove(p);
  3512. asml.remove(hp1);
  3513. p.free;
  3514. hp1.free;
  3515. p := hp2;
  3516. RemoveLastDeallocForFuncRes(p);
  3517. Result := true;
  3518. end
  3519. (* can't be done because the store operation rounds
  3520. else
  3521. { fst can't store an extended value! }
  3522. if (taicpu(p).opsize <> S_FX) and
  3523. (taicpu(p).opsize <> S_IQ) then
  3524. begin
  3525. if (taicpu(p).opcode = A_FSTP) then
  3526. taicpu(p).opcode := A_FST
  3527. else taicpu(p).opcode := A_FIST;
  3528. asml.remove(hp1);
  3529. hp1.free;
  3530. end
  3531. *)
  3532. end;
  3533. end;
  3534. function TX86AsmOptimizer.OptPass1FLD(var p : tai) : boolean;
  3535. var
  3536. hp1, hp2: tai;
  3537. begin
  3538. result:=false;
  3539. if MatchOpType(taicpu(p),top_reg) and
  3540. GetNextInstruction(p, hp1) and
  3541. (hp1.typ = Ait_Instruction) and
  3542. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3543. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  3544. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  3545. { change to
  3546. fld reg fxxx reg,st
  3547. fxxxp st, st1 (hp1)
  3548. Remark: non commutative operations must be reversed!
  3549. }
  3550. begin
  3551. case taicpu(hp1).opcode Of
  3552. A_FMULP,A_FADDP,
  3553. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  3554. begin
  3555. case taicpu(hp1).opcode Of
  3556. A_FADDP: taicpu(hp1).opcode := A_FADD;
  3557. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  3558. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  3559. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  3560. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  3561. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  3562. else
  3563. internalerror(2019050534);
  3564. end;
  3565. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  3566. taicpu(hp1).oper[1]^.reg := NR_ST;
  3567. asml.remove(p);
  3568. p.free;
  3569. p := hp1;
  3570. Result:=true;
  3571. exit;
  3572. end;
  3573. else
  3574. ;
  3575. end;
  3576. end
  3577. else
  3578. if MatchOpType(taicpu(p),top_ref) and
  3579. GetNextInstruction(p, hp2) and
  3580. (hp2.typ = Ait_Instruction) and
  3581. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  3582. (taicpu(p).opsize in [S_FS, S_FL]) and
  3583. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  3584. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  3585. if GetLastInstruction(p, hp1) and
  3586. MatchInstruction(hp1,A_FLD,A_FST,[taicpu(p).opsize]) and
  3587. MatchOpType(taicpu(hp1),top_ref) and
  3588. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  3589. if ((taicpu(hp2).opcode = A_FMULP) or
  3590. (taicpu(hp2).opcode = A_FADDP)) then
  3591. { change to
  3592. fld/fst mem1 (hp1) fld/fst mem1
  3593. fld mem1 (p) fadd/
  3594. faddp/ fmul st, st
  3595. fmulp st, st1 (hp2) }
  3596. begin
  3597. asml.remove(p);
  3598. p.free;
  3599. p := hp1;
  3600. if (taicpu(hp2).opcode = A_FADDP) then
  3601. taicpu(hp2).opcode := A_FADD
  3602. else
  3603. taicpu(hp2).opcode := A_FMUL;
  3604. taicpu(hp2).oper[1]^.reg := NR_ST;
  3605. end
  3606. else
  3607. { change to
  3608. fld/fst mem1 (hp1) fld/fst mem1
  3609. fld mem1 (p) fld st}
  3610. begin
  3611. taicpu(p).changeopsize(S_FL);
  3612. taicpu(p).loadreg(0,NR_ST);
  3613. end
  3614. else
  3615. begin
  3616. case taicpu(hp2).opcode Of
  3617. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  3618. { change to
  3619. fld/fst mem1 (hp1) fld/fst mem1
  3620. fld mem2 (p) fxxx mem2
  3621. fxxxp st, st1 (hp2) }
  3622. begin
  3623. case taicpu(hp2).opcode Of
  3624. A_FADDP: taicpu(p).opcode := A_FADD;
  3625. A_FMULP: taicpu(p).opcode := A_FMUL;
  3626. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  3627. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  3628. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  3629. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  3630. else
  3631. internalerror(2019050533);
  3632. end;
  3633. asml.remove(hp2);
  3634. hp2.free;
  3635. end
  3636. else
  3637. ;
  3638. end
  3639. end
  3640. end;
  3641. function TX86AsmOptimizer.OptPass1Cmp(var p: tai): boolean;
  3642. var
  3643. v: TCGInt;
  3644. hp1, hp2: tai;
  3645. begin
  3646. Result:=false;
  3647. if taicpu(p).oper[0]^.typ = top_const then
  3648. begin
  3649. { Though GetNextInstruction can be factored out, it is an expensive
  3650. call, so delay calling it until we have first checked cheaper
  3651. conditions that are independent of it. }
  3652. if (taicpu(p).oper[0]^.val = 0) and
  3653. (taicpu(p).oper[1]^.typ = top_reg) and
  3654. GetNextInstruction(p, hp1) and
  3655. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) then
  3656. begin
  3657. hp2 := p;
  3658. { When dealing with "cmp $0,%reg", only ZF and SF contain
  3659. anything meaningful once it's converted to "test %reg,%reg";
  3660. additionally, some jumps will always (or never) branch, so
  3661. evaluate every jump immediately following the
  3662. comparison, optimising the conditions if possible.
  3663. Similarly with SETcc... those that are always set to 0 or 1
  3664. are changed to MOV instructions }
  3665. while GetNextInstruction(hp2, hp1) and
  3666. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) do
  3667. begin
  3668. case taicpu(hp1).condition of
  3669. C_B, C_C, C_NAE, C_O:
  3670. { For B/NAE:
  3671. Will never branch since an unsigned integer can never be below zero
  3672. For C/O:
  3673. Result cannot overflow because 0 is being subtracted
  3674. }
  3675. begin
  3676. if taicpu(hp1).opcode = A_Jcc then
  3677. begin
  3678. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (jump removed)', hp1);
  3679. TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
  3680. AsmL.Remove(hp1);
  3681. hp1.Free;
  3682. { Since hp1 was deleted, hp2 must not be updated }
  3683. Continue;
  3684. end
  3685. else
  3686. begin
  3687. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (set -> mov 0)', hp1);
  3688. { Convert "set(c) %reg" instruction to "movb 0,%reg" }
  3689. taicpu(hp1).opcode := A_MOV;
  3690. taicpu(hp1).ops := 2;
  3691. taicpu(hp1).condition := C_None;
  3692. taicpu(hp1).opsize := S_B;
  3693. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  3694. taicpu(hp1).loadconst(0, 0);
  3695. end;
  3696. end;
  3697. C_BE, C_NA:
  3698. begin
  3699. { Will only branch if equal to zero }
  3700. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition BE/NA --> E', hp1);
  3701. taicpu(hp1).condition := C_E;
  3702. end;
  3703. C_A, C_NBE:
  3704. begin
  3705. { Will only branch if not equal to zero }
  3706. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition A/NBE --> NE', hp1);
  3707. taicpu(hp1).condition := C_NE;
  3708. end;
  3709. C_AE, C_NB, C_NC, C_NO:
  3710. begin
  3711. { Will always branch }
  3712. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition AE/NB/NC/NO --> Always', hp1);
  3713. if taicpu(hp1).opcode = A_Jcc then
  3714. begin
  3715. MakeUnconditional(taicpu(hp1));
  3716. { Any jumps/set that follow will now be dead code }
  3717. RemoveDeadCodeAfterJump(taicpu(hp1));
  3718. Break;
  3719. end
  3720. else
  3721. begin
  3722. { Convert "set(c) %reg" instruction to "movb 1,%reg" }
  3723. taicpu(hp1).opcode := A_MOV;
  3724. taicpu(hp1).ops := 2;
  3725. taicpu(hp1).condition := C_None;
  3726. taicpu(hp1).opsize := S_B;
  3727. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  3728. taicpu(hp1).loadconst(0, 1);
  3729. end;
  3730. end;
  3731. C_None:
  3732. InternalError(2020012201);
  3733. C_P, C_PE, C_NP, C_PO:
  3734. { We can't handle parity checks and they should never be generated
  3735. after a general-purpose CMP (it's used in some floating-point
  3736. comparisons that don't use CMP) }
  3737. InternalError(2020012202);
  3738. else
  3739. { Zero/Equality, Sign, their complements and all of the
  3740. signed comparisons do not need to be converted };
  3741. end;
  3742. hp2 := hp1;
  3743. end;
  3744. { Convert the instruction to a TEST }
  3745. taicpu(p).opcode := A_TEST;
  3746. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  3747. Result := True;
  3748. Exit;
  3749. end
  3750. else if (taicpu(p).oper[0]^.val = 1) and
  3751. GetNextInstruction(p, hp1) and
  3752. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  3753. (taicpu(hp1).condition in [C_L, C_NGE]) then
  3754. begin
  3755. { Convert; To:
  3756. cmp $1,r/m cmp $0,r/m
  3757. jl @lbl jle @lbl
  3758. }
  3759. DebugMsg(SPeepholeOptimization + 'Cmp1Jl2Cmp0Jle', p);
  3760. taicpu(p).oper[0]^.val := 0;
  3761. taicpu(hp1).condition := C_LE;
  3762. { If the instruction is now "cmp $0,%reg", convert it to a
  3763. TEST (and effectively do the work of the "cmp $0,%reg" in
  3764. the block above)
  3765. If it's a reference, we can get away with not setting
  3766. Result to True because he haven't evaluated the jump
  3767. in this pass yet.
  3768. }
  3769. if (taicpu(p).oper[1]^.typ = top_reg) then
  3770. begin
  3771. taicpu(p).opcode := A_TEST;
  3772. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  3773. Result := True;
  3774. end;
  3775. Exit;
  3776. end
  3777. else if (taicpu(p).oper[1]^.typ = top_reg) then
  3778. begin
  3779. { cmp register,$8000 neg register
  3780. je target --> jo target
  3781. .... only if register is deallocated before jump.}
  3782. case Taicpu(p).opsize of
  3783. S_B: v:=$80;
  3784. S_W: v:=$8000;
  3785. S_L: v:=qword($80000000);
  3786. { S_Q will never happen: cmp with 64 bit constants is not possible }
  3787. S_Q:
  3788. Exit;
  3789. else
  3790. internalerror(2013112905);
  3791. end;
  3792. if (taicpu(p).oper[0]^.val=v) and
  3793. GetNextInstruction(p, hp1) and
  3794. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  3795. (Taicpu(hp1).condition in [C_E,C_NE]) then
  3796. begin
  3797. TransferUsedRegs(TmpUsedRegs);
  3798. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  3799. if not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, TmpUsedRegs)) then
  3800. begin
  3801. DebugMsg(SPeepholeOptimization + 'CmpJe2NegJo done',p);
  3802. Taicpu(p).opcode:=A_NEG;
  3803. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  3804. Taicpu(p).clearop(1);
  3805. Taicpu(p).ops:=1;
  3806. if Taicpu(hp1).condition=C_E then
  3807. Taicpu(hp1).condition:=C_O
  3808. else
  3809. Taicpu(hp1).condition:=C_NO;
  3810. Result:=true;
  3811. exit;
  3812. end;
  3813. end;
  3814. end;
  3815. end;
  3816. end;
  3817. function TX86AsmOptimizer.OptPass1PXor(var p: tai): boolean;
  3818. var
  3819. hp1: tai;
  3820. begin
  3821. {
  3822. remove the second (v)pxor from
  3823. pxor reg,reg
  3824. ...
  3825. pxor reg,reg
  3826. }
  3827. Result:=false;
  3828. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  3829. MatchOpType(taicpu(p),top_reg,top_reg) and
  3830. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  3831. MatchInstruction(taicpu(hp1),taicpu(p).opcode,[taicpu(p).opsize]) and
  3832. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  3833. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) then
  3834. begin
  3835. DebugMsg(SPeepholeOptimization + 'PXorPXor2PXor done',hp1);
  3836. asml.Remove(hp1);
  3837. hp1.Free;
  3838. Result:=true;
  3839. Exit;
  3840. end;
  3841. end;
  3842. function TX86AsmOptimizer.OptPass1VPXor(var p: tai): boolean;
  3843. var
  3844. hp1: tai;
  3845. begin
  3846. {
  3847. remove the second (v)pxor from
  3848. (v)pxor reg,reg
  3849. ...
  3850. (v)pxor reg,reg
  3851. }
  3852. Result:=false;
  3853. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^,taicpu(p).oper[2]^) and
  3854. MatchOpType(taicpu(p),top_reg,top_reg,top_reg) and
  3855. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  3856. MatchInstruction(taicpu(hp1),taicpu(p).opcode,[taicpu(p).opsize]) and
  3857. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  3858. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^,taicpu(hp1).oper[2]^) then
  3859. begin
  3860. DebugMsg(SPeepholeOptimization + 'VPXorVPXor2PXor done',hp1);
  3861. asml.Remove(hp1);
  3862. hp1.Free;
  3863. Result:=true;
  3864. Exit;
  3865. end;
  3866. end;
  3867. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  3868. function IsXCHGAcceptable: Boolean; inline;
  3869. begin
  3870. { Always accept if optimising for size }
  3871. Result := (cs_opt_size in current_settings.optimizerswitches) or
  3872. (
  3873. {$ifdef x86_64}
  3874. { XCHG takes 3 cycles on AMD Athlon64 }
  3875. (current_settings.optimizecputype >= cpu_core_i)
  3876. {$else x86_64}
  3877. { From the Pentium M onwards, XCHG only has a latency of 2 rather
  3878. than 3, so it becomes a saving compared to three MOVs with two of
  3879. them able to execute simultaneously. [Kit] }
  3880. (current_settings.optimizecputype >= cpu_PentiumM)
  3881. {$endif x86_64}
  3882. );
  3883. end;
  3884. var
  3885. NewRef: TReference;
  3886. hp1,hp2,hp3: tai;
  3887. {$ifndef x86_64}
  3888. hp4: tai;
  3889. OperIdx: Integer;
  3890. {$endif x86_64}
  3891. begin
  3892. Result:=false;
  3893. if not GetNextInstruction(p, hp1) then
  3894. Exit;
  3895. if MatchInstruction(hp1, A_JMP, [S_NO]) then
  3896. begin
  3897. { Sometimes the MOVs that OptPass2JMP produces can be improved
  3898. further, but we can't just put this jump optimisation in pass 1
  3899. because it tends to perform worse when conditional jumps are
  3900. nearby (e.g. when converting CMOV instructions). [Kit] }
  3901. if OptPass2JMP(hp1) then
  3902. { call OptPass1MOV once to potentially merge any MOVs that were created }
  3903. Result := OptPass1MOV(p)
  3904. { OptPass2MOV will now exit but will be called again if OptPass1MOV
  3905. returned True and the instruction is still a MOV, thus checking
  3906. the optimisations below }
  3907. { If OptPass2JMP returned False, no optimisations were done to
  3908. the jump and there are no further optimisations that can be done
  3909. to the MOV instruction on this pass }
  3910. end
  3911. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3912. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  3913. MatchInstruction(hp1,A_ADD,A_SUB,[taicpu(p).opsize]) and
  3914. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3915. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) and
  3916. { be lazy, checking separately for sub would be slightly better }
  3917. (abs(taicpu(hp1).oper[0]^.val)<=$7fffffff) then
  3918. begin
  3919. { Change:
  3920. movl/q %reg1,%reg2 movl/q %reg1,%reg2
  3921. addl/q $x,%reg2 subl/q $x,%reg2
  3922. To:
  3923. leal/q x(%reg1),%reg2 leal/q -x(%reg1),%reg2
  3924. }
  3925. TransferUsedRegs(TmpUsedRegs);
  3926. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3927. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3928. if not GetNextInstruction(hp1, hp2) or
  3929. (
  3930. { The FLAGS register isn't always tracked properly, so do not
  3931. perform this optimisation if a conditional statement follows }
  3932. not RegReadByInstruction(NR_DEFAULTFLAGS, hp2) and
  3933. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)
  3934. ) then
  3935. begin
  3936. reference_reset(NewRef, 1, []);
  3937. NewRef.base := taicpu(p).oper[0]^.reg;
  3938. NewRef.scalefactor := 1;
  3939. if taicpu(hp1).opcode = A_ADD then
  3940. begin
  3941. DebugMsg(SPeepholeOptimization + 'MovAdd2Lea', p);
  3942. NewRef.offset := taicpu(hp1).oper[0]^.val;
  3943. end
  3944. else
  3945. begin
  3946. DebugMsg(SPeepholeOptimization + 'MovSub2Lea', p);
  3947. NewRef.offset := -taicpu(hp1).oper[0]^.val;
  3948. end;
  3949. taicpu(p).opcode := A_LEA;
  3950. taicpu(p).loadref(0, NewRef);
  3951. Asml.Remove(hp1);
  3952. hp1.Free;
  3953. Result := True;
  3954. Exit;
  3955. end;
  3956. end
  3957. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3958. {$ifdef x86_64}
  3959. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  3960. {$else x86_64}
  3961. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  3962. {$endif x86_64}
  3963. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3964. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  3965. { mov reg1, reg2 mov reg1, reg2
  3966. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  3967. begin
  3968. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  3969. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  3970. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  3971. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  3972. TransferUsedRegs(TmpUsedRegs);
  3973. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3974. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  3975. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  3976. then
  3977. begin
  3978. asml.remove(p);
  3979. p.free;
  3980. p := hp1;
  3981. Result:=true;
  3982. end;
  3983. exit;
  3984. end
  3985. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3986. IsXCHGAcceptable and
  3987. { XCHG doesn't support 8-byte registers }
  3988. (taicpu(p).opsize <> S_B) and
  3989. MatchInstruction(hp1, A_MOV, []) and
  3990. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3991. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  3992. GetNextInstruction(hp1, hp2) and
  3993. MatchInstruction(hp2, A_MOV, []) and
  3994. { Don't need to call MatchOpType for hp2 because the operand matches below cover for it }
  3995. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  3996. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) then
  3997. begin
  3998. { mov %reg1,%reg2
  3999. mov %reg3,%reg1 -> xchg %reg3,%reg1
  4000. mov %reg2,%reg3
  4001. (%reg2 not used afterwards)
  4002. Note that xchg takes 3 cycles to execute, and generally mov's take
  4003. only one cycle apiece, but the first two mov's can be executed in
  4004. parallel, only taking 2 cycles overall. Older processors should
  4005. therefore only optimise for size. [Kit]
  4006. }
  4007. TransferUsedRegs(TmpUsedRegs);
  4008. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4009. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  4010. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs) then
  4011. begin
  4012. DebugMsg(SPeepholeOptimization + 'MovMovMov2XChg', p);
  4013. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp1, UsedRegs);
  4014. taicpu(hp1).opcode := A_XCHG;
  4015. asml.Remove(p);
  4016. asml.Remove(hp2);
  4017. p.Free;
  4018. hp2.Free;
  4019. p := hp1;
  4020. Result := True;
  4021. Exit;
  4022. end;
  4023. end
  4024. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  4025. MatchInstruction(hp1, A_SAR, []) then
  4026. begin
  4027. if MatchOperand(taicpu(hp1).oper[0]^, 31) then
  4028. begin
  4029. { the use of %edx also covers the opsize being S_L }
  4030. if MatchOperand(taicpu(hp1).oper[1]^, NR_EDX) then
  4031. begin
  4032. { Note it has to be specifically "movl %eax,%edx", and those specific sub-registers }
  4033. if (taicpu(p).oper[0]^.reg = NR_EAX) and
  4034. (taicpu(p).oper[1]^.reg = NR_EDX) then
  4035. begin
  4036. { Change:
  4037. movl %eax,%edx
  4038. sarl $31,%edx
  4039. To:
  4040. cltd
  4041. }
  4042. DebugMsg(SPeepholeOptimization + 'MovSar2Cltd', p);
  4043. Asml.Remove(hp1);
  4044. hp1.Free;
  4045. taicpu(p).opcode := A_CDQ;
  4046. taicpu(p).opsize := S_NO;
  4047. taicpu(p).clearop(1);
  4048. taicpu(p).clearop(0);
  4049. taicpu(p).ops:=0;
  4050. Result := True;
  4051. end
  4052. else if (cs_opt_size in current_settings.optimizerswitches) and
  4053. (taicpu(p).oper[0]^.reg = NR_EDX) and
  4054. (taicpu(p).oper[1]^.reg = NR_EAX) then
  4055. begin
  4056. { Change:
  4057. movl %edx,%eax
  4058. sarl $31,%edx
  4059. To:
  4060. movl %edx,%eax
  4061. cltd
  4062. Note that this creates a dependency between the two instructions,
  4063. so only perform if optimising for size.
  4064. }
  4065. DebugMsg(SPeepholeOptimization + 'MovSar2MovCltd', p);
  4066. taicpu(hp1).opcode := A_CDQ;
  4067. taicpu(hp1).opsize := S_NO;
  4068. taicpu(hp1).clearop(1);
  4069. taicpu(hp1).clearop(0);
  4070. taicpu(hp1).ops:=0;
  4071. end;
  4072. {$ifndef x86_64}
  4073. end
  4074. { Don't bother if CMOV is supported, because a more optimal
  4075. sequence would have been generated for the Abs() intrinsic }
  4076. else if not(CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and
  4077. { the use of %eax also covers the opsize being S_L }
  4078. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) and
  4079. (taicpu(p).oper[0]^.reg = NR_EAX) and
  4080. (taicpu(p).oper[1]^.reg = NR_EDX) and
  4081. GetNextInstruction(hp1, hp2) and
  4082. MatchInstruction(hp2, A_XOR, [S_L]) and
  4083. MatchOperand(taicpu(hp2).oper[0]^, NR_EAX) and
  4084. MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) and
  4085. GetNextInstruction(hp2, hp3) and
  4086. MatchInstruction(hp3, A_SUB, [S_L]) and
  4087. MatchOperand(taicpu(hp3).oper[0]^, NR_EAX) and
  4088. MatchOperand(taicpu(hp3).oper[1]^, NR_EDX) then
  4089. begin
  4090. { Change:
  4091. movl %eax,%edx
  4092. sarl $31,%eax
  4093. xorl %eax,%edx
  4094. subl %eax,%edx
  4095. (Instruction that uses %edx)
  4096. (%eax deallocated)
  4097. (%edx deallocated)
  4098. To:
  4099. cltd
  4100. xorl %edx,%eax <-- Note the registers have swapped
  4101. subl %edx,%eax
  4102. (Instruction that uses %eax) <-- %eax rather than %edx
  4103. }
  4104. TransferUsedRegs(TmpUsedRegs);
  4105. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4106. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  4107. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  4108. if not RegUsedAfterInstruction(NR_EAX, hp3, TmpUsedRegs) then
  4109. begin
  4110. if GetNextInstruction(hp3, hp4) and
  4111. not RegModifiedByInstruction(NR_EDX, hp4) and
  4112. not RegUsedAfterInstruction(NR_EDX, hp4, TmpUsedRegs) then
  4113. begin
  4114. DebugMsg(SPeepholeOptimization + 'abs() intrinsic optimisation', p);
  4115. taicpu(p).opcode := A_CDQ;
  4116. taicpu(p).clearop(1);
  4117. taicpu(p).clearop(0);
  4118. taicpu(p).ops:=0;
  4119. AsmL.Remove(hp1);
  4120. hp1.Free;
  4121. taicpu(hp2).loadreg(0, NR_EDX);
  4122. taicpu(hp2).loadreg(1, NR_EAX);
  4123. taicpu(hp3).loadreg(0, NR_EDX);
  4124. taicpu(hp3).loadreg(1, NR_EAX);
  4125. AllocRegBetween(NR_EAX, hp3, hp4, TmpUsedRegs);
  4126. { Convert references in the following instruction (hp4) from %edx to %eax }
  4127. for OperIdx := 0 to taicpu(hp4).ops - 1 do
  4128. with taicpu(hp4).oper[OperIdx]^ do
  4129. case typ of
  4130. top_reg:
  4131. if getsupreg(reg) = RS_EDX then
  4132. reg := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  4133. top_ref:
  4134. begin
  4135. if getsupreg(reg) = RS_EDX then
  4136. ref^.base := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  4137. if getsupreg(reg) = RS_EDX then
  4138. ref^.index := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  4139. end;
  4140. else
  4141. ;
  4142. end;
  4143. end;
  4144. end;
  4145. {$else x86_64}
  4146. end;
  4147. end
  4148. else if MatchOperand(taicpu(hp1).oper[0]^, 63) and
  4149. { the use of %rdx also covers the opsize being S_Q }
  4150. MatchOperand(taicpu(hp1).oper[1]^, NR_RDX) then
  4151. begin
  4152. { Note it has to be specifically "movq %rax,%rdx", and those specific sub-registers }
  4153. if (taicpu(p).oper[0]^.reg = NR_RAX) and
  4154. (taicpu(p).oper[1]^.reg = NR_RDX) then
  4155. begin
  4156. { Change:
  4157. movq %rax,%rdx
  4158. sarq $63,%rdx
  4159. To:
  4160. cqto
  4161. }
  4162. DebugMsg(SPeepholeOptimization + 'MovSar2Cqto', p);
  4163. Asml.Remove(hp1);
  4164. hp1.Free;
  4165. taicpu(p).opcode := A_CQO;
  4166. taicpu(p).opsize := S_NO;
  4167. taicpu(p).clearop(1);
  4168. taicpu(p).clearop(0);
  4169. taicpu(p).ops:=0;
  4170. Result := True;
  4171. end
  4172. else if (cs_opt_size in current_settings.optimizerswitches) and
  4173. (taicpu(p).oper[0]^.reg = NR_RDX) and
  4174. (taicpu(p).oper[1]^.reg = NR_RAX) then
  4175. begin
  4176. { Change:
  4177. movq %rdx,%rax
  4178. sarq $63,%rdx
  4179. To:
  4180. movq %rdx,%rax
  4181. cqto
  4182. Note that this creates a dependency between the two instructions,
  4183. so only perform if optimising for size.
  4184. }
  4185. DebugMsg(SPeepholeOptimization + 'MovSar2MovCqto', p);
  4186. taicpu(hp1).opcode := A_CQO;
  4187. taicpu(hp1).opsize := S_NO;
  4188. taicpu(hp1).clearop(1);
  4189. taicpu(hp1).clearop(0);
  4190. taicpu(hp1).ops:=0;
  4191. {$endif x86_64}
  4192. end;
  4193. end;
  4194. end
  4195. else if MatchInstruction(hp1, A_MOV, []) and
  4196. (taicpu(hp1).oper[1]^.typ = top_reg) then
  4197. { Though "GetNextInstruction" could be factored out, along with
  4198. the instructions that depend on hp2, it is an expensive call that
  4199. should be delayed for as long as possible, hence we do cheaper
  4200. checks first that are likely to be False. [Kit] }
  4201. begin
  4202. if MatchOperand(taicpu(p).oper[1]^, NR_EDX) and
  4203. (
  4204. (
  4205. (taicpu(hp1).oper[1]^.reg = NR_EAX) and
  4206. (
  4207. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4208. MatchOperand(taicpu(hp1).oper[0]^, NR_EDX)
  4209. )
  4210. ) or
  4211. (
  4212. (taicpu(hp1).oper[1]^.reg = NR_EDX) and
  4213. (
  4214. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4215. MatchOperand(taicpu(hp1).oper[0]^, NR_EAX)
  4216. )
  4217. )
  4218. ) and
  4219. GetNextInstruction(hp1, hp2) and
  4220. MatchInstruction(hp2, A_SAR, []) and
  4221. MatchOperand(taicpu(hp2).oper[0]^, 31) then
  4222. begin
  4223. if MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) then
  4224. begin
  4225. { Change:
  4226. movl r/m,%edx movl r/m,%eax movl r/m,%edx movl r/m,%eax
  4227. movl %edx,%eax or movl %eax,%edx or movl r/m,%eax or movl r/m,%edx
  4228. sarl $31,%edx sarl $31,%edx sarl $31,%edx sarl $31,%edx
  4229. To:
  4230. movl r/m,%eax <- Note the change in register
  4231. cltd
  4232. }
  4233. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCltd', p);
  4234. AllocRegBetween(NR_EAX, p, hp1, UsedRegs);
  4235. taicpu(p).loadreg(1, NR_EAX);
  4236. taicpu(hp1).opcode := A_CDQ;
  4237. taicpu(hp1).clearop(1);
  4238. taicpu(hp1).clearop(0);
  4239. taicpu(hp1).ops:=0;
  4240. AsmL.Remove(hp2);
  4241. hp2.Free;
  4242. (*
  4243. {$ifdef x86_64}
  4244. end
  4245. else if MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) and
  4246. { This code sequence does not get generated - however it might become useful
  4247. if and when 128-bit signed integer types make an appearance, so the code
  4248. is kept here for when it is eventually needed. [Kit] }
  4249. (
  4250. (
  4251. (taicpu(hp1).oper[1]^.reg = NR_RAX) and
  4252. (
  4253. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4254. MatchOperand(taicpu(hp1).oper[0]^, NR_RDX)
  4255. )
  4256. ) or
  4257. (
  4258. (taicpu(hp1).oper[1]^.reg = NR_RDX) and
  4259. (
  4260. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4261. MatchOperand(taicpu(hp1).oper[0]^, NR_RAX)
  4262. )
  4263. )
  4264. ) and
  4265. GetNextInstruction(hp1, hp2) and
  4266. MatchInstruction(hp2, A_SAR, [S_Q]) and
  4267. MatchOperand(taicpu(hp2).oper[0]^, 63) and
  4268. MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) then
  4269. begin
  4270. { Change:
  4271. movq r/m,%rdx movq r/m,%rax movq r/m,%rdx movq r/m,%rax
  4272. movq %rdx,%rax or movq %rax,%rdx or movq r/m,%rax or movq r/m,%rdx
  4273. sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx
  4274. To:
  4275. movq r/m,%rax <- Note the change in register
  4276. cqto
  4277. }
  4278. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCqto', p);
  4279. AllocRegBetween(NR_RAX, p, hp1, UsedRegs);
  4280. taicpu(p).loadreg(1, NR_RAX);
  4281. taicpu(hp1).opcode := A_CQO;
  4282. taicpu(hp1).clearop(1);
  4283. taicpu(hp1).clearop(0);
  4284. taicpu(hp1).ops:=0;
  4285. AsmL.Remove(hp2);
  4286. hp2.Free;
  4287. {$endif x86_64}
  4288. *)
  4289. end;
  4290. end;
  4291. end
  4292. else if (taicpu(p).oper[0]^.typ = top_ref) and
  4293. (hp1.typ = ait_instruction) and
  4294. { while the GetNextInstruction(hp1,hp2) call could be factored out,
  4295. doing it separately in both branches allows to do the cheap checks
  4296. with low probability earlier }
  4297. ((IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  4298. GetNextInstruction(hp1,hp2) and
  4299. MatchInstruction(hp2,A_MOV,[])
  4300. ) or
  4301. ((taicpu(hp1).opcode=A_LEA) and
  4302. GetNextInstruction(hp1,hp2) and
  4303. MatchInstruction(hp2,A_MOV,[]) and
  4304. ((MatchReference(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  4305. (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg)
  4306. ) or
  4307. (MatchReference(taicpu(hp1).oper[0]^.ref^,NR_INVALID,
  4308. taicpu(p).oper[1]^.reg) and
  4309. (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg)) or
  4310. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_NO)) or
  4311. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,NR_NO,taicpu(p).oper[1]^.reg))
  4312. ) and
  4313. ((MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^)) or not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)))
  4314. )
  4315. ) and
  4316. MatchOperand(taicpu(hp1).oper[taicpu(hp1).ops-1]^,taicpu(hp2).oper[0]^) and
  4317. (taicpu(hp2).oper[1]^.typ = top_ref) then
  4318. begin
  4319. TransferUsedRegs(TmpUsedRegs);
  4320. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  4321. UpdateUsedRegs(TmpUsedRegs,tai(hp1.next));
  4322. if (RefsEqual(taicpu(hp2).oper[1]^.ref^,taicpu(p).oper[0]^.ref^) and
  4323. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,TmpUsedRegs))) then
  4324. { change mov (ref), reg
  4325. add/sub/or/... reg2/$const, reg
  4326. mov reg, (ref)
  4327. # release reg
  4328. to add/sub/or/... reg2/$const, (ref) }
  4329. begin
  4330. case taicpu(hp1).opcode of
  4331. A_INC,A_DEC,A_NOT,A_NEG :
  4332. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  4333. A_LEA :
  4334. begin
  4335. taicpu(hp1).opcode:=A_ADD;
  4336. if (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.index<>NR_NO) then
  4337. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.index)
  4338. else if (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.base<>NR_NO) then
  4339. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.base)
  4340. else
  4341. taicpu(hp1).loadconst(0,taicpu(hp1).oper[0]^.ref^.offset);
  4342. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  4343. DebugMsg(SPeepholeOptimization + 'FoldLea done',hp1);
  4344. end
  4345. else
  4346. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  4347. end;
  4348. asml.remove(p);
  4349. asml.remove(hp2);
  4350. p.free;
  4351. hp2.free;
  4352. p := hp1
  4353. end;
  4354. Exit;
  4355. {$ifdef x86_64}
  4356. end
  4357. else if (taicpu(p).opsize = S_L) and
  4358. (taicpu(p).oper[1]^.typ = top_reg) and
  4359. (
  4360. MatchInstruction(hp1, A_MOV,[]) and
  4361. (taicpu(hp1).opsize = S_L) and
  4362. (taicpu(hp1).oper[1]^.typ = top_reg)
  4363. ) and (
  4364. GetNextInstruction(hp1, hp2) and
  4365. (tai(hp2).typ=ait_instruction) and
  4366. (taicpu(hp2).opsize = S_Q) and
  4367. (
  4368. (
  4369. MatchInstruction(hp2, A_ADD,[]) and
  4370. (taicpu(hp2).opsize = S_Q) and
  4371. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4372. (
  4373. (
  4374. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4375. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4376. ) or (
  4377. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4378. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4379. )
  4380. )
  4381. ) or (
  4382. MatchInstruction(hp2, A_LEA,[]) and
  4383. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  4384. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  4385. (
  4386. (
  4387. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4388. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4389. ) or (
  4390. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4391. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  4392. )
  4393. ) and (
  4394. (
  4395. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4396. ) or (
  4397. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4398. )
  4399. )
  4400. )
  4401. )
  4402. ) and (
  4403. GetNextInstruction(hp2, hp3) and
  4404. MatchInstruction(hp3, A_SHR,[]) and
  4405. (taicpu(hp3).opsize = S_Q) and
  4406. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4407. (taicpu(hp3).oper[0]^.val = 1) and
  4408. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  4409. ) then
  4410. begin
  4411. { Change movl x, reg1d movl x, reg1d
  4412. movl y, reg2d movl y, reg2d
  4413. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  4414. shrq $1, reg1q shrq $1, reg1q
  4415. ( reg1d and reg2d can be switched around in the first two instructions )
  4416. To movl x, reg1d
  4417. addl y, reg1d
  4418. rcrl $1, reg1d
  4419. This corresponds to the common expression (x + y) shr 1, where
  4420. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  4421. smaller code, but won't account for x + y causing an overflow). [Kit]
  4422. }
  4423. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  4424. { Change first MOV command to have the same register as the final output }
  4425. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  4426. else
  4427. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  4428. { Change second MOV command to an ADD command. This is easier than
  4429. converting the existing command because it means we don't have to
  4430. touch 'y', which might be a complicated reference, and also the
  4431. fact that the third command might either be ADD or LEA. [Kit] }
  4432. taicpu(hp1).opcode := A_ADD;
  4433. { Delete old ADD/LEA instruction }
  4434. asml.remove(hp2);
  4435. hp2.free;
  4436. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  4437. taicpu(hp3).opcode := A_RCR;
  4438. taicpu(hp3).changeopsize(S_L);
  4439. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  4440. {$endif x86_64}
  4441. end;
  4442. end;
  4443. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  4444. var
  4445. hp1 : tai;
  4446. begin
  4447. Result:=false;
  4448. if (taicpu(p).ops >= 2) and
  4449. ((taicpu(p).oper[0]^.typ = top_const) or
  4450. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  4451. (taicpu(p).oper[1]^.typ = top_reg) and
  4452. ((taicpu(p).ops = 2) or
  4453. ((taicpu(p).oper[2]^.typ = top_reg) and
  4454. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  4455. GetLastInstruction(p,hp1) and
  4456. MatchInstruction(hp1,A_MOV,[]) and
  4457. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  4458. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4459. begin
  4460. TransferUsedRegs(TmpUsedRegs);
  4461. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) or
  4462. ((taicpu(p).ops = 3) and (taicpu(p).oper[1]^.reg=taicpu(p).oper[2]^.reg)) then
  4463. { change
  4464. mov reg1,reg2
  4465. imul y,reg2 to imul y,reg1,reg2 }
  4466. begin
  4467. taicpu(p).ops := 3;
  4468. taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
  4469. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  4470. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  4471. asml.remove(hp1);
  4472. hp1.free;
  4473. result:=true;
  4474. end;
  4475. end;
  4476. end;
  4477. procedure TX86AsmOptimizer.ConvertJumpToRET(const p: tai; const ret_p: tai);
  4478. var
  4479. ThisLabel: TAsmLabel;
  4480. begin
  4481. ThisLabel := tasmlabel(taicpu(p).oper[0]^.ref^.symbol);
  4482. ThisLabel.decrefs;
  4483. taicpu(p).opcode := A_RET;
  4484. taicpu(p).is_jmp := false;
  4485. taicpu(p).ops := taicpu(ret_p).ops;
  4486. case taicpu(ret_p).ops of
  4487. 0:
  4488. taicpu(p).clearop(0);
  4489. 1:
  4490. taicpu(p).loadconst(0,taicpu(ret_p).oper[0]^.val);
  4491. else
  4492. internalerror(2016041301);
  4493. end;
  4494. { If the original label is now dead, it might turn out that the label
  4495. immediately follows p. As a result, everything beyond it, which will
  4496. be just some final register configuration and a RET instruction, is
  4497. now dead code. [Kit] }
  4498. { NOTE: This is much faster than introducing a OptPass2RET routine and
  4499. running RemoveDeadCodeAfterJump for each RET instruction, because
  4500. this optimisation rarely happens and most RETs appear at the end of
  4501. routines where there is nothing that can be stripped. [Kit] }
  4502. if not ThisLabel.is_used then
  4503. RemoveDeadCodeAfterJump(p);
  4504. end;
  4505. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  4506. var
  4507. hp1, hp2, hp3: tai;
  4508. OperIdx: Integer;
  4509. begin
  4510. result:=false;
  4511. 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
  4512. (taicpu(p).oper[0]^.ref^.index=NR_NO) and (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) then
  4513. begin
  4514. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  4515. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and (hp1.typ = ait_instruction) then
  4516. begin
  4517. case taicpu(hp1).opcode of
  4518. A_RET:
  4519. {
  4520. change
  4521. jmp .L1
  4522. ...
  4523. .L1:
  4524. ret
  4525. into
  4526. ret
  4527. }
  4528. begin
  4529. ConvertJumpToRET(p, hp1);
  4530. result:=true;
  4531. end;
  4532. A_MOV:
  4533. {
  4534. change
  4535. jmp .L1
  4536. ...
  4537. .L1:
  4538. mov ##, ##
  4539. ret
  4540. into
  4541. mov ##, ##
  4542. ret
  4543. }
  4544. { This optimisation tends to increase code size if the pass 1 MOV optimisations aren't
  4545. re-run, so only do this particular optimisation if optimising for speed or when
  4546. optimisations are very in-depth. [Kit] }
  4547. if (current_settings.optimizerswitches * [cs_opt_level3, cs_opt_size]) <> [cs_opt_size] then
  4548. begin
  4549. GetNextInstruction(hp1, hp2);
  4550. if not Assigned(hp2) then
  4551. Exit;
  4552. if (hp2.typ in [ait_label, ait_align]) then
  4553. SkipLabels(hp2,hp2);
  4554. if Assigned(hp2) and MatchInstruction(hp2, A_RET, [S_NO]) then
  4555. begin
  4556. { Duplicate the MOV instruction }
  4557. hp3:=tai(hp1.getcopy);
  4558. asml.InsertBefore(hp3, p);
  4559. { Make sure the compiler knows about any final registers written here }
  4560. for OperIdx := 0 to 1 do
  4561. with taicpu(hp3).oper[OperIdx]^ do
  4562. begin
  4563. case typ of
  4564. top_ref:
  4565. begin
  4566. if (ref^.base <> NR_NO) {$ifdef x86_64} and (ref^.base <> NR_RIP) {$endif x86_64} then
  4567. AllocRegBetween(ref^.base, hp3, tai(p.Next), UsedRegs);
  4568. if (ref^.index <> NR_NO) {$ifdef x86_64} and (ref^.index <> NR_RIP) {$endif x86_64} then
  4569. AllocRegBetween(ref^.index, hp3, tai(p.Next), UsedRegs);
  4570. end;
  4571. top_reg:
  4572. AllocRegBetween(reg, hp3, tai(p.Next), UsedRegs);
  4573. else
  4574. ;
  4575. end;
  4576. end;
  4577. { Now change the jump into a RET instruction }
  4578. ConvertJumpToRET(p, hp2);
  4579. result:=true;
  4580. end;
  4581. end;
  4582. else
  4583. ;
  4584. end;
  4585. end;
  4586. end;
  4587. end;
  4588. class function TX86AsmOptimizer.CanBeCMOV(p : tai) : boolean;
  4589. begin
  4590. CanBeCMOV:=assigned(p) and
  4591. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  4592. { we can't use cmov ref,reg because
  4593. ref could be nil and cmov still throws an exception
  4594. if ref=nil but the mov isn't done (FK)
  4595. or ((taicpu(p).oper[0]^.typ = top_ref) and
  4596. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  4597. }
  4598. (taicpu(p).oper[1]^.typ = top_reg) and
  4599. (
  4600. (taicpu(p).oper[0]^.typ = top_reg) or
  4601. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  4602. it is not expected that this can cause a seg. violation }
  4603. (
  4604. (taicpu(p).oper[0]^.typ = top_ref) and
  4605. IsRefSafe(taicpu(p).oper[0]^.ref)
  4606. )
  4607. );
  4608. end;
  4609. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  4610. var
  4611. hp1,hp2,hp3,hp4,hpmov2: tai;
  4612. carryadd_opcode : TAsmOp;
  4613. l : Longint;
  4614. condition : TAsmCond;
  4615. symbol: TAsmSymbol;
  4616. reg: tsuperregister;
  4617. regavailable: Boolean;
  4618. begin
  4619. result:=false;
  4620. symbol:=nil;
  4621. if GetNextInstruction(p,hp1) then
  4622. begin
  4623. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  4624. if (hp1.typ=ait_instruction) and
  4625. GetNextInstruction(hp1,hp2) and
  4626. ((hp2.typ=ait_label) or
  4627. { trick to skip align }
  4628. ((hp2.typ=ait_align) and GetNextInstruction(hp2,hp2) and (hp2.typ=ait_label))
  4629. ) and
  4630. (Tasmlabel(symbol) = Tai_label(hp2).labsym) then
  4631. { jb @@1 cmc
  4632. inc/dec operand --> adc/sbb operand,0
  4633. @@1:
  4634. ... and ...
  4635. jnb @@1
  4636. inc/dec operand --> adc/sbb operand,0
  4637. @@1: }
  4638. begin
  4639. carryadd_opcode:=A_NONE;
  4640. if Taicpu(p).condition in [C_NAE,C_B,C_C] then
  4641. begin
  4642. if (Taicpu(hp1).opcode=A_INC) or
  4643. ((Taicpu(hp1).opcode=A_ADD) and
  4644. MatchOptype(Taicpu(hp1),top_const,top_reg) and
  4645. (Taicpu(hp1).oper[0]^.val=1)
  4646. ) then
  4647. carryadd_opcode:=A_ADC;
  4648. if (Taicpu(hp1).opcode=A_DEC) or
  4649. ((Taicpu(hp1).opcode=A_SUB) and
  4650. MatchOptype(Taicpu(hp1),top_const,top_reg) and
  4651. (Taicpu(hp1).oper[0]^.val=1)
  4652. ) then
  4653. carryadd_opcode:=A_SBB;
  4654. if carryadd_opcode<>A_NONE then
  4655. begin
  4656. Taicpu(p).clearop(0);
  4657. Taicpu(p).ops:=0;
  4658. Taicpu(p).is_jmp:=false;
  4659. Taicpu(p).opcode:=A_CMC;
  4660. Taicpu(p).condition:=C_NONE;
  4661. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2CmcAdc/Sbb',p);
  4662. Taicpu(hp1).ops:=2;
  4663. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  4664. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  4665. else
  4666. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  4667. Taicpu(hp1).loadconst(0,0);
  4668. Taicpu(hp1).opcode:=carryadd_opcode;
  4669. result:=true;
  4670. exit;
  4671. end;
  4672. end
  4673. else if Taicpu(p).condition in [C_AE,C_NB,C_NC] then
  4674. begin
  4675. if (Taicpu(hp1).opcode=A_INC) or
  4676. ((Taicpu(hp1).opcode=A_ADD) and
  4677. MatchOptype(Taicpu(hp1),top_const,top_reg) and
  4678. (Taicpu(hp1).oper[0]^.val=1)
  4679. ) then
  4680. carryadd_opcode:=A_ADC;
  4681. if (Taicpu(hp1).opcode=A_DEC) or
  4682. ((Taicpu(hp1).opcode=A_SUB) and
  4683. MatchOptype(Taicpu(hp1),top_const,top_reg) and
  4684. (Taicpu(hp1).oper[0]^.val=1)
  4685. ) then
  4686. carryadd_opcode:=A_SBB;
  4687. if carryadd_opcode<>A_NONE then
  4688. begin
  4689. Taicpu(hp1).ops:=2;
  4690. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2Adc/Sbb',p);
  4691. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  4692. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  4693. else
  4694. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  4695. Taicpu(hp1).loadconst(0,0);
  4696. Taicpu(hp1).opcode:=carryadd_opcode;
  4697. RemoveCurrentP(p, hp1);
  4698. result:=true;
  4699. exit;
  4700. end;
  4701. end
  4702. {
  4703. jcc @@1 setcc tmpreg
  4704. inc/dec/add/sub operand -> (movzx tmpreg)
  4705. @@1: add/sub tmpreg,operand
  4706. While this increases code size slightly, it makes the code much faster if the
  4707. jump is unpredictable
  4708. }
  4709. else if not(cs_opt_size in current_settings.optimizerswitches) and
  4710. ((((Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB)) and
  4711. (Taicpu(hp1).oper[0]^.typ=top_const) and
  4712. (Taicpu(hp1).oper[1]^.typ=top_reg) and
  4713. (Taicpu(hp1).oper[0]^.val=1)) or
  4714. ((Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC))
  4715. ) then
  4716. begin
  4717. TransferUsedRegs(TmpUsedRegs);
  4718. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4719. { search for an available register which is volatile }
  4720. regavailable:=false;
  4721. for reg in tcpuregisterset do
  4722. begin
  4723. if (reg in paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption)) and
  4724. not(reg in TmpUsedRegs[R_INTREGISTER].GetUsedRegs) and
  4725. not(RegInInstruction(newreg(R_INTREGISTER,reg,R_SUBL),hp1))
  4726. {$ifdef i386}
  4727. and (reg in [RS_EAX,RS_EBX,RS_ECX,RS_EDX])
  4728. {$endif i386}
  4729. then
  4730. begin
  4731. regavailable:=true;
  4732. break;
  4733. end;
  4734. end;
  4735. if regavailable then
  4736. begin
  4737. Taicpu(p).clearop(0);
  4738. Taicpu(p).ops:=1;
  4739. Taicpu(p).is_jmp:=false;
  4740. Taicpu(p).opcode:=A_SETcc;
  4741. DebugMsg(SPeepholeOptimization+'JccAdd2SetccAdd',p);
  4742. Taicpu(p).condition:=inverse_cond(Taicpu(p).condition);
  4743. Taicpu(p).loadreg(0,newreg(R_INTREGISTER,reg,R_SUBL));
  4744. if getsubreg(Taicpu(hp1).oper[1]^.reg)<>R_SUBL then
  4745. begin
  4746. case getsubreg(Taicpu(hp1).oper[1]^.reg) of
  4747. R_SUBW:
  4748. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BW,newreg(R_INTREGISTER,reg,R_SUBL),
  4749. newreg(R_INTREGISTER,reg,R_SUBW));
  4750. R_SUBD,
  4751. R_SUBQ:
  4752. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,newreg(R_INTREGISTER,reg,R_SUBL),
  4753. newreg(R_INTREGISTER,reg,R_SUBD));
  4754. else
  4755. Internalerror(2020030601);
  4756. end;
  4757. taicpu(hp2).fileinfo:=taicpu(hp1).fileinfo;
  4758. asml.InsertAfter(hp2,p);
  4759. end;
  4760. if (Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC) then
  4761. begin
  4762. Taicpu(hp1).ops:=2;
  4763. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^)
  4764. end;
  4765. Taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,reg,getsubreg(Taicpu(hp1).oper[1]^.reg)));
  4766. AllocRegBetween(newreg(R_INTREGISTER,reg,getsubreg(Taicpu(hp1).oper[1]^.reg)),p,hp1,UsedRegs);
  4767. end;
  4768. end;
  4769. end;
  4770. { Detect the following:
  4771. jmp<cond> @Lbl1
  4772. jmp @Lbl2
  4773. ...
  4774. @Lbl1:
  4775. ret
  4776. Change to:
  4777. jmp<inv_cond> @Lbl2
  4778. ret
  4779. }
  4780. if MatchInstruction(hp1,A_JMP,[]) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  4781. begin
  4782. hp2:=getlabelwithsym(TAsmLabel(symbol));
  4783. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  4784. MatchInstruction(hp2,A_RET,[S_NO]) then
  4785. begin
  4786. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  4787. { Change label address to that of the unconditional jump }
  4788. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  4789. TAsmLabel(symbol).DecRefs;
  4790. taicpu(hp1).opcode := A_RET;
  4791. taicpu(hp1).is_jmp := false;
  4792. taicpu(hp1).ops := taicpu(hp2).ops;
  4793. DebugMsg(SPeepholeOptimization+'JccJmpRet2J!ccRet',p);
  4794. case taicpu(hp2).ops of
  4795. 0:
  4796. taicpu(hp1).clearop(0);
  4797. 1:
  4798. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  4799. else
  4800. internalerror(2016041302);
  4801. end;
  4802. end;
  4803. end;
  4804. end;
  4805. {$ifndef i8086}
  4806. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  4807. begin
  4808. { check for
  4809. jCC xxx
  4810. <several movs>
  4811. xxx:
  4812. }
  4813. l:=0;
  4814. GetNextInstruction(p, hp1);
  4815. while assigned(hp1) and
  4816. CanBeCMOV(hp1) and
  4817. { stop on labels }
  4818. not(hp1.typ=ait_label) do
  4819. begin
  4820. inc(l);
  4821. GetNextInstruction(hp1,hp1);
  4822. end;
  4823. if assigned(hp1) then
  4824. begin
  4825. if FindLabel(tasmlabel(symbol),hp1) then
  4826. begin
  4827. if (l<=4) and (l>0) then
  4828. begin
  4829. condition:=inverse_cond(taicpu(p).condition);
  4830. GetNextInstruction(p,hp1);
  4831. repeat
  4832. if not Assigned(hp1) then
  4833. InternalError(2018062900);
  4834. taicpu(hp1).opcode:=A_CMOVcc;
  4835. taicpu(hp1).condition:=condition;
  4836. UpdateUsedRegs(hp1);
  4837. GetNextInstruction(hp1,hp1);
  4838. until not(CanBeCMOV(hp1));
  4839. { Remember what hp1 is in case there's multiple aligns to get rid of }
  4840. hp2 := hp1;
  4841. repeat
  4842. if not Assigned(hp2) then
  4843. InternalError(2018062910);
  4844. case hp2.typ of
  4845. ait_label:
  4846. { What we expected - break out of the loop (it won't be a dead label at the top of
  4847. a cluster because that was optimised at an earlier stage) }
  4848. Break;
  4849. ait_align:
  4850. { Go to the next entry until a label is found (may be multiple aligns before it) }
  4851. begin
  4852. hp2 := tai(hp2.Next);
  4853. Continue;
  4854. end;
  4855. else
  4856. begin
  4857. { Might be a comment or temporary allocation entry }
  4858. if not (hp2.typ in SkipInstr) then
  4859. InternalError(2018062911);
  4860. hp2 := tai(hp2.Next);
  4861. Continue;
  4862. end;
  4863. end;
  4864. until False;
  4865. { Now we can safely decrement the reference count }
  4866. tasmlabel(symbol).decrefs;
  4867. DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
  4868. { Remove the original jump }
  4869. asml.Remove(p);
  4870. p.Free;
  4871. GetNextInstruction(hp2, p); { Instruction after the label }
  4872. { Remove the label if this is its final reference }
  4873. if (tasmlabel(symbol).getrefs=0) then
  4874. StripLabelFast(hp1);
  4875. if Assigned(p) then
  4876. begin
  4877. UpdateUsedRegs(p);
  4878. result:=true;
  4879. end;
  4880. exit;
  4881. end;
  4882. end
  4883. else
  4884. begin
  4885. { check further for
  4886. jCC xxx
  4887. <several movs 1>
  4888. jmp yyy
  4889. xxx:
  4890. <several movs 2>
  4891. yyy:
  4892. }
  4893. { hp2 points to jmp yyy }
  4894. hp2:=hp1;
  4895. { skip hp1 to xxx (or an align right before it) }
  4896. GetNextInstruction(hp1, hp1);
  4897. if assigned(hp2) and
  4898. assigned(hp1) and
  4899. (l<=3) and
  4900. (hp2.typ=ait_instruction) and
  4901. (taicpu(hp2).is_jmp) and
  4902. (taicpu(hp2).condition=C_None) and
  4903. { real label and jump, no further references to the
  4904. label are allowed }
  4905. (tasmlabel(symbol).getrefs=1) and
  4906. FindLabel(tasmlabel(symbol),hp1) then
  4907. begin
  4908. l:=0;
  4909. { skip hp1 to <several moves 2> }
  4910. if (hp1.typ = ait_align) then
  4911. GetNextInstruction(hp1, hp1);
  4912. GetNextInstruction(hp1, hpmov2);
  4913. hp1 := hpmov2;
  4914. while assigned(hp1) and
  4915. CanBeCMOV(hp1) do
  4916. begin
  4917. inc(l);
  4918. GetNextInstruction(hp1, hp1);
  4919. end;
  4920. { hp1 points to yyy (or an align right before it) }
  4921. hp3 := hp1;
  4922. if assigned(hp1) and
  4923. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  4924. begin
  4925. condition:=inverse_cond(taicpu(p).condition);
  4926. GetNextInstruction(p,hp1);
  4927. repeat
  4928. taicpu(hp1).opcode:=A_CMOVcc;
  4929. taicpu(hp1).condition:=condition;
  4930. UpdateUsedRegs(hp1);
  4931. GetNextInstruction(hp1,hp1);
  4932. until not(assigned(hp1)) or
  4933. not(CanBeCMOV(hp1));
  4934. condition:=inverse_cond(condition);
  4935. hp1 := hpmov2;
  4936. { hp1 is now at <several movs 2> }
  4937. while Assigned(hp1) and CanBeCMOV(hp1) do
  4938. begin
  4939. taicpu(hp1).opcode:=A_CMOVcc;
  4940. taicpu(hp1).condition:=condition;
  4941. UpdateUsedRegs(hp1);
  4942. GetNextInstruction(hp1,hp1);
  4943. end;
  4944. hp1 := p;
  4945. { Get first instruction after label }
  4946. GetNextInstruction(hp3, p);
  4947. if assigned(p) and (hp3.typ = ait_align) then
  4948. GetNextInstruction(p, p);
  4949. { Don't dereference yet, as doing so will cause
  4950. GetNextInstruction to skip the label and
  4951. optional align marker. [Kit] }
  4952. GetNextInstruction(hp2, hp4);
  4953. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
  4954. { remove jCC }
  4955. asml.remove(hp1);
  4956. hp1.free;
  4957. { Now we can safely decrement it }
  4958. tasmlabel(symbol).decrefs;
  4959. { Remove label xxx (it will have a ref of zero due to the initial check }
  4960. StripLabelFast(hp4);
  4961. { remove jmp }
  4962. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  4963. asml.remove(hp2);
  4964. hp2.free;
  4965. { As before, now we can safely decrement it }
  4966. tasmlabel(symbol).decrefs;
  4967. { Remove label yyy (and the optional alignment) if its reference falls to zero }
  4968. if tasmlabel(symbol).getrefs = 0 then
  4969. StripLabelFast(hp3);
  4970. if Assigned(p) then
  4971. begin
  4972. UpdateUsedRegs(p);
  4973. result:=true;
  4974. end;
  4975. exit;
  4976. end;
  4977. end;
  4978. end;
  4979. end;
  4980. end;
  4981. {$endif i8086}
  4982. end;
  4983. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  4984. var
  4985. hp1,hp2: tai;
  4986. reg_and_hp1_is_instr: Boolean;
  4987. begin
  4988. result:=false;
  4989. reg_and_hp1_is_instr:=(taicpu(p).oper[1]^.typ = top_reg) and
  4990. GetNextInstruction(p,hp1) and
  4991. (hp1.typ = ait_instruction);
  4992. if reg_and_hp1_is_instr and
  4993. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  4994. GetNextInstruction(hp1,hp2) and
  4995. MatchInstruction(hp2,A_MOV,[]) and
  4996. (taicpu(hp2).oper[0]^.typ = top_reg) and
  4997. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  4998. ((taicpu(p).opsize in [S_BW,S_BL]) and (taicpu(hp2).opsize=S_B) or
  4999. (taicpu(p).opsize in [S_WL]) and (taicpu(hp2).opsize=S_W)) and
  5000. {$ifdef i386}
  5001. { not all registers have byte size sub registers on i386 }
  5002. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  5003. {$endif i386}
  5004. (((taicpu(hp1).ops=2) and
  5005. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  5006. ((taicpu(hp1).ops=1) and
  5007. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  5008. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  5009. begin
  5010. { change movsX/movzX reg/ref, reg2
  5011. add/sub/or/... reg3/$const, reg2
  5012. mov reg2 reg/ref
  5013. to add/sub/or/... reg3/$const, reg/ref }
  5014. { by example:
  5015. movswl %si,%eax movswl %si,%eax p
  5016. decl %eax addl %edx,%eax hp1
  5017. movw %ax,%si movw %ax,%si hp2
  5018. ->
  5019. movswl %si,%eax movswl %si,%eax p
  5020. decw %eax addw %edx,%eax hp1
  5021. movw %ax,%si movw %ax,%si hp2
  5022. }
  5023. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  5024. {
  5025. ->
  5026. movswl %si,%eax movswl %si,%eax p
  5027. decw %si addw %dx,%si hp1
  5028. movw %ax,%si movw %ax,%si hp2
  5029. }
  5030. case taicpu(hp1).ops of
  5031. 1:
  5032. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  5033. 2:
  5034. begin
  5035. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  5036. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  5037. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  5038. end;
  5039. else
  5040. internalerror(2008042701);
  5041. end;
  5042. {
  5043. ->
  5044. decw %si addw %dx,%si p
  5045. }
  5046. DebugMsg(SPeepholeOptimization + 'var3',p);
  5047. asml.remove(p);
  5048. asml.remove(hp2);
  5049. p.free;
  5050. hp2.free;
  5051. p:=hp1;
  5052. end
  5053. else if reg_and_hp1_is_instr and
  5054. (taicpu(hp1).opcode = A_MOV) and
  5055. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  5056. (MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^)
  5057. {$ifdef x86_64}
  5058. { check for implicit extension to 64 bit }
  5059. or
  5060. ((taicpu(p).opsize in [S_BL,S_WL]) and
  5061. (taicpu(hp1).opsize=S_Q) and
  5062. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg)
  5063. )
  5064. {$endif x86_64}
  5065. )
  5066. then
  5067. begin
  5068. { change
  5069. movx %reg1,%reg2
  5070. mov %reg2,%reg3
  5071. dealloc %reg2
  5072. into
  5073. movx %reg,%reg3
  5074. }
  5075. TransferUsedRegs(TmpUsedRegs);
  5076. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5077. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  5078. begin
  5079. DebugMsg(SPeepholeOptimization + 'MovxMov2Movx',p);
  5080. {$ifdef x86_64}
  5081. if (taicpu(p).opsize in [S_BL,S_WL]) and
  5082. (taicpu(hp1).opsize=S_Q) then
  5083. taicpu(p).loadreg(1,newreg(R_INTREGISTER,getsupreg(taicpu(hp1).oper[1]^.reg),R_SUBD))
  5084. else
  5085. {$endif x86_64}
  5086. taicpu(p).loadreg(1,taicpu(hp1).oper[1]^.reg);
  5087. asml.remove(hp1);
  5088. hp1.Free;
  5089. end;
  5090. end
  5091. else if reg_and_hp1_is_instr and
  5092. (taicpu(p).oper[0]^.typ = top_reg) and
  5093. (
  5094. (taicpu(hp1).opcode = A_SHL) or (taicpu(hp1).opcode = A_SAL)
  5095. ) and
  5096. (taicpu(hp1).oper[0]^.typ = top_const) and
  5097. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  5098. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  5099. { Minimum shift value allowed is the bit difference between the sizes }
  5100. (taicpu(hp1).oper[0]^.val >=
  5101. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  5102. 8 * (
  5103. tcgsize2size[reg_cgsize(taicpu(p).oper[1]^.reg)] -
  5104. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  5105. )
  5106. ) then
  5107. begin
  5108. { For:
  5109. movsx/movzx %reg1,%reg1 (same register, just different sizes)
  5110. shl/sal ##, %reg1
  5111. Remove the movsx/movzx instruction if the shift overwrites the
  5112. extended bits of the register (e.g. movslq %eax,%rax; shlq $32,%rax
  5113. }
  5114. DebugMsg(SPeepholeOptimization + 'MovxShl2Shl',p);
  5115. RemoveCurrentP(p, hp1);
  5116. Result := True;
  5117. Exit;
  5118. end
  5119. else if taicpu(p).opcode=A_MOVZX then
  5120. begin
  5121. { removes superfluous And's after movzx's }
  5122. if reg_and_hp1_is_instr and
  5123. (taicpu(hp1).opcode = A_AND) and
  5124. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5125. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  5126. begin
  5127. case taicpu(p).opsize Of
  5128. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  5129. if (taicpu(hp1).oper[0]^.val = $ff) then
  5130. begin
  5131. DebugMsg(SPeepholeOptimization + 'var4',p);
  5132. asml.remove(hp1);
  5133. hp1.free;
  5134. end;
  5135. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  5136. if (taicpu(hp1).oper[0]^.val = $ffff) then
  5137. begin
  5138. DebugMsg(SPeepholeOptimization + 'var5',p);
  5139. asml.remove(hp1);
  5140. hp1.free;
  5141. end;
  5142. {$ifdef x86_64}
  5143. S_LQ:
  5144. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  5145. begin
  5146. if (cs_asm_source in current_settings.globalswitches) then
  5147. asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
  5148. asml.remove(hp1);
  5149. hp1.Free;
  5150. end;
  5151. {$endif x86_64}
  5152. else
  5153. ;
  5154. end;
  5155. end;
  5156. { changes some movzx constructs to faster synonyms (all examples
  5157. are given with eax/ax, but are also valid for other registers)}
  5158. if MatchOpType(taicpu(p),top_reg,top_reg) then
  5159. begin
  5160. case taicpu(p).opsize of
  5161. { Technically, movzbw %al,%ax cannot be encoded in 32/64-bit mode
  5162. (the machine code is equivalent to movzbl %al,%eax), but the
  5163. code generator still generates that assembler instruction and
  5164. it is silently converted. This should probably be checked.
  5165. [Kit] }
  5166. S_BW:
  5167. begin
  5168. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  5169. (
  5170. not IsMOVZXAcceptable
  5171. { and $0xff,%ax has a smaller encoding but risks a partial write penalty }
  5172. or (
  5173. (cs_opt_size in current_settings.optimizerswitches) and
  5174. (taicpu(p).oper[1]^.reg = NR_AX)
  5175. )
  5176. ) then
  5177. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  5178. begin
  5179. DebugMsg(SPeepholeOptimization + 'var7',p);
  5180. taicpu(p).opcode := A_AND;
  5181. taicpu(p).changeopsize(S_W);
  5182. taicpu(p).loadConst(0,$ff);
  5183. Result := True;
  5184. end
  5185. else if not IsMOVZXAcceptable and
  5186. GetNextInstruction(p, hp1) and
  5187. (tai(hp1).typ = ait_instruction) and
  5188. (taicpu(hp1).opcode = A_AND) and
  5189. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5190. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  5191. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  5192. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  5193. begin
  5194. DebugMsg(SPeepholeOptimization + 'var8',p);
  5195. taicpu(p).opcode := A_MOV;
  5196. taicpu(p).changeopsize(S_W);
  5197. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  5198. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  5199. Result := True;
  5200. end;
  5201. end;
  5202. {$ifndef i8086} { movzbl %al,%eax cannot be encoded in 16-bit mode (the machine code is equivalent to movzbw %al,%ax }
  5203. S_BL:
  5204. begin
  5205. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  5206. (
  5207. not IsMOVZXAcceptable
  5208. { and $0xff,%eax has a smaller encoding but risks a partial write penalty }
  5209. or (
  5210. (cs_opt_size in current_settings.optimizerswitches) and
  5211. (taicpu(p).oper[1]^.reg = NR_EAX)
  5212. )
  5213. ) then
  5214. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  5215. begin
  5216. DebugMsg(SPeepholeOptimization + 'var9',p);
  5217. taicpu(p).opcode := A_AND;
  5218. taicpu(p).changeopsize(S_L);
  5219. taicpu(p).loadConst(0,$ff);
  5220. Result := True;
  5221. end
  5222. else if not IsMOVZXAcceptable and
  5223. GetNextInstruction(p, hp1) and
  5224. (tai(hp1).typ = ait_instruction) and
  5225. (taicpu(hp1).opcode = A_AND) and
  5226. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5227. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  5228. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  5229. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  5230. begin
  5231. DebugMsg(SPeepholeOptimization + 'var10',p);
  5232. taicpu(p).opcode := A_MOV;
  5233. taicpu(p).changeopsize(S_L);
  5234. { do not use R_SUBWHOLE
  5235. as movl %rdx,%eax
  5236. is invalid in assembler PM }
  5237. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  5238. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  5239. Result := True;
  5240. end;
  5241. end;
  5242. {$endif i8086}
  5243. S_WL:
  5244. if not IsMOVZXAcceptable then
  5245. begin
  5246. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) then
  5247. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  5248. begin
  5249. DebugMsg(SPeepholeOptimization + 'var11',p);
  5250. taicpu(p).opcode := A_AND;
  5251. taicpu(p).changeopsize(S_L);
  5252. taicpu(p).loadConst(0,$ffff);
  5253. Result := True;
  5254. end
  5255. else if GetNextInstruction(p, hp1) and
  5256. (tai(hp1).typ = ait_instruction) and
  5257. (taicpu(hp1).opcode = A_AND) and
  5258. (taicpu(hp1).oper[0]^.typ = top_const) and
  5259. (taicpu(hp1).oper[1]^.typ = top_reg) and
  5260. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  5261. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  5262. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  5263. begin
  5264. DebugMsg(SPeepholeOptimization + 'var12',p);
  5265. taicpu(p).opcode := A_MOV;
  5266. taicpu(p).changeopsize(S_L);
  5267. { do not use R_SUBWHOLE
  5268. as movl %rdx,%eax
  5269. is invalid in assembler PM }
  5270. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  5271. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  5272. Result := True;
  5273. end;
  5274. end;
  5275. else
  5276. InternalError(2017050705);
  5277. end;
  5278. end
  5279. else if not IsMOVZXAcceptable and (taicpu(p).oper[0]^.typ = top_ref) then
  5280. begin
  5281. if GetNextInstruction(p, hp1) and
  5282. (tai(hp1).typ = ait_instruction) and
  5283. (taicpu(hp1).opcode = A_AND) and
  5284. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5285. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  5286. begin
  5287. //taicpu(p).opcode := A_MOV;
  5288. case taicpu(p).opsize Of
  5289. S_BL:
  5290. begin
  5291. DebugMsg(SPeepholeOptimization + 'var13',p);
  5292. taicpu(hp1).changeopsize(S_L);
  5293. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  5294. end;
  5295. S_WL:
  5296. begin
  5297. DebugMsg(SPeepholeOptimization + 'var14',p);
  5298. taicpu(hp1).changeopsize(S_L);
  5299. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  5300. end;
  5301. S_BW:
  5302. begin
  5303. DebugMsg(SPeepholeOptimization + 'var15',p);
  5304. taicpu(hp1).changeopsize(S_W);
  5305. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  5306. end;
  5307. else
  5308. Internalerror(2017050704)
  5309. end;
  5310. Result := True;
  5311. end;
  5312. end;
  5313. end;
  5314. end;
  5315. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  5316. var
  5317. hp1 : tai;
  5318. MaskLength : Cardinal;
  5319. begin
  5320. Result:=false;
  5321. if GetNextInstruction(p, hp1) then
  5322. begin
  5323. if MatchOpType(taicpu(p),top_const,top_reg) and
  5324. MatchInstruction(hp1,A_AND,[]) and
  5325. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5326. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  5327. { the second register must contain the first one, so compare their subreg types }
  5328. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  5329. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  5330. { change
  5331. and const1, reg
  5332. and const2, reg
  5333. to
  5334. and (const1 and const2), reg
  5335. }
  5336. begin
  5337. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  5338. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  5339. asml.remove(p);
  5340. p.Free;
  5341. p:=hp1;
  5342. Result:=true;
  5343. exit;
  5344. end
  5345. else if MatchOpType(taicpu(p),top_const,top_reg) and
  5346. MatchInstruction(hp1,A_MOVZX,[]) and
  5347. (taicpu(hp1).oper[0]^.typ = top_reg) and
  5348. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  5349. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  5350. (((taicpu(p).opsize=S_W) and
  5351. (taicpu(hp1).opsize=S_BW)) or
  5352. ((taicpu(p).opsize=S_L) and
  5353. (taicpu(hp1).opsize in [S_WL,S_BL]))
  5354. {$ifdef x86_64}
  5355. or
  5356. ((taicpu(p).opsize=S_Q) and
  5357. (taicpu(hp1).opsize in [S_BQ,S_WQ]))
  5358. {$endif x86_64}
  5359. ) then
  5360. begin
  5361. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  5362. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  5363. ) or
  5364. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  5365. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  5366. then
  5367. begin
  5368. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  5369. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  5370. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  5371. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  5372. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  5373. }
  5374. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  5375. asml.remove(hp1);
  5376. hp1.free;
  5377. Exit;
  5378. end;
  5379. end
  5380. else if MatchOpType(taicpu(p),top_const,top_reg) and
  5381. MatchInstruction(hp1,A_SHL,[]) and
  5382. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5383. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  5384. begin
  5385. {$ifopt R+}
  5386. {$define RANGE_WAS_ON}
  5387. {$R-}
  5388. {$endif}
  5389. { get length of potential and mask }
  5390. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  5391. { really a mask? }
  5392. {$ifdef RANGE_WAS_ON}
  5393. {$R+}
  5394. {$endif}
  5395. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  5396. { unmasked part shifted out? }
  5397. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  5398. begin
  5399. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  5400. RemoveCurrentP(p, hp1);
  5401. Result:=true;
  5402. exit;
  5403. end;
  5404. end
  5405. else if MatchOpType(taicpu(p),top_const,top_reg) and
  5406. MatchInstruction(hp1,A_MOVSX{$ifdef x86_64},A_MOVSXD{$endif x86_64},[]) and
  5407. (taicpu(hp1).oper[0]^.typ = top_reg) and
  5408. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  5409. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  5410. (((taicpu(p).opsize=S_W) and
  5411. (taicpu(hp1).opsize=S_BW)) or
  5412. ((taicpu(p).opsize=S_L) and
  5413. (taicpu(hp1).opsize in [S_WL,S_BL]))
  5414. {$ifdef x86_64}
  5415. or
  5416. ((taicpu(p).opsize=S_Q) and
  5417. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_LQ]))
  5418. {$endif x86_64}
  5419. ) then
  5420. begin
  5421. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  5422. ((taicpu(p).oper[0]^.val and $7f)=taicpu(p).oper[0]^.val)
  5423. ) or
  5424. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  5425. ((taicpu(p).oper[0]^.val and $7fff)=taicpu(p).oper[0]^.val))
  5426. {$ifdef x86_64}
  5427. or
  5428. (((taicpu(hp1).opsize)=S_LQ) and
  5429. ((taicpu(p).oper[0]^.val and $7fffffff)=taicpu(p).oper[0]^.val)
  5430. )
  5431. {$endif x86_64}
  5432. then
  5433. begin
  5434. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  5435. asml.remove(hp1);
  5436. hp1.free;
  5437. Exit;
  5438. end;
  5439. end
  5440. else if (taicpu(p).oper[1]^.typ = top_reg) and
  5441. (hp1.typ = ait_instruction) and
  5442. (taicpu(hp1).is_jmp) and
  5443. (taicpu(hp1).opcode<>A_JMP) and
  5444. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  5445. begin
  5446. { change
  5447. and x, reg
  5448. jxx
  5449. to
  5450. test x, reg
  5451. jxx
  5452. if reg is deallocated before the
  5453. jump, but only if it's a conditional jump (PFV)
  5454. }
  5455. taicpu(p).opcode := A_TEST;
  5456. Exit;
  5457. end;
  5458. end;
  5459. { Lone AND tests }
  5460. if MatchOpType(taicpu(p),top_const,top_reg) then
  5461. begin
  5462. {
  5463. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  5464. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  5465. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  5466. }
  5467. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  5468. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  5469. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  5470. begin
  5471. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg);
  5472. if taicpu(p).opsize = S_L then
  5473. Include(OptsToCheck,aoc_MovAnd2Mov_3);
  5474. end;
  5475. end;
  5476. end;
  5477. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  5478. begin
  5479. Result:=false;
  5480. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  5481. MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  5482. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  5483. begin
  5484. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  5485. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  5486. taicpu(p).opcode:=A_ADD;
  5487. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  5488. result:=true;
  5489. end
  5490. else if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  5491. MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  5492. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  5493. begin
  5494. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  5495. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  5496. taicpu(p).opcode:=A_ADD;
  5497. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  5498. result:=true;
  5499. end;
  5500. end;
  5501. function TX86AsmOptimizer.OptPass2SUB(var p: tai): Boolean;
  5502. var
  5503. hp1: tai; NewRef: TReference;
  5504. begin
  5505. { Change:
  5506. subl/q $x,%reg1
  5507. movl/q %reg1,%reg2
  5508. To:
  5509. leal/q $-x(%reg1),%reg2
  5510. subl/q $x,%reg1
  5511. Breaks the dependency chain and potentially permits the removal of
  5512. a CMP instruction if one follows.
  5513. }
  5514. Result := False;
  5515. if not (cs_opt_size in current_settings.optimizerswitches) and
  5516. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  5517. MatchOpType(taicpu(p),top_const,top_reg) and
  5518. GetNextInstruction(p, hp1) and
  5519. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  5520. (taicpu(hp1).oper[1]^.typ = top_reg) and
  5521. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) then
  5522. begin
  5523. { Change the MOV instruction to a LEA instruction, and update the
  5524. first operand }
  5525. reference_reset(NewRef, 1, []);
  5526. NewRef.base := taicpu(p).oper[1]^.reg;
  5527. NewRef.scalefactor := 1;
  5528. NewRef.offset := -taicpu(p).oper[0]^.val;
  5529. taicpu(hp1).opcode := A_LEA;
  5530. taicpu(hp1).loadref(0, NewRef);
  5531. { Move what is now the LEA instruction to before the SUB instruction }
  5532. Asml.Remove(hp1);
  5533. Asml.InsertBefore(hp1, p);
  5534. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  5535. DebugMsg(SPeepholeOptimization + 'SubMov2LeaSub', p);
  5536. Result := True;
  5537. end;
  5538. end;
  5539. function TX86AsmOptimizer.SkipSimpleInstructions(var hp1 : tai) : Boolean;
  5540. begin
  5541. { we can skip all instructions not messing with the stack pointer }
  5542. while assigned(hp1) and {MatchInstruction(taicpu(hp1),[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  5543. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  5544. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  5545. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  5546. ({(taicpu(hp1).ops=0) or }
  5547. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  5548. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  5549. ) and }
  5550. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  5551. )
  5552. ) do
  5553. GetNextInstruction(hp1,hp1);
  5554. Result:=assigned(hp1);
  5555. end;
  5556. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  5557. var
  5558. hp1, hp2, hp3, hp4: tai;
  5559. begin
  5560. Result:=false;
  5561. { replace
  5562. leal(q) x(<stackpointer>),<stackpointer>
  5563. call procname
  5564. leal(q) -x(<stackpointer>),<stackpointer>
  5565. ret
  5566. by
  5567. jmp procname
  5568. but do it only on level 4 because it destroys stack back traces
  5569. }
  5570. if (cs_opt_level4 in current_settings.optimizerswitches) and
  5571. MatchOpType(taicpu(p),top_ref,top_reg) and
  5572. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  5573. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  5574. { the -8 or -24 are not required, but bail out early if possible,
  5575. higher values are unlikely }
  5576. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  5577. (taicpu(p).oper[0]^.ref^.offset=-24)) and
  5578. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  5579. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  5580. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  5581. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  5582. GetNextInstruction(p, hp1) and
  5583. { Take a copy of hp1 }
  5584. SetAndTest(hp1, hp4) and
  5585. { trick to skip label }
  5586. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  5587. SkipSimpleInstructions(hp1) and
  5588. MatchInstruction(hp1,A_CALL,[S_NO]) and
  5589. GetNextInstruction(hp1, hp2) and
  5590. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  5591. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  5592. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  5593. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  5594. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  5595. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  5596. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  5597. (taicpu(hp2).oper[0]^.ref^.segment=NR_NO) and
  5598. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  5599. GetNextInstruction(hp2, hp3) and
  5600. { trick to skip label }
  5601. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  5602. MatchInstruction(hp3,A_RET,[S_NO]) and
  5603. (taicpu(hp3).ops=0) then
  5604. begin
  5605. taicpu(hp1).opcode := A_JMP;
  5606. taicpu(hp1).is_jmp := true;
  5607. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  5608. RemoveCurrentP(p, hp4);
  5609. AsmL.Remove(hp2);
  5610. hp2.free;
  5611. AsmL.Remove(hp3);
  5612. hp3.free;
  5613. Result:=true;
  5614. end;
  5615. end;
  5616. function TX86AsmOptimizer.PostPeepholeOptPush(var p : tai) : Boolean;
  5617. var
  5618. hp1, hp2, hp3, hp4: tai;
  5619. begin
  5620. Result:=false;
  5621. {$ifdef x86_64}
  5622. { replace
  5623. push %rax
  5624. call procname
  5625. pop %rcx
  5626. ret
  5627. by
  5628. jmp procname
  5629. but do it only on level 4 because it destroys stack back traces
  5630. It depends on the fact, that the sequence push rax/pop rcx is used for stack alignment as rcx is volatile
  5631. for all supported calling conventions
  5632. }
  5633. if (cs_opt_level4 in current_settings.optimizerswitches) and
  5634. MatchOpType(taicpu(p),top_reg) and
  5635. (taicpu(p).oper[0]^.reg=NR_RAX) and
  5636. GetNextInstruction(p, hp1) and
  5637. { Take a copy of hp1 }
  5638. SetAndTest(hp1, hp4) and
  5639. { trick to skip label }
  5640. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  5641. SkipSimpleInstructions(hp1) and
  5642. MatchInstruction(hp1,A_CALL,[S_NO]) and
  5643. GetNextInstruction(hp1, hp2) and
  5644. MatchInstruction(hp2,A_POP,[taicpu(p).opsize]) and
  5645. MatchOpType(taicpu(hp2),top_reg) and
  5646. (taicpu(hp2).oper[0]^.reg=NR_RCX) and
  5647. GetNextInstruction(hp2, hp3) and
  5648. { trick to skip label }
  5649. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  5650. MatchInstruction(hp3,A_RET,[S_NO]) and
  5651. (taicpu(hp3).ops=0) then
  5652. begin
  5653. taicpu(hp1).opcode := A_JMP;
  5654. taicpu(hp1).is_jmp := true;
  5655. DebugMsg(SPeepholeOptimization + 'PushCallPushRet2Jmp done',p);
  5656. RemoveCurrentP(p, hp4);
  5657. AsmL.Remove(hp2);
  5658. hp2.free;
  5659. AsmL.Remove(hp3);
  5660. hp3.free;
  5661. Result:=true;
  5662. end;
  5663. {$endif x86_64}
  5664. end;
  5665. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  5666. var
  5667. Value, RegName: string;
  5668. begin
  5669. Result:=false;
  5670. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  5671. begin
  5672. case taicpu(p).oper[0]^.val of
  5673. 0:
  5674. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  5675. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  5676. begin
  5677. { change "mov $0,%reg" into "xor %reg,%reg" }
  5678. taicpu(p).opcode := A_XOR;
  5679. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  5680. Result := True;
  5681. end;
  5682. $1..$FFFFFFFF:
  5683. begin
  5684. { Code size reduction by J. Gareth "Kit" Moreton }
  5685. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  5686. case taicpu(p).opsize of
  5687. S_Q:
  5688. begin
  5689. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  5690. Value := debug_tostr(taicpu(p).oper[0]^.val);
  5691. { The actual optimization }
  5692. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5693. taicpu(p).changeopsize(S_L);
  5694. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  5695. Result := True;
  5696. end;
  5697. else
  5698. { Do nothing };
  5699. end;
  5700. end;
  5701. -1:
  5702. { Don't make this optimisation if the CPU flags are required, since OR scrambles them }
  5703. if (cs_opt_size in current_settings.optimizerswitches) and
  5704. (taicpu(p).opsize <> S_B) and
  5705. not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  5706. begin
  5707. { change "mov $-1,%reg" into "or $-1,%reg" }
  5708. { NOTES:
  5709. - No size saving is made when changing a Word-sized assignment unless the register is AX (smaller encoding)
  5710. - This operation creates a false dependency on the register, so only do it when optimising for size
  5711. - 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
  5712. }
  5713. taicpu(p).opcode := A_OR;
  5714. Result := True;
  5715. end;
  5716. end;
  5717. end;
  5718. end;
  5719. function TX86AsmOptimizer.PostPeepholeOptMOVSX(var p : tai) : boolean;
  5720. begin
  5721. Result := False;
  5722. if not MatchOpType(taicpu(p), top_reg, top_reg) then
  5723. Exit;
  5724. { Convert:
  5725. movswl %ax,%eax -> cwtl
  5726. movslq %eax,%rax -> cdqe
  5727. NOTE: Don't convert movswl %al,%ax to cbw, because cbw and cwde
  5728. refer to the same opcode and depends only on the assembler's
  5729. current operand-size attribute. [Kit]
  5730. }
  5731. with taicpu(p) do
  5732. case opsize of
  5733. S_WL:
  5734. if (oper[0]^.reg = NR_AX) and (oper[1]^.reg = NR_EAX) then
  5735. begin
  5736. DebugMsg(SPeepholeOptimization + 'Converted movswl %ax,%eax to cwtl', p);
  5737. opcode := A_CWDE;
  5738. clearop(0);
  5739. clearop(1);
  5740. ops := 0;
  5741. Result := True;
  5742. end;
  5743. {$ifdef x86_64}
  5744. S_LQ:
  5745. if (oper[0]^.reg = NR_EAX) and (oper[1]^.reg = NR_RAX) then
  5746. begin
  5747. DebugMsg(SPeepholeOptimization + 'Converted movslq %eax,%rax to cltq', p);
  5748. opcode := A_CDQE;
  5749. clearop(0);
  5750. clearop(1);
  5751. ops := 0;
  5752. Result := True;
  5753. end;
  5754. {$endif x86_64}
  5755. else
  5756. ;
  5757. end;
  5758. end;
  5759. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  5760. begin
  5761. Result:=false;
  5762. { change "cmp $0, %reg" to "test %reg, %reg" }
  5763. if MatchOpType(taicpu(p),top_const,top_reg) and
  5764. (taicpu(p).oper[0]^.val = 0) then
  5765. begin
  5766. taicpu(p).opcode := A_TEST;
  5767. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  5768. Result:=true;
  5769. end;
  5770. end;
  5771. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  5772. var
  5773. IsTestConstX : Boolean;
  5774. hp1,hp2 : tai;
  5775. begin
  5776. Result:=false;
  5777. { removes the line marked with (x) from the sequence
  5778. and/or/xor/add/sub/... $x, %y
  5779. test/or %y, %y | test $-1, %y (x)
  5780. j(n)z _Label
  5781. as the first instruction already adjusts the ZF
  5782. %y operand may also be a reference }
  5783. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  5784. MatchOperand(taicpu(p).oper[0]^,-1);
  5785. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  5786. GetLastInstruction(p, hp1) and
  5787. (tai(hp1).typ = ait_instruction) and
  5788. GetNextInstruction(p,hp2) and
  5789. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  5790. case taicpu(hp1).opcode Of
  5791. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  5792. begin
  5793. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  5794. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  5795. { and in case of carry for A(E)/B(E)/C/NC }
  5796. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  5797. ((taicpu(hp1).opcode <> A_ADD) and
  5798. (taicpu(hp1).opcode <> A_SUB))) then
  5799. begin
  5800. hp1 := tai(p.next);
  5801. asml.remove(p);
  5802. p.free;
  5803. p := tai(hp1);
  5804. Result:=true;
  5805. end;
  5806. end;
  5807. A_SHL, A_SAL, A_SHR, A_SAR:
  5808. begin
  5809. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  5810. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  5811. { therefore, it's only safe to do this optimization for }
  5812. { shifts by a (nonzero) constant }
  5813. (taicpu(hp1).oper[0]^.typ = top_const) and
  5814. (taicpu(hp1).oper[0]^.val <> 0) and
  5815. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  5816. { and in case of carry for A(E)/B(E)/C/NC }
  5817. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  5818. begin
  5819. hp1 := tai(p.next);
  5820. asml.remove(p);
  5821. p.free;
  5822. p := tai(hp1);
  5823. Result:=true;
  5824. end;
  5825. end;
  5826. A_DEC, A_INC, A_NEG:
  5827. begin
  5828. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  5829. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  5830. { and in case of carry for A(E)/B(E)/C/NC }
  5831. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  5832. begin
  5833. case taicpu(hp1).opcode of
  5834. A_DEC, A_INC:
  5835. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  5836. begin
  5837. case taicpu(hp1).opcode Of
  5838. A_DEC: taicpu(hp1).opcode := A_SUB;
  5839. A_INC: taicpu(hp1).opcode := A_ADD;
  5840. else
  5841. ;
  5842. end;
  5843. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  5844. taicpu(hp1).loadConst(0,1);
  5845. taicpu(hp1).ops:=2;
  5846. end;
  5847. else
  5848. ;
  5849. end;
  5850. hp1 := tai(p.next);
  5851. asml.remove(p);
  5852. p.free;
  5853. p := tai(hp1);
  5854. Result:=true;
  5855. end;
  5856. end
  5857. else
  5858. { change "test $-1,%reg" into "test %reg,%reg" }
  5859. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  5860. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  5861. end { case }
  5862. { change "test $-1,%reg" into "test %reg,%reg" }
  5863. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  5864. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  5865. end;
  5866. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  5867. var
  5868. hp1 : tai;
  5869. {$ifndef x86_64}
  5870. hp2 : taicpu;
  5871. {$endif x86_64}
  5872. begin
  5873. Result:=false;
  5874. {$ifndef x86_64}
  5875. { don't do this on modern CPUs, this really hurts them due to
  5876. broken call/ret pairing }
  5877. if (current_settings.optimizecputype < cpu_Pentium2) and
  5878. not(cs_create_pic in current_settings.moduleswitches) and
  5879. GetNextInstruction(p, hp1) and
  5880. MatchInstruction(hp1,A_JMP,[S_NO]) and
  5881. MatchOpType(taicpu(hp1),top_ref) and
  5882. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  5883. begin
  5884. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  5885. InsertLLItem(p.previous, p, hp2);
  5886. taicpu(p).opcode := A_JMP;
  5887. taicpu(p).is_jmp := true;
  5888. asml.remove(hp1);
  5889. hp1.free;
  5890. Result:=true;
  5891. end
  5892. else
  5893. {$endif x86_64}
  5894. { replace
  5895. call procname
  5896. ret
  5897. by
  5898. jmp procname
  5899. but do it only on level 4 because it destroys stack back traces
  5900. else if the subroutine is marked as no return, remove the ret
  5901. }
  5902. if ((cs_opt_level4 in current_settings.optimizerswitches) or
  5903. (po_noreturn in current_procinfo.procdef.procoptions)) and
  5904. GetNextInstruction(p, hp1) and
  5905. MatchInstruction(hp1,A_RET,[S_NO]) and
  5906. (taicpu(hp1).ops=0) then
  5907. begin
  5908. if (cs_opt_level4 in current_settings.optimizerswitches) and
  5909. { we might destroy stack alignment here if we do not do a call }
  5910. (target_info.stackalign<=sizeof(SizeUInt)) then
  5911. begin
  5912. taicpu(p).opcode := A_JMP;
  5913. taicpu(p).is_jmp := true;
  5914. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  5915. end
  5916. else
  5917. DebugMsg(SPeepholeOptimization + 'CallRet2Call done',p);
  5918. asml.remove(hp1);
  5919. hp1.free;
  5920. Result:=true;
  5921. end;
  5922. end;
  5923. {$ifdef x86_64}
  5924. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  5925. var
  5926. PreMessage: string;
  5927. begin
  5928. Result := False;
  5929. { Code size reduction by J. Gareth "Kit" Moreton }
  5930. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  5931. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  5932. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  5933. then
  5934. begin
  5935. { Has 64-bit register name and opcode suffix }
  5936. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  5937. { The actual optimization }
  5938. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5939. if taicpu(p).opsize = S_BQ then
  5940. taicpu(p).changeopsize(S_BL)
  5941. else
  5942. taicpu(p).changeopsize(S_WL);
  5943. DebugMsg(SPeepholeOptimization + PreMessage +
  5944. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  5945. end;
  5946. end;
  5947. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  5948. var
  5949. PreMessage, RegName: string;
  5950. begin
  5951. { Code size reduction by J. Gareth "Kit" Moreton }
  5952. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  5953. as this removes the REX prefix }
  5954. Result := False;
  5955. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  5956. Exit;
  5957. if taicpu(p).oper[0]^.typ <> top_reg then
  5958. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  5959. InternalError(2018011500);
  5960. case taicpu(p).opsize of
  5961. S_Q:
  5962. begin
  5963. 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
  5964. begin
  5965. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  5966. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  5967. { The actual optimization }
  5968. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  5969. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5970. taicpu(p).changeopsize(S_L);
  5971. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  5972. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  5973. end;
  5974. end;
  5975. else
  5976. ;
  5977. end;
  5978. end;
  5979. {$endif}
  5980. class procedure TX86AsmOptimizer.OptimizeRefs(var p: taicpu);
  5981. var
  5982. OperIdx: Integer;
  5983. begin
  5984. for OperIdx := 0 to p.ops - 1 do
  5985. if p.oper[OperIdx]^.typ = top_ref then
  5986. optimize_ref(p.oper[OperIdx]^.ref^, False);
  5987. end;
  5988. end.