llvm.org GIT mirror llvm / 1da5f2d
Delete unused global aliases with internal linkage. In fact this also deletes those with linkonce linkage, however this is currently dead because for the moment aliases aren't allowed to have this linkage type. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61742 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 10 years ago
2 changed file(s) with 31 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
2424 #include
2525 using namespace llvm;
2626
27 STATISTIC(NumAliases, "Number of global aliases removed");
2728 STATISTIC(NumFunctions, "Number of functions removed");
2829 STATISTIC(NumVariables, "Number of global variables removed");
2930
3132 struct VISIBILITY_HIDDEN GlobalDCE : public ModulePass {
3233 static char ID; // Pass identification, replacement for typeid
3334 GlobalDCE() : ModulePass(&ID) {}
34
35
3536 // run - Do the GlobalDCE pass on the specified module, optionally updating
3637 // the specified callgraph to reflect the changes.
3738 //
4041 private:
4142 std::set AliveGlobals;
4243
43 /// GlobalIsNeeded - the specific global value as needed, and
44 /// GlobalIsNeeded - mark the specific global value as needed, and
4445 /// recursively mark anything that it uses as also needed.
4546 void GlobalIsNeeded(GlobalValue *GV);
4647 void MarkUsedGlobalsAsNeeded(Constant *C);
7677 GlobalIsNeeded(I);
7778 }
7879
79
8080 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
8181 I != E; ++I) {
82 // Aliases are always needed even if they are not used.
83 MarkUsedGlobalsAsNeeded(I->getAliasee());
82 Changed |= RemoveUnusedGlobalValue(*I);
83 // Externally visible aliases are needed.
84 if (!I->hasInternalLinkage() && !I->hasLinkOnceLinkage())
85 GlobalIsNeeded(I);
8486 }
8587
8688 // Now that all globals which are needed are in the AliveGlobals set, we loop
9496 DeadGlobalVars.push_back(I); // Keep track of dead globals
9597 I->setInitializer(0);
9698 }
97
9899
99100 // The second pass drops the bodies of functions which are dead...
100101 std::vector DeadFunctions;
106107 }
107108
108109 if (!DeadFunctions.empty()) {
109 // Now that all interreferences have been dropped, delete the actual objects
110 // Now that all interferences have been dropped, delete the actual objects
110111 // themselves.
111112 for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
112113 RemoveUnusedGlobalValue(*DeadFunctions[i]);
123124 }
124125 NumVariables += DeadGlobalVars.size();
125126 Changed = true;
127 }
128
129 // Now delete any dead aliases.
130 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;) {
131 Module::alias_iterator J = I++;
132 if (!AliveGlobals.count(J)) {
133 RemoveUnusedGlobalValue(*J);
134 M.getAliasList().erase(J);
135 ++NumAliases;
136 Changed = true;
137 }
126138 }
127139
128140 // Make sure that all memory is released
146158 // referenced by the initializer to the alive set.
147159 if (GV->hasInitializer())
148160 MarkUsedGlobalsAsNeeded(GV->getInitializer());
149 } else if (!isa(G)) {
161 } else if (GlobalAlias *GA = dyn_cast(G)) {
162 // The target of a global alias is needed.
163 MarkUsedGlobalsAsNeeded(GA->getAliasee());
164 } else {
150165 // Otherwise this must be a function object. We have to scan the body of
151166 // the function looking for constants and global values which are used as
152167 // operands. Any operands of these types must be processed to ensure that
0 ; RUN: llvm-as < %s | opt -globaldce | llvm-dis | not grep @D
1 ; RUN: llvm-as < %s | opt -globaldce | llvm-dis | grep @L | count 3
2
3 @A = global i32 0
4 @D = alias internal i32* @A
5 @L1 = alias i32* @A
6 @L2 = alias internal i32* @L1
7 @L3 = alias i32* @L2