public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ville Voutilainen <ville.voutilainen@gmail.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: Jakub Jelinek <jakub@redhat.com>,
	Jonathan Wakely <jwakely.gcc@gmail.com>,
		"libstdc++" <libstdc++@gcc.gnu.org>,
	GCC Patches <gcc-patches@gcc.gnu.org>,
		Marc Glisse <marc.glisse@inria.fr>
Subject: Re: [PATCH] Use __builtin_is_constant_evaluated in std::less etc. (PR tree-optimization/88775)
Date: Mon, 14 Jan 2019 08:40:00 -0000	[thread overview]
Message-ID: <CAFk2RUYEJ+FXa6oOCEg=WYtnFzkC5qy+UG-3e+a3SmuxVMpeyg@mail.gmail.com> (raw)
In-Reply-To: <CAFiYyc0ZLu=X9YAKQhTAZ8Da5UX0wHo8-HeNPp6DU7xvBdWymQ@mail.gmail.com>

On Mon, 14 Jan 2019 at 10:29, Richard Biener <richard.guenther@gmail.com> wrote:
> >        _GLIBCXX14_CONSTEXPR bool
> >        operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW
> >        {
> > +#if __cplusplus >= 201402L
> > +#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
> > +       if (__builtin_is_constant_evaluated())
> > +#else
> >         if (__builtin_constant_p (__x > __y))
> > +#endif
> >           return __x > __y;
> > +#endif
> >         return (__UINTPTR_TYPE__)__x > (__UINTPTR_TYPE__)__y;
>
> I wonder what the idea behind this is.  It smells like trying to avoid
> undefined behavior (relational compare of pointers to different objects?)
> but then executing that nevertheless when "constant"?
>
> I think this just doesn't work since the compiler, when evaluating
> __x > __y [for constant folding] is exploiting the fact that doing
> non-equality compares on pointers into different objects invoke
> undefined behavior.

When that happens, the function is ill-formed when constant-evaluated,
which is fine.
When the comparison is not UB, it should constant-evaluate without problems.

> So why is this not just
>   return (__UINTPTR_TYPE__)__x > (__UINTPTR_TYPE__)__y;
> or with the casts elided?

Those casts are reinterpret_casts, so the function could never be
constant-evaluated.
The casts need to be there to avoid UB for the run-time cases.

  reply	other threads:[~2019-01-14  8:40 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-10  9:02 Jakub Jelinek
2019-01-10 10:52 ` Jonathan Wakely
2019-01-14  8:29 ` Richard Biener
2019-01-14  8:40   ` Ville Voutilainen [this message]
2019-01-14  8:42   ` Jakub Jelinek
2019-01-14  9:17     ` Richard Biener
2019-01-14  9:21       ` Jonathan Wakely
2019-01-14 10:43         ` Richard Biener
2019-01-14  9:22       ` Jonathan Wakely
2019-01-14  9:21   ` Jonathan Wakely

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='CAFk2RUYEJ+FXa6oOCEg=WYtnFzkC5qy+UG-3e+a3SmuxVMpeyg@mail.gmail.com' \
    --to=ville.voutilainen@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jwakely.gcc@gmail.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=marc.glisse@inria.fr \
    --cc=richard.guenther@gmail.com \
    /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).