Skip to content
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

[InstCombine] Fold bool (((A & B) ^ 1) & C) & A into ((B ^ 1) & A) & C #78150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

adamszilagyi
Copy link

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 15, 2024

@llvm/pr-subscribers-llvm-transforms

Author: None (adamszilagyi)

Changes

Issue #75004

Proof: https://alive2.llvm.org/ce/z/fQ62US


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+10)
  • (modified) llvm/test/Transforms/InstCombine/and.ll (+16)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 0620752e321394..21b2a6f181fe7a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2539,6 +2539,16 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
         return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
     }
 
+    // (((A & B) ^ 1) & C) & A -> ((B ^ 1) & A) & C
+    if (match(&I, m_And(m_And(m_Xor(m_And(m_Value(A), m_Value(B)), m_One()),
+                              m_Value(C)),
+                        m_Value(A))) &&
+        A->getType()->isIntOrIntVectorTy(1) &&
+        B->getType()->isIntOrIntVectorTy(1) &&
+        C->getType()->isIntOrIntVectorTy(1))
+      return BinaryOperator::CreateAnd(
+          Builder.CreateAnd(Builder.CreateXor(B, 1), A), C);
+
     // (A | B) & (~A ^ B) -> A & B
     // (A | B) & (B ^ ~A) -> A & B
     // (B | A) & (~A ^ B) -> A & B
diff --git a/llvm/test/Transforms/InstCombine/and.ll b/llvm/test/Transforms/InstCombine/and.ll
index 2e37fee07cc47b..ee7d4ecf18f576 100644
--- a/llvm/test/Transforms/InstCombine/and.ll
+++ b/llvm/test/Transforms/InstCombine/and.ll
@@ -2411,6 +2411,22 @@ define i8 @negate_lowbitmask_use1(i8 %x, i8 %y) {
   ret i8 %r
 }
 
+define i32 @fold_and_and_xor1_and(i1 %k, i1 %c, i1 %c1) {
+; CHECK-LABEL: @fold_and_and_xor1_and(
+; CHECK-NEXT:    [[TMP1:%.*]] = xor i1 [[C:%.*]], true
+; CHECK-NEXT:    [[TMP2:%.*]] = and i1 [[TMP1]], [[K:%.*]]
+; CHECK-NEXT:    [[NARROW:%.*]] = and i1 [[TMP2]], [[C1:%.*]]
+; CHECK-NEXT:    [[COND:%.*]] = zext i1 [[NARROW]] to i32
+; CHECK-NEXT:    ret i32 [[COND]]
+;
+  %and12 = and i1 %k, %c
+  %not.and12 = xor i1 %and12, true
+  %1 = and i1 %not.and12, %c1
+  %narrow = and i1 %k, %1
+  %cond = zext i1 %narrow to i32
+  ret i32 %cond
+}
+
 ; negative test
 
 define i8 @negate_lowbitmask_use2(i8 %x, i8 %y) {

@nikic
Copy link
Contributor

nikic commented Jan 15, 2024

I think this will already be fixed generically by #77231.

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.

None yet

3 participants