From patchwork Tue Sep 3 12:34:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1980021 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=B+lK3Zxt; 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 4WylTD4mwdz1yfv for ; Tue, 3 Sep 2024 22:35:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03CF0385DDF3 for ; Tue, 3 Sep 2024 12:35:21 +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.21]) by sourceware.org (Postfix) with ESMTPS id 4EB06385DDCA for ; Tue, 3 Sep 2024 12:34:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4EB06385DDCA 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 4EB06385DDCA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725366900; cv=none; b=FnExGgx10N79tdqxmfEwb86AOHFIfiAl3iG4pQAIjGMWRPJkA3ncpbuohLvCwql8u94avK8bRZPz5aldbOCuSyUhGOb+uqpNGIU0smIPKDcRublO4iFysuNVboDSTdwb8gT0qEAMWEJ3gMZDxMZqaK3tXj/Df0tZw8irQgnLJbY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725366900; c=relaxed/simple; bh=2y5Hadjz1g+VX/qEPiD8xg/xgNUqxH5UsjKaQfgeoPM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fzC7XK/8N9GzwOVp6OoFtX4x2SlEp/PbYM9m8fqR8SysGwhTwJG2KUP+t2GH5maPEvjwC0P0Tsv102ih6Gh5VgMJb0JhHV3m2N7PTSWbEaVPNEBPKw+w34rPOHbk0JAkDc4UNz/mjEzlMr8Qwo2mXJNpsOnidCs3RZzyJjxhCQc= 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=1725366898; x=1756902898; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=2y5Hadjz1g+VX/qEPiD8xg/xgNUqxH5UsjKaQfgeoPM=; b=B+lK3ZxtJjcYyzIArDsMZPLgZ/tUQpmJLPpI1TbYQQeZX0BfdzXywNLI RsN0r5jvAmzkNnolcvgUHCid1O1qmWKmsYntfRwmvbW2Ab3i0xJZn8RA3 jRwemMEGjKk7EWZj471Vx1ZEbZRTeDIw1i7U++jR2/RSECxAL8W91gvaQ A6Vll3hHOJ6sntRuOYVXY1VyQwSBOcCctKTAV7MVNRF/epqzmxw5FmAFQ hbOX+siQX0ylwAPlRBfn0OnySbTEjWD3iTzSzXmtvDCXYdCnGobbUDMqN NcxLFCU5CmUIDXfLmlfaFjx2rktvE6Pv8rHPr09cuzqCNjcH5Jva6MolH g==; X-CSE-ConnectionGUID: 01ib01tqTsCgzCYWh3IC+g== X-CSE-MsgGUID: qEje7TilSi+i/6Lg8Z7VJA== X-IronPort-AV: E=McAfee;i="6700,10204,11183"; a="23912354" X-IronPort-AV: E=Sophos;i="6.10,198,1719903600"; d="scan'208";a="23912354" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Sep 2024 05:34:57 -0700 X-CSE-ConnectionGUID: OmwLPHrYTfiEg3GPUnEW1Q== X-CSE-MsgGUID: lc6oqsQHRFiAdfNmR4G+vA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,198,1719903600"; d="scan'208";a="65103566" Received: from panli.sh.intel.com ([10.239.154.73]) by fmviesa010.fm.intel.com with ESMTP; 03 Sep 2024 05:34:55 -0700 From: pan2.li@intel.com To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, Tamar.Christina@arm.com, juzhe.zhong@rivai.ai, kito.cheng@gmail.com, jeffreyalaw@gmail.com, rdapp.gcc@gmail.com, Pan Li Subject: [PATCH v1] Match: Support form 2 for scalar signed integer .SAT_ADD Date: Tue, 3 Sep 2024 20:34:11 +0800 Message-ID: <20240903123411.788850-1-pan2.li@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 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, T_SCC_BODY_TEXT_LINE 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 2 of the scalar signed integer .SAT_ADD. Aka below example: Form 2: #define DEF_SAT_S_ADD_FMT_2(T, UT, MIN, MAX) \ T __attribute__((noinline)) \ sat_s_add_##T##_fmt_2 (T x, T y) \ { \ T sum = (UT)x + (UT)y; \ \ if ((x ^ y) < 0 || (sum ^ x) >= 0) \ return sum; \ \ return x < 0 ? MIN : MAX; \ } DEF_SAT_S_ADD_FMT_2(int8_t, uint8_t, INT8_MIN, INT8_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 │ int8_t sat_s_add_int8_t_fmt_2 (int8_t x, int8_t y) 6 │ { 7 │ int8_t sum; 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 _22; 19 │ signed char _23; 20 │ 21 │ ;; basic block 2, loop depth 0 22 │ ;; pred: ENTRY 23 │ x.0_1 = (unsigned char) x_7(D); 24 │ y.1_2 = (unsigned char) y_8(D); 25 │ _3 = x.0_1 + y.1_2; 26 │ sum_9 = (int8_t) _3; 27 │ _4 = x_7(D) ^ y_8(D); 28 │ _5 = x_7(D) ^ sum_9; 29 │ _23 = ~_4; 30 │ _22 = _5 & _23; 31 │ if (_22 >= 0) 32 │ goto ; [42.57%] 33 │ else 34 │ goto ; [57.43%] 35 │ ;; succ: 4 36 │ ;; 3 37 │ 38 │ ;; basic block 3, loop depth 0 39 │ ;; pred: 2 40 │ _11 = x_7(D) < 0; 41 │ _12 = (signed char) _11; 42 │ _13 = -_12; 43 │ _14 = _13 ^ 127; 44 │ ;; succ: 4 45 │ 46 │ ;; basic block 4, loop depth 0 47 │ ;; pred: 2 48 │ ;; 3 49 │ # _6 = PHI 50 │ return _6; 51 │ ;; succ: EXIT 52 │ 53 │ } After this patch: 4 │ __attribute__((noinline)) 5 │ int8_t sat_s_add_int8_t_fmt_2 (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_ADD (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 the form 2 of signed .SAT_ADD matching. Signed-off-by: Pan Li --- gcc/match.pd | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index 4298e89dad6..1372f2ba377 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3207,6 +3207,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type) && types_match (type, @0, @1)))) +/* Signed saturation add, case 2: + T sum = (T)((UT)X + (UT)Y) + SAT_S_ADD = (X ^ sum) & !(X ^ Y) >= 0 ? sum : (-(T)(X < 0) ^ MAX); + + The T and UT are type pair like T=int8_t, UT=uint8_t. */ +(match (signed_integer_sat_add @0 @1) + (cond^ (ge (bit_and:c (bit_xor:c @0 (nop_convert@2 (plus (nop_convert @0) + (nop_convert @1)))) + (bit_not (bit_xor:c @0 @1))) + integer_zerop) + @2 + (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value)) + (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type) + && types_match (type, @0, @1)))) + /* Unsigned saturation sub, case 1 (branch with gt): SAT_U_SUB = X > Y ? X - Y : 0 */ (match (unsigned_integer_sat_sub @0 @1)