aoptx86.pas 343 KB

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