Skip to content

[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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AZero13
Copy link
Contributor

@AZero13 AZero13 commented Jun 3, 2025

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.

@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2025

@llvm/pr-subscribers-llvm-transforms

Author: AZero13 (AZero13)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/142666.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+56-33)
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);

@nikic nikic removed their request for review June 3, 2025 20:46
@AZero13 AZero13 marked this pull request as draft June 3, 2025 22:14
@AZero13 AZero13 marked this pull request as ready for review June 4, 2025 00:27
@AZero13 AZero13 force-pushed the oneuse branch 2 times, most recently from 6de915e to 62dfbbf Compare June 4, 2025 00:29
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants