Demoting CHelpers.h to include/llvm/Support.
[oota-llvm.git] / lib / VMCore / Dominators.cpp
index 91b422c7044ec88cffe9da63b4cc86d4b0b5dce6..a1eaf4aa971f4737381235a5eb9996edc0cca6f6 100644 (file)
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/SetOperations.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Instructions.h"
 #include "llvm/Support/Streams.h"
+#include "DominatorCalculation.h"
 #include <algorithm>
 using namespace llvm;
 
@@ -42,20 +44,8 @@ static std::ostream &operator<<(std::ostream &o,
 //  DominatorTree Implementation
 //===----------------------------------------------------------------------===//
 //
-// DominatorTree construction - This pass constructs immediate dominator
-// information for a flow-graph based on the algorithm described in this
-// document:
-//
-//   A Fast Algorithm for Finding Dominators in a Flowgraph
-//   T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
-//
-// This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
-// LINK, but it turns out that the theoretically slower O(n*log(n))
-// implementation is actually faster than the "efficient" algorithm (even for
-// large CFGs) because the constant overheads are substantially smaller.  The
-// lower-complexity version can be enabled with the following #define:
-//
-#define BALANCE_IDOM_TREE 0
+// Provide public access to DominatorTree information.  Implementation details
+// can be found in DominatorCalculation.h.
 //
 //===----------------------------------------------------------------------===//
 
@@ -66,7 +56,6 @@ E("domtree", "Dominator Tree Construction", true);
 // NewBB is split and now it has one successor. Update dominator tree to
 // reflect this change.
 void DominatorTree::splitBlock(BasicBlock *NewBB) {
-
   assert(NewBB->getTerminator()->getNumSuccessors() == 1
          && "NewBB should have a single successor!");
   BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
@@ -92,7 +81,7 @@ void DominatorTree::splitBlock(BasicBlock *NewBB) {
     }
     
     for (; i != e; ++i)
-      if (PredBlocks[i] != OnePred && isReachableFromEntry(OnePred)){
+      if (PredBlocks[i] != OnePred && isReachableFromEntry(OnePred)) {
         NewBBDominatesNewBBSucc = false;
         break;
       }
@@ -119,8 +108,8 @@ void DominatorTree::splitBlock(BasicBlock *NewBB) {
       }
   }
 
-
-  // Find NewBB's immediate dominator and create new dominator tree node for NewBB.
+  // Find NewBB's immediate dominator and create new dominator tree node for
+  // NewBB.
   BasicBlock *NewBBIDom = 0;
   unsigned i = 0;
   for (i = 0; i < PredBlocks.size(); ++i)
@@ -146,261 +135,36 @@ void DominatorTree::splitBlock(BasicBlock *NewBB) {
   }
 }
 
-unsigned DominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
-                                      unsigned N) {
-  // This is more understandable as a recursive algorithm, but we can't use the
-  // recursive algorithm due to stack depth issues.  Keep it here for
-  // documentation purposes.
-#if 0
-  VInfo.Semi = ++N;
-  VInfo.Label = V;
-
-  Vertex.push_back(V);        // Vertex[n] = V;
-  //Info[V].Ancestor = 0;     // Ancestor[n] = 0
-  //Info[V].Child = 0;        // Child[v] = 0
-  VInfo.Size = 1;             // Size[v] = 1
-
-  for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
-    InfoRec &SuccVInfo = Info[*SI];
-    if (SuccVInfo.Semi == 0) {
-      SuccVInfo.Parent = V;
-      N = DFSPass(*SI, SuccVInfo, N);
-    }
-  }
-#else
-  std::vector<std::pair<BasicBlock*, unsigned> > Worklist;
-  Worklist.push_back(std::make_pair(V, 0U));
-  while (!Worklist.empty()) {
-    BasicBlock *BB = Worklist.back().first;
-    unsigned NextSucc = Worklist.back().second;
-    
-    // First time we visited this BB?
-    if (NextSucc == 0) {
-      InfoRec &BBInfo = Info[BB];
-      BBInfo.Semi = ++N;
-      BBInfo.Label = BB;
-      
-      Vertex.push_back(BB);       // Vertex[n] = V;
-      //BBInfo[V].Ancestor = 0;   // Ancestor[n] = 0
-      //BBInfo[V].Child = 0;      // Child[v] = 0
-      BBInfo.Size = 1;            // Size[v] = 1
-    }
-    
-    // If we are done with this block, remove it from the worklist.
-    if (NextSucc == BB->getTerminator()->getNumSuccessors()) {
-      Worklist.pop_back();
-      continue;
-    }
-    
-    // Otherwise, increment the successor number for the next time we get to it.
-    ++Worklist.back().second;
-    
-    // Visit the successor next, if it isn't already visited.
-    BasicBlock *Succ = BB->getTerminator()->getSuccessor(NextSucc);
-    
-    InfoRec &SuccVInfo = Info[Succ];
-    if (SuccVInfo.Semi == 0) {
-      SuccVInfo.Parent = BB;
-      Worklist.push_back(std::make_pair(Succ, 0U));
-    }
-  }
-#endif
-  return N;
-}
-
-void DominatorTree::Compress(BasicBlock *VIn) {
-
-  std::vector<BasicBlock *> Work;
-  std::set<BasicBlock *> Visited;
-  InfoRec &VInInfo = Info[VIn];
-  BasicBlock *VInAncestor = VInInfo.Ancestor;
-  InfoRec &VInVAInfo = Info[VInAncestor];
+void DominatorTreeBase::updateDFSNumbers() {
+  unsigned DFSNum = 0;
 
-  if (VInVAInfo.Ancestor != 0)
-    Work.push_back(VIn);
+  SmallVector<std::pair<DomTreeNode*, DomTreeNode::iterator>, 32> WorkStack;
   
-  while (!Work.empty()) {
-    BasicBlock *V = Work.back();
-    InfoRec &VInfo = Info[V];
-    BasicBlock *VAncestor = VInfo.Ancestor;
-    InfoRec &VAInfo = Info[VAncestor];
-
-    // Process Ancestor first
-    if (Visited.count(VAncestor) == 0 && VAInfo.Ancestor != 0) {
-      Work.push_back(VAncestor);
-      Visited.insert(VAncestor);
-      continue;
-    } 
-    Work.pop_back(); 
-
-    // Update VINfo based on Ancestor info
-    if (VAInfo.Ancestor == 0)
-      continue;
-    BasicBlock *VAncestorLabel = VAInfo.Label;
-    BasicBlock *VLabel = VInfo.Label;
-    if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
-      VInfo.Label = VAncestorLabel;
-    VInfo.Ancestor = VAInfo.Ancestor;
-  }
-}
-
-BasicBlock *DominatorTree::Eval(BasicBlock *V) {
-  InfoRec &VInfo = Info[V];
-#if !BALANCE_IDOM_TREE
-  // Higher-complexity but faster implementation
-  if (VInfo.Ancestor == 0)
-    return V;
-  Compress(V);
-  return VInfo.Label;
-#else
-  // Lower-complexity but slower implementation
-  if (VInfo.Ancestor == 0)
-    return VInfo.Label;
-  Compress(V);
-  BasicBlock *VLabel = VInfo.Label;
-
-  BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
-  if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
-    return VLabel;
-  else
-    return VAncestorLabel;
-#endif
-}
-
-void DominatorTree::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
-#if !BALANCE_IDOM_TREE
-  // Higher-complexity but faster implementation
-  WInfo.Ancestor = V;
-#else
-  // Lower-complexity but slower implementation
-  BasicBlock *WLabel = WInfo.Label;
-  unsigned WLabelSemi = Info[WLabel].Semi;
-  BasicBlock *S = W;
-  InfoRec *SInfo = &Info[S];
-
-  BasicBlock *SChild = SInfo->Child;
-  InfoRec *SChildInfo = &Info[SChild];
-
-  while (WLabelSemi < Info[SChildInfo->Label].Semi) {
-    BasicBlock *SChildChild = SChildInfo->Child;
-    if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
-      SChildInfo->Ancestor = S;
-      SInfo->Child = SChild = SChildChild;
-      SChildInfo = &Info[SChild];
-    } else {
-      SChildInfo->Size = SInfo->Size;
-      S = SInfo->Ancestor = SChild;
-      SInfo = SChildInfo;
-      SChild = SChildChild;
-      SChildInfo = &Info[SChild];
-    }
-  }
-
-  InfoRec &VInfo = Info[V];
-  SInfo->Label = WLabel;
-
-  assert(V != W && "The optimization here will not work in this case!");
-  unsigned WSize = WInfo.Size;
-  unsigned VSize = (VInfo.Size += WSize);
-
-  if (VSize < 2*WSize)
-    std::swap(S, VInfo.Child);
-
-  while (S) {
-    SInfo = &Info[S];
-    SInfo->Ancestor = V;
-    S = SInfo->Child;
-  }
-#endif
-}
-
-void DominatorTree::calculate(Function& F) {
-  BasicBlock* Root = Roots[0];
-
-  // Add a node for the root...
-  DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
-
-  Vertex.push_back(0);
-
-  // Step #1: Number blocks in depth-first order and initialize variables used
-  // in later stages of the algorithm.
-  unsigned N = 0;
-  for (unsigned i = 0, e = Roots.size(); i != e; ++i)
-    N = DFSPass(Roots[i], Info[Roots[i]], 0);
-
-  for (unsigned i = N; i >= 2; --i) {
-    BasicBlock *W = Vertex[i];
-    InfoRec &WInfo = Info[W];
-
-    // Step #2: Calculate the semidominators of all vertices
-    for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
-      if (Info.count(*PI)) {  // Only if this predecessor is reachable!
-        unsigned SemiU = Info[Eval(*PI)].Semi;
-        if (SemiU < WInfo.Semi)
-          WInfo.Semi = SemiU;
-      }
-
-    Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
-
-    BasicBlock *WParent = WInfo.Parent;
-    Link(WParent, W, WInfo);
-
-    // Step #3: Implicitly define the immediate dominator of vertices
-    std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
-    while (!WParentBucket.empty()) {
-      BasicBlock *V = WParentBucket.back();
-      WParentBucket.pop_back();
-      BasicBlock *U = Eval(V);
-      IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
-    }
-  }
-
-  // Step #4: Explicitly define the immediate dominator of each vertex
-  for (unsigned i = 2; i <= N; ++i) {
-    BasicBlock *W = Vertex[i];
-    BasicBlock *&WIDom = IDoms[W];
-    if (WIDom != Vertex[Info[W].Semi])
-      WIDom = IDoms[WIDom];
-  }
-
-  // Loop over all of the reachable blocks in the function...
-  for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
-    if (BasicBlock *ImmDom = getIDom(I)) {  // Reachable block.
-      DomTreeNode *&BBNode = DomTreeNodes[I];
-      if (!BBNode) {  // Haven't calculated this node yet?
-        // Get or calculate the node for the immediate dominator
-        DomTreeNode *IDomNode = getNodeForBlock(ImmDom);
-
-        // Add a new tree node for this BasicBlock, and link it as a child of
-        // IDomNode
-        DomTreeNode *C = new DomTreeNode(I, IDomNode);
-        DomTreeNodes[I] = C;
-        BBNode = IDomNode->addChild(C);
+  for (unsigned i = 0, e = Roots.size(); i != e; ++i) {
+    DomTreeNode *ThisRoot = getNode(Roots[i]);
+    WorkStack.push_back(std::make_pair(ThisRoot, ThisRoot->begin()));
+    ThisRoot->DFSNumIn = DFSNum++;
+    
+    while (!WorkStack.empty()) {
+      DomTreeNode *Node = WorkStack.back().first;
+      DomTreeNode::iterator ChildIt = WorkStack.back().second;
+
+      // If we visited all of the children of this node, "recurse" back up the
+      // stack setting the DFOutNum.
+      if (ChildIt == Node->end()) {
+        Node->DFSNumOut = DFSNum++;
+        WorkStack.pop_back();
+      } else {
+        // Otherwise, recursively visit this child.
+        DomTreeNode *Child = *ChildIt;
+        ++WorkStack.back().second;
+        
+        WorkStack.push_back(std::make_pair(Child, Child->begin()));
+        Child->DFSNumIn = DFSNum++;
       }
     }
-
-  // Free temporary memory used to construct idom's
-  Info.clear();
-  IDoms.clear();
-  std::vector<BasicBlock*>().swap(Vertex);
-
-  updateDFSNumbers();
-}
-
-void DominatorTreeBase::updateDFSNumbers()
-{
-  int dfsnum = 0;
-  // Iterate over all nodes in depth first order.
-  for (unsigned i = 0, e = Roots.size(); i != e; ++i)
-    for (df_iterator<BasicBlock*> I = df_begin(Roots[i]),
-           E = df_end(Roots[i]); I != E; ++I) {
-      BasicBlock *BB = *I;
-      DomTreeNode *BBNode = getNode(BB);
-      if (BBNode) {
-        if (!BBNode->getIDom())
-          BBNode->assignDFSNumber(dfsnum);
-      }
   }
+  
   SlowQueries = 0;
   DFSInfoValid = true;
 }
@@ -450,6 +214,21 @@ void DominatorTreeBase::reset() {
   RootNode = 0;
 }
 
+DomTreeNode *DominatorTreeBase::getNodeForBlock(BasicBlock *BB) {
+  if (DomTreeNode *BBNode = DomTreeNodes[BB])
+    return BBNode;
+
+  // Haven't calculated this node yet?  Get or calculate the node for the
+  // immediate dominator.
+  BasicBlock *IDom = getIDom(BB);
+  DomTreeNode *IDomNode = getNodeForBlock(IDom);
+
+  // Add a new tree node for this BasicBlock, and link it as a child of
+  // IDomNode
+  DomTreeNode *C = new DomTreeNode(BB, IDomNode);
+  return DomTreeNodes[BB] = IDomNode->addChild(C);
+}
+
 /// findNearestCommonDominator - Find nearest common dominator basic block
 /// for basic block A and B. If there is no such block then return NULL.
 BasicBlock *DominatorTreeBase::findNearestCommonDominator(BasicBlock *A, 
@@ -466,11 +245,11 @@ BasicBlock *DominatorTreeBase::findNearestCommonDominator(BasicBlock *A,
     return &Entry;
 
   // If B dominates A then B is nearest common dominator.
-  if (dominates(B,A))
+  if (dominates(B, A))
     return B;
 
   // If A dominates B then A is nearest common dominator.
-  if (dominates(A,B))
+  if (dominates(A, B))
     return A;
 
   DomTreeNode *NodeA = getNode(A);
@@ -480,7 +259,7 @@ BasicBlock *DominatorTreeBase::findNearestCommonDominator(BasicBlock *A,
   SmallPtrSet<DomTreeNode*, 16> NodeADoms;
   NodeADoms.insert(NodeA);
   DomTreeNode *IDomA = NodeA->getIDom();
-  while(IDomA) {
+  while (IDomA) {
     NodeADoms.insert(IDomA);
     IDomA = IDomA->getIDom();
   }
@@ -497,38 +276,6 @@ BasicBlock *DominatorTreeBase::findNearestCommonDominator(BasicBlock *A,
   return NULL;
 }
 
-/// assignDFSNumber - Assign In and Out numbers while walking dominator tree
-/// in dfs order.
-void DomTreeNode::assignDFSNumber(int num) {
-  std::vector<DomTreeNode *>  workStack;
-  std::set<DomTreeNode *> visitedNodes;
-  
-  workStack.push_back(this);
-  visitedNodes.insert(this);
-  this->DFSNumIn = num++;
-  
-  while (!workStack.empty()) {
-    DomTreeNode  *Node = workStack.back();
-    
-    bool visitChild = false;
-    for (std::vector<DomTreeNode*>::iterator DI = Node->begin(),
-           E = Node->end(); DI != E && !visitChild; ++DI) {
-      DomTreeNode *Child = *DI;
-      if (visitedNodes.count(Child) == 0) {
-        visitChild = true;
-        Child->DFSNumIn = num++;
-        workStack.push_back(Child);
-        visitedNodes.insert(Child);
-      }
-    }
-    if (!visitChild) {
-      // If we reach here means all children are visited
-      Node->DFSNumOut = num++;
-      workStack.pop_back();
-    }
-  }
-}
-
 void DomTreeNode::setIDom(DomTreeNode *NewIDom) {
   assert(IDom && "No immediate dominator?");
   if (IDom != NewIDom) {
@@ -545,28 +292,14 @@ void DomTreeNode::setIDom(DomTreeNode *NewIDom) {
   }
 }
 
-DomTreeNode *DominatorTree::getNodeForBlock(BasicBlock *BB) {
-  DomTreeNode *&BBNode = DomTreeNodes[BB];
-  if (BBNode) return BBNode;
-
-  // Haven't calculated this node yet?  Get or calculate the node for the
-  // immediate dominator.
-  BasicBlock *IDom = getIDom(BB);
-  DomTreeNode *IDomNode = getNodeForBlock(IDom);
-
-  // Add a new tree node for this BasicBlock, and link it as a child of
-  // IDomNode
-  DomTreeNode *C = new DomTreeNode(BB, IDomNode);
-  DomTreeNodes[BB] = C;
-  return BBNode = IDomNode->addChild(C);
-}
-
-static std::ostream &operator<<(std::ostream &o,
-                                const DomTreeNode *Node) {
+static std::ostream &operator<<(std::ostream &o, const DomTreeNode *Node) {
   if (Node->getBlock())
     WriteAsOperand(o, Node->getBlock(), false);
   else
     o << " <<exit node>>";
+  
+  o << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}";
+  
   return o << "\n";
 }
 
@@ -578,20 +311,47 @@ static void PrintDomTree(const DomTreeNode *N, std::ostream &o,
     PrintDomTree(*I, o, Lev+1);
 }
 
+/// eraseNode - Removes a node from  the domiantor tree. Block must not
+/// domiante any other blocks. Removes node from its immediate dominator's
+/// children list. Deletes dominator node associated with basic block BB.
+void DominatorTreeBase::eraseNode(BasicBlock *BB) {
+  DomTreeNode *Node = getNode(BB);
+  assert (Node && "Removing node that isn't in dominator tree.");
+  assert (Node->getChildren().empty() && "Node is not a leaf node.");
+
+    // Remove node from immediate dominator's children list.
+  DomTreeNode *IDom = Node->getIDom();
+  if (IDom) {
+    std::vector<DomTreeNode*>::iterator I =
+      std::find(IDom->Children.begin(), IDom->Children.end(), Node);
+    assert(I != IDom->Children.end() &&
+           "Not in immediate dominator children set!");
+    // I am no longer your child...
+    IDom->Children.erase(I);
+  }
+  
+  DomTreeNodes.erase(BB);
+  delete Node;
+}
+
 void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
-  o << "=============================--------------------------------\n"
-    << "Inorder Dominator Tree:\n";
+  o << "=============================--------------------------------\n";
+  o << "Inorder Dominator Tree: ";
+  if (DFSInfoValid)
+    o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
+  o << "\n";
+  
   PrintDomTree(getRootNode(), o, 1);
 }
 
 void DominatorTreeBase::dump() {
-  print (llvm::cerr);
+  print(llvm::cerr);
 }
 
 bool DominatorTree::runOnFunction(Function &F) {
   reset();     // Reset from the last time we were run...
   Roots.push_back(&F.getEntryBlock());
-  calculate(F);
+  DTcalculate(*this, F);
   return false;
 }
 
@@ -606,7 +366,6 @@ G("domfrontier", "Dominance Frontier Construction", true);
 // NewBB is split and now it has one successor. Update dominace frontier to
 // reflect this change.
 void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
-
   assert(NewBB->getTerminator()->getNumSuccessors() == 1
          && "NewBB should have a single successor!");
   BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
@@ -622,12 +381,7 @@ void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
     // other blocks.
     return;
 
-  DominatorTree &DT = getAnalysis<DominatorTree>();
-  bool NewBBDominatesNewBBSucc = true;
-  if (!DT.dominates(NewBB, NewBBSucc))
-    NewBBDominatesNewBBSucc = false;
-
-  // NewBBSucc inherites original NewBB frontier.
+  // NewBBSucc inherits original NewBB frontier.
   DominanceFrontier::iterator NewBBI = find(NewBB);
   if (NewBBI != end()) {
     DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
@@ -639,7 +393,8 @@ void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
   // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
   // DF(PredBlocks[0]) without the stuff that the new block does not dominate
   // a predecessor of.
-  if (NewBBDominatesNewBBSucc) {
+  DominatorTree &DT = getAnalysis<DominatorTree>();
+  if (DT.dominates(NewBB, NewBBSucc)) {
     DominanceFrontier::iterator DFI = find(PredBlocks[0]);
     if (DFI != end()) {
       DominanceFrontier::DomSetType Set = DFI->second;
@@ -683,9 +438,11 @@ void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
     DominanceFrontier::iterator DFI = find(FI);
     if (DFI == end()) continue;  // unreachable block.
     
-    // Only consider dominators of NewBBSucc
+    // Only consider nodes that have NewBBSucc in their dominator frontier.
     if (!DFI->second.count(NewBBSucc)) continue;
 
+    // Verify whether this block dominates a block in predblocks.  If not, do
+    // not update it.
     bool BlockDominatesAny = false;
     for (std::vector<BasicBlock*>::const_iterator BI = PredBlocks.begin(), 
            BE = PredBlocks.end(); BI != BE; ++BI) {
@@ -695,29 +452,27 @@ void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
       }
     }
     
-    if (BlockDominatesAny) {
-      // If NewBBSucc should not stay in our dominator frontier, remove it.
-      // We remove it unless there is a predecessor of NewBBSucc that we
-      // dominate, but we don't strictly dominate NewBBSucc.
-      bool ShouldRemove = true;
-      if ((BasicBlock*)FI == NewBBSucc
-          || !DT.dominates(FI, NewBBSucc)) {
-        // Okay, we know that PredDom does not strictly dominate NewBBSucc.
-        // Check to see if it dominates any predecessors of NewBBSucc.
-        for (pred_iterator PI = pred_begin(NewBBSucc),
-               E = pred_end(NewBBSucc); PI != E; ++PI)
-          if (DT.dominates(FI, *PI)) {
-            ShouldRemove = false;
-            break;
-          }
-        
-        if (ShouldRemove)
-          removeFromFrontier(DFI, NewBBSucc);
-        addToFrontier(DFI, NewBB);
-        
-        break;
-      }
+    if (!BlockDominatesAny)
+      continue;
+    
+    // If NewBBSucc should not stay in our dominator frontier, remove it.
+    // We remove it unless there is a predecessor of NewBBSucc that we
+    // dominate, but we don't strictly dominate NewBBSucc.
+    bool ShouldRemove = true;
+    if ((BasicBlock*)FI == NewBBSucc || !DT.dominates(FI, NewBBSucc)) {
+      // Okay, we know that PredDom does not strictly dominate NewBBSucc.
+      // Check to see if it dominates any predecessors of NewBBSucc.
+      for (pred_iterator PI = pred_begin(NewBBSucc),
+           E = pred_end(NewBBSucc); PI != E; ++PI)
+        if (DT.dominates(FI, *PI)) {
+          ShouldRemove = false;
+          break;
+        }
     }
+    
+    if (ShouldRemove)
+      removeFromFrontier(DFI, NewBBSucc);
+    addToFrontier(DFI, NewBB);
   }
 }