Bug 49915 - Function call with 2-D arrays and -O2 (or strict-aliasing and inlining) gives random results
Summary: Function call with 2-D arrays and -O2 (or strict-aliasing and inlining) gives...
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-30 18:33 UTC by Joseph Quinsey
Modified: 2011-07-30 19:24 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
test.i from gcc -v -save-temps -O2 test.c (234 bytes, application/octet-stream)
2011-07-30 18:33 UTC, Joseph Quinsey
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph Quinsey 2011-07-30 18:33:23 UTC
Created attachment 24872 [details]
test.i from gcc -v -save-temps -O2 test.c

A function call with two-dimensional arrays and -O2 (or the switches -fstrict-aliasing and -finline) gives random results.

Complete source code (14 lines):

static void copy(int n, int a[][n], int b[][n]) {
   int i, j;
   for (i = 0; i < 2; i++)    // 'n' not used in this example
      for (j = 0; j < 2; j++) // 'n' hard-coded to 2 for simplicity
         b[i][j] = a[i][j];
}

int main(int argc, char *argv[]) {
   int a[2][2] = {{1, 2},{3, 4}};
   int b[2][2];
   copy(2, a, b);    
   printf("%d %d %d %d\n", b[0][0], b[0][1], b[1][0], b[1][1]);
   return 0;
}

Test results:

gcc -O1 test.c && a.out
1 2 3 4
gcc -O2 test.c && a.out
12173125 -1079787588 -1079787736 13389812
gcc -O1 -fstrict-aliasing -finline test.c && a.out
12173125 -1080040260 -1080040408 13389812

All three tests also produced the (obvious) warning:

test.c: In function 'main':
test.c:12: warning: incompatible implicit declaration of built-in function 'printf'

gcc -v -save-temps -O2 test.c         
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)
 /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -E -quiet -v test.c -mtune=generic -O2 -fpch-preprocess -o test.i
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i386-redhat-linux/4.1.2/include
 /usr/include
End of search list.
 /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -fpreprocessed test.i -quiet -dumpbase test.c -mtune=generic -auxbase test -O2 -version -o test.s
GNU C version 4.1.2 20080704 (Red Hat 4.1.2-50) (i386-redhat-linux)
        compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-50).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: d8d95095eb3c93dae4bed2137d559f95
test.c: In function 'main':
test.c:12: warning: incompatible implicit declaration of built-in function 'printf'
 as -V -Qy -o test.o test.s
GNU assembler version 2.17.50.0.6-14.el5 (i386-redhat-linux) using BFD version 2.17.50.0.6-14.el5 20061020
 /usr/libexec/gcc/i386-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o /usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2/../../.. test.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crtn.o
Comment 1 Mikael Pettersson 2011-07-30 18:56:05 UTC
I can reproduce this error with gcc 4.0.4 and 4.1.2 on i686-linux, but 4.2.4 up to 4.6.1 work, as do 3.4.6/3.3.6/3.2.3/2.95.3.

4.1.2 is no longer supported upstream, please update to 4.4.6 or newer, or report this problem to your vendor (Red Hat?).
Comment 2 Paolo Carlini 2011-07-30 19:24:15 UTC
Let's close this, then.