From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A1ADE3858D38; Wed, 8 Nov 2023 11:40:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A1ADE3858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1699443621; bh=LCUacdEj9FPuctdCBGv054SQTgvMiXODbcfQJ9uFt+A=; h=From:To:Subject:Date:From; b=us31Jp0BRmexewnyjBwWvj6VJLs0R6Kp1mplsnMM5jmYp1S6T/6AMzxw/sa0rF5AX KPEWKop3AZy7mewGNkJK2Hjjzvbawwqa/xyzKfkMX1M121gQ678mBnoI3XcGEm1dQV N6ZKxTVclM/38p8lEEQQY4Sukwt+FWshbXloNdhI= From: "antoshkka at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/112440] New: Compiler does not grok basic_string::resize and basic_string::reserve if _CharT is char Date: Wed, 08 Nov 2023 11:40:20 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: antoshkka 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 keywords 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=3D112440 Bug ID: 112440 Summary: Compiler does not grok basic_string::resize and basic_string::reserve if _CharT is char Product: gcc Version: 13.2.1 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: antoshkka at gmail dot com Target Milestone: --- Consider the example: #include void test1(std::size_t summ) { std::string result; result.resize(summ); if (result.size() > summ) { __builtin_abort(); } } The resulting assembly contains `call abort` and code to check the string s= ize: https://godbolt.org/z/zcj3Pc3G8 Looks like this is due to char* aliasing with string internals, switching to std::u8string removes the `call abort` related assembly: https://godbolt.org/z/a6bKaqqn5 I've failed to come up with a generic solution, but looks like adding __builtin_unreachable() to the end of basic_string::resize and basic_string::reserve helps: https://godbolt.org/z/vWcjqGK94 P.S.: such hints help to shorten the assembly for reserve+append*n cases https://godbolt.org/z/nsEGsWdP3 , https://godbolt.org/z/qMf4b7dd8 , https://godbolt.org/z/1r6dd6d5M which are quire common=