public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52231] New: [missed optimization/diagnostics] address-of-reference
@ 2012-02-13 13:33 pluto at agmk dot net
  2012-02-13 17:56 ` [Bug c++/52231] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: pluto at agmk dot net @ 2012-02-13 13:33 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231

             Bug #: 52231
           Summary: [missed optimization/diagnostics] address-of-reference
    Classification: Unclassified
           Product: gcc
           Version: 4.6.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: pluto@agmk.net


int& singleton();
void foo();
void bar()
{
        int& a = singleton();
        int& b = singleton();
        if ( &a == 0 )         <== useless stmt (not diagnosed/not optimized)
                foo();
        a = 5;
        if ( b != 5 )
                foo();
}

in c++ the adress of refer

$ g++ 0.cpp -O2 -Wall -Wextra -fdump-tree-optimized -c && cat
0.cpp.143t.optimized

void bar() ()
{
  int & D.2079;
  int & D.2078;
  int D.2074;
<bb 2>:
  D.2078_1 = singleton ();
  D.2079_3 = singleton ();
  if (D.2078_1 == 0B)          <========
    goto <bb 3>;
  else
    goto <bb 4>;
<bb 3>:
  foo ();
<bb 4>:
  *D.2078_1 = 5;
  D.2074_5 = *D.2079_3;
  if (D.2074_5 != 5)
    goto <bb 5>;
  else
    goto <bb 6>;
<bb 5>:
  foo (); [tail call]

<bb 6>:
  return;
}


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

* [Bug c++/52231] [missed optimization/diagnostics] address-of-reference
  2012-02-13 13:33 [Bug c++/52231] New: [missed optimization/diagnostics] address-of-reference pluto at agmk dot net
@ 2012-02-13 17:56 ` pinskia at gcc dot gnu.org
  2012-02-13 18:13 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-02-13 17:56 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-02-13 17:56:32 UTC ---
Why do you think this is a missed optimization and/or diagnostic?
References are just like pointers, just &a is really a and a is really *a.


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

* [Bug c++/52231] [missed optimization/diagnostics] address-of-reference
  2012-02-13 13:33 [Bug c++/52231] New: [missed optimization/diagnostics] address-of-reference pluto at agmk dot net
  2012-02-13 17:56 ` [Bug c++/52231] " pinskia at gcc dot gnu.org
@ 2012-02-13 18:13 ` redi at gcc dot gnu.org
  2012-02-13 18:19 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2012-02-13 18:13 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-02-13
     Ever Confirmed|0                           |1

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-02-13 18:13:23 UTC ---
But the language guarantees that for a program without undefined behaviour a
reference is always bound to a valid object.

int* i = nullptr;
int& r = *i;  // undefined
if (&r == 0)  // cannot be true
  ;


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

* [Bug c++/52231] [missed optimization/diagnostics] address-of-reference
  2012-02-13 13:33 [Bug c++/52231] New: [missed optimization/diagnostics] address-of-reference pluto at agmk dot net
  2012-02-13 17:56 ` [Bug c++/52231] " pinskia at gcc dot gnu.org
  2012-02-13 18:13 ` redi at gcc dot gnu.org
@ 2012-02-13 18:19 ` redi at gcc dot gnu.org
  2015-02-18 19:36 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2012-02-13 18:19 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-02-13 18:19:39 UTC ---
(In reply to comment #1)
> References are just like pointers, just &a is really a and a is really *a.

This is wrong in so many ways.

Pointers can be null, pointers can be uninitialized, pointers can be re-seated.

Would you also argue this shouldn't be optimised?

int i=0;
int& r = i;
// ...
if (&i != &r)
  unreachable();


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

* [Bug c++/52231] [missed optimization/diagnostics] address-of-reference
  2012-02-13 13:33 [Bug c++/52231] New: [missed optimization/diagnostics] address-of-reference pluto at agmk dot net
                   ` (2 preceding siblings ...)
  2012-02-13 18:19 ` redi at gcc dot gnu.org
@ 2015-02-18 19:36 ` pinskia at gcc dot gnu.org
  2015-02-18 19:44 ` froydnj at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2015-02-18 19:36 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |froydnj at gcc dot gnu.org

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 65111 has been marked as a duplicate of this bug. ***


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

* [Bug c++/52231] [missed optimization/diagnostics] address-of-reference
  2012-02-13 13:33 [Bug c++/52231] New: [missed optimization/diagnostics] address-of-reference pluto at agmk dot net
                   ` (3 preceding siblings ...)
  2015-02-18 19:36 ` pinskia at gcc dot gnu.org
@ 2015-02-18 19:44 ` froydnj at gcc dot gnu.org
  2015-02-18 21:30 ` redi at gcc dot gnu.org
  2015-02-18 22:04 ` froydnj at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: froydnj at gcc dot gnu.org @ 2015-02-18 19:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Nathan Froyd <froydnj at gcc dot gnu.org> ---
FWIW, clang (>= 3.5) understands how to optimize the original testcase in
comment 0; it even issues a -Wtautological-undefined-compare warning.

This also showed up in the context of trying to hint to the compiler that
placement new didn't need null checks:

#include <new>

void init(int& p) { new (&p) float(3.14f); }

which clang understands how to optimize and GCC does not.


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

* [Bug c++/52231] [missed optimization/diagnostics] address-of-reference
  2012-02-13 13:33 [Bug c++/52231] New: [missed optimization/diagnostics] address-of-reference pluto at agmk dot net
                   ` (4 preceding siblings ...)
  2015-02-18 19:44 ` froydnj at gcc dot gnu.org
@ 2015-02-18 21:30 ` redi at gcc dot gnu.org
  2015-02-18 22:04 ` froydnj at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2015-02-18 21:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Nathan Froyd from comment #5)
> This also showed up in the context of trying to hint to the compiler that
> placement new didn't need null checks:

That's only become true quite recently:
http://open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1748

See also PR 35878


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

* [Bug c++/52231] [missed optimization/diagnostics] address-of-reference
  2012-02-13 13:33 [Bug c++/52231] New: [missed optimization/diagnostics] address-of-reference pluto at agmk dot net
                   ` (5 preceding siblings ...)
  2015-02-18 21:30 ` redi at gcc dot gnu.org
@ 2015-02-18 22:04 ` froydnj at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: froydnj at gcc dot gnu.org @ 2015-02-18 22:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Nathan Froyd <froydnj at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #6)
> (In reply to Nathan Froyd from comment #5)
> > This also showed up in the context of trying to hint to the compiler that
> > placement new didn't need null checks:
> 
> That's only become true quite recently:
> http://open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1748

Ah, thanks for the pointer.  I assumed that the compiler would be able to infer
that |&reference| would be non-null and eliminate the mandatory check
regardless, but it's nice to have spec language to back this up.

> See also PR 35878

Again, thanks for the pointer.  If we can do this in the frontend now, that
might be simple enough that I could take a look at it myself (assuming it
hasn't been done and that PR simply didn't get referenced).


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

end of thread, other threads:[~2015-02-18 21:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-13 13:33 [Bug c++/52231] New: [missed optimization/diagnostics] address-of-reference pluto at agmk dot net
2012-02-13 17:56 ` [Bug c++/52231] " pinskia at gcc dot gnu.org
2012-02-13 18:13 ` redi at gcc dot gnu.org
2012-02-13 18:19 ` redi at gcc dot gnu.org
2015-02-18 19:36 ` pinskia at gcc dot gnu.org
2015-02-18 19:44 ` froydnj at gcc dot gnu.org
2015-02-18 21:30 ` redi at gcc dot gnu.org
2015-02-18 22:04 ` froydnj 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).