Remove a bunch of empty, dead, and no-op methods from all of these
[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 CostKind {
53       CK_Variable,
54       CK_Always,
55       CK_Never
56     };
57
58     const int      Cost : 30; // The inlining cost if neither always nor never.
59     const unsigned Kind : 2;  // The type of cost, one of CostKind above.
60
61     /// \brief The adjusted threshold against which this cost should be tested.
62     const int Threshold;
63
64     // Trivial constructor, interesting logic in the factory functions below.
65     InlineCost(int Cost, CostKind Kind, int Threshold)
66       : Cost(Cost), Kind(Kind), Threshold(Threshold) {}
67
68   public:
69     static InlineCost get(int Cost, int Threshold) {
70       InlineCost Result(Cost, CK_Variable, Threshold);
71       assert(Result.Cost == Cost && "Cost exceeds InlineCost precision");
72       return Result;
73     }
74     static InlineCost getAlways() {
75       return InlineCost(0, CK_Always, 0);
76     }
77     static InlineCost getNever() {
78       return InlineCost(0, CK_Never, 0);
79     }
80
81     /// \brief Test whether the inline cost is low enough for inlining.
82     operator bool() const {
83       if (isAlways()) return true;
84       if (isNever()) return false;
85       return Cost < Threshold;
86     }
87
88     bool isVariable() const { return Kind == CK_Variable; }
89     bool isAlways() const   { return Kind == CK_Always; }
90     bool isNever() const    { return Kind == CK_Never; }
91
92     /// getCost() - Return a "variable" inline cost's amount. It is
93     /// an error to call this on an "always" or "never" InlineCost.
94     int getCost() const {
95       assert(Kind == CK_Variable && "Invalid access of InlineCost");
96       return Cost;
97     }
98
99     /// \brief Get the cost delta from the threshold for inlining.
100     /// Only valid if the cost is of the variable kind. Returns a negative
101     /// value if the cost is too high to inline.
102     int getCostDelta() const {
103       return Threshold - getCost();
104     }
105   };
106
107   /// InlineCostAnalyzer - Cost analyzer used by inliner.
108   class InlineCostAnalyzer {
109     // TargetData if available, or null.
110     const TargetData *TD;
111
112   public:
113     InlineCostAnalyzer(): TD(0) {}
114
115     void setTargetData(const TargetData *TData) { TD = TData; }
116
117     /// \brief Get an InlineCost object representing the cost of inlining this
118     /// callsite.
119     ///
120     /// Note that threshold is passed into this function. Only costs below the
121     /// threshold are computed with any accuracy. The threshold can be used to
122     /// bound the computation necessary to determine whether the cost is
123     /// sufficiently low to warrant inlining.
124     InlineCost getInlineCost(CallSite CS, int Threshold);
125   };
126
127   /// callIsSmall - If a call is likely to lower to a single target instruction,
128   /// or is otherwise deemed small return true.
129   bool callIsSmall(const Function *Callee);
130 }
131
132 #endif