From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3E58A3858C83; Mon, 17 Oct 2022 08:10:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E58A3858C83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665994223; bh=1clTg611umgGQu1bJvKRrghf92JTklJp1zqqGRzPa+A=; h=From:To:Subject:Date:In-Reply-To:References:From; b=f1DERcl0322QhNPec8UycGZ+D6Qze6rZ/xVR7jR3vQz8aNYAZUaDDDNBePOjxm5eQ BQeC019T+afB4AvFU1fmPbDFXGtE/m8MbmG6RdxE65bcLWlvV7BaWhXrrcUCUdYm+T Rdf9zMWpwTAzSJuvld6L2ZkEafFmRQkvHiATydu0= From: "alex.curiou at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed 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: RESOLVED X-Bugzilla-Resolution: INVALID 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: Message-ID: In-Reply-To: References: 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 --- Comment #3 from Alexey --- (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 optimizati= ons > (for -O0 already very early), if optimizations before it manage to optim= ize > 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 with= out > 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 unpredicta= ble. 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.=