@@ -1324,26 +1324,23 @@ Instruction *InstCombinerImpl::foldICmpWithZero(ICmpInst &Cmp) {
13241324// to
13251325// icmp eq 0, (and num, val - 1)
13261326// For value being power of two
1327- Instruction *InstCombinerImpl::foldNextMultiply (ICmpInst &Cmp) {
1328- Value *Op0 = Cmp.getOperand (0 );
1329- Value *Neg, *Add, * Num, *Mask, *Value;
1327+ Instruction *InstCombinerImpl::foldIsMultipleOfAPowerOfTwo (ICmpInst &Cmp) {
1328+ Value *Op0 = Cmp.getOperand (0 ), *Op1 = Cmp. getOperand ( 1 ) ;
1329+ Value *Neg, *Num, *Mask, *Value;
13301330 CmpPredicate Pred;
13311331 const APInt *NegConst, *MaskConst;
13321332
1333- // Match num + neg
1334- if (!match (Op0, m_OneUse (m_c_And (m_Value (Add), m_Value (Neg)))))
1333+ if (!match (&Cmp, m_c_ICmp (Pred, m_Value (Num),
1334+ m_OneUse (m_c_And (
1335+ m_OneUse (m_c_Add (m_Value (Num), m_Value (Mask))),
1336+ m_Value (Neg))))))
13351337 return nullptr ;
13361338
1337- // Match num & mask and handle commutative care
1338- if (!match (Op0, m_c_And (m_c_Add (m_Value (Num), m_Value (Mask)), m_Value (Neg))))
1339+ if (!ICmpInst::isEquality (Pred))
13391340 return nullptr ;
13401341
13411342 // Check the constant case
1342- if (match (Neg, m_APInt (NegConst)) && match (Mask, m_APInt (MaskConst))) {
1343- // Mask + 1 should be a power-of-two
1344- if (!(*MaskConst + 1 ).isPowerOf2 ())
1345- return nullptr ;
1346-
1343+ if (match (Neg, m_APInt (NegConst)) && match (Mask, m_LowBitMask (MaskConst))) {
13471344 // Neg = -(Mask + 1)
13481345 if (*NegConst != -(*MaskConst + 1 ))
13491346 return nullptr ;
@@ -1367,11 +1364,7 @@ Instruction *InstCombinerImpl::foldNextMultiply(ICmpInst &Cmp) {
13671364 return nullptr ;
13681365 }
13691366
1370- // Verify that Add and Num are connected by ICmp.
1371- if (!match (&Cmp, m_c_ICmp (Pred, m_Value (Add), m_Specific (Num))))
1372- return nullptr ;
1373-
1374- if (!ICmpInst::isEquality (Pred))
1367+ if (!match (Op0, m_Specific (Num)) && !match (Op1, m_Specific (Num)))
13751368 return nullptr ;
13761369
13771370 // Create new icmp eq (num & (val - 1)), 0
@@ -7705,7 +7698,7 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
77057698 if (Instruction *Res = foldICmpUsingKnownBits (I))
77067699 return Res;
77077700
7708- if (Instruction *Res = foldNextMultiply (I))
7701+ if (Instruction *Res = foldIsMultipleOfAPowerOfTwo (I))
77097702 return Res;
77107703
77117704 // Test if the ICmpInst instruction is used exclusively by a select as
0 commit comments