[llvm] [X86] Merge BT with a matching BTR/BTS/BTC (PR #193612)

Paweł Bylica via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 10:30:26 PDT 2026


https://github.com/chfast updated https://github.com/llvm/llvm-project/pull/193612

>From e408f05e382232740ba946c7a2d24b231f9c7230 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Thu, 7 May 2026 19:17:06 +0200
Subject: [PATCH 1/9] [X86][test] Pre-commit test for combineBT with mismatched
 bit positions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Pre-commit test for the upcoming BT-fuse-with-BTR/BTS/BTC combine: when
Src is shared between BT and a sibling bit-modify but the bit positions
differ, the combine must NOT fuse them. CHECKs are stable across the
upcoming impl change (the fold doesn't fire here either way) — landing
the test first locks in the boundary.
---
 .../CodeGen/X86/bt-merge-mismatched-bitpos.ll | 67 +++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/bt-merge-mismatched-bitpos.ll

diff --git a/llvm/test/CodeGen/X86/bt-merge-mismatched-bitpos.ll b/llvm/test/CodeGen/X86/bt-merge-mismatched-bitpos.ll
new file mode 100644
index 0000000000000..4936167ed6349
--- /dev/null
+++ b/llvm/test/CodeGen/X86/bt-merge-mismatched-bitpos.ll
@@ -0,0 +1,67 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=x86_64-- | FileCheck %s
+
+; combineBT only fuses BT with BTR/BTS/BTC when the bit positions match
+; (modulo trunc/zext/any-ext/and-with-mask). When the bit positions differ,
+; both instructions must remain in the output.
+
+define i1 @no_fuse_diff_pos_xor(ptr %word, i32 %a, i32 %b) nounwind {
+; CHECK-LABEL: no_fuse_diff_pos_xor:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl (%rdi), %eax
+; CHECK-NEXT:    movl %eax, %ecx
+; CHECK-NEXT:    btcl %edx, %ecx
+; CHECK-NEXT:    btl %esi, %eax
+; CHECK-NEXT:    setb %al
+; CHECK-NEXT:    movl %ecx, (%rdi)
+; CHECK-NEXT:    retq
+  %bita = shl i32 1, %a
+  %bitb = shl i32 1, %b
+  %ld = load i32, ptr %word
+  %res = xor i32 %ld, %bitb
+  %test = and i32 %ld, %bita
+  %cmp = icmp ne i32 %test, 0
+  store i32 %res, ptr %word
+  ret i1 %cmp
+}
+
+define i1 @no_fuse_diff_pos_or(ptr %word, i64 %a, i64 %b) nounwind {
+; CHECK-LABEL: no_fuse_diff_pos_or:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq (%rdi), %rax
+; CHECK-NEXT:    movq %rax, %rcx
+; CHECK-NEXT:    btsq %rdx, %rcx
+; CHECK-NEXT:    btq %rsi, %rax
+; CHECK-NEXT:    setb %al
+; CHECK-NEXT:    movq %rcx, (%rdi)
+; CHECK-NEXT:    retq
+  %bita = shl i64 1, %a
+  %bitb = shl i64 1, %b
+  %ld = load i64, ptr %word
+  %res = or i64 %ld, %bitb
+  %test = and i64 %ld, %bita
+  %cmp = icmp ne i64 %test, 0
+  store i64 %res, ptr %word
+  ret i1 %cmp
+}
+
+define i1 @no_fuse_diff_pos_and(ptr %word, i64 %a, i64 %b) nounwind {
+; CHECK-LABEL: no_fuse_diff_pos_and:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq (%rdi), %rax
+; CHECK-NEXT:    movq %rax, %rcx
+; CHECK-NEXT:    btrq %rdx, %rcx
+; CHECK-NEXT:    btq %rsi, %rax
+; CHECK-NEXT:    setae %al
+; CHECK-NEXT:    movq %rcx, (%rdi)
+; CHECK-NEXT:    retq
+  %bita = shl i64 1, %a
+  %bitb = shl i64 1, %b
+  %notb = xor i64 %bitb, -1
+  %ld = load i64, ptr %word
+  %res = and i64 %ld, %notb
+  %test = and i64 %ld, %bita
+  %cmp = icmp eq i64 %test, 0
+  store i64 %res, ptr %word
+  ret i1 %cmp
+}

>From 83af1d1b87aba98f349e376bcb9bc162a3c7e213 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Thu, 7 May 2026 19:17:07 +0200
Subject: [PATCH 2/9] [X86][test] Pre-commit test for combineBT BT/BTR-BTS-BTC
 fuse

Pre-commit test for the upcoming BT-fuse-with-BTR/BTS/BTC combine. The
CHECKs here show the redundant bt instruction the upcoming combine will
remove (paired with btc/bts/btr); the next commit updates them.

Three peek-through paths are covered:
- (and %p, BW-1) only on the modify side
- (and %p, BW-1) only on the BT side
- ZERO_EXTEND wrapper on the bit position
---
 llvm/test/CodeGen/X86/bt-merge-fuse.ll | 72 ++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/bt-merge-fuse.ll

diff --git a/llvm/test/CodeGen/X86/bt-merge-fuse.ll b/llvm/test/CodeGen/X86/bt-merge-fuse.ll
new file mode 100644
index 0000000000000..ea23221940ee7
--- /dev/null
+++ b/llvm/test/CodeGen/X86/bt-merge-fuse.ll
@@ -0,0 +1,72 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=x86_64-- | FileCheck %s
+
+; Pin the peek-through paths in combineBTToBitOpFlag: the bit positions of
+; BT and the bit-modify can differ by an (and x, BW-1) mask or by trunc/zext
+; wrappers, and the combine should still fuse them into a single BTR/BTS/BTC.
+
+; (and x, 31) on the modify side, bare on the BT side.
+define i1 @fuse_and_mask_on_modify(ptr %word, i32 %position) nounwind {
+; CHECK-LABEL: fuse_and_mask_on_modify:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl (%rdi), %eax
+; CHECK-NEXT:    movl %eax, %ecx
+; CHECK-NEXT:    btcl %esi, %ecx
+; CHECK-NEXT:    btl %esi, %eax
+; CHECK-NEXT:    setb %al
+; CHECK-NEXT:    movl %ecx, (%rdi)
+; CHECK-NEXT:    retq
+  %ofs = and i32 %position, 31
+  %bit = shl i32 1, %ofs
+  %ld = load i32, ptr %word
+  %res = xor i32 %ld, %bit
+  %test = and i32 %ld, %bit
+  %cmp = icmp ne i32 %test, 0
+  store i32 %res, ptr %word
+  ret i1 %cmp
+}
+
+; (and x, 63) on the BT side, bare on the modify side.
+define i1 @fuse_and_mask_on_bt(ptr %word, i64 %position) nounwind {
+; CHECK-LABEL: fuse_and_mask_on_bt:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq (%rdi), %rax
+; CHECK-NEXT:    movq %rax, %rcx
+; CHECK-NEXT:    btsq %rsi, %rcx
+; CHECK-NEXT:    btq %rsi, %rax
+; CHECK-NEXT:    setb %al
+; CHECK-NEXT:    movq %rcx, (%rdi)
+; CHECK-NEXT:    retq
+  %bit = shl i64 1, %position
+  %ld = load i64, ptr %word
+  %res = or i64 %ld, %bit
+  %ofs = and i64 %position, 63
+  %tbit = shl i64 1, %ofs
+  %test = and i64 %ld, %tbit
+  %cmp = icmp ne i64 %test, 0
+  store i64 %res, ptr %word
+  ret i1 %cmp
+}
+
+; ZERO_EXTEND wrapper on the bit position.
+define i1 @fuse_zext_pos(ptr %word, i32 %position) nounwind {
+; CHECK-LABEL: fuse_zext_pos:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    # kill: def $esi killed $esi def $rsi
+; CHECK-NEXT:    movq (%rdi), %rax
+; CHECK-NEXT:    movq %rax, %rcx
+; CHECK-NEXT:    btrq %rsi, %rcx
+; CHECK-NEXT:    btq %rsi, %rax
+; CHECK-NEXT:    setae %al
+; CHECK-NEXT:    movq %rcx, (%rdi)
+; CHECK-NEXT:    retq
+  %zext = zext i32 %position to i64
+  %bit = shl i64 1, %zext
+  %mask = xor i64 %bit, -1
+  %ld = load i64, ptr %word
+  %res = and i64 %ld, %mask
+  %test = and i64 %ld, %bit
+  %cmp = icmp eq i64 %test, 0
+  store i64 %res, ptr %word
+  ret i1 %cmp
+}

>From 11f9860b462869485a8f5c6df03592dd1ffc17e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Thu, 23 Apr 2026 00:22:07 +0200
Subject: [PATCH 3/9] [X86] Merge BT with a matching BTR/BTS/BTC

BTR/BTS/BTC set CF from the pre-operation bit value, so a subsequent BT
on the same source and bit index produces redundant EFLAGS. We were
emitting both, e.g. for `(and ld, ~bit) | (and ld, bit) != 0` we got:

    btrl %esi, %ecx
    btl  %esi, %eax
    setae %al

Introduce three flag-producing DAG nodes X86ISD::BTR/BTS/BTC that model
the register-register BTR/BTS/BTC as `(res, EFLAGS) = op src, bitno`
(the atomic locked variants live under X86ISD::LBTR/LBTS/LBTC already),
pattern-match them to the existing BTR/BTS/BTC encodings, and add a DAG
combine in combineBT that fuses an X86ISD::BT with a sibling
AND(Src, rotl -2, X) / OR(Src, shl 1, X) / XOR(Src, shl 1, X) on the
same source into a single flag-producing node. The bit-position operand
can differ from BT's by trunc/zext/and-with-mask that preserves the low
log2(BW) bits (BT already masks those implicitly), so peek through
those wrappers when matching.

After the fix the example lowers to:

    btrl %esi, %ecx
    setae %al

Fixes llvm#165291.
---
 llvm/lib/Target/X86/X86ISelLowering.cpp      | 108 +++++++
 llvm/lib/Target/X86/X86InstrCompiler.td      |  17 ++
 llvm/lib/Target/X86/X86InstrFragments.td     |   8 +
 llvm/test/CodeGen/X86/bittest-big-integer.ll | 306 +++++++------------
 llvm/test/CodeGen/X86/bt-merge-fuse.ll       |  12 +-
 5 files changed, 250 insertions(+), 201 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 5a1171b2b4ee6..c3c9eb7bfd2c4 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -56877,6 +56877,111 @@ static SDValue combineAndnp(SDNode *N, SelectionDAG &DAG,
   return SDValue();
 }
 
+// Strip TRUNCATE/ZERO_EXTEND/ANY_EXTEND wrappers and `and x, C` where C
+// preserves the low log2(BW) bits — these are transparent to BT/BTR/BTS/BTC,
+// which implicitly mask the bit index to log2(BW) bits.
+static SDValue peekThroughBitPosExtTrunc(SDValue V, unsigned BW) {
+  APInt LowBits =
+      APInt::getLowBitsSet(V.getScalarValueSizeInBits(), Log2_32(BW));
+  for (;;) {
+    unsigned Op = V.getOpcode();
+    if (Op == ISD::TRUNCATE || Op == ISD::ZERO_EXTEND ||
+        Op == ISD::ANY_EXTEND) {
+      V = V.getOperand(0);
+      LowBits = LowBits.zextOrTrunc(V.getScalarValueSizeInBits());
+      continue;
+    }
+    if (Op == ISD::AND) {
+      auto *C = dyn_cast<ConstantSDNode>(V.getOperand(1));
+      if (C && LowBits.isSubsetOf(C->getAPIntValue())) {
+        V = V.getOperand(0);
+        continue;
+      }
+    }
+    return V;
+  }
+}
+
+// Try to merge a (X86ISD::BT Src, BitNo) with a sibling bit-modifying op on
+// Src — AND(Src, rotl -2, X), OR(Src, shl 1, X), XOR(Src, shl 1, X) — into a
+// single flag-producing X86ISD::{BTR,BTS,BTC} node. Both BT and BTR/BTS/BTC
+// set CF from the pre-op bit value, so one instruction subsumes the other.
+// Fixes llvm#165291.
+static SDValue combineBTToBitOpFlag(SDNode *N, SelectionDAG &DAG) {
+  SDValue Src = N->getOperand(0);
+  SDValue BitNo = N->getOperand(1);
+  EVT VT = Src.getValueType();
+  SDLoc DL(N);
+
+  // BT is only emitted for legal integer widths (16/32/64); match those.
+  if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
+    return SDValue();
+
+  unsigned BW = VT.getScalarSizeInBits();
+  SDValue PeeledBitNo = peekThroughBitPosExtTrunc(BitNo, BW);
+
+  for (SDNode *User : Src->users()) {
+    if (User == N)
+      continue;
+    unsigned UOpc = User->getOpcode();
+    if (UOpc != ISD::AND && UOpc != ISD::OR && UOpc != ISD::XOR)
+      continue;
+    if (User->getValueType(0) != VT)
+      continue;
+
+    // Identify which operand of User is Src; the other is the mask.
+    SDValue UOp0 = User->getOperand(0);
+    SDValue UOp1 = User->getOperand(1);
+    SDValue Mask;
+    if (UOp0 == SDValue(Src.getNode(), Src.getResNo()))
+      Mask = UOp1;
+    else if (UOp1 == SDValue(Src.getNode(), Src.getResNo()))
+      Mask = UOp0;
+    else
+      continue;
+    // We will replace the mask's consumer (User); require the mask to have no
+    // other live uses so we can drop it.
+    if (!Mask.hasOneUse())
+      continue;
+
+    unsigned FlagOp = 0;
+    SDValue ShAmt;
+    if (UOpc == ISD::AND && Mask.getOpcode() == ISD::ROTL) {
+      // (and Src, (rotl -2, X)) — clears bit X.
+      if (auto *C = dyn_cast<ConstantSDNode>(Mask.getOperand(0)))
+        if (C->getAPIntValue() == APInt::getAllOnes(BW) - 1) {
+          FlagOp = X86ISD::BTR;
+          ShAmt = Mask.getOperand(1);
+        }
+    } else if ((UOpc == ISD::OR || UOpc == ISD::XOR) &&
+               Mask.getOpcode() == ISD::SHL) {
+      // (or/xor Src, (shl 1, X)) — sets / flips bit X.
+      if (auto *C = dyn_cast<ConstantSDNode>(Mask.getOperand(0)))
+        if (C->getAPIntValue() == 1) {
+          FlagOp = UOpc == ISD::OR ? X86ISD::BTS : X86ISD::BTC;
+          ShAmt = Mask.getOperand(1);
+        }
+    }
+    if (!FlagOp)
+      continue;
+
+    // The BT and the bit-op must address the same bit. They can differ only
+    // by truncation/extension or an AND that preserves the low log2(BW) bits.
+    if (peekThroughBitPosExtTrunc(ShAmt, BW) != PeeledBitNo)
+      continue;
+
+    // BTR/BTS/BTC *rr take the bit index in a register of the same width as
+    // the source. Extend or truncate to VT to match the instruction signature.
+    SDValue BN = DAG.getZExtOrTrunc(BitNo, DL, VT);
+    SDValue New = DAG.getNode(FlagOp, DL, DAG.getVTList(VT, MVT::i32), Src, BN);
+    // Reroute the value output through User's consumers.
+    DAG.ReplaceAllUsesOfValueWith(SDValue(User, 0), New.getValue(0));
+    // Return the flags output so combineBT installs it as N's replacement.
+    return New.getValue(1);
+  }
+  return SDValue();
+}
+
 static SDValue combineBT(SDNode *N, SelectionDAG &DAG,
                          TargetLowering::DAGCombinerInfo &DCI) {
   SDValue N1 = N->getOperand(1);
@@ -56890,6 +56995,9 @@ static SDValue combineBT(SDNode *N, SelectionDAG &DAG,
     return SDValue(N, 0);
   }
 
+  if (SDValue V = combineBTToBitOpFlag(N, DAG))
+    return V;
+
   return SDValue();
 }
 
diff --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td
index ebbfa48d2660c..735bc57e8eaba 100644
--- a/llvm/lib/Target/X86/X86InstrCompiler.td
+++ b/llvm/lib/Target/X86/X86InstrCompiler.td
@@ -2072,6 +2072,23 @@ defm : OneBitPats<GR16, i16, BTR16rr, BTS16rr, BTC16rr, shiftMask16>;
 defm : OneBitPats<GR32, i32, BTR32rr, BTS32rr, BTC32rr, shiftMask32>;
 defm : OneBitPats<GR64, i64, BTR64rr, BTS64rr, BTC64rr, shiftMask64>;
 
+// Flag-producing variants: reuse the BTR/BTS/BTC encodings so CF can replace
+// a separate X86ISD::BT. Emitted by a DAG combine in combineBT.
+multiclass OneBitFlagPats<RegisterClass rc, Instruction btr, Instruction bts,
+                          Instruction btc, SDNode btr_flag, SDNode bts_flag,
+                          SDNode btc_flag> {
+  def : Pat<(btr_flag rc:$src1, rc:$src2), (btr rc:$src1, rc:$src2)>;
+  def : Pat<(bts_flag rc:$src1, rc:$src2), (bts rc:$src1, rc:$src2)>;
+  def : Pat<(btc_flag rc:$src1, rc:$src2), (btc rc:$src1, rc:$src2)>;
+}
+
+defm : OneBitFlagPats<GR16, BTR16rr, BTS16rr, BTC16rr,
+                      X86btr_flag, X86bts_flag, X86btc_flag>;
+defm : OneBitFlagPats<GR32, BTR32rr, BTS32rr, BTC32rr,
+                      X86btr_flag, X86bts_flag, X86btc_flag>;
+defm : OneBitFlagPats<GR64, BTR64rr, BTS64rr, BTC64rr,
+                      X86btr_flag, X86bts_flag, X86btc_flag>;
+
 //===----------------------------------------------------------------------===//
 // EFLAGS-defining Patterns
 //===----------------------------------------------------------------------===//
diff --git a/llvm/lib/Target/X86/X86InstrFragments.td b/llvm/lib/Target/X86/X86InstrFragments.td
index bae1211e51330..e096aa1d074fa 100644
--- a/llvm/lib/Target/X86/X86InstrFragments.td
+++ b/llvm/lib/Target/X86/X86InstrFragments.td
@@ -165,6 +165,14 @@ let IsStrictFP = true in {
 // X86 bit-test instructions.
 def X86bt      : SDNode<"X86ISD::BT",       SDTX86CmpTest>;
 
+// X86 bit-test-and-modify instructions: res, EFLAGS = op src, bitno.
+// CF is set from the pre-operation bit value; BT on the same operands is
+// therefore redundant after one of these nodes. The atomic (locked) variants
+// live under X86ISD::LBTR/LBTS/LBTC.
+def X86btr_flag : SDNode<"X86ISD::BTR", SDTBinaryArithWithFlags>;
+def X86bts_flag : SDNode<"X86ISD::BTS", SDTBinaryArithWithFlags>;
+def X86btc_flag : SDNode<"X86ISD::BTC", SDTBinaryArithWithFlags>;
+
 // Conditional compare instructions
 def X86ccmp    : SDNode<"X86ISD::CCMP",     SDTX86Ccmp>;
 def X86ctest   : SDNode<"X86ISD::CTEST",    SDTX86Ccmp>;
diff --git a/llvm/test/CodeGen/X86/bittest-big-integer.ll b/llvm/test/CodeGen/X86/bittest-big-integer.ll
index 96ccc7b0f7527..6767cc45a2c5e 100644
--- a/llvm/test/CodeGen/X86/bittest-big-integer.ll
+++ b/llvm/test/CodeGen/X86/bittest-big-integer.ll
@@ -38,24 +38,18 @@ define i1 @test_eq_i32(ptr %word, i32 %position) nounwind {
 define i1 @complement_ne_i32(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: complement_ne_i32:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %esi
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
 ; X86-NEXT:    movl (%ecx), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    btcl %eax, %esi
-; X86-NEXT:    btl %eax, %edx
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    btcl %eax, %edx
 ; X86-NEXT:    setb %al
-; X86-NEXT:    movl %esi, (%ecx)
-; X86-NEXT:    popl %esi
+; X86-NEXT:    movl %edx, (%ecx)
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: complement_ne_i32:
 ; X64:       # %bb.0:
-; X64-NEXT:    movl (%rdi), %eax
-; X64-NEXT:    movl %eax, %ecx
+; X64-NEXT:    movl (%rdi), %ecx
 ; X64-NEXT:    btcl %esi, %ecx
-; X64-NEXT:    btl %esi, %eax
 ; X64-NEXT:    setb %al
 ; X64-NEXT:    movl %ecx, (%rdi)
 ; X64-NEXT:    retq
@@ -72,24 +66,18 @@ define i1 @complement_ne_i32(ptr %word, i32 %position) nounwind {
 define i1 @reset_eq_i32(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: reset_eq_i32:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %esi
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
 ; X86-NEXT:    movl (%ecx), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    btrl %eax, %esi
-; X86-NEXT:    btl %eax, %edx
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    btrl %eax, %edx
 ; X86-NEXT:    setae %al
-; X86-NEXT:    movl %esi, (%ecx)
-; X86-NEXT:    popl %esi
+; X86-NEXT:    movl %edx, (%ecx)
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: reset_eq_i32:
 ; X64:       # %bb.0:
-; X64-NEXT:    movl (%rdi), %eax
-; X64-NEXT:    movl %eax, %ecx
+; X64-NEXT:    movl (%rdi), %ecx
 ; X64-NEXT:    btrl %esi, %ecx
-; X64-NEXT:    btl %esi, %eax
 ; X64-NEXT:    setae %al
 ; X64-NEXT:    movl %ecx, (%rdi)
 ; X64-NEXT:    retq
@@ -107,24 +95,18 @@ define i1 @reset_eq_i32(ptr %word, i32 %position) nounwind {
 define i1 @set_ne_i32(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: set_ne_i32:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %esi
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
 ; X86-NEXT:    movl (%ecx), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    btsl %eax, %esi
-; X86-NEXT:    btl %eax, %edx
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    btsl %eax, %edx
 ; X86-NEXT:    setb %al
-; X86-NEXT:    movl %esi, (%ecx)
-; X86-NEXT:    popl %esi
+; X86-NEXT:    movl %edx, (%ecx)
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: set_ne_i32:
 ; X64:       # %bb.0:
-; X64-NEXT:    movl (%rdi), %eax
-; X64-NEXT:    movl %eax, %ecx
+; X64-NEXT:    movl (%rdi), %ecx
 ; X64-NEXT:    btsl %esi, %ecx
-; X64-NEXT:    btl %esi, %eax
 ; X64-NEXT:    setb %al
 ; X64-NEXT:    movl %ecx, (%rdi)
 ; X64-NEXT:    retq
@@ -145,14 +127,12 @@ define i1 @init_eq_i32(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movzbl {{[0-9]+}}(%esp), %eax
-; X86-NEXT:    shll %cl, %eax
-; X86-NEXT:    movl (%edx), %esi
-; X86-NEXT:    movl %esi, %edi
+; X86-NEXT:    movzbl {{[0-9]+}}(%esp), %esi
+; X86-NEXT:    shll %cl, %esi
+; X86-NEXT:    movl (%edx), %edi
 ; X86-NEXT:    btrl %ecx, %edi
-; X86-NEXT:    orl %eax, %edi
-; X86-NEXT:    btl %ecx, %esi
 ; X86-NEXT:    setae %al
+; X86-NEXT:    orl %esi, %edi
 ; X86-NEXT:    movl %edi, (%edx)
 ; X86-NEXT:    popl %esi
 ; X86-NEXT:    popl %edi
@@ -162,24 +142,20 @@ define i1 @init_eq_i32(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; SSE:       # %bb.0:
 ; SSE-NEXT:    movl %esi, %ecx
 ; SSE-NEXT:    shll %cl, %edx
-; SSE-NEXT:    movl (%rdi), %eax
-; SSE-NEXT:    movl %eax, %esi
+; SSE-NEXT:    movl (%rdi), %esi
 ; SSE-NEXT:    btrl %ecx, %esi
-; SSE-NEXT:    orl %edx, %esi
-; SSE-NEXT:    btl %ecx, %eax
 ; SSE-NEXT:    setae %al
+; SSE-NEXT:    orl %edx, %esi
 ; SSE-NEXT:    movl %esi, (%rdi)
 ; SSE-NEXT:    retq
 ;
 ; AVX-LABEL: init_eq_i32:
 ; AVX:       # %bb.0:
-; AVX-NEXT:    shlxl %esi, %edx, %eax
-; AVX-NEXT:    movl (%rdi), %ecx
-; AVX-NEXT:    movl %ecx, %edx
+; AVX-NEXT:    shlxl %esi, %edx, %ecx
+; AVX-NEXT:    movl (%rdi), %edx
 ; AVX-NEXT:    btrl %esi, %edx
-; AVX-NEXT:    orl %eax, %edx
-; AVX-NEXT:    btl %esi, %ecx
 ; AVX-NEXT:    setae %al
+; AVX-NEXT:    orl %ecx, %edx
 ; AVX-NEXT:    movl %edx, (%rdi)
 ; AVX-NEXT:    retq
   %ofs = and i32 %position, 31
@@ -232,29 +208,24 @@ define i1 @test_ne_i64(ptr %word, i32 %position) nounwind {
 define i1 @complement_ne_i64(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: complement_ne_i64:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    andl $32, %esi
-; X86-NEXT:    shrl $3, %esi
-; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    btl %edx, %edi
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    andl $32, %edx
+; X86-NEXT:    shrl $3, %edx
+; X86-NEXT:    movl (%ecx,%edx), %esi
+; X86-NEXT:    btcl %eax, %esi
 ; X86-NEXT:    setb %al
-; X86-NEXT:    btcl %edx, %edi
-; X86-NEXT:    movl %edi, (%ecx,%esi)
+; X86-NEXT:    movl %esi, (%ecx,%edx)
 ; X86-NEXT:    popl %esi
-; X86-NEXT:    popl %edi
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: complement_ne_i64:
 ; X64:       # %bb.0:
 ; X64-NEXT:    # kill: def $esi killed $esi def $rsi
-; X64-NEXT:    movq (%rdi), %rax
-; X64-NEXT:    movq %rax, %rcx
+; X64-NEXT:    movq (%rdi), %rcx
 ; X64-NEXT:    btcq %rsi, %rcx
-; X64-NEXT:    btq %rsi, %rax
 ; X64-NEXT:    setb %al
 ; X64-NEXT:    movq %rcx, (%rdi)
 ; X64-NEXT:    retq
@@ -272,29 +243,24 @@ define i1 @complement_ne_i64(ptr %word, i32 %position) nounwind {
 define i1 @reset_eq_i64(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: reset_eq_i64:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    andl $32, %esi
-; X86-NEXT:    shrl $3, %esi
-; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    btl %edx, %edi
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    andl $32, %edx
+; X86-NEXT:    shrl $3, %edx
+; X86-NEXT:    movl (%ecx,%edx), %esi
+; X86-NEXT:    btrl %eax, %esi
 ; X86-NEXT:    setae %al
-; X86-NEXT:    btrl %edx, %edi
-; X86-NEXT:    movl %edi, (%ecx,%esi)
+; X86-NEXT:    movl %esi, (%ecx,%edx)
 ; X86-NEXT:    popl %esi
-; X86-NEXT:    popl %edi
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: reset_eq_i64:
 ; X64:       # %bb.0:
 ; X64-NEXT:    # kill: def $esi killed $esi def $rsi
-; X64-NEXT:    movq (%rdi), %rax
-; X64-NEXT:    movq %rax, %rcx
+; X64-NEXT:    movq (%rdi), %rcx
 ; X64-NEXT:    btrq %rsi, %rcx
-; X64-NEXT:    btq %rsi, %rax
 ; X64-NEXT:    setae %al
 ; X64-NEXT:    movq %rcx, (%rdi)
 ; X64-NEXT:    retq
@@ -313,29 +279,24 @@ define i1 @reset_eq_i64(ptr %word, i32 %position) nounwind {
 define i1 @set_ne_i64(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: set_ne_i64:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    andl $32, %esi
-; X86-NEXT:    shrl $3, %esi
-; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    btl %edx, %edi
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    andl $32, %edx
+; X86-NEXT:    shrl $3, %edx
+; X86-NEXT:    movl (%ecx,%edx), %esi
+; X86-NEXT:    btsl %eax, %esi
 ; X86-NEXT:    setb %al
-; X86-NEXT:    btsl %edx, %edi
-; X86-NEXT:    movl %edi, (%ecx,%esi)
+; X86-NEXT:    movl %esi, (%ecx,%edx)
 ; X86-NEXT:    popl %esi
-; X86-NEXT:    popl %edi
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: set_ne_i64:
 ; X64:       # %bb.0:
 ; X64-NEXT:    # kill: def $esi killed $esi def $rsi
-; X64-NEXT:    movq (%rdi), %rax
-; X64-NEXT:    movq %rax, %rcx
+; X64-NEXT:    movq (%rdi), %rcx
 ; X64-NEXT:    btsq %rsi, %rcx
-; X64-NEXT:    btq %rsi, %rax
 ; X64-NEXT:    setb %al
 ; X64-NEXT:    movq %rcx, (%rdi)
 ; X64-NEXT:    retq
@@ -362,9 +323,8 @@ define i1 @init_eq_i64(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; X86-NEXT:    andl $32, %esi
 ; X86-NEXT:    shrl $3, %esi
 ; X86-NEXT:    movl (%edx,%esi), %edi
-; X86-NEXT:    btl %ecx, %edi
-; X86-NEXT:    setae %al
 ; X86-NEXT:    btrl %ecx, %edi
+; X86-NEXT:    setae %al
 ; X86-NEXT:    movzbl {{[0-9]+}}(%esp), %ebx
 ; X86-NEXT:    # kill: def $cl killed $cl killed $ecx
 ; X86-NEXT:    shll %cl, %ebx
@@ -378,14 +338,12 @@ define i1 @init_eq_i64(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; SSE-LABEL: init_eq_i64:
 ; SSE:       # %bb.0:
 ; SSE-NEXT:    movl %esi, %ecx
-; SSE-NEXT:    movl %edx, %eax
-; SSE-NEXT:    shlq %cl, %rax
-; SSE-NEXT:    movq (%rdi), %rdx
-; SSE-NEXT:    movq %rdx, %rsi
+; SSE-NEXT:    movl %edx, %edx
+; SSE-NEXT:    shlq %cl, %rdx
+; SSE-NEXT:    movq (%rdi), %rsi
 ; SSE-NEXT:    btrq %rcx, %rsi
-; SSE-NEXT:    orq %rax, %rsi
-; SSE-NEXT:    btq %rcx, %rdx
 ; SSE-NEXT:    setae %al
+; SSE-NEXT:    orq %rdx, %rsi
 ; SSE-NEXT:    movq %rsi, (%rdi)
 ; SSE-NEXT:    retq
 ;
@@ -393,13 +351,11 @@ define i1 @init_eq_i64(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; AVX:       # %bb.0:
 ; AVX-NEXT:    # kill: def $esi killed $esi def $rsi
 ; AVX-NEXT:    movl %edx, %eax
-; AVX-NEXT:    shlxq %rsi, %rax, %rax
-; AVX-NEXT:    movq (%rdi), %rcx
-; AVX-NEXT:    movq %rcx, %rdx
+; AVX-NEXT:    shlxq %rsi, %rax, %rcx
+; AVX-NEXT:    movq (%rdi), %rdx
 ; AVX-NEXT:    btrq %rsi, %rdx
-; AVX-NEXT:    orq %rax, %rdx
-; AVX-NEXT:    btq %rsi, %rcx
 ; AVX-NEXT:    setae %al
+; AVX-NEXT:    orq %rcx, %rdx
 ; AVX-NEXT:    movq %rdx, (%rdi)
 ; AVX-NEXT:    retq
   %rem = and i32 %position, 63
@@ -455,20 +411,17 @@ define i1 @test_ne_i128(ptr %word, i32 %position) nounwind {
 define i1 @complement_ne_i128(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: complement_ne_i128:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    andl $96, %esi
-; X86-NEXT:    shrl $3, %esi
-; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    btl %edx, %edi
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    andl $96, %edx
+; X86-NEXT:    shrl $3, %edx
+; X86-NEXT:    movl (%ecx,%edx), %esi
+; X86-NEXT:    btcl %eax, %esi
 ; X86-NEXT:    setb %al
-; X86-NEXT:    btcl %edx, %edi
-; X86-NEXT:    movl %edi, (%ecx,%esi)
+; X86-NEXT:    movl %esi, (%ecx,%edx)
 ; X86-NEXT:    popl %esi
-; X86-NEXT:    popl %edi
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: complement_ne_i128:
@@ -477,9 +430,8 @@ define i1 @complement_ne_i128(ptr %word, i32 %position) nounwind {
 ; X64-NEXT:    andl $96, %ecx
 ; X64-NEXT:    shrl $3, %ecx
 ; X64-NEXT:    movl (%rdi,%rcx), %edx
-; X64-NEXT:    btl %esi, %edx
-; X64-NEXT:    setb %al
 ; X64-NEXT:    btcl %esi, %edx
+; X64-NEXT:    setb %al
 ; X64-NEXT:    movl %edx, (%rdi,%rcx)
 ; X64-NEXT:    retq
   %rem = and i32 %position, 127
@@ -496,20 +448,17 @@ define i1 @complement_ne_i128(ptr %word, i32 %position) nounwind {
 define i1 @reset_eq_i128(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: reset_eq_i128:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    andl $96, %esi
-; X86-NEXT:    shrl $3, %esi
-; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    btl %edx, %edi
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    andl $96, %edx
+; X86-NEXT:    shrl $3, %edx
+; X86-NEXT:    movl (%ecx,%edx), %esi
+; X86-NEXT:    btrl %eax, %esi
 ; X86-NEXT:    setae %al
-; X86-NEXT:    btrl %edx, %edi
-; X86-NEXT:    movl %edi, (%ecx,%esi)
+; X86-NEXT:    movl %esi, (%ecx,%edx)
 ; X86-NEXT:    popl %esi
-; X86-NEXT:    popl %edi
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: reset_eq_i128:
@@ -518,9 +467,8 @@ define i1 @reset_eq_i128(ptr %word, i32 %position) nounwind {
 ; X64-NEXT:    andl $96, %ecx
 ; X64-NEXT:    shrl $3, %ecx
 ; X64-NEXT:    movl (%rdi,%rcx), %edx
-; X64-NEXT:    btl %esi, %edx
-; X64-NEXT:    setae %al
 ; X64-NEXT:    btrl %esi, %edx
+; X64-NEXT:    setae %al
 ; X64-NEXT:    movl %edx, (%rdi,%rcx)
 ; X64-NEXT:    retq
   %rem = and i32 %position, 127
@@ -538,20 +486,17 @@ define i1 @reset_eq_i128(ptr %word, i32 %position) nounwind {
 define i1 @set_ne_i128(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: set_ne_i128:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    andl $96, %esi
-; X86-NEXT:    shrl $3, %esi
-; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    btl %edx, %edi
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    andl $96, %edx
+; X86-NEXT:    shrl $3, %edx
+; X86-NEXT:    movl (%ecx,%edx), %esi
+; X86-NEXT:    btsl %eax, %esi
 ; X86-NEXT:    setb %al
-; X86-NEXT:    btsl %edx, %edi
-; X86-NEXT:    movl %edi, (%ecx,%esi)
+; X86-NEXT:    movl %esi, (%ecx,%edx)
 ; X86-NEXT:    popl %esi
-; X86-NEXT:    popl %edi
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: set_ne_i128:
@@ -560,9 +505,8 @@ define i1 @set_ne_i128(ptr %word, i32 %position) nounwind {
 ; X64-NEXT:    andl $96, %ecx
 ; X64-NEXT:    shrl $3, %ecx
 ; X64-NEXT:    movl (%rdi,%rcx), %edx
-; X64-NEXT:    btl %esi, %edx
-; X64-NEXT:    setb %al
 ; X64-NEXT:    btsl %esi, %edx
+; X64-NEXT:    setb %al
 ; X64-NEXT:    movl %edx, (%rdi,%rcx)
 ; X64-NEXT:    retq
   %rem = and i32 %position, 127
@@ -588,9 +532,8 @@ define i1 @init_eq_i128(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; X86-NEXT:    andl $96, %esi
 ; X86-NEXT:    shrl $3, %esi
 ; X86-NEXT:    movl (%edx,%esi), %edi
-; X86-NEXT:    btl %ecx, %edi
-; X86-NEXT:    setae %al
 ; X86-NEXT:    btrl %ecx, %edi
+; X86-NEXT:    setae %al
 ; X86-NEXT:    movzbl {{[0-9]+}}(%esp), %ebx
 ; X86-NEXT:    # kill: def $cl killed $cl killed $ecx
 ; X86-NEXT:    shll %cl, %ebx
@@ -607,10 +550,10 @@ define i1 @init_eq_i128(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; SSE-NEXT:    andl $96, %esi
 ; SSE-NEXT:    shrl $3, %esi
 ; SSE-NEXT:    movl (%rdi,%rsi), %r8d
-; SSE-NEXT:    btl %ecx, %r8d
+; SSE-NEXT:    btrl %ecx, %r8d
 ; SSE-NEXT:    setae %al
+; SSE-NEXT:    # kill: def $cl killed $cl killed $ecx
 ; SSE-NEXT:    shll %cl, %edx
-; SSE-NEXT:    btrl %ecx, %r8d
 ; SSE-NEXT:    orl %r8d, %edx
 ; SSE-NEXT:    movl %edx, (%rdi,%rsi)
 ; SSE-NEXT:    retq
@@ -621,9 +564,8 @@ define i1 @init_eq_i128(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; AVX-NEXT:    andl $96, %ecx
 ; AVX-NEXT:    shrl $3, %ecx
 ; AVX-NEXT:    movl (%rdi,%rcx), %r8d
-; AVX-NEXT:    btl %esi, %r8d
-; AVX-NEXT:    setae %al
 ; AVX-NEXT:    btrl %esi, %r8d
+; AVX-NEXT:    setae %al
 ; AVX-NEXT:    shlxl %esi, %edx, %edx
 ; AVX-NEXT:    orl %r8d, %edx
 ; AVX-NEXT:    movl %edx, (%rdi,%rcx)
@@ -679,20 +621,17 @@ define i1 @test_ne_i512(ptr %word, i32 %position) nounwind {
 define i1 @complement_ne_i512(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: complement_ne_i512:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    shrl $3, %esi
-; X86-NEXT:    andl $60, %esi
-; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    btl %edx, %edi
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    shrl $3, %edx
+; X86-NEXT:    andl $60, %edx
+; X86-NEXT:    movl (%ecx,%edx), %esi
+; X86-NEXT:    btcl %eax, %esi
 ; X86-NEXT:    setb %al
-; X86-NEXT:    btcl %edx, %edi
-; X86-NEXT:    movl %edi, (%ecx,%esi)
+; X86-NEXT:    movl %esi, (%ecx,%edx)
 ; X86-NEXT:    popl %esi
-; X86-NEXT:    popl %edi
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: complement_ne_i512:
@@ -701,9 +640,8 @@ define i1 @complement_ne_i512(ptr %word, i32 %position) nounwind {
 ; X64-NEXT:    shrl $3, %ecx
 ; X64-NEXT:    andl $60, %ecx
 ; X64-NEXT:    movl (%rdi,%rcx), %edx
-; X64-NEXT:    btl %esi, %edx
-; X64-NEXT:    setb %al
 ; X64-NEXT:    btcl %esi, %edx
+; X64-NEXT:    setb %al
 ; X64-NEXT:    movl %edx, (%rdi,%rcx)
 ; X64-NEXT:    retq
   %rem = and i32 %position, 511
@@ -720,20 +658,17 @@ define i1 @complement_ne_i512(ptr %word, i32 %position) nounwind {
 define i1 @reset_eq_i512(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: reset_eq_i512:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    shrl $3, %esi
-; X86-NEXT:    andl $60, %esi
-; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    btl %edx, %edi
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    shrl $3, %edx
+; X86-NEXT:    andl $60, %edx
+; X86-NEXT:    movl (%ecx,%edx), %esi
+; X86-NEXT:    btrl %eax, %esi
 ; X86-NEXT:    setae %al
-; X86-NEXT:    btrl %edx, %edi
-; X86-NEXT:    movl %edi, (%ecx,%esi)
+; X86-NEXT:    movl %esi, (%ecx,%edx)
 ; X86-NEXT:    popl %esi
-; X86-NEXT:    popl %edi
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: reset_eq_i512:
@@ -742,9 +677,8 @@ define i1 @reset_eq_i512(ptr %word, i32 %position) nounwind {
 ; X64-NEXT:    shrl $3, %ecx
 ; X64-NEXT:    andl $60, %ecx
 ; X64-NEXT:    movl (%rdi,%rcx), %edx
-; X64-NEXT:    btl %esi, %edx
-; X64-NEXT:    setae %al
 ; X64-NEXT:    btrl %esi, %edx
+; X64-NEXT:    setae %al
 ; X64-NEXT:    movl %edx, (%rdi,%rcx)
 ; X64-NEXT:    retq
   %rem = and i32 %position, 511
@@ -762,20 +696,17 @@ define i1 @reset_eq_i512(ptr %word, i32 %position) nounwind {
 define i1 @set_ne_i512(ptr %word, i32 %position) nounwind {
 ; X86-LABEL: set_ne_i512:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
-; X86-NEXT:    movl %edx, %esi
-; X86-NEXT:    shrl $3, %esi
-; X86-NEXT:    andl $60, %esi
-; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    btl %edx, %edi
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl %eax, %edx
+; X86-NEXT:    shrl $3, %edx
+; X86-NEXT:    andl $60, %edx
+; X86-NEXT:    movl (%ecx,%edx), %esi
+; X86-NEXT:    btsl %eax, %esi
 ; X86-NEXT:    setb %al
-; X86-NEXT:    btsl %edx, %edi
-; X86-NEXT:    movl %edi, (%ecx,%esi)
+; X86-NEXT:    movl %esi, (%ecx,%edx)
 ; X86-NEXT:    popl %esi
-; X86-NEXT:    popl %edi
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: set_ne_i512:
@@ -784,9 +715,8 @@ define i1 @set_ne_i512(ptr %word, i32 %position) nounwind {
 ; X64-NEXT:    shrl $3, %ecx
 ; X64-NEXT:    andl $60, %ecx
 ; X64-NEXT:    movl (%rdi,%rcx), %edx
-; X64-NEXT:    btl %esi, %edx
-; X64-NEXT:    setb %al
 ; X64-NEXT:    btsl %esi, %edx
+; X64-NEXT:    setb %al
 ; X64-NEXT:    movl %edx, (%rdi,%rcx)
 ; X64-NEXT:    retq
   %rem = and i32 %position, 511
@@ -812,9 +742,8 @@ define i1 @init_eq_i512(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; X86-NEXT:    shrl $3, %esi
 ; X86-NEXT:    andl $60, %esi
 ; X86-NEXT:    movl (%edx,%esi), %edi
-; X86-NEXT:    btl %ecx, %edi
-; X86-NEXT:    setae %al
 ; X86-NEXT:    btrl %ecx, %edi
+; X86-NEXT:    setae %al
 ; X86-NEXT:    movzbl {{[0-9]+}}(%esp), %ebx
 ; X86-NEXT:    # kill: def $cl killed $cl killed $ecx
 ; X86-NEXT:    shll %cl, %ebx
@@ -831,10 +760,10 @@ define i1 @init_eq_i512(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; SSE-NEXT:    shrl $3, %esi
 ; SSE-NEXT:    andl $60, %esi
 ; SSE-NEXT:    movl (%rdi,%rsi), %r8d
-; SSE-NEXT:    btl %ecx, %r8d
+; SSE-NEXT:    btrl %ecx, %r8d
 ; SSE-NEXT:    setae %al
+; SSE-NEXT:    # kill: def $cl killed $cl killed $ecx
 ; SSE-NEXT:    shll %cl, %edx
-; SSE-NEXT:    btrl %ecx, %r8d
 ; SSE-NEXT:    orl %r8d, %edx
 ; SSE-NEXT:    movl %edx, (%rdi,%rsi)
 ; SSE-NEXT:    retq
@@ -845,9 +774,8 @@ define i1 @init_eq_i512(ptr %word, i32 %position, i1 zeroext %value) nounwind {
 ; AVX-NEXT:    shrl $3, %ecx
 ; AVX-NEXT:    andl $60, %ecx
 ; AVX-NEXT:    movl (%rdi,%rcx), %r8d
-; AVX-NEXT:    btl %esi, %r8d
-; AVX-NEXT:    setae %al
 ; AVX-NEXT:    btrl %esi, %r8d
+; AVX-NEXT:    setae %al
 ; AVX-NEXT:    shlxl %esi, %edx, %edx
 ; AVX-NEXT:    orl %r8d, %edx
 ; AVX-NEXT:    movl %edx, (%rdi,%rcx)
@@ -1103,28 +1031,24 @@ define <8 x i16> @complement_ne_i128_bitcast(ptr %word, i32 %position) nounwind
 define i32 @reset_multiload_i128(ptr %word, i32 %position, ptr %p) nounwind {
 ; X86-LABEL: reset_multiload_i128:
 ; X86:       # %bb.0:
-; X86-NEXT:    pushl %ebx
 ; X86-NEXT:    pushl %edi
 ; X86-NEXT:    pushl %esi
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; X86-NEXT:    movl (%eax), %eax
 ; X86-NEXT:    movl %edx, %esi
 ; X86-NEXT:    andl $96, %esi
 ; X86-NEXT:    shrl $3, %esi
 ; X86-NEXT:    movl (%ecx,%esi), %edi
-; X86-NEXT:    movl %edi, %ebx
-; X86-NEXT:    btrl %edx, %ebx
-; X86-NEXT:    btl %edx, %edi
-; X86-NEXT:    movl %ebx, (%ecx,%esi)
+; X86-NEXT:    btrl %edx, %edi
+; X86-NEXT:    movl %edi, (%ecx,%esi)
 ; X86-NEXT:    jae .LBB23_2
 ; X86-NEXT:  # %bb.1:
 ; X86-NEXT:    xorl %eax, %eax
 ; X86-NEXT:  .LBB23_2:
 ; X86-NEXT:    popl %esi
 ; X86-NEXT:    popl %edi
-; X86-NEXT:    popl %ebx
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: reset_multiload_i128:
@@ -1132,11 +1056,9 @@ define i32 @reset_multiload_i128(ptr %word, i32 %position, ptr %p) nounwind {
 ; X64-NEXT:    movl %esi, %ecx
 ; X64-NEXT:    andl $96, %ecx
 ; X64-NEXT:    shrl $3, %ecx
-; X64-NEXT:    movl (%rdi,%rcx), %r9d
-; X64-NEXT:    movl %r9d, %r8d
-; X64-NEXT:    btrl %esi, %r8d
+; X64-NEXT:    movl (%rdi,%rcx), %r8d
 ; X64-NEXT:    xorl %eax, %eax
-; X64-NEXT:    btl %esi, %r9d
+; X64-NEXT:    btrl %esi, %r8d
 ; X64-NEXT:    jb .LBB23_2
 ; X64-NEXT:  # %bb.1:
 ; X64-NEXT:    movl (%rdx), %eax
diff --git a/llvm/test/CodeGen/X86/bt-merge-fuse.ll b/llvm/test/CodeGen/X86/bt-merge-fuse.ll
index ea23221940ee7..192931b023bfc 100644
--- a/llvm/test/CodeGen/X86/bt-merge-fuse.ll
+++ b/llvm/test/CodeGen/X86/bt-merge-fuse.ll
@@ -9,10 +9,8 @@
 define i1 @fuse_and_mask_on_modify(ptr %word, i32 %position) nounwind {
 ; CHECK-LABEL: fuse_and_mask_on_modify:
 ; CHECK:       # %bb.0:
-; CHECK-NEXT:    movl (%rdi), %eax
-; CHECK-NEXT:    movl %eax, %ecx
+; CHECK-NEXT:    movl (%rdi), %ecx
 ; CHECK-NEXT:    btcl %esi, %ecx
-; CHECK-NEXT:    btl %esi, %eax
 ; CHECK-NEXT:    setb %al
 ; CHECK-NEXT:    movl %ecx, (%rdi)
 ; CHECK-NEXT:    retq
@@ -30,10 +28,8 @@ define i1 @fuse_and_mask_on_modify(ptr %word, i32 %position) nounwind {
 define i1 @fuse_and_mask_on_bt(ptr %word, i64 %position) nounwind {
 ; CHECK-LABEL: fuse_and_mask_on_bt:
 ; CHECK:       # %bb.0:
-; CHECK-NEXT:    movq (%rdi), %rax
-; CHECK-NEXT:    movq %rax, %rcx
+; CHECK-NEXT:    movq (%rdi), %rcx
 ; CHECK-NEXT:    btsq %rsi, %rcx
-; CHECK-NEXT:    btq %rsi, %rax
 ; CHECK-NEXT:    setb %al
 ; CHECK-NEXT:    movq %rcx, (%rdi)
 ; CHECK-NEXT:    retq
@@ -53,10 +49,8 @@ define i1 @fuse_zext_pos(ptr %word, i32 %position) nounwind {
 ; CHECK-LABEL: fuse_zext_pos:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    # kill: def $esi killed $esi def $rsi
-; CHECK-NEXT:    movq (%rdi), %rax
-; CHECK-NEXT:    movq %rax, %rcx
+; CHECK-NEXT:    movq (%rdi), %rcx
 ; CHECK-NEXT:    btrq %rsi, %rcx
-; CHECK-NEXT:    btq %rsi, %rax
 ; CHECK-NEXT:    setae %al
 ; CHECK-NEXT:    movq %rcx, (%rdi)
 ; CHECK-NEXT:    retq

>From 0e28daec462ad731c9d4ac31eb816d8406796fd2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Thu, 7 May 2026 13:22:42 +0200
Subject: [PATCH 4/9] [X86] Address review comments

- Drop _flag postfix from X86btr/X86bts/X86btc SDNode names.
- Drop "Fixes" tag from combineBTToBitOpFlag comment.
- Replace VT i16/i32/i64 early-return with assert; getBT promotes
  smaller widths to i32 before creating X86ISD::BT, so only i32/i64
  reach the combine.
- Use sd_match with m_BitwiseLogic + m_Specific(Src) + m_OneUse to
  replace manual operand-finding, type-equality, and one-use checks.
---
 llvm/lib/Target/X86/X86ISelLowering.cpp  | 29 +++++++-----------------
 llvm/lib/Target/X86/X86InstrCompiler.td  | 24 ++++++++------------
 llvm/lib/Target/X86/X86InstrFragments.td |  6 ++---
 3 files changed, 21 insertions(+), 38 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index c3c9eb7bfd2c4..717e80e3af7ff 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -56906,16 +56906,17 @@ static SDValue peekThroughBitPosExtTrunc(SDValue V, unsigned BW) {
 // Src — AND(Src, rotl -2, X), OR(Src, shl 1, X), XOR(Src, shl 1, X) — into a
 // single flag-producing X86ISD::{BTR,BTS,BTC} node. Both BT and BTR/BTS/BTC
 // set CF from the pre-op bit value, so one instruction subsumes the other.
-// Fixes llvm#165291.
 static SDValue combineBTToBitOpFlag(SDNode *N, SelectionDAG &DAG) {
+  using namespace SDPatternMatch;
   SDValue Src = N->getOperand(0);
   SDValue BitNo = N->getOperand(1);
   EVT VT = Src.getValueType();
   SDLoc DL(N);
 
-  // BT is only emitted for legal integer widths (16/32/64); match those.
-  if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
-    return SDValue();
+  // X86ISD::BT is only emitted for 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");
 
   unsigned BW = VT.getScalarSizeInBits();
   SDValue PeeledBitNo = peekThroughBitPosExtTrunc(BitNo, BW);
@@ -56923,27 +56924,13 @@ static SDValue combineBTToBitOpFlag(SDNode *N, SelectionDAG &DAG) {
   for (SDNode *User : Src->users()) {
     if (User == N)
       continue;
-    unsigned UOpc = User->getOpcode();
-    if (UOpc != ISD::AND && UOpc != ISD::OR && UOpc != ISD::XOR)
-      continue;
-    if (User->getValueType(0) != VT)
-      continue;
 
-    // Identify which operand of User is Src; the other is the mask.
-    SDValue UOp0 = User->getOperand(0);
-    SDValue UOp1 = User->getOperand(1);
     SDValue Mask;
-    if (UOp0 == SDValue(Src.getNode(), Src.getResNo()))
-      Mask = UOp1;
-    else if (UOp1 == SDValue(Src.getNode(), Src.getResNo()))
-      Mask = UOp0;
-    else
-      continue;
-    // We will replace the mask's consumer (User); require the mask to have no
-    // other live uses so we can drop it.
-    if (!Mask.hasOneUse())
+    if (!sd_match(User,
+                  m_BitwiseLogic(m_Specific(Src), m_OneUse(m_Value(Mask)))))
       continue;
 
+    unsigned UOpc = User->getOpcode();
     unsigned FlagOp = 0;
     SDValue ShAmt;
     if (UOpc == ISD::AND && Mask.getOpcode() == ISD::ROTL) {
diff --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td
index 735bc57e8eaba..8ef2b62240ca0 100644
--- a/llvm/lib/Target/X86/X86InstrCompiler.td
+++ b/llvm/lib/Target/X86/X86InstrCompiler.td
@@ -2074,20 +2074,16 @@ defm : OneBitPats<GR64, i64, BTR64rr, BTS64rr, BTC64rr, shiftMask64>;
 
 // Flag-producing variants: reuse the BTR/BTS/BTC encodings so CF can replace
 // a separate X86ISD::BT. Emitted by a DAG combine in combineBT.
-multiclass OneBitFlagPats<RegisterClass rc, Instruction btr, Instruction bts,
-                          Instruction btc, SDNode btr_flag, SDNode bts_flag,
-                          SDNode btc_flag> {
-  def : Pat<(btr_flag rc:$src1, rc:$src2), (btr rc:$src1, rc:$src2)>;
-  def : Pat<(bts_flag rc:$src1, rc:$src2), (bts rc:$src1, rc:$src2)>;
-  def : Pat<(btc_flag rc:$src1, rc:$src2), (btc rc:$src1, rc:$src2)>;
-}
-
-defm : OneBitFlagPats<GR16, BTR16rr, BTS16rr, BTC16rr,
-                      X86btr_flag, X86bts_flag, X86btc_flag>;
-defm : OneBitFlagPats<GR32, BTR32rr, BTS32rr, BTC32rr,
-                      X86btr_flag, X86bts_flag, X86btc_flag>;
-defm : OneBitFlagPats<GR64, BTR64rr, BTS64rr, BTC64rr,
-                      X86btr_flag, X86bts_flag, X86btc_flag>;
+multiclass OneBitFlagPats<RegisterClass rc, Instruction btr_inst,
+                          Instruction bts_inst, Instruction btc_inst> {
+  def : Pat<(X86btr rc:$src1, rc:$src2), (btr_inst rc:$src1, rc:$src2)>;
+  def : Pat<(X86bts rc:$src1, rc:$src2), (bts_inst rc:$src1, rc:$src2)>;
+  def : Pat<(X86btc rc:$src1, rc:$src2), (btc_inst rc:$src1, rc:$src2)>;
+}
+
+defm : OneBitFlagPats<GR16, BTR16rr, BTS16rr, BTC16rr>;
+defm : OneBitFlagPats<GR32, BTR32rr, BTS32rr, BTC32rr>;
+defm : OneBitFlagPats<GR64, BTR64rr, BTS64rr, BTC64rr>;
 
 //===----------------------------------------------------------------------===//
 // EFLAGS-defining Patterns
diff --git a/llvm/lib/Target/X86/X86InstrFragments.td b/llvm/lib/Target/X86/X86InstrFragments.td
index e096aa1d074fa..9961aba630ffc 100644
--- a/llvm/lib/Target/X86/X86InstrFragments.td
+++ b/llvm/lib/Target/X86/X86InstrFragments.td
@@ -169,9 +169,9 @@ def X86bt      : SDNode<"X86ISD::BT",       SDTX86CmpTest>;
 // CF is set from the pre-operation bit value; BT on the same operands is
 // therefore redundant after one of these nodes. The atomic (locked) variants
 // live under X86ISD::LBTR/LBTS/LBTC.
-def X86btr_flag : SDNode<"X86ISD::BTR", SDTBinaryArithWithFlags>;
-def X86bts_flag : SDNode<"X86ISD::BTS", SDTBinaryArithWithFlags>;
-def X86btc_flag : SDNode<"X86ISD::BTC", SDTBinaryArithWithFlags>;
+def X86btr : SDNode<"X86ISD::BTR", SDTBinaryArithWithFlags>;
+def X86bts : SDNode<"X86ISD::BTS", SDTBinaryArithWithFlags>;
+def X86btc : SDNode<"X86ISD::BTC", SDTBinaryArithWithFlags>;
 
 // Conditional compare instructions
 def X86ccmp    : SDNode<"X86ISD::CCMP",     SDTX86Ccmp>;

>From dbb4e63dd601f7ea30c5353c79c22f47af5f94b5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Thu, 7 May 2026 13:50:00 +0200
Subject: [PATCH 5/9] [X86] Tidy combineBTToBitOpFlag

- Drop the no-op getZExtOrTrunc on BT's bit index; BT's bit index is
  constrained to be the same type as Src (SDTCisSameAs in SDTX86CmpTest)
  so we can pass BitNo directly to BTR/BTS/BTC.
- Accept SIGN_EXTEND in peekThroughBitPosExtTrunc; like the existing
  TRUNCATE/ZERO_EXTEND/ANY_EXTEND cases, it preserves the low log2(BW)
  bits the bit-test cares about.
- Add a one-line comment on the AND-mask check direction.
---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 717e80e3af7ff..8842ad29fda43 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -56877,21 +56877,22 @@ static SDValue combineAndnp(SDNode *N, SelectionDAG &DAG,
   return SDValue();
 }
 
-// Strip TRUNCATE/ZERO_EXTEND/ANY_EXTEND wrappers and `and x, C` where C
-// preserves the low log2(BW) bits — these are transparent to BT/BTR/BTS/BTC,
-// which implicitly mask the bit index to log2(BW) bits.
+// Strip TRUNCATE/Z|S|ANY_EXTEND wrappers and `and x, C` where C preserves
+// the low log2(BW) bits — these are transparent to BT/BTR/BTS/BTC, which
+// implicitly mask the bit index to log2(BW) bits.
 static SDValue peekThroughBitPosExtTrunc(SDValue V, unsigned BW) {
   APInt LowBits =
       APInt::getLowBitsSet(V.getScalarValueSizeInBits(), Log2_32(BW));
   for (;;) {
     unsigned Op = V.getOpcode();
     if (Op == ISD::TRUNCATE || Op == ISD::ZERO_EXTEND ||
-        Op == ISD::ANY_EXTEND) {
+        Op == ISD::SIGN_EXTEND || Op == ISD::ANY_EXTEND) {
       V = V.getOperand(0);
       LowBits = LowBits.zextOrTrunc(V.getScalarValueSizeInBits());
       continue;
     }
     if (Op == ISD::AND) {
+      // The mask must keep all bits the bit-test cares about set.
       auto *C = dyn_cast<ConstantSDNode>(V.getOperand(1));
       if (C && LowBits.isSubsetOf(C->getAPIntValue())) {
         V = V.getOperand(0);
@@ -56957,10 +56958,11 @@ static SDValue combineBTToBitOpFlag(SDNode *N, SelectionDAG &DAG) {
     if (peekThroughBitPosExtTrunc(ShAmt, BW) != PeeledBitNo)
       continue;
 
-    // BTR/BTS/BTC *rr take the bit index in a register of the same width as
-    // the source. Extend or truncate to VT to match the instruction signature.
-    SDValue BN = DAG.getZExtOrTrunc(BitNo, DL, VT);
-    SDValue New = DAG.getNode(FlagOp, DL, DAG.getVTList(VT, MVT::i32), Src, BN);
+    // BT's bit index is constrained to Src's type, so we can reuse it as-is
+    // for BTR/BTS/BTC's bit index operand.
+    assert(BitNo.getValueType() == VT && "BT bit index must match Src type");
+    SDValue New =
+        DAG.getNode(FlagOp, DL, DAG.getVTList(VT, MVT::i32), Src, BitNo);
     // Reroute the value output through User's consumers.
     DAG.ReplaceAllUsesOfValueWith(SDValue(User, 0), New.getValue(0));
     // Return the flags output so combineBT installs it as N's replacement.

>From 0d7bcdb7bf8cf1fd87dd3ecb84e7392eec081e0b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Thu, 7 May 2026 15:54:17 +0200
Subject: [PATCH 6/9] [X86] Drop SIGN_EXTEND from peekThroughBitPosExtTrunc

Reverts the SIGN_EXTEND case added earlier in this branch. No constructed
test produces different codegen with vs. without it (the X86 lit suite
passes identically), and BT's BitNo and the bit-op's ShAmt are typically
the same SDValue when their original IR root is shared, so a structural
compare passes either way without stripping SIGN_EXTEND. Speculative
coverage with no regression test isn't worth keeping.
---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 8842ad29fda43..bc5e83c311d7b 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -56886,7 +56886,7 @@ static SDValue peekThroughBitPosExtTrunc(SDValue V, unsigned BW) {
   for (;;) {
     unsigned Op = V.getOpcode();
     if (Op == ISD::TRUNCATE || Op == ISD::ZERO_EXTEND ||
-        Op == ISD::SIGN_EXTEND || Op == ISD::ANY_EXTEND) {
+        Op == ISD::ANY_EXTEND) {
       V = V.getOperand(0);
       LowBits = LowBits.zextOrTrunc(V.getScalarValueSizeInBits());
       continue;

>From 9c83963c2e5d778d57a1fe24bde2849b3b6c8ccc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Thu, 7 May 2026 15:56:41 +0200
Subject: [PATCH 7/9] [X86] Drop dead i16 OneBitFlagPats; clarify SDNode
 comment

i16 X86ISD::BT can never reach combineBTToBitOpFlag because getBT
promotes <32-bit Src to i32 before creating the BT node, so the
matching bit-modify (still on i16) and the BT (on i32) have different
Src types and m_Specific(Src) won't match. Verified via probe IR:
the i16 fold path does not fire. Drop the unused GR16 defm entry.

Also qualify the new SDNode header comment as "non-atomic" for
symmetry with the existing X86ISD::LBTR/LBTS/LBTC reference.
---
 llvm/lib/Target/X86/X86InstrCompiler.td  | 3 ++-
 llvm/lib/Target/X86/X86InstrFragments.td | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td
index 8ef2b62240ca0..3272a23565722 100644
--- a/llvm/lib/Target/X86/X86InstrCompiler.td
+++ b/llvm/lib/Target/X86/X86InstrCompiler.td
@@ -2081,7 +2081,8 @@ multiclass OneBitFlagPats<RegisterClass rc, Instruction btr_inst,
   def : Pat<(X86btc rc:$src1, rc:$src2), (btc_inst rc:$src1, rc:$src2)>;
 }
 
-defm : OneBitFlagPats<GR16, BTR16rr, BTS16rr, BTC16rr>;
+// Only i32/i64 are relevant: getBT promotes <32-bit Src to i32 before
+// creating X86ISD::BT, so an i16 BT can never reach combineBTToBitOpFlag.
 defm : OneBitFlagPats<GR32, BTR32rr, BTS32rr, BTC32rr>;
 defm : OneBitFlagPats<GR64, BTR64rr, BTS64rr, BTC64rr>;
 
diff --git a/llvm/lib/Target/X86/X86InstrFragments.td b/llvm/lib/Target/X86/X86InstrFragments.td
index 9961aba630ffc..74d5a38694edb 100644
--- a/llvm/lib/Target/X86/X86InstrFragments.td
+++ b/llvm/lib/Target/X86/X86InstrFragments.td
@@ -165,7 +165,7 @@ let IsStrictFP = true in {
 // X86 bit-test instructions.
 def X86bt      : SDNode<"X86ISD::BT",       SDTX86CmpTest>;
 
-// X86 bit-test-and-modify instructions: res, EFLAGS = op src, bitno.
+// X86 non-atomic bit-test-and-modify instructions: res, EFLAGS = op src, bitno.
 // CF is set from the pre-operation bit value; BT on the same operands is
 // therefore redundant after one of these nodes. The atomic (locked) variants
 // live under X86ISD::LBTR/LBTS/LBTC.

>From c542fbda575fbb8341533166c5c62cef4c89e7e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Thu, 7 May 2026 16:38:39 +0200
Subject: [PATCH 8/9] [X86] Inline OneBitFlagPats now that GR16 entry is gone

With only the GR32 and GR64 widths in scope, the multiclass abstraction
has only two instantiations and obscures more than it saves. Inline the
six Pat<> defs directly.
---
 llvm/lib/Target/X86/X86InstrCompiler.td | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td
index 3272a23565722..54cfa256e9fec 100644
--- a/llvm/lib/Target/X86/X86InstrCompiler.td
+++ b/llvm/lib/Target/X86/X86InstrCompiler.td
@@ -2073,18 +2073,14 @@ defm : OneBitPats<GR32, i32, BTR32rr, BTS32rr, BTC32rr, shiftMask32>;
 defm : OneBitPats<GR64, i64, BTR64rr, BTS64rr, BTC64rr, shiftMask64>;
 
 // Flag-producing variants: reuse the BTR/BTS/BTC encodings so CF can replace
-// a separate X86ISD::BT. Emitted by a DAG combine in combineBT.
-multiclass OneBitFlagPats<RegisterClass rc, Instruction btr_inst,
-                          Instruction bts_inst, Instruction btc_inst> {
-  def : Pat<(X86btr rc:$src1, rc:$src2), (btr_inst rc:$src1, rc:$src2)>;
-  def : Pat<(X86bts rc:$src1, rc:$src2), (bts_inst rc:$src1, rc:$src2)>;
-  def : Pat<(X86btc rc:$src1, rc:$src2), (btc_inst rc:$src1, rc:$src2)>;
-}
-
-// Only i32/i64 are relevant: getBT promotes <32-bit Src to i32 before
-// creating X86ISD::BT, so an i16 BT can never reach combineBTToBitOpFlag.
-defm : OneBitFlagPats<GR32, BTR32rr, BTS32rr, BTC32rr>;
-defm : OneBitFlagPats<GR64, BTR64rr, BTS64rr, BTC64rr>;
+// a separate X86ISD::BT. Emitted by a DAG combine in combineBT. Only i32/i64
+// since getBT promotes <32-bit Src to i32 before creating X86ISD::BT.
+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)>;
+def : Pat<(X86btr GR64:$src1, GR64:$src2), (BTR64rr GR64:$src1, GR64:$src2)>;
+def : Pat<(X86bts GR64:$src1, GR64:$src2), (BTS64rr GR64:$src1, GR64:$src2)>;
+def : Pat<(X86btc GR64:$src1, GR64:$src2), (BTC64rr GR64:$src1, GR64:$src2)>;
 
 //===----------------------------------------------------------------------===//
 // EFLAGS-defining Patterns

>From 9ddf141a0a2ce2e4753410bf581c13ba3d55e0d9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Thu, 7 May 2026 19:06:02 +0200
Subject: [PATCH 9/9] [X86] Polish combineBT fuse: assert and comment

- peekThroughBitPosExtTrunc: assert that the source width is >= log2(BW)
  after peeling a TRUNCATE; in X86 the smallest int (i8) is 8 bits and
  log2(BW) <= 6, so the LowBits zextOrTrunc cannot silently lose bits,
  but the assert documents the invariant and makes the helper safer
  to lift later.
- Restore the header comment that was lost when SIGN_EXTEND was dropped.
- Note in OneBitFlagPats why the new patterns differ from OneBitPats
  (no GR8 sub-reg insertion: the bit index is already same-width).
---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 8 +++++---
 llvm/lib/Target/X86/X86InstrCompiler.td | 4 +++-
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index bc5e83c311d7b..3483ed2f932b2 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -56877,9 +56877,9 @@ static SDValue combineAndnp(SDNode *N, SelectionDAG &DAG,
   return SDValue();
 }
 
-// Strip TRUNCATE/Z|S|ANY_EXTEND wrappers and `and x, C` where C preserves
-// the low log2(BW) bits — these are transparent to BT/BTR/BTS/BTC, which
-// implicitly mask the bit index to log2(BW) bits.
+// Strip TRUNCATE/ZERO_EXTEND/ANY_EXTEND wrappers and `and x, C` where C
+// preserves the low log2(BW) bits — these are transparent to BT/BTR/BTS/BTC,
+// which implicitly mask the bit index to log2(BW) bits.
 static SDValue peekThroughBitPosExtTrunc(SDValue V, unsigned BW) {
   APInt LowBits =
       APInt::getLowBitsSet(V.getScalarValueSizeInBits(), Log2_32(BW));
@@ -56888,6 +56888,8 @@ static SDValue peekThroughBitPosExtTrunc(SDValue V, unsigned BW) {
     if (Op == ISD::TRUNCATE || Op == ISD::ZERO_EXTEND ||
         Op == ISD::ANY_EXTEND) {
       V = V.getOperand(0);
+      assert(V.getScalarValueSizeInBits() >= Log2_32(BW) &&
+             "low bits constraint must survive width adjustment");
       LowBits = LowBits.zextOrTrunc(V.getScalarValueSizeInBits());
       continue;
     }
diff --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td
index 54cfa256e9fec..afa0733d45dd0 100644
--- a/llvm/lib/Target/X86/X86InstrCompiler.td
+++ b/llvm/lib/Target/X86/X86InstrCompiler.td
@@ -2074,7 +2074,9 @@ defm : OneBitPats<GR64, i64, BTR64rr, BTS64rr, BTC64rr, shiftMask64>;
 
 // Flag-producing variants: reuse the BTR/BTS/BTC encodings so CF can replace
 // a separate X86ISD::BT. Emitted by a DAG combine in combineBT. Only i32/i64
-// since getBT promotes <32-bit Src to i32 before creating X86ISD::BT.
+// 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 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)>;



More information about the llvm-commits mailing list