LLVM API Documentation

InlineCost.h
Go to the documentation of this file.
00001 //===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements heuristics for inlining decisions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ANALYSIS_INLINECOST_H
00015 #define LLVM_ANALYSIS_INLINECOST_H
00016 
00017 #include "llvm/Analysis/CallGraphSCCPass.h"
00018 #include <cassert>
00019 #include <climits>
00020 
00021 namespace llvm {
00022 class AssumptionTracker;
00023 class CallSite;
00024 class DataLayout;
00025 class Function;
00026 class TargetTransformInfo;
00027 
00028 namespace InlineConstants {
00029   // Various magic constants used to adjust heuristics.
00030   const int InstrCost = 5;
00031   const int IndirectCallThreshold = 100;
00032   const int CallPenalty = 25;
00033   const int LastCallToStaticBonus = -15000;
00034   const int ColdccPenalty = 2000;
00035   const int NoreturnPenalty = 10000;
00036   /// Do not inline functions which allocate this many bytes on the stack
00037   /// when the caller is recursive.
00038   const unsigned TotalAllocaSizeRecursiveCaller = 1024;
00039 }
00040 
00041 /// \brief Represents the cost of inlining a function.
00042 ///
00043 /// This supports special values for functions which should "always" or
00044 /// "never" be inlined. Otherwise, the cost represents a unitless amount;
00045 /// smaller values increase the likelihood of the function being inlined.
00046 ///
00047 /// Objects of this type also provide the adjusted threshold for inlining
00048 /// based on the information available for a particular callsite. They can be
00049 /// directly tested to determine if inlining should occur given the cost and
00050 /// threshold for this cost metric.
00051 class InlineCost {
00052   enum SentinelValues {
00053     AlwaysInlineCost = INT_MIN,
00054     NeverInlineCost = INT_MAX
00055   };
00056 
00057   /// \brief The estimated cost of inlining this callsite.
00058   const int Cost;
00059 
00060   /// \brief The adjusted threshold against which this cost was computed.
00061   const int Threshold;
00062 
00063   // Trivial constructor, interesting logic in the factory functions below.
00064   InlineCost(int Cost, int Threshold) : Cost(Cost), Threshold(Threshold) {}
00065 
00066 public:
00067   static InlineCost get(int Cost, int Threshold) {
00068     assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
00069     assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
00070     return InlineCost(Cost, Threshold);
00071   }
00072   static InlineCost getAlways() {
00073     return InlineCost(AlwaysInlineCost, 0);
00074   }
00075   static InlineCost getNever() {
00076     return InlineCost(NeverInlineCost, 0);
00077   }
00078 
00079   /// \brief Test whether the inline cost is low enough for inlining.
00080   LLVM_EXPLICIT operator bool() const {
00081     return Cost < Threshold;
00082   }
00083 
00084   bool isAlways() const { return Cost == AlwaysInlineCost; }
00085   bool isNever() const { return Cost == NeverInlineCost; }
00086   bool isVariable() const { return !isAlways() && !isNever(); }
00087 
00088   /// \brief Get the inline cost estimate.
00089   /// It is an error to call this on an "always" or "never" InlineCost.
00090   int getCost() const {
00091     assert(isVariable() && "Invalid access of InlineCost");
00092     return Cost;
00093   }
00094 
00095   /// \brief Get the cost delta from the threshold for inlining.
00096   /// Only valid if the cost is of the variable kind. Returns a negative
00097   /// value if the cost is too high to inline.
00098   int getCostDelta() const { return Threshold - getCost(); }
00099 };
00100 
00101 /// \brief Cost analyzer used by inliner.
00102 class InlineCostAnalysis : public CallGraphSCCPass {
00103   const TargetTransformInfo *TTI;
00104   AssumptionTracker *AT;
00105 
00106 public:
00107   static char ID;
00108 
00109   InlineCostAnalysis();
00110   ~InlineCostAnalysis();
00111 
00112   // Pass interface implementation.
00113   void getAnalysisUsage(AnalysisUsage &AU) const override;
00114   bool runOnSCC(CallGraphSCC &SCC) override;
00115 
00116   /// \brief Get an InlineCost object representing the cost of inlining this
00117   /// callsite.
00118   ///
00119   /// Note that threshold is passed into this function. Only costs below the
00120   /// threshold are computed with any accuracy. The threshold can be used to
00121   /// bound the computation necessary to determine whether the cost is
00122   /// sufficiently low to warrant inlining.
00123   ///
00124   /// Also note that calling this function *dynamically* computes the cost of
00125   /// inlining the callsite. It is an expensive, heavyweight call.
00126   InlineCost getInlineCost(CallSite CS, int Threshold);
00127 
00128   /// \brief Get an InlineCost with the callee explicitly specified.
00129   /// This allows you to calculate the cost of inlining a function via a
00130   /// pointer. This behaves exactly as the version with no explicit callee
00131   /// parameter in all other respects.
00132   //
00133   //  Note: This is used by out-of-tree passes, please do not remove without
00134   //  adding a replacement API.
00135   InlineCost getInlineCost(CallSite CS, Function *Callee, int Threshold);
00136 
00137   /// \brief Minimal filter to detect invalid constructs for inlining.
00138   bool isInlineViable(Function &Callee);
00139 };
00140 
00141 }
00142 
00143 #endif