@@ -633,101 +633,89 @@ static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
633
633
static void computeKnownBitsFromCmp (const Value *V, const ICmpInst *Cmp,
634
634
KnownBits &Known, unsigned Depth,
635
635
const SimplifyQuery &Q) {
636
+ if (Cmp->getOperand (1 )->getType ()->isPointerTy ()) {
637
+ // Handle comparison of pointer to null explicitly, as it will not be
638
+ // covered by the m_APInt() logic below.
639
+ if (match (Cmp->getOperand (1 ), m_Zero ())) {
640
+ switch (Cmp->getPredicate ()) {
641
+ case ICmpInst::ICMP_EQ:
642
+ Known.setAllZero ();
643
+ break ;
644
+ case ICmpInst::ICMP_SGE:
645
+ case ICmpInst::ICMP_SGT:
646
+ Known.makeNonNegative ();
647
+ break ;
648
+ case ICmpInst::ICMP_SLT:
649
+ Known.makeNegative ();
650
+ break ;
651
+ default :
652
+ break ;
653
+ }
654
+ }
655
+ return ;
656
+ }
657
+
636
658
unsigned BitWidth = Known.getBitWidth ();
637
- // We are attempting to compute known bits for the operands of an assume.
638
- // Do not try to use other assumptions for those recursive calls because
639
- // that can lead to mutual recursion and a compile-time explosion.
640
- // An example of the mutual recursion: computeKnownBits can call
641
- // isKnownNonZero which calls computeKnownBitsFromAssume (this function)
642
- // and so on.
643
- SimplifyQuery QueryNoAC = Q;
644
- QueryNoAC.AC = nullptr ;
645
-
646
- // Note that ptrtoint may change the bitwidth.
647
- Value *A, *B;
648
659
auto m_V =
649
660
m_CombineOr (m_Specific (V), m_PtrToIntSameSize (Q.DL , m_Specific (V)));
650
661
651
662
CmpInst::Predicate Pred;
652
- uint64_t C;
663
+ const APInt *Mask, *C;
664
+ uint64_t ShAmt;
653
665
switch (Cmp->getPredicate ()) {
654
666
case ICmpInst::ICMP_EQ:
655
- // assume(v = a)
656
- if (match (Cmp, m_c_ICmp (Pred, m_V, m_Value (A)))) {
657
- KnownBits RHSKnown = computeKnownBits (A, Depth + 1 , QueryNoAC);
658
- Known = Known.unionWith (RHSKnown);
659
- // assume(v & b = a)
660
- } else if (match (Cmp,
661
- m_c_ICmp (Pred, m_c_And (m_V, m_Value (B)), m_Value (A)))) {
662
- KnownBits RHSKnown = computeKnownBits (A, Depth + 1 , QueryNoAC);
663
- KnownBits MaskKnown = computeKnownBits (B, Depth + 1 , QueryNoAC);
664
-
665
- // For those bits in the mask that are known to be one, we can propagate
666
- // known bits from the RHS to V.
667
- Known.Zero |= RHSKnown.Zero & MaskKnown.One ;
668
- Known.One |= RHSKnown.One & MaskKnown.One ;
669
- // assume(v | b = a)
667
+ // assume(V = C)
668
+ if (match (Cmp, m_ICmp (Pred, m_V, m_APInt (C)))) {
669
+ Known = Known.unionWith (KnownBits::makeConstant (*C));
670
+ // assume(V & Mask = C)
670
671
} else if (match (Cmp,
671
- m_c_ICmp (Pred, m_c_Or (m_V, m_Value (B)), m_Value (A)))) {
672
- KnownBits RHSKnown = computeKnownBits (A, Depth + 1 , QueryNoAC);
673
- KnownBits BKnown = computeKnownBits (B, Depth + 1 , QueryNoAC);
674
-
675
- // For those bits in B that are known to be zero, we can propagate known
676
- // bits from the RHS to V.
677
- Known.Zero |= RHSKnown.Zero & BKnown.Zero ;
678
- Known.One |= RHSKnown.One & BKnown.Zero ;
679
- // assume(v ^ b = a)
672
+ m_ICmp (Pred, m_And (m_V, m_APInt (Mask)), m_APInt (C)))) {
673
+ // For one bits in Mask, we can propagate bits from C to V.
674
+ Known.Zero |= ~*C & *Mask;
675
+ Known.One |= *C & *Mask;
676
+ // assume(V | Mask = C)
677
+ } else if (match (Cmp, m_ICmp (Pred, m_Or (m_V, m_APInt (Mask)), m_APInt (C)))) {
678
+ // For zero bits in Mask, we can propagate bits from C to V.
679
+ Known.Zero |= ~*C & ~*Mask;
680
+ Known.One |= *C & ~*Mask;
681
+ // assume(V ^ Mask = C)
680
682
} else if (match (Cmp,
681
- m_c_ICmp (Pred, m_c_Xor (m_V, m_Value (B)), m_Value (A)))) {
682
- KnownBits RHSKnown = computeKnownBits (A, Depth + 1 , QueryNoAC);
683
- KnownBits BKnown = computeKnownBits (B, Depth + 1 , QueryNoAC);
684
-
685
- // For those bits in B that are known to be zero, we can propagate known
686
- // bits from the RHS to V. For those bits in B that are known to be one,
687
- // we can propagate inverted known bits from the RHS to V.
688
- Known.Zero |= RHSKnown.Zero & BKnown.Zero ;
689
- Known.One |= RHSKnown.One & BKnown.Zero ;
690
- Known.Zero |= RHSKnown.One & BKnown.One ;
691
- Known.One |= RHSKnown.Zero & BKnown.One ;
692
- // assume(v << c = a)
693
- } else if (match (Cmp, m_c_ICmp (Pred, m_Shl (m_V, m_ConstantInt (C)),
694
- m_Value (A))) &&
695
- C < BitWidth) {
696
- KnownBits RHSKnown = computeKnownBits (A, Depth + 1 , QueryNoAC);
697
-
698
- // For those bits in RHS that are known, we can propagate them to known
699
- // bits in V shifted to the right by C.
700
- RHSKnown.Zero .lshrInPlace (C);
701
- RHSKnown.One .lshrInPlace (C);
683
+ m_ICmp (Pred, m_Xor (m_V, m_APInt (Mask)), m_APInt (C)))) {
684
+ // Equivalent to assume(V == Mask ^ C)
685
+ Known = Known.unionWith (KnownBits::makeConstant (*C ^ *Mask));
686
+ // 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) {
690
+ // For those bits in C that are known, we can propagate them to known
691
+ // bits in V shifted to the right by ShAmt.
692
+ KnownBits RHSKnown = KnownBits::makeConstant (*C);
693
+ RHSKnown.Zero .lshrInPlace (ShAmt);
694
+ RHSKnown.One .lshrInPlace (ShAmt);
702
695
Known = Known.unionWith (RHSKnown);
703
- // assume(v >> c = a )
704
- } else if (match (Cmp, m_c_ICmp (Pred, m_Shr (m_V, m_ConstantInt (C )),
705
- m_Value (A ))) &&
706
- C < BitWidth) {
707
- KnownBits RHSKnown = computeKnownBits (A, Depth + 1 , QueryNoAC );
696
+ // 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) {
700
+ KnownBits RHSKnown = KnownBits::makeConstant (*C );
708
701
// For those bits in RHS that are known, we can propagate them to known
709
702
// bits in V shifted to the right by C.
710
- Known.Zero |= RHSKnown.Zero << C ;
711
- Known.One |= RHSKnown.One << C ;
703
+ Known.Zero |= RHSKnown.Zero << ShAmt ;
704
+ Known.One |= RHSKnown.One << ShAmt ;
712
705
}
713
706
break ;
714
707
case ICmpInst::ICMP_NE: {
715
- // assume (v & b != 0) where b is a power of 2
708
+ // assume (V & B != 0) where B is a power of 2
716
709
const APInt *BPow2;
717
- if (match (Cmp, m_ICmp (Pred, m_c_And (m_V, m_Power2 (BPow2)), m_Zero ()))) {
710
+ if (match (Cmp, m_ICmp (Pred, m_And (m_V, m_Power2 (BPow2)), m_Zero ())))
718
711
Known.One |= *BPow2;
719
- }
720
712
break ;
721
713
}
722
714
default :
723
715
const APInt *Offset = nullptr ;
724
716
if (match (Cmp, m_ICmp (Pred, m_CombineOr (m_V, m_Add (m_V, m_APInt (Offset))),
725
- m_Value (A)))) {
726
- KnownBits RHSKnown = computeKnownBits (A, Depth + 1 , QueryNoAC);
727
- ConstantRange RHSRange =
728
- ConstantRange::fromKnownBits (RHSKnown, Cmp->isSigned ());
729
- ConstantRange LHSRange =
730
- ConstantRange::makeAllowedICmpRegion (Pred, RHSRange);
717
+ m_APInt (C)))) {
718
+ ConstantRange LHSRange = ConstantRange::makeAllowedICmpRegion (Pred, *C);
731
719
if (Offset)
732
720
LHSRange = LHSRange.sub (*Offset);
733
721
Known = Known.unionWith (LHSRange.toKnownBits ());
0 commit comments