Skip to content

Commit 6ed152a

Browse files
committed
[SCEV] Compute AddRec range computations using different type BECount
Before this patch, we can only use the MaxBECount for an AddRec's range computation if the MaxBECount has <= bit width of the AddRec. This patch reasons that if a MaxBECount has > bit width, and is <= the max value of AddRec's bit width, we can still use the MaxBECount. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D151698
1 parent 46c59a5 commit 6ed152a

File tree

8 files changed

+32
-30
lines changed

8 files changed

+32
-30
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6703,8 +6703,15 @@ const ConstantRange &ScalarEvolution::getRangeRef(
67036703
getConstantMaxBackedgeTakenCount(AddRec->getLoop());
67046704
if (!isa<SCEVCouldNotCompute>(MaxBEScev)) {
67056705
APInt MaxBECount = cast<SCEVConstant>(MaxBEScev)->getAPInt();
6706-
if (MaxBECount.getBitWidth() < BitWidth)
6706+
6707+
// Adjust MaxBECount to the same bitwidth as AddRec. We can truncate if
6708+
// MaxBECount's active bits are all <= AddRec's bit width.
6709+
if (MaxBECount.getBitWidth() > BitWidth &&
6710+
MaxBECount.getActiveBits() <= BitWidth)
6711+
MaxBECount = MaxBECount.trunc(BitWidth);
6712+
else if (MaxBECount.getBitWidth() < BitWidth)
67076713
MaxBECount = MaxBECount.zext(BitWidth);
6714+
67086715
if (MaxBECount.getBitWidth() == BitWidth) {
67096716
auto RangeFromAffine = getRangeForAffineAR(
67106717
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount);

llvm/test/Analysis/DependenceAnalysis/NonCanonicalizedSubscript.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ define void @coupled_miv_type_mismatch(i32 %n) #0 {
5252
entry:
5353
br label %for.cond
5454

55-
; DELIN: da analyze - input [* *]!
56-
; DELIN: da analyze - anti [* *|<]!
55+
; DELIN: da analyze - none!
56+
; DELIN: da analyze - consistent anti [1 -2]!
5757
; DELIN: da analyze - none!
5858
for.cond: ; preds = %for.inc11, %entry
5959
%indvars.iv11 = phi i64 [ %indvars.iv.next12, %for.inc11 ], [ 1, %entry ]

llvm/test/Analysis/ScalarEvolution/increasing-or-decreasing-iv.ll

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ define void @f4(i1 %c) {
233233
; CHECK-NEXT: %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
234234
; CHECK-NEXT: --> {%start,+,%step}<nsw><%loop> U: [0,128) S: [0,128) Exits: ((127 * %step)<nsw> + %start) LoopDispositions: { %loop: Computable }
235235
; CHECK-NEXT: %iv.trunc = trunc i32 %iv to i16
236-
; CHECK-NEXT: --> {(trunc i32 %start to i16),+,(trunc i32 %step to i16)}<%loop> U: full-set S: full-set Exits: ((trunc i32 %start to i16) + (127 * (trunc i32 %step to i16))<nsw>) LoopDispositions: { %loop: Computable }
236+
; CHECK-NEXT: --> {(trunc i32 %start to i16),+,(trunc i32 %step to i16)}<%loop> U: [0,128) S: [0,128) Exits: ((trunc i32 %start to i16) + (127 * (trunc i32 %step to i16))<nsw>) LoopDispositions: { %loop: Computable }
237237
; CHECK-NEXT: %iv.next = add i32 %iv, %step
238238
; CHECK-NEXT: --> {(%step + %start),+,%step}<nw><%loop> U: [-256,256) S: [-256,256) Exits: ((128 * %step)<nsw> + %start) LoopDispositions: { %loop: Computable }
239239
; CHECK-NEXT: %loop.iv.inc = add i32 %loop.iv, 1
@@ -247,11 +247,6 @@ define void @f4(i1 %c) {
247247
; CHECK: Loop %loop: Trip multiple is 128
248248
;
249249

250-
; @f4() demonstrates a case where SCEV is not able to compute a
251-
; precise range for %iv.trunc, though it should be able to, in theory.
252-
; This is because SCEV looks into affine add recurrences only when the
253-
; backedge taken count of the loop has the same bitwidth as the
254-
; induction variable.
255250
entry:
256251
%start = select i1 %c, i32 127, i32 0
257252
%step = select i1 %c, i32 -1, i32 1

llvm/test/Analysis/ScalarEvolution/limit-depth.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ exit:
115115
define void @test_trunc(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) {
116116
; CHECK-LABEL: @test_trunc
117117
; CHECK: %trunc2 = trunc i64 %iv2.inc to i32
118-
; CHECK-NEXT: --> {(trunc i64 (1 + {7,+,1}<%loop>) to i32),+,1}<%loop2>
118+
; CHECK-NEXT: --> {(trunc i64 (1 + {7,+,1}<%loop>)<nuw><nsw> to i32),+,1}<%loop2> U: [8,53) S: [8,53) --> 52 U: [52,53) S: [52,53)
119119
entry:
120120
br label %loop
121121

llvm/test/Analysis/ScalarEvolution/nsw.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ define i32 @PR12375(ptr readnone %arg) {
174174
; CHECK-NEXT: %tmp2 = phi ptr [ %arg, %bb ], [ %tmp5, %bb1 ]
175175
; CHECK-NEXT: --> {%arg,+,4}<nuw><%bb1> U: full-set S: full-set Exits: (4 + %arg)<nuw> LoopDispositions: { %bb1: Computable }
176176
; CHECK-NEXT: %tmp3 = phi i32 [ 0, %bb ], [ %tmp4, %bb1 ]
177-
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%bb1> U: [0,-2147483648) S: [0,-2147483648) Exits: 1 LoopDispositions: { %bb1: Computable }
177+
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%bb1> U: [0,2) S: [0,2) Exits: 1 LoopDispositions: { %bb1: Computable }
178178
; CHECK-NEXT: %tmp4 = add nsw i32 %tmp3, 1
179-
; CHECK-NEXT: --> {1,+,1}<nuw><%bb1> U: [1,0) S: [1,0) Exits: 2 LoopDispositions: { %bb1: Computable }
179+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%bb1> U: [1,3) S: [1,3) Exits: 2 LoopDispositions: { %bb1: Computable }
180180
; CHECK-NEXT: %tmp5 = getelementptr inbounds i32, ptr %tmp2, i64 1
181181
; CHECK-NEXT: --> {(4 + %arg)<nuw>,+,4}<nuw><%bb1> U: [4,0) S: [4,0) Exits: (8 + %arg)<nuw> LoopDispositions: { %bb1: Computable }
182182
; CHECK-NEXT: Determining loop execution counts for: @PR12375

llvm/test/Analysis/ScalarEvolution/trip-count-negative-stride.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ define void @step_is_neg_addrec_slt_8(i64 %n) {
602602
; CHECK-LABEL: 'step_is_neg_addrec_slt_8'
603603
; CHECK-NEXT: Determining loop execution counts for: @step_is_neg_addrec_slt_8
604604
; CHECK-NEXT: Loop %inner: backedge-taken count is (7 /u {0,+,-1}<nuw><nsw><%outer.header>)
605-
; CHECK-NEXT: Loop %inner: constant max backedge-taken count is -2147483640
605+
; CHECK-NEXT: Loop %inner: constant max backedge-taken count is 8
606606
; CHECK-NEXT: Loop %inner: symbolic max backedge-taken count is (7 /u {0,+,-1}<nuw><nsw><%outer.header>)
607607
; CHECK-NEXT: Loop %inner: Predicated backedge-taken count is (7 /u {0,+,-1}<nuw><nsw><%outer.header>)
608608
; CHECK-NEXT: Predicates:
@@ -643,10 +643,10 @@ exit:
643643
define void @step_is_neg_addrec_slt_var(i32 %n) {
644644
; CHECK-LABEL: 'step_is_neg_addrec_slt_var'
645645
; CHECK-NEXT: Determining loop execution counts for: @step_is_neg_addrec_slt_var
646-
; CHECK-NEXT: Loop %inner: backedge-taken count is ((((-1 * (1 umin ({0,+,1}<nuw><%outer.header> + ({0,+,-1}<nuw><nsw><%outer.header> smax %n))))<nuw><nsw> + {0,+,1}<nuw><%outer.header> + ({0,+,-1}<nuw><nsw><%outer.header> smax %n)) /u (1 umax {0,+,-1}<nuw><nsw><%outer.header>)) + (1 umin ({0,+,1}<nuw><%outer.header> + ({0,+,-1}<nuw><nsw><%outer.header> smax %n))))
647-
; CHECK-NEXT: Loop %inner: constant max backedge-taken count is -1
648-
; CHECK-NEXT: Loop %inner: symbolic max backedge-taken count is ((((-1 * (1 umin ({0,+,1}<nuw><%outer.header> + ({0,+,-1}<nuw><nsw><%outer.header> smax %n))))<nuw><nsw> + {0,+,1}<nuw><%outer.header> + ({0,+,-1}<nuw><nsw><%outer.header> smax %n)) /u (1 umax {0,+,-1}<nuw><nsw><%outer.header>)) + (1 umin ({0,+,1}<nuw><%outer.header> + ({0,+,-1}<nuw><nsw><%outer.header> smax %n))))
649-
; CHECK-NEXT: Loop %inner: Predicated backedge-taken count is ((((-1 * (1 umin ({0,+,1}<nuw><%outer.header> + ({0,+,-1}<nuw><nsw><%outer.header> smax %n))))<nuw><nsw> + {0,+,1}<nuw><%outer.header> + ({0,+,-1}<nuw><nsw><%outer.header> smax %n)) /u (1 umax {0,+,-1}<nuw><nsw><%outer.header>)) + (1 umin ({0,+,1}<nuw><%outer.header> + ({0,+,-1}<nuw><nsw><%outer.header> smax %n))))
646+
; CHECK-NEXT: Loop %inner: backedge-taken count is ({0,+,1}<nuw><nsw><%outer.header> + ({0,+,-1}<nsw><%outer.header> smax %n))
647+
; CHECK-NEXT: Loop %inner: constant max backedge-taken count is 2147483647
648+
; CHECK-NEXT: Loop %inner: symbolic max backedge-taken count is ({0,+,1}<nuw><nsw><%outer.header> + ({0,+,-1}<nsw><%outer.header> smax %n))
649+
; CHECK-NEXT: Loop %inner: Predicated backedge-taken count is ({0,+,1}<nuw><nsw><%outer.header> + ({0,+,-1}<nsw><%outer.header> smax %n))
650650
; CHECK-NEXT: Predicates:
651651
; CHECK: Loop %inner: Trip multiple is 1
652652
; CHECK-NEXT: Loop %outer.header: backedge-taken count is 0
@@ -685,10 +685,10 @@ exit:
685685
define void @step_is_neg_addrec_unknown_start(i32 %n) {
686686
; CHECK-LABEL: 'step_is_neg_addrec_unknown_start'
687687
; CHECK-NEXT: Determining loop execution counts for: @step_is_neg_addrec_unknown_start
688-
; CHECK-NEXT: Loop %inner: backedge-taken count is ((((-1 * (1 umin ({(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nuw><nsw><%outer.header>))))<nuw><nsw> + {(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nuw><nsw><%outer.header>)) /u (1 umax {0,+,-1}<%outer.header>)) + (1 umin ({(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nuw><nsw><%outer.header>))))
688+
; CHECK-NEXT: Loop %inner: backedge-taken count is ({(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nsw><%outer.header>))
689689
; CHECK-NEXT: Loop %inner: constant max backedge-taken count is -2147483640
690-
; CHECK-NEXT: Loop %inner: symbolic max backedge-taken count is ((((-1 * (1 umin ({(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nuw><nsw><%outer.header>))))<nuw><nsw> + {(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nuw><nsw><%outer.header>)) /u (1 umax {0,+,-1}<%outer.header>)) + (1 umin ({(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nuw><nsw><%outer.header>))))
691-
; CHECK-NEXT: Loop %inner: Predicated backedge-taken count is ((((-1 * (1 umin ({(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nuw><nsw><%outer.header>))))<nuw><nsw> + {(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nuw><nsw><%outer.header>)) /u (1 umax {0,+,-1}<%outer.header>)) + (1 umin ({(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nuw><nsw><%outer.header>))))
690+
; CHECK-NEXT: Loop %inner: symbolic max backedge-taken count is ({(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nsw><%outer.header>))
691+
; CHECK-NEXT: Loop %inner: Predicated backedge-taken count is ({(-1 * %n),+,1}<nw><%outer.header> + (8 smax {%n,+,-1}<nsw><%outer.header>))
692692
; CHECK-NEXT: Predicates:
693693
; CHECK: Loop %inner: Trip multiple is 1
694694
; CHECK-NEXT: Loop %outer.header: backedge-taken count is 0

polly/test/CodeGen/switch-in-non-affine-region.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
; }
1515
; }
1616
;
17-
; CHECK: polly.stmt.if.then:
18-
; CHECK: %[[trunc:.*]] = trunc i64 %polly.indvar to i32
19-
; CHECK: %p_rem = srem i32 %[[trunc]], 4
20-
; CHECK: switch i32 %p_rem, label %polly.stmt.sw.epilog [
21-
; CHECK: i32 0, label %polly.stmt.sw.bb
22-
; CHECK: i32 1, label %polly.stmt.sw.bb.3
23-
; CHECK: ]
17+
; CHECK: polly.stmt.if.then:
18+
; CHECK-NEXT: %2 = trunc i64 %polly.indvar to i2
19+
; CHECK-NEXT: %3 = zext i2 %2 to i32
20+
; CHECK-NEXT: switch i32 %3, label %polly.stmt.sw.epilog [
21+
; CHECK-NEXT: i32 0, label %polly.stmt.sw.bb
22+
; CHECK-NEXT: i32 1, label %polly.stmt.sw.bb.3
23+
; CHECK-NEXT: ]
2424
;
2525
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
2626

polly/test/ScopInfo/NonAffine/non-affine-loop-condition-dependent-access_3.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
; INNERMOST-NEXT: Invariant Accesses: {
1515
; INNERMOST-NEXT: }
1616
; INNERMOST-NEXT: Context:
17-
; INNERMOST-NEXT: [p_0, p_1, p_2] -> { : 0 <= p_0 <= 2147483647 and 0 <= p_1 <= 1024 and 0 <= p_2 <= 1024 }
17+
; INNERMOST-NEXT: [p_0, p_1, p_2] -> { : 0 <= p_0 <= 1048576 and 0 <= p_1 <= 1024 and 0 <= p_2 <= 1024 }
1818
; INNERMOST-NEXT: Assumed Context:
1919
; INNERMOST-NEXT: [p_0, p_1, p_2] -> { : }
2020
; INNERMOST-NEXT: Invalid Context:
@@ -89,9 +89,9 @@
8989
; ALL-NEXT: ReadAccess := [Reduction Type: NONE] [Scalar: 0]
9090
; ALL-NEXT: { Stmt_bb15__TO__bb25[i0, i1] -> MemRef_A[i1] };
9191
; ALL-NEXT: ReadAccess := [Reduction Type: NONE] [Scalar: 0]
92-
; ALL-NEXT: { Stmt_bb15__TO__bb25[i0, i1] -> MemRef_A[o0] : 0 <= o0 <= 2147483647 };
92+
; ALL-NEXT: { Stmt_bb15__TO__bb25[i0, i1] -> MemRef_A[o0] : 0 <= o0 <= 1048576 };
9393
; ALL-NEXT: MayWriteAccess := [Reduction Type: NONE] [Scalar: 0]
94-
; ALL-NEXT: { Stmt_bb15__TO__bb25[i0, i1] -> MemRef_A[o0] : 0 <= o0 <= 2147483647 };
94+
; ALL-NEXT: { Stmt_bb15__TO__bb25[i0, i1] -> MemRef_A[o0] : 0 <= o0 <= 1048576 };
9595
; ALL-NEXT: }
9696
;
9797
; void f(int *A) {

0 commit comments

Comments
 (0)