Skip to content

Commit 3a5c578

Browse files
authored
[MachineLoopInfo] Fix getLoopID to handle multi latches. (#106195)
This patch also fixed `CodegenPrepare` to preserve loop metadata when merging blocks. This fixes issue #102632
1 parent b75fe11 commit 3a5c578

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,12 @@ void CodeGenPrepare::eliminateMostlyEmptyBlock(BasicBlock *BB) {
11831183
}
11841184
}
11851185

1186+
// Preserve loop Metadata.
1187+
if (BI->hasMetadata(LLVMContext::MD_loop)) {
1188+
for (auto *Pred : predecessors(BB))
1189+
Pred->getTerminator()->copyMetadata(*BI, LLVMContext::MD_loop);
1190+
}
1191+
11861192
// The PHIs are now updated, change everything that refers to BB to use
11871193
// DestBB and remove BB.
11881194
BB->replaceAllUsesWith(DestBB);

llvm/lib/CodeGen/MachineLoopInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ MDNode *MachineLoop::getLoopID() const {
212212
}
213213
}
214214
if (!MD)
215-
return nullptr;
215+
continue;
216216
if (!LoopID)
217217
LoopID = MD;
218218
else if (MD != LoopID)

llvm/test/CodeGen/X86/code-align-loops.ll

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ for.body5: ; preds = %for.body, %for.body
9696
br i1 %exitcond16.not, label %for.cond.cleanup4, label %for.body5, !llvm.loop !2
9797
}
9898

99-
; test3 is to check if .p2align can be correctly set on loops with multi latches.
100-
; The test IR is generated from below simple C file:
99+
; test3 and test4 is to check if .p2align can be correctly set on loops with
100+
; multi latches. The IR is generated from below simple C file:
101101
; $ clang -O0 -S -emit-llvm loop.c
102102
; $ cat loop.c
103103
; int test3() {
@@ -111,7 +111,7 @@ for.body5: ; preds = %for.body, %for.body
111111
; }
112112
; }
113113
; CHECK-LABEL: test3_multilatch:
114-
; ALIGN: .p2align 4, 0x90
114+
; ALIGN: .p2align 6, 0x90
115115
; ALIGN-NEXT: .LBB2_1: # %while.cond
116116
define dso_local i32 @test3_multilatch() #0 {
117117
entry:
@@ -146,6 +146,37 @@ while.end: ; preds = %while.cond
146146
ret i32 %3
147147
}
148148

149+
; CHECK-LABEL: test4_multilatch:
150+
; ALIGN: .p2align 6, 0x90
151+
; ALIGN-NEXT: .LBB3_4: # %bb4
152+
define void @test4_multilatch(i32 %a, i32 %b, i32 %c, i32 %d) nounwind {
153+
entry:
154+
br label %bb1
155+
156+
bb1: ; preds = %bb2, %bb4, %entry
157+
call void @bar()
158+
%cmp3 = icmp sgt i32 %c, 10
159+
br i1 %cmp3, label %bb3, label %bb4
160+
161+
bb2: ; preds = %bb3
162+
call void @bar()
163+
%cmp1 = icmp sgt i32 %a, 11
164+
br i1 %cmp1, label %bb1, label %exit, !llvm.loop !0
165+
166+
bb3: ; preds = %bb1
167+
call void @bar()
168+
%cmp2 = icmp sgt i32 %b, 12
169+
br i1 %cmp2, label %bb2, label %exit
170+
171+
bb4: ; preds = %bb1
172+
call void @bar()
173+
%cmp4 = icmp sgt i32 %d, 14
174+
br i1 %cmp4, label %bb1, label %exit
175+
176+
exit: ; preds = %bb2, %bb3, %bb4
177+
ret void
178+
}
179+
149180
declare void @bar()
150181
declare void @var()
151182

0 commit comments

Comments
 (0)