public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-6425] c++: constexpr base-to-derived conversion with offset 0 [PR103879] Date: Mon, 10 Jan 2022 19:59:05 +0000 (GMT) [thread overview] Message-ID: <20220110195905.E58A53892440@sourceware.org> (raw) https://gcc.gnu.org/g:ab36b554bd90e8db279d13b133369118814f13fb commit r12-6425-gab36b554bd90e8db279d13b133369118814f13fb Author: Patrick Palka <ppalka@redhat.com> Date: Mon Jan 10 14:57:54 2022 -0500 c++: constexpr base-to-derived conversion with offset 0 [PR103879] r12-136 made us canonicalize an object/offset pair with negative offset into one with a nonnegative offset, by iteratively absorbing the innermost component into the offset and stopping as soon as the offset becomes nonnegative. This patch strengthens this transformation by making it keep on absorbing even if the offset is already 0 as long as the innermost component is at position 0 (and thus absorbing doesn't change the offset). This lets us accept the two constexpr testcases below, which we'd previously reject essentially because cxx_fold_indirect_ref would be unable to resolve *(B*)&b.D123 (where D123 is the base A subobject at position 0) to just b. PR c++/103879 gcc/cp/ChangeLog: * constexpr.c (cxx_fold_indirect_ref): Split out object/offset canonicalization step into a local lambda. Strengthen it to absorb more components at position 0. Use it before both calls to cxx_fold_indirect_ref_1. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-base2.C: New test. * g++.dg/cpp1y/constexpr-base2a.C: New test. Diff: --- gcc/cp/constexpr.c | 38 ++++++++++++++++++--------- gcc/testsuite/g++.dg/cpp1y/constexpr-base2.C | 15 +++++++++++ gcc/testsuite/g++.dg/cpp1y/constexpr-base2a.C | 17 ++++++++++++ 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index af6a4a729e9..d0da4a70448 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5215,6 +5215,25 @@ cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, if (!INDIRECT_TYPE_P (subtype)) return NULL_TREE; + /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing + the innermost component into the offset until it would make the + offset positive, so that cxx_fold_indirect_ref_1 can identify + more folding opportunities. */ + auto canonicalize_obj_off = [] (tree& obj, tree& off) { + while (TREE_CODE (obj) == COMPONENT_REF + && (tree_int_cst_sign_bit (off) || integer_zerop (off))) + { + tree field = TREE_OPERAND (obj, 1); + tree pos = byte_position (field); + if (integer_zerop (off) && integer_nonzerop (pos)) + /* If the offset is already 0, keep going as long as the + component is at position 0. */ + break; + off = int_const_binop (PLUS_EXPR, off, pos); + obj = TREE_OPERAND (obj, 0); + } + }; + if (TREE_CODE (sub) == ADDR_EXPR) { tree op = TREE_OPERAND (sub, 0); @@ -5233,7 +5252,12 @@ cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, return op; } else - return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); + { + tree off = integer_zero_node; + canonicalize_obj_off (op, off); + gcc_assert (integer_zerop (off)); + return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); + } } else if (TREE_CODE (sub) == POINTER_PLUS_EXPR && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) @@ -5245,17 +5269,7 @@ cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, if (TREE_CODE (op00) == ADDR_EXPR) { tree obj = TREE_OPERAND (op00, 0); - while (TREE_CODE (obj) == COMPONENT_REF - && tree_int_cst_sign_bit (off)) - { - /* Canonicalize this object/offset pair by iteratively absorbing - the innermost component into the offset until the offset is - nonnegative, so that cxx_fold_indirect_ref_1 can identify - more folding opportunities. */ - tree field = TREE_OPERAND (obj, 1); - off = int_const_binop (PLUS_EXPR, off, byte_position (field)); - obj = TREE_OPERAND (obj, 0); - } + canonicalize_obj_off (obj, off); return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, tree_to_uhwi (off), empty_base); } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-base2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-base2.C new file mode 100644 index 00000000000..a267c14bc80 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-base2.C @@ -0,0 +1,15 @@ +// PR c++/103879 +// { dg-do compile { target c++14 } } + +struct A { int n = 42; }; +struct B : A { }; +struct C { B b; }; + +constexpr int f() { + C c; + A& a = static_cast<A&>(c.b); + B& b = static_cast<B&>(a); + return b.n; +} + +static_assert(f() == 42, ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-base2a.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-base2a.C new file mode 100644 index 00000000000..9eb72b96de9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-base2a.C @@ -0,0 +1,17 @@ +// PR c++/103879 +// { dg-do compile { target c++14 } } + +struct A { int n = 42; }; +struct Y { int m = 0; }; +struct X : Y, A { }; +struct B : X { }; +struct C { B b; }; + +constexpr int f() { + C c; + A& a = static_cast<A&>(c.b); + B& b = static_cast<B&>(a); + return b.n; +} + +static_assert(f() == 42, "");
reply other threads:[~2022-01-10 19:59 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220110195905.E58A53892440@sourceware.org \ --to=ppalka@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).