aoptx86.pas 267 KB

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