aoptx86.pas 275 KB

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