diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -6598,9 +6598,17 @@ // destination from conditional branches. if (BI->isUnconditional()) Builder.CreateUnreachable(); - else + else { + // Preserve guarding condition in assume, because it might not be + // inferrable from any dominating condition. + Value *Cond = BI->getCondition(); + if (BI->getSuccessor(0) == BB) + Builder.CreateAssumption(Builder.CreateNot(Cond)); + else + Builder.CreateAssumption(Cond); Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1) : BI->getSuccessor(0)); + } BI->eraseFromParent(); if (DTU) DTU->applyUpdates({{DominatorTree::Delete, Predecessor, BB}}); diff --git a/llvm/test/Transforms/SimplifyCFG/tautological-conditional-branch.ll b/llvm/test/Transforms/SimplifyCFG/tautological-conditional-branch.ll --- a/llvm/test/Transforms/SimplifyCFG/tautological-conditional-branch.ll +++ b/llvm/test/Transforms/SimplifyCFG/tautological-conditional-branch.ll @@ -14,8 +14,12 @@ ; CHECK-NEXT: unreachable ; CHECK: bb21: ; CHECK-NEXT: [[I:%.*]] = icmp eq %0* [[ARG17:%.*]], null +; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[I]], true +; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]]) ; CHECK-NEXT: call void @hoge() -; CHECK-NEXT: [[I27:%.*]] = getelementptr inbounds [[TMP0:%.*]], %0* [[ARG19:%.*]], i64 0, i32 0 +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[ARG16:%.*]], true +; CHECK-NEXT: call void @llvm.assume(i1 [[TMP1]]) +; CHECK-NEXT: [[I27:%.*]] = getelementptr inbounds [[TMP0]], %0* [[ARG19:%.*]], i64 0, i32 0 ; CHECK-NEXT: [[I28:%.*]] = load %1*, %1** [[I27]], align 8 ; CHECK-NEXT: call void @pluto.1(%1* [[I28]]) ; CHECK-NEXT: call void @pluto()