LCOV - code coverage report
Current view: top level - include/llvm/Analysis - TargetTransformInfoImpl.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 120 133 90.2 %
Date: 2018-02-23 15:42:53 Functions: 57 107 53.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- TargetTransformInfoImpl.h --------------------------------*- C++ -*-===//
       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 helpers for the implementation of
      11             : /// a TargetTransformInfo-conforming class.
      12             : ///
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
      16             : #define LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
      17             : 
      18             : #include "llvm/Analysis/ScalarEvolutionExpressions.h"
      19             : #include "llvm/Analysis/TargetTransformInfo.h"
      20             : #include "llvm/Analysis/VectorUtils.h"
      21             : #include "llvm/IR/CallSite.h"
      22             : #include "llvm/IR/DataLayout.h"
      23             : #include "llvm/IR/Function.h"
      24             : #include "llvm/IR/GetElementPtrTypeIterator.h"
      25             : #include "llvm/IR/Operator.h"
      26             : #include "llvm/IR/Type.h"
      27             : 
      28             : namespace llvm {
      29             : 
      30             : /// \brief Base class for use as a mix-in that aids implementing
      31             : /// a TargetTransformInfo-compatible class.
      32             : class TargetTransformInfoImplBase {
      33             : protected:
      34             :   typedef TargetTransformInfo TTI;
      35             : 
      36             :   const DataLayout &DL;
      37             : 
      38     2202065 :   explicit TargetTransformInfoImplBase(const DataLayout &DL) : DL(DL) {}
      39             : 
      40             : public:
      41             :   // Provide value semantics. MSVC requires that we spell all of these out.
      42             :   TargetTransformInfoImplBase(const TargetTransformInfoImplBase &Arg)
      43     2202065 :       : DL(Arg.DL) {}
      44     2216265 :   TargetTransformInfoImplBase(TargetTransformInfoImplBase &&Arg) : DL(Arg.DL) {}
      45             : 
      46        1552 :   const DataLayout &getDataLayout() const { return DL; }
      47             : 
      48     1385064 :   unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
      49     1385064 :     switch (Opcode) {
      50             :     default:
      51             :       // By default, just classify everything as 'basic'.
      52             :       return TTI::TCC_Basic;
      53             : 
      54           0 :     case Instruction::GetElementPtr:
      55           0 :       llvm_unreachable("Use getGEPCost for GEP operations!");
      56             : 
      57       28549 :     case Instruction::BitCast:
      58             :       assert(OpTy && "Cast instructions must provide the operand type");
      59       85552 :       if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
      60             :         // Identity and pointer-to-pointer casts are free.
      61             :         return TTI::TCC_Free;
      62             : 
      63             :       // Otherwise, the default basic cost is used.
      64             :       return TTI::TCC_Basic;
      65             : 
      66        2015 :     case Instruction::FDiv:
      67             :     case Instruction::FRem:
      68             :     case Instruction::SDiv:
      69             :     case Instruction::SRem:
      70             :     case Instruction::UDiv:
      71             :     case Instruction::URem:
      72        2015 :       return TTI::TCC_Expensive;
      73             : 
      74       10024 :     case Instruction::IntToPtr: {
      75             :       // An inttoptr cast is free so long as the input is a legal integer type
      76             :       // which doesn't contain values outside the range of a pointer.
      77       10024 :       unsigned OpSize = OpTy->getScalarSizeInBits();
      78       30069 :       if (DL.isLegalInteger(OpSize) &&
      79       10021 :           OpSize <= DL.getPointerTypeSizeInBits(Ty))
      80             :         return TTI::TCC_Free;
      81             : 
      82             :       // Otherwise it's not a no-op.
      83             :       return TTI::TCC_Basic;
      84             :     }
      85       18790 :     case Instruction::PtrToInt: {
      86             :       // A ptrtoint cast is free so long as the result is large enough to store
      87             :       // the pointer, and a legal integer type.
      88       18790 :       unsigned DestSize = Ty->getScalarSizeInBits();
      89       56361 :       if (DL.isLegalInteger(DestSize) &&
      90       18781 :           DestSize >= DL.getPointerTypeSizeInBits(OpTy))
      91             :         return TTI::TCC_Free;
      92             : 
      93             :       // Otherwise it's not a no-op.
      94             :       return TTI::TCC_Basic;
      95             :     }
      96          40 :     case Instruction::Trunc:
      97             :       // trunc to a native type is free (assuming the target has compare and
      98             :       // shift-right of the same width).
      99          80 :       if (DL.isLegalInteger(DL.getTypeSizeInBits(Ty)))
     100             :         return TTI::TCC_Free;
     101             : 
     102          31 :       return TTI::TCC_Basic;
     103             :     }
     104             :   }
     105             : 
     106             :   int getGEPCost(Type *PointeeType, const Value *Ptr,
     107             :                  ArrayRef<const Value *> Operands) {
     108             :     // In the basic model, we just assume that all-constant GEPs will be folded
     109             :     // into their uses via addressing modes.
     110             :     for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
     111             :       if (!isa<Constant>(Operands[Idx]))
     112             :         return TTI::TCC_Basic;
     113             : 
     114             :     return TTI::TCC_Free;
     115             :   }
     116             : 
     117             :   unsigned getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
     118             :                                             unsigned &JTSize) {
     119           2 :     JTSize = 0;
     120             :     return SI.getNumCases();
     121             :   }
     122             : 
     123             :   int getExtCost(const Instruction *I, const Value *Src) {
     124             :     return TTI::TCC_Basic;
     125             :   }
     126             : 
     127             :   unsigned getCallCost(FunctionType *FTy, int NumArgs) {
     128             :     assert(FTy && "FunctionType must be provided to this routine.");
     129             : 
     130             :     // The target-independent implementation just measures the size of the
     131             :     // function by approximating that each argument will take on average one
     132             :     // instruction to prepare.
     133             : 
     134      919259 :     if (NumArgs < 0)
     135             :       // Set the argument number to the number of explicit arguments in the
     136             :       // function.
     137           0 :       NumArgs = FTy->getNumParams();
     138             : 
     139      919259 :     return TTI::TCC_Basic * (NumArgs + 1);
     140             :   }
     141             : 
     142             :   unsigned getInliningThresholdMultiplier() { return 1; }
     143             : 
     144      390545 :   unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
     145             :                             ArrayRef<Type *> ParamTys) {
     146      390545 :     switch (IID) {
     147             :     default:
     148             :       // Intrinsics rarely (if ever) have normal argument setup constraints.
     149             :       // Model them as having a basic instruction cost.
     150             :       // FIXME: This is wrong for libc intrinsics.
     151             :       return TTI::TCC_Basic;
     152             : 
     153      388126 :     case Intrinsic::annotation:
     154             :     case Intrinsic::assume:
     155             :     case Intrinsic::sideeffect:
     156             :     case Intrinsic::dbg_declare:
     157             :     case Intrinsic::dbg_value:
     158             :     case Intrinsic::invariant_start:
     159             :     case Intrinsic::invariant_end:
     160             :     case Intrinsic::lifetime_start:
     161             :     case Intrinsic::lifetime_end:
     162             :     case Intrinsic::objectsize:
     163             :     case Intrinsic::ptr_annotation:
     164             :     case Intrinsic::var_annotation:
     165             :     case Intrinsic::experimental_gc_result:
     166             :     case Intrinsic::experimental_gc_relocate:
     167             :     case Intrinsic::coro_alloc:
     168             :     case Intrinsic::coro_begin:
     169             :     case Intrinsic::coro_free:
     170             :     case Intrinsic::coro_end:
     171             :     case Intrinsic::coro_frame:
     172             :     case Intrinsic::coro_size:
     173             :     case Intrinsic::coro_suspend:
     174             :     case Intrinsic::coro_param:
     175             :     case Intrinsic::coro_subfn_addr:
     176             :       // These intrinsics don't actually represent code after lowering.
     177             :       return TTI::TCC_Free;
     178             :     }
     179             :   }
     180             : 
     181             :   bool hasBranchDivergence() { return false; }
     182             : 
     183             :   bool isSourceOfDivergence(const Value *V) { return false; }
     184             : 
     185             :   bool isAlwaysUniform(const Value *V) { return false; }
     186             : 
     187             :   unsigned getFlatAddressSpace () {
     188             :     return -1;
     189             :   }
     190             : 
     191     1878260 :   bool isLoweredToCall(const Function *F) {
     192             :     assert(F && "A concrete function must be provided to this routine.");
     193             : 
     194             :     // FIXME: These should almost certainly not be handled here, and instead
     195             :     // handled with the help of TLI or the target itself. This was largely
     196             :     // ported from existing analysis heuristics here so that such refactorings
     197             :     // can take place in the future.
     198             : 
     199     1878260 :     if (F->isIntrinsic())
     200             :       return false;
     201             : 
     202     1270968 :     if (F->hasLocalLinkage() || !F->hasName())
     203             :       return true;
     204             : 
     205     1270968 :     StringRef Name = F->getName();
     206             : 
     207             :     // These will all likely lower to a single selection DAG node.
     208             :     if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
     209             :         Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
     210             :         Name == "fmin" || Name == "fminf" || Name == "fminl" ||
     211             :         Name == "fmax" || Name == "fmaxf" || Name == "fmaxl" ||
     212             :         Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
     213             :         Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
     214             :       return false;
     215             : 
     216             :     // These are all likely to be optimized into something smaller.
     217             :     if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
     218             :         Name == "exp2l" || Name == "exp2f" || Name == "floor" ||
     219             :         Name == "floorf" || Name == "ceil" || Name == "round" ||
     220             :         Name == "ffs" || Name == "ffsl" || Name == "abs" || Name == "labs" ||
     221             :         Name == "llabs")
     222             :       return false;
     223             : 
     224             :     return true;
     225             :   }
     226             : 
     227             :   void getUnrollingPreferences(Loop *, ScalarEvolution &,
     228             :                                TTI::UnrollingPreferences &) {}
     229             : 
     230             :   bool isLegalAddImmediate(int64_t Imm) { return false; }
     231             : 
     232             :   bool isLegalICmpImmediate(int64_t Imm) { return false; }
     233             : 
     234             :   bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
     235             :                              bool HasBaseReg, int64_t Scale,
     236             :                              unsigned AddrSpace, Instruction *I = nullptr) {
     237             :     // Guess that only reg and reg+reg addressing is allowed. This heuristic is
     238             :     // taken from the implementation of LSR.
     239       14243 :     return !BaseGV && BaseOffset == 0 && (Scale == 0 || Scale == 1);
     240             :   }
     241             : 
     242       12868 :   bool isLSRCostLess(TTI::LSRCost &C1, TTI::LSRCost &C2) {
     243       12868 :     return std::tie(C1.NumRegs, C1.AddRecCost, C1.NumIVMuls, C1.NumBaseAdds,
     244             :                     C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
     245       12868 :            std::tie(C2.NumRegs, C2.AddRecCost, C2.NumIVMuls, C2.NumBaseAdds,
     246       38604 :                     C2.ScaleCost, C2.ImmCost, C2.SetupCost);
     247             :   }
     248             : 
     249             :   bool canMacroFuseCmp() { return false; }
     250             : 
     251             :   bool isLegalMaskedStore(Type *DataType) { return false; }
     252             : 
     253             :   bool isLegalMaskedLoad(Type *DataType) { return false; }
     254             : 
     255             :   bool isLegalMaskedScatter(Type *DataType) { return false; }
     256             : 
     257             :   bool isLegalMaskedGather(Type *DataType) { return false; }
     258             : 
     259             :   bool hasDivRemOp(Type *DataType, bool IsSigned) { return false; }
     260             : 
     261             :   bool hasVolatileVariant(Instruction *I, unsigned AddrSpace) { return false; }
     262             : 
     263             :   bool prefersVectorizedAddressing() { return true; }
     264             : 
     265             :   int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
     266             :                            bool HasBaseReg, int64_t Scale, unsigned AddrSpace) {
     267             :     // Guess that all legal addressing mode are free.
     268             :     if (isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
     269             :                               Scale, AddrSpace))
     270             :       return 0;
     271             :     return -1;
     272             :   }
     273             : 
     274             :   bool LSRWithInstrQueries() { return false; }
     275             : 
     276             :   bool isTruncateFree(Type *Ty1, Type *Ty2) { return false; }
     277             : 
     278             :   bool isProfitableToHoist(Instruction *I) { return true; }
     279             : 
     280             :   bool isTypeLegal(Type *Ty) { return false; }
     281             : 
     282             :   unsigned getJumpBufAlignment() { return 0; }
     283             : 
     284             :   unsigned getJumpBufSize() { return 0; }
     285             : 
     286             :   bool shouldBuildLookupTables() { return true; }
     287             :   bool shouldBuildLookupTablesForConstant(Constant *C) { return true; }
     288             : 
     289             :   bool useColdCCForColdCall(Function &F) { return false; }
     290             : 
     291             :   unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
     292             :     return 0;
     293             :   }
     294             : 
     295             :   unsigned getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
     296             :                                             unsigned VF) { return 0; }
     297             : 
     298             :   bool supportsEfficientVectorElementLoadStore() { return false; }
     299             : 
     300             :   bool enableAggressiveInterleaving(bool LoopHasReductions) { return false; }
     301             : 
     302             :   const TTI::MemCmpExpansionOptions *enableMemCmpExpansion(
     303             :       bool IsZeroCmp) const {
     304             :     return nullptr;
     305             :   }
     306             : 
     307             :   bool enableInterleavedAccessVectorization() { return false; }
     308             : 
     309             :   bool isFPVectorizationPotentiallyUnsafe() { return false; }
     310             : 
     311             :   bool allowsMisalignedMemoryAccesses(LLVMContext &Context,
     312             :                                       unsigned BitWidth,
     313             :                                       unsigned AddressSpace,
     314             :                                       unsigned Alignment,
     315             :                                       bool *Fast) { return false; }
     316             : 
     317             :   TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) {
     318             :     return TTI::PSK_Software;
     319             :   }
     320             : 
     321             :   bool haveFastSqrt(Type *Ty) { return false; }
     322             : 
     323             :   bool isFCmpOrdCheaperThanFCmpZero(Type *Ty) { return true; }
     324             :   
     325             :   unsigned getFPOpCost(Type *Ty) { return TargetTransformInfo::TCC_Basic; }
     326             : 
     327             :   int getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
     328             :                             Type *Ty) {
     329             :     return 0;
     330             :   }
     331             : 
     332             :   unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
     333             : 
     334             :   unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
     335             :                          Type *Ty) {
     336             :     return TTI::TCC_Free;
     337             :   }
     338             : 
     339             :   unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
     340             :                          Type *Ty) {
     341             :     return TTI::TCC_Free;
     342             :   }
     343             : 
     344             :   unsigned getNumberOfRegisters(bool Vector) { return 8; }
     345             : 
     346             :   unsigned getRegisterBitWidth(bool Vector) const { return 32; }
     347             : 
     348             :   unsigned getMinVectorRegisterBitWidth() { return 128; }
     349             : 
     350             :   bool
     351             :   shouldConsiderAddressTypePromotion(const Instruction &I,
     352             :                                      bool &AllowPromotionWithoutCommonHeader) {
     353       26961 :     AllowPromotionWithoutCommonHeader = false;
     354             :     return false;
     355             :   }
     356             : 
     357             :   unsigned getCacheLineSize() { return 0; }
     358             : 
     359             :   llvm::Optional<unsigned> getCacheSize(TargetTransformInfo::CacheLevel Level) {
     360           4 :     switch (Level) {
     361             :     case TargetTransformInfo::CacheLevel::L1D:
     362             :       LLVM_FALLTHROUGH;
     363             :     case TargetTransformInfo::CacheLevel::L2D:
     364             :       return llvm::Optional<unsigned>();
     365             :     }
     366             : 
     367           0 :     llvm_unreachable("Unknown TargetTransformInfo::CacheLevel");
     368             :   }
     369             : 
     370             :   llvm::Optional<unsigned> getCacheAssociativity(
     371             :     TargetTransformInfo::CacheLevel Level) {
     372           2 :     switch (Level) {
     373             :     case TargetTransformInfo::CacheLevel::L1D:
     374             :       LLVM_FALLTHROUGH;
     375             :     case TargetTransformInfo::CacheLevel::L2D:
     376             :       return llvm::Optional<unsigned>();
     377             :     }
     378             : 
     379           0 :     llvm_unreachable("Unknown TargetTransformInfo::CacheLevel");
     380             :   }
     381             : 
     382             :   unsigned getPrefetchDistance() { return 0; }
     383             : 
     384             :   unsigned getMinPrefetchStride() { return 1; }
     385             : 
     386             :   unsigned getMaxPrefetchIterationsAhead() { return UINT_MAX; }
     387             : 
     388             :   unsigned getMaxInterleaveFactor(unsigned VF) { return 1; }
     389             : 
     390             :   unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
     391             :                                   TTI::OperandValueKind Opd1Info,
     392             :                                   TTI::OperandValueKind Opd2Info,
     393             :                                   TTI::OperandValueProperties Opd1PropInfo,
     394             :                                   TTI::OperandValueProperties Opd2PropInfo,
     395             :                                   ArrayRef<const Value *> Args) {
     396             :     return 1;
     397             :   }
     398             : 
     399             :   unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index,
     400             :                           Type *SubTp) {
     401             :     return 1;
     402             :   }
     403             : 
     404             :   unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
     405             :                             const Instruction *I) { return 1; }
     406             : 
     407             :   unsigned getExtractWithExtendCost(unsigned Opcode, Type *Dst,
     408             :                                     VectorType *VecTy, unsigned Index) {
     409             :     return 1;
     410             :   }
     411             : 
     412             :   unsigned getCFInstrCost(unsigned Opcode) { return 1; }
     413             : 
     414             :   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
     415             :                               const Instruction *I) {
     416             :     return 1;
     417             :   }
     418             : 
     419             :   unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
     420             :     return 1;
     421             :   }
     422             : 
     423             :   unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
     424             :                            unsigned AddressSpace, const Instruction *I) {
     425             :     return 1;
     426             :   }
     427             : 
     428             :   unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
     429             :                                  unsigned AddressSpace) {
     430             :     return 1;
     431             :   }
     432             : 
     433             :   unsigned getGatherScatterOpCost(unsigned Opcode, Type *DataTy, Value *Ptr,
     434             :                                   bool VariableMask,
     435             :                                   unsigned Alignment) {
     436             :     return 1;
     437             :   }
     438             : 
     439             :   unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
     440             :                                       unsigned Factor,
     441             :                                       ArrayRef<unsigned> Indices,
     442             :                                       unsigned Alignment,
     443             :                                       unsigned AddressSpace) {
     444             :     return 1;
     445             :   }
     446             : 
     447             :   unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
     448             :                                  ArrayRef<Type *> Tys, FastMathFlags FMF,
     449             :                                  unsigned ScalarizationCostPassed) {
     450             :     return 1;
     451             :   }
     452             :   unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
     453             :             ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) {
     454             :     return 1;
     455             :   }
     456             : 
     457             :   unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
     458             :     return 1;
     459             :   }
     460             : 
     461             :   unsigned getNumberOfParts(Type *Tp) { return 0; }
     462             : 
     463             :   unsigned getAddressComputationCost(Type *Tp, ScalarEvolution *,
     464             :                                      const SCEV *) {
     465             :     return 0;
     466             :   }
     467             : 
     468             :   unsigned getArithmeticReductionCost(unsigned, Type *, bool) { return 1; }
     469             : 
     470             :   unsigned getMinMaxReductionCost(Type *, Type *, bool, bool) { return 1; }
     471             : 
     472             :   unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
     473             : 
     474             :   bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) {
     475             :     return false;
     476             :   }
     477             : 
     478             :   unsigned getAtomicMemIntrinsicMaxElementSize() const {
     479             :     // Note for overrides: You must ensure for all element unordered-atomic
     480             :     // memory intrinsics that all power-of-2 element sizes up to, and
     481             :     // including, the return value of this method have a corresponding
     482             :     // runtime lib call. These runtime lib call definitions can be found
     483             :     // in RuntimeLibcalls.h
     484             :     return 0;
     485             :   }
     486             : 
     487             :   Value *getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
     488             :                                            Type *ExpectedType) {
     489             :     return nullptr;
     490             :   }
     491             : 
     492             :   Type *getMemcpyLoopLoweringType(LLVMContext &Context, Value *Length,
     493             :                                   unsigned SrcAlign, unsigned DestAlign) const {
     494          15 :     return Type::getInt8Ty(Context);
     495             :   }
     496             : 
     497           0 :   void getMemcpyLoopResidualLoweringType(SmallVectorImpl<Type *> &OpsOut,
     498             :                                          LLVMContext &Context,
     499             :                                          unsigned RemainingBytes,
     500             :                                          unsigned SrcAlign,
     501             :                                          unsigned DestAlign) const {
     502           0 :     for (unsigned i = 0; i != RemainingBytes; ++i)
     503           0 :       OpsOut.push_back(Type::getInt8Ty(Context));
     504           0 :   }
     505             : 
     506        1006 :   bool areInlineCompatible(const Function *Caller,
     507             :                            const Function *Callee) const {
     508             :     return (Caller->getFnAttribute("target-cpu") ==
     509        2010 :             Callee->getFnAttribute("target-cpu")) &&
     510             :            (Caller->getFnAttribute("target-features") ==
     511        1006 :             Callee->getFnAttribute("target-features"));
     512             :   }
     513             : 
     514             :   unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { return 128; }
     515             : 
     516             :   bool isLegalToVectorizeLoad(LoadInst *LI) const { return true; }
     517             : 
     518             :   bool isLegalToVectorizeStore(StoreInst *SI) const { return true; }
     519             : 
     520             :   bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
     521             :                                    unsigned Alignment,
     522             :                                    unsigned AddrSpace) const {
     523             :     return true;
     524             :   }
     525             : 
     526             :   bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
     527             :                                     unsigned Alignment,
     528             :                                     unsigned AddrSpace) const {
     529             :     return true;
     530             :   }
     531             : 
     532             :   unsigned getLoadVectorFactor(unsigned VF, unsigned LoadSize,
     533             :                                unsigned ChainSizeInBytes,
     534             :                                VectorType *VecTy) const {
     535             :     return VF;
     536             :   }
     537             : 
     538             :   unsigned getStoreVectorFactor(unsigned VF, unsigned StoreSize,
     539             :                                 unsigned ChainSizeInBytes,
     540             :                                 VectorType *VecTy) const {
     541             :     return VF;
     542             :   }
     543             : 
     544             :   bool useReductionIntrinsic(unsigned Opcode, Type *Ty,
     545             :                              TTI::ReductionFlags Flags) const {
     546             :     return false;
     547             :   }
     548             : 
     549             :   bool shouldExpandReduction(const IntrinsicInst *II) const {
     550             :     return true;
     551             :   }
     552             : 
     553             : protected:
     554             :   // Obtain the minimum required size to hold the value (without the sign)
     555             :   // In case of a vector it returns the min required size for one element.
     556          42 :   unsigned minRequiredElementSize(const Value* Val, bool &isSigned) {
     557          42 :     if (isa<ConstantDataVector>(Val) || isa<ConstantVector>(Val)) {
     558             :       const auto* VectorValue = cast<Constant>(Val);
     559             : 
     560             :       // In case of a vector need to pick the max between the min
     561             :       // required size for each element
     562          12 :       auto *VT = cast<VectorType>(Val->getType());
     563             : 
     564             :       // Assume unsigned elements
     565          12 :       isSigned = false;
     566             : 
     567             :       // The max required size is the total vector width divided by num
     568             :       // of elements in the vector
     569          12 :       unsigned MaxRequiredSize = VT->getBitWidth() / VT->getNumElements();
     570             : 
     571          12 :       unsigned MinRequiredSize = 0;
     572         108 :       for(unsigned i = 0, e = VT->getNumElements(); i < e; ++i) {
     573             :         if (auto* IntElement =
     574          48 :               dyn_cast<ConstantInt>(VectorValue->getAggregateElement(i))) {
     575          48 :           bool signedElement = IntElement->getValue().isNegative();
     576             :           // Get the element min required size.
     577             :           unsigned ElementMinRequiredSize =
     578          48 :             IntElement->getValue().getMinSignedBits() - 1;
     579             :           // In case one element is signed then all the vector is signed.
     580          48 :           isSigned |= signedElement;
     581             :           // Save the max required bit size between all the elements.
     582          48 :           MinRequiredSize = std::max(MinRequiredSize, ElementMinRequiredSize);
     583             :         }
     584             :         else {
     585             :           // not an int constant element
     586             :           return MaxRequiredSize;
     587             :         }
     588             :       }
     589          12 :       return MinRequiredSize;
     590             :     }
     591             : 
     592             :     if (const auto* CI = dyn_cast<ConstantInt>(Val)) {
     593           0 :       isSigned = CI->getValue().isNegative();
     594           0 :       return CI->getValue().getMinSignedBits() - 1;
     595             :     }
     596             : 
     597             :     if (const auto* Cast = dyn_cast<SExtInst>(Val)) {
     598           6 :       isSigned = true;
     599           6 :       return Cast->getSrcTy()->getScalarSizeInBits() - 1;
     600             :     }
     601             : 
     602             :     if (const auto* Cast = dyn_cast<ZExtInst>(Val)) {
     603           6 :       isSigned = false;
     604           6 :       return Cast->getSrcTy()->getScalarSizeInBits();
     605             :     }
     606             : 
     607          18 :     isSigned = false;
     608          18 :     return Val->getType()->getScalarSizeInBits();
     609             :   }
     610             : 
     611             :   bool isStridedAccess(const SCEV *Ptr) {
     612         550 :     return Ptr && isa<SCEVAddRecExpr>(Ptr);
     613             :   }
     614             : 
     615         147 :   const SCEVConstant *getConstantStrideStep(ScalarEvolution *SE,
     616             :                                             const SCEV *Ptr) {
     617             :     if (!isStridedAccess(Ptr))
     618             :       return nullptr;
     619             :     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ptr);
     620         130 :     return dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(*SE));
     621             :   }
     622             : 
     623          36 :   bool isConstantStridedAccessLessThan(ScalarEvolution *SE, const SCEV *Ptr,
     624             :                                        int64_t MergeDistance) {
     625          36 :     const SCEVConstant *Step = getConstantStrideStep(SE, Ptr);
     626          36 :     if (!Step)
     627             :       return false;
     628             :     APInt StrideVal = Step->getAPInt();
     629          19 :     if (StrideVal.getBitWidth() > 64)
     630             :       return false;
     631             :     // FIXME: Need to take absolute value for negative stride case.
     632          19 :     return StrideVal.getSExtValue() < MergeDistance;
     633             :   }
     634             : };
     635             : 
     636             : /// \brief CRTP base class for use as a mix-in that aids implementing
     637             : /// a TargetTransformInfo-compatible class.
     638             : template <typename T>
     639             : class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
     640             : private:
     641             :   typedef TargetTransformInfoImplBase BaseT;
     642             : 
     643             : protected:
     644             :   explicit TargetTransformInfoImplCRTPBase(const DataLayout &DL) : BaseT(DL) {}
     645             : 
     646             : public:
     647             :   using BaseT::getCallCost;
     648             : 
     649     1293592 :   unsigned getCallCost(const Function *F, int NumArgs) {
     650             :     assert(F && "A concrete function must be provided to this routine.");
     651             : 
     652     1293592 :     if (NumArgs < 0)
     653             :       // Set the argument number to the number of explicit arguments in the
     654             :       // function.
     655           0 :       NumArgs = F->arg_size();
     656             : 
     657     1293592 :     if (Intrinsic::ID IID = F->getIntrinsicID()) {
     658             :       FunctionType *FTy = F->getFunctionType();
     659      391783 :       SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
     660             :       return static_cast<T *>(this)
     661      391783 :           ->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys);
     662             :     }
     663             : 
     664      901809 :     if (!static_cast<T *>(this)->isLoweredToCall(F))
     665             :       return TTI::TCC_Basic; // Give a basic cost if it will be lowered
     666             :                              // directly.
     667             : 
     668      901780 :     return static_cast<T *>(this)->getCallCost(F->getFunctionType(), NumArgs);
     669             :   }
     670             : 
     671             :   unsigned getCallCost(const Function *F, ArrayRef<const Value *> Arguments) {
     672             :     // Simply delegate to generic handling of the call.
     673             :     // FIXME: We should use instsimplify or something else to catch calls which
     674             :     // will constant fold with these arguments.
     675     1293592 :     return static_cast<T *>(this)->getCallCost(F, Arguments.size());
     676             :   }
     677             : 
     678             :   using BaseT::getGEPCost;
     679             : 
     680      375477 :   int getGEPCost(Type *PointeeType, const Value *Ptr,
     681             :                  ArrayRef<const Value *> Operands) {
     682             :     const GlobalValue *BaseGV = nullptr;
     683      375477 :     if (Ptr != nullptr) {
     684             :       // TODO: will remove this when pointers have an opaque type.
     685             :       assert(Ptr->getType()->getScalarType()->getPointerElementType() ==
     686             :                  PointeeType &&
     687             :              "explicit pointee type doesn't match operand's pointee type");
     688      375477 :       BaseGV = dyn_cast<GlobalValue>(Ptr->stripPointerCasts());
     689             :     }
     690      375477 :     bool HasBaseReg = (BaseGV == nullptr);
     691             : 
     692      375477 :     auto PtrSizeBits = DL.getPointerTypeSizeInBits(Ptr->getType());
     693             :     APInt BaseOffset(PtrSizeBits, 0);
     694             :     int64_t Scale = 0;
     695             : 
     696             :     auto GTI = gep_type_begin(PointeeType, Operands);
     697             :     Type *TargetType = nullptr;
     698             : 
     699             :     // Handle the case where the GEP instruction has a single operand,
     700             :     // the basis, therefore TargetType is a nullptr.
     701      375477 :     if (Operands.empty())
     702           1 :       return !BaseGV ? TTI::TCC_Free : TTI::TCC_Basic;
     703             : 
     704     1845246 :     for (auto I = Operands.begin(); I != Operands.end(); ++I, ++GTI) {
     705      740149 :       TargetType = GTI.getIndexedType();
     706             :       // We assume that the cost of Scalar GEP with constant index and the
     707             :       // cost of Vector GEP with splat constant index are the same.
     708      740149 :       const ConstantInt *ConstIdx = dyn_cast<ConstantInt>(*I);
     709             :       if (!ConstIdx)
     710      324447 :         if (auto Splat = getSplatValue(*I))
     711             :           ConstIdx = dyn_cast<ConstantInt>(Splat);
     712       91385 :       if (StructType *STy = GTI.getStructTypeOrNull()) {
     713             :         // For structures the index is always splat or scalar constant
     714             :         assert(ConstIdx && "Unexpected GEP index");
     715             :         uint64_t Field = ConstIdx->getZExtValue();
     716      182770 :         BaseOffset += DL.getStructLayout(STy)->getElementOffset(Field);
     717             :       } else {
     718      648764 :         int64_t ElementSize = DL.getTypeAllocSize(GTI.getIndexedType());
     719      648764 :         if (ConstIdx) {
     720     1305280 :           BaseOffset +=
     721             :               ConstIdx->getValue().sextOrTrunc(PtrSizeBits) * ElementSize;
     722             :         } else {
     723             :           // Needs scale register.
     724      322444 :           if (Scale != 0)
     725             :             // No addressing mode takes two scale registers.
     726             :             return TTI::TCC_Basic;
     727             :           Scale = ElementSize;
     728             :         }
     729             :       }
     730             :     }
     731             : 
     732             :     // Assumes the address space is 0 when Ptr is nullptr.
     733             :     unsigned AS =
     734      360674 :         (Ptr == nullptr ? 0 : Ptr->getType()->getPointerAddressSpace());
     735             : 
     736      740424 :     if (static_cast<T *>(this)->isLegalAddressingMode(
     737             :             TargetType, const_cast<GlobalValue *>(BaseGV),
     738             :             BaseOffset.sextOrTrunc(64).getSExtValue(), HasBaseReg, Scale, AS))
     739             :       return TTI::TCC_Free;
     740      167072 :     return TTI::TCC_Basic;
     741             :   }
     742             : 
     743             :   using BaseT::getIntrinsicCost;
     744             : 
     745           4 :   unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
     746             :                             ArrayRef<const Value *> Arguments) {
     747             :     // Delegate to the generic intrinsic handling code. This mostly provides an
     748             :     // opportunity for targets to (for example) special case the cost of
     749             :     // certain intrinsics based on constants used as arguments.
     750             :     SmallVector<Type *, 8> ParamTys;
     751           4 :     ParamTys.reserve(Arguments.size());
     752          12 :     for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
     753          16 :       ParamTys.push_back(Arguments[Idx]->getType());
     754           8 :     return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys);
     755             :   }
     756             : 
     757     3132373 :   unsigned getUserCost(const User *U, ArrayRef<const Value *> Operands) {
     758             :     if (isa<PHINode>(U))
     759             :       return TTI::TCC_Free; // Model all PHI nodes as free.
     760             : 
     761             :     // Static alloca doesn't generate target instructions.
     762             :     if (auto *A = dyn_cast<AllocaInst>(U))
     763         430 :       if (A->isStaticAlloca())
     764             :         return TTI::TCC_Free;
     765             : 
     766             :     if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
     767             :       return static_cast<T *>(this)->getGEPCost(GEP->getSourceElementType(),
     768             :                                                 GEP->getPointerOperand(),
     769      686000 :                                                 Operands.drop_front());
     770             :     }
     771             : 
     772     2726268 :     if (auto CS = ImmutableCallSite(U)) {
     773             :       const Function *F = CS.getCalledFunction();
     774             :       if (!F) {
     775             :         // Just use the called value type.
     776       17479 :         Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
     777             :         return static_cast<T *>(this)
     778       34958 :             ->getCallCost(cast<FunctionType>(FTy), CS.arg_size());
     779             :       }
     780             : 
     781     1293592 :       SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
     782             :       return static_cast<T *>(this)->getCallCost(F, Arguments);
     783             :     }
     784             : 
     785             :     if (const CastInst *CI = dyn_cast<CastInst>(U)) {
     786             :       // Result of a cmp instruction is often extended (to be used by other
     787             :       // cmp instructions, logical or return instructions). These are usually
     788             :       // nop on most sane targets.
     789             :       if (isa<CmpInst>(CI->getOperand(0)))
     790             :         return TTI::TCC_Free;
     791       85542 :       if (isa<SExtInst>(CI) || isa<ZExtInst>(CI) || isa<FPExtInst>(CI))
     792       35980 :         return static_cast<T *>(this)->getExtCost(CI, Operands.back());
     793             :     }
     794             : 
     795             :     return static_cast<T *>(this)->getOperationCost(
     796             :         Operator::getOpcode(U), U->getType(),
     797     4758245 :         U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);
     798             :   }
     799             : 
     800          10 :   int getInstructionLatency(const Instruction *I) {
     801          10 :     SmallVector<const Value *, 4> Operands(I->value_op_begin(),
     802             :                                            I->value_op_end());
     803          10 :     if (getUserCost(I, Operands) == TTI::TCC_Free)
     804             :       return 0;
     805             : 
     806           5 :     if (isa<LoadInst>(I))
     807             :       return 4;
     808             : 
     809           5 :     Type *DstTy = I->getType();
     810             : 
     811             :     // Usually an intrinsic is a simple instruction.
     812             :     // A real function call is much slower.
     813             :     if (auto *CI = dyn_cast<CallInst>(I)) {
     814             :       const Function *F = CI->getCalledFunction();
     815           1 :       if (!F || static_cast<T *>(this)->isLoweredToCall(F))
     816             :         return 40;
     817             :       // Some intrinsics return a value and a flag, we use the value type
     818             :       // to decide its latency.
     819             :       if (StructType* StructTy = dyn_cast<StructType>(DstTy))
     820           1 :         DstTy = StructTy->getElementType(0);
     821             :       // Fall through to simple instructions.
     822             :     }
     823             : 
     824             :     if (VectorType *VectorTy = dyn_cast<VectorType>(DstTy))
     825           0 :       DstTy = VectorTy->getElementType();
     826             :     if (DstTy->isFloatingPointTy())
     827             :       return 3;
     828             : 
     829             :     return 1;
     830             :   }
     831             : };
     832             : }
     833             : 
     834             : #endif

Generated by: LCOV version 1.13