aoptx86.pas 130 KB

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