blob: 715f4462b299b3cdbbfb8c98dc3ef996b8350c40 [file] [log] [blame]
Chris Lattnerd075cc22003-08-31 19:10:30 +00001//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner530d4bf2003-05-29 15:11:31 +000010// This file implements bottom-up inlining of functions into callees.
Chris Lattner1c8d3f82002-04-18 18:52:03 +000011//
Chris Lattner2f7c9632001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13
Chris Lattnerd075cc22003-08-31 19:10:30 +000014#include "Inliner.h"
15#include "llvm/Function.h"
Chris Lattner530d4bf2003-05-29 15:11:31 +000016#include "llvm/iMemory.h"
Chris Lattnerd367d052003-08-24 06:59:28 +000017#include "llvm/Support/CallSite.h"
Chris Lattnerd075cc22003-08-31 19:10:30 +000018#include "llvm/Transforms/IPO.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000019
Brian Gaeke960707c2003-11-11 22:41:34 +000020namespace llvm {
21
Chris Lattner04805fa2002-02-26 21:46:54 +000022namespace {
Chris Lattner6dc0ae22003-10-06 15:52:43 +000023 // FunctionInfo - For each function, calculate the size of it in blocks and
24 // instructions.
25 struct FunctionInfo {
26 unsigned NumInsts, NumBlocks;
27
28 FunctionInfo() : NumInsts(0), NumBlocks(0) {}
29 };
30
31 class SimpleInliner : public Inliner {
32 std::map<const Function*, FunctionInfo> CachedFunctionInfo;
33 public:
Chris Lattnerd075cc22003-08-31 19:10:30 +000034 int getInlineCost(CallSite CS);
Chris Lattner04805fa2002-02-26 21:46:54 +000035 };
Chris Lattnerd075cc22003-08-31 19:10:30 +000036 RegisterOpt<SimpleInliner> X("inline", "Function Integration/Inlining");
Chris Lattner04805fa2002-02-26 21:46:54 +000037}
38
Chris Lattnerd075cc22003-08-31 19:10:30 +000039Pass *createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner530d4bf2003-05-29 15:11:31 +000040
Chris Lattnerd075cc22003-08-31 19:10:30 +000041// getInlineCost - The heuristic used to determine if we should inline the
42// function call or not.
Chris Lattner530d4bf2003-05-29 15:11:31 +000043//
Chris Lattnerd075cc22003-08-31 19:10:30 +000044int SimpleInliner::getInlineCost(CallSite CS) {
Chris Lattnerd367d052003-08-24 06:59:28 +000045 Instruction *TheCall = CS.getInstruction();
Chris Lattnerd367d052003-08-24 06:59:28 +000046 const Function *Callee = CS.getCalledFunction();
Chris Lattnerd367d052003-08-24 06:59:28 +000047 const Function *Caller = TheCall->getParent()->getParent();
Chris Lattner530d4bf2003-05-29 15:11:31 +000048
Chris Lattnerd075cc22003-08-31 19:10:30 +000049 // Don't inline a directly recursive call.
50 if (Caller == Callee) return 2000000000;
51
52 // InlineCost - This value measures how good of an inline candidate this call
53 // site is to inline. A lower inline cost make is more likely for the call to
54 // be inlined. This value may go negative.
Chris Lattner530d4bf2003-05-29 15:11:31 +000055 //
Chris Lattnerd075cc22003-08-31 19:10:30 +000056 int InlineCost = 0;
Chris Lattner530d4bf2003-05-29 15:11:31 +000057
58 // If there is only one call of the function, and it has internal linkage,
59 // make it almost guaranteed to be inlined.
60 //
Chris Lattner2d9c1172003-10-20 05:54:26 +000061 if (Callee->hasInternalLinkage() && Callee->hasOneUse())
Chris Lattnerd075cc22003-08-31 19:10:30 +000062 InlineCost -= 30000;
Chris Lattner530d4bf2003-05-29 15:11:31 +000063
Misha Brukman8b2bd4e2003-10-10 17:57:28 +000064 // Add to the inline quality for properties that make the call valuable to
Chris Lattner530d4bf2003-05-29 15:11:31 +000065 // inline. This includes factors that indicate that the result of inlining
66 // the function will be optimizable. Currently this just looks at arguments
67 // passed into the function.
68 //
Chris Lattnerd367d052003-08-24 06:59:28 +000069 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
70 I != E; ++I) {
Chris Lattner530d4bf2003-05-29 15:11:31 +000071 // Each argument passed in has a cost at both the caller and the callee
72 // sides. This favors functions that take many arguments over functions
73 // that take few arguments.
Chris Lattnerd075cc22003-08-31 19:10:30 +000074 InlineCost -= 20;
Chris Lattner530d4bf2003-05-29 15:11:31 +000075
76 // If this is a function being passed in, it is very likely that we will be
77 // able to turn an indirect function call into a direct function call.
78 if (isa<Function>(I))
Chris Lattnerd075cc22003-08-31 19:10:30 +000079 InlineCost -= 100;
Chris Lattner530d4bf2003-05-29 15:11:31 +000080
81 // If a constant, global variable or alloca is passed in, inlining this
82 // function is likely to allow significant future optimization possibilities
83 // (constant propagation, scalar promotion, and scalarization), so encourage
84 // the inlining of the function.
85 //
86 else if (isa<Constant>(I) || isa<GlobalVariable>(I) || isa<AllocaInst>(I))
Chris Lattnerd075cc22003-08-31 19:10:30 +000087 InlineCost -= 60;
Chris Lattner530d4bf2003-05-29 15:11:31 +000088 }
89
90 // Now that we have considered all of the factors that make the call site more
91 // likely to be inlined, look at factors that make us not want to inline it.
Chris Lattner6dc0ae22003-10-06 15:52:43 +000092 FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
Chris Lattner530d4bf2003-05-29 15:11:31 +000093
Chris Lattner6dc0ae22003-10-06 15:52:43 +000094 // If we haven't calculated this information yet...
95 if (CalleeFI.NumBlocks == 0) {
96 unsigned NumInsts = 0, NumBlocks = 0;
97
98 // Look at the size of the callee. Each basic block counts as 20 units, and
99 // each instruction counts as 10.
100 for (Function::const_iterator BB = Callee->begin(), E = Callee->end();
101 BB != E; ++BB) {
102 NumInsts += BB->size();
103 NumBlocks++;
104 }
105 CalleeFI.NumBlocks = NumBlocks;
106 CalleeFI.NumInsts = NumInsts;
107 }
108
Chris Lattnerf8492532003-10-07 19:33:31 +0000109 // Don't inline into something too big, which would make it bigger. Here, we
110 // count each basic block as a single unit.
111 InlineCost += Caller->size()*2;
112
113
114 // Look at the size of the callee. Each basic block counts as 20 units, and
Chris Lattner530d4bf2003-05-29 15:11:31 +0000115 // each instruction counts as 10.
Chris Lattner6dc0ae22003-10-06 15:52:43 +0000116 InlineCost += CalleeFI.NumInsts*10 + CalleeFI.NumBlocks*20;
Chris Lattnerd075cc22003-08-31 19:10:30 +0000117 return InlineCost;
Chris Lattner530d4bf2003-05-29 15:11:31 +0000118}
Brian Gaeke960707c2003-11-11 22:41:34 +0000119
120} // End llvm namespace