InlineCost.h revision 42c7d23c6d56e0743169d94025264eaf17eb799d
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 <cassert> 18#include <climits> 19#include <vector> 20#include "llvm/ADT/DenseMap.h" 21#include "llvm/ADT/ValueMap.h" 22#include "llvm/Analysis/CodeMetrics.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 namespace InlineConstants { 34 // Various magic constants used to adjust heuristics. 35 const int InstrCost = 5; 36 const int IndirectCallBonus = 500; 37 const int CallPenalty = 25; 38 const int LastCallToStaticBonus = -15000; 39 const int ColdccPenalty = 2000; 40 const int NoreturnPenalty = 10000; 41 } 42 43 /// InlineCost - Represent the cost of inlining a function. This 44 /// supports special values for functions which should "always" or 45 /// "never" be inlined. Otherwise, the cost represents a unitless 46 /// amount; smaller values increase the likelyhood of the function 47 /// being inlined. 48 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 100 ArgInfo(unsigned CWeight, unsigned AWeight) 101 : ConstantWeight(CWeight), AllocaWeight(AWeight) {} 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 /// CountCodeReductionForConstant - Figure out an approximation for how 114 /// many instructions will be constant folded if the specified value is 115 /// constant. 116 unsigned CountCodeReductionForConstant(Value *V); 117 118 /// CountCodeReductionForAlloca - Figure out an approximation of how much 119 /// smaller the function will be if it is inlined into a context where an 120 /// argument becomes an alloca. 121 /// 122 unsigned CountCodeReductionForAlloca(Value *V); 123 124 /// analyzeFunction - Add information about the specified function 125 /// to the current structure. 126 void analyzeFunction(Function *F); 127 128 /// NeverInline - Returns true if the function should never be 129 /// inlined into any caller. 130 bool NeverInline(); 131 }; 132 133 // The Function* for a function can be changed (by ArgumentPromotion); 134 // the ValueMap will update itself when this happens. 135 ValueMap<const Function *, FunctionInfo> CachedFunctionInfo; 136 137 public: 138 139 /// getInlineCost - The heuristic used to determine if we should inline the 140 /// function call or not. 141 /// 142 InlineCost getInlineCost(CallSite CS, 143 SmallPtrSet<const Function *, 16> &NeverInline); 144 /// getCalledFunction - The heuristic used to determine if we should inline 145 /// the function call or not. The callee is explicitly specified, to allow 146 /// you to calculate the cost of inlining a function via a pointer. The 147 /// result assumes that the inlined version will always be used. You should 148 /// weight it yourself in cases where this callee will not always be called. 149 InlineCost getInlineCost(CallSite CS, 150 Function *Callee, 151 SmallPtrSet<const Function *, 16> &NeverInline); 152 153 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a 154 /// higher threshold to determine if the function call should be inlined. 155 float getInlineFudgeFactor(CallSite CS); 156 157 /// resetCachedFunctionInfo - erase any cached cost info for this function. 158 void resetCachedCostInfo(Function* Caller) { 159 CachedFunctionInfo[Caller] = FunctionInfo(); 160 } 161 162 /// growCachedCostInfo - update the cached cost info for Caller after Callee 163 /// has been inlined. If Callee is NULL it means a dead call has been 164 /// eliminated. 165 void growCachedCostInfo(Function* Caller, Function* Callee); 166 167 /// clear - empty the cache of inline costs 168 void clear(); 169 }; 170 171 /// callIsSmall - If a call is likely to lower to a single target instruction, 172 /// or is otherwise deemed small return true. 173 bool callIsSmall(const Function *Callee); 174} 175 176#endif 177