blob: e5b2d41d52893a96dbcbe560c06bdceadf89a63d [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- 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// This pass extracts
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Instructions.h"
15#include "llvm/Module.h"
16#include "llvm/Pass.h"
17#include "llvm/Transforms/IPO.h"
18#include "llvm/Support/Compiler.h"
19using namespace llvm;
20
21namespace {
22 /// @brief A pass to extract specific functions and their dependencies.
23 class VISIBILITY_HIDDEN FunctionExtractorPass : public ModulePass {
24 Function *Named;
25 bool deleteFunc;
26 bool reLink;
27 public:
28 static char ID; // Pass identification, replacement for typeid
29
30 /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the
31 /// specified function. Otherwise, it deletes as much of the module as
32 /// possible, except for the function specified.
33 ///
Dan Gohman34c280e2007-08-01 15:32:29 +000034 explicit FunctionExtractorPass(Function *F = 0, bool deleteFn = true,
35 bool relinkCallees = false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 : ModulePass((intptr_t)&ID), Named(F), deleteFunc(deleteFn),
37 reLink(relinkCallees) {}
38
39 bool runOnModule(Module &M) {
40 if (Named == 0) {
41 Named = M.getFunction("main");
42 if (Named == 0) return false; // No function to extract
43 }
44
45 if (deleteFunc)
46 return deleteFunction();
47 M.setModuleInlineAsm("");
48 return isolateFunction(M);
49 }
50
51 bool deleteFunction() {
52 // If we're in relinking mode, set linkage of all internal callees to
53 // external. This will allow us extract function, and then - link
54 // everything together
55 if (reLink) {
56 for (Function::iterator B = Named->begin(), BE = Named->end();
57 B != BE; ++B) {
58 for (BasicBlock::iterator I = B->begin(), E = B->end();
59 I != E; ++I) {
60 if (CallInst* callInst = dyn_cast<CallInst>(&*I)) {
61 Function* Callee = callInst->getCalledFunction();
62 if (Callee && Callee->hasInternalLinkage())
63 Callee->setLinkage(GlobalValue::ExternalLinkage);
64 }
65 }
66 }
67 }
68
69 Named->setLinkage(GlobalValue::ExternalLinkage);
70 Named->deleteBody();
71 assert(Named->isDeclaration() && "This didn't make the function external!");
72 return true;
73 }
74
75 bool isolateFunction(Module &M) {
76 // Make sure our result is globally accessible...
77 Named->setLinkage(GlobalValue::ExternalLinkage);
78
79 // Mark all global variables internal
80 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
81 if (!I->isDeclaration()) {
82 I->setInitializer(0); // Make all variables external
83 I->setLinkage(GlobalValue::ExternalLinkage);
84 }
85
86 // All of the functions may be used by global variables or the named
87 // function. Loop through them and create a new, external functions that
88 // can be "used", instead of ones with bodies.
89 std::vector<Function*> NewFunctions;
90
91 Function *Last = --M.end(); // Figure out where the last real fn is.
92
93 for (Module::iterator I = M.begin(); ; ++I) {
94 if (&*I != Named) {
95 Function *New = new Function(I->getFunctionType(),
96 GlobalValue::ExternalLinkage);
97 New->setCallingConv(I->getCallingConv());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000098 New->setParamAttrs(I->getParamAttrs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
100 // If it's not the named function, delete the body of the function
101 I->dropAllReferences();
102
103 M.getFunctionList().push_back(New);
104 NewFunctions.push_back(New);
105 New->takeName(I);
106 }
107
108 if (&*I == Last) break; // Stop after processing the last function
109 }
110
111 // Now that we have replacements all set up, loop through the module,
112 // deleting the old functions, replacing them with the newly created
113 // functions.
114 if (!NewFunctions.empty()) {
115 unsigned FuncNum = 0;
116 Module::iterator I = M.begin();
117 do {
118 if (&*I != Named) {
119 // Make everything that uses the old function use the new dummy fn
120 I->replaceAllUsesWith(NewFunctions[FuncNum++]);
121
122 Function *Old = I;
123 ++I; // Move the iterator to the new function
124
125 // Delete the old function!
126 M.getFunctionList().erase(Old);
127
128 } else {
129 ++I; // Skip the function we are extracting
130 }
131 } while (&*I != NewFunctions[0]);
132 }
133
134 return true;
135 }
136 };
137
138 char FunctionExtractorPass::ID = 0;
139 RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
140}
141
142ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn,
143 bool relinkCallees) {
144 return new FunctionExtractorPass(F, deleteFn, relinkCallees);
145}