[Bug tree-optimization/66875] New: entire for loop got eliminated at cddce2 tree-opt stage

2015-07-15 Thread patricksung at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66875

Bug ID: 66875
   Summary: entire for loop got eliminated at cddce2 tree-opt
stage
   Product: gcc
   Version: 4.9.2
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: tree-optimization
  Assignee: unassigned at gcc dot gnu.org
  Reporter: patricksung at gmail dot com
  Target Milestone: ---

Created attachment 35985
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35985&action=edit
Intermediate of the C source

Hi the attached code in the parse_peppi_protocol() (which got inline into
parse()) with the for loop (and nearby code) from line 10821 to line 10995 got
eliminated.

We found out this problem starting with GCC version 4.9.2 and subsequently also
tested with GCC 4.9.3 and 5.1 also having this problem.

One of the following two ways will work around this problem,

1) -fno-aggressive-loop-optimizations (obvious?)
2) change the for loop code from "for (...; i++)" to "for (...; --i)", i.e. the
loop search backwards.

We also found that the code elimination was happening at the "cddce2" stage of
the tree optimization. Here is the command line used with the -v output (with
compiler warnings removed that I believe is irrelevant to this problem to make
the output a little bit cleaner here)

x86_64-peplink-linux-gnu-gcc -D_LARGEFILE_SOURCE=1 -D_LARGE_FILES
-D_FILE_OFFSET_BITS=64 -D_REENTRANT   -DXTABLES_LIBDIR=\"/usr/lib/xtables\"
-DXTABLES_INTERNAL -I../include -I.. -I../include
-I/home/psung/tmp/fw.63.ndpi-test/staging/usr/include
-I/home/psung/tmp/fw.63.ndpi-test/staging/usr/include
-Wp,-MMD,./.libxt_peppi.oo.d,-MT,libxt_peppi.oo -Wall -Waggregate-return
-Wmissing-declarations-Wmissing-prototypes -Wredundant-decls -Wshadow
-Wstrict-prototypes -Winline -pipe -D_INIT=libxt_peppi_init -DPIC -fPIC
-Wextra -g -O3 -o libxt_peppi.oo -c libxt_peppi.c -dp -save-temps
-fdump-tree-cddce2-details-stats -fno-strict-aliasing -fwrapv -v
x86_64-peplink-linux-gnu-gcc: warning: -pipe ignored because -save-temps
specified
Using built-in specs.
COLLECT_GCC=x86_64-peplink-linux-gnu-gcc
Target: x86_64-peplink-linux-gnu
Configured with:
/home/psung/toolchains-2015/x64-4.9.3/.build/src/gcc-4.9.3/configure
--build=x86_64-build_unknown-linux-gnu --host=x86_64-build_unknown-linux-gnu
--target=x86_64-peplink-linux-gnu
--prefix=/home/psung/x-tools-2015/x86_64-peplink-linux-gnu
--with-sysroot=/home/psung/x-tools-2015/x86_64-peplink-linux-gnu/x86_64-peplink-linux-gnu/sysroot
--enable-languages=c,c++,go --with-pkgversion='crosstool-NG
crosstool-ng-1.21.0-50-g488f7a8' --enable-__cxa_atexit --disable-libmudflap
--disable-libgomp --disable-libssp --disable-libquadmath
--disable-libquadmath-support --disable-libsanitizer
--with-gmp=/home/psung/toolchains-2015/x64-4.9.3/.build/x86_64-peplink-linux-gnu/buildtools
--with-mpfr=/home/psung/toolchains-2015/x64-4.9.3/.build/x86_64-peplink-linux-gnu/buildtools
--with-mpc=/home/psung/toolchains-2015/x64-4.9.3/.build/x86_64-peplink-linux-gnu/buildtools
--with-ppl=no --with-isl=no --with-cloog=no
--with-libelf=/home/psung/toolchains-2015/x64-4.9.3/.build/x86_64-peplink-linux-gnu/buildtools
--with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm'
--enable-threads=posix --enable-gold --disable-nls --disable-multilib
--with-local-prefix=/home/psung/x-tools-2015/x86_64-peplink-linux-gnu/x86_64-peplink-linux-gnu/sysroot
--enable-long-long
Thread model: posix
gcc version 4.9.3 (crosstool-NG crosstool-ng-1.21.0-50-g488f7a8)
COLLECT_GCC_OPTIONS='-D' '_LARGEFILE_SOURCE=1' '-D' '_LARGE_FILES' '-D'
'_FILE_OFFSET_BITS=64' '-D' '_REENTRANT' '-D'
'XTABLES_LIBDIR="/usr/lib/xtables"' '-D' 'XTABLES_INTERNAL' '-I' '../include'
'-I' '..' '-I' '../include' '-I'
'/home/psung/tmp/fw.63.ndpi-test/staging/usr/include' '-I'
'/home/psung/tmp/fw.63.ndpi-test/staging/usr/include' '-Wall'
'-Waggregate-return' '-Wmissing-declarations' '-Wmissing-prototypes'
'-Wredundant-decls' '-Wshadow' '-Wstrict-prototypes' '-Winline' '-pipe' '-D'
'_INIT=libxt_peppi_init' '-D' 'PIC' '-fPIC' '-Wextra' '-g' '-O3' '-o'
'libxt_peppi.oo' '-c' '-dp' '-save-temps' '-fdump-tree-cddce2-details-stats'
'-fno-strict-aliasing' '-fwrapv' '-v' '-mtune=generic' '-march=x86-64'

/home/psung/x-tools-2015/x86_64-peplink-linux-gnu/libexec/gcc/x86_64-peplink-linux-gnu/4.9.3/cc1
-E -quiet -v -I .

[Bug tree-optimization/66875] entire for loop got eliminated at cddce2 tree-opt stage

2015-07-15 Thread patricksung at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66875

--- Comment #3 from Patrick S.  ---
argh...

my mistake to not check the termination condition is actually matching the size
of the prot_short_str[] array.  fixing that by properly calculating the array
size would fix the compilation now. Thanks!

But one more thing. Since the compiler can see the static size of the array,
wouldn't it be possible to have a warning for possible access of array size out
of bound?


[Bug tree-optimization/66875] entire for loop got eliminated at cddce2 tree-opt stage

2015-07-15 Thread patricksung at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66875

--- Comment #4 from Patrick S.  ---
(In reply to Marek Polacek from comment #2)
> Yes, please try -fsanitize=undefined.  This looks like an invalid.

I am guessing this would allow the compiler to detect the loop is undefined and
would generate warning? if in that case no new warning/error was generated.