Skip to content

[MachineLoopInfo] Fix getLoopID to handle multi latches. #106195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/lib/CodeGen/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,12 @@ void CodeGenPrepare::eliminateMostlyEmptyBlock(BasicBlock *BB) {
}
}

// Preserve loop Metadata.
if (BI->hasMetadata(LLVMContext::MD_loop)) {
for (auto *Pred : predecessors(BB))
Pred->getTerminator()->copyMetadata(*BI, LLVMContext::MD_loop);
}

// The PHIs are now updated, change everything that refers to BB to use
// DestBB and remove BB.
BB->replaceAllUsesWith(DestBB);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/MachineLoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ MDNode *MachineLoop::getLoopID() const {
}
}
if (!MD)
return nullptr;
continue;
if (!LoopID)
LoopID = MD;
else if (MD != LoopID)
Expand Down
37 changes: 34 additions & 3 deletions llvm/test/CodeGen/X86/code-align-loops.ll
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ for.body5: ; preds = %for.body, %for.body
br i1 %exitcond16.not, label %for.cond.cleanup4, label %for.body5, !llvm.loop !2
}

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

; CHECK-LABEL: test4_multilatch:
; ALIGN: .p2align 6, 0x90
; ALIGN-NEXT: .LBB3_4: # %bb4
define void @test4_multilatch(i32 %a, i32 %b, i32 %c, i32 %d) nounwind {
entry:
br label %bb1

bb1: ; preds = %bb2, %bb4, %entry
call void @bar()
%cmp3 = icmp sgt i32 %c, 10
br i1 %cmp3, label %bb3, label %bb4

bb2: ; preds = %bb3
call void @bar()
%cmp1 = icmp sgt i32 %a, 11
br i1 %cmp1, label %bb1, label %exit, !llvm.loop !0

bb3: ; preds = %bb1
call void @bar()
%cmp2 = icmp sgt i32 %b, 12
br i1 %cmp2, label %bb2, label %exit

bb4: ; preds = %bb1
call void @bar()
%cmp4 = icmp sgt i32 %d, 14
br i1 %cmp4, label %bb1, label %exit

exit: ; preds = %bb2, %bb3, %bb4
ret void
}

declare void @bar()
declare void @var()

Expand Down
Loading