Skip to content

Lowering ~x | (x - 1) to ~blsi(x)#186722

Merged
dtcxzyw merged 4 commits into
llvm:mainfrom
user1342234:blsi-fix
Mar 19, 2026
Merged

Lowering ~x | (x - 1) to ~blsi(x)#186722
dtcxzyw merged 4 commits into
llvm:mainfrom
user1342234:blsi-fix

Conversation

@user1342234

@user1342234 user1342234 commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Alive2 proof:
https://alive2.llvm.org/ce/z/bK93Cn

I've implemented a fold in InstCombineAndOrXor.cpp to canonicalize ~x | (x - 1) to ~(x & -x) which enables the CodeGen to emit the blsi instruction.

I've also added a test in CodeGen/X86 though I'm not sure if it's required or not.

Fixes #184055

@user1342234
user1342234 requested a review from nikic as a code owner March 16, 2026 01:49
@github-actions

Copy link
Copy Markdown

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 llvmbot added backend:X86 llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Mar 16, 2026
@llvmbot

llvmbot commented Mar 16, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-llvm-transforms

Author: Abu (user1342234)

Changes

Alive2 proof:
https://alive2.llvm.org/ce/z/bK93Cn

I've implemented a fold in InstCombineAndOrXor.cpp to canonicalize ~x | (x - 1) to (x & -x) which enables the CodeGen to emit the blsi instruction.

I've also added a test in CodeGen/X86 though I'm not sure if it's required or not.

Fixes #184055


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

3 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+10)
  • (added) llvm/test/CodeGen/X86/fold-bmi.ll (+14)
  • (added) llvm/test/Transforms/InstCombine/fold-bmi.ll (+66)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 4c8c708fba986..97f1558a29f3e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -4208,6 +4208,16 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
     return BinaryOperator::CreateMul(X, IncrementY);
   }
 
+  // Canonicalization to achieve lowering to Bit Manipulation Instructions (BMI)
+  // ~X | (X-1) => (X & -X)
+  Value *Op;
+  if (match(&I, m_c_Or(m_OneUse(m_Not(m_Value(Op))),
+                       m_OneUse(m_Add(m_Deferred(Op), m_AllOnes()))))) {
+    Value *NegX = Builder.CreateNeg(Op);
+    Value *And = Builder.CreateAnd(Op, NegX);
+    return BinaryOperator::CreateNot(And);
+  }
+
   // (C && A) || (C && B) => select C, A, B (and similar cases)
   //
   // Note: This is the same transformation used in `foldSelectOfBools`,
diff --git a/llvm/test/CodeGen/X86/fold-bmi.ll b/llvm/test/CodeGen/X86/fold-bmi.ll
new file mode 100644
index 0000000000000..c4a4d1047f130
--- /dev/null
+++ b/llvm/test/CodeGen/X86/fold-bmi.ll
@@ -0,0 +1,14 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+bmi -filetype=asm | FileCheck %s
+
+define i64 @foo(i64 %x) {
+; CHECK-LABEL: foo:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    blsiq %rdi, %rax
+; CHECK-NEXT:    notq %rax
+; CHECK-NEXT:    retq
+  %neg = sub i64 0, %x
+  %and = and i64 %x, %neg
+  %not = xor i64 %and, -1
+  ret i64 %not
+}
diff --git a/llvm/test/Transforms/InstCombine/fold-bmi.ll b/llvm/test/Transforms/InstCombine/fold-bmi.ll
new file mode 100644
index 0000000000000..ca07d45c65519
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fold-bmi.ll
@@ -0,0 +1,66 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+define i64 @test(i64 %x) {
+; CHECK-LABEL: define i64 @foo(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub i64 0, [[X]]
+; CHECK-NEXT:    [[AND:%.*]] = and i64 [[X]], [[TMP1]]
+; CHECK-NEXT:    [[NOT:%.*]] = xor i64 [[AND]], -1
+; CHECK-NEXT:    ret i64 [[NOT]]
+;
+  %xor = xor i64 %x, -1
+  %add = add i64 %x, -1
+  %or  = or i64 %xor, %add
+  ret i64 %or
+}
+
+define i64 @commutative(i64 %x) {
+; CHECK-LABEL: define i64 @commutative(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub i64 0, [[X]]
+; CHECK-NEXT:    [[TMP2:%.*]] = and i64 [[X]], [[TMP1]]
+; CHECK-NEXT:    [[OR:%.*]] = xor i64 [[TMP2]], -1
+; CHECK-NEXT:    ret i64 [[OR]]
+;
+  %xor = xor i64 %x, -1
+  %add = add i64 %x, -1
+  %or  = or i64 %add, %xor
+  ret i64 %or
+}
+
+
+; NEGATIVE TEST: add has multiple uses
+define i64 @negative_multiuse_add(i64 %x, ptr %p) {
+; CHECK-LABEL: define i64 @negative_multiuse_add(
+; CHECK-SAME: i64 [[X:%.*]], ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[XOR:%.*]] = xor i64 [[X]], -1
+; CHECK-NEXT:    [[ADD:%.*]] = add i64 [[X]], -1
+; CHECK-NEXT:    store i64 [[ADD]], ptr [[P]], align 4
+; CHECK-NEXT:    [[OR:%.*]] = or i64 [[ADD]], [[XOR]]
+; CHECK-NEXT:    ret i64 [[OR]]
+;
+  %xor = xor i64 %x, -1
+  %add = add i64 %x, -1
+  store i64 %add, ptr %p     ; second use of %add
+  %or  = or i64 %add, %xor
+  ret i64 %or
+}
+
+; NEGATIVE TEST: xor has multiple uses
+define i64 @negative_multiuse_xor(i64 %x, ptr %p) {
+; CHECK-LABEL: define i64 @negative_multiuse_xor(
+; CHECK-SAME: i64 [[X:%.*]], ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[XOR:%.*]] = xor i64 [[X]], -1
+; CHECK-NEXT:    [[ADD:%.*]] = add i64 [[X]], -1
+; CHECK-NEXT:    store i64 [[XOR]], ptr [[P]], align 4
+; CHECK-NEXT:    [[OR:%.*]] = or i64 [[ADD]], [[XOR]]
+; CHECK-NEXT:    ret i64 [[OR]]
+;
+  %xor = xor i64 %x, -1
+  %add = add i64 %x, -1
+  store i64 %xor, ptr %p     ; second use of %xor
+  %or  = or i64 %add, %xor
+  ret i64 %or
+}
+

@llvmbot

llvmbot commented Mar 16, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-backend-x86

Author: Abu (user1342234)

Changes

Alive2 proof:
https://alive2.llvm.org/ce/z/bK93Cn

I've implemented a fold in InstCombineAndOrXor.cpp to canonicalize ~x | (x - 1) to (x &amp; -x) which enables the CodeGen to emit the blsi instruction.

I've also added a test in CodeGen/X86 though I'm not sure if it's required or not.

Fixes #184055


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

3 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+10)
  • (added) llvm/test/CodeGen/X86/fold-bmi.ll (+14)
  • (added) llvm/test/Transforms/InstCombine/fold-bmi.ll (+66)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 4c8c708fba986..97f1558a29f3e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -4208,6 +4208,16 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
     return BinaryOperator::CreateMul(X, IncrementY);
   }
 
+  // Canonicalization to achieve lowering to Bit Manipulation Instructions (BMI)
+  // ~X | (X-1) => (X & -X)
+  Value *Op;
+  if (match(&I, m_c_Or(m_OneUse(m_Not(m_Value(Op))),
+                       m_OneUse(m_Add(m_Deferred(Op), m_AllOnes()))))) {
+    Value *NegX = Builder.CreateNeg(Op);
+    Value *And = Builder.CreateAnd(Op, NegX);
+    return BinaryOperator::CreateNot(And);
+  }
+
   // (C && A) || (C && B) => select C, A, B (and similar cases)
   //
   // Note: This is the same transformation used in `foldSelectOfBools`,
diff --git a/llvm/test/CodeGen/X86/fold-bmi.ll b/llvm/test/CodeGen/X86/fold-bmi.ll
new file mode 100644
index 0000000000000..c4a4d1047f130
--- /dev/null
+++ b/llvm/test/CodeGen/X86/fold-bmi.ll
@@ -0,0 +1,14 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+bmi -filetype=asm | FileCheck %s
+
+define i64 @foo(i64 %x) {
+; CHECK-LABEL: foo:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    blsiq %rdi, %rax
+; CHECK-NEXT:    notq %rax
+; CHECK-NEXT:    retq
+  %neg = sub i64 0, %x
+  %and = and i64 %x, %neg
+  %not = xor i64 %and, -1
+  ret i64 %not
+}
diff --git a/llvm/test/Transforms/InstCombine/fold-bmi.ll b/llvm/test/Transforms/InstCombine/fold-bmi.ll
new file mode 100644
index 0000000000000..ca07d45c65519
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fold-bmi.ll
@@ -0,0 +1,66 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+define i64 @test(i64 %x) {
+; CHECK-LABEL: define i64 @foo(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub i64 0, [[X]]
+; CHECK-NEXT:    [[AND:%.*]] = and i64 [[X]], [[TMP1]]
+; CHECK-NEXT:    [[NOT:%.*]] = xor i64 [[AND]], -1
+; CHECK-NEXT:    ret i64 [[NOT]]
+;
+  %xor = xor i64 %x, -1
+  %add = add i64 %x, -1
+  %or  = or i64 %xor, %add
+  ret i64 %or
+}
+
+define i64 @commutative(i64 %x) {
+; CHECK-LABEL: define i64 @commutative(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub i64 0, [[X]]
+; CHECK-NEXT:    [[TMP2:%.*]] = and i64 [[X]], [[TMP1]]
+; CHECK-NEXT:    [[OR:%.*]] = xor i64 [[TMP2]], -1
+; CHECK-NEXT:    ret i64 [[OR]]
+;
+  %xor = xor i64 %x, -1
+  %add = add i64 %x, -1
+  %or  = or i64 %add, %xor
+  ret i64 %or
+}
+
+
+; NEGATIVE TEST: add has multiple uses
+define i64 @negative_multiuse_add(i64 %x, ptr %p) {
+; CHECK-LABEL: define i64 @negative_multiuse_add(
+; CHECK-SAME: i64 [[X:%.*]], ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[XOR:%.*]] = xor i64 [[X]], -1
+; CHECK-NEXT:    [[ADD:%.*]] = add i64 [[X]], -1
+; CHECK-NEXT:    store i64 [[ADD]], ptr [[P]], align 4
+; CHECK-NEXT:    [[OR:%.*]] = or i64 [[ADD]], [[XOR]]
+; CHECK-NEXT:    ret i64 [[OR]]
+;
+  %xor = xor i64 %x, -1
+  %add = add i64 %x, -1
+  store i64 %add, ptr %p     ; second use of %add
+  %or  = or i64 %add, %xor
+  ret i64 %or
+}
+
+; NEGATIVE TEST: xor has multiple uses
+define i64 @negative_multiuse_xor(i64 %x, ptr %p) {
+; CHECK-LABEL: define i64 @negative_multiuse_xor(
+; CHECK-SAME: i64 [[X:%.*]], ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[XOR:%.*]] = xor i64 [[X]], -1
+; CHECK-NEXT:    [[ADD:%.*]] = add i64 [[X]], -1
+; CHECK-NEXT:    store i64 [[XOR]], ptr [[P]], align 4
+; CHECK-NEXT:    [[OR:%.*]] = or i64 [[ADD]], [[XOR]]
+; CHECK-NEXT:    ret i64 [[OR]]
+;
+  %xor = xor i64 %x, -1
+  %add = add i64 %x, -1
+  store i64 %xor, ptr %p     ; second use of %xor
+  %or  = or i64 %add, %xor
+  ret i64 %or
+}
+

Comment thread llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Outdated
@github-actions

github-actions Bot commented Mar 16, 2026

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 132407 tests passed
  • 3011 tests skipped

✅ The build succeeded and all tests passed.

@github-actions

github-actions Bot commented Mar 16, 2026

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 192343 tests passed
  • 4935 tests skipped

✅ The build succeeded and all tests passed.

Comment thread llvm/test/CodeGen/X86/fold-bmi.ll Outdated
%and = and i64 %x, %neg
%not = xor i64 %and, -1
ret i64 %not
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please can you put this inside bmi.ll instead (renamed blsi64_not()) and add a blsi32_not variant as well - keep it with the other the blsi patterns inside bmi.ll

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks

@user1342234

user1342234 commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

Pushed changes to fix failing test and moved fold-bmi.ll into bmi.ll.

@user1342234
user1342234 requested a review from RKSimon March 17, 2026 08:10

@RKSimon RKSimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

a few minors

Comment thread llvm/test/CodeGen/X86/bmi.ll Outdated
Comment thread llvm/test/Transforms/InstCombine/fold-bmi.ll Outdated
Comment thread llvm/test/Transforms/InstCombine/fold-bmi.ll
@user1342234
user1342234 requested a review from RKSimon March 19, 2026 04:34

@RKSimon RKSimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@dtcxzyw are the instcombine changes OK?

@dtcxzyw dtcxzyw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LG

@dtcxzyw
dtcxzyw merged commit 1078a1d into llvm:main Mar 19, 2026
10 checks passed
@github-actions

Copy link
Copy Markdown

@user1342234 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:X86 llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[x86-64 BMI] ~x | (x - 1) should lower to ~blsi(x)

5 participants