convert test to use the existing classes that the multipatterns
[oota-llvm.git] / lib / Target / X86 / X86InstrArithmetic.td
1 //===- X86InstrArithmetic.td - Integer Arithmetic Instrs ---*- 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 integer arithmetic instructions in the X86
11 // architecture.
12 //
13 //===----------------------------------------------------------------------===//
14
15 //===----------------------------------------------------------------------===//
16 // LEA - Load Effective Address
17
18 let neverHasSideEffects = 1 in
19 def LEA16r   : I<0x8D, MRMSrcMem,
20                  (outs GR16:$dst), (ins i32mem:$src),
21                  "lea{w}\t{$src|$dst}, {$dst|$src}", []>, OpSize;
22 let isReMaterializable = 1 in
23 def LEA32r   : I<0x8D, MRMSrcMem,
24                  (outs GR32:$dst), (ins i32mem:$src),
25                  "lea{l}\t{$src|$dst}, {$dst|$src}",
26                  [(set GR32:$dst, lea32addr:$src)]>, Requires<[In32BitMode]>;
27
28 def LEA64_32r : I<0x8D, MRMSrcMem,
29                   (outs GR32:$dst), (ins lea64_32mem:$src),
30                   "lea{l}\t{$src|$dst}, {$dst|$src}",
31                   [(set GR32:$dst, lea32addr:$src)]>, Requires<[In64BitMode]>;
32
33 let isReMaterializable = 1 in
34 def LEA64r   : RI<0x8D, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
35                   "lea{q}\t{$src|$dst}, {$dst|$src}",
36                   [(set GR64:$dst, lea64addr:$src)]>;
37
38
39
40 //===----------------------------------------------------------------------===//
41 //  Fixed-Register Multiplication and Division Instructions.
42 //
43
44 // Extra precision multiplication
45
46 // AL is really implied by AX, but the registers in Defs must match the
47 // SDNode results (i8, i32).
48 let Defs = [AL,EFLAGS,AX], Uses = [AL] in
49 def MUL8r  : I<0xF6, MRM4r, (outs),  (ins GR8:$src), "mul{b}\t$src",
50                // FIXME: Used for 8-bit mul, ignore result upper 8 bits.
51                // This probably ought to be moved to a def : Pat<> if the
52                // syntax can be accepted.
53                [(set AL, (mul AL, GR8:$src)),
54                 (implicit EFLAGS)]>;     // AL,AH = AL*GR8
55
56 let Defs = [AX,DX,EFLAGS], Uses = [AX], neverHasSideEffects = 1 in
57 def MUL16r : I<0xF7, MRM4r, (outs),  (ins GR16:$src),
58                "mul{w}\t$src", 
59                []>, OpSize;    // AX,DX = AX*GR16
60
61 let Defs = [EAX,EDX,EFLAGS], Uses = [EAX], neverHasSideEffects = 1 in
62 def MUL32r : I<0xF7, MRM4r, (outs),  (ins GR32:$src),
63                "mul{l}\t$src",
64                []>; // EAX,EDX = EAX*GR32
65 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX], neverHasSideEffects = 1 in
66 def MUL64r : RI<0xF7, MRM4r, (outs), (ins GR64:$src),
67                 "mul{q}\t$src", []>;         // RAX,RDX = RAX*GR64
68
69 let Defs = [AL,EFLAGS,AX], Uses = [AL] in
70 def MUL8m  : I<0xF6, MRM4m, (outs), (ins i8mem :$src),
71                "mul{b}\t$src",
72                // FIXME: Used for 8-bit mul, ignore result upper 8 bits.
73                // This probably ought to be moved to a def : Pat<> if the
74                // syntax can be accepted.
75                [(set AL, (mul AL, (loadi8 addr:$src))),
76                 (implicit EFLAGS)]>;   // AL,AH = AL*[mem8]
77
78 let mayLoad = 1, neverHasSideEffects = 1 in {
79 let Defs = [AX,DX,EFLAGS], Uses = [AX] in
80 def MUL16m : I<0xF7, MRM4m, (outs), (ins i16mem:$src),
81                "mul{w}\t$src",
82                []>, OpSize; // AX,DX = AX*[mem16]
83
84 let Defs = [EAX,EDX,EFLAGS], Uses = [EAX] in
85 def MUL32m : I<0xF7, MRM4m, (outs), (ins i32mem:$src),
86               "mul{l}\t$src",
87               []>;          // EAX,EDX = EAX*[mem32]
88 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX], neverHasSideEffects = 1 in
89 def MUL64m : RI<0xF7, MRM4m, (outs), (ins i64mem:$src),
90                 "mul{q}\t$src", []>;         // RAX,RDX = RAX*[mem64]
91 }
92
93 let neverHasSideEffects = 1 in {
94 let Defs = [AL,EFLAGS,AX], Uses = [AL] in
95 def IMUL8r  : I<0xF6, MRM5r, (outs),  (ins GR8:$src), "imul{b}\t$src", []>;
96               // AL,AH = AL*GR8
97 let Defs = [AX,DX,EFLAGS], Uses = [AX] in
98 def IMUL16r : I<0xF7, MRM5r, (outs),  (ins GR16:$src), "imul{w}\t$src", []>,
99               OpSize;    // AX,DX = AX*GR16
100 let Defs = [EAX,EDX,EFLAGS], Uses = [EAX] in
101 def IMUL32r : I<0xF7, MRM5r, (outs),  (ins GR32:$src), "imul{l}\t$src", []>;
102               // EAX,EDX = EAX*GR32
103 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX], neverHasSideEffects = 1 in
104 def IMUL64r : RI<0xF7, MRM5r, (outs), (ins GR64:$src), "imul{q}\t$src", []>;
105               // RAX,RDX = RAX*GR64
106
107 let mayLoad = 1 in {
108 let Defs = [AL,EFLAGS,AX], Uses = [AL] in
109 def IMUL8m  : I<0xF6, MRM5m, (outs), (ins i8mem :$src),
110                 "imul{b}\t$src", []>;    // AL,AH = AL*[mem8]
111 let Defs = [AX,DX,EFLAGS], Uses = [AX] in
112 def IMUL16m : I<0xF7, MRM5m, (outs), (ins i16mem:$src),
113                 "imul{w}\t$src", []>, OpSize; // AX,DX = AX*[mem16]
114 let Defs = [EAX,EDX,EFLAGS], Uses = [EAX] in
115 def IMUL32m : I<0xF7, MRM5m, (outs), (ins i32mem:$src),
116                 "imul{l}\t$src", []>;  // EAX,EDX = EAX*[mem32]
117 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX], neverHasSideEffects = 1 in
118 def IMUL64m : RI<0xF7, MRM5m, (outs), (ins i64mem:$src),
119                  "imul{q}\t$src", []>;         // RAX,RDX = RAX*[mem64]
120 }
121 } // neverHasSideEffects
122
123
124 let Defs = [EFLAGS] in {
125 let Constraints = "$src1 = $dst" in {
126
127 let isCommutable = 1 in {  // X = IMUL Y, Z --> X = IMUL Z, Y
128 // Register-Register Signed Integer Multiply
129 def IMUL16rr : I<0xAF, MRMSrcReg, (outs GR16:$dst), (ins GR16:$src1,GR16:$src2),
130                  "imul{w}\t{$src2, $dst|$dst, $src2}",
131                  [(set GR16:$dst, EFLAGS,
132                        (X86smul_flag GR16:$src1, GR16:$src2))]>, TB, OpSize;
133 def IMUL32rr : I<0xAF, MRMSrcReg, (outs GR32:$dst), (ins GR32:$src1,GR32:$src2),
134                  "imul{l}\t{$src2, $dst|$dst, $src2}",
135                  [(set GR32:$dst, EFLAGS,
136                        (X86smul_flag GR32:$src1, GR32:$src2))]>, TB;
137 def IMUL64rr : RI<0xAF, MRMSrcReg, (outs GR64:$dst),
138                                    (ins GR64:$src1, GR64:$src2),
139                   "imul{q}\t{$src2, $dst|$dst, $src2}",
140                   [(set GR64:$dst, EFLAGS,
141                         (X86smul_flag GR64:$src1, GR64:$src2))]>, TB;
142 }
143
144 // Register-Memory Signed Integer Multiply
145 def IMUL16rm : I<0xAF, MRMSrcMem, (outs GR16:$dst),
146                                   (ins GR16:$src1, i16mem:$src2),
147                  "imul{w}\t{$src2, $dst|$dst, $src2}",
148                  [(set GR16:$dst, EFLAGS,
149                        (X86smul_flag GR16:$src1, (load addr:$src2)))]>,
150                TB, OpSize;
151 def IMUL32rm : I<0xAF, MRMSrcMem, (outs GR32:$dst), 
152                  (ins GR32:$src1, i32mem:$src2),
153                  "imul{l}\t{$src2, $dst|$dst, $src2}",
154                  [(set GR32:$dst, EFLAGS,
155                        (X86smul_flag GR32:$src1, (load addr:$src2)))]>, TB;
156 def IMUL64rm : RI<0xAF, MRMSrcMem, (outs GR64:$dst),
157                                    (ins GR64:$src1, i64mem:$src2),
158                   "imul{q}\t{$src2, $dst|$dst, $src2}",
159                   [(set GR64:$dst, EFLAGS,
160                         (X86smul_flag GR64:$src1, (load addr:$src2)))]>, TB;
161 } // Constraints = "$src1 = $dst"
162
163 } // Defs = [EFLAGS]
164
165 // Suprisingly enough, these are not two address instructions!
166 let Defs = [EFLAGS] in {
167 // Register-Integer Signed Integer Multiply
168 def IMUL16rri  : Ii16<0x69, MRMSrcReg,                      // GR16 = GR16*I16
169                       (outs GR16:$dst), (ins GR16:$src1, i16imm:$src2),
170                       "imul{w}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
171                       [(set GR16:$dst, EFLAGS, 
172                             (X86smul_flag GR16:$src1, imm:$src2))]>, OpSize;
173 def IMUL16rri8 : Ii8<0x6B, MRMSrcReg,                       // GR16 = GR16*I8
174                      (outs GR16:$dst), (ins GR16:$src1, i16i8imm:$src2),
175                      "imul{w}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
176                      [(set GR16:$dst, EFLAGS,
177                            (X86smul_flag GR16:$src1, i16immSExt8:$src2))]>,
178                  OpSize;
179 def IMUL32rri  : Ii32<0x69, MRMSrcReg,                      // GR32 = GR32*I32
180                       (outs GR32:$dst), (ins GR32:$src1, i32imm:$src2),
181                       "imul{l}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
182                       [(set GR32:$dst, EFLAGS,
183                             (X86smul_flag GR32:$src1, imm:$src2))]>;
184 def IMUL32rri8 : Ii8<0x6B, MRMSrcReg,                       // GR32 = GR32*I8
185                      (outs GR32:$dst), (ins GR32:$src1, i32i8imm:$src2),
186                      "imul{l}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
187                      [(set GR32:$dst, EFLAGS,
188                            (X86smul_flag GR32:$src1, i32immSExt8:$src2))]>;
189 def IMUL64rri32 : RIi32<0x69, MRMSrcReg,                    // GR64 = GR64*I32
190                         (outs GR64:$dst), (ins GR64:$src1, i64i32imm:$src2),
191                         "imul{q}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
192                        [(set GR64:$dst, EFLAGS,
193                              (X86smul_flag GR64:$src1, i64immSExt32:$src2))]>;
194 def IMUL64rri8 : RIi8<0x6B, MRMSrcReg,                      // GR64 = GR64*I8
195                       (outs GR64:$dst), (ins GR64:$src1, i64i8imm:$src2),
196                       "imul{q}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
197                       [(set GR64:$dst, EFLAGS,
198                             (X86smul_flag GR64:$src1, i64immSExt8:$src2))]>;
199
200
201 // Memory-Integer Signed Integer Multiply
202 def IMUL16rmi  : Ii16<0x69, MRMSrcMem,                     // GR16 = [mem16]*I16
203                       (outs GR16:$dst), (ins i16mem:$src1, i16imm:$src2),
204                       "imul{w}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
205                       [(set GR16:$dst, EFLAGS,
206                             (X86smul_flag (load addr:$src1), imm:$src2))]>,
207                  OpSize;
208 def IMUL16rmi8 : Ii8<0x6B, MRMSrcMem,                       // GR16 = [mem16]*I8
209                      (outs GR16:$dst), (ins i16mem:$src1, i16i8imm :$src2),
210                      "imul{w}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
211                      [(set GR16:$dst, EFLAGS,
212                            (X86smul_flag (load addr:$src1),
213                                          i16immSExt8:$src2))]>, OpSize;
214 def IMUL32rmi  : Ii32<0x69, MRMSrcMem,                     // GR32 = [mem32]*I32
215                       (outs GR32:$dst), (ins i32mem:$src1, i32imm:$src2),
216                       "imul{l}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
217                       [(set GR32:$dst, EFLAGS,
218                             (X86smul_flag (load addr:$src1), imm:$src2))]>;
219 def IMUL32rmi8 : Ii8<0x6B, MRMSrcMem,                       // GR32 = [mem32]*I8
220                      (outs GR32:$dst), (ins i32mem:$src1, i32i8imm: $src2),
221                      "imul{l}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
222                      [(set GR32:$dst, EFLAGS,
223                            (X86smul_flag (load addr:$src1),
224                                          i32immSExt8:$src2))]>;
225 def IMUL64rmi32 : RIi32<0x69, MRMSrcMem,                   // GR64 = [mem64]*I32
226                         (outs GR64:$dst), (ins i64mem:$src1, i64i32imm:$src2),
227                         "imul{q}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
228                         [(set GR64:$dst, EFLAGS,
229                               (X86smul_flag (load addr:$src1),
230                                             i64immSExt32:$src2))]>;
231 def IMUL64rmi8 : RIi8<0x6B, MRMSrcMem,                      // GR64 = [mem64]*I8
232                       (outs GR64:$dst), (ins i64mem:$src1, i64i8imm: $src2),
233                       "imul{q}\t{$src2, $src1, $dst|$dst, $src1, $src2}",
234                       [(set GR64:$dst, EFLAGS,
235                             (X86smul_flag (load addr:$src1),
236                                           i64immSExt8:$src2))]>;
237 } // Defs = [EFLAGS]
238
239
240
241
242 // unsigned division/remainder
243 let Defs = [AL,EFLAGS,AX], Uses = [AX] in
244 def DIV8r  : I<0xF6, MRM6r, (outs),  (ins GR8:$src),    // AX/r8 = AL,AH
245                "div{b}\t$src", []>;
246 let Defs = [AX,DX,EFLAGS], Uses = [AX,DX] in
247 def DIV16r : I<0xF7, MRM6r, (outs),  (ins GR16:$src),   // DX:AX/r16 = AX,DX
248                "div{w}\t$src", []>, OpSize;
249 let Defs = [EAX,EDX,EFLAGS], Uses = [EAX,EDX] in
250 def DIV32r : I<0xF7, MRM6r, (outs),  (ins GR32:$src),   // EDX:EAX/r32 = EAX,EDX
251                "div{l}\t$src", []>;
252 // RDX:RAX/r64 = RAX,RDX
253 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX,RDX] in
254 def DIV64r : RI<0xF7, MRM6r, (outs), (ins GR64:$src),
255                 "div{q}\t$src", []>;
256
257 let mayLoad = 1 in {
258 let Defs = [AL,EFLAGS,AX], Uses = [AX] in
259 def DIV8m  : I<0xF6, MRM6m, (outs), (ins i8mem:$src),   // AX/[mem8] = AL,AH
260                "div{b}\t$src", []>;
261 let Defs = [AX,DX,EFLAGS], Uses = [AX,DX] in
262 def DIV16m : I<0xF7, MRM6m, (outs), (ins i16mem:$src),  // DX:AX/[mem16] = AX,DX
263                "div{w}\t$src", []>, OpSize;
264 let Defs = [EAX,EDX,EFLAGS], Uses = [EAX,EDX] in    // EDX:EAX/[mem32] = EAX,EDX
265 def DIV32m : I<0xF7, MRM6m, (outs), (ins i32mem:$src),
266                "div{l}\t$src", []>;
267 // RDX:RAX/[mem64] = RAX,RDX
268 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX,RDX] in
269 def DIV64m : RI<0xF7, MRM6m, (outs), (ins i64mem:$src),
270                 "div{q}\t$src", []>;
271 }
272
273 // Signed division/remainder.
274 let Defs = [AL,EFLAGS,AX], Uses = [AX] in
275 def IDIV8r : I<0xF6, MRM7r, (outs),  (ins GR8:$src),    // AX/r8 = AL,AH
276                "idiv{b}\t$src", []>;
277 let Defs = [AX,DX,EFLAGS], Uses = [AX,DX] in
278 def IDIV16r: I<0xF7, MRM7r, (outs),  (ins GR16:$src),   // DX:AX/r16 = AX,DX
279                "idiv{w}\t$src", []>, OpSize;
280 let Defs = [EAX,EDX,EFLAGS], Uses = [EAX,EDX] in
281 def IDIV32r: I<0xF7, MRM7r, (outs),  (ins GR32:$src),   // EDX:EAX/r32 = EAX,EDX
282                "idiv{l}\t$src", []>;
283 // RDX:RAX/r64 = RAX,RDX
284 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX,RDX] in
285 def IDIV64r: RI<0xF7, MRM7r, (outs), (ins GR64:$src),
286                 "idiv{q}\t$src", []>;
287                
288 let mayLoad = 1, mayLoad = 1 in {
289 let Defs = [AL,EFLAGS,AX], Uses = [AX] in
290 def IDIV8m : I<0xF6, MRM7m, (outs), (ins i8mem:$src),   // AX/[mem8] = AL,AH
291                "idiv{b}\t$src", []>;
292 let Defs = [AX,DX,EFLAGS], Uses = [AX,DX] in
293 def IDIV16m: I<0xF7, MRM7m, (outs), (ins i16mem:$src),  // DX:AX/[mem16] = AX,DX
294                "idiv{w}\t$src", []>, OpSize;
295 let Defs = [EAX,EDX,EFLAGS], Uses = [EAX,EDX] in    // EDX:EAX/[mem32] = EAX,EDX
296 def IDIV32m: I<0xF7, MRM7m, (outs), (ins i32mem:$src), 
297                "idiv{l}\t$src", []>;
298 let Defs = [RAX,RDX,EFLAGS], Uses = [RAX,RDX] in // RDX:RAX/[mem64] = RAX,RDX
299 def IDIV64m: RI<0xF7, MRM7m, (outs), (ins i64mem:$src),
300                 "idiv{q}\t$src", []>;
301 }
302
303 //===----------------------------------------------------------------------===//
304 //  Two address Instructions.
305 //
306
307 // unary instructions
308 let CodeSize = 2 in {
309 let Defs = [EFLAGS] in {
310 let Constraints = "$src1 = $dst" in {
311 def NEG8r  : I<0xF6, MRM3r, (outs GR8 :$dst), (ins GR8 :$src1),
312                "neg{b}\t$dst",
313                [(set GR8:$dst, (ineg GR8:$src1)),
314                 (implicit EFLAGS)]>;
315 def NEG16r : I<0xF7, MRM3r, (outs GR16:$dst), (ins GR16:$src1),
316                "neg{w}\t$dst",
317                [(set GR16:$dst, (ineg GR16:$src1)),
318                 (implicit EFLAGS)]>, OpSize;
319 def NEG32r : I<0xF7, MRM3r, (outs GR32:$dst), (ins GR32:$src1),
320                "neg{l}\t$dst",
321                [(set GR32:$dst, (ineg GR32:$src1)),
322                 (implicit EFLAGS)]>;
323 def NEG64r : RI<0xF7, MRM3r, (outs GR64:$dst), (ins GR64:$src1), "neg{q}\t$dst",
324                 [(set GR64:$dst, (ineg GR64:$src1)),
325                  (implicit EFLAGS)]>;
326 } // Constraints = "$src1 = $dst"
327
328 def NEG8m  : I<0xF6, MRM3m, (outs), (ins i8mem :$dst),
329                "neg{b}\t$dst",
330                [(store (ineg (loadi8 addr:$dst)), addr:$dst),
331                 (implicit EFLAGS)]>;
332 def NEG16m : I<0xF7, MRM3m, (outs), (ins i16mem:$dst),
333                "neg{w}\t$dst",
334                [(store (ineg (loadi16 addr:$dst)), addr:$dst),
335                 (implicit EFLAGS)]>, OpSize;
336 def NEG32m : I<0xF7, MRM3m, (outs), (ins i32mem:$dst),
337                "neg{l}\t$dst",
338                [(store (ineg (loadi32 addr:$dst)), addr:$dst),
339                 (implicit EFLAGS)]>;
340 def NEG64m : RI<0xF7, MRM3m, (outs), (ins i64mem:$dst), "neg{q}\t$dst",
341                 [(store (ineg (loadi64 addr:$dst)), addr:$dst),
342                  (implicit EFLAGS)]>;
343 } // Defs = [EFLAGS]
344
345
346 // Note: NOT does not set EFLAGS!
347
348 let Constraints = "$src1 = $dst" in {
349 // Match xor -1 to not. Favors these over a move imm + xor to save code size.
350 let AddedComplexity = 15 in {
351 def NOT8r  : I<0xF6, MRM2r, (outs GR8 :$dst), (ins GR8 :$src1),
352                "not{b}\t$dst",
353                [(set GR8:$dst, (not GR8:$src1))]>;
354 def NOT16r : I<0xF7, MRM2r, (outs GR16:$dst), (ins GR16:$src1),
355                "not{w}\t$dst",
356                [(set GR16:$dst, (not GR16:$src1))]>, OpSize;
357 def NOT32r : I<0xF7, MRM2r, (outs GR32:$dst), (ins GR32:$src1),
358                "not{l}\t$dst",
359                [(set GR32:$dst, (not GR32:$src1))]>;
360 def NOT64r : RI<0xF7, MRM2r, (outs GR64:$dst), (ins GR64:$src1), "not{q}\t$dst",
361                 [(set GR64:$dst, (not GR64:$src1))]>;
362 }
363 } // Constraints = "$src1 = $dst"
364
365 def NOT8m  : I<0xF6, MRM2m, (outs), (ins i8mem :$dst),
366                "not{b}\t$dst",
367                [(store (not (loadi8 addr:$dst)), addr:$dst)]>;
368 def NOT16m : I<0xF7, MRM2m, (outs), (ins i16mem:$dst),
369                "not{w}\t$dst",
370                [(store (not (loadi16 addr:$dst)), addr:$dst)]>, OpSize;
371 def NOT32m : I<0xF7, MRM2m, (outs), (ins i32mem:$dst),
372                "not{l}\t$dst",
373                [(store (not (loadi32 addr:$dst)), addr:$dst)]>;
374 def NOT64m : RI<0xF7, MRM2m, (outs), (ins i64mem:$dst), "not{q}\t$dst",
375                 [(store (not (loadi64 addr:$dst)), addr:$dst)]>;
376 } // CodeSize
377
378 // TODO: inc/dec is slow for P4, but fast for Pentium-M.
379 let Defs = [EFLAGS] in {
380 let Constraints = "$src1 = $dst" in {
381 let CodeSize = 2 in
382 def INC8r  : I<0xFE, MRM0r, (outs GR8 :$dst), (ins GR8 :$src1),
383                "inc{b}\t$dst",
384                [(set GR8:$dst, EFLAGS, (X86inc_flag GR8:$src1))]>;
385
386 let isConvertibleToThreeAddress = 1, CodeSize = 1 in {  // Can xform into LEA.
387 def INC16r : I<0x40, AddRegFrm, (outs GR16:$dst), (ins GR16:$src1), 
388                "inc{w}\t$dst",
389                [(set GR16:$dst, EFLAGS, (X86inc_flag GR16:$src1))]>,
390              OpSize, Requires<[In32BitMode]>;
391 def INC32r : I<0x40, AddRegFrm, (outs GR32:$dst), (ins GR32:$src1), 
392                "inc{l}\t$dst",
393                [(set GR32:$dst, EFLAGS, (X86inc_flag GR32:$src1))]>,
394              Requires<[In32BitMode]>;
395 def INC64r : RI<0xFF, MRM0r, (outs GR64:$dst), (ins GR64:$src1), "inc{q}\t$dst",
396                 [(set GR64:$dst, EFLAGS, (X86inc_flag GR64:$src1))]>;
397 } // isConvertibleToThreeAddress = 1, CodeSize = 1
398
399
400 // In 64-bit mode, single byte INC and DEC cannot be encoded.
401 let isConvertibleToThreeAddress = 1, CodeSize = 2 in {
402 // Can transform into LEA.
403 def INC64_16r : I<0xFF, MRM0r, (outs GR16:$dst), (ins GR16:$src1), 
404                   "inc{w}\t$dst",
405                   [(set GR16:$dst, EFLAGS, (X86inc_flag GR16:$src1))]>,
406                 OpSize, Requires<[In64BitMode]>;
407 def INC64_32r : I<0xFF, MRM0r, (outs GR32:$dst), (ins GR32:$src1), 
408                   "inc{l}\t$dst",
409                   [(set GR32:$dst, EFLAGS, (X86inc_flag GR32:$src1))]>,
410                 Requires<[In64BitMode]>;
411 def DEC64_16r : I<0xFF, MRM1r, (outs GR16:$dst), (ins GR16:$src1), 
412                   "dec{w}\t$dst",
413                   [(set GR16:$dst, EFLAGS, (X86dec_flag GR16:$src1))]>,
414                 OpSize, Requires<[In64BitMode]>;
415 def DEC64_32r : I<0xFF, MRM1r, (outs GR32:$dst), (ins GR32:$src1), 
416                   "dec{l}\t$dst",
417                   [(set GR32:$dst, EFLAGS, (X86dec_flag GR32:$src1))]>,
418                 Requires<[In64BitMode]>;
419 } // isConvertibleToThreeAddress = 1, CodeSize = 2
420
421 } // Constraints = "$src1 = $dst"
422
423 let CodeSize = 2 in {
424   def INC8m  : I<0xFE, MRM0m, (outs), (ins i8mem :$dst), "inc{b}\t$dst",
425                [(store (add (loadi8 addr:$dst), 1), addr:$dst),
426                 (implicit EFLAGS)]>;
427   def INC16m : I<0xFF, MRM0m, (outs), (ins i16mem:$dst), "inc{w}\t$dst",
428                [(store (add (loadi16 addr:$dst), 1), addr:$dst),
429                 (implicit EFLAGS)]>,
430                OpSize, Requires<[In32BitMode]>;
431   def INC32m : I<0xFF, MRM0m, (outs), (ins i32mem:$dst), "inc{l}\t$dst",
432                [(store (add (loadi32 addr:$dst), 1), addr:$dst),
433                 (implicit EFLAGS)]>,
434                Requires<[In32BitMode]>;
435   def INC64m : RI<0xFF, MRM0m, (outs), (ins i64mem:$dst), "inc{q}\t$dst",
436                   [(store (add (loadi64 addr:$dst), 1), addr:$dst),
437                    (implicit EFLAGS)]>;
438                    
439 // These are duplicates of their 32-bit counterparts. Only needed so X86 knows
440 // how to unfold them.
441 // FIXME: What is this for??
442 def INC64_16m : I<0xFF, MRM0m, (outs), (ins i16mem:$dst), "inc{w}\t$dst",
443                   [(store (add (loadi16 addr:$dst), 1), addr:$dst),
444                     (implicit EFLAGS)]>,
445                 OpSize, Requires<[In64BitMode]>;
446 def INC64_32m : I<0xFF, MRM0m, (outs), (ins i32mem:$dst), "inc{l}\t$dst",
447                   [(store (add (loadi32 addr:$dst), 1), addr:$dst),
448                     (implicit EFLAGS)]>,
449                 Requires<[In64BitMode]>;
450 def DEC64_16m : I<0xFF, MRM1m, (outs), (ins i16mem:$dst), "dec{w}\t$dst",
451                   [(store (add (loadi16 addr:$dst), -1), addr:$dst),
452                     (implicit EFLAGS)]>,
453                 OpSize, Requires<[In64BitMode]>;
454 def DEC64_32m : I<0xFF, MRM1m, (outs), (ins i32mem:$dst), "dec{l}\t$dst",
455                   [(store (add (loadi32 addr:$dst), -1), addr:$dst),
456                     (implicit EFLAGS)]>,
457                 Requires<[In64BitMode]>;
458 } // CodeSize = 2
459
460 let Constraints = "$src1 = $dst" in {
461 let CodeSize = 2 in
462 def DEC8r  : I<0xFE, MRM1r, (outs GR8 :$dst), (ins GR8 :$src1),
463                "dec{b}\t$dst",
464                [(set GR8:$dst, EFLAGS, (X86dec_flag GR8:$src1))]>;
465 let isConvertibleToThreeAddress = 1, CodeSize = 1 in {   // Can xform into LEA.
466 def DEC16r : I<0x48, AddRegFrm, (outs GR16:$dst), (ins GR16:$src1), 
467                "dec{w}\t$dst",
468                [(set GR16:$dst, EFLAGS, (X86dec_flag GR16:$src1))]>,
469              OpSize, Requires<[In32BitMode]>;
470 def DEC32r : I<0x48, AddRegFrm, (outs GR32:$dst), (ins GR32:$src1), 
471                "dec{l}\t$dst",
472                [(set GR32:$dst, EFLAGS, (X86dec_flag GR32:$src1))]>,
473              Requires<[In32BitMode]>;
474 def DEC64r : RI<0xFF, MRM1r, (outs GR64:$dst), (ins GR64:$src1), "dec{q}\t$dst",
475                 [(set GR64:$dst, EFLAGS, (X86dec_flag GR64:$src1))]>;
476 } // CodeSize = 2
477 } // Constraints = "$src1 = $dst"
478
479
480 let CodeSize = 2 in {
481   def DEC8m  : I<0xFE, MRM1m, (outs), (ins i8mem :$dst), "dec{b}\t$dst",
482                [(store (add (loadi8 addr:$dst), -1), addr:$dst),
483                 (implicit EFLAGS)]>;
484   def DEC16m : I<0xFF, MRM1m, (outs), (ins i16mem:$dst), "dec{w}\t$dst",
485                [(store (add (loadi16 addr:$dst), -1), addr:$dst),
486                 (implicit EFLAGS)]>,
487                OpSize, Requires<[In32BitMode]>;
488   def DEC32m : I<0xFF, MRM1m, (outs), (ins i32mem:$dst), "dec{l}\t$dst",
489                [(store (add (loadi32 addr:$dst), -1), addr:$dst),
490                 (implicit EFLAGS)]>,
491                Requires<[In32BitMode]>;
492   def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst",
493                   [(store (add (loadi64 addr:$dst), -1), addr:$dst),
494                    (implicit EFLAGS)]>;
495 } // CodeSize = 2
496 } // Defs = [EFLAGS]
497
498
499 /// X86TypeInfo - This is a bunch of information that describes relevant X86
500 /// information about value types.  For example, it can tell you what the
501 /// register class and preferred load to use.
502 class X86TypeInfo<ValueType vt, string instrsuffix, RegisterClass regclass,
503                   PatFrag loadnode, X86MemOperand memoperand, ImmType immkind,
504                   Operand immoperand, SDPatternOperator immoperator,
505                   Operand imm8operand, SDPatternOperator imm8operator,
506                   bit hasOddOpcode, bit hasOpSizePrefix, bit hasREX_WPrefix> {
507   /// VT - This is the value type itself.
508   ValueType VT = vt;
509   
510   /// InstrSuffix - This is the suffix used on instructions with this type.  For
511   /// example, i8 -> "b", i16 -> "w", i32 -> "l", i64 -> "q".
512   string InstrSuffix = instrsuffix;
513   
514   /// RegClass - This is the register class associated with this type.  For
515   /// example, i8 -> GR8, i16 -> GR16, i32 -> GR32, i64 -> GR64.
516   RegisterClass RegClass = regclass;
517   
518   /// LoadNode - This is the load node associated with this type.  For
519   /// example, i8 -> loadi8, i16 -> loadi16, i32 -> loadi32, i64 -> loadi64.
520   PatFrag LoadNode = loadnode;
521   
522   /// MemOperand - This is the memory operand associated with this type.  For
523   /// example, i8 -> i8mem, i16 -> i16mem, i32 -> i32mem, i64 -> i64mem.
524   X86MemOperand MemOperand = memoperand;
525   
526   /// ImmEncoding - This is the encoding of an immediate of this type.  For
527   /// example, i8 -> Imm8, i16 -> Imm16, i32 -> Imm32.  Note that i64 -> Imm32
528   /// since the immediate fields of i64 instructions is a 32-bit sign extended
529   /// value.
530   ImmType ImmEncoding = immkind;
531   
532   /// ImmOperand - This is the operand kind of an immediate of this type.  For
533   /// example, i8 -> i8imm, i16 -> i16imm, i32 -> i32imm.  Note that i64 ->
534   /// i64i32imm since the immediate fields of i64 instructions is a 32-bit sign
535   /// extended value.
536   Operand ImmOperand = immoperand;
537   
538   /// ImmOperator - This is the operator that should be used to match an
539   /// immediate of this kind in a pattern (e.g. imm, or i64immSExt32).
540   SDPatternOperator ImmOperator = immoperator;
541   
542   /// Imm8Operand - This is the operand kind to use for an imm8 of this type.
543   /// For example, i8 -> <invalid>, i16 -> i16i8imm, i32 -> i32i8imm.  This is
544   /// only used for instructions that have a sign-extended imm8 field form.
545   Operand Imm8Operand = imm8operand;
546   
547   /// Imm8Operator - This is the operator that should be used to match an 8-bit
548   /// sign extended immediate of this kind in a pattern (e.g. imm16immSExt8).
549   SDPatternOperator Imm8Operator = imm8operator;
550   
551   /// HasOddOpcode - This bit is true if the instruction should have an odd (as
552   /// opposed to even) opcode.  Operations on i8 are usually even, operations on
553   /// other datatypes are odd.
554   bit HasOddOpcode = hasOddOpcode;
555   
556   /// HasOpSizePrefix - This bit is set to true if the instruction should have
557   /// the 0x66 operand size prefix.  This is set for i16 types.
558   bit HasOpSizePrefix = hasOpSizePrefix;
559   
560   /// HasREX_WPrefix - This bit is set to true if the instruction should have
561   /// the 0x40 REX prefix.  This is set for i64 types.
562   bit HasREX_WPrefix = hasREX_WPrefix;
563 }
564
565 def invalid_node : SDNode<"<<invalid_node>>", SDTIntLeaf,[],"<<invalid_node>>">;
566
567
568 def Xi8  : X86TypeInfo<i8 , "b", GR8 , loadi8 , i8mem ,
569                        Imm8 , i8imm ,    imm,          i8imm   , invalid_node,
570                        0, 0, 0>;
571 def Xi16 : X86TypeInfo<i16, "w", GR16, loadi16, i16mem,
572                        Imm16, i16imm,    imm,          i16i8imm, i16immSExt8,
573                        1, 1, 0>;
574 def Xi32 : X86TypeInfo<i32, "l", GR32, loadi32, i32mem,
575                        Imm32, i32imm,    imm,          i32i8imm, i32immSExt8,
576                        1, 0, 0>;
577 def Xi64 : X86TypeInfo<i64, "q", GR64, loadi64, i64mem,
578                        Imm32, i64i32imm, i64immSExt32, i64i8imm, i64immSExt8,
579                        1, 0, 1>;
580
581 /// ITy - This instruction base class takes the type info for the instruction.
582 /// Using this, it:
583 /// 1. Concatenates together the instruction mnemonic with the appropriate
584 ///    suffix letter, a tab, and the arguments.
585 /// 2. Infers whether the instruction should have a 0x66 prefix byte.
586 /// 3. Infers whether the instruction should have a 0x40 REX_W prefix.
587 /// 4. Infers whether the low bit of the opcode should be 0 (for i8 operations)
588 ///    or 1 (for i16,i32,i64 operations).
589 class ITy<bits<8> opcode, Format f, X86TypeInfo typeinfo, dag outs, dag ins, 
590           string mnemonic, string args, list<dag> pattern>
591   : I<{opcode{7}, opcode{6}, opcode{5}, opcode{4},
592        opcode{3}, opcode{2}, opcode{1}, typeinfo.HasOddOpcode },
593       f, outs, ins, 
594       !strconcat(mnemonic, "{", typeinfo.InstrSuffix, "}\t", args), pattern> {
595
596   // Infer instruction prefixes from type info.
597   let hasOpSizePrefix = typeinfo.HasOpSizePrefix;
598   let hasREX_WPrefix  = typeinfo.HasREX_WPrefix;
599 }
600
601 // BinOpRR - Instructions like "add reg, reg, reg".
602 class BinOpRR<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
603               dag outlist, list<dag> pattern, Format f = MRMDestReg>
604   : ITy<opcode, f, typeinfo, outlist,
605         (ins typeinfo.RegClass:$src1, typeinfo.RegClass:$src2),
606         mnemonic, "{$src2, $src1|$src1, $src2}", pattern>;
607
608 // BinOpRR_R - Instructions like "add reg, reg, reg", where the pattern has
609 // just a regclass (no eflags) as a result.
610 class BinOpRR_R<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
611                 SDNode opnode>
612   : BinOpRR<opcode, mnemonic, typeinfo, (outs typeinfo.RegClass:$dst),
613             [(set typeinfo.RegClass:$dst,
614                   (opnode typeinfo.RegClass:$src1, typeinfo.RegClass:$src2))]>;
615
616 // BinOpRR_F - Instructions like "cmp reg, Reg", where the pattern has
617 // just a EFLAGS as a result.
618 class BinOpRR_F<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
619                 SDPatternOperator opnode, Format f = MRMDestReg>
620   : BinOpRR<opcode, mnemonic, typeinfo, (outs),
621             [(set EFLAGS,
622                   (opnode typeinfo.RegClass:$src1, typeinfo.RegClass:$src2))],
623             f>;
624
625 // BinOpRR_RF - Instructions like "add reg, reg, reg", where the pattern has
626 // both a regclass and EFLAGS as a result.
627 class BinOpRR_RF<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
628                  SDNode opnode>
629   : BinOpRR<opcode, mnemonic, typeinfo, (outs typeinfo.RegClass:$dst),
630             [(set typeinfo.RegClass:$dst, EFLAGS,
631                   (opnode typeinfo.RegClass:$src1, typeinfo.RegClass:$src2))]>;
632
633 // BinOpRR_Rev - Instructions like "add reg, reg, reg" (reversed encoding).
634 class BinOpRR_Rev<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo>
635   : ITy<opcode, MRMSrcReg, typeinfo,
636         (outs typeinfo.RegClass:$dst),
637         (ins typeinfo.RegClass:$src1, typeinfo.RegClass:$src2),
638         mnemonic, "{$src2, $dst|$dst, $src2}", []> {
639   // The disassembler should know about this, but not the asmparser.
640   let isCodeGenOnly = 1;
641 }
642
643 // BinOpRM - Instructions like "add reg, reg, [mem]".
644 class BinOpRM<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
645               dag outlist, list<dag> pattern>
646   : ITy<opcode, MRMSrcMem, typeinfo, outlist,
647         (ins typeinfo.RegClass:$src1, typeinfo.MemOperand:$src2),
648         mnemonic, "{$src2, $src1|$src1, $src2}", pattern>;
649
650 // BinOpRM_R - Instructions like "add reg, reg, [mem]".
651 class BinOpRM_R<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
652               SDNode opnode>
653   : BinOpRM<opcode, mnemonic, typeinfo, (outs typeinfo.RegClass:$dst),
654             [(set typeinfo.RegClass:$dst,
655             (opnode typeinfo.RegClass:$src1, (typeinfo.LoadNode addr:$src2)))]>;
656
657 // BinOpRM_F - Instructions like "cmp reg, [mem]".
658 class BinOpRM_F<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
659               SDPatternOperator opnode>
660   : BinOpRM<opcode, mnemonic, typeinfo, (outs),
661             [(set EFLAGS,
662             (opnode typeinfo.RegClass:$src1, (typeinfo.LoadNode addr:$src2)))]>;
663
664 // BinOpRM_RF - Instructions like "add reg, reg, [mem]".
665 class BinOpRM_RF<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
666                  SDNode opnode>
667   : BinOpRM<opcode, mnemonic, typeinfo, (outs typeinfo.RegClass:$dst),
668             [(set typeinfo.RegClass:$dst, EFLAGS,
669             (opnode typeinfo.RegClass:$src1, (typeinfo.LoadNode addr:$src2)))]>;
670
671 // BinOpRI - Instructions like "add reg, reg, imm".
672 class BinOpRI<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
673               Format f, dag outlist, list<dag> pattern>
674   : ITy<opcode, f, typeinfo, outlist,
675         (ins typeinfo.RegClass:$src1, typeinfo.ImmOperand:$src2),
676         mnemonic, "{$src2, $src1|$src1, $src2}", pattern> {
677   let ImmT = typeinfo.ImmEncoding;
678 }
679
680 // BinOpRI_R - Instructions like "add reg, reg, imm".
681 class BinOpRI_R<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
682                 SDNode opnode, Format f>
683   : BinOpRI<opcode, mnemonic, typeinfo, f, (outs typeinfo.RegClass:$dst),
684             [(set typeinfo.RegClass:$dst,
685                 (opnode typeinfo.RegClass:$src1, typeinfo.ImmOperator:$src2))]>;
686
687 // BinOpRI_F - Instructions like "cmp reg, imm".
688 class BinOpRI_F<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
689                 SDPatternOperator opnode, Format f>
690   : BinOpRI<opcode, mnemonic, typeinfo, f, (outs),
691             [(set EFLAGS,
692                 (opnode typeinfo.RegClass:$src1, typeinfo.ImmOperator:$src2))]>;
693
694 // BinOpRI_RF - Instructions like "add reg, reg, imm".
695 class BinOpRI_RF<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
696                  SDNode opnode, Format f>
697   : BinOpRI<opcode, mnemonic, typeinfo, f, (outs typeinfo.RegClass:$dst),
698             [(set typeinfo.RegClass:$dst, EFLAGS, 
699                 (opnode typeinfo.RegClass:$src1, typeinfo.ImmOperator:$src2))]>;
700
701 // BinOpRI8 - Instructions like "add reg, reg, imm8".
702 class BinOpRI8<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
703                Format f, dag outlist, list<dag> pattern>
704   : ITy<opcode, f, typeinfo, outlist,
705         (ins typeinfo.RegClass:$src1, typeinfo.Imm8Operand:$src2),
706         mnemonic, "{$src2, $src1|$src1, $src2}", pattern> {
707   let ImmT = Imm8; // Always 8-bit immediate.
708 }
709
710 // BinOpRI8_R - Instructions like "add reg, reg, imm8".
711 class BinOpRI8_R<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
712                   SDNode opnode, Format f>
713   : BinOpRI8<opcode, mnemonic, typeinfo, f, (outs typeinfo.RegClass:$dst),
714              [(set typeinfo.RegClass:$dst,
715                (opnode typeinfo.RegClass:$src1, typeinfo.Imm8Operator:$src2))]>;
716                
717 // BinOpRI8_F - Instructions like "cmp reg, imm8".
718 class BinOpRI8_F<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
719                   SDNode opnode, Format f>
720   : BinOpRI8<opcode, mnemonic, typeinfo, f, (outs),
721              [(set EFLAGS,
722                (opnode typeinfo.RegClass:$src1, typeinfo.Imm8Operator:$src2))]>;
723
724 // BinOpRI8_RF - Instructions like "add reg, reg, imm8".
725 class BinOpRI8_RF<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
726                   SDNode opnode, Format f>
727   : BinOpRI8<opcode, mnemonic, typeinfo, f, (outs typeinfo.RegClass:$dst),
728              [(set typeinfo.RegClass:$dst, EFLAGS,
729                (opnode typeinfo.RegClass:$src1, typeinfo.Imm8Operator:$src2))]>;
730
731 // BinOpMR - Instructions like "add [mem], reg".
732 class BinOpMR<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
733               list<dag> pattern>
734   : ITy<opcode, MRMDestMem, typeinfo,
735         (outs), (ins typeinfo.MemOperand:$dst, typeinfo.RegClass:$src),
736         mnemonic, "{$src, $dst|$dst, $src}", pattern>;
737
738 // BinOpMR_RMW - Instructions like "add [mem], reg".
739 class BinOpMR_RMW<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
740                   SDNode opnode>
741   : BinOpMR<opcode, mnemonic, typeinfo,
742           [(store (opnode (load addr:$dst), typeinfo.RegClass:$src), addr:$dst),
743            (implicit EFLAGS)]>;
744
745 // BinOpMR_F - Instructions like "cmp [mem], reg".
746 class BinOpMR_F<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
747                   SDNode opnode>
748   : BinOpMR<opcode, mnemonic, typeinfo,
749             [(set EFLAGS, (opnode (load addr:$dst), typeinfo.RegClass:$src))]>;
750
751 // BinOpMI - Instructions like "add [mem], imm".
752 class BinOpMI<string mnemonic, X86TypeInfo typeinfo,
753               Format f, list<dag> pattern, bits<8> opcode = 0x80>
754   : ITy<opcode, f, typeinfo,
755         (outs), (ins typeinfo.MemOperand:$dst, typeinfo.ImmOperand:$src),
756         mnemonic, "{$src, $dst|$dst, $src}", pattern> {
757   let ImmT = typeinfo.ImmEncoding;
758 }
759
760 // BinOpMI_RMW - Instructions like "add [mem], imm".
761 class BinOpMI_RMW<string mnemonic, X86TypeInfo typeinfo,
762                   SDNode opnode, Format f>
763   : BinOpMI<mnemonic, typeinfo, f, 
764             [(store (opnode (typeinfo.VT (load addr:$dst)),
765                             typeinfo.ImmOperator:$src), addr:$dst),
766              (implicit EFLAGS)]>;
767
768 // BinOpMI_F - Instructions like "cmp [mem], imm".
769 class BinOpMI_F<string mnemonic, X86TypeInfo typeinfo,
770                 SDPatternOperator opnode, Format f, bits<8> opcode = 0x80>
771   : BinOpMI<mnemonic, typeinfo, f, 
772             [(set EFLAGS, (opnode (typeinfo.VT (load addr:$dst)),
773                                                typeinfo.ImmOperator:$src))],
774             opcode>;
775
776 // BinOpMI8 - Instructions like "add [mem], imm8".
777 class BinOpMI8<string mnemonic, X86TypeInfo typeinfo,
778                Format f, list<dag> pattern>
779   : ITy<0x82, f, typeinfo,
780         (outs), (ins typeinfo.MemOperand:$dst, typeinfo.Imm8Operand:$src),
781         mnemonic, "{$src, $dst|$dst, $src}", pattern> {
782   let ImmT = Imm8; // Always 8-bit immediate.
783 }
784
785 // BinOpMI8_RMW - Instructions like "add [mem], imm8".
786 class BinOpMI8_RMW<string mnemonic, X86TypeInfo typeinfo,
787                    SDNode opnode, Format f>
788   : BinOpMI8<mnemonic, typeinfo, f,
789              [(store (opnode (load addr:$dst),
790                              typeinfo.Imm8Operator:$src), addr:$dst),
791               (implicit EFLAGS)]>;
792
793 // BinOpMI8_F - Instructions like "cmp [mem], imm8".
794 class BinOpMI8_F<string mnemonic, X86TypeInfo typeinfo,
795                  SDNode opnode, Format f>
796   : BinOpMI8<mnemonic, typeinfo, f,
797              [(set EFLAGS, (opnode (load addr:$dst),
798                                    typeinfo.Imm8Operator:$src))]>;
799
800 // BinOpAI - Instructions like "add %eax, %eax, imm".
801 class BinOpAI<bits<8> opcode, string mnemonic, X86TypeInfo typeinfo,
802               Register areg>
803   : ITy<opcode, RawFrm, typeinfo,
804         (outs), (ins typeinfo.ImmOperand:$src),
805         mnemonic, !strconcat("{$src, %", areg.AsmName, "|%",
806                                areg.AsmName, ", $src}"), []> {
807   let ImmT = typeinfo.ImmEncoding;
808   let Uses = [areg];
809   let Defs = [areg];
810 }
811
812 /// ArithBinOp_RF - This is an arithmetic binary operator where the pattern is
813 /// defined with "(set GPR:$dst, EFLAGS, (...".
814 ///
815 /// It would be nice to get rid of the second and third argument here, but
816 /// tblgen can't handle dependent type references aggressively enough: PR8330
817 multiclass ArithBinOp_RF<bits<8> BaseOpc, bits<8> BaseOpc2, bits<8> BaseOpc4,
818                          string mnemonic, Format RegMRM, Format MemMRM,
819                          SDNode opnodeflag, SDNode opnode,
820                          bit CommutableRR, bit ConvertibleToThreeAddress> {
821   let Defs = [EFLAGS] in {
822     let Constraints = "$src1 = $dst" in {
823       let isCommutable = CommutableRR,
824           isConvertibleToThreeAddress = ConvertibleToThreeAddress in {
825         def #NAME#8rr  : BinOpRR_RF<BaseOpc, mnemonic, Xi8 , opnodeflag>;
826         def #NAME#16rr : BinOpRR_RF<BaseOpc, mnemonic, Xi16, opnodeflag>;
827         def #NAME#32rr : BinOpRR_RF<BaseOpc, mnemonic, Xi32, opnodeflag>;
828         def #NAME#64rr : BinOpRR_RF<BaseOpc, mnemonic, Xi64, opnodeflag>;
829       } // isCommutable
830
831       def #NAME#8rr_REV  : BinOpRR_Rev<BaseOpc2, mnemonic, Xi8>;
832       def #NAME#16rr_REV : BinOpRR_Rev<BaseOpc2, mnemonic, Xi16>;
833       def #NAME#32rr_REV : BinOpRR_Rev<BaseOpc2, mnemonic, Xi32>;
834       def #NAME#64rr_REV : BinOpRR_Rev<BaseOpc2, mnemonic, Xi64>;
835
836       def #NAME#8rm   : BinOpRM_RF<BaseOpc2, mnemonic, Xi8 , opnodeflag>;
837       def #NAME#16rm  : BinOpRM_RF<BaseOpc2, mnemonic, Xi16, opnodeflag>;
838       def #NAME#32rm  : BinOpRM_RF<BaseOpc2, mnemonic, Xi32, opnodeflag>;
839       def #NAME#64rm  : BinOpRM_RF<BaseOpc2, mnemonic, Xi64, opnodeflag>;
840
841       let isConvertibleToThreeAddress = ConvertibleToThreeAddress in {
842         def #NAME#8ri   : BinOpRI_RF<0x80, mnemonic, Xi8 , opnodeflag, RegMRM>;
843         def #NAME#16ri  : BinOpRI_RF<0x80, mnemonic, Xi16, opnodeflag, RegMRM>;
844         def #NAME#32ri  : BinOpRI_RF<0x80, mnemonic, Xi32, opnodeflag, RegMRM>;
845         def #NAME#64ri32: BinOpRI_RF<0x80, mnemonic, Xi64, opnodeflag, RegMRM>;
846
847         def #NAME#16ri8 : BinOpRI8_RF<0x82, mnemonic, Xi16, opnodeflag, RegMRM>;
848         def #NAME#32ri8 : BinOpRI8_RF<0x82, mnemonic, Xi32, opnodeflag, RegMRM>;
849         def #NAME#64ri8 : BinOpRI8_RF<0x82, mnemonic, Xi64, opnodeflag, RegMRM>;
850       }
851     } // Constraints = "$src1 = $dst"
852
853     def #NAME#8mr    : BinOpMR_RMW<BaseOpc, mnemonic, Xi8 , opnode>;
854     def #NAME#16mr   : BinOpMR_RMW<BaseOpc, mnemonic, Xi16, opnode>;
855     def #NAME#32mr   : BinOpMR_RMW<BaseOpc, mnemonic, Xi32, opnode>;
856     def #NAME#64mr   : BinOpMR_RMW<BaseOpc, mnemonic, Xi64, opnode>;
857
858     def #NAME#8mi    : BinOpMI_RMW<mnemonic, Xi8 , opnode, MemMRM>;
859     def #NAME#16mi   : BinOpMI_RMW<mnemonic, Xi16, opnode, MemMRM>;
860     def #NAME#32mi   : BinOpMI_RMW<mnemonic, Xi32, opnode, MemMRM>;
861     def #NAME#64mi32 : BinOpMI_RMW<mnemonic, Xi64, opnode, MemMRM>;
862
863     def #NAME#16mi8  : BinOpMI8_RMW<mnemonic, Xi16, opnode, MemMRM>;
864     def #NAME#32mi8  : BinOpMI8_RMW<mnemonic, Xi32, opnode, MemMRM>;
865     def #NAME#64mi8  : BinOpMI8_RMW<mnemonic, Xi64, opnode, MemMRM>;
866                        
867     def #NAME#8i8   : BinOpAI<BaseOpc4, mnemonic, Xi8 , AL>;
868     def #NAME#16i16 : BinOpAI<BaseOpc4, mnemonic, Xi16, AX>;
869     def #NAME#32i32 : BinOpAI<BaseOpc4, mnemonic, Xi32, EAX>;
870     def #NAME#64i32 : BinOpAI<BaseOpc4, mnemonic, Xi64, RAX>;
871   }                          
872 }
873
874 /// ArithBinOp_R - This is an arithmetic binary operator where the pattern is
875 /// defined with "(set GPR:$dst, (...".  It would be really nice to find a way
876 /// to factor this with the other ArithBinOp_*.
877 ///
878 multiclass ArithBinOp_R<bits<8> BaseOpc, bits<8> BaseOpc2, bits<8> BaseOpc4,
879                         string mnemonic, Format RegMRM, Format MemMRM,
880                         SDNode opnode,
881                         bit CommutableRR, bit ConvertibleToThreeAddress> {
882   let Defs = [EFLAGS] in {
883     let Constraints = "$src1 = $dst" in {
884       let isCommutable = CommutableRR,
885           isConvertibleToThreeAddress = ConvertibleToThreeAddress in {
886         def #NAME#8rr  : BinOpRR_R<BaseOpc, mnemonic, Xi8 , opnode>;
887         def #NAME#16rr : BinOpRR_R<BaseOpc, mnemonic, Xi16, opnode>;
888         def #NAME#32rr : BinOpRR_R<BaseOpc, mnemonic, Xi32, opnode>;
889         def #NAME#64rr : BinOpRR_R<BaseOpc, mnemonic, Xi64, opnode>;
890       } // isCommutable
891
892       def #NAME#8rr_REV  : BinOpRR_Rev<BaseOpc2, mnemonic, Xi8>;
893       def #NAME#16rr_REV : BinOpRR_Rev<BaseOpc2, mnemonic, Xi16>;
894       def #NAME#32rr_REV : BinOpRR_Rev<BaseOpc2, mnemonic, Xi32>;
895       def #NAME#64rr_REV : BinOpRR_Rev<BaseOpc2, mnemonic, Xi64>;
896
897       def #NAME#8rm   : BinOpRM_R<BaseOpc2, mnemonic, Xi8 , opnode>;
898       def #NAME#16rm  : BinOpRM_R<BaseOpc2, mnemonic, Xi16, opnode>;
899       def #NAME#32rm  : BinOpRM_R<BaseOpc2, mnemonic, Xi32, opnode>;
900       def #NAME#64rm  : BinOpRM_R<BaseOpc2, mnemonic, Xi64, opnode>;
901
902       let isConvertibleToThreeAddress = ConvertibleToThreeAddress in {
903         def #NAME#8ri   : BinOpRI_R<0x80, mnemonic, Xi8 , opnode, RegMRM>;
904         def #NAME#16ri  : BinOpRI_R<0x80, mnemonic, Xi16, opnode, RegMRM>;
905         def #NAME#32ri  : BinOpRI_R<0x80, mnemonic, Xi32, opnode, RegMRM>;
906         def #NAME#64ri32: BinOpRI_R<0x80, mnemonic, Xi64, opnode, RegMRM>;
907
908         def #NAME#16ri8 : BinOpRI8_R<0x82, mnemonic, Xi16, opnode, RegMRM>;
909         def #NAME#32ri8 : BinOpRI8_R<0x82, mnemonic, Xi32, opnode, RegMRM>;
910         def #NAME#64ri8 : BinOpRI8_R<0x82, mnemonic, Xi64, opnode, RegMRM>;
911       }
912     } // Constraints = "$src1 = $dst"
913
914     def #NAME#8mr    : BinOpMR_RMW<BaseOpc, mnemonic, Xi8 , opnode>;
915     def #NAME#16mr   : BinOpMR_RMW<BaseOpc, mnemonic, Xi16, opnode>;
916     def #NAME#32mr   : BinOpMR_RMW<BaseOpc, mnemonic, Xi32, opnode>;
917     def #NAME#64mr   : BinOpMR_RMW<BaseOpc, mnemonic, Xi64, opnode>;
918
919     def #NAME#8mi    : BinOpMI_RMW<mnemonic, Xi8 , opnode, MemMRM>;
920     def #NAME#16mi   : BinOpMI_RMW<mnemonic, Xi16, opnode, MemMRM>;
921     def #NAME#32mi   : BinOpMI_RMW<mnemonic, Xi32, opnode, MemMRM>;
922     def #NAME#64mi32 : BinOpMI_RMW<mnemonic, Xi64, opnode, MemMRM>;
923
924     def #NAME#16mi8  : BinOpMI8_RMW<mnemonic, Xi16, opnode, MemMRM>;
925     def #NAME#32mi8  : BinOpMI8_RMW<mnemonic, Xi32, opnode, MemMRM>;
926     def #NAME#64mi8  : BinOpMI8_RMW<mnemonic, Xi64, opnode, MemMRM>;
927                        
928     def #NAME#8i8   : BinOpAI<BaseOpc4, mnemonic, Xi8 , AL>;
929     def #NAME#16i16 : BinOpAI<BaseOpc4, mnemonic, Xi16, AX>;
930     def #NAME#32i32 : BinOpAI<BaseOpc4, mnemonic, Xi32, EAX>;
931     def #NAME#64i32 : BinOpAI<BaseOpc4, mnemonic, Xi64, RAX>;
932   }                          
933 }
934
935 /// ArithBinOp_F - This is an arithmetic binary operator where the pattern is
936 /// defined with "(set EFLAGS, (...".  It would be really nice to find a way
937 /// to factor this with the other ArithBinOp_*.
938 ///
939 multiclass ArithBinOp_F<bits<8> BaseOpc, bits<8> BaseOpc2, bits<8> BaseOpc4,
940                         string mnemonic, Format RegMRM, Format MemMRM,
941                         SDNode opnode,
942                         bit CommutableRR, bit ConvertibleToThreeAddress> {
943   let Defs = [EFLAGS] in {
944     let isCommutable = CommutableRR,
945         isConvertibleToThreeAddress = ConvertibleToThreeAddress in {
946       def #NAME#8rr  : BinOpRR_F<BaseOpc, mnemonic, Xi8 , opnode>;
947       def #NAME#16rr : BinOpRR_F<BaseOpc, mnemonic, Xi16, opnode>;
948       def #NAME#32rr : BinOpRR_F<BaseOpc, mnemonic, Xi32, opnode>;
949       def #NAME#64rr : BinOpRR_F<BaseOpc, mnemonic, Xi64, opnode>;
950     } // isCommutable
951
952     def #NAME#8rr_REV  : BinOpRR_Rev<BaseOpc2, mnemonic, Xi8>;
953     def #NAME#16rr_REV : BinOpRR_Rev<BaseOpc2, mnemonic, Xi16>;
954     def #NAME#32rr_REV : BinOpRR_Rev<BaseOpc2, mnemonic, Xi32>;
955     def #NAME#64rr_REV : BinOpRR_Rev<BaseOpc2, mnemonic, Xi64>;
956
957     def #NAME#8rm   : BinOpRM_F<BaseOpc2, mnemonic, Xi8 , opnode>;
958     def #NAME#16rm  : BinOpRM_F<BaseOpc2, mnemonic, Xi16, opnode>;
959     def #NAME#32rm  : BinOpRM_F<BaseOpc2, mnemonic, Xi32, opnode>;
960     def #NAME#64rm  : BinOpRM_F<BaseOpc2, mnemonic, Xi64, opnode>;
961
962     let isConvertibleToThreeAddress = ConvertibleToThreeAddress in {
963       def #NAME#8ri   : BinOpRI_F<0x80, mnemonic, Xi8 , opnode, RegMRM>;
964       def #NAME#16ri  : BinOpRI_F<0x80, mnemonic, Xi16, opnode, RegMRM>;
965       def #NAME#32ri  : BinOpRI_F<0x80, mnemonic, Xi32, opnode, RegMRM>;
966       def #NAME#64ri32: BinOpRI_F<0x80, mnemonic, Xi64, opnode, RegMRM>;
967
968       def #NAME#16ri8 : BinOpRI8_F<0x82, mnemonic, Xi16, opnode, RegMRM>;
969       def #NAME#32ri8 : BinOpRI8_F<0x82, mnemonic, Xi32, opnode, RegMRM>;
970       def #NAME#64ri8 : BinOpRI8_F<0x82, mnemonic, Xi64, opnode, RegMRM>;
971     }
972
973     def #NAME#8mr    : BinOpMR_F<BaseOpc, mnemonic, Xi8 , opnode>;
974     def #NAME#16mr   : BinOpMR_F<BaseOpc, mnemonic, Xi16, opnode>;
975     def #NAME#32mr   : BinOpMR_F<BaseOpc, mnemonic, Xi32, opnode>;
976     def #NAME#64mr   : BinOpMR_F<BaseOpc, mnemonic, Xi64, opnode>;
977
978     def #NAME#8mi    : BinOpMI_F<mnemonic, Xi8 , opnode, MemMRM>;
979     def #NAME#16mi   : BinOpMI_F<mnemonic, Xi16, opnode, MemMRM>;
980     def #NAME#32mi   : BinOpMI_F<mnemonic, Xi32, opnode, MemMRM>;
981     def #NAME#64mi32 : BinOpMI_F<mnemonic, Xi64, opnode, MemMRM>;
982
983     def #NAME#16mi8  : BinOpMI8_F<mnemonic, Xi16, opnode, MemMRM>;
984     def #NAME#32mi8  : BinOpMI8_F<mnemonic, Xi32, opnode, MemMRM>;
985     def #NAME#64mi8  : BinOpMI8_F<mnemonic, Xi64, opnode, MemMRM>;
986                        
987     def #NAME#8i8   : BinOpAI<BaseOpc4, mnemonic, Xi8 , AL>;
988     def #NAME#16i16 : BinOpAI<BaseOpc4, mnemonic, Xi16, AX>;
989     def #NAME#32i32 : BinOpAI<BaseOpc4, mnemonic, Xi32, EAX>;
990     def #NAME#64i32 : BinOpAI<BaseOpc4, mnemonic, Xi64, RAX>;
991   }                          
992 }
993
994
995 defm AND : ArithBinOp_RF<0x20, 0x22, 0x24, "and", MRM4r, MRM4m,
996                          X86and_flag, and, 1, 0>;
997 defm OR  : ArithBinOp_RF<0x08, 0x0A, 0x0C, "or", MRM1r, MRM1m,
998                          X86or_flag, or, 1, 0>;
999 defm XOR : ArithBinOp_RF<0x30, 0x32, 0x34, "xor", MRM6r, MRM6m,
1000                          X86xor_flag, xor, 1, 0>;
1001 defm ADD : ArithBinOp_RF<0x00, 0x02, 0x04, "add", MRM0r, MRM0m,
1002                          X86add_flag, add, 1, 1>;
1003 defm SUB : ArithBinOp_RF<0x28, 0x2A, 0x2C, "sub", MRM5r, MRM5m,
1004                          X86sub_flag, sub, 0, 0>;
1005
1006 // Arithmetic.
1007 let Uses = [EFLAGS] in {
1008   // FIXME: Delete ArithBinOp_R if these switch off adde/sube.
1009   defm ADC : ArithBinOp_R<0x10, 0x12, 0x14, "adc", MRM2r, MRM2m, adde, 1, 0>;
1010   defm SBB : ArithBinOp_R<0x18, 0x1A, 0x1C, "sbb", MRM3r, MRM3m, sube, 0, 0>;
1011 }
1012
1013
1014
1015 defm CMP : ArithBinOp_F<0x38, 0x3A, 0x3C, "cmp", MRM7r, MRM7m, X86cmp, 0, 0>;
1016
1017
1018 //===----------------------------------------------------------------------===//
1019 // Semantically, test instructions are similar like AND, except they don't
1020 // generate a result.  From an encoding perspective, they are very different:
1021 // they don't have all the usual imm8 and REV forms, and are encoded into a
1022 // different space.
1023 def X86testpat : PatFrag<(ops node:$lhs, node:$rhs),
1024                          (X86cmp (and_su node:$lhs, node:$rhs), 0)>;
1025
1026 let Defs = [EFLAGS] in {
1027   let isCommutable = 1 in {
1028     def TEST8rr  : BinOpRR_F<0x84, "test", Xi8 , X86testpat, MRMSrcReg>;
1029     def TEST16rr : BinOpRR_F<0x84, "test", Xi16, X86testpat, MRMSrcReg>;
1030     def TEST32rr : BinOpRR_F<0x84, "test", Xi32, X86testpat, MRMSrcReg>;
1031     def TEST64rr : BinOpRR_F<0x84, "test", Xi64, X86testpat, MRMSrcReg>;
1032   } // isCommutable
1033
1034   def TEST8rm    : BinOpRM_F<0x84, "test", Xi8 , X86testpat>;
1035   def TEST16rm   : BinOpRM_F<0x84, "test", Xi16, X86testpat>;
1036   def TEST32rm   : BinOpRM_F<0x84, "test", Xi32, X86testpat>;
1037   def TEST64rm   : BinOpRM_F<0x84, "test", Xi64, X86testpat>;
1038
1039   def TEST8ri    : BinOpRI_F<0xF6, "test", Xi8 , X86testpat, MRM0r>;
1040   def TEST16ri   : BinOpRI_F<0xF6, "test", Xi16, X86testpat, MRM0r>;
1041   def TEST32ri   : BinOpRI_F<0xF6, "test", Xi32, X86testpat, MRM0r>;
1042   def TEST64ri32 : BinOpRI_F<0xF6, "test", Xi64, X86testpat, MRM0r>;
1043
1044   def TEST8mi    : BinOpMI_F<"test", Xi8 , X86testpat, MRM0m, 0xF6>;
1045   def TEST16mi   : BinOpMI_F<"test", Xi16, X86testpat, MRM0m, 0xF6>;
1046   def TEST32mi   : BinOpMI_F<"test", Xi32, X86testpat, MRM0m, 0xF6>;
1047   def TEST64mi32 : BinOpMI_F<"test", Xi64, X86testpat, MRM0m, 0xF6>;
1048                      
1049   def TEST8i8    : BinOpAI<0xA8, "test", Xi8 , AL>;
1050   def TEST16i16  : BinOpAI<0xA8, "test", Xi16, AX>;
1051   def TEST32i32  : BinOpAI<0xA8, "test", Xi32, EAX>;
1052   def TEST64i32  : BinOpAI<0xA8, "test", Xi64, RAX>;
1053 }                          
1054