[llvm] [X86] Use 16-bit BT to eliminate explicit AND mask on bit index (PR #219836)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 30 12:16:31 PDT 2026
https://github.com/AZero13 updated https://github.com/llvm/llvm-project/pull/219836
>From cff56a12d3cf41bc841fa2f58b4fe9aa8c74c0d1 Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Sun, 30 Aug 2026 15:13:24 -0400
Subject: [PATCH 1/2] [X86] Pre-commit test for 16-bit BT optimization
---
llvm/test/CodeGen/X86/bittest-16.ll | 98 +++++++++++++++++++++++++++++
1 file changed, 98 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/bittest-16.ll
diff --git a/llvm/test/CodeGen/X86/bittest-16.ll b/llvm/test/CodeGen/X86/bittest-16.ll
new file mode 100644
index 0000000000000..124c62ce1ff2b
--- /dev/null
+++ b/llvm/test/CodeGen/X86/bittest-16.ll
@@ -0,0 +1,98 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown | FileCheck %s
+; ModuleID = 'test_bt_sizes.c'
+source_filename = "test_bt_sizes.c"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
+define dso_local zeroext i1 @bt_test_16_16(i16 noundef zeroext %a, i16 noundef zeroext %b) local_unnamed_addr #0 {
+; CHECK-LABEL: bt_test_16_16:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: andl $15, %esi
+; CHECK-NEXT: btl %esi, %edi
+; CHECK-NEXT: setb %al
+; CHECK-NEXT: retq
+entry:
+ %conv = zext i16 %a to i32
+ %0 = and i16 %b, 15
+ %and = zext nneg i16 %0 to i32
+ %shl = shl nuw nsw i32 1, %and
+ %and2 = and i32 %shl, %conv
+ %tobool = icmp ne i32 %and2, 0
+ ret i1 %tobool
+}
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
+define dso_local zeroext i1 @bt_test_16_32(i32 noundef %a, i32 noundef %b) local_unnamed_addr #0 {
+; CHECK-LABEL: bt_test_16_32:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: andl $15, %esi
+; CHECK-NEXT: btl %esi, %edi
+; CHECK-NEXT: setb %al
+; CHECK-NEXT: retq
+entry:
+ %and = and i32 %b, 15
+ %shl = shl nuw nsw i32 1, %and
+ %and1 = and i32 %shl, %a
+ %tobool = icmp ne i32 %and1, 0
+ ret i1 %tobool
+}
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
+define dso_local zeroext i1 @bt_test_16_64(i64 noundef %a, i64 noundef %b) local_unnamed_addr #0 {
+; CHECK-LABEL: bt_test_16_64:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: andl $15, %esi
+; CHECK-NEXT: btl %esi, %edi
+; CHECK-NEXT: setb %al
+; CHECK-NEXT: retq
+entry:
+ %and = and i64 %b, 15
+ %shl = shl nuw nsw i64 1, %and
+ %and1 = and i64 %shl, %a
+ %tobool = icmp ne i64 %and1, 0
+ ret i1 %tobool
+}
+
+; Function Attrs: nounwind uwtable
+define dso_local zeroext i1 @bt_test_16_multiuse(i32 noundef %a, i32 noundef %b) local_unnamed_addr #1 {
+; CHECK-LABEL: bt_test_16_multiuse:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: pushq %rbp
+; CHECK-NEXT: .cfi_def_cfa_offset 16
+; CHECK-NEXT: pushq %rbx
+; CHECK-NEXT: .cfi_def_cfa_offset 24
+; CHECK-NEXT: pushq %rax
+; CHECK-NEXT: .cfi_def_cfa_offset 32
+; CHECK-NEXT: .cfi_offset %rbx, -24
+; CHECK-NEXT: .cfi_offset %rbp, -16
+; CHECK-NEXT: movl %esi, %ebx
+; CHECK-NEXT: movl %edi, %ebp
+; CHECK-NEXT: andl $15, %ebx
+; CHECK-NEXT: movl %ebx, %edi
+; CHECK-NEXT: callq external_use at PLT
+; CHECK-NEXT: btl %ebx, %ebp
+; CHECK-NEXT: setb %al
+; CHECK-NEXT: addq $8, %rsp
+; CHECK-NEXT: .cfi_def_cfa_offset 24
+; CHECK-NEXT: popq %rbx
+; CHECK-NEXT: .cfi_def_cfa_offset 16
+; CHECK-NEXT: popq %rbp
+; CHECK-NEXT: .cfi_def_cfa_offset 8
+; CHECK-NEXT: retq
+entry:
+ %and = and i32 %b, 15
+ %call = tail call i32 @external_use(i32 noundef %and) #3
+ %shl = shl nuw nsw i32 1, %and
+ %and1 = and i32 %shl, %a
+ %tobool = icmp ne i32 %and1, 0
+ ret i1 %tobool
+}
+
+declare i32 @external_use(i32 noundef) local_unnamed_addr #2
+
+attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable }
+attributes #1 = { nounwind uwtable }
+attributes #2 = { "no-trapping-math"="true" }
+attributes #3 = { nounwind }
>From 820fe566119c815df68ef470ca7cd1043da6af5b Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Sun, 30 Aug 2026 15:16:13 -0400
Subject: [PATCH 2/2] [X86] Use 16-bit BT instruction when index is masked by
0xF
---
llvm/lib/Target/X86/X86ISelLowering.cpp | 26 ++++++++++++++++++++++---
llvm/lib/Target/X86/X86InstrCompiler.td | 3 +++
llvm/test/CodeGen/X86/bittest-16.ll | 9 +++------
3 files changed, 29 insertions(+), 9 deletions(-)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index c8a38aa1bb154..ac3497e2aa270 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -57511,10 +57511,10 @@ static SDValue combineBTToBitOpFlag(SDNode *N, SelectionDAG &DAG) {
EVT VT = Src.getValueType();
SDLoc DL(N);
- // X86ISD::BT is only emitted for i32/i64 (smaller widths are promoted in
+ // X86ISD::BT is only emitted for i16/i32/i64 (smaller widths are promoted in
// getBT before the node is created).
- assert((VT == MVT::i32 || VT == MVT::i64) &&
- "X86ISD::BT is only emitted for i32/i64");
+ assert((VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) &&
+ "X86ISD::BT is only emitted for i16/i32/i64");
unsigned BW = VT.getScalarSizeInBits();
SDValue PeeledBitNo = peekThroughBitPosExtTrunc(BitNo, BW);
@@ -57576,6 +57576,26 @@ static SDValue combineBT(SDNode *N, SelectionDAG &DAG,
return SDValue(N, 0);
}
+ // If the bit index is masked by 0xF, we can use a 16-bit BT instruction to
+ // eliminate the masking operation. The 16-bit instruction has a size penalty
+ // (0x66 prefix), but it's worth it if we can drop an explicit AND
+ // instruction.
+ if (BitWidth == 32 || BitWidth == 64) {
+ if (N1.getOpcode() == ISD::AND && N1.hasOneUse()) {
+ if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
+ uint64_t Mask = C->getZExtValue();
+ if ((Mask & (BitWidth - 1)) == 15) {
+ SDLoc DL(N);
+ SDValue Src =
+ DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, N->getOperand(0));
+ SDValue BitNo =
+ DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, N1.getOperand(0));
+ return DAG.getNode(X86ISD::BT, DL, MVT::i32, Src, BitNo);
+ }
+ }
+ }
+ }
+
if (SDValue V = combineBTToBitOpFlag(N, DAG))
return V;
diff --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td
index 40ad1eb9b72cb..5c1146c5e2130 100644
--- a/llvm/lib/Target/X86/X86InstrCompiler.td
+++ b/llvm/lib/Target/X86/X86InstrCompiler.td
@@ -2121,6 +2121,9 @@ defm : OneBitPats<GR64, i64, BTR64rr, BTS64rr, BTC64rr, shiftMask64>;
// since getBT promotes <32-bit Src to i32 before creating X86ISD::BT. Unlike
// the OneBitPats above, the bit index here is already a same-width register
// (combineBTToBitOpFlag emits it that way), so no GR8 sub-reg insertion.
+def : Pat<(X86btr GR16:$src1, GR16:$src2), (BTR16rr GR16:$src1, GR16:$src2)>;
+def : Pat<(X86bts GR16:$src1, GR16:$src2), (BTS16rr GR16:$src1, GR16:$src2)>;
+def : Pat<(X86btc GR16:$src1, GR16:$src2), (BTC16rr GR16:$src1, GR16:$src2)>;
def : Pat<(X86btr GR32:$src1, GR32:$src2), (BTR32rr GR32:$src1, GR32:$src2)>;
def : Pat<(X86bts GR32:$src1, GR32:$src2), (BTS32rr GR32:$src1, GR32:$src2)>;
def : Pat<(X86btc GR32:$src1, GR32:$src2), (BTC32rr GR32:$src1, GR32:$src2)>;
diff --git a/llvm/test/CodeGen/X86/bittest-16.ll b/llvm/test/CodeGen/X86/bittest-16.ll
index 124c62ce1ff2b..1dd19672190b3 100644
--- a/llvm/test/CodeGen/X86/bittest-16.ll
+++ b/llvm/test/CodeGen/X86/bittest-16.ll
@@ -9,8 +9,7 @@ target triple = "x86_64-unknown-linux-gnu"
define dso_local zeroext i1 @bt_test_16_16(i16 noundef zeroext %a, i16 noundef zeroext %b) local_unnamed_addr #0 {
; CHECK-LABEL: bt_test_16_16:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: andl $15, %esi
-; CHECK-NEXT: btl %esi, %edi
+; CHECK-NEXT: btw %si, %di
; CHECK-NEXT: setb %al
; CHECK-NEXT: retq
entry:
@@ -27,8 +26,7 @@ entry:
define dso_local zeroext i1 @bt_test_16_32(i32 noundef %a, i32 noundef %b) local_unnamed_addr #0 {
; CHECK-LABEL: bt_test_16_32:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: andl $15, %esi
-; CHECK-NEXT: btl %esi, %edi
+; CHECK-NEXT: btw %si, %di
; CHECK-NEXT: setb %al
; CHECK-NEXT: retq
entry:
@@ -43,8 +41,7 @@ entry:
define dso_local zeroext i1 @bt_test_16_64(i64 noundef %a, i64 noundef %b) local_unnamed_addr #0 {
; CHECK-LABEL: bt_test_16_64:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: andl $15, %esi
-; CHECK-NEXT: btl %esi, %edi
+; CHECK-NEXT: btw %si, %di
; CHECK-NEXT: setb %al
; CHECK-NEXT: retq
entry:
More information about the llvm-commits
mailing list