aoptx86.pas 334 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837
  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. { Check common LEA/LEA conditions }
  3099. if MatchInstruction(hp1,A_LEA,[taicpu(p).opsize]) and
  3100. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
  3101. (taicpu(p).oper[0]^.ref^.relsymbol = nil) and
  3102. (taicpu(p).oper[0]^.ref^.segment = NR_NO) and
  3103. (taicpu(p).oper[0]^.ref^.symbol = nil) and
  3104. (taicpu(hp1).oper[0]^.ref^.relsymbol = nil) and
  3105. (taicpu(hp1).oper[0]^.ref^.segment = NR_NO) and
  3106. (taicpu(hp1).oper[0]^.ref^.symbol = nil) and
  3107. (
  3108. (taicpu(p).oper[0]^.ref^.base = NR_NO) or { Don't call RegModifiedBetween unnecessarily }
  3109. not(RegModifiedBetween(taicpu(p).oper[0]^.ref^.base,p,hp1))
  3110. ) and (
  3111. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) or { Don't call RegModifiedBetween unnecessarily }
  3112. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  3113. not(RegModifiedBetween(taicpu(p).oper[0]^.ref^.index,p,hp1))
  3114. ) then
  3115. begin
  3116. { changes
  3117. lea (regX,scale), reg1
  3118. lea offset(reg1,reg1), reg1
  3119. to
  3120. lea offset(regX,scale*2), reg1
  3121. and
  3122. lea (regX,scale1), reg1
  3123. lea offset(reg1,scale2), reg1
  3124. to
  3125. lea offset(regX,scale1*scale2), reg1
  3126. ... so long as the final scale does not exceed 8
  3127. (Similarly, allow the first instruction to be "lea (regX,regX),reg1")
  3128. }
  3129. if (taicpu(p).oper[0]^.ref^.offset = 0) and
  3130. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  3131. (
  3132. (
  3133. (taicpu(p).oper[0]^.ref^.base = NR_NO)
  3134. ) or (
  3135. (taicpu(p).oper[0]^.ref^.scalefactor <= 1) and
  3136. (
  3137. (taicpu(p).oper[0]^.ref^.base = taicpu(p).oper[0]^.ref^.index) and
  3138. not(RegUsedBetween(taicpu(p).oper[0]^.ref^.index, p, hp1))
  3139. )
  3140. )
  3141. ) and (
  3142. (
  3143. { lea (reg1,scale2), reg1 variant }
  3144. (taicpu(hp1).oper[0]^.ref^.base = NR_NO) and
  3145. (
  3146. (
  3147. (taicpu(p).oper[0]^.ref^.base = NR_NO) and
  3148. (taicpu(hp1).oper[0]^.ref^.scalefactor * taicpu(p).oper[0]^.ref^.scalefactor <= 8)
  3149. ) or (
  3150. { lea (regX,regX), reg1 variant }
  3151. (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  3152. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 4)
  3153. )
  3154. )
  3155. ) or (
  3156. { lea (reg1,reg1), reg1 variant }
  3157. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  3158. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1)
  3159. )
  3160. ) then
  3161. begin
  3162. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea 2 done',p);
  3163. { Make everything homogeneous to make calculations easier }
  3164. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) then
  3165. begin
  3166. if taicpu(p).oper[0]^.ref^.index <> NR_NO then
  3167. { Convert lea (regX,regX),reg1 to lea (regX,2),reg1 }
  3168. taicpu(p).oper[0]^.ref^.scalefactor := 2
  3169. else
  3170. taicpu(p).oper[0]^.ref^.index := taicpu(p).oper[0]^.ref^.base;
  3171. taicpu(p).oper[0]^.ref^.base := NR_NO;
  3172. end;
  3173. if (taicpu(hp1).oper[0]^.ref^.base = NR_NO) then
  3174. begin
  3175. { Just to prevent miscalculations }
  3176. if (taicpu(hp1).oper[0]^.ref^.scalefactor = 0) then
  3177. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(p).oper[0]^.ref^.scalefactor
  3178. else
  3179. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(hp1).oper[0]^.ref^.scalefactor * taicpu(p).oper[0]^.ref^.scalefactor;
  3180. end
  3181. else
  3182. begin
  3183. taicpu(hp1).oper[0]^.ref^.base := NR_NO;
  3184. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(p).oper[0]^.ref^.scalefactor * 2;
  3185. end;
  3186. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.ref^.index;
  3187. RemoveCurrentP(p);
  3188. result:=true;
  3189. exit;
  3190. end
  3191. { changes
  3192. lea offset1(regX), reg1
  3193. lea offset2(reg1), reg1
  3194. to
  3195. lea offset1+offset2(regX), reg1 }
  3196. else if
  3197. (
  3198. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  3199. (taicpu(p).oper[0]^.ref^.index = NR_NO)
  3200. ) or (
  3201. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  3202. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1) and
  3203. (
  3204. (
  3205. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  3206. (taicpu(p).oper[0]^.ref^.base = NR_NO)
  3207. ) or (
  3208. (taicpu(p).oper[0]^.ref^.scalefactor <= 1) and
  3209. (
  3210. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  3211. (
  3212. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) and
  3213. (
  3214. (taicpu(hp1).oper[0]^.ref^.index = NR_NO) or
  3215. (taicpu(hp1).oper[0]^.ref^.base = NR_NO)
  3216. )
  3217. )
  3218. )
  3219. )
  3220. )
  3221. ) then
  3222. begin
  3223. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea 1 done',p);
  3224. if taicpu(hp1).oper[0]^.ref^.index=taicpu(p).oper[1]^.reg then
  3225. begin
  3226. taicpu(hp1).oper[0]^.ref^.index:=taicpu(p).oper[0]^.ref^.base;
  3227. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset*max(taicpu(hp1).oper[0]^.ref^.scalefactor,1));
  3228. { if the register is used as index and base, we have to increase for base as well
  3229. and adapt base }
  3230. if taicpu(hp1).oper[0]^.ref^.base=taicpu(p).oper[1]^.reg then
  3231. begin
  3232. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  3233. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  3234. end;
  3235. end
  3236. else
  3237. begin
  3238. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  3239. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  3240. end;
  3241. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  3242. begin
  3243. taicpu(hp1).oper[0]^.ref^.base:=taicpu(hp1).oper[0]^.ref^.index;
  3244. taicpu(hp1).oper[0]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  3245. taicpu(hp1).oper[0]^.ref^.scalefactor:=taicpu(p).oper[0]^.ref^.scalefactor;
  3246. end;
  3247. RemoveCurrentP(p);
  3248. result:=true;
  3249. exit;
  3250. end;
  3251. end;
  3252. { Change:
  3253. leal/q $x(%reg1),%reg2
  3254. ...
  3255. shll/q $y,%reg2
  3256. To:
  3257. leal/q $(x+2^y)(%reg1,2^y),%reg2 (if y <= 3)
  3258. }
  3259. if MatchInstruction(hp1, A_SHL, [taicpu(p).opsize]) and
  3260. MatchOpType(taicpu(hp1), top_const, top_reg) and
  3261. (taicpu(hp1).oper[0]^.val <= 3) then
  3262. begin
  3263. Multiple := 1 shl taicpu(hp1).oper[0]^.val;
  3264. TransferUsedRegs(TmpUsedRegs);
  3265. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3266. TempReg := taicpu(hp1).oper[1]^.reg; { Store locally to reduce the number of dereferences }
  3267. if
  3268. { This allows the optimisation in some circumstances even if the lea instruction already has a scale factor
  3269. (this works even if scalefactor is zero) }
  3270. ((Multiple * taicpu(p).oper[0]^.ref^.scalefactor) <= 8) and
  3271. { Ensure offset doesn't go out of bounds }
  3272. (abs(taicpu(p).oper[0]^.ref^.offset * Multiple) <= $7FFFFFFF) and
  3273. not (RegInUsedRegs(NR_DEFAULTFLAGS,TmpUsedRegs)) and
  3274. MatchOperand(taicpu(p).oper[1]^, TempReg) and
  3275. (
  3276. (
  3277. not SuperRegistersEqual(taicpu(p).oper[0]^.ref^.base, TempReg) and
  3278. (
  3279. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  3280. (taicpu(p).oper[0]^.ref^.index = NR_INVALID) or
  3281. (
  3282. { Check for lea $x(%reg1,%reg1),%reg2 and treat as it it were lea $x(%reg1,2),%reg2 }
  3283. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) and
  3284. (taicpu(p).oper[0]^.ref^.scalefactor <= 1)
  3285. )
  3286. )
  3287. ) or (
  3288. (
  3289. (taicpu(p).oper[0]^.ref^.base = NR_NO) or
  3290. (taicpu(p).oper[0]^.ref^.base = NR_INVALID)
  3291. ) and
  3292. not SuperRegistersEqual(taicpu(p).oper[0]^.ref^.index, TempReg)
  3293. )
  3294. ) then
  3295. begin
  3296. repeat
  3297. with taicpu(p).oper[0]^.ref^ do
  3298. begin
  3299. { Convert lea $x(%reg1,%reg1),%reg2 to lea $x(%reg1,2),%reg2 }
  3300. if index = base then
  3301. begin
  3302. if Multiple > 4 then
  3303. { Optimisation will no longer work because resultant
  3304. scale factor will exceed 8 }
  3305. Break;
  3306. base := NR_NO;
  3307. scalefactor := 2;
  3308. DebugMsg(SPeepholeOptimization + 'lea $x(%reg1,%reg1),%reg2 -> lea $x(%reg1,2),%reg2 for following optimisation', p);
  3309. end
  3310. else if (base <> NR_NO) and (base <> NR_INVALID) then
  3311. begin
  3312. { Scale factor only works on the index register }
  3313. index := base;
  3314. base := NR_NO;
  3315. end;
  3316. { For safety }
  3317. if scalefactor <= 1 then
  3318. begin
  3319. DebugMsg(SPeepholeOptimization + 'LeaShl2Lea 1', p);
  3320. scalefactor := Multiple;
  3321. end
  3322. else
  3323. begin
  3324. DebugMsg(SPeepholeOptimization + 'LeaShl2Lea 2', p);
  3325. scalefactor := scalefactor * Multiple;
  3326. end;
  3327. offset := offset * Multiple;
  3328. end;
  3329. RemoveInstruction(hp1);
  3330. Result := True;
  3331. Exit;
  3332. { This repeat..until loop exists for the benefit of Break }
  3333. until True;
  3334. end;
  3335. end;
  3336. end;
  3337. end;
  3338. function TX86AsmOptimizer.DoSubAddOpt(var p: tai): Boolean;
  3339. var
  3340. hp1 : tai;
  3341. begin
  3342. DoSubAddOpt := False;
  3343. if GetLastInstruction(p, hp1) and
  3344. (hp1.typ = ait_instruction) and
  3345. (taicpu(hp1).opsize = taicpu(p).opsize) then
  3346. case taicpu(hp1).opcode Of
  3347. A_DEC:
  3348. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  3349. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  3350. begin
  3351. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
  3352. RemoveInstruction(hp1);
  3353. end;
  3354. A_SUB:
  3355. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  3356. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  3357. begin
  3358. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
  3359. RemoveInstruction(hp1);
  3360. end;
  3361. A_ADD:
  3362. begin
  3363. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  3364. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  3365. begin
  3366. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  3367. RemoveInstruction(hp1);
  3368. if (taicpu(p).oper[0]^.val = 0) then
  3369. begin
  3370. hp1 := tai(p.next);
  3371. RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
  3372. if not GetLastInstruction(hp1, p) then
  3373. p := hp1;
  3374. DoSubAddOpt := True;
  3375. end
  3376. end;
  3377. end;
  3378. else
  3379. ;
  3380. end;
  3381. end;
  3382. function TX86AsmOptimizer.OptPass1Sub(var p : tai) : boolean;
  3383. {$ifdef i386}
  3384. var
  3385. hp1 : tai;
  3386. {$endif i386}
  3387. begin
  3388. Result:=false;
  3389. { * change "subl $2, %esp; pushw x" to "pushl x"}
  3390. { * change "sub/add const1, reg" or "dec reg" followed by
  3391. "sub const2, reg" to one "sub ..., reg" }
  3392. if MatchOpType(taicpu(p),top_const,top_reg) then
  3393. begin
  3394. {$ifdef i386}
  3395. if (taicpu(p).oper[0]^.val = 2) and
  3396. (taicpu(p).oper[1]^.reg = NR_ESP) and
  3397. { Don't do the sub/push optimization if the sub }
  3398. { comes from setting up the stack frame (JM) }
  3399. (not(GetLastInstruction(p,hp1)) or
  3400. not(MatchInstruction(hp1,A_MOV,[S_L]) and
  3401. MatchOperand(taicpu(hp1).oper[0]^,NR_ESP) and
  3402. MatchOperand(taicpu(hp1).oper[0]^,NR_EBP))) then
  3403. begin
  3404. hp1 := tai(p.next);
  3405. while Assigned(hp1) and
  3406. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  3407. not RegReadByInstruction(NR_ESP,hp1) and
  3408. not RegModifiedByInstruction(NR_ESP,hp1) do
  3409. hp1 := tai(hp1.next);
  3410. if Assigned(hp1) and
  3411. MatchInstruction(hp1,A_PUSH,[S_W]) then
  3412. begin
  3413. taicpu(hp1).changeopsize(S_L);
  3414. if taicpu(hp1).oper[0]^.typ=top_reg then
  3415. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  3416. hp1 := tai(p.next);
  3417. RemoveCurrentp(p, hp1);
  3418. Result:=true;
  3419. exit;
  3420. end;
  3421. end;
  3422. {$endif i386}
  3423. if DoSubAddOpt(p) then
  3424. Result:=true;
  3425. end;
  3426. end;
  3427. function TX86AsmOptimizer.OptPass1SHLSAL(var p : tai) : boolean;
  3428. var
  3429. TmpBool1,TmpBool2 : Boolean;
  3430. tmpref : treference;
  3431. hp1,hp2: tai;
  3432. mask: tcgint;
  3433. begin
  3434. Result:=false;
  3435. { All these optimisations work on "shl/sal const,%reg" }
  3436. if not MatchOpType(taicpu(p),top_const,top_reg) then
  3437. Exit;
  3438. if (taicpu(p).opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  3439. (taicpu(p).oper[0]^.val <= 3) then
  3440. { Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement }
  3441. begin
  3442. { should we check the next instruction? }
  3443. TmpBool1 := True;
  3444. { have we found an add/sub which could be
  3445. integrated in the lea? }
  3446. TmpBool2 := False;
  3447. reference_reset(tmpref,2,[]);
  3448. TmpRef.index := taicpu(p).oper[1]^.reg;
  3449. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  3450. while TmpBool1 and
  3451. GetNextInstruction(p, hp1) and
  3452. (tai(hp1).typ = ait_instruction) and
  3453. ((((taicpu(hp1).opcode = A_ADD) or
  3454. (taicpu(hp1).opcode = A_SUB)) and
  3455. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  3456. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  3457. (((taicpu(hp1).opcode = A_INC) or
  3458. (taicpu(hp1).opcode = A_DEC)) and
  3459. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  3460. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg)) or
  3461. ((taicpu(hp1).opcode = A_LEA) and
  3462. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  3463. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg))) and
  3464. (not GetNextInstruction(hp1,hp2) or
  3465. not instrReadsFlags(hp2)) Do
  3466. begin
  3467. TmpBool1 := False;
  3468. if taicpu(hp1).opcode=A_LEA then
  3469. begin
  3470. if (TmpRef.base = NR_NO) and
  3471. (taicpu(hp1).oper[0]^.ref^.symbol=nil) and
  3472. (taicpu(hp1).oper[0]^.ref^.relsymbol=nil) and
  3473. (taicpu(hp1).oper[0]^.ref^.segment=NR_NO) and
  3474. ((taicpu(hp1).oper[0]^.ref^.scalefactor=0) or
  3475. (taicpu(hp1).oper[0]^.ref^.scalefactor*tmpref.scalefactor<=8)) then
  3476. begin
  3477. TmpBool1 := True;
  3478. TmpBool2 := True;
  3479. inc(TmpRef.offset, taicpu(hp1).oper[0]^.ref^.offset);
  3480. if taicpu(hp1).oper[0]^.ref^.scalefactor<>0 then
  3481. tmpref.scalefactor:=tmpref.scalefactor*taicpu(hp1).oper[0]^.ref^.scalefactor;
  3482. TmpRef.base := taicpu(hp1).oper[0]^.ref^.base;
  3483. RemoveInstruction(hp1);
  3484. end
  3485. end
  3486. else if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  3487. begin
  3488. TmpBool1 := True;
  3489. TmpBool2 := True;
  3490. case taicpu(hp1).opcode of
  3491. A_ADD:
  3492. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  3493. A_SUB:
  3494. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  3495. else
  3496. internalerror(2019050536);
  3497. end;
  3498. RemoveInstruction(hp1);
  3499. end
  3500. else
  3501. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  3502. (((taicpu(hp1).opcode = A_ADD) and
  3503. (TmpRef.base = NR_NO)) or
  3504. (taicpu(hp1).opcode = A_INC) or
  3505. (taicpu(hp1).opcode = A_DEC)) then
  3506. begin
  3507. TmpBool1 := True;
  3508. TmpBool2 := True;
  3509. case taicpu(hp1).opcode of
  3510. A_ADD:
  3511. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  3512. A_INC:
  3513. inc(TmpRef.offset);
  3514. A_DEC:
  3515. dec(TmpRef.offset);
  3516. else
  3517. internalerror(2019050535);
  3518. end;
  3519. RemoveInstruction(hp1);
  3520. end;
  3521. end;
  3522. if TmpBool2
  3523. {$ifndef x86_64}
  3524. or
  3525. ((current_settings.optimizecputype < cpu_Pentium2) and
  3526. (taicpu(p).oper[0]^.val <= 3) and
  3527. not(cs_opt_size in current_settings.optimizerswitches))
  3528. {$endif x86_64}
  3529. then
  3530. begin
  3531. if not(TmpBool2) and
  3532. (taicpu(p).oper[0]^.val=1) then
  3533. begin
  3534. hp1:=taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  3535. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  3536. end
  3537. else
  3538. hp1:=taicpu.op_ref_reg(A_LEA, taicpu(p).opsize, TmpRef,
  3539. taicpu(p).oper[1]^.reg);
  3540. DebugMsg(SPeepholeOptimization + 'ShlAddLeaSubIncDec2Lea',p);
  3541. InsertLLItem(p.previous, p.next, hp1);
  3542. p.free;
  3543. p := hp1;
  3544. end;
  3545. end
  3546. {$ifndef x86_64}
  3547. else if (current_settings.optimizecputype < cpu_Pentium2) then
  3548. begin
  3549. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  3550. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  3551. (unlike shl, which is only Tairable in the U pipe) }
  3552. if taicpu(p).oper[0]^.val=1 then
  3553. begin
  3554. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  3555. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  3556. InsertLLItem(p.previous, p.next, hp1);
  3557. p.free;
  3558. p := hp1;
  3559. end
  3560. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  3561. "shl $3, %reg" to "lea (,%reg,8), %reg }
  3562. else if (taicpu(p).opsize = S_L) and
  3563. (taicpu(p).oper[0]^.val<= 3) then
  3564. begin
  3565. reference_reset(tmpref,2,[]);
  3566. TmpRef.index := taicpu(p).oper[1]^.reg;
  3567. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  3568. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  3569. InsertLLItem(p.previous, p.next, hp1);
  3570. p.free;
  3571. p := hp1;
  3572. end;
  3573. end
  3574. {$endif x86_64}
  3575. else if
  3576. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  3577. (
  3578. (
  3579. MatchInstruction(hp1, A_AND, [taicpu(p).opsize]) and
  3580. SetAndTest(hp1, hp2)
  3581. {$ifdef x86_64}
  3582. ) or
  3583. (
  3584. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  3585. GetNextInstruction(hp1, hp2) and
  3586. MatchInstruction(hp2, A_AND, [taicpu(p).opsize]) and
  3587. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  3588. (taicpu(hp1).oper[1]^.reg = taicpu(hp2).oper[0]^.reg)
  3589. {$endif x86_64}
  3590. )
  3591. ) and
  3592. (taicpu(p).oper[1]^.reg = taicpu(hp2).oper[1]^.reg) then
  3593. begin
  3594. { Change:
  3595. shl x, %reg1
  3596. mov -(1<<x), %reg2
  3597. and %reg2, %reg1
  3598. Or:
  3599. shl x, %reg1
  3600. and -(1<<x), %reg1
  3601. To just:
  3602. shl x, %reg1
  3603. Since the and operation only zeroes bits that are already zero from the shl operation
  3604. }
  3605. case taicpu(p).oper[0]^.val of
  3606. 8:
  3607. mask:=$FFFFFFFFFFFFFF00;
  3608. 16:
  3609. mask:=$FFFFFFFFFFFF0000;
  3610. 32:
  3611. mask:=$FFFFFFFF00000000;
  3612. 63:
  3613. { Constant pre-calculated to prevent overflow errors with Int64 }
  3614. mask:=$8000000000000000;
  3615. else
  3616. begin
  3617. if taicpu(p).oper[0]^.val >= 64 then
  3618. { Shouldn't happen realistically, since the register
  3619. is guaranteed to be set to zero at this point }
  3620. mask := 0
  3621. else
  3622. mask := -(Int64(1 shl taicpu(p).oper[0]^.val));
  3623. end;
  3624. end;
  3625. if taicpu(hp1).oper[0]^.val = mask then
  3626. begin
  3627. { Everything checks out, perform the optimisation, as long as
  3628. the FLAGS register isn't being used}
  3629. TransferUsedRegs(TmpUsedRegs);
  3630. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3631. {$ifdef x86_64}
  3632. if (hp1 <> hp2) then
  3633. begin
  3634. { "shl/mov/and" version }
  3635. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  3636. { Don't do the optimisation if the FLAGS register is in use }
  3637. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)) then
  3638. begin
  3639. DebugMsg(SPeepholeOptimization + 'ShlMovAnd2Shl', p);
  3640. { Don't remove the 'mov' instruction if its register is used elsewhere }
  3641. if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp2, TmpUsedRegs)) then
  3642. begin
  3643. RemoveInstruction(hp1);
  3644. Result := True;
  3645. end;
  3646. { Only set Result to True if the 'mov' instruction was removed }
  3647. RemoveInstruction(hp2);
  3648. end;
  3649. end
  3650. else
  3651. {$endif x86_64}
  3652. begin
  3653. { "shl/and" version }
  3654. { Don't do the optimisation if the FLAGS register is in use }
  3655. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  3656. begin
  3657. DebugMsg(SPeepholeOptimization + 'ShlAnd2Shl', p);
  3658. RemoveInstruction(hp1);
  3659. Result := True;
  3660. end;
  3661. end;
  3662. Exit;
  3663. end
  3664. else {$ifdef x86_64}if (hp1 = hp2) then{$endif x86_64}
  3665. begin
  3666. { Even if the mask doesn't allow for its removal, we might be
  3667. able to optimise the mask for the "shl/and" version, which
  3668. may permit other peephole optimisations }
  3669. {$ifdef DEBUG_AOPTCPU}
  3670. mask := taicpu(hp1).oper[0]^.val and mask;
  3671. if taicpu(hp1).oper[0]^.val <> mask then
  3672. begin
  3673. DebugMsg(
  3674. SPeepholeOptimization +
  3675. 'Changed mask from $' + debug_tostr(taicpu(hp1).oper[0]^.val) +
  3676. ' to $' + debug_tostr(mask) +
  3677. 'based on previous instruction (ShlAnd2ShlAnd)', hp1);
  3678. taicpu(hp1).oper[0]^.val := mask;
  3679. end;
  3680. {$else DEBUG_AOPTCPU}
  3681. { If debugging is off, just set the operand even if it's the same }
  3682. taicpu(hp1).oper[0]^.val := taicpu(hp1).oper[0]^.val and mask;
  3683. {$endif DEBUG_AOPTCPU}
  3684. end;
  3685. end;
  3686. end;
  3687. function TX86AsmOptimizer.OptPass1SETcc(var p: tai): boolean;
  3688. var
  3689. hp1,hp2,next: tai; SetC, JumpC: TAsmCond; Unconditional: Boolean;
  3690. begin
  3691. Result:=false;
  3692. if MatchOpType(taicpu(p),top_reg) and GetNextInstruction(p, hp1) then
  3693. begin
  3694. if ((MatchInstruction(hp1, A_TEST, [S_B]) and
  3695. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3696. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg)) or
  3697. (MatchInstruction(hp1, A_CMP, [S_B]) and
  3698. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3699. (taicpu(hp1).oper[0]^.val=0))
  3700. ) and
  3701. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  3702. GetNextInstruction(hp1, hp2) and
  3703. MatchInstruction(hp2, A_Jcc, []) then
  3704. { Change from: To:
  3705. set(C) %reg j(~C) label
  3706. test %reg,%reg/cmp $0,%reg
  3707. je label
  3708. set(C) %reg j(C) label
  3709. test %reg,%reg/cmp $0,%reg
  3710. jne label
  3711. }
  3712. begin
  3713. next := tai(p.Next);
  3714. TransferUsedRegs(TmpUsedRegs);
  3715. UpdateUsedRegs(TmpUsedRegs, next);
  3716. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  3717. JumpC := taicpu(hp2).condition;
  3718. Unconditional := False;
  3719. if conditions_equal(JumpC, C_E) then
  3720. SetC := inverse_cond(taicpu(p).condition)
  3721. else if conditions_equal(JumpC, C_NE) then
  3722. SetC := taicpu(p).condition
  3723. else
  3724. { We've got something weird here (and inefficent) }
  3725. begin
  3726. DebugMsg('DEBUG: Inefficient jump - check code generation', p);
  3727. SetC := C_NONE;
  3728. { JAE/JNB will always branch (use 'condition_in', since C_AE <> C_NB normally) }
  3729. if condition_in(C_AE, JumpC) then
  3730. Unconditional := True
  3731. else
  3732. { Not sure what to do with this jump - drop out }
  3733. Exit;
  3734. end;
  3735. RemoveInstruction(hp1);
  3736. if Unconditional then
  3737. MakeUnconditional(taicpu(hp2))
  3738. else
  3739. begin
  3740. if SetC = C_NONE then
  3741. InternalError(2018061402);
  3742. taicpu(hp2).SetCondition(SetC);
  3743. end;
  3744. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
  3745. begin
  3746. RemoveCurrentp(p, hp2);
  3747. Result := True;
  3748. end;
  3749. DebugMsg(SPeepholeOptimization + 'SETcc/TESTCmp/Jcc -> Jcc',p);
  3750. end
  3751. else if MatchInstruction(hp1, A_MOV, [S_B]) and
  3752. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3753. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) then
  3754. begin
  3755. TransferUsedRegs(TmpUsedRegs);
  3756. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3757. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp1, TmpUsedRegs) then
  3758. begin
  3759. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  3760. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[1]^.reg;
  3761. RemoveInstruction(hp1);
  3762. DebugMsg(SPeepholeOptimization + 'SETcc/Mov -> SETcc',p);
  3763. Result := true;
  3764. end;
  3765. end;
  3766. end;
  3767. end;
  3768. function TX86AsmOptimizer.OptPass1FSTP(var p: tai): boolean;
  3769. { returns true if a "continue" should be done after this optimization }
  3770. var
  3771. hp1, hp2: tai;
  3772. begin
  3773. Result := false;
  3774. if MatchOpType(taicpu(p),top_ref) and
  3775. GetNextInstruction(p, hp1) and
  3776. (hp1.typ = ait_instruction) and
  3777. (((taicpu(hp1).opcode = A_FLD) and
  3778. (taicpu(p).opcode = A_FSTP)) or
  3779. ((taicpu(p).opcode = A_FISTP) and
  3780. (taicpu(hp1).opcode = A_FILD))) and
  3781. MatchOpType(taicpu(hp1),top_ref) and
  3782. (taicpu(hp1).opsize = taicpu(p).opsize) and
  3783. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  3784. begin
  3785. { replacing fstp f;fld f by fst f is only valid for extended because of rounding or if fastmath is on }
  3786. if ((taicpu(p).opsize=S_FX) or (cs_opt_fastmath in current_settings.optimizerswitches)) and
  3787. GetNextInstruction(hp1, hp2) and
  3788. (hp2.typ = ait_instruction) and
  3789. IsExitCode(hp2) and
  3790. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  3791. not(assigned(current_procinfo.procdef.funcretsym) and
  3792. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  3793. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  3794. begin
  3795. RemoveInstruction(hp1);
  3796. RemoveCurrentP(p, hp2);
  3797. RemoveLastDeallocForFuncRes(p);
  3798. Result := true;
  3799. end
  3800. else
  3801. { we can do this only in fast math mode as fstp is rounding ...
  3802. ... still disabled as it breaks the compiler and/or rtl }
  3803. if ({ (cs_opt_fastmath in current_settings.optimizerswitches) or }
  3804. { ... or if another fstp equal to the first one follows }
  3805. (GetNextInstruction(hp1,hp2) and
  3806. (hp2.typ = ait_instruction) and
  3807. (taicpu(p).opcode=taicpu(hp2).opcode) and
  3808. (taicpu(p).opsize=taicpu(hp2).opsize))
  3809. ) and
  3810. { fst can't store an extended/comp value }
  3811. (taicpu(p).opsize <> S_FX) and
  3812. (taicpu(p).opsize <> S_IQ) then
  3813. begin
  3814. if (taicpu(p).opcode = A_FSTP) then
  3815. taicpu(p).opcode := A_FST
  3816. else
  3817. taicpu(p).opcode := A_FIST;
  3818. DebugMsg(SPeepholeOptimization + 'FstpFld2Fst',p);
  3819. RemoveInstruction(hp1);
  3820. end;
  3821. end;
  3822. end;
  3823. function TX86AsmOptimizer.OptPass1FLD(var p : tai) : boolean;
  3824. var
  3825. hp1, hp2: tai;
  3826. begin
  3827. result:=false;
  3828. if MatchOpType(taicpu(p),top_reg) and
  3829. GetNextInstruction(p, hp1) and
  3830. (hp1.typ = Ait_Instruction) and
  3831. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3832. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  3833. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  3834. { change to
  3835. fld reg fxxx reg,st
  3836. fxxxp st, st1 (hp1)
  3837. Remark: non commutative operations must be reversed!
  3838. }
  3839. begin
  3840. case taicpu(hp1).opcode Of
  3841. A_FMULP,A_FADDP,
  3842. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  3843. begin
  3844. case taicpu(hp1).opcode Of
  3845. A_FADDP: taicpu(hp1).opcode := A_FADD;
  3846. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  3847. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  3848. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  3849. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  3850. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  3851. else
  3852. internalerror(2019050534);
  3853. end;
  3854. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  3855. taicpu(hp1).oper[1]^.reg := NR_ST;
  3856. RemoveCurrentP(p, hp1);
  3857. Result:=true;
  3858. exit;
  3859. end;
  3860. else
  3861. ;
  3862. end;
  3863. end
  3864. else
  3865. if MatchOpType(taicpu(p),top_ref) and
  3866. GetNextInstruction(p, hp2) and
  3867. (hp2.typ = Ait_Instruction) and
  3868. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  3869. (taicpu(p).opsize in [S_FS, S_FL]) and
  3870. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  3871. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  3872. if GetLastInstruction(p, hp1) and
  3873. MatchInstruction(hp1,A_FLD,A_FST,[taicpu(p).opsize]) and
  3874. MatchOpType(taicpu(hp1),top_ref) and
  3875. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  3876. if ((taicpu(hp2).opcode = A_FMULP) or
  3877. (taicpu(hp2).opcode = A_FADDP)) then
  3878. { change to
  3879. fld/fst mem1 (hp1) fld/fst mem1
  3880. fld mem1 (p) fadd/
  3881. faddp/ fmul st, st
  3882. fmulp st, st1 (hp2) }
  3883. begin
  3884. RemoveCurrentP(p, hp1);
  3885. if (taicpu(hp2).opcode = A_FADDP) then
  3886. taicpu(hp2).opcode := A_FADD
  3887. else
  3888. taicpu(hp2).opcode := A_FMUL;
  3889. taicpu(hp2).oper[1]^.reg := NR_ST;
  3890. end
  3891. else
  3892. { change to
  3893. fld/fst mem1 (hp1) fld/fst mem1
  3894. fld mem1 (p) fld st}
  3895. begin
  3896. taicpu(p).changeopsize(S_FL);
  3897. taicpu(p).loadreg(0,NR_ST);
  3898. end
  3899. else
  3900. begin
  3901. case taicpu(hp2).opcode Of
  3902. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  3903. { change to
  3904. fld/fst mem1 (hp1) fld/fst mem1
  3905. fld mem2 (p) fxxx mem2
  3906. fxxxp st, st1 (hp2) }
  3907. begin
  3908. case taicpu(hp2).opcode Of
  3909. A_FADDP: taicpu(p).opcode := A_FADD;
  3910. A_FMULP: taicpu(p).opcode := A_FMUL;
  3911. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  3912. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  3913. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  3914. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  3915. else
  3916. internalerror(2019050533);
  3917. end;
  3918. RemoveInstruction(hp2);
  3919. end
  3920. else
  3921. ;
  3922. end
  3923. end
  3924. end;
  3925. function TX86AsmOptimizer.OptPass1Cmp(var p: tai): boolean;
  3926. var
  3927. v: TCGInt;
  3928. hp1, hp2: tai;
  3929. begin
  3930. Result:=false;
  3931. if taicpu(p).oper[0]^.typ = top_const then
  3932. begin
  3933. { Though GetNextInstruction can be factored out, it is an expensive
  3934. call, so delay calling it until we have first checked cheaper
  3935. conditions that are independent of it. }
  3936. if (taicpu(p).oper[0]^.val = 0) and
  3937. (taicpu(p).oper[1]^.typ = top_reg) and
  3938. GetNextInstruction(p, hp1) and
  3939. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) then
  3940. begin
  3941. hp2 := p;
  3942. { When dealing with "cmp $0,%reg", only ZF and SF contain
  3943. anything meaningful once it's converted to "test %reg,%reg";
  3944. additionally, some jumps will always (or never) branch, so
  3945. evaluate every jump immediately following the
  3946. comparison, optimising the conditions if possible.
  3947. Similarly with SETcc... those that are always set to 0 or 1
  3948. are changed to MOV instructions }
  3949. while GetNextInstruction(hp2, hp1) and
  3950. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) do
  3951. begin
  3952. case taicpu(hp1).condition of
  3953. C_B, C_C, C_NAE, C_O:
  3954. { For B/NAE:
  3955. Will never branch since an unsigned integer can never be below zero
  3956. For C/O:
  3957. Result cannot overflow because 0 is being subtracted
  3958. }
  3959. begin
  3960. if taicpu(hp1).opcode = A_Jcc then
  3961. begin
  3962. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (jump removed)', hp1);
  3963. TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
  3964. RemoveInstruction(hp1);
  3965. { Since hp1 was deleted, hp2 must not be updated }
  3966. Continue;
  3967. end
  3968. else
  3969. begin
  3970. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (set -> mov 0)', hp1);
  3971. { Convert "set(c) %reg" instruction to "movb 0,%reg" }
  3972. taicpu(hp1).opcode := A_MOV;
  3973. taicpu(hp1).ops := 2;
  3974. taicpu(hp1).condition := C_None;
  3975. taicpu(hp1).opsize := S_B;
  3976. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  3977. taicpu(hp1).loadconst(0, 0);
  3978. end;
  3979. end;
  3980. C_BE, C_NA:
  3981. begin
  3982. { Will only branch if equal to zero }
  3983. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition BE/NA --> E', hp1);
  3984. taicpu(hp1).condition := C_E;
  3985. end;
  3986. C_A, C_NBE:
  3987. begin
  3988. { Will only branch if not equal to zero }
  3989. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition A/NBE --> NE', hp1);
  3990. taicpu(hp1).condition := C_NE;
  3991. end;
  3992. C_AE, C_NB, C_NC, C_NO:
  3993. begin
  3994. { Will always branch }
  3995. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition AE/NB/NC/NO --> Always', hp1);
  3996. if taicpu(hp1).opcode = A_Jcc then
  3997. begin
  3998. MakeUnconditional(taicpu(hp1));
  3999. { Any jumps/set that follow will now be dead code }
  4000. RemoveDeadCodeAfterJump(taicpu(hp1));
  4001. Break;
  4002. end
  4003. else
  4004. begin
  4005. { Convert "set(c) %reg" instruction to "movb 1,%reg" }
  4006. taicpu(hp1).opcode := A_MOV;
  4007. taicpu(hp1).ops := 2;
  4008. taicpu(hp1).condition := C_None;
  4009. taicpu(hp1).opsize := S_B;
  4010. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  4011. taicpu(hp1).loadconst(0, 1);
  4012. end;
  4013. end;
  4014. C_None:
  4015. InternalError(2020012201);
  4016. C_P, C_PE, C_NP, C_PO:
  4017. { We can't handle parity checks and they should never be generated
  4018. after a general-purpose CMP (it's used in some floating-point
  4019. comparisons that don't use CMP) }
  4020. InternalError(2020012202);
  4021. else
  4022. { Zero/Equality, Sign, their complements and all of the
  4023. signed comparisons do not need to be converted };
  4024. end;
  4025. hp2 := hp1;
  4026. end;
  4027. { Convert the instruction to a TEST }
  4028. taicpu(p).opcode := A_TEST;
  4029. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  4030. Result := True;
  4031. Exit;
  4032. end
  4033. else if (taicpu(p).oper[0]^.val = 1) and
  4034. GetNextInstruction(p, hp1) and
  4035. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  4036. (taicpu(hp1).condition in [C_L, C_NGE]) then
  4037. begin
  4038. { Convert; To:
  4039. cmp $1,r/m cmp $0,r/m
  4040. jl @lbl jle @lbl
  4041. }
  4042. DebugMsg(SPeepholeOptimization + 'Cmp1Jl2Cmp0Jle', p);
  4043. taicpu(p).oper[0]^.val := 0;
  4044. taicpu(hp1).condition := C_LE;
  4045. { If the instruction is now "cmp $0,%reg", convert it to a
  4046. TEST (and effectively do the work of the "cmp $0,%reg" in
  4047. the block above)
  4048. If it's a reference, we can get away with not setting
  4049. Result to True because he haven't evaluated the jump
  4050. in this pass yet.
  4051. }
  4052. if (taicpu(p).oper[1]^.typ = top_reg) then
  4053. begin
  4054. taicpu(p).opcode := A_TEST;
  4055. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  4056. Result := True;
  4057. end;
  4058. Exit;
  4059. end
  4060. else if (taicpu(p).oper[1]^.typ = top_reg) then
  4061. begin
  4062. { cmp register,$8000 neg register
  4063. je target --> jo target
  4064. .... only if register is deallocated before jump.}
  4065. case Taicpu(p).opsize of
  4066. S_B: v:=$80;
  4067. S_W: v:=$8000;
  4068. S_L: v:=qword($80000000);
  4069. { S_Q will never happen: cmp with 64 bit constants is not possible }
  4070. S_Q:
  4071. Exit;
  4072. else
  4073. internalerror(2013112905);
  4074. end;
  4075. if (taicpu(p).oper[0]^.val=v) and
  4076. GetNextInstruction(p, hp1) and
  4077. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  4078. (Taicpu(hp1).condition in [C_E,C_NE]) then
  4079. begin
  4080. TransferUsedRegs(TmpUsedRegs);
  4081. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  4082. if not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, TmpUsedRegs)) then
  4083. begin
  4084. DebugMsg(SPeepholeOptimization + 'CmpJe2NegJo done',p);
  4085. Taicpu(p).opcode:=A_NEG;
  4086. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  4087. Taicpu(p).clearop(1);
  4088. Taicpu(p).ops:=1;
  4089. if Taicpu(hp1).condition=C_E then
  4090. Taicpu(hp1).condition:=C_O
  4091. else
  4092. Taicpu(hp1).condition:=C_NO;
  4093. Result:=true;
  4094. exit;
  4095. end;
  4096. end;
  4097. end;
  4098. end;
  4099. end;
  4100. function TX86AsmOptimizer.OptPass1PXor(var p: tai): boolean;
  4101. var
  4102. hp1: tai;
  4103. begin
  4104. {
  4105. remove the second (v)pxor from
  4106. pxor reg,reg
  4107. ...
  4108. pxor reg,reg
  4109. }
  4110. Result:=false;
  4111. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  4112. MatchOpType(taicpu(p),top_reg,top_reg) and
  4113. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  4114. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  4115. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  4116. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) then
  4117. begin
  4118. DebugMsg(SPeepholeOptimization + 'PXorPXor2PXor done',hp1);
  4119. RemoveInstruction(hp1);
  4120. Result:=true;
  4121. Exit;
  4122. end
  4123. {
  4124. replace
  4125. pxor reg1,reg1
  4126. movapd/s reg1,reg2
  4127. dealloc reg1
  4128. by
  4129. pxor reg2,reg2
  4130. }
  4131. else if GetNextInstruction(p,hp1) and
  4132. { we mix single and double opperations here because we assume that the compiler
  4133. generates vmovapd only after double operations and vmovaps only after single operations }
  4134. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  4135. MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  4136. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  4137. (taicpu(p).oper[0]^.typ=top_reg) then
  4138. begin
  4139. TransferUsedRegs(TmpUsedRegs);
  4140. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4141. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  4142. begin
  4143. taicpu(p).loadoper(0,taicpu(hp1).oper[1]^);
  4144. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  4145. DebugMsg(SPeepholeOptimization + 'PXorMovapd2PXor done',p);
  4146. RemoveInstruction(hp1);
  4147. result:=true;
  4148. end;
  4149. end;
  4150. end;
  4151. function TX86AsmOptimizer.OptPass1VPXor(var p: tai): boolean;
  4152. var
  4153. hp1: tai;
  4154. begin
  4155. {
  4156. remove the second (v)pxor from
  4157. (v)pxor reg,reg
  4158. ...
  4159. (v)pxor reg,reg
  4160. }
  4161. Result:=false;
  4162. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^,taicpu(p).oper[2]^) and
  4163. MatchOpType(taicpu(p),top_reg,top_reg,top_reg) and
  4164. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  4165. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  4166. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  4167. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^,taicpu(hp1).oper[2]^) then
  4168. begin
  4169. DebugMsg(SPeepholeOptimization + 'VPXorVPXor2PXor done',hp1);
  4170. RemoveInstruction(hp1);
  4171. Result:=true;
  4172. Exit;
  4173. end
  4174. else
  4175. Result:=OptPass1VOP(p);
  4176. end;
  4177. function TX86AsmOptimizer.OptPass1Imul(var p: tai): boolean;
  4178. var
  4179. hp1 : tai;
  4180. begin
  4181. result:=false;
  4182. { replace
  4183. IMul const,%mreg1,%mreg2
  4184. Mov %reg2,%mreg3
  4185. dealloc %mreg3
  4186. by
  4187. Imul const,%mreg1,%mreg23
  4188. }
  4189. if (taicpu(p).ops=3) and
  4190. GetNextInstruction(p,hp1) and
  4191. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  4192. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  4193. (taicpu(hp1).oper[1]^.typ=top_reg) then
  4194. begin
  4195. TransferUsedRegs(TmpUsedRegs);
  4196. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4197. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  4198. begin
  4199. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  4200. DebugMsg(SPeepholeOptimization + 'ImulMov2Imul done',p);
  4201. RemoveInstruction(hp1);
  4202. result:=true;
  4203. end;
  4204. end;
  4205. end;
  4206. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  4207. function IsXCHGAcceptable: Boolean; inline;
  4208. begin
  4209. { Always accept if optimising for size }
  4210. Result := (cs_opt_size in current_settings.optimizerswitches) or
  4211. (
  4212. {$ifdef x86_64}
  4213. { XCHG takes 3 cycles on AMD Athlon64 }
  4214. (current_settings.optimizecputype >= cpu_core_i)
  4215. {$else x86_64}
  4216. { From the Pentium M onwards, XCHG only has a latency of 2 rather
  4217. than 3, so it becomes a saving compared to three MOVs with two of
  4218. them able to execute simultaneously. [Kit] }
  4219. (current_settings.optimizecputype >= cpu_PentiumM)
  4220. {$endif x86_64}
  4221. );
  4222. end;
  4223. var
  4224. NewRef: TReference;
  4225. hp1,hp2,hp3: tai;
  4226. {$ifndef x86_64}
  4227. hp4: tai;
  4228. OperIdx: Integer;
  4229. {$endif x86_64}
  4230. begin
  4231. Result:=false;
  4232. if not GetNextInstruction(p, hp1) then
  4233. Exit;
  4234. if MatchInstruction(hp1, A_JMP, [S_NO]) then
  4235. begin
  4236. { Sometimes the MOVs that OptPass2JMP produces can be improved
  4237. further, but we can't just put this jump optimisation in pass 1
  4238. because it tends to perform worse when conditional jumps are
  4239. nearby (e.g. when converting CMOV instructions). [Kit] }
  4240. if OptPass2JMP(hp1) then
  4241. { call OptPass1MOV once to potentially merge any MOVs that were created }
  4242. Result := OptPass1MOV(p)
  4243. { OptPass2MOV will now exit but will be called again if OptPass1MOV
  4244. returned True and the instruction is still a MOV, thus checking
  4245. the optimisations below }
  4246. { If OptPass2JMP returned False, no optimisations were done to
  4247. the jump and there are no further optimisations that can be done
  4248. to the MOV instruction on this pass }
  4249. end
  4250. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  4251. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  4252. MatchInstruction(hp1,A_ADD,A_SUB,[taicpu(p).opsize]) and
  4253. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4254. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) and
  4255. { be lazy, checking separately for sub would be slightly better }
  4256. (abs(taicpu(hp1).oper[0]^.val)<=$7fffffff) then
  4257. begin
  4258. { Change:
  4259. movl/q %reg1,%reg2 movl/q %reg1,%reg2
  4260. addl/q $x,%reg2 subl/q $x,%reg2
  4261. To:
  4262. leal/q x(%reg1),%reg2 leal/q -x(%reg1),%reg2
  4263. }
  4264. TransferUsedRegs(TmpUsedRegs);
  4265. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4266. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  4267. if not GetNextInstruction(hp1, hp2) or
  4268. (
  4269. { The FLAGS register isn't always tracked properly, so do not
  4270. perform this optimisation if a conditional statement follows }
  4271. not RegReadByInstruction(NR_DEFAULTFLAGS, hp2) and
  4272. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)
  4273. ) then
  4274. begin
  4275. reference_reset(NewRef, 1, []);
  4276. NewRef.base := taicpu(p).oper[0]^.reg;
  4277. NewRef.scalefactor := 1;
  4278. if taicpu(hp1).opcode = A_ADD then
  4279. begin
  4280. DebugMsg(SPeepholeOptimization + 'MovAdd2Lea', p);
  4281. NewRef.offset := taicpu(hp1).oper[0]^.val;
  4282. end
  4283. else
  4284. begin
  4285. DebugMsg(SPeepholeOptimization + 'MovSub2Lea', p);
  4286. NewRef.offset := -taicpu(hp1).oper[0]^.val;
  4287. end;
  4288. taicpu(p).opcode := A_LEA;
  4289. taicpu(p).loadref(0, NewRef);
  4290. RemoveInstruction(hp1);
  4291. Result := True;
  4292. Exit;
  4293. end;
  4294. end
  4295. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  4296. {$ifdef x86_64}
  4297. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  4298. {$else x86_64}
  4299. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  4300. {$endif x86_64}
  4301. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  4302. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  4303. { mov reg1, reg2 mov reg1, reg2
  4304. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  4305. begin
  4306. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  4307. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  4308. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  4309. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  4310. TransferUsedRegs(TmpUsedRegs);
  4311. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4312. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  4313. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  4314. then
  4315. begin
  4316. RemoveCurrentP(p, hp1);
  4317. Result:=true;
  4318. end;
  4319. exit;
  4320. end
  4321. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  4322. IsXCHGAcceptable and
  4323. { XCHG doesn't support 8-byte registers }
  4324. (taicpu(p).opsize <> S_B) and
  4325. MatchInstruction(hp1, A_MOV, []) and
  4326. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  4327. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  4328. GetNextInstruction(hp1, hp2) and
  4329. MatchInstruction(hp2, A_MOV, []) and
  4330. { Don't need to call MatchOpType for hp2 because the operand matches below cover for it }
  4331. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  4332. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) then
  4333. begin
  4334. { mov %reg1,%reg2
  4335. mov %reg3,%reg1 -> xchg %reg3,%reg1
  4336. mov %reg2,%reg3
  4337. (%reg2 not used afterwards)
  4338. Note that xchg takes 3 cycles to execute, and generally mov's take
  4339. only one cycle apiece, but the first two mov's can be executed in
  4340. parallel, only taking 2 cycles overall. Older processors should
  4341. therefore only optimise for size. [Kit]
  4342. }
  4343. TransferUsedRegs(TmpUsedRegs);
  4344. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4345. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  4346. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs) then
  4347. begin
  4348. DebugMsg(SPeepholeOptimization + 'MovMovMov2XChg', p);
  4349. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp1, UsedRegs);
  4350. taicpu(hp1).opcode := A_XCHG;
  4351. RemoveCurrentP(p, hp1);
  4352. RemoveInstruction(hp2);
  4353. Result := True;
  4354. Exit;
  4355. end;
  4356. end
  4357. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  4358. MatchInstruction(hp1, A_SAR, []) then
  4359. begin
  4360. if MatchOperand(taicpu(hp1).oper[0]^, 31) then
  4361. begin
  4362. { the use of %edx also covers the opsize being S_L }
  4363. if MatchOperand(taicpu(hp1).oper[1]^, NR_EDX) then
  4364. begin
  4365. { Note it has to be specifically "movl %eax,%edx", and those specific sub-registers }
  4366. if (taicpu(p).oper[0]^.reg = NR_EAX) and
  4367. (taicpu(p).oper[1]^.reg = NR_EDX) then
  4368. begin
  4369. { Change:
  4370. movl %eax,%edx
  4371. sarl $31,%edx
  4372. To:
  4373. cltd
  4374. }
  4375. DebugMsg(SPeepholeOptimization + 'MovSar2Cltd', p);
  4376. RemoveInstruction(hp1);
  4377. taicpu(p).opcode := A_CDQ;
  4378. taicpu(p).opsize := S_NO;
  4379. taicpu(p).clearop(1);
  4380. taicpu(p).clearop(0);
  4381. taicpu(p).ops:=0;
  4382. Result := True;
  4383. end
  4384. else if (cs_opt_size in current_settings.optimizerswitches) and
  4385. (taicpu(p).oper[0]^.reg = NR_EDX) and
  4386. (taicpu(p).oper[1]^.reg = NR_EAX) then
  4387. begin
  4388. { Change:
  4389. movl %edx,%eax
  4390. sarl $31,%edx
  4391. To:
  4392. movl %edx,%eax
  4393. cltd
  4394. Note that this creates a dependency between the two instructions,
  4395. so only perform if optimising for size.
  4396. }
  4397. DebugMsg(SPeepholeOptimization + 'MovSar2MovCltd', p);
  4398. taicpu(hp1).opcode := A_CDQ;
  4399. taicpu(hp1).opsize := S_NO;
  4400. taicpu(hp1).clearop(1);
  4401. taicpu(hp1).clearop(0);
  4402. taicpu(hp1).ops:=0;
  4403. end;
  4404. {$ifndef x86_64}
  4405. end
  4406. { Don't bother if CMOV is supported, because a more optimal
  4407. sequence would have been generated for the Abs() intrinsic }
  4408. else if not(CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and
  4409. { the use of %eax also covers the opsize being S_L }
  4410. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) and
  4411. (taicpu(p).oper[0]^.reg = NR_EAX) and
  4412. (taicpu(p).oper[1]^.reg = NR_EDX) and
  4413. GetNextInstruction(hp1, hp2) and
  4414. MatchInstruction(hp2, A_XOR, [S_L]) and
  4415. MatchOperand(taicpu(hp2).oper[0]^, NR_EAX) and
  4416. MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) and
  4417. GetNextInstruction(hp2, hp3) and
  4418. MatchInstruction(hp3, A_SUB, [S_L]) and
  4419. MatchOperand(taicpu(hp3).oper[0]^, NR_EAX) and
  4420. MatchOperand(taicpu(hp3).oper[1]^, NR_EDX) then
  4421. begin
  4422. { Change:
  4423. movl %eax,%edx
  4424. sarl $31,%eax
  4425. xorl %eax,%edx
  4426. subl %eax,%edx
  4427. (Instruction that uses %edx)
  4428. (%eax deallocated)
  4429. (%edx deallocated)
  4430. To:
  4431. cltd
  4432. xorl %edx,%eax <-- Note the registers have swapped
  4433. subl %edx,%eax
  4434. (Instruction that uses %eax) <-- %eax rather than %edx
  4435. }
  4436. TransferUsedRegs(TmpUsedRegs);
  4437. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4438. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  4439. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  4440. if not RegUsedAfterInstruction(NR_EAX, hp3, TmpUsedRegs) then
  4441. begin
  4442. if GetNextInstruction(hp3, hp4) and
  4443. not RegModifiedByInstruction(NR_EDX, hp4) and
  4444. not RegUsedAfterInstruction(NR_EDX, hp4, TmpUsedRegs) then
  4445. begin
  4446. DebugMsg(SPeepholeOptimization + 'abs() intrinsic optimisation', p);
  4447. taicpu(p).opcode := A_CDQ;
  4448. taicpu(p).clearop(1);
  4449. taicpu(p).clearop(0);
  4450. taicpu(p).ops:=0;
  4451. RemoveInstruction(hp1);
  4452. taicpu(hp2).loadreg(0, NR_EDX);
  4453. taicpu(hp2).loadreg(1, NR_EAX);
  4454. taicpu(hp3).loadreg(0, NR_EDX);
  4455. taicpu(hp3).loadreg(1, NR_EAX);
  4456. AllocRegBetween(NR_EAX, hp3, hp4, TmpUsedRegs);
  4457. { Convert references in the following instruction (hp4) from %edx to %eax }
  4458. for OperIdx := 0 to taicpu(hp4).ops - 1 do
  4459. with taicpu(hp4).oper[OperIdx]^ do
  4460. case typ of
  4461. top_reg:
  4462. if getsupreg(reg) = RS_EDX then
  4463. reg := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  4464. top_ref:
  4465. begin
  4466. if getsupreg(reg) = RS_EDX then
  4467. ref^.base := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  4468. if getsupreg(reg) = RS_EDX then
  4469. ref^.index := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  4470. end;
  4471. else
  4472. ;
  4473. end;
  4474. end;
  4475. end;
  4476. {$else x86_64}
  4477. end;
  4478. end
  4479. else if MatchOperand(taicpu(hp1).oper[0]^, 63) and
  4480. { the use of %rdx also covers the opsize being S_Q }
  4481. MatchOperand(taicpu(hp1).oper[1]^, NR_RDX) then
  4482. begin
  4483. { Note it has to be specifically "movq %rax,%rdx", and those specific sub-registers }
  4484. if (taicpu(p).oper[0]^.reg = NR_RAX) and
  4485. (taicpu(p).oper[1]^.reg = NR_RDX) then
  4486. begin
  4487. { Change:
  4488. movq %rax,%rdx
  4489. sarq $63,%rdx
  4490. To:
  4491. cqto
  4492. }
  4493. DebugMsg(SPeepholeOptimization + 'MovSar2Cqto', p);
  4494. RemoveInstruction(hp1);
  4495. taicpu(p).opcode := A_CQO;
  4496. taicpu(p).opsize := S_NO;
  4497. taicpu(p).clearop(1);
  4498. taicpu(p).clearop(0);
  4499. taicpu(p).ops:=0;
  4500. Result := True;
  4501. end
  4502. else if (cs_opt_size in current_settings.optimizerswitches) and
  4503. (taicpu(p).oper[0]^.reg = NR_RDX) and
  4504. (taicpu(p).oper[1]^.reg = NR_RAX) then
  4505. begin
  4506. { Change:
  4507. movq %rdx,%rax
  4508. sarq $63,%rdx
  4509. To:
  4510. movq %rdx,%rax
  4511. cqto
  4512. Note that this creates a dependency between the two instructions,
  4513. so only perform if optimising for size.
  4514. }
  4515. DebugMsg(SPeepholeOptimization + 'MovSar2MovCqto', p);
  4516. taicpu(hp1).opcode := A_CQO;
  4517. taicpu(hp1).opsize := S_NO;
  4518. taicpu(hp1).clearop(1);
  4519. taicpu(hp1).clearop(0);
  4520. taicpu(hp1).ops:=0;
  4521. {$endif x86_64}
  4522. end;
  4523. end;
  4524. end
  4525. else if MatchInstruction(hp1, A_MOV, []) and
  4526. (taicpu(hp1).oper[1]^.typ = top_reg) then
  4527. { Though "GetNextInstruction" could be factored out, along with
  4528. the instructions that depend on hp2, it is an expensive call that
  4529. should be delayed for as long as possible, hence we do cheaper
  4530. checks first that are likely to be False. [Kit] }
  4531. begin
  4532. if MatchOperand(taicpu(p).oper[1]^, NR_EDX) and
  4533. (
  4534. (
  4535. (taicpu(hp1).oper[1]^.reg = NR_EAX) and
  4536. (
  4537. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4538. MatchOperand(taicpu(hp1).oper[0]^, NR_EDX)
  4539. )
  4540. ) or
  4541. (
  4542. (taicpu(hp1).oper[1]^.reg = NR_EDX) and
  4543. (
  4544. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4545. MatchOperand(taicpu(hp1).oper[0]^, NR_EAX)
  4546. )
  4547. )
  4548. ) and
  4549. GetNextInstruction(hp1, hp2) and
  4550. MatchInstruction(hp2, A_SAR, []) and
  4551. MatchOperand(taicpu(hp2).oper[0]^, 31) then
  4552. begin
  4553. if MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) then
  4554. begin
  4555. { Change:
  4556. movl r/m,%edx movl r/m,%eax movl r/m,%edx movl r/m,%eax
  4557. movl %edx,%eax or movl %eax,%edx or movl r/m,%eax or movl r/m,%edx
  4558. sarl $31,%edx sarl $31,%edx sarl $31,%edx sarl $31,%edx
  4559. To:
  4560. movl r/m,%eax <- Note the change in register
  4561. cltd
  4562. }
  4563. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCltd', p);
  4564. AllocRegBetween(NR_EAX, p, hp1, UsedRegs);
  4565. taicpu(p).loadreg(1, NR_EAX);
  4566. taicpu(hp1).opcode := A_CDQ;
  4567. taicpu(hp1).clearop(1);
  4568. taicpu(hp1).clearop(0);
  4569. taicpu(hp1).ops:=0;
  4570. RemoveInstruction(hp2);
  4571. (*
  4572. {$ifdef x86_64}
  4573. end
  4574. else if MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) and
  4575. { This code sequence does not get generated - however it might become useful
  4576. if and when 128-bit signed integer types make an appearance, so the code
  4577. is kept here for when it is eventually needed. [Kit] }
  4578. (
  4579. (
  4580. (taicpu(hp1).oper[1]^.reg = NR_RAX) and
  4581. (
  4582. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4583. MatchOperand(taicpu(hp1).oper[0]^, NR_RDX)
  4584. )
  4585. ) or
  4586. (
  4587. (taicpu(hp1).oper[1]^.reg = NR_RDX) and
  4588. (
  4589. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4590. MatchOperand(taicpu(hp1).oper[0]^, NR_RAX)
  4591. )
  4592. )
  4593. ) and
  4594. GetNextInstruction(hp1, hp2) and
  4595. MatchInstruction(hp2, A_SAR, [S_Q]) and
  4596. MatchOperand(taicpu(hp2).oper[0]^, 63) and
  4597. MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) then
  4598. begin
  4599. { Change:
  4600. movq r/m,%rdx movq r/m,%rax movq r/m,%rdx movq r/m,%rax
  4601. movq %rdx,%rax or movq %rax,%rdx or movq r/m,%rax or movq r/m,%rdx
  4602. sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx
  4603. To:
  4604. movq r/m,%rax <- Note the change in register
  4605. cqto
  4606. }
  4607. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCqto', p);
  4608. AllocRegBetween(NR_RAX, p, hp1, UsedRegs);
  4609. taicpu(p).loadreg(1, NR_RAX);
  4610. taicpu(hp1).opcode := A_CQO;
  4611. taicpu(hp1).clearop(1);
  4612. taicpu(hp1).clearop(0);
  4613. taicpu(hp1).ops:=0;
  4614. RemoveInstruction(hp2);
  4615. {$endif x86_64}
  4616. *)
  4617. end;
  4618. end;
  4619. {$ifdef x86_64}
  4620. end
  4621. else if (taicpu(p).opsize = S_L) and
  4622. (taicpu(p).oper[1]^.typ = top_reg) and
  4623. (
  4624. MatchInstruction(hp1, A_MOV,[]) and
  4625. (taicpu(hp1).opsize = S_L) and
  4626. (taicpu(hp1).oper[1]^.typ = top_reg)
  4627. ) and (
  4628. GetNextInstruction(hp1, hp2) and
  4629. (tai(hp2).typ=ait_instruction) and
  4630. (taicpu(hp2).opsize = S_Q) and
  4631. (
  4632. (
  4633. MatchInstruction(hp2, A_ADD,[]) and
  4634. (taicpu(hp2).opsize = S_Q) and
  4635. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4636. (
  4637. (
  4638. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4639. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4640. ) or (
  4641. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4642. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4643. )
  4644. )
  4645. ) or (
  4646. MatchInstruction(hp2, A_LEA,[]) and
  4647. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  4648. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  4649. (
  4650. (
  4651. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4652. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4653. ) or (
  4654. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4655. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  4656. )
  4657. ) and (
  4658. (
  4659. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4660. ) or (
  4661. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4662. )
  4663. )
  4664. )
  4665. )
  4666. ) and (
  4667. GetNextInstruction(hp2, hp3) and
  4668. MatchInstruction(hp3, A_SHR,[]) and
  4669. (taicpu(hp3).opsize = S_Q) and
  4670. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4671. (taicpu(hp3).oper[0]^.val = 1) and
  4672. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  4673. ) then
  4674. begin
  4675. { Change movl x, reg1d movl x, reg1d
  4676. movl y, reg2d movl y, reg2d
  4677. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  4678. shrq $1, reg1q shrq $1, reg1q
  4679. ( reg1d and reg2d can be switched around in the first two instructions )
  4680. To movl x, reg1d
  4681. addl y, reg1d
  4682. rcrl $1, reg1d
  4683. This corresponds to the common expression (x + y) shr 1, where
  4684. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  4685. smaller code, but won't account for x + y causing an overflow). [Kit]
  4686. }
  4687. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  4688. { Change first MOV command to have the same register as the final output }
  4689. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  4690. else
  4691. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  4692. { Change second MOV command to an ADD command. This is easier than
  4693. converting the existing command because it means we don't have to
  4694. touch 'y', which might be a complicated reference, and also the
  4695. fact that the third command might either be ADD or LEA. [Kit] }
  4696. taicpu(hp1).opcode := A_ADD;
  4697. { Delete old ADD/LEA instruction }
  4698. RemoveInstruction(hp2);
  4699. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  4700. taicpu(hp3).opcode := A_RCR;
  4701. taicpu(hp3).changeopsize(S_L);
  4702. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  4703. {$endif x86_64}
  4704. end;
  4705. end;
  4706. function TX86AsmOptimizer.OptPass2Movx(var p : tai) : boolean;
  4707. const
  4708. LIST_STEP_SIZE = 4;
  4709. var
  4710. ThisReg: TRegister;
  4711. MinSize, MaxSize, TrySmaller, TargetSize: TOpSize;
  4712. TargetSubReg: TSubRegister;
  4713. hp1, hp2: tai;
  4714. RegInUse, RegChanged, p_removed: Boolean;
  4715. { Store list of found instructions so we don't have to call
  4716. GetNextInstructionUsingReg multiple times }
  4717. InstrList: array of taicpu;
  4718. InstrMax, Index: Integer;
  4719. UpperLimit, TrySmallerLimit: TCgInt;
  4720. { Data flow analysis }
  4721. TestValMin, TestValMax: TCgInt;
  4722. SmallerOverflow: Boolean;
  4723. begin
  4724. Result := False;
  4725. p_removed := False;
  4726. { This is anything but quick! }
  4727. if not(cs_opt_level2 in current_settings.optimizerswitches) then
  4728. Exit;
  4729. SetLength(InstrList, 0);
  4730. InstrMax := -1;
  4731. ThisReg := taicpu(p).oper[1]^.reg;
  4732. hp1 := p;
  4733. case taicpu(p).opsize of
  4734. S_BW, S_BL:
  4735. begin
  4736. UpperLimit := $FF;
  4737. MinSize := S_B;
  4738. if taicpu(p).opsize = S_BW then
  4739. MaxSize := S_W
  4740. else
  4741. MaxSize := S_L;
  4742. end;
  4743. S_WL:
  4744. begin
  4745. UpperLimit := $FFFF;
  4746. MinSize := S_W;
  4747. MaxSize := S_L;
  4748. end
  4749. else
  4750. InternalError(2020112301);
  4751. end;
  4752. TestValMin := 0;
  4753. TestValMax := UpperLimit;
  4754. TrySmallerLimit := UpperLimit;
  4755. TrySmaller := S_NO;
  4756. SmallerOverflow := False;
  4757. RegChanged := False;
  4758. while GetNextInstructionUsingReg(hp1, hp1, ThisReg) and
  4759. (hp1.typ = ait_instruction) and
  4760. (
  4761. { Under -O1 and -O2, GetNextInstructionUsingReg may return an
  4762. instruction that doesn't actually contain ThisReg }
  4763. (cs_opt_level3 in current_settings.optimizerswitches) or
  4764. RegInInstruction(ThisReg, hp1)
  4765. ) do
  4766. begin
  4767. case taicpu(hp1).opcode of
  4768. A_INC,A_DEC:
  4769. begin
  4770. { Has to be an exact match on the register }
  4771. if not MatchOperand(taicpu(hp1).oper[0]^, ThisReg) then
  4772. Break;
  4773. if taicpu(hp1).opcode = A_INC then
  4774. begin
  4775. Inc(TestValMin);
  4776. Inc(TestValMax);
  4777. end
  4778. else
  4779. begin
  4780. Dec(TestValMin);
  4781. Dec(TestValMax);
  4782. end;
  4783. end;
  4784. { OR and XOR are not included because they can too easily fool
  4785. the data flow analysis (they can cause non-linear behaviour) }
  4786. A_ADD,A_SUB,A_AND,A_SHL,A_SHR:
  4787. begin
  4788. if
  4789. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  4790. { Has to be an exact match on the register }
  4791. (taicpu(hp1).oper[1]^.reg <> ThisReg) or not
  4792. (
  4793. (
  4794. (taicpu(hp1).oper[0]^.typ = top_const) and
  4795. (
  4796. (
  4797. (taicpu(hp1).opcode = A_SHL) and
  4798. (
  4799. ((MinSize = S_B) and (taicpu(hp1).oper[0]^.val < 8)) or
  4800. ((MinSize = S_W) and (taicpu(hp1).oper[0]^.val < 16)) or
  4801. ((MinSize = S_L) and (taicpu(hp1).oper[0]^.val < 32))
  4802. )
  4803. ) or (
  4804. (taicpu(hp1).opcode <> A_SHL) and
  4805. (
  4806. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  4807. { Is it in the negative range? }
  4808. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val))
  4809. )
  4810. )
  4811. )
  4812. ) or (
  4813. MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^.reg) and
  4814. ((taicpu(hp1).opcode = A_ADD) or (taicpu(hp1).opcode = A_AND) or (taicpu(hp1).opcode = A_SUB))
  4815. )
  4816. ) then
  4817. Break;
  4818. case taicpu(hp1).opcode of
  4819. A_ADD:
  4820. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  4821. begin
  4822. TestValMin := TestValMin * 2;
  4823. TestValMax := TestValMax * 2;
  4824. end
  4825. else
  4826. begin
  4827. TestValMin := TestValMin + taicpu(hp1).oper[0]^.val;
  4828. TestValMax := TestValMax + taicpu(hp1).oper[0]^.val;
  4829. end;
  4830. A_SUB:
  4831. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  4832. begin
  4833. TestValMin := 0;
  4834. TestValMax := 0;
  4835. end
  4836. else
  4837. begin
  4838. TestValMin := TestValMin - taicpu(hp1).oper[0]^.val;
  4839. TestValMax := TestValMax - taicpu(hp1).oper[0]^.val;
  4840. end;
  4841. A_AND:
  4842. if (taicpu(hp1).oper[0]^.typ = top_const) then
  4843. begin
  4844. { we might be able to go smaller if AND appears first }
  4845. if InstrMax = -1 then
  4846. case MinSize of
  4847. S_B:
  4848. ;
  4849. S_W:
  4850. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  4851. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  4852. begin
  4853. TrySmaller := S_B;
  4854. TrySmallerLimit := $FF;
  4855. end;
  4856. S_L:
  4857. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  4858. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  4859. begin
  4860. TrySmaller := S_B;
  4861. TrySmallerLimit := $FF;
  4862. end
  4863. else if ((taicpu(hp1).oper[0]^.val and $FFFF) = taicpu(hp1).oper[0]^.val) or
  4864. ((not(taicpu(hp1).oper[0]^.val) and $7FFF) = (not taicpu(hp1).oper[0]^.val)) then
  4865. begin
  4866. TrySmaller := S_W;
  4867. TrySmallerLimit := $FFFF;
  4868. end;
  4869. else
  4870. InternalError(2020112320);
  4871. end;
  4872. TestValMin := TestValMin and taicpu(hp1).oper[0]^.val;
  4873. TestValMax := TestValMax and taicpu(hp1).oper[0]^.val;
  4874. end;
  4875. A_SHL:
  4876. begin
  4877. TestValMin := TestValMin shl taicpu(hp1).oper[0]^.val;
  4878. TestValMax := TestValMax shl taicpu(hp1).oper[0]^.val;
  4879. end;
  4880. A_SHR:
  4881. begin
  4882. { we might be able to go smaller if SHR appears first }
  4883. if InstrMax = -1 then
  4884. case MinSize of
  4885. S_B:
  4886. ;
  4887. S_W:
  4888. if (taicpu(hp1).oper[0]^.val >= 8) then
  4889. begin
  4890. TrySmaller := S_B;
  4891. TrySmallerLimit := $FF;
  4892. end;
  4893. S_L:
  4894. if (taicpu(hp1).oper[0]^.val >= 24) then
  4895. begin
  4896. TrySmaller := S_B;
  4897. TrySmallerLimit := $FF;
  4898. end
  4899. else if (taicpu(hp1).oper[0]^.val >= 16) then
  4900. begin
  4901. TrySmaller := S_W;
  4902. TrySmallerLimit := $FFFF;
  4903. end;
  4904. else
  4905. InternalError(2020112321);
  4906. end;
  4907. TestValMin := TestValMin shr taicpu(hp1).oper[0]^.val;
  4908. TestValMax := TestValMax shr taicpu(hp1).oper[0]^.val;
  4909. end;
  4910. else
  4911. InternalError(2020112303);
  4912. end;
  4913. end;
  4914. (*
  4915. A_IMUL:
  4916. case taicpu(hp1).ops of
  4917. 2:
  4918. begin
  4919. if not MatchOpType(hp1, top_reg, top_reg) or
  4920. { Has to be an exact match on the register }
  4921. (taicpu(hp1).oper[0]^.reg <> ThisReg) or
  4922. (taicpu(hp1).oper[1]^.reg <> ThisReg) then
  4923. Break;
  4924. TestValMin := TestValMin * TestValMin;
  4925. TestValMax := TestValMax * TestValMax;
  4926. end;
  4927. 3:
  4928. begin
  4929. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  4930. { Has to be an exact match on the register }
  4931. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  4932. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  4933. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  4934. { Is it in the negative range? }
  4935. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  4936. Break;
  4937. TestValMin := TestValMin * taicpu(hp1).oper[0]^.val;
  4938. TestValMax := TestValMax * taicpu(hp1).oper[0]^.val;
  4939. end;
  4940. else
  4941. Break;
  4942. end;
  4943. A_IDIV:
  4944. case taicpu(hp1).ops of
  4945. 3:
  4946. begin
  4947. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  4948. { Has to be an exact match on the register }
  4949. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  4950. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  4951. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  4952. { Is it in the negative range? }
  4953. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  4954. Break;
  4955. TestValMin := TestValMin div taicpu(hp1).oper[0]^.val;
  4956. TestValMax := TestValMax div taicpu(hp1).oper[0]^.val;
  4957. end;
  4958. else
  4959. Break;
  4960. end;
  4961. *)
  4962. A_MOVZX:
  4963. begin
  4964. if not MatchOpType(taicpu(hp1), top_reg, top_reg) then
  4965. Break;
  4966. if not SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, ThisReg) then
  4967. begin
  4968. { Because hp1 was obtained via GetNextInstructionUsingReg
  4969. and ThisReg doesn't appear in the first operand, it
  4970. must appear in the second operand and hence gets
  4971. overwritten }
  4972. if (InstrMax = -1) and
  4973. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, ThisReg) then
  4974. begin
  4975. { The two MOVZX instructions are adjacent, so remove the first one }
  4976. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 5', p);
  4977. RemoveCurrentP(p);
  4978. Result := True;
  4979. Exit;
  4980. end;
  4981. Break;
  4982. end;
  4983. { The objective here is to try to find a combination that
  4984. removes one of the MOV/Z instructions. }
  4985. case taicpu(hp1).opsize of
  4986. S_WL:
  4987. if (MinSize in [S_B, S_W]) then
  4988. begin
  4989. TargetSize := S_L;
  4990. TargetSubReg := R_SUBD;
  4991. end
  4992. else if ((TrySmaller in [S_B, S_W]) and not SmallerOverflow) then
  4993. begin
  4994. TargetSize := TrySmaller;
  4995. if TrySmaller = S_B then
  4996. TargetSubReg := R_SUBL
  4997. else
  4998. TargetSubReg := R_SUBW;
  4999. end
  5000. else
  5001. Break;
  5002. S_BW:
  5003. if (MinSize in [S_B, S_W]) then
  5004. begin
  5005. TargetSize := S_W;
  5006. TargetSubReg := R_SUBW;
  5007. end
  5008. else if ((TrySmaller = S_B) and not SmallerOverflow) then
  5009. begin
  5010. TargetSize := S_B;
  5011. TargetSubReg := R_SUBL;
  5012. end
  5013. else
  5014. Break;
  5015. S_BL:
  5016. if (MinSize in [S_B, S_W]) then
  5017. begin
  5018. TargetSize := S_L;
  5019. TargetSubReg := R_SUBD;
  5020. end
  5021. else if ((TrySmaller = S_B) and not SmallerOverflow) then
  5022. begin
  5023. TargetSize := S_B;
  5024. TargetSubReg := R_SUBL;
  5025. end
  5026. else
  5027. Break;
  5028. else
  5029. InternalError(2020112302);
  5030. end;
  5031. { Update the register to its new size }
  5032. ThisReg := newreg(R_INTREGISTER, getsupreg(ThisReg), TargetSubReg);
  5033. if TargetSize = MinSize then
  5034. begin
  5035. { Convert the input MOVZX to a MOV }
  5036. if (taicpu(p).oper[0]^.typ = top_reg) and
  5037. SuperRegistersEqual(taicpu(p).oper[0]^.reg, ThisReg) then
  5038. begin
  5039. { Or remove it completely! }
  5040. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 1', p);
  5041. RemoveCurrentP(p);
  5042. p_removed := True;
  5043. end
  5044. else
  5045. begin
  5046. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 1', p);
  5047. taicpu(p).opcode := A_MOV;
  5048. taicpu(p).oper[1]^.reg := ThisReg;
  5049. taicpu(p).opsize := TargetSize;
  5050. end;
  5051. Result := True;
  5052. end
  5053. else if TargetSize <> MaxSize then
  5054. begin
  5055. case MaxSize of
  5056. S_L:
  5057. if TargetSize = S_W then
  5058. begin
  5059. DebugMsg(SPeepholeOptimization + 'movzbl2movzbw', p);
  5060. taicpu(p).opsize := S_BW;
  5061. taicpu(p).oper[1]^.reg := ThisReg;
  5062. Result := True;
  5063. end
  5064. else
  5065. InternalError(2020112341);
  5066. S_W:
  5067. if TargetSize = S_L then
  5068. begin
  5069. DebugMsg(SPeepholeOptimization + 'movzbw2movzbl', p);
  5070. taicpu(p).opsize := S_BL;
  5071. taicpu(p).oper[1]^.reg := ThisReg;
  5072. Result := True;
  5073. end
  5074. else
  5075. InternalError(2020112342);
  5076. else
  5077. ;
  5078. end;
  5079. end;
  5080. if (MaxSize = TargetSize) or
  5081. ((TargetSize = S_L) and (taicpu(hp1).opsize in [S_L, S_BL, S_WL])) or
  5082. ((TargetSize = S_W) and (taicpu(hp1).opsize in [S_W, S_BW])) then
  5083. begin
  5084. { Convert the output MOVZX to a MOV }
  5085. if SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) then
  5086. begin
  5087. { Or remove it completely! }
  5088. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 2', hp1);
  5089. { Be careful; if p = hp1 and p was also removed, p
  5090. will become a dangling pointer }
  5091. if p = hp1 then
  5092. RemoveCurrentp(p) { p = hp1 and will then become the next instruction }
  5093. else
  5094. RemoveInstruction(hp1);
  5095. end
  5096. else
  5097. begin
  5098. taicpu(hp1).opcode := A_MOV;
  5099. taicpu(hp1).oper[0]^.reg := ThisReg;
  5100. taicpu(hp1).opsize := TargetSize;
  5101. { Check to see if the active register is used afterwards;
  5102. if not, we can change it and make a saving. }
  5103. RegInUse := False;
  5104. TransferUsedRegs(TmpUsedRegs);
  5105. { The target register may be marked as in use to cross
  5106. a jump to a distant label, so exclude it }
  5107. ExcludeRegFromUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs);
  5108. hp2 := p;
  5109. repeat
  5110. UpdateUsedRegs(TmpUsedRegs, tai(hp2.next));
  5111. { Explicitly check for the excluded register (don't include the first
  5112. instruction as it may be reading from here }
  5113. if ((p <> hp2) and (RegInInstruction(taicpu(hp1).oper[1]^.reg, hp2))) or
  5114. RegInUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs) then
  5115. begin
  5116. RegInUse := True;
  5117. Break;
  5118. end;
  5119. if not GetNextInstruction(hp2, hp2) then
  5120. InternalError(2020112340);
  5121. until (hp2 = hp1);
  5122. if not RegInUse and not RegUsedAfterInstruction(ThisReg, hp1, TmpUsedRegs) then
  5123. begin
  5124. DebugMsg(SPeepholeOptimization + 'Simplified register usage so ' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' = ' + debug_regname(taicpu(p).oper[1]^.reg), p);
  5125. ThisReg := taicpu(hp1).oper[1]^.reg;
  5126. RegChanged := True;
  5127. TransferUsedRegs(TmpUsedRegs);
  5128. AllocRegBetween(ThisReg, p, hp1, TmpUsedRegs);
  5129. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 3', hp1);
  5130. if p = hp1 then
  5131. RemoveCurrentp(p) { p = hp1 and will then become the next instruction }
  5132. else
  5133. RemoveInstruction(hp1);
  5134. { Instruction will become "mov %reg,%reg" }
  5135. if not p_removed and (taicpu(p).opcode = A_MOV) and
  5136. MatchOperand(taicpu(p).oper[0]^, ThisReg) then
  5137. begin
  5138. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 6', p);
  5139. RemoveCurrentP(p);
  5140. p_removed := True;
  5141. end
  5142. else
  5143. taicpu(p).oper[1]^.reg := ThisReg;
  5144. Result := True;
  5145. end
  5146. else
  5147. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 2', hp1);
  5148. end;
  5149. end
  5150. else
  5151. InternalError(2020112330);
  5152. { Now go through every instruction we found and change the
  5153. size. If TargetSize = MaxSize, then almost no changes are
  5154. needed and Result can remain False if it hasn't been set
  5155. yet.
  5156. If RegChanged is True, then the register requires changing
  5157. and so the point about TargetSize = MaxSize doesn't apply. }
  5158. if ((TargetSize <> MaxSize) or RegChanged) and (InstrMax >= 0) then
  5159. begin
  5160. for Index := 0 to InstrMax do
  5161. begin
  5162. { If p_removed is true, then the original MOV/Z was removed
  5163. and removing the AND instruction may not be safe if it
  5164. appears first }
  5165. if (InstrList[Index].oper[InstrList[Index].ops - 1]^.typ <> top_reg) then
  5166. InternalError(2020112310);
  5167. if InstrList[Index].oper[0]^.typ = top_reg then
  5168. InstrList[Index].oper[0]^.reg := ThisReg;
  5169. InstrList[Index].oper[InstrList[Index].ops - 1]^.reg := ThisReg;
  5170. InstrList[Index].opsize := TargetSize;
  5171. end;
  5172. Result := True;
  5173. end;
  5174. Exit;
  5175. end;
  5176. else
  5177. { This includes ADC, SBB, IDIV and SAR }
  5178. Break;
  5179. end;
  5180. if (TestValMin < 0) or (TestValMax < 0) or
  5181. (TestValMin > UpperLimit) or (TestValMax > UpperLimit) then
  5182. { Overflow }
  5183. Break
  5184. else if not SmallerOverflow and (TrySmaller <> S_NO) and
  5185. ((TestValMin > TrySmallerLimit) or (TestValMax > TrySmallerLimit)) then
  5186. SmallerOverflow := True;
  5187. { Contains highest index (so instruction count - 1) }
  5188. Inc(InstrMax);
  5189. if InstrMax > High(InstrList) then
  5190. SetLength(InstrList, InstrMax + LIST_STEP_SIZE);
  5191. InstrList[InstrMax] := taicpu(hp1);
  5192. end;
  5193. end;
  5194. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  5195. var
  5196. hp1 : tai;
  5197. begin
  5198. Result:=false;
  5199. if (taicpu(p).ops >= 2) and
  5200. ((taicpu(p).oper[0]^.typ = top_const) or
  5201. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  5202. (taicpu(p).oper[1]^.typ = top_reg) and
  5203. ((taicpu(p).ops = 2) or
  5204. ((taicpu(p).oper[2]^.typ = top_reg) and
  5205. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  5206. GetLastInstruction(p,hp1) and
  5207. MatchInstruction(hp1,A_MOV,[]) and
  5208. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  5209. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  5210. begin
  5211. TransferUsedRegs(TmpUsedRegs);
  5212. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) or
  5213. ((taicpu(p).ops = 3) and (taicpu(p).oper[1]^.reg=taicpu(p).oper[2]^.reg)) then
  5214. { change
  5215. mov reg1,reg2
  5216. imul y,reg2 to imul y,reg1,reg2 }
  5217. begin
  5218. taicpu(p).ops := 3;
  5219. taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
  5220. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  5221. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  5222. RemoveInstruction(hp1);
  5223. result:=true;
  5224. end;
  5225. end;
  5226. end;
  5227. procedure TX86AsmOptimizer.ConvertJumpToRET(const p: tai; const ret_p: tai);
  5228. var
  5229. ThisLabel: TAsmLabel;
  5230. begin
  5231. ThisLabel := tasmlabel(taicpu(p).oper[0]^.ref^.symbol);
  5232. ThisLabel.decrefs;
  5233. taicpu(p).opcode := A_RET;
  5234. taicpu(p).is_jmp := false;
  5235. taicpu(p).ops := taicpu(ret_p).ops;
  5236. case taicpu(ret_p).ops of
  5237. 0:
  5238. taicpu(p).clearop(0);
  5239. 1:
  5240. taicpu(p).loadconst(0,taicpu(ret_p).oper[0]^.val);
  5241. else
  5242. internalerror(2016041301);
  5243. end;
  5244. { If the original label is now dead, it might turn out that the label
  5245. immediately follows p. As a result, everything beyond it, which will
  5246. be just some final register configuration and a RET instruction, is
  5247. now dead code. [Kit] }
  5248. { NOTE: This is much faster than introducing a OptPass2RET routine and
  5249. running RemoveDeadCodeAfterJump for each RET instruction, because
  5250. this optimisation rarely happens and most RETs appear at the end of
  5251. routines where there is nothing that can be stripped. [Kit] }
  5252. if not ThisLabel.is_used then
  5253. RemoveDeadCodeAfterJump(p);
  5254. end;
  5255. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  5256. var
  5257. hp1, hp2, hp3: tai;
  5258. OperIdx: Integer;
  5259. begin
  5260. result:=false;
  5261. 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
  5262. (taicpu(p).oper[0]^.ref^.index=NR_NO) and (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) then
  5263. begin
  5264. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  5265. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and (hp1.typ = ait_instruction) then
  5266. begin
  5267. case taicpu(hp1).opcode of
  5268. A_RET:
  5269. {
  5270. change
  5271. jmp .L1
  5272. ...
  5273. .L1:
  5274. ret
  5275. into
  5276. ret
  5277. }
  5278. begin
  5279. ConvertJumpToRET(p, hp1);
  5280. result:=true;
  5281. end;
  5282. A_MOV:
  5283. {
  5284. change
  5285. jmp .L1
  5286. ...
  5287. .L1:
  5288. mov ##, ##
  5289. ret
  5290. into
  5291. mov ##, ##
  5292. ret
  5293. }
  5294. { This optimisation tends to increase code size if the pass 1 MOV optimisations aren't
  5295. re-run, so only do this particular optimisation if optimising for speed or when
  5296. optimisations are very in-depth. [Kit] }
  5297. if (current_settings.optimizerswitches * [cs_opt_level3, cs_opt_size]) <> [cs_opt_size] then
  5298. begin
  5299. GetNextInstruction(hp1, hp2);
  5300. if not Assigned(hp2) then
  5301. Exit;
  5302. if (hp2.typ in [ait_label, ait_align]) then
  5303. SkipLabels(hp2,hp2);
  5304. if Assigned(hp2) and MatchInstruction(hp2, A_RET, [S_NO]) then
  5305. begin
  5306. { Duplicate the MOV instruction }
  5307. hp3:=tai(hp1.getcopy);
  5308. asml.InsertBefore(hp3, p);
  5309. { Make sure the compiler knows about any final registers written here }
  5310. for OperIdx := 0 to 1 do
  5311. with taicpu(hp3).oper[OperIdx]^ do
  5312. begin
  5313. case typ of
  5314. top_ref:
  5315. begin
  5316. if (ref^.base <> NR_NO) {$ifdef x86_64} and (ref^.base <> NR_RIP) {$endif x86_64} then
  5317. AllocRegBetween(ref^.base, hp3, tai(p.Next), UsedRegs);
  5318. if (ref^.index <> NR_NO) {$ifdef x86_64} and (ref^.index <> NR_RIP) {$endif x86_64} then
  5319. AllocRegBetween(ref^.index, hp3, tai(p.Next), UsedRegs);
  5320. end;
  5321. top_reg:
  5322. AllocRegBetween(reg, hp3, tai(p.Next), UsedRegs);
  5323. else
  5324. ;
  5325. end;
  5326. end;
  5327. { Now change the jump into a RET instruction }
  5328. ConvertJumpToRET(p, hp2);
  5329. result:=true;
  5330. end;
  5331. end;
  5332. else
  5333. ;
  5334. end;
  5335. end;
  5336. end;
  5337. end;
  5338. class function TX86AsmOptimizer.CanBeCMOV(p : tai) : boolean;
  5339. begin
  5340. CanBeCMOV:=assigned(p) and
  5341. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  5342. { we can't use cmov ref,reg because
  5343. ref could be nil and cmov still throws an exception
  5344. if ref=nil but the mov isn't done (FK)
  5345. or ((taicpu(p).oper[0]^.typ = top_ref) and
  5346. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  5347. }
  5348. (taicpu(p).oper[1]^.typ = top_reg) and
  5349. (
  5350. (taicpu(p).oper[0]^.typ = top_reg) or
  5351. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  5352. it is not expected that this can cause a seg. violation }
  5353. (
  5354. (taicpu(p).oper[0]^.typ = top_ref) and
  5355. IsRefSafe(taicpu(p).oper[0]^.ref)
  5356. )
  5357. );
  5358. end;
  5359. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  5360. var
  5361. hp1,hp2: tai;
  5362. {$ifndef i8086}
  5363. hp3,hp4,hpmov2: tai;
  5364. l : Longint;
  5365. condition : TAsmCond;
  5366. {$endif i8086}
  5367. carryadd_opcode : TAsmOp;
  5368. symbol: TAsmSymbol;
  5369. reg: tsuperregister;
  5370. increg, tmpreg: TRegister;
  5371. begin
  5372. result:=false;
  5373. if GetNextInstruction(p,hp1) and (hp1.typ=ait_instruction) then
  5374. begin
  5375. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  5376. if GetNextInstruction(hp1,hp2) and
  5377. (
  5378. (hp2.typ=ait_label) or
  5379. { trick to skip align }
  5380. ((hp2.typ=ait_align) and GetNextInstruction(hp2,hp2) and (hp2.typ=ait_label))
  5381. ) and
  5382. (Tasmlabel(symbol) = Tai_label(hp2).labsym) and
  5383. (
  5384. (
  5385. ((Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB)) and
  5386. MatchOptype(Taicpu(hp1),top_const,top_reg) and
  5387. (Taicpu(hp1).oper[0]^.val=1)
  5388. ) or
  5389. ((Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC))
  5390. ) then
  5391. { jb @@1 cmc
  5392. inc/dec operand --> adc/sbb operand,0
  5393. @@1:
  5394. ... and ...
  5395. jnb @@1
  5396. inc/dec operand --> adc/sbb operand,0
  5397. @@1: }
  5398. begin
  5399. if Taicpu(p).condition in [C_NAE,C_B,C_C] then
  5400. begin
  5401. case taicpu(hp1).opcode of
  5402. A_INC,
  5403. A_ADD:
  5404. carryadd_opcode:=A_ADC;
  5405. A_DEC,
  5406. A_SUB:
  5407. carryadd_opcode:=A_SBB;
  5408. else
  5409. InternalError(2021011001);
  5410. end;
  5411. Taicpu(p).clearop(0);
  5412. Taicpu(p).ops:=0;
  5413. Taicpu(p).is_jmp:=false;
  5414. Taicpu(p).opcode:=A_CMC;
  5415. Taicpu(p).condition:=C_NONE;
  5416. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2CmcAdc/Sbb',p);
  5417. Taicpu(hp1).ops:=2;
  5418. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  5419. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  5420. else
  5421. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  5422. Taicpu(hp1).loadconst(0,0);
  5423. Taicpu(hp1).opcode:=carryadd_opcode;
  5424. result:=true;
  5425. exit;
  5426. end
  5427. else if Taicpu(p).condition in [C_AE,C_NB,C_NC] then
  5428. begin
  5429. case taicpu(hp1).opcode of
  5430. A_INC,
  5431. A_ADD:
  5432. carryadd_opcode:=A_ADC;
  5433. A_DEC,
  5434. A_SUB:
  5435. carryadd_opcode:=A_SBB;
  5436. else
  5437. InternalError(2021011002);
  5438. end;
  5439. Taicpu(hp1).ops:=2;
  5440. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2Adc/Sbb',p);
  5441. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  5442. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  5443. else
  5444. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  5445. Taicpu(hp1).loadconst(0,0);
  5446. Taicpu(hp1).opcode:=carryadd_opcode;
  5447. RemoveCurrentP(p, hp1);
  5448. result:=true;
  5449. exit;
  5450. end
  5451. {
  5452. jcc @@1 setcc tmpreg
  5453. inc/dec/add/sub operand -> (movzx tmpreg)
  5454. @@1: add/sub tmpreg,operand
  5455. While this increases code size slightly, it makes the code much faster if the
  5456. jump is unpredictable
  5457. }
  5458. else if not(cs_opt_size in current_settings.optimizerswitches) then
  5459. begin
  5460. { search for an available register which is volatile }
  5461. for reg in tcpuregisterset do
  5462. begin
  5463. if
  5464. {$if defined(i386) or defined(i8086)}
  5465. { Only use registers whose lowest 8-bits can Be accessed }
  5466. (reg in [RS_EAX,RS_EBX,RS_ECX,RS_EDX]) and
  5467. {$endif i386 or i8086}
  5468. (reg in paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption)) and
  5469. not(reg in UsedRegs[R_INTREGISTER].GetUsedRegs)
  5470. { We don't need to check if tmpreg is in hp1 or not, because
  5471. it will be marked as in use at p (if not, this is
  5472. indictive of a compiler bug). }
  5473. then
  5474. begin
  5475. TAsmLabel(symbol).decrefs;
  5476. increg := newreg(R_INTREGISTER,reg,R_SUBL);
  5477. Taicpu(p).clearop(0);
  5478. Taicpu(p).ops:=1;
  5479. Taicpu(p).is_jmp:=false;
  5480. Taicpu(p).opcode:=A_SETcc;
  5481. DebugMsg(SPeepholeOptimization+'JccAdd2SetccAdd',p);
  5482. Taicpu(p).condition:=inverse_cond(Taicpu(p).condition);
  5483. Taicpu(p).loadreg(0,increg);
  5484. if getsubreg(Taicpu(hp1).oper[1]^.reg)<>R_SUBL then
  5485. begin
  5486. case getsubreg(Taicpu(hp1).oper[1]^.reg) of
  5487. R_SUBW:
  5488. begin
  5489. tmpreg := newreg(R_INTREGISTER,reg,R_SUBW);
  5490. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BW,increg,tmpreg);
  5491. end;
  5492. R_SUBD:
  5493. begin
  5494. tmpreg := newreg(R_INTREGISTER,reg,R_SUBD);
  5495. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,tmpreg);
  5496. end;
  5497. {$ifdef x86_64}
  5498. R_SUBQ:
  5499. begin
  5500. { MOVZX doesn't have a 64-bit variant, because
  5501. the 32-bit version implicitly zeroes the
  5502. upper 32-bits of the destination register }
  5503. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,
  5504. newreg(R_INTREGISTER,reg,R_SUBD));
  5505. tmpreg := newreg(R_INTREGISTER,reg,R_SUBQ);
  5506. end;
  5507. {$endif x86_64}
  5508. else
  5509. Internalerror(2020030601);
  5510. end;
  5511. taicpu(hp2).fileinfo:=taicpu(hp1).fileinfo;
  5512. asml.InsertAfter(hp2,p);
  5513. end
  5514. else
  5515. tmpreg := increg;
  5516. if (Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC) then
  5517. begin
  5518. Taicpu(hp1).ops:=2;
  5519. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^)
  5520. end;
  5521. Taicpu(hp1).loadreg(0,tmpreg);
  5522. AllocRegBetween(tmpreg,p,hp1,UsedRegs);
  5523. Result := True;
  5524. { p is no longer a Jcc instruction, so exit }
  5525. Exit;
  5526. end;
  5527. end;
  5528. end;
  5529. end;
  5530. { Detect the following:
  5531. jmp<cond> @Lbl1
  5532. jmp @Lbl2
  5533. ...
  5534. @Lbl1:
  5535. ret
  5536. Change to:
  5537. jmp<inv_cond> @Lbl2
  5538. ret
  5539. }
  5540. if MatchInstruction(hp1,A_JMP,[]) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  5541. begin
  5542. hp2:=getlabelwithsym(TAsmLabel(symbol));
  5543. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  5544. MatchInstruction(hp2,A_RET,[S_NO]) then
  5545. begin
  5546. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  5547. { Change label address to that of the unconditional jump }
  5548. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  5549. TAsmLabel(symbol).DecRefs;
  5550. taicpu(hp1).opcode := A_RET;
  5551. taicpu(hp1).is_jmp := false;
  5552. taicpu(hp1).ops := taicpu(hp2).ops;
  5553. DebugMsg(SPeepholeOptimization+'JccJmpRet2J!ccRet',p);
  5554. case taicpu(hp2).ops of
  5555. 0:
  5556. taicpu(hp1).clearop(0);
  5557. 1:
  5558. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  5559. else
  5560. internalerror(2016041302);
  5561. end;
  5562. end;
  5563. {$ifndef i8086}
  5564. end
  5565. else if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  5566. begin
  5567. { check for
  5568. jCC xxx
  5569. <several movs>
  5570. xxx:
  5571. }
  5572. l:=0;
  5573. while assigned(hp1) and
  5574. CanBeCMOV(hp1) and
  5575. { stop on labels }
  5576. not(hp1.typ=ait_label) do
  5577. begin
  5578. inc(l);
  5579. GetNextInstruction(hp1,hp1);
  5580. end;
  5581. if assigned(hp1) then
  5582. begin
  5583. if FindLabel(tasmlabel(symbol),hp1) then
  5584. begin
  5585. if (l<=4) and (l>0) then
  5586. begin
  5587. condition:=inverse_cond(taicpu(p).condition);
  5588. GetNextInstruction(p,hp1);
  5589. repeat
  5590. if not Assigned(hp1) then
  5591. InternalError(2018062900);
  5592. taicpu(hp1).opcode:=A_CMOVcc;
  5593. taicpu(hp1).condition:=condition;
  5594. UpdateUsedRegs(hp1);
  5595. GetNextInstruction(hp1,hp1);
  5596. until not(CanBeCMOV(hp1));
  5597. { Remember what hp1 is in case there's multiple aligns to get rid of }
  5598. hp2 := hp1;
  5599. repeat
  5600. if not Assigned(hp2) then
  5601. InternalError(2018062910);
  5602. case hp2.typ of
  5603. ait_label:
  5604. { What we expected - break out of the loop (it won't be a dead label at the top of
  5605. a cluster because that was optimised at an earlier stage) }
  5606. Break;
  5607. ait_align:
  5608. { Go to the next entry until a label is found (may be multiple aligns before it) }
  5609. begin
  5610. hp2 := tai(hp2.Next);
  5611. Continue;
  5612. end;
  5613. else
  5614. begin
  5615. { Might be a comment or temporary allocation entry }
  5616. if not (hp2.typ in SkipInstr) then
  5617. InternalError(2018062911);
  5618. hp2 := tai(hp2.Next);
  5619. Continue;
  5620. end;
  5621. end;
  5622. until False;
  5623. { Now we can safely decrement the reference count }
  5624. tasmlabel(symbol).decrefs;
  5625. DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
  5626. { Remove the original jump }
  5627. RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
  5628. GetNextInstruction(hp2, p); { Instruction after the label }
  5629. { Remove the label if this is its final reference }
  5630. if (tasmlabel(symbol).getrefs=0) then
  5631. StripLabelFast(hp1);
  5632. if Assigned(p) then
  5633. begin
  5634. UpdateUsedRegs(p);
  5635. result:=true;
  5636. end;
  5637. exit;
  5638. end;
  5639. end
  5640. else
  5641. begin
  5642. { check further for
  5643. jCC xxx
  5644. <several movs 1>
  5645. jmp yyy
  5646. xxx:
  5647. <several movs 2>
  5648. yyy:
  5649. }
  5650. { hp2 points to jmp yyy }
  5651. hp2:=hp1;
  5652. { skip hp1 to xxx (or an align right before it) }
  5653. GetNextInstruction(hp1, hp1);
  5654. if assigned(hp2) and
  5655. assigned(hp1) and
  5656. (l<=3) and
  5657. (hp2.typ=ait_instruction) and
  5658. (taicpu(hp2).is_jmp) and
  5659. (taicpu(hp2).condition=C_None) and
  5660. { real label and jump, no further references to the
  5661. label are allowed }
  5662. (tasmlabel(symbol).getrefs=1) and
  5663. FindLabel(tasmlabel(symbol),hp1) then
  5664. begin
  5665. l:=0;
  5666. { skip hp1 to <several moves 2> }
  5667. if (hp1.typ = ait_align) then
  5668. GetNextInstruction(hp1, hp1);
  5669. GetNextInstruction(hp1, hpmov2);
  5670. hp1 := hpmov2;
  5671. while assigned(hp1) and
  5672. CanBeCMOV(hp1) do
  5673. begin
  5674. inc(l);
  5675. GetNextInstruction(hp1, hp1);
  5676. end;
  5677. { hp1 points to yyy (or an align right before it) }
  5678. hp3 := hp1;
  5679. if assigned(hp1) and
  5680. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  5681. begin
  5682. condition:=inverse_cond(taicpu(p).condition);
  5683. GetNextInstruction(p,hp1);
  5684. repeat
  5685. taicpu(hp1).opcode:=A_CMOVcc;
  5686. taicpu(hp1).condition:=condition;
  5687. UpdateUsedRegs(hp1);
  5688. GetNextInstruction(hp1,hp1);
  5689. until not(assigned(hp1)) or
  5690. not(CanBeCMOV(hp1));
  5691. condition:=inverse_cond(condition);
  5692. hp1 := hpmov2;
  5693. { hp1 is now at <several movs 2> }
  5694. while Assigned(hp1) and CanBeCMOV(hp1) do
  5695. begin
  5696. taicpu(hp1).opcode:=A_CMOVcc;
  5697. taicpu(hp1).condition:=condition;
  5698. UpdateUsedRegs(hp1);
  5699. GetNextInstruction(hp1,hp1);
  5700. end;
  5701. hp1 := p;
  5702. { Get first instruction after label }
  5703. GetNextInstruction(hp3, p);
  5704. if assigned(p) and (hp3.typ = ait_align) then
  5705. GetNextInstruction(p, p);
  5706. { Don't dereference yet, as doing so will cause
  5707. GetNextInstruction to skip the label and
  5708. optional align marker. [Kit] }
  5709. GetNextInstruction(hp2, hp4);
  5710. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
  5711. { remove jCC }
  5712. RemoveInstruction(hp1);
  5713. { Now we can safely decrement it }
  5714. tasmlabel(symbol).decrefs;
  5715. { Remove label xxx (it will have a ref of zero due to the initial check }
  5716. StripLabelFast(hp4);
  5717. { remove jmp }
  5718. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  5719. RemoveInstruction(hp2);
  5720. { As before, now we can safely decrement it }
  5721. tasmlabel(symbol).decrefs;
  5722. { Remove label yyy (and the optional alignment) if its reference falls to zero }
  5723. if tasmlabel(symbol).getrefs = 0 then
  5724. StripLabelFast(hp3);
  5725. if Assigned(p) then
  5726. begin
  5727. UpdateUsedRegs(p);
  5728. result:=true;
  5729. end;
  5730. exit;
  5731. end;
  5732. end;
  5733. end;
  5734. end;
  5735. {$endif i8086}
  5736. end;
  5737. end;
  5738. end;
  5739. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  5740. var
  5741. hp1,hp2: tai;
  5742. reg_and_hp1_is_instr: Boolean;
  5743. begin
  5744. result:=false;
  5745. reg_and_hp1_is_instr:=(taicpu(p).oper[1]^.typ = top_reg) and
  5746. GetNextInstruction(p,hp1) and
  5747. (hp1.typ = ait_instruction);
  5748. if reg_and_hp1_is_instr and
  5749. (
  5750. (taicpu(hp1).opcode <> A_LEA) or
  5751. { If the LEA instruction can be converted into an arithmetic instruction,
  5752. it may be possible to then fold it. }
  5753. (
  5754. { If the flags register is in use, don't change the instruction
  5755. to an ADD otherwise this will scramble the flags. [Kit] }
  5756. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  5757. ConvertLEA(taicpu(hp1))
  5758. )
  5759. ) and
  5760. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  5761. GetNextInstruction(hp1,hp2) and
  5762. MatchInstruction(hp2,A_MOV,[]) and
  5763. (taicpu(hp2).oper[0]^.typ = top_reg) and
  5764. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  5765. ((taicpu(p).opsize in [S_BW,S_BL]) and (taicpu(hp2).opsize=S_B) or
  5766. (taicpu(p).opsize in [S_WL]) and (taicpu(hp2).opsize=S_W)) and
  5767. {$ifdef i386}
  5768. { not all registers have byte size sub registers on i386 }
  5769. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  5770. {$endif i386}
  5771. (((taicpu(hp1).ops=2) and
  5772. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  5773. ((taicpu(hp1).ops=1) and
  5774. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  5775. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  5776. begin
  5777. { change movsX/movzX reg/ref, reg2
  5778. add/sub/or/... reg3/$const, reg2
  5779. mov reg2 reg/ref
  5780. to add/sub/or/... reg3/$const, reg/ref }
  5781. { by example:
  5782. movswl %si,%eax movswl %si,%eax p
  5783. decl %eax addl %edx,%eax hp1
  5784. movw %ax,%si movw %ax,%si hp2
  5785. ->
  5786. movswl %si,%eax movswl %si,%eax p
  5787. decw %eax addw %edx,%eax hp1
  5788. movw %ax,%si movw %ax,%si hp2
  5789. }
  5790. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  5791. {
  5792. ->
  5793. movswl %si,%eax movswl %si,%eax p
  5794. decw %si addw %dx,%si hp1
  5795. movw %ax,%si movw %ax,%si hp2
  5796. }
  5797. case taicpu(hp1).ops of
  5798. 1:
  5799. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  5800. 2:
  5801. begin
  5802. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  5803. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  5804. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  5805. end;
  5806. else
  5807. internalerror(2008042702);
  5808. end;
  5809. {
  5810. ->
  5811. decw %si addw %dx,%si p
  5812. }
  5813. DebugMsg(SPeepholeOptimization + 'var3',p);
  5814. RemoveCurrentP(p, hp1);
  5815. RemoveInstruction(hp2);
  5816. end
  5817. else if reg_and_hp1_is_instr and
  5818. (taicpu(hp1).opcode = A_MOV) and
  5819. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  5820. (MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^)
  5821. {$ifdef x86_64}
  5822. { check for implicit extension to 64 bit }
  5823. or
  5824. ((taicpu(p).opsize in [S_BL,S_WL]) and
  5825. (taicpu(hp1).opsize=S_Q) and
  5826. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg)
  5827. )
  5828. {$endif x86_64}
  5829. )
  5830. then
  5831. begin
  5832. { change
  5833. movx %reg1,%reg2
  5834. mov %reg2,%reg3
  5835. dealloc %reg2
  5836. into
  5837. movx %reg,%reg3
  5838. }
  5839. TransferUsedRegs(TmpUsedRegs);
  5840. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5841. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  5842. begin
  5843. DebugMsg(SPeepholeOptimization + 'MovxMov2Movx',p);
  5844. {$ifdef x86_64}
  5845. if (taicpu(p).opsize in [S_BL,S_WL]) and
  5846. (taicpu(hp1).opsize=S_Q) then
  5847. taicpu(p).loadreg(1,newreg(R_INTREGISTER,getsupreg(taicpu(hp1).oper[1]^.reg),R_SUBD))
  5848. else
  5849. {$endif x86_64}
  5850. taicpu(p).loadreg(1,taicpu(hp1).oper[1]^.reg);
  5851. RemoveInstruction(hp1);
  5852. end;
  5853. end
  5854. else if reg_and_hp1_is_instr and
  5855. (taicpu(hp1).opcode = A_MOV) and
  5856. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  5857. (((taicpu(p).opsize in [S_BW,S_BL,S_WL{$ifdef x86_64},S_BQ,S_WQ,S_LQ{$endif x86_64}]) and
  5858. (taicpu(hp1).opsize=S_B)) or
  5859. ((taicpu(p).opsize in [S_WL{$ifdef x86_64},S_WQ,S_LQ{$endif x86_64}]) and
  5860. (taicpu(hp1).opsize=S_W))
  5861. {$ifdef x86_64}
  5862. or ((taicpu(p).opsize=S_LQ) and
  5863. (taicpu(hp1).opsize=S_L))
  5864. {$endif x86_64}
  5865. ) and
  5866. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg) then
  5867. begin
  5868. { change
  5869. movx %reg1,%reg2
  5870. mov %reg2,%reg3
  5871. dealloc %reg2
  5872. into
  5873. mov %reg1,%reg3
  5874. if the second mov accesses only the bits stored in reg1
  5875. }
  5876. TransferUsedRegs(TmpUsedRegs);
  5877. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5878. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  5879. begin
  5880. DebugMsg(SPeepholeOptimization + 'MovxMov2Mov',p);
  5881. if taicpu(p).oper[0]^.typ=top_reg then
  5882. begin
  5883. case taicpu(hp1).opsize of
  5884. S_B:
  5885. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBL));
  5886. S_W:
  5887. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBW));
  5888. S_L:
  5889. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBD));
  5890. else
  5891. Internalerror(2020102301);
  5892. end;
  5893. AllocRegBetween(taicpu(hp1).oper[0]^.reg,p,hp1,UsedRegs);
  5894. end
  5895. else
  5896. taicpu(hp1).loadref(0,taicpu(p).oper[0]^.ref^);
  5897. RemoveCurrentP(p);
  5898. result:=true;
  5899. exit;
  5900. end;
  5901. end
  5902. else if reg_and_hp1_is_instr and
  5903. (taicpu(p).oper[0]^.typ = top_reg) and
  5904. (
  5905. (taicpu(hp1).opcode = A_SHL) or (taicpu(hp1).opcode = A_SAL)
  5906. ) and
  5907. (taicpu(hp1).oper[0]^.typ = top_const) and
  5908. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  5909. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  5910. { Minimum shift value allowed is the bit difference between the sizes }
  5911. (taicpu(hp1).oper[0]^.val >=
  5912. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  5913. 8 * (
  5914. tcgsize2size[reg_cgsize(taicpu(p).oper[1]^.reg)] -
  5915. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  5916. )
  5917. ) then
  5918. begin
  5919. { For:
  5920. movsx/movzx %reg1,%reg1 (same register, just different sizes)
  5921. shl/sal ##, %reg1
  5922. Remove the movsx/movzx instruction if the shift overwrites the
  5923. extended bits of the register (e.g. movslq %eax,%rax; shlq $32,%rax
  5924. }
  5925. DebugMsg(SPeepholeOptimization + 'MovxShl2Shl',p);
  5926. RemoveCurrentP(p, hp1);
  5927. Result := True;
  5928. Exit;
  5929. end
  5930. else if reg_and_hp1_is_instr and
  5931. (taicpu(p).oper[0]^.typ = top_reg) and
  5932. (
  5933. ((taicpu(hp1).opcode = A_SHR) and (taicpu(p).opcode = A_MOVZX)) or
  5934. ((taicpu(hp1).opcode = A_SAR) and (taicpu(p).opcode <> A_MOVZX))
  5935. ) and
  5936. (taicpu(hp1).oper[0]^.typ = top_const) and
  5937. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  5938. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  5939. { Minimum shift value allowed is the bit size of the smallest register - 1 }
  5940. (taicpu(hp1).oper[0]^.val <
  5941. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  5942. 8 * (
  5943. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  5944. )
  5945. ) then
  5946. begin
  5947. { For:
  5948. movsx %reg1,%reg1 movzx %reg1,%reg1 (same register, just different sizes)
  5949. sar ##, %reg1 shr ##, %reg1
  5950. Move the shift to before the movx instruction if the shift value
  5951. is not too large.
  5952. }
  5953. asml.Remove(hp1);
  5954. asml.InsertBefore(hp1, p);
  5955. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  5956. case taicpu(p).opsize of
  5957. s_BW, S_BL{$ifdef x86_64}, S_BQ{$endif}:
  5958. taicpu(hp1).opsize := S_B;
  5959. S_WL{$ifdef x86_64}, S_WQ{$endif}:
  5960. taicpu(hp1).opsize := S_W;
  5961. {$ifdef x86_64}
  5962. S_LQ:
  5963. taicpu(hp1).opsize := S_L;
  5964. {$endif}
  5965. else
  5966. InternalError(2020112401);
  5967. end;
  5968. if (taicpu(hp1).opcode = A_SHR) then
  5969. DebugMsg(SPeepholeOptimization + 'MovzShr2ShrMovz', hp1)
  5970. else
  5971. DebugMsg(SPeepholeOptimization + 'MovsSar2SarMovs', hp1);
  5972. Result := True;
  5973. end
  5974. else if taicpu(p).opcode=A_MOVZX then
  5975. begin
  5976. { removes superfluous And's after movzx's }
  5977. if reg_and_hp1_is_instr and
  5978. (taicpu(hp1).opcode = A_AND) and
  5979. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5980. ((taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)
  5981. {$ifdef x86_64}
  5982. { check for implicit extension to 64 bit }
  5983. or
  5984. ((taicpu(p).opsize in [S_BL,S_WL]) and
  5985. (taicpu(hp1).opsize=S_Q) and
  5986. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg)
  5987. )
  5988. {$endif x86_64}
  5989. )
  5990. then
  5991. begin
  5992. case taicpu(p).opsize Of
  5993. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  5994. if (taicpu(hp1).oper[0]^.val = $ff) then
  5995. begin
  5996. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz1',p);
  5997. RemoveInstruction(hp1);
  5998. Result:=true;
  5999. exit;
  6000. end;
  6001. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  6002. if (taicpu(hp1).oper[0]^.val = $ffff) then
  6003. begin
  6004. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz2',p);
  6005. RemoveInstruction(hp1);
  6006. Result:=true;
  6007. exit;
  6008. end;
  6009. {$ifdef x86_64}
  6010. S_LQ:
  6011. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  6012. begin
  6013. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz3',p);
  6014. RemoveInstruction(hp1);
  6015. Result:=true;
  6016. exit;
  6017. end;
  6018. {$endif x86_64}
  6019. else
  6020. ;
  6021. end;
  6022. { we cannot get rid of the and, but can we get rid of the movz ?}
  6023. if SuperRegistersEqual(taicpu(p).oper[0]^.reg,taicpu(p).oper[1]^.reg) then
  6024. begin
  6025. case taicpu(p).opsize Of
  6026. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  6027. if (taicpu(hp1).oper[0]^.val and $ff)=taicpu(hp1).oper[0]^.val then
  6028. begin
  6029. DebugMsg(SPeepholeOptimization + 'MovzAnd2And1',p);
  6030. RemoveCurrentP(p,hp1);
  6031. Result:=true;
  6032. exit;
  6033. end;
  6034. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  6035. if (taicpu(hp1).oper[0]^.val and $ffff)=taicpu(hp1).oper[0]^.val then
  6036. begin
  6037. DebugMsg(SPeepholeOptimization + 'MovzAnd2And2',p);
  6038. RemoveCurrentP(p,hp1);
  6039. Result:=true;
  6040. exit;
  6041. end;
  6042. {$ifdef x86_64}
  6043. S_LQ:
  6044. if (taicpu(hp1).oper[0]^.val and $ffffffff)=taicpu(hp1).oper[0]^.val then
  6045. begin
  6046. DebugMsg(SPeepholeOptimization + 'MovzAnd2And3',p);
  6047. RemoveCurrentP(p,hp1);
  6048. Result:=true;
  6049. exit;
  6050. end;
  6051. {$endif x86_64}
  6052. else
  6053. ;
  6054. end;
  6055. end;
  6056. end;
  6057. { changes some movzx constructs to faster synonyms (all examples
  6058. are given with eax/ax, but are also valid for other registers)}
  6059. if MatchOpType(taicpu(p),top_reg,top_reg) then
  6060. begin
  6061. case taicpu(p).opsize of
  6062. { Technically, movzbw %al,%ax cannot be encoded in 32/64-bit mode
  6063. (the machine code is equivalent to movzbl %al,%eax), but the
  6064. code generator still generates that assembler instruction and
  6065. it is silently converted. This should probably be checked.
  6066. [Kit] }
  6067. S_BW:
  6068. begin
  6069. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  6070. (
  6071. not IsMOVZXAcceptable
  6072. { and $0xff,%ax has a smaller encoding but risks a partial write penalty }
  6073. or (
  6074. (cs_opt_size in current_settings.optimizerswitches) and
  6075. (taicpu(p).oper[1]^.reg = NR_AX)
  6076. )
  6077. ) then
  6078. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  6079. begin
  6080. DebugMsg(SPeepholeOptimization + 'var7',p);
  6081. taicpu(p).opcode := A_AND;
  6082. taicpu(p).changeopsize(S_W);
  6083. taicpu(p).loadConst(0,$ff);
  6084. Result := True;
  6085. end
  6086. else if not IsMOVZXAcceptable and
  6087. GetNextInstruction(p, hp1) and
  6088. (tai(hp1).typ = ait_instruction) and
  6089. (taicpu(hp1).opcode = A_AND) and
  6090. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6091. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6092. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  6093. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  6094. begin
  6095. DebugMsg(SPeepholeOptimization + 'var8',p);
  6096. taicpu(p).opcode := A_MOV;
  6097. taicpu(p).changeopsize(S_W);
  6098. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  6099. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6100. Result := True;
  6101. end;
  6102. end;
  6103. {$ifndef i8086} { movzbl %al,%eax cannot be encoded in 16-bit mode (the machine code is equivalent to movzbw %al,%ax }
  6104. S_BL:
  6105. begin
  6106. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  6107. (
  6108. not IsMOVZXAcceptable
  6109. { and $0xff,%eax has a smaller encoding but risks a partial write penalty }
  6110. or (
  6111. (cs_opt_size in current_settings.optimizerswitches) and
  6112. (taicpu(p).oper[1]^.reg = NR_EAX)
  6113. )
  6114. ) then
  6115. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  6116. begin
  6117. DebugMsg(SPeepholeOptimization + 'var9',p);
  6118. taicpu(p).opcode := A_AND;
  6119. taicpu(p).changeopsize(S_L);
  6120. taicpu(p).loadConst(0,$ff);
  6121. Result := True;
  6122. end
  6123. else if not IsMOVZXAcceptable and
  6124. GetNextInstruction(p, hp1) and
  6125. (tai(hp1).typ = ait_instruction) and
  6126. (taicpu(hp1).opcode = A_AND) and
  6127. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6128. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6129. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  6130. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  6131. begin
  6132. DebugMsg(SPeepholeOptimization + 'var10',p);
  6133. taicpu(p).opcode := A_MOV;
  6134. taicpu(p).changeopsize(S_L);
  6135. { do not use R_SUBWHOLE
  6136. as movl %rdx,%eax
  6137. is invalid in assembler PM }
  6138. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  6139. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6140. Result := True;
  6141. end;
  6142. end;
  6143. {$endif i8086}
  6144. S_WL:
  6145. if not IsMOVZXAcceptable then
  6146. begin
  6147. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) then
  6148. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  6149. begin
  6150. DebugMsg(SPeepholeOptimization + 'var11',p);
  6151. taicpu(p).opcode := A_AND;
  6152. taicpu(p).changeopsize(S_L);
  6153. taicpu(p).loadConst(0,$ffff);
  6154. Result := True;
  6155. end
  6156. else if GetNextInstruction(p, hp1) and
  6157. (tai(hp1).typ = ait_instruction) and
  6158. (taicpu(hp1).opcode = A_AND) and
  6159. (taicpu(hp1).oper[0]^.typ = top_const) and
  6160. (taicpu(hp1).oper[1]^.typ = top_reg) and
  6161. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6162. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  6163. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  6164. begin
  6165. DebugMsg(SPeepholeOptimization + 'var12',p);
  6166. taicpu(p).opcode := A_MOV;
  6167. taicpu(p).changeopsize(S_L);
  6168. { do not use R_SUBWHOLE
  6169. as movl %rdx,%eax
  6170. is invalid in assembler PM }
  6171. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  6172. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  6173. Result := True;
  6174. end;
  6175. end;
  6176. else
  6177. InternalError(2017050705);
  6178. end;
  6179. end
  6180. else if not IsMOVZXAcceptable and (taicpu(p).oper[0]^.typ = top_ref) then
  6181. begin
  6182. if GetNextInstruction(p, hp1) and
  6183. (tai(hp1).typ = ait_instruction) and
  6184. (taicpu(hp1).opcode = A_AND) and
  6185. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6186. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6187. begin
  6188. //taicpu(p).opcode := A_MOV;
  6189. case taicpu(p).opsize Of
  6190. S_BL:
  6191. begin
  6192. DebugMsg(SPeepholeOptimization + 'var13',p);
  6193. taicpu(hp1).changeopsize(S_L);
  6194. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6195. end;
  6196. S_WL:
  6197. begin
  6198. DebugMsg(SPeepholeOptimization + 'var14',p);
  6199. taicpu(hp1).changeopsize(S_L);
  6200. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  6201. end;
  6202. S_BW:
  6203. begin
  6204. DebugMsg(SPeepholeOptimization + 'var15',p);
  6205. taicpu(hp1).changeopsize(S_W);
  6206. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6207. end;
  6208. else
  6209. Internalerror(2017050704)
  6210. end;
  6211. Result := True;
  6212. end;
  6213. end;
  6214. end;
  6215. end;
  6216. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  6217. var
  6218. hp1, hp2 : tai;
  6219. MaskLength : Cardinal;
  6220. MaskedBits : TCgInt;
  6221. begin
  6222. Result:=false;
  6223. { There are no optimisations for reference targets }
  6224. if (taicpu(p).oper[1]^.typ <> top_reg) then
  6225. Exit;
  6226. while GetNextInstruction(p, hp1) and
  6227. (hp1.typ = ait_instruction) do
  6228. begin
  6229. if (taicpu(p).oper[0]^.typ = top_const) then
  6230. begin
  6231. if (taicpu(hp1).opcode = A_AND) and
  6232. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6233. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  6234. { the second register must contain the first one, so compare their subreg types }
  6235. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  6236. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  6237. { change
  6238. and const1, reg
  6239. and const2, reg
  6240. to
  6241. and (const1 and const2), reg
  6242. }
  6243. begin
  6244. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  6245. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  6246. RemoveCurrentP(p, hp1);
  6247. Result:=true;
  6248. exit;
  6249. end
  6250. else if (taicpu(hp1).opcode = A_MOVZX) and
  6251. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  6252. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg) and
  6253. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  6254. (((taicpu(p).opsize=S_W) and
  6255. (taicpu(hp1).opsize=S_BW)) or
  6256. ((taicpu(p).opsize=S_L) and
  6257. (taicpu(hp1).opsize in [S_WL,S_BL{$ifdef x86_64},S_BQ,S_WQ{$endif x86_64}]))
  6258. {$ifdef x86_64}
  6259. or
  6260. ((taicpu(p).opsize=S_Q) and
  6261. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_BL,S_WL]))
  6262. {$endif x86_64}
  6263. ) then
  6264. begin
  6265. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  6266. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  6267. ) or
  6268. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  6269. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  6270. then
  6271. begin
  6272. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  6273. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  6274. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  6275. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  6276. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  6277. }
  6278. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  6279. RemoveInstruction(hp1);
  6280. { See if there are other optimisations possible }
  6281. Continue;
  6282. end;
  6283. end
  6284. else if (taicpu(hp1).opcode = A_SHL) and
  6285. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6286. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  6287. begin
  6288. {$ifopt R+}
  6289. {$define RANGE_WAS_ON}
  6290. {$R-}
  6291. {$endif}
  6292. { get length of potential and mask }
  6293. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  6294. { really a mask? }
  6295. {$ifdef RANGE_WAS_ON}
  6296. {$R+}
  6297. {$endif}
  6298. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  6299. { unmasked part shifted out? }
  6300. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  6301. begin
  6302. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  6303. RemoveCurrentP(p, hp1);
  6304. Result:=true;
  6305. exit;
  6306. end;
  6307. end
  6308. else if (taicpu(hp1).opcode = A_SHR) and
  6309. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6310. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
  6311. (taicpu(hp1).oper[0]^.val <= 63) then
  6312. begin
  6313. { Does SHR combined with the AND cover all the bits?
  6314. e.g. for "andb $252,%reg; shrb $2,%reg" - the "and" can be removed }
  6315. MaskedBits := taicpu(p).oper[0]^.val or ((TCgInt(1) shl taicpu(hp1).oper[0]^.val) - 1);
  6316. if ((taicpu(p).opsize = S_B) and ((MaskedBits and $FF) = $FF)) or
  6317. ((taicpu(p).opsize = S_W) and ((MaskedBits and $FFFF) = $FFFF)) or
  6318. ((taicpu(p).opsize = S_L) and ((MaskedBits and $FFFFFFFF) = $FFFFFFFF)) then
  6319. begin
  6320. DebugMsg(SPeepholeOptimization + 'AndShrToShr done', p);
  6321. RemoveCurrentP(p, hp1);
  6322. Result := True;
  6323. Exit;
  6324. end;
  6325. end
  6326. else if ((taicpu(hp1).opcode = A_MOVSX){$ifdef x86_64} or (taicpu(hp1).opcode = A_MOVSXD){$endif x86_64}) and
  6327. (taicpu(hp1).oper[0]^.typ = top_reg) and
  6328. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg) then
  6329. begin
  6330. if SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  6331. (
  6332. (
  6333. (taicpu(hp1).opsize in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  6334. ((taicpu(p).oper[0]^.val and $7F) = taicpu(p).oper[0]^.val)
  6335. ) or (
  6336. (taicpu(hp1).opsize in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  6337. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val)
  6338. {$ifdef x86_64}
  6339. ) or (
  6340. (taicpu(hp1).opsize = S_LQ) and
  6341. ((taicpu(p).oper[0]^.val and $7fffffff) = taicpu(p).oper[0]^.val)
  6342. {$endif x86_64}
  6343. )
  6344. ) then
  6345. begin
  6346. if (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg){$ifdef x86_64} or (taicpu(hp1).opsize = S_LQ){$endif x86_64} then
  6347. begin
  6348. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  6349. RemoveInstruction(hp1);
  6350. { See if there are other optimisations possible }
  6351. Continue;
  6352. end;
  6353. { The super-registers are the same though.
  6354. Note that this change by itself doesn't improve
  6355. code speed, but it opens up other optimisations. }
  6356. {$ifdef x86_64}
  6357. { Convert 64-bit register to 32-bit }
  6358. case taicpu(hp1).opsize of
  6359. S_BQ:
  6360. begin
  6361. taicpu(hp1).opsize := S_BL;
  6362. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  6363. end;
  6364. S_WQ:
  6365. begin
  6366. taicpu(hp1).opsize := S_WL;
  6367. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  6368. end
  6369. else
  6370. ;
  6371. end;
  6372. {$endif x86_64}
  6373. DebugMsg(SPeepholeOptimization + 'AndMovsxToAndMovzx', hp1);
  6374. taicpu(hp1).opcode := A_MOVZX;
  6375. { See if there are other optimisations possible }
  6376. Continue;
  6377. end;
  6378. end;
  6379. end;
  6380. if (taicpu(hp1).is_jmp) and
  6381. (taicpu(hp1).opcode<>A_JMP) and
  6382. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  6383. begin
  6384. { change
  6385. and x, reg
  6386. jxx
  6387. to
  6388. test x, reg
  6389. jxx
  6390. if reg is deallocated before the
  6391. jump, but only if it's a conditional jump (PFV)
  6392. }
  6393. taicpu(p).opcode := A_TEST;
  6394. Exit;
  6395. end;
  6396. Break;
  6397. end;
  6398. { Lone AND tests }
  6399. if (taicpu(p).oper[0]^.typ = top_const) then
  6400. begin
  6401. {
  6402. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  6403. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  6404. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  6405. }
  6406. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  6407. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  6408. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  6409. begin
  6410. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg);
  6411. if taicpu(p).opsize = S_L then
  6412. begin
  6413. Include(OptsToCheck,aoc_MovAnd2Mov_3);
  6414. Result := True;
  6415. end;
  6416. end;
  6417. end;
  6418. { Backward check to determine necessity of and %reg,%reg }
  6419. if (taicpu(p).oper[0]^.typ = top_reg) and
  6420. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  6421. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  6422. GetLastInstruction(p, hp2) and
  6423. RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp2) and
  6424. { Check size of adjacent instruction to determine if the AND is
  6425. effectively a null operation }
  6426. (
  6427. (taicpu(p).opsize = taicpu(hp2).opsize) or
  6428. { Note: Don't include S_Q }
  6429. ((taicpu(p).opsize = S_L) and (taicpu(hp2).opsize in [S_BL, S_WL])) or
  6430. ((taicpu(p).opsize = S_W) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_L])) or
  6431. ((taicpu(p).opsize = S_B) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_W, S_L]))
  6432. ) then
  6433. begin
  6434. DebugMsg(SPeepholeOptimization + 'And2Nop', p);
  6435. { If GetNextInstruction returned False, hp1 will be nil }
  6436. RemoveCurrentP(p, hp1);
  6437. Result := True;
  6438. Exit;
  6439. end;
  6440. end;
  6441. function TX86AsmOptimizer.OptPass2ADD(var p : tai) : boolean;
  6442. var
  6443. hp1: tai;
  6444. { This entire nested function is used in an if-statement below, but we
  6445. want to avoid all the used reg transfers and GetNextInstruction calls
  6446. until we really have to check }
  6447. function MemRegisterNotUsedLater: Boolean; inline;
  6448. var
  6449. hp2: tai;
  6450. begin
  6451. TransferUsedRegs(TmpUsedRegs);
  6452. hp2 := p;
  6453. repeat
  6454. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  6455. until not GetNextInstruction(hp2, hp2) or (hp2 = hp1);
  6456. Result := not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs);
  6457. end;
  6458. begin
  6459. Result := False;
  6460. { Change:
  6461. add %reg2,%reg1
  6462. mov/s/z #(%reg1),%reg1 (%reg1 superregisters must be the same)
  6463. To:
  6464. mov/s/z #(%reg1,%reg2),%reg1
  6465. }
  6466. if (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif}]) and
  6467. MatchOpType(taicpu(p), top_reg, top_reg) and
  6468. GetNextInstruction(p, hp1) and
  6469. MatchInstruction(hp1, [A_MOV, A_MOVZX, A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif}], []) and
  6470. MatchOpType(taicpu(hp1), top_ref, top_reg) and
  6471. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1) and
  6472. (
  6473. (
  6474. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  6475. (taicpu(hp1).oper[0]^.ref^.index = NR_NO)
  6476. ) or (
  6477. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  6478. (taicpu(hp1).oper[0]^.ref^.base = NR_NO)
  6479. )
  6480. ) and (
  6481. Reg1WriteOverwritesReg2Entirely(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) or
  6482. (
  6483. { If the super registers ARE equal, then this MOV/S/Z does a partial write }
  6484. not SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  6485. MemRegisterNotUsedLater
  6486. )
  6487. ) then
  6488. begin
  6489. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[1]^.reg;
  6490. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  6491. DebugMsg(SPeepholeOptimization + 'AddMov2Mov done', p);
  6492. RemoveCurrentp(p, hp1);
  6493. Result := True;
  6494. Exit;
  6495. end;
  6496. end;
  6497. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  6498. begin
  6499. Result:=false;
  6500. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  6501. begin
  6502. if MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  6503. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  6504. begin
  6505. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  6506. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  6507. taicpu(p).opcode:=A_ADD;
  6508. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  6509. result:=true;
  6510. end
  6511. else if MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  6512. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  6513. begin
  6514. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  6515. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  6516. taicpu(p).opcode:=A_ADD;
  6517. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  6518. result:=true;
  6519. end;
  6520. end;
  6521. end;
  6522. function TX86AsmOptimizer.OptPass2SUB(var p: tai): Boolean;
  6523. var
  6524. hp1: tai; NewRef: TReference;
  6525. begin
  6526. { Change:
  6527. subl/q $x,%reg1
  6528. movl/q %reg1,%reg2
  6529. To:
  6530. leal/q $-x(%reg1),%reg2
  6531. subl/q $x,%reg1
  6532. Breaks the dependency chain and potentially permits the removal of
  6533. a CMP instruction if one follows.
  6534. }
  6535. Result := False;
  6536. if not (cs_opt_size in current_settings.optimizerswitches) and
  6537. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  6538. MatchOpType(taicpu(p),top_const,top_reg) and
  6539. GetNextInstruction(p, hp1) and
  6540. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  6541. (taicpu(hp1).oper[1]^.typ = top_reg) and
  6542. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) then
  6543. begin
  6544. { Change the MOV instruction to a LEA instruction, and update the
  6545. first operand }
  6546. reference_reset(NewRef, 1, []);
  6547. NewRef.base := taicpu(p).oper[1]^.reg;
  6548. NewRef.scalefactor := 1;
  6549. NewRef.offset := -taicpu(p).oper[0]^.val;
  6550. taicpu(hp1).opcode := A_LEA;
  6551. taicpu(hp1).loadref(0, NewRef);
  6552. { Move what is now the LEA instruction to before the SUB instruction }
  6553. Asml.Remove(hp1);
  6554. Asml.InsertBefore(hp1, p);
  6555. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  6556. DebugMsg(SPeepholeOptimization + 'SubMov2LeaSub', p);
  6557. Result := True;
  6558. end;
  6559. end;
  6560. function TX86AsmOptimizer.SkipSimpleInstructions(var hp1 : tai) : Boolean;
  6561. begin
  6562. { we can skip all instructions not messing with the stack pointer }
  6563. while assigned(hp1) and {MatchInstruction(hp1,[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  6564. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  6565. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  6566. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  6567. ({(taicpu(hp1).ops=0) or }
  6568. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  6569. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  6570. ) and }
  6571. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  6572. )
  6573. ) do
  6574. GetNextInstruction(hp1,hp1);
  6575. Result:=assigned(hp1);
  6576. end;
  6577. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  6578. var
  6579. hp1, hp2, hp3, hp4, hp5: tai;
  6580. begin
  6581. Result:=false;
  6582. hp5:=nil;
  6583. { replace
  6584. leal(q) x(<stackpointer>),<stackpointer>
  6585. call procname
  6586. leal(q) -x(<stackpointer>),<stackpointer>
  6587. ret
  6588. by
  6589. jmp procname
  6590. but do it only on level 4 because it destroys stack back traces
  6591. }
  6592. if (cs_opt_level4 in current_settings.optimizerswitches) and
  6593. MatchOpType(taicpu(p),top_ref,top_reg) and
  6594. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  6595. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  6596. { the -8 or -24 are not required, but bail out early if possible,
  6597. higher values are unlikely }
  6598. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  6599. (taicpu(p).oper[0]^.ref^.offset=-24)) and
  6600. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  6601. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  6602. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  6603. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  6604. GetNextInstruction(p, hp1) and
  6605. { Take a copy of hp1 }
  6606. SetAndTest(hp1, hp4) and
  6607. { trick to skip label }
  6608. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  6609. SkipSimpleInstructions(hp1) and
  6610. MatchInstruction(hp1,A_CALL,[S_NO]) and
  6611. GetNextInstruction(hp1, hp2) and
  6612. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  6613. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  6614. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  6615. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  6616. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  6617. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  6618. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  6619. (taicpu(hp2).oper[0]^.ref^.segment=NR_NO) and
  6620. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  6621. GetNextInstruction(hp2, hp3) and
  6622. { trick to skip label }
  6623. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  6624. (MatchInstruction(hp3,A_RET,[S_NO]) or
  6625. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  6626. SetAndTest(hp3,hp5) and
  6627. GetNextInstruction(hp3,hp3) and
  6628. MatchInstruction(hp3,A_RET,[S_NO])
  6629. )
  6630. ) and
  6631. (taicpu(hp3).ops=0) then
  6632. begin
  6633. taicpu(hp1).opcode := A_JMP;
  6634. taicpu(hp1).is_jmp := true;
  6635. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  6636. RemoveCurrentP(p, hp4);
  6637. RemoveInstruction(hp2);
  6638. RemoveInstruction(hp3);
  6639. if Assigned(hp5) then
  6640. begin
  6641. AsmL.Remove(hp5);
  6642. ASmL.InsertBefore(hp5,hp1)
  6643. end;
  6644. Result:=true;
  6645. end;
  6646. end;
  6647. function TX86AsmOptimizer.PostPeepholeOptPush(var p : tai) : Boolean;
  6648. {$ifdef x86_64}
  6649. var
  6650. hp1, hp2, hp3, hp4, hp5: tai;
  6651. {$endif x86_64}
  6652. begin
  6653. Result:=false;
  6654. {$ifdef x86_64}
  6655. hp5:=nil;
  6656. { replace
  6657. push %rax
  6658. call procname
  6659. pop %rcx
  6660. ret
  6661. by
  6662. jmp procname
  6663. but do it only on level 4 because it destroys stack back traces
  6664. It depends on the fact, that the sequence push rax/pop rcx is used for stack alignment as rcx is volatile
  6665. for all supported calling conventions
  6666. }
  6667. if (cs_opt_level4 in current_settings.optimizerswitches) and
  6668. MatchOpType(taicpu(p),top_reg) and
  6669. (taicpu(p).oper[0]^.reg=NR_RAX) and
  6670. GetNextInstruction(p, hp1) and
  6671. { Take a copy of hp1 }
  6672. SetAndTest(hp1, hp4) and
  6673. { trick to skip label }
  6674. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  6675. SkipSimpleInstructions(hp1) and
  6676. MatchInstruction(hp1,A_CALL,[S_NO]) and
  6677. GetNextInstruction(hp1, hp2) and
  6678. MatchInstruction(hp2,A_POP,[taicpu(p).opsize]) and
  6679. MatchOpType(taicpu(hp2),top_reg) and
  6680. (taicpu(hp2).oper[0]^.reg=NR_RCX) and
  6681. GetNextInstruction(hp2, hp3) and
  6682. { trick to skip label }
  6683. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  6684. (MatchInstruction(hp3,A_RET,[S_NO]) or
  6685. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  6686. SetAndTest(hp3,hp5) and
  6687. GetNextInstruction(hp3,hp3) and
  6688. MatchInstruction(hp3,A_RET,[S_NO])
  6689. )
  6690. ) and
  6691. (taicpu(hp3).ops=0) then
  6692. begin
  6693. taicpu(hp1).opcode := A_JMP;
  6694. taicpu(hp1).is_jmp := true;
  6695. DebugMsg(SPeepholeOptimization + 'PushCallPushRet2Jmp done',p);
  6696. RemoveCurrentP(p, hp4);
  6697. RemoveInstruction(hp2);
  6698. RemoveInstruction(hp3);
  6699. if Assigned(hp5) then
  6700. begin
  6701. AsmL.Remove(hp5);
  6702. ASmL.InsertBefore(hp5,hp1)
  6703. end;
  6704. Result:=true;
  6705. end;
  6706. {$endif x86_64}
  6707. end;
  6708. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  6709. var
  6710. Value, RegName: string;
  6711. begin
  6712. Result:=false;
  6713. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  6714. begin
  6715. case taicpu(p).oper[0]^.val of
  6716. 0:
  6717. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  6718. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  6719. begin
  6720. { change "mov $0,%reg" into "xor %reg,%reg" }
  6721. taicpu(p).opcode := A_XOR;
  6722. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  6723. Result := True;
  6724. end;
  6725. $1..$FFFFFFFF:
  6726. begin
  6727. { Code size reduction by J. Gareth "Kit" Moreton }
  6728. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  6729. case taicpu(p).opsize of
  6730. S_Q:
  6731. begin
  6732. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  6733. Value := debug_tostr(taicpu(p).oper[0]^.val);
  6734. { The actual optimization }
  6735. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  6736. taicpu(p).changeopsize(S_L);
  6737. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  6738. Result := True;
  6739. end;
  6740. else
  6741. { Do nothing };
  6742. end;
  6743. end;
  6744. -1:
  6745. { Don't make this optimisation if the CPU flags are required, since OR scrambles them }
  6746. if (cs_opt_size in current_settings.optimizerswitches) and
  6747. (taicpu(p).opsize <> S_B) and
  6748. not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  6749. begin
  6750. { change "mov $-1,%reg" into "or $-1,%reg" }
  6751. { NOTES:
  6752. - No size saving is made when changing a Word-sized assignment unless the register is AX (smaller encoding)
  6753. - This operation creates a false dependency on the register, so only do it when optimising for size
  6754. - 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
  6755. }
  6756. taicpu(p).opcode := A_OR;
  6757. Result := True;
  6758. end;
  6759. end;
  6760. end;
  6761. end;
  6762. function TX86AsmOptimizer.PostPeepholeOptAnd(var p : tai) : boolean;
  6763. var
  6764. hp1: tai;
  6765. begin
  6766. { Detect:
  6767. andw x, %ax (0 <= x < $8000)
  6768. ...
  6769. movzwl %ax,%eax
  6770. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  6771. }
  6772. Result := False;
  6773. if MatchOpType(taicpu(p), top_const, top_reg) and
  6774. (taicpu(p).oper[1]^.reg = NR_AX) and { This is also enough to determine that opsize = S_W }
  6775. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val) and
  6776. GetNextInstructionUsingReg(p, hp1, NR_EAX) and
  6777. MatchInstruction(hp1, A_MOVZX, [S_WL]) and
  6778. MatchOperand(taicpu(hp1).oper[0]^, NR_AX) and
  6779. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) then
  6780. begin
  6781. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via AndMovz2AndCwtl)', hp1);
  6782. taicpu(hp1).opcode := A_CWDE;
  6783. taicpu(hp1).clearop(0);
  6784. taicpu(hp1).clearop(1);
  6785. taicpu(hp1).ops := 0;
  6786. { A change was made, but not with p, so move forward 1 }
  6787. p := tai(p.Next);
  6788. Result := True;
  6789. end;
  6790. end;
  6791. function TX86AsmOptimizer.PostPeepholeOptMOVSX(var p : tai) : boolean;
  6792. begin
  6793. Result := False;
  6794. if not MatchOpType(taicpu(p), top_reg, top_reg) then
  6795. Exit;
  6796. { Convert:
  6797. movswl %ax,%eax -> cwtl
  6798. movslq %eax,%rax -> cdqe
  6799. NOTE: Don't convert movswl %al,%ax to cbw, because cbw and cwde
  6800. refer to the same opcode and depends only on the assembler's
  6801. current operand-size attribute. [Kit]
  6802. }
  6803. with taicpu(p) do
  6804. case opsize of
  6805. S_WL:
  6806. if (oper[0]^.reg = NR_AX) and (oper[1]^.reg = NR_EAX) then
  6807. begin
  6808. DebugMsg(SPeepholeOptimization + 'Converted movswl %ax,%eax to cwtl', p);
  6809. opcode := A_CWDE;
  6810. clearop(0);
  6811. clearop(1);
  6812. ops := 0;
  6813. Result := True;
  6814. end;
  6815. {$ifdef x86_64}
  6816. S_LQ:
  6817. if (oper[0]^.reg = NR_EAX) and (oper[1]^.reg = NR_RAX) then
  6818. begin
  6819. DebugMsg(SPeepholeOptimization + 'Converted movslq %eax,%rax to cltq', p);
  6820. opcode := A_CDQE;
  6821. clearop(0);
  6822. clearop(1);
  6823. ops := 0;
  6824. Result := True;
  6825. end;
  6826. {$endif x86_64}
  6827. else
  6828. ;
  6829. end;
  6830. end;
  6831. function TX86AsmOptimizer.PostPeepholeOptShr(var p : tai) : boolean;
  6832. var
  6833. hp1: tai;
  6834. begin
  6835. { Detect:
  6836. shr x, %ax (x > 0)
  6837. ...
  6838. movzwl %ax,%eax
  6839. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  6840. }
  6841. Result := False;
  6842. if MatchOpType(taicpu(p), top_const, top_reg) and
  6843. (taicpu(p).oper[1]^.reg = NR_AX) and { This is also enough to determine that opsize = S_W }
  6844. (taicpu(p).oper[0]^.val > 0) and
  6845. GetNextInstructionUsingReg(p, hp1, NR_EAX) and
  6846. MatchInstruction(hp1, A_MOVZX, [S_WL]) and
  6847. MatchOperand(taicpu(hp1).oper[0]^, NR_AX) and
  6848. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) then
  6849. begin
  6850. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via ShrMovz2ShrCwtl)', hp1);
  6851. taicpu(hp1).opcode := A_CWDE;
  6852. taicpu(hp1).clearop(0);
  6853. taicpu(hp1).clearop(1);
  6854. taicpu(hp1).ops := 0;
  6855. { A change was made, but not with p, so move forward 1 }
  6856. p := tai(p.Next);
  6857. Result := True;
  6858. end;
  6859. end;
  6860. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  6861. begin
  6862. Result:=false;
  6863. { change "cmp $0, %reg" to "test %reg, %reg" }
  6864. if MatchOpType(taicpu(p),top_const,top_reg) and
  6865. (taicpu(p).oper[0]^.val = 0) then
  6866. begin
  6867. taicpu(p).opcode := A_TEST;
  6868. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  6869. Result:=true;
  6870. end;
  6871. end;
  6872. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  6873. var
  6874. IsTestConstX : Boolean;
  6875. hp1,hp2 : tai;
  6876. begin
  6877. Result:=false;
  6878. { removes the line marked with (x) from the sequence
  6879. and/or/xor/add/sub/... $x, %y
  6880. test/or %y, %y | test $-1, %y (x)
  6881. j(n)z _Label
  6882. as the first instruction already adjusts the ZF
  6883. %y operand may also be a reference }
  6884. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  6885. MatchOperand(taicpu(p).oper[0]^,-1);
  6886. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  6887. GetLastInstruction(p, hp1) and
  6888. (tai(hp1).typ = ait_instruction) and
  6889. GetNextInstruction(p,hp2) and
  6890. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  6891. case taicpu(hp1).opcode Of
  6892. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  6893. begin
  6894. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  6895. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  6896. { and in case of carry for A(E)/B(E)/C/NC }
  6897. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  6898. ((taicpu(hp1).opcode <> A_ADD) and
  6899. (taicpu(hp1).opcode <> A_SUB))) then
  6900. begin
  6901. RemoveCurrentP(p, hp2);
  6902. Result:=true;
  6903. end;
  6904. end;
  6905. A_SHL, A_SAL, A_SHR, A_SAR:
  6906. begin
  6907. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  6908. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  6909. { therefore, it's only safe to do this optimization for }
  6910. { shifts by a (nonzero) constant }
  6911. (taicpu(hp1).oper[0]^.typ = top_const) and
  6912. (taicpu(hp1).oper[0]^.val <> 0) and
  6913. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  6914. { and in case of carry for A(E)/B(E)/C/NC }
  6915. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  6916. begin
  6917. RemoveCurrentP(p, hp2);
  6918. Result:=true;
  6919. end;
  6920. end;
  6921. A_DEC, A_INC, A_NEG:
  6922. begin
  6923. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  6924. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  6925. { and in case of carry for A(E)/B(E)/C/NC }
  6926. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  6927. begin
  6928. case taicpu(hp1).opcode of
  6929. A_DEC, A_INC:
  6930. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  6931. begin
  6932. case taicpu(hp1).opcode Of
  6933. A_DEC: taicpu(hp1).opcode := A_SUB;
  6934. A_INC: taicpu(hp1).opcode := A_ADD;
  6935. else
  6936. ;
  6937. end;
  6938. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  6939. taicpu(hp1).loadConst(0,1);
  6940. taicpu(hp1).ops:=2;
  6941. end;
  6942. else
  6943. ;
  6944. end;
  6945. RemoveCurrentP(p, hp2);
  6946. Result:=true;
  6947. end;
  6948. end
  6949. else
  6950. { change "test $-1,%reg" into "test %reg,%reg" }
  6951. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  6952. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  6953. end { case }
  6954. { change "test $-1,%reg" into "test %reg,%reg" }
  6955. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  6956. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  6957. end;
  6958. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  6959. var
  6960. hp1,hp3 : tai;
  6961. {$ifndef x86_64}
  6962. hp2 : taicpu;
  6963. {$endif x86_64}
  6964. begin
  6965. Result:=false;
  6966. hp3:=nil;
  6967. {$ifndef x86_64}
  6968. { don't do this on modern CPUs, this really hurts them due to
  6969. broken call/ret pairing }
  6970. if (current_settings.optimizecputype < cpu_Pentium2) and
  6971. not(cs_create_pic in current_settings.moduleswitches) and
  6972. GetNextInstruction(p, hp1) and
  6973. MatchInstruction(hp1,A_JMP,[S_NO]) and
  6974. MatchOpType(taicpu(hp1),top_ref) and
  6975. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  6976. begin
  6977. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  6978. InsertLLItem(p.previous, p, hp2);
  6979. taicpu(p).opcode := A_JMP;
  6980. taicpu(p).is_jmp := true;
  6981. RemoveInstruction(hp1);
  6982. Result:=true;
  6983. end
  6984. else
  6985. {$endif x86_64}
  6986. { replace
  6987. call procname
  6988. ret
  6989. by
  6990. jmp procname
  6991. but do it only on level 4 because it destroys stack back traces
  6992. else if the subroutine is marked as no return, remove the ret
  6993. }
  6994. if ((cs_opt_level4 in current_settings.optimizerswitches) or
  6995. (po_noreturn in current_procinfo.procdef.procoptions)) and
  6996. GetNextInstruction(p, hp1) and
  6997. (MatchInstruction(hp1,A_RET,[S_NO]) or
  6998. (MatchInstruction(hp1,A_VZEROUPPER,[S_NO]) and
  6999. SetAndTest(hp1,hp3) and
  7000. GetNextInstruction(hp1,hp1) and
  7001. MatchInstruction(hp1,A_RET,[S_NO])
  7002. )
  7003. ) and
  7004. (taicpu(hp1).ops=0) then
  7005. begin
  7006. if (cs_opt_level4 in current_settings.optimizerswitches) and
  7007. { we might destroy stack alignment here if we do not do a call }
  7008. (target_info.stackalign<=sizeof(SizeUInt)) then
  7009. begin
  7010. taicpu(p).opcode := A_JMP;
  7011. taicpu(p).is_jmp := true;
  7012. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  7013. end
  7014. else
  7015. DebugMsg(SPeepholeOptimization + 'CallRet2Call done',p);
  7016. RemoveInstruction(hp1);
  7017. if Assigned(hp3) then
  7018. begin
  7019. AsmL.Remove(hp3);
  7020. AsmL.InsertBefore(hp3,p)
  7021. end;
  7022. Result:=true;
  7023. end;
  7024. end;
  7025. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  7026. function ConstInRange(const Val: TCGInt; const OpSize: TOpSize): Boolean;
  7027. begin
  7028. case OpSize of
  7029. S_B, S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  7030. Result := (Val <= $FF) and (Val >= -128);
  7031. S_W, S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  7032. Result := (Val <= $FFFF) and (Val >= -32768);
  7033. S_L{$ifdef x86_64}, S_LQ{$endif x86_64}:
  7034. Result := (Val <= $FFFFFFFF) and (Val >= -2147483648);
  7035. else
  7036. Result := True;
  7037. end;
  7038. end;
  7039. var
  7040. hp1, hp2 : tai;
  7041. SizeChange: Boolean;
  7042. PreMessage: string;
  7043. begin
  7044. Result := False;
  7045. if (taicpu(p).oper[0]^.typ = top_reg) and
  7046. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  7047. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) then
  7048. begin
  7049. { Change (using movzbl %al,%eax as an example):
  7050. movzbl %al, %eax movzbl %al, %eax
  7051. cmpl x, %eax testl %eax,%eax
  7052. To:
  7053. cmpb x, %al testb %al, %al (Move one back to avoid a false dependency)
  7054. movzbl %al, %eax movzbl %al, %eax
  7055. Smaller instruction and minimises pipeline stall as the CPU
  7056. doesn't have to wait for the register to get zero-extended. [Kit]
  7057. Also allow if the smaller of the two registers is being checked,
  7058. as this still removes the false dependency.
  7059. }
  7060. if
  7061. (
  7062. (
  7063. (taicpu(hp1).opcode = A_CMP) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  7064. ConstInRange(taicpu(hp1).oper[0]^.val, taicpu(p).opsize)
  7065. ) or (
  7066. { If MatchOperand returns True, they must both be registers }
  7067. (taicpu(hp1).opcode = A_TEST) and MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)
  7068. )
  7069. ) and
  7070. (reg2opsize(taicpu(hp1).oper[1]^.reg) <= reg2opsize(taicpu(p).oper[1]^.reg)) and
  7071. SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) then
  7072. begin
  7073. 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);
  7074. asml.Remove(hp1);
  7075. asml.InsertBefore(hp1, p);
  7076. { Swap instructions in the case of cmp 0,%reg or test %reg,%reg }
  7077. if (taicpu(hp1).opcode = A_TEST) or (taicpu(hp1).oper[0]^.val = 0) then
  7078. begin
  7079. taicpu(hp1).opcode := A_TEST;
  7080. taicpu(hp1).loadreg(0, taicpu(p).oper[0]^.reg);
  7081. end;
  7082. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  7083. case taicpu(p).opsize of
  7084. S_BW, S_BL:
  7085. begin
  7086. SizeChange := taicpu(hp1).opsize <> S_B;
  7087. taicpu(hp1).changeopsize(S_B);
  7088. end;
  7089. S_WL:
  7090. begin
  7091. SizeChange := taicpu(hp1).opsize <> S_W;
  7092. taicpu(hp1).changeopsize(S_W);
  7093. end
  7094. else
  7095. InternalError(2020112701);
  7096. end;
  7097. UpdateUsedRegs(tai(p.Next));
  7098. { Check if the register is used aferwards - if not, we can
  7099. remove the movzx instruction completely }
  7100. if not RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, p, UsedRegs) then
  7101. begin
  7102. { Hp1 is a better position than p for debugging purposes }
  7103. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4a', hp1);
  7104. RemoveCurrentp(p, hp1);
  7105. Result := True;
  7106. end;
  7107. if SizeChange then
  7108. DebugMsg(SPeepholeOptimization + PreMessage +
  7109. 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)
  7110. else
  7111. DebugMsg(SPeepholeOptimization + 'MovzxCmp2CmpMovzx', hp1);
  7112. Exit;
  7113. end;
  7114. { Change (using movzwl %ax,%eax as an example):
  7115. movzwl %ax, %eax
  7116. movb %al, (dest) (Register is smaller than read register in movz)
  7117. To:
  7118. movb %al, (dest) (Move one back to avoid a false dependency)
  7119. movzwl %ax, %eax
  7120. }
  7121. if (taicpu(hp1).opcode = A_MOV) and
  7122. (taicpu(hp1).oper[0]^.typ = top_reg) and
  7123. not RegInOp(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^) and
  7124. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(p).oper[0]^.reg) and
  7125. (reg2opsize(taicpu(hp1).oper[0]^.reg) <= reg2opsize(taicpu(p).oper[0]^.reg)) then
  7126. begin
  7127. DebugMsg(SPeepholeOptimization + 'MovzxMov2MovMovzx', hp1);
  7128. hp2 := tai(hp1.Previous); { Effectively the old position of hp1 }
  7129. asml.Remove(hp1);
  7130. asml.InsertBefore(hp1, p);
  7131. if taicpu(hp1).oper[1]^.typ = top_reg then
  7132. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, hp2, UsedRegs);
  7133. { Check if the register is used aferwards - if not, we can
  7134. remove the movzx instruction completely }
  7135. if not RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, p, UsedRegs) then
  7136. begin
  7137. { Hp1 is a better position than p for debugging purposes }
  7138. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4b', hp1);
  7139. RemoveCurrentp(p, hp1);
  7140. Result := True;
  7141. end;
  7142. Exit;
  7143. end;
  7144. end;
  7145. {$ifdef x86_64}
  7146. { Code size reduction by J. Gareth "Kit" Moreton }
  7147. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  7148. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  7149. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  7150. then
  7151. begin
  7152. { Has 64-bit register name and opcode suffix }
  7153. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  7154. { The actual optimization }
  7155. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  7156. if taicpu(p).opsize = S_BQ then
  7157. taicpu(p).changeopsize(S_BL)
  7158. else
  7159. taicpu(p).changeopsize(S_WL);
  7160. DebugMsg(SPeepholeOptimization + PreMessage +
  7161. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  7162. end;
  7163. {$endif}
  7164. end;
  7165. {$ifdef x86_64}
  7166. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  7167. var
  7168. PreMessage, RegName: string;
  7169. begin
  7170. { Code size reduction by J. Gareth "Kit" Moreton }
  7171. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  7172. as this removes the REX prefix }
  7173. Result := False;
  7174. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  7175. Exit;
  7176. if taicpu(p).oper[0]^.typ <> top_reg then
  7177. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  7178. InternalError(2018011500);
  7179. case taicpu(p).opsize of
  7180. S_Q:
  7181. begin
  7182. 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
  7183. begin
  7184. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  7185. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  7186. { The actual optimization }
  7187. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  7188. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  7189. taicpu(p).changeopsize(S_L);
  7190. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  7191. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  7192. end;
  7193. end;
  7194. else
  7195. ;
  7196. end;
  7197. end;
  7198. {$endif}
  7199. class procedure TX86AsmOptimizer.OptimizeRefs(var p: taicpu);
  7200. var
  7201. OperIdx: Integer;
  7202. begin
  7203. for OperIdx := 0 to p.ops - 1 do
  7204. if p.oper[OperIdx]^.typ = top_ref then
  7205. optimize_ref(p.oper[OperIdx]^.ref^, False);
  7206. end;
  7207. end.