public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug pending/103875] New: Dead writes are not optimized out
@ 2021-12-31 13:08 lh_mouse at 126 dot com
  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
  0 siblings, 2 replies; 3+ messages in thread
From: lh_mouse at 126 dot com @ 2021-12-31 13:08 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug ipa/103875] Dead writes are not optimized out
  2021-12-31 13:08 [Bug pending/103875] New: Dead writes are not optimized out lh_mouse at 126 dot com
@ 2021-12-31 14:14 ` pinskia at gcc dot gnu.org
  2022-01-04 14:09 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-31 14:14 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
          Component|middle-end                  |ipa
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-12-31
                 CC|                            |marxin at gcc dot gnu.org

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The inliner removes the clobber in this case it seems:
We have:

void foo::pop (struct foo * const this)
{
  long unsigned int _1;
  long unsigned int _2;
  long unsigned int _3;
  value_type * _4;

  <bb 2> :
  _1 = this_6(D)->size;
  _2 = _1 + 18446744073709551615;
  this_6(D)->size = _2;
  _3 = this_6(D)->size;
  _4 = &this_6(D)->data[_3];
  *_4 ={v} {CLOBBER};
  return;

}
void use_foo (struct foo & f)
{
  int _1;
  int _2;
  value_type & _6;

  <bb 2> :
  _6 = foo::back (f_4(D));
  _1 = *_6;
  _2 = _1 * 42;
  *_6 = _2;
  foo::pop (f_4(D));
  return;

}

And then foo::pop (and back) gets inlined we get:

void use_foo (struct foo & f)
{
  value_type & D.2143;
  int _1;
  int _2;
  long unsigned int _5;
  value_type & _6;
  long unsigned int _9;
  long unsigned int _10;
  long unsigned int _11;
  value_type & _12;
  value_type & _13;

  <bb 2> :
  _10 = f_4(D)->size;
  _11 = _10 + 18446744073709551615;
  _12 = &f_4(D)->data[_11];
  _13 = _12;
  _6 = _13;
  _1 = *_6;
  _2 = _1 * 42;
  *_6 = _2;
  _5 = f_4(D)->size;
  _9 = _5 + 18446744073709551615;
  f_4(D)->size = _9;
  return;

}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug ipa/103875] Dead writes are not optimized out
  2021-12-31 13:08 [Bug pending/103875] New: Dead writes are not optimized out lh_mouse at 126 dot com
  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
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-01-04 14:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
The clobber is gone at the point we inline pop(), CDDCE1 removes it because
the clobbered address computation is dead.

Eliminating unnecessary statements:
Deleting : *_4 ={v} {CLOBBER};

Deleting : _4 = &this_6(D)->data[_2];

IL before CDDCE:

  <bb 2> :
  _1 = this_6(D)->size;
  _2 = _1 + 18446744073709551615;
  this_6(D)->size = _2;
  _4 = &this_6(D)->data[_2];
  *_4 ={v} {CLOBBER};
  return;

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-04 14:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-31 13:08 [Bug pending/103875] New: Dead writes are not optimized out lh_mouse at 126 dot com
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

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).