-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[InstCombine] Fold binary operator into select, when one of the opera… #166241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…nd is the select's condition
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index e45cc7b0f..7f78c1349 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -370,13 +370,11 @@ static Instruction *foldBinOpIntoSelectWhenConditionIsOperand(Instruction &I) {
ConstantInt::get(Ty, SelLeft->getValue() ^ 1),
ConstantInt::get(Ty, SelRight->getValue() ^ 0));
case Instruction::Add:
- return SelectInst::Create(CommonInput,
- ConstantInt::get(Ty, SelLeft->getValue() + 1),
- SelRight);
+ return SelectInst::Create(
+ CommonInput, ConstantInt::get(Ty, SelLeft->getValue() + 1), SelRight);
case Instruction::Sub:
- return SelectInst::Create(CommonInput,
- ConstantInt::get(Ty, SelLeft->getValue() - 1),
- SelRight);
+ return SelectInst::Create(
+ CommonInput, ConstantInt::get(Ty, SelLeft->getValue() - 1), SelRight);
case Instruction::Mul:
return SelectInst::Create(CommonInput,
ConstantInt::get(Ty, SelLeft->getValue()),
@@ -2510,7 +2508,6 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Y);
}
-
if (Instruction *Res = foldBinOpIntoSelectWhenConditionIsOperand(I))
return Res;
@@ -4167,7 +4164,6 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
if (Instruction *Res = foldBinOpIntoSelectWhenConditionIsOperand(I))
return Res;
-
// If the operands have no common bits set:
// or (mul X, Y), X --> add (mul X, Y), X --> mul X, (Y + 1)
if (match(&I, m_c_DisjointOr(m_OneUse(m_Mul(m_Value(X), m_Value(Y))),
@@ -5271,7 +5267,6 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
if (Instruction *Res = foldBinOpIntoSelectWhenConditionIsOperand(I))
return Res;
-
// Fold (X & M) ^ (Y & ~M) -> (X & M) | (Y & ~M)
// This it a special case in haveNoCommonBitsSet, but the computeKnownBits
// calls in there are unnecessary as SimplifyDemandedInstructionBits should
|
|
maybe it is possible to handle this cases in llvm-project/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp Lines 1753 to 1765 in c193eea
by adding handling of cast something like |
|
Thanks @andjo403 . It works! What to do next? Should I close this PR and you create another one with your code, since you came up with this elegant soultion. Or should I add the code to this PR and have you as another author? |
|
I created a PR with my solution now #166816 |
|
as you mention Integrate the for arithmetic operations with a fast search it seems like foldBinOpIntoSelectOrPhi or FoldOpIntoSelect is only called for add/sub with one constant so maybe that can be something for you to continue with if it is not to compile time heavy. also in foldBinOpIntoSelectOrPhi FoldOpIntoSelect is only called if the first operand is the select but do not think that is a must now that the second parameter is not always a constant. |
…nd is the select's condition
It is a prototype that fixes #163108 .
This patch would fix all the possible cases described in the original issue and cases beyond that (other binary operators).
TODO: