Skip to content

Commit 914836b

Browse files
committed
[SCEV] Infer nsw/nuw from nw for addrecs
If we no an addrec doesn't self-wrap, the increment is strictly positive, and the start value is the smallest representable value, then we know that the corresponding wrap type can not occur. Differential Revision: https://reviews.llvm.org/D108601
1 parent fda1768 commit 914836b

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,19 @@ StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type,
23902390
}
23912391
}
23922392

2393+
// <0,+,nonnegative><nw> is also nuw
2394+
// <INT_MIN,+,nonnegative><nw> is also nsw
2395+
if (Type == scAddRecExpr && ScalarEvolution::hasFlags(Flags, SCEV::FlagNW) &&
2396+
Ops.size() == 2) {
2397+
if (!ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) && Ops[0]->isZero() &&
2398+
IsKnownNonNegative(Ops[1]))
2399+
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
2400+
if (!ScalarEvolution::hasFlags(Flags, SCEV::FlagNSW) &&
2401+
isa<SCEVConstant>(Ops[0]) &&
2402+
cast<SCEVConstant>(Ops[0])->getAPInt().isMinSignedValue())
2403+
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
2404+
}
2405+
23932406
return Flags;
23942407
}
23952408

llvm/test/Analysis/ScalarEvolution/ptrtoint.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ define void @pr46786_c26_char(i8* %arg, i8* %arg1, i8* %arg2) {
386386
; X64-NEXT: %i9 = ptrtoint i8* %i7 to i64
387387
; X64-NEXT: --> {(ptrtoint i8* %arg to i64),+,1}<nuw><%bb6> U: full-set S: full-set Exits: (-1 + (ptrtoint i8* %arg1 to i64)) LoopDispositions: { %bb6: Computable }
388388
; X64-NEXT: %i10 = sub i64 %i9, %i4
389-
; X64-NEXT: --> {0,+,1}<nw><%bb6> U: full-set S: full-set Exits: (-1 + (-1 * (ptrtoint i8* %arg to i64)) + (ptrtoint i8* %arg1 to i64)) LoopDispositions: { %bb6: Computable }
389+
; X64-NEXT: --> {0,+,1}<nuw><%bb6> U: full-set S: full-set Exits: (-1 + (-1 * (ptrtoint i8* %arg to i64)) + (ptrtoint i8* %arg1 to i64)) LoopDispositions: { %bb6: Computable }
390390
; X64-NEXT: %i11 = getelementptr inbounds i8, i8* %arg2, i64 %i10
391391
; X64-NEXT: --> {%arg2,+,1}<nw><%bb6> U: full-set S: full-set Exits: (-1 + (-1 * (ptrtoint i8* %arg to i64)) + (ptrtoint i8* %arg1 to i64) + %arg2) LoopDispositions: { %bb6: Computable }
392392
; X64-NEXT: %i12 = load i8, i8* %i11, align 1
@@ -413,7 +413,7 @@ define void @pr46786_c26_char(i8* %arg, i8* %arg1, i8* %arg2) {
413413
; X32-NEXT: %i9 = ptrtoint i8* %i7 to i64
414414
; X32-NEXT: --> {(zext i32 (ptrtoint i8* %arg to i32) to i64),+,1}<nuw><%bb6> U: [0,8589934591) S: [0,8589934591) Exits: ((zext i32 (-1 + (-1 * (ptrtoint i8* %arg to i32)) + (ptrtoint i8* %arg1 to i32)) to i64) + (zext i32 (ptrtoint i8* %arg to i32) to i64)) LoopDispositions: { %bb6: Computable }
415415
; X32-NEXT: %i10 = sub i64 %i9, %i4
416-
; X32-NEXT: --> {0,+,1}<nw><%bb6> U: [0,4294967296) S: [0,4294967296) Exits: (zext i32 (-1 + (-1 * (ptrtoint i8* %arg to i32)) + (ptrtoint i8* %arg1 to i32)) to i64) LoopDispositions: { %bb6: Computable }
416+
; X32-NEXT: --> {0,+,1}<nuw><%bb6> U: [0,4294967296) S: [0,4294967296) Exits: (zext i32 (-1 + (-1 * (ptrtoint i8* %arg to i32)) + (ptrtoint i8* %arg1 to i32)) to i64) LoopDispositions: { %bb6: Computable }
417417
; X32-NEXT: %i11 = getelementptr inbounds i8, i8* %arg2, i64 %i10
418418
; X32-NEXT: --> {%arg2,+,1}<%bb6> U: full-set S: full-set Exits: (-1 + (-1 * (ptrtoint i8* %arg to i32)) + (ptrtoint i8* %arg1 to i32) + %arg2) LoopDispositions: { %bb6: Computable }
419419
; X32-NEXT: %i12 = load i8, i8* %i11, align 1
@@ -471,7 +471,7 @@ define void @pr46786_c26_int(i32* %arg, i32* %arg1, i32* %arg2) {
471471
; X64-NEXT: %i9 = ptrtoint i32* %i7 to i64
472472
; X64-NEXT: --> {(ptrtoint i32* %arg to i64),+,4}<nuw><%bb6> U: full-set S: full-set Exits: ((4 * ((-4 + (-1 * (ptrtoint i32* %arg to i64)) + (ptrtoint i32* %arg1 to i64)) /u 4))<nuw> + (ptrtoint i32* %arg to i64)) LoopDispositions: { %bb6: Computable }
473473
; X64-NEXT: %i10 = sub i64 %i9, %i4
474-
; X64-NEXT: --> {0,+,4}<nw><%bb6> U: [0,-3) S: [-9223372036854775808,9223372036854775805) Exits: (4 * ((-4 + (-1 * (ptrtoint i32* %arg to i64)) + (ptrtoint i32* %arg1 to i64)) /u 4))<nuw> LoopDispositions: { %bb6: Computable }
474+
; X64-NEXT: --> {0,+,4}<nuw><%bb6> U: [0,-3) S: [-9223372036854775808,9223372036854775805) Exits: (4 * ((-4 + (-1 * (ptrtoint i32* %arg to i64)) + (ptrtoint i32* %arg1 to i64)) /u 4))<nuw> LoopDispositions: { %bb6: Computable }
475475
; X64-NEXT: %i11 = ashr exact i64 %i10, 2
476476
; X64-NEXT: --> %i11 U: [-2305843009213693952,2305843009213693952) S: [-2305843009213693952,2305843009213693952) Exits: <<Unknown>> LoopDispositions: { %bb6: Variant }
477477
; X64-NEXT: %i12 = getelementptr inbounds i32, i32* %arg2, i64 %i11
@@ -500,7 +500,7 @@ define void @pr46786_c26_int(i32* %arg, i32* %arg1, i32* %arg2) {
500500
; X32-NEXT: %i9 = ptrtoint i32* %i7 to i64
501501
; X32-NEXT: --> {(zext i32 (ptrtoint i32* %arg to i32) to i64),+,4}<nuw><%bb6> U: [0,8589934588) S: [0,8589934588) Exits: ((zext i32 (ptrtoint i32* %arg to i32) to i64) + (4 * ((zext i32 (-4 + (-1 * (ptrtoint i32* %arg to i32)) + (ptrtoint i32* %arg1 to i32)) to i64) /u 4))<nuw><nsw>) LoopDispositions: { %bb6: Computable }
502502
; X32-NEXT: %i10 = sub i64 %i9, %i4
503-
; X32-NEXT: --> {0,+,4}<nw><%bb6> U: [0,4294967293) S: [0,4294967293) Exits: (4 * ((zext i32 (-4 + (-1 * (ptrtoint i32* %arg to i32)) + (ptrtoint i32* %arg1 to i32)) to i64) /u 4))<nuw><nsw> LoopDispositions: { %bb6: Computable }
503+
; X32-NEXT: --> {0,+,4}<nuw><%bb6> U: [0,4294967293) S: [0,4294967293) Exits: (4 * ((zext i32 (-4 + (-1 * (ptrtoint i32* %arg to i32)) + (ptrtoint i32* %arg1 to i32)) to i64) /u 4))<nuw><nsw> LoopDispositions: { %bb6: Computable }
504504
; X32-NEXT: %i11 = ashr exact i64 %i10, 2
505505
; X32-NEXT: --> %i11 U: [-2147483648,2147483648) S: [-2147483648,2147483648) Exits: <<Unknown>> LoopDispositions: { %bb6: Variant }
506506
; X32-NEXT: %i12 = getelementptr inbounds i32, i32* %arg2, i64 %i11

0 commit comments

Comments
 (0)