Bug 85588 - [6 Regression] -fwrapv miscompilation
Summary: [6 Regression] -fwrapv miscompilation
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 9.0
: P2 normal
Target Milestone: 6.5
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2018-05-01 14:49 UTC by Krister Walfridsson
Modified: 2018-06-21 09:51 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 6.4.1, 7.3.1, 8.1.1, 9.0
Known to fail: 6.4.0, 7.3.0, 8.1.0
Last reconfirmed: 2018-05-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Krister Walfridsson 2018-05-01 14:49:53 UTC
GCC miscompiles gcc/testsuite/gcc.dg/torture/pr57656.c when using -fwrapv

  > gcc -fwrapv pr57656.c
  > ./a.out
  Abort (core dumped)

The problem seems to be exactly the same as in PR57656 (but when using -fwrapv):
  t = 1 - ((a - b) / c);
is changed to
  t = (b - a) / c + 1;
which is not the same in this case where both (a - b) and (b - a) have the value 0x80000000.

This fails in GCC 6 and newer versions. Compiling using GCC 5 produces the correct result.
Comment 1 Marek Polacek 2018-05-01 14:55:35 UTC
Confirmed.
Comment 2 Marek Polacek 2018-05-01 18:30:54 UTC
Started with r229484.
Comment 3 Richard Biener 2018-05-02 07:39:02 UTC
Mine then.
Comment 4 Richard Biener 2018-05-03 09:33:57 UTC
A fix is probably as simple as

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c    (revision 259879)
+++ gcc/fold-const.c    (working copy)
@@ -472,7 +472,7 @@ negate_expr_p (tree t)
     case TRUNC_DIV_EXPR:
     case ROUND_DIV_EXPR:
     case EXACT_DIV_EXPR:
-      if (TYPE_UNSIGNED (type))
+      if (TYPE_OVERFLOW_WRAPS (type))
        break;
       if (negate_expr_p (TREE_OPERAND (t, 0)))
        return true;
Comment 5 Richard Biener 2018-05-03 12:17:17 UTC
Or rather

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c    (revision 259879)
+++ gcc/fold-const.c    (working copy)
@@ -474,12 +474,15 @@ negate_expr_p (tree t)
     case EXACT_DIV_EXPR:
       if (TYPE_UNSIGNED (type))
        break;
-      if (negate_expr_p (TREE_OPERAND (t, 0)))
+      /* In general we can't negate A in A / B, because if A is INT_MIN and
+         B is not 1 we change the sign of the result.  */
+      if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
+         && negate_expr_p (TREE_OPERAND (t, 0)))
        return true;
       /* In general we can't negate B in A / B, because if A is INT_MIN and
         B is 1, we may turn this into INT_MIN / -1 which is undefined
Comment 6 Richard Biener 2018-05-04 07:31:11 UTC
Fixed on trunk sofar.
Comment 7 Mikael Pettersson 2018-05-06 11:49:51 UTC
(In reply to Richard Biener from comment #6)
> Fixed on trunk sofar.

I believe the fix was mistakenly labelled as PR85574 (including the file name of the new test case).
Comment 8 Richard Biener 2018-05-08 07:55:33 UTC
(In reply to Mikael Pettersson from comment #7)
> (In reply to Richard Biener from comment #6)
> > Fixed on trunk sofar.
> 
> I believe the fix was mistakenly labelled as PR85574 (including the file
> name of the new test case).

Whoops sorry, will fix the file name.
Comment 9 Richard Biener 2018-05-08 07:55:56 UTC
Author: rguenth
Date: Tue May  8 07:55:24 2018
New Revision: 260024

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

	PR middle-end/85588
	* gcc.dg/torture/pr85574.c: Rename to...
	* gcc.dg/torture/pr85588.c: ... this.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/pr85588.c
      - copied unchanged from r260023, trunk/gcc/testsuite/gcc.dg/torture/pr85574.c
Removed:
    trunk/gcc/testsuite/gcc.dg/torture/pr85574.c
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 10 Richard Biener 2018-05-08 13:34:27 UTC
Author: rguenth
Date: Tue May  8 13:33:54 2018
New Revision: 260044

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

	Backport from mainline
	2018-05-04  Richard Biener  <rguenther@suse.de>

	PR middle-end/85588
	* fold-const.c (negate_expr_p): Restrict negation of operand
	zero of a division to when we know that can happen without
	overflow.
	(fold_negate_expr_1): Likewise.

	* gcc.dg/torture/pr85588.c: New testcase.
	* gcc.dg/torture/pr57656.c: Use dg-additional-options.

	2018-05-03  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/85615
	* tree-ssa-threadupdate.c (thread_block_1): Only allow exits
	to loops not nested in BBs loop father to avoid creating multi-entry
	loops.

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

	2018-05-02  Richard Biener  <rguenther@suse.de>

	PR middle-end/85567
	* gimplify.c (gimplify_save_expr): When in SSA form allow
	SAVE_EXPRs to compute to SSA vars.

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

	2018-05-02  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/85597
	* tree-vect-stmts.c (vectorizable_operation): For ternary SLP
	do not use split vect_get_vec_defs call but call vect_get_slp_defs
	directly.

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

Added:
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/torture/pr85567.c
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/torture/pr85588.c
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/torture/pr85615.c
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/vect/pr85597.c
Modified:
    branches/gcc-8-branch/gcc/ChangeLog
    branches/gcc-8-branch/gcc/fold-const.c
    branches/gcc-8-branch/gcc/gimplify.c
    branches/gcc-8-branch/gcc/testsuite/ChangeLog
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/torture/pr57656.c
    branches/gcc-8-branch/gcc/tree-ssa-threadupdate.c
    branches/gcc-8-branch/gcc/tree-vect-stmts.c
Comment 11 Richard Biener 2018-06-07 10:10:31 UTC
Author: rguenth
Date: Thu Jun  7 10:10:00 2018
New Revision: 261269

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

	Backport from mainline
	2018-05-04  Richard Biener  <rguenther@suse.de>

	PR middle-end/85588
	* fold-const.c (negate_expr_p): Restrict negation of operand
	zero of a division to when we know that can happen without
	overflow.
	(fold_negate_expr_1): Likewise.

	* gcc.dg/torture/pr85588.c: New testcase.
	* gcc.dg/torture/pr57656.c: Use dg-additional-options.

	2018-05-02  Richard Biener  <rguenther@suse.de>

	PR middle-end/85567
	* gimplify.c (gimplify_save_expr): When in SSA form allow
	SAVE_EXPRs to compute to SSA vars.

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

	2018-05-02  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/85597
	* tree-vect-stmts.c (vectorizable_operation): For ternary SLP
	do not use split vect_get_vec_defs call but call vect_get_slp_defs
	directly.

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

Added:
    branches/gcc-7-branch/gcc/testsuite/gcc.dg/torture/pr85567.c
    branches/gcc-7-branch/gcc/testsuite/gcc.dg/torture/pr85588.c
    branches/gcc-7-branch/gcc/testsuite/gcc.dg/vect/pr85597.c
Modified:
    branches/gcc-7-branch/gcc/ChangeLog
    branches/gcc-7-branch/gcc/fold-const.c
    branches/gcc-7-branch/gcc/gimplify.c
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
    branches/gcc-7-branch/gcc/testsuite/gcc.dg/torture/pr57656.c
    branches/gcc-7-branch/gcc/tree-vect-stmts.c
Comment 12 Richard Biener 2018-06-21 09:51:08 UTC
Author: rguenth
Date: Thu Jun 21 09:50:36 2018
New Revision: 261839

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

	Backport from mainline
	2018-02-28  Richard Biener  <rguenther@suse.de>

	PR middle-end/84607
	* genmatch.c (capture_info::walk_match): Do not mark
	captured expressions without operands as expr_p given
	they act more like predicates and should be subject to
	"lost tail" side-effect preserving.

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

	2018-05-04  Richard Biener  <rguenther@suse.de>

	PR middle-end/85588
	* fold-const.c (negate_expr_p): Restrict negation of operand
	zero of a division to when we know that can happen without
	overflow.
	(fold_negate_expr_1): Likewise.

	* gcc.dg/torture/pr85588.c: New testcase.
	* gcc.dg/torture/pr57656.c: Use dg-additional-options.

Added:
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/pr84607.c
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/torture/pr85588.c
Modified:
    branches/gcc-6-branch/gcc/ChangeLog
    branches/gcc-6-branch/gcc/fold-const.c
    branches/gcc-6-branch/gcc/genmatch.c
    branches/gcc-6-branch/gcc/testsuite/ChangeLog
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/torture/pr57656.c
Comment 13 Richard Biener 2018-06-21 09:51:37 UTC
Fixed.