blob: f98c6197dcaccff3f289ce15e5c2067e14ee5feb [file] [log] [blame]
Misha Brukman444fdea2003-11-17 19:35:17 +00001//===-- ExtractFunction.cpp - Function extraction pass --------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner1e2385b2003-11-21 21:54:22 +00009
Chris Lattner5113eb02002-11-19 18:42:59 +000010#include "llvm/Transforms/IPO.h"
11#include "llvm/Pass.h"
12#include "llvm/Module.h"
Chris Lattner1e2385b2003-11-21 21:54:22 +000013using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000014
Chris Lattner5113eb02002-11-19 18:42:59 +000015namespace {
16 class FunctionExtractorPass : public Pass {
17 Function *Named;
18 public:
19 FunctionExtractorPass(Function *F = 0) : Named(F) {}
20
21 bool run(Module &M) {
22 if (Named == 0) {
23 Named = M.getMainFunction();
24 if (Named == 0) return false; // No function to extract
25 }
26
Misha Brukmancf00c4a2003-10-10 17:57:28 +000027 // Make sure our result is globally accessible...
Chris Lattner4ad02e72003-04-16 20:28:45 +000028 Named->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattner5113eb02002-11-19 18:42:59 +000029
30 // Mark all global variables internal
31 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
32 if (!I->isExternal()) {
33 I->setInitializer(0); // Make all variables external
Chris Lattner4ad02e72003-04-16 20:28:45 +000034 I->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattner5113eb02002-11-19 18:42:59 +000035 }
36
37 // All of the functions may be used by global variables or the named
38 // function. Loop through them and create a new, external functions that
39 // can be "used", instead of ones with bodies.
40 //
41 std::vector<Function*> NewFunctions;
42
43 Function *Last = &M.back(); // Figure out where the last real fn is...
44
45 for (Module::iterator I = M.begin(); ; ++I) {
46 if (&*I != Named) {
Chris Lattner4ad02e72003-04-16 20:28:45 +000047 Function *New = new Function(I->getFunctionType(),
48 GlobalValue::ExternalLinkage,
49 I->getName());
Chris Lattner5113eb02002-11-19 18:42:59 +000050 I->setName(""); // Remove Old name
51
52 // If it's not the named function, delete the body of the function
53 I->dropAllReferences();
54
55 M.getFunctionList().push_back(New);
56 NewFunctions.push_back(New);
57 }
58
59 if (&*I == Last) break; // Stop after processing the last function
60 }
61
62 // Now that we have replacements all set up, loop through the module,
63 // deleting the old functions, replacing them with the newly created
64 // functions.
65 if (!NewFunctions.empty()) {
66 unsigned FuncNum = 0;
67 Module::iterator I = M.begin();
68 do {
69 if (&*I != Named) {
70 // Make everything that uses the old function use the new dummy fn
71 I->replaceAllUsesWith(NewFunctions[FuncNum++]);
72
73 Function *Old = I;
74 ++I; // Move the iterator to the new function
75
76 // Delete the old function!
77 M.getFunctionList().erase(Old);
78
79 } else {
80 ++I; // Skip the function we are extracting
81 }
82 } while (&*I != NewFunctions[0]);
83 }
84
85 return true;
86 }
87 };
88
89 RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
90}
91
Chris Lattner1e2385b2003-11-21 21:54:22 +000092Pass *llvm::createFunctionExtractionPass(Function *F) {
Chris Lattner5113eb02002-11-19 18:42:59 +000093 return new FunctionExtractorPass(F);
94}