Add SINT_TO_FP and FP_TO_SINT support for v8i32 types. Also move
[oota-llvm.git] / lib / Target / X86 / X86InstrSSE.td
1 //====- X86InstrSSE.td - Describe the X86 Instruction Set --*- tablegen -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes the X86 SSE instruction set, defining the instructions,
11 // and properties of the instructions which are needed for code generation,
12 // machine code emission, and analysis.
13 //
14 //===----------------------------------------------------------------------===//
15
16
17 //===----------------------------------------------------------------------===//
18 // SSE 1 & 2 Instructions Classes
19 //===----------------------------------------------------------------------===//
20
21 /// sse12_fp_scalar - SSE 1 & 2 scalar instructions class
22 multiclass sse12_fp_scalar<bits<8> opc, string OpcodeStr, SDNode OpNode,
23                            RegisterClass RC, X86MemOperand x86memop,
24                            bit Is2Addr = 1> {
25   let isCommutable = 1 in {
26     def rr : SI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
27        !if(Is2Addr,
28            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
29            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
30        [(set RC:$dst, (OpNode RC:$src1, RC:$src2))]>;
31   }
32   def rm : SI<opc, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
33        !if(Is2Addr,
34            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
35            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
36        [(set RC:$dst, (OpNode RC:$src1, (load addr:$src2)))]>;
37 }
38
39 /// sse12_fp_scalar_int - SSE 1 & 2 scalar instructions intrinsics class
40 multiclass sse12_fp_scalar_int<bits<8> opc, string OpcodeStr, RegisterClass RC,
41                              string asm, string SSEVer, string FPSizeStr,
42                              Operand memopr, ComplexPattern mem_cpat,
43                              bit Is2Addr = 1> {
44   def rr_Int : SI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
45        !if(Is2Addr,
46            !strconcat(asm, "\t{$src2, $dst|$dst, $src2}"),
47            !strconcat(asm, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
48        [(set RC:$dst, (!cast<Intrinsic>(
49                  !strconcat("int_x86_sse", SSEVer, "_", OpcodeStr, FPSizeStr))
50              RC:$src1, RC:$src2))]>;
51   def rm_Int : SI<opc, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, memopr:$src2),
52        !if(Is2Addr,
53            !strconcat(asm, "\t{$src2, $dst|$dst, $src2}"),
54            !strconcat(asm, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
55        [(set RC:$dst, (!cast<Intrinsic>(!strconcat("int_x86_sse",
56                                           SSEVer, "_", OpcodeStr, FPSizeStr))
57              RC:$src1, mem_cpat:$src2))]>;
58 }
59
60 /// sse12_fp_packed - SSE 1 & 2 packed instructions class
61 multiclass sse12_fp_packed<bits<8> opc, string OpcodeStr, SDNode OpNode,
62                            RegisterClass RC, ValueType vt,
63                            X86MemOperand x86memop, PatFrag mem_frag,
64                            Domain d, bit Is2Addr = 1> {
65   let isCommutable = 1 in
66     def rr : PI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
67        !if(Is2Addr,
68            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
69            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
70        [(set RC:$dst, (vt (OpNode RC:$src1, RC:$src2)))], d>;
71   let mayLoad = 1 in
72     def rm : PI<opc, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
73        !if(Is2Addr,
74            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
75            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
76        [(set RC:$dst, (OpNode RC:$src1, (mem_frag addr:$src2)))], d>;
77 }
78
79 /// sse12_fp_packed_logical_rm - SSE 1 & 2 packed instructions class
80 multiclass sse12_fp_packed_logical_rm<bits<8> opc, RegisterClass RC, Domain d,
81                                       string OpcodeStr, X86MemOperand x86memop,
82                                       list<dag> pat_rr, list<dag> pat_rm,
83                                       bit Is2Addr = 1> {
84   let isCommutable = 1 in
85     def rr : PI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
86        !if(Is2Addr,
87            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
88            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
89        pat_rr, d>;
90   def rm : PI<opc, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
91        !if(Is2Addr,
92            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
93            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
94        pat_rm, d>;
95 }
96
97 /// sse12_fp_packed_int - SSE 1 & 2 packed instructions intrinsics class
98 multiclass sse12_fp_packed_int<bits<8> opc, string OpcodeStr, RegisterClass RC,
99                            string asm, string SSEVer, string FPSizeStr,
100                            X86MemOperand x86memop, PatFrag mem_frag,
101                            Domain d, bit Is2Addr = 1> {
102   def rr_Int : PI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
103        !if(Is2Addr,
104            !strconcat(asm, "\t{$src2, $dst|$dst, $src2}"),
105            !strconcat(asm, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
106            [(set RC:$dst, (!cast<Intrinsic>(
107                      !strconcat("int_x86_", SSEVer, "_", OpcodeStr, FPSizeStr))
108                  RC:$src1, RC:$src2))], d>;
109   def rm_Int : PI<opc, MRMSrcMem, (outs RC:$dst), (ins RC:$src1,x86memop:$src2),
110        !if(Is2Addr,
111            !strconcat(asm, "\t{$src2, $dst|$dst, $src2}"),
112            !strconcat(asm, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
113        [(set RC:$dst, (!cast<Intrinsic>(
114                      !strconcat("int_x86_", SSEVer, "_", OpcodeStr, FPSizeStr))
115              RC:$src1, (mem_frag addr:$src2)))], d>;
116 }
117
118 //===----------------------------------------------------------------------===//
119 // SSE 1 & 2 - Move Instructions
120 //===----------------------------------------------------------------------===//
121
122 class sse12_move_rr<RegisterClass RC, ValueType vt, string asm> :
123       SI<0x10, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src1, RC:$src2), asm,
124       [(set (vt VR128:$dst), (movl VR128:$src1, (scalar_to_vector RC:$src2)))]>;
125
126 // Loading from memory automatically zeroing upper bits.
127 class sse12_move_rm<RegisterClass RC, X86MemOperand x86memop,
128                     PatFrag mem_pat, string OpcodeStr> :
129       SI<0x10, MRMSrcMem, (outs RC:$dst), (ins x86memop:$src),
130          !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
131                         [(set RC:$dst, (mem_pat addr:$src))]>;
132
133 // Move Instructions. Register-to-register movss/movsd is not used for FR32/64
134 // register copies because it's a partial register update; FsMOVAPSrr/FsMOVAPDrr
135 // is used instead. Register-to-register movss/movsd is not modeled as an
136 // INSERT_SUBREG because INSERT_SUBREG requires that the insert be implementable
137 // in terms of a copy, and just mentioned, we don't use movss/movsd for copies.
138 def VMOVSSrr : sse12_move_rr<FR32, v4f32,
139                 "movss\t{$src2, $src1, $dst|$dst, $src1, $src2}">, XS, VEX_4V;
140 def VMOVSDrr : sse12_move_rr<FR64, v2f64,
141                 "movsd\t{$src2, $src1, $dst|$dst, $src1, $src2}">, XD, VEX_4V;
142
143 let canFoldAsLoad = 1, isReMaterializable = 1 in {
144   def VMOVSSrm : sse12_move_rm<FR32, f32mem, loadf32, "movss">, XS, VEX;
145
146   let AddedComplexity = 20 in
147     def VMOVSDrm : sse12_move_rm<FR64, f64mem, loadf64, "movsd">, XD, VEX;
148 }
149
150 let Constraints = "$src1 = $dst" in {
151   def MOVSSrr : sse12_move_rr<FR32, v4f32,
152                           "movss\t{$src2, $dst|$dst, $src2}">, XS;
153   def MOVSDrr : sse12_move_rr<FR64, v2f64,
154                           "movsd\t{$src2, $dst|$dst, $src2}">, XD;
155 }
156
157 let canFoldAsLoad = 1, isReMaterializable = 1 in {
158   def MOVSSrm : sse12_move_rm<FR32, f32mem, loadf32, "movss">, XS;
159
160   let AddedComplexity = 20 in
161     def MOVSDrm : sse12_move_rm<FR64, f64mem, loadf64, "movsd">, XD;
162 }
163
164 let AddedComplexity = 15 in {
165 // Extract the low 32-bit value from one vector and insert it into another.
166 def : Pat<(v4f32 (movl VR128:$src1, VR128:$src2)),
167           (MOVSSrr (v4f32 VR128:$src1),
168                    (EXTRACT_SUBREG (v4f32 VR128:$src2), sub_ss))>;
169 // Extract the low 64-bit value from one vector and insert it into another.
170 def : Pat<(v2f64 (movl VR128:$src1, VR128:$src2)),
171           (MOVSDrr (v2f64 VR128:$src1),
172                    (EXTRACT_SUBREG (v2f64 VR128:$src2), sub_sd))>;
173 }
174
175 // Implicitly promote a 32-bit scalar to a vector.
176 def : Pat<(v4f32 (scalar_to_vector FR32:$src)),
177           (INSERT_SUBREG (v4f32 (IMPLICIT_DEF)), FR32:$src, sub_ss)>;
178 // Implicitly promote a 64-bit scalar to a vector.
179 def : Pat<(v2f64 (scalar_to_vector FR64:$src)),
180           (INSERT_SUBREG (v2f64 (IMPLICIT_DEF)), FR64:$src, sub_sd)>;
181 // Implicitly promote a 32-bit scalar to a vector.
182 def : Pat<(v8f32 (scalar_to_vector FR32:$src)),
183           (INSERT_SUBREG (v8f32 (IMPLICIT_DEF)), FR32:$src, sub_ss)>;
184 // Implicitly promote a 64-bit scalar to a vector.
185 def : Pat<(v4f64 (scalar_to_vector FR64:$src)),
186           (INSERT_SUBREG (v4f64 (IMPLICIT_DEF)), FR64:$src, sub_sd)>;
187
188 let AddedComplexity = 20 in {
189 // MOVSSrm zeros the high parts of the register; represent this
190 // with SUBREG_TO_REG.
191 def : Pat<(v4f32 (X86vzmovl (v4f32 (scalar_to_vector (loadf32 addr:$src))))),
192           (SUBREG_TO_REG (i32 0), (MOVSSrm addr:$src), sub_ss)>;
193 def : Pat<(v4f32 (scalar_to_vector (loadf32 addr:$src))),
194           (SUBREG_TO_REG (i32 0), (MOVSSrm addr:$src), sub_ss)>;
195 def : Pat<(v4f32 (X86vzmovl (loadv4f32 addr:$src))),
196           (SUBREG_TO_REG (i32 0), (MOVSSrm addr:$src), sub_ss)>;
197 // MOVSDrm zeros the high parts of the register; represent this
198 // with SUBREG_TO_REG.
199 def : Pat<(v2f64 (X86vzmovl (v2f64 (scalar_to_vector (loadf64 addr:$src))))),
200           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
201 def : Pat<(v2f64 (scalar_to_vector (loadf64 addr:$src))),
202           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
203 def : Pat<(v2f64 (X86vzmovl (loadv2f64 addr:$src))),
204           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
205 def : Pat<(v2f64 (X86vzmovl (bc_v2f64 (loadv4f32 addr:$src)))),
206           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
207 def : Pat<(v2f64 (X86vzload addr:$src)),
208           (SUBREG_TO_REG (i64 0), (MOVSDrm addr:$src), sub_sd)>;
209 }
210
211 // Store scalar value to memory.
212 def MOVSSmr : SSI<0x11, MRMDestMem, (outs), (ins f32mem:$dst, FR32:$src),
213                   "movss\t{$src, $dst|$dst, $src}",
214                   [(store FR32:$src, addr:$dst)]>;
215 def MOVSDmr : SDI<0x11, MRMDestMem, (outs), (ins f64mem:$dst, FR64:$src),
216                   "movsd\t{$src, $dst|$dst, $src}",
217                   [(store FR64:$src, addr:$dst)]>;
218
219 def VMOVSSmr : SI<0x11, MRMDestMem, (outs), (ins f32mem:$dst, FR32:$src),
220                   "movss\t{$src, $dst|$dst, $src}",
221                   [(store FR32:$src, addr:$dst)]>, XS, VEX;
222 def VMOVSDmr : SI<0x11, MRMDestMem, (outs), (ins f64mem:$dst, FR64:$src),
223                   "movsd\t{$src, $dst|$dst, $src}",
224                   [(store FR64:$src, addr:$dst)]>, XD, VEX;
225
226 // Extract and store.
227 def : Pat<(store (f32 (vector_extract (v4f32 VR128:$src), (iPTR 0))),
228                  addr:$dst),
229           (MOVSSmr addr:$dst,
230                    (EXTRACT_SUBREG (v4f32 VR128:$src), sub_ss))>;
231 def : Pat<(store (f64 (vector_extract (v2f64 VR128:$src), (iPTR 0))),
232                  addr:$dst),
233           (MOVSDmr addr:$dst,
234                    (EXTRACT_SUBREG (v2f64 VR128:$src), sub_sd))>;
235
236 // Move Aligned/Unaligned floating point values
237 multiclass sse12_mov_packed<bits<8> opc, RegisterClass RC,
238                             X86MemOperand x86memop, PatFrag ld_frag,
239                             string asm, Domain d,
240                             bit IsReMaterializable = 1> {
241 let neverHasSideEffects = 1 in
242   def rr : PI<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src),
243               !strconcat(asm, "\t{$src, $dst|$dst, $src}"), [], d>;
244 let canFoldAsLoad = 1, isReMaterializable = IsReMaterializable in
245   def rm : PI<opc, MRMSrcMem, (outs RC:$dst), (ins x86memop:$src),
246               !strconcat(asm, "\t{$src, $dst|$dst, $src}"),
247                    [(set RC:$dst, (ld_frag addr:$src))], d>;
248 }
249
250 defm VMOVAPS : sse12_mov_packed<0x28, VR128, f128mem, alignedloadv4f32,
251                               "movaps", SSEPackedSingle>, VEX;
252 defm VMOVAPD : sse12_mov_packed<0x28, VR128, f128mem, alignedloadv2f64,
253                               "movapd", SSEPackedDouble>, OpSize, VEX;
254 defm VMOVUPS : sse12_mov_packed<0x10, VR128, f128mem, loadv4f32,
255                               "movups", SSEPackedSingle>, VEX;
256 defm VMOVUPD : sse12_mov_packed<0x10, VR128, f128mem, loadv2f64,
257                               "movupd", SSEPackedDouble, 0>, OpSize, VEX;
258
259 defm VMOVAPSY : sse12_mov_packed<0x28, VR256, f256mem, alignedloadv8f32,
260                               "movaps", SSEPackedSingle>, VEX;
261 defm VMOVAPDY : sse12_mov_packed<0x28, VR256, f256mem, alignedloadv4f64,
262                               "movapd", SSEPackedDouble>, OpSize, VEX;
263 defm VMOVUPSY : sse12_mov_packed<0x10, VR256, f256mem, loadv8f32,
264                               "movups", SSEPackedSingle>, VEX;
265 defm VMOVUPDY : sse12_mov_packed<0x10, VR256, f256mem, loadv4f64,
266                               "movupd", SSEPackedDouble, 0>, OpSize, VEX;
267 defm MOVAPS : sse12_mov_packed<0x28, VR128, f128mem, alignedloadv4f32,
268                               "movaps", SSEPackedSingle>, TB;
269 defm MOVAPD : sse12_mov_packed<0x28, VR128, f128mem, alignedloadv2f64,
270                               "movapd", SSEPackedDouble>, TB, OpSize;
271 defm MOVUPS : sse12_mov_packed<0x10, VR128, f128mem, loadv4f32,
272                               "movups", SSEPackedSingle>, TB;
273 defm MOVUPD : sse12_mov_packed<0x10, VR128, f128mem, loadv2f64,
274                               "movupd", SSEPackedDouble, 0>, TB, OpSize;
275
276 def VMOVAPSmr : VPSI<0x29, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
277                    "movaps\t{$src, $dst|$dst, $src}",
278                    [(alignedstore (v4f32 VR128:$src), addr:$dst)]>, VEX;
279 def VMOVAPDmr : VPDI<0x29, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
280                    "movapd\t{$src, $dst|$dst, $src}",
281                    [(alignedstore (v2f64 VR128:$src), addr:$dst)]>, VEX;
282 def VMOVUPSmr : VPSI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
283                    "movups\t{$src, $dst|$dst, $src}",
284                    [(store (v4f32 VR128:$src), addr:$dst)]>, VEX;
285 def VMOVUPDmr : VPDI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
286                    "movupd\t{$src, $dst|$dst, $src}",
287                    [(store (v2f64 VR128:$src), addr:$dst)]>, VEX;
288 def VMOVAPSYmr : VPSI<0x29, MRMDestMem, (outs), (ins f256mem:$dst, VR256:$src),
289                    "movaps\t{$src, $dst|$dst, $src}",
290                    [(alignedstore (v8f32 VR256:$src), addr:$dst)]>, VEX;
291 def VMOVAPDYmr : VPDI<0x29, MRMDestMem, (outs), (ins f256mem:$dst, VR256:$src),
292                    "movapd\t{$src, $dst|$dst, $src}",
293                    [(alignedstore (v4f64 VR256:$src), addr:$dst)]>, VEX;
294 def VMOVUPSYmr : VPSI<0x11, MRMDestMem, (outs), (ins f256mem:$dst, VR256:$src),
295                    "movups\t{$src, $dst|$dst, $src}",
296                    [(store (v8f32 VR256:$src), addr:$dst)]>, VEX;
297 def VMOVUPDYmr : VPDI<0x11, MRMDestMem, (outs), (ins f256mem:$dst, VR256:$src),
298                    "movupd\t{$src, $dst|$dst, $src}",
299                    [(store (v4f64 VR256:$src), addr:$dst)]>, VEX;
300
301 def : Pat<(int_x86_avx_loadu_ps_256 addr:$src), (VMOVUPSYrm addr:$src)>;
302 def : Pat<(int_x86_avx_storeu_ps_256 addr:$dst, VR256:$src),
303           (VMOVUPSYmr addr:$dst, VR256:$src)>;
304
305 def : Pat<(int_x86_avx_loadu_pd_256 addr:$src), (VMOVUPDYrm addr:$src)>;
306 def : Pat<(int_x86_avx_storeu_pd_256 addr:$dst, VR256:$src),
307           (VMOVUPDYmr addr:$dst, VR256:$src)>;
308
309 def MOVAPSmr : PSI<0x29, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
310                    "movaps\t{$src, $dst|$dst, $src}",
311                    [(alignedstore (v4f32 VR128:$src), addr:$dst)]>;
312 def MOVAPDmr : PDI<0x29, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
313                    "movapd\t{$src, $dst|$dst, $src}",
314                    [(alignedstore (v2f64 VR128:$src), addr:$dst)]>;
315 def MOVUPSmr : PSI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
316                    "movups\t{$src, $dst|$dst, $src}",
317                    [(store (v4f32 VR128:$src), addr:$dst)]>;
318 def MOVUPDmr : PDI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
319                    "movupd\t{$src, $dst|$dst, $src}",
320                    [(store (v2f64 VR128:$src), addr:$dst)]>;
321
322 // Intrinsic forms of MOVUPS/D load and store
323 def VMOVUPSmr_Int : VPSI<0x11, MRMDestMem, (outs),
324            (ins f128mem:$dst, VR128:$src),
325            "movups\t{$src, $dst|$dst, $src}",
326            [(int_x86_sse_storeu_ps addr:$dst, VR128:$src)]>, VEX;
327 def VMOVUPDmr_Int : VPDI<0x11, MRMDestMem, (outs),
328            (ins f128mem:$dst, VR128:$src),
329            "movupd\t{$src, $dst|$dst, $src}",
330            [(int_x86_sse2_storeu_pd addr:$dst, VR128:$src)]>, VEX;
331
332 def MOVUPSmr_Int : PSI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
333                        "movups\t{$src, $dst|$dst, $src}",
334                        [(int_x86_sse_storeu_ps addr:$dst, VR128:$src)]>;
335 def MOVUPDmr_Int : PDI<0x11, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
336                        "movupd\t{$src, $dst|$dst, $src}",
337                        [(int_x86_sse2_storeu_pd addr:$dst, VR128:$src)]>;
338
339 // Move Low/High packed floating point values
340 multiclass sse12_mov_hilo_packed<bits<8>opc, RegisterClass RC,
341                                  PatFrag mov_frag, string base_opc,
342                                  string asm_opr> {
343   def PSrm : PI<opc, MRMSrcMem,
344          (outs VR128:$dst), (ins VR128:$src1, f64mem:$src2),
345          !strconcat(base_opc, "s", asm_opr),
346      [(set RC:$dst,
347        (mov_frag RC:$src1,
348               (bc_v4f32 (v2f64 (scalar_to_vector (loadf64 addr:$src2))))))],
349               SSEPackedSingle>, TB;
350
351   def PDrm : PI<opc, MRMSrcMem,
352          (outs RC:$dst), (ins RC:$src1, f64mem:$src2),
353          !strconcat(base_opc, "d", asm_opr),
354      [(set RC:$dst, (v2f64 (mov_frag RC:$src1,
355                               (scalar_to_vector (loadf64 addr:$src2)))))],
356               SSEPackedDouble>, TB, OpSize;
357 }
358
359 let AddedComplexity = 20 in {
360   defm VMOVL : sse12_mov_hilo_packed<0x12, VR128, movlp, "movlp",
361                      "\t{$src2, $src1, $dst|$dst, $src1, $src2}">, VEX_4V;
362   defm VMOVH : sse12_mov_hilo_packed<0x16, VR128, movlhps, "movhp",
363                      "\t{$src2, $src1, $dst|$dst, $src1, $src2}">, VEX_4V;
364 }
365 let Constraints = "$src1 = $dst", AddedComplexity = 20 in {
366   defm MOVL : sse12_mov_hilo_packed<0x12, VR128, movlp, "movlp",
367                                    "\t{$src2, $dst|$dst, $src2}">;
368   defm MOVH : sse12_mov_hilo_packed<0x16, VR128, movlhps, "movhp",
369                                    "\t{$src2, $dst|$dst, $src2}">;
370 }
371
372 def VMOVLPSmr : VPSI<0x13, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
373                    "movlps\t{$src, $dst|$dst, $src}",
374                    [(store (f64 (vector_extract (bc_v2f64 (v4f32 VR128:$src)),
375                                  (iPTR 0))), addr:$dst)]>, VEX;
376 def VMOVLPDmr : VPDI<0x13, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
377                    "movlpd\t{$src, $dst|$dst, $src}",
378                    [(store (f64 (vector_extract (v2f64 VR128:$src),
379                                  (iPTR 0))), addr:$dst)]>, VEX;
380 def MOVLPSmr : PSI<0x13, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
381                    "movlps\t{$src, $dst|$dst, $src}",
382                    [(store (f64 (vector_extract (bc_v2f64 (v4f32 VR128:$src)),
383                                  (iPTR 0))), addr:$dst)]>;
384 def MOVLPDmr : PDI<0x13, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
385                    "movlpd\t{$src, $dst|$dst, $src}",
386                    [(store (f64 (vector_extract (v2f64 VR128:$src),
387                                  (iPTR 0))), addr:$dst)]>;
388
389 // v2f64 extract element 1 is always custom lowered to unpack high to low
390 // and extract element 0 so the non-store version isn't too horrible.
391 def VMOVHPSmr : VPSI<0x17, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
392                    "movhps\t{$src, $dst|$dst, $src}",
393                    [(store (f64 (vector_extract
394                                  (unpckh (bc_v2f64 (v4f32 VR128:$src)),
395                                          (undef)), (iPTR 0))), addr:$dst)]>,
396                    VEX;
397 def VMOVHPDmr : VPDI<0x17, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
398                    "movhpd\t{$src, $dst|$dst, $src}",
399                    [(store (f64 (vector_extract
400                                  (v2f64 (unpckh VR128:$src, (undef))),
401                                  (iPTR 0))), addr:$dst)]>,
402                    VEX;
403 def MOVHPSmr : PSI<0x17, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
404                    "movhps\t{$src, $dst|$dst, $src}",
405                    [(store (f64 (vector_extract
406                                  (unpckh (bc_v2f64 (v4f32 VR128:$src)),
407                                          (undef)), (iPTR 0))), addr:$dst)]>;
408 def MOVHPDmr : PDI<0x17, MRMDestMem, (outs), (ins f64mem:$dst, VR128:$src),
409                    "movhpd\t{$src, $dst|$dst, $src}",
410                    [(store (f64 (vector_extract
411                                  (v2f64 (unpckh VR128:$src, (undef))),
412                                  (iPTR 0))), addr:$dst)]>;
413
414 let AddedComplexity = 20 in {
415   def VMOVLHPSrr : VPSI<0x16, MRMSrcReg, (outs VR128:$dst),
416                                        (ins VR128:$src1, VR128:$src2),
417                       "movlhps\t{$src2, $src1, $dst|$dst, $src1, $src2}",
418                       [(set VR128:$dst,
419                         (v4f32 (movlhps VR128:$src1, VR128:$src2)))]>,
420                       VEX_4V;
421   def VMOVHLPSrr : VPSI<0x12, MRMSrcReg, (outs VR128:$dst),
422                                        (ins VR128:$src1, VR128:$src2),
423                       "movhlps\t{$src2, $src1, $dst|$dst, $src1, $src2}",
424                       [(set VR128:$dst,
425                         (v4f32 (movhlps VR128:$src1, VR128:$src2)))]>,
426                       VEX_4V;
427 }
428 let Constraints = "$src1 = $dst", AddedComplexity = 20 in {
429   def MOVLHPSrr : PSI<0x16, MRMSrcReg, (outs VR128:$dst),
430                                        (ins VR128:$src1, VR128:$src2),
431                       "movlhps\t{$src2, $dst|$dst, $src2}",
432                       [(set VR128:$dst,
433                         (v4f32 (movlhps VR128:$src1, VR128:$src2)))]>;
434   def MOVHLPSrr : PSI<0x12, MRMSrcReg, (outs VR128:$dst),
435                                        (ins VR128:$src1, VR128:$src2),
436                       "movhlps\t{$src2, $dst|$dst, $src2}",
437                       [(set VR128:$dst,
438                         (v4f32 (movhlps VR128:$src1, VR128:$src2)))]>;
439 }
440
441 def : Pat<(movlhps VR128:$src1, (bc_v4i32 (v2i64 (X86vzload addr:$src2)))),
442           (MOVHPSrm (v4i32 VR128:$src1), addr:$src2)>;
443 let AddedComplexity = 20 in {
444   def : Pat<(v4f32 (movddup VR128:$src, (undef))),
445             (MOVLHPSrr (v4f32 VR128:$src), (v4f32 VR128:$src))>;
446   def : Pat<(v2i64 (movddup VR128:$src, (undef))),
447             (MOVLHPSrr (v2i64 VR128:$src), (v2i64 VR128:$src))>;
448 }
449
450 //===----------------------------------------------------------------------===//
451 // SSE 1 & 2 - Conversion Instructions
452 //===----------------------------------------------------------------------===//
453
454 multiclass sse12_cvt_s<bits<8> opc, RegisterClass SrcRC, RegisterClass DstRC,
455                      SDNode OpNode, X86MemOperand x86memop, PatFrag ld_frag,
456                      string asm> {
457   def rr : SI<opc, MRMSrcReg, (outs DstRC:$dst), (ins SrcRC:$src), asm,
458                         [(set DstRC:$dst, (OpNode SrcRC:$src))]>;
459   def rm : SI<opc, MRMSrcMem, (outs DstRC:$dst), (ins x86memop:$src), asm,
460                         [(set DstRC:$dst, (OpNode (ld_frag addr:$src)))]>;
461 }
462
463 multiclass sse12_cvt_s_np<bits<8> opc, RegisterClass SrcRC, RegisterClass DstRC,
464                           X86MemOperand x86memop, string asm> {
465   def rr : SI<opc, MRMSrcReg, (outs DstRC:$dst), (ins SrcRC:$src), asm,
466                         []>;
467   def rm : SI<opc, MRMSrcMem, (outs DstRC:$dst), (ins x86memop:$src), asm,
468                         []>;
469 }
470
471 multiclass sse12_cvt_p<bits<8> opc, RegisterClass SrcRC, RegisterClass DstRC,
472                          SDNode OpNode, X86MemOperand x86memop, PatFrag ld_frag,
473                          string asm, Domain d> {
474   def rr : PI<opc, MRMSrcReg, (outs DstRC:$dst), (ins SrcRC:$src), asm,
475                         [(set DstRC:$dst, (OpNode SrcRC:$src))], d>;
476   def rm : PI<opc, MRMSrcMem, (outs DstRC:$dst), (ins x86memop:$src), asm,
477                         [(set DstRC:$dst, (OpNode (ld_frag addr:$src)))], d>;
478 }
479
480 multiclass sse12_vcvt_avx<bits<8> opc, RegisterClass SrcRC, RegisterClass DstRC,
481                           X86MemOperand x86memop, string asm> {
482   def rr : SI<opc, MRMSrcReg, (outs DstRC:$dst), (ins DstRC:$src1, SrcRC:$src),
483               !strconcat(asm,"\t{$src, $src1, $dst|$dst, $src1, $src}"), []>;
484   def rm : SI<opc, MRMSrcMem, (outs DstRC:$dst),
485               (ins DstRC:$src1, x86memop:$src),
486               !strconcat(asm,"\t{$src, $src1, $dst|$dst, $src1, $src}"), []>;
487 }
488
489 defm VCVTTSS2SI   : sse12_cvt_s<0x2C, FR32, GR32, fp_to_sint, f32mem, loadf32,
490                                 "cvttss2si\t{$src, $dst|$dst, $src}">, XS, VEX;
491 defm VCVTTSS2SI64 : sse12_cvt_s<0x2C, FR32, GR64, fp_to_sint, f32mem, loadf32,
492                                 "cvttss2si\t{$src, $dst|$dst, $src}">, XS, VEX,
493                                 VEX_W;
494 defm VCVTTSD2SI   : sse12_cvt_s<0x2C, FR64, GR32, fp_to_sint, f64mem, loadf64,
495                                 "cvttsd2si\t{$src, $dst|$dst, $src}">, XD, VEX;
496 defm VCVTTSD2SI64 : sse12_cvt_s<0x2C, FR64, GR64, fp_to_sint, f64mem, loadf64,
497                                 "cvttsd2si\t{$src, $dst|$dst, $src}">, XD,
498                                 VEX, VEX_W;
499
500 // The assembler can recognize rr 64-bit instructions by seeing a rxx
501 // register, but the same isn't true when only using memory operands,
502 // provide other assembly "l" and "q" forms to address this explicitly
503 // where appropriate to do so.
504 defm VCVTSI2SS   : sse12_vcvt_avx<0x2A, GR32, FR32, i32mem, "cvtsi2ss">, XS,
505                                   VEX_4V;
506 defm VCVTSI2SS64 : sse12_vcvt_avx<0x2A, GR64, FR32, i64mem, "cvtsi2ss{q}">, XS,
507                                   VEX_4V, VEX_W;
508 defm VCVTSI2SD   : sse12_vcvt_avx<0x2A, GR32, FR64, i32mem, "cvtsi2sd">, XD,
509                                   VEX_4V;
510 defm VCVTSI2SDL  : sse12_vcvt_avx<0x2A, GR32, FR64, i32mem, "cvtsi2sd{l}">, XD,
511                                   VEX_4V;
512 defm VCVTSI2SD64 : sse12_vcvt_avx<0x2A, GR64, FR64, i64mem, "cvtsi2sd{q}">, XD,
513                                   VEX_4V, VEX_W;
514
515 let Predicates = [HasAVX] in {
516   def : Pat<(f32 (sint_to_fp (loadi32 addr:$src))),
517             (VCVTSI2SSrm (f32 (IMPLICIT_DEF)), addr:$src)>;
518   def : Pat<(f32 (sint_to_fp (loadi64 addr:$src))),
519             (VCVTSI2SS64rm (f32 (IMPLICIT_DEF)), addr:$src)>;
520   def : Pat<(f64 (sint_to_fp (loadi32 addr:$src))),
521             (VCVTSI2SDrm (f64 (IMPLICIT_DEF)), addr:$src)>;
522   def : Pat<(f64 (sint_to_fp (loadi64 addr:$src))),
523             (VCVTSI2SD64rm (f64 (IMPLICIT_DEF)), addr:$src)>;
524
525   def : Pat<(f32 (sint_to_fp GR32:$src)),
526             (VCVTSI2SSrr (f32 (IMPLICIT_DEF)), GR32:$src)>;
527   def : Pat<(f32 (sint_to_fp GR64:$src)),
528             (VCVTSI2SS64rr (f32 (IMPLICIT_DEF)), GR64:$src)>;
529   def : Pat<(f64 (sint_to_fp GR32:$src)),
530             (VCVTSI2SDrr (f64 (IMPLICIT_DEF)), GR32:$src)>;
531   def : Pat<(f64 (sint_to_fp GR64:$src)),
532             (VCVTSI2SD64rr (f64 (IMPLICIT_DEF)), GR64:$src)>;
533 }
534
535 defm CVTTSS2SI : sse12_cvt_s<0x2C, FR32, GR32, fp_to_sint, f32mem, loadf32,
536                       "cvttss2si\t{$src, $dst|$dst, $src}">, XS;
537 defm CVTTSS2SI64 : sse12_cvt_s<0x2C, FR32, GR64, fp_to_sint, f32mem, loadf32,
538                       "cvttss2si{q}\t{$src, $dst|$dst, $src}">, XS, REX_W;
539 defm CVTTSD2SI : sse12_cvt_s<0x2C, FR64, GR32, fp_to_sint, f64mem, loadf64,
540                       "cvttsd2si\t{$src, $dst|$dst, $src}">, XD;
541 defm CVTTSD2SI64 : sse12_cvt_s<0x2C, FR64, GR64, fp_to_sint, f64mem, loadf64,
542                       "cvttsd2si{q}\t{$src, $dst|$dst, $src}">, XD, REX_W;
543 defm CVTSI2SS  : sse12_cvt_s<0x2A, GR32, FR32, sint_to_fp, i32mem, loadi32,
544                       "cvtsi2ss\t{$src, $dst|$dst, $src}">, XS;
545 defm CVTSI2SS64 : sse12_cvt_s<0x2A, GR64, FR32, sint_to_fp, i64mem, loadi64,
546                       "cvtsi2ss{q}\t{$src, $dst|$dst, $src}">, XS, REX_W;
547 defm CVTSI2SD  : sse12_cvt_s<0x2A, GR32, FR64, sint_to_fp, i32mem, loadi32,
548                       "cvtsi2sd\t{$src, $dst|$dst, $src}">, XD;
549 defm CVTSI2SD64 : sse12_cvt_s<0x2A, GR64, FR64, sint_to_fp, i64mem, loadi64,
550                       "cvtsi2sd{q}\t{$src, $dst|$dst, $src}">, XD, REX_W;
551
552 // Conversion Instructions Intrinsics - Match intrinsics which expect MM
553 // and/or XMM operand(s).
554
555 multiclass sse12_cvt_sint<bits<8> opc, RegisterClass SrcRC, RegisterClass DstRC,
556                          Intrinsic Int, X86MemOperand x86memop, PatFrag ld_frag,
557                          string asm> {
558   def rr : SI<opc, MRMSrcReg, (outs DstRC:$dst), (ins SrcRC:$src),
559               !strconcat(asm, "\t{$src, $dst|$dst, $src}"),
560               [(set DstRC:$dst, (Int SrcRC:$src))]>;
561   def rm : SI<opc, MRMSrcMem, (outs DstRC:$dst), (ins x86memop:$src),
562               !strconcat(asm, "\t{$src, $dst|$dst, $src}"),
563               [(set DstRC:$dst, (Int (ld_frag addr:$src)))]>;
564 }
565
566 multiclass sse12_cvt_sint_3addr<bits<8> opc, RegisterClass SrcRC,
567                     RegisterClass DstRC, Intrinsic Int, X86MemOperand x86memop,
568                     PatFrag ld_frag, string asm, bit Is2Addr = 1> {
569   def rr : SI<opc, MRMSrcReg, (outs DstRC:$dst), (ins DstRC:$src1, SrcRC:$src2),
570               !if(Is2Addr,
571                   !strconcat(asm, "\t{$src2, $dst|$dst, $src2}"),
572                   !strconcat(asm, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
573               [(set DstRC:$dst, (Int DstRC:$src1, SrcRC:$src2))]>;
574   def rm : SI<opc, MRMSrcMem, (outs DstRC:$dst),
575               (ins DstRC:$src1, x86memop:$src2),
576               !if(Is2Addr,
577                   !strconcat(asm, "\t{$src2, $dst|$dst, $src2}"),
578                   !strconcat(asm, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
579               [(set DstRC:$dst, (Int DstRC:$src1, (ld_frag addr:$src2)))]>;
580 }
581
582 defm Int_VCVTSS2SI : sse12_cvt_sint<0x2D, VR128, GR32, int_x86_sse_cvtss2si,
583                       f32mem, load, "cvtss2si">, XS, VEX;
584 defm Int_VCVTSS2SI64 : sse12_cvt_sint<0x2D, VR128, GR64,
585                         int_x86_sse_cvtss2si64, f32mem, load, "cvtss2si">,
586                         XS, VEX, VEX_W;
587 defm Int_VCVTSD2SI : sse12_cvt_sint<0x2D, VR128, GR32, int_x86_sse2_cvtsd2si,
588                       f128mem, load, "cvtsd2si">, XD, VEX;
589 defm Int_VCVTSD2SI64 : sse12_cvt_sint<0x2D, VR128, GR64,
590                       int_x86_sse2_cvtsd2si64, f128mem, load, "cvtsd2si">,
591                       XD, VEX, VEX_W;
592
593 // FIXME: The asm matcher has a hack to ignore instructions with _Int and Int_
594 // Get rid of this hack or rename the intrinsics, there are several
595 // intructions that only match with the intrinsic form, why create duplicates
596 // to let them be recognized by the assembler?
597 defm VCVTSD2SI_alt : sse12_cvt_s_np<0x2D, FR64, GR32, f64mem,
598                       "cvtsd2si\t{$src, $dst|$dst, $src}">, XD, VEX;
599 defm VCVTSD2SI64   : sse12_cvt_s_np<0x2D, FR64, GR64, f64mem,
600                       "cvtsd2si\t{$src, $dst|$dst, $src}">, XD, VEX, VEX_W;
601 defm Int_CVTSS2SI : sse12_cvt_sint<0x2D, VR128, GR32, int_x86_sse_cvtss2si,
602                       f32mem, load, "cvtss2si">, XS;
603 defm Int_CVTSS2SI64 : sse12_cvt_sint<0x2D, VR128, GR64, int_x86_sse_cvtss2si64,
604                       f32mem, load, "cvtss2si{q}">, XS, REX_W;
605 defm CVTSD2SI : sse12_cvt_sint<0x2D, VR128, GR32, int_x86_sse2_cvtsd2si,
606                 f128mem, load, "cvtsd2si{l}">, XD;
607 defm CVTSD2SI64 : sse12_cvt_sint<0x2D, VR128, GR64, int_x86_sse2_cvtsd2si64,
608                   f128mem, load, "cvtsd2si{q}">, XD, REX_W;
609
610
611 defm Int_VCVTSI2SS : sse12_cvt_sint_3addr<0x2A, GR32, VR128,
612           int_x86_sse_cvtsi2ss, i32mem, loadi32, "cvtsi2ss", 0>, XS, VEX_4V;
613 defm Int_VCVTSI2SS64 : sse12_cvt_sint_3addr<0x2A, GR64, VR128,
614           int_x86_sse_cvtsi642ss, i64mem, loadi64, "cvtsi2ss", 0>, XS, VEX_4V,
615           VEX_W;
616 defm Int_VCVTSI2SD : sse12_cvt_sint_3addr<0x2A, GR32, VR128,
617           int_x86_sse2_cvtsi2sd, i32mem, loadi32, "cvtsi2sd", 0>, XD, VEX_4V;
618 defm Int_VCVTSI2SD64 : sse12_cvt_sint_3addr<0x2A, GR64, VR128,
619           int_x86_sse2_cvtsi642sd, i64mem, loadi64, "cvtsi2sd", 0>, XD,
620           VEX_4V, VEX_W;
621
622 let Constraints = "$src1 = $dst" in {
623   defm Int_CVTSI2SS : sse12_cvt_sint_3addr<0x2A, GR32, VR128,
624                         int_x86_sse_cvtsi2ss, i32mem, loadi32,
625                         "cvtsi2ss">, XS;
626   defm Int_CVTSI2SS64 : sse12_cvt_sint_3addr<0x2A, GR64, VR128,
627                         int_x86_sse_cvtsi642ss, i64mem, loadi64,
628                         "cvtsi2ss{q}">, XS, REX_W;
629   defm Int_CVTSI2SD : sse12_cvt_sint_3addr<0x2A, GR32, VR128,
630                         int_x86_sse2_cvtsi2sd, i32mem, loadi32,
631                         "cvtsi2sd">, XD;
632   defm Int_CVTSI2SD64 : sse12_cvt_sint_3addr<0x2A, GR64, VR128,
633                         int_x86_sse2_cvtsi642sd, i64mem, loadi64,
634                         "cvtsi2sd">, XD, REX_W;
635 }
636
637 /// SSE 1 Only
638
639 // Aliases for intrinsics
640 defm Int_VCVTTSS2SI : sse12_cvt_sint<0x2C, VR128, GR32, int_x86_sse_cvttss2si,
641                                     f32mem, load, "cvttss2si">, XS, VEX;
642 defm Int_VCVTTSS2SI64 : sse12_cvt_sint<0x2C, VR128, GR64,
643                                     int_x86_sse_cvttss2si64, f32mem, load,
644                                     "cvttss2si">, XS, VEX, VEX_W;
645 defm Int_VCVTTSD2SI : sse12_cvt_sint<0x2C, VR128, GR32, int_x86_sse2_cvttsd2si,
646                                     f128mem, load, "cvttsd2si">, XD, VEX;
647 defm Int_VCVTTSD2SI64 : sse12_cvt_sint<0x2C, VR128, GR64,
648                                     int_x86_sse2_cvttsd2si64, f128mem, load,
649                                     "cvttsd2si">, XD, VEX, VEX_W;
650 defm Int_CVTTSS2SI : sse12_cvt_sint<0x2C, VR128, GR32, int_x86_sse_cvttss2si,
651                                     f32mem, load, "cvttss2si">, XS;
652 defm Int_CVTTSS2SI64 : sse12_cvt_sint<0x2C, VR128, GR64,
653                                     int_x86_sse_cvttss2si64, f32mem, load,
654                                     "cvttss2si{q}">, XS, REX_W;
655 defm Int_CVTTSD2SI : sse12_cvt_sint<0x2C, VR128, GR32, int_x86_sse2_cvttsd2si,
656                                     f128mem, load, "cvttsd2si">, XD;
657 defm Int_CVTTSD2SI64 : sse12_cvt_sint<0x2C, VR128, GR64,
658                                     int_x86_sse2_cvttsd2si64, f128mem, load,
659                                     "cvttsd2si{q}">, XD, REX_W;
660
661 let Pattern = []<dag> in {
662 defm VCVTSS2SI   : sse12_cvt_s<0x2D, FR32, GR32, undef, f32mem, load,
663                                "cvtss2si{l}\t{$src, $dst|$dst, $src}">, XS, VEX;
664 defm VCVTSS2SI64 : sse12_cvt_s<0x2D, FR32, GR64, undef, f32mem, load,
665                                "cvtss2si\t{$src, $dst|$dst, $src}">, XS, VEX,
666                                VEX_W;
667 defm VCVTDQ2PS   : sse12_cvt_p<0x5B, VR128, VR128, undef, i128mem, load,
668                                "cvtdq2ps\t{$src, $dst|$dst, $src}",
669                                SSEPackedSingle>, TB, VEX;
670 defm VCVTDQ2PSY  : sse12_cvt_p<0x5B, VR256, VR256, undef, i256mem, load,
671                                "cvtdq2ps\t{$src, $dst|$dst, $src}",
672                                SSEPackedSingle>, TB, VEX;
673 }
674 let Pattern = []<dag> in {
675 defm CVTSS2SI : sse12_cvt_s<0x2D, FR32, GR32, undef, f32mem, load /*dummy*/,
676                           "cvtss2si{l}\t{$src, $dst|$dst, $src}">, XS;
677 defm CVTSS2SI64 : sse12_cvt_s<0x2D, FR32, GR64, undef, f32mem, load /*dummy*/,
678                           "cvtss2si{q}\t{$src, $dst|$dst, $src}">, XS, REX_W;
679 defm CVTDQ2PS : sse12_cvt_p<0x5B, VR128, VR128, undef, i128mem, load /*dummy*/,
680                             "cvtdq2ps\t{$src, $dst|$dst, $src}",
681                             SSEPackedSingle>, TB; /* PD SSE3 form is avaiable */
682 }
683
684 /// SSE 2 Only
685
686 // Convert scalar double to scalar single
687 def VCVTSD2SSrr  : VSDI<0x5A, MRMSrcReg, (outs FR32:$dst),
688                        (ins FR64:$src1, FR64:$src2),
689                       "cvtsd2ss\t{$src2, $src1, $dst|$dst, $src1, $src2}", []>,
690                       VEX_4V;
691 def VCVTSD2SSrm  : I<0x5A, MRMSrcMem, (outs FR32:$dst),
692                        (ins FR64:$src1, f64mem:$src2),
693                       "vcvtsd2ss\t{$src2, $src1, $dst|$dst, $src1, $src2}",
694                       []>, XD, Requires<[HasAVX, OptForSize]>, VEX_4V;
695 def : Pat<(f32 (fround FR64:$src)), (VCVTSD2SSrr FR64:$src, FR64:$src)>,
696         Requires<[HasAVX]>;
697
698 def CVTSD2SSrr  : SDI<0x5A, MRMSrcReg, (outs FR32:$dst), (ins FR64:$src),
699                       "cvtsd2ss\t{$src, $dst|$dst, $src}",
700                       [(set FR32:$dst, (fround FR64:$src))]>;
701 def CVTSD2SSrm  : I<0x5A, MRMSrcMem, (outs FR32:$dst), (ins f64mem:$src),
702                       "cvtsd2ss\t{$src, $dst|$dst, $src}",
703                       [(set FR32:$dst, (fround (loadf64 addr:$src)))]>, XD,
704                   Requires<[HasSSE2, OptForSize]>;
705
706 defm Int_VCVTSD2SS: sse12_cvt_sint_3addr<0x5A, VR128, VR128,
707                       int_x86_sse2_cvtsd2ss, f64mem, load, "cvtsd2ss", 0>,
708                       XS, VEX_4V;
709 let Constraints = "$src1 = $dst" in
710 defm Int_CVTSD2SS: sse12_cvt_sint_3addr<0x5A, VR128, VR128,
711                       int_x86_sse2_cvtsd2ss, f64mem, load, "cvtsd2ss">, XS;
712
713 // Convert scalar single to scalar double
714 // SSE2 instructions with XS prefix
715 def VCVTSS2SDrr : I<0x5A, MRMSrcReg, (outs FR64:$dst),
716                     (ins FR32:$src1, FR32:$src2),
717                     "vcvtss2sd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
718                     []>, XS, Requires<[HasAVX]>, VEX_4V;
719 def VCVTSS2SDrm : I<0x5A, MRMSrcMem, (outs FR64:$dst),
720                     (ins FR32:$src1, f32mem:$src2),
721                     "vcvtss2sd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
722                     []>, XS, VEX_4V, Requires<[HasAVX, OptForSize]>;
723
724 let Predicates = [HasAVX] in {
725   def : Pat<(f64 (fextend FR32:$src)),
726             (VCVTSS2SDrr FR32:$src, FR32:$src)>;
727   def : Pat<(fextend (loadf32 addr:$src)),
728             (VCVTSS2SDrm (f32 (IMPLICIT_DEF)), addr:$src)>;
729   def : Pat<(extloadf32 addr:$src),
730             (VCVTSS2SDrm (f32 (IMPLICIT_DEF)), addr:$src)>;
731 }
732
733 def CVTSS2SDrr : I<0x5A, MRMSrcReg, (outs FR64:$dst), (ins FR32:$src),
734                    "cvtss2sd\t{$src, $dst|$dst, $src}",
735                    [(set FR64:$dst, (fextend FR32:$src))]>, XS,
736                  Requires<[HasSSE2]>;
737 def CVTSS2SDrm : I<0x5A, MRMSrcMem, (outs FR64:$dst), (ins f32mem:$src),
738                    "cvtss2sd\t{$src, $dst|$dst, $src}",
739                    [(set FR64:$dst, (extloadf32 addr:$src))]>, XS,
740                  Requires<[HasSSE2, OptForSize]>;
741
742 def Int_VCVTSS2SDrr: I<0x5A, MRMSrcReg,
743                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
744                     "vcvtss2sd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
745                     [(set VR128:$dst, (int_x86_sse2_cvtss2sd VR128:$src1,
746                                        VR128:$src2))]>, XS, VEX_4V,
747                     Requires<[HasAVX]>;
748 def Int_VCVTSS2SDrm: I<0x5A, MRMSrcMem,
749                       (outs VR128:$dst), (ins VR128:$src1, f32mem:$src2),
750                     "vcvtss2sd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
751                     [(set VR128:$dst, (int_x86_sse2_cvtss2sd VR128:$src1,
752                                        (load addr:$src2)))]>, XS, VEX_4V,
753                     Requires<[HasAVX]>;
754 let Constraints = "$src1 = $dst" in { // SSE2 instructions with XS prefix
755 def Int_CVTSS2SDrr: I<0x5A, MRMSrcReg,
756                       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
757                     "cvtss2sd\t{$src2, $dst|$dst, $src2}",
758                     [(set VR128:$dst, (int_x86_sse2_cvtss2sd VR128:$src1,
759                                        VR128:$src2))]>, XS,
760                     Requires<[HasSSE2]>;
761 def Int_CVTSS2SDrm: I<0x5A, MRMSrcMem,
762                       (outs VR128:$dst), (ins VR128:$src1, f32mem:$src2),
763                     "cvtss2sd\t{$src2, $dst|$dst, $src2}",
764                     [(set VR128:$dst, (int_x86_sse2_cvtss2sd VR128:$src1,
765                                        (load addr:$src2)))]>, XS,
766                     Requires<[HasSSE2]>;
767 }
768
769 def : Pat<(extloadf32 addr:$src),
770           (CVTSS2SDrr (MOVSSrm addr:$src))>,
771       Requires<[HasSSE2, OptForSpeed]>;
772
773 // Convert doubleword to packed single/double fp
774 // SSE2 instructions without OpSize prefix
775 def Int_VCVTDQ2PSrr : I<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
776                        "vcvtdq2ps\t{$src, $dst|$dst, $src}",
777                        [(set VR128:$dst, (int_x86_sse2_cvtdq2ps VR128:$src))]>,
778                      TB, VEX, Requires<[HasAVX]>;
779 def Int_VCVTDQ2PSrm : I<0x5B, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
780                       "vcvtdq2ps\t{$src, $dst|$dst, $src}",
781                       [(set VR128:$dst, (int_x86_sse2_cvtdq2ps
782                                         (bitconvert (memopv2i64 addr:$src))))]>,
783                      TB, VEX, Requires<[HasAVX]>;
784 def Int_CVTDQ2PSrr : I<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
785                        "cvtdq2ps\t{$src, $dst|$dst, $src}",
786                        [(set VR128:$dst, (int_x86_sse2_cvtdq2ps VR128:$src))]>,
787                      TB, Requires<[HasSSE2]>;
788 def Int_CVTDQ2PSrm : I<0x5B, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
789                       "cvtdq2ps\t{$src, $dst|$dst, $src}",
790                       [(set VR128:$dst, (int_x86_sse2_cvtdq2ps
791                                         (bitconvert (memopv2i64 addr:$src))))]>,
792                      TB, Requires<[HasSSE2]>;
793
794 // FIXME: why the non-intrinsic version is described as SSE3?
795 // SSE2 instructions with XS prefix
796 def Int_VCVTDQ2PDrr : I<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
797                        "vcvtdq2pd\t{$src, $dst|$dst, $src}",
798                        [(set VR128:$dst, (int_x86_sse2_cvtdq2pd VR128:$src))]>,
799                      XS, VEX, Requires<[HasAVX]>;
800 def Int_VCVTDQ2PDrm : I<0xE6, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
801                        "vcvtdq2pd\t{$src, $dst|$dst, $src}",
802                        [(set VR128:$dst, (int_x86_sse2_cvtdq2pd
803                                         (bitconvert (memopv2i64 addr:$src))))]>,
804                      XS, VEX, Requires<[HasAVX]>;
805 def Int_CVTDQ2PDrr : I<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
806                        "cvtdq2pd\t{$src, $dst|$dst, $src}",
807                        [(set VR128:$dst, (int_x86_sse2_cvtdq2pd VR128:$src))]>,
808                      XS, Requires<[HasSSE2]>;
809 def Int_CVTDQ2PDrm : I<0xE6, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
810                      "cvtdq2pd\t{$src, $dst|$dst, $src}",
811                      [(set VR128:$dst, (int_x86_sse2_cvtdq2pd
812                                         (bitconvert (memopv2i64 addr:$src))))]>,
813                      XS, Requires<[HasSSE2]>;
814
815
816 // Convert packed single/double fp to doubleword
817 def VCVTPS2DQrr : VPDI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
818                        "cvtps2dq\t{$src, $dst|$dst, $src}", []>, VEX;
819 def VCVTPS2DQrm : VPDI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
820                        "cvtps2dq\t{$src, $dst|$dst, $src}", []>, VEX;
821 def VCVTPS2DQYrr : VPDI<0x5B, MRMSrcReg, (outs VR256:$dst), (ins VR256:$src),
822                         "cvtps2dq\t{$src, $dst|$dst, $src}", []>, VEX;
823 def VCVTPS2DQYrm : VPDI<0x5B, MRMSrcMem, (outs VR256:$dst), (ins f256mem:$src),
824                         "cvtps2dq\t{$src, $dst|$dst, $src}", []>, VEX;
825 def CVTPS2DQrr : PDI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
826                      "cvtps2dq\t{$src, $dst|$dst, $src}", []>;
827 def CVTPS2DQrm : PDI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
828                      "cvtps2dq\t{$src, $dst|$dst, $src}", []>;
829
830 def Int_VCVTPS2DQrr : VPDI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
831                         "cvtps2dq\t{$src, $dst|$dst, $src}",
832                         [(set VR128:$dst, (int_x86_sse2_cvtps2dq VR128:$src))]>,
833                         VEX;
834 def Int_VCVTPS2DQrm : VPDI<0x5B, MRMSrcMem, (outs VR128:$dst),
835                          (ins f128mem:$src),
836                          "cvtps2dq\t{$src, $dst|$dst, $src}",
837                          [(set VR128:$dst, (int_x86_sse2_cvtps2dq
838                                             (memop addr:$src)))]>, VEX;
839 def Int_CVTPS2DQrr : PDI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
840                         "cvtps2dq\t{$src, $dst|$dst, $src}",
841                         [(set VR128:$dst, (int_x86_sse2_cvtps2dq VR128:$src))]>;
842 def Int_CVTPS2DQrm : PDI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
843                          "cvtps2dq\t{$src, $dst|$dst, $src}",
844                          [(set VR128:$dst, (int_x86_sse2_cvtps2dq
845                                             (memop addr:$src)))]>;
846
847 // SSE2 packed instructions with XD prefix
848 def Int_VCVTPD2DQrr : I<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
849                        "vcvtpd2dq\t{$src, $dst|$dst, $src}",
850                        [(set VR128:$dst, (int_x86_sse2_cvtpd2dq VR128:$src))]>,
851                      XD, VEX, Requires<[HasAVX]>;
852 def Int_VCVTPD2DQrm : I<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
853                        "vcvtpd2dq\t{$src, $dst|$dst, $src}",
854                        [(set VR128:$dst, (int_x86_sse2_cvtpd2dq
855                                           (memop addr:$src)))]>,
856                      XD, VEX, Requires<[HasAVX]>;
857 def Int_CVTPD2DQrr : I<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
858                        "cvtpd2dq\t{$src, $dst|$dst, $src}",
859                        [(set VR128:$dst, (int_x86_sse2_cvtpd2dq VR128:$src))]>,
860                      XD, Requires<[HasSSE2]>;
861 def Int_CVTPD2DQrm : I<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
862                        "cvtpd2dq\t{$src, $dst|$dst, $src}",
863                        [(set VR128:$dst, (int_x86_sse2_cvtpd2dq
864                                           (memop addr:$src)))]>,
865                      XD, Requires<[HasSSE2]>;
866
867
868 // Convert with truncation packed single/double fp to doubleword
869 // SSE2 packed instructions with XS prefix
870 def VCVTTPS2DQrr : VSSI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
871                       "cvttps2dq\t{$src, $dst|$dst, $src}", []>, VEX;
872 def VCVTTPS2DQrm : VSSI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
873                       "cvttps2dq\t{$src, $dst|$dst, $src}", []>, VEX;
874 def VCVTTPS2DQYrr : VSSI<0x5B, MRMSrcReg, (outs VR256:$dst), (ins VR256:$src),
875                       "cvttps2dq\t{$src, $dst|$dst, $src}", []>, VEX;
876 def VCVTTPS2DQYrm : VSSI<0x5B, MRMSrcMem, (outs VR256:$dst), (ins f256mem:$src),
877                       "cvttps2dq\t{$src, $dst|$dst, $src}", []>, VEX;
878 def CVTTPS2DQrr : SSI<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
879                       "cvttps2dq\t{$src, $dst|$dst, $src}",
880                       [(set VR128:$dst,
881                             (int_x86_sse2_cvttps2dq VR128:$src))]>;
882 def CVTTPS2DQrm : SSI<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
883                       "cvttps2dq\t{$src, $dst|$dst, $src}",
884                       [(set VR128:$dst,
885                             (int_x86_sse2_cvttps2dq (memop addr:$src)))]>;
886
887 def Int_VCVTTPS2DQrr : I<0x5B, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
888                         "vcvttps2dq\t{$src, $dst|$dst, $src}",
889                         [(set VR128:$dst,
890                               (int_x86_sse2_cvttps2dq VR128:$src))]>,
891                       XS, VEX, Requires<[HasAVX]>;
892 def Int_VCVTTPS2DQrm : I<0x5B, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
893                         "vcvttps2dq\t{$src, $dst|$dst, $src}",
894                         [(set VR128:$dst, (int_x86_sse2_cvttps2dq
895                                            (memop addr:$src)))]>,
896                       XS, VEX, Requires<[HasAVX]>;
897
898 def : Pat<(v4f32 (sint_to_fp (v4i32 VR128:$src))),
899           (Int_CVTDQ2PSrr VR128:$src)>, Requires<[HasSSE2]>;
900 def : Pat<(v4i32 (fp_to_sint (v4f32 VR128:$src))),
901           (CVTTPS2DQrr VR128:$src)>, Requires<[HasSSE2]>;
902
903 def : Pat<(v8f32 (sint_to_fp (v8i32 VR256:$src))),
904           (VCVTDQ2PSYrr VR256:$src)>, Requires<[HasAVX]>;
905 def : Pat<(v8i32 (fp_to_sint (v8f32 VR256:$src))),
906           (VCVTTPS2DQYrr VR256:$src)>, Requires<[HasAVX]>;
907
908 def Int_VCVTTPD2DQrr : VPDI<0xE6, MRMSrcReg, (outs VR128:$dst),
909                             (ins VR128:$src),
910                           "cvttpd2dq\t{$src, $dst|$dst, $src}",
911                        [(set VR128:$dst, (int_x86_sse2_cvttpd2dq VR128:$src))]>,
912                        VEX;
913 def Int_VCVTTPD2DQrm : VPDI<0xE6, MRMSrcMem, (outs VR128:$dst),
914                           (ins f128mem:$src),
915                           "cvttpd2dq\t{$src, $dst|$dst, $src}",
916                           [(set VR128:$dst, (int_x86_sse2_cvttpd2dq
917                                              (memop addr:$src)))]>, VEX;
918 def CVTTPD2DQrr : PDI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
919                       "cvttpd2dq\t{$src, $dst|$dst, $src}",
920                       [(set VR128:$dst, (int_x86_sse2_cvttpd2dq VR128:$src))]>;
921 def CVTTPD2DQrm : PDI<0xE6, MRMSrcMem, (outs VR128:$dst),(ins f128mem:$src),
922                       "cvttpd2dq\t{$src, $dst|$dst, $src}",
923                       [(set VR128:$dst, (int_x86_sse2_cvttpd2dq
924                                         (memop addr:$src)))]>;
925
926 // The assembler can recognize rr 256-bit instructions by seeing a ymm
927 // register, but the same isn't true when using memory operands instead.
928 // Provide other assembly rr and rm forms to address this explicitly.
929 def VCVTTPD2DQrr : VPDI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
930                         "cvttpd2dq\t{$src, $dst|$dst, $src}", []>, VEX;
931 def VCVTTPD2DQXrYr : VPDI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR256:$src),
932                           "cvttpd2dq\t{$src, $dst|$dst, $src}", []>, VEX;
933
934 // XMM only
935 def VCVTTPD2DQXrr : VPDI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
936                          "cvttpd2dqx\t{$src, $dst|$dst, $src}", []>, VEX;
937 def VCVTTPD2DQXrm : VPDI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
938                          "cvttpd2dqx\t{$src, $dst|$dst, $src}", []>, VEX;
939
940 // YMM only
941 def VCVTTPD2DQYrr : VPDI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR256:$src),
942                          "cvttpd2dqy\t{$src, $dst|$dst, $src}", []>, VEX;
943 def VCVTTPD2DQYrm : VPDI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f256mem:$src),
944                          "cvttpd2dqy\t{$src, $dst|$dst, $src}", []>, VEX, VEX_L;
945
946 // Convert packed single to packed double
947 let Predicates = [HasAVX] in {
948                   // SSE2 instructions without OpSize prefix
949 def VCVTPS2PDrr : I<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
950                      "vcvtps2pd\t{$src, $dst|$dst, $src}", []>, VEX;
951 def VCVTPS2PDrm : I<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
952                      "vcvtps2pd\t{$src, $dst|$dst, $src}", []>, VEX;
953 def VCVTPS2PDYrr : I<0x5A, MRMSrcReg, (outs VR256:$dst), (ins VR128:$src),
954                      "vcvtps2pd\t{$src, $dst|$dst, $src}", []>, VEX;
955 def VCVTPS2PDYrm : I<0x5A, MRMSrcMem, (outs VR256:$dst), (ins f128mem:$src),
956                      "vcvtps2pd\t{$src, $dst|$dst, $src}", []>, VEX;
957 }
958 def CVTPS2PDrr : I<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
959                        "cvtps2pd\t{$src, $dst|$dst, $src}", []>, TB;
960 def CVTPS2PDrm : I<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
961                        "cvtps2pd\t{$src, $dst|$dst, $src}", []>, TB;
962
963 def Int_VCVTPS2PDrr : I<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
964                        "vcvtps2pd\t{$src, $dst|$dst, $src}",
965                        [(set VR128:$dst, (int_x86_sse2_cvtps2pd VR128:$src))]>,
966                      VEX, Requires<[HasAVX]>;
967 def Int_VCVTPS2PDrm : I<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
968                        "vcvtps2pd\t{$src, $dst|$dst, $src}",
969                        [(set VR128:$dst, (int_x86_sse2_cvtps2pd
970                                           (load addr:$src)))]>,
971                      VEX, Requires<[HasAVX]>;
972 def Int_CVTPS2PDrr : I<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
973                        "cvtps2pd\t{$src, $dst|$dst, $src}",
974                        [(set VR128:$dst, (int_x86_sse2_cvtps2pd VR128:$src))]>,
975                      TB, Requires<[HasSSE2]>;
976 def Int_CVTPS2PDrm : I<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
977                        "cvtps2pd\t{$src, $dst|$dst, $src}",
978                        [(set VR128:$dst, (int_x86_sse2_cvtps2pd
979                                           (load addr:$src)))]>,
980                      TB, Requires<[HasSSE2]>;
981
982 // Convert packed double to packed single
983 // The assembler can recognize rr 256-bit instructions by seeing a ymm
984 // register, but the same isn't true when using memory operands instead.
985 // Provide other assembly rr and rm forms to address this explicitly.
986 def VCVTPD2PSrr : VPDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
987                        "cvtpd2ps\t{$src, $dst|$dst, $src}", []>, VEX;
988 def VCVTPD2PSXrYr : VPDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR256:$src),
989                          "cvtpd2ps\t{$src, $dst|$dst, $src}", []>, VEX;
990
991 // XMM only
992 def VCVTPD2PSXrr : VPDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
993                         "cvtpd2psx\t{$src, $dst|$dst, $src}", []>, VEX;
994 def VCVTPD2PSXrm : VPDI<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
995                         "cvtpd2psx\t{$src, $dst|$dst, $src}", []>, VEX;
996
997 // YMM only
998 def VCVTPD2PSYrr : VPDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR256:$src),
999                         "cvtpd2psy\t{$src, $dst|$dst, $src}", []>, VEX;
1000 def VCVTPD2PSYrm : VPDI<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f256mem:$src),
1001                         "cvtpd2psy\t{$src, $dst|$dst, $src}", []>, VEX, VEX_L;
1002 def CVTPD2PSrr : PDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1003                      "cvtpd2ps\t{$src, $dst|$dst, $src}", []>;
1004 def CVTPD2PSrm : PDI<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1005                      "cvtpd2ps\t{$src, $dst|$dst, $src}", []>;
1006
1007
1008 def Int_VCVTPD2PSrr : VPDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1009                          "cvtpd2ps\t{$src, $dst|$dst, $src}",
1010                         [(set VR128:$dst, (int_x86_sse2_cvtpd2ps VR128:$src))]>;
1011 def Int_VCVTPD2PSrm : VPDI<0x5A, MRMSrcMem, (outs VR128:$dst),
1012                          (ins f128mem:$src),
1013                          "cvtpd2ps\t{$src, $dst|$dst, $src}",
1014                          [(set VR128:$dst, (int_x86_sse2_cvtpd2ps
1015                                             (memop addr:$src)))]>;
1016 def Int_CVTPD2PSrr : PDI<0x5A, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1017                          "cvtpd2ps\t{$src, $dst|$dst, $src}",
1018                         [(set VR128:$dst, (int_x86_sse2_cvtpd2ps VR128:$src))]>;
1019 def Int_CVTPD2PSrm : PDI<0x5A, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1020                          "cvtpd2ps\t{$src, $dst|$dst, $src}",
1021                          [(set VR128:$dst, (int_x86_sse2_cvtpd2ps
1022                                             (memop addr:$src)))]>;
1023
1024 // AVX 256-bit register conversion intrinsics
1025 // FIXME: Migrate SSE conversion intrinsics matching to use patterns as below
1026 // whenever possible to avoid declaring two versions of each one.
1027 def : Pat<(int_x86_avx_cvtdq2_ps_256 VR256:$src),
1028           (VCVTDQ2PSYrr VR256:$src)>;
1029 def : Pat<(int_x86_avx_cvtdq2_ps_256 (memopv8i32 addr:$src)),
1030           (VCVTDQ2PSYrm addr:$src)>;
1031
1032 def : Pat<(int_x86_avx_cvt_pd2_ps_256 VR256:$src),
1033           (VCVTPD2PSYrr VR256:$src)>;
1034 def : Pat<(int_x86_avx_cvt_pd2_ps_256 (memopv4f64 addr:$src)),
1035           (VCVTPD2PSYrm addr:$src)>;
1036
1037 def : Pat<(int_x86_avx_cvt_ps2dq_256 VR256:$src),
1038           (VCVTPS2DQYrr VR256:$src)>;
1039 def : Pat<(int_x86_avx_cvt_ps2dq_256 (memopv8f32 addr:$src)),
1040           (VCVTPS2DQYrm addr:$src)>;
1041
1042 def : Pat<(int_x86_avx_cvt_ps2_pd_256 VR128:$src),
1043           (VCVTPS2PDYrr VR128:$src)>;
1044 def : Pat<(int_x86_avx_cvt_ps2_pd_256 (memopv4f32 addr:$src)),
1045           (VCVTPS2PDYrm addr:$src)>;
1046
1047 def : Pat<(int_x86_avx_cvtt_pd2dq_256 VR256:$src),
1048           (VCVTTPD2DQYrr VR256:$src)>;
1049 def : Pat<(int_x86_avx_cvtt_pd2dq_256 (memopv4f64 addr:$src)),
1050           (VCVTTPD2DQYrm addr:$src)>;
1051
1052 def : Pat<(int_x86_avx_cvtt_ps2dq_256 VR256:$src),
1053           (VCVTTPS2DQYrr VR256:$src)>;
1054 def : Pat<(int_x86_avx_cvtt_ps2dq_256 (memopv8f32 addr:$src)),
1055           (VCVTTPS2DQYrm addr:$src)>;
1056
1057 //===----------------------------------------------------------------------===//
1058 // SSE 1 & 2 - Compare Instructions
1059 //===----------------------------------------------------------------------===//
1060
1061 // sse12_cmp_scalar - sse 1 & 2 compare scalar instructions
1062 multiclass sse12_cmp_scalar<RegisterClass RC, X86MemOperand x86memop,
1063                             string asm, string asm_alt> {
1064   let isAsmParserOnly = 1 in {
1065     def rr : SIi8<0xC2, MRMSrcReg,
1066                   (outs RC:$dst), (ins RC:$src1, RC:$src, SSECC:$cc),
1067                   asm, []>;
1068     let mayLoad = 1 in
1069     def rm : SIi8<0xC2, MRMSrcMem,
1070                   (outs RC:$dst), (ins RC:$src1, x86memop:$src, SSECC:$cc),
1071                   asm, []>;
1072   }
1073
1074   // Accept explicit immediate argument form instead of comparison code.
1075   def rr_alt : SIi8<0xC2, MRMSrcReg,
1076                 (outs RC:$dst), (ins RC:$src1, RC:$src, i8imm:$src2),
1077                 asm_alt, []>;
1078   let mayLoad = 1 in
1079   def rm_alt : SIi8<0xC2, MRMSrcMem,
1080                 (outs RC:$dst), (ins RC:$src1, x86memop:$src, i8imm:$src2),
1081                 asm_alt, []>;
1082 }
1083
1084 let neverHasSideEffects = 1 in {
1085   defm VCMPSS  : sse12_cmp_scalar<FR32, f32mem,
1086                   "cmp${cc}ss\t{$src, $src1, $dst|$dst, $src1, $src}",
1087                   "cmpss\t{$src2, $src, $src1, $dst|$dst, $src1, $src, $src2}">,
1088                   XS, VEX_4V;
1089   defm VCMPSD  : sse12_cmp_scalar<FR64, f64mem,
1090                   "cmp${cc}sd\t{$src, $src1, $dst|$dst, $src1, $src}",
1091                   "cmpsd\t{$src2, $src, $src1, $dst|$dst, $src1, $src, $src2}">,
1092                   XD, VEX_4V;
1093 }
1094
1095 let Constraints = "$src1 = $dst" in {
1096 def CMPSSrr : SIi8<0xC2, MRMSrcReg,
1097                   (outs FR32:$dst), (ins FR32:$src1, FR32:$src2, SSECC:$cc),
1098                   "cmp${cc}ss\t{$src2, $dst|$dst, $src2}",
1099                   [(set FR32:$dst, (X86cmpss (f32 FR32:$src1), FR32:$src2, imm:$cc))]>, XS;
1100 def CMPSSrm : SIi8<0xC2, MRMSrcMem,
1101                   (outs FR32:$dst), (ins FR32:$src1, f32mem:$src2, SSECC:$cc),
1102                   "cmp${cc}ss\t{$src2, $dst|$dst, $src2}",
1103                   [(set FR32:$dst, (X86cmpss (f32 FR32:$src1), (loadf32 addr:$src2), imm:$cc))]>, XS;
1104 def CMPSDrr : SIi8<0xC2, MRMSrcReg,
1105                   (outs FR64:$dst), (ins FR64:$src1, FR64:$src2, SSECC:$cc),
1106                   "cmp${cc}sd\t{$src2, $dst|$dst, $src2}",
1107                   [(set FR64:$dst, (X86cmpsd (f64 FR64:$src1), FR64:$src2, imm:$cc))]>, XD;
1108 def CMPSDrm : SIi8<0xC2, MRMSrcMem,
1109                   (outs FR64:$dst), (ins FR64:$src1, f64mem:$src2, SSECC:$cc),
1110                   "cmp${cc}sd\t{$src2, $dst|$dst, $src2}",
1111                   [(set FR64:$dst, (X86cmpsd (f64 FR64:$src1), (loadf64 addr:$src2), imm:$cc))]>, XD;
1112 }
1113 let Constraints = "$src1 = $dst", neverHasSideEffects = 1 in {
1114 def CMPSSrr_alt : SIi8<0xC2, MRMSrcReg,
1115                   (outs FR32:$dst), (ins FR32:$src1, FR32:$src, i8imm:$src2),
1116                   "cmpss\t{$src2, $src, $dst|$dst, $src, $src2}", []>, XS;
1117 def CMPSSrm_alt : SIi8<0xC2, MRMSrcMem,
1118                   (outs FR32:$dst), (ins FR32:$src1, f32mem:$src, i8imm:$src2),
1119                   "cmpss\t{$src2, $src, $dst|$dst, $src, $src2}", []>, XS;
1120 def CMPSDrr_alt : SIi8<0xC2, MRMSrcReg,
1121                   (outs FR64:$dst), (ins FR64:$src1, FR64:$src, i8imm:$src2),
1122                   "cmpsd\t{$src2, $src, $dst|$dst, $src, $src2}", []>, XD;
1123 def CMPSDrm_alt : SIi8<0xC2, MRMSrcMem,
1124                   (outs FR64:$dst), (ins FR64:$src1, f64mem:$src, i8imm:$src2),
1125                   "cmpsd\t{$src2, $src, $dst|$dst, $src, $src2}", []>, XD;
1126 }
1127
1128 multiclass sse12_cmp_scalar_int<RegisterClass RC, X86MemOperand x86memop,
1129                          Intrinsic Int, string asm> {
1130   def rr : SIi8<0xC2, MRMSrcReg, (outs VR128:$dst),
1131                       (ins VR128:$src1, VR128:$src, SSECC:$cc), asm,
1132                         [(set VR128:$dst, (Int VR128:$src1,
1133                                                VR128:$src, imm:$cc))]>;
1134   def rm : SIi8<0xC2, MRMSrcMem, (outs VR128:$dst),
1135                       (ins VR128:$src1, f32mem:$src, SSECC:$cc), asm,
1136                         [(set VR128:$dst, (Int VR128:$src1,
1137                                                (load addr:$src), imm:$cc))]>;
1138 }
1139
1140 // Aliases to match intrinsics which expect XMM operand(s).
1141 defm Int_VCMPSS  : sse12_cmp_scalar_int<VR128, f32mem, int_x86_sse_cmp_ss,
1142                      "cmp${cc}ss\t{$src, $src1, $dst|$dst, $src1, $src}">,
1143                      XS, VEX_4V;
1144 defm Int_VCMPSD  : sse12_cmp_scalar_int<VR128, f64mem, int_x86_sse2_cmp_sd,
1145                      "cmp${cc}sd\t{$src, $src1, $dst|$dst, $src1, $src}">,
1146                      XD, VEX_4V;
1147 let Constraints = "$src1 = $dst" in {
1148   defm Int_CMPSS  : sse12_cmp_scalar_int<VR128, f32mem, int_x86_sse_cmp_ss,
1149                        "cmp${cc}ss\t{$src, $dst|$dst, $src}">, XS;
1150   defm Int_CMPSD  : sse12_cmp_scalar_int<VR128, f64mem, int_x86_sse2_cmp_sd,
1151                        "cmp${cc}sd\t{$src, $dst|$dst, $src}">, XD;
1152 }
1153
1154
1155 // sse12_ord_cmp - Unordered/Ordered scalar fp compare and set EFLAGS
1156 multiclass sse12_ord_cmp<bits<8> opc, RegisterClass RC, SDNode OpNode,
1157                             ValueType vt, X86MemOperand x86memop,
1158                             PatFrag ld_frag, string OpcodeStr, Domain d> {
1159   def rr: PI<opc, MRMSrcReg, (outs), (ins RC:$src1, RC:$src2),
1160                      !strconcat(OpcodeStr, "\t{$src2, $src1|$src1, $src2}"),
1161                      [(set EFLAGS, (OpNode (vt RC:$src1), RC:$src2))], d>;
1162   def rm: PI<opc, MRMSrcMem, (outs), (ins RC:$src1, x86memop:$src2),
1163                      !strconcat(OpcodeStr, "\t{$src2, $src1|$src1, $src2}"),
1164                      [(set EFLAGS, (OpNode (vt RC:$src1),
1165                                            (ld_frag addr:$src2)))], d>;
1166 }
1167
1168 let Defs = [EFLAGS] in {
1169   defm VUCOMISS : sse12_ord_cmp<0x2E, FR32, X86cmp, f32, f32mem, loadf32,
1170                                   "ucomiss", SSEPackedSingle>, VEX;
1171   defm VUCOMISD : sse12_ord_cmp<0x2E, FR64, X86cmp, f64, f64mem, loadf64,
1172                                   "ucomisd", SSEPackedDouble>, OpSize, VEX;
1173   let Pattern = []<dag> in {
1174     defm VCOMISS  : sse12_ord_cmp<0x2F, VR128, undef, v4f32, f128mem, load,
1175                                     "comiss", SSEPackedSingle>, VEX;
1176     defm VCOMISD  : sse12_ord_cmp<0x2F, VR128, undef, v2f64, f128mem, load,
1177                                     "comisd", SSEPackedDouble>, OpSize, VEX;
1178   }
1179
1180   defm Int_VUCOMISS  : sse12_ord_cmp<0x2E, VR128, X86ucomi, v4f32, f128mem,
1181                             load, "ucomiss", SSEPackedSingle>, VEX;
1182   defm Int_VUCOMISD  : sse12_ord_cmp<0x2E, VR128, X86ucomi, v2f64, f128mem,
1183                             load, "ucomisd", SSEPackedDouble>, OpSize, VEX;
1184
1185   defm Int_VCOMISS  : sse12_ord_cmp<0x2F, VR128, X86comi, v4f32, f128mem,
1186                             load, "comiss", SSEPackedSingle>, VEX;
1187   defm Int_VCOMISD  : sse12_ord_cmp<0x2F, VR128, X86comi, v2f64, f128mem,
1188                             load, "comisd", SSEPackedDouble>, OpSize, VEX;
1189   defm UCOMISS  : sse12_ord_cmp<0x2E, FR32, X86cmp, f32, f32mem, loadf32,
1190                                   "ucomiss", SSEPackedSingle>, TB;
1191   defm UCOMISD  : sse12_ord_cmp<0x2E, FR64, X86cmp, f64, f64mem, loadf64,
1192                                   "ucomisd", SSEPackedDouble>, TB, OpSize;
1193
1194   let Pattern = []<dag> in {
1195     defm COMISS  : sse12_ord_cmp<0x2F, VR128, undef, v4f32, f128mem, load,
1196                                     "comiss", SSEPackedSingle>, TB;
1197     defm COMISD  : sse12_ord_cmp<0x2F, VR128, undef, v2f64, f128mem, load,
1198                                     "comisd", SSEPackedDouble>, TB, OpSize;
1199   }
1200
1201   defm Int_UCOMISS  : sse12_ord_cmp<0x2E, VR128, X86ucomi, v4f32, f128mem,
1202                               load, "ucomiss", SSEPackedSingle>, TB;
1203   defm Int_UCOMISD  : sse12_ord_cmp<0x2E, VR128, X86ucomi, v2f64, f128mem,
1204                               load, "ucomisd", SSEPackedDouble>, TB, OpSize;
1205
1206   defm Int_COMISS  : sse12_ord_cmp<0x2F, VR128, X86comi, v4f32, f128mem, load,
1207                                   "comiss", SSEPackedSingle>, TB;
1208   defm Int_COMISD  : sse12_ord_cmp<0x2F, VR128, X86comi, v2f64, f128mem, load,
1209                                   "comisd", SSEPackedDouble>, TB, OpSize;
1210 } // Defs = [EFLAGS]
1211
1212 // sse12_cmp_packed - sse 1 & 2 compared packed instructions
1213 multiclass sse12_cmp_packed<RegisterClass RC, X86MemOperand x86memop,
1214                             Intrinsic Int, string asm, string asm_alt,
1215                             Domain d> {
1216   let isAsmParserOnly = 1 in {
1217     def rri : PIi8<0xC2, MRMSrcReg,
1218                (outs RC:$dst), (ins RC:$src1, RC:$src, SSECC:$cc), asm,
1219                [(set RC:$dst, (Int RC:$src1, RC:$src, imm:$cc))], d>;
1220     def rmi : PIi8<0xC2, MRMSrcMem,
1221                (outs RC:$dst), (ins RC:$src1, f128mem:$src, SSECC:$cc), asm,
1222                [(set RC:$dst, (Int RC:$src1, (memop addr:$src), imm:$cc))], d>;
1223   }
1224
1225   // Accept explicit immediate argument form instead of comparison code.
1226   def rri_alt : PIi8<0xC2, MRMSrcReg,
1227              (outs RC:$dst), (ins RC:$src1, RC:$src, i8imm:$src2),
1228              asm_alt, [], d>;
1229   def rmi_alt : PIi8<0xC2, MRMSrcMem,
1230              (outs RC:$dst), (ins RC:$src1, f128mem:$src, i8imm:$src2),
1231              asm_alt, [], d>;
1232 }
1233
1234 defm VCMPPS : sse12_cmp_packed<VR128, f128mem, int_x86_sse_cmp_ps,
1235                "cmp${cc}ps\t{$src, $src1, $dst|$dst, $src1, $src}",
1236                "cmpps\t{$src2, $src, $src1, $dst|$dst, $src1, $src, $src2}",
1237                SSEPackedSingle>, VEX_4V;
1238 defm VCMPPD : sse12_cmp_packed<VR128, f128mem, int_x86_sse2_cmp_pd,
1239                "cmp${cc}pd\t{$src, $src1, $dst|$dst, $src1, $src}",
1240                "cmppd\t{$src2, $src, $src1, $dst|$dst, $src1, $src, $src2}",
1241                SSEPackedDouble>, OpSize, VEX_4V;
1242 defm VCMPPSY : sse12_cmp_packed<VR256, f256mem, int_x86_avx_cmp_ps_256,
1243                "cmp${cc}ps\t{$src, $src1, $dst|$dst, $src1, $src}",
1244                "cmpps\t{$src2, $src, $src1, $dst|$dst, $src1, $src, $src2}",
1245                SSEPackedSingle>, VEX_4V;
1246 defm VCMPPDY : sse12_cmp_packed<VR256, f256mem, int_x86_avx_cmp_pd_256,
1247                "cmp${cc}pd\t{$src, $src1, $dst|$dst, $src1, $src}",
1248                "cmppd\t{$src2, $src, $src1, $dst|$dst, $src1, $src, $src2}",
1249                SSEPackedDouble>, OpSize, VEX_4V;
1250 let Constraints = "$src1 = $dst" in {
1251   defm CMPPS : sse12_cmp_packed<VR128, f128mem, int_x86_sse_cmp_ps,
1252                  "cmp${cc}ps\t{$src, $dst|$dst, $src}",
1253                  "cmpps\t{$src2, $src, $dst|$dst, $src, $src2}",
1254                  SSEPackedSingle>, TB;
1255   defm CMPPD : sse12_cmp_packed<VR128, f128mem, int_x86_sse2_cmp_pd,
1256                  "cmp${cc}pd\t{$src, $dst|$dst, $src}",
1257                  "cmppd\t{$src2, $src, $dst|$dst, $src, $src2}",
1258                  SSEPackedDouble>, TB, OpSize;
1259 }
1260
1261 def : Pat<(v4i32 (X86cmpps (v4f32 VR128:$src1), VR128:$src2, imm:$cc)),
1262           (CMPPSrri (v4f32 VR128:$src1), (v4f32 VR128:$src2), imm:$cc)>;
1263 def : Pat<(v4i32 (X86cmpps (v4f32 VR128:$src1), (memop addr:$src2), imm:$cc)),
1264           (CMPPSrmi (v4f32 VR128:$src1), addr:$src2, imm:$cc)>;
1265 def : Pat<(v2i64 (X86cmppd (v2f64 VR128:$src1), VR128:$src2, imm:$cc)),
1266           (CMPPDrri VR128:$src1, VR128:$src2, imm:$cc)>;
1267 def : Pat<(v2i64 (X86cmppd (v2f64 VR128:$src1), (memop addr:$src2), imm:$cc)),
1268           (CMPPDrmi VR128:$src1, addr:$src2, imm:$cc)>;
1269
1270 //===----------------------------------------------------------------------===//
1271 // SSE 1 & 2 - Shuffle Instructions
1272 //===----------------------------------------------------------------------===//
1273
1274 /// sse12_shuffle - sse 1 & 2 shuffle instructions
1275 multiclass sse12_shuffle<RegisterClass RC, X86MemOperand x86memop,
1276                          ValueType vt, string asm, PatFrag mem_frag,
1277                          Domain d, bit IsConvertibleToThreeAddress = 0> {
1278   def rmi : PIi8<0xC6, MRMSrcMem, (outs RC:$dst),
1279                    (ins RC:$src1, f128mem:$src2, i8imm:$src3), asm,
1280                    [(set RC:$dst, (vt (shufp:$src3
1281                             RC:$src1, (mem_frag addr:$src2))))], d>;
1282   let isConvertibleToThreeAddress = IsConvertibleToThreeAddress in
1283     def rri : PIi8<0xC6, MRMSrcReg, (outs RC:$dst),
1284                    (ins RC:$src1, RC:$src2, i8imm:$src3), asm,
1285                    [(set RC:$dst,
1286                             (vt (shufp:$src3 RC:$src1, RC:$src2)))], d>;
1287 }
1288
1289 defm VSHUFPS  : sse12_shuffle<VR128, f128mem, v4f32,
1290            "shufps\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",
1291            memopv4f32, SSEPackedSingle>, TB, VEX_4V;
1292 defm VSHUFPSY : sse12_shuffle<VR256, f256mem, v8f32,
1293            "shufps\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",
1294            memopv8f32, SSEPackedSingle>, TB, VEX_4V;
1295 defm VSHUFPD  : sse12_shuffle<VR128, f128mem, v2f64,
1296            "shufpd\t{$src3, $src2, $src1, $dst|$dst, $src2, $src2, $src3}",
1297            memopv2f64, SSEPackedDouble>, TB, OpSize, VEX_4V;
1298 defm VSHUFPDY : sse12_shuffle<VR256, f256mem, v4f64,
1299            "shufpd\t{$src3, $src2, $src1, $dst|$dst, $src2, $src2, $src3}",
1300            memopv4f64, SSEPackedDouble>, TB, OpSize, VEX_4V;
1301
1302 let Constraints = "$src1 = $dst" in {
1303   defm SHUFPS : sse12_shuffle<VR128, f128mem, v4f32,
1304                     "shufps\t{$src3, $src2, $dst|$dst, $src2, $src3}",
1305                     memopv4f32, SSEPackedSingle, 1 /* cvt to pshufd */>,
1306                     TB;
1307   defm SHUFPD : sse12_shuffle<VR128, f128mem, v2f64,
1308                     "shufpd\t{$src3, $src2, $dst|$dst, $src2, $src3}",
1309                     memopv2f64, SSEPackedDouble>, TB, OpSize;
1310 }
1311
1312 //===----------------------------------------------------------------------===//
1313 // SSE 1 & 2 - Unpack Instructions
1314 //===----------------------------------------------------------------------===//
1315
1316 /// sse12_unpack_interleave - sse 1 & 2 unpack and interleave
1317 multiclass sse12_unpack_interleave<bits<8> opc, PatFrag OpNode, ValueType vt,
1318                                    PatFrag mem_frag, RegisterClass RC,
1319                                    X86MemOperand x86memop, string asm,
1320                                    Domain d> {
1321     def rr : PI<opc, MRMSrcReg,
1322                 (outs RC:$dst), (ins RC:$src1, RC:$src2),
1323                 asm, [(set RC:$dst,
1324                            (vt (OpNode RC:$src1, RC:$src2)))], d>;
1325     def rm : PI<opc, MRMSrcMem,
1326                 (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
1327                 asm, [(set RC:$dst,
1328                            (vt (OpNode RC:$src1,
1329                                        (mem_frag addr:$src2))))], d>;
1330 }
1331
1332 let AddedComplexity = 10 in {
1333   defm VUNPCKHPS: sse12_unpack_interleave<0x15, unpckh, v4f32, memopv4f32,
1334         VR128, f128mem, "unpckhps\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1335                        SSEPackedSingle>, VEX_4V;
1336   defm VUNPCKHPD: sse12_unpack_interleave<0x15, unpckh, v2f64, memopv2f64,
1337         VR128, f128mem, "unpckhpd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1338                        SSEPackedDouble>, OpSize, VEX_4V;
1339   defm VUNPCKLPS: sse12_unpack_interleave<0x14, unpckl, v4f32, memopv4f32,
1340         VR128, f128mem, "unpcklps\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1341                        SSEPackedSingle>, VEX_4V;
1342   defm VUNPCKLPD: sse12_unpack_interleave<0x14, unpckl, v2f64, memopv2f64,
1343         VR128, f128mem, "unpcklpd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1344                        SSEPackedDouble>, OpSize, VEX_4V;
1345
1346   defm VUNPCKHPSY: sse12_unpack_interleave<0x15, unpckh, v8f32, memopv8f32,
1347         VR256, f256mem, "unpckhps\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1348                        SSEPackedSingle>, VEX_4V;
1349   defm VUNPCKHPDY: sse12_unpack_interleave<0x15, unpckh, v4f64, memopv4f64,
1350         VR256, f256mem, "unpckhpd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1351                        SSEPackedDouble>, OpSize, VEX_4V;
1352   defm VUNPCKLPSY: sse12_unpack_interleave<0x14, unpckl, v8f32, memopv8f32,
1353         VR256, f256mem, "unpcklps\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1354                        SSEPackedSingle>, VEX_4V;
1355   defm VUNPCKLPDY: sse12_unpack_interleave<0x14, unpckl, v4f64, memopv4f64,
1356         VR256, f256mem, "unpcklpd\t{$src2, $src1, $dst|$dst, $src1, $src2}",
1357                        SSEPackedDouble>, OpSize, VEX_4V;
1358
1359   let Constraints = "$src1 = $dst" in {
1360     defm UNPCKHPS: sse12_unpack_interleave<0x15, unpckh, v4f32, memopv4f32,
1361           VR128, f128mem, "unpckhps\t{$src2, $dst|$dst, $src2}",
1362                          SSEPackedSingle>, TB;
1363     defm UNPCKHPD: sse12_unpack_interleave<0x15, unpckh, v2f64, memopv2f64,
1364           VR128, f128mem, "unpckhpd\t{$src2, $dst|$dst, $src2}",
1365                          SSEPackedDouble>, TB, OpSize;
1366     defm UNPCKLPS: sse12_unpack_interleave<0x14, unpckl, v4f32, memopv4f32,
1367           VR128, f128mem, "unpcklps\t{$src2, $dst|$dst, $src2}",
1368                          SSEPackedSingle>, TB;
1369     defm UNPCKLPD: sse12_unpack_interleave<0x14, unpckl, v2f64, memopv2f64,
1370           VR128, f128mem, "unpcklpd\t{$src2, $dst|$dst, $src2}",
1371                          SSEPackedDouble>, TB, OpSize;
1372   } // Constraints = "$src1 = $dst"
1373 } // AddedComplexity
1374
1375 //===----------------------------------------------------------------------===//
1376 // SSE 1 & 2 - Extract Floating-Point Sign mask
1377 //===----------------------------------------------------------------------===//
1378
1379 /// sse12_extr_sign_mask - sse 1 & 2 unpack and interleave
1380 multiclass sse12_extr_sign_mask<RegisterClass RC, Intrinsic Int, string asm,
1381                                 Domain d> {
1382   def rr32 : PI<0x50, MRMSrcReg, (outs GR32:$dst), (ins RC:$src),
1383                 !strconcat(asm, "\t{$src, $dst|$dst, $src}"),
1384                      [(set GR32:$dst, (Int RC:$src))], d>;
1385   def rr64 : PI<0x50, MRMSrcReg, (outs GR64:$dst), (ins RC:$src),
1386                 !strconcat(asm, "\t{$src, $dst|$dst, $src}"), [], d>, REX_W;
1387 }
1388
1389 // Mask creation
1390 defm VMOVMSKPS : sse12_extr_sign_mask<VR128, int_x86_sse_movmsk_ps,
1391                                       "movmskps", SSEPackedSingle>, VEX;
1392 defm VMOVMSKPD : sse12_extr_sign_mask<VR128, int_x86_sse2_movmsk_pd,
1393                                       "movmskpd", SSEPackedDouble>, OpSize,
1394                                       VEX;
1395 defm VMOVMSKPSY : sse12_extr_sign_mask<VR256, int_x86_avx_movmsk_ps_256,
1396                                       "movmskps", SSEPackedSingle>, VEX;
1397 defm VMOVMSKPDY : sse12_extr_sign_mask<VR256, int_x86_avx_movmsk_pd_256,
1398                                       "movmskpd", SSEPackedDouble>, OpSize,
1399                                       VEX;
1400 defm MOVMSKPS : sse12_extr_sign_mask<VR128, int_x86_sse_movmsk_ps, "movmskps",
1401                                      SSEPackedSingle>, TB;
1402 defm MOVMSKPD : sse12_extr_sign_mask<VR128, int_x86_sse2_movmsk_pd, "movmskpd",
1403                                      SSEPackedDouble>, TB, OpSize;
1404
1405 // X86fgetsign
1406 def MOVMSKPDrr32_alt : PI<0x50, MRMSrcReg, (outs GR32:$dst), (ins FR64:$src),
1407                     "movmskpd\t{$src, $dst|$dst, $src}",
1408                     [(set GR32:$dst, (X86fgetsign FR64:$src))], SSEPackedDouble>, TB, OpSize;
1409 def MOVMSKPDrr64_alt : PI<0x50, MRMSrcReg, (outs GR64:$dst), (ins FR64:$src),
1410                     "movmskpd\t{$src, $dst|$dst, $src}",
1411                     [(set GR64:$dst, (X86fgetsign FR64:$src))], SSEPackedDouble>, TB, OpSize;
1412 def MOVMSKPSrr32_alt : PI<0x50, MRMSrcReg, (outs GR32:$dst), (ins FR32:$src),
1413                     "movmskps\t{$src, $dst|$dst, $src}",
1414                     [(set GR32:$dst, (X86fgetsign FR32:$src))], SSEPackedSingle>, TB;
1415 def MOVMSKPSrr64_alt : PI<0x50, MRMSrcReg, (outs GR64:$dst), (ins FR32:$src),
1416                     "movmskps\t{$src, $dst|$dst, $src}",
1417                     [(set GR64:$dst, (X86fgetsign FR32:$src))], SSEPackedSingle>, TB;
1418
1419 // Assembler Only
1420 def VMOVMSKPSr64r : PI<0x50, MRMSrcReg, (outs GR64:$dst), (ins VR128:$src),
1421            "movmskps\t{$src, $dst|$dst, $src}", [], SSEPackedSingle>, VEX;
1422 def VMOVMSKPDr64r : PI<0x50, MRMSrcReg, (outs GR64:$dst), (ins VR128:$src),
1423            "movmskpd\t{$src, $dst|$dst, $src}", [], SSEPackedDouble>, OpSize,
1424            VEX;
1425 def VMOVMSKPSYr64r : PI<0x50, MRMSrcReg, (outs GR64:$dst), (ins VR256:$src),
1426            "movmskps\t{$src, $dst|$dst, $src}", [], SSEPackedSingle>, VEX;
1427 def VMOVMSKPDYr64r : PI<0x50, MRMSrcReg, (outs GR64:$dst), (ins VR256:$src),
1428            "movmskpd\t{$src, $dst|$dst, $src}", [], SSEPackedDouble>, OpSize,
1429            VEX;
1430
1431 //===----------------------------------------------------------------------===//
1432 // SSE 1 & 2 - Misc aliasing of packed SSE 1 & 2 instructions
1433 //===----------------------------------------------------------------------===//
1434
1435 // Aliases of packed SSE1 & SSE2 instructions for scalar use. These all have
1436 // names that start with 'Fs'.
1437
1438 // Alias instructions that map fld0 to pxor for sse.
1439 let isReMaterializable = 1, isAsCheapAsAMove = 1, isCodeGenOnly = 1,
1440     canFoldAsLoad = 1 in {
1441   // FIXME: Set encoding to pseudo!
1442 def FsFLD0SS : I<0xEF, MRMInitReg, (outs FR32:$dst), (ins), "",
1443                  [(set FR32:$dst, fp32imm0)]>,
1444                  Requires<[HasSSE1]>, TB, OpSize;
1445 def FsFLD0SD : I<0xEF, MRMInitReg, (outs FR64:$dst), (ins), "",
1446                  [(set FR64:$dst, fpimm0)]>,
1447                Requires<[HasSSE2]>, TB, OpSize;
1448 def VFsFLD0SS : I<0xEF, MRMInitReg, (outs FR32:$dst), (ins), "",
1449                   [(set FR32:$dst, fp32imm0)]>,
1450                   Requires<[HasAVX]>, TB, OpSize, VEX_4V;
1451 def VFsFLD0SD : I<0xEF, MRMInitReg, (outs FR64:$dst), (ins), "",
1452                   [(set FR64:$dst, fpimm0)]>,
1453                   Requires<[HasAVX]>, TB, OpSize, VEX_4V;
1454 }
1455
1456 // Alias instruction to do FR32 or FR64 reg-to-reg copy using movaps. Upper
1457 // bits are disregarded.
1458 let neverHasSideEffects = 1 in {
1459 def FsMOVAPSrr : PSI<0x28, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src),
1460                      "movaps\t{$src, $dst|$dst, $src}", []>;
1461 def FsMOVAPDrr : PDI<0x28, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src),
1462                      "movapd\t{$src, $dst|$dst, $src}", []>;
1463 }
1464
1465 // Alias instruction to load FR32 or FR64 from f128mem using movaps. Upper
1466 // bits are disregarded.
1467 let canFoldAsLoad = 1, isReMaterializable = 1 in {
1468 def FsMOVAPSrm : PSI<0x28, MRMSrcMem, (outs FR32:$dst), (ins f128mem:$src),
1469                      "movaps\t{$src, $dst|$dst, $src}",
1470                      [(set FR32:$dst, (alignedloadfsf32 addr:$src))]>;
1471 def FsMOVAPDrm : PDI<0x28, MRMSrcMem, (outs FR64:$dst), (ins f128mem:$src),
1472                      "movapd\t{$src, $dst|$dst, $src}",
1473                      [(set FR64:$dst, (alignedloadfsf64 addr:$src))]>;
1474 }
1475
1476 //===----------------------------------------------------------------------===//
1477 // SSE 1 & 2 - Logical Instructions
1478 //===----------------------------------------------------------------------===//
1479
1480 /// sse12_fp_alias_pack_logical - SSE 1 & 2 aliased packed FP logical ops
1481 ///
1482 multiclass sse12_fp_alias_pack_logical<bits<8> opc, string OpcodeStr,
1483                                        SDNode OpNode> {
1484   defm V#NAME#PS : sse12_fp_packed<opc, !strconcat(OpcodeStr, "ps"), OpNode,
1485               FR32, f32, f128mem, memopfsf32, SSEPackedSingle, 0>, VEX_4V;
1486
1487   defm V#NAME#PD : sse12_fp_packed<opc, !strconcat(OpcodeStr, "pd"), OpNode,
1488         FR64, f64, f128mem, memopfsf64, SSEPackedDouble, 0>, OpSize, VEX_4V;
1489
1490   let Constraints = "$src1 = $dst" in {
1491     defm PS : sse12_fp_packed<opc, !strconcat(OpcodeStr, "ps"), OpNode, FR32,
1492                 f32, f128mem, memopfsf32, SSEPackedSingle>, TB;
1493
1494     defm PD : sse12_fp_packed<opc, !strconcat(OpcodeStr, "pd"), OpNode, FR64,
1495                 f64, f128mem, memopfsf64, SSEPackedDouble>, TB, OpSize;
1496   }
1497 }
1498
1499 // Alias bitwise logical operations using SSE logical ops on packed FP values.
1500 let mayLoad = 0 in {
1501   defm FsAND  : sse12_fp_alias_pack_logical<0x54, "and", X86fand>;
1502   defm FsOR   : sse12_fp_alias_pack_logical<0x56, "or", X86for>;
1503   defm FsXOR  : sse12_fp_alias_pack_logical<0x57, "xor", X86fxor>;
1504 }
1505
1506 let neverHasSideEffects = 1, Pattern = []<dag>, isCommutable = 0 in
1507   defm FsANDN : sse12_fp_alias_pack_logical<0x55, "andn", undef>;
1508
1509 /// sse12_fp_packed_logical - SSE 1 & 2 packed FP logical ops
1510 ///
1511 multiclass sse12_fp_packed_logical<bits<8> opc, string OpcodeStr,
1512                                    SDNode OpNode> {
1513   let Pattern = []<dag> in {
1514     defm V#NAME#PS : sse12_fp_packed_logical_rm<opc, VR128, SSEPackedSingle,
1515          !strconcat(OpcodeStr, "ps"), f128mem,
1516          [(set VR128:$dst, (v2i64 (OpNode VR128:$src1, VR128:$src2)))],
1517          [(set VR128:$dst, (OpNode (bc_v2i64 (v4f32 VR128:$src1)),
1518                                    (memopv2i64 addr:$src2)))], 0>, VEX_4V;
1519
1520     defm V#NAME#PD : sse12_fp_packed_logical_rm<opc, VR128, SSEPackedDouble,
1521          !strconcat(OpcodeStr, "pd"), f128mem,
1522          [(set VR128:$dst, (OpNode (bc_v2i64 (v2f64 VR128:$src1)),
1523                                    (bc_v2i64 (v2f64 VR128:$src2))))],
1524          [(set VR128:$dst, (OpNode (bc_v2i64 (v2f64 VR128:$src1)),
1525                                    (memopv2i64 addr:$src2)))], 0>,
1526                                                    OpSize, VEX_4V;
1527   }
1528   let Constraints = "$src1 = $dst" in {
1529     defm PS : sse12_fp_packed_logical_rm<opc, VR128, SSEPackedSingle,
1530          !strconcat(OpcodeStr, "ps"), f128mem,
1531          [(set VR128:$dst, (v2i64 (OpNode VR128:$src1, VR128:$src2)))],
1532          [(set VR128:$dst, (OpNode (bc_v2i64 (v4f32 VR128:$src1)),
1533                                    (memopv2i64 addr:$src2)))]>, TB;
1534
1535     defm PD : sse12_fp_packed_logical_rm<opc, VR128, SSEPackedDouble,
1536          !strconcat(OpcodeStr, "pd"), f128mem,
1537          [(set VR128:$dst, (OpNode (bc_v2i64 (v2f64 VR128:$src1)),
1538                                    (bc_v2i64 (v2f64 VR128:$src2))))],
1539          [(set VR128:$dst, (OpNode (bc_v2i64 (v2f64 VR128:$src1)),
1540                                    (memopv2i64 addr:$src2)))]>, TB, OpSize;
1541   }
1542 }
1543
1544 /// sse12_fp_packed_logical_y - AVX 256-bit SSE 1 & 2 logical ops forms
1545 ///
1546 multiclass sse12_fp_packed_logical_y<bits<8> opc, string OpcodeStr,
1547                                      SDNode OpNode> {
1548     defm PSY : sse12_fp_packed_logical_rm<opc, VR256, SSEPackedSingle,
1549           !strconcat(OpcodeStr, "ps"), f256mem,
1550           [(set VR256:$dst, (v4i64 (OpNode VR256:$src1, VR256:$src2)))],
1551           [(set VR256:$dst, (OpNode (bc_v4i64 (v8f32 VR256:$src1)),
1552                                     (memopv4i64 addr:$src2)))], 0>, VEX_4V;
1553
1554     defm PDY : sse12_fp_packed_logical_rm<opc, VR256, SSEPackedDouble,
1555           !strconcat(OpcodeStr, "pd"), f256mem,
1556           [(set VR256:$dst, (OpNode (bc_v4i64 (v4f64 VR256:$src1)),
1557                                     (bc_v4i64 (v4f64 VR256:$src2))))],
1558           [(set VR256:$dst, (OpNode (bc_v4i64 (v4f64 VR256:$src1)),
1559                                     (memopv4i64 addr:$src2)))], 0>,
1560                                     OpSize, VEX_4V;
1561 }
1562
1563 // AVX 256-bit packed logical ops forms
1564 defm VAND  : sse12_fp_packed_logical_y<0x54, "and", and>;
1565 defm VOR   : sse12_fp_packed_logical_y<0x56, "or", or>;
1566 defm VXOR  : sse12_fp_packed_logical_y<0x57, "xor", xor>;
1567 defm VANDN : sse12_fp_packed_logical_y<0x55, "andn", X86andnp>;
1568
1569 defm AND  : sse12_fp_packed_logical<0x54, "and", and>;
1570 defm OR   : sse12_fp_packed_logical<0x56, "or", or>;
1571 defm XOR  : sse12_fp_packed_logical<0x57, "xor", xor>;
1572 let isCommutable = 0 in
1573   defm ANDN : sse12_fp_packed_logical<0x55, "andn", X86andnp>;
1574
1575 //===----------------------------------------------------------------------===//
1576 // SSE 1 & 2 - Arithmetic Instructions
1577 //===----------------------------------------------------------------------===//
1578
1579 /// basic_sse12_fp_binop_xxx - SSE 1 & 2 binops come in both scalar and
1580 /// vector forms.
1581 ///
1582 /// In addition, we also have a special variant of the scalar form here to
1583 /// represent the associated intrinsic operation.  This form is unlike the
1584 /// plain scalar form, in that it takes an entire vector (instead of a scalar)
1585 /// and leaves the top elements unmodified (therefore these cannot be commuted).
1586 ///
1587 /// These three forms can each be reg+reg or reg+mem.
1588 ///
1589
1590 /// FIXME: once all 256-bit intrinsics are matched, cleanup and refactor those
1591 /// classes below
1592 multiclass basic_sse12_fp_binop_s<bits<8> opc, string OpcodeStr, SDNode OpNode,
1593                                   bit Is2Addr = 1> {
1594   defm SS : sse12_fp_scalar<opc, !strconcat(OpcodeStr, "ss"),
1595                             OpNode, FR32, f32mem, Is2Addr>, XS;
1596   defm SD : sse12_fp_scalar<opc, !strconcat(OpcodeStr, "sd"),
1597                             OpNode, FR64, f64mem, Is2Addr>, XD;
1598 }
1599
1600 multiclass basic_sse12_fp_binop_p<bits<8> opc, string OpcodeStr, SDNode OpNode,
1601                                    bit Is2Addr = 1> {
1602   let mayLoad = 0 in {
1603   defm PS : sse12_fp_packed<opc, !strconcat(OpcodeStr, "ps"), OpNode, VR128,
1604               v4f32, f128mem, memopv4f32, SSEPackedSingle, Is2Addr>, TB;
1605   defm PD : sse12_fp_packed<opc, !strconcat(OpcodeStr, "pd"), OpNode, VR128,
1606               v2f64, f128mem, memopv2f64, SSEPackedDouble, Is2Addr>, TB, OpSize;
1607   }
1608 }
1609
1610 multiclass basic_sse12_fp_binop_p_y<bits<8> opc, string OpcodeStr,
1611                                     SDNode OpNode> {
1612   let mayLoad = 0 in {
1613     defm PSY : sse12_fp_packed<opc, !strconcat(OpcodeStr, "ps"), OpNode, VR256,
1614                 v8f32, f256mem, memopv8f32, SSEPackedSingle, 0>, TB;
1615     defm PDY : sse12_fp_packed<opc, !strconcat(OpcodeStr, "pd"), OpNode, VR256,
1616                 v4f64, f256mem, memopv4f64, SSEPackedDouble, 0>, TB, OpSize;
1617   }
1618 }
1619
1620 multiclass basic_sse12_fp_binop_s_int<bits<8> opc, string OpcodeStr,
1621                                       bit Is2Addr = 1> {
1622   defm SS : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1623      !strconcat(OpcodeStr, "ss"), "", "_ss", ssmem, sse_load_f32, Is2Addr>, XS;
1624   defm SD : sse12_fp_scalar_int<opc, OpcodeStr, VR128,
1625      !strconcat(OpcodeStr, "sd"), "2", "_sd", sdmem, sse_load_f64, Is2Addr>, XD;
1626 }
1627
1628 multiclass basic_sse12_fp_binop_p_int<bits<8> opc, string OpcodeStr,
1629                                       bit Is2Addr = 1> {
1630   defm PS : sse12_fp_packed_int<opc, OpcodeStr, VR128,
1631      !strconcat(OpcodeStr, "ps"), "sse", "_ps", f128mem, memopv4f32,
1632                                               SSEPackedSingle, Is2Addr>, TB;
1633
1634   defm PD : sse12_fp_packed_int<opc, OpcodeStr, VR128,
1635      !strconcat(OpcodeStr, "pd"), "sse2", "_pd", f128mem, memopv2f64,
1636                                       SSEPackedDouble, Is2Addr>, TB, OpSize;
1637 }
1638
1639 multiclass basic_sse12_fp_binop_p_y_int<bits<8> opc, string OpcodeStr> {
1640   defm PSY : sse12_fp_packed_int<opc, OpcodeStr, VR256,
1641      !strconcat(OpcodeStr, "ps"), "avx", "_ps_256", f256mem, memopv8f32,
1642       SSEPackedSingle, 0>, TB;
1643
1644   defm PDY : sse12_fp_packed_int<opc, OpcodeStr, VR256,
1645      !strconcat(OpcodeStr, "pd"), "avx", "_pd_256", f256mem, memopv4f64,
1646       SSEPackedDouble, 0>, TB, OpSize;
1647 }
1648
1649 // Binary Arithmetic instructions
1650 defm VADD : basic_sse12_fp_binop_s<0x58, "add", fadd, 0>,
1651             basic_sse12_fp_binop_s_int<0x58, "add", 0>,
1652             basic_sse12_fp_binop_p<0x58, "add", fadd, 0>,
1653             basic_sse12_fp_binop_p_y<0x58, "add", fadd>, VEX_4V;
1654 defm VMUL : basic_sse12_fp_binop_s<0x59, "mul", fmul, 0>,
1655             basic_sse12_fp_binop_s_int<0x59, "mul", 0>,
1656             basic_sse12_fp_binop_p<0x59, "mul", fmul, 0>,
1657             basic_sse12_fp_binop_p_y<0x59, "mul", fmul>, VEX_4V;
1658
1659 let isCommutable = 0 in {
1660   defm VSUB : basic_sse12_fp_binop_s<0x5C, "sub", fsub, 0>,
1661               basic_sse12_fp_binop_s_int<0x5C, "sub", 0>,
1662               basic_sse12_fp_binop_p<0x5C, "sub", fsub, 0>,
1663               basic_sse12_fp_binop_p_y<0x5C, "sub", fsub>, VEX_4V;
1664   defm VDIV : basic_sse12_fp_binop_s<0x5E, "div", fdiv, 0>,
1665               basic_sse12_fp_binop_s_int<0x5E, "div", 0>,
1666               basic_sse12_fp_binop_p<0x5E, "div", fdiv, 0>,
1667               basic_sse12_fp_binop_p_y<0x5E, "div", fdiv>, VEX_4V;
1668   defm VMAX : basic_sse12_fp_binop_s<0x5F, "max", X86fmax, 0>,
1669               basic_sse12_fp_binop_s_int<0x5F, "max", 0>,
1670               basic_sse12_fp_binop_p<0x5F, "max", X86fmax, 0>,
1671               basic_sse12_fp_binop_p_int<0x5F, "max", 0>,
1672               basic_sse12_fp_binop_p_y<0x5F, "max", X86fmax>,
1673               basic_sse12_fp_binop_p_y_int<0x5F, "max">, VEX_4V;
1674   defm VMIN : basic_sse12_fp_binop_s<0x5D, "min", X86fmin, 0>,
1675               basic_sse12_fp_binop_s_int<0x5D, "min", 0>,
1676               basic_sse12_fp_binop_p<0x5D, "min", X86fmin, 0>,
1677               basic_sse12_fp_binop_p_int<0x5D, "min", 0>,
1678               basic_sse12_fp_binop_p_y_int<0x5D, "min">,
1679               basic_sse12_fp_binop_p_y<0x5D, "min", X86fmin>, VEX_4V;
1680 }
1681
1682 let Constraints = "$src1 = $dst" in {
1683   defm ADD : basic_sse12_fp_binop_s<0x58, "add", fadd>,
1684              basic_sse12_fp_binop_p<0x58, "add", fadd>,
1685              basic_sse12_fp_binop_s_int<0x58, "add">;
1686   defm MUL : basic_sse12_fp_binop_s<0x59, "mul", fmul>,
1687              basic_sse12_fp_binop_p<0x59, "mul", fmul>,
1688              basic_sse12_fp_binop_s_int<0x59, "mul">;
1689
1690   let isCommutable = 0 in {
1691     defm SUB : basic_sse12_fp_binop_s<0x5C, "sub", fsub>,
1692                basic_sse12_fp_binop_p<0x5C, "sub", fsub>,
1693                basic_sse12_fp_binop_s_int<0x5C, "sub">;
1694     defm DIV : basic_sse12_fp_binop_s<0x5E, "div", fdiv>,
1695                basic_sse12_fp_binop_p<0x5E, "div", fdiv>,
1696                basic_sse12_fp_binop_s_int<0x5E, "div">;
1697     defm MAX : basic_sse12_fp_binop_s<0x5F, "max", X86fmax>,
1698                basic_sse12_fp_binop_p<0x5F, "max", X86fmax>,
1699                basic_sse12_fp_binop_s_int<0x5F, "max">,
1700                basic_sse12_fp_binop_p_int<0x5F, "max">;
1701     defm MIN : basic_sse12_fp_binop_s<0x5D, "min", X86fmin>,
1702                basic_sse12_fp_binop_p<0x5D, "min", X86fmin>,
1703                basic_sse12_fp_binop_s_int<0x5D, "min">,
1704                basic_sse12_fp_binop_p_int<0x5D, "min">;
1705   }
1706 }
1707
1708 /// Unop Arithmetic
1709 /// In addition, we also have a special variant of the scalar form here to
1710 /// represent the associated intrinsic operation.  This form is unlike the
1711 /// plain scalar form, in that it takes an entire vector (instead of a
1712 /// scalar) and leaves the top elements undefined.
1713 ///
1714 /// And, we have a special variant form for a full-vector intrinsic form.
1715
1716 /// sse1_fp_unop_s - SSE1 unops in scalar form.
1717 multiclass sse1_fp_unop_s<bits<8> opc, string OpcodeStr,
1718                           SDNode OpNode, Intrinsic F32Int> {
1719   def SSr : SSI<opc, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src),
1720                 !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
1721                 [(set FR32:$dst, (OpNode FR32:$src))]>;
1722   // For scalar unary operations, fold a load into the operation
1723   // only in OptForSize mode. It eliminates an instruction, but it also
1724   // eliminates a whole-register clobber (the load), so it introduces a
1725   // partial register update condition.
1726   def SSm : I<opc, MRMSrcMem, (outs FR32:$dst), (ins f32mem:$src),
1727                 !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
1728                 [(set FR32:$dst, (OpNode (load addr:$src)))]>, XS,
1729             Requires<[HasSSE1, OptForSize]>;
1730   def SSr_Int : SSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1731                     !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
1732                     [(set VR128:$dst, (F32Int VR128:$src))]>;
1733   def SSm_Int : SSI<opc, MRMSrcMem, (outs VR128:$dst), (ins ssmem:$src),
1734                     !strconcat(OpcodeStr, "ss\t{$src, $dst|$dst, $src}"),
1735                     [(set VR128:$dst, (F32Int sse_load_f32:$src))]>;
1736 }
1737
1738 /// sse1_fp_unop_s_avx - AVX SSE1 unops in scalar form.
1739 multiclass sse1_fp_unop_s_avx<bits<8> opc, string OpcodeStr,
1740                               SDNode OpNode, Intrinsic F32Int> {
1741   def SSr : SSI<opc, MRMSrcReg, (outs FR32:$dst), (ins FR32:$src1, FR32:$src2),
1742                 !strconcat(OpcodeStr,
1743                            "ss\t{$src2, $src1, $dst|$dst, $src1, $src2}"), []>;
1744   def SSm : I<opc, MRMSrcMem, (outs FR32:$dst), (ins FR32:$src1, f32mem:$src2),
1745                 !strconcat(OpcodeStr,
1746                            "ss\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
1747                 []>, XS, Requires<[HasAVX, OptForSize]>;
1748   def SSr_Int : SSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1749                 !strconcat(OpcodeStr,
1750                            "ss\t{$src, $dst, $dst|$dst, $dst, $src}"),
1751                 [(set VR128:$dst, (F32Int VR128:$src))]>;
1752   def SSm_Int : SSI<opc, MRMSrcMem, (outs VR128:$dst), (ins ssmem:$src),
1753                 !strconcat(OpcodeStr,
1754                            "ss\t{$src, $dst, $dst|$dst, $dst, $src}"),
1755                 [(set VR128:$dst, (F32Int sse_load_f32:$src))]>;
1756 }
1757
1758 /// sse1_fp_unop_p - SSE1 unops in packed form.
1759 multiclass sse1_fp_unop_p<bits<8> opc, string OpcodeStr, SDNode OpNode> {
1760   def PSr : PSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1761               !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1762               [(set VR128:$dst, (v4f32 (OpNode VR128:$src)))]>;
1763   def PSm : PSI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1764                 !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1765                 [(set VR128:$dst, (OpNode (memopv4f32 addr:$src)))]>;
1766 }
1767
1768 /// sse1_fp_unop_p_y - AVX 256-bit SSE1 unops in packed form.
1769 multiclass sse1_fp_unop_p_y<bits<8> opc, string OpcodeStr, SDNode OpNode> {
1770   def PSYr : PSI<opc, MRMSrcReg, (outs VR256:$dst), (ins VR256:$src),
1771               !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1772               [(set VR256:$dst, (v8f32 (OpNode VR256:$src)))]>;
1773   def PSYm : PSI<opc, MRMSrcMem, (outs VR256:$dst), (ins f256mem:$src),
1774                 !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1775                 [(set VR256:$dst, (OpNode (memopv8f32 addr:$src)))]>;
1776 }
1777
1778 /// sse1_fp_unop_p_int - SSE1 intrinsics unops in packed forms.
1779 multiclass sse1_fp_unop_p_int<bits<8> opc, string OpcodeStr,
1780                               Intrinsic V4F32Int> {
1781   def PSr_Int : PSI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1782                     !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1783                     [(set VR128:$dst, (V4F32Int VR128:$src))]>;
1784   def PSm_Int : PSI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1785                     !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1786                     [(set VR128:$dst, (V4F32Int (memopv4f32 addr:$src)))]>;
1787 }
1788
1789 /// sse1_fp_unop_p_y_int - AVX 256-bit intrinsics unops in packed forms.
1790 multiclass sse1_fp_unop_p_y_int<bits<8> opc, string OpcodeStr,
1791                                 Intrinsic V4F32Int> {
1792   def PSYr_Int : PSI<opc, MRMSrcReg, (outs VR256:$dst), (ins VR256:$src),
1793                     !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1794                     [(set VR256:$dst, (V4F32Int VR256:$src))]>;
1795   def PSYm_Int : PSI<opc, MRMSrcMem, (outs VR256:$dst), (ins f256mem:$src),
1796                     !strconcat(OpcodeStr, "ps\t{$src, $dst|$dst, $src}"),
1797                     [(set VR256:$dst, (V4F32Int (memopv8f32 addr:$src)))]>;
1798 }
1799
1800 /// sse2_fp_unop_s - SSE2 unops in scalar form.
1801 multiclass sse2_fp_unop_s<bits<8> opc, string OpcodeStr,
1802                           SDNode OpNode, Intrinsic F64Int> {
1803   def SDr : SDI<opc, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src),
1804                 !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1805                 [(set FR64:$dst, (OpNode FR64:$src))]>;
1806   // See the comments in sse1_fp_unop_s for why this is OptForSize.
1807   def SDm : I<opc, MRMSrcMem, (outs FR64:$dst), (ins f64mem:$src),
1808                 !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1809                 [(set FR64:$dst, (OpNode (load addr:$src)))]>, XD,
1810             Requires<[HasSSE2, OptForSize]>;
1811   def SDr_Int : SDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1812                     !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1813                     [(set VR128:$dst, (F64Int VR128:$src))]>;
1814   def SDm_Int : SDI<opc, MRMSrcMem, (outs VR128:$dst), (ins sdmem:$src),
1815                     !strconcat(OpcodeStr, "sd\t{$src, $dst|$dst, $src}"),
1816                     [(set VR128:$dst, (F64Int sse_load_f64:$src))]>;
1817 }
1818
1819 /// sse2_fp_unop_s_avx - AVX SSE2 unops in scalar form.
1820 multiclass sse2_fp_unop_s_avx<bits<8> opc, string OpcodeStr,
1821                               SDNode OpNode, Intrinsic F64Int> {
1822   def SDr : SDI<opc, MRMSrcReg, (outs FR64:$dst), (ins FR64:$src1, FR64:$src2),
1823                !strconcat(OpcodeStr,
1824                           "sd\t{$src2, $src1, $dst|$dst, $src1, $src2}"), []>;
1825   def SDm : SDI<opc, MRMSrcMem, (outs FR64:$dst),
1826                (ins FR64:$src1, f64mem:$src2),
1827                !strconcat(OpcodeStr,
1828                           "sd\t{$src2, $src1, $dst|$dst, $src1, $src2}"), []>;
1829   def SDr_Int : SDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1830            !strconcat(OpcodeStr, "sd\t{$src, $dst, $dst|$dst, $dst, $src}"),
1831            [(set VR128:$dst, (F64Int VR128:$src))]>;
1832   def SDm_Int : SDI<opc, MRMSrcMem, (outs VR128:$dst), (ins sdmem:$src),
1833            !strconcat(OpcodeStr, "sd\t{$src, $dst, $dst|$dst, $dst, $src}"),
1834            [(set VR128:$dst, (F64Int sse_load_f64:$src))]>;
1835 }
1836
1837 /// sse2_fp_unop_p - SSE2 unops in vector forms.
1838 multiclass sse2_fp_unop_p<bits<8> opc, string OpcodeStr,
1839                           SDNode OpNode> {
1840   def PDr : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1841               !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1842               [(set VR128:$dst, (v2f64 (OpNode VR128:$src)))]>;
1843   def PDm : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1844                 !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1845                 [(set VR128:$dst, (OpNode (memopv2f64 addr:$src)))]>;
1846 }
1847
1848 /// sse2_fp_unop_p_y - AVX SSE2 256-bit unops in vector forms.
1849 multiclass sse2_fp_unop_p_y<bits<8> opc, string OpcodeStr, SDNode OpNode> {
1850   def PDYr : PDI<opc, MRMSrcReg, (outs VR256:$dst), (ins VR256:$src),
1851               !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1852               [(set VR256:$dst, (v4f64 (OpNode VR256:$src)))]>;
1853   def PDYm : PDI<opc, MRMSrcMem, (outs VR256:$dst), (ins f256mem:$src),
1854                 !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1855                 [(set VR256:$dst, (OpNode (memopv4f64 addr:$src)))]>;
1856 }
1857
1858 /// sse2_fp_unop_p_int - SSE2 intrinsic unops in vector forms.
1859 multiclass sse2_fp_unop_p_int<bits<8> opc, string OpcodeStr,
1860                               Intrinsic V2F64Int> {
1861   def PDr_Int : PDI<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
1862                     !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1863                     [(set VR128:$dst, (V2F64Int VR128:$src))]>;
1864   def PDm_Int : PDI<opc, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
1865                     !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1866                     [(set VR128:$dst, (V2F64Int (memopv2f64 addr:$src)))]>;
1867 }
1868
1869 /// sse2_fp_unop_p_y_int - AVX 256-bit intrinsic unops in vector forms.
1870 multiclass sse2_fp_unop_p_y_int<bits<8> opc, string OpcodeStr,
1871                                 Intrinsic V2F64Int> {
1872   def PDYr_Int : PDI<opc, MRMSrcReg, (outs VR256:$dst), (ins VR256:$src),
1873                     !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1874                     [(set VR256:$dst, (V2F64Int VR256:$src))]>;
1875   def PDYm_Int : PDI<opc, MRMSrcMem, (outs VR256:$dst), (ins f256mem:$src),
1876                     !strconcat(OpcodeStr, "pd\t{$src, $dst|$dst, $src}"),
1877                     [(set VR256:$dst, (V2F64Int (memopv4f64 addr:$src)))]>;
1878 }
1879
1880 let Predicates = [HasAVX] in {
1881   // Square root.
1882   defm VSQRT  : sse1_fp_unop_s_avx<0x51, "vsqrt", fsqrt, int_x86_sse_sqrt_ss>,
1883                 sse2_fp_unop_s_avx<0x51, "vsqrt", fsqrt, int_x86_sse2_sqrt_sd>,
1884                 VEX_4V;
1885
1886   defm VSQRT  : sse1_fp_unop_p<0x51, "vsqrt", fsqrt>,
1887                 sse2_fp_unop_p<0x51, "vsqrt", fsqrt>,
1888                 sse1_fp_unop_p_y<0x51, "vsqrt", fsqrt>,
1889                 sse2_fp_unop_p_y<0x51, "vsqrt", fsqrt>,
1890                 sse1_fp_unop_p_int<0x51, "vsqrt", int_x86_sse_sqrt_ps>,
1891                 sse2_fp_unop_p_int<0x51, "vsqrt", int_x86_sse2_sqrt_pd>,
1892                 sse1_fp_unop_p_y_int<0x51, "vsqrt", int_x86_avx_sqrt_ps_256>,
1893                 sse2_fp_unop_p_y_int<0x51, "vsqrt", int_x86_avx_sqrt_pd_256>,
1894                 VEX;
1895
1896   // Reciprocal approximations. Note that these typically require refinement
1897   // in order to obtain suitable precision.
1898   defm VRSQRT : sse1_fp_unop_s_avx<0x52, "vrsqrt", X86frsqrt,
1899                                    int_x86_sse_rsqrt_ss>, VEX_4V;
1900   defm VRSQRT : sse1_fp_unop_p<0x52, "vrsqrt", X86frsqrt>,
1901                 sse1_fp_unop_p_y<0x52, "vrsqrt", X86frsqrt>,
1902                 sse1_fp_unop_p_y_int<0x52, "vrsqrt", int_x86_avx_rsqrt_ps_256>,
1903                 sse1_fp_unop_p_int<0x52, "vrsqrt", int_x86_sse_rsqrt_ps>, VEX;
1904
1905   defm VRCP   : sse1_fp_unop_s_avx<0x53, "vrcp", X86frcp, int_x86_sse_rcp_ss>,
1906                                    VEX_4V;
1907   defm VRCP   : sse1_fp_unop_p<0x53, "vrcp", X86frcp>,
1908                 sse1_fp_unop_p_y<0x53, "vrcp", X86frcp>,
1909                 sse1_fp_unop_p_y_int<0x53, "vrcp", int_x86_avx_rcp_ps_256>,
1910                 sse1_fp_unop_p_int<0x53, "vrcp", int_x86_sse_rcp_ps>, VEX;
1911 }
1912
1913 def : Pat<(f32 (fsqrt FR32:$src)),
1914           (VSQRTSSr (f32 (IMPLICIT_DEF)), FR32:$src)>, Requires<[HasAVX]>;
1915 def : Pat<(f64 (fsqrt FR64:$src)),
1916           (VSQRTSDr (f64 (IMPLICIT_DEF)), FR64:$src)>, Requires<[HasAVX]>;
1917 def : Pat<(f64 (fsqrt (load addr:$src))),
1918           (VSQRTSDm (f64 (IMPLICIT_DEF)), addr:$src)>,
1919           Requires<[HasAVX, OptForSize]>;
1920 def : Pat<(f32 (fsqrt (load addr:$src))),
1921           (VSQRTSSm (f32 (IMPLICIT_DEF)), addr:$src)>,
1922           Requires<[HasAVX, OptForSize]>;
1923
1924 // Square root.
1925 defm SQRT  : sse1_fp_unop_s<0x51, "sqrt",  fsqrt, int_x86_sse_sqrt_ss>,
1926              sse1_fp_unop_p<0x51, "sqrt",  fsqrt>,
1927              sse1_fp_unop_p_int<0x51, "sqrt",  int_x86_sse_sqrt_ps>,
1928              sse2_fp_unop_s<0x51, "sqrt",  fsqrt, int_x86_sse2_sqrt_sd>,
1929              sse2_fp_unop_p<0x51, "sqrt",  fsqrt>,
1930              sse2_fp_unop_p_int<0x51, "sqrt", int_x86_sse2_sqrt_pd>;
1931
1932 // Reciprocal approximations. Note that these typically require refinement
1933 // in order to obtain suitable precision.
1934 defm RSQRT : sse1_fp_unop_s<0x52, "rsqrt", X86frsqrt, int_x86_sse_rsqrt_ss>,
1935              sse1_fp_unop_p<0x52, "rsqrt", X86frsqrt>,
1936              sse1_fp_unop_p_int<0x52, "rsqrt", int_x86_sse_rsqrt_ps>;
1937 defm RCP   : sse1_fp_unop_s<0x53, "rcp", X86frcp, int_x86_sse_rcp_ss>,
1938              sse1_fp_unop_p<0x53, "rcp", X86frcp>,
1939              sse1_fp_unop_p_int<0x53, "rcp", int_x86_sse_rcp_ps>;
1940
1941 // There is no f64 version of the reciprocal approximation instructions.
1942
1943 //===----------------------------------------------------------------------===//
1944 // SSE 1 & 2 - Non-temporal stores
1945 //===----------------------------------------------------------------------===//
1946
1947 let AddedComplexity = 400 in { // Prefer non-temporal versions
1948   def VMOVNTPSmr : VPSI<0x2B, MRMDestMem, (outs),
1949                        (ins f128mem:$dst, VR128:$src),
1950                        "movntps\t{$src, $dst|$dst, $src}",
1951                        [(alignednontemporalstore (v4f32 VR128:$src),
1952                                                  addr:$dst)]>, VEX;
1953   def VMOVNTPDmr : VPDI<0x2B, MRMDestMem, (outs),
1954                        (ins f128mem:$dst, VR128:$src),
1955                        "movntpd\t{$src, $dst|$dst, $src}",
1956                        [(alignednontemporalstore (v2f64 VR128:$src),
1957                                                  addr:$dst)]>, VEX;
1958   def VMOVNTDQ_64mr : VPDI<0xE7, MRMDestMem, (outs),
1959                         (ins f128mem:$dst, VR128:$src),
1960                         "movntdq\t{$src, $dst|$dst, $src}",
1961                         [(alignednontemporalstore (v2f64 VR128:$src),
1962                                                   addr:$dst)]>, VEX;
1963
1964   let ExeDomain = SSEPackedInt in
1965   def VMOVNTDQmr    : VPDI<0xE7, MRMDestMem, (outs),
1966                            (ins f128mem:$dst, VR128:$src),
1967                            "movntdq\t{$src, $dst|$dst, $src}",
1968                            [(alignednontemporalstore (v4f32 VR128:$src),
1969                                                      addr:$dst)]>, VEX;
1970
1971   def : Pat<(alignednontemporalstore (v2i64 VR128:$src), addr:$dst),
1972             (VMOVNTDQmr addr:$dst, VR128:$src)>, Requires<[HasAVX]>;
1973
1974   def VMOVNTPSYmr : VPSI<0x2B, MRMDestMem, (outs),
1975                        (ins f256mem:$dst, VR256:$src),
1976                        "movntps\t{$src, $dst|$dst, $src}",
1977                        [(alignednontemporalstore (v8f32 VR256:$src),
1978                                                  addr:$dst)]>, VEX;
1979   def VMOVNTPDYmr : VPDI<0x2B, MRMDestMem, (outs),
1980                        (ins f256mem:$dst, VR256:$src),
1981                        "movntpd\t{$src, $dst|$dst, $src}",
1982                        [(alignednontemporalstore (v4f64 VR256:$src),
1983                                                  addr:$dst)]>, VEX;
1984   def VMOVNTDQY_64mr : VPDI<0xE7, MRMDestMem, (outs),
1985                         (ins f256mem:$dst, VR256:$src),
1986                         "movntdq\t{$src, $dst|$dst, $src}",
1987                         [(alignednontemporalstore (v4f64 VR256:$src),
1988                                                   addr:$dst)]>, VEX;
1989   let ExeDomain = SSEPackedInt in
1990   def VMOVNTDQYmr : VPDI<0xE7, MRMDestMem, (outs),
1991                       (ins f256mem:$dst, VR256:$src),
1992                       "movntdq\t{$src, $dst|$dst, $src}",
1993                       [(alignednontemporalstore (v8f32 VR256:$src),
1994                                                 addr:$dst)]>, VEX;
1995 }
1996
1997 def : Pat<(int_x86_avx_movnt_dq_256 addr:$dst, VR256:$src),
1998           (VMOVNTDQYmr addr:$dst, VR256:$src)>;
1999 def : Pat<(int_x86_avx_movnt_pd_256 addr:$dst, VR256:$src),
2000           (VMOVNTPDYmr addr:$dst, VR256:$src)>;
2001 def : Pat<(int_x86_avx_movnt_ps_256 addr:$dst, VR256:$src),
2002           (VMOVNTPSYmr addr:$dst, VR256:$src)>;
2003
2004 let AddedComplexity = 400 in { // Prefer non-temporal versions
2005 def MOVNTPSmr : PSI<0x2B, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
2006                     "movntps\t{$src, $dst|$dst, $src}",
2007                     [(alignednontemporalstore (v4f32 VR128:$src), addr:$dst)]>;
2008 def MOVNTPDmr : PDI<0x2B, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
2009                     "movntpd\t{$src, $dst|$dst, $src}",
2010                     [(alignednontemporalstore(v2f64 VR128:$src), addr:$dst)]>;
2011
2012 def MOVNTDQ_64mr : PDI<0xE7, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
2013                     "movntdq\t{$src, $dst|$dst, $src}",
2014                     [(alignednontemporalstore (v2f64 VR128:$src), addr:$dst)]>;
2015
2016 let ExeDomain = SSEPackedInt in
2017 def MOVNTDQmr : PDI<0xE7, MRMDestMem, (outs), (ins f128mem:$dst, VR128:$src),
2018                     "movntdq\t{$src, $dst|$dst, $src}",
2019                     [(alignednontemporalstore (v4f32 VR128:$src), addr:$dst)]>;
2020
2021 def : Pat<(alignednontemporalstore (v2i64 VR128:$src), addr:$dst),
2022           (MOVNTDQmr addr:$dst, VR128:$src)>;
2023
2024 // There is no AVX form for instructions below this point
2025 def MOVNTImr : I<0xC3, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src),
2026                  "movnti{l}\t{$src, $dst|$dst, $src}",
2027                  [(nontemporalstore (i32 GR32:$src), addr:$dst)]>,
2028                TB, Requires<[HasSSE2]>;
2029 def MOVNTI_64mr : RI<0xC3, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
2030                      "movnti{q}\t{$src, $dst|$dst, $src}",
2031                      [(nontemporalstore (i64 GR64:$src), addr:$dst)]>,
2032                   TB, Requires<[HasSSE2]>;
2033 }
2034
2035 //===----------------------------------------------------------------------===//
2036 // SSE 1 & 2 - Misc Instructions (No AVX form)
2037 //===----------------------------------------------------------------------===//
2038
2039 // Prefetch intrinsic.
2040 def PREFETCHT0   : PSI<0x18, MRM1m, (outs), (ins i8mem:$src),
2041     "prefetcht0\t$src", [(prefetch addr:$src, imm, (i32 3), (i32 1))]>;
2042 def PREFETCHT1   : PSI<0x18, MRM2m, (outs), (ins i8mem:$src),
2043     "prefetcht1\t$src", [(prefetch addr:$src, imm, (i32 2), (i32 1))]>;
2044 def PREFETCHT2   : PSI<0x18, MRM3m, (outs), (ins i8mem:$src),
2045     "prefetcht2\t$src", [(prefetch addr:$src, imm, (i32 1), (i32 1))]>;
2046 def PREFETCHNTA  : PSI<0x18, MRM0m, (outs), (ins i8mem:$src),
2047     "prefetchnta\t$src", [(prefetch addr:$src, imm, (i32 0), (i32 1))]>;
2048
2049 // Load, store, and memory fence
2050 def SFENCE : I<0xAE, MRM_F8, (outs), (ins), "sfence", [(int_x86_sse_sfence)]>,
2051              TB, Requires<[HasSSE1]>;
2052 def : Pat<(X86SFence), (SFENCE)>;
2053
2054 // Alias instructions that map zero vector to pxor / xorp* for sse.
2055 // We set canFoldAsLoad because this can be converted to a constant-pool
2056 // load of an all-zeros value if folding it would be beneficial.
2057 // FIXME: Change encoding to pseudo! This is blocked right now by the x86
2058 // JIT implementation, it does not expand the instructions below like
2059 // X86MCInstLower does.
2060 let isReMaterializable = 1, isAsCheapAsAMove = 1, canFoldAsLoad = 1,
2061     isCodeGenOnly = 1 in {
2062 def V_SET0PS : PSI<0x57, MRMInitReg, (outs VR128:$dst), (ins), "",
2063                  [(set VR128:$dst, (v4f32 immAllZerosV))]>;
2064 def V_SET0PD : PDI<0x57, MRMInitReg, (outs VR128:$dst), (ins), "",
2065                  [(set VR128:$dst, (v2f64 immAllZerosV))]>;
2066 let ExeDomain = SSEPackedInt in
2067 def V_SET0PI : PDI<0xEF, MRMInitReg, (outs VR128:$dst), (ins), "",
2068                  [(set VR128:$dst, (v4i32 immAllZerosV))]>;
2069 }
2070
2071 // The same as done above but for AVX. The 128-bit versions are the
2072 // same, but re-encoded. The 256-bit does not support PI version, and
2073 // doesn't need it because on sandy bridge the register is set to zero
2074 // at the rename stage without using any execution unit, so SET0PSY
2075 // and SET0PDY can be used for vector int instructions without penalty
2076 // FIXME: Change encoding to pseudo! This is blocked right now by the x86
2077 // JIT implementatioan, it does not expand the instructions below like
2078 // X86MCInstLower does.
2079 let isReMaterializable = 1, isAsCheapAsAMove = 1, canFoldAsLoad = 1,
2080     isCodeGenOnly = 1, Predicates = [HasAVX] in {
2081 def AVX_SET0PS  : PSI<0x57, MRMInitReg, (outs VR128:$dst), (ins), "",
2082                    [(set VR128:$dst, (v4f32 immAllZerosV))]>, VEX_4V;
2083 def AVX_SET0PD  : PDI<0x57, MRMInitReg, (outs VR128:$dst), (ins), "",
2084                    [(set VR128:$dst, (v2f64 immAllZerosV))]>, VEX_4V;
2085 def AVX_SET0PSY : PSI<0x57, MRMInitReg, (outs VR256:$dst), (ins), "",
2086                    [(set VR256:$dst, (v8f32 immAllZerosV))]>, VEX_4V;
2087 def AVX_SET0PDY : PDI<0x57, MRMInitReg, (outs VR256:$dst), (ins), "",
2088                    [(set VR256:$dst, (v4f64 immAllZerosV))]>, VEX_4V;
2089 let ExeDomain = SSEPackedInt in
2090 def AVX_SET0PI  : PDI<0xEF, MRMInitReg, (outs VR128:$dst), (ins), "",
2091                    [(set VR128:$dst, (v4i32 immAllZerosV))]>;
2092 }
2093
2094 def : Pat<(v2i64 immAllZerosV), (V_SET0PI)>;
2095 def : Pat<(v8i16 immAllZerosV), (V_SET0PI)>;
2096 def : Pat<(v16i8 immAllZerosV), (V_SET0PI)>;
2097
2098 def : Pat<(f32 (vector_extract (v4f32 VR128:$src), (iPTR 0))),
2099           (f32 (EXTRACT_SUBREG (v4f32 VR128:$src), sub_ss))>;
2100
2101 //===----------------------------------------------------------------------===//
2102 // SSE 1 & 2 - Load/Store XCSR register
2103 //===----------------------------------------------------------------------===//
2104
2105 def VLDMXCSR : VPSI<0xAE, MRM2m, (outs), (ins i32mem:$src),
2106                   "ldmxcsr\t$src", [(int_x86_sse_ldmxcsr addr:$src)]>, VEX;
2107 def VSTMXCSR : VPSI<0xAE, MRM3m, (outs), (ins i32mem:$dst),
2108                   "stmxcsr\t$dst", [(int_x86_sse_stmxcsr addr:$dst)]>, VEX;
2109
2110 def LDMXCSR : PSI<0xAE, MRM2m, (outs), (ins i32mem:$src),
2111                   "ldmxcsr\t$src", [(int_x86_sse_ldmxcsr addr:$src)]>;
2112 def STMXCSR : PSI<0xAE, MRM3m, (outs), (ins i32mem:$dst),
2113                   "stmxcsr\t$dst", [(int_x86_sse_stmxcsr addr:$dst)]>;
2114
2115 //===---------------------------------------------------------------------===//
2116 // SSE2 - Move Aligned/Unaligned Packed Integer Instructions
2117 //===---------------------------------------------------------------------===//
2118
2119 let ExeDomain = SSEPackedInt in { // SSE integer instructions
2120
2121 let neverHasSideEffects = 1 in {
2122 def VMOVDQArr  : VPDI<0x6F, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2123                     "movdqa\t{$src, $dst|$dst, $src}", []>, VEX;
2124 def VMOVDQAYrr : VPDI<0x6F, MRMSrcReg, (outs VR256:$dst), (ins VR256:$src),
2125                     "movdqa\t{$src, $dst|$dst, $src}", []>, VEX;
2126 }
2127 def VMOVDQUrr  : VPDI<0x6F, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2128                     "movdqu\t{$src, $dst|$dst, $src}", []>, XS, VEX;
2129 def VMOVDQUYrr : VPDI<0x6F, MRMSrcReg, (outs VR256:$dst), (ins VR256:$src),
2130                     "movdqu\t{$src, $dst|$dst, $src}", []>, XS, VEX;
2131
2132 let canFoldAsLoad = 1, mayLoad = 1 in {
2133 def VMOVDQArm  : VPDI<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
2134                    "movdqa\t{$src, $dst|$dst, $src}", []>, VEX;
2135 def VMOVDQAYrm : VPDI<0x6F, MRMSrcMem, (outs VR256:$dst), (ins i256mem:$src),
2136                    "movdqa\t{$src, $dst|$dst, $src}", []>, VEX;
2137 let Predicates = [HasAVX] in {
2138   def VMOVDQUrm  : I<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
2139                     "vmovdqu\t{$src, $dst|$dst, $src}",[]>, XS, VEX;
2140   def VMOVDQUYrm : I<0x6F, MRMSrcMem, (outs VR256:$dst), (ins i256mem:$src),
2141                     "vmovdqu\t{$src, $dst|$dst, $src}",[]>, XS, VEX;
2142 }
2143 }
2144
2145 let mayStore = 1 in {
2146 def VMOVDQAmr  : VPDI<0x7F, MRMDestMem, (outs),
2147                      (ins i128mem:$dst, VR128:$src),
2148                      "movdqa\t{$src, $dst|$dst, $src}", []>, VEX;
2149 def VMOVDQAYmr : VPDI<0x7F, MRMDestMem, (outs),
2150                      (ins i256mem:$dst, VR256:$src),
2151                      "movdqa\t{$src, $dst|$dst, $src}", []>, VEX;
2152 let Predicates = [HasAVX] in {
2153 def VMOVDQUmr  : I<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
2154                   "vmovdqu\t{$src, $dst|$dst, $src}",[]>, XS, VEX;
2155 def VMOVDQUYmr : I<0x7F, MRMDestMem, (outs), (ins i256mem:$dst, VR256:$src),
2156                   "vmovdqu\t{$src, $dst|$dst, $src}",[]>, XS, VEX;
2157 }
2158 }
2159
2160 let neverHasSideEffects = 1 in
2161 def MOVDQArr : PDI<0x6F, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2162                    "movdqa\t{$src, $dst|$dst, $src}", []>;
2163
2164 def MOVDQUrr :   I<0x6F, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
2165                    "movdqu\t{$src, $dst|$dst, $src}",
2166                    []>, XS, Requires<[HasSSE2]>;
2167
2168 let canFoldAsLoad = 1, mayLoad = 1 in {
2169 def MOVDQArm : PDI<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
2170                    "movdqa\t{$src, $dst|$dst, $src}",
2171                    [/*(set VR128:$dst, (alignedloadv2i64 addr:$src))*/]>;
2172 def MOVDQUrm :   I<0x6F, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
2173                    "movdqu\t{$src, $dst|$dst, $src}",
2174                    [/*(set VR128:$dst, (loadv2i64 addr:$src))*/]>,
2175                  XS, Requires<[HasSSE2]>;
2176 }
2177
2178 let mayStore = 1 in {
2179 def MOVDQAmr : PDI<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
2180                    "movdqa\t{$src, $dst|$dst, $src}",
2181                    [/*(alignedstore (v2i64 VR128:$src), addr:$dst)*/]>;
2182 def MOVDQUmr :   I<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
2183                    "movdqu\t{$src, $dst|$dst, $src}",
2184                    [/*(store (v2i64 VR128:$src), addr:$dst)*/]>,
2185                  XS, Requires<[HasSSE2]>;
2186 }
2187
2188 // Intrinsic forms of MOVDQU load and store
2189 def VMOVDQUmr_Int : I<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
2190                        "vmovdqu\t{$src, $dst|$dst, $src}",
2191                        [(int_x86_sse2_storeu_dq addr:$dst, VR128:$src)]>,
2192                      XS, VEX, Requires<[HasAVX]>;
2193
2194 def MOVDQUmr_Int :   I<0x7F, MRMDestMem, (outs), (ins i128mem:$dst, VR128:$src),
2195                        "movdqu\t{$src, $dst|$dst, $src}",
2196                        [(int_x86_sse2_storeu_dq addr:$dst, VR128:$src)]>,
2197                      XS, Requires<[HasSSE2]>;
2198
2199 } // ExeDomain = SSEPackedInt
2200
2201 def : Pat<(int_x86_avx_loadu_dq_256 addr:$src), (VMOVDQUYrm addr:$src)>;
2202 def : Pat<(int_x86_avx_storeu_dq_256 addr:$dst, VR256:$src),
2203           (VMOVDQUYmr addr:$dst, VR256:$src)>;
2204
2205 //===---------------------------------------------------------------------===//
2206 // SSE2 - Packed Integer Arithmetic Instructions
2207 //===---------------------------------------------------------------------===//
2208
2209 let ExeDomain = SSEPackedInt in { // SSE integer instructions
2210
2211 multiclass PDI_binop_rm_int<bits<8> opc, string OpcodeStr, Intrinsic IntId,
2212                             bit IsCommutable = 0, bit Is2Addr = 1> {
2213   let isCommutable = IsCommutable in
2214   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst),
2215        (ins VR128:$src1, VR128:$src2),
2216        !if(Is2Addr,
2217            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2218            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2219        [(set VR128:$dst, (IntId VR128:$src1, VR128:$src2))]>;
2220   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst),
2221        (ins VR128:$src1, i128mem:$src2),
2222        !if(Is2Addr,
2223            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2224            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2225        [(set VR128:$dst, (IntId VR128:$src1,
2226                                 (bitconvert (memopv2i64 addr:$src2))))]>;
2227 }
2228
2229 multiclass PDI_binop_rmi_int<bits<8> opc, bits<8> opc2, Format ImmForm,
2230                              string OpcodeStr, Intrinsic IntId,
2231                              Intrinsic IntId2, bit Is2Addr = 1> {
2232   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst),
2233        (ins VR128:$src1, VR128:$src2),
2234        !if(Is2Addr,
2235            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2236            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2237        [(set VR128:$dst, (IntId VR128:$src1, VR128:$src2))]>;
2238   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst),
2239        (ins VR128:$src1, i128mem:$src2),
2240        !if(Is2Addr,
2241            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2242            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2243        [(set VR128:$dst, (IntId VR128:$src1,
2244                                       (bitconvert (memopv2i64 addr:$src2))))]>;
2245   def ri : PDIi8<opc2, ImmForm, (outs VR128:$dst),
2246        (ins VR128:$src1, i32i8imm:$src2),
2247        !if(Is2Addr,
2248            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2249            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2250        [(set VR128:$dst, (IntId2 VR128:$src1, (i32 imm:$src2)))]>;
2251 }
2252
2253 /// PDI_binop_rm - Simple SSE2 binary operator.
2254 multiclass PDI_binop_rm<bits<8> opc, string OpcodeStr, SDNode OpNode,
2255                         ValueType OpVT, bit IsCommutable = 0, bit Is2Addr = 1> {
2256   let isCommutable = IsCommutable in
2257   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst),
2258        (ins VR128:$src1, VR128:$src2),
2259        !if(Is2Addr,
2260            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2261            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2262        [(set VR128:$dst, (OpVT (OpNode VR128:$src1, VR128:$src2)))]>;
2263   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst),
2264        (ins VR128:$src1, i128mem:$src2),
2265        !if(Is2Addr,
2266            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2267            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2268        [(set VR128:$dst, (OpVT (OpNode VR128:$src1,
2269                                      (bitconvert (memopv2i64 addr:$src2)))))]>;
2270 }
2271
2272 /// PDI_binop_rm_v2i64 - Simple SSE2 binary operator whose type is v2i64.
2273 ///
2274 /// FIXME: we could eliminate this and use PDI_binop_rm instead if tblgen knew
2275 /// to collapse (bitconvert VT to VT) into its operand.
2276 ///
2277 multiclass PDI_binop_rm_v2i64<bits<8> opc, string OpcodeStr, SDNode OpNode,
2278                               bit IsCommutable = 0, bit Is2Addr = 1> {
2279   let isCommutable = IsCommutable in
2280   def rr : PDI<opc, MRMSrcReg, (outs VR128:$dst),
2281        (ins VR128:$src1, VR128:$src2),
2282        !if(Is2Addr,
2283            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2284            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2285        [(set VR128:$dst, (v2i64 (OpNode VR128:$src1, VR128:$src2)))]>;
2286   def rm : PDI<opc, MRMSrcMem, (outs VR128:$dst),
2287        (ins VR128:$src1, i128mem:$src2),
2288        !if(Is2Addr,
2289            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
2290            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2291        [(set VR128:$dst, (OpNode VR128:$src1, (memopv2i64 addr:$src2)))]>;
2292 }
2293
2294 } // ExeDomain = SSEPackedInt
2295
2296 // 128-bit Integer Arithmetic
2297
2298 let Predicates = [HasAVX] in {
2299 defm VPADDB  : PDI_binop_rm<0xFC, "vpaddb", add, v16i8, 1, 0 /*3addr*/>, VEX_4V;
2300 defm VPADDW  : PDI_binop_rm<0xFD, "vpaddw", add, v8i16, 1, 0>, VEX_4V;
2301 defm VPADDD  : PDI_binop_rm<0xFE, "vpaddd", add, v4i32, 1, 0>, VEX_4V;
2302 defm VPADDQ  : PDI_binop_rm_v2i64<0xD4, "vpaddq", add, 1, 0>, VEX_4V;
2303 defm VPMULLW : PDI_binop_rm<0xD5, "vpmullw", mul, v8i16, 1, 0>, VEX_4V;
2304 defm VPSUBB : PDI_binop_rm<0xF8, "vpsubb", sub, v16i8, 0, 0>, VEX_4V;
2305 defm VPSUBW : PDI_binop_rm<0xF9, "vpsubw", sub, v8i16, 0, 0>, VEX_4V;
2306 defm VPSUBD : PDI_binop_rm<0xFA, "vpsubd", sub, v4i32, 0, 0>, VEX_4V;
2307 defm VPSUBQ : PDI_binop_rm_v2i64<0xFB, "vpsubq", sub, 0, 0>, VEX_4V;
2308
2309 // Intrinsic forms
2310 defm VPSUBSB  : PDI_binop_rm_int<0xE8, "vpsubsb" , int_x86_sse2_psubs_b, 0, 0>,
2311                                  VEX_4V;
2312 defm VPSUBSW  : PDI_binop_rm_int<0xE9, "vpsubsw" , int_x86_sse2_psubs_w, 0, 0>,
2313                                  VEX_4V;
2314 defm VPSUBUSB : PDI_binop_rm_int<0xD8, "vpsubusb", int_x86_sse2_psubus_b, 0, 0>,
2315                                  VEX_4V;
2316 defm VPSUBUSW : PDI_binop_rm_int<0xD9, "vpsubusw", int_x86_sse2_psubus_w, 0, 0>,
2317                                  VEX_4V;
2318 defm VPADDSB  : PDI_binop_rm_int<0xEC, "vpaddsb" , int_x86_sse2_padds_b, 1, 0>,
2319                                  VEX_4V;
2320 defm VPADDSW  : PDI_binop_rm_int<0xED, "vpaddsw" , int_x86_sse2_padds_w, 1, 0>,
2321                                  VEX_4V;
2322 defm VPADDUSB : PDI_binop_rm_int<0xDC, "vpaddusb", int_x86_sse2_paddus_b, 1, 0>,
2323                                  VEX_4V;
2324 defm VPADDUSW : PDI_binop_rm_int<0xDD, "vpaddusw", int_x86_sse2_paddus_w, 1, 0>,
2325                                  VEX_4V;
2326 defm VPMULHUW : PDI_binop_rm_int<0xE4, "vpmulhuw", int_x86_sse2_pmulhu_w, 1, 0>,
2327                                  VEX_4V;
2328 defm VPMULHW  : PDI_binop_rm_int<0xE5, "vpmulhw" , int_x86_sse2_pmulh_w, 1, 0>,
2329                                  VEX_4V;
2330 defm VPMULUDQ : PDI_binop_rm_int<0xF4, "vpmuludq", int_x86_sse2_pmulu_dq, 1, 0>,
2331                                  VEX_4V;
2332 defm VPMADDWD : PDI_binop_rm_int<0xF5, "vpmaddwd", int_x86_sse2_pmadd_wd, 1, 0>,
2333                                  VEX_4V;
2334 defm VPAVGB   : PDI_binop_rm_int<0xE0, "vpavgb", int_x86_sse2_pavg_b, 1, 0>,
2335                                  VEX_4V;
2336 defm VPAVGW   : PDI_binop_rm_int<0xE3, "vpavgw", int_x86_sse2_pavg_w, 1, 0>,
2337                                  VEX_4V;
2338 defm VPMINUB  : PDI_binop_rm_int<0xDA, "vpminub", int_x86_sse2_pminu_b, 1, 0>,
2339                                  VEX_4V;
2340 defm VPMINSW  : PDI_binop_rm_int<0xEA, "vpminsw", int_x86_sse2_pmins_w, 1, 0>,
2341                                  VEX_4V;
2342 defm VPMAXUB  : PDI_binop_rm_int<0xDE, "vpmaxub", int_x86_sse2_pmaxu_b, 1, 0>,
2343                                  VEX_4V;
2344 defm VPMAXSW  : PDI_binop_rm_int<0xEE, "vpmaxsw", int_x86_sse2_pmaxs_w, 1, 0>,
2345                                  VEX_4V;
2346 defm VPSADBW  : PDI_binop_rm_int<0xF6, "vpsadbw", int_x86_sse2_psad_bw, 1, 0>,
2347                                  VEX_4V;
2348 }
2349
2350 let Constraints = "$src1 = $dst" in {
2351 defm PADDB  : PDI_binop_rm<0xFC, "paddb", add, v16i8, 1>;
2352 defm PADDW  : PDI_binop_rm<0xFD, "paddw", add, v8i16, 1>;
2353 defm PADDD  : PDI_binop_rm<0xFE, "paddd", add, v4i32, 1>;
2354 defm PADDQ  : PDI_binop_rm_v2i64<0xD4, "paddq", add, 1>;
2355 defm PMULLW : PDI_binop_rm<0xD5, "pmullw", mul, v8i16, 1>;
2356 defm PSUBB : PDI_binop_rm<0xF8, "psubb", sub, v16i8>;
2357 defm PSUBW : PDI_binop_rm<0xF9, "psubw", sub, v8i16>;
2358 defm PSUBD : PDI_binop_rm<0xFA, "psubd", sub, v4i32>;
2359 defm PSUBQ : PDI_binop_rm_v2i64<0xFB, "psubq", sub>;
2360
2361 // Intrinsic forms
2362 defm PSUBSB  : PDI_binop_rm_int<0xE8, "psubsb" , int_x86_sse2_psubs_b>;
2363 defm PSUBSW  : PDI_binop_rm_int<0xE9, "psubsw" , int_x86_sse2_psubs_w>;
2364 defm PSUBUSB : PDI_binop_rm_int<0xD8, "psubusb", int_x86_sse2_psubus_b>;
2365 defm PSUBUSW : PDI_binop_rm_int<0xD9, "psubusw", int_x86_sse2_psubus_w>;
2366 defm PADDSB  : PDI_binop_rm_int<0xEC, "paddsb" , int_x86_sse2_padds_b, 1>;
2367 defm PADDSW  : PDI_binop_rm_int<0xED, "paddsw" , int_x86_sse2_padds_w, 1>;
2368 defm PADDUSB : PDI_binop_rm_int<0xDC, "paddusb", int_x86_sse2_paddus_b, 1>;
2369 defm PADDUSW : PDI_binop_rm_int<0xDD, "paddusw", int_x86_sse2_paddus_w, 1>;
2370 defm PMULHUW : PDI_binop_rm_int<0xE4, "pmulhuw", int_x86_sse2_pmulhu_w, 1>;
2371 defm PMULHW  : PDI_binop_rm_int<0xE5, "pmulhw" , int_x86_sse2_pmulh_w, 1>;
2372 defm PMULUDQ : PDI_binop_rm_int<0xF4, "pmuludq", int_x86_sse2_pmulu_dq, 1>;
2373 defm PMADDWD : PDI_binop_rm_int<0xF5, "pmaddwd", int_x86_sse2_pmadd_wd, 1>;
2374 defm PAVGB   : PDI_binop_rm_int<0xE0, "pavgb", int_x86_sse2_pavg_b, 1>;
2375 defm PAVGW   : PDI_binop_rm_int<0xE3, "pavgw", int_x86_sse2_pavg_w, 1>;
2376 defm PMINUB  : PDI_binop_rm_int<0xDA, "pminub", int_x86_sse2_pminu_b, 1>;
2377 defm PMINSW  : PDI_binop_rm_int<0xEA, "pminsw", int_x86_sse2_pmins_w, 1>;
2378 defm PMAXUB  : PDI_binop_rm_int<0xDE, "pmaxub", int_x86_sse2_pmaxu_b, 1>;
2379 defm PMAXSW  : PDI_binop_rm_int<0xEE, "pmaxsw", int_x86_sse2_pmaxs_w, 1>;
2380 defm PSADBW  : PDI_binop_rm_int<0xF6, "psadbw", int_x86_sse2_psad_bw, 1>;
2381
2382 } // Constraints = "$src1 = $dst"
2383
2384 //===---------------------------------------------------------------------===//
2385 // SSE2 - Packed Integer Logical Instructions
2386 //===---------------------------------------------------------------------===//
2387
2388 let Predicates = [HasAVX] in {
2389 defm VPSLLW : PDI_binop_rmi_int<0xF1, 0x71, MRM6r, "vpsllw",
2390                                 int_x86_sse2_psll_w, int_x86_sse2_pslli_w, 0>,
2391                                 VEX_4V;
2392 defm VPSLLD : PDI_binop_rmi_int<0xF2, 0x72, MRM6r, "vpslld",
2393                                 int_x86_sse2_psll_d, int_x86_sse2_pslli_d, 0>,
2394                                 VEX_4V;
2395 defm VPSLLQ : PDI_binop_rmi_int<0xF3, 0x73, MRM6r, "vpsllq",
2396                                 int_x86_sse2_psll_q, int_x86_sse2_pslli_q, 0>,
2397                                 VEX_4V;
2398
2399 defm VPSRLW : PDI_binop_rmi_int<0xD1, 0x71, MRM2r, "vpsrlw",
2400                                 int_x86_sse2_psrl_w, int_x86_sse2_psrli_w, 0>,
2401                                 VEX_4V;
2402 defm VPSRLD : PDI_binop_rmi_int<0xD2, 0x72, MRM2r, "vpsrld",
2403                                 int_x86_sse2_psrl_d, int_x86_sse2_psrli_d, 0>,
2404                                 VEX_4V;
2405 defm VPSRLQ : PDI_binop_rmi_int<0xD3, 0x73, MRM2r, "vpsrlq",
2406                                 int_x86_sse2_psrl_q, int_x86_sse2_psrli_q, 0>,
2407                                 VEX_4V;
2408
2409 defm VPSRAW : PDI_binop_rmi_int<0xE1, 0x71, MRM4r, "vpsraw",
2410                                 int_x86_sse2_psra_w, int_x86_sse2_psrai_w, 0>,
2411                                 VEX_4V;
2412 defm VPSRAD : PDI_binop_rmi_int<0xE2, 0x72, MRM4r, "vpsrad",
2413                                 int_x86_sse2_psra_d, int_x86_sse2_psrai_d, 0>,
2414                                 VEX_4V;
2415
2416 defm VPAND : PDI_binop_rm_v2i64<0xDB, "vpand", and, 1, 0>, VEX_4V;
2417 defm VPOR  : PDI_binop_rm_v2i64<0xEB, "vpor" , or, 1, 0>, VEX_4V;
2418 defm VPXOR : PDI_binop_rm_v2i64<0xEF, "vpxor", xor, 1, 0>, VEX_4V;
2419
2420 let ExeDomain = SSEPackedInt in {
2421   let neverHasSideEffects = 1 in {
2422     // 128-bit logical shifts.
2423     def VPSLLDQri : PDIi8<0x73, MRM7r,
2424                       (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
2425                       "vpslldq\t{$src2, $src1, $dst|$dst, $src1, $src2}", []>,
2426                       VEX_4V;
2427     def VPSRLDQri : PDIi8<0x73, MRM3r,
2428                       (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
2429                       "vpsrldq\t{$src2, $src1, $dst|$dst, $src1, $src2}", []>,
2430                       VEX_4V;
2431     // PSRADQri doesn't exist in SSE[1-3].
2432   }
2433   def VPANDNrr : PDI<0xDF, MRMSrcReg,
2434                     (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2435                     "vpandn\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2436                     [(set VR128:$dst, (v2i64 (and (vnot VR128:$src1),
2437                                               VR128:$src2)))]>, VEX_4V;
2438
2439   def VPANDNrm : PDI<0xDF, MRMSrcMem,
2440                     (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2441                     "vpandn\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2442                     [(set VR128:$dst, (v2i64 (and (vnot VR128:$src1),
2443                                               (memopv2i64 addr:$src2))))]>,
2444                                               VEX_4V;
2445 }
2446 }
2447
2448 let Constraints = "$src1 = $dst" in {
2449 defm PSLLW : PDI_binop_rmi_int<0xF1, 0x71, MRM6r, "psllw",
2450                                int_x86_sse2_psll_w, int_x86_sse2_pslli_w>;
2451 defm PSLLD : PDI_binop_rmi_int<0xF2, 0x72, MRM6r, "pslld",
2452                                int_x86_sse2_psll_d, int_x86_sse2_pslli_d>;
2453 defm PSLLQ : PDI_binop_rmi_int<0xF3, 0x73, MRM6r, "psllq",
2454                                int_x86_sse2_psll_q, int_x86_sse2_pslli_q>;
2455
2456 defm PSRLW : PDI_binop_rmi_int<0xD1, 0x71, MRM2r, "psrlw",
2457                                int_x86_sse2_psrl_w, int_x86_sse2_psrli_w>;
2458 defm PSRLD : PDI_binop_rmi_int<0xD2, 0x72, MRM2r, "psrld",
2459                                int_x86_sse2_psrl_d, int_x86_sse2_psrli_d>;
2460 defm PSRLQ : PDI_binop_rmi_int<0xD3, 0x73, MRM2r, "psrlq",
2461                                int_x86_sse2_psrl_q, int_x86_sse2_psrli_q>;
2462
2463 defm PSRAW : PDI_binop_rmi_int<0xE1, 0x71, MRM4r, "psraw",
2464                                int_x86_sse2_psra_w, int_x86_sse2_psrai_w>;
2465 defm PSRAD : PDI_binop_rmi_int<0xE2, 0x72, MRM4r, "psrad",
2466                                int_x86_sse2_psra_d, int_x86_sse2_psrai_d>;
2467
2468 defm PAND : PDI_binop_rm_v2i64<0xDB, "pand", and, 1>;
2469 defm POR  : PDI_binop_rm_v2i64<0xEB, "por" , or, 1>;
2470 defm PXOR : PDI_binop_rm_v2i64<0xEF, "pxor", xor, 1>;
2471
2472 let ExeDomain = SSEPackedInt in {
2473   let neverHasSideEffects = 1 in {
2474     // 128-bit logical shifts.
2475     def PSLLDQri : PDIi8<0x73, MRM7r,
2476                          (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
2477                          "pslldq\t{$src2, $dst|$dst, $src2}", []>;
2478     def PSRLDQri : PDIi8<0x73, MRM3r,
2479                          (outs VR128:$dst), (ins VR128:$src1, i32i8imm:$src2),
2480                          "psrldq\t{$src2, $dst|$dst, $src2}", []>;
2481     // PSRADQri doesn't exist in SSE[1-3].
2482   }
2483   def PANDNrr : PDI<0xDF, MRMSrcReg,
2484                     (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2485                     "pandn\t{$src2, $dst|$dst, $src2}", []>;
2486
2487   def PANDNrm : PDI<0xDF, MRMSrcMem,
2488                     (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2489                     "pandn\t{$src2, $dst|$dst, $src2}", []>;
2490 }
2491 } // Constraints = "$src1 = $dst"
2492
2493 let Predicates = [HasAVX] in {
2494   def : Pat<(int_x86_sse2_psll_dq VR128:$src1, imm:$src2),
2495             (v2i64 (VPSLLDQri VR128:$src1, (BYTE_imm imm:$src2)))>;
2496   def : Pat<(int_x86_sse2_psrl_dq VR128:$src1, imm:$src2),
2497             (v2i64 (VPSRLDQri VR128:$src1, (BYTE_imm imm:$src2)))>;
2498   def : Pat<(int_x86_sse2_psll_dq_bs VR128:$src1, imm:$src2),
2499             (v2i64 (VPSLLDQri VR128:$src1, imm:$src2))>;
2500   def : Pat<(int_x86_sse2_psrl_dq_bs VR128:$src1, imm:$src2),
2501             (v2i64 (VPSRLDQri VR128:$src1, imm:$src2))>;
2502   def : Pat<(v2f64 (X86fsrl VR128:$src1, i32immSExt8:$src2)),
2503             (v2f64 (VPSRLDQri VR128:$src1, (BYTE_imm imm:$src2)))>;
2504
2505   // Shift up / down and insert zero's.
2506   def : Pat<(v2i64 (X86vshl  VR128:$src, (i8 imm:$amt))),
2507             (v2i64 (VPSLLDQri VR128:$src, (BYTE_imm imm:$amt)))>;
2508   def : Pat<(v2i64 (X86vshr  VR128:$src, (i8 imm:$amt))),
2509             (v2i64 (VPSRLDQri VR128:$src, (BYTE_imm imm:$amt)))>;
2510 }
2511
2512 let Predicates = [HasSSE2] in {
2513   def : Pat<(int_x86_sse2_psll_dq VR128:$src1, imm:$src2),
2514             (v2i64 (PSLLDQri VR128:$src1, (BYTE_imm imm:$src2)))>;
2515   def : Pat<(int_x86_sse2_psrl_dq VR128:$src1, imm:$src2),
2516             (v2i64 (PSRLDQri VR128:$src1, (BYTE_imm imm:$src2)))>;
2517   def : Pat<(int_x86_sse2_psll_dq_bs VR128:$src1, imm:$src2),
2518             (v2i64 (PSLLDQri VR128:$src1, imm:$src2))>;
2519   def : Pat<(int_x86_sse2_psrl_dq_bs VR128:$src1, imm:$src2),
2520             (v2i64 (PSRLDQri VR128:$src1, imm:$src2))>;
2521   def : Pat<(v2f64 (X86fsrl VR128:$src1, i32immSExt8:$src2)),
2522             (v2f64 (PSRLDQri VR128:$src1, (BYTE_imm imm:$src2)))>;
2523
2524   // Shift up / down and insert zero's.
2525   def : Pat<(v2i64 (X86vshl  VR128:$src, (i8 imm:$amt))),
2526             (v2i64 (PSLLDQri VR128:$src, (BYTE_imm imm:$amt)))>;
2527   def : Pat<(v2i64 (X86vshr  VR128:$src, (i8 imm:$amt))),
2528             (v2i64 (PSRLDQri VR128:$src, (BYTE_imm imm:$amt)))>;
2529 }
2530
2531 //===---------------------------------------------------------------------===//
2532 // SSE2 - Packed Integer Comparison Instructions
2533 //===---------------------------------------------------------------------===//
2534
2535 let Predicates = [HasAVX] in {
2536   defm VPCMPEQB  : PDI_binop_rm_int<0x74, "vpcmpeqb", int_x86_sse2_pcmpeq_b, 1,
2537                                     0>, VEX_4V;
2538   defm VPCMPEQW  : PDI_binop_rm_int<0x75, "vpcmpeqw", int_x86_sse2_pcmpeq_w, 1,
2539                                     0>, VEX_4V;
2540   defm VPCMPEQD  : PDI_binop_rm_int<0x76, "vpcmpeqd", int_x86_sse2_pcmpeq_d, 1,
2541                                     0>, VEX_4V;
2542   defm VPCMPGTB  : PDI_binop_rm_int<0x64, "vpcmpgtb", int_x86_sse2_pcmpgt_b, 0,
2543                                     0>, VEX_4V;
2544   defm VPCMPGTW  : PDI_binop_rm_int<0x65, "vpcmpgtw", int_x86_sse2_pcmpgt_w, 0,
2545                                     0>, VEX_4V;
2546   defm VPCMPGTD  : PDI_binop_rm_int<0x66, "vpcmpgtd", int_x86_sse2_pcmpgt_d, 0,
2547                                     0>, VEX_4V;
2548 }
2549
2550 let Constraints = "$src1 = $dst" in {
2551   defm PCMPEQB  : PDI_binop_rm_int<0x74, "pcmpeqb", int_x86_sse2_pcmpeq_b, 1>;
2552   defm PCMPEQW  : PDI_binop_rm_int<0x75, "pcmpeqw", int_x86_sse2_pcmpeq_w, 1>;
2553   defm PCMPEQD  : PDI_binop_rm_int<0x76, "pcmpeqd", int_x86_sse2_pcmpeq_d, 1>;
2554   defm PCMPGTB  : PDI_binop_rm_int<0x64, "pcmpgtb", int_x86_sse2_pcmpgt_b>;
2555   defm PCMPGTW  : PDI_binop_rm_int<0x65, "pcmpgtw", int_x86_sse2_pcmpgt_w>;
2556   defm PCMPGTD  : PDI_binop_rm_int<0x66, "pcmpgtd", int_x86_sse2_pcmpgt_d>;
2557 } // Constraints = "$src1 = $dst"
2558
2559 def : Pat<(v16i8 (X86pcmpeqb VR128:$src1, VR128:$src2)),
2560           (PCMPEQBrr VR128:$src1, VR128:$src2)>;
2561 def : Pat<(v16i8 (X86pcmpeqb VR128:$src1, (memop addr:$src2))),
2562           (PCMPEQBrm VR128:$src1, addr:$src2)>;
2563 def : Pat<(v8i16 (X86pcmpeqw VR128:$src1, VR128:$src2)),
2564           (PCMPEQWrr VR128:$src1, VR128:$src2)>;
2565 def : Pat<(v8i16 (X86pcmpeqw VR128:$src1, (memop addr:$src2))),
2566           (PCMPEQWrm VR128:$src1, addr:$src2)>;
2567 def : Pat<(v4i32 (X86pcmpeqd VR128:$src1, VR128:$src2)),
2568           (PCMPEQDrr VR128:$src1, VR128:$src2)>;
2569 def : Pat<(v4i32 (X86pcmpeqd VR128:$src1, (memop addr:$src2))),
2570           (PCMPEQDrm VR128:$src1, addr:$src2)>;
2571
2572 def : Pat<(v16i8 (X86pcmpgtb VR128:$src1, VR128:$src2)),
2573           (PCMPGTBrr VR128:$src1, VR128:$src2)>;
2574 def : Pat<(v16i8 (X86pcmpgtb VR128:$src1, (memop addr:$src2))),
2575           (PCMPGTBrm VR128:$src1, addr:$src2)>;
2576 def : Pat<(v8i16 (X86pcmpgtw VR128:$src1, VR128:$src2)),
2577           (PCMPGTWrr VR128:$src1, VR128:$src2)>;
2578 def : Pat<(v8i16 (X86pcmpgtw VR128:$src1, (memop addr:$src2))),
2579           (PCMPGTWrm VR128:$src1, addr:$src2)>;
2580 def : Pat<(v4i32 (X86pcmpgtd VR128:$src1, VR128:$src2)),
2581           (PCMPGTDrr VR128:$src1, VR128:$src2)>;
2582 def : Pat<(v4i32 (X86pcmpgtd VR128:$src1, (memop addr:$src2))),
2583           (PCMPGTDrm VR128:$src1, addr:$src2)>;
2584
2585 //===---------------------------------------------------------------------===//
2586 // SSE2 - Packed Integer Pack Instructions
2587 //===---------------------------------------------------------------------===//
2588
2589 let Predicates = [HasAVX] in {
2590 defm VPACKSSWB : PDI_binop_rm_int<0x63, "vpacksswb", int_x86_sse2_packsswb_128,
2591                                   0, 0>, VEX_4V;
2592 defm VPACKSSDW : PDI_binop_rm_int<0x6B, "vpackssdw", int_x86_sse2_packssdw_128,
2593                                   0, 0>, VEX_4V;
2594 defm VPACKUSWB : PDI_binop_rm_int<0x67, "vpackuswb", int_x86_sse2_packuswb_128,
2595                                   0, 0>, VEX_4V;
2596 }
2597
2598 let Constraints = "$src1 = $dst" in {
2599 defm PACKSSWB : PDI_binop_rm_int<0x63, "packsswb", int_x86_sse2_packsswb_128>;
2600 defm PACKSSDW : PDI_binop_rm_int<0x6B, "packssdw", int_x86_sse2_packssdw_128>;
2601 defm PACKUSWB : PDI_binop_rm_int<0x67, "packuswb", int_x86_sse2_packuswb_128>;
2602 } // Constraints = "$src1 = $dst"
2603
2604 //===---------------------------------------------------------------------===//
2605 // SSE2 - Packed Integer Shuffle Instructions
2606 //===---------------------------------------------------------------------===//
2607
2608 let ExeDomain = SSEPackedInt in {
2609 multiclass sse2_pshuffle<string OpcodeStr, ValueType vt, PatFrag pshuf_frag,
2610                          PatFrag bc_frag> {
2611 def ri : Ii8<0x70, MRMSrcReg,
2612               (outs VR128:$dst), (ins VR128:$src1, i8imm:$src2),
2613               !strconcat(OpcodeStr,
2614                          "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
2615               [(set VR128:$dst, (vt (pshuf_frag:$src2 VR128:$src1,
2616                                                       (undef))))]>;
2617 def mi : Ii8<0x70, MRMSrcMem,
2618               (outs VR128:$dst), (ins i128mem:$src1, i8imm:$src2),
2619               !strconcat(OpcodeStr,
2620                          "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
2621               [(set VR128:$dst, (vt (pshuf_frag:$src2
2622                                       (bc_frag (memopv2i64 addr:$src1)),
2623                                       (undef))))]>;
2624 }
2625 } // ExeDomain = SSEPackedInt
2626
2627 let Predicates = [HasAVX] in {
2628   let AddedComplexity = 5 in
2629   defm VPSHUFD : sse2_pshuffle<"vpshufd", v4i32, pshufd, bc_v4i32>, OpSize,
2630                                VEX;
2631
2632   // SSE2 with ImmT == Imm8 and XS prefix.
2633   defm VPSHUFHW : sse2_pshuffle<"vpshufhw", v8i16, pshufhw, bc_v8i16>, XS,
2634                                VEX;
2635
2636   // SSE2 with ImmT == Imm8 and XD prefix.
2637   defm VPSHUFLW : sse2_pshuffle<"vpshuflw", v8i16, pshuflw, bc_v8i16>, XD,
2638                                VEX;
2639 }
2640
2641 let Predicates = [HasSSE2] in {
2642   let AddedComplexity = 5 in
2643   defm PSHUFD : sse2_pshuffle<"pshufd", v4i32, pshufd, bc_v4i32>, TB, OpSize;
2644
2645   // SSE2 with ImmT == Imm8 and XS prefix.
2646   defm PSHUFHW : sse2_pshuffle<"pshufhw", v8i16, pshufhw, bc_v8i16>, XS;
2647
2648   // SSE2 with ImmT == Imm8 and XD prefix.
2649   defm PSHUFLW : sse2_pshuffle<"pshuflw", v8i16, pshuflw, bc_v8i16>, XD;
2650 }
2651
2652 //===---------------------------------------------------------------------===//
2653 // SSE2 - Packed Integer Unpack Instructions
2654 //===---------------------------------------------------------------------===//
2655
2656 let ExeDomain = SSEPackedInt in {
2657 multiclass sse2_unpack<bits<8> opc, string OpcodeStr, ValueType vt,
2658                        SDNode OpNode, PatFrag bc_frag, bit Is2Addr = 1> {
2659   def rr : PDI<opc, MRMSrcReg,
2660       (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2661       !if(Is2Addr,
2662           !strconcat(OpcodeStr,"\t{$src2, $dst|$dst, $src2}"),
2663           !strconcat(OpcodeStr,"\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2664       [(set VR128:$dst, (vt (OpNode VR128:$src1, VR128:$src2)))]>;
2665   def rm : PDI<opc, MRMSrcMem,
2666       (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2667       !if(Is2Addr,
2668           !strconcat(OpcodeStr,"\t{$src2, $dst|$dst, $src2}"),
2669           !strconcat(OpcodeStr,"\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
2670       [(set VR128:$dst, (OpNode VR128:$src1,
2671                                   (bc_frag (memopv2i64
2672                                                addr:$src2))))]>;
2673 }
2674
2675 let Predicates = [HasAVX] in {
2676   defm VPUNPCKLBW  : sse2_unpack<0x60, "vpunpcklbw", v16i8, X86Punpcklbw,
2677                                  bc_v16i8, 0>, VEX_4V;
2678   defm VPUNPCKLWD  : sse2_unpack<0x61, "vpunpcklwd", v8i16, X86Punpcklwd,
2679                                  bc_v8i16, 0>, VEX_4V;
2680   defm VPUNPCKLDQ  : sse2_unpack<0x62, "vpunpckldq", v4i32, X86Punpckldq,
2681                                  bc_v4i32, 0>, VEX_4V;
2682
2683   /// FIXME: we could eliminate this and use sse2_unpack instead if tblgen
2684   /// knew to collapse (bitconvert VT to VT) into its operand.
2685   def VPUNPCKLQDQrr : PDI<0x6C, MRMSrcReg,
2686             (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2687             "vpunpcklqdq\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2688             [(set VR128:$dst, (v2i64 (X86Punpcklqdq VR128:$src1,
2689                                                     VR128:$src2)))]>, VEX_4V;
2690   def VPUNPCKLQDQrm : PDI<0x6C, MRMSrcMem,
2691             (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2692             "vpunpcklqdq\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2693             [(set VR128:$dst, (v2i64 (X86Punpcklqdq VR128:$src1,
2694                                         (memopv2i64 addr:$src2))))]>, VEX_4V;
2695
2696   defm VPUNPCKHBW  : sse2_unpack<0x68, "vpunpckhbw", v16i8, X86Punpckhbw,
2697                                  bc_v16i8, 0>, VEX_4V;
2698   defm VPUNPCKHWD  : sse2_unpack<0x69, "vpunpckhwd", v8i16, X86Punpckhwd,
2699                                  bc_v8i16, 0>, VEX_4V;
2700   defm VPUNPCKHDQ  : sse2_unpack<0x6A, "vpunpckhdq", v4i32, X86Punpckhdq,
2701                                  bc_v4i32, 0>, VEX_4V;
2702
2703   /// FIXME: we could eliminate this and use sse2_unpack instead if tblgen
2704   /// knew to collapse (bitconvert VT to VT) into its operand.
2705   def VPUNPCKHQDQrr : PDI<0x6D, MRMSrcReg,
2706              (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2707              "vpunpckhqdq\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2708              [(set VR128:$dst, (v2i64 (X86Punpckhqdq VR128:$src1,
2709                                                      VR128:$src2)))]>, VEX_4V;
2710   def VPUNPCKHQDQrm : PDI<0x6D, MRMSrcMem,
2711              (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2712              "vpunpckhqdq\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2713              [(set VR128:$dst, (v2i64 (X86Punpckhqdq VR128:$src1,
2714                                         (memopv2i64 addr:$src2))))]>, VEX_4V;
2715 }
2716
2717 let Constraints = "$src1 = $dst" in {
2718   defm PUNPCKLBW  : sse2_unpack<0x60, "punpcklbw", v16i8, X86Punpcklbw, bc_v16i8>;
2719   defm PUNPCKLWD  : sse2_unpack<0x61, "punpcklwd", v8i16, X86Punpcklwd, bc_v8i16>;
2720   defm PUNPCKLDQ  : sse2_unpack<0x62, "punpckldq", v4i32, X86Punpckldq, bc_v4i32>;
2721
2722   /// FIXME: we could eliminate this and use sse2_unpack instead if tblgen
2723   /// knew to collapse (bitconvert VT to VT) into its operand.
2724   def PUNPCKLQDQrr : PDI<0x6C, MRMSrcReg,
2725                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2726                          "punpcklqdq\t{$src2, $dst|$dst, $src2}",
2727                         [(set VR128:$dst,
2728                           (v2i64 (X86Punpcklqdq VR128:$src1, VR128:$src2)))]>;
2729   def PUNPCKLQDQrm : PDI<0x6C, MRMSrcMem,
2730                          (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2731                          "punpcklqdq\t{$src2, $dst|$dst, $src2}",
2732                         [(set VR128:$dst,
2733                           (v2i64 (X86Punpcklqdq VR128:$src1,
2734                                          (memopv2i64 addr:$src2))))]>;
2735
2736   defm PUNPCKHBW  : sse2_unpack<0x68, "punpckhbw", v16i8, X86Punpckhbw, bc_v16i8>;
2737   defm PUNPCKHWD  : sse2_unpack<0x69, "punpckhwd", v8i16, X86Punpckhwd, bc_v8i16>;
2738   defm PUNPCKHDQ  : sse2_unpack<0x6A, "punpckhdq", v4i32, X86Punpckhdq, bc_v4i32>;
2739
2740   /// FIXME: we could eliminate this and use sse2_unpack instead if tblgen
2741   /// knew to collapse (bitconvert VT to VT) into its operand.
2742   def PUNPCKHQDQrr : PDI<0x6D, MRMSrcReg,
2743                          (outs VR128:$dst), (ins VR128:$src1, VR128:$src2),
2744                          "punpckhqdq\t{$src2, $dst|$dst, $src2}",
2745                         [(set VR128:$dst,
2746                           (v2i64 (X86Punpckhqdq VR128:$src1, VR128:$src2)))]>;
2747   def PUNPCKHQDQrm : PDI<0x6D, MRMSrcMem,
2748                         (outs VR128:$dst), (ins VR128:$src1, i128mem:$src2),
2749                         "punpckhqdq\t{$src2, $dst|$dst, $src2}",
2750                         [(set VR128:$dst,
2751                           (v2i64 (X86Punpckhqdq VR128:$src1,
2752                                          (memopv2i64 addr:$src2))))]>;
2753 }
2754
2755 } // ExeDomain = SSEPackedInt
2756
2757 //===---------------------------------------------------------------------===//
2758 // SSE2 - Packed Integer Extract and Insert
2759 //===---------------------------------------------------------------------===//
2760
2761 let ExeDomain = SSEPackedInt in {
2762 multiclass sse2_pinsrw<bit Is2Addr = 1> {
2763   def rri : Ii8<0xC4, MRMSrcReg,
2764        (outs VR128:$dst), (ins VR128:$src1,
2765         GR32:$src2, i32i8imm:$src3),
2766        !if(Is2Addr,
2767            "pinsrw\t{$src3, $src2, $dst|$dst, $src2, $src3}",
2768            "vpinsrw\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}"),
2769        [(set VR128:$dst,
2770          (X86pinsrw VR128:$src1, GR32:$src2, imm:$src3))]>;
2771   def rmi : Ii8<0xC4, MRMSrcMem,
2772                        (outs VR128:$dst), (ins VR128:$src1,
2773                         i16mem:$src2, i32i8imm:$src3),
2774        !if(Is2Addr,
2775            "pinsrw\t{$src3, $src2, $dst|$dst, $src2, $src3}",
2776            "vpinsrw\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}"),
2777        [(set VR128:$dst,
2778          (X86pinsrw VR128:$src1, (extloadi16 addr:$src2),
2779                     imm:$src3))]>;
2780 }
2781
2782 // Extract
2783 let Predicates = [HasAVX] in
2784 def VPEXTRWri : Ii8<0xC5, MRMSrcReg,
2785                     (outs GR32:$dst), (ins VR128:$src1, i32i8imm:$src2),
2786                     "vpextrw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2787                     [(set GR32:$dst, (X86pextrw (v8i16 VR128:$src1),
2788                                                 imm:$src2))]>, OpSize, VEX;
2789 def PEXTRWri : PDIi8<0xC5, MRMSrcReg,
2790                     (outs GR32:$dst), (ins VR128:$src1, i32i8imm:$src2),
2791                     "pextrw\t{$src2, $src1, $dst|$dst, $src1, $src2}",
2792                     [(set GR32:$dst, (X86pextrw (v8i16 VR128:$src1),
2793                                                 imm:$src2))]>;
2794
2795 // Insert
2796 let Predicates = [HasAVX] in {
2797   defm VPINSRW : sse2_pinsrw<0>, OpSize, VEX_4V;
2798   def  VPINSRWrr64i : Ii8<0xC4, MRMSrcReg, (outs VR128:$dst),
2799        (ins VR128:$src1, GR64:$src2, i32i8imm:$src3),
2800        "vpinsrw\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",
2801        []>, OpSize, VEX_4V;
2802 }
2803
2804 let Constraints = "$src1 = $dst" in
2805   defm PINSRW : sse2_pinsrw, TB, OpSize, Requires<[HasSSE2]>;
2806
2807 } // ExeDomain = SSEPackedInt
2808
2809 //===---------------------------------------------------------------------===//
2810 // SSE2 - Packed Mask Creation
2811 //===---------------------------------------------------------------------===//
2812
2813 let ExeDomain = SSEPackedInt in {
2814
2815 def VPMOVMSKBrr  : VPDI<0xD7, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
2816            "pmovmskb\t{$src, $dst|$dst, $src}",
2817            [(set GR32:$dst, (int_x86_sse2_pmovmskb_128 VR128:$src))]>, VEX;
2818 def VPMOVMSKBr64r : VPDI<0xD7, MRMSrcReg, (outs GR64:$dst), (ins VR128:$src),
2819            "pmovmskb\t{$src, $dst|$dst, $src}", []>, VEX;
2820 def PMOVMSKBrr : PDI<0xD7, MRMSrcReg, (outs GR32:$dst), (ins VR128:$src),
2821            "pmovmskb\t{$src, $dst|$dst, $src}",
2822            [(set GR32:$dst, (int_x86_sse2_pmovmskb_128 VR128:$src))]>;
2823
2824 } // ExeDomain = SSEPackedInt
2825
2826 //===---------------------------------------------------------------------===//
2827 // SSE2 - Conditional Store
2828 //===---------------------------------------------------------------------===//
2829
2830 let ExeDomain = SSEPackedInt in {
2831
2832 let Uses = [EDI] in
2833 def VMASKMOVDQU : VPDI<0xF7, MRMSrcReg, (outs),
2834            (ins VR128:$src, VR128:$mask),
2835            "maskmovdqu\t{$mask, $src|$src, $mask}",
2836            [(int_x86_sse2_maskmov_dqu VR128:$src, VR128:$mask, EDI)]>, VEX;
2837 let Uses = [RDI] in
2838 def VMASKMOVDQU64 : VPDI<0xF7, MRMSrcReg, (outs),
2839            (ins VR128:$src, VR128:$mask),
2840            "maskmovdqu\t{$mask, $src|$src, $mask}",
2841            [(int_x86_sse2_maskmov_dqu VR128:$src, VR128:$mask, RDI)]>, VEX;
2842
2843 let Uses = [EDI] in
2844 def MASKMOVDQU : PDI<0xF7, MRMSrcReg, (outs), (ins VR128:$src, VR128:$mask),
2845            "maskmovdqu\t{$mask, $src|$src, $mask}",
2846            [(int_x86_sse2_maskmov_dqu VR128:$src, VR128:$mask, EDI)]>;
2847 let Uses = [RDI] in
2848 def MASKMOVDQU64 : PDI<0xF7, MRMSrcReg, (outs), (ins VR128:$src, VR128:$mask),
2849            "maskmovdqu\t{$mask, $src|$src, $mask}",
2850            [(int_x86_sse2_maskmov_dqu VR128:$src, VR128:$mask, RDI)]>;
2851
2852 } // ExeDomain = SSEPackedInt
2853
2854 //===---------------------------------------------------------------------===//
2855 // SSE2 - Move Doubleword
2856 //===---------------------------------------------------------------------===//
2857
2858 // Move Int Doubleword to Packed Double Int
2859 def VMOVDI2PDIrr : VPDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR32:$src),
2860                       "movd\t{$src, $dst|$dst, $src}",
2861                       [(set VR128:$dst,
2862                         (v4i32 (scalar_to_vector GR32:$src)))]>, VEX;
2863 def VMOVDI2PDIrm : VPDI<0x6E, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
2864                       "movd\t{$src, $dst|$dst, $src}",
2865                       [(set VR128:$dst,
2866                         (v4i32 (scalar_to_vector (loadi32 addr:$src))))]>,
2867                       VEX;
2868 def VMOV64toPQIrr : VRPDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR64:$src),
2869                         "mov{d|q}\t{$src, $dst|$dst, $src}",
2870                         [(set VR128:$dst,
2871                           (v2i64 (scalar_to_vector GR64:$src)))]>, VEX;
2872 def VMOV64toSDrr : VRPDI<0x6E, MRMSrcReg, (outs FR64:$dst), (ins GR64:$src),
2873                        "mov{d|q}\t{$src, $dst|$dst, $src}",
2874                        [(set FR64:$dst, (bitconvert GR64:$src))]>, VEX;
2875
2876 def MOVDI2PDIrr : PDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR32:$src),
2877                       "movd\t{$src, $dst|$dst, $src}",
2878                       [(set VR128:$dst,
2879                         (v4i32 (scalar_to_vector GR32:$src)))]>;
2880 def MOVDI2PDIrm : PDI<0x6E, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
2881                       "movd\t{$src, $dst|$dst, $src}",
2882                       [(set VR128:$dst,
2883                         (v4i32 (scalar_to_vector (loadi32 addr:$src))))]>;
2884 def MOV64toPQIrr : RPDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR64:$src),
2885                         "mov{d|q}\t{$src, $dst|$dst, $src}",
2886                         [(set VR128:$dst,
2887                           (v2i64 (scalar_to_vector GR64:$src)))]>;
2888 def MOV64toSDrr : RPDI<0x6E, MRMSrcReg, (outs FR64:$dst), (ins GR64:$src),
2889                        "mov{d|q}\t{$src, $dst|$dst, $src}",
2890                        [(set FR64:$dst, (bitconvert GR64:$src))]>;
2891
2892
2893 // Move Int Doubleword to Single Scalar
2894 def VMOVDI2SSrr  : VPDI<0x6E, MRMSrcReg, (outs FR32:$dst), (ins GR32:$src),
2895                       "movd\t{$src, $dst|$dst, $src}",
2896                       [(set FR32:$dst, (bitconvert GR32:$src))]>, VEX;
2897
2898 def VMOVDI2SSrm  : VPDI<0x6E, MRMSrcMem, (outs FR32:$dst), (ins i32mem:$src),
2899                       "movd\t{$src, $dst|$dst, $src}",
2900                       [(set FR32:$dst, (bitconvert (loadi32 addr:$src)))]>,
2901                       VEX;
2902 def MOVDI2SSrr  : PDI<0x6E, MRMSrcReg, (outs FR32:$dst), (ins GR32:$src),
2903                       "movd\t{$src, $dst|$dst, $src}",
2904                       [(set FR32:$dst, (bitconvert GR32:$src))]>;
2905
2906 def MOVDI2SSrm  : PDI<0x6E, MRMSrcMem, (outs FR32:$dst), (ins i32mem:$src),
2907                       "movd\t{$src, $dst|$dst, $src}",
2908                       [(set FR32:$dst, (bitconvert (loadi32 addr:$src)))]>;
2909
2910 // Move Packed Doubleword Int to Packed Double Int
2911 def VMOVPDI2DIrr  : VPDI<0x7E, MRMDestReg, (outs GR32:$dst), (ins VR128:$src),
2912                        "movd\t{$src, $dst|$dst, $src}",
2913                        [(set GR32:$dst, (vector_extract (v4i32 VR128:$src),
2914                                         (iPTR 0)))]>, VEX;
2915 def VMOVPDI2DImr  : VPDI<0x7E, MRMDestMem, (outs),
2916                        (ins i32mem:$dst, VR128:$src),
2917                        "movd\t{$src, $dst|$dst, $src}",
2918                        [(store (i32 (vector_extract (v4i32 VR128:$src),
2919                                      (iPTR 0))), addr:$dst)]>, VEX;
2920 def MOVPDI2DIrr  : PDI<0x7E, MRMDestReg, (outs GR32:$dst), (ins VR128:$src),
2921                        "movd\t{$src, $dst|$dst, $src}",
2922                        [(set GR32:$dst, (vector_extract (v4i32 VR128:$src),
2923                                         (iPTR 0)))]>;
2924 def MOVPDI2DImr  : PDI<0x7E, MRMDestMem, (outs), (ins i32mem:$dst, VR128:$src),
2925                        "movd\t{$src, $dst|$dst, $src}",
2926                        [(store (i32 (vector_extract (v4i32 VR128:$src),
2927                                      (iPTR 0))), addr:$dst)]>;
2928
2929 def MOVPQIto64rr  : RPDI<0x7E, MRMDestReg, (outs GR64:$dst), (ins VR128:$src),
2930                          "mov{d|q}\t{$src, $dst|$dst, $src}",
2931                          [(set GR64:$dst, (vector_extract (v2i64 VR128:$src),
2932                                            (iPTR 0)))]>;
2933 def MOV64toSDrm : S3SI<0x7E, MRMSrcMem, (outs FR64:$dst), (ins i64mem:$src),
2934                        "movq\t{$src, $dst|$dst, $src}",
2935                        [(set FR64:$dst, (bitconvert (loadi64 addr:$src)))]>;
2936
2937 def MOVSDto64rr  : RPDI<0x7E, MRMDestReg, (outs GR64:$dst), (ins FR64:$src),
2938                         "mov{d|q}\t{$src, $dst|$dst, $src}",
2939                         [(set GR64:$dst, (bitconvert FR64:$src))]>;
2940 def MOVSDto64mr  : RPDI<0x7E, MRMDestMem, (outs), (ins i64mem:$dst, FR64:$src),
2941                         "movq\t{$src, $dst|$dst, $src}",
2942                         [(store (i64 (bitconvert FR64:$src)), addr:$dst)]>;
2943
2944 // Move Scalar Single to Double Int
2945 def VMOVSS2DIrr  : VPDI<0x7E, MRMDestReg, (outs GR32:$dst), (ins FR32:$src),
2946                       "movd\t{$src, $dst|$dst, $src}",
2947                       [(set GR32:$dst, (bitconvert FR32:$src))]>, VEX;
2948 def VMOVSS2DImr  : VPDI<0x7E, MRMDestMem, (outs), (ins i32mem:$dst, FR32:$src),
2949                       "movd\t{$src, $dst|$dst, $src}",
2950                       [(store (i32 (bitconvert FR32:$src)), addr:$dst)]>, VEX;
2951 def MOVSS2DIrr  : PDI<0x7E, MRMDestReg, (outs GR32:$dst), (ins FR32:$src),
2952                       "movd\t{$src, $dst|$dst, $src}",
2953                       [(set GR32:$dst, (bitconvert FR32:$src))]>;
2954 def MOVSS2DImr  : PDI<0x7E, MRMDestMem, (outs), (ins i32mem:$dst, FR32:$src),
2955                       "movd\t{$src, $dst|$dst, $src}",
2956                       [(store (i32 (bitconvert FR32:$src)), addr:$dst)]>;
2957
2958 // movd / movq to XMM register zero-extends
2959 let AddedComplexity = 15 in {
2960 def VMOVZDI2PDIrr : VPDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR32:$src),
2961                        "movd\t{$src, $dst|$dst, $src}",
2962                        [(set VR128:$dst, (v4i32 (X86vzmovl
2963                                       (v4i32 (scalar_to_vector GR32:$src)))))]>,
2964                                       VEX;
2965 def VMOVZQI2PQIrr : VPDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR64:$src),
2966                        "mov{d|q}\t{$src, $dst|$dst, $src}", // X86-64 only
2967                        [(set VR128:$dst, (v2i64 (X86vzmovl
2968                                       (v2i64 (scalar_to_vector GR64:$src)))))]>,
2969                                       VEX, VEX_W;
2970 }
2971 let AddedComplexity = 15 in {
2972 def MOVZDI2PDIrr : PDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR32:$src),
2973                        "movd\t{$src, $dst|$dst, $src}",
2974                        [(set VR128:$dst, (v4i32 (X86vzmovl
2975                                       (v4i32 (scalar_to_vector GR32:$src)))))]>;
2976 def MOVZQI2PQIrr : RPDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR64:$src),
2977                        "mov{d|q}\t{$src, $dst|$dst, $src}", // X86-64 only
2978                        [(set VR128:$dst, (v2i64 (X86vzmovl
2979                                       (v2i64 (scalar_to_vector GR64:$src)))))]>;
2980 }
2981
2982 let AddedComplexity = 20 in {
2983 def VMOVZDI2PDIrm : VPDI<0x6E, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
2984                        "movd\t{$src, $dst|$dst, $src}",
2985                        [(set VR128:$dst,
2986                          (v4i32 (X86vzmovl (v4i32 (scalar_to_vector
2987                                                    (loadi32 addr:$src))))))]>,
2988                                                    VEX;
2989 def MOVZDI2PDIrm : PDI<0x6E, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
2990                        "movd\t{$src, $dst|$dst, $src}",
2991                        [(set VR128:$dst,
2992                          (v4i32 (X86vzmovl (v4i32 (scalar_to_vector
2993                                                    (loadi32 addr:$src))))))]>;
2994
2995 def : Pat<(v4i32 (X86vzmovl (loadv4i32 addr:$src))),
2996             (MOVZDI2PDIrm addr:$src)>;
2997 def : Pat<(v4i32 (X86vzmovl (bc_v4i32 (loadv4f32 addr:$src)))),
2998             (MOVZDI2PDIrm addr:$src)>;
2999 def : Pat<(v4i32 (X86vzmovl (bc_v4i32 (loadv2i64 addr:$src)))),
3000             (MOVZDI2PDIrm addr:$src)>;
3001 }
3002
3003 // These are the correct encodings of the instructions so that we know how to
3004 // read correct assembly, even though we continue to emit the wrong ones for
3005 // compatibility with Darwin's buggy assembler.
3006 def : InstAlias<"movq\t{$src, $dst|$dst, $src}",
3007                 (MOV64toPQIrr VR128:$dst, GR64:$src), 0>;
3008 def : InstAlias<"movq\t{$src, $dst|$dst, $src}",
3009                 (MOV64toSDrr FR64:$dst, GR64:$src), 0>;
3010 def : InstAlias<"movq\t{$src, $dst|$dst, $src}",
3011                 (MOVPQIto64rr GR64:$dst, VR128:$src), 0>;
3012 def : InstAlias<"movq\t{$src, $dst|$dst, $src}",
3013                 (MOVSDto64rr GR64:$dst, FR64:$src), 0>;
3014 def : InstAlias<"movq\t{$src, $dst|$dst, $src}",
3015                 (VMOVZQI2PQIrr VR128:$dst, GR64:$src), 0>;
3016 def : InstAlias<"movq\t{$src, $dst|$dst, $src}",
3017                 (MOVZQI2PQIrr VR128:$dst, GR64:$src), 0>;
3018
3019 //===---------------------------------------------------------------------===//
3020 // SSE2 - Move Quadword
3021 //===---------------------------------------------------------------------===//
3022
3023 // Move Quadword Int to Packed Quadword Int
3024 def VMOVQI2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
3025                     "vmovq\t{$src, $dst|$dst, $src}",
3026                     [(set VR128:$dst,
3027                       (v2i64 (scalar_to_vector (loadi64 addr:$src))))]>, XS,
3028                     VEX, Requires<[HasAVX]>;
3029 def MOVQI2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
3030                     "movq\t{$src, $dst|$dst, $src}",
3031                     [(set VR128:$dst,
3032                       (v2i64 (scalar_to_vector (loadi64 addr:$src))))]>, XS,
3033                     Requires<[HasSSE2]>; // SSE2 instruction with XS Prefix
3034
3035 // Move Packed Quadword Int to Quadword Int
3036 def VMOVPQI2QImr : VPDI<0xD6, MRMDestMem, (outs), (ins i64mem:$dst, VR128:$src),
3037                       "movq\t{$src, $dst|$dst, $src}",
3038                       [(store (i64 (vector_extract (v2i64 VR128:$src),
3039                                     (iPTR 0))), addr:$dst)]>, VEX;
3040 def MOVPQI2QImr : PDI<0xD6, MRMDestMem, (outs), (ins i64mem:$dst, VR128:$src),
3041                       "movq\t{$src, $dst|$dst, $src}",
3042                       [(store (i64 (vector_extract (v2i64 VR128:$src),
3043                                     (iPTR 0))), addr:$dst)]>;
3044
3045 def : Pat<(f64 (vector_extract (v2f64 VR128:$src), (iPTR 0))),
3046           (f64 (EXTRACT_SUBREG (v2f64 VR128:$src), sub_sd))>;
3047
3048 // Store / copy lower 64-bits of a XMM register.
3049 def VMOVLQ128mr : VPDI<0xD6, MRMDestMem, (outs), (ins i64mem:$dst, VR128:$src),
3050                      "movq\t{$src, $dst|$dst, $src}",
3051                      [(int_x86_sse2_storel_dq addr:$dst, VR128:$src)]>, VEX;
3052 def MOVLQ128mr : PDI<0xD6, MRMDestMem, (outs), (ins i64mem:$dst, VR128:$src),
3053                      "movq\t{$src, $dst|$dst, $src}",
3054                      [(int_x86_sse2_storel_dq addr:$dst, VR128:$src)]>;
3055
3056 let AddedComplexity = 20 in
3057 def VMOVZQI2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
3058                      "vmovq\t{$src, $dst|$dst, $src}",
3059                      [(set VR128:$dst,
3060                        (v2i64 (X86vzmovl (v2i64 (scalar_to_vector
3061                                                  (loadi64 addr:$src))))))]>,
3062                      XS, VEX, Requires<[HasAVX]>;
3063
3064 let AddedComplexity = 20 in {
3065 def MOVZQI2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
3066                      "movq\t{$src, $dst|$dst, $src}",
3067                      [(set VR128:$dst,
3068                        (v2i64 (X86vzmovl (v2i64 (scalar_to_vector
3069                                                  (loadi64 addr:$src))))))]>,
3070                      XS, Requires<[HasSSE2]>;
3071
3072 def : Pat<(v2i64 (X86vzmovl (loadv2i64 addr:$src))),
3073             (MOVZQI2PQIrm addr:$src)>;
3074 def : Pat<(v2i64 (X86vzmovl (bc_v2i64 (loadv4f32 addr:$src)))),
3075             (MOVZQI2PQIrm addr:$src)>;
3076 def : Pat<(v2i64 (X86vzload addr:$src)), (MOVZQI2PQIrm addr:$src)>;
3077 }
3078
3079 // Moving from XMM to XMM and clear upper 64 bits. Note, there is a bug in
3080 // IA32 document. movq xmm1, xmm2 does clear the high bits.
3081 let AddedComplexity = 15 in
3082 def VMOVZPQILo2PQIrr : I<0x7E, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3083                         "vmovq\t{$src, $dst|$dst, $src}",
3084                     [(set VR128:$dst, (v2i64 (X86vzmovl (v2i64 VR128:$src))))]>,
3085                       XS, VEX, Requires<[HasAVX]>;
3086 let AddedComplexity = 15 in
3087 def MOVZPQILo2PQIrr : I<0x7E, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3088                         "movq\t{$src, $dst|$dst, $src}",
3089                     [(set VR128:$dst, (v2i64 (X86vzmovl (v2i64 VR128:$src))))]>,
3090                       XS, Requires<[HasSSE2]>;
3091
3092 let AddedComplexity = 20 in
3093 def VMOVZPQILo2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
3094                         "vmovq\t{$src, $dst|$dst, $src}",
3095                     [(set VR128:$dst, (v2i64 (X86vzmovl
3096                                              (loadv2i64 addr:$src))))]>,
3097                       XS, VEX, Requires<[HasAVX]>;
3098 let AddedComplexity = 20 in {
3099 def MOVZPQILo2PQIrm : I<0x7E, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
3100                         "movq\t{$src, $dst|$dst, $src}",
3101                     [(set VR128:$dst, (v2i64 (X86vzmovl
3102                                              (loadv2i64 addr:$src))))]>,
3103                       XS, Requires<[HasSSE2]>;
3104
3105 def : Pat<(v2i64 (X86vzmovl (bc_v2i64 (loadv4i32 addr:$src)))),
3106             (MOVZPQILo2PQIrm addr:$src)>;
3107 }
3108
3109 // Instructions to match in the assembler
3110 def VMOVQs64rr : VPDI<0x6E, MRMSrcReg, (outs VR128:$dst), (ins GR64:$src),
3111                       "movq\t{$src, $dst|$dst, $src}", []>, VEX, VEX_W;
3112 def VMOVQd64rr : VPDI<0x7E, MRMDestReg, (outs GR64:$dst), (ins VR128:$src),
3113                       "movq\t{$src, $dst|$dst, $src}", []>, VEX, VEX_W;
3114 // Recognize "movd" with GR64 destination, but encode as a "movq"
3115 def VMOVQd64rr_alt : VPDI<0x7E, MRMDestReg, (outs GR64:$dst), (ins VR128:$src),
3116                           "movd\t{$src, $dst|$dst, $src}", []>, VEX, VEX_W;
3117
3118 // Instructions for the disassembler
3119 // xr = XMM register
3120 // xm = mem64
3121
3122 let Predicates = [HasAVX] in
3123 def VMOVQxrxr: I<0x7E, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3124                  "vmovq\t{$src, $dst|$dst, $src}", []>, VEX, XS;
3125 def MOVQxrxr : I<0x7E, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3126                  "movq\t{$src, $dst|$dst, $src}", []>, XS;
3127
3128 //===---------------------------------------------------------------------===//
3129 // SSE2 - Misc Instructions
3130 //===---------------------------------------------------------------------===//
3131
3132 // Flush cache
3133 def CLFLUSH : I<0xAE, MRM7m, (outs), (ins i8mem:$src),
3134                "clflush\t$src", [(int_x86_sse2_clflush addr:$src)]>,
3135               TB, Requires<[HasSSE2]>;
3136
3137 // Load, store, and memory fence
3138 def LFENCE : I<0xAE, MRM_E8, (outs), (ins),
3139                "lfence", [(int_x86_sse2_lfence)]>, TB, Requires<[HasSSE2]>;
3140 def MFENCE : I<0xAE, MRM_F0, (outs), (ins),
3141                "mfence", [(int_x86_sse2_mfence)]>, TB, Requires<[HasSSE2]>;
3142 def : Pat<(X86LFence), (LFENCE)>;
3143 def : Pat<(X86MFence), (MFENCE)>;
3144
3145
3146 // Pause. This "instruction" is encoded as "rep; nop", so even though it
3147 // was introduced with SSE2, it's backward compatible.
3148 def PAUSE : I<0x90, RawFrm, (outs), (ins), "pause", []>, REP;
3149
3150 // Alias instructions that map zero vector to pxor / xorp* for sse.
3151 // We set canFoldAsLoad because this can be converted to a constant-pool
3152 // load of an all-ones value if folding it would be beneficial.
3153 // FIXME: Change encoding to pseudo! This is blocked right now by the x86
3154 // JIT implementation, it does not expand the instructions below like
3155 // X86MCInstLower does.
3156 let isReMaterializable = 1, isAsCheapAsAMove = 1, canFoldAsLoad = 1,
3157     isCodeGenOnly = 1, ExeDomain = SSEPackedInt in
3158   def V_SETALLONES : PDI<0x76, MRMInitReg, (outs VR128:$dst), (ins), "",
3159                          [(set VR128:$dst, (v4i32 immAllOnesV))]>;
3160 let isReMaterializable = 1, isAsCheapAsAMove = 1, canFoldAsLoad = 1,
3161     isCodeGenOnly = 1, ExeDomain = SSEPackedInt, Predicates = [HasAVX] in
3162   def AVX_SETALLONES : PDI<0x76, MRMInitReg, (outs VR128:$dst), (ins), "",
3163                          [(set VR128:$dst, (v4i32 immAllOnesV))]>, VEX_4V;
3164
3165 //===---------------------------------------------------------------------===//
3166 // SSE3 - Conversion Instructions
3167 //===---------------------------------------------------------------------===//
3168
3169 // Convert Packed Double FP to Packed DW Integers
3170 let Predicates = [HasAVX] in {
3171 // The assembler can recognize rr 256-bit instructions by seeing a ymm
3172 // register, but the same isn't true when using memory operands instead.
3173 // Provide other assembly rr and rm forms to address this explicitly.
3174 def VCVTPD2DQrr  : S3DI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3175                        "vcvtpd2dq\t{$src, $dst|$dst, $src}", []>, VEX;
3176 def VCVTPD2DQXrYr  : S3DI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR256:$src),
3177                        "vcvtpd2dq\t{$src, $dst|$dst, $src}", []>, VEX;
3178
3179 // XMM only
3180 def VCVTPD2DQXrr : S3DI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3181                       "vcvtpd2dqx\t{$src, $dst|$dst, $src}", []>, VEX;
3182 def VCVTPD2DQXrm : S3DI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
3183                       "vcvtpd2dqx\t{$src, $dst|$dst, $src}", []>, VEX;
3184
3185 // YMM only
3186 def VCVTPD2DQYrr : S3DI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR256:$src),
3187                       "vcvtpd2dqy\t{$src, $dst|$dst, $src}", []>, VEX;
3188 def VCVTPD2DQYrm : S3DI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f256mem:$src),
3189                       "vcvtpd2dqy\t{$src, $dst|$dst, $src}", []>, VEX, VEX_L;
3190 }
3191
3192 def CVTPD2DQrm  : S3DI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
3193                        "cvtpd2dq\t{$src, $dst|$dst, $src}", []>;
3194 def CVTPD2DQrr  : S3DI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3195                        "cvtpd2dq\t{$src, $dst|$dst, $src}", []>;
3196
3197 // Convert Packed DW Integers to Packed Double FP
3198 let Predicates = [HasAVX] in {
3199 def VCVTDQ2PDrm  : S3SI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
3200                      "vcvtdq2pd\t{$src, $dst|$dst, $src}", []>, VEX;
3201 def VCVTDQ2PDrr  : S3SI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3202                      "vcvtdq2pd\t{$src, $dst|$dst, $src}", []>, VEX;
3203 def VCVTDQ2PDYrm  : S3SI<0xE6, MRMSrcMem, (outs VR256:$dst), (ins f128mem:$src),
3204                      "vcvtdq2pd\t{$src, $dst|$dst, $src}", []>, VEX;
3205 def VCVTDQ2PDYrr  : S3SI<0xE6, MRMSrcReg, (outs VR256:$dst), (ins VR128:$src),
3206                      "vcvtdq2pd\t{$src, $dst|$dst, $src}", []>, VEX;
3207 }
3208
3209 def CVTDQ2PDrm  : S3SI<0xE6, MRMSrcMem, (outs VR128:$dst), (ins f128mem:$src),
3210                        "cvtdq2pd\t{$src, $dst|$dst, $src}", []>;
3211 def CVTDQ2PDrr  : S3SI<0xE6, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3212                        "cvtdq2pd\t{$src, $dst|$dst, $src}", []>;
3213
3214 // AVX 256-bit register conversion intrinsics
3215 def : Pat<(int_x86_avx_cvtdq2_pd_256 VR128:$src),
3216            (VCVTDQ2PDYrr VR128:$src)>;
3217 def : Pat<(int_x86_avx_cvtdq2_pd_256 (memopv4i32 addr:$src)),
3218            (VCVTDQ2PDYrm addr:$src)>;
3219
3220 def : Pat<(int_x86_avx_cvt_pd2dq_256 VR256:$src),
3221           (VCVTPD2DQYrr VR256:$src)>;
3222 def : Pat<(int_x86_avx_cvt_pd2dq_256 (memopv4f64 addr:$src)),
3223           (VCVTPD2DQYrm addr:$src)>;
3224
3225 //===---------------------------------------------------------------------===//
3226 // SSE3 - Move Instructions
3227 //===---------------------------------------------------------------------===//
3228
3229 //===---------------------------------------------------------------------===//
3230 // Replicate Single FP - MOVSHDUP and MOVSLDUP
3231 //
3232 multiclass sse3_replicate_sfp<bits<8> op, SDNode OpNode, string OpcodeStr,
3233                               ValueType vt, RegisterClass RC, PatFrag mem_frag,
3234                               X86MemOperand x86memop> {
3235 def rr : S3SI<op, MRMSrcReg, (outs RC:$dst), (ins RC:$src),
3236                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3237                       [(set RC:$dst, (vt (OpNode RC:$src)))]>;
3238 def rm : S3SI<op, MRMSrcMem, (outs RC:$dst), (ins x86memop:$src),
3239                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3240                       [(set RC:$dst, (OpNode (mem_frag addr:$src)))]>;
3241 }
3242
3243 let Predicates = [HasAVX] in {
3244   defm VMOVSHDUP  : sse3_replicate_sfp<0x16, X86Movshdup, "vmovshdup",
3245                                        v4f32, VR128, memopv4f32, f128mem>, VEX;
3246   defm VMOVSLDUP  : sse3_replicate_sfp<0x12, X86Movsldup, "vmovsldup",
3247                                        v4f32, VR128, memopv4f32, f128mem>, VEX;
3248   defm VMOVSHDUPY : sse3_replicate_sfp<0x16, X86Movshdup, "vmovshdup",
3249                                        v8f32, VR256, memopv8f32, f256mem>, VEX;
3250   defm VMOVSLDUPY : sse3_replicate_sfp<0x12, X86Movsldup, "vmovsldup",
3251                                        v8f32, VR256, memopv8f32, f256mem>, VEX;
3252 }
3253 defm MOVSHDUP : sse3_replicate_sfp<0x16, X86Movshdup, "movshdup", v4f32, VR128,
3254                                    memopv4f32, f128mem>;
3255 defm MOVSLDUP : sse3_replicate_sfp<0x12, X86Movsldup, "movsldup", v4f32, VR128,
3256                                    memopv4f32, f128mem>;
3257
3258 let Predicates = [HasSSE3] in {
3259   def : Pat<(v4i32 (X86Movshdup VR128:$src)),
3260             (MOVSHDUPrr VR128:$src)>;
3261   def : Pat<(v4i32 (X86Movshdup (bc_v4i32 (memopv2i64 addr:$src)))),
3262             (MOVSHDUPrm addr:$src)>;
3263   def : Pat<(v4i32 (X86Movsldup VR128:$src)),
3264             (MOVSLDUPrr VR128:$src)>;
3265   def : Pat<(v4i32 (X86Movsldup (bc_v4i32 (memopv2i64 addr:$src)))),
3266             (MOVSLDUPrm addr:$src)>;
3267 }
3268
3269 let Predicates = [HasAVX] in {
3270   def : Pat<(v4i32 (X86Movshdup VR128:$src)),
3271             (VMOVSHDUPrr VR128:$src)>;
3272   def : Pat<(v4i32 (X86Movshdup (bc_v4i32 (memopv2i64 addr:$src)))),
3273             (VMOVSHDUPrm addr:$src)>;
3274   def : Pat<(v4i32 (X86Movsldup VR128:$src)),
3275             (VMOVSLDUPrr VR128:$src)>;
3276   def : Pat<(v4i32 (X86Movsldup (bc_v4i32 (memopv2i64 addr:$src)))),
3277             (VMOVSLDUPrm addr:$src)>;
3278   def : Pat<(v8i32 (X86Movshdup VR256:$src)),
3279             (VMOVSHDUPYrr VR256:$src)>;
3280   def : Pat<(v8i32 (X86Movshdup (bc_v8i32 (memopv4i64 addr:$src)))),
3281             (VMOVSHDUPYrm addr:$src)>;
3282   def : Pat<(v8i32 (X86Movsldup VR256:$src)),
3283             (VMOVSLDUPYrr VR256:$src)>;
3284   def : Pat<(v8i32 (X86Movsldup (bc_v8i32 (memopv4i64 addr:$src)))),
3285             (VMOVSLDUPYrm addr:$src)>;
3286 }
3287
3288 //===---------------------------------------------------------------------===//
3289 // Replicate Double FP - MOVDDUP
3290 //
3291 multiclass sse3_replicate_dfp<string OpcodeStr> {
3292 def rr  : S3DI<0x12, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3293                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3294                     [(set VR128:$dst,(v2f64 (movddup VR128:$src, (undef))))]>;
3295 def rm  : S3DI<0x12, MRMSrcMem, (outs VR128:$dst), (ins f64mem:$src),
3296                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3297                     [(set VR128:$dst,
3298                       (v2f64 (movddup (scalar_to_vector (loadf64 addr:$src)),
3299                                       (undef))))]>;
3300 }
3301
3302 multiclass sse3_replicate_dfp_y<string OpcodeStr> {
3303 def rr  : S3DI<0x12, MRMSrcReg, (outs VR256:$dst), (ins VR256:$src),
3304                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3305                     []>;
3306 def rm  : S3DI<0x12, MRMSrcMem, (outs VR256:$dst), (ins f256mem:$src),
3307                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3308                     []>;
3309 }
3310
3311 let Predicates = [HasAVX] in {
3312   // FIXME: Merge above classes when we have patterns for the ymm version
3313   defm VMOVDDUP  : sse3_replicate_dfp<"vmovddup">, VEX;
3314   defm VMOVDDUPY : sse3_replicate_dfp_y<"vmovddup">, VEX;
3315 }
3316 defm MOVDDUP : sse3_replicate_dfp<"movddup">;
3317
3318 // Move Unaligned Integer
3319 let Predicates = [HasAVX] in {
3320   def VLDDQUrm : S3DI<0xF0, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
3321                    "vlddqu\t{$src, $dst|$dst, $src}",
3322                    [(set VR128:$dst, (int_x86_sse3_ldu_dq addr:$src))]>, VEX;
3323   def VLDDQUYrm : S3DI<0xF0, MRMSrcMem, (outs VR256:$dst), (ins i256mem:$src),
3324                    "vlddqu\t{$src, $dst|$dst, $src}",
3325                    [(set VR256:$dst, (int_x86_avx_ldu_dq_256 addr:$src))]>, VEX;
3326 }
3327 def LDDQUrm : S3DI<0xF0, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
3328                    "lddqu\t{$src, $dst|$dst, $src}",
3329                    [(set VR128:$dst, (int_x86_sse3_ldu_dq addr:$src))]>;
3330
3331 def : Pat<(movddup (bc_v2f64 (v2i64 (scalar_to_vector (loadi64 addr:$src)))),
3332                    (undef)),
3333           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
3334
3335 // Several Move patterns
3336 let AddedComplexity = 5 in {
3337 def : Pat<(movddup (memopv2f64 addr:$src), (undef)),
3338           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
3339 def : Pat<(movddup (bc_v4f32 (memopv2f64 addr:$src)), (undef)),
3340           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
3341 def : Pat<(movddup (memopv2i64 addr:$src), (undef)),
3342           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
3343 def : Pat<(movddup (bc_v4i32 (memopv2i64 addr:$src)), (undef)),
3344           (MOVDDUPrm addr:$src)>, Requires<[HasSSE3]>;
3345 }
3346
3347 //===---------------------------------------------------------------------===//
3348 // SSE3 - Arithmetic
3349 //===---------------------------------------------------------------------===//
3350
3351 multiclass sse3_addsub<Intrinsic Int, string OpcodeStr, RegisterClass RC,
3352                        X86MemOperand x86memop, bit Is2Addr = 1> {
3353   def rr : I<0xD0, MRMSrcReg,
3354        (outs RC:$dst), (ins RC:$src1, RC:$src2),
3355        !if(Is2Addr,
3356            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3357            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
3358        [(set RC:$dst, (Int RC:$src1, RC:$src2))]>;
3359   def rm : I<0xD0, MRMSrcMem,
3360        (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
3361        !if(Is2Addr,
3362            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3363            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
3364        [(set RC:$dst, (Int RC:$src1, (memop addr:$src2)))]>;
3365 }
3366
3367 let Predicates = [HasAVX],
3368   ExeDomain = SSEPackedDouble in {
3369   defm VADDSUBPS : sse3_addsub<int_x86_sse3_addsub_ps, "vaddsubps", VR128,
3370                                f128mem, 0>, TB, XD, VEX_4V;
3371   defm VADDSUBPD : sse3_addsub<int_x86_sse3_addsub_pd, "vaddsubpd", VR128,
3372                                f128mem, 0>, TB, OpSize, VEX_4V;
3373   defm VADDSUBPSY : sse3_addsub<int_x86_avx_addsub_ps_256, "vaddsubps", VR256,
3374                                f256mem, 0>, TB, XD, VEX_4V;
3375   defm VADDSUBPDY : sse3_addsub<int_x86_avx_addsub_pd_256, "vaddsubpd", VR256,
3376                                f256mem, 0>, TB, OpSize, VEX_4V;
3377 }
3378 let Constraints = "$src1 = $dst", Predicates = [HasSSE3],
3379     ExeDomain = SSEPackedDouble in {
3380   defm ADDSUBPS : sse3_addsub<int_x86_sse3_addsub_ps, "addsubps", VR128,
3381                               f128mem>, TB, XD;
3382   defm ADDSUBPD : sse3_addsub<int_x86_sse3_addsub_pd, "addsubpd", VR128,
3383                               f128mem>, TB, OpSize;
3384 }
3385
3386 //===---------------------------------------------------------------------===//
3387 // SSE3 Instructions
3388 //===---------------------------------------------------------------------===//
3389
3390 // Horizontal ops
3391 multiclass S3D_Int<bits<8> o, string OpcodeStr, ValueType vt, RegisterClass RC,
3392                    X86MemOperand x86memop, Intrinsic IntId, bit Is2Addr = 1> {
3393   def rr : S3DI<o, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
3394        !if(Is2Addr,
3395          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3396          !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
3397       [(set RC:$dst, (vt (IntId RC:$src1, RC:$src2)))]>;
3398
3399   def rm : S3DI<o, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
3400        !if(Is2Addr,
3401          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3402          !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
3403       [(set RC:$dst, (vt (IntId RC:$src1, (memop addr:$src2))))]>;
3404 }
3405 multiclass S3_Int<bits<8> o, string OpcodeStr, ValueType vt, RegisterClass RC,
3406                   X86MemOperand x86memop, Intrinsic IntId, bit Is2Addr = 1> {
3407   def rr : S3I<o, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
3408        !if(Is2Addr,
3409          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3410          !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
3411       [(set RC:$dst, (vt (IntId RC:$src1, RC:$src2)))]>;
3412
3413   def rm : S3I<o, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
3414        !if(Is2Addr,
3415          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3416          !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
3417       [(set RC:$dst, (vt (IntId RC:$src1, (memop addr:$src2))))]>;
3418 }
3419
3420 let Predicates = [HasAVX] in {
3421   defm VHADDPS  : S3D_Int<0x7C, "vhaddps", v4f32, VR128, f128mem,
3422                           int_x86_sse3_hadd_ps, 0>, VEX_4V;
3423   defm VHADDPD  : S3_Int <0x7C, "vhaddpd", v2f64, VR128, f128mem,
3424                           int_x86_sse3_hadd_pd, 0>, VEX_4V;
3425   defm VHSUBPS  : S3D_Int<0x7D, "vhsubps", v4f32, VR128, f128mem,
3426                           int_x86_sse3_hsub_ps, 0>, VEX_4V;
3427   defm VHSUBPD  : S3_Int <0x7D, "vhsubpd", v2f64, VR128, f128mem,
3428                           int_x86_sse3_hsub_pd, 0>, VEX_4V;
3429   defm VHADDPSY : S3D_Int<0x7C, "vhaddps", v8f32, VR256, f256mem,
3430                           int_x86_avx_hadd_ps_256, 0>, VEX_4V;
3431   defm VHADDPDY : S3_Int <0x7C, "vhaddpd", v4f64, VR256, f256mem,
3432                           int_x86_avx_hadd_pd_256, 0>, VEX_4V;
3433   defm VHSUBPSY : S3D_Int<0x7D, "vhsubps", v8f32, VR256, f256mem,
3434                           int_x86_avx_hsub_ps_256, 0>, VEX_4V;
3435   defm VHSUBPDY : S3_Int <0x7D, "vhsubpd", v4f64, VR256, f256mem,
3436                           int_x86_avx_hsub_pd_256, 0>, VEX_4V;
3437 }
3438
3439 let Constraints = "$src1 = $dst" in {
3440   defm HADDPS : S3D_Int<0x7C, "haddps", v4f32, VR128, f128mem,
3441                         int_x86_sse3_hadd_ps>;
3442   defm HADDPD : S3_Int<0x7C, "haddpd", v2f64, VR128, f128mem,
3443                        int_x86_sse3_hadd_pd>;
3444   defm HSUBPS : S3D_Int<0x7D, "hsubps", v4f32, VR128, f128mem,
3445                         int_x86_sse3_hsub_ps>;
3446   defm HSUBPD : S3_Int<0x7D, "hsubpd", v2f64, VR128, f128mem,
3447                        int_x86_sse3_hsub_pd>;
3448 }
3449
3450 //===---------------------------------------------------------------------===//
3451 // SSSE3 - Packed Absolute Instructions
3452 //===---------------------------------------------------------------------===//
3453
3454
3455 /// SS3I_unop_rm_int - Simple SSSE3 unary op whose type can be v*{i8,i16,i32}.
3456 multiclass SS3I_unop_rm_int<bits<8> opc, string OpcodeStr,
3457                             PatFrag mem_frag128, Intrinsic IntId128> {
3458   def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
3459                     (ins VR128:$src),
3460                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3461                     [(set VR128:$dst, (IntId128 VR128:$src))]>,
3462                     OpSize;
3463
3464   def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
3465                     (ins i128mem:$src),
3466                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3467                     [(set VR128:$dst,
3468                       (IntId128
3469                        (bitconvert (mem_frag128 addr:$src))))]>, OpSize;
3470 }
3471
3472 let Predicates = [HasAVX] in {
3473   defm VPABSB  : SS3I_unop_rm_int<0x1C, "vpabsb", memopv16i8,
3474                                   int_x86_ssse3_pabs_b_128>, VEX;
3475   defm VPABSW  : SS3I_unop_rm_int<0x1D, "vpabsw", memopv8i16,
3476                                   int_x86_ssse3_pabs_w_128>, VEX;
3477   defm VPABSD  : SS3I_unop_rm_int<0x1E, "vpabsd", memopv4i32,
3478                                   int_x86_ssse3_pabs_d_128>, VEX;
3479 }
3480
3481 defm PABSB : SS3I_unop_rm_int<0x1C, "pabsb", memopv16i8,
3482                               int_x86_ssse3_pabs_b_128>;
3483 defm PABSW : SS3I_unop_rm_int<0x1D, "pabsw", memopv8i16,
3484                               int_x86_ssse3_pabs_w_128>;
3485 defm PABSD : SS3I_unop_rm_int<0x1E, "pabsd", memopv4i32,
3486                               int_x86_ssse3_pabs_d_128>;
3487
3488 //===---------------------------------------------------------------------===//
3489 // SSSE3 - Packed Binary Operator Instructions
3490 //===---------------------------------------------------------------------===//
3491
3492 /// SS3I_binop_rm_int - Simple SSSE3 bin op whose type can be v*{i8,i16,i32}.
3493 multiclass SS3I_binop_rm_int<bits<8> opc, string OpcodeStr,
3494                              PatFrag mem_frag128, Intrinsic IntId128,
3495                              bit Is2Addr = 1> {
3496   let isCommutable = 1 in
3497   def rr128 : SS38I<opc, MRMSrcReg, (outs VR128:$dst),
3498        (ins VR128:$src1, VR128:$src2),
3499        !if(Is2Addr,
3500          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3501          !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
3502        [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
3503        OpSize;
3504   def rm128 : SS38I<opc, MRMSrcMem, (outs VR128:$dst),
3505        (ins VR128:$src1, i128mem:$src2),
3506        !if(Is2Addr,
3507          !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
3508          !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
3509        [(set VR128:$dst,
3510          (IntId128 VR128:$src1,
3511           (bitconvert (memopv16i8 addr:$src2))))]>, OpSize;
3512 }
3513
3514 let Predicates = [HasAVX] in {
3515 let isCommutable = 0 in {
3516   defm VPHADDW    : SS3I_binop_rm_int<0x01, "vphaddw", memopv8i16,
3517                                       int_x86_ssse3_phadd_w_128, 0>, VEX_4V;
3518   defm VPHADDD    : SS3I_binop_rm_int<0x02, "vphaddd", memopv4i32,
3519                                       int_x86_ssse3_phadd_d_128, 0>, VEX_4V;
3520   defm VPHADDSW   : SS3I_binop_rm_int<0x03, "vphaddsw", memopv8i16,
3521                                       int_x86_ssse3_phadd_sw_128, 0>, VEX_4V;
3522   defm VPHSUBW    : SS3I_binop_rm_int<0x05, "vphsubw", memopv8i16,
3523                                       int_x86_ssse3_phsub_w_128, 0>, VEX_4V;
3524   defm VPHSUBD    : SS3I_binop_rm_int<0x06, "vphsubd", memopv4i32,
3525                                       int_x86_ssse3_phsub_d_128, 0>, VEX_4V;
3526   defm VPHSUBSW   : SS3I_binop_rm_int<0x07, "vphsubsw", memopv8i16,
3527                                       int_x86_ssse3_phsub_sw_128, 0>, VEX_4V;
3528   defm VPMADDUBSW : SS3I_binop_rm_int<0x04, "vpmaddubsw", memopv16i8,
3529                                       int_x86_ssse3_pmadd_ub_sw_128, 0>, VEX_4V;
3530   defm VPSHUFB    : SS3I_binop_rm_int<0x00, "vpshufb", memopv16i8,
3531                                       int_x86_ssse3_pshuf_b_128, 0>, VEX_4V;
3532   defm VPSIGNB    : SS3I_binop_rm_int<0x08, "vpsignb", memopv16i8,
3533                                       int_x86_ssse3_psign_b_128, 0>, VEX_4V;
3534   defm VPSIGNW    : SS3I_binop_rm_int<0x09, "vpsignw", memopv8i16,
3535                                       int_x86_ssse3_psign_w_128, 0>, VEX_4V;
3536   defm VPSIGND    : SS3I_binop_rm_int<0x0A, "vpsignd", memopv4i32,
3537                                       int_x86_ssse3_psign_d_128, 0>, VEX_4V;
3538 }
3539 defm VPMULHRSW    : SS3I_binop_rm_int<0x0B, "vpmulhrsw", memopv8i16,
3540                                       int_x86_ssse3_pmul_hr_sw_128, 0>, VEX_4V;
3541 }
3542
3543 // None of these have i8 immediate fields.
3544 let ImmT = NoImm, Constraints = "$src1 = $dst" in {
3545 let isCommutable = 0 in {
3546   defm PHADDW    : SS3I_binop_rm_int<0x01, "phaddw", memopv8i16,
3547                                      int_x86_ssse3_phadd_w_128>;
3548   defm PHADDD    : SS3I_binop_rm_int<0x02, "phaddd", memopv4i32,
3549                                      int_x86_ssse3_phadd_d_128>;
3550   defm PHADDSW   : SS3I_binop_rm_int<0x03, "phaddsw", memopv8i16,
3551                                      int_x86_ssse3_phadd_sw_128>;
3552   defm PHSUBW    : SS3I_binop_rm_int<0x05, "phsubw", memopv8i16,
3553                                      int_x86_ssse3_phsub_w_128>;
3554   defm PHSUBD    : SS3I_binop_rm_int<0x06, "phsubd", memopv4i32,
3555                                      int_x86_ssse3_phsub_d_128>;
3556   defm PHSUBSW   : SS3I_binop_rm_int<0x07, "phsubsw", memopv8i16,
3557                                      int_x86_ssse3_phsub_sw_128>;
3558   defm PMADDUBSW : SS3I_binop_rm_int<0x04, "pmaddubsw", memopv16i8,
3559                                      int_x86_ssse3_pmadd_ub_sw_128>;
3560   defm PSHUFB    : SS3I_binop_rm_int<0x00, "pshufb", memopv16i8,
3561                                      int_x86_ssse3_pshuf_b_128>;
3562   defm PSIGNB    : SS3I_binop_rm_int<0x08, "psignb", memopv16i8,
3563                                      int_x86_ssse3_psign_b_128>;
3564   defm PSIGNW    : SS3I_binop_rm_int<0x09, "psignw", memopv8i16,
3565                                      int_x86_ssse3_psign_w_128>;
3566   defm PSIGND    : SS3I_binop_rm_int<0x0A, "psignd", memopv4i32,
3567                                        int_x86_ssse3_psign_d_128>;
3568 }
3569 defm PMULHRSW    : SS3I_binop_rm_int<0x0B, "pmulhrsw", memopv8i16,
3570                                      int_x86_ssse3_pmul_hr_sw_128>;
3571 }
3572
3573 def : Pat<(X86pshufb VR128:$src, VR128:$mask),
3574           (PSHUFBrr128 VR128:$src, VR128:$mask)>, Requires<[HasSSSE3]>;
3575 def : Pat<(X86pshufb VR128:$src, (bc_v16i8 (memopv2i64 addr:$mask))),
3576           (PSHUFBrm128 VR128:$src, addr:$mask)>, Requires<[HasSSSE3]>;
3577
3578 def : Pat<(X86psignb VR128:$src1, VR128:$src2),
3579           (PSIGNBrr128 VR128:$src1, VR128:$src2)>, Requires<[HasSSSE3]>;
3580 def : Pat<(X86psignw VR128:$src1, VR128:$src2),
3581           (PSIGNWrr128 VR128:$src1, VR128:$src2)>, Requires<[HasSSSE3]>;
3582 def : Pat<(X86psignd VR128:$src1, VR128:$src2),
3583           (PSIGNDrr128 VR128:$src1, VR128:$src2)>, Requires<[HasSSSE3]>;
3584
3585 //===---------------------------------------------------------------------===//
3586 // SSSE3 - Packed Align Instruction Patterns
3587 //===---------------------------------------------------------------------===//
3588
3589 multiclass ssse3_palign<string asm, bit Is2Addr = 1> {
3590   def R128rr : SS3AI<0x0F, MRMSrcReg, (outs VR128:$dst),
3591       (ins VR128:$src1, VR128:$src2, i8imm:$src3),
3592       !if(Is2Addr,
3593         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3594         !strconcat(asm,
3595                   "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
3596       []>, OpSize;
3597   def R128rm : SS3AI<0x0F, MRMSrcMem, (outs VR128:$dst),
3598       (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
3599       !if(Is2Addr,
3600         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
3601         !strconcat(asm,
3602                   "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
3603       []>, OpSize;
3604 }
3605
3606 let Predicates = [HasAVX] in
3607   defm VPALIGN : ssse3_palign<"vpalignr", 0>, VEX_4V;
3608 let Constraints = "$src1 = $dst" in
3609   defm PALIGN : ssse3_palign<"palignr">;
3610
3611 let AddedComplexity = 5 in {
3612 def : Pat<(v4i32 (palign:$src3 VR128:$src1, VR128:$src2)),
3613           (PALIGNR128rr VR128:$src2, VR128:$src1,
3614                         (SHUFFLE_get_palign_imm VR128:$src3))>,
3615       Requires<[HasSSSE3]>;
3616 def : Pat<(v4f32 (palign:$src3 VR128:$src1, VR128:$src2)),
3617           (PALIGNR128rr VR128:$src2, VR128:$src1,
3618                         (SHUFFLE_get_palign_imm VR128:$src3))>,
3619       Requires<[HasSSSE3]>;
3620 def : Pat<(v8i16 (palign:$src3 VR128:$src1, VR128:$src2)),
3621           (PALIGNR128rr VR128:$src2, VR128:$src1,
3622                         (SHUFFLE_get_palign_imm VR128:$src3))>,
3623       Requires<[HasSSSE3]>;
3624 def : Pat<(v16i8 (palign:$src3 VR128:$src1, VR128:$src2)),
3625           (PALIGNR128rr VR128:$src2, VR128:$src1,
3626                         (SHUFFLE_get_palign_imm VR128:$src3))>,
3627       Requires<[HasSSSE3]>;
3628 }
3629
3630 //===---------------------------------------------------------------------===//
3631 // SSSE3 Misc Instructions
3632 //===---------------------------------------------------------------------===//
3633
3634 // Thread synchronization
3635 let usesCustomInserter = 1 in {
3636 def MONITOR : PseudoI<(outs), (ins i32mem:$src1, GR32:$src2, GR32:$src3),
3637                 [(int_x86_sse3_monitor addr:$src1, GR32:$src2, GR32:$src3)]>;
3638 def MWAIT : PseudoI<(outs), (ins GR32:$src1, GR32:$src2),
3639                 [(int_x86_sse3_mwait GR32:$src1, GR32:$src2)]>;
3640 }
3641
3642 let Uses = [EAX, ECX, EDX] in
3643 def MONITORrrr : I<0x01, MRM_C8, (outs), (ins), "monitor", []>, TB,
3644                  Requires<[HasSSE3]>;
3645 let Uses = [ECX, EAX] in
3646 def MWAITrr   : I<0x01, MRM_C9, (outs), (ins), "mwait", []>, TB,
3647                 Requires<[HasSSE3]>;
3648
3649 def : InstAlias<"mwait %eax, %ecx", (MWAITrr)>, Requires<[In32BitMode]>;
3650 def : InstAlias<"mwait %rax, %rcx", (MWAITrr)>, Requires<[In64BitMode]>;
3651
3652 def : InstAlias<"monitor %eax, %ecx, %edx", (MONITORrrr)>,
3653       Requires<[In32BitMode]>;
3654 def : InstAlias<"monitor %rax, %rcx, %rdx", (MONITORrrr)>,
3655       Requires<[In64BitMode]>;
3656
3657 //===---------------------------------------------------------------------===//
3658 // Non-Instruction Patterns
3659 //===---------------------------------------------------------------------===//
3660
3661 // extload f32 -> f64.  This matches load+fextend because we have a hack in
3662 // the isel (PreprocessForFPConvert) that can introduce loads after dag
3663 // combine.
3664 // Since these loads aren't folded into the fextend, we have to match it
3665 // explicitly here.
3666 let Predicates = [HasSSE2] in
3667  def : Pat<(fextend (loadf32 addr:$src)),
3668            (CVTSS2SDrm addr:$src)>;
3669
3670 // Bitcasts between 128-bit vector types. Return the original type since
3671 // no instruction is needed for the conversion
3672 let Predicates = [HasXMMInt] in {
3673   def : Pat<(v2i64 (bitconvert (v4i32 VR128:$src))), (v2i64 VR128:$src)>;
3674   def : Pat<(v2i64 (bitconvert (v8i16 VR128:$src))), (v2i64 VR128:$src)>;
3675   def : Pat<(v2i64 (bitconvert (v16i8 VR128:$src))), (v2i64 VR128:$src)>;
3676   def : Pat<(v2i64 (bitconvert (v2f64 VR128:$src))), (v2i64 VR128:$src)>;
3677   def : Pat<(v2i64 (bitconvert (v4f32 VR128:$src))), (v2i64 VR128:$src)>;
3678   def : Pat<(v4i32 (bitconvert (v2i64 VR128:$src))), (v4i32 VR128:$src)>;
3679   def : Pat<(v4i32 (bitconvert (v8i16 VR128:$src))), (v4i32 VR128:$src)>;
3680   def : Pat<(v4i32 (bitconvert (v16i8 VR128:$src))), (v4i32 VR128:$src)>;
3681   def : Pat<(v4i32 (bitconvert (v2f64 VR128:$src))), (v4i32 VR128:$src)>;
3682   def : Pat<(v4i32 (bitconvert (v4f32 VR128:$src))), (v4i32 VR128:$src)>;
3683   def : Pat<(v8i16 (bitconvert (v2i64 VR128:$src))), (v8i16 VR128:$src)>;
3684   def : Pat<(v8i16 (bitconvert (v4i32 VR128:$src))), (v8i16 VR128:$src)>;
3685   def : Pat<(v8i16 (bitconvert (v16i8 VR128:$src))), (v8i16 VR128:$src)>;
3686   def : Pat<(v8i16 (bitconvert (v2f64 VR128:$src))), (v8i16 VR128:$src)>;
3687   def : Pat<(v8i16 (bitconvert (v4f32 VR128:$src))), (v8i16 VR128:$src)>;
3688   def : Pat<(v16i8 (bitconvert (v2i64 VR128:$src))), (v16i8 VR128:$src)>;
3689   def : Pat<(v16i8 (bitconvert (v4i32 VR128:$src))), (v16i8 VR128:$src)>;
3690   def : Pat<(v16i8 (bitconvert (v8i16 VR128:$src))), (v16i8 VR128:$src)>;
3691   def : Pat<(v16i8 (bitconvert (v2f64 VR128:$src))), (v16i8 VR128:$src)>;
3692   def : Pat<(v16i8 (bitconvert (v4f32 VR128:$src))), (v16i8 VR128:$src)>;
3693   def : Pat<(v4f32 (bitconvert (v2i64 VR128:$src))), (v4f32 VR128:$src)>;
3694   def : Pat<(v4f32 (bitconvert (v4i32 VR128:$src))), (v4f32 VR128:$src)>;
3695   def : Pat<(v4f32 (bitconvert (v8i16 VR128:$src))), (v4f32 VR128:$src)>;
3696   def : Pat<(v4f32 (bitconvert (v16i8 VR128:$src))), (v4f32 VR128:$src)>;
3697   def : Pat<(v4f32 (bitconvert (v2f64 VR128:$src))), (v4f32 VR128:$src)>;
3698   def : Pat<(v2f64 (bitconvert (v2i64 VR128:$src))), (v2f64 VR128:$src)>;
3699   def : Pat<(v2f64 (bitconvert (v4i32 VR128:$src))), (v2f64 VR128:$src)>;
3700   def : Pat<(v2f64 (bitconvert (v8i16 VR128:$src))), (v2f64 VR128:$src)>;
3701   def : Pat<(v2f64 (bitconvert (v16i8 VR128:$src))), (v2f64 VR128:$src)>;
3702   def : Pat<(v2f64 (bitconvert (v4f32 VR128:$src))), (v2f64 VR128:$src)>;
3703 }
3704
3705 // Bitcasts between 256-bit vector types. Return the original type since
3706 // no instruction is needed for the conversion
3707 let Predicates = [HasAVX] in {
3708   def : Pat<(v4f64  (bitconvert (v8f32 VR256:$src))),  (v4f64 VR256:$src)>;
3709   def : Pat<(v4f64  (bitconvert (v8i32 VR256:$src))),  (v4f64 VR256:$src)>;
3710   def : Pat<(v4f64  (bitconvert (v4i64 VR256:$src))),  (v4f64 VR256:$src)>;
3711   def : Pat<(v4f64  (bitconvert (v16i16 VR256:$src))), (v4f64 VR256:$src)>;
3712   def : Pat<(v4f64  (bitconvert (v32i8 VR256:$src))),  (v4f64 VR256:$src)>;
3713   def : Pat<(v8f32  (bitconvert (v8i32 VR256:$src))),  (v8f32 VR256:$src)>;
3714   def : Pat<(v8f32  (bitconvert (v4i64 VR256:$src))),  (v8f32 VR256:$src)>;
3715   def : Pat<(v8f32  (bitconvert (v4f64 VR256:$src))),  (v8f32 VR256:$src)>;
3716   def : Pat<(v8f32  (bitconvert (v32i8 VR256:$src))),  (v8f32 VR256:$src)>;
3717   def : Pat<(v8f32  (bitconvert (v16i16 VR256:$src))), (v8f32 VR256:$src)>;
3718   def : Pat<(v4i64  (bitconvert (v8f32 VR256:$src))),  (v4i64 VR256:$src)>;
3719   def : Pat<(v4i64  (bitconvert (v8i32 VR256:$src))),  (v4i64 VR256:$src)>;
3720   def : Pat<(v4i64  (bitconvert (v4f64 VR256:$src))),  (v4i64 VR256:$src)>;
3721   def : Pat<(v4i64  (bitconvert (v32i8 VR256:$src))),  (v4i64 VR256:$src)>;
3722   def : Pat<(v4i64  (bitconvert (v16i16 VR256:$src))), (v4i64 VR256:$src)>;
3723   def : Pat<(v32i8  (bitconvert (v4f64 VR256:$src))),  (v32i8 VR256:$src)>;
3724   def : Pat<(v32i8  (bitconvert (v4i64 VR256:$src))),  (v32i8 VR256:$src)>;
3725   def : Pat<(v32i8  (bitconvert (v8f32 VR256:$src))),  (v32i8 VR256:$src)>;
3726   def : Pat<(v32i8  (bitconvert (v8i32 VR256:$src))),  (v32i8 VR256:$src)>;
3727   def : Pat<(v32i8  (bitconvert (v16i16 VR256:$src))), (v32i8 VR256:$src)>;
3728   def : Pat<(v8i32  (bitconvert (v32i8 VR256:$src))),  (v8i32 VR256:$src)>;
3729   def : Pat<(v8i32  (bitconvert (v16i16 VR256:$src))), (v8i32 VR256:$src)>;
3730   def : Pat<(v8i32  (bitconvert (v8f32 VR256:$src))),  (v8i32 VR256:$src)>;
3731   def : Pat<(v8i32  (bitconvert (v4i64 VR256:$src))),  (v8i32 VR256:$src)>;
3732   def : Pat<(v8i32  (bitconvert (v4f64 VR256:$src))),  (v8i32 VR256:$src)>;
3733   def : Pat<(v16i16 (bitconvert (v8f32 VR256:$src))),  (v16i16 VR256:$src)>;
3734   def : Pat<(v16i16 (bitconvert (v8i32 VR256:$src))),  (v16i16 VR256:$src)>;
3735   def : Pat<(v16i16 (bitconvert (v4i64 VR256:$src))),  (v16i16 VR256:$src)>;
3736   def : Pat<(v16i16 (bitconvert (v4f64 VR256:$src))),  (v16i16 VR256:$src)>;
3737   def : Pat<(v16i16 (bitconvert (v32i8 VR256:$src))),  (v16i16 VR256:$src)>;
3738 }
3739
3740 // Move scalar to XMM zero-extended
3741 // movd to XMM register zero-extends
3742 let AddedComplexity = 15 in {
3743 // Zeroing a VR128 then do a MOVS{S|D} to the lower bits.
3744 def : Pat<(v2f64 (X86vzmovl (v2f64 (scalar_to_vector FR64:$src)))),
3745           (MOVSDrr (v2f64 (V_SET0PS)), FR64:$src)>;
3746 def : Pat<(v4f32 (X86vzmovl (v4f32 (scalar_to_vector FR32:$src)))),
3747           (MOVSSrr (v4f32 (V_SET0PS)), FR32:$src)>;
3748 def : Pat<(v4f32 (X86vzmovl (v4f32 VR128:$src))),
3749           (MOVSSrr (v4f32 (V_SET0PS)),
3750                    (f32 (EXTRACT_SUBREG (v4f32 VR128:$src), sub_ss)))>;
3751 def : Pat<(v4i32 (X86vzmovl (v4i32 VR128:$src))),
3752           (MOVSSrr (v4i32 (V_SET0PI)),
3753                    (EXTRACT_SUBREG (v4i32 VR128:$src), sub_ss))>;
3754 }
3755
3756 // Splat v2f64 / v2i64
3757 let AddedComplexity = 10 in {
3758 def : Pat<(splat_lo (v2f64 VR128:$src), (undef)),
3759           (UNPCKLPDrr VR128:$src, VR128:$src)>,   Requires<[HasSSE2]>;
3760 def : Pat<(splat_lo (v2i64 VR128:$src), (undef)),
3761           (PUNPCKLQDQrr VR128:$src, VR128:$src)>, Requires<[HasSSE2]>;
3762 }
3763
3764 // Special unary SHUFPSrri case.
3765 def : Pat<(v4f32 (pshufd:$src3 VR128:$src1, (undef))),
3766           (SHUFPSrri VR128:$src1, VR128:$src1,
3767                      (SHUFFLE_get_shuf_imm VR128:$src3))>;
3768 let AddedComplexity = 5 in
3769 def : Pat<(v4f32 (pshufd:$src2 VR128:$src1, (undef))),
3770           (PSHUFDri VR128:$src1, (SHUFFLE_get_shuf_imm VR128:$src2))>,
3771       Requires<[HasSSE2]>;
3772 // Special unary SHUFPDrri case.
3773 def : Pat<(v2i64 (pshufd:$src3 VR128:$src1, (undef))),
3774           (SHUFPDrri VR128:$src1, VR128:$src1,
3775                      (SHUFFLE_get_shuf_imm VR128:$src3))>,
3776       Requires<[HasSSE2]>;
3777 // Special unary SHUFPDrri case.
3778 def : Pat<(v2f64 (pshufd:$src3 VR128:$src1, (undef))),
3779           (SHUFPDrri VR128:$src1, VR128:$src1,
3780                      (SHUFFLE_get_shuf_imm VR128:$src3))>,
3781       Requires<[HasSSE2]>;
3782 // Unary v4f32 shuffle with PSHUF* in order to fold a load.
3783 def : Pat<(pshufd:$src2 (bc_v4i32 (memopv4f32 addr:$src1)), (undef)),
3784           (PSHUFDmi addr:$src1, (SHUFFLE_get_shuf_imm VR128:$src2))>,
3785       Requires<[HasSSE2]>;
3786
3787 // Special binary v4i32 shuffle cases with SHUFPS.
3788 def : Pat<(v4i32 (shufp:$src3 VR128:$src1, (v4i32 VR128:$src2))),
3789           (SHUFPSrri VR128:$src1, VR128:$src2,
3790                      (SHUFFLE_get_shuf_imm VR128:$src3))>,
3791            Requires<[HasSSE2]>;
3792 def : Pat<(v4i32 (shufp:$src3 VR128:$src1, (bc_v4i32 (memopv2i64 addr:$src2)))),
3793           (SHUFPSrmi VR128:$src1, addr:$src2,
3794                     (SHUFFLE_get_shuf_imm VR128:$src3))>,
3795            Requires<[HasSSE2]>;
3796 // Special binary v2i64 shuffle cases using SHUFPDrri.
3797 def : Pat<(v2i64 (shufp:$src3 VR128:$src1, VR128:$src2)),
3798           (SHUFPDrri VR128:$src1, VR128:$src2,
3799                      (SHUFFLE_get_shuf_imm VR128:$src3))>,
3800           Requires<[HasSSE2]>;
3801
3802 let AddedComplexity = 20 in {
3803 // vector_shuffle v1, v2 <0, 1, 4, 5> using MOVLHPS
3804 def : Pat<(v4i32 (movlhps VR128:$src1, VR128:$src2)),
3805           (MOVLHPSrr VR128:$src1, VR128:$src2)>;
3806
3807 // vector_shuffle v1, v2 <6, 7, 2, 3> using MOVHLPS
3808 def : Pat<(v4i32 (movhlps VR128:$src1, VR128:$src2)),
3809           (MOVHLPSrr VR128:$src1, VR128:$src2)>;
3810
3811 // vector_shuffle v1, undef <2, ?, ?, ?> using MOVHLPS
3812 def : Pat<(v4f32 (movhlps_undef VR128:$src1, (undef))),
3813           (MOVHLPSrr VR128:$src1, VR128:$src1)>;
3814 def : Pat<(v4i32 (movhlps_undef VR128:$src1, (undef))),
3815           (MOVHLPSrr VR128:$src1, VR128:$src1)>;
3816 }
3817
3818 let AddedComplexity = 20 in {
3819 // vector_shuffle v1, (load v2) <4, 5, 2, 3> using MOVLPS
3820 def : Pat<(v4f32 (movlp VR128:$src1, (load addr:$src2))),
3821           (MOVLPSrm VR128:$src1, addr:$src2)>;
3822 def : Pat<(v2f64 (movlp VR128:$src1, (load addr:$src2))),
3823           (MOVLPDrm VR128:$src1, addr:$src2)>;
3824 def : Pat<(v4i32 (movlp VR128:$src1, (load addr:$src2))),
3825           (MOVLPSrm VR128:$src1, addr:$src2)>;
3826 def : Pat<(v2i64 (movlp VR128:$src1, (load addr:$src2))),
3827           (MOVLPDrm VR128:$src1, addr:$src2)>;
3828 }
3829
3830 // (store (vector_shuffle (load addr), v2, <4, 5, 2, 3>), addr) using MOVLPS
3831 def : Pat<(store (v4f32 (movlp (load addr:$src1), VR128:$src2)), addr:$src1),
3832           (MOVLPSmr addr:$src1, VR128:$src2)>;
3833 def : Pat<(store (v2f64 (movlp (load addr:$src1), VR128:$src2)), addr:$src1),
3834           (MOVLPDmr addr:$src1, VR128:$src2)>;
3835 def : Pat<(store (v4i32 (movlp (bc_v4i32 (loadv2i64 addr:$src1)), VR128:$src2)),
3836                  addr:$src1),
3837           (MOVLPSmr addr:$src1, VR128:$src2)>;
3838 def : Pat<(store (v2i64 (movlp (load addr:$src1), VR128:$src2)), addr:$src1),
3839           (MOVLPDmr addr:$src1, VR128:$src2)>;
3840
3841 let AddedComplexity = 15 in {
3842 // Setting the lowest element in the vector.
3843 def : Pat<(v4i32 (movl VR128:$src1, VR128:$src2)),
3844           (MOVSSrr (v4i32 VR128:$src1),
3845                    (EXTRACT_SUBREG (v4i32 VR128:$src2), sub_ss))>;
3846 def : Pat<(v2i64 (movl VR128:$src1, VR128:$src2)),
3847           (MOVSDrr (v2i64 VR128:$src1),
3848                    (EXTRACT_SUBREG (v2i64 VR128:$src2), sub_sd))>;
3849
3850 // vector_shuffle v1, v2 <4, 5, 2, 3> using movsd
3851 def : Pat<(v4f32 (movlp VR128:$src1, VR128:$src2)),
3852           (MOVSDrr VR128:$src1, (EXTRACT_SUBREG VR128:$src2, sub_sd))>,
3853       Requires<[HasSSE2]>;
3854 def : Pat<(v4i32 (movlp VR128:$src1, VR128:$src2)),
3855           (MOVSDrr VR128:$src1, (EXTRACT_SUBREG VR128:$src2, sub_sd))>,
3856       Requires<[HasSSE2]>;
3857 }
3858
3859 // vector_shuffle v1, v2 <4, 5, 2, 3> using SHUFPSrri (we prefer movsd, but
3860 // fall back to this for SSE1)
3861 def : Pat<(v4f32 (movlp:$src3 VR128:$src1, (v4f32 VR128:$src2))),
3862           (SHUFPSrri VR128:$src2, VR128:$src1,
3863                      (SHUFFLE_get_shuf_imm VR128:$src3))>;
3864
3865 // Set lowest element and zero upper elements.
3866 def : Pat<(v2f64 (X86vzmovl (v2f64 VR128:$src))),
3867           (MOVZPQILo2PQIrr VR128:$src)>, Requires<[HasSSE2]>;
3868
3869 // Use movaps / movups for SSE integer load / store (one byte shorter).
3870 // The instructions selected below are then converted to MOVDQA/MOVDQU
3871 // during the SSE domain pass.
3872 let Predicates = [HasSSE1] in {
3873   def : Pat<(alignedloadv4i32 addr:$src),
3874             (MOVAPSrm addr:$src)>;
3875   def : Pat<(loadv4i32 addr:$src),
3876             (MOVUPSrm addr:$src)>;
3877   def : Pat<(alignedloadv2i64 addr:$src),
3878             (MOVAPSrm addr:$src)>;
3879   def : Pat<(loadv2i64 addr:$src),
3880             (MOVUPSrm addr:$src)>;
3881
3882   def : Pat<(alignedstore (v2i64 VR128:$src), addr:$dst),
3883             (MOVAPSmr addr:$dst, VR128:$src)>;
3884   def : Pat<(alignedstore (v4i32 VR128:$src), addr:$dst),
3885             (MOVAPSmr addr:$dst, VR128:$src)>;
3886   def : Pat<(alignedstore (v8i16 VR128:$src), addr:$dst),
3887             (MOVAPSmr addr:$dst, VR128:$src)>;
3888   def : Pat<(alignedstore (v16i8 VR128:$src), addr:$dst),
3889             (MOVAPSmr addr:$dst, VR128:$src)>;
3890   def : Pat<(store (v2i64 VR128:$src), addr:$dst),
3891             (MOVUPSmr addr:$dst, VR128:$src)>;
3892   def : Pat<(store (v4i32 VR128:$src), addr:$dst),
3893             (MOVUPSmr addr:$dst, VR128:$src)>;
3894   def : Pat<(store (v8i16 VR128:$src), addr:$dst),
3895             (MOVUPSmr addr:$dst, VR128:$src)>;
3896   def : Pat<(store (v16i8 VR128:$src), addr:$dst),
3897             (MOVUPSmr addr:$dst, VR128:$src)>;
3898 }
3899
3900 // Use vmovaps/vmovups for AVX integer load/store.
3901 let Predicates = [HasAVX] in {
3902   // 128-bit load/store
3903   def : Pat<(alignedloadv4i32 addr:$src),
3904             (VMOVAPSrm addr:$src)>;
3905   def : Pat<(loadv4i32 addr:$src),
3906             (VMOVUPSrm addr:$src)>;
3907   def : Pat<(alignedloadv2i64 addr:$src),
3908             (VMOVAPSrm addr:$src)>;
3909   def : Pat<(loadv2i64 addr:$src),
3910             (VMOVUPSrm addr:$src)>;
3911
3912   def : Pat<(alignedstore (v2i64 VR128:$src), addr:$dst),
3913             (VMOVAPSmr addr:$dst, VR128:$src)>;
3914   def : Pat<(alignedstore (v4i32 VR128:$src), addr:$dst),
3915             (VMOVAPSmr addr:$dst, VR128:$src)>;
3916   def : Pat<(alignedstore (v8i16 VR128:$src), addr:$dst),
3917             (VMOVAPSmr addr:$dst, VR128:$src)>;
3918   def : Pat<(alignedstore (v16i8 VR128:$src), addr:$dst),
3919             (VMOVAPSmr addr:$dst, VR128:$src)>;
3920   def : Pat<(store (v2i64 VR128:$src), addr:$dst),
3921             (VMOVUPSmr addr:$dst, VR128:$src)>;
3922   def : Pat<(store (v4i32 VR128:$src), addr:$dst),
3923             (VMOVUPSmr addr:$dst, VR128:$src)>;
3924   def : Pat<(store (v8i16 VR128:$src), addr:$dst),
3925             (VMOVUPSmr addr:$dst, VR128:$src)>;
3926   def : Pat<(store (v16i8 VR128:$src), addr:$dst),
3927             (VMOVUPSmr addr:$dst, VR128:$src)>;
3928
3929   // 256-bit load/store
3930   def : Pat<(alignedloadv4i64 addr:$src),
3931             (VMOVAPSYrm addr:$src)>;
3932   def : Pat<(loadv4i64 addr:$src),
3933             (VMOVUPSYrm addr:$src)>;
3934   def : Pat<(alignedloadv8i32 addr:$src),
3935             (VMOVAPSYrm addr:$src)>;
3936   def : Pat<(loadv8i32 addr:$src),
3937             (VMOVUPSYrm addr:$src)>;
3938   def : Pat<(alignedstore (v4i64 VR256:$src), addr:$dst),
3939             (VMOVAPSYmr addr:$dst, VR256:$src)>;
3940   def : Pat<(alignedstore (v8i32 VR256:$src), addr:$dst),
3941             (VMOVAPSYmr addr:$dst, VR256:$src)>;
3942   def : Pat<(store (v4i64 VR256:$src), addr:$dst),
3943             (VMOVUPSYmr addr:$dst, VR256:$src)>;
3944   def : Pat<(store (v8i32 VR256:$src), addr:$dst),
3945             (VMOVUPSYmr addr:$dst, VR256:$src)>;
3946 }
3947
3948 //===----------------------------------------------------------------------===//
3949 // SSE4.1 - Packed Move with Sign/Zero Extend
3950 //===----------------------------------------------------------------------===//
3951
3952 multiclass SS41I_binop_rm_int8<bits<8> opc, string OpcodeStr, Intrinsic IntId> {
3953   def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
3954                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3955                  [(set VR128:$dst, (IntId VR128:$src))]>, OpSize;
3956
3957   def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst), (ins i64mem:$src),
3958                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
3959        [(set VR128:$dst,
3960          (IntId (bitconvert (v2i64 (scalar_to_vector (loadi64 addr:$src))))))]>,
3961        OpSize;
3962 }
3963
3964 let Predicates = [HasAVX] in {
3965 defm VPMOVSXBW : SS41I_binop_rm_int8<0x20, "vpmovsxbw", int_x86_sse41_pmovsxbw>,
3966                                      VEX;
3967 defm VPMOVSXWD : SS41I_binop_rm_int8<0x23, "vpmovsxwd", int_x86_sse41_pmovsxwd>,
3968                                      VEX;
3969 defm VPMOVSXDQ : SS41I_binop_rm_int8<0x25, "vpmovsxdq", int_x86_sse41_pmovsxdq>,
3970                                      VEX;
3971 defm VPMOVZXBW : SS41I_binop_rm_int8<0x30, "vpmovzxbw", int_x86_sse41_pmovzxbw>,
3972                                      VEX;
3973 defm VPMOVZXWD : SS41I_binop_rm_int8<0x33, "vpmovzxwd", int_x86_sse41_pmovzxwd>,
3974                                      VEX;
3975 defm VPMOVZXDQ : SS41I_binop_rm_int8<0x35, "vpmovzxdq", int_x86_sse41_pmovzxdq>,
3976                                      VEX;
3977 }
3978
3979 defm PMOVSXBW   : SS41I_binop_rm_int8<0x20, "pmovsxbw", int_x86_sse41_pmovsxbw>;
3980 defm PMOVSXWD   : SS41I_binop_rm_int8<0x23, "pmovsxwd", int_x86_sse41_pmovsxwd>;
3981 defm PMOVSXDQ   : SS41I_binop_rm_int8<0x25, "pmovsxdq", int_x86_sse41_pmovsxdq>;
3982 defm PMOVZXBW   : SS41I_binop_rm_int8<0x30, "pmovzxbw", int_x86_sse41_pmovzxbw>;
3983 defm PMOVZXWD   : SS41I_binop_rm_int8<0x33, "pmovzxwd", int_x86_sse41_pmovzxwd>;
3984 defm PMOVZXDQ   : SS41I_binop_rm_int8<0x35, "pmovzxdq", int_x86_sse41_pmovzxdq>;
3985
3986 // Common patterns involving scalar load.
3987 def : Pat<(int_x86_sse41_pmovsxbw (vzmovl_v2i64 addr:$src)),
3988           (PMOVSXBWrm addr:$src)>, Requires<[HasSSE41]>;
3989 def : Pat<(int_x86_sse41_pmovsxbw (vzload_v2i64 addr:$src)),
3990           (PMOVSXBWrm addr:$src)>, Requires<[HasSSE41]>;
3991
3992 def : Pat<(int_x86_sse41_pmovsxwd (vzmovl_v2i64 addr:$src)),
3993           (PMOVSXWDrm addr:$src)>, Requires<[HasSSE41]>;
3994 def : Pat<(int_x86_sse41_pmovsxwd (vzload_v2i64 addr:$src)),
3995           (PMOVSXWDrm addr:$src)>, Requires<[HasSSE41]>;
3996
3997 def : Pat<(int_x86_sse41_pmovsxdq (vzmovl_v2i64 addr:$src)),
3998           (PMOVSXDQrm addr:$src)>, Requires<[HasSSE41]>;
3999 def : Pat<(int_x86_sse41_pmovsxdq (vzload_v2i64 addr:$src)),
4000           (PMOVSXDQrm addr:$src)>, Requires<[HasSSE41]>;
4001
4002 def : Pat<(int_x86_sse41_pmovzxbw (vzmovl_v2i64 addr:$src)),
4003           (PMOVZXBWrm addr:$src)>, Requires<[HasSSE41]>;
4004 def : Pat<(int_x86_sse41_pmovzxbw (vzload_v2i64 addr:$src)),
4005           (PMOVZXBWrm addr:$src)>, Requires<[HasSSE41]>;
4006
4007 def : Pat<(int_x86_sse41_pmovzxwd (vzmovl_v2i64 addr:$src)),
4008           (PMOVZXWDrm addr:$src)>, Requires<[HasSSE41]>;
4009 def : Pat<(int_x86_sse41_pmovzxwd (vzload_v2i64 addr:$src)),
4010           (PMOVZXWDrm addr:$src)>, Requires<[HasSSE41]>;
4011
4012 def : Pat<(int_x86_sse41_pmovzxdq (vzmovl_v2i64 addr:$src)),
4013           (PMOVZXDQrm addr:$src)>, Requires<[HasSSE41]>;
4014 def : Pat<(int_x86_sse41_pmovzxdq (vzload_v2i64 addr:$src)),
4015           (PMOVZXDQrm addr:$src)>, Requires<[HasSSE41]>;
4016
4017
4018 multiclass SS41I_binop_rm_int4<bits<8> opc, string OpcodeStr, Intrinsic IntId> {
4019   def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
4020                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
4021                  [(set VR128:$dst, (IntId VR128:$src))]>, OpSize;
4022
4023   def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst), (ins i32mem:$src),
4024                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
4025        [(set VR128:$dst,
4026          (IntId (bitconvert (v4i32 (scalar_to_vector (loadi32 addr:$src))))))]>,
4027           OpSize;
4028 }
4029
4030 let Predicates = [HasAVX] in {
4031 defm VPMOVSXBD : SS41I_binop_rm_int4<0x21, "vpmovsxbd", int_x86_sse41_pmovsxbd>,
4032                                      VEX;
4033 defm VPMOVSXWQ : SS41I_binop_rm_int4<0x24, "vpmovsxwq", int_x86_sse41_pmovsxwq>,
4034                                      VEX;
4035 defm VPMOVZXBD : SS41I_binop_rm_int4<0x31, "vpmovzxbd", int_x86_sse41_pmovzxbd>,
4036                                      VEX;
4037 defm VPMOVZXWQ : SS41I_binop_rm_int4<0x34, "vpmovzxwq", int_x86_sse41_pmovzxwq>,
4038                                      VEX;
4039 }
4040
4041 defm PMOVSXBD   : SS41I_binop_rm_int4<0x21, "pmovsxbd", int_x86_sse41_pmovsxbd>;
4042 defm PMOVSXWQ   : SS41I_binop_rm_int4<0x24, "pmovsxwq", int_x86_sse41_pmovsxwq>;
4043 defm PMOVZXBD   : SS41I_binop_rm_int4<0x31, "pmovzxbd", int_x86_sse41_pmovzxbd>;
4044 defm PMOVZXWQ   : SS41I_binop_rm_int4<0x34, "pmovzxwq", int_x86_sse41_pmovzxwq>;
4045
4046 // Common patterns involving scalar load
4047 def : Pat<(int_x86_sse41_pmovsxbd (vzmovl_v4i32 addr:$src)),
4048           (PMOVSXBDrm addr:$src)>, Requires<[HasSSE41]>;
4049 def : Pat<(int_x86_sse41_pmovsxwq (vzmovl_v4i32 addr:$src)),
4050           (PMOVSXWQrm addr:$src)>, Requires<[HasSSE41]>;
4051
4052 def : Pat<(int_x86_sse41_pmovzxbd (vzmovl_v4i32 addr:$src)),
4053           (PMOVZXBDrm addr:$src)>, Requires<[HasSSE41]>;
4054 def : Pat<(int_x86_sse41_pmovzxwq (vzmovl_v4i32 addr:$src)),
4055           (PMOVZXWQrm addr:$src)>, Requires<[HasSSE41]>;
4056
4057
4058 multiclass SS41I_binop_rm_int2<bits<8> opc, string OpcodeStr, Intrinsic IntId> {
4059   def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst), (ins VR128:$src),
4060                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
4061                  [(set VR128:$dst, (IntId VR128:$src))]>, OpSize;
4062
4063   // Expecting a i16 load any extended to i32 value.
4064   def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst), (ins i16mem:$src),
4065                  !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
4066                  [(set VR128:$dst, (IntId (bitconvert
4067                      (v4i32 (scalar_to_vector (loadi16_anyext addr:$src))))))]>,
4068                  OpSize;
4069 }
4070
4071 let Predicates = [HasAVX] in {
4072 defm VPMOVSXBQ : SS41I_binop_rm_int2<0x22, "vpmovsxbq", int_x86_sse41_pmovsxbq>,
4073                                      VEX;
4074 defm VPMOVZXBQ : SS41I_binop_rm_int2<0x32, "vpmovzxbq", int_x86_sse41_pmovzxbq>,
4075                                      VEX;
4076 }
4077 defm PMOVSXBQ   : SS41I_binop_rm_int2<0x22, "pmovsxbq", int_x86_sse41_pmovsxbq>;
4078 defm PMOVZXBQ   : SS41I_binop_rm_int2<0x32, "pmovzxbq", int_x86_sse41_pmovzxbq>;
4079
4080 // Common patterns involving scalar load
4081 def : Pat<(int_x86_sse41_pmovsxbq
4082             (bitconvert (v4i32 (X86vzmovl
4083                              (v4i32 (scalar_to_vector (loadi32 addr:$src))))))),
4084           (PMOVSXBQrm addr:$src)>, Requires<[HasSSE41]>;
4085
4086 def : Pat<(int_x86_sse41_pmovzxbq
4087             (bitconvert (v4i32 (X86vzmovl
4088                              (v4i32 (scalar_to_vector (loadi32 addr:$src))))))),
4089           (PMOVZXBQrm addr:$src)>, Requires<[HasSSE41]>;
4090
4091 //===----------------------------------------------------------------------===//
4092 // SSE4.1 - Extract Instructions
4093 //===----------------------------------------------------------------------===//
4094
4095 /// SS41I_binop_ext8 - SSE 4.1 extract 8 bits to 32 bit reg or 8 bit mem
4096 multiclass SS41I_extract8<bits<8> opc, string OpcodeStr> {
4097   def rr : SS4AIi8<opc, MRMDestReg, (outs GR32:$dst),
4098                  (ins VR128:$src1, i32i8imm:$src2),
4099                  !strconcat(OpcodeStr,
4100                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4101                  [(set GR32:$dst, (X86pextrb (v16i8 VR128:$src1), imm:$src2))]>,
4102                  OpSize;
4103   def mr : SS4AIi8<opc, MRMDestMem, (outs),
4104                  (ins i8mem:$dst, VR128:$src1, i32i8imm:$src2),
4105                  !strconcat(OpcodeStr,
4106                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4107                  []>, OpSize;
4108 // FIXME:
4109 // There's an AssertZext in the way of writing the store pattern
4110 // (store (i8 (trunc (X86pextrb (v16i8 VR128:$src1), imm:$src2))), addr:$dst)
4111 }
4112
4113 let Predicates = [HasAVX] in {
4114   defm VPEXTRB : SS41I_extract8<0x14, "vpextrb">, VEX;
4115   def  VPEXTRBrr64 : SS4AIi8<0x14, MRMDestReg, (outs GR64:$dst),
4116          (ins VR128:$src1, i32i8imm:$src2),
4117          "vpextrb\t{$src2, $src1, $dst|$dst, $src1, $src2}", []>, OpSize, VEX;
4118 }
4119
4120 defm PEXTRB      : SS41I_extract8<0x14, "pextrb">;
4121
4122
4123 /// SS41I_extract16 - SSE 4.1 extract 16 bits to memory destination
4124 multiclass SS41I_extract16<bits<8> opc, string OpcodeStr> {
4125   def mr : SS4AIi8<opc, MRMDestMem, (outs),
4126                  (ins i16mem:$dst, VR128:$src1, i32i8imm:$src2),
4127                  !strconcat(OpcodeStr,
4128                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4129                  []>, OpSize;
4130 // FIXME:
4131 // There's an AssertZext in the way of writing the store pattern
4132 // (store (i16 (trunc (X86pextrw (v16i8 VR128:$src1), imm:$src2))), addr:$dst)
4133 }
4134
4135 let Predicates = [HasAVX] in
4136   defm VPEXTRW : SS41I_extract16<0x15, "vpextrw">, VEX;
4137
4138 defm PEXTRW      : SS41I_extract16<0x15, "pextrw">;
4139
4140
4141 /// SS41I_extract32 - SSE 4.1 extract 32 bits to int reg or memory destination
4142 multiclass SS41I_extract32<bits<8> opc, string OpcodeStr> {
4143   def rr : SS4AIi8<opc, MRMDestReg, (outs GR32:$dst),
4144                  (ins VR128:$src1, i32i8imm:$src2),
4145                  !strconcat(OpcodeStr,
4146                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4147                  [(set GR32:$dst,
4148                   (extractelt (v4i32 VR128:$src1), imm:$src2))]>, OpSize;
4149   def mr : SS4AIi8<opc, MRMDestMem, (outs),
4150                  (ins i32mem:$dst, VR128:$src1, i32i8imm:$src2),
4151                  !strconcat(OpcodeStr,
4152                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4153                  [(store (extractelt (v4i32 VR128:$src1), imm:$src2),
4154                           addr:$dst)]>, OpSize;
4155 }
4156
4157 let Predicates = [HasAVX] in
4158   defm VPEXTRD : SS41I_extract32<0x16, "vpextrd">, VEX;
4159
4160 defm PEXTRD      : SS41I_extract32<0x16, "pextrd">;
4161
4162 /// SS41I_extract32 - SSE 4.1 extract 32 bits to int reg or memory destination
4163 multiclass SS41I_extract64<bits<8> opc, string OpcodeStr> {
4164   def rr : SS4AIi8<opc, MRMDestReg, (outs GR64:$dst),
4165                  (ins VR128:$src1, i32i8imm:$src2),
4166                  !strconcat(OpcodeStr,
4167                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4168                  [(set GR64:$dst,
4169                   (extractelt (v2i64 VR128:$src1), imm:$src2))]>, OpSize, REX_W;
4170   def mr : SS4AIi8<opc, MRMDestMem, (outs),
4171                  (ins i64mem:$dst, VR128:$src1, i32i8imm:$src2),
4172                  !strconcat(OpcodeStr,
4173                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4174                  [(store (extractelt (v2i64 VR128:$src1), imm:$src2),
4175                           addr:$dst)]>, OpSize, REX_W;
4176 }
4177
4178 let Predicates = [HasAVX] in
4179   defm VPEXTRQ : SS41I_extract64<0x16, "vpextrq">, VEX, VEX_W;
4180
4181 defm PEXTRQ      : SS41I_extract64<0x16, "pextrq">;
4182
4183 /// SS41I_extractf32 - SSE 4.1 extract 32 bits fp value to int reg or memory
4184 /// destination
4185 multiclass SS41I_extractf32<bits<8> opc, string OpcodeStr> {
4186   def rr : SS4AIi8<opc, MRMDestReg, (outs GR32:$dst),
4187                  (ins VR128:$src1, i32i8imm:$src2),
4188                  !strconcat(OpcodeStr,
4189                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4190                  [(set GR32:$dst,
4191                     (extractelt (bc_v4i32 (v4f32 VR128:$src1)), imm:$src2))]>,
4192            OpSize;
4193   def mr : SS4AIi8<opc, MRMDestMem, (outs),
4194                  (ins f32mem:$dst, VR128:$src1, i32i8imm:$src2),
4195                  !strconcat(OpcodeStr,
4196                   "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4197                  [(store (extractelt (bc_v4i32 (v4f32 VR128:$src1)), imm:$src2),
4198                           addr:$dst)]>, OpSize;
4199 }
4200
4201 let Predicates = [HasAVX] in {
4202   defm VEXTRACTPS : SS41I_extractf32<0x17, "vextractps">, VEX;
4203   def VEXTRACTPSrr64 : SS4AIi8<0x17, MRMDestReg, (outs GR64:$dst),
4204                   (ins VR128:$src1, i32i8imm:$src2),
4205                   "vextractps \t{$src2, $src1, $dst|$dst, $src1, $src2}",
4206                   []>, OpSize, VEX;
4207 }
4208 defm EXTRACTPS   : SS41I_extractf32<0x17, "extractps">;
4209
4210 // Also match an EXTRACTPS store when the store is done as f32 instead of i32.
4211 def : Pat<(store (f32 (bitconvert (extractelt (bc_v4i32 (v4f32 VR128:$src1)),
4212                                               imm:$src2))),
4213                  addr:$dst),
4214           (EXTRACTPSmr addr:$dst, VR128:$src1, imm:$src2)>,
4215          Requires<[HasSSE41]>;
4216
4217 //===----------------------------------------------------------------------===//
4218 // SSE4.1 - Insert Instructions
4219 //===----------------------------------------------------------------------===//
4220
4221 multiclass SS41I_insert8<bits<8> opc, string asm, bit Is2Addr = 1> {
4222   def rr : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
4223       (ins VR128:$src1, GR32:$src2, i32i8imm:$src3),
4224       !if(Is2Addr,
4225         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4226         !strconcat(asm,
4227                    "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4228       [(set VR128:$dst,
4229         (X86pinsrb VR128:$src1, GR32:$src2, imm:$src3))]>, OpSize;
4230   def rm : SS4AIi8<opc, MRMSrcMem, (outs VR128:$dst),
4231       (ins VR128:$src1, i8mem:$src2, i32i8imm:$src3),
4232       !if(Is2Addr,
4233         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4234         !strconcat(asm,
4235                    "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4236       [(set VR128:$dst,
4237         (X86pinsrb VR128:$src1, (extloadi8 addr:$src2),
4238                    imm:$src3))]>, OpSize;
4239 }
4240
4241 let Predicates = [HasAVX] in
4242   defm VPINSRB : SS41I_insert8<0x20, "vpinsrb", 0>, VEX_4V;
4243 let Constraints = "$src1 = $dst" in
4244   defm PINSRB  : SS41I_insert8<0x20, "pinsrb">;
4245
4246 multiclass SS41I_insert32<bits<8> opc, string asm, bit Is2Addr = 1> {
4247   def rr : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
4248       (ins VR128:$src1, GR32:$src2, i32i8imm:$src3),
4249       !if(Is2Addr,
4250         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4251         !strconcat(asm,
4252                    "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4253       [(set VR128:$dst,
4254         (v4i32 (insertelt VR128:$src1, GR32:$src2, imm:$src3)))]>,
4255       OpSize;
4256   def rm : SS4AIi8<opc, MRMSrcMem, (outs VR128:$dst),
4257       (ins VR128:$src1, i32mem:$src2, i32i8imm:$src3),
4258       !if(Is2Addr,
4259         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4260         !strconcat(asm,
4261                    "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4262       [(set VR128:$dst,
4263         (v4i32 (insertelt VR128:$src1, (loadi32 addr:$src2),
4264                           imm:$src3)))]>, OpSize;
4265 }
4266
4267 let Predicates = [HasAVX] in
4268   defm VPINSRD : SS41I_insert32<0x22, "vpinsrd", 0>, VEX_4V;
4269 let Constraints = "$src1 = $dst" in
4270   defm PINSRD : SS41I_insert32<0x22, "pinsrd">;
4271
4272 multiclass SS41I_insert64<bits<8> opc, string asm, bit Is2Addr = 1> {
4273   def rr : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
4274       (ins VR128:$src1, GR64:$src2, i32i8imm:$src3),
4275       !if(Is2Addr,
4276         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4277         !strconcat(asm,
4278                    "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4279       [(set VR128:$dst,
4280         (v2i64 (insertelt VR128:$src1, GR64:$src2, imm:$src3)))]>,
4281       OpSize;
4282   def rm : SS4AIi8<opc, MRMSrcMem, (outs VR128:$dst),
4283       (ins VR128:$src1, i64mem:$src2, i32i8imm:$src3),
4284       !if(Is2Addr,
4285         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4286         !strconcat(asm,
4287                    "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4288       [(set VR128:$dst,
4289         (v2i64 (insertelt VR128:$src1, (loadi64 addr:$src2),
4290                           imm:$src3)))]>, OpSize;
4291 }
4292
4293 let Predicates = [HasAVX] in
4294   defm VPINSRQ : SS41I_insert64<0x22, "vpinsrq", 0>, VEX_4V, VEX_W;
4295 let Constraints = "$src1 = $dst" in
4296   defm PINSRQ : SS41I_insert64<0x22, "pinsrq">, REX_W;
4297
4298 // insertps has a few different modes, there's the first two here below which
4299 // are optimized inserts that won't zero arbitrary elements in the destination
4300 // vector. The next one matches the intrinsic and could zero arbitrary elements
4301 // in the target vector.
4302 multiclass SS41I_insertf32<bits<8> opc, string asm, bit Is2Addr = 1> {
4303   def rr : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
4304       (ins VR128:$src1, VR128:$src2, u32u8imm:$src3),
4305       !if(Is2Addr,
4306         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4307         !strconcat(asm,
4308                    "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4309       [(set VR128:$dst,
4310         (X86insrtps VR128:$src1, VR128:$src2, imm:$src3))]>,
4311       OpSize;
4312   def rm : SS4AIi8<opc, MRMSrcMem, (outs VR128:$dst),
4313       (ins VR128:$src1, f32mem:$src2, u32u8imm:$src3),
4314       !if(Is2Addr,
4315         !strconcat(asm, "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4316         !strconcat(asm,
4317                    "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4318       [(set VR128:$dst,
4319         (X86insrtps VR128:$src1,
4320                    (v4f32 (scalar_to_vector (loadf32 addr:$src2))),
4321                     imm:$src3))]>, OpSize;
4322 }
4323
4324 let Constraints = "$src1 = $dst" in
4325   defm INSERTPS : SS41I_insertf32<0x21, "insertps">;
4326 let Predicates = [HasAVX] in
4327   defm VINSERTPS : SS41I_insertf32<0x21, "vinsertps", 0>, VEX_4V;
4328
4329 def : Pat<(int_x86_sse41_insertps VR128:$src1, VR128:$src2, imm:$src3),
4330           (VINSERTPSrr VR128:$src1, VR128:$src2, imm:$src3)>,
4331           Requires<[HasAVX]>;
4332 def : Pat<(int_x86_sse41_insertps VR128:$src1, VR128:$src2, imm:$src3),
4333           (INSERTPSrr VR128:$src1, VR128:$src2, imm:$src3)>,
4334           Requires<[HasSSE41]>;
4335
4336 //===----------------------------------------------------------------------===//
4337 // SSE4.1 - Round Instructions
4338 //===----------------------------------------------------------------------===//
4339
4340 multiclass sse41_fp_unop_rm<bits<8> opcps, bits<8> opcpd, string OpcodeStr,
4341                             X86MemOperand x86memop, RegisterClass RC,
4342                             PatFrag mem_frag32, PatFrag mem_frag64,
4343                             Intrinsic V4F32Int, Intrinsic V2F64Int> {
4344   // Intrinsic operation, reg.
4345   // Vector intrinsic operation, reg
4346   def PSr : SS4AIi8<opcps, MRMSrcReg,
4347                     (outs RC:$dst), (ins RC:$src1, i32i8imm:$src2),
4348                     !strconcat(OpcodeStr,
4349                     "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4350                     [(set RC:$dst, (V4F32Int RC:$src1, imm:$src2))]>,
4351                     OpSize;
4352
4353   // Vector intrinsic operation, mem
4354   def PSm : Ii8<opcps, MRMSrcMem,
4355                     (outs RC:$dst), (ins f256mem:$src1, i32i8imm:$src2),
4356                     !strconcat(OpcodeStr,
4357                     "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4358                     [(set RC:$dst,
4359                           (V4F32Int (mem_frag32 addr:$src1),imm:$src2))]>,
4360                     TA, OpSize,
4361                 Requires<[HasSSE41]>;
4362
4363   // Vector intrinsic operation, reg
4364   def PDr : SS4AIi8<opcpd, MRMSrcReg,
4365                     (outs RC:$dst), (ins RC:$src1, i32i8imm:$src2),
4366                     !strconcat(OpcodeStr,
4367                     "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4368                     [(set RC:$dst, (V2F64Int RC:$src1, imm:$src2))]>,
4369                     OpSize;
4370
4371   // Vector intrinsic operation, mem
4372   def PDm : SS4AIi8<opcpd, MRMSrcMem,
4373                     (outs RC:$dst), (ins f256mem:$src1, i32i8imm:$src2),
4374                     !strconcat(OpcodeStr,
4375                     "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4376                     [(set RC:$dst,
4377                           (V2F64Int (mem_frag64 addr:$src1),imm:$src2))]>,
4378                     OpSize;
4379 }
4380
4381 multiclass sse41_fp_unop_rm_avx_p<bits<8> opcps, bits<8> opcpd,
4382                    RegisterClass RC, X86MemOperand x86memop, string OpcodeStr> {
4383   // Intrinsic operation, reg.
4384   // Vector intrinsic operation, reg
4385   def PSr_AVX : SS4AIi8<opcps, MRMSrcReg,
4386                     (outs RC:$dst), (ins RC:$src1, i32i8imm:$src2),
4387                     !strconcat(OpcodeStr,
4388                     "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4389                     []>, OpSize;
4390
4391   // Vector intrinsic operation, mem
4392   def PSm_AVX : Ii8<opcps, MRMSrcMem,
4393                     (outs RC:$dst), (ins x86memop:$src1, i32i8imm:$src2),
4394                     !strconcat(OpcodeStr,
4395                     "ps\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4396                     []>, TA, OpSize, Requires<[HasSSE41]>;
4397
4398   // Vector intrinsic operation, reg
4399   def PDr_AVX : SS4AIi8<opcpd, MRMSrcReg,
4400                     (outs RC:$dst), (ins RC:$src1, i32i8imm:$src2),
4401                     !strconcat(OpcodeStr,
4402                     "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4403                     []>, OpSize;
4404
4405   // Vector intrinsic operation, mem
4406   def PDm_AVX : SS4AIi8<opcpd, MRMSrcMem,
4407                     (outs RC:$dst), (ins x86memop:$src1, i32i8imm:$src2),
4408                     !strconcat(OpcodeStr,
4409                     "pd\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
4410                     []>, OpSize;
4411 }
4412
4413 multiclass sse41_fp_binop_rm<bits<8> opcss, bits<8> opcsd,
4414                             string OpcodeStr,
4415                             Intrinsic F32Int,
4416                             Intrinsic F64Int, bit Is2Addr = 1> {
4417   // Intrinsic operation, reg.
4418   def SSr : SS4AIi8<opcss, MRMSrcReg,
4419         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2, i32i8imm:$src3),
4420         !if(Is2Addr,
4421             !strconcat(OpcodeStr,
4422                 "ss\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4423             !strconcat(OpcodeStr,
4424                 "ss\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4425         [(set VR128:$dst, (F32Int VR128:$src1, VR128:$src2, imm:$src3))]>,
4426         OpSize;
4427
4428   // Intrinsic operation, mem.
4429   def SSm : SS4AIi8<opcss, MRMSrcMem,
4430         (outs VR128:$dst), (ins VR128:$src1, ssmem:$src2, i32i8imm:$src3),
4431         !if(Is2Addr,
4432             !strconcat(OpcodeStr,
4433                 "ss\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4434             !strconcat(OpcodeStr,
4435                 "ss\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4436         [(set VR128:$dst,
4437              (F32Int VR128:$src1, sse_load_f32:$src2, imm:$src3))]>,
4438         OpSize;
4439
4440   // Intrinsic operation, reg.
4441   def SDr : SS4AIi8<opcsd, MRMSrcReg,
4442         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2, i32i8imm:$src3),
4443         !if(Is2Addr,
4444             !strconcat(OpcodeStr,
4445                 "sd\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4446             !strconcat(OpcodeStr,
4447                 "sd\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4448         [(set VR128:$dst, (F64Int VR128:$src1, VR128:$src2, imm:$src3))]>,
4449         OpSize;
4450
4451   // Intrinsic operation, mem.
4452   def SDm : SS4AIi8<opcsd, MRMSrcMem,
4453         (outs VR128:$dst), (ins VR128:$src1, sdmem:$src2, i32i8imm:$src3),
4454         !if(Is2Addr,
4455             !strconcat(OpcodeStr,
4456                 "sd\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4457             !strconcat(OpcodeStr,
4458                 "sd\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4459         [(set VR128:$dst,
4460               (F64Int VR128:$src1, sse_load_f64:$src2, imm:$src3))]>,
4461         OpSize;
4462 }
4463
4464 multiclass sse41_fp_binop_rm_avx_s<bits<8> opcss, bits<8> opcsd,
4465                                    string OpcodeStr> {
4466   // Intrinsic operation, reg.
4467   def SSr_AVX : SS4AIi8<opcss, MRMSrcReg,
4468         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2, i32i8imm:$src3),
4469         !strconcat(OpcodeStr,
4470                 "ss\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}"),
4471         []>, OpSize;
4472
4473   // Intrinsic operation, mem.
4474   def SSm_AVX : SS4AIi8<opcss, MRMSrcMem,
4475         (outs VR128:$dst), (ins VR128:$src1, ssmem:$src2, i32i8imm:$src3),
4476         !strconcat(OpcodeStr,
4477                 "ss\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}"),
4478         []>, OpSize;
4479
4480   // Intrinsic operation, reg.
4481   def SDr_AVX : SS4AIi8<opcsd, MRMSrcReg,
4482         (outs VR128:$dst), (ins VR128:$src1, VR128:$src2, i32i8imm:$src3),
4483             !strconcat(OpcodeStr,
4484                 "sd\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}"),
4485         []>, OpSize;
4486
4487   // Intrinsic operation, mem.
4488   def SDm_AVX : SS4AIi8<opcsd, MRMSrcMem,
4489         (outs VR128:$dst), (ins VR128:$src1, sdmem:$src2, i32i8imm:$src3),
4490             !strconcat(OpcodeStr,
4491                 "sd\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}"),
4492         []>, OpSize;
4493 }
4494
4495 // FP round - roundss, roundps, roundsd, roundpd
4496 let Predicates = [HasAVX] in {
4497   // Intrinsic form
4498   defm VROUND  : sse41_fp_unop_rm<0x08, 0x09, "vround", f128mem, VR128,
4499                                   memopv4f32, memopv2f64,
4500                                   int_x86_sse41_round_ps,
4501                                   int_x86_sse41_round_pd>, VEX;
4502   defm VROUNDY : sse41_fp_unop_rm<0x08, 0x09, "vround", f256mem, VR256,
4503                                   memopv8f32, memopv4f64,
4504                                   int_x86_avx_round_ps_256,
4505                                   int_x86_avx_round_pd_256>, VEX;
4506   defm VROUND  : sse41_fp_binop_rm<0x0A, 0x0B, "vround",
4507                                   int_x86_sse41_round_ss,
4508                                   int_x86_sse41_round_sd, 0>, VEX_4V;
4509
4510   // Instructions for the assembler
4511   defm VROUND  : sse41_fp_unop_rm_avx_p<0x08, 0x09, VR128, f128mem, "vround">,
4512                                         VEX;
4513   defm VROUNDY : sse41_fp_unop_rm_avx_p<0x08, 0x09, VR256, f256mem, "vround">,
4514                                         VEX;
4515   defm VROUND  : sse41_fp_binop_rm_avx_s<0x0A, 0x0B, "vround">, VEX_4V;
4516 }
4517
4518 defm ROUND  : sse41_fp_unop_rm<0x08, 0x09, "round", f128mem, VR128,
4519                                memopv4f32, memopv2f64,
4520                                int_x86_sse41_round_ps, int_x86_sse41_round_pd>;
4521 let Constraints = "$src1 = $dst" in
4522 defm ROUND  : sse41_fp_binop_rm<0x0A, 0x0B, "round",
4523                                int_x86_sse41_round_ss, int_x86_sse41_round_sd>;
4524
4525 //===----------------------------------------------------------------------===//
4526 // SSE4.1 - Packed Bit Test
4527 //===----------------------------------------------------------------------===//
4528
4529 // ptest instruction we'll lower to this in X86ISelLowering primarily from
4530 // the intel intrinsic that corresponds to this.
4531 let Defs = [EFLAGS], Predicates = [HasAVX] in {
4532 def VPTESTrr  : SS48I<0x17, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
4533                 "vptest\t{$src2, $src1|$src1, $src2}",
4534                 [(set EFLAGS, (X86ptest VR128:$src1, (v4f32 VR128:$src2)))]>,
4535                 OpSize, VEX;
4536 def VPTESTrm  : SS48I<0x17, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
4537                 "vptest\t{$src2, $src1|$src1, $src2}",
4538                 [(set EFLAGS,(X86ptest VR128:$src1, (memopv4f32 addr:$src2)))]>,
4539                 OpSize, VEX;
4540
4541 def VPTESTYrr : SS48I<0x17, MRMSrcReg, (outs), (ins VR256:$src1, VR256:$src2),
4542                 "vptest\t{$src2, $src1|$src1, $src2}",
4543                 [(set EFLAGS, (X86ptest VR256:$src1, (v4i64 VR256:$src2)))]>,
4544                 OpSize, VEX;
4545 def VPTESTYrm : SS48I<0x17, MRMSrcMem, (outs), (ins VR256:$src1, i256mem:$src2),
4546                 "vptest\t{$src2, $src1|$src1, $src2}",
4547                 [(set EFLAGS,(X86ptest VR256:$src1, (memopv4i64 addr:$src2)))]>,
4548                 OpSize, VEX;
4549 }
4550
4551 let Defs = [EFLAGS] in {
4552 def PTESTrr : SS48I<0x17, MRMSrcReg, (outs), (ins VR128:$src1, VR128:$src2),
4553               "ptest \t{$src2, $src1|$src1, $src2}",
4554               [(set EFLAGS, (X86ptest VR128:$src1, (v4f32 VR128:$src2)))]>,
4555               OpSize;
4556 def PTESTrm : SS48I<0x17, MRMSrcMem, (outs), (ins VR128:$src1, f128mem:$src2),
4557               "ptest \t{$src2, $src1|$src1, $src2}",
4558               [(set EFLAGS, (X86ptest VR128:$src1, (memopv4f32 addr:$src2)))]>,
4559               OpSize;
4560 }
4561
4562 // The bit test instructions below are AVX only
4563 multiclass avx_bittest<bits<8> opc, string OpcodeStr, RegisterClass RC,
4564                        X86MemOperand x86memop, PatFrag mem_frag, ValueType vt> {
4565   def rr : SS48I<opc, MRMSrcReg, (outs), (ins RC:$src1, RC:$src2),
4566             !strconcat(OpcodeStr, "\t{$src2, $src1|$src1, $src2}"),
4567             [(set EFLAGS, (X86testp RC:$src1, (vt RC:$src2)))]>, OpSize, VEX;
4568   def rm : SS48I<opc, MRMSrcMem, (outs), (ins RC:$src1, x86memop:$src2),
4569             !strconcat(OpcodeStr, "\t{$src2, $src1|$src1, $src2}"),
4570             [(set EFLAGS, (X86testp RC:$src1, (mem_frag addr:$src2)))]>,
4571             OpSize, VEX;
4572 }
4573
4574 let Defs = [EFLAGS], Predicates = [HasAVX] in {
4575 defm VTESTPS  : avx_bittest<0x0E, "vtestps", VR128, f128mem, memopv4f32, v4f32>;
4576 defm VTESTPSY : avx_bittest<0x0E, "vtestps", VR256, f256mem, memopv8f32, v8f32>;
4577 defm VTESTPD  : avx_bittest<0x0F, "vtestpd", VR128, f128mem, memopv2f64, v2f64>;
4578 defm VTESTPDY : avx_bittest<0x0F, "vtestpd", VR256, f256mem, memopv4f64, v4f64>;
4579 }
4580
4581 //===----------------------------------------------------------------------===//
4582 // SSE4.1 - Misc Instructions
4583 //===----------------------------------------------------------------------===//
4584
4585 def POPCNT16rr : I<0xB8, MRMSrcReg, (outs GR16:$dst), (ins GR16:$src),
4586                    "popcnt{w}\t{$src, $dst|$dst, $src}",
4587                    [(set GR16:$dst, (ctpop GR16:$src))]>, OpSize, XS;
4588 def POPCNT16rm : I<0xB8, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
4589                    "popcnt{w}\t{$src, $dst|$dst, $src}",
4590                    [(set GR16:$dst, (ctpop (loadi16 addr:$src)))]>, OpSize, XS;
4591
4592 def POPCNT32rr : I<0xB8, MRMSrcReg, (outs GR32:$dst), (ins GR32:$src),
4593                    "popcnt{l}\t{$src, $dst|$dst, $src}",
4594                    [(set GR32:$dst, (ctpop GR32:$src))]>, XS;
4595 def POPCNT32rm : I<0xB8, MRMSrcMem, (outs GR32:$dst), (ins i32mem:$src),
4596                    "popcnt{l}\t{$src, $dst|$dst, $src}",
4597                    [(set GR32:$dst, (ctpop (loadi32 addr:$src)))]>, XS;
4598
4599 def POPCNT64rr : RI<0xB8, MRMSrcReg, (outs GR64:$dst), (ins GR64:$src),
4600                     "popcnt{q}\t{$src, $dst|$dst, $src}",
4601                     [(set GR64:$dst, (ctpop GR64:$src))]>, XS;
4602 def POPCNT64rm : RI<0xB8, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
4603                     "popcnt{q}\t{$src, $dst|$dst, $src}",
4604                     [(set GR64:$dst, (ctpop (loadi64 addr:$src)))]>, XS;
4605
4606
4607
4608 // SS41I_unop_rm_int_v16 - SSE 4.1 unary operator whose type is v8i16.
4609 multiclass SS41I_unop_rm_int_v16<bits<8> opc, string OpcodeStr,
4610                                  Intrinsic IntId128> {
4611   def rr128 : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
4612                     (ins VR128:$src),
4613                     !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
4614                     [(set VR128:$dst, (IntId128 VR128:$src))]>, OpSize;
4615   def rm128 : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
4616                      (ins i128mem:$src),
4617                      !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
4618                      [(set VR128:$dst,
4619                        (IntId128
4620                        (bitconvert (memopv8i16 addr:$src))))]>, OpSize;
4621 }
4622
4623 let Predicates = [HasAVX] in
4624 defm VPHMINPOSUW : SS41I_unop_rm_int_v16 <0x41, "vphminposuw",
4625                                          int_x86_sse41_phminposuw>, VEX;
4626 defm PHMINPOSUW : SS41I_unop_rm_int_v16 <0x41, "phminposuw",
4627                                          int_x86_sse41_phminposuw>;
4628
4629 /// SS41I_binop_rm_int - Simple SSE 4.1 binary operator
4630 multiclass SS41I_binop_rm_int<bits<8> opc, string OpcodeStr,
4631                               Intrinsic IntId128, bit Is2Addr = 1> {
4632   let isCommutable = 1 in
4633   def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
4634        (ins VR128:$src1, VR128:$src2),
4635        !if(Is2Addr,
4636            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
4637            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
4638        [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>, OpSize;
4639   def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
4640        (ins VR128:$src1, i128mem:$src2),
4641        !if(Is2Addr,
4642            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
4643            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
4644        [(set VR128:$dst,
4645          (IntId128 VR128:$src1,
4646           (bitconvert (memopv16i8 addr:$src2))))]>, OpSize;
4647 }
4648
4649 let Predicates = [HasAVX] in {
4650   let isCommutable = 0 in
4651   defm VPACKUSDW : SS41I_binop_rm_int<0x2B, "vpackusdw", int_x86_sse41_packusdw,
4652                                                          0>, VEX_4V;
4653   defm VPCMPEQQ  : SS41I_binop_rm_int<0x29, "vpcmpeqq",  int_x86_sse41_pcmpeqq,
4654                                                          0>, VEX_4V;
4655   defm VPMINSB   : SS41I_binop_rm_int<0x38, "vpminsb",   int_x86_sse41_pminsb,
4656                                                          0>, VEX_4V;
4657   defm VPMINSD   : SS41I_binop_rm_int<0x39, "vpminsd",   int_x86_sse41_pminsd,
4658                                                          0>, VEX_4V;
4659   defm VPMINUD   : SS41I_binop_rm_int<0x3B, "vpminud",   int_x86_sse41_pminud,
4660                                                          0>, VEX_4V;
4661   defm VPMINUW   : SS41I_binop_rm_int<0x3A, "vpminuw",   int_x86_sse41_pminuw,
4662                                                          0>, VEX_4V;
4663   defm VPMAXSB   : SS41I_binop_rm_int<0x3C, "vpmaxsb",   int_x86_sse41_pmaxsb,
4664                                                          0>, VEX_4V;
4665   defm VPMAXSD   : SS41I_binop_rm_int<0x3D, "vpmaxsd",   int_x86_sse41_pmaxsd,
4666                                                          0>, VEX_4V;
4667   defm VPMAXUD   : SS41I_binop_rm_int<0x3F, "vpmaxud",   int_x86_sse41_pmaxud,
4668                                                          0>, VEX_4V;
4669   defm VPMAXUW   : SS41I_binop_rm_int<0x3E, "vpmaxuw",   int_x86_sse41_pmaxuw,
4670                                                          0>, VEX_4V;
4671   defm VPMULDQ   : SS41I_binop_rm_int<0x28, "vpmuldq",   int_x86_sse41_pmuldq,
4672                                                          0>, VEX_4V;
4673 }
4674
4675 let Constraints = "$src1 = $dst" in {
4676   let isCommutable = 0 in
4677   defm PACKUSDW : SS41I_binop_rm_int<0x2B, "packusdw", int_x86_sse41_packusdw>;
4678   defm PCMPEQQ  : SS41I_binop_rm_int<0x29, "pcmpeqq",  int_x86_sse41_pcmpeqq>;
4679   defm PMINSB   : SS41I_binop_rm_int<0x38, "pminsb",   int_x86_sse41_pminsb>;
4680   defm PMINSD   : SS41I_binop_rm_int<0x39, "pminsd",   int_x86_sse41_pminsd>;
4681   defm PMINUD   : SS41I_binop_rm_int<0x3B, "pminud",   int_x86_sse41_pminud>;
4682   defm PMINUW   : SS41I_binop_rm_int<0x3A, "pminuw",   int_x86_sse41_pminuw>;
4683   defm PMAXSB   : SS41I_binop_rm_int<0x3C, "pmaxsb",   int_x86_sse41_pmaxsb>;
4684   defm PMAXSD   : SS41I_binop_rm_int<0x3D, "pmaxsd",   int_x86_sse41_pmaxsd>;
4685   defm PMAXUD   : SS41I_binop_rm_int<0x3F, "pmaxud",   int_x86_sse41_pmaxud>;
4686   defm PMAXUW   : SS41I_binop_rm_int<0x3E, "pmaxuw",   int_x86_sse41_pmaxuw>;
4687   defm PMULDQ   : SS41I_binop_rm_int<0x28, "pmuldq",   int_x86_sse41_pmuldq>;
4688 }
4689
4690 def : Pat<(v2i64 (X86pcmpeqq VR128:$src1, VR128:$src2)),
4691           (PCMPEQQrr VR128:$src1, VR128:$src2)>;
4692 def : Pat<(v2i64 (X86pcmpeqq VR128:$src1, (memop addr:$src2))),
4693           (PCMPEQQrm VR128:$src1, addr:$src2)>;
4694
4695 /// SS48I_binop_rm - Simple SSE41 binary operator.
4696 multiclass SS48I_binop_rm<bits<8> opc, string OpcodeStr, SDNode OpNode,
4697                         ValueType OpVT, bit Is2Addr = 1> {
4698   let isCommutable = 1 in
4699   def rr : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
4700        (ins VR128:$src1, VR128:$src2),
4701        !if(Is2Addr,
4702            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
4703            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
4704        [(set VR128:$dst, (OpVT (OpNode VR128:$src1, VR128:$src2)))]>,
4705        OpSize;
4706   def rm : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
4707        (ins VR128:$src1, i128mem:$src2),
4708        !if(Is2Addr,
4709            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
4710            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
4711        [(set VR128:$dst, (OpNode VR128:$src1,
4712                                   (bc_v4i32 (memopv2i64 addr:$src2))))]>,
4713        OpSize;
4714 }
4715
4716 let Predicates = [HasAVX] in
4717   defm VPMULLD : SS48I_binop_rm<0x40, "vpmulld", mul, v4i32, 0>, VEX_4V;
4718 let Constraints = "$src1 = $dst" in
4719   defm PMULLD : SS48I_binop_rm<0x40, "pmulld", mul, v4i32>;
4720
4721 /// SS41I_binop_rmi_int - SSE 4.1 binary operator with 8-bit immediate
4722 multiclass SS41I_binop_rmi_int<bits<8> opc, string OpcodeStr,
4723                  Intrinsic IntId, RegisterClass RC, PatFrag memop_frag,
4724                  X86MemOperand x86memop, bit Is2Addr = 1> {
4725   let isCommutable = 1 in
4726   def rri : SS4AIi8<opc, MRMSrcReg, (outs RC:$dst),
4727         (ins RC:$src1, RC:$src2, u32u8imm:$src3),
4728         !if(Is2Addr,
4729             !strconcat(OpcodeStr,
4730                 "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4731             !strconcat(OpcodeStr,
4732                 "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4733         [(set RC:$dst, (IntId RC:$src1, RC:$src2, imm:$src3))]>,
4734         OpSize;
4735   def rmi : SS4AIi8<opc, MRMSrcMem, (outs RC:$dst),
4736         (ins RC:$src1, x86memop:$src2, u32u8imm:$src3),
4737         !if(Is2Addr,
4738             !strconcat(OpcodeStr,
4739                 "\t{$src3, $src2, $dst|$dst, $src2, $src3}"),
4740             !strconcat(OpcodeStr,
4741                 "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}")),
4742         [(set RC:$dst,
4743           (IntId RC:$src1,
4744            (bitconvert (memop_frag addr:$src2)), imm:$src3))]>,
4745         OpSize;
4746 }
4747
4748 let Predicates = [HasAVX] in {
4749   let isCommutable = 0 in {
4750   defm VBLENDPS : SS41I_binop_rmi_int<0x0C, "vblendps", int_x86_sse41_blendps,
4751                                       VR128, memopv16i8, i128mem, 0>, VEX_4V;
4752   defm VBLENDPD : SS41I_binop_rmi_int<0x0D, "vblendpd", int_x86_sse41_blendpd,
4753                                       VR128, memopv16i8, i128mem, 0>, VEX_4V;
4754   defm VBLENDPSY : SS41I_binop_rmi_int<0x0C, "vblendps",
4755             int_x86_avx_blend_ps_256, VR256, memopv32i8, i256mem, 0>, VEX_4V;
4756   defm VBLENDPDY : SS41I_binop_rmi_int<0x0D, "vblendpd",
4757             int_x86_avx_blend_pd_256, VR256, memopv32i8, i256mem, 0>, VEX_4V;
4758   defm VPBLENDW : SS41I_binop_rmi_int<0x0E, "vpblendw", int_x86_sse41_pblendw,
4759                                       VR128, memopv16i8, i128mem, 0>, VEX_4V;
4760   defm VMPSADBW : SS41I_binop_rmi_int<0x42, "vmpsadbw", int_x86_sse41_mpsadbw,
4761                                       VR128, memopv16i8, i128mem, 0>, VEX_4V;
4762   }
4763   defm VDPPS : SS41I_binop_rmi_int<0x40, "vdpps", int_x86_sse41_dpps,
4764                                    VR128, memopv16i8, i128mem, 0>, VEX_4V;
4765   defm VDPPD : SS41I_binop_rmi_int<0x41, "vdppd", int_x86_sse41_dppd,
4766                                    VR128, memopv16i8, i128mem, 0>, VEX_4V;
4767   defm VDPPSY : SS41I_binop_rmi_int<0x40, "vdpps", int_x86_avx_dp_ps_256,
4768                                    VR256, memopv32i8, i256mem, 0>, VEX_4V;
4769 }
4770
4771 let Constraints = "$src1 = $dst" in {
4772   let isCommutable = 0 in {
4773   defm BLENDPS : SS41I_binop_rmi_int<0x0C, "blendps", int_x86_sse41_blendps,
4774                                      VR128, memopv16i8, i128mem>;
4775   defm BLENDPD : SS41I_binop_rmi_int<0x0D, "blendpd", int_x86_sse41_blendpd,
4776                                      VR128, memopv16i8, i128mem>;
4777   defm PBLENDW : SS41I_binop_rmi_int<0x0E, "pblendw", int_x86_sse41_pblendw,
4778                                      VR128, memopv16i8, i128mem>;
4779   defm MPSADBW : SS41I_binop_rmi_int<0x42, "mpsadbw", int_x86_sse41_mpsadbw,
4780                                      VR128, memopv16i8, i128mem>;
4781   }
4782   defm DPPS : SS41I_binop_rmi_int<0x40, "dpps", int_x86_sse41_dpps,
4783                                   VR128, memopv16i8, i128mem>;
4784   defm DPPD : SS41I_binop_rmi_int<0x41, "dppd", int_x86_sse41_dppd,
4785                                   VR128, memopv16i8, i128mem>;
4786 }
4787
4788 /// SS41I_quaternary_int_avx - AVX SSE 4.1 with 4 operators
4789 let Predicates = [HasAVX] in {
4790 multiclass SS41I_quaternary_int_avx<bits<8> opc, string OpcodeStr,
4791                                     RegisterClass RC, X86MemOperand x86memop,
4792                                     PatFrag mem_frag, Intrinsic IntId> {
4793   def rr : I<opc, MRMSrcReg, (outs RC:$dst),
4794                   (ins RC:$src1, RC:$src2, RC:$src3),
4795                   !strconcat(OpcodeStr,
4796                     "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}"),
4797                   [(set RC:$dst, (IntId RC:$src1, RC:$src2, RC:$src3))],
4798                   SSEPackedInt>, OpSize, TA, VEX_4V, VEX_I8IMM;
4799
4800   def rm : I<opc, MRMSrcMem, (outs RC:$dst),
4801                   (ins RC:$src1, x86memop:$src2, RC:$src3),
4802                   !strconcat(OpcodeStr,
4803                     "\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}"),
4804                   [(set RC:$dst,
4805                         (IntId RC:$src1, (bitconvert (mem_frag addr:$src2)),
4806                                RC:$src3))],
4807                   SSEPackedInt>, OpSize, TA, VEX_4V, VEX_I8IMM;
4808 }
4809 }
4810
4811 defm VBLENDVPD  : SS41I_quaternary_int_avx<0x4B, "vblendvpd", VR128, i128mem,
4812                                            memopv16i8, int_x86_sse41_blendvpd>;
4813 defm VBLENDVPS  : SS41I_quaternary_int_avx<0x4A, "vblendvps", VR128, i128mem,
4814                                            memopv16i8, int_x86_sse41_blendvps>;
4815 defm VPBLENDVB  : SS41I_quaternary_int_avx<0x4C, "vpblendvb", VR128, i128mem,
4816                                            memopv16i8, int_x86_sse41_pblendvb>;
4817 defm VBLENDVPDY : SS41I_quaternary_int_avx<0x4B, "vblendvpd", VR256, i256mem,
4818                                          memopv32i8, int_x86_avx_blendv_pd_256>;
4819 defm VBLENDVPSY : SS41I_quaternary_int_avx<0x4A, "vblendvps", VR256, i256mem,
4820                                          memopv32i8, int_x86_avx_blendv_ps_256>;
4821
4822 /// SS41I_ternary_int - SSE 4.1 ternary operator
4823 let Uses = [XMM0], Constraints = "$src1 = $dst" in {
4824   multiclass SS41I_ternary_int<bits<8> opc, string OpcodeStr, Intrinsic IntId> {
4825     def rr0 : SS48I<opc, MRMSrcReg, (outs VR128:$dst),
4826                     (ins VR128:$src1, VR128:$src2),
4827                     !strconcat(OpcodeStr,
4828                      "\t{$src2, $dst|$dst, $src2}"),
4829                     [(set VR128:$dst, (IntId VR128:$src1, VR128:$src2, XMM0))]>,
4830                     OpSize;
4831
4832     def rm0 : SS48I<opc, MRMSrcMem, (outs VR128:$dst),
4833                     (ins VR128:$src1, i128mem:$src2),
4834                     !strconcat(OpcodeStr,
4835                      "\t{$src2, $dst|$dst, $src2}"),
4836                     [(set VR128:$dst,
4837                       (IntId VR128:$src1,
4838                        (bitconvert (memopv16i8 addr:$src2)), XMM0))]>, OpSize;
4839   }
4840 }
4841
4842 defm BLENDVPD     : SS41I_ternary_int<0x15, "blendvpd", int_x86_sse41_blendvpd>;
4843 defm BLENDVPS     : SS41I_ternary_int<0x14, "blendvps", int_x86_sse41_blendvps>;
4844 defm PBLENDVB     : SS41I_ternary_int<0x10, "pblendvb", int_x86_sse41_pblendvb>;
4845
4846 def : Pat<(X86pblendv VR128:$src1, VR128:$src2, XMM0),
4847           (PBLENDVBrr0 VR128:$src1, VR128:$src2)>;
4848
4849 let Predicates = [HasAVX] in
4850 def VMOVNTDQArm : SS48I<0x2A, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
4851                        "vmovntdqa\t{$src, $dst|$dst, $src}",
4852                        [(set VR128:$dst, (int_x86_sse41_movntdqa addr:$src))]>,
4853                        OpSize, VEX;
4854 def MOVNTDQArm : SS48I<0x2A, MRMSrcMem, (outs VR128:$dst), (ins i128mem:$src),
4855                        "movntdqa\t{$src, $dst|$dst, $src}",
4856                        [(set VR128:$dst, (int_x86_sse41_movntdqa addr:$src))]>,
4857                        OpSize;
4858
4859 //===----------------------------------------------------------------------===//
4860 // SSE4.2 - Compare Instructions
4861 //===----------------------------------------------------------------------===//
4862
4863 /// SS42I_binop_rm_int - Simple SSE 4.2 binary operator
4864 multiclass SS42I_binop_rm_int<bits<8> opc, string OpcodeStr,
4865                               Intrinsic IntId128, bit Is2Addr = 1> {
4866   def rr : SS428I<opc, MRMSrcReg, (outs VR128:$dst),
4867        (ins VR128:$src1, VR128:$src2),
4868        !if(Is2Addr,
4869            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
4870            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
4871        [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
4872        OpSize;
4873   def rm : SS428I<opc, MRMSrcMem, (outs VR128:$dst),
4874        (ins VR128:$src1, i128mem:$src2),
4875        !if(Is2Addr,
4876            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
4877            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
4878        [(set VR128:$dst,
4879          (IntId128 VR128:$src1,
4880           (bitconvert (memopv16i8 addr:$src2))))]>, OpSize;
4881 }
4882
4883 let Predicates = [HasAVX] in
4884   defm VPCMPGTQ : SS42I_binop_rm_int<0x37, "vpcmpgtq", int_x86_sse42_pcmpgtq,
4885                                      0>, VEX_4V;
4886 let Constraints = "$src1 = $dst" in
4887   defm PCMPGTQ : SS42I_binop_rm_int<0x37, "pcmpgtq", int_x86_sse42_pcmpgtq>;
4888
4889 def : Pat<(v2i64 (X86pcmpgtq VR128:$src1, VR128:$src2)),
4890           (PCMPGTQrr VR128:$src1, VR128:$src2)>;
4891 def : Pat<(v2i64 (X86pcmpgtq VR128:$src1, (memop addr:$src2))),
4892           (PCMPGTQrm VR128:$src1, addr:$src2)>;
4893
4894 //===----------------------------------------------------------------------===//
4895 // SSE4.2 - String/text Processing Instructions
4896 //===----------------------------------------------------------------------===//
4897
4898 // Packed Compare Implicit Length Strings, Return Mask
4899 multiclass pseudo_pcmpistrm<string asm> {
4900   def REG : PseudoI<(outs VR128:$dst),
4901                     (ins VR128:$src1, VR128:$src2, i8imm:$src3),
4902     [(set VR128:$dst, (int_x86_sse42_pcmpistrm128 VR128:$src1, VR128:$src2,
4903                                                   imm:$src3))]>;
4904   def MEM : PseudoI<(outs VR128:$dst),
4905                     (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
4906     [(set VR128:$dst, (int_x86_sse42_pcmpistrm128
4907                        VR128:$src1, (load addr:$src2), imm:$src3))]>;
4908 }
4909
4910 let Defs = [EFLAGS], usesCustomInserter = 1 in {
4911   defm PCMPISTRM128 : pseudo_pcmpistrm<"#PCMPISTRM128">, Requires<[HasSSE42]>;
4912   defm VPCMPISTRM128 : pseudo_pcmpistrm<"#VPCMPISTRM128">, Requires<[HasAVX]>;
4913 }
4914
4915 let Defs = [XMM0, EFLAGS], Predicates = [HasAVX] in {
4916   def VPCMPISTRM128rr : SS42AI<0x62, MRMSrcReg, (outs),
4917       (ins VR128:$src1, VR128:$src2, i8imm:$src3),
4918       "vpcmpistrm\t{$src3, $src2, $src1|$src1, $src2, $src3}", []>, OpSize, VEX;
4919   def VPCMPISTRM128rm : SS42AI<0x62, MRMSrcMem, (outs),
4920       (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
4921       "vpcmpistrm\t{$src3, $src2, $src1|$src1, $src2, $src3}", []>, OpSize, VEX;
4922 }
4923
4924 let Defs = [XMM0, EFLAGS] in {
4925   def PCMPISTRM128rr : SS42AI<0x62, MRMSrcReg, (outs),
4926       (ins VR128:$src1, VR128:$src2, i8imm:$src3),
4927       "pcmpistrm\t{$src3, $src2, $src1|$src1, $src2, $src3}", []>, OpSize;
4928   def PCMPISTRM128rm : SS42AI<0x62, MRMSrcMem, (outs),
4929       (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
4930       "pcmpistrm\t{$src3, $src2, $src1|$src1, $src2, $src3}", []>, OpSize;
4931 }
4932
4933 // Packed Compare Explicit Length Strings, Return Mask
4934 multiclass pseudo_pcmpestrm<string asm> {
4935   def REG : PseudoI<(outs VR128:$dst),
4936                     (ins VR128:$src1, VR128:$src3, i8imm:$src5),
4937     [(set VR128:$dst, (int_x86_sse42_pcmpestrm128
4938                        VR128:$src1, EAX, VR128:$src3, EDX, imm:$src5))]>;
4939   def MEM : PseudoI<(outs VR128:$dst),
4940                     (ins VR128:$src1, i128mem:$src3, i8imm:$src5),
4941     [(set VR128:$dst, (int_x86_sse42_pcmpestrm128
4942                        VR128:$src1, EAX, (load addr:$src3), EDX, imm:$src5))]>;
4943 }
4944
4945 let Defs = [EFLAGS], Uses = [EAX, EDX], usesCustomInserter = 1 in {
4946   defm PCMPESTRM128 : pseudo_pcmpestrm<"#PCMPESTRM128">, Requires<[HasSSE42]>;
4947   defm VPCMPESTRM128 : pseudo_pcmpestrm<"#VPCMPESTRM128">, Requires<[HasAVX]>;
4948 }
4949
4950 let Predicates = [HasAVX],
4951     Defs = [XMM0, EFLAGS], Uses = [EAX, EDX] in {
4952   def VPCMPESTRM128rr : SS42AI<0x60, MRMSrcReg, (outs),
4953       (ins VR128:$src1, VR128:$src3, i8imm:$src5),
4954       "vpcmpestrm\t{$src5, $src3, $src1|$src1, $src3, $src5}", []>, OpSize, VEX;
4955   def VPCMPESTRM128rm : SS42AI<0x60, MRMSrcMem, (outs),
4956       (ins VR128:$src1, i128mem:$src3, i8imm:$src5),
4957       "vpcmpestrm\t{$src5, $src3, $src1|$src1, $src3, $src5}", []>, OpSize, VEX;
4958 }
4959
4960 let Defs = [XMM0, EFLAGS], Uses = [EAX, EDX] in {
4961   def PCMPESTRM128rr : SS42AI<0x60, MRMSrcReg, (outs),
4962       (ins VR128:$src1, VR128:$src3, i8imm:$src5),
4963       "pcmpestrm\t{$src5, $src3, $src1|$src1, $src3, $src5}", []>, OpSize;
4964   def PCMPESTRM128rm : SS42AI<0x60, MRMSrcMem, (outs),
4965       (ins VR128:$src1, i128mem:$src3, i8imm:$src5),
4966       "pcmpestrm\t{$src5, $src3, $src1|$src1, $src3, $src5}", []>, OpSize;
4967 }
4968
4969 // Packed Compare Implicit Length Strings, Return Index
4970 let Defs = [ECX, EFLAGS] in {
4971   multiclass SS42AI_pcmpistri<Intrinsic IntId128, string asm = "pcmpistri"> {
4972     def rr : SS42AI<0x63, MRMSrcReg, (outs),
4973       (ins VR128:$src1, VR128:$src2, i8imm:$src3),
4974       !strconcat(asm, "\t{$src3, $src2, $src1|$src1, $src2, $src3}"),
4975       [(set ECX, (IntId128 VR128:$src1, VR128:$src2, imm:$src3)),
4976        (implicit EFLAGS)]>, OpSize;
4977     def rm : SS42AI<0x63, MRMSrcMem, (outs),
4978       (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
4979       !strconcat(asm, "\t{$src3, $src2, $src1|$src1, $src2, $src3}"),
4980       [(set ECX, (IntId128 VR128:$src1, (load addr:$src2), imm:$src3)),
4981        (implicit EFLAGS)]>, OpSize;
4982   }
4983 }
4984
4985 let Predicates = [HasAVX] in {
4986 defm VPCMPISTRI  : SS42AI_pcmpistri<int_x86_sse42_pcmpistri128, "vpcmpistri">,
4987                                     VEX;
4988 defm VPCMPISTRIA : SS42AI_pcmpistri<int_x86_sse42_pcmpistria128, "vpcmpistri">,
4989                                     VEX;
4990 defm VPCMPISTRIC : SS42AI_pcmpistri<int_x86_sse42_pcmpistric128, "vpcmpistri">,
4991                                     VEX;
4992 defm VPCMPISTRIO : SS42AI_pcmpistri<int_x86_sse42_pcmpistrio128, "vpcmpistri">,
4993                                     VEX;
4994 defm VPCMPISTRIS : SS42AI_pcmpistri<int_x86_sse42_pcmpistris128, "vpcmpistri">,
4995                                     VEX;
4996 defm VPCMPISTRIZ : SS42AI_pcmpistri<int_x86_sse42_pcmpistriz128, "vpcmpistri">,
4997                                     VEX;
4998 }
4999
5000 defm PCMPISTRI  : SS42AI_pcmpistri<int_x86_sse42_pcmpistri128>;
5001 defm PCMPISTRIA : SS42AI_pcmpistri<int_x86_sse42_pcmpistria128>;
5002 defm PCMPISTRIC : SS42AI_pcmpistri<int_x86_sse42_pcmpistric128>;
5003 defm PCMPISTRIO : SS42AI_pcmpistri<int_x86_sse42_pcmpistrio128>;
5004 defm PCMPISTRIS : SS42AI_pcmpistri<int_x86_sse42_pcmpistris128>;
5005 defm PCMPISTRIZ : SS42AI_pcmpistri<int_x86_sse42_pcmpistriz128>;
5006
5007 // Packed Compare Explicit Length Strings, Return Index
5008 let Defs = [ECX, EFLAGS], Uses = [EAX, EDX] in {
5009   multiclass SS42AI_pcmpestri<Intrinsic IntId128, string asm = "pcmpestri"> {
5010     def rr : SS42AI<0x61, MRMSrcReg, (outs),
5011       (ins VR128:$src1, VR128:$src3, i8imm:$src5),
5012       !strconcat(asm, "\t{$src5, $src3, $src1|$src1, $src3, $src5}"),
5013       [(set ECX, (IntId128 VR128:$src1, EAX, VR128:$src3, EDX, imm:$src5)),
5014        (implicit EFLAGS)]>, OpSize;
5015     def rm : SS42AI<0x61, MRMSrcMem, (outs),
5016       (ins VR128:$src1, i128mem:$src3, i8imm:$src5),
5017       !strconcat(asm, "\t{$src5, $src3, $src1|$src1, $src3, $src5}"),
5018        [(set ECX,
5019              (IntId128 VR128:$src1, EAX, (load addr:$src3), EDX, imm:$src5)),
5020         (implicit EFLAGS)]>, OpSize;
5021   }
5022 }
5023
5024 let Predicates = [HasAVX] in {
5025 defm VPCMPESTRI  : SS42AI_pcmpestri<int_x86_sse42_pcmpestri128, "vpcmpestri">,
5026                                     VEX;
5027 defm VPCMPESTRIA : SS42AI_pcmpestri<int_x86_sse42_pcmpestria128, "vpcmpestri">,
5028                                     VEX;
5029 defm VPCMPESTRIC : SS42AI_pcmpestri<int_x86_sse42_pcmpestric128, "vpcmpestri">,
5030                                     VEX;
5031 defm VPCMPESTRIO : SS42AI_pcmpestri<int_x86_sse42_pcmpestrio128, "vpcmpestri">,
5032                                     VEX;
5033 defm VPCMPESTRIS : SS42AI_pcmpestri<int_x86_sse42_pcmpestris128, "vpcmpestri">,
5034                                     VEX;
5035 defm VPCMPESTRIZ : SS42AI_pcmpestri<int_x86_sse42_pcmpestriz128, "vpcmpestri">,
5036                                     VEX;
5037 }
5038
5039 defm PCMPESTRI  : SS42AI_pcmpestri<int_x86_sse42_pcmpestri128>;
5040 defm PCMPESTRIA : SS42AI_pcmpestri<int_x86_sse42_pcmpestria128>;
5041 defm PCMPESTRIC : SS42AI_pcmpestri<int_x86_sse42_pcmpestric128>;
5042 defm PCMPESTRIO : SS42AI_pcmpestri<int_x86_sse42_pcmpestrio128>;
5043 defm PCMPESTRIS : SS42AI_pcmpestri<int_x86_sse42_pcmpestris128>;
5044 defm PCMPESTRIZ : SS42AI_pcmpestri<int_x86_sse42_pcmpestriz128>;
5045
5046 //===----------------------------------------------------------------------===//
5047 // SSE4.2 - CRC Instructions
5048 //===----------------------------------------------------------------------===//
5049
5050 // No CRC instructions have AVX equivalents
5051
5052 // crc intrinsic instruction
5053 // This set of instructions are only rm, the only difference is the size
5054 // of r and m.
5055 let Constraints = "$src1 = $dst" in {
5056   def CRC32r32m8  : SS42FI<0xF0, MRMSrcMem, (outs GR32:$dst),
5057                       (ins GR32:$src1, i8mem:$src2),
5058                       "crc32{b} \t{$src2, $src1|$src1, $src2}",
5059                        [(set GR32:$dst,
5060                          (int_x86_sse42_crc32_32_8 GR32:$src1,
5061                          (load addr:$src2)))]>;
5062   def CRC32r32r8  : SS42FI<0xF0, MRMSrcReg, (outs GR32:$dst),
5063                       (ins GR32:$src1, GR8:$src2),
5064                       "crc32{b} \t{$src2, $src1|$src1, $src2}",
5065                        [(set GR32:$dst,
5066                          (int_x86_sse42_crc32_32_8 GR32:$src1, GR8:$src2))]>;
5067   def CRC32r32m16  : SS42FI<0xF1, MRMSrcMem, (outs GR32:$dst),
5068                       (ins GR32:$src1, i16mem:$src2),
5069                       "crc32{w} \t{$src2, $src1|$src1, $src2}",
5070                        [(set GR32:$dst,
5071                          (int_x86_sse42_crc32_32_16 GR32:$src1,
5072                          (load addr:$src2)))]>,
5073                          OpSize;
5074   def CRC32r32r16  : SS42FI<0xF1, MRMSrcReg, (outs GR32:$dst),
5075                       (ins GR32:$src1, GR16:$src2),
5076                       "crc32{w} \t{$src2, $src1|$src1, $src2}",
5077                        [(set GR32:$dst,
5078                          (int_x86_sse42_crc32_32_16 GR32:$src1, GR16:$src2))]>,
5079                          OpSize;
5080   def CRC32r32m32  : SS42FI<0xF1, MRMSrcMem, (outs GR32:$dst),
5081                       (ins GR32:$src1, i32mem:$src2),
5082                       "crc32{l} \t{$src2, $src1|$src1, $src2}",
5083                        [(set GR32:$dst,
5084                          (int_x86_sse42_crc32_32_32 GR32:$src1,
5085                          (load addr:$src2)))]>;
5086   def CRC32r32r32  : SS42FI<0xF1, MRMSrcReg, (outs GR32:$dst),
5087                       (ins GR32:$src1, GR32:$src2),
5088                       "crc32{l} \t{$src2, $src1|$src1, $src2}",
5089                        [(set GR32:$dst,
5090                          (int_x86_sse42_crc32_32_32 GR32:$src1, GR32:$src2))]>;
5091   def CRC32r64m8  : SS42FI<0xF0, MRMSrcMem, (outs GR64:$dst),
5092                       (ins GR64:$src1, i8mem:$src2),
5093                       "crc32{b} \t{$src2, $src1|$src1, $src2}",
5094                        [(set GR64:$dst,
5095                          (int_x86_sse42_crc32_64_8 GR64:$src1,
5096                          (load addr:$src2)))]>,
5097                          REX_W;
5098   def CRC32r64r8  : SS42FI<0xF0, MRMSrcReg, (outs GR64:$dst),
5099                       (ins GR64:$src1, GR8:$src2),
5100                       "crc32{b} \t{$src2, $src1|$src1, $src2}",
5101                        [(set GR64:$dst,
5102                          (int_x86_sse42_crc32_64_8 GR64:$src1, GR8:$src2))]>,
5103                          REX_W;
5104   def CRC32r64m64  : SS42FI<0xF1, MRMSrcMem, (outs GR64:$dst),
5105                       (ins GR64:$src1, i64mem:$src2),
5106                       "crc32{q} \t{$src2, $src1|$src1, $src2}",
5107                        [(set GR64:$dst,
5108                          (int_x86_sse42_crc32_64_64 GR64:$src1,
5109                          (load addr:$src2)))]>,
5110                          REX_W;
5111   def CRC32r64r64  : SS42FI<0xF1, MRMSrcReg, (outs GR64:$dst),
5112                       (ins GR64:$src1, GR64:$src2),
5113                       "crc32{q} \t{$src2, $src1|$src1, $src2}",
5114                        [(set GR64:$dst,
5115                          (int_x86_sse42_crc32_64_64 GR64:$src1, GR64:$src2))]>,
5116                          REX_W;
5117 }
5118
5119 //===----------------------------------------------------------------------===//
5120 // AES-NI Instructions
5121 //===----------------------------------------------------------------------===//
5122
5123 multiclass AESI_binop_rm_int<bits<8> opc, string OpcodeStr,
5124                               Intrinsic IntId128, bit Is2Addr = 1> {
5125   def rr : AES8I<opc, MRMSrcReg, (outs VR128:$dst),
5126        (ins VR128:$src1, VR128:$src2),
5127        !if(Is2Addr,
5128            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
5129            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
5130        [(set VR128:$dst, (IntId128 VR128:$src1, VR128:$src2))]>,
5131        OpSize;
5132   def rm : AES8I<opc, MRMSrcMem, (outs VR128:$dst),
5133        (ins VR128:$src1, i128mem:$src2),
5134        !if(Is2Addr,
5135            !strconcat(OpcodeStr, "\t{$src2, $dst|$dst, $src2}"),
5136            !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}")),
5137        [(set VR128:$dst,
5138          (IntId128 VR128:$src1,
5139           (bitconvert (memopv16i8 addr:$src2))))]>, OpSize;
5140 }
5141
5142 // Perform One Round of an AES Encryption/Decryption Flow
5143 let Predicates = [HasAVX, HasAES] in {
5144   defm VAESENC          : AESI_binop_rm_int<0xDC, "vaesenc",
5145                          int_x86_aesni_aesenc, 0>, VEX_4V;
5146   defm VAESENCLAST      : AESI_binop_rm_int<0xDD, "vaesenclast",
5147                          int_x86_aesni_aesenclast, 0>, VEX_4V;
5148   defm VAESDEC          : AESI_binop_rm_int<0xDE, "vaesdec",
5149                          int_x86_aesni_aesdec, 0>, VEX_4V;
5150   defm VAESDECLAST      : AESI_binop_rm_int<0xDF, "vaesdeclast",
5151                          int_x86_aesni_aesdeclast, 0>, VEX_4V;
5152 }
5153
5154 let Constraints = "$src1 = $dst" in {
5155   defm AESENC          : AESI_binop_rm_int<0xDC, "aesenc",
5156                          int_x86_aesni_aesenc>;
5157   defm AESENCLAST      : AESI_binop_rm_int<0xDD, "aesenclast",
5158                          int_x86_aesni_aesenclast>;
5159   defm AESDEC          : AESI_binop_rm_int<0xDE, "aesdec",
5160                          int_x86_aesni_aesdec>;
5161   defm AESDECLAST      : AESI_binop_rm_int<0xDF, "aesdeclast",
5162                          int_x86_aesni_aesdeclast>;
5163 }
5164
5165 def : Pat<(v2i64 (int_x86_aesni_aesenc VR128:$src1, VR128:$src2)),
5166           (AESENCrr VR128:$src1, VR128:$src2)>;
5167 def : Pat<(v2i64 (int_x86_aesni_aesenc VR128:$src1, (memop addr:$src2))),
5168           (AESENCrm VR128:$src1, addr:$src2)>;
5169 def : Pat<(v2i64 (int_x86_aesni_aesenclast VR128:$src1, VR128:$src2)),
5170           (AESENCLASTrr VR128:$src1, VR128:$src2)>;
5171 def : Pat<(v2i64 (int_x86_aesni_aesenclast VR128:$src1, (memop addr:$src2))),
5172           (AESENCLASTrm VR128:$src1, addr:$src2)>;
5173 def : Pat<(v2i64 (int_x86_aesni_aesdec VR128:$src1, VR128:$src2)),
5174           (AESDECrr VR128:$src1, VR128:$src2)>;
5175 def : Pat<(v2i64 (int_x86_aesni_aesdec VR128:$src1, (memop addr:$src2))),
5176           (AESDECrm VR128:$src1, addr:$src2)>;
5177 def : Pat<(v2i64 (int_x86_aesni_aesdeclast VR128:$src1, VR128:$src2)),
5178           (AESDECLASTrr VR128:$src1, VR128:$src2)>;
5179 def : Pat<(v2i64 (int_x86_aesni_aesdeclast VR128:$src1, (memop addr:$src2))),
5180           (AESDECLASTrm VR128:$src1, addr:$src2)>;
5181
5182 // Perform the AES InvMixColumn Transformation
5183 let Predicates = [HasAVX, HasAES] in {
5184   def VAESIMCrr : AES8I<0xDB, MRMSrcReg, (outs VR128:$dst),
5185       (ins VR128:$src1),
5186       "vaesimc\t{$src1, $dst|$dst, $src1}",
5187       [(set VR128:$dst,
5188         (int_x86_aesni_aesimc VR128:$src1))]>,
5189       OpSize, VEX;
5190   def VAESIMCrm : AES8I<0xDB, MRMSrcMem, (outs VR128:$dst),
5191       (ins i128mem:$src1),
5192       "vaesimc\t{$src1, $dst|$dst, $src1}",
5193       [(set VR128:$dst,
5194         (int_x86_aesni_aesimc (bitconvert (memopv2i64 addr:$src1))))]>,
5195       OpSize, VEX;
5196 }
5197 def AESIMCrr : AES8I<0xDB, MRMSrcReg, (outs VR128:$dst),
5198   (ins VR128:$src1),
5199   "aesimc\t{$src1, $dst|$dst, $src1}",
5200   [(set VR128:$dst,
5201     (int_x86_aesni_aesimc VR128:$src1))]>,
5202   OpSize;
5203 def AESIMCrm : AES8I<0xDB, MRMSrcMem, (outs VR128:$dst),
5204   (ins i128mem:$src1),
5205   "aesimc\t{$src1, $dst|$dst, $src1}",
5206   [(set VR128:$dst,
5207     (int_x86_aesni_aesimc (bitconvert (memopv2i64 addr:$src1))))]>,
5208   OpSize;
5209
5210 // AES Round Key Generation Assist
5211 let Predicates = [HasAVX, HasAES] in {
5212   def VAESKEYGENASSIST128rr : AESAI<0xDF, MRMSrcReg, (outs VR128:$dst),
5213       (ins VR128:$src1, i8imm:$src2),
5214       "vaeskeygenassist\t{$src2, $src1, $dst|$dst, $src1, $src2}",
5215       [(set VR128:$dst,
5216         (int_x86_aesni_aeskeygenassist VR128:$src1, imm:$src2))]>,
5217       OpSize, VEX;
5218   def VAESKEYGENASSIST128rm : AESAI<0xDF, MRMSrcMem, (outs VR128:$dst),
5219       (ins i128mem:$src1, i8imm:$src2),
5220       "vaeskeygenassist\t{$src2, $src1, $dst|$dst, $src1, $src2}",
5221       [(set VR128:$dst,
5222         (int_x86_aesni_aeskeygenassist (bitconvert (memopv2i64 addr:$src1)),
5223                                         imm:$src2))]>,
5224       OpSize, VEX;
5225 }
5226 def AESKEYGENASSIST128rr : AESAI<0xDF, MRMSrcReg, (outs VR128:$dst),
5227   (ins VR128:$src1, i8imm:$src2),
5228   "aeskeygenassist\t{$src2, $src1, $dst|$dst, $src1, $src2}",
5229   [(set VR128:$dst,
5230     (int_x86_aesni_aeskeygenassist VR128:$src1, imm:$src2))]>,
5231   OpSize;
5232 def AESKEYGENASSIST128rm : AESAI<0xDF, MRMSrcMem, (outs VR128:$dst),
5233   (ins i128mem:$src1, i8imm:$src2),
5234   "aeskeygenassist\t{$src2, $src1, $dst|$dst, $src1, $src2}",
5235   [(set VR128:$dst,
5236     (int_x86_aesni_aeskeygenassist (bitconvert (memopv2i64 addr:$src1)),
5237                                     imm:$src2))]>,
5238   OpSize;
5239
5240 //===----------------------------------------------------------------------===//
5241 // CLMUL Instructions
5242 //===----------------------------------------------------------------------===//
5243
5244 // Carry-less Multiplication instructions
5245 let Constraints = "$src1 = $dst" in {
5246 def PCLMULQDQrr : CLMULIi8<0x44, MRMSrcReg, (outs VR128:$dst),
5247            (ins VR128:$src1, VR128:$src2, i8imm:$src3),
5248            "pclmulqdq\t{$src3, $src2, $dst|$dst, $src2, $src3}",
5249            []>;
5250
5251 def PCLMULQDQrm : CLMULIi8<0x44, MRMSrcMem, (outs VR128:$dst),
5252            (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
5253            "pclmulqdq\t{$src3, $src2, $dst|$dst, $src2, $src3}",
5254            []>;
5255 }
5256
5257 // AVX carry-less Multiplication instructions
5258 def VPCLMULQDQrr : AVXCLMULIi8<0x44, MRMSrcReg, (outs VR128:$dst),
5259            (ins VR128:$src1, VR128:$src2, i8imm:$src3),
5260            "vpclmulqdq\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",
5261            []>;
5262
5263 def VPCLMULQDQrm : AVXCLMULIi8<0x44, MRMSrcMem, (outs VR128:$dst),
5264            (ins VR128:$src1, i128mem:$src2, i8imm:$src3),
5265            "vpclmulqdq\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",
5266            []>;
5267
5268
5269 multiclass pclmul_alias<string asm, int immop> {
5270   def : InstAlias<!strconcat("pclmul", asm, 
5271                            "dq {$src, $dst|$dst, $src}"),
5272                   (PCLMULQDQrr VR128:$dst, VR128:$src, immop)>;
5273
5274   def : InstAlias<!strconcat("pclmul", asm, 
5275                              "dq {$src, $dst|$dst, $src}"),
5276                   (PCLMULQDQrm VR128:$dst, i128mem:$src, immop)>;
5277
5278   def : InstAlias<!strconcat("vpclmul", asm, 
5279                              "dq {$src2, $src1, $dst|$dst, $src1, $src2}"),
5280                   (VPCLMULQDQrr VR128:$dst, VR128:$src1, VR128:$src2, immop)>;
5281
5282   def : InstAlias<!strconcat("vpclmul", asm, 
5283                              "dq {$src2, $src1, $dst|$dst, $src1, $src2}"),
5284                   (VPCLMULQDQrm VR128:$dst, VR128:$src1, i128mem:$src2, immop)>;
5285 }
5286 defm : pclmul_alias<"hqhq", 0x11>;
5287 defm : pclmul_alias<"hqlq", 0x01>;
5288 defm : pclmul_alias<"lqhq", 0x10>;
5289 defm : pclmul_alias<"lqlq", 0x00>;
5290
5291 //===----------------------------------------------------------------------===//
5292 // AVX Instructions
5293 //===----------------------------------------------------------------------===//
5294
5295 //===----------------------------------------------------------------------===//
5296 // VBROADCAST - Load from memory and broadcast to all elements of the
5297 //              destination operand
5298 //
5299 class avx_broadcast<bits<8> opc, string OpcodeStr, RegisterClass RC,
5300                     X86MemOperand x86memop, Intrinsic Int> :
5301   AVX8I<opc, MRMSrcMem, (outs RC:$dst), (ins x86memop:$src),
5302         !strconcat(OpcodeStr, "\t{$src, $dst|$dst, $src}"),
5303         [(set RC:$dst, (Int addr:$src))]>, VEX;
5304
5305 def VBROADCASTSS   : avx_broadcast<0x18, "vbroadcastss", VR128, f32mem,
5306                                    int_x86_avx_vbroadcastss>;
5307 def VBROADCASTSSY  : avx_broadcast<0x18, "vbroadcastss", VR256, f32mem,
5308                                    int_x86_avx_vbroadcastss_256>;
5309 def VBROADCASTSD   : avx_broadcast<0x19, "vbroadcastsd", VR256, f64mem,
5310                                    int_x86_avx_vbroadcast_sd_256>;
5311 def VBROADCASTF128 : avx_broadcast<0x1A, "vbroadcastf128", VR256, f128mem,
5312                                    int_x86_avx_vbroadcastf128_pd_256>;
5313
5314 def : Pat<(int_x86_avx_vbroadcastf128_ps_256 addr:$src),
5315           (VBROADCASTF128 addr:$src)>;
5316
5317 //===----------------------------------------------------------------------===//
5318 // VINSERTF128 - Insert packed floating-point values
5319 //
5320 def VINSERTF128rr : AVXAIi8<0x18, MRMSrcReg, (outs VR256:$dst),
5321           (ins VR256:$src1, VR128:$src2, i8imm:$src3),
5322           "vinsertf128\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",
5323           []>, VEX_4V;
5324 def VINSERTF128rm : AVXAIi8<0x18, MRMSrcMem, (outs VR256:$dst),
5325           (ins VR256:$src1, f128mem:$src2, i8imm:$src3),
5326           "vinsertf128\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",
5327           []>, VEX_4V;
5328
5329 def : Pat<(int_x86_avx_vinsertf128_pd_256 VR256:$src1, VR128:$src2, imm:$src3),
5330           (VINSERTF128rr VR256:$src1, VR128:$src2, imm:$src3)>;
5331 def : Pat<(int_x86_avx_vinsertf128_ps_256 VR256:$src1, VR128:$src2, imm:$src3),
5332           (VINSERTF128rr VR256:$src1, VR128:$src2, imm:$src3)>;
5333 def : Pat<(int_x86_avx_vinsertf128_si_256 VR256:$src1, VR128:$src2, imm:$src3),
5334           (VINSERTF128rr VR256:$src1, VR128:$src2, imm:$src3)>;
5335
5336 def : Pat<(vinsertf128_insert:$ins (v8f32 VR256:$src1), (v4f32 VR128:$src2),
5337                                    (i32 imm)),
5338           (VINSERTF128rr VR256:$src1, VR128:$src2,
5339                          (INSERT_get_vinsertf128_imm VR256:$ins))>;
5340 def : Pat<(vinsertf128_insert:$ins (v4f64 VR256:$src1), (v2f64 VR128:$src2),
5341                                    (i32 imm)),
5342           (VINSERTF128rr VR256:$src1, VR128:$src2,
5343                          (INSERT_get_vinsertf128_imm VR256:$ins))>;
5344 def : Pat<(vinsertf128_insert:$ins (v8i32 VR256:$src1), (v4i32 VR128:$src2),
5345                                    (i32 imm)),
5346           (VINSERTF128rr VR256:$src1, VR128:$src2,
5347                          (INSERT_get_vinsertf128_imm VR256:$ins))>;
5348 def : Pat<(vinsertf128_insert:$ins (v4i64 VR256:$src1), (v2i64 VR128:$src2),
5349                                    (i32 imm)),
5350           (VINSERTF128rr VR256:$src1, VR128:$src2,
5351                          (INSERT_get_vinsertf128_imm VR256:$ins))>;
5352 def : Pat<(vinsertf128_insert:$ins (v32i8 VR256:$src1), (v16i8 VR128:$src2),
5353                                    (i32 imm)),
5354           (VINSERTF128rr VR256:$src1, VR128:$src2,
5355                          (INSERT_get_vinsertf128_imm VR256:$ins))>;
5356 def : Pat<(vinsertf128_insert:$ins (v16i16 VR256:$src1), (v8i16 VR128:$src2),
5357                                    (i32 imm)),
5358           (VINSERTF128rr VR256:$src1, VR128:$src2,
5359                          (INSERT_get_vinsertf128_imm VR256:$ins))>;
5360
5361 // Special COPY patterns
5362 def : Pat<(insert_subvector undef, (v2i64 VR128:$src), (i32 0)),
5363           (INSERT_SUBREG (v4i64 (IMPLICIT_DEF)), VR128:$src, sub_xmm)>;
5364 def : Pat<(insert_subvector undef, (v2f64 VR128:$src), (i32 0)),
5365           (INSERT_SUBREG (v4f64 (IMPLICIT_DEF)), VR128:$src, sub_xmm)>;
5366 def : Pat<(insert_subvector undef, (v4i32 VR128:$src), (i32 0)),
5367           (INSERT_SUBREG (v8i32 (IMPLICIT_DEF)), VR128:$src, sub_xmm)>;
5368 def : Pat<(insert_subvector undef, (v4f32 VR128:$src), (i32 0)),
5369           (INSERT_SUBREG (v8f32 (IMPLICIT_DEF)), VR128:$src, sub_xmm)>;
5370 def : Pat<(insert_subvector undef, (v8i16 VR128:$src), (i32 0)),
5371           (INSERT_SUBREG (v16i16 (IMPLICIT_DEF)), VR128:$src, sub_xmm)>;
5372 def : Pat<(insert_subvector undef, (v16i8 VR128:$src), (i32 0)),
5373           (INSERT_SUBREG (v32i8 (IMPLICIT_DEF)), VR128:$src, sub_xmm)>;
5374
5375 //===----------------------------------------------------------------------===//
5376 // VEXTRACTF128 - Extract packed floating-point values
5377 //
5378 def VEXTRACTF128rr : AVXAIi8<0x19, MRMDestReg, (outs VR128:$dst),
5379           (ins VR256:$src1, i8imm:$src2),
5380           "vextractf128\t{$src2, $src1, $dst|$dst, $src1, $src2}",
5381           []>, VEX;
5382 def VEXTRACTF128mr : AVXAIi8<0x19, MRMDestMem, (outs),
5383           (ins f128mem:$dst, VR256:$src1, i8imm:$src2),
5384           "vextractf128\t{$src2, $src1, $dst|$dst, $src1, $src2}",
5385           []>, VEX;
5386
5387 def : Pat<(int_x86_avx_vextractf128_pd_256 VR256:$src1, imm:$src2),
5388           (VEXTRACTF128rr VR256:$src1, imm:$src2)>;
5389 def : Pat<(int_x86_avx_vextractf128_ps_256 VR256:$src1, imm:$src2),
5390           (VEXTRACTF128rr VR256:$src1, imm:$src2)>;
5391 def : Pat<(int_x86_avx_vextractf128_si_256 VR256:$src1, imm:$src2),
5392           (VEXTRACTF128rr VR256:$src1, imm:$src2)>;
5393
5394 def : Pat<(vextractf128_extract:$ext VR256:$src1, (i32 imm)),
5395           (v4f32 (VEXTRACTF128rr
5396                     (v8f32 VR256:$src1),
5397                     (EXTRACT_get_vextractf128_imm VR128:$ext)))>;
5398 def : Pat<(vextractf128_extract:$ext VR256:$src1, (i32 imm)),
5399           (v2f64 (VEXTRACTF128rr
5400                     (v4f64 VR256:$src1),
5401                     (EXTRACT_get_vextractf128_imm VR128:$ext)))>;
5402 def : Pat<(vextractf128_extract:$ext VR256:$src1, (i32 imm)),
5403           (v4i32 (VEXTRACTF128rr
5404                     (v8i32 VR256:$src1),
5405                     (EXTRACT_get_vextractf128_imm VR128:$ext)))>;
5406 def : Pat<(vextractf128_extract:$ext VR256:$src1, (i32 imm)),
5407           (v2i64 (VEXTRACTF128rr
5408                     (v4i64 VR256:$src1),
5409                     (EXTRACT_get_vextractf128_imm VR128:$ext)))>;
5410 def : Pat<(vextractf128_extract:$ext VR256:$src1, (i32 imm)),
5411           (v8i16 (VEXTRACTF128rr
5412                     (v16i16 VR256:$src1),
5413                     (EXTRACT_get_vextractf128_imm VR128:$ext)))>;
5414 def : Pat<(vextractf128_extract:$ext VR256:$src1, (i32 imm)),
5415           (v16i8 (VEXTRACTF128rr
5416                     (v32i8 VR256:$src1),
5417                     (EXTRACT_get_vextractf128_imm VR128:$ext)))>;
5418
5419 //===----------------------------------------------------------------------===//
5420 // VMASKMOV - Conditional SIMD Packed Loads and Stores
5421 //
5422 multiclass avx_movmask_rm<bits<8> opc_rm, bits<8> opc_mr, string OpcodeStr,
5423                           Intrinsic IntLd, Intrinsic IntLd256,
5424                           Intrinsic IntSt, Intrinsic IntSt256,
5425                           PatFrag pf128, PatFrag pf256> {
5426   def rm  : AVX8I<opc_rm, MRMSrcMem, (outs VR128:$dst),
5427              (ins VR128:$src1, f128mem:$src2),
5428              !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
5429              [(set VR128:$dst, (IntLd addr:$src2, VR128:$src1))]>,
5430              VEX_4V;
5431   def Yrm : AVX8I<opc_rm, MRMSrcMem, (outs VR256:$dst),
5432              (ins VR256:$src1, f256mem:$src2),
5433              !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
5434              [(set VR256:$dst, (IntLd256 addr:$src2, VR256:$src1))]>,
5435              VEX_4V;
5436   def mr  : AVX8I<opc_mr, MRMDestMem, (outs),
5437              (ins f128mem:$dst, VR128:$src1, VR128:$src2),
5438              !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
5439              [(IntSt addr:$dst, VR128:$src1, VR128:$src2)]>, VEX_4V;
5440   def Ymr : AVX8I<opc_mr, MRMDestMem, (outs),
5441              (ins f256mem:$dst, VR256:$src1, VR256:$src2),
5442              !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
5443              [(IntSt256 addr:$dst, VR256:$src1, VR256:$src2)]>, VEX_4V;
5444 }
5445
5446 defm VMASKMOVPS : avx_movmask_rm<0x2C, 0x2E, "vmaskmovps",
5447                                  int_x86_avx_maskload_ps,
5448                                  int_x86_avx_maskload_ps_256,
5449                                  int_x86_avx_maskstore_ps,
5450                                  int_x86_avx_maskstore_ps_256,
5451                                  memopv4f32, memopv8f32>;
5452 defm VMASKMOVPD : avx_movmask_rm<0x2D, 0x2F, "vmaskmovpd",
5453                                  int_x86_avx_maskload_pd,
5454                                  int_x86_avx_maskload_pd_256,
5455                                  int_x86_avx_maskstore_pd,
5456                                  int_x86_avx_maskstore_pd_256,
5457                                  memopv2f64, memopv4f64>;
5458
5459 //===----------------------------------------------------------------------===//
5460 // VPERM - Permute Floating-Point Values
5461 //
5462 multiclass avx_permil<bits<8> opc_rm, bits<8> opc_rmi, string OpcodeStr,
5463                       RegisterClass RC, X86MemOperand x86memop_f,
5464                       X86MemOperand x86memop_i, PatFrag f_frag, PatFrag i_frag,
5465                       Intrinsic IntVar, Intrinsic IntImm> {
5466   def rr  : AVX8I<opc_rm, MRMSrcReg, (outs RC:$dst),
5467              (ins RC:$src1, RC:$src2),
5468              !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
5469              [(set RC:$dst, (IntVar RC:$src1, RC:$src2))]>, VEX_4V;
5470   def rm  : AVX8I<opc_rm, MRMSrcMem, (outs RC:$dst),
5471              (ins RC:$src1, x86memop_i:$src2),
5472              !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
5473              [(set RC:$dst, (IntVar RC:$src1, (i_frag addr:$src2)))]>, VEX_4V;
5474
5475   def ri  : AVXAIi8<opc_rmi, MRMSrcReg, (outs RC:$dst),
5476              (ins RC:$src1, i8imm:$src2),
5477              !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
5478              [(set RC:$dst, (IntImm RC:$src1, imm:$src2))]>, VEX;
5479   def mi  : AVXAIi8<opc_rmi, MRMSrcMem, (outs RC:$dst),
5480              (ins x86memop_f:$src1, i8imm:$src2),
5481              !strconcat(OpcodeStr, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
5482              [(set RC:$dst, (IntImm (f_frag addr:$src1), imm:$src2))]>, VEX;
5483 }
5484
5485 defm VPERMILPS  : avx_permil<0x0C, 0x04, "vpermilps", VR128, f128mem, i128mem,
5486                              memopv4f32, memopv4i32,
5487                              int_x86_avx_vpermilvar_ps,
5488                              int_x86_avx_vpermil_ps>;
5489 defm VPERMILPSY : avx_permil<0x0C, 0x04, "vpermilps", VR256, f256mem, i256mem,
5490                              memopv8f32, memopv8i32,
5491                              int_x86_avx_vpermilvar_ps_256,
5492                              int_x86_avx_vpermil_ps_256>;
5493 defm VPERMILPD  : avx_permil<0x0D, 0x05, "vpermilpd", VR128, f128mem, i128mem,
5494                              memopv2f64, memopv2i64,
5495                              int_x86_avx_vpermilvar_pd,
5496                              int_x86_avx_vpermil_pd>;
5497 defm VPERMILPDY : avx_permil<0x0D, 0x05, "vpermilpd", VR256, f256mem, i256mem,
5498                              memopv4f64, memopv4i64,
5499                              int_x86_avx_vpermilvar_pd_256,
5500                              int_x86_avx_vpermil_pd_256>;
5501
5502 def VPERM2F128rr : AVXAIi8<0x06, MRMSrcReg, (outs VR256:$dst),
5503           (ins VR256:$src1, VR256:$src2, i8imm:$src3),
5504           "vperm2f128\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",
5505           []>, VEX_4V;
5506 def VPERM2F128rm : AVXAIi8<0x06, MRMSrcMem, (outs VR256:$dst),
5507           (ins VR256:$src1, f256mem:$src2, i8imm:$src3),
5508           "vperm2f128\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",
5509           []>, VEX_4V;
5510
5511 def : Pat<(int_x86_avx_vperm2f128_ps_256 VR256:$src1, VR256:$src2, imm:$src3),
5512           (VPERM2F128rr VR256:$src1, VR256:$src2, imm:$src3)>;
5513 def : Pat<(int_x86_avx_vperm2f128_pd_256 VR256:$src1, VR256:$src2, imm:$src3),
5514           (VPERM2F128rr VR256:$src1, VR256:$src2, imm:$src3)>;
5515 def : Pat<(int_x86_avx_vperm2f128_si_256 VR256:$src1, VR256:$src2, imm:$src3),
5516           (VPERM2F128rr VR256:$src1, VR256:$src2, imm:$src3)>;
5517
5518 def : Pat<(int_x86_avx_vperm2f128_ps_256
5519                   VR256:$src1, (memopv8f32 addr:$src2), imm:$src3),
5520           (VPERM2F128rm VR256:$src1, addr:$src2, imm:$src3)>;
5521 def : Pat<(int_x86_avx_vperm2f128_pd_256
5522                   VR256:$src1, (memopv4f64 addr:$src2), imm:$src3),
5523           (VPERM2F128rm VR256:$src1, addr:$src2, imm:$src3)>;
5524 def : Pat<(int_x86_avx_vperm2f128_si_256
5525                   VR256:$src1, (memopv8i32 addr:$src2), imm:$src3),
5526           (VPERM2F128rm VR256:$src1, addr:$src2, imm:$src3)>;
5527
5528 // Shuffle with VPERMIL instructions
5529 def : Pat<(v8f32 (X86VPermilpsy VR256:$src1, (i8 imm:$imm))),
5530           (VPERMILPSYri VR256:$src1, imm:$imm)>;
5531 def : Pat<(v4f64 (X86VPermilpdy VR256:$src1, (i8 imm:$imm))),
5532           (VPERMILPDYri VR256:$src1, imm:$imm)>;
5533
5534 //===----------------------------------------------------------------------===//
5535 // VZERO - Zero YMM registers
5536 //
5537 // Zero All YMM registers
5538 def VZEROALL : I<0x77, RawFrm, (outs), (ins), "vzeroall",
5539                  [(int_x86_avx_vzeroall)]>, VEX, VEX_L, Requires<[HasAVX]>;
5540
5541 // Zero Upper bits of YMM registers
5542 def VZEROUPPER : I<0x77, RawFrm, (outs), (ins), "vzeroupper",
5543                    [(int_x86_avx_vzeroupper)]>, VEX, Requires<[HasAVX]>;
5544
5545 //===----------------------------------------------------------------------===//
5546 // SSE Shuffle pattern fragments
5547 //===----------------------------------------------------------------------===//
5548
5549 // This is part of a "work in progress" refactoring. The idea is that all
5550 // vector shuffles are going to be translated into target specific nodes and
5551 // directly matched by the patterns below (which can be changed along the way)
5552 // The AVX version of some but not all of them are described here, and more
5553 // should come in a near future.
5554
5555 // Shuffle with PSHUFD instruction folding loads. The first two patterns match
5556 // SSE2 loads, which are always promoted to v2i64. The last one should match
5557 // the SSE1 case, where the only legal load is v4f32, but there is no PSHUFD
5558 // in SSE2, how does it ever worked? Anyway, the pattern will remain here until
5559 // we investigate further.
5560 def : Pat<(v4i32 (X86PShufd (bc_v4i32 (memopv2i64 addr:$src1)),
5561                                  (i8 imm:$imm))),
5562           (VPSHUFDmi addr:$src1, imm:$imm)>, Requires<[HasAVX]>;
5563 def : Pat<(v4i32 (X86PShufd (bc_v4i32 (memopv2i64 addr:$src1)),
5564                                  (i8 imm:$imm))),
5565           (PSHUFDmi addr:$src1, imm:$imm)>;
5566 def : Pat<(v4i32 (X86PShufd (bc_v4i32 (memopv4f32 addr:$src1)),
5567                                  (i8 imm:$imm))),
5568           (PSHUFDmi addr:$src1, imm:$imm)>; // FIXME: has this ever worked?
5569
5570 // Shuffle with PSHUFD instruction.
5571 def : Pat<(v4f32 (X86PShufd VR128:$src1, (i8 imm:$imm))),
5572           (VPSHUFDri VR128:$src1, imm:$imm)>, Requires<[HasAVX]>;
5573 def : Pat<(v4f32 (X86PShufd VR128:$src1, (i8 imm:$imm))),
5574           (PSHUFDri VR128:$src1, imm:$imm)>;
5575
5576 def : Pat<(v4i32 (X86PShufd VR128:$src1, (i8 imm:$imm))),
5577           (VPSHUFDri VR128:$src1, imm:$imm)>, Requires<[HasAVX]>;
5578 def : Pat<(v4i32 (X86PShufd VR128:$src1, (i8 imm:$imm))),
5579           (PSHUFDri VR128:$src1, imm:$imm)>;
5580
5581 // Shuffle with SHUFPD instruction.
5582 def : Pat<(v2f64 (X86Shufps VR128:$src1,
5583                      (memopv2f64 addr:$src2), (i8 imm:$imm))),
5584           (VSHUFPDrmi VR128:$src1, addr:$src2, imm:$imm)>, Requires<[HasAVX]>;
5585 def : Pat<(v2f64 (X86Shufps VR128:$src1,
5586                      (memopv2f64 addr:$src2), (i8 imm:$imm))),
5587           (SHUFPDrmi VR128:$src1, addr:$src2, imm:$imm)>;
5588
5589 def : Pat<(v2i64 (X86Shufpd VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5590           (VSHUFPDrri VR128:$src1, VR128:$src2, imm:$imm)>, Requires<[HasAVX]>;
5591 def : Pat<(v2i64 (X86Shufpd VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5592           (SHUFPDrri VR128:$src1, VR128:$src2, imm:$imm)>;
5593
5594 def : Pat<(v2f64 (X86Shufpd VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5595           (VSHUFPDrri VR128:$src1, VR128:$src2, imm:$imm)>, Requires<[HasAVX]>;
5596 def : Pat<(v2f64 (X86Shufpd VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5597           (SHUFPDrri VR128:$src1, VR128:$src2, imm:$imm)>;
5598
5599 // Shuffle with SHUFPS instruction.
5600 def : Pat<(v4f32 (X86Shufps VR128:$src1,
5601                      (memopv4f32 addr:$src2), (i8 imm:$imm))),
5602           (VSHUFPSrmi VR128:$src1, addr:$src2, imm:$imm)>, Requires<[HasAVX]>;
5603 def : Pat<(v4f32 (X86Shufps VR128:$src1,
5604                      (memopv4f32 addr:$src2), (i8 imm:$imm))),
5605           (SHUFPSrmi VR128:$src1, addr:$src2, imm:$imm)>;
5606
5607 def : Pat<(v4f32 (X86Shufps VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5608           (VSHUFPSrri VR128:$src1, VR128:$src2, imm:$imm)>, Requires<[HasAVX]>;
5609 def : Pat<(v4f32 (X86Shufps VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5610           (SHUFPSrri VR128:$src1, VR128:$src2, imm:$imm)>;
5611
5612 def : Pat<(v4i32 (X86Shufps VR128:$src1,
5613                      (bc_v4i32 (memopv2i64 addr:$src2)), (i8 imm:$imm))),
5614           (VSHUFPSrmi VR128:$src1, addr:$src2, imm:$imm)>, Requires<[HasAVX]>;
5615 def : Pat<(v4i32 (X86Shufps VR128:$src1,
5616                      (bc_v4i32 (memopv2i64 addr:$src2)), (i8 imm:$imm))),
5617           (SHUFPSrmi VR128:$src1, addr:$src2, imm:$imm)>;
5618
5619 def : Pat<(v4i32 (X86Shufps VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5620           (VSHUFPSrri VR128:$src1, VR128:$src2, imm:$imm)>, Requires<[HasAVX]>;
5621 def : Pat<(v4i32 (X86Shufps VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5622           (SHUFPSrri VR128:$src1, VR128:$src2, imm:$imm)>;
5623
5624 // Shuffle with MOVHLPS instruction
5625 def : Pat<(v4f32 (X86Movhlps VR128:$src1, VR128:$src2)),
5626           (MOVHLPSrr VR128:$src1, VR128:$src2)>;
5627 def : Pat<(v4i32 (X86Movhlps VR128:$src1, VR128:$src2)),
5628           (MOVHLPSrr VR128:$src1, VR128:$src2)>;
5629
5630 // Shuffle with MOVDDUP instruction
5631 def : Pat<(X86Movddup (memopv2f64 addr:$src)),
5632           (VMOVDDUPrm addr:$src)>, Requires<[HasAVX]>;
5633 def : Pat<(X86Movddup (memopv2f64 addr:$src)),
5634           (MOVDDUPrm addr:$src)>;
5635
5636 def : Pat<(X86Movddup (bc_v2f64 (memopv4f32 addr:$src))),
5637           (VMOVDDUPrm addr:$src)>, Requires<[HasAVX]>;
5638 def : Pat<(X86Movddup (bc_v2f64 (memopv4f32 addr:$src))),
5639           (MOVDDUPrm addr:$src)>;
5640
5641 def : Pat<(X86Movddup (bc_v2f64 (memopv2i64 addr:$src))),
5642           (VMOVDDUPrm addr:$src)>, Requires<[HasAVX]>;
5643 def : Pat<(X86Movddup (bc_v2f64 (memopv2i64 addr:$src))),
5644           (MOVDDUPrm addr:$src)>;
5645
5646 def : Pat<(X86Movddup (v2f64 (scalar_to_vector (loadf64 addr:$src)))),
5647           (VMOVDDUPrm addr:$src)>, Requires<[HasAVX]>;
5648 def : Pat<(X86Movddup (v2f64 (scalar_to_vector (loadf64 addr:$src)))),
5649           (MOVDDUPrm addr:$src)>;
5650
5651 def : Pat<(X86Movddup (bc_v2f64
5652                            (v2i64 (scalar_to_vector (loadi64 addr:$src))))),
5653           (VMOVDDUPrm addr:$src)>, Requires<[HasAVX]>;
5654 def : Pat<(X86Movddup (bc_v2f64
5655                            (v2i64 (scalar_to_vector (loadi64 addr:$src))))),
5656           (MOVDDUPrm addr:$src)>;
5657
5658
5659 // Shuffle with UNPCKLPS
5660 def : Pat<(v4f32 (X86Unpcklps VR128:$src1, (memopv4f32 addr:$src2))),
5661           (VUNPCKLPSrm VR128:$src1, addr:$src2)>, Requires<[HasAVX]>;
5662 def : Pat<(v8f32 (X86Unpcklpsy VR256:$src1, (memopv8f32 addr:$src2))),
5663           (VUNPCKLPSYrm VR256:$src1, addr:$src2)>, Requires<[HasAVX]>;
5664 def : Pat<(v4f32 (X86Unpcklps VR128:$src1, (memopv4f32 addr:$src2))),
5665           (UNPCKLPSrm VR128:$src1, addr:$src2)>;
5666
5667 def : Pat<(v4f32 (X86Unpcklps VR128:$src1, VR128:$src2)),
5668           (VUNPCKLPSrr VR128:$src1, VR128:$src2)>, Requires<[HasAVX]>;
5669 def : Pat<(v8f32 (X86Unpcklpsy VR256:$src1, VR256:$src2)),
5670           (VUNPCKLPSYrr VR256:$src1, VR256:$src2)>, Requires<[HasAVX]>;
5671 def : Pat<(v4f32 (X86Unpcklps VR128:$src1, VR128:$src2)),
5672           (UNPCKLPSrr VR128:$src1, VR128:$src2)>;
5673
5674 // Shuffle with UNPCKHPS
5675 def : Pat<(v4f32 (X86Unpckhps VR128:$src1, (memopv4f32 addr:$src2))),
5676           (VUNPCKHPSrm VR128:$src1, addr:$src2)>, Requires<[HasAVX]>;
5677 def : Pat<(v4f32 (X86Unpckhps VR128:$src1, (memopv4f32 addr:$src2))),
5678           (UNPCKHPSrm VR128:$src1, addr:$src2)>;
5679
5680 def : Pat<(v4f32 (X86Unpckhps VR128:$src1, VR128:$src2)),
5681           (VUNPCKHPSrr VR128:$src1, VR128:$src2)>, Requires<[HasAVX]>;
5682 def : Pat<(v4f32 (X86Unpckhps VR128:$src1, VR128:$src2)),
5683           (UNPCKHPSrr VR128:$src1, VR128:$src2)>;
5684
5685 // Shuffle with VUNPCKHPSY
5686 def : Pat<(v8f32 (X86Unpckhpsy VR256:$src1, (memopv8f32 addr:$src2))),
5687           (VUNPCKHPSYrm VR256:$src1, addr:$src2)>, Requires<[HasAVX]>;
5688 def : Pat<(v8f32 (X86Unpckhpsy VR256:$src1, VR256:$src2)),
5689           (VUNPCKHPSYrr VR256:$src1, VR256:$src2)>, Requires<[HasAVX]>;
5690
5691 // Shuffle with UNPCKLPD
5692 def : Pat<(v2f64 (X86Unpcklpd VR128:$src1, (memopv2f64 addr:$src2))),
5693           (VUNPCKLPDrm VR128:$src1, addr:$src2)>, Requires<[HasAVX]>;
5694 def : Pat<(v4f64 (X86Unpcklpdy VR256:$src1, (memopv4f64 addr:$src2))),
5695           (VUNPCKLPDYrm VR256:$src1, addr:$src2)>, Requires<[HasAVX]>;
5696 def : Pat<(v2f64 (X86Unpcklpd VR128:$src1, (memopv2f64 addr:$src2))),
5697           (UNPCKLPDrm VR128:$src1, addr:$src2)>;
5698
5699 def : Pat<(v2f64 (X86Unpcklpd VR128:$src1, VR128:$src2)),
5700           (VUNPCKLPDrr VR128:$src1, VR128:$src2)>, Requires<[HasAVX]>;
5701 def : Pat<(v4f64 (X86Unpcklpdy VR256:$src1, VR256:$src2)),
5702           (VUNPCKLPDYrr VR256:$src1, VR256:$src2)>, Requires<[HasAVX]>;
5703 def : Pat<(v2f64 (X86Unpcklpd VR128:$src1, VR128:$src2)),
5704           (UNPCKLPDrr VR128:$src1, VR128:$src2)>;
5705
5706 // Shuffle with UNPCKHPD
5707 def : Pat<(v2f64 (X86Unpckhpd VR128:$src1, (memopv2f64 addr:$src2))),
5708           (VUNPCKHPDrm VR128:$src1, addr:$src2)>, Requires<[HasAVX]>;
5709 def : Pat<(v2f64 (X86Unpckhpd VR128:$src1, (memopv2f64 addr:$src2))),
5710           (UNPCKHPDrm VR128:$src1, addr:$src2)>;
5711
5712 def : Pat<(v2f64 (X86Unpckhpd VR128:$src1, VR128:$src2)),
5713           (VUNPCKHPDrr VR128:$src1, VR128:$src2)>, Requires<[HasAVX]>;
5714 def : Pat<(v2f64 (X86Unpckhpd VR128:$src1, VR128:$src2)),
5715           (UNPCKHPDrr VR128:$src1, VR128:$src2)>;
5716
5717 // Shuffle with VUNPCKHPDY
5718 def : Pat<(v4f64 (X86Unpckhpdy VR256:$src1, (memopv4f64 addr:$src2))),
5719           (VUNPCKHPDYrm VR256:$src1, addr:$src2)>, Requires<[HasAVX]>;
5720 def : Pat<(v4f64 (X86Unpckhpdy VR256:$src1, VR256:$src2)),
5721           (VUNPCKHPDYrr VR256:$src1, VR256:$src2)>, Requires<[HasAVX]>;
5722
5723 // Shuffle with MOVLHPS
5724 def : Pat<(X86Movlhps VR128:$src1,
5725                     (bc_v4f32 (v2f64 (scalar_to_vector (loadf64 addr:$src2))))),
5726           (MOVHPSrm VR128:$src1, addr:$src2)>;
5727 def : Pat<(X86Movlhps VR128:$src1,
5728                     (bc_v4i32 (v2i64 (X86vzload addr:$src2)))),
5729           (MOVHPSrm VR128:$src1, addr:$src2)>;
5730 def : Pat<(v4f32 (X86Movlhps VR128:$src1, VR128:$src2)),
5731           (MOVLHPSrr VR128:$src1, VR128:$src2)>;
5732 def : Pat<(v4i32 (X86Movlhps VR128:$src1, VR128:$src2)),
5733           (MOVLHPSrr VR128:$src1, VR128:$src2)>;
5734 def : Pat<(v2i64 (X86Movlhps VR128:$src1, VR128:$src2)),
5735           (MOVLHPSrr (v2i64 VR128:$src1), VR128:$src2)>;
5736
5737 // FIXME: Instead of X86Movddup, there should be a X86Unpcklpd here, the problem
5738 // is during lowering, where it's not possible to recognize the load fold cause
5739 // it has two uses through a bitcast. One use disappears at isel time and the
5740 // fold opportunity reappears.
5741 def : Pat<(v2f64 (X86Movddup VR128:$src)),
5742           (UNPCKLPDrr VR128:$src, VR128:$src)>;
5743
5744 // Shuffle with MOVLHPD
5745 def : Pat<(v2f64 (X86Movlhpd VR128:$src1,
5746                     (scalar_to_vector (loadf64 addr:$src2)))),
5747           (MOVHPDrm VR128:$src1, addr:$src2)>;
5748
5749 // FIXME: Instead of X86Unpcklpd, there should be a X86Movlhpd here, the problem
5750 // is during lowering, where it's not possible to recognize the load fold cause
5751 // it has two uses through a bitcast. One use disappears at isel time and the
5752 // fold opportunity reappears.
5753 def : Pat<(v2f64 (X86Unpcklpd VR128:$src1,
5754                     (scalar_to_vector (loadf64 addr:$src2)))),
5755           (MOVHPDrm VR128:$src1, addr:$src2)>;
5756
5757 // Shuffle with MOVSS
5758 def : Pat<(v4f32 (X86Movss VR128:$src1, (scalar_to_vector FR32:$src2))),
5759           (MOVSSrr VR128:$src1, FR32:$src2)>;
5760 def : Pat<(v4i32 (X86Movss VR128:$src1, VR128:$src2)),
5761           (MOVSSrr (v4i32 VR128:$src1),
5762                    (EXTRACT_SUBREG (v4i32 VR128:$src2), sub_ss))>;
5763 def : Pat<(v4f32 (X86Movss VR128:$src1, VR128:$src2)),
5764           (MOVSSrr (v4f32 VR128:$src1),
5765                    (EXTRACT_SUBREG (v4f32 VR128:$src2), sub_ss))>;
5766 // FIXME: Instead of a X86Movss there should be a X86Movlps here, the problem
5767 // is during lowering, where it's not possible to recognize the load fold cause
5768 // it has two uses through a bitcast. One use disappears at isel time and the
5769 // fold opportunity reappears.
5770 def : Pat<(X86Movss VR128:$src1,
5771                     (bc_v4i32 (v2i64 (load addr:$src2)))),
5772           (MOVLPSrm VR128:$src1, addr:$src2)>;
5773
5774 // Shuffle with MOVSD
5775 def : Pat<(v2f64 (X86Movsd VR128:$src1, (scalar_to_vector FR64:$src2))),
5776           (MOVSDrr VR128:$src1, FR64:$src2)>;
5777 def : Pat<(v2i64 (X86Movsd VR128:$src1, VR128:$src2)),
5778           (MOVSDrr (v2i64 VR128:$src1),
5779                    (EXTRACT_SUBREG (v2i64 VR128:$src2), sub_sd))>;
5780 def : Pat<(v2f64 (X86Movsd VR128:$src1, VR128:$src2)),
5781           (MOVSDrr (v2f64 VR128:$src1),
5782                    (EXTRACT_SUBREG (v2f64 VR128:$src2), sub_sd))>;
5783 def : Pat<(v4f32 (X86Movsd VR128:$src1, VR128:$src2)),
5784           (MOVSDrr VR128:$src1, (EXTRACT_SUBREG (v4f32 VR128:$src2), sub_sd))>;
5785 def : Pat<(v4i32 (X86Movsd VR128:$src1, VR128:$src2)),
5786           (MOVSDrr VR128:$src1, (EXTRACT_SUBREG (v4i32 VR128:$src2), sub_sd))>;
5787
5788 // Shuffle with PSHUFHW
5789 def : Pat<(v8i16 (X86PShufhw VR128:$src, (i8 imm:$imm))),
5790           (PSHUFHWri VR128:$src, imm:$imm)>;
5791 def : Pat<(v8i16 (X86PShufhw (bc_v8i16 (memopv2i64 addr:$src)), (i8 imm:$imm))),
5792           (PSHUFHWmi addr:$src, imm:$imm)>;
5793
5794 // Shuffle with PSHUFLW
5795 def : Pat<(v8i16 (X86PShuflw VR128:$src, (i8 imm:$imm))),
5796           (PSHUFLWri VR128:$src, imm:$imm)>;
5797 def : Pat<(v8i16 (X86PShuflw (bc_v8i16 (memopv2i64 addr:$src)), (i8 imm:$imm))),
5798           (PSHUFLWmi addr:$src, imm:$imm)>;
5799
5800 // Shuffle with PALIGN
5801 def : Pat<(v4i32 (X86PAlign VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5802           (PALIGNR128rr VR128:$src2, VR128:$src1, imm:$imm)>;
5803 def : Pat<(v4f32 (X86PAlign VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5804           (PALIGNR128rr VR128:$src2, VR128:$src1, imm:$imm)>;
5805 def : Pat<(v8i16 (X86PAlign VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5806           (PALIGNR128rr VR128:$src2, VR128:$src1, imm:$imm)>;
5807 def : Pat<(v16i8 (X86PAlign VR128:$src1, VR128:$src2, (i8 imm:$imm))),
5808           (PALIGNR128rr VR128:$src2, VR128:$src1, imm:$imm)>;
5809
5810 // Shuffle with MOVLPS
5811 def : Pat<(v4f32 (X86Movlps VR128:$src1, (load addr:$src2))),
5812           (MOVLPSrm VR128:$src1, addr:$src2)>;
5813 def : Pat<(v4i32 (X86Movlps VR128:$src1, (load addr:$src2))),
5814           (MOVLPSrm VR128:$src1, addr:$src2)>;
5815 def : Pat<(X86Movlps VR128:$src1,
5816                     (bc_v4f32 (v2f64 (scalar_to_vector (loadf64 addr:$src2))))),
5817           (MOVLPSrm VR128:$src1, addr:$src2)>;
5818 // FIXME: Instead of a X86Movlps there should be a X86Movsd here, the problem
5819 // is during lowering, where it's not possible to recognize the load fold cause
5820 // it has two uses through a bitcast. One use disappears at isel time and the
5821 // fold opportunity reappears.
5822 def : Pat<(v4f32 (X86Movlps VR128:$src1, VR128:$src2)),
5823           (MOVSDrr VR128:$src1, (EXTRACT_SUBREG (v4f32 VR128:$src2), sub_sd))>;
5824
5825 def : Pat<(v4i32 (X86Movlps VR128:$src1, VR128:$src2)), 
5826           (MOVSDrr VR128:$src1, (EXTRACT_SUBREG (v4i32 VR128:$src2), sub_sd))>; 
5827
5828 // Shuffle with MOVLPD
5829 def : Pat<(v2f64 (X86Movlpd VR128:$src1, (load addr:$src2))),
5830           (MOVLPDrm VR128:$src1, addr:$src2)>;
5831 def : Pat<(v2i64 (X86Movlpd VR128:$src1, (load addr:$src2))),
5832           (MOVLPDrm VR128:$src1, addr:$src2)>;
5833 def : Pat<(v2f64 (X86Movlpd VR128:$src1,
5834                             (scalar_to_vector (loadf64 addr:$src2)))),
5835           (MOVLPDrm VR128:$src1, addr:$src2)>;
5836
5837 // Extra patterns to match stores with MOVHPS/PD and MOVLPS/PD
5838 def : Pat<(store (f64 (vector_extract
5839           (v2f64 (X86Unpckhps VR128:$src, (undef))), (iPTR 0))),addr:$dst),
5840           (MOVHPSmr addr:$dst, VR128:$src)>;
5841 def : Pat<(store (f64 (vector_extract
5842           (v2f64 (X86Unpckhpd VR128:$src, (undef))), (iPTR 0))),addr:$dst),
5843           (MOVHPDmr addr:$dst, VR128:$src)>;
5844
5845 def : Pat<(store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)),addr:$src1),
5846           (MOVLPSmr addr:$src1, VR128:$src2)>;
5847 def : Pat<(store (v4i32 (X86Movlps
5848                  (bc_v4i32 (loadv2i64 addr:$src1)), VR128:$src2)), addr:$src1),
5849           (MOVLPSmr addr:$src1, VR128:$src2)>;
5850
5851 def : Pat<(store (v2f64 (X86Movlpd (load addr:$src1), VR128:$src2)),addr:$src1),
5852           (MOVLPDmr addr:$src1, VR128:$src2)>;
5853 def : Pat<(store (v2i64 (X86Movlpd (load addr:$src1), VR128:$src2)),addr:$src1),
5854           (MOVLPDmr addr:$src1, VR128:$src2)>;