From 053d5e0cdd2f9d08b1a7a4275b40d2930936d9f6 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Wed, 30 Nov 2016 09:01:47 +0100 Subject: [PATCH] re PR tree-optimization/78586 (Wrong code caused by printf-return-value) PR tree-optimization/78586 * gimple-ssa-sprintf.c (format_integer): Use TYPE_MAX_VALUE or TYPE_MIN_VALUE or build_all_ones_cst instead of folding LSHIFT_EXPR. Don't build_int_cst min/max twice. Formatting fix. * gcc.c-torture/execute/pr78586.c: New test. From-SVN: r242998 --- gcc/ChangeLog | 7 ++++++ gcc/gimple-ssa-sprintf.c | 31 ++++++++++++++------------- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.c-torture/execute/pr78586.c | 17 +++++++++++++++ 4 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr78586.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 48862fb..1b8e061 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2016-11-30 Jakub Jelinek + + PR tree-optimization/78586 + * gimple-ssa-sprintf.c (format_integer): Use TYPE_MAX_VALUE or + TYPE_MIN_VALUE or build_all_ones_cst instead of folding LSHIFT_EXPR. + Don't build_int_cst min/max twice. Formatting fix. + 2016-11-30 Markus Trippelsdorf PR rtl-optimization/78588 diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c index 732bc42..99a635a 100644 --- a/gcc/gimple-ssa-sprintf.c +++ b/gcc/gimple-ssa-sprintf.c @@ -995,7 +995,8 @@ format_integer (const conversion_spec &spec, tree arg) tree argmin = NULL_TREE; tree argmax = NULL_TREE; - if (arg && TREE_CODE (arg) == SSA_NAME + if (arg + && TREE_CODE (arg) == SSA_NAME && TREE_CODE (argtype) == INTEGER_TYPE) { /* Try to determine the range of values of the integer argument @@ -1017,12 +1018,8 @@ format_integer (const conversion_spec &spec, tree arg) the upper bound for %i but -3 for %u. */ if (wi::neg_p (min) && !wi::neg_p (max)) { - argmin = build_int_cst (argtype, wi::fits_uhwi_p (min) - ? min.to_uhwi () : min.to_shwi ()); - - argmax = build_int_cst (argtype, wi::fits_uhwi_p (max) - ? max.to_uhwi () : max.to_shwi ()); - + argmin = res.argmin; + argmax = res.argmax; int minbytes = format_integer (spec, res.argmin).range.min; int maxbytes = format_integer (spec, res.argmax).range.max; if (maxbytes < minbytes) @@ -1081,21 +1078,25 @@ format_integer (const conversion_spec &spec, tree arg) int typeprec = TYPE_PRECISION (dirtype); int argprec = TYPE_PRECISION (argtype); - if (argprec < typeprec || POINTER_TYPE_P (argtype)) + if (argprec < typeprec) { - if (TYPE_UNSIGNED (argtype)) + if (POINTER_TYPE_P (argtype)) argmax = build_all_ones_cst (argtype); + else if (TYPE_UNSIGNED (argtype)) + argmax = TYPE_MAX_VALUE (argtype); else - argmax = fold_build2 (LSHIFT_EXPR, argtype, integer_one_node, - build_int_cst (integer_type_node, - argprec - 1)); + argmax = TYPE_MIN_VALUE (argtype); } else { - argmax = fold_build2 (LSHIFT_EXPR, dirtype, integer_one_node, - build_int_cst (integer_type_node, - typeprec - 1)); + if (POINTER_TYPE_P (dirtype)) + argmax = build_all_ones_cst (dirtype); + else if (TYPE_UNSIGNED (dirtype)) + argmax = TYPE_MAX_VALUE (dirtype); + else + argmax = TYPE_MIN_VALUE (dirtype); } + res.argmin = argmin; res.argmax = argmax; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 47e41fc..a79a4a8 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-11-30 Jakub Jelinek + + PR tree-optimization/78586 + * gcc.c-torture/execute/pr78586.c: New test. + 2016-11-30 Janus Weil PR fortran/78573 diff --git a/gcc/testsuite/gcc.c-torture/execute/pr78586.c b/gcc/testsuite/gcc.c-torture/execute/pr78586.c new file mode 100644 index 0000000..6982534 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr78586.c @@ -0,0 +1,17 @@ +/* PR tree-optimization/78586 */ + +void +foo (unsigned long x) +{ + char a[30]; + unsigned long b = __builtin_sprintf (a, "%lu", x); + if (b != 4) + __builtin_abort (); +} + +int +main () +{ + foo (1000); + return 0; +} -- 2.7.4