Skip to content

Commit 14b871f

Browse files
committed
handle range attribute in isKnownNonZero
1 parent 7180453 commit 14b871f

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,11 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
27832783
} else {
27842784
if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
27852785
return rangeMetadataExcludesValue(Ranges, APInt::getZero(BitWidth));
2786+
if (std::optional<ConstantRange> Range = Call->getRange()) {
2787+
const APInt ZeroValue(Range->getBitWidth(), 0);
2788+
if (!Range->contains(ZeroValue))
2789+
return true;
2790+
}
27862791
if (const Value *RV = Call->getReturnedArgOperand())
27872792
if (RV->getType() == I->getType() && isKnownNonZero(RV, Depth, Q))
27882793
return true;
@@ -2921,6 +2926,13 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
29212926
return false;
29222927
}
29232928

2929+
if (const auto *A = dyn_cast<Argument>(V))
2930+
if (std::optional<ConstantRange> Range = A->getRange()) {
2931+
const APInt ZeroValue(Range->getBitWidth(), 0);
2932+
if (!Range->contains(ZeroValue))
2933+
return true;
2934+
}
2935+
29242936
if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
29252937
return true;
29262938

llvm/test/Analysis/ValueTracking/known-non-zero.ll

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,9 +1305,7 @@ define <2 x i1> @range_metadata_vec(ptr %p, <2 x i32> %x) {
13051305

13061306
define i1 @range_attr(i8 range(i8 1, 0) %x, i8 %y) {
13071307
; CHECK-LABEL: @range_attr(
1308-
; CHECK-NEXT: [[I:%.*]] = or i8 [[Y:%.*]], [[X:%.*]]
1309-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I]], 0
1310-
; CHECK-NEXT: ret i1 [[CMP]]
1308+
; CHECK-NEXT: ret i1 false
13111309
;
13121310
%or = or i8 %y, %x
13131311
%cmp = icmp eq i8 %or, 0
@@ -1331,9 +1329,7 @@ declare range(i8 -1, 1) i8 @returns_contain_zero_range_helper()
13311329
define i1 @range_return(i8 %y) {
13321330
; CHECK-LABEL: @range_return(
13331331
; CHECK-NEXT: [[I:%.*]] = call i8 @returns_non_zero_range_helper()
1334-
; CHECK-NEXT: [[OR:%.*]] = or i8 [[Y:%.*]], [[I]]
1335-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[OR]], 0
1336-
; CHECK-NEXT: ret i1 [[CMP]]
1332+
; CHECK-NEXT: ret i1 false
13371333
;
13381334
%x = call i8 @returns_non_zero_range_helper()
13391335
%or = or i8 %y, %x
@@ -1359,9 +1355,7 @@ declare i8 @returns_i8_helper()
13591355
define i1 @range_call(i8 %y) {
13601356
; CHECK-LABEL: @range_call(
13611357
; CHECK-NEXT: [[I:%.*]] = call range(i8 1, 0) i8 @returns_i8_helper()
1362-
; CHECK-NEXT: [[OR:%.*]] = or i8 [[Y:%.*]], [[I]]
1363-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[OR]], 0
1364-
; CHECK-NEXT: ret i1 [[CMP]]
1358+
; CHECK-NEXT: ret i1 false
13651359
;
13661360
%x = call range(i8 1, 0) i8 @returns_i8_helper()
13671361
%or = or i8 %y, %x
@@ -1384,9 +1378,7 @@ define i1 @neg_range_call(i8 %y) {
13841378

13851379
define <2 x i1> @range_attr_vec(<2 x i8> range(i8 1, 0) %x, <2 x i8> %y) {
13861380
; CHECK-LABEL: @range_attr_vec(
1387-
; CHECK-NEXT: [[OR:%.*]] = or <2 x i8> [[Y:%.*]], [[X:%.*]]
1388-
; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[OR]], zeroinitializer
1389-
; CHECK-NEXT: ret <2 x i1> [[CMP]]
1381+
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
13901382
;
13911383
%or = or <2 x i8> %y, %x
13921384
%cmp = icmp ne <2 x i8> %or, zeroinitializer
@@ -1410,9 +1402,7 @@ declare range(i8 -1, 1) <2 x i8> @returns_contain_zero_range_helper_vec()
14101402
define <2 x i1> @range_return_vec(<2 x i8> %y) {
14111403
; CHECK-LABEL: @range_return_vec(
14121404
; CHECK-NEXT: [[I:%.*]] = call <2 x i8> @returns_non_zero_range_helper_vec()
1413-
; CHECK-NEXT: [[OR:%.*]] = or <2 x i8> [[Y:%.*]], [[I]]
1414-
; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[OR]], zeroinitializer
1415-
; CHECK-NEXT: ret <2 x i1> [[CMP]]
1405+
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
14161406
;
14171407
%x = call <2 x i8> @returns_non_zero_range_helper_vec()
14181408
%or = or <2 x i8> %y, %x
@@ -1438,9 +1428,7 @@ declare <2 x i8> @returns_i8_helper_vec()
14381428
define <2 x i1> @range_call_vec(<2 x i8> %y) {
14391429
; CHECK-LABEL: @range_call_vec(
14401430
; CHECK-NEXT: [[I:%.*]] = call range(i8 1, 0) <2 x i8> @returns_i8_helper_vec()
1441-
; CHECK-NEXT: [[OR:%.*]] = or <2 x i8> [[Y:%.*]], [[I]]
1442-
; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[OR]], zeroinitializer
1443-
; CHECK-NEXT: ret <2 x i1> [[CMP]]
1431+
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
14441432
;
14451433
%x = call range(i8 1, 0) <2 x i8> @returns_i8_helper_vec()
14461434
%or = or <2 x i8> %y, %x

0 commit comments

Comments
 (0)