https://gcc.gnu.org/g:3f8b1b21f377427adbdfa4cbfc21b979eb49c9d3
commit r15-3954-g3f8b1b21f377427adbdfa4cbfc21b979eb49c9d3 Author: Pan Li <pan2...@intel.com> Date: Wed Sep 25 09:26:07 2024 +0800 Match: Support form 1 for scalar signed integer SAT_SUB This patch would like to support the form 1 of the scalar signed integer SAT_SUB. Aka below example: Form 1: #define DEF_SAT_S_SUB_FMT_1(T, UT, MIN, MAX) \ T __attribute__((noinline)) \ sat_s_sub_##T##_fmt_1 (T x, T y) \ { \ T minus = (UT)x - (UT)y; \ return (x ^ y) >= 0 \ ? minus \ : (minus ^ x) >= 0 \ ? minus \ : x < 0 ? MIN : MAX; \ } DEF_SAT_S_SUB_FMT_1(int8_t, uint8_t, INT8_MIN, INT8_MAX) Before this patch: 4 │ __attribute__((noinline)) 5 │ int8_t sat_s_sub_int8_t_fmt_1 (int8_t x, int8_t y) 6 │ { 7 │ int8_t minus; 8 │ unsigned char x.0_1; 9 │ unsigned char y.1_2; 10 │ unsigned char _3; 11 │ signed char _4; 12 │ signed char _5; 13 │ int8_t _6; 14 │ _Bool _11; 15 │ signed char _12; 16 │ signed char _13; 17 │ signed char _14; 18 │ signed char _15; 19 │ 20 │ ;; basic block 2, loop depth 0 21 │ ;; pred: ENTRY 22 │ x.0_1 = (unsigned char) x_7(D); 23 │ y.1_2 = (unsigned char) y_8(D); 24 │ _3 = x.0_1 - y.1_2; 25 │ minus_9 = (int8_t) _3; 26 │ _4 = x_7(D) ^ y_8(D); 27 │ _5 = x_7(D) ^ minus_9; 28 │ _15 = _4 & _5; 29 │ if (_15 < 0) 30 │ goto <bb 3>; [41.00%] 31 │ else 32 │ goto <bb 4>; [59.00%] 33 │ ;; succ: 3 34 │ ;; 4 35 │ 36 │ ;; basic block 3, loop depth 0 37 │ ;; pred: 2 38 │ _11 = x_7(D) < 0; 39 │ _12 = (signed char) _11; 40 │ _13 = -_12; 41 │ _14 = _13 ^ 127; 42 │ ;; succ: 4 43 │ 44 │ ;; basic block 4, loop depth 0 45 │ ;; pred: 2 46 │ ;; 3 47 │ # _6 = PHI <minus_9(2), _14(3)> 48 │ return _6; 49 │ ;; succ: EXIT 50 │ 51 │ } After this patch: 4 │ __attribute__((noinline)) 5 │ int8_t sat_s_sub_int8_t_fmt_1 (int8_t x, int8_t y) 6 │ { 7 │ int8_t _6; 8 │ 9 │ ;; basic block 2, loop depth 0 10 │ ;; pred: ENTRY 11 │ _6 = .SAT_SUB (x_7(D), y_8(D)); [tail call] 12 │ return _6; 13 │ ;; succ: EXIT 14 │ 15 │ } The below test suites are passed for this patch. * The rv64gcv fully regression test. * The x86 bootstrap test. * The x86 fully regression test. gcc/ChangeLog: * match.pd: Add case 1 matching pattern for signed SAT_SUB. * tree-ssa-math-opts.cc (gimple_signed_integer_sat_sub): Add new decl for generated SAT_SUB matching func. (match_unsigned_saturation_sub): Rename from... (match_saturation_sub): ...Rename to and add signed SAT_SUB matching. (math_opts_dom_walker::after_dom_children): Leverage the named match func for both the unsigned and signed SAT_SUB. Signed-off-by: Pan Li <pan2...@intel.com> Diff: --- gcc/match.pd | 14 ++++++++++++++ gcc/tree-ssa-math-opts.cc | 8 +++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index e06a812e9766..5ec31ef6269a 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3360,6 +3360,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) } (if (wi::eq_p (sum, wi::uhwi (0, precision))))))) +/* Signed saturation sub, case 1: + T minus = (T)((UT)X - (UT)Y); + SAT_S_SUB = (X ^ Y) & (X ^ minus) < 0 ? (-(T)(X < 0) ^ MAX) : minus; + + The T and UT are type pair like T=int8_t, UT=uint8_t. */ +(match (signed_integer_sat_sub @0 @1) + (cond^ (lt (bit_and:c (bit_xor:c @0 @1) + (bit_xor @0 (nop_convert@2 (minus (nop_convert @0) + (nop_convert @1))))) + integer_zerop) + (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value) + @2) + (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type)))) + /* Unsigned saturation truncate, case 1, sizeof (WT) > sizeof (NT). SAT_U_TRUNC = (NT)x | (NT)(-(X > (WT)(NT)(-1))). */ (match (unsigned_integer_sat_trunc @0) diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index f1cfe7dfab08..a61559c52a94 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -4024,6 +4024,7 @@ extern bool gimple_unsigned_integer_sat_sub (tree, tree*, tree (*)(tree)); extern bool gimple_unsigned_integer_sat_trunc (tree, tree*, tree (*)(tree)); extern bool gimple_signed_integer_sat_add (tree, tree*, tree (*)(tree)); +extern bool gimple_signed_integer_sat_sub (tree, tree*, tree (*)(tree)); static void build_saturation_binary_arith_call (gimple_stmt_iterator *gsi, internal_fn fn, @@ -4162,7 +4163,7 @@ match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gassign *stmt) * <bb 4> [local count: 1073741824]: * _1 = .SAT_SUB (x_2(D), y_3(D)); */ static void -match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi) +match_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi) { if (gimple_phi_num_args (phi) != 2) return; @@ -4170,7 +4171,8 @@ match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi) tree ops[2]; tree phi_result = gimple_phi_result (phi); - if (gimple_unsigned_integer_sat_sub (phi_result, ops, NULL)) + if (gimple_unsigned_integer_sat_sub (phi_result, ops, NULL) + || gimple_signed_integer_sat_sub (phi_result, ops, NULL)) build_saturation_binary_arith_call (gsi, phi, IFN_SAT_SUB, phi_result, ops[0], ops[1]); } @@ -6139,7 +6141,7 @@ math_opts_dom_walker::after_dom_children (basic_block bb) /* The match_* may remove phi node. */ match_saturation_add (&gsi, psi.phi ()); - match_unsigned_saturation_sub (&gsi, psi.phi ()); + match_saturation_sub (&gsi, psi.phi ()); } for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)