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 /// NumInsts, NumBlocks - Keep track of how large each function is, which
82 /// is used to estimate the code size cost of inlining it.
83 unsigned NumInsts, NumBlocks;
85 /// NumVectorInsts - Keep track of how many instructions produce vector
86 /// values. The inliner is being more aggressive with inlining vector
88 unsigned NumVectorInsts;
90 /// ArgumentWeights - Each formal argument of the function is inspected to
91 /// see if it is used in any contexts where making it a constant or alloca
92 /// would reduce the code size. If so, we add some value to the argument
94 std::vector<ArgInfo> ArgumentWeights;
96 FunctionInfo() : NeverInline(false), NumInsts(0), NumBlocks(0),
99 /// analyzeFunction - Fill in the current structure with information
100 /// gleaned from the specified function.
101 void analyzeFunction(Function *F);
103 /// CountCodeReductionForConstant - Figure out an approximation for how
104 /// many instructions will be constant folded if the specified value is
106 unsigned CountCodeReductionForConstant(Value *V);
108 /// CountCodeReductionForAlloca - Figure out an approximation of how much
109 /// smaller the function will be if it is inlined into a context where an
110 /// argument becomes an alloca.
112 unsigned CountCodeReductionForAlloca(Value *V);
115 std::map<const Function *, FunctionInfo> CachedFunctionInfo;
119 /// getInlineCost - The heuristic used to determine if we should inline the
120 /// function call or not.
122 InlineCost getInlineCost(CallSite CS,
123 SmallPtrSet<const Function *, 16> &NeverInline);
125 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
126 /// higher threshold to determine if the function call should be inlined.
127 float getInlineFudgeFactor(CallSite CS);