InlineCost.h revision 74fa7327d690e6ceda6ce77e4e5b8ef75cb12538
1ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown//===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===// 2ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 3ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// The LLVM Compiler Infrastructure 4ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 5ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// This file is distributed under the University of Illinois Open Source 6ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// License. See LICENSE.TXT for details. 7ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 8ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown//===----------------------------------------------------------------------===// 9ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 10ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// This file implements heuristics for inlining decisions. 11ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 12ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown//===----------------------------------------------------------------------===// 13ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 14ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#ifndef LLVM_ANALYSIS_INLINECOST_H 15ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#define LLVM_ANALYSIS_INLINECOST_H 16ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 17ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <cassert> 18ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <climits> 19ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <vector> 20ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "llvm/ADT/DenseMap.h" 21ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "llvm/ADT/ValueMap.h" 22ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "llvm/Analysis/CodeMetrics.h" 23ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 24ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownnamespace llvm { 25ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 26ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class Value; 27ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class Function; 28ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class BasicBlock; 29ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class CallSite; 30ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown template<class PtrType, unsigned SmallSize> 31ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class SmallPtrSet; 32ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 33ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown namespace InlineConstants { 34ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown // Various magic constants used to adjust heuristics. 35ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const int InstrCost = 5; 36ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const int IndirectCallBonus = 500; 37ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const int CallPenalty = 25; 38ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const int LastCallToStaticBonus = -15000; 39ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const int ColdccPenalty = 2000; 40ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const int NoreturnPenalty = 10000; 41ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown } 42ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 43ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// InlineCost - Represent the cost of inlining a function. This 44ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// supports special values for functions which should "always" or 45ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// "never" be inlined. Otherwise, the cost represents a unitless 46ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// amount; smaller values increase the likelyhood of the function 47ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// being inlined. 48ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class InlineCost { 49 enum Kind { 50 Value, 51 Always, 52 Never 53 }; 54 55 // This is a do-it-yourself implementation of 56 // int Cost : 30; 57 // unsigned Type : 2; 58 // We used to use bitfields, but they were sometimes miscompiled (PR3822). 59 enum { TYPE_BITS = 2 }; 60 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS }; 61 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS; 62 63 Kind getType() const { 64 return Kind(TypedCost >> COST_BITS); 65 } 66 67 int getCost() const { 68 // Sign-extend the bottom COST_BITS bits. 69 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS; 70 } 71 72 InlineCost(int C, int T) { 73 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS); 74 assert(getCost() == C && "Cost exceeds InlineCost precision"); 75 } 76 public: 77 static InlineCost get(int Cost) { return InlineCost(Cost, Value); } 78 static InlineCost getAlways() { return InlineCost(0, Always); } 79 static InlineCost getNever() { return InlineCost(0, Never); } 80 81 bool isVariable() const { return getType() == Value; } 82 bool isAlways() const { return getType() == Always; } 83 bool isNever() const { return getType() == Never; } 84 85 /// getValue() - Return a "variable" inline cost's amount. It is 86 /// an error to call this on an "always" or "never" InlineCost. 87 int getValue() const { 88 assert(getType() == Value && "Invalid access of InlineCost"); 89 return getCost(); 90 } 91 }; 92 93 /// InlineCostAnalyzer - Cost analyzer used by inliner. 94 class InlineCostAnalyzer { 95 struct ArgInfo { 96 public: 97 unsigned ConstantWeight; 98 unsigned AllocaWeight; 99 unsigned ConstantBonus; 100 101 ArgInfo(unsigned CWeight, unsigned AWeight, unsigned CBonus) 102 : ConstantWeight(CWeight), AllocaWeight(AWeight), ConstantBonus(CBonus) 103 {} 104 }; 105 106 struct FunctionInfo { 107 CodeMetrics Metrics; 108 109 /// ArgumentWeights - Each formal argument of the function is inspected to 110 /// see if it is used in any contexts where making it a constant or alloca 111 /// would reduce the code size. If so, we add some value to the argument 112 /// entry here. 113 std::vector<ArgInfo> ArgumentWeights; 114 115 116 117 /// analyzeFunction - Add information about the specified function 118 /// to the current structure. 119 void analyzeFunction(Function *F); 120 121 /// NeverInline - Returns true if the function should never be 122 /// inlined into any caller. 123 bool NeverInline(); 124 }; 125 126 // The Function* for a function can be changed (by ArgumentPromotion); 127 // the ValueMap will update itself when this happens. 128 ValueMap<const Function *, FunctionInfo> CachedFunctionInfo; 129 130 public: 131 132 /// getInlineCost - The heuristic used to determine if we should inline the 133 /// function call or not. 134 /// 135 InlineCost getInlineCost(CallSite CS, 136 SmallPtrSet<const Function *, 16> &NeverInline); 137 /// getCalledFunction - The heuristic used to determine if we should inline 138 /// the function call or not. The callee is explicitly specified, to allow 139 /// you to calculate the cost of inlining a function via a pointer. The 140 /// result assumes that the inlined version will always be used. You should 141 /// weight it yourself in cases where this callee will not always be called. 142 InlineCost getInlineCost(CallSite CS, 143 Function *Callee, 144 SmallPtrSet<const Function *, 16> &NeverInline); 145 146 /// getSpecializationBonus - The heuristic used to determine the per-call 147 /// performance boost for using a specialization of Callee with argument 148 /// SpecializedArgNos replaced by a constant. 149 int getSpecializationBonus(Function *Callee, 150 SmallVectorImpl<unsigned> &SpecializedArgNo); 151 152 /// getSpecializationCost - The heuristic used to determine the code-size 153 /// impact of creating a specialized version of Callee with argument 154 /// SpecializedArgNo replaced by a constant. 155 InlineCost getSpecializationCost(Function *Callee, 156 SmallVectorImpl<unsigned> &SpecializedArgNo); 157 158 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a 159 /// higher threshold to determine if the function call should be inlined. 160 float getInlineFudgeFactor(CallSite CS); 161 162 /// resetCachedFunctionInfo - erase any cached cost info for this function. 163 void resetCachedCostInfo(Function* Caller) { 164 CachedFunctionInfo[Caller] = FunctionInfo(); 165 } 166 167 /// growCachedCostInfo - update the cached cost info for Caller after Callee 168 /// has been inlined. If Callee is NULL it means a dead call has been 169 /// eliminated. 170 void growCachedCostInfo(Function* Caller, Function* Callee); 171 172 /// clear - empty the cache of inline costs 173 void clear(); 174 }; 175 176 /// callIsSmall - If a call is likely to lower to a single target instruction, 177 /// or is otherwise deemed small return true. 178 bool callIsSmall(const Function *Callee); 179} 180 181#endif 182