2002-04-07 22:49:59 +02:00
|
|
|
//===- FunctionInlining.cpp - Code to perform function inlining -----------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2002-04-07 22:49:59 +02:00
|
|
|
// This file implements inlining of functions.
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
// Specifically, this:
|
2002-04-07 22:49:59 +02:00
|
|
|
// * Exports functionality to inline any function call
|
|
|
|
// * Inlines functions that consist of a single basic block
|
|
|
|
// * Is able to inline ANY function call
|
|
|
|
// . Has a smart heuristic for when to inline a function
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2002-04-18 20:52:03 +02:00
|
|
|
// FIXME: This pass should transform alloca instructions in the called function
|
2003-01-13 01:27:23 +01:00
|
|
|
// into malloc/free pairs! Or perhaps it should refuse to inline them!
|
2002-04-18 20:52:03 +02:00
|
|
|
//
|
2001-06-06 22:29:01 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-11-19 21:59:41 +01:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/Module.h"
|
2002-02-26 22:46:54 +01:00
|
|
|
#include "llvm/Pass.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/iTerminators.h"
|
2001-12-03 19:02:31 +01:00
|
|
|
#include "llvm/iPHINode.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/iOther.h"
|
2002-04-09 00:03:00 +02:00
|
|
|
#include "llvm/Type.h"
|
2002-10-02 00:38:37 +02:00
|
|
|
#include "Support/Statistic.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include <algorithm>
|
2002-06-25 18:13:24 +02:00
|
|
|
|
2002-10-02 00:38:37 +02:00
|
|
|
static Statistic<> NumInlined("inline", "Number of functions inlined");
|
2002-01-20 23:54:45 +01:00
|
|
|
using std::cerr;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
// InlineFunction - This function forcibly inlines the called function into the
|
2001-06-06 22:29:01 +02:00
|
|
|
// basic block of the caller. This returns false if it is not possible to
|
|
|
|
// inline this call. The program is still in a well defined state if this
|
|
|
|
// occurs though.
|
|
|
|
//
|
|
|
|
// Note that this only does one level of inlining. For example, if the
|
|
|
|
// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
|
|
|
|
// exists in the instruction stream. Similiarly this will inline a recursive
|
2002-04-07 22:49:59 +02:00
|
|
|
// function by one level.
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2002-06-25 18:13:24 +02:00
|
|
|
bool InlineFunction(CallInst *CI) {
|
|
|
|
assert(isa<CallInst>(CI) && "InlineFunction only works on CallInst nodes");
|
|
|
|
assert(CI->getParent() && "Instruction not embedded in basic block!");
|
|
|
|
assert(CI->getParent()->getParent() && "Instruction not in function!");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-06-25 18:13:24 +02:00
|
|
|
const Function *CalledFunc = CI->getCalledFunction();
|
|
|
|
if (CalledFunc == 0 || // Can't inline external function or indirect call!
|
|
|
|
CalledFunc->isExternal()) return false;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-06-25 18:13:24 +02:00
|
|
|
//cerr << "Inlining " << CalledFunc->getName() << " into "
|
2002-01-20 23:54:45 +01:00
|
|
|
// << CurrentMeth->getName() << "\n";
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
BasicBlock *OrigBB = CI->getParent();
|
|
|
|
|
|
|
|
// Call splitBasicBlock - The original basic block now ends at the instruction
|
|
|
|
// immediately before the call. The original basic block now ends with an
|
|
|
|
// unconditional branch to NewBB, and NewBB starts with the call instruction.
|
|
|
|
//
|
2002-06-25 18:13:24 +02:00
|
|
|
BasicBlock *NewBB = OrigBB->splitBasicBlock(CI);
|
2002-02-25 01:31:02 +01:00
|
|
|
NewBB->setName("InlinedFunctionReturnNode");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
// Remove (unlink) the CallInst from the start of the new basic block.
|
|
|
|
NewBB->getInstList().remove(CI);
|
|
|
|
|
|
|
|
// If we have a return value generated by this call, convert it into a PHI
|
|
|
|
// node that gets values from each of the old RET instructions in the original
|
2002-04-07 22:49:59 +02:00
|
|
|
// function.
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
PHINode *PHI = 0;
|
2002-11-19 22:54:07 +01:00
|
|
|
if (!CI->use_empty()) {
|
2001-06-06 22:29:01 +02:00
|
|
|
// The PHI node should go at the front of the new basic block to merge all
|
|
|
|
// possible incoming values.
|
|
|
|
//
|
2002-09-11 00:38:49 +02:00
|
|
|
PHI = new PHINode(CalledFunc->getReturnType(), CI->getName(),
|
|
|
|
NewBB->begin());
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
// Anything that used the result of the function call should now use the PHI
|
|
|
|
// node as their operand.
|
|
|
|
//
|
|
|
|
CI->replaceAllUsesWith(PHI);
|
|
|
|
}
|
|
|
|
|
2002-11-19 22:54:07 +01:00
|
|
|
// Get a pointer to the last basic block in the function, which will have the
|
|
|
|
// new function inlined after it.
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2002-11-19 22:54:07 +01:00
|
|
|
Function::iterator LastBlock = &OrigBB->getParent()->back();
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-11-19 22:54:07 +01:00
|
|
|
// Calculate the vector of arguments to pass into the function cloner...
|
2002-11-19 23:54:01 +01:00
|
|
|
std::map<const Value*, Value*> ValueMap;
|
|
|
|
assert((unsigned)std::distance(CalledFunc->abegin(), CalledFunc->aend()) ==
|
|
|
|
CI->getNumOperands()-1 && "No varargs calls can be inlined yet!");
|
|
|
|
|
|
|
|
unsigned i = 1;
|
|
|
|
for (Function::const_aiterator I = CalledFunc->abegin(), E=CalledFunc->aend();
|
|
|
|
I != E; ++I, ++i)
|
|
|
|
ValueMap[I] = CI->getOperand(i);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-11-19 22:54:07 +01:00
|
|
|
// Since we are now done with the CallInst, we can delete it.
|
|
|
|
delete CI;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-11-19 22:54:07 +01:00
|
|
|
// Make a vector to capture the return instructions in the cloned function...
|
|
|
|
std::vector<ReturnInst*> Returns;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-01-13 01:27:23 +01:00
|
|
|
// Populate the value map with all of the globals in the program.
|
|
|
|
Module &M = *OrigBB->getParent()->getParent();
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
|
|
ValueMap[I] = I;
|
|
|
|
for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
|
|
|
|
ValueMap[I] = I;
|
|
|
|
|
2002-11-19 22:54:07 +01:00
|
|
|
// Do all of the hard part of cloning the callee into the caller...
|
2002-11-19 23:54:01 +01:00
|
|
|
CloneFunctionInto(OrigBB->getParent(), CalledFunc, ValueMap, Returns, ".i");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-11-19 22:54:07 +01:00
|
|
|
// Loop over all of the return instructions, turning them into unconditional
|
|
|
|
// branches to the merge point now...
|
|
|
|
for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
|
|
|
|
ReturnInst *RI = Returns[i];
|
|
|
|
BasicBlock *BB = RI->getParent();
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-11-19 22:54:07 +01:00
|
|
|
// Add a branch to the merge point where the PHI node would live...
|
|
|
|
new BranchInst(NewBB, RI);
|
2002-09-22 20:41:25 +02:00
|
|
|
|
2002-11-19 22:54:07 +01:00
|
|
|
if (PHI) { // The PHI node should include this value!
|
|
|
|
assert(RI->getReturnValue() && "Ret should have value!");
|
|
|
|
assert(RI->getReturnValue()->getType() == PHI->getType() &&
|
|
|
|
"Ret value not consistent in function!");
|
|
|
|
PHI->addIncoming(RI->getReturnValue(), BB);
|
2002-09-22 20:41:25 +02:00
|
|
|
}
|
2002-11-19 22:54:07 +01:00
|
|
|
|
|
|
|
// Delete the return instruction now
|
|
|
|
BB->getInstList().erase(RI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if the PHI node only has one argument. This is a common
|
|
|
|
// case resulting from there only being a single return instruction in the
|
|
|
|
// function call. Because this is so common, eliminate the PHI node.
|
|
|
|
//
|
|
|
|
if (PHI && PHI->getNumIncomingValues() == 1) {
|
|
|
|
PHI->replaceAllUsesWith(PHI->getIncomingValue(0));
|
|
|
|
PHI->getParent()->getInstList().erase(PHI);
|
2002-09-22 20:41:25 +02:00
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
// Change the branch that used to go to NewBB to branch to the first basic
|
2002-04-07 22:49:59 +02:00
|
|
|
// block of the inlined function.
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
TerminatorInst *Br = OrigBB->getTerminator();
|
2001-07-07 21:24:15 +02:00
|
|
|
assert(Br && Br->getOpcode() == Instruction::Br &&
|
2001-06-06 22:29:01 +02:00
|
|
|
"splitBasicBlock broken!");
|
2002-11-19 22:54:07 +01:00
|
|
|
Br->setOperand(0, ++LastBlock);
|
2001-06-06 22:29:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) {
|
2001-06-06 22:29:01 +02:00
|
|
|
assert(CI->getParent() && CI->getParent()->getParent() &&
|
2002-04-27 08:56:12 +02:00
|
|
|
"Call not embedded into a function!");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
// Don't inline a recursive call.
|
2002-04-07 22:49:59 +02:00
|
|
|
if (CI->getParent()->getParent() == F) return false;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
// Don't inline something too big. This is a really crappy heuristic
|
2002-04-07 22:49:59 +02:00
|
|
|
if (F->size() > 3) return false;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
// Don't inline into something too big. This is a **really** crappy heuristic
|
2001-06-28 01:41:11 +02:00
|
|
|
if (CI->getParent()->getParent()->size() > 10) return false;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
// Go ahead and try just about anything else.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
static inline bool DoFunctionInlining(BasicBlock *BB) {
|
2001-06-28 01:41:11 +02:00
|
|
|
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
|
2002-06-25 18:13:24 +02:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(&*I)) {
|
2002-04-07 22:49:59 +02:00
|
|
|
// Check to see if we should inline this function
|
|
|
|
Function *F = CI->getCalledFunction();
|
2002-06-25 18:13:24 +02:00
|
|
|
if (F && ShouldInlineFunction(CI, F)) {
|
|
|
|
return InlineFunction(CI);
|
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
// doFunctionInlining - Use a heuristic based approach to inline functions that
|
2002-02-26 22:46:54 +01:00
|
|
|
// seem to look good.
|
|
|
|
//
|
2002-06-25 18:13:24 +02:00
|
|
|
static bool doFunctionInlining(Function &F) {
|
2001-06-06 22:29:01 +02:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Loop through now and inline instructions a basic block at a time...
|
2002-06-25 18:13:24 +02:00
|
|
|
for (Function::iterator I = F.begin(); I != F.end(); )
|
|
|
|
if (DoFunctionInlining(I)) {
|
2002-05-10 17:38:35 +02:00
|
|
|
++NumInlined;
|
2001-06-06 22:29:01 +02:00
|
|
|
Changed = true;
|
|
|
|
} else {
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
2002-02-26 22:46:54 +01:00
|
|
|
|
|
|
|
namespace {
|
2002-04-27 08:56:12 +02:00
|
|
|
struct FunctionInlining : public FunctionPass {
|
2002-06-25 18:13:24 +02:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
2002-04-07 22:49:59 +02:00
|
|
|
return doFunctionInlining(F);
|
2002-02-26 22:46:54 +01:00
|
|
|
}
|
|
|
|
};
|
2002-07-26 23:12:46 +02:00
|
|
|
RegisterOpt<FunctionInlining> X("inline", "Function Integration/Inlining");
|
2002-02-26 22:46:54 +01:00
|
|
|
}
|
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
Pass *createFunctionInliningPass() { return new FunctionInlining(); }
|