Skip to content

Commit 7d0668a

Browse files
committed
handle range attribute in computeConstantRange
1 parent 14b871f commit 7d0668a

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
@@ -9143,12 +9143,19 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
91439143
// TODO: Return ConstantRange.
91449144
setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
91459145
CR = ConstantRange::getNonEmpty(Lower, Upper);
9146-
}
9146+
} else if (const auto *A = dyn_cast<Argument>(V))
9147+
if (std::optional<ConstantRange> Range = A->getRange())
9148+
CR = *Range;
91479149

9148-
if (auto *I = dyn_cast<Instruction>(V))
9150+
if (auto *I = dyn_cast<Instruction>(V)) {
91499151
if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
91509152
CR = CR.intersectWith(getConstantRangeFromMetadata(*Range));
91519153

9154+
if (const auto *CB = dyn_cast<CallBase>(V))
9155+
if (std::optional<ConstantRange> Range = CB->getRange())
9156+
CR = CR.intersectWith(*Range);
9157+
}
9158+
91529159
if (CtxI && AC) {
91539160
// Try to restrict the range based on information from assumptions.
91549161
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
@@ -1143,8 +1143,7 @@ define <2 x i1> @heterogeneous_constvector(<2 x i8> %x) {
11431143

11441144
define i1 @icmp_eq_constant_range_attr(i8 range(i8 0, 10) %i) {
11451145
; CHECK-LABEL: @icmp_eq_constant_range_attr(
1146-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I:%.*]], 10
1147-
; CHECK-NEXT: ret i1 [[CMP]]
1146+
; CHECK-NEXT: ret i1 false
11481147
;
11491148
%cmp = icmp eq i8 %i, 10
11501149
ret i1 %cmp
@@ -1176,8 +1175,7 @@ define i1 @icmp_eq_constant_range_return() {
11761175
define i1 @neg_icmp_eq_constant_range_return() {
11771176
; CHECK-LABEL: @neg_icmp_eq_constant_range_return(
11781177
; CHECK-NEXT: [[I:%.*]] = call i8 @returns_contain_ten_range_helper()
1179-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I]], 10
1180-
; CHECK-NEXT: ret i1 [[CMP]]
1178+
; CHECK-NEXT: ret i1 false
11811179
;
11821180
%i = call i8 @returns_contain_ten_range_helper()
11831181
%cmp = icmp eq i8 %i, 10
@@ -1189,8 +1187,7 @@ declare i8 @returns_i8_helper()
11891187
define i1 @icmp_eq_constant_range_call() {
11901188
; CHECK-LABEL: @icmp_eq_constant_range_call(
11911189
; CHECK-NEXT: [[I:%.*]] = call range(i8 0, 10) i8 @returns_i8_helper()
1192-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I]], 10
1193-
; CHECK-NEXT: ret i1 [[CMP]]
1190+
; CHECK-NEXT: ret i1 false
11941191
;
11951192
%i = call range(i8 0, 10) i8 @returns_i8_helper()
11961193
%cmp = icmp eq i8 %i, 10
@@ -1210,8 +1207,7 @@ define i1 @neg_icmp_eq_constant_range_call() {
12101207

12111208
define <2 x i1> @icmp_eq_constant_range_attr_vec(<2 x i8> range(i8 0, 10) %i) {
12121209
; CHECK-LABEL: @icmp_eq_constant_range_attr_vec(
1213-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[I:%.*]], <i8 10, i8 10>
1214-
; CHECK-NEXT: ret <2 x i1> [[CMP]]
1210+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
12151211
;
12161212
%cmp = icmp eq <2 x i8> %i, <i8 10, i8 10>
12171213
ret <2 x i1> %cmp
@@ -1232,8 +1228,7 @@ declare range(i8 0, 11) <2 x i8> @returns_contain_ten_range_helper_vec()
12321228
define <2 x i1> @icmp_eq_constant_range_return_vec() {
12331229
; CHECK-LABEL: @icmp_eq_constant_range_return_vec(
12341230
; CHECK-NEXT: [[I:%.*]] = call <2 x i8> @returns_non_ten_range_helper_vec()
1235-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[I]], <i8 10, i8 10>
1236-
; CHECK-NEXT: ret <2 x i1> [[CMP]]
1231+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
12371232
;
12381233
%i = call <2 x i8> @returns_non_ten_range_helper_vec()
12391234
%cmp = icmp eq <2 x i8> %i, <i8 10, i8 10>
@@ -1256,8 +1251,7 @@ declare <2 x i8> @returns_i8_helper_vec()
12561251
define <2 x i1> @icmp_eq_constant_range_call_vec() {
12571252
; CHECK-LABEL: @icmp_eq_constant_range_call_vec(
12581253
; CHECK-NEXT: [[I:%.*]] = call range(i8 0, 10) <2 x i8> @returns_i8_helper_vec()
1259-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[I]], <i8 10, i8 10>
1260-
; CHECK-NEXT: ret <2 x i1> [[CMP]]
1254+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
12611255
;
12621256
%i = call range(i8 0, 10) <2 x i8> @returns_i8_helper_vec()
12631257
%cmp = icmp eq <2 x i8> %i, <i8 10, i8 10>

0 commit comments

Comments
 (0)