Rotate two of the functions used to count bonuses for the inline cost
[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       /// countCodeReductionForConstant - Figure out an approximation for how
114       /// many instructions will be constant folded if the specified value is
115       /// constant.
116       unsigned countCodeReductionForConstant(const CodeMetrics &Metrics,
117                                              Value *V);
118
119       /// countCodeReductionForAlloca - Figure out an approximation of how much
120       /// smaller the function will be if it is inlined into a context where an
121       /// argument becomes an alloca.
122       unsigned countCodeReductionForAlloca(const CodeMetrics &Metrics,
123                                            Value *V);
124
125       /// analyzeFunction - Add information about the specified function
126       /// to the current structure.
127       void analyzeFunction(Function *F, const TargetData *TD);
128
129       /// NeverInline - Returns true if the function should never be
130       /// inlined into any caller.
131       bool NeverInline();
132     };
133
134     // The Function* for a function can be changed (by ArgumentPromotion);
135     // the ValueMap will update itself when this happens.
136     ValueMap<const Function *, FunctionInfo> CachedFunctionInfo;
137
138     // TargetData if available, or null.
139     const TargetData *TD;
140
141     int CountBonusForConstant(Value *V, Constant *C = NULL);
142     int ConstantFunctionBonus(CallSite CS, Constant *C);
143     int getInlineSize(CallSite CS, Function *Callee);
144     int getInlineBonuses(CallSite CS, Function *Callee);
145   public:
146     InlineCostAnalyzer(): TD(0) {}
147
148     void setTargetData(const TargetData *TData) { TD = TData; }
149
150     /// getInlineCost - The heuristic used to determine if we should inline the
151     /// function call or not.
152     ///
153     InlineCost getInlineCost(CallSite CS,
154                              SmallPtrSet<const Function *, 16> &NeverInline);
155     /// getCalledFunction - The heuristic used to determine if we should inline
156     /// the function call or not.  The callee is explicitly specified, to allow
157     /// you to calculate the cost of inlining a function via a pointer.  The
158     /// result assumes that the inlined version will always be used.  You should
159     /// weight it yourself in cases where this callee will not always be called.
160     InlineCost getInlineCost(CallSite CS,
161                              Function *Callee,
162                              SmallPtrSet<const Function *, 16> &NeverInline);
163
164     /// getSpecializationBonus - The heuristic used to determine the per-call
165     /// performance boost for using a specialization of Callee with argument
166     /// SpecializedArgNos replaced by a constant.
167     int getSpecializationBonus(Function *Callee,
168              SmallVectorImpl<unsigned> &SpecializedArgNo);
169
170     /// getSpecializationCost - The heuristic used to determine the code-size
171     /// impact of creating a specialized version of Callee with argument
172     /// SpecializedArgNo replaced by a constant.
173     InlineCost getSpecializationCost(Function *Callee,
174                SmallVectorImpl<unsigned> &SpecializedArgNo);
175
176     /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
177     /// higher threshold to determine if the function call should be inlined.
178     float getInlineFudgeFactor(CallSite CS);
179
180     /// resetCachedFunctionInfo - erase any cached cost info for this function.
181     void resetCachedCostInfo(Function* Caller) {
182       CachedFunctionInfo[Caller] = FunctionInfo();
183     }
184
185     /// growCachedCostInfo - update the cached cost info for Caller after Callee
186     /// has been inlined. If Callee is NULL it means a dead call has been
187     /// eliminated.
188     void growCachedCostInfo(Function* Caller, Function* Callee);
189
190     /// clear - empty the cache of inline costs
191     void clear();
192   };
193
194   /// callIsSmall - If a call is likely to lower to a single target instruction,
195   /// or is otherwise deemed small return true.
196   bool callIsSmall(const Function *Callee);
197 }
198
199 #endif