From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 8F4C13858D37; Fri, 31 Dec 2021 13:08:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8F4C13858D37 From: "lh_mouse at 126 dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug pending/103875] New: Dead writes are not optimized out Date: Fri, 31 Dec 2021 13:08:59 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: pending X-Bugzilla-Version: 11.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: lh_mouse at 126 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 31 Dec 2021 13:08:59 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103875 Bug ID: 103875 Summary: Dead writes are not optimized out Product: gcc Version: 11.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: pending Assignee: unassigned at gcc dot gnu.org Reporter: lh_mouse at 126 dot com Target Milestone: --- I was reading , which I think is a great article. But the issue that was mentioned in that article seems to be specific to `std::vector`. Specifically, for this hand-written 'static vector', neither GCC or Clang is able to optimize out any dead writes (godbolt [1]): ``` struct foo { using value_type =3D int; unsigned long size; value_type data[10]; value_type& back() { return this->data[this->size-1]; } void pop() { this->size --; this->data[this->size].~value_type(); // the last element is destroyed here } }; void use_foo(foo& f) { f.back() *=3D 42; // this write should be dead f.pop(); } ``` ``` use_foo(foo&): mov rdx, QWORD PTR [rdi] lea rax, [rdx-1] imul edx, DWORD PTR [rdi+4+rdx*4], 42 mov DWORD PTR [rdi+8+rax*4], edx # dead write is here mov QWORD PTR [rdi], rax ret ``` Making `value_type` non-trivial as suggested does not help (godbolt [2]): ``` struct foo { struct value_type { int val =3D 0; int& operator*=3D(int x) { return this->val *=3D x; } ~value_type() { } // non-trivial }; unsigned long size; value_type data[10]; value_type& back() { return this->data[this->size-1]; } void pop() { this->size --; this->data[this->size].~value_type(); } }; void use_foo(foo& f) { f.back() *=3D 42; f.pop(); } ``` ``` use_foo(foo&): mov rdx, QWORD PTR [rdi] lea rax, [rdx-1] imul edx, DWORD PTR [rdi+4+rdx*4], 42 mov DWORD PTR [rdi+8+rax*4], edx # dead write is still h= ere mov QWORD PTR [rdi], rax ret ``` [1] https://gcc.godbolt.org/z/f9xx3c6Y7 [2] https://gcc.godbolt.org/z/Ysss5GMjj=