Bug 45529 - VLA in a loop with a const size causes wrong optimization
Summary: VLA in a loop with a const size causes wrong optimization
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.3.4
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2010-09-03 21:26 UTC by k_satoda
Modified: 2021-09-03 06:19 UTC (History)
1 user (show)

See Also:
Host:
Target: i686-pc-cygwin
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 k_satoda 2010-09-03 21:26:00 UTC
When a variable length array is defined in a loop, and its size is
specified with a const variable, gcc -O3 (and -Os) produces a call of
alloca() without specifying the size argument (eax in my case). This
causes stack overflow.

$ cat test.c
int const n = 4096;
void g(int* p);
void f() { for(;;) { int a[n]; g(a); } }

$ gcc -S -O3 -o - test.c
(snip)
.globl _f
        .def    _f;     .scl    2;      .type   32;     .endef
_f:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %esi
        pushl   %ebx
        subl    $16, %esp
        .p2align 4,,7
L2:
        movl    %esp, %ebx
        call    __alloca
(snip)

$ gcc -v
Using built-in specs.
Target: i686-pc-cygwin
Configured with:
  /gnu/gcc/releases/packaging/4.3.4-3/gcc4-4.3.4-3/src/gcc-4.3.4/configure
  --srcdir=/gnu/gcc/releases/packaging/4.3.4-3/gcc4-4.3.4-3/src/gcc-4.3.4
  --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin
  --libexecdir=/usr/lib --datadir=/usr/share --localstatedir=/var
  --sysconfdir=/etc --infodir=/usr/share/info --mandir=/usr/share/man
  --datadir=/usr/share --infodir=/usr/share/info --mandir=/usr/share/man
  -v --with-gmp=/usr --with-mpfr=/usr --enable-bootstrap
  --enable-version-specific-runtime-libs --with-slibdir=/usr/bin
  --libexecdir=/usr/lib --enable-static --enable-shared
  --enable-shared-libgcc --disable-__cxa_atexit --with-gnu-ld
  --with-gnu-as --with-dwarf2 --disable-sjlj-exceptions
  --enable-languages=ada,c,c++,fortran,java,objc,obj-c++
  --disable-symvers --enable-libjava --program-suffix=-4
  --enable-libgomp --enable-libssp --enable-libada
  --enable-threads=posix --with-arch=i686 --with-tune=generic
  --enable-libgcj-sublibs CC=gcc-4 CXX=g++-4 CC_FOR_TARGET=gcc-4
  CXX_FOR_TARGET=g++-4 GNATMAKE_FOR_TARGET=gnatmake
  GNATBIND_FOR_TARGET=gnatbind AS=/opt/gcc-tools/bin/as.exe
  AS_FOR_TARGET=/opt/gcc-tools/bin/as.exe LD=/opt/gcc-tools/bin/ld.exe
  LD_FOR_TARGET=/opt/gcc-tools/bin/ld.exe
  --with-ecj-jar=/usr/share/java/ecj.jar
Thread model: posix
gcc version 4.3.4 20090804 (release) 1 (GCC)