Skip to content

Commit a9bc886

Browse files
committed
handle range attribute in computeConstantRange
1 parent 45303ed commit a9bc886

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9126,12 +9126,19 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
91269126
// TODO: Return ConstantRange.
91279127
setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
91289128
CR = ConstantRange::getNonEmpty(Lower, Upper);
9129-
}
9129+
} else if (const Argument *A = dyn_cast<Argument>(V))
9130+
if (std::optional<ConstantRange> Range = A->getRange())
9131+
CR = *Range;
91309132

9131-
if (auto *I = dyn_cast<Instruction>(V))
9133+
if (auto *I = dyn_cast<Instruction>(V)) {
91329134
if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
91339135
CR = CR.intersectWith(getConstantRangeFromMetadata(*Range));
91349136

9137+
if (const CallBase *CB = dyn_cast<CallBase>(V))
9138+
if (std::optional<ConstantRange> Range = CB->getRange())
9139+
CR = CR.intersectWith(*Range);
9140+
}
9141+
91359142
if (CtxI && AC) {
91369143
// Try to restrict the range based on information from assumptions.
91379144
for (auto &AssumeVH : AC->assumptionsFor(V)) {

llvm/test/Transforms/InstSimplify/icmp-constant.ll

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,8 +1271,7 @@ define <2 x i1> @neg_icmp_eq_non_zero_range_call_vec() {
12711271

12721272
define i1 @icmp_eq_constant_range_attr(i8 range(i8 0, 10) %i) {
12731273
; CHECK-LABEL: @icmp_eq_constant_range_attr(
1274-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I:%.*]], 10
1275-
; CHECK-NEXT: ret i1 [[CMP]]
1274+
; CHECK-NEXT: ret i1 false
12761275
;
12771276
%cmp = icmp eq i8 %i, 10
12781277
ret i1 %cmp
@@ -1304,8 +1303,7 @@ define i1 @icmp_eq_constant_range_return() {
13041303
define i1 @neg_icmp_eq_constant_range_return() {
13051304
; CHECK-LABEL: @neg_icmp_eq_constant_range_return(
13061305
; CHECK-NEXT: [[I:%.*]] = call i8 @returns_contain_ten_range_helper()
1307-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I]], 10
1308-
; CHECK-NEXT: ret i1 [[CMP]]
1306+
; CHECK-NEXT: ret i1 false
13091307
;
13101308
%i = call i8 @returns_contain_ten_range_helper()
13111309
%cmp = icmp eq i8 %i, 10
@@ -1315,8 +1313,7 @@ define i1 @neg_icmp_eq_constant_range_return() {
13151313
define i1 @icmp_eq_constant_range_call() {
13161314
; CHECK-LABEL: @icmp_eq_constant_range_call(
13171315
; CHECK-NEXT: [[I:%.*]] = call range(i8 0, 10) i8 @returns_i8_helper()
1318-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I]], 10
1319-
; CHECK-NEXT: ret i1 [[CMP]]
1316+
; CHECK-NEXT: ret i1 false
13201317
;
13211318
%i = call range(i8 0, 10) i8 @returns_i8_helper()
13221319
%cmp = icmp eq i8 %i, 10
@@ -1336,8 +1333,7 @@ define i1 @neg_icmp_eq_constant_range_call() {
13361333

13371334
define <2 x i1> @icmp_eq_constant_range_attr_vec(<2 x i8> range(i8 0, 10) %i) {
13381335
; CHECK-LABEL: @icmp_eq_constant_range_attr_vec(
1339-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[I:%.*]], <i8 10, i8 10>
1340-
; CHECK-NEXT: ret <2 x i1> [[CMP]]
1336+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
13411337
;
13421338
%cmp = icmp eq <2 x i8> %i, <i8 10, i8 10>
13431339
ret <2 x i1> %cmp
@@ -1358,8 +1354,7 @@ declare range(i8 0, 11) <2 x i8> @returns_contain_ten_range_helper_vec()
13581354
define <2 x i1> @icmp_eq_constant_range_return_vec() {
13591355
; CHECK-LABEL: @icmp_eq_constant_range_return_vec(
13601356
; CHECK-NEXT: [[I:%.*]] = call <2 x i8> @returns_non_ten_range_helper_vec()
1361-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[I]], <i8 10, i8 10>
1362-
; CHECK-NEXT: ret <2 x i1> [[CMP]]
1357+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
13631358
;
13641359
%i = call <2 x i8> @returns_non_ten_range_helper_vec()
13651360
%cmp = icmp eq <2 x i8> %i, <i8 10, i8 10>
@@ -1380,8 +1375,7 @@ define <2 x i1> @neg_icmp_eq_constant_range_return_vec() {
13801375
define <2 x i1> @icmp_eq_constant_range_call_vec() {
13811376
; CHECK-LABEL: @icmp_eq_constant_range_call_vec(
13821377
; CHECK-NEXT: [[I:%.*]] = call range(i8 0, 10) <2 x i8> @returns_i8_helper_vec()
1383-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[I]], <i8 10, i8 10>
1384-
; CHECK-NEXT: ret <2 x i1> [[CMP]]
1378+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
13851379
;
13861380
%i = call range(i8 0, 10) <2 x i8> @returns_i8_helper_vec()
13871381
%cmp = icmp eq <2 x i8> %i, <i8 10, i8 10>

0 commit comments

Comments
 (0)