blob: 61d0ff94a3437341b6648fe8db9ca084306acf96 [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"
Bruno Cardoso Lopese2a1fa32014-08-25 17:51:14 +000025#include "llvm/Transforms/Utils/GlobalStatus.h"
Chris Lattnerec6d7a62003-09-16 19:27:31 +000026#include "llvm/Pass.h"
Peter Collingbourne0dbc7082015-03-19 18:23:29 +000027#include <unordered_map>
Chris Lattnerf52e03c2003-11-21 21:54:22 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chandler Carruth964daaa2014-04-22 02:55:47 +000030#define DEBUG_TYPE "globaldce"
31
Duncan Sands821d13c2009-01-05 20:39:50 +000032STATISTIC(NumAliases , "Number of global aliases removed");
Chris Lattner1631bcb2006-12-19 22:09:18 +000033STATISTIC(NumFunctions, "Number of functions removed");
34STATISTIC(NumVariables, "Number of global variables removed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000035
Chris Lattner1631bcb2006-12-19 22:09:18 +000036namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000037 struct GlobalDCE : public ModulePass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000039 GlobalDCE() : ModulePass(ID) {
40 initializeGlobalDCEPass(*PassRegistry::getPassRegistry());
41 }
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000042
Chris Lattner04805fa2002-02-26 21:46:54 +000043 // run - Do the GlobalDCE pass on the specified module, optionally updating
44 // the specified callgraph to reflect the changes.
45 //
Craig Topper3e4c6972014-03-05 09:10:37 +000046 bool runOnModule(Module &M) override;
Chris Lattner8f3acc62002-08-18 01:28:30 +000047
48 private:
Pete Cooper91e4ba22014-07-08 17:06:03 +000049 SmallPtrSet<GlobalValue*, 32> AliveGlobals;
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +000050 SmallPtrSet<Constant *, 8> SeenConstants;
Peter Collingbourne0dbc7082015-03-19 18:23:29 +000051 std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
Chris Lattner8f3acc62002-08-18 01:28:30 +000052
Duncan Sandsf5dbbae2009-01-05 20:37:33 +000053 /// GlobalIsNeeded - mark the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +000054 /// recursively mark anything that it uses as also needed.
55 void GlobalIsNeeded(GlobalValue *GV);
56 void MarkUsedGlobalsAsNeeded(Constant *C);
Chris Lattner8f3acc62002-08-18 01:28:30 +000057
Reid Spencer5f681592004-07-18 00:25:04 +000058 bool RemoveUnusedGlobalValue(GlobalValue &GV);
Chris Lattner04805fa2002-02-26 21:46:54 +000059 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000060}
Nico Weber4b2acde2014-05-02 18:35:25 +000061
62/// Returns true if F contains only a single "ret" instruction.
Richard Smithc167d652014-05-06 01:44:26 +000063static bool isEmptyFunction(Function *F) {
Nico Weber4b2acde2014-05-02 18:35:25 +000064 BasicBlock &Entry = F->getEntryBlock();
65 if (Entry.size() != 1 || !isa<ReturnInst>(Entry.front()))
66 return false;
67 ReturnInst &RI = cast<ReturnInst>(Entry.front());
Craig Topper66f09ad2014-06-08 22:29:17 +000068 return RI.getReturnValue() == nullptr;
Nico Weber4b2acde2014-05-02 18:35:25 +000069}
Chris Lattnerd5d56782002-01-31 00:45:11 +000070
Dan Gohmand78c4002008-05-13 00:00:25 +000071char GlobalDCE::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000072INITIALIZE_PASS(GlobalDCE, "globaldce",
Owen Andersondf7a4f22010-10-07 22:25:06 +000073 "Dead Global Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000074
Chris Lattner4f2cf032004-09-20 04:48:05 +000075ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
Chris Lattner8f3acc62002-08-18 01:28:30 +000076
Chris Lattner4f2cf032004-09-20 04:48:05 +000077bool GlobalDCE::runOnModule(Module &M) {
Chris Lattner8f3acc62002-08-18 01:28:30 +000078 bool Changed = false;
Nico Weber4b2acde2014-05-02 18:35:25 +000079
80 // Remove empty functions from the global ctors list.
Richard Smithc167d652014-05-06 01:44:26 +000081 Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
Nico Weber4b2acde2014-05-02 18:35:25 +000082
Peter Collingbourne0dbc7082015-03-19 18:23:29 +000083 // Collect the set of members for each comdat.
84 for (Function &F : M)
85 if (Comdat *C = F.getComdat())
86 ComdatMembers.insert(std::make_pair(C, &F));
87 for (GlobalVariable &GV : M.globals())
88 if (Comdat *C = GV.getComdat())
89 ComdatMembers.insert(std::make_pair(C, &GV));
90 for (GlobalAlias &GA : M.aliases())
91 if (Comdat *C = GA.getComdat())
92 ComdatMembers.insert(std::make_pair(C, &GA));
93
Chris Lattnerec6d7a62003-09-16 19:27:31 +000094 // Loop over the module, adding globals which are obviously necessary.
Chris Lattner8f3acc62002-08-18 01:28:30 +000095 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +000096 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerec6d7a62003-09-16 19:27:31 +000097 // Functions with external linkage are needed if they have a body
David Majnemerdad0a642014-06-27 18:19:56 +000098 if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
99 if (!I->isDiscardableIfUnused())
100 GlobalIsNeeded(I);
David Majnemerdad0a642014-06-27 18:19:56 +0000101 }
Chris Lattner8f3acc62002-08-18 01:28:30 +0000102 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000103
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000104 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
105 I != E; ++I) {
Reid Spencer5f681592004-07-18 00:25:04 +0000106 Changed |= RemoveUnusedGlobalValue(*I);
Chris Lattnerc8dfbbb2003-09-20 19:00:50 +0000107 // Externally visible & appending globals are needed, if they have an
108 // initializer.
David Majnemerdad0a642014-06-27 18:19:56 +0000109 if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
110 if (!I->isDiscardableIfUnused())
111 GlobalIsNeeded(I);
David Majnemerdad0a642014-06-27 18:19:56 +0000112 }
Chris Lattner8f3acc62002-08-18 01:28:30 +0000113 }
114
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000115 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
116 I != E; ++I) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000117 Changed |= RemoveUnusedGlobalValue(*I);
118 // Externally visible aliases are needed.
David Majnemerdad0a642014-06-27 18:19:56 +0000119 if (!I->isDiscardableIfUnused()) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000120 GlobalIsNeeded(I);
David Majnemerdad0a642014-06-27 18:19:56 +0000121 }
122 }
123
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000124 // Now that all globals which are needed are in the AliveGlobals set, we loop
125 // through the program, deleting those which are not alive.
Chris Lattner8f3acc62002-08-18 01:28:30 +0000126 //
Chris Lattner8f3acc62002-08-18 01:28:30 +0000127
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000128 // The first pass is to drop initializers of global variables which are dead.
129 std::vector<GlobalVariable*> DeadGlobalVars; // Keep track of dead globals
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000130 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
131 I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000132 if (!AliveGlobals.count(I)) {
133 DeadGlobalVars.push_back(I); // Keep track of dead globals
Bruno Cardoso Lopese2a1fa32014-08-25 17:51:14 +0000134 if (I->hasInitializer()) {
135 Constant *Init = I->getInitializer();
136 I->setInitializer(nullptr);
137 if (isSafeToDestroyConstant(Init))
138 Init->destroyConstant();
139 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000140 }
141
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000142 // The second pass drops the bodies of functions which are dead...
143 std::vector<Function*> DeadFunctions;
144 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
145 if (!AliveGlobals.count(I)) {
146 DeadFunctions.push_back(I); // Keep track of dead globals
Reid Spencer5301e7c2007-01-30 20:08:39 +0000147 if (!I->isDeclaration())
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000148 I->deleteBody();
149 }
150
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000151 // The third pass drops targets of aliases which are dead...
152 std::vector<GlobalAlias*> DeadAliases;
153 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
154 ++I)
155 if (!AliveGlobals.count(I)) {
156 DeadAliases.push_back(I);
Craig Topperf40110f2014-04-25 05:29:35 +0000157 I->setAliasee(nullptr);
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000158 }
159
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000160 if (!DeadFunctions.empty()) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000161 // Now that all interferences have been dropped, delete the actual objects
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000162 // themselves.
163 for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000164 RemoveUnusedGlobalValue(*DeadFunctions[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000165 M.getFunctionList().erase(DeadFunctions[i]);
166 }
167 NumFunctions += DeadFunctions.size();
168 Changed = true;
Chris Lattner8f3acc62002-08-18 01:28:30 +0000169 }
170
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000171 if (!DeadGlobalVars.empty()) {
172 for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
Reid Spencer5f681592004-07-18 00:25:04 +0000173 RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000174 M.getGlobalList().erase(DeadGlobalVars[i]);
175 }
176 NumVariables += DeadGlobalVars.size();
177 Changed = true;
178 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000179
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000180 // Now delete any dead aliases.
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000181 if (!DeadAliases.empty()) {
182 for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
183 RemoveUnusedGlobalValue(*DeadAliases[i]);
184 M.getAliasList().erase(DeadAliases[i]);
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000185 }
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000186 NumAliases += DeadAliases.size();
187 Changed = true;
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000188 }
189
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000190 // Make sure that all memory is released
191 AliveGlobals.clear();
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000192 SeenConstants.clear();
Peter Collingbourne0dbc7082015-03-19 18:23:29 +0000193 ComdatMembers.clear();
Devang Patelc5aa8c62009-08-11 06:31:57 +0000194
Chris Lattner8f3acc62002-08-18 01:28:30 +0000195 return Changed;
196}
197
Devang Patel95b18122008-11-11 19:16:41 +0000198/// GlobalIsNeeded - the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000199/// recursively mark anything that it uses as also needed.
200void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000201 // If the global is already in the set, no need to reprocess it.
David Blaikie70573dc2014-11-19 07:49:26 +0000202 if (!AliveGlobals.insert(G).second)
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000203 return;
David Majnemere0253212014-10-07 07:07:19 +0000204
David Majnemere0253212014-10-07 07:07:19 +0000205 if (Comdat *C = G->getComdat()) {
Peter Collingbourne0dbc7082015-03-19 18:23:29 +0000206 for (auto &&CM : make_range(ComdatMembers.equal_range(C)))
207 GlobalIsNeeded(CM.second);
David Majnemere0253212014-10-07 07:07:19 +0000208 }
209
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000210 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
211 // If this is a global variable, we must make sure to add any global values
212 // referenced by the initializer to the alive set.
Pete Cooper91e4ba22014-07-08 17:06:03 +0000213 if (GV->hasInitializer())
214 MarkUsedGlobalsAsNeeded(GV->getInitializer());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000215 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
216 // The target of a global alias is needed.
Duncan Sands187c5712009-01-07 18:45:53 +0000217 MarkUsedGlobalsAsNeeded(GA->getAliasee());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000218 } else {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000219 // Otherwise this must be a function object. We have to scan the body of
220 // the function looking for constants and global values which are used as
221 // operands. Any operands of these types must be processed to ensure that
222 // any globals used will be marked as needed.
223 Function *F = cast<Function>(G);
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000224
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000225 if (F->hasPrefixData())
226 MarkUsedGlobalsAsNeeded(F->getPrefixData());
227
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000228 if (F->hasPrologueData())
229 MarkUsedGlobalsAsNeeded(F->getPrologueData());
230
David Majnemer7fddecc2015-06-17 20:52:32 +0000231 if (F->hasPersonalityFn())
232 MarkUsedGlobalsAsNeeded(F->getPersonalityFn());
233
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000234 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000235 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000236 for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U)
237 if (GlobalValue *GV = dyn_cast<GlobalValue>(*U))
238 GlobalIsNeeded(GV);
239 else if (Constant *C = dyn_cast<Constant>(*U))
Misha Brukmanb1c93172005-04-21 23:48:37 +0000240 MarkUsedGlobalsAsNeeded(C);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000241 }
242}
243
244void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
Reid Spencer5f681592004-07-18 00:25:04 +0000245 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Chris Lattneree8b9512009-10-29 01:21:20 +0000246 return GlobalIsNeeded(GV);
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000247
Chris Lattneree8b9512009-10-29 01:21:20 +0000248 // Loop over all of the operands of the constant, adding any globals they
249 // use to the list of needed globals.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000250 for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) {
251 // If we've already processed this constant there's no need to do it again.
Benjamin Krameradc17272013-04-13 16:11:14 +0000252 Constant *Op = dyn_cast<Constant>(*I);
David Blaikie70573dc2014-11-19 07:49:26 +0000253 if (Op && SeenConstants.insert(Op).second)
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000254 MarkUsedGlobalsAsNeeded(Op);
255 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000256}
Chris Lattner8f3acc62002-08-18 01:28:30 +0000257
Reid Spencer5f681592004-07-18 00:25:04 +0000258// RemoveUnusedGlobalValue - Loop over all of the uses of the specified
Chris Lattner8f3acc62002-08-18 01:28:30 +0000259// GlobalValue, looking for the constant pointer ref that may be pointing to it.
260// If found, check to see if the constant pointer ref is safe to destroy, and if
261// so, nuke it. This will reduce the reference count on the global value, which
262// might make it deader.
263//
Reid Spencer5f681592004-07-18 00:25:04 +0000264bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
Chris Lattner9238d782004-07-18 07:22:58 +0000265 if (GV.use_empty()) return false;
266 GV.removeDeadConstantUsers();
267 return GV.use_empty();
268}