[llvm] [X86] Fold chained i32 CLMUL operands (PR #202250)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 7 20:35:41 PDT 2026


https://github.com/mygitljf created https://github.com/llvm/llvm-project/pull/202250

Avoid chained i32 CLMUL scalar shuttles.
I avoid the extra scalar round-trip that shows up in chained i32 CLMUL lowering on X86. The patch keeps the intermediate value in vector form when the next carry-less multiply only needs the low lane, so the generated code becomes a straight CLMUL chain with one final move back to the return register.
I added a focused codegen test for the SSE and AVX forms. 

Fixes #202215

>From 6317a85052d0bea143455fc22e57b0e6a9aca09d Mon Sep 17 00:00:00 2001
From: mygitljf <2410316423 at qq.com>
Date: Mon, 8 Jun 2026 03:24:41 +0000
Subject: [PATCH] [X86] Fold chained i32 CLMUL operands

---
 llvm/lib/Target/X86/X86ISelLowering.cpp  | 37 +++++++++++++++++++++++-
 llvm/test/CodeGen/X86/clmul-i32-chain.ll | 37 ++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/X86/clmul-i32-chain.ll

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a8236f02f8df4..c9ed34251de62 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -62907,12 +62907,47 @@ static SDValue combinePCLMULQDQ(SDNode *N, SelectionDAG &DAG,
     return true;
   };
 
+  auto FoldNarrowCLMULChainOperand = [&](SDValue &Op, uint64_t M) {
+    if (VT != MVT::v2i64 || M != 0)
+      return false;
+
+    SDValue Input = peekThroughBitcasts(Op);
+    if (Input.getOpcode() != ISD::SCALAR_TO_VECTOR ||
+        Input.getValueType() != VT)
+      return false;
+
+    SDValue Ext = Input.getOperand(0);
+    if (Ext.getOpcode() != ISD::ANY_EXTEND ||
+        Ext.getSimpleValueType() != MVT::i64)
+      return false;
+
+    SDValue Trunc = Ext.getOperand(0);
+    if (Trunc.getOpcode() != ISD::TRUNCATE ||
+        Trunc.getSimpleValueType() != MVT::i32)
+      return false;
+
+    SDValue Extract = Trunc.getOperand(0);
+    if (Extract.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
+        Extract.getSimpleValueType() != MVT::i64 ||
+        !isNullConstant(Extract.getOperand(1)))
+      return false;
+
+    SDValue Prev = peekThroughBitcasts(Extract.getOperand(0));
+    if (Prev.getOpcode() != X86ISD::PCLMULQDQ || Prev.getValueType() != VT)
+      return false;
+
+    Op = Prev;
+    return true;
+  };
+
   SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
   uint64_t Mask = N->getConstantOperandVal(2);
   uint64_t M0 = Mask & 0x01, M1 = (Mask & 0x10) >> 4;
   bool Simplify0 = SimplifyOperand(N0, M0);
   bool Simplify1 = SimplifyOperand(N1, M1);
-  if (Simplify0 || Simplify1) {
+  bool Fold0 = FoldNarrowCLMULChainOperand(N0, M0);
+  bool Fold1 = FoldNarrowCLMULChainOperand(N1, M1);
+  if (Simplify0 || Simplify1 || Fold0 || Fold1) {
     SDLoc DL(N);
     return DAG.getNode(X86ISD::PCLMULQDQ, DL, VT, N0, N1,
                        DAG.getTargetConstant((M1 << 4) | M0, DL, MVT::i8));
diff --git a/llvm/test/CodeGen/X86/clmul-i32-chain.ll b/llvm/test/CodeGen/X86/clmul-i32-chain.ll
new file mode 100644
index 0000000000000..c146c9013325a
--- /dev/null
+++ b/llvm/test/CodeGen/X86/clmul-i32-chain.ll
@@ -0,0 +1,37 @@
+; RUN: llc < %s -mtriple=x86_64-- -mattr=+sse2,+pclmul | FileCheck %s --check-prefix=SSE
+; RUN: llc < %s -mtriple=x86_64-- -mattr=+avx2,+pclmul | FileCheck %s --check-prefix=AVX
+
+define i32 @clmul_i32_chain(i32 %m) nounwind {
+; SSE-LABEL: clmul_i32_chain:
+; SSE:       pclmulqdq
+; SSE-NEXT:  pclmulqdq
+; SSE-NEXT:  pclmulqdq
+; SSE-NEXT:  pclmulqdq
+; SSE-NEXT:  pclmulqdq
+; SSE-NEXT:  pclmulqdq
+; SSE-NEXT:  movq
+; SSE-NEXT:  # kill
+; SSE-NEXT:  retq
+;
+; AVX-LABEL: clmul_i32_chain:
+; AVX:       vpclmulqdq
+; AVX-NEXT:  vpclmulqdq
+; AVX-NEXT:  vpclmulqdq
+; AVX-NEXT:  vpclmulqdq
+; AVX-NEXT:  vpclmulqdq
+; AVX-NEXT:  vpclmulqdq
+; AVX-NEXT:  vmovq
+; AVX-NEXT:  # kill
+; AVX-NEXT:  retq
+  %a0 = and i32 %m, 13
+  %a1 = and i32 %m, 2
+  %a2 = sub nsw i32 0, %a1
+  %a3 = xor i32 %a0, %a2
+  %c0 = tail call i32 @llvm.clmul.i32(i32 %a3, i32 %a3)
+  %c1 = tail call i32 @llvm.clmul.i32(i32 %c0, i32 %m)
+  %c2 = tail call i32 @llvm.clmul.i32(i32 %c1, i32 %c1)
+  %c3 = tail call i32 @llvm.clmul.i32(i32 %c2, i32 %m)
+  %c4 = tail call i32 @llvm.clmul.i32(i32 %c3, i32 %c3)
+  %c5 = tail call i32 @llvm.clmul.i32(i32 %c4, i32 %m)
+  ret i32 %c5
+}



More information about the llvm-commits mailing list