Skip to content

Commit 2071ea2

Browse files
authored
[LVI] Skip self loops in solveBlockValueNonLocal (#127763)
We cannot infer more information from backedges in `solveBlockValueNonLocal`. However, since DT is unavailable in LVI, there is not a precise way to check whether a BB edge is a backedge. This patch only skips self loops to unblock the range analysis. The motivating case is extracted from #127663. Compile-time impact is high: https://llvm-compile-time-tracker.com/compare.php?from=84ddda58c870681dd12ed765e9d59d5e00567f94&to=af032f1351358f2f5b5d9f4e87c5601c23b9bd37&stat=instructions:u
1 parent 96c7233 commit 2071ea2

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ LazyValueInfoImpl::solveBlockValueNonLocal(Value *Val, BasicBlock *BB) {
694694
// canonicalizing to make this true rather than relying on this happy
695695
// accident.
696696
for (BasicBlock *Pred : predecessors(BB)) {
697+
// Skip self loops.
698+
if (Pred == BB)
699+
continue;
697700
std::optional<ValueLatticeElement> EdgeResult = getEdgeValue(Val, Pred, BB);
698701
if (!EdgeResult)
699702
// Explore that input, then return here
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -passes=correlated-propagation -S %s | FileCheck %s
3+
4+
define i64 @test_self_loop(i64 %x, i1 %cond) {
5+
; CHECK-LABEL: define range(i64 0, 576460752303423488) i64 @test_self_loop(
6+
; CHECK-SAME: i64 [[X:%.*]], i1 [[COND:%.*]]) {
7+
; CHECK-NEXT: [[ENTRY:.*]]:
8+
; CHECK-NEXT: [[COND1:%.*]] = icmp ugt i64 [[X]], 576460752303423487
9+
; CHECK-NEXT: br i1 [[COND1]], label %[[COMMON_RET:.*]], label %[[LOOP:.*]]
10+
; CHECK: [[LOOP]]:
11+
; CHECK-NEXT: br i1 [[COND]], label %[[INDIRECT:.*]], label %[[LOOP]]
12+
; CHECK: [[INDIRECT]]:
13+
; CHECK-NEXT: [[COND2:%.*]] = icmp eq i64 [[X]], 0
14+
; CHECK-NEXT: br i1 [[COND2]], label %[[COMMON_RET]], label %[[EXIT:.*]]
15+
; CHECK: [[EXIT]]:
16+
; CHECK-NEXT: br label %[[COMMON_RET]]
17+
; CHECK: [[COMMON_RET]]:
18+
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[X]], %[[EXIT]] ], [ 0, %[[INDIRECT]] ], [ 0, %[[ENTRY]] ]
19+
; CHECK-NEXT: ret i64 [[RES]]
20+
;
21+
entry:
22+
%cond1 = icmp ugt i64 %x, 576460752303423487
23+
br i1 %cond1, label %common.ret, label %loop
24+
25+
loop:
26+
br i1 %cond, label %indirect, label %loop
27+
28+
indirect:
29+
%cond2 = icmp eq i64 %x, 0
30+
br i1 %cond2, label %common.ret, label %exit
31+
32+
exit:
33+
%smax = tail call i64 @llvm.smax.i64(i64 %x, i64 1)
34+
br label %common.ret
35+
36+
common.ret:
37+
%res = phi i64 [ %smax, %exit ], [ 0, %indirect ], [ 0, %entry ]
38+
ret i64 %res
39+
}

llvm/test/Transforms/JumpThreading/ddt-crash.ll

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,16 @@ define void @spam(ptr %arg, i1 %arg2) {
131131
; CHECK: bb31:
132132
; CHECK-NEXT: [[TMP32:%.*]] = phi i8 [ [[TMP32]], [[BB31]] ], [ [[TMP32_PR]], [[BB30]] ]
133133
; CHECK-NEXT: [[TMP33:%.*]] = icmp eq i8 [[TMP32]], 0
134-
; CHECK-NEXT: br i1 [[TMP33]], label [[BB31]], label [[BB37]]
134+
; CHECK-NEXT: br i1 [[TMP33]], label [[BB31]], label [[BB41_THREAD]]
135135
; CHECK: bb37:
136-
; CHECK-NEXT: [[TMP36:%.*]] = phi i1 [ false, [[BB23]] ], [ true, [[BB31]] ]
136+
; CHECK-NEXT: [[TMP36:%.*]] = phi i1 [ false, [[BB23]] ]
137137
; CHECK-NEXT: [[TMP38:%.*]] = icmp eq ptr [[TMP15]], null
138-
; CHECK-NEXT: br i1 [[TMP38]], label [[BB39:%.*]], label [[BB41:%.*]]
139-
; CHECK: bb39:
140-
; CHECK-NEXT: [[TMP364:%.*]] = phi i1 [ [[TMP36]], [[BB37]] ]
141-
; CHECK-NEXT: [[TMP40:%.*]] = load ptr, ptr @global, align 8
142-
; CHECK-NEXT: br i1 [[TMP364]], label [[BB41_THREAD]], label [[BB41_THREAD]]
138+
; CHECK-NEXT: br i1 [[TMP38]], label [[BB41:%.*]], label [[BB41_THREAD]]
143139
; CHECK: bb41:
144-
; CHECK-NEXT: [[TMP363:%.*]] = phi i1 [ [[TMP36]], [[BB37]] ]
145-
; CHECK-NEXT: br i1 [[TMP363]], label [[BB41_THREAD]], label [[BB41_THREAD]]
146-
; CHECK: bb41.thread:
147-
; CHECK-NEXT: [[TMP0:%.*]] = phi ptr [ undef, [[BB41]] ], [ undef, [[BB39]] ], [ undef, [[BB39]] ], [ undef, [[BB41]] ], [ undef, [[BB27]] ], [ undef, [[BB25]] ]
140+
; CHECK-NEXT: [[TMP40:%.*]] = load ptr, ptr @global, align 8
141+
; CHECK-NEXT: br label [[BB41_THREAD]]
142+
; CHECK: bb41.thread11:
143+
; CHECK-NEXT: [[TMP0:%.*]] = phi ptr [ undef, [[BB41]] ], [ undef, [[BB25]] ], [ undef, [[BB31]] ], [ undef, [[BB27]] ], [ undef, [[BB37]] ]
148144
; CHECK-NEXT: ret void
149145
;
150146
bb:

0 commit comments

Comments
 (0)