Skip to content

Commit dfda65c

Browse files
committed
[ValueTracking] Add support for non-splat vecs in cmpExcludesZero
Just a small QOL change.
1 parent 9427fce commit dfda65c

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,24 @@ static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
567567

568568
// All other predicates - rely on generic ConstantRange handling.
569569
const APInt *C;
570-
if (!match(RHS, m_APInt(C)))
570+
auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
571+
if (match(RHS, m_APInt(C))) {
572+
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C);
573+
return !TrueValues.contains(Zero);
574+
}
575+
576+
auto *VC = dyn_cast<ConstantDataVector>(RHS);
577+
if (VC == nullptr)
571578
return false;
572579

573-
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C);
574-
return !TrueValues.contains(APInt::getZero(C->getBitWidth()));
580+
for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
581+
++ElemIdx) {
582+
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
583+
Pred, VC->getElementAsAPInt(ElemIdx));
584+
if (TrueValues.contains(Zero))
585+
return false;
586+
}
587+
return true;
575588
}
576589

577590
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {

llvm/test/Analysis/ValueTracking/known-non-zero.ll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,11 +1163,7 @@ define i1 @sdiv_known_non_zero_fail(i8 %x, i8 %y) {
11631163

11641164
define <2 x i1> @cmp_excludes_zero_with_nonsplat_vec(<2 x i8> %a, <2 x i8> %b) {
11651165
; CHECK-LABEL: @cmp_excludes_zero_with_nonsplat_vec(
1166-
; CHECK-NEXT: [[C:%.*]] = icmp sge <2 x i8> [[A:%.*]], <i8 1, i8 4>
1167-
; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[C]], <2 x i8> [[A]], <2 x i8> <i8 4, i8 5>
1168-
; CHECK-NEXT: [[AND:%.*]] = or <2 x i8> [[S]], [[B:%.*]]
1169-
; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[AND]], zeroinitializer
1170-
; CHECK-NEXT: ret <2 x i1> [[R]]
1166+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
11711167
;
11721168
%c = icmp sge <2 x i8> %a, <i8 1, i8 4>
11731169
%s = select <2 x i1> %c, <2 x i8> %a, <2 x i8> <i8 4, i8 5>

0 commit comments

Comments
 (0)