1 /* { dg-do run } */
2 /* { dg-options "-O2 -fdump-tree-strlen" } */
3
4 #include "strlenopt.h"
5
6 __attribute__((noinline, noclone)) char *
foo(char * p,char * r)7 foo (char *p, char *r)
8 {
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;
22 }
23
24 int
main()25 main ()
26 {
27 char *volatile p = "string1";
28 char *volatile r = "string2";
29 char *q = foo (p, r);
30 if (q != NULL)
31 {
32 if (strcmp (q, "string1/abcdestring2"))
33 abort ();
34 free (q);
35 }
36 return 0;
37 }
38
39 /* { dg-final { scan-tree-dump-times "strlen \\(" 2 "strlen1" } } */
40 /* { dg-final { scan-tree-dump-times "memcpy \\(" 4 "strlen1" } } */
41 /* { dg-final { scan-tree-dump-times "strcpy \\(" 0 "strlen1" } } */
42 /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen1" } } */
43 /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen1" } } */
44 /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen1" } } */
45