Skip to content

Commit 11e65b9

Browse files
authored
[JumpThreading] Remove deleted BB from Unreachable (#126984)
Although an unreachable BB is skipped by processBlock, its successor can still be handled by processBlock, and maybeMergeBasicBlockIntoOnlyPred may merge the two BBs and delete the unreachable BB. Then the garbage pointer is left in Unreachable set. This patch avoids merging a BB into unreachable predecessor.
1 parent 63ecb01 commit 11e65b9

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

llvm/include/llvm/Transforms/Scalar/JumpThreading.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
9494
SmallPtrSet<const BasicBlock *, 16> LoopHeaders;
9595
#endif
9696

97+
// JumpThreading must not processes blocks unreachable from entry. It's a
98+
// waste of compute time and can potentially lead to hangs.
99+
SmallPtrSet<BasicBlock *, 16> Unreachable;
100+
97101
unsigned BBDupThreshold;
98102
unsigned DefaultBBDupThreshold;
99103

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,11 @@ bool JumpThreadingPass::runImpl(Function &F_, FunctionAnalysisManager *FAM_,
307307
else
308308
BBDupThreshold = DefaultBBDupThreshold;
309309

310-
// JumpThreading must not processes blocks unreachable from entry. It's a
311-
// waste of compute time and can potentially lead to hangs.
312-
SmallPtrSet<BasicBlock *, 16> Unreachable;
313310
assert(DTU && "DTU isn't passed into JumpThreading before using it.");
314311
assert(DTU->hasDomTree() && "JumpThreading relies on DomTree to proceed.");
315312
DominatorTree &DT = DTU->getDomTree();
313+
314+
Unreachable.clear();
316315
for (auto &BB : *F)
317316
if (!DT.isReachableFromEntry(&BB))
318317
Unreachable.insert(&BB);
@@ -1895,6 +1894,11 @@ bool JumpThreadingPass::maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB) {
18951894
SinglePred == BB || hasAddressTakenAndUsed(BB))
18961895
return false;
18971896

1897+
// MergeBasicBlockIntoOnlyPred may delete SinglePred, we need to avoid
1898+
// deleting a BB pointer from Unreachable.
1899+
if (Unreachable.count(SinglePred))
1900+
return false;
1901+
18981902
// If SinglePred was a loop header, BB becomes one.
18991903
if (LoopHeaders.erase(SinglePred))
19001904
LoopHeaders.insert(BB);

llvm/test/Transforms/JumpThreading/pr62908.ll

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@
55

66
define i32 @test() {
77
; CHECK-LABEL: define i32 @test() {
8-
; CHECK-NEXT: end:
8+
; CHECK-NEXT: join.thread:
9+
; CHECK-NEXT: br label [[END:%.*]]
10+
; CHECK: unreachable:
11+
; CHECK-NEXT: [[SH_PROM:%.*]] = zext i32 -1 to i64
12+
; CHECK-NEXT: [[SHL:%.*]] = shl nsw i64 -1, [[SH_PROM]]
13+
; CHECK-NEXT: [[CONV:%.*]] = trunc i64 [[SHL]] to i32
14+
; CHECK-NEXT: br label [[JOIN:%.*]]
15+
; CHECK: join:
16+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[CONV]], [[UNREACHABLE:%.*]] ]
17+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[PHI]], 0
18+
; CHECK-NEXT: br i1 [[CMP]], label [[END]], label [[END]]
19+
; CHECK: end:
920
; CHECK-NEXT: ret i32 0
1021
;
1122
entry:

0 commit comments

Comments
 (0)