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