blob: afa4890f035e6ed839760a960f52615014f9350b [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 Lattner62b7fd12002-04-07 20:49:59 +00003// This file implements inlining of functions.
Chris Lattner2f7c9632001-06-06 20:29:01 +00004//
5// Specifically, this:
Chris Lattner62b7fd12002-04-07 20:49:59 +00006// * Exports functionality to inline any function call
7// * Inlines functions that consist of a single basic block
8// * Is able to inline ANY function call
9// . Has a smart heuristic for when to inline a function
Chris Lattner2f7c9632001-06-06 20:29:01 +000010//
Chris Lattner1c8d3f82002-04-18 18:52:03 +000011// FIXME: This pass should transform alloca instructions in the called function
Chris Lattner661841c2003-01-13 00:27:23 +000012// into malloc/free pairs! Or perhaps it should refuse to inline them!
Chris Lattner1c8d3f82002-04-18 18:52:03 +000013//
Chris Lattner2f7c9632001-06-06 20:29:01 +000014//===----------------------------------------------------------------------===//
15
Chris Lattner16667512002-11-19 20:59:41 +000016#include "llvm/Transforms/IPO.h"
17#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/Module.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000019#include "llvm/Pass.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include "llvm/iOther.h"
Chris Lattnerf8e4dc32002-04-08 22:03:00 +000023#include "llvm/Type.h"
Chris Lattner3cf37822002-10-01 22:38:37 +000024#include "Support/Statistic.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000025#include <algorithm>
Chris Lattner113f4f42002-06-25 16:13:24 +000026
Chris Lattner3cf37822002-10-01 22:38:37 +000027static Statistic<> NumInlined("inline", "Number of functions inlined");
Chris Lattner7f74a562002-01-20 22:54:45 +000028using std::cerr;
Chris Lattner2f7c9632001-06-06 20:29:01 +000029
Chris Lattnerc8e66542002-04-27 06:56:12 +000030// InlineFunction - This function forcibly inlines the called function into the
Chris Lattner2f7c9632001-06-06 20:29:01 +000031// basic block of the caller. This returns false if it is not possible to
32// inline this call. The program is still in a well defined state if this
33// occurs though.
34//
35// Note that this only does one level of inlining. For example, if the
36// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
37// exists in the instruction stream. Similiarly this will inline a recursive
Chris Lattner62b7fd12002-04-07 20:49:59 +000038// function by one level.
Chris Lattner2f7c9632001-06-06 20:29:01 +000039//
Chris Lattner113f4f42002-06-25 16:13:24 +000040bool InlineFunction(CallInst *CI) {
41 assert(isa<CallInst>(CI) && "InlineFunction only works on CallInst nodes");
42 assert(CI->getParent() && "Instruction not embedded in basic block!");
43 assert(CI->getParent()->getParent() && "Instruction not in function!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000044
Chris Lattner113f4f42002-06-25 16:13:24 +000045 const Function *CalledFunc = CI->getCalledFunction();
46 if (CalledFunc == 0 || // Can't inline external function or indirect call!
47 CalledFunc->isExternal()) return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +000048
Chris Lattner113f4f42002-06-25 16:13:24 +000049 //cerr << "Inlining " << CalledFunc->getName() << " into "
Chris Lattner7f74a562002-01-20 22:54:45 +000050 // << CurrentMeth->getName() << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000051
52 BasicBlock *OrigBB = CI->getParent();
53
54 // Call splitBasicBlock - The original basic block now ends at the instruction
55 // immediately before the call. The original basic block now ends with an
56 // unconditional branch to NewBB, and NewBB starts with the call instruction.
57 //
Chris Lattner113f4f42002-06-25 16:13:24 +000058 BasicBlock *NewBB = OrigBB->splitBasicBlock(CI);
Chris Lattnerf20671a2002-02-25 00:31:02 +000059 NewBB->setName("InlinedFunctionReturnNode");
Chris Lattner2f7c9632001-06-06 20:29:01 +000060
61 // Remove (unlink) the CallInst from the start of the new basic block.
62 NewBB->getInstList().remove(CI);
63
64 // If we have a return value generated by this call, convert it into a PHI
65 // node that gets values from each of the old RET instructions in the original
Chris Lattner62b7fd12002-04-07 20:49:59 +000066 // function.
Chris Lattner2f7c9632001-06-06 20:29:01 +000067 //
68 PHINode *PHI = 0;
Chris Lattnerb1120052002-11-19 21:54:07 +000069 if (!CI->use_empty()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000070 // The PHI node should go at the front of the new basic block to merge all
71 // possible incoming values.
72 //
Chris Lattnerf80f7b02002-09-10 22:38:49 +000073 PHI = new PHINode(CalledFunc->getReturnType(), CI->getName(),
74 NewBB->begin());
Chris Lattner2f7c9632001-06-06 20:29:01 +000075
76 // Anything that used the result of the function call should now use the PHI
77 // node as their operand.
78 //
79 CI->replaceAllUsesWith(PHI);
80 }
81
Chris Lattnerb1120052002-11-19 21:54:07 +000082 // Get a pointer to the last basic block in the function, which will have the
83 // new function inlined after it.
Chris Lattner2f7c9632001-06-06 20:29:01 +000084 //
Chris Lattnerb1120052002-11-19 21:54:07 +000085 Function::iterator LastBlock = &OrigBB->getParent()->back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000086
Chris Lattnerb1120052002-11-19 21:54:07 +000087 // Calculate the vector of arguments to pass into the function cloner...
Chris Lattnerc3626182002-11-19 22:54:01 +000088 std::map<const Value*, Value*> ValueMap;
89 assert((unsigned)std::distance(CalledFunc->abegin(), CalledFunc->aend()) ==
90 CI->getNumOperands()-1 && "No varargs calls can be inlined yet!");
91
92 unsigned i = 1;
93 for (Function::const_aiterator I = CalledFunc->abegin(), E=CalledFunc->aend();
94 I != E; ++I, ++i)
95 ValueMap[I] = CI->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +000096
Chris Lattnerb1120052002-11-19 21:54:07 +000097 // Since we are now done with the CallInst, we can delete it.
98 delete CI;
Chris Lattner2f7c9632001-06-06 20:29:01 +000099
Chris Lattnerb1120052002-11-19 21:54:07 +0000100 // Make a vector to capture the return instructions in the cloned function...
101 std::vector<ReturnInst*> Returns;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000102
Chris Lattner661841c2003-01-13 00:27:23 +0000103 // Populate the value map with all of the globals in the program.
104 Module &M = *OrigBB->getParent()->getParent();
105 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
106 ValueMap[I] = I;
107 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
108 ValueMap[I] = I;
109
Chris Lattnerb1120052002-11-19 21:54:07 +0000110 // Do all of the hard part of cloning the callee into the caller...
Chris Lattnerc3626182002-11-19 22:54:01 +0000111 CloneFunctionInto(OrigBB->getParent(), CalledFunc, ValueMap, Returns, ".i");
Chris Lattnerb1120052002-11-19 21:54:07 +0000112
113 // Loop over all of the return instructions, turning them into unconditional
114 // branches to the merge point now...
115 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
116 ReturnInst *RI = Returns[i];
117 BasicBlock *BB = RI->getParent();
118
119 // Add a branch to the merge point where the PHI node would live...
120 new BranchInst(NewBB, RI);
121
122 if (PHI) { // The PHI node should include this value!
123 assert(RI->getReturnValue() && "Ret should have value!");
124 assert(RI->getReturnValue()->getType() == PHI->getType() &&
125 "Ret value not consistent in function!");
126 PHI->addIncoming(RI->getReturnValue(), BB);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127 }
128
Chris Lattnerb1120052002-11-19 21:54:07 +0000129 // Delete the return instruction now
130 BB->getInstList().erase(RI);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131 }
132
Chris Lattnerb1120052002-11-19 21:54:07 +0000133 // Check to see if the PHI node only has one argument. This is a common
134 // case resulting from there only being a single return instruction in the
135 // function call. Because this is so common, eliminate the PHI node.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136 //
Chris Lattnerb1120052002-11-19 21:54:07 +0000137 if (PHI && PHI->getNumIncomingValues() == 1) {
138 PHI->replaceAllUsesWith(PHI->getIncomingValue(0));
139 PHI->getParent()->getInstList().erase(PHI);
Chris Lattnerfacc7512002-09-22 18:41:25 +0000140 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000141
142 // Change the branch that used to go to NewBB to branch to the first basic
Chris Lattner62b7fd12002-04-07 20:49:59 +0000143 // block of the inlined function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000144 //
145 TerminatorInst *Br = OrigBB->getTerminator();
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000146 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147 "splitBasicBlock broken!");
Chris Lattnerb1120052002-11-19 21:54:07 +0000148 Br->setOperand(0, ++LastBlock);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149 return true;
150}
151
Chris Lattner62b7fd12002-04-07 20:49:59 +0000152static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000153 assert(CI->getParent() && CI->getParent()->getParent() &&
Chris Lattnerc8e66542002-04-27 06:56:12 +0000154 "Call not embedded into a function!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000155
156 // Don't inline a recursive call.
Chris Lattner62b7fd12002-04-07 20:49:59 +0000157 if (CI->getParent()->getParent() == F) return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158
159 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner62b7fd12002-04-07 20:49:59 +0000160 if (F->size() > 3) return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161
162 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner4cee8d82001-06-27 23:41:11 +0000163 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000164
165 // Go ahead and try just about anything else.
166 return true;
167}
168
169
Chris Lattner62b7fd12002-04-07 20:49:59 +0000170static inline bool DoFunctionInlining(BasicBlock *BB) {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000171 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000172 if (CallInst *CI = dyn_cast<CallInst>(&*I)) {
Chris Lattner62b7fd12002-04-07 20:49:59 +0000173 // Check to see if we should inline this function
174 Function *F = CI->getCalledFunction();
Chris Lattner113f4f42002-06-25 16:13:24 +0000175 if (F && ShouldInlineFunction(CI, F)) {
176 return InlineFunction(CI);
177 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000178 }
179 }
180 return false;
181}
182
Chris Lattner62b7fd12002-04-07 20:49:59 +0000183// doFunctionInlining - Use a heuristic based approach to inline functions that
Chris Lattner04805fa2002-02-26 21:46:54 +0000184// seem to look good.
185//
Chris Lattner113f4f42002-06-25 16:13:24 +0000186static bool doFunctionInlining(Function &F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187 bool Changed = false;
188
189 // Loop through now and inline instructions a basic block at a time...
Chris Lattner113f4f42002-06-25 16:13:24 +0000190 for (Function::iterator I = F.begin(); I != F.end(); )
191 if (DoFunctionInlining(I)) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000192 ++NumInlined;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193 Changed = true;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194 } else {
195 ++I;
196 }
197
198 return Changed;
199}
Chris Lattner04805fa2002-02-26 21:46:54 +0000200
201namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000202 struct FunctionInlining : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +0000203 virtual bool runOnFunction(Function &F) {
Chris Lattner62b7fd12002-04-07 20:49:59 +0000204 return doFunctionInlining(F);
Chris Lattner04805fa2002-02-26 21:46:54 +0000205 }
206 };
Chris Lattnerc8b70922002-07-26 21:12:46 +0000207 RegisterOpt<FunctionInlining> X("inline", "Function Integration/Inlining");
Chris Lattner04805fa2002-02-26 21:46:54 +0000208}
209
Chris Lattnerc8e66542002-04-27 06:56:12 +0000210Pass *createFunctionInliningPass() { return new FunctionInlining(); }