aoptx86.pas 159 KB

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