blob: cde2e4a729a7875c6069e00b5afd4e3519f2a745 [file] [log] [blame]
Chris Lattner62b7fd12002-04-07 20:49:59 +00001//===- FunctionInlining.cpp - Code to perform function inlining -----------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
Chris Lattner530d4bf2003-05-29 15:11:31 +00003// This file implements bottom-up inlining of functions into callees.
Chris Lattner1c8d3f82002-04-18 18:52:03 +00004//
Chris Lattner2f7c9632001-06-06 20:29:01 +00005//===----------------------------------------------------------------------===//
6
Chris Lattner16667512002-11-19 20:59:41 +00007#include "llvm/Transforms/IPO.h"
8#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/Module.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000010#include "llvm/Pass.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011#include "llvm/iOther.h"
Chris Lattner530d4bf2003-05-29 15:11:31 +000012#include "llvm/iMemory.h"
Chris Lattnerfbfcf012003-06-28 15:57:04 +000013#include "Support/CommandLine.h"
Chris Lattner8abcd562003-08-01 22:15:03 +000014#include "Support/Debug.h"
15#include "Support/Statistic.h"
Chris Lattner530d4bf2003-05-29 15:11:31 +000016#include <set>
Chris Lattner04805fa2002-02-26 21:46:54 +000017
18namespace {
Chris Lattner530d4bf2003-05-29 15:11:31 +000019 Statistic<> NumInlined("inline", "Number of functions inlined");
Chris Lattnerfbfcf012003-06-28 15:57:04 +000020 cl::opt<unsigned> // FIXME: 200 is VERY conservative
21 InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
22 cl::desc("Control the amount of inlining to perform (default = 200)"));
Chris Lattner530d4bf2003-05-29 15:11:31 +000023
24 struct FunctionInlining : public Pass {
25 virtual bool run(Module &M) {
26 bool Changed = false;
27 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
28 Changed |= doInlining(I);
29 ProcessedFunctions.clear();
30 return Changed;
Chris Lattner04805fa2002-02-26 21:46:54 +000031 }
Chris Lattner530d4bf2003-05-29 15:11:31 +000032
33 private:
34 std::set<Function*> ProcessedFunctions; // Prevent infinite recursion
35 bool doInlining(Function *F);
Chris Lattner04805fa2002-02-26 21:46:54 +000036 };
Chris Lattnerc8b70922002-07-26 21:12:46 +000037 RegisterOpt<FunctionInlining> X("inline", "Function Integration/Inlining");
Chris Lattner04805fa2002-02-26 21:46:54 +000038}
39
Chris Lattnerc8e66542002-04-27 06:56:12 +000040Pass *createFunctionInliningPass() { return new FunctionInlining(); }
Chris Lattner530d4bf2003-05-29 15:11:31 +000041
42
43// ShouldInlineFunction - The heuristic used to determine if we should inline
44// the function call or not.
45//
46static inline bool ShouldInlineFunction(const CallInst *CI) {
47 assert(CI->getParent() && CI->getParent()->getParent() &&
48 "Call not embedded into a function!");
49
50 const Function *Callee = CI->getCalledFunction();
51 if (Callee == 0 || Callee->isExternal())
52 return false; // Cannot inline an indirect call... or external function.
53
54 // Don't inline a recursive call.
55 const Function *Caller = CI->getParent()->getParent();
56 if (Caller == Callee) return false;
57
58 // InlineQuality - This value measures how good of an inline candidate this
59 // call site is to inline. The initial value determines how aggressive the
60 // inliner is. If this value is negative after the final computation,
61 // inlining is not performed.
62 //
Chris Lattnerfbfcf012003-06-28 15:57:04 +000063 int InlineQuality = InlineLimit;
Chris Lattner530d4bf2003-05-29 15:11:31 +000064
65 // If there is only one call of the function, and it has internal linkage,
66 // make it almost guaranteed to be inlined.
67 //
68 if (Callee->use_size() == 1 && Callee->hasInternalLinkage())
69 InlineQuality += 30000;
70
71 // Add to the inline quality for properties that make the call valueable to
72 // inline. This includes factors that indicate that the result of inlining
73 // the function will be optimizable. Currently this just looks at arguments
74 // passed into the function.
75 //
76 for (User::const_op_iterator I = CI->op_begin()+1, E = CI->op_end();
77 I != E; ++I){
78 // Each argument passed in has a cost at both the caller and the callee
79 // sides. This favors functions that take many arguments over functions
80 // that take few arguments.
81 InlineQuality += 20;
82
83 // If this is a function being passed in, it is very likely that we will be
84 // able to turn an indirect function call into a direct function call.
85 if (isa<Function>(I))
86 InlineQuality += 100;
87
88 // If a constant, global variable or alloca is passed in, inlining this
89 // function is likely to allow significant future optimization possibilities
90 // (constant propagation, scalar promotion, and scalarization), so encourage
91 // the inlining of the function.
92 //
93 else if (isa<Constant>(I) || isa<GlobalVariable>(I) || isa<AllocaInst>(I))
94 InlineQuality += 60;
95 }
96
97 // Now that we have considered all of the factors that make the call site more
98 // likely to be inlined, look at factors that make us not want to inline it.
99 // As soon as the inline quality gets negative, bail out.
100
101 // Look at the size of the callee. Each basic block counts as 20 units, and
102 // each instruction counts as 10.
103 for (Function::const_iterator BB = Callee->begin(), E = Callee->end();
104 BB != E; ++BB) {
105 InlineQuality -= BB->size()*10 + 20;
106 if (InlineQuality < 0) return false;
107 }
108
109 // Don't inline into something too big, which would make it bigger. Here, we
110 // count each basic block as a single unit.
111 for (Function::const_iterator BB = Caller->begin(), E = Caller->end();
112 BB != E; ++BB) {
113 --InlineQuality;
114 if (InlineQuality < 0) return false;
115 }
116
117 // If we get here, this call site is high enough "quality" to inline.
118 DEBUG(std::cerr << "Inlining in '" << Caller->getName()
119 << "', quality = " << InlineQuality << ": " << *CI);
120 return true;
121}
122
123
124// doInlining - Use a heuristic based approach to inline functions that seem to
125// look good.
126//
127bool FunctionInlining::doInlining(Function *F) {
128 // If we have already processed this function (ie, it is recursive) don't
129 // revisit.
130 std::set<Function*>::iterator PFI = ProcessedFunctions.lower_bound(F);
131 if (PFI != ProcessedFunctions.end() && *PFI == F) return false;
132
133 // Insert the function in the set so it doesn't get revisited.
134 ProcessedFunctions.insert(PFI, F);
135
136 bool Changed = false;
137 for (Function::iterator BB = F->begin(); BB != F->end(); ++BB)
138 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
139 bool ShouldInc = true;
140 // Found a call instruction? FIXME: This should also handle INVOKEs
141 if (CallInst *CI = dyn_cast<CallInst>(I)) {
142 if (Function *Callee = CI->getCalledFunction())
143 doInlining(Callee); // Inline in callees before callers!
144
145 // Decide whether we should inline this function...
146 if (ShouldInlineFunction(CI)) {
147 // Save an iterator to the instruction before the call if it exists,
148 // otherwise get an iterator at the end of the block... because the
149 // call will be destroyed.
150 //
151 BasicBlock::iterator SI;
152 if (I != BB->begin()) {
153 SI = I; --SI; // Instruction before the call...
154 } else {
155 SI = BB->end();
156 }
157
158 // Attempt to inline the function...
159 if (InlineFunction(CI)) {
160 ++NumInlined;
161 Changed = true;
162 // Move to instruction before the call...
163 I = (SI == BB->end()) ? BB->begin() : SI;
164 ShouldInc = false; // Don't increment iterator until next time
165 }
166 }
167 }
168 if (ShouldInc) ++I;
169 }
170
171 return Changed;
172}
173