Skip to content

Commit bf56cd2

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

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
@@ -569,11 +569,24 @@ static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
569569

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

575-
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C);
576-
return !TrueValues.contains(APInt::getZero(C->getBitWidth()));
582+
for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
583+
++ElemIdx) {
584+
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
585+
Pred, VC->getElementAsAPInt(ElemIdx));
586+
if (TrueValues.contains(Zero))
587+
return false;
588+
}
589+
return true;
577590
}
578591

579592
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)