Skip to content

Commit 0cf06ae

Browse files
committed
[MachineLoopInfo] Fix getLoopID to handle multi latches.
This patch also fixed CodegenPrepare to reserve loop metadata when merging blocks. This fixes issue #102632
1 parent b412ec5 commit 0cf06ae

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
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+
// Reserve 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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ MDNode *MachineLoop::getLoopID() const {
211211
break;
212212
}
213213
}
214-
if (!MD)
215-
return nullptr;
216214
if (!LoopID)
217215
LoopID = MD;
218216
else if (MD != LoopID)

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,56 @@ 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:
101+
; $ clang -O0 -S -emit-llvm loop.c
102+
; $ cat loop.c
103+
; int test3() {
104+
; int i = 0;
105+
; [[clang::code_align(32)]]
106+
; while (i < 10) {
107+
; if (i % 2) {
108+
; continue;
109+
; }
110+
; i++;
111+
; }
112+
; }
113+
; CHECK-LABEL: test3_multilatch:
114+
; ALIGN: .p2align 6, 0x90
115+
; ALIGN-NEXT: .LBB2_1: # %while.cond
116+
define dso_local i32 @test3_multilatch() #0 {
117+
entry:
118+
%retval = alloca i32, align 4
119+
%i = alloca i32, align 4
120+
store i32 0, ptr %retval, align 4
121+
store i32 0, ptr %i, align 4
122+
br label %while.cond
123+
124+
while.cond: ; preds = %if.end, %if.then, %entry
125+
%0 = load i32, ptr %i, align 4
126+
%cmp = icmp slt i32 %0, 10
127+
br i1 %cmp, label %while.body, label %while.end
128+
129+
while.body: ; preds = %while.cond
130+
%1 = load i32, ptr %i, align 4
131+
%rem = srem i32 %1, 2
132+
%tobool = icmp ne i32 %rem, 0
133+
br i1 %tobool, label %if.then, label %if.end
134+
135+
if.then: ; preds = %while.body
136+
br label %while.cond, !llvm.loop !0
137+
138+
if.end: ; preds = %while.body
139+
%2 = load i32, ptr %i, align 4
140+
%inc = add nsw i32 %2, 1
141+
store i32 %inc, ptr %i, align 4
142+
br label %while.cond, !llvm.loop !0
143+
144+
while.end: ; preds = %while.cond
145+
%3 = load i32, ptr %retval, align 4
146+
ret i32 %3
147+
}
148+
99149
declare void @bar()
100150
declare void @var()
101151

0 commit comments

Comments
 (0)