From 1dcffc8ddc48f0b45d3d0d2f763ef5870560eb9a Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Tue, 31 Mar 2020 11:06:43 +0200 Subject: [PATCH] fold-const: Fix division folding with vector operands [PR94412] The following testcase is miscompiled since 4.9, we treat unsigned vector types as if they were signed and "optimize" negations across it. 2020-03-31 Marc Glisse Jakub Jelinek PR middle-end/94412 * fold-const.c (fold_binary_loc) : Use ANY_INTEGRAL_TYPE_P instead of INTEGRAL_TYPE_P. * gcc.c-torture/execute/pr94412.c: New test. Co-authored-by: Marc Glisse --- gcc/ChangeLog | 7 +++++ gcc/fold-const.c | 8 +++--- gcc/testsuite/ChangeLog | 3 ++ gcc/testsuite/gcc.c-torture/execute/pr94412.c | 28 +++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr94412.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index e7ff9131f5d..4686c59f90c 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2020-03-31 Marc Glisse + Jakub Jelinek + + PR middle-end/94412 + * fold-const.c (fold_binary_loc) : Use + ANY_INTEGRAL_TYPE_P instead of INTEGRAL_TYPE_P. + 2020-03-31 Jakub Jelinek PR tree-optimization/94403 diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 92679142f04..b79d059d741 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -11148,11 +11148,11 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, /* Convert -A / -B to A / B when the type is signed and overflow is undefined. */ - if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) + if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) && TREE_CODE (op0) == NEGATE_EXPR && negate_expr_p (op1)) { - if (INTEGRAL_TYPE_P (type)) + if (ANY_INTEGRAL_TYPE_P (type)) fold_overflow_warning (("assuming signed overflow does not occur " "when distributing negation across " "division"), @@ -11162,11 +11162,11 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, TREE_OPERAND (arg0, 0)), negate_expr (op1)); } - if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) + if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) && TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (op0)) { - if (INTEGRAL_TYPE_P (type)) + if (ANY_INTEGRAL_TYPE_P (type)) fold_overflow_warning (("assuming signed overflow does not occur " "when distributing negation across " "division"), diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 3f21cb32106..32d5309882e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2020-03-31 Jakub Jelinek + PR middle-end/94412 + * gcc.c-torture/execute/pr94412.c: New test. + PR tree-optimization/94403 * g++.dg/tree-ssa/pr94403.C: New test. diff --git a/gcc/testsuite/gcc.c-torture/execute/pr94412.c b/gcc/testsuite/gcc.c-torture/execute/pr94412.c new file mode 100644 index 00000000000..6c806bbd90c --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr94412.c @@ -0,0 +1,28 @@ +/* PR middle-end/94412 */ + +typedef unsigned V __attribute__ ((__vector_size__ (sizeof (unsigned) * 2))); + +void +foo (V *v, V *w) +{ + *w = -*v / 11; +} + +void +bar (V *v, V *w) +{ + *w = -18 / -*v; +} + +int +main () +{ + V a = (V) { 1, 0 }; + V b = (V) { 3, __INT_MAX__ }; + V c, d; + foo (&a, &c); + bar (&b, &d); + if (c[0] != -1U / 11 || c[1] != 0 || d[0] != 0 || d[1] != -18U / -__INT_MAX__) + __builtin_abort (); + return 0; +} -- 2.30.2