The current gcc trunk (as well as gcc 4.8 and 4.9) miscompiles the following code on x86_64-linux at -O2 and -O3 in both 32-bit and 64-bit modes. This is a regression from 4.7.x. $ gcc-trunk -v Using built-in specs. COLLECT_GCC=gcc-trunk COLLECT_LTO_WRAPPER=/usr/local/gcc-trunk/libexec/gcc/x86_64-unknown-linux-gnu/4.10.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: ../gcc-trunk/configure --prefix=/usr/local/gcc-trunk --enable-languages=c,c++ --disable-werror --enable-multilib Thread model: posix gcc version 4.10.0 20140601 (experimental) [trunk revision 211110] (GCC) $ $ gcc-trunk -Os small.c; a.out $ gcc-4.7.3 -O2 small.c; a.out $ $ gcc-trunk -O2 small.c; a.out Floating point exception (core dumped) $ gcc-4.8.2 -O2 small.c; a.out Floating point exception (core dumped) $ ------------------------------ int a, b = 1, c, d, e, f, g; int fn1 () { int h; for (;;) { g = b; g = g ? 0 : 1 % g; e = a + 1; for (; d < 1; d = e) { h = f == 0 ? 0 : 1 % f; if (f < 1) c = 0; else if (h) break; } if (b) return 0; } } int main () { fn1 (); return 0; }
Confirmed.
Started with r190184.
This very minor variant also misbehaves with 4.7 and 4.6, so before my patch. An unsafe instruction (1%f) is taken out of a branch in ifcombine. int a, b = 1, c, d, e, f, g; int fn1 () { int h; for (;;) { g = b; g = g ? 0 : 1 % g; e = a + 1; for (; d < 1; d = e) { if (f == 0) h = 0; else h = 1 % f; if (f < 1) c = 0; else if (h) break; } if (b) return 0; } } int main () { fn1 (); return 0; }
There is similar issue in RTL PRE GCSE [1], PR45223.
(In reply to Marc Glisse from comment #3) > This very minor variant also misbehaves with 4.7 and 4.6, so before my > patch. An unsafe instruction (1%f) is taken out of a branch in ifcombine. > > int a, b = 1, c, d, e, f, g; > > int > fn1 () > { > int h; > for (;;) > { > g = b; > g = g ? 0 : 1 % g; > e = a + 1; > for (; d < 1; d = e) > { > if (f == 0) > h = 0; > else > h = 1 % f; > if (f < 1) > c = 0; > else if (h) > break; > } > if (b) > return 0; > } > } > > int > main () > { > fn1 (); > return 0; > } I have a patch in testing. (and fixed the tree PRE issue in the past...)
> There is similar issue in RTL PRE GCSE [1], PR45223. Right, there are various similar old issues in the RTL optimizers, but we must be extra careful because we really don't want to pessimize the common case to fix some artificial testcases. Note that we have some backup here: the (green) Dragon Book gives a code motion algorithm that is subject to this kind of issues and the authors elegantly write "For this reason, it is wise to use the following algorithm for code motion only if optimization may be inhibited by the programmer". :-)
Author: rguenth Date: Tue Jun 3 08:48:28 2014 New Revision: 211163 URL: http://gcc.gnu.org/viewcvs?rev=211163&root=gcc&view=rev Log: 2014-06-03 Richard Biener <rguenther@suse.de> PR tree-optimization/61383 * tree-ssa-ifcombine.c (bb_no_side_effects_p): Make sure stmts can't trap. * gcc.dg/torture/pr61383-1.c: New testcase. Added: trunk/gcc/testsuite/gcc.dg/torture/pr61383-1.c Modified: trunk/gcc/ChangeLog trunk/gcc/testsuite/ChangeLog trunk/gcc/tree-ssa-ifcombine.c
Author: rguenth Date: Wed Jun 4 13:40:33 2014 New Revision: 211231 URL: http://gcc.gnu.org/viewcvs?rev=211231&root=gcc&view=rev Log: 2014-06-04 Richard Biener <rguenther@suse.de> PR tree-optimization/61383 * tree-ssa-ifcombine.c (bb_no_side_effects_p): Make sure stmts can't trap. * gcc.dg/torture/pr61383-1.c: New testcase. Added: branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/torture/pr61383-1.c Modified: branches/gcc-4_9-branch/gcc/ChangeLog branches/gcc-4_9-branch/gcc/testsuite/ChangeLog branches/gcc-4_9-branch/gcc/tree-ssa-ifcombine.c
Author: rguenth Date: Wed Jun 4 13:41:09 2014 New Revision: 211232 URL: http://gcc.gnu.org/viewcvs?rev=211232&root=gcc&view=rev Log: 2014-06-04 Richard Biener <rguenther@suse.de> PR tree-optimization/61383 * tree-ssa-ifcombine.c (bb_no_side_effects_p): Make sure stmts can't trap. * gcc.dg/torture/pr61383-1.c: New testcase. Added: branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/torture/pr61383-1.c Modified: branches/gcc-4_8-branch/gcc/ChangeLog branches/gcc-4_8-branch/gcc/testsuite/ChangeLog branches/gcc-4_8-branch/gcc/tree-ssa-ifcombine.c
Fixed.