[llvm] [VectorCombine] Fold concat(binop(a,c), binop(b,d)) -> binop(concat(a,b), concat(c,d)) for i1 vectors (PR #206087)
Aayush Shrivastava via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 26 07:26:30 PDT 2026
https://github.com/iamaayushrivastava created https://github.com/llvm/llvm-project/pull/206087
Fixes #205707
`VectorCombine::foldShuffleOfBinops` rewrites `shuffle(binop(a,c), binop(b,d))` into `binop(shuffle(a,b), shuffle(c,d))` but only when the new form is strictly cheaper. For concat shuffles of i1 vectors, both forms have equal cost, so the transform was silently rejected.
This fix allows the equal-cost transform when both binops are single-use `BinaryOperators` (not icmps, to avoid widening narrow AVX-512 comparisons), canonicalising the IR to a wider binop.
The motivating case is AVX-512 mask operations: `NOT(concat(XOR(a,c), XOR(b,d)))` becomes `NOT(XOR(concat(a,b), concat(c,d)))`, which the backend naturally selects as `kxnorq`, reducing `kxord + kxord + kunpckdq + knotq` (4 instructions) to `kunpckdq + kunpckdq + kxnorq` (3 instructions).
>From 6d676aaa1a15bd85cfb452f78dc74f574478670a Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Fri, 26 Jun 2026 19:49:54 +0530
Subject: [PATCH] [VectorCombine] Fold concat(binop(a,c), binop(b,d)) ->
binop(concat(a,b), concat(c,d)) for i1 vectors
---
.../Transforms/Vectorize/VectorCombine.cpp | 11 +++++
llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll | 49 +++++++++++++++++++
2 files changed, 60 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll
diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index ef467b4c91aff..c557a58e5c90a 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -2673,6 +2673,17 @@ bool VectorCombine::foldShuffleOfBinops(Instruction &I) {
// original binop(s). If binops have multiple uses, they won't be eliminated.
ReducedInstCount |= SingleSrcBinOp && LHS->hasOneUser() && RHS->hasOneUser();
+ // For concat shuffles of i1 vectors where both binops are one-use, the
+ // transform keeps the same instruction count but canonicalises to a single
+ // wider binop, enabling downstream folds (e.g. NOT(XOR(concat(a,b),
+ // concat(c,d))) -> XNOR(concat(a,b),concat(c,d)) on AVX-512 mask regs).
+ // Restrict to BinaryOperator (not CmpInst) since narrow comparisons may
+ // be cheaper than wide ones on some targets (e.g. AVX-512 vpcmpeq).
+ ReducedInstCount |= cast<ShuffleVectorInst>(&I)->isConcat() &&
+ I.getType()->getScalarType()->isIntegerTy(1) &&
+ isa<BinaryOperator>(LHS) && LHS->hasOneUser() &&
+ RHS->hasOneUser();
+
auto *ShuffleCmpTy =
FixedVectorType::get(BinOpTy->getElementType(), ShuffleDstTy);
InstructionCost NewCost = TTI.getShuffleCost(
diff --git a/llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll b/llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll
new file mode 100644
index 0000000000000..8cd6b058b78a1
--- /dev/null
+++ b/llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll
@@ -0,0 +1,49 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512bw | FileCheck %s
+
+; Check that ~kunpckdq(a^c, b^d) is lowered as kunpckdq(a^~c, b^~d)
+; using kxnorq instead of kxorq + knotq.
+; The VectorCombine foldShuffleOfBinops pass first rewrites
+; shuffle(xor(a,c), xor(b,d)) -> xor(shuffle(a,b), shuffle(c,d))
+; after which the backend naturally selects kxnorq for NOT(XOR(wide,wide)).
+; See https://github.com/llvm/llvm-project/issues/205707
+
+define <64 x i1> @not_kunpckdq_xor(ptr align 4 %p0, ptr align 4 %p1,
+ ptr align 4 %p2, ptr align 4 %p3) {
+; CHECK-LABEL: not_kunpckdq_xor:
+; CHECK: # %bb.0:
+; CHECK-NEXT: kmovd (%rdi), %k0
+; CHECK-NEXT: kmovd (%rsi), %k1
+; CHECK-NEXT: kmovd (%rdx), %k2
+; CHECK-NEXT: kmovd (%rcx), %k3
+; CHECK-NEXT: kunpckdq %k0, %k1, %k0
+; CHECK-NEXT: kunpckdq %k2, %k3, %k1
+; CHECK-NEXT: kxnorq %k1, %k0, %k0
+; CHECK-NEXT: vpmovm2b %k0, %zmm0
+; CHECK-NEXT: retq
+ %a = load <32 x i1>, ptr %p0, align 4
+ %b = load <32 x i1>, ptr %p1, align 4
+ %c = load <32 x i1>, ptr %p2, align 4
+ %d = load <32 x i1>, ptr %p3, align 4
+ %concat_ab = shufflevector <32 x i1> %a, <32 x i1> %b,
+ <64 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7,
+ i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15,
+ i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23,
+ i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31,
+ i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39,
+ i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47,
+ i32 48, i32 49, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55,
+ i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63>
+ %concat_cd = shufflevector <32 x i1> %c, <32 x i1> %d,
+ <64 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7,
+ i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15,
+ i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23,
+ i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31,
+ i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39,
+ i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47,
+ i32 48, i32 49, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55,
+ i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63>
+ %xor = xor <64 x i1> %concat_ab, %concat_cd
+ %r = xor <64 x i1> %xor, splat (i1 true)
+ ret <64 x i1> %r
+}
More information about the llvm-commits
mailing list