From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 156473858C83; Mon, 17 Oct 2022 06:55:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 156473858C83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665989709; bh=+6FjRJV8e0pVhtXbeI92jfng16fdu1JoXFhJsbmq+cs=; h=From:To:Subject:Date:From; b=BH00ivx+esUm7K3xvbJ6wNplGLlOutTyQWXCQ1ROlTBoRTejaD+5pRW1NPAhXAmEl 3m0GvKb7khe0uvijezMxyUO3ZBVn5pXS8MGqzsPAqT/dnG0hw59Csacmcg3tuVhz4/ 9l6XZ6cAJcP+y04Y6AONkxyModkdLOVXpfsmSIng= From: "alex.curiou at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: alex.curiou at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107285 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 =3D 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 ~~~=