aoptx86.pas 296 KB

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