Add AVX compare packed instructions
[oota-llvm.git] / lib / Target / X86 / X86InstrSSE.td
1 //====- X86InstrSSE.td - Describe the X86 Instruction Set --*- tablegen -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes the X86 SSE instruction set, defining the instructions,
11 // and properties of the instructions which are needed for code generation,
12 // machine code emission, and analysis.
13 //
14 //===----------------------------------------------------------------------===//
15
16
17 //===----------------------------------------------------------------------===//
18 // SSE specific DAG Nodes.
19 //===----------------------------------------------------------------------===//
20
21 def SDTX86FPShiftOp : SDTypeProfile<1, 2, [ SDTCisSameAs<0, 1>,
22                                             SDTCisFP<0>, SDTCisInt<2> ]>;
23 def SDTX86VFCMP : SDTypeProfile<1, 3, [SDTCisInt<0>, SDTCisSameAs<1, 2>,
24                                        SDTCisFP<1>, SDTCisVT<3, i8>]>;
25
26 def X86fmin    : SDNode<"X86ISD::FMIN",      SDTFPBinOp>;
27 def X86fmax    : SDNode<"X86ISD::FMAX",      SDTFPBinOp>;
28 def X86fand    : SDNode<"X86ISD::FAND",      SDTFPBinOp,
29                         [SDNPCommutative, SDNPAssociative]>;
30 def X86for     : SDNode<"X86ISD::FOR",       SDTFPBinOp,
31                         [SDNPCommutative, SDNPAssociative]>;
32 def X86fxor    : SDNode<"X86ISD::FXOR",      SDTFPBinOp,
33                         [SDNPCommutative, SDNPAssociative]>;
34 def X86frsqrt  : SDNode<"X86ISD::FRSQRT",    SDTFPUnaryOp>;
35 def X86frcp    : SDNode<"X86ISD::FRCP",      SDTFPUnaryOp>;
36 def X86fsrl    : SDNode<"X86ISD::FSRL",      SDTX86FPShiftOp>;
37 def X86comi    : SDNode<"X86ISD::COMI",      SDTX86CmpTest>;
38 def X86ucomi   : SDNode<"X86ISD::UCOMI",     SDTX86CmpTest>;
39 def X86pshufb  : SDNode<"X86ISD::PSHUFB",
40                  SDTypeProfile<1, 2, [SDTCisVT<0, v16i8>, SDTCisSameAs<0,1>,
41                                       SDTCisSameAs<0,2>]>>;
42 def X86pextrb  : SDNode<"X86ISD::PEXTRB",
43                  SDTypeProfile<1, 2, [SDTCisVT<0, i32>, SDTCisPtrTy<2>]>>;
44 def X86pextrw  : SDNode<"X86ISD::PEXTRW",
45                  SDTypeProfile<1, 2, [SDTCisVT<0, i32>, SDTCisPtrTy<2>]>>;
46 def X86pinsrb  : SDNode<"X86ISD::PINSRB",
47                  SDTypeProfile<1, 3, [SDTCisVT<0, v16i8>, SDTCisSameAs<0,1>,
48                                       SDTCisVT<2, i32>, SDTCisPtrTy<3>]>>;
49 def X86pinsrw  : SDNode<"X86ISD::PINSRW",
50                  SDTypeProfile<1, 3, [SDTCisVT<0, v8i16>, SDTCisSameAs<0,1>,
51                                       SDTCisVT<2, i32>, SDTCisPtrTy<3>]>>;
52 def X86insrtps : SDNode<"X86ISD::INSERTPS",
53                  SDTypeProfile<1, 3, [SDTCisVT<0, v4f32>, SDTCisSameAs<0,1>,
54                                       SDTCisVT<2, v4f32>, SDTCisPtrTy<3>]>>;
55 def X86vzmovl  : SDNode<"X86ISD::VZEXT_MOVL",
56                  SDTypeProfile<1, 1, [SDTCisSameAs<0,1>]>>;
57 def X86vzload  : SDNode<"X86ISD::VZEXT_LOAD", SDTLoad,
58                         [SDNPHasChain, SDNPMayLoad]>;
59 def X86vshl    : SDNode<"X86ISD::VSHL",      SDTIntShiftOp>;
60 def X86vshr    : SDNode<"X86ISD::VSRL",      SDTIntShiftOp>;
61 def X86cmpps   : SDNode<"X86ISD::CMPPS",     SDTX86VFCMP>;
62 def X86cmppd   : SDNode<"X86ISD::CMPPD",     SDTX86VFCMP>;
63 def X86pcmpeqb : SDNode<"X86ISD::PCMPEQB", SDTIntBinOp, [SDNPCommutative]>;
64 def X86pcmpeqw : SDNode<"X86ISD::PCMPEQW", SDTIntBinOp, [SDNPCommutative]>;
65 def X86pcmpeqd : SDNode<"X86ISD::PCMPEQD", SDTIntBinOp, [SDNPCommutative]>;
66 def X86pcmpeqq : SDNode<"X86ISD::PCMPEQQ", SDTIntBinOp, [SDNPCommutative]>;
67 def X86pcmpgtb : SDNode<"X86ISD::PCMPGTB", SDTIntBinOp>;
68 def X86pcmpgtw : SDNode<"X86ISD::PCMPGTW", SDTIntBinOp>;
69 def X86pcmpgtd : SDNode<"X86ISD::PCMPGTD", SDTIntBinOp>;
70 def X86pcmpgtq : SDNode<"X86ISD::PCMPGTQ", SDTIntBinOp>;
71
72 def SDTX86CmpPTest : SDTypeProfile<1, 2, [SDTCisVT<0, i32>,
73                                           SDTCisVT<1, v4f32>,
74                                           SDTCisVT<2, v4f32>]>;
75 def X86ptest   : SDNode<"X86ISD::PTEST", SDTX86CmpPTest>;
76
77 //===----------------------------------------------------------------------===//
78 // SSE Complex Patterns
79 //===----------------------------------------------------------------------===//
80
81 // These are 'extloads' from a scalar to the low element of a vector, zeroing
82 // the top elements.  These are used for the SSE 'ss' and 'sd' instruction
83 // forms.
84 def sse_load_f32 : ComplexPattern<v4f32, 5, "SelectScalarSSELoad", [],
85                                   [SDNPHasChain, SDNPMayLoad]>;
86 def sse_load_f64 : ComplexPattern<v2f64, 5, "SelectScalarSSELoad", [],
87                                   [SDNPHasChain, SDNPMayLoad]>;
88
89 def ssmem : Operand<v4f32> {
90   let PrintMethod = "printf32mem";
91   let MIOperandInfo = (ops ptr_rc, i8imm, ptr_rc_nosp, i32imm, i8imm);
92   let ParserMatchClass = X86MemAsmOperand;
93 }
94 def sdmem : Operand<v2f64> {
95   let PrintMethod = "printf64mem";
96   let MIOperandInfo = (ops ptr_rc, i8imm, ptr_rc_nosp, i32imm, i8imm);
97   let ParserMatchClass = X86MemAsmOperand;
98 }
99
100 //===----------------------------------------------------------------------===//
101 // SSE pattern fragments
102 //===----------------------------------------------------------------------===//
103
104 def loadv4f32    : PatFrag<(ops node:$ptr), (v4f32 (load node:$ptr))>;
105 def loadv2f64    : PatFrag<(ops node:$ptr), (v2f64 (load node:$ptr))>;
106 def loadv4i32    : PatFrag<(ops node:$ptr), (v4i32 (load node:$ptr))>;
107 def loadv2i64    : PatFrag<(ops node:$ptr), (v2i64 (load node:$ptr))>;
108
109 // Like 'store', but always requires vector alignment.
110 def alignedstore : PatFrag<(ops node:$val, node:$ptr),
111                            (store node:$val, node:$ptr), [{
112   return cast<StoreSDNode>(N)->getAlignment() >= 16;
113 }]>;
114
115 // Like 'load', but always requires vector alignment.
116 def alignedload : PatFrag<(ops node:$ptr), (load node:$ptr), [{
117   return cast<LoadSDNode>(N)->getAlignment() >= 16;
118 }]>;
119
120 def alignedloadfsf32 : PatFrag<(ops node:$ptr),
121                                (f32 (alignedload node:$ptr))>;
122 def alignedloadfsf64 : PatFrag<(ops node:$ptr),
123                                (f64 (alignedload node:$ptr))>;
124 def alignedloadv4f32 : PatFrag<(ops node:$ptr),
125                                (v4f32 (alignedload node:$ptr))>;
126 def alignedloadv2f64 : PatFrag<(ops node:$ptr),
127                                (v2f64 (alignedload node:$ptr))>;
128 def alignedloadv4i32 : PatFrag<(ops node:$ptr),
129                                (v4i32 (alignedload node:$ptr))>;
130 def alignedloadv2i64 : PatFrag<(ops node:$ptr),
131                                (v2i64 (alignedload node:$ptr))>;
132
133 // Like 'load', but uses special alignment checks suitable for use in
134 // memory operands in most SSE instructions, which are required to
135 // be naturally aligned on some targets but not on others.  If the subtarget
136 // allows unaligned accesses, match any load, though this may require
137 // setting a feature bit in the processor (on startup, for example).
138 // Opteron 10h and later implement such a feature.
139 def memop : PatFrag<(ops node:$ptr), (load node:$ptr), [{
140   return    Subtarget->hasVectorUAMem()
141          || cast<LoadSDNode>(N)->getAlignment() >= 16;
142 }]>;
143
144 def memopfsf32 : PatFrag<(ops node:$ptr), (f32   (memop node:$ptr))>;
145 def memopfsf64 : PatFrag<(ops node:$ptr), (f64   (memop node:$ptr))>;
146 def memopv4f32 : PatFrag<(ops node:$ptr), (v4f32 (memop node:$ptr))>;
147 def memopv2f64 : PatFrag<(ops node:$ptr), (v2f64 (memop node:$ptr))>;
148 def memopv4i32 : PatFrag<(ops node:$ptr), (v4i32 (memop node:$ptr))>;
149 def memopv2i64 : PatFrag<(ops node:$ptr), (v2i64 (memop node:$ptr))>;
150 def memopv16i8 : PatFrag<(ops node:$ptr), (v16i8 (memop node:$ptr))>;
151
152 // SSSE3 uses MMX registers for some instructions. They aren't aligned on a
153 // 16-byte boundary.
154 // FIXME: 8 byte alignment for mmx reads is not required
155 def memop64 : PatFrag<(ops node:$ptr), (load node:$ptr), [{
156   return cast<LoadSDNode>(N)->getAlignment() >= 8;
157 }]>;
158
159 def memopv8i8  : PatFrag<(ops node:$ptr), (v8i8  (memop64 node:$ptr))>;
160 def memopv4i16 : PatFrag<(ops node:$ptr), (v4i16 (memop64 node:$ptr))>;
161 def memopv8i16 : PatFrag<(ops node:$ptr), (v8i16 (memop64 node:$ptr))>;
162 def memopv2i32 : PatFrag<(ops node:$ptr), (v2i32 (memop64 node:$ptr))>;
163
164 // MOVNT Support
165 // Like 'store', but requires the non-temporal bit to be set
166 def nontemporalstore : PatFrag<(ops node:$val, node:$ptr),
167                            (st node:$val, node:$ptr), [{
168   if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N))
169     return ST->isNonTemporal();
170   return false;
171 }]>;
172
173 def alignednontemporalstore : PatFrag<(ops node:$val, node:$ptr),
174                                    (st node:$val, node:$ptr), [{
175   if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N))
176     return ST->isNonTemporal() && !ST->isTruncatingStore() &&
177            ST->getAddressingMode() == ISD::UNINDEXED &&
178            ST->getAlignment() >= 16;
179   return false;
180 }]>;
181
182 def unalignednontemporalstore : PatFrag<(ops node:$val, node:$ptr),
183                                    (st node:$val, node:$ptr), [{
184   if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N))
185     return ST->isNonTemporal() &&
186            ST->getAlignment() < 16;
187   return false;
188 }]>;
189
190 def bc_v4f32 : PatFrag<(ops node:$in), (v4f32 (bitconvert node:$in))>;
191 def bc_v2f64 : PatFrag<(ops node:$in), (v2f64 (bitconvert node:$in))>;
192 def bc_v16i8 : PatFrag<(ops node:$in), (v16i8 (bitconvert node:$in))>;
193 def bc_v8i16 : PatFrag<(ops node:$in), (v8i16 (bitconvert node:$in))>;
194 def bc_v4i32 : PatFrag<(ops node:$in), (v4i32 (bitconvert node:$in))>;
195 def bc_v2i64 : PatFrag<(ops node:$in), (v2i64 (bitconvert node:$in))>;
196
197 def vzmovl_v2i64 : PatFrag<(ops node:$src),
198                            (bitconvert (v2i64 (X86vzmovl
199                              (v2i64 (scalar_to_vector (loadi64 node:$src))))))>;
200 def vzmovl_v4i32 : PatFrag<(ops node:$src),
201                            (bitconvert (v4i32 (X86vzmovl
202                              (v4i32 (scalar_to_vector (loadi32 node:$src))))))>;
203
204 def vzload_v2i64 : PatFrag<(ops node:$src),
205                            (bitconvert (v2i64 (X86vzload node:$src)))>;
206
207
208 def fp32imm0 : PatLeaf<(f32 fpimm), [{
209   return N->isExactlyValue(+0.0);
210 }]>;
211
212 // BYTE_imm - Transform bit immediates into byte immediates.
213 def BYTE_imm  : SDNodeXForm<imm, [{
214   // Transformation function: imm >> 3
215   return getI32Imm(N->getZExtValue() >> 3);
216 }]>;
217
218 // SHUFFLE_get_shuf_imm xform function: convert vector_shuffle mask to PSHUF*,
219 // SHUFP* etc. imm.
220 def SHUFFLE_get_shuf_imm : SDNodeXForm<vector_shuffle, [{
221   return getI8Imm(X86::getShuffleSHUFImmediate(N));
222 }]>;
223
224 // SHUFFLE_get_pshufhw_imm xform function: convert vector_shuffle mask to
225 // PSHUFHW imm.
226 def SHUFFLE_get_pshufhw_imm : SDNodeXForm<vector_shuffle, [{
227   return getI8Imm(X86::getShufflePSHUFHWImmediate(N));
228 }]>;
229
230 // SHUFFLE_get_pshuflw_imm xform function: convert vector_shuffle mask to
231 // PSHUFLW imm.
232 def SHUFFLE_get_pshuflw_imm : SDNodeXForm<vector_shuffle, [{
233   return getI8Imm(X86::getShufflePSHUFLWImmediate(N));
234 }]>;
235
236 // SHUFFLE_get_palign_imm xform function: convert vector_shuffle mask to
237 // a PALIGNR imm.
238 def SHUFFLE_get_palign_imm : SDNodeXForm<vector_shuffle, [{
239   return getI8Imm(X86::getShufflePALIGNRImmediate(N));
240 }]>;
241
242 def splat_lo : PatFrag<(ops node:$lhs, node:$rhs),
243                        (vector_shuffle node:$lhs, node:$rhs), [{
244   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
245   return SVOp->isSplat() && SVOp->getSplatIndex() == 0;
246 }]>;
247
248 def movddup : PatFrag<(ops node:$lhs, node:$rhs),
249                       (vector_shuffle node:$lhs, node:$rhs), [{
250   return X86::isMOVDDUPMask(cast<ShuffleVectorSDNode>(N));
251 }]>;
252
253 def movhlps : PatFrag<(ops node:$lhs, node:$rhs),
254                       (vector_shuffle node:$lhs, node:$rhs), [{
255   return X86::isMOVHLPSMask(cast<ShuffleVectorSDNode>(N));
256 }]>;
257
258 def movhlps_undef : PatFrag<(ops node:$lhs, node:$rhs),
259                             (vector_shuffle node:$lhs, node:$rhs), [{
260   return X86::isMOVHLPS_v_undef_Mask(cast<ShuffleVectorSDNode>(N));
261 }]>;
262
263 def movlhps : PatFrag<(ops node:$lhs, node:$rhs),
264                       (vector_shuffle node:$lhs, node:$rhs), [{
265   return X86::isMOVLHPSMask(cast<ShuffleVectorSDNode>(N));
266 }]>;
267
268 def movlp : PatFrag<(ops node:$lhs, node:$rhs),
269                     (vector_shuffle node:$lhs, node:$rhs), [{
270   return X86::isMOVLPMask(cast<ShuffleVectorSDNode>(N));
271 }]>;
272
273 def movl : PatFrag<(ops node:$lhs, node:$rhs),
274                    (vector_shuffle node:$lhs, node:$rhs), [{
275   return X86::isMOVLMask(cast<ShuffleVectorSDNode>(N));
276 }]>;
277
278 def movshdup : PatFrag<(ops node:$lhs, node:$rhs),
279                        (vector_shuffle node:$lhs, node:$rhs), [{
280   return X86::isMOVSHDUPMask(cast<ShuffleVectorSDNode>(N));
281 }]>;
282
283 def movsldup : PatFrag<(ops node:$lhs, node:$rhs),
284                        (vector_shuffle node:$lhs, node:$rhs), [{
285   return X86::isMOVSLDUPMask(cast<ShuffleVectorSDNode>(N));
286 }]>;
287
288 def unpckl : PatFrag<(ops node:$lhs, node:$rhs),
289                      (vector_shuffle node:$lhs, node:$rhs), [{
290   return X86::isUNPCKLMask(cast<ShuffleVectorSDNode>(N));
291 }]>;
292
293 def unpckh : PatFrag<(ops node:$lhs, node:$rhs),
294                      (vector_shuffle node:$lhs, node:$rhs), [{
295   return X86::isUNPCKHMask(cast<ShuffleVectorSDNode>(N));
296 }]>;
297
298 def unpckl_undef : PatFrag<(ops node:$lhs, node:$rhs),
299                            (vector_shuffle node:$lhs, node:$rhs), [{
300   return X86::isUNPCKL_v_undef_Mask(cast<ShuffleVectorSDNode>(N));
301 }]>;
302
303 def unpckh_undef : PatFrag<(ops node:$lhs, node:$rhs),
304                            (vector_shuffle node:$lhs, node:$rhs), [{
305   return X86::isUNPCKH_v_undef_Mask(cast<ShuffleVectorSDNode>(N));
306 }]>;
307
308 def pshufd : PatFrag<(ops node:$lhs, node:$rhs),
309                      (vector_shuffle node:$lhs, node:$rhs), [{
310   return X86::isPSHUFDMask(cast<ShuffleVectorSDNode>(N));
311 }], SHUFFLE_get_shuf_imm>;
312
313 def shufp : PatFrag<(ops node:$lhs, node:$rhs),
314                     (vector_shuffle node:$lhs, node:$rhs), [{
315   return X86::isSHUFPMask(cast<ShuffleVectorSDNode>(N));
316 }], SHUFFLE_get_shuf_imm>;
317
318 def pshufhw : PatFrag<(ops node:$lhs, node:$rhs),
319                       (vector_shuffle node:$lhs, node:$rhs), [{
320   return X86::isPSHUFHWMask(cast<ShuffleVectorSDNode>(N));
321 }], SHUFFLE_get_pshufhw_imm>;
322
323 def pshuflw : PatFrag<(ops node:$lhs, node:$rhs),
324                       (vector_shuffle node:$lhs, node:$rhs), [{
325   return X86::isPSHUFLWMask(cast<ShuffleVectorSDNode>(N));
326 }], SHUFFLE_get_pshuflw_imm>;
327
328 def palign : PatFrag<(ops node:$lhs, node:$rhs),
329                      (vector_shuffle node:$lhs, node:$rhs), [{
330   return X86::isPALIGNRMask(cast<ShuffleVectorSDNode>(N));
331 }], SHUFFLE_get_palign_imm>;
332
333 //===----------------------------------------------------------------------===//
334 // SSE scalar FP Instructions
335 //===----------------------------------------------------------------------===//
336
337 // CMOV* - Used to implement the SSE SELECT DAG operation.  Expanded after
338 // instruction selection into a branch sequence.
339 let Uses = [EFLAGS], usesCustomInserter = 1 in {
340   def CMOV_FR32 : I<0, Pseudo,
341                     (outs FR32:$dst), (ins FR32:$t, FR32:$f, i8imm:$cond),
342                     "#CMOV_FR32 PSEUDO!",
343                     [(set FR32:$dst, (X86cmov FR32:$t, FR32:$f, imm:$cond,
344                                                   EFLAGS))]>;
345   def CMOV_FR64 : I<0, Pseudo,
346                     (outs FR64:$dst), (ins FR64:$t, FR64:$f, i8imm:$cond),
347                     "#CMOV_FR64 PSEUDO!",
348                     [(set FR64:$dst, (X86cmov FR64:$t, FR64:$f, imm:$cond,
349                                                   EFLAGS))]>;
350   def CMOV_V4F32 : I<0, Pseudo,
351                     (outs VR128:$dst), (ins VR128:$t, VR128:$f, i8imm:$cond),
352                     "#CMOV_V4F32 PSEUDO!",
353                     [(set VR128:$dst,
354                       (v4f32 (X86cmov VR128:$t, VR128:$f, imm:$cond,
355                                           EFLAGS)))]>;
356   def CMOV_V2F64 : I<0, Pseudo,
357                     (outs VR128:$dst), (ins VR128:$t, VR128:$f, i8imm:$cond),
358                     "#CMOV_V2F64 PSEUDO!",
359                     [(set VR128:$dst,
360                       (v2f64 (X86cmov VR128:$t, VR128:$f, imm:$cond,
361                                           EFLAGS)))]>;
362   def CMOV_V2I64 : I<0, Pseudo,
363                     (outs VR128:$dst), (ins VR128:$t, VR128:$f, i8imm:$cond),
364                     "#CMOV_V2I64 PSEUDO!",
365                     [(set VR128:$dst,
366                       (v2i64 (X86cmov VR128:$t, VR128:$f, imm:$cond,
367                                           EFLAGS)))]>;
368 }
369
370 //===----------------------------------------------------------------------===//
371 // SSE 1 & 2 Instructions Classes
372 //===----------------------------------------------------------------------===//
373
374 /// sse12_fp_scalar - SSE 1 & 2 scalar instructions class
375 multiclass sse12_fp_scalar<bits<8> opc, string OpcodeStr, SDNode OpNode,
376                            RegisterClass RC, X86MemOperand x86memop> {
377   let isCommutable = 1 in {
378     def rr : SI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
379                 OpcodeStr, [(set RC:$dst, (OpNode RC:$src1, RC:$src2))]>;
380   }
381   def rm : SI<opc, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
382               OpcodeStr, [(set RC:$dst, (OpNode RC:$src1, (load addr:$src2)))]>;
383 }
384
385 /// sse12_fp_scalar_int - SSE 1 & 2 scalar instructions intrinsics class
386 multiclass sse12_fp_scalar_int<bits<8> opc, string OpcodeStr, RegisterClass RC,
387                                string asm, string SSEVer, string FPSizeStr,
388                                Operand memopr, ComplexPattern mem_cpat> {
389   def rr_Int : SI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
390                   asm, [(set RC:$dst, (
391                                 !nameconcat<Intrinsic>("int_x86_sse",
392                                 !strconcat(SSEVer, !strconcat("_",
393                                 !strconcat(OpcodeStr, FPSizeStr))))
394                          RC:$src1, RC:$src2))]>;
395   def rm_Int : SI<opc, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, memopr:$src2),
396                   asm, [(set RC:$dst, (
397                                 !nameconcat<Intrinsic>("int_x86_sse",
398                                 !strconcat(SSEVer, !strconcat("_",
399                                 !strconcat(OpcodeStr, FPSizeStr))))
400                          RC:$src1, mem_cpat:$src2))]>;
401 }
402
403 /// sse12_fp_packed - SSE 1 & 2 packed instructions class
404 multiclass sse12_fp_packed<bits<8> opc, string OpcodeStr, SDNode OpNode,
405                            RegisterClass RC, ValueType vt,
406                            X86MemOperand x86memop, PatFrag mem_frag,
407                            Domain d, bit MayLoad = 0> {
408   let isCommutable = 1 in
409     def rr : PI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
410                 OpcodeStr, [(set RC:$dst, (vt (OpNode RC:$src1, RC:$src2)))],d>;
411   let mayLoad = MayLoad in
412     def rm : PI<opc, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
413                 OpcodeStr, [(set RC:$dst, (OpNode RC:$src1,
414                                                   (mem_frag addr:$src2)))],d>;
415 }
416
417 /// sse12_fp_packed_logical_rm - SSE 1 & 2 packed instructions class
418 multiclass sse12_fp_packed_logical_rm<bits<8> opc, RegisterClass RC, Domain d,
419                                       string OpcodeStr, X86MemOperand x86memop,
420                                       list<dag> pat_rr, list<dag> pat_rm> {
421   let isCommutable = 1 in
422     def rr : PI<opc, MRMSrcReg, (outs RC:$dst),
423                 (ins RC:$src1, RC:$src2), OpcodeStr, pat_rr, d>;
424   def rm : PI<opc, MRMSrcMem, (outs RC:$dst),
425                 (ins RC:$src1, x86memop:$src2), OpcodeStr, pat_rm, d>;
426 }
427
428 /// sse12_fp_packed_int - SSE 1 & 2 packed instructions intrinsics class
429 multiclass sse12_fp_packed_int<bits<8> opc, string OpcodeStr, RegisterClass RC,
430                                string asm, string SSEVer, string FPSizeStr,
431                                X86MemOperand x86memop, PatFrag mem_frag,
432                                Domain d> {
433   def rr_Int : PI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
434                   asm, [(set RC:$dst, (
435                                 !nameconcat<Intrinsic>("int_x86_sse",
436                                 !strconcat(SSEVer, !strconcat("_",
437                                 !strconcat(OpcodeStr, FPSizeStr))))
438                          RC:$src1, RC:$src2))], d>;
439   def rm_Int : PI<opc, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
440                   asm, [(set RC:$dst, (
441                                 !nameconcat<Intrinsic>("int_x86_sse",
442                                 !strconcat(SSEVer, !strconcat("_",
443                                 !strconcat(OpcodeStr, FPSizeStr))))
444                          RC:$src1, (mem_frag addr:$src2)))], d>;
445 }
446
447 /// sse12_unpack_interleave - SSE 1 & 2 unpack and interleave
448 multiclass sse12_unpack_interleave<bits<8> opc, PatFrag OpNode, ValueType vt,
449                                    PatFrag mem_frag, RegisterClass RC,
450                                    X86MemOperand x86memop, string asm,
451                                    Domain d> {
452     def rr : PI<opc, MRMSrcReg,
453                 (outs RC:$dst), (ins RC:$src1, RC:$src2),
454                 asm, [(set RC:$dst,
455                            (vt (OpNode RC:$src1, RC:$src2)))], d>;
456     def rm : PI<opc, MRMSrcMem,
457                 (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
458                 asm, [(set RC:$dst,
459                            (vt (OpNode RC:$src1,
460                                        (mem_frag addr:$src2))))], d>;
461 }
462
463 multiclass sse12_cmp<RegisterClass RC, X86MemOperand x86memop, Intrinsic Int,
464                      string asm, Domain d, Operand sse_imm_op> {
465   def rri : PIi8<0xC2, MRMSrcReg,
466              (outs RC:$dst), (ins RC:$src1, RC:$src, sse_imm_op:$cc), asm,
467              [(set RC:$dst, (Int RC:$src1, RC:$src, imm:$cc))], d>;
468   def rmi : PIi8<0xC2, MRMSrcMem,
469              (outs RC:$dst), (ins RC:$src1, f128mem:$src, sse_imm_op:$cc), asm,
470              [(set RC:$dst, (Int RC:$src1, (memop addr:$src), imm:$cc))], d>;
471 }
472
473 // FIXME: rename instructions to only use the class above
474 multiclass sse12_cmp_alt<RegisterClass RC, string asm, Domain d,
475                          Operand sse_imm_op> {
476   def rri_alt : PIi8<0xC2, MRMSrcReg,
477             (outs RC:$dst), (ins RC:$src1, RC:$src, sse_imm_op:$src2), asm,
478             [], d>;
479   def rmi_alt : PIi8<0xC2, MRMSrcMem,
480             (outs RC:$dst), (ins RC:$src1, f128mem:$src, sse_imm_op:$src2), asm,
481             [], d>;
482 }
483
484 //===----------------------------------------------------------------------===//
485 // SSE1 Instructions
486 //===----------------------------------------------------------------------===//
487
488 // Conversion Instructions
489
490 // Match intrinsics which expect XMM operand(s).
491 def CVTSS2SIrr: SSI<0x2D, MRMSrcReg, (outs GR32:$dst), (ins FR32:$src),
492                     "cvtss2si{l}\t{$src, $dst|$dst, $src}", []>;
493 def CVTSS2SIrm: SSI<0x2D, MRMSrcMem, (outs GR32:$dst), (ins f32mem:$src),
494                     "cvtss2si{l}\t{$src, $dst|$dst, $src}", []>;
495
496 def CVTDQ2PSrr : PSI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
497                      "cvtdq2ps\t{$src, $dst|$dst, $src}", []>;
498 def CVTDQ2PSrm : PSI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
499                      "cvtdq2ps\t{$src, $dst|$dst, $src}", []>;
500
501 // Aliases for intrinsics
502 def Int_CVTTSS2SIrr : SSI<0x2C, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
503                           "cvttss2si\t{$src, $dst|$dst, $src}",
504                           [(set GR32:$dst,
505                             (int_x86_sse_cvttss2si VR128:$src))]>;
506 def Int_CVTTSS2SIrm : SSI<0x2C, MRMSrcMem, (outs GR32:$dst), (ins f32mem:$src),
507                           "cvttss2si\t{$src, $dst|$dst, $src}",
508                           [(set GR32:$dst,
509                             (int_x86_sse_cvttss2si(load addr:$src)))]>;
510
511 let Constraints = "$src1 = $dst" in {
512   def Int_CVTSI2SSrr : SSI<0x2A, MRMSrcReg,
513                            (outs VR128:$dst), (ins VR128:$src1, GR32:$src2),
514                            "cvtsi2ss\t{$src2, $dst|$dst, $src2}",
515                            [(set VR128:$dst, (int_x86_sse_cvtsi2ss VR128:$src1,
516                                               GR32:$src2))]>;
517   def Int_CVTSI2SSrm : SSI<0x2A, MRMSrcMem,
518                            (outs VR128:$dst), (ins VR128:$src1, i32mem:$src2),
519                            "cvtsi2ss\t{$src2, $dst|$dst, $src2}",
520                            [(set VR128:$dst, (int_x86_sse_cvtsi2ss VR128:$src1,
521                                               (loadi32 addr:$src2)))]>;
522 }
523
524 // Compare Instructions
525 let Defs = [EFLAGS] in {
526 def COMISSrr: PSI<0x2F, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
527                   "comiss\t{$src2, $src1|$src1, $src2}", []>;
528 def COMISSrm: PSI<0x2F, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
529                   "comiss\t{$src2, $src1|$src1, $src2}", []>;
530 } // Defs = [EFLAGS]
531
532 //===----------------------------------------------------------------------===//
533 // SSE 1 & 2 - Move Instructions
534 //===----------------------------------------------------------------------===//
535
536 class sse12_move_rr<RegisterClass RC, ValueType vt, string asm> :
537       SI<0x10, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, RC:$src2), asm,
538       [(set (vt VR128:$dst), (movl VR128:$src1, (scalar_to_vector RC:$src2)))]>;
539
540 // Loading from memory automatically zeroing upper bits.
541 class sse12_move_rm<RegisterClass RC, X86MemOperand x86memop,
542                     PatFrag mem_pat, string OpcodeStr> :
543       SI<0x10, MRMSrcMem, (outs RC:$dst), (ins x86memop:$src),
544          !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
545                         [(set RC:$dst, (mem_pat addr:$src))]>;
546
547 // Move Instructions. Register-to-register movss/movsd is not used for FR32/64
548 // register copies because it's a partial register update; FsMOVAPSrr/FsMOVAPDrr
549 // is used instead. Register-to-register movss/movsd is not modeled as an
550 // INSERT_SUBREG because INSERT_SUBREG requires that the insert be implementable
551 // in terms of a copy, and just mentioned, we don't use movss/movsd for copies.
552 let isAsmParserOnly = 1 in {
553   def VMOVSSrr : sse12_move_rr<FR32, v4f32,
554                   "movss\t{$src2, $src1, $dst|$dst, $src1, $src2}">, XS, VEX_4V;
555   def VMOVSDrr : sse12_move_rr<FR64, v2f64,
556                   "movsd\t{$src2, $src1, $dst|$dst, $src1, $src2}">, XD, VEX_4V;
557
558   let canFoldAsLoad = 1, isReMaterializable = 1 in {
559     def VMOVSSrm : sse12_move_rm<FR32, f32mem, loadf32, "movss">, XS, VEX;
560
561     let AddedComplexity = 20 in
562       def VMOVSDrm : sse12_move_rm<FR64, f64mem, loadf64, "movsd">, XD, VEX;
563   }
564 }
565
566 let Constraints = "$src1 = $dst" in {
567   def MOVSSrr : sse12_move_rr<FR32, v4f32,
568                           "movss\t{$src2, $dst|$dst, $src2}">, XS;
569   def MOVSDrr : sse12_move_rr<FR64, v2f64,
570                           "movsd\t{$src2, $dst|$dst, $src2}">, XD;
571 }
572
573 let canFoldAsLoad = 1, isReMaterializable = 1 in {
574   def MOVSSrm : sse12_move_rm<FR32, f32mem, loadf32, "movss">, XS;
575
576   let AddedComplexity = 20 in
577     def MOVSDrm : sse12_move_rm<FR64, f64mem, loadf64, "movsd">, XD;
578 }
579
580 let AddedComplexity = 15 in {
581 // Extract the low 32-bit value from one vector and insert it into another.
582 def : Pat<(v4f32 (movl VR128:$src1, VR128:$src2)),
583           (MOVSSrr (v4f32 VR128:$src1),
584                    (EXTRACT_SUBREG (v4f32 VR128:$src2), sub_ss))>;
585 // Extract the low 64-bit value from one vector and insert it into another.
586 def : Pat<(v2f64 (movl VR128:$src1, VR128:$src2)),
587           (MOVSDrr (v2f64 VR128:$src1),
588                    (EXTRACT_SUBREG (v2f64 VR128:$src2), sub_sd))>;
589 }
590
591 // Implicitly promote a 32-bit scalar to a vector.
592 def : Pat<(v4f32 (scalar_to_vector FR32:$src)),
593           (INSERT_SUBREG (v4f32 (IMPLICIT_DEF)), FR32:$src, sub_ss)>;
594 // Implicitly promote a 64-bit scalar to a vector.
595 def : Pat<(v2f64 (scalar_to_vector FR64:$src)),
596           (INSERT_SUBREG (v2f64 (IMPLICIT_DEF)), FR64:$src, sub_sd)>;
597
598 let AddedComplexity = 20 in {
599 // MOVSSrm zeros the high parts of the register; represent this
600 // with SUBREG_TO_REG.
601 def : Pat<(v4f32 (X86vzmovl (v4f32 (scalar_to_vector (loadf32 addr:$src))))),
602           (SUBREG_TO_REG (i32 0), (MOVSSrm addr:$src), sub_ss)>;
603 def : Pat<(v4f32 (scalar_to_vector (loadf32 addr:$src))),
604           (SUBREG_TO_REG (i32 0), (MOVSSrm addr:$src), sub_ss)>;
605 def : Pat<(v4f32 (X86vzmovl (loadv4f32 addr:$src))),
606           (SUBREG_TO_REG (i32 0), (MOVSSrm addr:$src), sub_ss)>;
607 // MOVSDrm zeros the high parts of the register; represent this
608 // with SUBREG_TO_REG.
609 def : Pat<(v2f64 (X86vzmovl (v2f64 (scalar_to_vector (loadf64 addr:$src))))),
610           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
611 def : Pat<(v2f64 (scalar_to_vector (loadf64 addr:$src))),
612           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
613 def : Pat<(v2f64 (X86vzmovl (loadv2f64 addr:$src))),
614           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
615 def : Pat<(v2f64 (X86vzmovl (bc_v2f64 (loadv4f32 addr:$src)))),
616           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
617 def : Pat<(v2f64 (X86vzload addr:$src)),
618           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
619 }
620
621 // Store scalar value to memory.
622 def MOVSSmr : SSI<0x11, MRMDestMem, (outs), (ins f32mem:$dst, FR32:$src),
623                   "movss\t{$src, $dst|$dst, $src}",
624                   [(store FR32:$src, addr:$dst)]>;
625 def MOVSDmr : SDI<0x11, MRMDestMem, (outs), (ins f64mem:$dst, FR64:$src),
626                   "movsd\t{$src, $dst|$dst, $src}",
627                   [(store FR64:$src, addr:$dst)]>;
628
629 let isAsmParserOnly = 1 in {
630 def VMOVSSmr : SI<0x11, MRMDestMem, (outs), (ins f32mem:$dst, FR32:$src),
631                   "movss\t{$src, $dst|$dst, $src}",
632                   [(store FR32:$src, addr:$dst)]>, XS, VEX_4V;
633 def VMOVSDmr : SI<0x11, MRMDestMem, (outs), (ins f64mem:$dst, FR64:$src),
634                   "movsd\t{$src, $dst|$dst, $src}",
635                   [(store FR64:$src, addr:$dst)]>, XD, VEX_4V;
636 }
637
638 // Extract and store.
639 def : Pat<(store (f32 (vector_extract (v4f32 VR128:$src), (iPTR 0))),
640                  addr:$dst),
641           (MOVSSmr addr:$dst,
642                    (EXTRACT_SUBREG (v4f32 VR128:$src), sub_ss))>;
643 def : Pat<(store (f64 (vector_extract (v2f64 VR128:$src), (iPTR 0))),
644                  addr:$dst),
645           (MOVSDmr addr:$dst,
646                    (EXTRACT_SUBREG (v2f64 VR128:$src), sub_sd))>;
647
648 //===----------------------------------------------------------------------===//
649 // SSE 1 & 2 - Conversion Instructions
650 //===----------------------------------------------------------------------===//
651
652 // Conversion instructions
653 def CVTTSS2SIrr : SSI<0x2C, MRMSrcReg, (outs GR32:$dst), (ins FR32:$src),
654                       "cvttss2si\t{$src, $dst|$dst, $src}",
655                       [(set GR32:$dst, (fp_to_sint FR32:$src))]>;
656 def CVTTSS2SIrm : SSI<0x2C, MRMSrcMem, (outs GR32:$dst), (ins f32mem:$src),
657                       "cvttss2si\t{$src, $dst|$dst, $src}",
658                       [(set GR32:$dst, (fp_to_sint (loadf32 addr:$src)))]>;
659 def CVTTSD2SIrr : SDI<0x2C, MRMSrcReg, (outs GR32:$dst), (ins FR64:$src),
660                       "cvttsd2si\t{$src, $dst|$dst, $src}",
661                       [(set GR32:$dst, (fp_to_sint FR64:$src))]>;
662 def CVTTSD2SIrm : SDI<0x2C, MRMSrcMem, (outs GR32:$dst), (ins f64mem:$src),
663                       "cvttsd2si\t{$src, $dst|$dst, $src}",
664                       [(set GR32:$dst, (fp_to_sint (loadf64 addr:$src)))]>;
665
666 def CVTSI2SSrr  : SSI<0x2A, MRMSrcReg, (outs FR32:$dst), (ins GR32:$src),
667                       "cvtsi2ss\t{$src, $dst|$dst, $src}",
668                       [(set FR32:$dst, (sint_to_fp GR32:$src))]>;
669 def CVTSI2SSrm  : SSI<0x2A, MRMSrcMem, (outs FR32:$dst), (ins i32mem:$src),
670                       "cvtsi2ss\t{$src, $dst|$dst, $src}",
671                       [(set FR32:$dst, (sint_to_fp (loadi32 addr:$src)))]>;
672 def CVTSI2SDrr  : SDI<0x2A, MRMSrcReg, (outs FR64:$dst), (ins GR32:$src),
673                       "cvtsi2sd\t{$src, $dst|$dst, $src}",
674                       [(set FR64:$dst, (sint_to_fp GR32:$src))]>;
675 def CVTSI2SDrm  : SDI<0x2A, MRMSrcMem, (outs FR64:$dst), (ins i32mem:$src),
676                       "cvtsi2sd\t{$src, $dst|$dst, $src}",
677                       [(set FR64:$dst, (sint_to_fp (loadi32 addr:$src)))]>;
678
679 // Match intrinsics which expect XMM operand(s).
680 def Int_CVTSS2SIrr : SSI<0x2D, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
681                          "cvtss2si\t{$src, $dst|$dst, $src}",
682                          [(set GR32:$dst, (int_x86_sse_cvtss2si VR128:$src))]>;
683 def Int_CVTSS2SIrm : SSI<0x2D, MRMSrcMem, (outs GR32:$dst), (ins f32mem:$src),
684                          "cvtss2si\t{$src, $dst|$dst, $src}",
685                          [(set GR32:$dst, (int_x86_sse_cvtss2si
686                                            (load addr:$src)))]>;
687 def Int_CVTSD2SIrr : SDI<0x2D, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
688                          "cvtsd2si\t{$src, $dst|$dst, $src}",
689                          [(set GR32:$dst, (int_x86_sse2_cvtsd2si VR128:$src))]>;
690 def Int_CVTSD2SIrm : SDI<0x2D, MRMSrcMem, (outs GR32:$dst), (ins f128mem:$src),
691                          "cvtsd2si\t{$src, $dst|$dst, $src}",
692                          [(set GR32:$dst, (int_x86_sse2_cvtsd2si
693                                            (load addr:$src)))]>;
694
695 // Match intrinsics which expect MM and XMM operand(s).
696 def Int_CVTPS2PIrr : PSI<0x2D, MRMSrcReg, (outs VR64:$dst), (ins VR128:$src),
697                          "cvtps2pi\t{$src, $dst|$dst, $src}",
698                          [(set VR64:$dst, (int_x86_sse_cvtps2pi VR128:$src))]>;
699 def Int_CVTPS2PIrm : PSI<0x2D, MRMSrcMem, (outs VR64:$dst), (ins f64mem:$src),
700                          "cvtps2pi\t{$src, $dst|$dst, $src}",
701                          [(set VR64:$dst, (int_x86_sse_cvtps2pi
702                                            (load addr:$src)))]>;
703 def Int_CVTPD2PIrr : PDI<0x2D, MRMSrcReg, (outs VR64:$dst), (ins VR128:$src),
704                          "cvtpd2pi\t{$src, $dst|$dst, $src}",
705                          [(set VR64:$dst, (int_x86_sse_cvtpd2pi VR128:$src))]>;
706 def Int_CVTPD2PIrm : PDI<0x2D, MRMSrcMem, (outs VR64:$dst), (ins f128mem:$src),
707                          "cvtpd2pi\t{$src, $dst|$dst, $src}",
708                          [(set VR64:$dst, (int_x86_sse_cvtpd2pi
709                                            (memop addr:$src)))]>;
710
711 // Match intrinsics which expect MM and XMM operand(s).
712 def Int_CVTTPS2PIrr: PSI<0x2C, MRMSrcReg, (outs VR64:$dst), (ins VR128:$src),
713                          "cvttps2pi\t{$src, $dst|$dst, $src}",
714                          [(set VR64:$dst, (int_x86_sse_cvttps2pi VR128:$src))]>;
715 def Int_CVTTPS2PIrm: PSI<0x2C, MRMSrcMem, (outs VR64:$dst), (ins f64mem:$src),
716                          "cvttps2pi\t{$src, $dst|$dst, $src}",
717                          [(set VR64:$dst, (int_x86_sse_cvttps2pi
718                                            (load addr:$src)))]>;
719 def Int_CVTTPD2PIrr: PDI<0x2C, MRMSrcReg, (outs VR64:$dst), (ins VR128:$src),
720                          "cvttpd2pi\t{$src, $dst|$dst, $src}",
721                          [(set VR64:$dst, (int_x86_sse_cvttpd2pi VR128:$src))]>;
722 def Int_CVTTPD2PIrm: PDI<0x2C, MRMSrcMem, (outs VR64:$dst), (ins f128mem:$src),
723                          "cvttpd2pi\t{$src, $dst|$dst, $src}",
724                          [(set VR64:$dst, (int_x86_sse_cvttpd2pi
725                                            (memop addr:$src)))]>;
726
727 let Constraints = "$src1 = $dst" in {
728   def Int_CVTPI2PSrr : PSI<0x2A, MRMSrcReg,
729                            (outs VR128:$dst), (ins VR128:$src1, VR64:$src2),
730                         "cvtpi2ps\t{$src2, $dst|$dst, $src2}",
731                         [(set VR128:$dst, (int_x86_sse_cvtpi2ps VR128:$src1,
732                                            VR64:$src2))]>;
733   def Int_CVTPI2PSrm : PSI<0x2A, MRMSrcMem,
734                            (outs VR128:$dst), (ins VR128:$src1, i64mem:$src2),
735                         "cvtpi2ps\t{$src2, $dst|$dst, $src2}",
736                         [(set VR128:$dst, (int_x86_sse_cvtpi2ps VR128:$src1,
737                                             (load addr:$src2)))]>;
738 }
739
740 //===----------------------------------------------------------------------===//
741 // SSE 1 & 2 - Compare Instructions
742 //===----------------------------------------------------------------------===//
743
744 // Comparison instructions
745 let Constraints = "$src1 = $dst", neverHasSideEffects = 1 in {
746   def CMPSSrr : SSIi8<0xC2, MRMSrcReg,
747                     (outs FR32:$dst), (ins FR32:$src1, FR32:$src, SSECC:$cc),
748                     "cmp${cc}ss\t{$src, $dst|$dst, $src}", []>;
749   let mayLoad = 1 in
750   def CMPSSrm : SSIi8<0xC2, MRMSrcMem,
751                     (outs FR32:$dst), (ins FR32:$src1, f32mem:$src, SSECC:$cc),
752                     "cmp${cc}ss\t{$src, $dst|$dst, $src}", []>;
753
754   def CMPSDrr : SDIi8<0xC2, MRMSrcReg,
755                     (outs FR64:$dst), (ins FR64:$src1, FR64:$src, SSECC:$cc),
756                     "cmp${cc}sd\t{$src, $dst|$dst, $src}", []>;
757   let mayLoad = 1 in
758   def CMPSDrm : SDIi8<0xC2, MRMSrcMem,
759                     (outs FR64:$dst), (ins FR64:$src1, f64mem:$src, SSECC:$cc),
760                     "cmp${cc}sd\t{$src, $dst|$dst, $src}", []>;
761
762 // Accept explicit immediate argument form instead of comparison code.
763 let isAsmParserOnly = 1 in {
764   def CMPSSrr_alt : SSIi8<0xC2, MRMSrcReg,
765                     (outs FR32:$dst), (ins FR32:$src1, FR32:$src, i8imm:$src2),
766                     "cmpss\t{$src2, $src, $dst|$dst, $src, $src2}", []>;
767   let mayLoad = 1 in
768   def CMPSSrm_alt : SSIi8<0xC2, MRMSrcMem,
769                     (outs FR32:$dst), (ins FR32:$src1, f32mem:$src, i8imm:$src2),
770                     "cmpss\t{$src2, $src, $dst|$dst, $src, $src2}", []>;
771
772   def CMPSDrr_alt : SDIi8<0xC2, MRMSrcReg,
773                     (outs FR64:$dst), (ins FR64:$src1, FR64:$src, i8imm:$src2),
774                     "cmpsd\t{$src2, $src, $dst|$dst, $src, $src2}", []>;
775   let mayLoad = 1 in
776   def CMPSDrm_alt : SDIi8<0xC2, MRMSrcMem,
777                     (outs FR64:$dst), (ins FR64:$src1, f64mem:$src, i8imm:$src2),
778                     "cmpsd\t{$src2, $src, $dst|$dst, $src, $src2}", []>;
779 }
780 }
781
782 let Defs = [EFLAGS] in {
783 def UCOMISSrr: PSI<0x2E, MRMSrcReg, (outs), (ins FR32:$src1, FR32:$src2),
784                    "ucomiss\t{$src2, $src1|$src1, $src2}",
785                    [(set EFLAGS, (X86cmp FR32:$src1, FR32:$src2))]>;
786 def UCOMISSrm: PSI<0x2E, MRMSrcMem, (outs), (ins FR32:$src1, f32mem:$src2),
787                    "ucomiss\t{$src2, $src1|$src1, $src2}",
788                    [(set EFLAGS, (X86cmp FR32:$src1, (loadf32 addr:$src2)))]>;
789 def UCOMISDrr: PDI<0x2E, MRMSrcReg, (outs), (ins FR64:$src1, FR64:$src2),
790                    "ucomisd\t{$src2, $src1|$src1, $src2}",
791                    [(set EFLAGS, (X86cmp FR64:$src1, FR64:$src2))]>;
792 def UCOMISDrm: PDI<0x2E, MRMSrcMem, (outs), (ins FR64:$src1, f64mem:$src2),
793                    "ucomisd\t{$src2, $src1|$src1, $src2}",
794                    [(set EFLAGS, (X86cmp FR64:$src1, (loadf64 addr:$src2)))]>;
795 } // Defs = [EFLAGS]
796
797 // Aliases to match intrinsics which expect XMM operand(s).
798 let Constraints = "$src1 = $dst" in {
799   def Int_CMPSSrr : SSIi8<0xC2, MRMSrcReg,
800                         (outs VR128:$dst),
801                         (ins VR128:$src1, VR128:$src, SSECC:$cc),
802                         "cmp${cc}ss\t{$src, $dst|$dst, $src}",
803                         [(set VR128:$dst, (int_x86_sse_cmp_ss
804                                              VR128:$src1,
805                                              VR128:$src, imm:$cc))]>;
806   def Int_CMPSSrm : SSIi8<0xC2, MRMSrcMem,
807                         (outs VR128:$dst),
808                         (ins VR128:$src1, f32mem:$src, SSECC:$cc),
809                         "cmp${cc}ss\t{$src, $dst|$dst, $src}",
810                         [(set VR128:$dst, (int_x86_sse_cmp_ss VR128:$src1,
811                                            (load addr:$src), imm:$cc))]>;
812
813   def Int_CMPSDrr : SDIi8<0xC2, MRMSrcReg,
814                         (outs VR128:$dst),
815                         (ins VR128:$src1, VR128:$src, SSECC:$cc),
816                         "cmp${cc}sd\t{$src, $dst|$dst, $src}",
817                         [(set VR128:$dst, (int_x86_sse2_cmp_sd VR128:$src1,
818                                            VR128:$src, imm:$cc))]>;
819   def Int_CMPSDrm : SDIi8<0xC2, MRMSrcMem,
820                         (outs VR128:$dst),
821                         (ins VR128:$src1, f64mem:$src, SSECC:$cc),
822                         "cmp${cc}sd\t{$src, $dst|$dst, $src}",
823                         [(set VR128:$dst, (int_x86_sse2_cmp_sd VR128:$src1,
824                                            (load addr:$src), imm:$cc))]>;
825 }
826
827 let Defs = [EFLAGS] in {
828 def Int_UCOMISSrr: PSI<0x2E, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
829                        "ucomiss\t{$src2, $src1|$src1, $src2}",
830                        [(set EFLAGS, (X86ucomi (v4f32 VR128:$src1),
831                                                VR128:$src2))]>;
832 def Int_UCOMISSrm: PSI<0x2E, MRMSrcMem, (outs),(ins VR128:$src1, f128mem:$src2),
833                        "ucomiss\t{$src2, $src1|$src1, $src2}",
834                        [(set EFLAGS, (X86ucomi (v4f32 VR128:$src1),
835                                                (load addr:$src2)))]>;
836 def Int_UCOMISDrr: PDI<0x2E, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
837                        "ucomisd\t{$src2, $src1|$src1, $src2}",
838                        [(set EFLAGS, (X86ucomi (v2f64 VR128:$src1),
839                                                VR128:$src2))]>;
840 def Int_UCOMISDrm: PDI<0x2E, MRMSrcMem, (outs),(ins VR128:$src1, f128mem:$src2),
841                        "ucomisd\t{$src2, $src1|$src1, $src2}",
842                        [(set EFLAGS, (X86ucomi (v2f64 VR128:$src1),
843                                                (load addr:$src2)))]>;
844
845 def Int_COMISSrr: PSI<0x2F, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
846                       "comiss\t{$src2, $src1|$src1, $src2}",
847                       [(set EFLAGS, (X86comi (v4f32 VR128:$src1),
848                                              VR128:$src2))]>;
849 def Int_COMISSrm: PSI<0x2F, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
850                       "comiss\t{$src2, $src1|$src1, $src2}",
851                       [(set EFLAGS, (X86comi (v4f32 VR128:$src1),
852                                              (load addr:$src2)))]>;
853 def Int_COMISDrr: PDI<0x2F, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
854                       "comisd\t{$src2, $src1|$src1, $src2}",
855                       [(set EFLAGS, (X86comi (v2f64 VR128:$src1),
856                                              VR128:$src2))]>;
857 def Int_COMISDrm: PDI<0x2F, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
858                       "comisd\t{$src2, $src1|$src1, $src2}",
859                       [(set EFLAGS, (X86comi (v2f64 VR128:$src1),
860                                              (load addr:$src2)))]>;
861 } // Defs = [EFLAGS]
862
863 // Aliases of packed SSE1 & SSE2 instructions for scalar use. These all have
864 // names that start with 'Fs'.
865
866 // Alias instructions that map fld0 to pxor for sse.
867 let isReMaterializable = 1, isAsCheapAsAMove = 1, isCodeGenOnly = 1,
868     canFoldAsLoad = 1 in {
869   // FIXME: Set encoding to pseudo!
870 def FsFLD0SS : I<0xEF, MRMInitReg, (outs FR32:$dst), (ins), "",
871                  [(set FR32:$dst, fp32imm0)]>,
872                  Requires<[HasSSE1]>, TB, OpSize;
873 def FsFLD0SD : I<0xEF, MRMInitReg, (outs FR64:$dst), (ins), "",
874                  [(set FR64:$dst, fpimm0)]>,
875                Requires<[HasSSE2]>, TB, OpSize;
876 }
877
878 // Alias instruction to do FR32 or FR64 reg-to-reg copy using movaps. Upper
879 // bits are disregarded.
880 let neverHasSideEffects = 1 in {
881 def FsMOVAPSrr : PSI<0x28, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src),
882                      "movaps\t{$src, $dst|$dst, $src}", []>;
883 def FsMOVAPDrr : PDI<0x28, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src),
884                      "movapd\t{$src, $dst|$dst, $src}", []>;
885 }
886
887 // Alias instruction to load FR32 or FR64 from f128mem using movaps. Upper
888 // bits are disregarded.
889 let canFoldAsLoad = 1, isReMaterializable = 1 in {
890 def FsMOVAPSrm : PSI<0x28, MRMSrcMem, (outs FR32:$dst), (ins f128mem:$src),
891                      "movaps\t{$src, $dst|$dst, $src}",
892                      [(set FR32:$dst, (alignedloadfsf32 addr:$src))]>;
893 def FsMOVAPDrm : PDI<0x28, MRMSrcMem, (outs FR64:$dst), (ins f128mem:$src),
894                      "movapd\t{$src, $dst|$dst, $src}",
895                      [(set FR64:$dst, (alignedloadfsf64 addr:$src))]>;
896 }
897
898 //===----------------------------------------------------------------------===//
899 // SSE 1 & 2 - Logical Instructions
900 //===----------------------------------------------------------------------===//
901
902 /// sse12_fp_alias_pack_logical - SSE 1 & 2 aliased packed FP logical ops
903 ///
904 multiclass sse12_fp_alias_pack_logical<bits<8> opc, string OpcodeStr,
905                                        SDNode OpNode, bit MayLoad = 0> {
906   let isAsmParserOnly = 1 in {
907     defm V#NAME#PS : sse12_fp_packed<opc, !strconcat(OpcodeStr,
908                 "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"), OpNode, FR32,
909                 f32, f128mem, memopfsf32, SSEPackedSingle, MayLoad>, VEX_4V;
910
911     defm V#NAME#PD : sse12_fp_packed<opc, !strconcat(OpcodeStr,
912                 "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"), OpNode, FR64,
913                 f64, f128mem, memopfsf64, SSEPackedDouble, MayLoad>, OpSize,
914                 VEX_4V;
915   }
916
917   let Constraints = "$src1 = $dst" in {
918     defm PS : sse12_fp_packed<opc, !strconcat(OpcodeStr,
919                 "ps\t{$src2, $dst|$dst, $src2}"), OpNode, FR32, f32,
920                 f128mem, memopfsf32, SSEPackedSingle, MayLoad>, TB;
921
922     defm PD : sse12_fp_packed<opc, !strconcat(OpcodeStr,
923                 "pd\t{$src2, $dst|$dst, $src2}"), OpNode, FR64, f64,
924                 f128mem, memopfsf64, SSEPackedDouble, MayLoad>, TB, OpSize;
925   }
926 }
927
928 // Alias bitwise logical operations using SSE logical ops on packed FP values.
929 defm FsAND  : sse12_fp_alias_pack_logical<0x54, "and", X86fand>;
930 defm FsOR   : sse12_fp_alias_pack_logical<0x56, "or", X86for>;
931 defm FsXOR  : sse12_fp_alias_pack_logical<0x57, "xor", X86fxor>;
932
933 let neverHasSideEffects = 1, Pattern = []<dag>, isCommutable = 0 in
934   defm FsANDN : sse12_fp_alias_pack_logical<0x55, "andn", undef, 1>;
935
936 /// sse12_fp_packed_logical - SSE 1 & 2 packed FP logical ops
937 ///
938 multiclass sse12_fp_packed_logical<bits<8> opc, string OpcodeStr,
939                                  SDNode OpNode, int HasPat = 0,
940                                  list<list<dag>> Pattern = []> {
941   let isAsmParserOnly = 1 in {
942     defm V#NAME#PS : sse12_fp_packed_logical_rm<opc, VR128, SSEPackedSingle,
943          !strconcat(OpcodeStr, "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
944          f128mem,
945          !if(HasPat, Pattern[0], // rr
946                      [(set VR128:$dst, (v2i64 (OpNode VR128:$src1,
947                                                       VR128:$src2)))]),
948          !if(HasPat, Pattern[2], // rm
949                      [(set VR128:$dst, (OpNode (bc_v2i64 (v4f32 VR128:$src1)),
950                                                (memopv2i64 addr:$src2)))])>,
951                                                VEX_4V;
952
953     defm V#NAME#PD : sse12_fp_packed_logical_rm<opc, VR128, SSEPackedDouble,
954          !strconcat(OpcodeStr, "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
955          f128mem,
956          !if(HasPat, Pattern[1], // rr
957                      [(set VR128:$dst, (OpNode (bc_v2i64 (v2f64 VR128:$src1)),
958                                                (bc_v2i64 (v2f64
959                                                VR128:$src2))))]),
960          !if(HasPat, Pattern[3], // rm
961                      [(set VR128:$dst, (OpNode (bc_v2i64 (v2f64 VR128:$src1)),
962                                                (memopv2i64 addr:$src2)))])>,
963                                                                OpSize, VEX_4V;
964   }
965   let Constraints = "$src1 = $dst" in {
966     defm PS : sse12_fp_packed_logical_rm<opc, VR128, SSEPackedSingle,
967          !strconcat(OpcodeStr, "ps\t{$src2, $dst|$dst, $src2}"), f128mem,
968          !if(HasPat, Pattern[0], // rr
969                      [(set VR128:$dst, (v2i64 (OpNode VR128:$src1,
970                                                       VR128:$src2)))]),
971          !if(HasPat, Pattern[2], // rm
972                      [(set VR128:$dst, (OpNode (bc_v2i64 (v4f32 VR128:$src1)),
973                                                (memopv2i64 addr:$src2)))])>, TB;
974
975     defm PD : sse12_fp_packed_logical_rm<opc, VR128, SSEPackedDouble,
976          !strconcat(OpcodeStr, "pd\t{$src2, $dst|$dst, $src2}"), f128mem,
977          !if(HasPat, Pattern[1], // rr
978                      [(set VR128:$dst, (OpNode (bc_v2i64 (v2f64 VR128:$src1)),
979                                                (bc_v2i64 (v2f64
980                                                VR128:$src2))))]),
981          !if(HasPat, Pattern[3], // rm
982                      [(set VR128:$dst, (OpNode (bc_v2i64 (v2f64 VR128:$src1)),
983                                                (memopv2i64 addr:$src2)))])>,
984                                                                     TB, OpSize;
985   }
986 }
987
988 defm AND  : sse12_fp_packed_logical<0x54, "and", and>;
989 defm OR   : sse12_fp_packed_logical<0x56, "or", or>;
990 defm XOR  : sse12_fp_packed_logical<0x57, "xor", xor>;
991 let isCommutable = 0 in
992   defm ANDN : sse12_fp_packed_logical<0x55, "andn", undef /* dummy */, 1, [
993     // single r+r
994     [(set VR128:$dst, (v2i64 (and (xor VR128:$src1,
995                                        (bc_v2i64 (v4i32 immAllOnesV))),
996                                    VR128:$src2)))],
997     // double r+r
998     [(set VR128:$dst, (and (vnot (bc_v2i64 (v2f64 VR128:$src1))),
999                                  (bc_v2i64 (v2f64 VR128:$src2))))],
1000     // single r+m
1001     [(set VR128:$dst, (v2i64 (and (xor (bc_v2i64 (v4f32 VR128:$src1)),
1002                                        (bc_v2i64 (v4i32 immAllOnesV))),
1003                                   (memopv2i64 addr:$src2))))],
1004     // double r+m
1005     [(set VR128:$dst, (and (vnot (bc_v2i64 (v2f64 VR128:$src1))),
1006                            (memopv2i64 addr:$src2)))]]>;
1007
1008 //===----------------------------------------------------------------------===//
1009 // SSE 1 & 2 - Arithmetic Instructions
1010 //===----------------------------------------------------------------------===//
1011
1012 /// basic_sse12_fp_binop_rm - SSE 1 & 2 binops come in both scalar and
1013 /// vector forms.
1014 ///
1015 /// In addition, we also have a special variant of the scalar form here to
1016 /// represent the associated intrinsic operation.  This form is unlike the
1017 /// plain scalar form, in that it takes an entire vector (instead of a scalar)
1018 /// and leaves the top elements unmodified (therefore these cannot be commuted).
1019 ///
1020 /// These three forms can each be reg+reg or reg+mem.
1021 ///
1022 multiclass basic_sse12_fp_binop_rm<bits<8> opc, string OpcodeStr,
1023                                    SDNode OpNode> {
1024
1025   let isAsmParserOnly = 1 in {
1026     defm V#NAME#SS : sse12_fp_scalar<opc,
1027         !strconcat(OpcodeStr, "ss\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1028                    OpNode, FR32, f32mem>, XS, VEX_4V;
1029
1030     defm V#NAME#SD : sse12_fp_scalar<opc,
1031         !strconcat(OpcodeStr, "sd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1032                    OpNode, FR64, f64mem>, XD, VEX_4V;
1033
1034     defm V#NAME#PS : sse12_fp_packed<opc, !strconcat(OpcodeStr,
1035                       "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"), OpNode,
1036                       VR128, v4f32, f128mem, memopv4f32, SSEPackedSingle>,
1037                       VEX_4V;
1038
1039     defm V#NAME#PD : sse12_fp_packed<opc, !strconcat(OpcodeStr,
1040                       "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"), OpNode,
1041                       VR128, v2f64, f128mem, memopv2f64, SSEPackedDouble>,
1042                       OpSize, VEX_4V;
1043
1044     defm V#NAME#SS : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1045        !strconcat(OpcodeStr, "ss\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1046                   "", "_ss", ssmem, sse_load_f32>, XS, VEX_4V;
1047
1048     defm V#NAME#SD : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1049        !strconcat(OpcodeStr, "sd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1050                   "2", "_sd", sdmem, sse_load_f64>, XD, VEX_4V;
1051   }
1052
1053   let Constraints = "$src1 = $dst" in {
1054     defm SS : sse12_fp_scalar<opc,
1055                     !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
1056                     OpNode, FR32, f32mem>, XS;
1057
1058     defm SD : sse12_fp_scalar<opc,
1059                     !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1060                     OpNode, FR64, f64mem>, XD;
1061
1062     defm PS : sse12_fp_packed<opc, !strconcat(OpcodeStr,
1063                 "ps\t{$src2, $dst|$dst, $src2}"), OpNode, VR128, v4f32,
1064                 f128mem, memopv4f32, SSEPackedSingle>, TB;
1065
1066     defm PD : sse12_fp_packed<opc, !strconcat(OpcodeStr,
1067                 "pd\t{$src2, $dst|$dst, $src2}"), OpNode, VR128, v2f64,
1068                 f128mem, memopv2f64, SSEPackedDouble>, TB, OpSize;
1069
1070     defm SS : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1071        !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
1072                   "", "_ss", ssmem, sse_load_f32>, XS;
1073
1074     defm SD : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1075        !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1076                   "2", "_sd", sdmem, sse_load_f64>, XD;
1077   }
1078 }
1079
1080 // Arithmetic instructions
1081 defm ADD : basic_sse12_fp_binop_rm<0x58, "add", fadd>;
1082 defm MUL : basic_sse12_fp_binop_rm<0x59, "mul", fmul>;
1083
1084 let isCommutable = 0 in {
1085   defm SUB : basic_sse12_fp_binop_rm<0x5C, "sub", fsub>;
1086   defm DIV : basic_sse12_fp_binop_rm<0x5E, "div", fdiv>;
1087 }
1088
1089 /// sse12_fp_binop_rm - Other SSE 1 & 2 binops
1090 ///
1091 /// This multiclass is like basic_sse12_fp_binop_rm, with the addition of
1092 /// instructions for a full-vector intrinsic form.  Operations that map
1093 /// onto C operators don't use this form since they just use the plain
1094 /// vector form instead of having a separate vector intrinsic form.
1095 ///
1096 multiclass sse12_fp_binop_rm<bits<8> opc, string OpcodeStr,
1097                              SDNode OpNode> {
1098
1099   let isAsmParserOnly = 1 in {
1100     // Scalar operation, reg+reg.
1101     defm V#NAME#SS : sse12_fp_scalar<opc,
1102       !strconcat(OpcodeStr, "ss\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1103                  OpNode, FR32, f32mem>, XS, VEX_4V;
1104
1105     defm V#NAME#SD : sse12_fp_scalar<opc,
1106       !strconcat(OpcodeStr, "sd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1107                  OpNode, FR64, f64mem>, XD, VEX_4V;
1108
1109     defm V#NAME#PS : sse12_fp_packed<opc, !strconcat(OpcodeStr,
1110                       "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"), OpNode,
1111                       VR128, v4f32, f128mem, memopv4f32, SSEPackedSingle>,
1112                       VEX_4V;
1113
1114     defm V#NAME#PD : sse12_fp_packed<opc, !strconcat(OpcodeStr,
1115                       "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"), OpNode,
1116                       VR128, v2f64, f128mem, memopv2f64, SSEPackedDouble>,
1117                       OpSize, VEX_4V;
1118
1119     defm V#NAME#SS : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1120        !strconcat(OpcodeStr, "ss\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1121                   "", "_ss", ssmem, sse_load_f32>, XS, VEX_4V;
1122
1123     defm V#NAME#SD : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1124        !strconcat(OpcodeStr, "sd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1125                   "2", "_sd", sdmem, sse_load_f64>, XD, VEX_4V;
1126
1127     defm V#NAME#PS : sse12_fp_packed_int<opc, OpcodeStr, VR128,
1128        !strconcat(OpcodeStr, "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1129                   "", "_ps", f128mem, memopv4f32, SSEPackedSingle>, VEX_4V;
1130
1131     defm V#NAME#PD : sse12_fp_packed_int<opc, OpcodeStr, VR128,
1132        !strconcat(OpcodeStr, "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1133                   "2", "_pd", f128mem, memopv2f64, SSEPackedDouble>, OpSize,
1134                   VEX_4V;
1135   }
1136
1137   let Constraints = "$src1 = $dst" in {
1138     // Scalar operation, reg+reg.
1139     defm SS : sse12_fp_scalar<opc,
1140                     !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
1141                     OpNode, FR32, f32mem>, XS;
1142     defm SD : sse12_fp_scalar<opc,
1143                     !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1144                     OpNode, FR64, f64mem>, XD;
1145     defm PS : sse12_fp_packed<opc, !strconcat(OpcodeStr,
1146                 "ps\t{$src2, $dst|$dst, $src2}"), OpNode, VR128, v4f32,
1147                 f128mem, memopv4f32, SSEPackedSingle>, TB;
1148
1149     defm PD : sse12_fp_packed<opc, !strconcat(OpcodeStr,
1150                 "pd\t{$src2, $dst|$dst, $src2}"), OpNode, VR128, v2f64,
1151                 f128mem, memopv2f64, SSEPackedDouble>, TB, OpSize;
1152
1153     defm SS : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1154        !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
1155                   "", "_ss", ssmem, sse_load_f32>, XS;
1156
1157     defm SD : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1158        !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1159                   "2", "_sd", sdmem, sse_load_f64>, XD;
1160
1161     defm PS : sse12_fp_packed_int<opc, OpcodeStr, VR128,
1162        !strconcat(OpcodeStr, "ps\t{$src2, $dst|$dst, $src2}"),
1163                   "", "_ps", f128mem, memopv4f32, SSEPackedSingle>, TB;
1164
1165     defm PD : sse12_fp_packed_int<opc, OpcodeStr, VR128,
1166        !strconcat(OpcodeStr, "pd\t{$src2, $dst|$dst, $src2}"),
1167                   "2", "_pd", f128mem, memopv2f64, SSEPackedDouble>, TB, OpSize;
1168   }
1169 }
1170
1171 let isCommutable = 0 in {
1172   defm MAX : sse12_fp_binop_rm<0x5F, "max", X86fmax>;
1173   defm MIN : sse12_fp_binop_rm<0x5D, "min", X86fmin>;
1174 }
1175
1176 //===----------------------------------------------------------------------===//
1177 // SSE packed FP Instructions
1178
1179 // Move Instructions
1180 let neverHasSideEffects = 1 in
1181 def MOVAPSrr : PSI<0x28, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1182                    "movaps\t{$src, $dst|$dst, $src}", []>;
1183 let canFoldAsLoad = 1, isReMaterializable = 1 in
1184 def MOVAPSrm : PSI<0x28, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1185                    "movaps\t{$src, $dst|$dst, $src}",
1186                    [(set VR128:$dst, (alignedloadv4f32 addr:$src))]>;
1187
1188 def MOVAPSmr : PSI<0x29, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1189                    "movaps\t{$src, $dst|$dst, $src}",
1190                    [(alignedstore (v4f32 VR128:$src), addr:$dst)]>;
1191
1192 let neverHasSideEffects = 1 in
1193 def MOVUPSrr : PSI<0x10, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1194                    "movups\t{$src, $dst|$dst, $src}", []>;
1195 let canFoldAsLoad = 1, isReMaterializable = 1 in
1196 def MOVUPSrm : PSI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1197                    "movups\t{$src, $dst|$dst, $src}",
1198                    [(set VR128:$dst, (loadv4f32 addr:$src))]>;
1199 def MOVUPSmr : PSI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1200                    "movups\t{$src, $dst|$dst, $src}",
1201                    [(store (v4f32 VR128:$src), addr:$dst)]>;
1202
1203 // Intrinsic forms of MOVUPS load and store
1204 let canFoldAsLoad = 1, isReMaterializable = 1 in
1205 def MOVUPSrm_Int : PSI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1206                        "movups\t{$src, $dst|$dst, $src}",
1207                        [(set VR128:$dst, (int_x86_sse_loadu_ps addr:$src))]>;
1208 def MOVUPSmr_Int : PSI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1209                        "movups\t{$src, $dst|$dst, $src}",
1210                        [(int_x86_sse_storeu_ps addr:$dst, VR128:$src)]>;
1211
1212 let Constraints = "$src1 = $dst" in {
1213   let AddedComplexity = 20 in {
1214     def MOVLPSrm : PSI<0x12, MRMSrcMem,
1215                        (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
1216                        "movlps\t{$src2, $dst|$dst, $src2}",
1217        [(set VR128:$dst,
1218          (movlp VR128:$src1,
1219                 (bc_v4f32 (v2f64 (scalar_to_vector (loadf64 addr:$src2))))))]>;
1220     def MOVHPSrm : PSI<0x16, MRMSrcMem,
1221                        (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
1222                        "movhps\t{$src2, $dst|$dst, $src2}",
1223        [(set VR128:$dst,
1224          (movlhps VR128:$src1,
1225                 (bc_v4f32 (v2f64 (scalar_to_vector (loadf64 addr:$src2))))))]>;
1226   } // AddedComplexity
1227 } // Constraints = "$src1 = $dst"
1228
1229
1230 def : Pat<(movlhps VR128:$src1, (bc_v4i32 (v2i64 (X86vzload addr:$src2)))),
1231           (MOVHPSrm (v4i32 VR128:$src1), addr:$src2)>;
1232
1233 def MOVLPSmr : PSI<0x13, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
1234                    "movlps\t{$src, $dst|$dst, $src}",
1235                    [(store (f64 (vector_extract (bc_v2f64 (v4f32 VR128:$src)),
1236                                  (iPTR 0))), addr:$dst)]>;
1237
1238 // v2f64 extract element 1 is always custom lowered to unpack high to low
1239 // and extract element 0 so the non-store version isn't too horrible.
1240 def MOVHPSmr : PSI<0x17, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
1241                    "movhps\t{$src, $dst|$dst, $src}",
1242                    [(store (f64 (vector_extract
1243                                  (unpckh (bc_v2f64 (v4f32 VR128:$src)),
1244                                          (undef)), (iPTR 0))), addr:$dst)]>;
1245
1246 let Constraints = "$src1 = $dst" in {
1247 let AddedComplexity = 20 in {
1248 def MOVLHPSrr : PSI<0x16, MRMSrcReg, (outs VR128:$dst),
1249                                      (ins VR128:$src1, VR128:$src2),
1250                     "movlhps\t{$src2, $dst|$dst, $src2}",
1251                     [(set VR128:$dst,
1252                       (v4f32 (movlhps VR128:$src1, VR128:$src2)))]>;
1253
1254 def MOVHLPSrr : PSI<0x12, MRMSrcReg, (outs VR128:$dst),
1255                                      (ins VR128:$src1, VR128:$src2),
1256                     "movhlps\t{$src2, $dst|$dst, $src2}",
1257                     [(set VR128:$dst,
1258                       (v4f32 (movhlps VR128:$src1, VR128:$src2)))]>;
1259 } // AddedComplexity
1260 } // Constraints = "$src1 = $dst"
1261
1262 let AddedComplexity = 20 in {
1263 def : Pat<(v4f32 (movddup VR128:$src, (undef))),
1264           (MOVLHPSrr (v4f32 VR128:$src), (v4f32 VR128:$src))>;
1265 def : Pat<(v2i64 (movddup VR128:$src, (undef))),
1266           (MOVLHPSrr (v2i64 VR128:$src), (v2i64 VR128:$src))>;
1267 }
1268
1269
1270
1271 // Arithmetic
1272
1273 /// sse1_fp_unop_rm - SSE1 unops come in both scalar and vector forms.
1274 ///
1275 /// In addition, we also have a special variant of the scalar form here to
1276 /// represent the associated intrinsic operation.  This form is unlike the
1277 /// plain scalar form, in that it takes an entire vector (instead of a
1278 /// scalar) and leaves the top elements undefined.
1279 ///
1280 /// And, we have a special variant form for a full-vector intrinsic form.
1281 ///
1282 /// These four forms can each have a reg or a mem operand, so there are a
1283 /// total of eight "instructions".
1284 ///
1285 multiclass sse1_fp_unop_rm<bits<8> opc, string OpcodeStr,
1286                            SDNode OpNode,
1287                            Intrinsic F32Int,
1288                            Intrinsic V4F32Int,
1289                            bit Commutable = 0> {
1290   // Scalar operation, reg.
1291   def SSr : SSI<opc, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src),
1292                 !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
1293                 [(set FR32:$dst, (OpNode FR32:$src))]> {
1294     let isCommutable = Commutable;
1295   }
1296
1297   // Scalar operation, mem.
1298   def SSm : I<opc, MRMSrcMem, (outs FR32:$dst), (ins f32mem:$src),
1299                 !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
1300                 [(set FR32:$dst, (OpNode (load addr:$src)))]>, XS,
1301             Requires<[HasSSE1, OptForSize]>;
1302
1303   // Vector operation, reg.
1304   def PSr : PSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1305               !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1306               [(set VR128:$dst, (v4f32 (OpNode VR128:$src)))]> {
1307     let isCommutable = Commutable;
1308   }
1309
1310   // Vector operation, mem.
1311   def PSm : PSI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1312                 !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1313                 [(set VR128:$dst, (OpNode (memopv4f32 addr:$src)))]>;
1314
1315   // Intrinsic operation, reg.
1316   def SSr_Int : SSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1317                     !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
1318                     [(set VR128:$dst, (F32Int VR128:$src))]> {
1319     let isCommutable = Commutable;
1320   }
1321
1322   // Intrinsic operation, mem.
1323   def SSm_Int : SSI<opc, MRMSrcMem, (outs VR128:$dst), (ins ssmem:$src),
1324                     !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
1325                     [(set VR128:$dst, (F32Int sse_load_f32:$src))]>;
1326
1327   // Vector intrinsic operation, reg
1328   def PSr_Int : PSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1329                     !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1330                     [(set VR128:$dst, (V4F32Int VR128:$src))]> {
1331     let isCommutable = Commutable;
1332   }
1333
1334   // Vector intrinsic operation, mem
1335   def PSm_Int : PSI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1336                     !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1337                     [(set VR128:$dst, (V4F32Int (memopv4f32 addr:$src)))]>;
1338 }
1339
1340 // Square root.
1341 defm SQRT  : sse1_fp_unop_rm<0x51, "sqrt",  fsqrt,
1342                              int_x86_sse_sqrt_ss, int_x86_sse_sqrt_ps>;
1343
1344 // Reciprocal approximations. Note that these typically require refinement
1345 // in order to obtain suitable precision.
1346 defm RSQRT : sse1_fp_unop_rm<0x52, "rsqrt", X86frsqrt,
1347                              int_x86_sse_rsqrt_ss, int_x86_sse_rsqrt_ps>;
1348 defm RCP   : sse1_fp_unop_rm<0x53, "rcp",   X86frcp,
1349                              int_x86_sse_rcp_ss, int_x86_sse_rcp_ps>;
1350
1351 // Compare
1352 let Constraints = "$src1 = $dst" in {
1353   defm CMPPS : sse12_cmp<VR128, f128mem, int_x86_sse_cmp_ps,
1354                  "cmp${cc}ps\t{$src, $dst|$dst, $src}", SSEPackedSingle, SSECC>,
1355                  TB;
1356   defm CMPPD : sse12_cmp<VR128, f128mem, int_x86_sse2_cmp_pd,
1357                  "cmp${cc}pd\t{$src, $dst|$dst, $src}", SSEPackedDouble, SSECC>,
1358                  TB, OpSize;
1359 }
1360 let isAsmParserOnly = 1 in {
1361   defm VCMPPS : sse12_cmp<VR128, f128mem, int_x86_sse_cmp_ps,
1362                  "cmp${cc}ps\t{$src, $src1, $dst|$dst, $src1, $src}",
1363                  SSEPackedSingle, SSECC>, VEX_4V;
1364   defm VCMPPD : sse12_cmp<VR128, f128mem, int_x86_sse2_cmp_pd,
1365                  "cmp${cc}pd\t{$src, $src1, $dst|$dst, $src1, $src}",
1366                  SSEPackedSingle, SSECC>, OpSize, VEX_4V;
1367 }
1368
1369 let isAsmParserOnly = 1, Pattern = []<dag> in {
1370   // Accept explicit immediate argument form instead of comparison code.
1371   let Constraints = "$src1 = $dst" in {
1372     defm CMPPS : sse12_cmp_alt<VR128,
1373                    "cmpps\t{$src2, $src, $dst|$dst, $src, $src2}",
1374                    SSEPackedSingle, i8imm>, TB;
1375     defm CMPPD : sse12_cmp_alt<VR128,
1376                    "cmppd\t{$src2, $src, $dst|$dst, $src, $src2}",
1377                    SSEPackedDouble, i8imm>, TB, OpSize;
1378   }
1379   defm VCMPPS : sse12_cmp_alt<VR128,
1380                  "cmpps\t{$src2, $src, $src1, $dst|$dst, $src1, $src, $src}",
1381                  SSEPackedSingle, i8imm>, VEX_4V;
1382   defm VCMPPD : sse12_cmp_alt<VR128,
1383                  "cmppd\t{$src2, $src, $src1, $dst|$dst, $src1, $src, $src2}",
1384                  SSEPackedSingle, i8imm>, OpSize, VEX_4V;
1385 }
1386
1387 def : Pat<(v4i32 (X86cmpps (v4f32 VR128:$src1), VR128:$src2, imm:$cc)),
1388           (CMPPSrri (v4f32 VR128:$src1), (v4f32 VR128:$src2), imm:$cc)>;
1389 def : Pat<(v4i32 (X86cmpps (v4f32 VR128:$src1), (memop addr:$src2), imm:$cc)),
1390           (CMPPSrmi (v4f32 VR128:$src1), addr:$src2, imm:$cc)>;
1391 def : Pat<(v2i64 (X86cmppd (v2f64 VR128:$src1), VR128:$src2, imm:$cc)),
1392           (CMPPDrri VR128:$src1, VR128:$src2, imm:$cc)>;
1393 def : Pat<(v2i64 (X86cmppd (v2f64 VR128:$src1), (memop addr:$src2), imm:$cc)),
1394           (CMPPDrmi VR128:$src1, addr:$src2, imm:$cc)>;
1395
1396 // Shuffle and unpack instructions
1397 let Constraints = "$src1 = $dst" in {
1398   let isConvertibleToThreeAddress = 1 in // Convert to pshufd
1399     def SHUFPSrri : PSIi8<0xC6, MRMSrcReg,
1400                           (outs VR128:$dst), (ins VR128:$src1,
1401                            VR128:$src2, i8imm:$src3),
1402                           "shufps\t{$src3, $src2, $dst|$dst, $src2, $src3}",
1403                           [(set VR128:$dst,
1404                             (v4f32 (shufp:$src3 VR128:$src1, VR128:$src2)))]>;
1405   def SHUFPSrmi : PSIi8<0xC6, MRMSrcMem,
1406                         (outs VR128:$dst), (ins VR128:$src1,
1407                          f128mem:$src2, i8imm:$src3),
1408                         "shufps\t{$src3, $src2, $dst|$dst, $src2, $src3}",
1409                         [(set VR128:$dst,
1410                           (v4f32 (shufp:$src3
1411                                   VR128:$src1, (memopv4f32 addr:$src2))))]>;
1412   def SHUFPDrri : PDIi8<0xC6, MRMSrcReg,
1413                  (outs VR128:$dst), (ins VR128:$src1, VR128:$src2, i8imm:$src3),
1414                  "shufpd\t{$src3, $src2, $dst|$dst, $src2, $src3}",
1415                  [(set VR128:$dst,
1416                    (v2f64 (shufp:$src3 VR128:$src1, VR128:$src2)))]>;
1417   def SHUFPDrmi : PDIi8<0xC6, MRMSrcMem,
1418                         (outs VR128:$dst), (ins VR128:$src1,
1419                          f128mem:$src2, i8imm:$src3),
1420                         "shufpd\t{$src3, $src2, $dst|$dst, $src2, $src3}",
1421                         [(set VR128:$dst,
1422                           (v2f64 (shufp:$src3
1423                                   VR128:$src1, (memopv2f64 addr:$src2))))]>;
1424
1425   let AddedComplexity = 10 in {
1426     let Constraints = "", isAsmParserOnly = 1 in {
1427     defm VUNPCKHPS: sse12_unpack_interleave<0x15, unpckh, v4f32, memopv4f32,
1428           VR128, f128mem, "unpckhps\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1429                          SSEPackedSingle>, VEX_4V;
1430     defm VUNPCKHPD: sse12_unpack_interleave<0x15, unpckh, v2f64, memopv2f64,
1431           VR128, f128mem, "unpckhpd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1432                          SSEPackedDouble>, OpSize, VEX_4V;
1433     defm VUNPCKLPS: sse12_unpack_interleave<0x14, unpckl, v4f32, memopv4f32,
1434           VR128, f128mem, "unpcklps\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1435                          SSEPackedSingle>, VEX_4V;
1436     defm VUNPCKLPD: sse12_unpack_interleave<0x14, unpckl, v2f64, memopv2f64,
1437           VR128, f128mem, "unpcklpd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1438                          SSEPackedDouble>, OpSize, VEX_4V;
1439     }
1440     defm UNPCKHPS: sse12_unpack_interleave<0x15, unpckh, v4f32, memopv4f32,
1441           VR128, f128mem, "unpckhps\t{$src2, $dst|$dst, $src2}",
1442                          SSEPackedSingle>, TB;
1443     defm UNPCKHPD: sse12_unpack_interleave<0x15, unpckh, v2f64, memopv2f64,
1444           VR128, f128mem, "unpckhpd\t{$src2, $dst|$dst, $src2}",
1445                          SSEPackedDouble>, TB, OpSize;
1446     defm UNPCKLPS: sse12_unpack_interleave<0x14, unpckl, v4f32, memopv4f32,
1447           VR128, f128mem, "unpcklps\t{$src2, $dst|$dst, $src2}",
1448                          SSEPackedSingle>, TB;
1449     defm UNPCKLPD: sse12_unpack_interleave<0x14, unpckl, v2f64, memopv2f64,
1450           VR128, f128mem, "unpcklpd\t{$src2, $dst|$dst, $src2}",
1451                          SSEPackedDouble>, TB, OpSize;
1452   } // AddedComplexity
1453 } // Constraints = "$src1 = $dst"
1454
1455 // Mask creation
1456 def MOVMSKPSrr : PSI<0x50, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
1457                      "movmskps\t{$src, $dst|$dst, $src}",
1458                      [(set GR32:$dst, (int_x86_sse_movmsk_ps VR128:$src))]>;
1459 def MOVMSKPDrr : PDI<0x50, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
1460                      "movmskpd\t{$src, $dst|$dst, $src}",
1461                      [(set GR32:$dst, (int_x86_sse2_movmsk_pd VR128:$src))]>;
1462
1463 // Prefetch intrinsic.
1464 def PREFETCHT0   : PSI<0x18, MRM1m, (outs), (ins i8mem:$src),
1465     "prefetcht0\t$src", [(prefetch addr:$src, imm, (i32 3))]>;
1466 def PREFETCHT1   : PSI<0x18, MRM2m, (outs), (ins i8mem:$src),
1467     "prefetcht1\t$src", [(prefetch addr:$src, imm, (i32 2))]>;
1468 def PREFETCHT2   : PSI<0x18, MRM3m, (outs), (ins i8mem:$src),
1469     "prefetcht2\t$src", [(prefetch addr:$src, imm, (i32 1))]>;
1470 def PREFETCHNTA  : PSI<0x18, MRM0m, (outs), (ins i8mem:$src),
1471     "prefetchnta\t$src", [(prefetch addr:$src, imm, (i32 0))]>;
1472
1473 // Non-temporal stores
1474 def MOVNTPSmr_Int : PSI<0x2B, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
1475                     "movntps\t{$src, $dst|$dst, $src}",
1476                     [(int_x86_sse_movnt_ps addr:$dst, VR128:$src)]>;
1477
1478 let AddedComplexity = 400 in { // Prefer non-temporal versions
1479 def MOVNTPSmr : PSI<0x2B, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1480                     "movntps\t{$src, $dst|$dst, $src}",
1481                     [(alignednontemporalstore (v4f32 VR128:$src), addr:$dst)]>;
1482
1483 def MOVNTDQ_64mr : PDI<0xE7, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1484                     "movntdq\t{$src, $dst|$dst, $src}",
1485                     [(alignednontemporalstore (v2f64 VR128:$src), addr:$dst)]>;
1486
1487 def MOVNTImr : I<0xC3, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src),
1488                  "movnti\t{$src, $dst|$dst, $src}",
1489                  [(nontemporalstore (i32 GR32:$src), addr:$dst)]>,
1490                TB, Requires<[HasSSE2]>;
1491
1492 def MOVNTI_64mr : RI<0xC3, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
1493                      "movnti\t{$src, $dst|$dst, $src}",
1494                      [(nontemporalstore (i64 GR64:$src), addr:$dst)]>,
1495                   TB, Requires<[HasSSE2]>;
1496 }
1497
1498 // Load, store, and memory fence
1499 def SFENCE : I<0xAE, MRM_F8, (outs), (ins), "sfence", [(int_x86_sse_sfence)]>,
1500              TB, Requires<[HasSSE1]>;
1501
1502 // MXCSR register
1503 def LDMXCSR : PSI<0xAE, MRM2m, (outs), (ins i32mem:$src),
1504                   "ldmxcsr\t$src", [(int_x86_sse_ldmxcsr addr:$src)]>;
1505 def STMXCSR : PSI<0xAE, MRM3m, (outs), (ins i32mem:$dst),
1506                   "stmxcsr\t$dst", [(int_x86_sse_stmxcsr addr:$dst)]>;
1507
1508 // Alias instructions that map zero vector to pxor / xorp* for sse.
1509 // We set canFoldAsLoad because this can be converted to a constant-pool
1510 // load of an all-zeros value if folding it would be beneficial.
1511 // FIXME: Change encoding to pseudo!
1512 let isReMaterializable = 1, isAsCheapAsAMove = 1, canFoldAsLoad = 1,
1513     isCodeGenOnly = 1 in {
1514 def V_SET0PS : PSI<0x57, MRMInitReg, (outs VR128:$dst), (ins), "",
1515                  [(set VR128:$dst, (v4f32 immAllZerosV))]>;
1516 def V_SET0PD : PDI<0x57, MRMInitReg, (outs VR128:$dst), (ins), "",
1517                  [(set VR128:$dst, (v2f64 immAllZerosV))]>;
1518 let ExeDomain = SSEPackedInt in
1519 def V_SET0PI : PDI<0xEF, MRMInitReg, (outs VR128:$dst), (ins), "",
1520                  [(set VR128:$dst, (v4i32 immAllZerosV))]>;
1521 }
1522
1523 def : Pat<(v2i64 immAllZerosV), (V_SET0PI)>;
1524 def : Pat<(v8i16 immAllZerosV), (V_SET0PI)>;
1525 def : Pat<(v16i8 immAllZerosV), (V_SET0PI)>;
1526
1527 def : Pat<(f32 (vector_extract (v4f32 VR128:$src), (iPTR 0))),
1528           (f32 (EXTRACT_SUBREG (v4f32 VR128:$src), sub_ss))>;
1529
1530 //===---------------------------------------------------------------------===//
1531 // SSE2 Instructions
1532 //===---------------------------------------------------------------------===//
1533
1534 // Conversion instructions
1535 def CVTSD2SSrr  : SDI<0x5A, MRMSrcReg, (outs FR32:$dst), (ins FR64:$src),
1536                       "cvtsd2ss\t{$src, $dst|$dst, $src}",
1537                       [(set FR32:$dst, (fround FR64:$src))]>;
1538 def CVTSD2SSrm  : I<0x5A, MRMSrcMem, (outs FR32:$dst), (ins f64mem:$src),
1539                       "cvtsd2ss\t{$src, $dst|$dst, $src}",
1540                       [(set FR32:$dst, (fround (loadf64 addr:$src)))]>, XD,
1541                   Requires<[HasSSE2, OptForSize]>;
1542
1543 def CVTPS2DQrr : PDI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1544                      "cvtps2dq\t{$src, $dst|$dst, $src}", []>;
1545 def CVTPS2DQrm : PDI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1546                      "cvtps2dq\t{$src, $dst|$dst, $src}", []>;
1547 def COMISDrr: PDI<0x2F, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
1548                   "comisd\t{$src2, $src1|$src1, $src2}", []>;
1549 def COMISDrm: PDI<0x2F, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
1550                       "comisd\t{$src2, $src1|$src1, $src2}", []>;
1551
1552 // SSE2 instructions with XS prefix
1553 def CVTSS2SDrr : I<0x5A, MRMSrcReg, (outs FR64:$dst), (ins FR32:$src),
1554                    "cvtss2sd\t{$src, $dst|$dst, $src}",
1555                    [(set FR64:$dst, (fextend FR32:$src))]>, XS,
1556                  Requires<[HasSSE2]>;
1557 def CVTSS2SDrm : I<0x5A, MRMSrcMem, (outs FR64:$dst), (ins f32mem:$src),
1558                    "cvtss2sd\t{$src, $dst|$dst, $src}",
1559                    [(set FR64:$dst, (extloadf32 addr:$src))]>, XS,
1560                  Requires<[HasSSE2, OptForSize]>;
1561
1562 def : Pat<(extloadf32 addr:$src),
1563           (CVTSS2SDrr (MOVSSrm addr:$src))>,
1564       Requires<[HasSSE2, OptForSpeed]>;
1565
1566 // Match intrinsics which expect MM and XMM operand(s).
1567 def Int_CVTPI2PDrr : PDI<0x2A, MRMSrcReg, (outs VR128:$dst), (ins VR64:$src),
1568                          "cvtpi2pd\t{$src, $dst|$dst, $src}",
1569                          [(set VR128:$dst, (int_x86_sse_cvtpi2pd VR64:$src))]>;
1570 def Int_CVTPI2PDrm : PDI<0x2A, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
1571                          "cvtpi2pd\t{$src, $dst|$dst, $src}",
1572                          [(set VR128:$dst, (int_x86_sse_cvtpi2pd
1573                                             (load addr:$src)))]>;
1574
1575 // Aliases for intrinsics
1576 def Int_CVTTSD2SIrr : SDI<0x2C, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
1577                           "cvttsd2si\t{$src, $dst|$dst, $src}",
1578                           [(set GR32:$dst,
1579                             (int_x86_sse2_cvttsd2si VR128:$src))]>;
1580 def Int_CVTTSD2SIrm : SDI<0x2C, MRMSrcMem, (outs GR32:$dst), (ins f128mem:$src),
1581                           "cvttsd2si\t{$src, $dst|$dst, $src}",
1582                           [(set GR32:$dst, (int_x86_sse2_cvttsd2si
1583                                             (load addr:$src)))]>;
1584
1585 //===---------------------------------------------------------------------===//
1586 // SSE packed FP Instructions
1587
1588 // Move Instructions
1589 let neverHasSideEffects = 1 in
1590 def MOVAPDrr : PDI<0x28, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1591                    "movapd\t{$src, $dst|$dst, $src}", []>;
1592 let canFoldAsLoad = 1, isReMaterializable = 1 in
1593 def MOVAPDrm : PDI<0x28, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1594                    "movapd\t{$src, $dst|$dst, $src}",
1595                    [(set VR128:$dst, (alignedloadv2f64 addr:$src))]>;
1596
1597 def MOVAPDmr : PDI<0x29, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1598                    "movapd\t{$src, $dst|$dst, $src}",
1599                    [(alignedstore (v2f64 VR128:$src), addr:$dst)]>;
1600
1601 let neverHasSideEffects = 1 in
1602 def MOVUPDrr : PDI<0x10, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1603                    "movupd\t{$src, $dst|$dst, $src}", []>;
1604 let canFoldAsLoad = 1 in
1605 def MOVUPDrm : PDI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1606                    "movupd\t{$src, $dst|$dst, $src}",
1607                    [(set VR128:$dst, (loadv2f64 addr:$src))]>;
1608 def MOVUPDmr : PDI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1609                    "movupd\t{$src, $dst|$dst, $src}",
1610                    [(store (v2f64 VR128:$src), addr:$dst)]>;
1611
1612 // Intrinsic forms of MOVUPD load and store
1613 def MOVUPDrm_Int : PDI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1614                        "movupd\t{$src, $dst|$dst, $src}",
1615                        [(set VR128:$dst, (int_x86_sse2_loadu_pd addr:$src))]>;
1616 def MOVUPDmr_Int : PDI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1617                        "movupd\t{$src, $dst|$dst, $src}",
1618                        [(int_x86_sse2_storeu_pd addr:$dst, VR128:$src)]>;
1619
1620 let Constraints = "$src1 = $dst" in {
1621   let AddedComplexity = 20 in {
1622     def MOVLPDrm : PDI<0x12, MRMSrcMem,
1623                        (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
1624                        "movlpd\t{$src2, $dst|$dst, $src2}",
1625                        [(set VR128:$dst,
1626                          (v2f64 (movlp VR128:$src1,
1627                                  (scalar_to_vector (loadf64 addr:$src2)))))]>;
1628     def MOVHPDrm : PDI<0x16, MRMSrcMem,
1629                        (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
1630                        "movhpd\t{$src2, $dst|$dst, $src2}",
1631                        [(set VR128:$dst,
1632                          (v2f64 (movlhps VR128:$src1,
1633                                  (scalar_to_vector (loadf64 addr:$src2)))))]>;
1634   } // AddedComplexity
1635 } // Constraints = "$src1 = $dst"
1636
1637 def MOVLPDmr : PDI<0x13, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
1638                    "movlpd\t{$src, $dst|$dst, $src}",
1639                    [(store (f64 (vector_extract (v2f64 VR128:$src),
1640                                  (iPTR 0))), addr:$dst)]>;
1641
1642 // v2f64 extract element 1 is always custom lowered to unpack high to low
1643 // and extract element 0 so the non-store version isn't too horrible.
1644 def MOVHPDmr : PDI<0x17, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
1645                    "movhpd\t{$src, $dst|$dst, $src}",
1646                    [(store (f64 (vector_extract
1647                                  (v2f64 (unpckh VR128:$src, (undef))),
1648                                  (iPTR 0))), addr:$dst)]>;
1649
1650 // SSE2 instructions without OpSize prefix
1651 def Int_CVTDQ2PSrr : I<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1652                        "cvtdq2ps\t{$src, $dst|$dst, $src}",
1653                        [(set VR128:$dst, (int_x86_sse2_cvtdq2ps VR128:$src))]>,
1654                      TB, Requires<[HasSSE2]>;
1655 def Int_CVTDQ2PSrm : I<0x5B, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
1656                       "cvtdq2ps\t{$src, $dst|$dst, $src}",
1657                       [(set VR128:$dst, (int_x86_sse2_cvtdq2ps
1658                                         (bitconvert (memopv2i64 addr:$src))))]>,
1659                      TB, Requires<[HasSSE2]>;
1660
1661 // SSE2 instructions with XS prefix
1662 def Int_CVTDQ2PDrr : I<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1663                        "cvtdq2pd\t{$src, $dst|$dst, $src}",
1664                        [(set VR128:$dst, (int_x86_sse2_cvtdq2pd VR128:$src))]>,
1665                      XS, Requires<[HasSSE2]>;
1666 def Int_CVTDQ2PDrm : I<0xE6, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
1667                      "cvtdq2pd\t{$src, $dst|$dst, $src}",
1668                      [(set VR128:$dst, (int_x86_sse2_cvtdq2pd
1669                                         (bitconvert (memopv2i64 addr:$src))))]>,
1670                      XS, Requires<[HasSSE2]>;
1671
1672 def Int_CVTPS2DQrr : PDI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1673                         "cvtps2dq\t{$src, $dst|$dst, $src}",
1674                         [(set VR128:$dst, (int_x86_sse2_cvtps2dq VR128:$src))]>;
1675 def Int_CVTPS2DQrm : PDI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1676                          "cvtps2dq\t{$src, $dst|$dst, $src}",
1677                          [(set VR128:$dst, (int_x86_sse2_cvtps2dq
1678                                             (memop addr:$src)))]>;
1679 // SSE2 packed instructions with XS prefix
1680 def CVTTPS2DQrr : SSI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1681                       "cvttps2dq\t{$src, $dst|$dst, $src}", []>;
1682 def CVTTPS2DQrm : SSI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1683                       "cvttps2dq\t{$src, $dst|$dst, $src}", []>;
1684
1685 def Int_CVTTPS2DQrr : I<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1686                         "cvttps2dq\t{$src, $dst|$dst, $src}",
1687                         [(set VR128:$dst,
1688                               (int_x86_sse2_cvttps2dq VR128:$src))]>,
1689                       XS, Requires<[HasSSE2]>;
1690 def Int_CVTTPS2DQrm : I<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1691                         "cvttps2dq\t{$src, $dst|$dst, $src}",
1692                         [(set VR128:$dst, (int_x86_sse2_cvttps2dq
1693                                            (memop addr:$src)))]>,
1694                       XS, Requires<[HasSSE2]>;
1695
1696 // SSE2 packed instructions with XD prefix
1697 def Int_CVTPD2DQrr : I<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1698                        "cvtpd2dq\t{$src, $dst|$dst, $src}",
1699                        [(set VR128:$dst, (int_x86_sse2_cvtpd2dq VR128:$src))]>,
1700                      XD, Requires<[HasSSE2]>;
1701 def Int_CVTPD2DQrm : I<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1702                        "cvtpd2dq\t{$src, $dst|$dst, $src}",
1703                        [(set VR128:$dst, (int_x86_sse2_cvtpd2dq
1704                                           (memop addr:$src)))]>,
1705                      XD, Requires<[HasSSE2]>;
1706
1707 def Int_CVTTPD2DQrr : PDI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1708                           "cvttpd2dq\t{$src, $dst|$dst, $src}",
1709                        [(set VR128:$dst, (int_x86_sse2_cvttpd2dq VR128:$src))]>;
1710 def Int_CVTTPD2DQrm : PDI<0xE6, MRMSrcMem, (outs VR128:$dst),(ins f128mem:$src),
1711                           "cvttpd2dq\t{$src, $dst|$dst, $src}",
1712                           [(set VR128:$dst, (int_x86_sse2_cvttpd2dq
1713                                              (memop addr:$src)))]>;
1714
1715 // SSE2 instructions without OpSize prefix
1716 def CVTPS2PDrr : I<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1717                        "cvtps2pd\t{$src, $dst|$dst, $src}", []>, TB;
1718 def CVTPS2PDrm : I<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
1719                        "cvtps2pd\t{$src, $dst|$dst, $src}", []>, TB;
1720
1721 def Int_CVTPS2PDrr : I<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1722                        "cvtps2pd\t{$src, $dst|$dst, $src}",
1723                        [(set VR128:$dst, (int_x86_sse2_cvtps2pd VR128:$src))]>,
1724                      TB, Requires<[HasSSE2]>;
1725 def Int_CVTPS2PDrm : I<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
1726                        "cvtps2pd\t{$src, $dst|$dst, $src}",
1727                        [(set VR128:$dst, (int_x86_sse2_cvtps2pd
1728                                           (load addr:$src)))]>,
1729                      TB, Requires<[HasSSE2]>;
1730
1731 def CVTPD2PSrr : PDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1732                      "cvtpd2ps\t{$src, $dst|$dst, $src}", []>;
1733 def CVTPD2PSrm : PDI<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1734                      "cvtpd2ps\t{$src, $dst|$dst, $src}", []>;
1735
1736
1737 def Int_CVTPD2PSrr : PDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1738                          "cvtpd2ps\t{$src, $dst|$dst, $src}",
1739                         [(set VR128:$dst, (int_x86_sse2_cvtpd2ps VR128:$src))]>;
1740 def Int_CVTPD2PSrm : PDI<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1741                          "cvtpd2ps\t{$src, $dst|$dst, $src}",
1742                          [(set VR128:$dst, (int_x86_sse2_cvtpd2ps
1743                                             (memop addr:$src)))]>;
1744
1745 // Match intrinsics which expect XMM operand(s).
1746 // Aliases for intrinsics
1747 let Constraints = "$src1 = $dst" in {
1748 def Int_CVTSI2SDrr: SDI<0x2A, MRMSrcReg,
1749                         (outs VR128:$dst), (ins VR128:$src1, GR32:$src2),
1750                         "cvtsi2sd\t{$src2, $dst|$dst, $src2}",
1751                         [(set VR128:$dst, (int_x86_sse2_cvtsi2sd VR128:$src1,
1752                                            GR32:$src2))]>;
1753 def Int_CVTSI2SDrm: SDI<0x2A, MRMSrcMem,
1754                         (outs VR128:$dst), (ins VR128:$src1, i32mem:$src2),
1755                         "cvtsi2sd\t{$src2, $dst|$dst, $src2}",
1756                         [(set VR128:$dst, (int_x86_sse2_cvtsi2sd VR128:$src1,
1757                                            (loadi32 addr:$src2)))]>;
1758 def Int_CVTSD2SSrr: SDI<0x5A, MRMSrcReg,
1759                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1760                    "cvtsd2ss\t{$src2, $dst|$dst, $src2}",
1761                    [(set VR128:$dst, (int_x86_sse2_cvtsd2ss VR128:$src1,
1762                                       VR128:$src2))]>;
1763 def Int_CVTSD2SSrm: SDI<0x5A, MRMSrcMem,
1764                         (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
1765                    "cvtsd2ss\t{$src2, $dst|$dst, $src2}",
1766                    [(set VR128:$dst, (int_x86_sse2_cvtsd2ss VR128:$src1,
1767                                       (load addr:$src2)))]>;
1768 def Int_CVTSS2SDrr: I<0x5A, MRMSrcReg,
1769                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1770                     "cvtss2sd\t{$src2, $dst|$dst, $src2}",
1771                     [(set VR128:$dst, (int_x86_sse2_cvtss2sd VR128:$src1,
1772                                        VR128:$src2))]>, XS,
1773                     Requires<[HasSSE2]>;
1774 def Int_CVTSS2SDrm: I<0x5A, MRMSrcMem,
1775                       (outs VR128:$dst), (ins VR128:$src1, f32mem:$src2),
1776                     "cvtss2sd\t{$src2, $dst|$dst, $src2}",
1777                     [(set VR128:$dst, (int_x86_sse2_cvtss2sd VR128:$src1,
1778                                        (load addr:$src2)))]>, XS,
1779                     Requires<[HasSSE2]>;
1780 }
1781
1782 // Arithmetic
1783
1784 /// sse2_fp_unop_rm - SSE2 unops come in both scalar and vector forms.
1785 ///
1786 /// In addition, we also have a special variant of the scalar form here to
1787 /// represent the associated intrinsic operation.  This form is unlike the
1788 /// plain scalar form, in that it takes an entire vector (instead of a
1789 /// scalar) and leaves the top elements undefined.
1790 ///
1791 /// And, we have a special variant form for a full-vector intrinsic form.
1792 ///
1793 /// These four forms can each have a reg or a mem operand, so there are a
1794 /// total of eight "instructions".
1795 ///
1796 multiclass sse2_fp_unop_rm<bits<8> opc, string OpcodeStr,
1797                            SDNode OpNode,
1798                            Intrinsic F64Int,
1799                            Intrinsic V2F64Int,
1800                            bit Commutable = 0> {
1801   // Scalar operation, reg.
1802   def SDr : SDI<opc, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src),
1803                 !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1804                 [(set FR64:$dst, (OpNode FR64:$src))]> {
1805     let isCommutable = Commutable;
1806   }
1807
1808   // Scalar operation, mem.
1809   def SDm : SDI<opc, MRMSrcMem, (outs FR64:$dst), (ins f64mem:$src),
1810                 !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1811                 [(set FR64:$dst, (OpNode (load addr:$src)))]>;
1812
1813   // Vector operation, reg.
1814   def PDr : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1815               !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1816               [(set VR128:$dst, (v2f64 (OpNode VR128:$src)))]> {
1817     let isCommutable = Commutable;
1818   }
1819
1820   // Vector operation, mem.
1821   def PDm : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1822                 !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1823                 [(set VR128:$dst, (OpNode (memopv2f64 addr:$src)))]>;
1824
1825   // Intrinsic operation, reg.
1826   def SDr_Int : SDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1827                     !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1828                     [(set VR128:$dst, (F64Int VR128:$src))]> {
1829     let isCommutable = Commutable;
1830   }
1831
1832   // Intrinsic operation, mem.
1833   def SDm_Int : SDI<opc, MRMSrcMem, (outs VR128:$dst), (ins sdmem:$src),
1834                     !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1835                     [(set VR128:$dst, (F64Int sse_load_f64:$src))]>;
1836
1837   // Vector intrinsic operation, reg
1838   def PDr_Int : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1839                     !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1840                     [(set VR128:$dst, (V2F64Int VR128:$src))]> {
1841     let isCommutable = Commutable;
1842   }
1843
1844   // Vector intrinsic operation, mem
1845   def PDm_Int : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1846                     !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1847                     [(set VR128:$dst, (V2F64Int (memopv2f64 addr:$src)))]>;
1848 }
1849
1850 // Square root.
1851 defm SQRT  : sse2_fp_unop_rm<0x51, "sqrt",  fsqrt,
1852                              int_x86_sse2_sqrt_sd, int_x86_sse2_sqrt_pd>;
1853
1854 // There is no f64 version of the reciprocal approximation instructions.
1855
1856 //===---------------------------------------------------------------------===//
1857 // SSE integer instructions
1858 let ExeDomain = SSEPackedInt in {
1859
1860 // Move Instructions
1861 let neverHasSideEffects = 1 in
1862 def MOVDQArr : PDI<0x6F, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1863                    "movdqa\t{$src, $dst|$dst, $src}", []>;
1864 let canFoldAsLoad = 1, mayLoad = 1 in
1865 def MOVDQArm : PDI<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
1866                    "movdqa\t{$src, $dst|$dst, $src}",
1867                    [/*(set VR128:$dst, (alignedloadv2i64 addr:$src))*/]>;
1868 let mayStore = 1 in
1869 def MOVDQAmr : PDI<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
1870                    "movdqa\t{$src, $dst|$dst, $src}",
1871                    [/*(alignedstore (v2i64 VR128:$src), addr:$dst)*/]>;
1872 let canFoldAsLoad = 1, mayLoad = 1 in
1873 def MOVDQUrm :   I<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
1874                    "movdqu\t{$src, $dst|$dst, $src}",
1875                    [/*(set VR128:$dst, (loadv2i64 addr:$src))*/]>,
1876                  XS, Requires<[HasSSE2]>;
1877 let mayStore = 1 in
1878 def MOVDQUmr :   I<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
1879                    "movdqu\t{$src, $dst|$dst, $src}",
1880                    [/*(store (v2i64 VR128:$src), addr:$dst)*/]>,
1881                  XS, Requires<[HasSSE2]>;
1882
1883 // Intrinsic forms of MOVDQU load and store
1884 let canFoldAsLoad = 1 in
1885 def MOVDQUrm_Int :   I<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
1886                        "movdqu\t{$src, $dst|$dst, $src}",
1887                        [(set VR128:$dst, (int_x86_sse2_loadu_dq addr:$src))]>,
1888                  XS, Requires<[HasSSE2]>;
1889 def MOVDQUmr_Int :   I<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
1890                        "movdqu\t{$src, $dst|$dst, $src}",
1891                        [(int_x86_sse2_storeu_dq addr:$dst, VR128:$src)]>,
1892                      XS, Requires<[HasSSE2]>;
1893
1894 let Constraints = "$src1 = $dst" in {
1895
1896 multiclass PDI_binop_rm_int<bits<8> opc, string OpcodeStr, Intrinsic IntId,
1897                             bit Commutable = 0> {
1898   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst),
1899                                (ins VR128:$src1, VR128:$src2),
1900                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1901                [(set VR128:$dst, (IntId VR128:$src1, VR128:$src2))]> {
1902     let isCommutable = Commutable;
1903   }
1904   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst),
1905                                (ins VR128:$src1, i128mem:$src2),
1906                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1907                [(set VR128:$dst, (IntId VR128:$src1,
1908                                         (bitconvert (memopv2i64
1909                                                      addr:$src2))))]>;
1910 }
1911
1912 multiclass PDI_binop_rmi_int<bits<8> opc, bits<8> opc2, Format ImmForm,
1913                              string OpcodeStr,
1914                              Intrinsic IntId, Intrinsic IntId2> {
1915   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst),
1916                                (ins VR128:$src1, VR128:$src2),
1917                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1918                [(set VR128:$dst, (IntId VR128:$src1, VR128:$src2))]>;
1919   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst),
1920                                (ins VR128:$src1, i128mem:$src2),
1921                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1922                [(set VR128:$dst, (IntId VR128:$src1,
1923                                       (bitconvert (memopv2i64 addr:$src2))))]>;
1924   def ri : PDIi8<opc2, ImmForm, (outs VR128:$dst),
1925                                 (ins VR128:$src1, i32i8imm:$src2),
1926                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1927                [(set VR128:$dst, (IntId2 VR128:$src1, (i32 imm:$src2)))]>;
1928 }
1929
1930 /// PDI_binop_rm - Simple SSE2 binary operator.
1931 multiclass PDI_binop_rm<bits<8> opc, string OpcodeStr, SDNode OpNode,
1932                         ValueType OpVT, bit Commutable = 0> {
1933   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst),
1934                                (ins VR128:$src1, VR128:$src2),
1935                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1936                [(set VR128:$dst, (OpVT (OpNode VR128:$src1, VR128:$src2)))]> {
1937     let isCommutable = Commutable;
1938   }
1939   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst),
1940                                (ins VR128:$src1, i128mem:$src2),
1941                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1942                [(set VR128:$dst, (OpVT (OpNode VR128:$src1,
1943                                      (bitconvert (memopv2i64 addr:$src2)))))]>;
1944 }
1945
1946 /// PDI_binop_rm_v2i64 - Simple SSE2 binary operator whose type is v2i64.
1947 ///
1948 /// FIXME: we could eliminate this and use PDI_binop_rm instead if tblgen knew
1949 /// to collapse (bitconvert VT to VT) into its operand.
1950 ///
1951 multiclass PDI_binop_rm_v2i64<bits<8> opc, string OpcodeStr, SDNode OpNode,
1952                               bit Commutable = 0> {
1953   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst),
1954                (ins VR128:$src1, VR128:$src2),
1955                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1956                [(set VR128:$dst, (v2i64 (OpNode VR128:$src1, VR128:$src2)))]> {
1957     let isCommutable = Commutable;
1958   }
1959   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst),
1960                (ins VR128:$src1, i128mem:$src2),
1961                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1962                [(set VR128:$dst, (OpNode VR128:$src1,
1963                (memopv2i64 addr:$src2)))]>;
1964 }
1965
1966 } // Constraints = "$src1 = $dst"
1967 } // ExeDomain = SSEPackedInt
1968
1969 // 128-bit Integer Arithmetic
1970
1971 defm PADDB : PDI_binop_rm<0xFC, "paddb", add, v16i8, 1>;
1972 defm PADDW : PDI_binop_rm<0xFD, "paddw", add, v8i16, 1>;
1973 defm PADDD : PDI_binop_rm<0xFE, "paddd", add, v4i32, 1>;
1974 defm PADDQ : PDI_binop_rm_v2i64<0xD4, "paddq", add, 1>;
1975
1976 defm PADDSB  : PDI_binop_rm_int<0xEC, "paddsb" , int_x86_sse2_padds_b, 1>;
1977 defm PADDSW  : PDI_binop_rm_int<0xED, "paddsw" , int_x86_sse2_padds_w, 1>;
1978 defm PADDUSB : PDI_binop_rm_int<0xDC, "paddusb", int_x86_sse2_paddus_b, 1>;
1979 defm PADDUSW : PDI_binop_rm_int<0xDD, "paddusw", int_x86_sse2_paddus_w, 1>;
1980
1981 defm PSUBB : PDI_binop_rm<0xF8, "psubb", sub, v16i8>;
1982 defm PSUBW : PDI_binop_rm<0xF9, "psubw", sub, v8i16>;
1983 defm PSUBD : PDI_binop_rm<0xFA, "psubd", sub, v4i32>;
1984 defm PSUBQ : PDI_binop_rm_v2i64<0xFB, "psubq", sub>;
1985
1986 defm PSUBSB  : PDI_binop_rm_int<0xE8, "psubsb" , int_x86_sse2_psubs_b>;
1987 defm PSUBSW  : PDI_binop_rm_int<0xE9, "psubsw" , int_x86_sse2_psubs_w>;
1988 defm PSUBUSB : PDI_binop_rm_int<0xD8, "psubusb", int_x86_sse2_psubus_b>;
1989 defm PSUBUSW : PDI_binop_rm_int<0xD9, "psubusw", int_x86_sse2_psubus_w>;
1990
1991 defm PMULLW : PDI_binop_rm<0xD5, "pmullw", mul, v8i16, 1>;
1992
1993 defm PMULHUW : PDI_binop_rm_int<0xE4, "pmulhuw", int_x86_sse2_pmulhu_w, 1>;
1994 defm PMULHW  : PDI_binop_rm_int<0xE5, "pmulhw" , int_x86_sse2_pmulh_w , 1>;
1995 defm PMULUDQ : PDI_binop_rm_int<0xF4, "pmuludq", int_x86_sse2_pmulu_dq, 1>;
1996
1997 defm PMADDWD : PDI_binop_rm_int<0xF5, "pmaddwd", int_x86_sse2_pmadd_wd, 1>;
1998
1999 defm PAVGB  : PDI_binop_rm_int<0xE0, "pavgb", int_x86_sse2_pavg_b, 1>;
2000 defm PAVGW  : PDI_binop_rm_int<0xE3, "pavgw", int_x86_sse2_pavg_w, 1>;
2001
2002
2003 defm PMINUB : PDI_binop_rm_int<0xDA, "pminub", int_x86_sse2_pminu_b, 1>;
2004 defm PMINSW : PDI_binop_rm_int<0xEA, "pminsw", int_x86_sse2_pmins_w, 1>;
2005 defm PMAXUB : PDI_binop_rm_int<0xDE, "pmaxub", int_x86_sse2_pmaxu_b, 1>;
2006 defm PMAXSW : PDI_binop_rm_int<0xEE, "pmaxsw", int_x86_sse2_pmaxs_w, 1>;
2007 defm PSADBW : PDI_binop_rm_int<0xF6, "psadbw", int_x86_sse2_psad_bw, 1>;
2008
2009
2010 defm PSLLW : PDI_binop_rmi_int<0xF1, 0x71, MRM6r, "psllw",
2011                                int_x86_sse2_psll_w, int_x86_sse2_pslli_w>;
2012 defm PSLLD : PDI_binop_rmi_int<0xF2, 0x72, MRM6r, "pslld",
2013                                int_x86_sse2_psll_d, int_x86_sse2_pslli_d>;
2014 defm PSLLQ : PDI_binop_rmi_int<0xF3, 0x73, MRM6r, "psllq",
2015                                int_x86_sse2_psll_q, int_x86_sse2_pslli_q>;
2016
2017 defm PSRLW : PDI_binop_rmi_int<0xD1, 0x71, MRM2r, "psrlw",
2018                                int_x86_sse2_psrl_w, int_x86_sse2_psrli_w>;
2019 defm PSRLD : PDI_binop_rmi_int<0xD2, 0x72, MRM2r, "psrld",
2020                                int_x86_sse2_psrl_d, int_x86_sse2_psrli_d>;
2021 defm PSRLQ : PDI_binop_rmi_int<0xD3, 0x73, MRM2r, "psrlq",
2022                                int_x86_sse2_psrl_q, int_x86_sse2_psrli_q>;
2023
2024 defm PSRAW : PDI_binop_rmi_int<0xE1, 0x71, MRM4r, "psraw",
2025                                int_x86_sse2_psra_w, int_x86_sse2_psrai_w>;
2026 defm PSRAD : PDI_binop_rmi_int<0xE2, 0x72, MRM4r, "psrad",
2027                                int_x86_sse2_psra_d, int_x86_sse2_psrai_d>;
2028
2029 // 128-bit logical shifts.
2030 let Constraints = "$src1 = $dst", neverHasSideEffects = 1,
2031     ExeDomain = SSEPackedInt in {
2032   def PSLLDQri : PDIi8<0x73, MRM7r,
2033                        (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
2034                        "pslldq\t{$src2, $dst|$dst, $src2}", []>;
2035   def PSRLDQri : PDIi8<0x73, MRM3r,
2036                        (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
2037                        "psrldq\t{$src2, $dst|$dst, $src2}", []>;
2038   // PSRADQri doesn't exist in SSE[1-3].
2039 }
2040
2041 let Predicates = [HasSSE2] in {
2042   def : Pat<(int_x86_sse2_psll_dq VR128:$src1, imm:$src2),
2043             (v2i64 (PSLLDQri VR128:$src1, (BYTE_imm imm:$src2)))>;
2044   def : Pat<(int_x86_sse2_psrl_dq VR128:$src1, imm:$src2),
2045             (v2i64 (PSRLDQri VR128:$src1, (BYTE_imm imm:$src2)))>;
2046   def : Pat<(int_x86_sse2_psll_dq_bs VR128:$src1, imm:$src2),
2047             (v2i64 (PSLLDQri VR128:$src1, imm:$src2))>;
2048   def : Pat<(int_x86_sse2_psrl_dq_bs VR128:$src1, imm:$src2),
2049             (v2i64 (PSRLDQri VR128:$src1, imm:$src2))>;
2050   def : Pat<(v2f64 (X86fsrl VR128:$src1, i32immSExt8:$src2)),
2051             (v2f64 (PSRLDQri VR128:$src1, (BYTE_imm imm:$src2)))>;
2052
2053   // Shift up / down and insert zero's.
2054   def : Pat<(v2i64 (X86vshl  VR128:$src, (i8 imm:$amt))),
2055             (v2i64 (PSLLDQri VR128:$src, (BYTE_imm imm:$amt)))>;
2056   def : Pat<(v2i64 (X86vshr  VR128:$src, (i8 imm:$amt))),
2057             (v2i64 (PSRLDQri VR128:$src, (BYTE_imm imm:$amt)))>;
2058 }
2059
2060 // Logical
2061 defm PAND : PDI_binop_rm_v2i64<0xDB, "pand", and, 1>;
2062 defm POR  : PDI_binop_rm_v2i64<0xEB, "por" , or , 1>;
2063 defm PXOR : PDI_binop_rm_v2i64<0xEF, "pxor", xor, 1>;
2064
2065 let Constraints = "$src1 = $dst", ExeDomain = SSEPackedInt in {
2066   def PANDNrr : PDI<0xDF, MRMSrcReg,
2067                     (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2068                     "pandn\t{$src2, $dst|$dst, $src2}",
2069                     [(set VR128:$dst, (v2i64 (and (vnot VR128:$src1),
2070                                               VR128:$src2)))]>;
2071
2072   def PANDNrm : PDI<0xDF, MRMSrcMem,
2073                     (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2074                     "pandn\t{$src2, $dst|$dst, $src2}",
2075                     [(set VR128:$dst, (v2i64 (and (vnot VR128:$src1),
2076                                               (memopv2i64 addr:$src2))))]>;
2077 }
2078
2079 // SSE2 Integer comparison
2080 defm PCMPEQB  : PDI_binop_rm_int<0x74, "pcmpeqb", int_x86_sse2_pcmpeq_b>;
2081 defm PCMPEQW  : PDI_binop_rm_int<0x75, "pcmpeqw", int_x86_sse2_pcmpeq_w>;
2082 defm PCMPEQD  : PDI_binop_rm_int<0x76, "pcmpeqd", int_x86_sse2_pcmpeq_d>;
2083 defm PCMPGTB  : PDI_binop_rm_int<0x64, "pcmpgtb", int_x86_sse2_pcmpgt_b>;
2084 defm PCMPGTW  : PDI_binop_rm_int<0x65, "pcmpgtw", int_x86_sse2_pcmpgt_w>;
2085 defm PCMPGTD  : PDI_binop_rm_int<0x66, "pcmpgtd", int_x86_sse2_pcmpgt_d>;
2086
2087 def : Pat<(v16i8 (X86pcmpeqb VR128:$src1, VR128:$src2)),
2088           (PCMPEQBrr VR128:$src1, VR128:$src2)>;
2089 def : Pat<(v16i8 (X86pcmpeqb VR128:$src1, (memop addr:$src2))),
2090           (PCMPEQBrm VR128:$src1, addr:$src2)>;
2091 def : Pat<(v8i16 (X86pcmpeqw VR128:$src1, VR128:$src2)),
2092           (PCMPEQWrr VR128:$src1, VR128:$src2)>;
2093 def : Pat<(v8i16 (X86pcmpeqw VR128:$src1, (memop addr:$src2))),
2094           (PCMPEQWrm VR128:$src1, addr:$src2)>;
2095 def : Pat<(v4i32 (X86pcmpeqd VR128:$src1, VR128:$src2)),
2096           (PCMPEQDrr VR128:$src1, VR128:$src2)>;
2097 def : Pat<(v4i32 (X86pcmpeqd VR128:$src1, (memop addr:$src2))),
2098           (PCMPEQDrm VR128:$src1, addr:$src2)>;
2099
2100 def : Pat<(v16i8 (X86pcmpgtb VR128:$src1, VR128:$src2)),
2101           (PCMPGTBrr VR128:$src1, VR128:$src2)>;
2102 def : Pat<(v16i8 (X86pcmpgtb VR128:$src1, (memop addr:$src2))),
2103           (PCMPGTBrm VR128:$src1, addr:$src2)>;
2104 def : Pat<(v8i16 (X86pcmpgtw VR128:$src1, VR128:$src2)),
2105           (PCMPGTWrr VR128:$src1, VR128:$src2)>;
2106 def : Pat<(v8i16 (X86pcmpgtw VR128:$src1, (memop addr:$src2))),
2107           (PCMPGTWrm VR128:$src1, addr:$src2)>;
2108 def : Pat<(v4i32 (X86pcmpgtd VR128:$src1, VR128:$src2)),
2109           (PCMPGTDrr VR128:$src1, VR128:$src2)>;
2110 def : Pat<(v4i32 (X86pcmpgtd VR128:$src1, (memop addr:$src2))),
2111           (PCMPGTDrm VR128:$src1, addr:$src2)>;
2112
2113
2114 // Pack instructions
2115 defm PACKSSWB : PDI_binop_rm_int<0x63, "packsswb", int_x86_sse2_packsswb_128>;
2116 defm PACKSSDW : PDI_binop_rm_int<0x6B, "packssdw", int_x86_sse2_packssdw_128>;
2117 defm PACKUSWB : PDI_binop_rm_int<0x67, "packuswb", int_x86_sse2_packuswb_128>;
2118
2119 let ExeDomain = SSEPackedInt in {
2120
2121 // Shuffle and unpack instructions
2122 let AddedComplexity = 5 in {
2123 def PSHUFDri : PDIi8<0x70, MRMSrcReg,
2124                      (outs VR128:$dst), (ins VR128:$src1, i8imm:$src2),
2125                      "pshufd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2126                      [(set VR128:$dst, (v4i32 (pshufd:$src2
2127                                                VR128:$src1, (undef))))]>;
2128 def PSHUFDmi : PDIi8<0x70, MRMSrcMem,
2129                      (outs VR128:$dst), (ins i128mem:$src1, i8imm:$src2),
2130                      "pshufd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2131                      [(set VR128:$dst, (v4i32 (pshufd:$src2
2132                                              (bc_v4i32 (memopv2i64 addr:$src1)),
2133                                              (undef))))]>;
2134 }
2135
2136 // SSE2 with ImmT == Imm8 and XS prefix.
2137 def PSHUFHWri : Ii8<0x70, MRMSrcReg,
2138                     (outs VR128:$dst), (ins VR128:$src1, i8imm:$src2),
2139                     "pshufhw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2140                     [(set VR128:$dst, (v8i16 (pshufhw:$src2 VR128:$src1,
2141                                                             (undef))))]>,
2142                 XS, Requires<[HasSSE2]>;
2143 def PSHUFHWmi : Ii8<0x70, MRMSrcMem,
2144                     (outs VR128:$dst), (ins i128mem:$src1, i8imm:$src2),
2145                     "pshufhw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2146                     [(set VR128:$dst, (v8i16 (pshufhw:$src2
2147                                             (bc_v8i16 (memopv2i64 addr:$src1)),
2148                                             (undef))))]>,
2149                 XS, Requires<[HasSSE2]>;
2150
2151 // SSE2 with ImmT == Imm8 and XD prefix.
2152 def PSHUFLWri : Ii8<0x70, MRMSrcReg,
2153                     (outs VR128:$dst), (ins VR128:$src1, i8imm:$src2),
2154                     "pshuflw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2155                     [(set VR128:$dst, (v8i16 (pshuflw:$src2 VR128:$src1,
2156                                                             (undef))))]>,
2157                 XD, Requires<[HasSSE2]>;
2158 def PSHUFLWmi : Ii8<0x70, MRMSrcMem,
2159                     (outs VR128:$dst), (ins i128mem:$src1, i8imm:$src2),
2160                     "pshuflw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2161                     [(set VR128:$dst, (v8i16 (pshuflw:$src2
2162                                              (bc_v8i16 (memopv2i64 addr:$src1)),
2163                                              (undef))))]>,
2164                 XD, Requires<[HasSSE2]>;
2165
2166 // Unpack instructions
2167 multiclass sse2_unpack<bits<8> opc, string OpcodeStr, ValueType vt,
2168                        PatFrag unp_frag, PatFrag bc_frag> {
2169   def rr : PDI<opc, MRMSrcReg,
2170                (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2171                !strconcat(OpcodeStr,"\t{$src2, $dst|$dst, $src2}"),
2172                [(set VR128:$dst, (vt (unp_frag VR128:$src1, VR128:$src2)))]>;
2173   def rm : PDI<opc, MRMSrcMem,
2174                (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2175                !strconcat(OpcodeStr,"\t{$src2, $dst|$dst, $src2}"),
2176                [(set VR128:$dst, (unp_frag VR128:$src1,
2177                                            (bc_frag (memopv2i64
2178                                                         addr:$src2))))]>;
2179 }
2180
2181 let Constraints = "$src1 = $dst" in {
2182   defm PUNPCKLBW  : sse2_unpack<0x60, "punpcklbw", v16i8, unpckl, bc_v16i8>;
2183   defm PUNPCKLWD  : sse2_unpack<0x61, "punpcklwd", v8i16, unpckl, bc_v8i16>;
2184   defm PUNPCKLDQ  : sse2_unpack<0x62, "punpckldq", v4i32, unpckl, bc_v4i32>;
2185
2186   /// FIXME: we could eliminate this and use sse2_unpack instead if tblgen
2187   /// knew to collapse (bitconvert VT to VT) into its operand.
2188   def PUNPCKLQDQrr : PDI<0x6C, MRMSrcReg,
2189                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2190                          "punpcklqdq\t{$src2, $dst|$dst, $src2}",
2191                         [(set VR128:$dst,
2192                           (v2i64 (unpckl VR128:$src1, VR128:$src2)))]>;
2193   def PUNPCKLQDQrm : PDI<0x6C, MRMSrcMem,
2194                          (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2195                          "punpcklqdq\t{$src2, $dst|$dst, $src2}",
2196                         [(set VR128:$dst,
2197                           (v2i64 (unpckl VR128:$src1,
2198                                          (memopv2i64 addr:$src2))))]>;
2199
2200   defm PUNPCKHBW  : sse2_unpack<0x68, "punpckhbw", v16i8, unpckh, bc_v16i8>;
2201   defm PUNPCKHWD  : sse2_unpack<0x69, "punpckhwd", v8i16, unpckh, bc_v8i16>;
2202   defm PUNPCKHDQ  : sse2_unpack<0x6A, "punpckhdq", v4i32, unpckh, bc_v4i32>;
2203
2204   /// FIXME: we could eliminate this and use sse2_unpack instead if tblgen
2205   /// knew to collapse (bitconvert VT to VT) into its operand.
2206   def PUNPCKHQDQrr : PDI<0x6D, MRMSrcReg,
2207                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2208                          "punpckhqdq\t{$src2, $dst|$dst, $src2}",
2209                         [(set VR128:$dst,
2210                           (v2i64 (unpckh VR128:$src1, VR128:$src2)))]>;
2211   def PUNPCKHQDQrm : PDI<0x6D, MRMSrcMem,
2212                         (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2213                         "punpckhqdq\t{$src2, $dst|$dst, $src2}",
2214                         [(set VR128:$dst,
2215                           (v2i64 (unpckh VR128:$src1,
2216                                          (memopv2i64 addr:$src2))))]>;
2217 }
2218
2219 // Extract / Insert
2220 def PEXTRWri : PDIi8<0xC5, MRMSrcReg,
2221                     (outs GR32:$dst), (ins VR128:$src1, i32i8imm:$src2),
2222                     "pextrw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2223                     [(set GR32:$dst, (X86pextrw (v8i16 VR128:$src1),
2224                                                 imm:$src2))]>;
2225 let Constraints = "$src1 = $dst" in {
2226   def PINSRWrri : PDIi8<0xC4, MRMSrcReg,
2227                        (outs VR128:$dst), (ins VR128:$src1,
2228                         GR32:$src2, i32i8imm:$src3),
2229                        "pinsrw\t{$src3, $src2, $dst|$dst, $src2, $src3}",
2230                        [(set VR128:$dst,
2231                          (X86pinsrw VR128:$src1, GR32:$src2, imm:$src3))]>;
2232   def PINSRWrmi : PDIi8<0xC4, MRMSrcMem,
2233                        (outs VR128:$dst), (ins VR128:$src1,
2234                         i16mem:$src2, i32i8imm:$src3),
2235                        "pinsrw\t{$src3, $src2, $dst|$dst, $src2, $src3}",
2236                        [(set VR128:$dst,
2237                          (X86pinsrw VR128:$src1, (extloadi16 addr:$src2),
2238                                     imm:$src3))]>;
2239 }
2240
2241 // Mask creation
2242 def PMOVMSKBrr : PDI<0xD7, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
2243                      "pmovmskb\t{$src, $dst|$dst, $src}",
2244                      [(set GR32:$dst, (int_x86_sse2_pmovmskb_128 VR128:$src))]>;
2245
2246 // Conditional store
2247 let Uses = [EDI] in
2248 def MASKMOVDQU : PDI<0xF7, MRMSrcReg, (outs), (ins VR128:$src, VR128:$mask),
2249                      "maskmovdqu\t{$mask, $src|$src, $mask}",
2250                      [(int_x86_sse2_maskmov_dqu VR128:$src, VR128:$mask, EDI)]>;
2251
2252 let Uses = [RDI] in
2253 def MASKMOVDQU64 : PDI<0xF7, MRMSrcReg, (outs), (ins VR128:$src, VR128:$mask),
2254                      "maskmovdqu\t{$mask, $src|$src, $mask}",
2255                      [(int_x86_sse2_maskmov_dqu VR128:$src, VR128:$mask, RDI)]>;
2256
2257 } // ExeDomain = SSEPackedInt
2258
2259 // Non-temporal stores
2260 def MOVNTPDmr_Int : PDI<0x2B, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
2261                         "movntpd\t{$src, $dst|$dst, $src}",
2262                         [(int_x86_sse2_movnt_pd addr:$dst, VR128:$src)]>;
2263 let ExeDomain = SSEPackedInt in
2264 def MOVNTDQmr_Int : PDI<0xE7, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
2265                         "movntdq\t{$src, $dst|$dst, $src}",
2266                         [(int_x86_sse2_movnt_dq addr:$dst, VR128:$src)]>;
2267 def MOVNTImr_Int  :   I<0xC3, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src),
2268                     "movnti\t{$src, $dst|$dst, $src}",
2269                     [(int_x86_sse2_movnt_i addr:$dst, GR32:$src)]>,
2270                   TB, Requires<[HasSSE2]>;
2271
2272 let AddedComplexity = 400 in { // Prefer non-temporal versions
2273 def MOVNTPDmr : PDI<0x2B, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
2274                     "movntpd\t{$src, $dst|$dst, $src}",
2275                     [(alignednontemporalstore(v2f64 VR128:$src), addr:$dst)]>;
2276
2277 let ExeDomain = SSEPackedInt in
2278 def MOVNTDQmr : PDI<0xE7, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
2279                     "movntdq\t{$src, $dst|$dst, $src}",
2280                     [(alignednontemporalstore (v4f32 VR128:$src), addr:$dst)]>;
2281 }
2282
2283 // Flush cache
2284 def CLFLUSH : I<0xAE, MRM7m, (outs), (ins i8mem:$src),
2285                "clflush\t$src", [(int_x86_sse2_clflush addr:$src)]>,
2286               TB, Requires<[HasSSE2]>;
2287
2288 // Load, store, and memory fence
2289 def LFENCE : I<0xAE, MRM_E8, (outs), (ins),
2290                "lfence", [(int_x86_sse2_lfence)]>, TB, Requires<[HasSSE2]>;
2291 def MFENCE : I<0xAE, MRM_F0, (outs), (ins),
2292                "mfence", [(int_x86_sse2_mfence)]>, TB, Requires<[HasSSE2]>;
2293
2294 // Pause. This "instruction" is encoded as "rep; nop", so even though it
2295 // was introduced with SSE2, it's backward compatible.
2296 def PAUSE : I<0x90, RawFrm, (outs), (ins), "pause", []>, REP;
2297
2298 //TODO: custom lower this so as to never even generate the noop
2299 def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm),
2300            (i8 0)), (NOOP)>;
2301 def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
2302 def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
2303 def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm),
2304            (i8 1)), (MFENCE)>;
2305
2306 // Alias instructions that map zero vector to pxor / xorp* for sse.
2307 // We set canFoldAsLoad because this can be converted to a constant-pool
2308 // load of an all-ones value if folding it would be beneficial.
2309 let isReMaterializable = 1, isAsCheapAsAMove = 1, canFoldAsLoad = 1,
2310     isCodeGenOnly = 1, ExeDomain = SSEPackedInt in
2311   // FIXME: Change encoding to pseudo.
2312   def V_SETALLONES : PDI<0x76, MRMInitReg, (outs VR128:$dst), (ins), "",
2313                          [(set VR128:$dst, (v4i32 immAllOnesV))]>;
2314
2315 def MOVDI2PDIrr : PDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR32:$src),
2316                       "movd\t{$src, $dst|$dst, $src}",
2317                       [(set VR128:$dst,
2318                         (v4i32 (scalar_to_vector GR32:$src)))]>;
2319 def MOVDI2PDIrm : PDI<0x6E, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
2320                       "movd\t{$src, $dst|$dst, $src}",
2321                       [(set VR128:$dst,
2322                         (v4i32 (scalar_to_vector (loadi32 addr:$src))))]>;
2323
2324 def MOVDI2SSrr  : PDI<0x6E, MRMSrcReg, (outs FR32:$dst), (ins GR32:$src),
2325                       "movd\t{$src, $dst|$dst, $src}",
2326                       [(set FR32:$dst, (bitconvert GR32:$src))]>;
2327
2328 def MOVDI2SSrm  : PDI<0x6E, MRMSrcMem, (outs FR32:$dst), (ins i32mem:$src),
2329                       "movd\t{$src, $dst|$dst, $src}",
2330                       [(set FR32:$dst, (bitconvert (loadi32 addr:$src)))]>;
2331
2332 // SSE2 instructions with XS prefix
2333 def MOVQI2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
2334                     "movq\t{$src, $dst|$dst, $src}",
2335                     [(set VR128:$dst,
2336                       (v2i64 (scalar_to_vector (loadi64 addr:$src))))]>, XS,
2337                   Requires<[HasSSE2]>;
2338 def MOVPQI2QImr : PDI<0xD6, MRMDestMem, (outs), (ins i64mem:$dst, VR128:$src),
2339                       "movq\t{$src, $dst|$dst, $src}",
2340                       [(store (i64 (vector_extract (v2i64 VR128:$src),
2341                                     (iPTR 0))), addr:$dst)]>;
2342
2343 def : Pat<(f64 (vector_extract (v2f64 VR128:$src), (iPTR 0))),
2344           (f64 (EXTRACT_SUBREG (v2f64 VR128:$src), sub_sd))>;
2345
2346 def MOVPDI2DIrr  : PDI<0x7E, MRMDestReg, (outs GR32:$dst), (ins VR128:$src),
2347                        "movd\t{$src, $dst|$dst, $src}",
2348                        [(set GR32:$dst, (vector_extract (v4i32 VR128:$src),
2349                                         (iPTR 0)))]>;
2350 def MOVPDI2DImr  : PDI<0x7E, MRMDestMem, (outs), (ins i32mem:$dst, VR128:$src),
2351                        "movd\t{$src, $dst|$dst, $src}",
2352                        [(store (i32 (vector_extract (v4i32 VR128:$src),
2353                                      (iPTR 0))), addr:$dst)]>;
2354
2355 def MOVSS2DIrr  : PDI<0x7E, MRMDestReg, (outs GR32:$dst), (ins FR32:$src),
2356                       "movd\t{$src, $dst|$dst, $src}",
2357                       [(set GR32:$dst, (bitconvert FR32:$src))]>;
2358 def MOVSS2DImr  : PDI<0x7E, MRMDestMem, (outs), (ins i32mem:$dst, FR32:$src),
2359                       "movd\t{$src, $dst|$dst, $src}",
2360                       [(store (i32 (bitconvert FR32:$src)), addr:$dst)]>;
2361
2362 // Store / copy lower 64-bits of a XMM register.
2363 def MOVLQ128mr : PDI<0xD6, MRMDestMem, (outs), (ins i64mem:$dst, VR128:$src),
2364                      "movq\t{$src, $dst|$dst, $src}",
2365                      [(int_x86_sse2_storel_dq addr:$dst, VR128:$src)]>;
2366
2367 // movd / movq to XMM register zero-extends
2368 let AddedComplexity = 15 in {
2369 def MOVZDI2PDIrr : PDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR32:$src),
2370                        "movd\t{$src, $dst|$dst, $src}",
2371                        [(set VR128:$dst, (v4i32 (X86vzmovl
2372                                       (v4i32 (scalar_to_vector GR32:$src)))))]>;
2373 // This is X86-64 only.
2374 def MOVZQI2PQIrr : RPDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR64:$src),
2375                        "mov{d|q}\t{$src, $dst|$dst, $src}",
2376                        [(set VR128:$dst, (v2i64 (X86vzmovl
2377                                       (v2i64 (scalar_to_vector GR64:$src)))))]>;
2378 }
2379
2380 let AddedComplexity = 20 in {
2381 def MOVZDI2PDIrm : PDI<0x6E, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
2382                        "movd\t{$src, $dst|$dst, $src}",
2383                        [(set VR128:$dst,
2384                          (v4i32 (X86vzmovl (v4i32 (scalar_to_vector
2385                                                    (loadi32 addr:$src))))))]>;
2386
2387 def : Pat<(v4i32 (X86vzmovl (loadv4i32 addr:$src))),
2388             (MOVZDI2PDIrm addr:$src)>;
2389 def : Pat<(v4i32 (X86vzmovl (bc_v4i32 (loadv4f32 addr:$src)))),
2390             (MOVZDI2PDIrm addr:$src)>;
2391 def : Pat<(v4i32 (X86vzmovl (bc_v4i32 (loadv2i64 addr:$src)))),
2392             (MOVZDI2PDIrm addr:$src)>;
2393
2394 def MOVZQI2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
2395                      "movq\t{$src, $dst|$dst, $src}",
2396                      [(set VR128:$dst,
2397                        (v2i64 (X86vzmovl (v2i64 (scalar_to_vector
2398                                                  (loadi64 addr:$src))))))]>, XS,
2399                    Requires<[HasSSE2]>;
2400
2401 def : Pat<(v2i64 (X86vzmovl (loadv2i64 addr:$src))),
2402             (MOVZQI2PQIrm addr:$src)>;
2403 def : Pat<(v2i64 (X86vzmovl (bc_v2i64 (loadv4f32 addr:$src)))),
2404             (MOVZQI2PQIrm addr:$src)>;
2405 def : Pat<(v2i64 (X86vzload addr:$src)), (MOVZQI2PQIrm addr:$src)>;
2406 }
2407
2408 // Moving from XMM to XMM and clear upper 64 bits. Note, there is a bug in
2409 // IA32 document. movq xmm1, xmm2 does clear the high bits.
2410 let AddedComplexity = 15 in
2411 def MOVZPQILo2PQIrr : I<0x7E, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2412                         "movq\t{$src, $dst|$dst, $src}",
2413                     [(set VR128:$dst, (v2i64 (X86vzmovl (v2i64 VR128:$src))))]>,
2414                       XS, Requires<[HasSSE2]>;
2415
2416 let AddedComplexity = 20 in {
2417 def MOVZPQILo2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
2418                         "movq\t{$src, $dst|$dst, $src}",
2419                     [(set VR128:$dst, (v2i64 (X86vzmovl
2420                                              (loadv2i64 addr:$src))))]>,
2421                       XS, Requires<[HasSSE2]>;
2422
2423 def : Pat<(v2i64 (X86vzmovl (bc_v2i64 (loadv4i32 addr:$src)))),
2424             (MOVZPQILo2PQIrm addr:$src)>;
2425 }
2426
2427 // Instructions for the disassembler
2428 // xr = XMM register
2429 // xm = mem64
2430
2431 def MOVQxrxr : I<0x7E, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2432                  "movq\t{$src, $dst|$dst, $src}", []>, XS;
2433
2434 //===---------------------------------------------------------------------===//
2435 // SSE3 Instructions
2436 //===---------------------------------------------------------------------===//
2437
2438 // Conversion Instructions
2439 def CVTPD2DQrm  : S3DI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
2440                        "cvtpd2dq\t{$src, $dst|$dst, $src}", []>;
2441 def CVTPD2DQrr  : S3DI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2442                        "cvtpd2dq\t{$src, $dst|$dst, $src}", []>;
2443 def CVTDQ2PDrm  : S3SI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
2444                        "cvtdq2pd\t{$src, $dst|$dst, $src}", []>;
2445 def CVTDQ2PDrr  : S3SI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2446                        "cvtdq2pd\t{$src, $dst|$dst, $src}", []>;
2447
2448 // Move Instructions
2449 def MOVSHDUPrr : S3SI<0x16, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2450                       "movshdup\t{$src, $dst|$dst, $src}",
2451                       [(set VR128:$dst, (v4f32 (movshdup
2452                                                 VR128:$src, (undef))))]>;
2453 def MOVSHDUPrm : S3SI<0x16, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
2454                       "movshdup\t{$src, $dst|$dst, $src}",
2455                       [(set VR128:$dst, (movshdup
2456                                          (memopv4f32 addr:$src), (undef)))]>;
2457
2458 def MOVSLDUPrr : S3SI<0x12, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2459                       "movsldup\t{$src, $dst|$dst, $src}",
2460                       [(set VR128:$dst, (v4f32 (movsldup
2461                                                 VR128:$src, (undef))))]>;
2462 def MOVSLDUPrm : S3SI<0x12, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
2463                       "movsldup\t{$src, $dst|$dst, $src}",
2464                       [(set VR128:$dst, (movsldup
2465                                          (memopv4f32 addr:$src), (undef)))]>;
2466
2467 def MOVDDUPrr  : S3DI<0x12, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2468                       "movddup\t{$src, $dst|$dst, $src}",
2469                       [(set VR128:$dst,(v2f64 (movddup VR128:$src, (undef))))]>;
2470 def MOVDDUPrm  : S3DI<0x12, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
2471                       "movddup\t{$src, $dst|$dst, $src}",
2472                     [(set VR128:$dst,
2473                       (v2f64 (movddup (scalar_to_vector (loadf64 addr:$src)),
2474                                       (undef))))]>;
2475
2476 def : Pat<(movddup (bc_v2f64 (v2i64 (scalar_to_vector (loadi64 addr:$src)))),
2477                    (undef)),
2478           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
2479
2480 let AddedComplexity = 5 in {
2481 def : Pat<(movddup (memopv2f64 addr:$src), (undef)),
2482           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
2483 def : Pat<(movddup (bc_v4f32 (memopv2f64 addr:$src)), (undef)),
2484           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
2485 def : Pat<(movddup (memopv2i64 addr:$src), (undef)),
2486           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
2487 def : Pat<(movddup (bc_v4i32 (memopv2i64 addr:$src)), (undef)),
2488           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
2489 }
2490
2491 // Arithmetic
2492 let Constraints = "$src1 = $dst" in {
2493   def ADDSUBPSrr : S3DI<0xD0, MRMSrcReg,
2494                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2495                         "addsubps\t{$src2, $dst|$dst, $src2}",
2496                         [(set VR128:$dst, (int_x86_sse3_addsub_ps VR128:$src1,
2497                                            VR128:$src2))]>;
2498   def ADDSUBPSrm : S3DI<0xD0, MRMSrcMem,
2499                         (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
2500                         "addsubps\t{$src2, $dst|$dst, $src2}",
2501                         [(set VR128:$dst, (int_x86_sse3_addsub_ps VR128:$src1,
2502                                            (memop addr:$src2)))]>;
2503   def ADDSUBPDrr : S3I<0xD0, MRMSrcReg,
2504                        (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2505                        "addsubpd\t{$src2, $dst|$dst, $src2}",
2506                        [(set VR128:$dst, (int_x86_sse3_addsub_pd VR128:$src1,
2507                                           VR128:$src2))]>;
2508   def ADDSUBPDrm : S3I<0xD0, MRMSrcMem,
2509                        (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
2510                        "addsubpd\t{$src2, $dst|$dst, $src2}",
2511                        [(set VR128:$dst, (int_x86_sse3_addsub_pd VR128:$src1,
2512                                           (memop addr:$src2)))]>;
2513 }
2514
2515 def LDDQUrm : S3DI<0xF0, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
2516                    "lddqu\t{$src, $dst|$dst, $src}",
2517                    [(set VR128:$dst, (int_x86_sse3_ldu_dq addr:$src))]>;
2518
2519 // Horizontal ops
2520 class S3D_Intrr<bits<8> o, string OpcodeStr, Intrinsic IntId>
2521   : S3DI<o, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2522          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2523          [(set VR128:$dst, (v4f32 (IntId VR128:$src1, VR128:$src2)))]>;
2524 class S3D_Intrm<bits<8> o, string OpcodeStr, Intrinsic IntId>
2525   : S3DI<o, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
2526          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2527          [(set VR128:$dst, (v4f32 (IntId VR128:$src1, (memop addr:$src2))))]>;
2528 class S3_Intrr<bits<8> o, string OpcodeStr, Intrinsic IntId>
2529   : S3I<o, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2530         !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2531         [(set VR128:$dst, (v2f64 (IntId VR128:$src1, VR128:$src2)))]>;
2532 class S3_Intrm<bits<8> o, string OpcodeStr, Intrinsic IntId>
2533   : S3I<o, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
2534         !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2535       [(set VR128:$dst, (v2f64 (IntId VR128:$src1, (memopv2f64 addr:$src2))))]>;
2536
2537 let Constraints = "$src1 = $dst" in {
2538   def HADDPSrr : S3D_Intrr<0x7C, "haddps", int_x86_sse3_hadd_ps>;
2539   def HADDPSrm : S3D_Intrm<0x7C, "haddps", int_x86_sse3_hadd_ps>;
2540   def HADDPDrr : S3_Intrr <0x7C, "haddpd", int_x86_sse3_hadd_pd>;
2541   def HADDPDrm : S3_Intrm <0x7C, "haddpd", int_x86_sse3_hadd_pd>;
2542   def HSUBPSrr : S3D_Intrr<0x7D, "hsubps", int_x86_sse3_hsub_ps>;
2543   def HSUBPSrm : S3D_Intrm<0x7D, "hsubps", int_x86_sse3_hsub_ps>;
2544   def HSUBPDrr : S3_Intrr <0x7D, "hsubpd", int_x86_sse3_hsub_pd>;
2545   def HSUBPDrm : S3_Intrm <0x7D, "hsubpd", int_x86_sse3_hsub_pd>;
2546 }
2547
2548 // Thread synchronization
2549 def MONITOR : I<0x01, MRM_C8, (outs), (ins), "monitor",
2550                 [(int_x86_sse3_monitor EAX, ECX, EDX)]>,TB, Requires<[HasSSE3]>;
2551 def MWAIT   : I<0x01, MRM_C9, (outs), (ins), "mwait",
2552                 [(int_x86_sse3_mwait ECX, EAX)]>, TB, Requires<[HasSSE3]>;
2553
2554 // vector_shuffle v1, <undef> <1, 1, 3, 3>
2555 let AddedComplexity = 15 in
2556 def : Pat<(v4i32 (movshdup VR128:$src, (undef))),
2557           (MOVSHDUPrr VR128:$src)>, Requires<[HasSSE3]>;
2558 let AddedComplexity = 20 in
2559 def : Pat<(v4i32 (movshdup (bc_v4i32 (memopv2i64 addr:$src)), (undef))),
2560           (MOVSHDUPrm addr:$src)>, Requires<[HasSSE3]>;
2561
2562 // vector_shuffle v1, <undef> <0, 0, 2, 2>
2563 let AddedComplexity = 15 in
2564   def : Pat<(v4i32 (movsldup VR128:$src, (undef))),
2565             (MOVSLDUPrr VR128:$src)>, Requires<[HasSSE3]>;
2566 let AddedComplexity = 20 in
2567   def : Pat<(v4i32 (movsldup (bc_v4i32 (memopv2i64 addr:$src)), (undef))),
2568             (MOVSLDUPrm addr:$src)>, Requires<[HasSSE3]>;
2569
2570 //===---------------------------------------------------------------------===//
2571 // SSSE3 Instructions
2572 //===---------------------------------------------------------------------===//
2573
2574 /// SS3I_unop_rm_int_8 - Simple SSSE3 unary operator whose type is v*i8.
2575 multiclass SS3I_unop_rm_int_8<bits<8> opc, string OpcodeStr,
2576                               Intrinsic IntId64, Intrinsic IntId128> {
2577   def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst), (ins VR64:$src),
2578                    !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2579                    [(set VR64:$dst, (IntId64 VR64:$src))]>;
2580
2581   def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst), (ins i64mem:$src),
2582                    !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2583                    [(set VR64:$dst,
2584                      (IntId64 (bitconvert (memopv8i8 addr:$src))))]>;
2585
2586   def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2587                     (ins VR128:$src),
2588                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2589                     [(set VR128:$dst, (IntId128 VR128:$src))]>,
2590                     OpSize;
2591
2592   def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2593                     (ins i128mem:$src),
2594                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2595                     [(set VR128:$dst,
2596                       (IntId128
2597                        (bitconvert (memopv16i8 addr:$src))))]>, OpSize;
2598 }
2599
2600 /// SS3I_unop_rm_int_16 - Simple SSSE3 unary operator whose type is v*i16.
2601 multiclass SS3I_unop_rm_int_16<bits<8> opc, string OpcodeStr,
2602                                Intrinsic IntId64, Intrinsic IntId128> {
2603   def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2604                    (ins VR64:$src),
2605                    !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2606                    [(set VR64:$dst, (IntId64 VR64:$src))]>;
2607
2608   def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2609                    (ins i64mem:$src),
2610                    !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2611                    [(set VR64:$dst,
2612                      (IntId64
2613                       (bitconvert (memopv4i16 addr:$src))))]>;
2614
2615   def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2616                     (ins VR128:$src),
2617                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2618                     [(set VR128:$dst, (IntId128 VR128:$src))]>,
2619                     OpSize;
2620
2621   def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2622                     (ins i128mem:$src),
2623                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2624                     [(set VR128:$dst,
2625                       (IntId128
2626                        (bitconvert (memopv8i16 addr:$src))))]>, OpSize;
2627 }
2628
2629 /// SS3I_unop_rm_int_32 - Simple SSSE3 unary operator whose type is v*i32.
2630 multiclass SS3I_unop_rm_int_32<bits<8> opc, string OpcodeStr,
2631                                Intrinsic IntId64, Intrinsic IntId128> {
2632   def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2633                    (ins VR64:$src),
2634                    !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2635                    [(set VR64:$dst, (IntId64 VR64:$src))]>;
2636
2637   def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2638                    (ins i64mem:$src),
2639                    !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2640                    [(set VR64:$dst,
2641                      (IntId64
2642                       (bitconvert (memopv2i32 addr:$src))))]>;
2643
2644   def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2645                     (ins VR128:$src),
2646                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2647                     [(set VR128:$dst, (IntId128 VR128:$src))]>,
2648                     OpSize;
2649
2650   def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2651                     (ins i128mem:$src),
2652                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2653                     [(set VR128:$dst,
2654                       (IntId128
2655                        (bitconvert (memopv4i32 addr:$src))))]>, OpSize;
2656 }
2657
2658 defm PABSB       : SS3I_unop_rm_int_8 <0x1C, "pabsb",
2659                                        int_x86_ssse3_pabs_b,
2660                                        int_x86_ssse3_pabs_b_128>;
2661 defm PABSW       : SS3I_unop_rm_int_16<0x1D, "pabsw",
2662                                        int_x86_ssse3_pabs_w,
2663                                        int_x86_ssse3_pabs_w_128>;
2664 defm PABSD       : SS3I_unop_rm_int_32<0x1E, "pabsd",
2665                                        int_x86_ssse3_pabs_d,
2666                                        int_x86_ssse3_pabs_d_128>;
2667
2668 /// SS3I_binop_rm_int_8 - Simple SSSE3 binary operator whose type is v*i8.
2669 let Constraints = "$src1 = $dst" in {
2670   multiclass SS3I_binop_rm_int_8<bits<8> opc, string OpcodeStr,
2671                                  Intrinsic IntId64, Intrinsic IntId128,
2672                                  bit Commutable = 0> {
2673     def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2674                      (ins VR64:$src1, VR64:$src2),
2675                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2676                      [(set VR64:$dst, (IntId64 VR64:$src1, VR64:$src2))]> {
2677       let isCommutable = Commutable;
2678     }
2679     def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2680                      (ins VR64:$src1, i64mem:$src2),
2681                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2682                      [(set VR64:$dst,
2683                        (IntId64 VR64:$src1,
2684                         (bitconvert (memopv8i8 addr:$src2))))]>;
2685
2686     def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2687                       (ins VR128:$src1, VR128:$src2),
2688                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2689                       [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
2690                       OpSize {
2691       let isCommutable = Commutable;
2692     }
2693     def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2694                       (ins VR128:$src1, i128mem:$src2),
2695                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2696                       [(set VR128:$dst,
2697                         (IntId128 VR128:$src1,
2698                          (bitconvert (memopv16i8 addr:$src2))))]>, OpSize;
2699   }
2700 }
2701
2702 /// SS3I_binop_rm_int_16 - Simple SSSE3 binary operator whose type is v*i16.
2703 let Constraints = "$src1 = $dst" in {
2704   multiclass SS3I_binop_rm_int_16<bits<8> opc, string OpcodeStr,
2705                                   Intrinsic IntId64, Intrinsic IntId128,
2706                                   bit Commutable = 0> {
2707     def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2708                      (ins VR64:$src1, VR64:$src2),
2709                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2710                      [(set VR64:$dst, (IntId64 VR64:$src1, VR64:$src2))]> {
2711       let isCommutable = Commutable;
2712     }
2713     def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2714                      (ins VR64:$src1, i64mem:$src2),
2715                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2716                      [(set VR64:$dst,
2717                        (IntId64 VR64:$src1,
2718                         (bitconvert (memopv4i16 addr:$src2))))]>;
2719
2720     def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2721                       (ins VR128:$src1, VR128:$src2),
2722                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2723                       [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
2724                       OpSize {
2725       let isCommutable = Commutable;
2726     }
2727     def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2728                       (ins VR128:$src1, i128mem:$src2),
2729                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2730                       [(set VR128:$dst,
2731                         (IntId128 VR128:$src1,
2732                          (bitconvert (memopv8i16 addr:$src2))))]>, OpSize;
2733   }
2734 }
2735
2736 /// SS3I_binop_rm_int_32 - Simple SSSE3 binary operator whose type is v*i32.
2737 let Constraints = "$src1 = $dst" in {
2738   multiclass SS3I_binop_rm_int_32<bits<8> opc, string OpcodeStr,
2739                                   Intrinsic IntId64, Intrinsic IntId128,
2740                                   bit Commutable = 0> {
2741     def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2742                      (ins VR64:$src1, VR64:$src2),
2743                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2744                      [(set VR64:$dst, (IntId64 VR64:$src1, VR64:$src2))]> {
2745       let isCommutable = Commutable;
2746     }
2747     def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2748                      (ins VR64:$src1, i64mem:$src2),
2749                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2750                      [(set VR64:$dst,
2751                        (IntId64 VR64:$src1,
2752                         (bitconvert (memopv2i32 addr:$src2))))]>;
2753
2754     def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2755                       (ins VR128:$src1, VR128:$src2),
2756                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2757                       [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
2758                       OpSize {
2759       let isCommutable = Commutable;
2760     }
2761     def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2762                       (ins VR128:$src1, i128mem:$src2),
2763                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2764                       [(set VR128:$dst,
2765                         (IntId128 VR128:$src1,
2766                          (bitconvert (memopv4i32 addr:$src2))))]>, OpSize;
2767   }
2768 }
2769
2770 let ImmT = NoImm in {  // None of these have i8 immediate fields.
2771 defm PHADDW      : SS3I_binop_rm_int_16<0x01, "phaddw",
2772                                         int_x86_ssse3_phadd_w,
2773                                         int_x86_ssse3_phadd_w_128>;
2774 defm PHADDD      : SS3I_binop_rm_int_32<0x02, "phaddd",
2775                                         int_x86_ssse3_phadd_d,
2776                                         int_x86_ssse3_phadd_d_128>;
2777 defm PHADDSW     : SS3I_binop_rm_int_16<0x03, "phaddsw",
2778                                         int_x86_ssse3_phadd_sw,
2779                                         int_x86_ssse3_phadd_sw_128>;
2780 defm PHSUBW      : SS3I_binop_rm_int_16<0x05, "phsubw",
2781                                         int_x86_ssse3_phsub_w,
2782                                         int_x86_ssse3_phsub_w_128>;
2783 defm PHSUBD      : SS3I_binop_rm_int_32<0x06, "phsubd",
2784                                         int_x86_ssse3_phsub_d,
2785                                         int_x86_ssse3_phsub_d_128>;
2786 defm PHSUBSW     : SS3I_binop_rm_int_16<0x07, "phsubsw",
2787                                         int_x86_ssse3_phsub_sw,
2788                                         int_x86_ssse3_phsub_sw_128>;
2789 defm PMADDUBSW   : SS3I_binop_rm_int_8 <0x04, "pmaddubsw",
2790                                         int_x86_ssse3_pmadd_ub_sw,
2791                                         int_x86_ssse3_pmadd_ub_sw_128>;
2792 defm PMULHRSW    : SS3I_binop_rm_int_16<0x0B, "pmulhrsw",
2793                                         int_x86_ssse3_pmul_hr_sw,
2794                                         int_x86_ssse3_pmul_hr_sw_128, 1>;
2795
2796 defm PSHUFB      : SS3I_binop_rm_int_8 <0x00, "pshufb",
2797                                         int_x86_ssse3_pshuf_b,
2798                                         int_x86_ssse3_pshuf_b_128>;
2799 defm PSIGNB      : SS3I_binop_rm_int_8 <0x08, "psignb",
2800                                         int_x86_ssse3_psign_b,
2801                                         int_x86_ssse3_psign_b_128>;
2802 defm PSIGNW      : SS3I_binop_rm_int_16<0x09, "psignw",
2803                                         int_x86_ssse3_psign_w,
2804                                         int_x86_ssse3_psign_w_128>;
2805 defm PSIGND      : SS3I_binop_rm_int_32<0x0A, "psignd",
2806                                         int_x86_ssse3_psign_d,
2807                                         int_x86_ssse3_psign_d_128>;
2808 }
2809
2810 // palignr patterns.
2811 let Constraints = "$src1 = $dst" in {
2812   def PALIGNR64rr  : SS3AI<0x0F, MRMSrcReg, (outs VR64:$dst),
2813                            (ins VR64:$src1, VR64:$src2, i8imm:$src3),
2814                            "palignr\t{$src3, $src2, $dst|$dst, $src2, $src3}",
2815                            []>;
2816   def PALIGNR64rm  : SS3AI<0x0F, MRMSrcMem, (outs VR64:$dst),
2817                            (ins VR64:$src1, i64mem:$src2, i8imm:$src3),
2818                            "palignr\t{$src3, $src2, $dst|$dst, $src2, $src3}",
2819                            []>;
2820
2821   def PALIGNR128rr : SS3AI<0x0F, MRMSrcReg, (outs VR128:$dst),
2822                            (ins VR128:$src1, VR128:$src2, i8imm:$src3),
2823                            "palignr\t{$src3, $src2, $dst|$dst, $src2, $src3}",
2824                            []>, OpSize;
2825   def PALIGNR128rm : SS3AI<0x0F, MRMSrcMem, (outs VR128:$dst),
2826                            (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
2827                            "palignr\t{$src3, $src2, $dst|$dst, $src2, $src3}",
2828                            []>, OpSize;
2829 }
2830
2831 let AddedComplexity = 5 in {
2832
2833 def : Pat<(v1i64 (palign:$src3 VR64:$src1, VR64:$src2)),
2834           (PALIGNR64rr VR64:$src2, VR64:$src1,
2835                        (SHUFFLE_get_palign_imm VR64:$src3))>,
2836           Requires<[HasSSSE3]>;
2837 def : Pat<(v2i32 (palign:$src3 VR64:$src1, VR64:$src2)),
2838           (PALIGNR64rr VR64:$src2, VR64:$src1,
2839                        (SHUFFLE_get_palign_imm VR64:$src3))>,
2840           Requires<[HasSSSE3]>;
2841 def : Pat<(v2f32 (palign:$src3 VR64:$src1, VR64:$src2)),
2842           (PALIGNR64rr VR64:$src2, VR64:$src1,
2843                        (SHUFFLE_get_palign_imm VR64:$src3))>,
2844           Requires<[HasSSSE3]>;
2845 def : Pat<(v4i16 (palign:$src3 VR64:$src1, VR64:$src2)),
2846           (PALIGNR64rr VR64:$src2, VR64:$src1,
2847                        (SHUFFLE_get_palign_imm VR64:$src3))>,
2848           Requires<[HasSSSE3]>;
2849 def : Pat<(v8i8 (palign:$src3 VR64:$src1, VR64:$src2)),
2850           (PALIGNR64rr VR64:$src2, VR64:$src1,
2851                        (SHUFFLE_get_palign_imm VR64:$src3))>,
2852           Requires<[HasSSSE3]>;
2853
2854 def : Pat<(v4i32 (palign:$src3 VR128:$src1, VR128:$src2)),
2855           (PALIGNR128rr VR128:$src2, VR128:$src1,
2856                         (SHUFFLE_get_palign_imm VR128:$src3))>,
2857       Requires<[HasSSSE3]>;
2858 def : Pat<(v4f32 (palign:$src3 VR128:$src1, VR128:$src2)),
2859           (PALIGNR128rr VR128:$src2, VR128:$src1,
2860                         (SHUFFLE_get_palign_imm VR128:$src3))>,
2861       Requires<[HasSSSE3]>;
2862 def : Pat<(v8i16 (palign:$src3 VR128:$src1, VR128:$src2)),
2863           (PALIGNR128rr VR128:$src2, VR128:$src1,
2864                         (SHUFFLE_get_palign_imm VR128:$src3))>,
2865       Requires<[HasSSSE3]>;
2866 def : Pat<(v16i8 (palign:$src3 VR128:$src1, VR128:$src2)),
2867           (PALIGNR128rr VR128:$src2, VR128:$src1,
2868                         (SHUFFLE_get_palign_imm VR128:$src3))>,
2869       Requires<[HasSSSE3]>;
2870 }
2871
2872 def : Pat<(X86pshufb VR128:$src, VR128:$mask),
2873           (PSHUFBrr128 VR128:$src, VR128:$mask)>, Requires<[HasSSSE3]>;
2874 def : Pat<(X86pshufb VR128:$src, (bc_v16i8 (memopv2i64 addr:$mask))),
2875           (PSHUFBrm128 VR128:$src, addr:$mask)>, Requires<[HasSSSE3]>;
2876
2877 //===---------------------------------------------------------------------===//
2878 // Non-Instruction Patterns
2879 //===---------------------------------------------------------------------===//
2880
2881 // extload f32 -> f64.  This matches load+fextend because we have a hack in
2882 // the isel (PreprocessForFPConvert) that can introduce loads after dag
2883 // combine.
2884 // Since these loads aren't folded into the fextend, we have to match it
2885 // explicitly here.
2886 let Predicates = [HasSSE2] in
2887  def : Pat<(fextend (loadf32 addr:$src)),
2888            (CVTSS2SDrm addr:$src)>;
2889
2890 // bit_convert
2891 let Predicates = [HasSSE2] in {
2892   def : Pat<(v2i64 (bitconvert (v4i32 VR128:$src))), (v2i64 VR128:$src)>;
2893   def : Pat<(v2i64 (bitconvert (v8i16 VR128:$src))), (v2i64 VR128:$src)>;
2894   def : Pat<(v2i64 (bitconvert (v16i8 VR128:$src))), (v2i64 VR128:$src)>;
2895   def : Pat<(v2i64 (bitconvert (v2f64 VR128:$src))), (v2i64 VR128:$src)>;
2896   def : Pat<(v2i64 (bitconvert (v4f32 VR128:$src))), (v2i64 VR128:$src)>;
2897   def : Pat<(v4i32 (bitconvert (v2i64 VR128:$src))), (v4i32 VR128:$src)>;
2898   def : Pat<(v4i32 (bitconvert (v8i16 VR128:$src))), (v4i32 VR128:$src)>;
2899   def : Pat<(v4i32 (bitconvert (v16i8 VR128:$src))), (v4i32 VR128:$src)>;
2900   def : Pat<(v4i32 (bitconvert (v2f64 VR128:$src))), (v4i32 VR128:$src)>;
2901   def : Pat<(v4i32 (bitconvert (v4f32 VR128:$src))), (v4i32 VR128:$src)>;
2902   def : Pat<(v8i16 (bitconvert (v2i64 VR128:$src))), (v8i16 VR128:$src)>;
2903   def : Pat<(v8i16 (bitconvert (v4i32 VR128:$src))), (v8i16 VR128:$src)>;
2904   def : Pat<(v8i16 (bitconvert (v16i8 VR128:$src))), (v8i16 VR128:$src)>;
2905   def : Pat<(v8i16 (bitconvert (v2f64 VR128:$src))), (v8i16 VR128:$src)>;
2906   def : Pat<(v8i16 (bitconvert (v4f32 VR128:$src))), (v8i16 VR128:$src)>;
2907   def : Pat<(v16i8 (bitconvert (v2i64 VR128:$src))), (v16i8 VR128:$src)>;
2908   def : Pat<(v16i8 (bitconvert (v4i32 VR128:$src))), (v16i8 VR128:$src)>;
2909   def : Pat<(v16i8 (bitconvert (v8i16 VR128:$src))), (v16i8 VR128:$src)>;
2910   def : Pat<(v16i8 (bitconvert (v2f64 VR128:$src))), (v16i8 VR128:$src)>;
2911   def : Pat<(v16i8 (bitconvert (v4f32 VR128:$src))), (v16i8 VR128:$src)>;
2912   def : Pat<(v4f32 (bitconvert (v2i64 VR128:$src))), (v4f32 VR128:$src)>;
2913   def : Pat<(v4f32 (bitconvert (v4i32 VR128:$src))), (v4f32 VR128:$src)>;
2914   def : Pat<(v4f32 (bitconvert (v8i16 VR128:$src))), (v4f32 VR128:$src)>;
2915   def : Pat<(v4f32 (bitconvert (v16i8 VR128:$src))), (v4f32 VR128:$src)>;
2916   def : Pat<(v4f32 (bitconvert (v2f64 VR128:$src))), (v4f32 VR128:$src)>;
2917   def : Pat<(v2f64 (bitconvert (v2i64 VR128:$src))), (v2f64 VR128:$src)>;
2918   def : Pat<(v2f64 (bitconvert (v4i32 VR128:$src))), (v2f64 VR128:$src)>;
2919   def : Pat<(v2f64 (bitconvert (v8i16 VR128:$src))), (v2f64 VR128:$src)>;
2920   def : Pat<(v2f64 (bitconvert (v16i8 VR128:$src))), (v2f64 VR128:$src)>;
2921   def : Pat<(v2f64 (bitconvert (v4f32 VR128:$src))), (v2f64 VR128:$src)>;
2922 }
2923
2924 // Move scalar to XMM zero-extended
2925 // movd to XMM register zero-extends
2926 let AddedComplexity = 15 in {
2927 // Zeroing a VR128 then do a MOVS{S|D} to the lower bits.
2928 def : Pat<(v2f64 (X86vzmovl (v2f64 (scalar_to_vector FR64:$src)))),
2929           (MOVSDrr (v2f64 (V_SET0PS)), FR64:$src)>;
2930 def : Pat<(v4f32 (X86vzmovl (v4f32 (scalar_to_vector FR32:$src)))),
2931           (MOVSSrr (v4f32 (V_SET0PS)), FR32:$src)>;
2932 def : Pat<(v4f32 (X86vzmovl (v4f32 VR128:$src))),
2933           (MOVSSrr (v4f32 (V_SET0PS)),
2934                    (f32 (EXTRACT_SUBREG (v4f32 VR128:$src), sub_ss)))>;
2935 def : Pat<(v4i32 (X86vzmovl (v4i32 VR128:$src))),
2936           (MOVSSrr (v4i32 (V_SET0PI)),
2937                    (EXTRACT_SUBREG (v4i32 VR128:$src), sub_ss))>;
2938 }
2939
2940 // Splat v2f64 / v2i64
2941 let AddedComplexity = 10 in {
2942 def : Pat<(splat_lo (v2f64 VR128:$src), (undef)),
2943           (UNPCKLPDrr VR128:$src, VR128:$src)>,   Requires<[HasSSE2]>;
2944 def : Pat<(unpckh (v2f64 VR128:$src), (undef)),
2945           (UNPCKHPDrr VR128:$src, VR128:$src)>,   Requires<[HasSSE2]>;
2946 def : Pat<(splat_lo (v2i64 VR128:$src), (undef)),
2947           (PUNPCKLQDQrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2948 def : Pat<(unpckh (v2i64 VR128:$src), (undef)),
2949           (PUNPCKHQDQrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2950 }
2951
2952 // Special unary SHUFPSrri case.
2953 def : Pat<(v4f32 (pshufd:$src3 VR128:$src1, (undef))),
2954           (SHUFPSrri VR128:$src1, VR128:$src1,
2955                      (SHUFFLE_get_shuf_imm VR128:$src3))>;
2956 let AddedComplexity = 5 in
2957 def : Pat<(v4f32 (pshufd:$src2 VR128:$src1, (undef))),
2958           (PSHUFDri VR128:$src1, (SHUFFLE_get_shuf_imm VR128:$src2))>,
2959       Requires<[HasSSE2]>;
2960 // Special unary SHUFPDrri case.
2961 def : Pat<(v2i64 (pshufd:$src3 VR128:$src1, (undef))),
2962           (SHUFPDrri VR128:$src1, VR128:$src1,
2963                      (SHUFFLE_get_shuf_imm VR128:$src3))>,
2964       Requires<[HasSSE2]>;
2965 // Special unary SHUFPDrri case.
2966 def : Pat<(v2f64 (pshufd:$src3 VR128:$src1, (undef))),
2967           (SHUFPDrri VR128:$src1, VR128:$src1,
2968                      (SHUFFLE_get_shuf_imm VR128:$src3))>,
2969       Requires<[HasSSE2]>;
2970 // Unary v4f32 shuffle with PSHUF* in order to fold a load.
2971 def : Pat<(pshufd:$src2 (bc_v4i32 (memopv4f32 addr:$src1)), (undef)),
2972           (PSHUFDmi addr:$src1, (SHUFFLE_get_shuf_imm VR128:$src2))>,
2973       Requires<[HasSSE2]>;
2974
2975 // Special binary v4i32 shuffle cases with SHUFPS.
2976 def : Pat<(v4i32 (shufp:$src3 VR128:$src1, (v4i32 VR128:$src2))),
2977           (SHUFPSrri VR128:$src1, VR128:$src2,
2978                      (SHUFFLE_get_shuf_imm VR128:$src3))>,
2979            Requires<[HasSSE2]>;
2980 def : Pat<(v4i32 (shufp:$src3 VR128:$src1, (bc_v4i32 (memopv2i64 addr:$src2)))),
2981           (SHUFPSrmi VR128:$src1, addr:$src2,
2982                     (SHUFFLE_get_shuf_imm VR128:$src3))>,
2983            Requires<[HasSSE2]>;
2984 // Special binary v2i64 shuffle cases using SHUFPDrri.
2985 def : Pat<(v2i64 (shufp:$src3 VR128:$src1, VR128:$src2)),
2986           (SHUFPDrri VR128:$src1, VR128:$src2,
2987                      (SHUFFLE_get_shuf_imm VR128:$src3))>,
2988           Requires<[HasSSE2]>;
2989
2990 // vector_shuffle v1, <undef>, <0, 0, 1, 1, ...>
2991 let AddedComplexity = 15 in {
2992 def : Pat<(v4i32 (unpckl_undef:$src2 VR128:$src, (undef))),
2993           (PSHUFDri VR128:$src, (SHUFFLE_get_shuf_imm VR128:$src2))>,
2994           Requires<[OptForSpeed, HasSSE2]>;
2995 def : Pat<(v4f32 (unpckl_undef:$src2 VR128:$src, (undef))),
2996           (PSHUFDri VR128:$src, (SHUFFLE_get_shuf_imm VR128:$src2))>,
2997           Requires<[OptForSpeed, HasSSE2]>;
2998 }
2999 let AddedComplexity = 10 in {
3000 def : Pat<(v4f32 (unpckl_undef VR128:$src, (undef))),
3001           (UNPCKLPSrr VR128:$src, VR128:$src)>;
3002 def : Pat<(v16i8 (unpckl_undef VR128:$src, (undef))),
3003           (PUNPCKLBWrr VR128:$src, VR128:$src)>;
3004 def : Pat<(v8i16 (unpckl_undef VR128:$src, (undef))),
3005           (PUNPCKLWDrr VR128:$src, VR128:$src)>;
3006 def : Pat<(v4i32 (unpckl_undef VR128:$src, (undef))),
3007           (PUNPCKLDQrr VR128:$src, VR128:$src)>;
3008 }
3009
3010 // vector_shuffle v1, <undef>, <2, 2, 3, 3, ...>
3011 let AddedComplexity = 15 in {
3012 def : Pat<(v4i32 (unpckh_undef:$src2 VR128:$src, (undef))),
3013           (PSHUFDri VR128:$src, (SHUFFLE_get_shuf_imm VR128:$src2))>,
3014           Requires<[OptForSpeed, HasSSE2]>;
3015 def : Pat<(v4f32 (unpckh_undef:$src2 VR128:$src, (undef))),
3016           (PSHUFDri VR128:$src, (SHUFFLE_get_shuf_imm VR128:$src2))>,
3017           Requires<[OptForSpeed, HasSSE2]>;
3018 }
3019 let AddedComplexity = 10 in {
3020 def : Pat<(v4f32 (unpckh_undef VR128:$src, (undef))),
3021           (UNPCKHPSrr VR128:$src, VR128:$src)>;
3022 def : Pat<(v16i8 (unpckh_undef VR128:$src, (undef))),
3023           (PUNPCKHBWrr VR128:$src, VR128:$src)>;
3024 def : Pat<(v8i16 (unpckh_undef VR128:$src, (undef))),
3025           (PUNPCKHWDrr VR128:$src, VR128:$src)>;
3026 def : Pat<(v4i32 (unpckh_undef VR128:$src, (undef))),
3027           (PUNPCKHDQrr VR128:$src, VR128:$src)>;
3028 }
3029
3030 let AddedComplexity = 20 in {
3031 // vector_shuffle v1, v2 <0, 1, 4, 5> using MOVLHPS
3032 def : Pat<(v4i32 (movlhps VR128:$src1, VR128:$src2)),
3033           (MOVLHPSrr VR128:$src1, VR128:$src2)>;
3034
3035 // vector_shuffle v1, v2 <6, 7, 2, 3> using MOVHLPS
3036 def : Pat<(v4i32 (movhlps VR128:$src1, VR128:$src2)),
3037           (MOVHLPSrr VR128:$src1, VR128:$src2)>;
3038
3039 // vector_shuffle v1, undef <2, ?, ?, ?> using MOVHLPS
3040 def : Pat<(v4f32 (movhlps_undef VR128:$src1, (undef))),
3041           (MOVHLPSrr VR128:$src1, VR128:$src1)>;
3042 def : Pat<(v4i32 (movhlps_undef VR128:$src1, (undef))),
3043           (MOVHLPSrr VR128:$src1, VR128:$src1)>;
3044 }
3045
3046 let AddedComplexity = 20 in {
3047 // vector_shuffle v1, (load v2) <4, 5, 2, 3> using MOVLPS
3048 def : Pat<(v4f32 (movlp VR128:$src1, (load addr:$src2))),
3049           (MOVLPSrm VR128:$src1, addr:$src2)>;
3050 def : Pat<(v2f64 (movlp VR128:$src1, (load addr:$src2))),
3051           (MOVLPDrm VR128:$src1, addr:$src2)>;
3052 def : Pat<(v4i32 (movlp VR128:$src1, (load addr:$src2))),
3053           (MOVLPSrm VR128:$src1, addr:$src2)>;
3054 def : Pat<(v2i64 (movlp VR128:$src1, (load addr:$src2))),
3055           (MOVLPDrm VR128:$src1, addr:$src2)>;
3056 }
3057
3058 // (store (vector_shuffle (load addr), v2, <4, 5, 2, 3>), addr) using MOVLPS
3059 def : Pat<(store (v4f32 (movlp (load addr:$src1), VR128:$src2)), addr:$src1),
3060           (MOVLPSmr addr:$src1, VR128:$src2)>;
3061 def : Pat<(store (v2f64 (movlp (load addr:$src1), VR128:$src2)), addr:$src1),
3062           (MOVLPDmr addr:$src1, VR128:$src2)>;
3063 def : Pat<(store (v4i32 (movlp (bc_v4i32 (loadv2i64 addr:$src1)), VR128:$src2)),
3064                  addr:$src1),
3065           (MOVLPSmr addr:$src1, VR128:$src2)>;
3066 def : Pat<(store (v2i64 (movlp (load addr:$src1), VR128:$src2)), addr:$src1),
3067           (MOVLPDmr addr:$src1, VR128:$src2)>;
3068
3069 let AddedComplexity = 15 in {
3070 // Setting the lowest element in the vector.
3071 def : Pat<(v4i32 (movl VR128:$src1, VR128:$src2)),
3072           (MOVSSrr (v4i32 VR128:$src1),
3073                    (EXTRACT_SUBREG (v4i32 VR128:$src2), sub_ss))>;
3074 def : Pat<(v2i64 (movl VR128:$src1, VR128:$src2)),
3075           (MOVSDrr (v2i64 VR128:$src1),
3076                    (EXTRACT_SUBREG (v2i64 VR128:$src2), sub_sd))>;
3077
3078 // vector_shuffle v1, v2 <4, 5, 2, 3> using movsd
3079 def : Pat<(v4f32 (movlp VR128:$src1, VR128:$src2)),
3080           (MOVSDrr VR128:$src1, (EXTRACT_SUBREG VR128:$src2, sub_sd))>,
3081       Requires<[HasSSE2]>;
3082 def : Pat<(v4i32 (movlp VR128:$src1, VR128:$src2)),
3083           (MOVSDrr VR128:$src1, (EXTRACT_SUBREG VR128:$src2, sub_sd))>,
3084       Requires<[HasSSE2]>;
3085 }
3086
3087 // vector_shuffle v1, v2 <4, 5, 2, 3> using SHUFPSrri (we prefer movsd, but
3088 // fall back to this for SSE1)
3089 def : Pat<(v4f32 (movlp:$src3 VR128:$src1, (v4f32 VR128:$src2))),
3090           (SHUFPSrri VR128:$src2, VR128:$src1,
3091                      (SHUFFLE_get_shuf_imm VR128:$src3))>;
3092
3093 // Set lowest element and zero upper elements.
3094 def : Pat<(v2f64 (X86vzmovl (v2f64 VR128:$src))),
3095           (MOVZPQILo2PQIrr VR128:$src)>, Requires<[HasSSE2]>;
3096
3097 // Some special case pandn patterns.
3098 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v4i32 immAllOnesV))),
3099                   VR128:$src2)),
3100           (PANDNrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
3101 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v8i16 immAllOnesV))),
3102                   VR128:$src2)),
3103           (PANDNrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
3104 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v16i8 immAllOnesV))),
3105                   VR128:$src2)),
3106           (PANDNrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
3107
3108 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v4i32 immAllOnesV))),
3109                   (memop addr:$src2))),
3110           (PANDNrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
3111 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v8i16 immAllOnesV))),
3112                   (memop addr:$src2))),
3113           (PANDNrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
3114 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v16i8 immAllOnesV))),
3115                   (memop addr:$src2))),
3116           (PANDNrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
3117
3118 // vector -> vector casts
3119 def : Pat<(v4f32 (sint_to_fp (v4i32 VR128:$src))),
3120           (Int_CVTDQ2PSrr VR128:$src)>, Requires<[HasSSE2]>;
3121 def : Pat<(v4i32 (fp_to_sint (v4f32 VR128:$src))),
3122           (Int_CVTTPS2DQrr VR128:$src)>, Requires<[HasSSE2]>;
3123 def : Pat<(v2f64 (sint_to_fp (v2i32 VR64:$src))),
3124           (Int_CVTPI2PDrr VR64:$src)>, Requires<[HasSSE2]>;
3125 def : Pat<(v2i32 (fp_to_sint (v2f64 VR128:$src))),
3126           (Int_CVTTPD2PIrr VR128:$src)>, Requires<[HasSSE2]>;
3127
3128 // Use movaps / movups for SSE integer load / store (one byte shorter).
3129 def : Pat<(alignedloadv4i32 addr:$src),
3130           (MOVAPSrm addr:$src)>;
3131 def : Pat<(loadv4i32 addr:$src),
3132           (MOVUPSrm addr:$src)>;
3133 def : Pat<(alignedloadv2i64 addr:$src),
3134           (MOVAPSrm addr:$src)>;
3135 def : Pat<(loadv2i64 addr:$src),
3136           (MOVUPSrm addr:$src)>;
3137
3138 def : Pat<(alignedstore (v2i64 VR128:$src), addr:$dst),
3139           (MOVAPSmr addr:$dst, VR128:$src)>;
3140 def : Pat<(alignedstore (v4i32 VR128:$src), addr:$dst),
3141           (MOVAPSmr addr:$dst, VR128:$src)>;
3142 def : Pat<(alignedstore (v8i16 VR128:$src), addr:$dst),
3143           (MOVAPSmr addr:$dst, VR128:$src)>;
3144 def : Pat<(alignedstore (v16i8 VR128:$src), addr:$dst),
3145           (MOVAPSmr addr:$dst, VR128:$src)>;
3146 def : Pat<(store (v2i64 VR128:$src), addr:$dst),
3147           (MOVUPSmr addr:$dst, VR128:$src)>;
3148 def : Pat<(store (v4i32 VR128:$src), addr:$dst),
3149           (MOVUPSmr addr:$dst, VR128:$src)>;
3150 def : Pat<(store (v8i16 VR128:$src), addr:$dst),
3151           (MOVUPSmr addr:$dst, VR128:$src)>;
3152 def : Pat<(store (v16i8 VR128:$src), addr:$dst),
3153           (MOVUPSmr addr:$dst, VR128:$src)>;
3154
3155 //===----------------------------------------------------------------------===//
3156 // SSE4.1 Instructions
3157 //===----------------------------------------------------------------------===//
3158
3159 multiclass sse41_fp_unop_rm<bits<8> opcps, bits<8> opcpd,
3160                             string OpcodeStr,
3161                             Intrinsic V4F32Int,
3162                             Intrinsic V2F64Int> {
3163   // Intrinsic operation, reg.
3164   // Vector intrinsic operation, reg
3165   def PSr_Int : SS4AIi8<opcps, MRMSrcReg,
3166                     (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
3167                     !strconcat(OpcodeStr,
3168                     "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3169                     [(set VR128:$dst, (V4F32Int VR128:$src1, imm:$src2))]>,
3170                     OpSize;
3171
3172   // Vector intrinsic operation, mem
3173   def PSm_Int : Ii8<opcps, MRMSrcMem,
3174                     (outs VR128:$dst), (ins f128mem:$src1, i32i8imm:$src2),
3175                     !strconcat(OpcodeStr,
3176                     "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3177                     [(set VR128:$dst,
3178                           (V4F32Int (memopv4f32 addr:$src1),imm:$src2))]>,
3179                     TA, OpSize,
3180                 Requires<[HasSSE41]>;
3181
3182   // Vector intrinsic operation, reg
3183   def PDr_Int : SS4AIi8<opcpd, MRMSrcReg,
3184                     (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
3185                     !strconcat(OpcodeStr,
3186                     "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3187                     [(set VR128:$dst, (V2F64Int VR128:$src1, imm:$src2))]>,
3188                     OpSize;
3189
3190   // Vector intrinsic operation, mem
3191   def PDm_Int : SS4AIi8<opcpd, MRMSrcMem,
3192                     (outs VR128:$dst), (ins f128mem:$src1, i32i8imm:$src2),
3193                     !strconcat(OpcodeStr,
3194                     "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3195                     [(set VR128:$dst,
3196                           (V2F64Int (memopv2f64 addr:$src1),imm:$src2))]>,
3197                     OpSize;
3198 }
3199
3200 let Constraints = "$src1 = $dst" in {
3201 multiclass sse41_fp_binop_rm<bits<8> opcss, bits<8> opcsd,
3202                             string OpcodeStr,
3203                             Intrinsic F32Int,
3204                             Intrinsic F64Int> {
3205   // Intrinsic operation, reg.
3206   def SSr_Int : SS4AIi8<opcss, MRMSrcReg,
3207                     (outs VR128:$dst),
3208                                  (ins VR128:$src1, VR128:$src2, i32i8imm:$src3),
3209                     !strconcat(OpcodeStr,
3210                     "ss\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3211                     [(set VR128:$dst,
3212                             (F32Int VR128:$src1, VR128:$src2, imm:$src3))]>,
3213                     OpSize;
3214
3215   // Intrinsic operation, mem.
3216   def SSm_Int : SS4AIi8<opcss, MRMSrcMem,
3217                     (outs VR128:$dst),
3218                                 (ins VR128:$src1, ssmem:$src2, i32i8imm:$src3),
3219                     !strconcat(OpcodeStr,
3220                     "ss\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3221                     [(set VR128:$dst,
3222                          (F32Int VR128:$src1, sse_load_f32:$src2, imm:$src3))]>,
3223                     OpSize;
3224
3225   // Intrinsic operation, reg.
3226   def SDr_Int : SS4AIi8<opcsd, MRMSrcReg,
3227                     (outs VR128:$dst),
3228                             (ins VR128:$src1, VR128:$src2, i32i8imm:$src3),
3229                     !strconcat(OpcodeStr,
3230                     "sd\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3231                     [(set VR128:$dst,
3232                             (F64Int VR128:$src1, VR128:$src2, imm:$src3))]>,
3233                     OpSize;
3234
3235   // Intrinsic operation, mem.
3236   def SDm_Int : SS4AIi8<opcsd, MRMSrcMem,
3237                     (outs VR128:$dst),
3238                             (ins VR128:$src1, sdmem:$src2, i32i8imm:$src3),
3239                     !strconcat(OpcodeStr,
3240                     "sd\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3241                     [(set VR128:$dst,
3242                         (F64Int VR128:$src1, sse_load_f64:$src2, imm:$src3))]>,
3243                     OpSize;
3244 }
3245 }
3246
3247 // FP round - roundss, roundps, roundsd, roundpd
3248 defm ROUND  : sse41_fp_unop_rm<0x08, 0x09, "round",
3249                                int_x86_sse41_round_ps, int_x86_sse41_round_pd>;
3250 defm ROUND  : sse41_fp_binop_rm<0x0A, 0x0B, "round",
3251                                int_x86_sse41_round_ss, int_x86_sse41_round_sd>;
3252
3253 // SS41I_unop_rm_int_v16 - SSE 4.1 unary operator whose type is v8i16.
3254 multiclass SS41I_unop_rm_int_v16<bits<8> opc, string OpcodeStr,
3255                                  Intrinsic IntId128> {
3256   def rr128 : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
3257                     (ins VR128:$src),
3258                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3259                     [(set VR128:$dst, (IntId128 VR128:$src))]>, OpSize;
3260   def rm128 : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
3261                      (ins i128mem:$src),
3262                      !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3263                      [(set VR128:$dst,
3264                        (IntId128
3265                        (bitconvert (memopv8i16 addr:$src))))]>, OpSize;
3266 }
3267
3268 defm PHMINPOSUW : SS41I_unop_rm_int_v16 <0x41, "phminposuw",
3269                                          int_x86_sse41_phminposuw>;
3270
3271 /// SS41I_binop_rm_int - Simple SSE 4.1 binary operator
3272 let Constraints = "$src1 = $dst" in {
3273   multiclass SS41I_binop_rm_int<bits<8> opc, string OpcodeStr,
3274                                 Intrinsic IntId128, bit Commutable = 0> {
3275     def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
3276                    (ins VR128:$src1, VR128:$src2),
3277                    !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3278                    [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
3279                    OpSize {
3280       let isCommutable = Commutable;
3281     }
3282     def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
3283                    (ins VR128:$src1, i128mem:$src2),
3284                    !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3285                    [(set VR128:$dst,
3286                      (IntId128 VR128:$src1,
3287                       (bitconvert (memopv16i8 addr:$src2))))]>, OpSize;
3288   }
3289 }
3290
3291 defm PCMPEQQ      : SS41I_binop_rm_int<0x29, "pcmpeqq",
3292                                        int_x86_sse41_pcmpeqq, 1>;
3293 defm PACKUSDW     : SS41I_binop_rm_int<0x2B, "packusdw",
3294                                        int_x86_sse41_packusdw, 0>;
3295 defm PMINSB       : SS41I_binop_rm_int<0x38, "pminsb",
3296                                        int_x86_sse41_pminsb, 1>;
3297 defm PMINSD       : SS41I_binop_rm_int<0x39, "pminsd",
3298                                        int_x86_sse41_pminsd, 1>;
3299 defm PMINUD       : SS41I_binop_rm_int<0x3B, "pminud",
3300                                        int_x86_sse41_pminud, 1>;
3301 defm PMINUW       : SS41I_binop_rm_int<0x3A, "pminuw",
3302                                        int_x86_sse41_pminuw, 1>;
3303 defm PMAXSB       : SS41I_binop_rm_int<0x3C, "pmaxsb",
3304                                        int_x86_sse41_pmaxsb, 1>;
3305 defm PMAXSD       : SS41I_binop_rm_int<0x3D, "pmaxsd",
3306                                        int_x86_sse41_pmaxsd, 1>;
3307 defm PMAXUD       : SS41I_binop_rm_int<0x3F, "pmaxud",
3308                                        int_x86_sse41_pmaxud, 1>;
3309 defm PMAXUW       : SS41I_binop_rm_int<0x3E, "pmaxuw",
3310                                        int_x86_sse41_pmaxuw, 1>;
3311
3312 defm PMULDQ       : SS41I_binop_rm_int<0x28, "pmuldq", int_x86_sse41_pmuldq, 1>;
3313
3314 def : Pat<(v2i64 (X86pcmpeqq VR128:$src1, VR128:$src2)),
3315           (PCMPEQQrr VR128:$src1, VR128:$src2)>;
3316 def : Pat<(v2i64 (X86pcmpeqq VR128:$src1, (memop addr:$src2))),
3317           (PCMPEQQrm VR128:$src1, addr:$src2)>;
3318
3319 /// SS41I_binop_rm_int - Simple SSE 4.1 binary operator
3320 let Constraints = "$src1 = $dst" in {
3321   multiclass SS41I_binop_patint<bits<8> opc, string OpcodeStr, ValueType OpVT,
3322                                 SDNode OpNode, Intrinsic IntId128,
3323                                 bit Commutable = 0> {
3324     def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
3325                    (ins VR128:$src1, VR128:$src2),
3326                    !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3327                    [(set VR128:$dst, (OpNode (OpVT VR128:$src1),
3328                                                    VR128:$src2))]>, OpSize {
3329       let isCommutable = Commutable;
3330     }
3331     def rr_int : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
3332                       (ins VR128:$src1, VR128:$src2),
3333                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3334                       [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
3335                       OpSize {
3336       let isCommutable = Commutable;
3337     }
3338     def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
3339                    (ins VR128:$src1, i128mem:$src2),
3340                    !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3341                    [(set VR128:$dst,
3342                      (OpVT (OpNode VR128:$src1, (memop addr:$src2))))]>, OpSize;
3343     def rm_int : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
3344                        (ins VR128:$src1, i128mem:$src2),
3345                        !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3346                        [(set VR128:$dst,
3347                         (IntId128 VR128:$src1, (memop addr:$src2)))]>,
3348                        OpSize;
3349   }
3350 }
3351
3352 /// SS48I_binop_rm - Simple SSE41 binary operator.
3353 let Constraints = "$src1 = $dst" in {
3354 multiclass SS48I_binop_rm<bits<8> opc, string OpcodeStr, SDNode OpNode,
3355                         ValueType OpVT, bit Commutable = 0> {
3356   def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
3357                                  (ins VR128:$src1, VR128:$src2),
3358                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3359                [(set VR128:$dst, (OpVT (OpNode VR128:$src1, VR128:$src2)))]>,
3360                OpSize {
3361     let isCommutable = Commutable;
3362   }
3363   def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
3364                                  (ins VR128:$src1, i128mem:$src2),
3365                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3366                [(set VR128:$dst, (OpNode VR128:$src1,
3367                                   (bc_v4i32 (memopv2i64 addr:$src2))))]>,
3368                OpSize;
3369 }
3370 }
3371
3372 defm PMULLD         : SS48I_binop_rm<0x40, "pmulld", mul, v4i32, 1>;
3373
3374 /// SS41I_binop_rmi_int - SSE 4.1 binary operator with 8-bit immediate
3375 let Constraints = "$src1 = $dst" in {
3376   multiclass SS41I_binop_rmi_int<bits<8> opc, string OpcodeStr,
3377                                  Intrinsic IntId128, bit Commutable = 0> {
3378     def rri : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
3379                     (ins VR128:$src1, VR128:$src2, i32i8imm:$src3),
3380                     !strconcat(OpcodeStr,
3381                      "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3382                     [(set VR128:$dst,
3383                       (IntId128 VR128:$src1, VR128:$src2, imm:$src3))]>,
3384                     OpSize {
3385       let isCommutable = Commutable;
3386     }
3387     def rmi : SS4AIi8<opc, MRMSrcMem, (outs VR128:$dst),
3388                     (ins VR128:$src1, i128mem:$src2, i32i8imm:$src3),
3389                     !strconcat(OpcodeStr,
3390                      "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3391                     [(set VR128:$dst,
3392                       (IntId128 VR128:$src1,
3393                        (bitconvert (memopv16i8 addr:$src2)), imm:$src3))]>,
3394                     OpSize;
3395   }
3396 }
3397
3398 defm BLENDPS      : SS41I_binop_rmi_int<0x0C, "blendps",
3399                                         int_x86_sse41_blendps, 0>;
3400 defm BLENDPD      : SS41I_binop_rmi_int<0x0D, "blendpd",
3401                                         int_x86_sse41_blendpd, 0>;
3402 defm PBLENDW      : SS41I_binop_rmi_int<0x0E, "pblendw",
3403                                         int_x86_sse41_pblendw, 0>;
3404 defm DPPS         : SS41I_binop_rmi_int<0x40, "dpps",
3405                                         int_x86_sse41_dpps, 1>;
3406 defm DPPD         : SS41I_binop_rmi_int<0x41, "dppd",
3407                                         int_x86_sse41_dppd, 1>;
3408 defm MPSADBW      : SS41I_binop_rmi_int<0x42, "mpsadbw",
3409                                         int_x86_sse41_mpsadbw, 0>;
3410
3411
3412 /// SS41I_ternary_int - SSE 4.1 ternary operator
3413 let Uses = [XMM0], Constraints = "$src1 = $dst" in {
3414   multiclass SS41I_ternary_int<bits<8> opc, string OpcodeStr, Intrinsic IntId> {
3415     def rr0 : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
3416                     (ins VR128:$src1, VR128:$src2),
3417                     !strconcat(OpcodeStr,
3418                      "\t{%xmm0, $src2, $dst|$dst, $src2, %xmm0}"),
3419                     [(set VR128:$dst, (IntId VR128:$src1, VR128:$src2, XMM0))]>,
3420                     OpSize;
3421
3422     def rm0 : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
3423                     (ins VR128:$src1, i128mem:$src2),
3424                     !strconcat(OpcodeStr,
3425                      "\t{%xmm0, $src2, $dst|$dst, $src2, %xmm0}"),
3426                     [(set VR128:$dst,
3427                       (IntId VR128:$src1,
3428                        (bitconvert (memopv16i8 addr:$src2)), XMM0))]>, OpSize;
3429   }
3430 }
3431
3432 defm BLENDVPD     : SS41I_ternary_int<0x15, "blendvpd", int_x86_sse41_blendvpd>;
3433 defm BLENDVPS     : SS41I_ternary_int<0x14, "blendvps", int_x86_sse41_blendvps>;
3434 defm PBLENDVB     : SS41I_ternary_int<0x10, "pblendvb", int_x86_sse41_pblendvb>;
3435
3436
3437 multiclass SS41I_binop_rm_int8<bits<8> opc, string OpcodeStr, Intrinsic IntId> {
3438   def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3439                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3440                  [(set VR128:$dst, (IntId VR128:$src))]>, OpSize;
3441
3442   def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
3443                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3444        [(set VR128:$dst,
3445          (IntId (bitconvert (v2i64 (scalar_to_vector (loadi64 addr:$src))))))]>,
3446        OpSize;
3447 }
3448
3449 defm PMOVSXBW   : SS41I_binop_rm_int8<0x20, "pmovsxbw", int_x86_sse41_pmovsxbw>;
3450 defm PMOVSXWD   : SS41I_binop_rm_int8<0x23, "pmovsxwd", int_x86_sse41_pmovsxwd>;
3451 defm PMOVSXDQ   : SS41I_binop_rm_int8<0x25, "pmovsxdq", int_x86_sse41_pmovsxdq>;
3452 defm PMOVZXBW   : SS41I_binop_rm_int8<0x30, "pmovzxbw", int_x86_sse41_pmovzxbw>;
3453 defm PMOVZXWD   : SS41I_binop_rm_int8<0x33, "pmovzxwd", int_x86_sse41_pmovzxwd>;
3454 defm PMOVZXDQ   : SS41I_binop_rm_int8<0x35, "pmovzxdq", int_x86_sse41_pmovzxdq>;
3455
3456 // Common patterns involving scalar load.
3457 def : Pat<(int_x86_sse41_pmovsxbw (vzmovl_v2i64 addr:$src)),
3458           (PMOVSXBWrm addr:$src)>, Requires<[HasSSE41]>;
3459 def : Pat<(int_x86_sse41_pmovsxbw (vzload_v2i64 addr:$src)),
3460           (PMOVSXBWrm addr:$src)>, Requires<[HasSSE41]>;
3461
3462 def : Pat<(int_x86_sse41_pmovsxwd (vzmovl_v2i64 addr:$src)),
3463           (PMOVSXWDrm addr:$src)>, Requires<[HasSSE41]>;
3464 def : Pat<(int_x86_sse41_pmovsxwd (vzload_v2i64 addr:$src)),
3465           (PMOVSXWDrm addr:$src)>, Requires<[HasSSE41]>;
3466
3467 def : Pat<(int_x86_sse41_pmovsxdq (vzmovl_v2i64 addr:$src)),
3468           (PMOVSXDQrm addr:$src)>, Requires<[HasSSE41]>;
3469 def : Pat<(int_x86_sse41_pmovsxdq (vzload_v2i64 addr:$src)),
3470           (PMOVSXDQrm addr:$src)>, Requires<[HasSSE41]>;
3471
3472 def : Pat<(int_x86_sse41_pmovzxbw (vzmovl_v2i64 addr:$src)),
3473           (PMOVZXBWrm addr:$src)>, Requires<[HasSSE41]>;
3474 def : Pat<(int_x86_sse41_pmovzxbw (vzload_v2i64 addr:$src)),
3475           (PMOVZXBWrm addr:$src)>, Requires<[HasSSE41]>;
3476
3477 def : Pat<(int_x86_sse41_pmovzxwd (vzmovl_v2i64 addr:$src)),
3478           (PMOVZXWDrm addr:$src)>, Requires<[HasSSE41]>;
3479 def : Pat<(int_x86_sse41_pmovzxwd (vzload_v2i64 addr:$src)),
3480           (PMOVZXWDrm addr:$src)>, Requires<[HasSSE41]>;
3481
3482 def : Pat<(int_x86_sse41_pmovzxdq (vzmovl_v2i64 addr:$src)),
3483           (PMOVZXDQrm addr:$src)>, Requires<[HasSSE41]>;
3484 def : Pat<(int_x86_sse41_pmovzxdq (vzload_v2i64 addr:$src)),
3485           (PMOVZXDQrm addr:$src)>, Requires<[HasSSE41]>;
3486
3487
3488 multiclass SS41I_binop_rm_int4<bits<8> opc, string OpcodeStr, Intrinsic IntId> {
3489   def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3490                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3491                  [(set VR128:$dst, (IntId VR128:$src))]>, OpSize;
3492
3493   def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
3494                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3495        [(set VR128:$dst,
3496          (IntId (bitconvert (v4i32 (scalar_to_vector (loadi32 addr:$src))))))]>,
3497           OpSize;
3498 }
3499
3500 defm PMOVSXBD   : SS41I_binop_rm_int4<0x21, "pmovsxbd", int_x86_sse41_pmovsxbd>;
3501 defm PMOVSXWQ   : SS41I_binop_rm_int4<0x24, "pmovsxwq", int_x86_sse41_pmovsxwq>;
3502 defm PMOVZXBD   : SS41I_binop_rm_int4<0x31, "pmovzxbd", int_x86_sse41_pmovzxbd>;
3503 defm PMOVZXWQ   : SS41I_binop_rm_int4<0x34, "pmovzxwq", int_x86_sse41_pmovzxwq>;
3504
3505 // Common patterns involving scalar load
3506 def : Pat<(int_x86_sse41_pmovsxbd (vzmovl_v4i32 addr:$src)),
3507           (PMOVSXBDrm addr:$src)>, Requires<[HasSSE41]>;
3508 def : Pat<(int_x86_sse41_pmovsxwq (vzmovl_v4i32 addr:$src)),
3509           (PMOVSXWQrm addr:$src)>, Requires<[HasSSE41]>;
3510
3511 def : Pat<(int_x86_sse41_pmovzxbd (vzmovl_v4i32 addr:$src)),
3512           (PMOVZXBDrm addr:$src)>, Requires<[HasSSE41]>;
3513 def : Pat<(int_x86_sse41_pmovzxwq (vzmovl_v4i32 addr:$src)),
3514           (PMOVZXWQrm addr:$src)>, Requires<[HasSSE41]>;
3515
3516
3517 multiclass SS41I_binop_rm_int2<bits<8> opc, string OpcodeStr, Intrinsic IntId> {
3518   def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3519                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3520                  [(set VR128:$dst, (IntId VR128:$src))]>, OpSize;
3521
3522   // Expecting a i16 load any extended to i32 value.
3523   def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst), (ins i16mem:$src),
3524                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3525                  [(set VR128:$dst, (IntId (bitconvert
3526                      (v4i32 (scalar_to_vector (loadi16_anyext addr:$src))))))]>,
3527                  OpSize;
3528 }
3529
3530 defm PMOVSXBQ   : SS41I_binop_rm_int2<0x22, "pmovsxbq", int_x86_sse41_pmovsxbq>;
3531 defm PMOVZXBQ   : SS41I_binop_rm_int2<0x32, "pmovzxbq", int_x86_sse41_pmovzxbq>;
3532
3533 // Common patterns involving scalar load
3534 def : Pat<(int_x86_sse41_pmovsxbq
3535             (bitconvert (v4i32 (X86vzmovl
3536                              (v4i32 (scalar_to_vector (loadi32 addr:$src))))))),
3537           (PMOVSXBQrm addr:$src)>, Requires<[HasSSE41]>;
3538
3539 def : Pat<(int_x86_sse41_pmovzxbq
3540             (bitconvert (v4i32 (X86vzmovl
3541                              (v4i32 (scalar_to_vector (loadi32 addr:$src))))))),
3542           (PMOVZXBQrm addr:$src)>, Requires<[HasSSE41]>;
3543
3544
3545 /// SS41I_binop_ext8 - SSE 4.1 extract 8 bits to 32 bit reg or 8 bit mem
3546 multiclass SS41I_extract8<bits<8> opc, string OpcodeStr> {
3547   def rr : SS4AIi8<opc, MRMDestReg, (outs GR32:$dst),
3548                  (ins VR128:$src1, i32i8imm:$src2),
3549                  !strconcat(OpcodeStr,
3550                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3551                  [(set GR32:$dst, (X86pextrb (v16i8 VR128:$src1), imm:$src2))]>,
3552                  OpSize;
3553   def mr : SS4AIi8<opc, MRMDestMem, (outs),
3554                  (ins i8mem:$dst, VR128:$src1, i32i8imm:$src2),
3555                  !strconcat(OpcodeStr,
3556                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3557                  []>, OpSize;
3558 // FIXME:
3559 // There's an AssertZext in the way of writing the store pattern
3560 // (store (i8 (trunc (X86pextrb (v16i8 VR128:$src1), imm:$src2))), addr:$dst)
3561 }
3562
3563 defm PEXTRB      : SS41I_extract8<0x14, "pextrb">;
3564
3565
3566 /// SS41I_extract16 - SSE 4.1 extract 16 bits to memory destination
3567 multiclass SS41I_extract16<bits<8> opc, string OpcodeStr> {
3568   def mr : SS4AIi8<opc, MRMDestMem, (outs),
3569                  (ins i16mem:$dst, VR128:$src1, i32i8imm:$src2),
3570                  !strconcat(OpcodeStr,
3571                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3572                  []>, OpSize;
3573 // FIXME:
3574 // There's an AssertZext in the way of writing the store pattern
3575 // (store (i16 (trunc (X86pextrw (v16i8 VR128:$src1), imm:$src2))), addr:$dst)
3576 }
3577
3578 defm PEXTRW      : SS41I_extract16<0x15, "pextrw">;
3579
3580
3581 /// SS41I_extract32 - SSE 4.1 extract 32 bits to int reg or memory destination
3582 multiclass SS41I_extract32<bits<8> opc, string OpcodeStr> {
3583   def rr : SS4AIi8<opc, MRMDestReg, (outs GR32:$dst),
3584                  (ins VR128:$src1, i32i8imm:$src2),
3585                  !strconcat(OpcodeStr,
3586                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3587                  [(set GR32:$dst,
3588                   (extractelt (v4i32 VR128:$src1), imm:$src2))]>, OpSize;
3589   def mr : SS4AIi8<opc, MRMDestMem, (outs),
3590                  (ins i32mem:$dst, VR128:$src1, i32i8imm:$src2),
3591                  !strconcat(OpcodeStr,
3592                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3593                  [(store (extractelt (v4i32 VR128:$src1), imm:$src2),
3594                           addr:$dst)]>, OpSize;
3595 }
3596
3597 defm PEXTRD      : SS41I_extract32<0x16, "pextrd">;
3598
3599
3600 /// SS41I_extractf32 - SSE 4.1 extract 32 bits fp value to int reg or memory
3601 /// destination
3602 multiclass SS41I_extractf32<bits<8> opc, string OpcodeStr> {
3603   def rr : SS4AIi8<opc, MRMDestReg, (outs GR32:$dst),
3604                  (ins VR128:$src1, i32i8imm:$src2),
3605                  !strconcat(OpcodeStr,
3606                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3607                  [(set GR32:$dst,
3608                     (extractelt (bc_v4i32 (v4f32 VR128:$src1)), imm:$src2))]>,
3609            OpSize;
3610   def mr : SS4AIi8<opc, MRMDestMem, (outs),
3611                  (ins f32mem:$dst, VR128:$src1, i32i8imm:$src2),
3612                  !strconcat(OpcodeStr,
3613                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
3614                  [(store (extractelt (bc_v4i32 (v4f32 VR128:$src1)), imm:$src2),
3615                           addr:$dst)]>, OpSize;
3616 }
3617
3618 defm EXTRACTPS   : SS41I_extractf32<0x17, "extractps">;
3619
3620 // Also match an EXTRACTPS store when the store is done as f32 instead of i32.
3621 def : Pat<(store (f32 (bitconvert (extractelt (bc_v4i32 (v4f32 VR128:$src1)),
3622                                               imm:$src2))),
3623                  addr:$dst),
3624           (EXTRACTPSmr addr:$dst, VR128:$src1, imm:$src2)>,
3625          Requires<[HasSSE41]>;
3626
3627 let Constraints = "$src1 = $dst" in {
3628   multiclass SS41I_insert8<bits<8> opc, string OpcodeStr> {
3629     def rr : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
3630                    (ins VR128:$src1, GR32:$src2, i32i8imm:$src3),
3631                    !strconcat(OpcodeStr,
3632                     "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3633                    [(set VR128:$dst,
3634                      (X86pinsrb VR128:$src1, GR32:$src2, imm:$src3))]>, OpSize;
3635     def rm : SS4AIi8<opc, MRMSrcMem, (outs VR128:$dst),
3636                    (ins VR128:$src1, i8mem:$src2, i32i8imm:$src3),
3637                    !strconcat(OpcodeStr,
3638                     "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3639                    [(set VR128:$dst,
3640                      (X86pinsrb VR128:$src1, (extloadi8 addr:$src2),
3641                                 imm:$src3))]>, OpSize;
3642   }
3643 }
3644
3645 defm PINSRB      : SS41I_insert8<0x20, "pinsrb">;
3646
3647 let Constraints = "$src1 = $dst" in {
3648   multiclass SS41I_insert32<bits<8> opc, string OpcodeStr> {
3649     def rr : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
3650                    (ins VR128:$src1, GR32:$src2, i32i8imm:$src3),
3651                    !strconcat(OpcodeStr,
3652                     "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3653                    [(set VR128:$dst,
3654                      (v4i32 (insertelt VR128:$src1, GR32:$src2, imm:$src3)))]>,
3655                    OpSize;
3656     def rm : SS4AIi8<opc, MRMSrcMem, (outs VR128:$dst),
3657                    (ins VR128:$src1, i32mem:$src2, i32i8imm:$src3),
3658                    !strconcat(OpcodeStr,
3659                     "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3660                    [(set VR128:$dst,
3661                      (v4i32 (insertelt VR128:$src1, (loadi32 addr:$src2),
3662                                        imm:$src3)))]>, OpSize;
3663   }
3664 }
3665
3666 defm PINSRD      : SS41I_insert32<0x22, "pinsrd">;
3667
3668 // insertps has a few different modes, there's the first two here below which
3669 // are optimized inserts that won't zero arbitrary elements in the destination
3670 // vector. The next one matches the intrinsic and could zero arbitrary elements
3671 // in the target vector.
3672 let Constraints = "$src1 = $dst" in {
3673   multiclass SS41I_insertf32<bits<8> opc, string OpcodeStr> {
3674     def rr : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
3675                    (ins VR128:$src1, VR128:$src2, i32i8imm:$src3),
3676                    !strconcat(OpcodeStr,
3677                     "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3678                    [(set VR128:$dst,
3679                      (X86insrtps VR128:$src1, VR128:$src2, imm:$src3))]>,
3680       OpSize;
3681     def rm : SS4AIi8<opc, MRMSrcMem, (outs VR128:$dst),
3682                    (ins VR128:$src1, f32mem:$src2, i32i8imm:$src3),
3683                    !strconcat(OpcodeStr,
3684                     "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3685                    [(set VR128:$dst,
3686                      (X86insrtps VR128:$src1,
3687                                 (v4f32 (scalar_to_vector (loadf32 addr:$src2))),
3688                                  imm:$src3))]>, OpSize;
3689   }
3690 }
3691
3692 defm INSERTPS    : SS41I_insertf32<0x21, "insertps">;
3693
3694 def : Pat<(int_x86_sse41_insertps VR128:$src1, VR128:$src2, imm:$src3),
3695           (INSERTPSrr VR128:$src1, VR128:$src2, imm:$src3)>;
3696
3697 // ptest instruction we'll lower to this in X86ISelLowering primarily from
3698 // the intel intrinsic that corresponds to this.
3699 let Defs = [EFLAGS] in {
3700 def PTESTrr : SS48I<0x17, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
3701                     "ptest \t{$src2, $src1|$src1, $src2}",
3702                     [(set EFLAGS, (X86ptest VR128:$src1, VR128:$src2))]>,
3703               OpSize;
3704 def PTESTrm : SS48I<0x17, MRMSrcMem, (outs), (ins VR128:$src1, i128mem:$src2),
3705                     "ptest \t{$src2, $src1|$src1, $src2}",
3706                     [(set EFLAGS, (X86ptest VR128:$src1, (load addr:$src2)))]>,
3707               OpSize;
3708 }
3709
3710 def MOVNTDQArm : SS48I<0x2A, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
3711                        "movntdqa\t{$src, $dst|$dst, $src}",
3712                        [(set VR128:$dst, (int_x86_sse41_movntdqa addr:$src))]>,
3713                        OpSize;
3714
3715
3716 //===----------------------------------------------------------------------===//
3717 // SSE4.2 Instructions
3718 //===----------------------------------------------------------------------===//
3719
3720 /// SS42I_binop_rm_int - Simple SSE 4.2 binary operator
3721 let Constraints = "$src1 = $dst" in {
3722   multiclass SS42I_binop_rm_int<bits<8> opc, string OpcodeStr,
3723                                 Intrinsic IntId128, bit Commutable = 0> {
3724     def rr : SS428I<opc, MRMSrcReg, (outs VR128:$dst),
3725                    (ins VR128:$src1, VR128:$src2),
3726                    !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3727                    [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
3728                    OpSize {
3729       let isCommutable = Commutable;
3730     }
3731     def rm : SS428I<opc, MRMSrcMem, (outs VR128:$dst),
3732                    (ins VR128:$src1, i128mem:$src2),
3733                    !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3734                    [(set VR128:$dst,
3735                      (IntId128 VR128:$src1,
3736                       (bitconvert (memopv16i8 addr:$src2))))]>, OpSize;
3737   }
3738 }
3739
3740 defm PCMPGTQ      : SS42I_binop_rm_int<0x37, "pcmpgtq", int_x86_sse42_pcmpgtq>;
3741
3742 def : Pat<(v2i64 (X86pcmpgtq VR128:$src1, VR128:$src2)),
3743           (PCMPGTQrr VR128:$src1, VR128:$src2)>;
3744 def : Pat<(v2i64 (X86pcmpgtq VR128:$src1, (memop addr:$src2))),
3745           (PCMPGTQrm VR128:$src1, addr:$src2)>;
3746
3747 // crc intrinsic instruction
3748 // This set of instructions are only rm, the only difference is the size
3749 // of r and m.
3750 let Constraints = "$src1 = $dst" in {
3751   def CRC32m8  : SS42FI<0xF0, MRMSrcMem, (outs GR32:$dst),
3752                       (ins GR32:$src1, i8mem:$src2),
3753                       "crc32{b} \t{$src2, $src1|$src1, $src2}",
3754                        [(set GR32:$dst,
3755                          (int_x86_sse42_crc32_8 GR32:$src1,
3756                          (load addr:$src2)))]>;
3757   def CRC32r8  : SS42FI<0xF0, MRMSrcReg, (outs GR32:$dst),
3758                       (ins GR32:$src1, GR8:$src2),
3759                       "crc32{b} \t{$src2, $src1|$src1, $src2}",
3760                        [(set GR32:$dst,
3761                          (int_x86_sse42_crc32_8 GR32:$src1, GR8:$src2))]>;
3762   def CRC32m16  : SS42FI<0xF1, MRMSrcMem, (outs GR32:$dst),
3763                       (ins GR32:$src1, i16mem:$src2),
3764                       "crc32{w} \t{$src2, $src1|$src1, $src2}",
3765                        [(set GR32:$dst,
3766                          (int_x86_sse42_crc32_16 GR32:$src1,
3767                          (load addr:$src2)))]>,
3768                          OpSize;
3769   def CRC32r16  : SS42FI<0xF1, MRMSrcReg, (outs GR32:$dst),
3770                       (ins GR32:$src1, GR16:$src2),
3771                       "crc32{w} \t{$src2, $src1|$src1, $src2}",
3772                        [(set GR32:$dst,
3773                          (int_x86_sse42_crc32_16 GR32:$src1, GR16:$src2))]>,
3774                          OpSize;
3775   def CRC32m32  : SS42FI<0xF1, MRMSrcMem, (outs GR32:$dst),
3776                       (ins GR32:$src1, i32mem:$src2),
3777                       "crc32{l} \t{$src2, $src1|$src1, $src2}",
3778                        [(set GR32:$dst,
3779                          (int_x86_sse42_crc32_32 GR32:$src1,
3780                          (load addr:$src2)))]>;
3781   def CRC32r32  : SS42FI<0xF1, MRMSrcReg, (outs GR32:$dst),
3782                       (ins GR32:$src1, GR32:$src2),
3783                       "crc32{l} \t{$src2, $src1|$src1, $src2}",
3784                        [(set GR32:$dst,
3785                          (int_x86_sse42_crc32_32 GR32:$src1, GR32:$src2))]>;
3786   def CRC64m8  : SS42FI<0xF0, MRMSrcMem, (outs GR64:$dst),
3787                       (ins GR64:$src1, i8mem:$src2),
3788                       "crc32{b} \t{$src2, $src1|$src1, $src2}",
3789                        [(set GR64:$dst,
3790                          (int_x86_sse42_crc64_8 GR64:$src1,
3791                          (load addr:$src2)))]>,
3792                          REX_W;
3793   def CRC64r8  : SS42FI<0xF0, MRMSrcReg, (outs GR64:$dst),
3794                       (ins GR64:$src1, GR8:$src2),
3795                       "crc32{b} \t{$src2, $src1|$src1, $src2}",
3796                        [(set GR64:$dst,
3797                          (int_x86_sse42_crc64_8 GR64:$src1, GR8:$src2))]>,
3798                          REX_W;
3799   def CRC64m64  : SS42FI<0xF1, MRMSrcMem, (outs GR64:$dst),
3800                       (ins GR64:$src1, i64mem:$src2),
3801                       "crc32{q} \t{$src2, $src1|$src1, $src2}",
3802                        [(set GR64:$dst,
3803                          (int_x86_sse42_crc64_64 GR64:$src1,
3804                          (load addr:$src2)))]>,
3805                          REX_W;
3806   def CRC64r64  : SS42FI<0xF1, MRMSrcReg, (outs GR64:$dst),
3807                       (ins GR64:$src1, GR64:$src2),
3808                       "crc32{q} \t{$src2, $src1|$src1, $src2}",
3809                        [(set GR64:$dst,
3810                          (int_x86_sse42_crc64_64 GR64:$src1, GR64:$src2))]>,
3811                          REX_W;
3812 }
3813
3814 // String/text processing instructions.
3815 let Defs = [EFLAGS], usesCustomInserter = 1 in {
3816 def PCMPISTRM128REG : SS42AI<0, Pseudo, (outs VR128:$dst),
3817   (ins VR128:$src1, VR128:$src2, i8imm:$src3),
3818   "#PCMPISTRM128rr PSEUDO!",
3819   [(set VR128:$dst, (int_x86_sse42_pcmpistrm128 VR128:$src1, VR128:$src2,
3820                                                 imm:$src3))]>, OpSize;
3821 def PCMPISTRM128MEM : SS42AI<0, Pseudo, (outs VR128:$dst),
3822   (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
3823   "#PCMPISTRM128rm PSEUDO!",
3824   [(set VR128:$dst, (int_x86_sse42_pcmpistrm128 VR128:$src1, (load addr:$src2),
3825                                                 imm:$src3))]>, OpSize;
3826 }
3827
3828 let Defs = [XMM0, EFLAGS] in {
3829 def PCMPISTRM128rr : SS42AI<0x62, MRMSrcReg, (outs),
3830   (ins VR128:$src1, VR128:$src2, i8imm:$src3),
3831    "pcmpistrm\t{$src3, $src2, $src1|$src1, $src2, $src3}", []>, OpSize;
3832 def PCMPISTRM128rm : SS42AI<0x62, MRMSrcMem, (outs),
3833   (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
3834   "pcmpistrm\t{$src3, $src2, $src1|$src1, $src2, $src3}", []>, OpSize;
3835 }
3836
3837 let Defs = [EFLAGS], Uses = [EAX, EDX], usesCustomInserter = 1 in {
3838 def PCMPESTRM128REG : SS42AI<0, Pseudo, (outs VR128:$dst),
3839   (ins VR128:$src1, VR128:$src3, i8imm:$src5),
3840   "#PCMPESTRM128rr PSEUDO!",
3841   [(set VR128:$dst,
3842         (int_x86_sse42_pcmpestrm128
3843          VR128:$src1, EAX, VR128:$src3, EDX, imm:$src5))]>, OpSize;
3844
3845 def PCMPESTRM128MEM : SS42AI<0, Pseudo, (outs VR128:$dst),
3846   (ins VR128:$src1, i128mem:$src3, i8imm:$src5),
3847   "#PCMPESTRM128rm PSEUDO!",
3848   [(set VR128:$dst, (int_x86_sse42_pcmpestrm128
3849                      VR128:$src1, EAX, (load addr:$src3), EDX, imm:$src5))]>,
3850   OpSize;
3851 }
3852
3853 let Defs = [XMM0, EFLAGS], Uses = [EAX, EDX] in {
3854 def PCMPESTRM128rr : SS42AI<0x60, MRMSrcReg, (outs),
3855   (ins VR128:$src1, VR128:$src3, i8imm:$src5),
3856   "pcmpestrm\t{$src5, $src3, $src1|$src1, $src3, $src5}", []>, OpSize;
3857 def PCMPESTRM128rm : SS42AI<0x60, MRMSrcMem, (outs),
3858   (ins VR128:$src1, i128mem:$src3, i8imm:$src5),
3859   "pcmpestrm\t{$src5, $src3, $src1|$src1, $src3, $src5}", []>, OpSize;
3860 }
3861
3862 let Defs = [ECX, EFLAGS] in {
3863   multiclass SS42AI_pcmpistri<Intrinsic IntId128> {
3864     def rr : SS42AI<0x63, MRMSrcReg, (outs),
3865       (ins VR128:$src1, VR128:$src2, i8imm:$src3),
3866       "pcmpistri\t{$src3, $src2, $src1|$src1, $src2, $src3}",
3867       [(set ECX, (IntId128 VR128:$src1, VR128:$src2, imm:$src3)),
3868        (implicit EFLAGS)]>, OpSize;
3869     def rm : SS42AI<0x63, MRMSrcMem, (outs),
3870       (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
3871       "pcmpistri\t{$src3, $src2, $src1|$src1, $src2, $src3}",
3872       [(set ECX, (IntId128 VR128:$src1, (load addr:$src2), imm:$src3)),
3873        (implicit EFLAGS)]>, OpSize;
3874   }
3875 }
3876
3877 defm PCMPISTRI  : SS42AI_pcmpistri<int_x86_sse42_pcmpistri128>;
3878 defm PCMPISTRIA : SS42AI_pcmpistri<int_x86_sse42_pcmpistria128>;
3879 defm PCMPISTRIC : SS42AI_pcmpistri<int_x86_sse42_pcmpistric128>;
3880 defm PCMPISTRIO : SS42AI_pcmpistri<int_x86_sse42_pcmpistrio128>;
3881 defm PCMPISTRIS : SS42AI_pcmpistri<int_x86_sse42_pcmpistris128>;
3882 defm PCMPISTRIZ : SS42AI_pcmpistri<int_x86_sse42_pcmpistriz128>;
3883
3884 let Defs = [ECX, EFLAGS] in {
3885 let Uses = [EAX, EDX] in {
3886   multiclass SS42AI_pcmpestri<Intrinsic IntId128> {
3887     def rr : SS42AI<0x61, MRMSrcReg, (outs),
3888       (ins VR128:$src1, VR128:$src3, i8imm:$src5),
3889       "pcmpestri\t{$src5, $src3, $src1|$src1, $src3, $src5}",
3890       [(set ECX, (IntId128 VR128:$src1, EAX, VR128:$src3, EDX, imm:$src5)),
3891        (implicit EFLAGS)]>, OpSize;
3892     def rm : SS42AI<0x61, MRMSrcMem, (outs),
3893       (ins VR128:$src1, i128mem:$src3, i8imm:$src5),
3894        "pcmpestri\t{$src5, $src3, $src1|$src1, $src3, $src5}",
3895        [(set ECX,
3896              (IntId128 VR128:$src1, EAX, (load addr:$src3), EDX, imm:$src5)),
3897         (implicit EFLAGS)]>, OpSize;
3898   }
3899 }
3900 }
3901
3902 defm PCMPESTRI  : SS42AI_pcmpestri<int_x86_sse42_pcmpestri128>;
3903 defm PCMPESTRIA : SS42AI_pcmpestri<int_x86_sse42_pcmpestria128>;
3904 defm PCMPESTRIC : SS42AI_pcmpestri<int_x86_sse42_pcmpestric128>;
3905 defm PCMPESTRIO : SS42AI_pcmpestri<int_x86_sse42_pcmpestrio128>;
3906 defm PCMPESTRIS : SS42AI_pcmpestri<int_x86_sse42_pcmpestris128>;
3907 defm PCMPESTRIZ : SS42AI_pcmpestri<int_x86_sse42_pcmpestriz128>;
3908
3909 //===----------------------------------------------------------------------===//
3910 // AES-NI Instructions
3911 //===----------------------------------------------------------------------===//
3912
3913 let Constraints = "$src1 = $dst" in {
3914   multiclass AESI_binop_rm_int<bits<8> opc, string OpcodeStr,
3915                                 Intrinsic IntId128, bit Commutable = 0> {
3916     def rr : AES8I<opc, MRMSrcReg, (outs VR128:$dst),
3917                    (ins VR128:$src1, VR128:$src2),
3918                    !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3919                    [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
3920                    OpSize {
3921       let isCommutable = Commutable;
3922     }
3923     def rm : AES8I<opc, MRMSrcMem, (outs VR128:$dst),
3924                    (ins VR128:$src1, i128mem:$src2),
3925                    !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3926                    [(set VR128:$dst,
3927                      (IntId128 VR128:$src1,
3928                       (bitconvert (memopv16i8 addr:$src2))))]>, OpSize;
3929   }
3930 }
3931
3932 defm AESENC          : AESI_binop_rm_int<0xDC, "aesenc",
3933                        int_x86_aesni_aesenc>;
3934 defm AESENCLAST      : AESI_binop_rm_int<0xDD, "aesenclast",
3935                        int_x86_aesni_aesenclast>;
3936 defm AESDEC          : AESI_binop_rm_int<0xDE, "aesdec",
3937                        int_x86_aesni_aesdec>;
3938 defm AESDECLAST      : AESI_binop_rm_int<0xDF, "aesdeclast",
3939                        int_x86_aesni_aesdeclast>;
3940
3941 def : Pat<(v2i64 (int_x86_aesni_aesenc VR128:$src1, VR128:$src2)),
3942           (AESENCrr VR128:$src1, VR128:$src2)>;
3943 def : Pat<(v2i64 (int_x86_aesni_aesenc VR128:$src1, (memop addr:$src2))),
3944           (AESENCrm VR128:$src1, addr:$src2)>;
3945 def : Pat<(v2i64 (int_x86_aesni_aesenclast VR128:$src1, VR128:$src2)),
3946           (AESENCLASTrr VR128:$src1, VR128:$src2)>;
3947 def : Pat<(v2i64 (int_x86_aesni_aesenclast VR128:$src1, (memop addr:$src2))),
3948           (AESENCLASTrm VR128:$src1, addr:$src2)>;
3949 def : Pat<(v2i64 (int_x86_aesni_aesdec VR128:$src1, VR128:$src2)),
3950           (AESDECrr VR128:$src1, VR128:$src2)>;
3951 def : Pat<(v2i64 (int_x86_aesni_aesdec VR128:$src1, (memop addr:$src2))),
3952           (AESDECrm VR128:$src1, addr:$src2)>;
3953 def : Pat<(v2i64 (int_x86_aesni_aesdeclast VR128:$src1, VR128:$src2)),
3954           (AESDECLASTrr VR128:$src1, VR128:$src2)>;
3955 def : Pat<(v2i64 (int_x86_aesni_aesdeclast VR128:$src1, (memop addr:$src2))),
3956           (AESDECLASTrm VR128:$src1, addr:$src2)>;
3957
3958 def AESIMCrr : AES8I<0xDB, MRMSrcReg, (outs VR128:$dst),
3959   (ins VR128:$src1),
3960   "aesimc\t{$src1, $dst|$dst, $src1}",
3961   [(set VR128:$dst,
3962     (int_x86_aesni_aesimc VR128:$src1))]>,
3963   OpSize;
3964
3965 def AESIMCrm : AES8I<0xDB, MRMSrcMem, (outs VR128:$dst),
3966   (ins i128mem:$src1),
3967   "aesimc\t{$src1, $dst|$dst, $src1}",
3968   [(set VR128:$dst,
3969     (int_x86_aesni_aesimc (bitconvert (memopv2i64 addr:$src1))))]>,
3970   OpSize;
3971
3972 def AESKEYGENASSIST128rr : AESAI<0xDF, MRMSrcReg, (outs VR128:$dst),
3973   (ins VR128:$src1, i8imm:$src2),
3974   "aeskeygenassist\t{$src2, $src1, $dst|$dst, $src1, $src2}",
3975   [(set VR128:$dst,
3976     (int_x86_aesni_aeskeygenassist VR128:$src1, imm:$src2))]>,
3977   OpSize;
3978 def AESKEYGENASSIST128rm : AESAI<0xDF, MRMSrcMem, (outs VR128:$dst),
3979   (ins i128mem:$src1, i8imm:$src2),
3980   "aeskeygenassist\t{$src2, $src1, $dst|$dst, $src1, $src2}",
3981   [(set VR128:$dst,
3982     (int_x86_aesni_aeskeygenassist (bitconvert (memopv2i64 addr:$src1)),
3983                                     imm:$src2))]>,
3984   OpSize;