blob: 6dbab594e8cad48954196f58d4ab32d5104cc337 [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//
11// Notice that:
Chris Lattneree965ab2002-01-21 23:17:48 +000012// * This pass opens up a lot of opportunities for constant propogation. It
13// is a good idea to to run a constant propogation pass, then a DCE pass
Chris Lattner2f7c9632001-06-06 20:29:01 +000014// sometime after running this pass.
15//
Chris Lattner1c8d3f82002-04-18 18:52:03 +000016// FIXME: This pass should transform alloca instructions in the called function
17// into malloc/free pairs!
18//
Chris Lattner2f7c9632001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner16667512002-11-19 20:59:41 +000021#include "llvm/Transforms/IPO.h"
22#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000023#include "llvm/Module.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000024#include "llvm/Pass.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000025#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000027#include "llvm/iOther.h"
Chris Lattnerf8e4dc32002-04-08 22:03:00 +000028#include "llvm/Type.h"
Chris Lattner3cf37822002-10-01 22:38:37 +000029#include "Support/Statistic.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000030#include <algorithm>
Chris Lattner113f4f42002-06-25 16:13:24 +000031
Chris Lattner3cf37822002-10-01 22:38:37 +000032static Statistic<> NumInlined("inline", "Number of functions inlined");
Chris Lattner7f74a562002-01-20 22:54:45 +000033using std::cerr;
Chris Lattner2f7c9632001-06-06 20:29:01 +000034
Chris Lattnerc8e66542002-04-27 06:56:12 +000035// InlineFunction - This function forcibly inlines the called function into the
Chris Lattner2f7c9632001-06-06 20:29:01 +000036// basic block of the caller. This returns false if it is not possible to
37// inline this call. The program is still in a well defined state if this
38// occurs though.
39//
40// Note that this only does one level of inlining. For example, if the
41// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
42// exists in the instruction stream. Similiarly this will inline a recursive
Chris Lattner62b7fd12002-04-07 20:49:59 +000043// function by one level.
Chris Lattner2f7c9632001-06-06 20:29:01 +000044//
Chris Lattner113f4f42002-06-25 16:13:24 +000045bool InlineFunction(CallInst *CI) {
46 assert(isa<CallInst>(CI) && "InlineFunction only works on CallInst nodes");
47 assert(CI->getParent() && "Instruction not embedded in basic block!");
48 assert(CI->getParent()->getParent() && "Instruction not in function!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000049
Chris Lattner113f4f42002-06-25 16:13:24 +000050 const Function *CalledFunc = CI->getCalledFunction();
51 if (CalledFunc == 0 || // Can't inline external function or indirect call!
52 CalledFunc->isExternal()) return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +000053
Chris Lattner113f4f42002-06-25 16:13:24 +000054 //cerr << "Inlining " << CalledFunc->getName() << " into "
Chris Lattner7f74a562002-01-20 22:54:45 +000055 // << CurrentMeth->getName() << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000056
57 BasicBlock *OrigBB = CI->getParent();
58
59 // Call splitBasicBlock - The original basic block now ends at the instruction
60 // immediately before the call. The original basic block now ends with an
61 // unconditional branch to NewBB, and NewBB starts with the call instruction.
62 //
Chris Lattner113f4f42002-06-25 16:13:24 +000063 BasicBlock *NewBB = OrigBB->splitBasicBlock(CI);
Chris Lattnerf20671a2002-02-25 00:31:02 +000064 NewBB->setName("InlinedFunctionReturnNode");
Chris Lattner2f7c9632001-06-06 20:29:01 +000065
66 // Remove (unlink) the CallInst from the start of the new basic block.
67 NewBB->getInstList().remove(CI);
68
69 // If we have a return value generated by this call, convert it into a PHI
70 // node that gets values from each of the old RET instructions in the original
Chris Lattner62b7fd12002-04-07 20:49:59 +000071 // function.
Chris Lattner2f7c9632001-06-06 20:29:01 +000072 //
73 PHINode *PHI = 0;
Chris Lattnerb1120052002-11-19 21:54:07 +000074 if (!CI->use_empty()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000075 // The PHI node should go at the front of the new basic block to merge all
76 // possible incoming values.
77 //
Chris Lattnerf80f7b02002-09-10 22:38:49 +000078 PHI = new PHINode(CalledFunc->getReturnType(), CI->getName(),
79 NewBB->begin());
Chris Lattner2f7c9632001-06-06 20:29:01 +000080
81 // Anything that used the result of the function call should now use the PHI
82 // node as their operand.
83 //
84 CI->replaceAllUsesWith(PHI);
85 }
86
Chris Lattnerb1120052002-11-19 21:54:07 +000087 // Get a pointer to the last basic block in the function, which will have the
88 // new function inlined after it.
Chris Lattner2f7c9632001-06-06 20:29:01 +000089 //
Chris Lattnerb1120052002-11-19 21:54:07 +000090 Function::iterator LastBlock = &OrigBB->getParent()->back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000091
Chris Lattnerb1120052002-11-19 21:54:07 +000092 // Calculate the vector of arguments to pass into the function cloner...
Chris Lattnerc3626182002-11-19 22:54:01 +000093 std::map<const Value*, Value*> ValueMap;
94 assert((unsigned)std::distance(CalledFunc->abegin(), CalledFunc->aend()) ==
95 CI->getNumOperands()-1 && "No varargs calls can be inlined yet!");
96
97 unsigned i = 1;
98 for (Function::const_aiterator I = CalledFunc->abegin(), E=CalledFunc->aend();
99 I != E; ++I, ++i)
100 ValueMap[I] = CI->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000101
Chris Lattnerb1120052002-11-19 21:54:07 +0000102 // Since we are now done with the CallInst, we can delete it.
103 delete CI;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104
Chris Lattnerb1120052002-11-19 21:54:07 +0000105 // Make a vector to capture the return instructions in the cloned function...
106 std::vector<ReturnInst*> Returns;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000107
Chris Lattnerb1120052002-11-19 21:54:07 +0000108 // Do all of the hard part of cloning the callee into the caller...
Chris Lattnerc3626182002-11-19 22:54:01 +0000109 CloneFunctionInto(OrigBB->getParent(), CalledFunc, ValueMap, Returns, ".i");
Chris Lattnerb1120052002-11-19 21:54:07 +0000110
111 // Loop over all of the return instructions, turning them into unconditional
112 // branches to the merge point now...
113 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
114 ReturnInst *RI = Returns[i];
115 BasicBlock *BB = RI->getParent();
116
117 // Add a branch to the merge point where the PHI node would live...
118 new BranchInst(NewBB, RI);
119
120 if (PHI) { // The PHI node should include this value!
121 assert(RI->getReturnValue() && "Ret should have value!");
122 assert(RI->getReturnValue()->getType() == PHI->getType() &&
123 "Ret value not consistent in function!");
124 PHI->addIncoming(RI->getReturnValue(), BB);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000125 }
126
Chris Lattnerb1120052002-11-19 21:54:07 +0000127 // Delete the return instruction now
128 BB->getInstList().erase(RI);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129 }
130
Chris Lattnerb1120052002-11-19 21:54:07 +0000131 // Check to see if the PHI node only has one argument. This is a common
132 // case resulting from there only being a single return instruction in the
133 // function call. Because this is so common, eliminate the PHI node.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000134 //
Chris Lattnerb1120052002-11-19 21:54:07 +0000135 if (PHI && PHI->getNumIncomingValues() == 1) {
136 PHI->replaceAllUsesWith(PHI->getIncomingValue(0));
137 PHI->getParent()->getInstList().erase(PHI);
Chris Lattnerfacc7512002-09-22 18:41:25 +0000138 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000139
140 // Change the branch that used to go to NewBB to branch to the first basic
Chris Lattner62b7fd12002-04-07 20:49:59 +0000141 // block of the inlined function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000142 //
143 TerminatorInst *Br = OrigBB->getTerminator();
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000144 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145 "splitBasicBlock broken!");
Chris Lattnerb1120052002-11-19 21:54:07 +0000146 Br->setOperand(0, ++LastBlock);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147 return true;
148}
149
Chris Lattner62b7fd12002-04-07 20:49:59 +0000150static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151 assert(CI->getParent() && CI->getParent()->getParent() &&
Chris Lattnerc8e66542002-04-27 06:56:12 +0000152 "Call not embedded into a function!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000153
154 // Don't inline a recursive call.
Chris Lattner62b7fd12002-04-07 20:49:59 +0000155 if (CI->getParent()->getParent() == F) return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156
157 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner62b7fd12002-04-07 20:49:59 +0000158 if (F->size() > 3) return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000159
160 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner4cee8d82001-06-27 23:41:11 +0000161 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162
163 // Go ahead and try just about anything else.
164 return true;
165}
166
167
Chris Lattner62b7fd12002-04-07 20:49:59 +0000168static inline bool DoFunctionInlining(BasicBlock *BB) {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000169 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000170 if (CallInst *CI = dyn_cast<CallInst>(&*I)) {
Chris Lattner62b7fd12002-04-07 20:49:59 +0000171 // Check to see if we should inline this function
172 Function *F = CI->getCalledFunction();
Chris Lattner113f4f42002-06-25 16:13:24 +0000173 if (F && ShouldInlineFunction(CI, F)) {
174 return InlineFunction(CI);
175 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000176 }
177 }
178 return false;
179}
180
Chris Lattner62b7fd12002-04-07 20:49:59 +0000181// doFunctionInlining - Use a heuristic based approach to inline functions that
Chris Lattner04805fa2002-02-26 21:46:54 +0000182// seem to look good.
183//
Chris Lattner113f4f42002-06-25 16:13:24 +0000184static bool doFunctionInlining(Function &F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185 bool Changed = false;
186
187 // Loop through now and inline instructions a basic block at a time...
Chris Lattner113f4f42002-06-25 16:13:24 +0000188 for (Function::iterator I = F.begin(); I != F.end(); )
189 if (DoFunctionInlining(I)) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000190 ++NumInlined;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191 Changed = true;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000192 } else {
193 ++I;
194 }
195
196 return Changed;
197}
Chris Lattner04805fa2002-02-26 21:46:54 +0000198
199namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000200 struct FunctionInlining : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +0000201 virtual bool runOnFunction(Function &F) {
Chris Lattner62b7fd12002-04-07 20:49:59 +0000202 return doFunctionInlining(F);
Chris Lattner04805fa2002-02-26 21:46:54 +0000203 }
204 };
Chris Lattnerc8b70922002-07-26 21:12:46 +0000205 RegisterOpt<FunctionInlining> X("inline", "Function Integration/Inlining");
Chris Lattner04805fa2002-02-26 21:46:54 +0000206}
207
Chris Lattnerc8e66542002-04-27 06:56:12 +0000208Pass *createFunctionInliningPass() { return new FunctionInlining(); }