Skip to content

Commit d4cd65e

Browse files
authored
[LVI] Handle range attributes (#86413)
This adds handling of range attribute for return values of Call and Invoke in getFromRangeMetadata and handling of argument with range attribute in solveBlockValueNonLocal. There is one additional check of the range metadata at line 1120 in getValueFromSimpleICmpCondition that is not covered in this PR as after #75311 there is no test that cover that check any more and I have not been able to create a test that trigger that code.
1 parent 4f19f15 commit d4cd65e

File tree

2 files changed

+84
-15
lines changed

2 files changed

+84
-15
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,14 @@ LazyValueInfoImpl::getBlockValue(Value *Val, BasicBlock *BB,
588588

589589
static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) {
590590
switch (BBI->getOpcode()) {
591-
default: break;
592-
case Instruction::Load:
591+
default:
592+
break;
593593
case Instruction::Call:
594594
case Instruction::Invoke:
595+
if (std::optional<ConstantRange> Range = cast<CallBase>(BBI)->getRange())
596+
return ValueLatticeElement::getRange(*Range);
597+
[[fallthrough]];
598+
case Instruction::Load:
595599
if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
596600
if (isa<IntegerType>(BBI->getType())) {
597601
return ValueLatticeElement::getRange(
@@ -706,10 +710,11 @@ std::optional<ValueLatticeElement>
706710
LazyValueInfoImpl::solveBlockValueNonLocal(Value *Val, BasicBlock *BB) {
707711
ValueLatticeElement Result; // Start Undefined.
708712

709-
// If this is the entry block, we must be asking about an argument. The
710-
// value is overdefined.
713+
// If this is the entry block, we must be asking about an argument.
711714
if (BB->isEntryBlock()) {
712715
assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
716+
if (std::optional<ConstantRange> Range = cast<Argument>(Val)->getRange())
717+
return ValueLatticeElement::getRange(*Range);
713718
return ValueLatticeElement::getOverdefined();
714719
}
715720

llvm/test/Transforms/CorrelatedValuePropagation/range.ll

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ if.end8:
102102
define i32 @test4(i32 %c) nounwind {
103103
; CHECK-LABEL: @test4(
104104
; CHECK-NEXT: switch i32 [[C:%.*]], label [[SW_DEFAULT:%.*]] [
105-
; CHECK-NEXT: i32 1, label [[SW_BB:%.*]]
106-
; CHECK-NEXT: i32 2, label [[SW_BB]]
107-
; CHECK-NEXT: i32 4, label [[SW_BB]]
105+
; CHECK-NEXT: i32 1, label [[SW_BB:%.*]]
106+
; CHECK-NEXT: i32 2, label [[SW_BB]]
107+
; CHECK-NEXT: i32 4, label [[SW_BB]]
108108
; CHECK-NEXT: ]
109109
; CHECK: sw.bb:
110110
; CHECK-NEXT: br i1 true, label [[IF_THEN:%.*]], label [[IF_END:%.*]]
@@ -207,8 +207,8 @@ define i1 @test7(i32 %c) nounwind {
207207
; CHECK-LABEL: @test7(
208208
; CHECK-NEXT: entry:
209209
; CHECK-NEXT: switch i32 [[C:%.*]], label [[SW_DEFAULT:%.*]] [
210-
; CHECK-NEXT: i32 6, label [[SW_BB:%.*]]
211-
; CHECK-NEXT: i32 7, label [[SW_BB]]
210+
; CHECK-NEXT: i32 6, label [[SW_BB:%.*]]
211+
; CHECK-NEXT: i32 7, label [[SW_BB]]
212212
; CHECK-NEXT: ]
213213
; CHECK: sw.bb:
214214
; CHECK-NEXT: ret i1 true
@@ -790,8 +790,8 @@ define i32 @test18(i8 %a) {
790790
; CHECK-NEXT: br label [[DISPATCH:%.*]]
791791
; CHECK: dispatch:
792792
; CHECK-NEXT: switch i8 [[A]], label [[DISPATCH]] [
793-
; CHECK-NEXT: i8 93, label [[TARGET93:%.*]]
794-
; CHECK-NEXT: i8 -111, label [[DISPATCH]]
793+
; CHECK-NEXT: i8 93, label [[TARGET93:%.*]]
794+
; CHECK-NEXT: i8 -111, label [[DISPATCH]]
795795
; CHECK-NEXT: ]
796796
; CHECK: target93:
797797
; CHECK-NEXT: ret i32 93
@@ -817,8 +817,8 @@ define i8 @test19(i8 %a) {
817817
; CHECK-NEXT: br label [[DISPATCH:%.*]]
818818
; CHECK: dispatch:
819819
; CHECK-NEXT: switch i8 [[A]], label [[DISPATCH]] [
820-
; CHECK-NEXT: i8 93, label [[TARGET93:%.*]]
821-
; CHECK-NEXT: i8 -111, label [[DISPATCH]]
820+
; CHECK-NEXT: i8 93, label [[TARGET93:%.*]]
821+
; CHECK-NEXT: i8 -111, label [[DISPATCH]]
822822
; CHECK-NEXT: ]
823823
; CHECK: target93:
824824
; CHECK-NEXT: ret i8 96
@@ -846,8 +846,8 @@ define i1 @test20(i64 %a) {
846846
; CHECK-NEXT: br label [[DISPATCH:%.*]]
847847
; CHECK: dispatch:
848848
; CHECK-NEXT: switch i64 [[A]], label [[DEFAULT:%.*]] [
849-
; CHECK-NEXT: i64 0, label [[EXIT2:%.*]]
850-
; CHECK-NEXT: i64 -2147483647, label [[EXIT2]]
849+
; CHECK-NEXT: i64 0, label [[EXIT2:%.*]]
850+
; CHECK-NEXT: i64 -2147483647, label [[EXIT2]]
851851
; CHECK-NEXT: ]
852852
; CHECK: default:
853853
; CHECK-NEXT: [[C:%.*]] = icmp eq i64 [[B]], 0
@@ -1123,6 +1123,70 @@ else:
11231123
ret i1 true
11241124
}
11251125

1126+
define i1 @icmp_eq_range_attr(i8 range(i8 1, 0) %i) {
1127+
; CHECK-LABEL: @icmp_eq_range_attr(
1128+
; CHECK-NEXT: ret i1 false
1129+
;
1130+
%cmp = icmp eq i8 %i, 0
1131+
ret i1 %cmp
1132+
}
1133+
1134+
define i1 @neg_icmp_eq_range_attr(i8 range(i8 -1, 1) %i) {
1135+
; CHECK-LABEL: @neg_icmp_eq_range_attr(
1136+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I:%.*]], 0
1137+
; CHECK-NEXT: ret i1 [[CMP]]
1138+
;
1139+
%cmp = icmp eq i8 %i, 0
1140+
ret i1 %cmp
1141+
}
1142+
1143+
declare range(i8 1, 0) i8 @returns_non_zero_range_helper()
1144+
declare range(i8 -1, 1) i8 @returns_contain_zero_range_helper()
1145+
1146+
define i1 @icmp_eq_range_return() {
1147+
; CHECK-LABEL: @icmp_eq_range_return(
1148+
; CHECK-NEXT: [[I:%.*]] = call i8 @returns_non_zero_range_helper()
1149+
; CHECK-NEXT: ret i1 false
1150+
;
1151+
%i = call i8 @returns_non_zero_range_helper()
1152+
%cmp = icmp eq i8 %i, 0
1153+
ret i1 %cmp
1154+
}
1155+
1156+
define i1 @neg_icmp_eq_range_return() {
1157+
; CHECK-LABEL: @neg_icmp_eq_range_return(
1158+
; CHECK-NEXT: [[I:%.*]] = call i8 @returns_contain_zero_range_helper()
1159+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I]], 0
1160+
; CHECK-NEXT: ret i1 [[CMP]]
1161+
;
1162+
%i = call i8 @returns_contain_zero_range_helper()
1163+
%cmp = icmp eq i8 %i, 0
1164+
ret i1 %cmp
1165+
}
1166+
1167+
declare i8 @returns_i8_helper()
1168+
1169+
define i1 @icmp_eq_range_call() {
1170+
; CHECK-LABEL: @icmp_eq_range_call(
1171+
; CHECK-NEXT: [[I:%.*]] = call range(i8 1, 0) i8 @returns_i8_helper()
1172+
; CHECK-NEXT: ret i1 false
1173+
;
1174+
%i = call range(i8 1, 0) i8 @returns_i8_helper()
1175+
%cmp = icmp eq i8 %i, 0
1176+
ret i1 %cmp
1177+
}
1178+
1179+
define i1 @neg_icmp_eq_range_call() {
1180+
; CHECK-LABEL: @neg_icmp_eq_range_call(
1181+
; CHECK-NEXT: [[I:%.*]] = call range(i8 0, 11) i8 @returns_i8_helper()
1182+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I]], 0
1183+
; CHECK-NEXT: ret i1 [[CMP]]
1184+
;
1185+
%i = call range(i8 0, 11) i8 @returns_i8_helper()
1186+
%cmp = icmp eq i8 %i, 0
1187+
ret i1 %cmp
1188+
}
1189+
11261190
declare i16 @llvm.ctlz.i16(i16, i1)
11271191
declare i16 @llvm.cttz.i16(i16, i1)
11281192
declare i16 @llvm.ctpop.i16(i16)

0 commit comments

Comments
 (0)