Index: llvm/include/llvm/Analysis/LoopInfoImpl.h =================================================================== --- llvm/include/llvm/Analysis/LoopInfoImpl.h +++ llvm/include/llvm/Analysis/LoopInfoImpl.h @@ -211,7 +211,7 @@ BlockT *Header = getHeader(); BlockT *Latch = nullptr; for (const auto Pred : children>(Header)) { - if (contains(Pred)) { + if (Latch != Pred && contains(Pred)) { if (Latch) return nullptr; Latch = Pred; Index: llvm/test/Transforms/LoopSimplify/basictest.ll =================================================================== --- llvm/test/Transforms/LoopSimplify/basictest.ll +++ llvm/test/Transforms/LoopSimplify/basictest.ll @@ -44,17 +44,15 @@ ; CHECK-NEXT: br label [[LOOP:%.*]] ; CHECK: loop: ; CHECK-NEXT: [[B:%.*]] = load volatile i8, i8* [[B_PTR:%.*]] -; CHECK-NEXT: switch i8 [[B]], label [[LOOP_BACKEDGE:%.*]] [ +; CHECK-NEXT: switch i8 [[B]], label [[LOOP]] [ ; CHECK-NEXT: i8 0, label [[EXIT_A_LOOPEXIT:%.*]] ; CHECK-NEXT: i8 1, label [[EXIT_B_LOOPEXIT:%.*]] -; CHECK-NEXT: i8 2, label [[LOOP_BACKEDGE]] +; CHECK-NEXT: i8 2, label [[LOOP]] ; CHECK-NEXT: i8 3, label [[EXIT_A_LOOPEXIT]] -; CHECK-NEXT: i8 4, label [[LOOP_BACKEDGE]] +; CHECK-NEXT: i8 4, label [[LOOP]] ; CHECK-NEXT: i8 5, label [[EXIT_A_LOOPEXIT]] -; CHECK-NEXT: i8 6, label [[LOOP_BACKEDGE]] +; CHECK-NEXT: i8 6, label [[LOOP]] ; CHECK-NEXT: ] -; CHECK: loop.backedge: -; CHECK-NEXT: br label [[LOOP]] ; CHECK: exit.a.loopexit: ; CHECK-NEXT: br label [[EXIT_A]] ; CHECK: exit.a: