@@ -1324,26 +1324,23 @@ Instruction *InstCombinerImpl::foldICmpWithZero(ICmpInst &Cmp) {
1324
1324
// to
1325
1325
// icmp eq 0, (and num, val - 1)
1326
1326
// 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;
1330
1330
CmpPredicate Pred;
1331
1331
const APInt *NegConst, *MaskConst;
1332
1332
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))))))
1335
1337
return nullptr ;
1336
1338
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))
1339
1340
return nullptr ;
1340
1341
1341
1342
// 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))) {
1347
1344
// Neg = -(Mask + 1)
1348
1345
if (*NegConst != -(*MaskConst + 1 ))
1349
1346
return nullptr ;
@@ -1367,11 +1364,7 @@ Instruction *InstCombinerImpl::foldNextMultiply(ICmpInst &Cmp) {
1367
1364
return nullptr ;
1368
1365
}
1369
1366
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)))
1375
1368
return nullptr ;
1376
1369
1377
1370
// Create new icmp eq (num & (val - 1)), 0
@@ -7705,7 +7698,7 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
7705
7698
if (Instruction *Res = foldICmpUsingKnownBits (I))
7706
7699
return Res;
7707
7700
7708
- if (Instruction *Res = foldNextMultiply (I))
7701
+ if (Instruction *Res = foldIsMultipleOfAPowerOfTwo (I))
7709
7702
return Res;
7710
7703
7711
7704
// Test if the ICmpInst instruction is used exclusively by a select as
0 commit comments