1 //===-- X86TargetTransformInfo.cpp - X86 specific TTI pass ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file implements a TargetTransformInfo analysis pass specific to the
11 /// X86 target machine. It uses the target's detailed information to provide
12 /// more precise answers to certain TTI queries, while letting the target
13 /// independent and default TTI implementations handle the rest.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "x86tti"
19 #include "X86TargetMachine.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Target/CostTable.h"
24 #include "llvm/Target/TargetLowering.h"
27 // Declare the pass initialization routine locally as target-specific passes
28 // don't havve a target-wide initialization entry point, and so we rely on the
29 // pass constructor initialization.
31 void initializeX86TTIPass(PassRegistry &);
36 class X86TTI final : public ImmutablePass, public TargetTransformInfo {
37 const X86Subtarget *ST;
38 const X86TargetLowering *TLI;
40 /// Estimate the overhead of scalarizing an instruction. Insert and Extract
41 /// are set if the result needs to be inserted and/or extracted from vectors.
42 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
45 X86TTI() : ImmutablePass(ID), ST(0), TLI(0) {
46 llvm_unreachable("This pass cannot be directly constructed");
49 X86TTI(const X86TargetMachine *TM)
50 : ImmutablePass(ID), ST(TM->getSubtargetImpl()),
51 TLI(TM->getTargetLowering()) {
52 initializeX86TTIPass(*PassRegistry::getPassRegistry());
55 virtual void initializePass() override {
59 virtual void finalizePass() {
63 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
64 TargetTransformInfo::getAnalysisUsage(AU);
67 /// Pass identification.
70 /// Provide necessary pointer adjustments for the two base classes.
71 virtual void *getAdjustedAnalysisPointer(const void *ID) override {
72 if (ID == &TargetTransformInfo::ID)
73 return (TargetTransformInfo*)this;
77 /// \name Scalar TTI Implementations
79 virtual PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override;
83 /// \name Vector TTI Implementations
86 virtual unsigned getNumberOfRegisters(bool Vector) const override;
87 virtual unsigned getRegisterBitWidth(bool Vector) const override;
88 virtual unsigned getMaximumUnrollFactor() const override;
89 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
91 OperandValueKind) const override;
92 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
93 int Index, Type *SubTp) const override;
94 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
95 Type *Src) const override;
96 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
97 Type *CondTy) const override;
98 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
99 unsigned Index) const override;
100 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
102 unsigned AddressSpace) const override;
105 getAddressComputationCost(Type *PtrTy, bool IsComplex) const override;
107 virtual unsigned getReductionCost(unsigned Opcode, Type *Ty,
108 bool IsPairwiseForm) const override;
110 virtual unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override;
112 virtual unsigned getIntImmCost(unsigned Opcode, const APInt &Imm,
113 Type *Ty) const override;
114 virtual unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
115 Type *Ty) const override;
120 } // end anonymous namespace
122 INITIALIZE_AG_PASS(X86TTI, TargetTransformInfo, "x86tti",
123 "X86 Target Transform Info", true, true, false)
127 llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) {
128 return new X86TTI(TM);
132 //===----------------------------------------------------------------------===//
136 //===----------------------------------------------------------------------===//
138 X86TTI::PopcntSupportKind X86TTI::getPopcntSupport(unsigned TyWidth) const {
139 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
140 // TODO: Currently the __builtin_popcount() implementation using SSE3
141 // instructions is inefficient. Once the problem is fixed, we should
142 // call ST->hasSSE3() instead of ST->hasPOPCNT().
143 return ST->hasPOPCNT() ? PSK_FastHardware : PSK_Software;
146 unsigned X86TTI::getNumberOfRegisters(bool Vector) const {
147 if (Vector && !ST->hasSSE1())
155 unsigned X86TTI::getRegisterBitWidth(bool Vector) const {
157 if (ST->hasAVX()) return 256;
158 if (ST->hasSSE1()) return 128;
168 unsigned X86TTI::getMaximumUnrollFactor() const {
172 // Sandybridge and Haswell have multiple execution ports and pipelined
180 unsigned X86TTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
181 OperandValueKind Op1Info,
182 OperandValueKind Op2Info) const {
183 // Legalize the type.
184 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
186 int ISD = TLI->InstructionOpcodeToISD(Opcode);
187 assert(ISD && "Invalid opcode");
189 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = {
190 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
191 // customize them to detect the cases where shift amount is a scalar one.
192 { ISD::SHL, MVT::v4i32, 1 },
193 { ISD::SRL, MVT::v4i32, 1 },
194 { ISD::SRA, MVT::v4i32, 1 },
195 { ISD::SHL, MVT::v8i32, 1 },
196 { ISD::SRL, MVT::v8i32, 1 },
197 { ISD::SRA, MVT::v8i32, 1 },
198 { ISD::SHL, MVT::v2i64, 1 },
199 { ISD::SRL, MVT::v2i64, 1 },
200 { ISD::SHL, MVT::v4i64, 1 },
201 { ISD::SRL, MVT::v4i64, 1 },
203 { ISD::SHL, MVT::v32i8, 42 }, // cmpeqb sequence.
204 { ISD::SHL, MVT::v16i16, 16*10 }, // Scalarized.
206 { ISD::SRL, MVT::v32i8, 32*10 }, // Scalarized.
207 { ISD::SRL, MVT::v16i16, 8*10 }, // Scalarized.
209 { ISD::SRA, MVT::v32i8, 32*10 }, // Scalarized.
210 { ISD::SRA, MVT::v16i16, 16*10 }, // Scalarized.
211 { ISD::SRA, MVT::v4i64, 4*10 }, // Scalarized.
213 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
214 { ISD::SDIV, MVT::v32i8, 32*20 },
215 { ISD::SDIV, MVT::v16i16, 16*20 },
216 { ISD::SDIV, MVT::v8i32, 8*20 },
217 { ISD::SDIV, MVT::v4i64, 4*20 },
218 { ISD::UDIV, MVT::v32i8, 32*20 },
219 { ISD::UDIV, MVT::v16i16, 16*20 },
220 { ISD::UDIV, MVT::v8i32, 8*20 },
221 { ISD::UDIV, MVT::v4i64, 4*20 },
224 // Look for AVX2 lowering tricks.
226 if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
227 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
228 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
229 // On AVX2, a packed v16i16 shift left by a constant build_vector
230 // is lowered into a vector multiply (vpmullw).
233 int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second);
235 return LT.first * AVX2CostTable[Idx].Cost;
238 static const CostTblEntry<MVT::SimpleValueType>
239 SSE2UniformConstCostTable[] = {
240 // We don't correctly identify costs of casts because they are marked as
242 // Constant splats are cheaper for the following instructions.
243 { ISD::SHL, MVT::v16i8, 1 }, // psllw.
244 { ISD::SHL, MVT::v8i16, 1 }, // psllw.
245 { ISD::SHL, MVT::v4i32, 1 }, // pslld
246 { ISD::SHL, MVT::v2i64, 1 }, // psllq.
248 { ISD::SRL, MVT::v16i8, 1 }, // psrlw.
249 { ISD::SRL, MVT::v8i16, 1 }, // psrlw.
250 { ISD::SRL, MVT::v4i32, 1 }, // psrld.
251 { ISD::SRL, MVT::v2i64, 1 }, // psrlq.
253 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb.
254 { ISD::SRA, MVT::v8i16, 1 }, // psraw.
255 { ISD::SRA, MVT::v4i32, 1 }, // psrad.
258 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
260 int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second);
262 return LT.first * SSE2UniformConstCostTable[Idx].Cost;
265 if (ISD == ISD::SHL &&
266 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
268 if ((VT == MVT::v8i16 && ST->hasSSE2()) ||
269 (VT == MVT::v4i32 && ST->hasSSE41()))
270 // Vector shift left by non uniform constant can be lowered
271 // into vector multiply (pmullw/pmulld).
273 if (VT == MVT::v4i32 && ST->hasSSE2())
274 // A vector shift left by non uniform constant is converted
275 // into a vector multiply; the new multiply is eventually
276 // lowered into a sequence of shuffles and 2 x pmuludq.
280 static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = {
281 // We don't correctly identify costs of casts because they are marked as
283 // For some cases, where the shift amount is a scalar we would be able
284 // to generate better code. Unfortunately, when this is the case the value
285 // (the splat) will get hoisted out of the loop, thereby making it invisible
286 // to ISel. The cost model must return worst case assumptions because it is
287 // used for vectorization and we don't want to make vectorized code worse
289 { ISD::SHL, MVT::v16i8, 30 }, // cmpeqb sequence.
290 { ISD::SHL, MVT::v8i16, 8*10 }, // Scalarized.
291 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul.
292 { ISD::SHL, MVT::v2i64, 2*10 }, // Scalarized.
293 { ISD::SHL, MVT::v4i64, 4*10 }, // Scalarized.
295 { ISD::SRL, MVT::v16i8, 16*10 }, // Scalarized.
296 { ISD::SRL, MVT::v8i16, 8*10 }, // Scalarized.
297 { ISD::SRL, MVT::v4i32, 4*10 }, // Scalarized.
298 { ISD::SRL, MVT::v2i64, 2*10 }, // Scalarized.
300 { ISD::SRA, MVT::v16i8, 16*10 }, // Scalarized.
301 { ISD::SRA, MVT::v8i16, 8*10 }, // Scalarized.
302 { ISD::SRA, MVT::v4i32, 4*10 }, // Scalarized.
303 { ISD::SRA, MVT::v2i64, 2*10 }, // Scalarized.
305 // It is not a good idea to vectorize division. We have to scalarize it and
306 // in the process we will often end up having to spilling regular
307 // registers. The overhead of division is going to dominate most kernels
308 // anyways so try hard to prevent vectorization of division - it is
309 // generally a bad idea. Assume somewhat arbitrarily that we have to be able
310 // to hide "20 cycles" for each lane.
311 { ISD::SDIV, MVT::v16i8, 16*20 },
312 { ISD::SDIV, MVT::v8i16, 8*20 },
313 { ISD::SDIV, MVT::v4i32, 4*20 },
314 { ISD::SDIV, MVT::v2i64, 2*20 },
315 { ISD::UDIV, MVT::v16i8, 16*20 },
316 { ISD::UDIV, MVT::v8i16, 8*20 },
317 { ISD::UDIV, MVT::v4i32, 4*20 },
318 { ISD::UDIV, MVT::v2i64, 2*20 },
322 int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second);
324 return LT.first * SSE2CostTable[Idx].Cost;
327 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = {
328 // We don't have to scalarize unsupported ops. We can issue two half-sized
329 // operations and we only need to extract the upper YMM half.
330 // Two ops + 1 extract + 1 insert = 4.
331 { ISD::MUL, MVT::v16i16, 4 },
332 { ISD::MUL, MVT::v8i32, 4 },
333 { ISD::SUB, MVT::v8i32, 4 },
334 { ISD::ADD, MVT::v8i32, 4 },
335 { ISD::SUB, MVT::v4i64, 4 },
336 { ISD::ADD, MVT::v4i64, 4 },
337 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
338 // are lowered as a series of long multiplies(3), shifts(4) and adds(2)
339 // Because we believe v4i64 to be a legal type, we must also include the
340 // split factor of two in the cost table. Therefore, the cost here is 18
342 { ISD::MUL, MVT::v4i64, 18 },
345 // Look for AVX1 lowering tricks.
346 if (ST->hasAVX() && !ST->hasAVX2()) {
349 // v16i16 and v8i32 shifts by non-uniform constants are lowered into a
350 // sequence of extract + two vector multiply + insert.
351 if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) &&
352 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)
355 int Idx = CostTableLookup(AVX1CostTable, ISD, VT);
357 return LT.first * AVX1CostTable[Idx].Cost;
360 // Custom lowering of vectors.
361 static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = {
362 // A v2i64/v4i64 and multiply is custom lowered as a series of long
363 // multiplies(3), shifts(4) and adds(2).
364 { ISD::MUL, MVT::v2i64, 9 },
365 { ISD::MUL, MVT::v4i64, 9 },
367 int Idx = CostTableLookup(CustomLowered, ISD, LT.second);
369 return LT.first * CustomLowered[Idx].Cost;
371 // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle,
372 // 2x pmuludq, 2x shuffle.
373 if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() &&
377 // Fallback to the default implementation.
378 return TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty, Op1Info,
382 unsigned X86TTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
384 // We only estimate the cost of reverse shuffles.
385 if (Kind != SK_Reverse)
386 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
388 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
390 if (LT.second.getSizeInBits() > 128)
391 Cost = 3; // Extract + insert + copy.
393 // Multiple by the number of parts.
394 return Cost * LT.first;
397 unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
398 int ISD = TLI->InstructionOpcodeToISD(Opcode);
399 assert(ISD && "Invalid opcode");
401 std::pair<unsigned, MVT> LTSrc = TLI->getTypeLegalizationCost(Src);
402 std::pair<unsigned, MVT> LTDest = TLI->getTypeLegalizationCost(Dst);
404 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
406 // These are somewhat magic numbers justified by looking at the output of
407 // Intel's IACA, running some kernels and making sure when we take
408 // legalization into account the throughput will be overestimated.
409 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
410 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
411 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
412 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
413 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
414 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
415 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
416 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
417 // There are faster sequences for float conversions.
418 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
419 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
420 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
421 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
422 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
423 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
424 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
425 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
428 if (ST->hasSSE2() && !ST->hasAVX()) {
430 ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second);
432 return LTSrc.first * SSE2ConvTbl[Idx].Cost;
435 EVT SrcTy = TLI->getValueType(Src);
436 EVT DstTy = TLI->getValueType(Dst);
438 // The function getSimpleVT only handles simple value types.
439 if (!SrcTy.isSimple() || !DstTy.isSimple())
440 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
442 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
443 AVX2ConversionTbl[] = {
444 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
445 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
446 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
447 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
448 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
449 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
450 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
451 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
452 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
453 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
454 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
455 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
456 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
457 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
458 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
459 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
461 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 },
462 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 },
463 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 },
464 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 },
465 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 },
466 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 },
469 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
470 AVXConversionTbl[] = {
471 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
472 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
473 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 },
474 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 },
475 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 },
476 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
477 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
478 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
479 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 },
480 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 },
481 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 },
482 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
483 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 },
484 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
485 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
486 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
488 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 },
489 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 },
490 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 },
491 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
492 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
493 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 },
494 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 },
496 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 },
497 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 },
498 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
499 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
500 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
501 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
502 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 },
503 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
504 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 },
505 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 },
506 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 },
507 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
509 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 },
510 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 },
511 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
512 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 },
513 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 },
514 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 },
515 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
516 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 },
517 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 },
518 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
519 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
520 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 },
522 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 },
523 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
527 int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
528 DstTy.getSimpleVT(), SrcTy.getSimpleVT());
530 return AVX2ConversionTbl[Idx].Cost;
534 int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(),
535 SrcTy.getSimpleVT());
537 return AVXConversionTbl[Idx].Cost;
540 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
543 unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
544 Type *CondTy) const {
545 // Legalize the type.
546 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
550 int ISD = TLI->InstructionOpcodeToISD(Opcode);
551 assert(ISD && "Invalid opcode");
553 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = {
554 { ISD::SETCC, MVT::v2f64, 1 },
555 { ISD::SETCC, MVT::v4f32, 1 },
556 { ISD::SETCC, MVT::v2i64, 1 },
557 { ISD::SETCC, MVT::v4i32, 1 },
558 { ISD::SETCC, MVT::v8i16, 1 },
559 { ISD::SETCC, MVT::v16i8, 1 },
562 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = {
563 { ISD::SETCC, MVT::v4f64, 1 },
564 { ISD::SETCC, MVT::v8f32, 1 },
565 // AVX1 does not support 8-wide integer compare.
566 { ISD::SETCC, MVT::v4i64, 4 },
567 { ISD::SETCC, MVT::v8i32, 4 },
568 { ISD::SETCC, MVT::v16i16, 4 },
569 { ISD::SETCC, MVT::v32i8, 4 },
572 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = {
573 { ISD::SETCC, MVT::v4i64, 1 },
574 { ISD::SETCC, MVT::v8i32, 1 },
575 { ISD::SETCC, MVT::v16i16, 1 },
576 { ISD::SETCC, MVT::v32i8, 1 },
580 int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy);
582 return LT.first * AVX2CostTbl[Idx].Cost;
586 int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy);
588 return LT.first * AVX1CostTbl[Idx].Cost;
591 if (ST->hasSSE42()) {
592 int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy);
594 return LT.first * SSE42CostTbl[Idx].Cost;
597 return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
600 unsigned X86TTI::getVectorInstrCost(unsigned Opcode, Type *Val,
601 unsigned Index) const {
602 assert(Val->isVectorTy() && "This must be a vector type");
605 // Legalize the type.
606 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Val);
608 // This type is legalized to a scalar type.
609 if (!LT.second.isVector())
612 // The type may be split. Normalize the index to the new type.
613 unsigned Width = LT.second.getVectorNumElements();
614 Index = Index % Width;
616 // Floating point scalars are already located in index #0.
617 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
621 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
624 unsigned X86TTI::getScalarizationOverhead(Type *Ty, bool Insert,
625 bool Extract) const {
626 assert (Ty->isVectorTy() && "Can only scalarize vectors");
629 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
631 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
633 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
639 unsigned X86TTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
640 unsigned AddressSpace) const {
641 // Handle non-power-of-two vectors such as <3 x float>
642 if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
643 unsigned NumElem = VTy->getVectorNumElements();
645 // Handle a few common cases:
647 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
648 // Cost = 64 bit store + extract + 32 bit store.
652 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
653 // Cost = 128 bit store + unpack + 64 bit store.
656 // Assume that all other non-power-of-two numbers are scalarized.
657 if (!isPowerOf2_32(NumElem)) {
658 unsigned Cost = TargetTransformInfo::getMemoryOpCost(Opcode,
659 VTy->getScalarType(),
662 unsigned SplitCost = getScalarizationOverhead(Src,
663 Opcode == Instruction::Load,
664 Opcode==Instruction::Store);
665 return NumElem * Cost + SplitCost;
669 // Legalize the type.
670 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
671 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
674 // Each load/store unit costs 1.
675 unsigned Cost = LT.first * 1;
677 // On Sandybridge 256bit load/stores are double pumped
678 // (but not on Haswell).
679 if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2())
685 unsigned X86TTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
686 // Address computations in vectorized code with non-consecutive addresses will
687 // likely result in more instructions compared to scalar code where the
688 // computation can more often be merged into the index mode. The resulting
689 // extra micro-ops can significantly decrease throughput.
690 unsigned NumVectorInstToHideOverhead = 10;
692 if (Ty->isVectorTy() && IsComplex)
693 return NumVectorInstToHideOverhead;
695 return TargetTransformInfo::getAddressComputationCost(Ty, IsComplex);
698 unsigned X86TTI::getReductionCost(unsigned Opcode, Type *ValTy,
699 bool IsPairwise) const {
701 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
705 int ISD = TLI->InstructionOpcodeToISD(Opcode);
706 assert(ISD && "Invalid opcode");
708 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
709 // and make it as the cost.
711 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = {
712 { ISD::FADD, MVT::v2f64, 2 },
713 { ISD::FADD, MVT::v4f32, 4 },
714 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
715 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
716 { ISD::ADD, MVT::v8i16, 5 },
719 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = {
720 { ISD::FADD, MVT::v4f32, 4 },
721 { ISD::FADD, MVT::v4f64, 5 },
722 { ISD::FADD, MVT::v8f32, 7 },
723 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
724 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
725 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8".
726 { ISD::ADD, MVT::v8i16, 5 },
727 { ISD::ADD, MVT::v8i32, 5 },
730 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = {
731 { ISD::FADD, MVT::v2f64, 2 },
732 { ISD::FADD, MVT::v4f32, 4 },
733 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
734 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3".
735 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3".
738 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = {
739 { ISD::FADD, MVT::v4f32, 3 },
740 { ISD::FADD, MVT::v4f64, 3 },
741 { ISD::FADD, MVT::v8f32, 4 },
742 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
743 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8".
744 { ISD::ADD, MVT::v4i64, 3 },
745 { ISD::ADD, MVT::v8i16, 4 },
746 { ISD::ADD, MVT::v8i32, 5 },
751 int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy);
753 return LT.first * AVX1CostTblPairWise[Idx].Cost;
756 if (ST->hasSSE42()) {
757 int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy);
759 return LT.first * SSE42CostTblPairWise[Idx].Cost;
763 int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy);
765 return LT.first * AVX1CostTblNoPairWise[Idx].Cost;
768 if (ST->hasSSE42()) {
769 int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy);
771 return LT.first * SSE42CostTblNoPairWise[Idx].Cost;
775 return TargetTransformInfo::getReductionCost(Opcode, ValTy, IsPairwise);
778 unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
779 assert(Ty->isIntegerTy());
781 unsigned BitSize = Ty->getPrimitiveSizeInBits();
785 if (Imm.getBitWidth() <= 64 &&
786 (isInt<32>(Imm.getSExtValue()) || isUInt<32>(Imm.getZExtValue())))
789 return 2 * TCC_Basic;
792 unsigned X86TTI::getIntImmCost(unsigned Opcode, const APInt &Imm,
794 assert(Ty->isIntegerTy());
796 unsigned BitSize = Ty->getPrimitiveSizeInBits();
801 case Instruction::Add:
802 case Instruction::Sub:
803 case Instruction::Mul:
804 case Instruction::UDiv:
805 case Instruction::SDiv:
806 case Instruction::URem:
807 case Instruction::SRem:
808 case Instruction::Shl:
809 case Instruction::LShr:
810 case Instruction::AShr:
811 case Instruction::And:
812 case Instruction::Or:
813 case Instruction::Xor:
814 case Instruction::ICmp:
815 if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
818 return X86TTI::getIntImmCost(Imm, Ty);
819 case Instruction::Trunc:
820 case Instruction::ZExt:
821 case Instruction::SExt:
822 case Instruction::IntToPtr:
823 case Instruction::PtrToInt:
824 case Instruction::BitCast:
825 case Instruction::Call:
826 case Instruction::Select:
827 case Instruction::Ret:
828 case Instruction::Load:
829 case Instruction::Store:
830 return X86TTI::getIntImmCost(Imm, Ty);
832 return TargetTransformInfo::getIntImmCost(Opcode, Imm, Ty);
835 unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
837 assert(Ty->isIntegerTy());
839 unsigned BitSize = Ty->getPrimitiveSizeInBits();
844 default: return TargetTransformInfo::getIntImmCost(IID, Imm, Ty);
845 case Intrinsic::sadd_with_overflow:
846 case Intrinsic::uadd_with_overflow:
847 case Intrinsic::ssub_with_overflow:
848 case Intrinsic::usub_with_overflow:
849 case Intrinsic::smul_with_overflow:
850 case Intrinsic::umul_with_overflow:
851 if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
854 return X86TTI::getIntImmCost(Imm, Ty);
855 case Intrinsic::experimental_stackmap:
856 case Intrinsic::experimental_patchpoint_void:
857 case Intrinsic::experimental_patchpoint_i64:
858 if (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))
861 return X86TTI::getIntImmCost(Imm, Ty);