[llvm] [DAG] Fold any-extend(and(trunc(x), C)) -> and(x, C) (PR #200052)

Aayush Shrivastava via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 04:00:35 PDT 2026


https://github.com/iamaayushrivastava updated https://github.com/llvm/llvm-project/pull/200052

>From 840f97bf6c1b20e17d34a7c5d63763173f335412 Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Thu, 28 May 2026 02:27:37 +0530
Subject: [PATCH 01/11] [DAGCombine] Fold any-extend(and(trunc(x), C)) ->
 and(x, C) on X86

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 10 +--
 llvm/test/CodeGen/X86/aext-and-trunc-fold.ll  | 77 +++++++++++++++++++
 2 files changed, 82 insertions(+), 5 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/aext-and-trunc-fold.ll

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 2a9ecebeb1508..bac76620fc77d 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -16163,15 +16163,15 @@ SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
     return DAG.getAnyExtOrTrunc(N0.getOperand(0), DL, VT);
 
   // Fold (aext (and (trunc x), cst)) -> (and x, cst)
-  // if the trunc is not free.
+  // if either of the casts is not free.
   if (N0.getOpcode() == ISD::AND &&
       N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
       N0.getOperand(1).getOpcode() == ISD::Constant &&
-      !TLI.isTruncateFree(N0.getOperand(0).getOperand(0), N0.getValueType())) {
+      (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0), N0.getValueType()) ||
+       !TLI.isZExtFree(N0.getValueType(), VT))) {
     SDValue X = DAG.getAnyExtOrTrunc(N0.getOperand(0).getOperand(0), DL, VT);
-    SDValue Y = DAG.getNode(ISD::ANY_EXTEND, DL, VT, N0.getOperand(1));
-    assert(isa<ConstantSDNode>(Y) && "Expected constant to be folded!");
-    return DAG.getNode(ISD::AND, DL, VT, X, Y);
+    APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
+    return DAG.getNode(ISD::AND, DL, VT, X, DAG.getConstant(Mask, DL, VT));
   }
 
   // fold (aext (load x)) -> (aext (truncate (extload x)))
diff --git a/llvm/test/CodeGen/X86/aext-and-trunc-fold.ll b/llvm/test/CodeGen/X86/aext-and-trunc-fold.ll
new file mode 100644
index 0000000000000..71d5b0aba250b
--- /dev/null
+++ b/llvm/test/CodeGen/X86/aext-and-trunc-fold.ll
@@ -0,0 +1,77 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s --check-prefix=X64
+; RUN: llc < %s -mtriple=i686-unknown-linux-gnu | FileCheck %s --check-prefix=X86
+
+; Test for GH#195575: fold (aext (and (trunc x) C)) -> (and x C)
+; when either the truncate or zero-extend is not free.
+; Previously visitANY_EXTEND only checked isTruncateFree (which is true on X86
+; for i32->i8), missing the case where isZExtFree is false (i8->i32 on X86).
+; This caused "and al, 7; movzx eax, al" instead of the optimal "and eax, 7".
+;
+; The DAGCombiner converts zero_extend to any_extend when the result is only
+; used by a shift (upper bits don't affect the output), but visitANY_EXTEND
+; was then failing to fold the (aext (and (trunc x) C)) pattern on X86 because
+; isTruncateFree(i32->i8) is true there. Adding the isZExtFree check (which is
+; false for i8->i32 on X86) makes the fold fire correctly.
+
+; Pattern that directly triggers the bug: shl context converts zext->aext before
+; the fold can run, and then visitANY_EXTEND needs the isZExtFree check.
+define i32 @aext_and_trunc_shl(i32 %x) {
+; X64-LABEL: aext_and_trunc_shl:
+; X64:       # %bb.0:
+; X64-NEXT:    # kill: def $edi killed $edi def $rdi
+; X64-NEXT:    andl $7, %edi
+; X64-NEXT:    leal (%rdi,%rdi), %eax
+; X64-NEXT:    retq
+;
+; X86-LABEL: aext_and_trunc_shl:
+; X86:       # %bb.0:
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    andl $7, %eax
+; X86-NEXT:    addl %eax, %eax
+; X86-NEXT:    retl
+  %trunc = trunc i32 %x to i8
+  %and = and i8 %trunc, 7
+  %zext = zext i8 %and to i32
+  %shl = shl i32 %zext, 1
+  ret i32 %shl
+}
+
+; Basic (aext (and (trunc i32) C)) -> (and i32 C) pattern.
+define i32 @aext_and_trunc_i32(i32 %x) {
+; X64-LABEL: aext_and_trunc_i32:
+; X64:       # %bb.0:
+; X64-NEXT:    movl %edi, %eax
+; X64-NEXT:    andl $7, %eax
+; X64-NEXT:    retq
+;
+; X86-LABEL: aext_and_trunc_i32:
+; X86:       # %bb.0:
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    andl $7, %eax
+; X86-NEXT:    retl
+  %trunc = trunc i32 %x to i8
+  %and = and i8 %trunc, 7
+  %zext = zext i8 %and to i32
+  ret i32 %zext
+}
+
+; (aext (and (trunc i64) C)) -> (and i32 C) on x86-64.
+define i32 @aext_and_trunc_i64(i64 %x) {
+; X64-LABEL: aext_and_trunc_i64:
+; X64:       # %bb.0:
+; X64-NEXT:    movq %rdi, %rax
+; X64-NEXT:    andl $15, %eax
+; X64-NEXT:    # kill: def $eax killed $eax killed $rax
+; X64-NEXT:    retq
+;
+; X86-LABEL: aext_and_trunc_i64:
+; X86:       # %bb.0:
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    andl $15, %eax
+; X86-NEXT:    retl
+  %trunc = trunc i64 %x to i8
+  %and = and i8 %trunc, 15
+  %zext = zext i8 %and to i32
+  ret i32 %zext
+}

>From a90e72cf5b2a2e72cb0c605f519cf2529292ef52 Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Thu, 28 May 2026 17:31:36 +0530
Subject: [PATCH 02/11] [DAGCombine] Regenerate tests affected by fold
 any-extend(and(trunc(x), C))

---
 llvm/test/CodeGen/AArch64/bitfield-insert.ll  |  2 +-
 llvm/test/CodeGen/AArch64/bswap-known-bits.ll |  2 +-
 llvm/test/CodeGen/AArch64/sshl_sat.ll         |  2 +-
 llvm/test/CodeGen/AArch64/ushl_sat.ll         |  2 +-
 llvm/test/CodeGen/RISCV/and.ll                |  9 ++++---
 llvm/test/CodeGen/RISCV/pr142004.ll           |  4 ++-
 llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll  | 25 ++++++++++++++----
 llvm/test/CodeGen/X86/and-with-overflow.ll    |  4 +--
 llvm/test/CodeGen/X86/combine-srem.ll         |  4 +--
 llvm/test/CodeGen/X86/llvm.frexp.ll           |  4 +--
 llvm/test/CodeGen/X86/masked_load.ll          | 26 +++++++++----------
 llvm/test/CodeGen/X86/masked_store.ll         |  2 +-
 ...asked-merge-scalar-constmask-innerouter.ll |  4 +--
 ...-merge-scalar-constmask-interleavedbits.ll |  4 +--
 ...-scalar-constmask-interleavedbytehalves.ll |  4 +--
 ...d-masked-merge-scalar-constmask-lowhigh.ll |  4 +--
 16 files changed, 61 insertions(+), 41 deletions(-)

diff --git a/llvm/test/CodeGen/AArch64/bitfield-insert.ll b/llvm/test/CodeGen/AArch64/bitfield-insert.ll
index eefb862c5313c..9402aa367ef2b 100644
--- a/llvm/test/CodeGen/AArch64/bitfield-insert.ll
+++ b/llvm/test/CodeGen/AArch64/bitfield-insert.ll
@@ -739,7 +739,7 @@ define i32 @orr_not_bfxil_test2_i32(i32 %0) {
 define i16 @implicit_trunc_of_imm(ptr %p, i16 %a, i16 %b) {
 ; CHECK-LABEL: implicit_trunc_of_imm:
 ; CHECK:       // %bb.0: // %entry
-; CHECK-NEXT:    and w8, w1, #0xffffe000
+; CHECK-NEXT:    and w8, w1, #0xe000
 ; CHECK-NEXT:    mov x9, x0
 ; CHECK-NEXT:    mov w10, w8
 ; CHECK-NEXT:    mov w0, w8
diff --git a/llvm/test/CodeGen/AArch64/bswap-known-bits.ll b/llvm/test/CodeGen/AArch64/bswap-known-bits.ll
index 7e881198ced02..b42cf9db7bd01 100644
--- a/llvm/test/CodeGen/AArch64/bswap-known-bits.ll
+++ b/llvm/test/CodeGen/AArch64/bswap-known-bits.ll
@@ -131,7 +131,7 @@ define i16 @bswap_src_and_lo_i16(i16 %x) {
 define i16 @bswap_src_and_hi_i16(i16 %x) {
 ; CHECK-LABEL: bswap_src_and_hi_i16:
 ; CHECK:       ; %bb.0:
-; CHECK-NEXT:    and w8, w0, #0xffffff00
+; CHECK-NEXT:    and w8, w0, #0xff00
 ; CHECK-NEXT:    rev16 w0, w8
 ; CHECK-NEXT:    ret
   %m = and i16 %x, 65280
diff --git a/llvm/test/CodeGen/AArch64/sshl_sat.ll b/llvm/test/CodeGen/AArch64/sshl_sat.ll
index be2b3e763733b..e68b1f090949f 100644
--- a/llvm/test/CodeGen/AArch64/sshl_sat.ll
+++ b/llvm/test/CodeGen/AArch64/sshl_sat.ll
@@ -131,7 +131,7 @@ define void @combine_shlsat_vector() nounwind {
 define i16 @combine_shlsat_to_shl(i16 %x) nounwind {
 ; CHECK-LABEL: combine_shlsat_to_shl:
 ; CHECK:       // %bb.0:
-; CHECK-NEXT:    and w0, w0, #0xfffffffc
+; CHECK-NEXT:    and w0, w0, #0xfffc
 ; CHECK-NEXT:    ret
   %x2 = ashr i16 %x, 2
   %tmp = call i16 @llvm.sshl.sat.i16(i16 %x2, i16 2)
diff --git a/llvm/test/CodeGen/AArch64/ushl_sat.ll b/llvm/test/CodeGen/AArch64/ushl_sat.ll
index 870f80545f999..ac28a3dfbd168 100644
--- a/llvm/test/CodeGen/AArch64/ushl_sat.ll
+++ b/llvm/test/CodeGen/AArch64/ushl_sat.ll
@@ -117,7 +117,7 @@ define void @combine_shlsat_vector() nounwind {
 define i16 @combine_shlsat_to_shl(i16 %x) nounwind {
 ; CHECK-LABEL: combine_shlsat_to_shl:
 ; CHECK:       // %bb.0:
-; CHECK-NEXT:    and w0, w0, #0xfffffffc
+; CHECK-NEXT:    and w0, w0, #0xfffc
 ; CHECK-NEXT:    ret
   %x2 = lshr i16 %x, 2
   %tmp = call i16 @llvm.ushl.sat.i16(i16 %x2, i16 2)
diff --git a/llvm/test/CodeGen/RISCV/and.ll b/llvm/test/CodeGen/RISCV/and.ll
index 31c63c7f9b18f..2b0b10ac57ff6 100644
--- a/llvm/test/CodeGen/RISCV/and.ll
+++ b/llvm/test/CodeGen/RISCV/and.ll
@@ -99,8 +99,8 @@ define i32 @and32_0xfffff000(i32 %x) {
 ;
 ; RV64I-LABEL: and32_0xfffff000:
 ; RV64I:       # %bb.0:
-; RV64I-NEXT:    lui a1, 1048575
-; RV64I-NEXT:    and a0, a0, a1
+; RV64I-NEXT:    srliw a0, a0, 12
+; RV64I-NEXT:    slli a0, a0, 12
 ; RV64I-NEXT:    ret
   %a = and i32 %x, -4096
   ret i32 %a
@@ -114,7 +114,10 @@ define i32 @and32_0xfffffa00(i32 %x) {
 ;
 ; RV64I-LABEL: and32_0xfffffa00:
 ; RV64I:       # %bb.0:
-; RV64I-NEXT:    andi a0, a0, -1536
+; RV64I-NEXT:    li a1, 1
+; RV64I-NEXT:    slli a1, a1, 32
+; RV64I-NEXT:    addi a1, a1, -1536
+; RV64I-NEXT:    and a0, a0, a1
 ; RV64I-NEXT:    ret
   %a = and i32 %x, -1536
   ret i32 %a
diff --git a/llvm/test/CodeGen/RISCV/pr142004.ll b/llvm/test/CodeGen/RISCV/pr142004.ll
index 709644e49e704..49173ca11d059 100644
--- a/llvm/test/CodeGen/RISCV/pr142004.ll
+++ b/llvm/test/CodeGen/RISCV/pr142004.ll
@@ -11,12 +11,14 @@ define i32 @foo(i32 %x) {
 ; CHECK-NEXT:    lui a1, %hi(f)
 ; CHECK-NEXT:    lui a2, %hi(d)
 ; CHECK-NEXT:    lbu a1, %lo(f)(a1)
-; CHECK-NEXT:    lhu a2, %lo(d)(a2)
+; CHECK-NEXT:    ld a2, %lo(d)(a2)
 ; CHECK-NEXT:    slli a0, a0, 48
 ; CHECK-NEXT:    srli a3, a0, 48
 ; CHECK-NEXT:    xori a0, a1, 255
 ; CHECK-NEXT:    or a0, a0, a2
 ; CHECK-NEXT:    lui a1, %hi(e)
+; CHECK-NEXT:    slli a0, a0, 48
+; CHECK-NEXT:    srli a0, a0, 48
 ; CHECK-NEXT:    sw a3, %lo(e)(a1)
 ; CHECK-NEXT:    ret
 entry:
diff --git a/llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll b/llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll
index 751b05ebdba2a..e57a47e9bc0c0 100644
--- a/llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll
+++ b/llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll
@@ -9,11 +9,26 @@
 ; RUN:   | FileCheck %s --check-prefixes=CHECK,RV64,ZBS,ZBS64
 
 define i32 @and0xabcdefff(i32 %x) {
-; CHECK-LABEL: and0xabcdefff:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    lui a1, 344865
-; CHECK-NEXT:    andn a0, a0, a1
-; CHECK-NEXT:    ret
+; RV32-LABEL: and0xabcdefff:
+; RV32:       # %bb.0:
+; RV32-NEXT:    lui a1, 344865
+; RV32-NEXT:    andn a0, a0, a1
+; RV32-NEXT:    ret
+;
+; NOZBS64-LABEL: and0xabcdefff:
+; NOZBS64:       # %bb.0:
+; NOZBS64-NEXT:    lui a1, 1048404
+; NOZBS64-NEXT:    addi a1, a1, 801
+; NOZBS64-NEXT:    slli a1, a1, 12
+; NOZBS64-NEXT:    andn a0, a0, a1
+; NOZBS64-NEXT:    ret
+;
+; ZBS64-LABEL: and0xabcdefff:
+; ZBS64:       # %bb.0:
+; ZBS64-NEXT:    lui a1, 869153
+; ZBS64-NEXT:    bclri a1, a1, 31
+; ZBS64-NEXT:    andn a0, a0, a1
+; ZBS64-NEXT:    ret
   %and = and i32 %x, -1412567041
   ret i32 %and
 }
diff --git a/llvm/test/CodeGen/X86/and-with-overflow.ll b/llvm/test/CodeGen/X86/and-with-overflow.ll
index a63f6cc6ea7e2..1fef4165f0591 100644
--- a/llvm/test/CodeGen/X86/and-with-overflow.ll
+++ b/llvm/test/CodeGen/X86/and-with-overflow.ll
@@ -21,8 +21,8 @@ define i8 @and_i8_ri(i8 zeroext %0, i8 zeroext %1) {
 ; X64-LABEL: and_i8_ri:
 ; X64:       # %bb.0:
 ; X64-NEXT:    movl %edi, %eax
-; X64-NEXT:    andb $-17, %al
-; X64-NEXT:    movzbl %al, %eax
+; X64-NEXT:    andl $-17, %eax
+; X64-NEXT:    testb $-17, %dil
 ; X64-NEXT:    cmovel %edi, %eax
 ; X64-NEXT:    # kill: def $al killed $al killed $eax
 ; X64-NEXT:    retq
diff --git a/llvm/test/CodeGen/X86/combine-srem.ll b/llvm/test/CodeGen/X86/combine-srem.ll
index 0ca79adf5392e..82ac096353f06 100644
--- a/llvm/test/CodeGen/X86/combine-srem.ll
+++ b/llvm/test/CodeGen/X86/combine-srem.ll
@@ -498,7 +498,7 @@ define i16 @combine_i16_srem_pow2(i16 %x) {
 ; CHECK-NEXT:    leal 15(%rax), %ecx
 ; CHECK-NEXT:    testw %ax, %ax
 ; CHECK-NEXT:    cmovnsl %edi, %ecx
-; CHECK-NEXT:    andl $-16, %ecx
+; CHECK-NEXT:    andl $65520, %ecx # imm = 0xFFF0
 ; CHECK-NEXT:    subl %ecx, %eax
 ; CHECK-NEXT:    # kill: def $ax killed $ax killed $rax
 ; CHECK-NEXT:    retq
@@ -513,7 +513,7 @@ define i16 @combine_i16_srem_negpow2(i16 %x) {
 ; CHECK-NEXT:    leal 255(%rax), %ecx
 ; CHECK-NEXT:    testw %ax, %ax
 ; CHECK-NEXT:    cmovnsl %edi, %ecx
-; CHECK-NEXT:    andl $-256, %ecx
+; CHECK-NEXT:    andl $65280, %ecx # imm = 0xFF00
 ; CHECK-NEXT:    subl %ecx, %eax
 ; CHECK-NEXT:    # kill: def $ax killed $ax killed $rax
 ; CHECK-NEXT:    retq
diff --git a/llvm/test/CodeGen/X86/llvm.frexp.ll b/llvm/test/CodeGen/X86/llvm.frexp.ll
index e3a1b1b83b2e3..9112c176eaf27 100644
--- a/llvm/test/CodeGen/X86/llvm.frexp.ll
+++ b/llvm/test/CodeGen/X86/llvm.frexp.ll
@@ -25,7 +25,7 @@ define { half, i32 } @test_frexp_f16_i32(half %a) nounwind {
 ; X64-NEXT:    cmpl $1024, %esi # imm = 0x400
 ; X64-NEXT:    cmovael %eax, %edi
 ; X64-NEXT:    addl $-14, %edi
-; X64-NEXT:    andl $-31745, %ecx # imm = 0x83FF
+; X64-NEXT:    andl $33791, %ecx # imm = 0x83FF
 ; X64-NEXT:    orl $14336, %ecx # imm = 0x3800
 ; X64-NEXT:    addl $-31744, %esi # imm = 0x8400
 ; X64-NEXT:    movzwl %si, %esi
@@ -76,7 +76,7 @@ define half @test_frexp_f16_i32_only_use_fract(half %a) nounwind {
 ; X64-NEXT:    andl $32767, %edx # imm = 0x7FFF
 ; X64-NEXT:    cmpl $1024, %edx # imm = 0x400
 ; X64-NEXT:    cmovael %ecx, %eax
-; X64-NEXT:    andl $-31745, %eax # imm = 0x83FF
+; X64-NEXT:    andl $33791, %eax # imm = 0x83FF
 ; X64-NEXT:    orl $14336, %eax # imm = 0x3800
 ; X64-NEXT:    addl $-31744, %edx # imm = 0x8400
 ; X64-NEXT:    movzwl %dx, %edx
diff --git a/llvm/test/CodeGen/X86/masked_load.ll b/llvm/test/CodeGen/X86/masked_load.ll
index d755c8e34e60b..88b51499518be 100644
--- a/llvm/test/CodeGen/X86/masked_load.ll
+++ b/llvm/test/CodeGen/X86/masked_load.ll
@@ -701,15 +701,15 @@ define <8 x double> @load_v8f64_i8(i8 %trigger, ptr %addr, <8 x double> %dst) {
 ; AVX1-LABEL: load_v8f64_i8:
 ; AVX1:       ## %bb.0:
 ; AVX1-NEXT:    movl %edi, %eax
-; AVX1-NEXT:    shrb %al
+; AVX1-NEXT:    shrb $2, %al
 ; AVX1-NEXT:    andb $1, %al
 ; AVX1-NEXT:    movl %edi, %ecx
+; AVX1-NEXT:    shrb %cl
 ; AVX1-NEXT:    andb $1, %cl
-; AVX1-NEXT:    vmovd %ecx, %xmm2
-; AVX1-NEXT:    vpinsrb $2, %eax, %xmm2, %xmm2
-; AVX1-NEXT:    movl %edi, %eax
-; AVX1-NEXT:    shrb $2, %al
-; AVX1-NEXT:    andb $1, %al
+; AVX1-NEXT:    movl %edi, %edx
+; AVX1-NEXT:    andl $1, %edx
+; AVX1-NEXT:    vmovd %edx, %xmm2
+; AVX1-NEXT:    vpinsrb $2, %ecx, %xmm2, %xmm2
 ; AVX1-NEXT:    vpinsrb $4, %eax, %xmm2, %xmm2
 ; AVX1-NEXT:    movl %edi, %eax
 ; AVX1-NEXT:    shrb $3, %al
@@ -750,15 +750,15 @@ define <8 x double> @load_v8f64_i8(i8 %trigger, ptr %addr, <8 x double> %dst) {
 ; AVX2-LABEL: load_v8f64_i8:
 ; AVX2:       ## %bb.0:
 ; AVX2-NEXT:    movl %edi, %eax
-; AVX2-NEXT:    shrb %al
+; AVX2-NEXT:    shrb $2, %al
 ; AVX2-NEXT:    andb $1, %al
 ; AVX2-NEXT:    movl %edi, %ecx
+; AVX2-NEXT:    shrb %cl
 ; AVX2-NEXT:    andb $1, %cl
-; AVX2-NEXT:    vmovd %ecx, %xmm2
-; AVX2-NEXT:    vpinsrb $2, %eax, %xmm2, %xmm2
-; AVX2-NEXT:    movl %edi, %eax
-; AVX2-NEXT:    shrb $2, %al
-; AVX2-NEXT:    andb $1, %al
+; AVX2-NEXT:    movl %edi, %edx
+; AVX2-NEXT:    andl $1, %edx
+; AVX2-NEXT:    vmovd %edx, %xmm2
+; AVX2-NEXT:    vpinsrb $2, %ecx, %xmm2, %xmm2
 ; AVX2-NEXT:    vpinsrb $4, %eax, %xmm2, %xmm2
 ; AVX2-NEXT:    movl %edi, %eax
 ; AVX2-NEXT:    shrb $3, %al
@@ -1205,7 +1205,7 @@ define <2 x float> @load_v2f32_i2(i2 %trigger, ptr %addr, <2 x float> %dst) {
 ; AVX1OR2-NEXT:    movl %edi, %eax
 ; AVX1OR2-NEXT:    andb $2, %al
 ; AVX1OR2-NEXT:    shrb %al
-; AVX1OR2-NEXT:    andb $1, %dil
+; AVX1OR2-NEXT:    andl $1, %edi
 ; AVX1OR2-NEXT:    vmovd %edi, %xmm1
 ; AVX1OR2-NEXT:    vpinsrb $8, %eax, %xmm1, %xmm1
 ; AVX1OR2-NEXT:    vinsertps {{.*#+}} xmm1 = xmm1[0,2],zero,zero
diff --git a/llvm/test/CodeGen/X86/masked_store.ll b/llvm/test/CodeGen/X86/masked_store.ll
index 5882f3924473e..c0897c290bc05 100644
--- a/llvm/test/CodeGen/X86/masked_store.ll
+++ b/llvm/test/CodeGen/X86/masked_store.ll
@@ -466,7 +466,7 @@ define void @store_v2f32_i2(i2 %trigger, ptr %addr, <2 x float> %val) nounwind {
 ; AVX1OR2-NEXT:    movl %edi, %eax
 ; AVX1OR2-NEXT:    andb $2, %al
 ; AVX1OR2-NEXT:    shrb %al
-; AVX1OR2-NEXT:    andb $1, %dil
+; AVX1OR2-NEXT:    andl $1, %edi
 ; AVX1OR2-NEXT:    vmovd %edi, %xmm1
 ; AVX1OR2-NEXT:    vpinsrb $8, %eax, %xmm1, %xmm1
 ; AVX1OR2-NEXT:    vinsertps {{.*#+}} xmm1 = xmm1[0,2],zero,zero
diff --git a/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-innerouter.ll b/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-innerouter.ll
index 9a8719f9a64fa..7b53f2b22b08c 100644
--- a/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-innerouter.ll
+++ b/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-innerouter.ll
@@ -39,7 +39,7 @@ define i16 @out16_constmask(i16 %x, i16 %y) {
 ; CHECK-NOBMI-NEXT:    # kill: def $esi killed $esi def $rsi
 ; CHECK-NOBMI-NEXT:    # kill: def $edi killed $edi def $rdi
 ; CHECK-NOBMI-NEXT:    andl $4080, %edi # imm = 0xFF0
-; CHECK-NOBMI-NEXT:    andl $-4081, %esi # imm = 0xF00F
+; CHECK-NOBMI-NEXT:    andl $61455, %esi # imm = 0xF00F
 ; CHECK-NOBMI-NEXT:    leal (%rsi,%rdi), %eax
 ; CHECK-NOBMI-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CHECK-NOBMI-NEXT:    retq
@@ -49,7 +49,7 @@ define i16 @out16_constmask(i16 %x, i16 %y) {
 ; CHECK-BMI-NEXT:    # kill: def $esi killed $esi def $rsi
 ; CHECK-BMI-NEXT:    # kill: def $edi killed $edi def $rdi
 ; CHECK-BMI-NEXT:    andl $4080, %edi # imm = 0xFF0
-; CHECK-BMI-NEXT:    andl $-4081, %esi # imm = 0xF00F
+; CHECK-BMI-NEXT:    andl $61455, %esi # imm = 0xF00F
 ; CHECK-BMI-NEXT:    leal (%rsi,%rdi), %eax
 ; CHECK-BMI-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CHECK-BMI-NEXT:    retq
diff --git a/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-interleavedbits.ll b/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-interleavedbits.ll
index c4c4e5ed1fdde..c58f110fe9e64 100644
--- a/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-interleavedbits.ll
+++ b/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-interleavedbits.ll
@@ -39,7 +39,7 @@ define i16 @out16_constmask(i16 %x, i16 %y) {
 ; CHECK-NOBMI-NEXT:    # kill: def $esi killed $esi def $rsi
 ; CHECK-NOBMI-NEXT:    # kill: def $edi killed $edi def $rdi
 ; CHECK-NOBMI-NEXT:    andl $21845, %edi # imm = 0x5555
-; CHECK-NOBMI-NEXT:    andl $-21846, %esi # imm = 0xAAAA
+; CHECK-NOBMI-NEXT:    andl $43690, %esi # imm = 0xAAAA
 ; CHECK-NOBMI-NEXT:    leal (%rsi,%rdi), %eax
 ; CHECK-NOBMI-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CHECK-NOBMI-NEXT:    retq
@@ -49,7 +49,7 @@ define i16 @out16_constmask(i16 %x, i16 %y) {
 ; CHECK-BMI-NEXT:    # kill: def $esi killed $esi def $rsi
 ; CHECK-BMI-NEXT:    # kill: def $edi killed $edi def $rdi
 ; CHECK-BMI-NEXT:    andl $21845, %edi # imm = 0x5555
-; CHECK-BMI-NEXT:    andl $-21846, %esi # imm = 0xAAAA
+; CHECK-BMI-NEXT:    andl $43690, %esi # imm = 0xAAAA
 ; CHECK-BMI-NEXT:    leal (%rsi,%rdi), %eax
 ; CHECK-BMI-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CHECK-BMI-NEXT:    retq
diff --git a/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-interleavedbytehalves.ll b/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-interleavedbytehalves.ll
index 2ea74f3942387..f5dbc51fe518f 100644
--- a/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-interleavedbytehalves.ll
+++ b/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-interleavedbytehalves.ll
@@ -39,7 +39,7 @@ define i16 @out16_constmask(i16 %x, i16 %y) {
 ; CHECK-NOBMI-NEXT:    # kill: def $esi killed $esi def $rsi
 ; CHECK-NOBMI-NEXT:    # kill: def $edi killed $edi def $rdi
 ; CHECK-NOBMI-NEXT:    andl $3855, %edi # imm = 0xF0F
-; CHECK-NOBMI-NEXT:    andl $-3856, %esi # imm = 0xF0F0
+; CHECK-NOBMI-NEXT:    andl $61680, %esi # imm = 0xF0F0
 ; CHECK-NOBMI-NEXT:    leal (%rsi,%rdi), %eax
 ; CHECK-NOBMI-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CHECK-NOBMI-NEXT:    retq
@@ -49,7 +49,7 @@ define i16 @out16_constmask(i16 %x, i16 %y) {
 ; CHECK-BMI-NEXT:    # kill: def $esi killed $esi def $rsi
 ; CHECK-BMI-NEXT:    # kill: def $edi killed $edi def $rdi
 ; CHECK-BMI-NEXT:    andl $3855, %edi # imm = 0xF0F
-; CHECK-BMI-NEXT:    andl $-3856, %esi # imm = 0xF0F0
+; CHECK-BMI-NEXT:    andl $61680, %esi # imm = 0xF0F0
 ; CHECK-BMI-NEXT:    leal (%rsi,%rdi), %eax
 ; CHECK-BMI-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CHECK-BMI-NEXT:    retq
diff --git a/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-lowhigh.ll b/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-lowhigh.ll
index eb6accd3e623b..9b85a21dd23bc 100644
--- a/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-lowhigh.ll
+++ b/llvm/test/CodeGen/X86/unfold-masked-merge-scalar-constmask-lowhigh.ll
@@ -37,7 +37,7 @@ define i16 @out16_constmask(i16 %x, i16 %y) {
 ; CHECK-NOBMI-LABEL: out16_constmask:
 ; CHECK-NOBMI:       # %bb.0:
 ; CHECK-NOBMI-NEXT:    movzbl %dil, %eax
-; CHECK-NOBMI-NEXT:    andl $-256, %esi
+; CHECK-NOBMI-NEXT:    andl $65280, %esi # imm = 0xFF00
 ; CHECK-NOBMI-NEXT:    orl %esi, %eax
 ; CHECK-NOBMI-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CHECK-NOBMI-NEXT:    retq
@@ -45,7 +45,7 @@ define i16 @out16_constmask(i16 %x, i16 %y) {
 ; CHECK-BMI-LABEL: out16_constmask:
 ; CHECK-BMI:       # %bb.0:
 ; CHECK-BMI-NEXT:    movzbl %dil, %eax
-; CHECK-BMI-NEXT:    andl $-256, %esi
+; CHECK-BMI-NEXT:    andl $65280, %esi # imm = 0xFF00
 ; CHECK-BMI-NEXT:    orl %esi, %eax
 ; CHECK-BMI-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CHECK-BMI-NEXT:    retq

>From c19e407fd0d1fec9b7f88e3f2ffa4e251a617536 Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Thu, 28 May 2026 20:18:27 +0530
Subject: [PATCH 03/11] [DAGCombine] Fix RISCV regressions in fold
 any-extend(and(trunc(x), C))

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp |  7 ++++--
 llvm/test/CodeGen/RISCV/and.ll                |  9 +++----
 llvm/test/CodeGen/RISCV/pr142004.ll           |  4 +--
 llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll  | 25 ++++---------------
 4 files changed, 14 insertions(+), 31 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index bac76620fc77d..d13c0af5c29b5 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -16163,12 +16163,15 @@ SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
     return DAG.getAnyExtOrTrunc(N0.getOperand(0), DL, VT);
 
   // Fold (aext (and (trunc x), cst)) -> (and x, cst)
-  // if either of the casts is not free.
+  // if either of the casts is not free, and sign-extending the narrow type is
+  // not cheaper than zero-extending it (which would indicate the target prefers
+  // to keep operations at the narrower width).
   if (N0.getOpcode() == ISD::AND &&
       N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
       N0.getOperand(1).getOpcode() == ISD::Constant &&
       (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0), N0.getValueType()) ||
-       !TLI.isZExtFree(N0.getValueType(), VT))) {
+       (!TLI.isZExtFree(N0.getValueType(), VT) &&
+        !TLI.isSExtCheaperThanZExt(N0.getValueType(), VT)))) {
     SDValue X = DAG.getAnyExtOrTrunc(N0.getOperand(0).getOperand(0), DL, VT);
     APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
     return DAG.getNode(ISD::AND, DL, VT, X, DAG.getConstant(Mask, DL, VT));
diff --git a/llvm/test/CodeGen/RISCV/and.ll b/llvm/test/CodeGen/RISCV/and.ll
index 2b0b10ac57ff6..31c63c7f9b18f 100644
--- a/llvm/test/CodeGen/RISCV/and.ll
+++ b/llvm/test/CodeGen/RISCV/and.ll
@@ -99,8 +99,8 @@ define i32 @and32_0xfffff000(i32 %x) {
 ;
 ; RV64I-LABEL: and32_0xfffff000:
 ; RV64I:       # %bb.0:
-; RV64I-NEXT:    srliw a0, a0, 12
-; RV64I-NEXT:    slli a0, a0, 12
+; RV64I-NEXT:    lui a1, 1048575
+; RV64I-NEXT:    and a0, a0, a1
 ; RV64I-NEXT:    ret
   %a = and i32 %x, -4096
   ret i32 %a
@@ -114,10 +114,7 @@ define i32 @and32_0xfffffa00(i32 %x) {
 ;
 ; RV64I-LABEL: and32_0xfffffa00:
 ; RV64I:       # %bb.0:
-; RV64I-NEXT:    li a1, 1
-; RV64I-NEXT:    slli a1, a1, 32
-; RV64I-NEXT:    addi a1, a1, -1536
-; RV64I-NEXT:    and a0, a0, a1
+; RV64I-NEXT:    andi a0, a0, -1536
 ; RV64I-NEXT:    ret
   %a = and i32 %x, -1536
   ret i32 %a
diff --git a/llvm/test/CodeGen/RISCV/pr142004.ll b/llvm/test/CodeGen/RISCV/pr142004.ll
index 49173ca11d059..709644e49e704 100644
--- a/llvm/test/CodeGen/RISCV/pr142004.ll
+++ b/llvm/test/CodeGen/RISCV/pr142004.ll
@@ -11,14 +11,12 @@ define i32 @foo(i32 %x) {
 ; CHECK-NEXT:    lui a1, %hi(f)
 ; CHECK-NEXT:    lui a2, %hi(d)
 ; CHECK-NEXT:    lbu a1, %lo(f)(a1)
-; CHECK-NEXT:    ld a2, %lo(d)(a2)
+; CHECK-NEXT:    lhu a2, %lo(d)(a2)
 ; CHECK-NEXT:    slli a0, a0, 48
 ; CHECK-NEXT:    srli a3, a0, 48
 ; CHECK-NEXT:    xori a0, a1, 255
 ; CHECK-NEXT:    or a0, a0, a2
 ; CHECK-NEXT:    lui a1, %hi(e)
-; CHECK-NEXT:    slli a0, a0, 48
-; CHECK-NEXT:    srli a0, a0, 48
 ; CHECK-NEXT:    sw a3, %lo(e)(a1)
 ; CHECK-NEXT:    ret
 entry:
diff --git a/llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll b/llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll
index e57a47e9bc0c0..751b05ebdba2a 100644
--- a/llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll
+++ b/llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll
@@ -9,26 +9,11 @@
 ; RUN:   | FileCheck %s --check-prefixes=CHECK,RV64,ZBS,ZBS64
 
 define i32 @and0xabcdefff(i32 %x) {
-; RV32-LABEL: and0xabcdefff:
-; RV32:       # %bb.0:
-; RV32-NEXT:    lui a1, 344865
-; RV32-NEXT:    andn a0, a0, a1
-; RV32-NEXT:    ret
-;
-; NOZBS64-LABEL: and0xabcdefff:
-; NOZBS64:       # %bb.0:
-; NOZBS64-NEXT:    lui a1, 1048404
-; NOZBS64-NEXT:    addi a1, a1, 801
-; NOZBS64-NEXT:    slli a1, a1, 12
-; NOZBS64-NEXT:    andn a0, a0, a1
-; NOZBS64-NEXT:    ret
-;
-; ZBS64-LABEL: and0xabcdefff:
-; ZBS64:       # %bb.0:
-; ZBS64-NEXT:    lui a1, 869153
-; ZBS64-NEXT:    bclri a1, a1, 31
-; ZBS64-NEXT:    andn a0, a0, a1
-; ZBS64-NEXT:    ret
+; CHECK-LABEL: and0xabcdefff:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    lui a1, 344865
+; CHECK-NEXT:    andn a0, a0, a1
+; CHECK-NEXT:    ret
   %and = and i32 %x, -1412567041
   ret i32 %and
 }

>From 945dc5f00d2625736aaf19df233103b3186fb6f5 Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Fri, 29 May 2026 06:06:44 +0530
Subject: [PATCH 04/11] [DAGCombine] Fix incorrect test comments in
 aext-and-trunc-fold.ll

---
 llvm/test/CodeGen/X86/aext-and-trunc-fold.ll | 54 +++++---------------
 1 file changed, 12 insertions(+), 42 deletions(-)

diff --git a/llvm/test/CodeGen/X86/aext-and-trunc-fold.ll b/llvm/test/CodeGen/X86/aext-and-trunc-fold.ll
index 71d5b0aba250b..5b01be813ed7a 100644
--- a/llvm/test/CodeGen/X86/aext-and-trunc-fold.ll
+++ b/llvm/test/CodeGen/X86/aext-and-trunc-fold.ll
@@ -2,50 +2,20 @@
 ; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s --check-prefix=X64
 ; RUN: llc < %s -mtriple=i686-unknown-linux-gnu | FileCheck %s --check-prefix=X86
 
-; Test for GH#195575: fold (aext (and (trunc x) C)) -> (and x C)
-; when either the truncate or zero-extend is not free.
-; Previously visitANY_EXTEND only checked isTruncateFree (which is true on X86
-; for i32->i8), missing the case where isZExtFree is false (i8->i32 on X86).
-; This caused "and al, 7; movzx eax, al" instead of the optimal "and eax, 7".
-;
-; The DAGCombiner converts zero_extend to any_extend when the result is only
-; used by a shift (upper bits don't affect the output), but visitANY_EXTEND
-; was then failing to fold the (aext (and (trunc x) C)) pattern on X86 because
-; isTruncateFree(i32->i8) is true there. Adding the isZExtFree check (which is
-; false for i8->i32 on X86) makes the fold fire correctly.
-
-; Pattern that directly triggers the bug: shl context converts zext->aext before
-; the fold can run, and then visitANY_EXTEND needs the isZExtFree check.
-define i32 @aext_and_trunc_shl(i32 %x) {
-; X64-LABEL: aext_and_trunc_shl:
-; X64:       # %bb.0:
-; X64-NEXT:    # kill: def $edi killed $edi def $rdi
-; X64-NEXT:    andl $7, %edi
-; X64-NEXT:    leal (%rdi,%rdi), %eax
-; X64-NEXT:    retq
-;
-; X86-LABEL: aext_and_trunc_shl:
-; X86:       # %bb.0:
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
-; X86-NEXT:    andl $7, %eax
-; X86-NEXT:    addl %eax, %eax
-; X86-NEXT:    retl
-  %trunc = trunc i32 %x to i8
-  %and = and i8 %trunc, 7
-  %zext = zext i8 %and to i32
-  %shl = shl i32 %zext, 1
-  ret i32 %shl
-}
+; Regression tests for the (zext (and (trunc x) C)) -> (and x C) fold in
+; visitZERO_EXTEND (and symmetrically in visitANY_EXTEND). On X86,
+; isTruncateFree(i32->i8) is true but isZExtFree(i8->i32) is false, so the
+; fold fires to avoid redundant narrow-AND + movzx sequences.
 
-; Basic (aext (and (trunc i32) C)) -> (and i32 C) pattern.
-define i32 @aext_and_trunc_i32(i32 %x) {
-; X64-LABEL: aext_and_trunc_i32:
+; Basic (zext (and (trunc i32) C)) -> (and i32 C) pattern.
+define i32 @zext_and_trunc_i32(i32 %x) {
+; X64-LABEL: zext_and_trunc_i32:
 ; X64:       # %bb.0:
 ; X64-NEXT:    movl %edi, %eax
 ; X64-NEXT:    andl $7, %eax
 ; X64-NEXT:    retq
 ;
-; X86-LABEL: aext_and_trunc_i32:
+; X86-LABEL: zext_and_trunc_i32:
 ; X86:       # %bb.0:
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; X86-NEXT:    andl $7, %eax
@@ -56,16 +26,16 @@ define i32 @aext_and_trunc_i32(i32 %x) {
   ret i32 %zext
 }
 
-; (aext (and (trunc i64) C)) -> (and i32 C) on x86-64.
-define i32 @aext_and_trunc_i64(i64 %x) {
-; X64-LABEL: aext_and_trunc_i64:
+; (zext (and (trunc i64) C)) -> (and i32 C) on x86-64.
+define i32 @zext_and_trunc_i64(i64 %x) {
+; X64-LABEL: zext_and_trunc_i64:
 ; X64:       # %bb.0:
 ; X64-NEXT:    movq %rdi, %rax
 ; X64-NEXT:    andl $15, %eax
 ; X64-NEXT:    # kill: def $eax killed $eax killed $rax
 ; X64-NEXT:    retq
 ;
-; X86-LABEL: aext_and_trunc_i64:
+; X86-LABEL: zext_and_trunc_i64:
 ; X86:       # %bb.0:
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; X86-NEXT:    andl $15, %eax

>From eba222751889d86c471d3151f002139622f15dd0 Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Fri, 29 May 2026 06:42:40 +0530
Subject: [PATCH 05/11] [DAGCombine] Add regression test for GH#195575

---
 .../X86/gh195575-and-movzx-to-and32.ll        | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll

diff --git a/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll b/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll
new file mode 100644
index 0000000000000..bb28ba712e741
--- /dev/null
+++ b/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll
@@ -0,0 +1,38 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f,+avx512bw | FileCheck %s
+
+; GH#195575: fold (and al, C; movzx eax, al) -> (and eax, C).
+;
+; When a v16i1 comparison result (in a k-register) is bitcast to i3 and then
+; zero-extended, type legalization expands this to:
+;   bitcast v16i1 -> i16, truncate i16 -> i8, and i8 C, zero_extend i8 -> i32
+;
+; The fold (zext (and (trunc x) C)) -> (and x C) in visitZERO_EXTEND (and
+; symmetrically visitANY_EXTEND) avoids the suboptimal "andb; movzbl" sequence
+; in favour of a single "andl" on the wider type.
+
+define zeroext range(i8 0, 4) i8 @gh_195575(ptr nocapture nonnull readonly align 4 %p) {
+; CHECK-LABEL: gh_195575:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    vmovd {{.*#+}} xmm0 = mem[0],zero,zero,zero
+; CHECK-NEXT:    vpbroadcastb {{.*#+}} xmm1 = [61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61]
+; CHECK-NEXT:    vpcmpneqb %zmm1, %zmm0, %k0
+; CHECK-NEXT:    kmovd %k0, %ecx
+; CHECK-NEXT:    andl $7, %ecx
+; CHECK-NEXT:    addl %ecx, %ecx
+; CHECK-NEXT:    movl $59796, %eax # imm = 0xE994
+; CHECK-NEXT:    # kill: def $cl killed $cl killed $ecx
+; CHECK-NEXT:    shrl %cl, %eax
+; CHECK-NEXT:    andl $3, %eax
+; CHECK-NEXT:    # kill: def $al killed $al killed $eax
+; CHECK-NEXT:    vzeroupper
+; CHECK-NEXT:    retq
+  %v = load <3 x i8>, ptr %p, align 4
+  %cmp = icmp ne <3 x i8> %v, splat (i8 61)
+  %bc = bitcast <3 x i1> %cmp to i3
+  %ct = tail call range(i3 0, -4) i3 @llvm.ctpop.i3(i3 %bc)
+  %ext = zext nneg i3 %ct to i8
+  ret i8 %ext
+}
+
+declare i3 @llvm.ctpop.i3(i3)

>From 44d82d5018744e5672de5a39c0f0b703856f4a4c Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Fri, 29 May 2026 14:36:37 +0530
Subject: [PATCH 06/11] Add regression test for GH#195575

---
 .../CodeGen/X86/gh195575-and-movzx-to-and32.ll     | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll b/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll
index bb28ba712e741..17c2346e606ba 100644
--- a/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll
+++ b/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll
@@ -1,7 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
-; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f,+avx512bw | FileCheck %s
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f,+avx512bw,+popcnt | FileCheck %s
 
-; GH#195575: fold (and al, C; movzx eax, al) -> (and eax, C).
+; GH#195575: fold (and al, 7; movzx eax, al) -> (and eax, 7).
 ;
 ; When a v16i1 comparison result (in a k-register) is bitcast to i3 and then
 ; zero-extended, type legalization expands this to:
@@ -17,13 +17,9 @@ define zeroext range(i8 0, 4) i8 @gh_195575(ptr nocapture nonnull readonly align
 ; CHECK-NEXT:    vmovd {{.*#+}} xmm0 = mem[0],zero,zero,zero
 ; CHECK-NEXT:    vpbroadcastb {{.*#+}} xmm1 = [61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61]
 ; CHECK-NEXT:    vpcmpneqb %zmm1, %zmm0, %k0
-; CHECK-NEXT:    kmovd %k0, %ecx
-; CHECK-NEXT:    andl $7, %ecx
-; CHECK-NEXT:    addl %ecx, %ecx
-; CHECK-NEXT:    movl $59796, %eax # imm = 0xE994
-; CHECK-NEXT:    # kill: def $cl killed $cl killed $ecx
-; CHECK-NEXT:    shrl %cl, %eax
-; CHECK-NEXT:    andl $3, %eax
+; CHECK-NEXT:    kmovd %k0, %eax
+; CHECK-NEXT:    andl $7, %eax
+; CHECK-NEXT:    popcntl %eax, %eax
 ; CHECK-NEXT:    # kill: def $al killed $al killed $eax
 ; CHECK-NEXT:    vzeroupper
 ; CHECK-NEXT:    retq

>From b845cc1fc8815ef16748e7bca6b2b790bdbd0ecb Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Fri, 29 May 2026 15:23:32 +0530
Subject: [PATCH 07/11] Add AVX512 regression test and removed unnecessary
 metadata

---
 .../test/CodeGen/X86/aext-and-trunc-avx512.ll | 25 ++++++++++++++
 .../X86/gh195575-and-movzx-to-and32.ll        | 34 -------------------
 2 files changed, 25 insertions(+), 34 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/aext-and-trunc-avx512.ll
 delete mode 100644 llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll

diff --git a/llvm/test/CodeGen/X86/aext-and-trunc-avx512.ll b/llvm/test/CodeGen/X86/aext-and-trunc-avx512.ll
new file mode 100644
index 0000000000000..2c31de54db623
--- /dev/null
+++ b/llvm/test/CodeGen/X86/aext-and-trunc-avx512.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f,+avx512bw,+popcnt | FileCheck %s
+
+; Test (zext (and (trunc x) C)) -> (and x C) fold with AVX512 mask registers.
+; Ensures "andb $7, %al; movzbl %al, %eax" is folded to "andl $7, %eax".
+
+define i8 @ctpop_aext_i3_v3i1(ptr %p) {
+; CHECK-LABEL: ctpop_aext_i3_v3i1:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    vmovd {{.*#+}} xmm0 = mem[0],zero,zero,zero
+; CHECK-NEXT:    vpbroadcastb {{.*#+}} xmm1 = [61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61]
+; CHECK-NEXT:    vpcmpneqb %zmm1, %zmm0, %k0
+; CHECK-NEXT:    kmovd %k0, %eax
+; CHECK-NEXT:    andl $7, %eax
+; CHECK-NEXT:    popcntl %eax, %eax
+; CHECK-NEXT:    # kill: def $al killed $al killed $eax
+; CHECK-NEXT:    vzeroupper
+; CHECK-NEXT:    retq
+  %v = load <3 x i8>, ptr %p
+  %cmp = icmp ne <3 x i8> %v, splat (i8 61)
+  %bc = bitcast <3 x i1> %cmp to i3
+  %ct = call i3 @llvm.ctpop.i3(i3 %bc)
+  %ext = zext i3 %ct to i8
+  ret i8 %ext
+}
diff --git a/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll b/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll
deleted file mode 100644
index 17c2346e606ba..0000000000000
--- a/llvm/test/CodeGen/X86/gh195575-and-movzx-to-and32.ll
+++ /dev/null
@@ -1,34 +0,0 @@
-; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
-; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f,+avx512bw,+popcnt | FileCheck %s
-
-; GH#195575: fold (and al, 7; movzx eax, al) -> (and eax, 7).
-;
-; When a v16i1 comparison result (in a k-register) is bitcast to i3 and then
-; zero-extended, type legalization expands this to:
-;   bitcast v16i1 -> i16, truncate i16 -> i8, and i8 C, zero_extend i8 -> i32
-;
-; The fold (zext (and (trunc x) C)) -> (and x C) in visitZERO_EXTEND (and
-; symmetrically visitANY_EXTEND) avoids the suboptimal "andb; movzbl" sequence
-; in favour of a single "andl" on the wider type.
-
-define zeroext range(i8 0, 4) i8 @gh_195575(ptr nocapture nonnull readonly align 4 %p) {
-; CHECK-LABEL: gh_195575:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    vmovd {{.*#+}} xmm0 = mem[0],zero,zero,zero
-; CHECK-NEXT:    vpbroadcastb {{.*#+}} xmm1 = [61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61]
-; CHECK-NEXT:    vpcmpneqb %zmm1, %zmm0, %k0
-; CHECK-NEXT:    kmovd %k0, %eax
-; CHECK-NEXT:    andl $7, %eax
-; CHECK-NEXT:    popcntl %eax, %eax
-; CHECK-NEXT:    # kill: def $al killed $al killed $eax
-; CHECK-NEXT:    vzeroupper
-; CHECK-NEXT:    retq
-  %v = load <3 x i8>, ptr %p, align 4
-  %cmp = icmp ne <3 x i8> %v, splat (i8 61)
-  %bc = bitcast <3 x i1> %cmp to i3
-  %ct = tail call range(i3 0, -4) i3 @llvm.ctpop.i3(i3 %bc)
-  %ext = zext nneg i3 %ct to i8
-  ret i8 %ext
-}
-
-declare i3 @llvm.ctpop.i3(i3)

>From 4d71da637c274830cf1d213eb560eddae5844e40 Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Fri, 29 May 2026 23:28:40 +0530
Subject: [PATCH 08/11] [DAG] Fix (zext (and (bitcast v8i1) C)) fold for
 AVX512DQ

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 23 +++++++++++
 .../test/CodeGen/X86/aext-and-trunc-avx512.ll | 40 +++++++++++++------
 2 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 5c585e64b8ddf..0617976dfbc5a 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -17702,6 +17702,29 @@ SDValue DAGCombiner::visitBITCAST(SDNode *N) {
   if (N0.getOpcode() == ISD::BITCAST)
     return DAG.getBitcast(VT, N0.getOperand(0));
 
+  // fold (bitcast (extract_subvector vNi1, 0) -> iK)
+  //   -> (truncate (bitcast vNi1 -> iN) -> iK)
+  // This canonicalises the AVX512DQ mask-narrowing path into the same
+  // trunc+bitcast form used without AVX512DQ, allowing the existing
+  // (zext (and (trunc x) C)) -> (and x C) fold to fire.
+  // Only apply after type legalization so that vNi1 types are only seen
+  // when they are genuinely legal (e.g. AVX512 k-registers), not as
+  // temporary pre-legalization nodes on non-AVX512 targets.
+  if (LegalTypes && N0.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
+      N0.getConstantOperandVal(1) == 0 && VT.isInteger() && !VT.isVector()) {
+    EVT SrcVT = N0.getOperand(0).getValueType();
+    if (SrcVT.isVector() &&
+        SrcVT.getVectorElementType() == MVT::i1 &&
+        VT.getSizeInBits() <= SrcVT.getSizeInBits()) {
+      SDLoc DL(N);
+      EVT WideIntVT =
+          EVT::getIntegerVT(*DAG.getContext(), SrcVT.getSizeInBits());
+      SDValue WideBitcast =
+          DAG.getNode(ISD::BITCAST, DL, WideIntVT, N0.getOperand(0));
+      return DAG.getNode(ISD::TRUNCATE, DL, VT, WideBitcast);
+    }
+  }
+
   // fold (conv (logicop (conv x), (c))) -> (logicop x, (conv c))
   // iff the current bitwise logicop type isn't legal
   if (ISD::isBitwiseLogicOp(N0.getOpcode()) && VT.isInteger() &&
diff --git a/llvm/test/CodeGen/X86/aext-and-trunc-avx512.ll b/llvm/test/CodeGen/X86/aext-and-trunc-avx512.ll
index 2c31de54db623..78f525e4637bc 100644
--- a/llvm/test/CodeGen/X86/aext-and-trunc-avx512.ll
+++ b/llvm/test/CodeGen/X86/aext-and-trunc-avx512.ll
@@ -1,21 +1,37 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
-; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f,+avx512bw,+popcnt | FileCheck %s
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f,+avx512bw,+popcnt | FileCheck %s --check-prefix=BW
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f,+avx512bw,+avx512dq,+popcnt | FileCheck %s --check-prefix=DQ
 
 ; Test (zext (and (trunc x) C)) -> (and x C) fold with AVX512 mask registers.
 ; Ensures "andb $7, %al; movzbl %al, %eax" is folded to "andl $7, %eax".
+; Without AVX512DQ: bitcast v16i1->i16 + truncate i16->i8 (TRUNCATE path).
+; With    AVX512DQ: extract_subvector v16i1->v8i1 + bitcast v8i1->i8, which
+;   visitBITCAST canonicalises to the same truncate form before the fold fires.
 
 define i8 @ctpop_aext_i3_v3i1(ptr %p) {
-; CHECK-LABEL: ctpop_aext_i3_v3i1:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    vmovd {{.*#+}} xmm0 = mem[0],zero,zero,zero
-; CHECK-NEXT:    vpbroadcastb {{.*#+}} xmm1 = [61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61]
-; CHECK-NEXT:    vpcmpneqb %zmm1, %zmm0, %k0
-; CHECK-NEXT:    kmovd %k0, %eax
-; CHECK-NEXT:    andl $7, %eax
-; CHECK-NEXT:    popcntl %eax, %eax
-; CHECK-NEXT:    # kill: def $al killed $al killed $eax
-; CHECK-NEXT:    vzeroupper
-; CHECK-NEXT:    retq
+; BW-LABEL: ctpop_aext_i3_v3i1:
+; BW:       # %bb.0:
+; BW-NEXT:    vmovd {{.*#+}} xmm0 = mem[0],zero,zero,zero
+; BW-NEXT:    vpbroadcastb {{.*#+}} xmm1 = [61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61]
+; BW-NEXT:    vpcmpneqb %zmm1, %zmm0, %k0
+; BW-NEXT:    kmovd %k0, %eax
+; BW-NEXT:    andl $7, %eax
+; BW-NEXT:    popcntl %eax, %eax
+; BW-NEXT:    # kill: def $al killed $al killed $eax
+; BW-NEXT:    vzeroupper
+; BW-NEXT:    retq
+;
+; DQ-LABEL: ctpop_aext_i3_v3i1:
+; DQ:       # %bb.0:
+; DQ-NEXT:    vmovd {{.*#+}} xmm0 = mem[0],zero,zero,zero
+; DQ-NEXT:    vpbroadcastb {{.*#+}} xmm1 = [61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61]
+; DQ-NEXT:    vpcmpneqb %zmm1, %zmm0, %k0
+; DQ-NEXT:    kmovd %k0, %eax
+; DQ-NEXT:    andl $7, %eax
+; DQ-NEXT:    popcntl %eax, %eax
+; DQ-NEXT:    # kill: def $al killed $al killed $eax
+; DQ-NEXT:    vzeroupper
+; DQ-NEXT:    retq
   %v = load <3 x i8>, ptr %p
   %cmp = icmp ne <3 x i8> %v, splat (i8 61)
   %bc = bitcast <3 x i1> %cmp to i3

>From f3776699df4712755d565234d4f77aa84599d335 Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Fri, 29 May 2026 23:45:00 +0530
Subject: [PATCH 09/11] [DAG] clang-format visitBITCAST fold condition

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 0617976dfbc5a..7ff941e46e98b 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -17713,8 +17713,7 @@ SDValue DAGCombiner::visitBITCAST(SDNode *N) {
   if (LegalTypes && N0.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
       N0.getConstantOperandVal(1) == 0 && VT.isInteger() && !VT.isVector()) {
     EVT SrcVT = N0.getOperand(0).getValueType();
-    if (SrcVT.isVector() &&
-        SrcVT.getVectorElementType() == MVT::i1 &&
+    if (SrcVT.isVector() && SrcVT.getVectorElementType() == MVT::i1 &&
         VT.getSizeInBits() <= SrcVT.getSizeInBits()) {
       SDLoc DL(N);
       EVT WideIntVT =

>From 71db2876d188773d477e2bf94712382e43da1f15 Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Sat, 30 May 2026 00:47:01 +0530
Subject: [PATCH 10/11] [DAG] Guard vNi1 bitcast fold against scalable vectors
 and illegal types

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 95 +++++++++++--------
 1 file changed, 56 insertions(+), 39 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 7ff941e46e98b..b73a1bd894e8d 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -15910,16 +15910,36 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
 
   // Fold (zext (and (trunc x), cst)) -> (and x, cst),
   // if either of the casts is not free.
+  // Also handles (zext (and (bitcast (extract_subvector vNi1, 0)) cst))
+  // by treating the bitcast+extract as equivalent to a truncate of the
+  // wider bitcast, e.g. on AVX512DQ where v8i1 extract replaces truncate.
   if (N0.getOpcode() == ISD::AND &&
-      N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
-      N0.getOperand(1).getOpcode() == ISD::Constant &&
-      (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0), N0.getValueType()) ||
-       !TLI.isZExtFree(N0.getValueType(), VT))) {
-    SDValue X = N0.getOperand(0).getOperand(0);
-    X = DAG.getAnyExtOrTrunc(X, SDLoc(X), VT);
-    APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
-    return DAG.getNode(ISD::AND, DL, VT,
-                       X, DAG.getConstant(Mask, DL, VT));
+      N0.getOperand(1).getOpcode() == ISD::Constant) {
+    SDValue AndSrc = N0.getOperand(0);
+    SDValue X;
+    if (AndSrc.getOpcode() == ISD::TRUNCATE) {
+      X = AndSrc.getOperand(0);
+    } else if (AndSrc.getOpcode() == ISD::BITCAST &&
+               AndSrc.getOperand(0).getOpcode() == ISD::EXTRACT_SUBVECTOR &&
+               AndSrc.getOperand(0).getConstantOperandVal(1) == 0) {
+      // (bitcast (extract_subvector vNi1, 0) -> iK) is equivalent to
+      // (truncate (bitcast vNi1 -> iN) -> iK); use the wider vNi1 as X.
+      SDValue Src = AndSrc.getOperand(0).getOperand(0);
+      EVT SrcVT = Src.getValueType();
+      if (SrcVT.isVector() && !SrcVT.isScalableVT() &&
+          SrcVT.getVectorElementType() == MVT::i1) {
+        EVT WideIntVT =
+            EVT::getIntegerVT(*DAG.getContext(), SrcVT.getSizeInBits());
+        if (TLI.isTypeLegal(WideIntVT))
+          X = DAG.getBitcast(WideIntVT, Src);
+      }
+    }
+    if (X && (!TLI.isTruncateFree(X, N0.getValueType()) ||
+              !TLI.isZExtFree(N0.getValueType(), VT))) {
+      X = DAG.getAnyExtOrTrunc(X, SDLoc(X), VT);
+      APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
+      return DAG.getNode(ISD::AND, DL, VT, X, DAG.getConstant(Mask, DL, VT));
+    }
   }
 
   // Try to simplify (zext (load x)).
@@ -16166,15 +16186,34 @@ SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
   // if either of the casts is not free, and sign-extending the narrow type is
   // not cheaper than zero-extending it (which would indicate the target prefers
   // to keep operations at the narrower width).
+  // Also handles (aext (and (bitcast (extract_subvector vNi1, 0)) cst))
+  // which arises on AVX512DQ where v8i1 extract replaces truncate.
   if (N0.getOpcode() == ISD::AND &&
-      N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
-      N0.getOperand(1).getOpcode() == ISD::Constant &&
-      (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0), N0.getValueType()) ||
-       (!TLI.isZExtFree(N0.getValueType(), VT) &&
-        !TLI.isSExtCheaperThanZExt(N0.getValueType(), VT)))) {
-    SDValue X = DAG.getAnyExtOrTrunc(N0.getOperand(0).getOperand(0), DL, VT);
-    APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
-    return DAG.getNode(ISD::AND, DL, VT, X, DAG.getConstant(Mask, DL, VT));
+      N0.getOperand(1).getOpcode() == ISD::Constant) {
+    SDValue AndSrc = N0.getOperand(0);
+    SDValue X;
+    if (AndSrc.getOpcode() == ISD::TRUNCATE) {
+      X = AndSrc.getOperand(0);
+    } else if (AndSrc.getOpcode() == ISD::BITCAST &&
+               AndSrc.getOperand(0).getOpcode() == ISD::EXTRACT_SUBVECTOR &&
+               AndSrc.getOperand(0).getConstantOperandVal(1) == 0) {
+      SDValue Src = AndSrc.getOperand(0).getOperand(0);
+      EVT SrcVT = Src.getValueType();
+      if (SrcVT.isVector() && !SrcVT.isScalableVT() &&
+          SrcVT.getVectorElementType() == MVT::i1) {
+        EVT WideIntVT =
+            EVT::getIntegerVT(*DAG.getContext(), SrcVT.getSizeInBits());
+        if (TLI.isTypeLegal(WideIntVT))
+          X = DAG.getBitcast(WideIntVT, Src);
+      }
+    }
+    if (X && (!TLI.isTruncateFree(X, N0.getValueType()) ||
+              (!TLI.isZExtFree(N0.getValueType(), VT) &&
+               !TLI.isSExtCheaperThanZExt(N0.getValueType(), VT)))) {
+      X = DAG.getAnyExtOrTrunc(X, DL, VT);
+      APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
+      return DAG.getNode(ISD::AND, DL, VT, X, DAG.getConstant(Mask, DL, VT));
+    }
   }
 
   // fold (aext (load x)) -> (aext (truncate (extload x)))
@@ -17702,28 +17741,6 @@ SDValue DAGCombiner::visitBITCAST(SDNode *N) {
   if (N0.getOpcode() == ISD::BITCAST)
     return DAG.getBitcast(VT, N0.getOperand(0));
 
-  // fold (bitcast (extract_subvector vNi1, 0) -> iK)
-  //   -> (truncate (bitcast vNi1 -> iN) -> iK)
-  // This canonicalises the AVX512DQ mask-narrowing path into the same
-  // trunc+bitcast form used without AVX512DQ, allowing the existing
-  // (zext (and (trunc x) C)) -> (and x C) fold to fire.
-  // Only apply after type legalization so that vNi1 types are only seen
-  // when they are genuinely legal (e.g. AVX512 k-registers), not as
-  // temporary pre-legalization nodes on non-AVX512 targets.
-  if (LegalTypes && N0.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
-      N0.getConstantOperandVal(1) == 0 && VT.isInteger() && !VT.isVector()) {
-    EVT SrcVT = N0.getOperand(0).getValueType();
-    if (SrcVT.isVector() && SrcVT.getVectorElementType() == MVT::i1 &&
-        VT.getSizeInBits() <= SrcVT.getSizeInBits()) {
-      SDLoc DL(N);
-      EVT WideIntVT =
-          EVT::getIntegerVT(*DAG.getContext(), SrcVT.getSizeInBits());
-      SDValue WideBitcast =
-          DAG.getNode(ISD::BITCAST, DL, WideIntVT, N0.getOperand(0));
-      return DAG.getNode(ISD::TRUNCATE, DL, VT, WideBitcast);
-    }
-  }
-
   // fold (conv (logicop (conv x), (c))) -> (logicop x, (conv c))
   // iff the current bitwise logicop type isn't legal
   if (ISD::isBitwiseLogicOp(N0.getOpcode()) && VT.isInteger() &&

>From 5b6377586667b1162bbab0c8389321786a7fafcf Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Mon, 1 Jun 2026 16:30:07 +0530
Subject: [PATCH 11/11] [DAG] Use isFixedLengthVectorOf and rename test file

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp               | 6 ++----
 .../X86/{aext-and-trunc-fold.ll => aext-and-trunc.ll}       | 0
 2 files changed, 2 insertions(+), 4 deletions(-)
 rename llvm/test/CodeGen/X86/{aext-and-trunc-fold.ll => aext-and-trunc.ll} (100%)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index b73a1bd894e8d..354a691181e51 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -15926,8 +15926,7 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
       // (truncate (bitcast vNi1 -> iN) -> iK); use the wider vNi1 as X.
       SDValue Src = AndSrc.getOperand(0).getOperand(0);
       EVT SrcVT = Src.getValueType();
-      if (SrcVT.isVector() && !SrcVT.isScalableVT() &&
-          SrcVT.getVectorElementType() == MVT::i1) {
+      if (SrcVT.isFixedLengthVectorOf(MVT::i1)) {
         EVT WideIntVT =
             EVT::getIntegerVT(*DAG.getContext(), SrcVT.getSizeInBits());
         if (TLI.isTypeLegal(WideIntVT))
@@ -16199,8 +16198,7 @@ SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
                AndSrc.getOperand(0).getConstantOperandVal(1) == 0) {
       SDValue Src = AndSrc.getOperand(0).getOperand(0);
       EVT SrcVT = Src.getValueType();
-      if (SrcVT.isVector() && !SrcVT.isScalableVT() &&
-          SrcVT.getVectorElementType() == MVT::i1) {
+      if (SrcVT.isFixedLengthVectorOf(MVT::i1)) {
         EVT WideIntVT =
             EVT::getIntegerVT(*DAG.getContext(), SrcVT.getSizeInBits());
         if (TLI.isTypeLegal(WideIntVT))
diff --git a/llvm/test/CodeGen/X86/aext-and-trunc-fold.ll b/llvm/test/CodeGen/X86/aext-and-trunc.ll
similarity index 100%
rename from llvm/test/CodeGen/X86/aext-and-trunc-fold.ll
rename to llvm/test/CodeGen/X86/aext-and-trunc.ll



More information about the llvm-commits mailing list