Skip to content

[JumpThreading] Remove deleted BB from Unreachable #126984

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 3 commits into from
Feb 27, 2025
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
4 changes: 4 additions & 0 deletions llvm/include/llvm/Transforms/Scalar/JumpThreading.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
SmallPtrSet<const BasicBlock *, 16> LoopHeaders;
#endif

// JumpThreading must not processes blocks unreachable from entry. It's a
// waste of compute time and can potentially lead to hangs.
SmallPtrSet<BasicBlock *, 16> Unreachable;

unsigned BBDupThreshold;
unsigned DefaultBBDupThreshold;

Expand Down
10 changes: 7 additions & 3 deletions llvm/lib/Transforms/Scalar/JumpThreading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,11 @@ bool JumpThreadingPass::runImpl(Function &F_, FunctionAnalysisManager *FAM_,
else
BBDupThreshold = DefaultBBDupThreshold;

// JumpThreading must not processes blocks unreachable from entry. It's a
// waste of compute time and can potentially lead to hangs.
SmallPtrSet<BasicBlock *, 16> Unreachable;
assert(DTU && "DTU isn't passed into JumpThreading before using it.");
assert(DTU->hasDomTree() && "JumpThreading relies on DomTree to proceed.");
DominatorTree &DT = DTU->getDomTree();

Unreachable.clear();
for (auto &BB : *F)
if (!DT.isReachableFromEntry(&BB))
Unreachable.insert(&BB);
Expand Down Expand Up @@ -1895,6 +1894,11 @@ bool JumpThreadingPass::maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB) {
SinglePred == BB || hasAddressTakenAndUsed(BB))
return false;

// MergeBasicBlockIntoOnlyPred may delete SinglePred, we need to avoid
// deleting a BB pointer from Unreachable.
if (Unreachable.count(SinglePred))
return false;

// If SinglePred was a loop header, BB becomes one.
if (LoopHeaders.erase(SinglePred))
LoopHeaders.insert(BB);
Expand Down
13 changes: 12 additions & 1 deletion llvm/test/Transforms/JumpThreading/pr62908.ll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please regenerate the test so the whole IR is visible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@

define i32 @test() {
; CHECK-LABEL: define i32 @test() {
; CHECK-NEXT: end:
; CHECK-NEXT: join.thread:
; CHECK-NEXT: br label [[END:%.*]]
; CHECK: unreachable:
; CHECK-NEXT: [[SH_PROM:%.*]] = zext i32 -1 to i64
; CHECK-NEXT: [[SHL:%.*]] = shl nsw i64 -1, [[SH_PROM]]
; CHECK-NEXT: [[CONV:%.*]] = trunc i64 [[SHL]] to i32
; CHECK-NEXT: br label [[JOIN:%.*]]
; CHECK: join:
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[CONV]], [[UNREACHABLE:%.*]] ]
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[PHI]], 0
; CHECK-NEXT: br i1 [[CMP]], label [[END]], label [[END]]
; CHECK: end:
; CHECK-NEXT: ret i32 0
;
entry:
Expand Down
Loading