From patchwork Wed Oct 9 03:28:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1994542 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=kz0vy/tE; 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 4XNdhQ5FPhz1xsv for ; Wed, 9 Oct 2024 14:31:01 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 63F79385E45E for ; Wed, 9 Oct 2024 03:30:59 +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.17]) by sourceware.org (Postfix) with ESMTPS id 220CD3858D37 for ; Wed, 9 Oct 2024 03:30:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 220CD3858D37 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 220CD3858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.17 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728444635; cv=none; b=BjUQ/caQgpYvmtMyJ8SyVnFMlouBOlsYMDGpj34V0tao0+V8S4LMzpmn00aokOQktDKJq+YhSCA8+wg14kmhJH8su3M6ngMkB1AFm4Cyc824me07Xbu3TO2YkhRItx2jti9bHAg+q0ZpLt1NJG88sjVOt+PhxPwC/Rw50kpDKKA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728444635; c=relaxed/simple; bh=sngPdlyIqIWT+1aR0X7lD0fB6rnigVlqqFtpilpS75E=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=yCMbFpmgcJw/rbTT68UdAjdb1TaaLOZXUf/SOT5U5du3bglLP4rUVT24Y1DGq84YpNgA2l2hsq/qUmX4zRvQWFDtqk/14kZmdczlN6CCp7Yvdc+UFZVsEB6yu4LG+FXI4oN3K22vjrudcLTdyacrKSTV63gzfeFOsZP1gOgnrkk= 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=1728444631; x=1759980631; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=sngPdlyIqIWT+1aR0X7lD0fB6rnigVlqqFtpilpS75E=; b=kz0vy/tETEeqfrlRWdM7UFUIsQa3GJ9FPgFTFPyd/ajucx6yZo+pQyyB qTeX33sqbzUEkAywq91XxVNtZmMQvBp+ObJVQ6IQWwxCZNy7WVDDjptx6 e/5zU/zX0wHQFSurXkLV8Y3EW5oJYlzE5Qvvd1uSPyxAnqg2xOTYoMnt2 hCc0T0vz3VPYvS6Qy9UKpNHn6xmWi+3vhWlAOuH+8UB6nA2lPLNHrFe7A STAGvuvC1WvOs+lnB65M54vr+lbg0NVtMi4yVgpGOJINMMKv+wfqUUkZL viJDc6kK7xq2FlluWPDhamATV/vO1+OyJlItgn69tg98O4Tl5IqwoHf0f w==; X-CSE-ConnectionGUID: 2PBdq66yTtm2J52QgMWLxw== X-CSE-MsgGUID: SMQ7stOaRnq1XbcACGAE/A== X-IronPort-AV: E=McAfee;i="6700,10204,11219"; a="27841713" X-IronPort-AV: E=Sophos;i="6.11,188,1725346800"; d="scan'208";a="27841713" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa109.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 20:30:29 -0700 X-CSE-ConnectionGUID: zJtOkYMPRxC8bpumb5LIEA== X-CSE-MsgGUID: lBM/NLnyR2y80GCjntjH1Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,188,1725346800"; d="scan'208";a="106868305" Received: from panli.sh.intel.com ([10.239.154.73]) by fmviesa001.fm.intel.com with ESMTP; 08 Oct 2024 20:30:28 -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 1/2] Match: Support form 2 for scalar signed integer SAT_TRUNC Date: Wed, 9 Oct 2024 11:28:59 +0800 Message-ID: <20241009032900.638602-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 2 of the scalar signed integer SAT_TRUNC. Aka below example: Form 2: #define DEF_SAT_S_TRUNC_FMT_2(NT, WT, NT_MIN, NT_MAX) \ NT __attribute__((noinline)) \ sat_s_trunc_##WT##_to_##NT##_fmt_2 (WT x) \ { \ NT trunc = (NT)x; \ return (WT)NT_MIN < x && x < (WT)NT_MAX \ ? trunc \ : x < 0 ? NT_MIN : NT_MAX; \ } DEF_SAT_S_TRUNC_FMT_2(int8_t, int16_t, INT8_MIN, INT8_MAX) Before this patch: 4 │ __attribute__((noinline)) 5 │ int8_t sat_s_trunc_int16_t_to_int8_t_fmt_2 (int16_t x) 6 │ { 7 │ int8_t trunc; 8 │ unsigned short x.0_1; 9 │ unsigned short _2; 10 │ int8_t _3; 11 │ _Bool _7; 12 │ signed char _8; 13 │ signed char _9; 14 │ signed char _10; 15 │ 16 │ ;; basic block 2, loop depth 0 17 │ ;; pred: ENTRY 18 │ x.0_1 = (unsigned short) x_4(D); 19 │ _2 = x.0_1 + 127; 20 │ if (_2 > 253) 21 │ goto ; [50.00%] 22 │ else 23 │ goto ; [50.00%] 24 │ ;; succ: 4 25 │ ;; 3 26 │ 27 │ ;; basic block 3, loop depth 0 28 │ ;; pred: 2 29 │ trunc_5 = (int8_t) x_4(D); 30 │ goto ; [100.00%] 31 │ ;; succ: 5 32 │ 33 │ ;; basic block 4, loop depth 0 34 │ ;; pred: 2 35 │ _7 = x_4(D) < 0; 36 │ _8 = (signed char) _7; 37 │ _9 = -_8; 38 │ _10 = _9 ^ 127; 39 │ ;; succ: 5 40 │ 41 │ ;; basic block 5, loop depth 0 42 │ ;; pred: 3 43 │ ;; 4 44 │ # _3 = PHI 45 │ return _3; 46 │ ;; succ: EXIT 47 │ 48 │ } After this patch: 4 │ __attribute__((noinline)) 5 │ int8_t sat_s_trunc_int16_t_to_int8_t_fmt_2 (int16_t x) 6 │ { 7 │ int8_t _3; 8 │ 9 │ ;; basic block 2, loop depth 0 10 │ ;; pred: ENTRY 11 │ _3 = .SAT_TRUNC (x_4(D)); [tail call] 12 │ return _3; 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 2 matching pattern for signed SAT_TRUNC. Signed-off-by: Pan Li --- gcc/match.pd | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index 6a924f409d9..70fdd10926f 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3461,7 +3461,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) } (if (wi::eq_p (trunc_max, int_cst_1) && wi::eq_p (max, int_cst_2)))))) -/* Signed saturation truncate, case 1, sizeof (WT) > sizeof (NT). +/* Signed saturation truncate, case 1 and case 2, sizeof (WT) > sizeof (NT). SAT_S_TRUNC(X) = (unsigned)X + NT_MAX + 1 > Unsigned_MAX ? (NT)X. */ (match (signed_integer_sat_trunc @0) (cond^ (gt (plus:c (convert@4 @0) INTEGER_CST@1) INTEGER_CST@2) @@ -3471,17 +3471,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && !TYPE_UNSIGNED (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@4))) (with { - unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0)); - unsigned otype_precision = TYPE_PRECISION (type); - wide_int offset = wi::uhwi (HOST_WIDE_INT_1U << (otype_precision - 1), itype_precision); - wide_int trunc_max = wi::mask (otype_precision, false, itype_precision); - wide_int max = wi::mask (otype_precision - 1, false, otype_precision); + unsigned itype_prec = TYPE_PRECISION (TREE_TYPE (@0)); + unsigned otype_prec = TYPE_PRECISION (type); + wide_int offset = wi::uhwi (HOST_WIDE_INT_1U << (otype_prec - 1), + itype_prec); // Aka 128 for int8_t + wide_int limit_0 = wi::mask (otype_prec, false, itype_prec); // Aka 255 + wide_int limit_1 = wi::uhwi ((HOST_WIDE_INT_1U << otype_prec) - 3, + itype_prec); // Aka 253 + wide_int otype_max = wi::mask (otype_prec - 1, false, otype_prec); + wide_int itype_max = wi::mask (otype_prec - 1, false, itype_prec); wide_int int_cst_1 = wi::to_wide (@1); wide_int int_cst_2 = wi::to_wide (@2); wide_int int_cst_3 = wi::to_wide (@3); } - (if (wi::eq_p (int_cst_1, offset) && wi::eq_p (int_cst_2, trunc_max) - && wi::eq_p (int_cst_3, max)))))) + (if (((wi::eq_p (int_cst_1, offset) && wi::eq_p (int_cst_2, limit_0)) + || (wi::eq_p (int_cst_1, itype_max) && wi::eq_p (int_cst_2, limit_1))) + && wi::eq_p (int_cst_3, otype_max)))))) /* x > y && x != XXX_MIN --> x > y x > y && x == XXX_MIN --> false . */