Skip to content

Ensure collectTransitivePredecessors returns Pred only from the Loop. #113831

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 1 commit into from
Oct 31, 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
7 changes: 6 additions & 1 deletion llvm/lib/Analysis/MustExecute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
/// Collect all blocks from \p CurLoop which lie on all possible paths from
/// the header of \p CurLoop (inclusive) to BB (exclusive) into the set
/// \p Predecessors. If \p BB is the header, \p Predecessors will be empty.
/// Note: It's possible that we encounter Irreducible control flow, due to
/// which, we may find that a few predecessors of \p BB are not a part of the
/// \p CurLoop. We only return Predecessors that are a part of \p CurLoop.
static void collectTransitivePredecessors(
const Loop *CurLoop, const BasicBlock *BB,
SmallPtrSetImpl<const BasicBlock *> &Predecessors) {
Expand All @@ -171,6 +174,8 @@ static void collectTransitivePredecessors(
return;
SmallVector<const BasicBlock *, 4> WorkList;
for (const auto *Pred : predecessors(BB)) {
if (!CurLoop->contains(Pred))
continue;
Predecessors.insert(Pred);
WorkList.push_back(Pred);
}
Expand All @@ -187,7 +192,7 @@ static void collectTransitivePredecessors(
// We can ignore backedge of all loops containing BB to get a sligtly more
// optimistic result.
for (const auto *PredPred : predecessors(Pred))
if (Predecessors.insert(PredPred).second)
if (CurLoop->contains(PredPred) && Predecessors.insert(PredPred).second)
WorkList.push_back(PredPred);
}
}
Expand Down
29 changes: 29 additions & 0 deletions llvm/test/Analysis/MustExecute/irreducible-cfg.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -disable-output -passes=print-mustexecute %s 2>&1 | FileCheck %s

; The loop body has two predecessors, %header and %side-entry. This leads to irreducible-cfg
define i64 @baz() {
; CHECK-LABEL: define i64 @baz() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: br label %[[HEADER:.*]]
; CHECK: [[HEADER]]:
; CHECK-NEXT: br label %[[BODY:.*]] ; (mustexec in: header)
; CHECK: [[SIDE_ENTRY:.*:]]
; CHECK-NEXT: br label %[[BODY]]
; CHECK: [[BODY]]:
; CHECK-NEXT: [[LOAD:%.*]] = load ptr addrspace(1), ptr addrspace(1) null, align 8 ; (mustexec in: header)
; CHECK-NEXT: br label %[[HEADER]] ; (mustexec in: header)
;
entry:
br label %header

header:
br label %body

side-entry:
br label %body

body:
%load = load ptr addrspace(1), ptr addrspace(1) null, align 8
br label %header
}
Loading