Fix incorrect assert that should be a user error for code like 'mov $0, %%eax'.
[oota-llvm.git] / lib / Target / X86 / X86Instr64bit.td
1 //====- X86Instr64bit.td - Describe X86-64 Instructions ----*- tablegen -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes the X86-64 instruction set, defining the instructions,
11 // and properties of the instructions which are needed for code generation,
12 // machine code emission, and analysis.
13 //
14 //===----------------------------------------------------------------------===//
15
16 //===----------------------------------------------------------------------===//
17 // Operand Definitions.
18 //
19
20 // 64-bits but only 32 bits are significant.
21 def i64i32imm  : Operand<i64>;
22
23 // 64-bits but only 32 bits are significant, and those bits are treated as being
24 // pc relative.
25 def i64i32imm_pcrel : Operand<i64> {
26   let PrintMethod = "print_pcrel_imm";
27 }
28
29
30 // 64-bits but only 8 bits are significant.
31 def i64i8imm   : Operand<i64> {
32   let ParserMatchClass = ImmSExt8AsmOperand;
33 }
34
35 def lea64mem : Operand<i64> {
36   let PrintMethod = "printlea64mem";
37   let MIOperandInfo = (ops GR64, i8imm, GR64_NOSP, i32imm);
38   let ParserMatchClass = X86MemAsmOperand;
39 }
40
41 def lea64_32mem : Operand<i32> {
42   let PrintMethod = "printlea64_32mem";
43   let AsmOperandLowerMethod = "lower_lea64_32mem";
44   let MIOperandInfo = (ops GR32, i8imm, GR32_NOSP, i32imm);
45   let ParserMatchClass = X86MemAsmOperand;
46 }
47
48 //===----------------------------------------------------------------------===//
49 // Complex Pattern Definitions.
50 //
51 def lea64addr : ComplexPattern<i64, 4, "SelectLEAAddr",
52                         [add, sub, mul, X86mul_imm, shl, or, frameindex,
53                          X86WrapperRIP], []>;
54
55 def tls64addr : ComplexPattern<i64, 4, "SelectTLSADDRAddr",
56                                [tglobaltlsaddr], []>;
57
58 //===----------------------------------------------------------------------===//
59 // Pattern fragments.
60 //
61
62 def i64immSExt8  : PatLeaf<(i64 imm), [{
63   // i64immSExt8 predicate - True if the 64-bit immediate fits in a 8-bit
64   // sign extended field.
65   return (int64_t)N->getZExtValue() == (int8_t)N->getZExtValue();
66 }]>;
67
68 def i64immSExt32  : PatLeaf<(i64 imm), [{
69   // i64immSExt32 predicate - True if the 64-bit immediate fits in a 32-bit
70   // sign extended field.
71   return (int64_t)N->getZExtValue() == (int32_t)N->getZExtValue();
72 }]>;
73
74 def i64immZExt32  : PatLeaf<(i64 imm), [{
75   // i64immZExt32 predicate - True if the 64-bit immediate fits in a 32-bit
76   // unsignedsign extended field.
77   return (uint64_t)N->getZExtValue() == (uint32_t)N->getZExtValue();
78 }]>;
79
80 def sextloadi64i8  : PatFrag<(ops node:$ptr), (i64 (sextloadi8 node:$ptr))>;
81 def sextloadi64i16 : PatFrag<(ops node:$ptr), (i64 (sextloadi16 node:$ptr))>;
82 def sextloadi64i32 : PatFrag<(ops node:$ptr), (i64 (sextloadi32 node:$ptr))>;
83
84 def zextloadi64i1  : PatFrag<(ops node:$ptr), (i64 (zextloadi1 node:$ptr))>;
85 def zextloadi64i8  : PatFrag<(ops node:$ptr), (i64 (zextloadi8 node:$ptr))>;
86 def zextloadi64i16 : PatFrag<(ops node:$ptr), (i64 (zextloadi16 node:$ptr))>;
87 def zextloadi64i32 : PatFrag<(ops node:$ptr), (i64 (zextloadi32 node:$ptr))>;
88
89 def extloadi64i1   : PatFrag<(ops node:$ptr), (i64 (extloadi1 node:$ptr))>;
90 def extloadi64i8   : PatFrag<(ops node:$ptr), (i64 (extloadi8 node:$ptr))>;
91 def extloadi64i16  : PatFrag<(ops node:$ptr), (i64 (extloadi16 node:$ptr))>;
92 def extloadi64i32  : PatFrag<(ops node:$ptr), (i64 (extloadi32 node:$ptr))>;
93
94 //===----------------------------------------------------------------------===//
95 // Instruction list...
96 //
97
98 // ADJCALLSTACKDOWN/UP implicitly use/def RSP because they may be expanded into
99 // a stack adjustment and the codegen must know that they may modify the stack
100 // pointer before prolog-epilog rewriting occurs.
101 // Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become
102 // sub / add which can clobber EFLAGS.
103 let Defs = [RSP, EFLAGS], Uses = [RSP] in {
104 def ADJCALLSTACKDOWN64 : I<0, Pseudo, (outs), (ins i32imm:$amt),
105                            "#ADJCALLSTACKDOWN",
106                            [(X86callseq_start timm:$amt)]>,
107                           Requires<[In64BitMode]>;
108 def ADJCALLSTACKUP64   : I<0, Pseudo, (outs), (ins i32imm:$amt1, i32imm:$amt2),
109                            "#ADJCALLSTACKUP",
110                            [(X86callseq_end timm:$amt1, timm:$amt2)]>,
111                           Requires<[In64BitMode]>;
112 }
113
114 //===----------------------------------------------------------------------===//
115 //  Call Instructions...
116 //
117 let isCall = 1 in
118   // All calls clobber the non-callee saved registers. RSP is marked as
119   // a use to prevent stack-pointer assignments that appear immediately
120   // before calls from potentially appearing dead. Uses for argument
121   // registers are added manually.
122   let Defs = [RAX, RCX, RDX, RSI, RDI, R8, R9, R10, R11,
123               FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0, ST1,
124               MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
125               XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
126               XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS],
127       Uses = [RSP] in {
128       
129     // NOTE: this pattern doesn't match "X86call imm", because we do not know
130     // that the offset between an arbitrary immediate and the call will fit in
131     // the 32-bit pcrel field that we have.
132     def CALL64pcrel32 : Ii32<0xE8, RawFrm,
133                           (outs), (ins i64i32imm_pcrel:$dst, variable_ops),
134                           "call\t$dst", []>,
135                         Requires<[In64BitMode, NotWin64]>;
136     def CALL64r       : I<0xFF, MRM2r, (outs), (ins GR64:$dst, variable_ops),
137                           "call\t{*}$dst", [(X86call GR64:$dst)]>,
138                         Requires<[NotWin64]>;
139     def CALL64m       : I<0xFF, MRM2m, (outs), (ins i64mem:$dst, variable_ops),
140                           "call\t{*}$dst", [(X86call (loadi64 addr:$dst))]>,
141                         Requires<[NotWin64]>;
142                         
143     def FARCALL64   : RI<0xFF, MRM3m, (outs), (ins opaque80mem:$dst),
144                          "lcall{q}\t{*}$dst", []>;
145   }
146
147   // FIXME: We need to teach codegen about single list of call-clobbered registers.
148 let isCall = 1 in
149   // All calls clobber the non-callee saved registers. RSP is marked as
150   // a use to prevent stack-pointer assignments that appear immediately
151   // before calls from potentially appearing dead. Uses for argument
152   // registers are added manually.
153   let Defs = [RAX, RCX, RDX, R8, R9, R10, R11,
154               FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0, ST1,
155               MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
156               XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, EFLAGS],
157       Uses = [RSP] in {
158     def WINCALL64pcrel32 : I<0xE8, RawFrm,
159                              (outs), (ins i64i32imm_pcrel:$dst, variable_ops),
160                              "call\t$dst", []>,
161                            Requires<[IsWin64]>;
162     def WINCALL64r       : I<0xFF, MRM2r, (outs), (ins GR64:$dst, variable_ops),
163                              "call\t{*}$dst",
164                              [(X86call GR64:$dst)]>, Requires<[IsWin64]>;
165     def WINCALL64m       : I<0xFF, MRM2m, (outs), (ins i64mem:$dst, variable_ops),
166                              "call\t{*}$dst",
167                              [(X86call (loadi64 addr:$dst))]>, Requires<[IsWin64]>;
168   }
169
170
171 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1 in
172 def TCRETURNdi64 : I<0, Pseudo, (outs), (ins i64imm:$dst, i32imm:$offset,
173                                          variable_ops),
174                  "#TC_RETURN $dst $offset",
175                  []>;
176
177 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1 in
178 def TCRETURNri64 : I<0, Pseudo, (outs), (ins GR64:$dst, i32imm:$offset,
179                                          variable_ops),
180                  "#TC_RETURN $dst $offset",
181                  []>;
182
183
184 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1 in
185   def TAILJMPr64 : I<0xFF, MRM4r, (outs), (ins GR64:$dst),
186                    "jmp{q}\t{*}$dst  # TAILCALL",
187                    []>;     
188
189 // Branches
190 let isBranch = 1, isTerminator = 1, isBarrier = 1, isIndirectBranch = 1 in {
191   def JMP64r     : I<0xFF, MRM4r, (outs), (ins GR64:$dst), "jmp{q}\t{*}$dst",
192                      [(brind GR64:$dst)]>;
193   def JMP64m     : I<0xFF, MRM4m, (outs), (ins i64mem:$dst), "jmp{q}\t{*}$dst",
194                      [(brind (loadi64 addr:$dst))]>;
195   def FARJMP64   : RI<0xFF, MRM5m, (outs), (ins opaque80mem:$dst),
196                       "ljmp{q}\t{*}$dst", []>;
197 }
198
199 //===----------------------------------------------------------------------===//
200 // EH Pseudo Instructions
201 //
202 let isTerminator = 1, isReturn = 1, isBarrier = 1,
203     hasCtrlDep = 1 in {
204 def EH_RETURN64   : I<0xC3, RawFrm, (outs), (ins GR64:$addr),
205                      "ret\t#eh_return, addr: $addr",
206                      [(X86ehret GR64:$addr)]>;
207
208 }
209
210 //===----------------------------------------------------------------------===//
211 //  Miscellaneous Instructions...
212 //
213 let Defs = [RBP,RSP], Uses = [RBP,RSP], mayLoad = 1, neverHasSideEffects = 1 in
214 def LEAVE64  : I<0xC9, RawFrm,
215                  (outs), (ins), "leave", []>;
216 let Defs = [RSP], Uses = [RSP], neverHasSideEffects=1 in {
217 let mayLoad = 1 in {
218 def POP64r   : I<0x58, AddRegFrm,
219                  (outs GR64:$reg), (ins), "pop{q}\t$reg", []>;
220 def POP64rmr: I<0x8F, MRM0r, (outs GR64:$reg), (ins), "pop{q}\t$reg", []>;
221 def POP64rmm: I<0x8F, MRM0m, (outs i64mem:$dst), (ins), "pop{q}\t$dst", []>;
222 }
223 let mayStore = 1 in {
224 def PUSH64r  : I<0x50, AddRegFrm,
225                  (outs), (ins GR64:$reg), "push{q}\t$reg", []>;
226 def PUSH64rmr: I<0xFF, MRM6r, (outs), (ins GR64:$reg), "push{q}\t$reg", []>;
227 def PUSH64rmm: I<0xFF, MRM6m, (outs), (ins i64mem:$src), "push{q}\t$src", []>;
228 }
229 }
230
231 let Defs = [RSP], Uses = [RSP], neverHasSideEffects = 1, mayStore = 1 in {
232 def PUSH64i8   : Ii8<0x6a, RawFrm, (outs), (ins i8imm:$imm), 
233                      "push{q}\t$imm", []>;
234 def PUSH64i16  : Ii16<0x68, RawFrm, (outs), (ins i16imm:$imm), 
235                       "push{q}\t$imm", []>;
236 def PUSH64i32  : Ii32<0x68, RawFrm, (outs), (ins i32imm:$imm), 
237                       "push{q}\t$imm", []>;
238 }
239
240 let Defs = [RSP, EFLAGS], Uses = [RSP], mayLoad = 1 in
241 def POPFQ    : I<0x9D, RawFrm, (outs), (ins), "popf", []>, REX_W;
242 let Defs = [RSP], Uses = [RSP, EFLAGS], mayStore = 1 in
243 def PUSHFQ   : I<0x9C, RawFrm, (outs), (ins), "pushf", []>;
244
245 def LEA64_32r : I<0x8D, MRMSrcMem,
246                   (outs GR32:$dst), (ins lea64_32mem:$src),
247                   "lea{l}\t{$src|$dst}, {$dst|$src}",
248                   [(set GR32:$dst, lea32addr:$src)]>, Requires<[In64BitMode]>;
249
250 let isReMaterializable = 1 in
251 def LEA64r   : RI<0x8D, MRMSrcMem, (outs GR64:$dst), (ins lea64mem:$src),
252                   "lea{q}\t{$src|$dst}, {$dst|$src}",
253                   [(set GR64:$dst, lea64addr:$src)]>;
254
255 let isTwoAddress = 1 in
256 def BSWAP64r : RI<0xC8, AddRegFrm, (outs GR64:$dst), (ins GR64:$src),
257                   "bswap{q}\t$dst", 
258                   [(set GR64:$dst, (bswap GR64:$src))]>, TB;
259
260 // Bit scan instructions.
261 let Defs = [EFLAGS] in {
262 def BSF64rr  : RI<0xBC, MRMSrcReg, (outs GR64:$dst), (ins GR64:$src),
263                   "bsf{q}\t{$src, $dst|$dst, $src}",
264                   [(set GR64:$dst, (X86bsf GR64:$src)), (implicit EFLAGS)]>, TB;
265 def BSF64rm  : RI<0xBC, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
266                   "bsf{q}\t{$src, $dst|$dst, $src}",
267                   [(set GR64:$dst, (X86bsf (loadi64 addr:$src))),
268                    (implicit EFLAGS)]>, TB;
269
270 def BSR64rr  : RI<0xBD, MRMSrcReg, (outs GR64:$dst), (ins GR64:$src),
271                   "bsr{q}\t{$src, $dst|$dst, $src}",
272                   [(set GR64:$dst, (X86bsr GR64:$src)), (implicit EFLAGS)]>, TB;
273 def BSR64rm  : RI<0xBD, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
274                   "bsr{q}\t{$src, $dst|$dst, $src}",
275                   [(set GR64:$dst, (X86bsr (loadi64 addr:$src))),
276                    (implicit EFLAGS)]>, TB;
277 } // Defs = [EFLAGS]
278
279 // Repeat string ops
280 let Defs = [RCX,RDI,RSI], Uses = [RCX,RDI,RSI] in
281 def REP_MOVSQ : RI<0xA5, RawFrm, (outs), (ins), "{rep;movsq|rep movsq}",
282                    [(X86rep_movs i64)]>, REP;
283 let Defs = [RCX,RDI], Uses = [RAX,RCX,RDI] in
284 def REP_STOSQ : RI<0xAB, RawFrm, (outs), (ins), "{rep;stosq|rep stosq}",
285                    [(X86rep_stos i64)]>, REP;
286
287 def SCAS64 : RI<0xAF, RawFrm, (outs), (ins), "scas{q}", []>;
288
289 def CMPS64 : RI<0xA7, RawFrm, (outs), (ins), "cmps{q}", []>;
290
291 // Fast system-call instructions
292 def SYSEXIT64 : RI<0x35, RawFrm,
293                    (outs), (ins), "sysexit", []>, TB;
294
295 //===----------------------------------------------------------------------===//
296 //  Move Instructions...
297 //
298
299 let neverHasSideEffects = 1 in
300 def MOV64rr : RI<0x89, MRMDestReg, (outs GR64:$dst), (ins GR64:$src),
301                  "mov{q}\t{$src, $dst|$dst, $src}", []>;
302
303 let isReMaterializable = 1, isAsCheapAsAMove = 1  in {
304 def MOV64ri : RIi64<0xB8, AddRegFrm, (outs GR64:$dst), (ins i64imm:$src),
305                     "movabs{q}\t{$src, $dst|$dst, $src}",
306                     [(set GR64:$dst, imm:$src)]>;
307 def MOV64ri32 : RIi32<0xC7, MRM0r, (outs GR64:$dst), (ins i64i32imm:$src),
308                       "mov{q}\t{$src, $dst|$dst, $src}",
309                       [(set GR64:$dst, i64immSExt32:$src)]>;
310 }
311
312 let canFoldAsLoad = 1 in
313 def MOV64rm : RI<0x8B, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
314                  "mov{q}\t{$src, $dst|$dst, $src}",
315                  [(set GR64:$dst, (load addr:$src))]>;
316
317 def MOV64mr : RI<0x89, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
318                  "mov{q}\t{$src, $dst|$dst, $src}",
319                  [(store GR64:$src, addr:$dst)]>;
320 def MOV64mi32 : RIi32<0xC7, MRM0m, (outs), (ins i64mem:$dst, i64i32imm:$src),
321                       "mov{q}\t{$src, $dst|$dst, $src}",
322                       [(store i64immSExt32:$src, addr:$dst)]>;
323
324 def MOV64o8a : RIi8<0xA0, RawFrm, (outs), (ins i8imm:$src),
325                       "mov{q}\t{$src, %rax|%rax, $src}", []>;
326 def MOV64o32a : RIi32<0xA1, RawFrm, (outs), (ins i32imm:$src),
327                        "mov{q}\t{$src, %rax|%rax, $src}", []>;
328 def MOV64ao8 : RIi8<0xA2, RawFrm, (outs i8imm:$dst), (ins),
329                        "mov{q}\t{%rax, $dst|$dst, %rax}", []>;
330 def MOV64ao32 : RIi32<0xA3, RawFrm, (outs i32imm:$dst), (ins),
331                        "mov{q}\t{%rax, $dst|$dst, %rax}", []>;
332
333 // Moves to and from segment registers
334 def MOV64rs : RI<0x8C, MRMDestReg, (outs GR64:$dst), (ins SEGMENT_REG:$src),
335                  "mov{w}\t{$src, $dst|$dst, $src}", []>;
336 def MOV64ms : RI<0x8C, MRMDestMem, (outs i64mem:$dst), (ins SEGMENT_REG:$src),
337                  "mov{w}\t{$src, $dst|$dst, $src}", []>;
338 def MOV64sr : RI<0x8E, MRMSrcReg, (outs SEGMENT_REG:$dst), (ins GR64:$src),
339                  "mov{w}\t{$src, $dst|$dst, $src}", []>;
340 def MOV64sm : RI<0x8E, MRMSrcMem, (outs SEGMENT_REG:$dst), (ins i64mem:$src),
341                  "mov{w}\t{$src, $dst|$dst, $src}", []>;
342
343 // Sign/Zero extenders
344
345 // MOVSX64rr8 always has a REX prefix and it has an 8-bit register
346 // operand, which makes it a rare instruction with an 8-bit register
347 // operand that can never access an h register. If support for h registers
348 // were generalized, this would require a special register class.
349 def MOVSX64rr8 : RI<0xBE, MRMSrcReg, (outs GR64:$dst), (ins GR8 :$src),
350                     "movs{bq|x}\t{$src, $dst|$dst, $src}",
351                     [(set GR64:$dst, (sext GR8:$src))]>, TB;
352 def MOVSX64rm8 : RI<0xBE, MRMSrcMem, (outs GR64:$dst), (ins i8mem :$src),
353                     "movs{bq|x}\t{$src, $dst|$dst, $src}",
354                     [(set GR64:$dst, (sextloadi64i8 addr:$src))]>, TB;
355 def MOVSX64rr16: RI<0xBF, MRMSrcReg, (outs GR64:$dst), (ins GR16:$src),
356                     "movs{wq|x}\t{$src, $dst|$dst, $src}",
357                     [(set GR64:$dst, (sext GR16:$src))]>, TB;
358 def MOVSX64rm16: RI<0xBF, MRMSrcMem, (outs GR64:$dst), (ins i16mem:$src),
359                     "movs{wq|x}\t{$src, $dst|$dst, $src}",
360                     [(set GR64:$dst, (sextloadi64i16 addr:$src))]>, TB;
361 def MOVSX64rr32: RI<0x63, MRMSrcReg, (outs GR64:$dst), (ins GR32:$src),
362                     "movs{lq|xd}\t{$src, $dst|$dst, $src}",
363                     [(set GR64:$dst, (sext GR32:$src))]>;
364 def MOVSX64rm32: RI<0x63, MRMSrcMem, (outs GR64:$dst), (ins i32mem:$src),
365                     "movs{lq|xd}\t{$src, $dst|$dst, $src}",
366                     [(set GR64:$dst, (sextloadi64i32 addr:$src))]>;
367
368 // Use movzbl instead of movzbq when the destination is a register; it's
369 // equivalent due to implicit zero-extending, and it has a smaller encoding.
370 def MOVZX64rr8 : I<0xB6, MRMSrcReg, (outs GR64:$dst), (ins GR8 :$src),
371                    "movz{bl|x}\t{$src, ${dst:subreg32}|${dst:subreg32}, $src}",
372                    [(set GR64:$dst, (zext GR8:$src))]>, TB;
373 def MOVZX64rm8 : I<0xB6, MRMSrcMem, (outs GR64:$dst), (ins i8mem :$src),
374                    "movz{bl|x}\t{$src, ${dst:subreg32}|${dst:subreg32}, $src}",
375                    [(set GR64:$dst, (zextloadi64i8 addr:$src))]>, TB;
376 // Use movzwl instead of movzwq when the destination is a register; it's
377 // equivalent due to implicit zero-extending, and it has a smaller encoding.
378 def MOVZX64rr16: I<0xB7, MRMSrcReg, (outs GR64:$dst), (ins GR16:$src),
379                    "movz{wl|x}\t{$src, ${dst:subreg32}|${dst:subreg32}, $src}",
380                    [(set GR64:$dst, (zext GR16:$src))]>, TB;
381 def MOVZX64rm16: I<0xB7, MRMSrcMem, (outs GR64:$dst), (ins i16mem:$src),
382                    "movz{wl|x}\t{$src, ${dst:subreg32}|${dst:subreg32}, $src}",
383                    [(set GR64:$dst, (zextloadi64i16 addr:$src))]>, TB;
384
385 // There's no movzlq instruction, but movl can be used for this purpose, using
386 // implicit zero-extension. The preferred way to do 32-bit-to-64-bit zero
387 // extension on x86-64 is to use a SUBREG_TO_REG to utilize implicit
388 // zero-extension, however this isn't possible when the 32-bit value is
389 // defined by a truncate or is copied from something where the high bits aren't
390 // necessarily all zero. In such cases, we fall back to these explicit zext
391 // instructions.
392 def MOVZX64rr32 : I<0x89, MRMDestReg, (outs GR64:$dst), (ins GR32:$src),
393                     "mov{l}\t{$src, ${dst:subreg32}|${dst:subreg32}, $src}",
394                     [(set GR64:$dst, (zext GR32:$src))]>;
395 def MOVZX64rm32 : I<0x8B, MRMSrcMem, (outs GR64:$dst), (ins i32mem:$src),
396                     "mov{l}\t{$src, ${dst:subreg32}|${dst:subreg32}, $src}",
397                     [(set GR64:$dst, (zextloadi64i32 addr:$src))]>;
398
399 // Any instruction that defines a 32-bit result leaves the high half of the
400 // register. Truncate can be lowered to EXTRACT_SUBREG. CopyFromReg may
401 // be copying from a truncate. And x86's cmov doesn't do anything if the
402 // condition is false. But any other 32-bit operation will zero-extend
403 // up to 64 bits.
404 def def32 : PatLeaf<(i32 GR32:$src), [{
405   return N->getOpcode() != ISD::TRUNCATE &&
406          N->getOpcode() != TargetInstrInfo::EXTRACT_SUBREG &&
407          N->getOpcode() != ISD::CopyFromReg &&
408          N->getOpcode() != X86ISD::CMOV;
409 }]>;
410
411 // In the case of a 32-bit def that is known to implicitly zero-extend,
412 // we can use a SUBREG_TO_REG.
413 def : Pat<(i64 (zext def32:$src)),
414           (SUBREG_TO_REG (i64 0), GR32:$src, x86_subreg_32bit)>;
415
416 let neverHasSideEffects = 1 in {
417   let Defs = [RAX], Uses = [EAX] in
418   def CDQE : RI<0x98, RawFrm, (outs), (ins),
419                "{cltq|cdqe}", []>;     // RAX = signext(EAX)
420
421   let Defs = [RAX,RDX], Uses = [RAX] in
422   def CQO  : RI<0x99, RawFrm, (outs), (ins),
423                 "{cqto|cqo}", []>; // RDX:RAX = signext(RAX)
424 }
425
426 //===----------------------------------------------------------------------===//
427 //  Arithmetic Instructions...
428 //
429
430 let Defs = [EFLAGS] in {
431
432 def ADD64i32 : RI<0x05, RawFrm, (outs), (ins i32imm:$src),
433                   "add{q}\t{$src, %rax|%rax, $src}", []>;
434
435 let isTwoAddress = 1 in {
436 let isConvertibleToThreeAddress = 1 in {
437 let isCommutable = 1 in
438 // Register-Register Addition
439 def ADD64rr    : RI<0x01, MRMDestReg, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
440                     "add{q}\t{$src2, $dst|$dst, $src2}",
441                     [(set GR64:$dst, (add GR64:$src1, GR64:$src2)),
442                      (implicit EFLAGS)]>;
443
444 // Register-Integer Addition
445 def ADD64ri8  : RIi8<0x83, MRM0r, (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2),
446                      "add{q}\t{$src2, $dst|$dst, $src2}",
447                      [(set GR64:$dst, (add GR64:$src1, i64immSExt8:$src2)),
448                       (implicit EFLAGS)]>;
449 def ADD64ri32 : RIi32<0x81, MRM0r, (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2),
450                       "add{q}\t{$src2, $dst|$dst, $src2}",
451                       [(set GR64:$dst, (add GR64:$src1, i64immSExt32:$src2)),
452                        (implicit EFLAGS)]>;
453 } // isConvertibleToThreeAddress
454
455 // Register-Memory Addition
456 def ADD64rm     : RI<0x03, MRMSrcMem, (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
457                      "add{q}\t{$src2, $dst|$dst, $src2}",
458                      [(set GR64:$dst, (add GR64:$src1, (load addr:$src2))),
459                       (implicit EFLAGS)]>;
460
461 // Register-Register Addition - Equivalent to the normal rr form (ADD64rr), but
462 //   differently encoded.
463 def ADD64mrmrr  : RI<0x03, MRMSrcReg, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
464                      "add{l}\t{$src2, $dst|$dst, $src2}", []>;
465
466 } // isTwoAddress
467
468 // Memory-Register Addition
469 def ADD64mr  : RI<0x01, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2),
470                   "add{q}\t{$src2, $dst|$dst, $src2}",
471                   [(store (add (load addr:$dst), GR64:$src2), addr:$dst),
472                    (implicit EFLAGS)]>;
473 def ADD64mi8 : RIi8<0x83, MRM0m, (outs), (ins i64mem:$dst, i64i8imm :$src2),
474                     "add{q}\t{$src2, $dst|$dst, $src2}",
475                 [(store (add (load addr:$dst), i64immSExt8:$src2), addr:$dst),
476                  (implicit EFLAGS)]>;
477 def ADD64mi32 : RIi32<0x81, MRM0m, (outs), (ins i64mem:$dst, i64i32imm :$src2),
478                       "add{q}\t{$src2, $dst|$dst, $src2}",
479                [(store (add (load addr:$dst), i64immSExt32:$src2), addr:$dst),
480                 (implicit EFLAGS)]>;
481
482 let Uses = [EFLAGS] in {
483
484 def ADC64i32 : RI<0x15, RawFrm, (outs), (ins i32imm:$src),
485                   "adc{q}\t{$src, %rax|%rax, $src}", []>;
486
487 let isTwoAddress = 1 in {
488 let isCommutable = 1 in
489 def ADC64rr  : RI<0x11, MRMDestReg, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
490                   "adc{q}\t{$src2, $dst|$dst, $src2}",
491                   [(set GR64:$dst, (adde GR64:$src1, GR64:$src2))]>;
492
493 def ADC64rm  : RI<0x13, MRMSrcMem , (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
494                   "adc{q}\t{$src2, $dst|$dst, $src2}",
495                   [(set GR64:$dst, (adde GR64:$src1, (load addr:$src2)))]>;
496
497 def ADC64ri8 : RIi8<0x83, MRM2r, (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2),
498                     "adc{q}\t{$src2, $dst|$dst, $src2}",
499                     [(set GR64:$dst, (adde GR64:$src1, i64immSExt8:$src2))]>;
500 def ADC64ri32 : RIi32<0x81, MRM2r, (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2),
501                       "adc{q}\t{$src2, $dst|$dst, $src2}",
502                       [(set GR64:$dst, (adde GR64:$src1, i64immSExt32:$src2))]>;
503 } // isTwoAddress
504
505 def ADC64mr  : RI<0x11, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2),
506                   "adc{q}\t{$src2, $dst|$dst, $src2}",
507                   [(store (adde (load addr:$dst), GR64:$src2), addr:$dst)]>;
508 def ADC64mi8 : RIi8<0x83, MRM2m, (outs), (ins i64mem:$dst, i64i8imm :$src2),
509                     "adc{q}\t{$src2, $dst|$dst, $src2}",
510                  [(store (adde (load addr:$dst), i64immSExt8:$src2), addr:$dst)]>;
511 def ADC64mi32 : RIi32<0x81, MRM2m, (outs), (ins i64mem:$dst, i64i32imm:$src2),
512                       "adc{q}\t{$src2, $dst|$dst, $src2}",
513                  [(store (adde (load addr:$dst), i64immSExt8:$src2), addr:$dst)]>;
514 } // Uses = [EFLAGS]
515
516 let isTwoAddress = 1 in {
517 // Register-Register Subtraction
518 def SUB64rr  : RI<0x29, MRMDestReg, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
519                   "sub{q}\t{$src2, $dst|$dst, $src2}",
520                   [(set GR64:$dst, (sub GR64:$src1, GR64:$src2)),
521                    (implicit EFLAGS)]>;
522
523 // Register-Memory Subtraction
524 def SUB64rm  : RI<0x2B, MRMSrcMem, (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
525                   "sub{q}\t{$src2, $dst|$dst, $src2}",
526                   [(set GR64:$dst, (sub GR64:$src1, (load addr:$src2))),
527                    (implicit EFLAGS)]>;
528
529 // Register-Integer Subtraction
530 def SUB64ri8 : RIi8<0x83, MRM5r, (outs GR64:$dst),
531                                  (ins GR64:$src1, i64i8imm:$src2),
532                     "sub{q}\t{$src2, $dst|$dst, $src2}",
533                     [(set GR64:$dst, (sub GR64:$src1, i64immSExt8:$src2)),
534                      (implicit EFLAGS)]>;
535 def SUB64ri32 : RIi32<0x81, MRM5r, (outs GR64:$dst),
536                                    (ins GR64:$src1, i64i32imm:$src2),
537                       "sub{q}\t{$src2, $dst|$dst, $src2}",
538                       [(set GR64:$dst, (sub GR64:$src1, i64immSExt32:$src2)),
539                        (implicit EFLAGS)]>;
540 } // isTwoAddress
541
542 def SUB64i32 : RI<0x2D, RawFrm, (outs), (ins i32imm:$src),
543                   "sub{q}\t{$src, %rax|%rax, $src}", []>;
544
545 // Memory-Register Subtraction
546 def SUB64mr  : RI<0x29, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2), 
547                   "sub{q}\t{$src2, $dst|$dst, $src2}",
548                   [(store (sub (load addr:$dst), GR64:$src2), addr:$dst),
549                    (implicit EFLAGS)]>;
550
551 // Memory-Integer Subtraction
552 def SUB64mi8 : RIi8<0x83, MRM5m, (outs), (ins i64mem:$dst, i64i8imm :$src2), 
553                     "sub{q}\t{$src2, $dst|$dst, $src2}",
554                     [(store (sub (load addr:$dst), i64immSExt8:$src2),
555                             addr:$dst),
556                      (implicit EFLAGS)]>;
557 def SUB64mi32 : RIi32<0x81, MRM5m, (outs), (ins i64mem:$dst, i64i32imm:$src2),
558                       "sub{q}\t{$src2, $dst|$dst, $src2}",
559                       [(store (sub (load addr:$dst), i64immSExt32:$src2),
560                               addr:$dst),
561                        (implicit EFLAGS)]>;
562
563 let Uses = [EFLAGS] in {
564 let isTwoAddress = 1 in {
565 def SBB64rr    : RI<0x19, MRMDestReg, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
566                     "sbb{q}\t{$src2, $dst|$dst, $src2}",
567                     [(set GR64:$dst, (sube GR64:$src1, GR64:$src2))]>;
568
569 def SBB64rm  : RI<0x1B, MRMSrcMem, (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
570                   "sbb{q}\t{$src2, $dst|$dst, $src2}",
571                   [(set GR64:$dst, (sube GR64:$src1, (load addr:$src2)))]>;
572
573 def SBB64ri8 : RIi8<0x83, MRM3r, (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2),
574                     "sbb{q}\t{$src2, $dst|$dst, $src2}",
575                     [(set GR64:$dst, (sube GR64:$src1, i64immSExt8:$src2))]>;
576 def SBB64ri32 : RIi32<0x81, MRM3r, (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2),
577                       "sbb{q}\t{$src2, $dst|$dst, $src2}",
578                       [(set GR64:$dst, (sube GR64:$src1, i64immSExt32:$src2))]>;
579 } // isTwoAddress
580
581 def SBB64i32 : RI<0x1D, RawFrm, (outs), (ins i32imm:$src),
582                   "sbb{q}\t{$src, %rax|%rax, $src}", []>;
583
584 def SBB64mr  : RI<0x19, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2), 
585                   "sbb{q}\t{$src2, $dst|$dst, $src2}",
586                   [(store (sube (load addr:$dst), GR64:$src2), addr:$dst)]>;
587 def SBB64mi8 : RIi8<0x83, MRM3m, (outs), (ins i64mem:$dst, i64i8imm :$src2), 
588                     "sbb{q}\t{$src2, $dst|$dst, $src2}",
589                [(store (sube (load addr:$dst), i64immSExt8:$src2), addr:$dst)]>;
590 def SBB64mi32 : RIi32<0x81, MRM3m, (outs), (ins i64mem:$dst, i64i32imm:$src2), 
591                       "sbb{q}\t{$src2, $dst|$dst, $src2}",
592               [(store (sube (load addr:$dst), i64immSExt32:$src2), addr:$dst)]>;
593 } // Uses = [EFLAGS]
594 } // Defs = [EFLAGS]
595
596 // Unsigned multiplication
597 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX], neverHasSideEffects = 1 in {
598 def MUL64r : RI<0xF7, MRM4r, (outs), (ins GR64:$src),
599                 "mul{q}\t$src", []>;         // RAX,RDX = RAX*GR64
600 let mayLoad = 1 in
601 def MUL64m : RI<0xF7, MRM4m, (outs), (ins i64mem:$src),
602                 "mul{q}\t$src", []>;         // RAX,RDX = RAX*[mem64]
603
604 // Signed multiplication
605 def IMUL64r : RI<0xF7, MRM5r, (outs), (ins GR64:$src),
606                  "imul{q}\t$src", []>;         // RAX,RDX = RAX*GR64
607 let mayLoad = 1 in
608 def IMUL64m : RI<0xF7, MRM5m, (outs), (ins i64mem:$src),
609                  "imul{q}\t$src", []>;         // RAX,RDX = RAX*[mem64]
610 }
611
612 let Defs = [EFLAGS] in {
613 let isTwoAddress = 1 in {
614 let isCommutable = 1 in
615 // Register-Register Signed Integer Multiplication
616 def IMUL64rr : RI<0xAF, MRMSrcReg, (outs GR64:$dst),
617                                    (ins GR64:$src1, GR64:$src2),
618                   "imul{q}\t{$src2, $dst|$dst, $src2}",
619                   [(set GR64:$dst, (mul GR64:$src1, GR64:$src2)),
620                    (implicit EFLAGS)]>, TB;
621
622 // Register-Memory Signed Integer Multiplication
623 def IMUL64rm : RI<0xAF, MRMSrcMem, (outs GR64:$dst),
624                                    (ins GR64:$src1, i64mem:$src2),
625                   "imul{q}\t{$src2, $dst|$dst, $src2}",
626                   [(set GR64:$dst, (mul GR64:$src1, (load addr:$src2))),
627                    (implicit EFLAGS)]>, TB;
628 } // isTwoAddress
629
630 // Suprisingly enough, these are not two address instructions!
631
632 // Register-Integer Signed Integer Multiplication
633 def IMUL64rri8 : RIi8<0x6B, MRMSrcReg,                      // GR64 = GR64*I8
634                       (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2),
635                       "imul{q}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
636                       [(set GR64:$dst, (mul GR64:$src1, i64immSExt8:$src2)),
637                        (implicit EFLAGS)]>;
638 def IMUL64rri32 : RIi32<0x69, MRMSrcReg,                    // GR64 = GR64*I32
639                         (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2),
640                         "imul{q}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
641                        [(set GR64:$dst, (mul GR64:$src1, i64immSExt32:$src2)),
642                         (implicit EFLAGS)]>;
643
644 // Memory-Integer Signed Integer Multiplication
645 def IMUL64rmi8 : RIi8<0x6B, MRMSrcMem,                      // GR64 = [mem64]*I8
646                       (outs GR64:$dst), (ins i64mem:$src1, i64i8imm: $src2),
647                       "imul{q}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
648                       [(set GR64:$dst, (mul (load addr:$src1),
649                                             i64immSExt8:$src2)),
650                        (implicit EFLAGS)]>;
651 def IMUL64rmi32 : RIi32<0x69, MRMSrcMem,                   // GR64 = [mem64]*I32
652                         (outs GR64:$dst), (ins i64mem:$src1, i64i32imm:$src2),
653                         "imul{q}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
654                         [(set GR64:$dst, (mul (load addr:$src1),
655                                               i64immSExt32:$src2)),
656                          (implicit EFLAGS)]>;
657 } // Defs = [EFLAGS]
658
659 // Unsigned division / remainder
660 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX,RDX] in {
661 def DIV64r : RI<0xF7, MRM6r, (outs), (ins GR64:$src),        // RDX:RAX/r64 = RAX,RDX
662                 "div{q}\t$src", []>;
663 // Signed division / remainder
664 def IDIV64r: RI<0xF7, MRM7r, (outs), (ins GR64:$src),        // RDX:RAX/r64 = RAX,RDX
665                 "idiv{q}\t$src", []>;
666 let mayLoad = 1 in {
667 def DIV64m : RI<0xF7, MRM6m, (outs), (ins i64mem:$src),      // RDX:RAX/[mem64] = RAX,RDX
668                 "div{q}\t$src", []>;
669 def IDIV64m: RI<0xF7, MRM7m, (outs), (ins i64mem:$src),      // RDX:RAX/[mem64] = RAX,RDX
670                 "idiv{q}\t$src", []>;
671 }
672 }
673
674 // Unary instructions
675 let Defs = [EFLAGS], CodeSize = 2 in {
676 let isTwoAddress = 1 in
677 def NEG64r : RI<0xF7, MRM3r, (outs GR64:$dst), (ins GR64:$src), "neg{q}\t$dst",
678                 [(set GR64:$dst, (ineg GR64:$src)),
679                  (implicit EFLAGS)]>;
680 def NEG64m : RI<0xF7, MRM3m, (outs), (ins i64mem:$dst), "neg{q}\t$dst",
681                 [(store (ineg (loadi64 addr:$dst)), addr:$dst),
682                  (implicit EFLAGS)]>;
683
684 let isTwoAddress = 1, isConvertibleToThreeAddress = 1 in
685 def INC64r : RI<0xFF, MRM0r, (outs GR64:$dst), (ins GR64:$src), "inc{q}\t$dst",
686                 [(set GR64:$dst, (add GR64:$src, 1)),
687                  (implicit EFLAGS)]>;
688 def INC64m : RI<0xFF, MRM0m, (outs), (ins i64mem:$dst), "inc{q}\t$dst",
689                 [(store (add (loadi64 addr:$dst), 1), addr:$dst),
690                  (implicit EFLAGS)]>;
691
692 let isTwoAddress = 1, isConvertibleToThreeAddress = 1 in
693 def DEC64r : RI<0xFF, MRM1r, (outs GR64:$dst), (ins GR64:$src), "dec{q}\t$dst",
694                 [(set GR64:$dst, (add GR64:$src, -1)),
695                  (implicit EFLAGS)]>;
696 def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst",
697                 [(store (add (loadi64 addr:$dst), -1), addr:$dst),
698                  (implicit EFLAGS)]>;
699
700 // In 64-bit mode, single byte INC and DEC cannot be encoded.
701 let isTwoAddress = 1, isConvertibleToThreeAddress = 1 in {
702 // Can transform into LEA.
703 def INC64_16r : I<0xFF, MRM0r, (outs GR16:$dst), (ins GR16:$src), "inc{w}\t$dst",
704                   [(set GR16:$dst, (add GR16:$src, 1)),
705                    (implicit EFLAGS)]>,
706                 OpSize, Requires<[In64BitMode]>;
707 def INC64_32r : I<0xFF, MRM0r, (outs GR32:$dst), (ins GR32:$src), "inc{l}\t$dst",
708                   [(set GR32:$dst, (add GR32:$src, 1)),
709                    (implicit EFLAGS)]>,
710                 Requires<[In64BitMode]>;
711 def DEC64_16r : I<0xFF, MRM1r, (outs GR16:$dst), (ins GR16:$src), "dec{w}\t$dst",
712                   [(set GR16:$dst, (add GR16:$src, -1)),
713                    (implicit EFLAGS)]>,
714                 OpSize, Requires<[In64BitMode]>;
715 def DEC64_32r : I<0xFF, MRM1r, (outs GR32:$dst), (ins GR32:$src), "dec{l}\t$dst",
716                   [(set GR32:$dst, (add GR32:$src, -1)),
717                    (implicit EFLAGS)]>,
718                 Requires<[In64BitMode]>;
719 } // isConvertibleToThreeAddress
720
721 // These are duplicates of their 32-bit counterparts. Only needed so X86 knows
722 // how to unfold them.
723 let isTwoAddress = 0, CodeSize = 2 in {
724   def INC64_16m : I<0xFF, MRM0m, (outs), (ins i16mem:$dst), "inc{w}\t$dst",
725                     [(store (add (loadi16 addr:$dst), 1), addr:$dst),
726                      (implicit EFLAGS)]>,
727                   OpSize, Requires<[In64BitMode]>;
728   def INC64_32m : I<0xFF, MRM0m, (outs), (ins i32mem:$dst), "inc{l}\t$dst",
729                     [(store (add (loadi32 addr:$dst), 1), addr:$dst),
730                      (implicit EFLAGS)]>,
731                   Requires<[In64BitMode]>;
732   def DEC64_16m : I<0xFF, MRM1m, (outs), (ins i16mem:$dst), "dec{w}\t$dst",
733                     [(store (add (loadi16 addr:$dst), -1), addr:$dst),
734                      (implicit EFLAGS)]>,
735                   OpSize, Requires<[In64BitMode]>;
736   def DEC64_32m : I<0xFF, MRM1m, (outs), (ins i32mem:$dst), "dec{l}\t$dst",
737                     [(store (add (loadi32 addr:$dst), -1), addr:$dst),
738                      (implicit EFLAGS)]>,
739                   Requires<[In64BitMode]>;
740 }
741 } // Defs = [EFLAGS], CodeSize
742
743
744 let Defs = [EFLAGS] in {
745 // Shift instructions
746 let isTwoAddress = 1 in {
747 let Uses = [CL] in
748 def SHL64rCL : RI<0xD3, MRM4r, (outs GR64:$dst), (ins GR64:$src),
749                   "shl{q}\t{%cl, $dst|$dst, %CL}",
750                   [(set GR64:$dst, (shl GR64:$src, CL))]>;
751 let isConvertibleToThreeAddress = 1 in   // Can transform into LEA.
752 def SHL64ri  : RIi8<0xC1, MRM4r, (outs GR64:$dst), (ins GR64:$src1, i8imm:$src2),
753                     "shl{q}\t{$src2, $dst|$dst, $src2}",
754                     [(set GR64:$dst, (shl GR64:$src1, (i8 imm:$src2)))]>;
755 // NOTE: We don't include patterns for shifts of a register by one, because
756 // 'add reg,reg' is cheaper.
757 def SHL64r1  : RI<0xD1, MRM4r, (outs GR64:$dst), (ins GR64:$src1),
758                  "shr{q}\t$dst", []>;
759 } // isTwoAddress
760
761 let Uses = [CL] in
762 def SHL64mCL : RI<0xD3, MRM4m, (outs), (ins i64mem:$dst),
763                   "shl{q}\t{%cl, $dst|$dst, %CL}",
764                   [(store (shl (loadi64 addr:$dst), CL), addr:$dst)]>;
765 def SHL64mi : RIi8<0xC1, MRM4m, (outs), (ins i64mem:$dst, i8imm:$src),
766                   "shl{q}\t{$src, $dst|$dst, $src}",
767                  [(store (shl (loadi64 addr:$dst), (i8 imm:$src)), addr:$dst)]>;
768 def SHL64m1 : RI<0xD1, MRM4m, (outs), (ins i64mem:$dst),
769                   "shl{q}\t$dst",
770                  [(store (shl (loadi64 addr:$dst), (i8 1)), addr:$dst)]>;
771
772 let isTwoAddress = 1 in {
773 let Uses = [CL] in
774 def SHR64rCL : RI<0xD3, MRM5r, (outs GR64:$dst), (ins GR64:$src),
775                   "shr{q}\t{%cl, $dst|$dst, %CL}",
776                   [(set GR64:$dst, (srl GR64:$src, CL))]>;
777 def SHR64ri : RIi8<0xC1, MRM5r, (outs GR64:$dst), (ins GR64:$src1, i8imm:$src2),
778                   "shr{q}\t{$src2, $dst|$dst, $src2}",
779                   [(set GR64:$dst, (srl GR64:$src1, (i8 imm:$src2)))]>;
780 def SHR64r1  : RI<0xD1, MRM5r, (outs GR64:$dst), (ins GR64:$src1),
781                  "shr{q}\t$dst",
782                  [(set GR64:$dst, (srl GR64:$src1, (i8 1)))]>;
783 } // isTwoAddress
784
785 let Uses = [CL] in
786 def SHR64mCL : RI<0xD3, MRM5m, (outs), (ins i64mem:$dst),
787                   "shr{q}\t{%cl, $dst|$dst, %CL}",
788                   [(store (srl (loadi64 addr:$dst), CL), addr:$dst)]>;
789 def SHR64mi : RIi8<0xC1, MRM5m, (outs), (ins i64mem:$dst, i8imm:$src),
790                   "shr{q}\t{$src, $dst|$dst, $src}",
791                  [(store (srl (loadi64 addr:$dst), (i8 imm:$src)), addr:$dst)]>;
792 def SHR64m1 : RI<0xD1, MRM5m, (outs), (ins i64mem:$dst),
793                   "shr{q}\t$dst",
794                  [(store (srl (loadi64 addr:$dst), (i8 1)), addr:$dst)]>;
795
796 let isTwoAddress = 1 in {
797 let Uses = [CL] in
798 def SAR64rCL : RI<0xD3, MRM7r, (outs GR64:$dst), (ins GR64:$src),
799                  "sar{q}\t{%cl, $dst|$dst, %CL}",
800                  [(set GR64:$dst, (sra GR64:$src, CL))]>;
801 def SAR64ri  : RIi8<0xC1, MRM7r, (outs GR64:$dst), (ins GR64:$src1, i8imm:$src2),
802                    "sar{q}\t{$src2, $dst|$dst, $src2}",
803                    [(set GR64:$dst, (sra GR64:$src1, (i8 imm:$src2)))]>;
804 def SAR64r1  : RI<0xD1, MRM7r, (outs GR64:$dst), (ins GR64:$src1),
805                  "sar{q}\t$dst",
806                  [(set GR64:$dst, (sra GR64:$src1, (i8 1)))]>;
807 } // isTwoAddress
808
809 let Uses = [CL] in
810 def SAR64mCL : RI<0xD3, MRM7m, (outs), (ins i64mem:$dst), 
811                  "sar{q}\t{%cl, $dst|$dst, %CL}",
812                  [(store (sra (loadi64 addr:$dst), CL), addr:$dst)]>;
813 def SAR64mi  : RIi8<0xC1, MRM7m, (outs), (ins i64mem:$dst, i8imm:$src),
814                     "sar{q}\t{$src, $dst|$dst, $src}",
815                  [(store (sra (loadi64 addr:$dst), (i8 imm:$src)), addr:$dst)]>;
816 def SAR64m1 : RI<0xD1, MRM7m, (outs), (ins i64mem:$dst),
817                   "sar{q}\t$dst",
818                  [(store (sra (loadi64 addr:$dst), (i8 1)), addr:$dst)]>;
819
820 // Rotate instructions
821 let isTwoAddress = 1 in {
822 let Uses = [CL] in
823 def ROL64rCL : RI<0xD3, MRM0r, (outs GR64:$dst), (ins GR64:$src),
824                   "rol{q}\t{%cl, $dst|$dst, %CL}",
825                   [(set GR64:$dst, (rotl GR64:$src, CL))]>;
826 def ROL64ri  : RIi8<0xC1, MRM0r, (outs GR64:$dst), (ins GR64:$src1, i8imm:$src2),
827                     "rol{q}\t{$src2, $dst|$dst, $src2}",
828                     [(set GR64:$dst, (rotl GR64:$src1, (i8 imm:$src2)))]>;
829 def ROL64r1  : RI<0xD1, MRM0r, (outs GR64:$dst), (ins GR64:$src1),
830                   "rol{q}\t$dst",
831                   [(set GR64:$dst, (rotl GR64:$src1, (i8 1)))]>;
832 } // isTwoAddress
833
834 let Uses = [CL] in
835 def ROL64mCL :  I<0xD3, MRM0m, (outs), (ins i64mem:$dst),
836                   "rol{q}\t{%cl, $dst|$dst, %CL}",
837                   [(store (rotl (loadi64 addr:$dst), CL), addr:$dst)]>;
838 def ROL64mi  : RIi8<0xC1, MRM0m, (outs), (ins i64mem:$dst, i8imm:$src),
839                     "rol{q}\t{$src, $dst|$dst, $src}",
840                 [(store (rotl (loadi64 addr:$dst), (i8 imm:$src)), addr:$dst)]>;
841 def ROL64m1  : RI<0xD1, MRM0m, (outs), (ins i64mem:$dst),
842                  "rol{q}\t$dst",
843                [(store (rotl (loadi64 addr:$dst), (i8 1)), addr:$dst)]>;
844
845 let isTwoAddress = 1 in {
846 let Uses = [CL] in
847 def ROR64rCL : RI<0xD3, MRM1r, (outs GR64:$dst), (ins GR64:$src),
848                   "ror{q}\t{%cl, $dst|$dst, %CL}",
849                   [(set GR64:$dst, (rotr GR64:$src, CL))]>;
850 def ROR64ri  : RIi8<0xC1, MRM1r, (outs GR64:$dst), (ins GR64:$src1, i8imm:$src2),
851                     "ror{q}\t{$src2, $dst|$dst, $src2}",
852                     [(set GR64:$dst, (rotr GR64:$src1, (i8 imm:$src2)))]>;
853 def ROR64r1  : RI<0xD1, MRM1r, (outs GR64:$dst), (ins GR64:$src1),
854                   "ror{q}\t$dst",
855                   [(set GR64:$dst, (rotr GR64:$src1, (i8 1)))]>;
856 } // isTwoAddress
857
858 let Uses = [CL] in
859 def ROR64mCL : RI<0xD3, MRM1m, (outs), (ins i64mem:$dst), 
860                   "ror{q}\t{%cl, $dst|$dst, %CL}",
861                   [(store (rotr (loadi64 addr:$dst), CL), addr:$dst)]>;
862 def ROR64mi  : RIi8<0xC1, MRM1m, (outs), (ins i64mem:$dst, i8imm:$src),
863                     "ror{q}\t{$src, $dst|$dst, $src}",
864                 [(store (rotr (loadi64 addr:$dst), (i8 imm:$src)), addr:$dst)]>;
865 def ROR64m1  : RI<0xD1, MRM1m, (outs), (ins i64mem:$dst),
866                  "ror{q}\t$dst",
867                [(store (rotr (loadi64 addr:$dst), (i8 1)), addr:$dst)]>;
868
869 // Double shift instructions (generalizations of rotate)
870 let isTwoAddress = 1 in {
871 let Uses = [CL] in {
872 def SHLD64rrCL : RI<0xA5, MRMDestReg, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
873                     "shld{q}\t{%cl, $src2, $dst|$dst, $src2, %CL}",
874                     [(set GR64:$dst, (X86shld GR64:$src1, GR64:$src2, CL))]>, TB;
875 def SHRD64rrCL : RI<0xAD, MRMDestReg, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
876                     "shrd{q}\t{%cl, $src2, $dst|$dst, $src2, %CL}",
877                     [(set GR64:$dst, (X86shrd GR64:$src1, GR64:$src2, CL))]>, TB;
878 }
879
880 let isCommutable = 1 in {  // FIXME: Update X86InstrInfo::commuteInstruction
881 def SHLD64rri8 : RIi8<0xA4, MRMDestReg,
882                       (outs GR64:$dst), (ins GR64:$src1, GR64:$src2, i8imm:$src3),
883                       "shld{q}\t{$src3, $src2, $dst|$dst, $src2, $src3}",
884                       [(set GR64:$dst, (X86shld GR64:$src1, GR64:$src2,
885                                        (i8 imm:$src3)))]>,
886                  TB;
887 def SHRD64rri8 : RIi8<0xAC, MRMDestReg,
888                       (outs GR64:$dst), (ins GR64:$src1, GR64:$src2, i8imm:$src3),
889                       "shrd{q}\t{$src3, $src2, $dst|$dst, $src2, $src3}",
890                       [(set GR64:$dst, (X86shrd GR64:$src1, GR64:$src2,
891                                        (i8 imm:$src3)))]>,
892                  TB;
893 } // isCommutable
894 } // isTwoAddress
895
896 let Uses = [CL] in {
897 def SHLD64mrCL : RI<0xA5, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2),
898                     "shld{q}\t{%cl, $src2, $dst|$dst, $src2, %CL}",
899                     [(store (X86shld (loadi64 addr:$dst), GR64:$src2, CL),
900                       addr:$dst)]>, TB;
901 def SHRD64mrCL : RI<0xAD, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2),
902                     "shrd{q}\t{%cl, $src2, $dst|$dst, $src2, %CL}",
903                     [(store (X86shrd (loadi64 addr:$dst), GR64:$src2, CL),
904                       addr:$dst)]>, TB;
905 }
906 def SHLD64mri8 : RIi8<0xA4, MRMDestMem,
907                       (outs), (ins i64mem:$dst, GR64:$src2, i8imm:$src3),
908                       "shld{q}\t{$src3, $src2, $dst|$dst, $src2, $src3}",
909                       [(store (X86shld (loadi64 addr:$dst), GR64:$src2,
910                                        (i8 imm:$src3)), addr:$dst)]>,
911                  TB;
912 def SHRD64mri8 : RIi8<0xAC, MRMDestMem, 
913                       (outs), (ins i64mem:$dst, GR64:$src2, i8imm:$src3),
914                       "shrd{q}\t{$src3, $src2, $dst|$dst, $src2, $src3}",
915                       [(store (X86shrd (loadi64 addr:$dst), GR64:$src2,
916                                        (i8 imm:$src3)), addr:$dst)]>,
917                  TB;
918 } // Defs = [EFLAGS]
919
920 //===----------------------------------------------------------------------===//
921 //  Logical Instructions...
922 //
923
924 let isTwoAddress = 1 , AddedComplexity = 15 in
925 def NOT64r : RI<0xF7, MRM2r, (outs GR64:$dst), (ins GR64:$src), "not{q}\t$dst",
926                 [(set GR64:$dst, (not GR64:$src))]>;
927 def NOT64m : RI<0xF7, MRM2m, (outs), (ins i64mem:$dst), "not{q}\t$dst",
928                 [(store (not (loadi64 addr:$dst)), addr:$dst)]>;
929
930 let Defs = [EFLAGS] in {
931 def AND64i32 : RI<0x25, RawFrm, (outs), (ins i32imm:$src),
932                   "and{q}\t{$src, %rax|%rax, $src}", []>;
933
934 let isTwoAddress = 1 in {
935 let isCommutable = 1 in
936 def AND64rr  : RI<0x21, MRMDestReg, 
937                   (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
938                   "and{q}\t{$src2, $dst|$dst, $src2}",
939                   [(set GR64:$dst, (and GR64:$src1, GR64:$src2)),
940                    (implicit EFLAGS)]>;
941 def AND64rm  : RI<0x23, MRMSrcMem,
942                   (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
943                   "and{q}\t{$src2, $dst|$dst, $src2}",
944                   [(set GR64:$dst, (and GR64:$src1, (load addr:$src2))),
945                    (implicit EFLAGS)]>;
946 def AND64ri8 : RIi8<0x83, MRM4r, 
947                     (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2),
948                     "and{q}\t{$src2, $dst|$dst, $src2}",
949                     [(set GR64:$dst, (and GR64:$src1, i64immSExt8:$src2)),
950                      (implicit EFLAGS)]>;
951 def AND64ri32  : RIi32<0x81, MRM4r, 
952                        (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2),
953                        "and{q}\t{$src2, $dst|$dst, $src2}",
954                        [(set GR64:$dst, (and GR64:$src1, i64immSExt32:$src2)),
955                         (implicit EFLAGS)]>;
956 } // isTwoAddress
957
958 def AND64mr  : RI<0x21, MRMDestMem,
959                   (outs), (ins i64mem:$dst, GR64:$src),
960                   "and{q}\t{$src, $dst|$dst, $src}",
961                   [(store (and (load addr:$dst), GR64:$src), addr:$dst),
962                    (implicit EFLAGS)]>;
963 def AND64mi8 : RIi8<0x83, MRM4m,
964                     (outs), (ins i64mem:$dst, i64i8imm :$src),
965                     "and{q}\t{$src, $dst|$dst, $src}",
966                  [(store (and (load addr:$dst), i64immSExt8:$src), addr:$dst),
967                   (implicit EFLAGS)]>;
968 def AND64mi32  : RIi32<0x81, MRM4m,
969                        (outs), (ins i64mem:$dst, i64i32imm:$src),
970                        "and{q}\t{$src, $dst|$dst, $src}",
971              [(store (and (loadi64 addr:$dst), i64immSExt32:$src), addr:$dst),
972               (implicit EFLAGS)]>;
973
974 let isTwoAddress = 1 in {
975 let isCommutable = 1 in
976 def OR64rr   : RI<0x09, MRMDestReg, (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
977                   "or{q}\t{$src2, $dst|$dst, $src2}",
978                   [(set GR64:$dst, (or GR64:$src1, GR64:$src2)),
979                    (implicit EFLAGS)]>;
980 def OR64rm   : RI<0x0B, MRMSrcMem , (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
981                   "or{q}\t{$src2, $dst|$dst, $src2}",
982                   [(set GR64:$dst, (or GR64:$src1, (load addr:$src2))),
983                    (implicit EFLAGS)]>;
984 def OR64ri8  : RIi8<0x83, MRM1r, (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2),
985                     "or{q}\t{$src2, $dst|$dst, $src2}",
986                     [(set GR64:$dst, (or GR64:$src1, i64immSExt8:$src2)),
987                      (implicit EFLAGS)]>;
988 def OR64ri32 : RIi32<0x81, MRM1r, (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2),
989                      "or{q}\t{$src2, $dst|$dst, $src2}",
990                      [(set GR64:$dst, (or GR64:$src1, i64immSExt32:$src2)),
991                       (implicit EFLAGS)]>;
992 } // isTwoAddress
993
994 def OR64mr : RI<0x09, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
995                 "or{q}\t{$src, $dst|$dst, $src}",
996                 [(store (or (load addr:$dst), GR64:$src), addr:$dst),
997                  (implicit EFLAGS)]>;
998 def OR64mi8  : RIi8<0x83, MRM1m, (outs), (ins i64mem:$dst, i64i8imm:$src),
999                     "or{q}\t{$src, $dst|$dst, $src}",
1000                   [(store (or (load addr:$dst), i64immSExt8:$src), addr:$dst),
1001                    (implicit EFLAGS)]>;
1002 def OR64mi32 : RIi32<0x81, MRM1m, (outs), (ins i64mem:$dst, i64i32imm:$src),
1003                      "or{q}\t{$src, $dst|$dst, $src}",
1004               [(store (or (loadi64 addr:$dst), i64immSExt32:$src), addr:$dst),
1005                (implicit EFLAGS)]>;
1006
1007 def OR64i32 : RIi32<0x0D, RawFrm, (outs), (ins i32imm:$src),
1008                     "or{q}\t{$src, %rax|%rax, $src}", []>;
1009
1010 let isTwoAddress = 1 in {
1011 let isCommutable = 1 in
1012 def XOR64rr  : RI<0x31, MRMDestReg,  (outs GR64:$dst), (ins GR64:$src1, GR64:$src2), 
1013                   "xor{q}\t{$src2, $dst|$dst, $src2}",
1014                   [(set GR64:$dst, (xor GR64:$src1, GR64:$src2)),
1015                    (implicit EFLAGS)]>;
1016 def XOR64rm  : RI<0x33, MRMSrcMem, (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2), 
1017                   "xor{q}\t{$src2, $dst|$dst, $src2}",
1018                   [(set GR64:$dst, (xor GR64:$src1, (load addr:$src2))),
1019                    (implicit EFLAGS)]>;
1020 def XOR64ri8 : RIi8<0x83, MRM6r,  (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2),
1021                     "xor{q}\t{$src2, $dst|$dst, $src2}",
1022                     [(set GR64:$dst, (xor GR64:$src1, i64immSExt8:$src2)),
1023                      (implicit EFLAGS)]>;
1024 def XOR64ri32 : RIi32<0x81, MRM6r, 
1025                       (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2), 
1026                       "xor{q}\t{$src2, $dst|$dst, $src2}",
1027                       [(set GR64:$dst, (xor GR64:$src1, i64immSExt32:$src2)),
1028                        (implicit EFLAGS)]>;
1029 } // isTwoAddress
1030
1031 def XOR64mr  : RI<0x31, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
1032                   "xor{q}\t{$src, $dst|$dst, $src}",
1033                   [(store (xor (load addr:$dst), GR64:$src), addr:$dst),
1034                    (implicit EFLAGS)]>;
1035 def XOR64mi8 : RIi8<0x83, MRM6m, (outs), (ins i64mem:$dst, i64i8imm :$src),
1036                     "xor{q}\t{$src, $dst|$dst, $src}",
1037                  [(store (xor (load addr:$dst), i64immSExt8:$src), addr:$dst),
1038                   (implicit EFLAGS)]>;
1039 def XOR64mi32 : RIi32<0x81, MRM6m, (outs), (ins i64mem:$dst, i64i32imm:$src),
1040                       "xor{q}\t{$src, $dst|$dst, $src}",
1041              [(store (xor (loadi64 addr:$dst), i64immSExt32:$src), addr:$dst),
1042               (implicit EFLAGS)]>;
1043               
1044 def XOR64i32 : RIi32<0x35, RawFrm, (outs), (ins i32imm:$src),
1045                      "xor{q}\t{$src, %rax|%rax, $src}", []>;
1046
1047 } // Defs = [EFLAGS]
1048
1049 //===----------------------------------------------------------------------===//
1050 //  Comparison Instructions...
1051 //
1052
1053 // Integer comparison
1054 let Defs = [EFLAGS] in {
1055 def TEST64i32 : RI<0xa9, RawFrm, (outs), (ins i32imm:$src),
1056                    "test{q}\t{$src, %rax|%rax, $src}", []>;
1057 let isCommutable = 1 in
1058 def TEST64rr : RI<0x85, MRMDestReg, (outs), (ins GR64:$src1, GR64:$src2),
1059                   "test{q}\t{$src2, $src1|$src1, $src2}",
1060                   [(X86cmp (and GR64:$src1, GR64:$src2), 0),
1061                    (implicit EFLAGS)]>;
1062 def TEST64rm : RI<0x85, MRMSrcMem, (outs), (ins GR64:$src1, i64mem:$src2),
1063                   "test{q}\t{$src2, $src1|$src1, $src2}",
1064                   [(X86cmp (and GR64:$src1, (loadi64 addr:$src2)), 0),
1065                    (implicit EFLAGS)]>;
1066 def TEST64ri32 : RIi32<0xF7, MRM0r, (outs),
1067                                         (ins GR64:$src1, i64i32imm:$src2),
1068                        "test{q}\t{$src2, $src1|$src1, $src2}",
1069                      [(X86cmp (and GR64:$src1, i64immSExt32:$src2), 0),
1070                       (implicit EFLAGS)]>;
1071 def TEST64mi32 : RIi32<0xF7, MRM0m, (outs),
1072                                         (ins i64mem:$src1, i64i32imm:$src2),
1073                        "test{q}\t{$src2, $src1|$src1, $src2}",
1074                 [(X86cmp (and (loadi64 addr:$src1), i64immSExt32:$src2), 0),
1075                  (implicit EFLAGS)]>;
1076
1077
1078 def CMP64i32 : RI<0x3D, RawFrm, (outs), (ins i32imm:$src),
1079                   "cmp{q}\t{$src, %rax|%rax, $src}", []>;
1080 def CMP64rr : RI<0x39, MRMDestReg, (outs), (ins GR64:$src1, GR64:$src2),
1081                  "cmp{q}\t{$src2, $src1|$src1, $src2}",
1082                  [(X86cmp GR64:$src1, GR64:$src2),
1083                   (implicit EFLAGS)]>;
1084 def CMP64mr : RI<0x39, MRMDestMem, (outs), (ins i64mem:$src1, GR64:$src2),
1085                  "cmp{q}\t{$src2, $src1|$src1, $src2}",
1086                  [(X86cmp (loadi64 addr:$src1), GR64:$src2),
1087                    (implicit EFLAGS)]>;
1088 def CMP64rm : RI<0x3B, MRMSrcMem, (outs), (ins GR64:$src1, i64mem:$src2),
1089                  "cmp{q}\t{$src2, $src1|$src1, $src2}",
1090                  [(X86cmp GR64:$src1, (loadi64 addr:$src2)),
1091                   (implicit EFLAGS)]>;
1092 def CMP64ri8 : RIi8<0x83, MRM7r, (outs), (ins GR64:$src1, i64i8imm:$src2),
1093                     "cmp{q}\t{$src2, $src1|$src1, $src2}",
1094                     [(X86cmp GR64:$src1, i64immSExt8:$src2),
1095                      (implicit EFLAGS)]>;
1096 def CMP64ri32 : RIi32<0x81, MRM7r, (outs), (ins GR64:$src1, i64i32imm:$src2),
1097                       "cmp{q}\t{$src2, $src1|$src1, $src2}",
1098                       [(X86cmp GR64:$src1, i64immSExt32:$src2),
1099                        (implicit EFLAGS)]>;
1100 def CMP64mi8 : RIi8<0x83, MRM7m, (outs), (ins i64mem:$src1, i64i8imm:$src2),
1101                     "cmp{q}\t{$src2, $src1|$src1, $src2}",
1102                     [(X86cmp (loadi64 addr:$src1), i64immSExt8:$src2),
1103                      (implicit EFLAGS)]>;
1104 def CMP64mi32 : RIi32<0x81, MRM7m, (outs),
1105                                        (ins i64mem:$src1, i64i32imm:$src2),
1106                       "cmp{q}\t{$src2, $src1|$src1, $src2}",
1107                       [(X86cmp (loadi64 addr:$src1), i64immSExt32:$src2),
1108                        (implicit EFLAGS)]>;
1109 } // Defs = [EFLAGS]
1110
1111 // Bit tests.
1112 // TODO: BTC, BTR, and BTS
1113 let Defs = [EFLAGS] in {
1114 def BT64rr : RI<0xA3, MRMDestReg, (outs), (ins GR64:$src1, GR64:$src2),
1115                "bt{q}\t{$src2, $src1|$src1, $src2}",
1116                [(X86bt GR64:$src1, GR64:$src2),
1117                 (implicit EFLAGS)]>, TB;
1118
1119 // Unlike with the register+register form, the memory+register form of the
1120 // bt instruction does not ignore the high bits of the index. From ISel's
1121 // perspective, this is pretty bizarre. Disable these instructions for now.
1122 //def BT64mr : RI<0xA3, MRMDestMem, (outs), (ins i64mem:$src1, GR64:$src2),
1123 //               "bt{q}\t{$src2, $src1|$src1, $src2}",
1124 //               [(X86bt (loadi64 addr:$src1), GR64:$src2),
1125 //                (implicit EFLAGS)]>, TB;
1126
1127 def BT64ri8 : Ii8<0xBA, MRM4r, (outs), (ins GR64:$src1, i64i8imm:$src2),
1128                 "bt{q}\t{$src2, $src1|$src1, $src2}",
1129                 [(X86bt GR64:$src1, i64immSExt8:$src2),
1130                  (implicit EFLAGS)]>, TB;
1131 // Note that these instructions don't need FastBTMem because that
1132 // only applies when the other operand is in a register. When it's
1133 // an immediate, bt is still fast.
1134 def BT64mi8 : Ii8<0xBA, MRM4m, (outs), (ins i64mem:$src1, i64i8imm:$src2),
1135                 "bt{q}\t{$src2, $src1|$src1, $src2}",
1136                 [(X86bt (loadi64 addr:$src1), i64immSExt8:$src2),
1137                  (implicit EFLAGS)]>, TB;
1138 } // Defs = [EFLAGS]
1139
1140 // Conditional moves
1141 let Uses = [EFLAGS], isTwoAddress = 1 in {
1142 let isCommutable = 1 in {
1143 def CMOVB64rr : RI<0x42, MRMSrcReg,       // if <u, GR64 = GR64
1144                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1145                    "cmovb\t{$src2, $dst|$dst, $src2}",
1146                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1147                                      X86_COND_B, EFLAGS))]>, TB;
1148 def CMOVAE64rr: RI<0x43, MRMSrcReg,       // if >=u, GR64 = GR64
1149                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1150                    "cmovae\t{$src2, $dst|$dst, $src2}",
1151                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1152                                      X86_COND_AE, EFLAGS))]>, TB;
1153 def CMOVE64rr : RI<0x44, MRMSrcReg,       // if ==, GR64 = GR64
1154                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1155                    "cmove\t{$src2, $dst|$dst, $src2}",
1156                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1157                                      X86_COND_E, EFLAGS))]>, TB;
1158 def CMOVNE64rr: RI<0x45, MRMSrcReg,       // if !=, GR64 = GR64
1159                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1160                    "cmovne\t{$src2, $dst|$dst, $src2}",
1161                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1162                                     X86_COND_NE, EFLAGS))]>, TB;
1163 def CMOVBE64rr: RI<0x46, MRMSrcReg,       // if <=u, GR64 = GR64
1164                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1165                    "cmovbe\t{$src2, $dst|$dst, $src2}",
1166                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1167                                     X86_COND_BE, EFLAGS))]>, TB;
1168 def CMOVA64rr : RI<0x47, MRMSrcReg,       // if >u, GR64 = GR64
1169                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1170                    "cmova\t{$src2, $dst|$dst, $src2}",
1171                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1172                                     X86_COND_A, EFLAGS))]>, TB;
1173 def CMOVL64rr : RI<0x4C, MRMSrcReg,       // if <s, GR64 = GR64
1174                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1175                    "cmovl\t{$src2, $dst|$dst, $src2}",
1176                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1177                                     X86_COND_L, EFLAGS))]>, TB;
1178 def CMOVGE64rr: RI<0x4D, MRMSrcReg,       // if >=s, GR64 = GR64
1179                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1180                    "cmovge\t{$src2, $dst|$dst, $src2}",
1181                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1182                                     X86_COND_GE, EFLAGS))]>, TB;
1183 def CMOVLE64rr: RI<0x4E, MRMSrcReg,       // if <=s, GR64 = GR64
1184                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1185                    "cmovle\t{$src2, $dst|$dst, $src2}",
1186                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1187                                     X86_COND_LE, EFLAGS))]>, TB;
1188 def CMOVG64rr : RI<0x4F, MRMSrcReg,       // if >s, GR64 = GR64
1189                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1190                    "cmovg\t{$src2, $dst|$dst, $src2}",
1191                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1192                                     X86_COND_G, EFLAGS))]>, TB;
1193 def CMOVS64rr : RI<0x48, MRMSrcReg,       // if signed, GR64 = GR64
1194                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1195                    "cmovs\t{$src2, $dst|$dst, $src2}",
1196                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1197                                     X86_COND_S, EFLAGS))]>, TB;
1198 def CMOVNS64rr: RI<0x49, MRMSrcReg,       // if !signed, GR64 = GR64
1199                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1200                    "cmovns\t{$src2, $dst|$dst, $src2}",
1201                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1202                                     X86_COND_NS, EFLAGS))]>, TB;
1203 def CMOVP64rr : RI<0x4A, MRMSrcReg,       // if parity, GR64 = GR64
1204                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1205                    "cmovp\t{$src2, $dst|$dst, $src2}",
1206                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1207                                     X86_COND_P, EFLAGS))]>, TB;
1208 def CMOVNP64rr : RI<0x4B, MRMSrcReg,       // if !parity, GR64 = GR64
1209                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1210                    "cmovnp\t{$src2, $dst|$dst, $src2}",
1211                     [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1212                                      X86_COND_NP, EFLAGS))]>, TB;
1213 def CMOVO64rr : RI<0x40, MRMSrcReg,       // if overflow, GR64 = GR64
1214                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1215                    "cmovo\t{$src2, $dst|$dst, $src2}",
1216                    [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1217                                     X86_COND_O, EFLAGS))]>, TB;
1218 def CMOVNO64rr : RI<0x41, MRMSrcReg,       // if !overflow, GR64 = GR64
1219                    (outs GR64:$dst), (ins GR64:$src1, GR64:$src2),
1220                    "cmovno\t{$src2, $dst|$dst, $src2}",
1221                     [(set GR64:$dst, (X86cmov GR64:$src1, GR64:$src2,
1222                                      X86_COND_NO, EFLAGS))]>, TB;
1223 } // isCommutable = 1
1224
1225 def CMOVB64rm : RI<0x42, MRMSrcMem,       // if <u, GR64 = [mem64]
1226                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1227                    "cmovb\t{$src2, $dst|$dst, $src2}",
1228                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1229                                      X86_COND_B, EFLAGS))]>, TB;
1230 def CMOVAE64rm: RI<0x43, MRMSrcMem,       // if >=u, GR64 = [mem64]
1231                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1232                    "cmovae\t{$src2, $dst|$dst, $src2}",
1233                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1234                                      X86_COND_AE, EFLAGS))]>, TB;
1235 def CMOVE64rm : RI<0x44, MRMSrcMem,       // if ==, GR64 = [mem64]
1236                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1237                    "cmove\t{$src2, $dst|$dst, $src2}",
1238                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1239                                      X86_COND_E, EFLAGS))]>, TB;
1240 def CMOVNE64rm: RI<0x45, MRMSrcMem,       // if !=, GR64 = [mem64]
1241                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1242                    "cmovne\t{$src2, $dst|$dst, $src2}",
1243                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1244                                     X86_COND_NE, EFLAGS))]>, TB;
1245 def CMOVBE64rm: RI<0x46, MRMSrcMem,       // if <=u, GR64 = [mem64]
1246                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1247                    "cmovbe\t{$src2, $dst|$dst, $src2}",
1248                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1249                                     X86_COND_BE, EFLAGS))]>, TB;
1250 def CMOVA64rm : RI<0x47, MRMSrcMem,       // if >u, GR64 = [mem64]
1251                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1252                    "cmova\t{$src2, $dst|$dst, $src2}",
1253                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1254                                     X86_COND_A, EFLAGS))]>, TB;
1255 def CMOVL64rm : RI<0x4C, MRMSrcMem,       // if <s, GR64 = [mem64]
1256                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1257                    "cmovl\t{$src2, $dst|$dst, $src2}",
1258                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1259                                     X86_COND_L, EFLAGS))]>, TB;
1260 def CMOVGE64rm: RI<0x4D, MRMSrcMem,       // if >=s, GR64 = [mem64]
1261                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1262                    "cmovge\t{$src2, $dst|$dst, $src2}",
1263                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1264                                     X86_COND_GE, EFLAGS))]>, TB;
1265 def CMOVLE64rm: RI<0x4E, MRMSrcMem,       // if <=s, GR64 = [mem64]
1266                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1267                    "cmovle\t{$src2, $dst|$dst, $src2}",
1268                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1269                                     X86_COND_LE, EFLAGS))]>, TB;
1270 def CMOVG64rm : RI<0x4F, MRMSrcMem,       // if >s, GR64 = [mem64]
1271                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1272                    "cmovg\t{$src2, $dst|$dst, $src2}",
1273                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1274                                     X86_COND_G, EFLAGS))]>, TB;
1275 def CMOVS64rm : RI<0x48, MRMSrcMem,       // if signed, GR64 = [mem64]
1276                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1277                    "cmovs\t{$src2, $dst|$dst, $src2}",
1278                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1279                                     X86_COND_S, EFLAGS))]>, TB;
1280 def CMOVNS64rm: RI<0x49, MRMSrcMem,       // if !signed, GR64 = [mem64]
1281                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1282                    "cmovns\t{$src2, $dst|$dst, $src2}",
1283                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1284                                     X86_COND_NS, EFLAGS))]>, TB;
1285 def CMOVP64rm : RI<0x4A, MRMSrcMem,       // if parity, GR64 = [mem64]
1286                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1287                    "cmovp\t{$src2, $dst|$dst, $src2}",
1288                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1289                                     X86_COND_P, EFLAGS))]>, TB;
1290 def CMOVNP64rm : RI<0x4B, MRMSrcMem,       // if !parity, GR64 = [mem64]
1291                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1292                    "cmovnp\t{$src2, $dst|$dst, $src2}",
1293                     [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1294                                      X86_COND_NP, EFLAGS))]>, TB;
1295 def CMOVO64rm : RI<0x40, MRMSrcMem,       // if overflow, GR64 = [mem64]
1296                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1297                    "cmovo\t{$src2, $dst|$dst, $src2}",
1298                    [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1299                                     X86_COND_O, EFLAGS))]>, TB;
1300 def CMOVNO64rm : RI<0x41, MRMSrcMem,       // if !overflow, GR64 = [mem64]
1301                    (outs GR64:$dst), (ins GR64:$src1, i64mem:$src2),
1302                    "cmovno\t{$src2, $dst|$dst, $src2}",
1303                     [(set GR64:$dst, (X86cmov GR64:$src1, (loadi64 addr:$src2),
1304                                      X86_COND_NO, EFLAGS))]>, TB;
1305 } // isTwoAddress
1306
1307 //===----------------------------------------------------------------------===//
1308 //  Conversion Instructions...
1309 //
1310
1311 // f64 -> signed i64
1312 def Int_CVTSD2SI64rr: RSDI<0x2D, MRMSrcReg, (outs GR64:$dst), (ins VR128:$src),
1313                            "cvtsd2si{q}\t{$src, $dst|$dst, $src}",
1314                            [(set GR64:$dst,
1315                              (int_x86_sse2_cvtsd2si64 VR128:$src))]>;
1316 def Int_CVTSD2SI64rm: RSDI<0x2D, MRMSrcMem, (outs GR64:$dst), (ins f128mem:$src),
1317                            "cvtsd2si{q}\t{$src, $dst|$dst, $src}",
1318                            [(set GR64:$dst, (int_x86_sse2_cvtsd2si64
1319                                              (load addr:$src)))]>;
1320 def CVTTSD2SI64rr: RSDI<0x2C, MRMSrcReg, (outs GR64:$dst), (ins FR64:$src),
1321                         "cvttsd2si{q}\t{$src, $dst|$dst, $src}",
1322                         [(set GR64:$dst, (fp_to_sint FR64:$src))]>;
1323 def CVTTSD2SI64rm: RSDI<0x2C, MRMSrcMem, (outs GR64:$dst), (ins f64mem:$src),
1324                         "cvttsd2si{q}\t{$src, $dst|$dst, $src}",
1325                         [(set GR64:$dst, (fp_to_sint (loadf64 addr:$src)))]>;
1326 def Int_CVTTSD2SI64rr: RSDI<0x2C, MRMSrcReg, (outs GR64:$dst), (ins VR128:$src),
1327                             "cvttsd2si{q}\t{$src, $dst|$dst, $src}",
1328                             [(set GR64:$dst,
1329                               (int_x86_sse2_cvttsd2si64 VR128:$src))]>;
1330 def Int_CVTTSD2SI64rm: RSDI<0x2C, MRMSrcMem, (outs GR64:$dst), (ins f128mem:$src),
1331                             "cvttsd2si{q}\t{$src, $dst|$dst, $src}",
1332                             [(set GR64:$dst,
1333                               (int_x86_sse2_cvttsd2si64
1334                                (load addr:$src)))]>;
1335
1336 // Signed i64 -> f64
1337 def CVTSI2SD64rr: RSDI<0x2A, MRMSrcReg, (outs FR64:$dst), (ins GR64:$src),
1338                        "cvtsi2sd{q}\t{$src, $dst|$dst, $src}",
1339                        [(set FR64:$dst, (sint_to_fp GR64:$src))]>;
1340 def CVTSI2SD64rm: RSDI<0x2A, MRMSrcMem, (outs FR64:$dst), (ins i64mem:$src),
1341                        "cvtsi2sd{q}\t{$src, $dst|$dst, $src}",
1342                        [(set FR64:$dst, (sint_to_fp (loadi64 addr:$src)))]>;
1343
1344 let isTwoAddress = 1 in {
1345 def Int_CVTSI2SD64rr: RSDI<0x2A, MRMSrcReg,
1346                            (outs VR128:$dst), (ins VR128:$src1, GR64:$src2),
1347                            "cvtsi2sd{q}\t{$src2, $dst|$dst, $src2}",
1348                            [(set VR128:$dst,
1349                              (int_x86_sse2_cvtsi642sd VR128:$src1,
1350                               GR64:$src2))]>;
1351 def Int_CVTSI2SD64rm: RSDI<0x2A, MRMSrcMem,
1352                            (outs VR128:$dst), (ins VR128:$src1, i64mem:$src2),
1353                            "cvtsi2sd{q}\t{$src2, $dst|$dst, $src2}",
1354                            [(set VR128:$dst,
1355                              (int_x86_sse2_cvtsi642sd VR128:$src1,
1356                               (loadi64 addr:$src2)))]>;
1357 } // isTwoAddress
1358
1359 // Signed i64 -> f32
1360 def CVTSI2SS64rr: RSSI<0x2A, MRMSrcReg, (outs FR32:$dst), (ins GR64:$src),
1361                        "cvtsi2ss{q}\t{$src, $dst|$dst, $src}",
1362                        [(set FR32:$dst, (sint_to_fp GR64:$src))]>;
1363 def CVTSI2SS64rm: RSSI<0x2A, MRMSrcMem, (outs FR32:$dst), (ins i64mem:$src),
1364                        "cvtsi2ss{q}\t{$src, $dst|$dst, $src}",
1365                        [(set FR32:$dst, (sint_to_fp (loadi64 addr:$src)))]>;
1366
1367 let isTwoAddress = 1 in {
1368   def Int_CVTSI2SS64rr : RSSI<0x2A, MRMSrcReg,
1369                               (outs VR128:$dst), (ins VR128:$src1, GR64:$src2),
1370                               "cvtsi2ss{q}\t{$src2, $dst|$dst, $src2}",
1371                               [(set VR128:$dst,
1372                                 (int_x86_sse_cvtsi642ss VR128:$src1,
1373                                  GR64:$src2))]>;
1374   def Int_CVTSI2SS64rm : RSSI<0x2A, MRMSrcMem,
1375                               (outs VR128:$dst), (ins VR128:$src1, i64mem:$src2),
1376                               "cvtsi2ss{q}\t{$src2, $dst|$dst, $src2}",
1377                               [(set VR128:$dst,
1378                                 (int_x86_sse_cvtsi642ss VR128:$src1,
1379                                  (loadi64 addr:$src2)))]>;
1380 }
1381
1382 // f32 -> signed i64
1383 def Int_CVTSS2SI64rr: RSSI<0x2D, MRMSrcReg, (outs GR64:$dst), (ins VR128:$src),
1384                            "cvtss2si{q}\t{$src, $dst|$dst, $src}",
1385                            [(set GR64:$dst,
1386                              (int_x86_sse_cvtss2si64 VR128:$src))]>;
1387 def Int_CVTSS2SI64rm: RSSI<0x2D, MRMSrcMem, (outs GR64:$dst), (ins f32mem:$src),
1388                            "cvtss2si{q}\t{$src, $dst|$dst, $src}",
1389                            [(set GR64:$dst, (int_x86_sse_cvtss2si64
1390                                              (load addr:$src)))]>;
1391 def CVTTSS2SI64rr: RSSI<0x2C, MRMSrcReg, (outs GR64:$dst), (ins FR32:$src),
1392                         "cvttss2si{q}\t{$src, $dst|$dst, $src}",
1393                         [(set GR64:$dst, (fp_to_sint FR32:$src))]>;
1394 def CVTTSS2SI64rm: RSSI<0x2C, MRMSrcMem, (outs GR64:$dst), (ins f32mem:$src),
1395                         "cvttss2si{q}\t{$src, $dst|$dst, $src}",
1396                         [(set GR64:$dst, (fp_to_sint (loadf32 addr:$src)))]>;
1397 def Int_CVTTSS2SI64rr: RSSI<0x2C, MRMSrcReg, (outs GR64:$dst), (ins VR128:$src),
1398                             "cvttss2si{q}\t{$src, $dst|$dst, $src}",
1399                             [(set GR64:$dst,
1400                               (int_x86_sse_cvttss2si64 VR128:$src))]>;
1401 def Int_CVTTSS2SI64rm: RSSI<0x2C, MRMSrcMem, (outs GR64:$dst), (ins f32mem:$src),
1402                             "cvttss2si{q}\t{$src, $dst|$dst, $src}",
1403                             [(set GR64:$dst,
1404                               (int_x86_sse_cvttss2si64 (load addr:$src)))]>;
1405
1406 //===----------------------------------------------------------------------===//
1407 // Alias Instructions
1408 //===----------------------------------------------------------------------===//
1409
1410 // Alias instructions that map movr0 to xor. Use xorl instead of xorq; it's
1411 // equivalent due to implicit zero-extending, and it sometimes has a smaller
1412 // encoding.
1413 // FIXME: AddedComplexity gives this a higher priority than MOV64ri32. Remove
1414 // when we have a better way to specify isel priority.
1415 let AddedComplexity = 1 in
1416 def : Pat<(i64 0),
1417           (SUBREG_TO_REG (i64 0), (MOV32r0), x86_subreg_32bit)>;
1418
1419
1420 // Materialize i64 constant where top 32-bits are zero.
1421 let AddedComplexity = 1, isReMaterializable = 1, isAsCheapAsAMove = 1 in
1422 def MOV64ri64i32 : Ii32<0xB8, AddRegFrm, (outs GR64:$dst), (ins i64i32imm:$src),
1423                         "mov{l}\t{$src, ${dst:subreg32}|${dst:subreg32}, $src}",
1424                         [(set GR64:$dst, i64immZExt32:$src)]>;
1425
1426 //===----------------------------------------------------------------------===//
1427 // Thread Local Storage Instructions
1428 //===----------------------------------------------------------------------===//
1429
1430 // All calls clobber the non-callee saved registers. RSP is marked as
1431 // a use to prevent stack-pointer assignments that appear immediately
1432 // before calls from potentially appearing dead.
1433 let Defs = [RAX, RCX, RDX, RSI, RDI, R8, R9, R10, R11,
1434             FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0, ST1,
1435             MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
1436             XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
1437             XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS],
1438     Uses = [RSP] in
1439 def TLS_addr64 : I<0, Pseudo, (outs), (ins lea64mem:$sym),
1440                    ".byte\t0x66; "
1441                    "leaq\t$sym(%rip), %rdi; "
1442                    ".word\t0x6666; "
1443                    "rex64; "
1444                    "call\t__tls_get_addr@PLT",
1445                   [(X86tlsaddr tls64addr:$sym)]>,
1446                   Requires<[In64BitMode]>;
1447
1448 let AddedComplexity = 5, isCodeGenOnly = 1 in
1449 def MOV64GSrm : RI<0x8B, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
1450                  "movq\t%gs:$src, $dst",
1451                  [(set GR64:$dst, (gsload addr:$src))]>, SegGS;
1452
1453 let AddedComplexity = 5, isCodeGenOnly = 1 in
1454 def MOV64FSrm : RI<0x8B, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
1455                  "movq\t%fs:$src, $dst",
1456                  [(set GR64:$dst, (fsload addr:$src))]>, SegFS;
1457
1458 //===----------------------------------------------------------------------===//
1459 // Atomic Instructions
1460 //===----------------------------------------------------------------------===//
1461
1462 let Defs = [RAX, EFLAGS], Uses = [RAX] in {
1463 def LCMPXCHG64 : RI<0xB1, MRMDestMem, (outs), (ins i64mem:$ptr, GR64:$swap),
1464                "lock\n\t"
1465                "cmpxchgq\t$swap,$ptr",
1466                [(X86cas addr:$ptr, GR64:$swap, 8)]>, TB, LOCK;
1467 }
1468
1469 let Constraints = "$val = $dst" in {
1470 let Defs = [EFLAGS] in
1471 def LXADD64 : RI<0xC1, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$ptr,GR64:$val),
1472                "lock\n\t"
1473                "xadd\t$val, $ptr",
1474                [(set GR64:$dst, (atomic_load_add_64 addr:$ptr, GR64:$val))]>,
1475                 TB, LOCK;
1476
1477 def XCHG64rm : RI<0x87, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$ptr,GR64:$val),
1478                   "xchg\t$val, $ptr", 
1479                   [(set GR64:$dst, (atomic_swap_64 addr:$ptr, GR64:$val))]>;
1480 }
1481
1482 // Optimized codegen when the non-memory output is not used.
1483 // FIXME: Use normal add / sub instructions and add lock prefix dynamically.
1484 def LOCK_ADD64mr : RI<0x03, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2),
1485                       "lock\n\t"
1486                       "add{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
1487 def LOCK_ADD64mi8 : RIi8<0x83, MRM0m, (outs),
1488                                       (ins i64mem:$dst, i64i8imm :$src2),
1489                     "lock\n\t"
1490                     "add{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
1491 def LOCK_ADD64mi32 : RIi32<0x81, MRM0m, (outs),
1492                                         (ins i64mem:$dst, i64i32imm :$src2),
1493                       "lock\n\t"
1494                       "add{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
1495 def LOCK_SUB64mr : RI<0x29, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2), 
1496                       "lock\n\t"
1497                       "sub{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
1498 def LOCK_SUB64mi8 : RIi8<0x83, MRM5m, (outs),
1499                                       (ins i64mem:$dst, i64i8imm :$src2), 
1500                       "lock\n\t"
1501                       "sub{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
1502 def LOCK_SUB64mi32 : RIi32<0x81, MRM5m, (outs),
1503                                         (ins i64mem:$dst, i64i32imm:$src2),
1504                       "lock\n\t"
1505                       "sub{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
1506 def LOCK_INC64m : RI<0xFF, MRM0m, (outs), (ins i64mem:$dst),
1507                      "lock\n\t"
1508                      "inc{q}\t$dst", []>, LOCK;
1509 def LOCK_DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst),
1510                       "lock\n\t"
1511                       "dec{q}\t$dst", []>, LOCK;
1512
1513 // Atomic exchange, and, or, xor
1514 let Constraints = "$val = $dst", Defs = [EFLAGS],
1515                   usesCustomDAGSchedInserter = 1 in {
1516 def ATOMAND64 : I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
1517                "#ATOMAND64 PSEUDO!", 
1518                [(set GR64:$dst, (atomic_load_and_64 addr:$ptr, GR64:$val))]>;
1519 def ATOMOR64 : I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
1520                "#ATOMOR64 PSEUDO!", 
1521                [(set GR64:$dst, (atomic_load_or_64 addr:$ptr, GR64:$val))]>;
1522 def ATOMXOR64 : I<0, Pseudo,(outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
1523                "#ATOMXOR64 PSEUDO!", 
1524                [(set GR64:$dst, (atomic_load_xor_64 addr:$ptr, GR64:$val))]>;
1525 def ATOMNAND64 : I<0, Pseudo,(outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
1526                "#ATOMNAND64 PSEUDO!", 
1527                [(set GR64:$dst, (atomic_load_nand_64 addr:$ptr, GR64:$val))]>;
1528 def ATOMMIN64: I<0, Pseudo, (outs GR64:$dst), (ins i64mem:$ptr, GR64:$val),
1529                "#ATOMMIN64 PSEUDO!", 
1530                [(set GR64:$dst, (atomic_load_min_64 addr:$ptr, GR64:$val))]>;
1531 def ATOMMAX64: I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
1532                "#ATOMMAX64 PSEUDO!", 
1533                [(set GR64:$dst, (atomic_load_max_64 addr:$ptr, GR64:$val))]>;
1534 def ATOMUMIN64: I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
1535                "#ATOMUMIN64 PSEUDO!", 
1536                [(set GR64:$dst, (atomic_load_umin_64 addr:$ptr, GR64:$val))]>;
1537 def ATOMUMAX64: I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
1538                "#ATOMUMAX64 PSEUDO!", 
1539                [(set GR64:$dst, (atomic_load_umax_64 addr:$ptr, GR64:$val))]>;
1540 }
1541
1542 //===----------------------------------------------------------------------===//
1543 // Non-Instruction Patterns
1544 //===----------------------------------------------------------------------===//
1545
1546 // ConstantPool GlobalAddress, ExternalSymbol, and JumpTable when not in small
1547 // code model mode, should use 'movabs'.  FIXME: This is really a hack, the
1548 //  'movabs' predicate should handle this sort of thing.
1549 def : Pat<(i64 (X86Wrapper tconstpool  :$dst)),
1550           (MOV64ri tconstpool  :$dst)>, Requires<[FarData]>;
1551 def : Pat<(i64 (X86Wrapper tjumptable  :$dst)),
1552           (MOV64ri tjumptable  :$dst)>, Requires<[FarData]>;
1553 def : Pat<(i64 (X86Wrapper tglobaladdr :$dst)),
1554           (MOV64ri tglobaladdr :$dst)>, Requires<[FarData]>;
1555 def : Pat<(i64 (X86Wrapper texternalsym:$dst)),
1556           (MOV64ri texternalsym:$dst)>, Requires<[FarData]>;
1557
1558 // In static codegen with small code model, we can get the address of a label
1559 // into a register with 'movl'.  FIXME: This is a hack, the 'imm' predicate of
1560 // the MOV64ri64i32 should accept these.
1561 def : Pat<(i64 (X86Wrapper tconstpool  :$dst)),
1562           (MOV64ri64i32 tconstpool  :$dst)>, Requires<[SmallCode]>;
1563 def : Pat<(i64 (X86Wrapper tjumptable  :$dst)),
1564           (MOV64ri64i32 tjumptable  :$dst)>, Requires<[SmallCode]>;
1565 def : Pat<(i64 (X86Wrapper tglobaladdr :$dst)),
1566           (MOV64ri64i32 tglobaladdr :$dst)>, Requires<[SmallCode]>;
1567 def : Pat<(i64 (X86Wrapper texternalsym:$dst)),
1568           (MOV64ri64i32 texternalsym:$dst)>, Requires<[SmallCode]>;
1569
1570 // In kernel code model, we can get the address of a label
1571 // into a register with 'movq'.  FIXME: This is a hack, the 'imm' predicate of
1572 // the MOV64ri32 should accept these.
1573 def : Pat<(i64 (X86Wrapper tconstpool  :$dst)),
1574           (MOV64ri32 tconstpool  :$dst)>, Requires<[KernelCode]>;
1575 def : Pat<(i64 (X86Wrapper tjumptable  :$dst)),
1576           (MOV64ri32 tjumptable  :$dst)>, Requires<[KernelCode]>;
1577 def : Pat<(i64 (X86Wrapper tglobaladdr :$dst)),
1578           (MOV64ri32 tglobaladdr :$dst)>, Requires<[KernelCode]>;
1579 def : Pat<(i64 (X86Wrapper texternalsym:$dst)),
1580           (MOV64ri32 texternalsym:$dst)>, Requires<[KernelCode]>;
1581
1582 // If we have small model and -static mode, it is safe to store global addresses
1583 // directly as immediates.  FIXME: This is really a hack, the 'imm' predicate
1584 // for MOV64mi32 should handle this sort of thing.
1585 def : Pat<(store (i64 (X86Wrapper tconstpool:$src)), addr:$dst),
1586           (MOV64mi32 addr:$dst, tconstpool:$src)>,
1587           Requires<[NearData, IsStatic]>;
1588 def : Pat<(store (i64 (X86Wrapper tjumptable:$src)), addr:$dst),
1589           (MOV64mi32 addr:$dst, tjumptable:$src)>,
1590           Requires<[NearData, IsStatic]>;
1591 def : Pat<(store (i64 (X86Wrapper tglobaladdr:$src)), addr:$dst),
1592           (MOV64mi32 addr:$dst, tglobaladdr:$src)>,
1593           Requires<[NearData, IsStatic]>;
1594 def : Pat<(store (i64 (X86Wrapper texternalsym:$src)), addr:$dst),
1595           (MOV64mi32 addr:$dst, texternalsym:$src)>,
1596           Requires<[NearData, IsStatic]>;
1597
1598 // Calls
1599 // Direct PC relative function call for small code model. 32-bit displacement
1600 // sign extended to 64-bit.
1601 def : Pat<(X86call (i64 tglobaladdr:$dst)),
1602           (CALL64pcrel32 tglobaladdr:$dst)>, Requires<[NotWin64]>;
1603 def : Pat<(X86call (i64 texternalsym:$dst)),
1604           (CALL64pcrel32 texternalsym:$dst)>, Requires<[NotWin64]>;
1605
1606 def : Pat<(X86call (i64 tglobaladdr:$dst)),
1607           (WINCALL64pcrel32 tglobaladdr:$dst)>, Requires<[IsWin64]>;
1608 def : Pat<(X86call (i64 texternalsym:$dst)),
1609           (WINCALL64pcrel32 texternalsym:$dst)>, Requires<[IsWin64]>;
1610
1611 // tailcall stuff
1612 def : Pat<(X86tcret GR64:$dst, imm:$off),
1613           (TCRETURNri64 GR64:$dst, imm:$off)>;
1614
1615 def : Pat<(X86tcret (i64 tglobaladdr:$dst), imm:$off),
1616           (TCRETURNdi64 texternalsym:$dst, imm:$off)>;
1617
1618 def : Pat<(X86tcret (i64 texternalsym:$dst), imm:$off),
1619           (TCRETURNdi64 texternalsym:$dst, imm:$off)>;
1620
1621 // Comparisons.
1622
1623 // TEST R,R is smaller than CMP R,0
1624 def : Pat<(parallel (X86cmp GR64:$src1, 0), (implicit EFLAGS)),
1625           (TEST64rr GR64:$src1, GR64:$src1)>;
1626
1627 // Conditional moves with folded loads with operands swapped and conditions
1628 // inverted.
1629 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_B, EFLAGS),
1630           (CMOVAE64rm GR64:$src2, addr:$src1)>;
1631 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_AE, EFLAGS),
1632           (CMOVB64rm GR64:$src2, addr:$src1)>;
1633 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_E, EFLAGS),
1634           (CMOVNE64rm GR64:$src2, addr:$src1)>;
1635 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_NE, EFLAGS),
1636           (CMOVE64rm GR64:$src2, addr:$src1)>;
1637 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_BE, EFLAGS),
1638           (CMOVA64rm GR64:$src2, addr:$src1)>;
1639 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_A, EFLAGS),
1640           (CMOVBE64rm GR64:$src2, addr:$src1)>;
1641 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_L, EFLAGS),
1642           (CMOVGE64rm GR64:$src2, addr:$src1)>;
1643 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_GE, EFLAGS),
1644           (CMOVL64rm GR64:$src2, addr:$src1)>;
1645 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_LE, EFLAGS),
1646           (CMOVG64rm GR64:$src2, addr:$src1)>;
1647 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_G, EFLAGS),
1648           (CMOVLE64rm GR64:$src2, addr:$src1)>;
1649 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_P, EFLAGS),
1650           (CMOVNP64rm GR64:$src2, addr:$src1)>;
1651 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_NP, EFLAGS),
1652           (CMOVP64rm GR64:$src2, addr:$src1)>;
1653 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_S, EFLAGS),
1654           (CMOVNS64rm GR64:$src2, addr:$src1)>;
1655 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_NS, EFLAGS),
1656           (CMOVS64rm GR64:$src2, addr:$src1)>;
1657 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_O, EFLAGS),
1658           (CMOVNO64rm GR64:$src2, addr:$src1)>;
1659 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_NO, EFLAGS),
1660           (CMOVO64rm GR64:$src2, addr:$src1)>;
1661
1662 // zextload bool -> zextload byte
1663 def : Pat<(zextloadi64i1 addr:$src), (MOVZX64rm8 addr:$src)>;
1664
1665 // extload
1666 // When extloading from 16-bit and smaller memory locations into 64-bit registers,
1667 // use zero-extending loads so that the entire 64-bit register is defined, avoiding
1668 // partial-register updates.
1669 def : Pat<(extloadi64i1 addr:$src),  (MOVZX64rm8  addr:$src)>;
1670 def : Pat<(extloadi64i8 addr:$src),  (MOVZX64rm8  addr:$src)>;
1671 def : Pat<(extloadi64i16 addr:$src), (MOVZX64rm16 addr:$src)>;
1672 // For other extloads, use subregs, since the high contents of the register are
1673 // defined after an extload.
1674 def : Pat<(extloadi64i32 addr:$src),
1675           (SUBREG_TO_REG (i64 0), (MOV32rm addr:$src),
1676                          x86_subreg_32bit)>;
1677
1678 // anyext. Define these to do an explicit zero-extend to
1679 // avoid partial-register updates.
1680 def : Pat<(i64 (anyext GR8 :$src)), (MOVZX64rr8  GR8  :$src)>;
1681 def : Pat<(i64 (anyext GR16:$src)), (MOVZX64rr16 GR16 :$src)>;
1682 def : Pat<(i64 (anyext GR32:$src)),
1683           (SUBREG_TO_REG (i64 0), GR32:$src, x86_subreg_32bit)>;
1684
1685 //===----------------------------------------------------------------------===//
1686 // Some peepholes
1687 //===----------------------------------------------------------------------===//
1688
1689 // Odd encoding trick: -128 fits into an 8-bit immediate field while
1690 // +128 doesn't, so in this special case use a sub instead of an add.
1691 def : Pat<(add GR64:$src1, 128),
1692           (SUB64ri8 GR64:$src1, -128)>;
1693 def : Pat<(store (add (loadi64 addr:$dst), 128), addr:$dst),
1694           (SUB64mi8 addr:$dst, -128)>;
1695
1696 // The same trick applies for 32-bit immediate fields in 64-bit
1697 // instructions.
1698 def : Pat<(add GR64:$src1, 0x0000000080000000),
1699           (SUB64ri32 GR64:$src1, 0xffffffff80000000)>;
1700 def : Pat<(store (add (loadi64 addr:$dst), 0x00000000800000000), addr:$dst),
1701           (SUB64mi32 addr:$dst, 0xffffffff80000000)>;
1702
1703 // r & (2^32-1) ==> movz
1704 def : Pat<(and GR64:$src, 0x00000000FFFFFFFF),
1705           (MOVZX64rr32 (EXTRACT_SUBREG GR64:$src, x86_subreg_32bit))>;
1706 // r & (2^16-1) ==> movz
1707 def : Pat<(and GR64:$src, 0xffff),
1708           (MOVZX64rr16 (i16 (EXTRACT_SUBREG GR64:$src, x86_subreg_16bit)))>;
1709 // r & (2^8-1) ==> movz
1710 def : Pat<(and GR64:$src, 0xff),
1711           (MOVZX64rr8 (i8 (EXTRACT_SUBREG GR64:$src, x86_subreg_8bit)))>;
1712 // r & (2^8-1) ==> movz
1713 def : Pat<(and GR32:$src1, 0xff),
1714            (MOVZX32rr8 (EXTRACT_SUBREG GR32:$src1, x86_subreg_8bit))>,
1715       Requires<[In64BitMode]>;
1716 // r & (2^8-1) ==> movz
1717 def : Pat<(and GR16:$src1, 0xff),
1718            (MOVZX16rr8 (i8 (EXTRACT_SUBREG GR16:$src1, x86_subreg_8bit)))>,
1719       Requires<[In64BitMode]>;
1720
1721 // sext_inreg patterns
1722 def : Pat<(sext_inreg GR64:$src, i32),
1723           (MOVSX64rr32 (EXTRACT_SUBREG GR64:$src, x86_subreg_32bit))>;
1724 def : Pat<(sext_inreg GR64:$src, i16),
1725           (MOVSX64rr16 (EXTRACT_SUBREG GR64:$src, x86_subreg_16bit))>;
1726 def : Pat<(sext_inreg GR64:$src, i8),
1727           (MOVSX64rr8 (EXTRACT_SUBREG GR64:$src, x86_subreg_8bit))>;
1728 def : Pat<(sext_inreg GR32:$src, i8),
1729           (MOVSX32rr8 (EXTRACT_SUBREG GR32:$src, x86_subreg_8bit))>,
1730       Requires<[In64BitMode]>;
1731 def : Pat<(sext_inreg GR16:$src, i8),
1732           (MOVSX16rr8 (i8 (EXTRACT_SUBREG GR16:$src, x86_subreg_8bit)))>,
1733       Requires<[In64BitMode]>;
1734
1735 // trunc patterns
1736 def : Pat<(i32 (trunc GR64:$src)),
1737           (EXTRACT_SUBREG GR64:$src, x86_subreg_32bit)>;
1738 def : Pat<(i16 (trunc GR64:$src)),
1739           (EXTRACT_SUBREG GR64:$src, x86_subreg_16bit)>;
1740 def : Pat<(i8 (trunc GR64:$src)),
1741           (EXTRACT_SUBREG GR64:$src, x86_subreg_8bit)>;
1742 def : Pat<(i8 (trunc GR32:$src)),
1743           (EXTRACT_SUBREG GR32:$src, x86_subreg_8bit)>,
1744       Requires<[In64BitMode]>;
1745 def : Pat<(i8 (trunc GR16:$src)),
1746           (EXTRACT_SUBREG GR16:$src, x86_subreg_8bit)>,
1747       Requires<[In64BitMode]>;
1748
1749 // h-register tricks.
1750 // For now, be conservative on x86-64 and use an h-register extract only if the
1751 // value is immediately zero-extended or stored, which are somewhat common
1752 // cases. This uses a bunch of code to prevent a register requiring a REX prefix
1753 // from being allocated in the same instruction as the h register, as there's
1754 // currently no way to describe this requirement to the register allocator.
1755
1756 // h-register extract and zero-extend.
1757 def : Pat<(and (srl_su GR64:$src, (i8 8)), (i64 255)),
1758           (SUBREG_TO_REG
1759             (i64 0),
1760             (MOVZX32_NOREXrr8
1761               (EXTRACT_SUBREG (COPY_TO_REGCLASS GR64:$src, GR64_ABCD),
1762                               x86_subreg_8bit_hi)),
1763             x86_subreg_32bit)>;
1764 def : Pat<(and (srl_su GR32:$src, (i8 8)), (i32 255)),
1765           (MOVZX32_NOREXrr8
1766             (EXTRACT_SUBREG (COPY_TO_REGCLASS GR32:$src, GR32_ABCD),
1767                             x86_subreg_8bit_hi))>,
1768       Requires<[In64BitMode]>;
1769 def : Pat<(srl_su GR16:$src, (i8 8)),
1770           (EXTRACT_SUBREG
1771             (MOVZX32_NOREXrr8
1772               (EXTRACT_SUBREG (COPY_TO_REGCLASS GR16:$src, GR16_ABCD),
1773                               x86_subreg_8bit_hi)),
1774             x86_subreg_16bit)>,
1775       Requires<[In64BitMode]>;
1776 def : Pat<(i32 (zext (srl_su GR16:$src, (i8 8)))),
1777           (MOVZX32_NOREXrr8
1778             (EXTRACT_SUBREG (COPY_TO_REGCLASS GR16:$src, GR16_ABCD),
1779                             x86_subreg_8bit_hi))>,
1780       Requires<[In64BitMode]>;
1781 def : Pat<(i32 (anyext (srl_su GR16:$src, (i8 8)))),
1782           (MOVZX32_NOREXrr8
1783             (EXTRACT_SUBREG (COPY_TO_REGCLASS GR16:$src, GR16_ABCD),
1784                             x86_subreg_8bit_hi))>,
1785       Requires<[In64BitMode]>;
1786 def : Pat<(i64 (zext (srl_su GR16:$src, (i8 8)))),
1787           (SUBREG_TO_REG
1788             (i64 0),
1789             (MOVZX32_NOREXrr8
1790               (EXTRACT_SUBREG (COPY_TO_REGCLASS GR16:$src, GR16_ABCD),
1791                               x86_subreg_8bit_hi)),
1792             x86_subreg_32bit)>;
1793 def : Pat<(i64 (anyext (srl_su GR16:$src, (i8 8)))),
1794           (SUBREG_TO_REG
1795             (i64 0),
1796             (MOVZX32_NOREXrr8
1797               (EXTRACT_SUBREG (COPY_TO_REGCLASS GR16:$src, GR16_ABCD),
1798                               x86_subreg_8bit_hi)),
1799             x86_subreg_32bit)>;
1800
1801 // h-register extract and store.
1802 def : Pat<(store (i8 (trunc_su (srl_su GR64:$src, (i8 8)))), addr:$dst),
1803           (MOV8mr_NOREX
1804             addr:$dst,
1805             (EXTRACT_SUBREG (COPY_TO_REGCLASS GR64:$src, GR64_ABCD),
1806                             x86_subreg_8bit_hi))>;
1807 def : Pat<(store (i8 (trunc_su (srl_su GR32:$src, (i8 8)))), addr:$dst),
1808           (MOV8mr_NOREX
1809             addr:$dst,
1810             (EXTRACT_SUBREG (COPY_TO_REGCLASS GR32:$src, GR32_ABCD),
1811                             x86_subreg_8bit_hi))>,
1812       Requires<[In64BitMode]>;
1813 def : Pat<(store (i8 (trunc_su (srl_su GR16:$src, (i8 8)))), addr:$dst),
1814           (MOV8mr_NOREX
1815             addr:$dst,
1816             (EXTRACT_SUBREG (COPY_TO_REGCLASS GR16:$src, GR16_ABCD),
1817                             x86_subreg_8bit_hi))>,
1818       Requires<[In64BitMode]>;
1819
1820 // (shl x, 1) ==> (add x, x)
1821 def : Pat<(shl GR64:$src1, (i8 1)), (ADD64rr GR64:$src1, GR64:$src1)>;
1822
1823 // (shl x (and y, 63)) ==> (shl x, y)
1824 def : Pat<(shl GR64:$src1, (and CL:$amt, 63)),
1825           (SHL64rCL GR64:$src1)>;
1826 def : Pat<(store (shl (loadi64 addr:$dst), (and CL:$amt, 63)), addr:$dst),
1827           (SHL64mCL addr:$dst)>;
1828
1829 def : Pat<(srl GR64:$src1, (and CL:$amt, 63)),
1830           (SHR64rCL GR64:$src1)>;
1831 def : Pat<(store (srl (loadi64 addr:$dst), (and CL:$amt, 63)), addr:$dst),
1832           (SHR64mCL addr:$dst)>;
1833
1834 def : Pat<(sra GR64:$src1, (and CL:$amt, 63)),
1835           (SAR64rCL GR64:$src1)>;
1836 def : Pat<(store (sra (loadi64 addr:$dst), (and CL:$amt, 63)), addr:$dst),
1837           (SAR64mCL addr:$dst)>;
1838
1839 // (or (x >> c) | (y << (64 - c))) ==> (shrd64 x, y, c)
1840 def : Pat<(or (srl GR64:$src1, CL:$amt),
1841               (shl GR64:$src2, (sub 64, CL:$amt))),
1842           (SHRD64rrCL GR64:$src1, GR64:$src2)>;
1843
1844 def : Pat<(store (or (srl (loadi64 addr:$dst), CL:$amt),
1845                      (shl GR64:$src2, (sub 64, CL:$amt))), addr:$dst),
1846           (SHRD64mrCL addr:$dst, GR64:$src2)>;
1847
1848 def : Pat<(or (srl GR64:$src1, (i8 (trunc RCX:$amt))),
1849               (shl GR64:$src2, (i8 (trunc (sub 64, RCX:$amt))))),
1850           (SHRD64rrCL GR64:$src1, GR64:$src2)>;
1851
1852 def : Pat<(store (or (srl (loadi64 addr:$dst), (i8 (trunc RCX:$amt))),
1853                      (shl GR64:$src2, (i8 (trunc (sub 64, RCX:$amt))))),
1854                  addr:$dst),
1855           (SHRD64mrCL addr:$dst, GR64:$src2)>;
1856
1857 def : Pat<(shrd GR64:$src1, (i8 imm:$amt1), GR64:$src2, (i8 imm:$amt2)),
1858           (SHRD64rri8 GR64:$src1, GR64:$src2, (i8 imm:$amt1))>;
1859
1860 def : Pat<(store (shrd (loadi64 addr:$dst), (i8 imm:$amt1),
1861                        GR64:$src2, (i8 imm:$amt2)), addr:$dst),
1862           (SHRD64mri8 addr:$dst, GR64:$src2, (i8 imm:$amt1))>;
1863
1864 // (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
1865 def : Pat<(or (shl GR64:$src1, CL:$amt),
1866               (srl GR64:$src2, (sub 64, CL:$amt))),
1867           (SHLD64rrCL GR64:$src1, GR64:$src2)>;
1868
1869 def : Pat<(store (or (shl (loadi64 addr:$dst), CL:$amt),
1870                      (srl GR64:$src2, (sub 64, CL:$amt))), addr:$dst),
1871           (SHLD64mrCL addr:$dst, GR64:$src2)>;
1872
1873 def : Pat<(or (shl GR64:$src1, (i8 (trunc RCX:$amt))),
1874               (srl GR64:$src2, (i8 (trunc (sub 64, RCX:$amt))))),
1875           (SHLD64rrCL GR64:$src1, GR64:$src2)>;
1876
1877 def : Pat<(store (or (shl (loadi64 addr:$dst), (i8 (trunc RCX:$amt))),
1878                      (srl GR64:$src2, (i8 (trunc (sub 64, RCX:$amt))))),
1879                  addr:$dst),
1880           (SHLD64mrCL addr:$dst, GR64:$src2)>;
1881
1882 def : Pat<(shld GR64:$src1, (i8 imm:$amt1), GR64:$src2, (i8 imm:$amt2)),
1883           (SHLD64rri8 GR64:$src1, GR64:$src2, (i8 imm:$amt1))>;
1884
1885 def : Pat<(store (shld (loadi64 addr:$dst), (i8 imm:$amt1),
1886                        GR64:$src2, (i8 imm:$amt2)), addr:$dst),
1887           (SHLD64mri8 addr:$dst, GR64:$src2, (i8 imm:$amt1))>;
1888
1889 // X86 specific add which produces a flag.
1890 def : Pat<(addc GR64:$src1, GR64:$src2),
1891           (ADD64rr GR64:$src1, GR64:$src2)>;
1892 def : Pat<(addc GR64:$src1, (load addr:$src2)),
1893           (ADD64rm GR64:$src1, addr:$src2)>;
1894 def : Pat<(addc GR64:$src1, i64immSExt8:$src2),
1895           (ADD64ri8 GR64:$src1, i64immSExt8:$src2)>;
1896 def : Pat<(addc GR64:$src1, i64immSExt32:$src2),
1897           (ADD64ri32 GR64:$src1, imm:$src2)>;
1898
1899 def : Pat<(subc GR64:$src1, GR64:$src2),
1900           (SUB64rr GR64:$src1, GR64:$src2)>;
1901 def : Pat<(subc GR64:$src1, (load addr:$src2)),
1902           (SUB64rm GR64:$src1, addr:$src2)>;
1903 def : Pat<(subc GR64:$src1, i64immSExt8:$src2),
1904           (SUB64ri8 GR64:$src1, i64immSExt8:$src2)>;
1905 def : Pat<(subc GR64:$src1, imm:$src2),
1906           (SUB64ri32 GR64:$src1, i64immSExt32:$src2)>;
1907
1908 //===----------------------------------------------------------------------===//
1909 // EFLAGS-defining Patterns
1910 //===----------------------------------------------------------------------===//
1911
1912 // Register-Register Addition with EFLAGS result
1913 def : Pat<(parallel (X86add_flag GR64:$src1, GR64:$src2),
1914                     (implicit EFLAGS)),
1915           (ADD64rr GR64:$src1, GR64:$src2)>;
1916
1917 // Register-Integer Addition with EFLAGS result
1918 def : Pat<(parallel (X86add_flag GR64:$src1, i64immSExt8:$src2),
1919                     (implicit EFLAGS)),
1920           (ADD64ri8 GR64:$src1, i64immSExt8:$src2)>;
1921 def : Pat<(parallel (X86add_flag GR64:$src1, i64immSExt32:$src2),
1922                     (implicit EFLAGS)),
1923           (ADD64ri32 GR64:$src1, i64immSExt32:$src2)>;
1924
1925 // Register-Memory Addition with EFLAGS result
1926 def : Pat<(parallel (X86add_flag GR64:$src1, (loadi64 addr:$src2)),
1927                     (implicit EFLAGS)),
1928           (ADD64rm GR64:$src1, addr:$src2)>;
1929
1930 // Memory-Register Addition with EFLAGS result
1931 def : Pat<(parallel (store (X86add_flag (loadi64 addr:$dst), GR64:$src2),
1932                            addr:$dst),
1933                     (implicit EFLAGS)),
1934           (ADD64mr addr:$dst, GR64:$src2)>;
1935 def : Pat<(parallel (store (X86add_flag (loadi64 addr:$dst), i64immSExt8:$src2),
1936                            addr:$dst),
1937                     (implicit EFLAGS)),
1938           (ADD64mi8 addr:$dst, i64immSExt8:$src2)>;
1939 def : Pat<(parallel (store (X86add_flag (loadi64 addr:$dst), i64immSExt32:$src2),
1940                            addr:$dst),
1941                     (implicit EFLAGS)),
1942           (ADD64mi32 addr:$dst, i64immSExt32:$src2)>;
1943
1944 // Register-Register Subtraction with EFLAGS result
1945 def : Pat<(parallel (X86sub_flag GR64:$src1, GR64:$src2),
1946                     (implicit EFLAGS)),
1947           (SUB64rr GR64:$src1, GR64:$src2)>;
1948
1949 // Register-Memory Subtraction with EFLAGS result
1950 def : Pat<(parallel (X86sub_flag GR64:$src1, (loadi64 addr:$src2)),
1951                     (implicit EFLAGS)),
1952           (SUB64rm GR64:$src1, addr:$src2)>;
1953
1954 // Register-Integer Subtraction with EFLAGS result
1955 def : Pat<(parallel (X86sub_flag GR64:$src1, i64immSExt8:$src2),
1956                     (implicit EFLAGS)),
1957           (SUB64ri8 GR64:$src1, i64immSExt8:$src2)>;
1958 def : Pat<(parallel (X86sub_flag GR64:$src1, i64immSExt32:$src2),
1959                     (implicit EFLAGS)),
1960           (SUB64ri32 GR64:$src1, i64immSExt32:$src2)>;
1961
1962 // Memory-Register Subtraction with EFLAGS result
1963 def : Pat<(parallel (store (X86sub_flag (loadi64 addr:$dst), GR64:$src2),
1964                            addr:$dst),
1965                     (implicit EFLAGS)),
1966           (SUB64mr addr:$dst, GR64:$src2)>;
1967
1968 // Memory-Integer Subtraction with EFLAGS result
1969 def : Pat<(parallel (store (X86sub_flag (loadi64 addr:$dst), i64immSExt8:$src2),
1970                            addr:$dst),
1971                     (implicit EFLAGS)),
1972           (SUB64mi8 addr:$dst, i64immSExt8:$src2)>;
1973 def : Pat<(parallel (store (X86sub_flag (loadi64 addr:$dst), i64immSExt32:$src2),
1974                            addr:$dst),
1975                     (implicit EFLAGS)),
1976           (SUB64mi32 addr:$dst, i64immSExt32:$src2)>;
1977
1978 // Register-Register Signed Integer Multiplication with EFLAGS result
1979 def : Pat<(parallel (X86smul_flag GR64:$src1, GR64:$src2),
1980                     (implicit EFLAGS)),
1981           (IMUL64rr GR64:$src1, GR64:$src2)>;
1982
1983 // Register-Memory Signed Integer Multiplication with EFLAGS result
1984 def : Pat<(parallel (X86smul_flag GR64:$src1, (loadi64 addr:$src2)),
1985                     (implicit EFLAGS)),
1986           (IMUL64rm GR64:$src1, addr:$src2)>;
1987
1988 // Register-Integer Signed Integer Multiplication with EFLAGS result
1989 def : Pat<(parallel (X86smul_flag GR64:$src1, i64immSExt8:$src2),
1990                     (implicit EFLAGS)),
1991           (IMUL64rri8 GR64:$src1, i64immSExt8:$src2)>;
1992 def : Pat<(parallel (X86smul_flag GR64:$src1, i64immSExt32:$src2),
1993                     (implicit EFLAGS)),
1994           (IMUL64rri32 GR64:$src1, i64immSExt32:$src2)>;
1995
1996 // Memory-Integer Signed Integer Multiplication with EFLAGS result
1997 def : Pat<(parallel (X86smul_flag (loadi64 addr:$src1), i64immSExt8:$src2),
1998                     (implicit EFLAGS)),
1999           (IMUL64rmi8 addr:$src1, i64immSExt8:$src2)>;
2000 def : Pat<(parallel (X86smul_flag (loadi64 addr:$src1), i64immSExt32:$src2),
2001                     (implicit EFLAGS)),
2002           (IMUL64rmi32 addr:$src1, i64immSExt32:$src2)>;
2003
2004 // INC and DEC with EFLAGS result. Note that these do not set CF.
2005 def : Pat<(parallel (X86inc_flag GR16:$src), (implicit EFLAGS)),
2006           (INC64_16r GR16:$src)>, Requires<[In64BitMode]>;
2007 def : Pat<(parallel (store (i16 (X86inc_flag (loadi16 addr:$dst))), addr:$dst),
2008                     (implicit EFLAGS)),
2009           (INC64_16m addr:$dst)>, Requires<[In64BitMode]>;
2010 def : Pat<(parallel (X86dec_flag GR16:$src), (implicit EFLAGS)),
2011           (DEC64_16r GR16:$src)>, Requires<[In64BitMode]>;
2012 def : Pat<(parallel (store (i16 (X86dec_flag (loadi16 addr:$dst))), addr:$dst),
2013                     (implicit EFLAGS)),
2014           (DEC64_16m addr:$dst)>, Requires<[In64BitMode]>;
2015
2016 def : Pat<(parallel (X86inc_flag GR32:$src), (implicit EFLAGS)),
2017           (INC64_32r GR32:$src)>, Requires<[In64BitMode]>;
2018 def : Pat<(parallel (store (i32 (X86inc_flag (loadi32 addr:$dst))), addr:$dst),
2019                     (implicit EFLAGS)),
2020           (INC64_32m addr:$dst)>, Requires<[In64BitMode]>;
2021 def : Pat<(parallel (X86dec_flag GR32:$src), (implicit EFLAGS)),
2022           (DEC64_32r GR32:$src)>, Requires<[In64BitMode]>;
2023 def : Pat<(parallel (store (i32 (X86dec_flag (loadi32 addr:$dst))), addr:$dst),
2024                     (implicit EFLAGS)),
2025           (DEC64_32m addr:$dst)>, Requires<[In64BitMode]>;
2026
2027 def : Pat<(parallel (X86inc_flag GR64:$src), (implicit EFLAGS)),
2028           (INC64r GR64:$src)>;
2029 def : Pat<(parallel (store (i64 (X86inc_flag (loadi64 addr:$dst))), addr:$dst),
2030                     (implicit EFLAGS)),
2031           (INC64m addr:$dst)>;
2032 def : Pat<(parallel (X86dec_flag GR64:$src), (implicit EFLAGS)),
2033           (DEC64r GR64:$src)>;
2034 def : Pat<(parallel (store (i64 (X86dec_flag (loadi64 addr:$dst))), addr:$dst),
2035                     (implicit EFLAGS)),
2036           (DEC64m addr:$dst)>;
2037
2038 //===----------------------------------------------------------------------===//
2039 // X86-64 SSE Instructions
2040 //===----------------------------------------------------------------------===//
2041
2042 // Move instructions...
2043
2044 def MOV64toPQIrr : RPDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR64:$src),
2045                         "mov{d|q}\t{$src, $dst|$dst, $src}",
2046                         [(set VR128:$dst,
2047                           (v2i64 (scalar_to_vector GR64:$src)))]>;
2048 def MOVPQIto64rr  : RPDI<0x7E, MRMDestReg, (outs GR64:$dst), (ins VR128:$src),
2049                          "mov{d|q}\t{$src, $dst|$dst, $src}",
2050                          [(set GR64:$dst, (vector_extract (v2i64 VR128:$src),
2051                                            (iPTR 0)))]>;
2052
2053 def MOV64toSDrr : RPDI<0x6E, MRMSrcReg, (outs FR64:$dst), (ins GR64:$src),
2054                        "mov{d|q}\t{$src, $dst|$dst, $src}",
2055                        [(set FR64:$dst, (bitconvert GR64:$src))]>;
2056 def MOV64toSDrm : RPDI<0x6E, MRMSrcMem, (outs FR64:$dst), (ins i64mem:$src),
2057                        "movq\t{$src, $dst|$dst, $src}",
2058                        [(set FR64:$dst, (bitconvert (loadi64 addr:$src)))]>;
2059
2060 def MOVSDto64rr  : RPDI<0x7E, MRMDestReg, (outs GR64:$dst), (ins FR64:$src),
2061                         "mov{d|q}\t{$src, $dst|$dst, $src}",
2062                         [(set GR64:$dst, (bitconvert FR64:$src))]>;
2063 def MOVSDto64mr  : RPDI<0x7E, MRMDestMem, (outs), (ins i64mem:$dst, FR64:$src),
2064                         "movq\t{$src, $dst|$dst, $src}",
2065                         [(store (i64 (bitconvert FR64:$src)), addr:$dst)]>;
2066
2067 //===----------------------------------------------------------------------===//
2068 // X86-64 SSE4.1 Instructions
2069 //===----------------------------------------------------------------------===//
2070
2071 /// SS41I_extract32 - SSE 4.1 extract 32 bits to int reg or memory destination
2072 multiclass SS41I_extract64<bits<8> opc, string OpcodeStr> {
2073   def rr : SS4AIi8<opc, MRMDestReg, (outs GR64:$dst),
2074                  (ins VR128:$src1, i32i8imm:$src2),
2075                  !strconcat(OpcodeStr, 
2076                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
2077                  [(set GR64:$dst,
2078                   (extractelt (v2i64 VR128:$src1), imm:$src2))]>, OpSize, REX_W;
2079   def mr : SS4AIi8<opc, MRMDestMem, (outs),
2080                  (ins i64mem:$dst, VR128:$src1, i32i8imm:$src2),
2081                  !strconcat(OpcodeStr, 
2082                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
2083                  [(store (extractelt (v2i64 VR128:$src1), imm:$src2),
2084                           addr:$dst)]>, OpSize, REX_W;
2085 }
2086
2087 defm PEXTRQ      : SS41I_extract64<0x16, "pextrq">;
2088
2089 let isTwoAddress = 1 in {
2090   multiclass SS41I_insert64<bits<8> opc, string OpcodeStr> {
2091     def rr : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
2092                    (ins VR128:$src1, GR64:$src2, i32i8imm:$src3),
2093                    !strconcat(OpcodeStr, 
2094                     "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
2095                    [(set VR128:$dst, 
2096                      (v2i64 (insertelt VR128:$src1, GR64:$src2, imm:$src3)))]>,
2097                    OpSize, REX_W;
2098     def rm : SS4AIi8<opc, MRMSrcMem, (outs VR128:$dst),
2099                    (ins VR128:$src1, i64mem:$src2, i32i8imm:$src3),
2100                    !strconcat(OpcodeStr,
2101                     "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
2102                    [(set VR128:$dst, 
2103                      (v2i64 (insertelt VR128:$src1, (loadi64 addr:$src2),
2104                                        imm:$src3)))]>, OpSize, REX_W;
2105   }
2106 }
2107
2108 defm PINSRQ      : SS41I_insert64<0x22, "pinsrq">;
2109
2110 // -disable-16bit support.
2111 def : Pat<(truncstorei16 (i64 imm:$src), addr:$dst),
2112           (MOV16mi addr:$dst, imm:$src)>;
2113 def : Pat<(truncstorei16 GR64:$src, addr:$dst),
2114           (MOV16mr addr:$dst, (EXTRACT_SUBREG GR64:$src, x86_subreg_16bit))>;
2115 def : Pat<(i64 (sextloadi16 addr:$dst)),
2116           (MOVSX64rm16 addr:$dst)>;
2117 def : Pat<(i64 (zextloadi16 addr:$dst)),
2118           (MOVZX64rm16 addr:$dst)>;
2119 def : Pat<(i64 (extloadi16 addr:$dst)),
2120           (MOVZX64rm16 addr:$dst)>;