From patchwork Tue Aug 27 03:20:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Liu, Hongtao" X-Patchwork-Id: 1977095 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=in4Qa0Kj; 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 4WtCVG1Nmfz1yg0 for ; Tue, 27 Aug 2024 13:20:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3F1C6385E459 for ; Tue, 27 Aug 2024 03:20: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.7]) by sourceware.org (Postfix) with ESMTPS id CEF933858C5F for ; Tue, 27 Aug 2024 03:20:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CEF933858C5F 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 CEF933858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724728806; cv=none; b=e2loDmVeLUbSc5sbQgOoqvRCYdsj7M0KotvZHjeaw9YS13nfugf0xvpMw7ILryEw8Nf/4DgK5dDK0tZ2hSOTIVR0NuIOeZupHj8ol1c+gfgaH11yrWygPqn2BcLSS6JPW5FUMbEGHo+LCfxn2DjFtxKtTFDjUbJNxfz4zwZ4kQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724728806; c=relaxed/simple; bh=8OFhDilNmLfkoiEPPciH84s0alaPq9JsN350KwMfuW4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ZHbdjO98nRbnEJxZ05Sy6L3NuVwLCACRDfOaxP5IKGdUzQOGlXN+JYLqiHePWxmGtTqN5VYlA9Z855DSMY3wlbj8bx4MGPmC/yZnNP5l14+9XoyyJ8uvAszwylWzSz5KXzezSM/zHay4GYDO2QlfNThUKXf35+mJSE+1vwoVfmk= 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=1724728804; x=1756264804; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8OFhDilNmLfkoiEPPciH84s0alaPq9JsN350KwMfuW4=; b=in4Qa0KjHHpG9vOVc6KiV28M/bzfAKvK8tclXVD5nzKi9E1RdyVDjGpo 7MXr0rifgiP4gkUj6qhPkpp1KTiE8QA3TLp/tImfEmpOGkJYunaXaN1wM jNatiFNirL114R7DL6BbDtvYwoAl+moJqoxDwlEIT3ls1qMWbvpOImaRD /zZ9XPtKWBrC+QnCBBZsuK7YXa0Ydrc+YyqUPXXGOjZVfmvql6CU57f+R jRQhKWLqVkNQLMzIxOsK9+6PIKPGhNahMpVmCCU5GxDOqJkXaUDaxQO9h anxM5b+N76cIniJ1MI9OyFZttqaW8spaDaP8FJC/E5x40eVKro6Y7ttHx Q==; X-CSE-ConnectionGUID: zX65GsyDQJSBYU7K3+EdFw== X-CSE-MsgGUID: HY/da7W1RweI1NLeE94Z6w== X-IronPort-AV: E=McAfee;i="6700,10204,11176"; a="48571388" X-IronPort-AV: E=Sophos;i="6.10,179,1719903600"; d="scan'208";a="48571388" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 20:20:03 -0700 X-CSE-ConnectionGUID: 4MCaCW3lTT6LuSiroR4H/w== X-CSE-MsgGUID: iU2we6K5RA26PHfiib/T4g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,179,1719903600"; d="scan'208";a="85910494" Received: from shliclel4217.sh.intel.com ([10.239.240.127]) by fmviesa002.fm.intel.com with ESMTP; 26 Aug 2024 20:20:02 -0700 From: liuhongt To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com Subject: [PATCH v2 1/2] Enhance cse_insn to handle all-zeros and all-ones for vector mode. Date: Tue, 27 Aug 2024 11:20:00 +0800 Message-Id: <20240827032001.4000076-1-hongtao.liu@intel.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 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, T_SCC_BODY_TEXT_LINE 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 > You are possibly overwriting src_related_elt - I'd suggest to either break > here or do the loop below for each found elt? Changed. > Do we know that will always succeed? 1) validate_subreg allows subreg for 2 vector modes with same component modes. 2) gen_lowpart in cse.cc is defined as gen_lowpart_if_possible, If it fails, it returns 0, just fallback to src_related = 0. > So on the GIMPLE side we are trying to handle such cases by maintaining > only a single element in the hashtables, thus hash and compare them > the same - them in this case (vec_dup:M (reg:c)) and (vec_dup:N (reg:c)), > leaving it up to the consumer to reject or pun mismatches. rtx_cost will be used to decided if it's profitable ((subreg:M (reg: N) 0) vs (vec_dup:M (reg:c))), if M and N is not tieable, rtx_cost will be expensive and failed the replacement. > > For constants that would hold even more - note CSEing vs. duplicating > constants might not be universally good. Assume you mean (reg:c) in (vec_dup:M (reg:c) is from a constant, the later rtl optimizer (.i.e forwprop/combine) will try to do the further simplication for the constants if rtx_cost is profitable.) For const_vector, it handled by the other codes 5063 /* Try to re-materialize a vec_dup with an existing constant. */ 5064 rtx src_elt; 5065 if ((!src_eqv_here || CONSTANT_P (src_eqv_here)) 5066 && const_vec_duplicate_p (src, &src_elt)) 5067 { Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}. Ok for trunk? Also try to handle redundant broadcasts when there's already a broadcast to a bigger mode with exactly the same component value. For broadcast, component mode needs to be the same. For all-zeros/ones, only need to check the bigger mode. gcc/ChangeLog: PR rtl-optimization/92080 * cse.cc (cse_insn): Handle all-ones/all-zeros, and vec_dup with variables. --- gcc/cse.cc | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/gcc/cse.cc b/gcc/cse.cc index 65794ac5f2c..fab2f515f8c 100644 --- a/gcc/cse.cc +++ b/gcc/cse.cc @@ -4870,6 +4870,50 @@ cse_insn (rtx_insn *insn) } } + /* Try to handle special const_vector with elt 0 or -1. + They can be represented with different modes, and can be cse. */ + if (src_const && src_related == 0 && CONST_VECTOR_P (src_const) + && (src_const == CONST0_RTX (mode) + || src_const == CONSTM1_RTX (mode)) + && GET_MODE_CLASS (mode) == MODE_VECTOR_INT) + { + machine_mode mode_iter; + + for (int l = 0; l != 2; l++) + { + FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_VECTOR_INT) + { + if (maybe_lt (GET_MODE_SIZE (mode_iter), + GET_MODE_SIZE (mode))) + continue; + + rtx src_const_iter = (src_const == CONST0_RTX (mode) + ? CONST0_RTX (mode_iter) + : CONSTM1_RTX (mode_iter)); + + struct table_elt *const_elt + = lookup (src_const_iter, HASH (src_const_iter, mode_iter), + mode_iter); + + if (const_elt == 0) + continue; + + for (const_elt = const_elt->first_same_value; + const_elt; const_elt = const_elt->next_same_value) + if (REG_P (const_elt->exp)) + { + src_related = gen_lowpart (mode, const_elt->exp); + break; + } + + if (src_related != 0) + break; + } + if (src_related != 0) + break; + } + } + /* See if we have a CONST_INT that is already in a register in a wider mode. */ @@ -5041,6 +5085,44 @@ cse_insn (rtx_insn *insn) } } + /* Try to find something like (vec_dup:v16si (reg:c)) + for (vec_dup:v8si (reg:c)). */ + if (src_related == 0 + && VECTOR_MODE_P (mode) + && GET_CODE (src) == VEC_DUPLICATE) + { + poly_uint64 nunits = GET_MODE_NUNITS (GET_MODE (src)) * 2; + rtx inner_elt = XEXP (src, 0); + machine_mode result_mode; + struct table_elt *src_related_elt = NULL;; + while (related_vector_mode (mode, GET_MODE_INNER (mode), + nunits).exists (&result_mode)) + { + rtx vec_dup = gen_rtx_VEC_DUPLICATE (result_mode, inner_elt); + struct table_elt* tmp = lookup (vec_dup, HASH (vec_dup, result_mode), + result_mode); + if (tmp) + { + src_related_elt = tmp; + break; + } + nunits *= 2; + } + + if (src_related_elt) + { + for (src_related_elt = src_related_elt->first_same_value; + src_related_elt; + src_related_elt = src_related_elt->next_same_value) + if (REG_P (src_related_elt->exp)) + { + src_related = gen_lowpart (mode, src_related_elt->exp); + break; + } + } + } + + if (src == src_folded) src_folded = 0;