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