1 //===-- ExtractFunction.cpp - Function extraction pass --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/Pass.h"
16 #include "llvm/Transforms/IPO.h"
20 class FunctionExtractorPass : public ModulePass {
24 /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the
25 /// specified function. Otherwise, it deletes as much of the module as
26 /// possible, except for the function specified.
28 FunctionExtractorPass(Function *F = 0, bool deleteFn = true)
29 : Named(F), deleteFunc(deleteFn) {}
31 bool runOnModule(Module &M) {
33 Named = M.getMainFunction();
34 if (Named == 0) return false; // No function to extract
38 return deleteFunction();
39 M.setModuleInlineAsm("");
40 return isolateFunction(M);
43 bool deleteFunction() {
44 Named->setLinkage(GlobalValue::ExternalLinkage);
46 assert(Named->isExternal() && "This didn't make the function external!");
50 bool isolateFunction(Module &M) {
51 // Make sure our result is globally accessible...
52 Named->setLinkage(GlobalValue::ExternalLinkage);
54 // Mark all global variables internal
55 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
56 if (!I->isExternal()) {
57 I->setInitializer(0); // Make all variables external
58 I->setLinkage(GlobalValue::ExternalLinkage);
61 // All of the functions may be used by global variables or the named
62 // function. Loop through them and create a new, external functions that
63 // can be "used", instead of ones with bodies.
64 std::vector<Function*> NewFunctions;
66 Function *Last = --M.end(); // Figure out where the last real fn is.
68 for (Module::iterator I = M.begin(); ; ++I) {
70 Function *New = new Function(I->getFunctionType(),
71 GlobalValue::ExternalLinkage,
73 New->setCallingConv(I->getCallingConv());
74 I->setName(""); // Remove Old name
76 // If it's not the named function, delete the body of the function
77 I->dropAllReferences();
79 M.getFunctionList().push_back(New);
80 NewFunctions.push_back(New);
83 if (&*I == Last) break; // Stop after processing the last function
86 // Now that we have replacements all set up, loop through the module,
87 // deleting the old functions, replacing them with the newly created
89 if (!NewFunctions.empty()) {
91 Module::iterator I = M.begin();
94 // Make everything that uses the old function use the new dummy fn
95 I->replaceAllUsesWith(NewFunctions[FuncNum++]);
98 ++I; // Move the iterator to the new function
100 // Delete the old function!
101 M.getFunctionList().erase(Old);
104 ++I; // Skip the function we are extracting
106 } while (&*I != NewFunctions[0]);
113 RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
116 ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn) {
117 return new FunctionExtractorPass(F, deleteFn);