blob: 1a284c4f5bf06ca83c9b460ac01c537daa1da4e7 [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
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000014#include "llvm/Instructions.h"
Chris Lattner5113eb02002-11-19 18:42:59 +000015#include "llvm/Module.h"
Misha Brukman79906c92004-04-22 22:52:22 +000016#include "llvm/Pass.h"
17#include "llvm/Transforms/IPO.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000018#include "llvm/Support/Compiler.h"
Chris Lattner1e2385b2003-11-21 21:54:22 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner5113eb02002-11-19 18:42:59 +000021namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000022 /// @brief A pass to extract specific functions and their dependencies.
23 class VISIBILITY_HIDDEN FunctionExtractorPass : public ModulePass {
Chris Lattner5113eb02002-11-19 18:42:59 +000024 Function *Named;
Misha Brukman7d248392004-04-22 23:00:51 +000025 bool deleteFunc;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000026 bool reLink;
Chris Lattner5113eb02002-11-19 18:42:59 +000027 public:
Devang Patel19974732007-05-03 01:11:54 +000028 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000029
Misha Brukman7d248392004-04-22 23:00:51 +000030 /// 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.
Misha Brukman79906c92004-04-22 22:52:22 +000033 ///
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000034 FunctionExtractorPass(Function *F = 0, bool deleteFn = true,
35 bool relinkCallees = false)
Devang Patel794fd752007-05-01 21:15:47 +000036 : ModulePass((intptr_t)&ID), Named(F), deleteFunc(deleteFn),
37 reLink(relinkCallees) {}
Chris Lattner5113eb02002-11-19 18:42:59 +000038
Chris Lattnerb12914b2004-09-20 04:48:05 +000039 bool runOnModule(Module &M) {
Chris Lattner5113eb02002-11-19 18:42:59 +000040 if (Named == 0) {
Reid Spencer688b0492007-02-05 21:19:13 +000041 Named = M.getFunction("main");
Chris Lattner5113eb02002-11-19 18:42:59 +000042 if (Named == 0) return false; // No function to extract
43 }
Chris Lattner9dcbf0d2006-10-20 21:35:41 +000044
Misha Brukman7d248392004-04-22 23:00:51 +000045 if (deleteFunc)
Misha Brukman79906c92004-04-22 22:52:22 +000046 return deleteFunction();
Chris Lattner9dcbf0d2006-10-20 21:35:41 +000047 M.setModuleInlineAsm("");
48 return isolateFunction(M);
Misha Brukman79906c92004-04-22 22:52:22 +000049 }
50
51 bool deleteFunction() {
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000052 // 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
Misha Brukman79906c92004-04-22 22:52:22 +000069 Named->setLinkage(GlobalValue::ExternalLinkage);
70 Named->deleteBody();
Reid Spencer5cbf9852007-01-30 20:08:39 +000071 assert(Named->isDeclaration() && "This didn't make the function external!");
Misha Brukman79906c92004-04-22 22:52:22 +000072 return true;
73 }
74
75 bool isolateFunction(Module &M) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +000076 // Make sure our result is globally accessible...
Chris Lattner4ad02e72003-04-16 20:28:45 +000077 Named->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattner5113eb02002-11-19 18:42:59 +000078
79 // Mark all global variables internal
Chris Lattnere4d5c442005-03-15 04:54:21 +000080 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000081 if (!I->isDeclaration()) {
Chris Lattner5113eb02002-11-19 18:42:59 +000082 I->setInitializer(0); // Make all variables external
Chris Lattner4ad02e72003-04-16 20:28:45 +000083 I->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattner5113eb02002-11-19 18:42:59 +000084 }
Misha Brukmanfd939082005-04-21 23:48:37 +000085
Chris Lattner5113eb02002-11-19 18:42:59 +000086 // 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.
Chris Lattner5113eb02002-11-19 18:42:59 +000089 std::vector<Function*> NewFunctions;
Misha Brukmanfd939082005-04-21 23:48:37 +000090
Chris Lattnerfeb15502005-03-15 05:19:49 +000091 Function *Last = --M.end(); // Figure out where the last real fn is.
Misha Brukmanfd939082005-04-21 23:48:37 +000092
Chris Lattner5113eb02002-11-19 18:42:59 +000093 for (Module::iterator I = M.begin(); ; ++I) {
94 if (&*I != Named) {
Chris Lattner4ad02e72003-04-16 20:28:45 +000095 Function *New = new Function(I->getFunctionType(),
Chris Lattner046800a2007-02-11 01:08:35 +000096 GlobalValue::ExternalLinkage);
Chris Lattner338305b2007-01-25 17:38:26 +000097 New->setCallingConv(I->getCallingConv());
Misha Brukmanfd939082005-04-21 23:48:37 +000098
Chris Lattner5113eb02002-11-19 18:42:59 +000099 // If it's not the named function, delete the body of the function
100 I->dropAllReferences();
Misha Brukmanfd939082005-04-21 23:48:37 +0000101
Chris Lattner5113eb02002-11-19 18:42:59 +0000102 M.getFunctionList().push_back(New);
103 NewFunctions.push_back(New);
Chris Lattner046800a2007-02-11 01:08:35 +0000104 New->takeName(I);
Chris Lattner5113eb02002-11-19 18:42:59 +0000105 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000106
Chris Lattner5113eb02002-11-19 18:42:59 +0000107 if (&*I == Last) break; // Stop after processing the last function
108 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000109
Chris Lattner5113eb02002-11-19 18:42:59 +0000110 // Now that we have replacements all set up, loop through the module,
111 // deleting the old functions, replacing them with the newly created
112 // functions.
113 if (!NewFunctions.empty()) {
114 unsigned FuncNum = 0;
115 Module::iterator I = M.begin();
116 do {
117 if (&*I != Named) {
118 // Make everything that uses the old function use the new dummy fn
119 I->replaceAllUsesWith(NewFunctions[FuncNum++]);
Misha Brukmanfd939082005-04-21 23:48:37 +0000120
Chris Lattner5113eb02002-11-19 18:42:59 +0000121 Function *Old = I;
122 ++I; // Move the iterator to the new function
Misha Brukmanfd939082005-04-21 23:48:37 +0000123
Chris Lattner5113eb02002-11-19 18:42:59 +0000124 // Delete the old function!
125 M.getFunctionList().erase(Old);
Misha Brukmanfd939082005-04-21 23:48:37 +0000126
Chris Lattner5113eb02002-11-19 18:42:59 +0000127 } else {
128 ++I; // Skip the function we are extracting
129 }
130 } while (&*I != NewFunctions[0]);
131 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000132
Chris Lattner5113eb02002-11-19 18:42:59 +0000133 return true;
134 }
135 };
136
Devang Patel19974732007-05-03 01:11:54 +0000137 char FunctionExtractorPass::ID = 0;
Chris Lattner5113eb02002-11-19 18:42:59 +0000138 RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
139}
140
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000141ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn,
142 bool relinkCallees) {
143 return new FunctionExtractorPass(F, deleteFn, relinkCallees);
Chris Lattner5113eb02002-11-19 18:42:59 +0000144}