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
|
|
|
|
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"
|
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 {
|
2004-09-20 12:48:05 +08:00
|
|
|
class FunctionExtractorPass : public ModulePass {
|
2002-11-20 02:42:59 +08:00
|
|
|
Function *Named;
|
2004-04-23 07:00:51 +08:00
|
|
|
bool deleteFunc;
|
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
|
|
|
///
|
2005-04-22 07:48:37 +08:00
|
|
|
FunctionExtractorPass(Function *F = 0, bool deleteFn = true)
|
2004-04-23 07:00:51 +08:00
|
|
|
: Named(F), deleteFunc(deleteFn) {}
|
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) {
|
|
|
|
Named = M.getMainFunction();
|
|
|
|
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() {
|
|
|
|
Named->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
Named->deleteBody();
|
|
|
|
assert(Named->isExternal() && "This didn't make the function external!");
|
|
|
|
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)
|
2002-11-20 02:42:59 +08:00
|
|
|
if (!I->isExternal()) {
|
|
|
|
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(),
|
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
I->getName());
|
2007-01-26 01:38:26 +08:00
|
|
|
New->setCallingConv(I->getCallingConv());
|
2002-11-20 02:42:59 +08:00
|
|
|
I->setName(""); // Remove Old name
|
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);
|
|
|
|
}
|
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");
|
|
|
|
}
|
|
|
|
|
2004-09-20 12:48:05 +08:00
|
|
|
ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn) {
|
2004-04-23 07:00:51 +08:00
|
|
|
return new FunctionExtractorPass(F, deleteFn);
|
2002-11-20 02:42:59 +08:00
|
|
|
}
|