Skip to content

Commit d15823e

Browse files
committed
[LVI] Compute SPF range even if one operands is overdefined
If we have a constant range for one operand but not the other, we can generally still compute a useful results for SPF min/max.
1 parent afb196c commit d15823e

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,13 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
795795
}
796796
}
797797

798+
static ConstantRange getConstantRangeOrFull(const ValueLatticeElement &Val,
799+
Type *Ty, const DataLayout &DL) {
800+
if (Val.isConstantRange())
801+
return Val.getConstantRange();
802+
return ConstantRange::getFull(DL.getTypeSizeInBits(Ty));
803+
}
804+
798805
Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueSelect(
799806
SelectInst *SI, BasicBlock *BB) {
800807
// Recurse on our inputs if needed
@@ -810,9 +817,11 @@ Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueSelect(
810817
return None;
811818
ValueLatticeElement &FalseVal = *OptFalseVal;
812819

813-
if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
814-
const ConstantRange &TrueCR = TrueVal.getConstantRange();
815-
const ConstantRange &FalseCR = FalseVal.getConstantRange();
820+
if (TrueVal.isConstantRange() || FalseVal.isConstantRange()) {
821+
const ConstantRange &TrueCR =
822+
getConstantRangeOrFull(TrueVal, SI->getType(), DL);
823+
const ConstantRange &FalseCR =
824+
getConstantRangeOrFull(FalseVal, SI->getType(), DL);
816825
Value *LHS = nullptr;
817826
Value *RHS = nullptr;
818827
SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
@@ -879,13 +888,7 @@ Optional<ConstantRange> LazyValueInfoImpl::getRangeFor(Value *V,
879888
Optional<ValueLatticeElement> OptVal = getBlockValue(V, BB, CxtI);
880889
if (!OptVal)
881890
return None;
882-
883-
ValueLatticeElement &Val = *OptVal;
884-
if (Val.isConstantRange())
885-
return Val.getConstantRange();
886-
887-
const unsigned OperandBitWidth = DL.getTypeSizeInBits(V->getType());
888-
return ConstantRange::getFull(OperandBitWidth);
891+
return getConstantRangeOrFull(*OptVal, V->getType(), DL);
889892
}
890893

891894
Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueCast(

llvm/test/Transforms/CorrelatedValuePropagation/basic.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,7 @@ define i1 @umin_lhs_overdefined_rhs_range(i32 %a, i32 %b) {
649649
; CHECK-NEXT: call void @llvm.assume(i1 [[ASSUME]])
650650
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[A:%.*]], [[B]]
651651
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[A]], i32 [[B]]
652-
; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32 [[SEL]], 42
653-
; CHECK-NEXT: ret i1 [[CMP2]]
652+
; CHECK-NEXT: ret i1 true
654653
;
655654
%assume = icmp ult i32 %b, 42
656655
call void @llvm.assume(i1 %assume)

0 commit comments

Comments
 (0)