Skip to content

Commit c644486

Browse files
committed
[ValueTracking] Pass unpacked arguments to computeKnownBitsFromCmp() (NFC)
This will alllow using it with an inverted predicate in the future.
1 parent 1294407 commit c644486

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -630,14 +630,14 @@ static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
630630
return false;
631631
}
632632

633-
static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp,
634-
KnownBits &Known, unsigned Depth,
635-
const SimplifyQuery &Q) {
636-
if (Cmp->getOperand(1)->getType()->isPointerTy()) {
633+
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
634+
Value *LHS, Value *RHS, KnownBits &Known,
635+
unsigned Depth, const SimplifyQuery &Q) {
636+
if (RHS->getType()->isPointerTy()) {
637637
// Handle comparison of pointer to null explicitly, as it will not be
638638
// covered by the m_APInt() logic below.
639-
if (match(Cmp->getOperand(1), m_Zero())) {
640-
switch (Cmp->getPredicate()) {
639+
if (match(RHS, m_Zero())) {
640+
switch (Pred) {
641641
case ICmpInst::ICMP_EQ:
642642
Known.setAllZero();
643643
break;
@@ -659,44 +659,41 @@ static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp,
659659
auto m_V =
660660
m_CombineOr(m_Specific(V), m_PtrToIntSameSize(Q.DL, m_Specific(V)));
661661

662-
CmpInst::Predicate Pred;
663662
const APInt *Mask, *C;
664663
uint64_t ShAmt;
665-
switch (Cmp->getPredicate()) {
664+
switch (Pred) {
666665
case ICmpInst::ICMP_EQ:
667666
// assume(V = C)
668-
if (match(Cmp, m_ICmp(Pred, m_V, m_APInt(C)))) {
667+
if (match(LHS, m_V) && match(RHS, m_APInt(C))) {
669668
Known = Known.unionWith(KnownBits::makeConstant(*C));
670669
// assume(V & Mask = C)
671-
} else if (match(Cmp,
672-
m_ICmp(Pred, m_And(m_V, m_APInt(Mask)), m_APInt(C)))) {
670+
} else if (match(LHS, m_And(m_V, m_APInt(Mask))) &&
671+
match(RHS, m_APInt(C))) {
673672
// For one bits in Mask, we can propagate bits from C to V.
674673
Known.Zero |= ~*C & *Mask;
675674
Known.One |= *C & *Mask;
676675
// assume(V | Mask = C)
677-
} else if (match(Cmp, m_ICmp(Pred, m_Or(m_V, m_APInt(Mask)), m_APInt(C)))) {
676+
} else if (match(LHS, m_Or(m_V, m_APInt(Mask))) && match(RHS, m_APInt(C))) {
678677
// For zero bits in Mask, we can propagate bits from C to V.
679678
Known.Zero |= ~*C & ~*Mask;
680679
Known.One |= *C & ~*Mask;
681680
// assume(V ^ Mask = C)
682-
} else if (match(Cmp,
683-
m_ICmp(Pred, m_Xor(m_V, m_APInt(Mask)), m_APInt(C)))) {
681+
} else if (match(LHS, m_Xor(m_V, m_APInt(Mask))) &&
682+
match(RHS, m_APInt(C))) {
684683
// Equivalent to assume(V == Mask ^ C)
685684
Known = Known.unionWith(KnownBits::makeConstant(*C ^ *Mask));
686685
// assume(V << ShAmt = C)
687-
} else if (match(Cmp, m_ICmp(Pred, m_Shl(m_V, m_ConstantInt(ShAmt)),
688-
m_APInt(C))) &&
689-
ShAmt < BitWidth) {
686+
} else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
687+
match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
690688
// For those bits in C that are known, we can propagate them to known
691689
// bits in V shifted to the right by ShAmt.
692690
KnownBits RHSKnown = KnownBits::makeConstant(*C);
693691
RHSKnown.Zero.lshrInPlace(ShAmt);
694692
RHSKnown.One.lshrInPlace(ShAmt);
695693
Known = Known.unionWith(RHSKnown);
696694
// assume(V >> ShAmt = C)
697-
} else if (match(Cmp, m_ICmp(Pred, m_Shr(m_V, m_ConstantInt(ShAmt)),
698-
m_APInt(C))) &&
699-
ShAmt < BitWidth) {
695+
} else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
696+
match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
700697
KnownBits RHSKnown = KnownBits::makeConstant(*C);
701698
// For those bits in RHS that are known, we can propagate them to known
702699
// bits in V shifted to the right by C.
@@ -707,14 +704,14 @@ static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp,
707704
case ICmpInst::ICMP_NE: {
708705
// assume (V & B != 0) where B is a power of 2
709706
const APInt *BPow2;
710-
if (match(Cmp, m_ICmp(Pred, m_And(m_V, m_Power2(BPow2)), m_Zero())))
707+
if (match(LHS, m_And(m_V, m_Power2(BPow2))) && match(RHS, m_Zero()))
711708
Known.One |= *BPow2;
712709
break;
713710
}
714711
default:
715712
const APInt *Offset = nullptr;
716-
if (match(Cmp, m_ICmp(Pred, m_CombineOr(m_V, m_Add(m_V, m_APInt(Offset))),
717-
m_APInt(C)))) {
713+
if (match(LHS, m_CombineOr(m_V, m_Add(m_V, m_APInt(Offset)))) &&
714+
match(RHS, m_APInt(C))) {
718715
ConstantRange LHSRange = ConstantRange::makeAllowedICmpRegion(Pred, *C);
719716
if (Offset)
720717
LHSRange = LHSRange.sub(*Offset);
@@ -788,7 +785,8 @@ void llvm::computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
788785
if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
789786
continue;
790787

791-
computeKnownBitsFromCmp(V, Cmp, Known, Depth, Q);
788+
computeKnownBitsFromCmp(V, Cmp->getPredicate(), Cmp->getOperand(0),
789+
Cmp->getOperand(1), Known, Depth, Q);
792790
}
793791

794792
// Conflicting assumption: Undefined behavior will occur on this execution

0 commit comments

Comments
 (0)