From patchwork Wed Aug 7 09:30:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1969939 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=UueNDyDN; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4Wf4gM0Vvmz1ybS for ; Wed, 7 Aug 2024 19:31:26 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 116B8385C6E1 for ; Wed, 7 Aug 2024 09:31:24 +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 [198.175.65.10]) by sourceware.org (Postfix) with ESMTPS id 3AB5D3858C41 for ; Wed, 7 Aug 2024 09:31:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3AB5D3858C41 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 3AB5D3858C41 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723023064; cv=none; b=EsaoZA8xnmeBAwU5zoRxyA6cy60/3pQ3dI+LDZvVHc/7DWb0CWXspfKHyCC0bgLApTMmczVdDRd8rHgYN4eJ0j8Rj/zJN/1+r3sE97Oppy1i432og+L+UeoxPoQPRTo2EgqDTsxUbWENej53k3DYVk6A7UPJqUhqebr8WPtTgHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723023064; c=relaxed/simple; bh=MnT1tCtVS2cusTpSDDKVndy3ZiUp4zbrTel6Cx4uJhs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=cCiAvgBuxvSvibTsXAnAVv9FVeN4BWioLZqW+tD9/ZVJCvAp+/LQKtARUErS+OObnKaYQ+3H6vxzZJ7CTSd2wpPaeEXZ9VLPMq5B0qOoOMCGIT4LINPg8/q2PHzuzEnRyNf3KE2eCu/kwmdS4vV3pXrjwG6zM3EmVduC7/dPPv8= 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=1723023063; x=1754559063; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=MnT1tCtVS2cusTpSDDKVndy3ZiUp4zbrTel6Cx4uJhs=; b=UueNDyDNfbpfjw1tMMt4nHfOTEDSmCQVVg7+1YeM/xpormRY65JZeExS WV0XI8rvtqMw2rOvCYc0L0jVZilUXak01738qyockx0ya9K6Qq/QGKmyf 1pp+Y1QHgr4aON/ASwoIZMSCWLZ1Krie13WDdN3hAB2RJM/xwpnIyk7QL 0Mxb/bmcojJ4Pblmm+nX0oi0mkz27RbOu6Y0DgINqyjfqyOV6v6l19Kd5 IgJSWC6lik5k8LlwKFndjoHUXhp2zh/6d8QOJSjIgtW/6MOYXhE/cDIkM 3RA3mbZktp0Qxm5s/UfEXdv+oRcaVNQvshC8+Ct15QnzcBqmWhBLF5E4B g==; X-CSE-ConnectionGUID: UZ8UZnJsRVqggQnbRyOo5g== X-CSE-MsgGUID: v6/jAnVUR9ieEC5ft918yw== X-IronPort-AV: E=McAfee;i="6700,10204,11156"; a="38539541" X-IronPort-AV: E=Sophos;i="6.09,269,1716274800"; d="scan'208";a="38539541" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2024 02:31:01 -0700 X-CSE-ConnectionGUID: DHfFIYvCSzqnjtOLLfMhAA== X-CSE-MsgGUID: rU6sX5EjSKS27j+WqGgSDw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,269,1716274800"; d="scan'208";a="57023600" Received: from shvmail02.sh.intel.com ([10.239.244.9]) by orviesa006.jf.intel.com with ESMTP; 07 Aug 2024 02:30:58 -0700 Received: from panli.sh.intel.com (panli.sh.intel.com [10.239.154.73]) by shvmail02.sh.intel.com (Postfix) with ESMTP id 2F56210057EE; Wed, 7 Aug 2024 17:30:57 +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 v2] Match: Support form 1 for scalar signed integer .SAT_ADD Date: Wed, 7 Aug 2024 17:30:49 +0800 Message-ID: <20240807093049.1327622-1-pan2.li@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.6 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, MIN, MAX) \ T __attribute__((noinline)) \ sat_s_add_##T##_fmt_1 (T x, T y) \ { \ T sum = x + y; \ return (x ^ y) < 0 \ ? sum \ : (sum ^ x) >= 0 \ ? sum \ : x < 0 ? MIN : MAX; \ } DEF_SAT_S_ADD_FMT_1(int64_t, INT64_MIN, INT64_MAX) 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 │ int64_t sum; 8 │ long int _1; 9 │ long int _2; 10 │ int64_t _3; 11 │ _Bool _8; 12 │ long int _9; 13 │ long int _10; 14 │ long int _11; 15 │ long int _12; 16 │ long int _13; 17 │ 18 │ [local count: 1073741824]: 19 │ sum_6 = x_4(D) + y_5(D); 20 │ _1 = x_4(D) ^ y_5(D); 21 │ _2 = x_4(D) ^ sum_6; 22 │ _12 = ~_1; 23 │ _13 = _2 & _12; 24 │ if (_13 < 0) 25 │ goto ; [41.00%] 26 │ else 27 │ goto ; [59.00%] 28 │ 29 │ [local count: 259738147]: 30 │ _8 = x_4(D) < 0; 31 │ _9 = (long int) _8; 32 │ _10 = -_9; 33 │ _11 = _10 ^ 9223372036854775807; 34 │ 35 │ [local count: 1073741824]: 36 │ # _3 = PHI 37 │ return _3; 38 │ 39 │ } 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 Signed-off-by: Pan Li --- gcc/match.pd | 17 ++++++++++++++++ gcc/tree-ssa-math-opts.cc | 42 ++++++++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index c9c8478d286..8b8a5dbcfe3 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3311,6 +3311,23 @@ 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 sum = X + Y; + SAT_S_ADD = (X ^ Y) < 0 + ? sum + : (sum ^ x) >= 0 + ? sum + : x < 0 ? MIN : MAX; */ +(match (signed_integer_sat_add @0 @1) + (cond^ (lt (bit_and:c (bit_xor:c @0 (convert?@2 (plus:c (convert? @0) + (convert? @1)))) + (bit_not (bit_xor:c @0 @1))) + integer_zerop) + (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value) + @2) + (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..f39c88741a4 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 ()); }