- // Regardless of whether or not we added a preheader to the loop we must
- // guarantee that the preheader dominates all exit nodes. If there are any
- // exit nodes not dominated, split them now.
- DominatorSet &DS = getAnalysis<DominatorSet>();
- BasicBlock *Header = L->getHeader();
- for (unsigned i = 0, e = L->getExitBlocks().size(); i != e; ++i)
- if (!DS.dominates(Header, L->getExitBlocks()[i])) {
- RewriteLoopExitBlock(L, L->getExitBlocks()[i]);
- assert(DS.dominates(Header, L->getExitBlocks()[i]) &&
- "RewriteLoopExitBlock failed?");
- NumInserted++;
- Changed = true;
+ // Next, check to make sure that all exit nodes of the loop only have
+ // predecessors that are inside of the loop. This check guarantees that the
+ // loop preheader/header will dominate the exit blocks. If the exit block has
+ // predecessors from outside of the loop, split the edge now.
+ for (unsigned i = 0, e = L->getExitBlocks().size(); i != e; ++i) {
+ BasicBlock *ExitBlock = L->getExitBlocks()[i];
+ for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
+ PI != PE; ++PI)
+ if (!L->contains(*PI)) {
+ RewriteLoopExitBlock(L, ExitBlock);
+ NumInserted++;
+ Changed = true;
+ break;
+ }