Lowering ~x | (x - 1) to ~blsi(x)#186722
Conversation
|
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 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. |
|
@llvm/pr-subscribers-llvm-transforms Author: Abu (user1342234) ChangesAlive2 proof: I've implemented a fold in I've also added a test in Fixes #184055 3 Files Affected:
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
+}
+
|
|
@llvm/pr-subscribers-backend-x86 Author: Abu (user1342234) ChangesAlive2 proof: I've implemented a fold in I've also added a test in Fixes #184055 3 Files Affected:
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
+}
+
|
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
| %and = and i64 %x, %neg | ||
| %not = xor i64 %and, -1 | ||
| ret i64 %not | ||
| } |
There was a problem hiding this comment.
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
Co-authored-by: Tim Gymnich <[email protected]>
|
Pushed changes to fix failing test and moved |
|
@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! |
Alive2 proof:
https://alive2.llvm.org/ce/z/bK93Cn
I've implemented a fold in
InstCombineAndOrXor.cppto canonicalize~x | (x - 1)to~(x & -x)which enables the CodeGen to emit theblsiinstruction.I've also added a test in
CodeGen/X86though I'm not sure if it's required or not.Fixes #184055