[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:01:59 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: AZero13 (AZero13)

<details>
<summary>Changes</summary>

With 0xF, we can use 16-bit bit-test instead of and and bit-test.

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


3 Files Affected:

- (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+23-3) 
- (modified) llvm/lib/Target/X86/X86InstrCompiler.td (+3) 
- (added) llvm/test/CodeGen/X86/bittest-16.ll (+107) 


``````````diff
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
new file mode 100644
index 0000000000000..70bca01539d09
--- /dev/null
+++ b/llvm/test/CodeGen/X86/bittest-16.ll
@@ -0,0 +1,107 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown | FileCheck %s
+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:    btw %si, %di
+; 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:    btw %si, %di
+; 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:    btw %si, %di
+; 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 "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+attributes #2 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+attributes #3 = { nounwind }
+
+!llvm.module.flags = !{!0, !1, !2}
+!llvm.ident = !{!3}
+!llvm.errno.tbaa = !{!4}
+
+!0 = !{i32 8, !"PIC Level", i32 2}
+!1 = !{i32 7, !"PIE Level", i32 2}
+!2 = !{i32 7, !"uwtable", i32 2}
+!3 = !{!"clang version 24.0.0git (https://github.com/AZero13/llvm-project.git 2b2524021e1b3594919dd82e60a7f16e221bac6f)"}
+!4 = !{!5, !6, i64 0}
+!5 = !{!"__libc_errno", !6, i64 0}
+!6 = !{!"int", !7, i64 0}
+!7 = !{!"omnipotent char", !8, i64 0}
+!8 = !{!"Simple C/C++ TBAA"}

``````````

</details>


https://github.com/llvm/llvm-project/pull/219836


More information about the llvm-commits mailing list