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 | // |
Chris Lattner | 25d17a5 | 2001-06-29 05:24:28 +0000 | [diff] [blame] | 13 | // TODO: This should REALLY be worklist driven instead of iterative. Right now, |
| 14 | // we scan linearly through values, removing unused ones as we go. The problem |
| 15 | // is that this may cause other earlier values to become unused. To make sure |
| 16 | // 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] | 17 | // 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] | 18 | // Piece of cake, and more efficient as well. |
| 19 | // |
| 20 | // Note, this is not trivial, because we have to worry about invalidating |
| 21 | // iterators. :( |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 25 | #include "llvm/Optimizations/DCE.h" |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame^] | 26 | #include "llvm/Support/STLExtras.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
| 28 | #include "llvm/Method.h" |
| 29 | #include "llvm/BasicBlock.h" |
| 30 | #include "llvm/iTerminators.h" |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 31 | #include "llvm/iOther.h" |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 32 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | dac6dda | 2001-06-07 21:18:45 +0000 | [diff] [blame] | 33 | #include "llvm/CFG.h" |
Chris Lattner | 25d17a5 | 2001-06-29 05:24:28 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | |
Chris Lattner | d36c91c | 2001-06-13 19:55:22 +0000 | [diff] [blame] | 36 | using namespace cfg; |
| 37 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | struct ConstPoolDCE { |
| 39 | enum { EndOffs = 0 }; |
Chris Lattner | c262f72 | 2001-07-09 19:38:52 +0000 | [diff] [blame] | 40 | static bool isDCEable(const ConstPoolVal *CPV) { |
| 41 | // TODO: The bytecode writer requires that all used types are in the |
| 42 | // constant pool for the current method. This is messy and is really |
| 43 | // irritating. FIXME |
| 44 | return CPV->getType() != Type::TypeTy; // Don't DCE Type plane constants! |
| 45 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | struct BasicBlockDCE { |
| 49 | enum { EndOffs = 1 }; |
| 50 | static bool isDCEable(const Instruction *I) { |
| 51 | return !I->hasSideEffects(); |
| 52 | } |
| 53 | }; |
| 54 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 20a155f | 2001-07-14 06:11:26 +0000 | [diff] [blame] | 56 | template<class Container, class DCEController> |
| 57 | static bool RemoveUnusedDefs(Container &Vals, DCEController DCEControl) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | bool Changed = false; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 59 | int Offset = DCEController::EndOffs; |
Chris Lattner | 20a155f | 2001-07-14 06:11:26 +0000 | [diff] [blame] | 60 | |
| 61 | for (typename Container::iterator DI = Vals.begin(); |
| 62 | DI != Vals.end()-Offset; ) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 63 | // Look for un"used" definitions... |
| 64 | if ((*DI)->use_empty() && DCEController::isDCEable(*DI)) { |
| 65 | // Bye bye |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 66 | //cerr << "Removing: " << *DI; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 67 | delete Vals.remove(DI); |
| 68 | Changed = true; |
| 69 | } else { |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 70 | ++DI; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | return Changed; |
| 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 | dac6dda | 2001-06-07 21:18:45 +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(); |
| 89 | if (!I->isPHINode()) 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; |
| 92 | //cerr << "Pred #0 = " << *pred_begin(BB); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 93 | |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 94 | //cerr << "Method == " << BB->getParent(); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 95 | |
| 96 | do { |
| 97 | PHINode *PN = (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(); |
| 105 | } while (I->isPHINode()); |
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 | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 110 | bool opt::DoRemoveUnusedConstants(SymTabValue *S) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 111 | bool Changed = false; |
| 112 | ConstantPool &CP = S->getConstantPool(); |
| 113 | for (ConstantPool::plane_iterator PI = CP.begin(); PI != CP.end(); ++PI) |
| 114 | Changed |= RemoveUnusedDefs(**PI, ConstPoolDCE()); |
| 115 | return Changed; |
| 116 | } |
| 117 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 118 | static void ReplaceUsesWithConstant(Instruction *I) { |
| 119 | // Get the method level constant pool |
| 120 | ConstantPool &CP = I->getParent()->getParent()->getConstantPool(); |
| 121 | |
| 122 | ConstPoolVal *CPV = 0; |
| 123 | ConstantPool::PlaneType *P; |
| 124 | if (!CP.getPlane(I->getType(), P)) { // Does plane exist? |
| 125 | // Yes, is it empty? |
| 126 | if (!P->empty()) CPV = P->front(); |
| 127 | } |
| 128 | |
| 129 | if (CPV == 0) { // We don't have an existing constant to reuse. Just add one. |
| 130 | CPV = ConstPoolVal::getNullConstant(I->getType()); // Create a new constant |
| 131 | |
| 132 | // Add the new value to the constant pool... |
| 133 | CP.insert(CPV); |
| 134 | } |
| 135 | |
| 136 | // Make all users of this instruction reference the constant instead |
| 137 | I->replaceAllUsesWith(CPV); |
| 138 | } |
| 139 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 140 | // PropogatePredecessors - This gets "Succ" ready to have the predecessors from |
| 141 | // "BB". This is a little tricky because "Succ" has PHI nodes, which need to |
| 142 | // have extra slots added to them to hold the merge edges from BB's |
| 143 | // predecessors. |
| 144 | // |
| 145 | // Assumption: BB is the single predecessor of Succ. |
| 146 | // |
| 147 | static void PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 148 | assert(Succ->front()->isPHINode() && "Only works on PHId BBs!"); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 149 | |
| 150 | // If there is more than one predecessor, and there are PHI nodes in |
| 151 | // 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] | 152 | // |
| 153 | const vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB)); |
| 154 | |
| 155 | BasicBlock::iterator I = Succ->begin(); |
| 156 | do { // Loop over all of the PHI nodes in the successor BB |
| 157 | PHINode *PN = (PHINode*)*I; |
| 158 | Value *OldVal = PN->removeIncomingValue(BB); |
| 159 | assert(OldVal && "No entry in PHI for Pred BB!"); |
| 160 | |
| 161 | for (vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(), |
| 162 | End = BBPreds.end(); PredI != End; ++PredI) { |
| 163 | // Add an incoming value for each of the new incoming values... |
| 164 | PN->addIncoming(OldVal, *PredI); |
| 165 | } |
| 166 | |
| 167 | ++I; |
| 168 | } while ((*I)->isPHINode()); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 171 | |
| 172 | // SimplifyCFG - This function is used to do simplification of a CFG. For |
| 173 | // example, it adjusts branches to branches to eliminate the extra hop, it |
| 174 | // eliminates unreachable basic blocks, and does other "peephole" optimization |
| 175 | // of the CFG. It returns true if a modification was made, and returns an |
| 176 | // iterator that designates the first element remaining after the block that |
| 177 | // was deleted. |
| 178 | // |
| 179 | // WARNING: The entry node of a method may not be simplified. |
| 180 | // |
| 181 | bool opt::SimplifyCFG(Method::iterator &BBIt) { |
| 182 | assert(*BBIt && (*BBIt)->getParent() && "Block not embedded in method!"); |
| 183 | BasicBlock *BB = *BBIt; |
| 184 | Method *M = BB->getParent(); |
| 185 | assert(BB->getTerminator() && "Degenerate basic block encountered!"); |
| 186 | assert(BB->getParent()->front() != BB && "Can't Simplify entry block!"); |
| 187 | |
| 188 | // Remove basic blocks that have no predecessors... which are unreachable. |
| 189 | if (pred_begin(BB) == pred_end(BB) && |
| 190 | !BB->hasConstantPoolReferences()) { |
| 191 | //cerr << "Removing BB: \n" << BB; |
| 192 | |
| 193 | // Loop through all of our successors and make sure they know that one |
| 194 | // of their predecessors is going away. |
| 195 | for_each(succ_begin(BB), succ_end(BB), |
| 196 | std::bind2nd(std::mem_fun(&BasicBlock::removePredecessor), BB)); |
| 197 | |
| 198 | while (!BB->empty()) { |
| 199 | Instruction *I = BB->back(); |
| 200 | // If this instruction is used, replace uses with an arbitrary |
| 201 | // constant value. Because control flow can't get here, we don't care |
| 202 | // what we replace the value with. Note that since this block is |
| 203 | // unreachable, and all values contained within it must dominate their |
| 204 | // uses, that all uses will eventually be removed. |
| 205 | if (!I->use_empty()) ReplaceUsesWithConstant(I); |
| 206 | |
| 207 | // Remove the instruction from the basic block |
| 208 | delete BB->getInstList().pop_back(); |
| 209 | } |
| 210 | delete M->getBasicBlocks().remove(BBIt); |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | // Check to see if this block has no instructions and only a single |
| 215 | // successor. If so, replace block references with successor. |
| 216 | succ_iterator SI(succ_begin(BB)); |
| 217 | if (SI != succ_end(BB) && ++SI == succ_end(BB)) { // One succ? |
| 218 | Instruction *I = BB->front(); |
| 219 | if (I->isTerminator()) { // Terminator is the only instruction! |
| 220 | BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor |
| 221 | //cerr << "Killing Trivial BB: \n" << BB; |
| 222 | |
| 223 | if (Succ != BB) { // Arg, don't hurt infinite loops! |
| 224 | if (Succ->front()->isPHINode()) { |
| 225 | // If our successor has PHI nodes, then we need to update them to |
| 226 | // include entries for BB's predecessors, not for BB itself. |
| 227 | // |
| 228 | PropogatePredecessorsForPHIs(BB, Succ); |
| 229 | } |
| 230 | |
| 231 | BB->replaceAllUsesWith(Succ); |
| 232 | BB = M->getBasicBlocks().remove(BBIt); |
| 233 | |
| 234 | if (BB->hasName() && !Succ->hasName()) // Transfer name if we can |
| 235 | Succ->setName(BB->getName()); |
| 236 | delete BB; // Delete basic block |
| 237 | |
| 238 | //cerr << "Method after removal: \n" << M; |
| 239 | return true; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // Merge basic blocks into their predecessor if there is only one pred, |
| 245 | // and if there is only one successor of the predecessor. |
| 246 | pred_iterator PI(pred_begin(BB)); |
| 247 | if (PI != pred_end(BB) && *PI != BB && // Not empty? Not same BB? |
| 248 | ++PI == pred_end(BB) && !BB->hasConstantPoolReferences()) { |
| 249 | BasicBlock *Pred = *pred_begin(BB); |
| 250 | TerminatorInst *Term = Pred->getTerminator(); |
| 251 | assert(Term != 0 && "malformed basic block without terminator!"); |
| 252 | |
| 253 | // Does the predecessor block only have a single successor? |
| 254 | succ_iterator SI(succ_begin(Pred)); |
| 255 | if (++SI == succ_end(Pred)) { |
| 256 | //cerr << "Merging: " << BB << "into: " << Pred; |
| 257 | |
| 258 | // Delete the unconditianal branch from the predecessor... |
| 259 | BasicBlock::iterator DI = Pred->end(); |
| 260 | assert(Pred->getTerminator() && |
| 261 | "Degenerate basic block encountered!"); // Empty bb??? |
| 262 | delete Pred->getInstList().remove(--DI); // Destroy uncond branch |
| 263 | |
| 264 | // Move all definitions in the succecessor to the predecessor... |
| 265 | while (!BB->empty()) { |
| 266 | DI = BB->begin(); |
| 267 | Instruction *Def = BB->getInstList().remove(DI); // Remove from front |
| 268 | Pred->getInstList().push_back(Def); // Add to end... |
| 269 | } |
| 270 | |
| 271 | // Remove basic block from the method... and advance iterator to the |
| 272 | // next valid block... |
| 273 | BB = M->getBasicBlocks().remove(BBIt); |
| 274 | |
| 275 | // Make all PHI nodes that refered to BB now refer to Pred as their |
| 276 | // source... |
| 277 | BB->replaceAllUsesWith(Pred); |
| 278 | |
| 279 | // Inherit predecessors name if it exists... |
| 280 | if (BB->hasName() && !Pred->hasName()) Pred->setName(BB->getName()); |
| 281 | |
| 282 | delete BB; // You ARE the weakest link... goodbye |
| 283 | return true; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return false; |
| 288 | } |
| 289 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 290 | static bool DoDCEPass(Method *M) { |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 291 | Method::iterator BBIt, BBEnd = M->end(); |
| 292 | if (M->begin() == BBEnd) return false; // Nothing to do |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 293 | bool Changed = false; |
| 294 | |
| 295 | // Loop through now and remove instructions that have no uses... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 296 | for (BBIt = M->begin(); BBIt != BBEnd; ++BBIt) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 297 | Changed |= RemoveUnusedDefs((*BBIt)->getInstList(), BasicBlockDCE()); |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 298 | Changed |= RemoveSingularPHIs(*BBIt); |
| 299 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 300 | |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 301 | // Loop over all of the basic blocks (except the first one) and remove them |
| 302 | // if they are unneeded... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 303 | // |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 304 | for (BBIt = M->begin(), ++BBIt; BBIt != M->end(); ) { |
| 305 | if (opt::SimplifyCFG(BBIt)) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 306 | Changed = true; |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 307 | } else { |
| 308 | ++BBIt; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | // Remove unused constants |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 313 | return Changed | opt::DoRemoveUnusedConstants(M); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
| 317 | // It is possible that we may require multiple passes over the code to fully |
| 318 | // eliminate dead code. Iterate until we are done. |
| 319 | // |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 320 | bool opt::DoDeadCodeElimination(Method *M) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 321 | bool Changed = false; |
| 322 | while (DoDCEPass(M)) Changed = true; |
| 323 | return Changed; |
| 324 | } |
| 325 | |
Chris Lattner | 7e02b7e | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 326 | bool opt::DoDeadCodeElimination(Module *C) { |
| 327 | bool Val = C->reduceApply(DoDeadCodeElimination); |
| 328 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 329 | while (DoRemoveUnusedConstants(C)) Val = true; |
| 330 | return Val; |
| 331 | } |