public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "alex.curiou at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/107285] Incorrect code generation when we use __builtin_constant_p built-in function.
Date: Mon, 17 Oct 2022 08:10:23 +0000	[thread overview]
Message-ID: <bug-107285-4-tEeu0jwhFz@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107285-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #3 from Alexey <alex.curiou at gmail dot com> ---
(In reply to Jakub Jelinek from comment #1)
> That is just bogus expectation.
> __builtin_constant_p works that it has a cut-off after certain optimizations
> (for  -O0 already very early), if optimizations before it manage to optimize
> the argument into a constant, it is true.  After the cut-off the remaining
> occurrences are folded into false.
> So, if bar is inlined or say IPA constant propagated, the argument is
> constant, while if it isn't inlined nor IPA constant propagated (etc.), as
> seems to be the case for -Og, it is not constant, thus you get what you
> asked for.
> __builtin_constant_p better should be used as a way to optimize code without
> changing the observable behavior...

For -O2: the generated code and the result should be consistent.
Otherwise the usage of the __builtin_constant_p function is just unpredictable.

But now the code of bar func will calculate 60 and the real return value is 13.

Or just change the explanation for the __builtin_constant_p
(https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html) and write that some
times the branching on the result of this function will have nothing to do with
the final result provided by the compiled code.

  parent reply	other threads:[~2022-10-17  8:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-17  6:54 [Bug c/107285] New: " alex.curiou at gmail dot com
2022-10-17  7:17 ` [Bug c/107285] " jakub at gcc dot gnu.org
2022-10-17  7:40 ` alex.curiou at gmail dot com
2022-10-17  8:10 ` alex.curiou at gmail dot com [this message]
2022-10-17  9:25 ` jakub 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-107285-4-tEeu0jwhFz@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).