aoptx86.pas 129 KB

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