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