InlineCost.h revision 48fdf9b37926a9c3debd3f0b5814a2a4b6bb5097
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/Analysis/CodeMetrics.h" 18#include <cassert> 19#include <climits> 20 21namespace llvm { 22 23 class CallSite; 24 class DataLayout; 25 class Function; 26 27 namespace InlineConstants { 28 // Various magic constants used to adjust heuristics. 29 const int InstrCost = 5; 30 const int IndirectCallThreshold = 100; 31 const int CallPenalty = 25; 32 const int LastCallToStaticBonus = -15000; 33 const int ColdccPenalty = 2000; 34 const int NoreturnPenalty = 10000; 35 /// Do not inline functions which allocate this many bytes on the stack 36 /// when the caller is recursive. 37 const unsigned TotalAllocaSizeRecursiveCaller = 1024; 38 } 39 40 /// \brief Represents the cost of inlining a function. 41 /// 42 /// This supports special values for functions which should "always" or 43 /// "never" be inlined. Otherwise, the cost represents a unitless amount; 44 /// smaller values increase the likelihood of the function being inlined. 45 /// 46 /// Objects of this type also provide the adjusted threshold for inlining 47 /// based on the information available for a particular callsite. They can be 48 /// directly tested to determine if inlining should occur given the cost and 49 /// threshold for this cost metric. 50 class InlineCost { 51 enum SentinelValues { 52 AlwaysInlineCost = INT_MIN, 53 NeverInlineCost = INT_MAX 54 }; 55 56 /// \brief The estimated cost of inlining this callsite. 57 const int Cost; 58 59 /// \brief The adjusted threshold against which this cost was computed. 60 const int Threshold; 61 62 // Trivial constructor, interesting logic in the factory functions below. 63 InlineCost(int Cost, int Threshold) 64 : Cost(Cost), Threshold(Threshold) {} 65 66 public: 67 static InlineCost get(int Cost, int Threshold) { 68 assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value"); 69 assert(Cost < NeverInlineCost && "Cost crosses sentinel value"); 70 return InlineCost(Cost, Threshold); 71 } 72 static InlineCost getAlways() { 73 return InlineCost(AlwaysInlineCost, 0); 74 } 75 static InlineCost getNever() { 76 return InlineCost(NeverInlineCost, 0); 77 } 78 79 /// \brief Test whether the inline cost is low enough for inlining. 80 operator bool() const { 81 return Cost < Threshold; 82 } 83 84 bool isAlways() const { return Cost == AlwaysInlineCost; } 85 bool isNever() const { return Cost == NeverInlineCost; } 86 bool isVariable() const { return !isAlways() && !isNever(); } 87 88 /// \brief Get the inline cost estimate. 89 /// It is an error to call this on an "always" or "never" InlineCost. 90 int getCost() const { 91 assert(isVariable() && "Invalid access of InlineCost"); 92 return Cost; 93 } 94 95 /// \brief Get the cost delta from the threshold for inlining. 96 /// Only valid if the cost is of the variable kind. Returns a negative 97 /// value if the cost is too high to inline. 98 int getCostDelta() const { return Threshold - getCost(); } 99 }; 100 101 /// InlineCostAnalyzer - Cost analyzer used by inliner. 102 class InlineCostAnalyzer { 103 // DataLayout if available, or null. 104 const DataLayout *TD; 105 106 public: 107 InlineCostAnalyzer(): TD(0) {} 108 109 void setDataLayout(const DataLayout *TData) { TD = TData; } 110 111 /// \brief Get an InlineCost object representing the cost of inlining this 112 /// callsite. 113 /// 114 /// Note that threshold is passed into this function. Only costs below the 115 /// threshold are computed with any accuracy. The threshold can be used to 116 /// bound the computation necessary to determine whether the cost is 117 /// sufficiently low to warrant inlining. 118 InlineCost getInlineCost(CallSite CS, int Threshold); 119 120 /// \brief Get an InlineCost with the callee explicitly specified. 121 /// This allows you to calculate the cost of inlining a function via a 122 /// pointer. This behaves exactly as the version with no explicit callee 123 /// parameter in all other respects. 124 // 125 // Note: This is used by out-of-tree passes, please do not remove without 126 // adding a replacement API. 127 InlineCost getInlineCost(CallSite CS, Function *Callee, int Threshold); 128 129 /// \brief Minimal filter to detect invalid constructs for inlining. 130 bool isInlineViable(Function &Callee); 131 }; 132} 133 134#endif 135