Home | History | Annotate | Download | only in Analysis
      1 //===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "llvm/Analysis/TargetTransformInfo.h"
     11 #include "llvm/Analysis/TargetTransformInfoImpl.h"
     12 #include "llvm/IR/CallSite.h"
     13 #include "llvm/IR/DataLayout.h"
     14 #include "llvm/IR/Instruction.h"
     15 #include "llvm/IR/Instructions.h"
     16 #include "llvm/IR/IntrinsicInst.h"
     17 #include "llvm/IR/Module.h"
     18 #include "llvm/IR/Operator.h"
     19 #include "llvm/Support/ErrorHandling.h"
     20 
     21 using namespace llvm;
     22 
     23 #define DEBUG_TYPE "tti"
     24 
     25 namespace {
     26 /// \brief No-op implementation of the TTI interface using the utility base
     27 /// classes.
     28 ///
     29 /// This is used when no target specific information is available.
     30 struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
     31   explicit NoTTIImpl(const DataLayout &DL)
     32       : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
     33 };
     34 }
     35 
     36 TargetTransformInfo::TargetTransformInfo(const DataLayout &DL)
     37     : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
     38 
     39 TargetTransformInfo::~TargetTransformInfo() {}
     40 
     41 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
     42     : TTIImpl(std::move(Arg.TTIImpl)) {}
     43 
     44 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
     45   TTIImpl = std::move(RHS.TTIImpl);
     46   return *this;
     47 }
     48 
     49 int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
     50                                           Type *OpTy) const {
     51   int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
     52   assert(Cost >= 0 && "TTI should not produce negative costs!");
     53   return Cost;
     54 }
     55 
     56 int TargetTransformInfo::getCallCost(FunctionType *FTy, int NumArgs) const {
     57   int Cost = TTIImpl->getCallCost(FTy, NumArgs);
     58   assert(Cost >= 0 && "TTI should not produce negative costs!");
     59   return Cost;
     60 }
     61 
     62 int TargetTransformInfo::getCallCost(const Function *F,
     63                                      ArrayRef<const Value *> Arguments) const {
     64   int Cost = TTIImpl->getCallCost(F, Arguments);
     65   assert(Cost >= 0 && "TTI should not produce negative costs!");
     66   return Cost;
     67 }
     68 
     69 int TargetTransformInfo::getIntrinsicCost(
     70     Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const {
     71   int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
     72   assert(Cost >= 0 && "TTI should not produce negative costs!");
     73   return Cost;
     74 }
     75 
     76 int TargetTransformInfo::getUserCost(const User *U) const {
     77   int Cost = TTIImpl->getUserCost(U);
     78   assert(Cost >= 0 && "TTI should not produce negative costs!");
     79   return Cost;
     80 }
     81 
     82 bool TargetTransformInfo::hasBranchDivergence() const {
     83   return TTIImpl->hasBranchDivergence();
     84 }
     85 
     86 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
     87   return TTIImpl->isSourceOfDivergence(V);
     88 }
     89 
     90 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
     91   return TTIImpl->isLoweredToCall(F);
     92 }
     93 
     94 void TargetTransformInfo::getUnrollingPreferences(
     95     Loop *L, UnrollingPreferences &UP) const {
     96   return TTIImpl->getUnrollingPreferences(L, UP);
     97 }
     98 
     99 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
    100   return TTIImpl->isLegalAddImmediate(Imm);
    101 }
    102 
    103 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
    104   return TTIImpl->isLegalICmpImmediate(Imm);
    105 }
    106 
    107 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
    108                                                 int64_t BaseOffset,
    109                                                 bool HasBaseReg,
    110                                                 int64_t Scale,
    111                                                 unsigned AddrSpace) const {
    112   return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
    113                                         Scale, AddrSpace);
    114 }
    115 
    116 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
    117   return TTIImpl->isLegalMaskedStore(DataType);
    118 }
    119 
    120 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
    121   return TTIImpl->isLegalMaskedLoad(DataType);
    122 }
    123 
    124 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
    125   return TTIImpl->isLegalMaskedGather(DataType);
    126 }
    127 
    128 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
    129   return TTIImpl->isLegalMaskedGather(DataType);
    130 }
    131 
    132 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
    133                                               int64_t BaseOffset,
    134                                               bool HasBaseReg,
    135                                               int64_t Scale,
    136                                               unsigned AddrSpace) const {
    137   int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
    138                                            Scale, AddrSpace);
    139   assert(Cost >= 0 && "TTI should not produce negative costs!");
    140   return Cost;
    141 }
    142 
    143 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
    144   return TTIImpl->isTruncateFree(Ty1, Ty2);
    145 }
    146 
    147 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
    148   return TTIImpl->isProfitableToHoist(I);
    149 }
    150 
    151 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
    152   return TTIImpl->isTypeLegal(Ty);
    153 }
    154 
    155 unsigned TargetTransformInfo::getJumpBufAlignment() const {
    156   return TTIImpl->getJumpBufAlignment();
    157 }
    158 
    159 unsigned TargetTransformInfo::getJumpBufSize() const {
    160   return TTIImpl->getJumpBufSize();
    161 }
    162 
    163 bool TargetTransformInfo::shouldBuildLookupTables() const {
    164   return TTIImpl->shouldBuildLookupTables();
    165 }
    166 
    167 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
    168   return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
    169 }
    170 
    171 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
    172   return TTIImpl->enableInterleavedAccessVectorization();
    173 }
    174 
    175 TargetTransformInfo::PopcntSupportKind
    176 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
    177   return TTIImpl->getPopcntSupport(IntTyWidthInBit);
    178 }
    179 
    180 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
    181   return TTIImpl->haveFastSqrt(Ty);
    182 }
    183 
    184 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
    185   int Cost = TTIImpl->getFPOpCost(Ty);
    186   assert(Cost >= 0 && "TTI should not produce negative costs!");
    187   return Cost;
    188 }
    189 
    190 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
    191   int Cost = TTIImpl->getIntImmCost(Imm, Ty);
    192   assert(Cost >= 0 && "TTI should not produce negative costs!");
    193   return Cost;
    194 }
    195 
    196 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
    197                                        const APInt &Imm, Type *Ty) const {
    198   int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
    199   assert(Cost >= 0 && "TTI should not produce negative costs!");
    200   return Cost;
    201 }
    202 
    203 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
    204                                        const APInt &Imm, Type *Ty) const {
    205   int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
    206   assert(Cost >= 0 && "TTI should not produce negative costs!");
    207   return Cost;
    208 }
    209 
    210 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
    211   return TTIImpl->getNumberOfRegisters(Vector);
    212 }
    213 
    214 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
    215   return TTIImpl->getRegisterBitWidth(Vector);
    216 }
    217 
    218 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
    219   return TTIImpl->getMaxInterleaveFactor(VF);
    220 }
    221 
    222 int TargetTransformInfo::getArithmeticInstrCost(
    223     unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
    224     OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
    225     OperandValueProperties Opd2PropInfo) const {
    226   int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
    227                                              Opd1PropInfo, Opd2PropInfo);
    228   assert(Cost >= 0 && "TTI should not produce negative costs!");
    229   return Cost;
    230 }
    231 
    232 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
    233                                         Type *SubTp) const {
    234   int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
    235   assert(Cost >= 0 && "TTI should not produce negative costs!");
    236   return Cost;
    237 }
    238 
    239 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
    240                                           Type *Src) const {
    241   int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src);
    242   assert(Cost >= 0 && "TTI should not produce negative costs!");
    243   return Cost;
    244 }
    245 
    246 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
    247   int Cost = TTIImpl->getCFInstrCost(Opcode);
    248   assert(Cost >= 0 && "TTI should not produce negative costs!");
    249   return Cost;
    250 }
    251 
    252 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
    253                                             Type *CondTy) const {
    254   int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
    255   assert(Cost >= 0 && "TTI should not produce negative costs!");
    256   return Cost;
    257 }
    258 
    259 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
    260                                             unsigned Index) const {
    261   int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
    262   assert(Cost >= 0 && "TTI should not produce negative costs!");
    263   return Cost;
    264 }
    265 
    266 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
    267                                          unsigned Alignment,
    268                                          unsigned AddressSpace) const {
    269   int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
    270   assert(Cost >= 0 && "TTI should not produce negative costs!");
    271   return Cost;
    272 }
    273 
    274 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
    275                                                unsigned Alignment,
    276                                                unsigned AddressSpace) const {
    277   int Cost =
    278       TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
    279   assert(Cost >= 0 && "TTI should not produce negative costs!");
    280   return Cost;
    281 }
    282 
    283 int TargetTransformInfo::getInterleavedMemoryOpCost(
    284     unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
    285     unsigned Alignment, unsigned AddressSpace) const {
    286   int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
    287                                                  Alignment, AddressSpace);
    288   assert(Cost >= 0 && "TTI should not produce negative costs!");
    289   return Cost;
    290 }
    291 
    292 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
    293                                                ArrayRef<Type *> Tys) const {
    294   int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys);
    295   assert(Cost >= 0 && "TTI should not produce negative costs!");
    296   return Cost;
    297 }
    298 
    299 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
    300                                           ArrayRef<Type *> Tys) const {
    301   int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
    302   assert(Cost >= 0 && "TTI should not produce negative costs!");
    303   return Cost;
    304 }
    305 
    306 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
    307   return TTIImpl->getNumberOfParts(Tp);
    308 }
    309 
    310 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
    311                                                    bool IsComplex) const {
    312   int Cost = TTIImpl->getAddressComputationCost(Tp, IsComplex);
    313   assert(Cost >= 0 && "TTI should not produce negative costs!");
    314   return Cost;
    315 }
    316 
    317 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
    318                                           bool IsPairwiseForm) const {
    319   int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
    320   assert(Cost >= 0 && "TTI should not produce negative costs!");
    321   return Cost;
    322 }
    323 
    324 unsigned
    325 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
    326   return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
    327 }
    328 
    329 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
    330                                              MemIntrinsicInfo &Info) const {
    331   return TTIImpl->getTgtMemIntrinsic(Inst, Info);
    332 }
    333 
    334 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
    335     IntrinsicInst *Inst, Type *ExpectedType) const {
    336   return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
    337 }
    338 
    339 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
    340                                               const Function *Callee) const {
    341   return TTIImpl->areInlineCompatible(Caller, Callee);
    342 }
    343 
    344 TargetTransformInfo::Concept::~Concept() {}
    345 
    346 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
    347 
    348 TargetIRAnalysis::TargetIRAnalysis(
    349     std::function<Result(const Function &)> TTICallback)
    350     : TTICallback(TTICallback) {}
    351 
    352 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F) {
    353   return TTICallback(F);
    354 }
    355 
    356 char TargetIRAnalysis::PassID;
    357 
    358 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
    359   return Result(F.getParent()->getDataLayout());
    360 }
    361 
    362 // Register the basic pass.
    363 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
    364                 "Target Transform Information", false, true)
    365 char TargetTransformInfoWrapperPass::ID = 0;
    366 
    367 void TargetTransformInfoWrapperPass::anchor() {}
    368 
    369 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
    370     : ImmutablePass(ID) {
    371   initializeTargetTransformInfoWrapperPassPass(
    372       *PassRegistry::getPassRegistry());
    373 }
    374 
    375 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
    376     TargetIRAnalysis TIRA)
    377     : ImmutablePass(ID), TIRA(std::move(TIRA)) {
    378   initializeTargetTransformInfoWrapperPassPass(
    379       *PassRegistry::getPassRegistry());
    380 }
    381 
    382 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
    383   TTI = TIRA.run(F);
    384   return *TTI;
    385 }
    386 
    387 ImmutablePass *
    388 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
    389   return new TargetTransformInfoWrapperPass(std::move(TIRA));
    390 }
    391