Finegrainify namespacification
[oota-llvm.git] / lib / Transforms / IPO / ExtractFunction.cpp
1 //===-- ExtractFunction.cpp - Function extraction pass --------------------===//
2 // 
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 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Transforms/IPO.h"
11 #include "llvm/Pass.h"
12 #include "llvm/Module.h"
13 using namespace llvm;
14
15 namespace {
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
27       // Make sure our result is globally accessible...
28       Named->setLinkage(GlobalValue::ExternalLinkage);
29
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
34           I->setLinkage(GlobalValue::ExternalLinkage);
35         }
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) {
47           Function *New = new Function(I->getFunctionType(),
48                                        GlobalValue::ExternalLinkage,
49                                        I->getName());
50           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
92 Pass *llvm::createFunctionExtractionPass(Function *F) {
93   return new FunctionExtractorPass(F);
94 }