This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
forked from luqmana/llvm
-
Notifications
You must be signed in to change notification settings - Fork 63
/
GlobalDCE.cpp
293 lines (253 loc) · 9.99 KB
/
GlobalDCE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This transform is designed to eliminate unreachable internal globals from the
// program. It uses an aggressive algorithm, searching out globals that are
// known to be alive. After it finds all of the globals which are needed, it
// deletes whatever is left over. This allows it to delete recursive chunks of
// the program which are unreachable.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/IPO/GlobalDCE.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Utils/CtorUtils.h"
#include "llvm/Transforms/Utils/GlobalStatus.h"
using namespace llvm;
#define DEBUG_TYPE "globaldce"
STATISTIC(NumAliases , "Number of global aliases removed");
STATISTIC(NumFunctions, "Number of functions removed");
STATISTIC(NumIFuncs, "Number of indirect functions removed");
STATISTIC(NumVariables, "Number of global variables removed");
namespace {
class GlobalDCELegacyPass : public ModulePass {
public:
static char ID; // Pass identification, replacement for typeid
GlobalDCELegacyPass() : ModulePass(ID) {
initializeGlobalDCELegacyPassPass(*PassRegistry::getPassRegistry());
}
// run - Do the GlobalDCE pass on the specified module, optionally updating
// the specified callgraph to reflect the changes.
//
bool runOnModule(Module &M) override {
if (skipModule(M))
return false;
// We need a minimally functional dummy module analysis manager. It needs
// to at least know about the possibility of proxying a function analysis
// manager.
FunctionAnalysisManager DummyFAM;
ModuleAnalysisManager DummyMAM;
DummyMAM.registerPass(
[&] { return FunctionAnalysisManagerModuleProxy(DummyFAM); });
auto PA = Impl.run(M, DummyMAM);
return !PA.areAllPreserved();
}
private:
GlobalDCEPass Impl;
};
}
char GlobalDCELegacyPass::ID = 0;
INITIALIZE_PASS(GlobalDCELegacyPass, "globaldce",
"Dead Global Elimination", false, false)
// Public interface to the GlobalDCEPass.
ModulePass *llvm::createGlobalDCEPass() {
return new GlobalDCELegacyPass();
}
/// Returns true if F contains only a single "ret" instruction.
static bool isEmptyFunction(Function *F) {
BasicBlock &Entry = F->getEntryBlock();
if (Entry.size() != 1 || !isa<ReturnInst>(Entry.front()))
return false;
ReturnInst &RI = cast<ReturnInst>(Entry.front());
return RI.getReturnValue() == nullptr;
}
/// Compute the set of GlobalValue that depends from V.
/// The recursion stops as soon as a GlobalValue is met.
void GlobalDCEPass::ComputeDependencies(Value *V,
SmallPtrSetImpl<GlobalValue *> &Deps) {
if (auto *I = dyn_cast<Instruction>(V)) {
Function *Parent = I->getParent()->getParent();
Deps.insert(Parent);
} else if (auto *GV = dyn_cast<GlobalValue>(V)) {
Deps.insert(GV);
} else if (auto *CE = dyn_cast<Constant>(V)) {
// Avoid walking the whole tree of a big ConstantExprs multiple times.
auto Where = ConstantDependenciesCache.find(CE);
if (Where != ConstantDependenciesCache.end()) {
auto const &K = Where->second;
Deps.insert(K.begin(), K.end());
} else {
SmallPtrSetImpl<GlobalValue *> &LocalDeps = ConstantDependenciesCache[CE];
for (User *CEUser : CE->users())
ComputeDependencies(CEUser, LocalDeps);
Deps.insert(LocalDeps.begin(), LocalDeps.end());
}
}
}
void GlobalDCEPass::UpdateGVDependencies(GlobalValue &GV) {
SmallPtrSet<GlobalValue *, 8> Deps;
for (User *User : GV.users())
ComputeDependencies(User, Deps);
Deps.erase(&GV); // Remove self-reference.
for (GlobalValue *GVU : Deps) {
GVDependencies[GVU].insert(&GV);
}
}
/// Mark Global value as Live
void GlobalDCEPass::MarkLive(GlobalValue &GV,
SmallVectorImpl<GlobalValue *> *Updates) {
auto const Ret = AliveGlobals.insert(&GV);
if (!Ret.second)
return;
if (Updates)
Updates->push_back(&GV);
if (Comdat *C = GV.getComdat()) {
for (auto &&CM : make_range(ComdatMembers.equal_range(C)))
MarkLive(*CM.second, Updates); // Recursion depth is only two because only
// globals in the same comdat are visited.
}
}
PreservedAnalyses GlobalDCEPass::run(Module &M, ModuleAnalysisManager &MAM) {
bool Changed = false;
// The algorithm first computes the set L of global variables that are
// trivially live. Then it walks the initialization of these variables to
// compute the globals used to initialize them, which effectively builds a
// directed graph where nodes are global variables, and an edge from A to B
// means B is used to initialize A. Finally, it propagates the liveness
// information through the graph starting from the nodes in L. Nodes note
// marked as alive are discarded.
// Remove empty functions from the global ctors list.
Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
// Collect the set of members for each comdat.
for (Function &F : M)
if (Comdat *C = F.getComdat())
ComdatMembers.insert(std::make_pair(C, &F));
for (GlobalVariable &GV : M.globals())
if (Comdat *C = GV.getComdat())
ComdatMembers.insert(std::make_pair(C, &GV));
for (GlobalAlias &GA : M.aliases())
if (Comdat *C = GA.getComdat())
ComdatMembers.insert(std::make_pair(C, &GA));
// Loop over the module, adding globals which are obviously necessary.
for (GlobalObject &GO : M.global_objects()) {
Changed |= RemoveUnusedGlobalValue(GO);
// Functions with external linkage are needed if they have a body.
// Externally visible & appending globals are needed, if they have an
// initializer.
if (!GO.isDeclaration() && !GO.hasAvailableExternallyLinkage())
if (!GO.isDiscardableIfUnused())
MarkLive(GO);
UpdateGVDependencies(GO);
}
// Compute direct dependencies of aliases.
for (GlobalAlias &GA : M.aliases()) {
Changed |= RemoveUnusedGlobalValue(GA);
// Externally visible aliases are needed.
if (!GA.isDiscardableIfUnused())
MarkLive(GA);
UpdateGVDependencies(GA);
}
// Compute direct dependencies of ifuncs.
for (GlobalIFunc &GIF : M.ifuncs()) {
Changed |= RemoveUnusedGlobalValue(GIF);
// Externally visible ifuncs are needed.
if (!GIF.isDiscardableIfUnused())
MarkLive(GIF);
UpdateGVDependencies(GIF);
}
// Propagate liveness from collected Global Values through the computed
// dependencies.
SmallVector<GlobalValue *, 8> NewLiveGVs{AliveGlobals.begin(),
AliveGlobals.end()};
while (!NewLiveGVs.empty()) {
GlobalValue *LGV = NewLiveGVs.pop_back_val();
for (auto *GVD : GVDependencies[LGV])
MarkLive(*GVD, &NewLiveGVs);
}
// Now that all globals which are needed are in the AliveGlobals set, we loop
// through the program, deleting those which are not alive.
//
// The first pass is to drop initializers of global variables which are dead.
std::vector<GlobalVariable *> DeadGlobalVars; // Keep track of dead globals
for (GlobalVariable &GV : M.globals())
if (!AliveGlobals.count(&GV)) {
DeadGlobalVars.push_back(&GV); // Keep track of dead globals
if (GV.hasInitializer()) {
Constant *Init = GV.getInitializer();
GV.setInitializer(nullptr);
if (isSafeToDestroyConstant(Init))
Init->destroyConstant();
}
}
// The second pass drops the bodies of functions which are dead...
std::vector<Function *> DeadFunctions;
for (Function &F : M)
if (!AliveGlobals.count(&F)) {
DeadFunctions.push_back(&F); // Keep track of dead globals
if (!F.isDeclaration())
F.deleteBody();
}
// The third pass drops targets of aliases which are dead...
std::vector<GlobalAlias*> DeadAliases;
for (GlobalAlias &GA : M.aliases())
if (!AliveGlobals.count(&GA)) {
DeadAliases.push_back(&GA);
GA.setAliasee(nullptr);
}
// The fourth pass drops targets of ifuncs which are dead...
std::vector<GlobalIFunc*> DeadIFuncs;
for (GlobalIFunc &GIF : M.ifuncs())
if (!AliveGlobals.count(&GIF)) {
DeadIFuncs.push_back(&GIF);
GIF.setResolver(nullptr);
}
// Now that all interferences have been dropped, delete the actual objects
// themselves.
auto EraseUnusedGlobalValue = [&](GlobalValue *GV) {
RemoveUnusedGlobalValue(*GV);
GV->eraseFromParent();
Changed = true;
};
NumFunctions += DeadFunctions.size();
for (Function *F : DeadFunctions)
EraseUnusedGlobalValue(F);
NumVariables += DeadGlobalVars.size();
for (GlobalVariable *GV : DeadGlobalVars)
EraseUnusedGlobalValue(GV);
NumAliases += DeadAliases.size();
for (GlobalAlias *GA : DeadAliases)
EraseUnusedGlobalValue(GA);
NumIFuncs += DeadIFuncs.size();
for (GlobalIFunc *GIF : DeadIFuncs)
EraseUnusedGlobalValue(GIF);
// Make sure that all memory is released
AliveGlobals.clear();
ConstantDependenciesCache.clear();
GVDependencies.clear();
ComdatMembers.clear();
if (Changed)
return PreservedAnalyses::none();
return PreservedAnalyses::all();
}
// RemoveUnusedGlobalValue - Loop over all of the uses of the specified
// GlobalValue, looking for the constant pointer ref that may be pointing to it.
// If found, check to see if the constant pointer ref is safe to destroy, and if
// so, nuke it. This will reduce the reference count on the global value, which
// might make it deader.
//
bool GlobalDCEPass::RemoveUnusedGlobalValue(GlobalValue &GV) {
if (GV.use_empty())
return false;
GV.removeDeadConstantUsers();
return GV.use_empty();
}