From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id CD216385772E; Thu, 23 Nov 2023 14:41:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD216385772E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1700750481; bh=/L4CEbid4ZgCdrPkzDCVqZvCMF4a3BLxpCqawWliytA=; h=From:To:Subject:Date:From; b=IFTWoVNIRY0TUohaj5fqbD8IiHTl+Co7+lKwIuLXw0APwHoTZyblsWxI3snRe9auj Y6urbUtLVcQgy8IAhn1bQo8IzS6v2p0TviwH19IZwuwCT3jQY4KNkuTPBnRE7BZhds JwmRnw/QCfmHeuyMfCsIWVIDhqQW0o0Qmz51zm3M= From: "antoshkka at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/112683] New: Optimizing memcpy range by extending to word bounds Date: Thu, 23 Nov 2023 14:41:21 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 14.0 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=3D112683 Bug ID: 112683 Summary: Optimizing memcpy range by extending to word bounds Product: gcc Version: 14.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: antoshkka at gmail dot com Target Milestone: --- Consider the minimized source code from libstdc++ ``` struct string { unsigned long _M_string_length; enum { _S_local_capacity =3D 15 }; char _M_local_buf[_S_local_capacity + 1]; }; string copy(const string& __str) noexcept { string result; if (__str._M_string_length > __str._S_local_capacity) __builtin_unreachable(); result._M_string_length =3D __str._M_string_length; __builtin_memcpy(result._M_local_buf, __str._M_local_buf, __str._M_string_length + 1); return result; } ``` Right now GCC with -O2 emits a long assembly with ~50 instructions https://godbolt.org/z/a89bh17hd However, note that * the `result._M_local_buf` is uninitialized, * there's at most 16 bytes to copy to `result._M_local_buf` which is of siz= e 16 bytes So the compiler could optimize the code to always copy 16 bytes. The behavi= or change is not observable by user as the uninitialized bytes could contain a= ny data, including the same bytes as `_str._M_local_buf`. As a result of always copying 16 bytes, the assembly becomes more than 7 ti= mes shorter, conditional jumps go away: https://godbolt.org/z/r5GPYTs4Y=