switch CMOVBE to the multipattern:
[oota-llvm.git] / lib / Target / X86 / X86InstrCompiler.td
1 //===- X86InstrCompiler.td - Compiler Pseudos and Patterns -*- 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 various pseudo instructions used by the compiler,
11 // as well as Pat patterns used during instruction selection.
12 //
13 //===----------------------------------------------------------------------===//
14
15 //===----------------------------------------------------------------------===//
16 // Pattern Matching Support
17
18 def GetLo32XForm : SDNodeXForm<imm, [{
19   // Transformation function: get the low 32 bits.
20   return getI32Imm((unsigned)N->getZExtValue());
21 }]>;
22
23
24 //===----------------------------------------------------------------------===//
25 // Random Pseudo Instructions.
26
27 // PIC base construction.  This expands to code that looks like this:
28 //     call  $next_inst
29 //     popl %destreg"
30 let neverHasSideEffects = 1, isNotDuplicable = 1, Uses = [ESP] in
31   def MOVPC32r : Ii32<0xE8, Pseudo, (outs GR32:$reg), (ins i32imm:$label),
32                       "", []>;
33
34
35 // ADJCALLSTACKDOWN/UP implicitly use/def ESP because they may be expanded into
36 // a stack adjustment and the codegen must know that they may modify the stack
37 // pointer before prolog-epilog rewriting occurs.
38 // Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become
39 // sub / add which can clobber EFLAGS.
40 let Defs = [ESP, EFLAGS], Uses = [ESP] in {
41 def ADJCALLSTACKDOWN32 : I<0, Pseudo, (outs), (ins i32imm:$amt),
42                            "#ADJCALLSTACKDOWN",
43                            [(X86callseq_start timm:$amt)]>,
44                           Requires<[In32BitMode]>;
45 def ADJCALLSTACKUP32   : I<0, Pseudo, (outs), (ins i32imm:$amt1, i32imm:$amt2),
46                            "#ADJCALLSTACKUP",
47                            [(X86callseq_end timm:$amt1, timm:$amt2)]>,
48                           Requires<[In32BitMode]>;
49 }
50
51 // ADJCALLSTACKDOWN/UP implicitly use/def RSP because they may be expanded into
52 // a stack adjustment and the codegen must know that they may modify the stack
53 // pointer before prolog-epilog rewriting occurs.
54 // Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become
55 // sub / add which can clobber EFLAGS.
56 let Defs = [RSP, EFLAGS], Uses = [RSP] in {
57 def ADJCALLSTACKDOWN64 : I<0, Pseudo, (outs), (ins i32imm:$amt),
58                            "#ADJCALLSTACKDOWN",
59                            [(X86callseq_start timm:$amt)]>,
60                           Requires<[In64BitMode]>;
61 def ADJCALLSTACKUP64   : I<0, Pseudo, (outs), (ins i32imm:$amt1, i32imm:$amt2),
62                            "#ADJCALLSTACKUP",
63                            [(X86callseq_end timm:$amt1, timm:$amt2)]>,
64                           Requires<[In64BitMode]>;
65 }
66
67
68
69 // x86-64 va_start lowering magic.
70 let usesCustomInserter = 1 in {
71 def VASTART_SAVE_XMM_REGS : I<0, Pseudo,
72                               (outs),
73                               (ins GR8:$al,
74                                    i64imm:$regsavefi, i64imm:$offset,
75                                    variable_ops),
76                               "#VASTART_SAVE_XMM_REGS $al, $regsavefi, $offset",
77                               [(X86vastart_save_xmm_regs GR8:$al,
78                                                          imm:$regsavefi,
79                                                          imm:$offset)]>;
80
81 // Dynamic stack allocation yields _alloca call for Cygwin/Mingw targets.  Calls
82 // to _alloca is needed to probe the stack when allocating more than 4k bytes in
83 // one go. Touching the stack at 4K increments is necessary to ensure that the
84 // guard pages used by the OS virtual memory manager are allocated in correct
85 // sequence.
86 // The main point of having separate instruction are extra unmodelled effects
87 // (compared to ordinary calls) like stack pointer change.
88
89 let Defs = [EAX, ESP, EFLAGS], Uses = [ESP] in
90   def MINGW_ALLOCA : I<0, Pseudo, (outs), (ins),
91                        "# dynamic stack allocation",
92                        [(X86MingwAlloca)]>;
93 }
94
95
96
97 //===----------------------------------------------------------------------===//
98 // EH Pseudo Instructions
99 //
100 let isTerminator = 1, isReturn = 1, isBarrier = 1,
101     hasCtrlDep = 1, isCodeGenOnly = 1 in {
102 def EH_RETURN   : I<0xC3, RawFrm, (outs), (ins GR32:$addr),
103                     "ret\t#eh_return, addr: $addr",
104                     [(X86ehret GR32:$addr)]>;
105
106 }
107
108 let isTerminator = 1, isReturn = 1, isBarrier = 1,
109     hasCtrlDep = 1, isCodeGenOnly = 1 in {
110 def EH_RETURN64   : I<0xC3, RawFrm, (outs), (ins GR64:$addr),
111                      "ret\t#eh_return, addr: $addr",
112                      [(X86ehret GR64:$addr)]>;
113
114 }
115
116 //===----------------------------------------------------------------------===//
117 // Alias Instructions
118 //===----------------------------------------------------------------------===//
119
120 // Alias instructions that map movr0 to xor.
121 // FIXME: remove when we can teach regalloc that xor reg, reg is ok.
122 // FIXME: Set encoding to pseudo.
123 let Defs = [EFLAGS], isReMaterializable = 1, isAsCheapAsAMove = 1,
124     isCodeGenOnly = 1 in {
125 def MOV8r0   : I<0x30, MRMInitReg, (outs GR8 :$dst), (ins), "",
126                  [(set GR8:$dst, 0)]>;
127
128 // We want to rewrite MOV16r0 in terms of MOV32r0, because it's a smaller
129 // encoding and avoids a partial-register update sometimes, but doing so
130 // at isel time interferes with rematerialization in the current register
131 // allocator. For now, this is rewritten when the instruction is lowered
132 // to an MCInst.
133 def MOV16r0   : I<0x31, MRMInitReg, (outs GR16:$dst), (ins),
134                  "",
135                  [(set GR16:$dst, 0)]>, OpSize;
136                  
137 // FIXME: Set encoding to pseudo.
138 def MOV32r0  : I<0x31, MRMInitReg, (outs GR32:$dst), (ins), "",
139                  [(set GR32:$dst, 0)]>;
140 }
141
142 // We want to rewrite MOV64r0 in terms of MOV32r0, because it's sometimes a
143 // smaller encoding, but doing so at isel time interferes with rematerialization
144 // in the current register allocator. For now, this is rewritten when the
145 // instruction is lowered to an MCInst.
146 // FIXME: AddedComplexity gives this a higher priority than MOV64ri32. Remove
147 // when we have a better way to specify isel priority.
148 let Defs = [EFLAGS],
149     AddedComplexity = 1, isReMaterializable = 1, isAsCheapAsAMove = 1 in
150 def MOV64r0   : I<0x31, MRMInitReg, (outs GR64:$dst), (ins), "",
151                  [(set GR64:$dst, 0)]>;
152
153 // Materialize i64 constant where top 32-bits are zero. This could theoretically
154 // use MOV32ri with a SUBREG_TO_REG to represent the zero-extension, however
155 // that would make it more difficult to rematerialize.
156 let AddedComplexity = 1, isReMaterializable = 1, isAsCheapAsAMove = 1 in
157 def MOV64ri64i32 : Ii32<0xB8, AddRegFrm, (outs GR64:$dst), (ins i64i32imm:$src),
158                         "", [(set GR64:$dst, i64immZExt32:$src)]>;
159
160
161 // Use sbb to materialize carry bit.
162 let Uses = [EFLAGS], Defs = [EFLAGS], isCodeGenOnly = 1 in {
163 // FIXME: These are pseudo ops that should be replaced with Pat<> patterns.
164 // However, Pat<> can't replicate the destination reg into the inputs of the
165 // result.
166 // FIXME: Change these to have encoding Pseudo when X86MCCodeEmitter replaces
167 // X86CodeEmitter.
168 def SETB_C8r : I<0x18, MRMInitReg, (outs GR8:$dst), (ins), "",
169                  [(set GR8:$dst, (X86setcc_c X86_COND_B, EFLAGS))]>;
170 def SETB_C16r : I<0x19, MRMInitReg, (outs GR16:$dst), (ins), "",
171                  [(set GR16:$dst, (X86setcc_c X86_COND_B, EFLAGS))]>,
172                 OpSize;
173 def SETB_C32r : I<0x19, MRMInitReg, (outs GR32:$dst), (ins), "",
174                  [(set GR32:$dst, (X86setcc_c X86_COND_B, EFLAGS))]>;
175 def SETB_C64r : RI<0x19, MRMInitReg, (outs GR64:$dst), (ins), "",
176                  [(set GR64:$dst, (X86setcc_c X86_COND_B, EFLAGS))]>;
177 } // isCodeGenOnly
178
179
180 def : Pat<(i64 (anyext (i8 (X86setcc_c X86_COND_B, EFLAGS)))),
181           (SETB_C64r)>;
182
183              
184 //===----------------------------------------------------------------------===//
185 // String Pseudo Instructions
186 //
187 let Defs = [ECX,EDI,ESI], Uses = [ECX,EDI,ESI], isCodeGenOnly = 1 in {
188 def REP_MOVSB : I<0xA4, RawFrm, (outs), (ins), "{rep;movsb|rep movsb}",
189                   [(X86rep_movs i8)]>, REP;
190 def REP_MOVSW : I<0xA5, RawFrm, (outs), (ins), "{rep;movsw|rep movsw}",
191                   [(X86rep_movs i16)]>, REP, OpSize;
192 def REP_MOVSD : I<0xA5, RawFrm, (outs), (ins), "{rep;movsl|rep movsd}",
193                   [(X86rep_movs i32)]>, REP;
194 }
195
196 let Defs = [RCX,RDI,RSI], Uses = [RCX,RDI,RSI], isCodeGenOnly = 1 in
197 def REP_MOVSQ : RI<0xA5, RawFrm, (outs), (ins), "{rep;movsq|rep movsq}",
198                    [(X86rep_movs i64)]>, REP;
199                    
200
201 // FIXME: Should use "(X86rep_stos AL)" as the pattern.
202 let Defs = [ECX,EDI], Uses = [AL,ECX,EDI], isCodeGenOnly = 1 in
203 def REP_STOSB : I<0xAA, RawFrm, (outs), (ins), "{rep;stosb|rep stosb}",
204                   [(X86rep_stos i8)]>, REP;
205 let Defs = [ECX,EDI], Uses = [AX,ECX,EDI], isCodeGenOnly = 1 in
206 def REP_STOSW : I<0xAB, RawFrm, (outs), (ins), "{rep;stosw|rep stosw}",
207                   [(X86rep_stos i16)]>, REP, OpSize;
208 let Defs = [ECX,EDI], Uses = [EAX,ECX,EDI], isCodeGenOnly = 1 in
209 def REP_STOSD : I<0xAB, RawFrm, (outs), (ins), "{rep;stosl|rep stosd}",
210                   [(X86rep_stos i32)]>, REP;
211
212 let Defs = [RCX,RDI], Uses = [RAX,RCX,RDI], isCodeGenOnly = 1 in
213 def REP_STOSQ : RI<0xAB, RawFrm, (outs), (ins), "{rep;stosq|rep stosq}",
214                    [(X86rep_stos i64)]>, REP;
215
216
217 //===----------------------------------------------------------------------===//
218 // Thread Local Storage Instructions
219 //
220
221 // ELF TLS Support
222 // All calls clobber the non-callee saved registers. ESP is marked as
223 // a use to prevent stack-pointer assignments that appear immediately
224 // before calls from potentially appearing dead.
225 let Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0,
226             MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
227             XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
228             XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS],
229     Uses = [ESP] in
230 def TLS_addr32 : I<0, Pseudo, (outs), (ins i32mem:$sym),
231                   "leal\t$sym, %eax; "
232                   "call\t___tls_get_addr@PLT",
233                   [(X86tlsaddr tls32addr:$sym)]>,
234                   Requires<[In32BitMode]>;
235
236 // All calls clobber the non-callee saved registers. RSP is marked as
237 // a use to prevent stack-pointer assignments that appear immediately
238 // before calls from potentially appearing dead.
239 let Defs = [RAX, RCX, RDX, RSI, RDI, R8, R9, R10, R11,
240             FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0, ST1,
241             MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
242             XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
243             XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS],
244     Uses = [RSP] in
245 def TLS_addr64 : I<0, Pseudo, (outs), (ins i64mem:$sym),
246                    ".byte\t0x66; "
247                    "leaq\t$sym(%rip), %rdi; "
248                    ".word\t0x6666; "
249                    "rex64; "
250                    "call\t__tls_get_addr@PLT",
251                   [(X86tlsaddr tls64addr:$sym)]>,
252                   Requires<[In64BitMode]>;
253
254 // Darwin TLS Support
255 // For i386, the address of the thunk is passed on the stack, on return the 
256 // address of the variable is in %eax.  %ecx is trashed during the function 
257 // call.  All other registers are preserved.
258 let Defs = [EAX, ECX],
259     Uses = [ESP],
260     usesCustomInserter = 1 in
261 def TLSCall_32 : I<0, Pseudo, (outs), (ins i32mem:$sym),
262                 "# TLSCall_32",
263                 [(X86TLSCall addr:$sym)]>,
264                 Requires<[In32BitMode]>;
265
266 // For x86_64, the address of the thunk is passed in %rdi, on return 
267 // the address of the variable is in %rax.  All other registers are preserved.
268 let Defs = [RAX],
269     Uses = [RDI],
270     usesCustomInserter = 1 in
271 def TLSCall_64 : I<0, Pseudo, (outs), (ins i64mem:$sym),
272                   "# TLSCall_64",
273                   [(X86TLSCall addr:$sym)]>,
274                   Requires<[In64BitMode]>;
275
276 //===----------------------------------------------------------------------===//
277 // Atomic Instruction Pseudo Instructions
278 //===----------------------------------------------------------------------===//
279
280 // Atomic exchange, and, or, xor
281 let Constraints = "$val = $dst", Defs = [EFLAGS],
282                   usesCustomInserter = 1 in {
283                   
284 def ATOMAND8 : I<0, Pseudo, (outs GR8:$dst),(ins i8mem:$ptr, GR8:$val),
285                "#ATOMAND8 PSEUDO!", 
286                [(set GR8:$dst, (atomic_load_and_8 addr:$ptr, GR8:$val))]>;
287 def ATOMOR8 : I<0, Pseudo, (outs GR8:$dst),(ins i8mem:$ptr, GR8:$val),
288                "#ATOMOR8 PSEUDO!", 
289                [(set GR8:$dst, (atomic_load_or_8 addr:$ptr, GR8:$val))]>;
290 def ATOMXOR8 : I<0, Pseudo,(outs GR8:$dst),(ins i8mem:$ptr, GR8:$val),
291                "#ATOMXOR8 PSEUDO!", 
292                [(set GR8:$dst, (atomic_load_xor_8 addr:$ptr, GR8:$val))]>;
293 def ATOMNAND8 : I<0, Pseudo,(outs GR8:$dst),(ins i8mem:$ptr, GR8:$val),
294                "#ATOMNAND8 PSEUDO!", 
295                [(set GR8:$dst, (atomic_load_nand_8 addr:$ptr, GR8:$val))]>;
296
297 def ATOMAND16 : I<0, Pseudo, (outs GR16:$dst),(ins i16mem:$ptr, GR16:$val),
298                "#ATOMAND16 PSEUDO!", 
299                [(set GR16:$dst, (atomic_load_and_16 addr:$ptr, GR16:$val))]>;
300 def ATOMOR16 : I<0, Pseudo, (outs GR16:$dst),(ins i16mem:$ptr, GR16:$val),
301                "#ATOMOR16 PSEUDO!", 
302                [(set GR16:$dst, (atomic_load_or_16 addr:$ptr, GR16:$val))]>;
303 def ATOMXOR16 : I<0, Pseudo,(outs GR16:$dst),(ins i16mem:$ptr, GR16:$val),
304                "#ATOMXOR16 PSEUDO!", 
305                [(set GR16:$dst, (atomic_load_xor_16 addr:$ptr, GR16:$val))]>;
306 def ATOMNAND16 : I<0, Pseudo,(outs GR16:$dst),(ins i16mem:$ptr, GR16:$val),
307                "#ATOMNAND16 PSEUDO!", 
308                [(set GR16:$dst, (atomic_load_nand_16 addr:$ptr, GR16:$val))]>;
309 def ATOMMIN16: I<0, Pseudo, (outs GR16:$dst), (ins i16mem:$ptr, GR16:$val),
310                "#ATOMMIN16 PSEUDO!", 
311                [(set GR16:$dst, (atomic_load_min_16 addr:$ptr, GR16:$val))]>;
312 def ATOMMAX16: I<0, Pseudo, (outs GR16:$dst),(ins i16mem:$ptr, GR16:$val),
313                "#ATOMMAX16 PSEUDO!", 
314                [(set GR16:$dst, (atomic_load_max_16 addr:$ptr, GR16:$val))]>;
315 def ATOMUMIN16: I<0, Pseudo, (outs GR16:$dst),(ins i16mem:$ptr, GR16:$val),
316                "#ATOMUMIN16 PSEUDO!", 
317                [(set GR16:$dst, (atomic_load_umin_16 addr:$ptr, GR16:$val))]>;
318 def ATOMUMAX16: I<0, Pseudo, (outs GR16:$dst),(ins i16mem:$ptr, GR16:$val),
319                "#ATOMUMAX16 PSEUDO!", 
320                [(set GR16:$dst, (atomic_load_umax_16 addr:$ptr, GR16:$val))]>;
321
322
323 def ATOMAND32 : I<0, Pseudo, (outs GR32:$dst),(ins i32mem:$ptr, GR32:$val),
324                "#ATOMAND32 PSEUDO!", 
325                [(set GR32:$dst, (atomic_load_and_32 addr:$ptr, GR32:$val))]>;
326 def ATOMOR32 : I<0, Pseudo, (outs GR32:$dst),(ins i32mem:$ptr, GR32:$val),
327                "#ATOMOR32 PSEUDO!", 
328                [(set GR32:$dst, (atomic_load_or_32 addr:$ptr, GR32:$val))]>;
329 def ATOMXOR32 : I<0, Pseudo,(outs GR32:$dst),(ins i32mem:$ptr, GR32:$val),
330                "#ATOMXOR32 PSEUDO!", 
331                [(set GR32:$dst, (atomic_load_xor_32 addr:$ptr, GR32:$val))]>;
332 def ATOMNAND32 : I<0, Pseudo,(outs GR32:$dst),(ins i32mem:$ptr, GR32:$val),
333                "#ATOMNAND32 PSEUDO!", 
334                [(set GR32:$dst, (atomic_load_nand_32 addr:$ptr, GR32:$val))]>;
335 def ATOMMIN32: I<0, Pseudo, (outs GR32:$dst), (ins i32mem:$ptr, GR32:$val),
336                "#ATOMMIN32 PSEUDO!", 
337                [(set GR32:$dst, (atomic_load_min_32 addr:$ptr, GR32:$val))]>;
338 def ATOMMAX32: I<0, Pseudo, (outs GR32:$dst),(ins i32mem:$ptr, GR32:$val),
339                "#ATOMMAX32 PSEUDO!", 
340                [(set GR32:$dst, (atomic_load_max_32 addr:$ptr, GR32:$val))]>;
341 def ATOMUMIN32: I<0, Pseudo, (outs GR32:$dst),(ins i32mem:$ptr, GR32:$val),
342                "#ATOMUMIN32 PSEUDO!", 
343                [(set GR32:$dst, (atomic_load_umin_32 addr:$ptr, GR32:$val))]>;
344 def ATOMUMAX32: I<0, Pseudo, (outs GR32:$dst),(ins i32mem:$ptr, GR32:$val),
345                "#ATOMUMAX32 PSEUDO!", 
346                [(set GR32:$dst, (atomic_load_umax_32 addr:$ptr, GR32:$val))]>;
347
348
349                   
350 def ATOMAND64 : I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
351                "#ATOMAND64 PSEUDO!", 
352                [(set GR64:$dst, (atomic_load_and_64 addr:$ptr, GR64:$val))]>;
353 def ATOMOR64 : I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
354                "#ATOMOR64 PSEUDO!", 
355                [(set GR64:$dst, (atomic_load_or_64 addr:$ptr, GR64:$val))]>;
356 def ATOMXOR64 : I<0, Pseudo,(outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
357                "#ATOMXOR64 PSEUDO!", 
358                [(set GR64:$dst, (atomic_load_xor_64 addr:$ptr, GR64:$val))]>;
359 def ATOMNAND64 : I<0, Pseudo,(outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
360                "#ATOMNAND64 PSEUDO!", 
361                [(set GR64:$dst, (atomic_load_nand_64 addr:$ptr, GR64:$val))]>;
362 def ATOMMIN64: I<0, Pseudo, (outs GR64:$dst), (ins i64mem:$ptr, GR64:$val),
363                "#ATOMMIN64 PSEUDO!", 
364                [(set GR64:$dst, (atomic_load_min_64 addr:$ptr, GR64:$val))]>;
365 def ATOMMAX64: I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
366                "#ATOMMAX64 PSEUDO!", 
367                [(set GR64:$dst, (atomic_load_max_64 addr:$ptr, GR64:$val))]>;
368 def ATOMUMIN64: I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
369                "#ATOMUMIN64 PSEUDO!", 
370                [(set GR64:$dst, (atomic_load_umin_64 addr:$ptr, GR64:$val))]>;
371 def ATOMUMAX64: I<0, Pseudo, (outs GR64:$dst),(ins i64mem:$ptr, GR64:$val),
372                "#ATOMUMAX64 PSEUDO!", 
373                [(set GR64:$dst, (atomic_load_umax_64 addr:$ptr, GR64:$val))]>;
374 }
375
376 let Constraints = "$val1 = $dst1, $val2 = $dst2", 
377                   Defs = [EFLAGS, EAX, EBX, ECX, EDX],
378                   Uses = [EAX, EBX, ECX, EDX],
379                   mayLoad = 1, mayStore = 1,
380                   usesCustomInserter = 1 in {
381 def ATOMAND6432 : I<0, Pseudo, (outs GR32:$dst1, GR32:$dst2),
382                                (ins i64mem:$ptr, GR32:$val1, GR32:$val2),
383                "#ATOMAND6432 PSEUDO!", []>;
384 def ATOMOR6432 : I<0, Pseudo, (outs GR32:$dst1, GR32:$dst2),
385                                (ins i64mem:$ptr, GR32:$val1, GR32:$val2),
386                "#ATOMOR6432 PSEUDO!", []>;
387 def ATOMXOR6432 : I<0, Pseudo, (outs GR32:$dst1, GR32:$dst2),
388                                (ins i64mem:$ptr, GR32:$val1, GR32:$val2),
389                "#ATOMXOR6432 PSEUDO!", []>;
390 def ATOMNAND6432 : I<0, Pseudo, (outs GR32:$dst1, GR32:$dst2),
391                                (ins i64mem:$ptr, GR32:$val1, GR32:$val2),
392                "#ATOMNAND6432 PSEUDO!", []>;
393 def ATOMADD6432 : I<0, Pseudo, (outs GR32:$dst1, GR32:$dst2),
394                                (ins i64mem:$ptr, GR32:$val1, GR32:$val2),
395                "#ATOMADD6432 PSEUDO!", []>;
396 def ATOMSUB6432 : I<0, Pseudo, (outs GR32:$dst1, GR32:$dst2),
397                                (ins i64mem:$ptr, GR32:$val1, GR32:$val2),
398                "#ATOMSUB6432 PSEUDO!", []>;
399 def ATOMSWAP6432 : I<0, Pseudo, (outs GR32:$dst1, GR32:$dst2),
400                                (ins i64mem:$ptr, GR32:$val1, GR32:$val2),
401                "#ATOMSWAP6432 PSEUDO!", []>;
402 }
403
404 //===----------------------------------------------------------------------===//
405 // Normal-Instructions-With-Lock-Prefix Pseudo Instructions
406 //===----------------------------------------------------------------------===//
407
408 // FIXME: Use normal instructions and add lock prefix dynamically.
409
410 // Memory barriers
411
412 // TODO: Get this to fold the constant into the instruction.           
413 def OR32mrLocked  : I<0x09, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$zero),
414                       "lock\n\t"
415                       "or{l}\t{$zero, $dst|$dst, $zero}",
416                       []>, Requires<[In32BitMode]>, LOCK;
417
418 let hasSideEffects = 1 in
419 def Int_MemBarrier : I<0, Pseudo, (outs), (ins),
420                      "#MEMBARRIER",
421                      [(X86MemBarrier)]>, Requires<[HasSSE2]>;
422
423 // TODO: Get this to fold the constant into the instruction.           
424 let hasSideEffects = 1, Defs = [ESP] in
425 def Int_MemBarrierNoSSE64  : RI<0x09, MRM1r, (outs), (ins GR64:$zero),
426                            "lock\n\t"
427                            "or{q}\t{$zero, (%rsp)|(%rsp), $zero}",
428                            [(X86MemBarrierNoSSE GR64:$zero)]>,
429                            Requires<[In64BitMode]>, LOCK;
430
431
432 // Optimized codegen when the non-memory output is not used.
433 let Defs = [EFLAGS], mayLoad = 1, mayStore = 1 in {
434 def LOCK_ADD8mr  : I<0x00, MRMDestMem, (outs), (ins i8mem:$dst, GR8:$src2),
435                     "lock\n\t"
436                     "add{b}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
437 def LOCK_ADD16mr  : I<0x01, MRMDestMem, (outs), (ins i16mem:$dst, GR16:$src2),
438                     "lock\n\t"
439                     "add{w}\t{$src2, $dst|$dst, $src2}", []>, OpSize, LOCK;
440 def LOCK_ADD32mr  : I<0x01, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src2),
441                     "lock\n\t"
442                     "add{l}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
443 def LOCK_ADD64mr : RI<0x01, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2),
444                       "lock\n\t"
445                       "add{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
446                       
447 def LOCK_ADD8mi   : Ii8<0x80, MRM0m, (outs), (ins i8mem :$dst, i8imm :$src2),
448                     "lock\n\t"
449                     "add{b}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
450 def LOCK_ADD16mi  : Ii16<0x81, MRM0m, (outs), (ins i16mem:$dst, i16imm:$src2),
451                     "lock\n\t"
452                      "add{w}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
453 def LOCK_ADD32mi  : Ii32<0x81, MRM0m, (outs), (ins i32mem:$dst, i32imm:$src2),
454                     "lock\n\t"
455                     "add{l}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
456 def LOCK_ADD64mi32 : RIi32<0x81, MRM0m, (outs),
457                                         (ins i64mem:$dst, i64i32imm :$src2),
458                       "lock\n\t"
459                       "add{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
460
461 def LOCK_ADD16mi8 : Ii8<0x83, MRM0m, (outs), (ins i16mem:$dst, i16i8imm :$src2),
462                     "lock\n\t"
463                     "add{w}\t{$src2, $dst|$dst, $src2}", []>, OpSize, LOCK;
464 def LOCK_ADD32mi8 : Ii8<0x83, MRM0m, (outs), (ins i32mem:$dst, i32i8imm :$src2),
465                     "lock\n\t"
466                     "add{l}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
467 def LOCK_ADD64mi8 : RIi8<0x83, MRM0m, (outs),
468                                       (ins i64mem:$dst, i64i8imm :$src2),
469                     "lock\n\t"
470                     "add{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
471
472 def LOCK_SUB8mr   : I<0x28, MRMDestMem, (outs), (ins i8mem :$dst, GR8 :$src2),
473                     "lock\n\t"
474                     "sub{b}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
475 def LOCK_SUB16mr  : I<0x29, MRMDestMem, (outs), (ins i16mem:$dst, GR16:$src2),
476                     "lock\n\t"
477                     "sub{w}\t{$src2, $dst|$dst, $src2}", []>, OpSize, LOCK;
478 def LOCK_SUB32mr  : I<0x29, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src2), 
479                     "lock\n\t"
480                     "sub{l}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
481 def LOCK_SUB64mr : RI<0x29, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src2), 
482                       "lock\n\t"
483                       "sub{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
484
485
486 def LOCK_SUB8mi   : Ii8<0x80, MRM5m, (outs), (ins i8mem :$dst, i8imm:$src2), 
487                     "lock\n\t"
488                     "sub{b}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
489 def LOCK_SUB16mi  : Ii16<0x81, MRM5m, (outs), (ins i16mem:$dst, i16imm:$src2), 
490                     "lock\n\t"
491                     "sub{w}\t{$src2, $dst|$dst, $src2}", []>, OpSize, LOCK;
492 def LOCK_SUB32mi  : Ii32<0x81, MRM5m, (outs), (ins i32mem:$dst, i32imm:$src2), 
493                     "lock\n\t"
494                      "sub{l}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
495 def LOCK_SUB64mi32 : RIi32<0x81, MRM5m, (outs),
496                                         (ins i64mem:$dst, i64i32imm:$src2),
497                       "lock\n\t"
498                       "sub{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
499
500
501 def LOCK_SUB16mi8 : Ii8<0x83, MRM5m, (outs), (ins i16mem:$dst, i16i8imm :$src2),
502                     "lock\n\t"
503                      "sub{w}\t{$src2, $dst|$dst, $src2}", []>, OpSize, LOCK;
504 def LOCK_SUB32mi8 : Ii8<0x83, MRM5m, (outs), (ins i32mem:$dst, i32i8imm :$src2),
505                     "lock\n\t"
506                      "sub{l}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
507 def LOCK_SUB64mi8 : RIi8<0x83, MRM5m, (outs),
508                                       (ins i64mem:$dst, i64i8imm :$src2), 
509                       "lock\n\t"
510                       "sub{q}\t{$src2, $dst|$dst, $src2}", []>, LOCK;
511
512 def LOCK_INC8m  : I<0xFE, MRM0m, (outs), (ins i8mem :$dst),
513                     "lock\n\t"
514                     "inc{b}\t$dst", []>, LOCK;
515 def LOCK_INC16m : I<0xFF, MRM0m, (outs), (ins i16mem:$dst),
516                     "lock\n\t"
517                     "inc{w}\t$dst", []>, OpSize, LOCK;
518 def LOCK_INC32m : I<0xFF, MRM0m, (outs), (ins i32mem:$dst),
519                     "lock\n\t"
520                     "inc{l}\t$dst", []>, LOCK;
521 def LOCK_INC64m : RI<0xFF, MRM0m, (outs), (ins i64mem:$dst),
522                      "lock\n\t"
523                      "inc{q}\t$dst", []>, LOCK;
524
525 def LOCK_DEC8m  : I<0xFE, MRM1m, (outs), (ins i8mem :$dst),
526                     "lock\n\t"
527                     "dec{b}\t$dst", []>, LOCK;
528 def LOCK_DEC16m : I<0xFF, MRM1m, (outs), (ins i16mem:$dst),
529                     "lock\n\t"
530                     "dec{w}\t$dst", []>, OpSize, LOCK;
531 def LOCK_DEC32m : I<0xFF, MRM1m, (outs), (ins i32mem:$dst),
532                     "lock\n\t"
533                     "dec{l}\t$dst", []>, LOCK;
534 def LOCK_DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst),
535                       "lock\n\t"
536                       "dec{q}\t$dst", []>, LOCK;
537 }
538
539 // Atomic compare and swap.
540 let Defs = [EAX, EDX, EFLAGS], Uses = [EAX, EBX, ECX, EDX] in {
541 def LCMPXCHG8B : I<0xC7, MRM1m, (outs), (ins i64mem:$ptr),
542                "lock\n\t"
543                "cmpxchg8b\t$ptr",
544                [(X86cas8 addr:$ptr)]>, TB, LOCK;
545 }
546 let Defs = [AL, EFLAGS], Uses = [AL] in {
547 def LCMPXCHG8 : I<0xB0, MRMDestMem, (outs), (ins i8mem:$ptr, GR8:$swap),
548                "lock\n\t"
549                "cmpxchg{b}\t{$swap, $ptr|$ptr, $swap}",
550                [(X86cas addr:$ptr, GR8:$swap, 1)]>, TB, LOCK;
551 }
552
553 let Defs = [AX, EFLAGS], Uses = [AX] in {
554 def LCMPXCHG16 : I<0xB1, MRMDestMem, (outs), (ins i16mem:$ptr, GR16:$swap),
555                "lock\n\t"
556                "cmpxchg{w}\t{$swap, $ptr|$ptr, $swap}",
557                [(X86cas addr:$ptr, GR16:$swap, 2)]>, TB, OpSize, LOCK;
558 }
559
560 let Defs = [EAX, EFLAGS], Uses = [EAX] in {
561 def LCMPXCHG32 : I<0xB1, MRMDestMem, (outs), (ins i32mem:$ptr, GR32:$swap),
562                "lock\n\t"
563                "cmpxchg{l}\t{$swap, $ptr|$ptr, $swap}",
564                [(X86cas addr:$ptr, GR32:$swap, 4)]>, TB, LOCK;
565 }
566
567 let Defs = [RAX, EFLAGS], Uses = [RAX] in {
568 def LCMPXCHG64 : RI<0xB1, MRMDestMem, (outs), (ins i64mem:$ptr, GR64:$swap),
569                "lock\n\t"
570                "cmpxchgq\t$swap,$ptr",
571                [(X86cas addr:$ptr, GR64:$swap, 8)]>, TB, LOCK;
572 }
573
574 // Atomic exchange and add
575 let Constraints = "$val = $dst", Defs = [EFLAGS] in {
576 def LXADD8  : I<0xC0, MRMSrcMem, (outs GR8:$dst), (ins GR8:$val, i8mem:$ptr),
577                "lock\n\t"
578                "xadd{b}\t{$val, $ptr|$ptr, $val}",
579                [(set GR8:$dst, (atomic_load_add_8 addr:$ptr, GR8:$val))]>,
580                 TB, LOCK;
581 def LXADD16 : I<0xC1, MRMSrcMem, (outs GR16:$dst), (ins GR16:$val, i16mem:$ptr),
582                "lock\n\t"
583                "xadd{w}\t{$val, $ptr|$ptr, $val}",
584                [(set GR16:$dst, (atomic_load_add_16 addr:$ptr, GR16:$val))]>,
585                 TB, OpSize, LOCK;
586 def LXADD32 : I<0xC1, MRMSrcMem, (outs GR32:$dst), (ins GR32:$val, i32mem:$ptr),
587                "lock\n\t"
588                "xadd{l}\t{$val, $ptr|$ptr, $val}",
589                [(set GR32:$dst, (atomic_load_add_32 addr:$ptr, GR32:$val))]>,
590                 TB, LOCK;
591 def LXADD64 : RI<0xC1, MRMSrcMem, (outs GR64:$dst), (ins GR64:$val,i64mem:$ptr),
592                "lock\n\t"
593                "xadd\t$val, $ptr",
594                [(set GR64:$dst, (atomic_load_add_64 addr:$ptr, GR64:$val))]>,
595                 TB, LOCK;
596 }
597
598 //===----------------------------------------------------------------------===//
599 // Conditional Move Pseudo Instructions.
600 //===----------------------------------------------------------------------===//
601
602
603 // CMOV* - Used to implement the SSE SELECT DAG operation.  Expanded after
604 // instruction selection into a branch sequence.
605 let Uses = [EFLAGS], usesCustomInserter = 1 in {
606   def CMOV_FR32 : I<0, Pseudo,
607                     (outs FR32:$dst), (ins FR32:$t, FR32:$f, i8imm:$cond),
608                     "#CMOV_FR32 PSEUDO!",
609                     [(set FR32:$dst, (X86cmov FR32:$t, FR32:$f, imm:$cond,
610                                                   EFLAGS))]>;
611   def CMOV_FR64 : I<0, Pseudo,
612                     (outs FR64:$dst), (ins FR64:$t, FR64:$f, i8imm:$cond),
613                     "#CMOV_FR64 PSEUDO!",
614                     [(set FR64:$dst, (X86cmov FR64:$t, FR64:$f, imm:$cond,
615                                                   EFLAGS))]>;
616   def CMOV_V4F32 : I<0, Pseudo,
617                     (outs VR128:$dst), (ins VR128:$t, VR128:$f, i8imm:$cond),
618                     "#CMOV_V4F32 PSEUDO!",
619                     [(set VR128:$dst,
620                       (v4f32 (X86cmov VR128:$t, VR128:$f, imm:$cond,
621                                           EFLAGS)))]>;
622   def CMOV_V2F64 : I<0, Pseudo,
623                     (outs VR128:$dst), (ins VR128:$t, VR128:$f, i8imm:$cond),
624                     "#CMOV_V2F64 PSEUDO!",
625                     [(set VR128:$dst,
626                       (v2f64 (X86cmov VR128:$t, VR128:$f, imm:$cond,
627                                           EFLAGS)))]>;
628   def CMOV_V2I64 : I<0, Pseudo,
629                     (outs VR128:$dst), (ins VR128:$t, VR128:$f, i8imm:$cond),
630                     "#CMOV_V2I64 PSEUDO!",
631                     [(set VR128:$dst,
632                       (v2i64 (X86cmov VR128:$t, VR128:$f, imm:$cond,
633                                           EFLAGS)))]>;
634 }
635
636
637 //===----------------------------------------------------------------------===//
638 // DAG Pattern Matching Rules
639 //===----------------------------------------------------------------------===//
640
641 // ConstantPool GlobalAddress, ExternalSymbol, and JumpTable
642 def : Pat<(i32 (X86Wrapper tconstpool  :$dst)), (MOV32ri tconstpool  :$dst)>;
643 def : Pat<(i32 (X86Wrapper tjumptable  :$dst)), (MOV32ri tjumptable  :$dst)>;
644 def : Pat<(i32 (X86Wrapper tglobaltlsaddr:$dst)),(MOV32ri tglobaltlsaddr:$dst)>;
645 def : Pat<(i32 (X86Wrapper tglobaladdr :$dst)), (MOV32ri tglobaladdr :$dst)>;
646 def : Pat<(i32 (X86Wrapper texternalsym:$dst)), (MOV32ri texternalsym:$dst)>;
647 def : Pat<(i32 (X86Wrapper tblockaddress:$dst)), (MOV32ri tblockaddress:$dst)>;
648
649 def : Pat<(add GR32:$src1, (X86Wrapper tconstpool:$src2)),
650           (ADD32ri GR32:$src1, tconstpool:$src2)>;
651 def : Pat<(add GR32:$src1, (X86Wrapper tjumptable:$src2)),
652           (ADD32ri GR32:$src1, tjumptable:$src2)>;
653 def : Pat<(add GR32:$src1, (X86Wrapper tglobaladdr :$src2)),
654           (ADD32ri GR32:$src1, tglobaladdr:$src2)>;
655 def : Pat<(add GR32:$src1, (X86Wrapper texternalsym:$src2)),
656           (ADD32ri GR32:$src1, texternalsym:$src2)>;
657 def : Pat<(add GR32:$src1, (X86Wrapper tblockaddress:$src2)),
658           (ADD32ri GR32:$src1, tblockaddress:$src2)>;
659
660 def : Pat<(store (i32 (X86Wrapper tglobaladdr:$src)), addr:$dst),
661           (MOV32mi addr:$dst, tglobaladdr:$src)>;
662 def : Pat<(store (i32 (X86Wrapper texternalsym:$src)), addr:$dst),
663           (MOV32mi addr:$dst, texternalsym:$src)>;
664 def : Pat<(store (i32 (X86Wrapper tblockaddress:$src)), addr:$dst),
665           (MOV32mi addr:$dst, tblockaddress:$src)>;
666
667
668
669 // ConstantPool GlobalAddress, ExternalSymbol, and JumpTable when not in small
670 // code model mode, should use 'movabs'.  FIXME: This is really a hack, the
671 //  'movabs' predicate should handle this sort of thing.
672 def : Pat<(i64 (X86Wrapper tconstpool  :$dst)),
673           (MOV64ri tconstpool  :$dst)>, Requires<[FarData]>;
674 def : Pat<(i64 (X86Wrapper tjumptable  :$dst)),
675           (MOV64ri tjumptable  :$dst)>, Requires<[FarData]>;
676 def : Pat<(i64 (X86Wrapper tglobaladdr :$dst)),
677           (MOV64ri tglobaladdr :$dst)>, Requires<[FarData]>;
678 def : Pat<(i64 (X86Wrapper texternalsym:$dst)),
679           (MOV64ri texternalsym:$dst)>, Requires<[FarData]>;
680 def : Pat<(i64 (X86Wrapper tblockaddress:$dst)),
681           (MOV64ri tblockaddress:$dst)>, Requires<[FarData]>;
682
683 // In static codegen with small code model, we can get the address of a label
684 // into a register with 'movl'.  FIXME: This is a hack, the 'imm' predicate of
685 // the MOV64ri64i32 should accept these.
686 def : Pat<(i64 (X86Wrapper tconstpool  :$dst)),
687           (MOV64ri64i32 tconstpool  :$dst)>, Requires<[SmallCode]>;
688 def : Pat<(i64 (X86Wrapper tjumptable  :$dst)),
689           (MOV64ri64i32 tjumptable  :$dst)>, Requires<[SmallCode]>;
690 def : Pat<(i64 (X86Wrapper tglobaladdr :$dst)),
691           (MOV64ri64i32 tglobaladdr :$dst)>, Requires<[SmallCode]>;
692 def : Pat<(i64 (X86Wrapper texternalsym:$dst)),
693           (MOV64ri64i32 texternalsym:$dst)>, Requires<[SmallCode]>;
694 def : Pat<(i64 (X86Wrapper tblockaddress:$dst)),
695           (MOV64ri64i32 tblockaddress:$dst)>, Requires<[SmallCode]>;
696
697 // In kernel code model, we can get the address of a label
698 // into a register with 'movq'.  FIXME: This is a hack, the 'imm' predicate of
699 // the MOV64ri32 should accept these.
700 def : Pat<(i64 (X86Wrapper tconstpool  :$dst)),
701           (MOV64ri32 tconstpool  :$dst)>, Requires<[KernelCode]>;
702 def : Pat<(i64 (X86Wrapper tjumptable  :$dst)),
703           (MOV64ri32 tjumptable  :$dst)>, Requires<[KernelCode]>;
704 def : Pat<(i64 (X86Wrapper tglobaladdr :$dst)),
705           (MOV64ri32 tglobaladdr :$dst)>, Requires<[KernelCode]>;
706 def : Pat<(i64 (X86Wrapper texternalsym:$dst)),
707           (MOV64ri32 texternalsym:$dst)>, Requires<[KernelCode]>;
708 def : Pat<(i64 (X86Wrapper tblockaddress:$dst)),
709           (MOV64ri32 tblockaddress:$dst)>, Requires<[KernelCode]>;
710
711 // If we have small model and -static mode, it is safe to store global addresses
712 // directly as immediates.  FIXME: This is really a hack, the 'imm' predicate
713 // for MOV64mi32 should handle this sort of thing.
714 def : Pat<(store (i64 (X86Wrapper tconstpool:$src)), addr:$dst),
715           (MOV64mi32 addr:$dst, tconstpool:$src)>,
716           Requires<[NearData, IsStatic]>;
717 def : Pat<(store (i64 (X86Wrapper tjumptable:$src)), addr:$dst),
718           (MOV64mi32 addr:$dst, tjumptable:$src)>,
719           Requires<[NearData, IsStatic]>;
720 def : Pat<(store (i64 (X86Wrapper tglobaladdr:$src)), addr:$dst),
721           (MOV64mi32 addr:$dst, tglobaladdr:$src)>,
722           Requires<[NearData, IsStatic]>;
723 def : Pat<(store (i64 (X86Wrapper texternalsym:$src)), addr:$dst),
724           (MOV64mi32 addr:$dst, texternalsym:$src)>,
725           Requires<[NearData, IsStatic]>;
726 def : Pat<(store (i64 (X86Wrapper tblockaddress:$src)), addr:$dst),
727           (MOV64mi32 addr:$dst, tblockaddress:$src)>,
728           Requires<[NearData, IsStatic]>;
729
730
731
732 // Calls
733
734 // tls has some funny stuff here...
735 // This corresponds to movabs $foo@tpoff, %rax
736 def : Pat<(i64 (X86Wrapper tglobaltlsaddr :$dst)),
737           (MOV64ri tglobaltlsaddr :$dst)>;
738 // This corresponds to add $foo@tpoff, %rax
739 def : Pat<(add GR64:$src1, (X86Wrapper tglobaltlsaddr :$dst)),
740           (ADD64ri32 GR64:$src1, tglobaltlsaddr :$dst)>;
741 // This corresponds to mov foo@tpoff(%rbx), %eax
742 def : Pat<(load (i64 (X86Wrapper tglobaltlsaddr :$dst))),
743           (MOV64rm tglobaltlsaddr :$dst)>;
744
745
746 // Direct PC relative function call for small code model. 32-bit displacement
747 // sign extended to 64-bit.
748 def : Pat<(X86call (i64 tglobaladdr:$dst)),
749           (CALL64pcrel32 tglobaladdr:$dst)>, Requires<[NotWin64]>;
750 def : Pat<(X86call (i64 texternalsym:$dst)),
751           (CALL64pcrel32 texternalsym:$dst)>, Requires<[NotWin64]>;
752
753 def : Pat<(X86call (i64 tglobaladdr:$dst)),
754           (WINCALL64pcrel32 tglobaladdr:$dst)>, Requires<[IsWin64]>;
755 def : Pat<(X86call (i64 texternalsym:$dst)),
756           (WINCALL64pcrel32 texternalsym:$dst)>, Requires<[IsWin64]>;
757
758 // tailcall stuff
759 def : Pat<(X86tcret GR32_TC:$dst, imm:$off),
760           (TCRETURNri GR32_TC:$dst, imm:$off)>,
761           Requires<[In32BitMode]>;
762
763 // FIXME: This is disabled for 32-bit PIC mode because the global base
764 // register which is part of the address mode may be assigned a 
765 // callee-saved register.
766 def : Pat<(X86tcret (load addr:$dst), imm:$off),
767           (TCRETURNmi addr:$dst, imm:$off)>,
768           Requires<[In32BitMode, IsNotPIC]>;
769
770 def : Pat<(X86tcret (i32 tglobaladdr:$dst), imm:$off),
771           (TCRETURNdi texternalsym:$dst, imm:$off)>,
772           Requires<[In32BitMode]>;
773
774 def : Pat<(X86tcret (i32 texternalsym:$dst), imm:$off),
775           (TCRETURNdi texternalsym:$dst, imm:$off)>,
776           Requires<[In32BitMode]>;
777
778 def : Pat<(X86tcret GR64_TC:$dst, imm:$off),
779           (TCRETURNri64 GR64_TC:$dst, imm:$off)>,
780           Requires<[In64BitMode]>;
781
782 def : Pat<(X86tcret (load addr:$dst), imm:$off),
783           (TCRETURNmi64 addr:$dst, imm:$off)>,
784           Requires<[In64BitMode]>;
785
786 def : Pat<(X86tcret (i64 tglobaladdr:$dst), imm:$off),
787           (TCRETURNdi64 tglobaladdr:$dst, imm:$off)>,
788           Requires<[In64BitMode]>;
789
790 def : Pat<(X86tcret (i64 texternalsym:$dst), imm:$off),
791           (TCRETURNdi64 texternalsym:$dst, imm:$off)>,
792           Requires<[In64BitMode]>;
793
794 // Normal calls, with various flavors of addresses.
795 def : Pat<(X86call (i32 tglobaladdr:$dst)),
796           (CALLpcrel32 tglobaladdr:$dst)>;
797 def : Pat<(X86call (i32 texternalsym:$dst)),
798           (CALLpcrel32 texternalsym:$dst)>;
799 def : Pat<(X86call (i32 imm:$dst)),
800           (CALLpcrel32 imm:$dst)>, Requires<[CallImmAddr]>;
801
802 // X86 specific add which produces a flag.
803 def : Pat<(addc GR32:$src1, GR32:$src2),
804           (ADD32rr GR32:$src1, GR32:$src2)>;
805 def : Pat<(addc GR32:$src1, (load addr:$src2)),
806           (ADD32rm GR32:$src1, addr:$src2)>;
807 def : Pat<(addc GR32:$src1, imm:$src2),
808           (ADD32ri GR32:$src1, imm:$src2)>;
809 def : Pat<(addc GR32:$src1, i32immSExt8:$src2),
810           (ADD32ri8 GR32:$src1, i32immSExt8:$src2)>;
811
812 def : Pat<(addc GR64:$src1, GR64:$src2),
813           (ADD64rr GR64:$src1, GR64:$src2)>;
814 def : Pat<(addc GR64:$src1, (load addr:$src2)),
815           (ADD64rm GR64:$src1, addr:$src2)>;
816 def : Pat<(addc GR64:$src1, i64immSExt8:$src2),
817           (ADD64ri8 GR64:$src1, i64immSExt8:$src2)>;
818 def : Pat<(addc GR64:$src1, i64immSExt32:$src2),
819           (ADD64ri32 GR64:$src1, imm:$src2)>;
820
821 def : Pat<(subc GR32:$src1, GR32:$src2),
822           (SUB32rr GR32:$src1, GR32:$src2)>;
823 def : Pat<(subc GR32:$src1, (load addr:$src2)),
824           (SUB32rm GR32:$src1, addr:$src2)>;
825 def : Pat<(subc GR32:$src1, imm:$src2),
826           (SUB32ri GR32:$src1, imm:$src2)>;
827 def : Pat<(subc GR32:$src1, i32immSExt8:$src2),
828           (SUB32ri8 GR32:$src1, i32immSExt8:$src2)>;
829
830 def : Pat<(subc GR64:$src1, GR64:$src2),
831           (SUB64rr GR64:$src1, GR64:$src2)>;
832 def : Pat<(subc GR64:$src1, (load addr:$src2)),
833           (SUB64rm GR64:$src1, addr:$src2)>;
834 def : Pat<(subc GR64:$src1, i64immSExt8:$src2),
835           (SUB64ri8 GR64:$src1, i64immSExt8:$src2)>;
836 def : Pat<(subc GR64:$src1, imm:$src2),
837           (SUB64ri32 GR64:$src1, i64immSExt32:$src2)>;
838
839 // Comparisons.
840
841 // TEST R,R is smaller than CMP R,0
842 def : Pat<(X86cmp GR8:$src1, 0),
843           (TEST8rr GR8:$src1, GR8:$src1)>;
844 def : Pat<(X86cmp GR16:$src1, 0),
845           (TEST16rr GR16:$src1, GR16:$src1)>;
846 def : Pat<(X86cmp GR32:$src1, 0),
847           (TEST32rr GR32:$src1, GR32:$src1)>;
848 def : Pat<(X86cmp GR64:$src1, 0),
849           (TEST64rr GR64:$src1, GR64:$src1)>;
850
851 // Conditional moves with folded loads with operands swapped and conditions
852 // inverted.
853 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_B, EFLAGS),
854           (CMOVAE16rm GR16:$src2, addr:$src1)>;
855 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_B, EFLAGS),
856           (CMOVAE32rm GR32:$src2, addr:$src1)>;
857 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_AE, EFLAGS),
858           (CMOVB16rm GR16:$src2, addr:$src1)>;
859 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_AE, EFLAGS),
860           (CMOVB32rm GR32:$src2, addr:$src1)>;
861 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_E, EFLAGS),
862           (CMOVNE16rm GR16:$src2, addr:$src1)>;
863 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_E, EFLAGS),
864           (CMOVNE32rm GR32:$src2, addr:$src1)>;
865 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_NE, EFLAGS),
866           (CMOVE16rm GR16:$src2, addr:$src1)>;
867 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_NE, EFLAGS),
868           (CMOVE32rm GR32:$src2, addr:$src1)>;
869 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_BE, EFLAGS),
870           (CMOVA16rm GR16:$src2, addr:$src1)>;
871 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_BE, EFLAGS),
872           (CMOVA32rm GR32:$src2, addr:$src1)>;
873 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_A, EFLAGS),
874           (CMOVBErm16 GR16:$src2, addr:$src1)>;
875 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_A, EFLAGS),
876           (CMOVBErm32 GR32:$src2, addr:$src1)>;
877 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_L, EFLAGS),
878           (CMOVGE16rm GR16:$src2, addr:$src1)>;
879 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_L, EFLAGS),
880           (CMOVGE32rm GR32:$src2, addr:$src1)>;
881 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_GE, EFLAGS),
882           (CMOVL16rm GR16:$src2, addr:$src1)>;
883 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_GE, EFLAGS),
884           (CMOVL32rm GR32:$src2, addr:$src1)>;
885 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_LE, EFLAGS),
886           (CMOVG16rm GR16:$src2, addr:$src1)>;
887 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_LE, EFLAGS),
888           (CMOVG32rm GR32:$src2, addr:$src1)>;
889 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_G, EFLAGS),
890           (CMOVLE16rm GR16:$src2, addr:$src1)>;
891 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_G, EFLAGS),
892           (CMOVLE32rm GR32:$src2, addr:$src1)>;
893 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_P, EFLAGS),
894           (CMOVNP16rm GR16:$src2, addr:$src1)>;
895 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_P, EFLAGS),
896           (CMOVNP32rm GR32:$src2, addr:$src1)>;
897 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_NP, EFLAGS),
898           (CMOVP16rm GR16:$src2, addr:$src1)>;
899 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_NP, EFLAGS),
900           (CMOVP32rm GR32:$src2, addr:$src1)>;
901 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_S, EFLAGS),
902           (CMOVNS16rm GR16:$src2, addr:$src1)>;
903 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_S, EFLAGS),
904           (CMOVNS32rm GR32:$src2, addr:$src1)>;
905 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_NS, EFLAGS),
906           (CMOVS16rm GR16:$src2, addr:$src1)>;
907 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_NS, EFLAGS),
908           (CMOVS32rm GR32:$src2, addr:$src1)>;
909 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_O, EFLAGS),
910           (CMOVNO16rm GR16:$src2, addr:$src1)>;
911 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_O, EFLAGS),
912           (CMOVNO32rm GR32:$src2, addr:$src1)>;
913 def : Pat<(X86cmov (loadi16 addr:$src1), GR16:$src2, X86_COND_NO, EFLAGS),
914           (CMOVO16rm GR16:$src2, addr:$src1)>;
915 def : Pat<(X86cmov (loadi32 addr:$src1), GR32:$src2, X86_COND_NO, EFLAGS),
916           (CMOVO32rm GR32:$src2, addr:$src1)>;
917
918 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_B, EFLAGS),
919           (CMOVAE64rm GR64:$src2, addr:$src1)>;
920 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_AE, EFLAGS),
921           (CMOVB64rm GR64:$src2, addr:$src1)>;
922 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_E, EFLAGS),
923           (CMOVNE64rm GR64:$src2, addr:$src1)>;
924 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_NE, EFLAGS),
925           (CMOVE64rm GR64:$src2, addr:$src1)>;
926 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_BE, EFLAGS),
927           (CMOVA64rm GR64:$src2, addr:$src1)>;
928 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_A, EFLAGS),
929           (CMOVBErm64 GR64:$src2, addr:$src1)>;
930 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_L, EFLAGS),
931           (CMOVGE64rm GR64:$src2, addr:$src1)>;
932 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_GE, EFLAGS),
933           (CMOVL64rm GR64:$src2, addr:$src1)>;
934 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_LE, EFLAGS),
935           (CMOVG64rm GR64:$src2, addr:$src1)>;
936 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_G, EFLAGS),
937           (CMOVLE64rm GR64:$src2, addr:$src1)>;
938 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_P, EFLAGS),
939           (CMOVNP64rm GR64:$src2, addr:$src1)>;
940 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_NP, EFLAGS),
941           (CMOVP64rm GR64:$src2, addr:$src1)>;
942 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_S, EFLAGS),
943           (CMOVNS64rm GR64:$src2, addr:$src1)>;
944 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_NS, EFLAGS),
945           (CMOVS64rm GR64:$src2, addr:$src1)>;
946 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_O, EFLAGS),
947           (CMOVNO64rm GR64:$src2, addr:$src1)>;
948 def : Pat<(X86cmov (loadi64 addr:$src1), GR64:$src2, X86_COND_NO, EFLAGS),
949           (CMOVO64rm GR64:$src2, addr:$src1)>;
950
951
952 // zextload bool -> zextload byte
953 def : Pat<(zextloadi8i1  addr:$src), (MOV8rm     addr:$src)>;
954 def : Pat<(zextloadi16i1 addr:$src), (MOVZX16rm8 addr:$src)>;
955 def : Pat<(zextloadi32i1 addr:$src), (MOVZX32rm8 addr:$src)>;
956 def : Pat<(zextloadi64i1 addr:$src), (MOVZX64rm8 addr:$src)>;
957
958 // extload bool -> extload byte
959 // When extloading from 16-bit and smaller memory locations into 64-bit 
960 // registers, use zero-extending loads so that the entire 64-bit register is 
961 // defined, avoiding partial-register updates.
962
963 def : Pat<(extloadi8i1 addr:$src),   (MOV8rm      addr:$src)>;
964 def : Pat<(extloadi16i1 addr:$src),  (MOVZX16rm8  addr:$src)>;
965 def : Pat<(extloadi32i1 addr:$src),  (MOVZX32rm8  addr:$src)>;
966 def : Pat<(extloadi16i8 addr:$src),  (MOVZX16rm8  addr:$src)>;
967 def : Pat<(extloadi32i8 addr:$src),  (MOVZX32rm8  addr:$src)>;
968 def : Pat<(extloadi32i16 addr:$src), (MOVZX32rm16 addr:$src)>;
969
970 def : Pat<(extloadi64i1 addr:$src),  (MOVZX64rm8  addr:$src)>;
971 def : Pat<(extloadi64i8 addr:$src),  (MOVZX64rm8  addr:$src)>;
972 def : Pat<(extloadi64i16 addr:$src), (MOVZX64rm16 addr:$src)>;
973 // For other extloads, use subregs, since the high contents of the register are
974 // defined after an extload.
975 def : Pat<(extloadi64i32 addr:$src),
976           (SUBREG_TO_REG (i64 0), (MOV32rm addr:$src),
977                          sub_32bit)>;
978
979 // anyext. Define these to do an explicit zero-extend to
980 // avoid partial-register updates.
981 def : Pat<(i16 (anyext GR8 :$src)), (MOVZX16rr8  GR8 :$src)>;
982 def : Pat<(i32 (anyext GR8 :$src)), (MOVZX32rr8  GR8 :$src)>;
983
984 // Except for i16 -> i32 since isel expect i16 ops to be promoted to i32.
985 def : Pat<(i32 (anyext GR16:$src)),
986           (INSERT_SUBREG (i32 (IMPLICIT_DEF)), GR16:$src, sub_16bit)>;
987
988 def : Pat<(i64 (anyext GR8 :$src)), (MOVZX64rr8  GR8  :$src)>;
989 def : Pat<(i64 (anyext GR16:$src)), (MOVZX64rr16 GR16 :$src)>;
990 def : Pat<(i64 (anyext GR32:$src)),
991           (SUBREG_TO_REG (i64 0), GR32:$src, sub_32bit)>;
992
993
994 // Any instruction that defines a 32-bit result leaves the high half of the
995 // register. Truncate can be lowered to EXTRACT_SUBREG. CopyFromReg may
996 // be copying from a truncate. And x86's cmov doesn't do anything if the
997 // condition is false. But any other 32-bit operation will zero-extend
998 // up to 64 bits.
999 def def32 : PatLeaf<(i32 GR32:$src), [{
1000   return N->getOpcode() != ISD::TRUNCATE &&
1001          N->getOpcode() != TargetOpcode::EXTRACT_SUBREG &&
1002          N->getOpcode() != ISD::CopyFromReg &&
1003          N->getOpcode() != X86ISD::CMOV;
1004 }]>;
1005
1006 // In the case of a 32-bit def that is known to implicitly zero-extend,
1007 // we can use a SUBREG_TO_REG.
1008 def : Pat<(i64 (zext def32:$src)),
1009           (SUBREG_TO_REG (i64 0), GR32:$src, sub_32bit)>;
1010
1011 //===----------------------------------------------------------------------===//
1012 // Some peepholes
1013 //===----------------------------------------------------------------------===//
1014
1015 // Odd encoding trick: -128 fits into an 8-bit immediate field while
1016 // +128 doesn't, so in this special case use a sub instead of an add.
1017 def : Pat<(add GR16:$src1, 128),
1018           (SUB16ri8 GR16:$src1, -128)>;
1019 def : Pat<(store (add (loadi16 addr:$dst), 128), addr:$dst),
1020           (SUB16mi8 addr:$dst, -128)>;
1021
1022 def : Pat<(add GR32:$src1, 128),
1023           (SUB32ri8 GR32:$src1, -128)>;
1024 def : Pat<(store (add (loadi32 addr:$dst), 128), addr:$dst),
1025           (SUB32mi8 addr:$dst, -128)>;
1026
1027 def : Pat<(add GR64:$src1, 128),
1028           (SUB64ri8 GR64:$src1, -128)>;
1029 def : Pat<(store (add (loadi64 addr:$dst), 128), addr:$dst),
1030           (SUB64mi8 addr:$dst, -128)>;
1031
1032 // The same trick applies for 32-bit immediate fields in 64-bit
1033 // instructions.
1034 def : Pat<(add GR64:$src1, 0x0000000080000000),
1035           (SUB64ri32 GR64:$src1, 0xffffffff80000000)>;
1036 def : Pat<(store (add (loadi64 addr:$dst), 0x00000000800000000), addr:$dst),
1037           (SUB64mi32 addr:$dst, 0xffffffff80000000)>;
1038
1039 // Use a 32-bit and with implicit zero-extension instead of a 64-bit and if it
1040 // has an immediate with at least 32 bits of leading zeros, to avoid needing to
1041 // materialize that immediate in a register first.
1042 def : Pat<(and GR64:$src, i64immZExt32:$imm),
1043           (SUBREG_TO_REG
1044             (i64 0),
1045             (AND32ri
1046               (EXTRACT_SUBREG GR64:$src, sub_32bit),
1047               (i32 (GetLo32XForm imm:$imm))),
1048             sub_32bit)>;
1049
1050
1051 // r & (2^16-1) ==> movz
1052 def : Pat<(and GR32:$src1, 0xffff),
1053           (MOVZX32rr16 (EXTRACT_SUBREG GR32:$src1, sub_16bit))>;
1054 // r & (2^8-1) ==> movz
1055 def : Pat<(and GR32:$src1, 0xff),
1056           (MOVZX32rr8 (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src1, 
1057                                                              GR32_ABCD)),
1058                                       sub_8bit))>,
1059       Requires<[In32BitMode]>;
1060 // r & (2^8-1) ==> movz
1061 def : Pat<(and GR16:$src1, 0xff),
1062           (MOVZX16rr8 (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src1, 
1063                                                              GR16_ABCD)),
1064                                       sub_8bit))>,
1065       Requires<[In32BitMode]>;
1066
1067 // r & (2^32-1) ==> movz
1068 def : Pat<(and GR64:$src, 0x00000000FFFFFFFF),
1069           (MOVZX64rr32 (EXTRACT_SUBREG GR64:$src, sub_32bit))>;
1070 // r & (2^16-1) ==> movz
1071 def : Pat<(and GR64:$src, 0xffff),
1072           (MOVZX64rr16 (i16 (EXTRACT_SUBREG GR64:$src, sub_16bit)))>;
1073 // r & (2^8-1) ==> movz
1074 def : Pat<(and GR64:$src, 0xff),
1075           (MOVZX64rr8 (i8 (EXTRACT_SUBREG GR64:$src, sub_8bit)))>;
1076 // r & (2^8-1) ==> movz
1077 def : Pat<(and GR32:$src1, 0xff),
1078            (MOVZX32rr8 (EXTRACT_SUBREG GR32:$src1, sub_8bit))>,
1079       Requires<[In64BitMode]>;
1080 // r & (2^8-1) ==> movz
1081 def : Pat<(and GR16:$src1, 0xff),
1082            (MOVZX16rr8 (i8 (EXTRACT_SUBREG GR16:$src1, sub_8bit)))>,
1083       Requires<[In64BitMode]>;
1084
1085
1086 // sext_inreg patterns
1087 def : Pat<(sext_inreg GR32:$src, i16),
1088           (MOVSX32rr16 (EXTRACT_SUBREG GR32:$src, sub_16bit))>;
1089 def : Pat<(sext_inreg GR32:$src, i8),
1090           (MOVSX32rr8 (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, 
1091                                                              GR32_ABCD)),
1092                                       sub_8bit))>,
1093       Requires<[In32BitMode]>;
1094 def : Pat<(sext_inreg GR16:$src, i8),
1095           (MOVSX16rr8 (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, 
1096                                                              GR16_ABCD)),
1097                                       sub_8bit))>,
1098       Requires<[In32BitMode]>;
1099
1100 def : Pat<(sext_inreg GR64:$src, i32),
1101           (MOVSX64rr32 (EXTRACT_SUBREG GR64:$src, sub_32bit))>;
1102 def : Pat<(sext_inreg GR64:$src, i16),
1103           (MOVSX64rr16 (EXTRACT_SUBREG GR64:$src, sub_16bit))>;
1104 def : Pat<(sext_inreg GR64:$src, i8),
1105           (MOVSX64rr8 (EXTRACT_SUBREG GR64:$src, sub_8bit))>;
1106 def : Pat<(sext_inreg GR32:$src, i8),
1107           (MOVSX32rr8 (EXTRACT_SUBREG GR32:$src, sub_8bit))>,
1108       Requires<[In64BitMode]>;
1109 def : Pat<(sext_inreg GR16:$src, i8),
1110           (MOVSX16rr8 (i8 (EXTRACT_SUBREG GR16:$src, sub_8bit)))>,
1111       Requires<[In64BitMode]>;
1112
1113
1114 // trunc patterns
1115 def : Pat<(i16 (trunc GR32:$src)),
1116           (EXTRACT_SUBREG GR32:$src, sub_16bit)>;
1117 def : Pat<(i8 (trunc GR32:$src)),
1118           (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, GR32_ABCD)),
1119                           sub_8bit)>,
1120       Requires<[In32BitMode]>;
1121 def : Pat<(i8 (trunc GR16:$src)),
1122           (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
1123                           sub_8bit)>,
1124       Requires<[In32BitMode]>;
1125 def : Pat<(i32 (trunc GR64:$src)),
1126           (EXTRACT_SUBREG GR64:$src, sub_32bit)>;
1127 def : Pat<(i16 (trunc GR64:$src)),
1128           (EXTRACT_SUBREG GR64:$src, sub_16bit)>;
1129 def : Pat<(i8 (trunc GR64:$src)),
1130           (EXTRACT_SUBREG GR64:$src, sub_8bit)>;
1131 def : Pat<(i8 (trunc GR32:$src)),
1132           (EXTRACT_SUBREG GR32:$src, sub_8bit)>,
1133       Requires<[In64BitMode]>;
1134 def : Pat<(i8 (trunc GR16:$src)),
1135           (EXTRACT_SUBREG GR16:$src, sub_8bit)>,
1136       Requires<[In64BitMode]>;
1137
1138 // h-register tricks
1139 def : Pat<(i8 (trunc (srl_su GR16:$src, (i8 8)))),
1140           (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
1141                           sub_8bit_hi)>,
1142       Requires<[In32BitMode]>;
1143 def : Pat<(i8 (trunc (srl_su GR32:$src, (i8 8)))),
1144           (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, GR32_ABCD)),
1145                           sub_8bit_hi)>,
1146       Requires<[In32BitMode]>;
1147 def : Pat<(srl GR16:$src, (i8 8)),
1148           (EXTRACT_SUBREG
1149             (MOVZX32rr8
1150               (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
1151                               sub_8bit_hi)),
1152             sub_16bit)>,
1153       Requires<[In32BitMode]>;
1154 def : Pat<(i32 (zext (srl_su GR16:$src, (i8 8)))),
1155           (MOVZX32rr8 (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, 
1156                                                              GR16_ABCD)),
1157                                       sub_8bit_hi))>,
1158       Requires<[In32BitMode]>;
1159 def : Pat<(i32 (anyext (srl_su GR16:$src, (i8 8)))),
1160           (MOVZX32rr8 (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, 
1161                                                              GR16_ABCD)),
1162                                       sub_8bit_hi))>,
1163       Requires<[In32BitMode]>;
1164 def : Pat<(and (srl_su GR32:$src, (i8 8)), (i32 255)),
1165           (MOVZX32rr8 (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, 
1166                                                              GR32_ABCD)),
1167                                       sub_8bit_hi))>,
1168       Requires<[In32BitMode]>;
1169 def : Pat<(srl (and_su GR32:$src, 0xff00), (i8 8)),
1170           (MOVZX32rr8 (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, 
1171                                                              GR32_ABCD)),
1172                                       sub_8bit_hi))>,
1173       Requires<[In32BitMode]>;
1174       
1175 // h-register tricks.
1176 // For now, be conservative on x86-64 and use an h-register extract only if the
1177 // value is immediately zero-extended or stored, which are somewhat common
1178 // cases. This uses a bunch of code to prevent a register requiring a REX prefix
1179 // from being allocated in the same instruction as the h register, as there's
1180 // currently no way to describe this requirement to the register allocator.
1181
1182 // h-register extract and zero-extend.
1183 def : Pat<(and (srl_su GR64:$src, (i8 8)), (i64 255)),
1184           (SUBREG_TO_REG
1185             (i64 0),
1186             (MOVZX32_NOREXrr8
1187               (EXTRACT_SUBREG (i64 (COPY_TO_REGCLASS GR64:$src, GR64_ABCD)),
1188                               sub_8bit_hi)),
1189             sub_32bit)>;
1190 def : Pat<(and (srl_su GR32:$src, (i8 8)), (i32 255)),
1191           (MOVZX32_NOREXrr8
1192             (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, GR32_ABCD)),
1193                             sub_8bit_hi))>,
1194       Requires<[In64BitMode]>;
1195 def : Pat<(srl (and_su GR32:$src, 0xff00), (i8 8)),
1196           (MOVZX32_NOREXrr8 (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, 
1197                                                                    GR32_ABCD)),
1198                                              sub_8bit_hi))>,
1199       Requires<[In64BitMode]>;
1200 def : Pat<(srl GR16:$src, (i8 8)),
1201           (EXTRACT_SUBREG
1202             (MOVZX32_NOREXrr8
1203               (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
1204                               sub_8bit_hi)),
1205             sub_16bit)>,
1206       Requires<[In64BitMode]>;
1207 def : Pat<(i32 (zext (srl_su GR16:$src, (i8 8)))),
1208           (MOVZX32_NOREXrr8
1209             (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
1210                             sub_8bit_hi))>,
1211       Requires<[In64BitMode]>;
1212 def : Pat<(i32 (anyext (srl_su GR16:$src, (i8 8)))),
1213           (MOVZX32_NOREXrr8
1214             (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
1215                             sub_8bit_hi))>,
1216       Requires<[In64BitMode]>;
1217 def : Pat<(i64 (zext (srl_su GR16:$src, (i8 8)))),
1218           (SUBREG_TO_REG
1219             (i64 0),
1220             (MOVZX32_NOREXrr8
1221               (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
1222                               sub_8bit_hi)),
1223             sub_32bit)>;
1224 def : Pat<(i64 (anyext (srl_su GR16:$src, (i8 8)))),
1225           (SUBREG_TO_REG
1226             (i64 0),
1227             (MOVZX32_NOREXrr8
1228               (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
1229                               sub_8bit_hi)),
1230             sub_32bit)>;
1231
1232 // h-register extract and store.
1233 def : Pat<(store (i8 (trunc_su (srl_su GR64:$src, (i8 8)))), addr:$dst),
1234           (MOV8mr_NOREX
1235             addr:$dst,
1236             (EXTRACT_SUBREG (i64 (COPY_TO_REGCLASS GR64:$src, GR64_ABCD)),
1237                             sub_8bit_hi))>;
1238 def : Pat<(store (i8 (trunc_su (srl_su GR32:$src, (i8 8)))), addr:$dst),
1239           (MOV8mr_NOREX
1240             addr:$dst,
1241             (EXTRACT_SUBREG (i32 (COPY_TO_REGCLASS GR32:$src, GR32_ABCD)),
1242                             sub_8bit_hi))>,
1243       Requires<[In64BitMode]>;
1244 def : Pat<(store (i8 (trunc_su (srl_su GR16:$src, (i8 8)))), addr:$dst),
1245           (MOV8mr_NOREX
1246             addr:$dst,
1247             (EXTRACT_SUBREG (i16 (COPY_TO_REGCLASS GR16:$src, GR16_ABCD)),
1248                             sub_8bit_hi))>,
1249       Requires<[In64BitMode]>;
1250       
1251       
1252 // (shl x, 1) ==> (add x, x)
1253 def : Pat<(shl GR8 :$src1, (i8 1)), (ADD8rr  GR8 :$src1, GR8 :$src1)>;
1254 def : Pat<(shl GR16:$src1, (i8 1)), (ADD16rr GR16:$src1, GR16:$src1)>;
1255 def : Pat<(shl GR32:$src1, (i8 1)), (ADD32rr GR32:$src1, GR32:$src1)>;
1256 def : Pat<(shl GR64:$src1, (i8 1)), (ADD64rr GR64:$src1, GR64:$src1)>;
1257
1258 // (shl x (and y, 31)) ==> (shl x, y)
1259 def : Pat<(shl GR8:$src1, (and CL, 31)),
1260           (SHL8rCL GR8:$src1)>;
1261 def : Pat<(shl GR16:$src1, (and CL, 31)),
1262           (SHL16rCL GR16:$src1)>;
1263 def : Pat<(shl GR32:$src1, (and CL, 31)),
1264           (SHL32rCL GR32:$src1)>;
1265 def : Pat<(store (shl (loadi8 addr:$dst), (and CL, 31)), addr:$dst),
1266           (SHL8mCL addr:$dst)>;
1267 def : Pat<(store (shl (loadi16 addr:$dst), (and CL, 31)), addr:$dst),
1268           (SHL16mCL addr:$dst)>;
1269 def : Pat<(store (shl (loadi32 addr:$dst), (and CL, 31)), addr:$dst),
1270           (SHL32mCL addr:$dst)>;
1271
1272 def : Pat<(srl GR8:$src1, (and CL, 31)),
1273           (SHR8rCL GR8:$src1)>;
1274 def : Pat<(srl GR16:$src1, (and CL, 31)),
1275           (SHR16rCL GR16:$src1)>;
1276 def : Pat<(srl GR32:$src1, (and CL, 31)),
1277           (SHR32rCL GR32:$src1)>;
1278 def : Pat<(store (srl (loadi8 addr:$dst), (and CL, 31)), addr:$dst),
1279           (SHR8mCL addr:$dst)>;
1280 def : Pat<(store (srl (loadi16 addr:$dst), (and CL, 31)), addr:$dst),
1281           (SHR16mCL addr:$dst)>;
1282 def : Pat<(store (srl (loadi32 addr:$dst), (and CL, 31)), addr:$dst),
1283           (SHR32mCL addr:$dst)>;
1284
1285 def : Pat<(sra GR8:$src1, (and CL, 31)),
1286           (SAR8rCL GR8:$src1)>;
1287 def : Pat<(sra GR16:$src1, (and CL, 31)),
1288           (SAR16rCL GR16:$src1)>;
1289 def : Pat<(sra GR32:$src1, (and CL, 31)),
1290           (SAR32rCL GR32:$src1)>;
1291 def : Pat<(store (sra (loadi8 addr:$dst), (and CL, 31)), addr:$dst),
1292           (SAR8mCL addr:$dst)>;
1293 def : Pat<(store (sra (loadi16 addr:$dst), (and CL, 31)), addr:$dst),
1294           (SAR16mCL addr:$dst)>;
1295 def : Pat<(store (sra (loadi32 addr:$dst), (and CL, 31)), addr:$dst),
1296           (SAR32mCL addr:$dst)>;
1297
1298 // (shl x (and y, 63)) ==> (shl x, y)
1299 def : Pat<(shl GR64:$src1, (and CL, 63)),
1300           (SHL64rCL GR64:$src1)>;
1301 def : Pat<(store (shl (loadi64 addr:$dst), (and CL, 63)), addr:$dst),
1302           (SHL64mCL addr:$dst)>;
1303
1304 def : Pat<(srl GR64:$src1, (and CL, 63)),
1305           (SHR64rCL GR64:$src1)>;
1306 def : Pat<(store (srl (loadi64 addr:$dst), (and CL, 63)), addr:$dst),
1307           (SHR64mCL addr:$dst)>;
1308
1309 def : Pat<(sra GR64:$src1, (and CL, 63)),
1310           (SAR64rCL GR64:$src1)>;
1311 def : Pat<(store (sra (loadi64 addr:$dst), (and CL, 63)), addr:$dst),
1312           (SAR64mCL addr:$dst)>;
1313
1314
1315 // (anyext (setcc_carry)) -> (setcc_carry)
1316 def : Pat<(i16 (anyext (i8 (X86setcc_c X86_COND_B, EFLAGS)))),
1317           (SETB_C16r)>;
1318 def : Pat<(i32 (anyext (i8 (X86setcc_c X86_COND_B, EFLAGS)))),
1319           (SETB_C32r)>;
1320 def : Pat<(i32 (anyext (i16 (X86setcc_c X86_COND_B, EFLAGS)))),
1321           (SETB_C32r)>;
1322
1323 // (or x1, x2) -> (add x1, x2) if two operands are known not to share bits.
1324 let AddedComplexity = 5 in { // Try this before the selecting to OR
1325 def : Pat<(or_is_add GR16:$src1, imm:$src2),
1326           (ADD16ri GR16:$src1, imm:$src2)>;
1327 def : Pat<(or_is_add GR32:$src1, imm:$src2),
1328           (ADD32ri GR32:$src1, imm:$src2)>;
1329 def : Pat<(or_is_add GR16:$src1, i16immSExt8:$src2),
1330           (ADD16ri8 GR16:$src1, i16immSExt8:$src2)>;
1331 def : Pat<(or_is_add GR32:$src1, i32immSExt8:$src2),
1332           (ADD32ri8 GR32:$src1, i32immSExt8:$src2)>;
1333 def : Pat<(or_is_add GR16:$src1, GR16:$src2),
1334           (ADD16rr GR16:$src1, GR16:$src2)>;
1335 def : Pat<(or_is_add GR32:$src1, GR32:$src2),
1336           (ADD32rr GR32:$src1, GR32:$src2)>;
1337 def : Pat<(or_is_add GR64:$src1, i64immSExt8:$src2),
1338           (ADD64ri8 GR64:$src1, i64immSExt8:$src2)>;
1339 def : Pat<(or_is_add GR64:$src1, i64immSExt32:$src2),
1340           (ADD64ri32 GR64:$src1, i64immSExt32:$src2)>;
1341 def : Pat<(or_is_add GR64:$src1, GR64:$src2),
1342           (ADD64rr GR64:$src1, GR64:$src2)>;
1343 } // AddedComplexity
1344
1345 //===----------------------------------------------------------------------===//
1346 // EFLAGS-defining Patterns
1347 //===----------------------------------------------------------------------===//
1348
1349 // add reg, reg
1350 def : Pat<(add GR8 :$src1, GR8 :$src2), (ADD8rr  GR8 :$src1, GR8 :$src2)>;
1351 def : Pat<(add GR16:$src1, GR16:$src2), (ADD16rr GR16:$src1, GR16:$src2)>;
1352 def : Pat<(add GR32:$src1, GR32:$src2), (ADD32rr GR32:$src1, GR32:$src2)>;
1353
1354 // add reg, mem
1355 def : Pat<(add GR8:$src1, (loadi8 addr:$src2)),
1356           (ADD8rm GR8:$src1, addr:$src2)>;
1357 def : Pat<(add GR16:$src1, (loadi16 addr:$src2)),
1358           (ADD16rm GR16:$src1, addr:$src2)>;
1359 def : Pat<(add GR32:$src1, (loadi32 addr:$src2)),
1360           (ADD32rm GR32:$src1, addr:$src2)>;
1361
1362 // add reg, imm
1363 def : Pat<(add GR8 :$src1, imm:$src2), (ADD8ri  GR8:$src1 , imm:$src2)>;
1364 def : Pat<(add GR16:$src1, imm:$src2), (ADD16ri GR16:$src1, imm:$src2)>;
1365 def : Pat<(add GR32:$src1, imm:$src2), (ADD32ri GR32:$src1, imm:$src2)>;
1366 def : Pat<(add GR16:$src1, i16immSExt8:$src2),
1367           (ADD16ri8 GR16:$src1, i16immSExt8:$src2)>;
1368 def : Pat<(add GR32:$src1, i32immSExt8:$src2),
1369           (ADD32ri8 GR32:$src1, i32immSExt8:$src2)>;
1370
1371 // sub reg, reg
1372 def : Pat<(sub GR8 :$src1, GR8 :$src2), (SUB8rr  GR8 :$src1, GR8 :$src2)>;
1373 def : Pat<(sub GR16:$src1, GR16:$src2), (SUB16rr GR16:$src1, GR16:$src2)>;
1374 def : Pat<(sub GR32:$src1, GR32:$src2), (SUB32rr GR32:$src1, GR32:$src2)>;
1375
1376 // sub reg, mem
1377 def : Pat<(sub GR8:$src1, (loadi8 addr:$src2)),
1378           (SUB8rm GR8:$src1, addr:$src2)>;
1379 def : Pat<(sub GR16:$src1, (loadi16 addr:$src2)),
1380           (SUB16rm GR16:$src1, addr:$src2)>;
1381 def : Pat<(sub GR32:$src1, (loadi32 addr:$src2)),
1382           (SUB32rm GR32:$src1, addr:$src2)>;
1383
1384 // sub reg, imm
1385 def : Pat<(sub GR8:$src1, imm:$src2),
1386           (SUB8ri GR8:$src1, imm:$src2)>;
1387 def : Pat<(sub GR16:$src1, imm:$src2),
1388           (SUB16ri GR16:$src1, imm:$src2)>;
1389 def : Pat<(sub GR32:$src1, imm:$src2),
1390           (SUB32ri GR32:$src1, imm:$src2)>;
1391 def : Pat<(sub GR16:$src1, i16immSExt8:$src2),
1392           (SUB16ri8 GR16:$src1, i16immSExt8:$src2)>;
1393 def : Pat<(sub GR32:$src1, i32immSExt8:$src2),
1394           (SUB32ri8 GR32:$src1, i32immSExt8:$src2)>;
1395
1396 // mul reg, reg
1397 def : Pat<(mul GR16:$src1, GR16:$src2),
1398           (IMUL16rr GR16:$src1, GR16:$src2)>;
1399 def : Pat<(mul GR32:$src1, GR32:$src2),
1400           (IMUL32rr GR32:$src1, GR32:$src2)>;
1401
1402 // mul reg, mem
1403 def : Pat<(mul GR16:$src1, (loadi16 addr:$src2)),
1404           (IMUL16rm GR16:$src1, addr:$src2)>;
1405 def : Pat<(mul GR32:$src1, (loadi32 addr:$src2)),
1406           (IMUL32rm GR32:$src1, addr:$src2)>;
1407
1408 // mul reg, imm
1409 def : Pat<(mul GR16:$src1, imm:$src2),
1410           (IMUL16rri GR16:$src1, imm:$src2)>;
1411 def : Pat<(mul GR32:$src1, imm:$src2),
1412           (IMUL32rri GR32:$src1, imm:$src2)>;
1413 def : Pat<(mul GR16:$src1, i16immSExt8:$src2),
1414           (IMUL16rri8 GR16:$src1, i16immSExt8:$src2)>;
1415 def : Pat<(mul GR32:$src1, i32immSExt8:$src2),
1416           (IMUL32rri8 GR32:$src1, i32immSExt8:$src2)>;
1417
1418 // reg = mul mem, imm
1419 def : Pat<(mul (loadi16 addr:$src1), imm:$src2),
1420           (IMUL16rmi addr:$src1, imm:$src2)>;
1421 def : Pat<(mul (loadi32 addr:$src1), imm:$src2),
1422           (IMUL32rmi addr:$src1, imm:$src2)>;
1423 def : Pat<(mul (loadi16 addr:$src1), i16immSExt8:$src2),
1424           (IMUL16rmi8 addr:$src1, i16immSExt8:$src2)>;
1425 def : Pat<(mul (loadi32 addr:$src1), i32immSExt8:$src2),
1426           (IMUL32rmi8 addr:$src1, i32immSExt8:$src2)>;
1427
1428 // Optimize multiply by 2 with EFLAGS result.
1429 let AddedComplexity = 2 in {
1430 def : Pat<(X86smul_flag GR16:$src1, 2), (ADD16rr GR16:$src1, GR16:$src1)>;
1431 def : Pat<(X86smul_flag GR32:$src1, 2), (ADD32rr GR32:$src1, GR32:$src1)>;
1432 }
1433
1434 // Patterns for nodes that do not produce flags, for instructions that do.
1435
1436 // addition
1437 def : Pat<(add GR64:$src1, GR64:$src2),
1438           (ADD64rr GR64:$src1, GR64:$src2)>;
1439 def : Pat<(add GR64:$src1, i64immSExt8:$src2),
1440           (ADD64ri8 GR64:$src1, i64immSExt8:$src2)>;
1441 def : Pat<(add GR64:$src1, i64immSExt32:$src2),
1442           (ADD64ri32 GR64:$src1, i64immSExt32:$src2)>;
1443 def : Pat<(add GR64:$src1, (loadi64 addr:$src2)),
1444           (ADD64rm GR64:$src1, addr:$src2)>;
1445
1446 // subtraction
1447 def : Pat<(sub GR64:$src1, GR64:$src2),
1448           (SUB64rr GR64:$src1, GR64:$src2)>;
1449 def : Pat<(sub GR64:$src1, (loadi64 addr:$src2)),
1450           (SUB64rm GR64:$src1, addr:$src2)>;
1451 def : Pat<(sub GR64:$src1, i64immSExt8:$src2),
1452           (SUB64ri8 GR64:$src1, i64immSExt8:$src2)>;
1453 def : Pat<(sub GR64:$src1, i64immSExt32:$src2),
1454           (SUB64ri32 GR64:$src1, i64immSExt32:$src2)>;
1455
1456 // Multiply
1457 def : Pat<(mul GR64:$src1, GR64:$src2),
1458           (IMUL64rr GR64:$src1, GR64:$src2)>;
1459 def : Pat<(mul GR64:$src1, (loadi64 addr:$src2)),
1460           (IMUL64rm GR64:$src1, addr:$src2)>;
1461 def : Pat<(mul GR64:$src1, i64immSExt8:$src2),
1462           (IMUL64rri8 GR64:$src1, i64immSExt8:$src2)>;
1463 def : Pat<(mul GR64:$src1, i64immSExt32:$src2),
1464           (IMUL64rri32 GR64:$src1, i64immSExt32:$src2)>;
1465 def : Pat<(mul (loadi64 addr:$src1), i64immSExt8:$src2),
1466           (IMUL64rmi8 addr:$src1, i64immSExt8:$src2)>;
1467 def : Pat<(mul (loadi64 addr:$src1), i64immSExt32:$src2),
1468           (IMUL64rmi32 addr:$src1, i64immSExt32:$src2)>;
1469
1470 // Increment reg.
1471 def : Pat<(add GR8 :$src, 1), (INC8r     GR8 :$src)>;
1472 def : Pat<(add GR16:$src, 1), (INC16r    GR16:$src)>, Requires<[In32BitMode]>;
1473 def : Pat<(add GR16:$src, 1), (INC64_16r GR16:$src)>, Requires<[In64BitMode]>;
1474 def : Pat<(add GR32:$src, 1), (INC32r    GR32:$src)>, Requires<[In32BitMode]>;
1475 def : Pat<(add GR32:$src, 1), (INC64_32r GR32:$src)>, Requires<[In64BitMode]>;
1476 def : Pat<(add GR64:$src, 1), (INC64r    GR64:$src)>;
1477
1478 // Decrement reg.
1479 def : Pat<(add GR8 :$src, -1), (DEC8r     GR8 :$src)>;
1480 def : Pat<(add GR16:$src, -1), (DEC16r    GR16:$src)>, Requires<[In32BitMode]>;
1481 def : Pat<(add GR16:$src, -1), (DEC64_16r GR16:$src)>, Requires<[In64BitMode]>;
1482 def : Pat<(add GR32:$src, -1), (DEC32r    GR32:$src)>, Requires<[In32BitMode]>;
1483 def : Pat<(add GR32:$src, -1), (DEC64_32r GR32:$src)>, Requires<[In64BitMode]>;
1484 def : Pat<(add GR64:$src, -1), (DEC64r    GR64:$src)>;
1485
1486 // or reg/reg.
1487 def : Pat<(or GR8 :$src1, GR8 :$src2), (OR8rr  GR8 :$src1, GR8 :$src2)>;
1488 def : Pat<(or GR16:$src1, GR16:$src2), (OR16rr GR16:$src1, GR16:$src2)>;
1489 def : Pat<(or GR32:$src1, GR32:$src2), (OR32rr GR32:$src1, GR32:$src2)>;
1490 def : Pat<(or GR64:$src1, GR64:$src2), (OR64rr GR64:$src1, GR64:$src2)>;
1491
1492 // or reg/mem
1493 def : Pat<(or GR8:$src1, (loadi8 addr:$src2)),
1494           (OR8rm GR8:$src1, addr:$src2)>;
1495 def : Pat<(or GR16:$src1, (loadi16 addr:$src2)),
1496           (OR16rm GR16:$src1, addr:$src2)>;
1497 def : Pat<(or GR32:$src1, (loadi32 addr:$src2)),
1498           (OR32rm GR32:$src1, addr:$src2)>;
1499 def : Pat<(or GR64:$src1, (loadi64 addr:$src2)),
1500           (OR64rm GR64:$src1, addr:$src2)>;
1501
1502 // or reg/imm
1503 def : Pat<(or GR8:$src1 , imm:$src2), (OR8ri  GR8 :$src1, imm:$src2)>;
1504 def : Pat<(or GR16:$src1, imm:$src2), (OR16ri GR16:$src1, imm:$src2)>;
1505 def : Pat<(or GR32:$src1, imm:$src2), (OR32ri GR32:$src1, imm:$src2)>;
1506 def : Pat<(or GR16:$src1, i16immSExt8:$src2),
1507           (OR16ri8 GR16:$src1, i16immSExt8:$src2)>;
1508 def : Pat<(or GR32:$src1, i32immSExt8:$src2),
1509           (OR32ri8 GR32:$src1, i32immSExt8:$src2)>;
1510 def : Pat<(or GR64:$src1, i64immSExt8:$src2),
1511           (OR64ri8 GR64:$src1, i64immSExt8:$src2)>;
1512 def : Pat<(or GR64:$src1, i64immSExt32:$src2),
1513           (OR64ri32 GR64:$src1, i64immSExt32:$src2)>;
1514
1515 // xor reg/reg
1516 def : Pat<(xor GR8 :$src1, GR8 :$src2), (XOR8rr  GR8 :$src1, GR8 :$src2)>;
1517 def : Pat<(xor GR16:$src1, GR16:$src2), (XOR16rr GR16:$src1, GR16:$src2)>;
1518 def : Pat<(xor GR32:$src1, GR32:$src2), (XOR32rr GR32:$src1, GR32:$src2)>;
1519 def : Pat<(xor GR64:$src1, GR64:$src2), (XOR64rr GR64:$src1, GR64:$src2)>;
1520
1521 // xor reg/mem
1522 def : Pat<(xor GR8:$src1, (loadi8 addr:$src2)),
1523           (XOR8rm GR8:$src1, addr:$src2)>;
1524 def : Pat<(xor GR16:$src1, (loadi16 addr:$src2)),
1525           (XOR16rm GR16:$src1, addr:$src2)>;
1526 def : Pat<(xor GR32:$src1, (loadi32 addr:$src2)),
1527           (XOR32rm GR32:$src1, addr:$src2)>;
1528 def : Pat<(xor GR64:$src1, (loadi64 addr:$src2)),
1529           (XOR64rm GR64:$src1, addr:$src2)>;
1530
1531 // xor reg/imm
1532 def : Pat<(xor GR8:$src1, imm:$src2),
1533           (XOR8ri GR8:$src1, imm:$src2)>;
1534 def : Pat<(xor GR16:$src1, imm:$src2),
1535           (XOR16ri GR16:$src1, imm:$src2)>;
1536 def : Pat<(xor GR32:$src1, imm:$src2),
1537           (XOR32ri GR32:$src1, imm:$src2)>;
1538 def : Pat<(xor GR16:$src1, i16immSExt8:$src2),
1539           (XOR16ri8 GR16:$src1, i16immSExt8:$src2)>;
1540 def : Pat<(xor GR32:$src1, i32immSExt8:$src2),
1541           (XOR32ri8 GR32:$src1, i32immSExt8:$src2)>;
1542 def : Pat<(xor GR64:$src1, i64immSExt8:$src2),
1543           (XOR64ri8 GR64:$src1, i64immSExt8:$src2)>;
1544 def : Pat<(xor GR64:$src1, i64immSExt32:$src2),
1545           (XOR64ri32 GR64:$src1, i64immSExt32:$src2)>;
1546
1547 // and reg/reg
1548 def : Pat<(and GR8 :$src1, GR8 :$src2), (AND8rr  GR8 :$src1, GR8 :$src2)>;
1549 def : Pat<(and GR16:$src1, GR16:$src2), (AND16rr GR16:$src1, GR16:$src2)>;
1550 def : Pat<(and GR32:$src1, GR32:$src2), (AND32rr GR32:$src1, GR32:$src2)>;
1551 def : Pat<(and GR64:$src1, GR64:$src2), (AND64rr GR64:$src1, GR64:$src2)>;
1552
1553 // and reg/mem
1554 def : Pat<(and GR8:$src1, (loadi8 addr:$src2)),
1555           (AND8rm GR8:$src1, addr:$src2)>;
1556 def : Pat<(and GR16:$src1, (loadi16 addr:$src2)),
1557           (AND16rm GR16:$src1, addr:$src2)>;
1558 def : Pat<(and GR32:$src1, (loadi32 addr:$src2)),
1559           (AND32rm GR32:$src1, addr:$src2)>;
1560 def : Pat<(and GR64:$src1, (loadi64 addr:$src2)),
1561           (AND64rm GR64:$src1, addr:$src2)>;
1562
1563 // and reg/imm
1564 def : Pat<(and GR8:$src1, imm:$src2),
1565           (AND8ri GR8:$src1, imm:$src2)>;
1566 def : Pat<(and GR16:$src1, imm:$src2),
1567           (AND16ri GR16:$src1, imm:$src2)>;
1568 def : Pat<(and GR32:$src1, imm:$src2),
1569           (AND32ri GR32:$src1, imm:$src2)>;
1570 def : Pat<(and GR16:$src1, i16immSExt8:$src2),
1571           (AND16ri8 GR16:$src1, i16immSExt8:$src2)>;
1572 def : Pat<(and GR32:$src1, i32immSExt8:$src2),
1573           (AND32ri8 GR32:$src1, i32immSExt8:$src2)>;
1574 def : Pat<(and GR64:$src1, i64immSExt8:$src2),
1575           (AND64ri8 GR64:$src1, i64immSExt8:$src2)>;
1576 def : Pat<(and GR64:$src1, i64immSExt32:$src2),
1577           (AND64ri32 GR64:$src1, i64immSExt32:$src2)>;
1578