[Bug c++/52231] [missed optimization/diagnostics] address-of-reference
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231 --- Comment #1 from Andrew Pinski 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.
[Bug c++/52231] [missed optimization/diagnostics] address-of-reference
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231 Jonathan Wakely changed: What|Removed |Added Status|UNCONFIRMED |NEW Last reconfirmed||2012-02-13 Ever Confirmed|0 |1 --- Comment #2 from Jonathan Wakely 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 ;
[Bug c++/52231] [missed optimization/diagnostics] address-of-reference
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231 --- Comment #3 from Jonathan Wakely 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();
[Bug c++/52231] [missed optimization/diagnostics] address-of-reference
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231 Pawel Sikora changed: What|Removed |Added Status|NEW |RESOLVED Known to work||6.2.1, 7.0 Resolution|--- |FIXED --- Comment #8 from Pawel Sikora --- s% ~/src/gcc-install/usr/local/bin/g++ pr52231.cpp -c -O2 -Wall -Wextra -fdump-tree-optimized -fdiagnostics-color=never pr52231.cpp: In function 'void bar()': pr52231.cpp:7:17: warning: the compiler can assume that the address of 'a' will never be NULL [-Waddress] if ( &a == 0 )// <== useless stmt (not diagnosed/not optimized) ~~~^~~~
[Bug c++/52231] [missed optimization/diagnostics] address-of-reference
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231 Andrew Pinski changed: What|Removed |Added CC||froydnj at gcc dot gnu.org --- Comment #4 from Andrew Pinski --- *** Bug 65111 has been marked as a duplicate of this bug. ***
[Bug c++/52231] [missed optimization/diagnostics] address-of-reference
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231 --- Comment #5 from Nathan Froyd --- 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 void init(int& p) { new (&p) float(3.14f); } which clang understands how to optimize and GCC does not.
[Bug c++/52231] [missed optimization/diagnostics] address-of-reference
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231 --- Comment #6 from Jonathan Wakely --- (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
[Bug c++/52231] [missed optimization/diagnostics] address-of-reference
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52231 --- Comment #7 from Nathan Froyd --- (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).