aoptx86.pas 152 KB

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