X86InstrCompiler.td 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  1. //===- X86InstrCompiler.td - Compiler Pseudos and Patterns -*- tablegen -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file describes the various pseudo instructions used by the compiler,
  10. // as well as Pat patterns used during instruction selection.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //===----------------------------------------------------------------------===//
  14. // Pattern Matching Support
  15. def GetLo32XForm : SDNodeXForm<imm, [{
  16. // Transformation function: get the low 32 bits.
  17. return getI32Imm((uint32_t)N->getZExtValue(), SDLoc(N));
  18. }]>;
  19. //===----------------------------------------------------------------------===//
  20. // Random Pseudo Instructions.
  21. // PIC base construction. This expands to code that looks like this:
  22. // call $next_inst
  23. // popl %destreg"
  24. let hasSideEffects = 0, isNotDuplicable = 1, Uses = [ESP, SSP],
  25. SchedRW = [WriteJump] in
  26. def MOVPC32r : Ii32<0xE8, Pseudo, (outs GR32:$reg), (ins i32imm:$label),
  27. "", []>;
  28. // ADJCALLSTACKDOWN/UP implicitly use/def ESP because they may be expanded into
  29. // a stack adjustment and the codegen must know that they may modify the stack
  30. // pointer before prolog-epilog rewriting occurs.
  31. // Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become
  32. // sub / add which can clobber EFLAGS.
  33. let Defs = [ESP, EFLAGS, SSP], Uses = [ESP, SSP], SchedRW = [WriteALU] in {
  34. def ADJCALLSTACKDOWN32 : I<0, Pseudo, (outs),
  35. (ins i32imm:$amt1, i32imm:$amt2, i32imm:$amt3),
  36. "#ADJCALLSTACKDOWN", []>, Requires<[NotLP64]>;
  37. def ADJCALLSTACKUP32 : I<0, Pseudo, (outs), (ins i32imm:$amt1, i32imm:$amt2),
  38. "#ADJCALLSTACKUP",
  39. [(X86callseq_end timm:$amt1, timm:$amt2)]>,
  40. Requires<[NotLP64]>;
  41. }
  42. def : Pat<(X86callseq_start timm:$amt1, timm:$amt2),
  43. (ADJCALLSTACKDOWN32 i32imm:$amt1, i32imm:$amt2, 0)>, Requires<[NotLP64]>;
  44. // ADJCALLSTACKDOWN/UP implicitly use/def RSP because they may be expanded into
  45. // a stack adjustment and the codegen must know that they may modify the stack
  46. // pointer before prolog-epilog rewriting occurs.
  47. // Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become
  48. // sub / add which can clobber EFLAGS.
  49. let Defs = [RSP, EFLAGS, SSP], Uses = [RSP, SSP], SchedRW = [WriteALU] in {
  50. def ADJCALLSTACKDOWN64 : I<0, Pseudo, (outs),
  51. (ins i32imm:$amt1, i32imm:$amt2, i32imm:$amt3),
  52. "#ADJCALLSTACKDOWN", []>, Requires<[IsLP64]>;
  53. def ADJCALLSTACKUP64 : I<0, Pseudo, (outs), (ins i32imm:$amt1, i32imm:$amt2),
  54. "#ADJCALLSTACKUP",
  55. [(X86callseq_end timm:$amt1, timm:$amt2)]>,
  56. Requires<[IsLP64]>;
  57. }
  58. def : Pat<(X86callseq_start timm:$amt1, timm:$amt2),
  59. (ADJCALLSTACKDOWN64 i32imm:$amt1, i32imm:$amt2, 0)>, Requires<[IsLP64]>;
  60. let SchedRW = [WriteSystem] in {
  61. // x86-64 va_start lowering magic.
  62. let hasSideEffects = 1, mayStore = 1, Defs = [EFLAGS] in {
  63. def VASTART_SAVE_XMM_REGS : I<0, Pseudo,
  64. (outs),
  65. (ins GR8:$al, i8mem:$regsavefi, variable_ops),
  66. "#VASTART_SAVE_XMM_REGS $al, $regsavefi",
  67. [(X86vastart_save_xmm_regs GR8:$al, addr:$regsavefi),
  68. (implicit EFLAGS)]>;
  69. }
  70. let usesCustomInserter = 1, Defs = [EFLAGS] in {
  71. // The VAARG_64 and VAARG_X32 pseudo-instructions take the address of the
  72. // va_list, and place the address of the next argument into a register.
  73. let Defs = [EFLAGS] in {
  74. def VAARG_64 : I<0, Pseudo,
  75. (outs GR64:$dst),
  76. (ins i8mem:$ap, i32imm:$size, i8imm:$mode, i32imm:$align),
  77. "#VAARG_64 $dst, $ap, $size, $mode, $align",
  78. [(set GR64:$dst,
  79. (X86vaarg64 addr:$ap, timm:$size, timm:$mode, timm:$align)),
  80. (implicit EFLAGS)]>, Requires<[In64BitMode, IsLP64]>;
  81. def VAARG_X32 : I<0, Pseudo,
  82. (outs GR32:$dst),
  83. (ins i8mem:$ap, i32imm:$size, i8imm:$mode, i32imm:$align),
  84. "#VAARG_X32 $dst, $ap, $size, $mode, $align",
  85. [(set GR32:$dst,
  86. (X86vaargx32 addr:$ap, timm:$size, timm:$mode, timm:$align)),
  87. (implicit EFLAGS)]>, Requires<[In64BitMode, NotLP64]>;
  88. }
  89. // When using segmented stacks these are lowered into instructions which first
  90. // check if the current stacklet has enough free memory. If it does, memory is
  91. // allocated by bumping the stack pointer. Otherwise memory is allocated from
  92. // the heap.
  93. let Defs = [EAX, ESP, EFLAGS], Uses = [ESP] in
  94. def SEG_ALLOCA_32 : I<0, Pseudo, (outs GR32:$dst), (ins GR32:$size),
  95. "# variable sized alloca for segmented stacks",
  96. [(set GR32:$dst,
  97. (X86SegAlloca GR32:$size))]>,
  98. Requires<[NotLP64]>;
  99. let Defs = [RAX, RSP, EFLAGS], Uses = [RSP] in
  100. def SEG_ALLOCA_64 : I<0, Pseudo, (outs GR64:$dst), (ins GR64:$size),
  101. "# variable sized alloca for segmented stacks",
  102. [(set GR64:$dst,
  103. (X86SegAlloca GR64:$size))]>,
  104. Requires<[In64BitMode]>;
  105. // To protect against stack clash, dynamic allocation should perform a memory
  106. // probe at each page.
  107. let Defs = [EAX, ESP, EFLAGS], Uses = [ESP] in
  108. def PROBED_ALLOCA_32 : I<0, Pseudo, (outs GR32:$dst), (ins GR32:$size),
  109. "# variable sized alloca with probing",
  110. [(set GR32:$dst,
  111. (X86ProbedAlloca GR32:$size))]>,
  112. Requires<[NotLP64]>;
  113. let Defs = [RAX, RSP, EFLAGS], Uses = [RSP] in
  114. def PROBED_ALLOCA_64 : I<0, Pseudo, (outs GR64:$dst), (ins GR64:$size),
  115. "# variable sized alloca with probing",
  116. [(set GR64:$dst,
  117. (X86ProbedAlloca GR64:$size))]>,
  118. Requires<[In64BitMode]>;
  119. }
  120. let hasNoSchedulingInfo = 1 in
  121. def STACKALLOC_W_PROBING : I<0, Pseudo, (outs), (ins i64imm:$stacksize),
  122. "# fixed size alloca with probing",
  123. []>;
  124. // Dynamic stack allocation yields a _chkstk or _alloca call for all Windows
  125. // targets. These calls are needed to probe the stack when allocating more than
  126. // 4k bytes in one go. Touching the stack at 4K increments is necessary to
  127. // ensure that the guard pages used by the OS virtual memory manager are
  128. // allocated in correct sequence.
  129. // The main point of having separate instruction are extra unmodelled effects
  130. // (compared to ordinary calls) like stack pointer change.
  131. let Defs = [EAX, ESP, EFLAGS], Uses = [ESP] in
  132. def DYN_ALLOCA_32 : I<0, Pseudo, (outs), (ins GR32:$size),
  133. "# dynamic stack allocation",
  134. [(X86DynAlloca GR32:$size)]>,
  135. Requires<[NotLP64]>;
  136. let Defs = [RAX, RSP, EFLAGS], Uses = [RSP] in
  137. def DYN_ALLOCA_64 : I<0, Pseudo, (outs), (ins GR64:$size),
  138. "# dynamic stack allocation",
  139. [(X86DynAlloca GR64:$size)]>,
  140. Requires<[In64BitMode]>;
  141. } // SchedRW
  142. // These instructions XOR the frame pointer into a GPR. They are used in some
  143. // stack protection schemes. These are post-RA pseudos because we only know the
  144. // frame register after register allocation.
  145. let Constraints = "$src = $dst", isMoveImm = 1, isPseudo = 1, Defs = [EFLAGS] in {
  146. def XOR32_FP : I<0, Pseudo, (outs GR32:$dst), (ins GR32:$src),
  147. "xorl\t$$FP, $src", []>,
  148. Requires<[NotLP64]>, Sched<[WriteALU]>;
  149. def XOR64_FP : I<0, Pseudo, (outs GR64:$dst), (ins GR64:$src),
  150. "xorq\t$$FP $src", []>,
  151. Requires<[In64BitMode]>, Sched<[WriteALU]>;
  152. }
  153. //===----------------------------------------------------------------------===//
  154. // EH Pseudo Instructions
  155. //
  156. let SchedRW = [WriteSystem] in {
  157. let isTerminator = 1, isReturn = 1, isBarrier = 1,
  158. hasCtrlDep = 1, isCodeGenOnly = 1 in {
  159. def EH_RETURN : I<0xC3, RawFrm, (outs), (ins GR32:$addr),
  160. "ret\t#eh_return, addr: $addr",
  161. [(X86ehret GR32:$addr)]>, Sched<[WriteJumpLd]>;
  162. }
  163. let isTerminator = 1, isReturn = 1, isBarrier = 1,
  164. hasCtrlDep = 1, isCodeGenOnly = 1 in {
  165. def EH_RETURN64 : I<0xC3, RawFrm, (outs), (ins GR64:$addr),
  166. "ret\t#eh_return, addr: $addr",
  167. [(X86ehret GR64:$addr)]>, Sched<[WriteJumpLd]>;
  168. }
  169. let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
  170. isCodeGenOnly = 1, isReturn = 1, isEHScopeReturn = 1 in {
  171. def CLEANUPRET : I<0, Pseudo, (outs), (ins), "# CLEANUPRET", [(cleanupret)]>;
  172. // CATCHRET needs a custom inserter for SEH.
  173. let usesCustomInserter = 1 in
  174. def CATCHRET : I<0, Pseudo, (outs), (ins brtarget32:$dst, brtarget32:$from),
  175. "# CATCHRET",
  176. [(catchret bb:$dst, bb:$from)]>;
  177. }
  178. let hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1,
  179. usesCustomInserter = 1 in {
  180. def EH_SjLj_SetJmp32 : I<0, Pseudo, (outs GR32:$dst), (ins i32mem:$buf),
  181. "#EH_SJLJ_SETJMP32",
  182. [(set GR32:$dst, (X86eh_sjlj_setjmp addr:$buf))]>,
  183. Requires<[Not64BitMode]>;
  184. def EH_SjLj_SetJmp64 : I<0, Pseudo, (outs GR32:$dst), (ins i64mem:$buf),
  185. "#EH_SJLJ_SETJMP64",
  186. [(set GR32:$dst, (X86eh_sjlj_setjmp addr:$buf))]>,
  187. Requires<[In64BitMode]>;
  188. let isTerminator = 1 in {
  189. def EH_SjLj_LongJmp32 : I<0, Pseudo, (outs), (ins i32mem:$buf),
  190. "#EH_SJLJ_LONGJMP32",
  191. [(X86eh_sjlj_longjmp addr:$buf)]>,
  192. Requires<[Not64BitMode]>;
  193. def EH_SjLj_LongJmp64 : I<0, Pseudo, (outs), (ins i64mem:$buf),
  194. "#EH_SJLJ_LONGJMP64",
  195. [(X86eh_sjlj_longjmp addr:$buf)]>,
  196. Requires<[In64BitMode]>;
  197. }
  198. }
  199. let isBranch = 1, isTerminator = 1, isCodeGenOnly = 1 in {
  200. def EH_SjLj_Setup : I<0, Pseudo, (outs), (ins brtarget:$dst),
  201. "#EH_SjLj_Setup\t$dst", []>;
  202. }
  203. } // SchedRW
  204. //===----------------------------------------------------------------------===//
  205. // Pseudo instructions used by unwind info.
  206. //
  207. let isPseudo = 1, SchedRW = [WriteSystem] in {
  208. def SEH_PushReg : I<0, Pseudo, (outs), (ins i32imm:$reg),
  209. "#SEH_PushReg $reg", []>;
  210. def SEH_SaveReg : I<0, Pseudo, (outs), (ins i32imm:$reg, i32imm:$dst),
  211. "#SEH_SaveReg $reg, $dst", []>;
  212. def SEH_SaveXMM : I<0, Pseudo, (outs), (ins i32imm:$reg, i32imm:$dst),
  213. "#SEH_SaveXMM $reg, $dst", []>;
  214. def SEH_StackAlloc : I<0, Pseudo, (outs), (ins i32imm:$size),
  215. "#SEH_StackAlloc $size", []>;
  216. def SEH_StackAlign : I<0, Pseudo, (outs), (ins i32imm:$align),
  217. "#SEH_StackAlign $align", []>;
  218. def SEH_SetFrame : I<0, Pseudo, (outs), (ins i32imm:$reg, i32imm:$offset),
  219. "#SEH_SetFrame $reg, $offset", []>;
  220. def SEH_PushFrame : I<0, Pseudo, (outs), (ins i1imm:$mode),
  221. "#SEH_PushFrame $mode", []>;
  222. def SEH_EndPrologue : I<0, Pseudo, (outs), (ins),
  223. "#SEH_EndPrologue", []>;
  224. def SEH_Epilogue : I<0, Pseudo, (outs), (ins),
  225. "#SEH_Epilogue", []>;
  226. }
  227. //===----------------------------------------------------------------------===//
  228. // Pseudo instructions used by KCFI.
  229. //===----------------------------------------------------------------------===//
  230. let
  231. Defs = [R10, R11, EFLAGS] in {
  232. def KCFI_CHECK : PseudoI<
  233. (outs), (ins GR64:$ptr, i32imm:$type), []>, Sched<[]>;
  234. }
  235. //===----------------------------------------------------------------------===//
  236. // Pseudo instructions used by address sanitizer.
  237. //===----------------------------------------------------------------------===//
  238. let
  239. Defs = [R10, R11, EFLAGS] in {
  240. def ASAN_CHECK_MEMACCESS : PseudoI<
  241. (outs), (ins GR64PLTSafe:$addr, i32imm:$accessinfo),
  242. [(int_asan_check_memaccess GR64PLTSafe:$addr, (i32 timm:$accessinfo))]>,
  243. Sched<[]>;
  244. }
  245. //===----------------------------------------------------------------------===//
  246. // Pseudo instructions used by segmented stacks.
  247. //
  248. // This is lowered into a RET instruction by MCInstLower. We need
  249. // this so that we don't have to have a MachineBasicBlock which ends
  250. // with a RET and also has successors.
  251. let isPseudo = 1, SchedRW = [WriteJumpLd] in {
  252. def MORESTACK_RET: I<0, Pseudo, (outs), (ins), "", []>;
  253. // This instruction is lowered to a RET followed by a MOV. The two
  254. // instructions are not generated on a higher level since then the
  255. // verifier sees a MachineBasicBlock ending with a non-terminator.
  256. def MORESTACK_RET_RESTORE_R10 : I<0, Pseudo, (outs), (ins), "", []>;
  257. }
  258. //===----------------------------------------------------------------------===//
  259. // Alias Instructions
  260. //===----------------------------------------------------------------------===//
  261. // Alias instruction mapping movr0 to xor.
  262. // FIXME: remove when we can teach regalloc that xor reg, reg is ok.
  263. let Defs = [EFLAGS], isReMaterializable = 1, isAsCheapAsAMove = 1,
  264. isPseudo = 1, isMoveImm = 1, AddedComplexity = 10 in
  265. def MOV32r0 : I<0, Pseudo, (outs GR32:$dst), (ins), "",
  266. [(set GR32:$dst, 0)]>, Sched<[WriteZero]>;
  267. // Other widths can also make use of the 32-bit xor, which may have a smaller
  268. // encoding and avoid partial register updates.
  269. let AddedComplexity = 10 in {
  270. def : Pat<(i8 0), (EXTRACT_SUBREG (MOV32r0), sub_8bit)>;
  271. def : Pat<(i16 0), (EXTRACT_SUBREG (MOV32r0), sub_16bit)>;
  272. def : Pat<(i64 0), (SUBREG_TO_REG (i64 0), (MOV32r0), sub_32bit)>;
  273. }
  274. let Predicates = [OptForSize, Not64BitMode],
  275. AddedComplexity = 10 in {
  276. let SchedRW = [WriteALU] in {
  277. // Pseudo instructions for materializing 1 and -1 using XOR+INC/DEC,
  278. // which only require 3 bytes compared to MOV32ri which requires 5.
  279. let Defs = [EFLAGS], isReMaterializable = 1, isPseudo = 1 in {
  280. def MOV32r1 : I<0, Pseudo, (outs GR32:$dst), (ins), "",
  281. [(set GR32:$dst, 1)]>;
  282. def MOV32r_1 : I<0, Pseudo, (outs GR32:$dst), (ins), "",
  283. [(set GR32:$dst, -1)]>;
  284. }
  285. } // SchedRW
  286. // MOV16ri is 4 bytes, so the instructions above are smaller.
  287. def : Pat<(i16 1), (EXTRACT_SUBREG (MOV32r1), sub_16bit)>;
  288. def : Pat<(i16 -1), (EXTRACT_SUBREG (MOV32r_1), sub_16bit)>;
  289. }
  290. let isReMaterializable = 1, isPseudo = 1, AddedComplexity = 5,
  291. SchedRW = [WriteALU] in {
  292. // AddedComplexity higher than MOV64ri but lower than MOV32r0 and MOV32r1.
  293. def MOV32ImmSExti8 : I<0, Pseudo, (outs GR32:$dst), (ins i32i8imm:$src), "",
  294. [(set GR32:$dst, i32immSExt8:$src)]>,
  295. Requires<[OptForMinSize, NotWin64WithoutFP]>;
  296. def MOV64ImmSExti8 : I<0, Pseudo, (outs GR64:$dst), (ins i64i8imm:$src), "",
  297. [(set GR64:$dst, i64immSExt8:$src)]>,
  298. Requires<[OptForMinSize, NotWin64WithoutFP]>;
  299. }
  300. // Materialize i64 constant where top 32-bits are zero. This could theoretically
  301. // use MOV32ri with a SUBREG_TO_REG to represent the zero-extension, however
  302. // that would make it more difficult to rematerialize.
  303. let AddedComplexity = 1, isReMaterializable = 1, isAsCheapAsAMove = 1,
  304. isPseudo = 1, SchedRW = [WriteMove] in
  305. def MOV32ri64 : I<0, Pseudo, (outs GR64:$dst), (ins i64i32imm:$src), "",
  306. [(set GR64:$dst, i64immZExt32:$src)]>;
  307. // This 64-bit pseudo-move can also be used for labels in the x86-64 small code
  308. // model.
  309. def mov64imm32 : ComplexPattern<i64, 1, "selectMOV64Imm32", [X86Wrapper]>;
  310. def : Pat<(i64 mov64imm32:$src), (MOV32ri64 mov64imm32:$src)>;
  311. // Use sbb to materialize carry bit.
  312. let Uses = [EFLAGS], Defs = [EFLAGS], isPseudo = 1, SchedRW = [WriteADC],
  313. hasSideEffects = 0 in {
  314. // FIXME: These are pseudo ops that should be replaced with Pat<> patterns.
  315. // However, Pat<> can't replicate the destination reg into the inputs of the
  316. // result.
  317. def SETB_C32r : I<0, Pseudo, (outs GR32:$dst), (ins), "", []>;
  318. def SETB_C64r : I<0, Pseudo, (outs GR64:$dst), (ins), "", []>;
  319. } // isCodeGenOnly
  320. //===----------------------------------------------------------------------===//
  321. // String Pseudo Instructions
  322. //
  323. let SchedRW = [WriteMicrocoded] in {
  324. let Defs = [ECX,EDI,ESI], Uses = [ECX,EDI,ESI], isCodeGenOnly = 1 in {
  325. def REP_MOVSB_32 : I<0xA4, RawFrm, (outs), (ins),
  326. "{rep;movsb (%esi), %es:(%edi)|rep movsb es:[edi], [esi]}",
  327. [(X86rep_movs i8)]>, REP, AdSize32,
  328. Requires<[NotLP64]>;
  329. def REP_MOVSW_32 : I<0xA5, RawFrm, (outs), (ins),
  330. "{rep;movsw (%esi), %es:(%edi)|rep movsw es:[edi], [esi]}",
  331. [(X86rep_movs i16)]>, REP, AdSize32, OpSize16,
  332. Requires<[NotLP64]>;
  333. def REP_MOVSD_32 : I<0xA5, RawFrm, (outs), (ins),
  334. "{rep;movsl (%esi), %es:(%edi)|rep movsd es:[edi], [esi]}",
  335. [(X86rep_movs i32)]>, REP, AdSize32, OpSize32,
  336. Requires<[NotLP64]>;
  337. def REP_MOVSQ_32 : RI<0xA5, RawFrm, (outs), (ins),
  338. "{rep;movsq (%esi), %es:(%edi)|rep movsq es:[edi], [esi]}",
  339. [(X86rep_movs i64)]>, REP, AdSize32,
  340. Requires<[NotLP64, In64BitMode]>;
  341. }
  342. let Defs = [RCX,RDI,RSI], Uses = [RCX,RDI,RSI], isCodeGenOnly = 1 in {
  343. def REP_MOVSB_64 : I<0xA4, RawFrm, (outs), (ins),
  344. "{rep;movsb (%rsi), %es:(%rdi)|rep movsb es:[rdi], [rsi]}",
  345. [(X86rep_movs i8)]>, REP, AdSize64,
  346. Requires<[IsLP64]>;
  347. def REP_MOVSW_64 : I<0xA5, RawFrm, (outs), (ins),
  348. "{rep;movsw (%rsi), %es:(%rdi)|rep movsw es:[rdi], [rsi]}",
  349. [(X86rep_movs i16)]>, REP, AdSize64, OpSize16,
  350. Requires<[IsLP64]>;
  351. def REP_MOVSD_64 : I<0xA5, RawFrm, (outs), (ins),
  352. "{rep;movsl (%rsi), %es:(%rdi)|rep movsdi es:[rdi], [rsi]}",
  353. [(X86rep_movs i32)]>, REP, AdSize64, OpSize32,
  354. Requires<[IsLP64]>;
  355. def REP_MOVSQ_64 : RI<0xA5, RawFrm, (outs), (ins),
  356. "{rep;movsq (%rsi), %es:(%rdi)|rep movsq es:[rdi], [rsi]}",
  357. [(X86rep_movs i64)]>, REP, AdSize64,
  358. Requires<[IsLP64]>;
  359. }
  360. // FIXME: Should use "(X86rep_stos AL)" as the pattern.
  361. let Defs = [ECX,EDI], isCodeGenOnly = 1 in {
  362. let Uses = [AL,ECX,EDI] in
  363. def REP_STOSB_32 : I<0xAA, RawFrm, (outs), (ins),
  364. "{rep;stosb %al, %es:(%edi)|rep stosb es:[edi], al}",
  365. [(X86rep_stos i8)]>, REP, AdSize32,
  366. Requires<[NotLP64]>;
  367. let Uses = [AX,ECX,EDI] in
  368. def REP_STOSW_32 : I<0xAB, RawFrm, (outs), (ins),
  369. "{rep;stosw %ax, %es:(%edi)|rep stosw es:[edi], ax}",
  370. [(X86rep_stos i16)]>, REP, AdSize32, OpSize16,
  371. Requires<[NotLP64]>;
  372. let Uses = [EAX,ECX,EDI] in
  373. def REP_STOSD_32 : I<0xAB, RawFrm, (outs), (ins),
  374. "{rep;stosl %eax, %es:(%edi)|rep stosd es:[edi], eax}",
  375. [(X86rep_stos i32)]>, REP, AdSize32, OpSize32,
  376. Requires<[NotLP64]>;
  377. let Uses = [RAX,RCX,RDI] in
  378. def REP_STOSQ_32 : RI<0xAB, RawFrm, (outs), (ins),
  379. "{rep;stosq %rax, %es:(%edi)|rep stosq es:[edi], rax}",
  380. [(X86rep_stos i64)]>, REP, AdSize32,
  381. Requires<[NotLP64, In64BitMode]>;
  382. }
  383. let Defs = [RCX,RDI], isCodeGenOnly = 1 in {
  384. let Uses = [AL,RCX,RDI] in
  385. def REP_STOSB_64 : I<0xAA, RawFrm, (outs), (ins),
  386. "{rep;stosb %al, %es:(%rdi)|rep stosb es:[rdi], al}",
  387. [(X86rep_stos i8)]>, REP, AdSize64,
  388. Requires<[IsLP64]>;
  389. let Uses = [AX,RCX,RDI] in
  390. def REP_STOSW_64 : I<0xAB, RawFrm, (outs), (ins),
  391. "{rep;stosw %ax, %es:(%rdi)|rep stosw es:[rdi], ax}",
  392. [(X86rep_stos i16)]>, REP, AdSize64, OpSize16,
  393. Requires<[IsLP64]>;
  394. let Uses = [RAX,RCX,RDI] in
  395. def REP_STOSD_64 : I<0xAB, RawFrm, (outs), (ins),
  396. "{rep;stosl %eax, %es:(%rdi)|rep stosd es:[rdi], eax}",
  397. [(X86rep_stos i32)]>, REP, AdSize64, OpSize32,
  398. Requires<[IsLP64]>;
  399. let Uses = [RAX,RCX,RDI] in
  400. def REP_STOSQ_64 : RI<0xAB, RawFrm, (outs), (ins),
  401. "{rep;stosq %rax, %es:(%rdi)|rep stosq es:[rdi], rax}",
  402. [(X86rep_stos i64)]>, REP, AdSize64,
  403. Requires<[IsLP64]>;
  404. }
  405. } // SchedRW
  406. //===----------------------------------------------------------------------===//
  407. // Thread Local Storage Instructions
  408. //
  409. let SchedRW = [WriteSystem] in {
  410. // ELF TLS Support
  411. // All calls clobber the non-callee saved registers. ESP is marked as
  412. // a use to prevent stack-pointer assignments that appear immediately
  413. // before calls from potentially appearing dead.
  414. let Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6, FP7,
  415. ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7,
  416. MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
  417. XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
  418. XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS, DF],
  419. usesCustomInserter = 1, Uses = [ESP, SSP] in {
  420. def TLS_addr32 : I<0, Pseudo, (outs), (ins i32mem:$sym),
  421. "# TLS_addr32",
  422. [(X86tlsaddr tls32addr:$sym)]>,
  423. Requires<[Not64BitMode]>;
  424. def TLS_base_addr32 : I<0, Pseudo, (outs), (ins i32mem:$sym),
  425. "# TLS_base_addr32",
  426. [(X86tlsbaseaddr tls32baseaddr:$sym)]>,
  427. Requires<[Not64BitMode]>;
  428. }
  429. // All calls clobber the non-callee saved registers. RSP is marked as
  430. // a use to prevent stack-pointer assignments that appear immediately
  431. // before calls from potentially appearing dead.
  432. let Defs = [RAX, RCX, RDX, RSI, RDI, R8, R9, R10, R11,
  433. FP0, FP1, FP2, FP3, FP4, FP5, FP6, FP7,
  434. ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7,
  435. MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
  436. XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
  437. XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS, DF],
  438. usesCustomInserter = 1, Uses = [RSP, SSP] in {
  439. def TLS_addr64 : I<0, Pseudo, (outs), (ins i64mem:$sym),
  440. "# TLS_addr64",
  441. [(X86tlsaddr tls64addr:$sym)]>,
  442. Requires<[In64BitMode, IsLP64]>;
  443. def TLS_base_addr64 : I<0, Pseudo, (outs), (ins i64mem:$sym),
  444. "# TLS_base_addr64",
  445. [(X86tlsbaseaddr tls64baseaddr:$sym)]>,
  446. Requires<[In64BitMode, IsLP64]>;
  447. def TLS_addrX32 : I<0, Pseudo, (outs), (ins i32mem:$sym),
  448. "# TLS_addrX32",
  449. [(X86tlsaddr tls32addr:$sym)]>,
  450. Requires<[In64BitMode, NotLP64]>;
  451. def TLS_base_addrX32 : I<0, Pseudo, (outs), (ins i32mem:$sym),
  452. "# TLS_base_addrX32",
  453. [(X86tlsbaseaddr tls32baseaddr:$sym)]>,
  454. Requires<[In64BitMode, NotLP64]>;
  455. }
  456. // Darwin TLS Support
  457. // For i386, the address of the thunk is passed on the stack, on return the
  458. // address of the variable is in %eax. %ecx is trashed during the function
  459. // call. All other registers are preserved.
  460. let Defs = [EAX, ECX, EFLAGS, DF],
  461. Uses = [ESP, SSP],
  462. usesCustomInserter = 1 in
  463. def TLSCall_32 : I<0, Pseudo, (outs), (ins i32mem:$sym),
  464. "# TLSCall_32",
  465. [(X86TLSCall addr:$sym)]>,
  466. Requires<[Not64BitMode]>;
  467. // For x86_64, the address of the thunk is passed in %rdi, but the
  468. // pseudo directly use the symbol, so do not add an implicit use of
  469. // %rdi. The lowering will do the right thing with RDI.
  470. // On return the address of the variable is in %rax. All other
  471. // registers are preserved.
  472. let Defs = [RAX, EFLAGS, DF],
  473. Uses = [RSP, SSP],
  474. usesCustomInserter = 1 in
  475. def TLSCall_64 : I<0, Pseudo, (outs), (ins i64mem:$sym),
  476. "# TLSCall_64",
  477. [(X86TLSCall addr:$sym)]>,
  478. Requires<[In64BitMode]>;
  479. } // SchedRW
  480. //===----------------------------------------------------------------------===//
  481. // Conditional Move Pseudo Instructions
  482. // CMOV* - Used to implement the SELECT DAG operation. Expanded after
  483. // instruction selection into a branch sequence.
  484. multiclass CMOVrr_PSEUDO<RegisterClass RC, ValueType VT> {
  485. def CMOV#NAME : I<0, Pseudo,
  486. (outs RC:$dst), (ins RC:$t, RC:$f, i8imm:$cond),
  487. "#CMOV_"#NAME#" PSEUDO!",
  488. [(set RC:$dst, (VT (X86cmov RC:$t, RC:$f, timm:$cond,
  489. EFLAGS)))]>;
  490. }
  491. let usesCustomInserter = 1, hasNoSchedulingInfo = 1, Uses = [EFLAGS] in {
  492. // X86 doesn't have 8-bit conditional moves. Use a customInserter to
  493. // emit control flow. An alternative to this is to mark i8 SELECT as Promote,
  494. // however that requires promoting the operands, and can induce additional
  495. // i8 register pressure.
  496. defm _GR8 : CMOVrr_PSEUDO<GR8, i8>;
  497. let Predicates = [NoCMOV] in {
  498. defm _GR32 : CMOVrr_PSEUDO<GR32, i32>;
  499. defm _GR16 : CMOVrr_PSEUDO<GR16, i16>;
  500. } // Predicates = [NoCMOV]
  501. // fcmov doesn't handle all possible EFLAGS, provide a fallback if there is no
  502. // SSE1/SSE2.
  503. let Predicates = [FPStackf32] in
  504. defm _RFP32 : CMOVrr_PSEUDO<RFP32, f32>;
  505. let Predicates = [FPStackf64] in
  506. defm _RFP64 : CMOVrr_PSEUDO<RFP64, f64>;
  507. defm _RFP80 : CMOVrr_PSEUDO<RFP80, f80>;
  508. let Predicates = [HasMMX] in
  509. defm _VR64 : CMOVrr_PSEUDO<VR64, x86mmx>;
  510. let Predicates = [HasSSE1,NoAVX512] in
  511. defm _FR32 : CMOVrr_PSEUDO<FR32, f32>;
  512. let Predicates = [HasSSE2,NoAVX512] in {
  513. defm _FR16 : CMOVrr_PSEUDO<FR16, f16>;
  514. defm _FR64 : CMOVrr_PSEUDO<FR64, f64>;
  515. }
  516. let Predicates = [HasAVX512] in {
  517. defm _FR16X : CMOVrr_PSEUDO<FR16X, f16>;
  518. defm _FR32X : CMOVrr_PSEUDO<FR32X, f32>;
  519. defm _FR64X : CMOVrr_PSEUDO<FR64X, f64>;
  520. }
  521. let Predicates = [NoVLX] in {
  522. defm _VR128 : CMOVrr_PSEUDO<VR128, v2i64>;
  523. defm _VR256 : CMOVrr_PSEUDO<VR256, v4i64>;
  524. }
  525. let Predicates = [HasVLX] in {
  526. defm _VR128X : CMOVrr_PSEUDO<VR128X, v2i64>;
  527. defm _VR256X : CMOVrr_PSEUDO<VR256X, v4i64>;
  528. }
  529. defm _VR512 : CMOVrr_PSEUDO<VR512, v8i64>;
  530. defm _VK1 : CMOVrr_PSEUDO<VK1, v1i1>;
  531. defm _VK2 : CMOVrr_PSEUDO<VK2, v2i1>;
  532. defm _VK4 : CMOVrr_PSEUDO<VK4, v4i1>;
  533. defm _VK8 : CMOVrr_PSEUDO<VK8, v8i1>;
  534. defm _VK16 : CMOVrr_PSEUDO<VK16, v16i1>;
  535. defm _VK32 : CMOVrr_PSEUDO<VK32, v32i1>;
  536. defm _VK64 : CMOVrr_PSEUDO<VK64, v64i1>;
  537. } // usesCustomInserter = 1, hasNoSchedulingInfo = 1, Uses = [EFLAGS]
  538. def : Pat<(f128 (X86cmov VR128:$t, VR128:$f, timm:$cond, EFLAGS)),
  539. (CMOV_VR128 VR128:$t, VR128:$f, timm:$cond)>;
  540. let Predicates = [NoVLX] in {
  541. def : Pat<(v16i8 (X86cmov VR128:$t, VR128:$f, timm:$cond, EFLAGS)),
  542. (CMOV_VR128 VR128:$t, VR128:$f, timm:$cond)>;
  543. def : Pat<(v8i16 (X86cmov VR128:$t, VR128:$f, timm:$cond, EFLAGS)),
  544. (CMOV_VR128 VR128:$t, VR128:$f, timm:$cond)>;
  545. def : Pat<(v4i32 (X86cmov VR128:$t, VR128:$f, timm:$cond, EFLAGS)),
  546. (CMOV_VR128 VR128:$t, VR128:$f, timm:$cond)>;
  547. def : Pat<(v4f32 (X86cmov VR128:$t, VR128:$f, timm:$cond, EFLAGS)),
  548. (CMOV_VR128 VR128:$t, VR128:$f, timm:$cond)>;
  549. def : Pat<(v2f64 (X86cmov VR128:$t, VR128:$f, timm:$cond, EFLAGS)),
  550. (CMOV_VR128 VR128:$t, VR128:$f, timm:$cond)>;
  551. def : Pat<(v32i8 (X86cmov VR256:$t, VR256:$f, timm:$cond, EFLAGS)),
  552. (CMOV_VR256 VR256:$t, VR256:$f, timm:$cond)>;
  553. def : Pat<(v16i16 (X86cmov VR256:$t, VR256:$f, timm:$cond, EFLAGS)),
  554. (CMOV_VR256 VR256:$t, VR256:$f, timm:$cond)>;
  555. def : Pat<(v8i32 (X86cmov VR256:$t, VR256:$f, timm:$cond, EFLAGS)),
  556. (CMOV_VR256 VR256:$t, VR256:$f, timm:$cond)>;
  557. def : Pat<(v8f32 (X86cmov VR256:$t, VR256:$f, timm:$cond, EFLAGS)),
  558. (CMOV_VR256 VR256:$t, VR256:$f, timm:$cond)>;
  559. def : Pat<(v4f64 (X86cmov VR256:$t, VR256:$f, timm:$cond, EFLAGS)),
  560. (CMOV_VR256 VR256:$t, VR256:$f, timm:$cond)>;
  561. }
  562. let Predicates = [HasVLX] in {
  563. def : Pat<(v16i8 (X86cmov VR128X:$t, VR128X:$f, timm:$cond, EFLAGS)),
  564. (CMOV_VR128X VR128X:$t, VR128X:$f, timm:$cond)>;
  565. def : Pat<(v8i16 (X86cmov VR128X:$t, VR128X:$f, timm:$cond, EFLAGS)),
  566. (CMOV_VR128X VR128X:$t, VR128X:$f, timm:$cond)>;
  567. def : Pat<(v8f16 (X86cmov VR128X:$t, VR128X:$f, timm:$cond, EFLAGS)),
  568. (CMOV_VR128X VR128X:$t, VR128X:$f, timm:$cond)>;
  569. def : Pat<(v4i32 (X86cmov VR128X:$t, VR128X:$f, timm:$cond, EFLAGS)),
  570. (CMOV_VR128X VR128X:$t, VR128X:$f, timm:$cond)>;
  571. def : Pat<(v4f32 (X86cmov VR128X:$t, VR128X:$f, timm:$cond, EFLAGS)),
  572. (CMOV_VR128X VR128X:$t, VR128X:$f, timm:$cond)>;
  573. def : Pat<(v2f64 (X86cmov VR128X:$t, VR128X:$f, timm:$cond, EFLAGS)),
  574. (CMOV_VR128X VR128X:$t, VR128X:$f, timm:$cond)>;
  575. def : Pat<(v32i8 (X86cmov VR256X:$t, VR256X:$f, timm:$cond, EFLAGS)),
  576. (CMOV_VR256X VR256X:$t, VR256X:$f, timm:$cond)>;
  577. def : Pat<(v16i16 (X86cmov VR256X:$t, VR256X:$f, timm:$cond, EFLAGS)),
  578. (CMOV_VR256X VR256X:$t, VR256X:$f, timm:$cond)>;
  579. def : Pat<(v16f16 (X86cmov VR256X:$t, VR256X:$f, timm:$cond, EFLAGS)),
  580. (CMOV_VR256X VR256X:$t, VR256X:$f, timm:$cond)>;
  581. def : Pat<(v8i32 (X86cmov VR256X:$t, VR256X:$f, timm:$cond, EFLAGS)),
  582. (CMOV_VR256X VR256X:$t, VR256X:$f, timm:$cond)>;
  583. def : Pat<(v8f32 (X86cmov VR256X:$t, VR256X:$f, timm:$cond, EFLAGS)),
  584. (CMOV_VR256X VR256X:$t, VR256X:$f, timm:$cond)>;
  585. def : Pat<(v4f64 (X86cmov VR256X:$t, VR256X:$f, timm:$cond, EFLAGS)),
  586. (CMOV_VR256X VR256X:$t, VR256X:$f, timm:$cond)>;
  587. }
  588. def : Pat<(v64i8 (X86cmov VR512:$t, VR512:$f, timm:$cond, EFLAGS)),
  589. (CMOV_VR512 VR512:$t, VR512:$f, timm:$cond)>;
  590. def : Pat<(v32i16 (X86cmov VR512:$t, VR512:$f, timm:$cond, EFLAGS)),
  591. (CMOV_VR512 VR512:$t, VR512:$f, timm:$cond)>;
  592. def : Pat<(v32f16 (X86cmov VR512:$t, VR512:$f, timm:$cond, EFLAGS)),
  593. (CMOV_VR512 VR512:$t, VR512:$f, timm:$cond)>;
  594. def : Pat<(v16i32 (X86cmov VR512:$t, VR512:$f, timm:$cond, EFLAGS)),
  595. (CMOV_VR512 VR512:$t, VR512:$f, timm:$cond)>;
  596. def : Pat<(v16f32 (X86cmov VR512:$t, VR512:$f, timm:$cond, EFLAGS)),
  597. (CMOV_VR512 VR512:$t, VR512:$f, timm:$cond)>;
  598. def : Pat<(v8f64 (X86cmov VR512:$t, VR512:$f, timm:$cond, EFLAGS)),
  599. (CMOV_VR512 VR512:$t, VR512:$f, timm:$cond)>;
  600. //===----------------------------------------------------------------------===//
  601. // Normal-Instructions-With-Lock-Prefix Pseudo Instructions
  602. //===----------------------------------------------------------------------===//
  603. // FIXME: Use normal instructions and add lock prefix dynamically.
  604. // Memory barriers
  605. let isCodeGenOnly = 1, Defs = [EFLAGS] in
  606. def OR32mi8Locked : Ii8<0x83, MRM1m, (outs), (ins i32mem:$dst, i32i8imm:$zero),
  607. "or{l}\t{$zero, $dst|$dst, $zero}", []>,
  608. Requires<[Not64BitMode]>, OpSize32, LOCK,
  609. Sched<[WriteALURMW]>;
  610. // RegOpc corresponds to the mr version of the instruction
  611. // ImmOpc corresponds to the mi version of the instruction
  612. // ImmOpc8 corresponds to the mi8 version of the instruction
  613. // ImmMod corresponds to the instruction format of the mi and mi8 versions
  614. multiclass LOCK_ArithBinOp<bits<8> RegOpc, bits<8> ImmOpc, bits<8> ImmOpc8,
  615. Format ImmMod, SDNode Op, string mnemonic> {
  616. let Defs = [EFLAGS], mayLoad = 1, mayStore = 1, isCodeGenOnly = 1,
  617. SchedRW = [WriteALURMW] in {
  618. def NAME#8mr : I<{RegOpc{7}, RegOpc{6}, RegOpc{5}, RegOpc{4},
  619. RegOpc{3}, RegOpc{2}, RegOpc{1}, 0 },
  620. MRMDestMem, (outs), (ins i8mem:$dst, GR8:$src2),
  621. !strconcat(mnemonic, "{b}\t",
  622. "{$src2, $dst|$dst, $src2}"),
  623. [(set EFLAGS, (Op addr:$dst, GR8:$src2))]>, LOCK;
  624. def NAME#16mr : I<{RegOpc{7}, RegOpc{6}, RegOpc{5}, RegOpc{4},
  625. RegOpc{3}, RegOpc{2}, RegOpc{1}, 1 },
  626. MRMDestMem, (outs), (ins i16mem:$dst, GR16:$src2),
  627. !strconcat(mnemonic, "{w}\t",
  628. "{$src2, $dst|$dst, $src2}"),
  629. [(set EFLAGS, (Op addr:$dst, GR16:$src2))]>,
  630. OpSize16, LOCK;
  631. def NAME#32mr : I<{RegOpc{7}, RegOpc{6}, RegOpc{5}, RegOpc{4},
  632. RegOpc{3}, RegOpc{2}, RegOpc{1}, 1 },
  633. MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src2),
  634. !strconcat(mnemonic, "{l}\t",
  635. "{$src2, $dst|$dst, $src2}"),
  636. [(set EFLAGS, (Op addr:$dst, GR32:$src2))]>,
  637. OpSize32, LOCK;
  638. def NAME#64mr : RI<{RegOpc{7}, RegOpc{6}, RegOpc{5}, RegOpc{4},
  639. RegOpc{3}, RegOpc{2}, RegOpc{1}, 1 },
  640. MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2),
  641. !strconcat(mnemonic, "{q}\t",
  642. "{$src2, $dst|$dst, $src2}"),
  643. [(set EFLAGS, (Op addr:$dst, GR64:$src2))]>, LOCK;
  644. // NOTE: These are order specific, we want the mi8 forms to be listed
  645. // first so that they are slightly preferred to the mi forms.
  646. def NAME#16mi8 : Ii8<{ImmOpc8{7}, ImmOpc8{6}, ImmOpc8{5}, ImmOpc8{4},
  647. ImmOpc8{3}, ImmOpc8{2}, ImmOpc8{1}, 1 },
  648. ImmMod, (outs), (ins i16mem :$dst, i16i8imm :$src2),
  649. !strconcat(mnemonic, "{w}\t",
  650. "{$src2, $dst|$dst, $src2}"),
  651. [(set EFLAGS, (Op addr:$dst, i16immSExt8:$src2))]>,
  652. OpSize16, LOCK;
  653. def NAME#32mi8 : Ii8<{ImmOpc8{7}, ImmOpc8{6}, ImmOpc8{5}, ImmOpc8{4},
  654. ImmOpc8{3}, ImmOpc8{2}, ImmOpc8{1}, 1 },
  655. ImmMod, (outs), (ins i32mem :$dst, i32i8imm :$src2),
  656. !strconcat(mnemonic, "{l}\t",
  657. "{$src2, $dst|$dst, $src2}"),
  658. [(set EFLAGS, (Op addr:$dst, i32immSExt8:$src2))]>,
  659. OpSize32, LOCK;
  660. def NAME#64mi8 : RIi8<{ImmOpc8{7}, ImmOpc8{6}, ImmOpc8{5}, ImmOpc8{4},
  661. ImmOpc8{3}, ImmOpc8{2}, ImmOpc8{1}, 1 },
  662. ImmMod, (outs), (ins i64mem :$dst, i64i8imm :$src2),
  663. !strconcat(mnemonic, "{q}\t",
  664. "{$src2, $dst|$dst, $src2}"),
  665. [(set EFLAGS, (Op addr:$dst, i64immSExt8:$src2))]>,
  666. LOCK;
  667. def NAME#8mi : Ii8<{ImmOpc{7}, ImmOpc{6}, ImmOpc{5}, ImmOpc{4},
  668. ImmOpc{3}, ImmOpc{2}, ImmOpc{1}, 0 },
  669. ImmMod, (outs), (ins i8mem :$dst, i8imm :$src2),
  670. !strconcat(mnemonic, "{b}\t",
  671. "{$src2, $dst|$dst, $src2}"),
  672. [(set EFLAGS, (Op addr:$dst, (i8 imm:$src2)))]>, LOCK;
  673. def NAME#16mi : Ii16<{ImmOpc{7}, ImmOpc{6}, ImmOpc{5}, ImmOpc{4},
  674. ImmOpc{3}, ImmOpc{2}, ImmOpc{1}, 1 },
  675. ImmMod, (outs), (ins i16mem :$dst, i16imm :$src2),
  676. !strconcat(mnemonic, "{w}\t",
  677. "{$src2, $dst|$dst, $src2}"),
  678. [(set EFLAGS, (Op addr:$dst, (i16 imm:$src2)))]>,
  679. OpSize16, LOCK;
  680. def NAME#32mi : Ii32<{ImmOpc{7}, ImmOpc{6}, ImmOpc{5}, ImmOpc{4},
  681. ImmOpc{3}, ImmOpc{2}, ImmOpc{1}, 1 },
  682. ImmMod, (outs), (ins i32mem :$dst, i32imm :$src2),
  683. !strconcat(mnemonic, "{l}\t",
  684. "{$src2, $dst|$dst, $src2}"),
  685. [(set EFLAGS, (Op addr:$dst, (i32 imm:$src2)))]>,
  686. OpSize32, LOCK;
  687. def NAME#64mi32 : RIi32S<{ImmOpc{7}, ImmOpc{6}, ImmOpc{5}, ImmOpc{4},
  688. ImmOpc{3}, ImmOpc{2}, ImmOpc{1}, 1 },
  689. ImmMod, (outs), (ins i64mem :$dst, i64i32imm :$src2),
  690. !strconcat(mnemonic, "{q}\t",
  691. "{$src2, $dst|$dst, $src2}"),
  692. [(set EFLAGS, (Op addr:$dst, i64immSExt32:$src2))]>,
  693. LOCK;
  694. }
  695. }
  696. defm LOCK_ADD : LOCK_ArithBinOp<0x00, 0x80, 0x83, MRM0m, X86lock_add, "add">;
  697. defm LOCK_SUB : LOCK_ArithBinOp<0x28, 0x80, 0x83, MRM5m, X86lock_sub, "sub">;
  698. defm LOCK_OR : LOCK_ArithBinOp<0x08, 0x80, 0x83, MRM1m, X86lock_or , "or">;
  699. defm LOCK_AND : LOCK_ArithBinOp<0x20, 0x80, 0x83, MRM4m, X86lock_and, "and">;
  700. defm LOCK_XOR : LOCK_ArithBinOp<0x30, 0x80, 0x83, MRM6m, X86lock_xor, "xor">;
  701. def X86lock_add_nocf : PatFrag<(ops node:$lhs, node:$rhs),
  702. (X86lock_add node:$lhs, node:$rhs), [{
  703. return hasNoCarryFlagUses(SDValue(N, 0));
  704. }]>;
  705. def X86lock_sub_nocf : PatFrag<(ops node:$lhs, node:$rhs),
  706. (X86lock_sub node:$lhs, node:$rhs), [{
  707. return hasNoCarryFlagUses(SDValue(N, 0));
  708. }]>;
  709. let Defs = [EFLAGS], mayLoad = 1, mayStore = 1, isCodeGenOnly = 1,
  710. SchedRW = [WriteALURMW] in {
  711. let Predicates = [UseIncDec] in {
  712. def LOCK_INC8m : I<0xFE, MRM0m, (outs), (ins i8mem :$dst),
  713. "inc{b}\t$dst",
  714. [(set EFLAGS, (X86lock_add_nocf addr:$dst, (i8 1)))]>,
  715. LOCK;
  716. def LOCK_INC16m : I<0xFF, MRM0m, (outs), (ins i16mem:$dst),
  717. "inc{w}\t$dst",
  718. [(set EFLAGS, (X86lock_add_nocf addr:$dst, (i16 1)))]>,
  719. OpSize16, LOCK;
  720. def LOCK_INC32m : I<0xFF, MRM0m, (outs), (ins i32mem:$dst),
  721. "inc{l}\t$dst",
  722. [(set EFLAGS, (X86lock_add_nocf addr:$dst, (i32 1)))]>,
  723. OpSize32, LOCK;
  724. def LOCK_DEC8m : I<0xFE, MRM1m, (outs), (ins i8mem :$dst),
  725. "dec{b}\t$dst",
  726. [(set EFLAGS, (X86lock_sub_nocf addr:$dst, (i8 1)))]>,
  727. LOCK;
  728. def LOCK_DEC16m : I<0xFF, MRM1m, (outs), (ins i16mem:$dst),
  729. "dec{w}\t$dst",
  730. [(set EFLAGS, (X86lock_sub_nocf addr:$dst, (i16 1)))]>,
  731. OpSize16, LOCK;
  732. def LOCK_DEC32m : I<0xFF, MRM1m, (outs), (ins i32mem:$dst),
  733. "dec{l}\t$dst",
  734. [(set EFLAGS, (X86lock_sub_nocf addr:$dst, (i32 1)))]>,
  735. OpSize32, LOCK;
  736. }
  737. let Predicates = [UseIncDec, In64BitMode] in {
  738. def LOCK_INC64m : RI<0xFF, MRM0m, (outs), (ins i64mem:$dst),
  739. "inc{q}\t$dst",
  740. [(set EFLAGS, (X86lock_add_nocf addr:$dst, (i64 1)))]>,
  741. LOCK;
  742. def LOCK_DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst),
  743. "dec{q}\t$dst",
  744. [(set EFLAGS, (X86lock_sub_nocf addr:$dst, (i64 1)))]>,
  745. LOCK;
  746. }
  747. }
  748. let Predicates = [UseIncDec] in {
  749. // Additional patterns for -1 constant.
  750. def : Pat<(X86lock_add addr:$dst, (i8 -1)), (LOCK_DEC8m addr:$dst)>;
  751. def : Pat<(X86lock_add addr:$dst, (i16 -1)), (LOCK_DEC16m addr:$dst)>;
  752. def : Pat<(X86lock_add addr:$dst, (i32 -1)), (LOCK_DEC32m addr:$dst)>;
  753. def : Pat<(X86lock_sub addr:$dst, (i8 -1)), (LOCK_INC8m addr:$dst)>;
  754. def : Pat<(X86lock_sub addr:$dst, (i16 -1)), (LOCK_INC16m addr:$dst)>;
  755. def : Pat<(X86lock_sub addr:$dst, (i32 -1)), (LOCK_INC32m addr:$dst)>;
  756. }
  757. let Predicates = [UseIncDec, In64BitMode] in {
  758. // Additional patterns for -1 constant.
  759. def : Pat<(X86lock_add addr:$dst, (i64 -1)), (LOCK_DEC64m addr:$dst)>;
  760. def : Pat<(X86lock_sub addr:$dst, (i64 -1)), (LOCK_INC64m addr:$dst)>;
  761. }
  762. // Atomic bit test.
  763. def X86LBTest : SDTypeProfile<1, 3, [SDTCisVT<0, i32>, SDTCisPtrTy<1>,
  764. SDTCisVT<2, i8>, SDTCisVT<3, i32>]>;
  765. def x86bts : SDNode<"X86ISD::LBTS", X86LBTest,
  766. [SDNPHasChain, SDNPMayLoad, SDNPMayStore, SDNPMemOperand]>;
  767. def x86btc : SDNode<"X86ISD::LBTC", X86LBTest,
  768. [SDNPHasChain, SDNPMayLoad, SDNPMayStore, SDNPMemOperand]>;
  769. def x86btr : SDNode<"X86ISD::LBTR", X86LBTest,
  770. [SDNPHasChain, SDNPMayLoad, SDNPMayStore, SDNPMemOperand]>;
  771. def X86LBTestRM : SDTypeProfile<1, 2, [SDTCisVT<0, i32>, SDTCisPtrTy<1>,
  772. SDTCisInt<2>]>;
  773. def x86_rm_bts : SDNode<"X86ISD::LBTS_RM", X86LBTestRM,
  774. [SDNPHasChain, SDNPMayLoad, SDNPMayStore, SDNPMemOperand]>;
  775. def x86_rm_btc : SDNode<"X86ISD::LBTC_RM", X86LBTestRM,
  776. [SDNPHasChain, SDNPMayLoad, SDNPMayStore, SDNPMemOperand]>;
  777. def x86_rm_btr : SDNode<"X86ISD::LBTR_RM", X86LBTestRM,
  778. [SDNPHasChain, SDNPMayLoad, SDNPMayStore, SDNPMemOperand]>;
  779. multiclass ATOMIC_LOGIC_OP<Format Form, string s> {
  780. let Defs = [EFLAGS], mayLoad = 1, mayStore = 1, isCodeGenOnly = 1,
  781. SchedRW = [WriteBitTestSetRegRMW] in {
  782. def 16m : Ii8<0xBA, Form, (outs), (ins i16mem:$src1, i8imm:$src2),
  783. !strconcat(s, "{w}\t{$src2, $src1|$src1, $src2}"),
  784. [(set EFLAGS, (!cast<SDNode>("x86" # s) addr:$src1, timm:$src2, (i32 16)))]>,
  785. OpSize16, TB, LOCK;
  786. def 32m : Ii8<0xBA, Form, (outs), (ins i32mem:$src1, i8imm:$src2),
  787. !strconcat(s, "{l}\t{$src2, $src1|$src1, $src2}"),
  788. [(set EFLAGS, (!cast<SDNode>("x86" # s) addr:$src1, timm:$src2, (i32 32)))]>,
  789. OpSize32, TB, LOCK;
  790. def 64m : RIi8<0xBA, Form, (outs), (ins i64mem:$src1, i8imm:$src2),
  791. !strconcat(s, "{q}\t{$src2, $src1|$src1, $src2}"),
  792. [(set EFLAGS, (!cast<SDNode>("x86" # s) addr:$src1, timm:$src2, (i32 64)))]>,
  793. TB, LOCK;
  794. }
  795. }
  796. multiclass ATOMIC_LOGIC_OP_RM<bits<8> Opc8, string s> {
  797. let Defs = [EFLAGS], mayLoad = 1, mayStore = 1, isCodeGenOnly = 1,
  798. SchedRW = [WriteBitTestSetRegRMW] in {
  799. def 16rm : Ii8<Opc8, MRMDestMem, (outs), (ins i16mem:$src1, GR16:$src2),
  800. !strconcat(s, "{w}\t{$src2, $src1|$src1, $src2}"),
  801. [(set EFLAGS, (!cast<SDNode>("x86_rm_" # s) addr:$src1, GR16:$src2))]>,
  802. OpSize16, TB, LOCK;
  803. def 32rm : Ii8<Opc8, MRMDestMem, (outs), (ins i32mem:$src1, GR32:$src2),
  804. !strconcat(s, "{l}\t{$src2, $src1|$src1, $src2}"),
  805. [(set EFLAGS, (!cast<SDNode>("x86_rm_" # s) addr:$src1, GR32:$src2))]>,
  806. OpSize32, TB, LOCK;
  807. def 64rm : RIi8<Opc8, MRMDestMem, (outs), (ins i64mem:$src1, GR64:$src2),
  808. !strconcat(s, "{q}\t{$src2, $src1|$src1, $src2}"),
  809. [(set EFLAGS, (!cast<SDNode>("x86_rm_" # s) addr:$src1, GR64:$src2))]>,
  810. TB, LOCK;
  811. }
  812. }
  813. defm LOCK_BTS : ATOMIC_LOGIC_OP<MRM5m, "bts">;
  814. defm LOCK_BTC : ATOMIC_LOGIC_OP<MRM7m, "btc">;
  815. defm LOCK_BTR : ATOMIC_LOGIC_OP<MRM6m, "btr">;
  816. defm LOCK_BTS_RM : ATOMIC_LOGIC_OP_RM<0xAB, "bts">;
  817. defm LOCK_BTC_RM : ATOMIC_LOGIC_OP_RM<0xBB, "btc">;
  818. defm LOCK_BTR_RM : ATOMIC_LOGIC_OP_RM<0xB3, "btr">;
  819. // Atomic compare and swap.
  820. multiclass LCMPXCHG_BinOp<bits<8> Opc8, bits<8> Opc, Format Form,
  821. string mnemonic, SDPatternOperator frag> {
  822. let isCodeGenOnly = 1, SchedRW = [WriteCMPXCHGRMW] in {
  823. let Defs = [AL, EFLAGS], Uses = [AL] in
  824. def NAME#8 : I<Opc8, Form, (outs), (ins i8mem:$ptr, GR8:$swap),
  825. !strconcat(mnemonic, "{b}\t{$swap, $ptr|$ptr, $swap}"),
  826. [(frag addr:$ptr, GR8:$swap, 1)]>, TB, LOCK;
  827. let Defs = [AX, EFLAGS], Uses = [AX] in
  828. def NAME#16 : I<Opc, Form, (outs), (ins i16mem:$ptr, GR16:$swap),
  829. !strconcat(mnemonic, "{w}\t{$swap, $ptr|$ptr, $swap}"),
  830. [(frag addr:$ptr, GR16:$swap, 2)]>, TB, OpSize16, LOCK;
  831. let Defs = [EAX, EFLAGS], Uses = [EAX] in
  832. def NAME#32 : I<Opc, Form, (outs), (ins i32mem:$ptr, GR32:$swap),
  833. !strconcat(mnemonic, "{l}\t{$swap, $ptr|$ptr, $swap}"),
  834. [(frag addr:$ptr, GR32:$swap, 4)]>, TB, OpSize32, LOCK;
  835. let Defs = [RAX, EFLAGS], Uses = [RAX] in
  836. def NAME#64 : RI<Opc, Form, (outs), (ins i64mem:$ptr, GR64:$swap),
  837. !strconcat(mnemonic, "{q}\t{$swap, $ptr|$ptr, $swap}"),
  838. [(frag addr:$ptr, GR64:$swap, 8)]>, TB, LOCK;
  839. }
  840. }
  841. let Defs = [EAX, EDX, EFLAGS], Uses = [EAX, EBX, ECX, EDX],
  842. Predicates = [HasCX8], SchedRW = [WriteCMPXCHGRMW],
  843. isCodeGenOnly = 1, usesCustomInserter = 1 in {
  844. def LCMPXCHG8B : I<0xC7, MRM1m, (outs), (ins i64mem:$ptr),
  845. "cmpxchg8b\t$ptr",
  846. [(X86cas8 addr:$ptr)]>, TB, LOCK;
  847. }
  848. let Defs = [RAX, RDX, EFLAGS], Uses = [RAX, RBX, RCX, RDX],
  849. Predicates = [HasCX16,In64BitMode], SchedRW = [WriteCMPXCHGRMW],
  850. isCodeGenOnly = 1, mayLoad = 1, mayStore = 1, hasSideEffects = 0 in {
  851. def LCMPXCHG16B : RI<0xC7, MRM1m, (outs), (ins i128mem:$ptr),
  852. "cmpxchg16b\t$ptr",
  853. []>, TB, LOCK;
  854. }
  855. // This pseudo must be used when the frame uses RBX as
  856. // the base pointer. Indeed, in such situation RBX is a reserved
  857. // register and the register allocator will ignore any use/def of
  858. // it. In other words, the register will not fix the clobbering of
  859. // RBX that will happen when setting the arguments for the instrucion.
  860. //
  861. // Unlike the actual related instruction, we mark that this one
  862. // defines RBX (instead of using RBX).
  863. // The rationale is that we will define RBX during the expansion of
  864. // the pseudo. The argument feeding RBX is rbx_input.
  865. //
  866. // The additional argument, $rbx_save, is a temporary register used to
  867. // save the value of RBX across the actual instruction.
  868. //
  869. // To make sure the register assigned to $rbx_save does not interfere with
  870. // the definition of the actual instruction, we use a definition $dst which
  871. // is tied to $rbx_save. That way, the live-range of $rbx_save spans across
  872. // the instruction and we are sure we will have a valid register to restore
  873. // the value of RBX.
  874. let Defs = [RAX, RDX, RBX, EFLAGS], Uses = [RAX, RCX, RDX],
  875. Predicates = [HasCX16,In64BitMode], SchedRW = [WriteCMPXCHGRMW],
  876. isCodeGenOnly = 1, isPseudo = 1,
  877. mayLoad = 1, mayStore = 1, hasSideEffects = 0,
  878. Constraints = "$rbx_save = $dst" in {
  879. def LCMPXCHG16B_SAVE_RBX :
  880. I<0, Pseudo, (outs GR64:$dst),
  881. (ins i128mem:$ptr, GR64:$rbx_input, GR64:$rbx_save), "", []>;
  882. }
  883. // Pseudo instruction that doesn't read/write RBX. Will be turned into either
  884. // LCMPXCHG16B_SAVE_RBX or LCMPXCHG16B via a custom inserter.
  885. let Defs = [RAX, RDX, EFLAGS], Uses = [RAX, RCX, RDX],
  886. Predicates = [HasCX16,In64BitMode], SchedRW = [WriteCMPXCHGRMW],
  887. isCodeGenOnly = 1, isPseudo = 1,
  888. mayLoad = 1, mayStore = 1, hasSideEffects = 0,
  889. usesCustomInserter = 1 in {
  890. def LCMPXCHG16B_NO_RBX :
  891. I<0, Pseudo, (outs), (ins i128mem:$ptr, GR64:$rbx_input), "",
  892. [(X86cas16 addr:$ptr, GR64:$rbx_input)]>;
  893. }
  894. // This pseudo must be used when the frame uses RBX/EBX as
  895. // the base pointer.
  896. // cf comment for LCMPXCHG16B_SAVE_RBX.
  897. let Defs = [EBX], Uses = [ECX, EAX],
  898. Predicates = [HasMWAITX], SchedRW = [WriteSystem],
  899. isCodeGenOnly = 1, isPseudo = 1, Constraints = "$rbx_save = $dst" in {
  900. def MWAITX_SAVE_RBX :
  901. I<0, Pseudo, (outs GR64:$dst),
  902. (ins GR32:$ebx_input, GR64:$rbx_save),
  903. "mwaitx",
  904. []>;
  905. }
  906. // Pseudo mwaitx instruction to use for custom insertion.
  907. let Predicates = [HasMWAITX], SchedRW = [WriteSystem],
  908. isCodeGenOnly = 1, isPseudo = 1,
  909. usesCustomInserter = 1 in {
  910. def MWAITX :
  911. I<0, Pseudo, (outs), (ins GR32:$ecx, GR32:$eax, GR32:$ebx),
  912. "mwaitx",
  913. [(int_x86_mwaitx GR32:$ecx, GR32:$eax, GR32:$ebx)]>;
  914. }
  915. defm LCMPXCHG : LCMPXCHG_BinOp<0xB0, 0xB1, MRMDestMem, "cmpxchg", X86cas>;
  916. // Atomic exchange and add
  917. multiclass ATOMIC_RMW_BINOP<bits<8> opc8, bits<8> opc, string mnemonic,
  918. string frag> {
  919. let Constraints = "$val = $dst", Defs = [EFLAGS], mayLoad = 1, mayStore = 1,
  920. isCodeGenOnly = 1, SchedRW = [WriteALURMW] in {
  921. def NAME#8 : I<opc8, MRMSrcMem, (outs GR8:$dst),
  922. (ins GR8:$val, i8mem:$ptr),
  923. !strconcat(mnemonic, "{b}\t{$val, $ptr|$ptr, $val}"),
  924. [(set GR8:$dst,
  925. (!cast<PatFrag>(frag # "_8") addr:$ptr, GR8:$val))]>;
  926. def NAME#16 : I<opc, MRMSrcMem, (outs GR16:$dst),
  927. (ins GR16:$val, i16mem:$ptr),
  928. !strconcat(mnemonic, "{w}\t{$val, $ptr|$ptr, $val}"),
  929. [(set
  930. GR16:$dst,
  931. (!cast<PatFrag>(frag # "_16") addr:$ptr, GR16:$val))]>,
  932. OpSize16;
  933. def NAME#32 : I<opc, MRMSrcMem, (outs GR32:$dst),
  934. (ins GR32:$val, i32mem:$ptr),
  935. !strconcat(mnemonic, "{l}\t{$val, $ptr|$ptr, $val}"),
  936. [(set
  937. GR32:$dst,
  938. (!cast<PatFrag>(frag # "_32") addr:$ptr, GR32:$val))]>,
  939. OpSize32;
  940. def NAME#64 : RI<opc, MRMSrcMem, (outs GR64:$dst),
  941. (ins GR64:$val, i64mem:$ptr),
  942. !strconcat(mnemonic, "{q}\t{$val, $ptr|$ptr, $val}"),
  943. [(set
  944. GR64:$dst,
  945. (!cast<PatFrag>(frag # "_64") addr:$ptr, GR64:$val))]>;
  946. }
  947. }
  948. defm LXADD : ATOMIC_RMW_BINOP<0xc0, 0xc1, "xadd", "atomic_load_add">, TB, LOCK;
  949. /* The following multiclass tries to make sure that in code like
  950. * x.store (immediate op x.load(acquire), release)
  951. * and
  952. * x.store (register op x.load(acquire), release)
  953. * an operation directly on memory is generated instead of wasting a register.
  954. * It is not automatic as atomic_store/load are only lowered to MOV instructions
  955. * extremely late to prevent them from being accidentally reordered in the backend
  956. * (see below the RELEASE_MOV* / ACQUIRE_MOV* pseudo-instructions)
  957. */
  958. multiclass RELEASE_BINOP_MI<string Name, SDNode op> {
  959. def : Pat<(atomic_store_8 addr:$dst,
  960. (op (atomic_load_8 addr:$dst), (i8 imm:$src))),
  961. (!cast<Instruction>(Name#"8mi") addr:$dst, imm:$src)>;
  962. def : Pat<(atomic_store_16 addr:$dst,
  963. (op (atomic_load_16 addr:$dst), (i16 imm:$src))),
  964. (!cast<Instruction>(Name#"16mi") addr:$dst, imm:$src)>;
  965. def : Pat<(atomic_store_32 addr:$dst,
  966. (op (atomic_load_32 addr:$dst), (i32 imm:$src))),
  967. (!cast<Instruction>(Name#"32mi") addr:$dst, imm:$src)>;
  968. def : Pat<(atomic_store_64 addr:$dst,
  969. (op (atomic_load_64 addr:$dst), (i64immSExt32:$src))),
  970. (!cast<Instruction>(Name#"64mi32") addr:$dst, (i64immSExt32:$src))>;
  971. def : Pat<(atomic_store_8 addr:$dst,
  972. (op (atomic_load_8 addr:$dst), (i8 GR8:$src))),
  973. (!cast<Instruction>(Name#"8mr") addr:$dst, GR8:$src)>;
  974. def : Pat<(atomic_store_16 addr:$dst,
  975. (op (atomic_load_16 addr:$dst), (i16 GR16:$src))),
  976. (!cast<Instruction>(Name#"16mr") addr:$dst, GR16:$src)>;
  977. def : Pat<(atomic_store_32 addr:$dst,
  978. (op (atomic_load_32 addr:$dst), (i32 GR32:$src))),
  979. (!cast<Instruction>(Name#"32mr") addr:$dst, GR32:$src)>;
  980. def : Pat<(atomic_store_64 addr:$dst,
  981. (op (atomic_load_64 addr:$dst), (i64 GR64:$src))),
  982. (!cast<Instruction>(Name#"64mr") addr:$dst, GR64:$src)>;
  983. }
  984. defm : RELEASE_BINOP_MI<"ADD", add>;
  985. defm : RELEASE_BINOP_MI<"AND", and>;
  986. defm : RELEASE_BINOP_MI<"OR", or>;
  987. defm : RELEASE_BINOP_MI<"XOR", xor>;
  988. defm : RELEASE_BINOP_MI<"SUB", sub>;
  989. // Atomic load + floating point patterns.
  990. // FIXME: This could also handle SIMD operations with *ps and *pd instructions.
  991. multiclass ATOMIC_LOAD_FP_BINOP_MI<string Name, SDNode op> {
  992. def : Pat<(op FR32:$src1, (bitconvert (i32 (atomic_load_32 addr:$src2)))),
  993. (!cast<Instruction>(Name#"SSrm") FR32:$src1, addr:$src2)>,
  994. Requires<[UseSSE1]>;
  995. def : Pat<(op FR32:$src1, (bitconvert (i32 (atomic_load_32 addr:$src2)))),
  996. (!cast<Instruction>("V"#Name#"SSrm") FR32:$src1, addr:$src2)>,
  997. Requires<[UseAVX]>;
  998. def : Pat<(op FR32X:$src1, (bitconvert (i32 (atomic_load_32 addr:$src2)))),
  999. (!cast<Instruction>("V"#Name#"SSZrm") FR32X:$src1, addr:$src2)>,
  1000. Requires<[HasAVX512]>;
  1001. def : Pat<(op FR64:$src1, (bitconvert (i64 (atomic_load_64 addr:$src2)))),
  1002. (!cast<Instruction>(Name#"SDrm") FR64:$src1, addr:$src2)>,
  1003. Requires<[UseSSE1]>;
  1004. def : Pat<(op FR64:$src1, (bitconvert (i64 (atomic_load_64 addr:$src2)))),
  1005. (!cast<Instruction>("V"#Name#"SDrm") FR64:$src1, addr:$src2)>,
  1006. Requires<[UseAVX]>;
  1007. def : Pat<(op FR64X:$src1, (bitconvert (i64 (atomic_load_64 addr:$src2)))),
  1008. (!cast<Instruction>("V"#Name#"SDZrm") FR64X:$src1, addr:$src2)>,
  1009. Requires<[HasAVX512]>;
  1010. }
  1011. defm : ATOMIC_LOAD_FP_BINOP_MI<"ADD", fadd>;
  1012. // FIXME: Add fsub, fmul, fdiv, ...
  1013. multiclass RELEASE_UNOP<string Name, dag dag8, dag dag16, dag dag32,
  1014. dag dag64> {
  1015. def : Pat<(atomic_store_8 addr:$dst, dag8),
  1016. (!cast<Instruction>(Name#8m) addr:$dst)>;
  1017. def : Pat<(atomic_store_16 addr:$dst, dag16),
  1018. (!cast<Instruction>(Name#16m) addr:$dst)>;
  1019. def : Pat<(atomic_store_32 addr:$dst, dag32),
  1020. (!cast<Instruction>(Name#32m) addr:$dst)>;
  1021. def : Pat<(atomic_store_64 addr:$dst, dag64),
  1022. (!cast<Instruction>(Name#64m) addr:$dst)>;
  1023. }
  1024. let Predicates = [UseIncDec] in {
  1025. defm : RELEASE_UNOP<"INC",
  1026. (add (atomic_load_8 addr:$dst), (i8 1)),
  1027. (add (atomic_load_16 addr:$dst), (i16 1)),
  1028. (add (atomic_load_32 addr:$dst), (i32 1)),
  1029. (add (atomic_load_64 addr:$dst), (i64 1))>;
  1030. defm : RELEASE_UNOP<"DEC",
  1031. (add (atomic_load_8 addr:$dst), (i8 -1)),
  1032. (add (atomic_load_16 addr:$dst), (i16 -1)),
  1033. (add (atomic_load_32 addr:$dst), (i32 -1)),
  1034. (add (atomic_load_64 addr:$dst), (i64 -1))>;
  1035. }
  1036. defm : RELEASE_UNOP<"NEG",
  1037. (ineg (i8 (atomic_load_8 addr:$dst))),
  1038. (ineg (i16 (atomic_load_16 addr:$dst))),
  1039. (ineg (i32 (atomic_load_32 addr:$dst))),
  1040. (ineg (i64 (atomic_load_64 addr:$dst)))>;
  1041. defm : RELEASE_UNOP<"NOT",
  1042. (not (i8 (atomic_load_8 addr:$dst))),
  1043. (not (i16 (atomic_load_16 addr:$dst))),
  1044. (not (i32 (atomic_load_32 addr:$dst))),
  1045. (not (i64 (atomic_load_64 addr:$dst)))>;
  1046. def : Pat<(atomic_store_8 addr:$dst, (i8 imm:$src)),
  1047. (MOV8mi addr:$dst, imm:$src)>;
  1048. def : Pat<(atomic_store_16 addr:$dst, (i16 imm:$src)),
  1049. (MOV16mi addr:$dst, imm:$src)>;
  1050. def : Pat<(atomic_store_32 addr:$dst, (i32 imm:$src)),
  1051. (MOV32mi addr:$dst, imm:$src)>;
  1052. def : Pat<(atomic_store_64 addr:$dst, (i64immSExt32:$src)),
  1053. (MOV64mi32 addr:$dst, i64immSExt32:$src)>;
  1054. def : Pat<(atomic_store_8 addr:$dst, GR8:$src),
  1055. (MOV8mr addr:$dst, GR8:$src)>;
  1056. def : Pat<(atomic_store_16 addr:$dst, GR16:$src),
  1057. (MOV16mr addr:$dst, GR16:$src)>;
  1058. def : Pat<(atomic_store_32 addr:$dst, GR32:$src),
  1059. (MOV32mr addr:$dst, GR32:$src)>;
  1060. def : Pat<(atomic_store_64 addr:$dst, GR64:$src),
  1061. (MOV64mr addr:$dst, GR64:$src)>;
  1062. def : Pat<(i8 (atomic_load_8 addr:$src)), (MOV8rm addr:$src)>;
  1063. def : Pat<(i16 (atomic_load_16 addr:$src)), (MOV16rm addr:$src)>;
  1064. def : Pat<(i32 (atomic_load_32 addr:$src)), (MOV32rm addr:$src)>;
  1065. def : Pat<(i64 (atomic_load_64 addr:$src)), (MOV64rm addr:$src)>;
  1066. // Floating point loads/stores.
  1067. def : Pat<(atomic_store_32 addr:$dst, (i32 (bitconvert (f32 FR32:$src)))),
  1068. (MOVSSmr addr:$dst, FR32:$src)>, Requires<[UseSSE1]>;
  1069. def : Pat<(atomic_store_32 addr:$dst, (i32 (bitconvert (f32 FR32:$src)))),
  1070. (VMOVSSmr addr:$dst, FR32:$src)>, Requires<[UseAVX]>;
  1071. def : Pat<(atomic_store_32 addr:$dst, (i32 (bitconvert (f32 FR32:$src)))),
  1072. (VMOVSSZmr addr:$dst, FR32:$src)>, Requires<[HasAVX512]>;
  1073. def : Pat<(atomic_store_64 addr:$dst, (i64 (bitconvert (f64 FR64:$src)))),
  1074. (MOVSDmr addr:$dst, FR64:$src)>, Requires<[UseSSE2]>;
  1075. def : Pat<(atomic_store_64 addr:$dst, (i64 (bitconvert (f64 FR64:$src)))),
  1076. (VMOVSDmr addr:$dst, FR64:$src)>, Requires<[UseAVX]>;
  1077. def : Pat<(atomic_store_64 addr:$dst, (i64 (bitconvert (f64 FR64:$src)))),
  1078. (VMOVSDmr addr:$dst, FR64:$src)>, Requires<[HasAVX512]>;
  1079. def : Pat<(f32 (bitconvert (i32 (atomic_load_32 addr:$src)))),
  1080. (MOVSSrm_alt addr:$src)>, Requires<[UseSSE1]>;
  1081. def : Pat<(f32 (bitconvert (i32 (atomic_load_32 addr:$src)))),
  1082. (VMOVSSrm_alt addr:$src)>, Requires<[UseAVX]>;
  1083. def : Pat<(f32 (bitconvert (i32 (atomic_load_32 addr:$src)))),
  1084. (VMOVSSZrm_alt addr:$src)>, Requires<[HasAVX512]>;
  1085. def : Pat<(f64 (bitconvert (i64 (atomic_load_64 addr:$src)))),
  1086. (MOVSDrm_alt addr:$src)>, Requires<[UseSSE2]>;
  1087. def : Pat<(f64 (bitconvert (i64 (atomic_load_64 addr:$src)))),
  1088. (VMOVSDrm_alt addr:$src)>, Requires<[UseAVX]>;
  1089. def : Pat<(f64 (bitconvert (i64 (atomic_load_64 addr:$src)))),
  1090. (VMOVSDZrm_alt addr:$src)>, Requires<[HasAVX512]>;
  1091. //===----------------------------------------------------------------------===//
  1092. // DAG Pattern Matching Rules
  1093. //===----------------------------------------------------------------------===//
  1094. // Use AND/OR to store 0/-1 in memory when optimizing for minsize. This saves
  1095. // binary size compared to a regular MOV, but it introduces an unnecessary
  1096. // load, so is not suitable for regular or optsize functions.
  1097. let Predicates = [OptForMinSize] in {
  1098. def : Pat<(simple_store (i16 0), addr:$dst), (AND16mi8 addr:$dst, 0)>;
  1099. def : Pat<(simple_store (i32 0), addr:$dst), (AND32mi8 addr:$dst, 0)>;
  1100. def : Pat<(simple_store (i64 0), addr:$dst), (AND64mi8 addr:$dst, 0)>;
  1101. def : Pat<(simple_store (i16 -1), addr:$dst), (OR16mi8 addr:$dst, -1)>;
  1102. def : Pat<(simple_store (i32 -1), addr:$dst), (OR32mi8 addr:$dst, -1)>;
  1103. def : Pat<(simple_store (i64 -1), addr:$dst), (OR64mi8 addr:$dst, -1)>;
  1104. }
  1105. // In kernel code model, we can get the address of a label
  1106. // into a register with 'movq'. FIXME: This is a hack, the 'imm' predicate of
  1107. // the MOV64ri32 should accept these.
  1108. def : Pat<(i64 (X86Wrapper tconstpool :$dst)),
  1109. (MOV64ri32 tconstpool :$dst)>, Requires<[KernelCode]>;
  1110. def : Pat<(i64 (X86Wrapper tjumptable :$dst)),
  1111. (MOV64ri32 tjumptable :$dst)>, Requires<[KernelCode]>;
  1112. def : Pat<(i64 (X86Wrapper tglobaladdr :$dst)),
  1113. (MOV64ri32 tglobaladdr :$dst)>, Requires<[KernelCode]>;
  1114. def : Pat<(i64 (X86Wrapper texternalsym:$dst)),
  1115. (MOV64ri32 texternalsym:$dst)>, Requires<[KernelCode]>;
  1116. def : Pat<(i64 (X86Wrapper mcsym:$dst)),
  1117. (MOV64ri32 mcsym:$dst)>, Requires<[KernelCode]>;
  1118. def : Pat<(i64 (X86Wrapper tblockaddress:$dst)),
  1119. (MOV64ri32 tblockaddress:$dst)>, Requires<[KernelCode]>;
  1120. // If we have small model and -static mode, it is safe to store global addresses
  1121. // directly as immediates. FIXME: This is really a hack, the 'imm' predicate
  1122. // for MOV64mi32 should handle this sort of thing.
  1123. def : Pat<(store (i64 (X86Wrapper tconstpool:$src)), addr:$dst),
  1124. (MOV64mi32 addr:$dst, tconstpool:$src)>,
  1125. Requires<[NearData, IsNotPIC]>;
  1126. def : Pat<(store (i64 (X86Wrapper tjumptable:$src)), addr:$dst),
  1127. (MOV64mi32 addr:$dst, tjumptable:$src)>,
  1128. Requires<[NearData, IsNotPIC]>;
  1129. def : Pat<(store (i64 (X86Wrapper tglobaladdr:$src)), addr:$dst),
  1130. (MOV64mi32 addr:$dst, tglobaladdr:$src)>,
  1131. Requires<[NearData, IsNotPIC]>;
  1132. def : Pat<(store (i64 (X86Wrapper texternalsym:$src)), addr:$dst),
  1133. (MOV64mi32 addr:$dst, texternalsym:$src)>,
  1134. Requires<[NearData, IsNotPIC]>;
  1135. def : Pat<(store (i64 (X86Wrapper mcsym:$src)), addr:$dst),
  1136. (MOV64mi32 addr:$dst, mcsym:$src)>,
  1137. Requires<[NearData, IsNotPIC]>;
  1138. def : Pat<(store (i64 (X86Wrapper tblockaddress:$src)), addr:$dst),
  1139. (MOV64mi32 addr:$dst, tblockaddress:$src)>,
  1140. Requires<[NearData, IsNotPIC]>;
  1141. def : Pat<(i32 (X86RecoverFrameAlloc mcsym:$dst)), (MOV32ri mcsym:$dst)>;
  1142. def : Pat<(i64 (X86RecoverFrameAlloc mcsym:$dst)), (MOV64ri mcsym:$dst)>;
  1143. // Calls
  1144. // tls has some funny stuff here...
  1145. // This corresponds to movabs $foo@tpoff, %rax
  1146. def : Pat<(i64 (X86Wrapper tglobaltlsaddr :$dst)),
  1147. (MOV64ri32 tglobaltlsaddr :$dst)>;
  1148. // This corresponds to add $foo@tpoff, %rax
  1149. def : Pat<(add GR64:$src1, (X86Wrapper tglobaltlsaddr :$dst)),
  1150. (ADD64ri32 GR64:$src1, tglobaltlsaddr :$dst)>;
  1151. // Direct PC relative function call for small code model. 32-bit displacement
  1152. // sign extended to 64-bit.
  1153. def : Pat<(X86call (i64 tglobaladdr:$dst)),
  1154. (CALL64pcrel32 tglobaladdr:$dst)>;
  1155. def : Pat<(X86call (i64 texternalsym:$dst)),
  1156. (CALL64pcrel32 texternalsym:$dst)>;
  1157. def : Pat<(X86call_rvmarker (i64 tglobaladdr:$rvfunc), (i64 texternalsym:$dst)),
  1158. (CALL64pcrel32_RVMARKER tglobaladdr:$rvfunc, texternalsym:$dst)>;
  1159. def : Pat<(X86call_rvmarker (i64 tglobaladdr:$rvfunc), (i64 tglobaladdr:$dst)),
  1160. (CALL64pcrel32_RVMARKER tglobaladdr:$rvfunc, tglobaladdr:$dst)>;
  1161. // Tailcall stuff. The TCRETURN instructions execute after the epilog, so they
  1162. // can never use callee-saved registers. That is the purpose of the GR64_TC
  1163. // register classes.
  1164. //
  1165. // The only volatile register that is never used by the calling convention is
  1166. // %r11. This happens when calling a vararg function with 6 arguments.
  1167. //
  1168. // Match an X86tcret that uses less than 7 volatile registers.
  1169. def X86tcret_6regs : PatFrag<(ops node:$ptr, node:$off),
  1170. (X86tcret node:$ptr, node:$off), [{
  1171. // X86tcret args: (*chain, ptr, imm, regs..., glue)
  1172. unsigned NumRegs = 0;
  1173. for (unsigned i = 3, e = N->getNumOperands(); i != e; ++i)
  1174. if (isa<RegisterSDNode>(N->getOperand(i)) && ++NumRegs > 6)
  1175. return false;
  1176. return true;
  1177. }]>;
  1178. def X86tcret_1reg : PatFrag<(ops node:$ptr, node:$off),
  1179. (X86tcret node:$ptr, node:$off), [{
  1180. // X86tcret args: (*chain, ptr, imm, regs..., glue)
  1181. unsigned NumRegs = 1;
  1182. const SDValue& BasePtr = cast<LoadSDNode>(N->getOperand(1))->getBasePtr();
  1183. if (isa<FrameIndexSDNode>(BasePtr))
  1184. NumRegs = 3;
  1185. else if (BasePtr->getNumOperands() && isa<GlobalAddressSDNode>(BasePtr->getOperand(0)))
  1186. NumRegs = 3;
  1187. for (unsigned i = 3, e = N->getNumOperands(); i != e; ++i)
  1188. if (isa<RegisterSDNode>(N->getOperand(i)) && ( NumRegs-- == 0))
  1189. return false;
  1190. return true;
  1191. }]>;
  1192. def : Pat<(X86tcret ptr_rc_tailcall:$dst, timm:$off),
  1193. (TCRETURNri ptr_rc_tailcall:$dst, timm:$off)>,
  1194. Requires<[Not64BitMode, NotUseIndirectThunkCalls]>;
  1195. // FIXME: This is disabled for 32-bit PIC mode because the global base
  1196. // register which is part of the address mode may be assigned a
  1197. // callee-saved register.
  1198. // Similar to X86tcret_6regs, here we only have 1 register left
  1199. def : Pat<(X86tcret_1reg (load addr:$dst), timm:$off),
  1200. (TCRETURNmi addr:$dst, timm:$off)>,
  1201. Requires<[Not64BitMode, IsNotPIC, NotUseIndirectThunkCalls]>;
  1202. def : Pat<(X86tcret (i32 tglobaladdr:$dst), timm:$off),
  1203. (TCRETURNdi tglobaladdr:$dst, timm:$off)>,
  1204. Requires<[NotLP64]>;
  1205. def : Pat<(X86tcret (i32 texternalsym:$dst), timm:$off),
  1206. (TCRETURNdi texternalsym:$dst, timm:$off)>,
  1207. Requires<[NotLP64]>;
  1208. def : Pat<(X86tcret ptr_rc_tailcall:$dst, timm:$off),
  1209. (TCRETURNri64 ptr_rc_tailcall:$dst, timm:$off)>,
  1210. Requires<[In64BitMode, NotUseIndirectThunkCalls]>;
  1211. // Don't fold loads into X86tcret requiring more than 6 regs.
  1212. // There wouldn't be enough scratch registers for base+index.
  1213. def : Pat<(X86tcret_6regs (load addr:$dst), timm:$off),
  1214. (TCRETURNmi64 addr:$dst, timm:$off)>,
  1215. Requires<[In64BitMode, NotUseIndirectThunkCalls]>;
  1216. def : Pat<(X86tcret ptr_rc_tailcall:$dst, timm:$off),
  1217. (INDIRECT_THUNK_TCRETURN64 ptr_rc_tailcall:$dst, timm:$off)>,
  1218. Requires<[In64BitMode, UseIndirectThunkCalls]>;
  1219. def : Pat<(X86tcret ptr_rc_tailcall:$dst, timm:$off),
  1220. (INDIRECT_THUNK_TCRETURN32 ptr_rc_tailcall:$dst, timm:$off)>,
  1221. Requires<[Not64BitMode, UseIndirectThunkCalls]>;
  1222. def : Pat<(X86tcret (i64 tglobaladdr:$dst), timm:$off),
  1223. (TCRETURNdi64 tglobaladdr:$dst, timm:$off)>,
  1224. Requires<[IsLP64]>;
  1225. def : Pat<(X86tcret (i64 texternalsym:$dst), timm:$off),
  1226. (TCRETURNdi64 texternalsym:$dst, timm:$off)>,
  1227. Requires<[IsLP64]>;
  1228. // Normal calls, with various flavors of addresses.
  1229. def : Pat<(X86call (i32 tglobaladdr:$dst)),
  1230. (CALLpcrel32 tglobaladdr:$dst)>;
  1231. def : Pat<(X86call (i32 texternalsym:$dst)),
  1232. (CALLpcrel32 texternalsym:$dst)>;
  1233. def : Pat<(X86call (i32 imm:$dst)),
  1234. (CALLpcrel32 imm:$dst)>, Requires<[CallImmAddr]>;
  1235. // Comparisons.
  1236. // TEST R,R is smaller than CMP R,0
  1237. def : Pat<(X86cmp GR8:$src1, 0),
  1238. (TEST8rr GR8:$src1, GR8:$src1)>;
  1239. def : Pat<(X86cmp GR16:$src1, 0),
  1240. (TEST16rr GR16:$src1, GR16:$src1)>;
  1241. def : Pat<(X86cmp GR32:$src1, 0),
  1242. (TEST32rr GR32:$src1, GR32:$src1)>;
  1243. def : Pat<(X86cmp GR64:$src1, 0),
  1244. (TEST64rr GR64:$src1, GR64:$src1)>;
  1245. // zextload bool -> zextload byte
  1246. // i1 stored in one byte in zero-extended form.
  1247. // Upper bits cleanup should be executed before Store.
  1248. def : Pat<(zextloadi8i1 addr:$src), (MOV8rm addr:$src)>;
  1249. def : Pat<(zextloadi16i1 addr:$src),
  1250. (EXTRACT_SUBREG (MOVZX32rm8 addr:$src), sub_16bit)>;
  1251. def : Pat<(zextloadi32i1 addr:$src), (MOVZX32rm8 addr:$src)>;
  1252. def : Pat<(zextloadi64i1 addr:$src),
  1253. (SUBREG_TO_REG (i64 0), (MOVZX32rm8 addr:$src), sub_32bit)>;
  1254. // extload bool -> extload byte
  1255. // When extloading from 16-bit and smaller memory locations into 64-bit
  1256. // registers, use zero-extending loads so that the entire 64-bit register is
  1257. // defined, avoiding partial-register updates.
  1258. def : Pat<(extloadi8i1 addr:$src), (MOV8rm addr:$src)>;
  1259. def : Pat<(extloadi16i1 addr:$src),
  1260. (EXTRACT_SUBREG (MOVZX32rm8 addr:$src), sub_16bit)>;
  1261. def : Pat<(extloadi32i1 addr:$src), (MOVZX32rm8 addr:$src)>;
  1262. def : Pat<(extloadi16i8 addr:$src),
  1263. (EXTRACT_SUBREG (MOVZX32rm8 addr:$src), sub_16bit)>;
  1264. def : Pat<(extloadi32i8 addr:$src), (MOVZX32rm8 addr:$src)>;
  1265. def : Pat<(extloadi32i16 addr:$src), (MOVZX32rm16 addr:$src)>;
  1266. // For other extloads, use subregs, since the high contents of the register are
  1267. // defined after an extload.
  1268. // NOTE: The extloadi64i32 pattern needs to be first as it will try to form
  1269. // 32-bit loads for 4 byte aligned i8/i16 loads.
  1270. def : Pat<(extloadi64i32 addr:$src),
  1271. (SUBREG_TO_REG (i64 0), (MOV32rm addr:$src), sub_32bit)>;
  1272. def : Pat<(extloadi64i1 addr:$src),
  1273. (SUBREG_TO_REG (i64 0), (MOVZX32rm8 addr:$src), sub_32bit)>;
  1274. def : Pat<(extloadi64i8 addr:$src),
  1275. (SUBREG_TO_REG (i64 0), (MOVZX32rm8 addr:$src), sub_32bit)>;
  1276. def : Pat<(extloadi64i16 addr:$src),
  1277. (SUBREG_TO_REG (i64 0), (MOVZX32rm16 addr:$src), sub_32bit)>;
  1278. // anyext. Define these to do an explicit zero-extend to
  1279. // avoid partial-register updates.
  1280. def : Pat<(i16 (anyext GR8 :$src)), (EXTRACT_SUBREG
  1281. (MOVZX32rr8 GR8 :$src), sub_16bit)>;
  1282. def : Pat<(i32 (anyext GR8 :$src)), (MOVZX32rr8 GR8 :$src)>;
  1283. // Except for i16 -> i32 since isel expect i16 ops to be promoted to i32.
  1284. def : Pat<(i32 (anyext GR16:$src)),
  1285. (INSERT_SUBREG (i32 (IMPLICIT_DEF)), GR16:$src, sub_16bit)>;
  1286. def : Pat<(i64 (anyext GR8 :$src)),
  1287. (SUBREG_TO_REG (i64 0), (MOVZX32rr8 GR8 :$src), sub_32bit)>;
  1288. def : Pat<(i64 (anyext GR16:$src)),
  1289. (SUBREG_TO_REG (i64 0), (MOVZX32rr16 GR16 :$src), sub_32bit)>;
  1290. def : Pat<(i64 (anyext GR32:$src)),
  1291. (INSERT_SUBREG (i64 (IMPLICIT_DEF)), GR32:$src, sub_32bit)>;
  1292. // If this is an anyext of the remainder of an 8-bit sdivrem, use a MOVSX
  1293. // instead of a MOVZX. The sdivrem lowering will emit emit a MOVSX to move
  1294. // %ah to the lower byte of a register. By using a MOVSX here we allow a
  1295. // post-isel peephole to merge the two MOVSX instructions into one.
  1296. def anyext_sdiv : PatFrag<(ops node:$lhs), (anyext node:$lhs),[{
  1297. return (N->getOperand(0).getOpcode() == ISD::SDIVREM &&
  1298. N->getOperand(0).getResNo() == 1);
  1299. }]>;
  1300. def : Pat<(i32 (anyext_sdiv GR8:$src)), (MOVSX32rr8 GR8:$src)>;
  1301. // Any instruction that defines a 32-bit result leaves the high half of the
  1302. // register. Truncate can be lowered to EXTRACT_SUBREG. CopyFromReg may
  1303. // be copying from a truncate. AssertSext/AssertZext/AssertAlign aren't saying
  1304. // anything about the upper 32 bits, they're probably just qualifying a
  1305. // CopyFromReg. FREEZE may be coming from a a truncate. Any other 32-bit
  1306. // operation will zero-extend up to 64 bits.
  1307. def def32 : PatLeaf<(i32 GR32:$src), [{
  1308. return N->getOpcode() != ISD::TRUNCATE &&
  1309. N->getOpcode() != TargetOpcode::EXTRACT_SUBREG &&
  1310. N->getOpcode() != ISD::CopyFromReg &&
  1311. N->getOpcode() != ISD::AssertSext &&
  1312. N->getOpcode() != ISD::AssertZext &&
  1313. N->getOpcode() != ISD::AssertAlign &&
  1314. N->getOpcode() != ISD::FREEZE;
  1315. }]>;
  1316. // In the case of a 32-bit def that is known to implicitly zero-extend,
  1317. // we can use a SUBREG_TO_REG.
  1318. def : Pat<(i64 (zext def32:$src)),
  1319. (SUBREG_TO_REG (i64 0), GR32:$src, sub_32bit)>;
  1320. def : Pat<(i64 (and (anyext def32:$src), 0x00000000FFFFFFFF)),
  1321. (SUBREG_TO_REG (i64 0), GR32:$src, sub_32bit)>;
  1322. //===----------------------------------------------------------------------===//
  1323. // Pattern match OR as ADD
  1324. //===----------------------------------------------------------------------===//
  1325. // If safe, we prefer to pattern match OR as ADD at isel time. ADD can be
  1326. // 3-addressified into an LEA instruction to avoid copies. However, we also
  1327. // want to finally emit these instructions as an or at the end of the code
  1328. // generator to make the generated code easier to read. To do this, we select
  1329. // into "disjoint bits" pseudo ops.
  1330. // Treat an 'or' node is as an 'add' if the or'ed bits are known to be zero.
  1331. def or_is_add : PatFrag<(ops node:$lhs, node:$rhs), (or node:$lhs, node:$rhs),[{
  1332. if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
  1333. return CurDAG->MaskedValueIsZero(N->getOperand(0), CN->getAPIntValue());
  1334. KnownBits Known0 = CurDAG->computeKnownBits(N->getOperand(0), 0);
  1335. KnownBits Known1 = CurDAG->computeKnownBits(N->getOperand(1), 0);
  1336. return (~Known0.Zero & ~Known1.Zero) == 0;
  1337. }]>;
  1338. // (or x1, x2) -> (add x1, x2) if two operands are known not to share bits.
  1339. // Try this before the selecting to OR.
  1340. let SchedRW = [WriteALU] in {
  1341. let isConvertibleToThreeAddress = 1, isPseudo = 1,
  1342. Constraints = "$src1 = $dst", Defs = [EFLAGS] in {
  1343. let isCommutable = 1 in {
  1344. def ADD8rr_DB : I<0, Pseudo, (outs GR8:$dst), (ins GR8:$src1, GR8:$src2),
  1345. "", // orb/addb REG, REG
  1346. [(set GR8:$dst, (or_is_add GR8:$src1, GR8:$src2))]>;
  1347. def ADD16rr_DB : I<0, Pseudo, (outs GR16:$dst), (ins GR16:$src1, GR16:$src2),
  1348. "", // orw/addw REG, REG
  1349. [(set GR16:$dst, (or_is_add GR16:$src1, GR16:$src2))]>;
  1350. def ADD32rr_DB : I<0, Pseudo, (outs GR32:$dst), (ins GR32:$src1, GR32:$src2),
  1351. "", // orl/addl REG, REG
  1352. [(set GR32:$dst, (or_is_add GR32:$src1, GR32:$src2))]>;
  1353. def ADD64rr_DB : I<0, Pseudo, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
  1354. "", // orq/addq REG, REG
  1355. [(set GR64:$dst, (or_is_add GR64:$src1, GR64:$src2))]>;
  1356. } // isCommutable
  1357. // NOTE: These are order specific, we want the ri8 forms to be listed
  1358. // first so that they are slightly preferred to the ri forms.
  1359. def ADD8ri_DB : I<0, Pseudo,
  1360. (outs GR8:$dst), (ins GR8:$src1, i8imm:$src2),
  1361. "", // orb/addb REG, imm8
  1362. [(set GR8:$dst, (or_is_add GR8:$src1, imm:$src2))]>;
  1363. def ADD16ri8_DB : I<0, Pseudo,
  1364. (outs GR16:$dst), (ins GR16:$src1, i16i8imm:$src2),
  1365. "", // orw/addw REG, imm8
  1366. [(set GR16:$dst,(or_is_add GR16:$src1,i16immSExt8:$src2))]>;
  1367. def ADD16ri_DB : I<0, Pseudo, (outs GR16:$dst), (ins GR16:$src1, i16imm:$src2),
  1368. "", // orw/addw REG, imm
  1369. [(set GR16:$dst, (or_is_add GR16:$src1, imm:$src2))]>;
  1370. def ADD32ri8_DB : I<0, Pseudo,
  1371. (outs GR32:$dst), (ins GR32:$src1, i32i8imm:$src2),
  1372. "", // orl/addl REG, imm8
  1373. [(set GR32:$dst,(or_is_add GR32:$src1,i32immSExt8:$src2))]>;
  1374. def ADD32ri_DB : I<0, Pseudo, (outs GR32:$dst), (ins GR32:$src1, i32imm:$src2),
  1375. "", // orl/addl REG, imm
  1376. [(set GR32:$dst, (or_is_add GR32:$src1, imm:$src2))]>;
  1377. def ADD64ri8_DB : I<0, Pseudo,
  1378. (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2),
  1379. "", // orq/addq REG, imm8
  1380. [(set GR64:$dst, (or_is_add GR64:$src1,
  1381. i64immSExt8:$src2))]>;
  1382. def ADD64ri32_DB : I<0, Pseudo,
  1383. (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2),
  1384. "", // orq/addq REG, imm
  1385. [(set GR64:$dst, (or_is_add GR64:$src1,
  1386. i64immSExt32:$src2))]>;
  1387. }
  1388. } // AddedComplexity, SchedRW
  1389. //===----------------------------------------------------------------------===//
  1390. // Pattern match XOR as ADD
  1391. //===----------------------------------------------------------------------===//
  1392. // Prefer to pattern match XOR with min_signed_value as ADD at isel time.
  1393. // ADD can be 3-addressified into an LEA instruction to avoid copies.
  1394. let AddedComplexity = 5 in {
  1395. def : Pat<(xor GR8:$src1, -128),
  1396. (ADD8ri GR8:$src1, -128)>;
  1397. def : Pat<(xor GR16:$src1, -32768),
  1398. (ADD16ri GR16:$src1, -32768)>;
  1399. def : Pat<(xor GR32:$src1, -2147483648),
  1400. (ADD32ri GR32:$src1, -2147483648)>;
  1401. }
  1402. //===----------------------------------------------------------------------===//
  1403. // Some peepholes
  1404. //===----------------------------------------------------------------------===//
  1405. // Odd encoding trick: -128 fits into an 8-bit immediate field while
  1406. // +128 doesn't, so in this special case use a sub instead of an add.
  1407. def : Pat<(add GR16:$src1, 128),
  1408. (SUB16ri8 GR16:$src1, -128)>;
  1409. def : Pat<(store (add (loadi16 addr:$dst), 128), addr:$dst),
  1410. (SUB16mi8 addr:$dst, -128)>;
  1411. def : Pat<(add GR32:$src1, 128),
  1412. (SUB32ri8 GR32:$src1, -128)>;
  1413. def : Pat<(store (add (loadi32 addr:$dst), 128), addr:$dst),
  1414. (SUB32mi8 addr:$dst, -128)>;
  1415. def : Pat<(add GR64:$src1, 128),
  1416. (SUB64ri8 GR64:$src1, -128)>;
  1417. def : Pat<(store (add (loadi64 addr:$dst), 128), addr:$dst),
  1418. (SUB64mi8 addr:$dst, -128)>;
  1419. def : Pat<(X86add_flag_nocf GR16:$src1, 128),
  1420. (SUB16ri8 GR16:$src1, -128)>;
  1421. def : Pat<(X86add_flag_nocf GR32:$src1, 128),
  1422. (SUB32ri8 GR32:$src1, -128)>;
  1423. def : Pat<(X86add_flag_nocf GR64:$src1, 128),
  1424. (SUB64ri8 GR64:$src1, -128)>;
  1425. // The same trick applies for 32-bit immediate fields in 64-bit
  1426. // instructions.
  1427. def : Pat<(add GR64:$src1, 0x0000000080000000),
  1428. (SUB64ri32 GR64:$src1, 0xffffffff80000000)>;
  1429. def : Pat<(store (add (loadi64 addr:$dst), 0x0000000080000000), addr:$dst),
  1430. (SUB64mi32 addr:$dst, 0xffffffff80000000)>;
  1431. def : Pat<(X86add_flag_nocf GR64:$src1, 0x0000000080000000),
  1432. (SUB64ri32 GR64:$src1, 0xffffffff80000000)>;
  1433. // To avoid needing to materialize an immediate in a register, use a 32-bit and
  1434. // with implicit zero-extension instead of a 64-bit and if the immediate has at
  1435. // least 32 bits of leading zeros. If in addition the last 32 bits can be
  1436. // represented with a sign extension of a 8 bit constant, use that.
  1437. // This can also reduce instruction size by eliminating the need for the REX
  1438. // prefix.
  1439. // AddedComplexity is needed to give priority over i64immSExt8 and i64immSExt32.
  1440. let AddedComplexity = 1 in {
  1441. def : Pat<(and GR64:$src, i64immZExt32SExt8:$imm),
  1442. (SUBREG_TO_REG
  1443. (i64 0),
  1444. (AND32ri8
  1445. (EXTRACT_SUBREG GR64:$src, sub_32bit),
  1446. (i32 (GetLo32XForm imm:$imm))),
  1447. sub_32bit)>;
  1448. def : Pat<(and GR64:$src, i64immZExt32:$imm),
  1449. (SUBREG_TO_REG
  1450. (i64 0),
  1451. (AND32ri
  1452. (EXTRACT_SUBREG GR64:$src, sub_32bit),
  1453. (i32 (GetLo32XForm imm:$imm))),
  1454. sub_32bit)>;
  1455. } // AddedComplexity = 1
  1456. // AddedComplexity is needed due to the increased complexity on the
  1457. // i64immZExt32SExt8 and i64immZExt32 patterns above. Applying this to all
  1458. // the MOVZX patterns keeps thems together in DAGIsel tables.
  1459. let AddedComplexity = 1 in {
  1460. // r & (2^16-1) ==> movz
  1461. def : Pat<(and GR32:$src1, 0xffff),
  1462. (MOVZX32rr16 (EXTRACT_SUBREG GR32:$src1, sub_16bit))>;
  1463. // r & (2^8-1) ==> movz
  1464. def : Pat<(and GR32:$src1, 0xff),
  1465. (MOVZX32rr8 (EXTRACT_SUBREG GR32:$src1, sub_8bit))>;
  1466. // r & (2^8-1) ==> movz
  1467. def : Pat<(and GR16:$src1, 0xff),
  1468. (EXTRACT_SUBREG (MOVZX32rr8 (EXTRACT_SUBREG GR16:$src1, sub_8bit)),
  1469. sub_16bit)>;
  1470. // r & (2^32-1) ==> movz
  1471. def : Pat<(and GR64:$src, 0x00000000FFFFFFFF),
  1472. (SUBREG_TO_REG (i64 0),
  1473. (MOV32rr (EXTRACT_SUBREG GR64:$src, sub_32bit)),
  1474. sub_32bit)>;
  1475. // r & (2^16-1) ==> movz
  1476. def : Pat<(and GR64:$src, 0xffff),
  1477. (SUBREG_TO_REG (i64 0),
  1478. (MOVZX32rr16 (i16 (EXTRACT_SUBREG GR64:$src, sub_16bit))),
  1479. sub_32bit)>;
  1480. // r & (2^8-1) ==> movz
  1481. def : Pat<(and GR64:$src, 0xff),
  1482. (SUBREG_TO_REG (i64 0),
  1483. (MOVZX32rr8 (i8 (EXTRACT_SUBREG GR64:$src, sub_8bit))),
  1484. sub_32bit)>;
  1485. } // AddedComplexity = 1
  1486. // Try to use BTS/BTR/BTC for single bit operations on the upper 32-bits.
  1487. def BTRXForm : SDNodeXForm<imm, [{
  1488. // Transformation function: Find the lowest 0.
  1489. return getI64Imm((uint8_t)N->getAPIntValue().countTrailingOnes(), SDLoc(N));
  1490. }]>;
  1491. def BTCBTSXForm : SDNodeXForm<imm, [{
  1492. // Transformation function: Find the lowest 1.
  1493. return getI64Imm((uint8_t)N->getAPIntValue().countTrailingZeros(), SDLoc(N));
  1494. }]>;
  1495. def BTRMask64 : ImmLeaf<i64, [{
  1496. return !isUInt<32>(Imm) && !isInt<32>(Imm) && isPowerOf2_64(~Imm);
  1497. }]>;
  1498. def BTCBTSMask64 : ImmLeaf<i64, [{
  1499. return !isInt<32>(Imm) && isPowerOf2_64(Imm);
  1500. }]>;
  1501. // For now only do this for optsize.
  1502. let AddedComplexity = 1, Predicates=[OptForSize] in {
  1503. def : Pat<(and GR64:$src1, BTRMask64:$mask),
  1504. (BTR64ri8 GR64:$src1, (BTRXForm imm:$mask))>;
  1505. def : Pat<(or GR64:$src1, BTCBTSMask64:$mask),
  1506. (BTS64ri8 GR64:$src1, (BTCBTSXForm imm:$mask))>;
  1507. def : Pat<(xor GR64:$src1, BTCBTSMask64:$mask),
  1508. (BTC64ri8 GR64:$src1, (BTCBTSXForm imm:$mask))>;
  1509. }
  1510. // sext_inreg patterns
  1511. def : Pat<(sext_inreg GR32:$src, i16),
  1512. (MOVSX32rr16 (EXTRACT_SUBREG GR32:$src, sub_16bit))>;
  1513. def : Pat<(sext_inreg GR32:$src, i8),
  1514. (MOVSX32rr8 (EXTRACT_SUBREG GR32:$src, sub_8bit))>;
  1515. def : Pat<(sext_inreg GR16:$src, i8),
  1516. (EXTRACT_SUBREG (MOVSX32rr8 (EXTRACT_SUBREG GR16:$src, sub_8bit)),
  1517. sub_16bit)>;
  1518. def : Pat<(sext_inreg GR64:$src, i32),
  1519. (MOVSX64rr32 (EXTRACT_SUBREG GR64:$src, sub_32bit))>;
  1520. def : Pat<(sext_inreg GR64:$src, i16),
  1521. (MOVSX64rr16 (EXTRACT_SUBREG GR64:$src, sub_16bit))>;
  1522. def : Pat<(sext_inreg GR64:$src, i8),
  1523. (MOVSX64rr8 (EXTRACT_SUBREG GR64:$src, sub_8bit))>;
  1524. // sext, sext_load, zext, zext_load
  1525. def: Pat<(i16 (sext GR8:$src)),
  1526. (EXTRACT_SUBREG (MOVSX32rr8 GR8:$src), sub_16bit)>;
  1527. def: Pat<(sextloadi16i8 addr:$src),
  1528. (EXTRACT_SUBREG (MOVSX32rm8 addr:$src), sub_16bit)>;
  1529. def: Pat<(i16 (zext GR8:$src)),
  1530. (EXTRACT_SUBREG (MOVZX32rr8 GR8:$src), sub_16bit)>;
  1531. def: Pat<(zextloadi16i8 addr:$src),
  1532. (EXTRACT_SUBREG (MOVZX32rm8 addr:$src), sub_16bit)>;
  1533. // trunc patterns
  1534. def : Pat<(i16 (trunc GR32:$src)),
  1535. (EXTRACT_SUBREG GR32:$src, sub_16bit)>;
  1536. def : Pat<(i8 (trunc GR32:$src)),
  1537. (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, GR32_ABCD)),
  1538. sub_8bit)>,
  1539. Requires<[Not64BitMode]>;
  1540. def : Pat<(i8 (trunc GR16:$src)),
  1541. (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
  1542. sub_8bit)>,
  1543. Requires<[Not64BitMode]>;
  1544. def : Pat<(i32 (trunc GR64:$src)),
  1545. (EXTRACT_SUBREG GR64:$src, sub_32bit)>;
  1546. def : Pat<(i16 (trunc GR64:$src)),
  1547. (EXTRACT_SUBREG GR64:$src, sub_16bit)>;
  1548. def : Pat<(i8 (trunc GR64:$src)),
  1549. (EXTRACT_SUBREG GR64:$src, sub_8bit)>;
  1550. def : Pat<(i8 (trunc GR32:$src)),
  1551. (EXTRACT_SUBREG GR32:$src, sub_8bit)>,
  1552. Requires<[In64BitMode]>;
  1553. def : Pat<(i8 (trunc GR16:$src)),
  1554. (EXTRACT_SUBREG GR16:$src, sub_8bit)>,
  1555. Requires<[In64BitMode]>;
  1556. def immff00_ffff : ImmLeaf<i32, [{
  1557. return Imm >= 0xff00 && Imm <= 0xffff;
  1558. }]>;
  1559. // h-register tricks
  1560. def : Pat<(i8 (trunc (srl_su GR16:$src, (i8 8)))),
  1561. (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)>,
  1562. Requires<[Not64BitMode]>;
  1563. def : Pat<(i8 (trunc (srl_su (i32 (anyext GR16:$src)), (i8 8)))),
  1564. (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)>,
  1565. Requires<[Not64BitMode]>;
  1566. def : Pat<(i8 (trunc (srl_su GR32:$src, (i8 8)))),
  1567. (EXTRACT_SUBREG GR32:$src, sub_8bit_hi)>,
  1568. Requires<[Not64BitMode]>;
  1569. def : Pat<(srl GR16:$src, (i8 8)),
  1570. (EXTRACT_SUBREG
  1571. (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)),
  1572. sub_16bit)>;
  1573. def : Pat<(i32 (zext (srl_su GR16:$src, (i8 8)))),
  1574. (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR16:$src, sub_8bit_hi))>;
  1575. def : Pat<(i32 (anyext (srl_su GR16:$src, (i8 8)))),
  1576. (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR16:$src, sub_8bit_hi))>;
  1577. def : Pat<(and (srl_su GR32:$src, (i8 8)), (i32 255)),
  1578. (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR32:$src, sub_8bit_hi))>;
  1579. def : Pat<(srl (and_su GR32:$src, immff00_ffff), (i8 8)),
  1580. (MOVZX32rr8_NOREX (EXTRACT_SUBREG GR32:$src, sub_8bit_hi))>;
  1581. // h-register tricks.
  1582. // For now, be conservative on x86-64 and use an h-register extract only if the
  1583. // value is immediately zero-extended or stored, which are somewhat common
  1584. // cases. This uses a bunch of code to prevent a register requiring a REX prefix
  1585. // from being allocated in the same instruction as the h register, as there's
  1586. // currently no way to describe this requirement to the register allocator.
  1587. // h-register extract and zero-extend.
  1588. def : Pat<(and (srl_su GR64:$src, (i8 8)), (i64 255)),
  1589. (SUBREG_TO_REG
  1590. (i64 0),
  1591. (MOVZX32rr8_NOREX
  1592. (EXTRACT_SUBREG GR64:$src, sub_8bit_hi)),
  1593. sub_32bit)>;
  1594. def : Pat<(i64 (zext (srl_su GR16:$src, (i8 8)))),
  1595. (SUBREG_TO_REG
  1596. (i64 0),
  1597. (MOVZX32rr8_NOREX
  1598. (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)),
  1599. sub_32bit)>;
  1600. def : Pat<(i64 (anyext (srl_su GR16:$src, (i8 8)))),
  1601. (SUBREG_TO_REG
  1602. (i64 0),
  1603. (MOVZX32rr8_NOREX
  1604. (EXTRACT_SUBREG GR16:$src, sub_8bit_hi)),
  1605. sub_32bit)>;
  1606. // h-register extract and store.
  1607. def : Pat<(store (i8 (trunc_su (srl_su GR64:$src, (i8 8)))), addr:$dst),
  1608. (MOV8mr_NOREX
  1609. addr:$dst,
  1610. (EXTRACT_SUBREG GR64:$src, sub_8bit_hi))>;
  1611. def : Pat<(store (i8 (trunc_su (srl_su GR32:$src, (i8 8)))), addr:$dst),
  1612. (MOV8mr_NOREX
  1613. addr:$dst,
  1614. (EXTRACT_SUBREG GR32:$src, sub_8bit_hi))>,
  1615. Requires<[In64BitMode]>;
  1616. def : Pat<(store (i8 (trunc_su (srl_su GR16:$src, (i8 8)))), addr:$dst),
  1617. (MOV8mr_NOREX
  1618. addr:$dst,
  1619. (EXTRACT_SUBREG GR16:$src, sub_8bit_hi))>,
  1620. Requires<[In64BitMode]>;
  1621. // Special pattern to catch the last step of __builtin_parity handling. Our
  1622. // goal is to use an xor of an h-register with the corresponding l-register.
  1623. // The above patterns would handle this on non 64-bit targets, but for 64-bit
  1624. // we need to be more careful. We're using a NOREX instruction here in case
  1625. // register allocation fails to keep the two registers together. So we need to
  1626. // make sure we can't accidentally mix R8-R15 with an h-register.
  1627. def : Pat<(X86xor_flag (i8 (trunc GR32:$src)),
  1628. (i8 (trunc (srl_su GR32:$src, (i8 8))))),
  1629. (XOR8rr_NOREX (EXTRACT_SUBREG GR32:$src, sub_8bit),
  1630. (EXTRACT_SUBREG GR32:$src, sub_8bit_hi))>;
  1631. // (shl x, 1) ==> (add x, x)
  1632. // Note that if x is undef (immediate or otherwise), we could theoretically
  1633. // end up with the two uses of x getting different values, producing a result
  1634. // where the least significant bit is not 0. However, the probability of this
  1635. // happening is considered low enough that this is officially not a
  1636. // "real problem".
  1637. def : Pat<(shl GR8 :$src1, (i8 1)), (ADD8rr GR8 :$src1, GR8 :$src1)>;
  1638. def : Pat<(shl GR16:$src1, (i8 1)), (ADD16rr GR16:$src1, GR16:$src1)>;
  1639. def : Pat<(shl GR32:$src1, (i8 1)), (ADD32rr GR32:$src1, GR32:$src1)>;
  1640. def : Pat<(shl GR64:$src1, (i8 1)), (ADD64rr GR64:$src1, GR64:$src1)>;
  1641. def shiftMask8 : PatFrag<(ops node:$lhs), (and node:$lhs, imm), [{
  1642. return isUnneededShiftMask(N, 3);
  1643. }]>;
  1644. def shiftMask16 : PatFrag<(ops node:$lhs), (and node:$lhs, imm), [{
  1645. return isUnneededShiftMask(N, 4);
  1646. }]>;
  1647. def shiftMask32 : PatFrag<(ops node:$lhs), (and node:$lhs, imm), [{
  1648. return isUnneededShiftMask(N, 5);
  1649. }]>;
  1650. def shiftMask64 : PatFrag<(ops node:$lhs), (and node:$lhs, imm), [{
  1651. return isUnneededShiftMask(N, 6);
  1652. }]>;
  1653. // Shift amount is implicitly masked.
  1654. multiclass MaskedShiftAmountPats<SDNode frag, string name> {
  1655. // (shift x (and y, 31)) ==> (shift x, y)
  1656. def : Pat<(frag GR8:$src1, (shiftMask32 CL)),
  1657. (!cast<Instruction>(name # "8rCL") GR8:$src1)>;
  1658. def : Pat<(frag GR16:$src1, (shiftMask32 CL)),
  1659. (!cast<Instruction>(name # "16rCL") GR16:$src1)>;
  1660. def : Pat<(frag GR32:$src1, (shiftMask32 CL)),
  1661. (!cast<Instruction>(name # "32rCL") GR32:$src1)>;
  1662. def : Pat<(store (frag (loadi8 addr:$dst), (shiftMask32 CL)), addr:$dst),
  1663. (!cast<Instruction>(name # "8mCL") addr:$dst)>;
  1664. def : Pat<(store (frag (loadi16 addr:$dst), (shiftMask32 CL)), addr:$dst),
  1665. (!cast<Instruction>(name # "16mCL") addr:$dst)>;
  1666. def : Pat<(store (frag (loadi32 addr:$dst), (shiftMask32 CL)), addr:$dst),
  1667. (!cast<Instruction>(name # "32mCL") addr:$dst)>;
  1668. // (shift x (and y, 63)) ==> (shift x, y)
  1669. def : Pat<(frag GR64:$src1, (shiftMask64 CL)),
  1670. (!cast<Instruction>(name # "64rCL") GR64:$src1)>;
  1671. def : Pat<(store (frag (loadi64 addr:$dst), (shiftMask64 CL)), addr:$dst),
  1672. (!cast<Instruction>(name # "64mCL") addr:$dst)>;
  1673. }
  1674. defm : MaskedShiftAmountPats<shl, "SHL">;
  1675. defm : MaskedShiftAmountPats<srl, "SHR">;
  1676. defm : MaskedShiftAmountPats<sra, "SAR">;
  1677. // ROL/ROR instructions allow a stronger mask optimization than shift for 8- and
  1678. // 16-bit. We can remove a mask of any (bitwidth - 1) on the rotation amount
  1679. // because over-rotating produces the same result. This is noted in the Intel
  1680. // docs with: "tempCOUNT <- (COUNT & COUNTMASK) MOD SIZE". Masking the rotation
  1681. // amount could affect EFLAGS results, but that does not matter because we are
  1682. // not tracking flags for these nodes.
  1683. multiclass MaskedRotateAmountPats<SDNode frag, string name> {
  1684. // (rot x (and y, BitWidth - 1)) ==> (rot x, y)
  1685. def : Pat<(frag GR8:$src1, (shiftMask8 CL)),
  1686. (!cast<Instruction>(name # "8rCL") GR8:$src1)>;
  1687. def : Pat<(frag GR16:$src1, (shiftMask16 CL)),
  1688. (!cast<Instruction>(name # "16rCL") GR16:$src1)>;
  1689. def : Pat<(frag GR32:$src1, (shiftMask32 CL)),
  1690. (!cast<Instruction>(name # "32rCL") GR32:$src1)>;
  1691. def : Pat<(store (frag (loadi8 addr:$dst), (shiftMask8 CL)), addr:$dst),
  1692. (!cast<Instruction>(name # "8mCL") addr:$dst)>;
  1693. def : Pat<(store (frag (loadi16 addr:$dst), (shiftMask16 CL)), addr:$dst),
  1694. (!cast<Instruction>(name # "16mCL") addr:$dst)>;
  1695. def : Pat<(store (frag (loadi32 addr:$dst), (shiftMask32 CL)), addr:$dst),
  1696. (!cast<Instruction>(name # "32mCL") addr:$dst)>;
  1697. // (rot x (and y, 63)) ==> (rot x, y)
  1698. def : Pat<(frag GR64:$src1, (shiftMask64 CL)),
  1699. (!cast<Instruction>(name # "64rCL") GR64:$src1)>;
  1700. def : Pat<(store (frag (loadi64 addr:$dst), (shiftMask64 CL)), addr:$dst),
  1701. (!cast<Instruction>(name # "64mCL") addr:$dst)>;
  1702. }
  1703. defm : MaskedRotateAmountPats<rotl, "ROL">;
  1704. defm : MaskedRotateAmountPats<rotr, "ROR">;
  1705. // Double "funnel" shift amount is implicitly masked.
  1706. // (fshl/fshr x (and y, 31)) ==> (fshl/fshr x, y) (NOTE: modulo32)
  1707. def : Pat<(X86fshl GR16:$src1, GR16:$src2, (shiftMask32 CL)),
  1708. (SHLD16rrCL GR16:$src1, GR16:$src2)>;
  1709. def : Pat<(X86fshr GR16:$src2, GR16:$src1, (shiftMask32 CL)),
  1710. (SHRD16rrCL GR16:$src1, GR16:$src2)>;
  1711. // (fshl/fshr x (and y, 31)) ==> (fshl/fshr x, y)
  1712. def : Pat<(fshl GR32:$src1, GR32:$src2, (shiftMask32 CL)),
  1713. (SHLD32rrCL GR32:$src1, GR32:$src2)>;
  1714. def : Pat<(fshr GR32:$src2, GR32:$src1, (shiftMask32 CL)),
  1715. (SHRD32rrCL GR32:$src1, GR32:$src2)>;
  1716. // (fshl/fshr x (and y, 63)) ==> (fshl/fshr x, y)
  1717. def : Pat<(fshl GR64:$src1, GR64:$src2, (shiftMask64 CL)),
  1718. (SHLD64rrCL GR64:$src1, GR64:$src2)>;
  1719. def : Pat<(fshr GR64:$src2, GR64:$src1, (shiftMask64 CL)),
  1720. (SHRD64rrCL GR64:$src1, GR64:$src2)>;
  1721. let Predicates = [HasBMI2] in {
  1722. let AddedComplexity = 1 in {
  1723. def : Pat<(sra GR32:$src1, (shiftMask32 GR8:$src2)),
  1724. (SARX32rr GR32:$src1,
  1725. (INSERT_SUBREG
  1726. (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1727. def : Pat<(sra GR64:$src1, (shiftMask64 GR8:$src2)),
  1728. (SARX64rr GR64:$src1,
  1729. (INSERT_SUBREG
  1730. (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1731. def : Pat<(srl GR32:$src1, (shiftMask32 GR8:$src2)),
  1732. (SHRX32rr GR32:$src1,
  1733. (INSERT_SUBREG
  1734. (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1735. def : Pat<(srl GR64:$src1, (shiftMask64 GR8:$src2)),
  1736. (SHRX64rr GR64:$src1,
  1737. (INSERT_SUBREG
  1738. (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1739. def : Pat<(shl GR32:$src1, (shiftMask32 GR8:$src2)),
  1740. (SHLX32rr GR32:$src1,
  1741. (INSERT_SUBREG
  1742. (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1743. def : Pat<(shl GR64:$src1, (shiftMask64 GR8:$src2)),
  1744. (SHLX64rr GR64:$src1,
  1745. (INSERT_SUBREG
  1746. (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1747. }
  1748. def : Pat<(sra (loadi32 addr:$src1), (shiftMask32 GR8:$src2)),
  1749. (SARX32rm addr:$src1,
  1750. (INSERT_SUBREG
  1751. (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1752. def : Pat<(sra (loadi64 addr:$src1), (shiftMask64 GR8:$src2)),
  1753. (SARX64rm addr:$src1,
  1754. (INSERT_SUBREG
  1755. (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1756. def : Pat<(srl (loadi32 addr:$src1), (shiftMask32 GR8:$src2)),
  1757. (SHRX32rm addr:$src1,
  1758. (INSERT_SUBREG
  1759. (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1760. def : Pat<(srl (loadi64 addr:$src1), (shiftMask64 GR8:$src2)),
  1761. (SHRX64rm addr:$src1,
  1762. (INSERT_SUBREG
  1763. (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1764. def : Pat<(shl (loadi32 addr:$src1), (shiftMask32 GR8:$src2)),
  1765. (SHLX32rm addr:$src1,
  1766. (INSERT_SUBREG
  1767. (i32 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1768. def : Pat<(shl (loadi64 addr:$src1), (shiftMask64 GR8:$src2)),
  1769. (SHLX64rm addr:$src1,
  1770. (INSERT_SUBREG
  1771. (i64 (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1772. }
  1773. // Use BTR/BTS/BTC for clearing/setting/toggling a bit in a variable location.
  1774. multiclass one_bit_patterns<RegisterClass RC, ValueType VT, Instruction BTR,
  1775. Instruction BTS, Instruction BTC,
  1776. PatFrag ShiftMask> {
  1777. def : Pat<(and RC:$src1, (rotl -2, GR8:$src2)),
  1778. (BTR RC:$src1,
  1779. (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1780. def : Pat<(or RC:$src1, (shl 1, GR8:$src2)),
  1781. (BTS RC:$src1,
  1782. (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1783. def : Pat<(xor RC:$src1, (shl 1, GR8:$src2)),
  1784. (BTC RC:$src1,
  1785. (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1786. // Similar to above, but removing unneeded masking of the shift amount.
  1787. def : Pat<(and RC:$src1, (rotl -2, (ShiftMask GR8:$src2))),
  1788. (BTR RC:$src1,
  1789. (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1790. def : Pat<(or RC:$src1, (shl 1, (ShiftMask GR8:$src2))),
  1791. (BTS RC:$src1,
  1792. (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1793. def : Pat<(xor RC:$src1, (shl 1, (ShiftMask GR8:$src2))),
  1794. (BTC RC:$src1,
  1795. (INSERT_SUBREG (VT (IMPLICIT_DEF)), GR8:$src2, sub_8bit))>;
  1796. }
  1797. defm : one_bit_patterns<GR16, i16, BTR16rr, BTS16rr, BTC16rr, shiftMask16>;
  1798. defm : one_bit_patterns<GR32, i32, BTR32rr, BTS32rr, BTC32rr, shiftMask32>;
  1799. defm : one_bit_patterns<GR64, i64, BTR64rr, BTS64rr, BTC64rr, shiftMask64>;
  1800. //===----------------------------------------------------------------------===//
  1801. // EFLAGS-defining Patterns
  1802. //===----------------------------------------------------------------------===//
  1803. // add reg, reg
  1804. def : Pat<(add GR8 :$src1, GR8 :$src2), (ADD8rr GR8 :$src1, GR8 :$src2)>;
  1805. def : Pat<(add GR16:$src1, GR16:$src2), (ADD16rr GR16:$src1, GR16:$src2)>;
  1806. def : Pat<(add GR32:$src1, GR32:$src2), (ADD32rr GR32:$src1, GR32:$src2)>;
  1807. def : Pat<(add GR64:$src1, GR64:$src2), (ADD64rr GR64:$src1, GR64:$src2)>;
  1808. // add reg, mem
  1809. def : Pat<(add GR8:$src1, (loadi8 addr:$src2)),
  1810. (ADD8rm GR8:$src1, addr:$src2)>;
  1811. def : Pat<(add GR16:$src1, (loadi16 addr:$src2)),
  1812. (ADD16rm GR16:$src1, addr:$src2)>;
  1813. def : Pat<(add GR32:$src1, (loadi32 addr:$src2)),
  1814. (ADD32rm GR32:$src1, addr:$src2)>;
  1815. def : Pat<(add GR64:$src1, (loadi64 addr:$src2)),
  1816. (ADD64rm GR64:$src1, addr:$src2)>;
  1817. // add reg, imm
  1818. def : Pat<(add GR8 :$src1, imm:$src2), (ADD8ri GR8:$src1 , imm:$src2)>;
  1819. def : Pat<(add GR16:$src1, imm:$src2), (ADD16ri GR16:$src1, imm:$src2)>;
  1820. def : Pat<(add GR32:$src1, imm:$src2), (ADD32ri GR32:$src1, imm:$src2)>;
  1821. def : Pat<(add GR16:$src1, i16immSExt8:$src2),
  1822. (ADD16ri8 GR16:$src1, i16immSExt8:$src2)>;
  1823. def : Pat<(add GR32:$src1, i32immSExt8:$src2),
  1824. (ADD32ri8 GR32:$src1, i32immSExt8:$src2)>;
  1825. def : Pat<(add GR64:$src1, i64immSExt8:$src2),
  1826. (ADD64ri8 GR64:$src1, i64immSExt8:$src2)>;
  1827. def : Pat<(add GR64:$src1, i64immSExt32:$src2),
  1828. (ADD64ri32 GR64:$src1, i64immSExt32:$src2)>;
  1829. // sub reg, reg
  1830. def : Pat<(sub GR8 :$src1, GR8 :$src2), (SUB8rr GR8 :$src1, GR8 :$src2)>;
  1831. def : Pat<(sub GR16:$src1, GR16:$src2), (SUB16rr GR16:$src1, GR16:$src2)>;
  1832. def : Pat<(sub GR32:$src1, GR32:$src2), (SUB32rr GR32:$src1, GR32:$src2)>;
  1833. def : Pat<(sub GR64:$src1, GR64:$src2), (SUB64rr GR64:$src1, GR64:$src2)>;
  1834. // sub reg, mem
  1835. def : Pat<(sub GR8:$src1, (loadi8 addr:$src2)),
  1836. (SUB8rm GR8:$src1, addr:$src2)>;
  1837. def : Pat<(sub GR16:$src1, (loadi16 addr:$src2)),
  1838. (SUB16rm GR16:$src1, addr:$src2)>;
  1839. def : Pat<(sub GR32:$src1, (loadi32 addr:$src2)),
  1840. (SUB32rm GR32:$src1, addr:$src2)>;
  1841. def : Pat<(sub GR64:$src1, (loadi64 addr:$src2)),
  1842. (SUB64rm GR64:$src1, addr:$src2)>;
  1843. // sub reg, imm
  1844. def : Pat<(sub GR8:$src1, imm:$src2),
  1845. (SUB8ri GR8:$src1, imm:$src2)>;
  1846. def : Pat<(sub GR16:$src1, imm:$src2),
  1847. (SUB16ri GR16:$src1, imm:$src2)>;
  1848. def : Pat<(sub GR32:$src1, imm:$src2),
  1849. (SUB32ri GR32:$src1, imm:$src2)>;
  1850. def : Pat<(sub GR16:$src1, i16immSExt8:$src2),
  1851. (SUB16ri8 GR16:$src1, i16immSExt8:$src2)>;
  1852. def : Pat<(sub GR32:$src1, i32immSExt8:$src2),
  1853. (SUB32ri8 GR32:$src1, i32immSExt8:$src2)>;
  1854. def : Pat<(sub GR64:$src1, i64immSExt8:$src2),
  1855. (SUB64ri8 GR64:$src1, i64immSExt8:$src2)>;
  1856. def : Pat<(sub GR64:$src1, i64immSExt32:$src2),
  1857. (SUB64ri32 GR64:$src1, i64immSExt32:$src2)>;
  1858. // sub 0, reg
  1859. def : Pat<(X86sub_flag 0, GR8 :$src), (NEG8r GR8 :$src)>;
  1860. def : Pat<(X86sub_flag 0, GR16:$src), (NEG16r GR16:$src)>;
  1861. def : Pat<(X86sub_flag 0, GR32:$src), (NEG32r GR32:$src)>;
  1862. def : Pat<(X86sub_flag 0, GR64:$src), (NEG64r GR64:$src)>;
  1863. // mul reg, reg
  1864. def : Pat<(mul GR16:$src1, GR16:$src2),
  1865. (IMUL16rr GR16:$src1, GR16:$src2)>;
  1866. def : Pat<(mul GR32:$src1, GR32:$src2),
  1867. (IMUL32rr GR32:$src1, GR32:$src2)>;
  1868. def : Pat<(mul GR64:$src1, GR64:$src2),
  1869. (IMUL64rr GR64:$src1, GR64:$src2)>;
  1870. // mul reg, mem
  1871. def : Pat<(mul GR16:$src1, (loadi16 addr:$src2)),
  1872. (IMUL16rm GR16:$src1, addr:$src2)>;
  1873. def : Pat<(mul GR32:$src1, (loadi32 addr:$src2)),
  1874. (IMUL32rm GR32:$src1, addr:$src2)>;
  1875. def : Pat<(mul GR64:$src1, (loadi64 addr:$src2)),
  1876. (IMUL64rm GR64:$src1, addr:$src2)>;
  1877. // mul reg, imm
  1878. def : Pat<(mul GR16:$src1, imm:$src2),
  1879. (IMUL16rri GR16:$src1, imm:$src2)>;
  1880. def : Pat<(mul GR32:$src1, imm:$src2),
  1881. (IMUL32rri GR32:$src1, imm:$src2)>;
  1882. def : Pat<(mul GR16:$src1, i16immSExt8:$src2),
  1883. (IMUL16rri8 GR16:$src1, i16immSExt8:$src2)>;
  1884. def : Pat<(mul GR32:$src1, i32immSExt8:$src2),
  1885. (IMUL32rri8 GR32:$src1, i32immSExt8:$src2)>;
  1886. def : Pat<(mul GR64:$src1, i64immSExt8:$src2),
  1887. (IMUL64rri8 GR64:$src1, i64immSExt8:$src2)>;
  1888. def : Pat<(mul GR64:$src1, i64immSExt32:$src2),
  1889. (IMUL64rri32 GR64:$src1, i64immSExt32:$src2)>;
  1890. // reg = mul mem, imm
  1891. def : Pat<(mul (loadi16 addr:$src1), imm:$src2),
  1892. (IMUL16rmi addr:$src1, imm:$src2)>;
  1893. def : Pat<(mul (loadi32 addr:$src1), imm:$src2),
  1894. (IMUL32rmi addr:$src1, imm:$src2)>;
  1895. def : Pat<(mul (loadi16 addr:$src1), i16immSExt8:$src2),
  1896. (IMUL16rmi8 addr:$src1, i16immSExt8:$src2)>;
  1897. def : Pat<(mul (loadi32 addr:$src1), i32immSExt8:$src2),
  1898. (IMUL32rmi8 addr:$src1, i32immSExt8:$src2)>;
  1899. def : Pat<(mul (loadi64 addr:$src1), i64immSExt8:$src2),
  1900. (IMUL64rmi8 addr:$src1, i64immSExt8:$src2)>;
  1901. def : Pat<(mul (loadi64 addr:$src1), i64immSExt32:$src2),
  1902. (IMUL64rmi32 addr:$src1, i64immSExt32:$src2)>;
  1903. // Increment/Decrement reg.
  1904. // Do not make INC/DEC if it is slow
  1905. let Predicates = [UseIncDec] in {
  1906. def : Pat<(add GR8:$src, 1), (INC8r GR8:$src)>;
  1907. def : Pat<(add GR16:$src, 1), (INC16r GR16:$src)>;
  1908. def : Pat<(add GR32:$src, 1), (INC32r GR32:$src)>;
  1909. def : Pat<(add GR64:$src, 1), (INC64r GR64:$src)>;
  1910. def : Pat<(add GR8:$src, -1), (DEC8r GR8:$src)>;
  1911. def : Pat<(add GR16:$src, -1), (DEC16r GR16:$src)>;
  1912. def : Pat<(add GR32:$src, -1), (DEC32r GR32:$src)>;
  1913. def : Pat<(add GR64:$src, -1), (DEC64r GR64:$src)>;
  1914. def : Pat<(X86add_flag_nocf GR8:$src, -1), (DEC8r GR8:$src)>;
  1915. def : Pat<(X86add_flag_nocf GR16:$src, -1), (DEC16r GR16:$src)>;
  1916. def : Pat<(X86add_flag_nocf GR32:$src, -1), (DEC32r GR32:$src)>;
  1917. def : Pat<(X86add_flag_nocf GR64:$src, -1), (DEC64r GR64:$src)>;
  1918. def : Pat<(X86sub_flag_nocf GR8:$src, -1), (INC8r GR8:$src)>;
  1919. def : Pat<(X86sub_flag_nocf GR16:$src, -1), (INC16r GR16:$src)>;
  1920. def : Pat<(X86sub_flag_nocf GR32:$src, -1), (INC32r GR32:$src)>;
  1921. def : Pat<(X86sub_flag_nocf GR64:$src, -1), (INC64r GR64:$src)>;
  1922. }
  1923. // or reg/reg.
  1924. def : Pat<(or GR8 :$src1, GR8 :$src2), (OR8rr GR8 :$src1, GR8 :$src2)>;
  1925. def : Pat<(or GR16:$src1, GR16:$src2), (OR16rr GR16:$src1, GR16:$src2)>;
  1926. def : Pat<(or GR32:$src1, GR32:$src2), (OR32rr GR32:$src1, GR32:$src2)>;
  1927. def : Pat<(or GR64:$src1, GR64:$src2), (OR64rr GR64:$src1, GR64:$src2)>;
  1928. // or reg/mem
  1929. def : Pat<(or GR8:$src1, (loadi8 addr:$src2)),
  1930. (OR8rm GR8:$src1, addr:$src2)>;
  1931. def : Pat<(or GR16:$src1, (loadi16 addr:$src2)),
  1932. (OR16rm GR16:$src1, addr:$src2)>;
  1933. def : Pat<(or GR32:$src1, (loadi32 addr:$src2)),
  1934. (OR32rm GR32:$src1, addr:$src2)>;
  1935. def : Pat<(or GR64:$src1, (loadi64 addr:$src2)),
  1936. (OR64rm GR64:$src1, addr:$src2)>;
  1937. // or reg/imm
  1938. def : Pat<(or GR8:$src1 , imm:$src2), (OR8ri GR8 :$src1, imm:$src2)>;
  1939. def : Pat<(or GR16:$src1, imm:$src2), (OR16ri GR16:$src1, imm:$src2)>;
  1940. def : Pat<(or GR32:$src1, imm:$src2), (OR32ri GR32:$src1, imm:$src2)>;
  1941. def : Pat<(or GR16:$src1, i16immSExt8:$src2),
  1942. (OR16ri8 GR16:$src1, i16immSExt8:$src2)>;
  1943. def : Pat<(or GR32:$src1, i32immSExt8:$src2),
  1944. (OR32ri8 GR32:$src1, i32immSExt8:$src2)>;
  1945. def : Pat<(or GR64:$src1, i64immSExt8:$src2),
  1946. (OR64ri8 GR64:$src1, i64immSExt8:$src2)>;
  1947. def : Pat<(or GR64:$src1, i64immSExt32:$src2),
  1948. (OR64ri32 GR64:$src1, i64immSExt32:$src2)>;
  1949. // xor reg/reg
  1950. def : Pat<(xor GR8 :$src1, GR8 :$src2), (XOR8rr GR8 :$src1, GR8 :$src2)>;
  1951. def : Pat<(xor GR16:$src1, GR16:$src2), (XOR16rr GR16:$src1, GR16:$src2)>;
  1952. def : Pat<(xor GR32:$src1, GR32:$src2), (XOR32rr GR32:$src1, GR32:$src2)>;
  1953. def : Pat<(xor GR64:$src1, GR64:$src2), (XOR64rr GR64:$src1, GR64:$src2)>;
  1954. // xor reg/mem
  1955. def : Pat<(xor GR8:$src1, (loadi8 addr:$src2)),
  1956. (XOR8rm GR8:$src1, addr:$src2)>;
  1957. def : Pat<(xor GR16:$src1, (loadi16 addr:$src2)),
  1958. (XOR16rm GR16:$src1, addr:$src2)>;
  1959. def : Pat<(xor GR32:$src1, (loadi32 addr:$src2)),
  1960. (XOR32rm GR32:$src1, addr:$src2)>;
  1961. def : Pat<(xor GR64:$src1, (loadi64 addr:$src2)),
  1962. (XOR64rm GR64:$src1, addr:$src2)>;
  1963. // xor reg/imm
  1964. def : Pat<(xor GR8:$src1, imm:$src2),
  1965. (XOR8ri GR8:$src1, imm:$src2)>;
  1966. def : Pat<(xor GR16:$src1, imm:$src2),
  1967. (XOR16ri GR16:$src1, imm:$src2)>;
  1968. def : Pat<(xor GR32:$src1, imm:$src2),
  1969. (XOR32ri GR32:$src1, imm:$src2)>;
  1970. def : Pat<(xor GR16:$src1, i16immSExt8:$src2),
  1971. (XOR16ri8 GR16:$src1, i16immSExt8:$src2)>;
  1972. def : Pat<(xor GR32:$src1, i32immSExt8:$src2),
  1973. (XOR32ri8 GR32:$src1, i32immSExt8:$src2)>;
  1974. def : Pat<(xor GR64:$src1, i64immSExt8:$src2),
  1975. (XOR64ri8 GR64:$src1, i64immSExt8:$src2)>;
  1976. def : Pat<(xor GR64:$src1, i64immSExt32:$src2),
  1977. (XOR64ri32 GR64:$src1, i64immSExt32:$src2)>;
  1978. // and reg/reg
  1979. def : Pat<(and GR8 :$src1, GR8 :$src2), (AND8rr GR8 :$src1, GR8 :$src2)>;
  1980. def : Pat<(and GR16:$src1, GR16:$src2), (AND16rr GR16:$src1, GR16:$src2)>;
  1981. def : Pat<(and GR32:$src1, GR32:$src2), (AND32rr GR32:$src1, GR32:$src2)>;
  1982. def : Pat<(and GR64:$src1, GR64:$src2), (AND64rr GR64:$src1, GR64:$src2)>;
  1983. // and reg/mem
  1984. def : Pat<(and GR8:$src1, (loadi8 addr:$src2)),
  1985. (AND8rm GR8:$src1, addr:$src2)>;
  1986. def : Pat<(and GR16:$src1, (loadi16 addr:$src2)),
  1987. (AND16rm GR16:$src1, addr:$src2)>;
  1988. def : Pat<(and GR32:$src1, (loadi32 addr:$src2)),
  1989. (AND32rm GR32:$src1, addr:$src2)>;
  1990. def : Pat<(and GR64:$src1, (loadi64 addr:$src2)),
  1991. (AND64rm GR64:$src1, addr:$src2)>;
  1992. // and reg/imm
  1993. def : Pat<(and GR8:$src1, imm:$src2),
  1994. (AND8ri GR8:$src1, imm:$src2)>;
  1995. def : Pat<(and GR16:$src1, imm:$src2),
  1996. (AND16ri GR16:$src1, imm:$src2)>;
  1997. def : Pat<(and GR32:$src1, imm:$src2),
  1998. (AND32ri GR32:$src1, imm:$src2)>;
  1999. def : Pat<(and GR16:$src1, i16immSExt8:$src2),
  2000. (AND16ri8 GR16:$src1, i16immSExt8:$src2)>;
  2001. def : Pat<(and GR32:$src1, i32immSExt8:$src2),
  2002. (AND32ri8 GR32:$src1, i32immSExt8:$src2)>;
  2003. def : Pat<(and GR64:$src1, i64immSExt8:$src2),
  2004. (AND64ri8 GR64:$src1, i64immSExt8:$src2)>;
  2005. def : Pat<(and GR64:$src1, i64immSExt32:$src2),
  2006. (AND64ri32 GR64:$src1, i64immSExt32:$src2)>;
  2007. // Bit scan instruction patterns to match explicit zero-undef behavior.
  2008. def : Pat<(cttz_zero_undef GR16:$src), (BSF16rr GR16:$src)>;
  2009. def : Pat<(cttz_zero_undef GR32:$src), (BSF32rr GR32:$src)>;
  2010. def : Pat<(cttz_zero_undef GR64:$src), (BSF64rr GR64:$src)>;
  2011. def : Pat<(cttz_zero_undef (loadi16 addr:$src)), (BSF16rm addr:$src)>;
  2012. def : Pat<(cttz_zero_undef (loadi32 addr:$src)), (BSF32rm addr:$src)>;
  2013. def : Pat<(cttz_zero_undef (loadi64 addr:$src)), (BSF64rm addr:$src)>;
  2014. // When HasMOVBE is enabled it is possible to get a non-legalized
  2015. // register-register 16 bit bswap. This maps it to a ROL instruction.
  2016. let Predicates = [HasMOVBE] in {
  2017. def : Pat<(bswap GR16:$src), (ROL16ri GR16:$src, (i8 8))>;
  2018. }