2003-11-18 03:35:17 +08:00
|
|
|
//===-- ExtractFunction.cpp - Function extraction pass --------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-04-23 06:52:22 +08:00
|
|
|
//
|
|
|
|
// This pass extracts
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-11-22 05:54:22 +08:00
|
|
|
|
2007-01-28 21:31:35 +08:00
|
|
|
#include "llvm/Instructions.h"
|
2002-11-20 02:42:59 +08:00
|
|
|
#include "llvm/Module.h"
|
2004-04-23 06:52:22 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2007-02-06 07:32:05 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2003-11-22 05:54:22 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
namespace {
|
2007-02-06 07:32:05 +08:00
|
|
|
/// @brief A pass to extract specific functions and their dependencies.
|
|
|
|
class VISIBILITY_HIDDEN FunctionExtractorPass : public ModulePass {
|
2002-11-20 02:42:59 +08:00
|
|
|
Function *Named;
|
2004-04-23 07:00:51 +08:00
|
|
|
bool deleteFunc;
|
2007-01-28 21:31:35 +08:00
|
|
|
bool reLink;
|
2002-11-20 02:42:59 +08:00
|
|
|
public:
|
2004-04-23 07:00:51 +08:00
|
|
|
/// FunctionExtractorPass - If deleteFn is true, this pass deletes as the
|
|
|
|
/// specified function. Otherwise, it deletes as much of the module as
|
|
|
|
/// possible, except for the function specified.
|
2004-04-23 06:52:22 +08:00
|
|
|
///
|
2007-01-28 21:31:35 +08:00
|
|
|
FunctionExtractorPass(Function *F = 0, bool deleteFn = true,
|
|
|
|
bool relinkCallees = false)
|
|
|
|
: Named(F), deleteFunc(deleteFn), reLink(relinkCallees) {}
|
2002-11-20 02:42:59 +08:00
|
|
|
|
2004-09-20 12:48:05 +08:00
|
|
|
bool runOnModule(Module &M) {
|
2002-11-20 02:42:59 +08:00
|
|
|
if (Named == 0) {
|
2007-02-06 05:19:13 +08:00
|
|
|
Named = M.getFunction("main");
|
2002-11-20 02:42:59 +08:00
|
|
|
if (Named == 0) return false; // No function to extract
|
|
|
|
}
|
2006-10-21 05:35:41 +08:00
|
|
|
|
2004-04-23 07:00:51 +08:00
|
|
|
if (deleteFunc)
|
2004-04-23 06:52:22 +08:00
|
|
|
return deleteFunction();
|
2006-10-21 05:35:41 +08:00
|
|
|
M.setModuleInlineAsm("");
|
|
|
|
return isolateFunction(M);
|
2004-04-23 06:52:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool deleteFunction() {
|
2007-01-28 21:31:35 +08:00
|
|
|
// If we're in relinking mode, set linkage of all internal callees to
|
|
|
|
// external. This will allow us extract function, and then - link
|
|
|
|
// everything together
|
|
|
|
if (reLink) {
|
|
|
|
for (Function::iterator B = Named->begin(), BE = Named->end();
|
|
|
|
B != BE; ++B) {
|
|
|
|
for (BasicBlock::iterator I = B->begin(), E = B->end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (CallInst* callInst = dyn_cast<CallInst>(&*I)) {
|
|
|
|
Function* Callee = callInst->getCalledFunction();
|
|
|
|
if (Callee && Callee->hasInternalLinkage())
|
|
|
|
Callee->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-23 06:52:22 +08:00
|
|
|
Named->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
Named->deleteBody();
|
2007-01-31 04:08:39 +08:00
|
|
|
assert(Named->isDeclaration() && "This didn't make the function external!");
|
2004-04-23 06:52:22 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isolateFunction(Module &M) {
|
2003-10-11 01:57:28 +08:00
|
|
|
// Make sure our result is globally accessible...
|
2003-04-17 04:28:45 +08:00
|
|
|
Named->setLinkage(GlobalValue::ExternalLinkage);
|
2002-11-20 02:42:59 +08:00
|
|
|
|
|
|
|
// Mark all global variables internal
|
2005-03-15 12:54:21 +08:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
|
2007-01-31 04:08:39 +08:00
|
|
|
if (!I->isDeclaration()) {
|
2002-11-20 02:42:59 +08:00
|
|
|
I->setInitializer(0); // Make all variables external
|
2003-04-17 04:28:45 +08:00
|
|
|
I->setLinkage(GlobalValue::ExternalLinkage);
|
2002-11-20 02:42:59 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
// All of the functions may be used by global variables or the named
|
|
|
|
// function. Loop through them and create a new, external functions that
|
|
|
|
// can be "used", instead of ones with bodies.
|
|
|
|
std::vector<Function*> NewFunctions;
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2005-03-15 13:19:49 +08:00
|
|
|
Function *Last = --M.end(); // Figure out where the last real fn is.
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
for (Module::iterator I = M.begin(); ; ++I) {
|
|
|
|
if (&*I != Named) {
|
2003-04-17 04:28:45 +08:00
|
|
|
Function *New = new Function(I->getFunctionType(),
|
2007-02-11 09:08:35 +08:00
|
|
|
GlobalValue::ExternalLinkage);
|
2007-01-26 01:38:26 +08:00
|
|
|
New->setCallingConv(I->getCallingConv());
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
// If it's not the named function, delete the body of the function
|
|
|
|
I->dropAllReferences();
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
M.getFunctionList().push_back(New);
|
|
|
|
NewFunctions.push_back(New);
|
2007-02-11 09:08:35 +08:00
|
|
|
New->takeName(I);
|
2002-11-20 02:42:59 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
if (&*I == Last) break; // Stop after processing the last function
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
// Now that we have replacements all set up, loop through the module,
|
|
|
|
// deleting the old functions, replacing them with the newly created
|
|
|
|
// functions.
|
|
|
|
if (!NewFunctions.empty()) {
|
|
|
|
unsigned FuncNum = 0;
|
|
|
|
Module::iterator I = M.begin();
|
|
|
|
do {
|
|
|
|
if (&*I != Named) {
|
|
|
|
// Make everything that uses the old function use the new dummy fn
|
|
|
|
I->replaceAllUsesWith(NewFunctions[FuncNum++]);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
Function *Old = I;
|
|
|
|
++I; // Move the iterator to the new function
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
// Delete the old function!
|
|
|
|
M.getFunctionList().erase(Old);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
} else {
|
|
|
|
++I; // Skip the function we are extracting
|
|
|
|
}
|
|
|
|
} while (&*I != NewFunctions[0]);
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 02:42:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
|
|
|
|
}
|
|
|
|
|
2007-01-28 21:31:35 +08:00
|
|
|
ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn,
|
|
|
|
bool relinkCallees) {
|
|
|
|
return new FunctionExtractorPass(F, deleteFn, relinkCallees);
|
2002-11-20 02:42:59 +08:00
|
|
|
}
|