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