From: Samarth Tandale <[email protected]>

For `x - (x >= q ? q : 0)`, combine forms RTL that the AArch64 backend
does not recognize, leaving the compare, conditional move and
subtraction as separate instructions.

Add a `define_insn_and_split` pattern to match this RTL and split it
into a subtraction with a condition-code update and a conditional
select. The subtraction maps to SUBS, whose flags CSEL uses, producing
SUBS/CSEL instead of CMP/CSEL/SUB.

Tested on aarch64-unknown-linux-gnu with no regressions.

gcc/ChangeLog:
        PR target/127538
        * config/aarch64/aarch64.md
        (*aarch64_conditional_subtract<mode>): New pattern.

gcc/testsuite/ChangeLog:
        * gcc.target/aarch64/pr127538.c: New test.

Signed-off-by: Samarth Tandale <[email protected]>
---
For `x - (x >= q ? q : 0)`, combine forms RTL that the AArch64 backend
does not recognize, leaving the compare, conditional move and
subtraction as separate instructions.

Add a `define_insn_and_split` pattern to match this RTL and split it
into a subtraction with a condition-code update and a conditional
select. The subtraction maps to SUBS, whose flags CSEL uses, producing
SUBS/CSEL instead of CMP/CSEL/SUB.

Tested on aarch64-unknown-linux-gnu with no regressions.

PR target/127538

```
gcc/ChangeLog:
        * config/aarch64/aarch64.md
        (*aarch64_conditional_subtract<mode>): New pattern.

gcc/testsuite/ChangeLog:
        * gcc.target/aarch64/pr127538.c: New test.
```

Signed-off-by: Samarth Tandale <[email protected]>



This is a forge pull request published on the gcc-patches mailing list mailing 
list
as requested by Samarth Tandale via Sourceware Forge 
<[email protected]>.
Forge discussion: https://forge.sourceware.org/gcc/gcc/pulls/284

Get it locally using:
```
git fetch forge-upstream "+refs/versioned_pull/284/*:refs/versioned_pull/284/*"
git switch -c "pr-284-v1" "refs/versioned_pull/284/1/head"
```
Or, download the patch at: https://forge.sourceware.org/gcc/gcc/pulls/284.diff
Created on: 2026-11-19 08:46:25+00:00
Latest update: 2026-11-19 09:15:59+00:00
Changes: 2 changed files, 48 additions, 0 deletions
Head revision: sam-100/gcc ref pr127538 commit 
36d7d05b622ab32008e84532ef9f7c263aef7625 
Base revision: gcc/gcc ref trunk commit 
a9f0d2a9c91246bce74b01cf1a06d39d9dfc724e r17-6449-ga9f0d2a9c91246
Merge base: a9f0d2a9c91246bce74b01cf1a06d39d9dfc724e
Requested Reviewers: 

Changed files:
- A: gcc/testsuite/gcc.target/aarch64/pr127538.c
- M: gcc/config/aarch64/aarch64.md

 gcc/config/aarch64/aarch64.md               | 26 +++++++++++++++++++++
 gcc/testsuite/gcc.target/aarch64/pr127538.c | 22 +++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/aarch64/pr127538.c

diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md
index 4d9a794e2929b..d87fe7adc09c7 100644
--- a/gcc/config/aarch64/aarch64.md
+++ b/gcc/config/aarch64/aarch64.md
@@ -4646,6 +4646,32 @@
   }
 )
 
+(define_insn_and_split "*aarch64_conditional_subtract<mode>"
+  [(set (match_operand:GPI 0 "register_operand" "=r")
+       (minus:GPI (match_operand:GPI 1 "register_operand" "r")
+           (if_then_else:GPI (geu (match_dup 1)
+                   (match_operand:GPI 2 "register_operand" "r"))
+               (match_dup 2) (const_int 0))))
+   (clobber (match_scratch:GPI 3 "=r"))
+   (clobber (reg:CC CC_REGNUM))]
+  ""
+  "#"
+  "&& 1"
+  [(parallel
+      [(set (reg:CC CC_REGNUM)
+           (compare:CC
+               (match_dup 1) (match_dup 2)))
+       (set (match_dup 3)
+           (minus:GPI (match_dup 1) (match_dup 2)))])
+   (set (match_dup 0)
+       (if_then_else:GPI (geu (reg:CC CC_REGNUM) (const_int 0))
+           (match_dup 3) (match_dup 1)))]
+  {
+    if (GET_CODE (operands[3]) == SCRATCH)
+      operands[3] = gen_reg_rtx (<MODE>mode);
+  }
+)
+
 ;; -------------------------------------------------------------------
 ;; Comparison insns
 ;; -------------------------------------------------------------------
diff --git a/gcc/testsuite/gcc.target/aarch64/pr127538.c 
b/gcc/testsuite/gcc.target/aarch64/pr127538.c
new file mode 100644
index 0000000000000..988ce1d7bd1ec
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr127538.c
@@ -0,0 +1,22 @@
+/* PR target/127538: Verify that unsigned conditional subtraction is
+   emitted as SUBS followed by CSEL, rather than as separate CMP/CSEL/SUB
+   instructions.  */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+unsigned long
+conditional_subtract (unsigned long x, unsigned long q)
+{
+  return x - (x >= q ? q : 0);
+}
+
+unsigned int
+conditional_subtract_si (unsigned int x, unsigned int q)
+{
+  return x - (x >= q ? q : 0);
+}
+
+/* { dg-final { scan-assembler-times {subs     [wx][0-9]+, [wx][0-9]+, 
[wx][0-9]+} 2 } } */
+/* { dg-final { scan-assembler-times {csel     [wx][0-9]+, [wx][0-9]+, 
[wx][0-9]+, (cs|hs)} 2 } } */
+/* { dg-final { scan-assembler-not {cmp        [wx][0-9]+, [wx][0-9]+} } } */
+/* { dg-final { scan-assembler-not {sub        [wx][0-9]+, [wx][0-9]+, 
[wx][0-9]+} } } */
-- 
2.56.0

Reply via email to