[llvm] 569ccfc - [SCEV] more accurate range for addrecexpr with nsw flag.
Zheng Chen via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 11 20:27:30 PST 2020
Author: Zheng Chen
Date: 2020-01-11T23:26:35-05:00
New Revision: 569ccfc384a5434c35c09adba8c44c46014297e6
URL: https://github.com/llvm/llvm-project/commit/569ccfc384a5434c35c09adba8c44c46014297e6
DIFF: https://github.com/llvm/llvm-project/commit/569ccfc384a5434c35c09adba8c44c46014297e6.diff
LOG: [SCEV] more accurate range for addrecexpr with nsw flag.
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D72436
Added:
Modified:
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 7d5c71eafed6..f8d23c40e0c9 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -5678,23 +5678,31 @@ ScalarEvolution::getRangeRef(const SCEV *S,
ConservativeResult = ConservativeResult.intersectWith(
ConstantRange(C->getAPInt(), APInt(BitWidth, 0)), RangeType);
- // If there's no signed wrap, and all the operands have the same sign or
- // zero, the value won't ever change sign.
+ // If there's no signed wrap, and all the operands except initial value have
+ // the same sign or zero, the value won't ever be:
+ // 1: smaller than initial value if operands are non negative,
+ // 2: bigger than initial value if operands are non positive.
+ // For both cases, value can not cross signed min/max boundary.
if (AddRec->hasNoSignedWrap()) {
bool AllNonNeg = true;
bool AllNonPos = true;
- for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
- if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false;
- if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false;
+ for (unsigned i = 1, e = AddRec->getNumOperands(); i != e; ++i) {
+ if (!isKnownNonNegative(AddRec->getOperand(i)))
+ AllNonNeg = false;
+ if (!isKnownNonPositive(AddRec->getOperand(i)))
+ AllNonPos = false;
}
if (AllNonNeg)
ConservativeResult = ConservativeResult.intersectWith(
- ConstantRange(APInt(BitWidth, 0),
- APInt::getSignedMinValue(BitWidth)), RangeType);
+ ConstantRange::getNonEmpty(getSignedRangeMin(AddRec->getStart()),
+ APInt::getSignedMinValue(BitWidth)),
+ RangeType);
else if (AllNonPos)
ConservativeResult = ConservativeResult.intersectWith(
- ConstantRange(APInt::getSignedMinValue(BitWidth),
- APInt(BitWidth, 1)), RangeType);
+ ConstantRange::getNonEmpty(
+ APInt::getSignedMinValue(BitWidth),
+ getSignedRangeMax(AddRec->getStart()) + 1),
+ RangeType);
}
// TODO: non-affine addrec
diff --git a/llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll b/llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll
index 336a84472410..ae447dcdfa57 100644
--- a/llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll
+++ b/llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll
@@ -40,7 +40,7 @@ exit:
}
; CHECK-LABEL: @test-addrec-nsw-start-neg-strip-neg
-; CHECK: --> {(-1 + (-10 smin %offset))<nsw>,+,-1}<nsw><%loop> U: [-2147483648,1) S: [-2147483648,1)
+; CHECK: --> {(-1 + (-10 smin %offset))<nsw>,+,-1}<nsw><%loop> U: [-2147483648,-10) S: [-2147483648,-10)
define void @test-addrec-nsw-start-neg-strip-neg(float* %input, i32 %offset, i32 %numIterations) {
entry:
%cmp = icmp slt i32 %offset, -10
@@ -60,7 +60,7 @@ exit:
}
; CHECK-LABEL: @test-addrec-nsw-start-pos-strip-neg
-; CHECK: --> {(-1 + (10 smin %offset))<nsw>,+,-1}<nsw><%loop> U: full-set S: full-set
+; CHECK: --> {(-1 + (10 smin %offset))<nsw>,+,-1}<nsw><%loop> U: [-2147483648,10) S: [-2147483648,10)
define void @test-addrec-nsw-start-pos-strip-neg(float* %input, i32 %offset, i32 %numIterations) {
entry:
%cmp = icmp slt i32 %offset, 10
@@ -80,7 +80,7 @@ exit:
}
; CHECK-LABEL: @test-addrec-nsw-start-pos-strip-pos
-; CHECK: --> {(1 + (10 smax %offset))<nuw><nsw>,+,1}<nuw><nsw><%loop> U: [0,-2147483648) S: [0,-2147483648)
+; CHECK: --> {(1 + (10 smax %offset))<nuw><nsw>,+,1}<nuw><nsw><%loop> U: [11,-2147483648) S: [11,-2147483648)
define void @test-addrec-nsw-start-pos-strip-pos(float* %input, i32 %offset, i32 %numIterations) {
entry:
%cmp = icmp sgt i32 %offset, 10
@@ -100,7 +100,7 @@ exit:
}
; CHECK-LABEL: @test-addrec-nsw-start-neg-strip-pos
-; CHECK: --> {(1 + (-10 smax %offset))<nsw>,+,1}<nsw><%loop> U: full-set S: full-set
+; CHECK: --> {(1 + (-10 smax %offset))<nsw>,+,1}<nsw><%loop> U: [-9,-2147483648) S: [-9,-2147483648)
define void @test-addrec-nsw-start-neg-strip-pos(float* %input, i32 %offset, i32 %numIterations) {
entry:
%cmp = icmp sgt i32 %offset, -10
More information about the llvm-commits
mailing list