blob: 03b17d5238631532f2c5fef8b53a1f2f6ce03fc2 [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"
Nico Weber4b2acde2014-05-02 18:35:25 +000022#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
Nico Weber4b2acde2014-05-02 18:35:25 +000024#include "llvm/Transforms/Utils/CtorUtils.h"
Chris Lattnerec6d7a62003-09-16 19:27:31 +000025#include "llvm/Pass.h"
Chris Lattnerf52e03c2003-11-21 21:54:22 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chandler Carruth964daaa2014-04-22 02:55:47 +000028#define DEBUG_TYPE "globaldce"
29
Duncan Sands821d13c2009-01-05 20:39:50 +000030STATISTIC(NumAliases , "Number of global aliases removed");
Chris Lattner1631bcb2006-12-19 22:09:18 +000031STATISTIC(NumFunctions, "Number of functions removed");
32STATISTIC(NumVariables, "Number of global variables removed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033
Chris Lattner1631bcb2006-12-19 22:09:18 +000034namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000035 struct GlobalDCE : public ModulePass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000036 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000037 GlobalDCE() : ModulePass(ID) {
38 initializeGlobalDCEPass(*PassRegistry::getPassRegistry());
39 }
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000040
Chris Lattner04805fa2002-02-26 21:46:54 +000041 // run - Do the GlobalDCE pass on the specified module, optionally updating
42 // the specified callgraph to reflect the changes.
43 //
Craig Topper3e4c6972014-03-05 09:10:37 +000044 bool runOnModule(Module &M) override;
Chris Lattner8f3acc62002-08-18 01:28:30 +000045
46 private:
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +000047 SmallPtrSet<GlobalValue*, 32> AliveGlobals;
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +000048 SmallPtrSet<Constant *, 8> SeenConstants;
Chris Lattner8f3acc62002-08-18 01:28:30 +000049
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000050 /// GlobalIsNeeded - mark the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +000051 /// recursively mark anything that it uses as also needed.
52 void GlobalIsNeeded(GlobalValue *GV);
53 void MarkUsedGlobalsAsNeeded(Constant *C);
Chris Lattner8f3acc62002-08-18 01:28:30 +000054
Reid Spencer5f681592004-07-18 00:25:04 +000055 bool RemoveUnusedGlobalValue(GlobalValue &GV);
Chris Lattner04805fa2002-02-26 21:46:54 +000056 };
Nico Weber4b2acde2014-05-02 18:35:25 +000057
58/// Returns true if F contains only a single "ret" instruction.
59bool isEmptyFunction(void *Context, Function *F) {
60 BasicBlock &Entry = F->getEntryBlock();
61 if (Entry.size() != 1 || !isa<ReturnInst>(Entry.front()))
62 return false;
63 ReturnInst &RI = cast<ReturnInst>(Entry.front());
64 return RI.getReturnValue() == NULL;
65}
Chris Lattnerd5d56782002-01-31 00:45:11 +000066}
67
Dan Gohmand78c4002008-05-13 00:00:25 +000068char GlobalDCE::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000069INITIALIZE_PASS(GlobalDCE, "globaldce",
Owen Andersondf7a4f22010-10-07 22:25:06 +000070 "Dead Global Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000071
Chris Lattner4f2cf032004-09-20 04:48:05 +000072ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
Chris Lattner8f3acc62002-08-18 01:28:30 +000073
Chris Lattner4f2cf032004-09-20 04:48:05 +000074bool GlobalDCE::runOnModule(Module &M) {
Chris Lattner8f3acc62002-08-18 01:28:30 +000075 bool Changed = false;
Nico Weber4b2acde2014-05-02 18:35:25 +000076
77 // Remove empty functions from the global ctors list.
78 Changed |= optimizeGlobalCtorsList(M, isEmptyFunction, nullptr);
79
Chris Lattnerec6d7a62003-09-16 19:27:31 +000080 // Loop over the module, adding globals which are obviously necessary.
Chris Lattner8f3acc62002-08-18 01:28:30 +000081 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +000082 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerec6d7a62003-09-16 19:27:31 +000083 // Functions with external linkage are needed if they have a body
Rafael Espindoladef1b092012-06-14 22:48:13 +000084 if (!I->isDiscardableIfUnused() &&
Torok Edwin79963392009-05-23 14:06:57 +000085 !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
Chris Lattnerec6d7a62003-09-16 19:27:31 +000086 GlobalIsNeeded(I);
Chris Lattner8f3acc62002-08-18 01:28:30 +000087 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +000088
Anton Korobeynikova97b6942007-04-25 14:27:10 +000089 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
90 I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +000091 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerc8dfbbb2003-09-20 19:00:50 +000092 // Externally visible & appending globals are needed, if they have an
93 // initializer.
Rafael Espindoladef1b092012-06-14 22:48:13 +000094 if (!I->isDiscardableIfUnused() &&
Torok Edwin79963392009-05-23 14:06:57 +000095 !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
Chris Lattnerec6d7a62003-09-16 19:27:31 +000096 GlobalIsNeeded(I);
Chris Lattner8f3acc62002-08-18 01:28:30 +000097 }
98
Anton Korobeynikova97b6942007-04-25 14:27:10 +000099 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
100 I != E; ++I) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000101 Changed |= RemoveUnusedGlobalValue(*I);
102 // Externally visible aliases are needed.
Rafael Espindoladef1b092012-06-14 22:48:13 +0000103 if (!I->isDiscardableIfUnused())
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000104 GlobalIsNeeded(I);
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000105 }
106
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000107 // Now that all globals which are needed are in the AliveGlobals set, we loop
108 // through the program, deleting those which are not alive.
Chris Lattner8f3acc62002-08-18 01:28:30 +0000109 //
Chris Lattner8f3acc62002-08-18 01:28:30 +0000110
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000111 // The first pass is to drop initializers of global variables which are dead.
112 std::vector<GlobalVariable*> DeadGlobalVars; // Keep track of dead globals
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000113 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
114 I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000115 if (!AliveGlobals.count(I)) {
116 DeadGlobalVars.push_back(I); // Keep track of dead globals
Craig Topperf40110f2014-04-25 05:29:35 +0000117 I->setInitializer(nullptr);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000118 }
119
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000120 // The second pass drops the bodies of functions which are dead...
121 std::vector<Function*> DeadFunctions;
122 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
123 if (!AliveGlobals.count(I)) {
124 DeadFunctions.push_back(I); // Keep track of dead globals
Reid Spencer5301e7c2007-01-30 20:08:39 +0000125 if (!I->isDeclaration())
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000126 I->deleteBody();
127 }
128
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000129 // The third pass drops targets of aliases which are dead...
130 std::vector<GlobalAlias*> DeadAliases;
131 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
132 ++I)
133 if (!AliveGlobals.count(I)) {
134 DeadAliases.push_back(I);
Craig Topperf40110f2014-04-25 05:29:35 +0000135 I->setAliasee(nullptr);
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000136 }
137
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000138 if (!DeadFunctions.empty()) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000139 // Now that all interferences have been dropped, delete the actual objects
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000140 // themselves.
141 for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000142 RemoveUnusedGlobalValue(*DeadFunctions[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000143 M.getFunctionList().erase(DeadFunctions[i]);
144 }
145 NumFunctions += DeadFunctions.size();
146 Changed = true;
Chris Lattner8f3acc62002-08-18 01:28:30 +0000147 }
148
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000149 if (!DeadGlobalVars.empty()) {
150 for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000151 RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000152 M.getGlobalList().erase(DeadGlobalVars[i]);
153 }
154 NumVariables += DeadGlobalVars.size();
155 Changed = true;
156 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000157
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000158 // Now delete any dead aliases.
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000159 if (!DeadAliases.empty()) {
160 for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
161 RemoveUnusedGlobalValue(*DeadAliases[i]);
162 M.getAliasList().erase(DeadAliases[i]);
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000163 }
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000164 NumAliases += DeadAliases.size();
165 Changed = true;
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000166 }
167
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000168 // Make sure that all memory is released
169 AliveGlobals.clear();
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000170 SeenConstants.clear();
Devang Patelc5aa8c62009-08-11 06:31:57 +0000171
Chris Lattner8f3acc62002-08-18 01:28:30 +0000172 return Changed;
173}
174
Devang Patel95b18122008-11-11 19:16:41 +0000175/// GlobalIsNeeded - the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000176/// recursively mark anything that it uses as also needed.
177void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000178 // If the global is already in the set, no need to reprocess it.
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000179 if (!AliveGlobals.insert(G))
180 return;
181
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000182 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
183 // If this is a global variable, we must make sure to add any global values
184 // referenced by the initializer to the alive set.
185 if (GV->hasInitializer())
186 MarkUsedGlobalsAsNeeded(GV->getInitializer());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000187 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
188 // The target of a global alias is needed.
Duncan Sands187c5712009-01-07 18:45:53 +0000189 MarkUsedGlobalsAsNeeded(GA->getAliasee());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000190 } else {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000191 // Otherwise this must be a function object. We have to scan the body of
192 // the function looking for constants and global values which are used as
193 // operands. Any operands of these types must be processed to ensure that
194 // any globals used will be marked as needed.
195 Function *F = cast<Function>(G);
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000196
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000197 if (F->hasPrefixData())
198 MarkUsedGlobalsAsNeeded(F->getPrefixData());
199
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000200 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000201 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000202 for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U)
203 if (GlobalValue *GV = dyn_cast<GlobalValue>(*U))
204 GlobalIsNeeded(GV);
205 else if (Constant *C = dyn_cast<Constant>(*U))
Misha Brukmanb1c93172005-04-21 23:48:37 +0000206 MarkUsedGlobalsAsNeeded(C);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000207 }
208}
209
210void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
Reid Spencer5f681592004-07-18 00:25:04 +0000211 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Chris Lattneree8b9512009-10-29 01:21:20 +0000212 return GlobalIsNeeded(GV);
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000213
Chris Lattneree8b9512009-10-29 01:21:20 +0000214 // Loop over all of the operands of the constant, adding any globals they
215 // use to the list of needed globals.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000216 for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) {
217 // If we've already processed this constant there's no need to do it again.
Benjamin Krameradc17272013-04-13 16:11:14 +0000218 Constant *Op = dyn_cast<Constant>(*I);
219 if (Op && SeenConstants.insert(Op))
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000220 MarkUsedGlobalsAsNeeded(Op);
221 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000222}
Chris Lattner8f3acc62002-08-18 01:28:30 +0000223
Reid Spencer5f681592004-07-18 00:25:04 +0000224// RemoveUnusedGlobalValue - Loop over all of the uses of the specified
Chris Lattner8f3acc62002-08-18 01:28:30 +0000225// GlobalValue, looking for the constant pointer ref that may be pointing to it.
226// If found, check to see if the constant pointer ref is safe to destroy, and if
227// so, nuke it. This will reduce the reference count on the global value, which
228// might make it deader.
229//
Reid Spencer5f681592004-07-18 00:25:04 +0000230bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
Chris Lattner9238d782004-07-18 07:22:58 +0000231 if (GV.use_empty()) return false;
232 GV.removeDeadConstantUsers();
233 return GV.use_empty();
234}