1 //===-- X86TargetTransformInfo.cpp - X86 specific TTI pass ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file implements a TargetTransformInfo analysis pass specific to the
10 /// X86 target machine. It uses the target's detailed information to provide
11 /// more precise answers to certain TTI queries, while letting the target
12 /// independent and default TTI implementations handle the rest.
13 ///
14 //===----------------------------------------------------------------------===//
15 /// About Cost Model numbers used below it's necessary to say the following:
16 /// the numbers correspond to some "generic" X86 CPU instead of usage of
17 /// concrete CPU model. Usually the numbers correspond to CPU where the feature
18 /// apeared at the first time. For example, if we do Subtarget.hasSSE42() in
19 /// the lookups below the cost is based on Nehalem as that was the first CPU
20 /// to support that feature level and thus has most likely the worst case cost.
21 /// Some examples of other technologies/CPUs:
22 ///   SSE 3   - Pentium4 / Athlon64
23 ///   SSE 4.1 - Penryn
24 ///   SSE 4.2 - Nehalem
25 ///   AVX     - Sandy Bridge
26 ///   AVX2    - Haswell
27 ///   AVX-512 - Xeon Phi / Skylake
28 /// And some examples of instruction target dependent costs (latency)
29 ///                   divss     sqrtss          rsqrtss
30 ///   AMD K7            11-16     19              3
31 ///   Piledriver        9-24      13-15           5
32 ///   Jaguar            14        16              2
33 ///   Pentium II,III    18        30              2
34 ///   Nehalem           7-14      7-18            3
35 ///   Haswell           10-13     11              5
36 /// TODO: Develop and implement  the target dependent cost model and
37 /// specialize cost numbers for different Cost Model Targets such as throughput,
38 /// code size, latency and uop count.
39 //===----------------------------------------------------------------------===//
40 
41 #include "X86TargetTransformInfo.h"
42 #include "llvm/Analysis/TargetTransformInfo.h"
43 #include "llvm/CodeGen/BasicTTIImpl.h"
44 #include "llvm/CodeGen/CostTable.h"
45 #include "llvm/CodeGen/TargetLowering.h"
46 #include "llvm/IR/IntrinsicInst.h"
47 #include "llvm/Support/Debug.h"
48 
49 using namespace llvm;
50 
51 #define DEBUG_TYPE "x86tti"
52 
53 //===----------------------------------------------------------------------===//
54 //
55 // X86 cost model.
56 //
57 //===----------------------------------------------------------------------===//
58 
59 TargetTransformInfo::PopcntSupportKind
60 X86TTIImpl::getPopcntSupport(unsigned TyWidth) {
61   assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
62   // TODO: Currently the __builtin_popcount() implementation using SSE3
63   //   instructions is inefficient. Once the problem is fixed, we should
64   //   call ST->hasSSE3() instead of ST->hasPOPCNT().
65   return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software;
66 }
67 
68 llvm::Optional<unsigned> X86TTIImpl::getCacheSize(
69   TargetTransformInfo::CacheLevel Level) const {
70   switch (Level) {
71   case TargetTransformInfo::CacheLevel::L1D:
72     //   - Penryn
73     //   - Nehalem
74     //   - Westmere
75     //   - Sandy Bridge
76     //   - Ivy Bridge
77     //   - Haswell
78     //   - Broadwell
79     //   - Skylake
80     //   - Kabylake
81     return 32 * 1024;  //  32 KByte
82   case TargetTransformInfo::CacheLevel::L2D:
83     //   - Penryn
84     //   - Nehalem
85     //   - Westmere
86     //   - Sandy Bridge
87     //   - Ivy Bridge
88     //   - Haswell
89     //   - Broadwell
90     //   - Skylake
91     //   - Kabylake
92     return 256 * 1024; // 256 KByte
93   }
94 
95   llvm_unreachable("Unknown TargetTransformInfo::CacheLevel");
96 }
97 
98 llvm::Optional<unsigned> X86TTIImpl::getCacheAssociativity(
99   TargetTransformInfo::CacheLevel Level) const {
100   //   - Penryn
101   //   - Nehalem
102   //   - Westmere
103   //   - Sandy Bridge
104   //   - Ivy Bridge
105   //   - Haswell
106   //   - Broadwell
107   //   - Skylake
108   //   - Kabylake
109   switch (Level) {
110   case TargetTransformInfo::CacheLevel::L1D:
111     LLVM_FALLTHROUGH;
112   case TargetTransformInfo::CacheLevel::L2D:
113     return 8;
114   }
115 
116   llvm_unreachable("Unknown TargetTransformInfo::CacheLevel");
117 }
118 
119 unsigned X86TTIImpl::getNumberOfRegisters(unsigned ClassID) const {
120   bool Vector = (ClassID == 1);
121   if (Vector && !ST->hasSSE1())
122     return 0;
123 
124   if (ST->is64Bit()) {
125     if (Vector && ST->hasAVX512())
126       return 32;
127     return 16;
128   }
129   return 8;
130 }
131 
132 TypeSize
133 X86TTIImpl::getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
134   unsigned PreferVectorWidth = ST->getPreferVectorWidth();
135   switch (K) {
136   case TargetTransformInfo::RGK_Scalar:
137     return TypeSize::getFixed(ST->is64Bit() ? 64 : 32);
138   case TargetTransformInfo::RGK_FixedWidthVector:
139     if (ST->hasAVX512() && PreferVectorWidth >= 512)
140       return TypeSize::getFixed(512);
141     if (ST->hasAVX() && PreferVectorWidth >= 256)
142       return TypeSize::getFixed(256);
143     if (ST->hasSSE1() && PreferVectorWidth >= 128)
144       return TypeSize::getFixed(128);
145     return TypeSize::getFixed(0);
146   case TargetTransformInfo::RGK_ScalableVector:
147     return TypeSize::getScalable(0);
148   }
149 
150   llvm_unreachable("Unsupported register kind");
151 }
152 
153 unsigned X86TTIImpl::getLoadStoreVecRegBitWidth(unsigned) const {
154   return getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector)
155       .getFixedSize();
156 }
157 
158 unsigned X86TTIImpl::getMaxInterleaveFactor(unsigned VF) {
159   // If the loop will not be vectorized, don't interleave the loop.
160   // Let regular unroll to unroll the loop, which saves the overflow
161   // check and memory check cost.
162   if (VF == 1)
163     return 1;
164 
165   if (ST->isAtom())
166     return 1;
167 
168   // Sandybridge and Haswell have multiple execution ports and pipelined
169   // vector units.
170   if (ST->hasAVX())
171     return 4;
172 
173   return 2;
174 }
175 
176 InstructionCost X86TTIImpl::getArithmeticInstrCost(
177     unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
178     TTI::OperandValueKind Op1Info, TTI::OperandValueKind Op2Info,
179     TTI::OperandValueProperties Opd1PropInfo,
180     TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args,
181     const Instruction *CxtI) {
182   // TODO: Handle more cost kinds.
183   if (CostKind != TTI::TCK_RecipThroughput)
184     return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
185                                          Op2Info, Opd1PropInfo,
186                                          Opd2PropInfo, Args, CxtI);
187   // Legalize the type.
188   std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
189 
190   int ISD = TLI->InstructionOpcodeToISD(Opcode);
191   assert(ISD && "Invalid opcode");
192 
193   static const CostTblEntry GLMCostTable[] = {
194     { ISD::FDIV,  MVT::f32,   18 }, // divss
195     { ISD::FDIV,  MVT::v4f32, 35 }, // divps
196     { ISD::FDIV,  MVT::f64,   33 }, // divsd
197     { ISD::FDIV,  MVT::v2f64, 65 }, // divpd
198   };
199 
200   if (ST->useGLMDivSqrtCosts())
201     if (const auto *Entry = CostTableLookup(GLMCostTable, ISD,
202                                             LT.second))
203       return LT.first * Entry->Cost;
204 
205   static const CostTblEntry SLMCostTable[] = {
206     { ISD::MUL,   MVT::v4i32, 11 }, // pmulld
207     { ISD::MUL,   MVT::v8i16, 2  }, // pmullw
208     { ISD::MUL,   MVT::v16i8, 14 }, // extend/pmullw/trunc sequence.
209     { ISD::FMUL,  MVT::f64,   2  }, // mulsd
210     { ISD::FMUL,  MVT::v2f64, 4  }, // mulpd
211     { ISD::FMUL,  MVT::v4f32, 2  }, // mulps
212     { ISD::FDIV,  MVT::f32,   17 }, // divss
213     { ISD::FDIV,  MVT::v4f32, 39 }, // divps
214     { ISD::FDIV,  MVT::f64,   32 }, // divsd
215     { ISD::FDIV,  MVT::v2f64, 69 }, // divpd
216     { ISD::FADD,  MVT::v2f64, 2  }, // addpd
217     { ISD::FSUB,  MVT::v2f64, 2  }, // subpd
218     // v2i64/v4i64 mul is custom lowered as a series of long:
219     // multiplies(3), shifts(3) and adds(2)
220     // slm muldq version throughput is 2 and addq throughput 4
221     // thus: 3X2 (muldq throughput) + 3X1 (shift throughput) +
222     //       3X4 (addq throughput) = 17
223     { ISD::MUL,   MVT::v2i64, 17 },
224     // slm addq\subq throughput is 4
225     { ISD::ADD,   MVT::v2i64, 4  },
226     { ISD::SUB,   MVT::v2i64, 4  },
227   };
228 
229   if (ST->isSLM()) {
230     if (Args.size() == 2 && ISD == ISD::MUL && LT.second == MVT::v4i32) {
231       // Check if the operands can be shrinked into a smaller datatype.
232       bool Op1Signed = false;
233       unsigned Op1MinSize = BaseT::minRequiredElementSize(Args[0], Op1Signed);
234       bool Op2Signed = false;
235       unsigned Op2MinSize = BaseT::minRequiredElementSize(Args[1], Op2Signed);
236 
237       bool SignedMode = Op1Signed || Op2Signed;
238       unsigned OpMinSize = std::max(Op1MinSize, Op2MinSize);
239 
240       if (OpMinSize <= 7)
241         return LT.first * 3; // pmullw/sext
242       if (!SignedMode && OpMinSize <= 8)
243         return LT.first * 3; // pmullw/zext
244       if (OpMinSize <= 15)
245         return LT.first * 5; // pmullw/pmulhw/pshuf
246       if (!SignedMode && OpMinSize <= 16)
247         return LT.first * 5; // pmullw/pmulhw/pshuf
248     }
249 
250     if (const auto *Entry = CostTableLookup(SLMCostTable, ISD,
251                                             LT.second)) {
252       return LT.first * Entry->Cost;
253     }
254   }
255 
256   if ((ISD == ISD::SDIV || ISD == ISD::SREM || ISD == ISD::UDIV ||
257        ISD == ISD::UREM) &&
258       (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
259        Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
260       Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) {
261     if (ISD == ISD::SDIV || ISD == ISD::SREM) {
262       // On X86, vector signed division by constants power-of-two are
263       // normally expanded to the sequence SRA + SRL + ADD + SRA.
264       // The OperandValue properties may not be the same as that of the previous
265       // operation; conservatively assume OP_None.
266       InstructionCost Cost =
267           2 * getArithmeticInstrCost(Instruction::AShr, Ty, CostKind, Op1Info,
268                                      Op2Info, TargetTransformInfo::OP_None,
269                                      TargetTransformInfo::OP_None);
270       Cost += getArithmeticInstrCost(Instruction::LShr, Ty, CostKind, Op1Info,
271                                      Op2Info,
272                                      TargetTransformInfo::OP_None,
273                                      TargetTransformInfo::OP_None);
274       Cost += getArithmeticInstrCost(Instruction::Add, Ty, CostKind, Op1Info,
275                                      Op2Info,
276                                      TargetTransformInfo::OP_None,
277                                      TargetTransformInfo::OP_None);
278 
279       if (ISD == ISD::SREM) {
280         // For SREM: (X % C) is the equivalent of (X - (X/C)*C)
281         Cost += getArithmeticInstrCost(Instruction::Mul, Ty, CostKind, Op1Info,
282                                        Op2Info);
283         Cost += getArithmeticInstrCost(Instruction::Sub, Ty, CostKind, Op1Info,
284                                        Op2Info);
285       }
286 
287       return Cost;
288     }
289 
290     // Vector unsigned division/remainder will be simplified to shifts/masks.
291     if (ISD == ISD::UDIV)
292       return getArithmeticInstrCost(Instruction::LShr, Ty, CostKind,
293                                     Op1Info, Op2Info,
294                                     TargetTransformInfo::OP_None,
295                                     TargetTransformInfo::OP_None);
296 
297     else // UREM
298       return getArithmeticInstrCost(Instruction::And, Ty, CostKind,
299                                     Op1Info, Op2Info,
300                                     TargetTransformInfo::OP_None,
301                                     TargetTransformInfo::OP_None);
302   }
303 
304   static const CostTblEntry AVX512BWUniformConstCostTable[] = {
305     { ISD::SHL,  MVT::v64i8,   2 }, // psllw + pand.
306     { ISD::SRL,  MVT::v64i8,   2 }, // psrlw + pand.
307     { ISD::SRA,  MVT::v64i8,   4 }, // psrlw, pand, pxor, psubb.
308   };
309 
310   if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
311       ST->hasBWI()) {
312     if (const auto *Entry = CostTableLookup(AVX512BWUniformConstCostTable, ISD,
313                                             LT.second))
314       return LT.first * Entry->Cost;
315   }
316 
317   static const CostTblEntry AVX512UniformConstCostTable[] = {
318     { ISD::SRA,  MVT::v2i64,   1 },
319     { ISD::SRA,  MVT::v4i64,   1 },
320     { ISD::SRA,  MVT::v8i64,   1 },
321 
322     { ISD::SHL,  MVT::v64i8,   4 }, // psllw + pand.
323     { ISD::SRL,  MVT::v64i8,   4 }, // psrlw + pand.
324     { ISD::SRA,  MVT::v64i8,   8 }, // psrlw, pand, pxor, psubb.
325 
326     { ISD::SDIV, MVT::v16i32,  6 }, // pmuludq sequence
327     { ISD::SREM, MVT::v16i32,  8 }, // pmuludq+mul+sub sequence
328     { ISD::UDIV, MVT::v16i32,  5 }, // pmuludq sequence
329     { ISD::UREM, MVT::v16i32,  7 }, // pmuludq+mul+sub sequence
330   };
331 
332   if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
333       ST->hasAVX512()) {
334     if (const auto *Entry = CostTableLookup(AVX512UniformConstCostTable, ISD,
335                                             LT.second))
336       return LT.first * Entry->Cost;
337   }
338 
339   static const CostTblEntry AVX2UniformConstCostTable[] = {
340     { ISD::SHL,  MVT::v32i8,   2 }, // psllw + pand.
341     { ISD::SRL,  MVT::v32i8,   2 }, // psrlw + pand.
342     { ISD::SRA,  MVT::v32i8,   4 }, // psrlw, pand, pxor, psubb.
343 
344     { ISD::SRA,  MVT::v4i64,   4 }, // 2 x psrad + shuffle.
345 
346     { ISD::SDIV, MVT::v8i32,   6 }, // pmuludq sequence
347     { ISD::SREM, MVT::v8i32,   8 }, // pmuludq+mul+sub sequence
348     { ISD::UDIV, MVT::v8i32,   5 }, // pmuludq sequence
349     { ISD::UREM, MVT::v8i32,   7 }, // pmuludq+mul+sub sequence
350   };
351 
352   if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
353       ST->hasAVX2()) {
354     if (const auto *Entry = CostTableLookup(AVX2UniformConstCostTable, ISD,
355                                             LT.second))
356       return LT.first * Entry->Cost;
357   }
358 
359   static const CostTblEntry SSE2UniformConstCostTable[] = {
360     { ISD::SHL,  MVT::v16i8,     2 }, // psllw + pand.
361     { ISD::SRL,  MVT::v16i8,     2 }, // psrlw + pand.
362     { ISD::SRA,  MVT::v16i8,     4 }, // psrlw, pand, pxor, psubb.
363 
364     { ISD::SHL,  MVT::v32i8,   4+2 }, // 2*(psllw + pand) + split.
365     { ISD::SRL,  MVT::v32i8,   4+2 }, // 2*(psrlw + pand) + split.
366     { ISD::SRA,  MVT::v32i8,   8+2 }, // 2*(psrlw, pand, pxor, psubb) + split.
367 
368     { ISD::SDIV, MVT::v8i32,  12+2 }, // 2*pmuludq sequence + split.
369     { ISD::SREM, MVT::v8i32,  16+2 }, // 2*pmuludq+mul+sub sequence + split.
370     { ISD::SDIV, MVT::v4i32,     6 }, // pmuludq sequence
371     { ISD::SREM, MVT::v4i32,     8 }, // pmuludq+mul+sub sequence
372     { ISD::UDIV, MVT::v8i32,  10+2 }, // 2*pmuludq sequence + split.
373     { ISD::UREM, MVT::v8i32,  14+2 }, // 2*pmuludq+mul+sub sequence + split.
374     { ISD::UDIV, MVT::v4i32,     5 }, // pmuludq sequence
375     { ISD::UREM, MVT::v4i32,     7 }, // pmuludq+mul+sub sequence
376   };
377 
378   // XOP has faster vXi8 shifts.
379   if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
380       ST->hasSSE2() && !ST->hasXOP()) {
381     if (const auto *Entry =
382             CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second))
383       return LT.first * Entry->Cost;
384   }
385 
386   static const CostTblEntry AVX512BWConstCostTable[] = {
387     { ISD::SDIV, MVT::v64i8,  14 }, // 2*ext+2*pmulhw sequence
388     { ISD::SREM, MVT::v64i8,  16 }, // 2*ext+2*pmulhw+mul+sub sequence
389     { ISD::UDIV, MVT::v64i8,  14 }, // 2*ext+2*pmulhw sequence
390     { ISD::UREM, MVT::v64i8,  16 }, // 2*ext+2*pmulhw+mul+sub sequence
391     { ISD::SDIV, MVT::v32i16,  6 }, // vpmulhw sequence
392     { ISD::SREM, MVT::v32i16,  8 }, // vpmulhw+mul+sub sequence
393     { ISD::UDIV, MVT::v32i16,  6 }, // vpmulhuw sequence
394     { ISD::UREM, MVT::v32i16,  8 }, // vpmulhuw+mul+sub sequence
395   };
396 
397   if ((Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
398        Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
399       ST->hasBWI()) {
400     if (const auto *Entry =
401             CostTableLookup(AVX512BWConstCostTable, ISD, LT.second))
402       return LT.first * Entry->Cost;
403   }
404 
405   static const CostTblEntry AVX512ConstCostTable[] = {
406     { ISD::SDIV, MVT::v16i32, 15 }, // vpmuldq sequence
407     { ISD::SREM, MVT::v16i32, 17 }, // vpmuldq+mul+sub sequence
408     { ISD::UDIV, MVT::v16i32, 15 }, // vpmuludq sequence
409     { ISD::UREM, MVT::v16i32, 17 }, // vpmuludq+mul+sub sequence
410     { ISD::SDIV, MVT::v64i8,  28 }, // 4*ext+4*pmulhw sequence
411     { ISD::SREM, MVT::v64i8,  32 }, // 4*ext+4*pmulhw+mul+sub sequence
412     { ISD::UDIV, MVT::v64i8,  28 }, // 4*ext+4*pmulhw sequence
413     { ISD::UREM, MVT::v64i8,  32 }, // 4*ext+4*pmulhw+mul+sub sequence
414     { ISD::SDIV, MVT::v32i16, 12 }, // 2*vpmulhw sequence
415     { ISD::SREM, MVT::v32i16, 16 }, // 2*vpmulhw+mul+sub sequence
416     { ISD::UDIV, MVT::v32i16, 12 }, // 2*vpmulhuw sequence
417     { ISD::UREM, MVT::v32i16, 16 }, // 2*vpmulhuw+mul+sub sequence
418   };
419 
420   if ((Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
421        Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
422       ST->hasAVX512()) {
423     if (const auto *Entry =
424             CostTableLookup(AVX512ConstCostTable, ISD, LT.second))
425       return LT.first * Entry->Cost;
426   }
427 
428   static const CostTblEntry AVX2ConstCostTable[] = {
429     { ISD::SDIV, MVT::v32i8,  14 }, // 2*ext+2*pmulhw sequence
430     { ISD::SREM, MVT::v32i8,  16 }, // 2*ext+2*pmulhw+mul+sub sequence
431     { ISD::UDIV, MVT::v32i8,  14 }, // 2*ext+2*pmulhw sequence
432     { ISD::UREM, MVT::v32i8,  16 }, // 2*ext+2*pmulhw+mul+sub sequence
433     { ISD::SDIV, MVT::v16i16,  6 }, // vpmulhw sequence
434     { ISD::SREM, MVT::v16i16,  8 }, // vpmulhw+mul+sub sequence
435     { ISD::UDIV, MVT::v16i16,  6 }, // vpmulhuw sequence
436     { ISD::UREM, MVT::v16i16,  8 }, // vpmulhuw+mul+sub sequence
437     { ISD::SDIV, MVT::v8i32,  15 }, // vpmuldq sequence
438     { ISD::SREM, MVT::v8i32,  19 }, // vpmuldq+mul+sub sequence
439     { ISD::UDIV, MVT::v8i32,  15 }, // vpmuludq sequence
440     { ISD::UREM, MVT::v8i32,  19 }, // vpmuludq+mul+sub sequence
441   };
442 
443   if ((Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
444        Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
445       ST->hasAVX2()) {
446     if (const auto *Entry = CostTableLookup(AVX2ConstCostTable, ISD, LT.second))
447       return LT.first * Entry->Cost;
448   }
449 
450   static const CostTblEntry SSE2ConstCostTable[] = {
451     { ISD::SDIV, MVT::v32i8,  28+2 }, // 4*ext+4*pmulhw sequence + split.
452     { ISD::SREM, MVT::v32i8,  32+2 }, // 4*ext+4*pmulhw+mul+sub sequence + split.
453     { ISD::SDIV, MVT::v16i8,    14 }, // 2*ext+2*pmulhw sequence
454     { ISD::SREM, MVT::v16i8,    16 }, // 2*ext+2*pmulhw+mul+sub sequence
455     { ISD::UDIV, MVT::v32i8,  28+2 }, // 4*ext+4*pmulhw sequence + split.
456     { ISD::UREM, MVT::v32i8,  32+2 }, // 4*ext+4*pmulhw+mul+sub sequence + split.
457     { ISD::UDIV, MVT::v16i8,    14 }, // 2*ext+2*pmulhw sequence
458     { ISD::UREM, MVT::v16i8,    16 }, // 2*ext+2*pmulhw+mul+sub sequence
459     { ISD::SDIV, MVT::v16i16, 12+2 }, // 2*pmulhw sequence + split.
460     { ISD::SREM, MVT::v16i16, 16+2 }, // 2*pmulhw+mul+sub sequence + split.
461     { ISD::SDIV, MVT::v8i16,     6 }, // pmulhw sequence
462     { ISD::SREM, MVT::v8i16,     8 }, // pmulhw+mul+sub sequence
463     { ISD::UDIV, MVT::v16i16, 12+2 }, // 2*pmulhuw sequence + split.
464     { ISD::UREM, MVT::v16i16, 16+2 }, // 2*pmulhuw+mul+sub sequence + split.
465     { ISD::UDIV, MVT::v8i16,     6 }, // pmulhuw sequence
466     { ISD::UREM, MVT::v8i16,     8 }, // pmulhuw+mul+sub sequence
467     { ISD::SDIV, MVT::v8i32,  38+2 }, // 2*pmuludq sequence + split.
468     { ISD::SREM, MVT::v8i32,  48+2 }, // 2*pmuludq+mul+sub sequence + split.
469     { ISD::SDIV, MVT::v4i32,    19 }, // pmuludq sequence
470     { ISD::SREM, MVT::v4i32,    24 }, // pmuludq+mul+sub sequence
471     { ISD::UDIV, MVT::v8i32,  30+2 }, // 2*pmuludq sequence + split.
472     { ISD::UREM, MVT::v8i32,  40+2 }, // 2*pmuludq+mul+sub sequence + split.
473     { ISD::UDIV, MVT::v4i32,    15 }, // pmuludq sequence
474     { ISD::UREM, MVT::v4i32,    20 }, // pmuludq+mul+sub sequence
475   };
476 
477   if ((Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
478        Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
479       ST->hasSSE2()) {
480     // pmuldq sequence.
481     if (ISD == ISD::SDIV && LT.second == MVT::v8i32 && ST->hasAVX())
482       return LT.first * 32;
483     if (ISD == ISD::SREM && LT.second == MVT::v8i32 && ST->hasAVX())
484       return LT.first * 38;
485     if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41())
486       return LT.first * 15;
487     if (ISD == ISD::SREM && LT.second == MVT::v4i32 && ST->hasSSE41())
488       return LT.first * 20;
489 
490     if (const auto *Entry = CostTableLookup(SSE2ConstCostTable, ISD, LT.second))
491       return LT.first * Entry->Cost;
492   }
493 
494   static const CostTblEntry AVX512BWShiftCostTable[] = {
495     { ISD::SHL,   MVT::v8i16,      1 }, // vpsllvw
496     { ISD::SRL,   MVT::v8i16,      1 }, // vpsrlvw
497     { ISD::SRA,   MVT::v8i16,      1 }, // vpsravw
498 
499     { ISD::SHL,   MVT::v16i16,     1 }, // vpsllvw
500     { ISD::SRL,   MVT::v16i16,     1 }, // vpsrlvw
501     { ISD::SRA,   MVT::v16i16,     1 }, // vpsravw
502 
503     { ISD::SHL,   MVT::v32i16,     1 }, // vpsllvw
504     { ISD::SRL,   MVT::v32i16,     1 }, // vpsrlvw
505     { ISD::SRA,   MVT::v32i16,     1 }, // vpsravw
506   };
507 
508   if (ST->hasBWI())
509     if (const auto *Entry = CostTableLookup(AVX512BWShiftCostTable, ISD, LT.second))
510       return LT.first * Entry->Cost;
511 
512   static const CostTblEntry AVX2UniformCostTable[] = {
513     // Uniform splats are cheaper for the following instructions.
514     { ISD::SHL,  MVT::v16i16, 1 }, // psllw.
515     { ISD::SRL,  MVT::v16i16, 1 }, // psrlw.
516     { ISD::SRA,  MVT::v16i16, 1 }, // psraw.
517     { ISD::SHL,  MVT::v32i16, 2 }, // 2*psllw.
518     { ISD::SRL,  MVT::v32i16, 2 }, // 2*psrlw.
519     { ISD::SRA,  MVT::v32i16, 2 }, // 2*psraw.
520   };
521 
522   if (ST->hasAVX2() &&
523       ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) ||
524        (Op2Info == TargetTransformInfo::OK_UniformValue))) {
525     if (const auto *Entry =
526             CostTableLookup(AVX2UniformCostTable, ISD, LT.second))
527       return LT.first * Entry->Cost;
528   }
529 
530   static const CostTblEntry SSE2UniformCostTable[] = {
531     // Uniform splats are cheaper for the following instructions.
532     { ISD::SHL,  MVT::v8i16,  1 }, // psllw.
533     { ISD::SHL,  MVT::v4i32,  1 }, // pslld
534     { ISD::SHL,  MVT::v2i64,  1 }, // psllq.
535 
536     { ISD::SRL,  MVT::v8i16,  1 }, // psrlw.
537     { ISD::SRL,  MVT::v4i32,  1 }, // psrld.
538     { ISD::SRL,  MVT::v2i64,  1 }, // psrlq.
539 
540     { ISD::SRA,  MVT::v8i16,  1 }, // psraw.
541     { ISD::SRA,  MVT::v4i32,  1 }, // psrad.
542   };
543 
544   if (ST->hasSSE2() &&
545       ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) ||
546        (Op2Info == TargetTransformInfo::OK_UniformValue))) {
547     if (const auto *Entry =
548             CostTableLookup(SSE2UniformCostTable, ISD, LT.second))
549       return LT.first * Entry->Cost;
550   }
551 
552   static const CostTblEntry AVX512DQCostTable[] = {
553     { ISD::MUL,  MVT::v2i64, 1 },
554     { ISD::MUL,  MVT::v4i64, 1 },
555     { ISD::MUL,  MVT::v8i64, 1 }
556   };
557 
558   // Look for AVX512DQ lowering tricks for custom cases.
559   if (ST->hasDQI())
560     if (const auto *Entry = CostTableLookup(AVX512DQCostTable, ISD, LT.second))
561       return LT.first * Entry->Cost;
562 
563   static const CostTblEntry AVX512BWCostTable[] = {
564     { ISD::SHL,   MVT::v64i8,     11 }, // vpblendvb sequence.
565     { ISD::SRL,   MVT::v64i8,     11 }, // vpblendvb sequence.
566     { ISD::SRA,   MVT::v64i8,     24 }, // vpblendvb sequence.
567 
568     { ISD::MUL,   MVT::v64i8,     11 }, // extend/pmullw/trunc sequence.
569     { ISD::MUL,   MVT::v32i8,      4 }, // extend/pmullw/trunc sequence.
570     { ISD::MUL,   MVT::v16i8,      4 }, // extend/pmullw/trunc sequence.
571   };
572 
573   // Look for AVX512BW lowering tricks for custom cases.
574   if (ST->hasBWI())
575     if (const auto *Entry = CostTableLookup(AVX512BWCostTable, ISD, LT.second))
576       return LT.first * Entry->Cost;
577 
578   static const CostTblEntry AVX512CostTable[] = {
579     { ISD::SHL,     MVT::v16i32,     1 },
580     { ISD::SRL,     MVT::v16i32,     1 },
581     { ISD::SRA,     MVT::v16i32,     1 },
582 
583     { ISD::SHL,     MVT::v8i64,      1 },
584     { ISD::SRL,     MVT::v8i64,      1 },
585 
586     { ISD::SRA,     MVT::v2i64,      1 },
587     { ISD::SRA,     MVT::v4i64,      1 },
588     { ISD::SRA,     MVT::v8i64,      1 },
589 
590     { ISD::MUL,     MVT::v64i8,     26 }, // extend/pmullw/trunc sequence.
591     { ISD::MUL,     MVT::v32i8,     13 }, // extend/pmullw/trunc sequence.
592     { ISD::MUL,     MVT::v16i8,      5 }, // extend/pmullw/trunc sequence.
593     { ISD::MUL,     MVT::v16i32,     1 }, // pmulld (Skylake from agner.org)
594     { ISD::MUL,     MVT::v8i32,      1 }, // pmulld (Skylake from agner.org)
595     { ISD::MUL,     MVT::v4i32,      1 }, // pmulld (Skylake from agner.org)
596     { ISD::MUL,     MVT::v8i64,      8 }, // 3*pmuludq/3*shift/2*add
597 
598     { ISD::FADD,    MVT::v8f64,      1 }, // Skylake from http://www.agner.org/
599     { ISD::FSUB,    MVT::v8f64,      1 }, // Skylake from http://www.agner.org/
600     { ISD::FMUL,    MVT::v8f64,      1 }, // Skylake from http://www.agner.org/
601 
602     { ISD::FADD,    MVT::v16f32,     1 }, // Skylake from http://www.agner.org/
603     { ISD::FSUB,    MVT::v16f32,     1 }, // Skylake from http://www.agner.org/
604     { ISD::FMUL,    MVT::v16f32,     1 }, // Skylake from http://www.agner.org/
605   };
606 
607   if (ST->hasAVX512())
608     if (const auto *Entry = CostTableLookup(AVX512CostTable, ISD, LT.second))
609       return LT.first * Entry->Cost;
610 
611   static const CostTblEntry AVX2ShiftCostTable[] = {
612     // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
613     // customize them to detect the cases where shift amount is a scalar one.
614     { ISD::SHL,     MVT::v4i32,    1 },
615     { ISD::SRL,     MVT::v4i32,    1 },
616     { ISD::SRA,     MVT::v4i32,    1 },
617     { ISD::SHL,     MVT::v8i32,    1 },
618     { ISD::SRL,     MVT::v8i32,    1 },
619     { ISD::SRA,     MVT::v8i32,    1 },
620     { ISD::SHL,     MVT::v2i64,    1 },
621     { ISD::SRL,     MVT::v2i64,    1 },
622     { ISD::SHL,     MVT::v4i64,    1 },
623     { ISD::SRL,     MVT::v4i64,    1 },
624   };
625 
626   if (ST->hasAVX512()) {
627     if (ISD == ISD::SHL && LT.second == MVT::v32i16 &&
628         (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
629          Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
630       // On AVX512, a packed v32i16 shift left by a constant build_vector
631       // is lowered into a vector multiply (vpmullw).
632       return getArithmeticInstrCost(Instruction::Mul, Ty, CostKind,
633                                     Op1Info, Op2Info,
634                                     TargetTransformInfo::OP_None,
635                                     TargetTransformInfo::OP_None);
636   }
637 
638   // Look for AVX2 lowering tricks.
639   if (ST->hasAVX2()) {
640     if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
641         (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
642          Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
643       // On AVX2, a packed v16i16 shift left by a constant build_vector
644       // is lowered into a vector multiply (vpmullw).
645       return getArithmeticInstrCost(Instruction::Mul, Ty, CostKind,
646                                     Op1Info, Op2Info,
647                                     TargetTransformInfo::OP_None,
648                                     TargetTransformInfo::OP_None);
649 
650     if (const auto *Entry = CostTableLookup(AVX2ShiftCostTable, ISD, LT.second))
651       return LT.first * Entry->Cost;
652   }
653 
654   static const CostTblEntry XOPShiftCostTable[] = {
655     // 128bit shifts take 1cy, but right shifts require negation beforehand.
656     { ISD::SHL,     MVT::v16i8,    1 },
657     { ISD::SRL,     MVT::v16i8,    2 },
658     { ISD::SRA,     MVT::v16i8,    2 },
659     { ISD::SHL,     MVT::v8i16,    1 },
660     { ISD::SRL,     MVT::v8i16,    2 },
661     { ISD::SRA,     MVT::v8i16,    2 },
662     { ISD::SHL,     MVT::v4i32,    1 },
663     { ISD::SRL,     MVT::v4i32,    2 },
664     { ISD::SRA,     MVT::v4i32,    2 },
665     { ISD::SHL,     MVT::v2i64,    1 },
666     { ISD::SRL,     MVT::v2i64,    2 },
667     { ISD::SRA,     MVT::v2i64,    2 },
668     // 256bit shifts require splitting if AVX2 didn't catch them above.
669     { ISD::SHL,     MVT::v32i8,  2+2 },
670     { ISD::SRL,     MVT::v32i8,  4+2 },
671     { ISD::SRA,     MVT::v32i8,  4+2 },
672     { ISD::SHL,     MVT::v16i16, 2+2 },
673     { ISD::SRL,     MVT::v16i16, 4+2 },
674     { ISD::SRA,     MVT::v16i16, 4+2 },
675     { ISD::SHL,     MVT::v8i32,  2+2 },
676     { ISD::SRL,     MVT::v8i32,  4+2 },
677     { ISD::SRA,     MVT::v8i32,  4+2 },
678     { ISD::SHL,     MVT::v4i64,  2+2 },
679     { ISD::SRL,     MVT::v4i64,  4+2 },
680     { ISD::SRA,     MVT::v4i64,  4+2 },
681   };
682 
683   // Look for XOP lowering tricks.
684   if (ST->hasXOP()) {
685     // If the right shift is constant then we'll fold the negation so
686     // it's as cheap as a left shift.
687     int ShiftISD = ISD;
688     if ((ShiftISD == ISD::SRL || ShiftISD == ISD::SRA) &&
689         (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
690          Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
691       ShiftISD = ISD::SHL;
692     if (const auto *Entry =
693             CostTableLookup(XOPShiftCostTable, ShiftISD, LT.second))
694       return LT.first * Entry->Cost;
695   }
696 
697   static const CostTblEntry SSE2UniformShiftCostTable[] = {
698     // Uniform splats are cheaper for the following instructions.
699     { ISD::SHL,  MVT::v16i16, 2+2 }, // 2*psllw + split.
700     { ISD::SHL,  MVT::v8i32,  2+2 }, // 2*pslld + split.
701     { ISD::SHL,  MVT::v4i64,  2+2 }, // 2*psllq + split.
702 
703     { ISD::SRL,  MVT::v16i16, 2+2 }, // 2*psrlw + split.
704     { ISD::SRL,  MVT::v8i32,  2+2 }, // 2*psrld + split.
705     { ISD::SRL,  MVT::v4i64,  2+2 }, // 2*psrlq + split.
706 
707     { ISD::SRA,  MVT::v16i16, 2+2 }, // 2*psraw + split.
708     { ISD::SRA,  MVT::v8i32,  2+2 }, // 2*psrad + split.
709     { ISD::SRA,  MVT::v2i64,    4 }, // 2*psrad + shuffle.
710     { ISD::SRA,  MVT::v4i64,  8+2 }, // 2*(2*psrad + shuffle) + split.
711   };
712 
713   if (ST->hasSSE2() &&
714       ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) ||
715        (Op2Info == TargetTransformInfo::OK_UniformValue))) {
716 
717     // Handle AVX2 uniform v4i64 ISD::SRA, it's not worth a table.
718     if (ISD == ISD::SRA && LT.second == MVT::v4i64 && ST->hasAVX2())
719       return LT.first * 4; // 2*psrad + shuffle.
720 
721     if (const auto *Entry =
722             CostTableLookup(SSE2UniformShiftCostTable, ISD, LT.second))
723       return LT.first * Entry->Cost;
724   }
725 
726   if (ISD == ISD::SHL &&
727       Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
728     MVT VT = LT.second;
729     // Vector shift left by non uniform constant can be lowered
730     // into vector multiply.
731     if (((VT == MVT::v8i16 || VT == MVT::v4i32) && ST->hasSSE2()) ||
732         ((VT == MVT::v16i16 || VT == MVT::v8i32) && ST->hasAVX()))
733       ISD = ISD::MUL;
734   }
735 
736   static const CostTblEntry AVX2CostTable[] = {
737     { ISD::SHL,  MVT::v32i8,     11 }, // vpblendvb sequence.
738     { ISD::SHL,  MVT::v64i8,     22 }, // 2*vpblendvb sequence.
739     { ISD::SHL,  MVT::v16i16,    10 }, // extend/vpsrlvd/pack sequence.
740     { ISD::SHL,  MVT::v32i16,    20 }, // 2*extend/vpsrlvd/pack sequence.
741 
742     { ISD::SRL,  MVT::v32i8,     11 }, // vpblendvb sequence.
743     { ISD::SRL,  MVT::v64i8,     22 }, // 2*vpblendvb sequence.
744     { ISD::SRL,  MVT::v16i16,    10 }, // extend/vpsrlvd/pack sequence.
745     { ISD::SRL,  MVT::v32i16,    20 }, // 2*extend/vpsrlvd/pack sequence.
746 
747     { ISD::SRA,  MVT::v32i8,     24 }, // vpblendvb sequence.
748     { ISD::SRA,  MVT::v64i8,     48 }, // 2*vpblendvb sequence.
749     { ISD::SRA,  MVT::v16i16,    10 }, // extend/vpsravd/pack sequence.
750     { ISD::SRA,  MVT::v32i16,    20 }, // 2*extend/vpsravd/pack sequence.
751     { ISD::SRA,  MVT::v2i64,      4 }, // srl/xor/sub sequence.
752     { ISD::SRA,  MVT::v4i64,      4 }, // srl/xor/sub sequence.
753 
754     { ISD::SUB,  MVT::v32i8,      1 }, // psubb
755     { ISD::ADD,  MVT::v32i8,      1 }, // paddb
756     { ISD::SUB,  MVT::v16i16,     1 }, // psubw
757     { ISD::ADD,  MVT::v16i16,     1 }, // paddw
758     { ISD::SUB,  MVT::v8i32,      1 }, // psubd
759     { ISD::ADD,  MVT::v8i32,      1 }, // paddd
760     { ISD::SUB,  MVT::v4i64,      1 }, // psubq
761     { ISD::ADD,  MVT::v4i64,      1 }, // paddq
762 
763     { ISD::MUL,  MVT::v32i8,     17 }, // extend/pmullw/trunc sequence.
764     { ISD::MUL,  MVT::v16i8,      7 }, // extend/pmullw/trunc sequence.
765     { ISD::MUL,  MVT::v16i16,     1 }, // pmullw
766     { ISD::MUL,  MVT::v8i32,      2 }, // pmulld (Haswell from agner.org)
767     { ISD::MUL,  MVT::v4i64,      8 }, // 3*pmuludq/3*shift/2*add
768 
769     { ISD::FADD, MVT::v4f64,      1 }, // Haswell from http://www.agner.org/
770     { ISD::FADD, MVT::v8f32,      1 }, // Haswell from http://www.agner.org/
771     { ISD::FSUB, MVT::v4f64,      1 }, // Haswell from http://www.agner.org/
772     { ISD::FSUB, MVT::v8f32,      1 }, // Haswell from http://www.agner.org/
773     { ISD::FMUL, MVT::v4f64,      1 }, // Haswell from http://www.agner.org/
774     { ISD::FMUL, MVT::v8f32,      1 }, // Haswell from http://www.agner.org/
775 
776     { ISD::FDIV, MVT::f32,        7 }, // Haswell from http://www.agner.org/
777     { ISD::FDIV, MVT::v4f32,      7 }, // Haswell from http://www.agner.org/
778     { ISD::FDIV, MVT::v8f32,     14 }, // Haswell from http://www.agner.org/
779     { ISD::FDIV, MVT::f64,       14 }, // Haswell from http://www.agner.org/
780     { ISD::FDIV, MVT::v2f64,     14 }, // Haswell from http://www.agner.org/
781     { ISD::FDIV, MVT::v4f64,     28 }, // Haswell from http://www.agner.org/
782   };
783 
784   // Look for AVX2 lowering tricks for custom cases.
785   if (ST->hasAVX2())
786     if (const auto *Entry = CostTableLookup(AVX2CostTable, ISD, LT.second))
787       return LT.first * Entry->Cost;
788 
789   static const CostTblEntry AVX1CostTable[] = {
790     // We don't have to scalarize unsupported ops. We can issue two half-sized
791     // operations and we only need to extract the upper YMM half.
792     // Two ops + 1 extract + 1 insert = 4.
793     { ISD::MUL,     MVT::v16i16,     4 },
794     { ISD::MUL,     MVT::v8i32,      4 },
795     { ISD::SUB,     MVT::v32i8,      4 },
796     { ISD::ADD,     MVT::v32i8,      4 },
797     { ISD::SUB,     MVT::v16i16,     4 },
798     { ISD::ADD,     MVT::v16i16,     4 },
799     { ISD::SUB,     MVT::v8i32,      4 },
800     { ISD::ADD,     MVT::v8i32,      4 },
801     { ISD::SUB,     MVT::v4i64,      4 },
802     { ISD::ADD,     MVT::v4i64,      4 },
803 
804     // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
805     // are lowered as a series of long multiplies(3), shifts(3) and adds(2)
806     // Because we believe v4i64 to be a legal type, we must also include the
807     // extract+insert in the cost table. Therefore, the cost here is 18
808     // instead of 8.
809     { ISD::MUL,     MVT::v4i64,     18 },
810 
811     { ISD::MUL,     MVT::v32i8,     26 }, // extend/pmullw/trunc sequence.
812 
813     { ISD::FDIV,    MVT::f32,       14 }, // SNB from http://www.agner.org/
814     { ISD::FDIV,    MVT::v4f32,     14 }, // SNB from http://www.agner.org/
815     { ISD::FDIV,    MVT::v8f32,     28 }, // SNB from http://www.agner.org/
816     { ISD::FDIV,    MVT::f64,       22 }, // SNB from http://www.agner.org/
817     { ISD::FDIV,    MVT::v2f64,     22 }, // SNB from http://www.agner.org/
818     { ISD::FDIV,    MVT::v4f64,     44 }, // SNB from http://www.agner.org/
819   };
820 
821   if (ST->hasAVX())
822     if (const auto *Entry = CostTableLookup(AVX1CostTable, ISD, LT.second))
823       return LT.first * Entry->Cost;
824 
825   static const CostTblEntry SSE42CostTable[] = {
826     { ISD::FADD, MVT::f64,     1 }, // Nehalem from http://www.agner.org/
827     { ISD::FADD, MVT::f32,     1 }, // Nehalem from http://www.agner.org/
828     { ISD::FADD, MVT::v2f64,   1 }, // Nehalem from http://www.agner.org/
829     { ISD::FADD, MVT::v4f32,   1 }, // Nehalem from http://www.agner.org/
830 
831     { ISD::FSUB, MVT::f64,     1 }, // Nehalem from http://www.agner.org/
832     { ISD::FSUB, MVT::f32 ,    1 }, // Nehalem from http://www.agner.org/
833     { ISD::FSUB, MVT::v2f64,   1 }, // Nehalem from http://www.agner.org/
834     { ISD::FSUB, MVT::v4f32,   1 }, // Nehalem from http://www.agner.org/
835 
836     { ISD::FMUL, MVT::f64,     1 }, // Nehalem from http://www.agner.org/
837     { ISD::FMUL, MVT::f32,     1 }, // Nehalem from http://www.agner.org/
838     { ISD::FMUL, MVT::v2f64,   1 }, // Nehalem from http://www.agner.org/
839     { ISD::FMUL, MVT::v4f32,   1 }, // Nehalem from http://www.agner.org/
840 
841     { ISD::FDIV,  MVT::f32,   14 }, // Nehalem from http://www.agner.org/
842     { ISD::FDIV,  MVT::v4f32, 14 }, // Nehalem from http://www.agner.org/
843     { ISD::FDIV,  MVT::f64,   22 }, // Nehalem from http://www.agner.org/
844     { ISD::FDIV,  MVT::v2f64, 22 }, // Nehalem from http://www.agner.org/
845   };
846 
847   if (ST->hasSSE42())
848     if (const auto *Entry = CostTableLookup(SSE42CostTable, ISD, LT.second))
849       return LT.first * Entry->Cost;
850 
851   static const CostTblEntry SSE41CostTable[] = {
852     { ISD::SHL,  MVT::v16i8,      11 }, // pblendvb sequence.
853     { ISD::SHL,  MVT::v32i8,  2*11+2 }, // pblendvb sequence + split.
854     { ISD::SHL,  MVT::v8i16,      14 }, // pblendvb sequence.
855     { ISD::SHL,  MVT::v16i16, 2*14+2 }, // pblendvb sequence + split.
856     { ISD::SHL,  MVT::v4i32,       4 }, // pslld/paddd/cvttps2dq/pmulld
857     { ISD::SHL,  MVT::v8i32,   2*4+2 }, // pslld/paddd/cvttps2dq/pmulld + split
858 
859     { ISD::SRL,  MVT::v16i8,      12 }, // pblendvb sequence.
860     { ISD::SRL,  MVT::v32i8,  2*12+2 }, // pblendvb sequence + split.
861     { ISD::SRL,  MVT::v8i16,      14 }, // pblendvb sequence.
862     { ISD::SRL,  MVT::v16i16, 2*14+2 }, // pblendvb sequence + split.
863     { ISD::SRL,  MVT::v4i32,      11 }, // Shift each lane + blend.
864     { ISD::SRL,  MVT::v8i32,  2*11+2 }, // Shift each lane + blend + split.
865 
866     { ISD::SRA,  MVT::v16i8,      24 }, // pblendvb sequence.
867     { ISD::SRA,  MVT::v32i8,  2*24+2 }, // pblendvb sequence + split.
868     { ISD::SRA,  MVT::v8i16,      14 }, // pblendvb sequence.
869     { ISD::SRA,  MVT::v16i16, 2*14+2 }, // pblendvb sequence + split.
870     { ISD::SRA,  MVT::v4i32,      12 }, // Shift each lane + blend.
871     { ISD::SRA,  MVT::v8i32,  2*12+2 }, // Shift each lane + blend + split.
872 
873     { ISD::MUL,  MVT::v4i32,       2 }  // pmulld (Nehalem from agner.org)
874   };
875 
876   if (ST->hasSSE41())
877     if (const auto *Entry = CostTableLookup(SSE41CostTable, ISD, LT.second))
878       return LT.first * Entry->Cost;
879 
880   static const CostTblEntry SSE2CostTable[] = {
881     // We don't correctly identify costs of casts because they are marked as
882     // custom.
883     { ISD::SHL,  MVT::v16i8,      26 }, // cmpgtb sequence.
884     { ISD::SHL,  MVT::v8i16,      32 }, // cmpgtb sequence.
885     { ISD::SHL,  MVT::v4i32,     2*5 }, // We optimized this using mul.
886     { ISD::SHL,  MVT::v2i64,       4 }, // splat+shuffle sequence.
887     { ISD::SHL,  MVT::v4i64,   2*4+2 }, // splat+shuffle sequence + split.
888 
889     { ISD::SRL,  MVT::v16i8,      26 }, // cmpgtb sequence.
890     { ISD::SRL,  MVT::v8i16,      32 }, // cmpgtb sequence.
891     { ISD::SRL,  MVT::v4i32,      16 }, // Shift each lane + blend.
892     { ISD::SRL,  MVT::v2i64,       4 }, // splat+shuffle sequence.
893     { ISD::SRL,  MVT::v4i64,   2*4+2 }, // splat+shuffle sequence + split.
894 
895     { ISD::SRA,  MVT::v16i8,      54 }, // unpacked cmpgtb sequence.
896     { ISD::SRA,  MVT::v8i16,      32 }, // cmpgtb sequence.
897     { ISD::SRA,  MVT::v4i32,      16 }, // Shift each lane + blend.
898     { ISD::SRA,  MVT::v2i64,      12 }, // srl/xor/sub sequence.
899     { ISD::SRA,  MVT::v4i64,  2*12+2 }, // srl/xor/sub sequence+split.
900 
901     { ISD::MUL,  MVT::v16i8,      12 }, // extend/pmullw/trunc sequence.
902     { ISD::MUL,  MVT::v8i16,       1 }, // pmullw
903     { ISD::MUL,  MVT::v4i32,       6 }, // 3*pmuludq/4*shuffle
904     { ISD::MUL,  MVT::v2i64,       8 }, // 3*pmuludq/3*shift/2*add
905 
906     { ISD::FDIV, MVT::f32,        23 }, // Pentium IV from http://www.agner.org/
907     { ISD::FDIV, MVT::v4f32,      39 }, // Pentium IV from http://www.agner.org/
908     { ISD::FDIV, MVT::f64,        38 }, // Pentium IV from http://www.agner.org/
909     { ISD::FDIV, MVT::v2f64,      69 }, // Pentium IV from http://www.agner.org/
910 
911     { ISD::FADD, MVT::f32,         2 }, // Pentium IV from http://www.agner.org/
912     { ISD::FADD, MVT::f64,         2 }, // Pentium IV from http://www.agner.org/
913 
914     { ISD::FSUB, MVT::f32,         2 }, // Pentium IV from http://www.agner.org/
915     { ISD::FSUB, MVT::f64,         2 }, // Pentium IV from http://www.agner.org/
916   };
917 
918   if (ST->hasSSE2())
919     if (const auto *Entry = CostTableLookup(SSE2CostTable, ISD, LT.second))
920       return LT.first * Entry->Cost;
921 
922   static const CostTblEntry SSE1CostTable[] = {
923     { ISD::FDIV, MVT::f32,   17 }, // Pentium III from http://www.agner.org/
924     { ISD::FDIV, MVT::v4f32, 34 }, // Pentium III from http://www.agner.org/
925 
926     { ISD::FADD, MVT::f32,    1 }, // Pentium III from http://www.agner.org/
927     { ISD::FADD, MVT::v4f32,  2 }, // Pentium III from http://www.agner.org/
928 
929     { ISD::FSUB, MVT::f32,    1 }, // Pentium III from http://www.agner.org/
930     { ISD::FSUB, MVT::v4f32,  2 }, // Pentium III from http://www.agner.org/
931 
932     { ISD::ADD, MVT::i8,      1 }, // Pentium III from http://www.agner.org/
933     { ISD::ADD, MVT::i16,     1 }, // Pentium III from http://www.agner.org/
934     { ISD::ADD, MVT::i32,     1 }, // Pentium III from http://www.agner.org/
935 
936     { ISD::SUB, MVT::i8,      1 }, // Pentium III from http://www.agner.org/
937     { ISD::SUB, MVT::i16,     1 }, // Pentium III from http://www.agner.org/
938     { ISD::SUB, MVT::i32,     1 }, // Pentium III from http://www.agner.org/
939   };
940 
941   if (ST->hasSSE1())
942     if (const auto *Entry = CostTableLookup(SSE1CostTable, ISD, LT.second))
943       return LT.first * Entry->Cost;
944 
945   // It is not a good idea to vectorize division. We have to scalarize it and
946   // in the process we will often end up having to spilling regular
947   // registers. The overhead of division is going to dominate most kernels
948   // anyways so try hard to prevent vectorization of division - it is
949   // generally a bad idea. Assume somewhat arbitrarily that we have to be able
950   // to hide "20 cycles" for each lane.
951   if (LT.second.isVector() && (ISD == ISD::SDIV || ISD == ISD::SREM ||
952                                ISD == ISD::UDIV || ISD == ISD::UREM)) {
953     InstructionCost ScalarCost = getArithmeticInstrCost(
954         Opcode, Ty->getScalarType(), CostKind, Op1Info, Op2Info,
955         TargetTransformInfo::OP_None, TargetTransformInfo::OP_None);
956     return 20 * LT.first * LT.second.getVectorNumElements() * ScalarCost;
957   }
958 
959   // Fallback to the default implementation.
960   return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info, Op2Info);
961 }
962 
963 InstructionCost X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
964                                            VectorType *BaseTp,
965                                            ArrayRef<int> Mask, int Index,
966                                            VectorType *SubTp) {
967   // 64-bit packed float vectors (v2f32) are widened to type v4f32.
968   // 64-bit packed integer vectors (v2i32) are widened to type v4i32.
969   std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, BaseTp);
970 
971   Kind = improveShuffleKindFromMask(Kind, Mask);
972   // Treat Transpose as 2-op shuffles - there's no difference in lowering.
973   if (Kind == TTI::SK_Transpose)
974     Kind = TTI::SK_PermuteTwoSrc;
975 
976   // For Broadcasts we are splatting the first element from the first input
977   // register, so only need to reference that input and all the output
978   // registers are the same.
979   if (Kind == TTI::SK_Broadcast)
980     LT.first = 1;
981 
982   // Subvector extractions are free if they start at the beginning of a
983   // vector and cheap if the subvectors are aligned.
984   if (Kind == TTI::SK_ExtractSubvector && LT.second.isVector()) {
985     int NumElts = LT.second.getVectorNumElements();
986     if ((Index % NumElts) == 0)
987       return 0;
988     std::pair<InstructionCost, MVT> SubLT =
989         TLI->getTypeLegalizationCost(DL, SubTp);
990     if (SubLT.second.isVector()) {
991       int NumSubElts = SubLT.second.getVectorNumElements();
992       if ((Index % NumSubElts) == 0 && (NumElts % NumSubElts) == 0)
993         return SubLT.first;
994       // Handle some cases for widening legalization. For now we only handle
995       // cases where the original subvector was naturally aligned and evenly
996       // fit in its legalized subvector type.
997       // FIXME: Remove some of the alignment restrictions.
998       // FIXME: We can use permq for 64-bit or larger extracts from 256-bit
999       // vectors.
1000       int OrigSubElts = cast<FixedVectorType>(SubTp)->getNumElements();
1001       if (NumSubElts > OrigSubElts && (Index % OrigSubElts) == 0 &&
1002           (NumSubElts % OrigSubElts) == 0 &&
1003           LT.second.getVectorElementType() ==
1004               SubLT.second.getVectorElementType() &&
1005           LT.second.getVectorElementType().getSizeInBits() ==
1006               BaseTp->getElementType()->getPrimitiveSizeInBits()) {
1007         assert(NumElts >= NumSubElts && NumElts > OrigSubElts &&
1008                "Unexpected number of elements!");
1009         auto *VecTy = FixedVectorType::get(BaseTp->getElementType(),
1010                                            LT.second.getVectorNumElements());
1011         auto *SubTy = FixedVectorType::get(BaseTp->getElementType(),
1012                                            SubLT.second.getVectorNumElements());
1013         int ExtractIndex = alignDown((Index % NumElts), NumSubElts);
1014         InstructionCost ExtractCost = getShuffleCost(
1015             TTI::SK_ExtractSubvector, VecTy, None, ExtractIndex, SubTy);
1016 
1017         // If the original size is 32-bits or more, we can use pshufd. Otherwise
1018         // if we have SSSE3 we can use pshufb.
1019         if (SubTp->getPrimitiveSizeInBits() >= 32 || ST->hasSSSE3())
1020           return ExtractCost + 1; // pshufd or pshufb
1021 
1022         assert(SubTp->getPrimitiveSizeInBits() == 16 &&
1023                "Unexpected vector size");
1024 
1025         return ExtractCost + 2; // worst case pshufhw + pshufd
1026       }
1027     }
1028   }
1029 
1030   // Subvector insertions are cheap if the subvectors are aligned.
1031   // Note that in general, the insertion starting at the beginning of a vector
1032   // isn't free, because we need to preserve the rest of the wide vector.
1033   if (Kind == TTI::SK_InsertSubvector && LT.second.isVector()) {
1034     int NumElts = LT.second.getVectorNumElements();
1035     std::pair<InstructionCost, MVT> SubLT =
1036         TLI->getTypeLegalizationCost(DL, SubTp);
1037     if (SubLT.second.isVector()) {
1038       int NumSubElts = SubLT.second.getVectorNumElements();
1039       if ((Index % NumSubElts) == 0 && (NumElts % NumSubElts) == 0)
1040         return SubLT.first;
1041     }
1042   }
1043 
1044   // Handle some common (illegal) sub-vector types as they are often very cheap
1045   // to shuffle even on targets without PSHUFB.
1046   EVT VT = TLI->getValueType(DL, BaseTp);
1047   if (VT.isSimple() && VT.isVector() && VT.getSizeInBits() < 128 &&
1048       !ST->hasSSSE3()) {
1049      static const CostTblEntry SSE2SubVectorShuffleTbl[] = {
1050       {TTI::SK_Broadcast,        MVT::v4i16, 1}, // pshuflw
1051       {TTI::SK_Broadcast,        MVT::v2i16, 1}, // pshuflw
1052       {TTI::SK_Broadcast,        MVT::v8i8,  2}, // punpck/pshuflw
1053       {TTI::SK_Broadcast,        MVT::v4i8,  2}, // punpck/pshuflw
1054       {TTI::SK_Broadcast,        MVT::v2i8,  1}, // punpck
1055 
1056       {TTI::SK_Reverse,          MVT::v4i16, 1}, // pshuflw
1057       {TTI::SK_Reverse,          MVT::v2i16, 1}, // pshuflw
1058       {TTI::SK_Reverse,          MVT::v4i8,  3}, // punpck/pshuflw/packus
1059       {TTI::SK_Reverse,          MVT::v2i8,  1}, // punpck
1060 
1061       {TTI::SK_PermuteTwoSrc,    MVT::v4i16, 2}, // punpck/pshuflw
1062       {TTI::SK_PermuteTwoSrc,    MVT::v2i16, 2}, // punpck/pshuflw
1063       {TTI::SK_PermuteTwoSrc,    MVT::v8i8,  7}, // punpck/pshuflw
1064       {TTI::SK_PermuteTwoSrc,    MVT::v4i8,  4}, // punpck/pshuflw
1065       {TTI::SK_PermuteTwoSrc,    MVT::v2i8,  2}, // punpck
1066 
1067       {TTI::SK_PermuteSingleSrc, MVT::v4i16, 1}, // pshuflw
1068       {TTI::SK_PermuteSingleSrc, MVT::v2i16, 1}, // pshuflw
1069       {TTI::SK_PermuteSingleSrc, MVT::v8i8,  5}, // punpck/pshuflw
1070       {TTI::SK_PermuteSingleSrc, MVT::v4i8,  3}, // punpck/pshuflw
1071       {TTI::SK_PermuteSingleSrc, MVT::v2i8,  1}, // punpck
1072     };
1073 
1074     if (ST->hasSSE2())
1075       if (const auto *Entry =
1076               CostTableLookup(SSE2SubVectorShuffleTbl, Kind, VT.getSimpleVT()))
1077         return Entry->Cost;
1078   }
1079 
1080   // We are going to permute multiple sources and the result will be in multiple
1081   // destinations. Providing an accurate cost only for splits where the element
1082   // type remains the same.
1083   if (Kind == TTI::SK_PermuteSingleSrc && LT.first != 1) {
1084     MVT LegalVT = LT.second;
1085     if (LegalVT.isVector() &&
1086         LegalVT.getVectorElementType().getSizeInBits() ==
1087             BaseTp->getElementType()->getPrimitiveSizeInBits() &&
1088         LegalVT.getVectorNumElements() <
1089             cast<FixedVectorType>(BaseTp)->getNumElements()) {
1090 
1091       unsigned VecTySize = DL.getTypeStoreSize(BaseTp);
1092       unsigned LegalVTSize = LegalVT.getStoreSize();
1093       // Number of source vectors after legalization:
1094       unsigned NumOfSrcs = (VecTySize + LegalVTSize - 1) / LegalVTSize;
1095       // Number of destination vectors after legalization:
1096       InstructionCost NumOfDests = LT.first;
1097 
1098       auto *SingleOpTy = FixedVectorType::get(BaseTp->getElementType(),
1099                                               LegalVT.getVectorNumElements());
1100 
1101       InstructionCost NumOfShuffles = (NumOfSrcs - 1) * NumOfDests;
1102       return NumOfShuffles * getShuffleCost(TTI::SK_PermuteTwoSrc, SingleOpTy,
1103                                             None, 0, nullptr);
1104     }
1105 
1106     return BaseT::getShuffleCost(Kind, BaseTp, Mask, Index, SubTp);
1107   }
1108 
1109   // For 2-input shuffles, we must account for splitting the 2 inputs into many.
1110   if (Kind == TTI::SK_PermuteTwoSrc && LT.first != 1) {
1111     // We assume that source and destination have the same vector type.
1112     InstructionCost NumOfDests = LT.first;
1113     InstructionCost NumOfShufflesPerDest = LT.first * 2 - 1;
1114     LT.first = NumOfDests * NumOfShufflesPerDest;
1115   }
1116 
1117   static const CostTblEntry AVX512VBMIShuffleTbl[] = {
1118       {TTI::SK_Reverse, MVT::v64i8, 1}, // vpermb
1119       {TTI::SK_Reverse, MVT::v32i8, 1}, // vpermb
1120 
1121       {TTI::SK_PermuteSingleSrc, MVT::v64i8, 1}, // vpermb
1122       {TTI::SK_PermuteSingleSrc, MVT::v32i8, 1}, // vpermb
1123 
1124       {TTI::SK_PermuteTwoSrc, MVT::v64i8, 2}, // vpermt2b
1125       {TTI::SK_PermuteTwoSrc, MVT::v32i8, 2}, // vpermt2b
1126       {TTI::SK_PermuteTwoSrc, MVT::v16i8, 2}  // vpermt2b
1127   };
1128 
1129   if (ST->hasVBMI())
1130     if (const auto *Entry =
1131             CostTableLookup(AVX512VBMIShuffleTbl, Kind, LT.second))
1132       return LT.first * Entry->Cost;
1133 
1134   static const CostTblEntry AVX512BWShuffleTbl[] = {
1135       {TTI::SK_Broadcast, MVT::v32i16, 1}, // vpbroadcastw
1136       {TTI::SK_Broadcast, MVT::v64i8, 1},  // vpbroadcastb
1137 
1138       {TTI::SK_Reverse, MVT::v32i16, 2}, // vpermw
1139       {TTI::SK_Reverse, MVT::v16i16, 2}, // vpermw
1140       {TTI::SK_Reverse, MVT::v64i8, 2},  // pshufb + vshufi64x2
1141 
1142       {TTI::SK_PermuteSingleSrc, MVT::v32i16, 2}, // vpermw
1143       {TTI::SK_PermuteSingleSrc, MVT::v16i16, 2}, // vpermw
1144       {TTI::SK_PermuteSingleSrc, MVT::v64i8, 8},  // extend to v32i16
1145 
1146       {TTI::SK_PermuteTwoSrc, MVT::v32i16, 2}, // vpermt2w
1147       {TTI::SK_PermuteTwoSrc, MVT::v16i16, 2}, // vpermt2w
1148       {TTI::SK_PermuteTwoSrc, MVT::v8i16, 2},  // vpermt2w
1149       {TTI::SK_PermuteTwoSrc, MVT::v64i8, 19}, // 6 * v32i8 + 1
1150 
1151       {TTI::SK_Select, MVT::v32i16, 1}, // vblendmw
1152       {TTI::SK_Select, MVT::v64i8,  1}, // vblendmb
1153   };
1154 
1155   if (ST->hasBWI())
1156     if (const auto *Entry =
1157             CostTableLookup(AVX512BWShuffleTbl, Kind, LT.second))
1158       return LT.first * Entry->Cost;
1159 
1160   static const CostTblEntry AVX512ShuffleTbl[] = {
1161       {TTI::SK_Broadcast, MVT::v8f64, 1},  // vbroadcastpd
1162       {TTI::SK_Broadcast, MVT::v16f32, 1}, // vbroadcastps
1163       {TTI::SK_Broadcast, MVT::v8i64, 1},  // vpbroadcastq
1164       {TTI::SK_Broadcast, MVT::v16i32, 1}, // vpbroadcastd
1165       {TTI::SK_Broadcast, MVT::v32i16, 1}, // vpbroadcastw
1166       {TTI::SK_Broadcast, MVT::v64i8, 1},  // vpbroadcastb
1167 
1168       {TTI::SK_Reverse, MVT::v8f64, 1},  // vpermpd
1169       {TTI::SK_Reverse, MVT::v16f32, 1}, // vpermps
1170       {TTI::SK_Reverse, MVT::v8i64, 1},  // vpermq
1171       {TTI::SK_Reverse, MVT::v16i32, 1}, // vpermd
1172       {TTI::SK_Reverse, MVT::v32i16, 7}, // per mca
1173       {TTI::SK_Reverse, MVT::v64i8,  7}, // per mca
1174 
1175       {TTI::SK_PermuteSingleSrc, MVT::v8f64, 1},  // vpermpd
1176       {TTI::SK_PermuteSingleSrc, MVT::v4f64, 1},  // vpermpd
1177       {TTI::SK_PermuteSingleSrc, MVT::v2f64, 1},  // vpermpd
1178       {TTI::SK_PermuteSingleSrc, MVT::v16f32, 1}, // vpermps
1179       {TTI::SK_PermuteSingleSrc, MVT::v8f32, 1},  // vpermps
1180       {TTI::SK_PermuteSingleSrc, MVT::v4f32, 1},  // vpermps
1181       {TTI::SK_PermuteSingleSrc, MVT::v8i64, 1},  // vpermq
1182       {TTI::SK_PermuteSingleSrc, MVT::v4i64, 1},  // vpermq
1183       {TTI::SK_PermuteSingleSrc, MVT::v2i64, 1},  // vpermq
1184       {TTI::SK_PermuteSingleSrc, MVT::v16i32, 1}, // vpermd
1185       {TTI::SK_PermuteSingleSrc, MVT::v8i32, 1},  // vpermd
1186       {TTI::SK_PermuteSingleSrc, MVT::v4i32, 1},  // vpermd
1187       {TTI::SK_PermuteSingleSrc, MVT::v16i8, 1},  // pshufb
1188 
1189       {TTI::SK_PermuteTwoSrc, MVT::v8f64, 1},  // vpermt2pd
1190       {TTI::SK_PermuteTwoSrc, MVT::v16f32, 1}, // vpermt2ps
1191       {TTI::SK_PermuteTwoSrc, MVT::v8i64, 1},  // vpermt2q
1192       {TTI::SK_PermuteTwoSrc, MVT::v16i32, 1}, // vpermt2d
1193       {TTI::SK_PermuteTwoSrc, MVT::v4f64, 1},  // vpermt2pd
1194       {TTI::SK_PermuteTwoSrc, MVT::v8f32, 1},  // vpermt2ps
1195       {TTI::SK_PermuteTwoSrc, MVT::v4i64, 1},  // vpermt2q
1196       {TTI::SK_PermuteTwoSrc, MVT::v8i32, 1},  // vpermt2d
1197       {TTI::SK_PermuteTwoSrc, MVT::v2f64, 1},  // vpermt2pd
1198       {TTI::SK_PermuteTwoSrc, MVT::v4f32, 1},  // vpermt2ps
1199       {TTI::SK_PermuteTwoSrc, MVT::v2i64, 1},  // vpermt2q
1200       {TTI::SK_PermuteTwoSrc, MVT::v4i32, 1},  // vpermt2d
1201 
1202       // FIXME: This just applies the type legalization cost rules above
1203       // assuming these completely split.
1204       {TTI::SK_PermuteSingleSrc, MVT::v32i16, 14},
1205       {TTI::SK_PermuteSingleSrc, MVT::v64i8,  14},
1206       {TTI::SK_PermuteTwoSrc,    MVT::v32i16, 42},
1207       {TTI::SK_PermuteTwoSrc,    MVT::v64i8,  42},
1208 
1209       {TTI::SK_Select, MVT::v32i16, 1}, // vpternlogq
1210       {TTI::SK_Select, MVT::v64i8,  1}, // vpternlogq
1211       {TTI::SK_Select, MVT::v8f64,  1}, // vblendmpd
1212       {TTI::SK_Select, MVT::v16f32, 1}, // vblendmps
1213       {TTI::SK_Select, MVT::v8i64,  1}, // vblendmq
1214       {TTI::SK_Select, MVT::v16i32, 1}, // vblendmd
1215   };
1216 
1217   if (ST->hasAVX512())
1218     if (const auto *Entry = CostTableLookup(AVX512ShuffleTbl, Kind, LT.second))
1219       return LT.first * Entry->Cost;
1220 
1221   static const CostTblEntry AVX2ShuffleTbl[] = {
1222       {TTI::SK_Broadcast, MVT::v4f64, 1},  // vbroadcastpd
1223       {TTI::SK_Broadcast, MVT::v8f32, 1},  // vbroadcastps
1224       {TTI::SK_Broadcast, MVT::v4i64, 1},  // vpbroadcastq
1225       {TTI::SK_Broadcast, MVT::v8i32, 1},  // vpbroadcastd
1226       {TTI::SK_Broadcast, MVT::v16i16, 1}, // vpbroadcastw
1227       {TTI::SK_Broadcast, MVT::v32i8, 1},  // vpbroadcastb
1228 
1229       {TTI::SK_Reverse, MVT::v4f64, 1},  // vpermpd
1230       {TTI::SK_Reverse, MVT::v8f32, 1},  // vpermps
1231       {TTI::SK_Reverse, MVT::v4i64, 1},  // vpermq
1232       {TTI::SK_Reverse, MVT::v8i32, 1},  // vpermd
1233       {TTI::SK_Reverse, MVT::v16i16, 2}, // vperm2i128 + pshufb
1234       {TTI::SK_Reverse, MVT::v32i8, 2},  // vperm2i128 + pshufb
1235 
1236       {TTI::SK_Select, MVT::v16i16, 1}, // vpblendvb
1237       {TTI::SK_Select, MVT::v32i8, 1},  // vpblendvb
1238 
1239       {TTI::SK_PermuteSingleSrc, MVT::v4f64, 1},  // vpermpd
1240       {TTI::SK_PermuteSingleSrc, MVT::v8f32, 1},  // vpermps
1241       {TTI::SK_PermuteSingleSrc, MVT::v4i64, 1},  // vpermq
1242       {TTI::SK_PermuteSingleSrc, MVT::v8i32, 1},  // vpermd
1243       {TTI::SK_PermuteSingleSrc, MVT::v16i16, 4}, // vperm2i128 + 2*vpshufb
1244                                                   // + vpblendvb
1245       {TTI::SK_PermuteSingleSrc, MVT::v32i8, 4},  // vperm2i128 + 2*vpshufb
1246                                                   // + vpblendvb
1247 
1248       {TTI::SK_PermuteTwoSrc, MVT::v4f64, 3},  // 2*vpermpd + vblendpd
1249       {TTI::SK_PermuteTwoSrc, MVT::v8f32, 3},  // 2*vpermps + vblendps
1250       {TTI::SK_PermuteTwoSrc, MVT::v4i64, 3},  // 2*vpermq + vpblendd
1251       {TTI::SK_PermuteTwoSrc, MVT::v8i32, 3},  // 2*vpermd + vpblendd
1252       {TTI::SK_PermuteTwoSrc, MVT::v16i16, 7}, // 2*vperm2i128 + 4*vpshufb
1253                                                // + vpblendvb
1254       {TTI::SK_PermuteTwoSrc, MVT::v32i8, 7},  // 2*vperm2i128 + 4*vpshufb
1255                                                // + vpblendvb
1256   };
1257 
1258   if (ST->hasAVX2())
1259     if (const auto *Entry = CostTableLookup(AVX2ShuffleTbl, Kind, LT.second))
1260       return LT.first * Entry->Cost;
1261 
1262   static const CostTblEntry XOPShuffleTbl[] = {
1263       {TTI::SK_PermuteSingleSrc, MVT::v4f64, 2},  // vperm2f128 + vpermil2pd
1264       {TTI::SK_PermuteSingleSrc, MVT::v8f32, 2},  // vperm2f128 + vpermil2ps
1265       {TTI::SK_PermuteSingleSrc, MVT::v4i64, 2},  // vperm2f128 + vpermil2pd
1266       {TTI::SK_PermuteSingleSrc, MVT::v8i32, 2},  // vperm2f128 + vpermil2ps
1267       {TTI::SK_PermuteSingleSrc, MVT::v16i16, 4}, // vextractf128 + 2*vpperm
1268                                                   // + vinsertf128
1269       {TTI::SK_PermuteSingleSrc, MVT::v32i8, 4},  // vextractf128 + 2*vpperm
1270                                                   // + vinsertf128
1271 
1272       {TTI::SK_PermuteTwoSrc, MVT::v16i16, 9}, // 2*vextractf128 + 6*vpperm
1273                                                // + vinsertf128
1274       {TTI::SK_PermuteTwoSrc, MVT::v8i16, 1},  // vpperm
1275       {TTI::SK_PermuteTwoSrc, MVT::v32i8, 9},  // 2*vextractf128 + 6*vpperm
1276                                                // + vinsertf128
1277       {TTI::SK_PermuteTwoSrc, MVT::v16i8, 1},  // vpperm
1278   };
1279 
1280   if (ST->hasXOP())
1281     if (const auto *Entry = CostTableLookup(XOPShuffleTbl, Kind, LT.second))
1282       return LT.first * Entry->Cost;
1283 
1284   static const CostTblEntry AVX1ShuffleTbl[] = {
1285       {TTI::SK_Broadcast, MVT::v4f64, 2},  // vperm2f128 + vpermilpd
1286       {TTI::SK_Broadcast, MVT::v8f32, 2},  // vperm2f128 + vpermilps
1287       {TTI::SK_Broadcast, MVT::v4i64, 2},  // vperm2f128 + vpermilpd
1288       {TTI::SK_Broadcast, MVT::v8i32, 2},  // vperm2f128 + vpermilps
1289       {TTI::SK_Broadcast, MVT::v16i16, 3}, // vpshuflw + vpshufd + vinsertf128
1290       {TTI::SK_Broadcast, MVT::v32i8, 2},  // vpshufb + vinsertf128
1291 
1292       {TTI::SK_Reverse, MVT::v4f64, 2},  // vperm2f128 + vpermilpd
1293       {TTI::SK_Reverse, MVT::v8f32, 2},  // vperm2f128 + vpermilps
1294       {TTI::SK_Reverse, MVT::v4i64, 2},  // vperm2f128 + vpermilpd
1295       {TTI::SK_Reverse, MVT::v8i32, 2},  // vperm2f128 + vpermilps
1296       {TTI::SK_Reverse, MVT::v16i16, 4}, // vextractf128 + 2*pshufb
1297                                          // + vinsertf128
1298       {TTI::SK_Reverse, MVT::v32i8, 4},  // vextractf128 + 2*pshufb
1299                                          // + vinsertf128
1300 
1301       {TTI::SK_Select, MVT::v4i64, 1},  // vblendpd
1302       {TTI::SK_Select, MVT::v4f64, 1},  // vblendpd
1303       {TTI::SK_Select, MVT::v8i32, 1},  // vblendps
1304       {TTI::SK_Select, MVT::v8f32, 1},  // vblendps
1305       {TTI::SK_Select, MVT::v16i16, 3}, // vpand + vpandn + vpor
1306       {TTI::SK_Select, MVT::v32i8, 3},  // vpand + vpandn + vpor
1307 
1308       {TTI::SK_PermuteSingleSrc, MVT::v4f64, 2},  // vperm2f128 + vshufpd
1309       {TTI::SK_PermuteSingleSrc, MVT::v4i64, 2},  // vperm2f128 + vshufpd
1310       {TTI::SK_PermuteSingleSrc, MVT::v8f32, 4},  // 2*vperm2f128 + 2*vshufps
1311       {TTI::SK_PermuteSingleSrc, MVT::v8i32, 4},  // 2*vperm2f128 + 2*vshufps
1312       {TTI::SK_PermuteSingleSrc, MVT::v16i16, 8}, // vextractf128 + 4*pshufb
1313                                                   // + 2*por + vinsertf128
1314       {TTI::SK_PermuteSingleSrc, MVT::v32i8, 8},  // vextractf128 + 4*pshufb
1315                                                   // + 2*por + vinsertf128
1316 
1317       {TTI::SK_PermuteTwoSrc, MVT::v4f64, 3},   // 2*vperm2f128 + vshufpd
1318       {TTI::SK_PermuteTwoSrc, MVT::v4i64, 3},   // 2*vperm2f128 + vshufpd
1319       {TTI::SK_PermuteTwoSrc, MVT::v8f32, 4},   // 2*vperm2f128 + 2*vshufps
1320       {TTI::SK_PermuteTwoSrc, MVT::v8i32, 4},   // 2*vperm2f128 + 2*vshufps
1321       {TTI::SK_PermuteTwoSrc, MVT::v16i16, 15}, // 2*vextractf128 + 8*pshufb
1322                                                 // + 4*por + vinsertf128
1323       {TTI::SK_PermuteTwoSrc, MVT::v32i8, 15},  // 2*vextractf128 + 8*pshufb
1324                                                 // + 4*por + vinsertf128
1325   };
1326 
1327   if (ST->hasAVX())
1328     if (const auto *Entry = CostTableLookup(AVX1ShuffleTbl, Kind, LT.second))
1329       return LT.first * Entry->Cost;
1330 
1331   static const CostTblEntry SSE41ShuffleTbl[] = {
1332       {TTI::SK_Select, MVT::v2i64, 1}, // pblendw
1333       {TTI::SK_Select, MVT::v2f64, 1}, // movsd
1334       {TTI::SK_Select, MVT::v4i32, 1}, // pblendw
1335       {TTI::SK_Select, MVT::v4f32, 1}, // blendps
1336       {TTI::SK_Select, MVT::v8i16, 1}, // pblendw
1337       {TTI::SK_Select, MVT::v16i8, 1}  // pblendvb
1338   };
1339 
1340   if (ST->hasSSE41())
1341     if (const auto *Entry = CostTableLookup(SSE41ShuffleTbl, Kind, LT.second))
1342       return LT.first * Entry->Cost;
1343 
1344   static const CostTblEntry SSSE3ShuffleTbl[] = {
1345       {TTI::SK_Broadcast, MVT::v8i16, 1}, // pshufb
1346       {TTI::SK_Broadcast, MVT::v16i8, 1}, // pshufb
1347 
1348       {TTI::SK_Reverse, MVT::v8i16, 1}, // pshufb
1349       {TTI::SK_Reverse, MVT::v16i8, 1}, // pshufb
1350 
1351       {TTI::SK_Select, MVT::v8i16, 3}, // 2*pshufb + por
1352       {TTI::SK_Select, MVT::v16i8, 3}, // 2*pshufb + por
1353 
1354       {TTI::SK_PermuteSingleSrc, MVT::v8i16, 1}, // pshufb
1355       {TTI::SK_PermuteSingleSrc, MVT::v16i8, 1}, // pshufb
1356 
1357       {TTI::SK_PermuteTwoSrc, MVT::v8i16, 3}, // 2*pshufb + por
1358       {TTI::SK_PermuteTwoSrc, MVT::v16i8, 3}, // 2*pshufb + por
1359   };
1360 
1361   if (ST->hasSSSE3())
1362     if (const auto *Entry = CostTableLookup(SSSE3ShuffleTbl, Kind, LT.second))
1363       return LT.first * Entry->Cost;
1364 
1365   static const CostTblEntry SSE2ShuffleTbl[] = {
1366       {TTI::SK_Broadcast, MVT::v2f64, 1}, // shufpd
1367       {TTI::SK_Broadcast, MVT::v2i64, 1}, // pshufd
1368       {TTI::SK_Broadcast, MVT::v4i32, 1}, // pshufd
1369       {TTI::SK_Broadcast, MVT::v8i16, 2}, // pshuflw + pshufd
1370       {TTI::SK_Broadcast, MVT::v16i8, 3}, // unpck + pshuflw + pshufd
1371 
1372       {TTI::SK_Reverse, MVT::v2f64, 1}, // shufpd
1373       {TTI::SK_Reverse, MVT::v2i64, 1}, // pshufd
1374       {TTI::SK_Reverse, MVT::v4i32, 1}, // pshufd
1375       {TTI::SK_Reverse, MVT::v8i16, 3}, // pshuflw + pshufhw + pshufd
1376       {TTI::SK_Reverse, MVT::v16i8, 9}, // 2*pshuflw + 2*pshufhw
1377                                         // + 2*pshufd + 2*unpck + packus
1378 
1379       {TTI::SK_Select, MVT::v2i64, 1}, // movsd
1380       {TTI::SK_Select, MVT::v2f64, 1}, // movsd
1381       {TTI::SK_Select, MVT::v4i32, 2}, // 2*shufps
1382       {TTI::SK_Select, MVT::v8i16, 3}, // pand + pandn + por
1383       {TTI::SK_Select, MVT::v16i8, 3}, // pand + pandn + por
1384 
1385       {TTI::SK_PermuteSingleSrc, MVT::v2f64, 1}, // shufpd
1386       {TTI::SK_PermuteSingleSrc, MVT::v2i64, 1}, // pshufd
1387       {TTI::SK_PermuteSingleSrc, MVT::v4i32, 1}, // pshufd
1388       {TTI::SK_PermuteSingleSrc, MVT::v8i16, 5}, // 2*pshuflw + 2*pshufhw
1389                                                   // + pshufd/unpck
1390     { TTI::SK_PermuteSingleSrc, MVT::v16i8, 10 }, // 2*pshuflw + 2*pshufhw
1391                                                   // + 2*pshufd + 2*unpck + 2*packus
1392 
1393     { TTI::SK_PermuteTwoSrc,    MVT::v2f64,  1 }, // shufpd
1394     { TTI::SK_PermuteTwoSrc,    MVT::v2i64,  1 }, // shufpd
1395     { TTI::SK_PermuteTwoSrc,    MVT::v4i32,  2 }, // 2*{unpck,movsd,pshufd}
1396     { TTI::SK_PermuteTwoSrc,    MVT::v8i16,  8 }, // blend+permute
1397     { TTI::SK_PermuteTwoSrc,    MVT::v16i8, 13 }, // blend+permute
1398   };
1399 
1400   if (ST->hasSSE2())
1401     if (const auto *Entry = CostTableLookup(SSE2ShuffleTbl, Kind, LT.second))
1402       return LT.first * Entry->Cost;
1403 
1404   static const CostTblEntry SSE1ShuffleTbl[] = {
1405     { TTI::SK_Broadcast,        MVT::v4f32, 1 }, // shufps
1406     { TTI::SK_Reverse,          MVT::v4f32, 1 }, // shufps
1407     { TTI::SK_Select,           MVT::v4f32, 2 }, // 2*shufps
1408     { TTI::SK_PermuteSingleSrc, MVT::v4f32, 1 }, // shufps
1409     { TTI::SK_PermuteTwoSrc,    MVT::v4f32, 2 }, // 2*shufps
1410   };
1411 
1412   if (ST->hasSSE1())
1413     if (const auto *Entry = CostTableLookup(SSE1ShuffleTbl, Kind, LT.second))
1414       return LT.first * Entry->Cost;
1415 
1416   return BaseT::getShuffleCost(Kind, BaseTp, Mask, Index, SubTp);
1417 }
1418 
1419 InstructionCost X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
1420                                              Type *Src,
1421                                              TTI::CastContextHint CCH,
1422                                              TTI::TargetCostKind CostKind,
1423                                              const Instruction *I) {
1424   int ISD = TLI->InstructionOpcodeToISD(Opcode);
1425   assert(ISD && "Invalid opcode");
1426 
1427   // TODO: Allow non-throughput costs that aren't binary.
1428   auto AdjustCost = [&CostKind](InstructionCost Cost) -> InstructionCost {
1429     if (CostKind != TTI::TCK_RecipThroughput)
1430       return Cost == 0 ? 0 : 1;
1431     return Cost;
1432   };
1433 
1434   // FIXME: Need a better design of the cost table to handle non-simple types of
1435   // potential massive combinations (elem_num x src_type x dst_type).
1436 
1437   static const TypeConversionCostTblEntry AVX512BWConversionTbl[] {
1438     { ISD::SIGN_EXTEND, MVT::v32i16, MVT::v32i8, 1 },
1439     { ISD::ZERO_EXTEND, MVT::v32i16, MVT::v32i8, 1 },
1440 
1441     // Mask sign extend has an instruction.
1442     { ISD::SIGN_EXTEND, MVT::v2i8,   MVT::v2i1,  1 },
1443     { ISD::SIGN_EXTEND, MVT::v2i16,  MVT::v2i1,  1 },
1444     { ISD::SIGN_EXTEND, MVT::v4i8,   MVT::v4i1,  1 },
1445     { ISD::SIGN_EXTEND, MVT::v4i16,  MVT::v4i1,  1 },
1446     { ISD::SIGN_EXTEND, MVT::v8i8,   MVT::v8i1,  1 },
1447     { ISD::SIGN_EXTEND, MVT::v8i16,  MVT::v8i1,  1 },
1448     { ISD::SIGN_EXTEND, MVT::v16i8,  MVT::v16i1, 1 },
1449     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i1, 1 },
1450     { ISD::SIGN_EXTEND, MVT::v32i8,  MVT::v32i1, 1 },
1451     { ISD::SIGN_EXTEND, MVT::v32i16, MVT::v32i1, 1 },
1452     { ISD::SIGN_EXTEND, MVT::v64i8,  MVT::v64i1, 1 },
1453 
1454     // Mask zero extend is a sext + shift.
1455     { ISD::ZERO_EXTEND, MVT::v2i8,   MVT::v2i1,  2 },
1456     { ISD::ZERO_EXTEND, MVT::v2i16,  MVT::v2i1,  2 },
1457     { ISD::ZERO_EXTEND, MVT::v4i8,   MVT::v4i1,  2 },
1458     { ISD::ZERO_EXTEND, MVT::v4i16,  MVT::v4i1,  2 },
1459     { ISD::ZERO_EXTEND, MVT::v8i8,   MVT::v8i1,  2 },
1460     { ISD::ZERO_EXTEND, MVT::v8i16,  MVT::v8i1,  2 },
1461     { ISD::ZERO_EXTEND, MVT::v16i8,  MVT::v16i1, 2 },
1462     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i1, 2 },
1463     { ISD::ZERO_EXTEND, MVT::v32i8,  MVT::v32i1, 2 },
1464     { ISD::ZERO_EXTEND, MVT::v32i16, MVT::v32i1, 2 },
1465     { ISD::ZERO_EXTEND, MVT::v64i8,  MVT::v64i1, 2 },
1466 
1467     { ISD::TRUNCATE,    MVT::v32i8,  MVT::v32i16, 2 },
1468     { ISD::TRUNCATE,    MVT::v16i8,  MVT::v16i16, 2 }, // widen to zmm
1469     { ISD::TRUNCATE,    MVT::v2i1,   MVT::v2i8,   2 }, // widen to zmm
1470     { ISD::TRUNCATE,    MVT::v2i1,   MVT::v2i16,  2 }, // widen to zmm
1471     { ISD::TRUNCATE,    MVT::v4i1,   MVT::v4i8,   2 }, // widen to zmm
1472     { ISD::TRUNCATE,    MVT::v4i1,   MVT::v4i16,  2 }, // widen to zmm
1473     { ISD::TRUNCATE,    MVT::v8i1,   MVT::v8i8,   2 }, // widen to zmm
1474     { ISD::TRUNCATE,    MVT::v8i1,   MVT::v8i16,  2 }, // widen to zmm
1475     { ISD::TRUNCATE,    MVT::v16i1,  MVT::v16i8,  2 }, // widen to zmm
1476     { ISD::TRUNCATE,    MVT::v16i1,  MVT::v16i16, 2 }, // widen to zmm
1477     { ISD::TRUNCATE,    MVT::v32i1,  MVT::v32i8,  2 }, // widen to zmm
1478     { ISD::TRUNCATE,    MVT::v32i1,  MVT::v32i16, 2 },
1479     { ISD::TRUNCATE,    MVT::v64i1,  MVT::v64i8,  2 },
1480   };
1481 
1482   static const TypeConversionCostTblEntry AVX512DQConversionTbl[] = {
1483     { ISD::SINT_TO_FP,  MVT::v8f32,  MVT::v8i64,  1 },
1484     { ISD::SINT_TO_FP,  MVT::v8f64,  MVT::v8i64,  1 },
1485 
1486     { ISD::UINT_TO_FP,  MVT::v8f32,  MVT::v8i64,  1 },
1487     { ISD::UINT_TO_FP,  MVT::v8f64,  MVT::v8i64,  1 },
1488 
1489     { ISD::FP_TO_SINT,  MVT::v8i64,  MVT::v8f32,  1 },
1490     { ISD::FP_TO_SINT,  MVT::v8i64,  MVT::v8f64,  1 },
1491 
1492     { ISD::FP_TO_UINT,  MVT::v8i64,  MVT::v8f32,  1 },
1493     { ISD::FP_TO_UINT,  MVT::v8i64,  MVT::v8f64,  1 },
1494   };
1495 
1496   // TODO: For AVX512DQ + AVX512VL, we also have cheap casts for 128-bit and
1497   // 256-bit wide vectors.
1498 
1499   static const TypeConversionCostTblEntry AVX512FConversionTbl[] = {
1500     { ISD::FP_EXTEND, MVT::v8f64,   MVT::v8f32,  1 },
1501     { ISD::FP_EXTEND, MVT::v8f64,   MVT::v16f32, 3 },
1502     { ISD::FP_ROUND,  MVT::v8f32,   MVT::v8f64,  1 },
1503 
1504     { ISD::TRUNCATE,  MVT::v2i1,    MVT::v2i8,   3 }, // sext+vpslld+vptestmd
1505     { ISD::TRUNCATE,  MVT::v4i1,    MVT::v4i8,   3 }, // sext+vpslld+vptestmd
1506     { ISD::TRUNCATE,  MVT::v8i1,    MVT::v8i8,   3 }, // sext+vpslld+vptestmd
1507     { ISD::TRUNCATE,  MVT::v16i1,   MVT::v16i8,  3 }, // sext+vpslld+vptestmd
1508     { ISD::TRUNCATE,  MVT::v2i1,    MVT::v2i16,  3 }, // sext+vpsllq+vptestmq
1509     { ISD::TRUNCATE,  MVT::v4i1,    MVT::v4i16,  3 }, // sext+vpsllq+vptestmq
1510     { ISD::TRUNCATE,  MVT::v8i1,    MVT::v8i16,  3 }, // sext+vpsllq+vptestmq
1511     { ISD::TRUNCATE,  MVT::v16i1,   MVT::v16i16, 3 }, // sext+vpslld+vptestmd
1512     { ISD::TRUNCATE,  MVT::v2i1,    MVT::v2i32,  2 }, // zmm vpslld+vptestmd
1513     { ISD::TRUNCATE,  MVT::v4i1,    MVT::v4i32,  2 }, // zmm vpslld+vptestmd
1514     { ISD::TRUNCATE,  MVT::v8i1,    MVT::v8i32,  2 }, // zmm vpslld+vptestmd
1515     { ISD::TRUNCATE,  MVT::v16i1,   MVT::v16i32, 2 }, // vpslld+vptestmd
1516     { ISD::TRUNCATE,  MVT::v2i1,    MVT::v2i64,  2 }, // zmm vpsllq+vptestmq
1517     { ISD::TRUNCATE,  MVT::v4i1,    MVT::v4i64,  2 }, // zmm vpsllq+vptestmq
1518     { ISD::TRUNCATE,  MVT::v8i1,    MVT::v8i64,  2 }, // vpsllq+vptestmq
1519     { ISD::TRUNCATE,  MVT::v16i8,   MVT::v16i32, 2 },
1520     { ISD::TRUNCATE,  MVT::v16i16,  MVT::v16i32, 2 },
1521     { ISD::TRUNCATE,  MVT::v8i8,    MVT::v8i64,  2 },
1522     { ISD::TRUNCATE,  MVT::v8i16,   MVT::v8i64,  2 },
1523     { ISD::TRUNCATE,  MVT::v8i32,   MVT::v8i64,  1 },
1524     { ISD::TRUNCATE,  MVT::v4i32,   MVT::v4i64,  1 }, // zmm vpmovqd
1525     { ISD::TRUNCATE,  MVT::v16i8,   MVT::v16i64, 5 },// 2*vpmovqd+concat+vpmovdb
1526 
1527     { ISD::TRUNCATE,  MVT::v16i8,  MVT::v16i16,  3 }, // extend to v16i32
1528     { ISD::TRUNCATE,  MVT::v32i8,  MVT::v32i16,  8 },
1529 
1530     // Sign extend is zmm vpternlogd+vptruncdb.
1531     // Zero extend is zmm broadcast load+vptruncdw.
1532     { ISD::SIGN_EXTEND, MVT::v2i8,   MVT::v2i1,   3 },
1533     { ISD::ZERO_EXTEND, MVT::v2i8,   MVT::v2i1,   4 },
1534     { ISD::SIGN_EXTEND, MVT::v4i8,   MVT::v4i1,   3 },
1535     { ISD::ZERO_EXTEND, MVT::v4i8,   MVT::v4i1,   4 },
1536     { ISD::SIGN_EXTEND, MVT::v8i8,   MVT::v8i1,   3 },
1537     { ISD::ZERO_EXTEND, MVT::v8i8,   MVT::v8i1,   4 },
1538     { ISD::SIGN_EXTEND, MVT::v16i8,  MVT::v16i1,  3 },
1539     { ISD::ZERO_EXTEND, MVT::v16i8,  MVT::v16i1,  4 },
1540 
1541     // Sign extend is zmm vpternlogd+vptruncdw.
1542     // Zero extend is zmm vpternlogd+vptruncdw+vpsrlw.
1543     { ISD::SIGN_EXTEND, MVT::v2i16,  MVT::v2i1,   3 },
1544     { ISD::ZERO_EXTEND, MVT::v2i16,  MVT::v2i1,   4 },
1545     { ISD::SIGN_EXTEND, MVT::v4i16,  MVT::v4i1,   3 },
1546     { ISD::ZERO_EXTEND, MVT::v4i16,  MVT::v4i1,   4 },
1547     { ISD::SIGN_EXTEND, MVT::v8i16,  MVT::v8i1,   3 },
1548     { ISD::ZERO_EXTEND, MVT::v8i16,  MVT::v8i1,   4 },
1549     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i1,  3 },
1550     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i1,  4 },
1551 
1552     { ISD::SIGN_EXTEND, MVT::v2i32,  MVT::v2i1,   1 }, // zmm vpternlogd
1553     { ISD::ZERO_EXTEND, MVT::v2i32,  MVT::v2i1,   2 }, // zmm vpternlogd+psrld
1554     { ISD::SIGN_EXTEND, MVT::v4i32,  MVT::v4i1,   1 }, // zmm vpternlogd
1555     { ISD::ZERO_EXTEND, MVT::v4i32,  MVT::v4i1,   2 }, // zmm vpternlogd+psrld
1556     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i1,   1 }, // zmm vpternlogd
1557     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i1,   2 }, // zmm vpternlogd+psrld
1558     { ISD::SIGN_EXTEND, MVT::v2i64,  MVT::v2i1,   1 }, // zmm vpternlogq
1559     { ISD::ZERO_EXTEND, MVT::v2i64,  MVT::v2i1,   2 }, // zmm vpternlogq+psrlq
1560     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i1,   1 }, // zmm vpternlogq
1561     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i1,   2 }, // zmm vpternlogq+psrlq
1562 
1563     { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1,  1 }, // vpternlogd
1564     { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1,  2 }, // vpternlogd+psrld
1565     { ISD::SIGN_EXTEND, MVT::v8i64,  MVT::v8i1,   1 }, // vpternlogq
1566     { ISD::ZERO_EXTEND, MVT::v8i64,  MVT::v8i1,   2 }, // vpternlogq+psrlq
1567 
1568     { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8,  1 },
1569     { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8,  1 },
1570     { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
1571     { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
1572     { ISD::SIGN_EXTEND, MVT::v8i64,  MVT::v8i8,   1 },
1573     { ISD::ZERO_EXTEND, MVT::v8i64,  MVT::v8i8,   1 },
1574     { ISD::SIGN_EXTEND, MVT::v8i64,  MVT::v8i16,  1 },
1575     { ISD::ZERO_EXTEND, MVT::v8i64,  MVT::v8i16,  1 },
1576     { ISD::SIGN_EXTEND, MVT::v8i64,  MVT::v8i32,  1 },
1577     { ISD::ZERO_EXTEND, MVT::v8i64,  MVT::v8i32,  1 },
1578 
1579     { ISD::SIGN_EXTEND, MVT::v32i16, MVT::v32i8, 3 }, // FIXME: May not be right
1580     { ISD::ZERO_EXTEND, MVT::v32i16, MVT::v32i8, 3 }, // FIXME: May not be right
1581 
1582     { ISD::SINT_TO_FP,  MVT::v8f64,  MVT::v8i1,   4 },
1583     { ISD::SINT_TO_FP,  MVT::v16f32, MVT::v16i1,  3 },
1584     { ISD::SINT_TO_FP,  MVT::v8f64,  MVT::v8i8,   2 },
1585     { ISD::SINT_TO_FP,  MVT::v16f32, MVT::v16i8,  2 },
1586     { ISD::SINT_TO_FP,  MVT::v8f64,  MVT::v8i16,  2 },
1587     { ISD::SINT_TO_FP,  MVT::v16f32, MVT::v16i16, 2 },
1588     { ISD::SINT_TO_FP,  MVT::v16f32, MVT::v16i32, 1 },
1589     { ISD::SINT_TO_FP,  MVT::v8f64,  MVT::v8i32,  1 },
1590 
1591     { ISD::UINT_TO_FP,  MVT::v8f64,  MVT::v8i1,   4 },
1592     { ISD::UINT_TO_FP,  MVT::v16f32, MVT::v16i1,  3 },
1593     { ISD::UINT_TO_FP,  MVT::v8f64,  MVT::v8i8,   2 },
1594     { ISD::UINT_TO_FP,  MVT::v16f32, MVT::v16i8,  2 },
1595     { ISD::UINT_TO_FP,  MVT::v8f64,  MVT::v8i16,  2 },
1596     { ISD::UINT_TO_FP,  MVT::v16f32, MVT::v16i16, 2 },
1597     { ISD::UINT_TO_FP,  MVT::v8f64,  MVT::v8i32,  1 },
1598     { ISD::UINT_TO_FP,  MVT::v16f32, MVT::v16i32, 1 },
1599     { ISD::UINT_TO_FP,  MVT::v8f32,  MVT::v8i64, 26 },
1600     { ISD::UINT_TO_FP,  MVT::v8f64,  MVT::v8i64,  5 },
1601 
1602     { ISD::FP_TO_SINT,  MVT::v8i8,   MVT::v8f64,  3 },
1603     { ISD::FP_TO_SINT,  MVT::v8i16,  MVT::v8f64,  3 },
1604     { ISD::FP_TO_SINT,  MVT::v16i8,  MVT::v16f32, 3 },
1605     { ISD::FP_TO_SINT,  MVT::v16i16, MVT::v16f32, 3 },
1606 
1607     { ISD::FP_TO_UINT,  MVT::v8i32,  MVT::v8f64,  1 },
1608     { ISD::FP_TO_UINT,  MVT::v8i16,  MVT::v8f64,  3 },
1609     { ISD::FP_TO_UINT,  MVT::v8i8,   MVT::v8f64,  3 },
1610     { ISD::FP_TO_UINT,  MVT::v16i32, MVT::v16f32, 1 },
1611     { ISD::FP_TO_UINT,  MVT::v16i16, MVT::v16f32, 3 },
1612     { ISD::FP_TO_UINT,  MVT::v16i8,  MVT::v16f32, 3 },
1613   };
1614 
1615   static const TypeConversionCostTblEntry AVX512BWVLConversionTbl[] {
1616     // Mask sign extend has an instruction.
1617     { ISD::SIGN_EXTEND, MVT::v2i8,   MVT::v2i1,  1 },
1618     { ISD::SIGN_EXTEND, MVT::v2i16,  MVT::v2i1,  1 },
1619     { ISD::SIGN_EXTEND, MVT::v4i8,   MVT::v4i1,  1 },
1620     { ISD::SIGN_EXTEND, MVT::v4i16,  MVT::v4i1,  1 },
1621     { ISD::SIGN_EXTEND, MVT::v8i8,   MVT::v8i1,  1 },
1622     { ISD::SIGN_EXTEND, MVT::v8i16,  MVT::v8i1,  1 },
1623     { ISD::SIGN_EXTEND, MVT::v16i8,  MVT::v16i1, 1 },
1624     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i1, 1 },
1625     { ISD::SIGN_EXTEND, MVT::v32i8,  MVT::v32i1, 1 },
1626 
1627     // Mask zero extend is a sext + shift.
1628     { ISD::ZERO_EXTEND, MVT::v2i8,   MVT::v2i1,  2 },
1629     { ISD::ZERO_EXTEND, MVT::v2i16,  MVT::v2i1,  2 },
1630     { ISD::ZERO_EXTEND, MVT::v4i8,   MVT::v4i1,  2 },
1631     { ISD::ZERO_EXTEND, MVT::v4i16,  MVT::v4i1,  2 },
1632     { ISD::ZERO_EXTEND, MVT::v8i8,   MVT::v8i1,  2 },
1633     { ISD::ZERO_EXTEND, MVT::v8i16,  MVT::v8i1,  2 },
1634     { ISD::ZERO_EXTEND, MVT::v16i8,  MVT::v16i1, 2 },
1635     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i1, 2 },
1636     { ISD::ZERO_EXTEND, MVT::v32i8,  MVT::v32i1, 2 },
1637 
1638     { ISD::TRUNCATE,    MVT::v16i8,  MVT::v16i16, 2 },
1639     { ISD::TRUNCATE,    MVT::v2i1,   MVT::v2i8,   2 }, // vpsllw+vptestmb
1640     { ISD::TRUNCATE,    MVT::v2i1,   MVT::v2i16,  2 }, // vpsllw+vptestmw
1641     { ISD::TRUNCATE,    MVT::v4i1,   MVT::v4i8,   2 }, // vpsllw+vptestmb
1642     { ISD::TRUNCATE,    MVT::v4i1,   MVT::v4i16,  2 }, // vpsllw+vptestmw
1643     { ISD::TRUNCATE,    MVT::v8i1,   MVT::v8i8,   2 }, // vpsllw+vptestmb
1644     { ISD::TRUNCATE,    MVT::v8i1,   MVT::v8i16,  2 }, // vpsllw+vptestmw
1645     { ISD::TRUNCATE,    MVT::v16i1,  MVT::v16i8,  2 }, // vpsllw+vptestmb
1646     { ISD::TRUNCATE,    MVT::v16i1,  MVT::v16i16, 2 }, // vpsllw+vptestmw
1647     { ISD::TRUNCATE,    MVT::v32i1,  MVT::v32i8,  2 }, // vpsllw+vptestmb
1648   };
1649 
1650   static const TypeConversionCostTblEntry AVX512DQVLConversionTbl[] = {
1651     { ISD::SINT_TO_FP,  MVT::v2f32,  MVT::v2i64,  1 },
1652     { ISD::SINT_TO_FP,  MVT::v2f64,  MVT::v2i64,  1 },
1653     { ISD::SINT_TO_FP,  MVT::v4f32,  MVT::v4i64,  1 },
1654     { ISD::SINT_TO_FP,  MVT::v4f64,  MVT::v4i64,  1 },
1655 
1656     { ISD::UINT_TO_FP,  MVT::v2f32,  MVT::v2i64,  1 },
1657     { ISD::UINT_TO_FP,  MVT::v2f64,  MVT::v2i64,  1 },
1658     { ISD::UINT_TO_FP,  MVT::v4f32,  MVT::v4i64,  1 },
1659     { ISD::UINT_TO_FP,  MVT::v4f64,  MVT::v4i64,  1 },
1660 
1661     { ISD::FP_TO_SINT,  MVT::v2i64,  MVT::v2f32,  1 },
1662     { ISD::FP_TO_SINT,  MVT::v4i64,  MVT::v4f32,  1 },
1663     { ISD::FP_TO_SINT,  MVT::v2i64,  MVT::v2f64,  1 },
1664     { ISD::FP_TO_SINT,  MVT::v4i64,  MVT::v4f64,  1 },
1665 
1666     { ISD::FP_TO_UINT,  MVT::v2i64,  MVT::v2f32,  1 },
1667     { ISD::FP_TO_UINT,  MVT::v4i64,  MVT::v4f32,  1 },
1668     { ISD::FP_TO_UINT,  MVT::v2i64,  MVT::v2f64,  1 },
1669     { ISD::FP_TO_UINT,  MVT::v4i64,  MVT::v4f64,  1 },
1670   };
1671 
1672   static const TypeConversionCostTblEntry AVX512VLConversionTbl[] = {
1673     { ISD::TRUNCATE,  MVT::v2i1,    MVT::v2i8,   3 }, // sext+vpslld+vptestmd
1674     { ISD::TRUNCATE,  MVT::v4i1,    MVT::v4i8,   3 }, // sext+vpslld+vptestmd
1675     { ISD::TRUNCATE,  MVT::v8i1,    MVT::v8i8,   3 }, // sext+vpslld+vptestmd
1676     { ISD::TRUNCATE,  MVT::v16i1,   MVT::v16i8,  8 }, // split+2*v8i8
1677     { ISD::TRUNCATE,  MVT::v2i1,    MVT::v2i16,  3 }, // sext+vpsllq+vptestmq
1678     { ISD::TRUNCATE,  MVT::v4i1,    MVT::v4i16,  3 }, // sext+vpsllq+vptestmq
1679     { ISD::TRUNCATE,  MVT::v8i1,    MVT::v8i16,  3 }, // sext+vpsllq+vptestmq
1680     { ISD::TRUNCATE,  MVT::v16i1,   MVT::v16i16, 8 }, // split+2*v8i16
1681     { ISD::TRUNCATE,  MVT::v2i1,    MVT::v2i32,  2 }, // vpslld+vptestmd
1682     { ISD::TRUNCATE,  MVT::v4i1,    MVT::v4i32,  2 }, // vpslld+vptestmd
1683     { ISD::TRUNCATE,  MVT::v8i1,    MVT::v8i32,  2 }, // vpslld+vptestmd
1684     { ISD::TRUNCATE,  MVT::v2i1,    MVT::v2i64,  2 }, // vpsllq+vptestmq
1685     { ISD::TRUNCATE,  MVT::v4i1,    MVT::v4i64,  2 }, // vpsllq+vptestmq
1686     { ISD::TRUNCATE,  MVT::v4i32,   MVT::v4i64,  1 }, // vpmovqd
1687 
1688     // sign extend is vpcmpeq+maskedmove+vpmovdw+vpacksswb
1689     // zero extend is vpcmpeq+maskedmove+vpmovdw+vpsrlw+vpackuswb
1690     { ISD::SIGN_EXTEND, MVT::v2i8,   MVT::v2i1,   5 },
1691     { ISD::ZERO_EXTEND, MVT::v2i8,   MVT::v2i1,   6 },
1692     { ISD::SIGN_EXTEND, MVT::v4i8,   MVT::v4i1,   5 },
1693     { ISD::ZERO_EXTEND, MVT::v4i8,   MVT::v4i1,   6 },
1694     { ISD::SIGN_EXTEND, MVT::v8i8,   MVT::v8i1,   5 },
1695     { ISD::ZERO_EXTEND, MVT::v8i8,   MVT::v8i1,   6 },
1696     { ISD::SIGN_EXTEND, MVT::v16i8,  MVT::v16i1, 10 },
1697     { ISD::ZERO_EXTEND, MVT::v16i8,  MVT::v16i1, 12 },
1698 
1699     // sign extend is vpcmpeq+maskedmove+vpmovdw
1700     // zero extend is vpcmpeq+maskedmove+vpmovdw+vpsrlw
1701     { ISD::SIGN_EXTEND, MVT::v2i16,  MVT::v2i1,   4 },
1702     { ISD::ZERO_EXTEND, MVT::v2i16,  MVT::v2i1,   5 },
1703     { ISD::SIGN_EXTEND, MVT::v4i16,  MVT::v4i1,   4 },
1704     { ISD::ZERO_EXTEND, MVT::v4i16,  MVT::v4i1,   5 },
1705     { ISD::SIGN_EXTEND, MVT::v8i16,  MVT::v8i1,   4 },
1706     { ISD::ZERO_EXTEND, MVT::v8i16,  MVT::v8i1,   5 },
1707     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i1, 10 },
1708     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i1, 12 },
1709 
1710     { ISD::SIGN_EXTEND, MVT::v2i32,  MVT::v2i1,   1 }, // vpternlogd
1711     { ISD::ZERO_EXTEND, MVT::v2i32,  MVT::v2i1,   2 }, // vpternlogd+psrld
1712     { ISD::SIGN_EXTEND, MVT::v4i32,  MVT::v4i1,   1 }, // vpternlogd
1713     { ISD::ZERO_EXTEND, MVT::v4i32,  MVT::v4i1,   2 }, // vpternlogd+psrld
1714     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i1,   1 }, // vpternlogd
1715     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i1,   2 }, // vpternlogd+psrld
1716     { ISD::SIGN_EXTEND, MVT::v2i64,  MVT::v2i1,   1 }, // vpternlogq
1717     { ISD::ZERO_EXTEND, MVT::v2i64,  MVT::v2i1,   2 }, // vpternlogq+psrlq
1718     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i1,   1 }, // vpternlogq
1719     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i1,   2 }, // vpternlogq+psrlq
1720 
1721     { ISD::UINT_TO_FP,  MVT::v2f64,  MVT::v2i8,   2 },
1722     { ISD::UINT_TO_FP,  MVT::v4f64,  MVT::v4i8,   2 },
1723     { ISD::UINT_TO_FP,  MVT::v8f32,  MVT::v8i8,   2 },
1724     { ISD::UINT_TO_FP,  MVT::v2f64,  MVT::v2i16,  5 },
1725     { ISD::UINT_TO_FP,  MVT::v4f64,  MVT::v4i16,  2 },
1726     { ISD::UINT_TO_FP,  MVT::v8f32,  MVT::v8i16,  2 },
1727     { ISD::UINT_TO_FP,  MVT::v2f32,  MVT::v2i32,  2 },
1728     { ISD::UINT_TO_FP,  MVT::v2f64,  MVT::v2i32,  1 },
1729     { ISD::UINT_TO_FP,  MVT::v4f32,  MVT::v4i32,  1 },
1730     { ISD::UINT_TO_FP,  MVT::v4f64,  MVT::v4i32,  1 },
1731     { ISD::UINT_TO_FP,  MVT::v8f32,  MVT::v8i32,  1 },
1732     { ISD::UINT_TO_FP,  MVT::v2f32,  MVT::v2i64,  5 },
1733     { ISD::UINT_TO_FP,  MVT::v2f64,  MVT::v2i64,  5 },
1734     { ISD::UINT_TO_FP,  MVT::v4f64,  MVT::v4i64,  5 },
1735 
1736     { ISD::UINT_TO_FP,  MVT::f32,    MVT::i64,    1 },
1737     { ISD::UINT_TO_FP,  MVT::f64,    MVT::i64,    1 },
1738 
1739     { ISD::FP_TO_SINT,  MVT::v8i8,   MVT::v8f32,  3 },
1740     { ISD::FP_TO_UINT,  MVT::v8i8,   MVT::v8f32,  3 },
1741 
1742     { ISD::FP_TO_UINT,  MVT::i64,    MVT::f32,    1 },
1743     { ISD::FP_TO_UINT,  MVT::i64,    MVT::f64,    1 },
1744 
1745     { ISD::FP_TO_UINT,  MVT::v2i32,  MVT::v2f32,  1 },
1746     { ISD::FP_TO_UINT,  MVT::v4i32,  MVT::v4f32,  1 },
1747     { ISD::FP_TO_UINT,  MVT::v2i32,  MVT::v2f64,  1 },
1748     { ISD::FP_TO_UINT,  MVT::v4i32,  MVT::v4f64,  1 },
1749     { ISD::FP_TO_UINT,  MVT::v8i32,  MVT::v8f32,  1 },
1750   };
1751 
1752   static const TypeConversionCostTblEntry AVX2ConversionTbl[] = {
1753     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i1,   3 },
1754     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i1,   3 },
1755     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i1,   3 },
1756     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i1,   3 },
1757     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i8,   1 },
1758     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i8,   1 },
1759     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i8,   1 },
1760     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i8,   1 },
1761     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i1,  1 },
1762     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i1,  1 },
1763     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8,  1 },
1764     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8,  1 },
1765     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i16,  1 },
1766     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i16,  1 },
1767     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i16,  1 },
1768     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i16,  1 },
1769     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i32,  1 },
1770     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i32,  1 },
1771     { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 3 },
1772     { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 3 },
1773 
1774     { ISD::TRUNCATE,    MVT::v4i32,  MVT::v4i64,  2 },
1775     { ISD::TRUNCATE,    MVT::v8i1,   MVT::v8i32,  2 },
1776 
1777     { ISD::TRUNCATE,    MVT::v4i8,   MVT::v4i64,  2 },
1778     { ISD::TRUNCATE,    MVT::v4i16,  MVT::v4i64,  2 },
1779     { ISD::TRUNCATE,    MVT::v8i8,   MVT::v8i32,  2 },
1780     { ISD::TRUNCATE,    MVT::v8i16,  MVT::v8i32,  2 },
1781 
1782     { ISD::FP_EXTEND,   MVT::v8f64,  MVT::v8f32,  3 },
1783     { ISD::FP_ROUND,    MVT::v8f32,  MVT::v8f64,  3 },
1784 
1785     { ISD::UINT_TO_FP,  MVT::v8f32,  MVT::v8i32,  8 },
1786   };
1787 
1788   static const TypeConversionCostTblEntry AVXConversionTbl[] = {
1789     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i1,  6 },
1790     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i1,  4 },
1791     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i1,  7 },
1792     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i1,  4 },
1793     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i8,  4 },
1794     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i8,  4 },
1795     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i8,  4 },
1796     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i8,  4 },
1797     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i1, 4 },
1798     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i1, 4 },
1799     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
1800     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
1801     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i16, 4 },
1802     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i16, 3 },
1803     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i16, 4 },
1804     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i16, 4 },
1805     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i32, 4 },
1806     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i32, 4 },
1807 
1808     { ISD::TRUNCATE,    MVT::v4i1,  MVT::v4i64,  4 },
1809     { ISD::TRUNCATE,    MVT::v8i1,  MVT::v8i32,  5 },
1810     { ISD::TRUNCATE,    MVT::v16i1, MVT::v16i16, 4 },
1811     { ISD::TRUNCATE,    MVT::v8i1,  MVT::v8i64,  9 },
1812     { ISD::TRUNCATE,    MVT::v16i1, MVT::v16i64, 11 },
1813 
1814     { ISD::TRUNCATE,    MVT::v16i8, MVT::v16i16, 4 },
1815     { ISD::TRUNCATE,    MVT::v8i8,  MVT::v8i32,  4 },
1816     { ISD::TRUNCATE,    MVT::v8i16, MVT::v8i32,  5 },
1817     { ISD::TRUNCATE,    MVT::v4i8,  MVT::v4i64,  4 },
1818     { ISD::TRUNCATE,    MVT::v4i16, MVT::v4i64,  4 },
1819     { ISD::TRUNCATE,    MVT::v4i32, MVT::v4i64,  2 },
1820     { ISD::TRUNCATE,    MVT::v8i8,  MVT::v8i64, 11 },
1821     { ISD::TRUNCATE,    MVT::v8i16, MVT::v8i64,  9 },
1822     { ISD::TRUNCATE,    MVT::v8i32, MVT::v8i64,  3 },
1823     { ISD::TRUNCATE,    MVT::v16i8, MVT::v16i64, 11 },
1824 
1825     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i1,  3 },
1826     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i1,  3 },
1827     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i1,  8 },
1828     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i8,  3 },
1829     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i8,  3 },
1830     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i8,  8 },
1831     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i16, 3 },
1832     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i16, 3 },
1833     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i16, 5 },
1834     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i32, 1 },
1835     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i32, 1 },
1836     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i32, 1 },
1837 
1838     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i1,  7 },
1839     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i1,  7 },
1840     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i1,  6 },
1841     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i8,  2 },
1842     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i8,  2 },
1843     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i8,  5 },
1844     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i16, 2 },
1845     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i16, 2 },
1846     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i16, 5 },
1847     { ISD::UINT_TO_FP,  MVT::v2f64, MVT::v2i32, 6 },
1848     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i32, 6 },
1849     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i32, 6 },
1850     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i32, 9 },
1851     { ISD::UINT_TO_FP,  MVT::v2f64, MVT::v2i64, 5 },
1852     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i64, 6 },
1853     // The generic code to compute the scalar overhead is currently broken.
1854     // Workaround this limitation by estimating the scalarization overhead
1855     // here. We have roughly 10 instructions per scalar element.
1856     // Multiply that by the vector width.
1857     // FIXME: remove that when PR19268 is fixed.
1858     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i64, 13 },
1859     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i64, 13 },
1860 
1861     { ISD::FP_TO_SINT,  MVT::v8i8,  MVT::v8f32, 4 },
1862     { ISD::FP_TO_SINT,  MVT::v4i8,  MVT::v4f64, 3 },
1863     { ISD::FP_TO_SINT,  MVT::v4i16, MVT::v4f64, 2 },
1864     { ISD::FP_TO_SINT,  MVT::v8i16, MVT::v8f32, 3 },
1865 
1866     { ISD::FP_TO_UINT,  MVT::v4i8,  MVT::v4f64, 3 },
1867     { ISD::FP_TO_UINT,  MVT::v4i16, MVT::v4f64, 2 },
1868     { ISD::FP_TO_UINT,  MVT::v8i8,  MVT::v8f32, 4 },
1869     { ISD::FP_TO_UINT,  MVT::v8i16, MVT::v8f32, 3 },
1870     // This node is expanded into scalarized operations but BasicTTI is overly
1871     // optimistic estimating its cost.  It computes 3 per element (one
1872     // vector-extract, one scalar conversion and one vector-insert).  The
1873     // problem is that the inserts form a read-modify-write chain so latency
1874     // should be factored in too.  Inflating the cost per element by 1.
1875     { ISD::FP_TO_UINT,  MVT::v8i32, MVT::v8f32, 8*4 },
1876     { ISD::FP_TO_UINT,  MVT::v4i32, MVT::v4f64, 4*4 },
1877 
1878     { ISD::FP_EXTEND,   MVT::v4f64,  MVT::v4f32,  1 },
1879     { ISD::FP_ROUND,    MVT::v4f32,  MVT::v4f64,  1 },
1880   };
1881 
1882   static const TypeConversionCostTblEntry SSE41ConversionTbl[] = {
1883     { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8,    2 },
1884     { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8,    2 },
1885     { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16,   2 },
1886     { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16,   2 },
1887     { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32,   2 },
1888     { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32,   2 },
1889 
1890     { ISD::ZERO_EXTEND, MVT::v4i16,  MVT::v4i8,   1 },
1891     { ISD::SIGN_EXTEND, MVT::v4i16,  MVT::v4i8,   2 },
1892     { ISD::ZERO_EXTEND, MVT::v4i32,  MVT::v4i8,   1 },
1893     { ISD::SIGN_EXTEND, MVT::v4i32,  MVT::v4i8,   1 },
1894     { ISD::ZERO_EXTEND, MVT::v8i16,  MVT::v8i8,   1 },
1895     { ISD::SIGN_EXTEND, MVT::v8i16,  MVT::v8i8,   1 },
1896     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i8,   2 },
1897     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i8,   2 },
1898     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8,  2 },
1899     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8,  2 },
1900     { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8,  4 },
1901     { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8,  4 },
1902     { ISD::ZERO_EXTEND, MVT::v4i32,  MVT::v4i16,  1 },
1903     { ISD::SIGN_EXTEND, MVT::v4i32,  MVT::v4i16,  1 },
1904     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i16,  2 },
1905     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i16,  2 },
1906     { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 4 },
1907     { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 4 },
1908 
1909     // These truncates end up widening elements.
1910     { ISD::TRUNCATE,    MVT::v2i1,   MVT::v2i8,   1 }, // PMOVXZBQ
1911     { ISD::TRUNCATE,    MVT::v2i1,   MVT::v2i16,  1 }, // PMOVXZWQ
1912     { ISD::TRUNCATE,    MVT::v4i1,   MVT::v4i8,   1 }, // PMOVXZBD
1913 
1914     { ISD::TRUNCATE,    MVT::v2i8,   MVT::v2i16,  1 },
1915     { ISD::TRUNCATE,    MVT::v4i8,   MVT::v4i16,  1 },
1916     { ISD::TRUNCATE,    MVT::v8i8,   MVT::v8i16,  1 },
1917     { ISD::TRUNCATE,    MVT::v4i8,   MVT::v4i32,  1 },
1918     { ISD::TRUNCATE,    MVT::v4i16,  MVT::v4i32,  1 },
1919     { ISD::TRUNCATE,    MVT::v8i8,   MVT::v8i32,  3 },
1920     { ISD::TRUNCATE,    MVT::v8i16,  MVT::v8i32,  3 },
1921     { ISD::TRUNCATE,    MVT::v16i16, MVT::v16i32, 6 },
1922     { ISD::TRUNCATE,    MVT::v2i8,   MVT::v2i64,  1 }, // PSHUFB
1923 
1924     { ISD::UINT_TO_FP,  MVT::f32,    MVT::i64,    4 },
1925     { ISD::UINT_TO_FP,  MVT::f64,    MVT::i64,    4 },
1926 
1927     { ISD::FP_TO_SINT,  MVT::v2i8,   MVT::v2f32,  3 },
1928     { ISD::FP_TO_SINT,  MVT::v2i8,   MVT::v2f64,  3 },
1929 
1930     { ISD::FP_TO_UINT,  MVT::v2i8,   MVT::v2f32,  3 },
1931     { ISD::FP_TO_UINT,  MVT::v2i8,   MVT::v2f64,  3 },
1932     { ISD::FP_TO_UINT,  MVT::v4i16,  MVT::v4f32,  2 },
1933   };
1934 
1935   static const TypeConversionCostTblEntry SSE2ConversionTbl[] = {
1936     // These are somewhat magic numbers justified by looking at the output of
1937     // Intel's IACA, running some kernels and making sure when we take
1938     // legalization into account the throughput will be overestimated.
1939     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
1940     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
1941     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
1942     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
1943     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 5 },
1944     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 2*10 },
1945     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2*10 },
1946     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
1947     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
1948 
1949     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
1950     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
1951     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
1952     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
1953     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
1954     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 },
1955     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 6 },
1956     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
1957 
1958     { ISD::FP_TO_SINT,  MVT::v2i8,   MVT::v2f32,  4 },
1959     { ISD::FP_TO_SINT,  MVT::v2i16,  MVT::v2f32,  2 },
1960     { ISD::FP_TO_SINT,  MVT::v4i8,   MVT::v4f32,  3 },
1961     { ISD::FP_TO_SINT,  MVT::v4i16,  MVT::v4f32,  2 },
1962     { ISD::FP_TO_SINT,  MVT::v2i16,  MVT::v2f64,  2 },
1963     { ISD::FP_TO_SINT,  MVT::v2i8,   MVT::v2f64,  4 },
1964 
1965     { ISD::FP_TO_SINT,  MVT::v2i32,  MVT::v2f64,  1 },
1966 
1967     { ISD::UINT_TO_FP,  MVT::f32,    MVT::i64,    6 },
1968     { ISD::UINT_TO_FP,  MVT::f64,    MVT::i64,    6 },
1969 
1970     { ISD::FP_TO_UINT,  MVT::i64,    MVT::f32,    4 },
1971     { ISD::FP_TO_UINT,  MVT::i64,    MVT::f64,    4 },
1972     { ISD::FP_TO_UINT,  MVT::v2i8,   MVT::v2f32,  4 },
1973     { ISD::FP_TO_UINT,  MVT::v2i8,   MVT::v2f64,  4 },
1974     { ISD::FP_TO_UINT,  MVT::v4i8,   MVT::v4f32,  3 },
1975     { ISD::FP_TO_UINT,  MVT::v2i16,  MVT::v2f32,  2 },
1976     { ISD::FP_TO_UINT,  MVT::v2i16,  MVT::v2f64,  2 },
1977     { ISD::FP_TO_UINT,  MVT::v4i16,  MVT::v4f32,  4 },
1978 
1979     { ISD::ZERO_EXTEND, MVT::v4i16,  MVT::v4i8,   1 },
1980     { ISD::SIGN_EXTEND, MVT::v4i16,  MVT::v4i8,   6 },
1981     { ISD::ZERO_EXTEND, MVT::v4i32,  MVT::v4i8,   2 },
1982     { ISD::SIGN_EXTEND, MVT::v4i32,  MVT::v4i8,   3 },
1983     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i8,   4 },
1984     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i8,   8 },
1985     { ISD::ZERO_EXTEND, MVT::v8i16,  MVT::v8i8,   1 },
1986     { ISD::SIGN_EXTEND, MVT::v8i16,  MVT::v8i8,   2 },
1987     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i8,   6 },
1988     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i8,   6 },
1989     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8,  3 },
1990     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8,  4 },
1991     { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8,  9 },
1992     { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8,  12 },
1993     { ISD::ZERO_EXTEND, MVT::v4i32,  MVT::v4i16,  1 },
1994     { ISD::SIGN_EXTEND, MVT::v4i32,  MVT::v4i16,  2 },
1995     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i16,  3 },
1996     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i16,  10 },
1997     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i16,  3 },
1998     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i16,  4 },
1999     { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 6 },
2000     { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 8 },
2001     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i32,  3 },
2002     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i32,  5 },
2003 
2004     // These truncates are really widening elements.
2005     { ISD::TRUNCATE,    MVT::v2i1,   MVT::v2i32,  1 }, // PSHUFD
2006     { ISD::TRUNCATE,    MVT::v2i1,   MVT::v2i16,  2 }, // PUNPCKLWD+DQ
2007     { ISD::TRUNCATE,    MVT::v2i1,   MVT::v2i8,   3 }, // PUNPCKLBW+WD+PSHUFD
2008     { ISD::TRUNCATE,    MVT::v4i1,   MVT::v4i16,  1 }, // PUNPCKLWD
2009     { ISD::TRUNCATE,    MVT::v4i1,   MVT::v4i8,   2 }, // PUNPCKLBW+WD
2010     { ISD::TRUNCATE,    MVT::v8i1,   MVT::v8i8,   1 }, // PUNPCKLBW
2011 
2012     { ISD::TRUNCATE,    MVT::v2i8,   MVT::v2i16,  2 }, // PAND+PACKUSWB
2013     { ISD::TRUNCATE,    MVT::v4i8,   MVT::v4i16,  2 }, // PAND+PACKUSWB
2014     { ISD::TRUNCATE,    MVT::v8i8,   MVT::v8i16,  2 }, // PAND+PACKUSWB
2015     { ISD::TRUNCATE,    MVT::v16i8,  MVT::v16i16, 3 },
2016     { ISD::TRUNCATE,    MVT::v2i8,   MVT::v2i32,  3 }, // PAND+2*PACKUSWB
2017     { ISD::TRUNCATE,    MVT::v2i16,  MVT::v2i32,  1 },
2018     { ISD::TRUNCATE,    MVT::v4i8,   MVT::v4i32,  3 },
2019     { ISD::TRUNCATE,    MVT::v4i16,  MVT::v4i32,  3 },
2020     { ISD::TRUNCATE,    MVT::v8i8,   MVT::v8i32,  4 },
2021     { ISD::TRUNCATE,    MVT::v16i8,  MVT::v16i32, 7 },
2022     { ISD::TRUNCATE,    MVT::v8i16,  MVT::v8i32,  5 },
2023     { ISD::TRUNCATE,    MVT::v16i16, MVT::v16i32, 10 },
2024     { ISD::TRUNCATE,    MVT::v2i8,   MVT::v2i64,  4 }, // PAND+3*PACKUSWB
2025     { ISD::TRUNCATE,    MVT::v2i16,  MVT::v2i64,  2 }, // PSHUFD+PSHUFLW
2026     { ISD::TRUNCATE,    MVT::v2i32,  MVT::v2i64,  1 }, // PSHUFD
2027   };
2028 
2029   std::pair<InstructionCost, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src);
2030   std::pair<InstructionCost, MVT> LTDest =
2031       TLI->getTypeLegalizationCost(DL, Dst);
2032 
2033   if (ST->hasSSE2() && !ST->hasAVX()) {
2034     if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD,
2035                                                    LTDest.second, LTSrc.second))
2036       return AdjustCost(LTSrc.first * Entry->Cost);
2037   }
2038 
2039   EVT SrcTy = TLI->getValueType(DL, Src);
2040   EVT DstTy = TLI->getValueType(DL, Dst);
2041 
2042   // The function getSimpleVT only handles simple value types.
2043   if (!SrcTy.isSimple() || !DstTy.isSimple())
2044     return AdjustCost(BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind));
2045 
2046   MVT SimpleSrcTy = SrcTy.getSimpleVT();
2047   MVT SimpleDstTy = DstTy.getSimpleVT();
2048 
2049   if (ST->useAVX512Regs()) {
2050     if (ST->hasBWI())
2051       if (const auto *Entry = ConvertCostTableLookup(AVX512BWConversionTbl, ISD,
2052                                                      SimpleDstTy, SimpleSrcTy))
2053         return AdjustCost(Entry->Cost);
2054 
2055     if (ST->hasDQI())
2056       if (const auto *Entry = ConvertCostTableLookup(AVX512DQConversionTbl, ISD,
2057                                                      SimpleDstTy, SimpleSrcTy))
2058         return AdjustCost(Entry->Cost);
2059 
2060     if (ST->hasAVX512())
2061       if (const auto *Entry = ConvertCostTableLookup(AVX512FConversionTbl, ISD,
2062                                                      SimpleDstTy, SimpleSrcTy))
2063         return AdjustCost(Entry->Cost);
2064   }
2065 
2066   if (ST->hasBWI())
2067     if (const auto *Entry = ConvertCostTableLookup(AVX512BWVLConversionTbl, ISD,
2068                                                    SimpleDstTy, SimpleSrcTy))
2069       return AdjustCost(Entry->Cost);
2070 
2071   if (ST->hasDQI())
2072     if (const auto *Entry = ConvertCostTableLookup(AVX512DQVLConversionTbl, ISD,
2073                                                    SimpleDstTy, SimpleSrcTy))
2074       return AdjustCost(Entry->Cost);
2075 
2076   if (ST->hasAVX512())
2077     if (const auto *Entry = ConvertCostTableLookup(AVX512VLConversionTbl, ISD,
2078                                                    SimpleDstTy, SimpleSrcTy))
2079       return AdjustCost(Entry->Cost);
2080 
2081   if (ST->hasAVX2()) {
2082     if (const auto *Entry = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
2083                                                    SimpleDstTy, SimpleSrcTy))
2084       return AdjustCost(Entry->Cost);
2085   }
2086 
2087   if (ST->hasAVX()) {
2088     if (const auto *Entry = ConvertCostTableLookup(AVXConversionTbl, ISD,
2089                                                    SimpleDstTy, SimpleSrcTy))
2090       return AdjustCost(Entry->Cost);
2091   }
2092 
2093   if (ST->hasSSE41()) {
2094     if (const auto *Entry = ConvertCostTableLookup(SSE41ConversionTbl, ISD,
2095                                                    SimpleDstTy, SimpleSrcTy))
2096       return AdjustCost(Entry->Cost);
2097   }
2098 
2099   if (ST->hasSSE2()) {
2100     if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD,
2101                                                    SimpleDstTy, SimpleSrcTy))
2102       return AdjustCost(Entry->Cost);
2103   }
2104 
2105   return AdjustCost(
2106       BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I));
2107 }
2108 
2109 InstructionCost X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
2110                                                Type *CondTy,
2111                                                CmpInst::Predicate VecPred,
2112                                                TTI::TargetCostKind CostKind,
2113                                                const Instruction *I) {
2114   // TODO: Handle other cost kinds.
2115   if (CostKind != TTI::TCK_RecipThroughput)
2116     return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind,
2117                                      I);
2118 
2119   // Legalize the type.
2120   std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
2121 
2122   MVT MTy = LT.second;
2123 
2124   int ISD = TLI->InstructionOpcodeToISD(Opcode);
2125   assert(ISD && "Invalid opcode");
2126 
2127   unsigned ExtraCost = 0;
2128   if (I && (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)) {
2129     // Some vector comparison predicates cost extra instructions.
2130     if (MTy.isVector() &&
2131         !((ST->hasXOP() && (!ST->hasAVX2() || MTy.is128BitVector())) ||
2132           (ST->hasAVX512() && 32 <= MTy.getScalarSizeInBits()) ||
2133           ST->hasBWI())) {
2134       switch (cast<CmpInst>(I)->getPredicate()) {
2135       case CmpInst::Predicate::ICMP_NE:
2136         // xor(cmpeq(x,y),-1)
2137         ExtraCost = 1;
2138         break;
2139       case CmpInst::Predicate::ICMP_SGE:
2140       case CmpInst::Predicate::ICMP_SLE:
2141         // xor(cmpgt(x,y),-1)
2142         ExtraCost = 1;
2143         break;
2144       case CmpInst::Predicate::ICMP_ULT:
2145       case CmpInst::Predicate::ICMP_UGT:
2146         // cmpgt(xor(x,signbit),xor(y,signbit))
2147         // xor(cmpeq(pmaxu(x,y),x),-1)
2148         ExtraCost = 2;
2149         break;
2150       case CmpInst::Predicate::ICMP_ULE:
2151       case CmpInst::Predicate::ICMP_UGE:
2152         if ((ST->hasSSE41() && MTy.getScalarSizeInBits() == 32) ||
2153             (ST->hasSSE2() && MTy.getScalarSizeInBits() < 32)) {
2154           // cmpeq(psubus(x,y),0)
2155           // cmpeq(pminu(x,y),x)
2156           ExtraCost = 1;
2157         } else {
2158           // xor(cmpgt(xor(x,signbit),xor(y,signbit)),-1)
2159           ExtraCost = 3;
2160         }
2161         break;
2162       default:
2163         break;
2164       }
2165     }
2166   }
2167 
2168   static const CostTblEntry SLMCostTbl[] = {
2169     // slm pcmpeq/pcmpgt throughput is 2
2170     { ISD::SETCC,   MVT::v2i64,   2 },
2171   };
2172 
2173   static const CostTblEntry AVX512BWCostTbl[] = {
2174     { ISD::SETCC,   MVT::v32i16,  1 },
2175     { ISD::SETCC,   MVT::v64i8,   1 },
2176 
2177     { ISD::SELECT,  MVT::v32i16,  1 },
2178     { ISD::SELECT,  MVT::v64i8,   1 },
2179   };
2180 
2181   static const CostTblEntry AVX512CostTbl[] = {
2182     { ISD::SETCC,   MVT::v8i64,   1 },
2183     { ISD::SETCC,   MVT::v16i32,  1 },
2184     { ISD::SETCC,   MVT::v8f64,   1 },
2185     { ISD::SETCC,   MVT::v16f32,  1 },
2186 
2187     { ISD::SELECT,  MVT::v8i64,   1 },
2188     { ISD::SELECT,  MVT::v16i32,  1 },
2189     { ISD::SELECT,  MVT::v8f64,   1 },
2190     { ISD::SELECT,  MVT::v16f32,  1 },
2191 
2192     { ISD::SETCC,   MVT::v32i16,  2 }, // FIXME: should probably be 4
2193     { ISD::SETCC,   MVT::v64i8,   2 }, // FIXME: should probably be 4
2194 
2195     { ISD::SELECT,  MVT::v32i16,  2 }, // FIXME: should be 3
2196     { ISD::SELECT,  MVT::v64i8,   2 }, // FIXME: should be 3
2197   };
2198 
2199   static const CostTblEntry AVX2CostTbl[] = {
2200     { ISD::SETCC,   MVT::v4i64,   1 },
2201     { ISD::SETCC,   MVT::v8i32,   1 },
2202     { ISD::SETCC,   MVT::v16i16,  1 },
2203     { ISD::SETCC,   MVT::v32i8,   1 },
2204 
2205     { ISD::SELECT,  MVT::v4i64,   1 }, // pblendvb
2206     { ISD::SELECT,  MVT::v8i32,   1 }, // pblendvb
2207     { ISD::SELECT,  MVT::v16i16,  1 }, // pblendvb
2208     { ISD::SELECT,  MVT::v32i8,   1 }, // pblendvb
2209   };
2210 
2211   static const CostTblEntry AVX1CostTbl[] = {
2212     { ISD::SETCC,   MVT::v4f64,   1 },
2213     { ISD::SETCC,   MVT::v8f32,   1 },
2214     // AVX1 does not support 8-wide integer compare.
2215     { ISD::SETCC,   MVT::v4i64,   4 },
2216     { ISD::SETCC,   MVT::v8i32,   4 },
2217     { ISD::SETCC,   MVT::v16i16,  4 },
2218     { ISD::SETCC,   MVT::v32i8,   4 },
2219 
2220     { ISD::SELECT,  MVT::v4f64,   1 }, // vblendvpd
2221     { ISD::SELECT,  MVT::v8f32,   1 }, // vblendvps
2222     { ISD::SELECT,  MVT::v4i64,   1 }, // vblendvpd
2223     { ISD::SELECT,  MVT::v8i32,   1 }, // vblendvps
2224     { ISD::SELECT,  MVT::v16i16,  3 }, // vandps + vandnps + vorps
2225     { ISD::SELECT,  MVT::v32i8,   3 }, // vandps + vandnps + vorps
2226   };
2227 
2228   static const CostTblEntry SSE42CostTbl[] = {
2229     { ISD::SETCC,   MVT::v2f64,   1 },
2230     { ISD::SETCC,   MVT::v4f32,   1 },
2231     { ISD::SETCC,   MVT::v2i64,   1 },
2232   };
2233 
2234   static const CostTblEntry SSE41CostTbl[] = {
2235     { ISD::SELECT,  MVT::v2f64,   1 }, // blendvpd
2236     { ISD::SELECT,  MVT::v4f32,   1 }, // blendvps
2237     { ISD::SELECT,  MVT::v2i64,   1 }, // pblendvb
2238     { ISD::SELECT,  MVT::v4i32,   1 }, // pblendvb
2239     { ISD::SELECT,  MVT::v8i16,   1 }, // pblendvb
2240     { ISD::SELECT,  MVT::v16i8,   1 }, // pblendvb
2241   };
2242 
2243   static const CostTblEntry SSE2CostTbl[] = {
2244     { ISD::SETCC,   MVT::v2f64,   2 },
2245     { ISD::SETCC,   MVT::f64,     1 },
2246     { ISD::SETCC,   MVT::v2i64,   8 },
2247     { ISD::SETCC,   MVT::v4i32,   1 },
2248     { ISD::SETCC,   MVT::v8i16,   1 },
2249     { ISD::SETCC,   MVT::v16i8,   1 },
2250 
2251     { ISD::SELECT,  MVT::v2f64,   3 }, // andpd + andnpd + orpd
2252     { ISD::SELECT,  MVT::v2i64,   3 }, // pand + pandn + por
2253     { ISD::SELECT,  MVT::v4i32,   3 }, // pand + pandn + por
2254     { ISD::SELECT,  MVT::v8i16,   3 }, // pand + pandn + por
2255     { ISD::SELECT,  MVT::v16i8,   3 }, // pand + pandn + por
2256   };
2257 
2258   static const CostTblEntry SSE1CostTbl[] = {
2259     { ISD::SETCC,   MVT::v4f32,   2 },
2260     { ISD::SETCC,   MVT::f32,     1 },
2261 
2262     { ISD::SELECT,  MVT::v4f32,   3 }, // andps + andnps + orps
2263   };
2264 
2265   if (ST->isSLM())
2266     if (const auto *Entry = CostTableLookup(SLMCostTbl, ISD, MTy))
2267       return LT.first * (ExtraCost + Entry->Cost);
2268 
2269   if (ST->hasBWI())
2270     if (const auto *Entry = CostTableLookup(AVX512BWCostTbl, ISD, MTy))
2271       return LT.first * (ExtraCost + Entry->Cost);
2272 
2273   if (ST->hasAVX512())
2274     if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
2275       return LT.first * (ExtraCost + Entry->Cost);
2276 
2277   if (ST->hasAVX2())
2278     if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy))
2279       return LT.first * (ExtraCost + Entry->Cost);
2280 
2281   if (ST->hasAVX())
2282     if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy))
2283       return LT.first * (ExtraCost + Entry->Cost);
2284 
2285   if (ST->hasSSE42())
2286     if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy))
2287       return LT.first * (ExtraCost + Entry->Cost);
2288 
2289   if (ST->hasSSE41())
2290     if (const auto *Entry = CostTableLookup(SSE41CostTbl, ISD, MTy))
2291       return LT.first * (ExtraCost + Entry->Cost);
2292 
2293   if (ST->hasSSE2())
2294     if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy))
2295       return LT.first * (ExtraCost + Entry->Cost);
2296 
2297   if (ST->hasSSE1())
2298     if (const auto *Entry = CostTableLookup(SSE1CostTbl, ISD, MTy))
2299       return LT.first * (ExtraCost + Entry->Cost);
2300 
2301   return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, I);
2302 }
2303 
2304 unsigned X86TTIImpl::getAtomicMemIntrinsicMaxElementSize() const { return 16; }
2305 
2306 InstructionCost
2307 X86TTIImpl::getTypeBasedIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
2308                                            TTI::TargetCostKind CostKind) {
2309 
2310   // Costs should match the codegen from:
2311   // BITREVERSE: llvm\test\CodeGen\X86\vector-bitreverse.ll
2312   // BSWAP: llvm\test\CodeGen\X86\bswap-vector.ll
2313   // CTLZ: llvm\test\CodeGen\X86\vector-lzcnt-*.ll
2314   // CTPOP: llvm\test\CodeGen\X86\vector-popcnt-*.ll
2315   // CTTZ: llvm\test\CodeGen\X86\vector-tzcnt-*.ll
2316 
2317   // TODO: Overflow intrinsics (*ADDO, *SUBO, *MULO) with vector types are not
2318   //       specialized in these tables yet.
2319   static const CostTblEntry AVX512CDCostTbl[] = {
2320     { ISD::CTLZ,       MVT::v8i64,   1 },
2321     { ISD::CTLZ,       MVT::v16i32,  1 },
2322     { ISD::CTLZ,       MVT::v32i16,  8 },
2323     { ISD::CTLZ,       MVT::v64i8,  20 },
2324     { ISD::CTLZ,       MVT::v4i64,   1 },
2325     { ISD::CTLZ,       MVT::v8i32,   1 },
2326     { ISD::CTLZ,       MVT::v16i16,  4 },
2327     { ISD::CTLZ,       MVT::v32i8,  10 },
2328     { ISD::CTLZ,       MVT::v2i64,   1 },
2329     { ISD::CTLZ,       MVT::v4i32,   1 },
2330     { ISD::CTLZ,       MVT::v8i16,   4 },
2331     { ISD::CTLZ,       MVT::v16i8,   4 },
2332   };
2333   static const CostTblEntry AVX512BWCostTbl[] = {
2334     { ISD::ABS,        MVT::v32i16,  1 },
2335     { ISD::ABS,        MVT::v64i8,   1 },
2336     { ISD::BITREVERSE, MVT::v8i64,   5 },
2337     { ISD::BITREVERSE, MVT::v16i32,  5 },
2338     { ISD::BITREVERSE, MVT::v32i16,  5 },
2339     { ISD::BITREVERSE, MVT::v64i8,   5 },
2340     { ISD::CTLZ,       MVT::v8i64,  23 },
2341     { ISD::CTLZ,       MVT::v16i32, 22 },
2342     { ISD::CTLZ,       MVT::v32i16, 18 },
2343     { ISD::CTLZ,       MVT::v64i8,  17 },
2344     { ISD::CTPOP,      MVT::v8i64,   7 },
2345     { ISD::CTPOP,      MVT::v16i32, 11 },
2346     { ISD::CTPOP,      MVT::v32i16,  9 },
2347     { ISD::CTPOP,      MVT::v64i8,   6 },
2348     { ISD::CTTZ,       MVT::v8i64,  10 },
2349     { ISD::CTTZ,       MVT::v16i32, 14 },
2350     { ISD::CTTZ,       MVT::v32i16, 12 },
2351     { ISD::CTTZ,       MVT::v64i8,   9 },
2352     { ISD::SADDSAT,    MVT::v32i16,  1 },
2353     { ISD::SADDSAT,    MVT::v64i8,   1 },
2354     { ISD::SMAX,       MVT::v32i16,  1 },
2355     { ISD::SMAX,       MVT::v64i8,   1 },
2356     { ISD::SMIN,       MVT::v32i16,  1 },
2357     { ISD::SMIN,       MVT::v64i8,   1 },
2358     { ISD::SSUBSAT,    MVT::v32i16,  1 },
2359     { ISD::SSUBSAT,    MVT::v64i8,   1 },
2360     { ISD::UADDSAT,    MVT::v32i16,  1 },
2361     { ISD::UADDSAT,    MVT::v64i8,   1 },
2362     { ISD::UMAX,       MVT::v32i16,  1 },
2363     { ISD::UMAX,       MVT::v64i8,   1 },
2364     { ISD::UMIN,       MVT::v32i16,  1 },
2365     { ISD::UMIN,       MVT::v64i8,   1 },
2366     { ISD::USUBSAT,    MVT::v32i16,  1 },
2367     { ISD::USUBSAT,    MVT::v64i8,   1 },
2368   };
2369   static const CostTblEntry AVX512CostTbl[] = {
2370     { ISD::ABS,        MVT::v8i64,   1 },
2371     { ISD::ABS,        MVT::v16i32,  1 },
2372     { ISD::ABS,        MVT::v32i16,  2 }, // FIXME: include split
2373     { ISD::ABS,        MVT::v64i8,   2 }, // FIXME: include split
2374     { ISD::ABS,        MVT::v4i64,   1 },
2375     { ISD::ABS,        MVT::v2i64,   1 },
2376     { ISD::BITREVERSE, MVT::v8i64,  36 },
2377     { ISD::BITREVERSE, MVT::v16i32, 24 },
2378     { ISD::BITREVERSE, MVT::v32i16, 10 },
2379     { ISD::BITREVERSE, MVT::v64i8,  10 },
2380     { ISD::CTLZ,       MVT::v8i64,  29 },
2381     { ISD::CTLZ,       MVT::v16i32, 35 },
2382     { ISD::CTLZ,       MVT::v32i16, 28 },
2383     { ISD::CTLZ,       MVT::v64i8,  18 },
2384     { ISD::CTPOP,      MVT::v8i64,  16 },
2385     { ISD::CTPOP,      MVT::v16i32, 24 },
2386     { ISD::CTPOP,      MVT::v32i16, 18 },
2387     { ISD::CTPOP,      MVT::v64i8,  12 },
2388     { ISD::CTTZ,       MVT::v8i64,  20 },
2389     { ISD::CTTZ,       MVT::v16i32, 28 },
2390     { ISD::CTTZ,       MVT::v32i16, 24 },
2391     { ISD::CTTZ,       MVT::v64i8,  18 },
2392     { ISD::SMAX,       MVT::v8i64,   1 },
2393     { ISD::SMAX,       MVT::v16i32,  1 },
2394     { ISD::SMAX,       MVT::v32i16,  2 }, // FIXME: include split
2395     { ISD::SMAX,       MVT::v64i8,   2 }, // FIXME: include split
2396     { ISD::SMAX,       MVT::v4i64,   1 },
2397     { ISD::SMAX,       MVT::v2i64,   1 },
2398     { ISD::SMIN,       MVT::v8i64,   1 },
2399     { ISD::SMIN,       MVT::v16i32,  1 },
2400     { ISD::SMIN,       MVT::v32i16,  2 }, // FIXME: include split
2401     { ISD::SMIN,       MVT::v64i8,   2 }, // FIXME: include split
2402     { ISD::SMIN,       MVT::v4i64,   1 },
2403     { ISD::SMIN,       MVT::v2i64,   1 },
2404     { ISD::UMAX,       MVT::v8i64,   1 },
2405     { ISD::UMAX,       MVT::v16i32,  1 },
2406     { ISD::UMAX,       MVT::v32i16,  2 }, // FIXME: include split
2407     { ISD::UMAX,       MVT::v64i8,   2 }, // FIXME: include split
2408     { ISD::UMAX,       MVT::v4i64,   1 },
2409     { ISD::UMAX,       MVT::v2i64,   1 },
2410     { ISD::UMIN,       MVT::v8i64,   1 },
2411     { ISD::UMIN,       MVT::v16i32,  1 },
2412     { ISD::UMIN,       MVT::v32i16,  2 }, // FIXME: include split
2413     { ISD::UMIN,       MVT::v64i8,   2 }, // FIXME: include split
2414     { ISD::UMIN,       MVT::v4i64,   1 },
2415     { ISD::UMIN,       MVT::v2i64,   1 },
2416     { ISD::USUBSAT,    MVT::v16i32,  2 }, // pmaxud + psubd
2417     { ISD::USUBSAT,    MVT::v2i64,   2 }, // pmaxuq + psubq
2418     { ISD::USUBSAT,    MVT::v4i64,   2 }, // pmaxuq + psubq
2419     { ISD::USUBSAT,    MVT::v8i64,   2 }, // pmaxuq + psubq
2420     { ISD::UADDSAT,    MVT::v16i32,  3 }, // not + pminud + paddd
2421     { ISD::UADDSAT,    MVT::v2i64,   3 }, // not + pminuq + paddq
2422     { ISD::UADDSAT,    MVT::v4i64,   3 }, // not + pminuq + paddq
2423     { ISD::UADDSAT,    MVT::v8i64,   3 }, // not + pminuq + paddq
2424     { ISD::SADDSAT,    MVT::v32i16,  2 }, // FIXME: include split
2425     { ISD::SADDSAT,    MVT::v64i8,   2 }, // FIXME: include split
2426     { ISD::SSUBSAT,    MVT::v32i16,  2 }, // FIXME: include split
2427     { ISD::SSUBSAT,    MVT::v64i8,   2 }, // FIXME: include split
2428     { ISD::UADDSAT,    MVT::v32i16,  2 }, // FIXME: include split
2429     { ISD::UADDSAT,    MVT::v64i8,   2 }, // FIXME: include split
2430     { ISD::USUBSAT,    MVT::v32i16,  2 }, // FIXME: include split
2431     { ISD::USUBSAT,    MVT::v64i8,   2 }, // FIXME: include split
2432     { ISD::FMAXNUM,    MVT::f32,     2 },
2433     { ISD::FMAXNUM,    MVT::v4f32,   2 },
2434     { ISD::FMAXNUM,    MVT::v8f32,   2 },
2435     { ISD::FMAXNUM,    MVT::v16f32,  2 },
2436     { ISD::FMAXNUM,    MVT::f64,     2 },
2437     { ISD::FMAXNUM,    MVT::v2f64,   2 },
2438     { ISD::FMAXNUM,    MVT::v4f64,   2 },
2439     { ISD::FMAXNUM,    MVT::v8f64,   2 },
2440   };
2441   static const CostTblEntry XOPCostTbl[] = {
2442     { ISD::BITREVERSE, MVT::v4i64,   4 },
2443     { ISD::BITREVERSE, MVT::v8i32,   4 },
2444     { ISD::BITREVERSE, MVT::v16i16,  4 },
2445     { ISD::BITREVERSE, MVT::v32i8,   4 },
2446     { ISD::BITREVERSE, MVT::v2i64,   1 },
2447     { ISD::BITREVERSE, MVT::v4i32,   1 },
2448     { ISD::BITREVERSE, MVT::v8i16,   1 },
2449     { ISD::BITREVERSE, MVT::v16i8,   1 },
2450     { ISD::BITREVERSE, MVT::i64,     3 },
2451     { ISD::BITREVERSE, MVT::i32,     3 },
2452     { ISD::BITREVERSE, MVT::i16,     3 },
2453     { ISD::BITREVERSE, MVT::i8,      3 }
2454   };
2455   static const CostTblEntry AVX2CostTbl[] = {
2456     { ISD::ABS,        MVT::v4i64,   2 }, // VBLENDVPD(X,VPSUBQ(0,X),X)
2457     { ISD::ABS,        MVT::v8i32,   1 },
2458     { ISD::ABS,        MVT::v16i16,  1 },
2459     { ISD::ABS,        MVT::v32i8,   1 },
2460     { ISD::BITREVERSE, MVT::v4i64,   5 },
2461     { ISD::BITREVERSE, MVT::v8i32,   5 },
2462     { ISD::BITREVERSE, MVT::v16i16,  5 },
2463     { ISD::BITREVERSE, MVT::v32i8,   5 },
2464     { ISD::BSWAP,      MVT::v4i64,   1 },
2465     { ISD::BSWAP,      MVT::v8i32,   1 },
2466     { ISD::BSWAP,      MVT::v16i16,  1 },
2467     { ISD::CTLZ,       MVT::v4i64,  23 },
2468     { ISD::CTLZ,       MVT::v8i32,  18 },
2469     { ISD::CTLZ,       MVT::v16i16, 14 },
2470     { ISD::CTLZ,       MVT::v32i8,   9 },
2471     { ISD::CTPOP,      MVT::v4i64,   7 },
2472     { ISD::CTPOP,      MVT::v8i32,  11 },
2473     { ISD::CTPOP,      MVT::v16i16,  9 },
2474     { ISD::CTPOP,      MVT::v32i8,   6 },
2475     { ISD::CTTZ,       MVT::v4i64,  10 },
2476     { ISD::CTTZ,       MVT::v8i32,  14 },
2477     { ISD::CTTZ,       MVT::v16i16, 12 },
2478     { ISD::CTTZ,       MVT::v32i8,   9 },
2479     { ISD::SADDSAT,    MVT::v16i16,  1 },
2480     { ISD::SADDSAT,    MVT::v32i8,   1 },
2481     { ISD::SMAX,       MVT::v8i32,   1 },
2482     { ISD::SMAX,       MVT::v16i16,  1 },
2483     { ISD::SMAX,       MVT::v32i8,   1 },
2484     { ISD::SMIN,       MVT::v8i32,   1 },
2485     { ISD::SMIN,       MVT::v16i16,  1 },
2486     { ISD::SMIN,       MVT::v32i8,   1 },
2487     { ISD::SSUBSAT,    MVT::v16i16,  1 },
2488     { ISD::SSUBSAT,    MVT::v32i8,   1 },
2489     { ISD::UADDSAT,    MVT::v16i16,  1 },
2490     { ISD::UADDSAT,    MVT::v32i8,   1 },
2491     { ISD::UADDSAT,    MVT::v8i32,   3 }, // not + pminud + paddd
2492     { ISD::UMAX,       MVT::v8i32,   1 },
2493     { ISD::UMAX,       MVT::v16i16,  1 },
2494     { ISD::UMAX,       MVT::v32i8,   1 },
2495     { ISD::UMIN,       MVT::v8i32,   1 },
2496     { ISD::UMIN,       MVT::v16i16,  1 },
2497     { ISD::UMIN,       MVT::v32i8,   1 },
2498     { ISD::USUBSAT,    MVT::v16i16,  1 },
2499     { ISD::USUBSAT,    MVT::v32i8,   1 },
2500     { ISD::USUBSAT,    MVT::v8i32,   2 }, // pmaxud + psubd
2501     { ISD::FMAXNUM,    MVT::v8f32,   3 }, // MAXPS + CMPUNORDPS + BLENDVPS
2502     { ISD::FMAXNUM,    MVT::v4f64,   3 }, // MAXPD + CMPUNORDPD + BLENDVPD
2503     { ISD::FSQRT,      MVT::f32,     7 }, // Haswell from http://www.agner.org/
2504     { ISD::FSQRT,      MVT::v4f32,   7 }, // Haswell from http://www.agner.org/
2505     { ISD::FSQRT,      MVT::v8f32,  14 }, // Haswell from http://www.agner.org/
2506     { ISD::FSQRT,      MVT::f64,    14 }, // Haswell from http://www.agner.org/
2507     { ISD::FSQRT,      MVT::v2f64,  14 }, // Haswell from http://www.agner.org/
2508     { ISD::FSQRT,      MVT::v4f64,  28 }, // Haswell from http://www.agner.org/
2509   };
2510   static const CostTblEntry AVX1CostTbl[] = {
2511     { ISD::ABS,        MVT::v4i64,   5 }, // VBLENDVPD(X,VPSUBQ(0,X),X)
2512     { ISD::ABS,        MVT::v8i32,   3 },
2513     { ISD::ABS,        MVT::v16i16,  3 },
2514     { ISD::ABS,        MVT::v32i8,   3 },
2515     { ISD::BITREVERSE, MVT::v4i64,  12 }, // 2 x 128-bit Op + extract/insert
2516     { ISD::BITREVERSE, MVT::v8i32,  12 }, // 2 x 128-bit Op + extract/insert
2517     { ISD::BITREVERSE, MVT::v16i16, 12 }, // 2 x 128-bit Op + extract/insert
2518     { ISD::BITREVERSE, MVT::v32i8,  12 }, // 2 x 128-bit Op + extract/insert
2519     { ISD::BSWAP,      MVT::v4i64,   4 },
2520     { ISD::BSWAP,      MVT::v8i32,   4 },
2521     { ISD::BSWAP,      MVT::v16i16,  4 },
2522     { ISD::CTLZ,       MVT::v4i64,  48 }, // 2 x 128-bit Op + extract/insert
2523     { ISD::CTLZ,       MVT::v8i32,  38 }, // 2 x 128-bit Op + extract/insert
2524     { ISD::CTLZ,       MVT::v16i16, 30 }, // 2 x 128-bit Op + extract/insert
2525     { ISD::CTLZ,       MVT::v32i8,  20 }, // 2 x 128-bit Op + extract/insert
2526     { ISD::CTPOP,      MVT::v4i64,  16 }, // 2 x 128-bit Op + extract/insert
2527     { ISD::CTPOP,      MVT::v8i32,  24 }, // 2 x 128-bit Op + extract/insert
2528     { ISD::CTPOP,      MVT::v16i16, 20 }, // 2 x 128-bit Op + extract/insert
2529     { ISD::CTPOP,      MVT::v32i8,  14 }, // 2 x 128-bit Op + extract/insert
2530     { ISD::CTTZ,       MVT::v4i64,  22 }, // 2 x 128-bit Op + extract/insert
2531     { ISD::CTTZ,       MVT::v8i32,  30 }, // 2 x 128-bit Op + extract/insert
2532     { ISD::CTTZ,       MVT::v16i16, 26 }, // 2 x 128-bit Op + extract/insert
2533     { ISD::CTTZ,       MVT::v32i8,  20 }, // 2 x 128-bit Op + extract/insert
2534     { ISD::SADDSAT,    MVT::v16i16,  4 }, // 2 x 128-bit Op + extract/insert
2535     { ISD::SADDSAT,    MVT::v32i8,   4 }, // 2 x 128-bit Op + extract/insert
2536     { ISD::SMAX,       MVT::v8i32,   4 }, // 2 x 128-bit Op + extract/insert
2537     { ISD::SMAX,       MVT::v16i16,  4 }, // 2 x 128-bit Op + extract/insert
2538     { ISD::SMAX,       MVT::v32i8,   4 }, // 2 x 128-bit Op + extract/insert
2539     { ISD::SMIN,       MVT::v8i32,   4 }, // 2 x 128-bit Op + extract/insert
2540     { ISD::SMIN,       MVT::v16i16,  4 }, // 2 x 128-bit Op + extract/insert
2541     { ISD::SMIN,       MVT::v32i8,   4 }, // 2 x 128-bit Op + extract/insert
2542     { ISD::SSUBSAT,    MVT::v16i16,  4 }, // 2 x 128-bit Op + extract/insert
2543     { ISD::SSUBSAT,    MVT::v32i8,   4 }, // 2 x 128-bit Op + extract/insert
2544     { ISD::UADDSAT,    MVT::v16i16,  4 }, // 2 x 128-bit Op + extract/insert
2545     { ISD::UADDSAT,    MVT::v32i8,   4 }, // 2 x 128-bit Op + extract/insert
2546     { ISD::UADDSAT,    MVT::v8i32,   8 }, // 2 x 128-bit Op + extract/insert
2547     { ISD::UMAX,       MVT::v8i32,   4 }, // 2 x 128-bit Op + extract/insert
2548     { ISD::UMAX,       MVT::v16i16,  4 }, // 2 x 128-bit Op + extract/insert
2549     { ISD::UMAX,       MVT::v32i8,   4 }, // 2 x 128-bit Op + extract/insert
2550     { ISD::UMIN,       MVT::v8i32,   4 }, // 2 x 128-bit Op + extract/insert
2551     { ISD::UMIN,       MVT::v16i16,  4 }, // 2 x 128-bit Op + extract/insert
2552     { ISD::UMIN,       MVT::v32i8,   4 }, // 2 x 128-bit Op + extract/insert
2553     { ISD::USUBSAT,    MVT::v16i16,  4 }, // 2 x 128-bit Op + extract/insert
2554     { ISD::USUBSAT,    MVT::v32i8,   4 }, // 2 x 128-bit Op + extract/insert
2555     { ISD::USUBSAT,    MVT::v8i32,   6 }, // 2 x 128-bit Op + extract/insert
2556     { ISD::FMAXNUM,    MVT::f32,     3 }, // MAXSS + CMPUNORDSS + BLENDVPS
2557     { ISD::FMAXNUM,    MVT::v4f32,   3 }, // MAXPS + CMPUNORDPS + BLENDVPS
2558     { ISD::FMAXNUM,    MVT::v8f32,   5 }, // MAXPS + CMPUNORDPS + BLENDVPS + ?
2559     { ISD::FMAXNUM,    MVT::f64,     3 }, // MAXSD + CMPUNORDSD + BLENDVPD
2560     { ISD::FMAXNUM,    MVT::v2f64,   3 }, // MAXPD + CMPUNORDPD + BLENDVPD
2561     { ISD::FMAXNUM,    MVT::v4f64,   5 }, // MAXPD + CMPUNORDPD + BLENDVPD + ?
2562     { ISD::FSQRT,      MVT::f32,    14 }, // SNB from http://www.agner.org/
2563     { ISD::FSQRT,      MVT::v4f32,  14 }, // SNB from http://www.agner.org/
2564     { ISD::FSQRT,      MVT::v8f32,  28 }, // SNB from http://www.agner.org/
2565     { ISD::FSQRT,      MVT::f64,    21 }, // SNB from http://www.agner.org/
2566     { ISD::FSQRT,      MVT::v2f64,  21 }, // SNB from http://www.agner.org/
2567     { ISD::FSQRT,      MVT::v4f64,  43 }, // SNB from http://www.agner.org/
2568   };
2569   static const CostTblEntry GLMCostTbl[] = {
2570     { ISD::FSQRT, MVT::f32,   19 }, // sqrtss
2571     { ISD::FSQRT, MVT::v4f32, 37 }, // sqrtps
2572     { ISD::FSQRT, MVT::f64,   34 }, // sqrtsd
2573     { ISD::FSQRT, MVT::v2f64, 67 }, // sqrtpd
2574   };
2575   static const CostTblEntry SLMCostTbl[] = {
2576     { ISD::FSQRT, MVT::f32,   20 }, // sqrtss
2577     { ISD::FSQRT, MVT::v4f32, 40 }, // sqrtps
2578     { ISD::FSQRT, MVT::f64,   35 }, // sqrtsd
2579     { ISD::FSQRT, MVT::v2f64, 70 }, // sqrtpd
2580   };
2581   static const CostTblEntry SSE42CostTbl[] = {
2582     { ISD::USUBSAT,    MVT::v4i32,   2 }, // pmaxud + psubd
2583     { ISD::UADDSAT,    MVT::v4i32,   3 }, // not + pminud + paddd
2584     { ISD::FSQRT,      MVT::f32,    18 }, // Nehalem from http://www.agner.org/
2585     { ISD::FSQRT,      MVT::v4f32,  18 }, // Nehalem from http://www.agner.org/
2586   };
2587   static const CostTblEntry SSE41CostTbl[] = {
2588     { ISD::ABS,        MVT::v2i64,   2 }, // BLENDVPD(X,PSUBQ(0,X),X)
2589     { ISD::SMAX,       MVT::v4i32,   1 },
2590     { ISD::SMAX,       MVT::v16i8,   1 },
2591     { ISD::SMIN,       MVT::v4i32,   1 },
2592     { ISD::SMIN,       MVT::v16i8,   1 },
2593     { ISD::UMAX,       MVT::v4i32,   1 },
2594     { ISD::UMAX,       MVT::v8i16,   1 },
2595     { ISD::UMIN,       MVT::v4i32,   1 },
2596     { ISD::UMIN,       MVT::v8i16,   1 },
2597   };
2598   static const CostTblEntry SSSE3CostTbl[] = {
2599     { ISD::ABS,        MVT::v4i32,   1 },
2600     { ISD::ABS,        MVT::v8i16,   1 },
2601     { ISD::ABS,        MVT::v16i8,   1 },
2602     { ISD::BITREVERSE, MVT::v2i64,   5 },
2603     { ISD::BITREVERSE, MVT::v4i32,   5 },
2604     { ISD::BITREVERSE, MVT::v8i16,   5 },
2605     { ISD::BITREVERSE, MVT::v16i8,   5 },
2606     { ISD::BSWAP,      MVT::v2i64,   1 },
2607     { ISD::BSWAP,      MVT::v4i32,   1 },
2608     { ISD::BSWAP,      MVT::v8i16,   1 },
2609     { ISD::CTLZ,       MVT::v2i64,  23 },
2610     { ISD::CTLZ,       MVT::v4i32,  18 },
2611     { ISD::CTLZ,       MVT::v8i16,  14 },
2612     { ISD::CTLZ,       MVT::v16i8,   9 },
2613     { ISD::CTPOP,      MVT::v2i64,   7 },
2614     { ISD::CTPOP,      MVT::v4i32,  11 },
2615     { ISD::CTPOP,      MVT::v8i16,   9 },
2616     { ISD::CTPOP,      MVT::v16i8,   6 },
2617     { ISD::CTTZ,       MVT::v2i64,  10 },
2618     { ISD::CTTZ,       MVT::v4i32,  14 },
2619     { ISD::CTTZ,       MVT::v8i16,  12 },
2620     { ISD::CTTZ,       MVT::v16i8,   9 }
2621   };
2622   static const CostTblEntry SSE2CostTbl[] = {
2623     { ISD::ABS,        MVT::v2i64,   4 },
2624     { ISD::ABS,        MVT::v4i32,   3 },
2625     { ISD::ABS,        MVT::v8i16,   2 },
2626     { ISD::ABS,        MVT::v16i8,   2 },
2627     { ISD::BITREVERSE, MVT::v2i64,  29 },
2628     { ISD::BITREVERSE, MVT::v4i32,  27 },
2629     { ISD::BITREVERSE, MVT::v8i16,  27 },
2630     { ISD::BITREVERSE, MVT::v16i8,  20 },
2631     { ISD::BSWAP,      MVT::v2i64,   7 },
2632     { ISD::BSWAP,      MVT::v4i32,   7 },
2633     { ISD::BSWAP,      MVT::v8i16,   7 },
2634     { ISD::CTLZ,       MVT::v2i64,  25 },
2635     { ISD::CTLZ,       MVT::v4i32,  26 },
2636     { ISD::CTLZ,       MVT::v8i16,  20 },
2637     { ISD::CTLZ,       MVT::v16i8,  17 },
2638     { ISD::CTPOP,      MVT::v2i64,  12 },
2639     { ISD::CTPOP,      MVT::v4i32,  15 },
2640     { ISD::CTPOP,      MVT::v8i16,  13 },
2641     { ISD::CTPOP,      MVT::v16i8,  10 },
2642     { ISD::CTTZ,       MVT::v2i64,  14 },
2643     { ISD::CTTZ,       MVT::v4i32,  18 },
2644     { ISD::CTTZ,       MVT::v8i16,  16 },
2645     { ISD::CTTZ,       MVT::v16i8,  13 },
2646     { ISD::SADDSAT,    MVT::v8i16,   1 },
2647     { ISD::SADDSAT,    MVT::v16i8,   1 },
2648     { ISD::SMAX,       MVT::v8i16,   1 },
2649     { ISD::SMIN,       MVT::v8i16,   1 },
2650     { ISD::SSUBSAT,    MVT::v8i16,   1 },
2651     { ISD::SSUBSAT,    MVT::v16i8,   1 },
2652     { ISD::UADDSAT,    MVT::v8i16,   1 },
2653     { ISD::UADDSAT,    MVT::v16i8,   1 },
2654     { ISD::UMAX,       MVT::v8i16,   2 },
2655     { ISD::UMAX,       MVT::v16i8,   1 },
2656     { ISD::UMIN,       MVT::v8i16,   2 },
2657     { ISD::UMIN,       MVT::v16i8,   1 },
2658     { ISD::USUBSAT,    MVT::v8i16,   1 },
2659     { ISD::USUBSAT,    MVT::v16i8,   1 },
2660     { ISD::FMAXNUM,    MVT::f64,     4 },
2661     { ISD::FMAXNUM,    MVT::v2f64,   4 },
2662     { ISD::FSQRT,      MVT::f64,    32 }, // Nehalem from http://www.agner.org/
2663     { ISD::FSQRT,      MVT::v2f64,  32 }, // Nehalem from http://www.agner.org/
2664   };
2665   static const CostTblEntry SSE1CostTbl[] = {
2666     { ISD::FMAXNUM,    MVT::f32,     4 },
2667     { ISD::FMAXNUM,    MVT::v4f32,   4 },
2668     { ISD::FSQRT,      MVT::f32,    28 }, // Pentium III from http://www.agner.org/
2669     { ISD::FSQRT,      MVT::v4f32,  56 }, // Pentium III from http://www.agner.org/
2670   };
2671   static const CostTblEntry BMI64CostTbl[] = { // 64-bit targets
2672     { ISD::CTTZ,       MVT::i64,     1 },
2673   };
2674   static const CostTblEntry BMI32CostTbl[] = { // 32 or 64-bit targets
2675     { ISD::CTTZ,       MVT::i32,     1 },
2676     { ISD::CTTZ,       MVT::i16,     1 },
2677     { ISD::CTTZ,       MVT::i8,      1 },
2678   };
2679   static const CostTblEntry LZCNT64CostTbl[] = { // 64-bit targets
2680     { ISD::CTLZ,       MVT::i64,     1 },
2681   };
2682   static const CostTblEntry LZCNT32CostTbl[] = { // 32 or 64-bit targets
2683     { ISD::CTLZ,       MVT::i32,     1 },
2684     { ISD::CTLZ,       MVT::i16,     1 },
2685     { ISD::CTLZ,       MVT::i8,      1 },
2686   };
2687   static const CostTblEntry POPCNT64CostTbl[] = { // 64-bit targets
2688     { ISD::CTPOP,      MVT::i64,     1 },
2689   };
2690   static const CostTblEntry POPCNT32CostTbl[] = { // 32 or 64-bit targets
2691     { ISD::CTPOP,      MVT::i32,     1 },
2692     { ISD::CTPOP,      MVT::i16,     1 },
2693     { ISD::CTPOP,      MVT::i8,      1 },
2694   };
2695   static const CostTblEntry X64CostTbl[] = { // 64-bit targets
2696     { ISD::ABS,        MVT::i64,     2 }, // SUB+CMOV
2697     { ISD::BITREVERSE, MVT::i64,    14 },
2698     { ISD::CTLZ,       MVT::i64,     4 }, // BSR+XOR or BSR+XOR+CMOV
2699     { ISD::CTTZ,       MVT::i64,     3 }, // TEST+BSF+CMOV/BRANCH
2700     { ISD::CTPOP,      MVT::i64,    10 },
2701     { ISD::SADDO,      MVT::i64,     1 },
2702     { ISD::UADDO,      MVT::i64,     1 },
2703     { ISD::UMULO,      MVT::i64,     2 }, // mulq + seto
2704   };
2705   static const CostTblEntry X86CostTbl[] = { // 32 or 64-bit targets
2706     { ISD::ABS,        MVT::i32,     2 }, // SUB+CMOV
2707     { ISD::ABS,        MVT::i16,     2 }, // SUB+CMOV
2708     { ISD::BITREVERSE, MVT::i32,    14 },
2709     { ISD::BITREVERSE, MVT::i16,    14 },
2710     { ISD::BITREVERSE, MVT::i8,     11 },
2711     { ISD::CTLZ,       MVT::i32,     4 }, // BSR+XOR or BSR+XOR+CMOV
2712     { ISD::CTLZ,       MVT::i16,     4 }, // BSR+XOR or BSR+XOR+CMOV
2713     { ISD::CTLZ,       MVT::i8,      4 }, // BSR+XOR or BSR+XOR+CMOV
2714     { ISD::CTTZ,       MVT::i32,     3 }, // TEST+BSF+CMOV/BRANCH
2715     { ISD::CTTZ,       MVT::i16,     3 }, // TEST+BSF+CMOV/BRANCH
2716     { ISD::CTTZ,       MVT::i8,      3 }, // TEST+BSF+CMOV/BRANCH
2717     { ISD::CTPOP,      MVT::i32,     8 },
2718     { ISD::CTPOP,      MVT::i16,     9 },
2719     { ISD::CTPOP,      MVT::i8,      7 },
2720     { ISD::SADDO,      MVT::i32,     1 },
2721     { ISD::SADDO,      MVT::i16,     1 },
2722     { ISD::SADDO,      MVT::i8,      1 },
2723     { ISD::UADDO,      MVT::i32,     1 },
2724     { ISD::UADDO,      MVT::i16,     1 },
2725     { ISD::UADDO,      MVT::i8,      1 },
2726     { ISD::UMULO,      MVT::i32,     2 }, // mul + seto
2727     { ISD::UMULO,      MVT::i16,     2 },
2728     { ISD::UMULO,      MVT::i8,      2 },
2729   };
2730 
2731   Type *RetTy = ICA.getReturnType();
2732   Type *OpTy = RetTy;
2733   Intrinsic::ID IID = ICA.getID();
2734   unsigned ISD = ISD::DELETED_NODE;
2735   switch (IID) {
2736   default:
2737     break;
2738   case Intrinsic::abs:
2739     ISD = ISD::ABS;
2740     break;
2741   case Intrinsic::bitreverse:
2742     ISD = ISD::BITREVERSE;
2743     break;
2744   case Intrinsic::bswap:
2745     ISD = ISD::BSWAP;
2746     break;
2747   case Intrinsic::ctlz:
2748     ISD = ISD::CTLZ;
2749     break;
2750   case Intrinsic::ctpop:
2751     ISD = ISD::CTPOP;
2752     break;
2753   case Intrinsic::cttz:
2754     ISD = ISD::CTTZ;
2755     break;
2756   case Intrinsic::maxnum:
2757   case Intrinsic::minnum:
2758     // FMINNUM has same costs so don't duplicate.
2759     ISD = ISD::FMAXNUM;
2760     break;
2761   case Intrinsic::sadd_sat:
2762     ISD = ISD::SADDSAT;
2763     break;
2764   case Intrinsic::smax:
2765     ISD = ISD::SMAX;
2766     break;
2767   case Intrinsic::smin:
2768     ISD = ISD::SMIN;
2769     break;
2770   case Intrinsic::ssub_sat:
2771     ISD = ISD::SSUBSAT;
2772     break;
2773   case Intrinsic::uadd_sat:
2774     ISD = ISD::UADDSAT;
2775     break;
2776   case Intrinsic::umax:
2777     ISD = ISD::UMAX;
2778     break;
2779   case Intrinsic::umin:
2780     ISD = ISD::UMIN;
2781     break;
2782   case Intrinsic::usub_sat:
2783     ISD = ISD::USUBSAT;
2784     break;
2785   case Intrinsic::sqrt:
2786     ISD = ISD::FSQRT;
2787     break;
2788   case Intrinsic::sadd_with_overflow:
2789   case Intrinsic::ssub_with_overflow:
2790     // SSUBO has same costs so don't duplicate.
2791     ISD = ISD::SADDO;
2792     OpTy = RetTy->getContainedType(0);
2793     break;
2794   case Intrinsic::uadd_with_overflow:
2795   case Intrinsic::usub_with_overflow:
2796     // USUBO has same costs so don't duplicate.
2797     ISD = ISD::UADDO;
2798     OpTy = RetTy->getContainedType(0);
2799     break;
2800   case Intrinsic::umul_with_overflow:
2801   case Intrinsic::smul_with_overflow:
2802     // SMULO has same costs so don't duplicate.
2803     ISD = ISD::UMULO;
2804     OpTy = RetTy->getContainedType(0);
2805     break;
2806   }
2807 
2808   if (ISD != ISD::DELETED_NODE) {
2809     // Legalize the type.
2810     std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, OpTy);
2811     MVT MTy = LT.second;
2812 
2813     // Attempt to lookup cost.
2814     if (ISD == ISD::BITREVERSE && ST->hasGFNI() && ST->hasSSSE3() &&
2815         MTy.isVector()) {
2816       // With PSHUFB the code is very similar for all types. If we have integer
2817       // byte operations, we just need a GF2P8AFFINEQB for vXi8. For other types
2818       // we also need a PSHUFB.
2819       unsigned Cost = MTy.getVectorElementType() == MVT::i8 ? 1 : 2;
2820 
2821       // Without byte operations, we need twice as many GF2P8AFFINEQB and PSHUFB
2822       // instructions. We also need an extract and an insert.
2823       if (!(MTy.is128BitVector() || (ST->hasAVX2() && MTy.is256BitVector()) ||
2824             (ST->hasBWI() && MTy.is512BitVector())))
2825         Cost = Cost * 2 + 2;
2826 
2827       return LT.first * Cost;
2828     }
2829 
2830     auto adjustTableCost = [](const CostTblEntry &Entry,
2831                               InstructionCost LegalizationCost,
2832                               FastMathFlags FMF) {
2833       // If there are no NANs to deal with, then these are reduced to a
2834       // single MIN** or MAX** instruction instead of the MIN/CMP/SELECT that we
2835       // assume is used in the non-fast case.
2836       if (Entry.ISD == ISD::FMAXNUM || Entry.ISD == ISD::FMINNUM) {
2837         if (FMF.noNaNs())
2838           return LegalizationCost * 1;
2839       }
2840       return LegalizationCost * (int)Entry.Cost;
2841     };
2842 
2843     if (ST->useGLMDivSqrtCosts())
2844       if (const auto *Entry = CostTableLookup(GLMCostTbl, ISD, MTy))
2845         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2846 
2847     if (ST->isSLM())
2848       if (const auto *Entry = CostTableLookup(SLMCostTbl, ISD, MTy))
2849         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2850 
2851     if (ST->hasCDI())
2852       if (const auto *Entry = CostTableLookup(AVX512CDCostTbl, ISD, MTy))
2853         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2854 
2855     if (ST->hasBWI())
2856       if (const auto *Entry = CostTableLookup(AVX512BWCostTbl, ISD, MTy))
2857         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2858 
2859     if (ST->hasAVX512())
2860       if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
2861         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2862 
2863     if (ST->hasXOP())
2864       if (const auto *Entry = CostTableLookup(XOPCostTbl, ISD, MTy))
2865         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2866 
2867     if (ST->hasAVX2())
2868       if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy))
2869         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2870 
2871     if (ST->hasAVX())
2872       if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy))
2873         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2874 
2875     if (ST->hasSSE42())
2876       if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy))
2877         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2878 
2879     if (ST->hasSSE41())
2880       if (const auto *Entry = CostTableLookup(SSE41CostTbl, ISD, MTy))
2881         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2882 
2883     if (ST->hasSSSE3())
2884       if (const auto *Entry = CostTableLookup(SSSE3CostTbl, ISD, MTy))
2885         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2886 
2887     if (ST->hasSSE2())
2888       if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy))
2889         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2890 
2891     if (ST->hasSSE1())
2892       if (const auto *Entry = CostTableLookup(SSE1CostTbl, ISD, MTy))
2893         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2894 
2895     if (ST->hasBMI()) {
2896       if (ST->is64Bit())
2897         if (const auto *Entry = CostTableLookup(BMI64CostTbl, ISD, MTy))
2898           return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2899 
2900       if (const auto *Entry = CostTableLookup(BMI32CostTbl, ISD, MTy))
2901         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2902     }
2903 
2904     if (ST->hasLZCNT()) {
2905       if (ST->is64Bit())
2906         if (const auto *Entry = CostTableLookup(LZCNT64CostTbl, ISD, MTy))
2907           return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2908 
2909       if (const auto *Entry = CostTableLookup(LZCNT32CostTbl, ISD, MTy))
2910         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2911     }
2912 
2913     if (ST->hasPOPCNT()) {
2914       if (ST->is64Bit())
2915         if (const auto *Entry = CostTableLookup(POPCNT64CostTbl, ISD, MTy))
2916           return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2917 
2918       if (const auto *Entry = CostTableLookup(POPCNT32CostTbl, ISD, MTy))
2919         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2920     }
2921 
2922     // TODO - add BMI (TZCNT) scalar handling
2923 
2924     if (ST->is64Bit())
2925       if (const auto *Entry = CostTableLookup(X64CostTbl, ISD, MTy))
2926         return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2927 
2928     if (const auto *Entry = CostTableLookup(X86CostTbl, ISD, MTy))
2929       return adjustTableCost(*Entry, LT.first, ICA.getFlags());
2930   }
2931 
2932   return BaseT::getIntrinsicInstrCost(ICA, CostKind);
2933 }
2934 
2935 InstructionCost
2936 X86TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
2937                                   TTI::TargetCostKind CostKind) {
2938   if (ICA.isTypeBasedOnly())
2939     return getTypeBasedIntrinsicInstrCost(ICA, CostKind);
2940 
2941   static const CostTblEntry AVX512CostTbl[] = {
2942     { ISD::ROTL,       MVT::v8i64,   1 },
2943     { ISD::ROTL,       MVT::v4i64,   1 },
2944     { ISD::ROTL,       MVT::v2i64,   1 },
2945     { ISD::ROTL,       MVT::v16i32,  1 },
2946     { ISD::ROTL,       MVT::v8i32,   1 },
2947     { ISD::ROTL,       MVT::v4i32,   1 },
2948     { ISD::ROTR,       MVT::v8i64,   1 },
2949     { ISD::ROTR,       MVT::v4i64,   1 },
2950     { ISD::ROTR,       MVT::v2i64,   1 },
2951     { ISD::ROTR,       MVT::v16i32,  1 },
2952     { ISD::ROTR,       MVT::v8i32,   1 },
2953     { ISD::ROTR,       MVT::v4i32,   1 }
2954   };
2955   // XOP: ROTL = VPROT(X,Y), ROTR = VPROT(X,SUB(0,Y))
2956   static const CostTblEntry XOPCostTbl[] = {
2957     { ISD::ROTL,       MVT::v4i64,   4 },
2958     { ISD::ROTL,       MVT::v8i32,   4 },
2959     { ISD::ROTL,       MVT::v16i16,  4 },
2960     { ISD::ROTL,       MVT::v32i8,   4 },
2961     { ISD::ROTL,       MVT::v2i64,   1 },
2962     { ISD::ROTL,       MVT::v4i32,   1 },
2963     { ISD::ROTL,       MVT::v8i16,   1 },
2964     { ISD::ROTL,       MVT::v16i8,   1 },
2965     { ISD::ROTR,       MVT::v4i64,   6 },
2966     { ISD::ROTR,       MVT::v8i32,   6 },
2967     { ISD::ROTR,       MVT::v16i16,  6 },
2968     { ISD::ROTR,       MVT::v32i8,   6 },
2969     { ISD::ROTR,       MVT::v2i64,   2 },
2970     { ISD::ROTR,       MVT::v4i32,   2 },
2971     { ISD::ROTR,       MVT::v8i16,   2 },
2972     { ISD::ROTR,       MVT::v16i8,   2 }
2973   };
2974   static const CostTblEntry X64CostTbl[] = { // 64-bit targets
2975     { ISD::ROTL,       MVT::i64,     1 },
2976     { ISD::ROTR,       MVT::i64,     1 },
2977     { ISD::FSHL,       MVT::i64,     4 }
2978   };
2979   static const CostTblEntry X86CostTbl[] = { // 32 or 64-bit targets
2980     { ISD::ROTL,       MVT::i32,     1 },
2981     { ISD::ROTL,       MVT::i16,     1 },
2982     { ISD::ROTL,       MVT::i8,      1 },
2983     { ISD::ROTR,       MVT::i32,     1 },
2984     { ISD::ROTR,       MVT::i16,     1 },
2985     { ISD::ROTR,       MVT::i8,      1 },
2986     { ISD::FSHL,       MVT::i32,     4 },
2987     { ISD::FSHL,       MVT::i16,     4 },
2988     { ISD::FSHL,       MVT::i8,      4 }
2989   };
2990 
2991   Intrinsic::ID IID = ICA.getID();
2992   Type *RetTy = ICA.getReturnType();
2993   const SmallVectorImpl<const Value *> &Args = ICA.getArgs();
2994   unsigned ISD = ISD::DELETED_NODE;
2995   switch (IID) {
2996   default:
2997     break;
2998   case Intrinsic::fshl:
2999     ISD = ISD::FSHL;
3000     if (Args[0] == Args[1])
3001       ISD = ISD::ROTL;
3002     break;
3003   case Intrinsic::fshr:
3004     // FSHR has same costs so don't duplicate.
3005     ISD = ISD::FSHL;
3006     if (Args[0] == Args[1])
3007       ISD = ISD::ROTR;
3008     break;
3009   }
3010 
3011   if (ISD != ISD::DELETED_NODE) {
3012     // Legalize the type.
3013     std::pair<InstructionCost, MVT> LT =
3014         TLI->getTypeLegalizationCost(DL, RetTy);
3015     MVT MTy = LT.second;
3016 
3017     // Attempt to lookup cost.
3018     if (ST->hasAVX512())
3019       if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
3020         return LT.first * Entry->Cost;
3021 
3022     if (ST->hasXOP())
3023       if (const auto *Entry = CostTableLookup(XOPCostTbl, ISD, MTy))
3024         return LT.first * Entry->Cost;
3025 
3026     if (ST->is64Bit())
3027       if (const auto *Entry = CostTableLookup(X64CostTbl, ISD, MTy))
3028         return LT.first * Entry->Cost;
3029 
3030     if (const auto *Entry = CostTableLookup(X86CostTbl, ISD, MTy))
3031       return LT.first * Entry->Cost;
3032   }
3033 
3034   return BaseT::getIntrinsicInstrCost(ICA, CostKind);
3035 }
3036 
3037 InstructionCost X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
3038                                                unsigned Index) {
3039   static const CostTblEntry SLMCostTbl[] = {
3040      { ISD::EXTRACT_VECTOR_ELT,       MVT::i8,      4 },
3041      { ISD::EXTRACT_VECTOR_ELT,       MVT::i16,     4 },
3042      { ISD::EXTRACT_VECTOR_ELT,       MVT::i32,     4 },
3043      { ISD::EXTRACT_VECTOR_ELT,       MVT::i64,     7 }
3044    };
3045 
3046   assert(Val->isVectorTy() && "This must be a vector type");
3047   Type *ScalarType = Val->getScalarType();
3048   int RegisterFileMoveCost = 0;
3049 
3050   if (Index != -1U && (Opcode == Instruction::ExtractElement ||
3051                        Opcode == Instruction::InsertElement)) {
3052     // Legalize the type.
3053     std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Val);
3054 
3055     // This type is legalized to a scalar type.
3056     if (!LT.second.isVector())
3057       return 0;
3058 
3059     // The type may be split. Normalize the index to the new type.
3060     unsigned NumElts = LT.second.getVectorNumElements();
3061     unsigned SubNumElts = NumElts;
3062     Index = Index % NumElts;
3063 
3064     // For >128-bit vectors, we need to extract higher 128-bit subvectors.
3065     // For inserts, we also need to insert the subvector back.
3066     if (LT.second.getSizeInBits() > 128) {
3067       assert((LT.second.getSizeInBits() % 128) == 0 && "Illegal vector");
3068       unsigned NumSubVecs = LT.second.getSizeInBits() / 128;
3069       SubNumElts = NumElts / NumSubVecs;
3070       if (SubNumElts <= Index) {
3071         RegisterFileMoveCost += (Opcode == Instruction::InsertElement ? 2 : 1);
3072         Index %= SubNumElts;
3073       }
3074     }
3075 
3076     if (Index == 0) {
3077       // Floating point scalars are already located in index #0.
3078       // Many insertions to #0 can fold away for scalar fp-ops, so let's assume
3079       // true for all.
3080       if (ScalarType->isFloatingPointTy())
3081         return RegisterFileMoveCost;
3082 
3083       // Assume movd/movq XMM -> GPR is relatively cheap on all targets.
3084       if (ScalarType->isIntegerTy() && Opcode == Instruction::ExtractElement)
3085         return 1 + RegisterFileMoveCost;
3086     }
3087 
3088     int ISD = TLI->InstructionOpcodeToISD(Opcode);
3089     assert(ISD && "Unexpected vector opcode");
3090     MVT MScalarTy = LT.second.getScalarType();
3091     if (ST->isSLM())
3092       if (auto *Entry = CostTableLookup(SLMCostTbl, ISD, MScalarTy))
3093         return Entry->Cost + RegisterFileMoveCost;
3094 
3095     // Assume pinsr/pextr XMM <-> GPR is relatively cheap on all targets.
3096     if ((MScalarTy == MVT::i16 && ST->hasSSE2()) ||
3097         (MScalarTy.isInteger() && ST->hasSSE41()))
3098       return 1 + RegisterFileMoveCost;
3099 
3100     // Assume insertps is relatively cheap on all targets.
3101     if (MScalarTy == MVT::f32 && ST->hasSSE41() &&
3102         Opcode == Instruction::InsertElement)
3103       return 1 + RegisterFileMoveCost;
3104 
3105     // For extractions we just need to shuffle the element to index 0, which
3106     // should be very cheap (assume cost = 1). For insertions we need to shuffle
3107     // the elements to its destination. In both cases we must handle the
3108     // subvector move(s).
3109     // If the vector type is already less than 128-bits then don't reduce it.
3110     // TODO: Under what circumstances should we shuffle using the full width?
3111     InstructionCost ShuffleCost = 1;
3112     if (Opcode == Instruction::InsertElement) {
3113       auto *SubTy = cast<VectorType>(Val);
3114       EVT VT = TLI->getValueType(DL, Val);
3115       if (VT.getScalarType() != MScalarTy || VT.getSizeInBits() >= 128)
3116         SubTy = FixedVectorType::get(ScalarType, SubNumElts);
3117       ShuffleCost =
3118           getShuffleCost(TTI::SK_PermuteTwoSrc, SubTy, None, 0, SubTy);
3119     }
3120     int IntOrFpCost = ScalarType->isFloatingPointTy() ? 0 : 1;
3121     return ShuffleCost + IntOrFpCost + RegisterFileMoveCost;
3122   }
3123 
3124   // Add to the base cost if we know that the extracted element of a vector is
3125   // destined to be moved to and used in the integer register file.
3126   if (Opcode == Instruction::ExtractElement && ScalarType->isPointerTy())
3127     RegisterFileMoveCost += 1;
3128 
3129   return BaseT::getVectorInstrCost(Opcode, Val, Index) + RegisterFileMoveCost;
3130 }
3131 
3132 InstructionCost X86TTIImpl::getScalarizationOverhead(VectorType *Ty,
3133                                                      const APInt &DemandedElts,
3134                                                      bool Insert,
3135                                                      bool Extract) {
3136   InstructionCost Cost = 0;
3137 
3138   // For insertions, a ISD::BUILD_VECTOR style vector initialization can be much
3139   // cheaper than an accumulation of ISD::INSERT_VECTOR_ELT.
3140   if (Insert) {
3141     std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
3142     MVT MScalarTy = LT.second.getScalarType();
3143 
3144     if ((MScalarTy == MVT::i16 && ST->hasSSE2()) ||
3145         (MScalarTy.isInteger() && ST->hasSSE41()) ||
3146         (MScalarTy == MVT::f32 && ST->hasSSE41())) {
3147       // For types we can insert directly, insertion into 128-bit sub vectors is
3148       // cheap, followed by a cheap chain of concatenations.
3149       if (LT.second.getSizeInBits() <= 128) {
3150         Cost +=
3151             BaseT::getScalarizationOverhead(Ty, DemandedElts, Insert, false);
3152       } else {
3153         // In each 128-lane, if at least one index is demanded but not all
3154         // indices are demanded and this 128-lane is not the first 128-lane of
3155         // the legalized-vector, then this 128-lane needs a extracti128; If in
3156         // each 128-lane, there is at least one demanded index, this 128-lane
3157         // needs a inserti128.
3158 
3159         // The following cases will help you build a better understanding:
3160         // Assume we insert several elements into a v8i32 vector in avx2,
3161         // Case#1: inserting into 1th index needs vpinsrd + inserti128.
3162         // Case#2: inserting into 5th index needs extracti128 + vpinsrd +
3163         // inserti128.
3164         // Case#3: inserting into 4,5,6,7 index needs 4*vpinsrd + inserti128.
3165         const int CostValue = *LT.first.getValue();
3166         assert(CostValue >= 0 && "Negative cost!");
3167         unsigned Num128Lanes = LT.second.getSizeInBits() / 128 * CostValue;
3168         unsigned NumElts = LT.second.getVectorNumElements() * CostValue;
3169         APInt WidenedDemandedElts = DemandedElts.zextOrSelf(NumElts);
3170         unsigned Scale = NumElts / Num128Lanes;
3171         // We iterate each 128-lane, and check if we need a
3172         // extracti128/inserti128 for this 128-lane.
3173         for (unsigned I = 0; I < NumElts; I += Scale) {
3174           APInt Mask = WidenedDemandedElts.getBitsSet(NumElts, I, I + Scale);
3175           APInt MaskedDE = Mask & WidenedDemandedElts;
3176           unsigned Population = MaskedDE.countPopulation();
3177           Cost += (Population > 0 && Population != Scale &&
3178                    I % LT.second.getVectorNumElements() != 0);
3179           Cost += Population > 0;
3180         }
3181         Cost += DemandedElts.countPopulation();
3182 
3183         // For vXf32 cases, insertion into the 0'th index in each v4f32
3184         // 128-bit vector is free.
3185         // NOTE: This assumes legalization widens vXf32 vectors.
3186         if (MScalarTy == MVT::f32)
3187           for (unsigned i = 0, e = cast<FixedVectorType>(Ty)->getNumElements();
3188                i < e; i += 4)
3189             if (DemandedElts[i])
3190               Cost--;
3191       }
3192     } else if (LT.second.isVector()) {
3193       // Without fast insertion, we need to use MOVD/MOVQ to pass each demanded
3194       // integer element as a SCALAR_TO_VECTOR, then we build the vector as a
3195       // series of UNPCK followed by CONCAT_VECTORS - all of these can be
3196       // considered cheap.
3197       if (Ty->isIntOrIntVectorTy())
3198         Cost += DemandedElts.countPopulation();
3199 
3200       // Get the smaller of the legalized or original pow2-extended number of
3201       // vector elements, which represents the number of unpacks we'll end up
3202       // performing.
3203       unsigned NumElts = LT.second.getVectorNumElements();
3204       unsigned Pow2Elts =
3205           PowerOf2Ceil(cast<FixedVectorType>(Ty)->getNumElements());
3206       Cost += (std::min<unsigned>(NumElts, Pow2Elts) - 1) * LT.first;
3207     }
3208   }
3209 
3210   // TODO: Use default extraction for now, but we should investigate extending this
3211   // to handle repeated subvector extraction.
3212   if (Extract)
3213     Cost += BaseT::getScalarizationOverhead(Ty, DemandedElts, false, Extract);
3214 
3215   return Cost;
3216 }
3217 
3218 InstructionCost X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
3219                                             MaybeAlign Alignment,
3220                                             unsigned AddressSpace,
3221                                             TTI::TargetCostKind CostKind,
3222                                             const Instruction *I) {
3223   // TODO: Handle other cost kinds.
3224   if (CostKind != TTI::TCK_RecipThroughput) {
3225     if (auto *SI = dyn_cast_or_null<StoreInst>(I)) {
3226       // Store instruction with index and scale costs 2 Uops.
3227       // Check the preceding GEP to identify non-const indices.
3228       if (auto *GEP = dyn_cast<GetElementPtrInst>(SI->getPointerOperand())) {
3229         if (!all_of(GEP->indices(), [](Value *V) { return isa<Constant>(V); }))
3230           return TTI::TCC_Basic * 2;
3231       }
3232     }
3233     return TTI::TCC_Basic;
3234   }
3235 
3236   assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
3237          "Invalid Opcode");
3238   // Type legalization can't handle structs
3239   if (TLI->getValueType(DL, Src, true) == MVT::Other)
3240     return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace,
3241                                   CostKind);
3242 
3243   // Handle non-power-of-two vectors such as <3 x float> and <48 x i16>
3244   if (auto *VTy = dyn_cast<FixedVectorType>(Src)) {
3245     const unsigned NumElem = VTy->getNumElements();
3246     if (!isPowerOf2_32(NumElem)) {
3247       // Factorize NumElem into sum of power-of-two.
3248       InstructionCost Cost = 0;
3249       unsigned NumElemDone = 0;
3250       for (unsigned NumElemLeft = NumElem, Factor;
3251            Factor = PowerOf2Floor(NumElemLeft), NumElemLeft > 0;
3252            NumElemLeft -= Factor) {
3253         Type *SubTy = FixedVectorType::get(VTy->getScalarType(), Factor);
3254         unsigned SubTyBytes = SubTy->getPrimitiveSizeInBits() / 8;
3255 
3256         Cost +=
3257             getMemoryOpCost(Opcode, SubTy, Alignment, AddressSpace, CostKind);
3258 
3259         std::pair<InstructionCost, MVT> LST =
3260             TLI->getTypeLegalizationCost(DL, SubTy);
3261         if (!LST.second.isVector()) {
3262           APInt DemandedElts =
3263               APInt::getBitsSet(NumElem, NumElemDone, NumElemDone + Factor);
3264           Cost += getScalarizationOverhead(VTy, DemandedElts,
3265                                            Opcode == Instruction::Load,
3266                                            Opcode == Instruction::Store);
3267         }
3268 
3269         NumElemDone += Factor;
3270         Alignment = commonAlignment(Alignment.valueOrOne(), SubTyBytes);
3271       }
3272       assert(NumElemDone == NumElem && "Processed wrong element count?");
3273       return Cost;
3274     }
3275   }
3276 
3277   // Legalize the type.
3278   std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
3279 
3280   // Each load/store unit costs 1.
3281   InstructionCost Cost = LT.first * 1;
3282 
3283   // This isn't exactly right. We're using slow unaligned 32-byte accesses as a
3284   // proxy for a double-pumped AVX memory interface such as on Sandybridge.
3285   if (LT.second.getStoreSize() == 32 && ST->isUnalignedMem32Slow())
3286     Cost *= 2;
3287 
3288   return Cost;
3289 }
3290 
3291 InstructionCost
3292 X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy, Align Alignment,
3293                                   unsigned AddressSpace,
3294                                   TTI::TargetCostKind CostKind) {
3295   bool IsLoad = (Instruction::Load == Opcode);
3296   bool IsStore = (Instruction::Store == Opcode);
3297 
3298   auto *SrcVTy = dyn_cast<FixedVectorType>(SrcTy);
3299   if (!SrcVTy)
3300     // To calculate scalar take the regular cost, without mask
3301     return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace, CostKind);
3302 
3303   unsigned NumElem = SrcVTy->getNumElements();
3304   auto *MaskTy =
3305       FixedVectorType::get(Type::getInt8Ty(SrcVTy->getContext()), NumElem);
3306   if ((IsLoad && !isLegalMaskedLoad(SrcVTy, Alignment)) ||
3307       (IsStore && !isLegalMaskedStore(SrcVTy, Alignment)) ||
3308       !isPowerOf2_32(NumElem)) {
3309     // Scalarization
3310     APInt DemandedElts = APInt::getAllOnesValue(NumElem);
3311     InstructionCost MaskSplitCost =
3312         getScalarizationOverhead(MaskTy, DemandedElts, false, true);
3313     InstructionCost ScalarCompareCost = getCmpSelInstrCost(
3314         Instruction::ICmp, Type::getInt8Ty(SrcVTy->getContext()), nullptr,
3315         CmpInst::BAD_ICMP_PREDICATE, CostKind);
3316     InstructionCost BranchCost = getCFInstrCost(Instruction::Br, CostKind);
3317     InstructionCost MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost);
3318     InstructionCost ValueSplitCost =
3319         getScalarizationOverhead(SrcVTy, DemandedElts, IsLoad, IsStore);
3320     InstructionCost MemopCost =
3321         NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
3322                                          Alignment, AddressSpace, CostKind);
3323     return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost;
3324   }
3325 
3326   // Legalize the type.
3327   std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy);
3328   auto VT = TLI->getValueType(DL, SrcVTy);
3329   InstructionCost Cost = 0;
3330   if (VT.isSimple() && LT.second != VT.getSimpleVT() &&
3331       LT.second.getVectorNumElements() == NumElem)
3332     // Promotion requires expand/truncate for data and a shuffle for mask.
3333     Cost += getShuffleCost(TTI::SK_PermuteTwoSrc, SrcVTy, None, 0, nullptr) +
3334             getShuffleCost(TTI::SK_PermuteTwoSrc, MaskTy, None, 0, nullptr);
3335 
3336   else if (LT.second.getVectorNumElements() > NumElem) {
3337     auto *NewMaskTy = FixedVectorType::get(MaskTy->getElementType(),
3338                                            LT.second.getVectorNumElements());
3339     // Expanding requires fill mask with zeroes
3340     Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, None, 0, MaskTy);
3341   }
3342 
3343   // Pre-AVX512 - each maskmov load costs 2 + store costs ~8.
3344   if (!ST->hasAVX512())
3345     return Cost + LT.first * (IsLoad ? 2 : 8);
3346 
3347   // AVX-512 masked load/store is cheapper
3348   return Cost + LT.first;
3349 }
3350 
3351 InstructionCost X86TTIImpl::getAddressComputationCost(Type *Ty,
3352                                                       ScalarEvolution *SE,
3353                                                       const SCEV *Ptr) {
3354   // Address computations in vectorized code with non-consecutive addresses will
3355   // likely result in more instructions compared to scalar code where the
3356   // computation can more often be merged into the index mode. The resulting
3357   // extra micro-ops can significantly decrease throughput.
3358   const unsigned NumVectorInstToHideOverhead = 10;
3359 
3360   // Cost modeling of Strided Access Computation is hidden by the indexing
3361   // modes of X86 regardless of the stride value. We dont believe that there
3362   // is a difference between constant strided access in gerenal and constant
3363   // strided value which is less than or equal to 64.
3364   // Even in the case of (loop invariant) stride whose value is not known at
3365   // compile time, the address computation will not incur more than one extra
3366   // ADD instruction.
3367   if (Ty->isVectorTy() && SE) {
3368     if (!BaseT::isStridedAccess(Ptr))
3369       return NumVectorInstToHideOverhead;
3370     if (!BaseT::getConstantStrideStep(SE, Ptr))
3371       return 1;
3372   }
3373 
3374   return BaseT::getAddressComputationCost(Ty, SE, Ptr);
3375 }
3376 
3377 InstructionCost
3378 X86TTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *ValTy,
3379                                        bool IsPairwise,
3380                                        TTI::TargetCostKind CostKind) {
3381   // Just use the default implementation for pair reductions.
3382   if (IsPairwise)
3383     return BaseT::getArithmeticReductionCost(Opcode, ValTy, IsPairwise, CostKind);
3384 
3385   // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
3386   // and make it as the cost.
3387 
3388   static const CostTblEntry SLMCostTblNoPairWise[] = {
3389     { ISD::FADD,  MVT::v2f64,   3 },
3390     { ISD::ADD,   MVT::v2i64,   5 },
3391   };
3392 
3393   static const CostTblEntry SSE2CostTblNoPairWise[] = {
3394     { ISD::FADD,  MVT::v2f64,   2 },
3395     { ISD::FADD,  MVT::v2f32,   2 },
3396     { ISD::FADD,  MVT::v4f32,   4 },
3397     { ISD::ADD,   MVT::v2i64,   2 },      // The data reported by the IACA tool is "1.6".
3398     { ISD::ADD,   MVT::v2i32,   2 }, // FIXME: chosen to be less than v4i32
3399     { ISD::ADD,   MVT::v4i32,   3 },      // The data reported by the IACA tool is "3.3".
3400     { ISD::ADD,   MVT::v2i16,   2 },      // The data reported by the IACA tool is "4.3".
3401     { ISD::ADD,   MVT::v4i16,   3 },      // The data reported by the IACA tool is "4.3".
3402     { ISD::ADD,   MVT::v8i16,   4 },      // The data reported by the IACA tool is "4.3".
3403     { ISD::ADD,   MVT::v2i8,    2 },
3404     { ISD::ADD,   MVT::v4i8,    2 },
3405     { ISD::ADD,   MVT::v8i8,    2 },
3406     { ISD::ADD,   MVT::v16i8,   3 },
3407   };
3408 
3409   static const CostTblEntry AVX1CostTblNoPairWise[] = {
3410     { ISD::FADD,  MVT::v4f64,   3 },
3411     { ISD::FADD,  MVT::v4f32,   3 },
3412     { ISD::FADD,  MVT::v8f32,   4 },
3413     { ISD::ADD,   MVT::v2i64,   1 },      // The data reported by the IACA tool is "1.5".
3414     { ISD::ADD,   MVT::v4i64,   3 },
3415     { ISD::ADD,   MVT::v8i32,   5 },
3416     { ISD::ADD,   MVT::v16i16,  5 },
3417     { ISD::ADD,   MVT::v32i8,   4 },
3418   };
3419 
3420   int ISD = TLI->InstructionOpcodeToISD(Opcode);
3421   assert(ISD && "Invalid opcode");
3422 
3423   // Before legalizing the type, give a chance to look up illegal narrow types
3424   // in the table.
3425   // FIXME: Is there a better way to do this?
3426   EVT VT = TLI->getValueType(DL, ValTy);
3427   if (VT.isSimple()) {
3428     MVT MTy = VT.getSimpleVT();
3429     if (ST->isSLM())
3430       if (const auto *Entry = CostTableLookup(SLMCostTblNoPairWise, ISD, MTy))
3431         return Entry->Cost;
3432 
3433     if (ST->hasAVX())
3434       if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy))
3435         return Entry->Cost;
3436 
3437     if (ST->hasSSE2())
3438       if (const auto *Entry = CostTableLookup(SSE2CostTblNoPairWise, ISD, MTy))
3439         return Entry->Cost;
3440   }
3441 
3442   std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
3443 
3444   MVT MTy = LT.second;
3445 
3446   auto *ValVTy = cast<FixedVectorType>(ValTy);
3447 
3448   // Special case: vXi8 mul reductions are performed as vXi16.
3449   if (ISD == ISD::MUL && MTy.getScalarType() == MVT::i8) {
3450     auto *WideSclTy = IntegerType::get(ValVTy->getContext(), 16);
3451     auto *WideVecTy = FixedVectorType::get(WideSclTy, ValVTy->getNumElements());
3452     return getCastInstrCost(Instruction::ZExt, WideVecTy, ValTy,
3453                             TargetTransformInfo::CastContextHint::None,
3454                             CostKind) +
3455            getArithmeticReductionCost(Opcode, WideVecTy, IsPairwise, CostKind);
3456   }
3457 
3458   InstructionCost ArithmeticCost = 0;
3459   if (LT.first != 1 && MTy.isVector() &&
3460       MTy.getVectorNumElements() < ValVTy->getNumElements()) {
3461     // Type needs to be split. We need LT.first - 1 arithmetic ops.
3462     auto *SingleOpTy = FixedVectorType::get(ValVTy->getElementType(),
3463                                             MTy.getVectorNumElements());
3464     ArithmeticCost = getArithmeticInstrCost(Opcode, SingleOpTy, CostKind);
3465     ArithmeticCost *= LT.first - 1;
3466   }
3467 
3468   if (ST->isSLM())
3469     if (const auto *Entry = CostTableLookup(SLMCostTblNoPairWise, ISD, MTy))
3470       return ArithmeticCost + Entry->Cost;
3471 
3472   if (ST->hasAVX())
3473     if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy))
3474       return ArithmeticCost + Entry->Cost;
3475 
3476   if (ST->hasSSE2())
3477     if (const auto *Entry = CostTableLookup(SSE2CostTblNoPairWise, ISD, MTy))
3478       return ArithmeticCost + Entry->Cost;
3479 
3480   // FIXME: These assume a naive kshift+binop lowering, which is probably
3481   // conservative in most cases.
3482   static const CostTblEntry AVX512BoolReduction[] = {
3483     { ISD::AND,  MVT::v2i1,   3 },
3484     { ISD::AND,  MVT::v4i1,   5 },
3485     { ISD::AND,  MVT::v8i1,   7 },
3486     { ISD::AND,  MVT::v16i1,  9 },
3487     { ISD::AND,  MVT::v32i1, 11 },
3488     { ISD::AND,  MVT::v64i1, 13 },
3489     { ISD::OR,   MVT::v2i1,   3 },
3490     { ISD::OR,   MVT::v4i1,   5 },
3491     { ISD::OR,   MVT::v8i1,   7 },
3492     { ISD::OR,   MVT::v16i1,  9 },
3493     { ISD::OR,   MVT::v32i1, 11 },
3494     { ISD::OR,   MVT::v64i1, 13 },
3495   };
3496 
3497   static const CostTblEntry AVX2BoolReduction[] = {
3498     { ISD::AND,  MVT::v16i16,  2 }, // vpmovmskb + cmp
3499     { ISD::AND,  MVT::v32i8,   2 }, // vpmovmskb + cmp
3500     { ISD::OR,   MVT::v16i16,  2 }, // vpmovmskb + cmp
3501     { ISD::OR,   MVT::v32i8,   2 }, // vpmovmskb + cmp
3502   };
3503 
3504   static const CostTblEntry AVX1BoolReduction[] = {
3505     { ISD::AND,  MVT::v4i64,   2 }, // vmovmskpd + cmp
3506     { ISD::AND,  MVT::v8i32,   2 }, // vmovmskps + cmp
3507     { ISD::AND,  MVT::v16i16,  4 }, // vextractf128 + vpand + vpmovmskb + cmp
3508     { ISD::AND,  MVT::v32i8,   4 }, // vextractf128 + vpand + vpmovmskb + cmp
3509     { ISD::OR,   MVT::v4i64,   2 }, // vmovmskpd + cmp
3510     { ISD::OR,   MVT::v8i32,   2 }, // vmovmskps + cmp
3511     { ISD::OR,   MVT::v16i16,  4 }, // vextractf128 + vpor + vpmovmskb + cmp
3512     { ISD::OR,   MVT::v32i8,   4 }, // vextractf128 + vpor + vpmovmskb + cmp
3513   };
3514 
3515   static const CostTblEntry SSE2BoolReduction[] = {
3516     { ISD::AND,  MVT::v2i64,   2 }, // movmskpd + cmp
3517     { ISD::AND,  MVT::v4i32,   2 }, // movmskps + cmp
3518     { ISD::AND,  MVT::v8i16,   2 }, // pmovmskb + cmp
3519     { ISD::AND,  MVT::v16i8,   2 }, // pmovmskb + cmp
3520     { ISD::OR,   MVT::v2i64,   2 }, // movmskpd + cmp
3521     { ISD::OR,   MVT::v4i32,   2 }, // movmskps + cmp
3522     { ISD::OR,   MVT::v8i16,   2 }, // pmovmskb + cmp
3523     { ISD::OR,   MVT::v16i8,   2 }, // pmovmskb + cmp
3524   };
3525 
3526   // Handle bool allof/anyof patterns.
3527   if (ValVTy->getElementType()->isIntegerTy(1)) {
3528     InstructionCost ArithmeticCost = 0;
3529     if (LT.first != 1 && MTy.isVector() &&
3530         MTy.getVectorNumElements() < ValVTy->getNumElements()) {
3531       // Type needs to be split. We need LT.first - 1 arithmetic ops.
3532       auto *SingleOpTy = FixedVectorType::get(ValVTy->getElementType(),
3533                                               MTy.getVectorNumElements());
3534       ArithmeticCost = getArithmeticInstrCost(Opcode, SingleOpTy, CostKind);
3535       ArithmeticCost *= LT.first - 1;
3536     }
3537 
3538     if (ST->hasAVX512())
3539       if (const auto *Entry = CostTableLookup(AVX512BoolReduction, ISD, MTy))
3540         return ArithmeticCost + Entry->Cost;
3541     if (ST->hasAVX2())
3542       if (const auto *Entry = CostTableLookup(AVX2BoolReduction, ISD, MTy))
3543         return ArithmeticCost + Entry->Cost;
3544     if (ST->hasAVX())
3545       if (const auto *Entry = CostTableLookup(AVX1BoolReduction, ISD, MTy))
3546         return ArithmeticCost + Entry->Cost;
3547     if (ST->hasSSE2())
3548       if (const auto *Entry = CostTableLookup(SSE2BoolReduction, ISD, MTy))
3549         return ArithmeticCost + Entry->Cost;
3550 
3551     return BaseT::getArithmeticReductionCost(Opcode, ValVTy, IsPairwise,
3552                                              CostKind);
3553   }
3554 
3555   unsigned NumVecElts = ValVTy->getNumElements();
3556   unsigned ScalarSize = ValVTy->getScalarSizeInBits();
3557 
3558   // Special case power of 2 reductions where the scalar type isn't changed
3559   // by type legalization.
3560   if (!isPowerOf2_32(NumVecElts) || ScalarSize != MTy.getScalarSizeInBits())
3561     return BaseT::getArithmeticReductionCost(Opcode, ValVTy, IsPairwise,
3562                                              CostKind);
3563 
3564   InstructionCost ReductionCost = 0;
3565 
3566   auto *Ty = ValVTy;
3567   if (LT.first != 1 && MTy.isVector() &&
3568       MTy.getVectorNumElements() < ValVTy->getNumElements()) {
3569     // Type needs to be split. We need LT.first - 1 arithmetic ops.
3570     Ty = FixedVectorType::get(ValVTy->getElementType(),
3571                               MTy.getVectorNumElements());
3572     ReductionCost = getArithmeticInstrCost(Opcode, Ty, CostKind);
3573     ReductionCost *= LT.first - 1;
3574     NumVecElts = MTy.getVectorNumElements();
3575   }
3576 
3577   // Now handle reduction with the legal type, taking into account size changes
3578   // at each level.
3579   while (NumVecElts > 1) {
3580     // Determine the size of the remaining vector we need to reduce.
3581     unsigned Size = NumVecElts * ScalarSize;
3582     NumVecElts /= 2;
3583     // If we're reducing from 256/512 bits, use an extract_subvector.
3584     if (Size > 128) {
3585       auto *SubTy = FixedVectorType::get(ValVTy->getElementType(), NumVecElts);
3586       ReductionCost +=
3587           getShuffleCost(TTI::SK_ExtractSubvector, Ty, None, NumVecElts, SubTy);
3588       Ty = SubTy;
3589     } else if (Size == 128) {
3590       // Reducing from 128 bits is a permute of v2f64/v2i64.
3591       FixedVectorType *ShufTy;
3592       if (ValVTy->isFloatingPointTy())
3593         ShufTy =
3594             FixedVectorType::get(Type::getDoubleTy(ValVTy->getContext()), 2);
3595       else
3596         ShufTy =
3597             FixedVectorType::get(Type::getInt64Ty(ValVTy->getContext()), 2);
3598       ReductionCost +=
3599           getShuffleCost(TTI::SK_PermuteSingleSrc, ShufTy, None, 0, nullptr);
3600     } else if (Size == 64) {
3601       // Reducing from 64 bits is a shuffle of v4f32/v4i32.
3602       FixedVectorType *ShufTy;
3603       if (ValVTy->isFloatingPointTy())
3604         ShufTy =
3605             FixedVectorType::get(Type::getFloatTy(ValVTy->getContext()), 4);
3606       else
3607         ShufTy =
3608             FixedVectorType::get(Type::getInt32Ty(ValVTy->getContext()), 4);
3609       ReductionCost +=
3610           getShuffleCost(TTI::SK_PermuteSingleSrc, ShufTy, None, 0, nullptr);
3611     } else {
3612       // Reducing from smaller size is a shift by immediate.
3613       auto *ShiftTy = FixedVectorType::get(
3614           Type::getIntNTy(ValVTy->getContext(), Size), 128 / Size);
3615       ReductionCost += getArithmeticInstrCost(
3616           Instruction::LShr, ShiftTy, CostKind,
3617           TargetTransformInfo::OK_AnyValue,
3618           TargetTransformInfo::OK_UniformConstantValue,
3619           TargetTransformInfo::OP_None, TargetTransformInfo::OP_None);
3620     }
3621 
3622     // Add the arithmetic op for this level.
3623     ReductionCost += getArithmeticInstrCost(Opcode, Ty, CostKind);
3624   }
3625 
3626   // Add the final extract element to the cost.
3627   return ReductionCost + getVectorInstrCost(Instruction::ExtractElement, Ty, 0);
3628 }
3629 
3630 InstructionCost X86TTIImpl::getMinMaxCost(Type *Ty, Type *CondTy,
3631                                           bool IsUnsigned) {
3632   std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
3633 
3634   MVT MTy = LT.second;
3635 
3636   int ISD;
3637   if (Ty->isIntOrIntVectorTy()) {
3638     ISD = IsUnsigned ? ISD::UMIN : ISD::SMIN;
3639   } else {
3640     assert(Ty->isFPOrFPVectorTy() &&
3641            "Expected float point or integer vector type.");
3642     ISD = ISD::FMINNUM;
3643   }
3644 
3645   static const CostTblEntry SSE1CostTbl[] = {
3646     {ISD::FMINNUM, MVT::v4f32, 1},
3647   };
3648 
3649   static const CostTblEntry SSE2CostTbl[] = {
3650     {ISD::FMINNUM, MVT::v2f64, 1},
3651     {ISD::SMIN,    MVT::v8i16, 1},
3652     {ISD::UMIN,    MVT::v16i8, 1},
3653   };
3654 
3655   static const CostTblEntry SSE41CostTbl[] = {
3656     {ISD::SMIN,    MVT::v4i32, 1},
3657     {ISD::UMIN,    MVT::v4i32, 1},
3658     {ISD::UMIN,    MVT::v8i16, 1},
3659     {ISD::SMIN,    MVT::v16i8, 1},
3660   };
3661 
3662   static const CostTblEntry SSE42CostTbl[] = {
3663     {ISD::UMIN,    MVT::v2i64, 3}, // xor+pcmpgtq+blendvpd
3664   };
3665 
3666   static const CostTblEntry AVX1CostTbl[] = {
3667     {ISD::FMINNUM, MVT::v8f32,  1},
3668     {ISD::FMINNUM, MVT::v4f64,  1},
3669     {ISD::SMIN,    MVT::v8i32,  3},
3670     {ISD::UMIN,    MVT::v8i32,  3},
3671     {ISD::SMIN,    MVT::v16i16, 3},
3672     {ISD::UMIN,    MVT::v16i16, 3},
3673     {ISD::SMIN,    MVT::v32i8,  3},
3674     {ISD::UMIN,    MVT::v32i8,  3},
3675   };
3676 
3677   static const CostTblEntry AVX2CostTbl[] = {
3678     {ISD::SMIN,    MVT::v8i32,  1},
3679     {ISD::UMIN,    MVT::v8i32,  1},
3680     {ISD::SMIN,    MVT::v16i16, 1},
3681     {ISD::UMIN,    MVT::v16i16, 1},
3682     {ISD::SMIN,    MVT::v32i8,  1},
3683     {ISD::UMIN,    MVT::v32i8,  1},
3684   };
3685 
3686   static const CostTblEntry AVX512CostTbl[] = {
3687     {ISD::FMINNUM, MVT::v16f32, 1},
3688     {ISD::FMINNUM, MVT::v8f64,  1},
3689     {ISD::SMIN,    MVT::v2i64,  1},
3690     {ISD::UMIN,    MVT::v2i64,  1},
3691     {ISD::SMIN,    MVT::v4i64,  1},
3692     {ISD::UMIN,    MVT::v4i64,  1},
3693     {ISD::SMIN,    MVT::v8i64,  1},
3694     {ISD::UMIN,    MVT::v8i64,  1},
3695     {ISD::SMIN,    MVT::v16i32, 1},
3696     {ISD::UMIN,    MVT::v16i32, 1},
3697   };
3698 
3699   static const CostTblEntry AVX512BWCostTbl[] = {
3700     {ISD::SMIN,    MVT::v32i16, 1},
3701     {ISD::UMIN,    MVT::v32i16, 1},
3702     {ISD::SMIN,    MVT::v64i8,  1},
3703     {ISD::UMIN,    MVT::v64i8,  1},
3704   };
3705 
3706   // If we have a native MIN/MAX instruction for this type, use it.
3707   if (ST->hasBWI())
3708     if (const auto *Entry = CostTableLookup(AVX512BWCostTbl, ISD, MTy))
3709       return LT.first * Entry->Cost;
3710 
3711   if (ST->hasAVX512())
3712     if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
3713       return LT.first * Entry->Cost;
3714 
3715   if (ST->hasAVX2())
3716     if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy))
3717       return LT.first * Entry->Cost;
3718 
3719   if (ST->hasAVX())
3720     if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy))
3721       return LT.first * Entry->Cost;
3722 
3723   if (ST->hasSSE42())
3724     if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy))
3725       return LT.first * Entry->Cost;
3726 
3727   if (ST->hasSSE41())
3728     if (const auto *Entry = CostTableLookup(SSE41CostTbl, ISD, MTy))
3729       return LT.first * Entry->Cost;
3730 
3731   if (ST->hasSSE2())
3732     if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy))
3733       return LT.first * Entry->Cost;
3734 
3735   if (ST->hasSSE1())
3736     if (const auto *Entry = CostTableLookup(SSE1CostTbl, ISD, MTy))
3737       return LT.first * Entry->Cost;
3738 
3739   unsigned CmpOpcode;
3740   if (Ty->isFPOrFPVectorTy()) {
3741     CmpOpcode = Instruction::FCmp;
3742   } else {
3743     assert(Ty->isIntOrIntVectorTy() &&
3744            "expecting floating point or integer type for min/max reduction");
3745     CmpOpcode = Instruction::ICmp;
3746   }
3747 
3748   TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
3749   // Otherwise fall back to cmp+select.
3750   InstructionCost Result =
3751       getCmpSelInstrCost(CmpOpcode, Ty, CondTy, CmpInst::BAD_ICMP_PREDICATE,
3752                          CostKind) +
3753       getCmpSelInstrCost(Instruction::Select, Ty, CondTy,
3754                          CmpInst::BAD_ICMP_PREDICATE, CostKind);
3755   return Result;
3756 }
3757 
3758 InstructionCost
3759 X86TTIImpl::getMinMaxReductionCost(VectorType *ValTy, VectorType *CondTy,
3760                                    bool IsPairwise, bool IsUnsigned,
3761                                    TTI::TargetCostKind CostKind) {
3762   // Just use the default implementation for pair reductions.
3763   if (IsPairwise)
3764     return BaseT::getMinMaxReductionCost(ValTy, CondTy, IsPairwise, IsUnsigned,
3765                                          CostKind);
3766 
3767   std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
3768 
3769   MVT MTy = LT.second;
3770 
3771   int ISD;
3772   if (ValTy->isIntOrIntVectorTy()) {
3773     ISD = IsUnsigned ? ISD::UMIN : ISD::SMIN;
3774   } else {
3775     assert(ValTy->isFPOrFPVectorTy() &&
3776            "Expected float point or integer vector type.");
3777     ISD = ISD::FMINNUM;
3778   }
3779 
3780   // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
3781   // and make it as the cost.
3782 
3783   static const CostTblEntry SSE2CostTblNoPairWise[] = {
3784       {ISD::UMIN, MVT::v2i16, 5}, // need pxors to use pminsw/pmaxsw
3785       {ISD::UMIN, MVT::v4i16, 7}, // need pxors to use pminsw/pmaxsw
3786       {ISD::UMIN, MVT::v8i16, 9}, // need pxors to use pminsw/pmaxsw
3787   };
3788 
3789   static const CostTblEntry SSE41CostTblNoPairWise[] = {
3790       {ISD::SMIN, MVT::v2i16, 3}, // same as sse2
3791       {ISD::SMIN, MVT::v4i16, 5}, // same as sse2
3792       {ISD::UMIN, MVT::v2i16, 5}, // same as sse2
3793       {ISD::UMIN, MVT::v4i16, 7}, // same as sse2
3794       {ISD::SMIN, MVT::v8i16, 4}, // phminposuw+xor
3795       {ISD::UMIN, MVT::v8i16, 4}, // FIXME: umin is cheaper than umax
3796       {ISD::SMIN, MVT::v2i8,  3}, // pminsb
3797       {ISD::SMIN, MVT::v4i8,  5}, // pminsb
3798       {ISD::SMIN, MVT::v8i8,  7}, // pminsb
3799       {ISD::SMIN, MVT::v16i8, 6},
3800       {ISD::UMIN, MVT::v2i8,  3}, // same as sse2
3801       {ISD::UMIN, MVT::v4i8,  5}, // same as sse2
3802       {ISD::UMIN, MVT::v8i8,  7}, // same as sse2
3803       {ISD::UMIN, MVT::v16i8, 6}, // FIXME: umin is cheaper than umax
3804   };
3805 
3806   static const CostTblEntry AVX1CostTblNoPairWise[] = {
3807       {ISD::SMIN, MVT::v16i16, 6},
3808       {ISD::UMIN, MVT::v16i16, 6}, // FIXME: umin is cheaper than umax
3809       {ISD::SMIN, MVT::v32i8, 8},
3810       {ISD::UMIN, MVT::v32i8, 8},
3811   };
3812 
3813   static const CostTblEntry AVX512BWCostTblNoPairWise[] = {
3814       {ISD::SMIN, MVT::v32i16, 8},
3815       {ISD::UMIN, MVT::v32i16, 8}, // FIXME: umin is cheaper than umax
3816       {ISD::SMIN, MVT::v64i8, 10},
3817       {ISD::UMIN, MVT::v64i8, 10},
3818   };
3819 
3820   // Before legalizing the type, give a chance to look up illegal narrow types
3821   // in the table.
3822   // FIXME: Is there a better way to do this?
3823   EVT VT = TLI->getValueType(DL, ValTy);
3824   if (VT.isSimple()) {
3825     MVT MTy = VT.getSimpleVT();
3826     if (ST->hasBWI())
3827       if (const auto *Entry = CostTableLookup(AVX512BWCostTblNoPairWise, ISD, MTy))
3828         return Entry->Cost;
3829 
3830     if (ST->hasAVX())
3831       if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy))
3832         return Entry->Cost;
3833 
3834     if (ST->hasSSE41())
3835       if (const auto *Entry = CostTableLookup(SSE41CostTblNoPairWise, ISD, MTy))
3836         return Entry->Cost;
3837 
3838     if (ST->hasSSE2())
3839       if (const auto *Entry = CostTableLookup(SSE2CostTblNoPairWise, ISD, MTy))
3840         return Entry->Cost;
3841   }
3842 
3843   auto *ValVTy = cast<FixedVectorType>(ValTy);
3844   unsigned NumVecElts = ValVTy->getNumElements();
3845 
3846   auto *Ty = ValVTy;
3847   InstructionCost MinMaxCost = 0;
3848   if (LT.first != 1 && MTy.isVector() &&
3849       MTy.getVectorNumElements() < ValVTy->getNumElements()) {
3850     // Type needs to be split. We need LT.first - 1 operations ops.
3851     Ty = FixedVectorType::get(ValVTy->getElementType(),
3852                               MTy.getVectorNumElements());
3853     auto *SubCondTy = FixedVectorType::get(CondTy->getElementType(),
3854                                            MTy.getVectorNumElements());
3855     MinMaxCost = getMinMaxCost(Ty, SubCondTy, IsUnsigned);
3856     MinMaxCost *= LT.first - 1;
3857     NumVecElts = MTy.getVectorNumElements();
3858   }
3859 
3860   if (ST->hasBWI())
3861     if (const auto *Entry = CostTableLookup(AVX512BWCostTblNoPairWise, ISD, MTy))
3862       return MinMaxCost + Entry->Cost;
3863 
3864   if (ST->hasAVX())
3865     if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy))
3866       return MinMaxCost + Entry->Cost;
3867 
3868   if (ST->hasSSE41())
3869     if (const auto *Entry = CostTableLookup(SSE41CostTblNoPairWise, ISD, MTy))
3870       return MinMaxCost + Entry->Cost;
3871 
3872   if (ST->hasSSE2())
3873     if (const auto *Entry = CostTableLookup(SSE2CostTblNoPairWise, ISD, MTy))
3874       return MinMaxCost + Entry->Cost;
3875 
3876   unsigned ScalarSize = ValTy->getScalarSizeInBits();
3877 
3878   // Special case power of 2 reductions where the scalar type isn't changed
3879   // by type legalization.
3880   if (!isPowerOf2_32(ValVTy->getNumElements()) ||
3881       ScalarSize != MTy.getScalarSizeInBits())
3882     return BaseT::getMinMaxReductionCost(ValTy, CondTy, IsPairwise, IsUnsigned,
3883                                          CostKind);
3884 
3885   // Now handle reduction with the legal type, taking into account size changes
3886   // at each level.
3887   while (NumVecElts > 1) {
3888     // Determine the size of the remaining vector we need to reduce.
3889     unsigned Size = NumVecElts * ScalarSize;
3890     NumVecElts /= 2;
3891     // If we're reducing from 256/512 bits, use an extract_subvector.
3892     if (Size > 128) {
3893       auto *SubTy = FixedVectorType::get(ValVTy->getElementType(), NumVecElts);
3894       MinMaxCost +=
3895           getShuffleCost(TTI::SK_ExtractSubvector, Ty, None, NumVecElts, SubTy);
3896       Ty = SubTy;
3897     } else if (Size == 128) {
3898       // Reducing from 128 bits is a permute of v2f64/v2i64.
3899       VectorType *ShufTy;
3900       if (ValTy->isFloatingPointTy())
3901         ShufTy =
3902             FixedVectorType::get(Type::getDoubleTy(ValTy->getContext()), 2);
3903       else
3904         ShufTy = FixedVectorType::get(Type::getInt64Ty(ValTy->getContext()), 2);
3905       MinMaxCost +=
3906           getShuffleCost(TTI::SK_PermuteSingleSrc, ShufTy, None, 0, nullptr);
3907     } else if (Size == 64) {
3908       // Reducing from 64 bits is a shuffle of v4f32/v4i32.
3909       FixedVectorType *ShufTy;
3910       if (ValTy->isFloatingPointTy())
3911         ShufTy = FixedVectorType::get(Type::getFloatTy(ValTy->getContext()), 4);
3912       else
3913         ShufTy = FixedVectorType::get(Type::getInt32Ty(ValTy->getContext()), 4);
3914       MinMaxCost +=
3915           getShuffleCost(TTI::SK_PermuteSingleSrc, ShufTy, None, 0, nullptr);
3916     } else {
3917       // Reducing from smaller size is a shift by immediate.
3918       auto *ShiftTy = FixedVectorType::get(
3919           Type::getIntNTy(ValTy->getContext(), Size), 128 / Size);
3920       MinMaxCost += getArithmeticInstrCost(
3921           Instruction::LShr, ShiftTy, TTI::TCK_RecipThroughput,
3922           TargetTransformInfo::OK_AnyValue,
3923           TargetTransformInfo::OK_UniformConstantValue,
3924           TargetTransformInfo::OP_None, TargetTransformInfo::OP_None);
3925     }
3926 
3927     // Add the arithmetic op for this level.
3928     auto *SubCondTy =
3929         FixedVectorType::get(CondTy->getElementType(), Ty->getNumElements());
3930     MinMaxCost += getMinMaxCost(Ty, SubCondTy, IsUnsigned);
3931   }
3932 
3933   // Add the final extract element to the cost.
3934   return MinMaxCost + getVectorInstrCost(Instruction::ExtractElement, Ty, 0);
3935 }
3936 
3937 /// Calculate the cost of materializing a 64-bit value. This helper
3938 /// method might only calculate a fraction of a larger immediate. Therefore it
3939 /// is valid to return a cost of ZERO.
3940 InstructionCost X86TTIImpl::getIntImmCost(int64_t Val) {
3941   if (Val == 0)
3942     return TTI::TCC_Free;
3943 
3944   if (isInt<32>(Val))
3945     return TTI::TCC_Basic;
3946 
3947   return 2 * TTI::TCC_Basic;
3948 }
3949 
3950 InstructionCost X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty,
3951                                           TTI::TargetCostKind CostKind) {
3952   assert(Ty->isIntegerTy());
3953 
3954   unsigned BitSize = Ty->getPrimitiveSizeInBits();
3955   if (BitSize == 0)
3956     return ~0U;
3957 
3958   // Never hoist constants larger than 128bit, because this might lead to
3959   // incorrect code generation or assertions in codegen.
3960   // Fixme: Create a cost model for types larger than i128 once the codegen
3961   // issues have been fixed.
3962   if (BitSize > 128)
3963     return TTI::TCC_Free;
3964 
3965   if (Imm == 0)
3966     return TTI::TCC_Free;
3967 
3968   // Sign-extend all constants to a multiple of 64-bit.
3969   APInt ImmVal = Imm;
3970   if (BitSize % 64 != 0)
3971     ImmVal = Imm.sext(alignTo(BitSize, 64));
3972 
3973   // Split the constant into 64-bit chunks and calculate the cost for each
3974   // chunk.
3975   InstructionCost Cost = 0;
3976   for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
3977     APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
3978     int64_t Val = Tmp.getSExtValue();
3979     Cost += getIntImmCost(Val);
3980   }
3981   // We need at least one instruction to materialize the constant.
3982   return std::max<InstructionCost>(1, Cost);
3983 }
3984 
3985 InstructionCost X86TTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
3986                                               const APInt &Imm, Type *Ty,
3987                                               TTI::TargetCostKind CostKind,
3988                                               Instruction *Inst) {
3989   assert(Ty->isIntegerTy());
3990 
3991   unsigned BitSize = Ty->getPrimitiveSizeInBits();
3992   // There is no cost model for constants with a bit size of 0. Return TCC_Free
3993   // here, so that constant hoisting will ignore this constant.
3994   if (BitSize == 0)
3995     return TTI::TCC_Free;
3996 
3997   unsigned ImmIdx = ~0U;
3998   switch (Opcode) {
3999   default:
4000     return TTI::TCC_Free;
4001   case Instruction::GetElementPtr:
4002     // Always hoist the base address of a GetElementPtr. This prevents the
4003     // creation of new constants for every base constant that gets constant
4004     // folded with the offset.
4005     if (Idx == 0)
4006       return 2 * TTI::TCC_Basic;
4007     return TTI::TCC_Free;
4008   case Instruction::Store:
4009     ImmIdx = 0;
4010     break;
4011   case Instruction::ICmp:
4012     // This is an imperfect hack to prevent constant hoisting of
4013     // compares that might be trying to check if a 64-bit value fits in
4014     // 32-bits. The backend can optimize these cases using a right shift by 32.
4015     // Ideally we would check the compare predicate here. There also other
4016     // similar immediates the backend can use shifts for.
4017     if (Idx == 1 && Imm.getBitWidth() == 64) {
4018       uint64_t ImmVal = Imm.getZExtValue();
4019       if (ImmVal == 0x100000000ULL || ImmVal == 0xffffffff)
4020         return TTI::TCC_Free;
4021     }
4022     ImmIdx = 1;
4023     break;
4024   case Instruction::And:
4025     // We support 64-bit ANDs with immediates with 32-bits of leading zeroes
4026     // by using a 32-bit operation with implicit zero extension. Detect such
4027     // immediates here as the normal path expects bit 31 to be sign extended.
4028     if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue()))
4029       return TTI::TCC_Free;
4030     ImmIdx = 1;
4031     break;
4032   case Instruction::Add:
4033   case Instruction::Sub:
4034     // For add/sub, we can use the opposite instruction for INT32_MIN.
4035     if (Idx == 1 && Imm.getBitWidth() == 64 && Imm.getZExtValue() == 0x80000000)
4036       return TTI::TCC_Free;
4037     ImmIdx = 1;
4038     break;
4039   case Instruction::UDiv:
4040   case Instruction::SDiv:
4041   case Instruction::URem:
4042   case Instruction::SRem:
4043     // Division by constant is typically expanded later into a different
4044     // instruction sequence. This completely changes the constants.
4045     // Report them as "free" to stop ConstantHoist from marking them as opaque.
4046     return TTI::TCC_Free;
4047   case Instruction::Mul:
4048   case Instruction::Or:
4049   case Instruction::Xor:
4050     ImmIdx = 1;
4051     break;
4052   // Always return TCC_Free for the shift value of a shift instruction.
4053   case Instruction::Shl:
4054   case Instruction::LShr:
4055   case Instruction::AShr:
4056     if (Idx == 1)
4057       return TTI::TCC_Free;
4058     break;
4059   case Instruction::Trunc:
4060   case Instruction::ZExt:
4061   case Instruction::SExt:
4062   case Instruction::IntToPtr:
4063   case Instruction::PtrToInt:
4064   case Instruction::BitCast:
4065   case Instruction::PHI:
4066   case Instruction::Call:
4067   case Instruction::Select:
4068   case Instruction::Ret:
4069   case Instruction::Load:
4070     break;
4071   }
4072 
4073   if (Idx == ImmIdx) {
4074     int NumConstants = divideCeil(BitSize, 64);
4075     InstructionCost Cost = X86TTIImpl::getIntImmCost(Imm, Ty, CostKind);
4076     return (Cost <= NumConstants * TTI::TCC_Basic)
4077                ? static_cast<int>(TTI::TCC_Free)
4078                : Cost;
4079   }
4080 
4081   return X86TTIImpl::getIntImmCost(Imm, Ty, CostKind);
4082 }
4083 
4084 InstructionCost X86TTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
4085                                                 const APInt &Imm, Type *Ty,
4086                                                 TTI::TargetCostKind CostKind) {
4087   assert(Ty->isIntegerTy());
4088 
4089   unsigned BitSize = Ty->getPrimitiveSizeInBits();
4090   // There is no cost model for constants with a bit size of 0. Return TCC_Free
4091   // here, so that constant hoisting will ignore this constant.
4092   if (BitSize == 0)
4093     return TTI::TCC_Free;
4094 
4095   switch (IID) {
4096   default:
4097     return TTI::TCC_Free;
4098   case Intrinsic::sadd_with_overflow:
4099   case Intrinsic::uadd_with_overflow:
4100   case Intrinsic::ssub_with_overflow:
4101   case Intrinsic::usub_with_overflow:
4102   case Intrinsic::smul_with_overflow:
4103   case Intrinsic::umul_with_overflow:
4104     if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
4105       return TTI::TCC_Free;
4106     break;
4107   case Intrinsic::experimental_stackmap:
4108     if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
4109       return TTI::TCC_Free;
4110     break;
4111   case Intrinsic::experimental_patchpoint_void:
4112   case Intrinsic::experimental_patchpoint_i64:
4113     if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
4114       return TTI::TCC_Free;
4115     break;
4116   }
4117   return X86TTIImpl::getIntImmCost(Imm, Ty, CostKind);
4118 }
4119 
4120 InstructionCost X86TTIImpl::getCFInstrCost(unsigned Opcode,
4121                                            TTI::TargetCostKind CostKind,
4122                                            const Instruction *I) {
4123   if (CostKind != TTI::TCK_RecipThroughput)
4124     return Opcode == Instruction::PHI ? 0 : 1;
4125   // Branches are assumed to be predicted.
4126   return 0;
4127 }
4128 
4129 int X86TTIImpl::getGatherOverhead() const {
4130   // Some CPUs have more overhead for gather. The specified overhead is relative
4131   // to the Load operation. "2" is the number provided by Intel architects. This
4132   // parameter is used for cost estimation of Gather Op and comparison with
4133   // other alternatives.
4134   // TODO: Remove the explicit hasAVX512()?, That would mean we would only
4135   // enable gather with a -march.
4136   if (ST->hasAVX512() || (ST->hasAVX2() && ST->hasFastGather()))
4137     return 2;
4138 
4139   return 1024;
4140 }
4141 
4142 int X86TTIImpl::getScatterOverhead() const {
4143   if (ST->hasAVX512())
4144     return 2;
4145 
4146   return 1024;
4147 }
4148 
4149 // Return an average cost of Gather / Scatter instruction, maybe improved later.
4150 // FIXME: Add TargetCostKind support.
4151 InstructionCost X86TTIImpl::getGSVectorCost(unsigned Opcode, Type *SrcVTy,
4152                                             const Value *Ptr, Align Alignment,
4153                                             unsigned AddressSpace) {
4154 
4155   assert(isa<VectorType>(SrcVTy) && "Unexpected type in getGSVectorCost");
4156   unsigned VF = cast<FixedVectorType>(SrcVTy)->getNumElements();
4157 
4158   // Try to reduce index size from 64 bit (default for GEP)
4159   // to 32. It is essential for VF 16. If the index can't be reduced to 32, the
4160   // operation will use 16 x 64 indices which do not fit in a zmm and needs
4161   // to split. Also check that the base pointer is the same for all lanes,
4162   // and that there's at most one variable index.
4163   auto getIndexSizeInBits = [](const Value *Ptr, const DataLayout &DL) {
4164     unsigned IndexSize = DL.getPointerSizeInBits();
4165     const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
4166     if (IndexSize < 64 || !GEP)
4167       return IndexSize;
4168 
4169     unsigned NumOfVarIndices = 0;
4170     const Value *Ptrs = GEP->getPointerOperand();
4171     if (Ptrs->getType()->isVectorTy() && !getSplatValue(Ptrs))
4172       return IndexSize;
4173     for (unsigned i = 1; i < GEP->getNumOperands(); ++i) {
4174       if (isa<Constant>(GEP->getOperand(i)))
4175         continue;
4176       Type *IndxTy = GEP->getOperand(i)->getType();
4177       if (auto *IndexVTy = dyn_cast<VectorType>(IndxTy))
4178         IndxTy = IndexVTy->getElementType();
4179       if ((IndxTy->getPrimitiveSizeInBits() == 64 &&
4180           !isa<SExtInst>(GEP->getOperand(i))) ||
4181          ++NumOfVarIndices > 1)
4182         return IndexSize; // 64
4183     }
4184     return (unsigned)32;
4185   };
4186 
4187   // Trying to reduce IndexSize to 32 bits for vector 16.
4188   // By default the IndexSize is equal to pointer size.
4189   unsigned IndexSize = (ST->hasAVX512() && VF >= 16)
4190                            ? getIndexSizeInBits(Ptr, DL)
4191                            : DL.getPointerSizeInBits();
4192 
4193   auto *IndexVTy = FixedVectorType::get(
4194       IntegerType::get(SrcVTy->getContext(), IndexSize), VF);
4195   std::pair<InstructionCost, MVT> IdxsLT =
4196       TLI->getTypeLegalizationCost(DL, IndexVTy);
4197   std::pair<InstructionCost, MVT> SrcLT =
4198       TLI->getTypeLegalizationCost(DL, SrcVTy);
4199   InstructionCost::CostType SplitFactor =
4200       *std::max(IdxsLT.first, SrcLT.first).getValue();
4201   if (SplitFactor > 1) {
4202     // Handle splitting of vector of pointers
4203     auto *SplitSrcTy =
4204         FixedVectorType::get(SrcVTy->getScalarType(), VF / SplitFactor);
4205     return SplitFactor * getGSVectorCost(Opcode, SplitSrcTy, Ptr, Alignment,
4206                                          AddressSpace);
4207   }
4208 
4209   // The gather / scatter cost is given by Intel architects. It is a rough
4210   // number since we are looking at one instruction in a time.
4211   const int GSOverhead = (Opcode == Instruction::Load)
4212                              ? getGatherOverhead()
4213                              : getScatterOverhead();
4214   return GSOverhead + VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
4215                                            MaybeAlign(Alignment), AddressSpace,
4216                                            TTI::TCK_RecipThroughput);
4217 }
4218 
4219 /// Return the cost of full scalarization of gather / scatter operation.
4220 ///
4221 /// Opcode - Load or Store instruction.
4222 /// SrcVTy - The type of the data vector that should be gathered or scattered.
4223 /// VariableMask - The mask is non-constant at compile time.
4224 /// Alignment - Alignment for one element.
4225 /// AddressSpace - pointer[s] address space.
4226 ///
4227 /// FIXME: Add TargetCostKind support.
4228 InstructionCost X86TTIImpl::getGSScalarCost(unsigned Opcode, Type *SrcVTy,
4229                                             bool VariableMask, Align Alignment,
4230                                             unsigned AddressSpace) {
4231   unsigned VF = cast<FixedVectorType>(SrcVTy)->getNumElements();
4232   APInt DemandedElts = APInt::getAllOnesValue(VF);
4233   TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
4234 
4235   InstructionCost MaskUnpackCost = 0;
4236   if (VariableMask) {
4237     auto *MaskTy =
4238         FixedVectorType::get(Type::getInt1Ty(SrcVTy->getContext()), VF);
4239     MaskUnpackCost =
4240         getScalarizationOverhead(MaskTy, DemandedElts, false, true);
4241     InstructionCost ScalarCompareCost = getCmpSelInstrCost(
4242         Instruction::ICmp, Type::getInt1Ty(SrcVTy->getContext()), nullptr,
4243         CmpInst::BAD_ICMP_PREDICATE, CostKind);
4244     InstructionCost BranchCost = getCFInstrCost(Instruction::Br, CostKind);
4245     MaskUnpackCost += VF * (BranchCost + ScalarCompareCost);
4246   }
4247 
4248   // The cost of the scalar loads/stores.
4249   InstructionCost MemoryOpCost =
4250       VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
4251                            MaybeAlign(Alignment), AddressSpace, CostKind);
4252 
4253   InstructionCost InsertExtractCost = 0;
4254   if (Opcode == Instruction::Load)
4255     for (unsigned i = 0; i < VF; ++i)
4256       // Add the cost of inserting each scalar load into the vector
4257       InsertExtractCost +=
4258         getVectorInstrCost(Instruction::InsertElement, SrcVTy, i);
4259   else
4260     for (unsigned i = 0; i < VF; ++i)
4261       // Add the cost of extracting each element out of the data vector
4262       InsertExtractCost +=
4263         getVectorInstrCost(Instruction::ExtractElement, SrcVTy, i);
4264 
4265   return MemoryOpCost + MaskUnpackCost + InsertExtractCost;
4266 }
4267 
4268 /// Calculate the cost of Gather / Scatter operation
4269 InstructionCost X86TTIImpl::getGatherScatterOpCost(
4270     unsigned Opcode, Type *SrcVTy, const Value *Ptr, bool VariableMask,
4271     Align Alignment, TTI::TargetCostKind CostKind,
4272     const Instruction *I = nullptr) {
4273   if (CostKind != TTI::TCK_RecipThroughput) {
4274     if ((Opcode == Instruction::Load &&
4275          isLegalMaskedGather(SrcVTy, Align(Alignment))) ||
4276         (Opcode == Instruction::Store &&
4277          isLegalMaskedScatter(SrcVTy, Align(Alignment))))
4278       return 1;
4279     return BaseT::getGatherScatterOpCost(Opcode, SrcVTy, Ptr, VariableMask,
4280                                          Alignment, CostKind, I);
4281   }
4282 
4283   assert(SrcVTy->isVectorTy() && "Unexpected data type for Gather/Scatter");
4284   unsigned VF = cast<FixedVectorType>(SrcVTy)->getNumElements();
4285   PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType());
4286   if (!PtrTy && Ptr->getType()->isVectorTy())
4287     PtrTy = dyn_cast<PointerType>(
4288         cast<VectorType>(Ptr->getType())->getElementType());
4289   assert(PtrTy && "Unexpected type for Ptr argument");
4290   unsigned AddressSpace = PtrTy->getAddressSpace();
4291 
4292   bool Scalarize = false;
4293   if ((Opcode == Instruction::Load &&
4294        !isLegalMaskedGather(SrcVTy, Align(Alignment))) ||
4295       (Opcode == Instruction::Store &&
4296        !isLegalMaskedScatter(SrcVTy, Align(Alignment))))
4297     Scalarize = true;
4298   // Gather / Scatter for vector 2 is not profitable on KNL / SKX
4299   // Vector-4 of gather/scatter instruction does not exist on KNL.
4300   // We can extend it to 8 elements, but zeroing upper bits of
4301   // the mask vector will add more instructions. Right now we give the scalar
4302   // cost of vector-4 for KNL. TODO: Check, maybe the gather/scatter instruction
4303   // is better in the VariableMask case.
4304   if (ST->hasAVX512() && (VF == 2 || (VF == 4 && !ST->hasVLX())))
4305     Scalarize = true;
4306 
4307   if (Scalarize)
4308     return getGSScalarCost(Opcode, SrcVTy, VariableMask, Alignment,
4309                            AddressSpace);
4310 
4311   return getGSVectorCost(Opcode, SrcVTy, Ptr, Alignment, AddressSpace);
4312 }
4313 
4314 bool X86TTIImpl::isLSRCostLess(TargetTransformInfo::LSRCost &C1,
4315                                TargetTransformInfo::LSRCost &C2) {
4316     // X86 specific here are "instruction number 1st priority".
4317     return std::tie(C1.Insns, C1.NumRegs, C1.AddRecCost,
4318                     C1.NumIVMuls, C1.NumBaseAdds,
4319                     C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
4320            std::tie(C2.Insns, C2.NumRegs, C2.AddRecCost,
4321                     C2.NumIVMuls, C2.NumBaseAdds,
4322                     C2.ScaleCost, C2.ImmCost, C2.SetupCost);
4323 }
4324 
4325 bool X86TTIImpl::canMacroFuseCmp() {
4326   return ST->hasMacroFusion() || ST->hasBranchFusion();
4327 }
4328 
4329 bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy, Align Alignment) {
4330   if (!ST->hasAVX())
4331     return false;
4332 
4333   // The backend can't handle a single element vector.
4334   if (isa<VectorType>(DataTy) &&
4335       cast<FixedVectorType>(DataTy)->getNumElements() == 1)
4336     return false;
4337   Type *ScalarTy = DataTy->getScalarType();
4338 
4339   if (ScalarTy->isPointerTy())
4340     return true;
4341 
4342   if (ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
4343     return true;
4344 
4345   if (!ScalarTy->isIntegerTy())
4346     return false;
4347 
4348   unsigned IntWidth = ScalarTy->getIntegerBitWidth();
4349   return IntWidth == 32 || IntWidth == 64 ||
4350          ((IntWidth == 8 || IntWidth == 16) && ST->hasBWI());
4351 }
4352 
4353 bool X86TTIImpl::isLegalMaskedStore(Type *DataType, Align Alignment) {
4354   return isLegalMaskedLoad(DataType, Alignment);
4355 }
4356 
4357 bool X86TTIImpl::isLegalNTLoad(Type *DataType, Align Alignment) {
4358   unsigned DataSize = DL.getTypeStoreSize(DataType);
4359   // The only supported nontemporal loads are for aligned vectors of 16 or 32
4360   // bytes.  Note that 32-byte nontemporal vector loads are supported by AVX2
4361   // (the equivalent stores only require AVX).
4362   if (Alignment >= DataSize && (DataSize == 16 || DataSize == 32))
4363     return DataSize == 16 ?  ST->hasSSE1() : ST->hasAVX2();
4364 
4365   return false;
4366 }
4367 
4368 bool X86TTIImpl::isLegalNTStore(Type *DataType, Align Alignment) {
4369   unsigned DataSize = DL.getTypeStoreSize(DataType);
4370 
4371   // SSE4A supports nontemporal stores of float and double at arbitrary
4372   // alignment.
4373   if (ST->hasSSE4A() && (DataType->isFloatTy() || DataType->isDoubleTy()))
4374     return true;
4375 
4376   // Besides the SSE4A subtarget exception above, only aligned stores are
4377   // available nontemporaly on any other subtarget.  And only stores with a size
4378   // of 4..32 bytes (powers of 2, only) are permitted.
4379   if (Alignment < DataSize || DataSize < 4 || DataSize > 32 ||
4380       !isPowerOf2_32(DataSize))
4381     return false;
4382 
4383   // 32-byte vector nontemporal stores are supported by AVX (the equivalent
4384   // loads require AVX2).
4385   if (DataSize == 32)
4386     return ST->hasAVX();
4387   else if (DataSize == 16)
4388     return ST->hasSSE1();
4389   return true;
4390 }
4391 
4392 bool X86TTIImpl::isLegalMaskedExpandLoad(Type *DataTy) {
4393   if (!isa<VectorType>(DataTy))
4394     return false;
4395 
4396   if (!ST->hasAVX512())
4397     return false;
4398 
4399   // The backend can't handle a single element vector.
4400   if (cast<FixedVectorType>(DataTy)->getNumElements() == 1)
4401     return false;
4402 
4403   Type *ScalarTy = cast<VectorType>(DataTy)->getElementType();
4404 
4405   if (ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
4406     return true;
4407 
4408   if (!ScalarTy->isIntegerTy())
4409     return false;
4410 
4411   unsigned IntWidth = ScalarTy->getIntegerBitWidth();
4412   return IntWidth == 32 || IntWidth == 64 ||
4413          ((IntWidth == 8 || IntWidth == 16) && ST->hasVBMI2());
4414 }
4415 
4416 bool X86TTIImpl::isLegalMaskedCompressStore(Type *DataTy) {
4417   return isLegalMaskedExpandLoad(DataTy);
4418 }
4419 
4420 bool X86TTIImpl::isLegalMaskedGather(Type *DataTy, Align Alignment) {
4421   // Some CPUs have better gather performance than others.
4422   // TODO: Remove the explicit ST->hasAVX512()?, That would mean we would only
4423   // enable gather with a -march.
4424   if (!(ST->hasAVX512() || (ST->hasFastGather() && ST->hasAVX2())))
4425     return false;
4426 
4427   // This function is called now in two cases: from the Loop Vectorizer
4428   // and from the Scalarizer.
4429   // When the Loop Vectorizer asks about legality of the feature,
4430   // the vectorization factor is not calculated yet. The Loop Vectorizer
4431   // sends a scalar type and the decision is based on the width of the
4432   // scalar element.
4433   // Later on, the cost model will estimate usage this intrinsic based on
4434   // the vector type.
4435   // The Scalarizer asks again about legality. It sends a vector type.
4436   // In this case we can reject non-power-of-2 vectors.
4437   // We also reject single element vectors as the type legalizer can't
4438   // scalarize it.
4439   if (auto *DataVTy = dyn_cast<FixedVectorType>(DataTy)) {
4440     unsigned NumElts = DataVTy->getNumElements();
4441     if (NumElts == 1)
4442       return false;
4443   }
4444   Type *ScalarTy = DataTy->getScalarType();
4445   if (ScalarTy->isPointerTy())
4446     return true;
4447 
4448   if (ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
4449     return true;
4450 
4451   if (!ScalarTy->isIntegerTy())
4452     return false;
4453 
4454   unsigned IntWidth = ScalarTy->getIntegerBitWidth();
4455   return IntWidth == 32 || IntWidth == 64;
4456 }
4457 
4458 bool X86TTIImpl::isLegalMaskedScatter(Type *DataType, Align Alignment) {
4459   // AVX2 doesn't support scatter
4460   if (!ST->hasAVX512())
4461     return false;
4462   return isLegalMaskedGather(DataType, Alignment);
4463 }
4464 
4465 bool X86TTIImpl::hasDivRemOp(Type *DataType, bool IsSigned) {
4466   EVT VT = TLI->getValueType(DL, DataType);
4467   return TLI->isOperationLegal(IsSigned ? ISD::SDIVREM : ISD::UDIVREM, VT);
4468 }
4469 
4470 bool X86TTIImpl::isFCmpOrdCheaperThanFCmpZero(Type *Ty) {
4471   return false;
4472 }
4473 
4474 bool X86TTIImpl::areInlineCompatible(const Function *Caller,
4475                                      const Function *Callee) const {
4476   const TargetMachine &TM = getTLI()->getTargetMachine();
4477 
4478   // Work this as a subsetting of subtarget features.
4479   const FeatureBitset &CallerBits =
4480       TM.getSubtargetImpl(*Caller)->getFeatureBits();
4481   const FeatureBitset &CalleeBits =
4482       TM.getSubtargetImpl(*Callee)->getFeatureBits();
4483 
4484   FeatureBitset RealCallerBits = CallerBits & ~InlineFeatureIgnoreList;
4485   FeatureBitset RealCalleeBits = CalleeBits & ~InlineFeatureIgnoreList;
4486   return (RealCallerBits & RealCalleeBits) == RealCalleeBits;
4487 }
4488 
4489 bool X86TTIImpl::areFunctionArgsABICompatible(
4490     const Function *Caller, const Function *Callee,
4491     SmallPtrSetImpl<Argument *> &Args) const {
4492   if (!BaseT::areFunctionArgsABICompatible(Caller, Callee, Args))
4493     return false;
4494 
4495   // If we get here, we know the target features match. If one function
4496   // considers 512-bit vectors legal and the other does not, consider them
4497   // incompatible.
4498   const TargetMachine &TM = getTLI()->getTargetMachine();
4499 
4500   if (TM.getSubtarget<X86Subtarget>(*Caller).useAVX512Regs() ==
4501       TM.getSubtarget<X86Subtarget>(*Callee).useAVX512Regs())
4502     return true;
4503 
4504   // Consider the arguments compatible if they aren't vectors or aggregates.
4505   // FIXME: Look at the size of vectors.
4506   // FIXME: Look at the element types of aggregates to see if there are vectors.
4507   // FIXME: The API of this function seems intended to allow arguments
4508   // to be removed from the set, but the caller doesn't check if the set
4509   // becomes empty so that may not work in practice.
4510   return llvm::none_of(Args, [](Argument *A) {
4511     auto *EltTy = cast<PointerType>(A->getType())->getElementType();
4512     return EltTy->isVectorTy() || EltTy->isAggregateType();
4513   });
4514 }
4515 
4516 X86TTIImpl::TTI::MemCmpExpansionOptions
4517 X86TTIImpl::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
4518   TTI::MemCmpExpansionOptions Options;
4519   Options.MaxNumLoads = TLI->getMaxExpandSizeMemcmp(OptSize);
4520   Options.NumLoadsPerBlock = 2;
4521   // All GPR and vector loads can be unaligned.
4522   Options.AllowOverlappingLoads = true;
4523   if (IsZeroCmp) {
4524     // Only enable vector loads for equality comparison. Right now the vector
4525     // version is not as fast for three way compare (see #33329).
4526     const unsigned PreferredWidth = ST->getPreferVectorWidth();
4527     if (PreferredWidth >= 512 && ST->hasAVX512()) Options.LoadSizes.push_back(64);
4528     if (PreferredWidth >= 256 && ST->hasAVX()) Options.LoadSizes.push_back(32);
4529     if (PreferredWidth >= 128 && ST->hasSSE2()) Options.LoadSizes.push_back(16);
4530   }
4531   if (ST->is64Bit()) {
4532     Options.LoadSizes.push_back(8);
4533   }
4534   Options.LoadSizes.push_back(4);
4535   Options.LoadSizes.push_back(2);
4536   Options.LoadSizes.push_back(1);
4537   return Options;
4538 }
4539 
4540 bool X86TTIImpl::enableInterleavedAccessVectorization() {
4541   // TODO: We expect this to be beneficial regardless of arch,
4542   // but there are currently some unexplained performance artifacts on Atom.
4543   // As a temporary solution, disable on Atom.
4544   return !(ST->isAtom());
4545 }
4546 
4547 // Get estimation for interleaved load/store operations for AVX2.
4548 // \p Factor is the interleaved-access factor (stride) - number of
4549 // (interleaved) elements in the group.
4550 // \p Indices contains the indices for a strided load: when the
4551 // interleaved load has gaps they indicate which elements are used.
4552 // If Indices is empty (or if the number of indices is equal to the size
4553 // of the interleaved-access as given in \p Factor) the access has no gaps.
4554 //
4555 // As opposed to AVX-512, AVX2 does not have generic shuffles that allow
4556 // computing the cost using a generic formula as a function of generic
4557 // shuffles. We therefore use a lookup table instead, filled according to
4558 // the instruction sequences that codegen currently generates.
4559 InstructionCost X86TTIImpl::getInterleavedMemoryOpCostAVX2(
4560     unsigned Opcode, FixedVectorType *VecTy, unsigned Factor,
4561     ArrayRef<unsigned> Indices, Align Alignment, unsigned AddressSpace,
4562     TTI::TargetCostKind CostKind, bool UseMaskForCond, bool UseMaskForGaps) {
4563 
4564   if (UseMaskForCond || UseMaskForGaps)
4565     return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
4566                                              Alignment, AddressSpace, CostKind,
4567                                              UseMaskForCond, UseMaskForGaps);
4568 
4569   // We currently Support only fully-interleaved groups, with no gaps.
4570   // TODO: Support also strided loads (interleaved-groups with gaps).
4571   if (Indices.size() && Indices.size() != Factor)
4572     return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
4573                                              Alignment, AddressSpace,
4574                                              CostKind);
4575 
4576   // VecTy for interleave memop is <VF*Factor x Elt>.
4577   // So, for VF=4, Interleave Factor = 3, Element type = i32 we have
4578   // VecTy = <12 x i32>.
4579   MVT LegalVT = getTLI()->getTypeLegalizationCost(DL, VecTy).second;
4580 
4581   // This function can be called with VecTy=<6xi128>, Factor=3, in which case
4582   // the VF=2, while v2i128 is an unsupported MVT vector type
4583   // (see MachineValueType.h::getVectorVT()).
4584   if (!LegalVT.isVector())
4585     return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
4586                                              Alignment, AddressSpace,
4587                                              CostKind);
4588 
4589   unsigned VF = VecTy->getNumElements() / Factor;
4590   Type *ScalarTy = VecTy->getElementType();
4591 
4592   // Calculate the number of memory operations (NumOfMemOps), required
4593   // for load/store the VecTy.
4594   unsigned VecTySize = DL.getTypeStoreSize(VecTy);
4595   unsigned LegalVTSize = LegalVT.getStoreSize();
4596   unsigned NumOfMemOps = (VecTySize + LegalVTSize - 1) / LegalVTSize;
4597 
4598   // Get the cost of one memory operation.
4599   auto *SingleMemOpTy = FixedVectorType::get(VecTy->getElementType(),
4600                                              LegalVT.getVectorNumElements());
4601   InstructionCost MemOpCost = getMemoryOpCost(
4602       Opcode, SingleMemOpTy, MaybeAlign(Alignment), AddressSpace, CostKind);
4603 
4604   auto *VT = FixedVectorType::get(ScalarTy, VF);
4605   EVT ETy = TLI->getValueType(DL, VT);
4606   if (!ETy.isSimple())
4607     return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
4608                                              Alignment, AddressSpace,
4609                                              CostKind);
4610 
4611   // TODO: Complete for other data-types and strides.
4612   // Each combination of Stride, ElementTy and VF results in a different
4613   // sequence; The cost tables are therefore accessed with:
4614   // Factor (stride) and VectorType=VFxElemType.
4615   // The Cost accounts only for the shuffle sequence;
4616   // The cost of the loads/stores is accounted for separately.
4617   //
4618   static const CostTblEntry AVX2InterleavedLoadTbl[] = {
4619     { 2, MVT::v4i64, 6 }, //(load 8i64 and) deinterleave into 2 x 4i64
4620     { 2, MVT::v4f64, 6 }, //(load 8f64 and) deinterleave into 2 x 4f64
4621 
4622     { 3, MVT::v2i8,  10 }, //(load 6i8 and)  deinterleave into 3 x 2i8
4623     { 3, MVT::v4i8,  4 },  //(load 12i8 and) deinterleave into 3 x 4i8
4624     { 3, MVT::v8i8,  9 },  //(load 24i8 and) deinterleave into 3 x 8i8
4625     { 3, MVT::v16i8, 11},  //(load 48i8 and) deinterleave into 3 x 16i8
4626     { 3, MVT::v32i8, 13},  //(load 96i8 and) deinterleave into 3 x 32i8
4627     { 3, MVT::v8f32, 17 }, //(load 24f32 and)deinterleave into 3 x 8f32
4628 
4629     { 4, MVT::v2i8,  12 }, //(load 8i8 and)   deinterleave into 4 x 2i8
4630     { 4, MVT::v4i8,  4 },  //(load 16i8 and)  deinterleave into 4 x 4i8
4631     { 4, MVT::v8i8,  20 }, //(load 32i8 and)  deinterleave into 4 x 8i8
4632     { 4, MVT::v16i8, 39 }, //(load 64i8 and)  deinterleave into 4 x 16i8
4633     { 4, MVT::v32i8, 80 }, //(load 128i8 and) deinterleave into 4 x 32i8
4634 
4635     { 8, MVT::v8f32, 40 }  //(load 64f32 and)deinterleave into 8 x 8f32
4636   };
4637 
4638   static const CostTblEntry AVX2InterleavedStoreTbl[] = {
4639     { 2, MVT::v4i64, 6 }, //interleave into 2 x 4i64 into 8i64 (and store)
4640     { 2, MVT::v4f64, 6 }, //interleave into 2 x 4f64 into 8f64 (and store)
4641 
4642     { 3, MVT::v2i8,  7 },  //interleave 3 x 2i8  into 6i8 (and store)
4643     { 3, MVT::v4i8,  8 },  //interleave 3 x 4i8  into 12i8 (and store)
4644     { 3, MVT::v8i8,  11 }, //interleave 3 x 8i8  into 24i8 (and store)
4645     { 3, MVT::v16i8, 11 }, //interleave 3 x 16i8 into 48i8 (and store)
4646     { 3, MVT::v32i8, 13 }, //interleave 3 x 32i8 into 96i8 (and store)
4647 
4648     { 4, MVT::v2i8,  12 }, //interleave 4 x 2i8  into 8i8 (and store)
4649     { 4, MVT::v4i8,  9 },  //interleave 4 x 4i8  into 16i8 (and store)
4650     { 4, MVT::v8i8,  10 }, //interleave 4 x 8i8  into 32i8 (and store)
4651     { 4, MVT::v16i8, 10 }, //interleave 4 x 16i8 into 64i8 (and store)
4652     { 4, MVT::v32i8, 12 }  //interleave 4 x 32i8 into 128i8 (and store)
4653   };
4654 
4655   if (Opcode == Instruction::Load) {
4656     if (const auto *Entry =
4657             CostTableLookup(AVX2InterleavedLoadTbl, Factor, ETy.getSimpleVT()))
4658       return NumOfMemOps * MemOpCost + Entry->Cost;
4659   } else {
4660     assert(Opcode == Instruction::Store &&
4661            "Expected Store Instruction at this  point");
4662     if (const auto *Entry =
4663             CostTableLookup(AVX2InterleavedStoreTbl, Factor, ETy.getSimpleVT()))
4664       return NumOfMemOps * MemOpCost + Entry->Cost;
4665   }
4666 
4667   return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
4668                                            Alignment, AddressSpace, CostKind);
4669 }
4670 
4671 // Get estimation for interleaved load/store operations and strided load.
4672 // \p Indices contains indices for strided load.
4673 // \p Factor - the factor of interleaving.
4674 // AVX-512 provides 3-src shuffles that significantly reduces the cost.
4675 InstructionCost X86TTIImpl::getInterleavedMemoryOpCostAVX512(
4676     unsigned Opcode, FixedVectorType *VecTy, unsigned Factor,
4677     ArrayRef<unsigned> Indices, Align Alignment, unsigned AddressSpace,
4678     TTI::TargetCostKind CostKind, bool UseMaskForCond, bool UseMaskForGaps) {
4679 
4680   if (UseMaskForCond || UseMaskForGaps)
4681     return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
4682                                              Alignment, AddressSpace, CostKind,
4683                                              UseMaskForCond, UseMaskForGaps);
4684 
4685   // VecTy for interleave memop is <VF*Factor x Elt>.
4686   // So, for VF=4, Interleave Factor = 3, Element type = i32 we have
4687   // VecTy = <12 x i32>.
4688 
4689   // Calculate the number of memory operations (NumOfMemOps), required
4690   // for load/store the VecTy.
4691   MVT LegalVT = getTLI()->getTypeLegalizationCost(DL, VecTy).second;
4692   unsigned VecTySize = DL.getTypeStoreSize(VecTy);
4693   unsigned LegalVTSize = LegalVT.getStoreSize();
4694   unsigned NumOfMemOps = (VecTySize + LegalVTSize - 1) / LegalVTSize;
4695 
4696   // Get the cost of one memory operation.
4697   auto *SingleMemOpTy = FixedVectorType::get(VecTy->getElementType(),
4698                                              LegalVT.getVectorNumElements());
4699   InstructionCost MemOpCost = getMemoryOpCost(
4700       Opcode, SingleMemOpTy, MaybeAlign(Alignment), AddressSpace, CostKind);
4701 
4702   unsigned VF = VecTy->getNumElements() / Factor;
4703   MVT VT = MVT::getVectorVT(MVT::getVT(VecTy->getScalarType()), VF);
4704 
4705   if (Opcode == Instruction::Load) {
4706     // The tables (AVX512InterleavedLoadTbl and AVX512InterleavedStoreTbl)
4707     // contain the cost of the optimized shuffle sequence that the
4708     // X86InterleavedAccess pass will generate.
4709     // The cost of loads and stores are computed separately from the table.
4710 
4711     // X86InterleavedAccess support only the following interleaved-access group.
4712     static const CostTblEntry AVX512InterleavedLoadTbl[] = {
4713         {3, MVT::v16i8, 12}, //(load 48i8 and) deinterleave into 3 x 16i8
4714         {3, MVT::v32i8, 14}, //(load 96i8 and) deinterleave into 3 x 32i8
4715         {3, MVT::v64i8, 22}, //(load 96i8 and) deinterleave into 3 x 32i8
4716     };
4717 
4718     if (const auto *Entry =
4719             CostTableLookup(AVX512InterleavedLoadTbl, Factor, VT))
4720       return NumOfMemOps * MemOpCost + Entry->Cost;
4721     //If an entry does not exist, fallback to the default implementation.
4722 
4723     // Kind of shuffle depends on number of loaded values.
4724     // If we load the entire data in one register, we can use a 1-src shuffle.
4725     // Otherwise, we'll merge 2 sources in each operation.
4726     TTI::ShuffleKind ShuffleKind =
4727         (NumOfMemOps > 1) ? TTI::SK_PermuteTwoSrc : TTI::SK_PermuteSingleSrc;
4728 
4729     InstructionCost ShuffleCost =
4730         getShuffleCost(ShuffleKind, SingleMemOpTy, None, 0, nullptr);
4731 
4732     unsigned NumOfLoadsInInterleaveGrp =
4733         Indices.size() ? Indices.size() : Factor;
4734     auto *ResultTy = FixedVectorType::get(VecTy->getElementType(),
4735                                           VecTy->getNumElements() / Factor);
4736     InstructionCost NumOfResults =
4737         getTLI()->getTypeLegalizationCost(DL, ResultTy).first *
4738         NumOfLoadsInInterleaveGrp;
4739 
4740     // About a half of the loads may be folded in shuffles when we have only
4741     // one result. If we have more than one result, we do not fold loads at all.
4742     unsigned NumOfUnfoldedLoads =
4743         NumOfResults > 1 ? NumOfMemOps : NumOfMemOps / 2;
4744 
4745     // Get a number of shuffle operations per result.
4746     unsigned NumOfShufflesPerResult =
4747         std::max((unsigned)1, (unsigned)(NumOfMemOps - 1));
4748 
4749     // The SK_MergeTwoSrc shuffle clobbers one of src operands.
4750     // When we have more than one destination, we need additional instructions
4751     // to keep sources.
4752     InstructionCost NumOfMoves = 0;
4753     if (NumOfResults > 1 && ShuffleKind == TTI::SK_PermuteTwoSrc)
4754       NumOfMoves = NumOfResults * NumOfShufflesPerResult / 2;
4755 
4756     InstructionCost Cost = NumOfResults * NumOfShufflesPerResult * ShuffleCost +
4757                            NumOfUnfoldedLoads * MemOpCost + NumOfMoves;
4758 
4759     return Cost;
4760   }
4761 
4762   // Store.
4763   assert(Opcode == Instruction::Store &&
4764          "Expected Store Instruction at this  point");
4765   // X86InterleavedAccess support only the following interleaved-access group.
4766   static const CostTblEntry AVX512InterleavedStoreTbl[] = {
4767       {3, MVT::v16i8, 12}, // interleave 3 x 16i8 into 48i8 (and store)
4768       {3, MVT::v32i8, 14}, // interleave 3 x 32i8 into 96i8 (and store)
4769       {3, MVT::v64i8, 26}, // interleave 3 x 64i8 into 96i8 (and store)
4770 
4771       {4, MVT::v8i8, 10},  // interleave 4 x 8i8  into 32i8  (and store)
4772       {4, MVT::v16i8, 11}, // interleave 4 x 16i8 into 64i8  (and store)
4773       {4, MVT::v32i8, 14}, // interleave 4 x 32i8 into 128i8 (and store)
4774       {4, MVT::v64i8, 24}  // interleave 4 x 32i8 into 256i8 (and store)
4775   };
4776 
4777   if (const auto *Entry =
4778           CostTableLookup(AVX512InterleavedStoreTbl, Factor, VT))
4779     return NumOfMemOps * MemOpCost + Entry->Cost;
4780   //If an entry does not exist, fallback to the default implementation.
4781 
4782   // There is no strided stores meanwhile. And store can't be folded in
4783   // shuffle.
4784   unsigned NumOfSources = Factor; // The number of values to be merged.
4785   InstructionCost ShuffleCost =
4786       getShuffleCost(TTI::SK_PermuteTwoSrc, SingleMemOpTy, None, 0, nullptr);
4787   unsigned NumOfShufflesPerStore = NumOfSources - 1;
4788 
4789   // The SK_MergeTwoSrc shuffle clobbers one of src operands.
4790   // We need additional instructions to keep sources.
4791   unsigned NumOfMoves = NumOfMemOps * NumOfShufflesPerStore / 2;
4792   InstructionCost Cost =
4793       NumOfMemOps * (MemOpCost + NumOfShufflesPerStore * ShuffleCost) +
4794       NumOfMoves;
4795   return Cost;
4796 }
4797 
4798 InstructionCost X86TTIImpl::getInterleavedMemoryOpCost(
4799     unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
4800     Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
4801     bool UseMaskForCond, bool UseMaskForGaps) {
4802   auto isSupportedOnAVX512 = [](Type *VecTy, bool HasBW) {
4803     Type *EltTy = cast<VectorType>(VecTy)->getElementType();
4804     if (EltTy->isFloatTy() || EltTy->isDoubleTy() || EltTy->isIntegerTy(64) ||
4805         EltTy->isIntegerTy(32) || EltTy->isPointerTy())
4806       return true;
4807     if (EltTy->isIntegerTy(16) || EltTy->isIntegerTy(8))
4808       return HasBW;
4809     return false;
4810   };
4811   if (ST->hasAVX512() && isSupportedOnAVX512(VecTy, ST->hasBWI()))
4812     return getInterleavedMemoryOpCostAVX512(
4813         Opcode, cast<FixedVectorType>(VecTy), Factor, Indices, Alignment,
4814         AddressSpace, CostKind, UseMaskForCond, UseMaskForGaps);
4815   if (ST->hasAVX2())
4816     return getInterleavedMemoryOpCostAVX2(
4817         Opcode, cast<FixedVectorType>(VecTy), Factor, Indices, Alignment,
4818         AddressSpace, CostKind, UseMaskForCond, UseMaskForGaps);
4819 
4820   return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
4821                                            Alignment, AddressSpace, CostKind,
4822                                            UseMaskForCond, UseMaskForGaps);
4823 }
4824