aoptx86.pas 198 KB

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