X86InstrCompiler.td 99 KB

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