NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

toolchain/40066: gcc -O1 generates wrong code in loop optimize



>Number:         40066
>Category:       toolchain
>Synopsis:       gcc -O1 generates wrong code in loop optimize
>Confidential:   no
>Severity:       critical
>Priority:       medium
>Responsible:    toolchain-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Nov 29 14:45:00 +0000 2008
>Originator:     Izumi Tsutsui
>Release:        NetBSD 5.0_BETA
>Organization:
>Environment:
System: NetBSD 5.0_BETA i386
Architecture: i386
Machine: i386
also happens on m68k

>Description:
As described in bin/40036, gcc(1) generates wrong code around
an assignment of structure with union in nested loops
(like src/sbin/ifconfig.c:init_afs() ) if -O1 is specified.

This doesn't happens if -O2 or -fno-loop-optimize is specfied (see below).

>How-To-Repeat:

% uname -mrs
NetBSD 5.0_BETA i386
% gcc --version
gcc (GCC) 4.1.3 20080704 prerelease (NetBSD nb1 20080202)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

% cat test.c
#include <inttypes.h>
#include <stdio.h>

struct kwinst {
        int                     k_type;
        const char              *k_word;
        union kwval {
                int64_t         u_sint;
                char            *u_str;
        } k_u;
#define k_int   k_u.u_sint
};

struct kwinst fkw[10];
int num[] = { 11, 22, 33, 44 };
char *name[] = {"aaaaaa","bbbbbb","cccccc","dddddd"};

main()
{
        int a, i;
        struct kwinst kw = {.k_type = 1};

        for (a = 0; a < 4; a++) {
                kw.k_int = num[a];
                kw.k_word = name[a];
                for (i = 0; i < 10; i++) {
                        if (fkw[i].k_word == NULL) {
                                fkw[i] = kw;
                                break;
                        }
                }
                printf("a=%d: kw.k_word=%s kw.k_int=%d, "
                    "fkw[%d].k_word=%s fkw[%d].k_int=%d\n", a,
                    kw.k_word, (int)kw.k_int,
                    i, fkw[i].k_word, i, (int)fkw[i].k_int);
        }
}

% cc -O test.c
% ./a.out
a=0: kw.k_word=aaaaaa kw.k_int=11, fkw[0].k_word=(null) fkw[0].k_int=11
a=1: kw.k_word=bbbbbb kw.k_int=22, fkw[0].k_word=aaaaaa fkw[0].k_int=22
a=2: kw.k_word=cccccc kw.k_int=33, fkw[1].k_word=bbbbbb fkw[1].k_int=33
a=3: kw.k_word=dddddd kw.k_int=44, fkw[2].k_word=cccccc fkw[2].k_int=44
% cc -O -fno-loop-optimize test.c
% ./a.out
a=0: kw.k_word=aaaaaa kw.k_int=11, fkw[0].k_word=aaaaaa fkw[0].k_int=11
a=1: kw.k_word=bbbbbb kw.k_int=22, fkw[1].k_word=bbbbbb fkw[1].k_int=22
a=2: kw.k_word=cccccc kw.k_int=33, fkw[2].k_word=cccccc fkw[2].k_int=33
a=3: kw.k_word=dddddd kw.k_int=44, fkw[3].k_word=dddddd fkw[3].k_int=44
% cc -O2 test.c
% ./a.out
a=0: kw.k_word=aaaaaa kw.k_int=11, fkw[0].k_word=aaaaaa fkw[0].k_int=11
a=1: kw.k_word=bbbbbb kw.k_int=22, fkw[1].k_word=bbbbbb fkw[1].k_int=22
a=2: kw.k_word=cccccc kw.k_int=33, fkw[2].k_word=cccccc fkw[2].k_int=33
a=3: kw.k_word=dddddd kw.k_int=44, fkw[3].k_word=dddddd fkw[3].k_int=44
% 

>Fix:
Unknown.  Workaround is -O2 or -fno-loop-optimize.



Home | Main Index | Thread Index | Old Index