aoptx86.pas 407 KB

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