blob: 9e84138b08e34d681eb9c316b2f8c42a66e65609 [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 Lattner483e14e2002-04-27 07:27:19 +000021#include "llvm/Transforms/FunctionInlining.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>
Chris Lattner697954c2002-01-20 22:54:45 +000031#include <iostream>
32using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000033
Chris Lattner00950542001-06-06 20:29:01 +000034// RemapInstruction - Convert the instruction operands from referencing the
35// current values into those specified by ValueMap.
36//
37static inline void RemapInstruction(Instruction *I,
Chris Lattner697954c2002-01-20 22:54:45 +000038 std::map<const Value *, Value*> &ValueMap) {
Chris Lattner00950542001-06-06 20:29:01 +000039
Chris Lattnerc8b25d42001-07-07 08:36:50 +000040 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
41 const Value *Op = I->getOperand(op);
Chris Lattner00950542001-06-06 20:29:01 +000042 Value *V = ValueMap[Op];
Chris Lattnere9bb2df2001-12-03 22:26:30 +000043 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
Chris Lattner4f685282001-10-31 02:27:26 +000044 continue; // Globals and constants don't get relocated
Chris Lattner00950542001-06-06 20:29:01 +000045
46 if (!V) {
Chris Lattner697954c2002-01-20 22:54:45 +000047 cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
48 cerr << "\nInst = " << I;
Chris Lattner00950542001-06-06 20:29:01 +000049 }
50 assert(V && "Referenced value not in value map!");
51 I->setOperand(op, V);
52 }
53}
54
Chris Lattnerf57b8452002-04-27 06:56:12 +000055// InlineFunction - This function forcibly inlines the called function into the
Chris Lattner00950542001-06-06 20:29:01 +000056// basic block of the caller. This returns false if it is not possible to
57// inline this call. The program is still in a well defined state if this
58// occurs though.
59//
60// Note that this only does one level of inlining. For example, if the
61// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
62// exists in the instruction stream. Similiarly this will inline a recursive
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000063// function by one level.
Chris Lattner00950542001-06-06 20:29:01 +000064//
Chris Lattnerf57b8452002-04-27 06:56:12 +000065bool InlineFunction(BasicBlock::iterator CIIt) {
66 assert(isa<CallInst>(*CIIt) && "InlineFunction only works on CallInst nodes");
Chris Lattner00950542001-06-06 20:29:01 +000067 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000068 assert((*CIIt)->getParent()->getParent() && "Instruction not in function!");
Chris Lattner00950542001-06-06 20:29:01 +000069
Chris Lattnerb00c5822001-10-02 03:41:24 +000070 CallInst *CI = cast<CallInst>(*CIIt);
Chris Lattnerdc89f872002-03-29 17:08:29 +000071 const Function *CalledMeth = CI->getCalledFunction();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000072 if (CalledMeth == 0 || // Can't inline external function or indirect call!
Chris Lattner5a0d4172001-10-13 06:52:31 +000073 CalledMeth->isExternal()) return false;
Chris Lattner00950542001-06-06 20:29:01 +000074
75 //cerr << "Inlining " << CalledMeth->getName() << " into "
Chris Lattner697954c2002-01-20 22:54:45 +000076 // << CurrentMeth->getName() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000077
78 BasicBlock *OrigBB = CI->getParent();
79
80 // Call splitBasicBlock - The original basic block now ends at the instruction
81 // immediately before the call. The original basic block now ends with an
82 // unconditional branch to NewBB, and NewBB starts with the call instruction.
83 //
84 BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
Chris Lattner41b66b12002-02-25 00:31:02 +000085 NewBB->setName("InlinedFunctionReturnNode");
Chris Lattner00950542001-06-06 20:29:01 +000086
87 // Remove (unlink) the CallInst from the start of the new basic block.
88 NewBB->getInstList().remove(CI);
89
90 // If we have a return value generated by this call, convert it into a PHI
91 // node that gets values from each of the old RET instructions in the original
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000092 // function.
Chris Lattner00950542001-06-06 20:29:01 +000093 //
94 PHINode *PHI = 0;
95 if (CalledMeth->getReturnType() != Type::VoidTy) {
96 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
97
98 // The PHI node should go at the front of the new basic block to merge all
99 // possible incoming values.
100 //
101 NewBB->getInstList().push_front(PHI);
102
103 // Anything that used the result of the function call should now use the PHI
104 // node as their operand.
105 //
106 CI->replaceAllUsesWith(PHI);
107 }
108
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000109 // Keep a mapping between the original function's values and the new
110 // duplicated code's values. This includes all of: Function arguments,
111 // instruction values, constant pool entries, and basic blocks.
Chris Lattner00950542001-06-06 20:29:01 +0000112 //
Chris Lattner697954c2002-01-20 22:54:45 +0000113 std::map<const Value *, Value*> ValueMap;
Chris Lattner00950542001-06-06 20:29:01 +0000114
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000115 // Add the function arguments to the mapping: (start counting at 1 to skip the
116 // function reference itself)
Chris Lattner00950542001-06-06 20:29:01 +0000117 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000118 Function::ArgumentListType::const_iterator PTI =
Chris Lattner00950542001-06-06 20:29:01 +0000119 CalledMeth->getArgumentList().begin();
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000120 for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
121 ValueMap[*PTI] = CI->getOperand(a);
Chris Lattner00950542001-06-06 20:29:01 +0000122
Chris Lattner00950542001-06-06 20:29:01 +0000123 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
124
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000125 // Loop over all of the basic blocks in the function, inlining them as
126 // appropriate. Keep track of the first basic block of the function...
Chris Lattner00950542001-06-06 20:29:01 +0000127 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000128 for (Function::const_iterator BI = CalledMeth->begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000129 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000130 const BasicBlock *BB = *BI;
131 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
132
133 // Create a new basic block to copy instructions into!
134 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
Chris Lattner41b66b12002-02-25 00:31:02 +0000135 if (BB->hasName()) IBB->setName(BB->getName()+".i"); // .i = inlined once
Chris Lattner00950542001-06-06 20:29:01 +0000136
Chris Lattner5fdc4c92001-10-14 23:29:30 +0000137 ValueMap[BB] = IBB; // Add basic block mapping.
Chris Lattner00950542001-06-06 20:29:01 +0000138
139 // Make sure to capture the mapping that a return will use...
140 // TODO: This assumes that the RET is returning a value computed in the same
141 // basic block as the return was issued from!
142 //
143 const TerminatorInst *TI = BB->getTerminator();
144
145 // Loop over all instructions copying them over...
146 Instruction *NewInst;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000147 for (BasicBlock::const_iterator II = BB->begin();
148 II != (BB->end()-1); ++II) {
Chris Lattner00950542001-06-06 20:29:01 +0000149 IBB->getInstList().push_back((NewInst = (*II)->clone()));
150 ValueMap[*II] = NewInst; // Add instruction map to value.
Chris Lattner41b66b12002-02-25 00:31:02 +0000151 if ((*II)->hasName())
152 NewInst->setName((*II)->getName()+".i"); // .i = inlined once
Chris Lattner00950542001-06-06 20:29:01 +0000153 }
154
155 // Copy over the terminator now...
Chris Lattnera41f50d2001-07-07 19:24:15 +0000156 switch (TI->getOpcode()) {
Chris Lattner00950542001-06-06 20:29:01 +0000157 case Instruction::Ret: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000158 const ReturnInst *RI = cast<const ReturnInst>(TI);
Chris Lattner00950542001-06-06 20:29:01 +0000159
160 if (PHI) { // The PHI node should include this value!
161 assert(RI->getReturnValue() && "Ret should have value!");
162 assert(RI->getReturnValue()->getType() == PHI->getType() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000163 "Ret value not consistent in function!");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000164 PHI->addIncoming((Value*)RI->getReturnValue(), cast<BasicBlock>(BB));
Chris Lattner00950542001-06-06 20:29:01 +0000165 }
166
167 // Add a branch to the code that was after the original Call.
168 IBB->getInstList().push_back(new BranchInst(NewBB));
169 break;
170 }
171 case Instruction::Br:
172 IBB->getInstList().push_back(TI->clone());
173 break;
174
175 default:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000176 cerr << "FunctionInlining: Don't know how to handle terminator: " << TI;
Chris Lattner00950542001-06-06 20:29:01 +0000177 abort();
178 }
179 }
180
181
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000182 // Loop over all of the instructions in the function, fixing up operand
Chris Lattner00950542001-06-06 20:29:01 +0000183 // references as we go. This uses ValueMap to do all the hard work.
184 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000185 for (Function::const_iterator BI = CalledMeth->begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000186 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000187 const BasicBlock *BB = *BI;
188 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
189
190 // Loop over all instructions, fixing each one as we find it...
191 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000192 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
Chris Lattner00950542001-06-06 20:29:01 +0000193 RemapInstruction(*II, ValueMap);
194 }
195
196 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
197
198 // Change the branch that used to go to NewBB to branch to the first basic
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000199 // block of the inlined function.
Chris Lattner00950542001-06-06 20:29:01 +0000200 //
201 TerminatorInst *Br = OrigBB->getTerminator();
Chris Lattnera41f50d2001-07-07 19:24:15 +0000202 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner00950542001-06-06 20:29:01 +0000203 "splitBasicBlock broken!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000204 Br->setOperand(0, ValueMap[CalledMeth->front()]);
Chris Lattner00950542001-06-06 20:29:01 +0000205
206 // Since we are now done with the CallInst, we can finally delete it.
207 delete CI;
208 return true;
209}
210
Chris Lattnerf57b8452002-04-27 06:56:12 +0000211bool InlineFunction(CallInst *CI) {
Chris Lattner00950542001-06-06 20:29:01 +0000212 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
213 BasicBlock *PBB = CI->getParent();
214
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000215 BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
216
217 assert(CallIt != PBB->end() &&
Chris Lattner00950542001-06-06 20:29:01 +0000218 "CallInst has parent that doesn't contain CallInst?!?");
Chris Lattnerf57b8452002-04-27 06:56:12 +0000219 return InlineFunction(CallIt);
Chris Lattner00950542001-06-06 20:29:01 +0000220}
221
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000222static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000223 assert(CI->getParent() && CI->getParent()->getParent() &&
Chris Lattnerf57b8452002-04-27 06:56:12 +0000224 "Call not embedded into a function!");
Chris Lattner00950542001-06-06 20:29:01 +0000225
226 // Don't inline a recursive call.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000227 if (CI->getParent()->getParent() == F) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000228
229 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000230 if (F->size() > 3) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000231
232 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000233 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000234
235 // Go ahead and try just about anything else.
236 return true;
237}
238
239
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000240static inline bool DoFunctionInlining(BasicBlock *BB) {
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000241 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000242 if (CallInst *CI = dyn_cast<CallInst>(*I)) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000243 // Check to see if we should inline this function
244 Function *F = CI->getCalledFunction();
245 if (F && ShouldInlineFunction(CI, F))
Chris Lattnerf57b8452002-04-27 06:56:12 +0000246 return InlineFunction(I);
Chris Lattner00950542001-06-06 20:29:01 +0000247 }
248 }
249 return false;
250}
251
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000252// doFunctionInlining - Use a heuristic based approach to inline functions that
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000253// seem to look good.
254//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000255static bool doFunctionInlining(Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000256 bool Changed = false;
257
258 // Loop through now and inline instructions a basic block at a time...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000259 for (Function::iterator I = F->begin(); I != F->end(); )
260 if (DoFunctionInlining(*I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000261 Changed = true;
262 // Iterator is now invalidated by new basic blocks inserted
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000263 I = F->begin();
Chris Lattner00950542001-06-06 20:29:01 +0000264 } else {
265 ++I;
266 }
267
268 return Changed;
269}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000270
271namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000272 struct FunctionInlining : public FunctionPass {
Chris Lattner96c466b2002-04-29 14:57:45 +0000273 const char *getPassName() const { return "Function Inlining"; }
Chris Lattnerf57b8452002-04-27 06:56:12 +0000274 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(); }