]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/X86/X86InstrVecCompiler.td
Merge ^/head r327624 through r327885.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / X86 / X86InstrVecCompiler.td
1 //===- X86InstrVecCompiler.td - Vector Compiler Patterns ---*- 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 various vector pseudo instructions used by the
11 // compiler, as well as Pat patterns used during instruction selection.
12 //
13 //===----------------------------------------------------------------------===//
14
15 //===----------------------------------------------------------------------===//
16 // No op bitconverts
17 //===----------------------------------------------------------------------===//
18
19 // Bitcasts between 128-bit vector types. Return the original type since
20 // no instruction is needed for the conversion
21 def : Pat<(v2i64 (bitconvert (v4i32 VR128:$src))), (v2i64 VR128:$src)>;
22 def : Pat<(v2i64 (bitconvert (v8i16 VR128:$src))), (v2i64 VR128:$src)>;
23 def : Pat<(v2i64 (bitconvert (v16i8 VR128:$src))), (v2i64 VR128:$src)>;
24 def : Pat<(v2i64 (bitconvert (v2f64 VR128:$src))), (v2i64 VR128:$src)>;
25 def : Pat<(v2i64 (bitconvert (v4f32 VR128:$src))), (v2i64 VR128:$src)>;
26 def : Pat<(v4i32 (bitconvert (v2i64 VR128:$src))), (v4i32 VR128:$src)>;
27 def : Pat<(v4i32 (bitconvert (v8i16 VR128:$src))), (v4i32 VR128:$src)>;
28 def : Pat<(v4i32 (bitconvert (v16i8 VR128:$src))), (v4i32 VR128:$src)>;
29 def : Pat<(v4i32 (bitconvert (v2f64 VR128:$src))), (v4i32 VR128:$src)>;
30 def : Pat<(v4i32 (bitconvert (v4f32 VR128:$src))), (v4i32 VR128:$src)>;
31 def : Pat<(v8i16 (bitconvert (v2i64 VR128:$src))), (v8i16 VR128:$src)>;
32 def : Pat<(v8i16 (bitconvert (v4i32 VR128:$src))), (v8i16 VR128:$src)>;
33 def : Pat<(v8i16 (bitconvert (v16i8 VR128:$src))), (v8i16 VR128:$src)>;
34 def : Pat<(v8i16 (bitconvert (v2f64 VR128:$src))), (v8i16 VR128:$src)>;
35 def : Pat<(v8i16 (bitconvert (v4f32 VR128:$src))), (v8i16 VR128:$src)>;
36 def : Pat<(v16i8 (bitconvert (v2i64 VR128:$src))), (v16i8 VR128:$src)>;
37 def : Pat<(v16i8 (bitconvert (v4i32 VR128:$src))), (v16i8 VR128:$src)>;
38 def : Pat<(v16i8 (bitconvert (v8i16 VR128:$src))), (v16i8 VR128:$src)>;
39 def : Pat<(v16i8 (bitconvert (v2f64 VR128:$src))), (v16i8 VR128:$src)>;
40 def : Pat<(v16i8 (bitconvert (v4f32 VR128:$src))), (v16i8 VR128:$src)>;
41 def : Pat<(v4f32 (bitconvert (v2i64 VR128:$src))), (v4f32 VR128:$src)>;
42 def : Pat<(v4f32 (bitconvert (v4i32 VR128:$src))), (v4f32 VR128:$src)>;
43 def : Pat<(v4f32 (bitconvert (v8i16 VR128:$src))), (v4f32 VR128:$src)>;
44 def : Pat<(v4f32 (bitconvert (v16i8 VR128:$src))), (v4f32 VR128:$src)>;
45 def : Pat<(v4f32 (bitconvert (v2f64 VR128:$src))), (v4f32 VR128:$src)>;
46 def : Pat<(v2f64 (bitconvert (v2i64 VR128:$src))), (v2f64 VR128:$src)>;
47 def : Pat<(v2f64 (bitconvert (v4i32 VR128:$src))), (v2f64 VR128:$src)>;
48 def : Pat<(v2f64 (bitconvert (v8i16 VR128:$src))), (v2f64 VR128:$src)>;
49 def : Pat<(v2f64 (bitconvert (v16i8 VR128:$src))), (v2f64 VR128:$src)>;
50 def : Pat<(v2f64 (bitconvert (v4f32 VR128:$src))), (v2f64 VR128:$src)>;
51 def : Pat<(f128  (bitconvert (i128  FR128:$src))), (f128  FR128:$src)>;
52 def : Pat<(i128  (bitconvert (f128  FR128:$src))), (i128  FR128:$src)>;
53
54 // Bitcasts between 256-bit vector types. Return the original type since
55 // no instruction is needed for the conversion
56 def : Pat<(v4i64  (bitconvert (v8i32  VR256:$src))), (v4i64  VR256:$src)>;
57 def : Pat<(v4i64  (bitconvert (v16i16 VR256:$src))), (v4i64  VR256:$src)>;
58 def : Pat<(v4i64  (bitconvert (v32i8  VR256:$src))), (v4i64  VR256:$src)>;
59 def : Pat<(v4i64  (bitconvert (v8f32  VR256:$src))), (v4i64  VR256:$src)>;
60 def : Pat<(v4i64  (bitconvert (v4f64  VR256:$src))), (v4i64  VR256:$src)>;
61 def : Pat<(v8i32  (bitconvert (v4i64  VR256:$src))), (v8i32  VR256:$src)>;
62 def : Pat<(v8i32  (bitconvert (v16i16 VR256:$src))), (v8i32  VR256:$src)>;
63 def : Pat<(v8i32  (bitconvert (v32i8  VR256:$src))), (v8i32  VR256:$src)>;
64 def : Pat<(v8i32  (bitconvert (v4f64  VR256:$src))), (v8i32  VR256:$src)>;
65 def : Pat<(v8i32  (bitconvert (v8f32  VR256:$src))), (v8i32  VR256:$src)>;
66 def : Pat<(v16i16 (bitconvert (v4i64  VR256:$src))), (v16i16 VR256:$src)>;
67 def : Pat<(v16i16 (bitconvert (v8i32  VR256:$src))), (v16i16 VR256:$src)>;
68 def : Pat<(v16i16 (bitconvert (v32i8  VR256:$src))), (v16i16 VR256:$src)>;
69 def : Pat<(v16i16 (bitconvert (v4f64  VR256:$src))), (v16i16 VR256:$src)>;
70 def : Pat<(v16i16 (bitconvert (v8f32  VR256:$src))), (v16i16 VR256:$src)>;
71 def : Pat<(v32i8  (bitconvert (v4i64  VR256:$src))), (v32i8  VR256:$src)>;
72 def : Pat<(v32i8  (bitconvert (v8i32  VR256:$src))), (v32i8  VR256:$src)>;
73 def : Pat<(v32i8  (bitconvert (v16i16 VR256:$src))), (v32i8  VR256:$src)>;
74 def : Pat<(v32i8  (bitconvert (v4f64  VR256:$src))), (v32i8  VR256:$src)>;
75 def : Pat<(v32i8  (bitconvert (v8f32  VR256:$src))), (v32i8  VR256:$src)>;
76 def : Pat<(v8f32  (bitconvert (v4i64  VR256:$src))), (v8f32  VR256:$src)>;
77 def : Pat<(v8f32  (bitconvert (v8i32  VR256:$src))), (v8f32  VR256:$src)>;
78 def : Pat<(v8f32  (bitconvert (v16i16 VR256:$src))), (v8f32  VR256:$src)>;
79 def : Pat<(v8f32  (bitconvert (v32i8  VR256:$src))), (v8f32  VR256:$src)>;
80 def : Pat<(v8f32  (bitconvert (v4f64  VR256:$src))), (v8f32  VR256:$src)>;
81 def : Pat<(v4f64  (bitconvert (v4i64  VR256:$src))), (v4f64  VR256:$src)>;
82 def : Pat<(v4f64  (bitconvert (v8i32  VR256:$src))), (v4f64  VR256:$src)>;
83 def : Pat<(v4f64  (bitconvert (v16i16 VR256:$src))), (v4f64  VR256:$src)>;
84 def : Pat<(v4f64  (bitconvert (v32i8  VR256:$src))), (v4f64  VR256:$src)>;
85 def : Pat<(v4f64  (bitconvert (v8f32  VR256:$src))), (v4f64  VR256:$src)>;
86
87 // Bitcasts between 512-bit vector types. Return the original type since
88 // no instruction is needed for the conversion.
89 def : Pat<(v8f64  (bitconvert (v8i64  VR512:$src))), (v8f64  VR512:$src)>;
90 def : Pat<(v8f64  (bitconvert (v16i32 VR512:$src))), (v8f64  VR512:$src)>;
91 def : Pat<(v8f64  (bitconvert (v32i16 VR512:$src))), (v8f64  VR512:$src)>;
92 def : Pat<(v8f64  (bitconvert (v64i8  VR512:$src))), (v8f64  VR512:$src)>;
93 def : Pat<(v8f64  (bitconvert (v16f32 VR512:$src))), (v8f64  VR512:$src)>;
94 def : Pat<(v16f32 (bitconvert (v8i64  VR512:$src))), (v16f32 VR512:$src)>;
95 def : Pat<(v16f32 (bitconvert (v16i32 VR512:$src))), (v16f32 VR512:$src)>;
96 def : Pat<(v16f32 (bitconvert (v32i16 VR512:$src))), (v16f32 VR512:$src)>;
97 def : Pat<(v16f32 (bitconvert (v64i8  VR512:$src))), (v16f32 VR512:$src)>;
98 def : Pat<(v16f32 (bitconvert (v8f64  VR512:$src))), (v16f32 VR512:$src)>;
99 def : Pat<(v8i64  (bitconvert (v16i32 VR512:$src))), (v8i64  VR512:$src)>;
100 def : Pat<(v8i64  (bitconvert (v32i16 VR512:$src))), (v8i64  VR512:$src)>;
101 def : Pat<(v8i64  (bitconvert (v64i8  VR512:$src))), (v8i64  VR512:$src)>;
102 def : Pat<(v8i64  (bitconvert (v8f64  VR512:$src))), (v8i64  VR512:$src)>;
103 def : Pat<(v8i64  (bitconvert (v16f32 VR512:$src))), (v8i64  VR512:$src)>;
104 def : Pat<(v16i32 (bitconvert (v8i64  VR512:$src))), (v16i32 VR512:$src)>;
105 def : Pat<(v16i32 (bitconvert (v16f32 VR512:$src))), (v16i32 VR512:$src)>;
106 def : Pat<(v16i32 (bitconvert (v32i16 VR512:$src))), (v16i32 VR512:$src)>;
107 def : Pat<(v16i32 (bitconvert (v64i8  VR512:$src))), (v16i32 VR512:$src)>;
108 def : Pat<(v16i32 (bitconvert (v8f64  VR512:$src))), (v16i32 VR512:$src)>;
109 def : Pat<(v32i16 (bitconvert (v8i64  VR512:$src))), (v32i16 VR512:$src)>;
110 def : Pat<(v32i16 (bitconvert (v16i32 VR512:$src))), (v32i16 VR512:$src)>;
111 def : Pat<(v32i16 (bitconvert (v64i8  VR512:$src))), (v32i16 VR512:$src)>;
112 def : Pat<(v32i16 (bitconvert (v8f64  VR512:$src))), (v32i16 VR512:$src)>;
113 def : Pat<(v32i16 (bitconvert (v16f32 VR512:$src))), (v32i16 VR512:$src)>;
114 def : Pat<(v32i16 (bitconvert (v16f32 VR512:$src))), (v32i16 VR512:$src)>;
115 def : Pat<(v64i8  (bitconvert (v8i64  VR512:$src))), (v64i8  VR512:$src)>;
116 def : Pat<(v64i8  (bitconvert (v16i32 VR512:$src))), (v64i8  VR512:$src)>;
117 def : Pat<(v64i8  (bitconvert (v32i16 VR512:$src))), (v64i8  VR512:$src)>;
118 def : Pat<(v64i8  (bitconvert (v8f64  VR512:$src))), (v64i8  VR512:$src)>;
119 def : Pat<(v64i8  (bitconvert (v16f32 VR512:$src))), (v64i8  VR512:$src)>;
120
121
122 //===----------------------------------------------------------------------===//
123 //  Non-instruction patterns
124 //===----------------------------------------------------------------------===//
125
126 // A vector extract of the first f32/f64 position is a subregister copy
127 def : Pat<(f32 (extractelt (v4f32 VR128:$src), (iPTR 0))),
128           (COPY_TO_REGCLASS (v4f32 VR128:$src), FR32)>;
129 def : Pat<(f64 (extractelt (v2f64 VR128:$src), (iPTR 0))),
130           (COPY_TO_REGCLASS (v2f64 VR128:$src), FR64)>;
131
132 // Implicitly promote a 32-bit scalar to a vector.
133 def : Pat<(v4f32 (scalar_to_vector FR32:$src)),
134           (COPY_TO_REGCLASS FR32:$src, VR128)>;
135 // Implicitly promote a 64-bit scalar to a vector.
136 def : Pat<(v2f64 (scalar_to_vector FR64:$src)),
137           (COPY_TO_REGCLASS FR64:$src, VR128)>;
138
139
140 //===----------------------------------------------------------------------===//
141 // Subvector tricks
142 //===----------------------------------------------------------------------===//
143
144 // Patterns for insert_subvector/extract_subvector to/from index=0
145 multiclass subvector_subreg_lowering<RegisterClass subRC, ValueType subVT,
146                                      RegisterClass RC, ValueType VT,
147                                      SubRegIndex subIdx> {
148   def : Pat<(subVT (extract_subvector (VT RC:$src), (iPTR 0))),
149             (subVT (EXTRACT_SUBREG RC:$src, subIdx))>;
150
151   let AddedComplexity = 25 in // to give priority over vinsertf128rm
152   def : Pat<(VT (insert_subvector undef, subRC:$src, (iPTR 0))),
153             (VT (INSERT_SUBREG (IMPLICIT_DEF), subRC:$src, subIdx))>;
154 }
155
156 // A 128-bit subvector extract from the first 256-bit vector position is a
157 // subregister copy that needs no instruction. Likewise, a 128-bit subvector
158 // insert to the first 256-bit vector position is a subregister copy that needs
159 // no instruction.
160 defm : subvector_subreg_lowering<VR128, v4i32, VR256, v8i32,  sub_xmm>;
161 defm : subvector_subreg_lowering<VR128, v4f32, VR256, v8f32,  sub_xmm>;
162 defm : subvector_subreg_lowering<VR128, v2i64, VR256, v4i64,  sub_xmm>;
163 defm : subvector_subreg_lowering<VR128, v2f64, VR256, v4f64,  sub_xmm>;
164 defm : subvector_subreg_lowering<VR128, v8i16, VR256, v16i16, sub_xmm>;
165 defm : subvector_subreg_lowering<VR128, v16i8, VR256, v32i8,  sub_xmm>;
166
167 // A 128-bit subvector extract from the first 512-bit vector position is a
168 // subregister copy that needs no instruction. Likewise, a 128-bit subvector
169 // insert to the first 512-bit vector position is a subregister copy that needs
170 // no instruction.
171 defm : subvector_subreg_lowering<VR128, v4i32, VR512, v16i32, sub_xmm>;
172 defm : subvector_subreg_lowering<VR128, v4f32, VR512, v16f32, sub_xmm>;
173 defm : subvector_subreg_lowering<VR128, v2i64, VR512, v8i64,  sub_xmm>;
174 defm : subvector_subreg_lowering<VR128, v2f64, VR512, v8f64,  sub_xmm>;
175 defm : subvector_subreg_lowering<VR128, v8i16, VR512, v32i16, sub_xmm>;
176 defm : subvector_subreg_lowering<VR128, v16i8, VR512, v64i8,  sub_xmm>;
177
178 // A 128-bit subvector extract from the first 512-bit vector position is a
179 // subregister copy that needs no instruction. Likewise, a 128-bit subvector
180 // insert to the first 512-bit vector position is a subregister copy that needs
181 // no instruction.
182 defm : subvector_subreg_lowering<VR256, v8i32,  VR512, v16i32, sub_ymm>;
183 defm : subvector_subreg_lowering<VR256, v8f32,  VR512, v16f32, sub_ymm>;
184 defm : subvector_subreg_lowering<VR256, v4i64,  VR512, v8i64,  sub_ymm>;
185 defm : subvector_subreg_lowering<VR256, v4f64,  VR512, v8f64,  sub_ymm>;
186 defm : subvector_subreg_lowering<VR256, v16i16, VR512, v32i16, sub_ymm>;
187 defm : subvector_subreg_lowering<VR256, v32i8,  VR512, v64i8,  sub_ymm>;
188
189
190 multiclass subvector_store_lowering<string AlignedStr, string UnalignedStr,
191                                     RegisterClass RC, ValueType DstTy,
192                                     ValueType SrcTy, SubRegIndex SubIdx> {
193   def : Pat<(alignedstore (DstTy (extract_subvector
194                                   (SrcTy RC:$src), (iPTR 0))), addr:$dst),
195             (!cast<Instruction>("VMOV"#AlignedStr#"mr") addr:$dst,
196              (DstTy (EXTRACT_SUBREG RC:$src, SubIdx)))>;
197
198   def : Pat<(store (DstTy (extract_subvector
199                            (SrcTy RC:$src), (iPTR 0))), addr:$dst),
200             (!cast<Instruction>("VMOV"#UnalignedStr#"mr") addr:$dst,
201              (DstTy (EXTRACT_SUBREG RC:$src, SubIdx)))>;
202 }
203
204 let Predicates = [HasAVX, NoVLX] in {
205   defm : subvector_store_lowering<"APD", "UPD", VR256X, v2f64, v4f64,  sub_xmm>;
206   defm : subvector_store_lowering<"APS", "UPS", VR256X, v4f32, v8f32,  sub_xmm>;
207   defm : subvector_store_lowering<"DQA", "DQU", VR256X, v2i64, v4i64,  sub_xmm>;
208   defm : subvector_store_lowering<"DQA", "DQU", VR256X, v4i32, v8i32,  sub_xmm>;
209   defm : subvector_store_lowering<"DQA", "DQU", VR256X, v8i16, v16i16, sub_xmm>;
210   defm : subvector_store_lowering<"DQA", "DQU", VR256X, v16i8, v32i8,  sub_xmm>;
211 }
212
213 let Predicates = [HasVLX] in {
214   // Special patterns for storing subvector extracts of lower 128-bits
215   // Its cheaper to just use VMOVAPS/VMOVUPS instead of VEXTRACTF128mr
216   defm : subvector_store_lowering<"APDZ128", "UPDZ128", VR256X, v2f64, v4f64,
217                                   sub_xmm>;
218   defm : subvector_store_lowering<"APSZ128", "UPSZ128", VR256X, v4f32, v8f32,
219                                   sub_xmm>;
220   defm : subvector_store_lowering<"DQA32Z128", "DQU32Z128", VR256X, v2i64,
221                                   v4i64, sub_xmm>;
222   defm : subvector_store_lowering<"DQA32Z128", "DQU32Z128", VR256X, v4i32,
223                                   v8i32, sub_xmm>;
224   defm : subvector_store_lowering<"DQA32Z128", "DQU32Z128", VR256X, v8i16,
225                                   v16i16, sub_xmm>;
226   defm : subvector_store_lowering<"DQA32Z128", "DQU32Z128", VR256X, v16i8,
227                                   v32i8, sub_xmm>;
228
229   // Special patterns for storing subvector extracts of lower 128-bits of 512.
230   // Its cheaper to just use VMOVAPS/VMOVUPS instead of VEXTRACTF128mr
231   defm : subvector_store_lowering<"APDZ128", "UPDZ128", VR512, v2f64, v8f64,
232                                   sub_xmm>;
233   defm : subvector_store_lowering<"APSZ128", "UPSZ128", VR512, v4f32, v16f32,
234                                   sub_xmm>;
235   defm : subvector_store_lowering<"DQA32Z128", "DQU32Z128", VR512, v2i64,
236                                   v8i64, sub_xmm>;
237   defm : subvector_store_lowering<"DQA32Z128", "DQU32Z128", VR512, v4i32,
238                                   v16i32, sub_xmm>;
239   defm : subvector_store_lowering<"DQA32Z128", "DQU32Z128", VR512, v8i16,
240                                   v32i16, sub_xmm>;
241   defm : subvector_store_lowering<"DQA32Z128", "DQU32Z128", VR512, v16i8,
242                                   v64i8, sub_xmm>;
243
244   // Special patterns for storing subvector extracts of lower 256-bits of 512.
245   // Its cheaper to just use VMOVAPS/VMOVUPS instead of VEXTRACTF128mr
246   defm : subvector_store_lowering<"APDZ256", "UPDZ256", VR512, v4f64, v8f64,
247                                   sub_ymm>;
248   defm : subvector_store_lowering<"APSZ256", "UPSZ256", VR512, v8f32, v16f32,
249                                   sub_ymm>;
250   defm : subvector_store_lowering<"DQA32Z256", "DQU32Z256", VR512, v4i64,
251                                   v8i64, sub_ymm>;
252   defm : subvector_store_lowering<"DQA32Z256", "DQU32Z256", VR512, v8i32,
253                                   v16i32, sub_ymm>;
254   defm : subvector_store_lowering<"DQA32Z256", "DQU32Z256", VR512, v16i16,
255                                   v32i16, sub_ymm>;
256   defm : subvector_store_lowering<"DQA32Z256", "DQU32Z256", VR512, v32i8,
257                                   v64i8, sub_ymm>;
258 }
259
260 // If we're inserting into an all zeros vector, just use a plain move which
261 // will zero the upper bits.
262 // TODO: Is there a safe way to detect whether the producing instruction
263 // already zeroed the upper bits?
264 multiclass subvector_zero_lowering<string MoveStr, RegisterClass RC,
265                                    ValueType DstTy, ValueType SrcTy,
266                                    ValueType ZeroTy, PatFrag memop,
267                                    SubRegIndex SubIdx> {
268   def : Pat<(DstTy (insert_subvector (bitconvert (ZeroTy immAllZerosV)),
269                                      (SrcTy RC:$src), (iPTR 0))),
270             (SUBREG_TO_REG (i64 0),
271              (!cast<Instruction>("VMOV"#MoveStr#"rr") RC:$src), SubIdx)>;
272
273   def : Pat<(DstTy (insert_subvector (bitconvert (ZeroTy immAllZerosV)),
274                                      (SrcTy (bitconvert (memop addr:$src))),
275                                      (iPTR 0))),
276             (SUBREG_TO_REG (i64 0),
277              (!cast<Instruction>("VMOV"#MoveStr#"rm") addr:$src), SubIdx)>;
278 }
279
280 let Predicates = [HasAVX, NoVLX] in {
281   defm : subvector_zero_lowering<"APD", VR128, v4f64, v2f64, v8i32, loadv2f64,
282                                  sub_xmm>;
283   defm : subvector_zero_lowering<"APS", VR128, v8f32, v4f32, v8i32, loadv4f32,
284                                  sub_xmm>;
285   defm : subvector_zero_lowering<"DQA", VR128, v4i64, v2i64, v8i32, loadv2i64,
286                                  sub_xmm>;
287   defm : subvector_zero_lowering<"DQA", VR128, v8i32, v4i32, v8i32, loadv2i64,
288                                  sub_xmm>;
289   defm : subvector_zero_lowering<"DQA", VR128, v16i16, v8i16, v8i32, loadv2i64,
290                                  sub_xmm>;
291   defm : subvector_zero_lowering<"DQA", VR128, v32i8, v16i8, v8i32, loadv2i64,
292                                  sub_xmm>;
293 }
294
295 let Predicates = [HasVLX] in {
296   defm : subvector_zero_lowering<"APDZ128", VR128X, v4f64, v2f64, v8i32,
297                                  loadv2f64, sub_xmm>;
298   defm : subvector_zero_lowering<"APSZ128", VR128X, v8f32, v4f32, v8i32,
299                                  loadv4f32, sub_xmm>;
300   defm : subvector_zero_lowering<"DQA64Z128", VR128X, v4i64, v2i64, v8i32,
301                                  loadv2i64, sub_xmm>;
302   defm : subvector_zero_lowering<"DQA64Z128", VR128X, v8i32, v4i32, v8i32,
303                                  loadv2i64, sub_xmm>;
304   defm : subvector_zero_lowering<"DQA64Z128", VR128X, v16i16, v8i16, v8i32,
305                                  loadv2i64, sub_xmm>;
306   defm : subvector_zero_lowering<"DQA64Z128", VR128X, v32i8, v16i8, v8i32,
307                                  loadv2i64, sub_xmm>;
308
309   defm : subvector_zero_lowering<"APDZ128", VR128X, v8f64, v2f64, v16i32,
310                                  loadv2f64, sub_xmm>;
311   defm : subvector_zero_lowering<"APSZ128", VR128X, v16f32, v4f32, v16i32,
312                                  loadv4f32, sub_xmm>;
313   defm : subvector_zero_lowering<"DQA64Z128", VR128X, v8i64, v2i64, v16i32,
314                                  loadv2i64, sub_xmm>;
315   defm : subvector_zero_lowering<"DQA64Z128", VR128X, v16i32, v4i32, v16i32,
316                                  loadv2i64, sub_xmm>;
317   defm : subvector_zero_lowering<"DQA64Z128", VR128X, v32i16, v8i16, v16i32,
318                                  loadv2i64, sub_xmm>;
319   defm : subvector_zero_lowering<"DQA64Z128", VR128X, v64i8, v16i8, v16i32,
320                                  loadv2i64, sub_xmm>;
321
322   defm : subvector_zero_lowering<"APDZ256", VR256X, v8f64, v4f64, v16i32,
323                                  loadv4f64, sub_ymm>;
324   defm : subvector_zero_lowering<"APSZ256", VR256X, v16f32, v8f32, v16i32,
325                                  loadv8f32, sub_ymm>;
326   defm : subvector_zero_lowering<"DQA64Z256", VR256X, v8i64, v4i64, v16i32,
327                                  loadv4i64, sub_ymm>;
328   defm : subvector_zero_lowering<"DQA64Z256", VR256X, v16i32, v8i32, v16i32,
329                                  loadv4i64, sub_ymm>;
330   defm : subvector_zero_lowering<"DQA64Z256", VR256X, v32i16, v16i16, v16i32,
331                                  loadv4i64, sub_ymm>;
332   defm : subvector_zero_lowering<"DQA64Z256", VR256X, v64i8, v32i8, v16i32,
333                                  loadv4i64, sub_ymm>;
334 }
335
336 let Predicates = [HasAVX512, NoVLX] in {
337   defm : subvector_zero_lowering<"APD", VR128, v8f64, v2f64, v16i32, loadv2f64,
338                                  sub_xmm>;
339   defm : subvector_zero_lowering<"APS", VR128, v16f32, v4f32, v16i32, loadv4f32,
340                                  sub_xmm>;
341   defm : subvector_zero_lowering<"DQA", VR128, v8i64, v2i64, v16i32, loadv2i64,
342                                  sub_xmm>;
343   defm : subvector_zero_lowering<"DQA", VR128, v16i32, v4i32, v16i32, loadv2i64,
344                                  sub_xmm>;
345   defm : subvector_zero_lowering<"DQA", VR128, v32i16, v8i16, v16i32, loadv2i64,
346                                  sub_xmm>;
347   defm : subvector_zero_lowering<"DQA", VR128, v64i8, v16i8, v16i32, loadv2i64,
348                                  sub_xmm>;
349
350   defm : subvector_zero_lowering<"APDY", VR256, v8f64, v4f64, v16i32,
351                                  loadv4f64, sub_ymm>;
352   defm : subvector_zero_lowering<"APSY", VR256, v16f32, v8f32, v16i32,
353                                  loadv8f32, sub_ymm>;
354   defm : subvector_zero_lowering<"DQAY", VR256, v8i64, v4i64, v16i32,
355                                  loadv4i64, sub_ymm>;
356   defm : subvector_zero_lowering<"DQAY", VR256, v16i32, v8i32, v16i32,
357                                  loadv4i64, sub_ymm>;
358   defm : subvector_zero_lowering<"DQAY", VR256, v32i16, v16i16, v16i32,
359                                  loadv4i64, sub_ymm>;
360   defm : subvector_zero_lowering<"DQAY", VR256, v64i8, v32i8, v16i32,
361                                  loadv4i64, sub_ymm>;
362 }
363
364 // List of opcodes that guaranteed to zero the upper elements of vector regs.
365 // TODO: Ideally this would be a blacklist instead of a whitelist. But SHA
366 // intrinsics and some MMX->XMM move instructions that aren't VEX encoded make
367 // this difficult. So starting with a couple opcodes used by reduction loops
368 // where we explicitly insert zeros.
369 class veczeroupper<ValueType vt, RegisterClass RC> :
370   PatLeaf<(vt RC:$src), [{
371     return N->getOpcode() == X86ISD::VPMADDWD ||
372            N->getOpcode() == X86ISD::PSADBW;
373   }]>;
374
375 def zeroupperv2f64 : veczeroupper<v2f64, VR128>;
376 def zeroupperv4f32 : veczeroupper<v4f32, VR128>;
377 def zeroupperv2i64 : veczeroupper<v2i64, VR128>;
378 def zeroupperv4i32 : veczeroupper<v4i32, VR128>;
379 def zeroupperv8i16 : veczeroupper<v8i16, VR128>;
380 def zeroupperv16i8 : veczeroupper<v16i8, VR128>;
381
382 def zeroupperv4f64  : veczeroupper<v4f64, VR256>;
383 def zeroupperv8f32  : veczeroupper<v8f32, VR256>;
384 def zeroupperv4i64  : veczeroupper<v4i64, VR256>;
385 def zeroupperv8i32  : veczeroupper<v8i32, VR256>;
386 def zeroupperv16i16 : veczeroupper<v16i16, VR256>;
387 def zeroupperv32i8  : veczeroupper<v32i8, VR256>;
388
389
390 // If we can guarantee the upper elements have already been zeroed we can elide
391 // an explicit zeroing.
392 multiclass subvector_zero_ellision<RegisterClass RC, ValueType DstTy,
393                                    ValueType SrcTy, ValueType ZeroTy,
394                                    SubRegIndex SubIdx, PatLeaf Zeroupper> {
395   def : Pat<(DstTy (insert_subvector (bitconvert (ZeroTy immAllZerosV)),
396                                      Zeroupper:$src, (iPTR 0))),
397             (SUBREG_TO_REG (i64 0), RC:$src, SubIdx)>;
398 }
399
400 // 128->256
401 defm: subvector_zero_ellision<VR128, v4f64,  v2f64, v8i32, sub_xmm, zeroupperv2f64>;
402 defm: subvector_zero_ellision<VR128, v8f32,  v4f32, v8i32, sub_xmm, zeroupperv4f32>;
403 defm: subvector_zero_ellision<VR128, v4i64,  v2i64, v8i32, sub_xmm, zeroupperv2i64>;
404 defm: subvector_zero_ellision<VR128, v8i32,  v4i32, v8i32, sub_xmm, zeroupperv4i32>;
405 defm: subvector_zero_ellision<VR128, v16i16, v8i16, v8i32, sub_xmm, zeroupperv8i16>;
406 defm: subvector_zero_ellision<VR128, v32i8,  v16i8, v8i32, sub_xmm, zeroupperv16i8>;
407
408 // 128->512
409 defm: subvector_zero_ellision<VR128, v8f64,  v2f64, v16i32, sub_xmm, zeroupperv2f64>;
410 defm: subvector_zero_ellision<VR128, v16f32, v4f32, v16i32, sub_xmm, zeroupperv4f32>;
411 defm: subvector_zero_ellision<VR128, v8i64,  v2i64, v16i32, sub_xmm, zeroupperv2i64>;
412 defm: subvector_zero_ellision<VR128, v16i32, v4i32, v16i32, sub_xmm, zeroupperv4i32>;
413 defm: subvector_zero_ellision<VR128, v32i16, v8i16, v16i32, sub_xmm, zeroupperv8i16>;
414 defm: subvector_zero_ellision<VR128, v64i8,  v16i8, v16i32, sub_xmm, zeroupperv16i8>;
415
416 // 256->512
417 defm: subvector_zero_ellision<VR256, v8f64,  v4f64,  v16i32, sub_ymm, zeroupperv4f64>;
418 defm: subvector_zero_ellision<VR256, v16f32, v8f32,  v16i32, sub_ymm, zeroupperv8f32>;
419 defm: subvector_zero_ellision<VR256, v8i64,  v4i64,  v16i32, sub_ymm, zeroupperv4i64>;
420 defm: subvector_zero_ellision<VR256, v16i32, v8i32,  v16i32, sub_ymm, zeroupperv8i32>;
421 defm: subvector_zero_ellision<VR256, v32i16, v16i16, v16i32, sub_ymm, zeroupperv16i16>;
422 defm: subvector_zero_ellision<VR256, v64i8,  v32i8,  v16i32, sub_ymm, zeroupperv32i8>;
423
424
425 class maskzeroupper<ValueType vt, RegisterClass RC> :
426   PatLeaf<(vt RC:$src), [{
427     return isMaskZeroExtended(N);
428   }]>;
429
430 def maskzeroupperv2i1  : maskzeroupper<v2i1,  VK2>;
431 def maskzeroupperv4i1  : maskzeroupper<v4i1,  VK4>;
432 def maskzeroupperv8i1  : maskzeroupper<v8i1,  VK8>;
433 def maskzeroupperv16i1 : maskzeroupper<v16i1, VK16>;
434 def maskzeroupperv32i1 : maskzeroupper<v32i1, VK32>;
435
436 // The patterns determine if we can depend on the upper bits of a mask register
437 // being zeroed by the previous operation so that we can skip explicit
438 // zeroing.
439 let Predicates = [HasBWI] in {
440   def : Pat<(v32i1 (insert_subvector (v32i1 immAllZerosV),
441                                      maskzeroupperv8i1:$src, (iPTR 0))),
442             (COPY_TO_REGCLASS VK8:$src, VK32)>;
443   def : Pat<(v32i1 (insert_subvector (v32i1 immAllZerosV),
444                                      maskzeroupperv16i1:$src, (iPTR 0))),
445             (COPY_TO_REGCLASS VK16:$src, VK32)>;
446   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
447                                      maskzeroupperv8i1:$src, (iPTR 0))),
448             (COPY_TO_REGCLASS VK8:$src, VK64)>;
449   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
450                                      maskzeroupperv16i1:$src, (iPTR 0))),
451             (COPY_TO_REGCLASS VK16:$src, VK64)>;
452   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
453                                      maskzeroupperv32i1:$src, (iPTR 0))),
454             (COPY_TO_REGCLASS VK32:$src, VK64)>;
455 }
456
457 let Predicates = [HasAVX512] in {
458   def : Pat<(v16i1 (insert_subvector (v16i1 immAllZerosV),
459                                      maskzeroupperv8i1:$src, (iPTR 0))),
460             (COPY_TO_REGCLASS VK8:$src, VK16)>;
461 }
462
463 let Predicates = [HasVLX, HasDQI] in {
464   def : Pat<(v8i1 (insert_subvector (v8i1 immAllZerosV),
465                                     maskzeroupperv2i1:$src, (iPTR 0))),
466             (COPY_TO_REGCLASS VK2:$src, VK8)>;
467   def : Pat<(v8i1 (insert_subvector (v8i1 immAllZerosV),
468                                     maskzeroupperv4i1:$src, (iPTR 0))),
469             (COPY_TO_REGCLASS VK4:$src, VK8)>;
470 }
471
472 let Predicates = [HasVLX] in {
473   def : Pat<(v16i1 (insert_subvector (v16i1 immAllZerosV),
474                                      maskzeroupperv2i1:$src, (iPTR 0))),
475             (COPY_TO_REGCLASS VK2:$src, VK16)>;
476   def : Pat<(v16i1 (insert_subvector (v16i1 immAllZerosV),
477                                      maskzeroupperv4i1:$src, (iPTR 0))),
478             (COPY_TO_REGCLASS VK4:$src, VK16)>;
479 }
480
481 let Predicates = [HasBWI, HasVLX] in {
482   def : Pat<(v32i1 (insert_subvector (v32i1 immAllZerosV),
483                                      maskzeroupperv2i1:$src, (iPTR 0))),
484             (COPY_TO_REGCLASS VK2:$src, VK32)>;
485   def : Pat<(v32i1 (insert_subvector (v32i1 immAllZerosV),
486                                      maskzeroupperv4i1:$src, (iPTR 0))),
487             (COPY_TO_REGCLASS VK4:$src, VK32)>;
488   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
489                                      maskzeroupperv2i1:$src, (iPTR 0))),
490             (COPY_TO_REGCLASS VK2:$src, VK64)>;
491   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
492                                      maskzeroupperv4i1:$src, (iPTR 0))),
493             (COPY_TO_REGCLASS VK4:$src, VK64)>;
494 }
495
496 // If the bits are not zero we have to fall back to explicitly zeroing by
497 // using shifts.
498 let Predicates = [HasAVX512, NoDQI] in {
499   def : Pat<(v16i1 (insert_subvector (v16i1 immAllZerosV),
500                                      (v8i1 VK8:$mask), (iPTR 0))),
501             (KSHIFTRWri (KSHIFTLWri (COPY_TO_REGCLASS VK8:$mask, VK16),
502                                     (i8 8)), (i8 8))>;
503 }
504
505 let Predicates = [HasDQI] in {
506   def : Pat<(v16i1 (insert_subvector (v16i1 immAllZerosV),
507                                      (v8i1 VK8:$mask), (iPTR 0))),
508             (COPY_TO_REGCLASS (KMOVBkk VK8:$mask), VK16)>;
509 }
510
511 let Predicates = [HasVLX, HasDQI] in {
512   def : Pat<(v8i1 (insert_subvector (v8i1 immAllZerosV),
513                                     (v2i1 VK2:$mask), (iPTR 0))),
514             (KSHIFTRBri (KSHIFTLBri (COPY_TO_REGCLASS VK2:$mask, VK8),
515                                     (i8 6)), (i8 6))>;
516   def : Pat<(v8i1 (insert_subvector (v8i1 immAllZerosV),
517                                     (v4i1 VK4:$mask), (iPTR 0))),
518             (KSHIFTRBri (KSHIFTLBri (COPY_TO_REGCLASS VK4:$mask, VK8),
519                                     (i8 4)), (i8 4))>;
520 }
521
522 let Predicates = [HasVLX] in {
523   def : Pat<(v16i1 (insert_subvector (v16i1 immAllZerosV),
524                                      (v2i1 VK2:$mask), (iPTR 0))),
525             (KSHIFTRWri (KSHIFTLWri (COPY_TO_REGCLASS VK2:$mask, VK16),
526                                     (i8 14)), (i8 14))>;
527   def : Pat<(v16i1 (insert_subvector (v16i1 immAllZerosV),
528                                      (v4i1 VK4:$mask), (iPTR 0))),
529             (KSHIFTRWri (KSHIFTLWri (COPY_TO_REGCLASS VK4:$mask, VK16),
530                                     (i8 12)), (i8 12))>;
531 }
532
533 let Predicates = [HasBWI] in {
534   def : Pat<(v32i1 (insert_subvector (v32i1 immAllZerosV),
535                                      (v16i1 VK16:$mask), (iPTR 0))),
536             (COPY_TO_REGCLASS (KMOVWkk VK16:$mask), VK32)>;
537
538   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
539                                      (v16i1 VK16:$mask), (iPTR 0))),
540             (COPY_TO_REGCLASS (KMOVWkk VK16:$mask), VK64)>;
541   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
542                                      (v32i1 VK32:$mask), (iPTR 0))),
543             (COPY_TO_REGCLASS (KMOVDkk VK32:$mask), VK64)>;
544 }
545
546 let Predicates = [HasBWI, NoDQI] in {
547   def : Pat<(v32i1 (insert_subvector (v32i1 immAllZerosV),
548                                      (v8i1 VK8:$mask), (iPTR 0))),
549             (KSHIFTRDri (KSHIFTLDri (COPY_TO_REGCLASS VK8:$mask, VK32),
550                                     (i8 24)), (i8 24))>;
551
552   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
553                                      (v8i1 VK8:$mask), (iPTR 0))),
554             (KSHIFTRQri (KSHIFTLQri (COPY_TO_REGCLASS VK8:$mask, VK64),
555                                     (i8 56)), (i8 56))>;
556 }
557
558 let Predicates = [HasBWI, HasDQI] in {
559   def : Pat<(v32i1 (insert_subvector (v32i1 immAllZerosV),
560                                      (v8i1 VK8:$mask), (iPTR 0))),
561             (COPY_TO_REGCLASS (KMOVBkk VK8:$mask), VK32)>;
562
563   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
564                                      (v8i1 VK8:$mask), (iPTR 0))),
565             (COPY_TO_REGCLASS (KMOVBkk VK8:$mask), VK64)>;
566 }
567
568 let Predicates = [HasBWI, HasVLX] in {
569   def : Pat<(v32i1 (insert_subvector (v32i1 immAllZerosV),
570                                      (v2i1 VK2:$mask), (iPTR 0))),
571             (KSHIFTRDri (KSHIFTLDri (COPY_TO_REGCLASS VK2:$mask, VK32),
572                                     (i8 30)), (i8 30))>;
573   def : Pat<(v32i1 (insert_subvector (v32i1 immAllZerosV),
574                                      (v4i1 VK4:$mask), (iPTR 0))),
575             (KSHIFTRDri (KSHIFTLDri (COPY_TO_REGCLASS VK4:$mask, VK32),
576                                     (i8 28)), (i8 28))>;
577
578   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
579                                      (v2i1 VK2:$mask), (iPTR 0))),
580             (KSHIFTRQri (KSHIFTLQri (COPY_TO_REGCLASS VK2:$mask, VK64),
581                                     (i8 62)), (i8 62))>;
582   def : Pat<(v64i1 (insert_subvector (v64i1 immAllZerosV),
583                                      (v4i1 VK4:$mask), (iPTR 0))),
584             (KSHIFTRQri (KSHIFTLQri (COPY_TO_REGCLASS VK4:$mask, VK64),
585                                     (i8 60)), (i8 60))>;
586 }