2018-04-09 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / gcc.dg / strlenopt-1.c
blob910ec672e96038bc5d41a4f2ef772a0dc14b2fa8
1 /* { dg-do run } */
2 /* { dg-options "-O2 -fdump-tree-strlen" } */
4 #include "strlenopt.h"
6 __attribute__((noinline, noclone)) char *
7 foo (char *p, char *r)
9 char *q = malloc (strlen (p) + strlen (r) + 64);
10 if (q == NULL) return NULL;
11 /* This strcpy can be optimized into memcpy, using the remembered
12 strlen (p). */
13 strcpy (q, p);
14 /* These two strcat can be optimized into memcpy. The first one
15 could be even optimized into a *ptr = '/'; store as the '\0'
16 is immediately overwritten. */
17 strcat (q, "/");
18 strcat (q, "abcde");
19 /* This can also be optimized into memcpy. */
20 strcat (q, r);
21 return q;
24 int
25 main ()
27 char *volatile p = "string1";
28 char *volatile r = "string2";
29 char *q = foo (p, r);
30 if (q != NULL)
32 if (strcmp (q, "string1/abcdestring2"))
33 abort ();
34 free (q);
36 return 0;
39 /* { dg-final { scan-tree-dump-times "strlen \\(" 2 "strlen" } } */
40 /* { dg-final { scan-tree-dump-times "memcpy \\(" 4 "strlen" } } */
41 /* { dg-final { scan-tree-dump-times "strcpy \\(" 0 "strlen" } } */
42 /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen" } } */
43 /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen" } } */
44 /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen" } } */