From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id D39A03850427; Tue, 11 Oct 2022 13:04:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D39A03850427 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665493468; bh=uk/pdaUja2M8Okje+eTVFcm/hh3sxsmWwgwav9aTxi4=; h=From:To:Subject:Date:From; b=cVn5jAN1ByPrRu0GTk/uHPjFjmNU1FapCCiSZK4Fnzthfx7P3NF/N3ldpXPGn2H4R yEzKiAydCc0r5qdQcVVcYhhLQeMONYx5UF4MjDs/6xay1VptZ/qkteZfztyh8Nb4g1 g3mN2tF+oZey3NDUt/RZuvJZplDJ8mep90Z4tHmU= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10304] tree-optimization/106131 - wrong code with FRE rewriting X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: b7878f05553bcf569a987dff8a7ef8b045b8d26c X-Git-Newrev: befa9c8b072ef8b81d4a5b46d83f81cd58318c2b Message-Id: <20221011130428.D39A03850427@sourceware.org> Date: Tue, 11 Oct 2022 13:04:28 +0000 (GMT) List-Id: https://gcc.gnu.org/g:befa9c8b072ef8b81d4a5b46d83f81cd58318c2b commit r11-10304-gbefa9c8b072ef8b81d4a5b46d83f81cd58318c2b Author: Richard Biener Date: Thu Jun 30 10:33:40 2022 +0200 tree-optimization/106131 - wrong code with FRE rewriting 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. 2022-06-30 Richard Biener PR tree-optimization/106131 * tree-ssa-sccvn.c (vn_reference_lookup_3): Force alias-set zero when offsetting the read looking through an aggregate copy. * g++.dg/torture/pr106131.C: New testcase. (cherry picked from commit 9701432ff79926a5dd3303be3417e0bd0c24140b) Diff: --- gcc/testsuite/g++.dg/torture/pr106131.C | 34 +++++++++++++++++++++++++++++++++ gcc/tree-ssa-sccvn.c | 16 ++++++++++++---- 2 files changed, 46 insertions(+), 4 deletions(-) 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.c b/gcc/tree-ssa-sccvn.c index 5d773dd3239..b4c9fac198b 100644 --- a/gcc/tree-ssa-sccvn.c +++ b/gcc/tree-ssa-sccvn.c @@ -3197,12 +3197,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--; @@ -3229,6 +3229,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) @@ -3241,6 +3242,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 @@ -3293,8 +3298,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. */