aoptx86.pas 325 KB

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