-
- // Loop over all of the alive blocks in the function. If any successor
- // blocks are not alive, we adjust the outgoing branches to branch to the
- // first live postdominator of the live block, adjusting any PHI nodes in
- // the block to reflect this.
- //
- for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I)
- if (AliveBlocks.count(I)) {
- BasicBlock *BB = I;
- TerminatorInst *TI = BB->getTerminator();
-
- // Loop over all of the successors, looking for ones that are not alive
- for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
- if (!AliveBlocks.count(TI->getSuccessor(i))) {
- // Scan up the postdominator tree, looking for the first
- // postdominator that is alive, and the last postdominator that is
- // dead...
- //
- DominatorTree::Node *LastNode = DT[TI->getSuccessor(i)];
- DominatorTree::Node *NextNode = LastNode->getIDom();
- while (!AliveBlocks.count(NextNode->getNode())) {
+ LiveSet.insert(NewEntry->getTerminator()); // The branch is live
+ }
+
+ // Loop over all of the alive blocks in the function. If any successor
+ // blocks are not alive, we adjust the outgoing branches to branch to the
+ // first live postdominator of the live block, adjusting any PHI nodes in
+ // the block to reflect this.
+ //
+ for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I)
+ if (AliveBlocks.count(I)) {
+ BasicBlock *BB = I;
+ TerminatorInst *TI = BB->getTerminator();
+
+ // If the terminator instruction is alive, but the block it is contained
+ // in IS alive, this means that this terminator is a conditional branch on
+ // a condition that doesn't matter. Make it an unconditional branch to
+ // ONE of the successors. This has the side effect of dropping a use of
+ // the conditional value, which may also be dead.
+ if (!LiveSet.count(TI))
+ TI = convertToUnconditionalBranch(TI);
+
+ // Loop over all of the successors, looking for ones that are not alive.
+ // We cannot save the number of successors in the terminator instruction
+ // here because we may remove them if we don't have a postdominator.
+ //
+ for (unsigned i = 0; i != TI->getNumSuccessors(); ++i)
+ if (!AliveBlocks.count(TI->getSuccessor(i))) {
+ // Scan up the postdominator tree, looking for the first
+ // postdominator that is alive, and the last postdominator that is
+ // dead...
+ //
+ DomTreeNode *LastNode = DT[TI->getSuccessor(i)];
+ DomTreeNode *NextNode = 0;
+
+ if (LastNode) {
+ NextNode = LastNode->getIDom();
+ while (!AliveBlocks.count(NextNode->getBlock())) {