Make headers standalone.
[oota-llvm.git] / include / llvm / Analysis / InlineCost.h
1 //===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements heuristics for inlining decisions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_INLINECOST_H
15 #define LLVM_ANALYSIS_INLINECOST_H
16
17 #include "llvm/Function.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/ValueMap.h"
20 #include "llvm/Analysis/CodeMetrics.h"
21 #include <cassert>
22 #include <climits>
23 #include <vector>
24
25 namespace llvm {
26
27   class CallSite;
28   template<class PtrType, unsigned SmallSize>
29   class SmallPtrSet;
30   class TargetData;
31
32   namespace InlineConstants {
33     // Various magic constants used to adjust heuristics.
34     const int InstrCost = 5;
35     const int IndirectCallBonus = -100;
36     const int CallPenalty = 25;
37     const int LastCallToStaticBonus = -15000;
38     const int ColdccPenalty = 2000;
39     const int NoreturnPenalty = 10000;
40   }
41
42   /// InlineCost - Represent the cost of inlining a function. This
43   /// supports special values for functions which should "always" or
44   /// "never" be inlined. Otherwise, the cost represents a unitless
45   /// amount; smaller values increase the likelihood of the function
46   /// being inlined.
47   class InlineCost {
48     enum Kind {
49       Value,
50       Always,
51       Never
52     };
53
54     // This is a do-it-yourself implementation of
55     //   int Cost : 30;
56     //   unsigned Type : 2;
57     // We used to use bitfields, but they were sometimes miscompiled (PR3822).
58     enum { TYPE_BITS = 2 };
59     enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
60     unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
61
62     Kind getType() const {
63       return Kind(TypedCost >> COST_BITS);
64     }
65
66     int getCost() const {
67       // Sign-extend the bottom COST_BITS bits.
68       return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
69     }
70
71     InlineCost(int C, int T) {
72       TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
73       assert(getCost() == C && "Cost exceeds InlineCost precision");
74     }
75   public:
76     static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
77     static InlineCost getAlways() { return InlineCost(0, Always); }
78     static InlineCost getNever() { return InlineCost(0, Never); }
79
80     bool isVariable() const { return getType() == Value; }
81     bool isAlways() const { return getType() == Always; }
82     bool isNever() const { return getType() == Never; }
83
84     /// getValue() - Return a "variable" inline cost's amount. It is
85     /// an error to call this on an "always" or "never" InlineCost.
86     int getValue() const {
87       assert(getType() == Value && "Invalid access of InlineCost");
88       return getCost();
89     }
90   };
91
92   /// InlineCostAnalyzer - Cost analyzer used by inliner.
93   class InlineCostAnalyzer {
94     struct ArgInfo {
95     public:
96       unsigned ConstantWeight;
97       unsigned AllocaWeight;
98
99       ArgInfo(unsigned CWeight, unsigned AWeight)
100         : ConstantWeight(CWeight), AllocaWeight(AWeight)
101           {}
102     };
103
104     struct FunctionInfo {
105       CodeMetrics Metrics;
106
107       /// ArgumentWeights - Each formal argument of the function is inspected to
108       /// see if it is used in any contexts where making it a constant or alloca
109       /// would reduce the code size.  If so, we add some value to the argument
110       /// entry here.
111       std::vector<ArgInfo> ArgumentWeights;
112
113       /// analyzeFunction - Add information about the specified function
114       /// to the current structure.
115       void analyzeFunction(Function *F, const TargetData *TD);
116
117       /// NeverInline - Returns true if the function should never be
118       /// inlined into any caller.
119       bool NeverInline();
120     };
121
122     // The Function* for a function can be changed (by ArgumentPromotion);
123     // the ValueMap will update itself when this happens.
124     ValueMap<const Function *, FunctionInfo> CachedFunctionInfo;
125
126     // TargetData if available, or null.
127     const TargetData *TD;
128
129     int CountBonusForConstant(Value *V, Constant *C = NULL);
130     int ConstantFunctionBonus(CallSite CS, Constant *C);
131     int getInlineSize(CallSite CS, Function *Callee);
132     int getInlineBonuses(CallSite CS, Function *Callee);
133   public:
134     InlineCostAnalyzer(): TD(0) {}
135
136     void setTargetData(const TargetData *TData) { TD = TData; }
137
138     /// getInlineCost - The heuristic used to determine if we should inline the
139     /// function call or not.
140     ///
141     InlineCost getInlineCost(CallSite CS,
142                              SmallPtrSet<const Function *, 16> &NeverInline);
143     /// getCalledFunction - The heuristic used to determine if we should inline
144     /// the function call or not.  The callee is explicitly specified, to allow
145     /// you to calculate the cost of inlining a function via a pointer.  The
146     /// result assumes that the inlined version will always be used.  You should
147     /// weight it yourself in cases where this callee will not always be called.
148     InlineCost getInlineCost(CallSite CS,
149                              Function *Callee,
150                              SmallPtrSet<const Function *, 16> &NeverInline);
151
152     /// getSpecializationBonus - The heuristic used to determine the per-call
153     /// performance boost for using a specialization of Callee with argument
154     /// SpecializedArgNos replaced by a constant.
155     int getSpecializationBonus(Function *Callee,
156              SmallVectorImpl<unsigned> &SpecializedArgNo);
157
158     /// getSpecializationCost - The heuristic used to determine the code-size
159     /// impact of creating a specialized version of Callee with argument
160     /// SpecializedArgNo replaced by a constant.
161     InlineCost getSpecializationCost(Function *Callee,
162                SmallVectorImpl<unsigned> &SpecializedArgNo);
163
164     /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
165     /// higher threshold to determine if the function call should be inlined.
166     float getInlineFudgeFactor(CallSite CS);
167
168     /// resetCachedFunctionInfo - erase any cached cost info for this function.
169     void resetCachedCostInfo(Function* Caller) {
170       CachedFunctionInfo[Caller] = FunctionInfo();
171     }
172
173     /// growCachedCostInfo - update the cached cost info for Caller after Callee
174     /// has been inlined. If Callee is NULL it means a dead call has been
175     /// eliminated.
176     void growCachedCostInfo(Function* Caller, Function* Callee);
177
178     /// clear - empty the cache of inline costs
179     void clear();
180   };
181
182   /// callIsSmall - If a call is likely to lower to a single target instruction,
183   /// or is otherwise deemed small return true.
184   bool callIsSmall(const Function *Callee);
185 }
186
187 #endif