aoptx86.pas 328 KB

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