Home | History | Annotate | Download | only in CodeGen
      1 //===- BasicTargetTransformInfo.cpp - Basic target-independent TTI impl ---===//
      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 /// \file
     10 /// This file provides the implementation of a basic TargetTransformInfo pass
     11 /// predicated on the target abstractions present in the target independent
     12 /// code generator. It uses these (primarily TargetLowering) to model as much
     13 /// of the TTI query interface as possible. It is included by most targets so
     14 /// that they can specialize only a small subset of the query space.
     15 ///
     16 //===----------------------------------------------------------------------===//
     17 
     18 #define DEBUG_TYPE "basictti"
     19 #include "llvm/CodeGen/Passes.h"
     20 #include "llvm/Analysis/TargetTransformInfo.h"
     21 #include "llvm/Target/TargetLowering.h"
     22 #include <utility>
     23 
     24 using namespace llvm;
     25 
     26 namespace {
     27 
     28 class BasicTTI : public ImmutablePass, public TargetTransformInfo {
     29   const TargetLoweringBase *TLI;
     30 
     31   /// Estimate the overhead of scalarizing an instruction. Insert and Extract
     32   /// are set if the result needs to be inserted and/or extracted from vectors.
     33   unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
     34 
     35 public:
     36   BasicTTI() : ImmutablePass(ID), TLI(0) {
     37     llvm_unreachable("This pass cannot be directly constructed");
     38   }
     39 
     40   BasicTTI(const TargetLoweringBase *TLI) : ImmutablePass(ID), TLI(TLI) {
     41     initializeBasicTTIPass(*PassRegistry::getPassRegistry());
     42   }
     43 
     44   virtual void initializePass() {
     45     pushTTIStack(this);
     46   }
     47 
     48   virtual void finalizePass() {
     49     popTTIStack();
     50   }
     51 
     52   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
     53     TargetTransformInfo::getAnalysisUsage(AU);
     54   }
     55 
     56   /// Pass identification.
     57   static char ID;
     58 
     59   /// Provide necessary pointer adjustments for the two base classes.
     60   virtual void *getAdjustedAnalysisPointer(const void *ID) {
     61     if (ID == &TargetTransformInfo::ID)
     62       return (TargetTransformInfo*)this;
     63     return this;
     64   }
     65 
     66   /// \name Scalar TTI Implementations
     67   /// @{
     68 
     69   virtual bool isLegalAddImmediate(int64_t imm) const;
     70   virtual bool isLegalICmpImmediate(int64_t imm) const;
     71   virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
     72                                      int64_t BaseOffset, bool HasBaseReg,
     73                                      int64_t Scale) const;
     74   virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
     75   virtual bool isTypeLegal(Type *Ty) const;
     76   virtual unsigned getJumpBufAlignment() const;
     77   virtual unsigned getJumpBufSize() const;
     78   virtual bool shouldBuildLookupTables() const;
     79 
     80   /// @}
     81 
     82   /// \name Vector TTI Implementations
     83   /// @{
     84 
     85   virtual unsigned getNumberOfRegisters(bool Vector) const;
     86   virtual unsigned getMaximumUnrollFactor() const;
     87   virtual unsigned getRegisterBitWidth(bool Vector) const;
     88   virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const;
     89   virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
     90                                   int Index, Type *SubTp) const;
     91   virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
     92                                     Type *Src) const;
     93   virtual unsigned getCFInstrCost(unsigned Opcode) const;
     94   virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
     95                                       Type *CondTy) const;
     96   virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
     97                                       unsigned Index) const;
     98   virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
     99                                    unsigned Alignment,
    100                                    unsigned AddressSpace) const;
    101   virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
    102                                          ArrayRef<Type*> Tys) const;
    103   virtual unsigned getNumberOfParts(Type *Tp) const;
    104   virtual unsigned getAddressComputationCost(Type *Ty) const;
    105 
    106   /// @}
    107 };
    108 
    109 }
    110 
    111 INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
    112                    "Target independent code generator's TTI", true, true, false)
    113 char BasicTTI::ID = 0;
    114 
    115 ImmutablePass *
    116 llvm::createBasicTargetTransformInfoPass(const TargetLoweringBase *TLI) {
    117   return new BasicTTI(TLI);
    118 }
    119 
    120 
    121 bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
    122   return TLI->isLegalAddImmediate(imm);
    123 }
    124 
    125 bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
    126   return TLI->isLegalICmpImmediate(imm);
    127 }
    128 
    129 bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
    130                                      int64_t BaseOffset, bool HasBaseReg,
    131                                      int64_t Scale) const {
    132   TargetLoweringBase::AddrMode AM;
    133   AM.BaseGV = BaseGV;
    134   AM.BaseOffs = BaseOffset;
    135   AM.HasBaseReg = HasBaseReg;
    136   AM.Scale = Scale;
    137   return TLI->isLegalAddressingMode(AM, Ty);
    138 }
    139 
    140 bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
    141   return TLI->isTruncateFree(Ty1, Ty2);
    142 }
    143 
    144 bool BasicTTI::isTypeLegal(Type *Ty) const {
    145   EVT T = TLI->getValueType(Ty);
    146   return TLI->isTypeLegal(T);
    147 }
    148 
    149 unsigned BasicTTI::getJumpBufAlignment() const {
    150   return TLI->getJumpBufAlignment();
    151 }
    152 
    153 unsigned BasicTTI::getJumpBufSize() const {
    154   return TLI->getJumpBufSize();
    155 }
    156 
    157 bool BasicTTI::shouldBuildLookupTables() const {
    158   return TLI->supportJumpTables() &&
    159       (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
    160        TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
    161 }
    162 
    163 //===----------------------------------------------------------------------===//
    164 //
    165 // Calls used by the vectorizers.
    166 //
    167 //===----------------------------------------------------------------------===//
    168 
    169 unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
    170                                             bool Extract) const {
    171   assert (Ty->isVectorTy() && "Can only scalarize vectors");
    172   unsigned Cost = 0;
    173 
    174   for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
    175     if (Insert)
    176       Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
    177     if (Extract)
    178       Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
    179   }
    180 
    181   return Cost;
    182 }
    183 
    184 unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
    185   return 1;
    186 }
    187 
    188 unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
    189   return 32;
    190 }
    191 
    192 unsigned BasicTTI::getMaximumUnrollFactor() const {
    193   return 1;
    194 }
    195 
    196 unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
    197   // Check if any of the operands are vector operands.
    198   int ISD = TLI->InstructionOpcodeToISD(Opcode);
    199   assert(ISD && "Invalid opcode");
    200 
    201   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
    202 
    203   if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
    204     // The operation is legal. Assume it costs 1.
    205     // If the type is split to multiple registers, assume that thre is some
    206     // overhead to this.
    207     // TODO: Once we have extract/insert subvector cost we need to use them.
    208     if (LT.first > 1)
    209       return LT.first * 2;
    210     return LT.first * 1;
    211   }
    212 
    213   if (!TLI->isOperationExpand(ISD, LT.second)) {
    214     // If the operation is custom lowered then assume
    215     // thare the code is twice as expensive.
    216     return LT.first * 2;
    217   }
    218 
    219   // Else, assume that we need to scalarize this op.
    220   if (Ty->isVectorTy()) {
    221     unsigned Num = Ty->getVectorNumElements();
    222     unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
    223     // return the cost of multiple scalar invocation plus the cost of inserting
    224     // and extracting the values.
    225     return getScalarizationOverhead(Ty, true, true) + Num * Cost;
    226   }
    227 
    228   // We don't know anything about this scalar instruction.
    229   return 1;
    230 }
    231 
    232 unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
    233                                   Type *SubTp) const {
    234   return 1;
    235 }
    236 
    237 unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
    238                                     Type *Src) const {
    239   int ISD = TLI->InstructionOpcodeToISD(Opcode);
    240   assert(ISD && "Invalid opcode");
    241 
    242   std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
    243   std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
    244 
    245   // Check for NOOP conversions.
    246   if (SrcLT.first == DstLT.first &&
    247       SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
    248 
    249       // Bitcast between types that are legalized to the same type are free.
    250       if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
    251         return 0;
    252   }
    253 
    254   if (Opcode == Instruction::Trunc &&
    255       TLI->isTruncateFree(SrcLT.second, DstLT.second))
    256     return 0;
    257 
    258   if (Opcode == Instruction::ZExt &&
    259       TLI->isZExtFree(SrcLT.second, DstLT.second))
    260     return 0;
    261 
    262   // If the cast is marked as legal (or promote) then assume low cost.
    263   if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
    264     return 1;
    265 
    266   // Handle scalar conversions.
    267   if (!Src->isVectorTy() && !Dst->isVectorTy()) {
    268 
    269     // Scalar bitcasts are usually free.
    270     if (Opcode == Instruction::BitCast)
    271       return 0;
    272 
    273     // Just check the op cost. If the operation is legal then assume it costs 1.
    274     if (!TLI->isOperationExpand(ISD, DstLT.second))
    275       return  1;
    276 
    277     // Assume that illegal scalar instruction are expensive.
    278     return 4;
    279   }
    280 
    281   // Check vector-to-vector casts.
    282   if (Dst->isVectorTy() && Src->isVectorTy()) {
    283 
    284     // If the cast is between same-sized registers, then the check is simple.
    285     if (SrcLT.first == DstLT.first &&
    286         SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
    287 
    288       // Assume that Zext is done using AND.
    289       if (Opcode == Instruction::ZExt)
    290         return 1;
    291 
    292       // Assume that sext is done using SHL and SRA.
    293       if (Opcode == Instruction::SExt)
    294         return 2;
    295 
    296       // Just check the op cost. If the operation is legal then assume it costs
    297       // 1 and multiply by the type-legalization overhead.
    298       if (!TLI->isOperationExpand(ISD, DstLT.second))
    299         return SrcLT.first * 1;
    300     }
    301 
    302     // If we are converting vectors and the operation is illegal, or
    303     // if the vectors are legalized to different types, estimate the
    304     // scalarization costs.
    305     unsigned Num = Dst->getVectorNumElements();
    306     unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
    307                                              Src->getScalarType());
    308 
    309     // Return the cost of multiple scalar invocation plus the cost of
    310     // inserting and extracting the values.
    311     return getScalarizationOverhead(Dst, true, true) + Num * Cost;
    312   }
    313 
    314   // We already handled vector-to-vector and scalar-to-scalar conversions. This
    315   // is where we handle bitcast between vectors and scalars. We need to assume
    316   //  that the conversion is scalarized in one way or another.
    317   if (Opcode == Instruction::BitCast)
    318     // Illegal bitcasts are done by storing and loading from a stack slot.
    319     return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
    320            (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
    321 
    322   llvm_unreachable("Unhandled cast");
    323  }
    324 
    325 unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
    326   // Branches are assumed to be predicted.
    327   return 0;
    328 }
    329 
    330 unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
    331                                       Type *CondTy) const {
    332   int ISD = TLI->InstructionOpcodeToISD(Opcode);
    333   assert(ISD && "Invalid opcode");
    334 
    335   // Selects on vectors are actually vector selects.
    336   if (ISD == ISD::SELECT) {
    337     assert(CondTy && "CondTy must exist");
    338     if (CondTy->isVectorTy())
    339       ISD = ISD::VSELECT;
    340   }
    341 
    342   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
    343 
    344   if (!TLI->isOperationExpand(ISD, LT.second)) {
    345     // The operation is legal. Assume it costs 1. Multiply
    346     // by the type-legalization overhead.
    347     return LT.first * 1;
    348   }
    349 
    350   // Otherwise, assume that the cast is scalarized.
    351   if (ValTy->isVectorTy()) {
    352     unsigned Num = ValTy->getVectorNumElements();
    353     if (CondTy)
    354       CondTy = CondTy->getScalarType();
    355     unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
    356                                                CondTy);
    357 
    358     // Return the cost of multiple scalar invocation plus the cost of inserting
    359     // and extracting the values.
    360     return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
    361   }
    362 
    363   // Unknown scalar opcode.
    364   return 1;
    365 }
    366 
    367 unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
    368                                       unsigned Index) const {
    369   return 1;
    370 }
    371 
    372 unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
    373                                    unsigned Alignment,
    374                                    unsigned AddressSpace) const {
    375   assert(!Src->isVoidTy() && "Invalid type");
    376   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
    377 
    378   // Assume that all loads of legal types cost 1.
    379   return LT.first;
    380 }
    381 
    382 unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
    383                                          ArrayRef<Type *> Tys) const {
    384   unsigned ISD = 0;
    385   switch (IID) {
    386   default: {
    387     // Assume that we need to scalarize this intrinsic.
    388     unsigned ScalarizationCost = 0;
    389     unsigned ScalarCalls = 1;
    390     if (RetTy->isVectorTy()) {
    391       ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
    392       ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
    393     }
    394     for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
    395       if (Tys[i]->isVectorTy()) {
    396         ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
    397         ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
    398       }
    399     }
    400 
    401     return ScalarCalls + ScalarizationCost;
    402   }
    403   // Look for intrinsics that can be lowered directly or turned into a scalar
    404   // intrinsic call.
    405   case Intrinsic::sqrt:    ISD = ISD::FSQRT;  break;
    406   case Intrinsic::sin:     ISD = ISD::FSIN;   break;
    407   case Intrinsic::cos:     ISD = ISD::FCOS;   break;
    408   case Intrinsic::exp:     ISD = ISD::FEXP;   break;
    409   case Intrinsic::exp2:    ISD = ISD::FEXP2;  break;
    410   case Intrinsic::log:     ISD = ISD::FLOG;   break;
    411   case Intrinsic::log10:   ISD = ISD::FLOG10; break;
    412   case Intrinsic::log2:    ISD = ISD::FLOG2;  break;
    413   case Intrinsic::fabs:    ISD = ISD::FABS;   break;
    414   case Intrinsic::floor:   ISD = ISD::FFLOOR; break;
    415   case Intrinsic::ceil:    ISD = ISD::FCEIL;  break;
    416   case Intrinsic::trunc:   ISD = ISD::FTRUNC; break;
    417   case Intrinsic::rint:    ISD = ISD::FRINT;  break;
    418   case Intrinsic::pow:     ISD = ISD::FPOW;   break;
    419   case Intrinsic::fma:     ISD = ISD::FMA;    break;
    420   case Intrinsic::fmuladd: ISD = ISD::FMA;    break; // FIXME: mul + add?
    421   }
    422 
    423   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
    424 
    425   if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
    426     // The operation is legal. Assume it costs 1.
    427     // If the type is split to multiple registers, assume that thre is some
    428     // overhead to this.
    429     // TODO: Once we have extract/insert subvector cost we need to use them.
    430     if (LT.first > 1)
    431       return LT.first * 2;
    432     return LT.first * 1;
    433   }
    434 
    435   if (!TLI->isOperationExpand(ISD, LT.second)) {
    436     // If the operation is custom lowered then assume
    437     // thare the code is twice as expensive.
    438     return LT.first * 2;
    439   }
    440 
    441   // Else, assume that we need to scalarize this intrinsic. For math builtins
    442   // this will emit a costly libcall, adding call overhead and spills. Make it
    443   // very expensive.
    444   if (RetTy->isVectorTy()) {
    445     unsigned Num = RetTy->getVectorNumElements();
    446     unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
    447                                                   Tys);
    448     return 10 * Cost * Num;
    449   }
    450 
    451   // This is going to be turned into a library call, make it expensive.
    452   return 10;
    453 }
    454 
    455 unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
    456   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
    457   return LT.first;
    458 }
    459 
    460 unsigned BasicTTI::getAddressComputationCost(Type *Ty) const {
    461   return 0;
    462 }
    463