@@ -689,32 +689,25 @@ static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal,
689
689
}
690
690
691
691
// / We want to turn:
692
- // / (select (icmp eq (and X, C1), 0), Y, (BinOp Y, C2))
692
+ // / (select (icmp eq (and X, C1), 0), Y, (or Y, C2))
693
693
// / into:
694
- // / IF C2 u>= C1
695
- // / (BinOp Y, (shl (and X, C1), C3))
696
- // / ELSE
697
- // / (BinOp Y, (lshr (and X, C1), C3))
694
+ // / (or (shl (and X, C1), C3), Y)
698
695
// / iff:
699
- // / 0 on the RHS is the identity value (i.e add, xor, shl, etc...)
700
696
// / C1 and C2 are both powers of 2
701
697
// / where:
702
- // / IF C2 u>= C1
703
- // / C3 = Log(C2) - Log(C1)
704
- // / ELSE
705
- // / C3 = Log(C1) - Log(C2)
698
+ // / C3 = Log(C2) - Log(C1)
706
699
// /
707
700
// / This transform handles cases where:
708
701
// / 1. The icmp predicate is inverted
709
702
// / 2. The select operands are reversed
710
703
// / 3. The magnitude of C2 and C1 are flipped
711
- static Value *foldSelectICmpAndBinOp (const ICmpInst *IC, Value *TrueVal,
704
+ static Value *foldSelectICmpAndOr (const ICmpInst *IC, Value *TrueVal,
712
705
Value *FalseVal,
713
706
InstCombiner::BuilderTy &Builder) {
714
707
// Only handle integer compares. Also, if this is a vector select, we need a
715
708
// vector compare.
716
709
if (!TrueVal->getType ()->isIntOrIntVectorTy () ||
717
- TrueVal->getType ()->isVectorTy () != IC->getType ()->isVectorTy ())
710
+ TrueVal->getType ()->isVectorTy () != IC->getType ()->isVectorTy ())
718
711
return nullptr ;
719
712
720
713
Value *CmpLHS = IC->getOperand (0 );
@@ -742,29 +735,21 @@ static Value *foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal,
742
735
NeedAnd = true ;
743
736
}
744
737
745
- Value *Y, *V = CmpLHS;
746
- BinaryOperator *BinOp;
738
+ Value *Or, *Y, *V = CmpLHS;
747
739
const APInt *C2;
748
740
bool NeedXor;
749
- if (match (FalseVal, m_BinOp (m_Specific (TrueVal), m_Power2 (C2)))) {
741
+ if (match (FalseVal, m_Or (m_Specific (TrueVal), m_Power2 (C2)))) {
750
742
Y = TrueVal;
751
- BinOp = cast<BinaryOperator>( FalseVal) ;
743
+ Or = FalseVal;
752
744
NeedXor = Pred == ICmpInst::ICMP_NE;
753
- } else if (match (TrueVal, m_BinOp (m_Specific (FalseVal), m_Power2 (C2)))) {
745
+ } else if (match (TrueVal, m_Or (m_Specific (FalseVal), m_Power2 (C2)))) {
754
746
Y = FalseVal;
755
- BinOp = cast<BinaryOperator>( TrueVal) ;
747
+ Or = TrueVal;
756
748
NeedXor = Pred == ICmpInst::ICMP_EQ;
757
749
} else {
758
750
return nullptr ;
759
751
}
760
752
761
- // Check that 0 on RHS is identity value for this binop.
762
- auto *IdentityC =
763
- ConstantExpr::getBinOpIdentity (BinOp->getOpcode (), BinOp->getType (),
764
- /* AllowRHSConstant*/ true );
765
- if (IdentityC == nullptr || !IdentityC->isNullValue ())
766
- return nullptr ;
767
-
768
753
unsigned C2Log = C2->logBase2 ();
769
754
770
755
bool NeedShift = C1Log != C2Log;
@@ -773,7 +758,7 @@ static Value *foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal,
773
758
774
759
// Make sure we don't create more instructions than we save.
775
760
if ((NeedShift + NeedXor + NeedZExtTrunc + NeedAnd) >
776
- (IC->hasOneUse () + BinOp ->hasOneUse ()))
761
+ (IC->hasOneUse () + Or ->hasOneUse ()))
777
762
return nullptr ;
778
763
779
764
if (NeedAnd) {
@@ -794,7 +779,7 @@ static Value *foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal,
794
779
if (NeedXor)
795
780
V = Builder.CreateXor (V, *C2);
796
781
797
- return Builder.CreateBinOp (BinOp-> getOpcode () , Y, V );
782
+ return Builder.CreateOr (V , Y);
798
783
}
799
784
800
785
// / Canonicalize a set or clear of a masked set of constant bits to
@@ -1792,7 +1777,7 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
1792
1777
if (Instruction *V = foldSelectZeroOrOnes (ICI, TrueVal, FalseVal, Builder))
1793
1778
return V;
1794
1779
1795
- if (Value *V = foldSelectICmpAndBinOp (ICI, TrueVal, FalseVal, Builder))
1780
+ if (Value *V = foldSelectICmpAndOr (ICI, TrueVal, FalseVal, Builder))
1796
1781
return replaceInstUsesWith (SI, V);
1797
1782
1798
1783
if (Value *V = foldSelectICmpLshrAshr (ICI, TrueVal, FalseVal, Builder))
0 commit comments