1 //===- InlineCost.cpp - Cost analysis for inliner ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements heuristics for inlining decisions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_UTILS_INLINECOST_H
15 #define LLVM_TRANSFORMS_UTILS_INLINECOST_H
17 #include "llvm/ADT/SmallPtrSet.h"
28 /// InlineCost - Represent the cost of inlining a function. This
29 /// supports special values for functions which should "always" or
30 /// "never" be inlined. Otherwise, the cost represents a unitless
31 /// amount; smaller values increase the likelyhood of the function
43 InlineCost(int C, int T) : Cost(C), Type(T) {
44 assert(Cost == C && "Cost exceeds InlineCost precision");
47 static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
48 static InlineCost getAlways() { return InlineCost(0, Always); }
49 static InlineCost getNever() { return InlineCost(0, Never); }
51 bool isVariable() const { return Type == Value; }
52 bool isAlways() const { return Type == Always; }
53 bool isNever() const { return Type == Never; }
55 /// getValue() - Return a "variable" inline cost's amount. It is
56 /// an error to call this on an "always" or "never" InlineCost.
57 int getValue() const {
58 assert(Type == Value && "Invalid access of InlineCost");
63 /// InlineCostAnalyzer - Cost analyzer used by inliner.
64 class InlineCostAnalyzer {
67 unsigned ConstantWeight;
68 unsigned AllocaWeight;
70 ArgInfo(unsigned CWeight, unsigned AWeight)
71 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
74 // FunctionInfo - For each function, calculate the size of it in blocks and
77 /// NeverInline - True if this callee should never be inlined into a
81 /// usesDynamicAlloca - True if this function calls alloca (in the C sense).
82 bool usesDynamicAlloca;
84 /// NumInsts, NumBlocks - Keep track of how large each function is, which
85 /// is used to estimate the code size cost of inlining it.
86 unsigned NumInsts, NumBlocks;
88 /// NumVectorInsts - Keep track of how many instructions produce vector
89 /// values. The inliner is being more aggressive with inlining vector
91 unsigned NumVectorInsts;
93 /// ArgumentWeights - Each formal argument of the function is inspected to
94 /// see if it is used in any contexts where making it a constant or alloca
95 /// would reduce the code size. If so, we add some value to the argument
97 std::vector<ArgInfo> ArgumentWeights;
99 FunctionInfo() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
100 NumBlocks(0), NumVectorInsts(0) {}
102 /// analyzeFunction - Fill in the current structure with information
103 /// gleaned from the specified function.
104 void analyzeFunction(Function *F);
106 /// CountCodeReductionForConstant - Figure out an approximation for how
107 /// many instructions will be constant folded if the specified value is
109 unsigned CountCodeReductionForConstant(Value *V);
111 /// CountCodeReductionForAlloca - Figure out an approximation of how much
112 /// smaller the function will be if it is inlined into a context where an
113 /// argument becomes an alloca.
115 unsigned CountCodeReductionForAlloca(Value *V);
118 std::map<const Function *, FunctionInfo> CachedFunctionInfo;
122 /// getInlineCost - The heuristic used to determine if we should inline the
123 /// function call or not.
125 InlineCost getInlineCost(CallSite CS,
126 SmallPtrSet<const Function *, 16> &NeverInline);
128 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
129 /// higher threshold to determine if the function call should be inlined.
130 float getInlineFudgeFactor(CallSite CS);
132 /// resetCachedFunctionInfo - erase any cached cost info for this function.
133 void resetCachedCostInfo(Function* Caller) {
134 CachedFunctionInfo[Caller].NumBlocks = 0;