From patchwork Fri Jul 26 09:19:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1965222 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=d4+RKfyC; 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 4WVj0J5V4Gz1yY5 for ; Fri, 26 Jul 2024 19:20:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B93AA3870927 for ; Fri, 26 Jul 2024 09:20:29 +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.18]) by sourceware.org (Postfix) with ESMTPS id D66EB3858408 for ; Fri, 26 Jul 2024 09:20:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D66EB3858408 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 D66EB3858408 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.18 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721985604; cv=none; b=ZdcvkteJ7ASxTgJhlgGJpYMxl4bb9VyCINMuW82SI2JQ5Uu3ecLPoRiUfTNTsuiy8DPqxgEYiXVbAcuuC3/TDvitxp64wdzoy73qtSL6rgkzYOyHGg5yOFybXN4TPv9JzXW1/RAgu/ZPm9OMEZqWotQkKf+0P9s1EbK/yqpnZIc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721985604; c=relaxed/simple; bh=zK9UDKJbVl3QWoIx1WT3kDJ0q1Q99iVwJPr5U1Jv5gA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=HVdDmbgv4AHX1DXdGxS4WfEWxJrJe5Wfk6oepJg8fAeRwVjITemWCIMj6icPGHHNSPA8bmRG2tOtPyC7mUk6PukP9pPFIB2n80SOqYl39n5ZwYSjDvErBXnm7Ytrl4uphsut/Kb9663YDdceGDa/qhGhVPOwEzhXtKrB2mRHM2M= 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=1721985602; x=1753521602; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=zK9UDKJbVl3QWoIx1WT3kDJ0q1Q99iVwJPr5U1Jv5gA=; b=d4+RKfyCrFTSGlL02Y0fXA0rDCmWbqwAqKaVkej4cynr9hAjd3qvUqiF Lg7v+jDgLV58TCwf8aGxWCoRs1DC7c+F3Tz1bLcuyZ/v+EO0kLyrQK13/ LOOFv6wE5iDVoXF6AeDhoDHzHuZlcO8Js6ivm1cgl048mbM7/kzDKu77d J75ABT9EC+UNIQ5VBnEiJ+rx/zmwqPy2HlGNWNSYySjRU1K5YIMOT7osd jEHy24yChEkhp4IHXKQk+Akh7FVEUBgpFkyu6k46M/JeBzbRL1eovweC7 rvFdtvg/s4u8Lvae1SN0mHEaR9e+6pHVkL0R6a1oqvwIQx0zT9X3vp6SE w==; X-CSE-ConnectionGUID: K6rmo6XrRX6K/0bBGQMIAQ== X-CSE-MsgGUID: ckMh7d0BSTa8HyKZ5lm+dQ== X-IronPort-AV: E=McAfee;i="6700,10204,11144"; a="19378701" X-IronPort-AV: E=Sophos;i="6.09,238,1716274800"; d="scan'208";a="19378701" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jul 2024 02:20:00 -0700 X-CSE-ConnectionGUID: CcXo3LyRR7OUUFIdwN4AFw== X-CSE-MsgGUID: XeQnmNrvQiCdnpoK7GNi8Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,238,1716274800"; d="scan'208";a="84175844" Received: from shvmail02.sh.intel.com ([10.239.244.9]) by fmviesa001.fm.intel.com with ESMTP; 26 Jul 2024 02:19:59 -0700 Received: from pli-ubuntu.sh.intel.com (pli-ubuntu.sh.intel.com [10.239.159.47]) by shvmail02.sh.intel.com (Postfix) with ESMTP id B4DDA10057EF; Fri, 26 Jul 2024 17:19:57 +0800 (CST) From: pan2.li@intel.com To: gcc-patches@gcc.gnu.org Cc: juzhe.zhong@rivai.ai, kito.cheng@gmail.com, richard.guenther@gmail.com, tamar.christina@arm.com, jeffreyalaw@gmail.com, rdapp.gcc@gmail.com, Pan Li Subject: [PATCH v1] Match: Support .SAT_SUB with IMM op for form 1-4 Date: Fri, 26 Jul 2024 17:19:56 +0800 Message-Id: <20240726091956.2860251-1-pan2.li@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, 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 .SAT_SUB when one of the op is IMM. Aka below 1-4 forms. Form 1: #define DEF_SAT_U_SUB_IMM_FMT_1(T, IMM) \ T __attribute__((noinline)) \ sat_u_sub_imm##IMM##_##T##_fmt_1 (T y) \ { \ return IMM >= y ? IMM - y : 0; \ } Form 2: #define DEF_SAT_U_SUB_IMM_FMT_2(T, IMM) \ T __attribute__((noinline)) \ sat_u_sub_imm##IMM##_##T##_fmt_2 (T y) \ { \ return IMM > y ? IMM - y : 0; \ } Form 3: #define DEF_SAT_U_SUB_IMM_FMT_3(T, IMM) \ T __attribute__((noinline)) \ sat_u_sub_imm##IMM##_##T##_fmt_3 (T x) \ { \ return x >= IMM ? x - IMM : 0; \ } Form 4: #define DEF_SAT_U_SUB_IMM_FMT_4(T, IMM) \ T __attribute__((noinline)) \ sat_u_sub_imm##IMM##_##T##_fmt_4 (T x) \ { \ return x > IMM ? x - IMM : 0; \ } Take below form 1 as example: DEF_SAT_U_SUB_OP0_IMM_FMT_1(uint32_t, 11) Before this patch: 4 │ __attribute__((noinline)) 5 │ uint64_t sat_u_sub_imm11_uint64_t_fmt_1 (uint64_t y) 6 │ { 7 │ uint64_t _1; 8 │ uint64_t _3; 9 │ 10 │ ;; basic block 2, loop depth 0 11 │ ;; pred: ENTRY 12 │ if (y_2(D) <= 11) 13 │ goto ; [50.00%] 14 │ else 15 │ goto ; [50.00%] 16 │ ;; succ: 3 17 │ ;; 4 18 │ 19 │ ;; basic block 3, loop depth 0 20 │ ;; pred: 2 21 │ _3 = 11 - y_2(D); 22 │ ;; succ: 4 23 │ 24 │ ;; basic block 4, loop depth 0 25 │ ;; pred: 2 26 │ ;; 3 27 │ # _1 = PHI <0(2), _3(3)> 28 │ return _1; 29 │ ;; succ: EXIT 30 │ 31 │ } After this patch: 4 │ __attribute__((noinline)) 5 │ uint64_t sat_u_sub_imm11_uint64_t_fmt_1 (uint64_t y) 6 │ { 7 │ uint64_t _1; 8 │ 9 │ ;; basic block 2, loop depth 0 10 │ ;; pred: ENTRY 11 │ _1 = .SAT_SUB (11, y_2(D)); [tail call] 12 │ return _1; 13 │ ;; succ: EXIT 14 │ 15 │ } The below test suites are passed for this patch: 1. The rv64gcv fully regression tests. 2. The x86 bootstrap tests. 3. The x86 fully regression tests. gcc/ChangeLog: * match.pd: Add case 9 and case 10 for .SAT_SUB when one of the op is IMM. Signed-off-by: Pan Li --- gcc/match.pd | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index cf359b0ec0f..b2e7d61790d 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3234,6 +3234,41 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) && types_match (type, @0, @1)))) +/* Unsigned saturation sub with op_0 imm, case 9 (branch with gt): + SAT_U_SUB = IMM > Y ? (IMM - Y) : 0. + = IMM >= Y ? (IMM - Y) : 0. */ +(match (unsigned_integer_sat_sub @0 @1) + (cond^ (le @1 INTEGER_CST@2) (minus INTEGER_CST@0 @1) integer_zerop) + (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) + && types_match (type, @1)) + (with + { + unsigned precision = TYPE_PRECISION (type); + wide_int max = wi::mask (precision, false, precision); + wide_int c0 = wi::to_wide (@0); + wide_int c2 = wi::to_wide (@2); + wide_int c2_add_1 = wi::add (c2, wi::uhwi (1, precision)); + bool equal_p = wi::eq_p (c0, c2); + bool less_than_1_p = !wi::eq_p (c2, max) && wi::eq_p (c2_add_1, c0); + } + (if (equal_p || less_than_1_p))))) + +/* Unsigned saturation sub with op_1 imm, case 10: + SAT_U_SUB = X > IMM ? (X - IMM) : 0. + = X >= IMM ? (X - IMM) : 0. */ +(match (unsigned_integer_sat_sub @0 @1) + (plus (max @0 INTEGER_CST@1) INTEGER_CST@2) + (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) + && types_match (type, @1)) + (with + { + unsigned precision = TYPE_PRECISION (type); + wide_int c1 = wi::to_wide (@1); + wide_int c2 = wi::to_wide (@2); + wide_int sum = wi::add (c1, c2); + } + (if (wi::eq_p (sum, wi::uhwi (0, precision))))))) + /* 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)