- for (BasicBlock::iterator II = NextAlive->begin();
- PHINode *PN = dyn_cast<PHINode>(*II); ++II) {
- // Get the incoming value for LastDead...
- int OldIdx = PN->getBasicBlockIndex(LastDead);
- assert(OldIdx != -1 && "LastDead is not a pred of NextAlive!");
- Value *InVal = PN->getIncomingValue(OldIdx);
-
- // Add an incoming value for BB now...
- PN->addIncoming(InVal, BB);
+ if (LastNode == 0) { // No postdominator!
+ // Call RemoveSuccessor to transmogrify the terminator instruction
+ // to not contain the outgoing branch, or to create a new
+ // terminator if the form fundementally changes (ie unconditional
+ // branch to return). Note that this will change a branch into an
+ // infinite loop into a return instruction!
+ //
+ RemoveSuccessor(TI, i);
+
+ // RemoveSuccessor may replace TI... make sure we have a fresh
+ // pointer... and e variable.
+ //
+ TI = BB->getTerminator();
+
+ // Rescan this successor...
+ --i;
+ } else {
+ PostDominatorTree::Node *NextNode = LastNode->getIDom();
+
+ while (!AliveBlocks.count(NextNode->getNode())) {
+ LastNode = NextNode;
+ NextNode = NextNode->getIDom();
+ }
+
+ // Get the basic blocks that we need...
+ BasicBlock *LastDead = LastNode->getNode();
+ BasicBlock *NextAlive = NextNode->getNode();
+
+ // Make the conditional branch now go to the next alive block...
+ TI->getSuccessor(i)->removePredecessor(BB);
+ TI->setSuccessor(i, NextAlive);
+
+ // If there are PHI nodes in NextAlive, we need to add entries to
+ // the PHI nodes for the new incoming edge. The incoming values
+ // should be identical to the incoming values for LastDead.
+ //
+ for (BasicBlock::iterator II = NextAlive->begin();
+ PHINode *PN = dyn_cast<PHINode>(&*II); ++II) {
+ // Get the incoming value for LastDead...
+ int OldIdx = PN->getBasicBlockIndex(LastDead);
+ assert(OldIdx != -1 && "LastDead is not a pred of NextAlive!");
+ Value *InVal = PN->getIncomingValue(OldIdx);
+
+ // Add an incoming value for BB now...
+ PN->addIncoming(InVal, BB);
+ }