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.
58 const int Cost : 30; // The inlining cost if neither always nor never.
59 const unsigned Kind : 2; // The type of cost, one of CostKind above.
61 /// \brief The adjusted threshold against which this cost should be tested.
64 // Trivial constructor, interesting logic in the factory functions below.
65 InlineCost(int Cost, CostKind Kind, int Threshold)
66 : Cost(Cost), Kind(Kind), Threshold(Threshold) {}
69 static InlineCost get(int Cost, int Threshold) {
70 InlineCost Result(Cost, CK_Variable, Threshold);
71 assert(Result.Cost == Cost && "Cost exceeds InlineCost precision");
74 static InlineCost getAlways() {
75 return InlineCost(0, CK_Always, 0);
77 static InlineCost getNever() {
78 return InlineCost(0, CK_Never, 0);
81 /// \brief Test whether the inline cost is low enough for inlining.
82 operator bool() const {
83 if (isAlways()) return true;
84 if (isNever()) return false;
85 return Cost < Threshold;
88 bool isVariable() const { return Kind == CK_Variable; }
89 bool isAlways() const { return Kind == CK_Always; }
90 bool isNever() const { return Kind == CK_Never; }
92 /// getCost() - Return a "variable" inline cost's amount. It is
93 /// an error to call this on an "always" or "never" InlineCost.
95 assert(Kind == CK_Variable && "Invalid access of InlineCost");
99 /// \brief Get the cost delta from the threshold for inlining.
100 /// Only valid if the cost is of the variable kind. Returns a negative
101 /// value if the cost is too high to inline.
102 int getCostDelta() const {
103 return Threshold - getCost();
107 /// InlineCostAnalyzer - Cost analyzer used by inliner.
108 class InlineCostAnalyzer {
109 // TargetData if available, or null.
110 const TargetData *TD;
113 InlineCostAnalyzer(): TD(0) {}
115 void setTargetData(const TargetData *TData) { TD = TData; }
117 /// \brief Get an InlineCost object representing the cost of inlining this
120 /// Note that threshold is passed into this function. Only costs below the
121 /// threshold are computed with any accuracy. The threshold can be used to
122 /// bound the computation necessary to determine whether the cost is
123 /// sufficiently low to warrant inlining.
124 InlineCost getInlineCost(CallSite CS, int Threshold);
127 /// callIsSmall - If a call is likely to lower to a single target instruction,
128 /// or is otherwise deemed small return true.
129 bool callIsSmall(const Function *Callee);