aoptx86.pas 189 KB

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