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