From a4f9edf36c8ceebac5c34d6d913871bf1c0f5190 Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Fri, 3 May 2019 10:44:17 +0000 Subject: [PATCH] re PR tree-optimization/87314 (pointless comparison of malloc result to a string not eliminated) 2019-05-03 Richard Biener PR middle-end/87314 * match.pd (cmp (convert1?@2 addr@0) (convert2? addr@1)): Handle STRING_CST vs DECL or STRING_CST. * gcc.dg/pr87314-1.c: New testcase. From-SVN: r270845 --- gcc/ChangeLog | 6 ++++++ gcc/match.pd | 19 ++++++++++++++++++- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr87314-1.c | 11 +++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/pr87314-1.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 80c34bea466..e9def2fc5d7 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2019-05-03 Richard Biener + + PR middle-end/87314 + * match.pd (cmp (convert1?@2 addr@0) (convert2? addr@1)): + Handle STRING_CST vs DECL or STRING_CST. + 2019-05-03 Richard Biener PR tree-optimization/88963 diff --git a/gcc/match.pd b/gcc/match.pd index 039ddacfed1..ec03a680e33 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3905,7 +3905,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) equal = (base0 == base1); if (equal == 0) { - if (!DECL_P (base0) || !DECL_P (base1)) + HOST_WIDE_INT ioff0 = -1, ioff1 = -1; + off0.is_constant (&ioff0); + off1.is_constant (&ioff1); + if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST) + || (TREE_CODE (base0) == STRING_CST && DECL_P (base1)) + || (TREE_CODE (base0) == STRING_CST + && TREE_CODE (base1) == STRING_CST + && ioff0 >= 0 && ioff1 >= 0 + && ioff0 < TREE_STRING_LENGTH (base0) + && ioff1 < TREE_STRING_LENGTH (base1) + /* This is a too conservative test that the STRING_CSTs + will not end up being string-merged. */ + && strncmp (TREE_STRING_POINTER (base0) + ioff0, + TREE_STRING_POINTER (base1) + ioff1, + MIN (TREE_STRING_LENGTH (base0) - ioff0, + TREE_STRING_LENGTH (base1) - ioff1)) != 0)) + ; + else if (!DECL_P (base0) || !DECL_P (base1)) equal = 2; else if (cmp != EQ_EXPR && cmp != NE_EXPR) equal = 2; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 57a07d167e5..0e23e5741d5 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-05-03 Richard Biener + + PR middle-end/87314 + * gcc.dg/pr87314-1.c: New testcase. + 2019-05-03 Richard Biener PR tree-optimization/88963 diff --git a/gcc/testsuite/gcc.dg/pr87314-1.c b/gcc/testsuite/gcc.dg/pr87314-1.c new file mode 100644 index 00000000000..4dc85c8eee6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr87314-1.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-original" } */ + +int f(){ int a; return &a==(void *)"hello"; } +int g(){ return "bye"=="hello"; } +int h() { return "bye"=="hellobye"+5; } + +/* { dg-final { scan-tree-dump-times "hello" 1 "original" } } */ +/* The test in h() should be retained because the result depends on + string merging. */ +/* { dg-final { scan-assembler "hello" } } */ -- 2.30.2