aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite
diff options
context:
space:
mode:
authorMartin Sebor <msebor@redhat.com>2019-11-22 16:39:37 +0000
committerMartin Sebor <msebor@gcc.gnu.org>2019-11-22 09:39:37 -0700
commitd2f8402a0886db2b2d33ed09e51cf27cc85db5e4 (patch)
treee3ef393eca7df548503d7fd5232b88831573fd91 /gcc/testsuite
parentaa84ec8415bd413b693e5ea0610e98f3b6216ad5 (diff)
downloadgcc-d2f8402a0886db2b2d33ed09e51cf27cc85db5e4.zip
gcc-d2f8402a0886db2b2d33ed09e51cf27cc85db5e4.tar.gz
gcc-d2f8402a0886db2b2d33ed09e51cf27cc85db5e4.tar.bz2
PR tree-optimization/92501 - strncmp with constant unterminated arrays not folded
gcc/testsuite/ChangeLog: PR tree-optimization/92501 * gcc.dg/strcmpopt_7.c: New test. gcc/ChangeLog: PR tree-optimization/92501 * gimple-fold.c ((gimple_fold_builtin_string_compare): Let strncmp handle unterminated arrays. Rename local variables for clarity. From-SVN: r278621
Diffstat (limited to 'gcc/testsuite')
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/gcc.dg/strcmpopt_7.c119
2 files changed, 124 insertions, 0 deletions
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index ac0b1ad..f602c9b 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2019-11-22 Martin Sebor <msebor@redhat.com>
+
+ PR tree-optimization/92501
+ * gcc.dg/strcmpopt_7.c: New test.
+
2019-11-22 Richard Sandiford <richard.sandiford@arm.com>
* gcc.dg/vect/vect-widen-mult-u8.c: Disable epilogue loop
diff --git a/gcc/testsuite/gcc.dg/strcmpopt_7.c b/gcc/testsuite/gcc.dg/strcmpopt_7.c
new file mode 100644
index 0000000..5e71317
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/strcmpopt_7.c
@@ -0,0 +1,119 @@
+/* PR tree-optimization/92501 - strncmp with constant unterminated arrays
+ not folded
+ { dg-do compile }
+ { dg-options "-O1 -Wall -fdump-tree-forwprop1" } */
+
+/* Unterminated arrays of the size encoded in name. */
+const char a1[] = { '1' };
+const char a12[] = { '1', '2' };
+const char a112[] = { '1', '1', '2' };
+const char a123[] = { '1', '2', '3' };
+
+/* Nul-terminated strings of the length encoded in name. */
+const char s[] = "";
+const char s1[] = "1";
+const char s12[] = "12";
+const char s112[] = "112";
+const char s123[] = "123";
+
+extern void failure_on_line (int);
+
+/* Verify that the test in 'if (EQL strncmp (S, T, N))' is folded. */
+#define T(eql, s, t, n) do { \
+ if (!(eql __builtin_strncmp (s, t, n))) \
+ failure_on_line (__LINE__); \
+ } while (0)
+
+
+void test (void)
+{
+ /* Mixed array and string. */
+ T (0 ==, a1, "", 0);
+ T (0 ==, a1, s, 0);
+ T (0 !=, a1, "", 1);
+ T (0 !=, a1, s, 1);
+
+ /* The following two are safe to fold because while strncmp compares
+ at most N bytes it doesn't compare any bytes past the first nul. */
+ T (0 !=, a1, "", 9);
+ T (0 !=, a1, s, 9);
+
+ T (0 ==, a1, "1", 0);
+ T (0 ==, a1, s1, 0);
+ T (0 ==, a1, "1", 1);
+ T (0 ==, a1, s1, 1);
+ T (0 ==, a1, "12", 1);
+ T (0 ==, a1, s12, 1);
+
+ /* As above, the following three are also safe to fold. */
+ T (0 !=, a1, s12 + 1, 1);
+ T (0 !=, a1, s12 + 1, 2);
+ T (0 !=, a1, s12 + 1, 9);
+
+ T (0 ==, a12, s, 0);
+ T (0 ==, a12, "", 0);
+ T (0 ==, a12, s1, 0);
+ T (0 ==, a12, "1", 0);
+ T (0 ==, a12, s1, 1);
+ T (0 ==, a12, "1", 1);
+ T (0 !=, a12, s1, 2);
+ T (0 !=, a12, "1", 2);
+ T (0 ==, a12, s12, 0);
+ T (0 ==, a12, "12", 0);
+ T (0 ==, a12, s12, 1);
+ T (0 ==, a12, "12", 1);
+ T (0 ==, a12, s12, 2);
+ T (0 ==, a12, "12", 2);
+ T (0 ==, a12, s123, 2);
+ T (0 ==, a12, "123", 2);
+
+ T (0 ==, a12 + 0, s123 + 1, 0);
+ T (0 !=, a12 + 0, s123 + 1, 1);
+ T (0 !=, a12 + 0, s123 + 1, 2);
+ T (0 ==, a12 + 1, s123 + 0, 0);
+ T (0 !=, a12 + 1, s123 + 0, 1);
+ T (0 !=, a12 + 1, s123 + 0, 2);
+ T (0 ==, a12 + 1, s123 + 1, 1);
+ T (0 !=, a12 + 1, s123 + 2, 1);
+ T (0 !=, a12 + 1, s123 + 3, 1);
+
+ T (0 ==, a12 + 1, "123" + 1, 1);
+ T (0 !=, a12 + 1, "123" + 2, 1);
+ T (0 !=, a12 + 1, "123" + 3, 1);
+ T (0 !=, a12 + 1, "123" + 3, 9);
+
+ /* Both arguments arrays. */
+ T (0 ==, a112 + 0, a1, 1);
+ T (0 ==, a112 + 1, a1, 1);
+ T (0 !=, a112 + 2, a1, 1);
+
+ T (0 ==, a1, a112 + 0, 1);
+ T (0 ==, a1, a112 + 1, 1);
+ T (0 !=, a1, a112 + 2, 1);
+
+ T (0 ==, a112 + 0, a12, 0);
+ T (0 ==, a112 + 0, a12, 1);
+ T (0 !=, a112 + 0, a12, 2);
+
+ T (0 ==, a112 + 1, a12, 2);
+ T (0 !=, a112 + 1, a12 + 1, 1);
+ T (0 ==, a112 + 2, a12 + 1, 1);
+
+ /* Mixed array and string. */
+ T (0 ==, s112 + 0, a12, 0);
+ T (0 ==, s112 + 0, a12, 1);
+ T (0 !=, s112 + 0, a12, 2);
+
+ T (0 ==, s112 + 1, a12, 0);
+ T (0 ==, s112 + 1, a12, 1);
+ T (0 ==, s112 + 1, a12, 2);
+ T (0 !=, s112 + 2, a12, 2);
+
+ T (0 ==, a112 + 0, s1, 1);
+ T (0 ==, a112 + 1, s1, 1);
+ T (0 !=, a112 + 2, s1, 1);
+}
+
+/* { dg-final { scan-tree-dump-not "strcmp" "forwprop1" } }
+ { dg-final { scan-tree-dump-not "strncmp" "forwprop1" } }
+ { dg-final { scan-tree-dump-not "failure_on_line_" "forwprop1" } } */