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