Skip to content

Commit 4f93d5c

Browse files
author
Hendrik Greving
committed
[BasicBlockUtils] Do not move loop metadata if outer loop header.
Fixes a bug preventing moving the loop's metadata to an outer loop's header, which happens if the loop's exit is also the header of an outer loop. Adjusts test for above. Fixes #55416. Differential Revision: https://reviews.llvm.org/D125574
1 parent e5d8fb6 commit 4f93d5c

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,11 @@ SplitBlockPredecessorsImpl(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
11621162
if (NewLatch != OldLatch) {
11631163
MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop");
11641164
NewLatch->getTerminator()->setMetadata("llvm.loop", MD);
1165-
OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr);
1165+
// It's still possible that OldLatch is the latch of another inner loop,
1166+
// in which case we do not remove the metadata.
1167+
Loop *IL = LI->getLoopFor(OldLatch);
1168+
if (IL && IL->getLoopLatch() != OldLatch)
1169+
OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr);
11661170
}
11671171
}
11681172

llvm/test/Transforms/LoopSimplify/update_latch_md2.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
; Tests loop-simplify does not move the loop metadata, because
22
; the loopexit block is not the latch of the loop _bb6.
33

4-
; FIXME(#55416): The metadata should not move.
5-
64
; RUN: opt < %s -passes=loop-simplify -S | FileCheck %s
75
; CHECK-LABEL: loop.header.loopexit:
86
; CHECK: br label %loop.header, !llvm.loop !0
97
; CHECK-LABEL: loop.latch:
10-
; CHECK-NOT: br i1 %p, label %loop.latch, label %loop.header.loopexit, !llvm.loop !0
8+
; CHECK: br i1 %p, label %loop.latch, label %loop.header.loopexit, !llvm.loop !0
119

1210
define void @func(i1 %p) {
1311
entry:

0 commit comments

Comments
 (0)