From 4d307e1ff39d0c2f90123ef789d611e2323be76d Mon Sep 17 00:00:00 2001 From: rguenth Date: Wed, 10 Feb 2010 11:54:14 +0000 Subject: [PATCH] 2010-02-10 Richard Guenther PR c/43007 * tree.c (get_unwidened): Handle constants. * convert.c (convert_to_integer): Handle TRUNC_DIV_EXPR. * gcc.c-torture/execute/20100209-1.c: New testcase. * gcc.dg/fold-div-3.c: Likewise. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@156653 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 6 ++++++ gcc/convert.c | 25 ++++++++++++++++++++++++ gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/gcc.c-torture/execute/20100209-1.c | 12 ++++++++++++ gcc/testsuite/gcc.dg/fold-div-3.c | 15 ++++++++++++++ gcc/tree.c | 8 ++++++++ 6 files changed, 72 insertions(+) create mode 100644 gcc/testsuite/gcc.c-torture/execute/20100209-1.c create mode 100644 gcc/testsuite/gcc.dg/fold-div-3.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ecda788..ce3d086 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2010-02-10 Richard Guenther + + PR c/43007 + * tree.c (get_unwidened): Handle constants. + * convert.c (convert_to_integer): Handle TRUNC_DIV_EXPR. + 2010-02-10 Martin Jambor PR lto/42985 diff --git a/gcc/convert.c b/gcc/convert.c index 4fe95ce..39fbd4d 100644 --- a/gcc/convert.c +++ b/gcc/convert.c @@ -673,6 +673,31 @@ convert_to_integer (tree type, tree expr) } break; + case TRUNC_DIV_EXPR: + { + tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type); + tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type); + + /* Don't distribute unless the output precision is at least as big + as the actual inputs and it has the same signedness. */ + if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0)) + && outprec >= TYPE_PRECISION (TREE_TYPE (arg1)) + /* If signedness of arg0 and arg1 don't match, + we can't necessarily find a type to compare them in. */ + && (TYPE_UNSIGNED (TREE_TYPE (arg0)) + == TYPE_UNSIGNED (TREE_TYPE (arg1))) + /* Do not change the sign of the division. */ + && (TYPE_UNSIGNED (TREE_TYPE (expr)) + == TYPE_UNSIGNED (TREE_TYPE (arg0))) + /* Either require unsigned division or a division by + a constant that is not -1. */ + && (TYPE_UNSIGNED (TREE_TYPE (arg0)) + || (TREE_CODE (arg1) == INTEGER_CST + && !integer_all_onesp (arg1)))) + goto trunc1; + break; + } + case MAX_EXPR: case MIN_EXPR: case MULT_EXPR: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 59181d7..273636c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2010-02-10 Richard Guenther + + PR c/43007 + * gcc.c-torture/execute/20100209-1.c: New testcase. + * gcc.dg/fold-div-3.c: Likewise. + 2010-02-10 Jakub Jelinek * gcc.dg/builtin-ffs-1.c: New test. diff --git a/gcc/testsuite/gcc.c-torture/execute/20100209-1.c b/gcc/testsuite/gcc.c-torture/execute/20100209-1.c new file mode 100644 index 0000000..bf0597b --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/20100209-1.c @@ -0,0 +1,12 @@ +int bar(int foo) +{ + return (int)(((unsigned long long)(long long)foo) / 8); +} +extern void abort (void); +int main() +{ + if (sizeof (long long) > sizeof (int) + && bar(-1) != -1) + abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/fold-div-3.c b/gcc/testsuite/gcc.dg/fold-div-3.c new file mode 100644 index 0000000..2e78757 --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-div-3.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-original" } */ + +unsigned int +apply_frontend_param (unsigned int spi_bias) +{ + static const int ppm = 8000; + spi_bias /= 1000ULL + ppm/1000; + return spi_bias; +} + +/* Make sure we perform the division in the narrower type. */ + +/* { dg-final { scan-tree-dump "spi_bias = spi_bias / 1008;" "original" } } */ +/* { dg-final { cleanup-tree-dump "original" } } */ diff --git a/gcc/tree.c b/gcc/tree.c index 5d44841..c51cca7 100644 --- a/gcc/tree.c +++ b/gcc/tree.c @@ -7623,6 +7623,14 @@ get_unwidened (tree op, tree for_type) } } + /* If we finally reach a constant see if it fits in for_type and + in that case convert it. */ + if (for_type + && TREE_CODE (win) == INTEGER_CST + && TREE_TYPE (win) != for_type + && int_fits_type_p (win, for_type)) + win = fold_convert (for_type, win); + return win; } -- 2.7.4