@@ -2772,6 +2772,7 @@ static Instruction *foldSelectWithClampedShift(SelectInst &SI,
27722772
27732773 auto MatchClampedShift = [&](Value *V, Value *Amt) -> BinaryOperator * {
27742774 Value *X, *Limit;
2775+ Instruction *I;
27752776
27762777 // Fold (select (icmp_ugt A, BW-1), TrueVal, (shift X, (umin A, C)))
27772778 // --> (select (icmp_ugt A, BW-1), TrueVal, (shift X, A))
@@ -2789,13 +2790,15 @@ static Instruction *foldSelectWithClampedShift(SelectInst &SI,
27892790 // --> (select (icmp_ugt A, BW-1), (shift X, A), FalseVal)
27902791 // Fold (select (icmp_ult A, BW), (shift X, (and A, C)), FalseVal)
27912792 // --> (select (icmp_ult A, BW), (shift X, A), FalseVal)
2792- // iff Pow2 element width and C masks all amt bits.
2793+ // iff Pow2 element width we just demand the amt mask bits.
27932794 if (isPowerOf2_64 (BW) &&
2794- match (V, m_OneUse (m_Shift (m_Value (X),
2795- m_And (m_Specific (Amt), m_Value (Limit)))))) {
2796- KnownBits KnownLimit = IC.computeKnownBits (Limit, 0 , &SI);
2797- if (KnownLimit.countMinTrailingOnes () >= Log2_64 (BW))
2798- return cast<BinaryOperator>(V);
2795+ match (V, m_OneUse (m_Shift (m_Value (X), m_Instruction (I))))) {
2796+ KnownBits Known (BW);
2797+ APInt DemandedBits = APInt::getLowBitsSet (BW, Log2_64 (BW));
2798+ if (Value *NewAmt = IC.SimplifyMultipleUseDemandedBits (
2799+ I, DemandedBits, Known, /* Depth=*/ 0 ,
2800+ IC.getSimplifyQuery ().getWithInstruction (I)))
2801+ return Amt == NewAmt ? cast<BinaryOperator>(V) : nullptr ;
27992802 }
28002803
28012804 return nullptr ;
0 commit comments