From patchwork Fri Jul 5 12:48:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1957313 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=SDBCZ68c; 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 4WFtck10Fhz1xpP for ; Fri, 5 Jul 2024 22:49:06 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 491573885C09 for ; Fri, 5 Jul 2024 12:49:04 +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.9]) by sourceware.org (Postfix) with ESMTPS id 3175E3884513 for ; Fri, 5 Jul 2024 12:48:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3175E3884513 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 3175E3884513 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720183728; cv=none; b=XFuhezMNkGh9pCaLodUbX0p9FM4ytMksf5TJpWtZQ/HprkxI3ePMMtLKqv16iJpV2skSntQCNVcqW9qeTXijh0pnsBmqUOi4f3m8Q2riVPTr3fhifyx362GV52FdfrYvhSXX0pocjkR2ZS5vvVOgiMvBYUq91aHb9KZQb2L0feE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720183728; c=relaxed/simple; bh=RU1g/nQIyhhyqU3QDsvXw9AWr85vTQCyvrv8XfAjs/4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=RLsieXvEGs0RkmaYzBglptUp8dJMXAbGA10y0CtwDwD+2JTNWhbltnTZ+7KqmR74qMfieZAFwXfbjNaaxxUaceVpCT/UJS2/xuea1CMa7Tm2KXdnoD3Ok1dmQ9FqVr8D68eLDWy33Mlrup7GhwNZUx0pKfDs/OpFtEdUXUkT3+Y= 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=1720183726; x=1751719726; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=RU1g/nQIyhhyqU3QDsvXw9AWr85vTQCyvrv8XfAjs/4=; b=SDBCZ68cM61E/d+fQbSLFCrxCchNgb7b4Jt979XU1/eNM4BAeCUwnn1U bj0YseR40RYG2qm/8gQHuc5KChXqKwlEtaXxgRwuLzFY7pzJj9fbg/9rb CvcoqcPCwCKcQ1DwgsFOY0fPp1qYSkFyRv7a979BLp4GXtDsN5VFSde12 6Dno8JsS5ZrOtf+NkrU08Ww6sAVYGskacEv6ET0TkvyJZBwBQJJmpq0xQ lWBPh6Ys2jInrwJMcSyTkpea8eFk66q5ZHXSaaox2fqxdd6f9oJEmBIr1 Z/fuBuhqDInPCs3q/Uo4YbSz+MkCBrqHfa+FBG/z0wnstp7Dg8rSIchio w==; X-CSE-ConnectionGUID: 77Z3LJF2T2Cekxe7X74reQ== X-CSE-MsgGUID: YEsZ13wiTHWoWEhPjQ2CoQ== X-IronPort-AV: E=McAfee;i="6700,10204,11123"; a="28149449" X-IronPort-AV: E=Sophos;i="6.09,184,1716274800"; d="scan'208";a="28149449" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jul 2024 05:48:45 -0700 X-CSE-ConnectionGUID: uYJUNg57TL2jTpc8fxrEbg== X-CSE-MsgGUID: HDbMnhRKRneiRjovB4Vq+Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,184,1716274800"; d="scan'208";a="51822867" Received: from shvmail02.sh.intel.com ([10.239.244.9]) by orviesa005.jf.intel.com with ESMTP; 05 Jul 2024 05:48:42 -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 2495D1005662; Fri, 5 Jul 2024 20:48:41 +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, hongtao.liu@intel.com, Pan Li Subject: [PATCH v1] Match: Support form 2 for the .SAT_TRUNC Date: Fri, 5 Jul 2024 20:48:39 +0800 Message-Id: <20240705124839.3901430-1-pan2.li@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 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 add form 2 support for the .SAT_TRUNC. Aka: Form 2: #define DEF_SAT_U_TRUC_FMT_2(NT, WT) \ NT __attribute__((noinline)) \ sat_u_truc_##WT##_to_##NT##_fmt_2 (WT x) \ { \ bool overflow = x > (WT)(NT)(-1); \ return overflow ? (NT)-1 : (NT)x; \ } DEF_SAT_U_TRUC_FMT_2(uint32, uint64) Before this patch: 3 │ 4 │ __attribute__((noinline)) 5 │ uint32_t sat_u_truc_uint64_t_to_uint32_t_fmt_2 (uint64_t x) 6 │ { 7 │ uint32_t _1; 8 │ long unsigned int _3; 9 │ 10 │ ;; basic block 2, loop depth 0 11 │ ;; pred: ENTRY 12 │ _3 = MIN_EXPR ; 13 │ _1 = (uint32_t) _3; 14 │ return _1; 15 │ ;; succ: EXIT 16 │ 17 │ } After this patch: 3 │ 4 │ __attribute__((noinline)) 5 │ uint32_t sat_u_truc_uint64_t_to_uint32_t_fmt_2 (uint64_t x) 6 │ { 7 │ uint32_t _1; 8 │ 9 │ ;; basic block 2, loop depth 0 10 │ ;; pred: ENTRY 11 │ _1 = .SAT_TRUNC (x_2(D)); [tail call] 12 │ return _1; 13 │ ;; succ: EXIT 14 │ 15 │ } The below test suites are passed for this patch: 1. The x86 bootstrap test. 2. The x86 fully regression test. 3. The rv64gcv fully regresssion test. gcc/ChangeLog: * match.pd: Add form 2 for .SAT_TRUNC. * tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children): Add new case NOP_EXPR, and try to match SAT_TRUNC. Signed-off-by: Pan Li --- gcc/match.pd | 17 ++++++++++++++++- gcc/tree-ssa-math-opts.cc | 4 ++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/gcc/match.pd b/gcc/match.pd index 4edfa2ae2c9..3759c64d461 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3234,7 +3234,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) && types_match (type, @0, @1)))) -/* Unsigned saturation truncate, case 1 (), sizeof (WT) > sizeof (NT). +/* 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) (bit_ior:c (negate (convert (gt @0 INTEGER_CST@1))) @@ -3250,6 +3250,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) } (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst)))))) +/* Unsigned saturation truncate, case 2, sizeof (WT) > sizeof (NT). + SAT_U_TRUNC = (NT)(MIN_EXPR (X, 255)). */ +(match (unsigned_integer_sat_trunc @0) + (convert (min @0 INTEGER_CST@1)) + (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type) + && TYPE_UNSIGNED (TREE_TYPE (@0))) + (with + { + unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0)); + unsigned otype_precision = TYPE_PRECISION (type); + wide_int trunc_max = wi::mask (otype_precision, false, itype_precision); + wide_int int_cst = wi::to_wide (@1, itype_precision); + } + (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst)))))) + /* 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 a35caf5f058..ac86be8eb94 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -6170,6 +6170,10 @@ math_opts_dom_walker::after_dom_children (basic_block bb) match_unsigned_saturation_sub (&gsi, as_a (stmt)); break; + case NOP_EXPR: + match_unsigned_saturation_trunc (&gsi, as_a (stmt)); + break; + default:; } }