blob: 9eb690d9474d58f1b7bd4c5719ea72d2185c7a1a [file] [log] [blame]
Andrew Lenharthd245a8a2008-03-07 19:51:57 +00001//===-- ExtractGV.cpp - Global Value extraction pass ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass extracts global values
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Instructions.h"
Owen Anderson14ce9ef2009-07-06 01:34:54 +000015#include "llvm/LLVMContext.h"
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000016#include "llvm/Module.h"
17#include "llvm/Pass.h"
18#include "llvm/Constants.h"
19#include "llvm/Transforms/IPO.h"
Ted Kremenek58d5e052008-03-09 18:32:50 +000020#include <algorithm>
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000021using namespace llvm;
22
23namespace {
24 /// @brief A pass to extract specific functions and their dependencies.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000025 class GVExtractorPass : public ModulePass {
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000026 std::vector<GlobalValue*> Named;
27 bool deleteStuff;
28 bool reLink;
29 public:
30 static char ID; // Pass identification, replacement for typeid
31
32 /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the
33 /// specified function. Otherwise, it deletes as much of the module as
34 /// possible, except for the function specified.
35 ///
36 explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true,
37 bool relinkCallees = false)
Owen Anderson90c579d2010-08-06 18:33:48 +000038 : ModulePass(ID), Named(GVs), deleteStuff(deleteS),
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000039 reLink(relinkCallees) {}
40
41 bool runOnModule(Module &M) {
42 if (Named.size() == 0) {
43 return false; // Nothing to extract
44 }
45
Owen Anderson001dbfe2009-07-16 18:04:31 +000046
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000047 if (deleteStuff)
48 return deleteGV();
49 M.setModuleInlineAsm("");
50 return isolateGV(M);
51 }
52
53 bool deleteGV() {
54 for (std::vector<GlobalValue*>::iterator GI = Named.begin(),
55 GE = Named.end(); GI != GE; ++GI) {
Dan Gohmanb9a31a12008-10-21 01:08:07 +000056 if (Function* NamedFunc = dyn_cast<Function>(*GI)) {
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000057 // If we're in relinking mode, set linkage of all internal callees to
58 // external. This will allow us extract function, and then - link
59 // everything together
60 if (reLink) {
61 for (Function::iterator B = NamedFunc->begin(), BE = NamedFunc->end();
62 B != BE; ++B) {
63 for (BasicBlock::iterator I = B->begin(), E = B->end();
64 I != E; ++I) {
65 if (CallInst* callInst = dyn_cast<CallInst>(&*I)) {
66 Function* Callee = callInst->getCalledFunction();
Rafael Espindolabb46f522009-01-15 20:18:42 +000067 if (Callee && Callee->hasLocalLinkage())
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000068 Callee->setLinkage(GlobalValue::ExternalLinkage);
69 }
70 }
71 }
72 }
73
74 NamedFunc->setLinkage(GlobalValue::ExternalLinkage);
75 NamedFunc->deleteBody();
76 assert(NamedFunc->isDeclaration() && "This didn't make the function external!");
77 } else {
78 if (!(*GI)->isDeclaration()) {
79 cast<GlobalVariable>(*GI)->setInitializer(0); //clear the initializer
80 (*GI)->setLinkage(GlobalValue::ExternalLinkage);
81 }
82 }
83 }
84 return true;
85 }
86
87 bool isolateGV(Module &M) {
88 // Mark all globals internal
Rafael Espindolabb46f522009-01-15 20:18:42 +000089 // FIXME: what should we do with private linkage?
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000090 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
91 if (!I->isDeclaration()) {
92 I->setLinkage(GlobalValue::InternalLinkage);
93 }
94 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
95 if (!I->isDeclaration()) {
96 I->setLinkage(GlobalValue::InternalLinkage);
97 }
98
99 // Make sure our result is globally accessible...
100 // by putting them in the used array
101 {
102 std::vector<Constant *> AUGs;
Owen Anderson1d0be152009-08-13 21:58:54 +0000103 const Type *SBP=
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000104 Type::getInt8PtrTy(M.getContext());
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000105 for (std::vector<GlobalValue*>::iterator GI = Named.begin(),
106 GE = Named.end(); GI != GE; ++GI) {
107 (*GI)->setLinkage(GlobalValue::ExternalLinkage);
Owen Andersonbaf3c402009-07-29 18:55:55 +0000108 AUGs.push_back(ConstantExpr::getBitCast(*GI, SBP));
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000109 }
Owen Andersondebcb012009-07-29 22:17:13 +0000110 ArrayType *AT = ArrayType::get(SBP, AUGs.size());
Owen Anderson1fd70962009-07-28 18:32:17 +0000111 Constant *Init = ConstantArray::get(AT, AUGs);
Owen Andersone9b11b42009-07-08 19:03:57 +0000112 GlobalValue *gv = new GlobalVariable(M, AT, false,
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000113 GlobalValue::AppendingLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000114 Init, "llvm.used");
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000115 gv->setSection("llvm.metadata");
116 }
117
118 // All of the functions may be used by global variables or the named
119 // globals. Loop through them and create a new, external functions that
120 // can be "used", instead of ones with bodies.
121 std::vector<Function*> NewFunctions;
122
123 Function *Last = --M.end(); // Figure out where the last real fn is.
124
125 for (Module::iterator I = M.begin(); ; ++I) {
126 if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) {
Gabor Greif051a9502008-04-06 20:25:17 +0000127 Function *New = Function::Create(I->getFunctionType(),
128 GlobalValue::ExternalLinkage);
Duncan Sands28c3cff2008-05-26 19:58:59 +0000129 New->copyAttributesFrom(I);
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000130
131 // If it's not the named function, delete the body of the function
132 I->dropAllReferences();
133
134 M.getFunctionList().push_back(New);
135 NewFunctions.push_back(New);
136 New->takeName(I);
137 }
138
139 if (&*I == Last) break; // Stop after processing the last function
140 }
141
142 // Now that we have replacements all set up, loop through the module,
143 // deleting the old functions, replacing them with the newly created
144 // functions.
145 if (!NewFunctions.empty()) {
146 unsigned FuncNum = 0;
147 Module::iterator I = M.begin();
148 do {
149 if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) {
150 // Make everything that uses the old function use the new dummy fn
151 I->replaceAllUsesWith(NewFunctions[FuncNum++]);
152
153 Function *Old = I;
154 ++I; // Move the iterator to the new function
155
156 // Delete the old function!
157 M.getFunctionList().erase(Old);
158
159 } else {
160 ++I; // Skip the function we are extracting
161 }
162 } while (&*I != NewFunctions[0]);
163 }
164
165 return true;
166 }
167 };
168
169 char GVExtractorPass::ID = 0;
170}
171
172ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs,
173 bool deleteFn, bool relinkCallees) {
174 return new GVExtractorPass(GVs, deleteFn, relinkCallees);
175}