Skip to content

Commit d714aa0

Browse files
rcorcsfhahn
authored andcommitted
[SimplifyCFG] Handle AssumptionCache being null.
AssumptionCache can be null in SimplifyCFGOptions. However, FoldCondBranchOnPHI() was not properly handling that when passing a null AssumptionCache to simplifyCFG. Patch by Rodrigo Caetano Rocha <[email protected]> Reviewers: fhahn, lebedev.ri, spatel Reviewed By: spatel Differential Revision: https://reviews.llvm.org/D69963
1 parent 071dca2 commit d714aa0

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,9 +2274,8 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL,
22742274
EdgeBB->getInstList().insert(InsertPt, N);
22752275

22762276
// Register the new instruction with the assumption cache if necessary.
2277-
if (auto *II = dyn_cast_or_null<IntrinsicInst>(N))
2278-
if (II->getIntrinsicID() == Intrinsic::assume)
2279-
AC->registerAssumption(II);
2277+
if (AC && match(N, m_Intrinsic<Intrinsic::assume>()))
2278+
AC->registerAssumption(cast<IntrinsicInst>(N));
22802279
}
22812280

22822281
// Loop over all of the edges from PredBB to BB, changing them to branch

llvm/unittests/Transforms/Utils/LocalTest.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/Transforms/Utils/Local.h"
1010
#include "llvm/Analysis/DomTreeUpdater.h"
1111
#include "llvm/Analysis/PostDominators.h"
12+
#include "llvm/Analysis/TargetTransformInfo.h"
1213
#include "llvm/AsmParser/Parser.h"
1314
#include "llvm/IR/BasicBlock.h"
1415
#include "llvm/IR/DIBuilder.h"
@@ -951,3 +952,52 @@ TEST(Local, RemoveUnreachableBlocks) {
951952

952953
runWithDomTree(*M, "f", checkRUBlocksRetVal);
953954
}
955+
956+
TEST(Local, SimplifyCFGWithNullAC) {
957+
LLVMContext Ctx;
958+
959+
std::unique_ptr<Module> M = parseIR(Ctx, R"(
960+
declare void @true_path()
961+
declare void @false_path()
962+
declare void @llvm.assume(i1 %cond);
963+
964+
define i32 @foo(i1, i32) {
965+
entry:
966+
%cmp = icmp sgt i32 %1, 0
967+
br i1 %cmp, label %if.bb1, label %then.bb1
968+
if.bb1:
969+
call void @true_path()
970+
br label %test.bb
971+
then.bb1:
972+
call void @false_path()
973+
br label %test.bb
974+
test.bb:
975+
%phi = phi i1 [1, %if.bb1], [%0, %then.bb1]
976+
call void @llvm.assume(i1 %0)
977+
br i1 %phi, label %if.bb2, label %then.bb2
978+
if.bb2:
979+
ret i32 %1
980+
then.bb2:
981+
ret i32 0
982+
}
983+
)");
984+
985+
Function &F = *cast<Function>(M->getNamedValue("foo"));
986+
TargetTransformInfo TTI(M->getDataLayout());
987+
988+
SimplifyCFGOptions Options{};
989+
Options.setAssumptionCache(nullptr);
990+
991+
// Obtain BasicBlock of interest to this test, %test.bb.
992+
BasicBlock *TestBB = nullptr;
993+
for (BasicBlock &BB : F) {
994+
if (BB.getName().equals("test.bb")) {
995+
TestBB = &BB;
996+
break;
997+
}
998+
}
999+
ASSERT_TRUE(TestBB);
1000+
1001+
// %test.bb is expected to be simplified by FoldCondBranchOnPHI.
1002+
EXPECT_TRUE(simplifyCFG(TestBB, TTI, Options));
1003+
}

0 commit comments

Comments
 (0)