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 | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | // |
| 13 | // TODO: This should REALLY be recursive instead of iterative. Right now, we |
| 14 | // scan linearly through values, removing unused ones as we go. The problem is |
| 15 | // that this may cause other earlier values to become unused. To make sure that |
| 16 | // we get them all, we iterate until things stop changing. Instead, when |
| 17 | // removing a value, recheck all of its operands to see if they are now unused. |
| 18 | // Piece of cake, and more efficient as well. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Method.h" |
| 24 | #include "llvm/BasicBlock.h" |
| 25 | #include "llvm/iTerminators.h" |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 26 | #include "llvm/iOther.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | #include "llvm/Opt/AllOpts.h" |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 28 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | |
| 30 | struct ConstPoolDCE { |
| 31 | enum { EndOffs = 0 }; |
| 32 | static bool isDCEable(const Value *) { return true; } |
| 33 | }; |
| 34 | |
| 35 | struct BasicBlockDCE { |
| 36 | enum { EndOffs = 1 }; |
| 37 | static bool isDCEable(const Instruction *I) { |
| 38 | return !I->hasSideEffects(); |
| 39 | } |
| 40 | }; |
| 41 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 42 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | template<class ValueSubclass, class ItemParentType, class DCEController> |
| 44 | static bool RemoveUnusedDefs(ValueHolder<ValueSubclass, ItemParentType> &Vals, |
| 45 | DCEController DCEControl) { |
| 46 | bool Changed = false; |
| 47 | typedef ValueHolder<ValueSubclass, ItemParentType> Container; |
| 48 | |
| 49 | int Offset = DCEController::EndOffs; |
| 50 | for (Container::iterator DI = Vals.begin(); DI != Vals.end()-Offset; ) { |
| 51 | // Look for un"used" definitions... |
| 52 | if ((*DI)->use_empty() && DCEController::isDCEable(*DI)) { |
| 53 | // Bye bye |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 54 | //cerr << "Removing: " << *DI; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 55 | delete Vals.remove(DI); |
| 56 | Changed = true; |
| 57 | } else { |
| 58 | DI++; |
| 59 | } |
| 60 | } |
| 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) { |
| 72 | BasicBlock::pred_iterator PI(BB->pred_begin()); |
| 73 | if (PI == BB->pred_end() || ++PI != BB->pred_end()) |
| 74 | return false; // More than one predecessor... |
| 75 | |
| 76 | Instruction *I = BB->getInstList().front(); |
| 77 | if (I->getInstType() != Instruction::PHINode) return false; // No PHI nodes |
| 78 | |
| 79 | cerr << "Killing PHIs from " << BB; |
| 80 | cerr << "Pred #0 = " << *BB->pred_begin(); |
| 81 | |
| 82 | cerr << "Method == " << BB->getParent(); |
| 83 | |
| 84 | do { |
| 85 | PHINode *PN = (PHINode*)I; |
| 86 | assert(PN->getOperand(1) == 0 && "PHI node should only have one value!"); |
| 87 | Value *V = PN->getOperand(0); |
| 88 | |
| 89 | PN->replaceAllUsesWith(V); // Replace PHI node with its single value. |
| 90 | delete BB->getInstList().remove(BB->getInstList().begin()); |
| 91 | |
| 92 | I = BB->getInstList().front(); |
| 93 | } while (I->getInstType() == Instruction::PHINode); |
| 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 | |
| 98 | bool DoRemoveUnusedConstants(SymTabValue *S) { |
| 99 | bool Changed = false; |
| 100 | ConstantPool &CP = S->getConstantPool(); |
| 101 | for (ConstantPool::plane_iterator PI = CP.begin(); PI != CP.end(); ++PI) |
| 102 | Changed |= RemoveUnusedDefs(**PI, ConstPoolDCE()); |
| 103 | return Changed; |
| 104 | } |
| 105 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 106 | static void ReplaceUsesWithConstant(Instruction *I) { |
| 107 | // Get the method level constant pool |
| 108 | ConstantPool &CP = I->getParent()->getParent()->getConstantPool(); |
| 109 | |
| 110 | ConstPoolVal *CPV = 0; |
| 111 | ConstantPool::PlaneType *P; |
| 112 | if (!CP.getPlane(I->getType(), P)) { // Does plane exist? |
| 113 | // Yes, is it empty? |
| 114 | if (!P->empty()) CPV = P->front(); |
| 115 | } |
| 116 | |
| 117 | if (CPV == 0) { // We don't have an existing constant to reuse. Just add one. |
| 118 | CPV = ConstPoolVal::getNullConstant(I->getType()); // Create a new constant |
| 119 | |
| 120 | // Add the new value to the constant pool... |
| 121 | CP.insert(CPV); |
| 122 | } |
| 123 | |
| 124 | // Make all users of this instruction reference the constant instead |
| 125 | I->replaceAllUsesWith(CPV); |
| 126 | } |
| 127 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 128 | // RemovePredecessorFromBlock - This function is called when we are about |
| 129 | // to remove a predecessor from a basic block. This function takes care of |
| 130 | // removing the predecessor from the PHI nodes in BB so that after the pred |
| 131 | // is removed, the number of PHI slots per bb is equal to the number of |
| 132 | // predecessors. |
| 133 | // |
| 134 | static void RemovePredecessorFromBlock(BasicBlock *BB, BasicBlock *Pred) { |
| 135 | BasicBlock::pred_iterator PI(BB->pred_begin()), EI(BB->pred_end()); |
| 136 | unsigned pred_idx = 0, max_idx; |
| 137 | |
| 138 | cerr << "RPFB: " << Pred << "From Block: " << BB; |
| 139 | |
| 140 | // Find out what index the predecessor is... |
| 141 | for (; *PI != BB; ++PI, ++pred_idx) { |
| 142 | assert(PI != EI && "Pred is not a predecessor of BB!"); |
| 143 | } |
| 144 | |
| 145 | // Loop over the rest of the predecssors until we run out, or until we find |
| 146 | // out that there are more than 2 predecessors. |
| 147 | for (max_idx = pred_idx; PI != EI && max_idx < 2; ++PI, ++max_idx) /*empty*/; |
| 148 | |
| 149 | // If there are exactly two predecessors, then we want to nuke the PHI nodes |
| 150 | // altogether. |
| 151 | bool NukePHIs = max_idx == 1; |
| 152 | |
| 153 | // Okay, now we know that we need to remove predecessor #pred_idx from all |
| 154 | // PHI nodes. Iterate over each PHI node fixing them up |
| 155 | BasicBlock::InstListType::iterator II(BB->getInstList().begin()); |
| 156 | for (; (*II)->getInstType() == Instruction::PHINode; ++II) { |
| 157 | PHINode *PN = (PHINode*)*II; |
| 158 | PN->removeIncomingValue(pred_idx); |
| 159 | |
| 160 | if (NukePHIs) { // Destroy the PHI altogether?? |
| 161 | assert(PN->getOperand(1) == 0 && "PHI node should only have one value!"); |
| 162 | Value *V = PN->getOperand(0); |
| 163 | |
| 164 | PN->replaceAllUsesWith(V); // Replace PHI node with its single value. |
| 165 | delete BB->getInstList().remove(II); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // PropogatePredecessors - This gets "Succ" ready to have the predecessors from |
| 171 | // "BB". This is a little tricky because "Succ" has PHI nodes, which need to |
| 172 | // have extra slots added to them to hold the merge edges from BB's |
| 173 | // predecessors. |
| 174 | // |
| 175 | // Assumption: BB is the single predecessor of Succ. |
| 176 | // |
| 177 | static void PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { |
| 178 | assert(BB && Succ && *Succ->pred_begin() == BB && "BB is only pred of Succ" && |
| 179 | ++Succ->pred_begin() == Succ->pred_end()); |
| 180 | |
| 181 | // If there is more than one predecessor, and there are PHI nodes in |
| 182 | // the successor, then we need to add incoming edges for the PHI nodes |
| 183 | BasicBlock::pred_iterator PI(BB->pred_begin()); |
| 184 | for (; PI != BB->pred_end(); ++PI) { |
| 185 | // TODO: |
| 186 | } |
| 187 | } |
| 188 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 189 | static bool DoDCEPass(Method *M) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 190 | Method::BasicBlocksType &BBs = M->getBasicBlocks(); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 191 | Method::BasicBlocksType::iterator BBIt, BBEnd = BBs.end(); |
| 192 | if (BBs.begin() == BBEnd) return false; // Nothing to do |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 193 | bool Changed = false; |
| 194 | |
| 195 | // Loop through now and remove instructions that have no uses... |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 196 | for (BBIt = BBs.begin(); BBIt != BBEnd; BBIt++) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 197 | Changed |= RemoveUnusedDefs((*BBIt)->getInstList(), BasicBlockDCE()); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 198 | Changed |= RemoveSingularPHIs(*BBIt); |
| 199 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 200 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 201 | // Loop over all of the basic blocks (except the first one) and remove them |
| 202 | // if they are unneeded... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 203 | // |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 204 | for (BBIt = BBs.begin(), ++BBIt; BBIt != BBs.end(); ++BBIt) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | BasicBlock *BB = *BBIt; |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 206 | assert(BB->getTerminator() && "Degenerate basic block encountered!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 207 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 208 | #if 0 |
| 209 | // Remove basic blocks that have no predecessors... which are unreachable. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 210 | if (BB->pred_begin() == BB->pred_end() && |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 211 | !BB->hasConstantPoolReferences() && 0) { |
| 212 | cerr << "Removing BB: \n" << BB; |
| 213 | |
| 214 | // Loop through all of our successors and make sure they know that one |
| 215 | // of their predecessors is going away. |
| 216 | for (BasicBlock::succ_iterator SI = BB->succ_begin(), EI = BB->succ_end(); |
| 217 | SI != EI; ++SI) |
| 218 | RemovePredecessorFromBlock(*SI, BB); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 219 | |
| 220 | while (!BB->getInstList().empty()) { |
| 221 | Instruction *I = BB->getInstList().front(); |
| 222 | // If this instruction is used, replace uses with an arbitrary |
| 223 | // constant value. Because control flow can't get here, we don't care |
| 224 | // what we replace the value with. |
| 225 | if (!I->use_empty()) ReplaceUsesWithConstant(I); |
| 226 | |
| 227 | // Remove the instruction from the basic block |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 228 | delete BB->getInstList().remove(BB->getInstList().begin()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 229 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 230 | delete BBs.remove(BBIt); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 231 | --BBIt; // remove puts use on the next block, we want the previous one |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 232 | Changed = true; |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 233 | continue; |
| 234 | } |
| 235 | |
| 236 | // Check to see if this block has no instructions and only a single |
| 237 | // successor. If so, replace block references with successor. |
| 238 | BasicBlock::succ_iterator SI(BB->succ_begin()); |
| 239 | if (SI != BB->succ_end() && ++SI == BB->succ_end()) { // One succ? |
| 240 | Instruction *I = BB->getInstList().front(); |
| 241 | if (I->isTerminator()) { // Terminator is the only instruction! |
| 242 | |
| 243 | if (Succ->getInstList().front()->getInstType() == Instruction::PHINode){ |
| 244 | // Add entries to the PHI nodes so that the PHI nodes have the right |
| 245 | // number of entries... |
| 246 | PropogatePredecessorsForPHIs(BB, Succ); |
| 247 | } |
| 248 | |
| 249 | BasicBlock *Succ = *BB->succ_begin(); // There is exactly one successor |
| 250 | BB->replaceAllUsesWith(Succ); |
| 251 | cerr << "Killing Trivial BB: \n" << BB; |
| 252 | |
| 253 | BB = BBs.remove(BBIt); |
| 254 | --BBIt; // remove puts use on the next block, we want the previous one |
| 255 | |
| 256 | if (BB->hasName() && !Succ->hasName()) // Transfer name if we can |
| 257 | Succ->setName(BB->getName()); |
| 258 | delete BB; // Delete basic block |
| 259 | |
| 260 | cerr << "Method after removal: \n" << M; |
| 261 | Changed = true; |
| 262 | continue; |
| 263 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 264 | } |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 265 | #endif |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 266 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 267 | // Merge basic blocks into their predecessor if there is only one pred, |
| 268 | // and if there is only one successor of the predecessor. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 269 | BasicBlock::pred_iterator PI(BB->pred_begin()); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 270 | if (PI != BB->pred_end() && *PI != BB && // Not empty? Not same BB? |
| 271 | ++PI == BB->pred_end() && !BB->hasConstantPoolReferences()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 272 | BasicBlock *Pred = *BB->pred_begin(); |
| 273 | TerminatorInst *Term = Pred->getTerminator(); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 274 | assert(Term != 0 && "malformed basic block without terminator!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 275 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 276 | // Does the predecessor block only have a single successor? |
| 277 | BasicBlock::succ_iterator SI(Pred->succ_begin()); |
| 278 | if (++SI == Pred->succ_end()) { |
| 279 | //cerr << "Merging: " << BB << "into: " << Pred; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 280 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame^] | 281 | // Delete the unconditianal branch from the predecessor... |
| 282 | BasicBlock::InstListType::iterator DI = Pred->getInstList().end(); |
| 283 | assert(Pred->getTerminator() && |
| 284 | "Degenerate basic block encountered!"); // Empty bb??? |
| 285 | delete Pred->getInstList().remove(--DI); |
| 286 | |
| 287 | // Move all definitions in the succecessor to the predecessor... |
| 288 | while (!BB->getInstList().empty()) { |
| 289 | DI = BB->getInstList().begin(); |
| 290 | Instruction *Def = BB->getInstList().remove(DI); // Remove from front |
| 291 | Pred->getInstList().push_back(Def); // Add to end... |
| 292 | } |
| 293 | |
| 294 | // Remove basic block from the method... and advance iterator to the |
| 295 | // next valid block... |
| 296 | BB = BBs.remove(BBIt); |
| 297 | --BBIt; // remove puts us on the NEXT bb. We want the prev BB |
| 298 | Changed = true; |
| 299 | |
| 300 | // Inherit predecessors name if it exists... |
| 301 | if (BB->hasName() && !Pred->hasName()) Pred->setName(BB->getName()); |
| 302 | |
| 303 | // You ARE the weakest link... goodbye |
| 304 | delete BB; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 305 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | // Remove unused constants |
| 310 | Changed |= DoRemoveUnusedConstants(M); |
| 311 | return Changed; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | // It is possible that we may require multiple passes over the code to fully |
| 316 | // eliminate dead code. Iterate until we are done. |
| 317 | // |
| 318 | bool DoDeadCodeElimination(Method *M) { |
| 319 | bool Changed = false; |
| 320 | while (DoDCEPass(M)) Changed = true; |
| 321 | return Changed; |
| 322 | } |
| 323 | |
| 324 | bool DoDeadCodeElimination(Module *C) { |
| 325 | bool Val = ApplyOptToAllMethods(C, DoDeadCodeElimination); |
| 326 | while (DoRemoveUnusedConstants(C)) Val = true; |
| 327 | return Val; |
| 328 | } |