common.opt: Add -foptimize-strlen option.
[gcc.git] / gcc / testsuite / gcc.dg / strlenopt-1.c
1 /* { dg-do run } */
2 /* { dg-options "-O2 -fdump-tree-strlen" } */
3
4 #include "strlenopt.h"
5
6 __attribute__((noinline, noclone)) char *
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 /* Due to inefficient PTA (PR50262) the above calls invalidate
20 string length of r, so it is optimized just into strcpy instead
21 of memcpy. */
22 strcat (q, r);
23 return q;
24 }
25
26 int
27 main ()
28 {
29 char *volatile p = "string1";
30 char *volatile r = "string2";
31 char *q = foo (p, r);
32 if (q != NULL)
33 {
34 if (strcmp (q, "string1/abcdestring2"))
35 abort ();
36 free (q);
37 }
38 return 0;
39 }
40
41 /* { dg-final { scan-tree-dump-times "strlen \\(" 2 "strlen" } } */
42 /* { dg-final { scan-tree-dump-times "memcpy \\(" 3 "strlen" } } */
43 /* { dg-final { scan-tree-dump-times "strcpy \\(" 1 "strlen" } } */
44 /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen" } } */
45 /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen" } } */
46 /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen" } } */
47 /* { dg-final { cleanup-tree-dump "strlen" } } */