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