[llvm] [ARM][AArch64] Replace manual CLS expansion with ISD::CTLS (PR #178430)
Hamza Hassanain via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 28 06:12:46 PST 2026
https://github.com/HamzaHassanain created https://github.com/llvm/llvm-project/pull/178430
Converts ARM scalar CLS intrinsics to use the unified ISD::CTLS node instead of custom manual expansion. This addresses the issue [#174337](https://github.com/llvm/llvm-project/issues/174337).
Changes:
- int_arm_cls and int_arm_cls64 now return ISD::CTLS nodes
- Added LowerCTLS to handle custom lowering for i32 and i64 CTLS operations
- i32 CTLS expansion generates the same CTLZ-based pattern as before
- i64 CTLS on 32-bit ARM splits into two 32-bit operations
- Added proper setOperationAction for CTLS operations
The assembly output remains identical to the previous manual expansion, ensuring compatibility with existing code. This unifies the CLS handling under the generic ISD::CTLS infrastructure for better maintainability.
>From 1d7668737a5eaffcd3fcf1ee76a25f33887667c6 Mon Sep 17 00:00:00 2001
From: Hamza Hassanain <hamzahassanain067 at gmail.com>
Date: Wed, 28 Jan 2026 15:55:45 +0200
Subject: [PATCH] [ARM] Replace manual CLS expansion with ISD::CTLS
Converts ARM scalar CLS intrinsics to use the unified ISD::CTLS node
instead of custom manual expansion. This addresses issue #174337.
Changes:
- int_arm_cls and int_arm_cls64 now return ISD::CTLS nodes
- Added LowerCTLS to handle custom lowering for i32 and i64 CTLS operations
- i32 CTLS expansion generates the same CTLZ-based pattern as before
- i64 CTLS on 32-bit ARM splits into two 32-bit operations
- Added proper setOperationAction for CTLS operations
The assembly output remains identical to the previous manual expansion,
ensuring compatibility with existing code. This unifies the CLS handling
under the generic ISD::CTLS infrastructure for better maintainability.
---
llvm/lib/Target/ARM/ARMISelLowering.cpp | 95 ++++++++++++++++---------
llvm/lib/Target/ARM/ARMISelLowering.h | 2 +
llvm/test/CodeGen/ARM/mve-cls.ll | 45 ++++++++++++
llvm/test/CodeGen/ARM/neon-cls.ll | 81 +++++++++++++++++++++
4 files changed, 191 insertions(+), 32 deletions(-)
create mode 100644 llvm/test/CodeGen/ARM/mve-cls.ll
create mode 100644 llvm/test/CodeGen/ARM/neon-cls.ll
diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp
index 7b240462c66fb..aee3c50b4a476 100644
--- a/llvm/lib/Target/ARM/ARMISelLowering.cpp
+++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp
@@ -1004,6 +1004,9 @@ ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM_,
setOperationAction(ISD::ROTR, VT, Expand);
}
setOperationAction(ISD::CTTZ, MVT::i32, Custom);
+ // CTLS (Count Leading Sign bits)
+ setOperationAction(ISD::CTLS, MVT::i32, Custom);
+ setOperationAction(ISD::CTLS, MVT::i64, Custom);
// TODO: These two should be set to LibCall, but this currently breaks
// the Linux kernel build. See #101786.
setOperationAction(ISD::CTPOP, MVT::i32, Expand);
@@ -3838,42 +3841,12 @@ ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
case Intrinsic::arm_cls: {
const SDValue &Operand = Op.getOperand(1);
const EVT VTy = Op.getValueType();
- SDValue SRA =
- DAG.getNode(ISD::SRA, dl, VTy, Operand, DAG.getConstant(31, dl, VTy));
- SDValue XOR = DAG.getNode(ISD::XOR, dl, VTy, SRA, Operand);
- SDValue SHL =
- DAG.getNode(ISD::SHL, dl, VTy, XOR, DAG.getConstant(1, dl, VTy));
- SDValue OR =
- DAG.getNode(ISD::OR, dl, VTy, SHL, DAG.getConstant(1, dl, VTy));
- SDValue Result = DAG.getNode(ISD::CTLZ, dl, VTy, OR);
- return Result;
+ return DAG.getNode(ISD::CTLS, dl, VTy, Operand);
}
case Intrinsic::arm_cls64: {
- // cls(x) = if cls(hi(x)) != 31 then cls(hi(x))
- // else 31 + clz(if hi(x) == 0 then lo(x) else not(lo(x)))
const SDValue &Operand = Op.getOperand(1);
const EVT VTy = Op.getValueType();
- SDValue Lo, Hi;
- std::tie(Lo, Hi) = DAG.SplitScalar(Operand, dl, VTy, VTy);
- SDValue Constant0 = DAG.getConstant(0, dl, VTy);
- SDValue Constant1 = DAG.getConstant(1, dl, VTy);
- SDValue Constant31 = DAG.getConstant(31, dl, VTy);
- SDValue SRAHi = DAG.getNode(ISD::SRA, dl, VTy, Hi, Constant31);
- SDValue XORHi = DAG.getNode(ISD::XOR, dl, VTy, SRAHi, Hi);
- SDValue SHLHi = DAG.getNode(ISD::SHL, dl, VTy, XORHi, Constant1);
- SDValue ORHi = DAG.getNode(ISD::OR, dl, VTy, SHLHi, Constant1);
- SDValue CLSHi = DAG.getNode(ISD::CTLZ, dl, VTy, ORHi);
- SDValue CheckLo =
- DAG.getSetCC(dl, MVT::i1, CLSHi, Constant31, ISD::CondCode::SETEQ);
- SDValue HiIsZero =
- DAG.getSetCC(dl, MVT::i1, Hi, Constant0, ISD::CondCode::SETEQ);
- SDValue AdjustedLo =
- DAG.getSelect(dl, VTy, HiIsZero, Lo, DAG.getNOT(dl, Lo, VTy));
- SDValue CLZAdjustedLo = DAG.getNode(ISD::CTLZ, dl, VTy, AdjustedLo);
- SDValue Result =
- DAG.getSelect(dl, VTy, CheckLo,
- DAG.getNode(ISD::ADD, dl, VTy, CLZAdjustedLo, Constant31), CLSHi);
- return Result;
+ return DAG.getNode(ISD::CTLS, dl, VTy, Operand);
}
case Intrinsic::eh_sjlj_lsda: {
MachineFunction &MF = DAG.getMachineFunction();
@@ -6309,6 +6282,63 @@ static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
return Res;
}
+static SDValue LowerCTLS(SDNode *N, SelectionDAG &DAG,
+ const ARMSubtarget *ST) {
+ SDLoc dl(N);
+ EVT VT = N->getValueType(0);
+ SDValue Operand = N->getOperand(0);
+
+ if (VT == MVT::i32) {
+ // ARM32 scalar CLS: CTLS(x) = CTLZ(OR(SHL(XOR(x, SRA(x, 31)), 1), 1))
+ SDValue SRA =
+ DAG.getNode(ISD::SRA, dl, VT, Operand, DAG.getConstant(31, dl, VT));
+ SDValue XOR = DAG.getNode(ISD::XOR, dl, VT, SRA, Operand);
+ SDValue SHL =
+ DAG.getNode(ISD::SHL, dl, VT, XOR, DAG.getConstant(1, dl, VT));
+ SDValue OR =
+ DAG.getNode(ISD::OR, dl, VT, SHL, DAG.getConstant(1, dl, VT));
+ SDValue Result = DAG.getNode(ISD::CTLZ, dl, VT, OR);
+ return Result;
+ }
+
+ if (VT == MVT::i64) {
+ // For 64-bit on 32-bit ARM, we need to split into two 32-bit operations
+ EVT VT32 = MVT::i32;
+ SDValue Lo, Hi;
+ std::tie(Lo, Hi) = DAG.SplitScalar(Operand, dl, VT32, VT32);
+
+ SDValue Constant0 = DAG.getConstant(0, dl, VT32);
+ SDValue Constant1 = DAG.getConstant(1, dl, VT32);
+ SDValue Constant31 = DAG.getConstant(31, dl, VT32);
+
+ // Compute CTLS of high part
+ SDValue SRAHi = DAG.getNode(ISD::SRA, dl, VT32, Hi, Constant31);
+ SDValue XORHi = DAG.getNode(ISD::XOR, dl, VT32, SRAHi, Hi);
+ SDValue SHLHi = DAG.getNode(ISD::SHL, dl, VT32, XORHi, Constant1);
+ SDValue ORHi = DAG.getNode(ISD::OR, dl, VT32, SHLHi, Constant1);
+ SDValue CLSHi = DAG.getNode(ISD::CTLZ, dl, VT32, ORHi);
+
+ // Check if CLSHi == 31 (all high bits are sign bits)
+ SDValue IsAllSignBits =
+ DAG.getSetCC(dl, MVT::i1, CLSHi, Constant31, ISD::SETEQ);
+
+ // If all high bits are sign bits, compute for low part
+ SDValue HiIsZero =
+ DAG.getSetCC(dl, MVT::i1, Hi, Constant0, ISD::SETEQ);
+ SDValue AdjustedLo =
+ DAG.getSelect(dl, VT32, HiIsZero, Lo, DAG.getNOT(dl, Lo, VT32));
+ SDValue CLZAdjustedLo = DAG.getNode(ISD::CTLZ, dl, VT32, AdjustedLo);
+ SDValue Result =
+ DAG.getSelect(dl, VT32, IsAllSignBits,
+ DAG.getNode(ISD::ADD, dl, VT32, CLZAdjustedLo, Constant31), CLSHi);
+
+ return Result;
+ }
+
+ // Vector types should be handled elsewhere
+ return SDValue();
+}
+
/// Getvshiftimm - Check if this is a valid build_vector for the immediate
/// operand of a vector shift operation, where all the elements of the
/// build_vector must have the same constant integer value.
@@ -10352,6 +10382,7 @@ SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
case ISD::CTTZ:
case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
+ case ISD::CTLS: return LowerCTLS(Op.getNode(), DAG, Subtarget);
case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
case ISD::SETCC: return LowerVSETCC(Op, DAG, Subtarget);
case ISD::SETCCCARRY: return LowerSETCCCARRY(Op, DAG);
diff --git a/llvm/lib/Target/ARM/ARMISelLowering.h b/llvm/lib/Target/ARM/ARMISelLowering.h
index bc84654f8bd5a..b6dc9851b1d56 100644
--- a/llvm/lib/Target/ARM/ARMISelLowering.h
+++ b/llvm/lib/Target/ARM/ARMISelLowering.h
@@ -608,6 +608,8 @@ class VectorType;
SDValue LowerFP_TO_BF16(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerCMP(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerABS(SDValue Op, SelectionDAG &DAG) const;
+ SDValue LowerCTLS(SDNode *N, SelectionDAG &DAG,
+ const ARMSubtarget *ST) const;
Register getRegisterByName(const char* RegName, LLT VT,
const MachineFunction &MF) const override;
diff --git a/llvm/test/CodeGen/ARM/mve-cls.ll b/llvm/test/CodeGen/ARM/mve-cls.ll
new file mode 100644
index 0000000000000..cbf708f637992
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/mve-cls.ll
@@ -0,0 +1,45 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=arm-none-eabi -mcpu=cortex-m85 -mattr=+mve %s -o - | FileCheck %s
+
+define <16 x i8> @test_cls_v16s8(<16 x i8> %a) nounwind {
+; CHECK-LABEL: test_cls_v16s8:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: vmov d1, r2, r3
+; CHECK-NEXT: vmov d0, r0, r1
+; CHECK-NEXT: vcls.s8 q0, q0
+; CHECK-NEXT: vmov r0, r1, d0
+; CHECK-NEXT: vmov r2, r3, d1
+; CHECK-NEXT: bx lr
+ %result = call <16 x i8> @llvm.arm.mve.vcls.v16s8(<16 x i8> %a)
+ ret <16 x i8> %result
+}
+
+define <8 x i16> @test_cls_v8s16(<8 x i16> %a) nounwind {
+; CHECK-LABEL: test_cls_v8s16:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: vmov d1, r2, r3
+; CHECK-NEXT: vmov d0, r0, r1
+; CHECK-NEXT: vcls.s16 q0, q0
+; CHECK-NEXT: vmov r0, r1, d0
+; CHECK-NEXT: vmov r2, r3, d1
+; CHECK-NEXT: bx lr
+ %result = call <8 x i16> @llvm.arm.mve.vcls.v8s16(<8 x i16> %a)
+ ret <8 x i16> %result
+}
+
+define <4 x i32> @test_cls_v4s32(<4 x i32> %a) nounwind {
+; CHECK-LABEL: test_cls_v4s32:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: vmov d1, r2, r3
+; CHECK-NEXT: vmov d0, r0, r1
+; CHECK-NEXT: vcls.s32 q0, q0
+; CHECK-NEXT: vmov r0, r1, d0
+; CHECK-NEXT: vmov r2, r3, d1
+; CHECK-NEXT: bx lr
+ %result = call <4 x i32> @llvm.arm.mve.vcls.v4s32(<4 x i32> %a)
+ ret <4 x i32> %result
+}
+
+declare <16 x i8> @llvm.arm.mve.vcls.v16s8(<16 x i8>) nounwind readnone
+declare <8 x i16> @llvm.arm.mve.vcls.v8s16(<8 x i16>) nounwind readnone
+declare <4 x i32> @llvm.arm.mve.vcls.v4s32(<4 x i32>) nounwind readnone
diff --git a/llvm/test/CodeGen/ARM/neon-cls.ll b/llvm/test/CodeGen/ARM/neon-cls.ll
new file mode 100644
index 0000000000000..4667b87a6f7bd
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/neon-cls.ll
@@ -0,0 +1,81 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=arm-eabi -mattr=+neon %s -o - | FileCheck %s
+
+define <8 x i8> @test_cls_v8i8(<8 x i8> %a) nounwind {
+; CHECK-LABEL: test_cls_v8i8:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: vmov d16, r0, r1
+; CHECK-NEXT: vcls.s8 d16, d16
+; CHECK-NEXT: vmov r0, r1, d16
+; CHECK-NEXT: mov pc, lr
+ %result = call <8 x i8> @llvm.arm.neon.vcls.v8i8(<8 x i8> %a)
+ ret <8 x i8> %result
+}
+
+define <16 x i8> @test_cls_v16i8(<16 x i8> %a) nounwind {
+; CHECK-LABEL: test_cls_v16i8:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: vmov d17, r2, r3
+; CHECK-NEXT: vmov d16, r0, r1
+; CHECK-NEXT: vcls.s8 q8, q8
+; CHECK-NEXT: vmov r0, r1, d16
+; CHECK-NEXT: vmov r2, r3, d17
+; CHECK-NEXT: mov pc, lr
+ %result = call <16 x i8> @llvm.arm.neon.vcls.v16i8(<16 x i8> %a)
+ ret <16 x i8> %result
+}
+
+define <4 x i16> @test_cls_v4i16(<4 x i16> %a) nounwind {
+; CHECK-LABEL: test_cls_v4i16:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: vmov d16, r0, r1
+; CHECK-NEXT: vcls.s16 d16, d16
+; CHECK-NEXT: vmov r0, r1, d16
+; CHECK-NEXT: mov pc, lr
+ %result = call <4 x i16> @llvm.arm.neon.vcls.v4i16(<4 x i16> %a)
+ ret <4 x i16> %result
+}
+
+define <8 x i16> @test_cls_v8i16(<8 x i16> %a) nounwind {
+; CHECK-LABEL: test_cls_v8i16:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: vmov d17, r2, r3
+; CHECK-NEXT: vmov d16, r0, r1
+; CHECK-NEXT: vcls.s16 q8, q8
+; CHECK-NEXT: vmov r0, r1, d16
+; CHECK-NEXT: vmov r2, r3, d17
+; CHECK-NEXT: mov pc, lr
+ %result = call <8 x i16> @llvm.arm.neon.vcls.v8i16(<8 x i16> %a)
+ ret <8 x i16> %result
+}
+
+define <2 x i32> @test_cls_v2i32(<2 x i32> %a) nounwind {
+; CHECK-LABEL: test_cls_v2i32:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: vmov d16, r0, r1
+; CHECK-NEXT: vcls.s32 d16, d16
+; CHECK-NEXT: vmov r0, r1, d16
+; CHECK-NEXT: mov pc, lr
+ %result = call <2 x i32> @llvm.arm.neon.vcls.v2i32(<2 x i32> %a)
+ ret <2 x i32> %result
+}
+
+define <4 x i32> @test_cls_v4i32(<4 x i32> %a) nounwind {
+; CHECK-LABEL: test_cls_v4i32:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: vmov d17, r2, r3
+; CHECK-NEXT: vmov d16, r0, r1
+; CHECK-NEXT: vcls.s32 q8, q8
+; CHECK-NEXT: vmov r0, r1, d16
+; CHECK-NEXT: vmov r2, r3, d17
+; CHECK-NEXT: mov pc, lr
+ %result = call <4 x i32> @llvm.arm.neon.vcls.v4i32(<4 x i32> %a)
+ ret <4 x i32> %result
+}
+
+declare <8 x i8> @llvm.arm.neon.vcls.v8i8(<8 x i8>) nounwind readnone
+declare <16 x i8> @llvm.arm.neon.vcls.v16i8(<16 x i8>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vcls.v4i16(<4 x i16>) nounwind readnone
+declare <8 x i16> @llvm.arm.neon.vcls.v8i16(<8 x i16>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vcls.v2i32(<2 x i32>) nounwind readnone
+declare <4 x i32> @llvm.arm.neon.vcls.v4i32(<4 x i32>) nounwind readnone
More information about the llvm-commits
mailing list