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