[llvm] 50000ec - [InstCombine] create helper function for mul patterns with 1<<X; NFC
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 29 09:52:24 PDT 2022
Author: Sanjay Patel
Date: 2022-10-29T12:50:19-04:00
New Revision: 50000ec2cb16a2f7c157164858ae10074064c1ee
URL: https://github.com/llvm/llvm-project/commit/50000ec2cb16a2f7c157164858ae10074064c1ee
DIFF: https://github.com/llvm/llvm-project/commit/50000ec2cb16a2f7c157164858ae10074064c1ee.diff
LOG: [InstCombine] create helper function for mul patterns with 1<<X; NFC
There are at least 2 other potential patterns that could go here.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 7df8e7f936ad..238853818eef 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -140,6 +140,21 @@ static Value *foldMulSelectToNegate(BinaryOperator &I,
return nullptr;
}
+/// Reduce integer multiplication patterns that contain a (1 << Z) factor.
+/// Callers are expected to call this twice to handle commuted patterns.
+static Instruction *foldMulShl1(Value *X, Value *Y, bool HasNSW, bool HasNUW) {
+ // X * (1 << Z) --> X << Z
+ Value *Z;
+ if (match(Y, m_Shl(m_One(), m_Value(Z)))) {
+ BinaryOperator *Shl = BinaryOperator::CreateShl(X, Z);
+ Shl->setHasNoUnsignedWrap(HasNUW);
+ Shl->setHasNoSignedWrap(HasNSW && cast<ShlOperator>(Y)->hasNoSignedWrap());
+ return Shl;
+ }
+
+ return nullptr;
+}
+
Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (Value *V = simplifyMulInst(Op0, Op1, SQ.getWithInstruction(&I)))
@@ -342,25 +357,10 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
match(Op1, m_And(m_Value(), m_One()))))
return BinaryOperator::CreateAnd(Op0, Op1);
- // X*(1 << Y) --> X << Y
- // (1 << Y)*X --> X << Y
- {
- Value *Y;
- BinaryOperator *BO = nullptr;
- bool ShlNSW = false;
- if (match(Op0, m_Shl(m_One(), m_Value(Y)))) {
- BO = BinaryOperator::CreateShl(Op1, Y);
- ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap();
- } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) {
- BO = BinaryOperator::CreateShl(Op0, Y);
- ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
- }
- if (BO) {
- BO->setHasNoUnsignedWrap(HasNUW);
- BO->setHasNoSignedWrap(HasNSW && ShlNSW);
- return BO;
- }
- }
+ if (Instruction *R = foldMulShl1(Op0, Op1, HasNSW, HasNUW))
+ return R;
+ if (Instruction *R = foldMulShl1(Op1, Op0, HasNSW, HasNUW))
+ return R;
// (zext bool X) * (zext bool Y) --> zext (and X, Y)
// (sext bool X) * (sext bool Y) --> zext (and X, Y)
More information about the llvm-commits
mailing list