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 bottom-up inlining of functions into callees.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/SmallPtrSet.h"
27 /// InlineCostAnalyzer - Cost analyzer used by inliner.
28 class InlineCostAnalyzer {
31 unsigned ConstantWeight;
32 unsigned AllocaWeight;
34 ArgInfo(unsigned CWeight, unsigned AWeight)
35 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
38 // FunctionInfo - For each function, calculate the size of it in blocks and
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;
45 /// NumVectorInsts - Keep track how many instrctions produce vector
46 /// values. The inliner is being more aggressive with inlining vector
48 unsigned NumVectorInsts;
50 /// ArgumentWeights - Each formal argument of the function is inspected to
51 /// see if it is used in any contexts where making it a constant or alloca
52 /// would reduce the code size. If so, we add some value to the argument
54 std::vector<ArgInfo> ArgumentWeights;
56 FunctionInfo() : NumInsts(0), NumBlocks(0), NumVectorInsts(0) {}
58 /// analyzeFunction - Fill in the current structure with information
59 /// gleaned from the specified function.
60 void analyzeFunction(Function *F);
62 /// CountCodeReductionForConstant - Figure out an approximation for how
63 /// many instructions will be constant folded if the specified value is
65 unsigned CountCodeReductionForConstant(Value *V);
67 /// CountCodeReductionForAlloca - Figure out an approximation of how much
68 /// smaller the function will be if it is inlined into a context where an
69 /// argument becomes an alloca.
71 unsigned CountCodeReductionForAlloca(Value *V);
74 std::map<const Function *, FunctionInfo> CachedFunctionInfo;
78 /// getInlineCost - The heuristic used to determine if we should inline the
79 /// function call or not.
81 int getInlineCost(CallSite CS,
82 SmallPtrSet<const Function *, 16> &NeverInline);
84 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
85 /// higher threshold to determine if the function call should be inlined.
86 float getInlineFudgeFactor(CallSite CS);