-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[InstCombine] Factor in op0's usages to decide leniency for one-use in foldComplexAndOrPatterns #142666
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
Open
AZero13
wants to merge
1
commit into
llvm:main
Choose a base branch
from
AZero13:oneuse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-transforms Author: AZero13 (AZero13) ChangesIf we can eliminate Op0 by replacing, which will happen if Op0 is one use, then we need not check if the other is one-use. Full diff: https://github.com/llvm/llvm-project/pull/142666.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 2fb4bfecda8aa..a459be8fd7934 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2036,12 +2036,10 @@ static Instruction *foldComplexAndOrPatterns(BinaryOperator &I,
// (~(A | B) & C) | ... --> ...
// (~(A & B) | C) & ... --> ...
- // TODO: One use checks are conservative. We just need to check that a total
- // number of multiple used values does not exceed reduction
- // in operations.
if (matchNotOrAnd(Op0, m_Value(A), m_Value(B), m_Value(C), X)) {
// (~(A | B) & C) | (~(A | C) & B) --> (B ^ C) & ~A
// (~(A & B) | C) & (~(A & C) | B) --> ~((B ^ C) & A)
+
if (matchNotOrAnd(Op1, m_Specific(A), m_Specific(C), m_Specific(B), Dummy,
true)) {
Value *Xor = Builder.CreateXor(B, C);
@@ -2060,17 +2058,29 @@ static Instruction *foldComplexAndOrPatterns(BinaryOperator &I,
: BinaryOperator::CreateNot(Builder.CreateAnd(Xor, B));
}
+ bool Op0OneUse = Op0->hasOneUse();
+
// (~(A | B) & C) | ~(A | C) --> ~((B & C) | A)
// (~(A & B) | C) & ~(A & C) --> ~((B | C) & A)
- if (match(Op1, m_OneUse(m_Not(m_OneUse(
- m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))))
+ if (!Op0OneUse && match(Op1, m_OneUse(m_Not(m_OneUse(m_c_BinOp(
+ Opcode, m_Specific(A), m_Specific(C)))))))
+ return BinaryOperator::CreateNot(Builder.CreateBinOp(
+ Opcode, Builder.CreateBinOp(FlippedOpcode, B, C), A));
+
+ if (Op0OneUse &&
+ match(Op1, m_Not(m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))
return BinaryOperator::CreateNot(Builder.CreateBinOp(
Opcode, Builder.CreateBinOp(FlippedOpcode, B, C), A));
// (~(A | B) & C) | ~(B | C) --> ~((A & C) | B)
// (~(A & B) | C) & ~(B & C) --> ~((A | C) & B)
- if (match(Op1, m_OneUse(m_Not(m_OneUse(
- m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)))))))
+ if (!Op0OneUse && match(Op1, m_OneUse(m_Not(m_OneUse(m_c_BinOp(
+ Opcode, m_Specific(B), m_Specific(C)))))))
+ return BinaryOperator::CreateNot(Builder.CreateBinOp(
+ Opcode, Builder.CreateBinOp(FlippedOpcode, A, C), B));
+
+ if (Op0OneUse &&
+ match(Op1, m_Not(m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)))))
return BinaryOperator::CreateNot(Builder.CreateBinOp(
Opcode, Builder.CreateBinOp(FlippedOpcode, A, C), B));
@@ -2078,7 +2088,7 @@ static Instruction *foldComplexAndOrPatterns(BinaryOperator &I,
// Note, the pattern with swapped and/or is not handled because the
// result is more undefined than a source:
// (~(A & B) | C) & ~(C & (A ^ B)) --> (A ^ B ^ C) | ~(A | C) is invalid.
- if (Opcode == Instruction::Or && Op0->hasOneUse() &&
+ if (Opcode == Instruction::Or && Op0OneUse &&
match(Op1, m_OneUse(m_Not(m_CombineAnd(
m_Value(Y),
m_c_BinOp(Opcode, m_Specific(C),
@@ -2092,30 +2102,29 @@ static Instruction *foldComplexAndOrPatterns(BinaryOperator &I,
// (~A & B & C) | ... --> ...
// (~A | B | C) | ... --> ...
- // TODO: One use checks are conservative. We just need to check that a total
- // number of multiple used values does not exceed reduction
- // in operations.
- if (match(Op0,
- m_OneUse(m_c_BinOp(FlippedOpcode,
- m_BinOp(FlippedOpcode, m_Value(B), m_Value(C)),
- m_CombineAnd(m_Value(X), m_Not(m_Value(A)))))) ||
- match(Op0, m_OneUse(m_c_BinOp(
- FlippedOpcode,
- m_c_BinOp(FlippedOpcode, m_Value(C),
- m_CombineAnd(m_Value(X), m_Not(m_Value(A)))),
- m_Value(B))))) {
+ if (match(Op0, m_c_BinOp(FlippedOpcode,
+ m_BinOp(FlippedOpcode, m_Value(B), m_Value(C)),
+ m_CombineAnd(m_Value(X), m_Not(m_Value(A))))) ||
+ match(Op0,
+ m_c_BinOp(FlippedOpcode,
+ m_c_BinOp(FlippedOpcode, m_Value(C),
+ m_CombineAnd(m_Value(X), m_Not(m_Value(A)))),
+ m_Value(B)))) {
+ bool Op0OneUse = Op0->hasOneUse();
+
// X = ~A
// (~A & B & C) | ~(A | B | C) --> ~(A | (B ^ C))
// (~A | B | C) & ~(A & B & C) --> (~A | (B ^ C))
- if (match(Op1, m_OneUse(m_Not(m_c_BinOp(
- Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)),
- m_Specific(C))))) ||
- match(Op1, m_OneUse(m_Not(m_c_BinOp(
- Opcode, m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)),
- m_Specific(A))))) ||
- match(Op1, m_OneUse(m_Not(m_c_BinOp(
- Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)),
- m_Specific(B)))))) {
+ if ((match(Op1, m_Not(m_c_BinOp(
+ Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)),
+ m_Specific(C)))) ||
+ match(Op1, m_Not(m_c_BinOp(
+ Opcode, m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)),
+ m_Specific(A)))) ||
+ match(Op1, m_Not(m_c_BinOp(
+ Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)),
+ m_Specific(B))))) &&
+ (Op0OneUse || Op1->hasOneUse())) {
Value *Xor = Builder.CreateXor(B, C);
return (Opcode == Instruction::Or)
? BinaryOperator::CreateNot(Builder.CreateOr(Xor, A))
@@ -2124,16 +2133,30 @@ static Instruction *foldComplexAndOrPatterns(BinaryOperator &I,
// (~A & B & C) | ~(A | B) --> (C | ~B) & ~A
// (~A | B | C) & ~(A & B) --> (C & ~B) | ~A
- if (match(Op1, m_OneUse(m_Not(m_OneUse(
- m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)))))))
+ if (!Op0OneUse && match(Op1, m_OneUse(m_Not(m_OneUse(m_c_BinOp(
+ Opcode, m_Specific(A), m_Specific(B)))))))
return BinaryOperator::Create(
FlippedOpcode, Builder.CreateBinOp(Opcode, C, Builder.CreateNot(B)),
X);
+ if (Op0OneUse &&
+ match(Op1, m_Not(m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)))))
+ return BinaryOperator::Create(
+ FlippedOpcode, Builder.CreateBinOp(Opcode, C, Builder.CreateNot(B)),
+ X);
+
+ // (~A & B & C) | ~(A | C) --> (B | ~C) & ~A
+ // (~A | B | C) & ~(A & C) --> (B & ~C) | ~A
+ if (!Op0OneUse && match(Op1, m_OneUse(m_Not(m_OneUse(m_c_BinOp(
+ Opcode, m_Specific(A), m_Specific(C)))))))
+ return BinaryOperator::Create(
+ FlippedOpcode, Builder.CreateBinOp(Opcode, B, Builder.CreateNot(C)),
+ X);
+
// (~A & B & C) | ~(A | C) --> (B | ~C) & ~A
// (~A | B | C) & ~(A & C) --> (B & ~C) | ~A
- if (match(Op1, m_OneUse(m_Not(m_OneUse(
- m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))))
+ if (Op0OneUse &&
+ match(Op1, m_Not(m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))
return BinaryOperator::Create(
FlippedOpcode, Builder.CreateBinOp(Opcode, B, Builder.CreateNot(C)),
X);
|
6de915e
to
62dfbbf
Compare
…n foldComplexAndOrPatterns If we can eliminate Op0 by replacing, which will happen if Op0 is one use, then we need not check if the other is one-use. This way, we can still be sure that there is a net negative of instructions.
This was referenced Jun 4, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If we can eliminate Op0 by replacing, which will happen if Op0 is one use, then we need not check if the other is one-use.
This way, we can still be sure that there is a net negative of instructions.