Skip to content

Commit

Permalink
[LVI] Compute SPF range even if one operands is overdefined
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Jan 18, 2022
1 parent afb196c commit d15823e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
23 changes: 13 additions & 10 deletions llvm/lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,13 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
}
}

static ConstantRange getConstantRangeOrFull(const ValueLatticeElement &Val,
Type *Ty, const DataLayout &DL) {
if (Val.isConstantRange())
return Val.getConstantRange();
return ConstantRange::getFull(DL.getTypeSizeInBits(Ty));
}

Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueSelect(
SelectInst *SI, BasicBlock *BB) {
// Recurse on our inputs if needed
Expand All @@ -810,9 +817,11 @@ Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueSelect(
return None;
ValueLatticeElement &FalseVal = *OptFalseVal;

if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
const ConstantRange &TrueCR = TrueVal.getConstantRange();
const ConstantRange &FalseCR = FalseVal.getConstantRange();
if (TrueVal.isConstantRange() || FalseVal.isConstantRange()) {
const ConstantRange &TrueCR =
getConstantRangeOrFull(TrueVal, SI->getType(), DL);
const ConstantRange &FalseCR =
getConstantRangeOrFull(FalseVal, SI->getType(), DL);
Value *LHS = nullptr;
Value *RHS = nullptr;
SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
Expand Down Expand Up @@ -879,13 +888,7 @@ Optional<ConstantRange> LazyValueInfoImpl::getRangeFor(Value *V,
Optional<ValueLatticeElement> OptVal = getBlockValue(V, BB, CxtI);
if (!OptVal)
return None;

ValueLatticeElement &Val = *OptVal;
if (Val.isConstantRange())
return Val.getConstantRange();

const unsigned OperandBitWidth = DL.getTypeSizeInBits(V->getType());
return ConstantRange::getFull(OperandBitWidth);
return getConstantRangeOrFull(*OptVal, V->getType(), DL);
}

Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueCast(
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,7 @@ define i1 @umin_lhs_overdefined_rhs_range(i32 %a, i32 %b) {
; CHECK-NEXT: call void @llvm.assume(i1 [[ASSUME]])
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[A:%.*]], [[B]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[A]], i32 [[B]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32 [[SEL]], 42
; CHECK-NEXT: ret i1 [[CMP2]]
; CHECK-NEXT: ret i1 true
;
%assume = icmp ult i32 %b, 42
call void @llvm.assume(i1 %assume)
Expand Down

0 comments on commit d15823e

Please sign in to comment.