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 //===----------------------------------------------------------------------===//
18 #include "X86TargetMachine.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/IR/IntrinsicInst.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Target/CostTable.h"
23 #include "llvm/Target/TargetLowering.h"
26 #define DEBUG_TYPE "x86tti"
28 // Declare the pass initialization routine locally as target-specific passes
29 // don't have a target-wide initialization entry point, and so we rely on the
30 // pass constructor initialization.
32 void initializeX86TTIPass(PassRegistry &);
37 class X86TTI final : public ImmutablePass, public TargetTransformInfo {
38 const X86Subtarget *ST;
39 const X86TargetLowering *TLI;
41 /// Estimate the overhead of scalarizing an instruction. Insert and Extract
42 /// are set if the result needs to be inserted and/or extracted from vectors.
43 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
46 X86TTI() : ImmutablePass(ID), ST(nullptr), TLI(nullptr) {
47 llvm_unreachable("This pass cannot be directly constructed");
50 X86TTI(const X86TargetMachine *TM)
51 : ImmutablePass(ID), ST(TM->getSubtargetImpl()),
52 TLI(TM->getSubtargetImpl()->getTargetLowering()) {
53 initializeX86TTIPass(*PassRegistry::getPassRegistry());
56 void initializePass() override {
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 TargetTransformInfo::getAnalysisUsage(AU);
64 /// Pass identification.
67 /// Provide necessary pointer adjustments for the two base classes.
68 void *getAdjustedAnalysisPointer(const void *ID) override {
69 if (ID == &TargetTransformInfo::ID)
70 return (TargetTransformInfo*)this;
74 /// \name Scalar TTI Implementations
76 PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override;
80 /// \name Vector TTI Implementations
83 unsigned getNumberOfRegisters(bool Vector) const override;
84 unsigned getRegisterBitWidth(bool Vector) const override;
85 unsigned getMaxInterleaveFactor() const override;
86 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
87 OperandValueKind, OperandValueProperties,
88 OperandValueProperties) const override;
89 unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
90 int Index, Type *SubTp) const override;
91 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
92 Type *Src) const override;
93 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
94 Type *CondTy) const override;
95 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
96 unsigned Index) const override;
97 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
98 unsigned AddressSpace) const override;
99 unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
101 unsigned AddressSpace) const override;
103 unsigned getAddressComputationCost(Type *PtrTy,
104 bool IsComplex) const override;
106 unsigned getReductionCost(unsigned Opcode, Type *Ty,
107 bool IsPairwiseForm) const override;
109 unsigned getIntImmCost(int64_t) const;
111 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override;
113 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
114 Type *Ty) const override;
115 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
116 Type *Ty) const override;
117 bool isLegalMaskedLoad (Type *DataType, int Consecutive) const override;
118 bool isLegalMaskedStore(Type *DataType, int Consecutive) const override;
123 } // end anonymous namespace
125 INITIALIZE_AG_PASS(X86TTI, TargetTransformInfo, "x86tti",
126 "X86 Target Transform Info", true, true, false)
130 llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) {
131 return new X86TTI(TM);
135 //===----------------------------------------------------------------------===//
139 //===----------------------------------------------------------------------===//
141 X86TTI::PopcntSupportKind X86TTI::getPopcntSupport(unsigned TyWidth) const {
142 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
143 // TODO: Currently the __builtin_popcount() implementation using SSE3
144 // instructions is inefficient. Once the problem is fixed, we should
145 // call ST->hasSSE3() instead of ST->hasPOPCNT().
146 return ST->hasPOPCNT() ? PSK_FastHardware : PSK_Software;
149 unsigned X86TTI::getNumberOfRegisters(bool Vector) const {
150 if (Vector && !ST->hasSSE1())
154 if (Vector && ST->hasAVX512())
161 unsigned X86TTI::getRegisterBitWidth(bool Vector) const {
163 if (ST->hasAVX512()) return 512;
164 if (ST->hasAVX()) return 256;
165 if (ST->hasSSE1()) return 128;
175 unsigned X86TTI::getMaxInterleaveFactor() const {
179 // Sandybridge and Haswell have multiple execution ports and pipelined
187 unsigned X86TTI::getArithmeticInstrCost(
188 unsigned Opcode, Type *Ty, OperandValueKind Op1Info,
189 OperandValueKind Op2Info, OperandValueProperties Opd1PropInfo,
190 OperandValueProperties Opd2PropInfo) const {
191 // Legalize the type.
192 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
194 int ISD = TLI->InstructionOpcodeToISD(Opcode);
195 assert(ISD && "Invalid opcode");
197 if (ISD == ISD::SDIV &&
198 Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
199 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) {
200 // On X86, vector signed division by constants power-of-two are
201 // normally expanded to the sequence SRA + SRL + ADD + SRA.
202 // The OperandValue properties many not be same as that of previous
203 // operation;conservatively assume OP_None.
205 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info, Op2Info,
206 TargetTransformInfo::OP_None,
207 TargetTransformInfo::OP_None);
208 Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info,
209 TargetTransformInfo::OP_None,
210 TargetTransformInfo::OP_None);
211 Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info,
212 TargetTransformInfo::OP_None,
213 TargetTransformInfo::OP_None);
218 static const CostTblEntry<MVT::SimpleValueType>
219 AVX2UniformConstCostTable[] = {
220 { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence
221 { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence
222 { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence
223 { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence
226 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
228 int Idx = CostTableLookup(AVX2UniformConstCostTable, ISD, LT.second);
230 return LT.first * AVX2UniformConstCostTable[Idx].Cost;
233 static const CostTblEntry<MVT::SimpleValueType> AVX512CostTable[] = {
234 { ISD::SHL, MVT::v16i32, 1 },
235 { ISD::SRL, MVT::v16i32, 1 },
236 { ISD::SRA, MVT::v16i32, 1 },
237 { ISD::SHL, MVT::v8i64, 1 },
238 { ISD::SRL, MVT::v8i64, 1 },
239 { ISD::SRA, MVT::v8i64, 1 },
242 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = {
243 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
244 // customize them to detect the cases where shift amount is a scalar one.
245 { ISD::SHL, MVT::v4i32, 1 },
246 { ISD::SRL, MVT::v4i32, 1 },
247 { ISD::SRA, MVT::v4i32, 1 },
248 { ISD::SHL, MVT::v8i32, 1 },
249 { ISD::SRL, MVT::v8i32, 1 },
250 { ISD::SRA, MVT::v8i32, 1 },
251 { ISD::SHL, MVT::v2i64, 1 },
252 { ISD::SRL, MVT::v2i64, 1 },
253 { ISD::SHL, MVT::v4i64, 1 },
254 { ISD::SRL, MVT::v4i64, 1 },
256 { ISD::SHL, MVT::v32i8, 42 }, // cmpeqb sequence.
257 { ISD::SHL, MVT::v16i16, 16*10 }, // Scalarized.
259 { ISD::SRL, MVT::v32i8, 32*10 }, // Scalarized.
260 { ISD::SRL, MVT::v16i16, 8*10 }, // Scalarized.
262 { ISD::SRA, MVT::v32i8, 32*10 }, // Scalarized.
263 { ISD::SRA, MVT::v16i16, 16*10 }, // Scalarized.
264 { ISD::SRA, MVT::v4i64, 4*10 }, // Scalarized.
266 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
267 { ISD::SDIV, MVT::v32i8, 32*20 },
268 { ISD::SDIV, MVT::v16i16, 16*20 },
269 { ISD::SDIV, MVT::v8i32, 8*20 },
270 { ISD::SDIV, MVT::v4i64, 4*20 },
271 { ISD::UDIV, MVT::v32i8, 32*20 },
272 { ISD::UDIV, MVT::v16i16, 16*20 },
273 { ISD::UDIV, MVT::v8i32, 8*20 },
274 { ISD::UDIV, MVT::v4i64, 4*20 },
277 if (ST->hasAVX512()) {
278 int Idx = CostTableLookup(AVX512CostTable, ISD, LT.second);
280 return LT.first * AVX512CostTable[Idx].Cost;
282 // Look for AVX2 lowering tricks.
284 if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
285 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
286 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
287 // On AVX2, a packed v16i16 shift left by a constant build_vector
288 // is lowered into a vector multiply (vpmullw).
291 int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second);
293 return LT.first * AVX2CostTable[Idx].Cost;
296 static const CostTblEntry<MVT::SimpleValueType>
297 SSE2UniformConstCostTable[] = {
298 // We don't correctly identify costs of casts because they are marked as
300 // Constant splats are cheaper for the following instructions.
301 { ISD::SHL, MVT::v16i8, 1 }, // psllw.
302 { ISD::SHL, MVT::v8i16, 1 }, // psllw.
303 { ISD::SHL, MVT::v4i32, 1 }, // pslld
304 { ISD::SHL, MVT::v2i64, 1 }, // psllq.
306 { ISD::SRL, MVT::v16i8, 1 }, // psrlw.
307 { ISD::SRL, MVT::v8i16, 1 }, // psrlw.
308 { ISD::SRL, MVT::v4i32, 1 }, // psrld.
309 { ISD::SRL, MVT::v2i64, 1 }, // psrlq.
311 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb.
312 { ISD::SRA, MVT::v8i16, 1 }, // psraw.
313 { ISD::SRA, MVT::v4i32, 1 }, // psrad.
315 { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence
316 { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence
317 { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence
318 { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence
321 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
324 if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41())
325 return LT.first * 15;
327 int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second);
329 return LT.first * SSE2UniformConstCostTable[Idx].Cost;
332 if (ISD == ISD::SHL &&
333 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
335 if ((VT == MVT::v8i16 && ST->hasSSE2()) ||
336 (VT == MVT::v4i32 && ST->hasSSE41()))
337 // Vector shift left by non uniform constant can be lowered
338 // into vector multiply (pmullw/pmulld).
340 if (VT == MVT::v4i32 && ST->hasSSE2())
341 // A vector shift left by non uniform constant is converted
342 // into a vector multiply; the new multiply is eventually
343 // lowered into a sequence of shuffles and 2 x pmuludq.
347 static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = {
348 // We don't correctly identify costs of casts because they are marked as
350 // For some cases, where the shift amount is a scalar we would be able
351 // to generate better code. Unfortunately, when this is the case the value
352 // (the splat) will get hoisted out of the loop, thereby making it invisible
353 // to ISel. The cost model must return worst case assumptions because it is
354 // used for vectorization and we don't want to make vectorized code worse
356 { ISD::SHL, MVT::v16i8, 30 }, // cmpeqb sequence.
357 { ISD::SHL, MVT::v8i16, 8*10 }, // Scalarized.
358 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul.
359 { ISD::SHL, MVT::v2i64, 2*10 }, // Scalarized.
360 { ISD::SHL, MVT::v4i64, 4*10 }, // Scalarized.
362 { ISD::SRL, MVT::v16i8, 16*10 }, // Scalarized.
363 { ISD::SRL, MVT::v8i16, 8*10 }, // Scalarized.
364 { ISD::SRL, MVT::v4i32, 4*10 }, // Scalarized.
365 { ISD::SRL, MVT::v2i64, 2*10 }, // Scalarized.
367 { ISD::SRA, MVT::v16i8, 16*10 }, // Scalarized.
368 { ISD::SRA, MVT::v8i16, 8*10 }, // Scalarized.
369 { ISD::SRA, MVT::v4i32, 4*10 }, // Scalarized.
370 { ISD::SRA, MVT::v2i64, 2*10 }, // Scalarized.
372 // It is not a good idea to vectorize division. We have to scalarize it and
373 // in the process we will often end up having to spilling regular
374 // registers. The overhead of division is going to dominate most kernels
375 // anyways so try hard to prevent vectorization of division - it is
376 // generally a bad idea. Assume somewhat arbitrarily that we have to be able
377 // to hide "20 cycles" for each lane.
378 { ISD::SDIV, MVT::v16i8, 16*20 },
379 { ISD::SDIV, MVT::v8i16, 8*20 },
380 { ISD::SDIV, MVT::v4i32, 4*20 },
381 { ISD::SDIV, MVT::v2i64, 2*20 },
382 { ISD::UDIV, MVT::v16i8, 16*20 },
383 { ISD::UDIV, MVT::v8i16, 8*20 },
384 { ISD::UDIV, MVT::v4i32, 4*20 },
385 { ISD::UDIV, MVT::v2i64, 2*20 },
389 int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second);
391 return LT.first * SSE2CostTable[Idx].Cost;
394 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = {
395 // We don't have to scalarize unsupported ops. We can issue two half-sized
396 // operations and we only need to extract the upper YMM half.
397 // Two ops + 1 extract + 1 insert = 4.
398 { ISD::MUL, MVT::v16i16, 4 },
399 { ISD::MUL, MVT::v8i32, 4 },
400 { ISD::SUB, MVT::v8i32, 4 },
401 { ISD::ADD, MVT::v8i32, 4 },
402 { ISD::SUB, MVT::v4i64, 4 },
403 { ISD::ADD, MVT::v4i64, 4 },
404 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
405 // are lowered as a series of long multiplies(3), shifts(4) and adds(2)
406 // Because we believe v4i64 to be a legal type, we must also include the
407 // split factor of two in the cost table. Therefore, the cost here is 18
409 { ISD::MUL, MVT::v4i64, 18 },
412 // Look for AVX1 lowering tricks.
413 if (ST->hasAVX() && !ST->hasAVX2()) {
416 // v16i16 and v8i32 shifts by non-uniform constants are lowered into a
417 // sequence of extract + two vector multiply + insert.
418 if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) &&
419 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)
422 int Idx = CostTableLookup(AVX1CostTable, ISD, VT);
424 return LT.first * AVX1CostTable[Idx].Cost;
427 // Custom lowering of vectors.
428 static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = {
429 // A v2i64/v4i64 and multiply is custom lowered as a series of long
430 // multiplies(3), shifts(4) and adds(2).
431 { ISD::MUL, MVT::v2i64, 9 },
432 { ISD::MUL, MVT::v4i64, 9 },
434 int Idx = CostTableLookup(CustomLowered, ISD, LT.second);
436 return LT.first * CustomLowered[Idx].Cost;
438 // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle,
439 // 2x pmuludq, 2x shuffle.
440 if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() &&
444 // Fallback to the default implementation.
445 return TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty, Op1Info,
449 unsigned X86TTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
451 // We only estimate the cost of reverse and alternate shuffles.
452 if (Kind != SK_Reverse && Kind != SK_Alternate)
453 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
455 if (Kind == SK_Reverse) {
456 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
458 if (LT.second.getSizeInBits() > 128)
459 Cost = 3; // Extract + insert + copy.
461 // Multiple by the number of parts.
462 return Cost * LT.first;
465 if (Kind == SK_Alternate) {
466 // 64-bit packed float vectors (v2f32) are widened to type v4f32.
467 // 64-bit packed integer vectors (v2i32) are promoted to type v2i64.
468 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
470 // The backend knows how to generate a single VEX.256 version of
471 // instruction VPBLENDW if the target supports AVX2.
472 if (ST->hasAVX2() && LT.second == MVT::v16i16)
475 static const CostTblEntry<MVT::SimpleValueType> AVXAltShuffleTbl[] = {
476 {ISD::VECTOR_SHUFFLE, MVT::v4i64, 1}, // vblendpd
477 {ISD::VECTOR_SHUFFLE, MVT::v4f64, 1}, // vblendpd
479 {ISD::VECTOR_SHUFFLE, MVT::v8i32, 1}, // vblendps
480 {ISD::VECTOR_SHUFFLE, MVT::v8f32, 1}, // vblendps
482 // This shuffle is custom lowered into a sequence of:
483 // 2x vextractf128 , 2x vpblendw , 1x vinsertf128
484 {ISD::VECTOR_SHUFFLE, MVT::v16i16, 5},
486 // This shuffle is custom lowered into a long sequence of:
487 // 2x vextractf128 , 4x vpshufb , 2x vpor , 1x vinsertf128
488 {ISD::VECTOR_SHUFFLE, MVT::v32i8, 9}
492 int Idx = CostTableLookup(AVXAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
494 return LT.first * AVXAltShuffleTbl[Idx].Cost;
497 static const CostTblEntry<MVT::SimpleValueType> SSE41AltShuffleTbl[] = {
498 // These are lowered into movsd.
499 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
500 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
502 // packed float vectors with four elements are lowered into BLENDI dag
503 // nodes. A v4i32/v4f32 BLENDI generates a single 'blendps'/'blendpd'.
504 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1},
505 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1},
507 // This shuffle generates a single pshufw.
508 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1},
510 // There is no instruction that matches a v16i8 alternate shuffle.
511 // The backend will expand it into the sequence 'pshufb + pshufb + or'.
512 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3}
515 if (ST->hasSSE41()) {
516 int Idx = CostTableLookup(SSE41AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
518 return LT.first * SSE41AltShuffleTbl[Idx].Cost;
521 static const CostTblEntry<MVT::SimpleValueType> SSSE3AltShuffleTbl[] = {
522 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
523 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
525 // SSE3 doesn't have 'blendps'. The following shuffles are expanded into
526 // the sequence 'shufps + pshufd'
527 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
528 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
530 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 3}, // pshufb + pshufb + or
531 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} // pshufb + pshufb + or
534 if (ST->hasSSSE3()) {
535 int Idx = CostTableLookup(SSSE3AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
537 return LT.first * SSSE3AltShuffleTbl[Idx].Cost;
540 static const CostTblEntry<MVT::SimpleValueType> SSEAltShuffleTbl[] = {
541 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
542 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
544 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, // shufps + pshufd
545 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, // shufps + pshufd
547 // This is expanded into a long sequence of four extract + four insert.
548 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 8}, // 4 x pextrw + 4 pinsrw.
550 // 8 x (pinsrw + pextrw + and + movb + movzb + or)
551 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 48}
554 // Fall-back (SSE3 and SSE2).
555 int Idx = CostTableLookup(SSEAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
557 return LT.first * SSEAltShuffleTbl[Idx].Cost;
558 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
561 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
564 unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
565 int ISD = TLI->InstructionOpcodeToISD(Opcode);
566 assert(ISD && "Invalid opcode");
568 std::pair<unsigned, MVT> LTSrc = TLI->getTypeLegalizationCost(Src);
569 std::pair<unsigned, MVT> LTDest = TLI->getTypeLegalizationCost(Dst);
571 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
573 // These are somewhat magic numbers justified by looking at the output of
574 // Intel's IACA, running some kernels and making sure when we take
575 // legalization into account the throughput will be overestimated.
576 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
577 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
578 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
579 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
580 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
581 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
582 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
583 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
584 // There are faster sequences for float conversions.
585 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
586 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 },
587 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
588 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
589 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
590 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
591 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
592 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
595 if (ST->hasSSE2() && !ST->hasAVX()) {
597 ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second);
599 return LTSrc.first * SSE2ConvTbl[Idx].Cost;
602 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
603 AVX512ConversionTbl[] = {
604 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 },
605 { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 },
606 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 },
607 { ISD::FP_ROUND, MVT::v16f32, MVT::v8f64, 3 },
609 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 },
610 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 },
611 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 },
612 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 },
613 { ISD::TRUNCATE, MVT::v16i32, MVT::v8i64, 4 },
615 // v16i1 -> v16i32 - load + broadcast
616 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
617 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
619 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
620 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
621 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
622 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
623 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v16i32, 3 },
624 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v16i32, 3 },
626 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 },
627 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 },
628 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 },
629 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 },
630 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 },
631 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 },
632 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 },
635 if (ST->hasAVX512()) {
636 int Idx = ConvertCostTableLookup(AVX512ConversionTbl, ISD, LTDest.second,
639 return AVX512ConversionTbl[Idx].Cost;
641 EVT SrcTy = TLI->getValueType(Src);
642 EVT DstTy = TLI->getValueType(Dst);
644 // The function getSimpleVT only handles simple value types.
645 if (!SrcTy.isSimple() || !DstTy.isSimple())
646 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
648 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
649 AVX2ConversionTbl[] = {
650 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
651 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
652 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
653 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
654 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
655 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
656 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
657 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
658 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
659 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
660 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
661 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
662 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
663 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
664 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
665 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
667 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 },
668 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 },
669 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 },
670 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 },
671 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 },
672 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 },
674 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 },
675 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 },
677 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 },
680 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
681 AVXConversionTbl[] = {
682 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
683 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
684 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 },
685 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 },
686 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 },
687 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
688 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
689 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
690 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 },
691 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 },
692 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 },
693 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
694 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 },
695 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
696 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
697 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
699 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 },
700 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 },
701 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 },
702 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
703 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
704 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 },
705 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 },
707 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 },
708 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 },
709 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
710 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
711 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
712 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
713 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 },
714 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
715 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 },
716 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 },
717 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 },
718 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
720 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 },
721 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 },
722 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
723 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 },
724 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 },
725 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 },
726 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
727 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 },
728 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 },
729 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
730 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
731 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 },
732 // The generic code to compute the scalar overhead is currently broken.
733 // Workaround this limitation by estimating the scalarization overhead
734 // here. We have roughly 10 instructions per scalar element.
735 // Multiply that by the vector width.
736 // FIXME: remove that when PR19268 is fixed.
737 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
738 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 4*10 },
740 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 },
741 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
742 // This node is expanded into scalarized operations but BasicTTI is overly
743 // optimistic estimating its cost. It computes 3 per element (one
744 // vector-extract, one scalar conversion and one vector-insert). The
745 // problem is that the inserts form a read-modify-write chain so latency
746 // should be factored in too. Inflating the cost per element by 1.
747 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 },
748 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 },
752 int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
753 DstTy.getSimpleVT(), SrcTy.getSimpleVT());
755 return AVX2ConversionTbl[Idx].Cost;
759 int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(),
760 SrcTy.getSimpleVT());
762 return AVXConversionTbl[Idx].Cost;
765 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
768 unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
769 Type *CondTy) const {
770 // Legalize the type.
771 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
775 int ISD = TLI->InstructionOpcodeToISD(Opcode);
776 assert(ISD && "Invalid opcode");
778 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = {
779 { ISD::SETCC, MVT::v2f64, 1 },
780 { ISD::SETCC, MVT::v4f32, 1 },
781 { ISD::SETCC, MVT::v2i64, 1 },
782 { ISD::SETCC, MVT::v4i32, 1 },
783 { ISD::SETCC, MVT::v8i16, 1 },
784 { ISD::SETCC, MVT::v16i8, 1 },
787 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = {
788 { ISD::SETCC, MVT::v4f64, 1 },
789 { ISD::SETCC, MVT::v8f32, 1 },
790 // AVX1 does not support 8-wide integer compare.
791 { ISD::SETCC, MVT::v4i64, 4 },
792 { ISD::SETCC, MVT::v8i32, 4 },
793 { ISD::SETCC, MVT::v16i16, 4 },
794 { ISD::SETCC, MVT::v32i8, 4 },
797 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = {
798 { ISD::SETCC, MVT::v4i64, 1 },
799 { ISD::SETCC, MVT::v8i32, 1 },
800 { ISD::SETCC, MVT::v16i16, 1 },
801 { ISD::SETCC, MVT::v32i8, 1 },
804 static const CostTblEntry<MVT::SimpleValueType> AVX512CostTbl[] = {
805 { ISD::SETCC, MVT::v8i64, 1 },
806 { ISD::SETCC, MVT::v16i32, 1 },
807 { ISD::SETCC, MVT::v8f64, 1 },
808 { ISD::SETCC, MVT::v16f32, 1 },
811 if (ST->hasAVX512()) {
812 int Idx = CostTableLookup(AVX512CostTbl, ISD, MTy);
814 return LT.first * AVX512CostTbl[Idx].Cost;
818 int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy);
820 return LT.first * AVX2CostTbl[Idx].Cost;
824 int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy);
826 return LT.first * AVX1CostTbl[Idx].Cost;
829 if (ST->hasSSE42()) {
830 int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy);
832 return LT.first * SSE42CostTbl[Idx].Cost;
835 return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
838 unsigned X86TTI::getVectorInstrCost(unsigned Opcode, Type *Val,
839 unsigned Index) const {
840 assert(Val->isVectorTy() && "This must be a vector type");
843 // Legalize the type.
844 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Val);
846 // This type is legalized to a scalar type.
847 if (!LT.second.isVector())
850 // The type may be split. Normalize the index to the new type.
851 unsigned Width = LT.second.getVectorNumElements();
852 Index = Index % Width;
854 // Floating point scalars are already located in index #0.
855 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
859 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
862 unsigned X86TTI::getScalarizationOverhead(Type *Ty, bool Insert,
863 bool Extract) const {
864 assert (Ty->isVectorTy() && "Can only scalarize vectors");
867 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
869 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
871 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
877 unsigned X86TTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
878 unsigned AddressSpace) const {
879 // Handle non-power-of-two vectors such as <3 x float>
880 if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
881 unsigned NumElem = VTy->getVectorNumElements();
883 // Handle a few common cases:
885 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
886 // Cost = 64 bit store + extract + 32 bit store.
890 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
891 // Cost = 128 bit store + unpack + 64 bit store.
894 // Assume that all other non-power-of-two numbers are scalarized.
895 if (!isPowerOf2_32(NumElem)) {
896 unsigned Cost = TargetTransformInfo::getMemoryOpCost(Opcode,
897 VTy->getScalarType(),
900 unsigned SplitCost = getScalarizationOverhead(Src,
901 Opcode == Instruction::Load,
902 Opcode==Instruction::Store);
903 return NumElem * Cost + SplitCost;
907 // Legalize the type.
908 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
909 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
912 // Each load/store unit costs 1.
913 unsigned Cost = LT.first * 1;
915 // On Sandybridge 256bit load/stores are double pumped
916 // (but not on Haswell).
917 if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2())
923 unsigned X86TTI::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy,
925 unsigned AddressSpace) const {
926 VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy);
928 // To calculate scalar take the regular cost, without mask
929 return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace);
931 unsigned NumElem = SrcVTy->getVectorNumElements();
933 VectorType::get(Type::getInt8Ty(getGlobalContext()), NumElem);
934 if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy, 1)) ||
935 (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy, 1)) ||
936 !isPowerOf2_32(NumElem)) {
938 unsigned MaskSplitCost = getScalarizationOverhead(MaskTy, false, true);
939 unsigned ScalarCompareCost =
940 getCmpSelInstrCost(Instruction::ICmp,
941 Type::getInt8Ty(getGlobalContext()), NULL);
942 unsigned BranchCost = getCFInstrCost(Instruction::Br);
943 unsigned MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost);
945 unsigned ValueSplitCost =
946 getScalarizationOverhead(SrcVTy, Opcode == Instruction::Load,
947 Opcode == Instruction::Store);
948 unsigned MemopCost = NumElem *
949 TargetTransformInfo::getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
950 Alignment, AddressSpace);
951 return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost;
954 // Legalize the type.
955 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(SrcVTy);
957 if (LT.second != TLI->getValueType(SrcVTy).getSimpleVT() &&
958 LT.second.getVectorNumElements() == NumElem)
959 // Promotion requires expand/truncate for data and a shuffle for mask.
960 Cost += getShuffleCost(TargetTransformInfo::SK_Alternate, SrcVTy, 0, 0) +
961 getShuffleCost(TargetTransformInfo::SK_Alternate, MaskTy, 0, 0);
963 else if (LT.second.getVectorNumElements() > NumElem) {
964 VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(),
965 LT.second.getVectorNumElements());
966 // Expanding requires fill mask with zeroes
967 Cost += getShuffleCost(TargetTransformInfo::SK_InsertSubvector,
968 NewMaskTy, 0, MaskTy);
970 if (!ST->hasAVX512())
971 return Cost + LT.first*4; // Each maskmov costs 4
973 // AVX-512 masked load/store is cheapper
974 return Cost+LT.first;
977 unsigned X86TTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
978 // Address computations in vectorized code with non-consecutive addresses will
979 // likely result in more instructions compared to scalar code where the
980 // computation can more often be merged into the index mode. The resulting
981 // extra micro-ops can significantly decrease throughput.
982 unsigned NumVectorInstToHideOverhead = 10;
984 if (Ty->isVectorTy() && IsComplex)
985 return NumVectorInstToHideOverhead;
987 return TargetTransformInfo::getAddressComputationCost(Ty, IsComplex);
990 unsigned X86TTI::getReductionCost(unsigned Opcode, Type *ValTy,
991 bool IsPairwise) const {
993 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
997 int ISD = TLI->InstructionOpcodeToISD(Opcode);
998 assert(ISD && "Invalid opcode");
1000 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
1001 // and make it as the cost.
1003 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = {
1004 { ISD::FADD, MVT::v2f64, 2 },
1005 { ISD::FADD, MVT::v4f32, 4 },
1006 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
1007 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
1008 { ISD::ADD, MVT::v8i16, 5 },
1011 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = {
1012 { ISD::FADD, MVT::v4f32, 4 },
1013 { ISD::FADD, MVT::v4f64, 5 },
1014 { ISD::FADD, MVT::v8f32, 7 },
1015 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
1016 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
1017 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8".
1018 { ISD::ADD, MVT::v8i16, 5 },
1019 { ISD::ADD, MVT::v8i32, 5 },
1022 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = {
1023 { ISD::FADD, MVT::v2f64, 2 },
1024 { ISD::FADD, MVT::v4f32, 4 },
1025 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
1026 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3".
1027 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3".
1030 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = {
1031 { ISD::FADD, MVT::v4f32, 3 },
1032 { ISD::FADD, MVT::v4f64, 3 },
1033 { ISD::FADD, MVT::v8f32, 4 },
1034 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
1035 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8".
1036 { ISD::ADD, MVT::v4i64, 3 },
1037 { ISD::ADD, MVT::v8i16, 4 },
1038 { ISD::ADD, MVT::v8i32, 5 },
1043 int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy);
1045 return LT.first * AVX1CostTblPairWise[Idx].Cost;
1048 if (ST->hasSSE42()) {
1049 int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy);
1051 return LT.first * SSE42CostTblPairWise[Idx].Cost;
1055 int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy);
1057 return LT.first * AVX1CostTblNoPairWise[Idx].Cost;
1060 if (ST->hasSSE42()) {
1061 int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy);
1063 return LT.first * SSE42CostTblNoPairWise[Idx].Cost;
1067 return TargetTransformInfo::getReductionCost(Opcode, ValTy, IsPairwise);
1070 /// \brief Calculate the cost of materializing a 64-bit value. This helper
1071 /// method might only calculate a fraction of a larger immediate. Therefore it
1072 /// is valid to return a cost of ZERO.
1073 unsigned X86TTI::getIntImmCost(int64_t Val) const {
1080 return 2 * TCC_Basic;
1083 unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
1084 assert(Ty->isIntegerTy());
1086 unsigned BitSize = Ty->getPrimitiveSizeInBits();
1090 // Never hoist constants larger than 128bit, because this might lead to
1091 // incorrect code generation or assertions in codegen.
1092 // Fixme: Create a cost model for types larger than i128 once the codegen
1093 // issues have been fixed.
1100 // Sign-extend all constants to a multiple of 64-bit.
1103 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU);
1105 // Split the constant into 64-bit chunks and calculate the cost for each
1108 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
1109 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
1110 int64_t Val = Tmp.getSExtValue();
1111 Cost += getIntImmCost(Val);
1113 // We need at least one instruction to materialze the constant.
1114 return std::max(1U, Cost);
1117 unsigned X86TTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
1119 assert(Ty->isIntegerTy());
1121 unsigned BitSize = Ty->getPrimitiveSizeInBits();
1122 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1123 // here, so that constant hoisting will ignore this constant.
1127 unsigned ImmIdx = ~0U;
1129 default: return TCC_Free;
1130 case Instruction::GetElementPtr:
1131 // Always hoist the base address of a GetElementPtr. This prevents the
1132 // creation of new constants for every base constant that gets constant
1133 // folded with the offset.
1135 return 2 * TCC_Basic;
1137 case Instruction::Store:
1140 case Instruction::Add:
1141 case Instruction::Sub:
1142 case Instruction::Mul:
1143 case Instruction::UDiv:
1144 case Instruction::SDiv:
1145 case Instruction::URem:
1146 case Instruction::SRem:
1147 case Instruction::And:
1148 case Instruction::Or:
1149 case Instruction::Xor:
1150 case Instruction::ICmp:
1153 // Always return TCC_Free for the shift value of a shift instruction.
1154 case Instruction::Shl:
1155 case Instruction::LShr:
1156 case Instruction::AShr:
1160 case Instruction::Trunc:
1161 case Instruction::ZExt:
1162 case Instruction::SExt:
1163 case Instruction::IntToPtr:
1164 case Instruction::PtrToInt:
1165 case Instruction::BitCast:
1166 case Instruction::PHI:
1167 case Instruction::Call:
1168 case Instruction::Select:
1169 case Instruction::Ret:
1170 case Instruction::Load:
1174 if (Idx == ImmIdx) {
1175 unsigned NumConstants = (BitSize + 63) / 64;
1176 unsigned Cost = X86TTI::getIntImmCost(Imm, Ty);
1177 return (Cost <= NumConstants * TCC_Basic)
1178 ? static_cast<unsigned>(TCC_Free)
1182 return X86TTI::getIntImmCost(Imm, Ty);
1185 unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
1186 const APInt &Imm, Type *Ty) const {
1187 assert(Ty->isIntegerTy());
1189 unsigned BitSize = Ty->getPrimitiveSizeInBits();
1190 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1191 // here, so that constant hoisting will ignore this constant.
1196 default: return TCC_Free;
1197 case Intrinsic::sadd_with_overflow:
1198 case Intrinsic::uadd_with_overflow:
1199 case Intrinsic::ssub_with_overflow:
1200 case Intrinsic::usub_with_overflow:
1201 case Intrinsic::smul_with_overflow:
1202 case Intrinsic::umul_with_overflow:
1203 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
1206 case Intrinsic::experimental_stackmap:
1207 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
1210 case Intrinsic::experimental_patchpoint_void:
1211 case Intrinsic::experimental_patchpoint_i64:
1212 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
1216 return X86TTI::getIntImmCost(Imm, Ty);
1219 bool X86TTI::isLegalMaskedLoad(Type *DataTy, int Consecutive) const {
1220 int DataWidth = DataTy->getPrimitiveSizeInBits();
1222 // Todo: AVX512 allows gather/scatter, works with strided and random as well
1223 if ((DataWidth < 32) || (Consecutive == 0))
1225 if (ST->hasAVX512() || ST->hasAVX2())
1230 bool X86TTI::isLegalMaskedStore(Type *DataType, int Consecutive) const {
1231 return isLegalMaskedLoad(DataType, Consecutive);