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();
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::giterator I = M.gbegin(), E = M.gend(); 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.back(); // 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 I->setName(""); // Remove Old name
75 // If it's not the named function, delete the body of the function
76 I->dropAllReferences();
78 M.getFunctionList().push_back(New);
79 NewFunctions.push_back(New);
82 if (&*I == Last) break; // Stop after processing the last function
85 // Now that we have replacements all set up, loop through the module,
86 // deleting the old functions, replacing them with the newly created
88 if (!NewFunctions.empty()) {
90 Module::iterator I = M.begin();
93 // Make everything that uses the old function use the new dummy fn
94 I->replaceAllUsesWith(NewFunctions[FuncNum++]);
97 ++I; // Move the iterator to the new function
99 // Delete the old function!
100 M.getFunctionList().erase(Old);
103 ++I; // Skip the function we are extracting
105 } while (&*I != NewFunctions[0]);
112 RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
115 ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn) {
116 return new FunctionExtractorPass(F, deleteFn);