aoptx86.pas 205 KB

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