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/CodeMetrics.h"
26 namespace InlineConstants {
27 // Various magic constants used to adjust heuristics.
28 const int InstrCost = 5;
29 const int IndirectCallThreshold = 100;
30 const int CallPenalty = 25;
31 const int LastCallToStaticBonus = -15000;
32 const int ColdccPenalty = 2000;
33 const int NoreturnPenalty = 10000;
34 /// Do not inline functions which allocate this many bytes on the stack
35 /// when the caller is recursive.
36 const unsigned TotalAllocaSizeRecursiveCaller = 1024;
39 /// \brief Represents the cost of inlining a function.
41 /// This supports special values for functions which should "always" or
42 /// "never" be inlined. Otherwise, the cost represents a unitless amount;
43 /// smaller values increase the likelihood of the function being inlined.
45 /// Objects of this type also provide the adjusted threshold for inlining
46 /// based on the information available for a particular callsite. They can be
47 /// directly tested to determine if inlining should occur given the cost and
48 /// threshold for this cost metric.
51 AlwaysInlineCost = INT_MIN,
52 NeverInlineCost = INT_MAX
55 /// \brief The estimated cost of inlining this callsite.
58 /// \brief The adjusted threshold against which this cost was computed.
61 // Trivial constructor, interesting logic in the factory functions below.
62 InlineCost(int Cost, int Threshold) : Cost(Cost), Threshold(Threshold) {}
65 static InlineCost get(int Cost, int Threshold) {
66 assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
67 assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
68 return InlineCost(Cost, Threshold);
70 static InlineCost getAlways() {
71 return InlineCost(AlwaysInlineCost, 0);
73 static InlineCost getNever() {
74 return InlineCost(NeverInlineCost, 0);
77 /// \brief Test whether the inline cost is low enough for inlining.
78 operator bool() const {
79 return Cost < Threshold;
82 bool isAlways() const { return Cost == AlwaysInlineCost; }
83 bool isNever() const { return Cost == NeverInlineCost; }
84 bool isVariable() const { return !isAlways() && !isNever(); }
86 /// \brief Get the inline cost estimate.
87 /// It is an error to call this on an "always" or "never" InlineCost.
89 assert(isVariable() && "Invalid access of InlineCost");
93 /// \brief Get the cost delta from the threshold for inlining.
94 /// Only valid if the cost is of the variable kind. Returns a negative
95 /// value if the cost is too high to inline.
96 int getCostDelta() const { return Threshold - getCost(); }
99 /// \brief Cost analyzer used by inliner.
100 class InlineCostAnalyzer {
101 // DataLayout if available, or null.
102 const DataLayout *TD;
105 InlineCostAnalyzer() : TD(0) {}
107 void setDataLayout(const DataLayout *TData) { TD = TData; }
109 /// \brief Get an InlineCost object representing the cost of inlining this
112 /// Note that threshold is passed into this function. Only costs below the
113 /// threshold are computed with any accuracy. The threshold can be used to
114 /// bound the computation necessary to determine whether the cost is
115 /// sufficiently low to warrant inlining.
116 InlineCost getInlineCost(CallSite CS, int Threshold);
118 /// \brief Get an InlineCost with the callee explicitly specified.
119 /// This allows you to calculate the cost of inlining a function via a
120 /// pointer. This behaves exactly as the version with no explicit callee
121 /// parameter in all other respects.
123 // Note: This is used by out-of-tree passes, please do not remove without
124 // adding a replacement API.
125 InlineCost getInlineCost(CallSite CS, Function *Callee, int Threshold);
127 /// \brief Minimal filter to detect invalid constructs for inlining.
128 bool isInlineViable(Function &Callee);