[llvm] [DAGCombiner] Add legality check for CLMULR fold to prevent infinite … (PR #182376)

via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 21 13:25:29 PST 2026


https://github.com/AbdallahRashed updated https://github.com/llvm/llvm-project/pull/182376

>From 5dacf7ddac5bbe81d3398152d1f587c41d63f50d Mon Sep 17 00:00:00 2001
From: AbdallahRashed <abdallah.mrashed at gmail.com>
Date: Thu, 19 Feb 2026 22:24:33 +0100
Subject: [PATCH] [DAGCombiner] Add legality check for CLMULR fold to prevent
 infinite loop

The bitreverse(clmul(bitreverse, bitreverse)) -> clmulr fold was missing
a legality check, causing an infinite loop when CLMULR isn't supported on
the target. Added the check to match other folds in visitBITREVERSE.

Fixes #182270
---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp |  3 +-
 .../AArch64/dagcombine-clmulr-bitreverse.ll   | 56 +++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/AArch64/dagcombine-clmulr-bitreverse.ll

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 2bbb373323dfa..9512c06cba944 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -12025,7 +12025,8 @@ SDValue DAGCombiner::visitBITREVERSE(SDNode *N) {
     return DAG.getNode(ISD::SRL, DL, VT, X, Y);
 
   // fold bitreverse(clmul(bitreverse(x), bitreverse(y))) -> clmulr(x, y)
-  if (sd_match(N0, m_Clmul(m_BitReverse(m_Value(X)), m_BitReverse(m_Value(Y)))))
+  if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::CLMULR, VT)) &&
+      sd_match(N0, m_Clmul(m_BitReverse(m_Value(X)), m_BitReverse(m_Value(Y)))))
     return DAG.getNode(ISD::CLMULR, DL, VT, X, Y);
 
   return SDValue();
diff --git a/llvm/test/CodeGen/AArch64/dagcombine-clmulr-bitreverse.ll b/llvm/test/CodeGen/AArch64/dagcombine-clmulr-bitreverse.ll
new file mode 100644
index 0000000000000..e0c3c2d37ab07
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/dagcombine-clmulr-bitreverse.ll
@@ -0,0 +1,56 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=aarch64-linux-gnu %s -o - | FileCheck %s
+
+; Test for DAGCombiner fix: bitreverse(clmul(bitreverse(x), bitreverse(y))) pattern
+; should NOT be folded to clmulr when CLMULR is not legal on the target.
+; This is a regression test for issue #182270 which caused an infinite loop
+; during compilation when the DAGCombiner would fold to CLMULR, then the target
+; would expand it back to the bitreverse pattern, causing an endless cycle.
+
+; The pattern bitreverse(clmul(bitreverse(x), bitreverse(y))) is mathematically
+; equivalent to clmulr(x, y), but since AArch64 does not have native CLMULR
+; instruction support, the fold should not happen. The infinite loop is triggered
+; by vector types where the DAGCombiner runs multiple times during legalization.
+
+define <16 x i8> @bitreverse_clmul_bitreverse_v16i8(<16 x i8> %a, <16 x i8> %b) nounwind {
+; CHECK-LABEL: bitreverse_clmul_bitreverse_v16i8:
+; CHECK:       // %bb.0:
+; CHECK-NOT:    clmulr
+; CHECK:        ret
+  %a.rev = call <16 x i8> @llvm.bitreverse.v16i8(<16 x i8> %a)
+  %b.rev = call <16 x i8> @llvm.bitreverse.v16i8(<16 x i8> %b)
+  %clmul = call <16 x i8> @llvm.clmul.v16i8(<16 x i8> %a.rev, <16 x i8> %b.rev)
+  %res = call <16 x i8> @llvm.bitreverse.v16i8(<16 x i8> %clmul)
+  ret <16 x i8> %res
+}
+
+define <8 x i16> @bitreverse_clmul_bitreverse_v8i16(<8 x i16> %a, <8 x i16> %b) nounwind {
+; CHECK-LABEL: bitreverse_clmul_bitreverse_v8i16:
+; CHECK:       // %bb.0:
+; CHECK-NOT:    clmulr
+; CHECK:        ret
+  %a.rev = call <8 x i16> @llvm.bitreverse.v8i16(<8 x i16> %a)
+  %b.rev = call <8 x i16> @llvm.bitreverse.v8i16(<8 x i16> %b)
+  %clmul = call <8 x i16> @llvm.clmul.v8i16(<8 x i16> %a.rev, <8 x i16> %b.rev)
+  %res = call <8 x i16> @llvm.bitreverse.v8i16(<8 x i16> %clmul)
+  ret <8 x i16> %res
+}
+
+define <4 x i32> @bitreverse_clmul_bitreverse_v4i32(<4 x i32> %a, <4 x i32> %b) nounwind {
+; CHECK-LABEL: bitreverse_clmul_bitreverse_v4i32:
+; CHECK:       // %bb.0:
+; CHECK-NOT:    clmulr
+; CHECK:        ret
+  %a.rev = call <4 x i32> @llvm.bitreverse.v4i32(<4 x i32> %a)
+  %b.rev = call <4 x i32> @llvm.bitreverse.v4i32(<4 x i32> %b)
+  %clmul = call <4 x i32> @llvm.clmul.v4i32(<4 x i32> %a.rev, <4 x i32> %b.rev)
+  %res = call <4 x i32> @llvm.bitreverse.v4i32(<4 x i32> %clmul)
+  ret <4 x i32> %res
+}
+
+declare <16 x i8> @llvm.bitreverse.v16i8(<16 x i8>)
+declare <8 x i16> @llvm.bitreverse.v8i16(<8 x i16>)
+declare <4 x i32> @llvm.bitreverse.v4i32(<4 x i32>)
+declare <16 x i8> @llvm.clmul.v16i8(<16 x i8>, <16 x i8>)
+declare <8 x i16> @llvm.clmul.v8i16(<8 x i16>, <8 x i16>)
+declare <4 x i32> @llvm.clmul.v4i32(<4 x i32>, <4 x i32>)



More information about the llvm-commits mailing list