InlineCost.h revision 220d2d7b50ddbbab1fe5e57fe9f39e72ef430a89
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/ValueMap.h" 20#include "llvm/Analysis/CodeMetrics.h" 21#include <cassert> 22#include <climits> 23#include <vector> 24 25namespace llvm { 26 27 class CallSite; 28 template<class PtrType, unsigned SmallSize> 29 class SmallPtrSet; 30 class TargetData; 31 32 namespace InlineConstants { 33 // Various magic constants used to adjust heuristics. 34 const int InstrCost = 5; 35 const int IndirectCallBonus = -100; 36 const int CallPenalty = 25; 37 const int LastCallToStaticBonus = -15000; 38 const int ColdccPenalty = 2000; 39 const int NoreturnPenalty = 10000; 40 } 41 42 /// InlineCost - Represent the cost of inlining a function. This 43 /// supports special values for functions which should "always" or 44 /// "never" be inlined. Otherwise, the cost represents a unitless 45 /// amount; smaller values increase the likelihood of the function 46 /// being inlined. 47 class InlineCost { 48 enum Kind { 49 Value, 50 Always, 51 Never 52 }; 53 54 // This is a do-it-yourself implementation of 55 // int Cost : 30; 56 // unsigned Type : 2; 57 // We used to use bitfields, but they were sometimes miscompiled (PR3822). 58 enum { TYPE_BITS = 2 }; 59 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS }; 60 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS; 61 62 Kind getType() const { 63 return Kind(TypedCost >> COST_BITS); 64 } 65 66 int getCost() const { 67 // Sign-extend the bottom COST_BITS bits. 68 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS; 69 } 70 71 InlineCost(int C, int T) { 72 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS); 73 assert(getCost() == C && "Cost exceeds InlineCost precision"); 74 } 75 public: 76 static InlineCost get(int Cost) { return InlineCost(Cost, Value); } 77 static InlineCost getAlways() { return InlineCost(0, Always); } 78 static InlineCost getNever() { return InlineCost(0, Never); } 79 80 bool isVariable() const { return getType() == Value; } 81 bool isAlways() const { return getType() == Always; } 82 bool isNever() const { return getType() == Never; } 83 84 /// getValue() - Return a "variable" inline cost's amount. It is 85 /// an error to call this on an "always" or "never" InlineCost. 86 int getValue() const { 87 assert(getType() == Value && "Invalid access of InlineCost"); 88 return getCost(); 89 } 90 }; 91 92 /// InlineCostAnalyzer - Cost analyzer used by inliner. 93 class InlineCostAnalyzer { 94 struct ArgInfo { 95 public: 96 unsigned ConstantWeight; 97 unsigned AllocaWeight; 98 99 ArgInfo(unsigned CWeight, unsigned AWeight) 100 : ConstantWeight(CWeight), AllocaWeight(AWeight) 101 {} 102 }; 103 104 struct FunctionInfo { 105 CodeMetrics Metrics; 106 107 /// ArgumentWeights - Each formal argument of the function is inspected to 108 /// see if it is used in any contexts where making it a constant or alloca 109 /// would reduce the code size. If so, we add some value to the argument 110 /// entry here. 111 std::vector<ArgInfo> ArgumentWeights; 112 113 /// PointerArgPairWeights - Weights to use when giving an inline bonus to 114 /// a call site due to correlated pairs of pointers. 115 DenseMap<std::pair<unsigned, unsigned>, unsigned> PointerArgPairWeights; 116 117 /// countCodeReductionForConstant - Figure out an approximation for how 118 /// many instructions will be constant folded if the specified value is 119 /// constant. 120 unsigned countCodeReductionForConstant(const CodeMetrics &Metrics, 121 Value *V); 122 123 /// countCodeReductionForAlloca - Figure out an approximation of how much 124 /// smaller the function will be if it is inlined into a context where an 125 /// argument becomes an alloca. 126 unsigned countCodeReductionForAlloca(const CodeMetrics &Metrics, 127 Value *V); 128 129 /// countCodeReductionForPointerPair - Count the bonus to apply to an 130 /// inline call site where a pair of arguments are pointers and one 131 /// argument is a constant offset from the other. The idea is to 132 /// recognize a common C++ idiom where a begin and end iterator are 133 /// actually pointers, and many operations on the pair of them will be 134 /// constants if the function is called with arguments that have 135 /// a constant offset. 136 void countCodeReductionForPointerPair( 137 const CodeMetrics &Metrics, 138 DenseMap<Value *, unsigned> &PointerArgs, 139 Value *V, unsigned ArgIdx); 140 141 /// analyzeFunction - Add information about the specified function 142 /// to the current structure. 143 void analyzeFunction(Function *F, const TargetData *TD); 144 145 /// NeverInline - Returns true if the function should never be 146 /// inlined into any caller. 147 bool NeverInline(); 148 }; 149 150 // The Function* for a function can be changed (by ArgumentPromotion); 151 // the ValueMap will update itself when this happens. 152 ValueMap<const Function *, FunctionInfo> CachedFunctionInfo; 153 154 // TargetData if available, or null. 155 const TargetData *TD; 156 157 int CountBonusForConstant(Value *V, Constant *C = NULL); 158 int ConstantFunctionBonus(CallSite CS, Constant *C); 159 int getInlineSize(CallSite CS, Function *Callee); 160 int getInlineBonuses(CallSite CS, Function *Callee); 161 public: 162 InlineCostAnalyzer(): TD(0) {} 163 164 void setTargetData(const TargetData *TData) { TD = TData; } 165 166 /// getInlineCost - The heuristic used to determine if we should inline the 167 /// function call or not. 168 /// 169 InlineCost getInlineCost(CallSite CS, 170 SmallPtrSet<const Function *, 16> &NeverInline); 171 /// getCalledFunction - The heuristic used to determine if we should inline 172 /// the function call or not. The callee is explicitly specified, to allow 173 /// you to calculate the cost of inlining a function via a pointer. The 174 /// result assumes that the inlined version will always be used. You should 175 /// weight it yourself in cases where this callee will not always be called. 176 InlineCost getInlineCost(CallSite CS, 177 Function *Callee, 178 SmallPtrSet<const Function *, 16> &NeverInline); 179 180 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a 181 /// higher threshold to determine if the function call should be inlined. 182 float getInlineFudgeFactor(CallSite CS); 183 184 /// resetCachedFunctionInfo - erase any cached cost info for this function. 185 void resetCachedCostInfo(Function* Caller) { 186 CachedFunctionInfo[Caller] = FunctionInfo(); 187 } 188 189 /// growCachedCostInfo - update the cached cost info for Caller after Callee 190 /// has been inlined. If Callee is NULL it means a dead call has been 191 /// eliminated. 192 void growCachedCostInfo(Function* Caller, Function* Callee); 193 194 /// clear - empty the cache of inline costs 195 void clear(); 196 }; 197 198 /// callIsSmall - If a call is likely to lower to a single target instruction, 199 /// or is otherwise deemed small return true. 200 bool callIsSmall(const Function *Callee); 201} 202 203#endif 204