Bug 63665 - [4.8 Regression] wrong code with signed overflow even with -fwrapv
Summary: [4.8 Regression] wrong code with signed overflow even with -fwrapv
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: 4.8.4
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2014-10-28 09:01 UTC by Zdenek Sojka
Modified: 2014-11-26 13:17 UTC (History)
0 users

See Also:
Host: x86_64-pc-linux-gnu
Target: x86_64-pc-linux-gnu
Build:
Known to work: 4.8.4, 4.9.2, 5.0
Known to fail:
Last reconfirmed: 2014-10-28 00:00:00


Attachments
reduced testcase (from gcc.c-torture/execute/20040409-1.c) (117 bytes, text/x-csrc)
2014-10-28 09:01 UTC, Zdenek Sojka
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Zdenek Sojka 2014-10-28 09:01:10 UTC
Created attachment 33826 [details]
reduced testcase (from gcc.c-torture/execute/20040409-1.c)

Output:
$ gcc -O -fno-tree-ccp -fno-tree-fre -fwrapv testcase.c
$ ./a.out 
Aborted

In the assembly output, main() unconditionally calls abort().

The original testcase, gcc.c-torture/execute/20040409-1.c, fails with:
$ gcc -O2 -flto -fno-tree-ccp -fno-tree-copy-prop -fno-tree-fre 20040409-1.i
$ ./a.out 
Aborted

Tested revisions:
r216724 - FAIL
4_9 r216431 - OK
4_8 r216430 - OK
Comment 1 Richard Biener 2014-10-28 09:07:51 UTC
Confirmed, I'll have a look.
Comment 2 Richard Biener 2014-10-28 09:25:03 UTC
To better bisect this also use -fno-tree-copy-prop (which otherwise hides
this on the 4.9 branch for example).

It's forwprop that does the bogus transform on

  y_5 = -2147483648;
  _6 = y_5 + -2147483648;

which ends up calling fold_binary (NE_EXPR, y_5 + -2147483648, 0) which
computes it as 1.

8769          tree new_const = int_const_binop (reverse_op, const2, const1);

results in -2147483648(OVF) even though we have -fwrapv in effect which
the triggers

      /* If the constant operation overflowed this can be
         simplified as a comparison against INT_MAX/INT_MIN.  */
      if (TREE_OVERFLOW (new_const))
        {

without changing how we compute TREE_OVERFLOW it seems that this test needs
to be guarded with !TYPE_OVERFLOW_WRAPS ().
Comment 3 Richard Biener 2014-10-28 11:43:14 UTC
Author: rguenth
Date: Tue Oct 28 11:42:43 2014
New Revision: 216781

URL: https://gcc.gnu.org/viewcvs?rev=216781&root=gcc&view=rev
Log:
2014-10-28  Richard Biener  <rguenther@suse.de>

	PR middle-end/63665
	* fold-const.c (fold_comparison): Properly guard simplifying
	against INT_MAX/INT_MIN with !TYPE_OVERFLOW_WRAPS.

	* gcc.dg/pr63665.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/pr63665.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Richard Biener 2014-10-28 11:44:05 UTC
Fixed on trunk.  Leaving open for a backport to 4.9/4.8.  Regression marker
so I don't miss it when doing the next batch of backports.
Comment 5 Richard Biener 2014-10-28 12:52:20 UTC
Latent issue.

pr45752.c:40:3: note: Load permutation 2 2 2 2 2 3 3 3 3 3 0 0 0 0 0 1 1 1 1 1 4 4 4 4 4

We have a SLP group size of 5 and computed an unroll factor of 2 (ok).  It looks
to me that

Index: gcc/tree-vect-slp.c
===================================================================
--- gcc/tree-vect-slp.c (revision 216771)
+++ gcc/tree-vect-slp.c (working copy)

@@ -3041,6 +3041,7 @@ vect_transform_slp_perm_load (slp_tree n
                                          &number_of_mask_fixes, &mask_fixed,
                                          &needs_first_vector))
                return false;
+             gcc_assert (current_mask_element < 2 * nunits);
              mask[index++] = current_mask_element;
 
               if (index == nunits)

makes x86_64 ICE as well.  The issue is that we suddenly need to skip _two_
vectors in vect_get_mask_element which isn't implemented so we get
out-of-bound indexes.  Seems we need to iterate instead in which case
we will reject vectorization correctly with

pr45752.c:40:3: note: Load permutation 2 2 2 2 2 3 3 3 3 3 0 0 0 0 0 1 1 1 1 1 4 4 4 4 4
pr45752.c:40:3: note: permutation requires at least three vectors c_71 = MEM[(unsigned int *)pInput2_135 + 8B];

pr45752.c:40:3: note: Build SLP failed: unsupported load permutation *pOutput2_136 = _83;
Comment 6 Richard Biener 2014-11-26 11:43:16 UTC
Author: rguenth
Date: Wed Nov 26 11:42:44 2014
New Revision: 218078

URL: https://gcc.gnu.org/viewcvs?rev=218078&root=gcc&view=rev
Log:
2014-11-26  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2014-11-26  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/62238
	* tree-predcom.c (ref_at_iteration): Unshare the expression
	before gimplifying it.

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

	2014-11-25  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/61927
	* tree-vect-loop.c (vect_analyze_loop_2): Revert ordering
	of group and pattern analysis to the one in GCC 4.8.

	2014-11-07  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/63605
	* fold-const.c (fold_binary_loc): Properly use element_precision
	for types that may not be scalar.

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

	2014-10-28  Richard Biener  <rguenther@suse.de>

	PR middle-end/63665
	* fold-const.c (fold_comparison): Properly guard simplifying
	against INT_MAX/INT_MIN with !TYPE_OVERFLOW_WRAPS.

	* gcc.dg/pr63665.c: New testcase.

Added:
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/pr63665.c
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/torture/pr62238.c
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/vect/pr63605.c
Modified:
    branches/gcc-4_9-branch/gcc/ChangeLog
    branches/gcc-4_9-branch/gcc/fold-const.c
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_9-branch/gcc/tree-predcom.c
    branches/gcc-4_9-branch/gcc/tree-vect-loop.c
Comment 7 Richard Biener 2014-11-26 13:15:49 UTC
Author: rguenth
Date: Wed Nov 26 13:15:16 2014
New Revision: 218079

URL: https://gcc.gnu.org/viewcvs?rev=218079&root=gcc&view=rev
Log:
2014-11-26  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2014-10-08  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/61969
	* tree-nrv.c (pass_nrv::execute): Properly test for automatic
	variables.

	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/pr62031.c: New testcase.

	2014-10-10  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/63379
	* tree-vect-slp.c (vect_get_constant_vectors): Do not compute
	a neutral operand for min/max when it is not a reduction chain.

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

	2014-11-07  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/63605
	* fold-const.c (fold_binary_loc): Properly use element_precision
	for types that may not be scalar.

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

	2014-10-28  Richard Biener  <rguenther@suse.de>

	PR middle-end/63665
	* fold-const.c (fold_comparison): Properly guard simplifying
	against INT_MAX/INT_MIN with !TYPE_OVERFLOW_WRAPS.

	* gcc.dg/pr63665.c: New testcase.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/pr63665.c
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/torture/pr62031.c
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/pr63379.c
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/pr63605.c
Modified:
    branches/gcc-4_8-branch/gcc/ChangeLog
    branches/gcc-4_8-branch/gcc/fold-const.c
    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-nrv.c
    branches/gcc-4_8-branch/gcc/tree-vect-slp.c
Comment 8 Richard Biener 2014-11-26 13:17:23 UTC
Fixed.