d4f35fa5b5e9a0753d65ced1d656cf482d3eb812
[oota-llvm.git] / lib / Target / X86 / X86InstrSSE.td
1 //====- X86InstrSSE.td - Describe the X86 Instruction Set -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Evan Cheng and is distributed under the University
6 // of Illinois Open Source 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
24 def X86fmin    : SDNode<"X86ISD::FMIN",      SDTFPBinOp>;
25 def X86fmax    : SDNode<"X86ISD::FMAX",      SDTFPBinOp>;
26 def X86fand    : SDNode<"X86ISD::FAND",      SDTFPBinOp,
27                         [SDNPCommutative, SDNPAssociative]>;
28 def X86for     : SDNode<"X86ISD::FOR",       SDTFPBinOp,
29                         [SDNPCommutative, SDNPAssociative]>;
30 def X86fxor    : SDNode<"X86ISD::FXOR",      SDTFPBinOp,
31                         [SDNPCommutative, SDNPAssociative]>;
32 def X86frsqrt  : SDNode<"X86ISD::FRSQRT",    SDTFPUnaryOp>;
33 def X86frcp    : SDNode<"X86ISD::FRCP",      SDTFPUnaryOp>;
34 def X86fsrl    : SDNode<"X86ISD::FSRL",      SDTX86FPShiftOp>;
35 def X86comi    : SDNode<"X86ISD::COMI",      SDTX86CmpTest,
36                         [SDNPHasChain, SDNPOutFlag]>;
37 def X86ucomi   : SDNode<"X86ISD::UCOMI",     SDTX86CmpTest,
38                         [SDNPHasChain, SDNPOutFlag]>;
39 def X86s2vec   : SDNode<"X86ISD::S2VEC",  SDTypeProfile<1, 1, []>, []>;
40 def X86pextrw  : SDNode<"X86ISD::PEXTRW", SDTypeProfile<1, 2, []>, []>;
41 def X86pinsrw  : SDNode<"X86ISD::PINSRW", SDTypeProfile<1, 3, []>, []>;
42
43 //===----------------------------------------------------------------------===//
44 // SSE 'Special' Instructions
45 //===----------------------------------------------------------------------===//
46
47 def IMPLICIT_DEF_VR128 : I<0, Pseudo, (outs VR128:$dst), (ins),
48                            "#IMPLICIT_DEF $dst",
49                            [(set VR128:$dst, (v4f32 (undef)))]>,
50                          Requires<[HasSSE1]>;
51 def IMPLICIT_DEF_FR32  : I<0, Pseudo, (outs FR32:$dst), (ins),
52                            "#IMPLICIT_DEF $dst",
53                            [(set FR32:$dst, (undef))]>, Requires<[HasSSE2]>;
54 def IMPLICIT_DEF_FR64  : I<0, Pseudo, (outs FR64:$dst), (ins),
55                            "#IMPLICIT_DEF $dst",
56                            [(set FR64:$dst, (undef))]>, Requires<[HasSSE2]>;
57
58 //===----------------------------------------------------------------------===//
59 // SSE Complex Patterns
60 //===----------------------------------------------------------------------===//
61
62 // These are 'extloads' from a scalar to the low element of a vector, zeroing
63 // the top elements.  These are used for the SSE 'ss' and 'sd' instruction
64 // forms.
65 def sse_load_f32 : ComplexPattern<v4f32, 4, "SelectScalarSSELoad", [],
66                                   [SDNPHasChain]>;
67 def sse_load_f64 : ComplexPattern<v2f64, 4, "SelectScalarSSELoad", [],
68                                   [SDNPHasChain]>;
69
70 def ssmem : Operand<v4f32> {
71   let PrintMethod = "printf32mem";
72   let MIOperandInfo = (ops ptr_rc, i8imm, ptr_rc, i32imm);
73 }
74 def sdmem : Operand<v2f64> {
75   let PrintMethod = "printf64mem";
76   let MIOperandInfo = (ops ptr_rc, i8imm, ptr_rc, i32imm);
77 }
78
79 //===----------------------------------------------------------------------===//
80 // SSE pattern fragments
81 //===----------------------------------------------------------------------===//
82
83 def loadv4f32    : PatFrag<(ops node:$ptr), (v4f32 (load node:$ptr))>;
84 def loadv2f64    : PatFrag<(ops node:$ptr), (v2f64 (load node:$ptr))>;
85 def loadv4i32    : PatFrag<(ops node:$ptr), (v4i32 (load node:$ptr))>;
86 def loadv2i64    : PatFrag<(ops node:$ptr), (v2i64 (load node:$ptr))>;
87
88 // Like 'store', but always requires vector alignment.
89 def alignedstore : PatFrag<(ops node:$val, node:$ptr),
90                            (st node:$val, node:$ptr), [{
91   if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N))
92     return !ST->isTruncatingStore() &&
93            ST->getAddressingMode() == ISD::UNINDEXED &&
94            ST->getAlignment() >= 16;
95   return false;
96 }]>;
97
98 // Like 'load', but always requires vector alignment.
99 def alignedload : PatFrag<(ops node:$ptr), (ld node:$ptr), [{
100   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
101     return LD->getExtensionType() == ISD::NON_EXTLOAD &&
102            LD->getAddressingMode() == ISD::UNINDEXED &&
103            LD->getAlignment() >= 16;
104   return false;
105 }]>;
106
107 def alignedloadfsf32 : PatFrag<(ops node:$ptr), (f32   (alignedload node:$ptr))>;
108 def alignedloadfsf64 : PatFrag<(ops node:$ptr), (f64   (alignedload node:$ptr))>;
109 def alignedloadv4f32 : PatFrag<(ops node:$ptr), (v4f32 (alignedload node:$ptr))>;
110 def alignedloadv2f64 : PatFrag<(ops node:$ptr), (v2f64 (alignedload node:$ptr))>;
111 def alignedloadv4i32 : PatFrag<(ops node:$ptr), (v4i32 (alignedload node:$ptr))>;
112 def alignedloadv2i64 : PatFrag<(ops node:$ptr), (v2i64 (alignedload node:$ptr))>;
113
114 // Like 'load', but uses special alignment checks suitable for use in
115 // memory operands in most SSE instructions, which are required to
116 // be naturally aligned on some targets but not on others.
117 // FIXME: Actually implement support for targets that don't require the
118 //        alignment. This probably wants a subtarget predicate.
119 def memop : PatFrag<(ops node:$ptr), (ld node:$ptr), [{
120   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
121     return LD->getExtensionType() == ISD::NON_EXTLOAD &&
122            LD->getAddressingMode() == ISD::UNINDEXED &&
123            LD->getAlignment() >= 16;
124   return false;
125 }]>;
126
127 def memopfsf32 : PatFrag<(ops node:$ptr), (f32   (memop node:$ptr))>;
128 def memopfsf64 : PatFrag<(ops node:$ptr), (f64   (memop node:$ptr))>;
129 def memopv4f32 : PatFrag<(ops node:$ptr), (v4f32 (memop node:$ptr))>;
130 def memopv2f64 : PatFrag<(ops node:$ptr), (v2f64 (memop node:$ptr))>;
131 def memopv4i32 : PatFrag<(ops node:$ptr), (v4i32 (memop node:$ptr))>;
132 def memopv2i64 : PatFrag<(ops node:$ptr), (v2i64 (memop node:$ptr))>;
133
134 // SSSE3 uses MMX registers for some instructions. They aren't aligned on a
135 // 16-byte boundary.
136 def memop64 : PatFrag<(ops node:$ptr), (ld node:$ptr), [{
137   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
138     return LD->getExtensionType() == ISD::NON_EXTLOAD &&
139            LD->getAddressingMode() == ISD::UNINDEXED &&
140            LD->getAlignment() >= 8;
141   return false;
142 }]>;
143
144 def memopv8i8  : PatFrag<(ops node:$ptr), (v8i8  (memop64 node:$ptr))>;
145 def memopv16i8 : PatFrag<(ops node:$ptr), (v16i8 (memop64 node:$ptr))>;
146 def memopv4i16 : PatFrag<(ops node:$ptr), (v4i16 (memop64 node:$ptr))>;
147 def memopv8i16 : PatFrag<(ops node:$ptr), (v8i16 (memop64 node:$ptr))>;
148 def memopv2i32 : PatFrag<(ops node:$ptr), (v2i32 (memop64 node:$ptr))>;
149
150 def bc_v4f32 : PatFrag<(ops node:$in), (v4f32 (bitconvert node:$in))>;
151 def bc_v2f64 : PatFrag<(ops node:$in), (v2f64 (bitconvert node:$in))>;
152 def bc_v16i8 : PatFrag<(ops node:$in), (v16i8 (bitconvert node:$in))>;
153 def bc_v8i16 : PatFrag<(ops node:$in), (v8i16 (bitconvert node:$in))>;
154 def bc_v4i32 : PatFrag<(ops node:$in), (v4i32 (bitconvert node:$in))>;
155 def bc_v2i64 : PatFrag<(ops node:$in), (v2i64 (bitconvert node:$in))>;
156
157 def fp32imm0 : PatLeaf<(f32 fpimm), [{
158   return N->isExactlyValue(+0.0);
159 }]>;
160
161 def PSxLDQ_imm  : SDNodeXForm<imm, [{
162   // Transformation function: imm >> 3
163   return getI32Imm(N->getValue() >> 3);
164 }]>;
165
166 // SHUFFLE_get_shuf_imm xform function: convert vector_shuffle mask to PSHUF*,
167 // SHUFP* etc. imm.
168 def SHUFFLE_get_shuf_imm : SDNodeXForm<build_vector, [{
169   return getI8Imm(X86::getShuffleSHUFImmediate(N));
170 }]>;
171
172 // SHUFFLE_get_pshufhw_imm xform function: convert vector_shuffle mask to 
173 // PSHUFHW imm.
174 def SHUFFLE_get_pshufhw_imm : SDNodeXForm<build_vector, [{
175   return getI8Imm(X86::getShufflePSHUFHWImmediate(N));
176 }]>;
177
178 // SHUFFLE_get_pshuflw_imm xform function: convert vector_shuffle mask to 
179 // PSHUFLW imm.
180 def SHUFFLE_get_pshuflw_imm : SDNodeXForm<build_vector, [{
181   return getI8Imm(X86::getShufflePSHUFLWImmediate(N));
182 }]>;
183
184 def SSE_splat_mask : PatLeaf<(build_vector), [{
185   return X86::isSplatMask(N);
186 }], SHUFFLE_get_shuf_imm>;
187
188 def SSE_splat_lo_mask : PatLeaf<(build_vector), [{
189   return X86::isSplatLoMask(N);
190 }]>;
191
192 def MOVHLPS_shuffle_mask : PatLeaf<(build_vector), [{
193   return X86::isMOVHLPSMask(N);
194 }]>;
195
196 def MOVHLPS_v_undef_shuffle_mask : PatLeaf<(build_vector), [{
197   return X86::isMOVHLPS_v_undef_Mask(N);
198 }]>;
199
200 def MOVHP_shuffle_mask : PatLeaf<(build_vector), [{
201   return X86::isMOVHPMask(N);
202 }]>;
203
204 def MOVLP_shuffle_mask : PatLeaf<(build_vector), [{
205   return X86::isMOVLPMask(N);
206 }]>;
207
208 def MOVL_shuffle_mask : PatLeaf<(build_vector), [{
209   return X86::isMOVLMask(N);
210 }]>;
211
212 def MOVSHDUP_shuffle_mask : PatLeaf<(build_vector), [{
213   return X86::isMOVSHDUPMask(N);
214 }]>;
215
216 def MOVSLDUP_shuffle_mask : PatLeaf<(build_vector), [{
217   return X86::isMOVSLDUPMask(N);
218 }]>;
219
220 def UNPCKL_shuffle_mask : PatLeaf<(build_vector), [{
221   return X86::isUNPCKLMask(N);
222 }]>;
223
224 def UNPCKH_shuffle_mask : PatLeaf<(build_vector), [{
225   return X86::isUNPCKHMask(N);
226 }]>;
227
228 def UNPCKL_v_undef_shuffle_mask : PatLeaf<(build_vector), [{
229   return X86::isUNPCKL_v_undef_Mask(N);
230 }]>;
231
232 def UNPCKH_v_undef_shuffle_mask : PatLeaf<(build_vector), [{
233   return X86::isUNPCKH_v_undef_Mask(N);
234 }]>;
235
236 def PSHUFD_shuffle_mask : PatLeaf<(build_vector), [{
237   return X86::isPSHUFDMask(N);
238 }], SHUFFLE_get_shuf_imm>;
239
240 def PSHUFHW_shuffle_mask : PatLeaf<(build_vector), [{
241   return X86::isPSHUFHWMask(N);
242 }], SHUFFLE_get_pshufhw_imm>;
243
244 def PSHUFLW_shuffle_mask : PatLeaf<(build_vector), [{
245   return X86::isPSHUFLWMask(N);
246 }], SHUFFLE_get_pshuflw_imm>;
247
248 def SHUFP_unary_shuffle_mask : PatLeaf<(build_vector), [{
249   return X86::isPSHUFDMask(N);
250 }], SHUFFLE_get_shuf_imm>;
251
252 def SHUFP_shuffle_mask : PatLeaf<(build_vector), [{
253   return X86::isSHUFPMask(N);
254 }], SHUFFLE_get_shuf_imm>;
255
256 def PSHUFD_binary_shuffle_mask : PatLeaf<(build_vector), [{
257   return X86::isSHUFPMask(N);
258 }], SHUFFLE_get_shuf_imm>;
259
260 //===----------------------------------------------------------------------===//
261 // SSE scalar FP Instructions
262 //===----------------------------------------------------------------------===//
263
264 // CMOV* - Used to implement the SSE SELECT DAG operation.  Expanded by the
265 // scheduler into a branch sequence.
266 let usesCustomDAGSchedInserter = 1 in {  // Expanded by the scheduler.
267   def CMOV_FR32 : I<0, Pseudo,
268                     (outs FR32:$dst), (ins FR32:$t, FR32:$f, i8imm:$cond),
269                     "#CMOV_FR32 PSEUDO!",
270                     [(set FR32:$dst, (X86cmov FR32:$t, FR32:$f, imm:$cond))]>;
271   def CMOV_FR64 : I<0, Pseudo,
272                     (outs FR64:$dst), (ins FR64:$t, FR64:$f, i8imm:$cond),
273                     "#CMOV_FR64 PSEUDO!",
274                     [(set FR64:$dst, (X86cmov FR64:$t, FR64:$f, imm:$cond))]>;
275   def CMOV_V4F32 : I<0, Pseudo,
276                     (outs VR128:$dst), (ins VR128:$t, VR128:$f, i8imm:$cond),
277                     "#CMOV_V4F32 PSEUDO!",
278                     [(set VR128:$dst,
279                       (v4f32 (X86cmov VR128:$t, VR128:$f, imm:$cond)))]>;
280   def CMOV_V2F64 : I<0, Pseudo,
281                     (outs VR128:$dst), (ins VR128:$t, VR128:$f, i8imm:$cond),
282                     "#CMOV_V2F64 PSEUDO!",
283                     [(set VR128:$dst,
284                       (v2f64 (X86cmov VR128:$t, VR128:$f, imm:$cond)))]>;
285   def CMOV_V2I64 : I<0, Pseudo,
286                     (outs VR128:$dst), (ins VR128:$t, VR128:$f, i8imm:$cond),
287                     "#CMOV_V2I64 PSEUDO!",
288                     [(set VR128:$dst,
289                       (v2i64 (X86cmov VR128:$t, VR128:$f, imm:$cond)))]>;
290 }
291
292 //===----------------------------------------------------------------------===//
293 // SSE1 Instructions
294 //===----------------------------------------------------------------------===//
295
296 // Move Instructions
297 def MOVSSrr : SSI<0x10, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src),
298                   "movss\t{$src, $dst|$dst, $src}", []>;
299 let isLoad = 1, isReMaterializable = 1 in
300 def MOVSSrm : SSI<0x10, MRMSrcMem, (outs FR32:$dst), (ins f32mem:$src),
301                   "movss\t{$src, $dst|$dst, $src}",
302                   [(set FR32:$dst, (loadf32 addr:$src))]>;
303 def MOVSSmr : SSI<0x11, MRMDestMem, (outs), (ins f32mem:$dst, FR32:$src),
304                   "movss\t{$src, $dst|$dst, $src}",
305                   [(store FR32:$src, addr:$dst)]>;
306
307 // Conversion instructions
308 def CVTTSS2SIrr : SSI<0x2C, MRMSrcReg, (outs GR32:$dst), (ins FR32:$src),
309                       "cvttss2si\t{$src, $dst|$dst, $src}",
310                       [(set GR32:$dst, (fp_to_sint FR32:$src))]>;
311 def CVTTSS2SIrm : SSI<0x2C, MRMSrcMem, (outs GR32:$dst), (ins f32mem:$src),
312                       "cvttss2si\t{$src, $dst|$dst, $src}",
313                       [(set GR32:$dst, (fp_to_sint (loadf32 addr:$src)))]>;
314 def CVTSI2SSrr  : SSI<0x2A, MRMSrcReg, (outs FR32:$dst), (ins GR32:$src),
315                       "cvtsi2ss\t{$src, $dst|$dst, $src}",
316                       [(set FR32:$dst, (sint_to_fp GR32:$src))]>;
317 def CVTSI2SSrm  : SSI<0x2A, MRMSrcMem, (outs FR32:$dst), (ins i32mem:$src),
318                       "cvtsi2ss\t{$src, $dst|$dst, $src}",
319                       [(set FR32:$dst, (sint_to_fp (loadi32 addr:$src)))]>;
320
321 // Match intrinsics which expect XMM operand(s).
322 def Int_CVTSS2SIrr : SSI<0x2D, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
323                          "cvtss2si\t{$src, $dst|$dst, $src}",
324                          [(set GR32:$dst, (int_x86_sse_cvtss2si VR128:$src))]>;
325 def Int_CVTSS2SIrm : SSI<0x2D, MRMSrcMem, (outs GR32:$dst), (ins f32mem:$src),
326                          "cvtss2si\t{$src, $dst|$dst, $src}",
327                          [(set GR32:$dst, (int_x86_sse_cvtss2si
328                                            (load addr:$src)))]>;
329
330 // Aliases for intrinsics
331 def Int_CVTTSS2SIrr : SSI<0x2C, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
332                           "cvttss2si\t{$src, $dst|$dst, $src}",
333                           [(set GR32:$dst,
334                             (int_x86_sse_cvttss2si VR128:$src))]>;
335 def Int_CVTTSS2SIrm : SSI<0x2C, MRMSrcMem, (outs GR32:$dst), (ins f32mem:$src),
336                           "cvttss2si\t{$src, $dst|$dst, $src}",
337                           [(set GR32:$dst,
338                             (int_x86_sse_cvttss2si(load addr:$src)))]>;
339
340 let isTwoAddress = 1 in {
341   def Int_CVTSI2SSrr : SSI<0x2A, MRMSrcReg,
342                            (outs VR128:$dst), (ins VR128:$src1, GR32:$src2),
343                            "cvtsi2ss\t{$src2, $dst|$dst, $src2}",
344                            [(set VR128:$dst, (int_x86_sse_cvtsi2ss VR128:$src1,
345                                               GR32:$src2))]>;
346   def Int_CVTSI2SSrm : SSI<0x2A, MRMSrcMem,
347                            (outs VR128:$dst), (ins VR128:$src1, i32mem:$src2),
348                            "cvtsi2ss\t{$src2, $dst|$dst, $src2}",
349                            [(set VR128:$dst, (int_x86_sse_cvtsi2ss VR128:$src1,
350                                               (loadi32 addr:$src2)))]>;
351 }
352
353 // Comparison instructions
354 let isTwoAddress = 1 in {
355   def CMPSSrr : SSI<0xC2, MRMSrcReg, 
356                     (outs FR32:$dst), (ins FR32:$src1, FR32:$src, SSECC:$cc),
357                     "cmp${cc}ss\t{$src, $dst|$dst, $src}", []>;
358   def CMPSSrm : SSI<0xC2, MRMSrcMem, 
359                     (outs FR32:$dst), (ins FR32:$src1, f32mem:$src, SSECC:$cc),
360                     "cmp${cc}ss\t{$src, $dst|$dst, $src}", []>;
361 }
362
363 def UCOMISSrr: PSI<0x2E, MRMSrcReg, (outs), (ins FR32:$src1, FR32:$src2),
364                    "ucomiss\t{$src2, $src1|$src1, $src2}",
365                    [(X86cmp FR32:$src1, FR32:$src2)]>;
366 def UCOMISSrm: PSI<0x2E, MRMSrcMem, (outs), (ins FR32:$src1, f32mem:$src2),
367                    "ucomiss\t{$src2, $src1|$src1, $src2}",
368                    [(X86cmp FR32:$src1, (loadf32 addr:$src2))]>;
369
370 // Aliases to match intrinsics which expect XMM operand(s).
371 let isTwoAddress = 1 in {
372   def Int_CMPSSrr : SSI<0xC2, MRMSrcReg, 
373                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src, SSECC:$cc),
374                         "cmp${cc}ss\t{$src, $dst|$dst, $src}",
375                         [(set VR128:$dst, (int_x86_sse_cmp_ss VR128:$src1,
376                                            VR128:$src, imm:$cc))]>;
377   def Int_CMPSSrm : SSI<0xC2, MRMSrcMem, 
378                         (outs VR128:$dst), (ins VR128:$src1, f32mem:$src, SSECC:$cc),
379                         "cmp${cc}ss\t{$src, $dst|$dst, $src}",
380                         [(set VR128:$dst, (int_x86_sse_cmp_ss VR128:$src1,
381                                            (load addr:$src), imm:$cc))]>;
382 }
383
384 def Int_UCOMISSrr: PSI<0x2E, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
385                        "ucomiss\t{$src2, $src1|$src1, $src2}",
386                        [(X86ucomi (v4f32 VR128:$src1), VR128:$src2)]>;
387 def Int_UCOMISSrm: PSI<0x2E, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
388                        "ucomiss\t{$src2, $src1|$src1, $src2}",
389                        [(X86ucomi (v4f32 VR128:$src1), (load addr:$src2))]>;
390
391 def Int_COMISSrr: PSI<0x2F, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
392                       "comiss\t{$src2, $src1|$src1, $src2}",
393                       [(X86comi (v4f32 VR128:$src1), VR128:$src2)]>;
394 def Int_COMISSrm: PSI<0x2F, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
395                       "comiss\t{$src2, $src1|$src1, $src2}",
396                       [(X86comi (v4f32 VR128:$src1), (load addr:$src2))]>;
397
398 // Aliases of packed SSE1 instructions for scalar use. These all have names that
399 // start with 'Fs'.
400
401 // Alias instructions that map fld0 to pxor for sse.
402 let isReMaterializable = 1 in
403 def FsFLD0SS : I<0xEF, MRMInitReg, (outs FR32:$dst), (ins),
404                  "pxor\t$dst, $dst", [(set FR32:$dst, fp32imm0)]>,
405                Requires<[HasSSE1]>, TB, OpSize;
406
407 // Alias instruction to do FR32 reg-to-reg copy using movaps. Upper bits are
408 // disregarded.
409 def FsMOVAPSrr : PSI<0x28, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src),
410                      "movaps\t{$src, $dst|$dst, $src}", []>;
411
412 // Alias instruction to load FR32 from f128mem using movaps. Upper bits are
413 // disregarded.
414 let isLoad = 1 in
415 def FsMOVAPSrm : PSI<0x28, MRMSrcMem, (outs FR32:$dst), (ins f128mem:$src),
416                      "movaps\t{$src, $dst|$dst, $src}",
417                      [(set FR32:$dst, (alignedloadfsf32 addr:$src))]>;
418
419 // Alias bitwise logical operations using SSE logical ops on packed FP values.
420 let isTwoAddress = 1 in {
421 let isCommutable = 1 in {
422   def FsANDPSrr : PSI<0x54, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src1, FR32:$src2),
423                       "andps\t{$src2, $dst|$dst, $src2}",
424                       [(set FR32:$dst, (X86fand FR32:$src1, FR32:$src2))]>;
425   def FsORPSrr  : PSI<0x56, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src1, FR32:$src2),
426                       "orps\t{$src2, $dst|$dst, $src2}",
427                       [(set FR32:$dst, (X86for FR32:$src1, FR32:$src2))]>;
428   def FsXORPSrr : PSI<0x57, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src1, FR32:$src2),
429                       "xorps\t{$src2, $dst|$dst, $src2}",
430                       [(set FR32:$dst, (X86fxor FR32:$src1, FR32:$src2))]>;
431 }
432
433 def FsANDPSrm : PSI<0x54, MRMSrcMem, (outs FR32:$dst), (ins FR32:$src1, f128mem:$src2),
434                     "andps\t{$src2, $dst|$dst, $src2}",
435                     [(set FR32:$dst, (X86fand FR32:$src1,
436                                       (memopfsf32 addr:$src2)))]>;
437 def FsORPSrm  : PSI<0x56, MRMSrcMem, (outs FR32:$dst), (ins FR32:$src1, f128mem:$src2),
438                     "orps\t{$src2, $dst|$dst, $src2}",
439                     [(set FR32:$dst, (X86for FR32:$src1,
440                                       (memopfsf32 addr:$src2)))]>;
441 def FsXORPSrm : PSI<0x57, MRMSrcMem, (outs FR32:$dst), (ins FR32:$src1, f128mem:$src2),
442                     "xorps\t{$src2, $dst|$dst, $src2}",
443                     [(set FR32:$dst, (X86fxor FR32:$src1,
444                                       (memopfsf32 addr:$src2)))]>;
445
446 def FsANDNPSrr : PSI<0x55, MRMSrcReg,
447                      (outs FR32:$dst), (ins FR32:$src1, FR32:$src2),
448                      "andnps\t{$src2, $dst|$dst, $src2}", []>;
449 def FsANDNPSrm : PSI<0x55, MRMSrcMem,
450                      (outs FR32:$dst), (ins FR32:$src1, f128mem:$src2),
451                      "andnps\t{$src2, $dst|$dst, $src2}", []>;
452 }
453
454 /// basic_sse1_fp_binop_rm - SSE1 binops come in both scalar and vector forms.
455 ///
456 /// In addition, we also have a special variant of the scalar form here to
457 /// represent the associated intrinsic operation.  This form is unlike the
458 /// plain scalar form, in that it takes an entire vector (instead of a scalar)
459 /// and leaves the top elements undefined.
460 ///
461 /// These three forms can each be reg+reg or reg+mem, so there are a total of
462 /// six "instructions".
463 ///
464 let isTwoAddress = 1 in {
465 multiclass basic_sse1_fp_binop_rm<bits<8> opc, string OpcodeStr,
466                                   SDNode OpNode, Intrinsic F32Int,
467                                   bit Commutable = 0> {
468   // Scalar operation, reg+reg.
469   def SSrr : SSI<opc, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src1, FR32:$src2),
470                  !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
471                  [(set FR32:$dst, (OpNode FR32:$src1, FR32:$src2))]> {
472     let isCommutable = Commutable;
473   }
474
475   // Scalar operation, reg+mem.
476   def SSrm : SSI<opc, MRMSrcMem, (outs FR32:$dst), (ins FR32:$src1, f32mem:$src2),
477                  !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
478                  [(set FR32:$dst, (OpNode FR32:$src1, (load addr:$src2)))]>;
479                  
480   // Vector operation, reg+reg.
481   def PSrr : PSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
482                !strconcat(OpcodeStr, "ps\t{$src2, $dst|$dst, $src2}"),
483                [(set VR128:$dst, (v4f32 (OpNode VR128:$src1, VR128:$src2)))]> {
484     let isCommutable = Commutable;
485   }
486
487   // Vector operation, reg+mem.
488   def PSrm : PSI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
489                  !strconcat(OpcodeStr, "ps\t{$src2, $dst|$dst, $src2}"),
490                  [(set VR128:$dst, (OpNode VR128:$src1, (memopv4f32 addr:$src2)))]>;
491
492   // Intrinsic operation, reg+reg.
493   def SSrr_Int : SSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
494                      !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
495                      [(set VR128:$dst, (F32Int VR128:$src1, VR128:$src2))]> {
496     let isCommutable = Commutable;
497   }
498
499   // Intrinsic operation, reg+mem.
500   def SSrm_Int : SSI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, ssmem:$src2),
501                      !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
502                      [(set VR128:$dst, (F32Int VR128:$src1,
503                                                sse_load_f32:$src2))]>;
504 }
505 }
506
507 // Arithmetic instructions
508 defm ADD : basic_sse1_fp_binop_rm<0x58, "add", fadd, int_x86_sse_add_ss, 1>;
509 defm MUL : basic_sse1_fp_binop_rm<0x59, "mul", fmul, int_x86_sse_mul_ss, 1>;
510 defm SUB : basic_sse1_fp_binop_rm<0x5C, "sub", fsub, int_x86_sse_sub_ss>;
511 defm DIV : basic_sse1_fp_binop_rm<0x5E, "div", fdiv, int_x86_sse_div_ss>;
512
513 /// sse1_fp_binop_rm - Other SSE1 binops
514 ///
515 /// This multiclass is like basic_sse1_fp_binop_rm, with the addition of
516 /// instructions for a full-vector intrinsic form.  Operations that map
517 /// onto C operators don't use this form since they just use the plain
518 /// vector form instead of having a separate vector intrinsic form.
519 ///
520 /// This provides a total of eight "instructions".
521 ///
522 let isTwoAddress = 1 in {
523 multiclass sse1_fp_binop_rm<bits<8> opc, string OpcodeStr,
524                             SDNode OpNode,
525                             Intrinsic F32Int,
526                             Intrinsic V4F32Int,
527                             bit Commutable = 0> {
528
529   // Scalar operation, reg+reg.
530   def SSrr : SSI<opc, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src1, FR32:$src2),
531                  !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
532                  [(set FR32:$dst, (OpNode FR32:$src1, FR32:$src2))]> {
533     let isCommutable = Commutable;
534   }
535
536   // Scalar operation, reg+mem.
537   def SSrm : SSI<opc, MRMSrcMem, (outs FR32:$dst), (ins FR32:$src1, f32mem:$src2),
538                  !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
539                  [(set FR32:$dst, (OpNode FR32:$src1, (load addr:$src2)))]>;
540                  
541   // Vector operation, reg+reg.
542   def PSrr : PSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
543                !strconcat(OpcodeStr, "ps\t{$src2, $dst|$dst, $src2}"),
544                [(set VR128:$dst, (v4f32 (OpNode VR128:$src1, VR128:$src2)))]> {
545     let isCommutable = Commutable;
546   }
547
548   // Vector operation, reg+mem.
549   def PSrm : PSI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
550                  !strconcat(OpcodeStr, "ps\t{$src2, $dst|$dst, $src2}"),
551                  [(set VR128:$dst, (OpNode VR128:$src1, (memopv4f32 addr:$src2)))]>;
552
553   // Intrinsic operation, reg+reg.
554   def SSrr_Int : SSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
555                      !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
556                      [(set VR128:$dst, (F32Int VR128:$src1, VR128:$src2))]> {
557     let isCommutable = Commutable;
558   }
559
560   // Intrinsic operation, reg+mem.
561   def SSrm_Int : SSI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, ssmem:$src2),
562                      !strconcat(OpcodeStr, "ss\t{$src2, $dst|$dst, $src2}"),
563                      [(set VR128:$dst, (F32Int VR128:$src1,
564                                                sse_load_f32:$src2))]>;
565
566   // Vector intrinsic operation, reg+reg.
567   def PSrr_Int : PSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
568                      !strconcat(OpcodeStr, "ps\t{$src2, $dst|$dst, $src2}"),
569                      [(set VR128:$dst, (V4F32Int VR128:$src1, VR128:$src2))]> {
570     let isCommutable = Commutable;
571   }
572
573   // Vector intrinsic operation, reg+mem.
574   def PSrm_Int : PSI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
575                      !strconcat(OpcodeStr, "ps\t{$src2, $dst|$dst, $src2}"),
576                      [(set VR128:$dst, (V4F32Int VR128:$src1, (load addr:$src2)))]>;
577 }
578 }
579
580 defm MAX : sse1_fp_binop_rm<0x5F, "max", X86fmax,
581                             int_x86_sse_max_ss, int_x86_sse_max_ps>;
582 defm MIN : sse1_fp_binop_rm<0x5D, "min", X86fmin,
583                             int_x86_sse_min_ss, int_x86_sse_min_ps>;
584
585 //===----------------------------------------------------------------------===//
586 // SSE packed FP Instructions
587
588 // Move Instructions
589 def MOVAPSrr : PSI<0x28, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
590                    "movaps\t{$src, $dst|$dst, $src}", []>;
591 let isLoad = 1, isReMaterializable = 1 in
592 def MOVAPSrm : PSI<0x28, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
593                    "movaps\t{$src, $dst|$dst, $src}",
594                    [(set VR128:$dst, (alignedloadv4f32 addr:$src))]>;
595
596 def MOVAPSmr : PSI<0x29, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
597                    "movaps\t{$src, $dst|$dst, $src}",
598                    [(alignedstore (v4f32 VR128:$src), addr:$dst)]>;
599
600 def MOVUPSrr : PSI<0x10, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
601                    "movups\t{$src, $dst|$dst, $src}", []>;
602 let isLoad = 1 in
603 def MOVUPSrm : PSI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
604                    "movups\t{$src, $dst|$dst, $src}",
605                    [(set VR128:$dst, (loadv4f32 addr:$src))]>;
606 def MOVUPSmr : PSI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
607                    "movups\t{$src, $dst|$dst, $src}",
608                    [(store (v4f32 VR128:$src), addr:$dst)]>;
609
610 // Intrinsic forms of MOVUPS load and store
611 let isLoad = 1 in
612 def MOVUPSrm_Int : PSI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
613                        "movups\t{$src, $dst|$dst, $src}",
614                        [(set VR128:$dst, (int_x86_sse_loadu_ps addr:$src))]>;
615 def MOVUPSmr_Int : PSI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
616                        "movups\t{$src, $dst|$dst, $src}",
617                        [(int_x86_sse_storeu_ps addr:$dst, VR128:$src)]>;
618
619 let isTwoAddress = 1 in {
620   let AddedComplexity = 20 in {
621     def MOVLPSrm : PSI<0x12, MRMSrcMem,
622                        (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
623                        "movlps\t{$src2, $dst|$dst, $src2}",
624                        [(set VR128:$dst, 
625                          (v4f32 (vector_shuffle VR128:$src1,
626                          (bc_v4f32 (v2f64 (scalar_to_vector (loadf64 addr:$src2)))),
627                                  MOVLP_shuffle_mask)))]>;
628     def MOVHPSrm : PSI<0x16, MRMSrcMem,
629                        (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
630                        "movhps\t{$src2, $dst|$dst, $src2}",
631                        [(set VR128:$dst, 
632                          (v4f32 (vector_shuffle VR128:$src1,
633                          (bc_v4f32 (v2f64 (scalar_to_vector (loadf64 addr:$src2)))),
634                                  MOVHP_shuffle_mask)))]>;
635   } // AddedComplexity
636 } // isTwoAddress
637
638 def MOVLPSmr : PSI<0x13, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
639                    "movlps\t{$src, $dst|$dst, $src}",
640                    [(store (f64 (vector_extract (bc_v2f64 (v4f32 VR128:$src)),
641                                  (iPTR 0))), addr:$dst)]>;
642
643 // v2f64 extract element 1 is always custom lowered to unpack high to low
644 // and extract element 0 so the non-store version isn't too horrible.
645 def MOVHPSmr : PSI<0x17, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
646                    "movhps\t{$src, $dst|$dst, $src}",
647                    [(store (f64 (vector_extract
648                                  (v2f64 (vector_shuffle
649                                          (bc_v2f64 (v4f32 VR128:$src)), (undef),
650                                          UNPCKH_shuffle_mask)), (iPTR 0))),
651                      addr:$dst)]>;
652
653 let isTwoAddress = 1 in {
654 let AddedComplexity = 15 in {
655 def MOVLHPSrr : PSI<0x16, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
656                     "movlhps\t{$src2, $dst|$dst, $src2}",
657                     [(set VR128:$dst,
658                       (v4f32 (vector_shuffle VR128:$src1, VR128:$src2,
659                               MOVHP_shuffle_mask)))]>;
660
661 def MOVHLPSrr : PSI<0x12, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
662                     "movhlps\t{$src2, $dst|$dst, $src2}",
663                     [(set VR128:$dst,
664                       (v4f32 (vector_shuffle VR128:$src1, VR128:$src2,
665                               MOVHLPS_shuffle_mask)))]>;
666 } // AddedComplexity
667 } // isTwoAddress
668
669
670
671 // Arithmetic
672
673 /// sse1_fp_unop_rm - SSE1 unops come in both scalar and vector forms.
674 ///
675 /// In addition, we also have a special variant of the scalar form here to
676 /// represent the associated intrinsic operation.  This form is unlike the
677 /// plain scalar form, in that it takes an entire vector (instead of a
678 /// scalar) and leaves the top elements undefined.
679 ///
680 /// And, we have a special variant form for a full-vector intrinsic form.
681 ///
682 /// These four forms can each have a reg or a mem operand, so there are a
683 /// total of eight "instructions".
684 ///
685 multiclass sse1_fp_unop_rm<bits<8> opc, string OpcodeStr,
686                            SDNode OpNode,
687                            Intrinsic F32Int,
688                            Intrinsic V4F32Int,
689                            bit Commutable = 0> {
690   // Scalar operation, reg.
691   def SSr : SSI<opc, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src),
692                 !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
693                 [(set FR32:$dst, (OpNode FR32:$src))]> {
694     let isCommutable = Commutable;
695   }
696
697   // Scalar operation, mem.
698   def SSm : SSI<opc, MRMSrcMem, (outs FR32:$dst), (ins f32mem:$src),
699                 !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
700                 [(set FR32:$dst, (OpNode (load addr:$src)))]>;
701                  
702   // Vector operation, reg.
703   def PSr : PSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
704               !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
705               [(set VR128:$dst, (v4f32 (OpNode VR128:$src)))]> {
706     let isCommutable = Commutable;
707   }
708
709   // Vector operation, mem.
710   def PSm : PSI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
711                 !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
712                 [(set VR128:$dst, (OpNode (memopv4f32 addr:$src)))]>;
713
714   // Intrinsic operation, reg.
715   def SSr_Int : SSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
716                     !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
717                     [(set VR128:$dst, (F32Int VR128:$src))]> {
718     let isCommutable = Commutable;
719   }
720
721   // Intrinsic operation, mem.
722   def SSm_Int : SSI<opc, MRMSrcMem, (outs VR128:$dst), (ins ssmem:$src),
723                     !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
724                     [(set VR128:$dst, (F32Int sse_load_f32:$src))]>;
725
726   // Vector intrinsic operation, reg
727   def PSr_Int : PSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
728                     !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
729                     [(set VR128:$dst, (V4F32Int VR128:$src))]> {
730     let isCommutable = Commutable;
731   }
732
733   // Vector intrinsic operation, mem
734   def PSm_Int : PSI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
735                     !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
736                     [(set VR128:$dst, (V4F32Int (load addr:$src)))]>;
737 }
738
739 // Square root.
740 defm SQRT  : sse1_fp_unop_rm<0x51, "sqrt",  fsqrt,
741                              int_x86_sse_sqrt_ss, int_x86_sse_sqrt_ps>;
742
743 // Reciprocal approximations. Note that these typically require refinement
744 // in order to obtain suitable precision.
745 defm RSQRT : sse1_fp_unop_rm<0x52, "rsqrt", X86frsqrt,
746                              int_x86_sse_rsqrt_ss, int_x86_sse_rsqrt_ps>;
747 defm RCP   : sse1_fp_unop_rm<0x53, "rcp",   X86frcp,
748                              int_x86_sse_rcp_ss, int_x86_sse_rcp_ps>;
749
750 // Logical
751 let isTwoAddress = 1 in {
752   let isCommutable = 1 in {
753     def ANDPSrr : PSI<0x54, MRMSrcReg,
754                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
755                       "andps\t{$src2, $dst|$dst, $src2}",
756                       [(set VR128:$dst, (v2i64
757                                          (and VR128:$src1, VR128:$src2)))]>;
758     def ORPSrr  : PSI<0x56, MRMSrcReg,
759                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
760                       "orps\t{$src2, $dst|$dst, $src2}",
761                       [(set VR128:$dst, (v2i64
762                                          (or VR128:$src1, VR128:$src2)))]>;
763     def XORPSrr : PSI<0x57, MRMSrcReg,
764                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
765                       "xorps\t{$src2, $dst|$dst, $src2}",
766                       [(set VR128:$dst, (v2i64
767                                          (xor VR128:$src1, VR128:$src2)))]>;
768   }
769
770   def ANDPSrm : PSI<0x54, MRMSrcMem,
771                     (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
772                     "andps\t{$src2, $dst|$dst, $src2}",
773                     [(set VR128:$dst, (and (bc_v2i64 (v4f32 VR128:$src1)),
774                                        (memopv2i64 addr:$src2)))]>;
775   def ORPSrm  : PSI<0x56, MRMSrcMem,
776                     (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
777                     "orps\t{$src2, $dst|$dst, $src2}",
778                     [(set VR128:$dst, (or (bc_v2i64 (v4f32 VR128:$src1)),
779                                        (memopv2i64 addr:$src2)))]>;
780   def XORPSrm : PSI<0x57, MRMSrcMem,
781                     (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
782                     "xorps\t{$src2, $dst|$dst, $src2}",
783                     [(set VR128:$dst, (xor (bc_v2i64 (v4f32 VR128:$src1)),
784                                        (memopv2i64 addr:$src2)))]>;
785   def ANDNPSrr : PSI<0x55, MRMSrcReg,
786                      (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
787                      "andnps\t{$src2, $dst|$dst, $src2}",
788                      [(set VR128:$dst,
789                        (v2i64 (and (xor VR128:$src1,
790                                     (bc_v2i64 (v4i32 immAllOnesV))),
791                                VR128:$src2)))]>;
792   def ANDNPSrm : PSI<0x55, MRMSrcMem,
793                      (outs VR128:$dst), (ins VR128:$src1,f128mem:$src2),
794                      "andnps\t{$src2, $dst|$dst, $src2}",
795                      [(set VR128:$dst,
796                        (v2i64 (and (xor (bc_v2i64 (v4f32 VR128:$src1)),
797                                     (bc_v2i64 (v4i32 immAllOnesV))),
798                                (memopv2i64 addr:$src2))))]>;
799 }
800
801 let isTwoAddress = 1 in {
802   def CMPPSrri : PSIi8<0xC2, MRMSrcReg, 
803                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src, SSECC:$cc),
804                       "cmp${cc}ps\t{$src, $dst|$dst, $src}",
805                       [(set VR128:$dst, (int_x86_sse_cmp_ps VR128:$src1,
806                                          VR128:$src, imm:$cc))]>;
807   def CMPPSrmi : PSIi8<0xC2, MRMSrcMem, 
808                       (outs VR128:$dst), (ins VR128:$src1, f128mem:$src, SSECC:$cc),
809                       "cmp${cc}ps\t{$src, $dst|$dst, $src}",
810                       [(set VR128:$dst, (int_x86_sse_cmp_ps VR128:$src1,
811                                          (load addr:$src), imm:$cc))]>;
812 }
813
814 // Shuffle and unpack instructions
815 let isTwoAddress = 1 in {
816   let isConvertibleToThreeAddress = 1 in // Convert to pshufd
817     def SHUFPSrri : PSIi8<0xC6, MRMSrcReg, 
818                           (outs VR128:$dst), (ins VR128:$src1,
819                            VR128:$src2, i32i8imm:$src3),
820                           "shufps\t{$src3, $src2, $dst|$dst, $src2, $src3}",
821                           [(set VR128:$dst,
822                             (v4f32 (vector_shuffle
823                                     VR128:$src1, VR128:$src2,
824                                     SHUFP_shuffle_mask:$src3)))]>;
825   def SHUFPSrmi : PSIi8<0xC6, MRMSrcMem, 
826                         (outs VR128:$dst), (ins VR128:$src1,
827                          f128mem:$src2, i32i8imm:$src3),
828                         "shufps\t{$src3, $src2, $dst|$dst, $src2, $src3}",
829                         [(set VR128:$dst,
830                           (v4f32 (vector_shuffle
831                                   VR128:$src1, (memopv4f32 addr:$src2),
832                                   SHUFP_shuffle_mask:$src3)))]>;
833
834   let AddedComplexity = 10 in {
835     def UNPCKHPSrr : PSI<0x15, MRMSrcReg, 
836                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
837                          "unpckhps\t{$src2, $dst|$dst, $src2}",
838                          [(set VR128:$dst,
839                            (v4f32 (vector_shuffle
840                                    VR128:$src1, VR128:$src2,
841                                    UNPCKH_shuffle_mask)))]>;
842     def UNPCKHPSrm : PSI<0x15, MRMSrcMem, 
843                          (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
844                          "unpckhps\t{$src2, $dst|$dst, $src2}",
845                          [(set VR128:$dst,
846                            (v4f32 (vector_shuffle
847                                    VR128:$src1, (memopv4f32 addr:$src2),
848                                    UNPCKH_shuffle_mask)))]>;
849
850     def UNPCKLPSrr : PSI<0x14, MRMSrcReg, 
851                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
852                          "unpcklps\t{$src2, $dst|$dst, $src2}",
853                          [(set VR128:$dst,
854                            (v4f32 (vector_shuffle
855                                    VR128:$src1, VR128:$src2,
856                                    UNPCKL_shuffle_mask)))]>;
857     def UNPCKLPSrm : PSI<0x14, MRMSrcMem, 
858                          (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
859                          "unpcklps\t{$src2, $dst|$dst, $src2}",
860                          [(set VR128:$dst,
861                            (v4f32 (vector_shuffle
862                                    VR128:$src1, (memopv4f32 addr:$src2),
863                                    UNPCKL_shuffle_mask)))]>;
864   } // AddedComplexity
865 } // isTwoAddress
866
867 // Mask creation
868 def MOVMSKPSrr : PSI<0x50, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
869                      "movmskps\t{$src, $dst|$dst, $src}",
870                      [(set GR32:$dst, (int_x86_sse_movmsk_ps VR128:$src))]>;
871 def MOVMSKPDrr : PSI<0x50, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
872                      "movmskpd\t{$src, $dst|$dst, $src}",
873                      [(set GR32:$dst, (int_x86_sse2_movmsk_pd VR128:$src))]>;
874
875 // Prefetching loads.
876 // TODO: no intrinsics for these?
877 def PREFETCHT0   : PSI<0x18, MRM1m, (outs), (ins i8mem:$src), "prefetcht0\t$src", []>;
878 def PREFETCHT1   : PSI<0x18, MRM2m, (outs), (ins i8mem:$src), "prefetcht1\t$src", []>;
879 def PREFETCHT2   : PSI<0x18, MRM3m, (outs), (ins i8mem:$src), "prefetcht2\t$src", []>;
880 def PREFETCHNTA  : PSI<0x18, MRM0m, (outs), (ins i8mem:$src), "prefetchnta\t$src", []>;
881
882 // Non-temporal stores
883 def MOVNTPSmr : PSI<0x2B, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
884                     "movntps\t{$src, $dst|$dst, $src}",
885                     [(int_x86_sse_movnt_ps addr:$dst, VR128:$src)]>;
886
887 // Load, store, and memory fence
888 def SFENCE : PSI<0xAE, MRM7m, (outs), (ins), "sfence", [(int_x86_sse_sfence)]>;
889
890 // MXCSR register
891 def LDMXCSR : PSI<0xAE, MRM2m, (outs), (ins i32mem:$src),
892                   "ldmxcsr\t$src", [(int_x86_sse_ldmxcsr addr:$src)]>;
893 def STMXCSR : PSI<0xAE, MRM3m, (outs), (ins i32mem:$dst),
894                   "stmxcsr\t$dst", [(int_x86_sse_stmxcsr addr:$dst)]>;
895
896 // Alias instructions that map zero vector to pxor / xorp* for sse.
897 // FIXME: remove when we can teach regalloc that xor reg, reg is ok.
898 let isReMaterializable = 1 in
899 def V_SET0 : PSI<0x57, MRMInitReg, (outs VR128:$dst), (ins),
900                  "xorps\t$dst, $dst",
901                  [(set VR128:$dst, (v4f32 immAllZerosV))]>;
902
903 // FR32 to 128-bit vector conversion.
904 def MOVSS2PSrr : SSI<0x10, MRMSrcReg, (outs VR128:$dst), (ins FR32:$src),
905                       "movss\t{$src, $dst|$dst, $src}",
906                       [(set VR128:$dst,
907                         (v4f32 (scalar_to_vector FR32:$src)))]>;
908 def MOVSS2PSrm : SSI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f32mem:$src),
909                      "movss\t{$src, $dst|$dst, $src}",
910                      [(set VR128:$dst,
911                        (v4f32 (scalar_to_vector (loadf32 addr:$src))))]>;
912
913 // FIXME: may not be able to eliminate this movss with coalescing the src and
914 // dest register classes are different. We really want to write this pattern
915 // like this:
916 // def : Pat<(f32 (vector_extract (v4f32 VR128:$src), (iPTR 0))),
917 //           (f32 FR32:$src)>;
918 def MOVPS2SSrr : SSI<0x10, MRMSrcReg, (outs FR32:$dst), (ins VR128:$src),
919                      "movss\t{$src, $dst|$dst, $src}",
920                      [(set FR32:$dst, (vector_extract (v4f32 VR128:$src),
921                                        (iPTR 0)))]>;
922 def MOVPS2SSmr : SSI<0x11, MRMDestMem, (outs), (ins f32mem:$dst, VR128:$src),
923                      "movss\t{$src, $dst|$dst, $src}",
924                      [(store (f32 (vector_extract (v4f32 VR128:$src),
925                                    (iPTR 0))), addr:$dst)]>;
926
927
928 // Move to lower bits of a VR128, leaving upper bits alone.
929 // Three operand (but two address) aliases.
930 let isTwoAddress = 1 in {
931   def MOVLSS2PSrr : SSI<0x10, MRMSrcReg,
932                         (outs VR128:$dst), (ins VR128:$src1, FR32:$src2),
933                         "movss\t{$src2, $dst|$dst, $src2}", []>;
934
935   let AddedComplexity = 15 in
936     def MOVLPSrr : SSI<0x10, MRMSrcReg,
937                        (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
938                        "movss\t{$src2, $dst|$dst, $src2}",
939                        [(set VR128:$dst,
940                          (v4f32 (vector_shuffle VR128:$src1, VR128:$src2,
941                                  MOVL_shuffle_mask)))]>;
942 }
943
944 // Move to lower bits of a VR128 and zeroing upper bits.
945 // Loading from memory automatically zeroing upper bits.
946 let AddedComplexity = 20 in
947 def MOVZSS2PSrm : SSI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f32mem:$src),
948                       "movss\t{$src, $dst|$dst, $src}",
949                       [(set VR128:$dst, (v4f32 (vector_shuffle immAllZerosV,
950                                  (v4f32 (scalar_to_vector (loadf32 addr:$src))),
951                                                 MOVL_shuffle_mask)))]>;
952
953
954 //===----------------------------------------------------------------------===//
955 // SSE2 Instructions
956 //===----------------------------------------------------------------------===//
957
958 // Move Instructions
959 def MOVSDrr : SDI<0x10, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src),
960                   "movsd\t{$src, $dst|$dst, $src}", []>;
961 let isLoad = 1, isReMaterializable = 1 in
962 def MOVSDrm : SDI<0x10, MRMSrcMem, (outs FR64:$dst), (ins f64mem:$src),
963                   "movsd\t{$src, $dst|$dst, $src}",
964                   [(set FR64:$dst, (loadf64 addr:$src))]>;
965 def MOVSDmr : SDI<0x11, MRMDestMem, (outs), (ins f64mem:$dst, FR64:$src),
966                   "movsd\t{$src, $dst|$dst, $src}",
967                   [(store FR64:$src, addr:$dst)]>;
968
969 // Conversion instructions
970 def CVTTSD2SIrr : SDI<0x2C, MRMSrcReg, (outs GR32:$dst), (ins FR64:$src),
971                       "cvttsd2si\t{$src, $dst|$dst, $src}",
972                       [(set GR32:$dst, (fp_to_sint FR64:$src))]>;
973 def CVTTSD2SIrm : SDI<0x2C, MRMSrcMem, (outs GR32:$dst), (ins f64mem:$src),
974                       "cvttsd2si\t{$src, $dst|$dst, $src}",
975                       [(set GR32:$dst, (fp_to_sint (loadf64 addr:$src)))]>;
976 def CVTSD2SSrr  : SDI<0x5A, MRMSrcReg, (outs FR32:$dst), (ins FR64:$src),
977                       "cvtsd2ss\t{$src, $dst|$dst, $src}",
978                       [(set FR32:$dst, (fround FR64:$src))]>;
979 def CVTSD2SSrm  : SDI<0x5A, MRMSrcMem, (outs FR32:$dst), (ins f64mem:$src), 
980                       "cvtsd2ss\t{$src, $dst|$dst, $src}",
981                       [(set FR32:$dst, (fround (loadf64 addr:$src)))]>;
982 def CVTSI2SDrr  : SDI<0x2A, MRMSrcReg, (outs FR64:$dst), (ins GR32:$src),
983                       "cvtsi2sd\t{$src, $dst|$dst, $src}",
984                       [(set FR64:$dst, (sint_to_fp GR32:$src))]>;
985 def CVTSI2SDrm  : SDI<0x2A, MRMSrcMem, (outs FR64:$dst), (ins i32mem:$src),
986                       "cvtsi2sd\t{$src, $dst|$dst, $src}",
987                       [(set FR64:$dst, (sint_to_fp (loadi32 addr:$src)))]>;
988
989 // SSE2 instructions with XS prefix
990 def CVTSS2SDrr : I<0x5A, MRMSrcReg, (outs FR64:$dst), (ins FR32:$src),
991                    "cvtss2sd\t{$src, $dst|$dst, $src}",
992                    [(set FR64:$dst, (fextend FR32:$src))]>, XS,
993                  Requires<[HasSSE2]>;
994 def CVTSS2SDrm : I<0x5A, MRMSrcMem, (outs FR64:$dst), (ins f32mem:$src),
995                    "cvtss2sd\t{$src, $dst|$dst, $src}",
996                    [(set FR64:$dst, (extloadf32 addr:$src))]>, XS,
997                  Requires<[HasSSE2]>;
998
999 // Match intrinsics which expect XMM operand(s).
1000 def Int_CVTSD2SIrr : SDI<0x2D, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
1001                          "cvtsd2si\t{$src, $dst|$dst, $src}",
1002                          [(set GR32:$dst, (int_x86_sse2_cvtsd2si VR128:$src))]>;
1003 def Int_CVTSD2SIrm : SDI<0x2D, MRMSrcMem, (outs GR32:$dst), (ins f128mem:$src),
1004                          "cvtsd2si\t{$src, $dst|$dst, $src}",
1005                          [(set GR32:$dst, (int_x86_sse2_cvtsd2si
1006                                            (load addr:$src)))]>;
1007
1008 // Aliases for intrinsics
1009 def Int_CVTTSD2SIrr : SDI<0x2C, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
1010                           "cvttsd2si\t{$src, $dst|$dst, $src}",
1011                           [(set GR32:$dst,
1012                             (int_x86_sse2_cvttsd2si VR128:$src))]>;
1013 def Int_CVTTSD2SIrm : SDI<0x2C, MRMSrcMem, (outs GR32:$dst), (ins f128mem:$src),
1014                           "cvttsd2si\t{$src, $dst|$dst, $src}",
1015                           [(set GR32:$dst, (int_x86_sse2_cvttsd2si
1016                                             (load addr:$src)))]>;
1017
1018 // Comparison instructions
1019 let isTwoAddress = 1 in {
1020   def CMPSDrr : SDI<0xC2, MRMSrcReg, 
1021                     (outs FR64:$dst), (ins FR64:$src1, FR64:$src, SSECC:$cc),
1022                     "cmp${cc}sd\t{$src, $dst|$dst, $src}", []>;
1023   def CMPSDrm : SDI<0xC2, MRMSrcMem, 
1024                     (outs FR64:$dst), (ins FR64:$src1, f64mem:$src, SSECC:$cc),
1025                     "cmp${cc}sd\t{$src, $dst|$dst, $src}", []>;
1026 }
1027
1028 def UCOMISDrr: PDI<0x2E, MRMSrcReg, (outs), (ins FR64:$src1, FR64:$src2),
1029                    "ucomisd\t{$src2, $src1|$src1, $src2}",
1030                    [(X86cmp FR64:$src1, FR64:$src2)]>;
1031 def UCOMISDrm: PDI<0x2E, MRMSrcMem, (outs), (ins FR64:$src1, f64mem:$src2),
1032                    "ucomisd\t{$src2, $src1|$src1, $src2}",
1033                    [(X86cmp FR64:$src1, (loadf64 addr:$src2))]>;
1034
1035 // Aliases to match intrinsics which expect XMM operand(s).
1036 let isTwoAddress = 1 in {
1037   def Int_CMPSDrr : SDI<0xC2, MRMSrcReg, 
1038                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src, SSECC:$cc),
1039                         "cmp${cc}sd\t{$src, $dst|$dst, $src}",
1040                         [(set VR128:$dst, (int_x86_sse2_cmp_sd VR128:$src1,
1041                                            VR128:$src, imm:$cc))]>;
1042   def Int_CMPSDrm : SDI<0xC2, MRMSrcMem, 
1043                         (outs VR128:$dst), (ins VR128:$src1, f64mem:$src, SSECC:$cc),
1044                         "cmp${cc}sd\t{$src, $dst|$dst, $src}",
1045                         [(set VR128:$dst, (int_x86_sse2_cmp_sd VR128:$src1,
1046                                            (load addr:$src), imm:$cc))]>;
1047 }
1048
1049 def Int_UCOMISDrr: PDI<0x2E, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
1050                        "ucomisd\t{$src2, $src1|$src1, $src2}",
1051                        [(X86ucomi (v2f64 VR128:$src1), (v2f64 VR128:$src2))]>;
1052 def Int_UCOMISDrm: PDI<0x2E, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
1053                        "ucomisd\t{$src2, $src1|$src1, $src2}",
1054                        [(X86ucomi (v2f64 VR128:$src1), (load addr:$src2))]>;
1055
1056 def Int_COMISDrr: PDI<0x2F, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
1057                       "comisd\t{$src2, $src1|$src1, $src2}",
1058                       [(X86comi (v2f64 VR128:$src1), (v2f64 VR128:$src2))]>;
1059 def Int_COMISDrm: PDI<0x2F, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
1060                       "comisd\t{$src2, $src1|$src1, $src2}",
1061                       [(X86comi (v2f64 VR128:$src1), (load addr:$src2))]>;
1062
1063 // Aliases of packed SSE2 instructions for scalar use. These all have names that
1064 // start with 'Fs'.
1065
1066 // Alias instructions that map fld0 to pxor for sse.
1067 let isReMaterializable = 1 in
1068 def FsFLD0SD : I<0xEF, MRMInitReg, (outs FR64:$dst), (ins),
1069                  "pxor\t$dst, $dst", [(set FR64:$dst, fpimm0)]>,
1070                Requires<[HasSSE2]>, TB, OpSize;
1071
1072 // Alias instruction to do FR64 reg-to-reg copy using movapd. Upper bits are
1073 // disregarded.
1074 def FsMOVAPDrr : PDI<0x28, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src),
1075                      "movapd\t{$src, $dst|$dst, $src}", []>;
1076
1077 // Alias instruction to load FR64 from f128mem using movapd. Upper bits are
1078 // disregarded.
1079 let isLoad = 1 in
1080 def FsMOVAPDrm : PDI<0x28, MRMSrcMem, (outs FR64:$dst), (ins f128mem:$src),
1081                      "movapd\t{$src, $dst|$dst, $src}",
1082                      [(set FR64:$dst, (alignedloadfsf64 addr:$src))]>;
1083
1084 // Alias bitwise logical operations using SSE logical ops on packed FP values.
1085 let isTwoAddress = 1 in {
1086 let isCommutable = 1 in {
1087   def FsANDPDrr : PDI<0x54, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src1, FR64:$src2),
1088                       "andpd\t{$src2, $dst|$dst, $src2}",
1089                       [(set FR64:$dst, (X86fand FR64:$src1, FR64:$src2))]>;
1090   def FsORPDrr  : PDI<0x56, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src1, FR64:$src2),
1091                       "orpd\t{$src2, $dst|$dst, $src2}",
1092                       [(set FR64:$dst, (X86for FR64:$src1, FR64:$src2))]>;
1093   def FsXORPDrr : PDI<0x57, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src1, FR64:$src2),
1094                       "xorpd\t{$src2, $dst|$dst, $src2}",
1095                       [(set FR64:$dst, (X86fxor FR64:$src1, FR64:$src2))]>;
1096 }
1097
1098 def FsANDPDrm : PDI<0x54, MRMSrcMem, (outs FR64:$dst), (ins FR64:$src1, f128mem:$src2),
1099                     "andpd\t{$src2, $dst|$dst, $src2}",
1100                     [(set FR64:$dst, (X86fand FR64:$src1,
1101                                       (memopfsf64 addr:$src2)))]>;
1102 def FsORPDrm  : PDI<0x56, MRMSrcMem, (outs FR64:$dst), (ins FR64:$src1, f128mem:$src2),
1103                     "orpd\t{$src2, $dst|$dst, $src2}",
1104                     [(set FR64:$dst, (X86for FR64:$src1,
1105                                       (memopfsf64 addr:$src2)))]>;
1106 def FsXORPDrm : PDI<0x57, MRMSrcMem, (outs FR64:$dst), (ins FR64:$src1, f128mem:$src2),
1107                     "xorpd\t{$src2, $dst|$dst, $src2}",
1108                     [(set FR64:$dst, (X86fxor FR64:$src1,
1109                                       (memopfsf64 addr:$src2)))]>;
1110
1111 def FsANDNPDrr : PDI<0x55, MRMSrcReg,
1112                      (outs FR64:$dst), (ins FR64:$src1, FR64:$src2),
1113                      "andnpd\t{$src2, $dst|$dst, $src2}", []>;
1114 def FsANDNPDrm : PDI<0x55, MRMSrcMem,
1115                      (outs FR64:$dst), (ins FR64:$src1, f128mem:$src2),
1116                      "andnpd\t{$src2, $dst|$dst, $src2}", []>;
1117 }
1118
1119 /// basic_sse2_fp_binop_rm - SSE2 binops come in both scalar and vector forms.
1120 ///
1121 /// In addition, we also have a special variant of the scalar form here to
1122 /// represent the associated intrinsic operation.  This form is unlike the
1123 /// plain scalar form, in that it takes an entire vector (instead of a scalar)
1124 /// and leaves the top elements undefined.
1125 ///
1126 /// These three forms can each be reg+reg or reg+mem, so there are a total of
1127 /// six "instructions".
1128 ///
1129 let isTwoAddress = 1 in {
1130 multiclass basic_sse2_fp_binop_rm<bits<8> opc, string OpcodeStr,
1131                                   SDNode OpNode, Intrinsic F64Int,
1132                                   bit Commutable = 0> {
1133   // Scalar operation, reg+reg.
1134   def SDrr : SDI<opc, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src1, FR64:$src2),
1135                  !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1136                  [(set FR64:$dst, (OpNode FR64:$src1, FR64:$src2))]> {
1137     let isCommutable = Commutable;
1138   }
1139
1140   // Scalar operation, reg+mem.
1141   def SDrm : SDI<opc, MRMSrcMem, (outs FR64:$dst), (ins FR64:$src1, f64mem:$src2),
1142                  !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1143                  [(set FR64:$dst, (OpNode FR64:$src1, (load addr:$src2)))]>;
1144                  
1145   // Vector operation, reg+reg.
1146   def PDrr : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1147                !strconcat(OpcodeStr, "pd\t{$src2, $dst|$dst, $src2}"),
1148                [(set VR128:$dst, (v2f64 (OpNode VR128:$src1, VR128:$src2)))]> {
1149     let isCommutable = Commutable;
1150   }
1151
1152   // Vector operation, reg+mem.
1153   def PDrm : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
1154                  !strconcat(OpcodeStr, "pd\t{$src2, $dst|$dst, $src2}"),
1155                  [(set VR128:$dst, (OpNode VR128:$src1, (memopv2f64 addr:$src2)))]>;
1156
1157   // Intrinsic operation, reg+reg.
1158   def SDrr_Int : SDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1159                      !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1160                      [(set VR128:$dst, (F64Int VR128:$src1, VR128:$src2))]> {
1161     let isCommutable = Commutable;
1162   }
1163
1164   // Intrinsic operation, reg+mem.
1165   def SDrm_Int : SDI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, sdmem:$src2),
1166                      !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1167                      [(set VR128:$dst, (F64Int VR128:$src1,
1168                                                sse_load_f64:$src2))]>;
1169 }
1170 }
1171
1172 // Arithmetic instructions
1173 defm ADD : basic_sse2_fp_binop_rm<0x58, "add", fadd, int_x86_sse2_add_sd, 1>;
1174 defm MUL : basic_sse2_fp_binop_rm<0x59, "mul", fmul, int_x86_sse2_mul_sd, 1>;
1175 defm SUB : basic_sse2_fp_binop_rm<0x5C, "sub", fsub, int_x86_sse2_sub_sd>;
1176 defm DIV : basic_sse2_fp_binop_rm<0x5E, "div", fdiv, int_x86_sse2_div_sd>;
1177
1178 /// sse2_fp_binop_rm - Other SSE2 binops
1179 ///
1180 /// This multiclass is like basic_sse2_fp_binop_rm, with the addition of
1181 /// instructions for a full-vector intrinsic form.  Operations that map
1182 /// onto C operators don't use this form since they just use the plain
1183 /// vector form instead of having a separate vector intrinsic form.
1184 ///
1185 /// This provides a total of eight "instructions".
1186 ///
1187 let isTwoAddress = 1 in {
1188 multiclass sse2_fp_binop_rm<bits<8> opc, string OpcodeStr,
1189                             SDNode OpNode,
1190                             Intrinsic F64Int,
1191                             Intrinsic V2F64Int,
1192                             bit Commutable = 0> {
1193
1194   // Scalar operation, reg+reg.
1195   def SDrr : SDI<opc, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src1, FR64:$src2),
1196                  !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1197                  [(set FR64:$dst, (OpNode FR64:$src1, FR64:$src2))]> {
1198     let isCommutable = Commutable;
1199   }
1200
1201   // Scalar operation, reg+mem.
1202   def SDrm : SDI<opc, MRMSrcMem, (outs FR64:$dst), (ins FR64:$src1, f64mem:$src2),
1203                  !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1204                  [(set FR64:$dst, (OpNode FR64:$src1, (load addr:$src2)))]>;
1205                  
1206   // Vector operation, reg+reg.
1207   def PDrr : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1208                !strconcat(OpcodeStr, "pd\t{$src2, $dst|$dst, $src2}"),
1209                [(set VR128:$dst, (v2f64 (OpNode VR128:$src1, VR128:$src2)))]> {
1210     let isCommutable = Commutable;
1211   }
1212
1213   // Vector operation, reg+mem.
1214   def PDrm : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
1215                  !strconcat(OpcodeStr, "pd\t{$src2, $dst|$dst, $src2}"),
1216                  [(set VR128:$dst, (OpNode VR128:$src1, (memopv2f64 addr:$src2)))]>;
1217
1218   // Intrinsic operation, reg+reg.
1219   def SDrr_Int : SDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1220                      !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1221                      [(set VR128:$dst, (F64Int VR128:$src1, VR128:$src2))]> {
1222     let isCommutable = Commutable;
1223   }
1224
1225   // Intrinsic operation, reg+mem.
1226   def SDrm_Int : SDI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, sdmem:$src2),
1227                      !strconcat(OpcodeStr, "sd\t{$src2, $dst|$dst, $src2}"),
1228                      [(set VR128:$dst, (F64Int VR128:$src1,
1229                                                sse_load_f64:$src2))]>;
1230
1231   // Vector intrinsic operation, reg+reg.
1232   def PDrr_Int : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1233                      !strconcat(OpcodeStr, "pd\t{$src2, $dst|$dst, $src2}"),
1234                      [(set VR128:$dst, (V2F64Int VR128:$src1, VR128:$src2))]> {
1235     let isCommutable = Commutable;
1236   }
1237
1238   // Vector intrinsic operation, reg+mem.
1239   def PDrm_Int : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
1240                      !strconcat(OpcodeStr, "pd\t{$src2, $dst|$dst, $src2}"),
1241                      [(set VR128:$dst, (V2F64Int VR128:$src1, (load addr:$src2)))]>;
1242 }
1243 }
1244
1245 defm MAX : sse2_fp_binop_rm<0x5F, "max", X86fmax,
1246                             int_x86_sse2_max_sd, int_x86_sse2_max_pd>;
1247 defm MIN : sse2_fp_binop_rm<0x5D, "min", X86fmin,
1248                             int_x86_sse2_min_sd, int_x86_sse2_min_pd>;
1249
1250 //===----------------------------------------------------------------------===//
1251 // SSE packed FP Instructions
1252
1253 // Move Instructions
1254 def MOVAPDrr : PDI<0x28, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1255                    "movapd\t{$src, $dst|$dst, $src}", []>;
1256 let isLoad = 1, isReMaterializable = 1 in
1257 def MOVAPDrm : PDI<0x28, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1258                    "movapd\t{$src, $dst|$dst, $src}",
1259                    [(set VR128:$dst, (alignedloadv2f64 addr:$src))]>;
1260
1261 def MOVAPDmr : PDI<0x29, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1262                    "movapd\t{$src, $dst|$dst, $src}",
1263                    [(alignedstore (v2f64 VR128:$src), addr:$dst)]>;
1264
1265 def MOVUPDrr : PDI<0x10, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1266                    "movupd\t{$src, $dst|$dst, $src}", []>;
1267 let isLoad = 1 in
1268 def MOVUPDrm : PDI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1269                    "movupd\t{$src, $dst|$dst, $src}",
1270                    [(set VR128:$dst, (loadv2f64 addr:$src))]>;
1271 def MOVUPDmr : PDI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1272                    "movupd\t{$src, $dst|$dst, $src}",
1273                    [(store (v2f64 VR128:$src), addr:$dst)]>;
1274
1275 // Intrinsic forms of MOVUPD load and store
1276 def MOVUPDrm_Int : PDI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1277                        "movupd\t{$src, $dst|$dst, $src}",
1278                        [(set VR128:$dst, (int_x86_sse2_loadu_pd addr:$src))]>;
1279 def MOVUPDmr_Int : PDI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
1280                        "movupd\t{$src, $dst|$dst, $src}",
1281                        [(int_x86_sse2_storeu_pd addr:$dst, VR128:$src)]>;
1282
1283 let isTwoAddress = 1 in {
1284   let AddedComplexity = 20 in {
1285     def MOVLPDrm : PDI<0x12, MRMSrcMem,
1286                        (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
1287                        "movlpd\t{$src2, $dst|$dst, $src2}",
1288                        [(set VR128:$dst, 
1289                          (v2f64 (vector_shuffle VR128:$src1,
1290                                  (scalar_to_vector (loadf64 addr:$src2)),
1291                                  MOVLP_shuffle_mask)))]>;
1292     def MOVHPDrm : PDI<0x16, MRMSrcMem,
1293                        (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
1294                        "movhpd\t{$src2, $dst|$dst, $src2}",
1295                        [(set VR128:$dst, 
1296                          (v2f64 (vector_shuffle VR128:$src1,
1297                                  (scalar_to_vector (loadf64 addr:$src2)),
1298                                  MOVHP_shuffle_mask)))]>;
1299   } // AddedComplexity
1300 } // isTwoAddress
1301
1302 def MOVLPDmr : PDI<0x13, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
1303                    "movlpd\t{$src, $dst|$dst, $src}",
1304                    [(store (f64 (vector_extract (v2f64 VR128:$src),
1305                                  (iPTR 0))), addr:$dst)]>;
1306
1307 // v2f64 extract element 1 is always custom lowered to unpack high to low
1308 // and extract element 0 so the non-store version isn't too horrible.
1309 def MOVHPDmr : PDI<0x17, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
1310                    "movhpd\t{$src, $dst|$dst, $src}",
1311                    [(store (f64 (vector_extract
1312                                  (v2f64 (vector_shuffle VR128:$src, (undef),
1313                                          UNPCKH_shuffle_mask)), (iPTR 0))),
1314                      addr:$dst)]>;
1315
1316 // SSE2 instructions without OpSize prefix
1317 def Int_CVTDQ2PSrr : I<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1318                        "cvtdq2ps\t{$src, $dst|$dst, $src}",
1319                        [(set VR128:$dst, (int_x86_sse2_cvtdq2ps VR128:$src))]>,
1320                      TB, Requires<[HasSSE2]>;
1321 def Int_CVTDQ2PSrm : I<0x5B, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
1322                        "cvtdq2ps\t{$src, $dst|$dst, $src}",
1323                        [(set VR128:$dst, (int_x86_sse2_cvtdq2ps
1324                                          (bitconvert (memopv2i64 addr:$src))))]>,
1325                      TB, Requires<[HasSSE2]>;
1326
1327 // SSE2 instructions with XS prefix
1328 def Int_CVTDQ2PDrr : I<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1329                        "cvtdq2pd\t{$src, $dst|$dst, $src}",
1330                        [(set VR128:$dst, (int_x86_sse2_cvtdq2pd VR128:$src))]>,
1331                      XS, Requires<[HasSSE2]>;
1332 def Int_CVTDQ2PDrm : I<0xE6, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
1333                        "cvtdq2pd\t{$src, $dst|$dst, $src}",
1334                        [(set VR128:$dst, (int_x86_sse2_cvtdq2pd
1335                                           (bitconvert (memopv2i64 addr:$src))))]>,
1336                      XS, Requires<[HasSSE2]>;
1337
1338 def Int_CVTPS2DQrr : PDI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1339                          "cvtps2dq\t{$src, $dst|$dst, $src}",
1340                          [(set VR128:$dst, (int_x86_sse2_cvtps2dq VR128:$src))]>;
1341 def Int_CVTPS2DQrm : PDI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1342                          "cvtps2dq\t{$src, $dst|$dst, $src}",
1343                          [(set VR128:$dst, (int_x86_sse2_cvtps2dq
1344                                             (load addr:$src)))]>;
1345 // SSE2 packed instructions with XS prefix
1346 def Int_CVTTPS2DQrr : I<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1347                         "cvttps2dq\t{$src, $dst|$dst, $src}",
1348                         [(set VR128:$dst, (int_x86_sse2_cvttps2dq VR128:$src))]>,
1349                       XS, Requires<[HasSSE2]>;
1350 def Int_CVTTPS2DQrm : I<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1351                         "cvttps2dq\t{$src, $dst|$dst, $src}",
1352                         [(set VR128:$dst, (int_x86_sse2_cvttps2dq
1353                                            (load addr:$src)))]>,
1354                       XS, Requires<[HasSSE2]>;
1355
1356 // SSE2 packed instructions with XD prefix
1357 def Int_CVTPD2DQrr : I<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1358                        "cvtpd2dq\t{$src, $dst|$dst, $src}",
1359                        [(set VR128:$dst, (int_x86_sse2_cvtpd2dq VR128:$src))]>,
1360                      XD, Requires<[HasSSE2]>;
1361 def Int_CVTPD2DQrm : I<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1362                        "cvtpd2dq\t{$src, $dst|$dst, $src}",
1363                        [(set VR128:$dst, (int_x86_sse2_cvtpd2dq
1364                                           (load addr:$src)))]>,
1365                      XD, Requires<[HasSSE2]>;
1366
1367 def Int_CVTTPD2DQrr : PDI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1368                           "cvttpd2dq\t{$src, $dst|$dst, $src}",
1369                        [(set VR128:$dst, (int_x86_sse2_cvttpd2dq VR128:$src))]>;
1370 def Int_CVTTPD2DQrm : PDI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1371                           "cvttpd2dq\t{$src, $dst|$dst, $src}",
1372                           [(set VR128:$dst, (int_x86_sse2_cvttpd2dq
1373                                              (load addr:$src)))]>;
1374
1375 // SSE2 instructions without OpSize prefix
1376 def Int_CVTPS2PDrr : I<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1377                        "cvtps2pd\t{$src, $dst|$dst, $src}",
1378                        [(set VR128:$dst, (int_x86_sse2_cvtps2pd VR128:$src))]>,
1379                      TB, Requires<[HasSSE2]>;
1380 def Int_CVTPS2PDrm : I<0x5A, MRMSrcReg, (outs VR128:$dst), (ins f64mem:$src),
1381                        "cvtps2pd\t{$src, $dst|$dst, $src}",
1382                        [(set VR128:$dst, (int_x86_sse2_cvtps2pd
1383                                           (load addr:$src)))]>,
1384                      TB, Requires<[HasSSE2]>;
1385
1386 def Int_CVTPD2PSrr : PDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1387                          "cvtpd2ps\t{$src, $dst|$dst, $src}",
1388                         [(set VR128:$dst, (int_x86_sse2_cvtpd2ps VR128:$src))]>;
1389 def Int_CVTPD2PSrm : PDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins f128mem:$src),
1390                          "cvtpd2ps\t{$src, $dst|$dst, $src}",
1391                          [(set VR128:$dst, (int_x86_sse2_cvtpd2ps
1392                                             (load addr:$src)))]>;
1393
1394 // Match intrinsics which expect XMM operand(s).
1395 // Aliases for intrinsics
1396 let isTwoAddress = 1 in {
1397 def Int_CVTSI2SDrr: SDI<0x2A, MRMSrcReg,
1398                         (outs VR128:$dst), (ins VR128:$src1, GR32:$src2),
1399                         "cvtsi2sd\t{$src2, $dst|$dst, $src2}",
1400                         [(set VR128:$dst, (int_x86_sse2_cvtsi2sd VR128:$src1,
1401                                            GR32:$src2))]>;
1402 def Int_CVTSI2SDrm: SDI<0x2A, MRMSrcMem,
1403                         (outs VR128:$dst), (ins VR128:$src1, i32mem:$src2),
1404                         "cvtsi2sd\t{$src2, $dst|$dst, $src2}",
1405                         [(set VR128:$dst, (int_x86_sse2_cvtsi2sd VR128:$src1,
1406                                            (loadi32 addr:$src2)))]>;
1407 def Int_CVTSD2SSrr: SDI<0x5A, MRMSrcReg,
1408                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1409                    "cvtsd2ss\t{$src2, $dst|$dst, $src2}",
1410                    [(set VR128:$dst, (int_x86_sse2_cvtsd2ss VR128:$src1,
1411                                       VR128:$src2))]>;
1412 def Int_CVTSD2SSrm: SDI<0x5A, MRMSrcMem,
1413                         (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2), 
1414                    "cvtsd2ss\t{$src2, $dst|$dst, $src2}",
1415                    [(set VR128:$dst, (int_x86_sse2_cvtsd2ss VR128:$src1,
1416                                       (load addr:$src2)))]>;
1417 def Int_CVTSS2SDrr: I<0x5A, MRMSrcReg,
1418                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1419                     "cvtss2sd\t{$src2, $dst|$dst, $src2}",
1420                     [(set VR128:$dst, (int_x86_sse2_cvtss2sd VR128:$src1,
1421                                        VR128:$src2))]>, XS,
1422                     Requires<[HasSSE2]>;
1423 def Int_CVTSS2SDrm: I<0x5A, MRMSrcMem,
1424                       (outs VR128:$dst), (ins VR128:$src1, f32mem:$src2),
1425                     "cvtss2sd\t{$src2, $dst|$dst, $src2}",
1426                     [(set VR128:$dst, (int_x86_sse2_cvtss2sd VR128:$src1,
1427                                        (load addr:$src2)))]>, XS,
1428                     Requires<[HasSSE2]>;
1429 }
1430
1431 // Arithmetic
1432
1433 /// sse2_fp_unop_rm - SSE2 unops come in both scalar and vector forms.
1434 ///
1435 /// In addition, we also have a special variant of the scalar form here to
1436 /// represent the associated intrinsic operation.  This form is unlike the
1437 /// plain scalar form, in that it takes an entire vector (instead of a
1438 /// scalar) and leaves the top elements undefined.
1439 ///
1440 /// And, we have a special variant form for a full-vector intrinsic form.
1441 ///
1442 /// These four forms can each have a reg or a mem operand, so there are a
1443 /// total of eight "instructions".
1444 ///
1445 multiclass sse2_fp_unop_rm<bits<8> opc, string OpcodeStr,
1446                            SDNode OpNode,
1447                            Intrinsic F64Int,
1448                            Intrinsic V2F64Int,
1449                            bit Commutable = 0> {
1450   // Scalar operation, reg.
1451   def SDr : SDI<opc, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src),
1452                 !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1453                 [(set FR64:$dst, (OpNode FR64:$src))]> {
1454     let isCommutable = Commutable;
1455   }
1456
1457   // Scalar operation, mem.
1458   def SDm : SDI<opc, MRMSrcMem, (outs FR64:$dst), (ins f64mem:$src),
1459                 !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1460                 [(set FR64:$dst, (OpNode (load addr:$src)))]>;
1461                  
1462   // Vector operation, reg.
1463   def PDr : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1464               !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1465               [(set VR128:$dst, (v2f64 (OpNode VR128:$src)))]> {
1466     let isCommutable = Commutable;
1467   }
1468
1469   // Vector operation, mem.
1470   def PDm : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1471                 !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1472                 [(set VR128:$dst, (OpNode (memopv2f64 addr:$src)))]>;
1473
1474   // Intrinsic operation, reg.
1475   def SDr_Int : SDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1476                     !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1477                     [(set VR128:$dst, (F64Int VR128:$src))]> {
1478     let isCommutable = Commutable;
1479   }
1480
1481   // Intrinsic operation, mem.
1482   def SDm_Int : SDI<opc, MRMSrcMem, (outs VR128:$dst), (ins sdmem:$src),
1483                     !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1484                     [(set VR128:$dst, (F64Int sse_load_f64:$src))]>;
1485
1486   // Vector intrinsic operation, reg
1487   def PDr_Int : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1488                     !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1489                     [(set VR128:$dst, (V2F64Int VR128:$src))]> {
1490     let isCommutable = Commutable;
1491   }
1492
1493   // Vector intrinsic operation, mem
1494   def PDm_Int : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1495                     !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1496                     [(set VR128:$dst, (V2F64Int (load addr:$src)))]>;
1497 }
1498
1499 // Square root.
1500 defm SQRT  : sse2_fp_unop_rm<0x51, "sqrt",  fsqrt,
1501                              int_x86_sse2_sqrt_sd, int_x86_sse2_sqrt_pd>;
1502
1503 // There is no f64 version of the reciprocal approximation instructions.
1504
1505 // Logical
1506 let isTwoAddress = 1 in {
1507   let isCommutable = 1 in {
1508     def ANDPDrr : PDI<0x54, MRMSrcReg,
1509                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1510                       "andpd\t{$src2, $dst|$dst, $src2}",
1511                       [(set VR128:$dst,
1512                         (and (bc_v2i64 (v2f64 VR128:$src1)),
1513                          (bc_v2i64 (v2f64 VR128:$src2))))]>;
1514     def ORPDrr  : PDI<0x56, MRMSrcReg,
1515                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1516                       "orpd\t{$src2, $dst|$dst, $src2}",
1517                       [(set VR128:$dst,
1518                         (or (bc_v2i64 (v2f64 VR128:$src1)),
1519                          (bc_v2i64 (v2f64 VR128:$src2))))]>;
1520     def XORPDrr : PDI<0x57, MRMSrcReg,
1521                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1522                       "xorpd\t{$src2, $dst|$dst, $src2}",
1523                       [(set VR128:$dst,
1524                         (xor (bc_v2i64 (v2f64 VR128:$src1)),
1525                          (bc_v2i64 (v2f64 VR128:$src2))))]>;
1526   }
1527
1528   def ANDPDrm : PDI<0x54, MRMSrcMem,
1529                     (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
1530                     "andpd\t{$src2, $dst|$dst, $src2}",
1531                     [(set VR128:$dst,
1532                       (and (bc_v2i64 (v2f64 VR128:$src1)),
1533                        (memopv2i64 addr:$src2)))]>;
1534   def ORPDrm  : PDI<0x56, MRMSrcMem,
1535                     (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
1536                     "orpd\t{$src2, $dst|$dst, $src2}",
1537                     [(set VR128:$dst,
1538                       (or (bc_v2i64 (v2f64 VR128:$src1)),
1539                        (memopv2i64 addr:$src2)))]>;
1540   def XORPDrm : PDI<0x57, MRMSrcMem,
1541                     (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
1542                     "xorpd\t{$src2, $dst|$dst, $src2}",
1543                     [(set VR128:$dst,
1544                       (xor (bc_v2i64 (v2f64 VR128:$src1)),
1545                        (memopv2i64 addr:$src2)))]>;
1546   def ANDNPDrr : PDI<0x55, MRMSrcReg,
1547                      (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1548                      "andnpd\t{$src2, $dst|$dst, $src2}",
1549                      [(set VR128:$dst,
1550                        (and (vnot (bc_v2i64 (v2f64 VR128:$src1))),
1551                         (bc_v2i64 (v2f64 VR128:$src2))))]>;
1552   def ANDNPDrm : PDI<0x55, MRMSrcMem,
1553                      (outs VR128:$dst), (ins VR128:$src1,f128mem:$src2),
1554                      "andnpd\t{$src2, $dst|$dst, $src2}",
1555                      [(set VR128:$dst,
1556                        (and (vnot (bc_v2i64 (v2f64 VR128:$src1))),
1557                         (memopv2i64 addr:$src2)))]>;
1558 }
1559
1560 let isTwoAddress = 1 in {
1561   def CMPPDrri : PDIi8<0xC2, MRMSrcReg, 
1562                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src, SSECC:$cc),
1563                       "cmp${cc}pd\t{$src, $dst|$dst, $src}",
1564                       [(set VR128:$dst, (int_x86_sse2_cmp_pd VR128:$src1,
1565                                          VR128:$src, imm:$cc))]>;
1566   def CMPPDrmi : PDIi8<0xC2, MRMSrcMem, 
1567                       (outs VR128:$dst), (ins VR128:$src1, f128mem:$src, SSECC:$cc),
1568                       "cmp${cc}pd\t{$src, $dst|$dst, $src}",
1569                       [(set VR128:$dst, (int_x86_sse2_cmp_pd VR128:$src1,
1570                                          (load addr:$src), imm:$cc))]>;
1571 }
1572
1573 // Shuffle and unpack instructions
1574 let isTwoAddress = 1 in {
1575   def SHUFPDrri : PDIi8<0xC6, MRMSrcReg, 
1576                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2, i8imm:$src3),
1577                         "shufpd\t{$src3, $src2, $dst|$dst, $src2, $src3}",
1578                         [(set VR128:$dst, (v2f64 (vector_shuffle
1579                                                   VR128:$src1, VR128:$src2,
1580                                                   SHUFP_shuffle_mask:$src3)))]>;
1581   def SHUFPDrmi : PDIi8<0xC6, MRMSrcMem, 
1582                         (outs VR128:$dst), (ins VR128:$src1,
1583                          f128mem:$src2, i8imm:$src3),
1584                         "shufpd\t{$src3, $src2, $dst|$dst, $src2, $src3}",
1585                         [(set VR128:$dst,
1586                           (v2f64 (vector_shuffle
1587                                   VR128:$src1, (memopv2f64 addr:$src2),
1588                                   SHUFP_shuffle_mask:$src3)))]>;
1589
1590   let AddedComplexity = 10 in {
1591     def UNPCKHPDrr : PDI<0x15, MRMSrcReg, 
1592                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1593                          "unpckhpd\t{$src2, $dst|$dst, $src2}",
1594                          [(set VR128:$dst,
1595                            (v2f64 (vector_shuffle
1596                                    VR128:$src1, VR128:$src2,
1597                                    UNPCKH_shuffle_mask)))]>;
1598     def UNPCKHPDrm : PDI<0x15, MRMSrcMem, 
1599                          (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
1600                          "unpckhpd\t{$src2, $dst|$dst, $src2}",
1601                          [(set VR128:$dst,
1602                            (v2f64 (vector_shuffle
1603                                    VR128:$src1, (memopv2f64 addr:$src2),
1604                                    UNPCKH_shuffle_mask)))]>;
1605
1606     def UNPCKLPDrr : PDI<0x14, MRMSrcReg, 
1607                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1608                          "unpcklpd\t{$src2, $dst|$dst, $src2}",
1609                          [(set VR128:$dst,
1610                            (v2f64 (vector_shuffle
1611                                    VR128:$src1, VR128:$src2,
1612                                    UNPCKL_shuffle_mask)))]>;
1613     def UNPCKLPDrm : PDI<0x14, MRMSrcMem, 
1614                          (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
1615                          "unpcklpd\t{$src2, $dst|$dst, $src2}",
1616                          [(set VR128:$dst,
1617                            (v2f64 (vector_shuffle
1618                                    VR128:$src1, (memopv2f64 addr:$src2),
1619                                    UNPCKL_shuffle_mask)))]>;
1620   } // AddedComplexity
1621 } // isTwoAddress
1622
1623
1624 //===----------------------------------------------------------------------===//
1625 // SSE integer instructions
1626
1627 // Move Instructions
1628 def MOVDQArr : PDI<0x6F, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1629                    "movdqa\t{$src, $dst|$dst, $src}", []>;
1630 let isLoad = 1 in
1631 def MOVDQArm : PDI<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
1632                    "movdqa\t{$src, $dst|$dst, $src}",
1633                    [/*(set VR128:$dst, (alignedloadv2i64 addr:$src))*/]>;
1634 def MOVDQAmr : PDI<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
1635                    "movdqa\t{$src, $dst|$dst, $src}",
1636                    [/*(alignedstore (v2i64 VR128:$src), addr:$dst)*/]>;
1637 let isLoad = 1 in
1638 def MOVDQUrm :   I<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
1639                    "movdqu\t{$src, $dst|$dst, $src}",
1640                    [/*(set VR128:$dst, (loadv2i64 addr:$src))*/]>,
1641                  XS, Requires<[HasSSE2]>;
1642 def MOVDQUmr :   I<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
1643                    "movdqu\t{$src, $dst|$dst, $src}",
1644                    [/*(store (v2i64 VR128:$src), addr:$dst)*/]>,
1645                  XS, Requires<[HasSSE2]>;
1646
1647 // Intrinsic forms of MOVDQU load and store
1648 let isLoad = 1 in
1649 def MOVDQUrm_Int :   I<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
1650                        "movdqu\t{$src, $dst|$dst, $src}",
1651                        [(set VR128:$dst, (int_x86_sse2_loadu_dq addr:$src))]>,
1652                  XS, Requires<[HasSSE2]>;
1653 def MOVDQUmr_Int :   I<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
1654                        "movdqu\t{$src, $dst|$dst, $src}",
1655                        [(int_x86_sse2_storeu_dq addr:$dst, VR128:$src)]>,
1656                      XS, Requires<[HasSSE2]>;
1657
1658 let isTwoAddress = 1 in {
1659
1660 multiclass PDI_binop_rm_int<bits<8> opc, string OpcodeStr, Intrinsic IntId,
1661                             bit Commutable = 0> {
1662   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1663                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1664                [(set VR128:$dst, (IntId VR128:$src1, VR128:$src2))]> {
1665     let isCommutable = Commutable;
1666   }
1667   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1668                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1669                [(set VR128:$dst, (IntId VR128:$src1,
1670                                         (bitconvert (memopv2i64 addr:$src2))))]>;
1671 }
1672
1673 multiclass PDI_binop_rmi_int<bits<8> opc, bits<8> opc2, Format ImmForm,
1674                              string OpcodeStr, Intrinsic IntId> {
1675   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1676                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1677                [(set VR128:$dst, (IntId VR128:$src1, VR128:$src2))]>;
1678   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1679                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1680                [(set VR128:$dst, (IntId VR128:$src1,
1681                                         (bitconvert (memopv2i64 addr:$src2))))]>;
1682   def ri : PDIi8<opc2, ImmForm, (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
1683                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1684                [(set VR128:$dst, (IntId VR128:$src1,
1685                                         (scalar_to_vector (i32 imm:$src2))))]>;
1686 }
1687
1688
1689 /// PDI_binop_rm - Simple SSE2 binary operator.
1690 multiclass PDI_binop_rm<bits<8> opc, string OpcodeStr, SDNode OpNode,
1691                         ValueType OpVT, bit Commutable = 0> {
1692   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1693                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1694                [(set VR128:$dst, (OpVT (OpNode VR128:$src1, VR128:$src2)))]> {
1695     let isCommutable = Commutable;
1696   }
1697   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1698                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1699                [(set VR128:$dst, (OpVT (OpNode VR128:$src1,
1700                                        (bitconvert (memopv2i64 addr:$src2)))))]>;
1701 }
1702
1703 /// PDI_binop_rm_v2i64 - Simple SSE2 binary operator whose type is v2i64.
1704 ///
1705 /// FIXME: we could eliminate this and use PDI_binop_rm instead if tblgen knew
1706 /// to collapse (bitconvert VT to VT) into its operand.
1707 ///
1708 multiclass PDI_binop_rm_v2i64<bits<8> opc, string OpcodeStr, SDNode OpNode,
1709                               bit Commutable = 0> {
1710   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1711                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1712                [(set VR128:$dst, (v2i64 (OpNode VR128:$src1, VR128:$src2)))]> {
1713     let isCommutable = Commutable;
1714   }
1715   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1716                !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
1717                [(set VR128:$dst, (OpNode VR128:$src1,(memopv2i64 addr:$src2)))]>;
1718 }
1719
1720 } // isTwoAddress
1721
1722 // 128-bit Integer Arithmetic
1723
1724 defm PADDB : PDI_binop_rm<0xFC, "paddb", add, v16i8, 1>;
1725 defm PADDW : PDI_binop_rm<0xFD, "paddw", add, v8i16, 1>;
1726 defm PADDD : PDI_binop_rm<0xFE, "paddd", add, v4i32, 1>;
1727 defm PADDQ : PDI_binop_rm_v2i64<0xD4, "paddq", add, 1>;
1728
1729 defm PADDSB  : PDI_binop_rm_int<0xEC, "paddsb" , int_x86_sse2_padds_b, 1>;
1730 defm PADDSW  : PDI_binop_rm_int<0xED, "paddsw" , int_x86_sse2_padds_w, 1>;
1731 defm PADDUSB : PDI_binop_rm_int<0xDC, "paddusb", int_x86_sse2_paddus_b, 1>;
1732 defm PADDUSW : PDI_binop_rm_int<0xDD, "paddusw", int_x86_sse2_paddus_w, 1>;
1733
1734 defm PSUBB : PDI_binop_rm<0xF8, "psubb", sub, v16i8>;
1735 defm PSUBW : PDI_binop_rm<0xF9, "psubw", sub, v8i16>;
1736 defm PSUBD : PDI_binop_rm<0xFA, "psubd", sub, v4i32>;
1737 defm PSUBQ : PDI_binop_rm_v2i64<0xFB, "psubq", sub>;
1738
1739 defm PSUBSB  : PDI_binop_rm_int<0xE8, "psubsb" , int_x86_sse2_psubs_b>;
1740 defm PSUBSW  : PDI_binop_rm_int<0xE9, "psubsw" , int_x86_sse2_psubs_w>;
1741 defm PSUBUSB : PDI_binop_rm_int<0xD8, "psubusb", int_x86_sse2_psubus_b>;
1742 defm PSUBUSW : PDI_binop_rm_int<0xD9, "psubusw", int_x86_sse2_psubus_w>;
1743
1744 defm PMULLW : PDI_binop_rm<0xD5, "pmullw", mul, v8i16, 1>;
1745
1746 defm PMULHUW : PDI_binop_rm_int<0xE4, "pmulhuw", int_x86_sse2_pmulhu_w, 1>;
1747 defm PMULHW  : PDI_binop_rm_int<0xE5, "pmulhw" , int_x86_sse2_pmulh_w , 1>;
1748 defm PMULUDQ : PDI_binop_rm_int<0xF4, "pmuludq", int_x86_sse2_pmulu_dq, 1>;
1749
1750 defm PMADDWD : PDI_binop_rm_int<0xF5, "pmaddwd", int_x86_sse2_pmadd_wd, 1>;
1751
1752 defm PAVGB  : PDI_binop_rm_int<0xE0, "pavgb", int_x86_sse2_pavg_b, 1>;
1753 defm PAVGW  : PDI_binop_rm_int<0xE3, "pavgw", int_x86_sse2_pavg_w, 1>;
1754
1755
1756 defm PMINUB : PDI_binop_rm_int<0xDA, "pminub", int_x86_sse2_pminu_b, 1>;
1757 defm PMINSW : PDI_binop_rm_int<0xEA, "pminsw", int_x86_sse2_pmins_w, 1>;
1758 defm PMAXUB : PDI_binop_rm_int<0xDE, "pmaxub", int_x86_sse2_pmaxu_b, 1>;
1759 defm PMAXSW : PDI_binop_rm_int<0xEE, "pmaxsw", int_x86_sse2_pmaxs_w, 1>;
1760 defm PSADBW : PDI_binop_rm_int<0xE0, "psadbw", int_x86_sse2_psad_bw, 1>;
1761
1762
1763 defm PSLLW : PDI_binop_rmi_int<0xF1, 0x71, MRM6r, "psllw", int_x86_sse2_psll_w>;
1764 defm PSLLD : PDI_binop_rmi_int<0xF2, 0x72, MRM6r, "pslld", int_x86_sse2_psll_d>;
1765 defm PSLLQ : PDI_binop_rmi_int<0xF3, 0x73, MRM6r, "psllq", int_x86_sse2_psll_q>;
1766
1767 defm PSRLW : PDI_binop_rmi_int<0xD1, 0x71, MRM2r, "psrlw", int_x86_sse2_psrl_w>;
1768 defm PSRLD : PDI_binop_rmi_int<0xD2, 0x72, MRM2r, "psrld", int_x86_sse2_psrl_d>;
1769 defm PSRLQ : PDI_binop_rmi_int<0xD3, 0x73, MRM2r, "psrlq", int_x86_sse2_psrl_q>;
1770
1771 defm PSRAW : PDI_binop_rmi_int<0xE1, 0x71, MRM4r, "psraw", int_x86_sse2_psra_w>;
1772 defm PSRAD : PDI_binop_rmi_int<0xE2, 0x72, MRM4r, "psrad", int_x86_sse2_psra_d>;
1773 // PSRAQ doesn't exist in SSE[1-3].
1774
1775 // 128-bit logical shifts.
1776 let isTwoAddress = 1 in {
1777   def PSLLDQri : PDIi8<0x73, MRM7r,
1778                        (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
1779                        "pslldq\t{$src2, $dst|$dst, $src2}", []>;
1780   def PSRLDQri : PDIi8<0x73, MRM3r,
1781                        (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
1782                        "psrldq\t{$src2, $dst|$dst, $src2}", []>;
1783   // PSRADQri doesn't exist in SSE[1-3].
1784 }
1785
1786 let Predicates = [HasSSE2] in {
1787   def : Pat<(int_x86_sse2_psll_dq VR128:$src1, imm:$src2),
1788             (v2i64 (PSLLDQri VR128:$src1, (PSxLDQ_imm imm:$src2)))>;
1789   def : Pat<(int_x86_sse2_psrl_dq VR128:$src1, imm:$src2),
1790             (v2i64 (PSRLDQri VR128:$src1, (PSxLDQ_imm imm:$src2)))>;
1791   def : Pat<(v2f64 (X86fsrl VR128:$src1, i32immSExt8:$src2)),
1792             (v2f64 (PSRLDQri VR128:$src1, (PSxLDQ_imm imm:$src2)))>;
1793 }
1794
1795 // Logical
1796 defm PAND : PDI_binop_rm_v2i64<0xDB, "pand", and, 1>;
1797 defm POR  : PDI_binop_rm_v2i64<0xEB, "por" , or , 1>;
1798 defm PXOR : PDI_binop_rm_v2i64<0xEF, "pxor", xor, 1>;
1799
1800 let isTwoAddress = 1 in {
1801   def PANDNrr : PDI<0xDF, MRMSrcReg,
1802                     (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1803                     "pandn\t{$src2, $dst|$dst, $src2}",
1804                     [(set VR128:$dst, (v2i64 (and (vnot VR128:$src1),
1805                                               VR128:$src2)))]>;
1806
1807   def PANDNrm : PDI<0xDF, MRMSrcMem,
1808                     (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1809                     "pandn\t{$src2, $dst|$dst, $src2}",
1810                     [(set VR128:$dst, (v2i64 (and (vnot VR128:$src1),
1811                                               (memopv2i64 addr:$src2))))]>;
1812 }
1813
1814 // SSE2 Integer comparison
1815 defm PCMPEQB  : PDI_binop_rm_int<0x74, "pcmpeqb", int_x86_sse2_pcmpeq_b>;
1816 defm PCMPEQW  : PDI_binop_rm_int<0x75, "pcmpeqw", int_x86_sse2_pcmpeq_w>;
1817 defm PCMPEQD  : PDI_binop_rm_int<0x76, "pcmpeqd", int_x86_sse2_pcmpeq_d>;
1818 defm PCMPGTB  : PDI_binop_rm_int<0x64, "pcmpgtb", int_x86_sse2_pcmpgt_b>;
1819 defm PCMPGTW  : PDI_binop_rm_int<0x65, "pcmpgtw", int_x86_sse2_pcmpgt_w>;
1820 defm PCMPGTD  : PDI_binop_rm_int<0x66, "pcmpgtd", int_x86_sse2_pcmpgt_d>;
1821
1822 // Pack instructions
1823 defm PACKSSWB : PDI_binop_rm_int<0x63, "packsswb", int_x86_sse2_packsswb_128>;
1824 defm PACKSSDW : PDI_binop_rm_int<0x6B, "packssdw", int_x86_sse2_packssdw_128>;
1825 defm PACKUSWB : PDI_binop_rm_int<0x67, "packuswb", int_x86_sse2_packuswb_128>;
1826
1827 // Shuffle and unpack instructions
1828 def PSHUFDri : PDIi8<0x70, MRMSrcReg,
1829                      (outs VR128:$dst), (ins VR128:$src1, i8imm:$src2),
1830                      "pshufd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1831                      [(set VR128:$dst, (v4i32 (vector_shuffle
1832                                                VR128:$src1, (undef),
1833                                                PSHUFD_shuffle_mask:$src2)))]>;
1834 def PSHUFDmi : PDIi8<0x70, MRMSrcMem,
1835                      (outs VR128:$dst), (ins i128mem:$src1, i8imm:$src2),
1836                      "pshufd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1837                      [(set VR128:$dst, (v4i32 (vector_shuffle
1838                                                (bc_v4i32(memopv2i64 addr:$src1)),
1839                                                (undef),
1840                                                PSHUFD_shuffle_mask:$src2)))]>;
1841
1842 // SSE2 with ImmT == Imm8 and XS prefix.
1843 def PSHUFHWri : Ii8<0x70, MRMSrcReg,
1844                     (outs VR128:$dst), (ins VR128:$src1, i8imm:$src2),
1845                     "pshufhw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1846                     [(set VR128:$dst, (v8i16 (vector_shuffle
1847                                               VR128:$src1, (undef),
1848                                               PSHUFHW_shuffle_mask:$src2)))]>,
1849                 XS, Requires<[HasSSE2]>;
1850 def PSHUFHWmi : Ii8<0x70, MRMSrcMem,
1851                     (outs VR128:$dst), (ins i128mem:$src1, i8imm:$src2),
1852                     "pshufhw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1853                     [(set VR128:$dst, (v8i16 (vector_shuffle
1854                                               (bc_v8i16 (memopv2i64 addr:$src1)),
1855                                               (undef),
1856                                               PSHUFHW_shuffle_mask:$src2)))]>,
1857                 XS, Requires<[HasSSE2]>;
1858
1859 // SSE2 with ImmT == Imm8 and XD prefix.
1860 def PSHUFLWri : Ii8<0x70, MRMSrcReg,
1861                     (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
1862                     "pshuflw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1863                     [(set VR128:$dst, (v8i16 (vector_shuffle
1864                                               VR128:$src1, (undef),
1865                                               PSHUFLW_shuffle_mask:$src2)))]>,
1866                 XD, Requires<[HasSSE2]>;
1867 def PSHUFLWmi : Ii8<0x70, MRMSrcMem,
1868                     (outs VR128:$dst), (ins i128mem:$src1, i32i8imm:$src2),
1869                     "pshuflw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1870                     [(set VR128:$dst, (v8i16 (vector_shuffle
1871                                               (bc_v8i16 (memopv2i64 addr:$src1)),
1872                                               (undef),
1873                                               PSHUFLW_shuffle_mask:$src2)))]>,
1874                 XD, Requires<[HasSSE2]>;
1875
1876
1877 let isTwoAddress = 1 in {
1878   def PUNPCKLBWrr : PDI<0x60, MRMSrcReg, 
1879                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1880                         "punpcklbw\t{$src2, $dst|$dst, $src2}",
1881                         [(set VR128:$dst,
1882                           (v16i8 (vector_shuffle VR128:$src1, VR128:$src2,
1883                                   UNPCKL_shuffle_mask)))]>;
1884   def PUNPCKLBWrm : PDI<0x60, MRMSrcMem, 
1885                         (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1886                         "punpcklbw\t{$src2, $dst|$dst, $src2}",
1887                         [(set VR128:$dst,
1888                           (v16i8 (vector_shuffle VR128:$src1,
1889                                   (bc_v16i8 (memopv2i64 addr:$src2)),
1890                                   UNPCKL_shuffle_mask)))]>;
1891   def PUNPCKLWDrr : PDI<0x61, MRMSrcReg, 
1892                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1893                         "punpcklwd\t{$src2, $dst|$dst, $src2}",
1894                         [(set VR128:$dst,
1895                           (v8i16 (vector_shuffle VR128:$src1, VR128:$src2,
1896                                   UNPCKL_shuffle_mask)))]>;
1897   def PUNPCKLWDrm : PDI<0x61, MRMSrcMem, 
1898                         (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1899                         "punpcklwd\t{$src2, $dst|$dst, $src2}",
1900                         [(set VR128:$dst,
1901                           (v8i16 (vector_shuffle VR128:$src1,
1902                                   (bc_v8i16 (memopv2i64 addr:$src2)),
1903                                   UNPCKL_shuffle_mask)))]>;
1904   def PUNPCKLDQrr : PDI<0x62, MRMSrcReg, 
1905                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1906                         "punpckldq\t{$src2, $dst|$dst, $src2}",
1907                         [(set VR128:$dst,
1908                           (v4i32 (vector_shuffle VR128:$src1, VR128:$src2,
1909                                   UNPCKL_shuffle_mask)))]>;
1910   def PUNPCKLDQrm : PDI<0x62, MRMSrcMem, 
1911                         (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1912                         "punpckldq\t{$src2, $dst|$dst, $src2}",
1913                         [(set VR128:$dst,
1914                           (v4i32 (vector_shuffle VR128:$src1,
1915                                   (bc_v4i32 (memopv2i64 addr:$src2)),
1916                                   UNPCKL_shuffle_mask)))]>;
1917   def PUNPCKLQDQrr : PDI<0x6C, MRMSrcReg, 
1918                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1919                          "punpcklqdq\t{$src2, $dst|$dst, $src2}",
1920                         [(set VR128:$dst,
1921                           (v2i64 (vector_shuffle VR128:$src1, VR128:$src2,
1922                                   UNPCKL_shuffle_mask)))]>;
1923   def PUNPCKLQDQrm : PDI<0x6C, MRMSrcMem, 
1924                          (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1925                          "punpcklqdq\t{$src2, $dst|$dst, $src2}",
1926                         [(set VR128:$dst,
1927                           (v2i64 (vector_shuffle VR128:$src1,
1928                                   (memopv2i64 addr:$src2),
1929                                   UNPCKL_shuffle_mask)))]>;
1930   
1931   def PUNPCKHBWrr : PDI<0x68, MRMSrcReg, 
1932                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1933                         "punpckhbw\t{$src2, $dst|$dst, $src2}",
1934                         [(set VR128:$dst,
1935                           (v16i8 (vector_shuffle VR128:$src1, VR128:$src2,
1936                                   UNPCKH_shuffle_mask)))]>;
1937   def PUNPCKHBWrm : PDI<0x68, MRMSrcMem, 
1938                         (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1939                         "punpckhbw\t{$src2, $dst|$dst, $src2}",
1940                         [(set VR128:$dst,
1941                           (v16i8 (vector_shuffle VR128:$src1,
1942                                   (bc_v16i8 (memopv2i64 addr:$src2)),
1943                                   UNPCKH_shuffle_mask)))]>;
1944   def PUNPCKHWDrr : PDI<0x69, MRMSrcReg, 
1945                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1946                         "punpckhwd\t{$src2, $dst|$dst, $src2}",
1947                         [(set VR128:$dst,
1948                           (v8i16 (vector_shuffle VR128:$src1, VR128:$src2,
1949                                   UNPCKH_shuffle_mask)))]>;
1950   def PUNPCKHWDrm : PDI<0x69, MRMSrcMem, 
1951                         (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1952                         "punpckhwd\t{$src2, $dst|$dst, $src2}",
1953                         [(set VR128:$dst,
1954                           (v8i16 (vector_shuffle VR128:$src1,
1955                                   (bc_v8i16 (memopv2i64 addr:$src2)),
1956                                   UNPCKH_shuffle_mask)))]>;
1957   def PUNPCKHDQrr : PDI<0x6A, MRMSrcReg, 
1958                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1959                         "punpckhdq\t{$src2, $dst|$dst, $src2}",
1960                         [(set VR128:$dst,
1961                           (v4i32 (vector_shuffle VR128:$src1, VR128:$src2,
1962                                   UNPCKH_shuffle_mask)))]>;
1963   def PUNPCKHDQrm : PDI<0x6A, MRMSrcMem, 
1964                         (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1965                         "punpckhdq\t{$src2, $dst|$dst, $src2}",
1966                         [(set VR128:$dst,
1967                           (v4i32 (vector_shuffle VR128:$src1,
1968                                   (bc_v4i32 (memopv2i64 addr:$src2)),
1969                                   UNPCKH_shuffle_mask)))]>;
1970   def PUNPCKHQDQrr : PDI<0x6D, MRMSrcReg, 
1971                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
1972                          "punpckhqdq\t{$src2, $dst|$dst, $src2}",
1973                         [(set VR128:$dst,
1974                           (v2i64 (vector_shuffle VR128:$src1, VR128:$src2,
1975                                   UNPCKH_shuffle_mask)))]>;
1976   def PUNPCKHQDQrm : PDI<0x6D, MRMSrcMem, 
1977                         (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
1978                         "punpckhqdq\t{$src2, $dst|$dst, $src2}",
1979                         [(set VR128:$dst,
1980                           (v2i64 (vector_shuffle VR128:$src1,
1981                                   (memopv2i64 addr:$src2),
1982                                   UNPCKH_shuffle_mask)))]>;
1983 }
1984
1985 // Extract / Insert
1986 def PEXTRWri : PDIi8<0xC5, MRMSrcReg,
1987                     (outs GR32:$dst), (ins VR128:$src1, i32i8imm:$src2),
1988                     "pextrw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1989                     [(set GR32:$dst, (X86pextrw (v8i16 VR128:$src1),
1990                                      (iPTR imm:$src2)))]>;
1991 let isTwoAddress = 1 in {
1992   def PINSRWrri : PDIi8<0xC4, MRMSrcReg,
1993                        (outs VR128:$dst), (ins VR128:$src1,
1994                         GR32:$src2, i32i8imm:$src3),
1995                        "pinsrw\t{$src3, $src2, $dst|$dst, $src2, $src3}",
1996                        [(set VR128:$dst,
1997                          (v8i16 (X86pinsrw (v8i16 VR128:$src1),
1998                                  GR32:$src2, (iPTR imm:$src3))))]>;
1999   def PINSRWrmi : PDIi8<0xC4, MRMSrcMem,
2000                        (outs VR128:$dst), (ins VR128:$src1,
2001                         i16mem:$src2, i32i8imm:$src3),
2002                        "pinsrw\t{$src3, $src2, $dst|$dst, $src2, $src3}",
2003                        [(set VR128:$dst,
2004                          (v8i16 (X86pinsrw (v8i16 VR128:$src1),
2005                                  (i32 (anyext (loadi16 addr:$src2))),
2006                                  (iPTR imm:$src3))))]>;
2007 }
2008
2009 // Mask creation
2010 def PMOVMSKBrr : PDI<0xD7, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
2011                      "pmovmskb\t{$src, $dst|$dst, $src}",
2012                      [(set GR32:$dst, (int_x86_sse2_pmovmskb_128 VR128:$src))]>;
2013
2014 // Conditional store
2015 def MASKMOVDQU : PDI<0xF7, MRMSrcReg, (outs), (ins VR128:$src, VR128:$mask),
2016                      "maskmovdqu\t{$mask, $src|$src, $mask}",
2017                      [(int_x86_sse2_maskmov_dqu VR128:$src, VR128:$mask, EDI)]>,
2018                  Imp<[EDI],[]>;
2019
2020 // Non-temporal stores
2021 def MOVNTPDmr : PDI<0x2B, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
2022                     "movntpd\t{$src, $dst|$dst, $src}",
2023                     [(int_x86_sse2_movnt_pd addr:$dst, VR128:$src)]>;
2024 def MOVNTDQmr : PDI<0xE7, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
2025                     "movntdq\t{$src, $dst|$dst, $src}",
2026                     [(int_x86_sse2_movnt_dq addr:$dst, VR128:$src)]>;
2027 def MOVNTImr  :   I<0xC3, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src),
2028                     "movnti\t{$src, $dst|$dst, $src}",
2029                     [(int_x86_sse2_movnt_i addr:$dst, GR32:$src)]>, 
2030                   TB, Requires<[HasSSE2]>;
2031
2032 // Flush cache
2033 def CLFLUSH : I<0xAE, MRM7m, (outs), (ins i8mem:$src),
2034                "clflush\t$src", [(int_x86_sse2_clflush addr:$src)]>,
2035               TB, Requires<[HasSSE2]>;
2036
2037 // Load, store, and memory fence
2038 def LFENCE : I<0xAE, MRM5m, (outs), (ins),
2039                "lfence", [(int_x86_sse2_lfence)]>, TB, Requires<[HasSSE2]>;
2040 def MFENCE : I<0xAE, MRM6m, (outs), (ins),
2041                "mfence", [(int_x86_sse2_mfence)]>, TB, Requires<[HasSSE2]>;
2042
2043
2044 // Alias instructions that map zero vector to pxor / xorp* for sse.
2045 // FIXME: remove when we can teach regalloc that xor reg, reg is ok.
2046 let isReMaterializable = 1 in
2047   def V_SETALLONES : PDI<0x76, MRMInitReg, (outs VR128:$dst), (ins),
2048                          "pcmpeqd\t$dst, $dst",
2049                          [(set VR128:$dst, (v2f64 immAllOnesV))]>;
2050
2051 // FR64 to 128-bit vector conversion.
2052 def MOVSD2PDrr : SDI<0x10, MRMSrcReg, (outs VR128:$dst), (ins FR64:$src),
2053                       "movsd\t{$src, $dst|$dst, $src}",
2054                       [(set VR128:$dst,
2055                         (v2f64 (scalar_to_vector FR64:$src)))]>;
2056 def MOVSD2PDrm : SDI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
2057                      "movsd\t{$src, $dst|$dst, $src}",
2058                      [(set VR128:$dst, 
2059                        (v2f64 (scalar_to_vector (loadf64 addr:$src))))]>;
2060
2061 def MOVDI2PDIrr : PDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR32:$src),
2062                       "movd\t{$src, $dst|$dst, $src}",
2063                       [(set VR128:$dst,
2064                         (v4i32 (scalar_to_vector GR32:$src)))]>;
2065 def MOVDI2PDIrm : PDI<0x6E, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
2066                       "movd\t{$src, $dst|$dst, $src}",
2067                       [(set VR128:$dst,
2068                         (v4i32 (scalar_to_vector (loadi32 addr:$src))))]>;
2069
2070 def MOVDI2SSrr  : PDI<0x6E, MRMSrcReg, (outs FR32:$dst), (ins GR32:$src),
2071                       "movd\t{$src, $dst|$dst, $src}",
2072                       [(set FR32:$dst, (bitconvert GR32:$src))]>;
2073
2074 def MOVDI2SSrm  : PDI<0x6E, MRMSrcMem, (outs FR32:$dst), (ins i32mem:$src),
2075                       "movd\t{$src, $dst|$dst, $src}",
2076                       [(set FR32:$dst, (bitconvert (loadi32 addr:$src)))]>;
2077
2078 // SSE2 instructions with XS prefix
2079 def MOVQI2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
2080                     "movq\t{$src, $dst|$dst, $src}",
2081                     [(set VR128:$dst,
2082                       (v2i64 (scalar_to_vector (loadi64 addr:$src))))]>, XS,
2083                   Requires<[HasSSE2]>;
2084 def MOVPQI2QImr : PDI<0xD6, MRMDestMem, (outs), (ins i64mem:$dst, VR128:$src),
2085                       "movq\t{$src, $dst|$dst, $src}",
2086                       [(store (i64 (vector_extract (v2i64 VR128:$src),
2087                                     (iPTR 0))), addr:$dst)]>;
2088
2089 // FIXME: may not be able to eliminate this movss with coalescing the src and
2090 // dest register classes are different. We really want to write this pattern
2091 // like this:
2092 // def : Pat<(f32 (vector_extract (v4f32 VR128:$src), (iPTR 0))),
2093 //           (f32 FR32:$src)>;
2094 def MOVPD2SDrr : SDI<0x10, MRMSrcReg, (outs FR64:$dst), (ins VR128:$src),
2095                      "movsd\t{$src, $dst|$dst, $src}",
2096                      [(set FR64:$dst, (vector_extract (v2f64 VR128:$src),
2097                                        (iPTR 0)))]>;
2098 def MOVPD2SDmr : SDI<0x11, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
2099                      "movsd\t{$src, $dst|$dst, $src}",
2100                      [(store (f64 (vector_extract (v2f64 VR128:$src),
2101                                    (iPTR 0))), addr:$dst)]>;
2102 def MOVPDI2DIrr  : PDI<0x7E, MRMDestReg, (outs GR32:$dst), (ins VR128:$src),
2103                        "movd\t{$src, $dst|$dst, $src}",
2104                        [(set GR32:$dst, (vector_extract (v4i32 VR128:$src),
2105                                         (iPTR 0)))]>;
2106 def MOVPDI2DImr  : PDI<0x7E, MRMDestMem, (outs), (ins i32mem:$dst, VR128:$src),
2107                        "movd\t{$src, $dst|$dst, $src}",
2108                        [(store (i32 (vector_extract (v4i32 VR128:$src),
2109                                      (iPTR 0))), addr:$dst)]>;
2110
2111 def MOVSS2DIrr  : PDI<0x7E, MRMDestReg, (outs GR32:$dst), (ins FR32:$src),
2112                       "movd\t{$src, $dst|$dst, $src}",
2113                       [(set GR32:$dst, (bitconvert FR32:$src))]>;
2114 def MOVSS2DImr  : PDI<0x7E, MRMDestMem, (outs), (ins i32mem:$dst, FR32:$src),
2115                       "movd\t{$src, $dst|$dst, $src}",
2116                       [(store (i32 (bitconvert FR32:$src)), addr:$dst)]>;
2117
2118
2119 // Move to lower bits of a VR128, leaving upper bits alone.
2120 // Three operand (but two address) aliases.
2121 let isTwoAddress = 1 in {
2122   def MOVLSD2PDrr : SDI<0x10, MRMSrcReg,
2123                         (outs VR128:$dst), (ins VR128:$src1, FR64:$src2),
2124                         "movsd\t{$src2, $dst|$dst, $src2}", []>;
2125
2126   let AddedComplexity = 15 in
2127     def MOVLPDrr : SDI<0x10, MRMSrcReg,
2128                        (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2129                        "movsd\t{$src2, $dst|$dst, $src2}",
2130                        [(set VR128:$dst,
2131                          (v2f64 (vector_shuffle VR128:$src1, VR128:$src2,
2132                                  MOVL_shuffle_mask)))]>;
2133 }
2134
2135 // Store / copy lower 64-bits of a XMM register.
2136 def MOVLQ128mr : PDI<0xD6, MRMDestMem, (outs), (ins i64mem:$dst, VR128:$src),
2137                      "movq\t{$src, $dst|$dst, $src}",
2138                      [(int_x86_sse2_storel_dq addr:$dst, VR128:$src)]>;
2139
2140 // Move to lower bits of a VR128 and zeroing upper bits.
2141 // Loading from memory automatically zeroing upper bits.
2142 let AddedComplexity = 20 in
2143   def MOVZSD2PDrm : SDI<0x10, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
2144                         "movsd\t{$src, $dst|$dst, $src}",
2145                         [(set VR128:$dst,
2146                           (v2f64 (vector_shuffle immAllZerosV,
2147                                   (v2f64 (scalar_to_vector
2148                                           (loadf64 addr:$src))),
2149                                   MOVL_shuffle_mask)))]>;
2150
2151 let AddedComplexity = 15 in
2152 // movd / movq to XMM register zero-extends
2153 def MOVZDI2PDIrr : PDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR32:$src),
2154                        "movd\t{$src, $dst|$dst, $src}",
2155                        [(set VR128:$dst,
2156                          (v4i32 (vector_shuffle immAllZerosV,
2157                                  (v4i32 (scalar_to_vector GR32:$src)),
2158                                  MOVL_shuffle_mask)))]>;
2159 let AddedComplexity = 20 in
2160 def MOVZDI2PDIrm : PDI<0x6E, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
2161                        "movd\t{$src, $dst|$dst, $src}",
2162                        [(set VR128:$dst,
2163                          (v4i32 (vector_shuffle immAllZerosV,
2164                                  (v4i32 (scalar_to_vector (loadi32 addr:$src))),
2165                                  MOVL_shuffle_mask)))]>;
2166
2167 // Moving from XMM to XMM but still clear upper 64 bits.
2168 let AddedComplexity = 15 in
2169 def MOVZQI2PQIrr : I<0x7E, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2170                      "movq\t{$src, $dst|$dst, $src}",
2171                      [(set VR128:$dst, (int_x86_sse2_movl_dq VR128:$src))]>,
2172                    XS, Requires<[HasSSE2]>;
2173 let AddedComplexity = 20 in
2174 def MOVZQI2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
2175                      "movq\t{$src, $dst|$dst, $src}",
2176                      [(set VR128:$dst, (int_x86_sse2_movl_dq
2177                                         (bitconvert (memopv2i64 addr:$src))))]>,
2178                    XS, Requires<[HasSSE2]>;
2179
2180
2181 //===----------------------------------------------------------------------===//
2182 // SSE3 Instructions
2183 //===----------------------------------------------------------------------===//
2184
2185 // Move Instructions
2186 def MOVSHDUPrr : S3SI<0x16, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2187                       "movshdup\t{$src, $dst|$dst, $src}",
2188                       [(set VR128:$dst, (v4f32 (vector_shuffle
2189                                                 VR128:$src, (undef),
2190                                                 MOVSHDUP_shuffle_mask)))]>;
2191 def MOVSHDUPrm : S3SI<0x16, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
2192                       "movshdup\t{$src, $dst|$dst, $src}",
2193                       [(set VR128:$dst, (v4f32 (vector_shuffle
2194                                                 (memopv4f32 addr:$src), (undef),
2195                                                 MOVSHDUP_shuffle_mask)))]>;
2196
2197 def MOVSLDUPrr : S3SI<0x12, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2198                       "movsldup\t{$src, $dst|$dst, $src}",
2199                       [(set VR128:$dst, (v4f32 (vector_shuffle
2200                                                 VR128:$src, (undef),
2201                                                 MOVSLDUP_shuffle_mask)))]>;
2202 def MOVSLDUPrm : S3SI<0x12, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
2203                       "movsldup\t{$src, $dst|$dst, $src}",
2204                       [(set VR128:$dst, (v4f32 (vector_shuffle
2205                                                 (memopv4f32 addr:$src), (undef),
2206                                                 MOVSLDUP_shuffle_mask)))]>;
2207
2208 def MOVDDUPrr  : S3DI<0x12, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2209                       "movddup\t{$src, $dst|$dst, $src}",
2210                       [(set VR128:$dst, (v2f64 (vector_shuffle
2211                                                 VR128:$src, (undef),
2212                                                 SSE_splat_lo_mask)))]>;
2213 def MOVDDUPrm  : S3DI<0x12, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
2214                       "movddup\t{$src, $dst|$dst, $src}",
2215                       [(set VR128:$dst,
2216                         (v2f64 (vector_shuffle
2217                                 (scalar_to_vector (loadf64 addr:$src)),
2218                                 (undef),
2219                                 SSE_splat_lo_mask)))]>;
2220
2221 // Arithmetic
2222 let isTwoAddress = 1 in {
2223   def ADDSUBPSrr : S3DI<0xD0, MRMSrcReg,
2224                         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2225                         "addsubps\t{$src2, $dst|$dst, $src2}",
2226                         [(set VR128:$dst, (int_x86_sse3_addsub_ps VR128:$src1,
2227                                            VR128:$src2))]>;
2228   def ADDSUBPSrm : S3DI<0xD0, MRMSrcMem,
2229                         (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
2230                         "addsubps\t{$src2, $dst|$dst, $src2}",
2231                         [(set VR128:$dst, (int_x86_sse3_addsub_ps VR128:$src1,
2232                                            (load addr:$src2)))]>;
2233   def ADDSUBPDrr : S3I<0xD0, MRMSrcReg,
2234                        (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2235                        "addsubpd\t{$src2, $dst|$dst, $src2}",
2236                        [(set VR128:$dst, (int_x86_sse3_addsub_pd VR128:$src1,
2237                                           VR128:$src2))]>;
2238   def ADDSUBPDrm : S3I<0xD0, MRMSrcMem,
2239                        (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
2240                        "addsubpd\t{$src2, $dst|$dst, $src2}",
2241                        [(set VR128:$dst, (int_x86_sse3_addsub_pd VR128:$src1,
2242                                           (load addr:$src2)))]>;
2243 }
2244
2245 def LDDQUrm : S3DI<0xF0, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
2246                    "lddqu\t{$src, $dst|$dst, $src}",
2247                    [(set VR128:$dst, (int_x86_sse3_ldu_dq addr:$src))]>;
2248
2249 // Horizontal ops
2250 class S3D_Intrr<bits<8> o, string OpcodeStr, Intrinsic IntId>
2251   : S3DI<o, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2252          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2253          [(set VR128:$dst, (v4f32 (IntId VR128:$src1, VR128:$src2)))]>;
2254 class S3D_Intrm<bits<8> o, string OpcodeStr, Intrinsic IntId>
2255   : S3DI<o, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
2256          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2257          [(set VR128:$dst, (v4f32 (IntId VR128:$src1, (load addr:$src2))))]>;
2258 class S3_Intrr<bits<8> o, string OpcodeStr, Intrinsic IntId>
2259   : S3I<o, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2260         !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2261         [(set VR128:$dst, (v2f64 (IntId VR128:$src1, VR128:$src2)))]>;
2262 class S3_Intrm<bits<8> o, string OpcodeStr, Intrinsic IntId>
2263   : S3I<o, MRMSrcMem, (outs VR128:$dst), (ins VR128:$src1, f128mem:$src2),
2264         !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2265         [(set VR128:$dst, (v2f64 (IntId VR128:$src1, (load addr:$src2))))]>;
2266
2267 let isTwoAddress = 1 in {
2268   def HADDPSrr : S3D_Intrr<0x7C, "haddps", int_x86_sse3_hadd_ps>;
2269   def HADDPSrm : S3D_Intrm<0x7C, "haddps", int_x86_sse3_hadd_ps>;
2270   def HADDPDrr : S3_Intrr <0x7C, "haddpd", int_x86_sse3_hadd_pd>;
2271   def HADDPDrm : S3_Intrm <0x7C, "haddpd", int_x86_sse3_hadd_pd>;
2272   def HSUBPSrr : S3D_Intrr<0x7D, "hsubps", int_x86_sse3_hsub_ps>;
2273   def HSUBPSrm : S3D_Intrm<0x7D, "hsubps", int_x86_sse3_hsub_ps>;
2274   def HSUBPDrr : S3_Intrr <0x7D, "hsubpd", int_x86_sse3_hsub_pd>;
2275   def HSUBPDrm : S3_Intrm <0x7D, "hsubpd", int_x86_sse3_hsub_pd>;
2276 }
2277
2278 // Thread synchronization
2279 def MONITOR : I<0xC8, RawFrm, (outs), (ins), "monitor",
2280                 [(int_x86_sse3_monitor EAX, ECX, EDX)]>,TB, Requires<[HasSSE3]>;
2281 def MWAIT   : I<0xC9, RawFrm, (outs), (ins), "mwait",
2282                 [(int_x86_sse3_mwait ECX, EAX)]>, TB, Requires<[HasSSE3]>;
2283
2284 // vector_shuffle v1, <undef> <1, 1, 3, 3>
2285 let AddedComplexity = 15 in
2286 def : Pat<(v4i32 (vector_shuffle VR128:$src, (undef),
2287                   MOVSHDUP_shuffle_mask)),
2288           (MOVSHDUPrr VR128:$src)>, Requires<[HasSSE3]>;
2289 let AddedComplexity = 20 in
2290 def : Pat<(v4i32 (vector_shuffle (bc_v4i32 (memopv2i64 addr:$src)), (undef),
2291                   MOVSHDUP_shuffle_mask)),
2292           (MOVSHDUPrm addr:$src)>, Requires<[HasSSE3]>;
2293
2294 // vector_shuffle v1, <undef> <0, 0, 2, 2>
2295 let AddedComplexity = 15 in
2296   def : Pat<(v4i32 (vector_shuffle VR128:$src, (undef),
2297                     MOVSLDUP_shuffle_mask)),
2298             (MOVSLDUPrr VR128:$src)>, Requires<[HasSSE3]>;
2299 let AddedComplexity = 20 in
2300   def : Pat<(v4i32 (vector_shuffle (bc_v4i32 (memopv2i64 addr:$src)), (undef),
2301                     MOVSLDUP_shuffle_mask)),
2302             (MOVSLDUPrm addr:$src)>, Requires<[HasSSE3]>;
2303
2304 //===----------------------------------------------------------------------===//
2305 // SSSE3 Instructions
2306 //===----------------------------------------------------------------------===//
2307
2308 // SSSE3 Instruction Templates:
2309 // 
2310 //   SS38I - SSSE3 instructions with T8 prefix.
2311 //   SS3AI - SSSE3 instructions with TA prefix.
2312 //
2313 // Note: SSSE3 instructions have 64-bit and 128-bit versions. The 64-bit version
2314 // uses the MMX registers. We put those instructions here because they better
2315 // fit into the SSSE3 instruction category rather than the MMX category.
2316
2317 class SS38I<bits<8> o, Format F, dag outs, dag ins, string asm,
2318             list<dag> pattern>
2319       : I<o, F, outs, ins, asm, pattern>, T8, Requires<[HasSSSE3]>;
2320 class SS3AI<bits<8> o, Format F, dag outs, dag ins, string asm,
2321             list<dag> pattern>
2322       : I<o, F, outs, ins, asm, pattern>, TA, Requires<[HasSSSE3]>;
2323
2324 /// SS3I_unop_rm_int_8 - Simple SSSE3 unary operator whose type is v*i8.
2325 let isTwoAddress = 1 in {
2326   multiclass SS3I_unop_rm_int_8<bits<8> opc, string OpcodeStr,
2327                                 Intrinsic IntId64, Intrinsic IntId128,
2328                                 bit Commutable = 0> {
2329     def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst), (ins VR64:$src),
2330                      !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2331                      [(set VR64:$dst, (IntId64 VR64:$src))]> {
2332       let isCommutable = Commutable;
2333     }
2334     def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst), (ins i64mem:$src),
2335                      !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2336                      [(set VR64:$dst,
2337                        (IntId64 (bitconvert (memopv8i8 addr:$src))))]>;
2338
2339     def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2340                       (ins VR128:$src),
2341                       !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2342                       [(set VR128:$dst, (IntId128 VR128:$src))]>,
2343                       OpSize {
2344       let isCommutable = Commutable;
2345     }
2346     def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2347                       (ins i128mem:$src),
2348                       !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2349                       [(set VR128:$dst,
2350                         (IntId128
2351                          (bitconvert (memopv16i8 addr:$src))))]>, OpSize;
2352   }
2353 }
2354
2355 /// SS3I_unop_rm_int_16 - Simple SSSE3 unary operator whose type is v*i16.
2356 let isTwoAddress = 1 in {
2357   multiclass SS3I_unop_rm_int_16<bits<8> opc, string OpcodeStr,
2358                                  Intrinsic IntId64, Intrinsic IntId128,
2359                                  bit Commutable = 0> {
2360     def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2361                      (ins VR64:$src),
2362                      !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2363                      [(set VR64:$dst, (IntId64 VR64:$src))]> {
2364       let isCommutable = Commutable;
2365     }
2366     def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2367                      (ins i64mem:$src),
2368                      !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2369                      [(set VR64:$dst,
2370                        (IntId64
2371                         (bitconvert (memopv4i16 addr:$src))))]>;
2372
2373     def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2374                       (ins VR128:$src),
2375                       !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2376                       [(set VR128:$dst, (IntId128 VR128:$src))]>,
2377                       OpSize {
2378       let isCommutable = Commutable;
2379     }
2380     def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2381                       (ins i128mem:$src),
2382                       !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2383                       [(set VR128:$dst,
2384                         (IntId128
2385                          (bitconvert (memopv8i16 addr:$src))))]>, OpSize;
2386   }
2387 }
2388
2389 /// SS3I_unop_rm_int_32 - Simple SSSE3 unary operator whose type is v*i32.
2390 let isTwoAddress = 1 in {
2391   multiclass SS3I_unop_rm_int_32<bits<8> opc, string OpcodeStr,
2392                                  Intrinsic IntId64, Intrinsic IntId128,
2393                                  bit Commutable = 0> {
2394     def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2395                      (ins VR64:$src),
2396                      !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2397                      [(set VR64:$dst, (IntId64 VR64:$src))]> {
2398       let isCommutable = Commutable;
2399     }
2400     def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2401                      (ins i64mem:$src),
2402                      !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2403                      [(set VR64:$dst,
2404                        (IntId64
2405                         (bitconvert (memopv2i32 addr:$src))))]>;
2406
2407     def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2408                       (ins VR128:$src),
2409                       !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2410                       [(set VR128:$dst, (IntId128 VR128:$src))]>,
2411                       OpSize {
2412       let isCommutable = Commutable;
2413     }
2414     def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2415                       (ins i128mem:$src),
2416                       !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
2417                       [(set VR128:$dst,
2418                         (IntId128
2419                          (bitconvert (memopv4i32 addr:$src))))]>, OpSize;
2420   }
2421 }
2422
2423 defm PABSB       : SS3I_unop_rm_int_8 <0x1C, "pabsb",
2424                                        int_x86_ssse3_pabs_b,
2425                                        int_x86_ssse3_pabs_b_128>;
2426 defm PABSW       : SS3I_unop_rm_int_16<0x1D, "pabsw",
2427                                        int_x86_ssse3_pabs_w,
2428                                        int_x86_ssse3_pabs_w_128>;
2429 defm PABSD       : SS3I_unop_rm_int_32<0x1E, "pabsd",
2430                                        int_x86_ssse3_pabs_d,
2431                                        int_x86_ssse3_pabs_d_128>;
2432
2433 /// SS3I_binop_rm_int_8 - Simple SSSE3 binary operator whose type is v*i8.
2434 let isTwoAddress = 1 in {
2435   multiclass SS3I_binop_rm_int_8<bits<8> opc, string OpcodeStr,
2436                                  Intrinsic IntId64, Intrinsic IntId128,
2437                                  bit Commutable = 0> {
2438     def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2439                      (ins VR64:$src1, VR64:$src2),
2440                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2441                      [(set VR64:$dst, (IntId64 VR64:$src1, VR64:$src2))]> {
2442       let isCommutable = Commutable;
2443     }
2444     def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2445                      (ins VR64:$src1, i64mem:$src2),
2446                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2447                      [(set VR64:$dst,
2448                        (IntId64 VR64:$src1,
2449                         (bitconvert (memopv8i8 addr:$src2))))]>;
2450
2451     def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2452                       (ins VR128:$src1, VR128:$src2),
2453                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2454                       [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
2455                       OpSize {
2456       let isCommutable = Commutable;
2457     }
2458     def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2459                       (ins VR128:$src1, i128mem:$src2),
2460                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2461                       [(set VR128:$dst,
2462                         (IntId128 VR128:$src1,
2463                          (bitconvert (memopv16i8 addr:$src2))))]>, OpSize;
2464   }
2465 }
2466
2467 /// SS3I_binop_rm_int_16 - Simple SSSE3 binary operator whose type is v*i16.
2468 let isTwoAddress = 1 in {
2469   multiclass SS3I_binop_rm_int_16<bits<8> opc, string OpcodeStr,
2470                                   Intrinsic IntId64, Intrinsic IntId128,
2471                                   bit Commutable = 0> {
2472     def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2473                      (ins VR64:$src1, VR64:$src2),
2474                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2475                      [(set VR64:$dst, (IntId64 VR64:$src1, VR64:$src2))]> {
2476       let isCommutable = Commutable;
2477     }
2478     def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2479                      (ins VR64:$src1, i64mem:$src2),
2480                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2481                      [(set VR64:$dst,
2482                        (IntId64 VR64:$src1,
2483                         (bitconvert (memopv4i16 addr:$src2))))]>;
2484
2485     def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2486                       (ins VR128:$src1, VR128:$src2),
2487                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2488                       [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
2489                       OpSize {
2490       let isCommutable = Commutable;
2491     }
2492     def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2493                       (ins VR128:$src1, i128mem:$src2),
2494                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2495                       [(set VR128:$dst,
2496                         (IntId128 VR128:$src1,
2497                          (bitconvert (memopv8i16 addr:$src2))))]>, OpSize;
2498   }
2499 }
2500
2501 /// SS3I_binop_rm_int_32 - Simple SSSE3 binary operator whose type is v*i32.
2502 let isTwoAddress = 1 in {
2503   multiclass SS3I_binop_rm_int_32<bits<8> opc, string OpcodeStr,
2504                                   Intrinsic IntId64, Intrinsic IntId128,
2505                                   bit Commutable = 0> {
2506     def rr64 : SS38I<opc, MRMSrcReg, (outs VR64:$dst),
2507                      (ins VR64:$src1, VR64:$src2),
2508                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2509                      [(set VR64:$dst, (IntId64 VR64:$src1, VR64:$src2))]> {
2510       let isCommutable = Commutable;
2511     }
2512     def rm64 : SS38I<opc, MRMSrcMem, (outs VR64:$dst),
2513                      (ins VR64:$src1, i64mem:$src2),
2514                      !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2515                      [(set VR64:$dst,
2516                        (IntId64 VR64:$src1,
2517                         (bitconvert (memopv2i32 addr:$src2))))]>;
2518
2519     def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
2520                       (ins VR128:$src1, VR128:$src2),
2521                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2522                       [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
2523                       OpSize {
2524       let isCommutable = Commutable;
2525     }
2526     def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
2527                       (ins VR128:$src1, i128mem:$src2),
2528                       !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2529                       [(set VR128:$dst,
2530                         (IntId128 VR128:$src1,
2531                          (bitconvert (memopv4i32 addr:$src2))))]>, OpSize;
2532   }
2533 }
2534
2535 defm PHADDW      : SS3I_binop_rm_int_16<0x01, "phaddw",
2536                                         int_x86_ssse3_phadd_w,
2537                                         int_x86_ssse3_phadd_w_128, 1>;
2538 defm PHADDD      : SS3I_binop_rm_int_32<0x02, "phaddd",
2539                                         int_x86_ssse3_phadd_d,
2540                                         int_x86_ssse3_phadd_d_128, 1>;
2541 defm PHADDSW     : SS3I_binop_rm_int_16<0x03, "phaddsw",
2542                                         int_x86_ssse3_phadd_sw,
2543                                         int_x86_ssse3_phadd_sw_128, 1>;
2544 defm PHSUBW      : SS3I_binop_rm_int_16<0x05, "phsubw",
2545                                         int_x86_ssse3_phsub_w,
2546                                         int_x86_ssse3_phsub_w_128>;
2547 defm PHSUBD      : SS3I_binop_rm_int_32<0x06, "phsubd",
2548                                         int_x86_ssse3_phsub_d,
2549                                         int_x86_ssse3_phsub_d_128>;
2550 defm PHSUBSW     : SS3I_binop_rm_int_16<0x07, "phsubsw",
2551                                         int_x86_ssse3_phsub_sw,
2552                                         int_x86_ssse3_phsub_sw_128>;
2553 defm PMADDUBSW   : SS3I_binop_rm_int_8 <0x04, "pmaddubsw",
2554                                         int_x86_ssse3_pmadd_ub_sw,
2555                                         int_x86_ssse3_pmadd_ub_sw_128, 1>;
2556 defm PMULHRSW    : SS3I_binop_rm_int_16<0x0B, "pmulhrsw",
2557                                         int_x86_ssse3_pmul_hr_sw,
2558                                         int_x86_ssse3_pmul_hr_sw_128, 1>;
2559 defm PSHUFB      : SS3I_binop_rm_int_8 <0x00, "pshufb",
2560                                         int_x86_ssse3_pshuf_b,
2561                                         int_x86_ssse3_pshuf_b_128>;
2562 defm PSIGNB      : SS3I_binop_rm_int_8 <0x08, "psignb",
2563                                         int_x86_ssse3_psign_b,
2564                                         int_x86_ssse3_psign_b_128>;
2565 defm PSIGNW      : SS3I_binop_rm_int_16<0x09, "psignw",
2566                                         int_x86_ssse3_psign_w,
2567                                         int_x86_ssse3_psign_w_128>;
2568 defm PSIGND      : SS3I_binop_rm_int_32<0x09, "psignd",
2569                                         int_x86_ssse3_psign_d,
2570                                         int_x86_ssse3_psign_d_128>;
2571
2572 let isTwoAddress = 1 in {
2573   def PALIGNR64rr  : SS3AI<0x0F, MRMSrcReg, (outs VR64:$dst),
2574                            (ins VR64:$src1, VR64:$src2, i16imm:$src3),
2575                            "palignr\t{$src2, $dst|$dst, $src2}",
2576                            [(set VR64:$dst,
2577                              (int_x86_ssse3_palign_r
2578                               VR64:$src1, VR64:$src2,
2579                               imm:$src3))]>;
2580   def PALIGNR64rm  : SS3AI<0x0F, MRMSrcReg, (outs VR64:$dst),
2581                            (ins VR64:$src1, i64mem:$src2, i16imm:$src3),
2582                            "palignr\t{$src2, $dst|$dst, $src2}",
2583                            [(set VR64:$dst,
2584                              (int_x86_ssse3_palign_r
2585                               VR64:$src1,
2586                               (bitconvert (memopv2i32 addr:$src2)),
2587                               imm:$src3))]>;
2588
2589   def PALIGNR128rr : SS3AI<0x0F, MRMSrcReg, (outs VR128:$dst),
2590                            (ins VR128:$src1, VR128:$src2, i32imm:$src3),
2591                            "palignr\t{$src2, $dst|$dst, $src2}",
2592                            [(set VR128:$dst,
2593                              (int_x86_ssse3_palign_r_128
2594                               VR128:$src1, VR128:$src2,
2595                               imm:$src3))]>, OpSize;
2596   def PALIGNR128rm : SS3AI<0x0F, MRMSrcReg, (outs VR128:$dst),
2597                            (ins VR128:$src1, i128mem:$src2, i32imm:$src3),
2598                            "palignr\t{$src2, $dst|$dst, $src2}",
2599                            [(set VR128:$dst,
2600                              (int_x86_ssse3_palign_r_128
2601                               VR128:$src1,
2602                               (bitconvert (memopv4i32 addr:$src2)),
2603                               imm:$src3))]>, OpSize;
2604 }
2605
2606 //===----------------------------------------------------------------------===//
2607 // Non-Instruction Patterns
2608 //===----------------------------------------------------------------------===//
2609
2610 // 128-bit vector undef's.
2611 def : Pat<(v4f32 (undef)), (IMPLICIT_DEF_VR128)>, Requires<[HasSSE2]>;
2612 def : Pat<(v2f64 (undef)), (IMPLICIT_DEF_VR128)>, Requires<[HasSSE2]>;
2613 def : Pat<(v16i8 (undef)), (IMPLICIT_DEF_VR128)>, Requires<[HasSSE2]>;
2614 def : Pat<(v8i16 (undef)), (IMPLICIT_DEF_VR128)>, Requires<[HasSSE2]>;
2615 def : Pat<(v4i32 (undef)), (IMPLICIT_DEF_VR128)>, Requires<[HasSSE2]>;
2616 def : Pat<(v2i64 (undef)), (IMPLICIT_DEF_VR128)>, Requires<[HasSSE2]>;
2617
2618 // 128-bit vector all zero's.
2619 def : Pat<(v16i8 immAllZerosV), (V_SET0)>, Requires<[HasSSE2]>;
2620 def : Pat<(v8i16 immAllZerosV), (V_SET0)>, Requires<[HasSSE2]>;
2621 def : Pat<(v4i32 immAllZerosV), (V_SET0)>, Requires<[HasSSE2]>;
2622 def : Pat<(v2i64 immAllZerosV), (V_SET0)>, Requires<[HasSSE2]>;
2623 def : Pat<(v2f64 immAllZerosV), (V_SET0)>, Requires<[HasSSE2]>;
2624
2625 // 128-bit vector all one's.
2626 def : Pat<(v16i8 immAllOnesV), (V_SETALLONES)>, Requires<[HasSSE2]>;
2627 def : Pat<(v8i16 immAllOnesV), (V_SETALLONES)>, Requires<[HasSSE2]>;
2628 def : Pat<(v4i32 immAllOnesV), (V_SETALLONES)>, Requires<[HasSSE2]>;
2629 def : Pat<(v2i64 immAllOnesV), (V_SETALLONES)>, Requires<[HasSSE2]>;
2630 def : Pat<(v4f32 immAllOnesV), (V_SETALLONES)>, Requires<[HasSSE1]>;
2631
2632
2633 // Scalar to v8i16 / v16i8. The source may be a GR32, but only the lower 8 or
2634 // 16-bits matter.
2635 def : Pat<(v8i16 (X86s2vec GR32:$src)), (MOVDI2PDIrr GR32:$src)>,
2636       Requires<[HasSSE2]>;
2637 def : Pat<(v16i8 (X86s2vec GR32:$src)), (MOVDI2PDIrr GR32:$src)>,
2638       Requires<[HasSSE2]>;
2639
2640 // bit_convert
2641 let Predicates = [HasSSE2] in {
2642   def : Pat<(v2i64 (bitconvert (v4i32 VR128:$src))), (v2i64 VR128:$src)>;
2643   def : Pat<(v2i64 (bitconvert (v8i16 VR128:$src))), (v2i64 VR128:$src)>;
2644   def : Pat<(v2i64 (bitconvert (v16i8 VR128:$src))), (v2i64 VR128:$src)>;
2645   def : Pat<(v2i64 (bitconvert (v2f64 VR128:$src))), (v2i64 VR128:$src)>;
2646   def : Pat<(v2i64 (bitconvert (v4f32 VR128:$src))), (v2i64 VR128:$src)>;
2647   def : Pat<(v4i32 (bitconvert (v2i64 VR128:$src))), (v4i32 VR128:$src)>;
2648   def : Pat<(v4i32 (bitconvert (v8i16 VR128:$src))), (v4i32 VR128:$src)>;
2649   def : Pat<(v4i32 (bitconvert (v16i8 VR128:$src))), (v4i32 VR128:$src)>;
2650   def : Pat<(v4i32 (bitconvert (v2f64 VR128:$src))), (v4i32 VR128:$src)>;
2651   def : Pat<(v4i32 (bitconvert (v4f32 VR128:$src))), (v4i32 VR128:$src)>;
2652   def : Pat<(v8i16 (bitconvert (v2i64 VR128:$src))), (v8i16 VR128:$src)>;
2653   def : Pat<(v8i16 (bitconvert (v4i32 VR128:$src))), (v8i16 VR128:$src)>;
2654   def : Pat<(v8i16 (bitconvert (v16i8 VR128:$src))), (v8i16 VR128:$src)>;
2655   def : Pat<(v8i16 (bitconvert (v2f64 VR128:$src))), (v8i16 VR128:$src)>;
2656   def : Pat<(v8i16 (bitconvert (v4f32 VR128:$src))), (v8i16 VR128:$src)>;
2657   def : Pat<(v16i8 (bitconvert (v2i64 VR128:$src))), (v16i8 VR128:$src)>;
2658   def : Pat<(v16i8 (bitconvert (v4i32 VR128:$src))), (v16i8 VR128:$src)>;
2659   def : Pat<(v16i8 (bitconvert (v8i16 VR128:$src))), (v16i8 VR128:$src)>;
2660   def : Pat<(v16i8 (bitconvert (v2f64 VR128:$src))), (v16i8 VR128:$src)>;
2661   def : Pat<(v16i8 (bitconvert (v4f32 VR128:$src))), (v16i8 VR128:$src)>;
2662   def : Pat<(v4f32 (bitconvert (v2i64 VR128:$src))), (v4f32 VR128:$src)>;
2663   def : Pat<(v4f32 (bitconvert (v4i32 VR128:$src))), (v4f32 VR128:$src)>;
2664   def : Pat<(v4f32 (bitconvert (v8i16 VR128:$src))), (v4f32 VR128:$src)>;
2665   def : Pat<(v4f32 (bitconvert (v16i8 VR128:$src))), (v4f32 VR128:$src)>;
2666   def : Pat<(v4f32 (bitconvert (v2f64 VR128:$src))), (v4f32 VR128:$src)>;
2667   def : Pat<(v2f64 (bitconvert (v2i64 VR128:$src))), (v2f64 VR128:$src)>;
2668   def : Pat<(v2f64 (bitconvert (v4i32 VR128:$src))), (v2f64 VR128:$src)>;
2669   def : Pat<(v2f64 (bitconvert (v8i16 VR128:$src))), (v2f64 VR128:$src)>;
2670   def : Pat<(v2f64 (bitconvert (v16i8 VR128:$src))), (v2f64 VR128:$src)>;
2671   def : Pat<(v2f64 (bitconvert (v4f32 VR128:$src))), (v2f64 VR128:$src)>;
2672 }
2673
2674 // Move scalar to XMM zero-extended
2675 // movd to XMM register zero-extends
2676 let AddedComplexity = 15 in {
2677 def : Pat<(v8i16 (vector_shuffle immAllZerosV,
2678                   (v8i16 (X86s2vec GR32:$src)), MOVL_shuffle_mask)),
2679           (MOVZDI2PDIrr GR32:$src)>, Requires<[HasSSE2]>;
2680 def : Pat<(v16i8 (vector_shuffle immAllZerosV,
2681                   (v16i8 (X86s2vec GR32:$src)), MOVL_shuffle_mask)),
2682           (MOVZDI2PDIrr GR32:$src)>, Requires<[HasSSE2]>;
2683 // Zeroing a VR128 then do a MOVS{S|D} to the lower bits.
2684 def : Pat<(v2f64 (vector_shuffle immAllZerosV,
2685                   (v2f64 (scalar_to_vector FR64:$src)), MOVL_shuffle_mask)),
2686           (MOVLSD2PDrr (V_SET0), FR64:$src)>, Requires<[HasSSE2]>;
2687 def : Pat<(v4f32 (vector_shuffle immAllZerosV,
2688                   (v4f32 (scalar_to_vector FR32:$src)), MOVL_shuffle_mask)),
2689           (MOVLSS2PSrr (V_SET0), FR32:$src)>, Requires<[HasSSE2]>;
2690 }
2691
2692 // Splat v2f64 / v2i64
2693 let AddedComplexity = 10 in {
2694 def : Pat<(vector_shuffle (v2f64 VR128:$src), (undef), SSE_splat_lo_mask:$sm),
2695           (UNPCKLPDrr VR128:$src, VR128:$src)>,   Requires<[HasSSE2]>;
2696 def : Pat<(vector_shuffle (v2f64 VR128:$src), (undef), UNPCKH_shuffle_mask:$sm),
2697           (UNPCKHPDrr VR128:$src, VR128:$src)>,   Requires<[HasSSE2]>;
2698 def : Pat<(vector_shuffle (v2i64 VR128:$src), (undef), SSE_splat_lo_mask:$sm),
2699           (PUNPCKLQDQrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2700 def : Pat<(vector_shuffle (v2i64 VR128:$src), (undef), UNPCKH_shuffle_mask:$sm),
2701           (PUNPCKHQDQrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2702 }
2703
2704 // Splat v4f32
2705 def : Pat<(vector_shuffle (v4f32 VR128:$src), (undef), SSE_splat_mask:$sm),
2706           (SHUFPSrri VR128:$src, VR128:$src, SSE_splat_mask:$sm)>,
2707       Requires<[HasSSE1]>;
2708
2709 // Special unary SHUFPSrri case.
2710 // FIXME: when we want non two-address code, then we should use PSHUFD?
2711 def : Pat<(vector_shuffle (v4f32 VR128:$src1), (undef),
2712            SHUFP_unary_shuffle_mask:$sm),
2713           (SHUFPSrri VR128:$src1, VR128:$src1, SHUFP_unary_shuffle_mask:$sm)>,
2714       Requires<[HasSSE1]>;
2715 // Special unary SHUFPDrri case.
2716 def : Pat<(vector_shuffle (v2f64 VR128:$src1), (undef),
2717            SHUFP_unary_shuffle_mask:$sm),
2718           (SHUFPDrri VR128:$src1, VR128:$src1, SHUFP_unary_shuffle_mask:$sm)>,
2719       Requires<[HasSSE2]>;
2720 // Unary v4f32 shuffle with PSHUF* in order to fold a load.
2721 def : Pat<(vector_shuffle (memopv4f32 addr:$src1), (undef),
2722            SHUFP_unary_shuffle_mask:$sm),
2723           (PSHUFDmi addr:$src1, SHUFP_unary_shuffle_mask:$sm)>,
2724       Requires<[HasSSE2]>;
2725 // Special binary v4i32 shuffle cases with SHUFPS.
2726 def : Pat<(vector_shuffle (v4i32 VR128:$src1), (v4i32 VR128:$src2),
2727            PSHUFD_binary_shuffle_mask:$sm),
2728           (SHUFPSrri VR128:$src1, VR128:$src2, PSHUFD_binary_shuffle_mask:$sm)>,
2729            Requires<[HasSSE2]>;
2730 def : Pat<(vector_shuffle (v4i32 VR128:$src1),
2731            (bc_v4i32 (memopv2i64 addr:$src2)), PSHUFD_binary_shuffle_mask:$sm),
2732           (SHUFPSrmi VR128:$src1, addr:$src2, PSHUFD_binary_shuffle_mask:$sm)>,
2733            Requires<[HasSSE2]>;
2734
2735 // vector_shuffle v1, <undef>, <0, 0, 1, 1, ...>
2736 let AddedComplexity = 10 in {
2737 def : Pat<(v4f32 (vector_shuffle VR128:$src, (undef),
2738                   UNPCKL_v_undef_shuffle_mask)),
2739           (UNPCKLPSrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2740 def : Pat<(v16i8 (vector_shuffle VR128:$src, (undef),
2741                   UNPCKL_v_undef_shuffle_mask)),
2742           (PUNPCKLBWrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2743 def : Pat<(v8i16 (vector_shuffle VR128:$src, (undef),
2744                   UNPCKL_v_undef_shuffle_mask)),
2745           (PUNPCKLWDrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2746 def : Pat<(v4i32 (vector_shuffle VR128:$src, (undef),
2747                   UNPCKL_v_undef_shuffle_mask)),
2748           (PUNPCKLDQrr VR128:$src, VR128:$src)>, Requires<[HasSSE1]>;
2749 }
2750
2751 // vector_shuffle v1, <undef>, <2, 2, 3, 3, ...>
2752 let AddedComplexity = 10 in {
2753 def : Pat<(v4f32 (vector_shuffle VR128:$src, (undef),
2754                   UNPCKH_v_undef_shuffle_mask)),
2755           (UNPCKHPSrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2756 def : Pat<(v16i8 (vector_shuffle VR128:$src, (undef),
2757                   UNPCKH_v_undef_shuffle_mask)),
2758           (PUNPCKHBWrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2759 def : Pat<(v8i16 (vector_shuffle VR128:$src, (undef),
2760                   UNPCKH_v_undef_shuffle_mask)),
2761           (PUNPCKHWDrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
2762 def : Pat<(v4i32 (vector_shuffle VR128:$src, (undef),
2763                   UNPCKH_v_undef_shuffle_mask)),
2764           (PUNPCKHDQrr VR128:$src, VR128:$src)>, Requires<[HasSSE1]>;
2765 }
2766
2767 let AddedComplexity = 15 in {
2768 // vector_shuffle v1, v2 <0, 1, 4, 5> using MOVLHPS
2769 def : Pat<(v4i32 (vector_shuffle VR128:$src1, VR128:$src2,
2770                   MOVHP_shuffle_mask)),
2771           (MOVLHPSrr VR128:$src1, VR128:$src2)>;
2772
2773 // vector_shuffle v1, v2 <6, 7, 2, 3> using MOVHLPS
2774 def : Pat<(v4i32 (vector_shuffle VR128:$src1, VR128:$src2,
2775                   MOVHLPS_shuffle_mask)),
2776           (MOVHLPSrr VR128:$src1, VR128:$src2)>;
2777
2778 // vector_shuffle v1, undef <2, ?, ?, ?> using MOVHLPS
2779 def : Pat<(v4f32 (vector_shuffle VR128:$src1, (undef),
2780                   MOVHLPS_v_undef_shuffle_mask)),
2781           (MOVHLPSrr VR128:$src1, VR128:$src1)>;
2782 def : Pat<(v4i32 (vector_shuffle VR128:$src1, (undef),
2783                   MOVHLPS_v_undef_shuffle_mask)),
2784           (MOVHLPSrr VR128:$src1, VR128:$src1)>;
2785 }
2786
2787 let AddedComplexity = 20 in {
2788 // vector_shuffle v1, (load v2) <4, 5, 2, 3> using MOVLPS
2789 // vector_shuffle v1, (load v2) <0, 1, 4, 5> using MOVHPS
2790 def : Pat<(v4f32 (vector_shuffle VR128:$src1, (memopv4f32 addr:$src2),
2791                   MOVLP_shuffle_mask)),
2792           (MOVLPSrm VR128:$src1, addr:$src2)>, Requires<[HasSSE1]>;
2793 def : Pat<(v2f64 (vector_shuffle VR128:$src1, (memopv2f64 addr:$src2),
2794                   MOVLP_shuffle_mask)),
2795           (MOVLPDrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
2796 def : Pat<(v4f32 (vector_shuffle VR128:$src1, (memopv4f32 addr:$src2),
2797                   MOVHP_shuffle_mask)),
2798           (MOVHPSrm VR128:$src1, addr:$src2)>, Requires<[HasSSE1]>;
2799 def : Pat<(v2f64 (vector_shuffle VR128:$src1, (memopv2f64 addr:$src2),
2800                   MOVHP_shuffle_mask)),
2801           (MOVHPDrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
2802
2803 def : Pat<(v4i32 (vector_shuffle VR128:$src1, (bc_v4i32 (memopv2i64 addr:$src2)),
2804                   MOVLP_shuffle_mask)),
2805           (MOVLPSrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
2806 def : Pat<(v2i64 (vector_shuffle VR128:$src1, (memopv2i64 addr:$src2),
2807                   MOVLP_shuffle_mask)),
2808           (MOVLPDrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
2809 def : Pat<(v4i32 (vector_shuffle VR128:$src1, (bc_v4i32 (memopv2i64 addr:$src2)),
2810                   MOVHP_shuffle_mask)),
2811           (MOVHPSrm VR128:$src1, addr:$src2)>, Requires<[HasSSE1]>;
2812 def : Pat<(v2i64 (vector_shuffle VR128:$src1, (memopv2i64 addr:$src2),
2813                   MOVLP_shuffle_mask)),
2814           (MOVLPDrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
2815 }
2816
2817 let AddedComplexity = 15 in {
2818 // Setting the lowest element in the vector.
2819 def : Pat<(v4i32 (vector_shuffle VR128:$src1, VR128:$src2,
2820                   MOVL_shuffle_mask)),
2821           (MOVLPSrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
2822 def : Pat<(v2i64 (vector_shuffle VR128:$src1, VR128:$src2,
2823                   MOVL_shuffle_mask)),
2824           (MOVLPDrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
2825
2826 // vector_shuffle v1, v2 <4, 5, 2, 3> using MOVLPDrr (movsd)
2827 def : Pat<(v4f32 (vector_shuffle VR128:$src1, VR128:$src2,
2828                   MOVLP_shuffle_mask)),
2829           (MOVLPDrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
2830 def : Pat<(v4i32 (vector_shuffle VR128:$src1, VR128:$src2,
2831                   MOVLP_shuffle_mask)),
2832           (MOVLPDrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
2833 }
2834
2835 // Set lowest element and zero upper elements.
2836 let AddedComplexity = 20 in
2837 def : Pat<(bc_v2i64 (vector_shuffle immAllZerosV,
2838                      (v2f64 (scalar_to_vector (loadf64 addr:$src))),
2839                      MOVL_shuffle_mask)),
2840           (MOVZQI2PQIrm addr:$src)>, Requires<[HasSSE2]>;
2841
2842 // FIXME: Temporary workaround since 2-wide shuffle is broken.
2843 def : Pat<(int_x86_sse2_movs_d  VR128:$src1, VR128:$src2),
2844           (v2f64 (MOVLPDrr VR128:$src1, VR128:$src2))>, Requires<[HasSSE2]>;
2845 def : Pat<(int_x86_sse2_loadh_pd VR128:$src1, addr:$src2),
2846           (v2f64 (MOVHPDrm VR128:$src1, addr:$src2))>, Requires<[HasSSE2]>;
2847 def : Pat<(int_x86_sse2_loadl_pd VR128:$src1, addr:$src2),
2848           (v2f64 (MOVLPDrm VR128:$src1, addr:$src2))>, Requires<[HasSSE2]>;
2849 def : Pat<(int_x86_sse2_shuf_pd VR128:$src1, VR128:$src2, imm:$src3),
2850           (v2f64 (SHUFPDrri VR128:$src1, VR128:$src2, imm:$src3))>,
2851       Requires<[HasSSE2]>;
2852 def : Pat<(int_x86_sse2_shuf_pd VR128:$src1, (load addr:$src2), imm:$src3),
2853           (v2f64 (SHUFPDrmi VR128:$src1, addr:$src2, imm:$src3))>,
2854       Requires<[HasSSE2]>;
2855 def : Pat<(int_x86_sse2_unpckh_pd VR128:$src1, VR128:$src2),
2856           (v2f64 (UNPCKHPDrr VR128:$src1, VR128:$src2))>, Requires<[HasSSE2]>;
2857 def : Pat<(int_x86_sse2_unpckh_pd VR128:$src1, (load addr:$src2)),
2858           (v2f64 (UNPCKHPDrm VR128:$src1, addr:$src2))>, Requires<[HasSSE2]>;
2859 def : Pat<(int_x86_sse2_unpckl_pd VR128:$src1, VR128:$src2),
2860           (v2f64 (UNPCKLPDrr VR128:$src1, VR128:$src2))>, Requires<[HasSSE2]>;
2861 def : Pat<(int_x86_sse2_unpckl_pd VR128:$src1, (load addr:$src2)),
2862           (v2f64 (UNPCKLPDrm VR128:$src1, addr:$src2))>, Requires<[HasSSE2]>;
2863 def : Pat<(int_x86_sse2_punpckh_qdq VR128:$src1, VR128:$src2),
2864           (v2i64 (PUNPCKHQDQrr VR128:$src1, VR128:$src2))>, Requires<[HasSSE2]>;
2865 def : Pat<(int_x86_sse2_punpckh_qdq VR128:$src1, (load addr:$src2)),
2866           (v2i64 (PUNPCKHQDQrm VR128:$src1, addr:$src2))>, Requires<[HasSSE2]>;
2867 def : Pat<(int_x86_sse2_punpckl_qdq VR128:$src1, VR128:$src2),
2868           (v2i64 (PUNPCKLQDQrr VR128:$src1, VR128:$src2))>, Requires<[HasSSE2]>;
2869 def : Pat<(int_x86_sse2_punpckl_qdq VR128:$src1, (load addr:$src2)),
2870           (PUNPCKLQDQrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
2871
2872 // Some special case pandn patterns.
2873 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v4i32 immAllOnesV))),
2874                   VR128:$src2)),
2875           (PANDNrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
2876 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v8i16 immAllOnesV))),
2877                   VR128:$src2)),
2878           (PANDNrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
2879 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v16i8 immAllOnesV))),
2880                   VR128:$src2)),
2881           (PANDNrr VR128:$src1, VR128:$src2)>, Requires<[HasSSE2]>;
2882
2883 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v4i32 immAllOnesV))),
2884                   (memopv2i64 addr:$src2))),
2885           (PANDNrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
2886 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v8i16 immAllOnesV))),
2887                   (memopv2i64 addr:$src2))),
2888           (PANDNrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
2889 def : Pat<(v2i64 (and (xor VR128:$src1, (bc_v2i64 (v16i8 immAllOnesV))),
2890                   (memopv2i64 addr:$src2))),
2891           (PANDNrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
2892
2893 // Use movaps / movups for SSE integer load / store (one byte shorter).
2894 def : Pat<(alignedloadv4i32 addr:$src),
2895           (MOVAPSrm addr:$src)>, Requires<[HasSSE1]>;
2896 def : Pat<(loadv4i32 addr:$src),
2897           (MOVUPSrm addr:$src)>, Requires<[HasSSE1]>;
2898 def : Pat<(alignedloadv2i64 addr:$src),
2899           (MOVAPSrm addr:$src)>, Requires<[HasSSE2]>;
2900 def : Pat<(loadv2i64 addr:$src),
2901           (MOVUPSrm addr:$src)>, Requires<[HasSSE2]>;
2902
2903 def : Pat<(alignedstore (v2i64 VR128:$src), addr:$dst),
2904           (MOVAPSmr addr:$dst, VR128:$src)>, Requires<[HasSSE2]>;
2905 def : Pat<(alignedstore (v4i32 VR128:$src), addr:$dst),
2906           (MOVAPSmr addr:$dst, VR128:$src)>, Requires<[HasSSE2]>;
2907 def : Pat<(alignedstore (v8i16 VR128:$src), addr:$dst),
2908           (MOVAPSmr addr:$dst, VR128:$src)>, Requires<[HasSSE2]>;
2909 def : Pat<(alignedstore (v16i8 VR128:$src), addr:$dst),
2910           (MOVAPSmr addr:$dst, VR128:$src)>, Requires<[HasSSE2]>;
2911 def : Pat<(store (v2i64 VR128:$src), addr:$dst),
2912           (MOVUPSmr addr:$dst, VR128:$src)>, Requires<[HasSSE2]>;
2913 def : Pat<(store (v4i32 VR128:$src), addr:$dst),
2914           (MOVUPSmr addr:$dst, VR128:$src)>, Requires<[HasSSE2]>;
2915 def : Pat<(store (v8i16 VR128:$src), addr:$dst),
2916           (MOVUPSmr addr:$dst, VR128:$src)>, Requires<[HasSSE2]>;
2917 def : Pat<(store (v16i8 VR128:$src), addr:$dst),
2918           (MOVUPSmr addr:$dst, VR128:$src)>, Requires<[HasSSE2]>;
2919
2920 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
2921 def : Pat<(vector_extract
2922            (bc_v4i32 (v4f32 (scalar_to_vector (loadf32 addr:$src)))), (iPTR 0)),
2923           (MOV32rm addr:$src)>, Requires<[HasSSE2]>;
2924 def : Pat<(vector_extract
2925            (bc_v2i64 (v2f64 (scalar_to_vector (loadf64 addr:$src)))), (iPTR 0)),
2926           (MOV64rm addr:$src)>, Requires<[HasSSE2, In64BitMode]>;