Skip to content

Commit 8bba8a5

Browse files
authored
[NFC][ValueTracking] Hoist the matching of RHS constant (#125818)
1 parent 4fdd28b commit 8bba8a5

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -683,28 +683,30 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
683683

684684
Value *Y;
685685
const APInt *Mask, *C;
686+
if (!match(RHS, m_APInt(C)))
687+
return;
688+
686689
uint64_t ShAmt;
687690
switch (Pred) {
688691
case ICmpInst::ICMP_EQ:
689692
// assume(V = C)
690-
if (match(LHS, m_V) && match(RHS, m_APInt(C))) {
693+
if (match(LHS, m_V)) {
691694
Known = Known.unionWith(KnownBits::makeConstant(*C));
692695
// assume(V & Mask = C)
693-
} else if (match(LHS, m_c_And(m_V, m_Value(Y))) &&
694-
match(RHS, m_APInt(C))) {
696+
} else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
695697
// For one bits in Mask, we can propagate bits from C to V.
696698
Known.One |= *C;
697699
if (match(Y, m_APInt(Mask)))
698700
Known.Zero |= ~*C & *Mask;
699701
// assume(V | Mask = C)
700-
} else if (match(LHS, m_c_Or(m_V, m_Value(Y))) && match(RHS, m_APInt(C))) {
702+
} else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
701703
// For zero bits in Mask, we can propagate bits from C to V.
702704
Known.Zero |= ~*C;
703705
if (match(Y, m_APInt(Mask)))
704706
Known.One |= *C & ~*Mask;
705707
// assume(V << ShAmt = C)
706708
} else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
707-
match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
709+
ShAmt < BitWidth) {
708710
// For those bits in C that are known, we can propagate them to known
709711
// bits in V shifted to the right by ShAmt.
710712
KnownBits RHSKnown = KnownBits::makeConstant(*C);
@@ -713,7 +715,7 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
713715
Known = Known.unionWith(RHSKnown);
714716
// assume(V >> ShAmt = C)
715717
} else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
716-
match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
718+
ShAmt < BitWidth) {
717719
KnownBits RHSKnown = KnownBits::makeConstant(*C);
718720
// For those bits in RHS that are known, we can propagate them to known
719721
// bits in V shifted to the right by C.
@@ -724,38 +726,36 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
724726
case ICmpInst::ICMP_NE: {
725727
// assume (V & B != 0) where B is a power of 2
726728
const APInt *BPow2;
727-
if (match(LHS, m_And(m_V, m_Power2(BPow2))) && match(RHS, m_Zero()))
729+
if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
728730
Known.One |= *BPow2;
729731
break;
730732
}
731-
default:
732-
if (match(RHS, m_APInt(C))) {
733-
const APInt *Offset = nullptr;
734-
if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
735-
ConstantRange LHSRange = ConstantRange::makeAllowedICmpRegion(Pred, *C);
736-
if (Offset)
737-
LHSRange = LHSRange.sub(*Offset);
738-
Known = Known.unionWith(LHSRange.toKnownBits());
739-
}
740-
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
741-
// X & Y u> C -> X u> C && Y u> C
742-
// X nuw- Y u> C -> X u> C
743-
if (match(LHS, m_c_And(m_V, m_Value())) ||
744-
match(LHS, m_NUWSub(m_V, m_Value())))
745-
Known.One.setHighBits(
746-
(*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
747-
}
748-
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
749-
// X | Y u< C -> X u< C && Y u< C
750-
// X nuw+ Y u< C -> X u< C && Y u< C
751-
if (match(LHS, m_c_Or(m_V, m_Value())) ||
752-
match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
753-
Known.Zero.setHighBits(
754-
(*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
755-
}
733+
default: {
734+
const APInt *Offset = nullptr;
735+
if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
736+
ConstantRange LHSRange = ConstantRange::makeAllowedICmpRegion(Pred, *C);
737+
if (Offset)
738+
LHSRange = LHSRange.sub(*Offset);
739+
Known = Known.unionWith(LHSRange.toKnownBits());
740+
}
741+
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
742+
// X & Y u> C -> X u> C && Y u> C
743+
// X nuw- Y u> C -> X u> C
744+
if (match(LHS, m_c_And(m_V, m_Value())) ||
745+
match(LHS, m_NUWSub(m_V, m_Value())))
746+
Known.One.setHighBits(
747+
(*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
748+
}
749+
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
750+
// X | Y u< C -> X u< C && Y u< C
751+
// X nuw+ Y u< C -> X u< C && Y u< C
752+
if (match(LHS, m_c_Or(m_V, m_Value())) ||
753+
match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
754+
Known.Zero.setHighBits(
755+
(*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
756756
}
757757
}
758-
break;
758+
} break;
759759
}
760760
}
761761

0 commit comments

Comments
 (0)