annotate lib/Transforms/IPO/GlobalDCE.cpp @ 128:c347d3398279 default tip

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