2003-11-17 20:35:17 +01:00
|
|
|
//===-- ExtractFunction.cpp - Function extraction pass --------------------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02: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 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-04-23 00:52:22 +02:00
|
|
|
//
|
|
|
|
// This pass extracts
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-11-21 22:54:22 +01:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
#include "llvm/Module.h"
|
2004-04-23 00:52:22 +02:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2003-11-21 22:54:22 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
namespace {
|
2004-09-20 06:48:05 +02:00
|
|
|
class FunctionExtractorPass : public ModulePass {
|
2002-11-19 19:42:59 +01:00
|
|
|
Function *Named;
|
2004-04-23 01:00:51 +02:00
|
|
|
bool deleteFunc;
|
2002-11-19 19:42:59 +01:00
|
|
|
public:
|
2004-04-23 01:00:51 +02: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 00:52:22 +02:00
|
|
|
///
|
2005-04-22 01:48:37 +02:00
|
|
|
FunctionExtractorPass(Function *F = 0, bool deleteFn = true)
|
2004-04-23 01:00:51 +02:00
|
|
|
: Named(F), deleteFunc(deleteFn) {}
|
2002-11-19 19:42:59 +01:00
|
|
|
|
2004-09-20 06:48:05 +02:00
|
|
|
bool runOnModule(Module &M) {
|
2002-11-19 19:42:59 +01:00
|
|
|
if (Named == 0) {
|
|
|
|
Named = M.getMainFunction();
|
|
|
|
if (Named == 0) return false; // No function to extract
|
|
|
|
}
|
|
|
|
|
2004-04-23 01:00:51 +02:00
|
|
|
if (deleteFunc)
|
2004-04-23 00:52:22 +02:00
|
|
|
return deleteFunction();
|
2005-04-22 01:48:37 +02:00
|
|
|
else
|
2004-04-23 01:00:51 +02:00
|
|
|
return isolateFunction(M);
|
2004-04-23 00:52:22 +02: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-10 19:57:28 +02:00
|
|
|
// Make sure our result is globally accessible...
|
2003-04-16 22:28:45 +02:00
|
|
|
Named->setLinkage(GlobalValue::ExternalLinkage);
|
2002-11-19 19:42:59 +01:00
|
|
|
|
|
|
|
// Mark all global variables internal
|
2005-03-15 05:54:21 +01:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
|
2002-11-19 19:42:59 +01:00
|
|
|
if (!I->isExternal()) {
|
|
|
|
I->setInitializer(0); // Make all variables external
|
2003-04-16 22:28:45 +02:00
|
|
|
I->setLinkage(GlobalValue::ExternalLinkage);
|
2002-11-19 19:42:59 +01:00
|
|
|
}
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01: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 01:48:37 +02:00
|
|
|
|
2005-03-15 06:19:49 +01:00
|
|
|
Function *Last = --M.end(); // Figure out where the last real fn is.
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
for (Module::iterator I = M.begin(); ; ++I) {
|
|
|
|
if (&*I != Named) {
|
2003-04-16 22:28:45 +02:00
|
|
|
Function *New = new Function(I->getFunctionType(),
|
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
I->getName());
|
2002-11-19 19:42:59 +01:00
|
|
|
I->setName(""); // Remove Old name
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
// If it's not the named function, delete the body of the function
|
|
|
|
I->dropAllReferences();
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
M.getFunctionList().push_back(New);
|
|
|
|
NewFunctions.push_back(New);
|
|
|
|
}
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
if (&*I == Last) break; // Stop after processing the last function
|
|
|
|
}
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01: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 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
Function *Old = I;
|
|
|
|
++I; // Move the iterator to the new function
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
// Delete the old function!
|
|
|
|
M.getFunctionList().erase(Old);
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
} else {
|
|
|
|
++I; // Skip the function we are extracting
|
|
|
|
}
|
|
|
|
} while (&*I != NewFunctions[0]);
|
|
|
|
}
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
|
|
|
|
}
|
|
|
|
|
2004-09-20 06:48:05 +02:00
|
|
|
ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn) {
|
2004-04-23 01:00:51 +02:00
|
|
|
return new FunctionExtractorPass(F, deleteFn);
|
2002-11-19 19:42:59 +01:00
|
|
|
}
|