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] New: Incorrect code generation when we use __builtin_constant_p built-in function. Date: Mon, 17 Oct 2022 06:54:40 +0000 [thread overview] Message-ID: <bug-107285-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107285 Bug ID: 107285 Summary: Incorrect code generation when we use __builtin_constant_p built-in function. Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: alex.curiou at gmail dot com Target Milestone: --- All versions of GCC are affected (I've checked versions from 5 till 12.2). The minimized example of code: ~~~ #define test(val1) \ __builtin_constant_p(val1) ? (val1 + 10) : (val1 * 20); unsigned int bar(unsigned int value1) { return test(value1); } int main(void) { unsigned int a = bar(3); return a; } ~~~ The __builtin_constant_p considers the val1 value to be NOT known to be constant at compile time. So the generated code is val1 * 20 But optimizer considers that the value is WELL known and can WELL be calculated. So, the return value of func bar is calculated at compile time and the call to the bar func is dropped away. And the final return value is WRONG: val1 + 10 As a result we have different return values with -O2 and -Og optimization flags, 13 and 60 accordingly. Here is the compiler output for version 12.2: -O2 will return 13 https://godbolt.org/z/Y1Thdcfch ~~~ bar: lea eax, [rdi+rdi*4] sal eax, 2 ret main: mov eax, 13 ret ~~~ -Og will return 60 https://godbolt.org/z/sbcer78rP ~~~ bar: lea eax, [rdi+rdi*4] sal eax, 2 ret main: mov edi, 3 call bar ret ~~~
next reply other threads:[~2022-10-17 6:55 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-10-17 6:54 alex.curiou at gmail dot com [this message] 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 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@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: 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).