aoptx86.pas 160 KB

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