PR tree-optimization/81303
[official-gcc.git] / gcc / testsuite / gcc.dg / strlenopt-4.c
blob802e4ca7ce49fca4c4b005a93e03d252e83f7801
1 /* { dg-do run } */
2 /* { dg-options "-O2 -fdump-tree-strlen" } */
4 #include "strlenopt.h"
6 /* If stpcpy can't be used, this is optimized into
7 strcpy (p, q); strcat (p, r); memcpy (p + strlen (p), "abcd", 5);
8 If stpcpy can be used (see strlenopt-4g.c test),
9 this is optimized into
10 memcpy (stpcpy (stpcpy (p, q), r), "abcd", 5); */
11 __attribute__((noinline, noclone)) void
12 foo (char *p, const char *q, const char *r)
14 strcpy (p, q);
15 strcat (p, r);
16 strcat (p, "abcd");
19 /* If stpcpy can't be used, this is optimized into
20 memcpy (p, "abcd", 4); strcpy (p + 4, q); strcat (p, r);
21 If stpcpy can be used, this is optimized into
22 memcpy (p, "abcd", 4); strcpy (stpcpy (p + 4, q), r); */
23 __attribute__((noinline, noclone)) void
24 bar (char *p, const char *q, const char *r)
26 strcpy (p, "abcd");
27 strcat (p, q);
28 strcat (p, r);
31 /* If stpcpy can't be used, this is optimized into
32 strcat (p, q); memcpy (t1 = p + strlen (p), "abcd", 4);
33 strcpy (t1 + 4, r); memcpy (p + strlen (p), "efgh", 5);
34 If stpcpy can be used, this is optimized into
35 t1 = stpcpy (p + strlen (p), q); memcpy (t1, "abcd", 4);
36 memcpy (stpcpy (t1 + 4, r), "efgh", 5); */
37 __attribute__((noinline, noclone)) void
38 baz (char *p, const char *q, const char *r)
40 strcat (p, q);
41 strcat (p, "abcd");
42 strcat (p, r);
43 strcat (p, "efgh");
46 char buf[64];
48 int
49 main ()
51 char *volatile p = buf;
52 const char *volatile q = "ij";
53 const char *volatile r = "klmno";
54 foo (p, q, r);
55 if (memcmp (buf, "ijklmnoabcd\0\0\0\0\0\0\0\0", 20) != 0)
56 abort ();
57 memset (buf, '\0', sizeof buf);
58 bar (p, q, r);
59 if (memcmp (buf, "abcdijklmno\0\0\0\0\0\0\0\0", 20) != 0)
60 abort ();
61 memset (buf, 'v', 3);
62 memset (buf + 3, '\0', -3 + sizeof buf);
63 baz (p, q, r);
64 if (memcmp (buf, "vvvijabcdklmnoefgh\0", 20) != 0)
65 abort ();
66 return 0;
69 /* { dg-final { scan-tree-dump-times "strlen \\(" 3 "strlen" } } */
70 /* { dg-final { scan-tree-dump-times "memcpy \\(" 4 "strlen" } } */
71 /* { dg-final { scan-tree-dump-times "strcpy \\(" 3 "strlen" } } */
72 /* { dg-final { scan-tree-dump-times "strcat \\(" 3 "strlen" } } */
73 /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen" } } */
74 /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen" } } */