509217b00c7b411a845a4d445865df1b1df73da4
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements bottom-up inlining of functions into callees.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef INLINECOST_H
15 #define 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       // NumInsts, NumBlocks - Keep track of how large each function is, which is
42       // used to estimate the code size cost of inlining it.
43       unsigned NumInsts, NumBlocks;
44       
45       // ArgumentWeights - Each formal argument of the function is inspected to
46       // see if it is used in any contexts where making it a constant or alloca
47       // would reduce the code size.  If so, we add some value to the argument
48       // entry here.
49       std::vector<ArgInfo> ArgumentWeights;
50       
51       FunctionInfo() : NumInsts(0), NumBlocks(0) {}
52       
53       /// analyzeFunction - Fill in the current structure with information gleaned
54       /// from the specified function.
55       void analyzeFunction(Function *F);
56
57       // CountCodeReductionForConstant - Figure out an approximation for how many
58       // instructions will be constant folded if the specified value is constant.
59       //
60       unsigned CountCodeReductionForConstant(Value *V);
61       
62       // CountCodeReductionForAlloca - Figure out an approximation of how much smaller
63       // the function will be if it is inlined into a context where an argument
64       // becomes an alloca.
65       //
66       unsigned CountCodeReductionForAlloca(Value *V);
67     };
68
69     std::map<const Function *, FunctionInfo>CachedFunctionInfo;
70
71   public:
72
73     // getInlineCost - The heuristic used to determine if we should inline the
74     // function call or not.
75     //
76     int getInlineCost(CallSite CS, SmallPtrSet<const Function *, 16> &NeverInline);
77   };
78 }
79
80 #endif