aoptx86.pas 257 KB

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