blob: 63d3032e131d1f6ee6bfc6ca3e8301a86e20718a [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
Nico Weber4b2acde2014-05-02 18:35:25 +000021#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000023#include "llvm/Pass.h"
24#include "llvm/Transforms/IPO.h"
Nico Weber4b2acde2014-05-02 18:35:25 +000025#include "llvm/Transforms/Utils/CtorUtils.h"
Bruno Cardoso Lopese2a1fa32014-08-25 17:51:14 +000026#include "llvm/Transforms/Utils/GlobalStatus.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) {
Andrew Kayloraa641a52016-04-22 22:06:11 +000078 if (skipModule(M))
79 return false;
80
Chris Lattner8f3acc62002-08-18 01:28:30 +000081 bool Changed = false;
Nico Weber4b2acde2014-05-02 18:35:25 +000082
83 // Remove empty functions from the global ctors list.
Richard Smithc167d652014-05-06 01:44:26 +000084 Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
Nico Weber4b2acde2014-05-02 18:35:25 +000085
Peter Collingbourne0dbc7082015-03-19 18:23:29 +000086 // Collect the set of members for each comdat.
87 for (Function &F : M)
88 if (Comdat *C = F.getComdat())
89 ComdatMembers.insert(std::make_pair(C, &F));
90 for (GlobalVariable &GV : M.globals())
91 if (Comdat *C = GV.getComdat())
92 ComdatMembers.insert(std::make_pair(C, &GV));
93 for (GlobalAlias &GA : M.aliases())
94 if (Comdat *C = GA.getComdat())
95 ComdatMembers.insert(std::make_pair(C, &GA));
96
Chris Lattnerec6d7a62003-09-16 19:27:31 +000097 // Loop over the module, adding globals which are obviously necessary.
Yaron Keren3d49f6d2015-07-18 19:57:34 +000098 for (Function &F : M) {
99 Changed |= RemoveUnusedGlobalValue(F);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000100 // Functions with external linkage are needed if they have a body
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000101 if (!F.isDeclaration() && !F.hasAvailableExternallyLinkage())
102 if (!F.isDiscardableIfUnused())
103 GlobalIsNeeded(&F);
Chris Lattner8f3acc62002-08-18 01:28:30 +0000104 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000105
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000106 for (GlobalVariable &GV : M.globals()) {
107 Changed |= RemoveUnusedGlobalValue(GV);
Chris Lattnerc8dfbbb2003-09-20 19:00:50 +0000108 // Externally visible & appending globals are needed, if they have an
109 // initializer.
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000110 if (!GV.isDeclaration() && !GV.hasAvailableExternallyLinkage())
111 if (!GV.isDiscardableIfUnused())
112 GlobalIsNeeded(&GV);
Chris Lattner8f3acc62002-08-18 01:28:30 +0000113 }
114
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000115 for (GlobalAlias &GA : M.aliases()) {
116 Changed |= RemoveUnusedGlobalValue(GA);
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000117 // Externally visible aliases are needed.
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000118 if (!GA.isDiscardableIfUnused())
119 GlobalIsNeeded(&GA);
David Majnemerdad0a642014-06-27 18:19:56 +0000120 }
121
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000122 // Now that all globals which are needed are in the AliveGlobals set, we loop
123 // through the program, deleting those which are not alive.
Chris Lattner8f3acc62002-08-18 01:28:30 +0000124 //
Chris Lattner8f3acc62002-08-18 01:28:30 +0000125
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000126 // The first pass is to drop initializers of global variables which are dead.
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000127 std::vector<GlobalVariable *> DeadGlobalVars; // Keep track of dead globals
128 for (GlobalVariable &GV : M.globals())
129 if (!AliveGlobals.count(&GV)) {
130 DeadGlobalVars.push_back(&GV); // Keep track of dead globals
131 if (GV.hasInitializer()) {
132 Constant *Init = GV.getInitializer();
133 GV.setInitializer(nullptr);
Bruno Cardoso Lopese2a1fa32014-08-25 17:51:14 +0000134 if (isSafeToDestroyConstant(Init))
135 Init->destroyConstant();
136 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000137 }
138
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000139 // The second pass drops the bodies of functions which are dead...
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000140 std::vector<Function *> DeadFunctions;
141 for (Function &F : M)
142 if (!AliveGlobals.count(&F)) {
143 DeadFunctions.push_back(&F); // Keep track of dead globals
144 if (!F.isDeclaration())
145 F.deleteBody();
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000146 }
147
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000148 // The third pass drops targets of aliases which are dead...
149 std::vector<GlobalAlias*> DeadAliases;
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000150 for (GlobalAlias &GA : M.aliases())
151 if (!AliveGlobals.count(&GA)) {
152 DeadAliases.push_back(&GA);
153 GA.setAliasee(nullptr);
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000154 }
155
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000156 if (!DeadFunctions.empty()) {
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000157 // Now that all interferences have been dropped, delete the actual objects
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000158 // themselves.
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000159 for (Function *F : DeadFunctions) {
160 RemoveUnusedGlobalValue(*F);
161 M.getFunctionList().erase(F);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000162 }
163 NumFunctions += DeadFunctions.size();
164 Changed = true;
Chris Lattner8f3acc62002-08-18 01:28:30 +0000165 }
166
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000167 if (!DeadGlobalVars.empty()) {
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000168 for (GlobalVariable *GV : DeadGlobalVars) {
169 RemoveUnusedGlobalValue(*GV);
170 M.getGlobalList().erase(GV);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000171 }
172 NumVariables += DeadGlobalVars.size();
173 Changed = true;
174 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000175
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000176 // Now delete any dead aliases.
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000177 if (!DeadAliases.empty()) {
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000178 for (GlobalAlias *GA : DeadAliases) {
179 RemoveUnusedGlobalValue(*GA);
180 M.getAliasList().erase(GA);
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000181 }
Duncan Sandsbf3ba5a2009-02-17 23:05:26 +0000182 NumAliases += DeadAliases.size();
183 Changed = true;
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000184 }
185
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000186 // Make sure that all memory is released
187 AliveGlobals.clear();
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000188 SeenConstants.clear();
Peter Collingbourne0dbc7082015-03-19 18:23:29 +0000189 ComdatMembers.clear();
Devang Patelc5aa8c62009-08-11 06:31:57 +0000190
Chris Lattner8f3acc62002-08-18 01:28:30 +0000191 return Changed;
192}
193
Devang Patel95b18122008-11-11 19:16:41 +0000194/// GlobalIsNeeded - the specific global value as needed, and
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000195/// recursively mark anything that it uses as also needed.
196void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000197 // If the global is already in the set, no need to reprocess it.
David Blaikie70573dc2014-11-19 07:49:26 +0000198 if (!AliveGlobals.insert(G).second)
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000199 return;
David Majnemere0253212014-10-07 07:07:19 +0000200
David Majnemere0253212014-10-07 07:07:19 +0000201 if (Comdat *C = G->getComdat()) {
Peter Collingbourne0dbc7082015-03-19 18:23:29 +0000202 for (auto &&CM : make_range(ComdatMembers.equal_range(C)))
203 GlobalIsNeeded(CM.second);
David Majnemere0253212014-10-07 07:07:19 +0000204 }
205
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000206 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
207 // If this is a global variable, we must make sure to add any global values
208 // referenced by the initializer to the alive set.
Pete Cooper91e4ba22014-07-08 17:06:03 +0000209 if (GV->hasInitializer())
210 MarkUsedGlobalsAsNeeded(GV->getInitializer());
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000211 } else if (GlobalIndirectSymbol *GIS = dyn_cast<GlobalIndirectSymbol>(G)) {
212 // The target of a global alias or ifunc is needed.
213 MarkUsedGlobalsAsNeeded(GIS->getIndirectSymbol());
Duncan Sandsf5dbbae2009-01-05 20:37:33 +0000214 } else {
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000215 // Otherwise this must be a function object. We have to scan the body of
216 // the function looking for constants and global values which are used as
217 // operands. Any operands of these types must be processed to ensure that
218 // any globals used will be marked as needed.
219 Function *F = cast<Function>(G);
Chris Lattnerb5d9c8c2009-11-01 19:03:42 +0000220
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000221 for (Use &U : F->operands())
222 MarkUsedGlobalsAsNeeded(cast<Constant>(U.get()));
David Majnemer7fddecc2015-06-17 20:52:32 +0000223
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000224 for (BasicBlock &BB : *F)
225 for (Instruction &I : BB)
226 for (Use &U : I.operands())
227 if (GlobalValue *GV = dyn_cast<GlobalValue>(U))
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000228 GlobalIsNeeded(GV);
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000229 else if (Constant *C = dyn_cast<Constant>(U))
Misha Brukmanb1c93172005-04-21 23:48:37 +0000230 MarkUsedGlobalsAsNeeded(C);
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000231 }
232}
233
234void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
Reid Spencer5f681592004-07-18 00:25:04 +0000235 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Chris Lattneree8b9512009-10-29 01:21:20 +0000236 return GlobalIsNeeded(GV);
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000237
Chris Lattneree8b9512009-10-29 01:21:20 +0000238 // Loop over all of the operands of the constant, adding any globals they
239 // use to the list of needed globals.
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000240 for (Use &U : C->operands()) {
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000241 // If we've already processed this constant there's no need to do it again.
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000242 Constant *Op = dyn_cast<Constant>(U);
David Blaikie70573dc2014-11-19 07:49:26 +0000243 if (Op && SeenConstants.insert(Op).second)
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000244 MarkUsedGlobalsAsNeeded(Op);
245 }
Chris Lattnerec6d7a62003-09-16 19:27:31 +0000246}
Chris Lattner8f3acc62002-08-18 01:28:30 +0000247
Reid Spencer5f681592004-07-18 00:25:04 +0000248// RemoveUnusedGlobalValue - Loop over all of the uses of the specified
Chris Lattner8f3acc62002-08-18 01:28:30 +0000249// GlobalValue, looking for the constant pointer ref that may be pointing to it.
250// If found, check to see if the constant pointer ref is safe to destroy, and if
251// so, nuke it. This will reduce the reference count on the global value, which
252// might make it deader.
253//
Reid Spencer5f681592004-07-18 00:25:04 +0000254bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
Yaron Keren3d49f6d2015-07-18 19:57:34 +0000255 if (GV.use_empty())
256 return false;
Chris Lattner9238d782004-07-18 07:22:58 +0000257 GV.removeDeadConstantUsers();
258 return GV.use_empty();
259}