1664e354dSChandler Carruth //===- BasicTargetTransformInfo.cpp - Basic target-independent TTI impl ---===// 2664e354dSChandler Carruth // 3664e354dSChandler Carruth // The LLVM Compiler Infrastructure 4664e354dSChandler Carruth // 5664e354dSChandler Carruth // This file is distributed under the University of Illinois Open Source 6664e354dSChandler Carruth // License. See LICENSE.TXT for details. 7664e354dSChandler Carruth // 8664e354dSChandler Carruth //===----------------------------------------------------------------------===// 9664e354dSChandler Carruth /// \file 10664e354dSChandler Carruth /// This file provides the implementation of a basic TargetTransformInfo pass 11664e354dSChandler Carruth /// predicated on the target abstractions present in the target independent 12664e354dSChandler Carruth /// code generator. It uses these (primarily TargetLowering) to model as much 13664e354dSChandler Carruth /// of the TTI query interface as possible. It is included by most targets so 14664e354dSChandler Carruth /// that they can specialize only a small subset of the query space. 15664e354dSChandler Carruth /// 16664e354dSChandler Carruth //===----------------------------------------------------------------------===// 17664e354dSChandler Carruth 18664e354dSChandler Carruth #define DEBUG_TYPE "basictti" 19664e354dSChandler Carruth #include "llvm/CodeGen/Passes.h" 20d3e73556SChandler Carruth #include "llvm/Analysis/TargetTransformInfo.h" 21664e354dSChandler Carruth #include "llvm/Target/TargetLowering.h" 22664e354dSChandler Carruth #include <utility> 23664e354dSChandler Carruth using namespace llvm; 24664e354dSChandler Carruth 25664e354dSChandler Carruth namespace { 26664e354dSChandler Carruth 2777dfe45fSCraig Topper class BasicTTI final : public ImmutablePass, public TargetTransformInfo { 28afc1036fSBill Wendling const TargetMachine *TM; 29664e354dSChandler Carruth 30664e354dSChandler Carruth /// Estimate the overhead of scalarizing an instruction. Insert and Extract 31664e354dSChandler Carruth /// are set if the result needs to be inserted and/or extracted from vectors. 32664e354dSChandler Carruth unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const; 33664e354dSChandler Carruth 34afc1036fSBill Wendling const TargetLoweringBase *getTLI() const { return TM->getTargetLowering(); } 35afc1036fSBill Wendling 36664e354dSChandler Carruth public: 37c0196b1bSCraig Topper BasicTTI() : ImmutablePass(ID), TM(nullptr) { 38664e354dSChandler Carruth llvm_unreachable("This pass cannot be directly constructed"); 39664e354dSChandler Carruth } 40664e354dSChandler Carruth 41afc1036fSBill Wendling BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) { 42664e354dSChandler Carruth initializeBasicTTIPass(*PassRegistry::getPassRegistry()); 43664e354dSChandler Carruth } 44664e354dSChandler Carruth 4524e685fdSCraig Topper void initializePass() override { 46664e354dSChandler Carruth pushTTIStack(this); 47664e354dSChandler Carruth } 48664e354dSChandler Carruth 4924e685fdSCraig Topper void getAnalysisUsage(AnalysisUsage &AU) const override { 50664e354dSChandler Carruth TargetTransformInfo::getAnalysisUsage(AU); 51664e354dSChandler Carruth } 52664e354dSChandler Carruth 53664e354dSChandler Carruth /// Pass identification. 54664e354dSChandler Carruth static char ID; 55664e354dSChandler Carruth 56664e354dSChandler Carruth /// Provide necessary pointer adjustments for the two base classes. 5724e685fdSCraig Topper void *getAdjustedAnalysisPointer(const void *ID) override { 58664e354dSChandler Carruth if (ID == &TargetTransformInfo::ID) 59664e354dSChandler Carruth return (TargetTransformInfo*)this; 60664e354dSChandler Carruth return this; 61664e354dSChandler Carruth } 62664e354dSChandler Carruth 6324e685fdSCraig Topper bool hasBranchDivergence() const override; 648b1e021eSTom Stellard 65664e354dSChandler Carruth /// \name Scalar TTI Implementations 66664e354dSChandler Carruth /// @{ 67664e354dSChandler Carruth 6824e685fdSCraig Topper bool isLegalAddImmediate(int64_t imm) const override; 6924e685fdSCraig Topper bool isLegalICmpImmediate(int64_t imm) const override; 7024e685fdSCraig Topper bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 71664e354dSChandler Carruth int64_t BaseOffset, bool HasBaseReg, 7273156025SCraig Topper int64_t Scale) const override; 7324e685fdSCraig Topper int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, 74bf490d4aSQuentin Colombet int64_t BaseOffset, bool HasBaseReg, 7573156025SCraig Topper int64_t Scale) const override; 7624e685fdSCraig Topper bool isTruncateFree(Type *Ty1, Type *Ty2) const override; 7724e685fdSCraig Topper bool isTypeLegal(Type *Ty) const override; 7824e685fdSCraig Topper unsigned getJumpBufAlignment() const override; 7924e685fdSCraig Topper unsigned getJumpBufSize() const override; 8024e685fdSCraig Topper bool shouldBuildLookupTables() const override; 8124e685fdSCraig Topper bool haveFastSqrt(Type *Ty) const override; 8224e685fdSCraig Topper void getUnrollingPreferences(Loop *L, 8324e685fdSCraig Topper UnrollingPreferences &UP) const override; 84664e354dSChandler Carruth 85664e354dSChandler Carruth /// @} 86664e354dSChandler Carruth 87664e354dSChandler Carruth /// \name Vector TTI Implementations 88664e354dSChandler Carruth /// @{ 89664e354dSChandler Carruth 9024e685fdSCraig Topper unsigned getNumberOfRegisters(bool Vector) const override; 9124e685fdSCraig Topper unsigned getMaximumUnrollFactor() const override; 9224e685fdSCraig Topper unsigned getRegisterBitWidth(bool Vector) const override; 9324e685fdSCraig Topper unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind, 9473156025SCraig Topper OperandValueKind) const override; 9524e685fdSCraig Topper unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, 9673156025SCraig Topper int Index, Type *SubTp) const override; 9724e685fdSCraig Topper unsigned getCastInstrCost(unsigned Opcode, Type *Dst, 9873156025SCraig Topper Type *Src) const override; 9924e685fdSCraig Topper unsigned getCFInstrCost(unsigned Opcode) const override; 10024e685fdSCraig Topper unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 10173156025SCraig Topper Type *CondTy) const override; 10224e685fdSCraig Topper unsigned getVectorInstrCost(unsigned Opcode, Type *Val, 10373156025SCraig Topper unsigned Index) const override; 10424e685fdSCraig Topper unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, 10573156025SCraig Topper unsigned AddressSpace) const override; 10624e685fdSCraig Topper unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy, 10724e685fdSCraig Topper ArrayRef<Type*> Tys) const override; 10824e685fdSCraig Topper unsigned getNumberOfParts(Type *Tp) const override; 10924e685fdSCraig Topper unsigned getAddressComputationCost( Type *Ty, bool IsComplex) const override; 11024e685fdSCraig Topper unsigned getReductionCost(unsigned Opcode, Type *Ty, 11173156025SCraig Topper bool IsPairwise) const override; 112664e354dSChandler Carruth 113664e354dSChandler Carruth /// @} 114664e354dSChandler Carruth }; 115664e354dSChandler Carruth 116664e354dSChandler Carruth } 117664e354dSChandler Carruth 118664e354dSChandler Carruth INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti", 119664e354dSChandler Carruth "Target independent code generator's TTI", true, true, false) 120664e354dSChandler Carruth char BasicTTI::ID = 0; 121664e354dSChandler Carruth 122664e354dSChandler Carruth ImmutablePass * 123afc1036fSBill Wendling llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) { 124afc1036fSBill Wendling return new BasicTTI(TM); 125664e354dSChandler Carruth } 126664e354dSChandler Carruth 1278b1e021eSTom Stellard bool BasicTTI::hasBranchDivergence() const { return false; } 128664e354dSChandler Carruth 129664e354dSChandler Carruth bool BasicTTI::isLegalAddImmediate(int64_t imm) const { 130afc1036fSBill Wendling return getTLI()->isLegalAddImmediate(imm); 131664e354dSChandler Carruth } 132664e354dSChandler Carruth 133664e354dSChandler Carruth bool BasicTTI::isLegalICmpImmediate(int64_t imm) const { 134afc1036fSBill Wendling return getTLI()->isLegalICmpImmediate(imm); 135664e354dSChandler Carruth } 136664e354dSChandler Carruth 137664e354dSChandler Carruth bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 138664e354dSChandler Carruth int64_t BaseOffset, bool HasBaseReg, 139664e354dSChandler Carruth int64_t Scale) const { 14056b31bd9SBenjamin Kramer TargetLoweringBase::AddrMode AM; 141664e354dSChandler Carruth AM.BaseGV = BaseGV; 142664e354dSChandler Carruth AM.BaseOffs = BaseOffset; 143664e354dSChandler Carruth AM.HasBaseReg = HasBaseReg; 144664e354dSChandler Carruth AM.Scale = Scale; 145afc1036fSBill Wendling return getTLI()->isLegalAddressingMode(AM, Ty); 146664e354dSChandler Carruth } 147664e354dSChandler Carruth 148bf490d4aSQuentin Colombet int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, 149bf490d4aSQuentin Colombet int64_t BaseOffset, bool HasBaseReg, 150bf490d4aSQuentin Colombet int64_t Scale) const { 151bf490d4aSQuentin Colombet TargetLoweringBase::AddrMode AM; 152bf490d4aSQuentin Colombet AM.BaseGV = BaseGV; 153bf490d4aSQuentin Colombet AM.BaseOffs = BaseOffset; 154bf490d4aSQuentin Colombet AM.HasBaseReg = HasBaseReg; 155bf490d4aSQuentin Colombet AM.Scale = Scale; 156afc1036fSBill Wendling return getTLI()->getScalingFactorCost(AM, Ty); 157bf490d4aSQuentin Colombet } 158bf490d4aSQuentin Colombet 159664e354dSChandler Carruth bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const { 160afc1036fSBill Wendling return getTLI()->isTruncateFree(Ty1, Ty2); 161664e354dSChandler Carruth } 162664e354dSChandler Carruth 163664e354dSChandler Carruth bool BasicTTI::isTypeLegal(Type *Ty) const { 164afc1036fSBill Wendling EVT T = getTLI()->getValueType(Ty); 165afc1036fSBill Wendling return getTLI()->isTypeLegal(T); 166664e354dSChandler Carruth } 167664e354dSChandler Carruth 168664e354dSChandler Carruth unsigned BasicTTI::getJumpBufAlignment() const { 169afc1036fSBill Wendling return getTLI()->getJumpBufAlignment(); 170664e354dSChandler Carruth } 171664e354dSChandler Carruth 172664e354dSChandler Carruth unsigned BasicTTI::getJumpBufSize() const { 173afc1036fSBill Wendling return getTLI()->getJumpBufSize(); 174664e354dSChandler Carruth } 175664e354dSChandler Carruth 176664e354dSChandler Carruth bool BasicTTI::shouldBuildLookupTables() const { 177afc1036fSBill Wendling const TargetLoweringBase *TLI = getTLI(); 178664e354dSChandler Carruth return TLI->supportJumpTables() && 179664e354dSChandler Carruth (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || 180664e354dSChandler Carruth TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); 181664e354dSChandler Carruth } 182664e354dSChandler Carruth 18337cd6cfbSRichard Sandiford bool BasicTTI::haveFastSqrt(Type *Ty) const { 18437cd6cfbSRichard Sandiford const TargetLoweringBase *TLI = getTLI(); 18537cd6cfbSRichard Sandiford EVT VT = TLI->getValueType(Ty); 18637cd6cfbSRichard Sandiford return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT); 18737cd6cfbSRichard Sandiford } 18837cd6cfbSRichard Sandiford 1898f2e7005SHal Finkel void BasicTTI::getUnrollingPreferences(Loop *, UnrollingPreferences &) const { } 1908f2e7005SHal Finkel 191664e354dSChandler Carruth //===----------------------------------------------------------------------===// 192664e354dSChandler Carruth // 193664e354dSChandler Carruth // Calls used by the vectorizers. 194664e354dSChandler Carruth // 195664e354dSChandler Carruth //===----------------------------------------------------------------------===// 196664e354dSChandler Carruth 197664e354dSChandler Carruth unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert, 198664e354dSChandler Carruth bool Extract) const { 199664e354dSChandler Carruth assert (Ty->isVectorTy() && "Can only scalarize vectors"); 200664e354dSChandler Carruth unsigned Cost = 0; 201664e354dSChandler Carruth 202664e354dSChandler Carruth for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { 203664e354dSChandler Carruth if (Insert) 204664e354dSChandler Carruth Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i); 205664e354dSChandler Carruth if (Extract) 206664e354dSChandler Carruth Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i); 207664e354dSChandler Carruth } 208664e354dSChandler Carruth 209664e354dSChandler Carruth return Cost; 210664e354dSChandler Carruth } 211664e354dSChandler Carruth 212664e354dSChandler Carruth unsigned BasicTTI::getNumberOfRegisters(bool Vector) const { 213664e354dSChandler Carruth return 1; 214664e354dSChandler Carruth } 215664e354dSChandler Carruth 216b1791a75SNadav Rotem unsigned BasicTTI::getRegisterBitWidth(bool Vector) const { 217b1791a75SNadav Rotem return 32; 218b1791a75SNadav Rotem } 219b1791a75SNadav Rotem 220b696c36fSNadav Rotem unsigned BasicTTI::getMaximumUnrollFactor() const { 221b696c36fSNadav Rotem return 1; 222b696c36fSNadav Rotem } 223b696c36fSNadav Rotem 224b9773871SArnold Schwaighofer unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty, 225b9773871SArnold Schwaighofer OperandValueKind, 226b9773871SArnold Schwaighofer OperandValueKind) const { 227664e354dSChandler Carruth // Check if any of the operands are vector operands. 228afc1036fSBill Wendling const TargetLoweringBase *TLI = getTLI(); 229664e354dSChandler Carruth int ISD = TLI->InstructionOpcodeToISD(Opcode); 230664e354dSChandler Carruth assert(ISD && "Invalid opcode"); 231664e354dSChandler Carruth 232664e354dSChandler Carruth std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty); 233664e354dSChandler Carruth 23487a0af6eSNadav Rotem bool IsFloat = Ty->getScalarType()->isFloatingPointTy(); 2350db0690aSNadav Rotem // Assume that floating point arithmetic operations cost twice as much as 2360db0690aSNadav Rotem // integer operations. 23787a0af6eSNadav Rotem unsigned OpCost = (IsFloat ? 2 : 1); 23887a0af6eSNadav Rotem 239664e354dSChandler Carruth if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { 240664e354dSChandler Carruth // The operation is legal. Assume it costs 1. 2410db0690aSNadav Rotem // If the type is split to multiple registers, assume that there is some 242664e354dSChandler Carruth // overhead to this. 243664e354dSChandler Carruth // TODO: Once we have extract/insert subvector cost we need to use them. 244664e354dSChandler Carruth if (LT.first > 1) 24587a0af6eSNadav Rotem return LT.first * 2 * OpCost; 24687a0af6eSNadav Rotem return LT.first * 1 * OpCost; 247664e354dSChandler Carruth } 248664e354dSChandler Carruth 249664e354dSChandler Carruth if (!TLI->isOperationExpand(ISD, LT.second)) { 250664e354dSChandler Carruth // If the operation is custom lowered then assume 251664e354dSChandler Carruth // thare the code is twice as expensive. 25287a0af6eSNadav Rotem return LT.first * 2 * OpCost; 253664e354dSChandler Carruth } 254664e354dSChandler Carruth 255664e354dSChandler Carruth // Else, assume that we need to scalarize this op. 256664e354dSChandler Carruth if (Ty->isVectorTy()) { 257664e354dSChandler Carruth unsigned Num = Ty->getVectorNumElements(); 258664e354dSChandler Carruth unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType()); 259664e354dSChandler Carruth // return the cost of multiple scalar invocation plus the cost of inserting 260664e354dSChandler Carruth // and extracting the values. 261664e354dSChandler Carruth return getScalarizationOverhead(Ty, true, true) + Num * Cost; 262664e354dSChandler Carruth } 263664e354dSChandler Carruth 264664e354dSChandler Carruth // We don't know anything about this scalar instruction. 26587a0af6eSNadav Rotem return OpCost; 266664e354dSChandler Carruth } 267664e354dSChandler Carruth 268664e354dSChandler Carruth unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index, 269664e354dSChandler Carruth Type *SubTp) const { 270664e354dSChandler Carruth return 1; 271664e354dSChandler Carruth } 272664e354dSChandler Carruth 273664e354dSChandler Carruth unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst, 274664e354dSChandler Carruth Type *Src) const { 275afc1036fSBill Wendling const TargetLoweringBase *TLI = getTLI(); 276664e354dSChandler Carruth int ISD = TLI->InstructionOpcodeToISD(Opcode); 277664e354dSChandler Carruth assert(ISD && "Invalid opcode"); 278664e354dSChandler Carruth 279664e354dSChandler Carruth std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src); 280664e354dSChandler Carruth std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst); 281664e354dSChandler Carruth 282e55aa3c8SNadav Rotem // Check for NOOP conversions. 283e55aa3c8SNadav Rotem if (SrcLT.first == DstLT.first && 284e55aa3c8SNadav Rotem SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { 285664e354dSChandler Carruth 286e55aa3c8SNadav Rotem // Bitcast between types that are legalized to the same type are free. 287e55aa3c8SNadav Rotem if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc) 288664e354dSChandler Carruth return 0; 289e55aa3c8SNadav Rotem } 290664e354dSChandler Carruth 291664e354dSChandler Carruth if (Opcode == Instruction::Trunc && 292664e354dSChandler Carruth TLI->isTruncateFree(SrcLT.second, DstLT.second)) 293664e354dSChandler Carruth return 0; 294664e354dSChandler Carruth 295664e354dSChandler Carruth if (Opcode == Instruction::ZExt && 296664e354dSChandler Carruth TLI->isZExtFree(SrcLT.second, DstLT.second)) 297664e354dSChandler Carruth return 0; 298664e354dSChandler Carruth 299e55aa3c8SNadav Rotem // If the cast is marked as legal (or promote) then assume low cost. 30055312debSHal Finkel if (SrcLT.first == DstLT.first && 30155312debSHal Finkel TLI->isOperationLegalOrPromote(ISD, DstLT.second)) 302e55aa3c8SNadav Rotem return 1; 303e55aa3c8SNadav Rotem 304e55aa3c8SNadav Rotem // Handle scalar conversions. 305e55aa3c8SNadav Rotem if (!Src->isVectorTy() && !Dst->isVectorTy()) { 306e55aa3c8SNadav Rotem 307e55aa3c8SNadav Rotem // Scalar bitcasts are usually free. 308e55aa3c8SNadav Rotem if (Opcode == Instruction::BitCast) 309e55aa3c8SNadav Rotem return 0; 310e55aa3c8SNadav Rotem 311664e354dSChandler Carruth // Just check the op cost. If the operation is legal then assume it costs 1. 312664e354dSChandler Carruth if (!TLI->isOperationExpand(ISD, DstLT.second)) 313664e354dSChandler Carruth return 1; 314664e354dSChandler Carruth 315664e354dSChandler Carruth // Assume that illegal scalar instruction are expensive. 316664e354dSChandler Carruth return 4; 317664e354dSChandler Carruth } 318664e354dSChandler Carruth 319664e354dSChandler Carruth // Check vector-to-vector casts. 320664e354dSChandler Carruth if (Dst->isVectorTy() && Src->isVectorTy()) { 321664e354dSChandler Carruth 322664e354dSChandler Carruth // If the cast is between same-sized registers, then the check is simple. 323664e354dSChandler Carruth if (SrcLT.first == DstLT.first && 324664e354dSChandler Carruth SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { 325664e354dSChandler Carruth 326664e354dSChandler Carruth // Assume that Zext is done using AND. 327664e354dSChandler Carruth if (Opcode == Instruction::ZExt) 328664e354dSChandler Carruth return 1; 329664e354dSChandler Carruth 330664e354dSChandler Carruth // Assume that sext is done using SHL and SRA. 331664e354dSChandler Carruth if (Opcode == Instruction::SExt) 332664e354dSChandler Carruth return 2; 333664e354dSChandler Carruth 334664e354dSChandler Carruth // Just check the op cost. If the operation is legal then assume it costs 335664e354dSChandler Carruth // 1 and multiply by the type-legalization overhead. 336664e354dSChandler Carruth if (!TLI->isOperationExpand(ISD, DstLT.second)) 337664e354dSChandler Carruth return SrcLT.first * 1; 338664e354dSChandler Carruth } 339664e354dSChandler Carruth 340664e354dSChandler Carruth // If we are converting vectors and the operation is illegal, or 341664e354dSChandler Carruth // if the vectors are legalized to different types, estimate the 342664e354dSChandler Carruth // scalarization costs. 343664e354dSChandler Carruth unsigned Num = Dst->getVectorNumElements(); 344664e354dSChandler Carruth unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(), 345664e354dSChandler Carruth Src->getScalarType()); 346664e354dSChandler Carruth 347664e354dSChandler Carruth // Return the cost of multiple scalar invocation plus the cost of 348664e354dSChandler Carruth // inserting and extracting the values. 349664e354dSChandler Carruth return getScalarizationOverhead(Dst, true, true) + Num * Cost; 350664e354dSChandler Carruth } 351664e354dSChandler Carruth 352664e354dSChandler Carruth // We already handled vector-to-vector and scalar-to-scalar conversions. This 353664e354dSChandler Carruth // is where we handle bitcast between vectors and scalars. We need to assume 354664e354dSChandler Carruth // that the conversion is scalarized in one way or another. 355664e354dSChandler Carruth if (Opcode == Instruction::BitCast) 356664e354dSChandler Carruth // Illegal bitcasts are done by storing and loading from a stack slot. 357664e354dSChandler Carruth return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) + 358664e354dSChandler Carruth (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0); 359664e354dSChandler Carruth 360664e354dSChandler Carruth llvm_unreachable("Unhandled cast"); 361664e354dSChandler Carruth } 362664e354dSChandler Carruth 363664e354dSChandler Carruth unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const { 364664e354dSChandler Carruth // Branches are assumed to be predicted. 365664e354dSChandler Carruth return 0; 366664e354dSChandler Carruth } 367664e354dSChandler Carruth 368664e354dSChandler Carruth unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 369664e354dSChandler Carruth Type *CondTy) const { 370afc1036fSBill Wendling const TargetLoweringBase *TLI = getTLI(); 371664e354dSChandler Carruth int ISD = TLI->InstructionOpcodeToISD(Opcode); 372664e354dSChandler Carruth assert(ISD && "Invalid opcode"); 373664e354dSChandler Carruth 374664e354dSChandler Carruth // Selects on vectors are actually vector selects. 375664e354dSChandler Carruth if (ISD == ISD::SELECT) { 376664e354dSChandler Carruth assert(CondTy && "CondTy must exist"); 377664e354dSChandler Carruth if (CondTy->isVectorTy()) 378664e354dSChandler Carruth ISD = ISD::VSELECT; 379664e354dSChandler Carruth } 380664e354dSChandler Carruth 381664e354dSChandler Carruth std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy); 382664e354dSChandler Carruth 383664e354dSChandler Carruth if (!TLI->isOperationExpand(ISD, LT.second)) { 384664e354dSChandler Carruth // The operation is legal. Assume it costs 1. Multiply 385664e354dSChandler Carruth // by the type-legalization overhead. 386664e354dSChandler Carruth return LT.first * 1; 387664e354dSChandler Carruth } 388664e354dSChandler Carruth 389664e354dSChandler Carruth // Otherwise, assume that the cast is scalarized. 390664e354dSChandler Carruth if (ValTy->isVectorTy()) { 391664e354dSChandler Carruth unsigned Num = ValTy->getVectorNumElements(); 392664e354dSChandler Carruth if (CondTy) 393664e354dSChandler Carruth CondTy = CondTy->getScalarType(); 394664e354dSChandler Carruth unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(), 395664e354dSChandler Carruth CondTy); 396664e354dSChandler Carruth 397664e354dSChandler Carruth // Return the cost of multiple scalar invocation plus the cost of inserting 398664e354dSChandler Carruth // and extracting the values. 399664e354dSChandler Carruth return getScalarizationOverhead(ValTy, true, false) + Num * Cost; 400664e354dSChandler Carruth } 401664e354dSChandler Carruth 402664e354dSChandler Carruth // Unknown scalar opcode. 403664e354dSChandler Carruth return 1; 404664e354dSChandler Carruth } 405664e354dSChandler Carruth 406664e354dSChandler Carruth unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val, 407664e354dSChandler Carruth unsigned Index) const { 408ce376c0fSRaul E. Silvera std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Val->getScalarType()); 409ce376c0fSRaul E. Silvera 410ce376c0fSRaul E. Silvera return LT.first; 411664e354dSChandler Carruth } 412664e354dSChandler Carruth 413664e354dSChandler Carruth unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src, 414664e354dSChandler Carruth unsigned Alignment, 415664e354dSChandler Carruth unsigned AddressSpace) const { 416664e354dSChandler Carruth assert(!Src->isVoidTy() && "Invalid type"); 417afc1036fSBill Wendling std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src); 418664e354dSChandler Carruth 4196fd19ab3SHal Finkel // Assuming that all loads of legal types cost 1. 4206fd19ab3SHal Finkel unsigned Cost = LT.first; 4216fd19ab3SHal Finkel 4226fd19ab3SHal Finkel if (Src->isVectorTy() && 4236fd19ab3SHal Finkel Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) { 4246fd19ab3SHal Finkel // This is a vector load that legalizes to a larger type than the vector 4256fd19ab3SHal Finkel // itself. Unless the corresponding extending load or truncating store is 4266fd19ab3SHal Finkel // legal, then this will scalarize. 427*56bf297eSHal Finkel TargetLowering::LegalizeAction LA = TargetLowering::Expand; 428*56bf297eSHal Finkel EVT MemVT = getTLI()->getValueType(Src, true); 429*56bf297eSHal Finkel if (MemVT.isSimple() && MemVT != MVT::Other) { 4306fd19ab3SHal Finkel if (Opcode == Instruction::Store) 431*56bf297eSHal Finkel LA = getTLI()->getTruncStoreAction(LT.second, MemVT.getSimpleVT()); 4326fd19ab3SHal Finkel else 433*56bf297eSHal Finkel LA = getTLI()->getLoadExtAction(ISD::EXTLOAD, MemVT.getSimpleVT()); 434*56bf297eSHal Finkel } 4356fd19ab3SHal Finkel 4366fd19ab3SHal Finkel if (LA != TargetLowering::Legal && LA != TargetLowering::Custom) { 4376fd19ab3SHal Finkel // This is a vector load/store for some illegal type that is scalarized. 4386fd19ab3SHal Finkel // We must account for the cost of building or decomposing the vector. 4396fd19ab3SHal Finkel Cost += getScalarizationOverhead(Src, Opcode != Instruction::Store, 4406fd19ab3SHal Finkel Opcode == Instruction::Store); 4416fd19ab3SHal Finkel } 4426fd19ab3SHal Finkel } 4436fd19ab3SHal Finkel 4446fd19ab3SHal Finkel return Cost; 445664e354dSChandler Carruth } 446664e354dSChandler Carruth 447f7cfac7aSBenjamin Kramer unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, 448664e354dSChandler Carruth ArrayRef<Type *> Tys) const { 449f7cfac7aSBenjamin Kramer unsigned ISD = 0; 450f7cfac7aSBenjamin Kramer switch (IID) { 451f7cfac7aSBenjamin Kramer default: { 452f7cfac7aSBenjamin Kramer // Assume that we need to scalarize this intrinsic. 453664e354dSChandler Carruth unsigned ScalarizationCost = 0; 454664e354dSChandler Carruth unsigned ScalarCalls = 1; 455664e354dSChandler Carruth if (RetTy->isVectorTy()) { 456664e354dSChandler Carruth ScalarizationCost = getScalarizationOverhead(RetTy, true, false); 457664e354dSChandler Carruth ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); 458664e354dSChandler Carruth } 459664e354dSChandler Carruth for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) { 460664e354dSChandler Carruth if (Tys[i]->isVectorTy()) { 461664e354dSChandler Carruth ScalarizationCost += getScalarizationOverhead(Tys[i], false, true); 462664e354dSChandler Carruth ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); 463664e354dSChandler Carruth } 464664e354dSChandler Carruth } 465f7cfac7aSBenjamin Kramer 466664e354dSChandler Carruth return ScalarCalls + ScalarizationCost; 467664e354dSChandler Carruth } 468f7cfac7aSBenjamin Kramer // Look for intrinsics that can be lowered directly or turned into a scalar 469f7cfac7aSBenjamin Kramer // intrinsic call. 470f7cfac7aSBenjamin Kramer case Intrinsic::sqrt: ISD = ISD::FSQRT; break; 471f7cfac7aSBenjamin Kramer case Intrinsic::sin: ISD = ISD::FSIN; break; 472f7cfac7aSBenjamin Kramer case Intrinsic::cos: ISD = ISD::FCOS; break; 473f7cfac7aSBenjamin Kramer case Intrinsic::exp: ISD = ISD::FEXP; break; 474f7cfac7aSBenjamin Kramer case Intrinsic::exp2: ISD = ISD::FEXP2; break; 475f7cfac7aSBenjamin Kramer case Intrinsic::log: ISD = ISD::FLOG; break; 476f7cfac7aSBenjamin Kramer case Intrinsic::log10: ISD = ISD::FLOG10; break; 477f7cfac7aSBenjamin Kramer case Intrinsic::log2: ISD = ISD::FLOG2; break; 478f7cfac7aSBenjamin Kramer case Intrinsic::fabs: ISD = ISD::FABS; break; 4790c5c01aaSHal Finkel case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break; 480f7cfac7aSBenjamin Kramer case Intrinsic::floor: ISD = ISD::FFLOOR; break; 481f7cfac7aSBenjamin Kramer case Intrinsic::ceil: ISD = ISD::FCEIL; break; 482f7cfac7aSBenjamin Kramer case Intrinsic::trunc: ISD = ISD::FTRUNC; break; 483ec474f28SHal Finkel case Intrinsic::nearbyint: 484ec474f28SHal Finkel ISD = ISD::FNEARBYINT; break; 485f7cfac7aSBenjamin Kramer case Intrinsic::rint: ISD = ISD::FRINT; break; 486171817eeSHal Finkel case Intrinsic::round: ISD = ISD::FROUND; break; 487f7cfac7aSBenjamin Kramer case Intrinsic::pow: ISD = ISD::FPOW; break; 488f7cfac7aSBenjamin Kramer case Intrinsic::fma: ISD = ISD::FMA; break; 489f7cfac7aSBenjamin Kramer case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add? 490a7cd6bf3SArnold Schwaighofer case Intrinsic::lifetime_start: 491a7cd6bf3SArnold Schwaighofer case Intrinsic::lifetime_end: 492a7cd6bf3SArnold Schwaighofer return 0; 493f7cfac7aSBenjamin Kramer } 494f7cfac7aSBenjamin Kramer 495afc1036fSBill Wendling const TargetLoweringBase *TLI = getTLI(); 496f7cfac7aSBenjamin Kramer std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy); 497f7cfac7aSBenjamin Kramer 498f7cfac7aSBenjamin Kramer if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { 499f7cfac7aSBenjamin Kramer // The operation is legal. Assume it costs 1. 500f7cfac7aSBenjamin Kramer // If the type is split to multiple registers, assume that thre is some 501f7cfac7aSBenjamin Kramer // overhead to this. 502f7cfac7aSBenjamin Kramer // TODO: Once we have extract/insert subvector cost we need to use them. 503f7cfac7aSBenjamin Kramer if (LT.first > 1) 504f7cfac7aSBenjamin Kramer return LT.first * 2; 505f7cfac7aSBenjamin Kramer return LT.first * 1; 506f7cfac7aSBenjamin Kramer } 507f7cfac7aSBenjamin Kramer 508f7cfac7aSBenjamin Kramer if (!TLI->isOperationExpand(ISD, LT.second)) { 509f7cfac7aSBenjamin Kramer // If the operation is custom lowered then assume 510f7cfac7aSBenjamin Kramer // thare the code is twice as expensive. 511f7cfac7aSBenjamin Kramer return LT.first * 2; 512f7cfac7aSBenjamin Kramer } 513f7cfac7aSBenjamin Kramer 514f7cfac7aSBenjamin Kramer // Else, assume that we need to scalarize this intrinsic. For math builtins 515f7cfac7aSBenjamin Kramer // this will emit a costly libcall, adding call overhead and spills. Make it 516f7cfac7aSBenjamin Kramer // very expensive. 517f7cfac7aSBenjamin Kramer if (RetTy->isVectorTy()) { 518f7cfac7aSBenjamin Kramer unsigned Num = RetTy->getVectorNumElements(); 519f7cfac7aSBenjamin Kramer unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(), 520f7cfac7aSBenjamin Kramer Tys); 521f7cfac7aSBenjamin Kramer return 10 * Cost * Num; 522f7cfac7aSBenjamin Kramer } 523f7cfac7aSBenjamin Kramer 524f7cfac7aSBenjamin Kramer // This is going to be turned into a library call, make it expensive. 525f7cfac7aSBenjamin Kramer return 10; 526f7cfac7aSBenjamin Kramer } 527664e354dSChandler Carruth 528664e354dSChandler Carruth unsigned BasicTTI::getNumberOfParts(Type *Tp) const { 529afc1036fSBill Wendling std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp); 530664e354dSChandler Carruth return LT.first; 531664e354dSChandler Carruth } 532594fa2dcSArnold Schwaighofer 5339da9a43aSArnold Schwaighofer unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const { 534594fa2dcSArnold Schwaighofer return 0; 535594fa2dcSArnold Schwaighofer } 536cae8735aSArnold Schwaighofer 537cae8735aSArnold Schwaighofer unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty, 538cae8735aSArnold Schwaighofer bool IsPairwise) const { 539cae8735aSArnold Schwaighofer assert(Ty->isVectorTy() && "Expect a vector type"); 540cae8735aSArnold Schwaighofer unsigned NumVecElts = Ty->getVectorNumElements(); 541cae8735aSArnold Schwaighofer unsigned NumReduxLevels = Log2_32(NumVecElts); 542cae8735aSArnold Schwaighofer unsigned ArithCost = NumReduxLevels * 543cae8735aSArnold Schwaighofer TopTTI->getArithmeticInstrCost(Opcode, Ty); 544cae8735aSArnold Schwaighofer // Assume the pairwise shuffles add a cost. 545cae8735aSArnold Schwaighofer unsigned ShuffleCost = 546cae8735aSArnold Schwaighofer NumReduxLevels * (IsPairwise + 1) * 547cae8735aSArnold Schwaighofer TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty); 548cae8735aSArnold Schwaighofer return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true); 549cae8735aSArnold Schwaighofer } 550