public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lh_mouse at 126 dot com" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-103875-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103875

            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
<https://quuxplusone.github.io/blog/2018/04/17/downsides-of-omitting-trivial-destructor-calls/>,
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 = 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() *= 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 = 0;
        int& operator*=(int x) { return this->val *= 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() *= 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 here
        mov     QWORD PTR [rdi], rax
        ret
```


[1] https://gcc.godbolt.org/z/f9xx3c6Y7
[2] https://gcc.godbolt.org/z/Ysss5GMjj

             reply	other threads:[~2021-12-31 13:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-31 13:08 lh_mouse at 126 dot com [this message]
2021-12-31 14:14 ` [Bug ipa/103875] " pinskia at gcc dot gnu.org
2022-01-04 14:09 ` rguenth at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-103875-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).