From patchwork Mon Feb 21 07:45:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1595377 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=IWqZXZaa; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4K2Dqw6QJRz9sFN for ; Mon, 21 Feb 2022 18:45:47 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8F317385E828 for ; Mon, 21 Feb 2022 07:45:45 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 1B9D2385802D for ; Mon, 21 Feb 2022 07:45:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1B9D2385802D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=UZh7l3DenG9b5bltiIJG7iMd8jalS0KZei8uyYuDF2Q=; b=IWqZXZaakrWhJPKAzZBca6w0+i UWMVSJhytVYIeWhR+Tm/fmjfKcY9F+TLAq5BlPHUllUXJw7k3nA+L2+77dMmluwfyIF4Drajyg2Pl JB3PnW/7j9SlAydLuP69nUVIgPd4AgvMupQ+RWVHhjukEAQLKRX3yjyTqrGIWH0+1bmrY8ri/993E GbY4p5Uo9nZicJhcZXMsluHC5bQyeqvjdVTmgf9Bx/D4sCuH7aDYxDeSBF2C/kLlbE82jgK/gqQtn 1sium6ROv4E9zxFPZPwo254irFH3i2hKpg+cfrVU92rNGVLPRw2B+3OOK+iU0lV9jmVpd2r2emmI2 euLQ7u+Q==; Received: from host86-186-213-42.range86-186.btcentralplus.com ([86.186.213.42]:55103 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nM3Np-0002z3-Aj for gcc-patches@gcc.gnu.org; Mon, 21 Feb 2022 02:45:21 -0500 From: "Roger Sayle" To: Subject: [PATCH] Implement constant-folding simplifications of reductions. Date: Mon, 21 Feb 2022 07:45:18 -0000 Message-ID: <003701d826f6$fa3e5930$eebb0b90$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adgm9m2/y3WmrFNRSwGPF6lZ/jTVTA== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 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 Sender: "Gcc-patches" This patch addresses a code quality regression in GCC 12 by implementing some constant folding/simplification transformations for REDUC_PLUS_EXPR in match.pd. The motivating example is gcc.dg/vect/pr89440.c which with -O2 -ffast-math (with vectorization now enabled) gets optimized to: float f (float x) { vector(4) float vect_x_14.11; vector(4) float _2; float _32; _2 = {x_9(D), 0.0, 0.0, 0.0}; vect_x_14.11_29 = _2 + { 1.0e+1, 2.6e+1, 4.2e+1, 5.8e+1 }; _32 = .REDUC_PLUS (vect_x_14.11_29); [tail call] return _32; } With these proposed new transformations, we can simplify the above code even further. float f (float x) { float _32; _32 = x_9(D) + 1.36e+2; return _32; } [which happens to match what we'd produce with -fno-tree-vectorize, and with GCC 11]. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check with no new failures. Ok for mainline? 2022-02-21 Roger Sayle gcc/ChangeLog * fold-const.cc (ctor_single_nonzero_element): New function to return the single non-zero element of a (vector) constructor. * fold-const.h (ctor_single_nonzero_element): Prototype here. * match.pd (reduc (constructor@0)): Simplify reductions of a constructor containing a single non-zero element. (reduc (@0 op VECTOR_CST) -> (reduc @0) op CONST): Simplify reductions of vector operations of the same operator with constant vector operands. gcc/testsuite/ChangeLog * gcc.dg/fold-reduc-1.c: New test case. Thanks in advance, Roger diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 386d573..4283308 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -16792,6 +16792,33 @@ address_compare (tree_code code, tree type, tree op0, tree op1, return equal; } +/* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */ +tree +ctor_single_nonzero_element (const_tree t) +{ + unsigned HOST_WIDE_INT idx; + constructor_elt *ce; + tree elt = NULL_TREE; + + if (TREE_CODE (t) == SSA_NAME) + { + gassign *def = dyn_cast (SSA_NAME_DEF_STMT (t)); + if (gimple_assign_rhs_code (def) == CONSTRUCTOR) + t = gimple_assign_rhs1 (def); + } + + if (TREE_CODE (t) != CONSTRUCTOR) + return NULL_TREE; + for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++) + if (!integer_zerop (ce->value) && !real_zerop (ce->value)) + { + if (elt) + return NULL_TREE; + elt = ce->value; + } + return elt; +} + #if CHECKING_P namespace selftest { diff --git a/gcc/fold-const.h b/gcc/fold-const.h index f217598..b2f0a2f 100644 --- a/gcc/fold-const.h +++ b/gcc/fold-const.h @@ -224,6 +224,7 @@ extern const char *c_getstr (tree); extern wide_int tree_nonzero_bits (const_tree); extern int address_compare (tree_code, tree, tree, tree, tree &, tree &, poly_int64 &, poly_int64 &, bool); +extern tree ctor_single_nonzero_element (const_tree); /* Return OFF converted to a pointer offset type suitable as offset for POINTER_PLUS_EXPR. Use location LOC for this conversion. */ diff --git a/gcc/match.pd b/gcc/match.pd index d9d8359..047fb50 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -7528,6 +7528,20 @@ and, (BIT_FIELD_REF:elt_type @0 { size; } { pos; }) { elt; }))))))) +/* Fold reduction of a single nonzero element constructor. */ +(for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR) + (simplify (reduc (CONSTRUCTOR@0)) + (with { tree elt = ctor_single_nonzero_element (@0); } + (if (elt) + (non_lvalue { elt; }))))) + +/* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */ +(for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX + IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR) + op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor) + (simplify (reduc (op @0 VECTOR_CST@1)) + (op (reduc:type @0) (reduc:type @1)))) + (simplify (vec_perm @0 @1 VECTOR_CST@2) (with diff --git a/gcc/testsuite/gcc.dg/fold-reduc-1.c b/gcc/testsuite/gcc.dg/fold-reduc-1.c new file mode 100644 index 0000000..c8360b0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-reduc-1.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -ffast-math -fdump-tree-optimized" } */ +float foo (float x) +{ + int i; + float j; + float a = 0; + for (i = 0; i < 4; ++i) + { + for (j = 0; j < 4; ++j) + { + a += 1; + x += a; + } + } + return x; +} + +/* { dg-final { scan-tree-dump-not "REDUC_PLUS" "optimized"} } */