@@ -2772,6 +2772,7 @@ static Instruction *foldSelectWithClampedShift(SelectInst &SI,
2772
2772
2773
2773
auto MatchClampedShift = [&](Value *V, Value *Amt) -> BinaryOperator * {
2774
2774
Value *X, *Limit;
2775
+ Instruction *I;
2775
2776
2776
2777
// Fold (select (icmp_ugt A, BW-1), TrueVal, (shift X, (umin A, C)))
2777
2778
// --> (select (icmp_ugt A, BW-1), TrueVal, (shift X, A))
@@ -2789,13 +2790,15 @@ static Instruction *foldSelectWithClampedShift(SelectInst &SI,
2789
2790
// --> (select (icmp_ugt A, BW-1), (shift X, A), FalseVal)
2790
2791
// Fold (select (icmp_ult A, BW), (shift X, (and A, C)), FalseVal)
2791
2792
// --> (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.
2793
2794
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 ;
2799
2802
}
2800
2803
2801
2804
return nullptr ;
0 commit comments