From patchwork Mon Aug 5 07:14:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1968910 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=TIo/d84y; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wcnl65X2Sz1yYD for ; Mon, 5 Aug 2024 17:15:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 969113857C4F for ; Mon, 5 Aug 2024 07:15:05 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by sourceware.org (Postfix) with ESMTPS id 02DFC385841C for ; Mon, 5 Aug 2024 07:14:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 02DFC385841C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 02DFC385841C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722842086; cv=none; b=S5mfba6KN1Cd8fPBPmoqXnpBow+HgtTFNUoxCS4op+X9gdrCBWEQqcuk1YYCISuCgdaG/cM0LNMcqLrTAxK9xJE9/QCJJ4TSnqHObPCzNRQCxF8wukj9elRel7R1Z1BUtd9BIKHxBQIw9Pq8jN+OwZXg/IzoV7WniwB64wcVHJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722842086; c=relaxed/simple; bh=TLSKz0mYWJxpqKFoGlDn963X1KrKxpIFwGeovLWlAeQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fGRUqn7OUPDQ9Nod0Jq8hdkQ0ClBE+RDo+j6j46j6P7XZy+5EMsI9bdO+td1uimsE4DBq64urN4BGblmLSCpO5AcUAi2MXFMFQORwO4y+VsUxhCb6bioBxFngnNHgzLTtTwNMPZoG832mvsvj/DVGHiJ0Oh0wRHgS8eAFtNuPhQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1722842084; x=1754378084; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=TLSKz0mYWJxpqKFoGlDn963X1KrKxpIFwGeovLWlAeQ=; b=TIo/d84yI6nxeivQ2a68/6F0d9H5gXKz4QMSLNtxzhHB5czqqWYcJGih c3SFP4RHIWQqnRUQm53yzoLk1k1526ZwBH3YaVBxOKTzI2Y54OFtGl6Fw q7WZr46EHbGEouVvQSQd9NzpPuHlz5dC126sSR2iWPKiifn3fhoT6P2Kv ugKhO2yxJqx2uEeVpQDo4GveGxvxMkkGKpPd7ffCknUx3NhhS9pOwZ8yC XkhaukcNOXMvaTMNSDn5xpkSFwM18j+mj+J+l6uwivaIkR4y4SzyUBqd6 tR0Y7vi2j9WZPh1dDqkO4YKaoW39CnGKCq2fD7kCBZMKK1rB/4SqVuPiK A==; X-CSE-ConnectionGUID: LEEHsJGiQ7S9/LJxOw4F2A== X-CSE-MsgGUID: xA3SZFPvR2K7Uu8P9RXLEQ== X-IronPort-AV: E=McAfee;i="6700,10204,11154"; a="32190108" X-IronPort-AV: E=Sophos;i="6.09,264,1716274800"; d="scan'208";a="32190108" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Aug 2024 00:14:17 -0700 X-CSE-ConnectionGUID: LgWv9J9PQmyiyAsvSspQhA== X-CSE-MsgGUID: W5+a7qXlS3uG9MEZ0+Elbg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,264,1716274800"; d="scan'208";a="56002923" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmviesa008.fm.intel.com with ESMTP; 05 Aug 2024 00:14:13 -0700 Received: from panli.sh.intel.com (panli.sh.intel.com [10.239.154.73]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 89A9A10056F5; Mon, 5 Aug 2024 15:14:12 +0800 (CST) From: pan2.li@intel.com To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, juzhe.zhong@rivai.ai, kito.cheng@gmail.com, jeffreyalaw@gmail.com, rdapp.gcc@gmail.com, Pan Li Subject: [PATCH v1] Match: Support form 1 for scalar signed integer .SAT_ADD Date: Mon, 5 Aug 2024 15:14:06 +0800 Message-ID: <20240805071406.4089749-1-pan2.li@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org From: Pan Li This patch would like to support the form 1 of the scalar signed integer .SAT_ADD. Aka below example: Form 1: #define DEF_SAT_S_ADD_FMT_1(T) \ T __attribute__((noinline)) \ sat_s_add_##T##_fmt_1 (T x, T y) \ { \ T min = (T)1u << (sizeof (T) * 8 - 1); \ T max = min - 1; \ return (x ^ y) < 0 \ ? (T)(x + y) \ : ((T)(x + y) ^ x) >= 0 \ ? (T)(x + y) \ : x < 0 ? min : max; \ } DEF_SAT_S_ADD_FMT_1 (int64_t) We can tell the difference before and after this patch if backend implemented the ssadd3 pattern similar as below. Before this patch: 4 │ __attribute__((noinline)) 5 │ int64_t sat_s_add_int64_t_fmt_1 (int64_t x, int64_t y) 6 │ { 7 │ long int _1; 8 │ long int _2; 9 │ long int _3; 10 │ int64_t _4; 11 │ long int _7; 12 │ _Bool _9; 13 │ long int _10; 14 │ long int _11; 15 │ long int _12; 16 │ long int _13; 17 │ 18 │ ;; basic block 2, loop depth 0 19 │ ;; pred: ENTRY 20 │ _1 = x_5(D) ^ y_6(D); 21 │ _13 = x_5(D) + y_6(D); 22 │ _3 = x_5(D) ^ _13; 23 │ _2 = ~_1; 24 │ _7 = _2 & _3; 25 │ if (_7 >= 0) 26 │ goto ; [59.00%] 27 │ else 28 │ goto ; [41.00%] 29 │ ;; succ: 4 30 │ ;; 3 31 │ 32 │ ;; basic block 3, loop depth 0 33 │ ;; pred: 2 34 │ _9 = x_5(D) < 0; 35 │ _10 = (long int) _9; 36 │ _11 = -_10; 37 │ _12 = _11 ^ 9223372036854775807; 38 │ ;; succ: 4 39 │ 40 │ ;; basic block 4, loop depth 0 41 │ ;; pred: 2 42 │ ;; 3 43 │ # _4 = PHI <_13(2), _12(3)> 44 │ return _4; 45 │ ;; succ: EXIT 46 │ 47 │ } After this patch: 4 │ __attribute__((noinline)) 5 │ int64_t sat_s_add_int64_t_fmt_1 (int64_t x, int64_t y) 6 │ { 7 │ int64_t _4; 8 │ 9 │ ;; basic block 2, loop depth 0 10 │ ;; pred: ENTRY 11 │ _4 = .SAT_ADD (x_5(D), y_6(D)); [tail call] 12 │ return _4; 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 the matching for signed .SAT_ADD. * tree-ssa-math-opts.cc (gimple_signed_integer_sat_add): Add new matching func decl. (match_unsigned_saturation_add): Try signed .SAT_ADD and rename to ... (match_saturation_add): ... here. (math_opts_dom_walker::after_dom_children): Update the above renamed func from caller. Signed-off-by: Pan Li --- gcc/match.pd | 14 +++++++++++++ gcc/tree-ssa-math-opts.cc | 42 ++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index c9c8478d286..0a2ffc733d3 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3311,6 +3311,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) } (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst)))))) +/* Signed saturation add, case 1: + T min = (T)1u << (sizeof (T) * 8 - 1); + T max = min - 1; + SAT_S_ADD = (X ^ Y) < 0 + ? (X + Y) + : ((T)(X + Y) ^ X) >= 0 ? (X + Y) : X < 0 ? min : max. */ +(match (signed_integer_sat_add @0 @1) + (cond^ (ge (bit_and:c (bit_xor @0 (convert? @2)) (bit_not (bit_xor @0 @1))) + integer_zerop) + (convert? (plus@2 (convert1? @0) (convert1? @1))) + (bit_xor (negate (convert (lt @0 integer_zerop))) max_value)) + (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type) + && types_match (type, @0, @1)))) + /* x > y && x != XXX_MIN --> x > y x > y && x == XXX_MIN --> false . */ (for eqne (eq ne) diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index 8d96a4c964b..d5c9b475f72 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -4023,6 +4023,8 @@ extern bool gimple_unsigned_integer_sat_add (tree, tree*, tree (*)(tree)); 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)); + static void build_saturation_binary_arith_call (gimple_stmt_iterator *gsi, internal_fn fn, tree lhs, tree op_0, tree op_1) @@ -4072,7 +4074,8 @@ match_unsigned_saturation_add (gimple_stmt_iterator *gsi, gassign *stmt) } /* - * Try to match saturation unsigned add with PHI. + * Try to match saturation add with PHI. + * For unsigned integer: * : * _1 = x_3(D) + y_4(D); * if (_1 >= x_3(D)) @@ -4086,10 +4089,38 @@ match_unsigned_saturation_add (gimple_stmt_iterator *gsi, gassign *stmt) * # _2 = PHI <255(2), _1(3)> * => * [local count: 1073741824]: - * _2 = .SAT_ADD (x_4(D), y_5(D)); */ + * _2 = .SAT_ADD (x_4(D), y_5(D)); + * + * For signed integer: + * _1 = x_5(D) ^ y_6(D); + * _13 = x_5(D) + y_6(D); + * _3 = x_5(D) ^ _13; + * _2 = ~_1; + * _7 = _2 & _3; + * if (_7 >= 0) + * goto ; [59.00%] + * else + * goto ; [41.00%] + * ;; succ: 4 + * ;; 3 + * ;; basic block 3, loop depth 0 + * ;; pred: 2 + * _9 = x_5(D) < 0; + * _10 = (long int) _9; + * _11 = -_10; + * _12 = _11 ^ 9223372036854775807; + * ;; succ: 4 + * ;; basic block 4, loop depth 0 + * ;; pred: 2 + * ;; 3 + * # _4 = PHI <_13(2), _12(3)> + * => + * ;; basic block 2, loop depth 0 + * ;; pred: ENTRY + * _4 = .SAT_ADD (x_5(D), y_6(D)); [tail call] */ static void -match_unsigned_saturation_add (gimple_stmt_iterator *gsi, gphi *phi) +match_saturation_add (gimple_stmt_iterator *gsi, gphi *phi) { if (gimple_phi_num_args (phi) != 2) return; @@ -4097,7 +4128,8 @@ match_unsigned_saturation_add (gimple_stmt_iterator *gsi, gphi *phi) tree ops[2]; tree phi_result = gimple_phi_result (phi); - if (gimple_unsigned_integer_sat_add (phi_result, ops, NULL)) + if (gimple_unsigned_integer_sat_add (phi_result, ops, NULL) + || gimple_signed_integer_sat_add (phi_result, ops, NULL)) build_saturation_binary_arith_call (gsi, phi, IFN_SAT_ADD, phi_result, ops[0], ops[1]); } @@ -6097,7 +6129,7 @@ math_opts_dom_walker::after_dom_children (basic_block bb) gsi_next (&psi)) { gimple_stmt_iterator gsi = gsi_after_labels (bb); - match_unsigned_saturation_add (&gsi, psi.phi ()); + match_saturation_add (&gsi, psi.phi ()); match_unsigned_saturation_sub (&gsi, psi.phi ()); }