aoptx86.pas 152 KB

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