Skip to content

Commit

Permalink
[ValueTracking] Add support for non-splat vecs in cmpExcludesZero
Browse files Browse the repository at this point in the history
Just a small QOL change.
  • Loading branch information
goldsteinn committed Oct 12, 2023
1 parent 9427fce commit dfda65c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 16 additions & 3 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,24 @@ static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {

// All other predicates - rely on generic ConstantRange handling.
const APInt *C;
if (!match(RHS, m_APInt(C)))
auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
if (match(RHS, m_APInt(C))) {
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C);
return !TrueValues.contains(Zero);
}

auto *VC = dyn_cast<ConstantDataVector>(RHS);
if (VC == nullptr)
return false;

ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C);
return !TrueValues.contains(APInt::getZero(C->getBitWidth()));
for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
++ElemIdx) {
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
Pred, VC->getElementAsAPInt(ElemIdx));
if (TrueValues.contains(Zero))
return false;
}
return true;
}

static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
Expand Down
6 changes: 1 addition & 5 deletions llvm/test/Analysis/ValueTracking/known-non-zero.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1163,11 +1163,7 @@ define i1 @sdiv_known_non_zero_fail(i8 %x, i8 %y) {

define <2 x i1> @cmp_excludes_zero_with_nonsplat_vec(<2 x i8> %a, <2 x i8> %b) {
; CHECK-LABEL: @cmp_excludes_zero_with_nonsplat_vec(
; CHECK-NEXT: [[C:%.*]] = icmp sge <2 x i8> [[A:%.*]], <i8 1, i8 4>
; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[C]], <2 x i8> [[A]], <2 x i8> <i8 4, i8 5>
; CHECK-NEXT: [[AND:%.*]] = or <2 x i8> [[S]], [[B:%.*]]
; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[AND]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[R]]
; CHECK-NEXT: ret <2 x i1> zeroinitializer
;
%c = icmp sge <2 x i8> %a, <i8 1, i8 4>
%s = select <2 x i1> %c, <2 x i8> %a, <2 x i8> <i8 4, i8 5>
Expand Down

0 comments on commit dfda65c

Please sign in to comment.