Bug 32697 - O2 optimizes away assignment
Summary: O2 optimizes away assignment
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-07-09 13:20 UTC by Dries Decock
Modified: 2007-07-09 14:07 UTC (History)
15 users (show)

See Also:
Host: i686-pc-linux
Target: i686-pc-linux i686-mingw32
Build: i686-pc-linux
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dries Decock 2007-07-09 13:20:26 UTC
Hi,

I'm using SWIG to combine Java and C++ code. At some point, SWIG generates a method like this :

1SWIGEXPORT jlong JNICALL Java_com_excentis_products_X_XUpcast(JNIEnv *jenv, jclass jcls, jlong jarg1) {
    jlong baseptr = 0;
    (void)jenv;
    (void)jcls;
    *(Flow **)&baseptr = *(TcpFlow **)&jarg1;
    return baseptr;
}

The problem is that the assignment is not executed and the return value is always zero, when using -O2 optimization. When I disable O2, or add a debug statement, the value is ok.

I don't like the generated code, but it should work fine.
Comment 1 Andreas Schwab 2007-07-09 13:35:29 UTC
Report this to the SWIG maintainers.  This is violating the C/C++ aliasing rules.

*** This bug has been marked as a duplicate of 21920 ***
Comment 2 Jeffrey Sorensen 2007-07-09 13:55:51 UTC
Please refer to https://sourceforge.net/tracker/?func=detail&atid=101645&aid=1560993&group_id=1645

Long story short - as it stands today: C++ standards team won't change the spec to allow type punning, gcc team won't "fix" code generation if it involves type punning, swig team won't rewrite the code generators to generate ANSI spec compliant code.

(The only way to do this by current ANSI spec rules is to memcpy the contents of the pointers themselves from a pointer of one type to a pointer of another type.)

Your only solution at this time is to use -fno-strict-aliasing when compiling swig wrapper code.  This has been a major headache mainly for package maintainers.
Comment 3 Dries Decock 2007-07-09 14:07:30 UTC
Thanks for the reply. 

I will use the -fno-strict-aliasing for now, and hope that the SWIG maintainers will fix this problem in the near future.

Thanks again.