Message ID | 1635328798-30341-1-git-send-email-apinski@marvell.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> 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 21644385841A for <patchwork@sourceware.org>; Wed, 27 Oct 2021 10:00:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 21644385841A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1635328838; bh=daO7xcypHfcinAKaVYJMWUk59NIIop6bg9IjI6naUkM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=doBuErmIa/Z2K0wyuOJUZM4Ocuz9EOoJDcWYAcHMz7Z5+TIkoXd6XujGVfiae96ly sqHP50RgYjIaUsgQ9YfGuA7Tm/0TaYRhXacc/VTAXKA3/AfM8VeZY62NfkJiUta9td KY8pDhSKM01xQsti9CXN1t3PfUgTdrJbHc1b4KOE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 1699A3858D39 for <gcc-patches@gcc.gnu.org>; Wed, 27 Oct 2021 10:00:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1699A3858D39 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19R6I12b032271 for <gcc-patches@gcc.gnu.org>; Wed, 27 Oct 2021 03:00:08 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com with ESMTP id 3by1ca8tkw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for <gcc-patches@gcc.gnu.org>; Wed, 27 Oct 2021 03:00:08 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Wed, 27 Oct 2021 03:00:04 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.18 via Frontend Transport; Wed, 27 Oct 2021 03:00:04 -0700 Received: from linux.wrightpinski.org.com (unknown [10.69.242.198]) by maili.marvell.com (Postfix) with ESMTP id DF8273F70D7; Wed, 27 Oct 2021 03:00:01 -0700 (PDT) To: <gcc-patches@gcc.gnu.org> Subject: [V2/PATCH] Fix tree-optimization/102216: missed optimization causing Warray-bounds Date: Wed, 27 Oct 2021 02:59:58 -0700 Message-ID: <1635328798-30341-1-git-send-email-apinski@marvell.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-GUID: JxOi64KrA_58O3lNsoCEZpN3yBXOcO-o X-Proofpoint-ORIG-GUID: JxOi64KrA_58O3lNsoCEZpN3yBXOcO-o X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.182.1,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.0.607.475 definitions=2021-10-27_03,2021-10-26_01,2020-04-07_01 X-Spam-Status: No, score=-14.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: apinski--- via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: apinski@marvell.com Cc: Andrew Pinski <apinski@marvell.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
[V2/PATCH] Fix tree-optimization/102216: missed optimization causing Warray-bounds
|
|
Commit Message
Li, Pan2 via Gcc-patches
Oct. 27, 2021, 9:59 a.m. UTC
From: Andrew Pinski <apinski@marvell.com>
The problem here is tree-ssa-forwprop.c likes to produce
&MEM <const char *> [(void *)_4 + 152B] which is the same as
_4 p+ 152 which the rest of GCC likes better.
This implements this transformation back to pointer plus to
improve better code generation later on.
OK? Bootstrapped and tested on aarch64-linux-gnu.
Changes from v1:
* v2: Add comments.
gcc/ChangeLog:
PR tree-optimization/102216
* tree-ssa-forwprop.c (rewrite_assign_addr): New function.
(forward_propagate_addr_expr_1): Use rewrite_assign_addr
when rewriting into the addr_expr into an assignment.
gcc/testsuite/ChangeLog:
PR tree-optimization/102216
* g++.dg/tree-ssa/pr102216.C: New test.
---
gcc/testsuite/g++.dg/tree-ssa/pr102216.C | 22 +++++++++
gcc/tree-ssa-forwprop.c | 58 ++++++++++++++++++------
2 files changed, 67 insertions(+), 13 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr102216.C
Comments
On 27 October 2021 11:59:58 CEST, apinski--- via Gcc-patches <gcc-patches@gcc.gnu.org> wrote: >From: Andrew Pinski <apinski@marvell.com> > >The problem here is tree-ssa-forwprop.c likes to produce >&MEM <const char *> [(void *)_4 + 152B] which is the same as >_4 p+ 152 which the rest of GCC likes better. >This implements this transformation back to pointer plus to >improve better code generation later on. > >OK? Bootstrapped and tested on aarch64-linux-gnu. > >Changes from v1: >* v2: Add comments. > >gcc/ChangeLog: > > PR tree-optimization/102216 > * tree-ssa-forwprop.c (rewrite_assign_addr): New function. > (forward_propagate_addr_expr_1): Use rewrite_assign_addr > when rewriting into the addr_expr into an assignment. > >gcc/testsuite/ChangeLog: > > PR tree-optimization/102216 > * g++.dg/tree-ssa/pr102216.C: New test. >--- > gcc/testsuite/g++.dg/tree-ssa/pr102216.C | 22 +++++++++ > gcc/tree-ssa-forwprop.c | 58 ++++++++++++++++++------ > 2 files changed, 67 insertions(+), 13 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr102216.C > >diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr102216.C b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C >new file mode 100644 >index 00000000000..b903e4eb57d >--- /dev/null >+++ b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C >@@ -0,0 +1,22 @@ >+/* { dg-options "-O2 -fdump-tree-optimized" } */ >+void link_error (); >+void g () >+{ >+ const char **language_names; >+ >+ language_names = new const char *[6]; >+ >+ const char **language_names_p = language_names; >+ >+ language_names_p++; >+ language_names_p++; >+ language_names_p++; >+ >+ if ( (language_names_p) - (language_names+3) != 0) >+ link_error(); >+ delete[] language_names; >+} >+/* We should have removed the link_error on the gimple level as GCC should >+ be able to tell that language_names_p is the same as language_names+3. */ >+/* { dg-final { scan-tree-dump-times "link_error" 0 "optimized" } } */ >+ >diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c >index a830bab78ba..e4331c60525 100644 >--- a/gcc/tree-ssa-forwprop.c >+++ b/gcc/tree-ssa-forwprop.c >@@ -637,6 +637,47 @@ forward_propagate_into_cond (gimple_stmt_iterator *gsi_p) > return 0; > } > >+/* Rewrite the DEF_RHS as needed into the (plain) use statement. */ >+ >+static void >+rewrite_assign_addr (gimple_stmt_iterator *use_stmt_gsi, tree def_rhs) >+{ >+ tree def_rhs_base; >+ poly_int64 def_rhs_offset; >+ >+ /* Get the base and offset. */ >+ if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), >+ &def_rhs_offset))) >+ { >+ tree new_ptr; >+ poly_offset_int off = 0; >+ >+ /* If the base was a MEM, then add the offset to the other >+ offset and adjust the base. */ >+ if (TREE_CODE (def_rhs_base) == MEM_REF) >+ { >+ off += mem_ref_offset (def_rhs_base); >+ new_ptr = TREE_OPERAND (def_rhs_base, 0); >+ } >+ else >+ new_ptr = build_fold_addr_expr (def_rhs_base); >+ >+ /* If we have the new base is not an address express, then use a p+ expression >+ as the new expression instead of &MEM[x, offset]. */ >+ if (TREE_CODE (new_ptr) != ADDR_EXPR) >+ { >+ tree offset = wide_int_to_tree (sizetype, off); >+ def_rhs = build2 (POINTER_PLUS_EXPR, TREE_TYPE (def_rhs), new_ptr, offset); >+ } >+ } >+ >+ /* Replace the rhs with the new expression. */ >+ def_rhs = unshare_expr (def_rhs); >+ gimple_assign_set_rhs_from_tree (use_stmt_gsi, def_rhs); >+ gimple *use_stmt = gsi_stmt (*use_stmt_gsi); >+ update_stmt (use_stmt); >+} >+ > /* We've just substituted an ADDR_EXPR into stmt. Update all the > relevant data structures to match. */ > >@@ -696,8 +737,8 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > if (single_use_p > && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))) > { >- gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs)); >- gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs)); >+ rewrite_assign_addr (use_stmt_gsi, def_rhs); >+ gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > return true; > } > >@@ -741,14 +782,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p)) > return true; > >- if (useless_type_conversion_p (TREE_TYPE (lhs), >- TREE_TYPE (new_def_rhs))) >- gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs), >- new_def_rhs); >- else if (is_gimple_min_invariant (new_def_rhs)) >- gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs); >- else >- return false; >+ rewrite_assign_addr (use_stmt_gsi, new_def_rhs); > gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > update_stmt (use_stmt); ISTM the above update_stmt is redundant now? thanks, > return true; >@@ -951,9 +985,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > unshare_expr (def_rhs), > fold_convert (ptr_type_node, > rhs2))); >- gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs); >- use_stmt = gsi_stmt (*use_stmt_gsi); >- update_stmt (use_stmt); >+ rewrite_assign_addr (use_stmt_gsi, new_rhs); > tidy_after_forward_propagate_addr (use_stmt); > return true; > }
On Wed, Oct 27, 2021 at 12:00 PM apinski--- via Gcc-patches <gcc-patches@gcc.gnu.org> wrote: > > From: Andrew Pinski <apinski@marvell.com> > > The problem here is tree-ssa-forwprop.c likes to produce > &MEM <const char *> [(void *)_4 + 152B] which is the same as > _4 p+ 152 which the rest of GCC likes better. > This implements this transformation back to pointer plus to > improve better code generation later on. Why do you think so? Can you pin-point the transform that now fixes the new testcase? Comments below > OK? Bootstrapped and tested on aarch64-linux-gnu. > > Changes from v1: > * v2: Add comments. > > gcc/ChangeLog: > > PR tree-optimization/102216 > * tree-ssa-forwprop.c (rewrite_assign_addr): New function. > (forward_propagate_addr_expr_1): Use rewrite_assign_addr > when rewriting into the addr_expr into an assignment. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/102216 > * g++.dg/tree-ssa/pr102216.C: New test. > --- > gcc/testsuite/g++.dg/tree-ssa/pr102216.C | 22 +++++++++ > gcc/tree-ssa-forwprop.c | 58 ++++++++++++++++++------ > 2 files changed, 67 insertions(+), 13 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr102216.C b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > new file mode 100644 > index 00000000000..b903e4eb57d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > @@ -0,0 +1,22 @@ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > +void link_error (); > +void g () > +{ > + const char **language_names; > + > + language_names = new const char *[6]; > + > + const char **language_names_p = language_names; > + > + language_names_p++; > + language_names_p++; > + language_names_p++; > + > + if ( (language_names_p) - (language_names+3) != 0) > + link_error(); > + delete[] language_names; > +} > +/* We should have removed the link_error on the gimple level as GCC should > + be able to tell that language_names_p is the same as language_names+3. */ > +/* { dg-final { scan-tree-dump-times "link_error" 0 "optimized" } } */ > + > diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c > index a830bab78ba..e4331c60525 100644 > --- a/gcc/tree-ssa-forwprop.c > +++ b/gcc/tree-ssa-forwprop.c > @@ -637,6 +637,47 @@ forward_propagate_into_cond (gimple_stmt_iterator *gsi_p) > return 0; > } > > +/* Rewrite the DEF_RHS as needed into the (plain) use statement. */ > + > +static void > +rewrite_assign_addr (gimple_stmt_iterator *use_stmt_gsi, tree def_rhs) > +{ > + tree def_rhs_base; > + poly_int64 def_rhs_offset; > + > + /* Get the base and offset. */ > + if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), > + &def_rhs_offset))) So this will cause us to rewrite &MEM[p_1].a.b.c; to a pointer-plus, right? Don't we want to preserve that for object-size stuff? So maybe directly pattern match ADDR_EXPR <MEM_REF <SSA_NAME, ..>> only? > + { > + tree new_ptr; > + poly_offset_int off = 0; > + > + /* If the base was a MEM, then add the offset to the other > + offset and adjust the base. */ > + if (TREE_CODE (def_rhs_base) == MEM_REF) > + { > + off += mem_ref_offset (def_rhs_base); > + new_ptr = TREE_OPERAND (def_rhs_base, 0); > + } > + else > + new_ptr = build_fold_addr_expr (def_rhs_base); > + > + /* If we have the new base is not an address express, then use a p+ expression > + as the new expression instead of &MEM[x, offset]. */ > + if (TREE_CODE (new_ptr) != ADDR_EXPR) > + { > + tree offset = wide_int_to_tree (sizetype, off); > + def_rhs = build2 (POINTER_PLUS_EXPR, TREE_TYPE (def_rhs), new_ptr, offset); Ick. You should be able to use gimple_assign_set_rhs_with_ops. > + } > + } > + > + /* Replace the rhs with the new expression. */ > + def_rhs = unshare_expr (def_rhs); and definitely no need to unshare anything here? > + gimple_assign_set_rhs_from_tree (use_stmt_gsi, def_rhs); > + gimple *use_stmt = gsi_stmt (*use_stmt_gsi); > + update_stmt (use_stmt); > +} > + > /* We've just substituted an ADDR_EXPR into stmt. Update all the > relevant data structures to match. */ > > @@ -696,8 +737,8 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > if (single_use_p > && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))) > { > - gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs)); > - gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs)); > + rewrite_assign_addr (use_stmt_gsi, def_rhs); > + gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > return true; > } > > @@ -741,14 +782,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p)) > return true; > > - if (useless_type_conversion_p (TREE_TYPE (lhs), > - TREE_TYPE (new_def_rhs))) > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs), > - new_def_rhs); > - else if (is_gimple_min_invariant (new_def_rhs)) > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs); > - else > - return false; > + rewrite_assign_addr (use_stmt_gsi, new_def_rhs); > gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > update_stmt (use_stmt); > return true; > @@ -951,9 +985,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > unshare_expr (def_rhs), > fold_convert (ptr_type_node, > rhs2))); > - gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs); > - use_stmt = gsi_stmt (*use_stmt_gsi); > - update_stmt (use_stmt); > + rewrite_assign_addr (use_stmt_gsi, new_rhs); so you only do this after addr_expr forwarding but not on stmts in general? You could do it that way in the 2nd loop over the BB. > tidy_after_forward_propagate_addr (use_stmt); > return true; > } > -- > 2.17.1 >
On 10/27/21 3:59 AM, apinski--- via Gcc-patches wrote: > From: Andrew Pinski <apinski@marvell.com> > > The problem here is tree-ssa-forwprop.c likes to produce > &MEM <const char *> [(void *)_4 + 152B] which is the same as > _4 p+ 152 which the rest of GCC likes better. > This implements this transformation back to pointer plus to > improve better code generation later on. Since the purpose of this transformation is to avoid a bogus -Warray-bounds can you please include a test case showing the difference it makes? (I.e., one that warns without the patch and doesn't with it. The test in the patch doesn't trigger a warning for me.) Thanks Martin > > OK? Bootstrapped and tested on aarch64-linux-gnu. > > Changes from v1: > * v2: Add comments. > > gcc/ChangeLog: > > PR tree-optimization/102216 > * tree-ssa-forwprop.c (rewrite_assign_addr): New function. > (forward_propagate_addr_expr_1): Use rewrite_assign_addr > when rewriting into the addr_expr into an assignment. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/102216 > * g++.dg/tree-ssa/pr102216.C: New test. > --- > gcc/testsuite/g++.dg/tree-ssa/pr102216.C | 22 +++++++++ > gcc/tree-ssa-forwprop.c | 58 ++++++++++++++++++------ > 2 files changed, 67 insertions(+), 13 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr102216.C b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > new file mode 100644 > index 00000000000..b903e4eb57d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > @@ -0,0 +1,22 @@ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > +void link_error (); > +void g () > +{ > + const char **language_names; > + > + language_names = new const char *[6]; > + > + const char **language_names_p = language_names; > + > + language_names_p++; > + language_names_p++; > + language_names_p++; > + > + if ( (language_names_p) - (language_names+3) != 0) > + link_error(); > + delete[] language_names; > +} > +/* We should have removed the link_error on the gimple level as GCC should > + be able to tell that language_names_p is the same as language_names+3. */ > +/* { dg-final { scan-tree-dump-times "link_error" 0 "optimized" } } */ > + > diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c > index a830bab78ba..e4331c60525 100644 > --- a/gcc/tree-ssa-forwprop.c > +++ b/gcc/tree-ssa-forwprop.c > @@ -637,6 +637,47 @@ forward_propagate_into_cond (gimple_stmt_iterator *gsi_p) > return 0; > } > > +/* Rewrite the DEF_RHS as needed into the (plain) use statement. */ > + > +static void > +rewrite_assign_addr (gimple_stmt_iterator *use_stmt_gsi, tree def_rhs) > +{ > + tree def_rhs_base; > + poly_int64 def_rhs_offset; > + > + /* Get the base and offset. */ > + if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), > + &def_rhs_offset))) > + { > + tree new_ptr; > + poly_offset_int off = 0; > + > + /* If the base was a MEM, then add the offset to the other > + offset and adjust the base. */ > + if (TREE_CODE (def_rhs_base) == MEM_REF) > + { > + off += mem_ref_offset (def_rhs_base); > + new_ptr = TREE_OPERAND (def_rhs_base, 0); > + } > + else > + new_ptr = build_fold_addr_expr (def_rhs_base); > + > + /* If we have the new base is not an address express, then use a p+ expression > + as the new expression instead of &MEM[x, offset]. */ > + if (TREE_CODE (new_ptr) != ADDR_EXPR) > + { > + tree offset = wide_int_to_tree (sizetype, off); > + def_rhs = build2 (POINTER_PLUS_EXPR, TREE_TYPE (def_rhs), new_ptr, offset); > + } > + } > + > + /* Replace the rhs with the new expression. */ > + def_rhs = unshare_expr (def_rhs); > + gimple_assign_set_rhs_from_tree (use_stmt_gsi, def_rhs); > + gimple *use_stmt = gsi_stmt (*use_stmt_gsi); > + update_stmt (use_stmt); > +} > + > /* We've just substituted an ADDR_EXPR into stmt. Update all the > relevant data structures to match. */ > > @@ -696,8 +737,8 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > if (single_use_p > && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))) > { > - gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs)); > - gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs)); > + rewrite_assign_addr (use_stmt_gsi, def_rhs); > + gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > return true; > } > > @@ -741,14 +782,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p)) > return true; > > - if (useless_type_conversion_p (TREE_TYPE (lhs), > - TREE_TYPE (new_def_rhs))) > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs), > - new_def_rhs); > - else if (is_gimple_min_invariant (new_def_rhs)) > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs); > - else > - return false; > + rewrite_assign_addr (use_stmt_gsi, new_def_rhs); > gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > update_stmt (use_stmt); > return true; > @@ -951,9 +985,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > unshare_expr (def_rhs), > fold_convert (ptr_type_node, > rhs2))); > - gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs); > - use_stmt = gsi_stmt (*use_stmt_gsi); > - update_stmt (use_stmt); > + rewrite_assign_addr (use_stmt_gsi, new_rhs); > tidy_after_forward_propagate_addr (use_stmt); > return true; > } >
On Wed, Oct 27, 2021 at 3:42 AM Richard Biener via Gcc-patches <gcc-patches@gcc.gnu.org> wrote: > > On Wed, Oct 27, 2021 at 12:00 PM apinski--- via Gcc-patches > <gcc-patches@gcc.gnu.org> wrote: > > > > From: Andrew Pinski <apinski@marvell.com> > > > > The problem here is tree-ssa-forwprop.c likes to produce > > &MEM <const char *> [(void *)_4 + 152B] which is the same as > > _4 p+ 152 which the rest of GCC likes better. > > This implements this transformation back to pointer plus to > > improve better code generation later on. > > Why do you think so? Can you pin-point the transform that now > fixes the new testcase? So we had originally: language_names_p_9 = &MEM <const char *> [(void *)_4 + 24B]; ... _2 = _4 + 40; if (_2 != language_names_p_9) Forwprop is able to figure out that the above if statement is now always false as we have (_4 +p 40) != (_4 +p 24) which gets simplified via a match-and-simplify pattern (). Does that answer your question? I will look into the other comments in a new patch. Thanks, Andrew > > Comments below > > > OK? Bootstrapped and tested on aarch64-linux-gnu. > > > > Changes from v1: > > * v2: Add comments. > > > > gcc/ChangeLog: > > > > PR tree-optimization/102216 > > * tree-ssa-forwprop.c (rewrite_assign_addr): New function. > > (forward_propagate_addr_expr_1): Use rewrite_assign_addr > > when rewriting into the addr_expr into an assignment. > > > > gcc/testsuite/ChangeLog: > > > > PR tree-optimization/102216 > > * g++.dg/tree-ssa/pr102216.C: New test. > > --- > > gcc/testsuite/g++.dg/tree-ssa/pr102216.C | 22 +++++++++ > > gcc/tree-ssa-forwprop.c | 58 ++++++++++++++++++------ > > 2 files changed, 67 insertions(+), 13 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > > > diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr102216.C b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > new file mode 100644 > > index 00000000000..b903e4eb57d > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > @@ -0,0 +1,22 @@ > > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > > +void link_error (); > > +void g () > > +{ > > + const char **language_names; > > + > > + language_names = new const char *[6]; > > + > > + const char **language_names_p = language_names; > > + > > + language_names_p++; > > + language_names_p++; > > + language_names_p++; > > + > > + if ( (language_names_p) - (language_names+3) != 0) > > + link_error(); > > + delete[] language_names; > > +} > > +/* We should have removed the link_error on the gimple level as GCC should > > + be able to tell that language_names_p is the same as language_names+3. */ > > +/* { dg-final { scan-tree-dump-times "link_error" 0 "optimized" } } */ > > + > > diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c > > index a830bab78ba..e4331c60525 100644 > > --- a/gcc/tree-ssa-forwprop.c > > +++ b/gcc/tree-ssa-forwprop.c > > @@ -637,6 +637,47 @@ forward_propagate_into_cond (gimple_stmt_iterator *gsi_p) > > return 0; > > } > > > > +/* Rewrite the DEF_RHS as needed into the (plain) use statement. */ > > + > > +static void > > +rewrite_assign_addr (gimple_stmt_iterator *use_stmt_gsi, tree def_rhs) > > +{ > > + tree def_rhs_base; > > + poly_int64 def_rhs_offset; > > + > > + /* Get the base and offset. */ > > + if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), > > + &def_rhs_offset))) > > So this will cause us to rewrite &MEM[p_1].a.b.c; to a pointer-plus, > right? Don't > we want to preserve that for object-size stuff? So maybe directly pattern > match ADDR_EXPR <MEM_REF <SSA_NAME, ..>> only? > > > + { > > + tree new_ptr; > > + poly_offset_int off = 0; > > + > > + /* If the base was a MEM, then add the offset to the other > > + offset and adjust the base. */ > > + if (TREE_CODE (def_rhs_base) == MEM_REF) > > + { > > + off += mem_ref_offset (def_rhs_base); > > + new_ptr = TREE_OPERAND (def_rhs_base, 0); > > + } > > + else > > + new_ptr = build_fold_addr_expr (def_rhs_base); > > + > > + /* If we have the new base is not an address express, then use a p+ expression > > + as the new expression instead of &MEM[x, offset]. */ > > + if (TREE_CODE (new_ptr) != ADDR_EXPR) > > + { > > + tree offset = wide_int_to_tree (sizetype, off); > > + def_rhs = build2 (POINTER_PLUS_EXPR, TREE_TYPE (def_rhs), new_ptr, offset); > > Ick. You should be able to use gimple_assign_set_rhs_with_ops. > > > + } > > + } > > + > > + /* Replace the rhs with the new expression. */ > > + def_rhs = unshare_expr (def_rhs); > > and definitely no need to unshare anything here? > > > + gimple_assign_set_rhs_from_tree (use_stmt_gsi, def_rhs); > > + gimple *use_stmt = gsi_stmt (*use_stmt_gsi); > > + update_stmt (use_stmt); > > +} > > + > > /* We've just substituted an ADDR_EXPR into stmt. Update all the > > relevant data structures to match. */ > > > > @@ -696,8 +737,8 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > > if (single_use_p > > && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))) > > { > > - gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs)); > > - gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs)); > > + rewrite_assign_addr (use_stmt_gsi, def_rhs); > > + gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > > return true; > > } > > > > @@ -741,14 +782,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > > if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p)) > > return true; > > > > - if (useless_type_conversion_p (TREE_TYPE (lhs), > > - TREE_TYPE (new_def_rhs))) > > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs), > > - new_def_rhs); > > - else if (is_gimple_min_invariant (new_def_rhs)) > > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs); > > - else > > - return false; > > + rewrite_assign_addr (use_stmt_gsi, new_def_rhs); > > gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > > update_stmt (use_stmt); > > return true; > > @@ -951,9 +985,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > > unshare_expr (def_rhs), > > fold_convert (ptr_type_node, > > rhs2))); > > - gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs); > > - use_stmt = gsi_stmt (*use_stmt_gsi); > > - update_stmt (use_stmt); > > + rewrite_assign_addr (use_stmt_gsi, new_rhs); > > so you only do this after addr_expr forwarding but not on stmts in > general? You could > do it that way in the 2nd loop over the BB. > > > tidy_after_forward_propagate_addr (use_stmt); > > return true; > > } > > -- > > 2.17.1 > >
On Mon, Nov 22, 2021 at 9:40 AM Andrew Pinski <pinskia@gmail.com> wrote: > > On Wed, Oct 27, 2021 at 3:42 AM Richard Biener via Gcc-patches > <gcc-patches@gcc.gnu.org> wrote: > > > > On Wed, Oct 27, 2021 at 12:00 PM apinski--- via Gcc-patches > > <gcc-patches@gcc.gnu.org> wrote: > > > > > > From: Andrew Pinski <apinski@marvell.com> > > > > > > The problem here is tree-ssa-forwprop.c likes to produce > > > &MEM <const char *> [(void *)_4 + 152B] which is the same as > > > _4 p+ 152 which the rest of GCC likes better. > > > This implements this transformation back to pointer plus to > > > improve better code generation later on. > > > > Why do you think so? Can you pin-point the transform that now > > fixes the new testcase? > > So we had originally: > language_names_p_9 = &MEM <const char *> [(void *)_4 + 24B]; > ... > _2 = _4 + 40; Of course if that would have been _2 = &MEM [_4 + 40B]; the issue would be fixed as well. That said, I agree that _4 + 40 is better but I think we should canonicalize all &MEM[SSA + CST] this way. There is a canonicalization phase in fold_stmt_1: /* First do required canonicalization of [TARGET_]MEM_REF addresses after propagation. ??? This shouldn't be done in generic folding but in the propagation helpers which also know whether an address was propagated. Also canonicalize operand order. */ switch (gimple_code (stmt)) { case GIMPLE_ASSIGN: if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS) { tree *rhs = gimple_assign_rhs1_ptr (stmt); if ((REFERENCE_CLASS_P (*rhs) || TREE_CODE (*rhs) == ADDR_EXPR) && maybe_canonicalize_mem_ref_addr (rhs)) changed = true; where this could be done (apart from adding a match.pd pattern for this). > if (_2 != language_names_p_9) > > Forwprop is able to figure out that the above if statement is now > always false as we have (_4 +p 40) != (_4 +p 24) which gets simplified > via a match-and-simplify pattern (). > Does that answer your question? > > I will look into the other comments in a new patch. > > Thanks, > Andrew > > > > > Comments below > > > > > OK? Bootstrapped and tested on aarch64-linux-gnu. > > > > > > Changes from v1: > > > * v2: Add comments. > > > > > > gcc/ChangeLog: > > > > > > PR tree-optimization/102216 > > > * tree-ssa-forwprop.c (rewrite_assign_addr): New function. > > > (forward_propagate_addr_expr_1): Use rewrite_assign_addr > > > when rewriting into the addr_expr into an assignment. > > > > > > gcc/testsuite/ChangeLog: > > > > > > PR tree-optimization/102216 > > > * g++.dg/tree-ssa/pr102216.C: New test. > > > --- > > > gcc/testsuite/g++.dg/tree-ssa/pr102216.C | 22 +++++++++ > > > gcc/tree-ssa-forwprop.c | 58 ++++++++++++++++++------ > > > 2 files changed, 67 insertions(+), 13 deletions(-) > > > create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > > > > > diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr102216.C b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > > new file mode 100644 > > > index 00000000000..b903e4eb57d > > > --- /dev/null > > > +++ b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > > @@ -0,0 +1,22 @@ > > > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > > > +void link_error (); > > > +void g () > > > +{ > > > + const char **language_names; > > > + > > > + language_names = new const char *[6]; > > > + > > > + const char **language_names_p = language_names; > > > + > > > + language_names_p++; > > > + language_names_p++; > > > + language_names_p++; > > > + > > > + if ( (language_names_p) - (language_names+3) != 0) > > > + link_error(); > > > + delete[] language_names; > > > +} > > > +/* We should have removed the link_error on the gimple level as GCC should > > > + be able to tell that language_names_p is the same as language_names+3. */ > > > +/* { dg-final { scan-tree-dump-times "link_error" 0 "optimized" } } */ > > > + > > > diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c > > > index a830bab78ba..e4331c60525 100644 > > > --- a/gcc/tree-ssa-forwprop.c > > > +++ b/gcc/tree-ssa-forwprop.c > > > @@ -637,6 +637,47 @@ forward_propagate_into_cond (gimple_stmt_iterator *gsi_p) > > > return 0; > > > } > > > > > > +/* Rewrite the DEF_RHS as needed into the (plain) use statement. */ > > > + > > > +static void > > > +rewrite_assign_addr (gimple_stmt_iterator *use_stmt_gsi, tree def_rhs) > > > +{ > > > + tree def_rhs_base; > > > + poly_int64 def_rhs_offset; > > > + > > > + /* Get the base and offset. */ > > > + if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), > > > + &def_rhs_offset))) > > > > So this will cause us to rewrite &MEM[p_1].a.b.c; to a pointer-plus, > > right? Don't > > we want to preserve that for object-size stuff? So maybe directly pattern > > match ADDR_EXPR <MEM_REF <SSA_NAME, ..>> only? > > > > > + { > > > + tree new_ptr; > > > + poly_offset_int off = 0; > > > + > > > + /* If the base was a MEM, then add the offset to the other > > > + offset and adjust the base. */ > > > + if (TREE_CODE (def_rhs_base) == MEM_REF) > > > + { > > > + off += mem_ref_offset (def_rhs_base); > > > + new_ptr = TREE_OPERAND (def_rhs_base, 0); > > > + } > > > + else > > > + new_ptr = build_fold_addr_expr (def_rhs_base); > > > + > > > + /* If we have the new base is not an address express, then use a p+ expression > > > + as the new expression instead of &MEM[x, offset]. */ > > > + if (TREE_CODE (new_ptr) != ADDR_EXPR) > > > + { > > > + tree offset = wide_int_to_tree (sizetype, off); > > > + def_rhs = build2 (POINTER_PLUS_EXPR, TREE_TYPE (def_rhs), new_ptr, offset); > > > > Ick. You should be able to use gimple_assign_set_rhs_with_ops. > > > > > + } > > > + } > > > + > > > + /* Replace the rhs with the new expression. */ > > > + def_rhs = unshare_expr (def_rhs); > > > > and definitely no need to unshare anything here? > > > > > + gimple_assign_set_rhs_from_tree (use_stmt_gsi, def_rhs); > > > + gimple *use_stmt = gsi_stmt (*use_stmt_gsi); > > > + update_stmt (use_stmt); > > > +} > > > + > > > /* We've just substituted an ADDR_EXPR into stmt. Update all the > > > relevant data structures to match. */ > > > > > > @@ -696,8 +737,8 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > > > if (single_use_p > > > && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))) > > > { > > > - gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs)); > > > - gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs)); > > > + rewrite_assign_addr (use_stmt_gsi, def_rhs); > > > + gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > > > return true; > > > } > > > > > > @@ -741,14 +782,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > > > if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p)) > > > return true; > > > > > > - if (useless_type_conversion_p (TREE_TYPE (lhs), > > > - TREE_TYPE (new_def_rhs))) > > > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs), > > > - new_def_rhs); > > > - else if (is_gimple_min_invariant (new_def_rhs)) > > > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs); > > > - else > > > - return false; > > > + rewrite_assign_addr (use_stmt_gsi, new_def_rhs); > > > gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > > > update_stmt (use_stmt); > > > return true; > > > @@ -951,9 +985,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > > > unshare_expr (def_rhs), > > > fold_convert (ptr_type_node, > > > rhs2))); > > > - gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs); > > > - use_stmt = gsi_stmt (*use_stmt_gsi); > > > - update_stmt (use_stmt); > > > + rewrite_assign_addr (use_stmt_gsi, new_rhs); > > > > so you only do this after addr_expr forwarding but not on stmts in > > general? You could > > do it that way in the 2nd loop over the BB. > > > > > tidy_after_forward_propagate_addr (use_stmt); > > > return true; > > > } > > > -- > > > 2.17.1 > > >
On Mon, Nov 22, 2021 at 3:40 AM Richard Biener <richard.guenther@gmail.com> wrote: > > On Mon, Nov 22, 2021 at 9:40 AM Andrew Pinski <pinskia@gmail.com> wrote: > > > > On Wed, Oct 27, 2021 at 3:42 AM Richard Biener via Gcc-patches > > <gcc-patches@gcc.gnu.org> wrote: > > > > > > On Wed, Oct 27, 2021 at 12:00 PM apinski--- via Gcc-patches > > > <gcc-patches@gcc.gnu.org> wrote: > > > > > > > > From: Andrew Pinski <apinski@marvell.com> > > > > > > > > The problem here is tree-ssa-forwprop.c likes to produce > > > > &MEM <const char *> [(void *)_4 + 152B] which is the same as > > > > _4 p+ 152 which the rest of GCC likes better. > > > > This implements this transformation back to pointer plus to > > > > improve better code generation later on. > > > > > > Why do you think so? Can you pin-point the transform that now > > > fixes the new testcase? > > > > So we had originally: > > language_names_p_9 = &MEM <const char *> [(void *)_4 + 24B]; > > ... > > _2 = _4 + 40; > > Of course if that would have been > > _2 = &MEM [_4 + 40B]; > > the issue would be fixed as well. That said, I agree that _4 + 40 > is better but I think we should canonicalize all &MEM[SSA + CST] > this way. There is a canonicalization phase in fold_stmt_1: > > /* First do required canonicalization of [TARGET_]MEM_REF addresses > after propagation. > ??? This shouldn't be done in generic folding but in the > propagation helpers which also know whether an address was > propagated. > Also canonicalize operand order. */ > switch (gimple_code (stmt)) > { > case GIMPLE_ASSIGN: > if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS) > { > tree *rhs = gimple_assign_rhs1_ptr (stmt); > if ((REFERENCE_CLASS_P (*rhs) > || TREE_CODE (*rhs) == ADDR_EXPR) > && maybe_canonicalize_mem_ref_addr (rhs)) > changed = true; > > where this could be done (apart from adding a match.pd pattern for this). Yes that is a good idea, I now have a patch which I am testing to add this canonicalization. It is actually simpler than the previous patch too. Thanks, Andrew > > > if (_2 != language_names_p_9) > > > > Forwprop is able to figure out that the above if statement is now > > always false as we have (_4 +p 40) != (_4 +p 24) which gets simplified > > via a match-and-simplify pattern (). > > Does that answer your question? > > > > I will look into the other comments in a new patch. > > > > Thanks, > > Andrew > > > > > > > > Comments below > > > > > > > OK? Bootstrapped and tested on aarch64-linux-gnu. > > > > > > > > Changes from v1: > > > > * v2: Add comments. > > > > > > > > gcc/ChangeLog: > > > > > > > > PR tree-optimization/102216 > > > > * tree-ssa-forwprop.c (rewrite_assign_addr): New function. > > > > (forward_propagate_addr_expr_1): Use rewrite_assign_addr > > > > when rewriting into the addr_expr into an assignment. > > > > > > > > gcc/testsuite/ChangeLog: > > > > > > > > PR tree-optimization/102216 > > > > * g++.dg/tree-ssa/pr102216.C: New test. > > > > --- > > > > gcc/testsuite/g++.dg/tree-ssa/pr102216.C | 22 +++++++++ > > > > gcc/tree-ssa-forwprop.c | 58 ++++++++++++++++++------ > > > > 2 files changed, 67 insertions(+), 13 deletions(-) > > > > create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > > > > > > > diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr102216.C b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > > > new file mode 100644 > > > > index 00000000000..b903e4eb57d > > > > --- /dev/null > > > > +++ b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C > > > > @@ -0,0 +1,22 @@ > > > > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > > > > +void link_error (); > > > > +void g () > > > > +{ > > > > + const char **language_names; > > > > + > > > > + language_names = new const char *[6]; > > > > + > > > > + const char **language_names_p = language_names; > > > > + > > > > + language_names_p++; > > > > + language_names_p++; > > > > + language_names_p++; > > > > + > > > > + if ( (language_names_p) - (language_names+3) != 0) > > > > + link_error(); > > > > + delete[] language_names; > > > > +} > > > > +/* We should have removed the link_error on the gimple level as GCC should > > > > + be able to tell that language_names_p is the same as language_names+3. */ > > > > +/* { dg-final { scan-tree-dump-times "link_error" 0 "optimized" } } */ > > > > + > > > > diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c > > > > index a830bab78ba..e4331c60525 100644 > > > > --- a/gcc/tree-ssa-forwprop.c > > > > +++ b/gcc/tree-ssa-forwprop.c > > > > @@ -637,6 +637,47 @@ forward_propagate_into_cond (gimple_stmt_iterator *gsi_p) > > > > return 0; > > > > } > > > > > > > > +/* Rewrite the DEF_RHS as needed into the (plain) use statement. */ > > > > + > > > > +static void > > > > +rewrite_assign_addr (gimple_stmt_iterator *use_stmt_gsi, tree def_rhs) > > > > +{ > > > > + tree def_rhs_base; > > > > + poly_int64 def_rhs_offset; > > > > + > > > > + /* Get the base and offset. */ > > > > + if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), > > > > + &def_rhs_offset))) > > > > > > So this will cause us to rewrite &MEM[p_1].a.b.c; to a pointer-plus, > > > right? Don't > > > we want to preserve that for object-size stuff? So maybe directly pattern > > > match ADDR_EXPR <MEM_REF <SSA_NAME, ..>> only? > > > > > > > + { > > > > + tree new_ptr; > > > > + poly_offset_int off = 0; > > > > + > > > > + /* If the base was a MEM, then add the offset to the other > > > > + offset and adjust the base. */ > > > > + if (TREE_CODE (def_rhs_base) == MEM_REF) > > > > + { > > > > + off += mem_ref_offset (def_rhs_base); > > > > + new_ptr = TREE_OPERAND (def_rhs_base, 0); > > > > + } > > > > + else > > > > + new_ptr = build_fold_addr_expr (def_rhs_base); > > > > + > > > > + /* If we have the new base is not an address express, then use a p+ expression > > > > + as the new expression instead of &MEM[x, offset]. */ > > > > + if (TREE_CODE (new_ptr) != ADDR_EXPR) > > > > + { > > > > + tree offset = wide_int_to_tree (sizetype, off); > > > > + def_rhs = build2 (POINTER_PLUS_EXPR, TREE_TYPE (def_rhs), new_ptr, offset); > > > > > > Ick. You should be able to use gimple_assign_set_rhs_with_ops. > > > > > > > + } > > > > + } > > > > + > > > > + /* Replace the rhs with the new expression. */ > > > > + def_rhs = unshare_expr (def_rhs); > > > > > > and definitely no need to unshare anything here? > > > > > > > + gimple_assign_set_rhs_from_tree (use_stmt_gsi, def_rhs); > > > > + gimple *use_stmt = gsi_stmt (*use_stmt_gsi); > > > > + update_stmt (use_stmt); > > > > +} > > > > + > > > > /* We've just substituted an ADDR_EXPR into stmt. Update all the > > > > relevant data structures to match. */ > > > > > > > > @@ -696,8 +737,8 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > > > > if (single_use_p > > > > && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))) > > > > { > > > > - gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs)); > > > > - gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs)); > > > > + rewrite_assign_addr (use_stmt_gsi, def_rhs); > > > > + gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > > > > return true; > > > > } > > > > > > > > @@ -741,14 +782,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > > > > if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p)) > > > > return true; > > > > > > > > - if (useless_type_conversion_p (TREE_TYPE (lhs), > > > > - TREE_TYPE (new_def_rhs))) > > > > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs), > > > > - new_def_rhs); > > > > - else if (is_gimple_min_invariant (new_def_rhs)) > > > > - gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs); > > > > - else > > > > - return false; > > > > + rewrite_assign_addr (use_stmt_gsi, new_def_rhs); > > > > gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); > > > > update_stmt (use_stmt); > > > > return true; > > > > @@ -951,9 +985,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, > > > > unshare_expr (def_rhs), > > > > fold_convert (ptr_type_node, > > > > rhs2))); > > > > - gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs); > > > > - use_stmt = gsi_stmt (*use_stmt_gsi); > > > > - update_stmt (use_stmt); > > > > + rewrite_assign_addr (use_stmt_gsi, new_rhs); > > > > > > so you only do this after addr_expr forwarding but not on stmts in > > > general? You could > > > do it that way in the 2nd loop over the BB. > > > > > > > tidy_after_forward_propagate_addr (use_stmt); > > > > return true; > > > > } > > > > -- > > > > 2.17.1 > > > >
diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr102216.C b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C new file mode 100644 index 00000000000..b903e4eb57d --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/pr102216.C @@ -0,0 +1,22 @@ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +void link_error (); +void g () +{ + const char **language_names; + + language_names = new const char *[6]; + + const char **language_names_p = language_names; + + language_names_p++; + language_names_p++; + language_names_p++; + + if ( (language_names_p) - (language_names+3) != 0) + link_error(); + delete[] language_names; +} +/* We should have removed the link_error on the gimple level as GCC should + be able to tell that language_names_p is the same as language_names+3. */ +/* { dg-final { scan-tree-dump-times "link_error" 0 "optimized" } } */ + diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c index a830bab78ba..e4331c60525 100644 --- a/gcc/tree-ssa-forwprop.c +++ b/gcc/tree-ssa-forwprop.c @@ -637,6 +637,47 @@ forward_propagate_into_cond (gimple_stmt_iterator *gsi_p) return 0; } +/* Rewrite the DEF_RHS as needed into the (plain) use statement. */ + +static void +rewrite_assign_addr (gimple_stmt_iterator *use_stmt_gsi, tree def_rhs) +{ + tree def_rhs_base; + poly_int64 def_rhs_offset; + + /* Get the base and offset. */ + if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), + &def_rhs_offset))) + { + tree new_ptr; + poly_offset_int off = 0; + + /* If the base was a MEM, then add the offset to the other + offset and adjust the base. */ + if (TREE_CODE (def_rhs_base) == MEM_REF) + { + off += mem_ref_offset (def_rhs_base); + new_ptr = TREE_OPERAND (def_rhs_base, 0); + } + else + new_ptr = build_fold_addr_expr (def_rhs_base); + + /* If we have the new base is not an address express, then use a p+ expression + as the new expression instead of &MEM[x, offset]. */ + if (TREE_CODE (new_ptr) != ADDR_EXPR) + { + tree offset = wide_int_to_tree (sizetype, off); + def_rhs = build2 (POINTER_PLUS_EXPR, TREE_TYPE (def_rhs), new_ptr, offset); + } + } + + /* Replace the rhs with the new expression. */ + def_rhs = unshare_expr (def_rhs); + gimple_assign_set_rhs_from_tree (use_stmt_gsi, def_rhs); + gimple *use_stmt = gsi_stmt (*use_stmt_gsi); + update_stmt (use_stmt); +} + /* We've just substituted an ADDR_EXPR into stmt. Update all the relevant data structures to match. */ @@ -696,8 +737,8 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, if (single_use_p && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))) { - gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs)); - gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs)); + rewrite_assign_addr (use_stmt_gsi, def_rhs); + gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); return true; } @@ -741,14 +782,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p)) return true; - if (useless_type_conversion_p (TREE_TYPE (lhs), - TREE_TYPE (new_def_rhs))) - gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs), - new_def_rhs); - else if (is_gimple_min_invariant (new_def_rhs)) - gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs); - else - return false; + rewrite_assign_addr (use_stmt_gsi, new_def_rhs); gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); update_stmt (use_stmt); return true; @@ -951,9 +985,7 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs, unshare_expr (def_rhs), fold_convert (ptr_type_node, rhs2))); - gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs); - use_stmt = gsi_stmt (*use_stmt_gsi); - update_stmt (use_stmt); + rewrite_assign_addr (use_stmt_gsi, new_rhs); tidy_after_forward_propagate_addr (use_stmt); return true; }