Home | History | Annotate | Download | only in Analysis
      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/CallGraphSCCPass.h"
     18 #include <cassert>
     19 #include <climits>
     20 
     21 namespace llvm {
     22 class AssumptionCacheTracker;
     23 class CallSite;
     24 class DataLayout;
     25 class Function;
     26 class ProfileSummaryInfo;
     27 class TargetTransformInfo;
     28 
     29 namespace InlineConstants {
     30   // Various magic constants used to adjust heuristics.
     31   const int InstrCost = 5;
     32   const int IndirectCallThreshold = 100;
     33   const int CallPenalty = 25;
     34   const int LastCallToStaticBonus = -15000;
     35   const int ColdccPenalty = 2000;
     36   const int NoreturnPenalty = 10000;
     37   /// Do not inline functions which allocate this many bytes on the stack
     38   /// when the caller is recursive.
     39   const unsigned TotalAllocaSizeRecursiveCaller = 1024;
     40 }
     41 
     42 /// \brief Represents the cost of inlining a function.
     43 ///
     44 /// This supports special values for functions which should "always" or
     45 /// "never" be inlined. Otherwise, the cost represents a unitless amount;
     46 /// smaller values increase the likelihood of the function being inlined.
     47 ///
     48 /// Objects of this type also provide the adjusted threshold for inlining
     49 /// based on the information available for a particular callsite. They can be
     50 /// directly tested to determine if inlining should occur given the cost and
     51 /// threshold for this cost metric.
     52 class InlineCost {
     53   enum SentinelValues {
     54     AlwaysInlineCost = INT_MIN,
     55     NeverInlineCost = INT_MAX
     56   };
     57 
     58   /// \brief The estimated cost of inlining this callsite.
     59   const int Cost;
     60 
     61   /// \brief The adjusted threshold against which this cost was computed.
     62   const int Threshold;
     63 
     64   // Trivial constructor, interesting logic in the factory functions below.
     65   InlineCost(int Cost, int Threshold) : Cost(Cost), Threshold(Threshold) {}
     66 
     67 public:
     68   static InlineCost get(int Cost, int Threshold) {
     69     assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
     70     assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
     71     return InlineCost(Cost, Threshold);
     72   }
     73   static InlineCost getAlways() {
     74     return InlineCost(AlwaysInlineCost, 0);
     75   }
     76   static InlineCost getNever() {
     77     return InlineCost(NeverInlineCost, 0);
     78   }
     79 
     80   /// \brief Test whether the inline cost is low enough for inlining.
     81   explicit operator bool() const {
     82     return Cost < Threshold;
     83   }
     84 
     85   bool isAlways() const { return Cost == AlwaysInlineCost; }
     86   bool isNever() const { return Cost == NeverInlineCost; }
     87   bool isVariable() const { return !isAlways() && !isNever(); }
     88 
     89   /// \brief Get the inline cost estimate.
     90   /// It is an error to call this on an "always" or "never" InlineCost.
     91   int getCost() const {
     92     assert(isVariable() && "Invalid access of InlineCost");
     93     return Cost;
     94   }
     95 
     96   /// \brief Get the cost delta from the threshold for inlining.
     97   /// Only valid if the cost is of the variable kind. Returns a negative
     98   /// value if the cost is too high to inline.
     99   int getCostDelta() const { return Threshold - getCost(); }
    100 };
    101 
    102 /// \brief Get an InlineCost object representing the cost of inlining this
    103 /// callsite.
    104 ///
    105 /// Note that a default threshold is passed into this function. This threshold
    106 /// could be modified based on callsite's properties and only costs below this
    107 /// new threshold are computed with any accuracy. The new threshold can be
    108 /// used to bound the computation necessary to determine whether the cost is
    109 /// sufficiently low to warrant inlining.
    110 ///
    111 /// Also note that calling this function *dynamically* computes the cost of
    112 /// inlining the callsite. It is an expensive, heavyweight call.
    113 InlineCost getInlineCost(CallSite CS, int DefaultThreshold,
    114                          TargetTransformInfo &CalleeTTI,
    115                          AssumptionCacheTracker *ACT, ProfileSummaryInfo *PSI);
    116 
    117 /// \brief Get an InlineCost with the callee explicitly specified.
    118 /// This allows you to calculate the cost of inlining a function via a
    119 /// pointer. This behaves exactly as the version with no explicit callee
    120 /// parameter in all other respects.
    121 //
    122 InlineCost getInlineCost(CallSite CS, Function *Callee, int DefaultThreshold,
    123                          TargetTransformInfo &CalleeTTI,
    124                          AssumptionCacheTracker *ACT, ProfileSummaryInfo *PSI);
    125 
    126 int computeThresholdFromOptLevels(unsigned OptLevel, unsigned SizeOptLevel);
    127 
    128 /// \brief Return the default value of -inline-threshold.
    129 int getDefaultInlineThreshold();
    130 
    131 /// \brief Minimal filter to detect invalid constructs for inlining.
    132 bool isInlineViable(Function &Callee);
    133 }
    134 
    135 #endif
    136