@@ -683,28 +683,30 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
683
683
684
684
Value *Y;
685
685
const APInt *Mask, *C;
686
+ if (!match (RHS, m_APInt (C)))
687
+ return ;
688
+
686
689
uint64_t ShAmt;
687
690
switch (Pred) {
688
691
case ICmpInst::ICMP_EQ:
689
692
// assume(V = C)
690
- if (match (LHS, m_V) && match (RHS, m_APInt (C)) ) {
693
+ if (match (LHS, m_V)) {
691
694
Known = Known.unionWith (KnownBits::makeConstant (*C));
692
695
// 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)))) {
695
697
// For one bits in Mask, we can propagate bits from C to V.
696
698
Known.One |= *C;
697
699
if (match (Y, m_APInt (Mask)))
698
700
Known.Zero |= ~*C & *Mask;
699
701
// 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)))) {
701
703
// For zero bits in Mask, we can propagate bits from C to V.
702
704
Known.Zero |= ~*C;
703
705
if (match (Y, m_APInt (Mask)))
704
706
Known.One |= *C & ~*Mask;
705
707
// assume(V << ShAmt = C)
706
708
} else if (match (LHS, m_Shl (m_V, m_ConstantInt (ShAmt))) &&
707
- match (RHS, m_APInt (C)) && ShAmt < BitWidth) {
709
+ ShAmt < BitWidth) {
708
710
// For those bits in C that are known, we can propagate them to known
709
711
// bits in V shifted to the right by ShAmt.
710
712
KnownBits RHSKnown = KnownBits::makeConstant (*C);
@@ -713,7 +715,7 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
713
715
Known = Known.unionWith (RHSKnown);
714
716
// assume(V >> ShAmt = C)
715
717
} else if (match (LHS, m_Shr (m_V, m_ConstantInt (ShAmt))) &&
716
- match (RHS, m_APInt (C)) && ShAmt < BitWidth) {
718
+ ShAmt < BitWidth) {
717
719
KnownBits RHSKnown = KnownBits::makeConstant (*C);
718
720
// For those bits in RHS that are known, we can propagate them to known
719
721
// bits in V shifted to the right by C.
@@ -724,38 +726,36 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
724
726
case ICmpInst::ICMP_NE: {
725
727
// assume (V & B != 0) where B is a power of 2
726
728
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))))
728
730
Known.One |= *BPow2;
729
731
break ;
730
732
}
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 ());
756
756
}
757
757
}
758
- break ;
758
+ } break ;
759
759
}
760
760
}
761
761
0 commit comments