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/Function.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/ValueMap.h"
21 #include "llvm/Analysis/CodeMetrics.h"
31 namespace InlineConstants {
32 // Various magic constants used to adjust heuristics.
33 const int InstrCost = 5;
34 const int IndirectCallThreshold = 100;
35 const int CallPenalty = 25;
36 const int LastCallToStaticBonus = -15000;
37 const int ColdccPenalty = 2000;
38 const int NoreturnPenalty = 10000;
41 /// \brief Represents the cost of inlining a function.
43 /// This supports special values for functions which should "always" or
44 /// "never" be inlined. Otherwise, the cost represents a unitless amount;
45 /// smaller values increase the likelihood of the function being inlined.
47 /// Objects of this type also provide the adjusted threshold for inlining
48 /// based on the information available for a particular callsite. They can be
49 /// directly tested to determine if inlining should occur given the cost and
50 /// threshold for this cost metric.
53 AlwaysInlineCost = INT_MIN,
54 NeverInlineCost = INT_MAX
57 /// \brief The estimated cost of inlining this callsite.
60 /// \brief The adjusted threshold against which this cost was computed.
63 // Trivial constructor, interesting logic in the factory functions below.
64 InlineCost(int Cost, int Threshold)
65 : 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 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 /// InlineCostAnalyzer - Cost analyzer used by inliner.
103 class InlineCostAnalyzer {
104 // TargetData if available, or null.
105 const TargetData *TD;
108 InlineCostAnalyzer(): TD(0) {}
110 void setTargetData(const TargetData *TData) { TD = TData; }
112 /// \brief Get an InlineCost object representing the cost of inlining this
115 /// Note that threshold is passed into this function. Only costs below the
116 /// threshold are computed with any accuracy. The threshold can be used to
117 /// bound the computation necessary to determine whether the cost is
118 /// sufficiently low to warrant inlining.
119 InlineCost getInlineCost(CallSite CS, int Threshold);
120 /// getCalledFunction - The heuristic used to determine if we should inline
121 /// the function call or not. The callee is explicitly specified, to allow
122 /// you to calculate the cost of inlining a function via a pointer. This
123 /// behaves exactly as the version with no explicit callee parameter in all
126 // Note: This is used by out-of-tree passes, please do not remove without
127 // adding a replacement API.
128 InlineCost getInlineCost(CallSite CS, Function *Callee, int Threshold);