aoptx86.pas 129 KB

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