- // Remove basic blocks that have no predecessors... which are unreachable.
- if (pred_begin(BB) == pred_end(BB) &&
- !BB->hasConstantPoolReferences()) {
- //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),
- std::bind2nd(std::mem_fun(&BasicBlock::removePredecessor), BB));
-
- while (!BB->empty()) {
- Instruction *I = BB->back();
- // 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. Note that since this block is
- // unreachable, and all values contained within it must dominate their
- // uses, that all uses will eventually be removed.
- if (!I->use_empty()) ReplaceUsesWithConstant(I);
-
- // Remove the instruction from the basic block
- delete BB->getInstList().pop_back();
- }
- delete M->getBasicBlocks().remove(BBIt);
- return true;
- }
-
- // 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 != BB) { // Arg, don't hurt infinite loops!
- 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);
-
- 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;
- return true;
- }
- }
- }
-
- // Merge basic blocks into their predecessor if there is only one pred,
- // and if there is only one successor of the predecessor.
- pred_iterator PI(pred_begin(BB));
- if (PI != pred_end(BB) && *PI != BB && // Not empty? Not same BB?
- ++PI == pred_end(BB) && !BB->hasConstantPoolReferences()) {
- BasicBlock *Pred = *pred_begin(BB);
- TerminatorInst *Term = Pred->getTerminator();
- assert(Term != 0 && "malformed basic block without terminator!");
-
- // Does the predecessor block only have a single successor?
- succ_iterator SI(succ_begin(Pred));
- if (++SI == succ_end(Pred)) {
- //cerr << "Merging: " << BB << "into: " << Pred;
-
- // Delete the unconditianal branch from the predecessor...
- BasicBlock::iterator DI = Pred->end();
- assert(Pred->getTerminator() &&
- "Degenerate basic block encountered!"); // Empty bb???
- delete Pred->getInstList().remove(--DI); // Destroy uncond branch
-
- // Move all definitions in the succecessor to the predecessor...
- while (!BB->empty()) {
- DI = BB->begin();
- Instruction *Def = BB->getInstList().remove(DI); // Remove from front
- Pred->getInstList().push_back(Def); // Add to end...
- }
-
- // Remove basic block from the method... and advance iterator to the
- // next valid block...
- BB = M->getBasicBlocks().remove(BBIt);