public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100746] New: NRVO should not introduce aliasing
@ 2021-05-24 16:16 antoshkka at gmail dot com
  2021-05-24 20:57 ` [Bug c++/100746] " glisse at gcc dot gnu.org
  2021-05-25  7:41 ` rguenth at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: antoshkka at gmail dot com @ 2021-05-24 16:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100746
           Summary: NRVO should not introduce aliasing
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the example:

struct NrvoPassed {
    NrvoPassed() = default;
    NrvoPassed(const NrvoPassed&);
    NrvoPassed(NrvoPassed&&);

    int i = 0;
};

auto test(int* data) {
    NrvoPassed x;
    *data = 3;
    if (x.i != 0) __builtin_abort();
    return x;
}


Resulting assembly contains call to `abort`:

test(int*):
  mov DWORD PTR [rdi], 0
  mov DWORD PTR [rsi], 3
  mov edx, DWORD PTR [rdi]
  test edx, edx
  jne .L3
  mov rax, rdi
  ret
test(int*) [clone .cold]:
.L3:
  push rax
  call abort

Optimizer thinks that the value of `x.i` is aliased by `data`, however `data`
is a local variable and it's address could not leak before the object is
constructed.

Some other compilers already have the proposed optimization:
https://godbolt.org/z/aqdveadnE

Adding `__restrict` to `data` fixes the codegen:

test2(int*):
  mov DWORD PTR [rdi], 0
  mov rax, rdi
  mov DWORD PTR [rsi], 3
  ret

Probably `__restrict` should be always added to the storage address passed for
NRVO.

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

* [Bug c++/100746] NRVO should not introduce aliasing
  2021-05-24 16:16 [Bug c++/100746] New: NRVO should not introduce aliasing antoshkka at gmail dot com
@ 2021-05-24 20:57 ` glisse at gcc dot gnu.org
  2021-05-25  7:41 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: glisse at gcc dot gnu.org @ 2021-05-24 20:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Marc Glisse <glisse at gcc dot gnu.org> ---
PR 80740 ?

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

* [Bug c++/100746] NRVO should not introduce aliasing
  2021-05-24 16:16 [Bug c++/100746] New: NRVO should not introduce aliasing antoshkka at gmail dot com
  2021-05-24 20:57 ` [Bug c++/100746] " glisse at gcc dot gnu.org
@ 2021-05-25  7:41 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-05-25  7:41 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
dup

*** This bug has been marked as a duplicate of bug 80740 ***

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

end of thread, other threads:[~2021-05-25  7:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-24 16:16 [Bug c++/100746] New: NRVO should not introduce aliasing antoshkka at gmail dot com
2021-05-24 20:57 ` [Bug c++/100746] " glisse at gcc dot gnu.org
2021-05-25  7:41 ` 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).