aoptx86.pas 184 KB

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