https://gcc.gnu.org/g:abcaae9e947e7de5b4dfeabd852dfb2232fefe07
commit r17-6494-gabcaae9e947e7de5b4dfeabd852dfb2232fefe07 Author: Samarth Tandale <[email protected]> Date: Tue Nov 17 10:41:02 2026 +0000 aarch64: Restore SUBS/CSEL for unsigned conditional subtraction [PR127538] 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]> Diff: --- gcc/config/aarch64/aarch64.md | 26 ++++++++++++++++++++++++ gcc/testsuite/gcc.target/aarch64/pr127538.c | 31 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md index 350175e13bbe..951f1e008045 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 000000000000..5db75863f4f6 --- /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); +} +
