- for (BBIt = M->begin(), ++BBIt; BBIt != M->end(); ++BBIt) {
- BasicBlock *BB = *BBIt;
- assert(BB->getTerminator() && "Degenerate basic block encountered!");
-
-#if 0 // This is know to basically work?
- // Remove basic blocks that have no predecessors... which are unreachable.
- if (pred_begin(BB) == pred_end(BB) &&
- !BB->hasConstantPoolReferences() && 0) {
- cerr << "Removing BB: \n" << BB;
-
- // Loop through all of our successors and make sure they know that one
- // of their predecessors is going away.
- for_each(succ_begin(BB), succ_end(BB),
- bind_2nd(RemovePredecessorFromBlock, BB));
-
- while (!BB->empty()) {
- Instruction *I = BB->front();
- // If this instruction is used, replace uses with an arbitrary
- // constant value. Because control flow can't get here, we don't care
- // what we replace the value with.
- if (!I->use_empty()) ReplaceUsesWithConstant(I);
-
- // Remove the instruction from the basic block
- delete BB->getInstList().remove(BB->begin());
- }
- delete M->getBasicBlocks().remove(BBIt);
- --BBIt; // remove puts use on the next block, we want the previous one
- Changed = true;
- continue;
- }
-#endif
-
-#if 0 // This has problems
- // Check to see if this block has no instructions and only a single
- // successor. If so, replace block references with successor.
- succ_iterator SI(succ_begin(BB));
- if (SI != succ_end(BB) && ++SI == succ_end(BB)) { // One succ?
- Instruction *I = BB->front();
- if (I->isTerminator()) { // Terminator is the only instruction!
- BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor
- cerr << "Killing Trivial BB: \n" << BB;
-
- if (Succ->front()->isPHINode()) {
- // If our successor has PHI nodes, then we need to update them to
- // include entries for BB's predecessors, not for BB itself.
- //
- PropogatePredecessorsForPHIs(BB, Succ);
- }
-
- BB->replaceAllUsesWith(Succ);
-
- BB = M->getBasicBlocks().remove(BBIt);
- --BBIt; // remove puts use on the next block, we want the previous one
-
- if (BB->hasName() && !Succ->hasName()) // Transfer name if we can
- Succ->setName(BB->getName());
- delete BB; // Delete basic block
-
- cerr << "Method after removal: \n" << M;
- Changed = true;
- continue;
+ bool MadeChange = false;
+ while (!WorkList.empty()) {
+ Instruction *I = WorkList.back();
+ WorkList.pop_back();
+
+ if (isInstructionTriviallyDead(I)) { // If the instruction is dead.
+ // Loop over all of the values that the instruction uses, if there are
+ // instructions being used, add them to the worklist, because they might
+ // go dead after this one is removed.
+ //
+ for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
+ if (Instruction *Used = dyn_cast<Instruction>(*OI))
+ WorkList.push_back(Used);
+
+ // Remove the instruction.
+ I->eraseFromParent();
+
+ // Remove the instruction from the worklist if it still exists in it.
+ for (std::vector<Instruction*>::iterator WI = WorkList.begin();
+ WI != WorkList.end(); ) {
+ if (*WI == I)
+ WI = WorkList.erase(WI);
+ else
+ ++WI;