aoptx86.pas 184 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410
  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))) and
  2425. (not GetNextInstruction(hp1,hp2) or
  2426. not instrReadsFlags(hp2)) Do
  2427. begin
  2428. TmpBool1 := False;
  2429. if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  2430. begin
  2431. TmpBool1 := True;
  2432. TmpBool2 := True;
  2433. case taicpu(hp1).opcode of
  2434. A_ADD:
  2435. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  2436. A_SUB:
  2437. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  2438. else
  2439. internalerror(2019050536);
  2440. end;
  2441. asml.remove(hp1);
  2442. hp1.free;
  2443. end
  2444. else
  2445. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  2446. (((taicpu(hp1).opcode = A_ADD) and
  2447. (TmpRef.base = NR_NO)) or
  2448. (taicpu(hp1).opcode = A_INC) or
  2449. (taicpu(hp1).opcode = A_DEC)) then
  2450. begin
  2451. TmpBool1 := True;
  2452. TmpBool2 := True;
  2453. case taicpu(hp1).opcode of
  2454. A_ADD:
  2455. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  2456. A_INC:
  2457. inc(TmpRef.offset);
  2458. A_DEC:
  2459. dec(TmpRef.offset);
  2460. else
  2461. internalerror(2019050535);
  2462. end;
  2463. asml.remove(hp1);
  2464. hp1.free;
  2465. end;
  2466. end;
  2467. if TmpBool2
  2468. {$ifndef x86_64}
  2469. or
  2470. ((current_settings.optimizecputype < cpu_Pentium2) and
  2471. (taicpu(p).oper[0]^.val <= 3) and
  2472. not(cs_opt_size in current_settings.optimizerswitches))
  2473. {$endif x86_64}
  2474. then
  2475. begin
  2476. if not(TmpBool2) and
  2477. (taicpu(p).oper[0]^.val = 1) then
  2478. begin
  2479. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  2480. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  2481. end
  2482. else
  2483. hp1 := taicpu.op_ref_reg(A_LEA, taicpu(p).opsize, TmpRef,
  2484. taicpu(p).oper[1]^.reg);
  2485. InsertLLItem(p.previous, p.next, hp1);
  2486. p.free;
  2487. p := hp1;
  2488. end;
  2489. end
  2490. {$ifndef x86_64}
  2491. else if (current_settings.optimizecputype < cpu_Pentium2) and
  2492. MatchOpType(taicpu(p),top_const,top_reg) then
  2493. begin
  2494. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  2495. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  2496. (unlike shl, which is only Tairable in the U pipe) }
  2497. if taicpu(p).oper[0]^.val=1 then
  2498. begin
  2499. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  2500. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  2501. InsertLLItem(p.previous, p.next, hp1);
  2502. p.free;
  2503. p := hp1;
  2504. end
  2505. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  2506. "shl $3, %reg" to "lea (,%reg,8), %reg }
  2507. else if (taicpu(p).opsize = S_L) and
  2508. (taicpu(p).oper[0]^.val<= 3) then
  2509. begin
  2510. reference_reset(tmpref,2,[]);
  2511. TmpRef.index := taicpu(p).oper[1]^.reg;
  2512. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  2513. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  2514. InsertLLItem(p.previous, p.next, hp1);
  2515. p.free;
  2516. p := hp1;
  2517. end;
  2518. end
  2519. {$endif x86_64}
  2520. ;
  2521. end;
  2522. function TX86AsmOptimizer.OptPass1SETcc(var p: tai): boolean;
  2523. var
  2524. hp1,hp2,next: tai; SetC, JumpC: TAsmCond;
  2525. begin
  2526. Result:=false;
  2527. if MatchOpType(taicpu(p),top_reg) and
  2528. GetNextInstruction(p, hp1) and
  2529. MatchInstruction(hp1, A_TEST, [S_B]) and
  2530. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2531. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  2532. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  2533. GetNextInstruction(hp1, hp2) and
  2534. MatchInstruction(hp2, A_Jcc, []) then
  2535. { Change from: To:
  2536. set(C) %reg j(~C) label
  2537. test %reg,%reg
  2538. je label
  2539. set(C) %reg j(C) label
  2540. test %reg,%reg
  2541. jne label
  2542. }
  2543. begin
  2544. next := tai(p.Next);
  2545. TransferUsedRegs(TmpUsedRegs);
  2546. UpdateUsedRegs(TmpUsedRegs, next);
  2547. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2548. asml.Remove(hp1);
  2549. hp1.Free;
  2550. JumpC := taicpu(hp2).condition;
  2551. if conditions_equal(JumpC, C_E) then
  2552. SetC := inverse_cond(taicpu(p).condition)
  2553. else if conditions_equal(JumpC, C_NE) then
  2554. SetC := taicpu(p).condition
  2555. else
  2556. InternalError(2018061400);
  2557. if SetC = C_NONE then
  2558. InternalError(2018061401);
  2559. taicpu(hp2).SetCondition(SetC);
  2560. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
  2561. begin
  2562. asml.Remove(p);
  2563. UpdateUsedRegs(next);
  2564. p.Free;
  2565. Result := True;
  2566. p := hp2;
  2567. end;
  2568. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> Jcc',p);
  2569. end;
  2570. end;
  2571. function TX86AsmOptimizer.OptPass1FSTP(var p: tai): boolean;
  2572. { returns true if a "continue" should be done after this optimization }
  2573. var
  2574. hp1, hp2: tai;
  2575. begin
  2576. Result := false;
  2577. if MatchOpType(taicpu(p),top_ref) and
  2578. GetNextInstruction(p, hp1) and
  2579. (hp1.typ = ait_instruction) and
  2580. (((taicpu(hp1).opcode = A_FLD) and
  2581. (taicpu(p).opcode = A_FSTP)) or
  2582. ((taicpu(p).opcode = A_FISTP) and
  2583. (taicpu(hp1).opcode = A_FILD))) and
  2584. MatchOpType(taicpu(hp1),top_ref) and
  2585. (taicpu(hp1).opsize = taicpu(p).opsize) and
  2586. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  2587. begin
  2588. { replacing fstp f;fld f by fst f is only valid for extended because of rounding }
  2589. if (taicpu(p).opsize=S_FX) and
  2590. GetNextInstruction(hp1, hp2) and
  2591. (hp2.typ = ait_instruction) and
  2592. IsExitCode(hp2) and
  2593. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  2594. not(assigned(current_procinfo.procdef.funcretsym) and
  2595. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  2596. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  2597. begin
  2598. asml.remove(p);
  2599. asml.remove(hp1);
  2600. p.free;
  2601. hp1.free;
  2602. p := hp2;
  2603. RemoveLastDeallocForFuncRes(p);
  2604. Result := true;
  2605. end
  2606. (* can't be done because the store operation rounds
  2607. else
  2608. { fst can't store an extended value! }
  2609. if (taicpu(p).opsize <> S_FX) and
  2610. (taicpu(p).opsize <> S_IQ) then
  2611. begin
  2612. if (taicpu(p).opcode = A_FSTP) then
  2613. taicpu(p).opcode := A_FST
  2614. else taicpu(p).opcode := A_FIST;
  2615. asml.remove(hp1);
  2616. hp1.free;
  2617. end
  2618. *)
  2619. end;
  2620. end;
  2621. function TX86AsmOptimizer.OptPass1FLD(var p : tai) : boolean;
  2622. var
  2623. hp1, hp2: tai;
  2624. begin
  2625. result:=false;
  2626. if MatchOpType(taicpu(p),top_reg) and
  2627. GetNextInstruction(p, hp1) and
  2628. (hp1.typ = Ait_Instruction) and
  2629. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2630. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  2631. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  2632. { change to
  2633. fld reg fxxx reg,st
  2634. fxxxp st, st1 (hp1)
  2635. Remark: non commutative operations must be reversed!
  2636. }
  2637. begin
  2638. case taicpu(hp1).opcode Of
  2639. A_FMULP,A_FADDP,
  2640. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  2641. begin
  2642. case taicpu(hp1).opcode Of
  2643. A_FADDP: taicpu(hp1).opcode := A_FADD;
  2644. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  2645. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  2646. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  2647. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  2648. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  2649. else
  2650. internalerror(2019050534);
  2651. end;
  2652. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  2653. taicpu(hp1).oper[1]^.reg := NR_ST;
  2654. asml.remove(p);
  2655. p.free;
  2656. p := hp1;
  2657. Result:=true;
  2658. exit;
  2659. end;
  2660. else
  2661. ;
  2662. end;
  2663. end
  2664. else
  2665. if MatchOpType(taicpu(p),top_ref) and
  2666. GetNextInstruction(p, hp2) and
  2667. (hp2.typ = Ait_Instruction) and
  2668. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  2669. (taicpu(p).opsize in [S_FS, S_FL]) and
  2670. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  2671. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  2672. if GetLastInstruction(p, hp1) and
  2673. MatchInstruction(hp1,A_FLD,A_FST,[taicpu(p).opsize]) and
  2674. MatchOpType(taicpu(hp1),top_ref) and
  2675. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  2676. if ((taicpu(hp2).opcode = A_FMULP) or
  2677. (taicpu(hp2).opcode = A_FADDP)) then
  2678. { change to
  2679. fld/fst mem1 (hp1) fld/fst mem1
  2680. fld mem1 (p) fadd/
  2681. faddp/ fmul st, st
  2682. fmulp st, st1 (hp2) }
  2683. begin
  2684. asml.remove(p);
  2685. p.free;
  2686. p := hp1;
  2687. if (taicpu(hp2).opcode = A_FADDP) then
  2688. taicpu(hp2).opcode := A_FADD
  2689. else
  2690. taicpu(hp2).opcode := A_FMUL;
  2691. taicpu(hp2).oper[1]^.reg := NR_ST;
  2692. end
  2693. else
  2694. { change to
  2695. fld/fst mem1 (hp1) fld/fst mem1
  2696. fld mem1 (p) fld st}
  2697. begin
  2698. taicpu(p).changeopsize(S_FL);
  2699. taicpu(p).loadreg(0,NR_ST);
  2700. end
  2701. else
  2702. begin
  2703. case taicpu(hp2).opcode Of
  2704. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  2705. { change to
  2706. fld/fst mem1 (hp1) fld/fst mem1
  2707. fld mem2 (p) fxxx mem2
  2708. fxxxp st, st1 (hp2) }
  2709. begin
  2710. case taicpu(hp2).opcode Of
  2711. A_FADDP: taicpu(p).opcode := A_FADD;
  2712. A_FMULP: taicpu(p).opcode := A_FMUL;
  2713. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  2714. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  2715. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  2716. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  2717. else
  2718. internalerror(2019050533);
  2719. end;
  2720. asml.remove(hp2);
  2721. hp2.free;
  2722. end
  2723. else
  2724. ;
  2725. end
  2726. end
  2727. end;
  2728. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  2729. var
  2730. hp1,hp2: tai;
  2731. {$ifdef x86_64}
  2732. hp3: tai;
  2733. {$endif x86_64}
  2734. begin
  2735. Result:=false;
  2736. if MatchOpType(taicpu(p),top_reg,top_reg) and
  2737. GetNextInstruction(p, hp1) and
  2738. {$ifdef x86_64}
  2739. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  2740. {$else x86_64}
  2741. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  2742. {$endif x86_64}
  2743. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2744. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  2745. { mov reg1, reg2 mov reg1, reg2
  2746. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  2747. begin
  2748. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  2749. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  2750. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  2751. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  2752. TransferUsedRegs(TmpUsedRegs);
  2753. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2754. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  2755. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  2756. then
  2757. begin
  2758. asml.remove(p);
  2759. p.free;
  2760. p := hp1;
  2761. Result:=true;
  2762. end;
  2763. exit;
  2764. end
  2765. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  2766. GetNextInstruction(p, hp1) and
  2767. {$ifdef x86_64}
  2768. MatchInstruction(hp1,[A_MOV,A_MOVZX,A_MOVSX,A_MOVSXD],[]) and
  2769. {$else x86_64}
  2770. MatchInstruction(hp1,A_MOV,A_MOVZX,A_MOVSX,[]) and
  2771. {$endif x86_64}
  2772. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  2773. ((taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg)
  2774. or
  2775. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg)
  2776. ) and
  2777. (getsupreg(taicpu(hp1).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) then
  2778. { mov reg1, reg2
  2779. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  2780. begin
  2781. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) then
  2782. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[0]^.reg;
  2783. if (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) then
  2784. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  2785. DebugMsg(SPeepholeOptimization + 'MovMovXX2MoVXX 1 done',p);
  2786. asml.remove(p);
  2787. p.free;
  2788. p := hp1;
  2789. Result:=true;
  2790. exit;
  2791. end
  2792. else if (taicpu(p).oper[0]^.typ = top_ref) and
  2793. GetNextInstruction(p,hp1) and
  2794. (hp1.typ = ait_instruction) and
  2795. { while the GetNextInstruction(hp1,hp2) call could be factored out,
  2796. doing it separately in both branches allows to do the cheap checks
  2797. with low probability earlier }
  2798. ((IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  2799. GetNextInstruction(hp1,hp2) and
  2800. MatchInstruction(hp2,A_MOV,[])
  2801. ) or
  2802. ((taicpu(hp1).opcode=A_LEA) and
  2803. GetNextInstruction(hp1,hp2) and
  2804. MatchInstruction(hp2,A_MOV,[]) and
  2805. ((MatchReference(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  2806. (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg)
  2807. ) or
  2808. (MatchReference(taicpu(hp1).oper[0]^.ref^,NR_INVALID,
  2809. taicpu(p).oper[1]^.reg) and
  2810. (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg)) or
  2811. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_NO)) or
  2812. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,NR_NO,taicpu(p).oper[1]^.reg))
  2813. ) and
  2814. ((MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^)) or not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)))
  2815. )
  2816. ) and
  2817. MatchOperand(taicpu(hp1).oper[taicpu(hp1).ops-1]^,taicpu(hp2).oper[0]^) and
  2818. (taicpu(hp2).oper[1]^.typ = top_ref) then
  2819. begin
  2820. TransferUsedRegs(TmpUsedRegs);
  2821. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  2822. UpdateUsedRegs(TmpUsedRegs,tai(hp1.next));
  2823. if (RefsEqual(taicpu(hp2).oper[1]^.ref^,taicpu(p).oper[0]^.ref^) and
  2824. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,TmpUsedRegs))) then
  2825. { change mov (ref), reg
  2826. add/sub/or/... reg2/$const, reg
  2827. mov reg, (ref)
  2828. # release reg
  2829. to add/sub/or/... reg2/$const, (ref) }
  2830. begin
  2831. case taicpu(hp1).opcode of
  2832. A_INC,A_DEC,A_NOT,A_NEG :
  2833. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  2834. A_LEA :
  2835. begin
  2836. taicpu(hp1).opcode:=A_ADD;
  2837. if (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.index<>NR_NO) then
  2838. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.index)
  2839. else if (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.base<>NR_NO) then
  2840. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.base)
  2841. else
  2842. taicpu(hp1).loadconst(0,taicpu(hp1).oper[0]^.ref^.offset);
  2843. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  2844. DebugMsg(SPeepholeOptimization + 'FoldLea done',hp1);
  2845. end
  2846. else
  2847. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  2848. end;
  2849. asml.remove(p);
  2850. asml.remove(hp2);
  2851. p.free;
  2852. hp2.free;
  2853. p := hp1
  2854. end;
  2855. Exit;
  2856. {$ifdef x86_64}
  2857. end
  2858. else if (taicpu(p).opsize = S_L) and
  2859. (taicpu(p).oper[1]^.typ = top_reg) and
  2860. (
  2861. GetNextInstruction(p, hp1) and
  2862. MatchInstruction(hp1, A_MOV,[]) and
  2863. (taicpu(hp1).opsize = S_L) and
  2864. (taicpu(hp1).oper[1]^.typ = top_reg)
  2865. ) and (
  2866. GetNextInstruction(hp1, hp2) and
  2867. (tai(hp2).typ=ait_instruction) and
  2868. (taicpu(hp2).opsize = S_Q) and
  2869. (
  2870. (
  2871. MatchInstruction(hp2, A_ADD,[]) and
  2872. (taicpu(hp2).opsize = S_Q) and
  2873. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  2874. (
  2875. (
  2876. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  2877. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2878. ) or (
  2879. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  2880. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  2881. )
  2882. )
  2883. ) or (
  2884. MatchInstruction(hp2, A_LEA,[]) and
  2885. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  2886. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  2887. (
  2888. (
  2889. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  2890. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2891. ) or (
  2892. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  2893. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  2894. )
  2895. ) and (
  2896. (
  2897. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2898. ) or (
  2899. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  2900. )
  2901. )
  2902. )
  2903. )
  2904. ) and (
  2905. GetNextInstruction(hp2, hp3) and
  2906. MatchInstruction(hp3, A_SHR,[]) and
  2907. (taicpu(hp3).opsize = S_Q) and
  2908. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  2909. (taicpu(hp3).oper[0]^.val = 1) and
  2910. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  2911. ) then
  2912. begin
  2913. { Change movl x, reg1d movl x, reg1d
  2914. movl y, reg2d movl y, reg2d
  2915. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  2916. shrq $1, reg1q shrq $1, reg1q
  2917. ( reg1d and reg2d can be switched around in the first two instructions )
  2918. To movl x, reg1d
  2919. addl y, reg1d
  2920. rcrl $1, reg1d
  2921. This corresponds to the common expression (x + y) shr 1, where
  2922. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  2923. smaller code, but won't account for x + y causing an overflow). [Kit]
  2924. }
  2925. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  2926. { Change first MOV command to have the same register as the final output }
  2927. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  2928. else
  2929. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  2930. { Change second MOV command to an ADD command. This is easier than
  2931. converting the existing command because it means we don't have to
  2932. touch 'y', which might be a complicated reference, and also the
  2933. fact that the third command might either be ADD or LEA. [Kit] }
  2934. taicpu(hp1).opcode := A_ADD;
  2935. { Delete old ADD/LEA instruction }
  2936. asml.remove(hp2);
  2937. hp2.free;
  2938. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  2939. taicpu(hp3).opcode := A_RCR;
  2940. taicpu(hp3).changeopsize(S_L);
  2941. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  2942. {$endif x86_64}
  2943. end;
  2944. end;
  2945. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  2946. var
  2947. hp1 : tai;
  2948. begin
  2949. Result:=false;
  2950. if (taicpu(p).ops >= 2) and
  2951. ((taicpu(p).oper[0]^.typ = top_const) or
  2952. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  2953. (taicpu(p).oper[1]^.typ = top_reg) and
  2954. ((taicpu(p).ops = 2) or
  2955. ((taicpu(p).oper[2]^.typ = top_reg) and
  2956. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  2957. GetLastInstruction(p,hp1) and
  2958. MatchInstruction(hp1,A_MOV,[]) and
  2959. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2960. ((taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) or
  2961. ((taicpu(hp1).opsize=S_L) and (taicpu(p).opsize=S_Q) and SuperRegistersEqual(taicpu(hp1).oper[1]^.reg,taicpu(p).oper[1]^.reg))) then
  2962. begin
  2963. TransferUsedRegs(TmpUsedRegs);
  2964. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) then
  2965. { change
  2966. mov reg1,reg2
  2967. imul y,reg2 to imul y,reg1,reg2 }
  2968. begin
  2969. taicpu(p).ops := 3;
  2970. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  2971. taicpu(p).loadreg(2,taicpu(hp1).oper[1]^.reg);
  2972. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  2973. asml.remove(hp1);
  2974. hp1.free;
  2975. result:=true;
  2976. end;
  2977. end;
  2978. end;
  2979. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  2980. var
  2981. hp1 : tai;
  2982. begin
  2983. {
  2984. change
  2985. jmp .L1
  2986. ...
  2987. .L1:
  2988. ret
  2989. into
  2990. ret
  2991. }
  2992. result:=false;
  2993. 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
  2994. (taicpu(p).oper[0]^.ref^.index=NR_NO) then
  2995. begin
  2996. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  2997. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and
  2998. MatchInstruction(hp1,A_RET,[S_NO]) then
  2999. begin
  3000. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  3001. taicpu(p).opcode:=A_RET;
  3002. taicpu(p).is_jmp:=false;
  3003. taicpu(p).ops:=taicpu(hp1).ops;
  3004. case taicpu(hp1).ops of
  3005. 0:
  3006. taicpu(p).clearop(0);
  3007. 1:
  3008. taicpu(p).loadconst(0,taicpu(hp1).oper[0]^.val);
  3009. else
  3010. internalerror(2016041301);
  3011. end;
  3012. result:=true;
  3013. end;
  3014. end;
  3015. end;
  3016. function CanBeCMOV(p : tai) : boolean;
  3017. begin
  3018. CanBeCMOV:=assigned(p) and
  3019. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  3020. { we can't use cmov ref,reg because
  3021. ref could be nil and cmov still throws an exception
  3022. if ref=nil but the mov isn't done (FK)
  3023. or ((taicpu(p).oper[0]^.typ = top_ref) and
  3024. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  3025. }
  3026. (MatchOpType(taicpu(p),top_reg,top_reg) or
  3027. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  3028. it is not expected that this can cause a seg. violation }
  3029. (MatchOpType(taicpu(p),top_ref,top_reg) and
  3030. (((taicpu(p).oper[0]^.ref^.base=NR_NO) and (taicpu(p).oper[0]^.ref^.refaddr=addr_no)){$ifdef x86_64} or
  3031. ((taicpu(p).oper[0]^.ref^.base=NR_RIP) and (taicpu(p).oper[0]^.ref^.refaddr=addr_pic)){$endif x86_64}
  3032. ) and
  3033. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  3034. (taicpu(p).oper[0]^.ref^.offset=0)
  3035. )
  3036. );
  3037. end;
  3038. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  3039. var
  3040. hp1,hp2,hp3,hp4,hpmov2: tai;
  3041. carryadd_opcode : TAsmOp;
  3042. l : Longint;
  3043. condition : TAsmCond;
  3044. symbol: TAsmSymbol;
  3045. begin
  3046. result:=false;
  3047. symbol:=nil;
  3048. if GetNextInstruction(p,hp1) then
  3049. begin
  3050. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  3051. if (hp1.typ=ait_instruction) and
  3052. GetNextInstruction(hp1,hp2) and (hp2.typ=ait_label) and
  3053. (Tasmlabel(symbol) = Tai_label(hp2).labsym) then
  3054. { jb @@1 cmc
  3055. inc/dec operand --> adc/sbb operand,0
  3056. @@1:
  3057. ... and ...
  3058. jnb @@1
  3059. inc/dec operand --> adc/sbb operand,0
  3060. @@1: }
  3061. begin
  3062. carryadd_opcode:=A_NONE;
  3063. if Taicpu(p).condition in [C_NAE,C_B] then
  3064. begin
  3065. if Taicpu(hp1).opcode=A_INC then
  3066. carryadd_opcode:=A_ADC;
  3067. if Taicpu(hp1).opcode=A_DEC then
  3068. carryadd_opcode:=A_SBB;
  3069. if carryadd_opcode<>A_NONE then
  3070. begin
  3071. Taicpu(p).clearop(0);
  3072. Taicpu(p).ops:=0;
  3073. Taicpu(p).is_jmp:=false;
  3074. Taicpu(p).opcode:=A_CMC;
  3075. Taicpu(p).condition:=C_NONE;
  3076. Taicpu(hp1).ops:=2;
  3077. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  3078. Taicpu(hp1).loadconst(0,0);
  3079. Taicpu(hp1).opcode:=carryadd_opcode;
  3080. result:=true;
  3081. exit;
  3082. end;
  3083. end;
  3084. if Taicpu(p).condition in [C_AE,C_NB] then
  3085. begin
  3086. if Taicpu(hp1).opcode=A_INC then
  3087. carryadd_opcode:=A_ADC;
  3088. if Taicpu(hp1).opcode=A_DEC then
  3089. carryadd_opcode:=A_SBB;
  3090. if carryadd_opcode<>A_NONE then
  3091. begin
  3092. asml.remove(p);
  3093. p.free;
  3094. Taicpu(hp1).ops:=2;
  3095. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  3096. Taicpu(hp1).loadconst(0,0);
  3097. Taicpu(hp1).opcode:=carryadd_opcode;
  3098. p:=hp1;
  3099. result:=true;
  3100. exit;
  3101. end;
  3102. end;
  3103. end;
  3104. { Detect the following:
  3105. jmp<cond> @Lbl1
  3106. jmp @Lbl2
  3107. ...
  3108. @Lbl1:
  3109. ret
  3110. Change to:
  3111. jmp<inv_cond> @Lbl2
  3112. ret
  3113. }
  3114. if MatchInstruction(hp1, A_JMP, []) then
  3115. begin
  3116. hp2 := getlabelwithsym(TAsmLabel(symbol));
  3117. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  3118. MatchInstruction(hp2,A_RET,[S_NO]) then
  3119. begin
  3120. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  3121. { Change label address to that of the unconditional jump }
  3122. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  3123. TAsmLabel(symbol).DecRefs;
  3124. taicpu(hp1).opcode := A_RET;
  3125. taicpu(hp1).is_jmp := false;
  3126. taicpu(hp1).ops := taicpu(hp2).ops;
  3127. case taicpu(hp2).ops of
  3128. 0:
  3129. taicpu(hp1).clearop(0);
  3130. 1:
  3131. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  3132. else
  3133. internalerror(2016041302);
  3134. end;
  3135. end;
  3136. end;
  3137. end;
  3138. {$ifndef i8086}
  3139. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  3140. begin
  3141. { check for
  3142. jCC xxx
  3143. <several movs>
  3144. xxx:
  3145. }
  3146. l:=0;
  3147. GetNextInstruction(p, hp1);
  3148. while assigned(hp1) and
  3149. CanBeCMOV(hp1) and
  3150. { stop on labels }
  3151. not(hp1.typ=ait_label) do
  3152. begin
  3153. inc(l);
  3154. GetNextInstruction(hp1,hp1);
  3155. end;
  3156. if assigned(hp1) then
  3157. begin
  3158. if FindLabel(tasmlabel(symbol),hp1) then
  3159. begin
  3160. if (l<=4) and (l>0) then
  3161. begin
  3162. condition:=inverse_cond(taicpu(p).condition);
  3163. GetNextInstruction(p,hp1);
  3164. repeat
  3165. if not Assigned(hp1) then
  3166. InternalError(2018062900);
  3167. taicpu(hp1).opcode:=A_CMOVcc;
  3168. taicpu(hp1).condition:=condition;
  3169. UpdateUsedRegs(hp1);
  3170. GetNextInstruction(hp1,hp1);
  3171. until not(CanBeCMOV(hp1));
  3172. { Don't decrement the reference count on the label yet, otherwise
  3173. GetNextInstruction might skip over the label if it drops to
  3174. zero. }
  3175. GetNextInstruction(hp1,hp2);
  3176. { if the label refs. reach zero, remove any alignment before the label }
  3177. if (hp1.typ = ait_align) and (hp2.typ = ait_label) then
  3178. begin
  3179. { Ref = 1 means it will drop to zero }
  3180. if (tasmlabel(symbol).getrefs=1) then
  3181. begin
  3182. asml.Remove(hp1);
  3183. hp1.Free;
  3184. end;
  3185. end
  3186. else
  3187. hp2 := hp1;
  3188. { Remember what the first hp2 is in case there's multiple aligns and labels to get rid of }
  3189. hp3 := hp2;
  3190. repeat
  3191. if not Assigned(hp2) then
  3192. InternalError(2018062910);
  3193. case hp2.typ of
  3194. ait_label:
  3195. { What we expected - break out of the loop (it won't be a dead label at the top of
  3196. a cluster because that was optimised at an earlier stage) }
  3197. Break;
  3198. ait_align:
  3199. { Go to the next entry until a label is found (may be multiple aligns before it) }
  3200. begin
  3201. hp2 := tai(hp2.Next);
  3202. Continue;
  3203. end;
  3204. else
  3205. InternalError(2018062911);
  3206. end;
  3207. until False;
  3208. { Now we can safely decrement the reference count }
  3209. tasmlabel(symbol).decrefs;
  3210. { Remove the original jump }
  3211. asml.Remove(p);
  3212. p.Free;
  3213. GetNextInstruction(hp2, p); { Instruction after the label }
  3214. { Remove the label if this is its final reference }
  3215. if (tasmlabel(symbol).getrefs=0) then
  3216. StripLabelFast(hp3);
  3217. if Assigned(p) then
  3218. begin
  3219. UpdateUsedRegs(p);
  3220. result:=true;
  3221. end;
  3222. exit;
  3223. end;
  3224. end
  3225. else
  3226. begin
  3227. { check further for
  3228. jCC xxx
  3229. <several movs 1>
  3230. jmp yyy
  3231. xxx:
  3232. <several movs 2>
  3233. yyy:
  3234. }
  3235. { hp2 points to jmp yyy }
  3236. hp2:=hp1;
  3237. { skip hp1 to xxx (or an align right before it) }
  3238. GetNextInstruction(hp1, hp1);
  3239. if assigned(hp2) and
  3240. assigned(hp1) and
  3241. (l<=3) and
  3242. (hp2.typ=ait_instruction) and
  3243. (taicpu(hp2).is_jmp) and
  3244. (taicpu(hp2).condition=C_None) and
  3245. { real label and jump, no further references to the
  3246. label are allowed }
  3247. (tasmlabel(symbol).getrefs=1) and
  3248. FindLabel(tasmlabel(symbol),hp1) then
  3249. begin
  3250. l:=0;
  3251. { skip hp1 to <several moves 2> }
  3252. if (hp1.typ = ait_align) then
  3253. GetNextInstruction(hp1, hp1);
  3254. GetNextInstruction(hp1, hpmov2);
  3255. hp1 := hpmov2;
  3256. while assigned(hp1) and
  3257. CanBeCMOV(hp1) do
  3258. begin
  3259. inc(l);
  3260. GetNextInstruction(hp1, hp1);
  3261. end;
  3262. { hp1 points to yyy (or an align right before it) }
  3263. hp3 := hp1;
  3264. if assigned(hp1) and
  3265. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  3266. begin
  3267. condition:=inverse_cond(taicpu(p).condition);
  3268. GetNextInstruction(p,hp1);
  3269. repeat
  3270. taicpu(hp1).opcode:=A_CMOVcc;
  3271. taicpu(hp1).condition:=condition;
  3272. UpdateUsedRegs(hp1);
  3273. GetNextInstruction(hp1,hp1);
  3274. until not(assigned(hp1)) or
  3275. not(CanBeCMOV(hp1));
  3276. condition:=inverse_cond(condition);
  3277. hp1 := hpmov2;
  3278. { hp1 is now at <several movs 2> }
  3279. while Assigned(hp1) and CanBeCMOV(hp1) do
  3280. begin
  3281. taicpu(hp1).opcode:=A_CMOVcc;
  3282. taicpu(hp1).condition:=condition;
  3283. UpdateUsedRegs(hp1);
  3284. GetNextInstruction(hp1,hp1);
  3285. end;
  3286. hp1 := p;
  3287. { Get first instruction after label }
  3288. GetNextInstruction(hp3, p);
  3289. if assigned(p) and (hp3.typ = ait_align) then
  3290. GetNextInstruction(p, p);
  3291. { Don't dereference yet, as doing so will cause
  3292. GetNextInstruction to skip the label and
  3293. optional align marker. [Kit] }
  3294. GetNextInstruction(hp2, hp4);
  3295. { remove jCC }
  3296. asml.remove(hp1);
  3297. hp1.free;
  3298. { Now we can safely decrement it }
  3299. tasmlabel(symbol).decrefs;
  3300. { Remove label xxx (it will have a ref of zero due to the initial check }
  3301. StripLabelFast(hp4);
  3302. { remove jmp }
  3303. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  3304. asml.remove(hp2);
  3305. hp2.free;
  3306. { As before, now we can safely decrement it }
  3307. tasmlabel(symbol).decrefs;
  3308. { Remove label yyy (and the optional alignment) if its reference falls to zero }
  3309. if tasmlabel(symbol).getrefs = 0 then
  3310. StripLabelFast(hp3);
  3311. if Assigned(p) then
  3312. begin
  3313. UpdateUsedRegs(p);
  3314. result:=true;
  3315. end;
  3316. exit;
  3317. end;
  3318. end;
  3319. end;
  3320. end;
  3321. end;
  3322. {$endif i8086}
  3323. end;
  3324. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  3325. var
  3326. hp1,hp2: tai;
  3327. begin
  3328. result:=false;
  3329. if (taicpu(p).oper[1]^.typ = top_reg) and
  3330. GetNextInstruction(p,hp1) and
  3331. (hp1.typ = ait_instruction) and
  3332. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  3333. GetNextInstruction(hp1,hp2) and
  3334. MatchInstruction(hp2,A_MOV,[]) and
  3335. (taicpu(hp2).oper[0]^.typ = top_reg) and
  3336. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  3337. {$ifdef i386}
  3338. { not all registers have byte size sub registers on i386 }
  3339. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  3340. {$endif i386}
  3341. (((taicpu(hp1).ops=2) and
  3342. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  3343. ((taicpu(hp1).ops=1) and
  3344. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  3345. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  3346. begin
  3347. { change movsX/movzX reg/ref, reg2
  3348. add/sub/or/... reg3/$const, reg2
  3349. mov reg2 reg/ref
  3350. to add/sub/or/... reg3/$const, reg/ref }
  3351. { by example:
  3352. movswl %si,%eax movswl %si,%eax p
  3353. decl %eax addl %edx,%eax hp1
  3354. movw %ax,%si movw %ax,%si hp2
  3355. ->
  3356. movswl %si,%eax movswl %si,%eax p
  3357. decw %eax addw %edx,%eax hp1
  3358. movw %ax,%si movw %ax,%si hp2
  3359. }
  3360. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  3361. {
  3362. ->
  3363. movswl %si,%eax movswl %si,%eax p
  3364. decw %si addw %dx,%si hp1
  3365. movw %ax,%si movw %ax,%si hp2
  3366. }
  3367. case taicpu(hp1).ops of
  3368. 1:
  3369. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  3370. 2:
  3371. begin
  3372. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  3373. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  3374. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  3375. end;
  3376. else
  3377. internalerror(2008042701);
  3378. end;
  3379. {
  3380. ->
  3381. decw %si addw %dx,%si p
  3382. }
  3383. DebugMsg(SPeepholeOptimization + 'var3',p);
  3384. asml.remove(p);
  3385. asml.remove(hp2);
  3386. p.free;
  3387. hp2.free;
  3388. p:=hp1;
  3389. end
  3390. else if taicpu(p).opcode=A_MOVZX then
  3391. begin
  3392. { removes superfluous And's after movzx's }
  3393. if (taicpu(p).oper[1]^.typ = top_reg) and
  3394. GetNextInstruction(p, hp1) and
  3395. (tai(hp1).typ = ait_instruction) and
  3396. (taicpu(hp1).opcode = A_AND) and
  3397. (taicpu(hp1).oper[0]^.typ = top_const) and
  3398. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3399. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3400. begin
  3401. case taicpu(p).opsize Of
  3402. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  3403. if (taicpu(hp1).oper[0]^.val = $ff) then
  3404. begin
  3405. DebugMsg(SPeepholeOptimization + 'var4',p);
  3406. asml.remove(hp1);
  3407. hp1.free;
  3408. end;
  3409. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  3410. if (taicpu(hp1).oper[0]^.val = $ffff) then
  3411. begin
  3412. DebugMsg(SPeepholeOptimization + 'var5',p);
  3413. asml.remove(hp1);
  3414. hp1.free;
  3415. end;
  3416. {$ifdef x86_64}
  3417. S_LQ:
  3418. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  3419. begin
  3420. if (cs_asm_source in current_settings.globalswitches) then
  3421. asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
  3422. asml.remove(hp1);
  3423. hp1.Free;
  3424. end;
  3425. {$endif x86_64}
  3426. else
  3427. ;
  3428. end;
  3429. end;
  3430. { changes some movzx constructs to faster synonims (all examples
  3431. are given with eax/ax, but are also valid for other registers)}
  3432. if (taicpu(p).oper[1]^.typ = top_reg) then
  3433. if (taicpu(p).oper[0]^.typ = top_reg) then
  3434. case taicpu(p).opsize of
  3435. S_BW:
  3436. begin
  3437. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3438. not(cs_opt_size in current_settings.optimizerswitches) then
  3439. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  3440. begin
  3441. taicpu(p).opcode := A_AND;
  3442. taicpu(p).changeopsize(S_W);
  3443. taicpu(p).loadConst(0,$ff);
  3444. DebugMsg(SPeepholeOptimization + 'var7',p);
  3445. end
  3446. else if GetNextInstruction(p, hp1) and
  3447. (tai(hp1).typ = ait_instruction) and
  3448. (taicpu(hp1).opcode = A_AND) and
  3449. (taicpu(hp1).oper[0]^.typ = top_const) and
  3450. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3451. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3452. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  3453. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  3454. begin
  3455. DebugMsg(SPeepholeOptimization + 'var8',p);
  3456. taicpu(p).opcode := A_MOV;
  3457. taicpu(p).changeopsize(S_W);
  3458. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  3459. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3460. end;
  3461. end;
  3462. S_BL:
  3463. begin
  3464. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3465. not(cs_opt_size in current_settings.optimizerswitches) then
  3466. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  3467. begin
  3468. taicpu(p).opcode := A_AND;
  3469. taicpu(p).changeopsize(S_L);
  3470. taicpu(p).loadConst(0,$ff)
  3471. end
  3472. else if GetNextInstruction(p, hp1) and
  3473. (tai(hp1).typ = ait_instruction) and
  3474. (taicpu(hp1).opcode = A_AND) and
  3475. (taicpu(hp1).oper[0]^.typ = top_const) and
  3476. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3477. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3478. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  3479. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  3480. begin
  3481. DebugMsg(SPeepholeOptimization + 'var10',p);
  3482. taicpu(p).opcode := A_MOV;
  3483. taicpu(p).changeopsize(S_L);
  3484. { do not use R_SUBWHOLE
  3485. as movl %rdx,%eax
  3486. is invalid in assembler PM }
  3487. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3488. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3489. end
  3490. end;
  3491. {$ifndef i8086}
  3492. S_WL:
  3493. begin
  3494. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3495. not(cs_opt_size in current_settings.optimizerswitches) then
  3496. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  3497. begin
  3498. DebugMsg(SPeepholeOptimization + 'var11',p);
  3499. taicpu(p).opcode := A_AND;
  3500. taicpu(p).changeopsize(S_L);
  3501. taicpu(p).loadConst(0,$ffff);
  3502. end
  3503. else if GetNextInstruction(p, hp1) and
  3504. (tai(hp1).typ = ait_instruction) and
  3505. (taicpu(hp1).opcode = A_AND) and
  3506. (taicpu(hp1).oper[0]^.typ = top_const) and
  3507. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3508. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3509. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  3510. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  3511. begin
  3512. DebugMsg(SPeepholeOptimization + 'var12',p);
  3513. taicpu(p).opcode := A_MOV;
  3514. taicpu(p).changeopsize(S_L);
  3515. { do not use R_SUBWHOLE
  3516. as movl %rdx,%eax
  3517. is invalid in assembler PM }
  3518. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3519. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  3520. end;
  3521. end;
  3522. {$endif i8086}
  3523. else
  3524. ;
  3525. end
  3526. else if (taicpu(p).oper[0]^.typ = top_ref) then
  3527. begin
  3528. if GetNextInstruction(p, hp1) and
  3529. (tai(hp1).typ = ait_instruction) and
  3530. (taicpu(hp1).opcode = A_AND) and
  3531. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3532. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3533. begin
  3534. //taicpu(p).opcode := A_MOV;
  3535. case taicpu(p).opsize Of
  3536. S_BL:
  3537. begin
  3538. DebugMsg(SPeepholeOptimization + 'var13',p);
  3539. taicpu(hp1).changeopsize(S_L);
  3540. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3541. end;
  3542. S_WL:
  3543. begin
  3544. DebugMsg(SPeepholeOptimization + 'var14',p);
  3545. taicpu(hp1).changeopsize(S_L);
  3546. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  3547. end;
  3548. S_BW:
  3549. begin
  3550. DebugMsg(SPeepholeOptimization + 'var15',p);
  3551. taicpu(hp1).changeopsize(S_W);
  3552. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3553. end;
  3554. {$ifdef x86_64}
  3555. S_BQ:
  3556. begin
  3557. DebugMsg(SPeepholeOptimization + 'var16',p);
  3558. taicpu(hp1).changeopsize(S_Q);
  3559. taicpu(hp1).loadConst(
  3560. 0, taicpu(hp1).oper[0]^.val and $ff);
  3561. end;
  3562. S_WQ:
  3563. begin
  3564. DebugMsg(SPeepholeOptimization + 'var17',p);
  3565. taicpu(hp1).changeopsize(S_Q);
  3566. taicpu(hp1).loadConst(0, taicpu(hp1).oper[0]^.val and $ffff);
  3567. end;
  3568. S_LQ:
  3569. begin
  3570. DebugMsg(SPeepholeOptimization + 'var18',p);
  3571. taicpu(hp1).changeopsize(S_Q);
  3572. taicpu(hp1).loadConst(
  3573. 0, taicpu(hp1).oper[0]^.val and $ffffffff);
  3574. end;
  3575. {$endif x86_64}
  3576. else
  3577. Internalerror(2017050704)
  3578. end;
  3579. end;
  3580. end;
  3581. end;
  3582. end;
  3583. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  3584. var
  3585. hp1 : tai;
  3586. MaskLength : Cardinal;
  3587. begin
  3588. Result:=false;
  3589. if GetNextInstruction(p, hp1) then
  3590. begin
  3591. if MatchOpType(taicpu(p),top_const,top_reg) and
  3592. MatchInstruction(hp1,A_AND,[]) and
  3593. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3594. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3595. { the second register must contain the first one, so compare their subreg types }
  3596. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  3597. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  3598. { change
  3599. and const1, reg
  3600. and const2, reg
  3601. to
  3602. and (const1 and const2), reg
  3603. }
  3604. begin
  3605. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  3606. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  3607. asml.remove(p);
  3608. p.Free;
  3609. p:=hp1;
  3610. Result:=true;
  3611. exit;
  3612. end
  3613. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3614. MatchInstruction(hp1,A_MOVZX,[]) and
  3615. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3616. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3617. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3618. (((taicpu(p).opsize=S_W) and
  3619. (taicpu(hp1).opsize=S_BW)) or
  3620. ((taicpu(p).opsize=S_L) and
  3621. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3622. {$ifdef x86_64}
  3623. or
  3624. ((taicpu(p).opsize=S_Q) and
  3625. (taicpu(hp1).opsize in [S_BQ,S_WQ]))
  3626. {$endif x86_64}
  3627. ) then
  3628. begin
  3629. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3630. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  3631. ) or
  3632. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3633. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  3634. then
  3635. begin
  3636. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  3637. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  3638. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  3639. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  3640. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  3641. }
  3642. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  3643. asml.remove(hp1);
  3644. hp1.free;
  3645. Exit;
  3646. end;
  3647. end
  3648. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3649. MatchInstruction(hp1,A_SHL,[]) and
  3650. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3651. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  3652. begin
  3653. {$ifopt R+}
  3654. {$define RANGE_WAS_ON}
  3655. {$R-}
  3656. {$endif}
  3657. { get length of potential and mask }
  3658. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  3659. { really a mask? }
  3660. {$ifdef RANGE_WAS_ON}
  3661. {$R+}
  3662. {$endif}
  3663. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  3664. { unmasked part shifted out? }
  3665. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  3666. begin
  3667. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  3668. { take care of the register (de)allocs following p }
  3669. UpdateUsedRegs(tai(p.next));
  3670. asml.remove(p);
  3671. p.free;
  3672. p:=hp1;
  3673. Result:=true;
  3674. exit;
  3675. end;
  3676. end
  3677. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3678. MatchInstruction(hp1,A_MOVSX{$ifdef x86_64},A_MOVSXD{$endif x86_64},[]) and
  3679. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3680. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3681. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3682. (((taicpu(p).opsize=S_W) and
  3683. (taicpu(hp1).opsize=S_BW)) or
  3684. ((taicpu(p).opsize=S_L) and
  3685. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3686. {$ifdef x86_64}
  3687. or
  3688. ((taicpu(p).opsize=S_Q) and
  3689. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_LQ]))
  3690. {$endif x86_64}
  3691. ) then
  3692. begin
  3693. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3694. ((taicpu(p).oper[0]^.val and $7f)=taicpu(p).oper[0]^.val)
  3695. ) or
  3696. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3697. ((taicpu(p).oper[0]^.val and $7fff)=taicpu(p).oper[0]^.val))
  3698. {$ifdef x86_64}
  3699. or
  3700. (((taicpu(hp1).opsize)=S_LQ) and
  3701. ((taicpu(p).oper[0]^.val and $7fffffff)=taicpu(p).oper[0]^.val)
  3702. )
  3703. {$endif x86_64}
  3704. then
  3705. begin
  3706. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  3707. asml.remove(hp1);
  3708. hp1.free;
  3709. Exit;
  3710. end;
  3711. end
  3712. else if (taicpu(p).oper[1]^.typ = top_reg) and
  3713. (hp1.typ = ait_instruction) and
  3714. (taicpu(hp1).is_jmp) and
  3715. (taicpu(hp1).opcode<>A_JMP) and
  3716. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  3717. begin
  3718. { change
  3719. and x, reg
  3720. jxx
  3721. to
  3722. test x, reg
  3723. jxx
  3724. if reg is deallocated before the
  3725. jump, but only if it's a conditional jump (PFV)
  3726. }
  3727. taicpu(p).opcode := A_TEST;
  3728. Exit;
  3729. end;
  3730. end;
  3731. { Lone AND tests }
  3732. if MatchOpType(taicpu(p),top_const,top_reg) then
  3733. begin
  3734. {
  3735. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  3736. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  3737. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  3738. }
  3739. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  3740. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  3741. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  3742. begin
  3743. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg)
  3744. end;
  3745. end;
  3746. end;
  3747. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  3748. begin
  3749. Result:=false;
  3750. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3751. MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  3752. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  3753. begin
  3754. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  3755. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  3756. taicpu(p).opcode:=A_ADD;
  3757. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  3758. result:=true;
  3759. end
  3760. else if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3761. MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  3762. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  3763. begin
  3764. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  3765. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  3766. taicpu(p).opcode:=A_ADD;
  3767. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  3768. result:=true;
  3769. end;
  3770. end;
  3771. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  3772. function SkipSimpleInstructions(var hp1 : tai) : Boolean;
  3773. begin
  3774. { we can skip all instructions not messing with the stack pointer }
  3775. while assigned(hp1) and {MatchInstruction(taicpu(hp1),[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  3776. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  3777. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  3778. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  3779. ({(taicpu(hp1).ops=0) or }
  3780. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  3781. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  3782. ) and }
  3783. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  3784. )
  3785. ) do
  3786. GetNextInstruction(hp1,hp1);
  3787. Result:=assigned(hp1);
  3788. end;
  3789. var
  3790. hp1, hp2, hp3: tai;
  3791. begin
  3792. Result:=false;
  3793. { replace
  3794. leal(q) x(<stackpointer>),<stackpointer>
  3795. call procname
  3796. leal(q) -x(<stackpointer>),<stackpointer>
  3797. ret
  3798. by
  3799. jmp procname
  3800. but do it only on level 4 because it destroys stack back traces
  3801. }
  3802. if (cs_opt_level4 in current_settings.optimizerswitches) and
  3803. MatchOpType(taicpu(p),top_ref,top_reg) and
  3804. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  3805. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  3806. { the -8 or -24 are not required, but bail out early if possible,
  3807. higher values are unlikely }
  3808. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  3809. (taicpu(p).oper[0]^.ref^.offset=-24)) and
  3810. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  3811. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  3812. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  3813. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  3814. GetNextInstruction(p, hp1) and
  3815. { trick to skip label }
  3816. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  3817. SkipSimpleInstructions(hp1) and
  3818. MatchInstruction(hp1,A_CALL,[S_NO]) and
  3819. GetNextInstruction(hp1, hp2) and
  3820. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  3821. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  3822. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  3823. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  3824. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  3825. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  3826. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  3827. (taicpu(hp2).oper[0]^.ref^.segment=NR_NO) and
  3828. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  3829. GetNextInstruction(hp2, hp3) and
  3830. { trick to skip label }
  3831. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  3832. MatchInstruction(hp3,A_RET,[S_NO]) and
  3833. (taicpu(hp3).ops=0) then
  3834. begin
  3835. taicpu(hp1).opcode := A_JMP;
  3836. taicpu(hp1).is_jmp := true;
  3837. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  3838. RemoveCurrentP(p);
  3839. AsmL.Remove(hp2);
  3840. hp2.free;
  3841. AsmL.Remove(hp3);
  3842. hp3.free;
  3843. Result:=true;
  3844. end;
  3845. end;
  3846. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  3847. var
  3848. Value, RegName: string;
  3849. begin
  3850. Result:=false;
  3851. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  3852. begin
  3853. case taicpu(p).oper[0]^.val of
  3854. 0:
  3855. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  3856. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  3857. begin
  3858. { change "mov $0,%reg" into "xor %reg,%reg" }
  3859. taicpu(p).opcode := A_XOR;
  3860. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  3861. Result := True;
  3862. end;
  3863. $1..$FFFFFFFF:
  3864. begin
  3865. { Code size reduction by J. Gareth "Kit" Moreton }
  3866. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  3867. case taicpu(p).opsize of
  3868. S_Q:
  3869. begin
  3870. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  3871. Value := debug_tostr(taicpu(p).oper[0]^.val);
  3872. { The actual optimization }
  3873. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3874. taicpu(p).changeopsize(S_L);
  3875. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  3876. Result := True;
  3877. end;
  3878. else
  3879. ;
  3880. end;
  3881. end;
  3882. end;
  3883. end;
  3884. end;
  3885. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  3886. begin
  3887. Result:=false;
  3888. { change "cmp $0, %reg" to "test %reg, %reg" }
  3889. if MatchOpType(taicpu(p),top_const,top_reg) and
  3890. (taicpu(p).oper[0]^.val = 0) then
  3891. begin
  3892. taicpu(p).opcode := A_TEST;
  3893. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  3894. Result:=true;
  3895. end;
  3896. end;
  3897. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  3898. var
  3899. IsTestConstX : Boolean;
  3900. hp1,hp2 : tai;
  3901. begin
  3902. Result:=false;
  3903. { removes the line marked with (x) from the sequence
  3904. and/or/xor/add/sub/... $x, %y
  3905. test/or %y, %y | test $-1, %y (x)
  3906. j(n)z _Label
  3907. as the first instruction already adjusts the ZF
  3908. %y operand may also be a reference }
  3909. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  3910. MatchOperand(taicpu(p).oper[0]^,-1);
  3911. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  3912. GetLastInstruction(p, hp1) and
  3913. (tai(hp1).typ = ait_instruction) and
  3914. GetNextInstruction(p,hp2) and
  3915. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  3916. case taicpu(hp1).opcode Of
  3917. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  3918. begin
  3919. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  3920. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3921. { and in case of carry for A(E)/B(E)/C/NC }
  3922. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  3923. ((taicpu(hp1).opcode <> A_ADD) and
  3924. (taicpu(hp1).opcode <> A_SUB))) then
  3925. begin
  3926. hp1 := tai(p.next);
  3927. asml.remove(p);
  3928. p.free;
  3929. p := tai(hp1);
  3930. Result:=true;
  3931. end;
  3932. end;
  3933. A_SHL, A_SAL, A_SHR, A_SAR:
  3934. begin
  3935. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  3936. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  3937. { therefore, it's only safe to do this optimization for }
  3938. { shifts by a (nonzero) constant }
  3939. (taicpu(hp1).oper[0]^.typ = top_const) and
  3940. (taicpu(hp1).oper[0]^.val <> 0) and
  3941. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3942. { and in case of carry for A(E)/B(E)/C/NC }
  3943. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  3944. begin
  3945. hp1 := tai(p.next);
  3946. asml.remove(p);
  3947. p.free;
  3948. p := tai(hp1);
  3949. Result:=true;
  3950. end;
  3951. end;
  3952. A_DEC, A_INC, A_NEG:
  3953. begin
  3954. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  3955. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3956. { and in case of carry for A(E)/B(E)/C/NC }
  3957. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  3958. begin
  3959. case taicpu(hp1).opcode of
  3960. A_DEC, A_INC:
  3961. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  3962. begin
  3963. case taicpu(hp1).opcode Of
  3964. A_DEC: taicpu(hp1).opcode := A_SUB;
  3965. A_INC: taicpu(hp1).opcode := A_ADD;
  3966. else
  3967. ;
  3968. end;
  3969. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  3970. taicpu(hp1).loadConst(0,1);
  3971. taicpu(hp1).ops:=2;
  3972. end;
  3973. else
  3974. ;
  3975. end;
  3976. hp1 := tai(p.next);
  3977. asml.remove(p);
  3978. p.free;
  3979. p := tai(hp1);
  3980. Result:=true;
  3981. end;
  3982. end
  3983. else
  3984. { change "test $-1,%reg" into "test %reg,%reg" }
  3985. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  3986. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  3987. end { case }
  3988. { change "test $-1,%reg" into "test %reg,%reg" }
  3989. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  3990. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  3991. end;
  3992. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  3993. var
  3994. hp1 : tai;
  3995. {$ifndef x86_64}
  3996. hp2 : taicpu;
  3997. {$endif x86_64}
  3998. begin
  3999. Result:=false;
  4000. {$ifndef x86_64}
  4001. { don't do this on modern CPUs, this really hurts them due to
  4002. broken call/ret pairing }
  4003. if (current_settings.optimizecputype < cpu_Pentium2) and
  4004. not(cs_create_pic in current_settings.moduleswitches) and
  4005. GetNextInstruction(p, hp1) and
  4006. MatchInstruction(hp1,A_JMP,[S_NO]) and
  4007. MatchOpType(taicpu(hp1),top_ref) and
  4008. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  4009. begin
  4010. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  4011. InsertLLItem(p.previous, p, hp2);
  4012. taicpu(p).opcode := A_JMP;
  4013. taicpu(p).is_jmp := true;
  4014. asml.remove(hp1);
  4015. hp1.free;
  4016. Result:=true;
  4017. end
  4018. else
  4019. {$endif x86_64}
  4020. { replace
  4021. call procname
  4022. ret
  4023. by
  4024. jmp procname
  4025. but do it only on level 4 because it destroys stack back traces
  4026. }
  4027. if (cs_opt_level4 in current_settings.optimizerswitches) and
  4028. GetNextInstruction(p, hp1) and
  4029. MatchInstruction(hp1,A_RET,[S_NO]) and
  4030. (taicpu(hp1).ops=0) then
  4031. begin
  4032. taicpu(p).opcode := A_JMP;
  4033. taicpu(p).is_jmp := true;
  4034. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  4035. asml.remove(hp1);
  4036. hp1.free;
  4037. Result:=true;
  4038. end;
  4039. end;
  4040. {$ifdef x86_64}
  4041. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  4042. var
  4043. PreMessage: string;
  4044. begin
  4045. Result := False;
  4046. { Code size reduction by J. Gareth "Kit" Moreton }
  4047. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  4048. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  4049. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  4050. then
  4051. begin
  4052. { Has 64-bit register name and opcode suffix }
  4053. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  4054. { The actual optimization }
  4055. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  4056. if taicpu(p).opsize = S_BQ then
  4057. taicpu(p).changeopsize(S_BL)
  4058. else
  4059. taicpu(p).changeopsize(S_WL);
  4060. DebugMsg(SPeepholeOptimization + PreMessage +
  4061. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  4062. end;
  4063. end;
  4064. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  4065. var
  4066. PreMessage, RegName: string;
  4067. begin
  4068. { Code size reduction by J. Gareth "Kit" Moreton }
  4069. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  4070. as this removes the REX prefix }
  4071. Result := False;
  4072. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  4073. Exit;
  4074. if taicpu(p).oper[0]^.typ <> top_reg then
  4075. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  4076. InternalError(2018011500);
  4077. case taicpu(p).opsize of
  4078. S_Q:
  4079. begin
  4080. 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
  4081. begin
  4082. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  4083. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  4084. { The actual optimization }
  4085. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  4086. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  4087. taicpu(p).changeopsize(S_L);
  4088. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  4089. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  4090. end;
  4091. end;
  4092. else
  4093. ;
  4094. end;
  4095. end;
  4096. {$endif}
  4097. procedure TX86AsmOptimizer.OptReferences;
  4098. var
  4099. p: tai;
  4100. i: Integer;
  4101. begin
  4102. p := BlockStart;
  4103. while (p <> BlockEnd) Do
  4104. begin
  4105. if p.typ=ait_instruction then
  4106. begin
  4107. for i:=0 to taicpu(p).ops-1 do
  4108. if taicpu(p).oper[i]^.typ=top_ref then
  4109. optimize_ref(taicpu(p).oper[i]^.ref^,false);
  4110. end;
  4111. p:=tai(p.next);
  4112. end;
  4113. end;
  4114. end.