Bug 66272 - [4.8 Regression] wrong code at -O3 on x86_64-linux-gnu
Summary: [4.8 Regression] wrong code at -O3 on x86_64-linux-gnu
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: 4.9.3
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2015-05-24 10:02 UTC by Zhendong Su
Modified: 2015-06-03 13:51 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.9.3, 5.2.0, 6.0
Known to fail: 4.9.2, 5.1.0
Last reconfirmed: 2015-05-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Zhendong Su 2015-05-24 10:02:26 UTC
The current gcc trunk miscompiles the following code on x86_64-linux at -O3 in both 32-bit and 64-bit modes.  

It also affects gcc 5.1.x and 4.9.2 (but not 4.9.0 and I didn't check 4.9.1). 


$ 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/6.0.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 6.0.0 20150524 (experimental) [trunk revision 223630] (GCC) 
$ 
$ gcc-trunk -O2 small.c; ./a.out
$ gcc-4.9.0 -O3 small.c; ./a.out
$ 
$ gcc-trunk -O3 small.c  
$ ./a.out
Aborted (core dumped)
$ gcc-4.9.2 -O3 small.c
$ ./a.out
Aborted (core dumped)
$ gcc-5.1.0 -O3 small.c
$ ./a.out
Aborted (core dumped)
$ 


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


struct S
{
  int f0;
  int f1;
} a[2], c;

int b;

int
main ()
{
  struct S d = { 0, 1 };

  for (b = 0; b < 2; b++)
    {
      a[b] = d;
      c = d = a[0];
    }

  if (c.f1 != 1)
    __builtin_abort ();

  return 0;
}
Comment 1 Mikhail Maltsev 2015-05-24 23:29:03 UTC
The testcase can be simplified a bit (I eliminated variable c and moved array a inside main, so it's now local).

struct S
{
  int f0;
  int f1;
};

int b;

int main ()
{
    struct S a[2] = { 0 };
    struct S d = { 0, 1 };
    for (b = 0; b < 2; b++) {
        a[b] = d;
        d = a[0];
    }
    if (d.f1 != 1)
        __builtin_abort ();
}

Wrong transformation is made in predictive commoning pass (the program compiled with -O3 -fno-predictive-commoning does not abort). The store d.f1 = a[0].f1; is assumed to be independent of the previous store to a[b] (which is obviously wrong) and it is hoisted out of the loop.

I think the reason is that find_data_references_in_stmt or some related function misenterprets MEM[(struct S[2] *)&a + 4B] as some object that does not alias with MEM[(struct S *)&a]'s SCEV.
By the way, the expression itself looks strange: struct S * should be "a", not "&a".

(Data Dep:
#(Data Ref:
#  bb: 3
#  stmt: MEM[(struct S *)_4 + 4B] = d$f1_7;
#  ref: MEM[(struct S *)_4 + 4B];
#  base_object: MEM[(struct S *)&a];
#  Access function 0: {4B, +, 8}_1
#)
#(Data Ref:
#  bb: 3
#  stmt: d$f1_16 = MEM[(struct S[2] *)&a + 4B];
#  ref: MEM[(struct S[2] *)&a + 4B];
#  base_object: MEM[(struct S[2] *)&a + 4B];
#)
    (no dependence)
)

If I scalarize the assignment d = a[0] (I mean, in the source), the bug disappears.
Comment 2 Jakub Jelinek 2015-05-25 07:21:53 UTC
Started with r214006.
Comment 3 Richard Biener 2015-05-26 10:46:23 UTC
Bah, mine.
Comment 4 Richard Biener 2015-05-27 14:22:02 UTC
Author: rguenth
Date: Wed May 27 14:20:48 2015
New Revision: 223759

URL: https://gcc.gnu.org/viewcvs?rev=223759&root=gcc&view=rev
Log:
2015-05-27  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66272
	Revert parts of
	2014-08-15  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/62031
	* tree-data-ref.c (dr_analyze_indices): Do not set
	DR_UNCONSTRAINED_BASE.
	(dr_may_alias_p): All indirect accesses have to go the
	formerly DR_UNCONSTRAINED_BASE path.
	* tree-data-ref.h (struct indices): Remove
	unconstrained_base member.
	(DR_UNCONSTRAINED_BASE): Remove.

	* gcc.dg/torture/pr66272.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/pr66272.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-data-ref.c
    trunk/gcc/tree-data-ref.h
Comment 5 Richard Biener 2015-05-27 14:22:29 UTC
Fixed on trunk sofar.
Comment 6 Richard Biener 2015-06-03 07:39:38 UTC
Author: rguenth
Date: Wed Jun  3 07:39:06 2015
New Revision: 224059

URL: https://gcc.gnu.org/viewcvs?rev=224059&root=gcc&view=rev
Log:
2015-06-03  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2015-06-02  Richard Biener  <rguenther@suse.de>

	PR debug/65549
	* dwarf2out.c (lookup_context_die): New function.
	(resolve_addr): Avoid forcing a full DIE for the
	target of a DW_TAG_GNU_call_site during late compilation.
	Instead create a stub DIE without a type if we have a
	context DIE present.

	* g++.dg/lto/pr65549_0.C: New testcase.

	2015-06-01  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66280
	* tree-vect-slp.c (vect_detect_hybrid_slp_stmts): Fix pattern
	def-use walking.

	* g++.dg/torture/pr66280.C: New testcase.
	* g++.dg/torture/pr66280-2.C: Likewise.

	2015-05-27  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66272
	Revert parts of
	2014-08-15  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/62031
	* tree-data-ref.c (dr_analyze_indices): Do not set
	DR_UNCONSTRAINED_BASE.
	(dr_may_alias_p): All indirect accesses have to go the
	formerly DR_UNCONSTRAINED_BASE path.
	* tree-data-ref.h (struct indices): Remove
	unconstrained_base member.
	(DR_UNCONSTRAINED_BASE): Remove.

	* gcc.dg/torture/pr66272.c: New testcase.

	2015-05-21  Richard Biener  <rguenther@suse.de>

	PR c++/66211
	* match.pd: Guard pattern optimzing (int)(float)int
	conversions to apply only on GIMPLE.

	* g++.dg/conversion/pr66211.C: New testcase.
	* gcc.dg/tree-ssa/forwprop-18.c: Adjust.

	2015-05-13  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66123
	* tree-ssa-dom.c (propagate_rhs_into_lhs): Check if we found
	a taken edge.

	* gcc.dg/torture/pr66123.c: New testcase.

Added:
    branches/gcc-5-branch/gcc/testsuite/g++.dg/conversion/pr66211.C
    branches/gcc-5-branch/gcc/testsuite/g++.dg/lto/pr65549_0.C
    branches/gcc-5-branch/gcc/testsuite/g++.dg/torture/pr66280-2.C
    branches/gcc-5-branch/gcc/testsuite/g++.dg/torture/pr66280.C
    branches/gcc-5-branch/gcc/testsuite/gcc.dg/torture/pr66123.c
    branches/gcc-5-branch/gcc/testsuite/gcc.dg/torture/pr66272.c
Modified:
    branches/gcc-5-branch/gcc/ChangeLog
    branches/gcc-5-branch/gcc/dwarf2out.c
    branches/gcc-5-branch/gcc/match.pd
    branches/gcc-5-branch/gcc/testsuite/ChangeLog
    branches/gcc-5-branch/gcc/testsuite/gcc.dg/tree-ssa/forwprop-18.c
    branches/gcc-5-branch/gcc/tree-data-ref.c
    branches/gcc-5-branch/gcc/tree-data-ref.h
    branches/gcc-5-branch/gcc/tree-ssa-dom.c
    branches/gcc-5-branch/gcc/tree-vect-slp.c
Comment 7 Richard Biener 2015-06-03 11:03:58 UTC
Author: rguenth
Date: Wed Jun  3 11:03:26 2015
New Revision: 224072

URL: https://gcc.gnu.org/viewcvs?rev=224072&root=gcc&view=rev
Log:
2015-06-03  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2015-05-26  Michael Matz  <matz@suse.de>

	PR middle-end/66251
	* tree-vect-stmts.c (vect_create_vectorized_demotion_stmts): Always set
	STMT_VINFO_VEC_STMT, also with SLP.

	* gcc.dg/vect/pr66251.c: New test.

	2015-05-22  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66251
	* tree-vect-stmts.c (vectorizable_conversion): Properly
	set STMT_VINFO_VEC_STMT even for the SLP case.

	* gfortran.fortran-torture/compile/pr66251.f90: New testcase.

	2015-05-27  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66272
	Revert parts of
	2014-08-15  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/62031
	* tree-data-ref.c (dr_analyze_indices): Do not set
	DR_UNCONSTRAINED_BASE.
	(dr_may_alias_p): All indirect accesses have to go the
	formerly DR_UNCONSTRAINED_BASE path.
	* tree-data-ref.h (struct indices): Remove
	unconstrained_base member.
	(DR_UNCONSTRAINED_BASE): Remove.

	* gcc.dg/torture/pr66272.c: New testcase.

	2015-05-13  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66123
	* tree-ssa-dom.c (propagate_rhs_into_lhs): Check if we found
	a taken edge.

	* gcc.dg/torture/pr66123.c: New testcase.

	2015-06-02  Richard Biener  <rguenther@suse.de>

	PR debug/65549
	* dwarf2out.c (lookup_context_die): New function.
	(resolve_addr): Avoid forcing a full DIE for the
	target of a DW_TAG_GNU_call_site during late compilation.
	Instead create a stub DIE without a type if we have a
	context DIE present.

	* g++.dg/lto/pr65549_0.C: New testcase.

	2015-03-23  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/65518
	* tree-vect-stmts.c (vectorizable_load): Reject single-element
	interleaving cases we generate absymal code for.

	* gcc.dg/vect/pr65518.c: New testcase.

Added:
    branches/gcc-4_9-branch/gcc/testsuite/g++.dg/lto/pr65549_0.C
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/torture/pr66123.c
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/torture/pr66272.c
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/vect/pr65518.c
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/vect/pr66251.c
    branches/gcc-4_9-branch/gcc/testsuite/gfortran.fortran-torture/compile/pr66251.f90
Modified:
    branches/gcc-4_9-branch/gcc/ChangeLog
    branches/gcc-4_9-branch/gcc/dwarf2out.c
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_9-branch/gcc/tree-data-ref.c
    branches/gcc-4_9-branch/gcc/tree-data-ref.h
    branches/gcc-4_9-branch/gcc/tree-ssa-dom.c
    branches/gcc-4_9-branch/gcc/tree-vect-stmts.c
Comment 8 Richard Biener 2015-06-03 13:50:44 UTC
Author: rguenth
Date: Wed Jun  3 13:50:11 2015
New Revision: 224081

URL: https://gcc.gnu.org/viewcvs?rev=224081&root=gcc&view=rev
Log:
2015-06-03  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2015-05-26  Michael Matz  <matz@suse.de>

	PR middle-end/66251
	* tree-vect-stmts.c (vect_create_vectorized_demotion_stmts): Always set
	STMT_VINFO_VEC_STMT, also with SLP.

	* gcc.dg/vect/pr66251.c: New test.

	2015-05-22  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66251
	* tree-vect-stmts.c (vectorizable_conversion): Properly
	set STMT_VINFO_VEC_STMT even for the SLP case.

	* gfortran.fortran-torture/compile/pr66251.f90: New testcase.

	2015-03-23  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/65518
	* tree-vect-stmts.c (vectorizable_load): Reject single-element
	interleaving cases we generate absymal code for.

	* gcc.dg/vect/pr65518.c: New testcase.

	2015-05-13  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66123
	* tree-ssa-dom.c (propagate_rhs_into_lhs): Check if we found
	a taken edge.

	* gcc.dg/torture/pr66123.c: New testcase.

	2015-05-27  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/66272
	Revert parts of
	2014-08-15  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/62031
	* tree-data-ref.c (dr_analyze_indices): Do not set
	DR_UNCONSTRAINED_BASE.
	(dr_may_alias_p): All indirect accesses have to go the
	formerly DR_UNCONSTRAINED_BASE path.
	* tree-data-ref.h (struct indices): Remove
	unconstrained_base member.
	(DR_UNCONSTRAINED_BASE): Remove.

	* gcc.dg/torture/pr66272.c: New testcase.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/torture/pr66123.c
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/torture/pr66272.c
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/pr65518.c
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/pr66251.c
    branches/gcc-4_8-branch/gcc/testsuite/gfortran.fortran-torture/compile/pr66251.f90
Modified:
    branches/gcc-4_8-branch/gcc/ChangeLog
    branches/gcc-4_8-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_8-branch/gcc/tree-data-ref.c
    branches/gcc-4_8-branch/gcc/tree-data-ref.h
    branches/gcc-4_8-branch/gcc/tree-ssa-dom.c
    branches/gcc-4_8-branch/gcc/tree-vect-stmts.c
Comment 9 Richard Biener 2015-06-03 13:51:35 UTC
Fixed.