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