From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 85E3C3858D35; Wed, 4 Jan 2023 13:20:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85E3C3858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672838440; bh=Tcr1Qk09f8ItlT9r8jTECyUXWVrxxu4JGrFZ8cmTEAM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=GHoaZq40ikj7WTSPayX3lHjhTpL2CRSviZgIhi3pdpHGURp7k96oRjTtJYzxP7DTq L8kRJ0adXBKBQE0zzyBY81Qp1h7xcLotv3FRZNp0n1/r6sXf2FchrCNUVDsjbX1R3+ TXCe/ns4RhaPV1m64OwC5FYNDxQs72PXq04A/emY= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/108243] [10/11/12/13 Regression] Missed optimization for static const std::string_view(const char*) Date: Wed, 04 Jan 2023 13:20:40 +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: 11.3.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW 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: short_desc 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=3D108243 Jonathan Wakely changed: What |Removed |Added ---------------------------------------------------------------------------- Summary|Missed optimization for |[10/11/12/13 Regression] |static const |Missed optimization for |std::string_view(const |static const |char*) |std::string_view(const | |char*) --- Comment #3 from Jonathan Wakely --- And if the variable is like this it does optimize it: static constexpr string_view foo("bar"); Because then we take the constant evaluation path and calculate the length = by hand. The problem seems to be that GCC considers the __builtin_is_constant_evaluated() branch to be reachable for non-constexpr = and so doesn't inline as aggressively. If we have: struct traits { static constexpr unsigned long length(const char* s) { #ifndef FIX if (__builtin_is_constant_evaluated()) { unsigned long n =3D 0; while (*s++) ++n; return n; } #endif return __builtin_strlen(s); } }; struct string_view { constexpr string_view(const char* s) : str(s), len(traits::length(s)) { } unsigned long size() const { return len; } const char* str; unsigned long len; }; int main() { static const string_view foo("bar"); return foo.size(); } Then with -DFIX we optimize correctly, but without -DFIX we don't. That blo= ck of code should make no difference to inlining decisions, because at runtime= it is always dead code.=