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