From 64da3a9a3f6f01f59be701b3ec55428abb563941 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Thu, 15 Oct 2015 09:39:35 +0000 Subject: [PATCH] re PR tree-optimization/67953 (match.pd: X - (X / Y) * Y wrong on change of sign) PR tree-optimization/67953 * match.pd (X - (X / Y) * Y): Don't change signedness of @0. * gcc.dg/fold-minus-6.c (fn4): Change the type of A to unsigned. * gcc.dg/torture/pr67953.c: New test. From-SVN: r228839 --- gcc/ChangeLog | 5 ++++ gcc/match.pd | 3 ++- gcc/testsuite/ChangeLog | 7 +++++ gcc/testsuite/gcc.dg/fold-minus-6.c | 2 +- gcc/testsuite/gcc.dg/torture/pr67953.c | 36 ++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr67953.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 5a0fde82ca7..6dec962d283 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2015-10-15 Marek Polacek + + PR tree-optimization/67953 + * match.pd (X - (X / Y) * Y): Don't change signedness of @0. + 2015-10-15 Jiong Wang * config.gcc: Recognize "." in architecture base name for AArch64. diff --git a/gcc/match.pd b/gcc/match.pd index 655c9ffcf26..24e19a96472 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -267,7 +267,8 @@ along with GCC; see the file COPYING3. If not see /* X - (X / Y) * Y is the same as X % Y. */ (simplify (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1))) - (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) + (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) + && TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (type)) (trunc_mod (convert @0) (convert @1)))) /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f45ab81e5ca..631d8a7b613 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2015-10-15 Marek Polacek + + PR tree-optimization/67953 + * gcc.dg/fold-minus-6.c (fn4): Change the type of A to + unsigned. + * gcc.dg/torture/pr67953.c: New test. + 2015-10-14 Jeff Law * gcc.dg/tree-ssa/ssa-dom-thread-2.c: Deleted. The six functions diff --git a/gcc/testsuite/gcc.dg/fold-minus-6.c b/gcc/testsuite/gcc.dg/fold-minus-6.c index 1c22c252f72..153545266d8 100644 --- a/gcc/testsuite/gcc.dg/fold-minus-6.c +++ b/gcc/testsuite/gcc.dg/fold-minus-6.c @@ -20,7 +20,7 @@ fn3 (long int x) } int -fn4 (int a, int b) +fn4 (unsigned int a, int b) { return a - (unsigned) ((a / b) * b); } diff --git a/gcc/testsuite/gcc.dg/torture/pr67953.c b/gcc/testsuite/gcc.dg/torture/pr67953.c new file mode 100644 index 00000000000..68ff0132425 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr67953.c @@ -0,0 +1,36 @@ +/* PR tree-optimization/67953 */ +/* { dg-do run } */ + +unsigned int +fn1 (signed int a) +{ + return (unsigned int) a - ((a / 3) * 3); +} + +unsigned int +fn2 (signed int a) +{ + return a - ((a / 3) * 3); +} + +unsigned int +fn3 (int a) +{ + return a - (unsigned) ((a / 3) * 3); +} + +signed int +fn4 (int a) +{ + return (unsigned) a - (unsigned) ((a / 3) * 3); +} + +int +main () +{ + if (fn1 (-5) != -2 + || fn2 (-5) != -2 + || fn3 (-5) != -2 + || fn4 (-5) != -2) + __builtin_abort (); +} -- 2.30.2