aoptx86.pas 249 KB

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