InlineCost.h revision 5b13a1a4c3a90c1dfd4b5c2521b5900ff367b12c
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_ANALYSIS_INLINECOST_H
15#define LLVM_ANALYSIS_INLINECOST_H
16
17#include <cassert>
18#include <climits>
19#include <map>
20#include <vector>
21#include "llvm/ADT/DenseMap.h"
22
23namespace llvm {
24
25  class Value;
26  class Function;
27  class BasicBlock;
28  class CallSite;
29  template<class PtrType, unsigned SmallSize>
30  class SmallPtrSet;
31
32  // CodeMetrics - Calculate size and a few similar metrics for a set of
33  // basic blocks.
34  struct CodeMetrics {
35    /// NeverInline - True if this callee should never be inlined into a
36    /// caller.
37    bool NeverInline;
38
39    /// usesDynamicAlloca - True if this function calls alloca (in the C sense).
40    bool usesDynamicAlloca;
41
42    /// NumInsts, NumBlocks - Keep track of how large each function is, which
43    /// is used to estimate the code size cost of inlining it.
44    unsigned NumInsts, NumBlocks;
45
46    /// NumBBInsts - Keeps track of basic block code size estimates.
47    DenseMap<const BasicBlock *, unsigned> NumBBInsts;
48
49    /// NumCalls - Keep track of the number of calls to 'big' functions.
50    unsigned NumCalls;
51
52    /// NumVectorInsts - Keep track of how many instructions produce vector
53    /// values.  The inliner is being more aggressive with inlining vector
54    /// kernels.
55    unsigned NumVectorInsts;
56
57    /// NumRets - Keep track of how many Ret instructions the block contains.
58    unsigned NumRets;
59
60    CodeMetrics() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
61                    NumBlocks(0), NumCalls(0), NumVectorInsts(0), NumRets(0) {}
62
63    /// analyzeBasicBlock - Add information about the specified basic block
64    /// to the current structure.
65    void analyzeBasicBlock(const BasicBlock *BB);
66
67    /// analyzeFunction - Add information about the specified function
68    /// to the current structure.
69    void analyzeFunction(Function *F);
70  };
71
72  namespace InlineConstants {
73    // Various magic constants used to adjust heuristics.
74    const int InstrCost = 5;
75    const int IndirectCallBonus = 500;
76    const int CallPenalty = 25;
77    const int LastCallToStaticBonus = -15000;
78    const int ColdccPenalty = 2000;
79    const int NoreturnPenalty = 10000;
80  }
81
82  /// InlineCost - Represent the cost of inlining a function. This
83  /// supports special values for functions which should "always" or
84  /// "never" be inlined. Otherwise, the cost represents a unitless
85  /// amount; smaller values increase the likelyhood of the function
86  /// being inlined.
87  class InlineCost {
88    enum Kind {
89      Value,
90      Always,
91      Never
92    };
93
94    // This is a do-it-yourself implementation of
95    //   int Cost : 30;
96    //   unsigned Type : 2;
97    // We used to use bitfields, but they were sometimes miscompiled (PR3822).
98    enum { TYPE_BITS = 2 };
99    enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
100    unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
101
102    Kind getType() const {
103      return Kind(TypedCost >> COST_BITS);
104    }
105
106    int getCost() const {
107      // Sign-extend the bottom COST_BITS bits.
108      return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
109    }
110
111    InlineCost(int C, int T) {
112      TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
113      assert(getCost() == C && "Cost exceeds InlineCost precision");
114    }
115  public:
116    static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
117    static InlineCost getAlways() { return InlineCost(0, Always); }
118    static InlineCost getNever() { return InlineCost(0, Never); }
119
120    bool isVariable() const { return getType() == Value; }
121    bool isAlways() const { return getType() == Always; }
122    bool isNever() const { return getType() == Never; }
123
124    /// getValue() - Return a "variable" inline cost's amount. It is
125    /// an error to call this on an "always" or "never" InlineCost.
126    int getValue() const {
127      assert(getType() == Value && "Invalid access of InlineCost");
128      return getCost();
129    }
130  };
131
132  /// InlineCostAnalyzer - Cost analyzer used by inliner.
133  class InlineCostAnalyzer {
134    struct ArgInfo {
135    public:
136      unsigned ConstantWeight;
137      unsigned AllocaWeight;
138
139      ArgInfo(unsigned CWeight, unsigned AWeight)
140        : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
141    };
142
143    struct FunctionInfo {
144      CodeMetrics Metrics;
145
146      /// ArgumentWeights - Each formal argument of the function is inspected to
147      /// see if it is used in any contexts where making it a constant or alloca
148      /// would reduce the code size.  If so, we add some value to the argument
149      /// entry here.
150      std::vector<ArgInfo> ArgumentWeights;
151
152      /// CountCodeReductionForConstant - Figure out an approximation for how
153      /// many instructions will be constant folded if the specified value is
154      /// constant.
155      unsigned CountCodeReductionForConstant(Value *V);
156
157      /// CountCodeReductionForAlloca - Figure out an approximation of how much
158      /// smaller the function will be if it is inlined into a context where an
159      /// argument becomes an alloca.
160      ///
161      unsigned CountCodeReductionForAlloca(Value *V);
162
163      /// analyzeFunction - Add information about the specified function
164      /// to the current structure.
165      void analyzeFunction(Function *F);
166    };
167
168    std::map<const Function *, FunctionInfo> CachedFunctionInfo;
169
170  public:
171
172    /// getInlineCost - The heuristic used to determine if we should inline the
173    /// function call or not.
174    ///
175    InlineCost getInlineCost(CallSite CS,
176                             SmallPtrSet<const Function *, 16> &NeverInline);
177
178    /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
179    /// higher threshold to determine if the function call should be inlined.
180    float getInlineFudgeFactor(CallSite CS);
181
182    /// resetCachedFunctionInfo - erase any cached cost info for this function.
183    void resetCachedCostInfo(Function* Caller) {
184      CachedFunctionInfo[Caller] = FunctionInfo();
185    }
186
187    /// growCachedCostInfo - update the cached cost info for Caller after Callee
188    /// has been inlined. If Callee is NULL it means a dead call has been
189    /// eliminated.
190    void growCachedCostInfo(Function* Caller, Function* Callee);
191  };
192
193  /// callIsSmall - If a call is likely to lower to a single target instruction,
194  /// or is otherwise deemed small return true.
195  bool callIsSmall(const Function *Callee);
196}
197
198#endif
199