[llvm] Lowering `~x | (x - 1)` to `~blsi(x)` (PR #186722)

via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 15 18:49:49 PDT 2026


https://github.com/user1342234 created https://github.com/llvm/llvm-project/pull/186722

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 

>From 92e4a56ffeb9f85d2dce16bb31260c5d6fc3be47 Mon Sep 17 00:00:00 2001
From: abu <ayywarepremium at gmail.com>
Date: Sun, 15 Mar 2026 18:08:52 -0700
Subject: [PATCH] Lowering `~x | (x - 1)to `~blsi(x)`

---
 .../InstCombine/InstCombineAndOrXor.cpp       | 10 +++
 llvm/test/CodeGen/X86/fold-bmi.ll             | 14 ++++
 llvm/test/Transforms/InstCombine/fold-bmi.ll  | 66 +++++++++++++++++++
 3 files changed, 90 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/fold-bmi.ll
 create mode 100644 llvm/test/Transforms/InstCombine/fold-bmi.ll

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
+}
+



More information about the llvm-commits mailing list