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