diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -3954,21 +3954,15 @@ // Recursively visit successors. If this is a branch or switch on a // constant, only visit the reachable successor. Instruction *TI = BB->getTerminator(); - if (BranchInst *BI = dyn_cast(TI); BI && BI->isConditional()) { - if (isa(BI->getCondition())) - // Branch on undef is UB. - continue; - if (auto *Cond = dyn_cast(BI->getCondition())) { - bool CondVal = Cond->getZExtValue(); + if (BranchInst *BI = dyn_cast(TI)) { + if (BI->isConditional() && isa(BI->getCondition())) { + bool CondVal = cast(BI->getCondition())->getZExtValue(); BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); Worklist.push_back(ReachableBB); continue; } } else if (SwitchInst *SI = dyn_cast(TI)) { - if (isa(SI->getCondition())) - // Switch on undef is UB. - continue; - if (auto *Cond = dyn_cast(SI->getCondition())) { + if (ConstantInt *Cond = dyn_cast(SI->getCondition())) { Worklist.push_back(SI->findCaseValue(Cond)->getCaseSuccessor()); continue; } diff --git a/llvm/test/Transforms/InstCombine/unreachable-code.ll b/llvm/test/Transforms/InstCombine/unreachable-code.ll --- a/llvm/test/Transforms/InstCombine/unreachable-code.ll +++ b/llvm/test/Transforms/InstCombine/unreachable-code.ll @@ -52,8 +52,10 @@ ; CHECK-SAME: (i1 [[X:%.*]]) { ; CHECK-NEXT: br i1 undef, label [[IF:%.*]], label [[ELSE:%.*]] ; CHECK: if: +; CHECK-NEXT: call void @dummy() ; CHECK-NEXT: ret void ; CHECK: else: +; CHECK-NEXT: call void @dummy() ; CHECK-NEXT: ret void ; %c = xor i1 %x, undef @@ -127,8 +129,10 @@ ; CHECK-NEXT: i32 0, label [[CASE0:%.*]] ; CHECK-NEXT: ] ; CHECK: case0: +; CHECK-NEXT: call void @dummy() ; CHECK-NEXT: ret void ; CHECK: default: +; CHECK-NEXT: call void @dummy() ; CHECK-NEXT: ret void ; %v = xor i32 %x, undef