aoptx86.pas 773 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031
  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. {$ifdef EXTDEBUG}
  21. {$define DEBUG_AOPTCPU}
  22. {$endif EXTDEBUG}
  23. interface
  24. uses
  25. globtype,cclasses,
  26. cpubase,
  27. aasmtai,aasmcpu,
  28. cgbase,cgutils,
  29. aopt,aoptobj;
  30. type
  31. TOptsToCheck = (
  32. aoc_MovAnd2Mov_3,
  33. aoc_ForceNewIteration,
  34. aoc_DoPass2JccOpts
  35. );
  36. TX86AsmOptimizer = class(TAsmOptimizer)
  37. { some optimizations are very expensive to check, so the
  38. pre opt pass can be used to set some flags, depending on the found
  39. instructions if it is worth to check a certain optimization }
  40. OptsToCheck : set of TOptsToCheck;
  41. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  42. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  43. class function RegReadByInstruction(reg : TRegister; hp : tai) : boolean; static;
  44. function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  45. function GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  46. { Identical to GetNextInstructionUsingReg, but returns a value indicating
  47. how many instructions away that Next is from Current is.
  48. 0 = failure, equivalent to False in GetNextInstructionUsingReg }
  49. function GetNextInstructionUsingRegCount(Current: tai; out Next: tai; reg: TRegister): Cardinal;
  50. { This version of GetNextInstructionUsingReg will look across conditional jumps,
  51. potentially allowing further optimisation (although it might need to know if
  52. it crossed a conditional jump. }
  53. function GetNextInstructionUsingRegCond(Current: tai; out Next: tai; reg: TRegister; var JumpTracking: TLinkedList; var CrossJump: Boolean): Boolean;
  54. {
  55. In comparison with GetNextInstructionUsingReg, GetNextInstructionUsingRegTrackingUse tracks
  56. the use of a register by allocs/dealloc, so it can ignore calls.
  57. In the following example, GetNextInstructionUsingReg will return the second movq,
  58. GetNextInstructionUsingRegTrackingUse won't.
  59. movq %rdi,%rax
  60. # Register rdi released
  61. # Register rdi allocated
  62. movq %rax,%rdi
  63. While in this example:
  64. movq %rdi,%rax
  65. call proc
  66. movq %rdi,%rax
  67. GetNextInstructionUsingRegTrackingUse will return the second instruction while GetNextInstructionUsingReg
  68. won't.
  69. }
  70. function GetNextInstructionUsingRegTrackingUse(Current: tai; out Next: tai; reg: TRegister): Boolean;
  71. function RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean; override;
  72. { returns true if any of the registers in ref are modified by any
  73. instruction between p1 and p2, or if those instructions write to the
  74. reference }
  75. function RefModifiedBetween(Ref: TReference; RefSize: ASizeInt; p1, p2: tai): Boolean;
  76. private
  77. function SkipSimpleInstructions(var hp1: tai): Boolean;
  78. protected
  79. class function IsMOVZXAcceptable: Boolean; static; inline;
  80. function CheckMovMov2MovMov2(const p, hp1: tai): Boolean;
  81. { Attempts to allocate a volatile integer register for use between p and hp,
  82. using AUsedRegs for the current register usage information. Returns NR_NO
  83. if no free register could be found }
  84. function GetIntRegisterBetween(RegSize: TSubRegister; var AUsedRegs: TAllUsedRegs; p, hp: tai; DontAlloc: Boolean = False): TRegister;
  85. { Attempts to allocate a volatile MM register for use between p and hp,
  86. using AUsedRegs for the current register usage information. Returns NR_NO
  87. if no free register could be found }
  88. function GetMMRegisterBetween(RegSize: TSubRegister; var AUsedRegs: TAllUsedRegs; p, hp: tai; DontAlloc: Boolean = False): TRegister;
  89. { checks whether loading a new value in reg1 overwrites the entirety of reg2 }
  90. class function Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean; static;
  91. { checks whether reading the value in reg1 depends on the value of reg2. This
  92. is very similar to SuperRegisterEquals, except it takes into account that
  93. R_SUBH and R_SUBL are independendent (e.g. reading from AL does not
  94. depend on the value in AH). }
  95. class function Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean; static;
  96. { Replaces all references to AOldReg in a memory reference to ANewReg }
  97. class function ReplaceRegisterInRef(var ref: TReference; const AOldReg, ANewReg: TRegister): Boolean; static;
  98. { Replaces all references to AOldReg in an operand to ANewReg }
  99. class function ReplaceRegisterInOper(const p: taicpu; const OperIdx: Integer; const AOldReg, ANewReg: TRegister): Boolean; static;
  100. { Replaces all references to AOldReg in an instruction to ANewReg,
  101. except where the register is being written }
  102. class function ReplaceRegisterInInstruction(const p: taicpu; const AOldReg, ANewReg: TRegister): Boolean; static;
  103. { Returns true if the reference only refers to ESP or EBP (or their 64-bit equivalents),
  104. or writes to a global symbol }
  105. class function IsRefSafe(const ref: PReference): Boolean; static;
  106. { Returns true if the given MOV instruction can be safely converted to CMOV }
  107. class function CanBeCMOV(p, cond_p: tai; var RefModified: Boolean) : boolean; static;
  108. { Like UpdateUsedRegs, but ignores deallocations }
  109. class procedure UpdateIntRegsNoDealloc(var AUsedRegs: TAllUsedRegs; p: Tai); static;
  110. { Returns true if the given logic instruction can be converted into a BTx instruction (BT not included) }
  111. class function IsBTXAcceptable(p : tai) : boolean; static;
  112. { Converts the LEA instruction to ADD/INC/SUB/DEC. Returns True if the
  113. conversion was successful }
  114. function ConvertLEA(const p : taicpu): Boolean;
  115. function DeepMOVOpt(const p_mov: taicpu; const hp: taicpu): Boolean;
  116. function FuncMov2Func(var p: tai; const hp1: tai): Boolean;
  117. {$ifdef x86_64}
  118. { If a "mov %reg1d,%reg2d; and %reg1d,%reg1d" is found, we can possibly
  119. replace %reg2q with %reg1q in later instructions }
  120. function DoZeroUpper32Opt(var mov_p: tai; var and_p: tai): Boolean;
  121. {$endif x86_64}
  122. procedure DebugMsg(const s : string; p : tai);inline;
  123. class function IsExitCode(p : tai) : boolean; static;
  124. class function isFoldableArithOp(hp1 : taicpu; reg : tregister) : boolean; static;
  125. class function IsShrMovZFoldable(shr_size, movz_size: topsize; Shift: TCGInt): Boolean; static;
  126. procedure RemoveLastDeallocForFuncRes(p : tai);
  127. function DoArithCombineOpt(var p : tai) : Boolean;
  128. function DoMovCmpMemOpt(var p : tai; const hp1: tai) : Boolean;
  129. function DoSETccLblRETOpt(var p: tai; const hp_label: tai_label) : Boolean;
  130. function HandleSHRMerge(var p: tai; const PostPeephole: Boolean): Boolean;
  131. function PrePeepholeOptSxx(var p : tai) : boolean;
  132. function PrePeepholeOptIMUL(var p : tai) : boolean;
  133. function PrePeepholeOptAND(var p : tai) : boolean;
  134. function OptPass1Test(var p: tai): boolean;
  135. function OptPass1Add(var p: tai): boolean;
  136. function OptPass1AND(var p : tai) : boolean;
  137. function OptPass1CMOVcc(var p: tai): Boolean;
  138. function OptPass1_V_MOVAP(var p : tai) : boolean;
  139. function OptPass1VOP(var p : tai) : boolean;
  140. function OptPass1MOV(var p : tai) : boolean;
  141. function OptPass1MOVD(var p : tai) : boolean;
  142. function OptPass1Movx(var p : tai) : boolean;
  143. function OptPass1MOVXX(var p : tai) : boolean;
  144. function OptPass1OP(var p : tai) : boolean;
  145. function OptPass1LEA(var p : tai) : boolean;
  146. function OptPass1Sub(var p : tai) : boolean;
  147. function OptPass1SHLSAL(var p : tai) : boolean;
  148. function OptPass1SHR(var p : tai) : boolean;
  149. function OptPass1FSTP(var p : tai) : boolean;
  150. function OptPass1FLD(var p : tai) : boolean;
  151. function OptPass1Cmp(var p : tai) : boolean;
  152. function OptPass1PXor(var p : tai) : boolean;
  153. function OptPass1VPXor(var p: tai): boolean;
  154. function OptPass1Imul(var p : tai) : boolean;
  155. function OptPass1Jcc(var p : tai) : boolean;
  156. function OptPass1SHXX(var p: tai): boolean;
  157. function OptPass1VMOVDQ(var p: tai): Boolean;
  158. function OptPass1_V_Cvtss2sd(var p: tai): boolean;
  159. function OptPass1STCCLC(var p: tai): Boolean;
  160. function OptPass2STCCLC(var p: tai): Boolean;
  161. function OptPass2CMOVcc(var p: tai): Boolean;
  162. function OptPass2Movx(var p : tai): Boolean;
  163. function OptPass2MOV(var p : tai) : boolean;
  164. function OptPass2Imul(var p : tai) : boolean;
  165. function OptPass2Jmp(var p : tai) : boolean;
  166. function OptPass2Jcc(var p : tai) : boolean;
  167. function OptPass2Lea(var p: tai): Boolean;
  168. function OptPass2SUB(var p: tai): Boolean;
  169. function OptPass2ADD(var p : tai): Boolean;
  170. function OptPass2SETcc(var p : tai) : boolean;
  171. function OptPass2Cmp(var p: tai): Boolean;
  172. function OptPass2Test(var p: tai): Boolean;
  173. function CheckMemoryWrite(var first_mov, second_mov: taicpu): Boolean;
  174. function PostPeepholeOptMov(var p : tai) : Boolean;
  175. function PostPeepholeOptMovzx(var p : tai) : Boolean;
  176. function PostPeepholeOptXor(var p : tai) : Boolean;
  177. function PostPeepholeOptAnd(var p : tai) : boolean;
  178. function PostPeepholeOptMOVSX(var p : tai) : boolean;
  179. function PostPeepholeOptCmp(var p : tai) : Boolean;
  180. function PostPeepholeOptTestOr(var p : tai) : Boolean;
  181. function PostPeepholeOptCall(var p : tai) : Boolean;
  182. function PostPeepholeOptLea(var p : tai) : Boolean;
  183. function PostPeepholeOptPush(var p: tai): Boolean;
  184. function PostPeepholeOptShr(var p : tai) : boolean;
  185. function PostPeepholeOptADDSUB(var p : tai) : Boolean;
  186. function PostPeepholeOptVPXOR(var p: tai): Boolean;
  187. function PostPeepholeOptRET(var p: tai): Boolean;
  188. procedure ConvertJumpToRET(const p: tai; const ret_p: tai);
  189. function CheckJumpMovTransferOpt(var p: tai; hp1: tai; LoopCount: Integer; out Count: Integer): Boolean;
  190. function TrySwapMovOp(var p, hp1: tai): Boolean;
  191. function TrySwapMovCmp(var p, hp1: tai): Boolean;
  192. function TryCmpCMovOpts(var p, hp1: tai) : Boolean;
  193. function TryJccStcClcOpt(var p, hp1: tai): Boolean;
  194. { Processor-dependent reference optimisation }
  195. class procedure OptimizeRefs(var p: taicpu); static;
  196. end;
  197. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  198. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  199. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  200. function MatchInstruction(const instr: tai; const ops: array of TAsmOp; const opsize: topsizes): boolean;
  201. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  202. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  203. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  204. {$if max_operands>2}
  205. function MatchOperand(const oper1: TOper; const oper2: TOper; const oper3: TOper): boolean;
  206. {$endif max_operands>2}
  207. function RefsEqual(const r1, r2: treference): boolean;
  208. { Like RefsEqual, but doesn't compare the offsets }
  209. function RefsAlmostEqual(const r1, r2: treference): boolean;
  210. { Note that Result is set to True if the references COULD overlap but the
  211. compiler cannot be sure (e.g. "(%reg1)" and "4(%reg2)" with a range of 4
  212. might still overlap because %reg2 could be equal to %reg1-4 }
  213. function RefsMightOverlap(const r1, r2: treference; const Range: asizeint): boolean;
  214. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  215. { returns true, if ref is a reference using only the registers passed as base and index
  216. and having an offset }
  217. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  218. implementation
  219. uses
  220. cutils,verbose,
  221. systems,
  222. globals,
  223. cpuinfo,
  224. procinfo,
  225. paramgr,
  226. aasmbase,
  227. aoptbase,aoptutils,
  228. symconst,symsym,
  229. cgx86,
  230. itcpugas;
  231. {$ifndef 8086}
  232. const
  233. MAX_CMOV_INSTRUCTIONS = 4;
  234. MAX_CMOV_REGISTERS = 8;
  235. type
  236. TCMovTrackingState = (tsInvalid, tsSimple, tsDetour, tsBranching,
  237. tsDouble, tsDoubleBranchSame, tsDoubleBranchDifferent, tsDoubleSecondBranching,
  238. tsProcessed);
  239. { For OptPass2Jcc }
  240. TCMOVTracking = object
  241. private
  242. CMOVScore, ConstCount: LongInt;
  243. RegWrites: array[0..MAX_CMOV_INSTRUCTIONS*2 - 1] of TRegister;
  244. ConstRegs: array[0..MAX_CMOV_REGISTERS - 1] of TRegister;
  245. ConstVals: array[0..MAX_CMOV_REGISTERS - 1] of TCGInt;
  246. ConstSizes: array[0..MAX_CMOV_REGISTERS - 1] of TSubRegister; { May not match ConstRegs if one is shared over multiple CMOVs. }
  247. ConstMovs: array[0..MAX_CMOV_REGISTERS - 1] of tai; { Location of initialisation instruction }
  248. ConstWriteSizes: array[0..first_int_imreg - 1] of TSubRegister; { Largest size of register written. }
  249. fOptimizer: TX86AsmOptimizer;
  250. fLabel: TAsmSymbol;
  251. fInsertionPoint,
  252. fCondition,
  253. fInitialJump,
  254. fFirstMovBlock,
  255. fFirstMovBlockStop,
  256. fSecondJump,
  257. fThirdJump,
  258. fSecondMovBlock,
  259. fSecondMovBlockStop,
  260. fMidLabel,
  261. fEndLabel,
  262. fAllocationRange: tai;
  263. fState: TCMovTrackingState;
  264. function TryCMOVConst(p, start, stop: tai; var Count: LongInt): Boolean;
  265. function InitialiseBlock(BlockStart, OneBeforeBlock: tai; out BlockStop: tai; out EndJump: tai): Boolean;
  266. function AnalyseMOVBlock(BlockStart, BlockStop, SearchStart: tai): LongInt;
  267. public
  268. RegisterTracking: TAllUsedRegs;
  269. constructor Init(Optimizer: TX86AsmOptimizer; var p_initialjump, p_initialmov: tai; var AFirstLabel: TAsmLabel);
  270. destructor Done;
  271. procedure Process(out new_p: tai);
  272. property State: TCMovTrackingState read fState;
  273. end;
  274. PCMOVTracking = ^TCMOVTracking;
  275. {$endif 8086}
  276. {$ifdef DEBUG_AOPTCPU}
  277. const
  278. SPeepholeOptimization: shortstring = 'Peephole Optimization: ';
  279. {$else DEBUG_AOPTCPU}
  280. { Empty strings help the optimizer to remove string concatenations that won't
  281. ever appear to the user on release builds. [Kit] }
  282. const
  283. SPeepholeOptimization = '';
  284. {$endif DEBUG_AOPTCPU}
  285. LIST_STEP_SIZE = 4;
  286. type
  287. TJumpTrackingItem = class(TLinkedListItem)
  288. private
  289. FSymbol: TAsmSymbol;
  290. FRefs: LongInt;
  291. public
  292. constructor Create(ASymbol: TAsmSymbol);
  293. procedure IncRefs; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  294. property Symbol: TAsmSymbol read FSymbol;
  295. property Refs: LongInt read FRefs;
  296. end;
  297. constructor TJumpTrackingItem.Create(ASymbol: TAsmSymbol);
  298. begin
  299. inherited Create;
  300. FSymbol := ASymbol;
  301. FRefs := 0;
  302. end;
  303. procedure TJumpTrackingItem.IncRefs; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  304. begin
  305. Inc(FRefs);
  306. end;
  307. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  308. begin
  309. result :=
  310. (instr.typ = ait_instruction) and
  311. (taicpu(instr).opcode = op) and
  312. ((opsize = []) or (taicpu(instr).opsize in opsize));
  313. end;
  314. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  315. begin
  316. result :=
  317. (instr.typ = ait_instruction) and
  318. ((taicpu(instr).opcode = op1) or
  319. (taicpu(instr).opcode = op2)
  320. ) and
  321. ((opsize = []) or (taicpu(instr).opsize in opsize));
  322. end;
  323. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  324. begin
  325. result :=
  326. (instr.typ = ait_instruction) and
  327. ((taicpu(instr).opcode = op1) or
  328. (taicpu(instr).opcode = op2) or
  329. (taicpu(instr).opcode = op3)
  330. ) and
  331. ((opsize = []) or (taicpu(instr).opsize in opsize));
  332. end;
  333. function MatchInstruction(const instr : tai;const ops : array of TAsmOp;
  334. const opsize : topsizes) : boolean;
  335. var
  336. op : TAsmOp;
  337. begin
  338. result:=false;
  339. if (instr.typ <> ait_instruction) or
  340. ((opsize <> []) and not(taicpu(instr).opsize in opsize)) then
  341. exit;
  342. for op in ops do
  343. begin
  344. if taicpu(instr).opcode = op then
  345. begin
  346. result:=true;
  347. exit;
  348. end;
  349. end;
  350. end;
  351. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  352. begin
  353. result := (oper.typ = top_reg) and (oper.reg = reg);
  354. end;
  355. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  356. begin
  357. result := (oper.typ = top_const) and (oper.val = a);
  358. end;
  359. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  360. begin
  361. result := oper1.typ = oper2.typ;
  362. if result then
  363. case oper1.typ of
  364. top_const:
  365. Result:=oper1.val = oper2.val;
  366. top_reg:
  367. Result:=oper1.reg = oper2.reg;
  368. top_ref:
  369. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  370. else
  371. internalerror(2013102801);
  372. end
  373. end;
  374. function MatchOperand(const oper1: TOper; const oper2: TOper; const oper3: TOper): boolean;
  375. begin
  376. result := (oper1.typ = oper2.typ) and (oper1.typ = oper3.typ);
  377. if result then
  378. case oper1.typ of
  379. top_const:
  380. Result:=(oper1.val = oper2.val) and (oper1.val = oper3.val);
  381. top_reg:
  382. Result:=(oper1.reg = oper2.reg) and (oper1.reg = oper3.reg);
  383. top_ref:
  384. Result:=RefsEqual(oper1.ref^, oper2.ref^) and RefsEqual(oper1.ref^, oper3.ref^);
  385. else
  386. internalerror(2020052401);
  387. end
  388. end;
  389. function RefsEqual(const r1, r2: treference): boolean;
  390. begin
  391. RefsEqual :=
  392. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  393. (r1.relsymbol = r2.relsymbol) and
  394. (r1.segment = r2.segment) and (r1.base = r2.base) and
  395. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  396. (r1.offset = r2.offset) and
  397. (r1.volatility + r2.volatility = []);
  398. end;
  399. function RefsAlmostEqual(const r1, r2: treference): boolean;
  400. begin
  401. RefsAlmostEqual :=
  402. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  403. (r1.relsymbol = r2.relsymbol) and
  404. (r1.segment = r2.segment) and (r1.base = r2.base) and
  405. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  406. { Don't compare the offsets }
  407. (r1.volatility + r2.volatility = []);
  408. end;
  409. function RefsMightOverlap(const r1, r2: treference; const Range: asizeint): boolean;
  410. begin
  411. if (r1.symbol<>r2.symbol) then
  412. { If the index registers are different, there's a chance one could
  413. be set so it equals the other symbol }
  414. Exit((r1.index<>r2.index) or (r1.scalefactor<>r2.scalefactor));
  415. if (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  416. (r1.relsymbol = r2.relsymbol) and
  417. (r1.segment = r2.segment) and (r1.base = r2.base) and
  418. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  419. (r1.volatility + r2.volatility = []) then
  420. { In this case, it all depends on the offsets }
  421. Exit(abs(r1.offset - r2.offset) < Range);
  422. { There's a chance things MIGHT overlap, so take no chances }
  423. Result := True;
  424. end;
  425. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  426. begin
  427. Result:=(ref.offset=0) and
  428. (ref.scalefactor in [0,1]) and
  429. (ref.segment=NR_NO) and
  430. (ref.symbol=nil) and
  431. (ref.relsymbol=nil) and
  432. ((base=NR_INVALID) or
  433. (ref.base=base)) and
  434. ((index=NR_INVALID) or
  435. (ref.index=index)) and
  436. (ref.volatility=[]);
  437. end;
  438. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  439. begin
  440. Result:=(ref.scalefactor in [0,1]) and
  441. (ref.segment=NR_NO) and
  442. (ref.symbol=nil) and
  443. (ref.relsymbol=nil) and
  444. ((base=NR_INVALID) or
  445. (ref.base=base)) and
  446. ((index=NR_INVALID) or
  447. (ref.index=index)) and
  448. (ref.volatility=[]);
  449. end;
  450. function InstrReadsFlags(p: tai): boolean;
  451. begin
  452. InstrReadsFlags := true;
  453. case p.typ of
  454. ait_instruction:
  455. if InsProp[taicpu(p).opcode].Ch*
  456. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  457. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  458. Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc,Ch_All]<>[] then
  459. exit;
  460. ait_label:
  461. exit;
  462. else
  463. ;
  464. end;
  465. InstrReadsFlags := false;
  466. end;
  467. function TX86AsmOptimizer.GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  468. begin
  469. Next:=Current;
  470. repeat
  471. Result:=GetNextInstruction(Next,Next);
  472. until not (Result) or
  473. not(cs_opt_level3 in current_settings.optimizerswitches) or
  474. (Next.typ<>ait_instruction) or
  475. RegInInstruction(reg,Next) or
  476. is_calljmp(taicpu(Next).opcode);
  477. end;
  478. function TX86AsmOptimizer.GetNextInstructionUsingRegCount(Current: tai; out Next: tai; reg: TRegister): Cardinal;
  479. var
  480. GetNextResult: Boolean;
  481. begin
  482. Result:=0;
  483. Next:=Current;
  484. repeat
  485. GetNextResult := GetNextInstruction(Next,Next);
  486. if GetNextResult then
  487. Inc(Result)
  488. else
  489. { Must return zero upon hitting the end of the linked list without a match }
  490. Result := 0;
  491. until not (GetNextResult) or
  492. not(cs_opt_level3 in current_settings.optimizerswitches) or
  493. (Next.typ<>ait_instruction) or
  494. RegInInstruction(reg,Next) or
  495. is_calljmp(taicpu(Next).opcode);
  496. end;
  497. function TX86AsmOptimizer.GetNextInstructionUsingRegCond(Current: tai; out Next: tai; reg: TRegister; var JumpTracking: TLinkedList; var CrossJump: Boolean): Boolean;
  498. procedure TrackJump(Symbol: TAsmSymbol);
  499. var
  500. Search: TJumpTrackingItem;
  501. begin
  502. { See if an entry already exists in our jump tracking list
  503. (faster to search backwards due to the higher chance of
  504. matching destinations) }
  505. Search := TJumpTrackingItem(JumpTracking.Last);
  506. while Assigned(Search) do
  507. begin
  508. if Search.Symbol = Symbol then
  509. begin
  510. { Found it - remove it so it can be pushed to the front }
  511. JumpTracking.Remove(Search);
  512. Break;
  513. end;
  514. Search := TJumpTrackingItem(Search.Previous);
  515. end;
  516. if not Assigned(Search) then
  517. Search := TJumpTrackingItem.Create(JumpTargetOp(taicpu(Next))^.ref^.symbol);
  518. JumpTracking.Concat(Search);
  519. Search.IncRefs;
  520. end;
  521. function LabelAccountedFor(Symbol: TAsmSymbol): Boolean;
  522. var
  523. Search: TJumpTrackingItem;
  524. begin
  525. Result := False;
  526. { See if this label appears in the tracking list }
  527. Search := TJumpTrackingItem(JumpTracking.Last);
  528. while Assigned(Search) do
  529. begin
  530. if Search.Symbol = Symbol then
  531. begin
  532. { Found it - let's see what we can discover }
  533. if Search.Symbol.getrefs = Search.Refs then
  534. begin
  535. { Success - all the references are accounted for }
  536. JumpTracking.Remove(Search);
  537. Search.Free;
  538. { It is logically impossible for CrossJump to be false here
  539. because we must have run into a conditional jump for
  540. this label at some point }
  541. if not CrossJump then
  542. InternalError(2022041710);
  543. if JumpTracking.First = nil then
  544. { Tracking list is now empty - no more cross jumps }
  545. CrossJump := False;
  546. Result := True;
  547. Exit;
  548. end;
  549. { If the references don't match, it's possible to enter
  550. this label through other means, so drop out }
  551. Exit;
  552. end;
  553. Search := TJumpTrackingItem(Search.Previous);
  554. end;
  555. end;
  556. var
  557. Next_Label: tai;
  558. begin
  559. { Note, CrossJump keeps its input value if a conditional jump is not found - it doesn't get set to False }
  560. Next := Current;
  561. repeat
  562. Result := GetNextInstruction(Next,Next);
  563. if not Result then
  564. Break;
  565. if (Next.typ=ait_instruction) and is_calljmp(taicpu(Next).opcode) then
  566. if is_calljmpuncondret(taicpu(Next).opcode) then
  567. begin
  568. if (taicpu(Next).opcode = A_JMP) and
  569. { Remove dead code now to save time }
  570. RemoveDeadCodeAfterJump(taicpu(Next)) then
  571. { A jump was removed, but not the current instruction, and
  572. Result doesn't necessarily translate into an optimisation
  573. routine's Result, so use the "Force New Iteration" flag so
  574. mark a new pass }
  575. Include(OptsToCheck, aoc_ForceNewIteration);
  576. if not Assigned(JumpTracking) then
  577. begin
  578. { Cross-label optimisations often causes other optimisations
  579. to perform worse because they're not given the chance to
  580. optimise locally. In this case, don't do the cross-label
  581. optimisations yet, but flag them as a potential possibility
  582. for the next iteration of Pass 1 }
  583. if not NotFirstIteration then
  584. Include(OptsToCheck, aoc_ForceNewIteration);
  585. end
  586. else if IsJumpToLabel(taicpu(Next)) and
  587. GetNextInstruction(Next, Next_Label) then
  588. begin
  589. { If we have JMP .lbl, and the label after it has all of its
  590. references tracked, then this is probably an if-else style of
  591. block and we can keep tracking. If the label for this jump
  592. then appears later and is fully tracked, then it's the end
  593. of the if-else blocks and the code paths converge (thus
  594. marking the end of the cross-jump) }
  595. if (Next_Label.typ = ait_label) then
  596. begin
  597. if LabelAccountedFor(tai_label(Next_Label).labsym) then
  598. begin
  599. TrackJump(JumpTargetOp(taicpu(Next))^.ref^.symbol);
  600. Next := Next_Label;
  601. { CrossJump gets set to false by LabelAccountedFor if the
  602. list is completely emptied (as it indicates that all
  603. code paths have converged). We could avoid this nuance
  604. by moving the TrackJump call to before the
  605. LabelAccountedFor call, but this is slower in situations
  606. where LabelAccountedFor would return False due to the
  607. creation of a new object that is not used and destroyed
  608. soon after. }
  609. CrossJump := True;
  610. Continue;
  611. end;
  612. end
  613. else if (Next_Label.typ <> ait_marker) then
  614. { We just did a RemoveDeadCodeAfterJump, so either we find
  615. a label, the end of the procedure or some kind of marker}
  616. InternalError(2022041720);
  617. end;
  618. Result := False;
  619. Exit;
  620. end
  621. else
  622. begin
  623. if not Assigned(JumpTracking) then
  624. begin
  625. { Cross-label optimisations often causes other optimisations
  626. to perform worse because they're not given the chance to
  627. optimise locally. In this case, don't do the cross-label
  628. optimisations yet, but flag them as a potential possibility
  629. for the next iteration of Pass 1 }
  630. if not NotFirstIteration then
  631. Include(OptsToCheck, aoc_ForceNewIteration);
  632. end
  633. else if IsJumpToLabel(taicpu(Next)) then
  634. TrackJump(JumpTargetOp(taicpu(Next))^.ref^.symbol)
  635. else
  636. { Conditional jumps should always be a jump to label }
  637. InternalError(2022041701);
  638. CrossJump := True;
  639. Continue;
  640. end;
  641. if Next.typ = ait_label then
  642. begin
  643. if not Assigned(JumpTracking) then
  644. begin
  645. { Cross-label optimisations often causes other optimisations
  646. to perform worse because they're not given the chance to
  647. optimise locally. In this case, don't do the cross-label
  648. optimisations yet, but flag them as a potential possibility
  649. for the next iteration of Pass 1 }
  650. if not NotFirstIteration then
  651. Include(OptsToCheck, aoc_ForceNewIteration);
  652. end
  653. else if LabelAccountedFor(tai_label(Next).labsym) then
  654. Continue;
  655. { If we reach here, we're at a label that hasn't been seen before
  656. (or JumpTracking was nil) }
  657. Break;
  658. end;
  659. until not Result or
  660. not (cs_opt_level3 in current_settings.optimizerswitches) or
  661. not (Next.typ in [ait_label, ait_instruction]) or
  662. RegInInstruction(reg,Next);
  663. end;
  664. function TX86AsmOptimizer.GetNextInstructionUsingRegTrackingUse(Current: tai; out Next: tai; reg: TRegister): Boolean;
  665. begin
  666. if not(cs_opt_level3 in current_settings.optimizerswitches) then
  667. begin
  668. Result:=GetNextInstruction(Current,Next);
  669. exit;
  670. end;
  671. Next:=tai(Current.Next);
  672. Result:=false;
  673. while assigned(Next) do
  674. begin
  675. if ((Next.typ=ait_instruction) and is_calljmp(taicpu(Next).opcode) and not(taicpu(Next).opcode=A_CALL)) or
  676. ((Next.typ=ait_regalloc) and (getsupreg(tai_regalloc(Next).reg)=getsupreg(reg))) or
  677. ((Next.typ=ait_label) and not(labelCanBeSkipped(Tai_Label(Next)))) then
  678. exit
  679. else if (Next.typ=ait_instruction) and RegInInstruction(reg,Next) and not(taicpu(Next).opcode=A_CALL) then
  680. begin
  681. Result:=true;
  682. exit;
  683. end;
  684. Next:=tai(Next.Next);
  685. end;
  686. end;
  687. function TX86AsmOptimizer.InstructionLoadsFromReg(const reg: TRegister;const hp: tai): boolean;
  688. begin
  689. Result:=RegReadByInstruction(reg,hp);
  690. end;
  691. class function TX86AsmOptimizer.RegReadByInstruction(reg: TRegister; hp: tai): boolean;
  692. var
  693. p: taicpu;
  694. opcount: longint;
  695. begin
  696. RegReadByInstruction := false;
  697. if hp.typ <> ait_instruction then
  698. exit;
  699. p := taicpu(hp);
  700. case p.opcode of
  701. A_CALL:
  702. regreadbyinstruction := true;
  703. A_IMUL:
  704. case p.ops of
  705. 1:
  706. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  707. (
  708. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  709. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  710. );
  711. 2,3:
  712. regReadByInstruction :=
  713. reginop(reg,p.oper[0]^) or
  714. reginop(reg,p.oper[1]^);
  715. else
  716. InternalError(2019112801);
  717. end;
  718. A_MUL:
  719. begin
  720. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  721. (
  722. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  723. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  724. );
  725. end;
  726. A_IDIV,A_DIV:
  727. begin
  728. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  729. (
  730. (getregtype(reg)=R_INTREGISTER) and
  731. (
  732. (getsupreg(reg)=RS_EAX) or ((getsupreg(reg)=RS_EDX) and (p.opsize<>S_B))
  733. )
  734. );
  735. end;
  736. else
  737. begin
  738. if (p.opcode=A_LEA) and is_segment_reg(reg) then
  739. begin
  740. RegReadByInstruction := false;
  741. exit;
  742. end;
  743. for opcount := 0 to p.ops-1 do
  744. if (p.oper[opCount]^.typ = top_ref) and
  745. RegInRef(reg,p.oper[opcount]^.ref^) then
  746. begin
  747. RegReadByInstruction := true;
  748. exit
  749. end;
  750. { special handling for SSE MOVSD }
  751. if (p.opcode=A_MOVSD) and (p.ops>0) then
  752. begin
  753. if p.ops<>2 then
  754. internalerror(2017042702);
  755. regReadByInstruction := reginop(reg,p.oper[0]^) or
  756. (
  757. (p.oper[1]^.typ=top_reg) and (p.oper[0]^.typ=top_reg) and reginop(reg, p.oper[1]^)
  758. );
  759. exit;
  760. end;
  761. with insprop[p.opcode] do
  762. begin
  763. case getregtype(reg) of
  764. R_INTREGISTER:
  765. begin
  766. case getsupreg(reg) of
  767. RS_EAX:
  768. if [Ch_REAX,Ch_RWEAX,Ch_MEAX,Ch_WRAX,Ch_RWRAX,Ch_MRAX]*Ch<>[] then
  769. begin
  770. RegReadByInstruction := true;
  771. exit
  772. end;
  773. RS_ECX:
  774. if [Ch_RECX,Ch_RWECX,Ch_MECX,Ch_WRCX,Ch_RWRCX,Ch_MRCX]*Ch<>[] then
  775. begin
  776. RegReadByInstruction := true;
  777. exit
  778. end;
  779. RS_EDX:
  780. if [Ch_REDX,Ch_RWEDX,Ch_MEDX,Ch_WRDX,Ch_RWRDX,Ch_MRDX]*Ch<>[] then
  781. begin
  782. RegReadByInstruction := true;
  783. exit
  784. end;
  785. RS_EBX:
  786. if [Ch_REBX,Ch_RWEBX,Ch_MEBX,Ch_WRBX,Ch_RWRBX,Ch_MRBX]*Ch<>[] then
  787. begin
  788. RegReadByInstruction := true;
  789. exit
  790. end;
  791. RS_ESP:
  792. if [Ch_RESP,Ch_RWESP,Ch_MESP,Ch_WRSP,Ch_RWRSP,Ch_MRSP]*Ch<>[] then
  793. begin
  794. RegReadByInstruction := true;
  795. exit
  796. end;
  797. RS_EBP:
  798. if [Ch_REBP,Ch_RWEBP,Ch_MEBP,Ch_WRBP,Ch_RWRBP,Ch_MRBP]*Ch<>[] then
  799. begin
  800. RegReadByInstruction := true;
  801. exit
  802. end;
  803. RS_ESI:
  804. if [Ch_RESI,Ch_RWESI,Ch_MESI,Ch_WRSI,Ch_RWRSI,Ch_MRSI]*Ch<>[] then
  805. begin
  806. RegReadByInstruction := true;
  807. exit
  808. end;
  809. RS_EDI:
  810. if [Ch_REDI,Ch_RWEDI,Ch_MEDI,Ch_WRDI,Ch_RWRDI,Ch_MRDI]*Ch<>[] then
  811. begin
  812. RegReadByInstruction := true;
  813. exit
  814. end;
  815. end;
  816. end;
  817. R_MMREGISTER:
  818. begin
  819. case getsupreg(reg) of
  820. RS_XMM0:
  821. if [Ch_RXMM0,Ch_RWXMM0,Ch_MXMM0]*Ch<>[] then
  822. begin
  823. RegReadByInstruction := true;
  824. exit
  825. end;
  826. end;
  827. end;
  828. else
  829. ;
  830. end;
  831. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  832. begin
  833. if (Ch_RFLAGScc in Ch) and not(getsubreg(reg) in [R_SUBW,R_SUBD,R_SUBQ]) then
  834. begin
  835. case p.condition of
  836. C_A,C_NBE, { CF=0 and ZF=0 }
  837. C_BE,C_NA: { CF=1 or ZF=1 }
  838. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY,R_SUBFLAGZERO];
  839. C_AE,C_NB,C_NC, { CF=0 }
  840. C_B,C_NAE,C_C: { CF=1 }
  841. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY];
  842. C_NE,C_NZ, { ZF=0 }
  843. C_E,C_Z: { ZF=1 }
  844. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO];
  845. C_G,C_NLE, { ZF=0 and SF=OF }
  846. C_LE,C_NG: { ZF=1 or SF<>OF }
  847. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO,R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  848. C_GE,C_NL, { SF=OF }
  849. C_L,C_NGE: { SF<>OF }
  850. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  851. C_NO, { OF=0 }
  852. C_O: { OF=1 }
  853. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGOVERFLOW];
  854. C_NP,C_PO, { PF=0 }
  855. C_P,C_PE: { PF=1 }
  856. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGPARITY];
  857. C_NS, { SF=0 }
  858. C_S: { SF=1 }
  859. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN];
  860. else
  861. internalerror(2017042701);
  862. end;
  863. if RegReadByInstruction then
  864. exit;
  865. end;
  866. case getsubreg(reg) of
  867. R_SUBW,R_SUBD,R_SUBQ:
  868. RegReadByInstruction :=
  869. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  870. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  871. Ch_RDirFlag,Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc]*Ch<>[];
  872. R_SUBFLAGCARRY:
  873. RegReadByInstruction:=[Ch_RCarryFlag,Ch_RWCarryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  874. R_SUBFLAGPARITY:
  875. RegReadByInstruction:=[Ch_RParityFlag,Ch_RWParityFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  876. R_SUBFLAGAUXILIARY:
  877. RegReadByInstruction:=[Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  878. R_SUBFLAGZERO:
  879. RegReadByInstruction:=[Ch_RZeroFlag,Ch_RWZeroFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  880. R_SUBFLAGSIGN:
  881. RegReadByInstruction:=[Ch_RSignFlag,Ch_RWSignFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  882. R_SUBFLAGOVERFLOW:
  883. RegReadByInstruction:=[Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  884. R_SUBFLAGINTERRUPT:
  885. RegReadByInstruction:=[Ch_RFlags,Ch_RWFlags]*Ch<>[];
  886. R_SUBFLAGDIRECTION:
  887. RegReadByInstruction:=[Ch_RDirFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  888. else
  889. internalerror(2017042601);
  890. end;
  891. exit;
  892. end;
  893. if (Ch_NoReadIfEqualRegs in Ch) and (p.ops=2) and
  894. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  895. (p.oper[0]^.reg=p.oper[1]^.reg) then
  896. exit;
  897. if ([CH_RWOP1,CH_ROP1,CH_MOP1]*Ch<>[]) and reginop(reg,p.oper[0]^) then
  898. begin
  899. RegReadByInstruction := true;
  900. exit
  901. end;
  902. if ([Ch_RWOP2,Ch_ROP2,Ch_MOP2]*Ch<>[]) and reginop(reg,p.oper[1]^) then
  903. begin
  904. RegReadByInstruction := true;
  905. exit
  906. end;
  907. if ([Ch_RWOP3,Ch_ROP3,Ch_MOP3]*Ch<>[]) and reginop(reg,p.oper[2]^) then
  908. begin
  909. RegReadByInstruction := true;
  910. exit
  911. end;
  912. if ([Ch_RWOP4,Ch_ROP4,Ch_MOP4]*Ch<>[]) and reginop(reg,p.oper[3]^) then
  913. begin
  914. RegReadByInstruction := true;
  915. exit
  916. end;
  917. end;
  918. end;
  919. end;
  920. end;
  921. function TX86AsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  922. begin
  923. result:=false;
  924. if p1.typ<>ait_instruction then
  925. exit;
  926. if (Ch_All in insprop[taicpu(p1).opcode].Ch) then
  927. exit(true);
  928. if (getregtype(reg)=R_INTREGISTER) and
  929. { change information for xmm movsd are not correct }
  930. ((taicpu(p1).opcode<>A_MOVSD) or (taicpu(p1).ops=0)) then
  931. begin
  932. { Handle instructions that behave differently depending on the size and operand count }
  933. case taicpu(p1).opcode of
  934. A_MUL, A_DIV, A_IDIV:
  935. if taicpu(p1).opsize = S_B then
  936. Result := (getsupreg(Reg) = RS_EAX)
  937. else
  938. Result := (getsupreg(Reg) in [RS_EAX, RS_EDX]);
  939. A_IMUL:
  940. if taicpu(p1).ops = 1 then
  941. begin
  942. if taicpu(p1).opsize = S_B then
  943. Result := (getsupreg(Reg) = RS_EAX)
  944. else
  945. Result := (getsupreg(Reg) in [RS_EAX, RS_EDX]);
  946. end;
  947. { If ops are greater than 1, call inherited method }
  948. else
  949. case getsupreg(reg) of
  950. { RS_EAX = RS_RAX on x86-64 }
  951. RS_EAX:
  952. result:=([Ch_REAX,Ch_RRAX,Ch_WEAX,Ch_WRAX,Ch_RWEAX,Ch_RWRAX,Ch_MEAX,Ch_MRAX]*insprop[taicpu(p1).opcode].Ch)<>[];
  953. RS_ECX:
  954. result:=([Ch_RECX,Ch_RRCX,Ch_WECX,Ch_WRCX,Ch_RWECX,Ch_RWRCX,Ch_MECX,Ch_MRCX]*insprop[taicpu(p1).opcode].Ch)<>[];
  955. RS_EDX:
  956. result:=([Ch_REDX,Ch_RRDX,Ch_WEDX,Ch_WRDX,Ch_RWEDX,Ch_RWRDX,Ch_MEDX,Ch_MRDX]*insprop[taicpu(p1).opcode].Ch)<>[];
  957. RS_EBX:
  958. result:=([Ch_REBX,Ch_RRBX,Ch_WEBX,Ch_WRBX,Ch_RWEBX,Ch_RWRBX,Ch_MEBX,Ch_MRBX]*insprop[taicpu(p1).opcode].Ch)<>[];
  959. RS_ESP:
  960. result:=([Ch_RESP,Ch_RRSP,Ch_WESP,Ch_WRSP,Ch_RWESP,Ch_RWRSP,Ch_MESP,Ch_MRSP]*insprop[taicpu(p1).opcode].Ch)<>[];
  961. RS_EBP:
  962. result:=([Ch_REBP,Ch_RRBP,Ch_WEBP,Ch_WRBP,Ch_RWEBP,Ch_RWRBP,Ch_MEBP,Ch_MRBP]*insprop[taicpu(p1).opcode].Ch)<>[];
  963. RS_ESI:
  964. result:=([Ch_RESI,Ch_RRSI,Ch_WESI,Ch_WRSI,Ch_RWESI,Ch_RWRSI,Ch_MESI,Ch_MRSI,Ch_RMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  965. RS_EDI:
  966. result:=([Ch_REDI,Ch_RRDI,Ch_WEDI,Ch_WRDI,Ch_RWEDI,Ch_RWRDI,Ch_MEDI,Ch_MRDI,Ch_WMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  967. else
  968. ;
  969. end;
  970. end;
  971. if result then
  972. exit;
  973. end
  974. else if getregtype(reg)=R_MMREGISTER then
  975. begin
  976. case getsupreg(reg) of
  977. RS_XMM0:
  978. result:=([Ch_RXMM0,Ch_WXMM0,Ch_RWXMM0,Ch_MXMM0]*insprop[taicpu(p1).opcode].Ch)<>[];
  979. else
  980. ;
  981. end;
  982. if result then
  983. exit;
  984. end
  985. else if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  986. begin
  987. if ([Ch_RFlags,Ch_WFlags,Ch_RWFlags,Ch_RFLAGScc]*insprop[taicpu(p1).opcode].Ch)<>[] then
  988. exit(true);
  989. case getsubreg(reg) of
  990. R_SUBFLAGCARRY:
  991. Result:=([Ch_RCarryFlag,Ch_RWCarryFlag,Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  992. R_SUBFLAGPARITY:
  993. Result:=([Ch_RParityFlag,Ch_RWParityFlag,Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  994. R_SUBFLAGAUXILIARY:
  995. Result:=([Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  996. R_SUBFLAGZERO:
  997. Result:=([Ch_RZeroFlag,Ch_RWZeroFlag,Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  998. R_SUBFLAGSIGN:
  999. Result:=([Ch_RSignFlag,Ch_RWSignFlag,Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  1000. R_SUBFLAGOVERFLOW:
  1001. Result:=([Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  1002. R_SUBFLAGINTERRUPT:
  1003. Result:=([Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  1004. R_SUBFLAGDIRECTION:
  1005. Result:=([Ch_RDirFlag,Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  1006. R_SUBW,R_SUBD,R_SUBQ:
  1007. { Everything except the direction bits }
  1008. Result:=
  1009. ([Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  1010. Ch_WCarryFlag,Ch_WParityFlag,Ch_WAuxiliaryFlag,Ch_WZeroFlag,Ch_WSignFlag,Ch_WOverflowFlag,
  1011. Ch_W0CarryFlag,Ch_W0ParityFlag,Ch_W0AuxiliaryFlag,Ch_W0ZeroFlag,Ch_W0SignFlag,Ch_W0OverflowFlag,
  1012. Ch_W1CarryFlag,Ch_W1ParityFlag,Ch_W1AuxiliaryFlag,Ch_W1ZeroFlag,Ch_W1SignFlag,Ch_W1OverflowFlag,
  1013. Ch_WUCarryFlag,Ch_WUParityFlag,Ch_WUAuxiliaryFlag,Ch_WUZeroFlag,Ch_WUSignFlag,Ch_WUOverflowFlag,
  1014. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag
  1015. ]*insprop[taicpu(p1).opcode].Ch)<>[];
  1016. else
  1017. ;
  1018. end;
  1019. if result then
  1020. exit;
  1021. end
  1022. else if (getregtype(reg)=R_FPUREGISTER) and (Ch_FPU in insprop[taicpu(p1).opcode].Ch) then
  1023. exit(true);
  1024. Result:=inherited RegInInstruction(Reg, p1);
  1025. end;
  1026. function TX86AsmOptimizer.RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean;
  1027. const
  1028. WriteOps: array[0..3] of set of TInsChange =
  1029. ([CH_RWOP1,CH_WOP1,CH_MOP1],
  1030. [Ch_RWOP2,Ch_WOP2,Ch_MOP2],
  1031. [Ch_RWOP3,Ch_WOP3,Ch_MOP3],
  1032. [Ch_RWOP4,Ch_WOP4,Ch_MOP4]);
  1033. var
  1034. OperIdx: Integer;
  1035. begin
  1036. Result := False;
  1037. if p1.typ <> ait_instruction then
  1038. exit;
  1039. with insprop[taicpu(p1).opcode] do
  1040. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  1041. begin
  1042. case getsubreg(reg) of
  1043. R_SUBW,R_SUBD,R_SUBQ:
  1044. Result :=
  1045. [Ch_WCarryFlag,Ch_WParityFlag,Ch_WAuxiliaryFlag,Ch_WZeroFlag,Ch_WSignFlag,Ch_WOverflowFlag,
  1046. Ch_W0CarryFlag,Ch_W0ParityFlag,Ch_W0AuxiliaryFlag,Ch_W0ZeroFlag,Ch_W0SignFlag,Ch_W0OverflowFlag,
  1047. Ch_W1CarryFlag,Ch_W1ParityFlag,Ch_W1AuxiliaryFlag,Ch_W1ZeroFlag,Ch_W1SignFlag,Ch_W1OverflowFlag,
  1048. Ch_WUCarryFlag,Ch_WUParityFlag,Ch_WUAuxiliaryFlag,Ch_WUZeroFlag,Ch_WUSignFlag,Ch_WUOverflowFlag,
  1049. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  1050. Ch_W0DirFlag,Ch_W1DirFlag,Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  1051. R_SUBFLAGCARRY:
  1052. Result:=[Ch_WCarryFlag,Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WUCarryFlag,Ch_RWCarryFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  1053. R_SUBFLAGPARITY:
  1054. Result:=[Ch_WParityFlag,Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WUParityFlag,Ch_RWParityFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  1055. R_SUBFLAGAUXILIARY:
  1056. Result:=[Ch_WAuxiliaryFlag,Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WUAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  1057. R_SUBFLAGZERO:
  1058. Result:=[Ch_WZeroFlag,Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WUZeroFlag,Ch_RWZeroFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  1059. R_SUBFLAGSIGN:
  1060. Result:=[Ch_WSignFlag,Ch_W0SignFlag,Ch_W1SignFlag,Ch_WUSignFlag,Ch_RWSignFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  1061. R_SUBFLAGOVERFLOW:
  1062. Result:=[Ch_WOverflowFlag,Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WUOverflowFlag,Ch_RWOverflowFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  1063. R_SUBFLAGINTERRUPT:
  1064. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  1065. R_SUBFLAGDIRECTION:
  1066. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  1067. else
  1068. internalerror(2017042602);
  1069. end;
  1070. exit;
  1071. end;
  1072. case taicpu(p1).opcode of
  1073. A_CALL:
  1074. { We could potentially set Result to False if the register in
  1075. question is non-volatile for the subroutine's calling convention,
  1076. but this would require detecting the calling convention in use and
  1077. also assuming that the routine doesn't contain malformed assembly
  1078. language, for example... so it could only be done under -O4 as it
  1079. would be considered a side-effect. [Kit] }
  1080. Result := True;
  1081. A_MOVSD:
  1082. { special handling for SSE MOVSD }
  1083. if (taicpu(p1).ops>0) then
  1084. begin
  1085. if taicpu(p1).ops<>2 then
  1086. internalerror(2017042703);
  1087. Result := (taicpu(p1).oper[1]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[1]^);
  1088. end;
  1089. { VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  1090. so fix it here (FK)
  1091. }
  1092. A_VMOVSS,
  1093. A_VMOVSD:
  1094. begin
  1095. Result := (taicpu(p1).ops=3) and (taicpu(p1).oper[2]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[2]^);
  1096. exit;
  1097. end;
  1098. A_MUL, A_DIV, A_IDIV:
  1099. begin
  1100. if taicpu(p1).opsize = S_B then
  1101. Result := (getsupreg(Reg) = RS_EAX)
  1102. else
  1103. Result := (getsupreg(Reg) in [RS_EAX, RS_EDX]);
  1104. end;
  1105. A_IMUL:
  1106. begin
  1107. if taicpu(p1).ops = 1 then
  1108. begin
  1109. Result := (getsupreg(Reg) in [RS_EAX, RS_EDX]);
  1110. end
  1111. else
  1112. Result := (taicpu(p1).oper[taicpu(p1).ops-1]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[taicpu(p1).ops-1]^);
  1113. Exit;
  1114. end;
  1115. else
  1116. ;
  1117. end;
  1118. if Result then
  1119. exit;
  1120. with insprop[taicpu(p1).opcode] do
  1121. begin
  1122. if getregtype(reg)=R_INTREGISTER then
  1123. begin
  1124. case getsupreg(reg) of
  1125. RS_EAX:
  1126. if [Ch_WEAX,Ch_RWEAX,Ch_MEAX,Ch_WRAX,Ch_RWRAX,Ch_MRAX]*Ch<>[] then
  1127. begin
  1128. Result := True;
  1129. exit
  1130. end;
  1131. RS_ECX:
  1132. if [Ch_WECX,Ch_RWECX,Ch_MECX,Ch_WRCX,Ch_RWRCX,Ch_MRCX]*Ch<>[] then
  1133. begin
  1134. Result := True;
  1135. exit
  1136. end;
  1137. RS_EDX:
  1138. if [Ch_WEDX,Ch_RWEDX,Ch_MEDX,Ch_WRDX,Ch_RWRDX,Ch_MRDX]*Ch<>[] then
  1139. begin
  1140. Result := True;
  1141. exit
  1142. end;
  1143. RS_EBX:
  1144. if [Ch_WEBX,Ch_RWEBX,Ch_MEBX,Ch_WRBX,Ch_RWRBX,Ch_MRBX]*Ch<>[] then
  1145. begin
  1146. Result := True;
  1147. exit
  1148. end;
  1149. RS_ESP:
  1150. if [Ch_WESP,Ch_RWESP,Ch_MESP,Ch_WRSP,Ch_RWRSP,Ch_MRSP]*Ch<>[] then
  1151. begin
  1152. Result := True;
  1153. exit
  1154. end;
  1155. RS_EBP:
  1156. if [Ch_WEBP,Ch_RWEBP,Ch_MEBP,Ch_WRBP,Ch_RWRBP,Ch_MRBP]*Ch<>[] then
  1157. begin
  1158. Result := True;
  1159. exit
  1160. end;
  1161. RS_ESI:
  1162. if [Ch_WESI,Ch_RWESI,Ch_MESI,Ch_WRSI,Ch_RWRSI,Ch_MRSI]*Ch<>[] then
  1163. begin
  1164. Result := True;
  1165. exit
  1166. end;
  1167. RS_EDI:
  1168. if [Ch_WEDI,Ch_RWEDI,Ch_MEDI,Ch_WRDI,Ch_RWRDI,Ch_MRDI]*Ch<>[] then
  1169. begin
  1170. Result := True;
  1171. exit
  1172. end;
  1173. end;
  1174. end;
  1175. for OperIdx := 0 to taicpu(p1).ops - 1 do
  1176. if (WriteOps[OperIdx]*Ch<>[]) and
  1177. { The register doesn't get modified inside a reference }
  1178. (taicpu(p1).oper[OperIdx]^.typ = top_reg) and
  1179. SuperRegistersEqual(reg,taicpu(p1).oper[OperIdx]^.reg) then
  1180. begin
  1181. Result := true;
  1182. exit
  1183. end;
  1184. end;
  1185. end;
  1186. function TX86AsmOptimizer.RefModifiedBetween(Ref: TReference; RefSize: ASizeInt; p1, p2: tai): Boolean;
  1187. const
  1188. WriteOps: array[0..3] of set of TInsChange =
  1189. ([CH_RWOP1,CH_WOP1,CH_MOP1],
  1190. [Ch_RWOP2,Ch_WOP2,Ch_MOP2],
  1191. [Ch_RWOP3,Ch_WOP3,Ch_MOP3],
  1192. [Ch_RWOP4,Ch_WOP4,Ch_MOP4]);
  1193. var
  1194. X: Integer;
  1195. CurrentP1Size: asizeint;
  1196. begin
  1197. Result := (
  1198. (Ref.base <> NR_NO) and
  1199. {$ifdef x86_64}
  1200. (Ref.base <> NR_RIP) and
  1201. {$endif x86_64}
  1202. RegModifiedBetween(Ref.base, p1, p2)
  1203. ) or
  1204. (
  1205. (Ref.index <> NR_NO) and
  1206. (Ref.index <> Ref.base) and
  1207. RegModifiedBetween(Ref.index, p1, p2)
  1208. );
  1209. { Now check to see if the memory itself is written to }
  1210. if not Result then
  1211. begin
  1212. while assigned(p1) and assigned(p2) and GetNextInstruction(p1,p1) and (p1<>p2) do
  1213. if p1.typ = ait_instruction then
  1214. begin
  1215. CurrentP1Size := topsize2memsize[taicpu(p1).opsize] shr 3; { Convert to bytes }
  1216. with insprop[taicpu(p1).opcode] do
  1217. for X := 0 to taicpu(p1).ops - 1 do
  1218. if (taicpu(p1).oper[X]^.typ = top_ref) and
  1219. RefsAlmostEqual(Ref, taicpu(p1).oper[X]^.ref^) and
  1220. { Catch any potential overlaps }
  1221. (
  1222. (RefSize = 0) or
  1223. ((taicpu(p1).oper[X]^.ref^.offset - Ref.offset) < RefSize)
  1224. ) and
  1225. (
  1226. (CurrentP1Size = 0) or
  1227. ((Ref.offset - taicpu(p1).oper[X]^.ref^.offset) < CurrentP1Size)
  1228. ) and
  1229. { Reference is used, but does the instruction write to it? }
  1230. (
  1231. (Ch_All in Ch) or
  1232. ((WriteOps[X] * Ch) <> [])
  1233. ) then
  1234. begin
  1235. Result := True;
  1236. Break;
  1237. end;
  1238. end;
  1239. end;
  1240. end;
  1241. {$ifdef DEBUG_AOPTCPU}
  1242. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);
  1243. begin
  1244. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  1245. end;
  1246. function debug_tostr(i: tcgint): string; inline;
  1247. begin
  1248. Result := tostr(i);
  1249. end;
  1250. function debug_hexstr(i: tcgint): string;
  1251. begin
  1252. Result := '0x';
  1253. case i of
  1254. 0..$FF:
  1255. Result := Result + hexstr(i, 2);
  1256. $100..$FFFF:
  1257. Result := Result + hexstr(i, 4);
  1258. $10000..$FFFFFF:
  1259. Result := Result + hexstr(i, 6);
  1260. $1000000..$FFFFFFFF:
  1261. Result := Result + hexstr(i, 8);
  1262. else
  1263. Result := Result + hexstr(i, 16);
  1264. end;
  1265. end;
  1266. function debug_regname(r: TRegister): string; inline;
  1267. begin
  1268. Result := '%' + std_regname(r);
  1269. end;
  1270. { Debug output function - creates a string representation of an operator }
  1271. function debug_operstr(oper: TOper): string;
  1272. begin
  1273. case oper.typ of
  1274. top_const:
  1275. Result := '$' + debug_tostr(oper.val);
  1276. top_reg:
  1277. Result := debug_regname(oper.reg);
  1278. top_ref:
  1279. begin
  1280. if oper.ref^.offset <> 0 then
  1281. Result := debug_tostr(oper.ref^.offset) + '('
  1282. else
  1283. Result := '(';
  1284. if (oper.ref^.base <> NR_INVALID) and (oper.ref^.base <> NR_NO) then
  1285. begin
  1286. Result := Result + debug_regname(oper.ref^.base);
  1287. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  1288. Result := Result + ',' + debug_regname(oper.ref^.index);
  1289. end
  1290. else
  1291. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  1292. Result := Result + debug_regname(oper.ref^.index);
  1293. if (oper.ref^.scalefactor > 1) then
  1294. Result := Result + ',' + debug_tostr(oper.ref^.scalefactor) + ')'
  1295. else
  1296. Result := Result + ')';
  1297. end;
  1298. else
  1299. Result := '[UNKNOWN]';
  1300. end;
  1301. end;
  1302. function debug_op2str(opcode: tasmop): string; inline;
  1303. begin
  1304. Result := std_op2str[opcode];
  1305. end;
  1306. function debug_opsize2str(opsize: topsize): string; inline;
  1307. begin
  1308. Result := gas_opsize2str[opsize];
  1309. end;
  1310. {$else DEBUG_AOPTCPU}
  1311. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  1312. begin
  1313. end;
  1314. function debug_tostr(i: tcgint): string; inline;
  1315. begin
  1316. Result := '';
  1317. end;
  1318. function debug_hexstr(i: tcgint): string; inline;
  1319. begin
  1320. Result := '';
  1321. end;
  1322. function debug_regname(r: TRegister): string; inline;
  1323. begin
  1324. Result := '';
  1325. end;
  1326. function debug_operstr(oper: TOper): string; inline;
  1327. begin
  1328. Result := '';
  1329. end;
  1330. function debug_op2str(opcode: tasmop): string; inline;
  1331. begin
  1332. Result := '';
  1333. end;
  1334. function debug_opsize2str(opsize: topsize): string; inline;
  1335. begin
  1336. Result := '';
  1337. end;
  1338. {$endif DEBUG_AOPTCPU}
  1339. class function TX86AsmOptimizer.IsMOVZXAcceptable: Boolean; inline;
  1340. begin
  1341. {$ifdef x86_64}
  1342. { Always fine on x86-64 }
  1343. Result := True;
  1344. {$else x86_64}
  1345. Result :=
  1346. {$ifdef i8086}
  1347. (current_settings.cputype >= cpu_386) and
  1348. {$endif i8086}
  1349. (
  1350. { Always accept if optimising for size }
  1351. (cs_opt_size in current_settings.optimizerswitches) or
  1352. { From the Pentium II onwards, MOVZX only takes 1 cycle. [Kit] }
  1353. (current_settings.optimizecputype >= cpu_Pentium2)
  1354. );
  1355. {$endif x86_64}
  1356. end;
  1357. { Attempts to allocate a volatile integer register for use between p and hp,
  1358. using AUsedRegs for the current register usage information. Returns NR_NO
  1359. if no free register could be found }
  1360. function TX86AsmOptimizer.GetIntRegisterBetween(RegSize: TSubRegister; var AUsedRegs: TAllUsedRegs; p, hp: tai; DontAlloc: Boolean = False): TRegister;
  1361. var
  1362. RegSet: TCPURegisterSet;
  1363. CurrentSuperReg: Integer;
  1364. CurrentReg: TRegister;
  1365. Currentp: tai;
  1366. Breakout: Boolean;
  1367. begin
  1368. Result := NR_NO;
  1369. RegSet :=
  1370. paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption) +
  1371. current_procinfo.saved_regs_int;
  1372. (*
  1373. { Don't use the frame register unless explicitly allowed (fixes i40111) }
  1374. if ([cs_useebp, cs_userbp] * current_settings.optimizerswitches) = [] then
  1375. Exclude(RegSet, RS_FRAME_POINTER_REG);
  1376. *)
  1377. for CurrentSuperReg in RegSet do
  1378. begin
  1379. CurrentReg := newreg(R_INTREGISTER, TSuperRegister(CurrentSuperReg), RegSize);
  1380. if not AUsedRegs[R_INTREGISTER].IsUsed(CurrentReg)
  1381. {$if defined(i386) or defined(i8086)}
  1382. { If the target size is 8-bit, make sure we can actually encode it }
  1383. and (
  1384. (RegSize >= R_SUBW) or { Not R_SUBL or R_SUBH }
  1385. (GetSupReg(CurrentReg) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX])
  1386. )
  1387. {$endif i386 or i8086}
  1388. then
  1389. begin
  1390. Currentp := p;
  1391. Breakout := False;
  1392. while not Breakout and GetNextInstruction(Currentp, Currentp) and (Currentp <> hp) do
  1393. begin
  1394. case Currentp.typ of
  1395. ait_instruction:
  1396. begin
  1397. if RegInInstruction(CurrentReg, Currentp) then
  1398. begin
  1399. Breakout := True;
  1400. Break;
  1401. end;
  1402. { Cannot allocate across an unconditional jump }
  1403. if is_calljmpuncondret(taicpu(Currentp).opcode) then
  1404. Exit;
  1405. end;
  1406. ait_marker:
  1407. { Don't try anything more if a marker is hit }
  1408. Exit;
  1409. ait_regalloc:
  1410. if (tai_regalloc(Currentp).ratype <> ra_dealloc) and SuperRegistersEqual(CurrentReg, tai_regalloc(Currentp).reg) then
  1411. begin
  1412. Breakout := True;
  1413. Break;
  1414. end;
  1415. else
  1416. ;
  1417. end;
  1418. end;
  1419. if Breakout then
  1420. { Try the next register }
  1421. Continue;
  1422. { We have a free register available }
  1423. Result := CurrentReg;
  1424. if not DontAlloc then
  1425. AllocRegBetween(CurrentReg, p, hp, AUsedRegs);
  1426. Exit;
  1427. end;
  1428. end;
  1429. end;
  1430. { Attempts to allocate a volatile MM register for use between p and hp,
  1431. using AUsedRegs for the current register usage information. Returns NR_NO
  1432. if no free register could be found }
  1433. function TX86AsmOptimizer.GetMMRegisterBetween(RegSize: TSubRegister; var AUsedRegs: TAllUsedRegs; p, hp: tai; DontAlloc: Boolean = False): TRegister;
  1434. var
  1435. RegSet: TCPURegisterSet;
  1436. CurrentSuperReg: Integer;
  1437. CurrentReg: TRegister;
  1438. Currentp: tai;
  1439. Breakout: Boolean;
  1440. begin
  1441. Result := NR_NO;
  1442. RegSet :=
  1443. paramanager.get_volatile_registers_mm(current_procinfo.procdef.proccalloption) +
  1444. current_procinfo.saved_regs_mm;
  1445. for CurrentSuperReg in RegSet do
  1446. begin
  1447. CurrentReg := newreg(R_MMREGISTER, TSuperRegister(CurrentSuperReg), RegSize);
  1448. if not AUsedRegs[R_MMREGISTER].IsUsed(CurrentReg) then
  1449. begin
  1450. Currentp := p;
  1451. Breakout := False;
  1452. while not Breakout and GetNextInstruction(Currentp, Currentp) and (Currentp <> hp) do
  1453. begin
  1454. case Currentp.typ of
  1455. ait_instruction:
  1456. begin
  1457. if RegInInstruction(CurrentReg, Currentp) then
  1458. begin
  1459. Breakout := True;
  1460. Break;
  1461. end;
  1462. { Cannot allocate across an unconditional jump }
  1463. if is_calljmpuncondret(taicpu(Currentp).opcode) then
  1464. Exit;
  1465. end;
  1466. ait_marker:
  1467. { Don't try anything more if a marker is hit }
  1468. Exit;
  1469. ait_regalloc:
  1470. if (tai_regalloc(Currentp).ratype <> ra_dealloc) and SuperRegistersEqual(CurrentReg, tai_regalloc(Currentp).reg) then
  1471. begin
  1472. Breakout := True;
  1473. Break;
  1474. end;
  1475. else
  1476. ;
  1477. end;
  1478. end;
  1479. if Breakout then
  1480. { Try the next register }
  1481. Continue;
  1482. { We have a free register available }
  1483. Result := CurrentReg;
  1484. if not DontAlloc then
  1485. AllocRegBetween(CurrentReg, p, hp, AUsedRegs);
  1486. Exit;
  1487. end;
  1488. end;
  1489. end;
  1490. class function TX86AsmOptimizer.Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  1491. begin
  1492. if not SuperRegistersEqual(reg1,reg2) then
  1493. exit(false);
  1494. if getregtype(reg1)<>R_INTREGISTER then
  1495. exit(true); {because SuperRegisterEqual is true}
  1496. case getsubreg(reg1) of
  1497. { A write to R_SUBL doesn't change R_SUBH and if reg2 is R_SUBW or
  1498. higher, it preserves the high bits, so the new value depends on
  1499. reg2's previous value. In other words, it is equivalent to doing:
  1500. reg2 := (reg2 and $ffffff00) or byte(reg1); }
  1501. R_SUBL:
  1502. exit(getsubreg(reg2)=R_SUBL);
  1503. { A write to R_SUBH doesn't change R_SUBL and if reg2 is R_SUBW or
  1504. higher, it actually does a:
  1505. reg2 := (reg2 and $ffff00ff) or (reg1 and $ff00); }
  1506. R_SUBH:
  1507. exit(getsubreg(reg2)=R_SUBH);
  1508. { If reg2 is R_SUBD or larger, a write to R_SUBW preserves the high 16
  1509. bits of reg2:
  1510. reg2 := (reg2 and $ffff0000) or word(reg1); }
  1511. R_SUBW:
  1512. exit(getsubreg(reg2) in [R_SUBL,R_SUBH,R_SUBW]);
  1513. { a write to R_SUBD always overwrites every other subregister,
  1514. because it clears the high 32 bits of R_SUBQ on x86_64 }
  1515. R_SUBD,
  1516. R_SUBQ:
  1517. exit(true);
  1518. else
  1519. internalerror(2017042801);
  1520. end;
  1521. end;
  1522. class function TX86AsmOptimizer.Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  1523. begin
  1524. if not SuperRegistersEqual(reg1,reg2) then
  1525. exit(false);
  1526. if getregtype(reg1)<>R_INTREGISTER then
  1527. exit(true); {because SuperRegisterEqual is true}
  1528. case getsubreg(reg1) of
  1529. R_SUBL:
  1530. exit(getsubreg(reg2)<>R_SUBH);
  1531. R_SUBH:
  1532. exit(getsubreg(reg2)<>R_SUBL);
  1533. R_SUBW,
  1534. R_SUBD,
  1535. R_SUBQ:
  1536. exit(true);
  1537. else
  1538. internalerror(2017042802);
  1539. end;
  1540. end;
  1541. function TX86AsmOptimizer.PrePeepholeOptSxx(var p : tai) : boolean;
  1542. var
  1543. hp1 : tai;
  1544. l : TCGInt;
  1545. begin
  1546. result:=false;
  1547. if not(GetNextInstruction(p, hp1)) then
  1548. exit;
  1549. { changes the code sequence
  1550. shr/sar const1, x
  1551. shl const2, x
  1552. to
  1553. either "sar/and", "shl/and" or just "and" depending on const1 and const2 }
  1554. if (taicpu(p).oper[0]^.typ = top_const) and
  1555. MatchInstruction(hp1,A_SHL,[]) and
  1556. (taicpu(hp1).oper[0]^.typ = top_const) and
  1557. (taicpu(hp1).opsize = taicpu(p).opsize) and
  1558. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[1]^.typ) and
  1559. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) then
  1560. begin
  1561. if (taicpu(p).oper[0]^.val > taicpu(hp1).oper[0]^.val) and
  1562. not(cs_opt_size in current_settings.optimizerswitches)
  1563. {$ifdef x86_64}
  1564. and (
  1565. (taicpu(p).opsize <> S_Q) or
  1566. { 64-bit AND can only store signed 32-bit immediates }
  1567. (taicpu(p).oper[0]^.val < 32)
  1568. )
  1569. {$endif x86_64}
  1570. then
  1571. begin
  1572. { shr/sar const1, %reg
  1573. shl const2, %reg
  1574. with const1 > const2 }
  1575. DebugMsg(SPeepholeOptimization + 'SxrShl2SxrAnd 1 done',p);
  1576. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  1577. taicpu(hp1).opcode := A_AND;
  1578. l := (1 shl (taicpu(hp1).oper[0]^.val)) - 1;
  1579. case taicpu(p).opsize Of
  1580. S_B: taicpu(hp1).loadConst(0,l Xor $ff);
  1581. S_W: taicpu(hp1).loadConst(0,l Xor $ffff);
  1582. S_L: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffff));
  1583. S_Q: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffffffffffff));
  1584. else
  1585. Internalerror(2017050703)
  1586. end;
  1587. end
  1588. else if (taicpu(p).oper[0]^.val<taicpu(hp1).oper[0]^.val) and
  1589. not(cs_opt_size in current_settings.optimizerswitches)
  1590. {$ifdef x86_64}
  1591. and (
  1592. (taicpu(p).opsize <> S_Q) or
  1593. { 64-bit AND can only store signed 32-bit immediates }
  1594. (taicpu(p).oper[0]^.val < 32)
  1595. )
  1596. {$endif x86_64}
  1597. then
  1598. begin
  1599. { shr/sar const1, %reg
  1600. shl const2, %reg
  1601. with const1 < const2 }
  1602. DebugMsg(SPeepholeOptimization + 'SxrShl2SxrAnd 2 done',p);
  1603. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val-taicpu(p).oper[0]^.val);
  1604. taicpu(p).opcode := A_AND;
  1605. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  1606. case taicpu(p).opsize Of
  1607. S_B: taicpu(p).loadConst(0,l Xor $ff);
  1608. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  1609. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  1610. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  1611. else
  1612. Internalerror(2017050702)
  1613. end;
  1614. end
  1615. else if (taicpu(p).oper[0]^.val = taicpu(hp1).oper[0]^.val)
  1616. {$ifdef x86_64}
  1617. and (
  1618. (taicpu(p).opsize <> S_Q) or
  1619. { 64-bit AND can only store signed 32-bit immediates }
  1620. (taicpu(p).oper[0]^.val < 32)
  1621. )
  1622. {$endif x86_64}
  1623. then
  1624. begin
  1625. { shr/sar const1, %reg
  1626. shl const2, %reg
  1627. with const1 = const2 }
  1628. DebugMsg(SPeepholeOptimization + 'SxrShl2And done',p);
  1629. taicpu(p).opcode := A_AND;
  1630. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  1631. case taicpu(p).opsize Of
  1632. S_B: taicpu(p).loadConst(0,l Xor $ff);
  1633. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  1634. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  1635. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  1636. else
  1637. Internalerror(2017050701)
  1638. end;
  1639. RemoveInstruction(hp1);
  1640. end;
  1641. end;
  1642. end;
  1643. function TX86AsmOptimizer.PrePeepholeOptIMUL(var p : tai) : boolean;
  1644. var
  1645. opsize : topsize;
  1646. hp1, hp2 : tai;
  1647. tmpref : treference;
  1648. ShiftValue : Cardinal;
  1649. BaseValue : TCGInt;
  1650. begin
  1651. result:=false;
  1652. opsize:=taicpu(p).opsize;
  1653. { changes certain "imul const, %reg"'s to lea sequences }
  1654. if (MatchOpType(taicpu(p),top_const,top_reg) or
  1655. MatchOpType(taicpu(p),top_const,top_reg,top_reg)) and
  1656. (opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) then
  1657. if (taicpu(p).oper[0]^.val = 1) then
  1658. if (taicpu(p).ops = 2) then
  1659. { remove "imul $1, reg" }
  1660. begin
  1661. DebugMsg(SPeepholeOptimization + 'Imul2Nop done',p);
  1662. Result := RemoveCurrentP(p);
  1663. end
  1664. else
  1665. { change "imul $1, reg1, reg2" to "mov reg1, reg2" }
  1666. begin
  1667. hp1 := taicpu.Op_Reg_Reg(A_MOV, opsize, taicpu(p).oper[1]^.reg,taicpu(p).oper[2]^.reg);
  1668. taicpu(hp1).fileinfo := taicpu(p).fileinfo;
  1669. asml.InsertAfter(hp1, p);
  1670. DebugMsg(SPeepholeOptimization + 'Imul2Mov done',p);
  1671. RemoveCurrentP(p, hp1);
  1672. Result := True;
  1673. end
  1674. else if ((taicpu(p).ops <= 2) or
  1675. (taicpu(p).oper[2]^.typ = Top_Reg)) and
  1676. not(cs_opt_size in current_settings.optimizerswitches) and
  1677. (not(GetNextInstruction(p, hp1)) or
  1678. not((tai(hp1).typ = ait_instruction) and
  1679. ((taicpu(hp1).opcode=A_Jcc) and
  1680. (taicpu(hp1).condition in [C_O,C_NO])))) then
  1681. begin
  1682. {
  1683. imul X, reg1, reg2 to
  1684. lea (reg1,reg1,Y), reg2
  1685. shl ZZ,reg2
  1686. imul XX, reg1 to
  1687. lea (reg1,reg1,YY), reg1
  1688. shl ZZ,reg2
  1689. This optimziation makes sense for pretty much every x86, except the VIA Nano3000: it has IMUL latency 2, lea/shl pair as well,
  1690. it does not exist as a separate optimization target in FPC though.
  1691. This optimziation can be applied as long as only two bits are set in the constant and those two bits are separated by
  1692. at most two zeros
  1693. }
  1694. reference_reset(tmpref,1,[]);
  1695. if (PopCnt(QWord(taicpu(p).oper[0]^.val))=2) and (BsrQWord(taicpu(p).oper[0]^.val)-BsfQWord(taicpu(p).oper[0]^.val)<=3) then
  1696. begin
  1697. ShiftValue:=BsfQWord(taicpu(p).oper[0]^.val);
  1698. BaseValue:=taicpu(p).oper[0]^.val shr ShiftValue;
  1699. TmpRef.base := taicpu(p).oper[1]^.reg;
  1700. TmpRef.index := taicpu(p).oper[1]^.reg;
  1701. if not(BaseValue in [3,5,9]) then
  1702. Internalerror(2018110101);
  1703. TmpRef.ScaleFactor := BaseValue-1;
  1704. if (taicpu(p).ops = 2) then
  1705. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[1]^.reg)
  1706. else
  1707. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[2]^.reg);
  1708. AsmL.InsertAfter(hp1,p);
  1709. DebugMsg(SPeepholeOptimization + 'Imul2LeaShl done',p);
  1710. taicpu(hp1).fileinfo:=taicpu(p).fileinfo;
  1711. RemoveCurrentP(p, hp1);
  1712. if ShiftValue>0 then
  1713. begin
  1714. hp2 := taicpu.op_const_reg(A_SHL, opsize, ShiftValue, taicpu(hp1).oper[1]^.reg);
  1715. AsmL.InsertAfter(hp2,hp1);
  1716. taicpu(hp2).fileinfo:=taicpu(hp1).fileinfo;
  1717. end;
  1718. Result := True;
  1719. end;
  1720. end;
  1721. end;
  1722. function TX86AsmOptimizer.PrePeepholeOptAND(var p : tai) : boolean;
  1723. begin
  1724. Result := False;
  1725. if MatchOperand(taicpu(p).oper[0]^, 0) and
  1726. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) then
  1727. begin
  1728. DebugMsg(SPeepholeOptimization + 'AND 0 -> MOV 0', p);
  1729. taicpu(p).opcode := A_MOV;
  1730. Result := True;
  1731. end;
  1732. end;
  1733. function TX86AsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  1734. var
  1735. p: taicpu absolute hp; { Implicit typecast }
  1736. i: Integer;
  1737. begin
  1738. Result := False;
  1739. if not assigned(hp) or
  1740. (hp.typ <> ait_instruction) then
  1741. Exit;
  1742. Prefetch(insprop[p.opcode]);
  1743. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  1744. with insprop[p.opcode] do
  1745. begin
  1746. case getsubreg(reg) of
  1747. R_SUBW,R_SUBD,R_SUBQ:
  1748. Result:=
  1749. { ZF, CF, OF, SF, PF and AF must all be set in some way (ordered so the most
  1750. uncommon flags are checked first }
  1751. ([Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag,Ch_WFlags] * Ch <> []) and
  1752. ([Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag,Ch_WFlags]*Ch <> []) and
  1753. ([Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag,Ch_WFlags]*Ch <> []) and
  1754. ([Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag,Ch_WFlags]*Ch <> []) and
  1755. ([Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag,Ch_WFlags]*Ch <> []) and
  1756. ([Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag,Ch_WFlags]*Ch <> []);
  1757. R_SUBFLAGCARRY:
  1758. Result:=[Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag,Ch_WFlags]*Ch<>[];
  1759. R_SUBFLAGPARITY:
  1760. Result:=[Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag,Ch_WFlags]*Ch<>[];
  1761. R_SUBFLAGAUXILIARY:
  1762. Result:=[Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag,Ch_WFlags]*Ch<>[];
  1763. R_SUBFLAGZERO:
  1764. Result:=[Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag,Ch_WFlags]*Ch<>[];
  1765. R_SUBFLAGSIGN:
  1766. Result:=[Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag,Ch_WFlags]*Ch<>[];
  1767. R_SUBFLAGOVERFLOW:
  1768. Result:=[Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag,Ch_WFlags]*Ch<>[];
  1769. R_SUBFLAGINTERRUPT:
  1770. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*Ch<>[];
  1771. R_SUBFLAGDIRECTION:
  1772. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*Ch<>[];
  1773. else
  1774. internalerror(2017050501);
  1775. end;
  1776. exit;
  1777. end;
  1778. { Handle special cases first }
  1779. case p.opcode of
  1780. A_MOV, A_MOVZX, A_MOVSX, A_LEA, A_VMOVSS, A_VMOVSD, A_VMOVAPD,
  1781. A_VMOVAPS, A_VMOVQ, A_MOVSS, A_MOVSD, A_MOVQ, A_MOVAPD, A_MOVAPS:
  1782. begin
  1783. Result :=
  1784. (p.ops=2) and { A_MOVSD can have zero operands, so this check is needed }
  1785. (p.oper[1]^.typ = top_reg) and
  1786. (Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg)) and
  1787. (
  1788. (p.oper[0]^.typ = top_const) or
  1789. (
  1790. (p.oper[0]^.typ = top_reg) and
  1791. not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))
  1792. ) or (
  1793. (p.oper[0]^.typ = top_ref) and
  1794. not RegInRef(reg,p.oper[0]^.ref^)
  1795. )
  1796. );
  1797. end;
  1798. A_MUL, A_IMUL:
  1799. Result :=
  1800. (
  1801. (p.ops=3) and { IMUL only }
  1802. (Reg1WriteOverwritesReg2Entirely(p.oper[2]^.reg,reg)) and
  1803. (
  1804. (
  1805. (p.oper[1]^.typ=top_reg) and
  1806. not Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg)
  1807. ) or (
  1808. (p.oper[1]^.typ=top_ref) and
  1809. not RegInRef(reg,p.oper[1]^.ref^)
  1810. )
  1811. )
  1812. ) or (
  1813. (
  1814. (p.ops=1) and
  1815. (
  1816. (
  1817. (
  1818. (p.oper[0]^.typ=top_reg) and
  1819. not Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg)
  1820. )
  1821. ) or (
  1822. (p.oper[0]^.typ=top_ref) and
  1823. not RegInRef(reg,p.oper[0]^.ref^)
  1824. )
  1825. ) and (
  1826. (
  1827. (p.opsize=S_B) and
  1828. Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and
  1829. not Reg1ReadDependsOnReg2(NR_AL,reg)
  1830. ) or (
  1831. (p.opsize=S_W) and
  1832. Reg1WriteOverwritesReg2Entirely(NR_DX,reg)
  1833. ) or (
  1834. (p.opsize=S_L) and
  1835. Reg1WriteOverwritesReg2Entirely(NR_EDX,reg)
  1836. {$ifdef x86_64}
  1837. ) or (
  1838. (p.opsize=S_Q) and
  1839. Reg1WriteOverwritesReg2Entirely(NR_RDX,reg)
  1840. {$endif x86_64}
  1841. )
  1842. )
  1843. )
  1844. );
  1845. A_CBW:
  1846. Result := Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg));
  1847. {$ifndef x86_64}
  1848. A_LDS:
  1849. Result := (reg=NR_DS) and not(RegInRef(reg,p.oper[0]^.ref^));
  1850. A_LES:
  1851. Result := (reg=NR_ES) and not(RegInRef(reg,p.oper[0]^.ref^));
  1852. {$endif not x86_64}
  1853. A_LFS:
  1854. Result := (reg=NR_FS) and not(RegInRef(reg,p.oper[0]^.ref^));
  1855. A_LGS:
  1856. Result := (reg=NR_GS) and not(RegInRef(reg,p.oper[0]^.ref^));
  1857. A_LSS:
  1858. Result := (reg=NR_SS) and not(RegInRef(reg,p.oper[0]^.ref^));
  1859. A_LAHF{$ifndef x86_64}, A_AAM{$endif not x86_64}:
  1860. Result := Reg1WriteOverwritesReg2Entirely(NR_AH,reg);
  1861. A_LODSB:
  1862. Result := Reg1WriteOverwritesReg2Entirely(NR_AL,reg);
  1863. A_LODSW:
  1864. Result := Reg1WriteOverwritesReg2Entirely(NR_AX,reg);
  1865. {$ifdef x86_64}
  1866. A_LODSQ:
  1867. Result := Reg1WriteOverwritesReg2Entirely(NR_RAX,reg);
  1868. {$endif x86_64}
  1869. A_LODSD:
  1870. Result := Reg1WriteOverwritesReg2Entirely(NR_EAX,reg);
  1871. A_FSTSW, A_FNSTSW:
  1872. Result := (p.oper[0]^.typ=top_reg) and Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg);
  1873. else
  1874. begin
  1875. with insprop[p.opcode] do
  1876. begin
  1877. if (
  1878. { xor %reg,%reg etc. is classed as a new value }
  1879. (([Ch_NoReadIfEqualRegs]*Ch)<>[]) and
  1880. MatchOpType(p, top_reg, top_reg) and
  1881. (p.oper[0]^.reg = p.oper[1]^.reg) and
  1882. Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg)
  1883. ) then
  1884. begin
  1885. Result := True;
  1886. Exit;
  1887. end;
  1888. { Make sure the entire register is overwritten }
  1889. if (getregtype(reg) = R_INTREGISTER) then
  1890. begin
  1891. if (p.ops > 0) then
  1892. begin
  1893. if RegInOp(reg, p.oper[0]^) then
  1894. begin
  1895. if (p.oper[0]^.typ = top_ref) then
  1896. begin
  1897. if RegInRef(reg, p.oper[0]^.ref^) then
  1898. begin
  1899. Result := False;
  1900. Exit;
  1901. end;
  1902. end
  1903. else if (p.oper[0]^.typ = top_reg) then
  1904. begin
  1905. if ([Ch_ROp1, Ch_RWOp1, Ch_MOp1]*Ch<>[]) then
  1906. begin
  1907. Result := False;
  1908. Exit;
  1909. end
  1910. else if ([Ch_WOp1]*Ch<>[]) then
  1911. begin
  1912. if Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg, reg) then
  1913. Result := True
  1914. else
  1915. begin
  1916. Result := False;
  1917. Exit;
  1918. end;
  1919. end;
  1920. end;
  1921. end;
  1922. if (p.ops > 1) then
  1923. begin
  1924. if RegInOp(reg, p.oper[1]^) then
  1925. begin
  1926. if (p.oper[1]^.typ = top_ref) then
  1927. begin
  1928. if RegInRef(reg, p.oper[1]^.ref^) then
  1929. begin
  1930. Result := False;
  1931. Exit;
  1932. end;
  1933. end
  1934. else if (p.oper[1]^.typ = top_reg) then
  1935. begin
  1936. if ([Ch_ROp2, Ch_RWOp2, Ch_MOp2]*Ch<>[]) then
  1937. begin
  1938. Result := False;
  1939. Exit;
  1940. end
  1941. else if ([Ch_WOp2]*Ch<>[]) then
  1942. begin
  1943. if Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg, reg) then
  1944. Result := True
  1945. else
  1946. begin
  1947. Result := False;
  1948. Exit;
  1949. end;
  1950. end;
  1951. end;
  1952. end;
  1953. if (p.ops > 2) then
  1954. begin
  1955. if RegInOp(reg, p.oper[2]^) then
  1956. begin
  1957. if (p.oper[2]^.typ = top_ref) then
  1958. begin
  1959. if RegInRef(reg, p.oper[2]^.ref^) then
  1960. begin
  1961. Result := False;
  1962. Exit;
  1963. end;
  1964. end
  1965. else if (p.oper[2]^.typ = top_reg) then
  1966. begin
  1967. if ([Ch_ROp3, Ch_RWOp3, Ch_MOp3]*Ch<>[]) then
  1968. begin
  1969. Result := False;
  1970. Exit;
  1971. end
  1972. else if ([Ch_WOp3]*Ch<>[]) then
  1973. begin
  1974. if Reg1WriteOverwritesReg2Entirely(p.oper[2]^.reg, reg) then
  1975. Result := True
  1976. else
  1977. begin
  1978. Result := False;
  1979. Exit;
  1980. end;
  1981. end;
  1982. end;
  1983. end;
  1984. if (p.ops > 3) and RegInOp(reg, p.oper[3]^) then
  1985. begin
  1986. if (p.oper[3]^.typ = top_ref) then
  1987. begin
  1988. if RegInRef(reg, p.oper[3]^.ref^) then
  1989. begin
  1990. Result := False;
  1991. Exit;
  1992. end;
  1993. end
  1994. else if (p.oper[3]^.typ = top_reg) then
  1995. begin
  1996. if ([Ch_ROp4, Ch_RWOp4, Ch_MOp4]*Ch<>[]) then
  1997. begin
  1998. Result := False;
  1999. Exit;
  2000. end
  2001. else if ([Ch_WOp4]*Ch<>[]) then
  2002. begin
  2003. if Reg1WriteOverwritesReg2Entirely(p.oper[3]^.reg, reg) then
  2004. Result := True
  2005. else
  2006. begin
  2007. Result := False;
  2008. Exit;
  2009. end;
  2010. end;
  2011. end;
  2012. end;
  2013. end;
  2014. end;
  2015. end;
  2016. { Don't do these ones first in case an input operand is equal to an explicit output register }
  2017. case getsupreg(reg) of
  2018. RS_EAX:
  2019. if ([Ch_WEAX{$ifdef x86_64},Ch_WRAX{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EAX, reg) then
  2020. begin
  2021. Result := True;
  2022. Exit;
  2023. end;
  2024. RS_ECX:
  2025. if ([Ch_WECX{$ifdef x86_64},Ch_WRCX{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_ECX, reg) then
  2026. begin
  2027. Result := True;
  2028. Exit;
  2029. end;
  2030. RS_EDX:
  2031. if ([Ch_REDX{$ifdef x86_64},Ch_WRDX{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EDX, reg) then
  2032. begin
  2033. Result := True;
  2034. Exit;
  2035. end;
  2036. RS_EBX:
  2037. if ([Ch_WEBX{$ifdef x86_64},Ch_WRBX{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EBX, reg) then
  2038. begin
  2039. Result := True;
  2040. Exit;
  2041. end;
  2042. RS_ESP:
  2043. if ([Ch_WESP{$ifdef x86_64},Ch_WRSP{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_ESP, reg) then
  2044. begin
  2045. Result := True;
  2046. Exit;
  2047. end;
  2048. RS_EBP:
  2049. if ([Ch_WEBP{$ifdef x86_64},Ch_WRBP{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EBP, reg) then
  2050. begin
  2051. Result := True;
  2052. Exit;
  2053. end;
  2054. RS_ESI:
  2055. if ([Ch_WESI{$ifdef x86_64},Ch_WRSI{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_ESI, reg) then
  2056. begin
  2057. Result := True;
  2058. Exit;
  2059. end;
  2060. RS_EDI:
  2061. if ([Ch_WEDI{$ifdef x86_64},Ch_WRDI{$endif x86_64}]*Ch<>[]) and Reg1WriteOverwritesReg2Entirely(NR_EDI, reg) then
  2062. begin
  2063. Result := True;
  2064. Exit;
  2065. end;
  2066. else
  2067. ;
  2068. end;
  2069. end;
  2070. end;
  2071. end;
  2072. end;
  2073. end;
  2074. class function TX86AsmOptimizer.IsExitCode(p : tai) : boolean;
  2075. var
  2076. hp2,hp3 : tai;
  2077. begin
  2078. { some x86-64 issue a NOP before the real exit code }
  2079. if MatchInstruction(p,A_NOP,[]) then
  2080. GetNextInstruction(p,p);
  2081. result:=assigned(p) and (p.typ=ait_instruction) and
  2082. ((taicpu(p).opcode = A_RET) or
  2083. ((taicpu(p).opcode=A_LEAVE) and
  2084. GetNextInstruction(p,hp2) and
  2085. MatchInstruction(hp2,A_RET,[S_NO])
  2086. ) or
  2087. (((taicpu(p).opcode=A_LEA) and
  2088. MatchOpType(taicpu(p),top_ref,top_reg) and
  2089. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  2090. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  2091. ) and
  2092. GetNextInstruction(p,hp2) and
  2093. MatchInstruction(hp2,A_RET,[S_NO])
  2094. ) or
  2095. ((((taicpu(p).opcode=A_MOV) and
  2096. MatchOpType(taicpu(p),top_reg,top_reg) and
  2097. (taicpu(p).oper[0]^.reg=current_procinfo.framepointer) and
  2098. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)) or
  2099. ((taicpu(p).opcode=A_LEA) and
  2100. MatchOpType(taicpu(p),top_ref,top_reg) and
  2101. (taicpu(p).oper[0]^.ref^.base=current_procinfo.framepointer) and
  2102. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  2103. )
  2104. ) and
  2105. GetNextInstruction(p,hp2) and
  2106. MatchInstruction(hp2,A_POP,[reg2opsize(current_procinfo.framepointer)]) and
  2107. MatchOpType(taicpu(hp2),top_reg) and
  2108. (taicpu(hp2).oper[0]^.reg=current_procinfo.framepointer) and
  2109. GetNextInstruction(hp2,hp3) and
  2110. MatchInstruction(hp3,A_RET,[S_NO])
  2111. )
  2112. );
  2113. end;
  2114. class function TX86AsmOptimizer.isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  2115. begin
  2116. isFoldableArithOp := False;
  2117. case hp1.opcode of
  2118. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  2119. isFoldableArithOp :=
  2120. ((taicpu(hp1).oper[0]^.typ = top_const) or
  2121. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  2122. (taicpu(hp1).oper[0]^.reg <> reg))) and
  2123. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2124. (taicpu(hp1).oper[1]^.reg = reg);
  2125. A_INC,A_DEC,A_NEG,A_NOT:
  2126. isFoldableArithOp :=
  2127. (taicpu(hp1).oper[0]^.typ = top_reg) and
  2128. (taicpu(hp1).oper[0]^.reg = reg);
  2129. else
  2130. ;
  2131. end;
  2132. end;
  2133. procedure TX86AsmOptimizer.RemoveLastDeallocForFuncRes(p: tai);
  2134. procedure DoRemoveLastDeallocForFuncRes( supreg: tsuperregister);
  2135. var
  2136. hp2: tai;
  2137. begin
  2138. hp2 := p;
  2139. repeat
  2140. hp2 := tai(hp2.previous);
  2141. if assigned(hp2) and
  2142. (hp2.typ = ait_regalloc) and
  2143. (tai_regalloc(hp2).ratype=ra_dealloc) and
  2144. (getregtype(tai_regalloc(hp2).reg) = R_INTREGISTER) and
  2145. (getsupreg(tai_regalloc(hp2).reg) = supreg) then
  2146. begin
  2147. RemoveInstruction(hp2);
  2148. break;
  2149. end;
  2150. until not(assigned(hp2)) or regInInstruction(newreg(R_INTREGISTER,supreg,R_SUBWHOLE),hp2);
  2151. end;
  2152. begin
  2153. case current_procinfo.procdef.returndef.typ of
  2154. arraydef,recorddef,pointerdef,
  2155. stringdef,enumdef,procdef,objectdef,errordef,
  2156. filedef,setdef,procvardef,
  2157. classrefdef,forwarddef:
  2158. DoRemoveLastDeallocForFuncRes(RS_EAX);
  2159. orddef:
  2160. if current_procinfo.procdef.returndef.size <> 0 then
  2161. begin
  2162. DoRemoveLastDeallocForFuncRes(RS_EAX);
  2163. { for int64/qword }
  2164. if current_procinfo.procdef.returndef.size = 8 then
  2165. DoRemoveLastDeallocForFuncRes(RS_EDX);
  2166. end;
  2167. else
  2168. ;
  2169. end;
  2170. end;
  2171. function TX86AsmOptimizer.OptPass1CMOVcc(var p: tai): Boolean;
  2172. var
  2173. hp1: tai;
  2174. operswap: poper;
  2175. begin
  2176. Result := False;
  2177. { Optimise:
  2178. cmov(c) %reg1,%reg2
  2179. mov %reg2,%reg1
  2180. (%reg2 dealloc.)
  2181. To:
  2182. cmov(~c) %reg2,%reg1
  2183. }
  2184. if (taicpu(p).oper[0]^.typ = top_reg) then
  2185. while GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[1]^.reg) and
  2186. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  2187. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) and
  2188. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) do
  2189. begin
  2190. TransferUsedRegs(TmpUsedRegs);
  2191. UpdateUsedRegsBetween(TmpUsedRegs, p, hp1);
  2192. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) then
  2193. begin
  2194. DebugMsg(SPeepholeOptimization + 'CMOV(c) %reg1,%reg2; MOV %reg2,%reg1 -> CMOV(~c) %reg2,%reg1 (CMovMov2CMov)', p);
  2195. { Save time by swapping the pointers (they're both registers, so
  2196. we don't need to worry about reference counts) }
  2197. operswap := taicpu(p).oper[0];
  2198. taicpu(p).oper[0] := taicpu(p).oper[1];
  2199. taicpu(p).oper[1] := operswap;
  2200. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  2201. RemoveInstruction(hp1);
  2202. { It's still a CMOV, so we can look further ahead }
  2203. Include(OptsToCheck, aoc_ForceNewIteration);
  2204. { But first, let's see if this will get optimised again
  2205. (probably won't happen, but best to be sure) }
  2206. Continue;
  2207. end;
  2208. Break;
  2209. end;
  2210. end;
  2211. function TX86AsmOptimizer.OptPass1_V_MOVAP(var p : tai) : boolean;
  2212. var
  2213. hp1,hp2 : tai;
  2214. begin
  2215. result:=false;
  2216. if MatchOpType(taicpu(p),top_reg,top_reg) then
  2217. begin
  2218. { vmova* reg1,reg1
  2219. =>
  2220. <nop> }
  2221. if taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg then
  2222. begin
  2223. RemoveCurrentP(p);
  2224. result:=true;
  2225. exit;
  2226. end;
  2227. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) and
  2228. (hp1.typ = ait_instruction) and
  2229. (
  2230. { Under -O2 and below, the instructions are always adjacent }
  2231. not (cs_opt_level3 in current_settings.optimizerswitches) or
  2232. (taicpu(hp1).ops <= 1) or
  2233. not RegInOp(taicpu(p).oper[0]^.reg, taicpu(hp1).oper[1]^) or
  2234. { If reg1 = reg3, reg1 must not be modified in between }
  2235. not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)
  2236. ) then
  2237. begin
  2238. if MatchInstruction(hp1,[taicpu(p).opcode],[S_NO]) and
  2239. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  2240. begin
  2241. { vmova* reg1,reg2
  2242. ...
  2243. vmova* reg2,reg3
  2244. dealloc reg2
  2245. =>
  2246. vmova* reg1,reg3 }
  2247. TransferUsedRegs(TmpUsedRegs);
  2248. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2249. if MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2250. not RegUsedBetween(taicpu(hp1).oper[1]^.reg, p, hp1) and
  2251. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2252. begin
  2253. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 1',p);
  2254. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  2255. TransferUsedRegs(TmpUsedRegs);
  2256. AllocRegBetween(taicpu(hp1).oper[1]^.reg, p, hp1, TmpUsedRegs);
  2257. RemoveInstruction(hp1);
  2258. result:=true;
  2259. exit;
  2260. end;
  2261. { special case:
  2262. vmova* reg1,<op>
  2263. ...
  2264. vmova* <op>,reg1
  2265. =>
  2266. vmova* reg1,<op> }
  2267. if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  2268. ((taicpu(p).oper[0]^.typ<>top_ref) or
  2269. (not(vol_read in taicpu(p).oper[0]^.ref^.volatility))
  2270. ) then
  2271. begin
  2272. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 2',p);
  2273. RemoveInstruction(hp1);
  2274. result:=true;
  2275. exit;
  2276. end
  2277. end
  2278. else if ((MatchInstruction(p,[A_MOVAPS,A_VMOVAPS],[S_NO]) and
  2279. MatchInstruction(hp1,[A_MOVSS,A_VMOVSS],[S_NO])) or
  2280. ((MatchInstruction(p,[A_MOVAPD,A_VMOVAPD],[S_NO]) and
  2281. MatchInstruction(hp1,[A_MOVSD,A_VMOVSD],[S_NO])))
  2282. ) and
  2283. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  2284. begin
  2285. { vmova* reg1,reg2
  2286. ...
  2287. vmovs* reg2,<op>
  2288. dealloc reg2
  2289. =>
  2290. vmovs* reg1,<op> }
  2291. TransferUsedRegs(TmpUsedRegs);
  2292. UpdateUsedRegsBetween(TmpUsedRegs, p, hp1);
  2293. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2294. begin
  2295. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVS*2(V)MOVS* 1',p);
  2296. taicpu(p).opcode:=taicpu(hp1).opcode;
  2297. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  2298. TransferUsedRegs(TmpUsedRegs);
  2299. AllocRegBetween(taicpu(p).oper[0]^.reg, p, hp1, TmpUsedRegs);
  2300. RemoveInstruction(hp1);
  2301. result:=true;
  2302. exit;
  2303. end
  2304. end;
  2305. if MatchInstruction(hp1,[A_VFMADDPD,
  2306. A_VFMADD132PD,
  2307. A_VFMADD132PS,
  2308. A_VFMADD132SD,
  2309. A_VFMADD132SS,
  2310. A_VFMADD213PD,
  2311. A_VFMADD213PS,
  2312. A_VFMADD213SD,
  2313. A_VFMADD213SS,
  2314. A_VFMADD231PD,
  2315. A_VFMADD231PS,
  2316. A_VFMADD231SD,
  2317. A_VFMADD231SS,
  2318. A_VFMADDSUB132PD,
  2319. A_VFMADDSUB132PS,
  2320. A_VFMADDSUB213PD,
  2321. A_VFMADDSUB213PS,
  2322. A_VFMADDSUB231PD,
  2323. A_VFMADDSUB231PS,
  2324. A_VFMSUB132PD,
  2325. A_VFMSUB132PS,
  2326. A_VFMSUB132SD,
  2327. A_VFMSUB132SS,
  2328. A_VFMSUB213PD,
  2329. A_VFMSUB213PS,
  2330. A_VFMSUB213SD,
  2331. A_VFMSUB213SS,
  2332. A_VFMSUB231PD,
  2333. A_VFMSUB231PS,
  2334. A_VFMSUB231SD,
  2335. A_VFMSUB231SS,
  2336. A_VFMSUBADD132PD,
  2337. A_VFMSUBADD132PS,
  2338. A_VFMSUBADD213PD,
  2339. A_VFMSUBADD213PS,
  2340. A_VFMSUBADD231PD,
  2341. A_VFMSUBADD231PS,
  2342. A_VFNMADD132PD,
  2343. A_VFNMADD132PS,
  2344. A_VFNMADD132SD,
  2345. A_VFNMADD132SS,
  2346. A_VFNMADD213PD,
  2347. A_VFNMADD213PS,
  2348. A_VFNMADD213SD,
  2349. A_VFNMADD213SS,
  2350. A_VFNMADD231PD,
  2351. A_VFNMADD231PS,
  2352. A_VFNMADD231SD,
  2353. A_VFNMADD231SS,
  2354. A_VFNMSUB132PD,
  2355. A_VFNMSUB132PS,
  2356. A_VFNMSUB132SD,
  2357. A_VFNMSUB132SS,
  2358. A_VFNMSUB213PD,
  2359. A_VFNMSUB213PS,
  2360. A_VFNMSUB213SD,
  2361. A_VFNMSUB213SS,
  2362. A_VFNMSUB231PD,
  2363. A_VFNMSUB231PS,
  2364. A_VFNMSUB231SD,
  2365. A_VFNMSUB231SS],[S_NO]) and
  2366. { we mix single and double opperations here because we assume that the compiler
  2367. generates vmovapd only after double operations and vmovaps only after single operations }
  2368. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[2]^.reg) and
  2369. GetNextInstructionUsingReg(hp1, hp2, taicpu(hp1).oper[2]^.reg) and
  2370. MatchInstruction(hp2,[A_VMOVAPD,A_VMOVAPS,A_MOVAPD,A_MOVAPS],[S_NO]) and
  2371. MatchOperand(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) then
  2372. begin
  2373. TransferUsedRegs(TmpUsedRegs);
  2374. UpdateUsedRegsBetween(TmpUsedRegs, p, hp2);
  2375. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  2376. begin
  2377. taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
  2378. if (cs_opt_level3 in current_settings.optimizerswitches) then
  2379. RemoveCurrentP(p)
  2380. else
  2381. RemoveCurrentP(p, hp1); // hp1 is guaranteed to be the immediate next instruction in this case.
  2382. RemoveInstruction(hp2);
  2383. end;
  2384. end
  2385. else if (hp1.typ = ait_instruction) and
  2386. (((taicpu(p).opcode=A_MOVAPS) and
  2387. ((taicpu(hp1).opcode=A_ADDSS) or (taicpu(hp1).opcode=A_SUBSS) or
  2388. (taicpu(hp1).opcode=A_MULSS) or (taicpu(hp1).opcode=A_DIVSS))) or
  2389. ((taicpu(p).opcode=A_MOVAPD) and
  2390. ((taicpu(hp1).opcode=A_ADDSD) or (taicpu(hp1).opcode=A_SUBSD) or
  2391. (taicpu(hp1).opcode=A_MULSD) or (taicpu(hp1).opcode=A_DIVSD)))
  2392. ) and
  2393. GetNextInstructionUsingReg(hp1, hp2, taicpu(hp1).oper[1]^.reg) and
  2394. MatchInstruction(hp2,taicpu(p).opcode,[]) and
  2395. OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  2396. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  2397. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) then
  2398. { change
  2399. movapX reg,reg2
  2400. addsX/subsX/... reg3, reg2
  2401. movapX reg2,reg
  2402. to
  2403. addsX/subsX/... reg3,reg
  2404. }
  2405. begin
  2406. TransferUsedRegs(TmpUsedRegs);
  2407. UpdateUsedRegsBetween(TmpUsedRegs, p, hp2);
  2408. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  2409. begin
  2410. DebugMsg(SPeepholeOptimization + 'MovapXOpMovapX2Op ('+
  2411. debug_op2str(taicpu(p).opcode)+' '+
  2412. debug_op2str(taicpu(hp1).opcode)+' '+
  2413. debug_op2str(taicpu(hp2).opcode)+') done',p);
  2414. { we cannot eliminate the first move if
  2415. the operations uses the same register for source and dest }
  2416. if not(OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp1).oper[0]^)) then
  2417. { Remember that hp1 is not necessarily the immediate
  2418. next instruction }
  2419. RemoveCurrentP(p);
  2420. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  2421. RemoveInstruction(hp2);
  2422. result:=true;
  2423. end;
  2424. end
  2425. else if (hp1.typ = ait_instruction) and
  2426. (((taicpu(p).opcode=A_VMOVAPD) and
  2427. (taicpu(hp1).opcode=A_VCOMISD)) or
  2428. ((taicpu(p).opcode=A_VMOVAPS) and
  2429. ((taicpu(hp1).opcode=A_VCOMISS))
  2430. )
  2431. ) and not(OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp1).oper[0]^)) then
  2432. { change
  2433. movapX reg,reg1
  2434. vcomisX reg1,reg1
  2435. to
  2436. vcomisX reg,reg
  2437. }
  2438. begin
  2439. TransferUsedRegs(TmpUsedRegs);
  2440. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2441. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2442. begin
  2443. DebugMsg(SPeepholeOptimization + 'MovapXComisX2ComisX2 ('+
  2444. debug_op2str(taicpu(p).opcode)+' '+
  2445. debug_op2str(taicpu(hp1).opcode)+') done',p);
  2446. if OpsEqual(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  2447. taicpu(hp1).loadoper(0, taicpu(p).oper[0]^);
  2448. if OpsEqual(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) then
  2449. taicpu(hp1).loadoper(1, taicpu(p).oper[0]^);
  2450. RemoveCurrentP(p);
  2451. result:=true;
  2452. exit;
  2453. end;
  2454. end
  2455. end;
  2456. end;
  2457. end;
  2458. function TX86AsmOptimizer.OptPass1VOP(var p : tai) : boolean;
  2459. var
  2460. hp1 : tai;
  2461. begin
  2462. result:=false;
  2463. { replace
  2464. V<Op>X %mreg1,%mreg2,%mreg3
  2465. VMovX %mreg3,%mreg4
  2466. dealloc %mreg3
  2467. by
  2468. V<Op>X %mreg1,%mreg2,%mreg4
  2469. ?
  2470. }
  2471. if GetNextInstruction(p,hp1) and
  2472. { we mix single and double operations here because we assume that the compiler
  2473. generates vmovapd only after double operations and vmovaps only after single operations }
  2474. MatchInstruction(hp1,A_VMOVAPD,A_VMOVAPS,[S_NO]) and
  2475. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  2476. (taicpu(hp1).oper[1]^.typ=top_reg) then
  2477. begin
  2478. TransferUsedRegs(TmpUsedRegs);
  2479. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2480. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  2481. begin
  2482. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  2483. DebugMsg(SPeepholeOptimization + 'VOpVmov2VOp done',p);
  2484. RemoveInstruction(hp1);
  2485. result:=true;
  2486. end;
  2487. end;
  2488. end;
  2489. { Replaces all references to AOldReg in a memory reference to ANewReg }
  2490. class function TX86AsmOptimizer.ReplaceRegisterInRef(var ref: TReference; const AOldReg, ANewReg: TRegister): Boolean;
  2491. begin
  2492. Result := False;
  2493. { For safety reasons, only check for exact register matches }
  2494. { Check base register }
  2495. if (ref.base = AOldReg) then
  2496. begin
  2497. ref.base := ANewReg;
  2498. Result := True;
  2499. end;
  2500. { Check index register }
  2501. if (ref.index = AOldReg) and (getsupreg(ANewReg)<>RS_ESP) then
  2502. begin
  2503. ref.index := ANewReg;
  2504. Result := True;
  2505. end;
  2506. end;
  2507. { Replaces all references to AOldReg in an operand to ANewReg }
  2508. class function TX86AsmOptimizer.ReplaceRegisterInOper(const p: taicpu; const OperIdx: Integer; const AOldReg, ANewReg: TRegister): Boolean;
  2509. var
  2510. OldSupReg, NewSupReg: TSuperRegister;
  2511. OldSubReg, NewSubReg: TSubRegister;
  2512. OldRegType: TRegisterType;
  2513. ThisOper: POper;
  2514. begin
  2515. ThisOper := p.oper[OperIdx]; { Faster to access overall }
  2516. Result := False;
  2517. if (AOldReg = NR_NO) or (ANewReg = NR_NO) then
  2518. InternalError(2020011801);
  2519. OldSupReg := getsupreg(AOldReg);
  2520. OldSubReg := getsubreg(AOldReg);
  2521. OldRegType := getregtype(AOldReg);
  2522. NewSupReg := getsupreg(ANewReg);
  2523. NewSubReg := getsubreg(ANewReg);
  2524. if OldRegType <> getregtype(ANewReg) then
  2525. InternalError(2020011802);
  2526. if OldSubReg <> NewSubReg then
  2527. InternalError(2020011803);
  2528. case ThisOper^.typ of
  2529. top_reg:
  2530. if (
  2531. (ThisOper^.reg = AOldReg) or
  2532. (
  2533. (OldRegType = R_INTREGISTER) and
  2534. (getsupreg(ThisOper^.reg) = OldSupReg) and
  2535. (getregtype(ThisOper^.reg) = R_INTREGISTER) and
  2536. (
  2537. (getsubreg(ThisOper^.reg) <= OldSubReg)
  2538. {$ifndef x86_64}
  2539. and (
  2540. { Under i386 and i8086, ESI, EDI, EBP and ESP
  2541. don't have an 8-bit representation }
  2542. (getsubreg(ThisOper^.reg) >= R_SUBW) or
  2543. not (NewSupReg in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  2544. )
  2545. {$endif x86_64}
  2546. )
  2547. )
  2548. ) then
  2549. begin
  2550. ThisOper^.reg := newreg(getregtype(ANewReg), NewSupReg, getsubreg(p.oper[OperIdx]^.reg));
  2551. Result := True;
  2552. end;
  2553. top_ref:
  2554. if ReplaceRegisterInRef(ThisOper^.ref^, AOldReg, ANewReg) then
  2555. Result := True;
  2556. else
  2557. ;
  2558. end;
  2559. end;
  2560. { Replaces all references to AOldReg in an instruction to ANewReg }
  2561. class function TX86AsmOptimizer.ReplaceRegisterInInstruction(const p: taicpu; const AOldReg, ANewReg: TRegister): Boolean;
  2562. const
  2563. ReadFlag: array[0..3] of TInsChange = (Ch_Rop1, Ch_Rop2, Ch_Rop3, Ch_Rop4);
  2564. var
  2565. OperIdx: Integer;
  2566. begin
  2567. Result := False;
  2568. for OperIdx := 0 to p.ops - 1 do
  2569. if (ReadFlag[OperIdx] in InsProp[p.Opcode].Ch) then
  2570. begin
  2571. { The shift and rotate instructions can only use CL }
  2572. if not (
  2573. (OperIdx = 0) and
  2574. { This second condition just helps to avoid unnecessarily
  2575. calling MatchInstruction for 10 different opcodes }
  2576. (p.oper[0]^.reg = NR_CL) and
  2577. MatchInstruction(p, [A_RCL, A_RCR, A_ROL, A_ROR, A_SAL, A_SAR, A_SHL, A_SHLD, A_SHR, A_SHRD], [])
  2578. ) then
  2579. Result := ReplaceRegisterInOper(p, OperIdx, AOldReg, ANewReg) or Result;
  2580. end
  2581. else if p.oper[OperIdx]^.typ = top_ref then
  2582. { It's okay to replace registers in references that get written to }
  2583. Result := ReplaceRegisterInOper(p, OperIdx, AOldReg, ANewReg) or Result;
  2584. end;
  2585. class function TX86AsmOptimizer.IsRefSafe(const ref: PReference): Boolean;
  2586. begin
  2587. Result :=
  2588. (ref^.index = NR_NO) and
  2589. (
  2590. {$ifdef x86_64}
  2591. (
  2592. (ref^.base = NR_RIP) and
  2593. (ref^.refaddr in [addr_pic, addr_pic_no_got])
  2594. ) or
  2595. {$endif x86_64}
  2596. (ref^.refaddr = addr_full) or
  2597. (ref^.base = NR_STACK_POINTER_REG) or
  2598. (ref^.base = current_procinfo.framepointer)
  2599. );
  2600. end;
  2601. function TX86AsmOptimizer.ConvertLEA(const p: taicpu): Boolean;
  2602. var
  2603. l: asizeint;
  2604. begin
  2605. Result := False;
  2606. { Should have been checked previously }
  2607. if p.opcode <> A_LEA then
  2608. InternalError(2020072501);
  2609. { do not mess with the stack point as adjusting it by lea is recommend, except if we optimize for size }
  2610. if (p.oper[1]^.reg=NR_STACK_POINTER_REG) and
  2611. not(cs_opt_size in current_settings.optimizerswitches) then
  2612. exit;
  2613. with p.oper[0]^.ref^ do
  2614. begin
  2615. if (base <> p.oper[1]^.reg) or
  2616. (index <> NR_NO) or
  2617. assigned(symbol) then
  2618. exit;
  2619. l:=offset;
  2620. if (l=1) and UseIncDec then
  2621. begin
  2622. p.opcode:=A_INC;
  2623. p.loadreg(0,p.oper[1]^.reg);
  2624. p.ops:=1;
  2625. DebugMsg(SPeepholeOptimization + 'Lea2Inc done',p);
  2626. end
  2627. else if (l=-1) and UseIncDec then
  2628. begin
  2629. p.opcode:=A_DEC;
  2630. p.loadreg(0,p.oper[1]^.reg);
  2631. p.ops:=1;
  2632. DebugMsg(SPeepholeOptimization + 'Lea2Dec done',p);
  2633. end
  2634. else
  2635. begin
  2636. if (l<0) and (l<>-2147483648) then
  2637. begin
  2638. p.opcode:=A_SUB;
  2639. p.loadConst(0,-l);
  2640. DebugMsg(SPeepholeOptimization + 'Lea2Sub done',p);
  2641. end
  2642. else
  2643. begin
  2644. p.opcode:=A_ADD;
  2645. p.loadConst(0,l);
  2646. DebugMsg(SPeepholeOptimization + 'Lea2Add done',p);
  2647. end;
  2648. end;
  2649. end;
  2650. Result := True;
  2651. end;
  2652. function TX86AsmOptimizer.DeepMOVOpt(const p_mov: taicpu; const hp: taicpu): Boolean;
  2653. var
  2654. CurrentReg, ReplaceReg: TRegister;
  2655. begin
  2656. Result := False;
  2657. ReplaceReg := taicpu(p_mov).oper[0]^.reg;
  2658. CurrentReg := taicpu(p_mov).oper[1]^.reg;
  2659. case hp.opcode of
  2660. A_FSTSW, A_FNSTSW,
  2661. A_IN, A_INS, A_OUT, A_OUTS,
  2662. A_CMPS, A_LODS, A_MOVS, A_SCAS, A_STOS:
  2663. { These routines have explicit operands, but they are restricted in
  2664. what they can be (e.g. IN and OUT can only read from AL, AX or
  2665. EAX. }
  2666. Exit;
  2667. A_IMUL:
  2668. begin
  2669. { The 1-operand version writes to implicit registers
  2670. The 2-operand version reads from the first operator, and reads
  2671. from and writes to the second (equivalent to Ch_ROp1, ChRWOp2).
  2672. the 3-operand version reads from a register that it doesn't write to
  2673. }
  2674. case hp.ops of
  2675. 1:
  2676. if (
  2677. (
  2678. (hp.opsize = S_B) and (getsupreg(CurrentReg) <> RS_EAX)
  2679. ) or
  2680. not (getsupreg(CurrentReg) in [RS_EAX, RS_EDX])
  2681. ) and ReplaceRegisterInOper(hp, 0, CurrentReg, ReplaceReg) then
  2682. begin
  2683. Result := True;
  2684. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 1)', hp);
  2685. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  2686. end;
  2687. 2:
  2688. { Only modify the first parameter }
  2689. if ReplaceRegisterInOper(hp, 0, CurrentReg, ReplaceReg) then
  2690. begin
  2691. Result := True;
  2692. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 2)', hp);
  2693. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  2694. end;
  2695. 3:
  2696. { Only modify the second parameter }
  2697. if ReplaceRegisterInOper(hp, 1, CurrentReg, ReplaceReg) then
  2698. begin
  2699. Result := True;
  2700. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 3)', hp);
  2701. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  2702. end;
  2703. else
  2704. InternalError(2020012901);
  2705. end;
  2706. end;
  2707. else
  2708. if (hp.ops > 0) and
  2709. ReplaceRegisterInInstruction(hp, CurrentReg, ReplaceReg) then
  2710. begin
  2711. Result := True;
  2712. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovXXX2MovXXX)', hp);
  2713. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  2714. end;
  2715. end;
  2716. end;
  2717. function TX86AsmOptimizer.FuncMov2Func(var p: tai; const hp1: tai): Boolean;
  2718. var
  2719. hp2, hp_regalloc: tai;
  2720. p_SourceReg, p_TargetReg: TRegister;
  2721. begin
  2722. Result := False;
  2723. { Backward optimisation. If we have:
  2724. func. %reg1,%reg2
  2725. mov %reg2,%reg3
  2726. (dealloc %reg2)
  2727. Change to:
  2728. func. %reg1,%reg3 (see comment below for what a valid func. is)
  2729. Perform similar optimisations with 1, 3 and 4-operand instructions
  2730. that only have one output.
  2731. }
  2732. if MatchOpType(taicpu(p), top_reg, top_reg) then
  2733. begin
  2734. p_SourceReg := taicpu(p).oper[0]^.reg;
  2735. p_TargetReg := taicpu(p).oper[1]^.reg;
  2736. TransferUsedRegs(TmpUsedRegs);
  2737. if not RegUsedAfterInstruction(p_SourceReg, p, TmpUsedRegs) and
  2738. GetLastInstruction(p, hp2) and
  2739. (hp2.typ = ait_instruction) and
  2740. { Have to make sure it's an instruction that only reads from
  2741. the first operands and only writes (not reads or modifies) to
  2742. the last one; in essence, a pure function such as BSR, POPCNT
  2743. or ANDN }
  2744. (
  2745. (
  2746. (taicpu(hp2).ops = 1) and
  2747. (insprop[taicpu(hp2).opcode].Ch * [Ch_Wop1] = [Ch_Wop1])
  2748. ) or
  2749. (
  2750. (taicpu(hp2).ops = 2) and
  2751. (insprop[taicpu(hp2).opcode].Ch * [Ch_Rop1, Ch_Wop2] = [Ch_Rop1, Ch_Wop2])
  2752. ) or
  2753. (
  2754. (taicpu(hp2).ops = 3) and
  2755. (insprop[taicpu(hp2).opcode].Ch * [Ch_Rop1, Ch_Rop2, Ch_Wop3] = [Ch_Rop1, Ch_Rop2, Ch_Wop3])
  2756. ) or
  2757. (
  2758. (taicpu(hp2).ops = 4) and
  2759. (insprop[taicpu(hp2).opcode].Ch * [Ch_Rop1, Ch_Rop2, Ch_Rop3, Ch_Wop4] = [Ch_Rop1, Ch_Rop2, Ch_Rop3, Ch_Wop4])
  2760. )
  2761. ) and
  2762. (taicpu(hp2).oper[taicpu(hp2).ops-1]^.typ = top_reg) and
  2763. (taicpu(hp2).oper[taicpu(hp2).ops-1]^.reg = p_SourceReg) then
  2764. begin
  2765. case taicpu(hp2).opcode of
  2766. A_FSTSW, A_FNSTSW,
  2767. A_IN, A_INS, A_OUT, A_OUTS,
  2768. A_CMPS, A_LODS, A_MOVS, A_SCAS, A_STOS:
  2769. { These routines have explicit operands, but they are restricted in
  2770. what they can be (e.g. IN and OUT can only read from AL, AX or
  2771. EAX. }
  2772. ;
  2773. else
  2774. begin
  2775. DebugMsg(SPeepholeOptimization + 'Removed MOV and changed destination on previous instruction to optimise register usage (FuncMov2Func)', p);
  2776. { if %reg2 (p_SourceReg) is allocated before func., remove it completely }
  2777. hp_regalloc := FindRegAllocBackward(p_SourceReg, hp2);
  2778. if Assigned(hp_regalloc) then
  2779. begin
  2780. Asml.Remove(hp_regalloc);
  2781. if Assigned(FindRegDealloc(p_SourceReg, p)) then
  2782. begin
  2783. ExcludeRegFromUsedRegs(p_SourceReg, UsedRegs);
  2784. hp_regalloc.Free;
  2785. end
  2786. else
  2787. { If the register is not explicitly deallocated, it's
  2788. being reused, so move the allocation to after func. }
  2789. AsmL.InsertAfter(hp_regalloc, hp2);
  2790. end;
  2791. if not RegInInstruction(p_TargetReg, hp2) then
  2792. begin
  2793. TransferUsedRegs(TmpUsedRegs);
  2794. AllocRegBetween(p_TargetReg, hp2, p, TmpUsedRegs);
  2795. end;
  2796. { Actually make the changes }
  2797. taicpu(hp2).oper[taicpu(hp2).ops-1]^.reg := p_TargetReg;
  2798. RemoveCurrentp(p, hp1);
  2799. { If the Func was another MOV instruction, we might get
  2800. "mov %reg,%reg" that doesn't get removed in Pass 2
  2801. otherwise, so deal with it here (also do something
  2802. similar with lea (%reg),%reg}
  2803. if (taicpu(hp2).opcode = A_MOV) and MatchOperand(taicpu(hp2).oper[0]^, taicpu(hp2).oper[1]^.reg) then
  2804. begin
  2805. DebugMsg(SPeepholeOptimization + 'Mov2Nop 1a done', hp2);
  2806. if p = hp2 then
  2807. RemoveCurrentp(p)
  2808. else
  2809. RemoveInstruction(hp2);
  2810. end;
  2811. Result := True;
  2812. Exit;
  2813. end;
  2814. end;
  2815. end;
  2816. end;
  2817. end;
  2818. function TX86AsmOptimizer.CheckMovMov2MovMov2(const p, hp1: tai) : boolean;
  2819. begin
  2820. Result := False;
  2821. if MatchOpType(taicpu(p),top_ref,top_reg) and
  2822. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  2823. (taicpu(p).opsize = taicpu(hp1).opsize) and
  2824. RefsEqual(taicpu(p).oper[0]^.ref^,taicpu(hp1).oper[0]^.ref^) and
  2825. (taicpu(p).oper[0]^.ref^.volatility=[]) and
  2826. (taicpu(hp1).oper[0]^.ref^.volatility=[]) and
  2827. not(SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^.base)) and
  2828. not(SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^.index)) then
  2829. begin
  2830. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 2',p);
  2831. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  2832. Result := True;
  2833. Include(OptsToCheck, aoc_ForceNewIteration);
  2834. end;
  2835. end;
  2836. function TX86AsmOptimizer.OptPass1MOV(var p : tai) : boolean;
  2837. var
  2838. hp1, hp2, hp3, hp4, last_hp1: tai;
  2839. GetNextInstruction_p, DoOptimisation, TempBool: Boolean;
  2840. p_SourceReg, p_TargetReg, NewMMReg: TRegister;
  2841. {$ifdef x86_64}
  2842. NewConst: TCGInt;
  2843. {$endif x86_64}
  2844. procedure convert_mov_value(signed_movop: tasmop; max_value: tcgint); inline;
  2845. begin
  2846. if taicpu(hp1).opcode = signed_movop then
  2847. begin
  2848. if taicpu(p).oper[0]^.val > max_value shr 1 then
  2849. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val - max_value - 1 { Convert to signed }
  2850. end
  2851. else
  2852. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and max_value; { Trim to unsigned }
  2853. end;
  2854. function GetNextHp1(const in_p: tai): Boolean;
  2855. begin
  2856. if NotFirstIteration and (cs_opt_level3 in current_settings.optimizerswitches) then
  2857. GetNextInstruction_p := GetNextInstructionUsingReg(in_p, hp1, p_TargetReg)
  2858. else
  2859. GetNextInstruction_p := GetNextInstruction(in_p, hp1);
  2860. Result := GetNextInstruction_p and (hp1.typ = ait_instruction);
  2861. end;
  2862. function TryConstMerge(var p1, p2: tai): Boolean;
  2863. var
  2864. ThisRef: TReference;
  2865. begin
  2866. Result := False;
  2867. ThisRef := taicpu(p2).oper[1]^.ref^;
  2868. { Only permit writes to the stack, since we can guarantee alignment with that }
  2869. if (ThisRef.index = NR_NO) and
  2870. (
  2871. (ThisRef.base = NR_STACK_POINTER_REG) or
  2872. (ThisRef.base = current_procinfo.framepointer)
  2873. ) then
  2874. begin
  2875. case taicpu(p).opsize of
  2876. S_B:
  2877. begin
  2878. { Word writes must be on a 2-byte boundary }
  2879. if (taicpu(p1).oper[1]^.ref^.offset mod 2) = 0 then
  2880. begin
  2881. { Reduce offset of second reference to see if it is sequential with the first }
  2882. Dec(ThisRef.offset, 1);
  2883. if RefsEqual(taicpu(p1).oper[1]^.ref^, ThisRef) then
  2884. begin
  2885. { Make sure the constants aren't represented as a
  2886. negative number, as these won't merge properly }
  2887. taicpu(p1).opsize := S_W;
  2888. taicpu(p1).oper[0]^.val := (taicpu(p1).oper[0]^.val and $FF) or ((taicpu(p2).oper[0]^.val and $FF) shl 8);
  2889. DebugMsg(SPeepholeOptimization + 'Merged two byte-sized constant writes to stack (MovMov2Mov 2a)', p1);
  2890. RemoveInstruction(p2);
  2891. Result := True;
  2892. end;
  2893. end;
  2894. end;
  2895. S_W:
  2896. begin
  2897. { Longword writes must be on a 4-byte boundary }
  2898. if (taicpu(p1).oper[1]^.ref^.offset mod 4) = 0 then
  2899. begin
  2900. { Reduce offset of second reference to see if it is sequential with the first }
  2901. Dec(ThisRef.offset, 2);
  2902. if RefsEqual(taicpu(p1).oper[1]^.ref^, ThisRef) then
  2903. begin
  2904. { Make sure the constants aren't represented as a
  2905. negative number, as these won't merge properly }
  2906. taicpu(p1).opsize := S_L;
  2907. taicpu(p1).oper[0]^.val := (taicpu(p1).oper[0]^.val and $FFFF) or ((taicpu(p2).oper[0]^.val and $FFFF) shl 16);
  2908. DebugMsg(SPeepholeOptimization + 'Merged two word-sized constant writes to stack (MovMov2Mov 2b)', p1);
  2909. RemoveInstruction(p2);
  2910. Result := True;
  2911. end;
  2912. end;
  2913. end;
  2914. {$ifdef x86_64}
  2915. S_L:
  2916. begin
  2917. { Only sign-extended 32-bit constants can be written to 64-bit memory directly, so check to
  2918. see if the constants can be encoded this way. }
  2919. NewConst := (taicpu(p1).oper[0]^.val and $FFFFFFFF) or (taicpu(p2).oper[0]^.val shl 32);
  2920. if (NewConst >= -2147483648) and (NewConst <= 2147483647) and
  2921. { Quadword writes must be on an 8-byte boundary }
  2922. ((taicpu(p1).oper[1]^.ref^.offset mod 8) = 0) then
  2923. begin
  2924. { Reduce offset of second reference to see if it is sequential with the first }
  2925. Dec(ThisRef.offset, 4);
  2926. if RefsEqual(taicpu(p1).oper[1]^.ref^, ThisRef) then
  2927. begin
  2928. { Make sure the constants aren't represented as a
  2929. negative number, as these won't merge properly }
  2930. taicpu(p1).opsize := S_Q;
  2931. { Force a typecast into a 32-bit signed integer (that will then be sign-extended to 64-bit) }
  2932. taicpu(p1).oper[0]^.val := NewConst;
  2933. DebugMsg(SPeepholeOptimization + 'Merged two longword-sized constant writes to stack (MovMov2Mov 2c)', p1);
  2934. RemoveInstruction(p2);
  2935. Result := True;
  2936. end;
  2937. end;
  2938. end;
  2939. {$endif x86_64}
  2940. else
  2941. ;
  2942. end;
  2943. end;
  2944. end;
  2945. var
  2946. TempRegUsed, CrossJump: Boolean;
  2947. PreMessage, RegName1, RegName2, InputVal, MaskNum: string;
  2948. NewSize: topsize; NewOffset: asizeint;
  2949. SourceRef, TargetRef: TReference;
  2950. MovAligned, MovUnaligned: TAsmOp;
  2951. JumpTracking: TLinkedList;
  2952. begin
  2953. Result:=false;
  2954. { remove mov reg1,reg1? }
  2955. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^)
  2956. then
  2957. begin
  2958. DebugMsg(SPeepholeOptimization + 'Mov2Nop 1 done',p);
  2959. { take care of the register (de)allocs following p }
  2960. RemoveCurrentP(p);
  2961. Result := True;
  2962. exit;
  2963. end;
  2964. { Prevent compiler warnings }
  2965. p_SourceReg := NR_NO;
  2966. p_TargetReg := NR_NO;
  2967. hp1 := nil;
  2968. if taicpu(p).oper[1]^.typ = top_reg then
  2969. begin
  2970. { Saves on a large number of dereferences }
  2971. p_TargetReg := taicpu(p).oper[1]^.reg;
  2972. TransferUsedRegs(TmpUsedRegs);
  2973. last_hp1 := p;
  2974. if GetNextHp1(p) then
  2975. while True do
  2976. begin
  2977. if (taicpu(hp1).opcode = A_AND) and
  2978. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2979. SuperRegistersEqual(p_TargetReg, taicpu(hp1).oper[1]^.reg) then
  2980. begin
  2981. UpdateUsedRegsBetween(TmpUsedRegs, last_hp1, hp1);
  2982. if not RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) and
  2983. (taicpu(hp1).oper[0]^.typ = top_const) and
  2984. (taicpu(p).opsize = taicpu(hp1).opsize) then
  2985. begin
  2986. case taicpu(p).opsize of
  2987. S_L:
  2988. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  2989. begin
  2990. { Optimize out:
  2991. mov x, %reg
  2992. and ffffffffh, %reg
  2993. }
  2994. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 1 done',p);
  2995. hp2 := tai(hp1.Previous);
  2996. RemoveInstruction(hp1);
  2997. //Include(OptsToCheck, aoc_ForceNewIteration);
  2998. if GetNextHp1(hp2) then
  2999. Continue
  3000. else
  3001. Exit;
  3002. end;
  3003. S_Q: { TODO: Confirm if this is even possible }
  3004. if (taicpu(hp1).oper[0]^.val = $ffffffffffffffff) then
  3005. begin
  3006. { Optimize out:
  3007. mov x, %reg
  3008. and ffffffffffffffffh, %reg
  3009. }
  3010. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 2 done',p);
  3011. hp2 := tai(hp1.Previous);
  3012. RemoveInstruction(hp1);
  3013. //Include(OptsToCheck, aoc_ForceNewIteration);
  3014. if GetNextHp1(hp2) then
  3015. Continue
  3016. else
  3017. Exit;
  3018. end;
  3019. else
  3020. ;
  3021. end;
  3022. if (
  3023. { Make sure that if a reference is used, its registers
  3024. are not modified in between }
  3025. (
  3026. (taicpu(p).oper[0]^.typ = top_reg) and
  3027. not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)
  3028. ) or
  3029. (
  3030. (taicpu(p).oper[0]^.typ = top_ref) and
  3031. (taicpu(p).oper[0]^.ref^.refaddr <> addr_full) and
  3032. not RefModifiedBetween(taicpu(p).oper[0]^.ref^, topsize2memsize[taicpu(p).opsize] shr 3, p, hp1)
  3033. )
  3034. ) and
  3035. GetNextInstruction(hp1,hp2) and
  3036. MatchInstruction(hp2,A_TEST,[]) and
  3037. (
  3038. MatchOperand(taicpu(hp1).oper[1]^,taicpu(hp2).oper[1]^) or
  3039. (
  3040. { If the register being tested is smaller than the one
  3041. that received a bitwise AND, permit it if the constant
  3042. fits into the smaller size }
  3043. (taicpu(hp1).oper[1]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  3044. SuperRegistersEqual(taicpu(hp1).oper[1]^.reg,taicpu(hp2).oper[1]^.reg) and
  3045. (taicpu(hp1).oper[0]^.typ = top_const) and (taicpu(hp1).oper[0]^.val >= 0) and
  3046. (GetSubReg(taicpu(hp2).oper[1]^.reg) < GetSubReg(taicpu(hp1).oper[1]^.reg)) and
  3047. (
  3048. (
  3049. (GetSubReg(taicpu(hp2).oper[1]^.reg) = R_SUBL) and
  3050. (taicpu(hp1).oper[0]^.val <= $FF)
  3051. ) or
  3052. (
  3053. (GetSubReg(taicpu(hp2).oper[1]^.reg) = R_SUBW) and
  3054. (taicpu(hp1).oper[0]^.val <= $FFFF)
  3055. {$ifdef x86_64}
  3056. ) or
  3057. (
  3058. (GetSubReg(taicpu(hp2).oper[1]^.reg) = R_SUBD) and
  3059. (taicpu(hp1).oper[0]^.val <= $FFFFFFFF)
  3060. {$endif x86_64}
  3061. )
  3062. )
  3063. )
  3064. ) and
  3065. (
  3066. MatchOperand(taicpu(hp2).oper[0]^,taicpu(hp2).oper[1]^) or
  3067. MatchOperand(taicpu(hp2).oper[0]^,-1)
  3068. ) and
  3069. GetNextInstruction(hp2,hp3) and
  3070. MatchInstruction(hp3,A_Jcc,A_Setcc,[]) and
  3071. (taicpu(hp3).condition in [C_E,C_NE]) then
  3072. begin
  3073. TransferUsedRegs(TmpUsedRegs);
  3074. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1);
  3075. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3076. if not(RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp2, TmpUsedRegs)) then
  3077. begin
  3078. DebugMsg(SPeepholeOptimization + 'MovAndTest2Test done',p);
  3079. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  3080. taicpu(hp1).opcode:=A_TEST;
  3081. { Shrink the TEST instruction down to the smallest possible size }
  3082. case taicpu(hp1).oper[0]^.val of
  3083. 0..255:
  3084. if (taicpu(hp1).opsize <> S_B)
  3085. {$ifndef x86_64}
  3086. and (
  3087. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  3088. { Cannot encode byte-sized ESI, EDI, EBP or ESP under i386 }
  3089. (GetSupReg(taicpu(hp1).oper[1]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])
  3090. )
  3091. {$endif x86_64}
  3092. then
  3093. begin
  3094. if taicpu(hp1).opsize <> taicpu(hp2).opsize then
  3095. { Only print debug message if the TEST instruction
  3096. is a different size before and after }
  3097. DebugMsg(SPeepholeOptimization + 'test' + debug_opsize2str(taicpu(hp1).opsize) + ' -> testb to reduce instruction size (Test2Test 1a)' , p);
  3098. taicpu(hp1).opsize := S_B;
  3099. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  3100. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBL);
  3101. end;
  3102. 256..65535:
  3103. if (taicpu(hp1).opsize <> S_W) then
  3104. begin
  3105. if taicpu(hp1).opsize <> taicpu(hp2).opsize then
  3106. { Only print debug message if the TEST instruction
  3107. is a different size before and after }
  3108. DebugMsg(SPeepholeOptimization + 'test' + debug_opsize2str(taicpu(hp1).opsize) + ' -> testw to reduce instruction size (Test2Test 1b)' , p);
  3109. taicpu(hp1).opsize := S_W;
  3110. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  3111. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBW);
  3112. end;
  3113. {$ifdef x86_64}
  3114. 65536..$7FFFFFFF:
  3115. if (taicpu(hp1).opsize <> S_L) then
  3116. begin
  3117. if taicpu(hp1).opsize <> taicpu(hp2).opsize then
  3118. { Only print debug message if the TEST instruction
  3119. is a different size before and after }
  3120. DebugMsg(SPeepholeOptimization + 'test' + debug_opsize2str(taicpu(hp1).opsize) + ' -> testl to reduce instruction size (Test2Test 1c)' , p);
  3121. taicpu(hp1).opsize := S_L;
  3122. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  3123. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  3124. end;
  3125. {$endif x86_64}
  3126. else
  3127. ;
  3128. end;
  3129. RemoveInstruction(hp2);
  3130. RemoveCurrentP(p);
  3131. Result:=true;
  3132. exit;
  3133. end;
  3134. end;
  3135. end;
  3136. if not RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) and
  3137. (taicpu(p).opsize = taicpu(hp1).opsize) and
  3138. (taicpu(hp1).oper[0]^.typ <> top_ref) and
  3139. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  3140. MatchOperand(taicpu(p).oper[1]^, taicpu(hp1).oper[1]^) and
  3141. (
  3142. not (cs_opt_level3 in current_settings.optimizerswitches) or
  3143. (taicpu(hp1).oper[0]^.typ = top_const) or
  3144. not RegModifiedBetween(taicpu(hp1).oper[0]^.reg, p, hp1)
  3145. ) then
  3146. begin
  3147. { With:
  3148. mov %reg1,%reg2
  3149. ...
  3150. and %reg1,%reg2
  3151. Or:
  3152. mov $x,%reg2
  3153. ...
  3154. and $x,%reg2
  3155. Remove the 'and' instruction
  3156. }
  3157. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 4 done',hp1);
  3158. hp2 := tai(hp1.Previous);
  3159. RemoveInstruction(hp1);
  3160. //Include(OptsToCheck, aoc_ForceNewIteration);
  3161. if GetNextHp1(hp2) then
  3162. Continue
  3163. else
  3164. Exit;
  3165. end;
  3166. if IsMOVZXAcceptable and
  3167. (taicpu(p).oper[0]^.typ <> top_const) then { MOVZX only supports registers and memory, not immediates (use MOV for that!) }
  3168. begin
  3169. InputVal := debug_operstr(taicpu(p).oper[0]^);
  3170. MaskNum := debug_tostr(taicpu(hp1).oper[0]^.val);
  3171. case taicpu(p).opsize of
  3172. S_B:
  3173. if (taicpu(hp1).oper[0]^.val = $ff) then
  3174. begin
  3175. { Convert:
  3176. movb x, %regl movb x, %regl
  3177. andw ffh, %regw andl ffh, %regd
  3178. To:
  3179. movzbw x, %regd movzbl x, %regd
  3180. (Identical registers, just different sizes)
  3181. }
  3182. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 8-bit register name }
  3183. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 16/32-bit register name }
  3184. case taicpu(hp1).opsize of
  3185. S_W: NewSize := S_BW;
  3186. S_L: NewSize := S_BL;
  3187. {$ifdef x86_64}
  3188. S_Q: NewSize := S_BQ;
  3189. {$endif x86_64}
  3190. else
  3191. InternalError(2018011510);
  3192. end;
  3193. end
  3194. else
  3195. NewSize := S_NO;
  3196. S_W:
  3197. if (taicpu(hp1).oper[0]^.val = $ffff) then
  3198. begin
  3199. { Convert:
  3200. movw x, %regw
  3201. andl ffffh, %regd
  3202. To:
  3203. movzwl x, %regd
  3204. (Identical registers, just different sizes)
  3205. }
  3206. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 16-bit register name }
  3207. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 32-bit register name }
  3208. case taicpu(hp1).opsize of
  3209. S_L: NewSize := S_WL;
  3210. {$ifdef x86_64}
  3211. S_Q: NewSize := S_WQ;
  3212. {$endif x86_64}
  3213. else
  3214. InternalError(2018011511);
  3215. end;
  3216. end
  3217. else
  3218. NewSize := S_NO;
  3219. else
  3220. NewSize := S_NO;
  3221. end;
  3222. if NewSize <> S_NO then
  3223. begin
  3224. PreMessage := 'mov' + debug_opsize2str(taicpu(p).opsize) + ' ' + InputVal + ',' + RegName1;
  3225. { The actual optimization }
  3226. taicpu(p).opcode := A_MOVZX;
  3227. taicpu(p).changeopsize(NewSize);
  3228. taicpu(p).loadoper(1, taicpu(hp1).oper[1]^);
  3229. { Make sure we deal with any reference counts that were increased }
  3230. if taicpu(hp1).oper[1]^.typ = top_ref then
  3231. begin
  3232. if Assigned(taicpu(hp1).oper[1]^.ref^.symbol) then
  3233. taicpu(hp1).oper[1]^.ref^.symbol.decrefs;
  3234. if Assigned(taicpu(hp1).oper[1]^.ref^.relsymbol) then
  3235. taicpu(hp1).oper[1]^.ref^.relsymbol.decrefs;
  3236. end;
  3237. { Safeguard if "and" is followed by a conditional command }
  3238. TransferUsedRegs(TmpUsedRegs);
  3239. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.next), hp1);
  3240. if (RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  3241. begin
  3242. { At this point, the "and" command is effectively equivalent to
  3243. "test %reg,%reg". This will be handled separately by the
  3244. Peephole Optimizer. [Kit] }
  3245. DebugMsg(SPeepholeOptimization + PreMessage +
  3246. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  3247. end
  3248. else
  3249. begin
  3250. DebugMsg(SPeepholeOptimization + PreMessage + '; and' + debug_opsize2str(taicpu(hp1).opsize) + ' $' + MaskNum + ',' + RegName2 +
  3251. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  3252. RemoveInstruction(hp1);
  3253. end;
  3254. Result := True;
  3255. Exit;
  3256. { Go through DeepMOVOpt again (jump to "while True do") }
  3257. Continue;
  3258. end;
  3259. end;
  3260. end;
  3261. if taicpu(p).oper[0]^.typ = top_reg then
  3262. begin
  3263. p_SourceReg := taicpu(p).oper[0]^.reg;
  3264. { Look for:
  3265. mov %reg1,%reg2
  3266. ??? %reg2,r/m
  3267. Change to:
  3268. mov %reg1,%reg2
  3269. ??? %reg1,r/m
  3270. }
  3271. if RegReadByInstruction(p_TargetReg, hp1) and
  3272. not RegModifiedBetween(p_SourceReg, p, hp1) and
  3273. DeepMOVOpt(taicpu(p), taicpu(hp1)) then
  3274. begin
  3275. { A change has occurred, just not in p }
  3276. Include(OptsToCheck, aoc_ForceNewIteration);
  3277. TransferUsedRegs(TmpUsedRegs);
  3278. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1);
  3279. if not RegUsedAfterInstruction(p_TargetReg, hp1, TmpUsedRegs) and
  3280. { Just in case something didn't get modified (e.g. an
  3281. implicit register) }
  3282. not RegReadByInstruction(p_TargetReg, hp1) then
  3283. begin
  3284. { We can remove the original MOV }
  3285. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3 done',p);
  3286. RemoveCurrentP(p);
  3287. { UsedRegs got updated by RemoveCurrentp }
  3288. Result := True;
  3289. Exit;
  3290. end;
  3291. { If we know a MOV instruction has become a null operation, we might as well
  3292. get rid of it now to save time. }
  3293. if (taicpu(hp1).opcode = A_MOV) and
  3294. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3295. SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[0]^.reg) and
  3296. { Just being a register is enough to confirm it's a null operation }
  3297. (taicpu(hp1).oper[0]^.typ = top_reg) then
  3298. begin
  3299. Result := True;
  3300. { Speed-up to reduce a pipeline stall... if we had something like...
  3301. movl %eax,%edx
  3302. movw %dx,%ax
  3303. ... the second instruction would change to movw %ax,%ax, but
  3304. given that it is now %ax that's active rather than %eax,
  3305. penalties might occur due to a partial register write, so instead,
  3306. change it to a MOVZX instruction when optimising for speed.
  3307. }
  3308. if not (cs_opt_size in current_settings.optimizerswitches) and
  3309. IsMOVZXAcceptable and
  3310. (taicpu(hp1).opsize < taicpu(p).opsize)
  3311. {$ifdef x86_64}
  3312. { operations already implicitly set the upper 64 bits to zero }
  3313. and not ((taicpu(hp1).opsize = S_L) and (taicpu(p).opsize = S_Q))
  3314. {$endif x86_64}
  3315. then
  3316. begin
  3317. DebugMsg(SPeepholeOptimization + 'Zero-extension to minimise pipeline stall (Mov2Movz)',hp1);
  3318. case taicpu(p).opsize of
  3319. S_W:
  3320. if taicpu(hp1).opsize = S_B then
  3321. taicpu(hp1).opsize := S_BL
  3322. else
  3323. InternalError(2020012911);
  3324. S_L{$ifdef x86_64}, S_Q{$endif x86_64}:
  3325. case taicpu(hp1).opsize of
  3326. S_B:
  3327. taicpu(hp1).opsize := S_BL;
  3328. S_W:
  3329. taicpu(hp1).opsize := S_WL;
  3330. else
  3331. InternalError(2020012912);
  3332. end;
  3333. else
  3334. InternalError(2020012910);
  3335. end;
  3336. taicpu(hp1).opcode := A_MOVZX;
  3337. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  3338. end
  3339. else
  3340. begin
  3341. GetNextInstruction_p := GetNextInstruction(hp1, hp2);
  3342. DebugMsg(SPeepholeOptimization + 'Mov2Nop 4 done',hp1);
  3343. RemoveInstruction(hp1);
  3344. { The instruction after what was hp1 is now the immediate next instruction,
  3345. so we can continue to make optimisations if it's present }
  3346. if not GetNextInstruction_p or (hp2.typ <> ait_instruction) then
  3347. Exit;
  3348. hp1 := hp2;
  3349. end;
  3350. end;
  3351. end;
  3352. {$ifdef x86_64}
  3353. { Change:
  3354. movl %reg1l,%reg2l
  3355. movq %reg2q,%reg1q
  3356. To:
  3357. movl %reg1l,%reg2l
  3358. andl %reg1l,%reg1l
  3359. }
  3360. if (taicpu(p).opsize = S_L) and MatchInstruction(hp1,A_MOV,[S_Q]) and
  3361. not RegModifiedBetween(p_SourceReg, p, hp1) and
  3362. MatchOpType(taicpu(hp1), top_reg, top_reg) and
  3363. SuperRegistersEqual(p_TargetReg, taicpu(hp1).oper[0]^.reg) and
  3364. SuperRegistersEqual(p_SourceReg, taicpu(hp1).oper[1]^.reg) then
  3365. begin
  3366. TransferUsedRegs(TmpUsedRegs);
  3367. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1);
  3368. taicpu(hp1).opsize := S_L;
  3369. taicpu(hp1).loadreg(0, p_SourceReg);
  3370. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  3371. AllocRegBetween(p_SourceReg, p, hp1, UsedRegs);
  3372. DebugMsg(SPeepholeOptimization + 'Made 32-to-64-bit zero extension more efficient (MovlMovq2MovlAndl 1)', hp1);
  3373. taicpu(hp1).opcode := A_AND;
  3374. { We may be able to do more and replace references
  3375. to %reg2q with %reg1q etc. }
  3376. if (cs_opt_level3 in current_settings.optimizerswitches) and
  3377. { p_TargetReg is not used between, otherwise the earlier
  3378. GetNextInstructionUsingReg would have stopped sooner }
  3379. DoZeroUpper32Opt(p,hp1) then
  3380. begin
  3381. Result := True;
  3382. Exit;
  3383. end;
  3384. end;
  3385. {
  3386. If we have the following already in the code...
  3387. movl %reg1l,%reg2l
  3388. andl %reg1l,%reg1l
  3389. ...we may be able to do more and replace references to
  3390. %reg2q with %reg1q etc. (program flow won't reach this
  3391. point if the second instruction was originally a MOV
  3392. and just got changed to AND)
  3393. }
  3394. if (cs_opt_level3 in current_settings.optimizerswitches) and
  3395. (taicpu(p).opsize = S_L) and MatchInstruction(hp1,A_AND,[S_L]) and
  3396. not RegModifiedBetween(p_SourceReg, p, hp1) and
  3397. { p_TargetReg is not used between, otherwise the earlier
  3398. GetNextInstructionUsingReg would have stopped sooner }
  3399. MatchOperand(taicpu(hp1).oper[1]^, p_SourceReg) and
  3400. (
  3401. MatchOperand(taicpu(hp1).oper[0]^, p_SourceReg) or
  3402. MatchOperand(taicpu(hp1).oper[0]^, $ffffffff)
  3403. ) and
  3404. DoZeroUpper32Opt(p,hp1) then
  3405. begin
  3406. Result := True;
  3407. Exit;
  3408. end;
  3409. {$endif x86_64}
  3410. end
  3411. else if taicpu(p).oper[0]^.typ = top_const then
  3412. begin
  3413. if (taicpu(hp1).opcode = A_OR) and
  3414. (taicpu(p).oper[1]^.typ = top_reg) and
  3415. MatchOperand(taicpu(p).oper[0]^, 0) and
  3416. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) then
  3417. begin
  3418. { mov 0, %reg
  3419. or ###,%reg
  3420. Change to (only if the flags are not used):
  3421. mov ###,%reg
  3422. }
  3423. TransferUsedRegs(TmpUsedRegs);
  3424. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1);
  3425. DoOptimisation := True;
  3426. { Even if the flags are used, we might be able to do the optimisation
  3427. if the conditions are predictable }
  3428. if RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) then
  3429. begin
  3430. { Only perform if ### = %reg (the same register) or equal to 0,
  3431. so %reg is guaranteed to still have a value of zero }
  3432. if MatchOperand(taicpu(hp1).oper[0]^, 0) or
  3433. MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^.reg) then
  3434. begin
  3435. hp2 := hp1;
  3436. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3437. while RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) and
  3438. GetNextInstruction(hp2, hp3) do
  3439. begin
  3440. { Don't continue modifying if the flags state is getting changed }
  3441. if RegModifiedByInstruction(NR_DEFAULTFLAGS, hp3) then
  3442. Break;
  3443. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  3444. if MatchInstruction(hp3, A_Jcc, A_SETcc, A_CMOVcc, []) then
  3445. begin
  3446. if condition_in(C_E, taicpu(hp3).condition) or (taicpu(hp3).condition in [C_NC, C_NS, C_NO]) then
  3447. begin
  3448. { Condition is always true }
  3449. case taicpu(hp3).opcode of
  3450. A_Jcc:
  3451. begin
  3452. { Check for jump shortcuts before we destroy the condition }
  3453. hp4 := hp3;
  3454. DoJumpOptimizations(hp3, TempBool);
  3455. { Make sure hp3 hasn't changed }
  3456. if (hp4 = hp3) then
  3457. begin
  3458. DebugMsg(SPeepholeOptimization + 'Condition is always true (jump made unconditional)', hp3);
  3459. MakeUnconditional(taicpu(hp3));
  3460. end;
  3461. Result := True;
  3462. end;
  3463. A_CMOVcc:
  3464. begin
  3465. DebugMsg(SPeepholeOptimization + 'Condition is always true (CMOVcc -> MOV)', hp3);
  3466. taicpu(hp3).opcode := A_MOV;
  3467. taicpu(hp3).condition := C_None;
  3468. Result := True;
  3469. end;
  3470. A_SETcc:
  3471. begin
  3472. DebugMsg(SPeepholeOptimization + 'Condition is always true (changed to MOV 1)', hp3);
  3473. { Convert "set(c) %reg" instruction to "movb 1,%reg" }
  3474. taicpu(hp3).opcode := A_MOV;
  3475. taicpu(hp3).ops := 2;
  3476. taicpu(hp3).condition := C_None;
  3477. taicpu(hp3).opsize := S_B;
  3478. taicpu(hp3).loadreg(1,taicpu(hp3).oper[0]^.reg);
  3479. taicpu(hp3).loadconst(0, 1);
  3480. Result := True;
  3481. end;
  3482. else
  3483. InternalError(2021090701);
  3484. end;
  3485. end
  3486. else if (taicpu(hp3).condition in [C_A, C_B, C_C, C_G, C_L, C_NE, C_NZ, C_O, C_S]) then
  3487. begin
  3488. { Condition is always false }
  3489. case taicpu(hp3).opcode of
  3490. A_Jcc:
  3491. begin
  3492. DebugMsg(SPeepholeOptimization + 'Condition is always false (jump removed)', hp3);
  3493. TAsmLabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  3494. RemoveInstruction(hp3);
  3495. Result := True;
  3496. { Since hp3 was deleted, hp2 must not be updated }
  3497. Continue;
  3498. end;
  3499. A_CMOVcc:
  3500. begin
  3501. DebugMsg(SPeepholeOptimization + 'Condition is always false (conditional load removed)', hp3);
  3502. RemoveInstruction(hp3);
  3503. Result := True;
  3504. { Since hp3 was deleted, hp2 must not be updated }
  3505. Continue;
  3506. end;
  3507. A_SETcc:
  3508. begin
  3509. DebugMsg(SPeepholeOptimization + 'Condition is always false (changed to MOV 0)', hp3);
  3510. { Convert "set(c) %reg" instruction to "movb 0,%reg" }
  3511. taicpu(hp3).opcode := A_MOV;
  3512. taicpu(hp3).ops := 2;
  3513. taicpu(hp3).condition := C_None;
  3514. taicpu(hp3).opsize := S_B;
  3515. taicpu(hp3).loadreg(1,taicpu(hp3).oper[0]^.reg);
  3516. taicpu(hp3).loadconst(0, 0);
  3517. Result := True;
  3518. end;
  3519. else
  3520. InternalError(2021090702);
  3521. end;
  3522. end
  3523. else
  3524. { Uncertain what to do - don't optimise (although optimise other conditional statements if present) }
  3525. DoOptimisation := False;
  3526. end;
  3527. hp2 := hp3;
  3528. end;
  3529. if DoOptimisation then
  3530. begin
  3531. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  3532. if RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) then
  3533. { Flags are still in use - don't optimise }
  3534. DoOptimisation := False;
  3535. end;
  3536. end
  3537. else
  3538. DoOptimisation := False;
  3539. end;
  3540. if DoOptimisation then
  3541. begin
  3542. {$ifdef x86_64}
  3543. { OR only supports 32-bit sign-extended constants for 64-bit
  3544. instructions, so compensate for this if the constant is
  3545. encoded as a value greater than or equal to 2^31 }
  3546. if (taicpu(hp1).opsize = S_Q) and
  3547. (taicpu(hp1).oper[0]^.typ = top_const) and
  3548. (taicpu(hp1).oper[0]^.val >= $80000000) then
  3549. taicpu(hp1).oper[0]^.val := taicpu(hp1).oper[0]^.val or $FFFFFFFF00000000;
  3550. {$endif x86_64}
  3551. DebugMsg(SPeepholeOptimization + 'MOV 0 / OR -> MOV', p);
  3552. taicpu(hp1).opcode := A_MOV;
  3553. RemoveCurrentP(p);
  3554. Result := True;
  3555. Exit;
  3556. end;
  3557. end;
  3558. end
  3559. else if
  3560. { oper[0] is a reference }
  3561. (taicpu(p).oper[0]^.ref^.refaddr <> addr_full) then
  3562. begin
  3563. if MatchInstruction(hp1,A_LEA,[S_L{$ifdef x86_64},S_Q{$endif x86_64}]) then
  3564. begin
  3565. if ((MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(hp1).oper[1]^.reg,Taicpu(p).oper[1]^.reg) and
  3566. (Taicpu(hp1).oper[0]^.ref^.base<>Taicpu(p).oper[1]^.reg)
  3567. ) or
  3568. (MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(p).oper[1]^.reg,Taicpu(hp1).oper[1]^.reg) and
  3569. (Taicpu(hp1).oper[0]^.ref^.index<>Taicpu(p).oper[1]^.reg)
  3570. )
  3571. ) and
  3572. not RegModifiedBetween(Taicpu(hp1).oper[1]^.reg, p, hp1) then
  3573. { mov ref,reg1
  3574. lea (reg1,reg2),reg2
  3575. to
  3576. add ref,reg2 }
  3577. begin
  3578. TransferUsedRegs(TmpUsedRegs);
  3579. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1);
  3580. { If the flags register is in use, don't change the instruction to an
  3581. ADD otherwise this will scramble the flags. [Kit] }
  3582. if not RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) and
  3583. { reg1 may not be used afterwards }
  3584. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  3585. begin
  3586. Taicpu(hp1).opcode:=A_ADD;
  3587. Taicpu(hp1).oper[0]^.ref^:=Taicpu(p).oper[0]^.ref^;
  3588. DebugMsg(SPeepholeOptimization + 'MovLea2Add done',hp1);
  3589. RemoveCurrentp(p);
  3590. result:=true;
  3591. exit;
  3592. end;
  3593. end;
  3594. { If the LEA instruction can be converted into an arithmetic instruction,
  3595. it may be possible to then fold it in the next optimisation. }
  3596. if ConvertLEA(taicpu(hp1)) then
  3597. Include(OptsToCheck, aoc_ForceNewIteration);
  3598. end;
  3599. {
  3600. mov ref,reg0
  3601. <op> reg0,reg1
  3602. dealloc reg0
  3603. to
  3604. <op> ref,reg1
  3605. }
  3606. if MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3607. (taicpu(hp1).oper[0]^.reg = p_TargetReg) and
  3608. MatchInstruction(hp1, [A_AND, A_OR, A_XOR, A_ADD, A_SUB, A_CMP, A_TEST, A_CMOVcc, A_BSR, A_BSF, A_POPCNT, A_LZCNT], [taicpu(p).opsize]) and
  3609. not SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, p_TargetReg) and
  3610. not RefModifiedBetween(taicpu(p).oper[0]^.ref^, topsize2memsize[taicpu(p).opsize] shr 3, p, hp1) then
  3611. begin
  3612. TransferUsedRegs(TmpUsedRegs);
  3613. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1);
  3614. if not RegUsedAfterInstruction(p_TargetReg, hp1, TmpUsedRegs) then
  3615. begin
  3616. taicpu(hp1).loadref(0,taicpu(p).oper[0]^.ref^);
  3617. { loadref increases the reference count, so decrement it again }
  3618. if Assigned(taicpu(p).oper[0]^.ref^.symbol) then
  3619. taicpu(p).oper[0]^.ref^.symbol.decrefs;
  3620. if Assigned(taicpu(p).oper[0]^.ref^.relsymbol) then
  3621. taicpu(p).oper[0]^.ref^.relsymbol.decrefs;
  3622. DebugMsg(SPeepholeOptimization + 'MovOp2Op done',hp1);
  3623. { See if we can remove the allocation of reg0 }
  3624. if not RegInRef(p_TargetReg, taicpu(p).oper[0]^.ref^) then
  3625. TryRemoveRegAlloc(p_TargetReg, p, hp1);
  3626. RemoveCurrentp(p);
  3627. Result:=true;
  3628. exit;
  3629. end;
  3630. end;
  3631. end;
  3632. { Depending on the DeepMOVOpt above, it may turn out that hp1 completely
  3633. overwrites the original destination register. e.g.
  3634. movl ###,%reg2d
  3635. movslq ###,%reg2q (### doesn't have to be the same as the first one)
  3636. In this case, we can remove the MOV (Go to "Mov2Nop 5" below)
  3637. }
  3638. if MatchInstruction(hp1, [A_LEA, A_MOV, A_MOVSX, A_MOVZX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}], []) and
  3639. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3640. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[1]^.reg) then
  3641. begin
  3642. if RegInOp(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[0]^) then
  3643. begin
  3644. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  3645. case taicpu(p).oper[0]^.typ of
  3646. top_const:
  3647. { We have something like:
  3648. movb $x, %regb
  3649. movzbl %regb,%regd
  3650. Change to:
  3651. movl $x, %regd
  3652. }
  3653. begin
  3654. case taicpu(hp1).opsize of
  3655. S_BW:
  3656. begin
  3657. convert_mov_value(A_MOVSX, $FF);
  3658. setsubreg(taicpu(p).oper[1]^.reg, R_SUBW);
  3659. taicpu(p).opsize := S_W;
  3660. end;
  3661. S_BL:
  3662. begin
  3663. convert_mov_value(A_MOVSX, $FF);
  3664. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3665. taicpu(p).opsize := S_L;
  3666. end;
  3667. S_WL:
  3668. begin
  3669. convert_mov_value(A_MOVSX, $FFFF);
  3670. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3671. taicpu(p).opsize := S_L;
  3672. end;
  3673. {$ifdef x86_64}
  3674. S_BQ:
  3675. begin
  3676. convert_mov_value(A_MOVSX, $FF);
  3677. setsubreg(taicpu(p).oper[1]^.reg, R_SUBQ);
  3678. taicpu(p).opsize := S_Q;
  3679. end;
  3680. S_WQ:
  3681. begin
  3682. convert_mov_value(A_MOVSX, $FFFF);
  3683. setsubreg(taicpu(p).oper[1]^.reg, R_SUBQ);
  3684. taicpu(p).opsize := S_Q;
  3685. end;
  3686. S_LQ:
  3687. begin
  3688. convert_mov_value(A_MOVSXD, $FFFFFFFF); { Note it's MOVSXD, not MOVSX }
  3689. setsubreg(taicpu(p).oper[1]^.reg, R_SUBQ);
  3690. taicpu(p).opsize := S_Q;
  3691. end;
  3692. {$endif x86_64}
  3693. else
  3694. { If hp1 was a MOV instruction, it should have been
  3695. optimised already }
  3696. InternalError(2020021001);
  3697. end;
  3698. DebugMsg(SPeepholeOptimization + 'MovMovXX2MovXX 2 done',p);
  3699. RemoveInstruction(hp1);
  3700. Result := True;
  3701. Exit;
  3702. end;
  3703. top_ref:
  3704. begin
  3705. { We have something like:
  3706. movb mem, %regb
  3707. movzbl %regb,%regd
  3708. Change to:
  3709. movzbl mem, %regd
  3710. }
  3711. if (taicpu(p).oper[0]^.ref^.refaddr<>addr_full) and (IsMOVZXAcceptable or (taicpu(hp1).opcode<>A_MOVZX)) then
  3712. begin
  3713. DebugMsg(SPeepholeOptimization + 'MovMovXX2MovXX 1 done',p);
  3714. taicpu(p).opcode := taicpu(hp1).opcode;
  3715. taicpu(p).opsize := taicpu(hp1).opsize;
  3716. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg;
  3717. RemoveInstruction(hp1);
  3718. Result := True;
  3719. Exit;
  3720. end;
  3721. end;
  3722. else
  3723. if (taicpu(hp1).opcode <> A_MOV) and (taicpu(hp1).opcode <> A_LEA) then
  3724. { Just to make a saving, since there are no more optimisations with MOVZX and MOVSX/D }
  3725. Exit;
  3726. end;
  3727. end
  3728. { The RegInOp check makes sure that movl r/m,%reg1l; movzbl (%reg1l),%reg1l"
  3729. and "movl r/m,%reg1; leal $1(%reg1,%reg2),%reg1" etc. are not incorrectly
  3730. optimised }
  3731. else
  3732. begin
  3733. DebugMsg(SPeepholeOptimization + 'Mov2Nop 5 done',p);
  3734. RemoveCurrentP(p);
  3735. Result := True;
  3736. Exit;
  3737. end;
  3738. end;
  3739. if (taicpu(hp1).opcode = A_MOV) and
  3740. (
  3741. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^)
  3742. {$ifdef x86_64}
  3743. or (
  3744. { Permit zero extension from 32- to 64-bit when writing
  3745. a constant (it will be checked to see if it fits into
  3746. a signed 32-bit integer) }
  3747. (taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and
  3748. (
  3749. { Valid situations... writing an unsigned 32-bit
  3750. immediate, or the destination is a 64-bit register }
  3751. (taicpu(p).oper[0]^.typ = top_const) or
  3752. (taicpu(hp1).oper[1]^.typ = top_reg)
  3753. ) and
  3754. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3755. SuperRegistersEqual(p_TargetReg, taicpu(hp1).oper[0]^.reg)
  3756. )
  3757. {$endif x86_64}
  3758. ) then
  3759. begin
  3760. { Remember that p_TargetReg contains taicpu(p).oper[1]^.reg }
  3761. TransferUsedRegs(TmpUsedRegs);
  3762. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1);
  3763. { we have
  3764. mov x, %treg
  3765. mov %treg, y
  3766. }
  3767. if not(RegInOp(p_TargetReg, taicpu(hp1).oper[1]^)) then
  3768. if not(RegUsedAfterInstruction(p_TargetReg, hp1, TmpUsedRegs)) then
  3769. begin
  3770. { we've got
  3771. mov x, %treg
  3772. mov %treg, y
  3773. with %treg is not used after }
  3774. case taicpu(p).oper[0]^.typ Of
  3775. { top_reg is covered by DeepMOVOpt }
  3776. top_const:
  3777. begin
  3778. { change
  3779. mov const, %treg
  3780. mov %treg, y
  3781. to
  3782. mov const, y
  3783. }
  3784. {$ifdef x86_64}
  3785. if (taicpu(hp1).oper[1]^.typ=top_reg) or
  3786. (
  3787. { For 32-to-64-bit zero-extension, the immediate
  3788. must be between 0 and 2^31 - 1}
  3789. (taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and
  3790. ((taicpu(p).oper[0]^.val>=0) and (taicpu(p).oper[0]^.val<=high(longint)))
  3791. ) or
  3792. (
  3793. not ((taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q)) and
  3794. (
  3795. (taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))
  3796. )
  3797. ) then
  3798. {$endif x86_64}
  3799. begin
  3800. taicpu(hp1).loadconst(0, taicpu(p).oper[0]^.val);
  3801. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 5 done', hp1);
  3802. RemoveCurrentP(p);
  3803. Result := True;
  3804. Exit;
  3805. end;
  3806. end;
  3807. top_ref:
  3808. case taicpu(hp1).oper[1]^.typ of
  3809. top_reg:
  3810. { change
  3811. mov mem, %treg
  3812. mov %treg, %reg
  3813. to
  3814. mov mem, %reg"
  3815. }
  3816. if not RegUsedBetween(taicpu(hp1).oper[1]^.reg, p, hp1) then
  3817. begin
  3818. {$ifdef x86_64}
  3819. { If zero extending from 32-bit to 64-bit,
  3820. we have to make sure the replaced
  3821. register is the right size }
  3822. taicpu(p).loadreg(1, newreg(R_INTREGISTER,getsupreg(taicpu(hp1).oper[1]^.reg),getsubreg(p_TargetReg)));
  3823. {$else}
  3824. taicpu(p).loadreg(1, taicpu(hp1).oper[1]^.reg);
  3825. {$endif x86_64}
  3826. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3a done', p);
  3827. AllocRegBetween(taicpu(hp1).oper[1]^.reg, p, hp1, UsedRegs);
  3828. RemoveInstruction(hp1);
  3829. Result := True;
  3830. Exit;
  3831. end
  3832. else if
  3833. { Make sure that if a reference is used, its
  3834. registers are not modified in between }
  3835. not RefModifiedBetween(taicpu(p).oper[0]^.ref^, topsize2memsize[taicpu(p).opsize] shr 3, p, hp1) then
  3836. begin
  3837. if (taicpu(p).oper[0]^.ref^.base <> NR_NO){$ifdef x86_64} and (taicpu(p).oper[0]^.ref^.base <> NR_RIP){$endif x86_64} then
  3838. AllocRegBetween(taicpu(p).oper[0]^.ref^.base, p, hp1, UsedRegs);
  3839. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[0]^.ref^.base) then
  3840. AllocRegBetween(taicpu(p).oper[0]^.ref^.index, p, hp1, UsedRegs);
  3841. taicpu(hp1).loadref(0, taicpu(p).oper[0]^.ref^);
  3842. if Assigned(taicpu(p).oper[0]^.ref^.symbol) then
  3843. taicpu(p).oper[0]^.ref^.symbol.decrefs;
  3844. if Assigned(taicpu(p).oper[0]^.ref^.relsymbol) then
  3845. taicpu(p).oper[0]^.ref^.relsymbol.decrefs;
  3846. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3 done', hp1);
  3847. RemoveCurrentP(p);
  3848. Result := True;
  3849. Exit;
  3850. end;
  3851. top_ref:
  3852. if not RegInRef(p_TargetReg, taicpu(p).oper[0]^.ref^) then
  3853. begin
  3854. {$ifdef x86_64}
  3855. { Look for the following to simplify:
  3856. mov x(mem1), %reg
  3857. mov %reg, y(mem2)
  3858. mov x+8(mem1), %reg
  3859. mov %reg, y+8(mem2)
  3860. Change to:
  3861. movdqu x(mem1), %xmmreg
  3862. movdqu %xmmreg, y(mem2)
  3863. ...but only as long as the memory blocks don't overlap
  3864. }
  3865. SourceRef := taicpu(p).oper[0]^.ref^;
  3866. TargetRef := taicpu(hp1).oper[1]^.ref^;
  3867. if (taicpu(p).opsize = S_Q) and
  3868. not RegUsedAfterInstruction(p_TargetReg, hp1, TmpUsedRegs) and
  3869. GetNextInstruction(hp1, hp2) and
  3870. MatchInstruction(hp2, A_MOV, [taicpu(p).opsize]) and
  3871. MatchOpType(taicpu(hp2), top_ref, top_reg) then
  3872. begin
  3873. { Delay calling GetNextInstruction(hp2, hp3) for as long as possible }
  3874. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3875. Inc(SourceRef.offset, 8);
  3876. if UseAVX then
  3877. begin
  3878. MovAligned := A_VMOVDQA;
  3879. MovUnaligned := A_VMOVDQU;
  3880. end
  3881. else
  3882. begin
  3883. MovAligned := A_MOVDQA;
  3884. MovUnaligned := A_MOVDQU;
  3885. end;
  3886. if RefsEqual(SourceRef, taicpu(hp2).oper[0]^.ref^) and
  3887. not RefsMightOverlap(taicpu(p).oper[0]^.ref^, TargetRef, 16) then
  3888. begin
  3889. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  3890. Inc(TargetRef.offset, 8);
  3891. if GetNextInstruction(hp2, hp3) and
  3892. MatchInstruction(hp3, A_MOV, [taicpu(p).opsize]) and
  3893. MatchOpType(taicpu(hp3), top_reg, top_ref) and
  3894. (taicpu(hp2).oper[1]^.reg = taicpu(hp3).oper[0]^.reg) and
  3895. RefsEqual(TargetRef, taicpu(hp3).oper[1]^.ref^) and
  3896. not RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp3, TmpUsedRegs) then
  3897. begin
  3898. NewMMReg := GetMMRegisterBetween(R_SUBMMX, UsedRegs, p, hp3);
  3899. if NewMMReg <> NR_NO then
  3900. begin
  3901. { Remember that the offsets are 8 ahead }
  3902. if ((SourceRef.offset mod 16) = 8) and
  3903. (
  3904. { Base pointer is always aligned (stack pointer won't be if there's no stack frame) }
  3905. (SourceRef.base = current_procinfo.framepointer) or
  3906. ((SourceRef.alignment >= 16) and ((SourceRef.alignment mod 16) = 0))
  3907. ) then
  3908. taicpu(p).opcode := MovAligned
  3909. else
  3910. taicpu(p).opcode := MovUnaligned;
  3911. taicpu(p).opsize := S_XMM;
  3912. taicpu(p).oper[1]^.reg := NewMMReg;
  3913. if ((TargetRef.offset mod 16) = 8) and
  3914. (
  3915. { Base pointer is always aligned (stack pointer won't be if there's no stack frame) }
  3916. (TargetRef.base = current_procinfo.framepointer) or
  3917. ((TargetRef.alignment >= 16) and ((TargetRef.alignment mod 16) = 0))
  3918. ) then
  3919. taicpu(hp1).opcode := MovAligned
  3920. else
  3921. taicpu(hp1).opcode := MovUnaligned;
  3922. taicpu(hp1).opsize := S_XMM;
  3923. taicpu(hp1).oper[0]^.reg := NewMMReg;
  3924. DebugMsg(SPeepholeOptimization + 'Used ' + debug_regname(NewMMReg) + ' to merge a pair of memory moves (MovMovMovMov2MovdqMovdq 1)', p);
  3925. RemoveInstruction(hp2);
  3926. RemoveInstruction(hp3);
  3927. Result := True;
  3928. Exit;
  3929. end;
  3930. end;
  3931. end
  3932. else
  3933. begin
  3934. { See if the next references are 8 less rather than 8 greater }
  3935. Dec(SourceRef.offset, 16); { -8 the other way }
  3936. if RefsEqual(SourceRef, taicpu(hp2).oper[0]^.ref^) then
  3937. begin
  3938. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  3939. Dec(TargetRef.offset, 8); { Only 8, not 16, as it wasn't incremented unlike SourceRef }
  3940. if not RefsMightOverlap(SourceRef, TargetRef, 16) and
  3941. GetNextInstruction(hp2, hp3) and
  3942. MatchInstruction(hp3, A_MOV, [taicpu(p).opsize]) and
  3943. MatchOpType(taicpu(hp3), top_reg, top_ref) and
  3944. (taicpu(hp2).oper[1]^.reg = taicpu(hp3).oper[0]^.reg) and
  3945. RefsEqual(TargetRef, taicpu(hp3).oper[1]^.ref^) and
  3946. not RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp3, TmpUsedRegs) then
  3947. begin
  3948. NewMMReg := GetMMRegisterBetween(R_SUBMMX, UsedRegs, p, hp3);
  3949. if NewMMReg <> NR_NO then
  3950. begin
  3951. { hp2 and hp3 are the starting offsets, so mod = 0 this time }
  3952. if ((SourceRef.offset mod 16) = 0) and
  3953. (
  3954. { Base pointer is always aligned (stack pointer won't be if there's no stack frame) }
  3955. (SourceRef.base = current_procinfo.framepointer) or
  3956. ((SourceRef.alignment >= 16) and ((SourceRef.alignment mod 16) = 0))
  3957. ) then
  3958. taicpu(hp2).opcode := MovAligned
  3959. else
  3960. taicpu(hp2).opcode := MovUnaligned;
  3961. taicpu(hp2).opsize := S_XMM;
  3962. taicpu(hp2).oper[1]^.reg := NewMMReg;
  3963. if ((TargetRef.offset mod 16) = 0) and
  3964. (
  3965. { Base pointer is always aligned (stack pointer won't be if there's no stack frame) }
  3966. (TargetRef.base = current_procinfo.framepointer) or
  3967. ((TargetRef.alignment >= 16) and ((TargetRef.alignment mod 16) = 0))
  3968. ) then
  3969. taicpu(hp3).opcode := MovAligned
  3970. else
  3971. taicpu(hp3).opcode := MovUnaligned;
  3972. taicpu(hp3).opsize := S_XMM;
  3973. taicpu(hp3).oper[0]^.reg := NewMMReg;
  3974. DebugMsg(SPeepholeOptimization + 'Used ' + debug_regname(NewMMReg) + ' to merge a pair of memory moves (MovMovMovMov2MovdqMovdq 2)', p);
  3975. RemoveInstruction(hp1);
  3976. RemoveCurrentP(p);
  3977. Result := True;
  3978. Exit;
  3979. end;
  3980. end;
  3981. end;
  3982. end;
  3983. end;
  3984. {$endif x86_64}
  3985. end;
  3986. else
  3987. { The write target should be a reg or a ref }
  3988. InternalError(2021091601);
  3989. end;
  3990. else
  3991. ;
  3992. end;
  3993. end
  3994. else if (taicpu(p).oper[0]^.typ = top_const) and
  3995. { %treg is used afterwards, but all eventualities other
  3996. than the first MOV instruction being a constant are
  3997. covered by DeepMOVOpt, so only check for that }
  3998. (
  3999. { For MOV operations, a size saving is only made if the register/const is byte-sized }
  4000. not (cs_opt_size in current_settings.optimizerswitches) or
  4001. (taicpu(hp1).opsize = S_B)
  4002. ) and
  4003. (
  4004. (taicpu(hp1).oper[1]^.typ=top_reg) or
  4005. (
  4006. { For 32-to-64-bit zero-extension, the immediate
  4007. must be between 0 and 2^31 - 1}
  4008. (taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and
  4009. ((taicpu(p).oper[0]^.val>=0) and (taicpu(p).oper[0]^.val<=high(longint)))
  4010. ) or
  4011. (
  4012. not ((taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q)) and
  4013. (
  4014. (taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))
  4015. )
  4016. )
  4017. ) then
  4018. begin
  4019. DebugMsg(SPeepholeOptimization + debug_operstr(taicpu(hp1).oper[0]^) + ' = $' + debug_tostr(taicpu(p).oper[0]^.val) + '; changed to minimise pipeline stall (MovMov2Mov 6b)',hp1);
  4020. taicpu(hp1).loadconst(0, taicpu(p).oper[0]^.val);
  4021. Include(OptsToCheck, aoc_ForceNewIteration);
  4022. end;
  4023. end;
  4024. Break;
  4025. end;
  4026. end;
  4027. if taicpu(p).oper[0]^.typ = top_reg then
  4028. begin
  4029. { oper[1] is a reference }
  4030. { Saves on a large number of dereferences }
  4031. p_SourceReg := taicpu(p).oper[0]^.reg;
  4032. if NotFirstIteration and (cs_opt_level3 in current_settings.optimizerswitches) then
  4033. GetNextInstruction_p := GetNextInstructionUsingReg(p, hp1, p_SourceReg)
  4034. else
  4035. GetNextInstruction_p := GetNextInstruction(p, hp1);
  4036. if GetNextInstruction_p and (hp1.typ = ait_instruction) then
  4037. begin
  4038. if taicpu(p).oper[1]^.typ = top_reg then
  4039. begin
  4040. p_TargetReg := taicpu(p).oper[1]^.reg;
  4041. { Change:
  4042. movl %reg1,%reg2
  4043. ...
  4044. movl x(%reg1),%reg1 (If something other than %reg1 is written to, DeepMOVOpt would have caught it)
  4045. ...
  4046. movl x(%reg2),%regX (%regX can be %reg2 or something else)
  4047. To:
  4048. movl %reg1,%reg2 (if %regX = %reg2, then remove this instruction)
  4049. ...
  4050. movl x(%reg1),%reg1
  4051. ...
  4052. movl %reg1,%regX
  4053. }
  4054. if MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  4055. (taicpu(hp1).oper[0]^.typ = top_ref) { The other operand will be a register } and
  4056. (taicpu(hp1).oper[1]^.reg = p_SourceReg) and
  4057. RegInRef(p_SourceReg, taicpu(hp1).oper[0]^.ref^) and
  4058. not RegModifiedBetween(p_TargetReg, p, hp1) and
  4059. GetNextInstructionUsingReg(hp1, hp2, p_TargetReg) and
  4060. MatchInstruction(hp2, A_MOV, [taicpu(p).opsize]) and
  4061. (taicpu(hp2).oper[0]^.typ = top_ref) { The other operand will be a register } and
  4062. not RegModifiedBetween(p_SourceReg, hp1, hp2) then
  4063. begin
  4064. SourceRef := taicpu(hp2).oper[0]^.ref^;
  4065. if RegInRef(p_TargetReg, SourceRef) and
  4066. { If %reg1 also appears in the second reference, then it will
  4067. not refer to the same memory block as the first reference }
  4068. not RegInRef(p_SourceReg, SourceRef) then
  4069. begin
  4070. { Check to see if the references match if %reg2 is changed to %reg1 }
  4071. if SourceRef.base = p_TargetReg then
  4072. SourceRef.base := p_SourceReg;
  4073. if SourceRef.index = p_TargetReg then
  4074. SourceRef.index := p_SourceReg;
  4075. { RefsEqual also checks to ensure both references are non-volatile }
  4076. if RefsEqual(taicpu(hp1).oper[0]^.ref^, SourceRef) then
  4077. begin
  4078. taicpu(hp2).loadreg(0, p_SourceReg);
  4079. TransferUsedRegs(TmpUsedRegs);
  4080. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1);
  4081. { Make sure the register is allocated between these instructions
  4082. even though it doesn't change value, since it may cause
  4083. optimisations on a later pass to behave incorrectly. (Fixes #41155) }
  4084. AllocRegBetween(p_SourceReg, hp1, hp2, TmpUsedRegs);
  4085. DebugMsg(SPeepholeOptimization + 'Optimised register duplication and memory read (MovMovMov2MovMovMov)', p);
  4086. Result := True;
  4087. if taicpu(hp2).oper[1]^.reg = p_TargetReg then
  4088. begin
  4089. DebugMsg(SPeepholeOptimization + 'Mov2Nop 5a done', p);
  4090. RemoveCurrentP(p);
  4091. Exit;
  4092. end
  4093. else
  4094. begin
  4095. if not RegUsedAfterInstruction(p_TargetReg, hp2, TmpUsedRegs) then
  4096. begin
  4097. DebugMsg(SPeepholeOptimization + 'Mov2Nop 5b done', p);
  4098. RemoveCurrentP(p);
  4099. Exit;
  4100. end;
  4101. end;
  4102. { If we reach this point, p and hp1 weren't actually modified,
  4103. so we can do a bit more work on this pass }
  4104. end;
  4105. end;
  4106. end;
  4107. end;
  4108. end;
  4109. end;
  4110. GetNextInstruction_p:=GetNextInstruction(p, hp1);
  4111. { All the next optimisations require a next instruction }
  4112. if not GetNextInstruction_p or (hp1.typ <> ait_instruction) then
  4113. Exit;
  4114. { Change:
  4115. movl/q (ref), %reg
  4116. movd/q %reg, %xmm0
  4117. (dealloc %reg)
  4118. To:
  4119. movd/q (ref), %xmm0
  4120. }
  4121. if MatchOpType(taicpu(p),top_ref,top_reg) and
  4122. MatchInstruction(hp1,[A_MOVD,A_VMOVD{$ifdef x86_64},A_MOVQ,A_VMOVQ{$endif x86_64}],[]) and
  4123. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^.reg) and
  4124. (taicpu(hp1).oper[1]^.typ=top_reg) and
  4125. (GetRegType(taicpu(hp1).oper[1]^.reg)=R_MMREGISTER) then
  4126. begin
  4127. TransferUsedRegs(TmpUsedRegs);
  4128. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4129. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs) then
  4130. begin
  4131. taicpu(hp1).loadref(0,taicpu(p).oper[0]^.ref^);
  4132. { loadref increases the reference count, so decrement it again }
  4133. if Assigned(taicpu(p).oper[0]^.ref^.symbol) then
  4134. taicpu(p).oper[0]^.ref^.symbol.decrefs;
  4135. if Assigned(taicpu(p).oper[0]^.ref^.relsymbol) then
  4136. taicpu(p).oper[0]^.ref^.relsymbol.decrefs;
  4137. DebugMsg(SPeepholeOptimization+'Merged MOV and (V)MOVD/(V)MOVQ to eliminate intermediate register (MovMovD/Q2MovD/Q)',p);
  4138. RemoveCurrentP(p,hp1);
  4139. Result:=True;
  4140. Exit;
  4141. end;
  4142. end;
  4143. { Next instruction is also a MOV ? }
  4144. if MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) then
  4145. begin
  4146. if MatchOpType(taicpu(p), top_const, top_ref) and
  4147. MatchOpType(taicpu(hp1), top_const, top_ref) and
  4148. TryConstMerge(p, hp1) then
  4149. begin
  4150. Result := True;
  4151. { In case we have four byte writes in a row, check for 2 more
  4152. right now so we don't have to wait for another iteration of
  4153. pass 1
  4154. }
  4155. { If two byte-writes were merged, the opsize is now S_W, not S_B }
  4156. case taicpu(p).opsize of
  4157. S_W:
  4158. begin
  4159. if GetNextInstruction(p, hp1) and
  4160. MatchInstruction(hp1, A_MOV, [S_B]) and
  4161. MatchOpType(taicpu(hp1), top_const, top_ref) and
  4162. GetNextInstruction(hp1, hp2) and
  4163. MatchInstruction(hp2, A_MOV, [S_B]) and
  4164. MatchOpType(taicpu(hp2), top_const, top_ref) and
  4165. { Try to merge the two bytes }
  4166. TryConstMerge(hp1, hp2) then
  4167. { Now try to merge the two words (hp2 will get deleted) }
  4168. TryConstMerge(p, hp1);
  4169. end;
  4170. S_L:
  4171. begin
  4172. { Though this only really benefits x86_64 and not i386, it
  4173. gets a potential optimisation done faster and hence
  4174. reduces the number of times OptPass1MOV is entered }
  4175. if GetNextInstruction(p, hp1) and
  4176. MatchInstruction(hp1, A_MOV, [S_W]) and
  4177. MatchOpType(taicpu(hp1), top_const, top_ref) and
  4178. GetNextInstruction(hp1, hp2) and
  4179. MatchInstruction(hp2, A_MOV, [S_W]) and
  4180. MatchOpType(taicpu(hp2), top_const, top_ref) and
  4181. { Try to merge the two words }
  4182. TryConstMerge(hp1, hp2) then
  4183. { This will always fail on i386, so don't bother
  4184. calling it unless we're doing x86_64 }
  4185. {$ifdef x86_64}
  4186. { Now try to merge the two longwords (hp2 will get deleted) }
  4187. TryConstMerge(p, hp1)
  4188. {$endif x86_64}
  4189. ;
  4190. end;
  4191. else
  4192. ;
  4193. end;
  4194. Exit;
  4195. end;
  4196. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  4197. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  4198. { mov reg1, mem1 or mov mem1, reg1
  4199. mov mem2, reg2 mov reg2, mem2}
  4200. begin
  4201. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  4202. { mov reg1, mem1 or mov mem1, reg1
  4203. mov mem2, reg1 mov reg2, mem1}
  4204. begin
  4205. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  4206. { Removes the second statement from
  4207. mov reg1, mem1/reg2
  4208. mov mem1/reg2, reg1 }
  4209. begin
  4210. if taicpu(p).oper[0]^.typ=top_reg then
  4211. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  4212. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 1',p);
  4213. RemoveInstruction(hp1);
  4214. Result:=true;
  4215. if (taicpu(p).oper[1]^.typ = top_reg) then
  4216. begin
  4217. TransferUsedRegs(TmpUsedRegs);
  4218. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, p, TmpUsedRegs) then
  4219. begin
  4220. { reg2 is no longer in use }
  4221. DebugMsg(SPeepholeOptimization + 'Mov2Nop 6 done',p);
  4222. RemoveCurrentP(p);
  4223. end;
  4224. end;
  4225. exit;
  4226. end
  4227. else
  4228. begin
  4229. TransferUsedRegs(TmpUsedRegs);
  4230. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  4231. if (taicpu(p).oper[1]^.typ = top_ref) and
  4232. { mov reg1, mem1
  4233. mov mem2, reg1 }
  4234. (taicpu(hp1).oper[0]^.ref^.refaddr = addr_no) and
  4235. GetNextInstruction(hp1, hp2) and
  4236. MatchInstruction(hp2,A_CMP,[taicpu(p).opsize]) and
  4237. OpsEqual(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  4238. OpsEqual(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) and
  4239. not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs)) then
  4240. { change to
  4241. mov reg1, mem1 mov reg1, mem1
  4242. mov mem2, reg1 cmp reg1, mem2
  4243. cmp mem1, reg1
  4244. }
  4245. begin
  4246. RemoveInstruction(hp2);
  4247. taicpu(hp1).opcode := A_CMP;
  4248. taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
  4249. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  4250. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  4251. DebugMsg(SPeepholeOptimization + 'MovMovCmp2MovCmp done',hp1);
  4252. end;
  4253. end;
  4254. end
  4255. else if (taicpu(p).oper[1]^.typ=top_ref) and
  4256. OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  4257. begin
  4258. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  4259. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  4260. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov1 done',p);
  4261. end
  4262. else
  4263. begin
  4264. TransferUsedRegs(TmpUsedRegs);
  4265. if GetNextInstruction(hp1, hp2) and
  4266. MatchOpType(taicpu(p),top_ref,top_reg) and
  4267. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  4268. (taicpu(hp1).oper[1]^.typ = top_ref) and
  4269. MatchInstruction(hp2,A_MOV,[taicpu(p).opsize]) and
  4270. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  4271. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  4272. if not RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^) and
  4273. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,tmpUsedRegs)) then
  4274. { mov mem1, %reg1
  4275. mov %reg1, mem2
  4276. mov mem2, reg2
  4277. to:
  4278. mov mem1, reg2
  4279. mov reg2, mem2}
  4280. begin
  4281. AllocRegBetween(taicpu(hp2).oper[1]^.reg,p,hp2,usedregs);
  4282. DebugMsg(SPeepholeOptimization + 'MovMovMov2MovMov 1 done',p);
  4283. taicpu(p).loadoper(1,taicpu(hp2).oper[1]^);
  4284. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  4285. RemoveInstruction(hp2);
  4286. Result := True;
  4287. end
  4288. {$ifdef i386}
  4289. { this is enabled for i386 only, as the rules to create the reg sets below
  4290. are too complicated for x86-64, so this makes this code too error prone
  4291. on x86-64
  4292. }
  4293. else if (taicpu(p).oper[1]^.reg <> taicpu(hp2).oper[1]^.reg) and
  4294. not(RegInRef(taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.ref^)) and
  4295. not(RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^)) then
  4296. { mov mem1, reg1 mov mem1, reg1
  4297. mov reg1, mem2 mov reg1, mem2
  4298. mov mem2, reg2 mov mem2, reg1
  4299. to: to:
  4300. mov mem1, reg1 mov mem1, reg1
  4301. mov mem1, reg2 mov reg1, mem2
  4302. mov reg1, mem2
  4303. or (if mem1 depends on reg1
  4304. and/or if mem2 depends on reg2)
  4305. to:
  4306. mov mem1, reg1
  4307. mov reg1, mem2
  4308. mov reg1, reg2
  4309. }
  4310. begin
  4311. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  4312. taicpu(hp1).loadReg(1,taicpu(hp2).oper[1]^.reg);
  4313. taicpu(hp2).loadRef(1,taicpu(hp2).oper[0]^.ref^);
  4314. taicpu(hp2).loadReg(0,taicpu(p).oper[1]^.reg);
  4315. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  4316. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  4317. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  4318. AllocRegBetween(taicpu(p).oper[0]^.ref^.base,p,hp2,usedregs);
  4319. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and
  4320. (getsupreg(taicpu(p).oper[0]^.ref^.index) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  4321. AllocRegBetween(taicpu(p).oper[0]^.ref^.index,p,hp2,usedregs);
  4322. end
  4323. else if (taicpu(hp1).Oper[0]^.reg <> taicpu(hp2).Oper[1]^.reg) then
  4324. begin
  4325. taicpu(hp2).loadReg(0,taicpu(hp1).Oper[0]^.reg);
  4326. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  4327. end
  4328. else
  4329. begin
  4330. RemoveInstruction(hp2);
  4331. end
  4332. {$endif i386}
  4333. ;
  4334. end;
  4335. end
  4336. { movl [mem1],reg1
  4337. movl [mem1],reg2
  4338. to
  4339. movl [mem1],reg1
  4340. movl reg1,reg2
  4341. }
  4342. else if not CheckMovMov2MovMov2(p, hp1) and
  4343. { movl const1,[mem1]
  4344. movl [mem1],reg1
  4345. to
  4346. movl const1,reg1
  4347. movl reg1,[mem1]
  4348. }
  4349. MatchOpType(Taicpu(p),top_const,top_ref) and
  4350. MatchOpType(Taicpu(hp1),top_ref,top_reg) and
  4351. (taicpu(p).opsize = taicpu(hp1).opsize) and
  4352. RefsEqual(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.ref^) and
  4353. not(RegInRef(taicpu(hp1).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^)) then
  4354. begin
  4355. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  4356. taicpu(hp1).loadReg(0,taicpu(hp1).oper[1]^.reg);
  4357. taicpu(hp1).loadRef(1,taicpu(p).oper[1]^.ref^);
  4358. taicpu(p).loadReg(1,taicpu(hp1).oper[0]^.reg);
  4359. taicpu(hp1).fileinfo := taicpu(p).fileinfo;
  4360. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 1',p);
  4361. Result:=true;
  4362. exit;
  4363. end;
  4364. { mov x,reg1; mov y,reg1 -> mov y,reg1 is handled by the Mov2Nop 5 optimisation }
  4365. end;
  4366. { search further than the next instruction for a mov (as long as it's not a jump) }
  4367. if not is_calljmpuncondret(taicpu(hp1).opcode) and
  4368. { check as much as possible before the expensive GetNextInstructionUsingRegCond call }
  4369. (taicpu(p).oper[1]^.typ = top_reg) and
  4370. (taicpu(p).oper[0]^.typ in [top_reg,top_const]) and
  4371. not RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp1) then
  4372. begin
  4373. { we work with hp2 here, so hp1 can be still used later on when
  4374. checking for GetNextInstruction_p }
  4375. hp3 := hp1;
  4376. { Initialise CrossJump (if it becomes True at any point, it will remain True) }
  4377. CrossJump := (taicpu(hp1).opcode = A_Jcc);
  4378. { Remember that p_TargetReg contains taicpu(p).oper[1]^.reg }
  4379. TransferUsedRegs(TmpUsedRegs);
  4380. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4381. if NotFirstIteration then
  4382. JumpTracking := TLinkedList.Create
  4383. else
  4384. JumpTracking := nil;
  4385. while GetNextInstructionUsingRegCond(hp3,hp2,p_TargetReg,JumpTracking,CrossJump) and
  4386. { GetNextInstructionUsingRegCond only searches one instruction ahead unless -O3 is specified }
  4387. (hp2.typ=ait_instruction) do
  4388. begin
  4389. case taicpu(hp2).opcode of
  4390. A_POP:
  4391. if MatchOperand(taicpu(hp2).oper[0]^,p_TargetReg) then
  4392. begin
  4393. if not CrossJump and
  4394. not RegUsedBetween(p_TargetReg, p, hp2) then
  4395. begin
  4396. { We can remove the original MOV since the register
  4397. wasn't used between it and its popping from the stack }
  4398. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3c done',p);
  4399. RemoveCurrentp(p, hp1);
  4400. Result := True;
  4401. JumpTracking.Free;
  4402. Exit;
  4403. end;
  4404. { Can't go any further }
  4405. Break;
  4406. end;
  4407. A_MOV:
  4408. if MatchOperand(taicpu(hp2).oper[0]^,p_TargetReg) and
  4409. ((taicpu(p).oper[0]^.typ=top_const) or
  4410. ((taicpu(p).oper[0]^.typ=top_reg) and
  4411. not(RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp2))
  4412. )
  4413. ) then
  4414. begin
  4415. { we have
  4416. mov x, %treg
  4417. mov %treg, y
  4418. }
  4419. { We don't need to call UpdateUsedRegs for every instruction between
  4420. p and hp2 because the register we're concerned about will not
  4421. become deallocated (otherwise GetNextInstructionUsingReg would
  4422. have stopped at an earlier instruction). [Kit] }
  4423. TempRegUsed :=
  4424. CrossJump { Assume the register is in use if it crossed a conditional jump } or
  4425. RegReadByInstruction(p_TargetReg, hp3) or
  4426. RegUsedAfterInstruction(p_TargetReg, hp2, TmpUsedRegs);
  4427. case taicpu(p).oper[0]^.typ Of
  4428. top_reg:
  4429. begin
  4430. { change
  4431. mov %reg, %treg
  4432. mov %treg, y
  4433. to
  4434. mov %reg, y
  4435. }
  4436. p_SourceReg := taicpu(p).oper[0]^.reg; { Saves on a handful of pointer dereferences }
  4437. RegName1 := debug_regname(taicpu(hp2).oper[0]^.reg);
  4438. if MatchOperand(taicpu(hp2).oper[1]^, p_SourceReg) then
  4439. begin
  4440. { %reg = y - remove hp2 completely (doing it here instead of relying on
  4441. the "mov %reg,%reg" optimisation might cut down on a pass iteration) }
  4442. if TempRegUsed then
  4443. begin
  4444. DebugMsg(SPeepholeOptimization + debug_regname(p_SourceReg) + ' = ' + RegName1 + '; removed unnecessary instruction (MovMov2MovNop 6b}',hp2);
  4445. AllocRegBetween(p_SourceReg, p, hp2, UsedRegs);
  4446. { Set the start of the next GetNextInstructionUsingRegCond search
  4447. to start at the entry right before hp2 (which is about to be removed) }
  4448. hp3 := tai(hp2.Previous);
  4449. RemoveInstruction(hp2);
  4450. Include(OptsToCheck, aoc_ForceNewIteration);
  4451. { See if there's more we can optimise }
  4452. Continue;
  4453. end
  4454. else
  4455. begin
  4456. RemoveInstruction(hp2);
  4457. { We can remove the original MOV too }
  4458. DebugMsg(SPeepholeOptimization + 'MovMov2NopNop 6b done',p);
  4459. RemoveCurrentP(p, hp1);
  4460. Result:=true;
  4461. JumpTracking.Free;
  4462. Exit;
  4463. end;
  4464. end
  4465. else
  4466. begin
  4467. AllocRegBetween(p_SourceReg, p, hp2, UsedRegs);
  4468. taicpu(hp2).loadReg(0, p_SourceReg);
  4469. DebugMsg(SPeepholeOptimization + RegName1 + ' = ' + debug_regname(p_SourceReg) + '; changed to minimise pipeline stall (MovMov2Mov 6a}',hp2);
  4470. { Check to see if the register also appears in the reference }
  4471. if (taicpu(hp2).oper[1]^.typ = top_ref) then
  4472. ReplaceRegisterInRef(taicpu(hp2).oper[1]^.ref^, p_TargetReg, p_SourceReg);
  4473. { ReplaceRegisterInRef won't actually replace the register if it's a different size }
  4474. if not RegInOp(p_TargetReg, taicpu(hp2).oper[1]^) then
  4475. begin
  4476. { Don't remove the first instruction if the temporary register is in use }
  4477. if not TempRegUsed then
  4478. begin
  4479. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 6 done',p);
  4480. RemoveCurrentP(p, hp1);
  4481. Result:=true;
  4482. JumpTracking.Free;
  4483. Exit;
  4484. end;
  4485. { No need to set Result to True here. If there's another instruction later
  4486. on that can be optimised, it will be detected when the main Pass 1 loop
  4487. reaches what is now hp2 and passes it through OptPass1MOV. [Kit] }
  4488. hp3 := hp2;
  4489. Continue;
  4490. end;
  4491. end;
  4492. end;
  4493. top_const:
  4494. if not (cs_opt_size in current_settings.optimizerswitches) or (taicpu(hp2).opsize = S_B) then
  4495. begin
  4496. { change
  4497. mov const, %treg
  4498. mov %treg, y
  4499. to
  4500. mov const, y
  4501. }
  4502. if (taicpu(hp2).oper[1]^.typ=top_reg) or
  4503. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  4504. begin
  4505. RegName1 := debug_regname(taicpu(hp2).oper[0]^.reg);
  4506. taicpu(hp2).loadOper(0,taicpu(p).oper[0]^);
  4507. if TempRegUsed then
  4508. begin
  4509. { Don't remove the first instruction if the temporary register is in use }
  4510. DebugMsg(SPeepholeOptimization + RegName1 + ' = ' + debug_tostr(taicpu(p).oper[0]^.val) + '; changed to minimise pipeline stall (MovMov2Mov 7a)',hp2);
  4511. { No need to set Result to True. If there's another instruction later on
  4512. that can be optimised, it will be detected when the main Pass 1 loop
  4513. reaches what is now hp2 and passes it through OptPass1MOV. [Kit] };
  4514. end
  4515. else
  4516. begin
  4517. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 7 done',p);
  4518. RemoveCurrentP(p, hp1);
  4519. Result:=true;
  4520. Exit;
  4521. end;
  4522. end;
  4523. end;
  4524. else
  4525. Internalerror(2019103001);
  4526. end;
  4527. end
  4528. else if MatchOperand(taicpu(hp2).oper[1]^, p_TargetReg) then
  4529. begin
  4530. if not CrossJump and
  4531. not RegUsedBetween(p_TargetReg, p, hp2) and
  4532. not RegReadByInstruction(p_TargetReg, hp2) then
  4533. begin
  4534. { Register is not used before it is overwritten }
  4535. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3a done',p);
  4536. RemoveCurrentp(p, hp1);
  4537. Result := True;
  4538. Exit;
  4539. end;
  4540. if (taicpu(p).oper[0]^.typ = top_const) and
  4541. (taicpu(hp2).oper[0]^.typ = top_const) then
  4542. begin
  4543. if taicpu(p).oper[0]^.val = taicpu(hp2).oper[0]^.val then
  4544. begin
  4545. { Same value - register hasn't changed }
  4546. DebugMsg(SPeepholeOptimization + 'Mov2Nop 2 done', hp2);
  4547. RemoveInstruction(hp2);
  4548. Include(OptsToCheck, aoc_ForceNewIteration);
  4549. { See if there's more we can optimise }
  4550. Continue;
  4551. end;
  4552. end;
  4553. {$ifdef x86_64}
  4554. end
  4555. { Change:
  4556. movl %reg1l,%reg2l
  4557. ...
  4558. movq %reg2q,%reg3q (%reg1 <> %reg3)
  4559. To:
  4560. movl %reg1l,%reg2l
  4561. ...
  4562. movl %reg1l,%reg3l (Upper 32 bits of %reg3q will be zero)
  4563. If %reg1 = %reg3, convert to:
  4564. movl %reg1l,%reg2l
  4565. ...
  4566. andl %reg1l,%reg1l
  4567. }
  4568. else if (taicpu(p).opsize = S_L) and MatchInstruction(hp2,A_MOV,[S_Q]) and
  4569. (taicpu(p).oper[0]^.typ = top_reg) and
  4570. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  4571. SuperRegistersEqual(p_TargetReg, taicpu(hp2).oper[0]^.reg) and
  4572. not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp2) then
  4573. begin
  4574. TempRegUsed :=
  4575. CrossJump { Assume the register is in use if it crossed a conditional jump } or
  4576. RegReadByInstruction(p_TargetReg, hp3) or
  4577. RegUsedAfterInstruction(p_TargetReg, hp2, TmpUsedRegs);
  4578. taicpu(hp2).opsize := S_L;
  4579. taicpu(hp2).loadreg(0, taicpu(p).oper[0]^.reg);
  4580. setsubreg(taicpu(hp2).oper[1]^.reg, R_SUBD);
  4581. AllocRegBetween(taicpu(p).oper[0]^.reg, p, hp2, UsedRegs);
  4582. if (taicpu(p).oper[0]^.reg = taicpu(hp2).oper[1]^.reg) then
  4583. begin
  4584. { %reg1 = %reg3 }
  4585. DebugMsg(SPeepholeOptimization + 'Made 32-to-64-bit zero extension more efficient (MovlMovq2MovlAndl 2)', hp2);
  4586. taicpu(hp2).opcode := A_AND;
  4587. end
  4588. else
  4589. begin
  4590. { %reg1 <> %reg3 }
  4591. DebugMsg(SPeepholeOptimization + 'Made 32-to-64-bit zero extension more efficient (MovlMovq2MovlMovl 2)', hp2);
  4592. end;
  4593. if not TempRegUsed then
  4594. begin
  4595. DebugMsg(SPeepholeOptimization + 'Mov2Nop 8a done', p);
  4596. RemoveCurrentP(p, hp1);
  4597. Result := True;
  4598. Exit;
  4599. end
  4600. else
  4601. begin
  4602. { Initial instruction wasn't actually changed }
  4603. Include(OptsToCheck, aoc_ForceNewIteration);
  4604. { if %reg1 = %reg3, don't do the long-distance lookahead that
  4605. appears below since %reg1 has technically changed }
  4606. if taicpu(hp2).opcode = A_AND then
  4607. Break;
  4608. end;
  4609. {$endif x86_64}
  4610. end
  4611. else if (taicpu(hp2).oper[0]^.typ = top_ref) and
  4612. GetNextInstruction(hp2, hp4) and
  4613. (hp4.typ = ait_instruction) and (taicpu(hp4).opcode = A_MOV) then
  4614. { Optimise the following first:
  4615. movl [mem1],reg1
  4616. movl [mem1],reg2
  4617. to
  4618. movl [mem1],reg1
  4619. movl reg1,reg2
  4620. If [mem1] contains the target register and reg1 is the
  4621. the source register, this optimisation will get missed
  4622. and produce less efficient code later on.
  4623. }
  4624. if CheckMovMov2MovMov2(hp2, hp4) then
  4625. { Initial instruction wasn't actually changed }
  4626. Include(OptsToCheck, aoc_ForceNewIteration);
  4627. A_MOVZX, A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}:
  4628. if MatchOpType(taicpu(hp2), top_reg, top_reg) and
  4629. MatchOperand(taicpu(hp2).oper[0]^, p_TargetReg) and
  4630. SuperRegistersEqual(taicpu(hp2).oper[1]^.reg, p_TargetReg) then
  4631. begin
  4632. {
  4633. Change from:
  4634. mov ###, %reg
  4635. ...
  4636. movs/z %reg,%reg (Same register, just different sizes)
  4637. To:
  4638. movs/z ###, %reg (Longer version)
  4639. ...
  4640. (remove)
  4641. }
  4642. DebugMsg(SPeepholeOptimization + 'MovMovs/z2Mov/s/z done', p);
  4643. taicpu(p).oper[1]^.reg := taicpu(hp2).oper[1]^.reg;
  4644. { Keep the first instruction as mov if ### is a constant }
  4645. if taicpu(p).oper[0]^.typ = top_const then
  4646. taicpu(p).opsize := reg2opsize(taicpu(hp2).oper[1]^.reg)
  4647. else
  4648. begin
  4649. taicpu(p).opcode := taicpu(hp2).opcode;
  4650. taicpu(p).opsize := taicpu(hp2).opsize;
  4651. end;
  4652. DebugMsg(SPeepholeOptimization + 'Removed movs/z instruction and extended earlier write (MovMovs/z2Mov/s/z)', hp2);
  4653. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp2, UsedRegs);
  4654. RemoveInstruction(hp2);
  4655. Result := True;
  4656. JumpTracking.Free;
  4657. Exit;
  4658. end;
  4659. else
  4660. { Move down to the if-block below };
  4661. end;
  4662. { Also catches MOV/S/Z instructions that aren't modified }
  4663. if taicpu(p).oper[0]^.typ = top_reg then
  4664. begin
  4665. p_SourceReg := taicpu(p).oper[0]^.reg;
  4666. if
  4667. not RegModifiedByInstruction(p_SourceReg, hp3) and
  4668. not RegModifiedBetween(p_SourceReg, hp3, hp2) and
  4669. DeepMOVOpt(taicpu(p), taicpu(hp2)) then
  4670. begin
  4671. Result := True;
  4672. { Just in case something didn't get modified (e.g. an
  4673. implicit register). Also, if it does read from this
  4674. register, then there's no longer an advantage to
  4675. changing the register on subsequent instructions.}
  4676. if not RegReadByInstruction(p_TargetReg, hp2) then
  4677. begin
  4678. { If a conditional jump was crossed, do not delete
  4679. the original MOV no matter what }
  4680. if not CrossJump and
  4681. { RegEndOfLife returns True if the register is
  4682. deallocated before the next instruction or has
  4683. been loaded with a new value }
  4684. RegEndOfLife(p_TargetReg, taicpu(hp2)) then
  4685. begin
  4686. { We can remove the original MOV }
  4687. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3b done',p);
  4688. RemoveCurrentp(p, hp1);
  4689. JumpTracking.Free;
  4690. Result := True;
  4691. Exit;
  4692. end;
  4693. if not RegModifiedByInstruction(p_TargetReg, hp2) then
  4694. begin
  4695. { See if there's more we can optimise }
  4696. hp3 := hp2;
  4697. Continue;
  4698. end;
  4699. end;
  4700. end;
  4701. end;
  4702. { Break out of the while loop under normal circumstances }
  4703. Break;
  4704. end;
  4705. JumpTracking.Free;
  4706. end;
  4707. if (aoc_MovAnd2Mov_3 in OptsToCheck) and
  4708. (taicpu(p).oper[1]^.typ = top_reg) and
  4709. (taicpu(p).opsize = S_L) and
  4710. GetNextInstructionUsingRegTrackingUse(p,hp2,taicpu(p).oper[1]^.reg) and
  4711. (hp2.typ = ait_instruction) and
  4712. (taicpu(hp2).opcode = A_AND) and
  4713. (MatchOpType(taicpu(hp2),top_const,top_reg) or
  4714. (MatchOpType(taicpu(hp2),top_reg,top_reg) and
  4715. MatchOperand(taicpu(hp2).oper[0]^,taicpu(hp2).oper[1]^))
  4716. ) then
  4717. begin
  4718. if SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp2).oper[1]^.reg) then
  4719. begin
  4720. if ((taicpu(hp2).oper[0]^.typ=top_const) and (taicpu(hp2).oper[0]^.val = $ffffffff)) or
  4721. ((taicpu(hp2).oper[0]^.typ=top_reg) and (taicpu(hp2).opsize=S_L)) then
  4722. begin
  4723. { Optimize out:
  4724. mov x, %reg
  4725. and ffffffffh, %reg
  4726. }
  4727. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 3 done',p);
  4728. RemoveInstruction(hp2);
  4729. Result:=true;
  4730. exit;
  4731. end;
  4732. end;
  4733. end;
  4734. { leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  4735. x >= RetOffset) as it doesn't do anything (it writes either to a
  4736. parameter or to the temporary storage room for the function
  4737. result)
  4738. }
  4739. if IsExitCode(hp1) and
  4740. (taicpu(p).oper[1]^.typ = top_ref) and
  4741. (taicpu(p).oper[1]^.ref^.index = NR_NO) and
  4742. (
  4743. (
  4744. (taicpu(p).oper[1]^.ref^.base = current_procinfo.FramePointer) and
  4745. not (
  4746. assigned(current_procinfo.procdef.funcretsym) and
  4747. (taicpu(p).oper[1]^.ref^.offset <= tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)
  4748. )
  4749. ) or
  4750. { Also discard writes to the stack that are below the base pointer,
  4751. as this is temporary storage rather than a function result on the
  4752. stack, say. }
  4753. (
  4754. (taicpu(p).oper[1]^.ref^.base = NR_STACK_POINTER_REG) and
  4755. (taicpu(p).oper[1]^.ref^.offset < current_procinfo.final_localsize)
  4756. )
  4757. ) then
  4758. begin
  4759. RemoveCurrentp(p, hp1);
  4760. DebugMsg(SPeepholeOptimization + 'removed deadstore before leave/ret',p);
  4761. RemoveLastDeallocForFuncRes(p);
  4762. Result:=true;
  4763. exit;
  4764. end;
  4765. if MatchInstruction(hp1,A_CMP,A_TEST,[taicpu(p).opsize]) then
  4766. begin
  4767. if MatchOpType(taicpu(p),top_reg,top_ref) and
  4768. (taicpu(hp1).oper[1]^.typ = top_ref) and
  4769. RefsEqual(taicpu(p).oper[1]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  4770. begin
  4771. { change
  4772. mov reg1, mem1
  4773. test/cmp x, mem1
  4774. to
  4775. mov reg1, mem1
  4776. test/cmp x, reg1
  4777. }
  4778. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  4779. DebugMsg(SPeepholeOptimization + 'MovTestCmp2MovTestCmp 1',hp1);
  4780. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  4781. Result := True;
  4782. Exit;
  4783. end;
  4784. if DoMovCmpMemOpt(p, hp1) then
  4785. begin
  4786. Result := True;
  4787. Exit;
  4788. end;
  4789. end;
  4790. if (taicpu(p).oper[1]^.typ = top_reg) and
  4791. (hp1.typ = ait_instruction) and
  4792. GetNextInstruction(hp1, hp2) and
  4793. MatchInstruction(hp2,A_MOV,[]) and
  4794. (SuperRegistersEqual(taicpu(hp2).oper[0]^.reg,taicpu(p).oper[1]^.reg)) and
  4795. (topsize2memsize[taicpu(hp1).opsize]>=topsize2memsize[taicpu(hp2).opsize]) and
  4796. (
  4797. IsFoldableArithOp(taicpu(hp1), taicpu(p).oper[1]^.reg)
  4798. {$ifdef x86_64}
  4799. or
  4800. (
  4801. (taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and (taicpu(hp2).opsize=S_L) and
  4802. IsFoldableArithOp(taicpu(hp1), newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[1]^.reg),R_SUBQ))
  4803. )
  4804. {$endif x86_64}
  4805. ) then
  4806. begin
  4807. if OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  4808. (taicpu(hp2).oper[0]^.typ=top_reg) then
  4809. { change movsX/movzX reg/ref, reg2
  4810. add/sub/or/... reg3/$const, reg2
  4811. mov reg2 reg/ref
  4812. dealloc reg2
  4813. to
  4814. add/sub/or/... reg3/$const, reg/ref }
  4815. begin
  4816. TransferUsedRegs(TmpUsedRegs);
  4817. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4818. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  4819. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  4820. begin
  4821. { by example:
  4822. movswl %si,%eax movswl %si,%eax p
  4823. decl %eax addl %edx,%eax hp1
  4824. movw %ax,%si movw %ax,%si hp2
  4825. ->
  4826. movswl %si,%eax movswl %si,%eax p
  4827. decw %eax addw %edx,%eax hp1
  4828. movw %ax,%si movw %ax,%si hp2
  4829. }
  4830. DebugMsg(SPeepholeOptimization + 'MovOpMov2Op ('+
  4831. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  4832. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  4833. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  4834. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  4835. {
  4836. ->
  4837. movswl %si,%eax movswl %si,%eax p
  4838. decw %si addw %dx,%si hp1
  4839. movw %ax,%si movw %ax,%si hp2
  4840. }
  4841. case taicpu(hp1).ops of
  4842. 1:
  4843. begin
  4844. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  4845. if taicpu(hp1).oper[0]^.typ=top_reg then
  4846. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  4847. end;
  4848. 2:
  4849. begin
  4850. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  4851. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  4852. (taicpu(hp1).opcode<>A_SHL) and
  4853. (taicpu(hp1).opcode<>A_SHR) and
  4854. (taicpu(hp1).opcode<>A_SAR) then
  4855. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  4856. end;
  4857. else
  4858. internalerror(2008042701);
  4859. end;
  4860. {
  4861. ->
  4862. decw %si addw %dx,%si p
  4863. }
  4864. RemoveInstruction(hp2);
  4865. RemoveCurrentP(p, hp1);
  4866. Result:=True;
  4867. Exit;
  4868. end;
  4869. end;
  4870. if MatchOpType(taicpu(hp2),top_reg,top_reg) and
  4871. not(SuperRegistersEqual(taicpu(hp1).oper[0]^.reg,taicpu(hp2).oper[1]^.reg)) and
  4872. ((topsize2memsize[taicpu(hp1).opsize]<= topsize2memsize[taicpu(hp2).opsize]) or
  4873. { opsize matters for these opcodes, we could probably work around this, but it is not worth the effort }
  4874. ((taicpu(hp1).opcode<>A_SHL) and (taicpu(hp1).opcode<>A_SHR) and (taicpu(hp1).opcode<>A_SAR))
  4875. ) and
  4876. { if ref contains a symbol, we cannot change its size to a smaller size }
  4877. ((taicpu(p).oper[0]^.typ<>top_ref) or (taicpu(p).oper[0]^.ref^.symbol=nil) or
  4878. (topsize2memsize[taicpu(p).opsize]<=topsize2memsize[taicpu(hp2).opsize])
  4879. )
  4880. {$ifdef i386}
  4881. { byte registers of esi, edi, ebp, esp are not available on i386 }
  4882. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  4883. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(p).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  4884. {$endif i386}
  4885. then
  4886. { change movsX/movzX reg/ref, reg2
  4887. add/sub/or/... regX/$const, reg2
  4888. mov reg2, reg3
  4889. dealloc reg2
  4890. to
  4891. movsX/movzX reg/ref, reg3
  4892. add/sub/or/... reg3/$const, reg3
  4893. }
  4894. begin
  4895. TransferUsedRegs(TmpUsedRegs);
  4896. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4897. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  4898. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  4899. begin
  4900. { by example:
  4901. movswl %si,%eax movswl %si,%eax p
  4902. decl %eax addl %edx,%eax hp1
  4903. movw %ax,%si movw %ax,%si hp2
  4904. ->
  4905. movswl %si,%eax movswl %si,%eax p
  4906. decw %eax addw %edx,%eax hp1
  4907. movw %ax,%si movw %ax,%si hp2
  4908. }
  4909. DebugMsg(SPeepholeOptimization + 'MovOpMov2MovOp ('+
  4910. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  4911. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  4912. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  4913. { limit size of constants as well to avoid assembler errors, but
  4914. check opsize to avoid overflow when left shifting the 1 }
  4915. if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=63) then
  4916. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl topsize2memsize[taicpu(hp2).opsize])-1);
  4917. {$ifdef x86_64}
  4918. { Be careful of, for example:
  4919. movl %reg1,%reg2
  4920. addl %reg3,%reg2
  4921. movq %reg2,%reg4
  4922. This will cause problems if the upper 32-bits of %reg3 or %reg4 are non-zero
  4923. }
  4924. if (taicpu(hp1).opsize = S_L) and (taicpu(hp2).opsize = S_Q) then
  4925. begin
  4926. taicpu(hp2).changeopsize(S_L);
  4927. setsubreg(taicpu(hp2).oper[0]^.reg, R_SUBD);
  4928. setsubreg(taicpu(hp2).oper[1]^.reg, R_SUBD);
  4929. end;
  4930. {$endif x86_64}
  4931. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  4932. taicpu(p).changeopsize(taicpu(hp2).opsize);
  4933. if taicpu(p).oper[0]^.typ=top_reg then
  4934. setsubreg(taicpu(p).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  4935. taicpu(p).loadoper(1, taicpu(hp2).oper[1]^);
  4936. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,usedregs);
  4937. {
  4938. ->
  4939. movswl %si,%eax movswl %si,%eax p
  4940. decw %si addw %dx,%si hp1
  4941. movw %ax,%si movw %ax,%si hp2
  4942. }
  4943. case taicpu(hp1).ops of
  4944. 1:
  4945. begin
  4946. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  4947. if taicpu(hp1).oper[0]^.typ=top_reg then
  4948. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  4949. end;
  4950. 2:
  4951. begin
  4952. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  4953. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  4954. (taicpu(hp1).opcode<>A_SHL) and
  4955. (taicpu(hp1).opcode<>A_SHR) and
  4956. (taicpu(hp1).opcode<>A_SAR) then
  4957. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  4958. end;
  4959. else
  4960. internalerror(2018111801);
  4961. end;
  4962. {
  4963. ->
  4964. decw %si addw %dx,%si p
  4965. }
  4966. RemoveInstruction(hp2);
  4967. end;
  4968. end;
  4969. end;
  4970. if MatchInstruction(hp1,A_BTS,A_BTR,[Taicpu(p).opsize]) and
  4971. GetNextInstruction(hp1, hp2) and
  4972. MatchInstruction(hp2,A_OR,[Taicpu(p).opsize]) and
  4973. MatchOperand(Taicpu(p).oper[0]^,0) and
  4974. (Taicpu(p).oper[1]^.typ = top_reg) and
  4975. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp1).oper[1]^) and
  4976. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp2).oper[1]^) then
  4977. { mov reg1,0
  4978. bts reg1,operand1 --> mov reg1,operand2
  4979. or reg1,operand2 bts reg1,operand1}
  4980. begin
  4981. Taicpu(hp2).opcode:=A_MOV;
  4982. DebugMsg(SPeepholeOptimization + 'MovBtsOr2MovBts done',hp1);
  4983. asml.remove(hp1);
  4984. insertllitem(hp2,hp2.next,hp1);
  4985. RemoveCurrentp(p, hp1);
  4986. Result:=true;
  4987. exit;
  4988. end;
  4989. if MatchInstruction(hp1,A_SUB,[Taicpu(p).opsize]) and
  4990. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp1).oper[1]^) and
  4991. GetNextInstruction(hp1, hp2) and
  4992. MatchInstruction(hp2,A_CMP,[Taicpu(p).opsize]) and
  4993. MatchOperand(Taicpu(p).oper[0]^,Taicpu(hp2).oper[1]^) and
  4994. MatchOperand(Taicpu(hp1).oper[0]^,Taicpu(hp2).oper[0]^) then
  4995. { change
  4996. mov reg1,reg2
  4997. sub reg3,reg2
  4998. cmp reg3,reg1
  4999. into
  5000. mov reg1,reg2
  5001. sub reg3,reg2
  5002. }
  5003. begin
  5004. DebugMsg(SPeepholeOptimization + 'MovSubCmp2MovSub done',p);
  5005. RemoveInstruction(hp2);
  5006. Result:=true;
  5007. exit;
  5008. end;
  5009. if (taicpu(p).oper[0]^.typ = top_ref) and { Second operand will be a register }
  5010. MatchInstruction(hp1, A_SHR, A_SAR, [taicpu(p).opsize]) and
  5011. MatchOpType(taicpu(hp1), top_const, top_reg) and
  5012. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  5013. begin
  5014. RegName1 := debug_regname(taicpu(hp1).oper[1]^.reg);
  5015. {$ifdef x86_64}
  5016. { Convert:
  5017. movq x(ref),%reg64
  5018. shrq y,%reg64
  5019. To:
  5020. movl x+4(ref),%reg32
  5021. shrl y-32,%reg32 (Remove if y = 32)
  5022. }
  5023. if (taicpu(p).opsize = S_Q) and
  5024. (taicpu(hp1).opcode = A_SHR) and
  5025. (taicpu(hp1).oper[0]^.val >= 32) then
  5026. begin
  5027. PreMessage := 'movq ' + debug_operstr(taicpu(p).oper[0]^) + ',' + RegName1 + '; ' +
  5028. 'shrq $' + debug_tostr(taicpu(hp1).oper[0]^.val) + ',' + RegName1 + ' -> movl ';
  5029. { Convert to 32-bit }
  5030. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5031. taicpu(p).opsize := S_L;
  5032. Inc(taicpu(p).oper[0]^.ref^.offset, 4);
  5033. PreMessage := PreMessage + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg);
  5034. if (taicpu(hp1).oper[0]^.val = 32) then
  5035. begin
  5036. DebugMsg(SPeepholeOptimization + PreMessage + ' (MovShr2Mov)', p);
  5037. RemoveInstruction(hp1);
  5038. end
  5039. else
  5040. begin
  5041. { This will potentially open up more arithmetic operations since
  5042. the peephole optimizer now has a big hint that only the lower
  5043. 32 bits are currently in use (and opcodes are smaller in size) }
  5044. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  5045. taicpu(hp1).opsize := S_L;
  5046. Dec(taicpu(hp1).oper[0]^.val, 32);
  5047. DebugMsg(SPeepholeOptimization + PreMessage +
  5048. '; shrl $' + debug_tostr(taicpu(hp1).oper[0]^.val) + ',' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' (MovShr2MovShr)', p);
  5049. end;
  5050. Result := True;
  5051. Exit;
  5052. end;
  5053. {$endif x86_64}
  5054. { Convert:
  5055. movl x(ref),%reg
  5056. shrl $24,%reg
  5057. To:
  5058. movzbl x+3(ref),%reg
  5059. Do similar things for movl; shrl $16 -> movzwl and movw; shrw $8 -> movzbw
  5060. Also accept sar instead of shr, but convert to movsx instead of movzx
  5061. }
  5062. if taicpu(hp1).opcode = A_SHR then
  5063. MovUnaligned := A_MOVZX
  5064. else
  5065. MovUnaligned := A_MOVSX;
  5066. NewSize := S_NO;
  5067. NewOffset := 0;
  5068. case taicpu(p).opsize of
  5069. S_B:
  5070. { No valid combinations };
  5071. S_W:
  5072. if (taicpu(hp1).oper[0]^.val = 8) then
  5073. begin
  5074. NewSize := S_BW;
  5075. NewOffset := 1;
  5076. end;
  5077. S_L:
  5078. case taicpu(hp1).oper[0]^.val of
  5079. 16:
  5080. begin
  5081. NewSize := S_WL;
  5082. NewOffset := 2;
  5083. end;
  5084. 24:
  5085. begin
  5086. NewSize := S_BL;
  5087. NewOffset := 3;
  5088. end;
  5089. else
  5090. ;
  5091. end;
  5092. {$ifdef x86_64}
  5093. S_Q:
  5094. case taicpu(hp1).oper[0]^.val of
  5095. 32:
  5096. begin
  5097. if taicpu(hp1).opcode = A_SAR then
  5098. begin
  5099. { 32-bit to 64-bit is a distinct instruction }
  5100. MovUnaligned := A_MOVSXD;
  5101. NewSize := S_LQ;
  5102. NewOffset := 4;
  5103. end
  5104. else
  5105. { Should have been handled by MovShr2Mov above }
  5106. InternalError(2022081811);
  5107. end;
  5108. 48:
  5109. begin
  5110. NewSize := S_WQ;
  5111. NewOffset := 6;
  5112. end;
  5113. 56:
  5114. begin
  5115. NewSize := S_BQ;
  5116. NewOffset := 7;
  5117. end;
  5118. else
  5119. ;
  5120. end;
  5121. {$endif x86_64}
  5122. else
  5123. InternalError(2022081810);
  5124. end;
  5125. if (NewSize <> S_NO) and
  5126. (taicpu(p).oper[0]^.ref^.offset <= $7FFFFFFF - NewOffset) then
  5127. begin
  5128. PreMessage := 'mov' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + RegName1 + '; ' +
  5129. 'shr' + debug_opsize2str(taicpu(p).opsize) + ' $' + debug_tostr(taicpu(hp1).oper[0]^.val) + ',' + RegName1 + ' -> ' +
  5130. debug_op2str(MovUnaligned);
  5131. {$ifdef x86_64}
  5132. if MovUnaligned <> A_MOVSXD then
  5133. { Don't add size suffix for MOVSXD }
  5134. {$endif x86_64}
  5135. PreMessage := PreMessage + debug_opsize2str(NewSize);
  5136. Inc(taicpu(p).oper[0]^.ref^.offset, NewOffset);
  5137. taicpu(p).opcode := MovUnaligned;
  5138. taicpu(p).opsize := NewSize;
  5139. DebugMsg(SPeepholeOptimization + PreMessage + ' ' +
  5140. debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' (MovShr/Sar2Movx)', p);
  5141. RemoveInstruction(hp1);
  5142. Result := True;
  5143. Exit;
  5144. end;
  5145. end;
  5146. { Backward optimisation shared with OptPass2MOV }
  5147. if FuncMov2Func(p, hp1) then
  5148. begin
  5149. Result := True;
  5150. Exit;
  5151. end;
  5152. end;
  5153. function TX86AsmOptimizer.OptPass1MOVD(var p : tai) : boolean;
  5154. { This function also handles the 64-bit version, MOVQ }
  5155. var
  5156. hp1: tai;
  5157. begin
  5158. Result:=false;
  5159. { Change:
  5160. movd/q %xmm0, %reg
  5161. ...
  5162. movl/q %reg, (ref)
  5163. (dealloc %reg)
  5164. To:
  5165. movd/q %xmm0, (ref)
  5166. }
  5167. if MatchOpType(taicpu(p),top_reg,top_reg) and
  5168. (GetRegType(taicpu(p).oper[0]^.reg)=R_MMREGISTER) and
  5169. (GetRegType(taicpu(p).oper[1]^.reg)=R_INTREGISTER) and
  5170. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) and
  5171. MatchInstruction(hp1, A_MOV, []) and
  5172. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^.reg) and
  5173. (taicpu(hp1).oper[1]^.typ=top_ref) and
  5174. not RegInRef(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.ref^) then
  5175. begin
  5176. TransferUsedRegs(TmpUsedRegs);
  5177. UpdateUsedRegsBetween(TmpUsedRegs,p,hp1);
  5178. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs) then
  5179. begin
  5180. if (
  5181. { Instructions are always adjacent under -O2 and under }
  5182. not(cs_opt_level3 in current_settings.optimizerswitches) or
  5183. (
  5184. (
  5185. (taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  5186. not RegModifiedBetween(taicpu(hp1).oper[1]^.ref^.base,p,hp1)
  5187. ) and
  5188. (
  5189. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  5190. not RegModifiedBetween(taicpu(hp1).oper[1]^.ref^.index,p,hp1)
  5191. )
  5192. )
  5193. ) then
  5194. begin
  5195. DebugMsg(SPeepholeOptimization+'Merged (V)MOVD/(V)MOVQ and MOV to eliminate intermediate register (MovD/QMov2MovD/Q 1a)',p);
  5196. taicpu(p).loadref(1,taicpu(hp1).oper[1]^.ref^);
  5197. { loadref increases the reference count, so decrement it again }
  5198. if Assigned(taicpu(hp1).oper[1]^.ref^.symbol) then
  5199. taicpu(hp1).oper[1]^.ref^.symbol.decrefs;
  5200. if Assigned(taicpu(hp1).oper[1]^.ref^.relsymbol) then
  5201. taicpu(hp1).oper[1]^.ref^.relsymbol.decrefs;
  5202. RemoveInstruction(hp1);
  5203. Include(OptsToCheck, aoc_ForceNewIteration);
  5204. end
  5205. else if not RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1) then
  5206. begin
  5207. { Still possible to optimise if hp1 is converted instead }
  5208. DebugMsg(SPeepholeOptimization+'Merged (V)MOVD/(V)MOVQ and MOV to eliminate intermediate register (MovD/QMov2MovD/Q 1b)',hp1);
  5209. { Decrement the reference prior to replacing it }
  5210. if Assigned(taicpu(hp1).oper[1]^.ref^.symbol) then
  5211. taicpu(hp1).oper[1]^.ref^.symbol.decrefs;
  5212. if Assigned(taicpu(hp1).oper[1]^.ref^.relsymbol) then
  5213. taicpu(hp1).oper[1]^.ref^.relsymbol.decrefs;
  5214. taicpu(hp1).opcode:=taicpu(p).opcode;
  5215. taicpu(hp1).opsize:=taicpu(p).opsize;
  5216. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  5217. TransferUsedRegs(TmpUsedRegs);
  5218. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,TmpUsedRegs);
  5219. RemoveCurrentP(p);
  5220. Result:=True;
  5221. Exit;
  5222. end;
  5223. end;
  5224. end;
  5225. end;
  5226. function TX86AsmOptimizer.OptPass1MOVXX(var p : tai) : boolean;
  5227. var
  5228. hp1 : tai;
  5229. begin
  5230. Result:=false;
  5231. if taicpu(p).ops <> 2 then
  5232. exit;
  5233. if (MatchOpType(taicpu(p),top_reg,top_reg) and GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg)) or
  5234. GetNextInstruction(p,hp1) then
  5235. begin
  5236. if MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  5237. (taicpu(hp1).ops = 2) then
  5238. begin
  5239. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  5240. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  5241. { movXX reg1, mem1 or movXX mem1, reg1
  5242. movXX mem2, reg2 movXX reg2, mem2}
  5243. begin
  5244. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  5245. { movXX reg1, mem1 or movXX mem1, reg1
  5246. movXX mem2, reg1 movXX reg2, mem1}
  5247. begin
  5248. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  5249. begin
  5250. { Removes the second statement from
  5251. movXX reg1, mem1/reg2
  5252. movXX mem1/reg2, reg1
  5253. }
  5254. if taicpu(p).oper[0]^.typ=top_reg then
  5255. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  5256. { Removes the second statement from
  5257. movXX mem1/reg1, reg2
  5258. movXX reg2, mem1/reg1
  5259. }
  5260. if (taicpu(p).oper[1]^.typ=top_reg) and
  5261. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)) then
  5262. begin
  5263. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2Nop 1 done',p);
  5264. RemoveInstruction(hp1);
  5265. RemoveCurrentp(p); { p will now be equal to the instruction that follows what was hp1 }
  5266. Result:=true;
  5267. exit;
  5268. end
  5269. else if (taicpu(hp1).oper[1]^.typ<>top_ref) or (not(vol_write in taicpu(hp1).oper[1]^.ref^.volatility)) and
  5270. (taicpu(hp1).oper[0]^.typ<>top_ref) or (not(vol_read in taicpu(hp1).oper[0]^.ref^.volatility)) then
  5271. begin
  5272. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
  5273. RemoveInstruction(hp1);
  5274. Result:=true;
  5275. exit;
  5276. end;
  5277. end
  5278. end;
  5279. end;
  5280. end;
  5281. end;
  5282. end;
  5283. function TX86AsmOptimizer.OptPass1OP(var p : tai) : boolean;
  5284. var
  5285. hp1 : tai;
  5286. begin
  5287. result:=false;
  5288. { replace
  5289. <Op>X %mreg1,%mreg2 // Op in [ADD,MUL]
  5290. MovX %mreg2,%mreg1
  5291. dealloc %mreg2
  5292. by
  5293. <Op>X %mreg2,%mreg1
  5294. ?
  5295. }
  5296. if GetNextInstruction(p,hp1) and
  5297. { we mix single and double opperations here because we assume that the compiler
  5298. generates vmovapd only after double operations and vmovaps only after single operations }
  5299. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  5300. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  5301. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  5302. (taicpu(p).oper[0]^.typ=top_reg) then
  5303. begin
  5304. TransferUsedRegs(TmpUsedRegs);
  5305. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5306. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  5307. begin
  5308. taicpu(p).loadoper(0,taicpu(hp1).oper[0]^);
  5309. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  5310. DebugMsg(SPeepholeOptimization + 'OpMov2Op done',p);
  5311. RemoveInstruction(hp1);
  5312. result:=true;
  5313. end;
  5314. end;
  5315. end;
  5316. function TX86AsmOptimizer.OptPass1Test(var p: tai) : boolean;
  5317. var
  5318. hp1, p_label, p_dist, hp1_dist, hp1_last: tai;
  5319. JumpLabel, JumpLabel_dist: TAsmLabel;
  5320. FirstValue, SecondValue: TCGInt;
  5321. function OptimizeJump(var InputP: tai): Boolean;
  5322. var
  5323. TempBool: Boolean;
  5324. begin
  5325. Result := False;
  5326. TempBool := True;
  5327. if DoJumpOptimizations(InputP, TempBool) or
  5328. not TempBool then
  5329. begin
  5330. Result := True;
  5331. if Assigned(InputP) then
  5332. begin
  5333. { CollapseZeroDistJump will be set to the label or an align
  5334. before it after the jump if it optimises, whether or not
  5335. the label is live or dead }
  5336. if (InputP.typ = ait_align) or
  5337. (
  5338. (InputP.typ = ait_label) and
  5339. not (tai_label(InputP).labsym.is_used)
  5340. ) then
  5341. GetNextInstruction(InputP, InputP);
  5342. end;
  5343. Exit;
  5344. end;
  5345. end;
  5346. begin
  5347. Result := False;
  5348. if (taicpu(p).oper[0]^.typ = top_const) and
  5349. (taicpu(p).oper[0]^.val <> -1) then
  5350. begin
  5351. { Convert unsigned maximum constants to -1 to aid optimisation }
  5352. case taicpu(p).opsize of
  5353. S_B:
  5354. if (taicpu(p).oper[0]^.val and $FF) = $FF then
  5355. begin
  5356. taicpu(p).oper[0]^.val := -1;
  5357. Result := True;
  5358. Exit;
  5359. end;
  5360. S_W:
  5361. if (taicpu(p).oper[0]^.val and $FFFF) = $FFFF then
  5362. begin
  5363. taicpu(p).oper[0]^.val := -1;
  5364. Result := True;
  5365. Exit;
  5366. end;
  5367. S_L:
  5368. if (taicpu(p).oper[0]^.val and $FFFFFFFF) = $FFFFFFFF then
  5369. begin
  5370. taicpu(p).oper[0]^.val := -1;
  5371. Result := True;
  5372. Exit;
  5373. end;
  5374. {$ifdef x86_64}
  5375. S_Q:
  5376. { Storing anything greater than $7FFFFFFF is not possible so do
  5377. nothing };
  5378. {$endif x86_64}
  5379. else
  5380. InternalError(2021121001);
  5381. end;
  5382. end;
  5383. if GetNextInstruction(p, hp1) and
  5384. TrySwapMovCmp(p, hp1) then
  5385. begin
  5386. Result := True;
  5387. Exit;
  5388. end;
  5389. p_label := nil;
  5390. JumpLabel := nil;
  5391. if MatchInstruction(hp1, A_Jcc, []) then
  5392. begin
  5393. if OptimizeJump(hp1) then
  5394. begin
  5395. Result := True;
  5396. if Assigned(hp1) then
  5397. begin
  5398. { CollapseZeroDistJump will be set to the label or an align
  5399. before it after the jump if it optimises, whether or not
  5400. the label is live or dead }
  5401. if (hp1.typ = ait_align) or
  5402. (
  5403. (hp1.typ = ait_label) and
  5404. not (tai_label(hp1).labsym.is_used)
  5405. ) then
  5406. GetNextInstruction(hp1, hp1);
  5407. end;
  5408. TransferUsedRegs(TmpUsedRegs);
  5409. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  5410. if not Assigned(hp1) or
  5411. (
  5412. not MatchInstruction(hp1, A_Jcc, A_SETcc, A_CMOVcc, []) and
  5413. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)
  5414. ) then
  5415. begin
  5416. { No more conditional jumps; conditional statement is no longer required }
  5417. DebugMsg(SPeepholeOptimization + 'Removed unnecessary condition (Test2Nop)', p);
  5418. RemoveCurrentP(p);
  5419. end;
  5420. Exit;
  5421. end;
  5422. if IsJumpToLabel(taicpu(hp1)) then
  5423. begin
  5424. JumpLabel := TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol);
  5425. if Assigned(JumpLabel) then
  5426. p_label := getlabelwithsym(JumpLabel);
  5427. end;
  5428. end;
  5429. { Search for:
  5430. test $x,(reg/ref)
  5431. jne @lbl1
  5432. test $y,(reg/ref) (same register or reference)
  5433. jne @lbl1
  5434. Change to:
  5435. test $(x or y),(reg/ref)
  5436. jne @lbl1
  5437. (Note, this doesn't work with je instead of jne)
  5438. Also catch cases where "cmp $0,(reg/ref)" and "test %reg,%reg" are used.
  5439. Also search for:
  5440. test $x,(reg/ref)
  5441. je @lbl1
  5442. ...
  5443. test $y,(reg/ref)
  5444. je/jne @lbl2
  5445. If (x or y) = x, then the second jump is deterministic
  5446. }
  5447. if (
  5448. (
  5449. (taicpu(p).oper[0]^.typ = top_const) or
  5450. (
  5451. { test %reg,%reg can be considered equivalent to test, -1,%reg }
  5452. (taicpu(p).oper[0]^.typ = top_reg) and
  5453. MatchOperand(taicpu(p).oper[1]^, taicpu(p).oper[0]^.reg)
  5454. )
  5455. ) and
  5456. MatchInstruction(hp1, A_JCC, [])
  5457. ) then
  5458. begin
  5459. if (taicpu(p).oper[0]^.typ = top_reg) and
  5460. MatchOperand(taicpu(p).oper[1]^, taicpu(p).oper[0]^.reg) then
  5461. FirstValue := -1
  5462. else
  5463. FirstValue := taicpu(p).oper[0]^.val;
  5464. { If we have several test/jne's in a row, it might be the case that
  5465. the second label doesn't go to the same location, but the one
  5466. after it might (e.g. test; jne @lbl1; test; jne @lbl2; test @lbl1),
  5467. so accommodate for this with a while loop.
  5468. }
  5469. hp1_last := hp1;
  5470. while (
  5471. (
  5472. (taicpu(p).oper[1]^.typ = top_reg) and
  5473. GetNextInstructionUsingReg(hp1_last, p_dist, taicpu(p).oper[1]^.reg)
  5474. ) or GetNextInstruction(hp1_last, p_dist)
  5475. ) and (p_dist.typ = ait_instruction) do
  5476. begin
  5477. if (
  5478. (
  5479. (taicpu(p_dist).opcode = A_TEST) and
  5480. (
  5481. (taicpu(p_dist).oper[0]^.typ = top_const) or
  5482. { test %reg,%reg can be considered equivalent to test, -1,%reg }
  5483. MatchOperand(taicpu(p_dist).oper[1]^, taicpu(p_dist).oper[0]^)
  5484. )
  5485. ) or
  5486. (
  5487. { cmp 0,%reg = test %reg,%reg }
  5488. (taicpu(p_dist).opcode = A_CMP) and
  5489. MatchOperand(taicpu(p_dist).oper[0]^, 0)
  5490. )
  5491. ) and
  5492. { Make sure the destination operands are actually the same }
  5493. MatchOperand(taicpu(p_dist).oper[1]^, taicpu(p).oper[1]^) and
  5494. GetNextInstruction(p_dist, hp1_dist) and
  5495. MatchInstruction(hp1_dist, A_JCC, []) then
  5496. begin
  5497. if OptimizeJump(hp1_dist) then
  5498. begin
  5499. Result := True;
  5500. Exit;
  5501. end;
  5502. if
  5503. (taicpu(p_dist).opcode = A_CMP) { constant will be zero } or
  5504. (
  5505. (taicpu(p_dist).oper[0]^.typ = top_reg) and
  5506. MatchOperand(taicpu(p_dist).oper[1]^, taicpu(p_dist).oper[0]^.reg)
  5507. ) then
  5508. SecondValue := -1
  5509. else
  5510. SecondValue := taicpu(p_dist).oper[0]^.val;
  5511. { If both of the TEST constants are identical, delete the
  5512. second TEST that is unnecessary (be careful though, just
  5513. in case the flags are modified in between) }
  5514. if (FirstValue = SecondValue) then
  5515. begin
  5516. if condition_in(taicpu(hp1_dist).condition, taicpu(hp1).condition) then
  5517. begin
  5518. { Since the second jump's condition is a subset of the first, we
  5519. know it will never branch because the first jump dominates it.
  5520. Get it out of the way now rather than wait for the jump
  5521. optimisations for a speed boost. }
  5522. if IsJumpToLabel(taicpu(hp1_dist)) then
  5523. TAsmLabel(taicpu(hp1_dist).oper[0]^.ref^.symbol).DecRefs;
  5524. DebugMsg(SPeepholeOptimization + 'Removed dominated jump (via TEST/Jcc/TEST)', hp1_dist);
  5525. RemoveInstruction(hp1_dist);
  5526. Result := True;
  5527. end
  5528. else if condition_in(inverse_cond(taicpu(hp1).condition), taicpu(hp1_dist).condition) then
  5529. begin
  5530. { If the inverse of the first condition is a subset of the second,
  5531. the second one will definitely branch if the first one doesn't }
  5532. DebugMsg(SPeepholeOptimization + 'Conditional jump will always branch (via TEST/Jcc/TEST)', hp1_dist);
  5533. { We can remove the TEST instruction too }
  5534. DebugMsg(SPeepholeOptimization + 'TEST/Jcc/TEST; removed superfluous TEST', p_dist);
  5535. RemoveInstruction(p_dist);
  5536. MakeUnconditional(taicpu(hp1_dist));
  5537. RemoveDeadCodeAfterJump(hp1_dist);
  5538. { Since the jump is now unconditional, we can't
  5539. continue any further with this particular
  5540. optimisation. The original TEST is still intact
  5541. though, so there might be something else we can
  5542. do }
  5543. Include(OptsToCheck, aoc_ForceNewIteration);
  5544. Break;
  5545. end;
  5546. if Result or
  5547. { If a jump wasn't removed or made unconditional, only
  5548. remove the identical TEST instruction if the flags
  5549. weren't modified }
  5550. not RegModifiedBetween(NR_DEFAULTFLAGS, hp1, p_dist) then
  5551. begin
  5552. DebugMsg(SPeepholeOptimization + 'TEST/Jcc/TEST; removed superfluous TEST', p_dist);
  5553. RemoveInstruction(p_dist);
  5554. { If the jump was removed or made unconditional, we
  5555. don't need to allocate NR_DEFAULTFLAGS over the
  5556. entire range }
  5557. if not Result then
  5558. begin
  5559. { Mark the flags as 'in use' over the entire range }
  5560. AllocRegBetween(NR_DEFAULTFLAGS, hp1, hp1_dist, UsedRegs);
  5561. { Speed gain - continue search from the Jcc instruction }
  5562. hp1_last := hp1_dist;
  5563. { Only the TEST instruction was removed, and the
  5564. original was unchanged, so we can safely do
  5565. another iteration of the while loop }
  5566. Include(OptsToCheck, aoc_ForceNewIteration);
  5567. Continue;
  5568. end;
  5569. Exit;
  5570. end;
  5571. end;
  5572. hp1_last := nil;
  5573. if (taicpu(hp1).condition in [C_NE, C_NZ]) and
  5574. (
  5575. { In this situation, the TEST/JNE pairs must be adjacent (fixes #40366) }
  5576. { Always adjacent under -O2 and under }
  5577. not(cs_opt_level3 in current_settings.optimizerswitches) or
  5578. (
  5579. GetNextInstruction(hp1, hp1_last) and
  5580. (hp1_last = p_dist)
  5581. )
  5582. ) and
  5583. (
  5584. (
  5585. { Test the following variant:
  5586. test $x,(reg/ref)
  5587. jne @lbl1
  5588. test $y,(reg/ref)
  5589. je @lbl2
  5590. @lbl1:
  5591. Becomes:
  5592. test $(x or y),(reg/ref)
  5593. je @lbl2
  5594. @lbl1: (may become a dead label)
  5595. }
  5596. (taicpu(hp1_dist).condition in [C_E, C_Z]) and
  5597. GetNextInstruction(hp1_dist, hp1_last) and
  5598. (hp1_last = p_label)
  5599. ) or
  5600. (
  5601. (taicpu(hp1_dist).condition in [C_NE, C_NZ]) and
  5602. { If the first instruction is test %reg,%reg or test $-1,%reg,
  5603. then the second jump will never branch, so it can also be
  5604. removed regardless of where it goes }
  5605. (
  5606. (FirstValue = -1) or
  5607. (SecondValue = -1) or
  5608. MatchOperand(taicpu(hp1_dist).oper[0]^, taicpu(hp1).oper[0]^)
  5609. )
  5610. )
  5611. ) then
  5612. begin
  5613. { Same jump location... can be a register since nothing's changed }
  5614. { If any of the entries are equivalent to test %reg,%reg, then the
  5615. merged $(x or y) is also test %reg,%reg / test $-1,%reg }
  5616. taicpu(p).loadconst(0, FirstValue or SecondValue);
  5617. if (hp1_last = p_label) then
  5618. begin
  5619. { Variant }
  5620. DebugMsg(SPeepholeOptimization + 'TEST/JNE/TEST/JE/@Lbl merged', p);
  5621. RemoveInstruction(p_dist);
  5622. if Assigned(JumpLabel) then
  5623. JumpLabel.decrefs;
  5624. RemoveInstruction(hp1);
  5625. end
  5626. else
  5627. begin
  5628. { Only remove the second test if no jumps or other conditional instructions follow }
  5629. TransferUsedRegs(TmpUsedRegs);
  5630. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  5631. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  5632. UpdateUsedRegs(TmpUsedRegs, tai(p_dist.Next));
  5633. if not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1_dist, TmpUsedRegs) then
  5634. begin
  5635. DebugMsg(SPeepholeOptimization + 'TEST/JNE/TEST/JNE merged', p);
  5636. RemoveInstruction(p_dist);
  5637. { Remove the first jump, not the second, to keep
  5638. any register deallocations between the second
  5639. TEST/JNE pair in the same place. Aids future
  5640. optimisation. }
  5641. if Assigned(JumpLabel) then
  5642. JumpLabel.decrefs;
  5643. RemoveInstruction(hp1);
  5644. end
  5645. else
  5646. begin
  5647. DebugMsg(SPeepholeOptimization + 'TEST/JNE/TEST/JNE merged (second TEST preserved)', p);
  5648. if IsJumpToLabel(taicpu(hp1_dist)) then
  5649. TAsmLabel(taicpu(hp1_dist).oper[0]^.ref^.symbol).DecRefs;
  5650. { Remove second jump in this instance }
  5651. RemoveInstruction(hp1_dist);
  5652. end;
  5653. end;
  5654. Result := True;
  5655. Exit;
  5656. end;
  5657. end;
  5658. if { If -O2 and under, it may stop on any old instruction }
  5659. (cs_opt_level3 in current_settings.optimizerswitches) and
  5660. (taicpu(p).oper[1]^.typ = top_reg) and
  5661. not RegModifiedByInstruction(taicpu(p).oper[1]^.reg, p_dist) then
  5662. begin
  5663. hp1_last := p_dist;
  5664. Continue;
  5665. end;
  5666. Break;
  5667. end;
  5668. end;
  5669. { Search for:
  5670. test %reg,%reg
  5671. j(c1) @lbl1
  5672. ...
  5673. @lbl:
  5674. test %reg,%reg (same register)
  5675. j(c2) @lbl2
  5676. If c2 is a subset of c1, change to:
  5677. test %reg,%reg
  5678. j(c1) @lbl2
  5679. (@lbl1 may become a dead label as a result)
  5680. }
  5681. if (taicpu(p).oper[1]^.typ = top_reg) and
  5682. (taicpu(p).oper[0]^.typ = top_reg) and
  5683. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  5684. { p_label <> nil is a marker that hp1 is a Jcc to a label }
  5685. Assigned(p_label) and
  5686. GetNextInstruction(p_label, p_dist) and
  5687. MatchInstruction(p_dist, A_TEST, []) and
  5688. { It's fine if the second test uses smaller sub-registers }
  5689. (taicpu(p_dist).opsize <= taicpu(p).opsize) and
  5690. MatchOpType(taicpu(p_dist), top_reg, top_reg) and
  5691. SuperRegistersEqual(taicpu(p_dist).oper[0]^.reg, taicpu(p).oper[0]^.reg) and
  5692. SuperRegistersEqual(taicpu(p_dist).oper[1]^.reg, taicpu(p).oper[1]^.reg) and
  5693. GetNextInstruction(p_dist, hp1_dist) and
  5694. MatchInstruction(hp1_dist, A_JCC, []) then { This doesn't have to be an explicit label }
  5695. begin
  5696. JumpLabel_dist := TAsmLabel(taicpu(hp1_dist).oper[0]^.ref^.symbol);
  5697. if JumpLabel = JumpLabel_dist then
  5698. { This is an infinite loop }
  5699. Exit;
  5700. { Best optimisation when the first condition is a subset (or equal) of the second }
  5701. if condition_in(taicpu(hp1).condition, taicpu(hp1_dist).condition) then
  5702. begin
  5703. { Any registers used here will already be allocated }
  5704. if Assigned(JumpLabel) then
  5705. JumpLabel.DecRefs;
  5706. DebugMsg(SPeepholeOptimization + 'TEST/Jcc/@Lbl/TEST/Jcc -> TEST/Jcc, redirecting first jump', hp1);
  5707. taicpu(hp1).loadref(0, taicpu(hp1_dist).oper[0]^.ref^); { This also increases the reference count }
  5708. Result := True;
  5709. Exit;
  5710. end;
  5711. end;
  5712. end;
  5713. function TX86AsmOptimizer.OptPass1Add(var p : tai) : boolean;
  5714. var
  5715. hp1, hp2: tai;
  5716. ActiveReg: TRegister;
  5717. OldOffset: asizeint;
  5718. ThisConst: TCGInt;
  5719. function RegDeallocated: Boolean;
  5720. begin
  5721. TransferUsedRegs(TmpUsedRegs);
  5722. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5723. Result := not(RegUsedAfterInstruction(ActiveReg,hp1,TmpUsedRegs))
  5724. end;
  5725. begin
  5726. result:=false;
  5727. hp1 := nil;
  5728. { replace
  5729. addX const,%reg1
  5730. leaX (%reg1,%reg1,Y),%reg2 // Base or index might not be equal to reg1
  5731. dealloc %reg1
  5732. by
  5733. leaX const+const*Y(%reg1,%reg1,Y),%reg2
  5734. }
  5735. if MatchOpType(taicpu(p),top_const,top_reg) then
  5736. begin
  5737. ActiveReg := taicpu(p).oper[1]^.reg;
  5738. { Ensures the entire register was updated }
  5739. if (taicpu(p).opsize >= S_L) and
  5740. GetNextInstructionUsingReg(p,hp1, ActiveReg) and
  5741. MatchInstruction(hp1,A_LEA,[]) and
  5742. (SuperRegistersEqual(ActiveReg, taicpu(hp1).oper[0]^.ref^.base) or
  5743. SuperRegistersEqual(ActiveReg, taicpu(hp1).oper[0]^.ref^.index)) and
  5744. (
  5745. { Cover the case where the register in the reference is also the destination register }
  5746. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, ActiveReg) or
  5747. (
  5748. { Try to avoid the expensive check of RegUsedAfterInstruction if we know it will return False }
  5749. not SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ActiveReg) and
  5750. RegDeallocated
  5751. )
  5752. ) then
  5753. begin
  5754. OldOffset := taicpu(hp1).oper[0]^.ref^.offset;
  5755. {$push}
  5756. {$R-}{$Q-}
  5757. { Explicitly disable overflow checking for these offset calculation
  5758. as those do not matter for the final result }
  5759. if ActiveReg=taicpu(hp1).oper[0]^.ref^.base then
  5760. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.val);
  5761. if ActiveReg=taicpu(hp1).oper[0]^.ref^.index then
  5762. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.val*max(taicpu(hp1).oper[0]^.ref^.scalefactor,1));
  5763. {$pop}
  5764. {$ifdef x86_64}
  5765. if (taicpu(hp1).oper[0]^.ref^.offset > $7FFFFFFF) or (taicpu(hp1).oper[0]^.ref^.offset < -2147483648) then
  5766. begin
  5767. { Overflow; abort }
  5768. taicpu(hp1).oper[0]^.ref^.offset := OldOffset;
  5769. end
  5770. else
  5771. {$endif x86_64}
  5772. begin
  5773. DebugMsg(SPeepholeOptimization + 'AddLea2Lea done',p);
  5774. if not (cs_opt_level3 in current_settings.optimizerswitches) then
  5775. { hp1 is the immediate next instruction for sure - good for a quick speed boost }
  5776. RemoveCurrentP(p, hp1)
  5777. else
  5778. RemoveCurrentP(p);
  5779. result:=true;
  5780. Exit;
  5781. end;
  5782. end;
  5783. if (
  5784. { Save calling GetNextInstructionUsingReg again }
  5785. Assigned(hp1) or
  5786. GetNextInstructionUsingReg(p,hp1, ActiveReg)
  5787. ) and
  5788. MatchInstruction(hp1,A_ADD,A_SUB,[taicpu(p).opsize]) and
  5789. (taicpu(hp1).oper[1]^.reg = ActiveReg) then
  5790. begin
  5791. { Make sure the flags aren't in use by the second operation }
  5792. TransferUsedRegs(TmpUsedRegs);
  5793. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.next), hp1);
  5794. if not RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) then
  5795. begin
  5796. if taicpu(hp1).oper[0]^.typ = top_const then
  5797. begin
  5798. { Merge add const1,%reg; add/sub const2,%reg to add const1+/-const2,%reg }
  5799. if taicpu(hp1).opcode = A_ADD then
  5800. ThisConst := taicpu(p).oper[0]^.val + taicpu(hp1).oper[0]^.val
  5801. else
  5802. ThisConst := taicpu(p).oper[0]^.val - taicpu(hp1).oper[0]^.val;
  5803. Result := True;
  5804. { Handle any overflows }
  5805. case taicpu(p).opsize of
  5806. S_B:
  5807. taicpu(p).oper[0]^.val := ThisConst and $FF;
  5808. S_W:
  5809. taicpu(p).oper[0]^.val := ThisConst and $FFFF;
  5810. S_L:
  5811. taicpu(p).oper[0]^.val := ThisConst and $FFFFFFFF;
  5812. {$ifdef x86_64}
  5813. S_Q:
  5814. if (ThisConst > $7FFFFFFF) or (ThisConst < -2147483648) then
  5815. { Overflow; abort }
  5816. Result := False
  5817. else
  5818. taicpu(p).oper[0]^.val := ThisConst;
  5819. {$endif x86_64}
  5820. else
  5821. InternalError(2021102610);
  5822. end;
  5823. { Result may get set to False again if the combined immediate overflows for S_Q sizes }
  5824. if Result then
  5825. begin
  5826. if (taicpu(p).oper[0]^.val < 0) and
  5827. (
  5828. ((taicpu(p).opsize = S_B) and (taicpu(p).oper[0]^.val <> -128)) or
  5829. ((taicpu(p).opsize = S_W) and (taicpu(p).oper[0]^.val <> -32768)) or
  5830. ((taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and (taicpu(p).oper[0]^.val <> -2147483648))
  5831. ) then
  5832. begin
  5833. DebugMsg(SPeepholeOptimization + 'ADD; ADD/SUB -> SUB',p);
  5834. taicpu(p).opcode := A_SUB;
  5835. taicpu(p).oper[0]^.val := -taicpu(p).oper[0]^.val;
  5836. end
  5837. else
  5838. DebugMsg(SPeepholeOptimization + 'ADD; ADD/SUB -> ADD',p);
  5839. RemoveInstruction(hp1);
  5840. end;
  5841. end
  5842. else
  5843. begin
  5844. { Move the constant addition to after the reg/ref addition to improve optimisation }
  5845. DebugMsg(SPeepholeOptimization + 'Add/sub swap 1a done',p);
  5846. Asml.Remove(p);
  5847. Asml.InsertAfter(p, hp1);
  5848. p := hp1;
  5849. Result := True;
  5850. Exit;
  5851. end;
  5852. end;
  5853. end;
  5854. if DoArithCombineOpt(p) then
  5855. Result:=true;
  5856. end;
  5857. end;
  5858. function TX86AsmOptimizer.OptPass1LEA(var p : tai) : boolean;
  5859. var
  5860. hp1, hp2: tai;
  5861. ref: Integer;
  5862. saveref: treference;
  5863. offsetcalc: Int64;
  5864. TempReg: TRegister;
  5865. Multiple: TCGInt;
  5866. Adjacent, IntermediateRegDiscarded: Boolean;
  5867. begin
  5868. Result:=false;
  5869. { play save and throw an error if LEA uses a seg register prefix,
  5870. this is most likely an error somewhere else }
  5871. if taicpu(p).oper[0]^.ref^.Segment<>NR_NO then
  5872. internalerror(2022022001);
  5873. { changes "lea (%reg1), %reg2" into "mov %reg1, %reg2" }
  5874. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  5875. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  5876. (
  5877. { do not mess with leas accessing the stack pointer
  5878. unless it's a null operation }
  5879. (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) or
  5880. (
  5881. (taicpu(p).oper[0]^.ref^.base = NR_STACK_POINTER_REG) and
  5882. (taicpu(p).oper[0]^.ref^.offset = 0)
  5883. )
  5884. ) and
  5885. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  5886. begin
  5887. if (taicpu(p).oper[0]^.ref^.offset = 0) then
  5888. begin
  5889. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) then
  5890. begin
  5891. taicpu(p).opcode := A_MOV;
  5892. taicpu(p).loadreg(0, taicpu(p).oper[0]^.ref^.base);
  5893. DebugMsg(SPeepholeOptimization + 'Lea2Mov done',p);
  5894. end
  5895. else
  5896. begin
  5897. DebugMsg(SPeepholeOptimization + 'Lea2Nop done',p);
  5898. RemoveCurrentP(p);
  5899. end;
  5900. Result:=true;
  5901. exit;
  5902. end
  5903. else if (
  5904. { continue to use lea to adjust the stack pointer,
  5905. it is the recommended way, but only if not optimizing for size }
  5906. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) or
  5907. (cs_opt_size in current_settings.optimizerswitches)
  5908. ) and
  5909. { If the flags register is in use, don't change the instruction
  5910. to an ADD otherwise this will scramble the flags. [Kit] }
  5911. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  5912. ConvertLEA(taicpu(p)) then
  5913. begin
  5914. Result:=true;
  5915. exit;
  5916. end;
  5917. end;
  5918. { Don't optimise if the stack or frame pointer is the destination register }
  5919. if (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) or (taicpu(p).oper[1]^.reg=current_procinfo.framepointer) then
  5920. Exit;
  5921. if GetNextInstruction(p,hp1) and
  5922. (hp1.typ=ait_instruction) then
  5923. begin
  5924. if MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  5925. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  5926. MatchOpType(Taicpu(hp1),top_reg,top_reg) then
  5927. begin
  5928. TransferUsedRegs(TmpUsedRegs);
  5929. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5930. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  5931. begin
  5932. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  5933. DebugMsg(SPeepholeOptimization + 'LeaMov2Lea done',p);
  5934. RemoveInstruction(hp1);
  5935. result:=true;
  5936. exit;
  5937. end;
  5938. end;
  5939. { changes
  5940. lea <ref1>, reg1
  5941. <op> ...,<ref. with reg1>,...
  5942. to
  5943. <op> ...,<ref1>,... }
  5944. { find a reference which uses reg1 }
  5945. 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
  5946. ref:=0
  5947. 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
  5948. ref:=1
  5949. else
  5950. ref:=-1;
  5951. if (ref<>-1) and
  5952. { reg1 must be either the base or the index }
  5953. ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) xor (taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg)) then
  5954. begin
  5955. { reg1 can be removed from the reference }
  5956. saveref:=taicpu(hp1).oper[ref]^.ref^;
  5957. if taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg then
  5958. taicpu(hp1).oper[ref]^.ref^.base:=NR_NO
  5959. else if taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg then
  5960. taicpu(hp1).oper[ref]^.ref^.index:=NR_NO
  5961. else
  5962. Internalerror(2019111201);
  5963. { check if the can insert all data of the lea into the second instruction }
  5964. if ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) or (taicpu(hp1).oper[ref]^.ref^.scalefactor <= 1)) and
  5965. ((taicpu(p).oper[0]^.ref^.base=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.base=NR_NO)) and
  5966. ((taicpu(p).oper[0]^.ref^.index=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.index=NR_NO)) and
  5967. ((taicpu(p).oper[0]^.ref^.symbol=nil) or (taicpu(hp1).oper[ref]^.ref^.symbol=nil)) and
  5968. ((taicpu(p).oper[0]^.ref^.relsymbol=nil) or (taicpu(hp1).oper[ref]^.ref^.relsymbol=nil)) and
  5969. ((taicpu(p).oper[0]^.ref^.scalefactor <= 1) or (taicpu(hp1).oper[ref]^.ref^.scalefactor <= 1)) and
  5970. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.segment=NR_NO)
  5971. {$ifdef x86_64}
  5972. and (abs(taicpu(hp1).oper[ref]^.ref^.offset+taicpu(p).oper[0]^.ref^.offset)<=$7fffffff)
  5973. and (((taicpu(p).oper[0]^.ref^.base<>NR_RIP) and (taicpu(p).oper[0]^.ref^.index<>NR_RIP)) or
  5974. ((taicpu(hp1).oper[ref]^.ref^.base=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.index=NR_NO))
  5975. )
  5976. {$endif x86_64}
  5977. then
  5978. begin
  5979. { reg1 might not used by the second instruction after it is remove from the reference }
  5980. if not(RegInInstruction(taicpu(p).oper[1]^.reg,taicpu(hp1))) then
  5981. begin
  5982. TransferUsedRegs(TmpUsedRegs);
  5983. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5984. { reg1 is not updated so it might not be used afterwards }
  5985. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  5986. begin
  5987. DebugMsg(SPeepholeOptimization + 'LeaOp2Op done',p);
  5988. if taicpu(p).oper[0]^.ref^.base<>NR_NO then
  5989. taicpu(hp1).oper[ref]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  5990. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  5991. taicpu(hp1).oper[ref]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  5992. if taicpu(p).oper[0]^.ref^.symbol<>nil then
  5993. taicpu(hp1).oper[ref]^.ref^.symbol:=taicpu(p).oper[0]^.ref^.symbol;
  5994. if taicpu(p).oper[0]^.ref^.relsymbol<>nil then
  5995. taicpu(hp1).oper[ref]^.ref^.relsymbol:=taicpu(p).oper[0]^.ref^.relsymbol;
  5996. if taicpu(p).oper[0]^.ref^.scalefactor > 1 then
  5997. taicpu(hp1).oper[ref]^.ref^.scalefactor:=taicpu(p).oper[0]^.ref^.scalefactor;
  5998. inc(taicpu(hp1).oper[ref]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  5999. RemoveCurrentP(p, hp1);
  6000. result:=true;
  6001. exit;
  6002. end
  6003. end;
  6004. end;
  6005. { recover }
  6006. taicpu(hp1).oper[ref]^.ref^:=saveref;
  6007. end;
  6008. Adjacent := RegInInstruction(taicpu(p).oper[1]^.reg, hp1);
  6009. if Adjacent or
  6010. { Check further ahead (up to 2 instructions ahead for -O2) }
  6011. GetNextInstructionUsingReg(hp1,hp1,taicpu(p).oper[1]^.reg) then
  6012. begin
  6013. { Check common LEA/LEA conditions }
  6014. if MatchInstruction(hp1,A_LEA,[taicpu(p).opsize]) and
  6015. (taicpu(p).oper[0]^.ref^.relsymbol = nil) and
  6016. (taicpu(p).oper[0]^.ref^.segment = NR_NO) and
  6017. (taicpu(p).oper[0]^.ref^.symbol = nil) and
  6018. (taicpu(hp1).oper[0]^.ref^.relsymbol = nil) and
  6019. (taicpu(hp1).oper[0]^.ref^.segment = NR_NO) and
  6020. (taicpu(hp1).oper[0]^.ref^.symbol = nil) and
  6021. (
  6022. { If p and hp1 are adjacent, RegModifiedBetween always returns False, so avoid
  6023. calling it (since it calls GetNextInstruction) }
  6024. Adjacent or
  6025. (
  6026. (
  6027. (taicpu(p).oper[0]^.ref^.base = NR_NO) or { Don't call RegModifiedBetween unnecessarily }
  6028. not(RegModifiedBetween(taicpu(p).oper[0]^.ref^.base,p,hp1))
  6029. ) and (
  6030. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) or { Don't call RegModifiedBetween unnecessarily }
  6031. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  6032. not(RegModifiedBetween(taicpu(p).oper[0]^.ref^.index,p,hp1))
  6033. )
  6034. )
  6035. ) then
  6036. begin
  6037. TransferUsedRegs(TmpUsedRegs);
  6038. hp2 := p;
  6039. repeat
  6040. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  6041. until not GetNextInstruction(hp2, hp2) or (hp2 = hp1);
  6042. IntermediateRegDiscarded :=
  6043. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) or
  6044. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs);
  6045. { changes
  6046. lea offset1(regX,scale), reg1
  6047. lea offset2(reg1,reg1), reg2
  6048. to
  6049. lea (offset1*scale*2)+offset2(regX,scale*2), reg2
  6050. and
  6051. lea offset1(regX,scale1), reg1
  6052. lea offset2(reg1,scale2), reg2
  6053. to
  6054. lea (offset1*scale1*2)+offset2(regX,scale1*scale2), reg2
  6055. and
  6056. lea offset1(regX,scale1), reg1
  6057. lea offset2(reg3,reg1,scale2), reg2
  6058. to
  6059. lea (offset1*scale*2)+offset2(reg3,regX,scale1*scale2), reg2
  6060. ... so long as the final scale does not exceed 8
  6061. (Similarly, allow the first instruction to be "lea (regX,regX),reg1")
  6062. }
  6063. if (taicpu(p).oper[0]^.ref^.base<>NR_STACK_POINTER_REG) and { lea (%rsp,scale),reg is not a valid encoding }
  6064. (
  6065. { Don't optimise if size is a concern and the intermediate register remains in use }
  6066. IntermediateRegDiscarded or
  6067. (
  6068. not (cs_opt_size in current_settings.optimizerswitches) and
  6069. { If the intermediate register is not discarded, it must not
  6070. appear in the first LEA's reference. (Fixes #41166) }
  6071. not RegInRef(taicpu(p).oper[1]^.reg, taicpu(p).oper[0]^.ref^)
  6072. )
  6073. ) and
  6074. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  6075. (
  6076. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[0]^.ref^.index) or
  6077. (taicpu(p).oper[0]^.ref^.scalefactor <= 1)
  6078. ) and (
  6079. (
  6080. { lea (reg1,scale2), reg2 variant }
  6081. (taicpu(hp1).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  6082. (
  6083. Adjacent or
  6084. not RegModifiedBetween(taicpu(hp1).oper[0]^.ref^.base, p, hp1)
  6085. ) and
  6086. (
  6087. (
  6088. (taicpu(p).oper[0]^.ref^.base = NR_NO) and
  6089. (taicpu(hp1).oper[0]^.ref^.scalefactor * taicpu(p).oper[0]^.ref^.scalefactor <= 8)
  6090. ) or (
  6091. { lea (regX,regX), reg1 variant }
  6092. (taicpu(p).oper[0]^.ref^.base = taicpu(p).oper[0]^.ref^.index) and
  6093. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 4)
  6094. )
  6095. )
  6096. ) or (
  6097. { lea (reg1,reg1), reg1 variant }
  6098. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  6099. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1)
  6100. )
  6101. ) then
  6102. begin
  6103. { Make everything homogeneous to make calculations easier }
  6104. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) then
  6105. begin
  6106. if taicpu(p).oper[0]^.ref^.index <> NR_NO then
  6107. { Convert lea (regX,regX),reg1 to lea (regX,2),reg1 }
  6108. taicpu(p).oper[0]^.ref^.scalefactor := 2
  6109. else
  6110. taicpu(p).oper[0]^.ref^.index := taicpu(p).oper[0]^.ref^.base;
  6111. taicpu(p).oper[0]^.ref^.base := NR_NO;
  6112. end;
  6113. { Make sure the offset doesn't go out of range (use 64-bit arithmetic)}
  6114. offsetcalc := taicpu(hp1).oper[0]^.ref^.offset;
  6115. Inc(offsetcalc, Int64(taicpu(p).oper[0]^.ref^.offset) * max(taicpu(hp1).oper[0]^.ref^.scalefactor, 1));
  6116. if (offsetcalc <= $7FFFFFFF) and (offsetcalc >= -2147483648) then
  6117. begin
  6118. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  6119. (taicpu(hp1).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) then
  6120. begin
  6121. { Put the register to change in the index register }
  6122. TempReg := taicpu(hp1).oper[0]^.ref^.index;
  6123. taicpu(hp1).oper[0]^.ref^.index := taicpu(hp1).oper[0]^.ref^.base;
  6124. taicpu(hp1).oper[0]^.ref^.base := TempReg;
  6125. end;
  6126. { Change lea (reg,reg) to lea(,reg,2) }
  6127. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) then
  6128. begin
  6129. taicpu(hp1).oper[0]^.ref^.base := NR_NO;
  6130. taicpu(hp1).oper[0]^.ref^.scalefactor := 2;
  6131. end;
  6132. if (taicpu(p).oper[0]^.ref^.offset <> 0) then
  6133. Inc(taicpu(hp1).oper[0]^.ref^.offset, taicpu(p).oper[0]^.ref^.offset * max(taicpu(hp1).oper[0]^.ref^.scalefactor, 1));
  6134. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.ref^.index;
  6135. { Just to prevent miscalculations }
  6136. if (taicpu(hp1).oper[0]^.ref^.scalefactor = 0) then
  6137. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(p).oper[0]^.ref^.scalefactor
  6138. else
  6139. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(hp1).oper[0]^.ref^.scalefactor * max(taicpu(p).oper[0]^.ref^.scalefactor, 1);
  6140. { Only remove the first LEA if we don't need the intermediate register's value as is }
  6141. if IntermediateRegDiscarded then
  6142. begin
  6143. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea 2 done',p);
  6144. RemoveCurrentP(p);
  6145. end
  6146. else
  6147. DebugMsg(SPeepholeOptimization + 'LeaLea2LeaLea 2 done (intermediate register still in use)',p);
  6148. result:=true;
  6149. exit;
  6150. end;
  6151. end;
  6152. { changes
  6153. lea offset1(regX), reg1
  6154. lea offset2(reg1), reg2
  6155. to
  6156. lea offset1+offset2(regX), reg2 }
  6157. if (
  6158. { Don't optimise if size is a concern and the intermediate register remains in use }
  6159. IntermediateRegDiscarded or
  6160. (
  6161. not (cs_opt_size in current_settings.optimizerswitches) and
  6162. { If the intermediate register is not discarded, it must not
  6163. appear in the first LEA's reference. (Fixes #41166) }
  6164. not RegInRef(taicpu(p).oper[1]^.reg, taicpu(p).oper[0]^.ref^)
  6165. )
  6166. ) and
  6167. (
  6168. (
  6169. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  6170. (getsupreg(taicpu(p).oper[0]^.ref^.base)<>RS_ESP) and
  6171. (taicpu(p).oper[0]^.ref^.index = NR_NO)
  6172. ) or (
  6173. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  6174. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1) and
  6175. (
  6176. (
  6177. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  6178. (taicpu(p).oper[0]^.ref^.base = NR_NO)
  6179. ) or (
  6180. (taicpu(p).oper[0]^.ref^.scalefactor <= 1) and
  6181. (
  6182. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  6183. (
  6184. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) and
  6185. (
  6186. (taicpu(hp1).oper[0]^.ref^.index = NR_NO) or
  6187. (taicpu(hp1).oper[0]^.ref^.base = NR_NO)
  6188. )
  6189. )
  6190. )
  6191. )
  6192. )
  6193. )
  6194. ) then
  6195. begin
  6196. { Make sure the offset doesn't go out of range (use 64-bit arithmetic)}
  6197. offsetcalc := taicpu(hp1).oper[0]^.ref^.offset;
  6198. Inc(offsetcalc, Int64(taicpu(p).oper[0]^.ref^.offset) * max(taicpu(hp1).oper[0]^.ref^.scalefactor, 1));
  6199. if (offsetcalc <= $7FFFFFFF) and (offsetcalc >= -2147483648) then
  6200. begin
  6201. if taicpu(hp1).oper[0]^.ref^.index=taicpu(p).oper[1]^.reg then
  6202. begin
  6203. taicpu(hp1).oper[0]^.ref^.index:=taicpu(p).oper[0]^.ref^.base;
  6204. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset*max(taicpu(hp1).oper[0]^.ref^.scalefactor,1));
  6205. { if the register is used as index and base, we have to increase for base as well
  6206. and adapt base }
  6207. if taicpu(hp1).oper[0]^.ref^.base=taicpu(p).oper[1]^.reg then
  6208. begin
  6209. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  6210. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  6211. end;
  6212. end
  6213. else
  6214. begin
  6215. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  6216. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  6217. end;
  6218. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  6219. begin
  6220. taicpu(hp1).oper[0]^.ref^.base:=taicpu(hp1).oper[0]^.ref^.index;
  6221. taicpu(hp1).oper[0]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  6222. if (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) then
  6223. { Catch the situation where the base = index
  6224. and treat this as *2. The scalefactor of
  6225. p will be 0 or 1 due to the conditional
  6226. checks above. Fixes i40647 }
  6227. taicpu(hp1).oper[0]^.ref^.scalefactor := 2
  6228. else
  6229. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(p).oper[0]^.ref^.scalefactor;
  6230. end;
  6231. { Only remove the first LEA if we don't need the intermediate register's value as is }
  6232. if IntermediateRegDiscarded then
  6233. begin
  6234. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea 1 done',p);
  6235. RemoveCurrentP(p);
  6236. end
  6237. else
  6238. DebugMsg(SPeepholeOptimization + 'LeaLea2LeaLea 1 done (intermediate register still in use)',p);
  6239. result:=true;
  6240. exit;
  6241. end;
  6242. end;
  6243. end;
  6244. { Change:
  6245. leal/q $x(%reg1),%reg2
  6246. ...
  6247. shll/q $y,%reg2
  6248. To:
  6249. leal/q $(x+2^y)(%reg1,2^y),%reg2 (if y <= 3)
  6250. }
  6251. if (taicpu(p).oper[0]^.ref^.base<>NR_STACK_POINTER_REG) and { lea (%rsp,scale),reg is not a valid encoding }
  6252. MatchInstruction(hp1, A_SHL, [taicpu(p).opsize]) and
  6253. MatchOpType(taicpu(hp1), top_const, top_reg) and
  6254. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) and
  6255. (taicpu(hp1).oper[0]^.val <= 3) then
  6256. begin
  6257. Multiple := 1 shl taicpu(hp1).oper[0]^.val;
  6258. TransferUsedRegs(TmpUsedRegs);
  6259. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  6260. if
  6261. { This allows the optimisation in some circumstances even if the lea instruction already has a scale factor
  6262. (this works even if scalefactor is zero) }
  6263. ((Multiple * taicpu(p).oper[0]^.ref^.scalefactor) <= 8) and
  6264. { Ensure offset doesn't go out of bounds }
  6265. (abs(taicpu(p).oper[0]^.ref^.offset * Multiple) <= $7FFFFFFF) and
  6266. not (RegInUsedRegs(NR_DEFAULTFLAGS,TmpUsedRegs)) and
  6267. (
  6268. (
  6269. not SuperRegistersEqual(taicpu(p).oper[0]^.ref^.base, taicpu(p).oper[1]^.reg) and
  6270. (
  6271. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  6272. (taicpu(p).oper[0]^.ref^.index = NR_INVALID) or
  6273. (
  6274. { Check for lea $x(%reg1,%reg1),%reg2 and treat as it it were lea $x(%reg1,2),%reg2 }
  6275. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) and
  6276. (taicpu(p).oper[0]^.ref^.scalefactor <= 1)
  6277. )
  6278. )
  6279. ) or (
  6280. (
  6281. (taicpu(p).oper[0]^.ref^.base = NR_NO) or
  6282. (taicpu(p).oper[0]^.ref^.base = NR_INVALID)
  6283. ) and
  6284. not SuperRegistersEqual(taicpu(p).oper[0]^.ref^.index, taicpu(p).oper[1]^.reg)
  6285. )
  6286. ) then
  6287. begin
  6288. repeat
  6289. with taicpu(p).oper[0]^.ref^ do
  6290. begin
  6291. { Convert lea $x(%reg1,%reg1),%reg2 to lea $x(%reg1,2),%reg2 }
  6292. if index = base then
  6293. begin
  6294. if Multiple > 4 then
  6295. { Optimisation will no longer work because resultant
  6296. scale factor will exceed 8 }
  6297. Break;
  6298. base := NR_NO;
  6299. scalefactor := 2;
  6300. DebugMsg(SPeepholeOptimization + 'lea $x(%reg1,%reg1),%reg2 -> lea $x(%reg1,2),%reg2 for following optimisation', p);
  6301. end
  6302. else if (base <> NR_NO) and (base <> NR_INVALID) then
  6303. begin
  6304. { Scale factor only works on the index register }
  6305. index := base;
  6306. base := NR_NO;
  6307. end;
  6308. { For safety }
  6309. if scalefactor <= 1 then
  6310. begin
  6311. DebugMsg(SPeepholeOptimization + 'LeaShl2Lea 1', p);
  6312. scalefactor := Multiple;
  6313. end
  6314. else
  6315. begin
  6316. DebugMsg(SPeepholeOptimization + 'LeaShl2Lea 2', p);
  6317. scalefactor := scalefactor * Multiple;
  6318. end;
  6319. offset := offset * Multiple;
  6320. end;
  6321. RemoveInstruction(hp1);
  6322. Result := True;
  6323. Exit;
  6324. { This repeat..until loop exists for the benefit of Break }
  6325. until True;
  6326. end;
  6327. end;
  6328. end;
  6329. end;
  6330. end;
  6331. function TX86AsmOptimizer.DoArithCombineOpt(var p: tai): Boolean;
  6332. var
  6333. hp1 : tai;
  6334. SubInstr: Boolean;
  6335. ThisConst: TCGInt;
  6336. const
  6337. OverflowMin: array[S_B..S_Q] of TCGInt = (-128, -32768, -2147483648, -2147483648);
  6338. { Note: 64-bit-sized arithmetic instructions can only take signed 32-bit immediates }
  6339. OverflowMax: array[S_B..S_Q] of TCGInt = ( 255, 65535, $FFFFFFFF, 2147483647);
  6340. begin
  6341. Result := False;
  6342. if taicpu(p).oper[0]^.typ <> top_const then
  6343. { Should have been confirmed before calling }
  6344. InternalError(2021102601);
  6345. SubInstr := (taicpu(p).opcode = A_SUB);
  6346. if not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  6347. GetLastInstruction(p, hp1) and
  6348. (hp1.typ = ait_instruction) and
  6349. (taicpu(hp1).opsize = taicpu(p).opsize) then
  6350. begin
  6351. if not (taicpu(p).opsize in [S_B, S_W, S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) then
  6352. { Bad size }
  6353. InternalError(2022042001);
  6354. case taicpu(hp1).opcode Of
  6355. A_INC:
  6356. if MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  6357. begin
  6358. if SubInstr then
  6359. ThisConst := taicpu(p).oper[0]^.val - 1
  6360. else
  6361. ThisConst := taicpu(p).oper[0]^.val + 1;
  6362. end
  6363. else
  6364. Exit;
  6365. A_DEC:
  6366. if MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  6367. begin
  6368. if SubInstr then
  6369. ThisConst := taicpu(p).oper[0]^.val + 1
  6370. else
  6371. ThisConst := taicpu(p).oper[0]^.val - 1;
  6372. end
  6373. else
  6374. Exit;
  6375. A_SUB:
  6376. if (taicpu(hp1).oper[0]^.typ = top_const) and
  6377. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  6378. begin
  6379. if SubInstr then
  6380. ThisConst := taicpu(p).oper[0]^.val + taicpu(hp1).oper[0]^.val
  6381. else
  6382. ThisConst := taicpu(p).oper[0]^.val - taicpu(hp1).oper[0]^.val;
  6383. end
  6384. else
  6385. Exit;
  6386. A_ADD:
  6387. if (taicpu(hp1).oper[0]^.typ = top_const) and
  6388. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  6389. begin
  6390. if SubInstr then
  6391. ThisConst := taicpu(p).oper[0]^.val - taicpu(hp1).oper[0]^.val
  6392. else
  6393. ThisConst := taicpu(p).oper[0]^.val + taicpu(hp1).oper[0]^.val;
  6394. end
  6395. else
  6396. Exit;
  6397. else
  6398. Exit;
  6399. end;
  6400. { Check that the values are in range }
  6401. if (ThisConst < OverflowMin[taicpu(p).opsize]) or (ThisConst > OverflowMax[taicpu(p).opsize]) then
  6402. { Overflow; abort }
  6403. Exit;
  6404. if (ThisConst = 0) then
  6405. begin
  6406. DebugMsg(SPeepholeOptimization + 'Arithmetic combine: ' +
  6407. debug_op2str(taicpu(hp1).opcode) + ' $' + debug_tostr(taicpu(hp1).oper[0]^.val) + ',' + debug_operstr(taicpu(hp1).oper[1]^) + '; ' +
  6408. debug_op2str(taicpu(p).opcode) + ' $' + debug_tostr(taicpu(p).oper[0]^.val) + ',' + debug_operstr(taicpu(p).oper[1]^) + ' cancel out (NOP)', p);
  6409. RemoveInstruction(hp1);
  6410. hp1 := tai(p.next);
  6411. RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
  6412. if not GetLastInstruction(hp1, p) then
  6413. p := hp1;
  6414. end
  6415. else
  6416. begin
  6417. if taicpu(hp1).opercnt=1 then
  6418. DebugMsg(SPeepholeOptimization + 'Arithmetic combine: ' +
  6419. debug_op2str(taicpu(hp1).opcode) + ' $' + debug_tostr(taicpu(hp1).oper[0]^.val) + '; ' +
  6420. debug_op2str(taicpu(p).opcode) + ' $' + debug_tostr(taicpu(p).oper[0]^.val) + ',' + debug_operstr(taicpu(p).oper[1]^) + ' -> ' +
  6421. debug_op2str(taicpu(p).opcode) + ' $' + debug_tostr(ThisConst) + ' ' + debug_operstr(taicpu(p).oper[1]^), p)
  6422. else
  6423. DebugMsg(SPeepholeOptimization + 'Arithmetic combine: ' +
  6424. debug_op2str(taicpu(hp1).opcode) + ' $' + debug_tostr(taicpu(hp1).oper[0]^.val) + ',' + debug_operstr(taicpu(hp1).oper[1]^) + '; ' +
  6425. debug_op2str(taicpu(p).opcode) + ' $' + debug_tostr(taicpu(p).oper[0]^.val) + ',' + debug_operstr(taicpu(p).oper[1]^) + ' -> ' +
  6426. debug_op2str(taicpu(p).opcode) + ' $' + debug_tostr(ThisConst) + ' ' + debug_operstr(taicpu(p).oper[1]^), p);
  6427. RemoveInstruction(hp1);
  6428. taicpu(p).loadconst(0, ThisConst);
  6429. end;
  6430. Result := True;
  6431. end;
  6432. end;
  6433. function TX86AsmOptimizer.DoMovCmpMemOpt(var p : tai; const hp1: tai) : Boolean;
  6434. begin
  6435. Result := False;
  6436. if MatchOpType(taicpu(p),top_ref,top_reg) and
  6437. { The x86 assemblers have difficulty comparing values against absolute addresses }
  6438. (taicpu(p).oper[0]^.ref^.refaddr <> addr_full) and
  6439. (taicpu(hp1).oper[0]^.typ <> top_ref) and
  6440. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  6441. (
  6442. (
  6443. (taicpu(hp1).opcode = A_TEST)
  6444. ) or (
  6445. (taicpu(hp1).opcode = A_CMP) and
  6446. { A sanity check more than anything }
  6447. not MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg)
  6448. )
  6449. ) then
  6450. begin
  6451. { change
  6452. mov mem, %reg
  6453. ...
  6454. cmp/test x, %reg / test %reg,%reg
  6455. (reg deallocated)
  6456. to
  6457. cmp/test x, mem / cmp 0, mem
  6458. }
  6459. TransferUsedRegs(TmpUsedRegs);
  6460. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  6461. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) then
  6462. begin
  6463. { Convert test %reg,%reg or test $-1,%reg to cmp $0,mem }
  6464. if (taicpu(hp1).opcode = A_TEST) and
  6465. (
  6466. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) or
  6467. MatchOperand(taicpu(hp1).oper[0]^, -1)
  6468. ) then
  6469. begin
  6470. taicpu(hp1).opcode := A_CMP;
  6471. taicpu(hp1).loadconst(0, 0);
  6472. end;
  6473. taicpu(hp1).loadref(1, taicpu(p).oper[0]^.ref^);
  6474. DebugMsg(SPeepholeOptimization + 'MOV/CMP -> CMP (memory check)', p);
  6475. RemoveCurrentP(p);
  6476. if (p <> hp1) then
  6477. { Correctly update TmpUsedRegs if p and hp1 aren't adjacent }
  6478. UpdateUsedRegsBetween(TmpUsedRegs, p, hp1);
  6479. { Make sure the flags are allocated across the CMP instruction }
  6480. if not RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) then
  6481. AllocRegBetween(NR_DEFAULTFLAGS, hp1, hp1, TmpUsedRegs);
  6482. Result := True;
  6483. Exit;
  6484. end;
  6485. end;
  6486. end;
  6487. function TX86AsmOptimizer.DoSETccLblRETOpt(var p: tai; const hp_label: tai_label) : Boolean;
  6488. var
  6489. hp_allocstart, hp_pos, hp2, hp3, hp4, hp5, hp6: tai;
  6490. ThisReg, SecondReg: TRegister;
  6491. JumpLoc: TAsmLabel;
  6492. NewSize: TOpSize;
  6493. begin
  6494. Result := False;
  6495. {
  6496. Convert:
  6497. j<c> .L1
  6498. .L2:
  6499. mov 1,reg
  6500. jmp .L3 (or ret, although it might not be a RET yet)
  6501. .L1:
  6502. mov 0,reg
  6503. jmp .L3 (or ret)
  6504. ( As long as .L3 <> .L1 or .L2)
  6505. To:
  6506. mov 0,reg
  6507. set<not(c)> reg
  6508. jmp .L3 (or ret)
  6509. .L2:
  6510. mov 1,reg
  6511. jmp .L3 (or ret)
  6512. .L1:
  6513. mov 0,reg
  6514. jmp .L3 (or ret)
  6515. }
  6516. if JumpTargetOp(taicpu(p))^.ref^.refaddr<>addr_full then
  6517. Exit;
  6518. JumpLoc := TAsmLabel(JumpTargetOp(taicpu(p))^.ref^.symbol);
  6519. if GetNextInstruction(hp_label, hp2) and
  6520. MatchInstruction(hp2,A_MOV,[]) and
  6521. (taicpu(hp2).oper[0]^.typ = top_const) and
  6522. (
  6523. (
  6524. (taicpu(hp2).oper[1]^.typ = top_reg)
  6525. {$ifdef i386}
  6526. { Under i386, ESI, EDI, EBP and ESP
  6527. don't have an 8-bit representation }
  6528. and not (getsupreg(taicpu(hp2).oper[1]^.reg) in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  6529. {$endif i386}
  6530. ) or (
  6531. {$ifdef i386}
  6532. (taicpu(hp2).oper[1]^.typ <> top_reg) and
  6533. {$endif i386}
  6534. (taicpu(hp2).opsize = S_B)
  6535. )
  6536. ) and
  6537. GetNextInstruction(hp2, hp3) and
  6538. MatchInstruction(hp3, A_JMP, A_RET, []) and
  6539. (
  6540. (taicpu(hp3).opcode=A_RET) or
  6541. (
  6542. (taicpu(hp3).oper[0]^.ref^.refaddr=addr_full) and
  6543. (tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol)<>tai_label(hp_label).labsym)
  6544. )
  6545. ) and
  6546. GetNextInstruction(hp3, hp4) and
  6547. FindLabel(JumpLoc, hp4) and
  6548. (
  6549. not (cs_opt_size in current_settings.optimizerswitches) or
  6550. { If the initial jump is the label's only reference, then it will
  6551. become a dead label if the other conditions are met and hence
  6552. remove at least 2 instructions, including a jump }
  6553. (JumpLoc.getrefs = 1)
  6554. ) and
  6555. { Don't check if hp3 jumps to hp4 because this is a zero-distance jump
  6556. that will be optimised out }
  6557. GetNextInstruction(hp4, hp5) and
  6558. MatchInstruction(hp5,A_MOV,[taicpu(hp2).opsize]) and
  6559. (taicpu(hp5).oper[0]^.typ = top_const) and
  6560. (
  6561. ((taicpu(hp2).oper[0]^.val = 0) and (taicpu(hp5).oper[0]^.val = 1)) or
  6562. ((taicpu(hp2).oper[0]^.val = 1) and (taicpu(hp5).oper[0]^.val = 0))
  6563. ) and
  6564. MatchOperand(taicpu(hp2).oper[1]^,taicpu(hp5).oper[1]^) and
  6565. GetNextInstruction(hp5,hp6) and
  6566. (
  6567. not (hp6.typ in [ait_align, ait_label]) or
  6568. SkipLabels(hp6, hp6)
  6569. ) and
  6570. (hp6.typ=ait_instruction) then
  6571. begin
  6572. { First, let's look at the two jumps that are hp3 and hp6 }
  6573. if not
  6574. (
  6575. (taicpu(hp6).opcode=taicpu(hp3).opcode) and { Both RET or both JMP to the same label }
  6576. (
  6577. (taicpu(hp6).opcode=A_RET) or
  6578. MatchOperand(taicpu(hp6).oper[0]^, taicpu(hp3).oper[0]^)
  6579. )
  6580. ) then
  6581. { If condition is False, then the JMP/RET instructions matched conventionally }
  6582. begin
  6583. { See if one of the jumps can be instantly converted into a RET }
  6584. if (taicpu(hp3).opcode=A_JMP) then
  6585. begin
  6586. { Reuse hp5 }
  6587. hp5 := getlabelwithsym(TAsmLabel(JumpTargetOp(taicpu(hp3))^.ref^.symbol));
  6588. { Make sure hp5 doesn't jump back to .L1 (zero distance jump) or .L2 (infinite loop) }
  6589. if not Assigned(hp5) or (hp5 = hp_label) or (hp5 = hp4) or not GetNextInstruction(hp5, hp5) then
  6590. Exit;
  6591. if MatchInstruction(hp5, A_RET, []) then
  6592. begin
  6593. DebugMsg(SPeepholeOptimization + 'Converted JMP to RET as part of SETcc optimisation (1st jump)', hp3);
  6594. ConvertJumpToRET(hp3, hp5);
  6595. Result := True;
  6596. end
  6597. else
  6598. Exit;
  6599. end;
  6600. if (taicpu(hp6).opcode=A_JMP) then
  6601. begin
  6602. { Reuse hp5 }
  6603. hp5 := getlabelwithsym(TAsmLabel(JumpTargetOp(taicpu(hp6))^.ref^.symbol));
  6604. if not Assigned(hp5) or not GetNextInstruction(hp5, hp5) then
  6605. Exit;
  6606. if MatchInstruction(hp5, A_RET, []) then
  6607. begin
  6608. DebugMsg(SPeepholeOptimization + 'Converted JMP to RET as part of SETcc optimisation (2nd jump)', hp6);
  6609. ConvertJumpToRET(hp6, hp5);
  6610. Result := True;
  6611. end
  6612. else
  6613. Exit;
  6614. end;
  6615. if not
  6616. (
  6617. (taicpu(hp6).opcode=taicpu(hp3).opcode) and { Both RET or both JMP to the same label }
  6618. (
  6619. (taicpu(hp6).opcode=A_RET) or
  6620. MatchOperand(taicpu(hp6).oper[0]^, taicpu(hp3).oper[0]^)
  6621. )
  6622. ) then
  6623. { Still doesn't match }
  6624. Exit;
  6625. end;
  6626. if (taicpu(hp2).oper[0]^.val = 1) then
  6627. begin
  6628. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  6629. DebugMsg(SPeepholeOptimization + 'J(c)Mov1Jmp/RetMov0Jmp/Ret -> Set(~c)Jmp/Ret',p)
  6630. end
  6631. else
  6632. DebugMsg(SPeepholeOptimization + 'J(c)Mov0Jmp/RetMov1Jmp/Ret -> Set(c)Jmp/Ret',p);
  6633. if taicpu(hp2).opsize=S_B then
  6634. begin
  6635. if taicpu(hp2).oper[1]^.typ = top_reg then
  6636. begin
  6637. SecondReg := taicpu(hp2).oper[1]^.reg;
  6638. hp4:=taicpu.op_reg(A_SETcc, S_B, SecondReg);
  6639. end
  6640. else
  6641. begin
  6642. hp4:=taicpu.op_ref(A_SETcc, S_B, taicpu(hp2).oper[1]^.ref^);
  6643. SecondReg := NR_NO;
  6644. end;
  6645. hp_pos := p;
  6646. hp_allocstart := hp4;
  6647. end
  6648. else
  6649. begin
  6650. { Will be a register because the size can't be S_B otherwise }
  6651. SecondReg:=taicpu(hp2).oper[1]^.reg;
  6652. ThisReg:=newreg(R_INTREGISTER,getsupreg(SecondReg), R_SUBL);
  6653. hp4:=taicpu.op_reg(A_SETcc, S_B, ThisReg);
  6654. if (cs_opt_size in current_settings.optimizerswitches) then
  6655. begin
  6656. { Favour using MOVZX when optimising for size }
  6657. case taicpu(hp2).opsize of
  6658. S_W:
  6659. NewSize := S_BW;
  6660. S_L:
  6661. NewSize := S_BL;
  6662. {$ifdef x86_64}
  6663. S_Q:
  6664. begin
  6665. NewSize := S_BL;
  6666. { Will implicitly zero-extend to 64-bit }
  6667. setsubreg(SecondReg, R_SUBD);
  6668. end;
  6669. {$endif x86_64}
  6670. else
  6671. InternalError(2022101301);
  6672. end;
  6673. hp5:=taicpu.op_reg_reg(A_MOVZX, NewSize, ThisReg, SecondReg);
  6674. { Inserting it right before p will guarantee that the flags are also tracked }
  6675. Asml.InsertBefore(hp5, p);
  6676. { Make sure the SET instruction gets inserted before the MOVZX instruction }
  6677. hp_pos := hp5;
  6678. hp_allocstart := hp4;
  6679. end
  6680. else
  6681. begin
  6682. hp5:=taicpu.op_const_reg(A_MOV, taicpu(hp2).opsize, 0, SecondReg);
  6683. { Inserting it right before p will guarantee that the flags are also tracked }
  6684. Asml.InsertBefore(hp5, p);
  6685. hp_pos := p;
  6686. hp_allocstart := hp5;
  6687. end;
  6688. taicpu(hp5).fileinfo:=taicpu(p).fileinfo;
  6689. end;
  6690. taicpu(hp4).fileinfo := taicpu(p).fileinfo;
  6691. taicpu(hp4).condition := taicpu(p).condition;
  6692. asml.InsertBefore(hp4, hp_pos);
  6693. if taicpu(hp3).is_jmp then
  6694. begin
  6695. JumpLoc.decrefs;
  6696. MakeUnconditional(taicpu(p));
  6697. { This also increases the reference count }
  6698. taicpu(p).loadref(0, JumpTargetOp(taicpu(hp3))^.ref^);
  6699. end
  6700. else
  6701. ConvertJumpToRET(p, hp3);
  6702. if SecondReg <> NR_NO then
  6703. { Ensure the destination register is allocated over this region }
  6704. AllocRegBetween(SecondReg, hp_allocstart, p, UsedRegs);
  6705. if (JumpLoc.getrefs = 0) then
  6706. RemoveDeadCodeAfterJump(hp3);
  6707. Result:=true;
  6708. exit;
  6709. end;
  6710. end;
  6711. function TX86AsmOptimizer.OptPass1Sub(var p : tai) : boolean;
  6712. var
  6713. hp1, hp2: tai;
  6714. ActiveReg: TRegister;
  6715. OldOffset: asizeint;
  6716. ThisConst: TCGInt;
  6717. function RegDeallocated: Boolean;
  6718. begin
  6719. TransferUsedRegs(TmpUsedRegs);
  6720. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  6721. Result := not(RegUsedAfterInstruction(ActiveReg,hp1,TmpUsedRegs))
  6722. end;
  6723. begin
  6724. Result:=false;
  6725. hp1 := nil;
  6726. { replace
  6727. subX const,%reg1
  6728. leaX (%reg1,%reg1,Y),%reg2 // Base or index might not be equal to reg1
  6729. dealloc %reg1
  6730. by
  6731. leaX -const-const*Y(%reg1,%reg1,Y),%reg2
  6732. }
  6733. if MatchOpType(taicpu(p),top_const,top_reg) then
  6734. begin
  6735. ActiveReg := taicpu(p).oper[1]^.reg;
  6736. { Ensures the entire register was updated }
  6737. if (taicpu(p).opsize >= S_L) and
  6738. GetNextInstructionUsingReg(p,hp1, ActiveReg) and
  6739. MatchInstruction(hp1,A_LEA,[]) and
  6740. (SuperRegistersEqual(ActiveReg, taicpu(hp1).oper[0]^.ref^.base) or
  6741. SuperRegistersEqual(ActiveReg, taicpu(hp1).oper[0]^.ref^.index)) and
  6742. (
  6743. { Cover the case where the register in the reference is also the destination register }
  6744. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, ActiveReg) or
  6745. (
  6746. { Try to avoid the expensive check of RegUsedAfterInstruction if we know it will return False }
  6747. not SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ActiveReg) and
  6748. RegDeallocated
  6749. )
  6750. ) then
  6751. begin
  6752. OldOffset := taicpu(hp1).oper[0]^.ref^.offset;
  6753. if SuperRegistersEqual(ActiveReg,taicpu(hp1).oper[0]^.ref^.base) then
  6754. Dec(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.val);
  6755. if SuperRegistersEqual(ActiveReg,taicpu(hp1).oper[0]^.ref^.index) then
  6756. Dec(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.val*max(taicpu(hp1).oper[0]^.ref^.scalefactor,1));
  6757. {$ifdef x86_64}
  6758. if (taicpu(hp1).oper[0]^.ref^.offset > $7FFFFFFF) or (taicpu(hp1).oper[0]^.ref^.offset < -2147483648) then
  6759. begin
  6760. { Overflow; abort }
  6761. taicpu(hp1).oper[0]^.ref^.offset := OldOffset;
  6762. end
  6763. else
  6764. {$endif x86_64}
  6765. begin
  6766. DebugMsg(SPeepholeOptimization + 'SubLea2Lea done',p);
  6767. if not (cs_opt_level3 in current_settings.optimizerswitches) then
  6768. { hp1 is the immediate next instruction for sure - good for a quick speed boost }
  6769. RemoveCurrentP(p, hp1)
  6770. else
  6771. RemoveCurrentP(p);
  6772. result:=true;
  6773. Exit;
  6774. end;
  6775. end;
  6776. if (
  6777. { Save calling GetNextInstructionUsingReg again }
  6778. Assigned(hp1) or
  6779. GetNextInstructionUsingReg(p,hp1, ActiveReg)
  6780. ) and
  6781. MatchInstruction(hp1,A_SUB,[taicpu(p).opsize]) and
  6782. (taicpu(hp1).oper[1]^.reg = ActiveReg) then
  6783. begin
  6784. { Make sure the flags aren't in use by the second operation }
  6785. TransferUsedRegs(TmpUsedRegs);
  6786. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.next), hp1);
  6787. if not RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) then
  6788. begin
  6789. if (taicpu(hp1).oper[0]^.typ = top_const) then
  6790. begin
  6791. { Merge add const1,%reg; add const2,%reg to add const1+const2,%reg }
  6792. ThisConst := taicpu(p).oper[0]^.val + taicpu(hp1).oper[0]^.val;
  6793. Result := True;
  6794. { Handle any overflows }
  6795. case taicpu(p).opsize of
  6796. S_B:
  6797. taicpu(p).oper[0]^.val := ThisConst and $FF;
  6798. S_W:
  6799. taicpu(p).oper[0]^.val := ThisConst and $FFFF;
  6800. S_L:
  6801. taicpu(p).oper[0]^.val := ThisConst and $FFFFFFFF;
  6802. {$ifdef x86_64}
  6803. S_Q:
  6804. if (ThisConst > $7FFFFFFF) or (ThisConst < -2147483648) then
  6805. { Overflow; abort }
  6806. Result := False
  6807. else
  6808. taicpu(p).oper[0]^.val := ThisConst;
  6809. {$endif x86_64}
  6810. else
  6811. InternalError(2021102611);
  6812. end;
  6813. { Result may get set to False again if the combined immediate overflows for S_Q sizes }
  6814. if Result then
  6815. begin
  6816. if (taicpu(p).oper[0]^.val < 0) and
  6817. (
  6818. ((taicpu(p).opsize = S_B) and (taicpu(p).oper[0]^.val <> -128)) or
  6819. ((taicpu(p).opsize = S_W) and (taicpu(p).oper[0]^.val <> -32768)) or
  6820. ((taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and (taicpu(p).oper[0]^.val <> -2147483648))
  6821. ) then
  6822. begin
  6823. DebugMsg(SPeepholeOptimization + 'SUB; ADD/SUB -> ADD',p);
  6824. taicpu(p).opcode := A_SUB;
  6825. taicpu(p).oper[0]^.val := -taicpu(p).oper[0]^.val;
  6826. end
  6827. else
  6828. DebugMsg(SPeepholeOptimization + 'SUB; ADD/SUB -> SUB',p);
  6829. RemoveInstruction(hp1);
  6830. end;
  6831. end
  6832. else
  6833. begin
  6834. { Move the constant subtraction to after the reg/ref addition to improve optimisation }
  6835. DebugMsg(SPeepholeOptimization + 'Add/sub swap 1b done',p);
  6836. Asml.Remove(p);
  6837. Asml.InsertAfter(p, hp1);
  6838. p := hp1;
  6839. Result := True;
  6840. Exit;
  6841. end;
  6842. end;
  6843. end;
  6844. { * change "subl $2, %esp; pushw x" to "pushl x"}
  6845. { * change "sub/add const1, reg" or "dec reg" followed by
  6846. "sub const2, reg" to one "sub ..., reg" }
  6847. {$ifdef i386}
  6848. if (taicpu(p).oper[0]^.val = 2) and
  6849. (ActiveReg = NR_ESP) and
  6850. { Don't do the sub/push optimization if the sub }
  6851. { comes from setting up the stack frame (JM) }
  6852. (not(GetLastInstruction(p,hp1)) or
  6853. not(MatchInstruction(hp1,A_MOV,[S_L]) and
  6854. MatchOperand(taicpu(hp1).oper[0]^,NR_ESP) and
  6855. MatchOperand(taicpu(hp1).oper[0]^,NR_EBP))) then
  6856. begin
  6857. hp1 := tai(p.next);
  6858. while Assigned(hp1) and
  6859. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  6860. not RegReadByInstruction(NR_ESP,hp1) and
  6861. not RegModifiedByInstruction(NR_ESP,hp1) do
  6862. hp1 := tai(hp1.next);
  6863. if Assigned(hp1) and
  6864. MatchInstruction(hp1,A_PUSH,[S_W]) then
  6865. begin
  6866. taicpu(hp1).changeopsize(S_L);
  6867. if taicpu(hp1).oper[0]^.typ=top_reg then
  6868. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  6869. hp1 := tai(p.next);
  6870. RemoveCurrentp(p, hp1);
  6871. Result:=true;
  6872. exit;
  6873. end;
  6874. end;
  6875. {$endif i386}
  6876. if DoArithCombineOpt(p) then
  6877. Result:=true;
  6878. end;
  6879. end;
  6880. function TX86AsmOptimizer.OptPass1SHLSAL(var p : tai) : boolean;
  6881. var
  6882. TmpBool1,TmpBool2 : Boolean;
  6883. tmpref : treference;
  6884. hp1,hp2: tai;
  6885. mask, shiftval: tcgint;
  6886. begin
  6887. Result:=false;
  6888. { All these optimisations work on "shl/sal const,%reg" }
  6889. if not MatchOpType(taicpu(p),top_const,top_reg) then
  6890. Exit;
  6891. if (taicpu(p).opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  6892. (taicpu(p).oper[0]^.val <= 3) then
  6893. { Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement }
  6894. begin
  6895. { should we check the next instruction? }
  6896. TmpBool1 := True;
  6897. { have we found an add/sub which could be
  6898. integrated in the lea? }
  6899. TmpBool2 := False;
  6900. reference_reset(tmpref,2,[]);
  6901. TmpRef.index := taicpu(p).oper[1]^.reg;
  6902. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  6903. while TmpBool1 and
  6904. GetNextInstruction(p, hp1) and
  6905. (tai(hp1).typ = ait_instruction) and
  6906. ((((taicpu(hp1).opcode = A_ADD) or
  6907. (taicpu(hp1).opcode = A_SUB)) and
  6908. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  6909. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  6910. (((taicpu(hp1).opcode = A_INC) or
  6911. (taicpu(hp1).opcode = A_DEC)) and
  6912. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  6913. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg)) or
  6914. ((taicpu(hp1).opcode = A_LEA) and
  6915. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  6916. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg))) and
  6917. (not GetNextInstruction(hp1,hp2) or
  6918. not instrReadsFlags(hp2)) Do
  6919. begin
  6920. TmpBool1 := False;
  6921. if taicpu(hp1).opcode=A_LEA then
  6922. begin
  6923. if (TmpRef.base = NR_NO) and
  6924. (taicpu(hp1).oper[0]^.ref^.symbol=nil) and
  6925. (taicpu(hp1).oper[0]^.ref^.relsymbol=nil) and
  6926. { Segment register isn't a concern here }
  6927. ((taicpu(hp1).oper[0]^.ref^.scalefactor=0) or
  6928. (taicpu(hp1).oper[0]^.ref^.scalefactor*tmpref.scalefactor<=8)) then
  6929. begin
  6930. TmpBool1 := True;
  6931. TmpBool2 := True;
  6932. inc(TmpRef.offset, taicpu(hp1).oper[0]^.ref^.offset);
  6933. if taicpu(hp1).oper[0]^.ref^.scalefactor<>0 then
  6934. tmpref.scalefactor:=tmpref.scalefactor*taicpu(hp1).oper[0]^.ref^.scalefactor;
  6935. TmpRef.base := taicpu(hp1).oper[0]^.ref^.base;
  6936. RemoveInstruction(hp1);
  6937. end
  6938. end
  6939. else if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  6940. begin
  6941. TmpBool1 := True;
  6942. TmpBool2 := True;
  6943. case taicpu(hp1).opcode of
  6944. A_ADD:
  6945. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  6946. A_SUB:
  6947. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  6948. else
  6949. internalerror(2019050536);
  6950. end;
  6951. RemoveInstruction(hp1);
  6952. end
  6953. else
  6954. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  6955. (((taicpu(hp1).opcode = A_ADD) and
  6956. (TmpRef.base = NR_NO)) or
  6957. (taicpu(hp1).opcode = A_INC) or
  6958. (taicpu(hp1).opcode = A_DEC)) then
  6959. begin
  6960. TmpBool1 := True;
  6961. TmpBool2 := True;
  6962. case taicpu(hp1).opcode of
  6963. A_ADD:
  6964. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  6965. A_INC:
  6966. inc(TmpRef.offset);
  6967. A_DEC:
  6968. dec(TmpRef.offset);
  6969. else
  6970. internalerror(2019050535);
  6971. end;
  6972. RemoveInstruction(hp1);
  6973. end;
  6974. end;
  6975. if TmpBool2
  6976. {$ifndef x86_64}
  6977. or
  6978. ((current_settings.optimizecputype < cpu_Pentium2) and
  6979. (taicpu(p).oper[0]^.val <= 3) and
  6980. not(cs_opt_size in current_settings.optimizerswitches))
  6981. {$endif x86_64}
  6982. then
  6983. begin
  6984. if not(TmpBool2) and
  6985. (taicpu(p).oper[0]^.val=1) then
  6986. begin
  6987. taicpu(p).opcode := A_ADD;
  6988. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg);
  6989. end
  6990. else
  6991. begin
  6992. taicpu(p).opcode := A_LEA;
  6993. taicpu(p).loadref(0, TmpRef);
  6994. end;
  6995. DebugMsg(SPeepholeOptimization + 'ShlAddLeaSubIncDec2Lea',p);
  6996. Result := True;
  6997. end;
  6998. end
  6999. {$ifndef x86_64}
  7000. else if (current_settings.optimizecputype < cpu_Pentium2) then
  7001. begin
  7002. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  7003. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  7004. (unlike shl, which is only Tairable in the U pipe) }
  7005. if taicpu(p).oper[0]^.val=1 then
  7006. begin
  7007. taicpu(p).opcode := A_ADD;
  7008. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg);
  7009. Result := True;
  7010. end
  7011. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  7012. "shl $3, %reg" to "lea (,%reg,8), %reg }
  7013. else if (taicpu(p).opsize = S_L) and
  7014. (taicpu(p).oper[0]^.val<= 3) then
  7015. begin
  7016. reference_reset(tmpref,2,[]);
  7017. TmpRef.index := taicpu(p).oper[1]^.reg;
  7018. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  7019. taicpu(p).opcode := A_LEA;
  7020. taicpu(p).loadref(0, TmpRef);
  7021. Result := True;
  7022. end;
  7023. end
  7024. {$endif x86_64}
  7025. else if
  7026. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  7027. (
  7028. (
  7029. MatchInstruction(hp1, A_AND, [taicpu(p).opsize]) and
  7030. SetAndTest(hp1, hp2)
  7031. {$ifdef x86_64}
  7032. ) or
  7033. (
  7034. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  7035. GetNextInstruction(hp1, hp2) and
  7036. MatchInstruction(hp2, A_AND, [taicpu(p).opsize]) and
  7037. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  7038. (taicpu(hp1).oper[1]^.reg = taicpu(hp2).oper[0]^.reg)
  7039. {$endif x86_64}
  7040. )
  7041. ) and
  7042. (taicpu(p).oper[1]^.reg = taicpu(hp2).oper[1]^.reg) then
  7043. begin
  7044. { Change:
  7045. shl x, %reg1
  7046. mov -(1<<x), %reg2
  7047. and %reg2, %reg1
  7048. Or:
  7049. shl x, %reg1
  7050. and -(1<<x), %reg1
  7051. To just:
  7052. shl x, %reg1
  7053. Since the and operation only zeroes bits that are already zero from the shl operation
  7054. }
  7055. case taicpu(p).oper[0]^.val of
  7056. 8:
  7057. mask:=$FFFFFFFFFFFFFF00;
  7058. 16:
  7059. mask:=$FFFFFFFFFFFF0000;
  7060. 32:
  7061. mask:=$FFFFFFFF00000000;
  7062. 63:
  7063. { Constant pre-calculated to prevent overflow errors with Int64 }
  7064. mask:=$8000000000000000;
  7065. else
  7066. begin
  7067. if taicpu(p).oper[0]^.val >= 64 then
  7068. { Shouldn't happen realistically, since the register
  7069. is guaranteed to be set to zero at this point }
  7070. mask := 0
  7071. else
  7072. mask := -(Int64(1 shl taicpu(p).oper[0]^.val));
  7073. end;
  7074. end;
  7075. if taicpu(hp1).oper[0]^.val = mask then
  7076. begin
  7077. { Everything checks out, perform the optimisation, as long as
  7078. the FLAGS register isn't being used}
  7079. TransferUsedRegs(TmpUsedRegs);
  7080. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  7081. {$ifdef x86_64}
  7082. if (hp1 <> hp2) then
  7083. begin
  7084. { "shl/mov/and" version }
  7085. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  7086. { Don't do the optimisation if the FLAGS register is in use }
  7087. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)) then
  7088. begin
  7089. DebugMsg(SPeepholeOptimization + 'ShlMovAnd2Shl', p);
  7090. { Don't remove the 'mov' instruction if its register is used elsewhere }
  7091. if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp2, TmpUsedRegs)) then
  7092. begin
  7093. RemoveInstruction(hp1);
  7094. Result := True;
  7095. end;
  7096. { Only set Result to True if the 'mov' instruction was removed }
  7097. RemoveInstruction(hp2);
  7098. end;
  7099. end
  7100. else
  7101. {$endif x86_64}
  7102. begin
  7103. { "shl/and" version }
  7104. { Don't do the optimisation if the FLAGS register is in use }
  7105. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  7106. begin
  7107. DebugMsg(SPeepholeOptimization + 'ShlAnd2Shl', p);
  7108. RemoveInstruction(hp1);
  7109. Result := True;
  7110. end;
  7111. end;
  7112. Exit;
  7113. end
  7114. else {$ifdef x86_64}if (hp1 = hp2) then{$endif x86_64}
  7115. begin
  7116. { Even if the mask doesn't allow for its removal, we might be
  7117. able to optimise the mask for the "shl/and" version, which
  7118. may permit other peephole optimisations }
  7119. {$ifdef DEBUG_AOPTCPU}
  7120. mask := taicpu(hp1).oper[0]^.val and mask;
  7121. if taicpu(hp1).oper[0]^.val <> mask then
  7122. begin
  7123. DebugMsg(
  7124. SPeepholeOptimization +
  7125. 'Changed mask from $' + debug_tostr(taicpu(hp1).oper[0]^.val) +
  7126. ' to $' + debug_tostr(mask) +
  7127. 'based on previous instruction (ShlAnd2ShlAnd)', hp1);
  7128. taicpu(hp1).oper[0]^.val := mask;
  7129. end;
  7130. {$else DEBUG_AOPTCPU}
  7131. { If debugging is off, just set the operand even if it's the same }
  7132. taicpu(hp1).oper[0]^.val := taicpu(hp1).oper[0]^.val and mask;
  7133. {$endif DEBUG_AOPTCPU}
  7134. end;
  7135. end;
  7136. {
  7137. change
  7138. shl/sal const,reg
  7139. <op> ...(...,reg,1),...
  7140. into
  7141. <op> ...(...,reg,1 shl const),...
  7142. if const in 1..3
  7143. }
  7144. if MatchOpType(taicpu(p), top_const, top_reg) and
  7145. (taicpu(p).oper[0]^.val in [1..3]) and
  7146. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) and
  7147. ((MatchInstruction(hp1,A_MOV,A_LEA,[]) and
  7148. MatchOpType(taicpu(hp1),top_ref,top_reg)) or
  7149. (MatchInstruction(hp1,A_FST,A_FSTP,A_FLD,[]) and
  7150. MatchOpType(taicpu(hp1),top_ref))
  7151. ) and
  7152. (taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.index) and
  7153. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^.ref^.base) and
  7154. (taicpu(hp1).oper[0]^.ref^.scalefactor in [0,1]) then
  7155. begin
  7156. TransferUsedRegs(TmpUsedRegs);
  7157. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  7158. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  7159. begin
  7160. taicpu(hp1).oper[0]^.ref^.scalefactor:=1 shl taicpu(p).oper[0]^.val;
  7161. DebugMsg(SPeepholeOptimization + 'ShlOp2Op', p);
  7162. RemoveCurrentP(p);
  7163. Result:=true;
  7164. exit;
  7165. end;
  7166. end;
  7167. if MatchOpType(taicpu(p), top_const, top_reg) and
  7168. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) and
  7169. MatchInstruction(hp1,A_SHL,[taicpu(p).opsize]) and
  7170. MatchOpType(taicpu(hp1),top_const,top_reg) and
  7171. (taicpu(p).oper[1]^.reg=taicpu(hp1).oper[1]^.reg) then
  7172. begin
  7173. shiftval:=taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val;
  7174. if ((taicpu(p).opsize=S_B) and (shiftval>7)) or
  7175. ((taicpu(p).opsize=S_W) and (shiftval>15)) or
  7176. {$ifdef x86_64}
  7177. ((taicpu(p).opsize=S_Q) and (shiftval>63)) or
  7178. {$endif x86_64}
  7179. ((taicpu(p).opsize=S_L) and (shiftval>31)) then
  7180. begin
  7181. DebugMsg(SPeepholeOptimization + 'ShlShl2Mov', p);
  7182. taicpu(hp1).opcode:=A_MOV;
  7183. taicpu(hp1).oper[0]^.val:=0;
  7184. end
  7185. else
  7186. begin
  7187. DebugMsg(SPeepholeOptimization + 'ShlShl2Shl', p);
  7188. taicpu(hp1).oper[0]^.val:=shiftval;
  7189. end;
  7190. RemoveCurrentP(p);
  7191. Result:=true;
  7192. exit;
  7193. end;
  7194. end;
  7195. class function TX86AsmOptimizer.IsShrMovZFoldable(shr_size, movz_size: topsize; Shift: TCGInt): Boolean;
  7196. begin
  7197. case shr_size of
  7198. S_B:
  7199. { No valid combinations }
  7200. Result := False;
  7201. S_W:
  7202. Result := (Shift >= 8) and (movz_size = S_BW);
  7203. S_L:
  7204. Result :=
  7205. (Shift >= 24) { Any opsize is valid for this shift } or
  7206. ((Shift >= 16) and (movz_size = S_WL));
  7207. {$ifdef x86_64}
  7208. S_Q:
  7209. Result :=
  7210. (Shift >= 56) { Any opsize is valid for this shift } or
  7211. ((Shift >= 48) and (movz_size = S_WL));
  7212. {$endif x86_64}
  7213. else
  7214. InternalError(2022081510);
  7215. end;
  7216. end;
  7217. function TX86AsmOptimizer.HandleSHRMerge(var p: tai; const PostPeephole: Boolean): Boolean;
  7218. var
  7219. hp1, hp2: tai;
  7220. IdentityMask, Shift: TCGInt;
  7221. LimitSize: Topsize;
  7222. DoNotMerge: Boolean;
  7223. begin
  7224. if not MatchInstruction(p, A_SHR, []) then
  7225. InternalError(2025040301);
  7226. Result := False;
  7227. DoNotMerge := False;
  7228. Shift := taicpu(p).oper[0]^.val;
  7229. LimitSize := taicpu(p).opsize;
  7230. hp1 := p;
  7231. repeat
  7232. if not GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[1]^.reg) or (hp1.typ <> ait_instruction) then
  7233. Exit;
  7234. case taicpu(hp1).opcode of
  7235. A_AND:
  7236. { Detect:
  7237. shr x, %reg
  7238. and y, %reg
  7239. If and y, %reg doesn't actually change the value of %reg (e.g. with
  7240. "shrl $24,%reg; andl $255,%reg", remove the AND instruction.
  7241. (Post-peephole only)
  7242. }
  7243. if PostPeephole and
  7244. (taicpu(hp1).opsize = taicpu(p).opsize) and
  7245. MatchOpType(taicpu(hp1), top_const, top_reg) and
  7246. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  7247. begin
  7248. { Make sure the FLAGS register isn't in use }
  7249. TransferUsedRegs(TmpUsedRegs);
  7250. hp2 := p;
  7251. repeat
  7252. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  7253. until not GetNextInstruction(hp2, hp2) or (hp2 = hp1);
  7254. if not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs) then
  7255. begin
  7256. { Generate the identity mask }
  7257. case taicpu(p).opsize of
  7258. S_B:
  7259. IdentityMask := $FF shr Shift;
  7260. S_W:
  7261. IdentityMask := $FFFF shr Shift;
  7262. S_L:
  7263. IdentityMask := $FFFFFFFF shr Shift;
  7264. {$ifdef x86_64}
  7265. S_Q:
  7266. { We need to force the operands to be unsigned 64-bit
  7267. integers otherwise the wrong value is generated }
  7268. IdentityMask := TCGInt(QWord($FFFFFFFFFFFFFFFF) shr QWord(Shift));
  7269. {$endif x86_64}
  7270. else
  7271. InternalError(2022081501);
  7272. end;
  7273. if (taicpu(hp1).oper[0]^.val and IdentityMask) = IdentityMask then
  7274. begin
  7275. DebugMsg(SPeepholeOptimization + 'Removed AND instruction since previous SHR makes this an identity operation (ShrAnd2Shr)', hp1);
  7276. { All the possible 1 bits are covered, so we can remove the AND }
  7277. hp2 := tai(hp1.Previous);
  7278. RemoveInstruction(hp1);
  7279. { p wasn't actually changed, so don't set Result to True,
  7280. but a change was nonetheless made elsewhere }
  7281. Include(OptsToCheck, aoc_ForceNewIteration);
  7282. { Do another pass in case other AND or MOVZX instructions
  7283. follow }
  7284. hp1 := hp2;
  7285. Continue;
  7286. end;
  7287. end;
  7288. end;
  7289. A_TEST, A_CMP:
  7290. { Skip over relevant comparisons, but shift instructions must
  7291. now not be merged since the original value is being read }
  7292. begin
  7293. DoNotMerge := True;
  7294. Continue;
  7295. end;
  7296. A_Jcc:
  7297. { Skip over conditional jumps and relevant comparisons }
  7298. Continue;
  7299. A_MOVZX:
  7300. if MatchOpType(taicpu(hp1), top_reg, top_reg) and
  7301. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg) then
  7302. begin
  7303. { Since the original register is being read as is, subsequent
  7304. SHRs must not be merged at this point }
  7305. DoNotMerge := True;
  7306. if IsShrMovZFoldable(taicpu(p).opsize, taicpu(hp1).opsize, Shift) then
  7307. begin
  7308. if SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg) then
  7309. begin
  7310. { If the MOVZX instruction reads and writes the same register,
  7311. defer this to the post-peephole optimisation stage }
  7312. if PostPeephole then
  7313. begin
  7314. DebugMsg(SPeepholeOptimization + 'Removed MOVZX instruction since previous SHR makes it unnecessary (ShrMovz2Shr)', hp1);
  7315. { All the possible 1 bits are covered, so we can remove the MOVZX }
  7316. hp2 := tai(hp1.Previous);
  7317. RemoveInstruction(hp1);
  7318. hp1 := hp2;
  7319. end;
  7320. end
  7321. else { Different register target }
  7322. begin
  7323. DebugMsg(SPeepholeOptimization + 'Converted MOVZX instruction to MOV since previous SHR makes zero-extension unnecessary (ShrMovz2ShrMov 1)', hp1);
  7324. taicpu(hp1).opcode := A_MOV;
  7325. setsubreg(taicpu(hp1).oper[0]^.reg, getsubreg(taicpu(hp1).oper[1]^.reg));
  7326. case taicpu(hp1).opsize of
  7327. S_BW:
  7328. taicpu(hp1).opsize := S_W;
  7329. S_BL, S_WL:
  7330. taicpu(hp1).opsize := S_L;
  7331. else
  7332. InternalError(2022081503);
  7333. end;
  7334. { p itself hasn't changed, so no need to set Result to True }
  7335. Include(OptsToCheck, aoc_ForceNewIteration);
  7336. { See if there's anything afterwards that can be
  7337. optimised, since the input register hasn't changed }
  7338. Continue;
  7339. end;
  7340. Exit;
  7341. end
  7342. else if PostPeephole and
  7343. (Shift > 0) and
  7344. (taicpu(p).opsize = S_W) and
  7345. (taicpu(hp1).opsize = S_WL) and
  7346. (taicpu(hp1).oper[0]^.reg = NR_AX) and
  7347. (taicpu(hp1).oper[1]^.reg = NR_EAX) then
  7348. begin
  7349. { Detect:
  7350. shr x, %ax (x > 0)
  7351. ...
  7352. movzwl %ax,%eax
  7353. -
  7354. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  7355. But first, check to see if movzwl %ax,%eax can be removed...
  7356. }
  7357. hp2 := tai(hp1.Previous);
  7358. TransferUsedRegs(TmpUsedRegs);
  7359. UpdateUsedRegsBetween(UsedRegs, p, hp1);
  7360. if PostPeepholeOptMovZX(hp1) then
  7361. hp1 := hp2
  7362. else
  7363. begin
  7364. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via ShrMovz2ShrCwtl)', hp1);
  7365. taicpu(hp1).opcode := A_CWDE;
  7366. taicpu(hp1).clearop(0);
  7367. taicpu(hp1).clearop(1);
  7368. taicpu(hp1).ops := 0;
  7369. end;
  7370. RestoreUsedRegs(TmpUsedRegs);
  7371. { Don't need to set aoc_ForceNewIteration if
  7372. PostPeepholeOptMovZX returned True because it's the
  7373. post-peephole stage }
  7374. end;
  7375. { Move onto the next instruction }
  7376. Continue;
  7377. end;
  7378. A_SHL, A_SAL, A_SHR:
  7379. if (taicpu(hp1).opsize <= LimitSize) and
  7380. MatchOpType(taicpu(hp1), top_const, top_reg) and
  7381. SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[1]^.reg) then
  7382. begin
  7383. { Make sure the sizes don't exceed the register size limit
  7384. (measured by the shift value falling below the limit) }
  7385. if taicpu(hp1).opsize < LimitSize then
  7386. LimitSize := taicpu(hp1).opsize;
  7387. if taicpu(hp1).opcode = A_SHR then
  7388. Inc(Shift, taicpu(hp1).oper[0]^.val)
  7389. else
  7390. begin
  7391. Dec(Shift, taicpu(hp1).oper[0]^.val);
  7392. DoNotMerge := True;
  7393. end;
  7394. if Shift < topsize2memsize[taicpu(p).opsize] - topsize2memsize[LimitSize] then
  7395. Exit;
  7396. { Since we've established that the combined shift is within
  7397. limits, we can actually combine the adjacent SHR
  7398. instructions even if they're different sizes }
  7399. if not DoNotMerge and (taicpu(hp1).opcode = A_SHR) then
  7400. begin
  7401. hp2 := tai(hp1.Previous);
  7402. DebugMsg(SPeepholeOptimization + 'ShrShr2Shr 1', p);
  7403. Inc(taicpu(p).oper[0]^.val, taicpu(hp1).oper[0]^.val);
  7404. RemoveInstruction(hp1);
  7405. hp1 := hp2;
  7406. { Though p has changed, only the constant has, and its
  7407. effects can still be detected on the next iteration of
  7408. the repeat..until loop }
  7409. Include(OptsToCheck, aoc_ForceNewIteration);
  7410. end;
  7411. { Move onto the next instruction }
  7412. Continue;
  7413. end;
  7414. else
  7415. ;
  7416. end;
  7417. { If the register isn't actually modified, move onto the next instruction,
  7418. but set DoNotMerge to True since the register is being read }
  7419. if (
  7420. { Under -O2 and below, GetNextInstructionUsingReg only returns
  7421. the next instruction, whether or not it contains the register }
  7422. (cs_opt_level3 in current_settings.optimizerswitches) or
  7423. RegReadByInstruction(taicpu(p).oper[1]^.reg, hp1)
  7424. ) and not RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp1) then
  7425. begin
  7426. DoNotMerge := True;
  7427. Continue;
  7428. end;
  7429. Break;
  7430. until False;
  7431. end;
  7432. function TX86AsmOptimizer.OptPass1SHR(var p : tai) : boolean;
  7433. begin
  7434. Result := False;
  7435. { All these optimisations work on "shr const,%reg" }
  7436. if not MatchOpType(taicpu(p), top_const, top_reg) then
  7437. Exit;
  7438. Result := HandleSHRMerge(p, False);
  7439. end;
  7440. function TX86AsmOptimizer.CheckMemoryWrite(var first_mov, second_mov: taicpu): Boolean;
  7441. var
  7442. CurrentRef: TReference;
  7443. FullReg: TRegister;
  7444. hp1, hp2: tai;
  7445. begin
  7446. Result := False;
  7447. if (first_mov.opsize <> S_B) or (second_mov.opsize <> S_B) then
  7448. Exit;
  7449. { We assume you've checked if the operand is actually a reference by
  7450. this point. If it isn't, you'll most likely get an access violation }
  7451. CurrentRef := first_mov.oper[1]^.ref^;
  7452. { Memory must be aligned }
  7453. if (CurrentRef.offset mod 4) <> 0 then
  7454. Exit;
  7455. Inc(CurrentRef.offset);
  7456. CurrentRef.alignment := 1; { Otherwise references_equal will return False }
  7457. if MatchOperand(second_mov.oper[0]^, 0) and
  7458. references_equal(second_mov.oper[1]^.ref^, CurrentRef) and
  7459. GetNextInstruction(second_mov, hp1) and
  7460. (hp1.typ = ait_instruction) and
  7461. (taicpu(hp1).opcode = A_MOV) and
  7462. MatchOpType(taicpu(hp1), top_const, top_ref) and
  7463. (taicpu(hp1).oper[0]^.val = 0) then
  7464. begin
  7465. Inc(CurrentRef.offset);
  7466. CurrentRef.alignment := taicpu(hp1).oper[1]^.ref^.alignment; { Otherwise references_equal might return False }
  7467. FullReg := newreg(R_INTREGISTER,getsupreg(first_mov.oper[0]^.reg), R_SUBD);
  7468. if references_equal(taicpu(hp1).oper[1]^.ref^, CurrentRef) then
  7469. begin
  7470. case taicpu(hp1).opsize of
  7471. S_B:
  7472. if GetNextInstruction(hp1, hp2) and
  7473. MatchInstruction(taicpu(hp2), A_MOV, [S_B]) and
  7474. MatchOpType(taicpu(hp2), top_const, top_ref) and
  7475. (taicpu(hp2).oper[0]^.val = 0) then
  7476. begin
  7477. Inc(CurrentRef.offset);
  7478. CurrentRef.alignment := 1; { Otherwise references_equal will return False }
  7479. if references_equal(taicpu(hp2).oper[1]^.ref^, CurrentRef) and
  7480. (taicpu(hp2).opsize = S_B) then
  7481. begin
  7482. RemoveInstruction(hp1);
  7483. RemoveInstruction(hp2);
  7484. first_mov.opsize := S_L;
  7485. if first_mov.oper[0]^.typ = top_reg then
  7486. begin
  7487. DebugMsg(SPeepholeOptimization + 'MOVb/MOVb/MOVb/MOVb -> MOVZX/MOVl', first_mov);
  7488. { Reuse second_mov as a MOVZX instruction }
  7489. second_mov.opcode := A_MOVZX;
  7490. second_mov.opsize := S_BL;
  7491. second_mov.loadreg(0, first_mov.oper[0]^.reg);
  7492. second_mov.loadreg(1, FullReg);
  7493. first_mov.oper[0]^.reg := FullReg;
  7494. asml.Remove(second_mov);
  7495. asml.InsertBefore(second_mov, first_mov);
  7496. end
  7497. else
  7498. { It's a value }
  7499. begin
  7500. DebugMsg(SPeepholeOptimization + 'MOVb/MOVb/MOVb/MOVb -> MOVl', first_mov);
  7501. RemoveInstruction(second_mov);
  7502. end;
  7503. Result := True;
  7504. Exit;
  7505. end;
  7506. end;
  7507. S_W:
  7508. begin
  7509. RemoveInstruction(hp1);
  7510. first_mov.opsize := S_L;
  7511. if first_mov.oper[0]^.typ = top_reg then
  7512. begin
  7513. DebugMsg(SPeepholeOptimization + 'MOVb/MOVb/MOVw -> MOVZX/MOVl', first_mov);
  7514. { Reuse second_mov as a MOVZX instruction }
  7515. second_mov.opcode := A_MOVZX;
  7516. second_mov.opsize := S_BL;
  7517. second_mov.loadreg(0, first_mov.oper[0]^.reg);
  7518. second_mov.loadreg(1, FullReg);
  7519. first_mov.oper[0]^.reg := FullReg;
  7520. asml.Remove(second_mov);
  7521. asml.InsertBefore(second_mov, first_mov);
  7522. end
  7523. else
  7524. { It's a value }
  7525. begin
  7526. DebugMsg(SPeepholeOptimization + 'MOVb/MOVb/MOVw -> MOVl', first_mov);
  7527. RemoveInstruction(second_mov);
  7528. end;
  7529. Result := True;
  7530. Exit;
  7531. end;
  7532. else
  7533. ;
  7534. end;
  7535. end;
  7536. end;
  7537. end;
  7538. function TX86AsmOptimizer.OptPass1FSTP(var p: tai): boolean;
  7539. { returns true if a "continue" should be done after this optimization }
  7540. var
  7541. hp1, hp2, hp3: tai;
  7542. begin
  7543. Result := false;
  7544. hp3 := nil;
  7545. if MatchOpType(taicpu(p),top_ref) and
  7546. GetNextInstruction(p, hp1) and
  7547. (hp1.typ = ait_instruction) and
  7548. (((taicpu(hp1).opcode = A_FLD) and
  7549. (taicpu(p).opcode = A_FSTP)) or
  7550. ((taicpu(p).opcode = A_FISTP) and
  7551. (taicpu(hp1).opcode = A_FILD))) and
  7552. MatchOpType(taicpu(hp1),top_ref) and
  7553. (taicpu(hp1).opsize = taicpu(p).opsize) and
  7554. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  7555. begin
  7556. { replacing fstp f;fld f by fst f is only valid for extended because of rounding or if fastmath is on }
  7557. if ((taicpu(p).opsize=S_FX) or (cs_opt_fastmath in current_settings.optimizerswitches)) and
  7558. GetNextInstruction(hp1, hp2) and
  7559. (((hp2.typ = ait_instruction) and
  7560. IsExitCode(hp2) and
  7561. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  7562. not(assigned(current_procinfo.procdef.funcretsym) and
  7563. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  7564. (taicpu(p).oper[0]^.ref^.index = NR_NO)) or
  7565. { fstp <temp>
  7566. fld <temp>
  7567. <dealloc> <temp>
  7568. }
  7569. ((taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  7570. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  7571. SetAndTest(FindTempDeAlloc(taicpu(p).oper[0]^.ref^.offset,tai(hp1.next)),hp2) and
  7572. (tai_tempalloc(hp2).temppos=taicpu(p).oper[0]^.ref^.offset) and
  7573. (((taicpu(p).opsize=S_FX) and (tai_tempalloc(hp2).tempsize=16)) or
  7574. ((taicpu(p).opsize in [S_IQ,S_FL]) and (tai_tempalloc(hp2).tempsize=8)) or
  7575. ((taicpu(p).opsize=S_FS) and (tai_tempalloc(hp2).tempsize=4))
  7576. )
  7577. )
  7578. ) then
  7579. begin
  7580. DebugMsg(SPeepholeOptimization + 'FstpFld2<Nop>',p);
  7581. RemoveInstruction(hp1);
  7582. RemoveCurrentP(p, hp2);
  7583. { first case: exit code }
  7584. if hp2.typ = ait_instruction then
  7585. RemoveLastDeallocForFuncRes(p);
  7586. Result := true;
  7587. end
  7588. else
  7589. { we can do this only in fast math mode as fstp is rounding ...
  7590. ... still disabled as it breaks the compiler and/or rtl }
  7591. if { (cs_opt_fastmath in current_settings.optimizerswitches) or }
  7592. { ... or if another fstp equal to the first one follows }
  7593. GetNextInstruction(hp1,hp2) and
  7594. (hp2.typ = ait_instruction) and
  7595. (taicpu(p).opcode=taicpu(hp2).opcode) and
  7596. (taicpu(p).opsize=taicpu(hp2).opsize) then
  7597. begin
  7598. if (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  7599. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  7600. SetAndTest(FindTempDeAlloc(taicpu(p).oper[0]^.ref^.offset,tai(hp2.next)),hp3) and
  7601. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  7602. (tai_tempalloc(hp3).temppos=taicpu(p).oper[0]^.ref^.offset) and
  7603. (((taicpu(p).opsize=S_FX) and (tai_tempalloc(hp3).tempsize=16)) or
  7604. ((taicpu(p).opsize in [S_IQ,S_FL]) and (tai_tempalloc(hp3).tempsize=8)) or
  7605. ((taicpu(p).opsize=S_FS) and (tai_tempalloc(hp3).tempsize=4))
  7606. ) then
  7607. begin
  7608. DebugMsg(SPeepholeOptimization + 'FstpFldFstp2Fstp',p);
  7609. RemoveCurrentP(p,hp2);
  7610. RemoveInstruction(hp1);
  7611. Result := true;
  7612. end
  7613. else if { fst can't store an extended/comp value }
  7614. (taicpu(p).opsize <> S_FX) and
  7615. (taicpu(p).opsize <> S_IQ) then
  7616. begin
  7617. if (taicpu(p).opcode = A_FSTP) then
  7618. taicpu(p).opcode := A_FST
  7619. else
  7620. taicpu(p).opcode := A_FIST;
  7621. DebugMsg(SPeepholeOptimization + 'FstpFld2Fst',p);
  7622. RemoveInstruction(hp1);
  7623. Result := true;
  7624. end;
  7625. end;
  7626. end;
  7627. end;
  7628. function TX86AsmOptimizer.OptPass1FLD(var p : tai) : boolean;
  7629. var
  7630. hp1, hp2, hp3: tai;
  7631. begin
  7632. result:=false;
  7633. if MatchOpType(taicpu(p),top_reg) and
  7634. GetNextInstruction(p, hp1) and
  7635. (hp1.typ = Ait_Instruction) and
  7636. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  7637. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  7638. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  7639. { change to
  7640. fld reg fxxx reg,st
  7641. fxxxp st, st1 (hp1)
  7642. Remark: non commutative operations must be reversed!
  7643. }
  7644. begin
  7645. case taicpu(hp1).opcode Of
  7646. A_FMULP,A_FADDP,
  7647. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  7648. begin
  7649. case taicpu(hp1).opcode Of
  7650. A_FADDP: taicpu(hp1).opcode := A_FADD;
  7651. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  7652. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  7653. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  7654. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  7655. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  7656. else
  7657. internalerror(2019050534);
  7658. end;
  7659. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  7660. taicpu(hp1).oper[1]^.reg := NR_ST;
  7661. DebugMsg(SPeepholeOptimization + 'FldF*p2F*',hp1);
  7662. RemoveCurrentP(p, hp1);
  7663. Result:=true;
  7664. exit;
  7665. end;
  7666. else
  7667. ;
  7668. end;
  7669. end
  7670. else
  7671. if MatchOpType(taicpu(p),top_ref) and
  7672. GetNextInstruction(p, hp2) and
  7673. (hp2.typ = Ait_Instruction) and
  7674. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  7675. (taicpu(p).opsize in [S_FS, S_FL]) and
  7676. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  7677. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  7678. if GetLastInstruction(p, hp1) and
  7679. MatchInstruction(hp1,A_FLD,A_FST,[taicpu(p).opsize]) and
  7680. MatchOpType(taicpu(hp1),top_ref) and
  7681. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  7682. if ((taicpu(hp2).opcode = A_FMULP) or
  7683. (taicpu(hp2).opcode = A_FADDP)) then
  7684. { change to
  7685. fld/fst mem1 (hp1) fld/fst mem1
  7686. fld mem1 (p) fadd/
  7687. faddp/ fmul st, st
  7688. fmulp st, st1 (hp2) }
  7689. begin
  7690. DebugMsg(SPeepholeOptimization + 'Fld/FstFldFaddp/Fmulp2Fld/FstFadd/Fmul',hp1);
  7691. RemoveCurrentP(p, hp1);
  7692. if (taicpu(hp2).opcode = A_FADDP) then
  7693. taicpu(hp2).opcode := A_FADD
  7694. else
  7695. taicpu(hp2).opcode := A_FMUL;
  7696. taicpu(hp2).oper[1]^.reg := NR_ST;
  7697. end
  7698. else
  7699. { change to
  7700. fld/fst mem1 (hp1) fld/fst mem1
  7701. fld mem1 (p) fld st
  7702. }
  7703. begin
  7704. DebugMsg(SPeepholeOptimization + 'Fld/Fst<mem>Fld<mem>2Fld/Fst<mem>Fld<reg>',hp1);
  7705. taicpu(p).changeopsize(S_FL);
  7706. taicpu(p).loadreg(0,NR_ST);
  7707. end
  7708. else
  7709. begin
  7710. case taicpu(hp2).opcode Of
  7711. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  7712. { change to
  7713. fld/fst mem1 (hp1) fld/fst mem1
  7714. fld mem2 (p) fxxx mem2
  7715. fxxxp st, st1 (hp2) }
  7716. begin
  7717. case taicpu(hp2).opcode Of
  7718. A_FADDP: taicpu(p).opcode := A_FADD;
  7719. A_FMULP: taicpu(p).opcode := A_FMUL;
  7720. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  7721. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  7722. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  7723. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  7724. else
  7725. internalerror(2019050533);
  7726. end;
  7727. DebugMsg(SPeepholeOptimization + 'Fld/FstFldF*2Fld/FstF*',p);
  7728. RemoveInstruction(hp2);
  7729. end
  7730. else
  7731. ;
  7732. end
  7733. end
  7734. end;
  7735. function IsCmpSubset(cond1, cond2: TAsmCond): Boolean; inline;
  7736. begin
  7737. Result := condition_in(cond1, cond2) or
  7738. { Not strictly subsets due to the actual flags checked, but because we're
  7739. comparing integers, E is a subset of AE and GE and their aliases }
  7740. ((cond1 in [C_E, C_Z]) and (cond2 in [C_AE, C_NB, C_NC, C_GE, C_NL]));
  7741. end;
  7742. function TX86AsmOptimizer.OptPass1Cmp(var p: tai): boolean;
  7743. var
  7744. v: TCGInt;
  7745. true_hp1, hp1, hp2, p_dist, p_jump, hp1_dist, p_label, hp1_label: tai;
  7746. FirstMatch, TempBool: Boolean;
  7747. NewReg: TRegister;
  7748. JumpLabel, JumpLabel_dist, JumpLabel_far: TAsmLabel;
  7749. begin
  7750. Result:=false;
  7751. { All these optimisations need a next instruction }
  7752. if not GetNextInstruction(p, hp1) then
  7753. Exit;
  7754. true_hp1 := hp1;
  7755. { Search for:
  7756. cmp ###,###
  7757. j(c1) @lbl1
  7758. ...
  7759. @lbl:
  7760. cmp ###,### (same comparison as above)
  7761. j(c2) @lbl2
  7762. If c1 is a subset of c2, change to:
  7763. cmp ###,###
  7764. j(c1) @lbl2
  7765. (@lbl1 may become a dead label as a result)
  7766. }
  7767. { Also handle cases where there are multiple jumps in a row }
  7768. p_jump := hp1;
  7769. while Assigned(p_jump) and MatchInstruction(p_jump, A_JCC, []) do
  7770. begin
  7771. Prefetch(p_jump.Next);
  7772. if IsJumpToLabel(taicpu(p_jump)) then
  7773. begin
  7774. { Do jump optimisations first in case the condition becomes
  7775. unnecessary }
  7776. TempBool := True;
  7777. if DoJumpOptimizations(p_jump, TempBool) or
  7778. not TempBool then
  7779. begin
  7780. if Assigned(p_jump) then
  7781. begin
  7782. { CollapseZeroDistJump will be set to the label or an align
  7783. before it after the jump if it optimises, whether or not
  7784. the label is live or dead }
  7785. if (p_jump.typ = ait_align) or
  7786. (
  7787. (p_jump.typ = ait_label) and
  7788. not (tai_label(p_jump).labsym.is_used)
  7789. ) then
  7790. GetNextInstruction(p_jump, p_jump);
  7791. end;
  7792. TransferUsedRegs(TmpUsedRegs);
  7793. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  7794. if not Assigned(p_jump) or
  7795. (
  7796. not MatchInstruction(p_jump, A_Jcc, A_SETcc, A_CMOVcc, []) and
  7797. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, p_jump, TmpUsedRegs)
  7798. ) then
  7799. begin
  7800. { No more conditional jumps; conditional statement is no longer required }
  7801. DebugMsg(SPeepholeOptimization + 'Removed unnecessary condition (Cmp2Nop)', p);
  7802. RemoveCurrentP(p);
  7803. Result := True;
  7804. Exit;
  7805. end;
  7806. hp1 := p_jump;
  7807. Include(OptsToCheck, aoc_ForceNewIteration);
  7808. Continue;
  7809. end;
  7810. JumpLabel := TAsmLabel(taicpu(p_jump).oper[0]^.ref^.symbol);
  7811. if GetNextInstruction(p_jump, hp2) and
  7812. (
  7813. OptimizeConditionalJump(JumpLabel, p_jump, hp2, TempBool) or
  7814. not TempBool
  7815. ) then
  7816. begin
  7817. hp1 := p_jump;
  7818. Include(OptsToCheck, aoc_ForceNewIteration);
  7819. Continue;
  7820. end;
  7821. p_label := nil;
  7822. if Assigned(JumpLabel) then
  7823. p_label := getlabelwithsym(JumpLabel);
  7824. if Assigned(p_label) and
  7825. GetNextInstruction(p_label, p_dist) and
  7826. MatchInstruction(p_dist, A_CMP, []) and
  7827. MatchOperand(taicpu(p_dist).oper[0]^, taicpu(p).oper[0]^) and
  7828. MatchOperand(taicpu(p_dist).oper[1]^, taicpu(p).oper[1]^) and
  7829. GetNextInstruction(p_dist, hp1_dist) and
  7830. MatchInstruction(hp1_dist, A_JCC, []) then { This doesn't have to be an explicit label }
  7831. begin
  7832. JumpLabel_dist := TAsmLabel(taicpu(hp1_dist).oper[0]^.ref^.symbol);
  7833. if JumpLabel = JumpLabel_dist then
  7834. { This is an infinite loop }
  7835. Exit;
  7836. { Best optimisation when the first condition is a subset (or equal) of the second }
  7837. if IsCmpSubset(taicpu(p_jump).condition, taicpu(hp1_dist).condition) then
  7838. begin
  7839. { Any registers used here will already be allocated }
  7840. if Assigned(JumpLabel) then
  7841. JumpLabel.DecRefs;
  7842. DebugMsg(SPeepholeOptimization + 'CMP/Jcc/@Lbl/CMP/Jcc -> CMP/Jcc, redirecting first jump', p_jump);
  7843. taicpu(p_jump).loadref(0, taicpu(hp1_dist).oper[0]^.ref^); { This also increases the reference count }
  7844. Include(OptsToCheck, aoc_ForceNewIteration);
  7845. { Don't exit yet. Since p and p_jump haven't actually been
  7846. removed, we can check for more on this iteration }
  7847. end
  7848. else if IsCmpSubset(taicpu(hp1_dist).condition, inverse_cond(taicpu(p_jump).condition)) and
  7849. GetNextInstruction(hp1_dist, hp1_label) and
  7850. (hp1_label.typ = ait_label) then
  7851. begin
  7852. JumpLabel_far := tai_label(hp1_label).labsym;
  7853. if (JumpLabel_far = JumpLabel_dist) or (JumpLabel_far = JumpLabel) then
  7854. { This is an infinite loop }
  7855. Exit;
  7856. if Assigned(JumpLabel_far) then
  7857. begin
  7858. { In this situation, if the first jump branches, the second one will never,
  7859. branch so change the destination label to after the second jump }
  7860. DebugMsg(SPeepholeOptimization + 'CMP/Jcc/@Lbl/CMP/Jcc/@Lbl -> CMP/Jcc, redirecting first jump to 2nd label', p_jump);
  7861. if Assigned(JumpLabel) then
  7862. JumpLabel.DecRefs;
  7863. JumpLabel_far.IncRefs;
  7864. taicpu(p_jump).oper[0]^.ref^.symbol := JumpLabel_far;
  7865. Result := True;
  7866. { Don't exit yet. Since p and p_jump haven't actually been
  7867. removed, we can check for more on this iteration }
  7868. Continue;
  7869. end;
  7870. end;
  7871. end;
  7872. end;
  7873. { Search for:
  7874. cmp ###,###
  7875. j(c1) @lbl1
  7876. cmp ###,### (same as first)
  7877. Remove second cmp
  7878. }
  7879. if GetNextInstruction(p_jump, hp2) and
  7880. (
  7881. (
  7882. MatchInstruction(hp2, A_CMP, [taicpu(p).opsize]) and
  7883. (
  7884. (
  7885. MatchOpType(taicpu(p), top_const, top_reg) and
  7886. MatchOpType(taicpu(hp2), top_const, top_reg) and
  7887. (taicpu(hp2).oper[0]^.val = taicpu(p).oper[0]^.val) and
  7888. Reg1WriteOverwritesReg2Entirely(taicpu(hp2).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  7889. ) or (
  7890. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  7891. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^)
  7892. )
  7893. )
  7894. ) or (
  7895. { Also match cmp $0,%reg; jcc @lbl; test %reg,%reg }
  7896. MatchOperand(taicpu(p).oper[0]^, 0) and
  7897. (taicpu(p).oper[1]^.typ = top_reg) and
  7898. MatchInstruction(hp2, A_TEST, []) and
  7899. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  7900. (taicpu(hp2).oper[0]^.reg = taicpu(hp2).oper[1]^.reg) and
  7901. Reg1WriteOverwritesReg2Entirely(taicpu(hp2).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  7902. )
  7903. ) then
  7904. begin
  7905. DebugMsg(SPeepholeOptimization + 'CMP/Jcc/CMP; removed superfluous CMP', hp2);
  7906. TransferUsedRegs(TmpUsedRegs);
  7907. AllocRegBetween(NR_DEFAULTFLAGS, p, hp2, TmpUsedRegs);
  7908. RemoveInstruction(hp2);
  7909. Result := True;
  7910. { Continue the while loop in case "Jcc/CMP" follows the second CMP that was just removed }
  7911. end
  7912. else
  7913. begin
  7914. { hp2 is the next instruction, so save time and just set p_jump
  7915. to it instead of calling GetNextInstruction below }
  7916. p_jump := hp2;
  7917. Continue;
  7918. end;
  7919. GetNextInstruction(p_jump, p_jump);
  7920. end;
  7921. if (
  7922. { Don't call GetNextInstruction again if we already have it }
  7923. (true_hp1 = p_jump) or
  7924. GetNextInstruction(p, hp1)
  7925. ) and
  7926. MatchInstruction(hp1, A_Jcc, []) and
  7927. IsJumpToLabel(taicpu(hp1)) and
  7928. (taicpu(hp1).condition in [C_E, C_Z, C_NE, C_NZ]) and
  7929. GetNextInstruction(hp1, hp2) then
  7930. begin
  7931. {
  7932. cmp x, y (or "cmp y, x")
  7933. je @lbl
  7934. mov x, y
  7935. @lbl:
  7936. (x and y can be constants, registers or references)
  7937. Change to:
  7938. mov x, y (x and y will always be equal in the end)
  7939. @lbl: (may beceome a dead label)
  7940. Also:
  7941. cmp x, y (or "cmp y, x")
  7942. jne @lbl
  7943. mov x, y
  7944. @lbl:
  7945. (x and y can be constants, registers or references)
  7946. Change to:
  7947. Absolutely nothing! (Except @lbl if it's still live)
  7948. }
  7949. if MatchInstruction(hp2, A_MOV, [taicpu(p).opsize]) and
  7950. (
  7951. (
  7952. MatchOperand(taicpu(p).oper[0]^, taicpu(hp2).oper[0]^) and
  7953. MatchOperand(taicpu(p).oper[1]^, taicpu(hp2).oper[1]^)
  7954. ) or (
  7955. MatchOperand(taicpu(p).oper[0]^, taicpu(hp2).oper[1]^) and
  7956. MatchOperand(taicpu(p).oper[1]^, taicpu(hp2).oper[0]^)
  7957. )
  7958. ) and
  7959. GetNextInstruction(hp2, hp1_label) and
  7960. (hp1_label.typ = ait_label) and
  7961. (tai_label(hp1_label).labsym = taicpu(hp1).oper[0]^.ref^.symbol) then
  7962. begin
  7963. tai_label(hp1_label).labsym.DecRefs;
  7964. if (taicpu(hp1).condition in [C_NE, C_NZ]) then
  7965. begin
  7966. DebugMsg(SPeepholeOptimization + 'CMP/JNE/MOV/@Lbl -> NOP, since the MOV is only executed if the operands are equal (CmpJneMov2Nop)', p);
  7967. RemoveInstruction(hp2);
  7968. hp2 := hp1_label; { So RemoveCurrentp below can be set to something valid }
  7969. end
  7970. else
  7971. DebugMsg(SPeepholeOptimization + 'CMP/JE/MOV/@Lbl -> MOV, since the MOV is only executed if the operands aren''t equal (CmpJeMov2Mov)', p);
  7972. RemoveInstruction(hp1);
  7973. RemoveCurrentp(p, hp2);
  7974. Result := True;
  7975. Exit;
  7976. end;
  7977. {
  7978. Try to optimise the following:
  7979. cmp $x,### ($x and $y can be registers or constants)
  7980. je @lbl1 (only reference)
  7981. cmp $y,### (### are identical)
  7982. @Lbl:
  7983. sete %reg1
  7984. Change to:
  7985. cmp $x,###
  7986. sete %reg2 (allocate new %reg2)
  7987. cmp $y,###
  7988. sete %reg1
  7989. orb %reg2,%reg1
  7990. (dealloc %reg2)
  7991. This adds an instruction (so don't perform under -Os), but it removes
  7992. a conditional branch.
  7993. }
  7994. if not (cs_opt_size in current_settings.optimizerswitches) and
  7995. MatchInstruction(hp2, A_CMP, A_TEST, [taicpu(p).opsize]) and
  7996. MatchOperand(taicpu(p).oper[1]^, taicpu(hp2).oper[1]^) and
  7997. { The first operand of CMP instructions can only be a register or
  7998. immediate anyway, so no need to check }
  7999. GetNextInstruction(hp2, p_label) and
  8000. (p_label.typ = ait_label) and
  8001. (tai_label(p_label).labsym.getrefs = 1) and
  8002. (JumpTargetOp(taicpu(hp1))^.ref^.symbol = tai_label(p_label).labsym) and
  8003. GetNextInstruction(p_label, p_dist) and
  8004. MatchInstruction(p_dist, A_SETcc, []) and
  8005. (taicpu(p_dist).condition in [C_E, C_Z]) and
  8006. (taicpu(p_dist).oper[0]^.typ = top_reg) then
  8007. begin
  8008. TransferUsedRegs(TmpUsedRegs);
  8009. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  8010. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  8011. UpdateUsedRegs(TmpUsedRegs, tai(p_label.Next));
  8012. UpdateUsedRegs(TmpUsedRegs, tai(p_dist.Next));
  8013. if not RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) and
  8014. { Get the instruction after the SETcc instruction so we can
  8015. allocate a new register over the entire range }
  8016. GetNextInstruction(p_dist, hp1_dist) then
  8017. begin
  8018. { Register can appear in p if it's not used afterwards, so only
  8019. allocate between hp1 and hp1_dist }
  8020. NewReg := GetIntRegisterBetween(R_SUBL, TmpUsedRegs, hp1, hp1_dist);
  8021. if NewReg <> NR_NO then
  8022. begin
  8023. DebugMsg(SPeepholeOptimization + 'CMP/JE/CMP/@Lbl/SETE -> CMP/SETE/CMP/SETE/OR, removing conditional branch', p);
  8024. { Change the jump instruction into a SETcc instruction }
  8025. taicpu(hp1).opcode := A_SETcc;
  8026. taicpu(hp1).opsize := S_B;
  8027. taicpu(hp1).loadreg(0, NewReg);
  8028. { This is now a dead label }
  8029. tai_label(p_label).labsym.decrefs;
  8030. { Prefer adding before the next instruction so the FLAGS
  8031. register is deallicated first }
  8032. AsmL.InsertBefore(
  8033. taicpu.op_reg_reg(A_OR, S_B, NewReg, taicpu(p_dist).oper[0]^.reg),
  8034. hp1_dist
  8035. );
  8036. Result := True;
  8037. { Don't exit yet, as p wasn't changed and hp1, while
  8038. modified, is still intact and might be optimised by the
  8039. SETcc optimisation below }
  8040. end;
  8041. end;
  8042. end;
  8043. end;
  8044. if (taicpu(p).oper[0]^.typ = top_const) and
  8045. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) then
  8046. begin
  8047. if (taicpu(p).oper[0]^.val = 0) and
  8048. (taicpu(p).oper[1]^.typ = top_reg) then
  8049. begin
  8050. hp2 := p;
  8051. FirstMatch := True;
  8052. { When dealing with "cmp $0,%reg", only ZF and SF contain
  8053. anything meaningful once it's converted to "test %reg,%reg";
  8054. additionally, some jumps will always (or never) branch, so
  8055. evaluate every jump immediately following the
  8056. comparison, optimising the conditions if possible.
  8057. Similarly with SETcc... those that are always set to 0 or 1
  8058. are changed to MOV instructions }
  8059. while FirstMatch or { Saves calling GetNextInstruction unnecessarily }
  8060. (
  8061. GetNextInstruction(hp2, hp1) and
  8062. MatchInstruction(hp1,A_Jcc,A_SETcc,[])
  8063. ) do
  8064. begin
  8065. Prefetch(hp1.Next);
  8066. FirstMatch := False;
  8067. case taicpu(hp1).condition of
  8068. C_B, C_C, C_NAE, C_O:
  8069. { For B/NAE:
  8070. Will never branch since an unsigned integer can never be below zero
  8071. For C/O:
  8072. Result cannot overflow because 0 is being subtracted
  8073. }
  8074. begin
  8075. if taicpu(hp1).opcode = A_Jcc then
  8076. begin
  8077. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (jump removed)', hp1);
  8078. TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
  8079. RemoveInstruction(hp1);
  8080. { Since hp1 was deleted, hp2 must not be updated }
  8081. Continue;
  8082. end
  8083. else
  8084. begin
  8085. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (set -> mov 0)', hp1);
  8086. { Convert "set(c) %reg" instruction to "movb 0,%reg" }
  8087. taicpu(hp1).opcode := A_MOV;
  8088. taicpu(hp1).ops := 2;
  8089. taicpu(hp1).condition := C_None;
  8090. taicpu(hp1).opsize := S_B;
  8091. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  8092. taicpu(hp1).loadconst(0, 0);
  8093. end;
  8094. end;
  8095. C_BE, C_NA:
  8096. begin
  8097. { Will only branch if equal to zero }
  8098. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition BE/NA --> E', hp1);
  8099. taicpu(hp1).condition := C_E;
  8100. end;
  8101. C_A, C_NBE:
  8102. begin
  8103. { Will only branch if not equal to zero }
  8104. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition A/NBE --> NE', hp1);
  8105. taicpu(hp1).condition := C_NE;
  8106. end;
  8107. C_AE, C_NB, C_NC, C_NO:
  8108. begin
  8109. { Will always branch }
  8110. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition AE/NB/NC/NO --> Always', hp1);
  8111. if taicpu(hp1).opcode = A_Jcc then
  8112. begin
  8113. MakeUnconditional(taicpu(hp1));
  8114. { Any jumps/set that follow will now be dead code }
  8115. RemoveDeadCodeAfterJump(taicpu(hp1));
  8116. Break;
  8117. end
  8118. else
  8119. begin
  8120. { Convert "set(c) %reg" instruction to "movb 1,%reg" }
  8121. taicpu(hp1).opcode := A_MOV;
  8122. taicpu(hp1).ops := 2;
  8123. taicpu(hp1).condition := C_None;
  8124. taicpu(hp1).opsize := S_B;
  8125. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  8126. taicpu(hp1).loadconst(0, 1);
  8127. end;
  8128. end;
  8129. C_None:
  8130. InternalError(2020012201);
  8131. C_P, C_PE, C_NP, C_PO:
  8132. { We can't handle parity checks and they should never be generated
  8133. after a general-purpose CMP (it's used in some floating-point
  8134. comparisons that don't use CMP) }
  8135. InternalError(2020012202);
  8136. else
  8137. { Zero/Equality, Sign, their complements and all of the
  8138. signed comparisons do not need to be converted };
  8139. end;
  8140. hp2 := hp1;
  8141. end;
  8142. { Convert the instruction to a TEST }
  8143. taicpu(p).opcode := A_TEST;
  8144. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  8145. Result := True;
  8146. Exit;
  8147. end
  8148. else
  8149. begin
  8150. TransferUsedRegs(TmpUsedRegs);
  8151. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  8152. if not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs) then
  8153. begin
  8154. if (taicpu(p).oper[0]^.val = 1) and
  8155. (taicpu(hp1).condition in [C_L, C_NL, C_NGE, C_GE]) then
  8156. begin
  8157. { Convert; To:
  8158. cmp $1,r/m cmp $0,r/m
  8159. jl @lbl jle @lbl
  8160. (Also do inverted conditions)
  8161. }
  8162. DebugMsg(SPeepholeOptimization + 'Cmp1Jl2Cmp0Jle', p);
  8163. taicpu(p).oper[0]^.val := 0;
  8164. if taicpu(hp1).condition in [C_L, C_NGE] then
  8165. taicpu(hp1).condition := C_LE
  8166. else
  8167. taicpu(hp1).condition := C_NLE;
  8168. { If the instruction is now "cmp $0,%reg", convert it to a
  8169. TEST (and effectively do the work of the "cmp $0,%reg" in
  8170. the block above)
  8171. }
  8172. if (taicpu(p).oper[1]^.typ = top_reg) then
  8173. begin
  8174. taicpu(p).opcode := A_TEST;
  8175. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  8176. end;
  8177. Result := True;
  8178. Exit;
  8179. end
  8180. else if (taicpu(p).oper[1]^.typ = top_reg)
  8181. {$ifdef x86_64}
  8182. and (taicpu(p).opsize <> S_Q) { S_Q will never happen: cmp with 64 bit constants is not possible }
  8183. {$endif x86_64}
  8184. then
  8185. begin
  8186. { cmp register,$8000 neg register
  8187. je target --> jo target
  8188. .... only if register is deallocated before jump.}
  8189. case Taicpu(p).opsize of
  8190. S_B: v:=$80;
  8191. S_W: v:=$8000;
  8192. S_L: v:=qword($80000000);
  8193. else
  8194. internalerror(2013112905);
  8195. end;
  8196. if (taicpu(p).oper[0]^.val=v) and
  8197. (Taicpu(hp1).condition in [C_E,C_NE]) then
  8198. begin
  8199. TransferUsedRegs(TmpUsedRegs);
  8200. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  8201. if not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, TmpUsedRegs)) then
  8202. begin
  8203. DebugMsg(SPeepholeOptimization + 'CmpJe2NegJo done',p);
  8204. Taicpu(p).opcode:=A_NEG;
  8205. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  8206. Taicpu(p).clearop(1);
  8207. Taicpu(p).ops:=1;
  8208. if Taicpu(hp1).condition=C_E then
  8209. Taicpu(hp1).condition:=C_O
  8210. else
  8211. Taicpu(hp1).condition:=C_NO;
  8212. Result:=true;
  8213. exit;
  8214. end;
  8215. end;
  8216. end;
  8217. end;
  8218. end;
  8219. end;
  8220. if TrySwapMovCmp(p, hp1) then
  8221. begin
  8222. Result := True;
  8223. Exit;
  8224. end;
  8225. end;
  8226. function TX86AsmOptimizer.OptPass1PXor(var p: tai): boolean;
  8227. var
  8228. hp1: tai;
  8229. begin
  8230. {
  8231. remove the second (v)pxor from
  8232. pxor reg,reg
  8233. ...
  8234. pxor reg,reg
  8235. }
  8236. Result:=false;
  8237. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  8238. MatchOpType(taicpu(p),top_reg,top_reg) and
  8239. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  8240. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  8241. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  8242. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) then
  8243. begin
  8244. DebugMsg(SPeepholeOptimization + 'PXorPXor2PXor done',hp1);
  8245. RemoveInstruction(hp1);
  8246. Result:=true;
  8247. Exit;
  8248. end
  8249. {
  8250. replace
  8251. pxor reg1,reg1
  8252. movapd/s reg1,reg2
  8253. dealloc reg1
  8254. by
  8255. pxor reg2,reg2
  8256. }
  8257. else if GetNextInstruction(p,hp1) and
  8258. { we mix single and double opperations here because we assume that the compiler
  8259. generates vmovapd only after double operations and vmovaps only after single operations }
  8260. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  8261. MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  8262. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  8263. (taicpu(p).oper[0]^.typ=top_reg) then
  8264. begin
  8265. TransferUsedRegs(TmpUsedRegs);
  8266. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  8267. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  8268. begin
  8269. taicpu(p).loadoper(0,taicpu(hp1).oper[1]^);
  8270. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  8271. DebugMsg(SPeepholeOptimization + 'PXorMovapd2PXor done',p);
  8272. RemoveInstruction(hp1);
  8273. result:=true;
  8274. end;
  8275. end;
  8276. end;
  8277. function TX86AsmOptimizer.OptPass1VPXor(var p: tai): boolean;
  8278. var
  8279. hp1: tai;
  8280. begin
  8281. {
  8282. remove the second (v)pxor from
  8283. (v)pxor reg,reg
  8284. ...
  8285. (v)pxor reg,reg
  8286. }
  8287. Result:=false;
  8288. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^,taicpu(p).oper[2]^) and
  8289. MatchOpType(taicpu(p),top_reg,top_reg,top_reg) then
  8290. begin
  8291. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  8292. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  8293. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  8294. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^,taicpu(hp1).oper[2]^) then
  8295. begin
  8296. DebugMsg(SPeepholeOptimization + 'VPXorVPXor2VPXor done',hp1);
  8297. RemoveInstruction(hp1);
  8298. Result:=true;
  8299. Exit;
  8300. end;
  8301. {$ifdef x86_64}
  8302. {
  8303. replace
  8304. vpxor reg1,reg1,reg1
  8305. vmov reg,mem
  8306. by
  8307. movq $0,mem
  8308. }
  8309. if GetNextInstruction(p,hp1) and
  8310. MatchInstruction(hp1,A_VMOVSD,[]) and
  8311. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  8312. MatchOpType(taicpu(hp1),top_reg,top_ref) then
  8313. begin
  8314. TransferUsedRegs(TmpUsedRegs);
  8315. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  8316. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  8317. begin
  8318. taicpu(hp1).loadconst(0,0);
  8319. taicpu(hp1).opcode:=A_MOV;
  8320. taicpu(hp1).opsize:=S_Q;
  8321. DebugMsg(SPeepholeOptimization + 'VPXorVMov2Mov done',p);
  8322. RemoveCurrentP(p);
  8323. result:=true;
  8324. Exit;
  8325. end;
  8326. end;
  8327. {$endif x86_64}
  8328. end
  8329. {
  8330. replace
  8331. vpxor reg1,reg1,reg2
  8332. by
  8333. vpxor reg2,reg2,reg2
  8334. to avoid unncessary data dependencies
  8335. }
  8336. else if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  8337. MatchOpType(taicpu(p),top_reg,top_reg,top_reg) then
  8338. begin
  8339. DebugMsg(SPeepholeOptimization + 'VPXor2VPXor done',p);
  8340. { avoid unncessary data dependency }
  8341. taicpu(p).loadreg(0,taicpu(p).oper[2]^.reg);
  8342. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  8343. result:=true;
  8344. exit;
  8345. end;
  8346. Result:=OptPass1VOP(p);
  8347. end;
  8348. function TX86AsmOptimizer.OptPass1Imul(var p: tai): boolean;
  8349. var
  8350. hp1 : tai;
  8351. begin
  8352. result:=false;
  8353. { replace
  8354. IMul const,%mreg1,%mreg2
  8355. Mov %reg2,%mreg3
  8356. dealloc %mreg3
  8357. by
  8358. Imul const,%mreg1,%mreg23
  8359. }
  8360. if (taicpu(p).ops=3) and
  8361. GetNextInstruction(p,hp1) and
  8362. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  8363. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  8364. (taicpu(hp1).oper[1]^.typ=top_reg) then
  8365. begin
  8366. TransferUsedRegs(TmpUsedRegs);
  8367. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  8368. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  8369. begin
  8370. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  8371. DebugMsg(SPeepholeOptimization + 'ImulMov2Imul done',p);
  8372. RemoveInstruction(hp1);
  8373. result:=true;
  8374. end;
  8375. end;
  8376. end;
  8377. function TX86AsmOptimizer.OptPass1SHXX(var p: tai): boolean;
  8378. var
  8379. hp1 : tai;
  8380. begin
  8381. result:=false;
  8382. { replace
  8383. IMul %reg0,%reg1,%reg2
  8384. Mov %reg2,%reg3
  8385. dealloc %reg2
  8386. by
  8387. Imul %reg0,%reg1,%reg3
  8388. }
  8389. if GetNextInstruction(p,hp1) and
  8390. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  8391. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  8392. (taicpu(hp1).oper[1]^.typ=top_reg) then
  8393. begin
  8394. TransferUsedRegs(TmpUsedRegs);
  8395. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  8396. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  8397. begin
  8398. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  8399. DebugMsg(SPeepholeOptimization + 'SHXXMov2SHXX done',p);
  8400. RemoveInstruction(hp1);
  8401. result:=true;
  8402. end;
  8403. end;
  8404. end;
  8405. function TX86AsmOptimizer.OptPass1_V_Cvtss2sd(var p: tai): boolean;
  8406. var
  8407. hp1: tai;
  8408. begin
  8409. Result:=false;
  8410. { get rid of
  8411. (v)cvtss2sd reg0,<reg1,>reg2
  8412. (v)cvtss2sd reg2,<reg2,>reg0
  8413. }
  8414. if GetNextInstruction(p,hp1) and
  8415. (((taicpu(p).opcode=A_CVTSS2SD) and MatchInstruction(hp1,A_CVTSD2SS,[taicpu(p).opsize]) and
  8416. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^)) or
  8417. ((taicpu(p).opcode=A_VCVTSS2SD) and MatchInstruction(hp1,A_VCVTSD2SS,[taicpu(p).opsize]) and
  8418. MatchOpType(taicpu(p),top_reg,top_reg,top_reg) and
  8419. MatchOpType(taicpu(hp1),top_reg,top_reg,top_reg) and
  8420. (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  8421. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  8422. (getsupreg(taicpu(p).oper[2]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg))
  8423. )
  8424. ) then
  8425. begin
  8426. if ((taicpu(p).opcode=A_CVTSS2SD) and (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  8427. ((taicpu(p).opcode=A_VCVTSS2SD) and (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[2]^.reg))) then
  8428. begin
  8429. DebugMsg(SPeepholeOptimization + '(V)Cvtss2CvtSd(V)Cvtsd2ss2Nop done',p);
  8430. RemoveCurrentP(p);
  8431. RemoveInstruction(hp1);
  8432. end
  8433. else
  8434. begin
  8435. DebugMsg(SPeepholeOptimization + '(V)Cvtss2CvtSd(V)Cvtsd2ss2Vmovaps done',p);
  8436. if taicpu(hp1).opcode=A_CVTSD2SS then
  8437. begin
  8438. taicpu(p).loadreg(1,taicpu(hp1).oper[1]^.reg);
  8439. taicpu(p).opcode:=A_MOVAPS;
  8440. end
  8441. else
  8442. begin
  8443. taicpu(p).loadreg(1,taicpu(hp1).oper[2]^.reg);
  8444. taicpu(p).opcode:=A_VMOVAPS;
  8445. end;
  8446. taicpu(p).ops:=2;
  8447. RemoveInstruction(hp1);
  8448. end;
  8449. Result:=true;
  8450. Exit;
  8451. end;
  8452. end;
  8453. function TX86AsmOptimizer.OptPass1Jcc(var p : tai) : boolean;
  8454. var
  8455. hp1, hp2, hp3, hp4, hp5: tai;
  8456. ThisReg: TRegister;
  8457. begin
  8458. Result := False;
  8459. if not GetNextInstruction(p,hp1) then
  8460. Exit;
  8461. {
  8462. convert
  8463. j<c> .L1
  8464. mov 1,reg
  8465. jmp .L2
  8466. .L1
  8467. mov 0,reg
  8468. .L2
  8469. into
  8470. mov 0,reg
  8471. set<not(c)> reg
  8472. take care of alignment and that the mov 0,reg is not converted into a xor as this
  8473. would destroy the flag contents
  8474. Use MOVZX if size is preferred, since while mov 0,reg is bigger, it can be
  8475. executed at the same time as a previous comparison.
  8476. set<not(c)> reg
  8477. movzx reg, reg
  8478. }
  8479. if MatchInstruction(hp1,A_MOV,[]) and
  8480. (taicpu(hp1).oper[0]^.typ = top_const) and
  8481. (
  8482. (
  8483. (taicpu(hp1).oper[1]^.typ = top_reg)
  8484. {$ifdef i386}
  8485. { Under i386, ESI, EDI, EBP and ESP
  8486. don't have an 8-bit representation }
  8487. and not (getsupreg(taicpu(hp1).oper[1]^.reg) in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  8488. {$endif i386}
  8489. ) or (
  8490. {$ifdef i386}
  8491. (taicpu(hp1).oper[1]^.typ <> top_reg) and
  8492. {$endif i386}
  8493. (taicpu(hp1).opsize = S_B)
  8494. )
  8495. ) and
  8496. GetNextInstruction(hp1,hp2) and
  8497. MatchInstruction(hp2,A_JMP,[]) and (taicpu(hp2).oper[0]^.ref^.refaddr=addr_full) and
  8498. GetNextInstruction(hp2,hp3) and
  8499. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp3) and
  8500. GetNextInstruction(hp3,hp4) and
  8501. MatchInstruction(hp4,A_MOV,[taicpu(hp1).opsize]) and
  8502. (taicpu(hp4).oper[0]^.typ = top_const) and
  8503. (
  8504. ((taicpu(hp1).oper[0]^.val = 0) and (taicpu(hp4).oper[0]^.val = 1)) or
  8505. ((taicpu(hp1).oper[0]^.val = 1) and (taicpu(hp4).oper[0]^.val = 0))
  8506. ) and
  8507. MatchOperand(taicpu(hp1).oper[1]^,taicpu(hp4).oper[1]^) and
  8508. GetNextInstruction(hp4,hp5) and
  8509. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol), hp5) then
  8510. begin
  8511. if (taicpu(hp1).oper[0]^.val = 1) and (taicpu(hp4).oper[0]^.val = 0) then
  8512. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  8513. tai_label(hp3).labsym.DecRefs;
  8514. { If this isn't the only reference to the middle label, we can
  8515. still make a saving - only that the first jump and everything
  8516. that follows will remain. }
  8517. if (tai_label(hp3).labsym.getrefs = 0) then
  8518. begin
  8519. if (taicpu(hp1).oper[0]^.val = 1) and (taicpu(hp4).oper[0]^.val = 0) then
  8520. DebugMsg(SPeepholeOptimization + 'J(c)Mov1JmpMov0 -> Set(~c)',p)
  8521. else
  8522. DebugMsg(SPeepholeOptimization + 'J(c)Mov0JmpMov1 -> Set(c)',p);
  8523. { remove jump, first label and second MOV (also catching any aligns) }
  8524. repeat
  8525. if not GetNextInstruction(hp2, hp3) then
  8526. InternalError(2021040810);
  8527. RemoveInstruction(hp2);
  8528. hp2 := hp3;
  8529. until hp2 = hp5;
  8530. { Don't decrement reference count before the removal loop
  8531. above, otherwise GetNextInstruction won't stop on the
  8532. the label }
  8533. tai_label(hp5).labsym.DecRefs;
  8534. end
  8535. else
  8536. begin
  8537. if (taicpu(hp1).oper[0]^.val = 1) and (taicpu(hp4).oper[0]^.val = 0) then
  8538. DebugMsg(SPeepholeOptimization + 'J(c)Mov1JmpMov0 -> Set(~c) (partial)',p)
  8539. else
  8540. DebugMsg(SPeepholeOptimization + 'J(c)Mov0JmpMov1 -> Set(c) (partial)',p);
  8541. end;
  8542. taicpu(p).opcode:=A_SETcc;
  8543. taicpu(p).opsize:=S_B;
  8544. taicpu(p).is_jmp:=False;
  8545. if taicpu(hp1).opsize=S_B then
  8546. begin
  8547. taicpu(p).loadoper(0, taicpu(hp1).oper[1]^);
  8548. if taicpu(hp1).oper[1]^.typ = top_reg then
  8549. AllocRegBetween(taicpu(hp1).oper[1]^.reg, p, hp2, UsedRegs);
  8550. RemoveInstruction(hp1);
  8551. end
  8552. else
  8553. begin
  8554. { Will be a register because the size can't be S_B otherwise }
  8555. ThisReg := newreg(R_INTREGISTER,getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBL);
  8556. taicpu(p).loadreg(0, ThisReg);
  8557. AllocRegBetween(ThisReg, p, hp2, UsedRegs);
  8558. if (cs_opt_size in current_settings.optimizerswitches) and IsMOVZXAcceptable then
  8559. begin
  8560. case taicpu(hp1).opsize of
  8561. S_W:
  8562. taicpu(hp1).opsize := S_BW;
  8563. S_L:
  8564. taicpu(hp1).opsize := S_BL;
  8565. {$ifdef x86_64}
  8566. S_Q:
  8567. begin
  8568. taicpu(hp1).opsize := S_BL;
  8569. { Change the destination register to 32-bit }
  8570. taicpu(hp1).loadreg(1, newreg(R_INTREGISTER,getsupreg(ThisReg), R_SUBD));
  8571. end;
  8572. {$endif x86_64}
  8573. else
  8574. InternalError(2021040820);
  8575. end;
  8576. taicpu(hp1).opcode := A_MOVZX;
  8577. taicpu(hp1).loadreg(0, ThisReg);
  8578. end
  8579. else
  8580. begin
  8581. AllocRegBetween(NR_FLAGS,p,hp1,UsedRegs);
  8582. { hp1 is already a MOV instruction with the correct register }
  8583. taicpu(hp1).loadconst(0, 0);
  8584. { Inserting it right before p will guarantee that the flags are also tracked }
  8585. asml.Remove(hp1);
  8586. asml.InsertBefore(hp1, p);
  8587. end;
  8588. end;
  8589. Result:=true;
  8590. exit;
  8591. end
  8592. else if MatchInstruction(hp1, A_CLC, A_STC, []) then
  8593. Result := TryJccStcClcOpt(p, hp1)
  8594. else if (hp1.typ = ait_label) then
  8595. Result := DoSETccLblRETOpt(p, tai_label(hp1));
  8596. end;
  8597. function TX86AsmOptimizer.OptPass1VMOVDQ(var p: tai): Boolean;
  8598. var
  8599. hp1, hp2, hp3: tai;
  8600. SourceRef, TargetRef: TReference;
  8601. CurrentReg: TRegister;
  8602. begin
  8603. { VMOVDQU/CMOVDQA shouldn't have even been generated }
  8604. if not UseAVX then
  8605. InternalError(2021100501);
  8606. Result := False;
  8607. { Look for the following to simplify:
  8608. vmovdqa/u x(mem1), %xmmreg
  8609. vmovdqa/u %xmmreg, y(mem2)
  8610. vmovdqa/u x+16(mem1), %xmmreg
  8611. vmovdqa/u %xmmreg, y+16(mem2)
  8612. Change to:
  8613. vmovdqa/u x(mem1), %ymmreg
  8614. vmovdqa/u %ymmreg, y(mem2)
  8615. vpxor %ymmreg, %ymmreg, %ymmreg
  8616. ( The VPXOR instruction is to zero the upper half, thus removing the
  8617. need to call the potentially expensive VZEROUPPER instruction. Other
  8618. peephole optimisations can remove VPXOR if it's unnecessary )
  8619. }
  8620. TransferUsedRegs(TmpUsedRegs);
  8621. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  8622. { NOTE: In the optimisations below, if the references dictate that an
  8623. aligned move is possible (i.e. VMOVDQA), the existing instructions
  8624. should already be VMOVDQA because if (x mod 32) = 0, then (x mod 16) = 0 }
  8625. if (taicpu(p).opsize = S_XMM) and
  8626. MatchOpType(taicpu(p), top_ref, top_reg) and
  8627. GetNextInstruction(p, hp1) and
  8628. MatchInstruction(hp1, A_VMOVDQA, A_VMOVDQU, [S_XMM]) and
  8629. MatchOpType(taicpu(hp1), top_reg, top_ref) and
  8630. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) then
  8631. begin
  8632. SourceRef := taicpu(p).oper[0]^.ref^;
  8633. TargetRef := taicpu(hp1).oper[1]^.ref^;
  8634. if GetNextInstruction(hp1, hp2) and
  8635. MatchInstruction(hp2, A_VMOVDQA, A_VMOVDQU, [S_XMM]) and
  8636. MatchOpType(taicpu(hp2), top_ref, top_reg) then
  8637. begin
  8638. { Delay calling GetNextInstruction(hp2, hp3) for as long as possible }
  8639. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  8640. Inc(SourceRef.offset, 16);
  8641. { Reuse the register in the first block move }
  8642. CurrentReg := newreg(R_MMREGISTER, getsupreg(taicpu(p).oper[1]^.reg), R_SUBMMY);
  8643. if RefsEqual(SourceRef, taicpu(hp2).oper[0]^.ref^) and
  8644. not RefsMightOverlap(taicpu(p).oper[0]^.ref^, TargetRef, 32) then
  8645. begin
  8646. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  8647. Inc(TargetRef.offset, 16);
  8648. if GetNextInstruction(hp2, hp3) and
  8649. MatchInstruction(hp3, A_VMOVDQA, A_VMOVDQU, [S_XMM]) and
  8650. MatchOpType(taicpu(hp3), top_reg, top_ref) and
  8651. (taicpu(hp2).oper[1]^.reg = taicpu(hp3).oper[0]^.reg) and
  8652. RefsEqual(TargetRef, taicpu(hp3).oper[1]^.ref^) and
  8653. not RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp3, TmpUsedRegs) then
  8654. begin
  8655. { Update the register tracking to the new size }
  8656. AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
  8657. { Remember that the offsets are 16 ahead }
  8658. { Switch to unaligned if the memory isn't on a 32-byte boundary }
  8659. if not (
  8660. ((SourceRef.offset mod 32) = 16) and
  8661. (SourceRef.alignment >= 32) and ((SourceRef.alignment mod 32) = 0)
  8662. ) then
  8663. taicpu(p).opcode := A_VMOVDQU;
  8664. taicpu(p).opsize := S_YMM;
  8665. taicpu(p).oper[1]^.reg := CurrentReg;
  8666. if not (
  8667. ((TargetRef.offset mod 32) = 16) and
  8668. (TargetRef.alignment >= 32) and ((TargetRef.alignment mod 32) = 0)
  8669. ) then
  8670. taicpu(hp1).opcode := A_VMOVDQU;
  8671. taicpu(hp1).opsize := S_YMM;
  8672. taicpu(hp1).oper[0]^.reg := CurrentReg;
  8673. DebugMsg(SPeepholeOptimization + 'Used ' + debug_regname(CurrentReg) + ' to merge a pair of memory moves (VmovdqxVmovdqxVmovdqxVmovdqx2VmovdqyVmovdqy 1)', p);
  8674. { If pi_uses_ymm is set, VZEROUPPER is present to do this for us }
  8675. if (pi_uses_ymm in current_procinfo.flags) then
  8676. RemoveInstruction(hp2)
  8677. else
  8678. begin
  8679. taicpu(hp2).opcode := A_VPXOR;
  8680. taicpu(hp2).opsize := S_YMM;
  8681. taicpu(hp2).loadreg(0, CurrentReg);
  8682. taicpu(hp2).loadreg(1, CurrentReg);
  8683. taicpu(hp2).loadreg(2, CurrentReg);
  8684. taicpu(hp2).ops := 3;
  8685. end;
  8686. RemoveInstruction(hp3);
  8687. Result := True;
  8688. Exit;
  8689. end;
  8690. end
  8691. else
  8692. begin
  8693. { See if the next references are 16 less rather than 16 greater }
  8694. Dec(SourceRef.offset, 32); { -16 the other way }
  8695. if RefsEqual(SourceRef, taicpu(hp2).oper[0]^.ref^) then
  8696. begin
  8697. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  8698. Dec(TargetRef.offset, 16); { Only 16, not 32, as it wasn't incremented unlike SourceRef }
  8699. if not RefsMightOverlap(SourceRef, TargetRef, 32) and
  8700. GetNextInstruction(hp2, hp3) and
  8701. MatchInstruction(hp3, A_MOV, [taicpu(p).opsize]) and
  8702. MatchOpType(taicpu(hp3), top_reg, top_ref) and
  8703. (taicpu(hp2).oper[1]^.reg = taicpu(hp3).oper[0]^.reg) and
  8704. RefsEqual(TargetRef, taicpu(hp3).oper[1]^.ref^) and
  8705. not RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp3, TmpUsedRegs) then
  8706. begin
  8707. { Update the register tracking to the new size }
  8708. AllocRegBetween(CurrentReg, hp2, hp3, UsedRegs);
  8709. { hp2 and hp3 are the starting offsets, so mod = 0 this time }
  8710. { Switch to unaligned if the memory isn't on a 32-byte boundary }
  8711. if not(
  8712. ((SourceRef.offset mod 32) = 0) and
  8713. (SourceRef.alignment >= 32) and ((SourceRef.alignment mod 32) = 0)
  8714. ) then
  8715. taicpu(hp2).opcode := A_VMOVDQU;
  8716. taicpu(hp2).opsize := S_YMM;
  8717. taicpu(hp2).oper[1]^.reg := CurrentReg;
  8718. if not (
  8719. ((TargetRef.offset mod 32) = 0) and
  8720. (TargetRef.alignment >= 32) and ((TargetRef.alignment mod 32) = 0)
  8721. ) then
  8722. taicpu(hp3).opcode := A_VMOVDQU;
  8723. taicpu(hp3).opsize := S_YMM;
  8724. taicpu(hp3).oper[0]^.reg := CurrentReg;
  8725. DebugMsg(SPeepholeOptimization + 'Used ' + debug_regname(CurrentReg) + ' to merge a pair of memory moves (VmovdqxVmovdqxVmovdqxVmovdqx2VmovdqyVmovdqy 2)', p);
  8726. { If pi_uses_ymm is set, VZEROUPPER is present to do this for us }
  8727. if (pi_uses_ymm in current_procinfo.flags) then
  8728. RemoveInstruction(hp1)
  8729. else
  8730. begin
  8731. taicpu(hp1).opcode := A_VPXOR;
  8732. taicpu(hp1).opsize := S_YMM;
  8733. taicpu(hp1).loadreg(0, CurrentReg);
  8734. taicpu(hp1).loadreg(1, CurrentReg);
  8735. taicpu(hp1).loadreg(2, CurrentReg);
  8736. taicpu(hp1).ops := 3;
  8737. Asml.Remove(hp1);
  8738. Asml.InsertAfter(hp1, hp3); { Register deallocations will be after hp3 }
  8739. end;
  8740. RemoveCurrentP(p, hp2);
  8741. Result := True;
  8742. Exit;
  8743. end;
  8744. end;
  8745. end;
  8746. end;
  8747. end;
  8748. end;
  8749. function TX86AsmOptimizer.CheckJumpMovTransferOpt(var p: tai; hp1: tai; LoopCount: Integer; out Count: Integer): Boolean;
  8750. var
  8751. hp2, hp3, first_assignment: tai;
  8752. IncCount, OperIdx: Integer;
  8753. OrigLabel: TAsmLabel;
  8754. begin
  8755. Count := 0;
  8756. Result := False;
  8757. first_assignment := nil;
  8758. if (LoopCount >= 20) then
  8759. begin
  8760. { Guard against infinite loops }
  8761. Exit;
  8762. end;
  8763. if (taicpu(p).oper[0]^.typ <> top_ref) or
  8764. (taicpu(p).oper[0]^.ref^.refaddr <> addr_full) or
  8765. (taicpu(p).oper[0]^.ref^.base <> NR_NO) or
  8766. (taicpu(p).oper[0]^.ref^.index <> NR_NO) or
  8767. not (taicpu(p).oper[0]^.ref^.symbol is TAsmLabel) then
  8768. Exit;
  8769. OrigLabel := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  8770. {
  8771. change
  8772. jmp .L1
  8773. ...
  8774. .L1:
  8775. mov ##, ## ( multiple movs possible )
  8776. jmp/ret
  8777. into
  8778. mov ##, ##
  8779. jmp/ret
  8780. }
  8781. if not Assigned(hp1) then
  8782. begin
  8783. hp1 := GetLabelWithSym(OrigLabel);
  8784. if not Assigned(hp1) or not SkipLabels(hp1, hp1) then
  8785. Exit;
  8786. end;
  8787. hp2 := hp1;
  8788. while Assigned(hp2) do
  8789. begin
  8790. if Assigned(hp2) and (hp2.typ = ait_label) then
  8791. SkipLabels(hp2,hp2);
  8792. if not Assigned(hp2) or (hp2.typ <> ait_instruction) then
  8793. Break;
  8794. case taicpu(hp2).opcode of
  8795. A_MOVSD:
  8796. begin
  8797. if taicpu(hp2).ops = 0 then
  8798. { Wrong MOVSD }
  8799. Break;
  8800. Inc(Count);
  8801. if Count >= 5 then
  8802. { Too many to be worthwhile }
  8803. Break;
  8804. GetNextInstruction(hp2, hp2);
  8805. Continue;
  8806. end;
  8807. A_MOV,
  8808. A_MOVD,
  8809. A_MOVQ,
  8810. A_MOVSX,
  8811. {$ifdef x86_64}
  8812. A_MOVSXD,
  8813. {$endif x86_64}
  8814. A_MOVZX,
  8815. A_MOVAPS,
  8816. A_MOVUPS,
  8817. A_MOVSS,
  8818. A_MOVAPD,
  8819. A_MOVUPD,
  8820. A_MOVDQA,
  8821. A_MOVDQU,
  8822. A_VMOVSS,
  8823. A_VMOVAPS,
  8824. A_VMOVUPS,
  8825. A_VMOVSD,
  8826. A_VMOVAPD,
  8827. A_VMOVUPD,
  8828. A_VMOVDQA,
  8829. A_VMOVDQU:
  8830. begin
  8831. Inc(Count);
  8832. if Count >= 5 then
  8833. { Too many to be worthwhile }
  8834. Break;
  8835. GetNextInstruction(hp2, hp2);
  8836. Continue;
  8837. end;
  8838. A_JMP:
  8839. begin
  8840. { Guard against infinite loops }
  8841. if taicpu(hp2).oper[0]^.ref^.symbol = OrigLabel then
  8842. Exit;
  8843. { Analyse this jump first in case it also duplicates assignments }
  8844. if CheckJumpMovTransferOpt(hp2, nil, LoopCount + 1, IncCount) then
  8845. begin
  8846. { Something did change! }
  8847. Result := True;
  8848. Inc(Count, IncCount);
  8849. if Count >= 5 then
  8850. begin
  8851. { Too many to be worthwhile }
  8852. Exit;
  8853. end;
  8854. if MatchInstruction(hp2, [A_JMP, A_RET], []) then
  8855. Break;
  8856. end;
  8857. Result := True;
  8858. Break;
  8859. end;
  8860. A_RET:
  8861. begin
  8862. Result := True;
  8863. Break;
  8864. end;
  8865. else
  8866. Break;
  8867. end;
  8868. end;
  8869. if Result then
  8870. begin
  8871. { A count of zero can happen when CheckJumpMovTransferOpt is called recursively }
  8872. if Count = 0 then
  8873. begin
  8874. Result := False;
  8875. Exit;
  8876. end;
  8877. TransferUsedRegs(TmpUsedRegs);
  8878. hp3 := p;
  8879. DebugMsg(SPeepholeOptimization + 'Duplicated ' + debug_tostr(Count) + ' assignment(s) and redirected jump', p);
  8880. while True do
  8881. begin
  8882. if Assigned(hp1) and (hp1.typ = ait_label) then
  8883. SkipLabels(hp1,hp1);
  8884. case hp1.typ of
  8885. ait_regalloc:
  8886. if tai_regalloc(hp1).ratype = ra_dealloc then
  8887. begin
  8888. { Duplicate the register deallocation... }
  8889. hp3:=tai(hp1.getcopy);
  8890. if first_assignment = nil then
  8891. first_assignment := hp3;
  8892. asml.InsertBefore(hp3, p);
  8893. { ... but also reallocate it after the jump }
  8894. hp3:=tai(hp1.getcopy);
  8895. tai_regalloc(hp3).ratype := ra_alloc;
  8896. asml.InsertAfter(hp3, p);
  8897. end;
  8898. ait_instruction:
  8899. case taicpu(hp1).opcode of
  8900. A_JMP:
  8901. begin
  8902. { Change the original jump to the new destination }
  8903. OrigLabel.decrefs;
  8904. taicpu(hp1).oper[0]^.ref^.symbol.increfs;
  8905. taicpu(p).loadref(0, taicpu(hp1).oper[0]^.ref^);
  8906. { Set p to the first duplicated assignment so it can get optimised if needs be }
  8907. if not Assigned(first_assignment) then
  8908. InternalError(2021040810)
  8909. else
  8910. p := first_assignment;
  8911. Exit;
  8912. end;
  8913. A_RET:
  8914. begin
  8915. { Now change the jump into a RET instruction }
  8916. ConvertJumpToRET(p, hp1);
  8917. { Set p to the first duplicated assignment so it can get optimised if needs be }
  8918. if not Assigned(first_assignment) then
  8919. InternalError(2021040811)
  8920. else
  8921. p := first_assignment;
  8922. Exit;
  8923. end;
  8924. else
  8925. begin
  8926. { Duplicate the MOV instruction }
  8927. hp3:=tai(hp1.getcopy);
  8928. if first_assignment = nil then
  8929. first_assignment := hp3;
  8930. asml.InsertBefore(hp3, p);
  8931. { Make sure the compiler knows about any final registers written here }
  8932. for OperIdx := 0 to taicpu(hp3).ops - 1 do
  8933. with taicpu(hp3).oper[OperIdx]^ do
  8934. begin
  8935. case typ of
  8936. top_ref:
  8937. begin
  8938. if (ref^.base <> NR_NO) and
  8939. (getsupreg(ref^.base) <> RS_STACK_POINTER_REG) and
  8940. (
  8941. (getsupreg(ref^.base) <> RS_FRAME_POINTER_REG) or
  8942. (
  8943. { Allow the frame pointer if it's not being used by the procedure as such }
  8944. Assigned(current_procinfo) and
  8945. (current_procinfo.framepointer <> NR_FRAME_POINTER_REG)
  8946. )
  8947. )
  8948. {$ifdef x86_64} and (ref^.base <> NR_RIP) {$endif x86_64}
  8949. then
  8950. begin
  8951. AllocRegBetween(ref^.base, hp3, p, TmpUsedRegs);
  8952. if not Assigned(first_assignment) then
  8953. IncludeRegInUsedRegs(ref^.base, UsedRegs);
  8954. end;
  8955. if (ref^.index <> NR_NO) and
  8956. (getsupreg(ref^.index) <> RS_STACK_POINTER_REG) and
  8957. (
  8958. (getsupreg(ref^.index) <> RS_FRAME_POINTER_REG) or
  8959. (
  8960. { Allow the frame pointer if it's not being used by the procedure as such }
  8961. Assigned(current_procinfo) and
  8962. (current_procinfo.framepointer <> NR_FRAME_POINTER_REG)
  8963. )
  8964. )
  8965. {$ifdef x86_64} and (ref^.index <> NR_RIP) {$endif x86_64} and
  8966. (ref^.index <> ref^.base) then
  8967. begin
  8968. AllocRegBetween(ref^.index, hp3, p, TmpUsedRegs);
  8969. if not Assigned(first_assignment) then
  8970. IncludeRegInUsedRegs(ref^.index, UsedRegs);
  8971. end;
  8972. end;
  8973. top_reg:
  8974. begin
  8975. AllocRegBetween(reg, hp3, p, TmpUsedRegs);
  8976. if not Assigned(first_assignment) then
  8977. IncludeRegInUsedRegs(reg, UsedRegs);
  8978. end;
  8979. else
  8980. ;
  8981. end;
  8982. end;
  8983. end;
  8984. end;
  8985. else
  8986. InternalError(2021040720);
  8987. end;
  8988. if not GetNextInstruction(hp1, hp1, [ait_regalloc]) then
  8989. { Should have dropped out earlier }
  8990. InternalError(2021040710);
  8991. end;
  8992. end;
  8993. end;
  8994. const
  8995. WriteOp: array[0..3] of set of TInsChange = (
  8996. [Ch_Wop1, Ch_RWop1, Ch_Mop1],
  8997. [Ch_Wop2, Ch_RWop2, Ch_Mop2],
  8998. [Ch_Wop3, Ch_RWop3, Ch_Mop3],
  8999. [Ch_Wop4, Ch_RWop4, Ch_Mop4]);
  9000. RegWriteFlags: array[0..7] of set of TInsChange = (
  9001. { The order is important: EAX, ECX, EDX, EBX, ESI, EDI, EBP, ESP }
  9002. [Ch_WEAX, Ch_RWEAX, Ch_MEAX{$ifdef x86_64}, Ch_WRAX, Ch_RWRAX, Ch_MRAX{$endif x86_64}],
  9003. [Ch_WECX, Ch_RWECX, Ch_MECX{$ifdef x86_64}, Ch_WRCX, Ch_RWRCX, Ch_MRCX{$endif x86_64}],
  9004. [Ch_WEDX, Ch_RWEDX, Ch_MEDX{$ifdef x86_64}, Ch_WRDX, Ch_RWRDX, Ch_MRDX{$endif x86_64}],
  9005. [Ch_WEBX, Ch_RWEBX, Ch_MEBX{$ifdef x86_64}, Ch_WRBX, Ch_RWRBX, Ch_MRBX{$endif x86_64}],
  9006. [Ch_WESI, Ch_RWESI, Ch_MESI{$ifdef x86_64}, Ch_WRSI, Ch_RWRSI, Ch_MRSI{$endif x86_64}],
  9007. [Ch_WEDI, Ch_RWEDI, Ch_MEDI{$ifdef x86_64}, Ch_WRDI, Ch_RWRDI, Ch_MRDI{$endif x86_64}],
  9008. [Ch_WEBP, Ch_RWEBP, Ch_MEBP{$ifdef x86_64}, Ch_WRBP, Ch_RWRBP, Ch_MRBP{$endif x86_64}],
  9009. [Ch_WESP, Ch_RWESP, Ch_MESP{$ifdef x86_64}, Ch_WRSP, Ch_RWRSP, Ch_MRSP{$endif x86_64}]);
  9010. function TX86AsmOptimizer.TrySwapMovOp(var p, hp1: tai): Boolean;
  9011. var
  9012. hp2: tai;
  9013. X: Integer;
  9014. begin
  9015. { If we have something like:
  9016. op ###,###
  9017. mov ###,###
  9018. Try to move the MOV instruction to before OP as long as OP and MOV don't
  9019. interfere in regards to what they write to.
  9020. NOTE: p must be a 2-operand instruction
  9021. }
  9022. Result := False;
  9023. if (hp1.typ <> ait_instruction) or
  9024. taicpu(hp1).is_jmp or
  9025. RegInInstruction(NR_DEFAULTFLAGS, hp1) then
  9026. Exit;
  9027. { NOP is a pipeline fence, likely marking the beginning of the function
  9028. epilogue, so drop out. Similarly, drop out if POP or RET are
  9029. encountered }
  9030. if MatchInstruction(hp1, A_NOP, A_POP, A_RET, []) then
  9031. Exit;
  9032. if (taicpu(hp1).opcode = A_MOVSD) and
  9033. (taicpu(hp1).ops = 0) then
  9034. { Wrong MOVSD }
  9035. Exit;
  9036. { Check for writes to specific registers first }
  9037. { EAX, ECX, EDX, EBX, ESI, EDI, EBP, ESP in that order }
  9038. for X := 0 to 7 do
  9039. if (RegWriteFlags[X] * InsProp[taicpu(hp1).opcode].Ch <> [])
  9040. and RegInInstruction(newreg(R_INTREGISTER, TSuperRegister(X), R_SUBWHOLE), p) then
  9041. Exit;
  9042. for X := 0 to taicpu(hp1).ops - 1 do
  9043. begin
  9044. { Check to see if this operand writes to something }
  9045. if ((WriteOp[X] * InsProp[taicpu(hp1).opcode].Ch) <> []) and
  9046. { And matches something in the CMP/TEST instruction }
  9047. (
  9048. MatchOperand(taicpu(hp1).oper[X]^, taicpu(p).oper[0]^) or
  9049. MatchOperand(taicpu(hp1).oper[X]^, taicpu(p).oper[1]^) or
  9050. (
  9051. { If it's a register, make sure the register written to doesn't
  9052. appear in the cmp instruction as part of a reference }
  9053. (taicpu(hp1).oper[X]^.typ = top_reg) and
  9054. RegInInstruction(taicpu(hp1).oper[X]^.reg, p)
  9055. )
  9056. ) then
  9057. Exit;
  9058. end;
  9059. { Check p to make sure it doesn't write to something that affects hp1 }
  9060. { Check for writes to specific registers first }
  9061. { EAX, ECX, EDX, EBX, ESI, EDI, EBP, ESP in that order }
  9062. for X := 0 to 7 do
  9063. if (RegWriteFlags[X] * InsProp[taicpu(p).opcode].Ch <> [])
  9064. and RegInInstruction(newreg(R_INTREGISTER, TSuperRegister(X), R_SUBWHOLE), hp1) then
  9065. Exit;
  9066. for X := 0 to taicpu(p).ops - 1 do
  9067. begin
  9068. { Check to see if this operand writes to something }
  9069. if ((WriteOp[X] * InsProp[taicpu(p).opcode].Ch) <> []) and
  9070. { And matches something in hp1 }
  9071. (taicpu(p).oper[X]^.typ = top_reg) and
  9072. RegInInstruction(taicpu(p).oper[X]^.reg, hp1) then
  9073. Exit;
  9074. end;
  9075. { The instruction can be safely moved }
  9076. asml.Remove(hp1);
  9077. { Try to insert after the last instructions where the FLAGS register is not
  9078. yet in use, so "mov $0,%reg" can be optimised into "xor %reg,%reg" later }
  9079. if SetAndTest(FindRegAllocBackward(NR_DEFAULTFLAGS, tai(p.Previous)), hp2) then
  9080. asml.InsertBefore(hp1, hp2)
  9081. { Failing that, try to insert after the last instructions where the
  9082. FLAGS register is not yet in use }
  9083. else if GetLastInstruction(p, hp2) and
  9084. (
  9085. (hp2.typ <> ait_instruction) or
  9086. { Don't insert after an instruction that uses the flags when p doesn't use them }
  9087. RegInInstruction(NR_DEFAULTFLAGS, p) or
  9088. not RegInInstruction(NR_DEFAULTFLAGS, hp2)
  9089. ) then
  9090. asml.InsertAfter(hp1, hp2)
  9091. else
  9092. { Note, if p.Previous is nil (even if it should logically never be the
  9093. case), FindRegAllocBackward immediately exits with False and so we
  9094. safely land here (we can't just pass p because FindRegAllocBackward
  9095. immediately exits on an instruction). [Kit] }
  9096. asml.InsertBefore(hp1, p);
  9097. DebugMsg(SPeepholeOptimization + 'Swapped ' + debug_op2str(taicpu(p).opcode) + ' and ' + debug_op2str(taicpu(hp1).opcode) + ' instructions to improve optimisation potential', hp1);
  9098. { We can't trust UsedRegs because we're looking backwards, although we
  9099. know the registers are allocated after p at the very least, so manually
  9100. create tai_regalloc objects if needed }
  9101. for X := 0 to taicpu(hp1).ops - 1 do
  9102. case taicpu(hp1).oper[X]^.typ of
  9103. top_reg:
  9104. begin
  9105. asml.InsertBefore(tai_regalloc.alloc(taicpu(hp1).oper[X]^.reg, nil), hp1);
  9106. IncludeRegInUsedRegs(taicpu(hp1).oper[X]^.reg, UsedRegs);
  9107. AllocRegBetween(taicpu(hp1).oper[X]^.reg, hp1, p, UsedRegs);
  9108. end;
  9109. top_ref:
  9110. begin
  9111. if taicpu(hp1).oper[X]^.ref^.base <> NR_NO then
  9112. begin
  9113. asml.InsertBefore(tai_regalloc.alloc(taicpu(hp1).oper[X]^.ref^.base, nil), hp1);
  9114. IncludeRegInUsedRegs(taicpu(hp1).oper[X]^.ref^.base, UsedRegs);
  9115. AllocRegBetween(taicpu(hp1).oper[X]^.ref^.base, hp1, p, UsedRegs);
  9116. end;
  9117. if taicpu(hp1).oper[X]^.ref^.index <> NR_NO then
  9118. begin
  9119. asml.InsertBefore(tai_regalloc.alloc(taicpu(hp1).oper[X]^.ref^.index, nil), hp1);
  9120. IncludeRegInUsedRegs(taicpu(hp1).oper[X]^.ref^.index, UsedRegs);
  9121. AllocRegBetween(taicpu(hp1).oper[X]^.ref^.index, hp1, p, UsedRegs);
  9122. end;
  9123. end;
  9124. else
  9125. ;
  9126. end;
  9127. Result := True;
  9128. end;
  9129. function TX86AsmOptimizer.TrySwapMovCmp(var p, hp1: tai): Boolean;
  9130. var
  9131. hp2: tai;
  9132. X: Integer;
  9133. begin
  9134. { If we have something like:
  9135. cmp ###,%reg1
  9136. mov 0,%reg2
  9137. And no modified registers are shared, move the instruction to before
  9138. the comparison as this means it can be optimised without worrying
  9139. about the FLAGS register. (CMP/MOV is generated by
  9140. "J(c)Mov1JmpMov0 -> Set(~c)", among other things).
  9141. As long as the second instruction doesn't use the flags or one of the
  9142. registers used by CMP or TEST (also check any references that use the
  9143. registers), then it can be moved prior to the comparison.
  9144. }
  9145. Result := False;
  9146. if not TrySwapMovOp(p, hp1) then
  9147. Exit;
  9148. if taicpu(hp1).opcode = A_LEA then
  9149. { The flags will be overwritten by the CMP/TEST instruction }
  9150. ConvertLEA(taicpu(hp1));
  9151. Result := True;
  9152. { Can we move it one further back? }
  9153. if GetLastInstruction(hp1, hp2) and (hp2.typ = ait_instruction) and
  9154. { Check to see if CMP/TEST is a comparison against zero }
  9155. (
  9156. (
  9157. (taicpu(p).opcode = A_CMP) and
  9158. MatchOperand(taicpu(p).oper[0]^, 0)
  9159. ) or
  9160. (
  9161. (taicpu(p).opcode = A_TEST) and
  9162. (
  9163. OpsEqual(taicpu(p).oper[0]^, taicpu(p).oper[1]^) or
  9164. MatchOperand(taicpu(p).oper[0]^, -1)
  9165. )
  9166. )
  9167. ) and
  9168. { These instructions set the zero flag if the result is zero }
  9169. MatchInstruction(hp2, [A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_POPCNT, A_LZCNT], []) and
  9170. OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) then
  9171. { Looks like we can - if successful, this benefits PostPeepholeOptTestOr }
  9172. TrySwapMovOp(hp2, hp1);
  9173. end;
  9174. function TX86AsmOptimizer.OptPass1STCCLC(var p: tai): Boolean;
  9175. var
  9176. hp1, hp2, p_last, p_dist, hp1_dist: tai;
  9177. JumpLabel: TAsmLabel;
  9178. TmpBool: Boolean;
  9179. begin
  9180. Result := False;
  9181. { Look for:
  9182. stc/clc
  9183. j(c) .L1
  9184. ...
  9185. .L1:
  9186. set(n)cb %reg
  9187. (flags deallocated)
  9188. j(c) .L2
  9189. Change to:
  9190. mov $0/$1,%reg (depending on if the carry bit is cleared or not)
  9191. j(c) .L2
  9192. }
  9193. p_last := p;
  9194. while GetNextInstruction(p_last, hp1) and
  9195. (hp1.typ = ait_instruction) and
  9196. IsJumpToLabel(taicpu(hp1)) do
  9197. begin
  9198. if DoJumpOptimizations(hp1, TmpBool) then
  9199. { Re-evaluate from p_last. Probably could be faster, but it's guaranteed to be correct }
  9200. Continue;
  9201. JumpLabel := TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol);
  9202. if not Assigned(JumpLabel) then
  9203. InternalError(2024012801);
  9204. { Optimise the J(c); stc/clc optimisation first since this will
  9205. get missed if the main optimisation takes place }
  9206. if (taicpu(hp1).opcode = A_JCC) then
  9207. begin
  9208. if GetNextInstruction(hp1, hp2) and
  9209. MatchInstruction(hp2, A_CLC, A_STC, []) and
  9210. TryJccStcClcOpt(hp1, hp2) then
  9211. begin
  9212. Result := True;
  9213. Exit;
  9214. end;
  9215. hp2 := nil; { Suppress compiler warning }
  9216. if (taicpu(hp1).condition in [C_C, C_NC]) and
  9217. { Make sure the flags aren't used again }
  9218. SetAndTest(FindRegDealloc(NR_DEFAULTFLAGS, tai(hp1.Next)), hp2) then
  9219. begin
  9220. { clc + jc = False; clc + jnc = True; stc + jc = True; stc + jnc = False }
  9221. if ((taicpu(p).opcode = A_STC) xor (taicpu(hp1).condition = C_NC)) then
  9222. begin
  9223. if (taicpu(p).opcode = A_STC) then
  9224. DebugMsg(SPeepholeOptimization + 'STC; JC -> JMP (Deterministic jump) (StcJc2Jmp)', p)
  9225. else
  9226. DebugMsg(SPeepholeOptimization + 'CLC; JNC -> JMP (Deterministic jump) (ClcJnc2Jmp)', p);
  9227. MakeUnconditional(taicpu(hp1));
  9228. { Move the jump to after the flag deallocations }
  9229. Asml.Remove(hp1);
  9230. Asml.InsertAfter(hp1, hp2);
  9231. RemoveCurrentP(p); { hp1 may not be the immediate next instruction }
  9232. Result := True;
  9233. Exit;
  9234. end
  9235. else
  9236. begin
  9237. if (taicpu(p).opcode = A_STC) then
  9238. DebugMsg(SPeepholeOptimization + 'STC; JNC -> NOP (Deterministic jump) (StcJnc2Nop)', p)
  9239. else
  9240. DebugMsg(SPeepholeOptimization + 'CLC; JC -> NOP (Deterministic jump) (ClcJc2Nop)', p);
  9241. { In this case, the jump is deterministic in that it will never be taken }
  9242. JumpLabel.DecRefs;
  9243. RemoveInstruction(hp1);
  9244. RemoveCurrentP(p); { hp1 may not have been the immediate next instruction }
  9245. Result := True;
  9246. Exit;
  9247. end;
  9248. end;
  9249. end;
  9250. hp2 := nil; { Suppress compiler warning }
  9251. if
  9252. { Make sure the carry flag doesn't appear in the jump conditions }
  9253. not (taicpu(hp1).condition in [C_AE, C_NB, C_NC, C_B, C_C, C_NAE, C_BE, C_NA]) and
  9254. SetAndTest(getlabelwithsym(JumpLabel), hp2) and
  9255. GetNextInstruction(hp2, p_dist) and
  9256. MatchInstruction(p_dist, A_Jcc, A_SETcc, []) and
  9257. (taicpu(p_dist).condition in [C_C, C_NC]) then
  9258. begin
  9259. case taicpu(p_dist).opcode of
  9260. A_Jcc:
  9261. begin
  9262. if DoJumpOptimizations(p_dist, TmpBool) then
  9263. { Re-evaluate from p_last. Probably could be faster, but it's guaranteed to be correct }
  9264. Continue;
  9265. { clc + jc = False; clc + jnc = True; stc + jc = True; stc + jnc = False }
  9266. if ((taicpu(p).opcode = A_STC) xor (taicpu(p_dist).condition = C_NC)) then
  9267. begin
  9268. DebugMsg(SPeepholeOptimization + 'STC/CLC; JMP/Jcc; ... J(N)C -> JMP/Jcc (StcClcJ(c)2Jmp)', p);
  9269. JumpLabel.decrefs;
  9270. taicpu(hp1).loadsymbol(0, taicpu(p_dist).oper[0]^.ref^.symbol, 0);
  9271. RemoveCurrentP(p); { hp1 may not be the immediate next instruction }
  9272. Result := True;
  9273. Exit;
  9274. end
  9275. else if GetNextInstruction(p_dist, hp1_dist) and
  9276. (hp1_dist.typ = ait_label) then
  9277. begin
  9278. DebugMsg(SPeepholeOptimization + 'STC/CLC; JMP/Jcc; ... J(N)C; .Lbl -> JMP/Jcc .Lbl (StcClcJ(~c)Lbl2Jmp)', p);
  9279. JumpLabel.decrefs;
  9280. taicpu(hp1).loadsymbol(0, tai_label(hp1_dist).labsym, 0);
  9281. RemoveCurrentP(p); { hp1 may not be the immediate next instruction }
  9282. Result := True;
  9283. Exit;
  9284. end;
  9285. end;
  9286. A_SETcc:
  9287. if { Make sure the flags aren't used again }
  9288. SetAndTest(FindRegDealloc(NR_DEFAULTFLAGS, tai(p_dist.Next)), hp2) and
  9289. GetNextInstruction(hp2, hp1_dist) and
  9290. (hp1_dist.typ = ait_instruction) and
  9291. IsJumpToLabel(taicpu(hp1_dist)) and
  9292. not (taicpu(hp1_dist).condition in [C_AE, C_NB, C_NC, C_B, C_C, C_NAE, C_BE, C_NA]) and
  9293. { This works if hp1_dist or both are regular JMP instructions }
  9294. condition_in(taicpu(hp1).condition, taicpu(hp1_dist).condition) and
  9295. (
  9296. (taicpu(p_dist).oper[0]^.typ <> top_reg) or
  9297. { Make sure the register isn't still in use, otherwise it
  9298. may get corrupted (fixes #40659) }
  9299. not RegUsedBetween(taicpu(p_dist).oper[0]^.reg, p, p_dist)
  9300. ) then
  9301. begin
  9302. taicpu(p).allocate_oper(2);
  9303. taicpu(p).ops := 2;
  9304. { clc + setc = 0; clc + setnc = 1; stc + setc = 1; stc + setnc = 0 }
  9305. taicpu(p).loadconst(0, TCGInt((taicpu(p).opcode = A_STC) xor (taicpu(p_dist).condition = C_NC)));
  9306. taicpu(p).loadoper(1, taicpu(p_dist).oper[0]^);
  9307. taicpu(p).opcode := A_MOV;
  9308. taicpu(p).opsize := S_B;
  9309. if (taicpu(p_dist).oper[0]^.typ = top_reg) then
  9310. AllocRegBetween(taicpu(p_dist).oper[0]^.reg, p, hp1, UsedRegs);
  9311. DebugMsg(SPeepholeOptimization + 'STC/CLC; JMP; ... SET(N)C; JMP -> MOV; JMP (StcClcSet(c)2Mov)', p);
  9312. JumpLabel.decrefs;
  9313. taicpu(hp1).loadsymbol(0, taicpu(hp1_dist).oper[0]^.ref^.symbol, 0);
  9314. { If a flag allocation is found, try to move it to after the MOV so "mov $0,%reg" gets optimised to "xor %reg,%reg" }
  9315. if SetAndTest(FindRegAllocBackward(NR_DEFAULTFLAGS, tai(p.Previous)), hp2) and
  9316. (tai_regalloc(hp2).ratype = ra_alloc) then
  9317. begin
  9318. Asml.Remove(hp2);
  9319. Asml.InsertAfter(hp2, p);
  9320. end;
  9321. Result := True;
  9322. Exit;
  9323. end;
  9324. else
  9325. ;
  9326. end;
  9327. end;
  9328. p_last := hp1;
  9329. end;
  9330. end;
  9331. function TX86AsmOptimizer.TryJccStcClcOpt(var p, hp1: tai): Boolean;
  9332. var
  9333. hp2, hp3: tai;
  9334. TempBool: Boolean;
  9335. begin
  9336. Result := False;
  9337. {
  9338. j(c) .L1
  9339. stc/clc
  9340. .L1:
  9341. jc/jnc .L2
  9342. (Flags deallocated)
  9343. Change to:
  9344. j)c) .L1
  9345. jmp .L2
  9346. .L1:
  9347. jc/jnc .L2
  9348. Then call DoJumpOptimizations to convert to:
  9349. j(nc) .L2
  9350. .L1: (may become a dead label)
  9351. jc/jnc .L2
  9352. }
  9353. if GetNextInstruction(hp1, hp2) and
  9354. (hp2.typ = ait_label) and
  9355. (tai_label(hp2).labsym = TAsmLabel(taicpu(p).oper[0]^.ref^.symbol)) and
  9356. GetNextInstruction(hp2, hp3) and
  9357. MatchInstruction(hp3, A_Jcc, []) and
  9358. (
  9359. (
  9360. (taicpu(hp3).condition = C_C) and
  9361. (taicpu(hp1).opcode = A_STC)
  9362. ) or (
  9363. (taicpu(hp3).condition = C_NC) and
  9364. (taicpu(hp1).opcode = A_CLC)
  9365. )
  9366. ) and
  9367. { Make sure the flags aren't used again }
  9368. Assigned(FindRegDealloc(NR_DEFAULTFLAGS, tai(hp3.Next))) then
  9369. begin
  9370. taicpu(hp1).allocate_oper(1);
  9371. taicpu(hp1).ops := 1;
  9372. taicpu(hp1).loadsymbol(0, TAsmLabel(taicpu(hp3).oper[0]^.ref^.symbol), 0);
  9373. taicpu(hp1).opcode := A_JMP;
  9374. taicpu(hp1).is_jmp := True;
  9375. TempBool := True; { Prevent compiler warnings }
  9376. if DoJumpOptimizations(p, TempBool) then
  9377. Result := True
  9378. else
  9379. Include(OptsToCheck, aoc_ForceNewIteration);
  9380. end;
  9381. end;
  9382. function TX86AsmOptimizer.OptPass2STCCLC(var p: tai): Boolean;
  9383. begin
  9384. { This generally only executes under -O3 and above }
  9385. Result := (aoc_DoPass2JccOpts in OptsToCheck) and OptPass1STCCLC(p);
  9386. end;
  9387. function TX86AsmOptimizer.OptPass2CMOVcc(var p: tai): Boolean;
  9388. var
  9389. hp1, hp2: tai;
  9390. FoundComparison: Boolean;
  9391. begin
  9392. { Run the pass 1 optimisations as well, since they may have some effect
  9393. after the CMOV blocks are created in OptPass2Jcc }
  9394. Result := False;
  9395. { Result := OptPass1CMOVcc(p);
  9396. if Result then
  9397. Exit;}
  9398. { Sometimes, the CMOV optimisations in OptPass2Jcc are a bit overzealous
  9399. and make a slightly inefficent result on branching-type blocks, notably
  9400. when setting a function result then jumping to the function epilogue.
  9401. In this case, change:
  9402. cmov(c) %reg1,%reg2
  9403. j(c) @lbl
  9404. (%reg2 deallocated)
  9405. To:
  9406. mov %reg11,%reg2
  9407. j(c) @lbl
  9408. Note, we can't use GetNextInstructionUsingReg to find the conditional
  9409. jump because if it's not present, we may end up with a jump that's
  9410. completely unrelated.
  9411. }
  9412. hp1 := p;
  9413. while GetNextInstruction(hp1, hp1) and
  9414. MatchInstruction(hp1, A_MOV, A_CMOVcc, []) do { loop };
  9415. if (hp1.typ = ait_instruction) and
  9416. (taicpu(hp1).opcode = A_Jcc) and
  9417. condition_in(taicpu(hp1).condition, taicpu(p).condition) then
  9418. begin
  9419. TransferUsedRegs(TmpUsedRegs);
  9420. UpdateUsedRegsBetween(TmpUsedRegs, p, hp1);
  9421. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) or
  9422. (
  9423. { See if we can find a more distant instruction that overwrites
  9424. the destination register }
  9425. (cs_opt_level3 in current_settings.optimizerswitches) and
  9426. GetNextInstructionUsingReg(hp1, hp2, taicpu(p).oper[1]^.reg) and
  9427. RegLoadedWithNewValue(taicpu(p).oper[1]^.reg, hp2)
  9428. ) then
  9429. begin
  9430. if (taicpu(p).oper[0]^.typ = top_reg) then
  9431. begin
  9432. { Search backwards to see if the source register is set to a
  9433. constant }
  9434. FoundComparison := False;
  9435. hp1 := p;
  9436. while GetLastInstruction(hp1, hp1) and (hp1.typ = ait_instruction) do
  9437. begin
  9438. if RegModifiedByInstruction(NR_DEFAULTFLAGS, hp1) then
  9439. begin
  9440. FoundComparison := True;
  9441. Continue;
  9442. end;
  9443. { Once we find the CMP, TEST or similar instruction, we
  9444. have to stop if we find anything other than a MOV }
  9445. if FoundComparison and (taicpu(hp1).opcode <> A_MOV) then
  9446. Break;
  9447. if RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp1) then
  9448. { Destination register was modified }
  9449. Break;
  9450. if (taicpu(hp1).opcode = A_MOV) and MatchOpType(taicpu(hp1), top_const, toP_reg)
  9451. and (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) then
  9452. begin
  9453. { Found a constant! }
  9454. taicpu(p).loadconst(0, taicpu(hp1).oper[0]^.val);
  9455. if not RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, p, UsedRegs) then
  9456. { The source register is no longer in use }
  9457. RemoveInstruction(hp1);
  9458. Break;
  9459. end;
  9460. if RegModifiedByInstruction(taicpu(p).oper[0]^.reg, hp1) then
  9461. { Some other instruction has modified the source register }
  9462. Break;
  9463. end;
  9464. end;
  9465. DebugMsg(SPeepholeOptimization + 'CMOVcc/Jcc -> MOV/Jcc since register is not used if not branching', p);
  9466. taicpu(p).opcode := A_MOV;
  9467. taicpu(p).condition := C_None;
  9468. { Rely on the post peephole stage to put the MOV before the
  9469. CMP/TEST instruction that appears prior }
  9470. Result := True;
  9471. Exit;
  9472. end;
  9473. end;
  9474. end;
  9475. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  9476. function IsXCHGAcceptable: Boolean; inline;
  9477. begin
  9478. { Always accept if optimising for size }
  9479. Result := (cs_opt_size in current_settings.optimizerswitches) or
  9480. { From the Pentium M onwards, XCHG only has a latency of 2 rather
  9481. than 3, so it becomes a saving compared to three MOVs with two of
  9482. them able to execute simultaneously. [Kit] }
  9483. (CPUX86_HINT_FAST_XCHG in cpu_optimization_hints[current_settings.optimizecputype]);
  9484. end;
  9485. var
  9486. NewRef: TReference;
  9487. hp1, hp2, hp3, hp4: Tai;
  9488. {$ifndef x86_64}
  9489. OperIdx: Integer;
  9490. {$endif x86_64}
  9491. NewInstr : Taicpu;
  9492. NewAligh : Tai_align;
  9493. DestLabel: TAsmLabel;
  9494. TempTracking: TAllUsedRegs;
  9495. function TryMovArith2Lea(InputInstr: tai): Boolean;
  9496. var
  9497. NextInstr: tai;
  9498. begin
  9499. Result := False;
  9500. UpdateUsedRegs(TmpUsedRegs, tai(InputInstr.Next));
  9501. if not GetNextInstruction(InputInstr, NextInstr) or
  9502. (
  9503. { The FLAGS register isn't always tracked properly, so do not
  9504. perform this optimisation if a conditional statement follows }
  9505. not RegReadByInstruction(NR_DEFAULTFLAGS, NextInstr) and
  9506. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, NextInstr, TmpUsedRegs)
  9507. ) then
  9508. begin
  9509. reference_reset(NewRef, 1, []);
  9510. NewRef.base := taicpu(p).oper[0]^.reg;
  9511. NewRef.scalefactor := 1;
  9512. if taicpu(InputInstr).opcode = A_ADD then
  9513. begin
  9514. DebugMsg(SPeepholeOptimization + 'MovAdd2Lea', p);
  9515. NewRef.offset := taicpu(InputInstr).oper[0]^.val;
  9516. end
  9517. else
  9518. begin
  9519. DebugMsg(SPeepholeOptimization + 'MovSub2Lea', p);
  9520. NewRef.offset := -taicpu(InputInstr).oper[0]^.val;
  9521. end;
  9522. taicpu(p).opcode := A_LEA;
  9523. taicpu(p).loadref(0, NewRef);
  9524. { For the sake of debugging, have the line info match the
  9525. arithmetic instruction rather than the MOV instruction }
  9526. taicpu(p).fileinfo := taicpu(InputInstr).fileinfo;
  9527. RemoveInstruction(InputInstr);
  9528. Result := True;
  9529. end;
  9530. end;
  9531. begin
  9532. Result:=false;
  9533. { This optimisation adds an instruction, so only do it for speed }
  9534. if not (cs_opt_size in current_settings.optimizerswitches) and
  9535. MatchOpType(taicpu(p), top_const, top_reg) and
  9536. (taicpu(p).oper[0]^.val = 0) then
  9537. begin
  9538. { To avoid compiler warning }
  9539. DestLabel := nil;
  9540. if (p.typ <> ait_instruction) or (taicpu(p).oper[1]^.typ <> top_reg) then
  9541. InternalError(2021040750);
  9542. if not GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[1]^.reg) then
  9543. Exit;
  9544. case hp1.typ of
  9545. ait_label:
  9546. begin
  9547. { Change:
  9548. mov $0,%reg mov $0,%reg
  9549. @Lbl1: @Lbl1:
  9550. test %reg,%reg / cmp $0,%reg test %reg,%reg / mov $0,%reg
  9551. je @Lbl2 jne @Lbl2
  9552. To: To:
  9553. mov $0,%reg mov $0,%reg
  9554. jmp @Lbl2 jmp @Lbl3
  9555. (align) (align)
  9556. @Lbl1: @Lbl1:
  9557. test %reg,%reg / cmp $0,%reg test %reg,%reg / cmp $0,%reg
  9558. je @Lbl2 je @Lbl2
  9559. @Lbl3: <-- Only if label exists
  9560. (Not if it's optimised for size)
  9561. }
  9562. if not GetNextInstruction(hp1, hp2) then
  9563. Exit;
  9564. if (hp2.typ = ait_instruction) and
  9565. (
  9566. { Register sizes must exactly match }
  9567. (
  9568. (taicpu(hp2).opcode = A_CMP) and
  9569. MatchOperand(taicpu(hp2).oper[0]^, 0) and
  9570. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^.reg)
  9571. ) or (
  9572. (taicpu(hp2).opcode = A_TEST) and
  9573. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  9574. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^.reg)
  9575. )
  9576. ) and GetNextInstruction(hp2, hp3) and
  9577. (hp3.typ = ait_instruction) and
  9578. (taicpu(hp3).opcode = A_JCC) and
  9579. (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
  9580. (taicpu(hp3).oper[0]^.ref^.index=NR_NO) and (taicpu(hp3).oper[0]^.ref^.symbol is tasmlabel) then
  9581. begin
  9582. { Check condition of jump }
  9583. { Always true? }
  9584. if condition_in(C_E, taicpu(hp3).condition) then
  9585. begin
  9586. { Copy label symbol and obtain matching label entry for the
  9587. conditional jump, as this will be our destination}
  9588. DestLabel := tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol);
  9589. DebugMsg(SPeepholeOptimization + 'Mov0LblCmp0Je -> Mov0JmpLblCmp0Je', p);
  9590. Result := True;
  9591. end
  9592. { Always false? }
  9593. else if condition_in(C_NE, taicpu(hp3).condition) and GetNextInstruction(hp3, hp2) then
  9594. begin
  9595. { This is only worth it if there's a jump to take }
  9596. case hp2.typ of
  9597. ait_instruction:
  9598. begin
  9599. if taicpu(hp2).opcode = A_JMP then
  9600. begin
  9601. DestLabel := tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol);
  9602. { An unconditional jump follows the conditional jump which will always be false,
  9603. so use this jump's destination for the new jump }
  9604. DebugMsg(SPeepholeOptimization + 'Mov0LblCmp0Jne -> Mov0JmpLblCmp0Jne (with JMP)', p);
  9605. Result := True;
  9606. end
  9607. else if taicpu(hp2).opcode = A_JCC then
  9608. begin
  9609. DestLabel := tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol);
  9610. if condition_in(C_E, taicpu(hp2).condition) then
  9611. begin
  9612. { A second conditional jump follows the conditional jump which will always be false,
  9613. while the second jump is always True, so use this jump's destination for the new jump }
  9614. DebugMsg(SPeepholeOptimization + 'Mov0LblCmp0Jne -> Mov0JmpLblCmp0Jne (with second Jcc)', p);
  9615. Result := True;
  9616. end;
  9617. { Don't risk it if the jump isn't always true (Result remains False) }
  9618. end;
  9619. end;
  9620. else
  9621. { If anything else don't optimise };
  9622. end;
  9623. end;
  9624. if Result then
  9625. begin
  9626. { Just so we have something to insert as a paremeter}
  9627. reference_reset(NewRef, 1, []);
  9628. NewInstr := taicpu.op_ref(A_JMP, S_NO, NewRef);
  9629. { Now actually load the correct parameter (this also
  9630. increases the reference count) }
  9631. NewInstr.loadsymbol(0, DestLabel, 0);
  9632. if (cs_opt_level3 in current_settings.optimizerswitches) then
  9633. begin
  9634. { Get instruction before original label (may not be p under -O3) }
  9635. if not GetLastInstruction(hp1, hp2) then
  9636. { Shouldn't fail here }
  9637. InternalError(2021040701);
  9638. end
  9639. else
  9640. hp2 := p;
  9641. taicpu(NewInstr).fileinfo := taicpu(hp2).fileinfo;
  9642. AsmL.InsertAfter(NewInstr, hp2);
  9643. { Add new alignment field }
  9644. (* AsmL.InsertAfter(
  9645. cai_align.create_max(
  9646. current_settings.alignment.jumpalign,
  9647. current_settings.alignment.jumpalignskipmax
  9648. ),
  9649. NewInstr
  9650. ); *)
  9651. end;
  9652. Exit;
  9653. end;
  9654. end;
  9655. else
  9656. ;
  9657. end;
  9658. end;
  9659. if not GetNextInstruction(p, hp1) then
  9660. Exit;
  9661. if MatchInstruction(hp1, A_CMP, A_TEST, []) then
  9662. begin
  9663. if (taicpu(hp1).opsize = taicpu(p).opsize) and DoMovCmpMemOpt(p, hp1) then
  9664. begin
  9665. Result := True;
  9666. Exit;
  9667. end;
  9668. { This optimisation is only effective on a second run of Pass 2,
  9669. hence -O3 or above.
  9670. Change:
  9671. mov %reg1,%reg2
  9672. cmp/test (contains %reg1)
  9673. mov x, %reg1
  9674. (another mov or a j(c))
  9675. To:
  9676. mov %reg1,%reg2
  9677. mov x, %reg1
  9678. cmp (%reg1 replaced with %reg2)
  9679. (another mov or a j(c))
  9680. The requirement of an additional MOV or a jump ensures there
  9681. isn't performance loss, since a j(c) will permit macro-fusion
  9682. with the cmp instruction, while another MOV likely means it's
  9683. not all being executed in a single cycle due to parallelisation.
  9684. }
  9685. if (cs_opt_level3 in current_settings.optimizerswitches) and
  9686. MatchOpType(taicpu(p), top_reg, top_reg) and
  9687. RegInInstruction(taicpu(p).oper[0]^.reg, taicpu(hp1)) and
  9688. GetNextInstruction(hp1, hp2) and
  9689. MatchInstruction(hp2, A_MOV, []) and
  9690. (taicpu(hp2).oper[1]^.typ = top_reg) and
  9691. { Registers don't have to be the same size in this case }
  9692. SuperRegistersEqual(taicpu(hp2).oper[1]^.reg, taicpu(p).oper[0]^.reg) and
  9693. GetNextInstruction(hp2, hp3) and
  9694. MatchInstruction(hp3, A_MOV, A_Jcc, []) and
  9695. { Make sure the operands in the camparison can be safely replaced }
  9696. (
  9697. not RegInOp(taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^) or
  9698. ReplaceRegisterInOper(taicpu(hp1), 0, taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg)
  9699. ) and
  9700. (
  9701. not RegInOp(taicpu(p).oper[0]^.reg, taicpu(hp1).oper[1]^) or
  9702. ReplaceRegisterInOper(taicpu(hp1), 1, taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg)
  9703. ) then
  9704. begin
  9705. DebugMsg(SPeepholeOptimization + 'MOV/CMP/MOV -> MOV/MOV/CMP', p);
  9706. AsmL.Remove(hp2);
  9707. AsmL.InsertAfter(hp2, p);
  9708. Result := True;
  9709. Exit;
  9710. end;
  9711. end;
  9712. if MatchInstruction(hp1, A_JMP, [S_NO]) then
  9713. begin
  9714. { Sometimes the MOVs that OptPass2JMP produces can be improved
  9715. further, but we can't just put this jump optimisation in pass 1
  9716. because it tends to perform worse when conditional jumps are
  9717. nearby (e.g. when converting CMOV instructions). [Kit] }
  9718. CopyUsedRegs(TempTracking);
  9719. UpdateUsedRegs(tai(p.Next));
  9720. if OptPass2JMP(hp1) then
  9721. begin
  9722. { Restore register state }
  9723. RestoreUsedRegs(TempTracking);
  9724. ReleaseUsedRegs(TempTracking);
  9725. { call OptPass1MOV once to potentially merge any MOVs that were created }
  9726. OptPass1MOV(p);
  9727. Result := True;
  9728. Exit;
  9729. end;
  9730. { If OptPass2JMP returned False, no optimisations were done to
  9731. the jump and there are no further optimisations that can be done
  9732. to the MOV instruction on this pass other than FuncMov2Func }
  9733. { Restore register state }
  9734. RestoreUsedRegs(TempTracking);
  9735. ReleaseUsedRegs(TempTracking);
  9736. Result := FuncMov2Func(p, hp1);
  9737. Exit;
  9738. end;
  9739. if MatchOpType(taicpu(p),top_reg,top_reg) and
  9740. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  9741. MatchInstruction(hp1,A_ADD,A_SUB,[taicpu(p).opsize]) and
  9742. (taicpu(hp1).oper[1]^.typ = top_reg) and
  9743. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  9744. begin
  9745. { Change:
  9746. movl/q %reg1,%reg2 movl/q %reg1,%reg2
  9747. addl/q $x,%reg2 subl/q $x,%reg2
  9748. To:
  9749. leal/q x(%reg1),%reg2 leal/q -x(%reg1),%reg2
  9750. }
  9751. if (taicpu(hp1).oper[0]^.typ = top_const) and
  9752. { be lazy, checking separately for sub would be slightly better }
  9753. (abs(taicpu(hp1).oper[0]^.val)<=$7fffffff) then
  9754. begin
  9755. TransferUsedRegs(TmpUsedRegs);
  9756. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  9757. if TryMovArith2Lea(hp1) then
  9758. begin
  9759. Result := True;
  9760. Exit;
  9761. end
  9762. end
  9763. else if not RegInOp(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[0]^) and
  9764. GetNextInstructionUsingReg(hp1, hp2, taicpu(p).oper[1]^.reg) and
  9765. { Same as above, but also adds or subtracts to %reg2 in between.
  9766. It's still valid as long as the flags aren't in use }
  9767. MatchInstruction(hp2,A_ADD,A_SUB,[taicpu(p).opsize]) and
  9768. MatchOpType(taicpu(hp2), top_const, top_reg) and
  9769. (taicpu(hp2).oper[1]^.reg = taicpu(p).oper[1]^.reg) and
  9770. { be lazy, checking separately for sub would be slightly better }
  9771. (abs(taicpu(hp2).oper[0]^.val)<=$7fffffff) then
  9772. begin
  9773. TransferUsedRegs(TmpUsedRegs);
  9774. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  9775. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  9776. if TryMovArith2Lea(hp2) then
  9777. begin
  9778. Result := True;
  9779. Exit;
  9780. end;
  9781. end;
  9782. end;
  9783. if MatchOpType(taicpu(p),top_reg,top_reg) and
  9784. {$ifdef x86_64}
  9785. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  9786. {$else x86_64}
  9787. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  9788. {$endif x86_64}
  9789. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  9790. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  9791. { mov reg1, reg2 mov reg1, reg2
  9792. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  9793. begin
  9794. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  9795. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  9796. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  9797. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  9798. TransferUsedRegs(TmpUsedRegs);
  9799. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  9800. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  9801. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  9802. then
  9803. begin
  9804. RemoveCurrentP(p, hp1);
  9805. Result:=true;
  9806. end;
  9807. Exit;
  9808. end;
  9809. if MatchOpType(taicpu(p),top_reg,top_reg) and
  9810. IsXCHGAcceptable and
  9811. { XCHG doesn't support 8-bit registers }
  9812. (taicpu(p).opsize <> S_B) and
  9813. MatchInstruction(hp1, A_MOV, []) and
  9814. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  9815. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  9816. GetNextInstruction(hp1, hp2) and
  9817. MatchInstruction(hp2, A_MOV, []) and
  9818. { Don't need to call MatchOpType for hp2 because the operand matches below cover for it }
  9819. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  9820. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) then
  9821. begin
  9822. { mov %reg1,%reg2
  9823. mov %reg3,%reg1 -> xchg %reg3,%reg1
  9824. mov %reg2,%reg3
  9825. (%reg2 not used afterwards)
  9826. Note that xchg takes 3 cycles to execute, and generally mov's take
  9827. only one cycle apiece, but the first two mov's can be executed in
  9828. parallel, only taking 2 cycles overall. Older processors should
  9829. therefore only optimise for size. [Kit]
  9830. }
  9831. TransferUsedRegs(TmpUsedRegs);
  9832. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  9833. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  9834. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs) then
  9835. begin
  9836. DebugMsg(SPeepholeOptimization + 'MovMovMov2XChg', p);
  9837. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp1, UsedRegs);
  9838. taicpu(hp1).opcode := A_XCHG;
  9839. RemoveCurrentP(p, hp1);
  9840. RemoveInstruction(hp2);
  9841. Result := True;
  9842. Exit;
  9843. end;
  9844. end;
  9845. if MatchOpType(taicpu(p),top_reg,top_reg) and
  9846. MatchInstruction(hp1, A_SAR, []) then
  9847. begin
  9848. if MatchOperand(taicpu(hp1).oper[0]^, 31) then
  9849. begin
  9850. { the use of %edx also covers the opsize being S_L }
  9851. if MatchOperand(taicpu(hp1).oper[1]^, NR_EDX) then
  9852. begin
  9853. { Note it has to be specifically "movl %eax,%edx", and those specific sub-registers }
  9854. if (taicpu(p).oper[0]^.reg = NR_EAX) and
  9855. (taicpu(p).oper[1]^.reg = NR_EDX) then
  9856. begin
  9857. { Change:
  9858. movl %eax,%edx
  9859. sarl $31,%edx
  9860. To:
  9861. cltd
  9862. }
  9863. DebugMsg(SPeepholeOptimization + 'MovSar2Cltd', p);
  9864. RemoveInstruction(hp1);
  9865. taicpu(p).opcode := A_CDQ;
  9866. taicpu(p).opsize := S_NO;
  9867. taicpu(p).clearop(1);
  9868. taicpu(p).clearop(0);
  9869. taicpu(p).ops:=0;
  9870. Result := True;
  9871. Exit;
  9872. end
  9873. else if (cs_opt_size in current_settings.optimizerswitches) and
  9874. (taicpu(p).oper[0]^.reg = NR_EDX) and
  9875. (taicpu(p).oper[1]^.reg = NR_EAX) then
  9876. begin
  9877. { Change:
  9878. movl %edx,%eax
  9879. sarl $31,%edx
  9880. To:
  9881. movl %edx,%eax
  9882. cltd
  9883. Note that this creates a dependency between the two instructions,
  9884. so only perform if optimising for size.
  9885. }
  9886. DebugMsg(SPeepholeOptimization + 'MovSar2MovCltd', p);
  9887. taicpu(hp1).opcode := A_CDQ;
  9888. taicpu(hp1).opsize := S_NO;
  9889. taicpu(hp1).clearop(1);
  9890. taicpu(hp1).clearop(0);
  9891. taicpu(hp1).ops:=0;
  9892. Include(OptsToCheck, aoc_ForceNewIteration);
  9893. Exit;
  9894. end;
  9895. {$ifndef x86_64}
  9896. end
  9897. { Don't bother if CMOV is supported, because a more optimal
  9898. sequence would have been generated for the Abs() intrinsic }
  9899. else if not(CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and
  9900. { the use of %eax also covers the opsize being S_L }
  9901. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) and
  9902. (taicpu(p).oper[0]^.reg = NR_EAX) and
  9903. (taicpu(p).oper[1]^.reg = NR_EDX) and
  9904. GetNextInstruction(hp1, hp2) and
  9905. MatchInstruction(hp2, A_XOR, [S_L]) and
  9906. MatchOperand(taicpu(hp2).oper[0]^, NR_EAX) and
  9907. MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) and
  9908. GetNextInstruction(hp2, hp3) and
  9909. MatchInstruction(hp3, A_SUB, [S_L]) and
  9910. MatchOperand(taicpu(hp3).oper[0]^, NR_EAX) and
  9911. MatchOperand(taicpu(hp3).oper[1]^, NR_EDX) then
  9912. begin
  9913. { Change:
  9914. movl %eax,%edx
  9915. sarl $31,%eax
  9916. xorl %eax,%edx
  9917. subl %eax,%edx
  9918. (Instruction that uses %edx)
  9919. (%eax deallocated)
  9920. (%edx deallocated)
  9921. To:
  9922. cltd
  9923. xorl %edx,%eax <-- Note the registers have swapped
  9924. subl %edx,%eax
  9925. (Instruction that uses %eax) <-- %eax rather than %edx
  9926. }
  9927. TransferUsedRegs(TmpUsedRegs);
  9928. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  9929. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  9930. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  9931. if not RegUsedAfterInstruction(NR_EAX, hp3, TmpUsedRegs) then
  9932. begin
  9933. if GetNextInstruction(hp3, hp4) and
  9934. not RegModifiedByInstruction(NR_EDX, hp4) and
  9935. not RegUsedAfterInstruction(NR_EDX, hp4, TmpUsedRegs) then
  9936. begin
  9937. DebugMsg(SPeepholeOptimization + 'abs() intrinsic optimisation', p);
  9938. taicpu(p).opcode := A_CDQ;
  9939. taicpu(p).clearop(1);
  9940. taicpu(p).clearop(0);
  9941. taicpu(p).ops:=0;
  9942. RemoveInstruction(hp1);
  9943. taicpu(hp2).loadreg(0, NR_EDX);
  9944. taicpu(hp2).loadreg(1, NR_EAX);
  9945. taicpu(hp3).loadreg(0, NR_EDX);
  9946. taicpu(hp3).loadreg(1, NR_EAX);
  9947. AllocRegBetween(NR_EAX, hp3, hp4, TmpUsedRegs);
  9948. { Convert references in the following instruction (hp4) from %edx to %eax }
  9949. for OperIdx := 0 to taicpu(hp4).ops - 1 do
  9950. with taicpu(hp4).oper[OperIdx]^ do
  9951. case typ of
  9952. top_reg:
  9953. if getsupreg(reg) = RS_EDX then
  9954. reg := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  9955. top_ref:
  9956. begin
  9957. if getsupreg(reg) = RS_EDX then
  9958. ref^.base := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  9959. if getsupreg(reg) = RS_EDX then
  9960. ref^.index := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  9961. end;
  9962. else
  9963. ;
  9964. end;
  9965. Result := True;
  9966. Exit;
  9967. end;
  9968. end;
  9969. {$else x86_64}
  9970. end;
  9971. end
  9972. else if MatchOperand(taicpu(hp1).oper[0]^, 63) and
  9973. { the use of %rdx also covers the opsize being S_Q }
  9974. MatchOperand(taicpu(hp1).oper[1]^, NR_RDX) then
  9975. begin
  9976. { Note it has to be specifically "movq %rax,%rdx", and those specific sub-registers }
  9977. if (taicpu(p).oper[0]^.reg = NR_RAX) and
  9978. (taicpu(p).oper[1]^.reg = NR_RDX) then
  9979. begin
  9980. { Change:
  9981. movq %rax,%rdx
  9982. sarq $63,%rdx
  9983. To:
  9984. cqto
  9985. }
  9986. DebugMsg(SPeepholeOptimization + 'MovSar2Cqto', p);
  9987. RemoveInstruction(hp1);
  9988. taicpu(p).opcode := A_CQO;
  9989. taicpu(p).opsize := S_NO;
  9990. taicpu(p).clearop(1);
  9991. taicpu(p).clearop(0);
  9992. taicpu(p).ops:=0;
  9993. Result := True;
  9994. Exit;
  9995. end
  9996. else if (cs_opt_size in current_settings.optimizerswitches) and
  9997. (taicpu(p).oper[0]^.reg = NR_RDX) and
  9998. (taicpu(p).oper[1]^.reg = NR_RAX) then
  9999. begin
  10000. { Change:
  10001. movq %rdx,%rax
  10002. sarq $63,%rdx
  10003. To:
  10004. movq %rdx,%rax
  10005. cqto
  10006. Note that this creates a dependency between the two instructions,
  10007. so only perform if optimising for size.
  10008. }
  10009. DebugMsg(SPeepholeOptimization + 'MovSar2MovCqto', p);
  10010. taicpu(hp1).opcode := A_CQO;
  10011. taicpu(hp1).opsize := S_NO;
  10012. taicpu(hp1).clearop(1);
  10013. taicpu(hp1).clearop(0);
  10014. taicpu(hp1).ops:=0;
  10015. Include(OptsToCheck, aoc_ForceNewIteration);
  10016. Exit;
  10017. {$endif x86_64}
  10018. end;
  10019. end;
  10020. end;
  10021. if MatchInstruction(hp1, A_MOV, []) and
  10022. (taicpu(hp1).oper[1]^.typ = top_reg) then
  10023. { Though "GetNextInstruction" could be factored out, along with
  10024. the instructions that depend on hp2, it is an expensive call that
  10025. should be delayed for as long as possible, hence we do cheaper
  10026. checks first that are likely to be False. [Kit] }
  10027. begin
  10028. if (
  10029. (
  10030. MatchOperand(taicpu(p).oper[1]^, NR_EDX) and
  10031. (taicpu(hp1).oper[1]^.reg = NR_EAX) and
  10032. (
  10033. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  10034. MatchOperand(taicpu(hp1).oper[0]^, NR_EDX)
  10035. )
  10036. ) or
  10037. (
  10038. MatchOperand(taicpu(p).oper[1]^, NR_EAX) and
  10039. (taicpu(hp1).oper[1]^.reg = NR_EDX) and
  10040. (
  10041. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  10042. MatchOperand(taicpu(hp1).oper[0]^, NR_EAX)
  10043. )
  10044. )
  10045. ) and
  10046. GetNextInstruction(hp1, hp2) and
  10047. MatchInstruction(hp2, A_SAR, []) and
  10048. MatchOperand(taicpu(hp2).oper[0]^, 31) then
  10049. begin
  10050. if MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) then
  10051. begin
  10052. { Change:
  10053. movl r/m,%edx movl r/m,%eax movl r/m,%edx movl r/m,%eax
  10054. movl %edx,%eax or movl %eax,%edx or movl r/m,%eax or movl r/m,%edx
  10055. sarl $31,%edx sarl $31,%edx sarl $31,%edx sarl $31,%edx
  10056. To:
  10057. movl r/m,%eax <- Note the change in register
  10058. cltd
  10059. }
  10060. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCltd', p);
  10061. AllocRegBetween(NR_EAX, p, hp1, UsedRegs);
  10062. taicpu(p).loadreg(1, NR_EAX);
  10063. taicpu(hp1).opcode := A_CDQ;
  10064. taicpu(hp1).clearop(1);
  10065. taicpu(hp1).clearop(0);
  10066. taicpu(hp1).ops:=0;
  10067. RemoveInstruction(hp2);
  10068. Include(OptsToCheck, aoc_ForceNewIteration);
  10069. (*
  10070. {$ifdef x86_64}
  10071. end
  10072. else if MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) and
  10073. { This code sequence does not get generated - however it might become useful
  10074. if and when 128-bit signed integer types make an appearance, so the code
  10075. is kept here for when it is eventually needed. [Kit] }
  10076. (
  10077. (
  10078. (taicpu(hp1).oper[1]^.reg = NR_RAX) and
  10079. (
  10080. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  10081. MatchOperand(taicpu(hp1).oper[0]^, NR_RDX)
  10082. )
  10083. ) or
  10084. (
  10085. (taicpu(hp1).oper[1]^.reg = NR_RDX) and
  10086. (
  10087. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  10088. MatchOperand(taicpu(hp1).oper[0]^, NR_RAX)
  10089. )
  10090. )
  10091. ) and
  10092. GetNextInstruction(hp1, hp2) and
  10093. MatchInstruction(hp2, A_SAR, [S_Q]) and
  10094. MatchOperand(taicpu(hp2).oper[0]^, 63) and
  10095. MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) then
  10096. begin
  10097. { Change:
  10098. movq r/m,%rdx movq r/m,%rax movq r/m,%rdx movq r/m,%rax
  10099. movq %rdx,%rax or movq %rax,%rdx or movq r/m,%rax or movq r/m,%rdx
  10100. sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx
  10101. To:
  10102. movq r/m,%rax <- Note the change in register
  10103. cqto
  10104. }
  10105. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCqto', p);
  10106. AllocRegBetween(NR_RAX, p, hp1, UsedRegs);
  10107. taicpu(p).loadreg(1, NR_RAX);
  10108. taicpu(hp1).opcode := A_CQO;
  10109. taicpu(hp1).clearop(1);
  10110. taicpu(hp1).clearop(0);
  10111. taicpu(hp1).ops:=0;
  10112. RemoveInstruction(hp2);
  10113. Include(OptsToCheck, aoc_ForceNewIteration);
  10114. {$endif x86_64}
  10115. *)
  10116. end;
  10117. end;
  10118. {$ifdef x86_64}
  10119. end;
  10120. if (taicpu(p).opsize = S_L) and
  10121. (taicpu(p).oper[1]^.typ = top_reg) and
  10122. (
  10123. MatchInstruction(hp1, A_MOV,[]) and
  10124. (taicpu(hp1).opsize = S_L) and
  10125. (taicpu(hp1).oper[1]^.typ = top_reg)
  10126. ) and (
  10127. GetNextInstruction(hp1, hp2) and
  10128. (tai(hp2).typ=ait_instruction) and
  10129. (taicpu(hp2).opsize = S_Q) and
  10130. (
  10131. (
  10132. MatchInstruction(hp2, A_ADD,[]) and
  10133. (taicpu(hp2).opsize = S_Q) and
  10134. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  10135. (
  10136. (
  10137. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  10138. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  10139. ) or (
  10140. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  10141. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  10142. )
  10143. )
  10144. ) or (
  10145. MatchInstruction(hp2, A_LEA,[]) and
  10146. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  10147. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  10148. (
  10149. (
  10150. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  10151. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  10152. ) or (
  10153. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  10154. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  10155. )
  10156. ) and (
  10157. (
  10158. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  10159. ) or (
  10160. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  10161. )
  10162. )
  10163. )
  10164. )
  10165. ) and (
  10166. GetNextInstruction(hp2, hp3) and
  10167. MatchInstruction(hp3, A_SHR,[]) and
  10168. (taicpu(hp3).opsize = S_Q) and
  10169. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  10170. (taicpu(hp3).oper[0]^.val = 1) and
  10171. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  10172. ) then
  10173. begin
  10174. { Change movl x, reg1d movl x, reg1d
  10175. movl y, reg2d movl y, reg2d
  10176. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  10177. shrq $1, reg1q shrq $1, reg1q
  10178. ( reg1d and reg2d can be switched around in the first two instructions )
  10179. To movl x, reg1d
  10180. addl y, reg1d
  10181. rcrl $1, reg1d
  10182. This corresponds to the common expression (x + y) shr 1, where
  10183. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  10184. smaller code, but won't account for x + y causing an overflow). [Kit]
  10185. }
  10186. DebugMsg(SPeepholeOptimization + 'MovMov*Shr2MovMov*Rcr', p);
  10187. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  10188. begin
  10189. { Change first MOV command to have the same register as the final output }
  10190. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg;
  10191. AllocRegBetween(taicpu(hp1).oper[1]^.reg, p, hp1, UsedRegs);
  10192. Result := True;
  10193. end
  10194. else
  10195. begin
  10196. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  10197. Include(OptsToCheck, aoc_ForceNewIteration);
  10198. end;
  10199. { Change second MOV command to an ADD command. This is easier than
  10200. converting the existing command because it means we don't have to
  10201. touch 'y', which might be a complicated reference, and also the
  10202. fact that the third command might either be ADD or LEA. [Kit] }
  10203. taicpu(hp1).opcode := A_ADD;
  10204. { Delete old ADD/LEA instruction }
  10205. RemoveInstruction(hp2);
  10206. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  10207. taicpu(hp3).opcode := A_RCR;
  10208. taicpu(hp3).changeopsize(S_L);
  10209. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  10210. { Don't need to Exit yet as p is still a MOV and hp1 hasn't been
  10211. called, so FuncMov2Func below is safe to call }
  10212. {$endif x86_64}
  10213. end;
  10214. {$ifdef x86_64}
  10215. { Note, this optimisation was moved from Pass 1 because the CMOV
  10216. optimisations in OptPass2Jcc fall foul of the loss of information
  10217. about the upper 32 bits of the target register. Fixes #41317. }
  10218. { Change:
  10219. movl %reg1l,%reg2l
  10220. movq %reg2q,%reg3q (%reg1 <> %reg3)
  10221. To:
  10222. movl %reg1l,%reg2l
  10223. movl %reg1l,%reg3l (Upper 32 bits of %reg3q will be zero)
  10224. }
  10225. if MatchOpType(taicpu(p), top_reg, top_reg) and
  10226. (taicpu(p).opsize = S_L) and
  10227. (
  10228. not(cs_opt_level3 in current_settings.optimizerswitches) or
  10229. { Look further ahead for this one }
  10230. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[1]^.reg)
  10231. ) and
  10232. MatchInstruction(hp1,A_MOV,[S_Q]) and
  10233. not RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1) and
  10234. MatchOpType(taicpu(hp1), top_reg, top_reg) and
  10235. SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[0]^.reg) then
  10236. begin
  10237. TransferUsedRegs(TmpUsedRegs);
  10238. UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1);
  10239. taicpu(hp1).opsize := S_L;
  10240. taicpu(hp1).loadreg(0, taicpu(p).oper[0]^.reg);
  10241. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  10242. AllocRegBetween(taicpu(p).oper[0]^.reg, p, hp1, UsedRegs);
  10243. DebugMsg(SPeepholeOptimization + 'Made 32-to-64-bit zero extension more efficient (MovlMovq2MovlMovl 1)', hp1);
  10244. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp1, TmpUsedRegs) then
  10245. begin
  10246. DebugMsg(SPeepholeOptimization + 'Mov2Nop 8 done', p);
  10247. RemoveCurrentP(p);
  10248. Result := True;
  10249. Exit;
  10250. end
  10251. else
  10252. begin
  10253. { Initial instruction wasn't actually changed }
  10254. Include(OptsToCheck, aoc_ForceNewIteration);
  10255. end;
  10256. end;
  10257. {$endif x86_64}
  10258. if FuncMov2Func(p, hp1) then
  10259. begin
  10260. Result := True;
  10261. Exit;
  10262. end;
  10263. end;
  10264. {$push}
  10265. {$q-}{$r-}
  10266. function TX86AsmOptimizer.OptPass2Movx(var p : tai) : boolean;
  10267. var
  10268. ThisReg: TRegister;
  10269. MinSize, MaxSize, TryShiftDown, TargetSize: TOpSize;
  10270. TargetSubReg: TSubRegister;
  10271. hp1, hp2: tai;
  10272. RegInUse, RegChanged, p_removed, hp1_removed: Boolean;
  10273. { Store list of found instructions so we don't have to call
  10274. GetNextInstructionUsingReg multiple times }
  10275. InstrList: array of taicpu;
  10276. InstrMax, Index: Integer;
  10277. UpperLimit, SignedUpperLimit, SignedUpperLimitBottom,
  10278. LowerLimit, SignedLowerLimit, SignedLowerLimitBottom,
  10279. TryShiftDownLimit, TryShiftDownSignedLimit, TryShiftDownSignedLimitLower,
  10280. WorkingValue: TCgInt;
  10281. PreMessage: string;
  10282. { Data flow analysis }
  10283. TestValMin, TestValMax, TestValSignedMax: TCgInt;
  10284. BitwiseOnly, OrXorUsed,
  10285. ShiftDownOverflow, UpperSignedOverflow, UpperUnsignedOverflow, LowerSignedOverflow, LowerUnsignedOverflow: Boolean;
  10286. function CheckOverflowConditions: Boolean;
  10287. begin
  10288. Result := True;
  10289. if (TestValSignedMax > SignedUpperLimit) then
  10290. UpperSignedOverflow := True;
  10291. if (TestValSignedMax > SignedLowerLimit) or (TestValSignedMax < SignedLowerLimitBottom) then
  10292. LowerSignedOverflow := True;
  10293. if (TestValMin > LowerLimit) or (TestValMax > LowerLimit) then
  10294. LowerUnsignedOverflow := True;
  10295. if (TestValMin > UpperLimit) or (TestValMax > UpperLimit) or (TestValSignedMax > UpperLimit) or
  10296. (TestValMin < SignedUpperLimitBottom) or (TestValMax < SignedUpperLimitBottom) or (TestValSignedMax < SignedUpperLimitBottom) then
  10297. begin
  10298. { Absolute overflow }
  10299. Result := False;
  10300. Exit;
  10301. end;
  10302. if not ShiftDownOverflow and (TryShiftDown <> S_NO) and
  10303. ((TestValMin > TryShiftDownLimit) or (TestValMax > TryShiftDownLimit)) then
  10304. ShiftDownOverflow := True;
  10305. if (TestValMin < 0) or (TestValMax < 0) then
  10306. begin
  10307. LowerUnsignedOverflow := True;
  10308. UpperUnsignedOverflow := True;
  10309. end;
  10310. end;
  10311. function AdjustInitialLoadAndSize: Boolean;
  10312. begin
  10313. Result := False;
  10314. if not p_removed then
  10315. begin
  10316. if TargetSize = MinSize then
  10317. begin
  10318. { Convert the input MOVZX to a MOV }
  10319. if (taicpu(p).oper[0]^.typ = top_reg) and
  10320. SuperRegistersEqual(taicpu(p).oper[0]^.reg, ThisReg) then
  10321. begin
  10322. { Or remove it completely! }
  10323. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 1', p);
  10324. RemoveCurrentP(p);
  10325. p_removed := True;
  10326. end
  10327. else
  10328. begin
  10329. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 1', p);
  10330. taicpu(p).opcode := A_MOV;
  10331. taicpu(p).oper[1]^.reg := ThisReg;
  10332. taicpu(p).opsize := TargetSize;
  10333. end;
  10334. Result := True;
  10335. end
  10336. else if TargetSize <> MaxSize then
  10337. begin
  10338. case MaxSize of
  10339. S_L:
  10340. if TargetSize = S_W then
  10341. begin
  10342. DebugMsg(SPeepholeOptimization + 'movzbl2movzbw', p);
  10343. taicpu(p).opsize := S_BW;
  10344. taicpu(p).oper[1]^.reg := ThisReg;
  10345. Result := True;
  10346. end
  10347. else
  10348. InternalError(2020112341);
  10349. S_W:
  10350. if TargetSize = S_L then
  10351. begin
  10352. DebugMsg(SPeepholeOptimization + 'movzbw2movzbl', p);
  10353. taicpu(p).opsize := S_BL;
  10354. taicpu(p).oper[1]^.reg := ThisReg;
  10355. Result := True;
  10356. end
  10357. else
  10358. InternalError(2020112342);
  10359. else
  10360. ;
  10361. end;
  10362. end
  10363. else if not hp1_removed and not RegInUse then
  10364. begin
  10365. { If we have something like:
  10366. movzbl (oper),%regd
  10367. add x, %regd
  10368. movzbl %regb, %regd
  10369. We can reduce the register size to the input of the final
  10370. movzbl instruction. Overflows won't have any effect.
  10371. }
  10372. if (taicpu(p).opsize in [S_BW, S_BL]) and
  10373. (taicpu(hp1).opsize in [S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}]) then
  10374. begin
  10375. TargetSize := S_B;
  10376. setsubreg(ThisReg, R_SUBL);
  10377. Result := True;
  10378. end
  10379. else if (taicpu(p).opsize = S_WL) and
  10380. (taicpu(hp1).opsize in [S_WL{$ifdef x86_64}, S_BQ{$endif x86_64}]) then
  10381. begin
  10382. TargetSize := S_W;
  10383. setsubreg(ThisReg, R_SUBW);
  10384. Result := True;
  10385. end;
  10386. if Result then
  10387. begin
  10388. { Convert the input MOVZX to a MOV }
  10389. if (taicpu(p).oper[0]^.typ = top_reg) and
  10390. SuperRegistersEqual(taicpu(p).oper[0]^.reg, ThisReg) then
  10391. begin
  10392. { Or remove it completely! }
  10393. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 1a', p);
  10394. RemoveCurrentP(p);
  10395. p_removed := True;
  10396. end
  10397. else
  10398. begin
  10399. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 1a', p);
  10400. taicpu(p).opcode := A_MOV;
  10401. taicpu(p).oper[1]^.reg := ThisReg;
  10402. taicpu(p).opsize := TargetSize;
  10403. end;
  10404. end;
  10405. end;
  10406. end;
  10407. end;
  10408. procedure AdjustFinalLoad;
  10409. begin
  10410. if not LowerUnsignedOverflow then
  10411. begin
  10412. if ((TargetSize = S_L) and (taicpu(hp1).opsize in [S_L, S_BL, S_WL])) or
  10413. ((TargetSize = S_W) and (taicpu(hp1).opsize in [S_W, S_BW])) then
  10414. begin
  10415. { Convert the output MOVZX to a MOV }
  10416. if SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) then
  10417. begin
  10418. { Make sure the zero-expansion covers at least the minimum size (fixes i40003) }
  10419. if (MinSize = S_B) or
  10420. (not ShiftDownOverflow and (TryShiftDown = S_B)) or
  10421. ((MinSize = S_W) and (taicpu(hp1).opsize = S_WL)) then
  10422. begin
  10423. { Remove it completely! }
  10424. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 2', hp1);
  10425. { Be careful; if p = hp1 and p was also removed, p
  10426. will become a dangling pointer }
  10427. if p = hp1 then
  10428. begin
  10429. RemoveCurrentp(p); { p = hp1 and will then become the next instruction }
  10430. p_removed := True;
  10431. end
  10432. else
  10433. RemoveInstruction(hp1);
  10434. hp1_removed := True;
  10435. end;
  10436. end
  10437. else
  10438. begin
  10439. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 2', hp1);
  10440. taicpu(hp1).opcode := A_MOV;
  10441. taicpu(hp1).oper[0]^.reg := ThisReg;
  10442. taicpu(hp1).opsize := TargetSize;
  10443. end;
  10444. end
  10445. else if (TargetSize = S_B) and (MaxSize = S_W) and (taicpu(hp1).opsize = S_WL) then
  10446. begin
  10447. { Need to change the size of the output }
  10448. DebugMsg(SPeepholeOptimization + 'movzwl2movzbl 2', hp1);
  10449. taicpu(hp1).oper[0]^.reg := ThisReg;
  10450. taicpu(hp1).opsize := S_BL;
  10451. end;
  10452. end;
  10453. end;
  10454. function CompressInstructions: Boolean;
  10455. var
  10456. LocalIndex: Integer;
  10457. begin
  10458. Result := False;
  10459. { The objective here is to try to find a combination that
  10460. removes one of the MOV/Z instructions. }
  10461. if (
  10462. (taicpu(p).oper[0]^.typ <> top_reg) or
  10463. not SuperRegistersEqual(taicpu(p).oper[0]^.reg, ThisReg)
  10464. ) and
  10465. (taicpu(hp1).oper[1]^.typ = top_reg) and
  10466. SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) then
  10467. begin
  10468. { Make a preference to remove the second MOVZX instruction }
  10469. case taicpu(hp1).opsize of
  10470. S_BL, S_WL:
  10471. begin
  10472. TargetSize := S_L;
  10473. TargetSubReg := R_SUBD;
  10474. end;
  10475. S_BW:
  10476. begin
  10477. TargetSize := S_W;
  10478. TargetSubReg := R_SUBW;
  10479. end;
  10480. else
  10481. InternalError(2020112302);
  10482. end;
  10483. end
  10484. else
  10485. begin
  10486. if LowerUnsignedOverflow and not UpperUnsignedOverflow then
  10487. begin
  10488. { Exceeded lower bound but not upper bound }
  10489. TargetSize := MaxSize;
  10490. end
  10491. else if not LowerUnsignedOverflow then
  10492. begin
  10493. { Size didn't exceed lower bound }
  10494. TargetSize := MinSize;
  10495. end
  10496. else
  10497. Exit;
  10498. end;
  10499. case TargetSize of
  10500. S_B:
  10501. TargetSubReg := R_SUBL;
  10502. S_W:
  10503. TargetSubReg := R_SUBW;
  10504. S_L:
  10505. TargetSubReg := R_SUBD;
  10506. else
  10507. InternalError(2020112350);
  10508. end;
  10509. { Update the register to its new size }
  10510. setsubreg(ThisReg, TargetSubReg);
  10511. RegInUse := False;
  10512. if not SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) then
  10513. begin
  10514. { Check to see if the active register is used afterwards;
  10515. if not, we can change it and make a saving. }
  10516. TransferUsedRegs(TmpUsedRegs);
  10517. { The target register may be marked as in use to cross
  10518. a jump to a distant label, so exclude it }
  10519. ExcludeRegFromUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs);
  10520. hp2 := p;
  10521. repeat
  10522. { Explicitly check for the excluded register (don't include the first
  10523. instruction as it may be reading from here }
  10524. if ((p <> hp2) and (RegInInstruction(taicpu(hp1).oper[1]^.reg, hp2))) or
  10525. RegInUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs) then
  10526. begin
  10527. RegInUse := True;
  10528. Break;
  10529. end;
  10530. UpdateUsedRegs(TmpUsedRegs, tai(hp2.next));
  10531. if not GetNextInstruction(hp2, hp2) then
  10532. InternalError(2020112340);
  10533. until (hp2 = hp1);
  10534. if not RegInUse and RegUsedAfterInstruction(ThisReg, hp1, TmpUsedRegs) then
  10535. { We might still be able to get away with this }
  10536. RegInUse := not
  10537. (
  10538. GetNextInstructionUsingReg(hp1, hp2, ThisReg) and
  10539. (hp2.typ = ait_instruction) and
  10540. (
  10541. { Under -O1 and -O2, GetNextInstructionUsingReg may return an
  10542. instruction that doesn't actually contain ThisReg }
  10543. (cs_opt_level3 in current_settings.optimizerswitches) or
  10544. RegInInstruction(ThisReg, hp2)
  10545. ) and
  10546. RegLoadedWithNewValue(ThisReg, hp2)
  10547. );
  10548. if not RegInUse then
  10549. begin
  10550. { Force the register size to the same as this instruction so it can be removed}
  10551. if (taicpu(hp1).opsize in [S_L, S_BL, S_WL]) then
  10552. begin
  10553. TargetSize := S_L;
  10554. TargetSubReg := R_SUBD;
  10555. end
  10556. else if (taicpu(hp1).opsize in [S_W, S_BW]) then
  10557. begin
  10558. TargetSize := S_W;
  10559. TargetSubReg := R_SUBW;
  10560. end;
  10561. ThisReg := taicpu(hp1).oper[1]^.reg;
  10562. setsubreg(ThisReg, TargetSubReg);
  10563. RegChanged := True;
  10564. DebugMsg(SPeepholeOptimization + 'Simplified register usage so ' + debug_regname(ThisReg) + ' = ' + debug_regname(taicpu(p).oper[1]^.reg), p);
  10565. TransferUsedRegs(TmpUsedRegs);
  10566. AllocRegBetween(ThisReg, p, hp1, TmpUsedRegs);
  10567. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 3', hp1);
  10568. if p = hp1 then
  10569. begin
  10570. RemoveCurrentp(p); { p = hp1 and will then become the next instruction }
  10571. p_removed := True;
  10572. end
  10573. else
  10574. RemoveInstruction(hp1);
  10575. hp1_removed := True;
  10576. { Instruction will become "mov %reg,%reg" }
  10577. if not p_removed and (taicpu(p).opcode = A_MOV) and
  10578. MatchOperand(taicpu(p).oper[0]^, ThisReg) then
  10579. begin
  10580. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 6', p);
  10581. RemoveCurrentP(p);
  10582. p_removed := True;
  10583. end
  10584. else
  10585. taicpu(p).oper[1]^.reg := ThisReg;
  10586. Result := True;
  10587. end
  10588. else
  10589. begin
  10590. if TargetSize <> MaxSize then
  10591. begin
  10592. { Since the register is in use, we have to force it to
  10593. MaxSize otherwise part of it may become undefined later on }
  10594. TargetSize := MaxSize;
  10595. case TargetSize of
  10596. S_B:
  10597. TargetSubReg := R_SUBL;
  10598. S_W:
  10599. TargetSubReg := R_SUBW;
  10600. S_L:
  10601. TargetSubReg := R_SUBD;
  10602. else
  10603. InternalError(2020112351);
  10604. end;
  10605. setsubreg(ThisReg, TargetSubReg);
  10606. end;
  10607. AdjustFinalLoad;
  10608. end;
  10609. end
  10610. else
  10611. AdjustFinalLoad;
  10612. Result := AdjustInitialLoadAndSize or Result;
  10613. { Now go through every instruction we found and change the
  10614. size. If TargetSize = MaxSize, then almost no changes are
  10615. needed and Result can remain False if it hasn't been set
  10616. yet.
  10617. If RegChanged is True, then the register requires changing
  10618. and so the point about TargetSize = MaxSize doesn't apply. }
  10619. if ((TargetSize <> MaxSize) or RegChanged) and (InstrMax >= 0) then
  10620. begin
  10621. for LocalIndex := 0 to InstrMax do
  10622. begin
  10623. { If p_removed is true, then the original MOV/Z was removed
  10624. and removing the AND instruction may not be safe if it
  10625. appears first }
  10626. if (InstrList[LocalIndex].oper[InstrList[LocalIndex].ops - 1]^.typ <> top_reg) then
  10627. InternalError(2020112310);
  10628. if InstrList[LocalIndex].oper[0]^.typ = top_reg then
  10629. InstrList[LocalIndex].oper[0]^.reg := ThisReg;
  10630. InstrList[LocalIndex].oper[InstrList[LocalIndex].ops - 1]^.reg := ThisReg;
  10631. InstrList[LocalIndex].opsize := TargetSize;
  10632. end;
  10633. Result := True;
  10634. end;
  10635. end;
  10636. begin
  10637. Result := False;
  10638. p_removed := False;
  10639. hp1_removed := False;
  10640. ThisReg := taicpu(p).oper[1]^.reg;
  10641. { Check for:
  10642. movs/z ###,%ecx (or %cx or %rcx)
  10643. ...
  10644. shl/shr/sar/rcl/rcr/ror/rol %cl,###
  10645. (dealloc %ecx)
  10646. Change to:
  10647. mov ###,%cl (if ### = %cl, then remove completely)
  10648. ...
  10649. shl/shr/sar/rcl/rcr/ror/rol %cl,###
  10650. }
  10651. if (getsupreg(ThisReg) = RS_ECX) and
  10652. GetNextInstructionUsingReg(p, hp1, NR_ECX) and
  10653. (hp1.typ = ait_instruction) and
  10654. (
  10655. { Under -O1 and -O2, GetNextInstructionUsingReg may return an
  10656. instruction that doesn't actually contain ECX }
  10657. (cs_opt_level3 in current_settings.optimizerswitches) or
  10658. RegInInstruction(NR_ECX, hp1) or
  10659. (
  10660. { It's common for the shift/rotate's read/write register to be
  10661. initialised in between, so under -O2 and under, search ahead
  10662. one more instruction
  10663. }
  10664. GetNextInstruction(hp1, hp1) and
  10665. (hp1.typ = ait_instruction) and
  10666. RegInInstruction(NR_ECX, hp1)
  10667. )
  10668. ) and
  10669. MatchInstruction(hp1, [A_SHL, A_SHR, A_SAR, A_ROR, A_ROL, A_RCR, A_RCL], []) and
  10670. (taicpu(hp1).oper[0]^.typ = top_reg) { This is enough to determine that it's %cl } then
  10671. begin
  10672. TransferUsedRegs(TmpUsedRegs);
  10673. hp2 := p;
  10674. repeat
  10675. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  10676. until not GetNextInstruction(hp2, hp2) or (hp2 = hp1);
  10677. if not RegUsedAfterInstruction(NR_CL, hp1, TmpUsedRegs) then
  10678. begin
  10679. case taicpu(p).opsize of
  10680. S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  10681. if MatchOperand(taicpu(p).oper[0]^, NR_CL) then
  10682. begin
  10683. DebugMsg(SPeepholeOptimization + 'MovxOp2Op 3a', p);
  10684. RemoveCurrentP(p);
  10685. end
  10686. else
  10687. begin
  10688. taicpu(p).opcode := A_MOV;
  10689. taicpu(p).opsize := S_B;
  10690. taicpu(p).oper[1]^.reg := NR_CL;
  10691. DebugMsg(SPeepholeOptimization + 'MovxOp2MovOp 1', p);
  10692. end;
  10693. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  10694. if MatchOperand(taicpu(p).oper[0]^, NR_CX) then
  10695. begin
  10696. DebugMsg(SPeepholeOptimization + 'MovxOp2Op 3b', p);
  10697. RemoveCurrentP(p);
  10698. end
  10699. else
  10700. begin
  10701. taicpu(p).opcode := A_MOV;
  10702. taicpu(p).opsize := S_W;
  10703. taicpu(p).oper[1]^.reg := NR_CX;
  10704. DebugMsg(SPeepholeOptimization + 'MovxOp2MovOp 2', p);
  10705. end;
  10706. {$ifdef x86_64}
  10707. S_LQ:
  10708. if MatchOperand(taicpu(p).oper[0]^, NR_ECX) then
  10709. begin
  10710. DebugMsg(SPeepholeOptimization + 'MovxOp2Op 3c', p);
  10711. RemoveCurrentP(p);
  10712. end
  10713. else
  10714. begin
  10715. taicpu(p).opcode := A_MOV;
  10716. taicpu(p).opsize := S_L;
  10717. taicpu(p).oper[1]^.reg := NR_ECX;
  10718. DebugMsg(SPeepholeOptimization + 'MovxOp2MovOp 3', p);
  10719. end;
  10720. {$endif x86_64}
  10721. else
  10722. InternalError(2021120401);
  10723. end;
  10724. Result := True;
  10725. Exit;
  10726. end;
  10727. end;
  10728. { This is anything but quick! }
  10729. if not(cs_opt_level2 in current_settings.optimizerswitches) then
  10730. Exit;
  10731. SetLength(InstrList, 0);
  10732. InstrMax := -1;
  10733. case taicpu(p).opsize of
  10734. S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  10735. begin
  10736. {$if defined(i386) or defined(i8086)}
  10737. { If the target size is 8-bit, make sure we can actually encode it }
  10738. if not (GetSupReg(ThisReg) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX]) then
  10739. Exit;
  10740. {$endif i386 or i8086}
  10741. LowerLimit := $FF;
  10742. SignedLowerLimit := $7F;
  10743. SignedLowerLimitBottom := -128;
  10744. MinSize := S_B;
  10745. if taicpu(p).opsize = S_BW then
  10746. begin
  10747. MaxSize := S_W;
  10748. UpperLimit := $FFFF;
  10749. SignedUpperLimit := $7FFF;
  10750. SignedUpperLimitBottom := -32768;
  10751. end
  10752. else
  10753. begin
  10754. { Keep at a 32-bit limit for BQ as well since one can't really optimise otherwise }
  10755. MaxSize := S_L;
  10756. UpperLimit := $FFFFFFFF;
  10757. SignedUpperLimit := $7FFFFFFF;
  10758. SignedUpperLimitBottom := -2147483648;
  10759. end;
  10760. end;
  10761. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  10762. begin
  10763. { Keep at a 32-bit limit for WQ as well since one can't really optimise otherwise }
  10764. LowerLimit := $FFFF;
  10765. SignedLowerLimit := $7FFF;
  10766. SignedLowerLimitBottom := -32768;
  10767. UpperLimit := $FFFFFFFF;
  10768. SignedUpperLimit := $7FFFFFFF;
  10769. SignedUpperLimitBottom := -2147483648;
  10770. MinSize := S_W;
  10771. MaxSize := S_L;
  10772. end;
  10773. {$ifdef x86_64}
  10774. S_LQ:
  10775. begin
  10776. { Both the lower and upper limits are set to 32-bit. If a limit
  10777. is breached, then optimisation is impossible }
  10778. LowerLimit := $FFFFFFFF;
  10779. SignedLowerLimit := $7FFFFFFF;
  10780. SignedLowerLimitBottom := -2147483648;
  10781. UpperLimit := $FFFFFFFF;
  10782. SignedUpperLimit := $7FFFFFFF;
  10783. SignedUpperLimitBottom := -2147483648;
  10784. MinSize := S_L;
  10785. MaxSize := S_L;
  10786. end;
  10787. {$endif x86_64}
  10788. else
  10789. InternalError(2020112301);
  10790. end;
  10791. TestValMin := 0;
  10792. TestValMax := LowerLimit;
  10793. TestValSignedMax := SignedLowerLimit;
  10794. TryShiftDownLimit := LowerLimit;
  10795. TryShiftDown := S_NO;
  10796. ShiftDownOverflow := False;
  10797. RegChanged := False;
  10798. BitwiseOnly := True;
  10799. OrXorUsed := False;
  10800. UpperSignedOverflow := False;
  10801. LowerSignedOverflow := False;
  10802. UpperUnsignedOverflow := False;
  10803. LowerUnsignedOverflow := False;
  10804. hp1 := p;
  10805. while GetNextInstructionUsingReg(hp1, hp1, ThisReg) and
  10806. (hp1.typ = ait_instruction) and
  10807. (
  10808. { Under -O1 and -O2, GetNextInstructionUsingReg may return an
  10809. instruction that doesn't actually contain ThisReg }
  10810. (cs_opt_level3 in current_settings.optimizerswitches) or
  10811. { This allows this Movx optimisation to work through the SETcc instructions
  10812. inserted by the 'CMP/JE/CMP/@Lbl/SETE -> CMP/SETE/CMP/SETE/OR'
  10813. optimisation on -O1 and -O2 (on -O3, GetNextInstructionUsingReg will
  10814. skip over these SETcc instructions). }
  10815. (taicpu(hp1).opcode = A_SETcc) or
  10816. RegInInstruction(ThisReg, hp1)
  10817. ) do
  10818. begin
  10819. case taicpu(hp1).opcode of
  10820. A_INC,A_DEC:
  10821. begin
  10822. { Has to be an exact match on the register }
  10823. if not MatchOperand(taicpu(hp1).oper[0]^, ThisReg) then
  10824. Break;
  10825. if taicpu(hp1).opcode = A_INC then
  10826. begin
  10827. Inc(TestValMin);
  10828. Inc(TestValMax);
  10829. Inc(TestValSignedMax);
  10830. end
  10831. else
  10832. begin
  10833. Dec(TestValMin);
  10834. Dec(TestValMax);
  10835. Dec(TestValSignedMax);
  10836. end;
  10837. end;
  10838. A_TEST, A_CMP:
  10839. begin
  10840. if (
  10841. { Too high a risk of non-linear behaviour that breaks DFA
  10842. here, unless it's cmp $0,%reg, which is equivalent to
  10843. test %reg,%reg }
  10844. OrXorUsed and
  10845. (taicpu(hp1).opcode = A_CMP) and
  10846. not Matchoperand(taicpu(hp1).oper[0]^, 0)
  10847. ) or
  10848. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  10849. { Has to be an exact match on the register }
  10850. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  10851. (
  10852. { Permit "test %reg,%reg" }
  10853. (taicpu(hp1).opcode = A_TEST) and
  10854. (taicpu(hp1).oper[0]^.typ = top_reg) and
  10855. (taicpu(hp1).oper[0]^.reg <> ThisReg)
  10856. ) or
  10857. (taicpu(hp1).oper[0]^.typ <> top_const) or
  10858. { Make sure the comparison value is not smaller than the
  10859. smallest allowed signed value for the minimum size (e.g.
  10860. -128 for 8-bit) }
  10861. not (
  10862. ((taicpu(hp1).oper[0]^.val and LowerLimit) = taicpu(hp1).oper[0]^.val) or
  10863. { Is it in the negative range? }
  10864. (
  10865. (taicpu(hp1).oper[0]^.val < 0) and
  10866. (taicpu(hp1).oper[0]^.val >= SignedLowerLimitBottom)
  10867. )
  10868. ) then
  10869. Break;
  10870. { Check to see if the active register is used afterwards }
  10871. TransferUsedRegs(TmpUsedRegs);
  10872. IncludeRegInUsedRegs(ThisReg, TmpUsedRegs);
  10873. if not RegUsedAfterInstruction(ThisReg, hp1, TmpUsedRegs) then
  10874. begin
  10875. { Make sure the comparison or any previous instructions
  10876. hasn't pushed the test values outside of the range of
  10877. MinSize }
  10878. if LowerUnsignedOverflow and not UpperUnsignedOverflow then
  10879. begin
  10880. { Exceeded lower bound but not upper bound }
  10881. Exit;
  10882. end
  10883. else if not LowerSignedOverflow or not LowerUnsignedOverflow then
  10884. begin
  10885. { Size didn't exceed lower bound }
  10886. TargetSize := MinSize;
  10887. end
  10888. else
  10889. Break;
  10890. case TargetSize of
  10891. S_B:
  10892. TargetSubReg := R_SUBL;
  10893. S_W:
  10894. TargetSubReg := R_SUBW;
  10895. S_L:
  10896. TargetSubReg := R_SUBD;
  10897. else
  10898. InternalError(2021051002);
  10899. end;
  10900. if TargetSize <> MaxSize then
  10901. begin
  10902. { Update the register to its new size }
  10903. setsubreg(ThisReg, TargetSubReg);
  10904. DebugMsg(SPeepholeOptimization + 'CMP instruction resized thanks to register size optimisation (see MOV/Z assignment above)', hp1);
  10905. taicpu(hp1).oper[1]^.reg := ThisReg;
  10906. taicpu(hp1).opsize := TargetSize;
  10907. { Convert the input MOVZX to a MOV if necessary }
  10908. AdjustInitialLoadAndSize;
  10909. if (InstrMax >= 0) then
  10910. begin
  10911. for Index := 0 to InstrMax do
  10912. begin
  10913. { If p_removed is true, then the original MOV/Z was removed
  10914. and removing the AND instruction may not be safe if it
  10915. appears first }
  10916. if (InstrList[Index].oper[InstrList[Index].ops - 1]^.typ <> top_reg) then
  10917. InternalError(2020112311);
  10918. if InstrList[Index].oper[0]^.typ = top_reg then
  10919. InstrList[Index].oper[0]^.reg := ThisReg;
  10920. InstrList[Index].oper[InstrList[Index].ops - 1]^.reg := ThisReg;
  10921. InstrList[Index].opsize := MinSize;
  10922. end;
  10923. end;
  10924. Result := True;
  10925. end;
  10926. Exit;
  10927. end;
  10928. end;
  10929. A_SETcc:
  10930. begin
  10931. { This allows this Movx optimisation to work through the SETcc instructions
  10932. inserted by the 'CMP/JE/CMP/@Lbl/SETE -> CMP/SETE/CMP/SETE/OR'
  10933. optimisation on -O1 and -O2 (on -O3, GetNextInstructionUsingReg will
  10934. skip over these SETcc instructions). }
  10935. if (cs_opt_level3 in current_settings.optimizerswitches) or
  10936. { Of course, break out if the current register is used }
  10937. RegInOp(ThisReg, taicpu(hp1).oper[0]^) then
  10938. Break
  10939. else
  10940. { We must use Continue so the instruction doesn't get added
  10941. to InstrList }
  10942. Continue;
  10943. end;
  10944. A_ADD,A_SUB,A_AND,A_OR,A_XOR,A_SHL,A_SHR,A_SAR:
  10945. begin
  10946. if
  10947. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  10948. { Has to be an exact match on the register }
  10949. (taicpu(hp1).oper[1]^.reg <> ThisReg) or not
  10950. (
  10951. (
  10952. (taicpu(hp1).oper[0]^.typ = top_const) and
  10953. (
  10954. (
  10955. (taicpu(hp1).opcode = A_SHL) and
  10956. (
  10957. ((MinSize = S_B) and (taicpu(hp1).oper[0]^.val < 8)) or
  10958. ((MinSize = S_W) and (taicpu(hp1).oper[0]^.val < 16)) or
  10959. ((MinSize = S_L) and (taicpu(hp1).oper[0]^.val < 32))
  10960. )
  10961. ) or (
  10962. (taicpu(hp1).opcode <> A_SHL) and
  10963. (
  10964. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  10965. { Is it in the negative range? }
  10966. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val))
  10967. )
  10968. )
  10969. )
  10970. ) or (
  10971. MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^.reg) and
  10972. ((taicpu(hp1).opcode = A_ADD) or (taicpu(hp1).opcode = A_AND) or (taicpu(hp1).opcode = A_SUB))
  10973. )
  10974. ) then
  10975. Break;
  10976. { Only process OR and XOR if there are only bitwise operations,
  10977. since otherwise they can too easily fool the data flow
  10978. analysis (they can cause non-linear behaviour) }
  10979. case taicpu(hp1).opcode of
  10980. A_ADD:
  10981. begin
  10982. if OrXorUsed then
  10983. { Too high a risk of non-linear behaviour that breaks DFA here }
  10984. Break
  10985. else
  10986. BitwiseOnly := False;
  10987. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  10988. begin
  10989. TestValMin := TestValMin * 2;
  10990. TestValMax := TestValMax * 2;
  10991. TestValSignedMax := TestValSignedMax * 2;
  10992. end
  10993. else
  10994. begin
  10995. WorkingValue := taicpu(hp1).oper[0]^.val;
  10996. TestValMin := TestValMin + WorkingValue;
  10997. TestValMax := TestValMax + WorkingValue;
  10998. TestValSignedMax := TestValSignedMax + WorkingValue;
  10999. end;
  11000. end;
  11001. A_SUB:
  11002. begin
  11003. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  11004. begin
  11005. TestValMin := 0;
  11006. TestValMax := 0;
  11007. TestValSignedMax := 0;
  11008. end
  11009. else
  11010. begin
  11011. if OrXorUsed then
  11012. { Too high a risk of non-linear behaviour that breaks DFA here }
  11013. Break
  11014. else
  11015. BitwiseOnly := False;
  11016. WorkingValue := taicpu(hp1).oper[0]^.val;
  11017. TestValMin := TestValMin - WorkingValue;
  11018. TestValMax := TestValMax - WorkingValue;
  11019. TestValSignedMax := TestValSignedMax - WorkingValue;
  11020. end;
  11021. end;
  11022. A_AND:
  11023. if (taicpu(hp1).oper[0]^.typ = top_const) then
  11024. begin
  11025. { we might be able to go smaller if AND appears first }
  11026. if InstrMax = -1 then
  11027. case MinSize of
  11028. S_B:
  11029. ;
  11030. S_W:
  11031. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  11032. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  11033. begin
  11034. TryShiftDown := S_B;
  11035. TryShiftDownLimit := $FF;
  11036. end;
  11037. S_L:
  11038. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  11039. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  11040. begin
  11041. TryShiftDown := S_B;
  11042. TryShiftDownLimit := $FF;
  11043. end
  11044. else if ((taicpu(hp1).oper[0]^.val and $FFFF) = taicpu(hp1).oper[0]^.val) or
  11045. ((not(taicpu(hp1).oper[0]^.val) and $7FFF) = (not taicpu(hp1).oper[0]^.val)) then
  11046. begin
  11047. TryShiftDown := S_W;
  11048. TryShiftDownLimit := $FFFF;
  11049. end;
  11050. else
  11051. InternalError(2020112320);
  11052. end;
  11053. WorkingValue := taicpu(hp1).oper[0]^.val;
  11054. TestValMin := TestValMin and WorkingValue;
  11055. TestValMax := TestValMax and WorkingValue;
  11056. TestValSignedMax := TestValSignedMax and WorkingValue;
  11057. end;
  11058. A_OR:
  11059. begin
  11060. if not BitwiseOnly then
  11061. Break;
  11062. OrXorUsed := True;
  11063. WorkingValue := taicpu(hp1).oper[0]^.val;
  11064. TestValMin := TestValMin or WorkingValue;
  11065. TestValMax := TestValMax or WorkingValue;
  11066. TestValSignedMax := TestValSignedMax or WorkingValue;
  11067. end;
  11068. A_XOR:
  11069. begin
  11070. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  11071. begin
  11072. TestValMin := 0;
  11073. TestValMax := 0;
  11074. TestValSignedMax := 0;
  11075. end
  11076. else
  11077. begin
  11078. if not BitwiseOnly then
  11079. Break;
  11080. OrXorUsed := True;
  11081. WorkingValue := taicpu(hp1).oper[0]^.val;
  11082. TestValMin := TestValMin xor WorkingValue;
  11083. TestValMax := TestValMax xor WorkingValue;
  11084. TestValSignedMax := TestValSignedMax xor WorkingValue;
  11085. end;
  11086. end;
  11087. A_SHL:
  11088. begin
  11089. BitwiseOnly := False;
  11090. WorkingValue := taicpu(hp1).oper[0]^.val;
  11091. TestValMin := TestValMin shl WorkingValue;
  11092. TestValMax := TestValMax shl WorkingValue;
  11093. TestValSignedMax := TestValSignedMax shl WorkingValue;
  11094. end;
  11095. A_SHR,
  11096. { The first instruction was MOVZX, so the value won't be negative }
  11097. A_SAR:
  11098. begin
  11099. if InstrMax <> -1 then
  11100. BitwiseOnly := False
  11101. else
  11102. { we might be able to go smaller if SHR appears first }
  11103. case MinSize of
  11104. S_B:
  11105. ;
  11106. S_W:
  11107. if (taicpu(hp1).oper[0]^.val >= 8) then
  11108. begin
  11109. TryShiftDown := S_B;
  11110. TryShiftDownLimit := $FF;
  11111. TryShiftDownSignedLimit := $7F;
  11112. TryShiftDownSignedLimitLower := -128;
  11113. end;
  11114. S_L:
  11115. if (taicpu(hp1).oper[0]^.val >= 24) then
  11116. begin
  11117. TryShiftDown := S_B;
  11118. TryShiftDownLimit := $FF;
  11119. TryShiftDownSignedLimit := $7F;
  11120. TryShiftDownSignedLimitLower := -128;
  11121. end
  11122. else if (taicpu(hp1).oper[0]^.val >= 16) then
  11123. begin
  11124. TryShiftDown := S_W;
  11125. TryShiftDownLimit := $FFFF;
  11126. TryShiftDownSignedLimit := $7FFF;
  11127. TryShiftDownSignedLimitLower := -32768;
  11128. end;
  11129. else
  11130. InternalError(2020112321);
  11131. end;
  11132. WorkingValue := taicpu(hp1).oper[0]^.val;
  11133. if taicpu(hp1).opcode = A_SAR then
  11134. begin
  11135. TestValMin := SarInt64(TestValMin, WorkingValue);
  11136. TestValMax := SarInt64(TestValMax, WorkingValue);
  11137. TestValSignedMax := SarInt64(TestValSignedMax, WorkingValue);
  11138. end
  11139. else
  11140. begin
  11141. TestValMin := TestValMin shr WorkingValue;
  11142. TestValMax := TestValMax shr WorkingValue;
  11143. TestValSignedMax := TestValSignedMax shr WorkingValue;
  11144. end;
  11145. end;
  11146. else
  11147. InternalError(2020112303);
  11148. end;
  11149. end;
  11150. (*
  11151. A_IMUL:
  11152. case taicpu(hp1).ops of
  11153. 2:
  11154. begin
  11155. if not MatchOpType(hp1, top_reg, top_reg) or
  11156. { Has to be an exact match on the register }
  11157. (taicpu(hp1).oper[0]^.reg <> ThisReg) or
  11158. (taicpu(hp1).oper[1]^.reg <> ThisReg) then
  11159. Break;
  11160. TestValMin := TestValMin * TestValMin;
  11161. TestValMax := TestValMax * TestValMax;
  11162. TestValSignedMax := TestValSignedMax * TestValMax;
  11163. end;
  11164. 3:
  11165. begin
  11166. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  11167. { Has to be an exact match on the register }
  11168. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  11169. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  11170. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  11171. { Is it in the negative range? }
  11172. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  11173. Break;
  11174. TestValMin := TestValMin * taicpu(hp1).oper[0]^.val;
  11175. TestValMax := TestValMax * taicpu(hp1).oper[0]^.val;
  11176. TestValSignedMax := TestValSignedMax * taicpu(hp1).oper[0]^.val;
  11177. end;
  11178. else
  11179. Break;
  11180. end;
  11181. A_IDIV:
  11182. case taicpu(hp1).ops of
  11183. 3:
  11184. begin
  11185. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  11186. { Has to be an exact match on the register }
  11187. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  11188. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  11189. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  11190. { Is it in the negative range? }
  11191. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  11192. Break;
  11193. TestValMin := TestValMin div taicpu(hp1).oper[0]^.val;
  11194. TestValMax := TestValMax div taicpu(hp1).oper[0]^.val;
  11195. TestValSignedMax := TestValSignedMax div taicpu(hp1).oper[0]^.val;
  11196. end;
  11197. else
  11198. Break;
  11199. end;
  11200. *)
  11201. A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}:
  11202. begin
  11203. { If there are no instructions in between, then we might be able to make a saving }
  11204. if UpperSignedOverflow or (taicpu(hp1).oper[0]^.typ <> top_reg) or (taicpu(hp1).oper[0]^.reg <> ThisReg) then
  11205. Break;
  11206. { We have something like:
  11207. movzbw %dl,%dx
  11208. ...
  11209. movswl %dx,%edx
  11210. Change the latter to a zero-extension then enter the
  11211. A_MOVZX case branch.
  11212. }
  11213. {$ifdef x86_64}
  11214. if (taicpu(hp1).opsize = S_LQ) and SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) then
  11215. begin
  11216. { this becomes a zero extension from 32-bit to 64-bit, but
  11217. the upper 32 bits are already zero, so just delete the
  11218. instruction }
  11219. DebugMsg(SPeepholeOptimization + 'MovzMovsxd2MovzNop', hp1);
  11220. RemoveInstruction(hp1);
  11221. Result := True;
  11222. Exit;
  11223. end
  11224. else
  11225. {$endif x86_64}
  11226. begin
  11227. DebugMsg(SPeepholeOptimization + 'MovzMovs2MovzMovz', hp1);
  11228. taicpu(hp1).opcode := A_MOVZX;
  11229. {$ifdef x86_64}
  11230. case taicpu(hp1).opsize of
  11231. S_BQ:
  11232. begin
  11233. taicpu(hp1).opsize := S_BL;
  11234. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  11235. end;
  11236. S_WQ:
  11237. begin
  11238. taicpu(hp1).opsize := S_WL;
  11239. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  11240. end;
  11241. S_LQ:
  11242. begin
  11243. taicpu(hp1).opcode := A_MOV;
  11244. taicpu(hp1).opsize := S_L;
  11245. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  11246. { In this instance, we need to break out because the
  11247. instruction is no longer MOVZX or MOVSXD }
  11248. Result := True;
  11249. Exit;
  11250. end;
  11251. else
  11252. ;
  11253. end;
  11254. {$endif x86_64}
  11255. Result := CompressInstructions;
  11256. Exit;
  11257. end;
  11258. end;
  11259. A_MOVZX:
  11260. begin
  11261. if UpperUnsignedOverflow or (taicpu(hp1).oper[0]^.typ <> top_reg) then
  11262. Break;
  11263. if (InstrMax = -1) then
  11264. begin
  11265. if SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, ThisReg) then
  11266. begin
  11267. { Optimise around i40003 }
  11268. { Check to see if the active register is used afterwards }
  11269. TransferUsedRegs(TmpUsedRegs);
  11270. IncludeRegInUsedRegs(ThisReg, TmpUsedRegs);
  11271. if (
  11272. SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) or
  11273. not RegUsedAfterInstruction(ThisReg, hp1, TmpUsedRegs)
  11274. ) and
  11275. (taicpu(p).opsize = S_WL) and (taicpu(hp1).opsize = S_BL)
  11276. {$ifndef x86_64}
  11277. and (
  11278. (taicpu(p).oper[0]^.typ <> top_reg) or
  11279. { Cannot encode byte-sized ESI, EDI, EBP or ESP under i386 }
  11280. (GetSupReg(taicpu(p).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])
  11281. )
  11282. {$endif not x86_64}
  11283. then
  11284. begin
  11285. if (taicpu(p).oper[0]^.typ = top_reg) then
  11286. setsubreg(taicpu(p).oper[0]^.reg, R_SUBL);
  11287. DebugMsg(SPeepholeOptimization + 'movzwl2movzbl 1', p);
  11288. taicpu(p).opsize := S_BL;
  11289. { Only remove if the active register is overwritten }
  11290. if SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) then
  11291. begin
  11292. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 2a', hp1);
  11293. RemoveInstruction(hp1);
  11294. end;
  11295. Result := True;
  11296. Exit;
  11297. end;
  11298. end
  11299. else
  11300. begin
  11301. { Will return false if the second parameter isn't ThisReg
  11302. (can happen on -O2 and under) }
  11303. if Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, ThisReg) then
  11304. begin
  11305. { The two MOVZX instructions are adjacent, so remove the first one }
  11306. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 5', p);
  11307. RemoveCurrentP(p);
  11308. Result := True;
  11309. Exit;
  11310. end;
  11311. Break;
  11312. end;
  11313. end;
  11314. Result := CompressInstructions;
  11315. Exit;
  11316. end;
  11317. else
  11318. { This includes ADC, SBB and IDIV }
  11319. Break;
  11320. end;
  11321. if not CheckOverflowConditions then
  11322. Break;
  11323. { Contains highest index (so instruction count - 1) }
  11324. Inc(InstrMax);
  11325. if InstrMax > High(InstrList) then
  11326. SetLength(InstrList, InstrMax + LIST_STEP_SIZE);
  11327. InstrList[InstrMax] := taicpu(hp1);
  11328. end;
  11329. end;
  11330. {$pop}
  11331. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  11332. var
  11333. hp1 : tai;
  11334. begin
  11335. Result:=false;
  11336. if (taicpu(p).ops >= 2) and
  11337. ((taicpu(p).oper[0]^.typ = top_const) or
  11338. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  11339. (taicpu(p).oper[1]^.typ = top_reg) and
  11340. ((taicpu(p).ops = 2) or
  11341. ((taicpu(p).oper[2]^.typ = top_reg) and
  11342. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  11343. GetLastInstruction(p,hp1) and
  11344. MatchInstruction(hp1,A_MOV,[]) and
  11345. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  11346. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  11347. begin
  11348. TransferUsedRegs(TmpUsedRegs);
  11349. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) or
  11350. ((taicpu(p).ops = 3) and (taicpu(p).oper[1]^.reg=taicpu(p).oper[2]^.reg)) then
  11351. { change
  11352. mov reg1,reg2
  11353. imul y,reg2 to imul y,reg1,reg2 }
  11354. begin
  11355. taicpu(p).ops := 3;
  11356. taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
  11357. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  11358. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  11359. RemoveInstruction(hp1);
  11360. result:=true;
  11361. end;
  11362. end;
  11363. end;
  11364. procedure TX86AsmOptimizer.ConvertJumpToRET(const p: tai; const ret_p: tai);
  11365. var
  11366. ThisLabel: TAsmLabel;
  11367. begin
  11368. ThisLabel := tasmlabel(taicpu(p).oper[0]^.ref^.symbol);
  11369. ThisLabel.decrefs;
  11370. taicpu(p).condition := C_None;
  11371. taicpu(p).opcode := A_RET;
  11372. taicpu(p).is_jmp := false;
  11373. taicpu(p).ops := taicpu(ret_p).ops;
  11374. case taicpu(ret_p).ops of
  11375. 0:
  11376. taicpu(p).clearop(0);
  11377. 1:
  11378. taicpu(p).loadconst(0,taicpu(ret_p).oper[0]^.val);
  11379. else
  11380. internalerror(2016041301);
  11381. end;
  11382. { If the original label is now dead, it might turn out that the label
  11383. immediately follows p. As a result, everything beyond it, which will
  11384. be just some final register configuration and a RET instruction, is
  11385. now dead code. [Kit] }
  11386. { NOTE: This is much faster than introducing a OptPass2RET routine and
  11387. running RemoveDeadCodeAfterJump for each RET instruction, because
  11388. this optimisation rarely happens and most RETs appear at the end of
  11389. routines where there is nothing that can be stripped. [Kit] }
  11390. if not ThisLabel.is_used then
  11391. RemoveDeadCodeAfterJump(p);
  11392. end;
  11393. function TX86AsmOptimizer.OptPass2SETcc(var p: tai): boolean;
  11394. var
  11395. hp1,hp2,next: tai; SetC, JumpC: TAsmCond;
  11396. Unconditional, PotentialModified: Boolean;
  11397. OperPtr: POper;
  11398. NewRef: TReference;
  11399. InstrList: array of taicpu;
  11400. InstrMax, Index: Integer;
  11401. const
  11402. {$ifdef DEBUG_AOPTCPU}
  11403. SNoFlags: shortstring = ' so the flags aren''t modified';
  11404. {$else DEBUG_AOPTCPU}
  11405. SNoFlags = '';
  11406. {$endif DEBUG_AOPTCPU}
  11407. begin
  11408. Result:=false;
  11409. if MatchOpType(taicpu(p),top_reg) and GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  11410. begin
  11411. if MatchInstruction(hp1, A_TEST, [S_B]) and
  11412. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  11413. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  11414. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  11415. GetNextInstruction(hp1, hp2) and
  11416. MatchInstruction(hp2, A_Jcc, A_SETcc, []) then
  11417. { Change from: To:
  11418. set(C) %reg j(~C) label
  11419. test %reg,%reg/cmp $0,%reg
  11420. je label
  11421. set(C) %reg j(C) label
  11422. test %reg,%reg/cmp $0,%reg
  11423. jne label
  11424. (Also do something similar with sete/setne instead of je/jne)
  11425. }
  11426. begin
  11427. { Before we do anything else, we need to check the instructions
  11428. in between SETcc and TEST to make sure they don't modify the
  11429. FLAGS register - if -O2 or under, there won't be any
  11430. instructions between SET and TEST }
  11431. TransferUsedRegs(TmpUsedRegs);
  11432. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  11433. if (cs_opt_level3 in current_settings.optimizerswitches) then
  11434. begin
  11435. next := p;
  11436. SetLength(InstrList, 0);
  11437. InstrMax := -1;
  11438. PotentialModified := False;
  11439. { Make a note of every instruction that modifies the FLAGS
  11440. register }
  11441. while GetNextInstruction(next, next) and (next <> hp1) do
  11442. begin
  11443. if next.typ <> ait_instruction then
  11444. { GetNextInstructionUsingReg should have returned False }
  11445. InternalError(2021051701);
  11446. if RegModifiedByInstruction(NR_DEFAULTFLAGS, next) then
  11447. begin
  11448. case taicpu(next).opcode of
  11449. A_SETcc,
  11450. A_CMOVcc,
  11451. A_Jcc:
  11452. begin
  11453. if PotentialModified then
  11454. { Not safe because the flags were modified earlier }
  11455. Exit
  11456. else
  11457. { Condition is the same as the initial SETcc, so this is safe
  11458. (don't add to instruction list though) }
  11459. Continue;
  11460. end;
  11461. A_ADD:
  11462. begin
  11463. if { LEA doesn't support 8-bit in general and 16-bit on x86-64 operands }
  11464. (taicpu(next).opsize in [S_B{$ifdef x86_64},S_W{$endif x86_64}]) or
  11465. (taicpu(next).oper[1]^.typ <> top_reg) or
  11466. { Must write to a register }
  11467. (taicpu(next).oper[0]^.typ = top_ref) then
  11468. { Require a constant or a register }
  11469. Exit;
  11470. PotentialModified := True;
  11471. end;
  11472. A_SUB:
  11473. begin
  11474. if { LEA doesn't support 8-bit in general and 16-bit on x86-64 operands }
  11475. (taicpu(next).opsize in [S_B{$ifdef x86_64},S_W{$endif x86_64}]) or
  11476. (taicpu(next).oper[1]^.typ <> top_reg) or
  11477. { Must write to a register }
  11478. (taicpu(next).oper[0]^.typ <> top_const) or
  11479. (taicpu(next).oper[0]^.val = $80000000) then
  11480. { Can't subtract a register with LEA - also
  11481. check that the value isn't -2^31, as this
  11482. can't be negated }
  11483. Exit;
  11484. PotentialModified := True;
  11485. end;
  11486. A_SAL,
  11487. A_SHL:
  11488. begin
  11489. if { LEA doesn't support 8-bit in general and 16-bit on x86-64 operands }
  11490. (taicpu(next).opsize in [S_B{$ifdef x86_64},S_W{$endif x86_64}]) or
  11491. (taicpu(next).oper[1]^.typ <> top_reg) or
  11492. { Must write to a register }
  11493. (taicpu(next).oper[0]^.typ <> top_const) or
  11494. (taicpu(next).oper[0]^.val < 0) or
  11495. (taicpu(next).oper[0]^.val > 3) then
  11496. Exit;
  11497. PotentialModified := True;
  11498. end;
  11499. A_IMUL:
  11500. begin
  11501. if (taicpu(next).ops <> 3) or
  11502. (taicpu(next).oper[1]^.typ <> top_reg) or
  11503. { Must write to a register }
  11504. (taicpu(next).oper[2]^.val in [2,3,4,5,8,9]) then
  11505. { We can convert "imul x,%reg1,%reg2" (where x = 2, 4 or 8)
  11506. to "lea (%reg1,x),%reg2". If x = 3, 5 or 9, we can
  11507. change this to "lea (%reg1,%reg1,(x-1)),%reg2" }
  11508. Exit
  11509. else
  11510. PotentialModified := True;
  11511. end;
  11512. else
  11513. { Don't know how to change this, so abort }
  11514. Exit;
  11515. end;
  11516. { Contains highest index (so instruction count - 1) }
  11517. Inc(InstrMax);
  11518. if InstrMax > High(InstrList) then
  11519. SetLength(InstrList, InstrMax + LIST_STEP_SIZE);
  11520. InstrList[InstrMax] := taicpu(next);
  11521. end;
  11522. UpdateUsedRegs(TmpUsedRegs, tai(next.next));
  11523. end;
  11524. if not Assigned(next) or (next <> hp1) then
  11525. { It should be equal to hp1 }
  11526. InternalError(2021051702);
  11527. { Cycle through each instruction and check to see if we can
  11528. change them to versions that don't modify the flags }
  11529. if (InstrMax >= 0) then
  11530. begin
  11531. for Index := 0 to InstrMax do
  11532. case InstrList[Index].opcode of
  11533. A_ADD:
  11534. begin
  11535. DebugMsg(SPeepholeOptimization + 'ADD -> LEA' + SNoFlags, InstrList[Index]);
  11536. InstrList[Index].opcode := A_LEA;
  11537. reference_reset(NewRef, 1, []);
  11538. NewRef.base := InstrList[Index].oper[1]^.reg;
  11539. if InstrList[Index].oper[0]^.typ = top_reg then
  11540. begin
  11541. NewRef.index := InstrList[Index].oper[0]^.reg;
  11542. NewRef.scalefactor := 1;
  11543. end
  11544. else
  11545. NewRef.offset := InstrList[Index].oper[0]^.val;
  11546. InstrList[Index].loadref(0, NewRef);
  11547. end;
  11548. A_SUB:
  11549. begin
  11550. DebugMsg(SPeepholeOptimization + 'SUB -> LEA' + SNoFlags, InstrList[Index]);
  11551. InstrList[Index].opcode := A_LEA;
  11552. reference_reset(NewRef, 1, []);
  11553. NewRef.base := InstrList[Index].oper[1]^.reg;
  11554. NewRef.offset := -InstrList[Index].oper[0]^.val;
  11555. InstrList[Index].loadref(0, NewRef);
  11556. end;
  11557. A_SHL,
  11558. A_SAL:
  11559. begin
  11560. DebugMsg(SPeepholeOptimization + 'SHL -> LEA' + SNoFlags, InstrList[Index]);
  11561. InstrList[Index].opcode := A_LEA;
  11562. reference_reset(NewRef, 1, []);
  11563. NewRef.index := InstrList[Index].oper[1]^.reg;
  11564. NewRef.scalefactor := 1 shl (InstrList[Index].oper[0]^.val);
  11565. InstrList[Index].loadref(0, NewRef);
  11566. end;
  11567. A_IMUL:
  11568. begin
  11569. DebugMsg(SPeepholeOptimization + 'IMUL -> LEA' + SNoFlags, InstrList[Index]);
  11570. InstrList[Index].opcode := A_LEA;
  11571. reference_reset(NewRef, 1, []);
  11572. NewRef.index := InstrList[Index].oper[1]^.reg;
  11573. case InstrList[Index].oper[0]^.val of
  11574. 2, 4, 8:
  11575. NewRef.scalefactor := InstrList[Index].oper[0]^.val;
  11576. else {3, 5 and 9}
  11577. begin
  11578. NewRef.scalefactor := InstrList[Index].oper[0]^.val - 1;
  11579. NewRef.base := InstrList[Index].oper[1]^.reg;
  11580. end;
  11581. end;
  11582. InstrList[Index].loadref(0, NewRef);
  11583. end;
  11584. else
  11585. InternalError(2021051710);
  11586. end;
  11587. end;
  11588. { Mark the FLAGS register as used across this whole block }
  11589. AllocRegBetween(NR_DEFAULTFLAGS, p, hp1, UsedRegs);
  11590. end;
  11591. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  11592. JumpC := taicpu(hp2).condition;
  11593. Unconditional := False;
  11594. if conditions_equal(JumpC, C_E) then
  11595. SetC := inverse_cond(taicpu(p).condition)
  11596. else if conditions_equal(JumpC, C_NE) then
  11597. SetC := taicpu(p).condition
  11598. else
  11599. { We've got something weird here (and inefficent) }
  11600. begin
  11601. DebugMsg('DEBUG: Inefficient jump - check code generation', p);
  11602. SetC := C_NONE;
  11603. { JAE/JNB will always branch (use 'condition_in', since C_AE <> C_NB normally) }
  11604. if condition_in(C_AE, JumpC) then
  11605. Unconditional := True
  11606. else
  11607. { Not sure what to do with this jump - drop out }
  11608. Exit;
  11609. end;
  11610. RemoveInstruction(hp1);
  11611. if Unconditional then
  11612. MakeUnconditional(taicpu(hp2))
  11613. else
  11614. begin
  11615. if SetC = C_NONE then
  11616. InternalError(2018061402);
  11617. taicpu(hp2).SetCondition(SetC);
  11618. end;
  11619. { as hp2 is a jump, we cannot use RegUsedAfterInstruction but we have to check if it is included in
  11620. TmpUsedRegs }
  11621. if not TmpUsedRegs[getregtype(taicpu(p).oper[0]^.reg)].IsUsed(taicpu(p).oper[0]^.reg) then
  11622. begin
  11623. RemoveCurrentp(p, hp2);
  11624. if taicpu(hp2).opcode = A_SETcc then
  11625. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/SETcc -> SETcc',p)
  11626. else
  11627. begin
  11628. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> Jcc',p);
  11629. if (cs_opt_level3 in current_settings.optimizerswitches) then
  11630. Include(OptsToCheck, aoc_DoPass2JccOpts);
  11631. end;
  11632. end
  11633. else
  11634. if taicpu(hp2).opcode = A_SETcc then
  11635. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/SETcc -> SETcc/SETcc',p)
  11636. else
  11637. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> SETcc/Jcc',p);
  11638. Result := True;
  11639. end
  11640. else if
  11641. { Make sure the instructions are adjacent }
  11642. (
  11643. not (cs_opt_level3 in current_settings.optimizerswitches) or
  11644. GetNextInstruction(p, hp1)
  11645. ) and
  11646. MatchInstruction(hp1, A_MOV, [S_B]) and
  11647. { Writing to memory is allowed }
  11648. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) then
  11649. begin
  11650. {
  11651. Watch out for sequences such as:
  11652. set(c)b %regb
  11653. movb %regb,(ref)
  11654. movb $0,1(ref)
  11655. movb $0,2(ref)
  11656. movb $0,3(ref)
  11657. Much more efficient to turn it into:
  11658. movl $0,%regl
  11659. set(c)b %regb
  11660. movl %regl,(ref)
  11661. Or:
  11662. set(c)b %regb
  11663. movzbl %regb,%regl
  11664. movl %regl,(ref)
  11665. }
  11666. if (taicpu(hp1).oper[1]^.typ = top_ref) and
  11667. GetNextInstruction(hp1, hp2) and
  11668. MatchInstruction(hp2, A_MOV, [S_B]) and
  11669. (taicpu(hp2).oper[1]^.typ = top_ref) and
  11670. CheckMemoryWrite(taicpu(hp1), taicpu(hp2)) then
  11671. begin
  11672. { Don't do anything else except set Result to True }
  11673. end
  11674. else
  11675. begin
  11676. if taicpu(p).oper[0]^.typ = top_reg then
  11677. begin
  11678. TransferUsedRegs(TmpUsedRegs);
  11679. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  11680. end;
  11681. { If it's not a register, it's a memory address }
  11682. if (taicpu(p).oper[0]^.typ <> top_reg) or RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp1, TmpUsedRegs) then
  11683. begin
  11684. { Even if the register is still in use, we can minimise the
  11685. pipeline stall by changing the MOV into another SETcc. }
  11686. taicpu(hp1).opcode := A_SETcc;
  11687. taicpu(hp1).condition := taicpu(p).condition;
  11688. if taicpu(hp1).oper[1]^.typ = top_ref then
  11689. begin
  11690. { Swapping the operand pointers like this is probably a
  11691. bit naughty, but it is far faster than using loadoper
  11692. to transfer the reference from oper[1] to oper[0] if
  11693. you take into account the extra procedure calls and
  11694. the memory allocation and deallocation required }
  11695. OperPtr := taicpu(hp1).oper[1];
  11696. taicpu(hp1).oper[1] := taicpu(hp1).oper[0];
  11697. taicpu(hp1).oper[0] := OperPtr;
  11698. end
  11699. else
  11700. taicpu(hp1).oper[0]^.reg := taicpu(hp1).oper[1]^.reg;
  11701. taicpu(hp1).clearop(1);
  11702. taicpu(hp1).ops := 1;
  11703. DebugMsg(SPeepholeOptimization + 'SETcc/Mov -> SETcc/SETcc',p);
  11704. end
  11705. else
  11706. begin
  11707. if taicpu(hp1).oper[1]^.typ = top_reg then
  11708. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,UsedRegs);
  11709. taicpu(p).loadoper(0, taicpu(hp1).oper[1]^);
  11710. RemoveInstruction(hp1);
  11711. DebugMsg(SPeepholeOptimization + 'SETcc/Mov -> SETcc',p);
  11712. end
  11713. end;
  11714. Result := True;
  11715. end;
  11716. end;
  11717. end;
  11718. function TX86AsmOptimizer.TryCmpCMovOpts(var p, hp1: tai): Boolean;
  11719. var
  11720. hp2, pCond, pFirstMOV, pLastMOV, pCMOV: tai;
  11721. TargetReg: TRegister;
  11722. condition, inverted_condition: TAsmCond;
  11723. FoundMOV: Boolean;
  11724. begin
  11725. Result := False;
  11726. { In some situations, the CMOV optimisations in OptPass2Jcc can't
  11727. create the most optimial instructions possible due to limited
  11728. register availability, and there are situations where two
  11729. complementary "simple" CMOV blocks are created which, after the fact
  11730. can be merged into a "double" block. For example:
  11731. movw $257,%ax
  11732. movw $2,%r8w
  11733. xorl r9d,%r9d
  11734. testw $16,18(%rcx)
  11735. cmovew %ax,%dx
  11736. cmovew %r8w,%bx
  11737. cmovel %r9d,%r14d
  11738. movw $1283,%ax
  11739. movw $4,%r8w
  11740. movl $9,%r9d
  11741. cmovnew %ax,%dx
  11742. cmovnew %r8w,%bx
  11743. cmovnel %r9d,%r14d
  11744. The CMOVNE instructions at the end can be removed, and the
  11745. destination registers copied into the MOV instructions directly
  11746. above them, before finally being moved to before the first CMOVE
  11747. instructions, to produce:
  11748. movw $257,%ax
  11749. movw $2,%r8w
  11750. xorl r9d,%r9d
  11751. testw $16,18(%rcx)
  11752. movw $1283,%dx
  11753. movw $4,%bx
  11754. movl $9,%r14d
  11755. cmovew %ax,%dx
  11756. cmovew %r8w,%bx
  11757. cmovel %r9d,%r14d
  11758. Which can then be later optimised to:
  11759. movw $257,%ax
  11760. movw $2,%r8w
  11761. xorl r9d,%r9d
  11762. movw $1283,%dx
  11763. movw $4,%bx
  11764. movl $9,%r14d
  11765. testw $16,18(%rcx)
  11766. cmovew %ax,%dx
  11767. cmovew %r8w,%bx
  11768. cmovel %r9d,%r14d
  11769. }
  11770. TargetReg := taicpu(hp1).oper[1]^.reg;
  11771. condition := taicpu(hp1).condition;
  11772. inverted_condition := inverse_cond(condition);
  11773. pFirstMov := nil;
  11774. pLastMov := nil;
  11775. pCMOV := nil;
  11776. if (p.typ = ait_instruction) then
  11777. pCond := p
  11778. else if not GetNextInstruction(p, pCond) then
  11779. InternalError(2024012501);
  11780. if not MatchInstruction(pCond, A_CMP, A_TEST, []) then
  11781. { We should get the CMP or TEST instructeion }
  11782. InternalError(2024012502);
  11783. if (
  11784. (taicpu(hp1).oper[0]^.typ = top_reg) or
  11785. IsRefSafe(taicpu(hp1).oper[0]^.ref)
  11786. ) then
  11787. begin
  11788. { We have to tread carefully here, hence why we're not using
  11789. GetNextInstructionUsingReg... we can only accept MOV and other
  11790. CMOV instructions. Anything else and we must drop out}
  11791. hp2 := hp1;
  11792. while GetNextInstruction(hp2, hp2) and (hp2 <> BlockEnd) do
  11793. begin
  11794. if (hp2.typ <> ait_instruction) then
  11795. Exit;
  11796. case taicpu(hp2).opcode of
  11797. A_MOV:
  11798. begin
  11799. if not Assigned(pFirstMov) then
  11800. pFirstMov := hp2;
  11801. pLastMOV := hp2;
  11802. if not MatchOpType(taicpu(hp2), top_const, top_reg) then
  11803. { Something different - drop out }
  11804. Exit;
  11805. { Otherwise, leave it for now }
  11806. end;
  11807. A_CMOVcc:
  11808. begin
  11809. if taicpu(hp2).condition = inverted_condition then
  11810. begin
  11811. { We found what we're looking for }
  11812. if taicpu(hp2).oper[1]^.reg = TargetReg then
  11813. begin
  11814. if (taicpu(hp2).oper[0]^.typ = top_reg) or
  11815. IsRefSafe(taicpu(hp2).oper[0]^.ref) then
  11816. begin
  11817. pCMOV := hp2;
  11818. Break;
  11819. end
  11820. else
  11821. { Unsafe reference - drop out }
  11822. Exit;
  11823. end;
  11824. end
  11825. else if taicpu(hp2).condition <> condition then
  11826. { Something weird - drop out }
  11827. Exit;
  11828. end;
  11829. else
  11830. { Invalid }
  11831. Exit;
  11832. end;
  11833. end;
  11834. if not Assigned(pCMOV) then
  11835. { No complementary CMOV found }
  11836. Exit;
  11837. if not Assigned(pFirstMov) or (taicpu(pCMOV).oper[0]^.typ = top_ref) then
  11838. begin
  11839. { Don't need to do anything special or search for a matching MOV }
  11840. Asml.Remove(pCMOV);
  11841. if RegInInstruction(TargetReg, pCond) then
  11842. { Make sure we don't overwrite the register if it's being used in the condition }
  11843. Asml.InsertAfter(pCMOV, pCond)
  11844. else
  11845. Asml.InsertBefore(pCMOV, pCond);
  11846. taicpu(pCMOV).opcode := A_MOV;
  11847. taicpu(pCMOV).condition := C_None;
  11848. { Don't need to worry about allocating new registers in these cases }
  11849. DebugMsg(SPeepholeOptimization + 'CMovCMov2MovCMov 2', pCMOV);
  11850. Result := True;
  11851. Exit;
  11852. end
  11853. else
  11854. begin
  11855. DebugMsg(SPeepholeOptimization + 'CMovCMov2MovCMov 1', hp1);
  11856. FoundMOV := False;
  11857. { Search for the MOV that sets the target register }
  11858. hp2 := pFirstMov;
  11859. repeat
  11860. if (taicpu(hp2).opcode = A_MOV) and
  11861. (taicpu(hp2).oper[1]^.typ = top_reg) and
  11862. SuperRegistersEqual(taicpu(hp2).oper[1]^.reg, taicpu(pCMOV).oper[0]^.reg) then
  11863. begin
  11864. { Change the destination }
  11865. taicpu(hp2).loadreg(1, newreg(R_INTREGISTER, getsupreg(TargetReg), getsubreg(taicpu(hp2).oper[1]^.reg)));
  11866. if not FoundMOV then
  11867. begin
  11868. FoundMOV := True;
  11869. { Make sure the register is allocated }
  11870. AllocRegBetween(TargetReg, p, hp2, UsedRegs);
  11871. end;
  11872. hp1 := tai(hp2.Previous);
  11873. Asml.Remove(hp2);
  11874. if RegInInstruction(TargetReg, pCond) then
  11875. { Make sure we don't overwrite the register if it's being used in the condition }
  11876. Asml.InsertAfter(hp2, pCond)
  11877. else
  11878. Asml.InsertBefore(hp2, pCond);
  11879. if (hp2 = pLastMov) then
  11880. { If the MOV instruction is the last one, "hp2 = pLastMOV" won't trigger }
  11881. Break;
  11882. hp2 := hp1;
  11883. end;
  11884. until (hp2 = pLastMOV) or not GetNextInstruction(hp2, hp2) or (hp2 = BlockEnd) or (hp2.typ <> ait_instruction);
  11885. if FoundMOV then
  11886. { Delete the CMOV }
  11887. RemoveInstruction(pCMOV)
  11888. else
  11889. begin
  11890. { If no MOV was found, we have to actually move and transmute the CMOV }
  11891. Asml.Remove(pCMOV);
  11892. if RegInInstruction(TargetReg, pCond) then
  11893. { Make sure we don't overwrite the register if it's being used in the condition }
  11894. Asml.InsertAfter(pCMOV, pCond)
  11895. else
  11896. Asml.InsertBefore(pCMOV, pCond);
  11897. taicpu(pCMOV).opcode := A_MOV;
  11898. taicpu(pCMOV).condition := C_None;
  11899. end;
  11900. Result := True;
  11901. Exit;
  11902. end;
  11903. end;
  11904. end;
  11905. function TX86AsmOptimizer.OptPass2Cmp(var p: tai): Boolean;
  11906. var
  11907. hp1, hp2, pCond: tai;
  11908. begin
  11909. Result := False;
  11910. { Search ahead for CMOV instructions }
  11911. if (cs_opt_level2 in current_settings.optimizerswitches) then
  11912. begin
  11913. hp1 := p;
  11914. hp2 := p;
  11915. pCond := nil; { To prevent compiler warnings }
  11916. { For TryCmpCMOVOpts, try to insert MOVs before the allocation of
  11917. DEFAULTFLAGS }
  11918. if not SetAndTest(FindRegAllocBackward(NR_DEFAULTFLAGS, p), pCond) or
  11919. (tai_regalloc(pCond).ratype = ra_dealloc) then
  11920. pCond := p;
  11921. while GetNextInstruction(hp1, hp1) and (hp1 <> BlockEnd) do
  11922. begin
  11923. if (hp1.typ <> ait_instruction) then
  11924. { Break out on markers and labels etc. }
  11925. Break;
  11926. case taicpu(hp1).opcode of
  11927. A_MOV:
  11928. { Ignore regular MOVs unless they are obviously not related
  11929. to a CMOV block }
  11930. if taicpu(hp1).oper[1]^.typ <> top_reg then
  11931. Break;
  11932. A_CMOVcc:
  11933. if TryCmpCMovOpts(pCond, hp1) then
  11934. begin
  11935. hp1 := hp2;
  11936. { p itself isn't changed, and we're still inside a
  11937. while loop to catch subsequent CMOVs, so just flag
  11938. a new iteration }
  11939. Include(OptsToCheck, aoc_ForceNewIteration);
  11940. Continue;
  11941. end;
  11942. else
  11943. { Drop out if we find anything else }
  11944. Break;
  11945. end;
  11946. hp2 := hp1;
  11947. end;
  11948. end;
  11949. end;
  11950. function TX86AsmOptimizer.OptPass2Test(var p: tai): Boolean;
  11951. var
  11952. hp1, hp2, pCond: tai;
  11953. SourceReg, TargetReg: TRegister;
  11954. begin
  11955. Result := False;
  11956. { In some situations, we end up with an inefficient arrangement of
  11957. instructions in the form of:
  11958. or %reg1,%reg2
  11959. (%reg1 deallocated)
  11960. test %reg2,%reg2
  11961. mov x,%reg2
  11962. we may be able to swap and rearrange the registers to produce:
  11963. or %reg2,%reg1
  11964. mov x,%reg2
  11965. test %reg1,%reg1
  11966. (%reg1 deallocated)
  11967. }
  11968. if (cs_opt_level3 in current_settings.optimizerswitches) and
  11969. (taicpu(p).oper[1]^.typ = top_reg) and
  11970. (
  11971. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^.reg) or
  11972. MatchOperand(taicpu(p).oper[0]^, -1)
  11973. ) and
  11974. GetNextInstruction(p, hp1) and
  11975. MatchInstruction(hp1, A_MOV, []) and
  11976. (taicpu(hp1).oper[1]^.typ = top_reg) and
  11977. SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[1]^.reg) then
  11978. begin
  11979. TargetReg := taicpu(p).oper[1]^.reg;
  11980. { Now look backwards to find a simple commutative operation: ADD,
  11981. IMUL (2-register version), OR, AND or XOR - whose destination
  11982. register is the same as TEST }
  11983. hp2 := p;
  11984. while GetLastInstruction(hp2, hp2) and (hp2.typ = ait_instruction) do
  11985. if RegInInstruction(TargetReg, hp2) then
  11986. begin
  11987. if MatchInstruction(hp2, [A_ADD, A_IMUL, A_OR, A_AND, A_XOR], [taicpu(p).opsize]) and
  11988. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  11989. (taicpu(hp2).oper[1]^.reg = TargetReg) and
  11990. (taicpu(hp2).oper[0]^.reg <> TargetReg) then
  11991. begin
  11992. SourceReg := taicpu(hp2).oper[0]^.reg;
  11993. if
  11994. { Make sure the MOV doesn't use the other register }
  11995. not RegInOp(SourceReg, taicpu(hp1).oper[0]^) and
  11996. { And make sure the source register is not used afterwards }
  11997. not RegInUsedRegs(SourceReg, UsedRegs) then
  11998. begin
  11999. DebugMsg(SPeepholeOptimization + 'OpTest2OpTest (register swap) done', hp2);
  12000. taicpu(hp2).oper[0]^.reg := TargetReg;
  12001. taicpu(hp2).oper[1]^.reg := SourceReg;
  12002. if taicpu(p).oper[0]^.typ = top_reg then
  12003. taicpu(p).oper[0]^.reg := SourceReg;
  12004. taicpu(p).oper[1]^.reg := SourceReg;
  12005. IncludeRegInUsedRegs(SourceReg, UsedRegs);
  12006. AllocRegBetween(SourceReg, hp2, p, UsedRegs);
  12007. Include(OptsToCheck, aoc_ForceNewIteration);
  12008. { We can still check the following optimisations since
  12009. the instruction is still a TEST }
  12010. end;
  12011. end;
  12012. Break;
  12013. end;
  12014. end;
  12015. { Search ahead3 for CMOV instructions }
  12016. if (cs_opt_level2 in current_settings.optimizerswitches) then
  12017. begin
  12018. hp1 := p;
  12019. hp2 := p;
  12020. pCond := nil; { To prevent compiler warnings }
  12021. { For TryCmpCMOVOpts, try to insert MOVs before the allocation of
  12022. DEFAULTFLAGS }
  12023. if not SetAndTest(FindRegAllocBackward(NR_DEFAULTFLAGS, p), pCond) or
  12024. (tai_regalloc(pCond).ratype = ra_dealloc) then
  12025. pCond := p;
  12026. while GetNextInstruction(hp1, hp1) and (hp1 <> BlockEnd) do
  12027. begin
  12028. if (hp1.typ <> ait_instruction) then
  12029. { Break out on markers and labels etc. }
  12030. Break;
  12031. case taicpu(hp1).opcode of
  12032. A_MOV:
  12033. { Ignore regular MOVs unless they are obviously not related
  12034. to a CMOV block }
  12035. if taicpu(hp1).oper[1]^.typ <> top_reg then
  12036. Break;
  12037. A_CMOVcc:
  12038. if TryCmpCMovOpts(pCond, hp1) then
  12039. begin
  12040. hp1 := hp2;
  12041. { p itself isn't changed, and we're still inside a
  12042. while loop to catch subsequent CMOVs, so just flag
  12043. a new iteration }
  12044. Include(OptsToCheck, aoc_ForceNewIteration);
  12045. Continue;
  12046. end;
  12047. else
  12048. { Drop out if we find anything else }
  12049. Break;
  12050. end;
  12051. hp2 := hp1;
  12052. end;
  12053. end;
  12054. end;
  12055. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  12056. var
  12057. hp1: tai;
  12058. Count: Integer;
  12059. OrigLabel: TAsmLabel;
  12060. begin
  12061. result := False;
  12062. { Sometimes, the optimisations below can permit this }
  12063. RemoveDeadCodeAfterJump(p);
  12064. 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
  12065. (taicpu(p).oper[0]^.ref^.index=NR_NO) and (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) then
  12066. begin
  12067. OrigLabel := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  12068. { Also a side-effect of optimisations }
  12069. if CollapseZeroDistJump(p, OrigLabel) then
  12070. begin
  12071. Result := True;
  12072. Exit;
  12073. end;
  12074. hp1 := GetLabelWithSym(OrigLabel);
  12075. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and (hp1.typ = ait_instruction) then
  12076. begin
  12077. if taicpu(hp1).opcode = A_RET then
  12078. begin
  12079. {
  12080. change
  12081. jmp .L1
  12082. ...
  12083. .L1:
  12084. ret
  12085. into
  12086. ret
  12087. }
  12088. begin
  12089. ConvertJumpToRET(p, hp1);
  12090. result:=true;
  12091. end;
  12092. end
  12093. else if (cs_opt_level3 in current_settings.optimizerswitches) and
  12094. not (cs_opt_size in current_settings.optimizerswitches) and
  12095. CheckJumpMovTransferOpt(p, hp1, 0, Count) then
  12096. begin
  12097. Result := True;
  12098. Exit;
  12099. end;
  12100. end;
  12101. end;
  12102. end;
  12103. class function TX86AsmOptimizer.CanBeCMOV(p, cond_p: tai; var RefModified: Boolean) : boolean;
  12104. begin
  12105. Result := assigned(p) and
  12106. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  12107. (taicpu(p).oper[1]^.typ = top_reg) and
  12108. (
  12109. (taicpu(p).oper[0]^.typ = top_reg) or
  12110. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  12111. it is not expected that this can cause a seg. violation }
  12112. (
  12113. (taicpu(p).oper[0]^.typ = top_ref) and
  12114. { TODO: Can we detect which references become constants at this
  12115. stage so we don't have to do a blanket ban? }
  12116. (taicpu(p).oper[0]^.ref^.refaddr <> addr_full) and
  12117. (
  12118. IsRefSafe(taicpu(p).oper[0]^.ref) or
  12119. (
  12120. { Don't use the reference in the condition if one of its registers got modified by a previous MOV }
  12121. not RefModified and
  12122. { If the reference also appears in the condition, then we know it's safe, otherwise
  12123. any kind of access violation would have occurred already }
  12124. Assigned(cond_p) and
  12125. { Make sure the sizes match too so we're reading and writing the same number of bytes }
  12126. (cond_p.typ = ait_instruction) and
  12127. (taicpu(cond_p).opsize = taicpu(p).opsize) and
  12128. { Just consider 2-operand comparison instructions for now to be safe }
  12129. (taicpu(cond_p).ops = 2) and
  12130. (
  12131. ((taicpu(cond_p).oper[1]^.typ = top_ref) and RefsEqual(taicpu(cond_p).oper[1]^.ref^, taicpu(p).oper[0]^.ref^)) or
  12132. (
  12133. (taicpu(cond_p).oper[0]^.typ = top_ref) and
  12134. { Don't risk identical registers but different offsets, as we may have constructs
  12135. such as buffer streams with things like length fields that indicate whether
  12136. any more data follows. And there are probably some contrived examples where
  12137. writing to offsets behind the one being read also lead to access violations }
  12138. RefsEqual(taicpu(cond_p).oper[0]^.ref^, taicpu(p).oper[0]^.ref^) and
  12139. (
  12140. { Check that we're not modifying a register that appears in the reference }
  12141. (InsProp[taicpu(cond_p).opcode].Ch * [Ch_Mop2, Ch_RWop2, Ch_Wop2] = []) or
  12142. (taicpu(cond_p).oper[1]^.typ <> top_reg) or
  12143. not RegInRef(taicpu(cond_p).oper[1]^.reg, taicpu(cond_p).oper[0]^.ref^)
  12144. )
  12145. )
  12146. )
  12147. )
  12148. )
  12149. )
  12150. );
  12151. end;
  12152. class procedure TX86AsmOptimizer.UpdateIntRegsNoDealloc(var AUsedRegs: TAllUsedRegs; p: Tai);
  12153. begin
  12154. { Update integer registers, ignoring deallocations }
  12155. repeat
  12156. while assigned(p) and
  12157. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  12158. (p.typ = ait_label) or
  12159. ((p.typ = ait_marker) and
  12160. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  12161. p := tai(p.next);
  12162. while assigned(p) and
  12163. (p.typ=ait_RegAlloc) Do
  12164. begin
  12165. if (getregtype(tai_regalloc(p).reg) = R_INTREGISTER) then
  12166. begin
  12167. case tai_regalloc(p).ratype of
  12168. ra_alloc :
  12169. IncludeRegInUsedRegs(tai_regalloc(p).reg, AUsedRegs);
  12170. else
  12171. ;
  12172. end;
  12173. end;
  12174. p := tai(p.next);
  12175. end;
  12176. until not(assigned(p)) or
  12177. (not(p.typ in SkipInstr) and
  12178. not((p.typ = ait_label) and
  12179. labelCanBeSkipped(tai_label(p))));
  12180. end;
  12181. {$ifndef 8086}
  12182. function TCMOVTracking.InitialiseBlock(BlockStart, OneBeforeBlock: tai; out BlockStop: tai; out EndJump: tai): Boolean;
  12183. begin
  12184. Result := False;
  12185. EndJump := nil;
  12186. BlockStop := nil;
  12187. while (BlockStart <> fOptimizer.BlockEnd) and
  12188. { stop on labels }
  12189. (BlockStart.typ <> ait_label) do
  12190. begin
  12191. { Keep track of all integer registers that are used }
  12192. fOptimizer.UpdateIntRegsNoDealloc(RegisterTracking, tai(OneBeforeBlock.Next));
  12193. if BlockStart.typ = ait_instruction then
  12194. begin
  12195. if (taicpu(BlockStart).opcode = A_JMP) then
  12196. begin
  12197. if not IsJumpToLabel(taicpu(BlockStart)) or
  12198. (JumpTargetOp(taicpu(BlockStart))^.ref^.index <> NR_NO) then
  12199. Exit;
  12200. EndJump := BlockStart;
  12201. Break;
  12202. end
  12203. { Check to see if we have a valid MOV instruction instead }
  12204. else if (taicpu(BlockStart).opcode <> A_MOV) or
  12205. (taicpu(BlockStart).oper[1]^.typ <> top_reg) or
  12206. not (taicpu(BlockStart).opsize in [S_W, S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) then
  12207. begin
  12208. Exit;
  12209. end
  12210. else
  12211. { This will be a valid MOV }
  12212. fAllocationRange := BlockStart;
  12213. end;
  12214. OneBeforeBlock := BlockStart;
  12215. fOptimizer.GetNextInstruction(BlockStart, BlockStart);
  12216. end;
  12217. if (BlockStart = fOptimizer.BlockEnd) then
  12218. Exit;
  12219. BlockStop := BlockStart;
  12220. Result := True;
  12221. end;
  12222. function TCMOVTracking.AnalyseMOVBlock(BlockStart, BlockStop, SearchStart: tai): LongInt;
  12223. var
  12224. hp1: tai;
  12225. RefModified: Boolean;
  12226. begin
  12227. Result := 0;
  12228. hp1 := BlockStart;
  12229. RefModified := False; { As long as the condition is inverted, this can be reset }
  12230. while assigned(hp1) and
  12231. (hp1 <> BlockStop) do
  12232. begin
  12233. case hp1.typ of
  12234. ait_instruction:
  12235. if MatchInstruction(hp1, A_MOV, [S_W, S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) then
  12236. begin
  12237. if fOptimizer.CanBeCMOV(hp1, fCondition, RefModified) then
  12238. begin
  12239. Inc(Result);
  12240. if { Make sure the sizes match too so we're reading and writing the same number of bytes }
  12241. Assigned(fCondition) and
  12242. { Will have 2 operands }
  12243. (
  12244. (
  12245. (taicpu(fCondition).oper[0]^.typ = top_ref) and
  12246. fOptimizer.RegInRef(taicpu(hp1).oper[1]^.reg, taicpu(fCondition).oper[0]^.ref^)
  12247. ) or
  12248. (
  12249. (taicpu(fCondition).oper[1]^.typ = top_ref) and
  12250. fOptimizer.RegInRef(taicpu(hp1).oper[1]^.reg, taicpu(fCondition).oper[1]^.ref^)
  12251. )
  12252. ) then
  12253. { It is no longer safe to use the reference in the condition.
  12254. this prevents problems such as:
  12255. mov (%reg),%reg
  12256. mov (%reg),...
  12257. When the comparison is cmp (%reg),0 and guarding against a null pointer deallocation
  12258. (fixes #40165)
  12259. Note: "mov (%reg1),%reg2; mov (%reg2),..." won't be optimised this way since
  12260. at least one of (%reg1) and (%reg2) won't be in the condition and is hence unsafe.
  12261. }
  12262. RefModified := True;
  12263. end
  12264. else if not (cs_opt_size in current_settings.optimizerswitches) and
  12265. { CMOV with constants grows the code size }
  12266. TryCMOVConst(hp1, SearchStart, BlockStop, Result) then
  12267. begin
  12268. { Register was reserved by TryCMOVConst and
  12269. stored on ConstRegs }
  12270. end
  12271. else
  12272. begin
  12273. Result := -1;
  12274. Exit;
  12275. end;
  12276. end
  12277. else
  12278. begin
  12279. Result := -1;
  12280. Exit;
  12281. end;
  12282. else
  12283. { Most likely an align };
  12284. end;
  12285. fOptimizer.GetNextInstruction(hp1, hp1);
  12286. end;
  12287. end;
  12288. constructor TCMOVTracking.Init(Optimizer: TX86AsmOptimizer; var p_initialjump, p_initialmov: tai; var AFirstLabel: TAsmLabel);
  12289. { For the tsBranching type, increase the weighting score to account for the new conditional jump
  12290. (this is done as a separate stage because the double types are extensions of the branching type,
  12291. but we can't discount the conditional jump until the last step) }
  12292. procedure EvaluateBranchingType;
  12293. begin
  12294. Inc(CMOVScore);
  12295. if (CMOVScore > MAX_CMOV_INSTRUCTIONS) then
  12296. { Too many instructions to be worthwhile }
  12297. fState := tsInvalid;
  12298. end;
  12299. var
  12300. hp1: tai;
  12301. Count: Integer;
  12302. begin
  12303. { Table of valid CMOV block types
  12304. Block type 2nd Jump Mid-label 2nd MOVs 3rd Jump End-label
  12305. ---------- --------- --------- --------- --------- ---------
  12306. tsSimple X Yes X X X
  12307. tsDetour = 1st X X X X
  12308. tsBranching <> Mid Yes X X X
  12309. tsDouble End-label Yes * Yes X Yes
  12310. tsDoubleBranchSame <> Mid Yes * Yes = 2nd X
  12311. tsDoubleBranchDifferent <> Mid Yes * Yes <> 2nd X
  12312. tsDoubleSecondBranching End-label Yes * Yes <> 2nd Yes
  12313. * Only one reference allowed
  12314. }
  12315. hp1 := nil; { To prevent compiler warnings }
  12316. Optimizer.CopyUsedRegs(RegisterTracking);
  12317. fOptimizer := Optimizer;
  12318. fLabel := AFirstLabel;
  12319. CMOVScore := 0;
  12320. ConstCount := 0;
  12321. { Initialise RegWrites, ConstRegs, ConstVals, ConstSizes, ConstWriteSizes and ConstMovs }
  12322. FillChar(RegWrites[0], MAX_CMOV_INSTRUCTIONS * 2 * SizeOf(TRegister), 0);
  12323. FillChar(ConstRegs[0], MAX_CMOV_REGISTERS * SizeOf(TRegister), 0);
  12324. FillChar(ConstVals[0], MAX_CMOV_REGISTERS * SizeOf(TCGInt), 0);
  12325. FillChar(ConstSizes[0], MAX_CMOV_REGISTERS * SizeOf(TSubRegister), 0);
  12326. FillChar(ConstWriteSizes[0], first_int_imreg * SizeOf(TOpSize), 0);
  12327. FillChar(ConstMovs[0], MAX_CMOV_REGISTERS * SizeOf(taicpu), 0);
  12328. fInsertionPoint := p_initialjump;
  12329. fCondition := nil;
  12330. fInitialJump := p_initialjump;
  12331. fFirstMovBlock := p_initialmov;
  12332. fFirstMovBlockStop := nil;
  12333. fSecondJump := nil;
  12334. fSecondMovBlock := nil;
  12335. fSecondMovBlockStop := nil;
  12336. fMidLabel := nil;
  12337. fSecondJump := nil;
  12338. fSecondMovBlock := nil;
  12339. fEndLabel := nil;
  12340. fAllocationRange := nil;
  12341. { Assume it all goes horribly wrong! }
  12342. fState := tsInvalid;
  12343. { Look backwards at the comparisons to get an accurate picture of register usage and a better position for any MOV const,reg insertions }
  12344. if Optimizer.GetLastInstruction(p_initialjump, fCondition) and
  12345. MatchInstruction(fCondition, [A_CMP, A_TEST, A_BSR, A_BSF, A_COMISS, A_COMISD, A_UCOMISS, A_UCOMISD, A_VCOMISS, A_VCOMISD, A_VUCOMISS, A_VUCOMISD], []) then
  12346. begin
  12347. { Mark all the registers in the comparison as 'in use', even if they've just been deallocated }
  12348. for Count := 0 to 1 do
  12349. with taicpu(fCondition).oper[Count]^ do
  12350. case typ of
  12351. top_reg:
  12352. if getregtype(reg) = R_INTREGISTER then
  12353. Optimizer.IncludeRegInUsedRegs(reg, RegisterTracking);
  12354. top_ref:
  12355. begin
  12356. if
  12357. {$ifdef x86_64}
  12358. (ref^.base <> NR_RIP) and
  12359. {$endif x86_64}
  12360. (ref^.base <> NR_NO) then
  12361. Optimizer.IncludeRegInUsedRegs(ref^.base, RegisterTracking);
  12362. if (ref^.index <> NR_NO) then
  12363. Optimizer.IncludeRegInUsedRegs(ref^.index, RegisterTracking);
  12364. end
  12365. else
  12366. ;
  12367. end;
  12368. { When inserting instructions before hp_prev, try to insert them
  12369. before the allocation of the FLAGS register }
  12370. if not SetAndTest(Optimizer.FindRegAllocBackward(NR_DEFAULTFLAGS, tai(fCondition.Previous)), fInsertionPoint) or
  12371. (tai_regalloc(fInsertionPoint).ratype = ra_dealloc) then
  12372. { If not found, set it equal to the condition so it's something sensible }
  12373. fInsertionPoint := fCondition;
  12374. { When dealing with a comparison against zero, take note of the
  12375. instruction before it to see if we can move instructions further
  12376. back in order to benefit PostPeepholeOptTestOr.
  12377. }
  12378. if (
  12379. (
  12380. (taicpu(fCondition).opcode = A_CMP) and
  12381. MatchOperand(taicpu(fCondition).oper[0]^, 0)
  12382. ) or
  12383. (
  12384. (taicpu(fCondition).opcode = A_TEST) and
  12385. (
  12386. Optimizer.OpsEqual(taicpu(fCondition).oper[0]^, taicpu(fCondition).oper[1]^) or
  12387. MatchOperand(taicpu(fCondition).oper[0]^, -1)
  12388. )
  12389. )
  12390. ) and
  12391. Optimizer.GetLastInstruction(fCondition, hp1) then
  12392. begin
  12393. { These instructions set the zero flag if the result is zero }
  12394. if MatchInstruction(hp1, [A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_POPCNT, A_LZCNT], []) then
  12395. begin
  12396. fInsertionPoint := hp1;
  12397. { Also mark all the registers in this previous instruction
  12398. as 'in use', even if they've just been deallocated }
  12399. for Count := 0 to 1 do
  12400. with taicpu(hp1).oper[Count]^ do
  12401. case typ of
  12402. top_reg:
  12403. if getregtype(reg) = R_INTREGISTER then
  12404. Optimizer.IncludeRegInUsedRegs(reg, RegisterTracking);
  12405. top_ref:
  12406. begin
  12407. if
  12408. {$ifdef x86_64}
  12409. (ref^.base <> NR_RIP) and
  12410. {$endif x86_64}
  12411. (ref^.base <> NR_NO) then
  12412. Optimizer.IncludeRegInUsedRegs(ref^.base, RegisterTracking);
  12413. if (ref^.index <> NR_NO) then
  12414. Optimizer.IncludeRegInUsedRegs(ref^.index, RegisterTracking);
  12415. end
  12416. else
  12417. ;
  12418. end;
  12419. end;
  12420. end;
  12421. end
  12422. else
  12423. fCondition := nil;
  12424. { When inserting instructions, try to insert them before the allocation of the FLAGS register }
  12425. if SetAndTest(Optimizer.FindRegAllocBackward(NR_DEFAULTFLAGS, tai(p_initialjump.Previous)), hp1) and
  12426. (tai_regalloc(hp1).ratype <> ra_dealloc) then
  12427. { If not found, set it equal to p so it's something sensible }
  12428. fInsertionPoint := hp1;
  12429. hp1 := p_initialmov;
  12430. if not InitialiseBlock(p_initialmov, p_initialjump, fFirstMovBlockStop, fSecondJump) then
  12431. Exit;
  12432. hp1 := fFirstMovBlockStop; { Will either be on a label or a jump }
  12433. if (hp1.typ <> ait_label) then { should be on a jump }
  12434. begin
  12435. if not Optimizer.GetNextInstruction(hp1, fMidLabel) or not (fMidLabel.typ = ait_label) then
  12436. { Need a label afterwards }
  12437. Exit;
  12438. end
  12439. else
  12440. fMidLabel := hp1;
  12441. if tai_label(fMidLabel).labsym <> AFirstLabel then
  12442. { Not the correct label }
  12443. fMidLabel := nil;
  12444. if not Assigned(fSecondJump) and not Assigned(fMidLabel) then
  12445. { If there's neither a 2nd jump nor correct label, then it's invalid
  12446. (see above table) }
  12447. Exit;
  12448. { Analyse the first block of MOVs more closely }
  12449. CMOVScore := AnalyseMOVBlock(fFirstMovBlock, fFirstMovBlockStop, fInsertionPoint);
  12450. if Assigned(fSecondJump) then
  12451. begin
  12452. if (JumpTargetOp(taicpu(fSecondJump))^.ref^.symbol = AFirstLabel) then
  12453. begin
  12454. fState := tsDetour
  12455. end
  12456. else
  12457. begin
  12458. { Need the correct mid-label for this one }
  12459. if not Assigned(fMidLabel) then
  12460. Exit;
  12461. fState := tsBranching;
  12462. end;
  12463. end
  12464. else
  12465. { No jump. but mid-label is present }
  12466. fState := tsSimple;
  12467. if (CMOVScore > MAX_CMOV_INSTRUCTIONS) or (CMOVScore <= 0) then
  12468. begin
  12469. { Invalid or too many instructions to be worthwhile }
  12470. fState := tsInvalid;
  12471. Exit;
  12472. end;
  12473. { check further for
  12474. jCC xxx
  12475. <several movs 1>
  12476. jmp yyy
  12477. xxx:
  12478. <several movs 2>
  12479. yyy:
  12480. etc.
  12481. }
  12482. if (fState = tsBranching) and
  12483. { Estimate for required savings for extra jump }
  12484. (CMOVScore <= MAX_CMOV_INSTRUCTIONS - 1) and
  12485. { Only one reference is allowed for double blocks }
  12486. (AFirstLabel.getrefs = 1) then
  12487. begin
  12488. Optimizer.GetNextInstruction(fMidLabel, hp1);
  12489. fSecondMovBlock := hp1;
  12490. if not InitialiseBlock(fSecondMovBlock, fMidLabel, fSecondMovBlockStop, fThirdJump) then
  12491. begin
  12492. EvaluateBranchingType;
  12493. Exit;
  12494. end;
  12495. hp1 := fSecondMovBlockStop; { Will either be on a label or a jump }
  12496. if (hp1.typ <> ait_label) then { should be on a jump }
  12497. begin
  12498. if not Optimizer.GetNextInstruction(hp1, fEndLabel) or not (fEndLabel.typ = ait_label) then
  12499. begin
  12500. { Need a label afterwards }
  12501. EvaluateBranchingType;
  12502. Exit;
  12503. end;
  12504. end
  12505. else
  12506. fEndLabel := hp1;
  12507. if tai_label(fEndLabel).labsym <> JumpTargetOp(taicpu(fSecondJump))^.ref^.symbol then
  12508. { Second jump doesn't go to the end }
  12509. fEndLabel := nil;
  12510. if not Assigned(fThirdJump) and not Assigned(fEndLabel) then
  12511. begin
  12512. { If there's neither a 3rd jump nor correct end label, then it's
  12513. not a invalid double block, but is a valid single branching
  12514. block (see above table) }
  12515. EvaluateBranchingType;
  12516. Exit;
  12517. end;
  12518. Count := AnalyseMOVBlock(fSecondMovBlock, fSecondMovBlockStop, fMidLabel);
  12519. if (Count > MAX_CMOV_INSTRUCTIONS) or (Count <= 0) then
  12520. { Invalid or too many instructions to be worthwhile }
  12521. Exit;
  12522. Inc(CMOVScore, Count);
  12523. if Assigned(fThirdJump) then
  12524. begin
  12525. if not Assigned(fSecondJump) then
  12526. fState := tsDoubleSecondBranching
  12527. else if (JumpTargetOp(taicpu(fSecondJump))^.ref^.symbol = JumpTargetOp(taicpu(fThirdJump))^.ref^.symbol) then
  12528. fState := tsDoubleBranchSame
  12529. else
  12530. fState := tsDoubleBranchDifferent;
  12531. end
  12532. else
  12533. fState := tsDouble;
  12534. end;
  12535. if fState = tsBranching then
  12536. EvaluateBranchingType;
  12537. end;
  12538. { Tries to convert a mov const,%reg instruction into a CMOV by reserving a
  12539. new register to store the constant }
  12540. function TCMOVTracking.TryCMOVConst(p, start, stop: tai; var Count: LongInt): Boolean;
  12541. var
  12542. RegSize: TSubRegister;
  12543. CurrentVal: TCGInt;
  12544. ANewReg: TRegister;
  12545. X: ShortInt;
  12546. begin
  12547. Result := False;
  12548. if not MatchOpType(taicpu(p), top_const, top_reg) then
  12549. Exit;
  12550. if ConstCount >= MAX_CMOV_REGISTERS then
  12551. { Arrays are full }
  12552. Exit;
  12553. { Remember that CMOV can't encode 8-bit registers }
  12554. case taicpu(p).opsize of
  12555. S_W:
  12556. RegSize := R_SUBW;
  12557. S_L:
  12558. RegSize := R_SUBD;
  12559. {$ifdef x86_64}
  12560. S_Q:
  12561. RegSize := R_SUBQ;
  12562. {$endif x86_64}
  12563. else
  12564. InternalError(2021100401);
  12565. end;
  12566. { See if the value has already been reserved for another CMOV instruction }
  12567. CurrentVal := taicpu(p).oper[0]^.val;
  12568. for X := 0 to ConstCount - 1 do
  12569. if ConstVals[X] = CurrentVal then
  12570. begin
  12571. ConstRegs[ConstCount] := ConstRegs[X];
  12572. ConstSizes[ConstCount] := RegSize;
  12573. ConstVals[ConstCount] := CurrentVal;
  12574. Inc(ConstCount);
  12575. Inc(Count);
  12576. Result := True;
  12577. Exit;
  12578. end;
  12579. ANewReg := fOptimizer.GetIntRegisterBetween(R_SUBWHOLE, RegisterTracking, start, stop, True);
  12580. if ANewReg = NR_NO then
  12581. { No free registers }
  12582. Exit;
  12583. { Reserve the register so subsequent TryCMOVConst calls don't all end
  12584. up vying for the same register }
  12585. fOptimizer.IncludeRegInUsedRegs(ANewReg, RegisterTracking);
  12586. ConstRegs[ConstCount] := ANewReg;
  12587. ConstSizes[ConstCount] := RegSize;
  12588. ConstVals[ConstCount] := CurrentVal;
  12589. Inc(ConstCount);
  12590. Inc(Count);
  12591. Result := True;
  12592. end;
  12593. destructor TCMOVTracking.Done;
  12594. begin
  12595. TAOptObj.ReleaseUsedRegs(RegisterTracking);
  12596. end;
  12597. procedure TCMOVTracking.Process(out new_p: tai);
  12598. var
  12599. Count, Writes: LongInt;
  12600. RegMatch: Boolean;
  12601. hp1, hp_new: tai;
  12602. inverted_condition, condition: TAsmCond;
  12603. begin
  12604. if (fState in [tsInvalid, tsProcessed]) then
  12605. InternalError(2023110701);
  12606. { Repurpose RegisterTracking to mark registers that we've defined }
  12607. RegisterTracking[R_INTREGISTER].Clear;
  12608. Count := 0;
  12609. Writes := 0;
  12610. condition := taicpu(fInitialJump).condition;
  12611. inverted_condition := inverse_cond(condition);
  12612. { Exclude tsDoubleBranchDifferent from this check, as the second block
  12613. doesn't get CMOVs in this case }
  12614. if (fState in [tsDouble, tsDoubleBranchSame, tsDoubleSecondBranching]) then
  12615. begin
  12616. { Include the jump in the flag tracking }
  12617. if Assigned(fThirdJump) then
  12618. begin
  12619. if (fState = tsDoubleBranchSame) then
  12620. begin
  12621. { Will be an unconditional jump, so track to the instruction before it }
  12622. if not fOptimizer.GetLastInstruction(fThirdJump, hp1) then
  12623. InternalError(2023110710);
  12624. end
  12625. else
  12626. hp1 := fThirdJump;
  12627. end
  12628. else
  12629. hp1 := fSecondMovBlockStop;
  12630. end
  12631. else
  12632. begin
  12633. { Include a conditional jump in the flag tracking }
  12634. if Assigned(fSecondJump) then
  12635. begin
  12636. if (fState = tsDetour) then
  12637. begin
  12638. { Will be an unconditional jump, so track to the instruction before it }
  12639. if not fOptimizer.GetLastInstruction(fSecondJump, hp1) then
  12640. InternalError(2023110711);
  12641. end
  12642. else
  12643. hp1 := fSecondJump;
  12644. end
  12645. else
  12646. hp1 := fFirstMovBlockStop;
  12647. end;
  12648. fOptimizer.AllocRegBetween(NR_DEFAULTFLAGS, fInitialJump, hp1, fOptimizer.UsedRegs);
  12649. { Process the second set of MOVs first, because if a destination
  12650. register is shared between the first and second MOV sets, it is more
  12651. efficient to turn the first one into a MOV instruction and place it
  12652. before the CMP if possible, but we won't know which registers are
  12653. shared until we've processed at least one list, so we might as well
  12654. make it the second one since that won't be modified again. }
  12655. if (fState in [tsDouble, tsDoubleBranchSame, tsDoubleBranchDifferent, tsDoubleSecondBranching]) then
  12656. begin
  12657. hp1 := fSecondMovBlock;
  12658. repeat
  12659. if not Assigned(hp1) then
  12660. InternalError(2018062902);
  12661. if (hp1.typ = ait_instruction) then
  12662. begin
  12663. { Extra safeguard }
  12664. if (taicpu(hp1).opcode <> A_MOV) then
  12665. InternalError(2018062903);
  12666. { Note: tsDoubleBranchDifferent is essentially identical to
  12667. tsBranching and the 2nd block is best left largely
  12668. untouched, but we need to evaluate which registers the MOVs
  12669. write to in order to track what would be complementary CMOV
  12670. pairs that can be further optimised. [Kit] }
  12671. if fState <> tsDoubleBranchDifferent then
  12672. begin
  12673. if taicpu(hp1).oper[0]^.typ = top_const then
  12674. begin
  12675. RegMatch := False;
  12676. for Count := 0 to ConstCount - 1 do
  12677. if (ConstVals[Count] = taicpu(hp1).oper[0]^.val) and
  12678. (getsubreg(taicpu(hp1).oper[1]^.reg) = ConstSizes[Count]) then
  12679. begin
  12680. RegMatch := True;
  12681. { If it's in RegisterTracking, then this register
  12682. is being used more than once and hence has
  12683. already had its value defined (it gets added to
  12684. UsedRegs through AllocRegBetween below) }
  12685. if not RegisterTracking[R_INTREGISTER].IsUsed(ConstRegs[Count]) then
  12686. begin
  12687. hp_new := taicpu.op_const_reg(A_MOV, subreg2opsize(R_SUBWHOLE), taicpu(hp1).oper[0]^.val, ConstRegs[Count]);
  12688. taicpu(hp_new).fileinfo := taicpu(fInitialJump).fileinfo;
  12689. fOptimizer.asml.InsertBefore(hp_new, fInsertionPoint);
  12690. fOptimizer.IncludeRegInUsedRegs(ConstRegs[Count], RegisterTracking);
  12691. ConstMovs[Count] := hp_new;
  12692. end
  12693. else
  12694. { We just need an instruction between hp_prev and hp1
  12695. where we know the register is marked as in use }
  12696. hp_new := fSecondMovBlock;
  12697. { Keep track of largest write for this register so it can be optimised later }
  12698. if (getsubreg(taicpu(hp1).oper[1]^.reg) > ConstWriteSizes[getsupreg(ConstRegs[Count])]) then
  12699. ConstWriteSizes[getsupreg(ConstRegs[Count])] := getsubreg(taicpu(hp1).oper[1]^.reg);
  12700. fOptimizer.AllocRegBetween(ConstRegs[Count], hp_new, hp1, fOptimizer.UsedRegs);
  12701. taicpu(hp1).loadreg(0, newreg(R_INTREGISTER, getsupreg(ConstRegs[Count]), ConstSizes[Count]));
  12702. Break;
  12703. end;
  12704. if not RegMatch then
  12705. InternalError(2021100411);
  12706. end;
  12707. taicpu(hp1).opcode := A_CMOVcc;
  12708. taicpu(hp1).condition := condition;
  12709. end;
  12710. { Store these writes to search for duplicates later on }
  12711. RegWrites[Writes] := taicpu(hp1).oper[1]^.reg;
  12712. Inc(Writes);
  12713. end;
  12714. fOptimizer.GetNextInstruction(hp1, hp1);
  12715. until (hp1 = fSecondMovBlockStop);
  12716. end;
  12717. { Now do the first set of MOVs }
  12718. hp1 := fFirstMovBlock;
  12719. repeat
  12720. if not Assigned(hp1) then
  12721. InternalError(2018062904);
  12722. if (hp1.typ = ait_instruction) then
  12723. begin
  12724. RegMatch := False;
  12725. { Extra safeguard }
  12726. if (taicpu(hp1).opcode <> A_MOV) then
  12727. InternalError(2018062905);
  12728. { Search through the RegWrites list to see if there are any
  12729. opposing CMOV pairs that write to the same register }
  12730. for Count := 0 to Writes - 1 do
  12731. if (RegWrites[Count] = taicpu(hp1).oper[1]^.reg) then
  12732. begin
  12733. { We have a match. Keep this as a MOV }
  12734. { Move ahead in preparation }
  12735. fOptimizer.GetNextInstruction(hp1, hp1);
  12736. RegMatch := True;
  12737. Break;
  12738. end;
  12739. if RegMatch then
  12740. Continue;
  12741. if taicpu(hp1).oper[0]^.typ = top_const then
  12742. begin
  12743. for Count := 0 to ConstCount - 1 do
  12744. if (ConstVals[Count] = taicpu(hp1).oper[0]^.val) and
  12745. (getsubreg(taicpu(hp1).oper[1]^.reg) = ConstSizes[Count]) then
  12746. begin
  12747. RegMatch := True;
  12748. { If it's in RegisterTracking, then this register is
  12749. being used more than once and hence has already had
  12750. its value defined (it gets added to UsedRegs through
  12751. AllocRegBetween below) }
  12752. if not RegisterTracking[R_INTREGISTER].IsUsed(ConstRegs[Count]) then
  12753. begin
  12754. hp_new := taicpu.op_const_reg(A_MOV, subreg2opsize(R_SUBWHOLE), taicpu(hp1).oper[0]^.val, ConstRegs[Count]);
  12755. taicpu(hp_new).fileinfo := taicpu(fInitialJump).fileinfo;
  12756. fOptimizer.asml.InsertBefore(hp_new, fInsertionPoint);
  12757. fOptimizer.IncludeRegInUsedRegs(ConstRegs[Count], RegisterTracking);
  12758. ConstMovs[Count] := hp_new;
  12759. end
  12760. else
  12761. { We just need an instruction between hp_prev and hp1
  12762. where we know the register is marked as in use }
  12763. hp_new := fFirstMovBlock;
  12764. { Keep track of largest write for this register so it can be optimised later }
  12765. if (getsubreg(taicpu(hp1).oper[1]^.reg) > ConstWriteSizes[getsupreg(ConstRegs[Count])]) then
  12766. ConstWriteSizes[getsupreg(ConstRegs[Count])] := getsubreg(taicpu(hp1).oper[1]^.reg);
  12767. fOptimizer.AllocRegBetween(ConstRegs[Count], hp_new, hp1, fOptimizer.UsedRegs);
  12768. taicpu(hp1).loadreg(0, newreg(R_INTREGISTER, getsupreg(ConstRegs[Count]), ConstSizes[Count]));
  12769. Break;
  12770. end;
  12771. if not RegMatch then
  12772. InternalError(2021100412);
  12773. end;
  12774. taicpu(hp1).opcode := A_CMOVcc;
  12775. taicpu(hp1).condition := inverted_condition;
  12776. if (fState = tsDoubleBranchDifferent) then
  12777. begin
  12778. { Store these writes to search for duplicates later on }
  12779. RegWrites[Writes] := taicpu(hp1).oper[1]^.reg;
  12780. Inc(Writes);
  12781. end;
  12782. end;
  12783. fOptimizer.GetNextInstruction(hp1, hp1);
  12784. until (hp1 = fFirstMovBlockStop);
  12785. { Update initialisation MOVs to the smallest possible size }
  12786. for Count := 0 to ConstCount - 1 do
  12787. if Assigned(ConstMovs[Count]) then
  12788. begin
  12789. taicpu(ConstMovs[Count]).opsize := subreg2opsize(ConstWriteSizes[Word(ConstRegs[Count])]);
  12790. setsubreg(taicpu(ConstMovs[Count]).oper[1]^.reg, ConstWriteSizes[Word(ConstRegs[Count])]);
  12791. end;
  12792. case fState of
  12793. tsSimple:
  12794. begin
  12795. fOptimizer.DebugMsg(SPeepholeOptimization + 'CMOV Block (Simple type)', fInitialJump);
  12796. { No branch to delete }
  12797. end;
  12798. tsDetour:
  12799. begin
  12800. fOptimizer.DebugMsg(SPeepholeOptimization + 'CMOV Block (Detour type)', fInitialJump);
  12801. { Preserve jump }
  12802. end;
  12803. tsBranching, tsDoubleBranchDifferent:
  12804. begin
  12805. if (fState = tsBranching) then
  12806. fOptimizer.DebugMsg(SPeepholeOptimization + 'CMOV Block (Branching type)', fInitialJump)
  12807. else
  12808. fOptimizer.DebugMsg(SPeepholeOptimization + 'CMOV Block (Double branching (different) type)', fInitialJump);
  12809. taicpu(fSecondJump).opcode := A_JCC;
  12810. taicpu(fSecondJump).condition := inverted_condition;
  12811. end;
  12812. tsDouble, tsDoubleBranchSame:
  12813. begin
  12814. if (fState = tsDouble) then
  12815. fOptimizer.DebugMsg(SPeepholeOptimization + 'CMOV Block (Double type)', fInitialJump)
  12816. else
  12817. fOptimizer.DebugMsg(SPeepholeOptimization + 'CMOV Block (Double branching (same) type)', fInitialJump);
  12818. { Delete second jump }
  12819. JumpTargetOp(taicpu(fSecondJump))^.ref^.symbol.decrefs;
  12820. fOptimizer.RemoveInstruction(fSecondJump);
  12821. end;
  12822. tsDoubleSecondBranching:
  12823. begin
  12824. fOptimizer.DebugMsg(SPeepholeOptimization + 'CMOV Block (Double, second branching type)', fInitialJump);
  12825. { Delete second jump, preserve third jump as conditional }
  12826. JumpTargetOp(taicpu(fSecondJump))^.ref^.symbol.decrefs;
  12827. fOptimizer.RemoveInstruction(fSecondJump);
  12828. taicpu(fThirdJump).opcode := A_JCC;
  12829. taicpu(fThirdJump).condition := condition;
  12830. end;
  12831. else
  12832. InternalError(2023110720);
  12833. end;
  12834. { Now we can safely decrement the reference count }
  12835. tasmlabel(fLabel).decrefs;
  12836. fOptimizer.UpdateUsedRegs(tai(fInitialJump.next));
  12837. { Remove the original jump }
  12838. fOptimizer.RemoveInstruction(fInitialJump); { Note, the choice to not use RemoveCurrentp is deliberate }
  12839. new_p := fFirstMovBlock; { Appears immediately after the initial jump }
  12840. fState := tsProcessed;
  12841. end;
  12842. {$endif 8086}
  12843. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  12844. var
  12845. hp1,hp2: tai;
  12846. carryadd_opcode : TAsmOp;
  12847. symbol: TAsmSymbol;
  12848. increg, tmpreg: TRegister;
  12849. {$ifndef i8086}
  12850. CMOVTracking: PCMOVTracking;
  12851. hp3,hp4,hp5: tai;
  12852. {$endif i8086}
  12853. TempBool: Boolean;
  12854. begin
  12855. if (aoc_DoPass2JccOpts in OptsToCheck) and
  12856. DoJumpOptimizations(p, TempBool) then
  12857. Exit(True);
  12858. result:=false;
  12859. if GetNextInstruction(p,hp1) then
  12860. begin
  12861. if (hp1.typ=ait_label) then
  12862. begin
  12863. Result := DoSETccLblRETOpt(p, tai_label(hp1));
  12864. Exit;
  12865. end
  12866. else if (hp1.typ<>ait_instruction) then
  12867. Exit;
  12868. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  12869. if (
  12870. (
  12871. ((Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB)) and
  12872. MatchOptype(Taicpu(hp1),top_const,top_reg) and
  12873. (Taicpu(hp1).oper[0]^.val=1)
  12874. ) or
  12875. ((Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC))
  12876. ) and
  12877. GetNextInstruction(hp1,hp2) and
  12878. FindLabel(TAsmLabel(symbol), hp2) then
  12879. { jb @@1 cmc
  12880. inc/dec operand --> adc/sbb operand,0
  12881. @@1:
  12882. ... and ...
  12883. jnb @@1
  12884. inc/dec operand --> adc/sbb operand,0
  12885. @@1: }
  12886. begin
  12887. if Taicpu(p).condition in [C_NAE,C_B,C_C] then
  12888. begin
  12889. case taicpu(hp1).opcode of
  12890. A_INC,
  12891. A_ADD:
  12892. carryadd_opcode:=A_ADC;
  12893. A_DEC,
  12894. A_SUB:
  12895. carryadd_opcode:=A_SBB;
  12896. else
  12897. InternalError(2021011001);
  12898. end;
  12899. Taicpu(p).clearop(0);
  12900. Taicpu(p).ops:=0;
  12901. Taicpu(p).is_jmp:=false;
  12902. Taicpu(p).opcode:=A_CMC;
  12903. Taicpu(p).condition:=C_NONE;
  12904. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2CmcAdc/Sbb',p);
  12905. Taicpu(hp1).ops:=2;
  12906. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  12907. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  12908. else
  12909. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  12910. Taicpu(hp1).loadconst(0,0);
  12911. Taicpu(hp1).opcode:=carryadd_opcode;
  12912. result:=true;
  12913. exit;
  12914. end
  12915. else if Taicpu(p).condition in [C_AE,C_NB,C_NC] then
  12916. begin
  12917. case taicpu(hp1).opcode of
  12918. A_INC,
  12919. A_ADD:
  12920. carryadd_opcode:=A_ADC;
  12921. A_DEC,
  12922. A_SUB:
  12923. carryadd_opcode:=A_SBB;
  12924. else
  12925. InternalError(2021011002);
  12926. end;
  12927. Taicpu(hp1).ops:=2;
  12928. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2Adc/Sbb',p);
  12929. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  12930. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  12931. else
  12932. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  12933. Taicpu(hp1).loadconst(0,0);
  12934. Taicpu(hp1).opcode:=carryadd_opcode;
  12935. RemoveCurrentP(p, hp1);
  12936. result:=true;
  12937. exit;
  12938. end
  12939. {
  12940. jcc @@1 setcc tmpreg
  12941. inc/dec/add/sub operand -> (movzx tmpreg)
  12942. @@1: add/sub tmpreg,operand
  12943. While this increases code size slightly, it makes the code much faster if the
  12944. jump is unpredictable
  12945. }
  12946. else if not(cs_opt_size in current_settings.optimizerswitches) then
  12947. begin
  12948. { search for an available register which is volatile }
  12949. increg := GetIntRegisterBetween(R_SUBL, UsedRegs, p, hp1);
  12950. if increg <> NR_NO then
  12951. begin
  12952. { We don't need to check if tmpreg is in hp1 or not, because
  12953. it will be marked as in use at p (if not, this is
  12954. indictive of a compiler bug). }
  12955. TAsmLabel(symbol).decrefs;
  12956. Taicpu(p).clearop(0);
  12957. Taicpu(p).ops:=1;
  12958. Taicpu(p).is_jmp:=false;
  12959. Taicpu(p).opcode:=A_SETcc;
  12960. DebugMsg(SPeepholeOptimization+'JccAdd2SetccAdd',p);
  12961. Taicpu(p).condition:=inverse_cond(Taicpu(p).condition);
  12962. Taicpu(p).loadreg(0,increg);
  12963. if getsubreg(Taicpu(hp1).oper[1]^.reg)<>R_SUBL then
  12964. begin
  12965. case getsubreg(Taicpu(hp1).oper[1]^.reg) of
  12966. R_SUBW:
  12967. begin
  12968. tmpreg := newreg(R_INTREGISTER,getsupreg(increg),R_SUBW);
  12969. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BW,increg,tmpreg);
  12970. end;
  12971. R_SUBD:
  12972. begin
  12973. tmpreg := newreg(R_INTREGISTER,getsupreg(increg),R_SUBD);
  12974. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,tmpreg);
  12975. end;
  12976. {$ifdef x86_64}
  12977. R_SUBQ:
  12978. begin
  12979. { MOVZX doesn't have a 64-bit variant, because
  12980. the 32-bit version implicitly zeroes the
  12981. upper 32-bits of the destination register }
  12982. tmpreg := newreg(R_INTREGISTER,getsupreg(increg),R_SUBD);
  12983. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,tmpreg);
  12984. setsubreg(tmpreg, R_SUBQ);
  12985. end;
  12986. {$endif x86_64}
  12987. else
  12988. Internalerror(2020030601);
  12989. end;
  12990. taicpu(hp2).fileinfo:=taicpu(hp1).fileinfo;
  12991. asml.InsertAfter(hp2,p);
  12992. end
  12993. else
  12994. tmpreg := increg;
  12995. if (Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC) then
  12996. begin
  12997. Taicpu(hp1).ops:=2;
  12998. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^)
  12999. end;
  13000. Taicpu(hp1).loadreg(0,tmpreg);
  13001. AllocRegBetween(tmpreg,p,hp1,UsedRegs);
  13002. Result := True;
  13003. { p is no longer a Jcc instruction, so exit }
  13004. Exit;
  13005. end;
  13006. end;
  13007. end;
  13008. { Detect the following:
  13009. jmp<cond> @Lbl1
  13010. jmp @Lbl2
  13011. ...
  13012. @Lbl1:
  13013. ret
  13014. Change to:
  13015. jmp<inv_cond> @Lbl2
  13016. ret
  13017. }
  13018. if MatchInstruction(hp1,A_JMP,[]) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  13019. begin
  13020. hp2:=getlabelwithsym(TAsmLabel(symbol));
  13021. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  13022. MatchInstruction(hp2,A_RET,[S_NO]) then
  13023. begin
  13024. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  13025. { Change label address to that of the unconditional jump }
  13026. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  13027. TAsmLabel(symbol).DecRefs;
  13028. taicpu(hp1).opcode := A_RET;
  13029. taicpu(hp1).is_jmp := false;
  13030. taicpu(hp1).ops := taicpu(hp2).ops;
  13031. DebugMsg(SPeepholeOptimization+'JccJmpRet2J!ccRet',p);
  13032. case taicpu(hp2).ops of
  13033. 0:
  13034. taicpu(hp1).clearop(0);
  13035. 1:
  13036. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  13037. else
  13038. internalerror(2016041302);
  13039. end;
  13040. end;
  13041. {$ifndef i8086}
  13042. end
  13043. {
  13044. convert
  13045. j<c> .L1
  13046. mov 1,reg
  13047. jmp .L2
  13048. .L1
  13049. mov 0,reg
  13050. .L2
  13051. into
  13052. mov 0,reg
  13053. set<not(c)> reg
  13054. take care of alignment and that the mov 0,reg is not converted into a xor as this
  13055. would destroy the flag contents
  13056. }
  13057. else if MatchInstruction(hp1,A_MOV,[]) and
  13058. MatchOpType(taicpu(hp1),top_const,top_reg) and
  13059. {$ifdef i386}
  13060. (
  13061. { Under i386, ESI, EDI, EBP and ESP
  13062. don't have an 8-bit representation }
  13063. not (getsupreg(taicpu(hp1).oper[1]^.reg) in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  13064. ) and
  13065. {$endif i386}
  13066. (taicpu(hp1).oper[0]^.val=1) and
  13067. GetNextInstruction(hp1,hp2) and
  13068. MatchInstruction(hp2,A_JMP,[]) and (taicpu(hp2).oper[0]^.ref^.refaddr=addr_full) and
  13069. GetNextInstruction(hp2,hp3) and
  13070. (hp3.typ=ait_label) and
  13071. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol)=tai_label(hp3).labsym) and
  13072. (tai_label(hp3).labsym.getrefs=1) and
  13073. GetNextInstruction(hp3,hp4) and
  13074. MatchInstruction(hp4,A_MOV,[]) and
  13075. MatchOpType(taicpu(hp4),top_const,top_reg) and
  13076. (taicpu(hp4).oper[0]^.val=0) and
  13077. MatchOperand(taicpu(hp1).oper[1]^,taicpu(hp4).oper[1]^) and
  13078. GetNextInstruction(hp4,hp5) and
  13079. (hp5.typ=ait_label) and
  13080. (tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol)=tai_label(hp5).labsym) and
  13081. (tai_label(hp5).labsym.getrefs=1) then
  13082. begin
  13083. AllocRegBetween(NR_FLAGS,p,hp4,UsedRegs);
  13084. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2MovSetcc',p);
  13085. { remove last label }
  13086. RemoveInstruction(hp5);
  13087. { remove second label }
  13088. RemoveInstruction(hp3);
  13089. { remove jmp }
  13090. RemoveInstruction(hp2);
  13091. if taicpu(hp1).opsize=S_B then
  13092. RemoveInstruction(hp1)
  13093. else
  13094. taicpu(hp1).loadconst(0,0);
  13095. taicpu(hp4).opcode:=A_SETcc;
  13096. taicpu(hp4).opsize:=S_B;
  13097. taicpu(hp4).condition:=inverse_cond(taicpu(p).condition);
  13098. taicpu(hp4).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(hp4).oper[1]^.reg),R_SUBL));
  13099. taicpu(hp4).opercnt:=1;
  13100. taicpu(hp4).ops:=1;
  13101. taicpu(hp4).freeop(1);
  13102. RemoveCurrentP(p);
  13103. Result:=true;
  13104. exit;
  13105. end
  13106. else if (CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and
  13107. MatchInstruction(hp1,A_MOV,[S_W,S_L{$ifdef x86_64},S_Q{$endif x86_64}]) then
  13108. begin
  13109. { check for
  13110. jCC xxx
  13111. <several movs>
  13112. xxx:
  13113. Also spot:
  13114. Jcc xxx
  13115. <several movs>
  13116. jmp xxx
  13117. Change to:
  13118. <several cmovs with inverted condition>
  13119. jmp xxx (only for the 2nd case)
  13120. }
  13121. CMOVTracking := New(PCMOVTracking, Init(Self, p, hp1, TAsmLabel(symbol)));
  13122. if CMOVTracking^.State <> tsInvalid then
  13123. begin
  13124. CMovTracking^.Process(p);
  13125. Result := True;
  13126. end;
  13127. CMOVTracking^.Done;
  13128. {$endif i8086}
  13129. end;
  13130. end;
  13131. end;
  13132. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  13133. var
  13134. hp1,hp2,hp3: tai;
  13135. reg_and_hp1_is_instr, RegUsed, AndTest: Boolean;
  13136. NewSize: TOpSize;
  13137. NewRegSize: TSubRegister;
  13138. Limit: TCgInt;
  13139. SwapOper: POper;
  13140. begin
  13141. result:=false;
  13142. reg_and_hp1_is_instr:=(taicpu(p).oper[1]^.typ = top_reg) and
  13143. GetNextInstruction(p,hp1) and
  13144. (hp1.typ = ait_instruction);
  13145. if reg_and_hp1_is_instr and
  13146. (
  13147. (taicpu(hp1).opcode <> A_LEA) or
  13148. { If the LEA instruction can be converted into an arithmetic instruction,
  13149. it may be possible to then fold it. }
  13150. (
  13151. { If the flags register is in use, don't change the instruction
  13152. to an ADD otherwise this will scramble the flags. [Kit] }
  13153. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  13154. ConvertLEA(taicpu(hp1))
  13155. )
  13156. ) and
  13157. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  13158. GetNextInstruction(hp1,hp2) and
  13159. MatchInstruction(hp2,A_MOV,[]) and
  13160. (taicpu(hp2).oper[0]^.typ = top_reg) and
  13161. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  13162. ((taicpu(p).opsize in [S_BW,S_BL]) and (taicpu(hp2).opsize=S_B) or
  13163. (taicpu(p).opsize in [S_WL]) and (taicpu(hp2).opsize=S_W)) and
  13164. {$ifdef i386}
  13165. { not all registers have byte size sub registers on i386 }
  13166. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  13167. {$endif i386}
  13168. (((taicpu(hp1).ops=2) and
  13169. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  13170. ((taicpu(hp1).ops=1) and
  13171. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  13172. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  13173. begin
  13174. { change movsX/movzX reg/ref, reg2
  13175. add/sub/or/... reg3/$const, reg2
  13176. mov reg2 reg/ref
  13177. to add/sub/or/... reg3/$const, reg/ref }
  13178. { by example:
  13179. movswl %si,%eax movswl %si,%eax p
  13180. decl %eax addl %edx,%eax hp1
  13181. movw %ax,%si movw %ax,%si hp2
  13182. ->
  13183. movswl %si,%eax movswl %si,%eax p
  13184. decw %eax addw %edx,%eax hp1
  13185. movw %ax,%si movw %ax,%si hp2
  13186. }
  13187. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  13188. {
  13189. ->
  13190. movswl %si,%eax movswl %si,%eax p
  13191. decw %si addw %dx,%si hp1
  13192. movw %ax,%si movw %ax,%si hp2
  13193. }
  13194. case taicpu(hp1).ops of
  13195. 1:
  13196. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  13197. 2:
  13198. begin
  13199. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  13200. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  13201. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  13202. end;
  13203. else
  13204. internalerror(2008042702);
  13205. end;
  13206. {
  13207. ->
  13208. decw %si addw %dx,%si p
  13209. }
  13210. DebugMsg(SPeepholeOptimization + 'var3',p);
  13211. RemoveCurrentP(p, hp1);
  13212. RemoveInstruction(hp2);
  13213. Result := True;
  13214. Exit;
  13215. end;
  13216. if reg_and_hp1_is_instr and
  13217. (taicpu(hp1).opcode = A_MOV) and
  13218. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  13219. (MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^)
  13220. {$ifdef x86_64}
  13221. { check for implicit extension to 64 bit }
  13222. or
  13223. ((taicpu(p).opsize in [S_BL,S_WL]) and
  13224. (taicpu(hp1).opsize=S_Q) and
  13225. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg)
  13226. )
  13227. {$endif x86_64}
  13228. )
  13229. then
  13230. begin
  13231. { change
  13232. movx %reg1,%reg2
  13233. mov %reg2,%reg3
  13234. dealloc %reg2
  13235. into
  13236. movx %reg,%reg3
  13237. }
  13238. TransferUsedRegs(TmpUsedRegs);
  13239. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  13240. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  13241. begin
  13242. DebugMsg(SPeepholeOptimization + 'MovxMov2Movx',p);
  13243. {$ifdef x86_64}
  13244. if (taicpu(p).opsize in [S_BL,S_WL]) and
  13245. (taicpu(hp1).opsize=S_Q) then
  13246. taicpu(p).loadreg(1,newreg(R_INTREGISTER,getsupreg(taicpu(hp1).oper[1]^.reg),R_SUBD))
  13247. else
  13248. {$endif x86_64}
  13249. taicpu(p).loadreg(1,taicpu(hp1).oper[1]^.reg);
  13250. RemoveInstruction(hp1);
  13251. Result := True;
  13252. Exit;
  13253. end;
  13254. end;
  13255. if reg_and_hp1_is_instr and
  13256. ((taicpu(hp1).opcode=A_MOV) or
  13257. (taicpu(hp1).opcode=A_ADD) or
  13258. (taicpu(hp1).opcode=A_SUB) or
  13259. (taicpu(hp1).opcode=A_CMP) or
  13260. (taicpu(hp1).opcode=A_OR) or
  13261. (taicpu(hp1).opcode=A_XOR) or
  13262. (taicpu(hp1).opcode=A_AND)
  13263. ) and
  13264. (taicpu(hp1).oper[1]^.typ = top_reg) then
  13265. begin
  13266. AndTest := (taicpu(hp1).opcode=A_AND) and
  13267. GetNextInstruction(hp1, hp2) and
  13268. (hp2.typ = ait_instruction) and
  13269. (
  13270. (
  13271. (taicpu(hp2).opcode=A_TEST) and
  13272. (
  13273. MatchOperand(taicpu(hp2).oper[0]^, taicpu(hp1).oper[1]^.reg) or
  13274. MatchOperand(taicpu(hp2).oper[0]^, -1) or
  13275. (
  13276. { If the AND and TEST instructions share a constant, this is also valid }
  13277. (taicpu(hp1).oper[0]^.typ = top_const) and
  13278. MatchOperand(taicpu(hp2).oper[0]^, taicpu(hp1).oper[0]^.val)
  13279. )
  13280. ) and
  13281. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[1]^.reg)
  13282. ) or
  13283. (
  13284. (taicpu(hp2).opcode=A_CMP) and
  13285. MatchOperand(taicpu(hp2).oper[0]^, 0) and
  13286. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[1]^.reg)
  13287. )
  13288. );
  13289. { change
  13290. movx (oper),%reg2
  13291. and $x,%reg2
  13292. test %reg2,%reg2
  13293. dealloc %reg2
  13294. into
  13295. op %reg1,%reg3
  13296. if the second op accesses only the bits stored in reg1
  13297. }
  13298. if ((taicpu(p).oper[0]^.typ=top_reg) or
  13299. ((taicpu(p).oper[0]^.typ=top_ref) and (taicpu(p).oper[0]^.ref^.refaddr<>addr_full))) and
  13300. (taicpu(hp1).oper[0]^.typ = top_const) and
  13301. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) and
  13302. AndTest then
  13303. begin
  13304. { Check if the AND constant is in range }
  13305. case taicpu(p).opsize of
  13306. S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  13307. begin
  13308. NewSize := S_B;
  13309. Limit := $FF;
  13310. end;
  13311. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  13312. begin
  13313. NewSize := S_W;
  13314. Limit := $FFFF;
  13315. end;
  13316. {$ifdef x86_64}
  13317. S_LQ:
  13318. begin
  13319. NewSize := S_L;
  13320. Limit := $FFFFFFFF;
  13321. end;
  13322. {$endif x86_64}
  13323. else
  13324. InternalError(2021120303);
  13325. end;
  13326. if (
  13327. ((taicpu(hp1).oper[0]^.val and Limit) = taicpu(hp1).oper[0]^.val) or
  13328. { Check for negative operands }
  13329. (((not taicpu(hp1).oper[0]^.val) and Limit) = (not taicpu(hp1).oper[0]^.val))
  13330. ) and
  13331. GetNextInstruction(hp2,hp3) and
  13332. MatchInstruction(hp3,A_Jcc,A_Setcc,A_CMOVcc,[]) and
  13333. (taicpu(hp3).condition in [C_E,C_NE]) then
  13334. begin
  13335. TransferUsedRegs(TmpUsedRegs);
  13336. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  13337. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  13338. if not(RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp2, TmpUsedRegs)) then
  13339. begin
  13340. DebugMsg(SPeepholeOptimization + 'MovxAndTest2Test done',p);
  13341. taicpu(hp1).loadoper(1, taicpu(p).oper[0]^);
  13342. taicpu(hp1).opcode := A_TEST;
  13343. taicpu(hp1).opsize := NewSize;
  13344. RemoveInstruction(hp2);
  13345. RemoveCurrentP(p, hp1);
  13346. Result:=true;
  13347. exit;
  13348. end;
  13349. end;
  13350. end;
  13351. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  13352. (((taicpu(p).opsize in [S_BW,S_BL,S_WL{$ifdef x86_64},S_BQ,S_WQ,S_LQ{$endif x86_64}]) and
  13353. (taicpu(hp1).opsize=S_B)) or
  13354. ((taicpu(p).opsize in [S_WL{$ifdef x86_64},S_WQ,S_LQ{$endif x86_64}]) and
  13355. (taicpu(hp1).opsize=S_W))
  13356. {$ifdef x86_64}
  13357. or ((taicpu(p).opsize=S_LQ) and
  13358. (taicpu(hp1).opsize=S_L))
  13359. {$endif x86_64}
  13360. ) and
  13361. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg) then
  13362. begin
  13363. { change
  13364. movx %reg1,%reg2
  13365. op %reg2,%reg3
  13366. dealloc %reg2
  13367. into
  13368. op %reg1,%reg3
  13369. if the second op accesses only the bits stored in reg1
  13370. }
  13371. TransferUsedRegs(TmpUsedRegs);
  13372. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  13373. if AndTest then
  13374. begin
  13375. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  13376. RegUsed := RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs);
  13377. end
  13378. else
  13379. RegUsed := RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs);
  13380. if not RegUsed then
  13381. begin
  13382. DebugMsg(SPeepholeOptimization + 'MovxOp2Op 1',p);
  13383. if taicpu(p).oper[0]^.typ=top_reg then
  13384. begin
  13385. case taicpu(hp1).opsize of
  13386. S_B:
  13387. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBL));
  13388. S_W:
  13389. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBW));
  13390. S_L:
  13391. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBD));
  13392. else
  13393. Internalerror(2020102301);
  13394. end;
  13395. AllocRegBetween(taicpu(hp1).oper[0]^.reg,p,hp1,UsedRegs);
  13396. end
  13397. else
  13398. taicpu(hp1).loadref(0,taicpu(p).oper[0]^.ref^);
  13399. RemoveCurrentP(p);
  13400. if AndTest then
  13401. RemoveInstruction(hp2);
  13402. result:=true;
  13403. exit;
  13404. end;
  13405. end
  13406. else if (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
  13407. (
  13408. { Bitwise operations only }
  13409. (taicpu(hp1).opcode=A_AND) or
  13410. (taicpu(hp1).opcode=A_TEST) or
  13411. (
  13412. (taicpu(hp1).oper[0]^.typ = top_const) and
  13413. (
  13414. (taicpu(hp1).opcode=A_OR) or
  13415. (taicpu(hp1).opcode=A_XOR)
  13416. )
  13417. )
  13418. ) and
  13419. (
  13420. (taicpu(hp1).oper[0]^.typ = top_const) or
  13421. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) or
  13422. not RegInOp(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[0]^)
  13423. ) then
  13424. begin
  13425. { change
  13426. movx %reg2,%reg2
  13427. op const,%reg2
  13428. into
  13429. op const,%reg2 (smaller version)
  13430. movx %reg2,%reg2
  13431. also change
  13432. movx %reg1,%reg2
  13433. and/test (oper),%reg2
  13434. dealloc %reg2
  13435. into
  13436. and/test (oper),%reg1
  13437. }
  13438. case taicpu(p).opsize of
  13439. S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  13440. begin
  13441. NewSize := S_B;
  13442. NewRegSize := R_SUBL;
  13443. Limit := $FF;
  13444. end;
  13445. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  13446. begin
  13447. NewSize := S_W;
  13448. NewRegSize := R_SUBW;
  13449. Limit := $FFFF;
  13450. end;
  13451. {$ifdef x86_64}
  13452. S_LQ:
  13453. begin
  13454. NewSize := S_L;
  13455. NewRegSize := R_SUBD;
  13456. Limit := $FFFFFFFF;
  13457. end;
  13458. {$endif x86_64}
  13459. else
  13460. Internalerror(2021120302);
  13461. end;
  13462. TransferUsedRegs(TmpUsedRegs);
  13463. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  13464. if AndTest then
  13465. begin
  13466. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  13467. RegUsed := RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs);
  13468. end
  13469. else
  13470. RegUsed := RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs);
  13471. if
  13472. (
  13473. (taicpu(p).opcode = A_MOVZX) and
  13474. (
  13475. (taicpu(hp1).opcode=A_AND) or
  13476. (taicpu(hp1).opcode=A_TEST)
  13477. ) and
  13478. not (
  13479. { If both are references, then the final instruction will have
  13480. both operands as references, which is not allowed }
  13481. (taicpu(p).oper[0]^.typ = top_ref) and
  13482. (taicpu(hp1).oper[0]^.typ = top_ref)
  13483. ) and
  13484. not RegUsed
  13485. ) or
  13486. (
  13487. (
  13488. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) or
  13489. not RegUsed
  13490. ) and
  13491. (taicpu(p).oper[0]^.typ = top_reg) and
  13492. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  13493. (taicpu(hp1).oper[0]^.typ = top_const) and
  13494. ((taicpu(hp1).oper[0]^.val and Limit) = taicpu(hp1).oper[0]^.val)
  13495. ) then
  13496. begin
  13497. {$if defined(i386) or defined(i8086)}
  13498. { If the target size is 8-bit, make sure we can actually encode it }
  13499. if (NewRegSize = R_SUBL) and (taicpu(hp1).oper[0]^.typ = top_reg) and not (GetSupReg(taicpu(hp1).oper[0]^.reg) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX]) then
  13500. Exit;
  13501. {$endif i386 or i8086}
  13502. DebugMsg(SPeepholeOptimization + 'MovxOp2Op 2',p);
  13503. taicpu(hp1).opsize := NewSize;
  13504. taicpu(hp1).loadoper(1, taicpu(p).oper[0]^);
  13505. if AndTest then
  13506. begin
  13507. RemoveInstruction(hp2);
  13508. if not RegUsed then
  13509. begin
  13510. taicpu(hp1).opcode := A_TEST;
  13511. if (taicpu(hp1).oper[0]^.typ = top_ref) then
  13512. begin
  13513. { Make sure the reference is the second operand }
  13514. SwapOper := taicpu(hp1).oper[0];
  13515. taicpu(hp1).oper[0] := taicpu(hp1).oper[1];
  13516. taicpu(hp1).oper[1] := SwapOper;
  13517. end;
  13518. end;
  13519. end;
  13520. case taicpu(hp1).oper[0]^.typ of
  13521. top_reg:
  13522. setsubreg(taicpu(hp1).oper[0]^.reg, NewRegSize);
  13523. top_const:
  13524. { For the AND/TEST case }
  13525. taicpu(hp1).oper[0]^.val := taicpu(hp1).oper[0]^.val and Limit;
  13526. else
  13527. ;
  13528. end;
  13529. if RegUsed then
  13530. begin
  13531. AsmL.Remove(p);
  13532. AsmL.InsertAfter(p, hp1);
  13533. p := hp1;
  13534. end
  13535. else
  13536. RemoveCurrentP(p, hp1);
  13537. result:=true;
  13538. exit;
  13539. end;
  13540. end;
  13541. end;
  13542. if reg_and_hp1_is_instr and
  13543. (taicpu(p).oper[0]^.typ = top_reg) and
  13544. (
  13545. (taicpu(hp1).opcode = A_SHL) or (taicpu(hp1).opcode = A_SAL)
  13546. ) and
  13547. (taicpu(hp1).oper[0]^.typ = top_const) and
  13548. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  13549. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  13550. { Minimum shift value allowed is the bit difference between the sizes }
  13551. (taicpu(hp1).oper[0]^.val >=
  13552. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  13553. 8 * (
  13554. tcgsize2size[reg_cgsize(taicpu(p).oper[1]^.reg)] -
  13555. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  13556. )
  13557. ) then
  13558. begin
  13559. { For:
  13560. movsx/movzx %reg1,%reg1 (same register, just different sizes)
  13561. shl/sal ##, %reg1
  13562. Remove the movsx/movzx instruction if the shift overwrites the
  13563. extended bits of the register (e.g. movslq %eax,%rax; shlq $32,%rax
  13564. }
  13565. DebugMsg(SPeepholeOptimization + 'MovxShl2Shl',p);
  13566. RemoveCurrentP(p, hp1);
  13567. Result := True;
  13568. Exit;
  13569. end
  13570. else if reg_and_hp1_is_instr and
  13571. (taicpu(p).oper[0]^.typ = top_reg) and
  13572. (
  13573. ((taicpu(hp1).opcode = A_SHR) and (taicpu(p).opcode = A_MOVZX)) or
  13574. ((taicpu(hp1).opcode = A_SAR) and (taicpu(p).opcode <> A_MOVZX))
  13575. ) and
  13576. (taicpu(hp1).oper[0]^.typ = top_const) and
  13577. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  13578. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  13579. { Minimum shift value allowed is the bit size of the smallest register - 1 }
  13580. (taicpu(hp1).oper[0]^.val <
  13581. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  13582. 8 * (
  13583. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  13584. )
  13585. ) then
  13586. begin
  13587. { For:
  13588. movsx %reg1,%reg1 movzx %reg1,%reg1 (same register, just different sizes)
  13589. sar ##, %reg1 shr ##, %reg1
  13590. Move the shift to before the movx instruction if the shift value
  13591. is not too large.
  13592. }
  13593. asml.Remove(hp1);
  13594. asml.InsertBefore(hp1, p);
  13595. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  13596. case taicpu(p).opsize of
  13597. s_BW, S_BL{$ifdef x86_64}, S_BQ{$endif}:
  13598. taicpu(hp1).opsize := S_B;
  13599. S_WL{$ifdef x86_64}, S_WQ{$endif}:
  13600. taicpu(hp1).opsize := S_W;
  13601. {$ifdef x86_64}
  13602. S_LQ:
  13603. taicpu(hp1).opsize := S_L;
  13604. {$endif}
  13605. else
  13606. InternalError(2020112401);
  13607. end;
  13608. if (taicpu(hp1).opcode = A_SHR) then
  13609. DebugMsg(SPeepholeOptimization + 'MovzShr2ShrMovz', hp1)
  13610. else
  13611. DebugMsg(SPeepholeOptimization + 'MovsSar2SarMovs', hp1);
  13612. Result := True;
  13613. end;
  13614. if reg_and_hp1_is_instr and
  13615. (taicpu(p).oper[0]^.typ = top_reg) and
  13616. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  13617. (
  13618. (taicpu(hp1).opcode = taicpu(p).opcode)
  13619. or ((taicpu(p).opcode = A_MOVZX) and ((taicpu(hp1).opcode = A_MOVSX){$ifdef x86_64} or (taicpu(hp1).opcode = A_MOVSXD){$endif x86_64}))
  13620. {$ifdef x86_64}
  13621. or ((taicpu(p).opcode = A_MOVSX) and (taicpu(hp1).opcode = A_MOVSXD))
  13622. {$endif x86_64}
  13623. ) then
  13624. begin
  13625. if MatchOpType(taicpu(hp1), top_reg, top_reg) and
  13626. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[0]^.reg) and
  13627. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg) then
  13628. begin
  13629. {
  13630. For example:
  13631. movzbw %al,%ax
  13632. movzwl %ax,%eax
  13633. Compress into:
  13634. movzbl %al,%eax
  13635. }
  13636. RegUsed := False;
  13637. case taicpu(p).opsize of
  13638. S_BW:
  13639. case taicpu(hp1).opsize of
  13640. S_WL:
  13641. begin
  13642. taicpu(p).opsize := S_BL;
  13643. RegUsed := True;
  13644. end;
  13645. {$ifdef x86_64}
  13646. S_WQ:
  13647. begin
  13648. if taicpu(p).opcode = A_MOVZX then
  13649. begin
  13650. taicpu(p).opsize := S_BL;
  13651. { 64-bit zero extension is implicit, so change to the 32-bit register }
  13652. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  13653. end
  13654. else
  13655. taicpu(p).opsize := S_BQ;
  13656. RegUsed := True;
  13657. end;
  13658. {$endif x86_64}
  13659. else
  13660. ;
  13661. end;
  13662. {$ifdef x86_64}
  13663. S_BL:
  13664. case taicpu(hp1).opsize of
  13665. S_LQ:
  13666. begin
  13667. if taicpu(p).opcode = A_MOVZX then
  13668. begin
  13669. taicpu(p).opsize := S_BL;
  13670. { 64-bit zero extension is implicit, so change to the 32-bit register }
  13671. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  13672. end
  13673. else
  13674. taicpu(p).opsize := S_BQ;
  13675. RegUsed := True;
  13676. end;
  13677. else
  13678. ;
  13679. end;
  13680. S_WL:
  13681. case taicpu(hp1).opsize of
  13682. S_LQ:
  13683. begin
  13684. if taicpu(p).opcode = A_MOVZX then
  13685. begin
  13686. taicpu(p).opsize := S_WL;
  13687. { 64-bit zero extension is implicit, so change to the 32-bit register }
  13688. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  13689. end
  13690. else
  13691. taicpu(p).opsize := S_WQ;
  13692. RegUsed := True;
  13693. end;
  13694. else
  13695. ;
  13696. end;
  13697. {$endif x86_64}
  13698. else
  13699. ;
  13700. end;
  13701. if RegUsed then
  13702. begin
  13703. DebugMsg(SPeepholeOptimization + 'MovxMovx2Movx', p);
  13704. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg;
  13705. RemoveInstruction(hp1);
  13706. Result := True;
  13707. Exit;
  13708. end;
  13709. end;
  13710. if (taicpu(hp1).opsize = taicpu(p).opsize) and
  13711. not RegInInstruction(taicpu(p).oper[1]^.reg, hp1) and
  13712. GetNextInstruction(hp1, hp2) and
  13713. MatchInstruction(hp2, [A_AND, A_OR, A_XOR, A_TEST], []) and
  13714. (
  13715. ((taicpu(hp2).opsize = S_W) and (taicpu(p).opsize = S_BW)) or
  13716. ((taicpu(hp2).opsize = S_L) and (taicpu(p).opsize in [S_BL, S_WL]))
  13717. {$ifdef x86_64}
  13718. or ((taicpu(hp2).opsize = S_Q) and (taicpu(p).opsize in [S_BL, S_BQ, S_WL, S_WQ, S_LQ]))
  13719. {$endif x86_64}
  13720. ) and
  13721. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  13722. (
  13723. (
  13724. (taicpu(hp2).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  13725. (taicpu(hp2).oper[1]^.reg = taicpu(p).oper[1]^.reg)
  13726. ) or
  13727. (
  13728. { Only allow the operands in reverse order for TEST instructions }
  13729. (taicpu(hp2).opcode = A_TEST) and
  13730. (taicpu(hp2).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  13731. (taicpu(hp2).oper[1]^.reg = taicpu(hp1).oper[1]^.reg)
  13732. )
  13733. ) then
  13734. begin
  13735. {
  13736. For example:
  13737. movzbl %al,%eax
  13738. movzbl (ref),%edx
  13739. andl %edx,%eax
  13740. (%edx deallocated)
  13741. Change to:
  13742. andb (ref),%al
  13743. movzbl %al,%eax
  13744. Rules are:
  13745. - First two instructions have the same opcode and opsize
  13746. - First instruction's operands are the same super-register
  13747. - Second instruction operates on a different register
  13748. - Third instruction is AND, OR, XOR or TEST
  13749. - Third instruction's operands are the destination registers of the first two instructions
  13750. - Third instruction writes to the destination register of the first instruction (except with TEST)
  13751. - Second instruction's destination register is deallocated afterwards
  13752. }
  13753. TransferUsedRegs(TmpUsedRegs);
  13754. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  13755. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  13756. if not RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp2, TmpUsedRegs) then
  13757. begin
  13758. case taicpu(p).opsize of
  13759. S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  13760. NewSize := S_B;
  13761. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  13762. NewSize := S_W;
  13763. {$ifdef x86_64}
  13764. S_LQ:
  13765. NewSize := S_L;
  13766. {$endif x86_64}
  13767. else
  13768. InternalError(2021120301);
  13769. end;
  13770. taicpu(hp2).loadoper(0, taicpu(hp1).oper[0]^);
  13771. taicpu(hp2).loadreg(1, taicpu(p).oper[0]^.reg);
  13772. taicpu(hp2).opsize := NewSize;
  13773. RemoveInstruction(hp1);
  13774. { With TEST, it's best to keep the MOVX instruction at the top }
  13775. if (taicpu(hp2).opcode <> A_TEST) then
  13776. begin
  13777. DebugMsg(SPeepholeOptimization + 'MovxMovxTest2MovxTest', p);
  13778. asml.Remove(p);
  13779. { If the third instruction uses the flags, the MOVX instruction won't modify then }
  13780. asml.InsertAfter(p, hp2);
  13781. p := hp2;
  13782. end
  13783. else
  13784. DebugMsg(SPeepholeOptimization + 'MovxMovxOp2OpMovx', p);
  13785. Result := True;
  13786. Exit;
  13787. end;
  13788. end;
  13789. end;
  13790. if taicpu(p).opcode=A_MOVZX then
  13791. begin
  13792. { removes superfluous And's after movzx's }
  13793. if reg_and_hp1_is_instr and
  13794. (taicpu(hp1).opcode = A_AND) and
  13795. MatchOpType(taicpu(hp1),top_const,top_reg) and
  13796. ((taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)
  13797. {$ifdef x86_64}
  13798. { check for implicit extension to 64 bit }
  13799. or
  13800. ((taicpu(p).opsize in [S_BL,S_WL]) and
  13801. (taicpu(hp1).opsize=S_Q) and
  13802. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg)
  13803. )
  13804. {$endif x86_64}
  13805. )
  13806. then
  13807. begin
  13808. case taicpu(p).opsize Of
  13809. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  13810. if (taicpu(hp1).oper[0]^.val = $ff) then
  13811. begin
  13812. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz1',p);
  13813. RemoveInstruction(hp1);
  13814. Result:=true;
  13815. exit;
  13816. end;
  13817. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  13818. if (taicpu(hp1).oper[0]^.val = $ffff) then
  13819. begin
  13820. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz2',p);
  13821. RemoveInstruction(hp1);
  13822. Result:=true;
  13823. exit;
  13824. end;
  13825. {$ifdef x86_64}
  13826. S_LQ:
  13827. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  13828. begin
  13829. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz3',p);
  13830. RemoveInstruction(hp1);
  13831. Result:=true;
  13832. exit;
  13833. end;
  13834. {$endif x86_64}
  13835. else
  13836. ;
  13837. end;
  13838. { we cannot get rid of the and, but can we get rid of the movz ?}
  13839. if SuperRegistersEqual(taicpu(p).oper[0]^.reg,taicpu(p).oper[1]^.reg) then
  13840. begin
  13841. case taicpu(p).opsize Of
  13842. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  13843. if (taicpu(hp1).oper[0]^.val and $ff)=taicpu(hp1).oper[0]^.val then
  13844. begin
  13845. DebugMsg(SPeepholeOptimization + 'MovzAnd2And1',p);
  13846. RemoveCurrentP(p,hp1);
  13847. Result:=true;
  13848. exit;
  13849. end;
  13850. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  13851. if (taicpu(hp1).oper[0]^.val and $ffff)=taicpu(hp1).oper[0]^.val then
  13852. begin
  13853. DebugMsg(SPeepholeOptimization + 'MovzAnd2And2',p);
  13854. RemoveCurrentP(p,hp1);
  13855. Result:=true;
  13856. exit;
  13857. end;
  13858. {$ifdef x86_64}
  13859. S_LQ:
  13860. if (taicpu(hp1).oper[0]^.val and $ffffffff)=taicpu(hp1).oper[0]^.val then
  13861. begin
  13862. DebugMsg(SPeepholeOptimization + 'MovzAnd2And3',p);
  13863. RemoveCurrentP(p,hp1);
  13864. Result:=true;
  13865. exit;
  13866. end;
  13867. {$endif x86_64}
  13868. else
  13869. ;
  13870. end;
  13871. end;
  13872. end;
  13873. { changes some movzx constructs to faster synonyms (all examples
  13874. are given with eax/ax, but are also valid for other registers)}
  13875. if MatchOpType(taicpu(p),top_reg,top_reg) then
  13876. begin
  13877. case taicpu(p).opsize of
  13878. { Technically, movzbw %al,%ax cannot be encoded in 32/64-bit mode
  13879. (the machine code is equivalent to movzbl %al,%eax), but the
  13880. code generator still generates that assembler instruction and
  13881. it is silently converted. This should probably be checked.
  13882. [Kit] }
  13883. S_BW:
  13884. begin
  13885. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  13886. (
  13887. not IsMOVZXAcceptable
  13888. { and $0xff,%ax has a smaller encoding but risks a partial write penalty }
  13889. or (
  13890. (cs_opt_size in current_settings.optimizerswitches) and
  13891. (taicpu(p).oper[1]^.reg = NR_AX)
  13892. )
  13893. ) then
  13894. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  13895. begin
  13896. DebugMsg(SPeepholeOptimization + 'var7',p);
  13897. taicpu(p).opcode := A_AND;
  13898. taicpu(p).changeopsize(S_W);
  13899. taicpu(p).loadConst(0,$ff);
  13900. Result := True;
  13901. end
  13902. else if not IsMOVZXAcceptable and
  13903. GetNextInstruction(p, hp1) and
  13904. (tai(hp1).typ = ait_instruction) and
  13905. (taicpu(hp1).opcode = A_AND) and
  13906. MatchOpType(taicpu(hp1),top_const,top_reg) and
  13907. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  13908. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  13909. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  13910. begin
  13911. DebugMsg(SPeepholeOptimization + 'var8',p);
  13912. taicpu(p).opcode := A_MOV;
  13913. taicpu(p).changeopsize(S_W);
  13914. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  13915. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  13916. Result := True;
  13917. end;
  13918. end;
  13919. {$ifndef i8086} { movzbl %al,%eax cannot be encoded in 16-bit mode (the machine code is equivalent to movzbw %al,%ax }
  13920. S_BL:
  13921. if not IsMOVZXAcceptable then
  13922. begin
  13923. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) then
  13924. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  13925. begin
  13926. DebugMsg(SPeepholeOptimization + 'var9',p);
  13927. taicpu(p).opcode := A_AND;
  13928. taicpu(p).changeopsize(S_L);
  13929. taicpu(p).loadConst(0,$ff);
  13930. Result := True;
  13931. end
  13932. else if GetNextInstruction(p, hp1) and
  13933. (tai(hp1).typ = ait_instruction) and
  13934. (taicpu(hp1).opcode = A_AND) and
  13935. MatchOpType(taicpu(hp1),top_const,top_reg) and
  13936. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  13937. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  13938. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  13939. begin
  13940. DebugMsg(SPeepholeOptimization + 'var10',p);
  13941. taicpu(p).opcode := A_MOV;
  13942. taicpu(p).changeopsize(S_L);
  13943. { do not use R_SUBWHOLE
  13944. as movl %rdx,%eax
  13945. is invalid in assembler PM }
  13946. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  13947. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  13948. Result := True;
  13949. end;
  13950. end;
  13951. {$endif i8086}
  13952. S_WL:
  13953. if not IsMOVZXAcceptable then
  13954. begin
  13955. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) then
  13956. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  13957. begin
  13958. DebugMsg(SPeepholeOptimization + 'var11',p);
  13959. taicpu(p).opcode := A_AND;
  13960. taicpu(p).changeopsize(S_L);
  13961. taicpu(p).loadConst(0,$ffff);
  13962. Result := True;
  13963. end
  13964. else if GetNextInstruction(p, hp1) and
  13965. (tai(hp1).typ = ait_instruction) and
  13966. (taicpu(hp1).opcode = A_AND) and
  13967. (taicpu(hp1).oper[0]^.typ = top_const) and
  13968. (taicpu(hp1).oper[1]^.typ = top_reg) and
  13969. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  13970. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  13971. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  13972. begin
  13973. DebugMsg(SPeepholeOptimization + 'var12',p);
  13974. taicpu(p).opcode := A_MOV;
  13975. taicpu(p).changeopsize(S_L);
  13976. { do not use R_SUBWHOLE
  13977. as movl %rdx,%eax
  13978. is invalid in assembler PM }
  13979. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  13980. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  13981. Result := True;
  13982. end;
  13983. end;
  13984. else
  13985. InternalError(2017050705);
  13986. end;
  13987. end
  13988. else if not IsMOVZXAcceptable and (taicpu(p).oper[0]^.typ = top_ref) then
  13989. begin
  13990. if GetNextInstruction(p, hp1) and
  13991. (tai(hp1).typ = ait_instruction) and
  13992. (taicpu(hp1).opcode = A_AND) and
  13993. MatchOpType(taicpu(hp1),top_const,top_reg) and
  13994. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  13995. begin
  13996. case taicpu(p).opsize Of
  13997. S_BL:
  13998. if (taicpu(hp1).opsize <> S_L) or
  13999. (taicpu(hp1).oper[0]^.val > $FF) then
  14000. begin
  14001. DebugMsg(SPeepholeOptimization + 'var13',p);
  14002. taicpu(hp1).changeopsize(S_L);
  14003. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  14004. Include(OptsToCheck, aoc_ForceNewIteration);
  14005. end;
  14006. S_WL:
  14007. if (taicpu(hp1).opsize <> S_L) or
  14008. (taicpu(hp1).oper[0]^.val > $FFFF) then
  14009. begin
  14010. DebugMsg(SPeepholeOptimization + 'var14',p);
  14011. taicpu(hp1).changeopsize(S_L);
  14012. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  14013. Include(OptsToCheck, aoc_ForceNewIteration);
  14014. end;
  14015. S_BW:
  14016. if (taicpu(hp1).opsize <> S_W) or
  14017. (taicpu(hp1).oper[0]^.val > $FF) then
  14018. begin
  14019. DebugMsg(SPeepholeOptimization + 'var15',p);
  14020. taicpu(hp1).changeopsize(S_W);
  14021. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  14022. Include(OptsToCheck, aoc_ForceNewIteration);
  14023. end;
  14024. else
  14025. Internalerror(2017050704)
  14026. end;
  14027. end;
  14028. end;
  14029. end;
  14030. end;
  14031. {$ifdef x86_64}
  14032. function TX86AsmOptimizer.DoZeroUpper32Opt(var mov_p: tai; var and_p: tai): Boolean;
  14033. var
  14034. hp1, old_hp1: tai;
  14035. FullSourceReg, FullTargetReg: TRegister;
  14036. begin
  14037. if (mov_p.typ<>ait_instruction) or
  14038. (taicpu(mov_p).opsize<>S_L) or
  14039. not MatchOpType(taicpu(mov_p),top_reg,top_reg) then
  14040. InternalError(2025062801);
  14041. Result:=False;
  14042. FullSourceReg:=taicpu(mov_p).oper[0]^.reg; setsubreg(FullSourceReg, R_SUBQ);
  14043. FullTargetReg:=taicpu(mov_p).oper[1]^.reg; setsubreg(FullTargetReg, R_SUBQ);
  14044. { Mark the registers in the MOV command as "used" }
  14045. IncludeRegInUsedRegs(FullSourceReg,UsedRegs);
  14046. IncludeRegInUsedRegs(FullTargetReg,UsedRegs);
  14047. { This is a little hack to get DeepMOVOpt to replace the full 64-bit
  14048. registers. The MOV instruction will be put back as it was afterwards
  14049. (unless it got removed). }
  14050. taicpu(mov_p).oper[0]^.reg:=FullSourceReg;
  14051. taicpu(mov_p).oper[1]^.reg:=FullTargetReg;
  14052. { Start after the and_p otherwise that instruction will be considered
  14053. to have modified the source register }
  14054. old_hp1:=and_p;
  14055. while GetNextInstructionUsingReg(old_hp1,hp1,FullTargetReg) and
  14056. (hp1.typ=ait_instruction) do
  14057. begin
  14058. if RegReadByInstruction(FullTargetReg,hp1) and
  14059. not RegModifiedBetween(FullSourceReg,old_hp1,hp1) and
  14060. DeepMOVOpt(taicpu(mov_p),taicpu(hp1)) then
  14061. begin
  14062. { A change has occurred, just not in mov_p }
  14063. Include(OptsToCheck, aoc_ForceNewIteration);
  14064. TransferUsedRegs(TmpUsedRegs);
  14065. UpdateUsedRegsBetween(TmpUsedRegs,tai(mov_p.Next), hp1);
  14066. if not RegUsedAfterInstruction(FullTargetReg,hp1,TmpUsedRegs) and
  14067. { Just in case something didn't get modified (e.g. an
  14068. implicit register) }
  14069. not RegReadByInstruction(FullTargetReg,hp1) then
  14070. begin
  14071. { We can remove the original MOV }
  14072. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3d done',mov_p);
  14073. RemoveCurrentP(mov_p);
  14074. Result := True;
  14075. Exit;
  14076. end;
  14077. end
  14078. else
  14079. Break;
  14080. old_hp1:=hp1;
  14081. end;
  14082. { Put the MOV instruction back as it was }
  14083. setsubreg(taicpu(mov_p).oper[0]^.reg,R_SUBD);
  14084. setsubreg(taicpu(mov_p).oper[1]^.reg,R_SUBD);
  14085. end;
  14086. {$endif x86_64}
  14087. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  14088. var
  14089. hp1, hp2 : tai;
  14090. MaskLength : Cardinal;
  14091. MaskedBits : TCgInt;
  14092. ActiveReg : TRegister;
  14093. begin
  14094. Result:=false;
  14095. { There are no optimisations for reference targets }
  14096. if (taicpu(p).oper[1]^.typ <> top_reg) then
  14097. Exit;
  14098. { Saves on a bunch of dereferences }
  14099. ActiveReg := taicpu(p).oper[1]^.reg;
  14100. while GetNextInstruction(p, hp1) and
  14101. (hp1.typ = ait_instruction) do
  14102. begin
  14103. if (taicpu(p).oper[0]^.typ = top_const) then
  14104. begin
  14105. case taicpu(hp1).opcode of
  14106. A_AND:
  14107. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  14108. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  14109. { the second register must contain the first one, so compare their subreg types }
  14110. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  14111. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  14112. { change
  14113. and const1, reg
  14114. and const2, reg
  14115. to
  14116. and (const1 and const2), reg
  14117. }
  14118. begin
  14119. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  14120. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  14121. RemoveCurrentP(p, hp1);
  14122. Result:=true;
  14123. exit;
  14124. end;
  14125. A_CMP:
  14126. if (PopCnt(DWord(taicpu(p).oper[0]^.val)) = 1) and { Only 1 bit set }
  14127. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.val) and
  14128. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  14129. { Just check that the condition on the next instruction is compatible }
  14130. GetNextInstruction(hp1, hp2) and
  14131. (hp2.typ = ait_instruction) and
  14132. (taicpu(hp2).condition in [C_Z, C_E, C_NZ, C_NE])
  14133. then
  14134. { change
  14135. and 2^n, reg
  14136. cmp 2^n, reg
  14137. j(c) / set(c) / cmov(c) (c is equal or not equal)
  14138. to
  14139. and 2^n, reg
  14140. test reg, reg
  14141. j(~c) / set(~c) / cmov(~c)
  14142. }
  14143. begin
  14144. { Keep TEST instruction in, rather than remove it, because
  14145. it may trigger other optimisations such as MovAndTest2Test }
  14146. taicpu(hp1).loadreg(0, taicpu(hp1).oper[1]^.reg);
  14147. taicpu(hp1).opcode := A_TEST;
  14148. DebugMsg(SPeepholeOptimization + 'AND/CMP/J(c) -> AND/J(~c) with power of 2 constant', p);
  14149. taicpu(hp2).condition := inverse_cond(taicpu(hp2).condition);
  14150. Result := True;
  14151. Exit;
  14152. end
  14153. else if ((taicpu(p).oper[0]^.val=$ff) or (taicpu(p).oper[0]^.val=$ffff) or (taicpu(p).oper[0]^.val=$ffffffff)) and
  14154. MatchOpType(taicpu(hp1),top_const,top_reg) and
  14155. (taicpu(p).oper[0]^.val>=taicpu(hp1).oper[0]^.val) and
  14156. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg) then
  14157. { change
  14158. and $ff/$ff/$ffff, reg
  14159. cmp val<=$ff/val<=$ffff/val<=$ffffffff, reg
  14160. dealloc reg
  14161. to
  14162. cmp val<=$ff/val<=$ffff/val<=$ffffffff, resized reg
  14163. }
  14164. begin
  14165. TransferUsedRegs(TmpUsedRegs);
  14166. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  14167. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) then
  14168. begin
  14169. DebugMsg(SPeepholeOptimization + 'AND/CMP -> CMP', p);
  14170. case taicpu(p).oper[0]^.val of
  14171. $ff:
  14172. begin
  14173. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBL);
  14174. taicpu(hp1).opsize:=S_B;
  14175. end;
  14176. $ffff:
  14177. begin
  14178. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBW);
  14179. taicpu(hp1).opsize:=S_W;
  14180. end;
  14181. $ffffffff:
  14182. begin
  14183. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  14184. taicpu(hp1).opsize:=S_L;
  14185. end;
  14186. else
  14187. Internalerror(2023030401);
  14188. end;
  14189. RemoveCurrentP(p);
  14190. Result := True;
  14191. Exit;
  14192. end;
  14193. end;
  14194. A_MOVZX:
  14195. if MatchOpType(taicpu(hp1),top_reg,top_reg) and
  14196. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg) and
  14197. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  14198. (
  14199. (
  14200. (taicpu(p).opsize=S_W) and
  14201. (taicpu(hp1).opsize=S_BW)
  14202. ) or
  14203. (
  14204. (taicpu(p).opsize=S_L) and
  14205. (taicpu(hp1).opsize in [S_WL,S_BL{$ifdef x86_64},S_BQ,S_WQ{$endif x86_64}])
  14206. )
  14207. {$ifdef x86_64}
  14208. or
  14209. (
  14210. (taicpu(p).opsize=S_Q) and
  14211. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_BL,S_WL])
  14212. )
  14213. {$endif x86_64}
  14214. ) then
  14215. begin
  14216. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  14217. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  14218. ) or
  14219. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  14220. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  14221. then
  14222. begin
  14223. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  14224. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  14225. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  14226. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  14227. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  14228. }
  14229. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  14230. RemoveInstruction(hp1);
  14231. { See if there are other optimisations possible }
  14232. Continue;
  14233. end;
  14234. end;
  14235. A_SHL:
  14236. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  14237. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  14238. begin
  14239. {$ifopt R+}
  14240. {$define RANGE_WAS_ON}
  14241. {$R-}
  14242. {$endif}
  14243. { get length of potential and mask }
  14244. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  14245. { really a mask? }
  14246. {$ifdef RANGE_WAS_ON}
  14247. {$R+}
  14248. {$endif}
  14249. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  14250. { unmasked part shifted out? }
  14251. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  14252. begin
  14253. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  14254. RemoveCurrentP(p, hp1);
  14255. Result:=true;
  14256. exit;
  14257. end;
  14258. end;
  14259. A_SHR:
  14260. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  14261. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
  14262. (taicpu(hp1).oper[0]^.val <= 63) then
  14263. begin
  14264. { Does SHR combined with the AND cover all the bits?
  14265. e.g. for "andb $252,%reg; shrb $2,%reg" - the "and" can be removed }
  14266. MaskedBits := taicpu(p).oper[0]^.val or ((TCgInt(1) shl taicpu(hp1).oper[0]^.val) - 1);
  14267. if ((taicpu(p).opsize = S_B) and ((MaskedBits and $FF) = $FF)) or
  14268. ((taicpu(p).opsize = S_W) and ((MaskedBits and $FFFF) = $FFFF)) or
  14269. ((taicpu(p).opsize = S_L) and ((MaskedBits and $FFFFFFFF) = $FFFFFFFF)) then
  14270. begin
  14271. DebugMsg(SPeepholeOptimization + 'AndShrToShr done', p);
  14272. RemoveCurrentP(p, hp1);
  14273. Result := True;
  14274. Exit;
  14275. end;
  14276. end;
  14277. A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}:
  14278. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  14279. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg) then
  14280. begin
  14281. if SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  14282. (
  14283. (
  14284. (taicpu(hp1).opsize in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  14285. ((taicpu(p).oper[0]^.val and $7F) = taicpu(p).oper[0]^.val)
  14286. ) or (
  14287. (taicpu(hp1).opsize in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  14288. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val)
  14289. {$ifdef x86_64}
  14290. ) or (
  14291. (taicpu(hp1).opsize = S_LQ) and
  14292. ((taicpu(p).oper[0]^.val and $7fffffff) = taicpu(p).oper[0]^.val)
  14293. {$endif x86_64}
  14294. )
  14295. ) then
  14296. begin
  14297. if (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg){$ifdef x86_64} or (taicpu(hp1).opsize = S_LQ){$endif x86_64} then
  14298. begin
  14299. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  14300. RemoveInstruction(hp1);
  14301. { See if there are other optimisations possible }
  14302. Continue;
  14303. end;
  14304. { The super-registers are the same though.
  14305. Note that this change by itself doesn't improve
  14306. code speed, but it opens up other optimisations. }
  14307. {$ifdef x86_64}
  14308. { Convert 64-bit register to 32-bit }
  14309. case taicpu(hp1).opsize of
  14310. S_BQ:
  14311. begin
  14312. taicpu(hp1).opsize := S_BL;
  14313. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  14314. end;
  14315. S_WQ:
  14316. begin
  14317. taicpu(hp1).opsize := S_WL;
  14318. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  14319. end
  14320. else
  14321. ;
  14322. end;
  14323. {$endif x86_64}
  14324. DebugMsg(SPeepholeOptimization + 'AndMovsxToAndMovzx', hp1);
  14325. taicpu(hp1).opcode := A_MOVZX;
  14326. { See if there are other optimisations possible }
  14327. Continue;
  14328. end;
  14329. end;
  14330. else
  14331. ;
  14332. end;
  14333. end
  14334. else if MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^.reg) and
  14335. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) then
  14336. begin
  14337. {$ifdef x86_64}
  14338. if (taicpu(p).opsize = S_Q) then
  14339. begin
  14340. { Never necessary }
  14341. DebugMsg(SPeepholeOptimization + 'Andq2Nop', p);
  14342. RemoveCurrentP(p, hp1);
  14343. Result := True;
  14344. Exit;
  14345. end;
  14346. {$endif x86_64}
  14347. { Forward check to determine necessity of and %reg,%reg }
  14348. TransferUsedRegs(TmpUsedRegs);
  14349. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  14350. case taicpu(hp1).opcode of
  14351. A_MOV, A_MOVZX, A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}:
  14352. if (
  14353. (taicpu(hp1).oper[0]^.typ <> top_ref) or
  14354. not RegInRef(ActiveReg, taicpu(hp1).oper[0]^.ref^)
  14355. ) and
  14356. (
  14357. (taicpu(hp1).opcode <> A_MOV) or
  14358. (taicpu(hp1).oper[1]^.typ <> top_ref) or
  14359. not RegInRef(ActiveReg, taicpu(hp1).oper[1]^.ref^)
  14360. ) and
  14361. not (
  14362. { If mov %reg,%reg is present, remove that instruction instead in OptPass1MOV }
  14363. (taicpu(hp1).opcode = A_MOV) and
  14364. MatchOperand(taicpu(hp1).oper[0]^, ActiveReg) and
  14365. MatchOperand(taicpu(hp1).oper[1]^, ActiveReg)
  14366. ) and
  14367. (
  14368. (
  14369. (taicpu(hp1).oper[0]^.typ = top_reg) and
  14370. (taicpu(hp1).oper[0]^.reg = ActiveReg) and
  14371. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg)
  14372. ) or
  14373. (
  14374. {$ifdef x86_64}
  14375. (
  14376. { If we read from the register, make sure it's not dependent on the upper 32 bits }
  14377. (taicpu(hp1).oper[0]^.typ <> top_reg) or
  14378. not SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, ActiveReg) or
  14379. (GetSubReg(taicpu(hp1).oper[0]^.reg) <> R_SUBQ)
  14380. ) and
  14381. {$endif x86_64}
  14382. not RegUsedAfterInstruction(ActiveReg, hp1, TmpUsedRegs)
  14383. )
  14384. ) then
  14385. begin
  14386. DebugMsg(SPeepholeOptimization + 'AndMovx2Movx', p);
  14387. RemoveCurrentP(p, hp1);
  14388. Result := True;
  14389. Exit;
  14390. end;
  14391. A_ADD,
  14392. A_AND,
  14393. A_BSF,
  14394. A_BSR,
  14395. A_BTC,
  14396. A_BTR,
  14397. A_BTS,
  14398. A_OR,
  14399. A_SUB,
  14400. A_XOR:
  14401. { Register is written to, so this will clear the upper 32 bits (2-operand instructions) }
  14402. if (
  14403. (taicpu(hp1).oper[0]^.typ <> top_ref) or
  14404. not RegInRef(ActiveReg, taicpu(hp1).oper[0]^.ref^)
  14405. ) and
  14406. MatchOperand(taicpu(hp1).oper[1]^, ActiveReg) then
  14407. begin
  14408. DebugMsg(SPeepholeOptimization + 'AndOp2Op 2', p);
  14409. RemoveCurrentP(p, hp1);
  14410. Result := True;
  14411. Exit;
  14412. end;
  14413. A_CMP,
  14414. A_TEST:
  14415. if (
  14416. (taicpu(hp1).oper[0]^.typ <> top_ref) or
  14417. not RegInRef(ActiveReg, taicpu(hp1).oper[0]^.ref^)
  14418. ) and
  14419. MatchOperand(taicpu(hp1).oper[1]^, ActiveReg) and
  14420. not RegUsedAfterInstruction(ActiveReg, hp1, TmpUsedRegs) then
  14421. begin
  14422. DebugMsg(SPeepholeOptimization + 'AND; CMP/TEST -> CMP/TEST', p);
  14423. RemoveCurrentP(p, hp1);
  14424. Result := True;
  14425. Exit;
  14426. end;
  14427. A_BSWAP,
  14428. A_NEG,
  14429. A_NOT:
  14430. { Register is written to, so this will clear the upper 32 bits (1-operand instructions) }
  14431. if MatchOperand(taicpu(hp1).oper[0]^, ActiveReg) then
  14432. begin
  14433. DebugMsg(SPeepholeOptimization + 'AndOp2Op 1', p);
  14434. RemoveCurrentP(p, hp1);
  14435. Result := True;
  14436. Exit;
  14437. end;
  14438. else
  14439. ;
  14440. end;
  14441. end;
  14442. if (taicpu(hp1).is_jmp) and
  14443. (taicpu(hp1).opcode<>A_JMP) and
  14444. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  14445. begin
  14446. { change
  14447. and x, reg
  14448. jxx
  14449. to
  14450. test x, reg
  14451. jxx
  14452. if reg is deallocated before the
  14453. jump, but only if it's a conditional jump (PFV)
  14454. }
  14455. DebugMsg(SPeepholeOptimization + 'AndJcc2TestJcc', p);
  14456. taicpu(p).opcode := A_TEST;
  14457. Exit;
  14458. end;
  14459. Break;
  14460. end;
  14461. { Lone AND tests }
  14462. if (taicpu(p).oper[0]^.typ = top_const) then
  14463. begin
  14464. {
  14465. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  14466. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  14467. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  14468. }
  14469. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  14470. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  14471. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  14472. begin
  14473. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg);
  14474. if taicpu(p).opsize = S_L then
  14475. begin
  14476. Include(OptsToCheck,aoc_MovAnd2Mov_3);
  14477. Result := True;
  14478. end;
  14479. end;
  14480. end;
  14481. { Backward check to determine necessity of and %reg,%reg }
  14482. if (taicpu(p).oper[0]^.typ = top_reg) and
  14483. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  14484. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) then
  14485. begin
  14486. hp2:=p;
  14487. while GetLastInstruction(hp2, hp2) and
  14488. (cs_opt_level3 in current_settings.optimizerswitches) and
  14489. (hp2.typ=ait_instruction) and
  14490. not RegModifiedByInstruction(ActiveReg,hp2) do { loop };
  14491. if Assigned(hp2) and
  14492. RegModifiedByInstruction(ActiveReg,hp2) and { Also checks if hp2 is an instruction }
  14493. { Check size of instruction to determine if the AND is effectively
  14494. a null operation }
  14495. (
  14496. (taicpu(p).opsize = taicpu(hp2).opsize) or
  14497. { Note: Don't include S_Q }
  14498. ((taicpu(p).opsize = S_L) and (taicpu(hp2).opsize in [S_BL, S_WL])) or
  14499. ((taicpu(p).opsize = S_W) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_L])) or
  14500. ((taicpu(p).opsize = S_B) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_W, S_L]))
  14501. ) then
  14502. begin
  14503. { AND %reg,%reg is unnecessary to zero the upper 32 bits. }
  14504. DebugMsg(SPeepholeOptimization + 'AND %reg,%reg proven unnecessary after backward search (And2Nop)', p);
  14505. RemoveCurrentP(p, hp1);
  14506. Result:=True;
  14507. Exit;
  14508. end;
  14509. end;
  14510. end;
  14511. function TX86AsmOptimizer.OptPass2ADD(var p : tai) : boolean;
  14512. var
  14513. hp1, hp2: tai;
  14514. NewRef: TReference;
  14515. Distance: Cardinal;
  14516. TempTracking: TAllUsedRegs;
  14517. DoAddMov2Lea: Boolean;
  14518. { This entire nested function is used in an if-statement below, but we
  14519. want to avoid all the used reg transfers and GetNextInstruction calls
  14520. until we really have to check }
  14521. function MemRegisterNotUsedLater: Boolean; inline;
  14522. var
  14523. hp2: tai;
  14524. begin
  14525. TransferUsedRegs(TmpUsedRegs);
  14526. if (cs_opt_level3 in current_settings.optimizerswitches) then
  14527. UpdateUsedRegsBetween(TmpUsedRegs, p, hp1)
  14528. else
  14529. { p and hp1 will be adjacent }
  14530. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  14531. Result := not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs);
  14532. end;
  14533. begin
  14534. Result := False;
  14535. if (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif}]) and
  14536. (taicpu(p).oper[1]^.typ = top_reg) then
  14537. begin
  14538. Distance := GetNextInstructionUsingRegCount(p, hp1, taicpu(p).oper[1]^.reg);
  14539. if (Distance = 0) or (Distance > 3) { Likely too far to make a meaningful difference } or
  14540. (hp1.typ <> ait_instruction) or
  14541. not
  14542. (
  14543. (cs_opt_level3 in current_settings.optimizerswitches) or
  14544. { GetNextInstructionUsingRegCount just returns the next valid instruction under -O2 and under }
  14545. RegInInstruction(taicpu(p).oper[1]^.reg, hp1)
  14546. ) then
  14547. Exit;
  14548. { Some of the MOV optimisations are much more in-depth. For example, if we have:
  14549. addq $x, %rax
  14550. movq %rax, %rdx
  14551. sarq $63, %rdx
  14552. (%rax still in use)
  14553. ...letting OptPass2ADD run its course (and without -Os) will produce:
  14554. leaq $x(%rax),%rdx
  14555. addq $x, %rax
  14556. sarq $63, %rdx
  14557. ...which is okay since it breaks the dependency chain between
  14558. addq and movq, but if OptPass2MOV is called first:
  14559. addq $x, %rax
  14560. cqto
  14561. ...which is better in all ways, taking only 2 cycles to execute
  14562. and much smaller in code size.
  14563. }
  14564. { The extra register tracking is quite strenuous }
  14565. if (cs_opt_level2 in current_settings.optimizerswitches) and
  14566. MatchInstruction(hp1, A_MOV, []) then
  14567. begin
  14568. { Update the register tracking to the MOV instruction }
  14569. CopyUsedRegs(TempTracking);
  14570. if (cs_opt_level3 in current_settings.optimizerswitches) then
  14571. UpdateUsedRegsBetween(UsedRegs, p, hp1)
  14572. else
  14573. { p and hp1 will be adjacent }
  14574. UpdateUsedRegs(UsedRegs, tai(p.Next));
  14575. hp2 := hp1;
  14576. if OptPass2MOV(hp1) then
  14577. Include(OptsToCheck, aoc_ForceNewIteration);
  14578. { Reset the tracking to the current instruction }
  14579. RestoreUsedRegs(TempTracking);
  14580. ReleaseUsedRegs(TempTracking);
  14581. { if hp1 <> hp2 after the call, then hp1 got removed, so let
  14582. OptPass2ADD get called again }
  14583. if (hp1 <> hp2) then
  14584. begin
  14585. Result := True;
  14586. Exit;
  14587. end;
  14588. end;
  14589. { Change:
  14590. add %reg2,%reg1
  14591. (%reg2 not modified in between)
  14592. mov/s/z #(%reg1),%reg1 (%reg1 superregisters must be the same)
  14593. To:
  14594. mov/s/z #(%reg1,%reg2),%reg1
  14595. }
  14596. if (taicpu(p).oper[0]^.typ = top_reg) and
  14597. MatchInstruction(hp1, [A_MOV, A_MOVZX, A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif}], []) and
  14598. MatchOpType(taicpu(hp1), top_ref, top_reg) and
  14599. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1) and
  14600. (
  14601. (
  14602. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  14603. (taicpu(hp1).oper[0]^.ref^.index = NR_NO) and
  14604. { r/esp cannot be an index }
  14605. (taicpu(p).oper[0]^.reg<>NR_STACK_POINTER_REG)
  14606. ) or (
  14607. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  14608. (taicpu(hp1).oper[0]^.ref^.base = NR_NO)
  14609. )
  14610. ) and (
  14611. Reg1WriteOverwritesReg2Entirely(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) or
  14612. (
  14613. { If the super registers ARE equal, then this MOV/S/Z does a partial write }
  14614. not SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  14615. MemRegisterNotUsedLater
  14616. )
  14617. ) then
  14618. begin
  14619. if (
  14620. { Instructions are guaranteed to be adjacent on -O2 and under }
  14621. (cs_opt_level3 in current_settings.optimizerswitches) and
  14622. RegModifiedBetween(taicpu(p).oper[0]^.reg, p, hp1)
  14623. ) then
  14624. begin
  14625. { If the other register is used in between, move the MOV
  14626. instruction to right after the ADD instruction so a
  14627. saving can still be made }
  14628. Asml.Remove(hp1);
  14629. Asml.InsertAfter(hp1, p);
  14630. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[1]^.reg;
  14631. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  14632. DebugMsg(SPeepholeOptimization + 'AddMov2Mov done (instruction moved)', p);
  14633. RemoveCurrentp(p, hp1);
  14634. end
  14635. else
  14636. begin
  14637. AllocRegBetween(taicpu(p).oper[0]^.reg, p, hp1, UsedRegs);
  14638. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[1]^.reg;
  14639. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  14640. DebugMsg(SPeepholeOptimization + 'AddMov2Mov done', p);
  14641. if (cs_opt_level3 in current_settings.optimizerswitches) then
  14642. { hp1 may not be the immediate next instruction under -O3 }
  14643. RemoveCurrentp(p)
  14644. else
  14645. RemoveCurrentp(p, hp1);
  14646. end;
  14647. Result := True;
  14648. Exit;
  14649. end;
  14650. { Change:
  14651. addl/q $x,%reg1
  14652. movl/q %reg1,%reg2
  14653. To:
  14654. leal/q $x(%reg1),%reg2
  14655. addl/q $x,%reg1 (can be removed if %reg1 or the flags are not used afterwards)
  14656. Breaks the dependency chain.
  14657. }
  14658. if (taicpu(p).oper[0]^.typ = top_const) and
  14659. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  14660. (taicpu(hp1).oper[1]^.typ = top_reg) and
  14661. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) and
  14662. (
  14663. { Instructions are guaranteed to be adjacent on -O2 and under }
  14664. not (cs_opt_level3 in current_settings.optimizerswitches) or
  14665. (
  14666. { If the flags are used, don't make the optimisation,
  14667. otherwise they will be scrambled. Fixes #41148 }
  14668. (
  14669. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) or
  14670. not RegUsedBetween(NR_DEFAULTFLAGS, p, hp1)
  14671. ) and
  14672. not RegUsedBetween(taicpu(hp1).oper[1]^.reg, p, hp1)
  14673. )
  14674. ) then
  14675. begin
  14676. TransferUsedRegs(TmpUsedRegs);
  14677. if (cs_opt_level3 in current_settings.optimizerswitches) then
  14678. UpdateUsedRegsBetween(TmpUsedRegs, p, hp1)
  14679. else
  14680. { p and hp1 will be adjacent }
  14681. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  14682. if (
  14683. SetAndTest(
  14684. (
  14685. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) and
  14686. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)
  14687. ),
  14688. DoAddMov2Lea
  14689. ) or
  14690. { Don't do AddMov2LeaAdd under -Os, but do allow AddMov2Lea }
  14691. not (cs_opt_size in current_settings.optimizerswitches)
  14692. ) then
  14693. begin
  14694. { Change the MOV instruction to a LEA instruction, and update the
  14695. first operand }
  14696. reference_reset(NewRef, 1, []);
  14697. NewRef.base := taicpu(p).oper[1]^.reg;
  14698. NewRef.scalefactor := 1;
  14699. NewRef.offset := asizeint(taicpu(p).oper[0]^.val);
  14700. taicpu(hp1).opcode := A_LEA;
  14701. taicpu(hp1).loadref(0, NewRef);
  14702. if DoAddMov2Lea then
  14703. begin
  14704. { Since %reg1 or the flags aren't used afterwards, we can delete p completely }
  14705. DebugMsg(SPeepholeOptimization + 'AddMov2Lea', hp1);
  14706. if (cs_opt_level3 in current_settings.optimizerswitches) then
  14707. { hp1 may not be the immediate next instruction under -O3 }
  14708. RemoveCurrentp(p)
  14709. else
  14710. RemoveCurrentp(p, hp1);
  14711. end
  14712. else
  14713. begin
  14714. hp2 := tai(hp1.Next); { for the benefit of AllocRegBetween }
  14715. { Move what is now the LEA instruction to before the ADD instruction }
  14716. Asml.Remove(hp1);
  14717. Asml.InsertBefore(hp1, p);
  14718. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, hp2, UsedRegs);
  14719. DebugMsg(SPeepholeOptimization + 'AddMov2LeaAdd', p);
  14720. p := hp1;
  14721. end;
  14722. Result := True;
  14723. end;
  14724. end;
  14725. end;
  14726. end;
  14727. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  14728. var
  14729. SubReg: TSubRegister;
  14730. hp1, hp2: tai;
  14731. CallJmp: Boolean;
  14732. begin
  14733. Result := False;
  14734. CallJmp := False;
  14735. SubReg := getsubreg(taicpu(p).oper[1]^.reg);
  14736. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  14737. with taicpu(p).oper[0]^.ref^ do
  14738. if not Assigned(symbol) and not Assigned(relsymbol) and (index <> NR_NO) then
  14739. if (offset = 0) then
  14740. begin
  14741. if (scalefactor <= 1) and SuperRegistersEqual(base, taicpu(p).oper[1]^.reg) then
  14742. begin
  14743. taicpu(p).loadreg(0, newreg(R_INTREGISTER, getsupreg(index), SubReg));
  14744. taicpu(p).opcode := A_ADD;
  14745. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  14746. Result := True;
  14747. end
  14748. else if SuperRegistersEqual(index, taicpu(p).oper[1]^.reg) then
  14749. begin
  14750. if (base <> NR_NO) then
  14751. begin
  14752. if (scalefactor <= 1) then
  14753. begin
  14754. taicpu(p).loadreg(0, newreg(R_INTREGISTER, getsupreg(base), SubReg));
  14755. taicpu(p).opcode := A_ADD;
  14756. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  14757. Result := True;
  14758. end;
  14759. end
  14760. else
  14761. { Convert lea (%reg,2^x),%reg to shl x,%reg }
  14762. if (scalefactor in [2, 4, 8]) then
  14763. begin
  14764. { BsrByte is, in essence, the base-2 logarithm of the scale factor }
  14765. taicpu(p).loadconst(0, BsrByte(scalefactor));
  14766. taicpu(p).opcode := A_SHL;
  14767. DebugMsg(SPeepholeOptimization + 'Lea2Shl done',p);
  14768. Result := True;
  14769. end;
  14770. end;
  14771. end
  14772. { lea x(%reg1,%reg2),%reg3 and lea x(symbol,%reg2),%reg3 have a
  14773. lot of latency, so break off the offset if %reg3 is used soon
  14774. afterwards }
  14775. else if not (cs_opt_size in current_settings.optimizerswitches) and
  14776. { If 3-component addresses don't have additional latency, don't
  14777. perform this optimisation }
  14778. not (CPUX86_HINT_FAST_3COMP_ADDR in cpu_optimization_hints[current_settings.optimizecputype]) and
  14779. GetNextInstruction(p, hp1) and
  14780. (hp1.typ = ait_instruction) and
  14781. (
  14782. (
  14783. { Permit jumps and calls since they have a larger degree of overhead }
  14784. (
  14785. not SetAndTest(is_calljmp(taicpu(hp1).opcode), CallJmp) or
  14786. (
  14787. { ... unless the register specifies the location }
  14788. (taicpu(hp1).ops > 0) and
  14789. RegInOp(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[0]^)
  14790. )
  14791. ) and
  14792. (
  14793. not CallJmp and { Use the Boolean result to avoid calling "is_calljmp" twice }
  14794. RegInInstruction(taicpu(p).oper[1]^.reg, hp1)
  14795. )
  14796. )
  14797. or
  14798. (
  14799. { Check up to two instructions ahead }
  14800. GetNextInstruction(hp1, hp2) and
  14801. (hp2.typ = ait_instruction) and
  14802. (
  14803. not SetAndTest(is_calljmp(taicpu(hp2).opcode), CallJmp) or
  14804. (
  14805. { Same as above }
  14806. (taicpu(hp2).ops > 0) and
  14807. RegInOp(taicpu(p).oper[1]^.reg, taicpu(hp2).oper[0]^)
  14808. )
  14809. ) and
  14810. (
  14811. not CallJmp and { Use the Boolean result to avoid calling "is_calljmp" twice }
  14812. RegInInstruction(taicpu(p).oper[1]^.reg, hp2)
  14813. )
  14814. )
  14815. ) then
  14816. begin
  14817. { Offset will be a 32-bit signed integer, so it's safe to use in the 64-bit version of ADD }
  14818. hp2 := taicpu.op_const_reg(A_ADD, taicpu(p).opsize, offset, taicpu(p).oper[1]^.reg);
  14819. taicpu(hp2).fileinfo := taicpu(p).fileinfo;
  14820. offset := 0;
  14821. if Assigned(symbol) or Assigned(relsymbol) then
  14822. DebugMsg(SPeepholeOptimization + 'lea x(sym,%reg1),%reg2 -> lea(sym,%reg1),%reg2; add $x,%reg2 to minimise instruction latency (Lea2LeaAdd)', p)
  14823. else
  14824. DebugMsg(SPeepholeOptimization + 'lea x(%reg1,%reg2),%reg3 -> lea(%reg1,%reg2),%reg3; add $x,%reg3 to minimise instruction latency (Lea2LeaAdd)', p);
  14825. { Inserting before the next instruction rather than after the
  14826. current instruction gives more accurate register tracking }
  14827. asml.InsertBefore(hp2, hp1);
  14828. AllocRegBetween(taicpu(p).oper[1]^.reg, p, hp2, UsedRegs);
  14829. Result := True;
  14830. end;
  14831. end;
  14832. function TX86AsmOptimizer.OptPass2SUB(var p: tai): Boolean;
  14833. var
  14834. hp1, hp2: tai;
  14835. NewRef: TReference;
  14836. Distance: Cardinal;
  14837. TempTracking: TAllUsedRegs;
  14838. DoSubMov2Lea: Boolean;
  14839. begin
  14840. Result := False;
  14841. if (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif}]) and
  14842. MatchOpType(taicpu(p),top_const,top_reg) then
  14843. begin
  14844. Distance := GetNextInstructionUsingRegCount(p, hp1, taicpu(p).oper[1]^.reg);
  14845. if (Distance = 0) or (Distance > 3) { Likely too far to make a meaningful difference } or
  14846. (hp1.typ <> ait_instruction) or
  14847. not
  14848. (
  14849. (cs_opt_level3 in current_settings.optimizerswitches) or
  14850. { GetNextInstructionUsingRegCount just returns the next valid instruction under -O2 and under }
  14851. RegInInstruction(taicpu(p).oper[1]^.reg, hp1)
  14852. ) then
  14853. Exit;
  14854. { Some of the MOV optimisations are much more in-depth. For example, if we have:
  14855. subq $x, %rax
  14856. movq %rax, %rdx
  14857. sarq $63, %rdx
  14858. (%rax still in use)
  14859. ...letting OptPass2SUB run its course (and without -Os) will produce:
  14860. leaq $-x(%rax),%rdx
  14861. movq $x, %rax
  14862. sarq $63, %rdx
  14863. ...which is okay since it breaks the dependency chain between
  14864. subq and movq, but if OptPass2MOV is called first:
  14865. subq $x, %rax
  14866. cqto
  14867. ...which is better in all ways, taking only 2 cycles to execute
  14868. and much smaller in code size.
  14869. }
  14870. { The extra register tracking is quite strenuous }
  14871. if (cs_opt_level2 in current_settings.optimizerswitches) and
  14872. MatchInstruction(hp1, A_MOV, []) then
  14873. begin
  14874. { Update the register tracking to the MOV instruction }
  14875. CopyUsedRegs(TempTracking);
  14876. if (cs_opt_level3 in current_settings.optimizerswitches) then
  14877. UpdateUsedRegsBetween(UsedRegs, p, hp1)
  14878. else
  14879. { p and hp1 will be adjacent }
  14880. UpdateUsedRegs(UsedRegs, tai(p.Next));
  14881. hp2 := hp1;
  14882. if OptPass2MOV(hp1) then
  14883. Include(OptsToCheck, aoc_ForceNewIteration);
  14884. { Reset the tracking to the current instruction }
  14885. RestoreUsedRegs(TempTracking);
  14886. ReleaseUsedRegs(TempTracking);
  14887. { if hp1 <> hp2 after the call, then hp1 got removed, so let
  14888. OptPass2SUB get called again }
  14889. if (hp1 <> hp2) then
  14890. begin
  14891. Result := True;
  14892. Exit;
  14893. end;
  14894. end;
  14895. { Change:
  14896. subl/q $x,%reg1
  14897. movl/q %reg1,%reg2
  14898. To:
  14899. leal/q $-x(%reg1),%reg2
  14900. subl/q $x,%reg1 (can be removed if %reg1 or the flags are not used afterwards)
  14901. Breaks the dependency chain and potentially permits the removal of
  14902. a CMP instruction if one follows.
  14903. }
  14904. if MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  14905. (taicpu(hp1).oper[1]^.typ = top_reg) and
  14906. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) and
  14907. (
  14908. { Instructions are guaranteed to be adjacent on -O2 and under }
  14909. not (cs_opt_level3 in current_settings.optimizerswitches) or
  14910. (
  14911. { If the flags are used, don't make the optimisation,
  14912. otherwise they will be scrambled. Fixes #41148 }
  14913. (
  14914. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) or
  14915. not RegUsedBetween(NR_DEFAULTFLAGS, p, hp1)
  14916. ) and
  14917. not RegUsedBetween(taicpu(hp1).oper[1]^.reg, p, hp1)
  14918. )
  14919. ) then
  14920. begin
  14921. TransferUsedRegs(TmpUsedRegs);
  14922. if (cs_opt_level3 in current_settings.optimizerswitches) then
  14923. UpdateUsedRegsBetween(TmpUsedRegs, p, hp1)
  14924. else
  14925. { p and hp1 will be adjacent }
  14926. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  14927. if (
  14928. SetAndTest(
  14929. (
  14930. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) and
  14931. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)
  14932. ),
  14933. DoSubMov2Lea
  14934. ) or
  14935. { Don't do SubMov2LeaSub under -Os, but do allow SubMov2Lea }
  14936. not (cs_opt_size in current_settings.optimizerswitches)
  14937. ) then
  14938. begin
  14939. { Change the MOV instruction to a LEA instruction, and update the
  14940. first operand }
  14941. reference_reset(NewRef, 1, []);
  14942. NewRef.base := taicpu(p).oper[1]^.reg;
  14943. NewRef.scalefactor := 1;
  14944. NewRef.offset := -taicpu(p).oper[0]^.val;
  14945. taicpu(hp1).opcode := A_LEA;
  14946. taicpu(hp1).loadref(0, NewRef);
  14947. if DoSubMov2Lea then
  14948. begin
  14949. { Since %reg1 or the flags aren't used afterwards, we can delete p completely }
  14950. DebugMsg(SPeepholeOptimization + 'SubMov2Lea', hp1);
  14951. if (cs_opt_level3 in current_settings.optimizerswitches) then
  14952. { hp1 may not be the immediate next instruction under -O3 }
  14953. RemoveCurrentp(p)
  14954. else
  14955. RemoveCurrentp(p, hp1);
  14956. end
  14957. else
  14958. begin
  14959. hp2 := tai(hp1.Next); { for the benefit of AllocRegBetween }
  14960. { Move what is now the LEA instruction to before the SUB instruction }
  14961. Asml.Remove(hp1);
  14962. Asml.InsertBefore(hp1, p);
  14963. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, hp2, UsedRegs);
  14964. DebugMsg(SPeepholeOptimization + 'SubMov2LeaSub', p);
  14965. p := hp1;
  14966. end;
  14967. Result := True;
  14968. end;
  14969. end;
  14970. end;
  14971. end;
  14972. function TX86AsmOptimizer.SkipSimpleInstructions(var hp1 : tai) : Boolean;
  14973. begin
  14974. { we can skip all instructions not messing with the stack pointer }
  14975. while assigned(hp1) and {MatchInstruction(hp1,[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  14976. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  14977. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  14978. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  14979. ({(taicpu(hp1).ops=0) or }
  14980. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  14981. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  14982. ) and }
  14983. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  14984. )
  14985. ) do
  14986. GetNextInstruction(hp1,hp1);
  14987. Result:=assigned(hp1);
  14988. end;
  14989. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  14990. var
  14991. hp1, hp2, hp3, hp4, hp5, hp6, hp7, hp8: tai;
  14992. begin
  14993. Result:=false;
  14994. {$ifdef x86_64}
  14995. { Change:
  14996. lea x(%reg1d,%reg2d),%reg3d
  14997. To:
  14998. lea x(%reg1q,%reg2q),%reg3d
  14999. Reduces the number of bytes of machine code
  15000. }
  15001. if (getsubreg(taicpu(p).oper[1]^.reg)=R_SUBD) and
  15002. (
  15003. (getsubreg(taicpu(p).oper[0]^.ref^.base)=R_SUBD) or
  15004. (getsubreg(taicpu(p).oper[0]^.ref^.index)=R_SUBD)
  15005. ) then
  15006. begin
  15007. DebugMsg(SPeepholeOptimization + 'Changed 32-bit registers in reference to 64-bit (reduces instruction size)', p);
  15008. if (getsubreg(taicpu(p).oper[0]^.ref^.base)=R_SUBD) then
  15009. setsubreg(taicpu(p).oper[0]^.ref^.base,R_SUBQ);
  15010. if (getsubreg(taicpu(p).oper[0]^.ref^.index)=R_SUBD) then
  15011. setsubreg(taicpu(p).oper[0]^.ref^.index,R_SUBQ);
  15012. { No reason to set Result to true }
  15013. end;
  15014. {$endif x86_64}
  15015. hp5:=nil;
  15016. hp6:=nil;
  15017. hp7:=nil;
  15018. hp8:=nil;
  15019. { replace
  15020. leal(q) x(<stackpointer>),<stackpointer>
  15021. <optional .seh_stackalloc ...>
  15022. <optional .seh_endprologue ...>
  15023. call procname
  15024. <optional NOP>
  15025. leal(q) -x(<stackpointer>),<stackpointer>
  15026. <optional VZEROUPPER>
  15027. ret
  15028. by
  15029. jmp procname
  15030. but do it only on level 4 because it destroys stack back traces
  15031. }
  15032. if (cs_opt_level4 in current_settings.optimizerswitches) and
  15033. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  15034. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  15035. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  15036. { the -8, -24, -40 are not required, but bail out early if possible,
  15037. higher values are unlikely }
  15038. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  15039. (taicpu(p).oper[0]^.ref^.offset=-24) or
  15040. (taicpu(p).oper[0]^.ref^.offset=-40)) and
  15041. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  15042. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  15043. GetNextInstruction(p, hp1) and
  15044. { Take a copy of hp1 }
  15045. SetAndTest(hp1, hp4) and
  15046. { trick to skip label }
  15047. ((hp1.typ=ait_instruction) or (SetAndTest(hp1, hp7) and GetNextInstruction(hp1, hp1))) and
  15048. { skip directives, .seh_stackalloc and .seh_endprologue on windows
  15049. ((hp1.typ=ait_instruction) or (SetAndTest(hp1, hp7) and GetNextInstruction(hp1, hp1))) and
  15050. ((hp1.typ=ait_instruction) or (SetAndTest(hp1, hp8) and GetNextInstruction(hp1, hp1))) and }
  15051. SkipSimpleInstructions(hp1) and
  15052. MatchInstruction(hp1,A_CALL,[S_NO]) and
  15053. GetNextInstruction(hp1, hp2) and
  15054. (MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) or
  15055. { skip nop instruction on win64 }
  15056. (MatchInstruction(hp2,A_NOP,[S_NO]) and
  15057. SetAndTest(hp2,hp6) and
  15058. GetNextInstruction(hp2,hp2) and
  15059. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]))
  15060. ) and
  15061. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  15062. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  15063. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  15064. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  15065. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  15066. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  15067. { Segment register will be NR_NO }
  15068. GetNextInstruction(hp2, hp3) and
  15069. { trick to skip label }
  15070. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  15071. (MatchInstruction(hp3,A_RET,[S_NO]) or
  15072. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  15073. SetAndTest(hp3,hp5) and
  15074. GetNextInstruction(hp3,hp3) and
  15075. MatchInstruction(hp3,A_RET,[S_NO])
  15076. )
  15077. ) and
  15078. (taicpu(hp3).ops=0) then
  15079. begin
  15080. taicpu(hp1).opcode := A_JMP;
  15081. taicpu(hp1).is_jmp := true;
  15082. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  15083. { search for the stackalloc directive and remove it }
  15084. hp7:=tai(p.next);
  15085. while assigned(hp7) and (tai(hp7).typ<>ait_instruction) do
  15086. begin
  15087. if (hp7.typ=ait_seh_directive) and (tai_seh_directive(hp7).kind=ash_stackalloc) then
  15088. begin
  15089. { sanity check }
  15090. if taicpu(p).oper[0]^.ref^.offset<>-tai_seh_directive(hp7).data.offset then
  15091. Internalerror(2024012201);
  15092. hp8:=tai(hp7.next);
  15093. RemoveInstruction(tai(hp7));
  15094. hp7:=hp8;
  15095. break;
  15096. end
  15097. else
  15098. hp7:=tai(hp7.next);
  15099. end;
  15100. RemoveCurrentP(p, hp4);
  15101. RemoveInstruction(hp2);
  15102. RemoveInstruction(hp3);
  15103. { if there is a vzeroupper instruction then move it before the jmp }
  15104. if Assigned(hp5) then
  15105. begin
  15106. AsmL.Remove(hp5);
  15107. ASmL.InsertBefore(hp5,hp1)
  15108. end;
  15109. { remove nop on win64 }
  15110. if Assigned(hp6) then
  15111. RemoveInstruction(hp6);
  15112. Result:=true;
  15113. end;
  15114. end;
  15115. function TX86AsmOptimizer.PostPeepholeOptPush(var p : tai) : Boolean;
  15116. {$ifdef x86_64}
  15117. var
  15118. hp1, hp2, hp3, hp4, hp5: tai;
  15119. {$endif x86_64}
  15120. begin
  15121. Result:=false;
  15122. {$ifdef x86_64}
  15123. hp5:=nil;
  15124. { replace
  15125. push %rax
  15126. call procname
  15127. pop %rcx
  15128. ret
  15129. by
  15130. jmp procname
  15131. but do it only on level 4 because it destroys stack back traces
  15132. It depends on the fact, that the sequence push rax/pop rcx is used for stack alignment as rcx is volatile
  15133. for all supported calling conventions
  15134. }
  15135. if (cs_opt_level4 in current_settings.optimizerswitches) and
  15136. MatchOpType(taicpu(p),top_reg) and
  15137. (taicpu(p).oper[0]^.reg=NR_RAX) and
  15138. GetNextInstruction(p, hp1) and
  15139. { Take a copy of hp1 }
  15140. SetAndTest(hp1, hp4) and
  15141. { trick to skip label }
  15142. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  15143. SkipSimpleInstructions(hp1) and
  15144. MatchInstruction(hp1,A_CALL,[S_NO]) and
  15145. GetNextInstruction(hp1, hp2) and
  15146. MatchInstruction(hp2,A_POP,[taicpu(p).opsize]) and
  15147. MatchOpType(taicpu(hp2),top_reg) and
  15148. (taicpu(hp2).oper[0]^.reg=NR_RCX) and
  15149. GetNextInstruction(hp2, hp3) and
  15150. { trick to skip label }
  15151. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  15152. (MatchInstruction(hp3,A_RET,[S_NO]) or
  15153. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  15154. SetAndTest(hp3,hp5) and
  15155. GetNextInstruction(hp3,hp3) and
  15156. MatchInstruction(hp3,A_RET,[S_NO])
  15157. )
  15158. ) and
  15159. (taicpu(hp3).ops=0) then
  15160. begin
  15161. taicpu(hp1).opcode := A_JMP;
  15162. taicpu(hp1).is_jmp := true;
  15163. DebugMsg(SPeepholeOptimization + 'PushCallPushRet2Jmp done',p);
  15164. RemoveCurrentP(p, hp4);
  15165. RemoveInstruction(hp2);
  15166. RemoveInstruction(hp3);
  15167. if Assigned(hp5) then
  15168. begin
  15169. AsmL.Remove(hp5);
  15170. ASmL.InsertBefore(hp5,hp1)
  15171. end;
  15172. Result:=true;
  15173. end;
  15174. {$endif x86_64}
  15175. end;
  15176. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  15177. var
  15178. Value, RegName: string;
  15179. hp1: tai;
  15180. begin
  15181. Result:=false;
  15182. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  15183. begin
  15184. case taicpu(p).oper[0]^.val of
  15185. 0:
  15186. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  15187. if not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs) or
  15188. (
  15189. { See if we can still convert the instruction }
  15190. GetNextInstructionUsingReg(p, hp1, NR_DEFAULTFLAGS) and
  15191. RegLoadedWithNewValue(NR_DEFAULTFLAGS, hp1)
  15192. ) then
  15193. begin
  15194. { change "mov $0,%reg" into "xor %reg,%reg" }
  15195. taicpu(p).opcode := A_XOR;
  15196. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  15197. Result := True;
  15198. {$ifdef x86_64}
  15199. end
  15200. else if (taicpu(p).opsize = S_Q) then
  15201. begin
  15202. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  15203. { The actual optimization }
  15204. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  15205. taicpu(p).changeopsize(S_L);
  15206. DebugMsg(SPeepholeOptimization + 'movq $0,' + RegName + ' -> movl $0,' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  15207. Result := True;
  15208. end;
  15209. $1..$FFFFFFFF:
  15210. begin
  15211. { Code size reduction by J. Gareth "Kit" Moreton }
  15212. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  15213. case taicpu(p).opsize of
  15214. S_Q:
  15215. begin
  15216. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  15217. Value := debug_tostr(taicpu(p).oper[0]^.val);
  15218. { The actual optimization }
  15219. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  15220. taicpu(p).changeopsize(S_L);
  15221. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  15222. Result := True;
  15223. end;
  15224. else
  15225. { Do nothing };
  15226. end;
  15227. {$endif x86_64}
  15228. end;
  15229. -1:
  15230. { Don't make this optimisation if the CPU flags are required, since OR scrambles them }
  15231. if (cs_opt_size in current_settings.optimizerswitches) and
  15232. (taicpu(p).opsize <> S_B) and
  15233. (
  15234. not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs) or
  15235. (
  15236. { See if we can still convert the instruction }
  15237. GetNextInstructionUsingReg(p, hp1, NR_DEFAULTFLAGS) and
  15238. RegLoadedWithNewValue(NR_DEFAULTFLAGS, hp1)
  15239. )
  15240. ) then
  15241. begin
  15242. { change "mov $-1,%reg" into "or $-1,%reg" }
  15243. { NOTES:
  15244. - No size saving is made when changing a Word-sized assignment unless the register is AX (smaller encoding)
  15245. - This operation creates a false dependency on the register, so only do it when optimising for size
  15246. - 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
  15247. }
  15248. taicpu(p).opcode := A_OR;
  15249. DebugMsg(SPeepholeOptimization + 'Mov-12Or-1',p);
  15250. Result := True;
  15251. end;
  15252. else
  15253. { Do nothing };
  15254. end;
  15255. end;
  15256. end;
  15257. { Returns true if the given logic instruction can be converted into a BTx instruction (BT not included) }
  15258. class function TX86AsmOptimizer.IsBTXAcceptable(p : tai) : boolean;
  15259. begin
  15260. Result := False;
  15261. if not (CPUX86_HAS_BTX in cpu_capabilities[current_settings.optimizecputype]) then
  15262. Exit;
  15263. { For sizes less than S_L, the byte size is equal or larger with BTx,
  15264. so don't bother optimising }
  15265. if not MatchInstruction(p, A_AND, A_OR, A_XOR, [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) then
  15266. Exit;
  15267. if (taicpu(p).oper[0]^.typ <> top_const) or
  15268. { If the value can fit into an 8-bit signed integer, a smaller
  15269. instruction can be encoded with AND/OR/XOR, so don't optimise if it
  15270. falls within this range }
  15271. (
  15272. (taicpu(p).oper[0]^.val > -128) and
  15273. (taicpu(p).oper[0]^.val <= 127)
  15274. ) then
  15275. Exit;
  15276. { If we're optimising for size, this is acceptable }
  15277. if (cs_opt_size in current_settings.optimizerswitches) then
  15278. Exit(True);
  15279. if (taicpu(p).oper[1]^.typ = top_reg) and
  15280. (CPUX86_HINT_FAST_BTX_REG_IMM in cpu_optimization_hints[current_settings.optimizecputype]) then
  15281. Exit(True);
  15282. if (taicpu(p).oper[1]^.typ <> top_reg) and
  15283. (CPUX86_HINT_FAST_BTX_MEM_IMM in cpu_optimization_hints[current_settings.optimizecputype]) then
  15284. Exit(True);
  15285. end;
  15286. function TX86AsmOptimizer.PostPeepholeOptAnd(var p : tai) : boolean;
  15287. var
  15288. hp1: tai;
  15289. Value: TCGInt;
  15290. begin
  15291. Result := False;
  15292. if MatchOpType(taicpu(p), top_const, top_reg) then
  15293. begin
  15294. { Detect:
  15295. andw x, %ax (0 <= x < $8000)
  15296. ...
  15297. movzwl %ax,%eax
  15298. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  15299. }
  15300. if (taicpu(p).oper[1]^.reg = NR_AX) and { This is also enough to determine that opsize = S_W }
  15301. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val) and
  15302. GetNextInstructionUsingReg(p, hp1, NR_EAX) and
  15303. MatchInstruction(hp1, A_MOVZX, [S_WL]) and
  15304. MatchOperand(taicpu(hp1).oper[0]^, NR_AX) and
  15305. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) then
  15306. begin
  15307. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via AndMovz2AndCwtl)', hp1);
  15308. taicpu(hp1).opcode := A_CWDE;
  15309. taicpu(hp1).clearop(0);
  15310. taicpu(hp1).clearop(1);
  15311. taicpu(hp1).ops := 0;
  15312. { A change was made, but not with p, so don't set Result, but
  15313. notify the compiler that a change was made }
  15314. Include(OptsToCheck, aoc_ForceNewIteration);
  15315. Exit; { and -> btr won't happen because an opsize of S_W won't be optimised anyway }
  15316. end;
  15317. end;
  15318. { If "not x" is a power of 2 (popcnt = 1), change:
  15319. and $x, %reg/ref
  15320. To:
  15321. btr lb(x), %reg/ref
  15322. }
  15323. if IsBTXAcceptable(p) and
  15324. (
  15325. { Make sure a TEST doesn't follow that plays with the register }
  15326. not GetNextInstruction(p, hp1) or
  15327. not MatchInstruction(hp1, A_TEST, A_CMP, [taicpu(p).opsize]) or
  15328. not MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg)
  15329. ) then
  15330. begin
  15331. {$push}{$R-}{$Q-}
  15332. { Value is a sign-extended 32-bit integer - just correct it
  15333. if it's represented as an unsigned value. Also, IsBTXAcceptable
  15334. checks to see if this operand is an immediate. }
  15335. Value := not taicpu(p).oper[0]^.val;
  15336. {$pop}
  15337. {$ifdef x86_64}
  15338. if taicpu(p).opsize = S_L then
  15339. {$endif x86_64}
  15340. Value := Value and $FFFFFFFF;
  15341. if (PopCnt(QWord(Value)) = 1) then
  15342. begin
  15343. DebugMsg(SPeepholeOptimization + 'Changed AND (not $' + debug_hexstr(taicpu(p).oper[0]^.val) + ') to BTR $' + debug_tostr(BsrQWord(Value)) + ' to shrink instruction size (And2Btr)', p);
  15344. taicpu(p).opcode := A_BTR;
  15345. taicpu(p).oper[0]^.val := BsrQWord(Value); { Essentially the base 2 logarithm }
  15346. Result := True;
  15347. Exit;
  15348. end;
  15349. end;
  15350. end;
  15351. function TX86AsmOptimizer.PostPeepholeOptMOVSX(var p : tai) : boolean;
  15352. begin
  15353. Result := False;
  15354. if not MatchOpType(taicpu(p), top_reg, top_reg) then
  15355. Exit;
  15356. { Convert:
  15357. movswl %ax,%eax -> cwtl
  15358. movslq %eax,%rax -> cdqe
  15359. NOTE: Don't convert movswl %al,%ax to cbw, because cbw and cwde
  15360. refer to the same opcode and depends only on the assembler's
  15361. current operand-size attribute. [Kit]
  15362. }
  15363. with taicpu(p) do
  15364. case opsize of
  15365. S_WL:
  15366. if (oper[0]^.reg = NR_AX) and (oper[1]^.reg = NR_EAX) then
  15367. begin
  15368. DebugMsg(SPeepholeOptimization + 'Converted movswl %ax,%eax to cwtl', p);
  15369. opcode := A_CWDE;
  15370. clearop(0);
  15371. clearop(1);
  15372. ops := 0;
  15373. Result := True;
  15374. end;
  15375. {$ifdef x86_64}
  15376. S_LQ:
  15377. if (oper[0]^.reg = NR_EAX) and (oper[1]^.reg = NR_RAX) then
  15378. begin
  15379. DebugMsg(SPeepholeOptimization + 'Converted movslq %eax,%rax to cltq', p);
  15380. opcode := A_CDQE;
  15381. clearop(0);
  15382. clearop(1);
  15383. ops := 0;
  15384. Result := True;
  15385. end;
  15386. {$endif x86_64}
  15387. else
  15388. ;
  15389. end;
  15390. end;
  15391. function TX86AsmOptimizer.PostPeepholeOptShr(var p : tai) : boolean;
  15392. var
  15393. hp1: tai;
  15394. begin
  15395. Result := False;
  15396. { All these optimisations work on "shr const,%reg" }
  15397. if not MatchOpType(taicpu(p), top_const, top_reg) then
  15398. Exit;
  15399. if HandleSHRMerge(p, True) then
  15400. begin
  15401. Result := True;
  15402. Exit;
  15403. end;
  15404. { Detect the following (looking backwards):
  15405. shr %cl,%reg
  15406. shr x, %reg
  15407. Swap the two SHR instructions to minimise a pipeline stall.
  15408. }
  15409. if GetLastInstruction(p, hp1) and
  15410. MatchInstruction(hp1, A_SHR, [taicpu(p).opsize]) and
  15411. MatchOpType(taicpu(hp1), top_reg, top_reg) and
  15412. { First operand will be %cl }
  15413. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) and
  15414. { Just to be sure }
  15415. (getsupreg(taicpu(hp1).oper[1]^.reg) <> RS_ECX) then
  15416. begin
  15417. DebugMsg(SPeepholeOptimization + 'Swapped variable and constant SHR instructions to minimise pipeline stall (ShrShr2ShrShr)', hp1);
  15418. { Moving the entries this way ensures the register tracking remains correct }
  15419. Asml.Remove(p);
  15420. Asml.InsertBefore(p, hp1);
  15421. p := hp1;
  15422. { Don't set Result to True because the current instruction is now
  15423. "shr %cl,%reg" and there's nothing more we can do with it }
  15424. end;
  15425. end;
  15426. function TX86AsmOptimizer.PostPeepholeOptADDSUB(var p : tai) : boolean;
  15427. var
  15428. hp1, hp2: tai;
  15429. Opposite, SecondOpposite: TAsmOp;
  15430. NewCond: TAsmCond;
  15431. begin
  15432. Result := False;
  15433. { Change:
  15434. add/sub 128,(dest)
  15435. To:
  15436. sub/add -128,(dest)
  15437. This generaally takes fewer bytes to encode because -128 can be stored
  15438. in a signed byte, whereas +128 cannot.
  15439. }
  15440. if (taicpu(p).opsize <> S_B) and MatchOperand(taicpu(p).oper[0]^, 128) then
  15441. begin
  15442. if taicpu(p).opcode = A_ADD then
  15443. Opposite := A_SUB
  15444. else
  15445. Opposite := A_ADD;
  15446. { Be careful if the flags are in use, because the CF flag inverts
  15447. when changing from ADD to SUB and vice versa }
  15448. if RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  15449. GetNextInstruction(p, hp1) then
  15450. begin
  15451. TransferUsedRegs(TmpUsedRegs);
  15452. TmpUsedRegs[R_SPECIALREGISTER].Update(tai(p.Next), True);
  15453. hp2 := hp1;
  15454. { Scan ahead to check if everything's safe }
  15455. while Assigned(hp1) and RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) do
  15456. begin
  15457. if (hp1.typ <> ait_instruction) then
  15458. { Probably unsafe since the flags are still in use }
  15459. Exit;
  15460. if MatchInstruction(hp1, A_CALL, A_JMP, A_RET, []) then
  15461. { Stop searching at an unconditional jump }
  15462. Break;
  15463. if not
  15464. (
  15465. MatchInstruction(hp1, A_ADC, A_SBB, []) and
  15466. (taicpu(hp1).oper[0]^.typ = top_const) { We need to be able to invert a constant }
  15467. ) and
  15468. (taicpu(hp1).condition = C_None) and RegInInstruction(NR_DEFAULTFLAGS, hp1) then
  15469. { Instruction depends on FLAGS (and is not ADC or SBB); break out }
  15470. Exit;
  15471. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  15472. TmpUsedRegs[R_SPECIALREGISTER].Update(tai(hp1.Next), True);
  15473. { Move to the next instruction }
  15474. GetNextInstruction(hp1, hp1);
  15475. end;
  15476. while Assigned(hp2) and (hp2 <> hp1) do
  15477. begin
  15478. NewCond := C_None;
  15479. case taicpu(hp2).condition of
  15480. C_A, C_NBE:
  15481. NewCond := C_BE;
  15482. C_B, C_C, C_NAE:
  15483. NewCond := C_AE;
  15484. C_AE, C_NB, C_NC:
  15485. NewCond := C_B;
  15486. C_BE, C_NA:
  15487. NewCond := C_A;
  15488. else
  15489. { No change needed };
  15490. end;
  15491. if NewCond <> C_None then
  15492. begin
  15493. DebugMsg(SPeepholeOptimization + 'Condition changed from ' + cond2str[taicpu(hp2).condition] + ' to ' + cond2str[NewCond] +
  15494. ' to accommodate ' + debug_op2str(taicpu(p).opcode) + ' -> ' + debug_op2str(opposite) + ' above', hp2);
  15495. taicpu(hp2).condition := NewCond;
  15496. end
  15497. else
  15498. if MatchInstruction(hp2, A_ADC, A_SBB, []) then
  15499. begin
  15500. { Because of the flipping of the carry bit, to ensure
  15501. the operation remains equivalent, ADC becomes SBB
  15502. and vice versa, and the constant is not-inverted.
  15503. If multiple ADCs or SBBs appear in a row, each one
  15504. changed causes the carry bit to invert, so they all
  15505. need to be flipped }
  15506. if taicpu(hp2).opcode = A_ADC then
  15507. SecondOpposite := A_SBB
  15508. else
  15509. SecondOpposite := A_ADC;
  15510. if taicpu(hp2).oper[0]^.typ <> top_const then
  15511. { Should have broken out of this optimisation already }
  15512. InternalError(2021112901);
  15513. DebugMsg(SPeepholeOptimization + debug_op2str(taicpu(hp2).opcode) + debug_opsize2str(taicpu(hp2).opsize) + ' $' + debug_tostr(taicpu(hp2).oper[0]^.val) + ',' + debug_operstr(taicpu(hp2).oper[1]^) + ' -> ' +
  15514. debug_op2str(SecondOpposite) + debug_opsize2str(taicpu(hp2).opsize) + ' $' + debug_tostr(not taicpu(hp2).oper[0]^.val) + ',' + debug_operstr(taicpu(hp2).oper[1]^) + ' to accommodate inverted carry bit', hp2);
  15515. { Bit-invert the constant (effectively equivalent to "-1 - val") }
  15516. taicpu(hp2).opcode := SecondOpposite;
  15517. taicpu(hp2).oper[0]^.val := not taicpu(hp2).oper[0]^.val;
  15518. end;
  15519. { Move to the next instruction }
  15520. GetNextInstruction(hp2, hp2);
  15521. end;
  15522. if (hp2 <> hp1) then
  15523. InternalError(2021111501);
  15524. end;
  15525. DebugMsg(SPeepholeOptimization + debug_op2str(taicpu(p).opcode) + debug_opsize2str(taicpu(p).opsize) + ' $128,' + debug_operstr(taicpu(p).oper[1]^) + ' changed to ' +
  15526. debug_op2str(opposite) + debug_opsize2str(taicpu(p).opsize) + ' $-128,' + debug_operstr(taicpu(p).oper[1]^) + ' to reduce instruction size', p);
  15527. taicpu(p).opcode := Opposite;
  15528. taicpu(p).oper[0]^.val := -128;
  15529. { No further optimisations can be made on this instruction, so move
  15530. onto the next one to save time }
  15531. p := tai(p.Next);
  15532. UpdateUsedRegs(p);
  15533. Result := True;
  15534. Exit;
  15535. end;
  15536. { Detect:
  15537. add/sub %reg2,(dest)
  15538. add/sub x, (dest)
  15539. (dest can be a register or a reference)
  15540. Swap the instructions to minimise a pipeline stall. This reverses the
  15541. "Add swap" and "Sub swap" optimisations done in pass 1 if no new
  15542. optimisations could be made.
  15543. }
  15544. if (taicpu(p).oper[0]^.typ = top_reg) and
  15545. not RegInOp(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^) and
  15546. (
  15547. (
  15548. (taicpu(p).oper[1]^.typ = top_reg) and
  15549. { We can try searching further ahead if we're writing to a register }
  15550. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[1]^.reg)
  15551. ) or
  15552. (
  15553. (taicpu(p).oper[1]^.typ = top_ref) and
  15554. GetNextInstruction(p, hp1)
  15555. )
  15556. ) and
  15557. MatchInstruction(hp1, A_ADD, A_SUB, [taicpu(p).opsize]) and
  15558. (taicpu(hp1).oper[0]^.typ = top_const) and
  15559. MatchOperand(taicpu(p).oper[1]^, taicpu(hp1).oper[1]^) then
  15560. begin
  15561. { Make doubly sure the flags aren't in use because the order of additions may affect them }
  15562. TransferUsedRegs(TmpUsedRegs);
  15563. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  15564. hp2 := p;
  15565. while not (cs_opt_level3 in current_settings.optimizerswitches) and
  15566. GetNextInstruction(hp2, hp2) and (hp2 <> hp1) do
  15567. UpdateUsedRegs(TmpUsedRegs, tai(hp2.next));
  15568. if not RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) then
  15569. begin
  15570. asml.remove(hp1);
  15571. asml.InsertBefore(hp1, p);
  15572. DebugMsg(SPeepholeOptimization + 'Add/Sub swap 2 done', hp1);
  15573. Result := True;
  15574. end;
  15575. end;
  15576. end;
  15577. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  15578. var
  15579. hp1: tai;
  15580. begin
  15581. Result:=false;
  15582. { Final check to see if CMP/MOV pairs can be changed to MOV/CMP }
  15583. while GetNextInstruction(p, hp1) and
  15584. TrySwapMovCmp(p, hp1) do
  15585. begin
  15586. if MatchInstruction(hp1, A_MOV, []) then
  15587. begin
  15588. if RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) then
  15589. begin
  15590. { A little hacky, but since CMP doesn't read the flags, only
  15591. modify them, it's safe if they get scrambled by MOV -> XOR }
  15592. ExcludeRegFromUsedRegs(NR_DEFAULTFLAGS, UsedRegs);
  15593. Result := PostPeepholeOptMov(hp1);
  15594. {$ifdef x86_64}
  15595. if Result and MatchInstruction(hp1, A_XOR, [S_Q]) then
  15596. { Used to shrink instruction size }
  15597. PostPeepholeOptXor(hp1);
  15598. {$endif x86_64}
  15599. IncludeRegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs);
  15600. end
  15601. else
  15602. begin
  15603. Result := PostPeepholeOptMov(hp1);
  15604. {$ifdef x86_64}
  15605. if Result and MatchInstruction(hp1, A_XOR, [S_Q]) then
  15606. { Used to shrink instruction size }
  15607. PostPeepholeOptXor(hp1);
  15608. {$endif x86_64}
  15609. end;
  15610. end;
  15611. { Enabling this flag is actually a null operation, but it marks
  15612. the code as 'modified' during this pass }
  15613. Include(OptsToCheck, aoc_ForceNewIteration);
  15614. end;
  15615. { change "cmp $0, %reg" to "test %reg, %reg" }
  15616. if MatchOpType(taicpu(p),top_const,top_reg) and
  15617. (taicpu(p).oper[0]^.val = 0) then
  15618. begin
  15619. taicpu(p).opcode := A_TEST;
  15620. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  15621. DebugMsg(SPeepholeOptimization + 'Cmp2Test', p);
  15622. Result:=true;
  15623. end;
  15624. end;
  15625. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  15626. var
  15627. IsTestConstX, IsValid : Boolean;
  15628. hp1,hp2 : tai;
  15629. begin
  15630. Result:=false;
  15631. { Final check to see if TEST/MOV pairs can be changed to MOV/TEST }
  15632. if (taicpu(p).opcode = A_TEST) then
  15633. while GetNextInstruction(p, hp1) and
  15634. TrySwapMovCmp(p, hp1) do
  15635. begin
  15636. if MatchInstruction(hp1, A_MOV, []) then
  15637. begin
  15638. if RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) then
  15639. begin
  15640. { A little hacky, but since TEST doesn't read the flags, only
  15641. modify them, it's safe if they get scrambled by MOV -> XOR }
  15642. ExcludeRegFromUsedRegs(NR_DEFAULTFLAGS, UsedRegs);
  15643. Result := PostPeepholeOptMov(hp1);
  15644. {$ifdef x86_64}
  15645. if Result and MatchInstruction(hp1, A_XOR, [S_Q]) then
  15646. { Used to shrink instruction size }
  15647. PostPeepholeOptXor(hp1);
  15648. {$endif x86_64}
  15649. IncludeRegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs);
  15650. end
  15651. else
  15652. begin
  15653. Result := PostPeepholeOptMov(hp1);
  15654. {$ifdef x86_64}
  15655. if Result and MatchInstruction(hp1, A_XOR, [S_Q]) then
  15656. { Used to shrink instruction size }
  15657. PostPeepholeOptXor(hp1);
  15658. {$endif x86_64}
  15659. end;
  15660. end;
  15661. { Enabling this flag is actually a null operation, but it marks
  15662. the code as 'modified' during this pass }
  15663. Include(OptsToCheck, aoc_ForceNewIteration);
  15664. end;
  15665. { If x is a power of 2 (popcnt = 1), change:
  15666. or $x, %reg/ref
  15667. To:
  15668. bts lb(x), %reg/ref
  15669. }
  15670. if (taicpu(p).opcode = A_OR) and
  15671. IsBTXAcceptable(p) and
  15672. { IsBTXAcceptable checks to see if oper[0] is an immediate }
  15673. (PopCnt(QWord(taicpu(p).oper[0]^.val)) = 1) and
  15674. (
  15675. { Don't optimise if a test instruction follows }
  15676. not GetNextInstruction(p, hp1) or
  15677. not MatchInstruction(hp1, A_TEST, [taicpu(p).opsize])
  15678. ) then
  15679. begin
  15680. DebugMsg(SPeepholeOptimization + 'Changed OR $' + debug_hexstr(taicpu(p).oper[0]^.val) + ' to BTS $' + debug_tostr(BsrQWord(taicpu(p).oper[0]^.val)) + ' to shrink instruction size (Or2Bts)', p);
  15681. taicpu(p).opcode := A_BTS;
  15682. taicpu(p).oper[0]^.val := BsrQWord(taicpu(p).oper[0]^.val); { Essentially the base 2 logarithm }
  15683. Result := True;
  15684. Exit;
  15685. end;
  15686. { If x is a power of 2 (popcnt = 1), change:
  15687. test $x, %reg/ref
  15688. je / sete / cmove (or jne / setne)
  15689. To:
  15690. bt lb(x), %reg/ref
  15691. jnc / setnc / cmovnc (or jc / setc / cmovnc)
  15692. }
  15693. if (taicpu(p).opcode = A_TEST) and
  15694. (CPUX86_HAS_BTX in cpu_capabilities[current_settings.optimizecputype]) and
  15695. (taicpu(p).oper[0]^.typ = top_const) and
  15696. (
  15697. (cs_opt_size in current_settings.optimizerswitches) or
  15698. (
  15699. (taicpu(p).oper[1]^.typ = top_reg) and
  15700. (CPUX86_HINT_FAST_BT_REG_IMM in cpu_optimization_hints[current_settings.optimizecputype])
  15701. ) or
  15702. (
  15703. (taicpu(p).oper[1]^.typ <> top_reg) and
  15704. (CPUX86_HINT_FAST_BT_MEM_IMM in cpu_optimization_hints[current_settings.optimizecputype])
  15705. )
  15706. ) and
  15707. (PopCnt(QWord(taicpu(p).oper[0]^.val)) = 1) and
  15708. { For sizes less than S_L, the byte size is equal or larger with BT,
  15709. so don't bother optimising }
  15710. (taicpu(p).opsize >= S_L) then
  15711. begin
  15712. IsValid := True;
  15713. { Check the next set of instructions, watching the FLAGS register
  15714. and the conditions used }
  15715. TransferUsedRegs(TmpUsedRegs);
  15716. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  15717. hp1 := p;
  15718. hp2 := nil;
  15719. while GetNextInstruction(hp1, hp1) do
  15720. begin
  15721. if not Assigned(hp2) then
  15722. { The first instruction after TEST }
  15723. hp2 := hp1;
  15724. if (hp1.typ <> ait_instruction) then
  15725. begin
  15726. { If the flags are no longer in use, everything is fine }
  15727. if RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) then
  15728. IsValid := False;
  15729. Break;
  15730. end;
  15731. case taicpu(hp1).condition of
  15732. C_None:
  15733. begin
  15734. if RegInUsedRegs(NR_DEFAULTFLAGS, TmpUsedRegs) and
  15735. not RegLoadedWithNewValue(NR_DEFAULTFLAGS, hp1) then
  15736. { Something is not quite normal, so play safe and don't change }
  15737. IsValid := False;
  15738. Break;
  15739. end;
  15740. C_E, C_Z, C_NE, C_NZ:
  15741. { This is fine };
  15742. else
  15743. begin
  15744. { Unsupported condition }
  15745. IsValid := False;
  15746. Break;
  15747. end;
  15748. end;
  15749. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  15750. end;
  15751. if IsValid then
  15752. begin
  15753. while hp2 <> hp1 do
  15754. begin
  15755. case taicpu(hp2).condition of
  15756. C_Z, C_E:
  15757. taicpu(hp2).condition := C_NC;
  15758. C_NZ, C_NE:
  15759. taicpu(hp2).condition := C_C;
  15760. else
  15761. { Should not get this by this point }
  15762. InternalError(2022110701);
  15763. end;
  15764. GetNextInstruction(hp2, hp2);
  15765. end;
  15766. DebugMsg(SPeepholeOptimization + 'Changed TEST $' + debug_hexstr(taicpu(p).oper[0]^.val) + ' to BT $' + debug_tostr(BsrQWord(taicpu(p).oper[0]^.val)) + ' to shrink instruction size (Test2Bt)', p);
  15767. taicpu(p).opcode := A_BT;
  15768. taicpu(p).oper[0]^.val := BsrQWord(taicpu(p).oper[0]^.val); { Essentially the base 2 logarithm }
  15769. Result := True;
  15770. Exit;
  15771. end;
  15772. end;
  15773. { removes the line marked with (x) from the sequence
  15774. and/or/xor/add/sub/... $x, %y
  15775. test/or %y, %y | test $-1, %y (x)
  15776. j(n)z _Label
  15777. as the first instruction already adjusts the ZF
  15778. %y operand may also be a reference }
  15779. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  15780. MatchOperand(taicpu(p).oper[0]^,-1);
  15781. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  15782. GetLastInstruction(p, hp1) and
  15783. (tai(hp1).typ = ait_instruction) and
  15784. GetNextInstruction(p,hp2) and
  15785. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  15786. case taicpu(hp1).opcode Of
  15787. A_ADD, A_SUB, A_OR, A_XOR, A_AND,
  15788. { These two instructions set the zero flag if the result is zero }
  15789. A_POPCNT, A_LZCNT:
  15790. begin
  15791. if (
  15792. { With POPCNT, an input of zero will set the zero flag
  15793. because the population count of zero is zero }
  15794. (taicpu(hp1).opcode = A_POPCNT) and
  15795. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) and
  15796. (
  15797. OpsEqual(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^) or
  15798. { Faster than going through the second half of the 'or'
  15799. condition below }
  15800. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^)
  15801. )
  15802. ) or (
  15803. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) and
  15804. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  15805. { and in case of carry for A(E)/B(E)/C/NC }
  15806. (
  15807. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  15808. (
  15809. (taicpu(hp1).opcode <> A_ADD) and
  15810. (taicpu(hp1).opcode <> A_SUB) and
  15811. (taicpu(hp1).opcode <> A_LZCNT)
  15812. )
  15813. )
  15814. ) then
  15815. begin
  15816. DebugMsg(SPeepholeOptimization + 'OpTest/Or2Op (2-op) done', hp1);
  15817. RemoveCurrentP(p, hp2);
  15818. Result:=true;
  15819. Exit;
  15820. end;
  15821. end;
  15822. A_SHL, A_SAL, A_SHR, A_SAR:
  15823. begin
  15824. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  15825. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  15826. { therefore, it's only safe to do this optimization for }
  15827. { shifts by a (nonzero) constant }
  15828. (taicpu(hp1).oper[0]^.typ = top_const) and
  15829. (taicpu(hp1).oper[0]^.val <> 0) and
  15830. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  15831. { and in case of carry for A(E)/B(E)/C/NC }
  15832. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  15833. begin
  15834. DebugMsg(SPeepholeOptimization + 'OpTest/Or2Op (shift) done', hp1);
  15835. RemoveCurrentP(p, hp2);
  15836. Result:=true;
  15837. Exit;
  15838. end;
  15839. end;
  15840. A_DEC, A_INC, A_NEG:
  15841. begin
  15842. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  15843. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  15844. { and in case of carry for A(E)/B(E)/C/NC }
  15845. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  15846. begin
  15847. DebugMsg(SPeepholeOptimization + 'OpTest/Or2Op (1-op) done', hp1);
  15848. RemoveCurrentP(p, hp2);
  15849. Result:=true;
  15850. Exit;
  15851. end;
  15852. end;
  15853. A_ANDN, A_BZHI:
  15854. begin
  15855. if OpsEqual(taicpu(hp1).oper[2]^,taicpu(p).oper[1]^) and
  15856. { Only the zero and sign flags are consistent with what the result is }
  15857. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE,C_S,C_NS]) then
  15858. begin
  15859. DebugMsg(SPeepholeOptimization + 'OpTest/Or2Op (ANDN/BZHI) done', hp1);
  15860. RemoveCurrentP(p, hp2);
  15861. Result:=true;
  15862. Exit;
  15863. end;
  15864. end;
  15865. A_BEXTR:
  15866. begin
  15867. if OpsEqual(taicpu(hp1).oper[2]^,taicpu(p).oper[1]^) and
  15868. { Only the zero flag is set }
  15869. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  15870. begin
  15871. DebugMsg(SPeepholeOptimization + 'OpTest/Or2Op (BEXTR) done', hp1);
  15872. RemoveCurrentP(p, hp2);
  15873. Result:=true;
  15874. Exit;
  15875. end;
  15876. end;
  15877. else
  15878. ;
  15879. end; { case }
  15880. { change "test $-1,%reg" into "test %reg,%reg" }
  15881. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  15882. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  15883. { Change "or %reg,%reg" to "test %reg,%reg" as OR generates a false dependency }
  15884. if MatchInstruction(p, A_OR, []) and
  15885. { Can only match if they're both registers }
  15886. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) then
  15887. begin
  15888. DebugMsg(SPeepholeOptimization + 'or %reg,%reg -> test %reg,%reg to remove false dependency (Or2Test)', p);
  15889. taicpu(p).opcode := A_TEST;
  15890. { No need to set Result to True, as we've done all the optimisations we can }
  15891. end;
  15892. end;
  15893. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  15894. var
  15895. hp1,hp3 : tai;
  15896. {$ifndef x86_64}
  15897. hp2 : taicpu;
  15898. {$endif x86_64}
  15899. begin
  15900. Result:=false;
  15901. hp3:=nil;
  15902. {$ifndef x86_64}
  15903. { don't do this on modern CPUs, this really hurts them due to
  15904. broken call/ret pairing }
  15905. if (current_settings.optimizecputype < cpu_Pentium2) and
  15906. not(cs_create_pic in current_settings.moduleswitches) and
  15907. GetNextInstruction(p, hp1) and
  15908. MatchInstruction(hp1,A_JMP,[S_NO]) and
  15909. MatchOpType(taicpu(hp1),top_ref) and
  15910. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  15911. begin
  15912. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  15913. taicpu(hp2).fileinfo := taicpu(p).fileinfo;
  15914. InsertLLItem(p.previous, p, hp2);
  15915. taicpu(p).opcode := A_JMP;
  15916. taicpu(p).is_jmp := true;
  15917. RemoveInstruction(hp1);
  15918. Result:=true;
  15919. end
  15920. else
  15921. {$endif x86_64}
  15922. { replace
  15923. call procname
  15924. ret
  15925. by
  15926. jmp procname
  15927. but do it only on level 4 because it destroys stack back traces
  15928. else if the subroutine is marked as no return, remove the ret
  15929. }
  15930. if ((cs_opt_level4 in current_settings.optimizerswitches) or
  15931. (po_noreturn in current_procinfo.procdef.procoptions)) and
  15932. GetNextInstruction(p, hp1) and
  15933. (MatchInstruction(hp1,A_RET,[S_NO]) or
  15934. (MatchInstruction(hp1,A_VZEROUPPER,[S_NO]) and
  15935. SetAndTest(hp1,hp3) and
  15936. GetNextInstruction(hp1,hp1) and
  15937. MatchInstruction(hp1,A_RET,[S_NO])
  15938. )
  15939. ) and
  15940. (taicpu(hp1).ops=0) then
  15941. begin
  15942. if (cs_opt_level4 in current_settings.optimizerswitches) and
  15943. { we might destroy stack alignment here if we do not do a call }
  15944. (target_info.stackalign<=sizeof(SizeUInt)) then
  15945. begin
  15946. taicpu(p).opcode := A_JMP;
  15947. taicpu(p).is_jmp := true;
  15948. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  15949. end
  15950. else
  15951. DebugMsg(SPeepholeOptimization + 'CallRet2Call done',p);
  15952. RemoveInstruction(hp1);
  15953. if Assigned(hp3) then
  15954. begin
  15955. AsmL.Remove(hp3);
  15956. AsmL.InsertBefore(hp3,p)
  15957. end;
  15958. Result:=true;
  15959. end;
  15960. end;
  15961. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  15962. function ConstInRange(const Val: TCGInt; const OpSize: TOpSize): Boolean;
  15963. begin
  15964. case OpSize of
  15965. S_B, S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  15966. Result := (Val <= $FF) and (Val >= -128);
  15967. S_W, S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  15968. Result := (Val <= $FFFF) and (Val >= -32768);
  15969. S_L{$ifdef x86_64}, S_LQ{$endif x86_64}:
  15970. Result := (Val <= $FFFFFFFF) and (Val >= -2147483648);
  15971. else
  15972. Result := True;
  15973. end;
  15974. end;
  15975. var
  15976. hp1, hp2 : tai;
  15977. SizeChange: Boolean;
  15978. PreMessage: string;
  15979. begin
  15980. Result := False;
  15981. if (taicpu(p).oper[0]^.typ = top_reg) and
  15982. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  15983. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) then
  15984. begin
  15985. { Change (using movzbl %al,%eax as an example):
  15986. movzbl %al, %eax movzbl %al, %eax
  15987. cmpl x, %eax testl %eax,%eax
  15988. To:
  15989. cmpb x, %al testb %al, %al (Move one back to avoid a false dependency)
  15990. movzbl %al, %eax movzbl %al, %eax
  15991. Smaller instruction and minimises pipeline stall as the CPU
  15992. doesn't have to wait for the register to get zero-extended. [Kit]
  15993. Also allow if the smaller of the two registers is being checked,
  15994. as this still removes the false dependency.
  15995. }
  15996. if
  15997. (
  15998. (
  15999. (taicpu(hp1).opcode = A_CMP) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  16000. ConstInRange(taicpu(hp1).oper[0]^.val, taicpu(p).opsize)
  16001. ) or (
  16002. { If MatchOperand returns True, they must both be registers }
  16003. (taicpu(hp1).opcode = A_TEST) and MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)
  16004. )
  16005. ) and
  16006. (reg2opsize(taicpu(hp1).oper[1]^.reg) <= reg2opsize(taicpu(p).oper[1]^.reg)) and
  16007. SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) then
  16008. begin
  16009. 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);
  16010. asml.Remove(hp1);
  16011. asml.InsertBefore(hp1, p);
  16012. { Swap instructions in the case of cmp 0,%reg or test %reg,%reg }
  16013. if (taicpu(hp1).opcode = A_TEST) or (taicpu(hp1).oper[0]^.val = 0) then
  16014. begin
  16015. taicpu(hp1).opcode := A_TEST;
  16016. taicpu(hp1).loadreg(0, taicpu(p).oper[0]^.reg);
  16017. end;
  16018. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  16019. case taicpu(p).opsize of
  16020. S_BW, S_BL:
  16021. begin
  16022. SizeChange := taicpu(hp1).opsize <> S_B;
  16023. taicpu(hp1).changeopsize(S_B);
  16024. end;
  16025. S_WL:
  16026. begin
  16027. SizeChange := taicpu(hp1).opsize <> S_W;
  16028. taicpu(hp1).changeopsize(S_W);
  16029. end
  16030. else
  16031. InternalError(2020112701);
  16032. end;
  16033. UpdateUsedRegs(tai(p.Next));
  16034. { Check if the register is used aferwards - if not, we can
  16035. remove the movzx instruction completely }
  16036. if not RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, p, UsedRegs) then
  16037. begin
  16038. { Hp1 is a better position than p for debugging purposes }
  16039. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4a', hp1);
  16040. RemoveCurrentp(p, hp1);
  16041. Result := True;
  16042. end;
  16043. if SizeChange then
  16044. DebugMsg(SPeepholeOptimization + PreMessage +
  16045. 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)
  16046. else
  16047. DebugMsg(SPeepholeOptimization + 'MovzxCmp2CmpMovzx', hp1);
  16048. Exit;
  16049. end;
  16050. { Change (using movzwl %ax,%eax as an example):
  16051. movzwl %ax, %eax
  16052. movb %al, (dest) (Register is smaller than read register in movz)
  16053. To:
  16054. movb %al, (dest) (Move one back to avoid a false dependency)
  16055. movzwl %ax, %eax
  16056. }
  16057. if (taicpu(hp1).opcode = A_MOV) and
  16058. (taicpu(hp1).oper[0]^.typ = top_reg) and
  16059. not RegInOp(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^) and
  16060. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(p).oper[0]^.reg) and
  16061. (reg2opsize(taicpu(hp1).oper[0]^.reg) <= reg2opsize(taicpu(p).oper[0]^.reg)) then
  16062. begin
  16063. DebugMsg(SPeepholeOptimization + 'MovzxMov2MovMovzx', hp1);
  16064. hp2 := tai(hp1.Previous); { Effectively the old position of hp1 }
  16065. asml.Remove(hp1);
  16066. asml.InsertBefore(hp1, p);
  16067. if taicpu(hp1).oper[1]^.typ = top_reg then
  16068. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, hp2, UsedRegs);
  16069. { Check if the register is used aferwards - if not, we can
  16070. remove the movzx instruction completely }
  16071. if not RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, p, UsedRegs) then
  16072. begin
  16073. { Hp1 is a better position than p for debugging purposes }
  16074. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4b', hp1);
  16075. RemoveCurrentp(p, hp1);
  16076. Result := True;
  16077. end;
  16078. Exit;
  16079. end;
  16080. end;
  16081. end;
  16082. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  16083. var
  16084. hp1: tai;
  16085. {$ifdef x86_64}
  16086. PreMessage, RegName: string;
  16087. {$endif x86_64}
  16088. begin
  16089. Result := False;
  16090. { If x is a power of 2 (popcnt = 1), change:
  16091. xor $x, %reg/ref
  16092. To:
  16093. btc lb(x), %reg/ref
  16094. }
  16095. if IsBTXAcceptable(p) and
  16096. { IsBTXAcceptable checks to see if oper[0] is an immediate }
  16097. (PopCnt(QWord(taicpu(p).oper[0]^.val)) = 1) and
  16098. (
  16099. { Don't optimise if a test instruction follows }
  16100. not GetNextInstruction(p, hp1) or
  16101. not MatchInstruction(hp1, A_TEST, [taicpu(p).opsize])
  16102. ) then
  16103. begin
  16104. DebugMsg(SPeepholeOptimization + 'Changed XOR $' + debug_hexstr(taicpu(p).oper[0]^.val) + ' to BTC $' + debug_tostr(BsrQWord(taicpu(p).oper[0]^.val)) + ' to shrink instruction size (Xor2Btc)', p);
  16105. taicpu(p).opcode := A_BTC;
  16106. taicpu(p).oper[0]^.val := BsrQWord(taicpu(p).oper[0]^.val); { Essentially the base 2 logarithm }
  16107. Result := True;
  16108. Exit;
  16109. end;
  16110. {$ifdef x86_64}
  16111. { Code size reduction by J. Gareth "Kit" Moreton }
  16112. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  16113. as this removes the REX prefix }
  16114. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  16115. Exit;
  16116. if taicpu(p).oper[0]^.typ <> top_reg then
  16117. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  16118. InternalError(2018011500);
  16119. case taicpu(p).opsize of
  16120. S_Q:
  16121. begin
  16122. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  16123. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  16124. { The actual optimization }
  16125. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  16126. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  16127. taicpu(p).changeopsize(S_L);
  16128. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  16129. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (32-bit register recommended when zeroing 64-bit counterpart)', p);
  16130. end;
  16131. else
  16132. ;
  16133. end;
  16134. {$endif x86_64}
  16135. end;
  16136. function TX86AsmOptimizer.PostPeepholeOptVPXOR(var p : tai) : Boolean;
  16137. var
  16138. XReg: TRegister;
  16139. begin
  16140. Result := False;
  16141. { Turn "vpxor %ymmreg2,%ymmreg2,%ymmreg1" to "vpxor %xmmreg2,%xmmreg2,%xmmreg1"
  16142. Smaller encoding and slightly faster on some platforms (also works for
  16143. ZMM-sized registers) }
  16144. if (taicpu(p).opsize in [S_YMM, S_ZMM]) and
  16145. MatchOpType(taicpu(p), top_reg, top_reg, top_reg) then
  16146. begin
  16147. XReg := taicpu(p).oper[0]^.reg;
  16148. if (taicpu(p).oper[1]^.reg = XReg) then
  16149. begin
  16150. taicpu(p).changeopsize(S_XMM);
  16151. setsubreg(taicpu(p).oper[2]^.reg, R_SUBMMX);
  16152. if (cs_opt_size in current_settings.optimizerswitches) then
  16153. begin
  16154. { Change input registers to %xmm0 to reduce size. Note that
  16155. there's a risk of a false dependency doing this, so only
  16156. optimise for size here }
  16157. XReg := NR_XMM0;
  16158. DebugMsg(SPeepholeOptimization + 'Changed zero-setting vpxor from Y/ZMM to XMM and changed input registers to %xmm0 to reduce size', p);
  16159. end
  16160. else
  16161. begin
  16162. setsubreg(XReg, R_SUBMMX);
  16163. DebugMsg(SPeepholeOptimization + 'Changed zero-setting vpxor from Y/ZMM to XMM to reduce size and increase efficiency', p);
  16164. end;
  16165. taicpu(p).oper[0]^.reg := XReg;
  16166. taicpu(p).oper[1]^.reg := XReg;
  16167. Result := True;
  16168. end;
  16169. end;
  16170. end;
  16171. function TX86AsmOptimizer.PostPeepholeOptRET(var p: tai): Boolean;
  16172. var
  16173. hp1, p_new: tai;
  16174. begin
  16175. Result := False;
  16176. { Check for:
  16177. ret
  16178. .Lbl:
  16179. ret
  16180. Remove first 'ret'
  16181. }
  16182. if GetNextInstruction(p, hp1) and
  16183. { Remember where the label is }
  16184. SetAndTest(hp1, p_new) and
  16185. (hp1.typ in [ait_align, ait_label]) and
  16186. SkipLabels(hp1, hp1) and
  16187. MatchInstruction(hp1, A_RET, []) and
  16188. { To be safe, make sure the RET instructions are identical }
  16189. (taicpu(p).ops = taicpu(hp1).ops) and
  16190. (
  16191. (taicpu(p).ops = 0) or
  16192. (
  16193. (taicpu(p).ops = 1) and
  16194. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^)
  16195. )
  16196. ) then
  16197. begin
  16198. DebugMsg(SPeepholeOptimization + 'Removed superfluous RET', p);
  16199. UpdateUsedRegs(tai(p.Next));
  16200. RemoveCurrentP(p, p_new);
  16201. Result := True;
  16202. Exit;
  16203. end;
  16204. end;
  16205. class procedure TX86AsmOptimizer.OptimizeRefs(var p: taicpu);
  16206. var
  16207. OperIdx: Integer;
  16208. begin
  16209. for OperIdx := 0 to p.ops - 1 do
  16210. if p.oper[OperIdx]^.typ = top_ref then
  16211. optimize_ref(p.oper[OperIdx]^.ref^, False);
  16212. end;
  16213. end.