Bug 59014 - [4.7 Regression] wrong code at -Os and above on x86_64-linux-gnu
Summary: [4.7 Regression] wrong code at -Os and above on x86_64-linux-gnu
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.9.0
: P2 normal
Target Milestone: 4.7.4
Assignee: Jakub Jelinek
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2013-11-05 20:16 UTC by Zhendong Su
Modified: 2014-05-07 16:21 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work: 4.7.4, 4.8.3, 4.9.0
Known to fail: 4.8.2
Last reconfirmed: 2013-11-06 00:00:00


Attachments
gcc49-pr59014-test.patch (427 bytes, patch)
2013-11-27 14:46 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Zhendong Su 2013-11-05 20:16:56 UTC
The current gcc trunk miscompiles the following code on x86_64-linux-gnu at -Os and above in both 32-bit and 64-bit modes. 

This is a regression from 4.8.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.9.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-trunk/configure --enable-languages=c,c++,objc,obj-c++,fortran,lto --disable-werror --enable-checking=release --with-gmp=/usr/local/gcc-trunk --with-mpfr=/usr/local/gcc-trunk --with-mpc=/usr/local/gcc-trunk --with-cloog=/usr/local/gcc-trunk --prefix=/usr/local/gcc-trunk
Thread model: posix
gcc version 4.9.0 20131105 (experimental) [trunk revision 204388] (GCC) 
$ 
$ gcc-trunk -O1 small.c; a.out
2
$ gcc-4.8.2 -Os small.c; a.out
2
$ gcc-trunk -Os small.c; a.out
0
$ 
$ 


---------------------------------


int printf (const char *, ...);

int a = 2, b, c, d;

int
foo ()
{
  for (;; c++)
    if ((b > 0) | (a & 1))
      ;
    else
      {
	d = a;
	return 0;
      }
}

int
main ()
{
  foo ();
  printf ("%d\n", d);
  return 0;
}
Comment 1 Marek Polacek 2013-11-06 10:39:30 UTC
Confirmed.
Comment 2 Marek Polacek 2013-11-06 10:46:57 UTC
I'd say this is .vrp1.  In .mergephi2, we have

  <bb 4>: 
  a.1_11 = a.1_5;
  d = a.1_11;
  return 0;

but in .vrp1:

  <bb 4>: 
  a.1_13 = 0;
  a.1_11 = 0;
  d = 0;
  return 0;

And yeah, the bug goes away with -fno-tree-vrp.
Comment 3 Marek Polacek 2013-11-06 11:11:24 UTC
And, it's not related to -Os; the bug triggers exactly when VRP is run.
Comment 4 Marek Polacek 2013-11-06 12:03:58 UTC
It seems we create wrong ASSERT_EXPR:

  a.1_5 = a;
  _7 = (_Bool) a.1_5;
  _8 = _4 | _7; 
  if (_8 != 0)
    goto <bb 5>; 
  else
    goto <bb 6>; 

  <bb 6>: 
  a.1_15 = ASSERT_EXPR <a.1_5, a.1_5 == 0>;

Why does it think that a.1_5 must be 0?
Comment 5 Marek Polacek 2013-11-06 21:24:20 UTC
(In reply to Marek Polacek from comment #4)
> Why does it think that a.1_5 must be 0?

That's because VRP is innocent here after all, it's the forwprop1 what does

-  _6 = a.1_5 & 1;
-  _7 = (_Bool) _6;
+  _7 = (_Bool) a.1_5;

More (hopefully) tomorrow...
Comment 6 Marek Polacek 2013-11-07 11:15:24 UTC
      if (TREE_CODE (rhs_def_operand1) == SSA_NAME
          && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand1)
          && TREE_CODE (rhs_def_operand2) == INTEGER_CST
          && operand_equal_p (rhs_def_operand2,
                              build_low_bits_mask (TREE_TYPE (rhs_def_operand2),
                                                   TYPE_PRECISION (lhs_type)),
                                                   0))

in simplify_conversion_from_bitmask in tree-ssa-forwprop.c will need some tweaking
Comment 7 Jakub Jelinek 2013-11-26 21:29:33 UTC
Author: jakub
Date: Tue Nov 26 21:29:30 2013
New Revision: 205417

URL: http://gcc.gnu.org/viewcvs?rev=205417&root=gcc&view=rev
Log:
	PR tree-optimization/59014
	* tree-vrp.c (register_edge_assert_for_1): Don't look
	through conversions from non-integral types or through
	narrowing conversions.

	* gcc.c-torture/execute/pr59014.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr59014.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vrp.c
Comment 8 Jakub Jelinek 2013-11-26 21:31:11 UTC
Fixed.
Comment 9 Jakub Jelinek 2013-11-27 14:45:23 UTC
Reopening, as the bug exists also on 4.7/4.8.
Comment 10 Jakub Jelinek 2013-11-27 14:46:35 UTC
Created attachment 31310 [details]
gcc49-pr59014-test.patch

Testcase that reproduces also on 4.8 and 4.7.  I'll bootstrap/regtest both patches now on 4.8.
Comment 11 Jakub Jelinek 2013-11-27 17:03:28 UTC
Author: jakub
Date: Wed Nov 27 17:03:27 2013
New Revision: 205454

URL: http://gcc.gnu.org/viewcvs?rev=205454&root=gcc&view=rev
Log:
	PR tree-optimization/59014
	* gcc.c-torture/execute/pr59014-2.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr59014-2.c
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 12 Jakub Jelinek 2013-11-27 17:05:24 UTC
Author: jakub
Date: Wed Nov 27 17:05:21 2013
New Revision: 205456

URL: http://gcc.gnu.org/viewcvs?rev=205456&root=gcc&view=rev
Log:
	Backported from mainline
	2013-11-26  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/59014
	* tree-vrp.c (register_edge_assert_for_1): Don't look
	through conversions from non-integral types or through
	narrowing conversions.

	* gcc.c-torture/execute/pr59014.c: New test.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/gcc.c-torture/execute/pr59014.c
Modified:
    branches/gcc-4_8-branch/gcc/ChangeLog
    branches/gcc-4_8-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_8-branch/gcc/tree-vrp.c
Comment 13 Jakub Jelinek 2013-11-27 17:06:47 UTC
Author: jakub
Date: Wed Nov 27 17:06:44 2013
New Revision: 205457

URL: http://gcc.gnu.org/viewcvs?rev=205457&root=gcc&view=rev
Log:
	PR tree-optimization/59014
	* gcc.c-torture/execute/pr59014-2.c: New test.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/gcc.c-torture/execute/pr59014-2.c
Modified:
    branches/gcc-4_8-branch/gcc/testsuite/ChangeLog
Comment 14 Jakub Jelinek 2014-05-07 16:08:59 UTC
Author: jakub
Date: Wed May  7 16:08:28 2014
New Revision: 210177

URL: http://gcc.gnu.org/viewcvs?rev=210177&root=gcc&view=rev
Log:
	Backported from mainline
	2013-11-27  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/59014
	* gcc.c-torture/execute/pr59014-2.c: New test.

	2013-11-26  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/59014
	* tree-vrp.c (register_edge_assert_for_1): Don't look
	through conversions from non-integral types or through
	narrowing conversions.

	* gcc.c-torture/execute/pr59014.c: New test.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/gcc.c-torture/execute/pr59014-2.c
    branches/gcc-4_7-branch/gcc/testsuite/gcc.c-torture/execute/pr59014.c
Modified:
    branches/gcc-4_7-branch/gcc/ChangeLog
    branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_7-branch/gcc/tree-vrp.c
Comment 15 Jakub Jelinek 2014-05-07 16:21:40 UTC
Fixed.