bebbo / gcc

Bebbo's gcc-6-branch for m68k-amigaos
GNU General Public License v2.0
33 stars 11 forks source link

Wrong arithmetic result with -O2 and -O3 #225

Closed anchor76 closed 7 months ago

anchor76 commented 7 months ago

Hello, i found a very strange behavior with arithmetic shift if -O2 or -O3 is used. Sample code:

`#include

include

include

typedef unsigned int u32; const int _count = 8;

int main(int argc, char argv[]) { u32 _len = _count sizeof(u32); u32 _data = (u32)malloc(_len); memset(_data, 0, _len);

printf("before shift\n");
for (u32 _i = 0; _i < _count; _i++)
{
    printf("i:%d data:%08x\n", _i, _data[_i]);
}

// do shift
for (u32 _i = 0; _i < _count; _i++)
{
    _data[_i] <<= 1;
}

printf("after shift\n");
for (u32 _i = 0; _i < _count; _i++)
{
    printf("i:%d data:%08x\n", _i, _data[_i]);
}

free(_data);

return 0;

}`

The expected output is zero for all elements, but with -O2 -O3 i see random numbers.

Is it possible to switch off optimization features one by one? Which optimization should i turn off? Any help would be appreciated.

(Note: source code, compiled binaries, and asm outputs attached.)

gcc_test.zip

bebbo commented 7 months ago

confirmed. workaround: -fno-peephole2

bebbo commented 7 months ago

fix is live in ~1 hour from now, if test builds succeed.

thanks for reporting!