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 (X & C1) - (X & C2) --> X & (C1 ^ C2) if (C1 & C2) == C2 #119316

Merged
merged 2 commits into from
Dec 10, 2024

Conversation

fengfeng09
Copy link
Contributor

@fengfeng09 fengfeng09 commented Dec 10, 2024

if (C1 & C2) == C2 then (X & C1) - (X & C2) --> X & (C1 ^ C2)

Alive2: https://alive2.llvm.org/ce/z/JvQU8w

@llvmbot
Copy link
Member

llvmbot commented Dec 10, 2024

@llvm/pr-subscribers-llvm-transforms

Author: fengfeng (fengfeng09)

Changes

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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp (+9)
  • (added) llvm/test/Transforms/InstCombine/X86/and-sub-combine.ll (+14)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index ea7942ef978110..51831bba9c423b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2280,6 +2280,15 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
   if (match(Op0, m_OneUse(m_Add(m_Value(X), m_AllOnes()))))
     return BinaryOperator::CreateAdd(Builder.CreateNot(Op1), X);
 
+  const APInt *C1, *C2;
+  // if (C1 & C2) == C2 then (X & C1) - (X & C2) -> X & (C1 ^ C2)
+  if (match(Op0, m_And(m_Value(X), m_APInt(C1))) &&
+      match(Op1, m_And(m_Specific(X), m_APInt(C2)))) {
+    if (C2->eq(*C1 & *C2))
+      return BinaryOperator::CreateAnd(
+          X, ConstantInt::get(I.getType(), *C1 ^ *C2));
+  }
+
   // Reassociate sub/add sequences to create more add instructions and
   // reduce dependency chains:
   // ((X - Y) + Z) - Op1 --> (X + Z) - (Y + Op1)
diff --git a/llvm/test/Transforms/InstCombine/X86/and-sub-combine.ll b/llvm/test/Transforms/InstCombine/X86/and-sub-combine.ll
new file mode 100644
index 00000000000000..5120717bf1481a
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/X86/and-sub-combine.ll
@@ -0,0 +1,14 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -mtriple=x86_64-unknown-unknown -S | FileCheck %s
+
+define i8 @and_sub(i8 %a) {
+; CHECK-LABEL: @and_sub(
+; CHECK-NEXT:    [[RET:%.*]] = and i8 [[A:%.*]], 12
+; CHECK-NEXT:    ret i8 [[RET]]
+;
+  %and1 = and i8 %a, 15
+  %and2 = and i8 %a, 3
+
+  %ret = sub i8 %and1, %and2
+  ret i8 %ret
+}

@dtcxzyw
Copy link
Member

dtcxzyw commented Dec 10, 2024

Please read https://llvm.org/docs/InstCombineContributorGuide.html and

  • Provide alive2 proofs
  • Adjust the PR title

@bcl5980 bcl5980 requested a review from dtcxzyw December 10, 2024 03:40
@fengfeng09 fengfeng09 changed the title And sub combine [InstCombine] And sub combine Dec 10, 2024
@fengfeng09
Copy link
Contributor Author

@dtcxzyw Updated.

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please wait for additional approval from other reviewers.

@dtcxzyw dtcxzyw changed the title [InstCombine] And sub combine [InstCombine] Fold (X & C1) - (X & C2) --> X & (C1 ^ C2) if (C1 & C2) == C2 Dec 10, 2024
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nikic nikic merged commit c3175c5 into llvm:main Dec 10, 2024
8 checks passed
jrbyrnes pushed a commit to jrbyrnes/llvm-project that referenced this pull request Mar 7, 2025
…C2) == C2` (llvm#119316)

if (C1 & C2) == C2 then (X & C1) - (X & C2) --> X & (C1 ^ C2)

Alive2: https://alive2.llvm.org/ce/z/JvQU8w
jrbyrnes added a commit to jrbyrnes/llvm-project that referenced this pull request Mar 7, 2025
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.

4 participants