diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -1057,9 +1057,11 @@ } } - // If the terminator is branching on an undef, we can pick any of the - // successors to branch to. Let GetBestDestForJumpOnUndef decide. - if (isa(Condition)) { + // If the terminator is branching on an undef or freeze undef, we can pick any + // of the successors to branch to. Let GetBestDestForJumpOnUndef decide. + auto *FI = dyn_cast(Condition); + if (isa(Condition) || + (FI && isa(FI->getOperand(0)) && FI->hasOneUse())) { unsigned BestSucc = GetBestDestForJumpOnUndef(BB); std::vector Updates; @@ -1078,6 +1080,8 @@ BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm); BBTerm->eraseFromParent(); DTU->applyUpdatesPermissive(Updates); + if (FI) + FI->eraseFromParent(); return true; } diff --git a/llvm/test/Transforms/JumpThreading/freeze.ll b/llvm/test/Transforms/JumpThreading/freeze.ll --- a/llvm/test/Transforms/JumpThreading/freeze.ll +++ b/llvm/test/Transforms/JumpThreading/freeze.ll @@ -128,19 +128,13 @@ define i32 @test1_undef(i1 %cond) { ; CHECK-LABEL: @test1_undef( -; CHECK-NEXT: br i1 [[COND:%.*]], label [[MERGE_THREAD:%.*]], label [[MERGE:%.*]] -; CHECK: Merge.thread: -; CHECK-NEXT: [[V1:%.*]] = call i32 @f1() -; CHECK-NEXT: br label [[T2:%.*]] -; CHECK: Merge: -; CHECK-NEXT: [[V2:%.*]] = call i32 @f2() -; CHECK-NEXT: [[A_FR:%.*]] = freeze i1 undef -; CHECK-NEXT: br i1 [[A_FR]], label [[T2]], label [[F2:%.*]] +; CHECK-NEXT: br i1 [[COND:%.*]], label [[T2:%.*]], label [[F2:%.*]] ; CHECK: T2: -; CHECK-NEXT: [[B4:%.*]] = phi i32 [ [[V1]], [[MERGE_THREAD]] ], [ [[V2]], [[MERGE]] ] +; CHECK-NEXT: [[V1:%.*]] = call i32 @f1() ; CHECK-NEXT: call void @f3() -; CHECK-NEXT: ret i32 [[B4]] +; CHECK-NEXT: ret i32 [[V1]] ; CHECK: F2: +; CHECK-NEXT: [[V2:%.*]] = call i32 @f2() ; CHECK-NEXT: ret i32 [[V2]] ; br i1 %cond, label %T1, label %F1