@@ -630,14 +630,14 @@ static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
630
630
return false ;
631
631
}
632
632
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 ()) {
637
637
// Handle comparison of pointer to null explicitly, as it will not be
638
638
// 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 ) {
641
641
case ICmpInst::ICMP_EQ:
642
642
Known.setAllZero ();
643
643
break ;
@@ -659,44 +659,41 @@ static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp,
659
659
auto m_V =
660
660
m_CombineOr (m_Specific (V), m_PtrToIntSameSize (Q.DL , m_Specific (V)));
661
661
662
- CmpInst::Predicate Pred;
663
662
const APInt *Mask, *C;
664
663
uint64_t ShAmt;
665
- switch (Cmp-> getPredicate () ) {
664
+ switch (Pred ) {
666
665
case ICmpInst::ICMP_EQ:
667
666
// 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))) {
669
668
Known = Known.unionWith (KnownBits::makeConstant (*C));
670
669
// 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 ))) {
673
672
// For one bits in Mask, we can propagate bits from C to V.
674
673
Known.Zero |= ~*C & *Mask;
675
674
Known.One |= *C & *Mask;
676
675
// 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))) {
678
677
// For zero bits in Mask, we can propagate bits from C to V.
679
678
Known.Zero |= ~*C & ~*Mask;
680
679
Known.One |= *C & ~*Mask;
681
680
// 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 ))) {
684
683
// Equivalent to assume(V == Mask ^ C)
685
684
Known = Known.unionWith (KnownBits::makeConstant (*C ^ *Mask));
686
685
// 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) {
690
688
// For those bits in C that are known, we can propagate them to known
691
689
// bits in V shifted to the right by ShAmt.
692
690
KnownBits RHSKnown = KnownBits::makeConstant (*C);
693
691
RHSKnown.Zero .lshrInPlace (ShAmt);
694
692
RHSKnown.One .lshrInPlace (ShAmt);
695
693
Known = Known.unionWith (RHSKnown);
696
694
// 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) {
700
697
KnownBits RHSKnown = KnownBits::makeConstant (*C);
701
698
// For those bits in RHS that are known, we can propagate them to known
702
699
// bits in V shifted to the right by C.
@@ -707,14 +704,14 @@ static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp,
707
704
case ICmpInst::ICMP_NE: {
708
705
// assume (V & B != 0) where B is a power of 2
709
706
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 ()))
711
708
Known.One |= *BPow2;
712
709
break ;
713
710
}
714
711
default :
715
712
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))) {
718
715
ConstantRange LHSRange = ConstantRange::makeAllowedICmpRegion (Pred, *C);
719
716
if (Offset)
720
717
LHSRange = LHSRange.sub (*Offset);
@@ -788,7 +785,8 @@ void llvm::computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
788
785
if (!isValidAssumeForContext (I, Q.CxtI , Q.DT ))
789
786
continue ;
790
787
791
- computeKnownBitsFromCmp (V, Cmp, Known, Depth, Q);
788
+ computeKnownBitsFromCmp (V, Cmp->getPredicate (), Cmp->getOperand (0 ),
789
+ Cmp->getOperand (1 ), Known, Depth, Q);
792
790
}
793
791
794
792
// Conflicting assumption: Undefined behavior will occur on this execution
0 commit comments