Revert r98089, it was breaking a clang test.
[oota-llvm.git] / include / llvm / Analysis / InlineCost.h
1 //===- InlineCost.cpp - 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 <cassert>
18 #include <climits>
19 #include <map>
20 #include <vector>
21
22 namespace llvm {
23
24   class Value;
25   class Function;
26   class BasicBlock;
27   class CallSite;
28   template<class PtrType, unsigned SmallSize>
29   class SmallPtrSet;
30
31   // CodeMetrics - Calculate size and a few similar metrics for a set of
32   // basic blocks.
33   struct CodeMetrics {
34     /// NeverInline - True if this callee should never be inlined into a
35     /// caller.
36     bool NeverInline;
37
38     /// usesDynamicAlloca - True if this function calls alloca (in the C sense).
39     bool usesDynamicAlloca;
40
41     /// NumInsts, NumBlocks - Keep track of how large each function is, which
42     /// is used to estimate the code size cost of inlining it.
43     unsigned NumInsts, NumBlocks;
44
45     /// NumCalls - Keep track of the number of calls to 'big' functions.
46     unsigned NumCalls;
47
48     /// NumVectorInsts - Keep track of how many instructions produce vector
49     /// values.  The inliner is being more aggressive with inlining vector
50     /// kernels.
51     unsigned NumVectorInsts;
52
53     /// NumRets - Keep track of how many Ret instructions the block contains.
54     unsigned NumRets;
55
56     CodeMetrics() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
57                     NumBlocks(0), NumCalls(0), NumVectorInsts(0), NumRets(0) {}
58
59     /// analyzeBasicBlock - Add information about the specified basic block
60     /// to the current structure.
61     void analyzeBasicBlock(const BasicBlock *BB);
62
63     /// analyzeFunction - Add information about the specified function
64     /// to the current structure.
65     void analyzeFunction(Function *F);
66   };
67
68   namespace InlineConstants {
69     // Various magic constants used to adjust heuristics.
70     const int InstrCost = 5;
71     const int IndirectCallBonus = 500;
72     const int CallPenalty = 25;
73     const int LastCallToStaticBonus = -15000;
74     const int ColdccPenalty = 2000;
75     const int NoreturnPenalty = 10000;
76   }
77
78   /// InlineCost - Represent the cost of inlining a function. This
79   /// supports special values for functions which should "always" or
80   /// "never" be inlined. Otherwise, the cost represents a unitless
81   /// amount; smaller values increase the likelyhood of the function
82   /// being inlined.
83   class InlineCost {
84     enum Kind {
85       Value,
86       Always,
87       Never
88     };
89
90     // This is a do-it-yourself implementation of
91     //   int Cost : 30;
92     //   unsigned Type : 2;
93     // We used to use bitfields, but they were sometimes miscompiled (PR3822).
94     enum { TYPE_BITS = 2 };
95     enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
96     unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
97
98     Kind getType() const {
99       return Kind(TypedCost >> COST_BITS);
100     }
101
102     int getCost() const {
103       // Sign-extend the bottom COST_BITS bits.
104       return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
105     }
106
107     InlineCost(int C, int T) {
108       TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
109       assert(getCost() == C && "Cost exceeds InlineCost precision");
110     }
111   public:
112     static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
113     static InlineCost getAlways() { return InlineCost(0, Always); }
114     static InlineCost getNever() { return InlineCost(0, Never); }
115
116     bool isVariable() const { return getType() == Value; }
117     bool isAlways() const { return getType() == Always; }
118     bool isNever() const { return getType() == Never; }
119
120     /// getValue() - Return a "variable" inline cost's amount. It is
121     /// an error to call this on an "always" or "never" InlineCost.
122     int getValue() const {
123       assert(getType() == Value && "Invalid access of InlineCost");
124       return getCost();
125     }
126   };
127
128   /// InlineCostAnalyzer - Cost analyzer used by inliner.
129   class InlineCostAnalyzer {
130     struct ArgInfo {
131     public:
132       unsigned ConstantWeight;
133       unsigned AllocaWeight;
134
135       ArgInfo(unsigned CWeight, unsigned AWeight)
136         : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
137     };
138
139     struct FunctionInfo {
140       CodeMetrics Metrics;
141
142       /// ArgumentWeights - Each formal argument of the function is inspected to
143       /// see if it is used in any contexts where making it a constant or alloca
144       /// would reduce the code size.  If so, we add some value to the argument
145       /// entry here.
146       std::vector<ArgInfo> ArgumentWeights;
147
148       /// CountCodeReductionForConstant - Figure out an approximation for how
149       /// many instructions will be constant folded if the specified value is
150       /// constant.
151       unsigned CountCodeReductionForConstant(Value *V);
152
153       /// CountCodeReductionForAlloca - Figure out an approximation of how much
154       /// smaller the function will be if it is inlined into a context where an
155       /// argument becomes an alloca.
156       ///
157       unsigned CountCodeReductionForAlloca(Value *V);
158
159       /// analyzeFunction - Add information about the specified function
160       /// to the current structure.
161       void analyzeFunction(Function *F);
162     };
163
164     std::map<const Function *, FunctionInfo> CachedFunctionInfo;
165
166   public:
167
168     /// getInlineCost - The heuristic used to determine if we should inline the
169     /// function call or not.
170     ///
171     InlineCost getInlineCost(CallSite CS,
172                              SmallPtrSet<const Function *, 16> &NeverInline);
173
174     /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
175     /// higher threshold to determine if the function call should be inlined.
176     float getInlineFudgeFactor(CallSite CS);
177
178     /// resetCachedFunctionInfo - erase any cached cost info for this function.
179     void resetCachedCostInfo(Function* Caller) {
180       CachedFunctionInfo[Caller] = FunctionInfo();
181     }
182   };
183 }
184
185 #endif