Index: llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp =================================================================== --- llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -50,6 +50,7 @@ #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/Transforms/Utils/Local.h" #include "llvm/Transforms/Utils/LoopUtils.h" #include "llvm/Transforms/Utils/ValueMapper.h" #include @@ -1139,9 +1140,21 @@ // Replace the cloned branch with an unconditional branch to the cloned // unswitched successor. auto *ClonedSuccBB = cast(VMap.lookup(UnswitchedSuccBB)); - ClonedParentBB->getTerminator()->eraseFromParent(); + Instruction *ClonedTerminator = ClonedParentBB->getTerminator(); + // Trivial Simplification. If Terminator is a conditional branch and + // condition becomes dead - erase it. + Value *ClonedConditionToErase = nullptr; + if (auto *BI = dyn_cast(ClonedTerminator)) + ClonedConditionToErase = BI->getCondition(); + else if (auto *SI = dyn_cast(ClonedTerminator)) + ClonedConditionToErase = SI->getCondition(); + + ClonedTerminator->eraseFromParent(); BranchInst::Create(ClonedSuccBB, ClonedParentBB); + if (ClonedConditionToErase) + RecursivelyDeleteTriviallyDeadInstructions(ClonedConditionToErase); + // If there are duplicate entries in the PHI nodes because of multiple edges // to the unswitched successor, we need to nuke all but one as we replaced it // with a direct branch. Index: llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll =================================================================== --- llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll +++ llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll @@ -2720,8 +2720,6 @@ ; CHECK-NEXT: br label %loop_begin.us ; ; CHECK: loop_begin.us: -; CHECK-NEXT: %[[V1_US:.*]] = load i1, i1* %ptr -; CHECK-NEXT: %[[OR_US:.*]] = or i1 %[[V1_US]], true ; CHECK-NEXT: br label %loop_a.us ; ; CHECK: loop_a.us: @@ -2796,12 +2794,6 @@ ; CHECK-NEXT: br label %loop_begin.us ; ; CHECK: loop_begin.us: -; CHECK-NEXT: %[[V1_US:.*]] = load i1, i1* %ptr1 -; CHECK-NEXT: %[[V2_US:.*]] = load i1, i1* %ptr2 -; CHECK-NEXT: %[[AND1_US:.*]] = and i1 %[[V1_US]], %cond1 -; CHECK-NEXT: %[[OR1_US:.*]] = or i1 %[[V2_US]], %cond2 -; CHECK-NEXT: %[[AND2_US:.*]] = and i1 %[[AND1_US]], %[[OR1_US]] -; CHECK-NEXT: %[[AND3_US:.*]] = and i1 %[[AND2_US]], %cond3 ; CHECK-NEXT: br label %loop_b.us ; ; CHECK: loop_b.us: @@ -2883,12 +2875,6 @@ ; CHECK-NEXT: br label %loop_begin.us ; ; CHECK: loop_begin.us: -; CHECK-NEXT: %[[V1_US:.*]] = load i1, i1* %ptr1 -; CHECK-NEXT: %[[V2_US:.*]] = load i1, i1* %ptr2 -; CHECK-NEXT: %[[OR1_US:.*]] = or i1 %[[V1_US]], %cond1 -; CHECK-NEXT: %[[AND1_US:.*]] = and i1 %[[V2_US]], %cond2 -; CHECK-NEXT: %[[OR2_US:.*]] = or i1 %[[OR1_US]], %[[AND1_US]] -; CHECK-NEXT: %[[OR3_US:.*]] = or i1 %[[OR2_US]], %cond3 ; CHECK-NEXT: br label %loop_b.us ; ; CHECK: loop_b.us: