+
+ // Update dominator info
+ if (DF && DT) {
+
+ SmallVector<BasicBlock *,4> ExitingBlocks;
+ L->getExitingBlocks(ExitingBlocks);
+
+ // Clone dominator info for all cloned basic block.
+ for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
+ BasicBlock *LBB = LoopBlocks[i];
+ BasicBlock *NBB = NewBlocks[i];
+ CloneDomInfo(NBB, LBB, NewPreheader, OrigPreheader,
+ OrigHeader, DT, DF, ValueMap);
+
+ // If LBB's dominance frontier includes DFMember
+ // such that DFMember is also a member of LoopDF then
+ // - Remove DFMember from LBB's dominance frontier
+ // - Copy loop exiting blocks', that are dominated by BB, dominance frontier
+ // member in BB's dominance frontier
+
+ DominanceFrontier::iterator LBBI = DF->find(LBB);
+ DominanceFrontier::iterator NBBI = DF->find(NBB);
+ if (LBBI == DF->end())
+ continue;
+
+ DominanceFrontier::DomSetType &LBSet = LBBI->second;
+ for (DominanceFrontier::DomSetType::iterator LI = LBSet.begin(),
+ LE = LBSet.end(); LI != LE; /* NULL */) {
+ BasicBlock *B = *LI++;
+ if (B == LBB && B == L->getHeader())
+ continue;
+ bool removeB = false;
+ if (!LoopDF.count(B))
+ continue;
+
+ // If LBB dominates loop exits then insert loop exit block's DF
+ // into B's DF.
+ for(SmallVector<BasicBlock *, 4>::iterator LExitI = ExitingBlocks.begin(),
+ LExitE = ExitingBlocks.end(); LExitI != LExitE; ++LExitI) {
+ BasicBlock *E = *LExitI;
+
+ if (!DT->dominates(LBB,E))
+ continue;
+
+ DenseMap<BasicBlock *, BasicBlock *>::iterator DFBI =
+ OrigLoopExitMap.find(E);
+ if (DFBI == OrigLoopExitMap.end())
+ continue;
+
+ BasicBlock *DFB = DFBI->second;
+ DF->addToFrontier(LBBI, DFB);
+ DF->addToFrontier(NBBI, DFB);
+ removeB = true;
+ }
+
+ // If B's replacement is inserted in DF then now is the time to remove B.
+ if (removeB) {
+ DF->removeFromFrontier(LBBI, B);
+ if (L->contains(B))
+ DF->removeFromFrontier(NBBI, cast<BasicBlock>(ValueMap[B]));
+ else
+ DF->removeFromFrontier(NBBI, B);
+ }
+ }
+
+ }
+
+ // MiddleBlocks are dominated by original pre header. SplitEdge updated
+ // MiddleBlocks' dominance frontier appropriately.
+ for (unsigned i = 0, e = MiddleBlocks.size(); i != e; ++i) {
+ BasicBlock *MBB = MiddleBlocks[i];
+ if (!MBB->getSinglePredecessor())
+ DT->changeImmediateDominator(MBB, OrigPreheader);
+ }
+
+ // All Outsiders are now dominated by original pre header.
+ for (SmallPtrSet<BasicBlock *, 8>::iterator OI = OutSiders.begin(),
+ OE = OutSiders.end(); OI != OE; ++OI) {
+ BasicBlock *OB = *OI;
+ DT->changeImmediateDominator(OB, OrigPreheader);
+ }
+
+ // New loop headers are dominated by original preheader
+ DT->changeImmediateDominator(NewBlocks[0], OrigPreheader);
+ DT->changeImmediateDominator(LoopBlocks[0], OrigPreheader);
+ }
+