diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp --- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -1157,7 +1157,7 @@ UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit); } - if (OldLatch) { + if (L == LI->getLoopFor(OldLatch) && OldLatch) { BasicBlock *NewLatch = L->getLoopLatch(); if (NewLatch != OldLatch) { MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop"); diff --git a/llvm/test/Transforms/LoopSimplify/metadata.ll b/llvm/test/Transforms/LoopSimplify/metadata.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/LoopSimplify/metadata.ll @@ -0,0 +1,34 @@ +; Tests loop-simplify does not move the loop metadata, because +; the loopexit block is not the latch of the loop _bb6. + +; RUN: opt < %s -loop-simplify -S | FileCheck %s +; CHECK-LABEL: _bb7.loopexit: +; CHECK-NOT: br label %_bb7, !llvm.loop !0 +; CHECK-LABEL: _bb6: +; CHECK: br i1 %p, label %_bb6, label %_bb7.loopexit, !llvm.loop !0 + +define void @func(i1 %p) { +_bb1: + br label %_bb7 + +_bb7: + br i1 %p, label %_bb4, label %_bb2 + +_bb4: + br i1 %p, label %_bb5, label %_bb3 + +_bb5: + br label %_bb3 + +_bb3: + br label %_bb6 + +_bb6: + br i1 %p, label %_bb6, label %_bb7, !llvm.loop !0 + +_bb2: + ret void +} + +!0 = distinct !{!0, !1} +!1 = !{!"llvm.loop.mustprogress"}