Assorted comment/naming fixes, 80-col violations, and reindentation.
[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 <map>
19 #include <vector>
20
21 namespace llvm {
22
23   class Value;
24   class Function;
25   class CallSite;
26
27   /// InlineCostAnalyzer - Cost analyzer used by inliner.
28   class InlineCostAnalyzer {
29     struct ArgInfo {
30     public:
31       unsigned ConstantWeight;
32       unsigned AllocaWeight;
33       
34       ArgInfo(unsigned CWeight, unsigned AWeight)
35         : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
36     };
37     
38     // FunctionInfo - For each function, calculate the size of it in blocks and
39     // instructions.
40     struct FunctionInfo {
41       /// NeverInline - True if this callee should never be inlined into a
42       /// caller.
43       bool NeverInline;
44       
45       /// NumInsts, NumBlocks - Keep track of how large each function is, which
46       /// is used to estimate the code size cost of inlining it.
47       unsigned NumInsts, NumBlocks;
48
49       /// NumVectorInsts - Keep track of how many instructions produce vector
50       /// values.  The inliner is being more aggressive with inlining vector
51       /// kernels.
52       unsigned NumVectorInsts;
53       
54       /// ArgumentWeights - Each formal argument of the function is inspected to
55       /// see if it is used in any contexts where making it a constant or alloca
56       /// would reduce the code size.  If so, we add some value to the argument
57       /// entry here.
58       std::vector<ArgInfo> ArgumentWeights;
59       
60       FunctionInfo() : NeverInline(false), NumInsts(0), NumBlocks(0),
61                        NumVectorInsts(0) {}
62       
63       /// analyzeFunction - Fill in the current structure with information
64       /// gleaned from the specified function.
65       void analyzeFunction(Function *F);
66
67       /// CountCodeReductionForConstant - Figure out an approximation for how
68       /// many instructions will be constant folded if the specified value is
69       /// constant.
70       unsigned CountCodeReductionForConstant(Value *V);
71       
72       /// CountCodeReductionForAlloca - Figure out an approximation of how much
73       /// smaller the function will be if it is inlined into a context where an
74       /// argument becomes an alloca.
75       ///
76       unsigned CountCodeReductionForAlloca(Value *V);
77     };
78
79     std::map<const Function *, FunctionInfo> CachedFunctionInfo;
80
81   public:
82
83     /// getInlineCost - The heuristic used to determine if we should inline the
84     /// function call or not.
85     ///
86     int getInlineCost(CallSite CS,
87                       SmallPtrSet<const Function *, 16> &NeverInline);
88
89     /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
90     /// higher threshold to determine if the function call should be inlined.
91     float getInlineFudgeFactor(CallSite CS);
92   };
93 }
94
95 #endif