LCOV - code coverage report
Current view: top level - include/llvm/Analysis - InlineCost.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 4 16 25.0 %
Date: 2018-10-20 13:21:21 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- InlineCost.h - Cost analysis for inliner -----------------*- 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             : //
      10             : // This file implements heuristics for inlining decisions.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #ifndef LLVM_ANALYSIS_INLINECOST_H
      15             : #define LLVM_ANALYSIS_INLINECOST_H
      16             : 
      17             : #include "llvm/Analysis/AssumptionCache.h"
      18             : #include "llvm/Analysis/CallGraphSCCPass.h"
      19             : #include "llvm/Analysis/OptimizationRemarkEmitter.h"
      20             : #include <cassert>
      21             : #include <climits>
      22             : 
      23             : namespace llvm {
      24             : class AssumptionCacheTracker;
      25             : class BlockFrequencyInfo;
      26             : class CallSite;
      27             : class DataLayout;
      28             : class Function;
      29             : class ProfileSummaryInfo;
      30             : class TargetTransformInfo;
      31             : 
      32             : namespace InlineConstants {
      33             : // Various thresholds used by inline cost analysis.
      34             : /// Use when optsize (-Os) is specified.
      35             : const int OptSizeThreshold = 50;
      36             : 
      37             : /// Use when minsize (-Oz) is specified.
      38             : const int OptMinSizeThreshold = 5;
      39             : 
      40             : /// Use when -O3 is specified.
      41             : const int OptAggressiveThreshold = 250;
      42             : 
      43             : // Various magic constants used to adjust heuristics.
      44             : const int InstrCost = 5;
      45             : const int IndirectCallThreshold = 100;
      46             : const int CallPenalty = 25;
      47             : const int LastCallToStaticBonus = 15000;
      48             : const int ColdccPenalty = 2000;
      49             : const int NoreturnPenalty = 10000;
      50             : /// Do not inline functions which allocate this many bytes on the stack
      51             : /// when the caller is recursive.
      52             : const unsigned TotalAllocaSizeRecursiveCaller = 1024;
      53             : }
      54             : 
      55             : /// Represents the cost of inlining a function.
      56             : ///
      57             : /// This supports special values for functions which should "always" or
      58             : /// "never" be inlined. Otherwise, the cost represents a unitless amount;
      59             : /// smaller values increase the likelihood of the function being inlined.
      60             : ///
      61             : /// Objects of this type also provide the adjusted threshold for inlining
      62             : /// based on the information available for a particular callsite. They can be
      63             : /// directly tested to determine if inlining should occur given the cost and
      64             : /// threshold for this cost metric.
      65             : class InlineCost {
      66             :   enum SentinelValues {
      67             :     AlwaysInlineCost = INT_MIN,
      68             :     NeverInlineCost = INT_MAX
      69             :   };
      70             : 
      71             :   /// The estimated cost of inlining this callsite.
      72             :   const int Cost;
      73             : 
      74             :   /// The adjusted threshold against which this cost was computed.
      75             :   const int Threshold;
      76             : 
      77             :   /// Must be set for Always and Never instances.
      78             :   const char *Reason = nullptr;
      79             : 
      80             :   // Trivial constructor, interesting logic in the factory functions below.
      81             :   InlineCost(int Cost, int Threshold, const char *Reason = nullptr)
      82             :       : Cost(Cost), Threshold(Threshold), Reason(Reason) {
      83             :     assert((isVariable() || Reason) &&
      84             :            "Reason must be provided for Never or Always");
      85             :   }
      86             : 
      87             : public:
      88             :   static InlineCost get(int Cost, int Threshold) {
      89             :     assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
      90             :     assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
      91             :     return InlineCost(Cost, Threshold);
      92             :   }
      93             :   static InlineCost getAlways(const char *Reason) {
      94             :     return InlineCost(AlwaysInlineCost, 0, Reason);
      95             :   }
      96             :   static InlineCost getNever(const char *Reason) {
      97             :     return InlineCost(NeverInlineCost, 0, Reason);
      98             :   }
      99             : 
     100             :   /// Test whether the inline cost is low enough for inlining.
     101           0 :   explicit operator bool() const {
     102           0 :     return Cost < Threshold;
     103             :   }
     104             : 
     105           0 :   bool isAlways() const { return Cost == AlwaysInlineCost; }
     106           0 :   bool isNever() const { return Cost == NeverInlineCost; }
     107             :   bool isVariable() const { return !isAlways() && !isNever(); }
     108             : 
     109             :   /// Get the inline cost estimate.
     110             :   /// It is an error to call this on an "always" or "never" InlineCost.
     111           0 :   int getCost() const {
     112             :     assert(isVariable() && "Invalid access of InlineCost");
     113           0 :     return Cost;
     114             :   }
     115             : 
     116             :   /// Get the threshold against which the cost was computed
     117           0 :   int getThreshold() const {
     118             :     assert(isVariable() && "Invalid access of InlineCost");
     119           0 :     return Threshold;
     120             :   }
     121             : 
     122             :   /// Get the reason of Always or Never.
     123           0 :   const char *getReason() const {
     124             :     assert((Reason || isVariable()) &&
     125             :            "InlineCost reason must be set for Always or Never");
     126           0 :     return Reason;
     127             :   }
     128             : 
     129             :   /// Get the cost delta from the threshold for inlining.
     130             :   /// Only valid if the cost is of the variable kind. Returns a negative
     131             :   /// value if the cost is too high to inline.
     132           0 :   int getCostDelta() const { return Threshold - getCost(); }
     133             : };
     134             : 
     135             : /// InlineResult is basically true or false. For false results the message
     136             : /// describes a reason why it is decided not to inline.
     137             : struct InlineResult {
     138             :   const char *message = nullptr;
     139             :   InlineResult(bool result, const char *message = nullptr)
     140      215300 :       : message(result ? nullptr : (message ? message : "cost > threshold")) {}
     141    18196401 :   InlineResult(const char *message = nullptr) : message(message) {}
     142          31 :   operator bool() const { return !message; }
     143           0 :   operator const char *() const { return message; }
     144             : };
     145             : 
     146             : /// Thresholds to tune inline cost analysis. The inline cost analysis decides
     147             : /// the condition to apply a threshold and applies it. Otherwise,
     148             : /// DefaultThreshold is used. If a threshold is Optional, it is applied only
     149             : /// when it has a valid value. Typically, users of inline cost analysis
     150             : /// obtain an InlineParams object through one of the \c getInlineParams methods
     151             : /// and pass it to \c getInlineCost. Some specialized versions of inliner
     152             : /// (such as the pre-inliner) might have custom logic to compute \c InlineParams
     153             : /// object.
     154             : 
     155         103 : struct InlineParams {
     156             :   /// The default threshold to start with for a callee.
     157             :   int DefaultThreshold;
     158             : 
     159             :   /// Threshold to use for callees with inline hint.
     160             :   Optional<int> HintThreshold;
     161             : 
     162             :   /// Threshold to use for cold callees.
     163             :   Optional<int> ColdThreshold;
     164             : 
     165             :   /// Threshold to use when the caller is optimized for size.
     166             :   Optional<int> OptSizeThreshold;
     167             : 
     168             :   /// Threshold to use when the caller is optimized for minsize.
     169             :   Optional<int> OptMinSizeThreshold;
     170             : 
     171             :   /// Threshold to use when the callsite is considered hot.
     172             :   Optional<int> HotCallSiteThreshold;
     173             : 
     174             :   /// Threshold to use when the callsite is considered hot relative to function
     175             :   /// entry.
     176             :   Optional<int> LocallyHotCallSiteThreshold;
     177             : 
     178             :   /// Threshold to use when the callsite is considered cold.
     179             :   Optional<int> ColdCallSiteThreshold;
     180             : 
     181             :   /// Compute inline cost even when the cost has exceeded the threshold.
     182             :   Optional<bool> ComputeFullInlineCost;
     183             : };
     184             : 
     185             : /// Generate the parameters to tune the inline cost analysis based only on the
     186             : /// commandline options.
     187             : InlineParams getInlineParams();
     188             : 
     189             : /// Generate the parameters to tune the inline cost analysis based on command
     190             : /// line options. If -inline-threshold option is not explicitly passed,
     191             : /// \p Threshold is used as the default threshold.
     192             : InlineParams getInlineParams(int Threshold);
     193             : 
     194             : /// Generate the parameters to tune the inline cost analysis based on command
     195             : /// line options. If -inline-threshold option is not explicitly passed,
     196             : /// the default threshold is computed from \p OptLevel and \p SizeOptLevel.
     197             : /// An \p OptLevel value above 3 is considered an aggressive optimization mode.
     198             : /// \p SizeOptLevel of 1 corresponds to the -Os flag and 2 corresponds to
     199             : /// the -Oz flag.
     200             : InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel);
     201             : 
     202             : /// Return the cost associated with a callsite, including parameter passing
     203             : /// and the call/return instruction.
     204             : int getCallsiteCost(CallSite CS, const DataLayout &DL);
     205             : 
     206             : /// Get an InlineCost object representing the cost of inlining this
     207             : /// callsite.
     208             : ///
     209             : /// Note that a default threshold is passed into this function. This threshold
     210             : /// could be modified based on callsite's properties and only costs below this
     211             : /// new threshold are computed with any accuracy. The new threshold can be
     212             : /// used to bound the computation necessary to determine whether the cost is
     213             : /// sufficiently low to warrant inlining.
     214             : ///
     215             : /// Also note that calling this function *dynamically* computes the cost of
     216             : /// inlining the callsite. It is an expensive, heavyweight call.
     217             : InlineCost getInlineCost(
     218             :     CallSite CS, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
     219             :     std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
     220             :     Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
     221             :     ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE = nullptr);
     222             : 
     223             : /// Get an InlineCost with the callee explicitly specified.
     224             : /// This allows you to calculate the cost of inlining a function via a
     225             : /// pointer. This behaves exactly as the version with no explicit callee
     226             : /// parameter in all other respects.
     227             : //
     228             : InlineCost
     229             : getInlineCost(CallSite CS, Function *Callee, const InlineParams &Params,
     230             :               TargetTransformInfo &CalleeTTI,
     231             :               std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
     232             :               Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
     233             :               ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE);
     234             : 
     235             : /// Minimal filter to detect invalid constructs for inlining.
     236             : bool isInlineViable(Function &Callee);
     237             : }
     238             : 
     239             : #endif

Generated by: LCOV version 1.13