public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/51392] New: Wrong code with -Os when __attribute__((__const__)) function returns structure
@ 2011-12-03 10:09 cyp561 at gmail dot com
  2011-12-03 16:27 ` [Bug c++/51392] " cybersphinx.gcc at dispostable dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: cyp561 at gmail dot com @ 2011-12-03 10:09 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 51392
           Summary: Wrong code with -Os when __attribute__((__const__))
                    function returns structure
    Classification: Unclassified
           Product: gcc
           Version: 4.5.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: cyp561@gmail.com
            Target: x86_64-pc-linux-gnu
             Build: gcc version 4.5.3 (Gentoo 4.5.3-r1 p1.0, pie-0.4.5)


Fails with -Os, works with -O2:
4.5.3
Works always:
4.1.2  4.2.4  4.3.6  4.4.6
Fails always:
3.4.6

Only fails if the copy constructor is manually defined.
Works with __attribute__((__pure__)) instead of __attribute__((__const__)).
Still fails with printf and #include <stdio.h> removed.

Maybe it's somehow illegal with __attribute__((__const__)) to return a
structure with a manually defined copy constructor?



Testcase, reduced as much as I could:
/*

$ g++ -O2 -o error error.cpp
$ ./error
(-11 1 4)
$ g++ -Os -o error error.cpp
$ ./error
(1790250 1127603896 -1465067344)
Aborted
$ ./error
(1801140 -1464045896 574146144)
Aborted

*/

extern "C" void abort();

struct Vector3f
{
    Vector3f() {}
    Vector3f(int x, int y, int z) : x(x), y(y), z(z) {}
    Vector3f(Vector3f const &v) : x(v.x), y(v.y), z(v.z) {}  // Essential

    int x, y, z;
};

__attribute__((__const__))
Vector3f pie_SurfaceNormal3fv(Vector3f a, Vector3f b)
{
    return Vector3f(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
}

void normalsOnTile(Vector3f *normals)
{
    Vector3f b(3, 5, 7);
    Vector3f c(4, 8, 9);

    *normals = pie_SurfaceNormal3fv(b, c);
}

#include <stdio.h>

int main()
{
    Vector3f normals;
    normalsOnTile(&normals);
    printf("(%d %d %d)\n", normals.x, normals.y, normals.z);
    if (normals.x != -11)
        abort();
}


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

* [Bug c++/51392] Wrong code with -Os when __attribute__((__const__)) function returns structure
  2011-12-03 10:09 [Bug c++/51392] New: Wrong code with -Os when __attribute__((__const__)) function returns structure cyp561 at gmail dot com
@ 2011-12-03 16:27 ` cybersphinx.gcc at dispostable dot com
  2011-12-04 14:04 ` mikpe at it dot uu.se
  2011-12-04 15:51 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: cybersphinx.gcc at dispostable dot com @ 2011-12-03 16:27 UTC (permalink / raw)
  To: gcc-bugs

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

cybersphinx <cybersphinx.gcc at dispostable dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |cybersphinx.gcc at
                   |                            |dispostable dot com

--- Comment #1 from cybersphinx <cybersphinx.gcc at dispostable dot com> 2011-12-03 16:27:09 UTC ---
With Debian's gcc 4.6.2-5 the testcase fails with -Os, -O1, -O2, ok with -O0,
-O3, -Ofast. 4.5.3-9 fails with -Os, -O1, ok with -O0, -O2, -O3.


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

* [Bug c++/51392] Wrong code with -Os when __attribute__((__const__)) function returns structure
  2011-12-03 10:09 [Bug c++/51392] New: Wrong code with -Os when __attribute__((__const__)) function returns structure cyp561 at gmail dot com
  2011-12-03 16:27 ` [Bug c++/51392] " cybersphinx.gcc at dispostable dot com
@ 2011-12-04 14:04 ` mikpe at it dot uu.se
  2011-12-04 15:51 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: mikpe at it dot uu.se @ 2011-12-04 14:04 UTC (permalink / raw)
  To: gcc-bugs

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

Mikael Pettersson <mikpe at it dot uu.se> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mikpe at it dot uu.se

--- Comment #2 from Mikael Pettersson <mikpe at it dot uu.se> 2011-12-04 14:04:06 UTC ---
I can reproduce the error on i686-linux with g++ 4.7-20111203, 4.6-20111202,
and 4.5-20111201 at -Os/-O1.  Works at -O0/-O3, -O2 varies.  4.4-20111108 seems
to work.


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

* [Bug c++/51392] Wrong code with -Os when __attribute__((__const__)) function returns structure
  2011-12-03 10:09 [Bug c++/51392] New: Wrong code with -Os when __attribute__((__const__)) function returns structure cyp561 at gmail dot com
  2011-12-03 16:27 ` [Bug c++/51392] " cybersphinx.gcc at dispostable dot com
  2011-12-04 14:04 ` mikpe at it dot uu.se
@ 2011-12-04 15:51 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-12-04 15:51 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-12-04 15:50:40 UTC ---
__attribute__((__const__))
Vector3f pie_SurfaceNormal3fv(Vector3f a, Vector3f b)
{
    return Vector3f(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
}

I think it is illegal to use the const attribute when the C++ frontend
may elide the return value copy to being passed by invisible reference.
That way the middle-end sees a store to global memory which is a side-effect
that the const attribute does not allow (similar for pure btw.).

Note that the Vector3f() constructors are not const either but they are
both called from pie_SurfaceNormal3fv.


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

end of thread, other threads:[~2011-12-04 15:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-03 10:09 [Bug c++/51392] New: Wrong code with -Os when __attribute__((__const__)) function returns structure cyp561 at gmail dot com
2011-12-03 16:27 ` [Bug c++/51392] " cybersphinx.gcc at dispostable dot com
2011-12-04 14:04 ` mikpe at it dot uu.se
2011-12-04 15:51 ` 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).