aoptx86.pas 159 KB

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