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

            Bug ID: 85244
           Summary: Bad optimisation with flexible array member (may be
                    related to -ftree-dominator-opts)
           Product: gcc
           Version: 8.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jean at pryen dot org
  Target Milestone: ---

Created attachment 43863
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=43863&action=edit
Main program. The companion b.i is not provided but inlined

Since gcc-6 (specific version bellow), there is a bad behavior with -O1 (and
better) in the provided program that I tried to make it minimal.

[A]. `gcc-8 -O1` cause the issue 
[B]. `gcc-6 -O1` also has the issue
[C]. `gcc-5 -O1` was OK
[D]. `gcc-8 -O1 -fno-tree-dominator-opts` does not have the issue


When diffing the generated assembly code between good [D] and bad [A], it seems
that the value `val.tab[idx]` is reused as is from a saved value of
`val.tab[0]`, i.e. without considering the value of `idx`.

--- bad/m.s     2018-04-05 21:07:11.602154334 -0400
+++ good/m.s    2018-04-05 21:07:11.542157334 -0400
@@ -13,11 +13,9 @@
 main:
 .LFB11:
        .cfi_startproc
-       pushq   %rbx
+       subq    $8, %rsp
        .cfi_def_cfa_offset 16
-       .cfi_offset 3, -16
-       movl    12+val(%rip), %ebx
-       movl    %ebx, %esi
+       movl    12+val(%rip), %esi
        leaq    .LC0(%rip), %rdi
        movl    $0, %eax
        call    printf@PLT
@@ -25,13 +23,14 @@
        leaq    .LC1(%rip), %rdi
        movl    $0, %eax
        call    printf@PLT
-       movl    %ebx, %edx
        movq    idx(%rip), %rsi
+       leaq    val(%rip), %rax
+       movl    12(%rax,%rsi,4), %edx
        leaq    .LC2(%rip), %rdi
        movl    $0, %eax
        call    printf@PLT
        movl    $0, %eax
-       popq    %rbx
+       addq    $8, %rsp
        .cfi_def_cfa_offset 8
        ret
        .cfi_endproc


Note that the following changes in the source also cause the issue (and the
diff in m.s) to disappear:

* removing `const` from the `extern const struct s val`
* removing or changing the types of `s.a` and `s.b`
* defining the const `val` inside the source file

---

Compilation line:

gcc-8 -v ../m.c ../b.c -O1 -save-temps

Output:

Using built-in specs.
COLLECT_GCC=gcc-8
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 8-20180321-1'
--with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-8
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie
--with-system-zlib --with-target-system-zlib --enable-objc-gc=auto
--enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 8.0.1 20180321 (experimental) [trunk revision 258712] (Debian
8-20180321-1) 
COLLECT_GCC_OPTIONS='-v' '-O1' '-save-temps' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/8/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu
../m.c -mtune=generic -march=x86-64 -O1 -fpch-preprocess -o m.i
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/8/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-O1' '-save-temps' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/8/cc1 -fpreprocessed m.i -quiet -dumpbase m.c
-mtune=generic -march=x86-64 -auxbase m -O1 -version -o m.s
GNU C17 (Debian 8-20180321-1) version 8.0.1 20180321 (experimental) [trunk
revision 258712] (x86_64-linux-gnu)
        compiled by GNU C version 8.0.1 20180321 (experimental) [trunk revision
258712], GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version
isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C17 (Debian 8-20180321-1) version 8.0.1 20180321 (experimental) [trunk
revision 258712] (x86_64-linux-gnu)
        compiled by GNU C version 8.0.1 20180321 (experimental) [trunk revision
258712], GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version
isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9ce40b2d6acfd4e504e10342a20ef051
COLLECT_GCC_OPTIONS='-v' '-O1' '-save-temps' '-mtune=generic' '-march=x86-64'
 as -v --64 -o m.o m.s
Version de l'assembleur GNU 2.30 (x86_64-linux-gnu) utilisant la version BFD
(GNU Binutils for Debian) 2.30
COLLECT_GCC_OPTIONS='-v' '-O1' '-save-temps' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/8/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu
../b.c -mtune=generic -march=x86-64 -O1 -fpch-preprocess -o b.i
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/8/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-O1' '-save-temps' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/8/cc1 -fpreprocessed b.i -quiet -dumpbase b.c
-mtune=generic -march=x86-64 -auxbase b -O1 -version -o b.s
GNU C17 (Debian 8-20180321-1) version 8.0.1 20180321 (experimental) [trunk
revision 258712] (x86_64-linux-gnu)
        compiled by GNU C version 8.0.1 20180321 (experimental) [trunk revision
258712], GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version
isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C17 (Debian 8-20180321-1) version 8.0.1 20180321 (experimental) [trunk
revision 258712] (x86_64-linux-gnu)
        compiled by GNU C version 8.0.1 20180321 (experimental) [trunk revision
258712], GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version
isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9ce40b2d6acfd4e504e10342a20ef051
COLLECT_GCC_OPTIONS='-v' '-O1' '-save-temps' '-mtune=generic' '-march=x86-64'
 as -v --64 -o b.o b.s
Version de l'assembleur GNU 2.30 (x86_64-linux-gnu) utilisant la version BFD
(GNU Binutils for Debian) 2.30
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/8/:/usr/lib/gcc/x86_64-linux-gnu/8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/8/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/8/:/usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-O1' '-save-temps' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/8/collect2 -plugin
/usr/lib/gcc/x86_64-linux-gnu/8/liblto_plugin.so
-plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
-plugin-opt=-fresolution=m.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 --sysroot=/
--build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker
/lib64/ld-linux-x86-64.so.2 -pie
/usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o
/usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/8/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/8
-L/usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu
-L/usr/lib/gcc/x86_64-linux-gnu/8/../../../../lib -L/lib/x86_64-linux-gnu
-L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib
-L/usr/lib/gcc/x86_64-linux-gnu/8/../../.. m.o b.o -lgcc --as-needed -lgcc_s
--no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed
/usr/lib/gcc/x86_64-linux-gnu/8/crtendS.o
/usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-v' '-O1' '-save-temps' '-mtune=generic' '-march=x86-64'

What is displayed when executing the final program:

ok val.tab[0] == 42
ok val.tab[1] == 1337
KO val.tab[1] == 42

What is expected:

ok val.tab[0] == 42
ok val.tab[1] == 1337
KO val.tab[1] == 1337

Companion b.i file (only needed to build the executable and see the behavior)

# 1 "b.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "b.c"
# 1 "t.h" 1
struct s {
 long a;
 int b;
 int tab[];
};

extern const struct s val;
extern int idx;
# 2 "b.c" 2

int idx = 1;
const struct s val = {
 0,
 0,
 {
  42,
  1337
 }
};

Reply via email to