https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65918

            Bug ID: 65918
           Summary: Optimized code (> -O0) on 2-dim array iteration
                    incorrect
           Product: gcc
           Version: 5.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: habanero_pizza at yahoo dot com
  Target Milestone: ---
              Host: i686-pc-linux-gnu
            Target: i686-pc-linux-gnu
             Build: i686-pc-linux-gnu

Created attachment 35413
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35413&action=edit
Test case source:  execution fails if OKAY is not defined...

When the following function is compiled with gcc 4.8.3, 4.9.2, or 5.1.0, it
only executes properly when no optimization is specified (-O0):

int f(int value, int type)
{
    static const int limits[2][3] = {
        { 400, 480, 512 },
        { 460, 492, 500 }
    };

    int retval = 0;

    while ((value > limits[type][retval]) && (retval < 3)) {
        retval++;
    }

    return retval;
}

The problem is in the case where retval should be 3; for example, if value is
550 and type is 0, the retval should be 3, but the function will return 2.  For
cases where the function is supposed to return 0, 1, or 2, it does work
correctly.

However, if gcc 4.6.4 or gcc 4.7.2 is used, it works correctly with and without
optimization.

If the loop is changed to use an array of a single dimension, the problem is
also eliminated (4.8, 4.9, 5.1):

    const int *limit = limits[type];

    while ((value > limit[retval]) && (retval < 3)) {
        retval++;
    }

I have also confirmed that the problem appears for x86_64 and ARM
architectures.

See attached file (test-opt-2-dim-array-iter.c) for further information.

Compilation details:

Using built-in specs.
COLLECT_GCC=i386-eabi-gcc
COLLECT_LTO_WRAPPER=/usr/local/i386-eabi-5.1.0/libexec/gcc/i686-pc-linux-gnu/5.1.0/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: ../../gcc-5.1.0/configure --prefix=/usr/local/i386-eabi-5.1.0
--program-prefix=i386-eabi- --enable-languages=c,c++
--with-gmp=/usr/local/i386-eabi-5.1.0 --with-mpfr=/usr/local/i386-eabi-5.1.0
--with-mpc=/usr/local/i386-eabi-5.1.0 --with-isl=/usr/local/i386-eabi-5.1.0
--with-zlib=/usr/local --enable-lto --enable-gold
Thread model: posix
gcc version 5.1.0 (GCC) 
COLLECT_GCC_OPTIONS='-O1' '-v' '-save-temps' '-o' 'test-opt-2-dim-array-iter'
'-mtune=generic' '-march=pentiumpro'
 /usr/local/i386-eabi-5.1.0/libexec/gcc/i686-pc-linux-gnu/5.1.0/cc1 -E -quiet
-v test-opt-2-dim-array-iter.c -mtune=generic -march=pentiumpro -O1
-fpch-preprocess -o test-opt-2-dim-array-iter.i
ignoring nonexistent directory
"/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/../../../../i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/include
 /usr/local/include
 /usr/local/i386-eabi-5.1.0/include
 /usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-O1' '-v' '-save-temps' '-o' 'test-opt-2-dim-array-iter'
'-mtune=generic' '-march=pentiumpro'
 /usr/local/i386-eabi-5.1.0/libexec/gcc/i686-pc-linux-gnu/5.1.0/cc1
-fpreprocessed test-opt-2-dim-array-iter.i -quiet -dumpbase
test-opt-2-dim-array-iter.c -mtune=generic -march=pentiumpro -auxbase
test-opt-2-dim-array-iter -O1 -version -o test-opt-2-dim-array-iter.s
GNU C11 (GCC) version 5.1.0 (i686-pc-linux-gnu)
        compiled by GNU C version 5.1.0, GMP version 5.1.3, MPFR version 3.1.2,
MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C11 (GCC) version 5.1.0 (i686-pc-linux-gnu)
        compiled by GNU C version 5.1.0, GMP version 5.1.3, MPFR version 3.1.2,
MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: d06407f4e6fff3c174315d9bcdf98d85
COLLECT_GCC_OPTIONS='-O1' '-v' '-save-temps' '-o' 'test-opt-2-dim-array-iter'
'-mtune=generic' '-march=pentiumpro'

/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/../../../../i686-pc-linux-gnu/bin/as
-v --32 -o test-opt-2-dim-array-iter.o test-opt-2-dim-array-iter.s
GNU assembler version 2.25 (i686-pc-linux-gnu) using BFD version (GNU Binutils)
2.25
COMPILER_PATH=/usr/local/i386-eabi-5.1.0/libexec/gcc/i686-pc-linux-gnu/5.1.0/:/usr/local/i386-eabi-5.1.0/libexec/gcc/i686-pc-linux-gnu/5.1.0/:/usr/local/i386-eabi-5.1.0/libexec/gcc/i686-pc-linux-gnu/:/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/:/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/:/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/../../../../i686-pc-linux-gnu/bin/
LIBRARY_PATH=/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/:/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-O1' '-v' '-save-temps' '-o' 'test-opt-2-dim-array-iter'
'-mtune=generic' '-march=pentiumpro'
 /usr/local/i386-eabi-5.1.0/libexec/gcc/i686-pc-linux-gnu/5.1.0/collect2
-plugin
/usr/local/i386-eabi-5.1.0/libexec/gcc/i686-pc-linux-gnu/5.1.0/liblto_plugin.so
-plugin-opt=/usr/local/i386-eabi-5.1.0/libexec/gcc/i686-pc-linux-gnu/5.1.0/lto-wrapper
-plugin-opt=-fresolution=test-opt-2-dim-array-iter.res
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc
-plugin-opt=-pass-through=-lgcc_s --eh-frame-hdr -m elf_i386 -dynamic-linker
/lib/ld-linux.so.2 -o test-opt-2-dim-array-iter /usr/lib/crt1.o /usr/lib/crti.o
/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/crtbegin.o
-L/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0
-L/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/../../..
test-opt-2-dim-array-iter.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc
--as-needed -lgcc_s --no-as-needed
/usr/local/i386-eabi-5.1.0/lib/gcc/i686-pc-linux-gnu/5.1.0/crtend.o
/usr/lib/crtn.o

Preprocessed code:

  [Will add as an additional attachment.]

Reply via email to