[PATCH] Fix dominator descendants for unreachable blocks.

Diego Novillo dnovillo at google.com
Thu Nov 28 15:57:52 PST 2013


Hi chandlerc,


When a block is unreachable, asking its dom/pdom tree descendants should
return the empty set. However, the computation of the descendants
was causing a segmentation fault because the dom tree node we get
from the basic block is initially NULL.

Fixed by adding a test for a valid dom tree node before we iterate.

The patch also adds some unit tests to the existing dom tree tests.

OK to commit?

http://llvm-reviews.chandlerc.com/D2288

Files:
  include/llvm/Analysis/Dominators.h
  unittests/IR/DominatorTreeTest.cpp

Index: include/llvm/Analysis/Dominators.h
===================================================================
--- include/llvm/Analysis/Dominators.h
+++ include/llvm/Analysis/Dominators.h
@@ -353,7 +353,7 @@
     WL.push_back(RN);
     Result.clear();
 
-    while (!WL.empty()) {
+    while (!WL.empty() && RN) {
       const DomTreeNodeBase<NodeT> *N = WL.pop_back_val();
       Result.push_back(N->getBlock());
       WL.append(N->begin(), N->end());
Index: unittests/IR/DominatorTreeTest.cpp
===================================================================
--- unittests/IR/DominatorTreeTest.cpp
+++ unittests/IR/DominatorTreeTest.cpp
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/Dominators.h"
+#include "llvm/Analysis/PostDominators.h"
 #include "llvm/Assembly/Parser.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/LLVMContext.h"
@@ -26,6 +27,7 @@
       static char ID;
       virtual bool runOnFunction(Function &F) {
         DominatorTree *DT = &getAnalysis<DominatorTree>();
+        PostDominatorTree *PDT = &getAnalysis<PostDominatorTree>();
         Function::iterator FI = F.begin();
 
         BasicBlock *BB0 = FI++;
@@ -148,10 +150,33 @@
 
         EXPECT_TRUE(DT->dominates(Y6, BB3));
 
+        // Post dominance.
+        EXPECT_TRUE(PDT->dominates(BB0, BB0));
+        EXPECT_FALSE(PDT->dominates(BB1, BB0));
+        EXPECT_FALSE(PDT->dominates(BB2, BB0));
+        EXPECT_FALSE(PDT->dominates(BB3, BB0));
+        EXPECT_TRUE(PDT->dominates(BB4, BB1));
+
+        // Dominance descendants.
+        SmallVector<BasicBlock *, 8> DominatedBBs, PostDominatedBBs;
+
+        DT->getDescendants(BB0, DominatedBBs);
+        PDT->getDescendants(BB0, PostDominatedBBs);
+        EXPECT_EQ(DominatedBBs.size(), 4UL);
+        EXPECT_EQ(PostDominatedBBs.size(), 1UL);
+
+        DominatedBBs.clear();
+        PostDominatedBBs.clear();
+        DT->getDescendants(BB3, DominatedBBs);
+        DT->getDescendants(BB3, PostDominatedBBs);
+        EXPECT_EQ(DominatedBBs.size(), 0UL);
+        EXPECT_EQ(PostDominatedBBs.size(), 0UL);
+
         return false;
       }
       virtual void getAnalysisUsage(AnalysisUsage &AU) const {
         AU.addRequired<DominatorTree>();
+        AU.addRequired<PostDominatorTree>();
       }
       DPass() : FunctionPass(ID) {
         initializeDPassPass(*PassRegistry::getPassRegistry());
@@ -201,4 +226,5 @@
 
 INITIALIZE_PASS_BEGIN(DPass, "dpass", "dpass", false, false)
 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
+INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
 INITIALIZE_PASS_END(DPass, "dpass", "dpass", false, false)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2288.1.patch
Type: text/x-patch
Size: 2668 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131128/8c2079af/attachment.bin>


More information about the llvm-commits mailing list