OSDN Git Service

Not all blocks are reachable from entry. Don't assume they are.
authorNick Lewycky <nicholas@mxc.ca>
Mon, 1 Apr 2019 20:03:16 +0000 (20:03 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Mon, 1 Apr 2019 20:03:16 +0000 (20:03 +0000)
Fixes a bug in isPotentiallyReachable, noticed by inspection.

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

lib/Analysis/CFG.cpp
unittests/Analysis/CFGTest.cpp

index d71dd11..6ef36dc 100644 (file)
@@ -226,10 +226,17 @@ bool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B,
     Worklist.push_back(const_cast<BasicBlock*>(A->getParent()));
   }
 
-  if (A->getParent() == &A->getParent()->getParent()->getEntryBlock())
-    return true;
-  if (B->getParent() == &A->getParent()->getParent()->getEntryBlock())
-    return false;
+  if (DT) {
+    if (DT->isReachableFromEntry(A->getParent()) !=
+        DT->isReachableFromEntry(B->getParent()))
+      return false;
+    if (A->getParent() == &A->getParent()->getParent()->getEntryBlock() &&
+        DT->isReachableFromEntry(B->getParent()))
+      return true;
+    if (B->getParent() == &A->getParent()->getParent()->getEntryBlock() &&
+        DT->isReachableFromEntry(A->getParent()))
+      return false;
+  }
 
   return isPotentiallyReachableFromMany(
       Worklist, const_cast<BasicBlock *>(B->getParent()), DT, LI);
index d2f0d8e..aad3aa6 100644 (file)
@@ -385,3 +385,43 @@ TEST_F(IsPotentiallyReachableTest, ModifyTest) {
   S[0] = OldBB;
   ExpectPath(true);
 }
+
+TEST_F(IsPotentiallyReachableTest, UnreachableFromEntryTest) {
+  ParseAssembly("define void @test() {\n"
+                "entry:\n"
+                "  %A = bitcast i8 undef to i8\n"
+                "  ret void\n"
+                "not.reachable:\n"
+                "  %B = bitcast i8 undef to i8\n"
+                "  ret void\n"
+                "}");
+  ExpectPath(false);
+}
+
+TEST_F(IsPotentiallyReachableTest, UnreachableBlocksTest1) {
+  ParseAssembly("define void @test() {\n"
+                "entry:\n"
+                "  ret void\n"
+                "not.reachable.1:\n"
+                "  %A = bitcast i8 undef to i8\n"
+                "  br label %not.reachable.2\n"
+                "not.reachable.2:\n"
+                "  %B = bitcast i8 undef to i8\n"
+                "  ret void\n"
+                "}");
+  ExpectPath(true);
+}
+
+TEST_F(IsPotentiallyReachableTest, UnreachableBlocksTest2) {
+  ParseAssembly("define void @test() {\n"
+                "entry:\n"
+                "  ret void\n"
+                "not.reachable.1:\n"
+                "  %B = bitcast i8 undef to i8\n"
+                "  br label %not.reachable.2\n"
+                "not.reachable.2:\n"
+                "  %A = bitcast i8 undef to i8\n"
+                "  ret void\n"
+                "}");
+  ExpectPath(false);
+}