blob: 0cf03a54c2244a6d3c5f5230d017173f69c12bc9 [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 Lattner99a53f62002-07-24 17:12:05 +000018#include "llvm/Transforms/IPO.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/Module.h"
Chris Lattnerec6d7a62003-09-16 19:27:31 +000023#include "llvm/Pass.h"
Chris Lattnerf52e03c2003-11-21 21:54:22 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chandler Carruth964daaa2014-04-22 02:55:47 +000026#define DEBUG_TYPE "globaldce"
27
Duncan Sands821d13c2009-01-05 20:39:50 +000028STATISTIC(NumAliases , "Number of global aliases removed");
Chris Lattner1631bcb2006-12-19 22:09:18 +000029STATISTIC(NumFunctions, "Number of functions removed");
30STATISTIC(NumVariables, "Number of global variables removed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000031
Chris Lattner1631bcb2006-12-19 22:09:18 +000032namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000033 struct GlobalDCE : public ModulePass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000034 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000035 GlobalDCE() : ModulePass(ID) {
36 initializeGlobalDCEPass(*PassRegistry::getPassRegistry());
37 }
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000038
Chris Lattner04805fa2002-02-26 21:46:54 +000039 // run - Do the GlobalDCE pass on the specified module, optionally updating
40 // the specified callgraph to reflect the changes.
41 //
Craig Topper3e4c6972014-03-05 09:10:37 +000042 bool runOnModule(Module &M) override;
Chris Lattner8f3acc62002-08-18 01:28:30 +000043
44 private:
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +000045 SmallPtrSet<GlobalValue*, 32> AliveGlobals;
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +000046 SmallPtrSet<Constant *, 8> SeenConstants;
Chris Lattner8f3acc62002-08-18 01:28:30 +000047
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000048 /// GlobalIsNeeded - mark the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +000049 /// recursively mark anything that it uses as also needed.
50 void GlobalIsNeeded(GlobalValue *GV);
51 void MarkUsedGlobalsAsNeeded(Constant *C);
Chris Lattner8f3acc62002-08-18 01:28:30 +000052
Reid Spencer5f681592004-07-18 00:25:04 +000053 bool RemoveUnusedGlobalValue(GlobalValue &GV);
Chris Lattner04805fa2002-02-26 21:46:54 +000054 };
Chris Lattnerd5d56782002-01-31 00:45:11 +000055}
56
Dan Gohmand78c4002008-05-13 00:00:25 +000057char GlobalDCE::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000058INITIALIZE_PASS(GlobalDCE, "globaldce",
Owen Andersondf7a4f22010-10-07 22:25:06 +000059 "Dead Global Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000060
Chris Lattner4f2cf032004-09-20 04:48:05 +000061ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
Chris Lattner8f3acc62002-08-18 01:28:30 +000062
Chris Lattner4f2cf032004-09-20 04:48:05 +000063bool GlobalDCE::runOnModule(Module &M) {
Chris Lattner8f3acc62002-08-18 01:28:30 +000064 bool Changed = false;
Owen Anderson20b34ac2009-07-16 18:04:31 +000065
Chris Lattnerec6d7a62003-09-16 19:27:31 +000066 // Loop over the module, adding globals which are obviously necessary.
Chris Lattner8f3acc62002-08-18 01:28:30 +000067 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +000068 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerec6d7a62003-09-16 19:27:31 +000069 // Functions with external linkage are needed if they have a body
Rafael Espindoladef1b092012-06-14 22:48:13 +000070 if (!I->isDiscardableIfUnused() &&
Torok Edwin79963392009-05-23 14:06:57 +000071 !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
Chris Lattnerec6d7a62003-09-16 19:27:31 +000072 GlobalIsNeeded(I);
Chris Lattner8f3acc62002-08-18 01:28:30 +000073 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +000074
Anton Korobeynikova97b6942007-04-25 14:27:10 +000075 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
76 I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +000077 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerc8dfbbb2003-09-20 19:00:50 +000078 // Externally visible & appending globals are needed, if they have an
79 // initializer.
Rafael Espindoladef1b092012-06-14 22:48:13 +000080 if (!I->isDiscardableIfUnused() &&
Torok Edwin79963392009-05-23 14:06:57 +000081 !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
Chris Lattnerec6d7a62003-09-16 19:27:31 +000082 GlobalIsNeeded(I);
Chris Lattner8f3acc62002-08-18 01:28:30 +000083 }
84
Anton Korobeynikova97b6942007-04-25 14:27:10 +000085 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
86 I != E; ++I) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000087 Changed |= RemoveUnusedGlobalValue(*I);
88 // Externally visible aliases are needed.
Rafael Espindoladef1b092012-06-14 22:48:13 +000089 if (!I->isDiscardableIfUnused())
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000090 GlobalIsNeeded(I);
Anton Korobeynikova97b6942007-04-25 14:27:10 +000091 }
92
Chris Lattnerec6d7a62003-09-16 19:27:31 +000093 // Now that all globals which are needed are in the AliveGlobals set, we loop
94 // through the program, deleting those which are not alive.
Chris Lattner8f3acc62002-08-18 01:28:30 +000095 //
Chris Lattner8f3acc62002-08-18 01:28:30 +000096
Chris Lattnerec6d7a62003-09-16 19:27:31 +000097 // The first pass is to drop initializers of global variables which are dead.
98 std::vector<GlobalVariable*> DeadGlobalVars; // Keep track of dead globals
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +000099 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
100 I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000101 if (!AliveGlobals.count(I)) {
102 DeadGlobalVars.push_back(I); // Keep track of dead globals
Craig Topperf40110f2014-04-25 05:29:35 +0000103 I->setInitializer(nullptr);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000104 }
105
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000106 // The second pass drops the bodies of functions which are dead...
107 std::vector<Function*> DeadFunctions;
108 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
109 if (!AliveGlobals.count(I)) {
110 DeadFunctions.push_back(I); // Keep track of dead globals
Reid Spencer5301e7c2007-01-30 20:08:39 +0000111 if (!I->isDeclaration())
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000112 I->deleteBody();
113 }
114
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000115 // The third pass drops targets of aliases which are dead...
116 std::vector<GlobalAlias*> DeadAliases;
117 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
118 ++I)
119 if (!AliveGlobals.count(I)) {
120 DeadAliases.push_back(I);
Craig Topperf40110f2014-04-25 05:29:35 +0000121 I->setAliasee(nullptr);
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000122 }
123
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000124 if (!DeadFunctions.empty()) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000125 // Now that all interferences have been dropped, delete the actual objects
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000126 // themselves.
127 for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000128 RemoveUnusedGlobalValue(*DeadFunctions[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000129 M.getFunctionList().erase(DeadFunctions[i]);
130 }
131 NumFunctions += DeadFunctions.size();
132 Changed = true;
Chris Lattner8f3acc62002-08-18 01:28:30 +0000133 }
134
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000135 if (!DeadGlobalVars.empty()) {
136 for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000137 RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000138 M.getGlobalList().erase(DeadGlobalVars[i]);
139 }
140 NumVariables += DeadGlobalVars.size();
141 Changed = true;
142 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000143
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000144 // Now delete any dead aliases.
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000145 if (!DeadAliases.empty()) {
146 for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
147 RemoveUnusedGlobalValue(*DeadAliases[i]);
148 M.getAliasList().erase(DeadAliases[i]);
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000149 }
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000150 NumAliases += DeadAliases.size();
151 Changed = true;
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000152 }
153
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000154 // Make sure that all memory is released
155 AliveGlobals.clear();
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000156 SeenConstants.clear();
Devang Patelc5aa8c62009-08-11 06:31:57 +0000157
Chris Lattner8f3acc62002-08-18 01:28:30 +0000158 return Changed;
159}
160
Devang Patel95b18122008-11-11 19:16:41 +0000161/// GlobalIsNeeded - the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000162/// recursively mark anything that it uses as also needed.
163void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000164 // If the global is already in the set, no need to reprocess it.
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000165 if (!AliveGlobals.insert(G))
166 return;
167
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000168 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
169 // If this is a global variable, we must make sure to add any global values
170 // referenced by the initializer to the alive set.
171 if (GV->hasInitializer())
172 MarkUsedGlobalsAsNeeded(GV->getInitializer());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000173 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
174 // The target of a global alias is needed.
Duncan Sands187c5712009-01-07 18:45:53 +0000175 MarkUsedGlobalsAsNeeded(GA->getAliasee());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000176 } else {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000177 // Otherwise this must be a function object. We have to scan the body of
178 // the function looking for constants and global values which are used as
179 // operands. Any operands of these types must be processed to ensure that
180 // any globals used will be marked as needed.
181 Function *F = cast<Function>(G);
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000182
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000183 if (F->hasPrefixData())
184 MarkUsedGlobalsAsNeeded(F->getPrefixData());
185
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000186 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000187 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000188 for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U)
189 if (GlobalValue *GV = dyn_cast<GlobalValue>(*U))
190 GlobalIsNeeded(GV);
191 else if (Constant *C = dyn_cast<Constant>(*U))
Misha Brukmanb1c93172005-04-21 23:48:37 +0000192 MarkUsedGlobalsAsNeeded(C);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000193 }
194}
195
196void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
Reid Spencer5f681592004-07-18 00:25:04 +0000197 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Chris Lattneree8b9512009-10-29 01:21:20 +0000198 return GlobalIsNeeded(GV);
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000199
Chris Lattneree8b9512009-10-29 01:21:20 +0000200 // Loop over all of the operands of the constant, adding any globals they
201 // use to the list of needed globals.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000202 for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) {
203 // If we've already processed this constant there's no need to do it again.
Benjamin Krameradc17272013-04-13 16:11:14 +0000204 Constant *Op = dyn_cast<Constant>(*I);
205 if (Op && SeenConstants.insert(Op))
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000206 MarkUsedGlobalsAsNeeded(Op);
207 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000208}
Chris Lattner8f3acc62002-08-18 01:28:30 +0000209
Reid Spencer5f681592004-07-18 00:25:04 +0000210// RemoveUnusedGlobalValue - Loop over all of the uses of the specified
Chris Lattner8f3acc62002-08-18 01:28:30 +0000211// GlobalValue, looking for the constant pointer ref that may be pointing to it.
212// If found, check to see if the constant pointer ref is safe to destroy, and if
213// so, nuke it. This will reduce the reference count on the global value, which
214// might make it deader.
215//
Reid Spencer5f681592004-07-18 00:25:04 +0000216bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
Chris Lattner9238d782004-07-18 07:22:58 +0000217 if (GV.use_empty()) return false;
218 GV.removeDeadConstantUsers();
219 return GV.use_empty();
220}