aoptx86.pas 261 KB

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