From patchwork Wed May 22 06:19:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1937642 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=Zeoqr3/Y; 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 4Vkh4b6fStz2020 for ; Wed, 22 May 2024 16:20:30 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8EF2C384B112 for ; Wed, 22 May 2024 06:20:27 +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.20]) by sourceware.org (Postfix) with ESMTPS id BAD303858D1E for ; Wed, 22 May 2024 06:20:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BAD303858D1E 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 BAD303858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.20 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716358804; cv=none; b=azbkFwVoz1xbQAsQGbVDIDafNG/PCA5AIql22xzrSLCIltjFgK4ID620Nm8KrOpC3v2ekdiyV6wS5ZDaQF7/PcyC3VZxRTTY1yczfC/NM2k3t8qLjY012SaVn6CHv59HmUM8ucvbTSI7M+nT4AmKdjbNPuYkIKSVBabCEkcNrRw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716358804; c=relaxed/simple; bh=MlOZmT3WP2vWCglFJ6NCb9gV5BCpWyQGlagqqergte4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=MLDf3gUOrsEv7F9XHNoI0fP+FE22GdQtNdbwi/U2d8SliRcrKSCwgSdRdSo2+820zIkfZPo+4ElGlSRyEo8UF4A2dPEhs7xUBoBOLx+m5/3H/z0jQqF+ruwuMnHm4NA9IqPkCUEZ/tEeUd/k+nwooeVg/uLBMyOfzEtJnU1VIJE= 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=1716358804; x=1747894804; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=MlOZmT3WP2vWCglFJ6NCb9gV5BCpWyQGlagqqergte4=; b=Zeoqr3/Y8ZtrrdOiNz/1WeecHioQZ34knUfX9kgi47CyJjEA+j47hRYh vVxZgzNtZ9dbluwzpkgLeOuYccUUHEdS7tWEGO7Y6h8IVjutoo7bDBo5n 9+F3fY0tHW1Y+ANf4mefnAkOr30chmBFYMmZmHoJfoCqqDqXfPAsfuAqN Gir0PTc9lgZdnaATeyHu1hpVlu2BygjckaKZVg+cQVUrjwAnAB9XQ2PDk yGkRjQ0Sy1wSJicK78LbfcjZ81TxQpA9irdIYXa9wfZzii83Kc7H3ufpt 0Qp6rE7+EWbRQuSVwxXKM+EM+4EuWPO0AxJM2TPZFZlW5Da5BO2UYxjej Q==; X-CSE-ConnectionGUID: S2smxASARXmsF4Jh6Mdy2g== X-CSE-MsgGUID: rCgegPOETDmBVbNIoPDvSw== X-IronPort-AV: E=McAfee;i="6600,9927,11079"; a="12440795" X-IronPort-AV: E=Sophos;i="6.08,179,1712646000"; d="scan'208";a="12440795" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 23:19:50 -0700 X-CSE-ConnectionGUID: UuR4SAFpQ5qKWJqigakLCA== X-CSE-MsgGUID: pCC2ap1GSkCTo9gBa2OrEQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,179,1712646000"; d="scan'208";a="33094126" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmviesa007.fm.intel.com with ESMTP; 21 May 2024 23:19:46 -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 C43F510077F8; Wed, 22 May 2024 14:19:45 +0800 (CST) From: pan2.li@intel.com To: gcc-patches@gcc.gnu.org Cc: juzhe.zhong@rivai.ai, kito.cheng@gmail.com, tamar.christina@arm.com, richard.guenther@gmail.com, pinskia@gmail.com, Pan Li Subject: [PATCH v2] Match: Support branch form for unsigned SAT_ADD Date: Wed, 22 May 2024 14:19:35 +0800 Message-Id: <20240522061935.933182-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, RCVD_IN_MSPIKE_H2, 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 branch form for unsigned SAT_ADD. For example as below: uint64_t sat_add (uint64_t x, uint64_t y) { return (uint64_t) (x + y) >= x ? (x + y) : -1; } Different to the branchless version, we leverage the simplify to convert the branch version of SAT_ADD into branchless if and only if the backend has supported the IFN_SAT_ADD. Thus, the backend has the ability to choose branch or branchless implementation of .SAT_ADD. For example, some target can take care of branches code more optimally. When the target implement the IFN_SAT_ADD for unsigned and before this patch: uint64_t sat_add_u_1_uint64_t (uint64_t x, uint64_t y) { long unsigned int _1; uint64_t _2; __complex__ long unsigned int _6; long unsigned int _7; ;; basic block 2, loop depth 0 ;; pred: ENTRY _6 = .ADD_OVERFLOW (x_3(D), y_4(D)); _1 = REALPART_EXPR <_6>; _7 = IMAGPART_EXPR <_6>; if (_7 == 0) goto ; [65.00%] else goto ; [35.00%] ;; succ: 4 ;; 3 ;; basic block 3, loop depth 0 ;; pred: 2 ;; succ: 4 ;; basic block 4, loop depth 0 ;; pred: 3 ;; 2 # _2 = PHI <18446744073709551615(3), _1(2)> return _2; ;; succ: EXIT } After this patch: uint64_t sat_add (uint64_t x, uint64_t y) { long unsigned int _9; ;; basic block 2, loop depth 0 ;; pred: ENTRY _9 = .SAT_ADD (x_3(D), y_4(D)); [tail call] return _9; ;; succ: EXIT } The below test suites are passed for this patch: * The x86 bootstrap test. * The x86 fully regression test. * The riscv fully regression test. gcc/ChangeLog: * match.pd: Add new simplify to convert branch SAT_ADD into branchless, if and only if backend implement the IFN. Signed-off-by: Pan Li --- gcc/match.pd | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index 35e3d82b131..fd8140e6641 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3096,6 +3096,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (match (unsigned_integer_sat_add @0 @1) (bit_ior:c (usadd_left_part_2 @0 @1) (usadd_right_part_2 @0 @1))) +#if GIMPLE + +/* Simplify the branch version of SAT_ADD into branchless if and only if + the backend has supported the IFN_SAT_ADD. Thus, the backend has the + ability to choose branch or branchless implementation of .SAT_ADD. */ + +(simplify + (cond (ge (plus:c@2 @0 @1) @0) @2 integer_minus_onep) + (if (ternary_integer_types_match_p (type, @0, @1) && TYPE_UNSIGNED (type) + && direct_internal_fn_supported_p (IFN_SAT_ADD, type, OPTIMIZE_FOR_BOTH)) + (bit_ior @2 (negate (convert (lt @2 @0)))))) + +(simplify + (cond (le @0 (plus:c@2 @0 @1)) @2 integer_minus_onep) + (if (ternary_integer_types_match_p (type, @0, @1) && TYPE_UNSIGNED (type) + && direct_internal_fn_supported_p (IFN_SAT_ADD, type, OPTIMIZE_FOR_BOTH)) + (bit_ior @2 (negate (convert (lt @2 @0)))))) + +#endif + /* x > y && x != XXX_MIN --> x > y x > y && x == XXX_MIN --> false . */ (for eqne (eq ne)