From patchwork Fri Jul 1 06:53:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 55620 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 33245385802A for ; Fri, 1 Jul 2022 06:54:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 33245385802A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1656658451; bh=09YNreIxqcf20CrJGC/gR++qMpHjYe3WGZ5Agv6OlLo=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=TdkeXU2ZkUUwwtyhZck1MCFfOXZRLh9CTlCoe6l8zb2OUYFU61C8SO+OwyvcAFCbp yDA1dOU5MU4mZucm+fuKD5qqEVw94lngWVablP9kO4QdDj+sz+DXRi4+2zcKPVCqtb TlfOtlh6jniruVvRPbKO4XmJU2w6Biq29KeGuR/A= 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 C32143858437 for ; Fri, 1 Jul 2022 06:53:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C32143858437 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 5D4941FB2F for ; Fri, 1 Jul 2022 06:53:40 +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 56BED2C141 for ; Fri, 1 Jul 2022 06:53:40 +0000 (UTC) Date: Fri, 1 Jul 2022 06:53:40 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/106131 - wrong code with FRE rewriting Message-ID: User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 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.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.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" The following makes sure to not use the original TBAA type for looking up a value across an aggregate copy when we had to offset the read. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed to trunk. 2022-06-30 Richard Biener PR tree-optimization/106131 * tree-ssa-sccvn.cc (vn_reference_lookup_3): Force alias-set zero when offsetting the read looking through an aggregate copy. * g++.dg/torture/pr106131.C: New testcase. --- gcc/testsuite/g++.dg/torture/pr106131.C | 34 +++++++++++++++++++++++++ gcc/tree-ssa-sccvn.cc | 16 +++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/torture/pr106131.C diff --git a/gcc/testsuite/g++.dg/torture/pr106131.C b/gcc/testsuite/g++.dg/torture/pr106131.C new file mode 100644 index 00000000000..e110f4a8fe6 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr106131.C @@ -0,0 +1,34 @@ +// { dg-do run { target c++11 } } + +struct Pair { + int a, b; + Pair(const Pair &) = default; + Pair(int _a, int _b) : a(_a), b(_b) {} + Pair &operator=(const Pair &z) { + a = z.a; + b = z.b; + return *this; + } +}; + +const int &max(const int &a, const int &b) +{ + return a < b ? b : a; +} + +int foo(Pair x, Pair y) +{ + return max(x.b, y.b); +} + +int main() +{ + auto f = new Pair[3] {{0, -11}, {0, -8}, {0, 2}}; + for (int i = 0; i < 1; i++) { + f[i] = f[0]; + if(i == 0) + f[i] = f[2]; + if (foo(f[i], f[1]) != 2) + __builtin_abort(); + } +} diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index 9deedeac378..76d92895a3a 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -3243,12 +3243,12 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_, poly_int64 extra_off = 0; if (j == 0 && i >= 0 && lhs_ops[0].opcode == MEM_REF - && maybe_ne (lhs_ops[0].off, -1)) + && known_ne (lhs_ops[0].off, -1)) { if (known_eq (lhs_ops[0].off, vr->operands[i].off)) i--, j--; else if (vr->operands[i].opcode == MEM_REF - && maybe_ne (vr->operands[i].off, -1)) + && known_ne (vr->operands[i].off, -1)) { extra_off = vr->operands[i].off - lhs_ops[0].off; i--, j--; @@ -3275,6 +3275,7 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_, copy_reference_ops_from_ref (rhs1, &rhs); /* Apply an extra offset to the inner MEM_REF of the RHS. */ + bool force_no_tbaa = false; if (maybe_ne (extra_off, 0)) { if (rhs.length () < 2) @@ -3287,6 +3288,10 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_, rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0, build_int_cst (TREE_TYPE (rhs[ix].op0), extra_off)); + /* When we have offsetted the RHS, reading only parts of it, + we can no longer use the original TBAA type, force alias-set + zero. */ + force_no_tbaa = true; } /* Save the operands since we need to use the original ones for @@ -3339,8 +3344,11 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_, /* Adjust *ref from the new operands. */ ao_ref rhs1_ref; ao_ref_init (&rhs1_ref, rhs1); - if (!ao_ref_init_from_vn_reference (&r, ao_ref_alias_set (&rhs1_ref), - ao_ref_base_alias_set (&rhs1_ref), + if (!ao_ref_init_from_vn_reference (&r, + force_no_tbaa ? 0 + : ao_ref_alias_set (&rhs1_ref), + force_no_tbaa ? 0 + : ao_ref_base_alias_set (&rhs1_ref), vr->type, vr->operands)) return (void *)-1; /* This can happen with bitfields. */