Add comments.
[oota-llvm.git] / include / llvm / Analysis / DominatorInternals.h
index 6d95a4b38e92d7e8ece66ca5a0f9a6bd3239bddd..654289e6305c1b44d219cb1ecad7822138d46f3c 100644 (file)
@@ -11,8 +11,8 @@
 #define LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
 
 #include "llvm/Analysis/Dominators.h"
-#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallPtrSet.h"
+
 //===----------------------------------------------------------------------===//
 //
 // DominatorTree construction - This pass constructs immediate dominator
@@ -116,7 +116,7 @@ unsigned DFSPass(DominatorTreeBase<typename GraphT::NodeType>& DT,
 template<class GraphT>
 void Compress(DominatorTreeBase<typename GraphT::NodeType>& DT,
               typename GraphT::NodeType *VIn) {
-  std::vector<typename GraphT::NodeType*> Work;
+  SmallVector<typename GraphT::NodeType*, 32> Work;
   SmallPtrSet<typename GraphT::NodeType*, 32> Visited;
   typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInVAInfo =
                                       DT.Info[DT.Vertex[DT.Info[VIn].Ancestor]];
@@ -152,8 +152,9 @@ void Compress(DominatorTreeBase<typename GraphT::NodeType>& DT,
 }
 
 template<class GraphT>
-typename GraphT::NodeType* Eval(DominatorTreeBase<typename GraphT::NodeType>& DT,
-                                typename GraphT::NodeType *V) {
+typename GraphT::NodeType* 
+Eval(DominatorTreeBase<typename GraphT::NodeType>& DT,
+     typename GraphT::NodeType *V) {
   typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
                                                                      DT.Info[V];
 #if !BALANCE_IDOM_TREE
@@ -233,14 +234,7 @@ void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
   typedef GraphTraits<NodeT> GraphT;
 
   unsigned N = 0;
-
-  // Add a node for the root.  This node might be the actual root, if there is
-  // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
-  // which postdominates all real exits if there are multiple exit blocks.
-  typename GraphT::NodeType* Root = DT.Roots.size() == 1 ? DT.Roots[0]
-                   : 0;
   bool MultipleRoots = (DT.Roots.size() > 1);
-
   if (MultipleRoots) {
     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
         DT.Info[NULL];
@@ -255,39 +249,35 @@ void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
 
   // Step #1: Number blocks in depth-first order and initialize variables used
   // in later stages of the algorithm.
-  for (unsigned i = 0, e = DT.Roots.size(); i != e; ++i)
+  for (unsigned i = 0, e = static_cast<unsigned>(DT.Roots.size());
+       i != e; ++i)
     N = DFSPass<GraphT>(DT, DT.Roots[i], N);
 
+  // it might be that some blocks did not get a DFS number (e.g., blocks of 
+  // infinite loops). In these cases an artificial exit node is required.
+  MultipleRoots |= (DT.isPostDominator() && N != F.size());
+
   for (unsigned i = N; i >= 2; --i) {
     typename GraphT::NodeType* W = DT.Vertex[i];
     typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
                                                                      DT.Info[W];
 
     // Step #2: Calculate the semidominators of all vertices
-    bool HasChildOutsideDFS = false;
 
     // initialize the semi dominator to point to the parent node
     WInfo.Semi = WInfo.Parent;
-    for (typename GraphTraits<Inverse<NodeT> >::ChildIteratorType CI =
-         GraphTraits<Inverse<NodeT> >::child_begin(W),
-         E = GraphTraits<Inverse<NodeT> >::child_end(W); CI != E; ++CI) {
-      if (DT.Info.count(*CI)) {  // Only if this predecessor is reachable!
-        unsigned SemiU = DT.Info[Eval<GraphT>(DT, *CI)].Semi;
+    typedef GraphTraits<Inverse<NodeT> > InvTraits;
+    for (typename InvTraits::ChildIteratorType CI =
+         InvTraits::child_begin(W),
+         E = InvTraits::child_end(W); CI != E; ++CI) {
+      typename InvTraits::NodeType *N = *CI;
+      if (DT.Info.count(N)) {  // Only if this predecessor is reachable!
+        unsigned SemiU = DT.Info[Eval<GraphT>(DT, N)].Semi;
         if (SemiU < WInfo.Semi)
           WInfo.Semi = SemiU;
       }
-      else {
-        // if the child has no DFS number it is not post-dominated by any exit, 
-        // and so is the current block.
-        HasChildOutsideDFS = true;
-      }
     }
 
-    // if some child has no DFS number it is not post-dominated by any exit, 
-    // and so is the current block.
-    if (DT.isPostDominator() && HasChildOutsideDFS)
-      WInfo.Semi = 0;
-
     DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
 
     typename GraphT::NodeType* WParent = DT.Vertex[WInfo.Parent];
@@ -311,15 +301,18 @@ void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
     if (WIDom != DT.Vertex[DT.Info[W].Semi])
       WIDom = DT.IDoms[WIDom];
   }
-  
+
   if (DT.Roots.empty()) return;
-  
+
   // Add a node for the root.  This node might be the actual root, if there is
   // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
-  // which postdominates all real exits if there are multiple exit blocks.
+  // which postdominates all real exits if there are multiple exit blocks, or
+  // an infinite loop.
+  typename GraphT::NodeType* Root = !MultipleRoots ? DT.Roots[0] : 0;
+
   DT.DomTreeNodes[Root] = DT.RootNode =
                         new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
-  
+
   // Loop over all of the reachable blocks in the function...
   for (unsigned i = 2; i <= N; ++i) {
     typename GraphT::NodeType* W = DT.Vertex[i];
@@ -329,20 +322,12 @@ void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
 
     typename GraphT::NodeType* ImmDom = DT.getIDom(W);
 
-    // skip all non root nodes that have no dominator - this occures with 
-    // infinite loops.
-    if (!ImmDom && std::count(DT.Roots.begin(), DT.Roots.end(), W) == 0)
-      continue;
+    assert(ImmDom || DT.DomTreeNodes[NULL]);
 
     // Get or calculate the node for the immediate dominator
     DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
                                                      DT.getNodeForBlock(ImmDom);
 
-    // skip all children that are dominated by a non root node that, by itself,
-    // has no dominator.
-    if (!IDomNode)
-      continue;
-
     // Add a new tree node for this BasicBlock, and link it as a child of
     // IDomNode
     DomTreeNodeBase<typename GraphT::NodeType> *C =
@@ -354,15 +339,8 @@ void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
   DT.IDoms.clear();
   DT.Info.clear();
   std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
-  
-  // FIXME: This does not work on PostDomTrees.  It seems likely that this is
-  // due to an error in the algorithm for post-dominators.  This really should
-  // be investigated and fixed at some point.
-  // DT.updateDFSNumbers();
-
-  // Start out with the DFS numbers being invalid.  Let them be computed if
-  // demanded.
-  DT.DFSInfoValid = false;
+
+  DT.updateDFSNumbers();
 }
 
 }