From ec97cf390ef39e4f55dcae06c5a5e9a4e81b6044 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Fri, 30 Aug 2019 13:52:29 +0200 Subject: [PATCH] backport: re PR tree-optimization/89314 (ICE in wide_int_to_tree_1, at tree.c:1561) Backported from mainline 2019-02-14 Jakub Jelinek PR tree-optimization/89314 * fold-const.c (fold_binary_loc): Cast strlen argument to const char * before dereferencing it. Formatting fixes. * gcc.dg/pr89314.c: New test. From-SVN: r275110 --- gcc/ChangeLog | 6 ++++++ gcc/fold-const.c | 16 ++++++++++------ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr89314.c | 13 +++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr89314.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 0b4bef2d1562..b0acf9e48fe0 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,6 +1,12 @@ 2019-08-30 Jakub Jelinek Backported from mainline + 2019-02-14 Jakub Jelinek + + PR tree-optimization/89314 + * fold-const.c (fold_binary_loc): Cast strlen argument to + const char * before dereferencing it. Formatting fixes. + 2019-02-13 Jakub Jelinek PR middle-end/89303 diff --git a/gcc/fold-const.c b/gcc/fold-const.c index abe2ee34e7d0..3e1355f7b2f4 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -10726,8 +10726,7 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, strlen(ptr) != 0 => *ptr != 0 Other cases should reduce to one of these two (or a constant) due to the return value of strlen being unsigned. */ - if (TREE_CODE (arg0) == CALL_EXPR - && integer_zerop (arg1)) + if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1)) { tree fndecl = get_callee_fndecl (arg0); @@ -10735,12 +10734,17 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN && call_expr_nargs (arg0) == 1 - && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE) + && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) + == POINTER_TYPE)) { - tree iref = build_fold_indirect_ref_loc (loc, - CALL_EXPR_ARG (arg0, 0)); + tree ptrtype + = build_pointer_type (build_qualified_type (char_type_node, + TYPE_QUAL_CONST)); + tree ptr = fold_convert_loc (loc, ptrtype, + CALL_EXPR_ARG (arg0, 0)); + tree iref = build_fold_indirect_ref_loc (loc, ptr); return fold_build2_loc (loc, code, type, iref, - build_int_cst (TREE_TYPE (iref), 0)); + build_int_cst (TREE_TYPE (iref), 0)); } } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 85e0a2ee2943..1a02952c35b2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,6 +1,11 @@ 2019-08-30 Jakub Jelinek Backported from mainline + 2019-02-14 Jakub Jelinek + + PR tree-optimization/89314 + * gcc.dg/pr89314.c: New test. + 2019-02-13 Jonathan Wakely Jakub Jelinek diff --git a/gcc/testsuite/gcc.dg/pr89314.c b/gcc/testsuite/gcc.dg/pr89314.c new file mode 100644 index 000000000000..d9c53fb52a6c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr89314.c @@ -0,0 +1,13 @@ +/* PR tree-optimization/89314 */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +extern __SIZE_TYPE__ strlen (const float *); +void bar (void); + +void +foo (float *s) +{ + if (strlen (s) > 0) + bar (); +} -- 2.47.2