aoptx86.pas 158 KB

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