Bug 32714

Summary: Wrong optimisation for -O3
Product: gcc Reporter: honza
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: attardi, cdfrey, chiabaut, Dries.Decock, gcc-bugs, gcc2eran, hans.buchmann.wantuch, honza, lucifer_ww, mihai.dontu, rosenfeld, sb, schendel, sorenj, stillzhang, takahisa.yokota
Priority: P3    
Version: 4.1.2   
Target Milestone: ---   
Host: i686-pc-linux-gnu Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu Known to work:
Known to fail: Last reconfirmed:

Description honza 2007-07-10 08:28:20 UTC
Overview:
When using optimisation level -O3 the code generated from attached example is wrong. It is accumalating the stream increment and thus the function readFloat reads twice the same number and then it skips two in a row.
Problem seems to be in this line 
    const float val = *(const float *&)stream;
when I change it to 
    const float val = *(const float *)stream;
it starts working even on -O3.

Steps to reproduce:
Compile this code with -O3.

#include <stdio.h>
float readFloat(const char *&stream)
{
    const float val = *(const float *&)stream;
    stream += sizeof(float);
    return val;
}
int main(int argc, char **argv)
{
    float stream[] = { 2.0f,
        1.0f, 2.0f,
        3.0f, 4.0f,
        5.0f, 6.0f };
    const char *stream2 = (const char *) stream;

    for (float i = 0, n = readFloat(stream2); i <= n; i += 1.0f) {
        const float x = readFloat(stream2);
        const float y = readFloat(stream2);
        printf("%f,%f\n", x,y);
    }
    return 0;
}

Actual results:
Prints:
1.000000,1.000000
3.000000,3.000000
5.000000,5.000000
Expected results:
to print:
1.000000,2.000000
3.000000,4.000000
5.000000,6.000000

Build date & platform:
gcc (GCC) 4.1.2 20070626 (Red Hat 4.1.2-13)

Additional Builds and Platforms:
gcc (GCC) 4.0.2
gcc (GCC) 4.1.1
gcc (GCC) 4.2.0

This bug is not reproducible on gcc 3.3 series.
Comment 1 Richard Biener 2007-07-10 09:23:39 UTC
You violate C/C++ aliasing rules by reading/storing a value of type (char *) to
a memory location of type (float *).

*** This bug has been marked as a duplicate of 21920 ***