llvm.org GIT mirror llvm / b4e3cda
Rewrite ExtractGV, removing a bunch of stuff that didn't fully work, and was over-complicated, and replacing it with a simple implementation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112120 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
3 changed file(s) with 33 addition(s) and 131 deletion(s). Raw diff Collapse all Expand all
9292 /// possible, except for the global values specified.
9393 ///
9494 ModulePass *createGVExtractionPass(std::vector& GVs, bool
95 deleteFn = false,
96 bool relinkCallees = false);
95 deleteFn = false);
9796
9897 //===----------------------------------------------------------------------===//
9998 /// createFunctionInliningPass - Return a new pass object that uses a heuristic
1616 #include "llvm/Pass.h"
1717 #include "llvm/Constants.h"
1818 #include "llvm/Transforms/IPO.h"
19 #include "llvm/ADT/SetVector.h"
1920 #include
2021 using namespace llvm;
2122
2223 namespace {
2324 /// @brief A pass to extract specific functions and their dependencies.
2425 class GVExtractorPass : public ModulePass {
25 std::vector*> Named;
26 SetVector*> Named;
2627 bool deleteStuff;
27 bool reLink;
2828 public:
2929 static char ID; // Pass identification, replacement for typeid
3030
3232 /// specified function. Otherwise, it deletes as much of the module as
3333 /// possible, except for the function specified.
3434 ///
35 explicit GVExtractorPass(std::vector& GVs, bool deleteS = true,
36 bool relinkCallees = false)
37 : ModulePass(ID), Named(GVs), deleteStuff(deleteS),
38 reLink(relinkCallees) {}
35 explicit GVExtractorPass(std::vector& GVs, bool deleteS = true)
36 : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
3937
4038 bool runOnModule(Module &M) {
41 if (Named.size() == 0) {
42 return false; // Nothing to extract
43 }
44
45
46 if (deleteStuff)
47 return deleteGV();
48 M.setModuleInlineAsm("");
49 return isolateGV(M);
50 }
39 // Visit the global inline asm.
40 if (!deleteStuff)
41 M.setModuleInlineAsm("");
5142
52 bool deleteGV() {
53 for (std::vector::iterator GI = Named.begin(),
54 GE = Named.end(); GI != GE; ++GI) {
55 if (Function* NamedFunc = dyn_cast(*GI)) {
56 // If we're in relinking mode, set linkage of all internal callees to
57 // external. This will allow us extract function, and then - link
58 // everything together
59 if (reLink) {
60 for (Function::iterator B = NamedFunc->begin(), BE = NamedFunc->end();
61 B != BE; ++B) {
62 for (BasicBlock::iterator I = B->begin(), E = B->end();
63 I != E; ++I) {
64 if (CallInst* callInst = dyn_cast(&*I)) {
65 Function* Callee = callInst->getCalledFunction();
66 if (Callee && Callee->hasLocalLinkage())
67 Callee->setLinkage(GlobalValue::ExternalLinkage);
68 }
69 }
70 }
71 }
72
73 NamedFunc->setLinkage(GlobalValue::ExternalLinkage);
74 NamedFunc->deleteBody();
75 assert(NamedFunc->isDeclaration() && "This didn't make the function external!");
76 } else {
77 if (!(*GI)->isDeclaration()) {
78 cast(*GI)->setInitializer(0); //clear the initializer
79 (*GI)->setLinkage(GlobalValue::ExternalLinkage);
80 }
43 // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
44 // implementation could figure out which GlobalValues are actually
45 // referenced by the Named set, and which GlobalValues in the rest of
46 // the module are referenced by the NamedSet, and get away with leaving
47 // more internal and private things internal and private. But for now,
48 // be conservative and simple.
49
50 // Visit the GlobalVariables.
51 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
52 I != E; ++I)
53 if (!I->isDeclaration()) {
54 if (I->hasLocalLinkage())
55 I->setVisibility(GlobalValue::HiddenVisibility);
56 I->setLinkage(GlobalValue::ExternalLinkage);
57 if (deleteStuff == Named.count(I))
58 I->setInitializer(0);
8159 }
82 }
83 return true;
84 }
8560
86 bool isolateGV(Module &M) {
87 // Mark all globals internal
88 // FIXME: what should we do with private linkage?
89 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
90 if (!I->isDeclaration()) {
91 I->setLinkage(GlobalValue::InternalLinkage);
92 }
61 // Visit the Functions.
9362 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
9463 if (!I->isDeclaration()) {
95 I->setLinkage(GlobalValue::InternalLinkage);
64 if (I->hasLocalLinkage())
65 I->setVisibility(GlobalValue::HiddenVisibility);
66 I->setLinkage(GlobalValue::ExternalLinkage);
67 if (deleteStuff == Named.count(I))
68 I->deleteBody();
9669 }
97
98 // Make sure our result is globally accessible...
99 // by putting them in the used array
100 {
101 std::vector AUGs;
102 const Type *SBP=
103 Type::getInt8PtrTy(M.getContext());
104 for (std::vector::iterator GI = Named.begin(),
105 GE = Named.end(); GI != GE; ++GI) {
106 (*GI)->setLinkage(GlobalValue::ExternalLinkage);
107 AUGs.push_back(ConstantExpr::getBitCast(*GI, SBP));
108 }
109 ArrayType *AT = ArrayType::get(SBP, AUGs.size());
110 Constant *Init = ConstantArray::get(AT, AUGs);
111 GlobalValue *gv = new GlobalVariable(M, AT, false,
112 GlobalValue::AppendingLinkage,
113 Init, "llvm.used");
114 gv->setSection("llvm.metadata");
115 }
116
117 // All of the functions may be used by global variables or the named
118 // globals. Loop through them and create a new, external functions that
119 // can be "used", instead of ones with bodies.
120 std::vector NewFunctions;
121
122 Function *Last = --M.end(); // Figure out where the last real fn is.
123
124 for (Module::iterator I = M.begin(); ; ++I) {
125 if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) {
126 Function *New = Function::Create(I->getFunctionType(),
127 GlobalValue::ExternalLinkage);
128 New->copyAttributesFrom(I);
129
130 // If it's not the named function, delete the body of the function
131 I->dropAllReferences();
132
133 M.getFunctionList().push_back(New);
134 NewFunctions.push_back(New);
135 New->takeName(I);
136 }
137
138 if (&*I == Last) break; // Stop after processing the last function
139 }
140
141 // Now that we have replacements all set up, loop through the module,
142 // deleting the old functions, replacing them with the newly created
143 // functions.
144 if (!NewFunctions.empty()) {
145 unsigned FuncNum = 0;
146 Module::iterator I = M.begin();
147 do {
148 if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) {
149 // Make everything that uses the old function use the new dummy fn
150 I->replaceAllUsesWith(NewFunctions[FuncNum++]);
151
152 Function *Old = I;
153 ++I; // Move the iterator to the new function
154
155 // Delete the old function!
156 M.getFunctionList().erase(Old);
157
158 } else {
159 ++I; // Skip the function we are extracting
160 }
161 } while (&*I != NewFunctions[0]);
162 }
16370
16471 return true;
16572 }
16976 }
17077
17178 ModulePass *llvm::createGVExtractionPass(std::vector& GVs,
172 bool deleteFn, bool relinkCallees) {
173 return new GVExtractorPass(GVs, deleteFn, relinkCallees);
79 bool deleteFn) {
80 return new GVExtractorPass(GVs, deleteFn);
17481 }
4242
4343 static cl::opt
4444 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
45
46 static cl::opt
47 Relink("relink",
48 cl::desc("Turn external linkage for callees of function to delete"));
4945
5046 // ExtractFuncs - The functions to extract from the module...
5147 static cl::list
121117 PassManager Passes;
122118 Passes.add(new TargetData(M.get())); // Use correct TargetData
123119
124 Passes.add(createGVExtractionPass(GVs, DeleteFn, Relink));
120 Passes.add(createGVExtractionPass(GVs, DeleteFn));
125121 if (!DeleteFn)
126122 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
127123 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info