aoptx86.pas 252 KB

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