Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===- DCE.cpp - Code to perform dead code elimination --------------------===// |
| 2 | // |
| 3 | // This file implements dead code elimination and basic block merging. |
| 4 | // |
| 5 | // Specifically, this: |
| 6 | // * removes definitions with no uses (including unused constants) |
| 7 | // * removes basic blocks with no predecessors |
| 8 | // * merges a basic block into its predecessor if there is only one and the |
| 9 | // predecessor only has one successor. |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 10 | // * Eliminates PHI nodes for basic blocks with a single predecessor |
| 11 | // * Eliminates a basic block that only contains an unconditional branch |
Chris Lattner | ee7cb29 | 2001-07-28 17:51:49 +0000 | [diff] [blame] | 12 | // * Eliminates method prototypes that are not referenced |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | // |
Chris Lattner | 25d17a5 | 2001-06-29 05:24:28 +0000 | [diff] [blame] | 14 | // TODO: This should REALLY be worklist driven instead of iterative. Right now, |
| 15 | // we scan linearly through values, removing unused ones as we go. The problem |
| 16 | // is that this may cause other earlier values to become unused. To make sure |
| 17 | // that we get them all, we iterate until things stop changing. Instead, when |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | // removing a value, recheck all of its operands to see if they are now unused. |
Chris Lattner | d36c91c | 2001-06-13 19:55:22 +0000 | [diff] [blame] | 19 | // Piece of cake, and more efficient as well. |
| 20 | // |
| 21 | // Note, this is not trivial, because we have to worry about invalidating |
| 22 | // iterators. :( |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | // |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 26 | #include "llvm/Optimizations/DCE.h" |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 27 | #include "llvm/Support/STLExtras.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 28 | #include "llvm/Module.h" |
Chris Lattner | 5680ee6 | 2001-10-18 01:32:34 +0000 | [diff] [blame] | 29 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | #include "llvm/Method.h" |
| 31 | #include "llvm/BasicBlock.h" |
| 32 | #include "llvm/iTerminators.h" |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 33 | #include "llvm/iOther.h" |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 34 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 25d17a5 | 2001-06-29 05:24:28 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | |
Chris Lattner | a1f6e64 | 2001-11-01 07:00:27 +0000 | [diff] [blame] | 37 | // dceInstruction - Inspect the instruction at *BBI and figure out if it's |
| 38 | // [trivially] dead. If so, remove the instruction and update the iterator |
| 39 | // to point to the instruction that immediately succeeded the original |
| 40 | // instruction. |
| 41 | // |
| 42 | bool opt::DeadCodeElimination::dceInstruction(BasicBlock::InstListType &BBIL, |
| 43 | BasicBlock::iterator &BBI) { |
| 44 | // Look for un"used" definitions... |
| 45 | if ((*BBI)->use_empty() && !(*BBI)->hasSideEffects() && |
| 46 | !isa<TerminatorInst>(*BBI)) { |
| 47 | delete BBIL.remove(BBI); // Bye bye |
| 48 | return true; |
| 49 | } |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | static inline bool RemoveUnusedDefs(BasicBlock::InstListType &Vals) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 54 | bool Changed = false; |
Chris Lattner | edefaa1 | 2001-11-01 05:55:29 +0000 | [diff] [blame] | 55 | for (BasicBlock::InstListType::iterator DI = Vals.begin(); |
Chris Lattner | a1f6e64 | 2001-11-01 07:00:27 +0000 | [diff] [blame] | 56 | DI != Vals.end(); ) |
| 57 | if (opt::DeadCodeElimination::dceInstruction(Vals, DI)) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | Changed = true; |
Chris Lattner | a1f6e64 | 2001-11-01 07:00:27 +0000 | [diff] [blame] | 59 | else |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 60 | ++DI; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | return Changed; |
| 62 | } |
| 63 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 64 | // RemoveSingularPHIs - This removes PHI nodes from basic blocks that have only |
| 65 | // a single predecessor. This means that the PHI node must only have a single |
| 66 | // RHS value and can be eliminated. |
| 67 | // |
| 68 | // This routine is very simple because we know that PHI nodes must be the first |
| 69 | // things in a basic block, if they are present. |
| 70 | // |
| 71 | static bool RemoveSingularPHIs(BasicBlock *BB) { |
Chris Lattner | f0604b8 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 72 | BasicBlock::pred_iterator PI(BB->pred_begin()); |
| 73 | if (PI == BB->pred_end() || ++PI != BB->pred_end()) |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 74 | return false; // More than one predecessor... |
| 75 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 76 | Instruction *I = BB->front(); |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 77 | if (!isa<PHINode>(I)) return false; // No PHI nodes |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 78 | |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 79 | //cerr << "Killing PHIs from " << BB; |
Chris Lattner | f0604b8 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 80 | //cerr << "Pred #0 = " << *BB->pred_begin(); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 81 | |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 82 | //cerr << "Method == " << BB->getParent(); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 83 | |
| 84 | do { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 85 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 86 | assert(PN->getNumOperands() == 2 && "PHI node should only have one value!"); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 87 | Value *V = PN->getOperand(0); |
| 88 | |
| 89 | PN->replaceAllUsesWith(V); // Replace PHI node with its single value. |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 90 | delete BB->getInstList().remove(BB->begin()); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 92 | I = BB->front(); |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 93 | } while (isa<PHINode>(I)); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 94 | |
| 95 | return true; // Yes, we nuked at least one phi node |
| 96 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 98 | static void ReplaceUsesWithConstant(Instruction *I) { |
Chris Lattner | 2f11a9d | 2001-09-07 16:42:08 +0000 | [diff] [blame] | 99 | ConstPoolVal *CPV = ConstPoolVal::getNullConstant(I->getType()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 100 | |
| 101 | // Make all users of this instruction reference the constant instead |
| 102 | I->replaceAllUsesWith(CPV); |
| 103 | } |
| 104 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 105 | // PropogatePredecessors - This gets "Succ" ready to have the predecessors from |
| 106 | // "BB". This is a little tricky because "Succ" has PHI nodes, which need to |
| 107 | // have extra slots added to them to hold the merge edges from BB's |
| 108 | // predecessors. |
| 109 | // |
| 110 | // Assumption: BB is the single predecessor of Succ. |
| 111 | // |
| 112 | static void PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 113 | assert(isa<PHINode>(Succ->front()) && "Only works on PHId BBs!"); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 114 | |
| 115 | // If there is more than one predecessor, and there are PHI nodes in |
| 116 | // the successor, then we need to add incoming edges for the PHI nodes |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 117 | // |
Chris Lattner | f0604b8 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 118 | const vector<BasicBlock*> BBPreds(BB->pred_begin(), BB->pred_end()); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 119 | |
| 120 | BasicBlock::iterator I = Succ->begin(); |
| 121 | do { // Loop over all of the PHI nodes in the successor BB |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 122 | PHINode *PN = cast<PHINode>(*I); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 123 | Value *OldVal = PN->removeIncomingValue(BB); |
| 124 | assert(OldVal && "No entry in PHI for Pred BB!"); |
| 125 | |
| 126 | for (vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(), |
| 127 | End = BBPreds.end(); PredI != End; ++PredI) { |
| 128 | // Add an incoming value for each of the new incoming values... |
| 129 | PN->addIncoming(OldVal, *PredI); |
| 130 | } |
| 131 | |
| 132 | ++I; |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 133 | } while (isa<PHINode>(*I)); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 136 | |
| 137 | // SimplifyCFG - This function is used to do simplification of a CFG. For |
| 138 | // example, it adjusts branches to branches to eliminate the extra hop, it |
| 139 | // eliminates unreachable basic blocks, and does other "peephole" optimization |
| 140 | // of the CFG. It returns true if a modification was made, and returns an |
| 141 | // iterator that designates the first element remaining after the block that |
| 142 | // was deleted. |
| 143 | // |
| 144 | // WARNING: The entry node of a method may not be simplified. |
| 145 | // |
| 146 | bool opt::SimplifyCFG(Method::iterator &BBIt) { |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 147 | BasicBlock *BB = *BBIt; |
| 148 | Method *M = BB->getParent(); |
Chris Lattner | 2f11a9d | 2001-09-07 16:42:08 +0000 | [diff] [blame] | 149 | |
| 150 | assert(BB && BB->getParent() && "Block not embedded in method!"); |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 151 | assert(BB->getTerminator() && "Degenerate basic block encountered!"); |
| 152 | assert(BB->getParent()->front() != BB && "Can't Simplify entry block!"); |
| 153 | |
Chris Lattner | 2f11a9d | 2001-09-07 16:42:08 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 155 | // Remove basic blocks that have no predecessors... which are unreachable. |
Chris Lattner | f0604b8 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 156 | if (BB->pred_begin() == BB->pred_end() && |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 157 | !BB->hasConstantPoolReferences()) { |
| 158 | //cerr << "Removing BB: \n" << BB; |
| 159 | |
| 160 | // Loop through all of our successors and make sure they know that one |
| 161 | // of their predecessors is going away. |
Chris Lattner | f0604b8 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 162 | for_each(BB->succ_begin(), BB->succ_end(), |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 163 | std::bind2nd(std::mem_fun(&BasicBlock::removePredecessor), BB)); |
| 164 | |
| 165 | while (!BB->empty()) { |
| 166 | Instruction *I = BB->back(); |
| 167 | // If this instruction is used, replace uses with an arbitrary |
| 168 | // constant value. Because control flow can't get here, we don't care |
| 169 | // what we replace the value with. Note that since this block is |
| 170 | // unreachable, and all values contained within it must dominate their |
| 171 | // uses, that all uses will eventually be removed. |
| 172 | if (!I->use_empty()) ReplaceUsesWithConstant(I); |
| 173 | |
| 174 | // Remove the instruction from the basic block |
| 175 | delete BB->getInstList().pop_back(); |
| 176 | } |
| 177 | delete M->getBasicBlocks().remove(BBIt); |
| 178 | return true; |
Chris Lattner | 2f11a9d | 2001-09-07 16:42:08 +0000 | [diff] [blame] | 179 | } |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 180 | |
| 181 | // Check to see if this block has no instructions and only a single |
| 182 | // successor. If so, replace block references with successor. |
Chris Lattner | f0604b8 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 183 | BasicBlock::succ_iterator SI(BB->succ_begin()); |
| 184 | if (SI != BB->succ_end() && ++SI == BB->succ_end()) { // One succ? |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 185 | Instruction *I = BB->front(); |
| 186 | if (I->isTerminator()) { // Terminator is the only instruction! |
Chris Lattner | f0604b8 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 187 | BasicBlock *Succ = *BB->succ_begin(); // There is exactly one successor |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 188 | //cerr << "Killing Trivial BB: \n" << BB; |
| 189 | |
| 190 | if (Succ != BB) { // Arg, don't hurt infinite loops! |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 191 | if (isa<PHINode>(Succ->front())) { |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 192 | // If our successor has PHI nodes, then we need to update them to |
| 193 | // include entries for BB's predecessors, not for BB itself. |
| 194 | // |
| 195 | PropogatePredecessorsForPHIs(BB, Succ); |
| 196 | } |
| 197 | |
| 198 | BB->replaceAllUsesWith(Succ); |
| 199 | BB = M->getBasicBlocks().remove(BBIt); |
| 200 | |
| 201 | if (BB->hasName() && !Succ->hasName()) // Transfer name if we can |
| 202 | Succ->setName(BB->getName()); |
| 203 | delete BB; // Delete basic block |
| 204 | |
| 205 | //cerr << "Method after removal: \n" << M; |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // Merge basic blocks into their predecessor if there is only one pred, |
| 212 | // and if there is only one successor of the predecessor. |
Chris Lattner | f0604b8 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 213 | BasicBlock::pred_iterator PI(BB->pred_begin()); |
| 214 | if (PI != BB->pred_end() && *PI != BB && // Not empty? Not same BB? |
| 215 | ++PI == BB->pred_end() && !BB->hasConstantPoolReferences()) { |
| 216 | BasicBlock *Pred = *BB->pred_begin(); |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 217 | TerminatorInst *Term = Pred->getTerminator(); |
| 218 | assert(Term != 0 && "malformed basic block without terminator!"); |
| 219 | |
| 220 | // Does the predecessor block only have a single successor? |
Chris Lattner | f0604b8 | 2001-10-01 13:19:53 +0000 | [diff] [blame] | 221 | BasicBlock::succ_iterator SI(Pred->succ_begin()); |
| 222 | if (++SI == Pred->succ_end()) { |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 223 | //cerr << "Merging: " << BB << "into: " << Pred; |
| 224 | |
| 225 | // Delete the unconditianal branch from the predecessor... |
| 226 | BasicBlock::iterator DI = Pred->end(); |
| 227 | assert(Pred->getTerminator() && |
| 228 | "Degenerate basic block encountered!"); // Empty bb??? |
| 229 | delete Pred->getInstList().remove(--DI); // Destroy uncond branch |
| 230 | |
| 231 | // Move all definitions in the succecessor to the predecessor... |
| 232 | while (!BB->empty()) { |
| 233 | DI = BB->begin(); |
| 234 | Instruction *Def = BB->getInstList().remove(DI); // Remove from front |
| 235 | Pred->getInstList().push_back(Def); // Add to end... |
| 236 | } |
| 237 | |
| 238 | // Remove basic block from the method... and advance iterator to the |
| 239 | // next valid block... |
| 240 | BB = M->getBasicBlocks().remove(BBIt); |
| 241 | |
| 242 | // Make all PHI nodes that refered to BB now refer to Pred as their |
| 243 | // source... |
| 244 | BB->replaceAllUsesWith(Pred); |
| 245 | |
| 246 | // Inherit predecessors name if it exists... |
| 247 | if (BB->hasName() && !Pred->hasName()) Pred->setName(BB->getName()); |
| 248 | |
| 249 | delete BB; // You ARE the weakest link... goodbye |
| 250 | return true; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return false; |
| 255 | } |
| 256 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 257 | static bool DoDCEPass(Method *M) { |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 258 | Method::iterator BBIt, BBEnd = M->end(); |
| 259 | if (M->begin() == BBEnd) return false; // Nothing to do |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 260 | bool Changed = false; |
| 261 | |
| 262 | // Loop through now and remove instructions that have no uses... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 263 | for (BBIt = M->begin(); BBIt != BBEnd; ++BBIt) { |
Chris Lattner | edefaa1 | 2001-11-01 05:55:29 +0000 | [diff] [blame] | 264 | Changed |= RemoveUnusedDefs((*BBIt)->getInstList()); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 265 | Changed |= RemoveSingularPHIs(*BBIt); |
| 266 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 267 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 268 | // Loop over all of the basic blocks (except the first one) and remove them |
| 269 | // if they are unneeded... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 270 | // |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 271 | for (BBIt = M->begin(), ++BBIt; BBIt != M->end(); ) { |
| 272 | if (opt::SimplifyCFG(BBIt)) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 273 | Changed = true; |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 274 | } else { |
| 275 | ++BBIt; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Chris Lattner | 2f11a9d | 2001-09-07 16:42:08 +0000 | [diff] [blame] | 279 | return Changed; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
| 283 | // It is possible that we may require multiple passes over the code to fully |
| 284 | // eliminate dead code. Iterate until we are done. |
| 285 | // |
Chris Lattner | 5680ee6 | 2001-10-18 01:32:34 +0000 | [diff] [blame] | 286 | bool opt::DeadCodeElimination::doDCE(Method *M) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 287 | bool Changed = false; |
| 288 | while (DoDCEPass(M)) Changed = true; |
| 289 | return Changed; |
| 290 | } |
| 291 | |
Chris Lattner | 5680ee6 | 2001-10-18 01:32:34 +0000 | [diff] [blame] | 292 | bool opt::DeadCodeElimination::RemoveUnusedGlobalValues(Module *Mod) { |
Chris Lattner | ee7cb29 | 2001-07-28 17:51:49 +0000 | [diff] [blame] | 293 | bool Changed = false; |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 294 | |
Chris Lattner | ee7cb29 | 2001-07-28 17:51:49 +0000 | [diff] [blame] | 295 | for (Module::iterator MI = Mod->begin(); MI != Mod->end(); ) { |
| 296 | Method *Meth = *MI; |
Chris Lattner | 5680ee6 | 2001-10-18 01:32:34 +0000 | [diff] [blame] | 297 | if (Meth->isExternal() && Meth->use_size() == 0) { |
| 298 | // No references to prototype? |
Chris Lattner | ee7cb29 | 2001-07-28 17:51:49 +0000 | [diff] [blame] | 299 | //cerr << "Removing method proto: " << Meth->getName() << endl; |
| 300 | delete Mod->getMethodList().remove(MI); // Remove prototype |
| 301 | // Remove moves iterator to point to the next one automatically |
Chris Lattner | 5680ee6 | 2001-10-18 01:32:34 +0000 | [diff] [blame] | 302 | Changed = true; |
Chris Lattner | ee7cb29 | 2001-07-28 17:51:49 +0000 | [diff] [blame] | 303 | } else { |
| 304 | ++MI; // Skip prototype in use. |
| 305 | } |
| 306 | } |
| 307 | |
Chris Lattner | 5680ee6 | 2001-10-18 01:32:34 +0000 | [diff] [blame] | 308 | for (Module::giterator GI = Mod->gbegin(); GI != Mod->gend(); ) { |
| 309 | GlobalVariable *GV = *GI; |
| 310 | if (!GV->hasInitializer() && GV->use_size() == 0) { |
| 311 | // No references to uninitialized global variable? |
| 312 | //cerr << "Removing global var: " << GV->getName() << endl; |
| 313 | delete Mod->getGlobalList().remove(GI); |
| 314 | // Remove moves iterator to point to the next one automatically |
| 315 | Changed = true; |
| 316 | } else { |
| 317 | ++GI; |
| 318 | } |
| 319 | } |
| 320 | |
Chris Lattner | ee7cb29 | 2001-07-28 17:51:49 +0000 | [diff] [blame] | 321 | return Changed; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 322 | } |