LCOV - code coverage report
Current view: top level - include/llvm/Analysis - TargetTransformInfoImpl.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 160 174 92.0 %
Date: 2017-09-14 15:23:50 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     1705848 :   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     1705848 :       : DL(Arg.DL) {}
      44     1717926 :   TargetTransformInfoImplBase(TargetTransformInfoImplBase &&Arg) : DL(Arg.DL) {}
      45             : 
      46        1251 :   const DataLayout &getDataLayout() const { return DL; }
      47             : 
      48      907493 :   unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
      49      907493 :     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       16012 :     case Instruction::BitCast:
      58             :       assert(OpTy && "Cast instructions must provide the operand type");
      59       47970 :       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        1020 :     case Instruction::FDiv:
      67             :     case Instruction::FRem:
      68             :     case Instruction::SDiv:
      69             :     case Instruction::SRem:
      70             :     case Instruction::UDiv:
      71             :     case Instruction::URem:
      72        1020 :       return TTI::TCC_Expensive;
      73             : 
      74        7763 :     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        7763 :       unsigned OpSize = OpTy->getScalarSizeInBits();
      78       23286 :       if (DL.isLegalInteger(OpSize) &&
      79        7760 :           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       17095 :     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       17095 :       unsigned DestSize = Ty->getScalarSizeInBits();
      89       51276 :       if (DL.isLegalInteger(DestSize) &&
      90       17086 :           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          31 :     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          62 :       if (DL.isLegalInteger(DL.getTypeSizeInBits(Ty)))
     100             :         return TTI::TCC_Free;
     101             : 
     102          22 :       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           2 :     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      840158 :     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      840158 :     return TTI::TCC_Basic * (NumArgs + 1);
     140             :   }
     141             : 
     142             :   unsigned getInliningThresholdMultiplier() { return 1; }
     143             : 
     144      280976 :   unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
     145             :                             ArrayRef<Type *> ParamTys) {
     146      280976 :     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      278533 :     case Intrinsic::annotation:
     154             :     case Intrinsic::assume:
     155             :     case Intrinsic::dbg_declare:
     156             :     case Intrinsic::dbg_value:
     157             :     case Intrinsic::invariant_start:
     158             :     case Intrinsic::invariant_end:
     159             :     case Intrinsic::lifetime_start:
     160             :     case Intrinsic::lifetime_end:
     161             :     case Intrinsic::objectsize:
     162             :     case Intrinsic::ptr_annotation:
     163             :     case Intrinsic::var_annotation:
     164             :     case Intrinsic::experimental_gc_result:
     165             :     case Intrinsic::experimental_gc_relocate:
     166             :     case Intrinsic::coro_alloc:
     167             :     case Intrinsic::coro_begin:
     168             :     case Intrinsic::coro_free:
     169             :     case Intrinsic::coro_end:
     170             :     case Intrinsic::coro_frame:
     171             :     case Intrinsic::coro_size:
     172             :     case Intrinsic::coro_suspend:
     173             :     case Intrinsic::coro_param:
     174             :     case Intrinsic::coro_subfn_addr:
     175             :       // These intrinsics don't actually represent code after lowering.
     176             :       return TTI::TCC_Free;
     177             :     }
     178             :   }
     179             : 
     180             :   bool hasBranchDivergence() { return false; }
     181             : 
     182             :   bool isSourceOfDivergence(const Value *V) { return false; }
     183             : 
     184             :   bool isAlwaysUniform(const Value *V) { return false; }
     185             : 
     186             :   unsigned getFlatAddressSpace () {
     187             :     return -1;
     188             :   }
     189             : 
     190     1746931 :   bool isLoweredToCall(const Function *F) {
     191             :     // FIXME: These should almost certainly not be handled here, and instead
     192             :     // handled with the help of TLI or the target itself. This was largely
     193             :     // ported from existing analysis heuristics here so that such refactorings
     194             :     // can take place in the future.
     195             : 
     196     1746931 :     if (F->isIntrinsic())
     197             :       return false;
     198             : 
     199     4016324 :     if (F->hasLocalLinkage() || !F->hasName())
     200             :       return true;
     201             : 
     202     1183702 :     StringRef Name = F->getName();
     203             : 
     204             :     // These will all likely lower to a single selection DAG node.
     205     3551106 :     if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
     206     4734803 :         Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
     207     3551082 :         Name == "fmin" || Name == "fminf" || Name == "fminl" ||
     208     3551082 :         Name == "fmax" || Name == "fmaxf" || Name == "fmaxl" ||
     209     4734776 :         Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
     210     4734768 :         Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
     211             :       return false;
     212             : 
     213             :     // These are all likely to be optimized into something smaller.
     214     4734760 :     if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
     215     3551070 :         Name == "exp2l" || Name == "exp2f" || Name == "floor" ||
     216     3551070 :         Name == "floorf" || Name == "ceil" || Name == "round" ||
     217     4734760 :         Name == "ffs" || Name == "ffsl" || Name == "abs" || Name == "labs" ||
     218     1183690 :         Name == "llabs")
     219             :       return false;
     220             : 
     221             :     return true;
     222             :   }
     223             : 
     224             :   void getUnrollingPreferences(Loop *, ScalarEvolution &,
     225             :                                TTI::UnrollingPreferences &) {}
     226             : 
     227             :   bool isLegalAddImmediate(int64_t Imm) { return false; }
     228             : 
     229             :   bool isLegalICmpImmediate(int64_t Imm) { return false; }
     230             : 
     231             :   bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
     232             :                              bool HasBaseReg, int64_t Scale,
     233             :                              unsigned AddrSpace, Instruction *I = nullptr) {
     234             :     // Guess that only reg and reg+reg addressing is allowed. This heuristic is
     235             :     // taken from the implementation of LSR.
     236       13866 :     return !BaseGV && BaseOffset == 0 && (Scale == 0 || Scale == 1);
     237             :   }
     238             : 
     239       12339 :   bool isLSRCostLess(TTI::LSRCost &C1, TTI::LSRCost &C2) {
     240       12339 :     return std::tie(C1.NumRegs, C1.AddRecCost, C1.NumIVMuls, C1.NumBaseAdds,
     241             :                     C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
     242       12339 :            std::tie(C2.NumRegs, C2.AddRecCost, C2.NumIVMuls, C2.NumBaseAdds,
     243       61695 :                     C2.ScaleCost, C2.ImmCost, C2.SetupCost);
     244             :   }
     245             : 
     246             :   bool isLegalMaskedStore(Type *DataType) { return false; }
     247             : 
     248             :   bool isLegalMaskedLoad(Type *DataType) { return false; }
     249             : 
     250             :   bool isLegalMaskedScatter(Type *DataType) { return false; }
     251             : 
     252             :   bool isLegalMaskedGather(Type *DataType) { return false; }
     253             : 
     254             :   bool hasDivRemOp(Type *DataType, bool IsSigned) { return false; }
     255             : 
     256             :   bool prefersVectorizedAddressing() { return true; }
     257             : 
     258             :   int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
     259             :                            bool HasBaseReg, int64_t Scale, unsigned AddrSpace) {
     260             :     // Guess that all legal addressing mode are free.
     261        1066 :     if (isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
     262             :                               Scale, AddrSpace))
     263             :       return 0;
     264             :     return -1;
     265             :   }
     266             : 
     267             :   bool LSRWithInstrQueries() { return false; }
     268             : 
     269             :   bool isTruncateFree(Type *Ty1, Type *Ty2) { return false; }
     270             : 
     271             :   bool isProfitableToHoist(Instruction *I) { return true; }
     272             : 
     273             :   bool isTypeLegal(Type *Ty) { return false; }
     274             : 
     275             :   unsigned getJumpBufAlignment() { return 0; }
     276             : 
     277             :   unsigned getJumpBufSize() { return 0; }
     278             : 
     279             :   bool shouldBuildLookupTables() { return true; }
     280             :   bool shouldBuildLookupTablesForConstant(Constant *C) { return true; }
     281             : 
     282             :   unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
     283             :     return 0;
     284             :   }
     285             : 
     286             :   unsigned getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
     287             :                                             unsigned VF) { return 0; }
     288             : 
     289             :   bool supportsEfficientVectorElementLoadStore() { return false; }
     290             : 
     291             :   bool enableAggressiveInterleaving(bool LoopHasReductions) { return false; }
     292             : 
     293             :   bool expandMemCmp(Instruction *I, unsigned &MaxLoadSize) { return false; }
     294             : 
     295             :   bool enableInterleavedAccessVectorization() { return false; }
     296             : 
     297             :   bool isFPVectorizationPotentiallyUnsafe() { return false; }
     298             : 
     299             :   bool allowsMisalignedMemoryAccesses(LLVMContext &Context,
     300             :                                       unsigned BitWidth,
     301             :                                       unsigned AddressSpace,
     302             :                                       unsigned Alignment,
     303             :                                       bool *Fast) { return false; }
     304             : 
     305             :   TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) {
     306             :     return TTI::PSK_Software;
     307             :   }
     308             : 
     309             :   bool haveFastSqrt(Type *Ty) { return false; }
     310             : 
     311             :   unsigned getFPOpCost(Type *Ty) { return TargetTransformInfo::TCC_Basic; }
     312             : 
     313             :   int getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
     314             :                             Type *Ty) {
     315             :     return 0;
     316             :   }
     317             : 
     318             :   unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
     319             : 
     320             :   unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
     321             :                          Type *Ty) {
     322             :     return TTI::TCC_Free;
     323             :   }
     324             : 
     325             :   unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
     326             :                          Type *Ty) {
     327             :     return TTI::TCC_Free;
     328             :   }
     329             : 
     330             :   unsigned getNumberOfRegisters(bool Vector) { return 8; }
     331             : 
     332             :   unsigned getRegisterBitWidth(bool Vector) const { return 32; }
     333             : 
     334             :   unsigned getMinVectorRegisterBitWidth() { return 128; }
     335             : 
     336             :   bool
     337             :   shouldConsiderAddressTypePromotion(const Instruction &I,
     338             :                                      bool &AllowPromotionWithoutCommonHeader) {
     339       23211 :     AllowPromotionWithoutCommonHeader = false;
     340             :     return false;
     341             :   }
     342             : 
     343             :   unsigned getCacheLineSize() { return 0; }
     344             : 
     345             :   llvm::Optional<unsigned> getCacheSize(TargetTransformInfo::CacheLevel Level) {
     346           4 :     switch (Level) {
     347           4 :     case TargetTransformInfo::CacheLevel::L1D:
     348             :       LLVM_FALLTHROUGH;
     349             :     case TargetTransformInfo::CacheLevel::L2D:
     350           4 :       return llvm::Optional<unsigned>();
     351             :     }
     352             : 
     353           0 :     llvm_unreachable("Unknown TargetTransformInfo::CacheLevel");
     354             :   }
     355             : 
     356             :   llvm::Optional<unsigned> getCacheAssociativity(
     357             :     TargetTransformInfo::CacheLevel Level) {
     358           2 :     switch (Level) {
     359           2 :     case TargetTransformInfo::CacheLevel::L1D:
     360             :       LLVM_FALLTHROUGH;
     361             :     case TargetTransformInfo::CacheLevel::L2D:
     362           2 :       return llvm::Optional<unsigned>();
     363             :     }
     364             : 
     365           0 :     llvm_unreachable("Unknown TargetTransformInfo::CacheLevel");
     366             :   }
     367             : 
     368             :   unsigned getPrefetchDistance() { return 0; }
     369             : 
     370             :   unsigned getMinPrefetchStride() { return 1; }
     371             : 
     372             :   unsigned getMaxPrefetchIterationsAhead() { return UINT_MAX; }
     373             : 
     374             :   unsigned getMaxInterleaveFactor(unsigned VF) { return 1; }
     375             : 
     376             :   unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
     377             :                                   TTI::OperandValueKind Opd1Info,
     378             :                                   TTI::OperandValueKind Opd2Info,
     379             :                                   TTI::OperandValueProperties Opd1PropInfo,
     380             :                                   TTI::OperandValueProperties Opd2PropInfo,
     381             :                                   ArrayRef<const Value *> Args) {
     382             :     return 1;
     383             :   }
     384             : 
     385             :   unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index,
     386             :                           Type *SubTp) {
     387             :     return 1;
     388             :   }
     389             : 
     390             :   unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
     391             :                             const Instruction *I) { return 1; }
     392             : 
     393             :   unsigned getExtractWithExtendCost(unsigned Opcode, Type *Dst,
     394             :                                     VectorType *VecTy, unsigned Index) {
     395             :     return 1;
     396             :   }
     397             : 
     398             :   unsigned getCFInstrCost(unsigned Opcode) { return 1; }
     399             : 
     400             :   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
     401             :                               const Instruction *I) {
     402             :     return 1;
     403             :   }
     404             : 
     405             :   unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
     406             :     return 1;
     407             :   }
     408             : 
     409             :   unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
     410             :                            unsigned AddressSpace, const Instruction *I) {
     411             :     return 1;
     412             :   }
     413             : 
     414             :   unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
     415             :                                  unsigned AddressSpace) {
     416             :     return 1;
     417             :   }
     418             : 
     419             :   unsigned getGatherScatterOpCost(unsigned Opcode, Type *DataTy, Value *Ptr,
     420             :                                   bool VariableMask,
     421             :                                   unsigned Alignment) {
     422             :     return 1;
     423             :   }
     424             : 
     425             :   unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
     426             :                                       unsigned Factor,
     427             :                                       ArrayRef<unsigned> Indices,
     428             :                                       unsigned Alignment,
     429             :                                       unsigned AddressSpace) {
     430             :     return 1;
     431             :   }
     432             : 
     433             :   unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
     434             :                                  ArrayRef<Type *> Tys, FastMathFlags FMF,
     435             :                                  unsigned ScalarizationCostPassed) {
     436             :     return 1;
     437             :   }
     438             :   unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
     439             :             ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) {
     440             :     return 1;
     441             :   }
     442             : 
     443             :   unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
     444             :     return 1;
     445             :   }
     446             : 
     447             :   unsigned getNumberOfParts(Type *Tp) { return 0; }
     448             : 
     449             :   unsigned getAddressComputationCost(Type *Tp, ScalarEvolution *,
     450             :                                      const SCEV *) {
     451             :     return 0;
     452             :   }
     453             : 
     454             :   unsigned getArithmeticReductionCost(unsigned, Type *, bool) { return 1; }
     455             : 
     456             :   unsigned getMinMaxReductionCost(Type *, Type *, bool, bool) { return 1; }
     457             : 
     458             :   unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
     459             : 
     460             :   bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) {
     461             :     return false;
     462             :   }
     463             : 
     464             :   unsigned getAtomicMemIntrinsicMaxElementSize() const {
     465             :     // Note for overrides: You must ensure for all element unordered-atomic
     466             :     // memory intrinsics that all power-of-2 element sizes up to, and
     467             :     // including, the return value of this method have a corresponding
     468             :     // runtime lib call. These runtime lib call definitions can be found
     469             :     // in RuntimeLibcalls.h
     470             :     return 0;
     471             :   }
     472             : 
     473             :   Value *getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
     474             :                                            Type *ExpectedType) {
     475             :     return nullptr;
     476             :   }
     477             : 
     478             :   Type *getMemcpyLoopLoweringType(LLVMContext &Context, Value *Length,
     479             :                                   unsigned SrcAlign, unsigned DestAlign) const {
     480          11 :     return Type::getInt8Ty(Context);
     481             :   }
     482             : 
     483           0 :   void getMemcpyLoopResidualLoweringType(SmallVectorImpl<Type *> &OpsOut,
     484             :                                          LLVMContext &Context,
     485             :                                          unsigned RemainingBytes,
     486             :                                          unsigned SrcAlign,
     487             :                                          unsigned DestAlign) const {
     488           0 :     for (unsigned i = 0; i != RemainingBytes; ++i)
     489           0 :       OpsOut.push_back(Type::getInt8Ty(Context));
     490           0 :   }
     491             : 
     492         968 :   bool areInlineCompatible(const Function *Caller,
     493             :                            const Function *Callee) const {
     494        2904 :     return (Caller->getFnAttribute("target-cpu") ==
     495        1934 :             Callee->getFnAttribute("target-cpu")) &&
     496        2898 :            (Caller->getFnAttribute("target-features") ==
     497         968 :             Callee->getFnAttribute("target-features"));
     498             :   }
     499             : 
     500             :   unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { return 128; }
     501             : 
     502             :   bool isLegalToVectorizeLoad(LoadInst *LI) const { return true; }
     503             : 
     504             :   bool isLegalToVectorizeStore(StoreInst *SI) const { return true; }
     505             : 
     506             :   bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
     507             :                                    unsigned Alignment,
     508             :                                    unsigned AddrSpace) const {
     509             :     return true;
     510             :   }
     511             : 
     512             :   bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
     513             :                                     unsigned Alignment,
     514             :                                     unsigned AddrSpace) const {
     515             :     return true;
     516             :   }
     517             : 
     518             :   unsigned getLoadVectorFactor(unsigned VF, unsigned LoadSize,
     519             :                                unsigned ChainSizeInBytes,
     520             :                                VectorType *VecTy) const {
     521             :     return VF;
     522             :   }
     523             : 
     524             :   unsigned getStoreVectorFactor(unsigned VF, unsigned StoreSize,
     525             :                                 unsigned ChainSizeInBytes,
     526             :                                 VectorType *VecTy) const {
     527             :     return VF;
     528             :   }
     529             : 
     530             :   bool useReductionIntrinsic(unsigned Opcode, Type *Ty,
     531             :                              TTI::ReductionFlags Flags) const {
     532             :     return false;
     533             :   }
     534             : 
     535             :   bool shouldExpandReduction(const IntrinsicInst *II) const {
     536             :     return true;
     537             :   }
     538             : 
     539             : protected:
     540             :   // Obtain the minimum required size to hold the value (without the sign)
     541             :   // In case of a vector it returns the min required size for one element.
     542          42 :   unsigned minRequiredElementSize(const Value* Val, bool &isSigned) {
     543         114 :     if (isa<ConstantDataVector>(Val) || isa<ConstantVector>(Val)) {
     544          24 :       const auto* VectorValue = cast<Constant>(Val);
     545             : 
     546             :       // In case of a vector need to pick the max between the min
     547             :       // required size for each element
     548          24 :       auto *VT = cast<VectorType>(Val->getType());
     549             : 
     550             :       // Assume unsigned elements
     551          12 :       isSigned = false;
     552             : 
     553             :       // The max required size is the total vector width divided by num
     554             :       // of elements in the vector
     555          12 :       unsigned MaxRequiredSize = VT->getBitWidth() / VT->getNumElements();
     556             : 
     557          12 :       unsigned MinRequiredSize = 0;
     558         120 :       for(unsigned i = 0, e = VT->getNumElements(); i < e; ++i) {
     559             :         if (auto* IntElement =
     560          96 :               dyn_cast<ConstantInt>(VectorValue->getAggregateElement(i))) {
     561          96 :           bool signedElement = IntElement->getValue().isNegative();
     562             :           // Get the element min required size.
     563             :           unsigned ElementMinRequiredSize =
     564          48 :             IntElement->getValue().getMinSignedBits() - 1;
     565             :           // In case one element is signed then all the vector is signed.
     566          48 :           isSigned |= signedElement;
     567             :           // Save the max required bit size between all the elements.
     568          48 :           MinRequiredSize = std::max(MinRequiredSize, ElementMinRequiredSize);
     569             :         }
     570             :         else {
     571             :           // not an int constant element
     572             :           return MaxRequiredSize;
     573             :         }
     574             :       }
     575          12 :       return MinRequiredSize;
     576             :     }
     577             : 
     578          30 :     if (const auto* CI = dyn_cast<ConstantInt>(Val)) {
     579           0 :       isSigned = CI->getValue().isNegative();
     580           0 :       return CI->getValue().getMinSignedBits() - 1;
     581             :     }
     582             : 
     583          36 :     if (const auto* Cast = dyn_cast<SExtInst>(Val)) {
     584           6 :       isSigned = true;
     585          12 :       return Cast->getSrcTy()->getScalarSizeInBits() - 1;
     586             :     }
     587             : 
     588          30 :     if (const auto* Cast = dyn_cast<ZExtInst>(Val)) {
     589           6 :       isSigned = false;
     590          12 :       return Cast->getSrcTy()->getScalarSizeInBits();
     591             :     }
     592             : 
     593          18 :     isSigned = false;
     594          18 :     return Val->getType()->getScalarSizeInBits();
     595             :   }
     596             : 
     597             :   bool isStridedAccess(const SCEV *Ptr) {
     598         540 :     return Ptr && isa<SCEVAddRecExpr>(Ptr);
     599             :   }
     600             : 
     601         138 :   const SCEVConstant *getConstantStrideStep(ScalarEvolution *SE,
     602             :                                             const SCEV *Ptr) {
     603         259 :     if (!isStridedAccess(Ptr))
     604             :       return nullptr;
     605         121 :     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ptr);
     606         121 :     return dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(*SE));
     607             :   }
     608             : 
     609          28 :   bool isConstantStridedAccessLessThan(ScalarEvolution *SE, const SCEV *Ptr,
     610             :                                        int64_t MergeDistance) {
     611          28 :     const SCEVConstant *Step = getConstantStrideStep(SE, Ptr);
     612          28 :     if (!Step)
     613             :       return false;
     614          22 :     APInt StrideVal = Step->getAPInt();
     615          11 :     if (StrideVal.getBitWidth() > 64)
     616             :       return false;
     617             :     // FIXME: Need to take absolute value for negative stride case.
     618          11 :     return StrideVal.getSExtValue() < MergeDistance;
     619             :   }
     620             : };
     621             : 
     622             : /// \brief CRTP base class for use as a mix-in that aids implementing
     623             : /// a TargetTransformInfo-compatible class.
     624             : template <typename T>
     625     6847548 : class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
     626             : private:
     627             :   typedef TargetTransformInfoImplBase BaseT;
     628             : 
     629             : protected:
     630     3423774 :   explicit TargetTransformInfoImplCRTPBase(const DataLayout &DL) : BaseT(DL) {}
     631             : 
     632             : public:
     633             :   using BaseT::getCallCost;
     634             : 
     635     1105499 :   unsigned getCallCost(const Function *F, int NumArgs) {
     636             :     assert(F && "A concrete function must be provided to this routine.");
     637             : 
     638     1105499 :     if (NumArgs < 0)
     639             :       // Set the argument number to the number of explicit arguments in the
     640             :       // function.
     641           0 :       NumArgs = F->arg_size();
     642             : 
     643     1105499 :     if (Intrinsic::ID IID = F->getIntrinsicID()) {
     644      281940 :       FunctionType *FTy = F->getFunctionType();
     645      845820 :       SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
     646             :       return static_cast<T *>(this)
     647      563880 :           ->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys);
     648             :     }
     649             : 
     650      823559 :     if (!static_cast<T *>(this)->isLoweredToCall(F))
     651             :       return TTI::TCC_Basic; // Give a basic cost if it will be lowered
     652             :                              // directly.
     653             : 
     654     2470647 :     return static_cast<T *>(this)->getCallCost(F->getFunctionType(), NumArgs);
     655             :   }
     656             : 
     657             :   unsigned getCallCost(const Function *F, ArrayRef<const Value *> Arguments) {
     658             :     // Simply delegate to generic handling of the call.
     659             :     // FIXME: We should use instsimplify or something else to catch calls which
     660             :     // will constant fold with these arguments.
     661     1105499 :     return static_cast<T *>(this)->getCallCost(F, Arguments.size());
     662             :   }
     663             : 
     664             :   using BaseT::getGEPCost;
     665             : 
     666      318677 :   int getGEPCost(Type *PointeeType, const Value *Ptr,
     667             :                  ArrayRef<const Value *> Operands) {
     668      318677 :     const GlobalValue *BaseGV = nullptr;
     669      318677 :     if (Ptr != nullptr) {
     670             :       // TODO: will remove this when pointers have an opaque type.
     671             :       assert(Ptr->getType()->getScalarType()->getPointerElementType() ==
     672             :                  PointeeType &&
     673             :              "explicit pointee type doesn't match operand's pointee type");
     674      318677 :       BaseGV = dyn_cast<GlobalValue>(Ptr->stripPointerCasts());
     675             :     }
     676      318677 :     bool HasBaseReg = (BaseGV == nullptr);
     677      318677 :     int64_t BaseOffset = 0;
     678      318677 :     int64_t Scale = 0;
     679             : 
     680      637354 :     auto GTI = gep_type_begin(PointeeType, Operands);
     681      318677 :     Type *TargetType = nullptr;
     682             : 
     683             :     // Handle the case where the GEP instruction has a single operand,
     684             :     // the basis, therefore TargetType is a nullptr.
     685      318677 :     if (Operands.empty())
     686           1 :       return !BaseGV ? TTI::TCC_Free : TTI::TCC_Basic;
     687             : 
     688     1560656 :     for (auto I = Operands.begin(); I != Operands.end(); ++I, ++GTI) {
     689      625702 :       TargetType = GTI.getIndexedType();
     690             :       // We assume that the cost of Scalar GEP with constant index and the
     691             :       // cost of Vector GEP with splat constant index are the same.
     692      625702 :       const ConstantInt *ConstIdx = dyn_cast<ConstantInt>(*I);
     693             :       if (!ConstIdx)
     694      293988 :         if (auto Splat = getSplatValue(*I))
     695             :           ConstIdx = dyn_cast<ConstantInt>(Splat);
     696       56836 :       if (StructType *STy = GTI.getStructTypeOrNull()) {
     697             :         // For structures the index is always splat or scalar constant
     698             :         assert(ConstIdx && "Unexpected GEP index");
     699       56836 :         uint64_t Field = ConstIdx->getZExtValue();
     700      113672 :         BaseOffset += DL.getStructLayout(STy)->getElementOffset(Field);
     701             :       } else {
     702      568866 :         int64_t ElementSize = DL.getTypeAllocSize(GTI.getIndexedType());
     703      568866 :         if (ConstIdx)
     704      276881 :           BaseOffset += ConstIdx->getSExtValue() * ElementSize;
     705             :         else {
     706             :           // Needs scale register.
     707      291985 :           if (Scale != 0)
     708             :             // No addressing mode takes two scale registers.
     709             :             return TTI::TCC_Basic;
     710             :           Scale = ElementSize;
     711             :         }
     712             :       }
     713             :     }
     714             : 
     715             :     // Assumes the address space is 0 when Ptr is nullptr.
     716      618767 :     unsigned AS =
     717      304803 :         (Ptr == nullptr ? 0 : Ptr->getType()->getPointerAddressSpace());
     718      618789 :     if (static_cast<T *>(this)->isLegalAddressingMode(
     719             :             TargetType, const_cast<GlobalValue *>(BaseGV), BaseOffset,
     720             :             HasBaseReg, Scale, AS))
     721             :       return TTI::TCC_Free;
     722      143783 :     return TTI::TCC_Basic;
     723             :   }
     724             : 
     725             :   using BaseT::getIntrinsicCost;
     726             : 
     727           4 :   unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
     728             :                             ArrayRef<const Value *> Arguments) {
     729             :     // Delegate to the generic intrinsic handling code. This mostly provides an
     730             :     // opportunity for targets to (for example) special case the cost of
     731             :     // certain intrinsics based on constants used as arguments.
     732           8 :     SmallVector<Type *, 8> ParamTys;
     733           4 :     ParamTys.reserve(Arguments.size());
     734          12 :     for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
     735          16 :       ParamTys.push_back(Arguments[Idx]->getType());
     736          12 :     return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys);
     737             :   }
     738             : 
     739     2381143 :   unsigned getUserCost(const User *U, ArrayRef<const Value *> Operands) {
     740     2381143 :     if (isa<PHINode>(U))
     741             :       return TTI::TCC_Free; // Model all PHI nodes as free.
     742             : 
     743     2634366 :     if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
     744             :       return static_cast<T *>(this)->getGEPCost(GEP->getSourceElementType(),
     745             :                                                 GEP->getPointerOperand(),
     746     1147599 :                                                 Operands.drop_front());
     747             :     }
     748             : 
     749     6167982 :     if (auto CS = ImmutableCallSite(U)) {
     750     1105499 :       const Function *F = CS.getCalledFunction();
     751             :       if (!F) {
     752             :         // Just use the called value type.
     753       33218 :         Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
     754             :         return static_cast<T *>(this)
     755       49827 :             ->getCallCost(cast<FunctionType>(FTy), CS.arg_size());
     756             :       }
     757             : 
     758     3316497 :       SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
     759     3316497 :       return static_cast<T *>(this)->getCallCost(F, Arguments);
     760             :     }
     761             : 
     762     1001471 :     if (const CastInst *CI = dyn_cast<CastInst>(U)) {
     763             :       // Result of a cmp instruction is often extended (to be used by other
     764             :       // cmp instructions, logical or return instructions). These are usually
     765             :       // nop on most sane targets.
     766      135170 :       if (isa<CmpInst>(CI->getOperand(0)))
     767             :         return TTI::TCC_Free;
     768      243111 :       if (isa<SExtInst>(CI) || isa<ZExtInst>(CI) || isa<FPExtInst>(CI))
     769       33848 :         return static_cast<T *>(this)->getExtCost(CI, Operands.back());
     770             :     }
     771             : 
     772             :     return static_cast<T *>(this)->getOperationCost(
     773             :         Operator::getOpcode(U), U->getType(),
     774     3101334 :         U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);
     775             :   }
     776             : 
     777           2 :   int getInstructionLatency(const Instruction *I) {
     778           4 :     if (isa<PHINode>(I))
     779             :       return 0;
     780             : 
     781           4 :     if (isa<CallInst>(I))
     782             :       return 40;
     783             : 
     784           4 :     if (isa<LoadInst>(I))
     785             :       return 4;
     786             : 
     787           2 :     Type *dstTy = I->getType();
     788           0 :     if (VectorType *VectorTy = dyn_cast<VectorType>(dstTy))
     789           0 :       dstTy = VectorTy->getElementType();
     790           2 :     if (dstTy->isFloatingPointTy())
     791             :       return 3;
     792             : 
     793             :     return 1;
     794             :   }
     795             : };
     796             : }
     797             : 
     798             : #endif

Generated by: LCOV version 1.13