Do not inline functions with (dynamic) alloca into
[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       /// usesDynamicAlloca - True if this function calls alloca (in the C sense).
82       bool usesDynamicAlloca;
83
84       /// NumInsts, NumBlocks - Keep track of how large each function is, which
85       /// is used to estimate the code size cost of inlining it.
86       unsigned NumInsts, NumBlocks;
87
88       /// NumVectorInsts - Keep track of how many instructions produce vector
89       /// values.  The inliner is being more aggressive with inlining vector
90       /// kernels.
91       unsigned NumVectorInsts;
92       
93       /// ArgumentWeights - Each formal argument of the function is inspected to
94       /// see if it is used in any contexts where making it a constant or alloca
95       /// would reduce the code size.  If so, we add some value to the argument
96       /// entry here.
97       std::vector<ArgInfo> ArgumentWeights;
98       
99       FunctionInfo() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
100                        NumBlocks(0), NumVectorInsts(0) {}
101       
102       /// analyzeFunction - Fill in the current structure with information
103       /// gleaned from the specified function.
104       void analyzeFunction(Function *F);
105
106       /// CountCodeReductionForConstant - Figure out an approximation for how
107       /// many instructions will be constant folded if the specified value is
108       /// constant.
109       unsigned CountCodeReductionForConstant(Value *V);
110       
111       /// CountCodeReductionForAlloca - Figure out an approximation of how much
112       /// smaller the function will be if it is inlined into a context where an
113       /// argument becomes an alloca.
114       ///
115       unsigned CountCodeReductionForAlloca(Value *V);
116     };
117
118     std::map<const Function *, FunctionInfo> CachedFunctionInfo;
119
120   public:
121
122     /// getInlineCost - The heuristic used to determine if we should inline the
123     /// function call or not.
124     ///
125     InlineCost getInlineCost(CallSite CS,
126                              SmallPtrSet<const Function *, 16> &NeverInline);
127
128     /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
129     /// higher threshold to determine if the function call should be inlined.
130     float getInlineFudgeFactor(CallSite CS);
131   };
132 }
133
134 #endif