blob: 7e7a4c0ae8354c69cae1470acbb266681f2b4dc3 [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 };
Richard Smithc167d652014-05-06 01:44:26 +000057}
Nico Weber4b2acde2014-05-02 18:35:25 +000058
59/// Returns true if F contains only a single "ret" instruction.
Richard Smithc167d652014-05-06 01:44:26 +000060static bool isEmptyFunction(Function *F) {
Nico Weber4b2acde2014-05-02 18:35:25 +000061 BasicBlock &Entry = F->getEntryBlock();
62 if (Entry.size() != 1 || !isa<ReturnInst>(Entry.front()))
63 return false;
64 ReturnInst &RI = cast<ReturnInst>(Entry.front());
Craig Topper66f09ad2014-06-08 22:29:17 +000065 return RI.getReturnValue() == nullptr;
Nico Weber4b2acde2014-05-02 18:35:25 +000066}
Chris Lattnerd5d56782002-01-31 00:45:11 +000067
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.
Richard Smithc167d652014-05-06 01:44:26 +000078 Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
Nico Weber4b2acde2014-05-02 18:35:25 +000079
David Majnemerdad0a642014-06-27 18:19:56 +000080 typedef std::multimap<const Comdat *, GlobalValue *> ComdatGVPairsTy;
81 ComdatGVPairsTy ComdatGVPairs;
82
Chris Lattnerec6d7a62003-09-16 19:27:31 +000083 // Loop over the module, adding globals which are obviously necessary.
Chris Lattner8f3acc62002-08-18 01:28:30 +000084 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +000085 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerec6d7a62003-09-16 19:27:31 +000086 // Functions with external linkage are needed if they have a body
David Majnemerdad0a642014-06-27 18:19:56 +000087 if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
88 if (!I->isDiscardableIfUnused())
89 GlobalIsNeeded(I);
90 else if (const Comdat *C = I->getComdat())
91 ComdatGVPairs.insert(std::make_pair(C, I));
92 }
Chris Lattner8f3acc62002-08-18 01:28:30 +000093 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +000094
Anton Korobeynikova97b6942007-04-25 14:27:10 +000095 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
96 I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +000097 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerc8dfbbb2003-09-20 19:00:50 +000098 // Externally visible & appending globals are needed, if they have an
99 // initializer.
David Majnemerdad0a642014-06-27 18:19:56 +0000100 if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
101 if (!I->isDiscardableIfUnused())
102 GlobalIsNeeded(I);
103 else if (const Comdat *C = I->getComdat())
104 ComdatGVPairs.insert(std::make_pair(C, I));
105 }
Chris Lattner8f3acc62002-08-18 01:28:30 +0000106 }
107
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000108 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
109 I != E; ++I) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000110 Changed |= RemoveUnusedGlobalValue(*I);
111 // Externally visible aliases are needed.
David Majnemerdad0a642014-06-27 18:19:56 +0000112 if (!I->isDiscardableIfUnused()) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000113 GlobalIsNeeded(I);
David Majnemerdad0a642014-06-27 18:19:56 +0000114 } else if (const Comdat *C = I->getComdat()) {
115 ComdatGVPairs.insert(std::make_pair(C, I));
116 }
117 }
118
119 for (ComdatGVPairsTy::iterator I = ComdatGVPairs.begin(),
120 E = ComdatGVPairs.end();
121 I != E;) {
122 ComdatGVPairsTy::iterator UB = ComdatGVPairs.upper_bound(I->first);
123 bool CanDiscard = std::all_of(I, UB, [](ComdatGVPairsTy::value_type Pair) {
124 return Pair.second->isDiscardableIfUnused();
125 });
126 if (!CanDiscard) {
127 std::for_each(I, UB, [this](ComdatGVPairsTy::value_type Pair) {
128 GlobalIsNeeded(Pair.second);
129 });
130 }
131 I = UB;
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000132 }
133
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000134 // Now that all globals which are needed are in the AliveGlobals set, we loop
135 // through the program, deleting those which are not alive.
Chris Lattner8f3acc62002-08-18 01:28:30 +0000136 //
Chris Lattner8f3acc62002-08-18 01:28:30 +0000137
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000138 // The first pass is to drop initializers of global variables which are dead.
139 std::vector<GlobalVariable*> DeadGlobalVars; // Keep track of dead globals
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000140 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
141 I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000142 if (!AliveGlobals.count(I)) {
143 DeadGlobalVars.push_back(I); // Keep track of dead globals
Craig Topperf40110f2014-04-25 05:29:35 +0000144 I->setInitializer(nullptr);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000145 }
146
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000147 // The second pass drops the bodies of functions which are dead...
148 std::vector<Function*> DeadFunctions;
149 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
150 if (!AliveGlobals.count(I)) {
151 DeadFunctions.push_back(I); // Keep track of dead globals
Reid Spencer5301e7c2007-01-30 20:08:39 +0000152 if (!I->isDeclaration())
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000153 I->deleteBody();
154 }
155
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000156 // The third pass drops targets of aliases which are dead...
157 std::vector<GlobalAlias*> DeadAliases;
158 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
159 ++I)
160 if (!AliveGlobals.count(I)) {
161 DeadAliases.push_back(I);
Craig Topperf40110f2014-04-25 05:29:35 +0000162 I->setAliasee(nullptr);
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000163 }
164
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000165 if (!DeadFunctions.empty()) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000166 // Now that all interferences have been dropped, delete the actual objects
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000167 // themselves.
168 for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000169 RemoveUnusedGlobalValue(*DeadFunctions[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000170 M.getFunctionList().erase(DeadFunctions[i]);
171 }
172 NumFunctions += DeadFunctions.size();
173 Changed = true;
Chris Lattner8f3acc62002-08-18 01:28:30 +0000174 }
175
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000176 if (!DeadGlobalVars.empty()) {
177 for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000178 RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000179 M.getGlobalList().erase(DeadGlobalVars[i]);
180 }
181 NumVariables += DeadGlobalVars.size();
182 Changed = true;
183 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000184
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000185 // Now delete any dead aliases.
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000186 if (!DeadAliases.empty()) {
187 for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
188 RemoveUnusedGlobalValue(*DeadAliases[i]);
189 M.getAliasList().erase(DeadAliases[i]);
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000190 }
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000191 NumAliases += DeadAliases.size();
192 Changed = true;
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000193 }
194
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000195 // Make sure that all memory is released
196 AliveGlobals.clear();
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000197 SeenConstants.clear();
Devang Patelc5aa8c62009-08-11 06:31:57 +0000198
Chris Lattner8f3acc62002-08-18 01:28:30 +0000199 return Changed;
200}
201
Devang Patel95b18122008-11-11 19:16:41 +0000202/// GlobalIsNeeded - the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000203/// recursively mark anything that it uses as also needed.
204void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000205 // If the global is already in the set, no need to reprocess it.
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000206 if (!AliveGlobals.insert(G))
207 return;
208
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000209 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
210 // If this is a global variable, we must make sure to add any global values
211 // referenced by the initializer to the alive set.
212 if (GV->hasInitializer())
213 MarkUsedGlobalsAsNeeded(GV->getInitializer());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000214 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
215 // The target of a global alias is needed.
Duncan Sands187c5712009-01-07 18:45:53 +0000216 MarkUsedGlobalsAsNeeded(GA->getAliasee());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000217 } else {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000218 // Otherwise this must be a function object. We have to scan the body of
219 // the function looking for constants and global values which are used as
220 // operands. Any operands of these types must be processed to ensure that
221 // any globals used will be marked as needed.
222 Function *F = cast<Function>(G);
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000223
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000224 if (F->hasPrefixData())
225 MarkUsedGlobalsAsNeeded(F->getPrefixData());
226
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000227 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000228 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000229 for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U)
230 if (GlobalValue *GV = dyn_cast<GlobalValue>(*U))
231 GlobalIsNeeded(GV);
232 else if (Constant *C = dyn_cast<Constant>(*U))
Misha Brukmanb1c93172005-04-21 23:48:37 +0000233 MarkUsedGlobalsAsNeeded(C);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000234 }
235}
236
237void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
Reid Spencer5f681592004-07-18 00:25:04 +0000238 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Chris Lattneree8b9512009-10-29 01:21:20 +0000239 return GlobalIsNeeded(GV);
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000240
Chris Lattneree8b9512009-10-29 01:21:20 +0000241 // Loop over all of the operands of the constant, adding any globals they
242 // use to the list of needed globals.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000243 for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) {
244 // If we've already processed this constant there's no need to do it again.
Benjamin Krameradc17272013-04-13 16:11:14 +0000245 Constant *Op = dyn_cast<Constant>(*I);
246 if (Op && SeenConstants.insert(Op))
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000247 MarkUsedGlobalsAsNeeded(Op);
248 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000249}
Chris Lattner8f3acc62002-08-18 01:28:30 +0000250
Reid Spencer5f681592004-07-18 00:25:04 +0000251// RemoveUnusedGlobalValue - Loop over all of the uses of the specified
Chris Lattner8f3acc62002-08-18 01:28:30 +0000252// GlobalValue, looking for the constant pointer ref that may be pointing to it.
253// If found, check to see if the constant pointer ref is safe to destroy, and if
254// so, nuke it. This will reduce the reference count on the global value, which
255// might make it deader.
256//
Reid Spencer5f681592004-07-18 00:25:04 +0000257bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
Chris Lattner9238d782004-07-18 07:22:58 +0000258 if (GV.use_empty()) return false;
259 GV.removeDeadConstantUsers();
260 return GV.use_empty();
261}