From b66208429d844918c63932db8853a109b2d1d6d5 Mon Sep 17 00:00:00 2001 From: rguenth Date: Fri, 4 May 2018 07:25:54 +0000 Subject: [PATCH] 2018-05-04 Richard Biener PR middle-end/85574 * 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/pr85574.c: New testcase. * gcc.dg/torture/pr57656.c: Use dg-additional-options. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@259922 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 8 ++++++++ gcc/fold-const.c | 14 ++++++++++---- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/gcc.dg/torture/pr57656.c | 2 +- gcc/testsuite/gcc.dg/torture/pr85574.c | 4 ++++ 5 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr85574.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 229e0951371..3e769f54e6b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2018-05-04 Richard Biener + + PR middle-end/85574 + * 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. + 2018-05-04 Jakub Jelinek PR libstdc++/85466 diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 6472f103094..faa184a2bbd 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -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 and actually traps on some architectures. */ - if (! INTEGRAL_TYPE_P (TREE_TYPE (t)) + if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t)) || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t)) || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST && ! integer_onep (TREE_OPERAND (t, 1)))) @@ -652,14 +655,17 @@ fold_negate_expr_1 (location_t loc, 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 fold_build2_loc (loc, TREE_CODE (t), type, negate_expr (TREE_OPERAND (t, 0)), TREE_OPERAND (t, 1)); /* 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 and actually traps on some architectures. */ - if ((! INTEGRAL_TYPE_P (TREE_TYPE (t)) + if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t)) || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t)) || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST && ! integer_onep (TREE_OPERAND (t, 1)))) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6b5077535db..9e5784eb346 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2018-05-04 Richard Biener + + PR middle-end/85574 + * gcc.dg/torture/pr85574.c: New testcase. + * gcc.dg/torture/pr57656.c: Use dg-additional-options. + 2018-05-04 Jakub Jelinek PR libstdc++/85466 diff --git a/gcc/testsuite/gcc.dg/torture/pr57656.c b/gcc/testsuite/gcc.dg/torture/pr57656.c index 4f3645e4693..02490140105 100644 --- a/gcc/testsuite/gcc.dg/torture/pr57656.c +++ b/gcc/testsuite/gcc.dg/torture/pr57656.c @@ -1,5 +1,5 @@ /* { dg-do run } */ -/* { dg-options "-fstrict-overflow" } */ +/* { dg-additional-options "-fstrict-overflow" } */ int main (void) { diff --git a/gcc/testsuite/gcc.dg/torture/pr85574.c b/gcc/testsuite/gcc.dg/torture/pr85574.c new file mode 100644 index 00000000000..5d95c96bc35 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr85574.c @@ -0,0 +1,4 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fwrapv" } */ + +#include "pr57656.c" -- 2.11.4.GIT