public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/89074] valid pointer equality constexpr comparison rejected
Date: Sat, 08 Jan 2022 08:54:30 +0000	[thread overview]
Message-ID: <bug-89074-4-CMaLaZozvN@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-89074-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89074

--- Comment #15 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:51d464b608b38b9e2007948d10b1e0f1dcec142c

commit r12-6382-g51d464b608b38b9e2007948d10b1e0f1dcec142c
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Sat Jan 8 09:53:00 2022 +0100

    c++, match.pd: Evaluate in constant evaluation comparisons like &var1 + 12
== &var2 + 24 [PR89074]

    The match.pd address_comparison simplification can only handle
    ADDR_EXPR comparisons possibly converted to some other type (I wonder
    if we shouldn't restrict it in address_compare to casts to pointer
    types or pointer-sized integer types, I think we shouldn't optimize
    (short) (&var) == (short) (&var2) because we really don't know whether
    it will be true or false).  On GIMPLE, most of pointer to pointer
    casts are useless and optimized away and further we have in
    gimple_fold_stmt_to_constant_1 an optimization that folds
    &something p+ const_int
    into
    &MEM_REF[..., off]
    On GENERIC, we don't do that and e.g. for constant evaluation it
    could be pretty harmful if e.g. such pointers are dereferenced, because
    it can lose what exact field it was starting with etc., all it knows
    is the base and offset, type and alias set.
    Instead of teaching the match.pd address_compare about 3 extra variants
    where one or both compared operands are pointer_plus, this patch attempts
    to fold operands of comparisons similarly to gimple_fold_stmt_to_constant_1
    before calling fold_binary on it.
    There is another thing though, while we do have (x p+ y) p+ z to
    x p+ (y + z) simplification which works on GIMPLE well because of the
    useless pointer conversions, on GENERIC we can have pointer casts in
between
    and at that point we can end up with large expressions like
    ((type3) (((type2) ((type1) (&var + 2) + 2) + 2) + 2))
    etc.  Pointer-plus doesn't really care what exact pointer type it has as
    long as it is a pointer, so the following match.pd simplification for
    GENERIC only (it is useless for GIMPLE) also moves the cast so that nested
    p+ can be simplified.

    Note, I've noticed we don't really diagnose going out of bounds with
    pointer_plus (unlike e.g. with ARRAY_REF) during constant evaluation, I
    think another patch for cxx_eval_binary_expression with POINTER_PLUS will
be
    needed.  But it isn't clear to me what exactly it should do in case of
    subobjects.  If we start with address of a whole var, (&var), I guess we
    should diagnose if the pointer_plus gets before start of the var (i.e.
    "negative") or 1 byte past the end of the var, but what if we start with
    &var.field or &var.field[3] ?  For &var.field, shall we diagnose out of
    bounds of field (except perhaps flexible members?) or the whole var?
    For ARRAY_REFs, I assume we must at least strip all the outer ARRAY_REFs
    and so start with &var.field too, right?

    2022-01-08  Jakub Jelinek  <jakub@redhat.com>

            PR c++/89074
    gcc/
            * match.pd ((ptr) (x p+ y) p+ z -> (ptr) (x p+ (y + z))): New
GENERIC
            simplification.
    gcc/cp/
            * constexpr.c (cxx_maybe_fold_addr_pointer_plus): New function.
            (cxx_eval_binary_expression): Use it.
    gcc/testsuite/
            * g++.dg/cpp1y/constexpr-89074-2.C: New test.
            * g++.dg/cpp1z/constexpr-89074-1.C: New test.

  parent reply	other threads:[~2022-01-08  8:54 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-89074-4@http.gcc.gnu.org/bugzilla/>
2021-07-27  9:30 ` pinskia at gcc dot gnu.org
2021-11-18 14:18 ` redi at gcc dot gnu.org
2021-11-18 16:14 ` cvs-commit at gcc dot gnu.org
2022-01-05 16:38 ` ppalka at gcc dot gnu.org
2022-01-05 16:56 ` jakub at gcc dot gnu.org
2022-01-05 17:07 ` ppalka at gcc dot gnu.org
2022-01-05 19:45 ` jakub at gcc dot gnu.org
2022-01-05 19:56 ` jakub at gcc dot gnu.org
2022-01-05 22:07 ` cvs-commit at gcc dot gnu.org
2022-01-05 22:07 ` cvs-commit at gcc dot gnu.org
2022-01-06 20:01 ` jakub at gcc dot gnu.org
2022-01-08  8:54 ` cvs-commit at gcc dot gnu.org [this message]
2022-01-14 11:08 ` cvs-commit at gcc dot gnu.org
2022-01-19  8:26 ` cvs-commit at gcc dot gnu.org
2022-02-06 10:21 ` cvs-commit at gcc dot gnu.org
2022-07-07 16:38 ` cvs-commit at gcc dot gnu.org
2022-11-07 14:16 ` ppalka at gcc dot gnu.org

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=bug-89074-4-CMaLaZozvN@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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: link
Be 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).