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 | |
Chris Lattner | 99a53f6 | 2002-07-24 17:12:05 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/IPO.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" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/CtorUtils.h" |
Bruno Cardoso Lopes | e2a1fa3 | 2014-08-25 17:51:14 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Peter Collingbourne | 0dbc708 | 2015-03-19 18:23:29 +0000 | [diff] [blame] | 27 | #include <unordered_map> |
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"); |
| 34 | STATISTIC(NumVariables, "Number of global variables removed"); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 36 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 37 | struct GlobalDCE : public ModulePass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 38 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 39 | GlobalDCE() : ModulePass(ID) { |
| 40 | initializeGlobalDCEPass(*PassRegistry::getPassRegistry()); |
| 41 | } |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 43 | // run - Do the GlobalDCE pass on the specified module, optionally updating |
| 44 | // the specified callgraph to reflect the changes. |
| 45 | // |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 46 | bool runOnModule(Module &M) override; |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 47 | |
| 48 | private: |
Pete Cooper | 91e4ba2 | 2014-07-08 17:06:03 +0000 | [diff] [blame] | 49 | SmallPtrSet<GlobalValue*, 32> AliveGlobals; |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 50 | SmallPtrSet<Constant *, 8> SeenConstants; |
Peter Collingbourne | 0dbc708 | 2015-03-19 18:23:29 +0000 | [diff] [blame] | 51 | std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers; |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 52 | |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 53 | /// GlobalIsNeeded - mark the specific global value as needed, and |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 54 | /// recursively mark anything that it uses as also needed. |
| 55 | void GlobalIsNeeded(GlobalValue *GV); |
| 56 | void MarkUsedGlobalsAsNeeded(Constant *C); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 57 | |
Reid Spencer | 5f68159 | 2004-07-18 00:25:04 +0000 | [diff] [blame] | 58 | bool RemoveUnusedGlobalValue(GlobalValue &GV); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 59 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 60 | } |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 61 | |
| 62 | /// Returns true if F contains only a single "ret" instruction. |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 63 | static bool isEmptyFunction(Function *F) { |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 64 | BasicBlock &Entry = F->getEntryBlock(); |
| 65 | if (Entry.size() != 1 || !isa<ReturnInst>(Entry.front())) |
| 66 | return false; |
| 67 | ReturnInst &RI = cast<ReturnInst>(Entry.front()); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 68 | return RI.getReturnValue() == nullptr; |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 69 | } |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 70 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 71 | char GlobalDCE::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 72 | INITIALIZE_PASS(GlobalDCE, "globaldce", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 73 | "Dead Global Elimination", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 75 | ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); } |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 77 | bool GlobalDCE::runOnModule(Module &M) { |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 78 | bool Changed = false; |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 79 | |
| 80 | // Remove empty functions from the global ctors list. |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 81 | Changed |= optimizeGlobalCtorsList(M, isEmptyFunction); |
Nico Weber | 4b2acde | 2014-05-02 18:35:25 +0000 | [diff] [blame] | 82 | |
Peter Collingbourne | 0dbc708 | 2015-03-19 18:23:29 +0000 | [diff] [blame] | 83 | // 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 Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 94 | // Loop over the module, adding globals which are obviously necessary. |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 95 | for (Function &F : M) { |
| 96 | Changed |= RemoveUnusedGlobalValue(F); |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 97 | // Functions with external linkage are needed if they have a body |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 98 | if (!F.isDeclaration() && !F.hasAvailableExternallyLinkage()) |
| 99 | if (!F.isDiscardableIfUnused()) |
| 100 | GlobalIsNeeded(&F); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 101 | } |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 102 | |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 103 | for (GlobalVariable &GV : M.globals()) { |
| 104 | Changed |= RemoveUnusedGlobalValue(GV); |
Chris Lattner | c8dfbbb | 2003-09-20 19:00:50 +0000 | [diff] [blame] | 105 | // Externally visible & appending globals are needed, if they have an |
| 106 | // initializer. |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 107 | if (!GV.isDeclaration() && !GV.hasAvailableExternallyLinkage()) |
| 108 | if (!GV.isDiscardableIfUnused()) |
| 109 | GlobalIsNeeded(&GV); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 112 | for (GlobalAlias &GA : M.aliases()) { |
| 113 | Changed |= RemoveUnusedGlobalValue(GA); |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 114 | // Externally visible aliases are needed. |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 115 | if (!GA.isDiscardableIfUnused()) |
| 116 | GlobalIsNeeded(&GA); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 119 | // Now that all globals which are needed are in the AliveGlobals set, we loop |
| 120 | // through the program, deleting those which are not alive. |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 121 | // |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 122 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 123 | // 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] | 124 | std::vector<GlobalVariable *> DeadGlobalVars; // Keep track of dead globals |
| 125 | for (GlobalVariable &GV : M.globals()) |
| 126 | if (!AliveGlobals.count(&GV)) { |
| 127 | DeadGlobalVars.push_back(&GV); // Keep track of dead globals |
| 128 | if (GV.hasInitializer()) { |
| 129 | Constant *Init = GV.getInitializer(); |
| 130 | GV.setInitializer(nullptr); |
Bruno Cardoso Lopes | e2a1fa3 | 2014-08-25 17:51:14 +0000 | [diff] [blame] | 131 | if (isSafeToDestroyConstant(Init)) |
| 132 | Init->destroyConstant(); |
| 133 | } |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 136 | // The second pass drops the bodies of functions which are dead... |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 137 | std::vector<Function *> DeadFunctions; |
| 138 | for (Function &F : M) |
| 139 | if (!AliveGlobals.count(&F)) { |
| 140 | DeadFunctions.push_back(&F); // Keep track of dead globals |
| 141 | if (!F.isDeclaration()) |
| 142 | F.deleteBody(); |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Duncan Sands | bf3ba5a | 2009-02-17 23:05:26 +0000 | [diff] [blame] | 145 | // The third pass drops targets of aliases which are dead... |
| 146 | std::vector<GlobalAlias*> DeadAliases; |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 147 | for (GlobalAlias &GA : M.aliases()) |
| 148 | if (!AliveGlobals.count(&GA)) { |
| 149 | DeadAliases.push_back(&GA); |
| 150 | GA.setAliasee(nullptr); |
Duncan Sands | bf3ba5a | 2009-02-17 23:05:26 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 153 | if (!DeadFunctions.empty()) { |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 154 | // Now that all interferences have been dropped, delete the actual objects |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 155 | // themselves. |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 156 | for (Function *F : DeadFunctions) { |
| 157 | RemoveUnusedGlobalValue(*F); |
| 158 | M.getFunctionList().erase(F); |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 159 | } |
| 160 | NumFunctions += DeadFunctions.size(); |
| 161 | Changed = true; |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 164 | if (!DeadGlobalVars.empty()) { |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 165 | for (GlobalVariable *GV : DeadGlobalVars) { |
| 166 | RemoveUnusedGlobalValue(*GV); |
| 167 | M.getGlobalList().erase(GV); |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 168 | } |
| 169 | NumVariables += DeadGlobalVars.size(); |
| 170 | Changed = true; |
| 171 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 172 | |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 173 | // Now delete any dead aliases. |
Duncan Sands | bf3ba5a | 2009-02-17 23:05:26 +0000 | [diff] [blame] | 174 | if (!DeadAliases.empty()) { |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 175 | for (GlobalAlias *GA : DeadAliases) { |
| 176 | RemoveUnusedGlobalValue(*GA); |
| 177 | M.getAliasList().erase(GA); |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 178 | } |
Duncan Sands | bf3ba5a | 2009-02-17 23:05:26 +0000 | [diff] [blame] | 179 | NumAliases += DeadAliases.size(); |
| 180 | Changed = true; |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 183 | // Make sure that all memory is released |
| 184 | AliveGlobals.clear(); |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 185 | SeenConstants.clear(); |
Peter Collingbourne | 0dbc708 | 2015-03-19 18:23:29 +0000 | [diff] [blame] | 186 | ComdatMembers.clear(); |
Devang Patel | c5aa8c6 | 2009-08-11 06:31:57 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 188 | return Changed; |
| 189 | } |
| 190 | |
Devang Patel | 95b1812 | 2008-11-11 19:16:41 +0000 | [diff] [blame] | 191 | /// GlobalIsNeeded - the specific global value as needed, and |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 192 | /// recursively mark anything that it uses as also needed. |
| 193 | void GlobalDCE::GlobalIsNeeded(GlobalValue *G) { |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 194 | // If the global is already in the set, no need to reprocess it. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 195 | if (!AliveGlobals.insert(G).second) |
Chris Lattner | b5d9c8c | 2009-11-01 19:03:42 +0000 | [diff] [blame] | 196 | return; |
David Majnemer | e025321 | 2014-10-07 07:07:19 +0000 | [diff] [blame] | 197 | |
David Majnemer | e025321 | 2014-10-07 07:07:19 +0000 | [diff] [blame] | 198 | if (Comdat *C = G->getComdat()) { |
Peter Collingbourne | 0dbc708 | 2015-03-19 18:23:29 +0000 | [diff] [blame] | 199 | for (auto &&CM : make_range(ComdatMembers.equal_range(C))) |
| 200 | GlobalIsNeeded(CM.second); |
David Majnemer | e025321 | 2014-10-07 07:07:19 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 203 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) { |
| 204 | // If this is a global variable, we must make sure to add any global values |
| 205 | // referenced by the initializer to the alive set. |
Pete Cooper | 91e4ba2 | 2014-07-08 17:06:03 +0000 | [diff] [blame] | 206 | if (GV->hasInitializer()) |
| 207 | MarkUsedGlobalsAsNeeded(GV->getInitializer()); |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 208 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) { |
| 209 | // The target of a global alias is needed. |
Duncan Sands | 187c571 | 2009-01-07 18:45:53 +0000 | [diff] [blame] | 210 | MarkUsedGlobalsAsNeeded(GA->getAliasee()); |
Duncan Sands | f5dbbae | 2009-01-05 20:37:33 +0000 | [diff] [blame] | 211 | } else { |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 212 | // Otherwise this must be a function object. We have to scan the body of |
| 213 | // the function looking for constants and global values which are used as |
| 214 | // operands. Any operands of these types must be processed to ensure that |
| 215 | // any globals used will be marked as needed. |
| 216 | Function *F = cast<Function>(G); |
Chris Lattner | b5d9c8c | 2009-11-01 19:03:42 +0000 | [diff] [blame] | 217 | |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 218 | if (F->hasPrefixData()) |
| 219 | MarkUsedGlobalsAsNeeded(F->getPrefixData()); |
| 220 | |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 221 | if (F->hasPrologueData()) |
| 222 | MarkUsedGlobalsAsNeeded(F->getPrologueData()); |
| 223 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 224 | if (F->hasPersonalityFn()) |
| 225 | MarkUsedGlobalsAsNeeded(F->getPersonalityFn()); |
| 226 | |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 227 | for (BasicBlock &BB : *F) |
| 228 | for (Instruction &I : BB) |
| 229 | for (Use &U : I.operands()) |
| 230 | if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 231 | GlobalIsNeeded(GV); |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 232 | else if (Constant *C = dyn_cast<Constant>(U)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 233 | MarkUsedGlobalsAsNeeded(C); |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) { |
Reid Spencer | 5f68159 | 2004-07-18 00:25:04 +0000 | [diff] [blame] | 238 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Chris Lattner | ee8b951 | 2009-10-29 01:21:20 +0000 | [diff] [blame] | 239 | return GlobalIsNeeded(GV); |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 240 | |
Chris Lattner | ee8b951 | 2009-10-29 01:21:20 +0000 | [diff] [blame] | 241 | // Loop over all of the operands of the constant, adding any globals they |
| 242 | // use to the list of needed globals. |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 243 | for (Use &U : C->operands()) { |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 244 | // If we've already processed this constant there's no need to do it again. |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 245 | Constant *Op = dyn_cast<Constant>(U); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 246 | if (Op && SeenConstants.insert(Op).second) |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 247 | MarkUsedGlobalsAsNeeded(Op); |
| 248 | } |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 250 | |
Reid Spencer | 5f68159 | 2004-07-18 00:25:04 +0000 | [diff] [blame] | 251 | // RemoveUnusedGlobalValue - Loop over all of the uses of the specified |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 252 | // 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 Spencer | 5f68159 | 2004-07-18 00:25:04 +0000 | [diff] [blame] | 257 | bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) { |
Yaron Keren | 3d49f6d | 2015-07-18 19:57:34 +0000 | [diff] [blame] | 258 | if (GV.use_empty()) |
| 259 | return false; |
Chris Lattner | 9238d78 | 2004-07-18 07:22:58 +0000 | [diff] [blame] | 260 | GV.removeDeadConstantUsers(); |
| 261 | return GV.use_empty(); |
| 262 | } |