[PATCH] D85446: [InstCombine] Add vector support to mul(add(x,c),negpow2) -> mul(sub(-c,x),pow2) folds
Roman Lebedev via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 6 09:46:15 PDT 2020
lebedev.ri added a comment.
Aha, good catch.
But this is not enough of a generalization:
diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 172b304b776..06a0fcbf0fa 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -233,29 +233,11 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
}
}
- if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
- // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n
- // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n
- // The "* (2**n)" thus becomes a potential shifting opportunity.
- {
- const APInt & Val = CI->getValue();
- const APInt &PosVal = Val.abs();
- if (Val.isNegative() && PosVal.isPowerOf2()) {
- Value *X = nullptr, *Y = nullptr;
- if (Op0->hasOneUse()) {
- ConstantInt *C1;
- Value *Sub = nullptr;
- if (match(Op0, m_Sub(m_Value(Y), m_Value(X))))
- Sub = Builder.CreateSub(X, Y, "suba");
- else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1))))
- Sub = Builder.CreateSub(Builder.CreateNeg(C1), Y, "subc");
- if (Sub)
- return
- BinaryOperator::CreateMul(Sub,
- ConstantInt::get(Y->getType(), PosVal));
- }
- }
- }
+ if (match(Op1, m_NegatedPower2())) {
+ // Interpret X * (-1<<C) as (-X) * (1<<C) and try to sink the negation.
+ if (Value *NegOp0 = Negator::Negate(/*IsNegation*/ false, Op0, *this))
+ return BinaryOperator::CreateMul(
+ NegOp0, ConstantExpr::getNeg(cast<Constant>(Op1)), I.getName());
}
if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
We miss a similar thing for `sdiv exact %x, -1<<C --> -(ashr exact %x, C)`
I can take over this patch if you prefer.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D85446/new/
https://reviews.llvm.org/D85446
More information about the llvm-commits
mailing list