[llvm] [AArch64] Use CMN instruction for negative SUBS immediates (PR #221398)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 4 23:05:37 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-aarch64
Author: Mugundan S (MGN-GIT)
<details>
<summary>Changes</summary>
Description:
* This patch transforms negative SUBS immediates to CMN when the immediate is representable by the AArch64 arithmetic immediate encoding.
* This avoids materializing the constant into a register for use by a subsequent CCMP.
* The transformation applies only to constants less than -31, since CCMN already supports immediates in the [-31, -1] range.
Mathematically :
```
b>-33 can be written as b+33>0
So, it is safe to emit CMN when the immediate falls in the AArch64 arithmetic immediate encoding
````
Reference: https://support.arm.com/documentation/ddi0487/mb/-Part-C-The-AArch64-Instruction-Set/-Chapter-C6-A64-Base-Instruction-Descriptions/-C6-2-Alphabetical-list-of-A64-base-instructions/-C6-2-93-CMN--immediate-
---
Full diff: https://github.com/llvm/llvm-project/pull/221398.diff
2 Files Affected:
- (modified) llvm/lib/Target/AArch64/AArch64ISelLowering.cpp (+33)
- (added) llvm/test/CodeGen/AArch64/aarch64-test-cmn-opt.ll (+104)
``````````diff
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 71e29b6852772..b553e31580f28 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -4218,6 +4218,17 @@ static bool canEmitConjunction(SelectionDAG &DAG, const SDValue Val,
// of a SUB operation can be reused.
PreferFirst = DAG.doesNodeExist(ISD::SUB, DAG.getVTList(VT),
{Val->getOperand(0), Val->getOperand(1)});
+ // The first comparison supports the full 12-bit CMP/CMN immediate range,
+ // while subsequent CCMP/CCMN comparisons only support a 5-bit immediate [0, 31].
+ // Prefer a wider-immediate comparison first to avoid materializing constants.
+ if (!PreferFirst && VT.isInteger()) {
+ if (auto *C = dyn_cast<ConstantSDNode>(Val->getOperand(1))) {
+ APInt CVal = C->getAPIntValue();
+ if (CVal.abs().ugt(31) &&
+ AArch64_AM::isLegalArithImmed(CVal.abs().getZExtValue()))
+ PreferFirst = true;
+ }
+ }
return true;
}
// Protect against exponential runtime and stack overflow.
@@ -21996,6 +22007,28 @@ static SDValue performANDORCSELCombine(SDNode *N, SelectionDAG &DAG) {
return SDValue();
SDLoc DL(N);
+ // If the opcode is SUBS and the comparison value is negative, transform
+ // it to ADDS (CMN) when abs(C) - 1 is a legal arithmetic immediate,
+ // avoiding materialization of the constant into a register.
+ if (Cmp0.getOpcode() == AArch64ISD::SUBS) {
+ if (auto *C0 = dyn_cast<ConstantSDNode>(Cmp0.getOperand(1))) {
+ APInt C0Val = C0->getAPIntValue();
+ if (C0Val.isNegative() && C0Val.abs().ugt(31) &&
+ (CC0 == AArch64CC::LE || CC0 == AArch64CC::GT)) {
+ APInt AbsC0Minus1 = C0Val.abs() - 1;
+ if (AArch64_AM::isLegalArithImmed(AbsC0Minus1.getZExtValue())) {
+ SDValue NewImm =
+ DAG.getConstant(AbsC0Minus1, DL, C0->getValueType(0));
+ Cmp0 = DAG.getNode(AArch64ISD::ADDS, DL,
+ DAG.getVTList(Cmp0.getOperand(0).getValueType(),
+ FlagsVT),
+ Cmp0.getOperand(0), NewImm)
+ .getValue(1);
+ CC0 = (CC0 == AArch64CC::LE) ? AArch64CC::LT : AArch64CC::GE;
+ }
+ }
+ }
+ }
SDValue CCmp, Condition;
unsigned NZCV;
diff --git a/llvm/test/CodeGen/AArch64/aarch64-test-cmn-opt.ll b/llvm/test/CodeGen/AArch64/aarch64-test-cmn-opt.ll
new file mode 100644
index 0000000000000..66b2ef86e5f5f
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/aarch64-test-cmn-opt.ll
@@ -0,0 +1,104 @@
+; RUN: llc -mtriple=aarch64-linux-gnu -verify-machineinstrs < %s | FileCheck %s
+
+; This test file validates the CMN optimization for negative SUBS immediates.
+; Example: if (b > -34 && ...) should emit CMN w1, #34.
+
+; Test: negative constant -34
+define i32 @test_neg34(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: test_neg34:
+; CHECK: cmn w1, #34
+; CHECK: ccmp w0, w2, #0, gt
+; CHECK: csel w0, w1, w0, lt
+; CHECK: ret
+ %cmp1 = icmp sgt i32 %b, -34
+ %cmp2 = icmp slt i32 %a, %c
+ %and = and i1 %cmp1, %cmp2
+ %res = select i1 %and, i32 %b, i32 %a
+ ret i32 %res
+}
+
+
+; Test: boundary around CCMN immediate range
+; -31 should not require the special transformation.
+define i32 @test_neg31(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: test_neg31:
+; CHECK: ccmp
+; CHECK: ret
+ %cmp1 = icmp sgt i32 %b, -31
+ %cmp2 = icmp slt i32 %a, %c
+ %and = and i1 %cmp1, %cmp2
+ %res = select i1 %and, i32 %b, i32 %a
+ ret i32 %res
+}
+
+
+; Test: -32
+define i32 @test_neg32(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: test_neg32:
+; CHECK: ccmp
+; CHECK: ret
+ %cmp1 = icmp sgt i32 %b, -32
+ %cmp2 = icmp slt i32 %a, %c
+ %and = and i1 %cmp1, %cmp2
+ %res = select i1 %and, i32 %b, i32 %a
+ ret i32 %res
+}
+
+
+; Test: -33
+; This is the first value outside the CCMN immediate range.
+define i32 @test_neg33(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: test_neg33:
+; CHECK: cmn w1, #33
+; CHECK: ccmp
+; CHECK: ret
+ %cmp1 = icmp sgt i32 %b, -33
+ %cmp2 = icmp slt i32 %a, %c
+ %and = and i1 %cmp1, %cmp2
+ %res = select i1 %and, i32 %b, i32 %a
+ ret i32 %res
+}
+
+
+; Test: -4095
+; Maximum value representable by a 12-bit arithmetic immediate.
+define i32 @test_neg4095(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: test_neg4095:
+; CHECK: cmn w1, #4095
+; CHECK: ccmp
+; CHECK: ret
+ %cmp1 = icmp sgt i32 %b, -4095
+ %cmp2 = icmp slt i32 %a, %c
+ %and = and i1 %cmp1, %cmp2
+ %res = select i1 %and, i32 %b, i32 %a
+ ret i32 %res
+}
+
+
+; Test: -4096
+; Should not use plain CMN #4096 because it is outside
+; the 12-bit immediate range.
+define i32 @test_neg4096(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: test_neg4096:
+; CHECK-NOT: cmn w1, #4096
+; CHECK: ret
+ %cmp1 = icmp sgt i32 %b, -4096
+ %cmp2 = icmp slt i32 %a, %c
+ %and = and i1 %cmp1, %cmp2
+ %res = select i1 %and, i32 %b, i32 %a
+ ret i32 %res
+}
+
+
+; Test: positive constant.
+; The CMN transformation should not apply.
+define i32 @test_positive(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: test_positive:
+; CHECK-NOT: cmn
+; CHECK: ret
+ %cmp1 = icmp sgt i32 %b, 34
+ %cmp2 = icmp slt i32 %a, %c
+ %and = and i1 %cmp1, %cmp2
+ %res = select i1 %and, i32 %b, i32 %a
+ ret i32 %res
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/221398
More information about the llvm-commits
mailing list