aoptx86.pas 273 KB

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