blob: 69d0926fb6212334c84443c82bb0913fad951843 [file] [log] [blame]
Misha Brukman444fdea2003-11-17 19:35:17 +00001//===-- ExtractFunction.cpp - Function extraction pass --------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Brukmanfd939082005-04-21 23:48:37 +000028 FunctionExtractorPass(Function *F = 0, bool deleteFn = true)
Misha Brukman7d248392004-04-22 23:00:51 +000029 : 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 }
Chris Lattner9dcbf0d2006-10-20 21:35:41 +000036
Misha Brukman7d248392004-04-22 23:00:51 +000037 if (deleteFunc)
Misha Brukman79906c92004-04-22 22:52:22 +000038 return deleteFunction();
Chris Lattner9dcbf0d2006-10-20 21:35:41 +000039 M.setModuleInlineAsm("");
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 }
Misha Brukmanfd939082005-04-21 23:48:37 +000060
Chris Lattner5113eb02002-11-19 18:42:59 +000061 // 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;
Misha Brukmanfd939082005-04-21 23:48:37 +000065
Chris Lattnerfeb15502005-03-15 05:19:49 +000066 Function *Last = --M.end(); // Figure out where the last real fn is.
Misha Brukmanfd939082005-04-21 23:48:37 +000067
Chris Lattner5113eb02002-11-19 18:42:59 +000068 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
Misha Brukmanfd939082005-04-21 23:48:37 +000074
Chris Lattner5113eb02002-11-19 18:42:59 +000075 // If it's not the named function, delete the body of the function
76 I->dropAllReferences();
Misha Brukmanfd939082005-04-21 23:48:37 +000077
Chris Lattner5113eb02002-11-19 18:42:59 +000078 M.getFunctionList().push_back(New);
79 NewFunctions.push_back(New);
80 }
Misha Brukmanfd939082005-04-21 23:48:37 +000081
Chris Lattner5113eb02002-11-19 18:42:59 +000082 if (&*I == Last) break; // Stop after processing the last function
83 }
Misha Brukmanfd939082005-04-21 23:48:37 +000084
Chris Lattner5113eb02002-11-19 18:42:59 +000085 // 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++]);
Misha Brukmanfd939082005-04-21 23:48:37 +000095
Chris Lattner5113eb02002-11-19 18:42:59 +000096 Function *Old = I;
97 ++I; // Move the iterator to the new function
Misha Brukmanfd939082005-04-21 23:48:37 +000098
Chris Lattner5113eb02002-11-19 18:42:59 +000099 // Delete the old function!
100 M.getFunctionList().erase(Old);
Misha Brukmanfd939082005-04-21 23:48:37 +0000101
Chris Lattner5113eb02002-11-19 18:42:59 +0000102 } else {
103 ++I; // Skip the function we are extracting
104 }
105 } while (&*I != NewFunctions[0]);
106 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000107
Chris Lattner5113eb02002-11-19 18:42:59 +0000108 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}