From patchwork Wed Jan 26 15:56:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 50457 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7DF8C38515C5 for ; Wed, 26 Jan 2022 15:56:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7DF8C38515C5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643212605; bh=p0miRFuKA2ywEeJM+EQDFbC69YtkB6xncZVX7g+wZ1s=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Q2jhACYtMdLemFmrpaVvmfff/k8Fr5QXL3ksJuzHSikr8h7jVMceVN+hfawlURiRq 0vmXsltSmBIHOHdxT4z/H/5dC8NxO0zy0Cd2QSQTVQzcGhWv850vM4k1BuujDh3qEF Z1s3Q6P16SICOkMPCPWaBfeb2St7IAv/zhppHtM4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 45C713858C78 for ; Wed, 26 Jan 2022 15:56:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 45C713858C78 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 9E50B21904 for ; Wed, 26 Jan 2022 15:56:14 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 88F5413E1A for ; Wed, 26 Jan 2022 15:56:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id XgVAIB5v8WH8MAAAMHmgww (envelope-from ) for ; Wed, 26 Jan 2022 15:56:14 +0000 Date: Wed, 26 Jan 2022 16:56:14 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/104162 - CSE of &MEM[ptr].a[i] and ptr + CST MIME-Version: 1.0 Message-Id: <20220126155614.88F5413E1A@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 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 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: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This adds the capability to value-numbering of treating complex address expressions where the offset becomes invariant as equal to a POINTER_PLUS_EXPR. This restores CSE that is now prevented by early lowering of &MEM[ptr + CST] to a POINTER_PLUS_EXPR. Unfortunately this regresses gcc.dg/asan/pr99673.c again. Bootstrapped and tested on x86_64-unknown-linux-gnu - any opinions? Richard. 2022-01-26 Richard Biener PR tree-optimization/104162 * tree-ssa-sccvn.cc (vn_reference_lookup): Handle &MEM[_1 + 5].a[i] like a POINTER_PLUS_EXPR if the offset becomes invariant. (vn_reference_insert): Likewise. * gcc.dg/tree-ssa/ssa-fre-99.c: New testcase. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-99.c | 27 ++++++++++ gcc/tree-ssa-sccvn.cc | 62 +++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-99.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-99.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-99.c new file mode 100644 index 00000000000..101d0d63f7a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-99.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* Disable FRE1 because that for the sake of __builtin_object_size + will not consider the equality but still valueize 'i', defeating + the purpose of the check. */ +/* { dg-options "-O -fdump-tree-fre3 -fdisable-tree-fre1" } */ + +struct S { int a[4]; }; + +int i; +int bar (struct S *p) +{ + char *q = (char *)p + 4; + i = 1; + int *r = &((struct S *)p)->a[i]; + return q == (char *)r; +} +int baz (struct S *p) +{ + i = 1; + int *r = &((struct S *)p)->a[i]; + char *q = (char *)p + 4; + return q == (char *)r; +} + +/* Verify FRE can handle valueizing &p->a[i] and value-numbering it + equal to a POINTER_PLUS_EXPR. */ +/* { dg-final { scan-tree-dump-times "return 1;" 2 "fre3" } } */ diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index a03f0aae924..482cf372686 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -3669,6 +3669,36 @@ vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind, vr1.vuse = vuse_ssa_val (vuse); vr1.operands = operands = valueize_shared_reference_ops_from_ref (op, &valueized_anything); + + /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. */ + if (operands[0].opcode == ADDR_EXPR + && operands.last ().opcode == SSA_NAME) + { + poly_int64 off = 0; + vn_reference_op_t vro; + unsigned i; + for (i = 1; operands.iterate (i, &vro); ++i) + { + if (vro->opcode == SSA_NAME) + break; + else if (known_eq (vro->off, -1)) + break; + off += vro->off; + } + if (i == operands.length () - 1) + { + gcc_assert (operands[i-1].opcode == MEM_REF); + tree ops[2]; + ops[0] = operands[i].op0; + ops[1] = wide_int_to_tree (sizetype, off); + tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR, + TREE_TYPE (op), ops, NULL); + if (res) + return res; + return NULL_TREE; + } + } + vr1.type = TREE_TYPE (op); ao_ref op_ref; ao_ref_init (&op_ref, op); @@ -3760,13 +3790,43 @@ vn_reference_insert (tree op, tree result, tree vuse, tree vdef) vn_reference_t vr1; bool tem; + vec operands + = valueize_shared_reference_ops_from_ref (op, &tem); + /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. */ + if (operands[0].opcode == ADDR_EXPR + && operands.last ().opcode == SSA_NAME) + { + poly_int64 off = 0; + vn_reference_op_t vro; + unsigned i; + for (i = 1; operands.iterate (i, &vro); ++i) + { + if (vro->opcode == SSA_NAME) + break; + else if (known_eq (vro->off, -1)) + break; + off += vro->off; + } + if (i == operands.length () - 1) + { + gcc_assert (operands[i-1].opcode == MEM_REF); + tree ops[2]; + ops[0] = operands[i].op0; + ops[1] = wide_int_to_tree (sizetype, off); + vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR, + TREE_TYPE (op), ops, result, + VN_INFO (result)->value_id); + return; + } + } + vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s); if (TREE_CODE (result) == SSA_NAME) vr1->value_id = VN_INFO (result)->value_id; else vr1->value_id = get_or_alloc_constant_value_id (result); vr1->vuse = vuse_ssa_val (vuse); - vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy (); + vr1->operands = operands.copy (); vr1->type = TREE_TYPE (op); vr1->punned = false; ao_ref op_ref;