The attached code demonstrates a test program that will fail when optimized at -O2 and higher for GCC versions 4.4.1 and later, but only when a particular procedure is inlined (either explicitly at the request of the user, or implicitly due to auto-inlining).
% gcc --version | head -1 gcc (GCC) 4.4.1 20090725 (Red Hat 4.4.1-2) The test case will fail if (__may_alias__) is not added to a particular type: % gcc -O2 test_alias.c % ./a.out Abort And will pass if the "may alias" attribute is added: % gcc -DMAY_ALIAS -O2 test_alias.c % ./a.out The compiler will issue a warning if -Wall is asserted: % gcc -Wall -Werror -O2 test_alias.c cc1: warnings being treated as errors test_alias.c: In function 'main': test_alias.c:31: error: dereferencing pointer 'dest' does break strict-aliasing rules test_alias.c:29: note: initialized from here test_alias.c:31: error: dereferencing pointer 'src' does break strict-aliasing rules test_alias.c:30: note: initialized from here Note that this check is only implemented if optimization (-O2 or -O3) enabled, because the strict aliasing optimization only kicks in at those levels. This will issue no diagnostic (and perform no strict aliasing optimizations): % gcc -Wall -Werror -O1 test_alias.c When inlining is *explicitly* disabled, no errors are detected, and the resulting program will pass. % sed -e 's/inline/__attribute__((__noinline__))/' \ test_alias.c > test_alias_without_inline.c % gcc -Wall -Werror -O2 test_alias_without_inline.c % ./a.out If we simply remove the "inline" modifiers, we get the error back, because the compiler implicitly inlined the static functions. % sed -e 's/inline//' test_alias.c > test_alias_without_inline.c % gcc -Wall -Werror -O2 test_alias_without_inline.c cc1: warnings being treated as errors test_alias_without_inline.c: In function 'main': test_alias_without_inline.c:31: error: dereferencing pointer 'dest' does break strict-aliasing rules test_alias_without_inline.c:29: note: initialized from here test_alias_without_inline.c:31: error: dereferencing pointer 'src' does break strict-aliasing rules test_alias_without_inline.c:30: note: initialized from here I don't know what the expected behavior is here, either for GCC or as dictated by the C standard. However, note that: when this program is compiled without optimization it works, but it fails when compiled with optimization, and *no* warnings are issued. The test will pass if inlining is explicitly disabled, but will fail when either explicit or implicit inlining is present. One possible solution might be to always issue (by default) the strict-aliasing warning if the aliasing arises as the result of inlining (if that is technically possible). Or, always issue the strict-aliasing warning by default? -- Summary: Inlining changes semantics when strict aliasing optimization is enabled Product: gcc Version: 4.4.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: gary at intrepid dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42830