0cba135222b9b1bf64b9bce5905588743b4bc889
[oota-llvm.git] / include / llvm / Analysis / InlineCost.h
1 //===- InlineCost.h - 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 "llvm/Function.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/ValueMap.h"
21 #include "llvm/Analysis/CodeMetrics.h"
22 #include <cassert>
23 #include <climits>
24 #include <vector>
25
26 namespace llvm {
27
28   class CallSite;
29   class TargetData;
30
31   namespace InlineConstants {
32     // Various magic constants used to adjust heuristics.
33     const int InstrCost = 5;
34     const int IndirectCallThreshold = 100;
35     const int CallPenalty = 25;
36     const int LastCallToStaticBonus = -15000;
37     const int ColdccPenalty = 2000;
38     const int NoreturnPenalty = 10000;
39   }
40
41   /// \brief Represents the cost of inlining a function.
42   ///
43   /// This supports special values for functions which should "always" or
44   /// "never" be inlined. Otherwise, the cost represents a unitless amount;
45   /// smaller values increase the likelihood of the function being inlined.
46   ///
47   /// Objects of this type also provide the adjusted threshold for inlining
48   /// based on the information available for a particular callsite. They can be
49   /// directly tested to determine if inlining should occur given the cost and
50   /// threshold for this cost metric.
51   class InlineCost {
52     enum SentinelValues {
53       AlwaysInlineCost = INT_MIN,
54       NeverInlineCost = INT_MAX
55     };
56
57     /// \brief The estimated cost of inlining this callsite.
58     const int Cost;
59
60     /// \brief The adjusted threshold against which this cost was computed.
61     const int Threshold;
62
63     // Trivial constructor, interesting logic in the factory functions below.
64     InlineCost(int Cost, int Threshold)
65       : Cost(Cost), Threshold(Threshold) {}
66
67   public:
68     static InlineCost get(int Cost, int Threshold) {
69       assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
70       assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
71       return InlineCost(Cost, Threshold);
72     }
73     static InlineCost getAlways() {
74       return InlineCost(AlwaysInlineCost, 0);
75     }
76     static InlineCost getNever() {
77       return InlineCost(NeverInlineCost, 0);
78     }
79
80     /// \brief Test whether the inline cost is low enough for inlining.
81     operator bool() const {
82       return Cost < Threshold;
83     }
84
85     bool isAlways() const   { return Cost == AlwaysInlineCost; }
86     bool isNever() const    { return Cost == NeverInlineCost; }
87     bool isVariable() const { return !isAlways() && !isNever(); }
88
89     /// \brief Get the inline cost estimate.
90     /// It is an error to call this on an "always" or "never" InlineCost.
91     int getCost() const {
92       assert(isVariable() && "Invalid access of InlineCost");
93       return Cost;
94     }
95
96     /// \brief Get the cost delta from the threshold for inlining.
97     /// Only valid if the cost is of the variable kind. Returns a negative
98     /// value if the cost is too high to inline.
99     int getCostDelta() const { return Threshold - getCost(); }
100   };
101
102   /// InlineCostAnalyzer - Cost analyzer used by inliner.
103   class InlineCostAnalyzer {
104     // TargetData if available, or null.
105     const TargetData *TD;
106
107   public:
108     InlineCostAnalyzer(): TD(0) {}
109
110     void setTargetData(const TargetData *TData) { TD = TData; }
111
112     /// \brief Get an InlineCost object representing the cost of inlining this
113     /// callsite.
114     ///
115     /// Note that threshold is passed into this function. Only costs below the
116     /// threshold are computed with any accuracy. The threshold can be used to
117     /// bound the computation necessary to determine whether the cost is
118     /// sufficiently low to warrant inlining.
119     InlineCost getInlineCost(CallSite CS, int Threshold);
120     /// getCalledFunction - The heuristic used to determine if we should inline
121     /// the function call or not.  The callee is explicitly specified, to allow
122     /// you to calculate the cost of inlining a function via a pointer.  This
123     /// behaves exactly as the version with no explicit callee parameter in all
124     /// other respects.
125     //
126     //  Note: This is used by out-of-tree passes, please do not remove without
127     //  adding a replacement API.
128     InlineCost getInlineCost(CallSite CS, Function *Callee, int Threshold);
129   };
130 }
131
132 #endif