blob: 901295d65a771b3740b1ac941a3c197b70a175be [file] [log] [blame]
Chris Lattner62b7fd12002-04-07 20:49:59 +00001//===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbd422e62001-11-26 18:42:17 +00009//
Chris Lattnerec6d7a62003-09-16 19:27:31 +000010// This transform is designed to eliminate unreachable internal globals from the
11// program. It uses an aggressive algorithm, searching out globals that are
12// known to be alive. After it finds all of the globals which are needed, it
13// deletes whatever is left over. This allows it to delete recursive chunks of
14// the program which are unreachable.
Chris Lattnerbd422e62001-11-26 18:42:17 +000015//
16//===----------------------------------------------------------------------===//
17
Chris Lattner1631bcb2006-12-19 22:09:18 +000018#define DEBUG_TYPE "globaldce"
Chris Lattner99a53f62002-07-24 17:12:05 +000019#include "llvm/Transforms/IPO.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Constants.h"
23#include "llvm/IR/Module.h"
Chris Lattnerec6d7a62003-09-16 19:27:31 +000024#include "llvm/Pass.h"
Chris Lattnerf52e03c2003-11-21 21:54:22 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Duncan Sands821d13c2009-01-05 20:39:50 +000027STATISTIC(NumAliases , "Number of global aliases removed");
Chris Lattner1631bcb2006-12-19 22:09:18 +000028STATISTIC(NumFunctions, "Number of functions removed");
29STATISTIC(NumVariables, "Number of global variables removed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000030
Chris Lattner1631bcb2006-12-19 22:09:18 +000031namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000032 struct GlobalDCE : public ModulePass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000033 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000034 GlobalDCE() : ModulePass(ID) {
35 initializeGlobalDCEPass(*PassRegistry::getPassRegistry());
36 }
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000037
Chris Lattner04805fa2002-02-26 21:46:54 +000038 // run - Do the GlobalDCE pass on the specified module, optionally updating
39 // the specified callgraph to reflect the changes.
40 //
Chris Lattner4f2cf032004-09-20 04:48:05 +000041 bool runOnModule(Module &M);
Chris Lattner8f3acc62002-08-18 01:28:30 +000042
43 private:
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +000044 SmallPtrSet<GlobalValue*, 32> AliveGlobals;
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +000045 SmallPtrSet<Constant *, 8> SeenConstants;
Chris Lattner8f3acc62002-08-18 01:28:30 +000046
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000047 /// GlobalIsNeeded - mark the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +000048 /// recursively mark anything that it uses as also needed.
49 void GlobalIsNeeded(GlobalValue *GV);
50 void MarkUsedGlobalsAsNeeded(Constant *C);
Chris Lattner8f3acc62002-08-18 01:28:30 +000051
Reid Spencer5f681592004-07-18 00:25:04 +000052 bool RemoveUnusedGlobalValue(GlobalValue &GV);
Chris Lattner04805fa2002-02-26 21:46:54 +000053 };
Chris Lattnerd5d56782002-01-31 00:45:11 +000054}
55
Dan Gohmand78c4002008-05-13 00:00:25 +000056char GlobalDCE::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000057INITIALIZE_PASS(GlobalDCE, "globaldce",
Owen Andersondf7a4f22010-10-07 22:25:06 +000058 "Dead Global Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000059
Chris Lattner4f2cf032004-09-20 04:48:05 +000060ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
Chris Lattner8f3acc62002-08-18 01:28:30 +000061
Chris Lattner4f2cf032004-09-20 04:48:05 +000062bool GlobalDCE::runOnModule(Module &M) {
Chris Lattner8f3acc62002-08-18 01:28:30 +000063 bool Changed = false;
Owen Anderson20b34ac2009-07-16 18:04:31 +000064
Chris Lattnerec6d7a62003-09-16 19:27:31 +000065 // Loop over the module, adding globals which are obviously necessary.
Chris Lattner8f3acc62002-08-18 01:28:30 +000066 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +000067 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerec6d7a62003-09-16 19:27:31 +000068 // Functions with external linkage are needed if they have a body
Rafael Espindoladef1b092012-06-14 22:48:13 +000069 if (!I->isDiscardableIfUnused() &&
Torok Edwin79963392009-05-23 14:06:57 +000070 !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
Chris Lattnerec6d7a62003-09-16 19:27:31 +000071 GlobalIsNeeded(I);
Chris Lattner8f3acc62002-08-18 01:28:30 +000072 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +000073
Anton Korobeynikova97b6942007-04-25 14:27:10 +000074 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
75 I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +000076 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerc8dfbbb2003-09-20 19:00:50 +000077 // Externally visible & appending globals are needed, if they have an
78 // initializer.
Rafael Espindoladef1b092012-06-14 22:48:13 +000079 if (!I->isDiscardableIfUnused() &&
Torok Edwin79963392009-05-23 14:06:57 +000080 !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
Chris Lattnerec6d7a62003-09-16 19:27:31 +000081 GlobalIsNeeded(I);
Chris Lattner8f3acc62002-08-18 01:28:30 +000082 }
83
Anton Korobeynikova97b6942007-04-25 14:27:10 +000084 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
85 I != E; ++I) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000086 Changed |= RemoveUnusedGlobalValue(*I);
87 // Externally visible aliases are needed.
Rafael Espindoladef1b092012-06-14 22:48:13 +000088 if (!I->isDiscardableIfUnused())
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000089 GlobalIsNeeded(I);
Anton Korobeynikova97b6942007-04-25 14:27:10 +000090 }
91
Chris Lattnerec6d7a62003-09-16 19:27:31 +000092 // Now that all globals which are needed are in the AliveGlobals set, we loop
93 // through the program, deleting those which are not alive.
Chris Lattner8f3acc62002-08-18 01:28:30 +000094 //
Chris Lattner8f3acc62002-08-18 01:28:30 +000095
Chris Lattnerec6d7a62003-09-16 19:27:31 +000096 // The first pass is to drop initializers of global variables which are dead.
97 std::vector<GlobalVariable*> DeadGlobalVars; // Keep track of dead globals
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +000098 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
99 I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000100 if (!AliveGlobals.count(I)) {
101 DeadGlobalVars.push_back(I); // Keep track of dead globals
102 I->setInitializer(0);
103 }
104
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000105 // The second pass drops the bodies of functions which are dead...
106 std::vector<Function*> DeadFunctions;
107 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
108 if (!AliveGlobals.count(I)) {
109 DeadFunctions.push_back(I); // Keep track of dead globals
Reid Spencer5301e7c2007-01-30 20:08:39 +0000110 if (!I->isDeclaration())
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000111 I->deleteBody();
112 }
113
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000114 // The third pass drops targets of aliases which are dead...
115 std::vector<GlobalAlias*> DeadAliases;
116 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
117 ++I)
118 if (!AliveGlobals.count(I)) {
119 DeadAliases.push_back(I);
120 I->setAliasee(0);
121 }
122
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000123 if (!DeadFunctions.empty()) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000124 // Now that all interferences have been dropped, delete the actual objects
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000125 // themselves.
126 for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000127 RemoveUnusedGlobalValue(*DeadFunctions[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000128 M.getFunctionList().erase(DeadFunctions[i]);
129 }
130 NumFunctions += DeadFunctions.size();
131 Changed = true;
Chris Lattner8f3acc62002-08-18 01:28:30 +0000132 }
133
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000134 if (!DeadGlobalVars.empty()) {
135 for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000136 RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000137 M.getGlobalList().erase(DeadGlobalVars[i]);
138 }
139 NumVariables += DeadGlobalVars.size();
140 Changed = true;
141 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000142
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000143 // Now delete any dead aliases.
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000144 if (!DeadAliases.empty()) {
145 for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
146 RemoveUnusedGlobalValue(*DeadAliases[i]);
147 M.getAliasList().erase(DeadAliases[i]);
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000148 }
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000149 NumAliases += DeadAliases.size();
150 Changed = true;
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000151 }
152
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000153 // Make sure that all memory is released
154 AliveGlobals.clear();
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000155 SeenConstants.clear();
Devang Patelc5aa8c62009-08-11 06:31:57 +0000156
Chris Lattner8f3acc62002-08-18 01:28:30 +0000157 return Changed;
158}
159
Devang Patel95b18122008-11-11 19:16:41 +0000160/// GlobalIsNeeded - the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000161/// recursively mark anything that it uses as also needed.
162void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000163 // If the global is already in the set, no need to reprocess it.
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000164 if (!AliveGlobals.insert(G))
165 return;
166
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000167 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
168 // If this is a global variable, we must make sure to add any global values
169 // referenced by the initializer to the alive set.
170 if (GV->hasInitializer())
171 MarkUsedGlobalsAsNeeded(GV->getInitializer());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000172 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
173 // The target of a global alias is needed.
Duncan Sands187c5712009-01-07 18:45:53 +0000174 MarkUsedGlobalsAsNeeded(GA->getAliasee());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000175 } else {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000176 // Otherwise this must be a function object. We have to scan the body of
177 // the function looking for constants and global values which are used as
178 // operands. Any operands of these types must be processed to ensure that
179 // any globals used will be marked as needed.
180 Function *F = cast<Function>(G);
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000181
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000182 if (F->hasPrefixData())
183 MarkUsedGlobalsAsNeeded(F->getPrefixData());
184
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000185 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000186 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000187 for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U)
188 if (GlobalValue *GV = dyn_cast<GlobalValue>(*U))
189 GlobalIsNeeded(GV);
190 else if (Constant *C = dyn_cast<Constant>(*U))
Misha Brukmanb1c93172005-04-21 23:48:37 +0000191 MarkUsedGlobalsAsNeeded(C);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000192 }
193}
194
195void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
Reid Spencer5f681592004-07-18 00:25:04 +0000196 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Chris Lattneree8b9512009-10-29 01:21:20 +0000197 return GlobalIsNeeded(GV);
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000198
Chris Lattneree8b9512009-10-29 01:21:20 +0000199 // Loop over all of the operands of the constant, adding any globals they
200 // use to the list of needed globals.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000201 for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) {
202 // If we've already processed this constant there's no need to do it again.
Benjamin Krameradc17272013-04-13 16:11:14 +0000203 Constant *Op = dyn_cast<Constant>(*I);
204 if (Op && SeenConstants.insert(Op))
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000205 MarkUsedGlobalsAsNeeded(Op);
206 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000207}
Chris Lattner8f3acc62002-08-18 01:28:30 +0000208
Reid Spencer5f681592004-07-18 00:25:04 +0000209// RemoveUnusedGlobalValue - Loop over all of the uses of the specified
Chris Lattner8f3acc62002-08-18 01:28:30 +0000210// GlobalValue, looking for the constant pointer ref that may be pointing to it.
211// If found, check to see if the constant pointer ref is safe to destroy, and if
212// so, nuke it. This will reduce the reference count on the global value, which
213// might make it deader.
214//
Reid Spencer5f681592004-07-18 00:25:04 +0000215bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
Chris Lattner9238d782004-07-18 07:22:58 +0000216 if (GV.use_empty()) return false;
217 GV.removeDeadConstantUsers();
218 return GV.use_empty();
219}