blob: 482964e86f2c62fd7482340566111bff40cc47e8 [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//===----------------------------------------------------------------------===//
Misha Brukman79906c92004-04-22 22:52:22 +00009//
10// This pass extracts
11//
12//===----------------------------------------------------------------------===//
Chris Lattner1e2385b2003-11-21 21:54:22 +000013
Chris Lattner5113eb02002-11-19 18:42:59 +000014#include "llvm/Module.h"
Misha Brukman79906c92004-04-22 22:52:22 +000015#include "llvm/Pass.h"
16#include "llvm/Transforms/IPO.h"
Chris Lattner1e2385b2003-11-21 21:54:22 +000017using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000018
Chris Lattner5113eb02002-11-19 18:42:59 +000019namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000020 class FunctionExtractorPass : public ModulePass {
Chris Lattner5113eb02002-11-19 18:42:59 +000021 Function *Named;
Misha Brukman7d248392004-04-22 23:00:51 +000022 bool deleteFunc;
Chris Lattner5113eb02002-11-19 18:42:59 +000023 public:
Misha Brukman7d248392004-04-22 23:00:51 +000024 /// 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.
Misha Brukman79906c92004-04-22 22:52:22 +000027 ///
Misha Brukman7d248392004-04-22 23:00:51 +000028 FunctionExtractorPass(Function *F = 0, bool deleteFn = true)
29 : Named(F), deleteFunc(deleteFn) {}
Chris Lattner5113eb02002-11-19 18:42:59 +000030
Chris Lattnerb12914b2004-09-20 04:48:05 +000031 bool runOnModule(Module &M) {
Chris Lattner5113eb02002-11-19 18:42:59 +000032 if (Named == 0) {
33 Named = M.getMainFunction();
34 if (Named == 0) return false; // No function to extract
35 }
36
Misha Brukman7d248392004-04-22 23:00:51 +000037 if (deleteFunc)
Misha Brukman79906c92004-04-22 22:52:22 +000038 return deleteFunction();
Misha Brukman7d248392004-04-22 23:00:51 +000039 else
40 return isolateFunction(M);
Misha Brukman79906c92004-04-22 22:52:22 +000041 }
42
43 bool deleteFunction() {
44 Named->setLinkage(GlobalValue::ExternalLinkage);
45 Named->deleteBody();
46 assert(Named->isExternal() && "This didn't make the function external!");
47 return true;
48 }
49
50 bool isolateFunction(Module &M) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +000051 // Make sure our result is globally accessible...
Chris Lattner4ad02e72003-04-16 20:28:45 +000052 Named->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattner5113eb02002-11-19 18:42:59 +000053
54 // Mark all global variables internal
Chris Lattnere4d5c442005-03-15 04:54:21 +000055 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattner5113eb02002-11-19 18:42:59 +000056 if (!I->isExternal()) {
57 I->setInitializer(0); // Make all variables external
Chris Lattner4ad02e72003-04-16 20:28:45 +000058 I->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattner5113eb02002-11-19 18:42:59 +000059 }
60
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.
Chris Lattner5113eb02002-11-19 18:42:59 +000064 std::vector<Function*> NewFunctions;
65
66 Function *Last = &M.back(); // Figure out where the last real fn is...
67
68 for (Module::iterator I = M.begin(); ; ++I) {
69 if (&*I != Named) {
Chris Lattner4ad02e72003-04-16 20:28:45 +000070 Function *New = new Function(I->getFunctionType(),
71 GlobalValue::ExternalLinkage,
72 I->getName());
Chris Lattner5113eb02002-11-19 18:42:59 +000073 I->setName(""); // Remove Old name
74
75 // If it's not the named function, delete the body of the function
76 I->dropAllReferences();
77
78 M.getFunctionList().push_back(New);
79 NewFunctions.push_back(New);
80 }
81
82 if (&*I == Last) break; // Stop after processing the last function
83 }
84
85 // Now that we have replacements all set up, loop through the module,
86 // deleting the old functions, replacing them with the newly created
87 // functions.
88 if (!NewFunctions.empty()) {
89 unsigned FuncNum = 0;
90 Module::iterator I = M.begin();
91 do {
92 if (&*I != Named) {
93 // Make everything that uses the old function use the new dummy fn
94 I->replaceAllUsesWith(NewFunctions[FuncNum++]);
95
96 Function *Old = I;
97 ++I; // Move the iterator to the new function
98
99 // Delete the old function!
100 M.getFunctionList().erase(Old);
101
102 } else {
103 ++I; // Skip the function we are extracting
104 }
105 } while (&*I != NewFunctions[0]);
106 }
107
108 return true;
109 }
110 };
111
112 RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
113}
114
Chris Lattnerb12914b2004-09-20 04:48:05 +0000115ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn) {
Misha Brukman7d248392004-04-22 23:00:51 +0000116 return new FunctionExtractorPass(F, deleteFn);
Chris Lattner5113eb02002-11-19 18:42:59 +0000117}