InlineCost.h revision f96769bed208b8a3f82b53771350dc0a743db85a
1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//===- InlineCost.cpp - Cost analysis for inliner ---------------*- C++ -*-===//
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//                     The LLVM Compiler Infrastructure
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// This file is distributed under the University of Illinois Open Source
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// License. See LICENSE.TXT for details.
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//===----------------------------------------------------------------------===//
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// This file implements heuristics for inlining decisions.
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//===----------------------------------------------------------------------===//
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef LLVM_ANALYSIS_INLINECOST_H
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define LLVM_ANALYSIS_INLINECOST_H
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <cassert>
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <climits>
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <map>
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <vector>
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "llvm/ADT/DenseMap.h"
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querunamespace llvm {
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  class Value;
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  class Function;
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  class BasicBlock;
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  class CallSite;
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  template<class PtrType, unsigned SmallSize>
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  class SmallPtrSet;
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  // CodeMetrics - Calculate size and a few similar metrics for a set of
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  // basic blocks.
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  struct CodeMetrics {
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// NeverInline - True if this callee should never be inlined into a
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// caller.
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    bool NeverInline;
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// usesDynamicAlloca - True if this function calls alloca (in the C sense).
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    bool usesDynamicAlloca;
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// NumInsts, NumBlocks - Keep track of how large each function is, which
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// is used to estimate the code size cost of inlining it.
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    unsigned NumInsts, NumBlocks;
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// NumBBInsts - Keeps track of basic block code size estimates.
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    DenseMap<const BasicBlock *, unsigned> NumBBInsts;
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// NumCalls - Keep track of the number of calls to 'big' functions.
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    unsigned NumCalls;
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// NumVectorInsts - Keep track of how many instructions produce vector
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// values.  The inliner is being more aggressive with inlining vector
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// kernels.
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    unsigned NumVectorInsts;
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// NumRets - Keep track of how many Ret instructions the block contains.
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    unsigned NumRets;
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    CodeMetrics() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    NumBlocks(0), NumCalls(0), NumVectorInsts(0), NumRets(0) {}
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// analyzeBasicBlock - Add information about the specified basic block
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// to the current structure.
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void analyzeBasicBlock(const BasicBlock *BB);
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// analyzeFunction - Add information about the specified function
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /// to the current structure.
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void analyzeFunction(Function *F);
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  };
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  namespace InlineConstants {
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // Various magic constants used to adjust heuristics.
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const int InstrCost = 5;
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const int IndirectCallBonus = 500;
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const int CallPenalty = 25;
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const int LastCallToStaticBonus = -15000;
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const int ColdccPenalty = 2000;
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const int NoreturnPenalty = 10000;
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  }
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  /// InlineCost - Represent the cost of inlining a function. This
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  /// supports special values for functions which should "always" or
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  /// "never" be inlined. Otherwise, the cost represents a unitless
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  /// amount; smaller values increase the likelyhood of the function
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  /// being inlined.
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  class InlineCost {
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    enum Kind {
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      Value,
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      Always,
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      Never
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    };
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // This is a do-it-yourself implementation of
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //   int Cost : 30;
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //   unsigned Type : 2;
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // We used to use bitfields, but they were sometimes miscompiled (PR3822).
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    enum { TYPE_BITS = 2 };
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    Kind getType() const {
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      return Kind(TypedCost >> COST_BITS);
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int getCost() const {
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      // Sign-extend the bottom COST_BITS bits.
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
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
194#endif
195