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.
Confirmed.
Started with r229484.
Mine then.
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;
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
Fixed on trunk sofar.
(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).
(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.
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
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
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
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
Fixed.