blob: fcb1e4fa40e5b087e6e2c14d38ba8c78d9195dba [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===- FunctionInlining.cpp - Code to perform function inlining -----------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00003// This file implements inlining of functions.
Chris Lattner00950542001-06-06 20:29:01 +00004//
5// Specifically, this:
Chris Lattner2fbfdcf2002-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 Lattner00950542001-06-06 20:29:01 +000010//
11// Notice that:
Chris Lattner59b6b8e2002-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 Lattner00950542001-06-06 20:29:01 +000014// sometime after running this pass.
15//
Chris Lattner01545052002-04-18 18:52:03 +000016// FIXME: This pass should transform alloca instructions in the called function
17// into malloc/free pairs!
18//
Chris Lattner00950542001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner59b6b8e2002-01-21 23:17:48 +000021#include "llvm/Transforms/MethodInlining.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/Module.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023#include "llvm/Function.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000024#include "llvm/Pass.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000027#include "llvm/iOther.h"
Chris Lattner237e6d12002-04-08 22:03:00 +000028#include "llvm/Type.h"
Chris Lattner73e21422002-04-09 19:48:49 +000029#include "llvm/Argument.h"
Chris Lattner00950542001-06-06 20:29:01 +000030#include <algorithm>
31#include <map>
Chris Lattner697954c2002-01-20 22:54:45 +000032#include <iostream>
33using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner00950542001-06-06 20:29:01 +000035// RemapInstruction - Convert the instruction operands from referencing the
36// current values into those specified by ValueMap.
37//
38static inline void RemapInstruction(Instruction *I,
Chris Lattner697954c2002-01-20 22:54:45 +000039 std::map<const Value *, Value*> &ValueMap) {
Chris Lattner00950542001-06-06 20:29:01 +000040
Chris Lattnerc8b25d42001-07-07 08:36:50 +000041 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
42 const Value *Op = I->getOperand(op);
Chris Lattner00950542001-06-06 20:29:01 +000043 Value *V = ValueMap[Op];
Chris Lattnere9bb2df2001-12-03 22:26:30 +000044 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
Chris Lattner4f685282001-10-31 02:27:26 +000045 continue; // Globals and constants don't get relocated
Chris Lattner00950542001-06-06 20:29:01 +000046
47 if (!V) {
Chris Lattner697954c2002-01-20 22:54:45 +000048 cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
49 cerr << "\nInst = " << I;
Chris Lattner00950542001-06-06 20:29:01 +000050 }
51 assert(V && "Referenced value not in value map!");
52 I->setOperand(op, V);
53 }
54}
55
Chris Lattnerf57b8452002-04-27 06:56:12 +000056// InlineFunction - This function forcibly inlines the called function into the
Chris Lattner00950542001-06-06 20:29:01 +000057// basic block of the caller. This returns false if it is not possible to
58// inline this call. The program is still in a well defined state if this
59// occurs though.
60//
61// Note that this only does one level of inlining. For example, if the
62// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
63// exists in the instruction stream. Similiarly this will inline a recursive
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000064// function by one level.
Chris Lattner00950542001-06-06 20:29:01 +000065//
Chris Lattnerf57b8452002-04-27 06:56:12 +000066bool InlineFunction(BasicBlock::iterator CIIt) {
67 assert(isa<CallInst>(*CIIt) && "InlineFunction only works on CallInst nodes");
Chris Lattner00950542001-06-06 20:29:01 +000068 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000069 assert((*CIIt)->getParent()->getParent() && "Instruction not in function!");
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattnerb00c5822001-10-02 03:41:24 +000071 CallInst *CI = cast<CallInst>(*CIIt);
Chris Lattnerdc89f872002-03-29 17:08:29 +000072 const Function *CalledMeth = CI->getCalledFunction();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000073 if (CalledMeth == 0 || // Can't inline external function or indirect call!
Chris Lattner5a0d4172001-10-13 06:52:31 +000074 CalledMeth->isExternal()) return false;
Chris Lattner00950542001-06-06 20:29:01 +000075
76 //cerr << "Inlining " << CalledMeth->getName() << " into "
Chris Lattner697954c2002-01-20 22:54:45 +000077 // << CurrentMeth->getName() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000078
79 BasicBlock *OrigBB = CI->getParent();
80
81 // Call splitBasicBlock - The original basic block now ends at the instruction
82 // immediately before the call. The original basic block now ends with an
83 // unconditional branch to NewBB, and NewBB starts with the call instruction.
84 //
85 BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
Chris Lattner41b66b12002-02-25 00:31:02 +000086 NewBB->setName("InlinedFunctionReturnNode");
Chris Lattner00950542001-06-06 20:29:01 +000087
88 // Remove (unlink) the CallInst from the start of the new basic block.
89 NewBB->getInstList().remove(CI);
90
91 // If we have a return value generated by this call, convert it into a PHI
92 // node that gets values from each of the old RET instructions in the original
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000093 // function.
Chris Lattner00950542001-06-06 20:29:01 +000094 //
95 PHINode *PHI = 0;
96 if (CalledMeth->getReturnType() != Type::VoidTy) {
97 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
98
99 // The PHI node should go at the front of the new basic block to merge all
100 // possible incoming values.
101 //
102 NewBB->getInstList().push_front(PHI);
103
104 // Anything that used the result of the function call should now use the PHI
105 // node as their operand.
106 //
107 CI->replaceAllUsesWith(PHI);
108 }
109
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000110 // Keep a mapping between the original function's values and the new
111 // duplicated code's values. This includes all of: Function arguments,
112 // instruction values, constant pool entries, and basic blocks.
Chris Lattner00950542001-06-06 20:29:01 +0000113 //
Chris Lattner697954c2002-01-20 22:54:45 +0000114 std::map<const Value *, Value*> ValueMap;
Chris Lattner00950542001-06-06 20:29:01 +0000115
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000116 // Add the function arguments to the mapping: (start counting at 1 to skip the
117 // function reference itself)
Chris Lattner00950542001-06-06 20:29:01 +0000118 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000119 Function::ArgumentListType::const_iterator PTI =
Chris Lattner00950542001-06-06 20:29:01 +0000120 CalledMeth->getArgumentList().begin();
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000121 for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
122 ValueMap[*PTI] = CI->getOperand(a);
Chris Lattner00950542001-06-06 20:29:01 +0000123
Chris Lattner00950542001-06-06 20:29:01 +0000124 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
125
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000126 // Loop over all of the basic blocks in the function, inlining them as
127 // appropriate. Keep track of the first basic block of the function...
Chris Lattner00950542001-06-06 20:29:01 +0000128 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000129 for (Function::const_iterator BI = CalledMeth->begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000130 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000131 const BasicBlock *BB = *BI;
132 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
133
134 // Create a new basic block to copy instructions into!
135 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
Chris Lattner41b66b12002-02-25 00:31:02 +0000136 if (BB->hasName()) IBB->setName(BB->getName()+".i"); // .i = inlined once
Chris Lattner00950542001-06-06 20:29:01 +0000137
Chris Lattner5fdc4c92001-10-14 23:29:30 +0000138 ValueMap[BB] = IBB; // Add basic block mapping.
Chris Lattner00950542001-06-06 20:29:01 +0000139
140 // Make sure to capture the mapping that a return will use...
141 // TODO: This assumes that the RET is returning a value computed in the same
142 // basic block as the return was issued from!
143 //
144 const TerminatorInst *TI = BB->getTerminator();
145
146 // Loop over all instructions copying them over...
147 Instruction *NewInst;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000148 for (BasicBlock::const_iterator II = BB->begin();
149 II != (BB->end()-1); ++II) {
Chris Lattner00950542001-06-06 20:29:01 +0000150 IBB->getInstList().push_back((NewInst = (*II)->clone()));
151 ValueMap[*II] = NewInst; // Add instruction map to value.
Chris Lattner41b66b12002-02-25 00:31:02 +0000152 if ((*II)->hasName())
153 NewInst->setName((*II)->getName()+".i"); // .i = inlined once
Chris Lattner00950542001-06-06 20:29:01 +0000154 }
155
156 // Copy over the terminator now...
Chris Lattnera41f50d2001-07-07 19:24:15 +0000157 switch (TI->getOpcode()) {
Chris Lattner00950542001-06-06 20:29:01 +0000158 case Instruction::Ret: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000159 const ReturnInst *RI = cast<const ReturnInst>(TI);
Chris Lattner00950542001-06-06 20:29:01 +0000160
161 if (PHI) { // The PHI node should include this value!
162 assert(RI->getReturnValue() && "Ret should have value!");
163 assert(RI->getReturnValue()->getType() == PHI->getType() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000164 "Ret value not consistent in function!");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000165 PHI->addIncoming((Value*)RI->getReturnValue(), cast<BasicBlock>(BB));
Chris Lattner00950542001-06-06 20:29:01 +0000166 }
167
168 // Add a branch to the code that was after the original Call.
169 IBB->getInstList().push_back(new BranchInst(NewBB));
170 break;
171 }
172 case Instruction::Br:
173 IBB->getInstList().push_back(TI->clone());
174 break;
175
176 default:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000177 cerr << "FunctionInlining: Don't know how to handle terminator: " << TI;
Chris Lattner00950542001-06-06 20:29:01 +0000178 abort();
179 }
180 }
181
182
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000183 // Loop over all of the instructions in the function, fixing up operand
Chris Lattner00950542001-06-06 20:29:01 +0000184 // references as we go. This uses ValueMap to do all the hard work.
185 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000186 for (Function::const_iterator BI = CalledMeth->begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000187 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000188 const BasicBlock *BB = *BI;
189 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
190
191 // Loop over all instructions, fixing each one as we find it...
192 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000193 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
Chris Lattner00950542001-06-06 20:29:01 +0000194 RemapInstruction(*II, ValueMap);
195 }
196
197 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
198
199 // Change the branch that used to go to NewBB to branch to the first basic
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000200 // block of the inlined function.
Chris Lattner00950542001-06-06 20:29:01 +0000201 //
202 TerminatorInst *Br = OrigBB->getTerminator();
Chris Lattnera41f50d2001-07-07 19:24:15 +0000203 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner00950542001-06-06 20:29:01 +0000204 "splitBasicBlock broken!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000205 Br->setOperand(0, ValueMap[CalledMeth->front()]);
Chris Lattner00950542001-06-06 20:29:01 +0000206
207 // Since we are now done with the CallInst, we can finally delete it.
208 delete CI;
209 return true;
210}
211
Chris Lattnerf57b8452002-04-27 06:56:12 +0000212bool InlineFunction(CallInst *CI) {
Chris Lattner00950542001-06-06 20:29:01 +0000213 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
214 BasicBlock *PBB = CI->getParent();
215
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000216 BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
217
218 assert(CallIt != PBB->end() &&
Chris Lattner00950542001-06-06 20:29:01 +0000219 "CallInst has parent that doesn't contain CallInst?!?");
Chris Lattnerf57b8452002-04-27 06:56:12 +0000220 return InlineFunction(CallIt);
Chris Lattner00950542001-06-06 20:29:01 +0000221}
222
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000223static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000224 assert(CI->getParent() && CI->getParent()->getParent() &&
Chris Lattnerf57b8452002-04-27 06:56:12 +0000225 "Call not embedded into a function!");
Chris Lattner00950542001-06-06 20:29:01 +0000226
227 // Don't inline a recursive call.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000228 if (CI->getParent()->getParent() == F) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000229
230 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000231 if (F->size() > 3) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000232
233 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000234 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000235
236 // Go ahead and try just about anything else.
237 return true;
238}
239
240
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000241static inline bool DoFunctionInlining(BasicBlock *BB) {
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000242 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000243 if (CallInst *CI = dyn_cast<CallInst>(*I)) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000244 // Check to see if we should inline this function
245 Function *F = CI->getCalledFunction();
246 if (F && ShouldInlineFunction(CI, F))
Chris Lattnerf57b8452002-04-27 06:56:12 +0000247 return InlineFunction(I);
Chris Lattner00950542001-06-06 20:29:01 +0000248 }
249 }
250 return false;
251}
252
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000253// doFunctionInlining - Use a heuristic based approach to inline functions that
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000254// seem to look good.
255//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000256static bool doFunctionInlining(Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000257 bool Changed = false;
258
259 // Loop through now and inline instructions a basic block at a time...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000260 for (Function::iterator I = F->begin(); I != F->end(); )
261 if (DoFunctionInlining(*I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000262 Changed = true;
263 // Iterator is now invalidated by new basic blocks inserted
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000264 I = F->begin();
Chris Lattner00950542001-06-06 20:29:01 +0000265 } else {
266 ++I;
267 }
268
269 return Changed;
270}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000271
272namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000273 struct FunctionInlining : public FunctionPass {
274 virtual bool runOnFunction(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000275 return doFunctionInlining(F);
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000276 }
277 };
278}
279
Chris Lattnerf57b8452002-04-27 06:56:12 +0000280Pass *createFunctionInliningPass() { return new FunctionInlining(); }