GCC Bugzilla has been upgraded from version 4.4.9 to 5.0rc3. If you see any problem, please report it to bug 64968.
Bug 34169 - Bad code generated with -O2
Summary: Bad code generated with -O2
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.1.3
: P3 major
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-21 00:06 UTC by Unai Uribarri
Modified: 2007-11-21 00:18 UTC (History)
19 users (show)

See Also:
Host:
Target:
Build:
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 Unai Uribarri 2007-11-21 00:06:04 UTC
$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.1.3 --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)
$ gcc -O2 -w p.c -o p ; ./p
Bad
$ gcc -O2 -fno-tree-pre -w p.c -o p ; ./p
$ gcc -O2 -fno-strict-aliasing -w p.c -o p ; ./p
$ cat p.c
int bmemcmp(void *a1, void *a2, unsigned len)
{
        int dwlen = len / 4;
        while (dwlen)
        {
                int res = *(int *)a1 - *(int *)a2;
                if (res)
                        return res;

                --dwlen;
                ++*(int **)&a1;
                ++*(int **)&a2;
        }
        return 0;
}

int main(int argc, char *argv[])
{
        int *p1 = (int*)calloc(sizeof(int), 1024 * 1024 * 4);
        int *p2 = (int*)calloc(sizeof(int), 1024 * 1024 * 4);
        p1[3] = 1;
        if (bmemcmp(p1, p2, 1024 * 1024 * 4) == 0)
                printf("Bad\n");
        return 0;
}
Comment 1 Andrew Pinski 2007-11-21 00:18:12 UTC
                ++*(int **)&a1;
                ++*(int **)&a2;

you are accessing a void* as an int* which violates C/C++ aliasing rules so you are invoking undefined behavior.

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