Bug 61383 - [4.8/4.9/4.10 Regression] wrong code at -O2 and -O3 on x86_64-linux-gnu
Summary: [4.8/4.9/4.10 Regression] wrong code at -O2 and -O3 on x86_64-linux-gnu
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: unknown
: P3 normal
Target Milestone: 4.8.4
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2014-06-01 22:00 UTC by Zhendong Su
Modified: 2014-06-04 13:41 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.10.0, 4.7.3, 4.8.4, 4.9.1
Known to fail: 4.8.3, 4.9.0
Last reconfirmed: 2014-06-02 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Zhendong Su 2014-06-01 22:00:19 UTC
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;
}
Comment 1 Richard Biener 2014-06-02 08:10:30 UTC
Confirmed.
Comment 2 Jakub Jelinek 2014-06-02 08:44:22 UTC
Started with r190184.
Comment 3 Marc Glisse 2014-06-02 13:19:18 UTC
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;
}
Comment 4 Uroš Bizjak 2014-06-02 13:37:41 UTC
There is similar issue in RTL PRE GCSE [1], PR45223.
Comment 5 Richard Biener 2014-06-02 14:02:05 UTC
(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...)
Comment 6 Eric Botcazou 2014-06-02 20:59:03 UTC
> 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". :-)
Comment 7 Richard Biener 2014-06-03 08:48:59 UTC
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
Comment 8 Richard Biener 2014-06-04 13:41:05 UTC
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
Comment 9 Richard Biener 2014-06-04 13:41:41 UTC
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
Comment 10 Richard Biener 2014-06-04 13:41:53 UTC
Fixed.