Skip to content

Commit 66d7eb9

Browse files
committed
Not all blocks are reachable from entry. Don't assume they are.
Fixes a bug in isPotentiallyReachable, noticed by inspection. llvm-svn: 357425
1 parent c791a20 commit 66d7eb9

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

llvm/lib/Analysis/CFG.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,17 @@ bool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B,
226226
Worklist.push_back(const_cast<BasicBlock*>(A->getParent()));
227227
}
228228

229-
if (A->getParent() == &A->getParent()->getParent()->getEntryBlock())
230-
return true;
231-
if (B->getParent() == &A->getParent()->getParent()->getEntryBlock())
232-
return false;
229+
if (DT) {
230+
if (DT->isReachableFromEntry(A->getParent()) !=
231+
DT->isReachableFromEntry(B->getParent()))
232+
return false;
233+
if (A->getParent() == &A->getParent()->getParent()->getEntryBlock() &&
234+
DT->isReachableFromEntry(B->getParent()))
235+
return true;
236+
if (B->getParent() == &A->getParent()->getParent()->getEntryBlock() &&
237+
DT->isReachableFromEntry(A->getParent()))
238+
return false;
239+
}
233240

234241
return isPotentiallyReachableFromMany(
235242
Worklist, const_cast<BasicBlock *>(B->getParent()), DT, LI);

llvm/unittests/Analysis/CFGTest.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,43 @@ TEST_F(IsPotentiallyReachableTest, ModifyTest) {
385385
S[0] = OldBB;
386386
ExpectPath(true);
387387
}
388+
389+
TEST_F(IsPotentiallyReachableTest, UnreachableFromEntryTest) {
390+
ParseAssembly("define void @test() {\n"
391+
"entry:\n"
392+
" %A = bitcast i8 undef to i8\n"
393+
" ret void\n"
394+
"not.reachable:\n"
395+
" %B = bitcast i8 undef to i8\n"
396+
" ret void\n"
397+
"}");
398+
ExpectPath(false);
399+
}
400+
401+
TEST_F(IsPotentiallyReachableTest, UnreachableBlocksTest1) {
402+
ParseAssembly("define void @test() {\n"
403+
"entry:\n"
404+
" ret void\n"
405+
"not.reachable.1:\n"
406+
" %A = bitcast i8 undef to i8\n"
407+
" br label %not.reachable.2\n"
408+
"not.reachable.2:\n"
409+
" %B = bitcast i8 undef to i8\n"
410+
" ret void\n"
411+
"}");
412+
ExpectPath(true);
413+
}
414+
415+
TEST_F(IsPotentiallyReachableTest, UnreachableBlocksTest2) {
416+
ParseAssembly("define void @test() {\n"
417+
"entry:\n"
418+
" ret void\n"
419+
"not.reachable.1:\n"
420+
" %B = bitcast i8 undef to i8\n"
421+
" br label %not.reachable.2\n"
422+
"not.reachable.2:\n"
423+
" %A = bitcast i8 undef to i8\n"
424+
" ret void\n"
425+
"}");
426+
ExpectPath(false);
427+
}

0 commit comments

Comments
 (0)