aoptx86.pas 155 KB

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