Add InlineCost class for represent the estimated cost of inlining a
[oota-llvm.git] / include / llvm / Transforms / Utils / 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_TRANSFORMS_UTILS_INLINECOST_H
15 #define LLVM_TRANSFORMS_UTILS_INLINECOST_H
16
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include <cassert>
19 #include <map>
20 #include <vector>
21
22 namespace llvm {
23
24   class Value;
25   class Function;
26   class CallSite;
27
28   /// InlineCost - Represent the cost of inlining a function. This
29   /// supports special values for functions which should "always" or
30   /// "never" be inlined. Otherwise, the cost represents a unitless
31   /// amount; smaller values increase the likelyhood of the function
32   /// being inlined.
33   class InlineCost {
34     enum Kind {
35       Value,
36       Always,
37       Never
38     };
39
40     int Cost : 30;
41     unsigned Type :  2;
42
43     InlineCost(int C, int T) : Cost(C), Type(T) {
44       assert(Cost == C && "Cost exceeds InlineCost precision");
45     }
46   public:
47     static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
48     static InlineCost getAlways() { return InlineCost(0, Always); }
49     static InlineCost getNever() { return InlineCost(0, Never); } 
50
51     bool isVariable() const { return Type == Value; }
52     bool isAlways() const { return Type == Always; }
53     bool isNever() const { return Type == Never; }
54
55     /// getValue() - Return a "variable" inline cost's amount. It is
56     /// an error to call this on an "always" or "never" InlineCost.
57     int getValue() const { 
58       assert(Type == Value && "Invalid access of InlineCost");
59       return Cost;
60     }
61   };
62   
63   /// InlineCostAnalyzer - Cost analyzer used by inliner.
64   class InlineCostAnalyzer {
65     struct ArgInfo {
66     public:
67       unsigned ConstantWeight;
68       unsigned AllocaWeight;
69       
70       ArgInfo(unsigned CWeight, unsigned AWeight)
71         : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
72     };
73     
74     // FunctionInfo - For each function, calculate the size of it in blocks and
75     // instructions.
76     struct FunctionInfo {
77       /// NeverInline - True if this callee should never be inlined into a
78       /// caller.
79       bool NeverInline;
80       
81       /// NumInsts, NumBlocks - Keep track of how large each function is, which
82       /// is used to estimate the code size cost of inlining it.
83       unsigned NumInsts, NumBlocks;
84
85       /// NumVectorInsts - Keep track of how many instructions produce vector
86       /// values.  The inliner is being more aggressive with inlining vector
87       /// kernels.
88       unsigned NumVectorInsts;
89       
90       /// ArgumentWeights - Each formal argument of the function is inspected to
91       /// see if it is used in any contexts where making it a constant or alloca
92       /// would reduce the code size.  If so, we add some value to the argument
93       /// entry here.
94       std::vector<ArgInfo> ArgumentWeights;
95       
96       FunctionInfo() : NeverInline(false), NumInsts(0), NumBlocks(0),
97                        NumVectorInsts(0) {}
98       
99       /// analyzeFunction - Fill in the current structure with information
100       /// gleaned from the specified function.
101       void analyzeFunction(Function *F);
102
103       /// CountCodeReductionForConstant - Figure out an approximation for how
104       /// many instructions will be constant folded if the specified value is
105       /// constant.
106       unsigned CountCodeReductionForConstant(Value *V);
107       
108       /// CountCodeReductionForAlloca - Figure out an approximation of how much
109       /// smaller the function will be if it is inlined into a context where an
110       /// argument becomes an alloca.
111       ///
112       unsigned CountCodeReductionForAlloca(Value *V);
113     };
114
115     std::map<const Function *, FunctionInfo> CachedFunctionInfo;
116
117   public:
118
119     /// getInlineCost - The heuristic used to determine if we should inline the
120     /// function call or not.
121     ///
122     InlineCost getInlineCost(CallSite CS,
123                              SmallPtrSet<const Function *, 16> &NeverInline);
124
125     /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
126     /// higher threshold to determine if the function call should be inlined.
127     float getInlineFudgeFactor(CallSite CS);
128   };
129 }
130
131 #endif