[JumpThreading] Fold br(freeze(undef))

This patch makes JumpThreading fold br(freeze(undef)) if the freeze instruction
is only used by the branch.

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D84818
This commit is contained in:
Juneyoung Lee 2020-07-30 09:38:50 +09:00
parent 618a0c0d3b
commit 111a02decd
2 changed files with 11 additions and 13 deletions

View File

@ -1057,9 +1057,11 @@ bool JumpThreadingPass::ProcessBlock(BasicBlock *BB) {
}
}
// If the terminator is branching on an undef, we can pick any of the
// successors to branch to. Let GetBestDestForJumpOnUndef decide.
if (isa<UndefValue>(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<FreezeInst>(Condition);
if (isa<UndefValue>(Condition) ||
(FI && isa<UndefValue>(FI->getOperand(0)) && FI->hasOneUse())) {
unsigned BestSucc = GetBestDestForJumpOnUndef(BB);
std::vector<DominatorTree::UpdateType> Updates;
@ -1078,6 +1080,8 @@ bool JumpThreadingPass::ProcessBlock(BasicBlock *BB) {
BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm);
BBTerm->eraseFromParent();
DTU->applyUpdatesPermissive(Updates);
if (FI)
FI->eraseFromParent();
return true;
}

View File

@ -128,19 +128,13 @@ F2:
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