aoptx86.pas 260 KB

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