Skip to content

Commit 26ffc92

Browse files
committed
[LVI] Handle range attributes
1 parent 875963b commit 26ffc92

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
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: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,7 @@ else:
11251125

11261126
define i1 @icmp_eq_range_attr(i8 range(i8 1, 0) %i) {
11271127
; CHECK-LABEL: @icmp_eq_range_attr(
1128-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I:%.*]], 0
1129-
; CHECK-NEXT: ret i1 [[CMP]]
1128+
; CHECK-NEXT: ret i1 false
11301129
;
11311130
%cmp = icmp eq i8 %i, 0
11321131
ret i1 %cmp
@@ -1147,8 +1146,7 @@ declare range(i8 -1, 1) i8 @returns_contain_zero_range_helper()
11471146
define i1 @icmp_eq_range_return() {
11481147
; CHECK-LABEL: @icmp_eq_range_return(
11491148
; CHECK-NEXT: [[I:%.*]] = call i8 @returns_non_zero_range_helper()
1150-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I]], 0
1151-
; CHECK-NEXT: ret i1 [[CMP]]
1149+
; CHECK-NEXT: ret i1 false
11521150
;
11531151
%i = call i8 @returns_non_zero_range_helper()
11541152
%cmp = icmp eq i8 %i, 0
@@ -1171,8 +1169,7 @@ declare i8 @returns_i8_helper()
11711169
define i1 @icmp_eq_range_call() {
11721170
; CHECK-LABEL: @icmp_eq_range_call(
11731171
; CHECK-NEXT: [[I:%.*]] = call range(i8 1, 0) i8 @returns_i8_helper()
1174-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[I]], 0
1175-
; CHECK-NEXT: ret i1 [[CMP]]
1172+
; CHECK-NEXT: ret i1 false
11761173
;
11771174
%i = call range(i8 1, 0) i8 @returns_i8_helper()
11781175
%cmp = icmp eq i8 %i, 0

0 commit comments

Comments
 (0)