From patchwork Fri Jun 28 03:44:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1953673 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=jtpgcelv; 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 4W9Lsf3z84z20Xf for ; Fri, 28 Jun 2024 13:44:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1C3203882079 for ; Fri, 28 Jun 2024 03:44:33 +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.14]) by sourceware.org (Postfix) with ESMTPS id 5BBF238313B6 for ; Fri, 28 Jun 2024 03:44:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5BBF238313B6 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 5BBF238313B6 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719546255; cv=none; b=bPt3yWvI9mLdC9sSBa03BdSn0QCxPlmkoyIrNYUyqueGpqKLiR8rRUshh8kL6hGTTD66bTmwHGp/+CpiJEm2KugA/0XNeupQj8fewI2CNz2sQC3/kfq3+aaND8ShCS/AV+K55HwC/vpMlWIXwcfc5iLsokClc/c3PuZKVaoRDpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719546255; c=relaxed/simple; bh=w+r5nCE9jBYGgrvcHz/Kb3ugCsp6YBAco3UUcTTmEe4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=XRwRlksHpxJea8Hqe7xeuT9hvm+tiveQdrLf+N4eTj8DCx87QlAxNdm3behh8Picikw37MVDOw80NzK57RxVHgT4hxya80n3U37KwRZuTNch49gXq+SygNQhB6625Jt6My3rHwQi7jZO3QEGUEa2Nf4hA4vQmhaSVDQFX33K2mA= 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=1719546254; x=1751082254; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=w+r5nCE9jBYGgrvcHz/Kb3ugCsp6YBAco3UUcTTmEe4=; b=jtpgcelv0nz26LrlrIeWFiJPmVvSrw50n6mxzBMhKHrvsN39JhSoOM9k UhSLdmJ83Z3olncJL7czSz9JpD6UuWHxrmQjS//r3PFHqZhD19ArGdVps h8/ZprqUTfsfGXcSaLd6fnsGlxWYVf9tnnL4iWcUrob/lR0L6wYqE+4kC lt9SCDjpTC51CtZmb7/mRg72aIc6XRPKC8hyms5HOCjWVCgzOKMNrZugQ AVhD8zDJpDWQMnrutyhUO80hNVL3K1/BZaprXFAzlMJiy4mblNLYtVCI3 IZmbeUSJT6+O1eZfzEyt9212QJyYdWLwFc+57ZXsPO3I8/y6YMNFv+K4A w==; X-CSE-ConnectionGUID: 2/lPGURlQVKS4jb/LTX+zQ== X-CSE-MsgGUID: Y3XgZfRQQ62J8KaW3Q8+3w== X-IronPort-AV: E=McAfee;i="6700,10204,11116"; a="16940723" X-IronPort-AV: E=Sophos;i="6.09,167,1716274800"; d="scan'208";a="16940723" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2024 20:44:12 -0700 X-CSE-ConnectionGUID: yHnXeGJcQt+9P491Zn6Krg== X-CSE-MsgGUID: vkf/evmdR+SnZVsBtOkgMg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,167,1716274800"; d="scan'208";a="49199766" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmviesa004.fm.intel.com with ESMTP; 27 Jun 2024 20:44:09 -0700 Received: from pli-ubuntu.sh.intel.com (pli-ubuntu.sh.intel.com [10.239.159.47]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 2C06A100568C; Fri, 28 Jun 2024 11:44:08 +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, jeffreyalaw@gmail.com, rdapp.gcc@gmail.com, Pan Li Subject: [PATCH v1] Match: Support imm form for unsigned scalar .SAT_ADD Date: Fri, 28 Jun 2024 11:44:06 +0800 Message-Id: <20240628034406.2657998-1-pan2.li@intel.com> X-Mailer: git-send-email 2.34.1 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 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 of unsigned scalar .SAT_ADD when one of the op is IMM. For example as below: Form IMM: #define DEF_SAT_U_ADD_IMM_FMT_1(T) \ T __attribute__((noinline)) \ sat_u_add_imm_##T##_fmt_1 (T x) \ { \ return (T)(x + 9) >= x ? (x + 9) : -1; \ } DEF_SAT_U_ADD_IMM_FMT_1(uint64_t) Before this patch: __attribute__((noinline)) uint64_t sat_u_add_imm_uint64_t_fmt_1 (uint64_t x) { long unsigned int _1; uint64_t _3; ;; basic block 2, loop depth 0 ;; pred: ENTRY _1 = MIN_EXPR ; _3 = _1 + 9; return _3; ;; succ: EXIT } After this patch: __attribute__((noinline)) uint64_t sat_u_add_imm_uint64_t_fmt_1 (uint64_t x) { uint64_t _3; ;; basic block 2, loop depth 0 ;; pred: ENTRY _3 = .SAT_ADD (x_2(D), 9); [tail call] return _3; ;; succ: EXIT } The below test suites are passed for this patch: 1. The rv64gcv fully regression test with newlib. 2. The x86 bootstrap test. 3. The x86 fully regression test. gcc/ChangeLog: * match.pd: Add imm form for .SAT_ADD matching. * tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children): Add .SAT_ADD matching under PLUS_EXPR. Signed-off-by: Pan Li --- gcc/match.pd | 22 ++++++++++++++++++++++ gcc/tree-ssa-math-opts.cc | 2 ++ 2 files changed, 24 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index 3fa3f2e8296..d738c7ee9b4 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3154,6 +3154,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (match (unsigned_integer_sat_add @0 @1) (cond^ (gt @0 (usadd_left_part_1@2 @0 @1)) integer_minus_onep @2)) +/* Unsigned saturation add, case 9 (one op is imm): + SAT_U_ADD = (X + 3) >= x ? (X + 3) : -1. */ +(match (unsigned_integer_sat_add @0 @1) + (plus:c (min @0 INTEGER_CST@2) INTEGER_CST@1) + (with { + unsigned precision = TYPE_PRECISION (type); + wide_int cst_1 = wi::to_wide (@1, precision); + wide_int cst_2 = wi::to_wide (@2, precision); + wide_int max = wi::mask (precision, false, precision); + wide_int sum = wi::add (cst_1, cst_2); + } + (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) + && types_match (type, @0, @1) && wi::eq_p (max, sum))))) + +/* Unsigned saturation add, case 10 (one op is imm): + SAT_U_ADD = __builtin_add_overflow (X, 3, &ret) == 0 ? ret : -1. */ +(match (unsigned_integer_sat_add @0 @1) + (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c@2 @0 INTEGER_CST@1)) integer_zerop) + integer_minus_onep (realpart @2)) + (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) + && types_match (type, @0)))) + /* Unsigned saturation sub, case 1 (branch with gt): SAT_U_SUB = X > Y ? X - Y : 0 */ (match (unsigned_integer_sat_sub @0 @1) diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index 3783a874699..3b5433ec000 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -6195,6 +6195,8 @@ math_opts_dom_walker::after_dom_children (basic_block bb) break; case PLUS_EXPR: + match_unsigned_saturation_add (&gsi, as_a (stmt)); + /* fall-through */ case MINUS_EXPR: if (!convert_plusminus_to_widen (&gsi, stmt, code)) {