[llvm] [InstCombine] Fold mul (shr exact (X, N)), 2^N + 1 -> add (X , shr exact (X, N)) (PR #112407)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 7 12:44:53 PST 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 6ef978b8c41a83378af3de1dceeea434715f80f4 19ecf328827d862db4feeae0716606641a3fc75d --extensions cpp -- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index dba6d1bba8..e787574058 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -260,28 +260,28 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
Value *NewOp;
const APInt *ShiftC;
const APInt *MulAP;
- if (BitWidth > 2 && match(&I, m_Mul(m_Exact(m_Shr(m_Value(NewOp), m_APInt(ShiftC))),
- m_APInt(MulAP))) && (*MulAP - 1).isPowerOf2() &&
- *ShiftC == MulAP->logBase2()) {
- Value *BinOp = Op0;
- BinaryOperator *OpBO = cast<BinaryOperator>(Op0);
-
- // mul nuw (ashr exact X, N) -> add nuw (X, lshr exact (X, N))
- if (HasNUW && OpBO->getOpcode() == Instruction::AShr &&
- OpBO->hasOneUse())
- BinOp = Builder.CreateLShr(NewOp, ConstantInt::get(Ty, *ShiftC), "",
- /*isExact=*/true);
-
- auto *NewAdd = BinaryOperator::CreateAdd(NewOp, BinOp);
- if (HasNSW && (HasNUW || OpBO->getOpcode() == Instruction::LShr ||
- ShiftC->getZExtValue() < BitWidth - 1))
- NewAdd->setHasNoSignedWrap(true);
-
- NewAdd->setHasNoUnsignedWrap(HasNUW);
- return NewAdd;
- }
+ if (BitWidth > 2 &&
+ match(&I, m_Mul(m_Exact(m_Shr(m_Value(NewOp), m_APInt(ShiftC))),
+ m_APInt(MulAP))) &&
+ (*MulAP - 1).isPowerOf2() && *ShiftC == MulAP->logBase2()) {
+ Value *BinOp = Op0;
+ BinaryOperator *OpBO = cast<BinaryOperator>(Op0);
+
+ // mul nuw (ashr exact X, N) -> add nuw (X, lshr exact (X, N))
+ if (HasNUW && OpBO->getOpcode() == Instruction::AShr && OpBO->hasOneUse())
+ BinOp = Builder.CreateLShr(NewOp, ConstantInt::get(Ty, *ShiftC), "",
+ /*isExact=*/true);
+
+ auto *NewAdd = BinaryOperator::CreateAdd(NewOp, BinOp);
+ if (HasNSW && (HasNUW || OpBO->getOpcode() == Instruction::LShr ||
+ ShiftC->getZExtValue() < BitWidth - 1))
+ NewAdd->setHasNoSignedWrap(true);
+
+ NewAdd->setHasNoUnsignedWrap(HasNUW);
+ return NewAdd;
}
}
+}
if (Op0->hasOneUse() && match(Op1, m_NegatedPower2())) {
// Interpret X * (-1<<C) as (-X) * (1<<C) and try to sink the negation.
``````````
</details>
https://github.com/llvm/llvm-project/pull/112407
More information about the llvm-commits
mailing list