@@ -1411,13 +1411,23 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
1411
1411
1412
1412
const APInt *MulC;
1413
1413
if (match (Op0, m_NUWMul (m_Value (X), m_APInt (MulC)))) {
1414
- // Look for a "splat" mul pattern - it replicates bits across each half of
1415
- // a value, so a right shift is just a mask of the low bits:
1416
- // lshr i[2N] (mul nuw X, (2^N)+1), N --> and iN X, (2^N)-1
1417
- // TODO: Generalize to allow more than just half-width shifts?
1418
- if (BitWidth > 2 && ShAmtC * 2 == BitWidth && (*MulC - 1 ).isPowerOf2 () &&
1419
- MulC->logBase2 () == ShAmtC)
1420
- return BinaryOperator::CreateAnd (X, ConstantInt::get (Ty, *MulC - 2 ));
1414
+ if ((*MulC - 1 ).isPowerOf2 () && MulC->logBase2 () == ShAmtC) {
1415
+ // Look for a "splat" mul pattern - it replicates bits across each half
1416
+ // of a value, so a right shift is just a mask of the low bits:
1417
+ // lshr i[2N] (mul nuw X, (2^N)+1), N --> and iN X, (2^N)-1
1418
+ if (BitWidth > 2 && ShAmtC * 2 == BitWidth)
1419
+ return BinaryOperator::CreateAnd (X, ConstantInt::get (Ty, *MulC - 2 ));
1420
+
1421
+ // lshr (mul (X, 2^N + 1)), N -> add (X, lshr(X, N))
1422
+ if (Op0->hasOneUse ()) {
1423
+ auto *NewAdd = BinaryOperator::CreateNUWAdd (
1424
+ X, Builder.CreateLShr (X, ConstantInt::get (Ty, ShAmtC), " " ,
1425
+ I.isExact ()));
1426
+ NewAdd->setHasNoSignedWrap (
1427
+ cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap ());
1428
+ return NewAdd;
1429
+ }
1430
+ }
1421
1431
1422
1432
// The one-use check is not strictly necessary, but codegen may not be
1423
1433
// able to invert the transform and perf may suffer with an extra mul
0 commit comments