Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1 | //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 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" |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Pass.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 22 | #include "Support/Statistic.h" |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 23 | #include <set> |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 24 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 25 | namespace llvm { |
| 26 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 27 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 28 | Statistic<> NumFunctions("globaldce","Number of functions removed"); |
| 29 | Statistic<> NumVariables("globaldce","Number of global variables removed"); |
| 30 | Statistic<> NumCPRs("globaldce", "Number of const pointer refs removed"); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 32 | struct GlobalDCE : public Pass { |
| 33 | // run - Do the GlobalDCE pass on the specified module, optionally updating |
| 34 | // the specified callgraph to reflect the changes. |
| 35 | // |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 36 | bool run(Module &M); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 37 | |
| 38 | private: |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 39 | std::set<GlobalValue*> AliveGlobals; |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 40 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 41 | /// MarkGlobalIsNeeded - the specific global value as needed, and |
| 42 | /// recursively mark anything that it uses as also needed. |
| 43 | void GlobalIsNeeded(GlobalValue *GV); |
| 44 | void MarkUsedGlobalsAsNeeded(Constant *C); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 46 | bool RemoveUnusedConstantPointerRef(GlobalValue &GV); |
| 47 | bool SafeToDestroyConstant(Constant *C); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 48 | }; |
Chris Lattner | a2c0985 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 49 | RegisterOpt<GlobalDCE> X("globaldce", "Dead Global Elimination"); |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 52 | Pass *createGlobalDCEPass() { return new GlobalDCE(); } |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 53 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 54 | bool GlobalDCE::run(Module &M) { |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 55 | bool Changed = false; |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 56 | // Loop over the module, adding globals which are obviously necessary. |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 57 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 58 | Changed |= RemoveUnusedConstantPointerRef(*I); |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 59 | // Functions with external linkage are needed if they have a body |
Chris Lattner | c8dfbbb | 2003-09-20 19:00:50 +0000 | [diff] [blame] | 60 | if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) && |
| 61 | !I->isExternal()) |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 62 | GlobalIsNeeded(I); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 65 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 66 | Changed |= RemoveUnusedConstantPointerRef(*I); |
Chris Lattner | c8dfbbb | 2003-09-20 19:00:50 +0000 | [diff] [blame] | 67 | // Externally visible & appending globals are needed, if they have an |
| 68 | // initializer. |
| 69 | if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) && |
| 70 | !I->isExternal()) |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 71 | GlobalIsNeeded(I); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 74 | |
| 75 | // Now that all globals which are needed are in the AliveGlobals set, we loop |
| 76 | // through the program, deleting those which are not alive. |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 77 | // |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 78 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 79 | // The first pass is to drop initializers of global variables which are dead. |
| 80 | std::vector<GlobalVariable*> DeadGlobalVars; // Keep track of dead globals |
| 81 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 82 | if (!AliveGlobals.count(I)) { |
| 83 | DeadGlobalVars.push_back(I); // Keep track of dead globals |
| 84 | I->setInitializer(0); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | // The second pass drops the bodies of functions which are dead... |
| 89 | std::vector<Function*> DeadFunctions; |
| 90 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 91 | if (!AliveGlobals.count(I)) { |
| 92 | DeadFunctions.push_back(I); // Keep track of dead globals |
| 93 | if (!I->isExternal()) |
| 94 | I->deleteBody(); |
| 95 | } |
| 96 | |
| 97 | if (!DeadFunctions.empty()) { |
| 98 | // Now that all interreferences have been dropped, delete the actual objects |
| 99 | // themselves. |
| 100 | for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) { |
| 101 | RemoveUnusedConstantPointerRef(*DeadFunctions[i]); |
| 102 | M.getFunctionList().erase(DeadFunctions[i]); |
| 103 | } |
| 104 | NumFunctions += DeadFunctions.size(); |
| 105 | Changed = true; |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 108 | if (!DeadGlobalVars.empty()) { |
| 109 | for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) { |
| 110 | RemoveUnusedConstantPointerRef(*DeadGlobalVars[i]); |
| 111 | M.getGlobalList().erase(DeadGlobalVars[i]); |
| 112 | } |
| 113 | NumVariables += DeadGlobalVars.size(); |
| 114 | Changed = true; |
| 115 | } |
| 116 | |
| 117 | // Make sure that all memory is released |
| 118 | AliveGlobals.clear(); |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 119 | return Changed; |
| 120 | } |
| 121 | |
Chris Lattner | ec6d7a6 | 2003-09-16 19:27:31 +0000 | [diff] [blame] | 122 | /// MarkGlobalIsNeeded - the specific global value as needed, and |
| 123 | /// recursively mark anything that it uses as also needed. |
| 124 | void GlobalDCE::GlobalIsNeeded(GlobalValue *G) { |
| 125 | std::set<GlobalValue*>::iterator I = AliveGlobals.lower_bound(G); |
| 126 | |
| 127 | // If the global is already in the set, no need to reprocess it. |
| 128 | if (I != AliveGlobals.end() && *I == G) return; |
| 129 | |
| 130 | // Otherwise insert it now, so we do not infinitely recurse |
| 131 | AliveGlobals.insert(I, G); |
| 132 | |
| 133 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) { |
| 134 | // If this is a global variable, we must make sure to add any global values |
| 135 | // referenced by the initializer to the alive set. |
| 136 | if (GV->hasInitializer()) |
| 137 | MarkUsedGlobalsAsNeeded(GV->getInitializer()); |
| 138 | } else { |
| 139 | // Otherwise this must be a function object. We have to scan the body of |
| 140 | // the function looking for constants and global values which are used as |
| 141 | // operands. Any operands of these types must be processed to ensure that |
| 142 | // any globals used will be marked as needed. |
| 143 | Function *F = cast<Function>(G); |
| 144 | // For all basic blocks... |
| 145 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 146 | // For all instructions... |
| 147 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 148 | // For all operands... |
| 149 | for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U) |
| 150 | if (GlobalValue *GV = dyn_cast<GlobalValue>(*U)) |
| 151 | GlobalIsNeeded(GV); |
| 152 | else if (Constant *C = dyn_cast<Constant>(*U)) |
| 153 | MarkUsedGlobalsAsNeeded(C); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) { |
| 158 | if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) |
| 159 | GlobalIsNeeded(CPR->getValue()); |
| 160 | else { |
| 161 | // Loop over all of the operands of the constant, adding any globals they |
| 162 | // use to the list of needed globals. |
| 163 | for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) |
| 164 | MarkUsedGlobalsAsNeeded(cast<Constant>(*I)); |
| 165 | } |
| 166 | } |
Chris Lattner | 8f3acc6 | 2002-08-18 01:28:30 +0000 | [diff] [blame] | 167 | |
| 168 | // RemoveUnusedConstantPointerRef - Loop over all of the uses of the specified |
| 169 | // GlobalValue, looking for the constant pointer ref that may be pointing to it. |
| 170 | // If found, check to see if the constant pointer ref is safe to destroy, and if |
| 171 | // so, nuke it. This will reduce the reference count on the global value, which |
| 172 | // might make it deader. |
| 173 | // |
| 174 | bool GlobalDCE::RemoveUnusedConstantPointerRef(GlobalValue &GV) { |
| 175 | for (Value::use_iterator I = GV.use_begin(), E = GV.use_end(); I != E; ++I) |
| 176 | if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(*I)) |
| 177 | if (SafeToDestroyConstant(CPR)) { // Only if unreferenced... |
| 178 | CPR->destroyConstant(); |
| 179 | ++NumCPRs; |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | // SafeToDestroyConstant - It is safe to destroy a constant iff it is only used |
| 187 | // by constants itself. Note that constants cannot be cyclic, so this test is |
| 188 | // pretty easy to implement recursively. |
| 189 | // |
| 190 | bool GlobalDCE::SafeToDestroyConstant(Constant *C) { |
| 191 | for (Value::use_iterator I = C->use_begin(), E = C->use_end(); I != E; ++I) |
| 192 | if (Constant *User = dyn_cast<Constant>(*I)) { |
| 193 | if (!SafeToDestroyConstant(User)) return false; |
| 194 | } else { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | return true; |
| 199 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 200 | |
| 201 | } // End llvm namespace |