I've found a case where it appears that the optimizer has reordered some code
surrounding the initialization of a local constant array, stomping on some of
the elements after they have been loaded with the correct values. Here's a
snippet of the source and resulting assembly that illustrates the problem (these
are pulled from the complete source which is attached):
// C++
586: {
587: const byte encrypted[] = { // from Eric Young's libdes
588: 0xf3,0x09,0x62,0x49,0xc7,0xf4,0x6e,0x51,
589: 0x35,0xf2,0x4a,0x24,0x2e,0xeb,0x3d,0x3f,
590: 0x3d,0x6d,0x5b,0xe3,0x25,0x5a,0xf8,0xc3};
...
}
// ASM
//
// It appears that the local array "encrypted" is at offset 0xffffff08 from
// %ebp. We dutifully load the first byte (0xf3) and then do some other stuff
// apparently using that same location for scratch and then proceed with
// the rest of the array initialization
//
2cab: c6 85 08 ff ff ff f3 movb $0xf3,0xffffff08(%ebp)
2cb2: ba 08 00 00 00 mov $0x8,%edx
2cb7: b9 08 00 00 00 mov $0x8,%ecx
2cbc: 89 95 08 ff ff ff mov %edx,0xffffff08(%ebp)
2cc2: 31 c0 xor %eax,%eax
2cc4: 8d 55 b8 lea 0xffffffb8(%ebp),%edx
2cc7: 89 8d 28 ff ff ff mov %ecx,0xffffff28(%ebp)
2ccd: 8d 8d 30 ff ff ff lea 0xffffff30(%ebp),%ecx
2cd3: c6 85 09 ff ff ff 09 movb $0x9,0xffffff09(%ebp)
2cda: c6 85 0a ff ff ff 62 movb $0x62,0xffffff0a(%ebp)
2ce1: c6 85 0b ff ff ff 49 movb $0x49,0xffffff0b(%ebp)
2ce8: c6 85 0c ff ff ff c7 movb $0xc7,0xffffff0c(%ebp)
2cef: c6 85 0d ff ff ff f4 movb $0xf4,0xffffff0d(%ebp)
2cf6: c6 85 0e ff ff ff 6e movb $0x6e,0xffffff0e(%ebp)
2cfd: c6 85 0f ff ff ff 51 movb $0x51,0xffffff0f(%ebp)
2d04: c6 85 10 ff ff ff 35 movb $0x35,0xffffff10(%ebp)
2d0b: c6 85 11 ff ff ff f2 movb $0xf2,0xffffff11(%ebp)
2d12: c6 85 12 ff ff ff 4a movb $0x4a,0xffffff12(%ebp)
2d19: c6 85 13 ff ff ff 24 movb $0x24,0xffffff13(%ebp)
2d20: c6 85 14 ff ff ff 2e movb $0x2e,0xffffff14(%ebp)
2d27: c6 85 15 ff ff ff eb movb $0xeb,0xffffff15(%ebp)
2d2e: c6 85 16 ff ff ff 3d movb $0x3d,0xffffff16(%ebp)
2d35: c6 85 17 ff ff ff 3f movb $0x3f,0xffffff17(%ebp)
2d3c: c6 85 18 ff ff ff 3d movb $0x3d,0xffffff18(%ebp)
2d43: c6 85 19 ff ff ff 6d movb $0x6d,0xffffff19(%ebp)
2d4a: c6 85 1a ff ff ff 5b movb $0x5b,0xffffff1a(%ebp)
2d51: c6 85 1b ff ff ff e3 movb $0xe3,0xffffff1b(%ebp)
2d58: c6 85 1c ff ff ff 25 movb $0x25,0xffffff1c(%ebp)
2d5f: c6 85 1d ff ff ff 5a movb $0x5a,0xffffff1d(%ebp)
2d66: c6 85 1e ff ff ff f8 movb $0xf8,0xffffff1e(%ebp)
2d6d: c6 85 1f ff ff ff c3 movb $0xc3,0xffffff1f(%ebp)
Here's the gcc -v output for the offending source file:
/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/g++ -v -save-temps -g -O3 -pipe
-fno-strict-aliasing -ansi -pthread -fexceptions -D_POSIX_C_SOURCE=199506L
-D_FILE_OFFSET_BITS=64 -gstabs+ -Wno-parentheses -Wcast-align -Wno-deprecated
-Werror -DGLNX86 -DSIMULINK_V2 -DINTERNATIONALIZE=1 -I. -I../include
-I../../include -c -o /tmp/validat1.o validat1.cpp
g++: warning: -pipe ignored because -save-temps specified
Reading specs from
/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../lib/gcc/i686-pc-linux-gnu/3.4.4/specs
Configured with: /home/cnewbold/src/gnu/gcc-3.4.4/configure
--enable-__cxa_atexit --prefix=/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4
Thread model: posix
gcc version 3.4.4
/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../libexec/gcc/i686-pc-linux-gnu/3.4.4/cc1plus
-E -quiet -v -I. -I../include -I../../include -iprefix
/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../lib/gcc/i686-pc-linux-gnu/3.4.4/
-D_GNU_SOURCE -D_REENTRANT -D_POSIX_C_SOURCE=199506L -D_FILE_OFFSET_BITS=64
-DGLNX86 -DSIMULINK_V2 -DINTERNATIONALIZE=1 validat1.cpp -mtune=pentiumpro -ansi
-Wno-parentheses -Wcast-align -Wno-deprecated -Werror -fno-strict-aliasing
-fexceptions -fworking-directory -O3 -o validat1.ii
ignoring nonexistent directory
"/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../lib/gcc/i686-pc-linux-gnu/3.4.4/../../../../i686-pc-linux-gnu/include"
ignoring duplicate directory
"/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/lib/gcc/i686-pc-linux-gnu/3.4.4/../../../../include/c++/3.4.4"
ignoring duplicate directory
"/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/lib/gcc/i686-pc-linux-gnu/3.4.4/../../../../include/c++/3.4.4/i686-pc-linux-gnu"
ignoring duplicate directory
"/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/lib/gcc/i686-pc-linux-gnu/3.4.4/../../../../include/c++/3.4.4/backward"
ignoring duplicate directory
"/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/lib/gcc/i686-pc-linux-gnu/3.4.4/include"
ignoring nonexistent directory
"/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/lib/gcc/i686-pc-linux-gnu/3.4.4/../../../../i686-pc-linux-gnu/include"
ignoring nonexistent directory "../include"
ignoring nonexistent directory "../../include"
#include "..." search starts here:
#include <...> search starts here:
.
/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../lib/gcc/i686-pc-linux-gnu/3.4.4/../../../../include/c++/3.4.4
/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../lib/gcc/i686-pc-linux-gnu/3.4.4/../../../../include/c++/3.4.4/i686-pc-linux-gnu
/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../lib/gcc/i686-pc-linux-gnu/3.4.4/../../../../include/c++/3.4.4/backward
/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../lib/gcc/i686-pc-linux-gnu/3.4.4/include
/usr/local/include
/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/include
/usr/include
End of search list.
/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../libexec/gcc/i686-pc-linux-gnu/3.4.4/cc1plus
-fpreprocessed validat1.ii -quiet -dumpbase validat1.cpp -mtune=pentiumpro -ansi
-auxbase-strip /tmp/validat1.o -g -gstabs+ -O3 -Wno-parentheses -Wcast-align
-Wno-deprecated -Werror -ansi -version -fno-strict-aliasing -fexceptions -o
validat1.s
GNU C++ version 3.4.4 (i686-pc-linux-gnu)
compiled by GNU C version 3.4.4.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
/mathworks/hub/Linux/glibc-2.3.2/i686/apps/gcc-3.4.4/bin/../lib/gcc/i686-pc-linux-gnu/3.4.4/../../../../i686-pc-linux-gnu/bin/as
-V -Qy -o /tmp/validat1.o validat1.s
GNU assembler version 2.15 (i686-pc-linux-gnu) using BFD version 2.15
--
Summary: Optimization stomps const, initialized local array
Product: gcc
Version: 3.4.4
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: cnewbold at mathworks dot com
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22129