[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
Sun Jul 5 11:58:52 PDT 2026


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

>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 1/2] [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
+}

>From 8f51e6195e62b7da72fc40175334ca84534a2ef3 Mon Sep 17 00:00:00 2001
From: iamaayushrivastava <iamaayushrivastava at gmail.com>
Date: Mon, 6 Jul 2026 00:28:33 +0530
Subject: [PATCH 2/2] [VectorCombine] Add correct test for i1 concat-of-binops
 fold

---
 llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll  | 49 ----------------
 .../VectorCombine/X86/shuffle-of-binops-i1.ll | 57 +++++++++++++++++++
 2 files changed, 57 insertions(+), 49 deletions(-)
 delete mode 100644 llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll
 create mode 100644 llvm/test/Transforms/VectorCombine/X86/shuffle-of-binops-i1.ll

diff --git a/llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll b/llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll
deleted file mode 100644
index 8cd6b058b78a1..0000000000000
--- a/llvm/test/CodeGen/X86/avx512-not-kunpckdq.ll
+++ /dev/null
@@ -1,49 +0,0 @@
-; 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
-}
diff --git a/llvm/test/Transforms/VectorCombine/X86/shuffle-of-binops-i1.ll b/llvm/test/Transforms/VectorCombine/X86/shuffle-of-binops-i1.ll
new file mode 100644
index 0000000000000..23538c245de5a
--- /dev/null
+++ b/llvm/test/Transforms/VectorCombine/X86/shuffle-of-binops-i1.ll
@@ -0,0 +1,57 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=vector-combine -S -mtriple=x86_64-- -mcpu=x86-64-v4 | FileCheck %s
+
+; Verify that foldShuffleOfBinops rewrites concat(binop(a,c), binop(b,d)) into
+; binop(concat(a,b), concat(c,d)) for i1 vectors at equal cost, enabling the
+; backend to select kxnorq instead of kxord+kxord+kunpckdq+knotq.
+; See https://github.com/llvm/llvm-project/issues/205707
+
+; Basic case: concat(xor(a,c), xor(b,d)) -> xor(concat(a,b), concat(c,d))
+define <64 x i1> @concat_xor_i1(<32 x i1> %a, <32 x i1> %b, <32 x i1> %c, <32 x i1> %d) {
+; CHECK-LABEL: @concat_xor_i1(
+; CHECK-NEXT:    [[TMP2:%.*]] = 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>
+; CHECK-NEXT:    [[TMP3:%.*]] = shufflevector <32 x i1> [[C1:%.*]], <32 x i1> [[D1:%.*]], <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>
+; CHECK-NEXT:    [[R:%.*]] = xor <64 x i1> [[TMP2]], [[TMP3]]
+; CHECK-NEXT:    ret <64 x i1> [[R]]
+;
+  %xac = xor <32 x i1> %a, %c
+  %xbd = xor <32 x i1> %b, %d
+  %r = shufflevector <32 x i1> %xac, <32 x i1> %xbd, <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>
+  ret <64 x i1> %r
+}
+
+; The motivating case: NOT(concat(xor(a,c), xor(b,d))) enables kxnorq.
+define <64 x i1> @not_concat_xor_i1(<32 x i1> %a, <32 x i1> %b, <32 x i1> %c, <32 x i1> %d) {
+; CHECK-LABEL: @not_concat_xor_i1(
+; CHECK-NEXT:    [[TMP2:%.*]] = 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>
+; CHECK-NEXT:    [[TMP3:%.*]] = shufflevector <32 x i1> [[C1:%.*]], <32 x i1> [[D1:%.*]], <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>
+; CHECK-NEXT:    [[PACK:%.*]] = xor <64 x i1> [[TMP2]], [[TMP3]]
+; CHECK-NEXT:    [[R:%.*]] = xor <64 x i1> [[PACK]], splat (i1 true)
+; CHECK-NEXT:    ret <64 x i1> [[R]]
+;
+  %xac = xor <32 x i1> %a, %c
+  %xbd = xor <32 x i1> %b, %d
+  %pack = shufflevector <32 x i1> %xac, <32 x i1> %xbd, <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>
+  %r = xor <64 x i1> %pack, splat (i1 true)
+  ret <64 x i1> %r
+}
+
+; Negative test: concat with multi-use binops should NOT be folded.
+define <64 x i1> @concat_xor_i1_multiuse(<32 x i1> %a, <32 x i1> %b, <32 x i1> %c, <32 x i1> %d) {
+; CHECK-LABEL: @concat_xor_i1_multiuse(
+; CHECK-NEXT:    [[XAC:%.*]] = xor <32 x i1> [[A:%.*]], [[C:%.*]]
+; CHECK-NEXT:    [[XBD:%.*]] = xor <32 x i1> [[B:%.*]], [[D:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = shufflevector <32 x i1> [[XAC]], <32 x i1> [[XBD]], <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>
+; CHECK-NEXT:    call void @use32(<32 x i1> [[XAC]])
+; CHECK-NEXT:    call void @use32(<32 x i1> [[XBD]])
+; CHECK-NEXT:    ret <64 x i1> [[R]]
+;
+  %xac = xor <32 x i1> %a, %c
+  %xbd = xor <32 x i1> %b, %d
+  %r = shufflevector <32 x i1> %xac, <32 x i1> %xbd, <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>
+  call void @use32(<32 x i1> %xac)
+  call void @use32(<32 x i1> %xbd)
+  ret <64 x i1> %r
+}
+
+declare void @use32(<32 x i1>)



More information about the llvm-commits mailing list