Skip to content

Commit 1a9b7f5

Browse files
committed
[InstCombine] lshr (mul (X, 2^N + 1)), N -> add (X, lshr(X, N))
A generalization of the proposed x * 3/2 -> x + (x >> 1) transformation but applies to all multiplications that are one more than a power of 2. Proof: https://alive2.llvm.org/ce/z/WaZYvn
1 parent 600cad2 commit 1a9b7f5

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,13 +1411,24 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
14111411

14121412
const APInt *MulC;
14131413
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 (BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
1415+
MulC->logBase2() == ShAmtC) {
1416+
// Look for a "splat" mul pattern - it replicates bits across each half
1417+
// of a value, so a right shift is just a mask of the low bits:
1418+
// lshr i[2N] (mul nuw X, (2^N)+1), N --> and iN X, (2^N)-1
1419+
if (ShAmtC * 2 == BitWidth)
1420+
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2));
1421+
1422+
// lshr (mul nuw (X, 2^N + 1)), N -> add nuw (X, lshr(X, N))
1423+
if (Op0->hasOneUse()) {
1424+
auto *NewAdd = BinaryOperator::CreateNUWAdd(
1425+
X, Builder.CreateLShr(X, ConstantInt::get(Ty, ShAmtC), "",
1426+
I.isExact()));
1427+
NewAdd->setHasNoSignedWrap(
1428+
cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap());
1429+
return NewAdd;
1430+
}
1431+
}
14211432

14221433
// The one-use check is not strictly necessary, but codegen may not be
14231434
// able to invert the transform and perf may suffer with an extra mul
@@ -1437,6 +1448,16 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
14371448
}
14381449
}
14391450

1451+
// lshr (mul nsw (X, 2^N + 1)), N -> add nsw (X, lshr(X, N))
1452+
if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_APInt(MulC))))) {
1453+
if (BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
1454+
MulC->logBase2() == ShAmtC) {
1455+
return BinaryOperator::CreateNSWAdd(
1456+
X, Builder.CreateLShr(X, ConstantInt::get(Ty, ShAmtC), "",
1457+
I.isExact()));
1458+
}
1459+
}
1460+
14401461
// Try to narrow bswap.
14411462
// In the case where the shift amount equals the bitwidth difference, the
14421463
// shift is eliminated.
@@ -1681,4 +1702,4 @@ Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) {
16811702
}
16821703

16831704
return nullptr;
1684-
}
1705+
}

0 commit comments

Comments
 (0)