Fix PR1098 by correcting the postdominators analysis.
authorOwen Anderson <resistor@mac.com>
Sun, 4 May 2008 21:07:35 +0000 (21:07 +0000)
committerOwen Anderson <resistor@mac.com>
Sun, 4 May 2008 21:07:35 +0000 (21:07 +0000)
Patch by Florian Brandner.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50628 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/DominatorInternals.h
include/llvm/Analysis/Dominators.h
test/Analysis/PostDominators/dg.exp [new file with mode: 0644]
test/Analysis/PostDominators/pr1098.ll [new file with mode: 0644]

index 6d95a4b38e92d7e8ece66ca5a0f9a6bd3239bddd..063602e92f297953a9a9b7fc35f65450c24feef7 100644 (file)
@@ -233,14 +233,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];
@@ -258,6 +251,10 @@ void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
   for (unsigned i = 0, e = 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 =
@@ -311,15 +308,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 +329,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 =
index 11c0bc234ca988d89cb282c8da77a6233953168f..ad1766c85ab1c204f3ae1f55871844655a55fd5e 100644 (file)
@@ -605,17 +605,9 @@ protected:
     // immediate dominator.
     NodeT *IDom = getIDom(BB);
 
-    // skip all non root nodes that have no dominator
-    if (!IDom && std::count(this->Roots.begin(), this->Roots.end(), BB) == 0)
-      return NULL;
-
+    assert(IDom || this->DomTreeNodes[NULL]);
     DomTreeNodeBase<NodeT> *IDomNode = getNodeForBlock(IDom);
 
-    // skip all nodes that are dominated by a non root node that, by itself,
-    // has no dominator.
-    if (!IDomNode)
-      return NULL;
-
     // Add a new tree node for this BasicBlock, and link it as a child of
     // IDomNode
     DomTreeNodeBase<NodeT> *C = new DomTreeNodeBase<NodeT>(BB, IDomNode);
diff --git a/test/Analysis/PostDominators/dg.exp b/test/Analysis/PostDominators/dg.exp
new file mode 100644 (file)
index 0000000..879685c
--- /dev/null
@@ -0,0 +1,3 @@
+load_lib llvm.exp
+
+RunLLVMTests [lsort [glob -nocomplain $srcdir/$subdir/*.{ll,llx,c,cpp,tr}]]
diff --git a/test/Analysis/PostDominators/pr1098.ll b/test/Analysis/PostDominators/pr1098.ll
new file mode 100644 (file)
index 0000000..b54a9fe
--- /dev/null
@@ -0,0 +1,14 @@
+; RUN: llvm-as < %s | opt -postdomtree -analyze | grep entry
+; PR932
+
+define void @foo(i1 %x) {
+entry:
+        br i1 %x, label %bb1, label %bb0
+bb0:            ; preds = %entry, bb0
+        br label %bb0
+bb1:            ; preds = %entry
+        br label %bb2
+bb2:            ; preds = %bb1
+        ret void
+}
+