This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/29406] New: code generation / optimisation bug


I was writing a magic squares program for my sons homework and somewhat
surprised/annoyed that gcc-4.1.1 and 4.0.3 have a horrible code generation bug.
Compiled with -O or -O2 on gcc-4.0.3 the following code infinitely loops.
gcc-4.1.1 with no optimisation works, but -O2 fails. Local variable "n" in
main() is modified by the try function when it cannot be.

# include       <stdio.h>
# include       <string.h>

int     nums[] = {15, 9, 3, 7, 13, 17, 11, 5, 19};

void try_sq(int n);

int main(int argc, char **argv)
{
        int     n;

        for (n = 0; n < 999999999; n++) {
                if ((n % 10000000) == 0)
                        printf("%d\n", n);
                try_sq(n);
                }
        return 0;
}
void try_sq(int n)
{       int     sq[9], sq2[9];
        int     i, s;

        memcpy(sq, nums, sizeof nums);
        for (i = 0; i < 9; i++) {
                int     d = n % 10;
                n /= 10;
                if (sq[d] == 0)
                        return;
                sq2[i] = sq[d];
                sq[d] = 0;
                }
        /***********************************************/
        /*   Rows.                                     */
        /***********************************************/
        s = sq2[0] + sq2[1] + sq2[2];
        if (s != 33)
                return;
        s = sq2[3] + sq2[4] + sq2[5];
        if (s != 33)
                return;
        s = sq2[6] + sq2[7] + sq2[8];
        if (s != 33)
                return;
        /***********************************************/
        /*   Columns.                                  */
        /***********************************************/
        s = sq2[0] + sq2[3] + sq2[6];
        if (s != 33)
                return;
        s = sq2[1] + sq2[4] + sq2[8];
        if (s != 33)
                return;
        s = sq2[2] + sq2[5] + sq2[8];
        if (s != 33)
                return;
        /***********************************************/
        /*   Diagonal.                                 */
        /***********************************************/
        s = sq2[0] + sq2[4] + sq2[8];
        if (s != 33)
                return;
        s = sq2[2] + sq2[4] + sq2[6];
        if (s != 33)
                return;
        printf("%2d %2d %2d\n", sq2[0], sq2[1], sq2[2]);
        printf("%2d %2d %2d\n", sq2[3], sq2[4], sq2[5]);
        printf("%2d %2d %2d\n", sq2[6], sq2[7], sq2[28]);
        printf("\n===\n");
}


-- 
           Summary: code generation / optimisation bug
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: fox at crisp dot demon dot co dot uk
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29406


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]