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