From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8920 invoked by alias); 3 Dec 2011 10:09:48 -0000 Received: (qmail 8911 invoked by uid 22791); 3 Dec 2011 10:09:47 -0000 X-SWARE-Spam-Status: No, hits=-2.9 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 03 Dec 2011 10:09:26 +0000 From: "cyp561 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/51392] New: Wrong code with -Os when __attribute__((__const__)) function returns structure Date: Sat, 03 Dec 2011 10:09:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: cyp561 at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-12/txt/msg00224.txt.bz2 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 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 int main() { Vector3f normals; normalsOnTile(&normals); printf("(%d %d %d)\n", normals.x, normals.y, normals.z); if (normals.x != -11) abort(); }