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