From patchwork Thu Oct 12 10:44:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 151920 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp1132069vqb; Thu, 12 Oct 2023 03:45:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH8b9VtTMSgclTjj/THz/7Ex4htJLvL+aBtHwCX8kb6bVyElLV7huvMWcY6omTKleU7KZCF X-Received: by 2002:a17:906:10d1:b0:9ad:ec25:825b with SMTP id v17-20020a17090610d100b009adec25825bmr18637188ejv.58.1697107528342; Thu, 12 Oct 2023 03:45:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697107528; cv=none; d=google.com; s=arc-20160816; b=ZV1RD2rbAFyel78wHMvTpBScKzmecVIS4psVqtqXgNbTxZO6bjwV9wsCSvbdg/+wZk 6Kzp+r2CaRjOB/nn8iIzTZWD9q2WvdZLOm3ovjPFszGS/W4e34Q77DHlYmU7sufQlx7k PZbM8GnPRW1wsbFPT6hntsgFH/jsAOCHUJ01FCphByIGZN6y/KPbQcL1K+w+Od8ZJchz XQHqFnCmSQzN+Jnf1OIHymgemuMVl9W9+vfdpp0LIaT8xnqYTENUejyxyHS20mn/rsjw /zJyXitm28dm77ykBObAgq+7PpXuK/TEXbwQaWf8WdYa2JPAeNdskGR0uPJCJPDJI67K 3lhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:errors-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :user-agent:subject:cc:to:from:date:dmarc-filter:delivered-to; bh=VSmnlnkrTjo06djeFYxx372wAHumWUHHJ6yLrpZft80=; fh=SxmBKge6TDfxkcsfaq5MGIe/1yu+vISC/5NyjxR3PtE=; b=1LXhlAcpVf+VVPkBgIywJlL7uyfKf9LGtV5tPIknGnoubugmZ5nj9Cfhv2ErrSvLWl avIApu9yr1Oh5s/WE+xJ8U9iYHC3VnyO9qfna9hPjtkTElUn82Ncq4GSzOGr9G3B85uX z0l54KcqMM3JqBJTXP5dxuzCdpxS/uPrZAPn8nhPrk7WPPDjOnozS6O+At686/nMO6GM Xq9imUdH8kpOtAH7IXQ7jqwSFG/8833IgkxqkPzlzxataDG+1Tlu5zCID09SAkD6IxJg XXAfXzgCcUnDa6UfG4A/Ge7F8L3M/rBHaYZNGS6A3DWK5wREWlYfHc8XxGbOUqClHYyo TOIQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id rn4-20020a170906d92400b00991f9e2a81asi7369081ejb.238.2023.10.12.03.45.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 03:45:28 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2F2793858281 for ; Thu, 12 Oct 2023 10:45:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 2B2943858284; Thu, 12 Oct 2023 10:44:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2B2943858284 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id E78FC1F74B; Thu, 12 Oct 2023 10:44:46 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id B4FD52C975; Thu, 12 Oct 2023 10:44:46 +0000 (UTC) Date: Thu, 12 Oct 2023 10:44:46 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: Martin Jambor , fortran@gcc.gnu.org Subject: [PATCH] tree-optimization/111779 - Handle some BIT_FIELD_REFs in SRA User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Level: X-Spamd-Bar: / Authentication-Results: smtp-out2.suse.de; dkim=none; dmarc=none; spf=softfail (smtp-out2.suse.de: 149.44.160.134 is neither permitted nor denied by domain of rguenther@suse.de) smtp.mailfrom=rguenther@suse.de X-Rspamd-Server: rspamd2 X-Spamd-Result: default: False [-0.01 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; TO_DN_SOME(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.20)[suse.de]; R_SPF_SOFTFAIL(0.60)[~all:c]; TO_MATCH_ENVRCPT_SOME(0.00)[]; MISSING_MID(2.50)[]; MX_GOOD(-0.01)[]; NEURAL_HAM_SHORT(-1.00)[-1.000]; VIOLATED_DIRECT_SPF(3.50)[]; RWL_MAILSPIKE_GOOD(0.00)[149.44.160.134:from]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.20)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: -0.01 X-Rspamd-Queue-Id: E78FC1F74B X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, MISSING_MID, SPF_HELO_NONE, SPF_PASS, 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+ouuuleilei=gmail.com@gcc.gnu.org Message-Id: <20231012104523.2F2793858281@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779546223675117390 X-GMAIL-MSGID: 1779546223675117390 The following handles byte-aligned, power-of-two and byte-multiple sized BIT_FIELD_REF reads in SRA. In particular this should cover BIT_FIELD_REFs created by optimize_bit_field_compare. For gcc.dg/tree-ssa/ssa-dse-26.c we now SRA the BIT_FIELD_REF appearing there leading to more DSE, fully eliding the aggregates. This results in the same false positive -Wuninitialized as the older attempt to remove the folding from optimize_bit_field_compare, fixed by initializing part of the aggregate unconditionally. Bootstrapped and tested on x86_64-unknown-linux-gnu for all languages. Martin is on leave so I'll push this tomorrow unless the Fortran folks have objections. Thanks, Richard. PR tree-optimization/111779 gcc/ * tree-sra.cc (sra_handled_bf_read_p): New function. (build_access_from_expr_1): Handle some BIT_FIELD_REFs. (sra_modify_expr): Likewise. (make_fancy_name_1): Skip over BIT_FIELD_REF. gcc/fortran/ * trans-expr.cc (gfc_trans_assignment_1): Initialize lhs_caf_attr and rhs_caf_attr codimension flag to avoid false positive -Wuninitialized. gcc/testsuite/ * gcc.dg/tree-ssa/ssa-dse-26.c: Adjust for more DSE. * gcc.dg/vect/vect-pr111779.c: New testcase. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-26.c | 4 +- gcc/testsuite/gcc.dg/vect/vect-pr111779.c | 56 ++++++++++++++++++++++ gcc/tree-sra.cc | 24 ++++++++-- 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/vect/vect-pr111779.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-26.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-26.c index e3c33f49ef6..43152de5616 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-26.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-26.c @@ -31,5 +31,5 @@ constraint_equal (struct constraint a, struct constraint b) && constraint_expr_equal (a.rhs, b.rhs); } -/* { dg-final { scan-tree-dump-times "Deleted dead store: x = " 1 "dse1" } } */ -/* { dg-final { scan-tree-dump-times "Deleted dead store: y = " 1 "dse1" } } */ +/* { dg-final { scan-tree-dump-times "Deleted dead store: x = " 2 "dse1" } } */ +/* { dg-final { scan-tree-dump-times "Deleted dead store: y = " 2 "dse1" } } */ diff --git a/gcc/testsuite/gcc.dg/vect/vect-pr111779.c b/gcc/testsuite/gcc.dg/vect/vect-pr111779.c new file mode 100644 index 00000000000..79b72aebc78 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/vect-pr111779.c @@ -0,0 +1,56 @@ +#include +#include "tree-vect.h" + +struct C +{ + int c; + int d; + bool f :1; + float e; +}; + +struct A +{ + unsigned int a; + unsigned char c1, c2; + bool b1 : 1; + bool b2 : 1; + bool b3 : 1; + struct C b4; +}; + +void __attribute__((noipa)) +foo (const struct A * __restrict x, int y) +{ + int s = 0, i = 0; + for (i = 0; i < y; ++i) + { + const struct A a = x[i]; + s += a.b4.f ? 1 : 0; + } + if (s != 0) + __builtin_abort (); +} + +int +main () +{ + struct A x[100]; + int i; + + check_vect (); + + __builtin_memset (x, -1, sizeof (x)); +#pragma GCC novect + for (i = 0; i < 100; i++) + { + x[i].b1 = false; + x[i].b2 = false; + x[i].b3 = false; + x[i].b4.f = false; + } + foo (x, 100); + return 0; +} + +/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" { target vect_int } } } */ diff --git a/gcc/tree-sra.cc b/gcc/tree-sra.cc index 56a8ba26135..24d0c20da6a 100644 --- a/gcc/tree-sra.cc +++ b/gcc/tree-sra.cc @@ -1113,6 +1113,21 @@ disqualify_base_of_expr (tree t, const char *reason) disqualify_candidate (t, reason); } +/* Return true if the BIT_FIELD_REF read EXPR is handled by SRA. */ + +static bool +sra_handled_bf_read_p (tree expr) +{ + uint64_t size, offset; + if (bit_field_size (expr).is_constant (&size) + && bit_field_offset (expr).is_constant (&offset) + && size % BITS_PER_UNIT == 0 + && offset % BITS_PER_UNIT == 0 + && pow2p_hwi (size)) + return true; + return false; +} + /* Scan expression EXPR and create access structures for all accesses to candidates for scalarization. Return the created access or NULL if none is created. */ @@ -1123,7 +1138,8 @@ build_access_from_expr_1 (tree expr, gimple *stmt, bool write) struct access *ret = NULL; bool partial_ref; - if (TREE_CODE (expr) == BIT_FIELD_REF + if ((TREE_CODE (expr) == BIT_FIELD_REF + && (write || !sra_handled_bf_read_p (expr))) || TREE_CODE (expr) == IMAGPART_EXPR || TREE_CODE (expr) == REALPART_EXPR) { @@ -1170,6 +1186,7 @@ build_access_from_expr_1 (tree expr, gimple *stmt, bool write) case COMPONENT_REF: case ARRAY_REF: case ARRAY_RANGE_REF: + case BIT_FIELD_REF: ret = create_access (expr, stmt, write); break; @@ -1549,6 +1566,7 @@ make_fancy_name_1 (tree expr) obstack_grow (&name_obstack, buffer, strlen (buffer)); break; + case BIT_FIELD_REF: case ADDR_EXPR: make_fancy_name_1 (TREE_OPERAND (expr, 0)); break; @@ -1564,7 +1582,6 @@ make_fancy_name_1 (tree expr) } break; - case BIT_FIELD_REF: case REALPART_EXPR: case IMAGPART_EXPR: gcc_unreachable (); /* we treat these as scalars. */ @@ -3769,7 +3786,8 @@ sra_modify_expr (tree *expr, gimple_stmt_iterator *gsi, bool write) tree type, bfr, orig_expr; bool partial_cplx_access = false; - if (TREE_CODE (*expr) == BIT_FIELD_REF) + if (TREE_CODE (*expr) == BIT_FIELD_REF + && (write || !sra_handled_bf_read_p (*expr))) { bfr = *expr; expr = &TREE_OPERAND (*expr, 0);