1 //===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements heuristics for inlining decisions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_INLINECOST_H
15 #define LLVM_ANALYSIS_INLINECOST_H
17 #include "llvm/Analysis/CallGraphSCCPass.h"
22 class AssumptionCacheTracker;
26 class ProfileSummaryInfo;
27 class TargetTransformInfo;
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;
42 /// \brief Represents the cost of inlining a function.
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.
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.
54 AlwaysInlineCost = INT_MIN,
55 NeverInlineCost = INT_MAX
58 /// \brief The estimated cost of inlining this callsite.
61 /// \brief The adjusted threshold against which this cost was computed.
64 // Trivial constructor, interesting logic in the factory functions below.
65 InlineCost(int Cost, int Threshold) : Cost(Cost), Threshold(Threshold) {}
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);
73 static InlineCost getAlways() {
74 return InlineCost(AlwaysInlineCost, 0);
76 static InlineCost getNever() {
77 return InlineCost(NeverInlineCost, 0);
80 /// \brief Test whether the inline cost is low enough for inlining.
81 explicit operator bool() const {
82 return Cost < Threshold;
85 bool isAlways() const { return Cost == AlwaysInlineCost; }
86 bool isNever() const { return Cost == NeverInlineCost; }
87 bool isVariable() const { return !isAlways() && !isNever(); }
89 /// \brief Get the inline cost estimate.
90 /// It is an error to call this on an "always" or "never" InlineCost.
92 assert(isVariable() && "Invalid access of InlineCost");
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(); }
102 /// \brief Get an InlineCost object representing the cost of inlining this
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.
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);
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.
122 InlineCost getInlineCost(CallSite CS, Function *Callee, int DefaultThreshold,
123 TargetTransformInfo &CalleeTTI,
124 AssumptionCacheTracker *ACT, ProfileSummaryInfo *PSI);
126 int computeThresholdFromOptLevels(unsigned OptLevel, unsigned SizeOptLevel);
128 /// \brief Return the default value of -inline-threshold.
129 int getDefaultInlineThreshold();
131 /// \brief Minimal filter to detect invalid constructs for inlining.
132 bool isInlineViable(Function &Callee);