aoptx86.pas 226 KB

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