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]>
---
Changes since v1:
- Replace the assembly scans with check-function-bodies to verify the
SUBS/CSEL sequence in each function.
- Change `unsigned long` to `unsigned long long` for 64-bit test case.
The backend pattern is unchanged.
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-v2" "refs/versioned_pull/284/2/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 14:30:27+00:00
Changes: 2 changed files, 57 additions, 0 deletions
Head revision: sam-100/gcc ref pr127538 commit
30d246f20669b7b79f33d5b5221ca7df9f525fe9
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 | 31 +++++++++++++++++++++
2 files changed, 57 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..5db75863f4f62
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr127538.c
@@ -0,0 +1,31 @@
+/* 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" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** conditional_subtract:
+** subs\t(x[0-9]+), x0, x1
+** csel\tx0, \1, x0, cs
+** ret
+*/
+unsigned long long
+conditional_subtract (unsigned long long x, unsigned long long q)
+{
+ return x - (x >= q ? q : 0);
+}
+
+/*
+** conditional_subtract_si:
+** subs\t(w[0-9]+), w0, w1
+** csel\tw0, \1, w0, cs
+** ret
+*/
+unsigned int
+conditional_subtract_si (unsigned int x, unsigned int q)
+{
+ return x - (x >= q ? q : 0);
+}
+
Range-diff against v1:
1: 36d7d05b622ab ! 1: 30d246f20669b aarch64: Restore SUBS/CSEL for unsigned
conditional subtraction [PR127538]
@@ gcc/testsuite/gcc.target/aarch64/pr127538.c (new)
+ instructions. */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
++/* { dg-final { check-function-bodies "**" "" "" } } */
+
-+unsigned long
-+conditional_subtract (unsigned long x, unsigned long q)
++/*
++** conditional_subtract:
++** subs\t(x[0-9]+), x0, x1
++** csel\tx0, \1, x0, cs
++** ret
++*/
++unsigned long long
++conditional_subtract (unsigned long long x, unsigned long long q)
+{
+ return x - (x >= q ? q : 0);
+}
+
++/*
++** conditional_subtract_si:
++** subs\t(w[0-9]+), w0, w1
++** csel\tw0, \1, w0, cs
++** ret
++*/
+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