Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1 | //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | bd422e6 | 2001-11-26 18:42:17 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 10 | // 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 Lattner | bd422e6 | 2001-11-26 18:42:17 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/IPO/GlobalDCE.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Module.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 23 | #include "llvm/Pass.h" |
| 24 | #include "llvm/Transforms/IPO.h" |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/CtorUtils.h" |
Bruno Cardoso Lopes | e2a1fa3 | 2014-08-25 17:51:14 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 27 | |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "globaldce" |
| 31 | |
Duncan Sands | 821d13c | 2009-01-05 20:39:50 +0000 | [diff] [blame] | 32 | STATISTIC(NumAliases , "Number of global aliases removed"); |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 33 | STATISTIC(NumFunctions, "Number of functions removed"); |
David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 34 | STATISTIC(NumIFuncs, "Number of indirect functions removed"); |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 35 | STATISTIC(NumVariables, "Number of global variables removed"); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 37 | namespace { |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 38 | class GlobalDCELegacyPass : public ModulePass { |
| 39 | public: |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 40 | static char ID; // Pass identification, replacement for typeid |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 41 | GlobalDCELegacyPass() : ModulePass(ID) { |
| 42 | initializeGlobalDCELegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 43 | } |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 45 | // run - Do the GlobalDCE pass on the specified module, optionally updating |
| 46 | // the specified callgraph to reflect the changes. |
| 47 | // |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 48 | bool runOnModule(Module &M) override { |
| 49 | if (skipModule(M)) |
| 50 | return false; |
| 51 | |
Chandler Carruth | e8c66b2 | 2017-01-23 08:33:24 +0000 | [diff] [blame] | 52 | // We need a minimally functional dummy module analysis manager. It needs |
| 53 | // to at least know about the possibility of proxying a function analysis |
| 54 | // manager. |
| 55 | FunctionAnalysisManager DummyFAM; |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 56 | ModuleAnalysisManager DummyMAM; |
Chandler Carruth | e8c66b2 | 2017-01-23 08:33:24 +0000 | [diff] [blame] | 57 | DummyMAM.registerPass( |
| 58 | [&] { return FunctionAnalysisManagerModuleProxy(DummyFAM); }); |
| 59 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 60 | auto PA = Impl.run(M, DummyMAM); |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 61 | return !PA.areAllPreserved(); |
| 62 | } |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 63 | |
| 64 | private: |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 65 | GlobalDCEPass Impl; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 66 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 67 | } |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 68 | |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 69 | char GlobalDCELegacyPass::ID = 0; |
| 70 | INITIALIZE_PASS(GlobalDCELegacyPass, "globaldce", |
| 71 | "Dead Global Elimination", false, false) |
| 72 | |
| 73 | // Public interface to the GlobalDCEPass. |
| 74 | ModulePass *llvm::createGlobalDCEPass() { |
| 75 | return new GlobalDCELegacyPass(); |
| 76 | } |
| 77 | |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 78 | /// Returns true if F contains only a single "ret" instruction. |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 79 | static bool isEmptyFunction(Function *F) { |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 80 | BasicBlock &Entry = F->getEntryBlock(); |
| 81 | if (Entry.size() != 1 || !isa<ReturnInst>(Entry.front())) |
| 82 | return false; |
| 83 | ReturnInst &RI = cast<ReturnInst>(Entry.front()); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 84 | return RI.getReturnValue() == nullptr; |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 86 | |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 87 | /// Compute the set of GlobalValue that depends from V. |
| 88 | /// The recursion stops as soon as a GlobalValue is met. |
| 89 | void GlobalDCEPass::ComputeDependencies(Value *V, |
| 90 | SmallPtrSetImpl<GlobalValue *> &Deps) { |
| 91 | if (auto *I = dyn_cast<Instruction>(V)) { |
| 92 | Function *Parent = I->getParent()->getParent(); |
| 93 | Deps.insert(Parent); |
| 94 | } else if (auto *GV = dyn_cast<GlobalValue>(V)) { |
| 95 | Deps.insert(GV); |
| 96 | } else if (auto *CE = dyn_cast<Constant>(V)) { |
| 97 | // Avoid walking the whole tree of a big ConstantExprs multiple times. |
| 98 | auto Where = ConstantDependenciesCache.find(CE); |
| 99 | if (Where != ConstantDependenciesCache.end()) { |
| 100 | auto const &K = Where->second; |
| 101 | Deps.insert(K.begin(), K.end()); |
| 102 | } else { |
| 103 | SmallPtrSetImpl<GlobalValue *> &LocalDeps = ConstantDependenciesCache[CE]; |
| 104 | for (User *CEUser : CE->users()) |
| 105 | ComputeDependencies(CEUser, LocalDeps); |
| 106 | Deps.insert(LocalDeps.begin(), LocalDeps.end()); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void GlobalDCEPass::UpdateGVDependencies(GlobalValue &GV) { |
| 112 | SmallPtrSet<GlobalValue *, 8> Deps; |
| 113 | for (User *User : GV.users()) |
| 114 | ComputeDependencies(User, Deps); |
| 115 | Deps.erase(&GV); // Remove self-reference. |
| 116 | for (GlobalValue *GVU : Deps) { |
Michael Zolotukhin | c4fcc18 | 2017-10-17 23:47:06 +0000 | [diff] [blame] | 117 | GVDependencies[GVU].insert(&GV); |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | /// Mark Global value as Live |
| 122 | void GlobalDCEPass::MarkLive(GlobalValue &GV, |
| 123 | SmallVectorImpl<GlobalValue *> *Updates) { |
| 124 | auto const Ret = AliveGlobals.insert(&GV); |
| 125 | if (!Ret.second) |
| 126 | return; |
| 127 | |
| 128 | if (Updates) |
| 129 | Updates->push_back(&GV); |
| 130 | if (Comdat *C = GV.getComdat()) { |
| 131 | for (auto &&CM : make_range(ComdatMembers.equal_range(C))) |
| 132 | MarkLive(*CM.second, Updates); // Recursion depth is only two because only |
| 133 | // globals in the same comdat are visited. |
| 134 | } |
| 135 | } |
| 136 | |
Chandler Carruth | e8c66b2 | 2017-01-23 08:33:24 +0000 | [diff] [blame] | 137 | PreservedAnalyses GlobalDCEPass::run(Module &M, ModuleAnalysisManager &MAM) { |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 138 | bool Changed = false; |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 139 | |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 140 | // The algorithm first computes the set L of global variables that are |
| 141 | // trivially live. Then it walks the initialization of these variables to |
| 142 | // compute the globals used to initialize them, which effectively builds a |
| 143 | // directed graph where nodes are global variables, and an edge from A to B |
| 144 | // means B is used to initialize A. Finally, it propagates the liveness |
| 145 | // information through the graph starting from the nodes in L. Nodes note |
| 146 | // marked as alive are discarded. |
| 147 | |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 148 | // Remove empty functions from the global ctors list. |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 149 | Changed |= optimizeGlobalCtorsList(M, isEmptyFunction); |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 150 | |
Peter Collingbourne | 0dbc708 | 2015-03-19 18:23:29 +0000 | [diff] [blame] | 151 | // Collect the set of members for each comdat. |
| 152 | for (Function &F : M) |
| 153 | if (Comdat *C = F.getComdat()) |
| 154 | ComdatMembers.insert(std::make_pair(C, &F)); |
| 155 | for (GlobalVariable &GV : M.globals()) |
| 156 | if (Comdat *C = GV.getComdat()) |
| 157 | ComdatMembers.insert(std::make_pair(C, &GV)); |
| 158 | for (GlobalAlias &GA : M.aliases()) |
| 159 | if (Comdat *C = GA.getComdat()) |
| 160 | ComdatMembers.insert(std::make_pair(C, &GA)); |
| 161 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 162 | // Loop over the module, adding globals which are obviously necessary. |
Peter Collingbourne | 6d88fde | 2016-06-22 20:29:42 +0000 | [diff] [blame] | 163 | for (GlobalObject &GO : M.global_objects()) { |
| 164 | Changed |= RemoveUnusedGlobalValue(GO); |
| 165 | // Functions with external linkage are needed if they have a body. |
Chris Lattner | c8dfbbb | 2003-09-20 19:00:50 +0000 | [diff] [blame] | 166 | // Externally visible & appending globals are needed, if they have an |
| 167 | // initializer. |
Peter Collingbourne | 6d88fde | 2016-06-22 20:29:42 +0000 | [diff] [blame] | 168 | if (!GO.isDeclaration() && !GO.hasAvailableExternallyLinkage()) |
| 169 | if (!GO.isDiscardableIfUnused()) |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 170 | MarkLive(GO); |
| 171 | |
| 172 | UpdateGVDependencies(GO); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 175 | // Compute direct dependencies of aliases. |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 176 | for (GlobalAlias &GA : M.aliases()) { |
| 177 | Changed |= RemoveUnusedGlobalValue(GA); |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 178 | // Externally visible aliases are needed. |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 179 | if (!GA.isDiscardableIfUnused()) |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 180 | MarkLive(GA); |
| 181 | |
| 182 | UpdateGVDependencies(GA); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 185 | // Compute direct dependencies of ifuncs. |
David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 186 | for (GlobalIFunc &GIF : M.ifuncs()) { |
| 187 | Changed |= RemoveUnusedGlobalValue(GIF); |
| 188 | // Externally visible ifuncs are needed. |
| 189 | if (!GIF.isDiscardableIfUnused()) |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 190 | MarkLive(GIF); |
| 191 | |
| 192 | UpdateGVDependencies(GIF); |
| 193 | } |
| 194 | |
| 195 | // Propagate liveness from collected Global Values through the computed |
| 196 | // dependencies. |
| 197 | SmallVector<GlobalValue *, 8> NewLiveGVs{AliveGlobals.begin(), |
| 198 | AliveGlobals.end()}; |
| 199 | while (!NewLiveGVs.empty()) { |
| 200 | GlobalValue *LGV = NewLiveGVs.pop_back_val(); |
Michael Zolotukhin | c4fcc18 | 2017-10-17 23:47:06 +0000 | [diff] [blame] | 201 | for (auto *GVD : GVDependencies[LGV]) |
| 202 | MarkLive(*GVD, &NewLiveGVs); |
David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 205 | // Now that all globals which are needed are in the AliveGlobals set, we loop |
| 206 | // through the program, deleting those which are not alive. |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 207 | // |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 208 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 209 | // The first pass is to drop initializers of global variables which are dead. |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 210 | std::vector<GlobalVariable *> DeadGlobalVars; // Keep track of dead globals |
| 211 | for (GlobalVariable &GV : M.globals()) |
| 212 | if (!AliveGlobals.count(&GV)) { |
| 213 | DeadGlobalVars.push_back(&GV); // Keep track of dead globals |
| 214 | if (GV.hasInitializer()) { |
| 215 | Constant *Init = GV.getInitializer(); |
| 216 | GV.setInitializer(nullptr); |
Bruno Cardoso Lopes | e2a1fa3 | 2014-08-25 17:51:14 +0000 | [diff] [blame] | 217 | if (isSafeToDestroyConstant(Init)) |
| 218 | Init->destroyConstant(); |
| 219 | } |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 222 | // The second pass drops the bodies of functions which are dead... |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 223 | std::vector<Function *> DeadFunctions; |
| 224 | for (Function &F : M) |
| 225 | if (!AliveGlobals.count(&F)) { |
| 226 | DeadFunctions.push_back(&F); // Keep track of dead globals |
Chandler Carruth | 6acdca7 | 2017-01-24 12:55:57 +0000 | [diff] [blame] | 227 | if (!F.isDeclaration()) |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 228 | F.deleteBody(); |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Duncan Sands | bf3ba5a | 2009-02-17 23:05:26 +0000 | [diff] [blame] | 231 | // The third pass drops targets of aliases which are dead... |
| 232 | std::vector<GlobalAlias*> DeadAliases; |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 233 | for (GlobalAlias &GA : M.aliases()) |
| 234 | if (!AliveGlobals.count(&GA)) { |
| 235 | DeadAliases.push_back(&GA); |
| 236 | GA.setAliasee(nullptr); |
Duncan Sands | bf3ba5a | 2009-02-17 23:05:26 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 239 | // The fourth pass drops targets of ifuncs which are dead... |
David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 240 | std::vector<GlobalIFunc*> DeadIFuncs; |
| 241 | for (GlobalIFunc &GIF : M.ifuncs()) |
| 242 | if (!AliveGlobals.count(&GIF)) { |
| 243 | DeadIFuncs.push_back(&GIF); |
| 244 | GIF.setResolver(nullptr); |
| 245 | } |
| 246 | |
Peter Collingbourne | 7695cb6 | 2016-10-25 01:58:26 +0000 | [diff] [blame] | 247 | // Now that all interferences have been dropped, delete the actual objects |
| 248 | // themselves. |
| 249 | auto EraseUnusedGlobalValue = [&](GlobalValue *GV) { |
| 250 | RemoveUnusedGlobalValue(*GV); |
| 251 | GV->eraseFromParent(); |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 252 | Changed = true; |
Peter Collingbourne | 7695cb6 | 2016-10-25 01:58:26 +0000 | [diff] [blame] | 253 | }; |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 254 | |
Peter Collingbourne | 7695cb6 | 2016-10-25 01:58:26 +0000 | [diff] [blame] | 255 | NumFunctions += DeadFunctions.size(); |
| 256 | for (Function *F : DeadFunctions) |
| 257 | EraseUnusedGlobalValue(F); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 258 | |
Peter Collingbourne | 7695cb6 | 2016-10-25 01:58:26 +0000 | [diff] [blame] | 259 | NumVariables += DeadGlobalVars.size(); |
| 260 | for (GlobalVariable *GV : DeadGlobalVars) |
| 261 | EraseUnusedGlobalValue(GV); |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 262 | |
Peter Collingbourne | 7695cb6 | 2016-10-25 01:58:26 +0000 | [diff] [blame] | 263 | NumAliases += DeadAliases.size(); |
| 264 | for (GlobalAlias *GA : DeadAliases) |
| 265 | EraseUnusedGlobalValue(GA); |
| 266 | |
Peter Collingbourne | 4f3b2df | 2016-10-25 02:57:27 +0000 | [diff] [blame] | 267 | NumIFuncs += DeadIFuncs.size(); |
Peter Collingbourne | 7695cb6 | 2016-10-25 01:58:26 +0000 | [diff] [blame] | 268 | for (GlobalIFunc *GIF : DeadIFuncs) |
| 269 | EraseUnusedGlobalValue(GIF); |
David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 270 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 271 | // Make sure that all memory is released |
| 272 | AliveGlobals.clear(); |
Mehdi Amini | 888dee4 | 2017-01-27 19:48:57 +0000 | [diff] [blame] | 273 | ConstantDependenciesCache.clear(); |
| 274 | GVDependencies.clear(); |
Peter Collingbourne | 0dbc708 | 2015-03-19 18:23:29 +0000 | [diff] [blame] | 275 | ComdatMembers.clear(); |
Devang Patel | c5aa8c6 | 2009-08-11 06:31:57 +0000 | [diff] [blame] | 276 | |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 277 | if (Changed) |
| 278 | return PreservedAnalyses::none(); |
| 279 | return PreservedAnalyses::all(); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Reid Spencer | 5f68159 | 2004-07-18 00:25:04 +0000 | [diff] [blame] | 282 | // RemoveUnusedGlobalValue - Loop over all of the uses of the specified |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 283 | // GlobalValue, looking for the constant pointer ref that may be pointing to it. |
| 284 | // If found, check to see if the constant pointer ref is safe to destroy, and if |
| 285 | // so, nuke it. This will reduce the reference count on the global value, which |
| 286 | // might make it deader. |
| 287 | // |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 288 | bool GlobalDCEPass::RemoveUnusedGlobalValue(GlobalValue &GV) { |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 289 | if (GV.use_empty()) |
| 290 | return false; |
Chris Lattner | 9238d78 | 2004-07-18 07:22:58 +0000 | [diff] [blame] | 291 | GV.removeDeadConstantUsers(); |
| 292 | return GV.use_empty(); |
| 293 | } |