Skip to content

Commit a6342c2

Browse files
author
Zheng Chen
committed
[SCEV] accurate range for addrecexpr with nuw flag
If addrecexpr has nuw flag, the value should never be less than its start value and start value does not required to be SCEVConstant. Reviewed By: nikic, sanjoy Differential Revision: https://reviews.llvm.org/D71690
1 parent 0113cf1 commit a6342c2

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5672,11 +5672,12 @@ ScalarEvolution::getRangeRef(const SCEV *S,
56725672
if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
56735673
// If there's no unsigned wrap, the value will never be less than its
56745674
// initial value.
5675-
if (AddRec->hasNoUnsignedWrap())
5676-
if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart()))
5677-
if (!C->getValue()->isZero())
5678-
ConservativeResult = ConservativeResult.intersectWith(
5679-
ConstantRange(C->getAPInt(), APInt(BitWidth, 0)), RangeType);
5675+
if (AddRec->hasNoUnsignedWrap()) {
5676+
APInt UnsignedMinValue = getUnsignedRangeMin(AddRec->getStart());
5677+
if (!UnsignedMinValue.isNullValue())
5678+
ConservativeResult = ConservativeResult.intersectWith(
5679+
ConstantRange(UnsignedMinValue, APInt(BitWidth, 0)), RangeType);
5680+
}
56805681

56815682
// If there's no signed wrap, and all the operands except initial value have
56825683
// the same sign or zero, the value won't ever be:

llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
; copied from flags-from-poison.ll
44
; CHECK-LABEL: @test-add-nuw
5-
; CHECK: --> {(1 + %offset)<nuw>,+,1}<nuw><%loop> U: full-set S: full-set
5+
; CHECK: --> {(1 + %offset)<nuw>,+,1}<nuw><%loop> U: [1,0) S: [1,0)
66
define void @test-add-nuw(float* %input, i32 %offset, i32 %numIterations) {
77
entry:
88
br label %loop
@@ -20,7 +20,7 @@ exit:
2020
}
2121

2222
; CHECK-LABEL: @test-addrec-nuw
23-
; CHECK: --> {(1 + (10 smax %offset))<nuw>,+,1}<nuw><%loop> U: full-set S: full-set
23+
; CHECK: --> {(1 + (10 smax %offset))<nuw>,+,1}<nuw><%loop> U: [11,0) S: [11,0)
2424
define void @test-addrec-nuw(float* %input, i32 %offset, i32 %numIterations) {
2525
entry:
2626
%cmp = icmp sgt i32 %offset, 10

0 commit comments

Comments
 (0)