aoptx86.pas 248 KB

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