aoptx86.pas 159 KB

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