1 /* This test needs runtime that provides stpcpy and mempcpy functions. */
2 /* { dg-do run { target *-*-linux* } } */
3 /* { dg-options "-O2 -fdump-tree-strlen" } */
8 __attribute__((noinline
, noclone
)) char *
9 fn1 (char *p
, size_t *l1
, size_t *l2
)
11 char *a
= mempcpy (p
, "abcde", 6);
12 /* This strlen needs to stay. */
13 size_t la
= strlen (a
);
14 /* This strlen can be optimized into 5. */
15 size_t lp
= strlen (p
);
21 __attribute__((noinline
, noclone
)) char *
22 fn2 (char *p
, const char *q
, size_t *l1
, size_t *l2
, size_t *l3
)
24 /* This strlen needs to stay. */
25 size_t lq
= strlen (q
);
26 char *a
= mempcpy (p
, q
, lq
+ 1);
27 /* This strlen needs to stay. */
28 size_t la
= strlen (a
);
29 /* This strlen can be optimized into lq. */
30 size_t lp
= strlen (p
);
37 __attribute__((noinline
, noclone
)) char *
38 fn3 (char *p
, size_t *l1
, size_t *l2
)
40 char *a
= stpcpy (p
, "abcde");
41 /* This strlen can be optimized into 0. */
42 size_t la
= strlen (a
);
43 /* This strlen can be optimized into 5. */
44 size_t lp
= strlen (p
);
50 __attribute__((noinline
, noclone
)) char *
51 fn4 (char *p
, const char *q
, size_t *l1
, size_t *l2
, size_t *l3
)
53 /* This strlen needs to stay. */
54 size_t lq
= strlen (q
);
55 char *a
= stpcpy (p
, q
);
56 /* This strlen can be optimized into 0. */
57 size_t la
= strlen (a
);
58 /* This strlen can be optimized into lq. */
59 size_t lp
= strlen (p
);
66 __attribute__((noinline
, noclone
)) char *
67 fn5 (char *p
, const char *q
, size_t *l1
, size_t *l2
)
69 char *a
= stpcpy (p
, q
);
70 /* This strlen can be optimized into 0. */
71 size_t la
= strlen (a
);
72 /* This strlen can be optimized into a - p. */
73 size_t lp
= strlen (p
);
83 const char *volatile q
= "ABCDEFGH";
85 memset (buf
, '\0', sizeof buf
);
86 memset (buf
+ 6, 'z', 7);
87 if (fn1 (buf
, &l1
, &l2
) != buf
+ 6 || l1
!= 7 || l2
!= 5
88 || memcmp (buf
, "abcde\0zzzzzzz", 14) != 0)
90 if (fn2 (buf
, q
, &l1
, &l2
, &l3
) != buf
+ 9 || l1
!= 8 || l2
!= 4 || l3
!= 8
91 || memcmp (buf
, "ABCDEFGH\0zzzz", 14) != 0)
93 if (fn3 (buf
, &l1
, &l2
) != buf
+ 5 || l1
!= 0 || l2
!= 5
94 || memcmp (buf
, "abcde\0GH\0zzzz", 14) != 0)
98 if (fn4 (buf
, q
, &l1
, &l2
, &l3
) != buf
+ 8 || l1
!= 8 || l2
!= 0 || l3
!= 8
99 || memcmp (buf
, "ABCDEFGH\0zzzz", 14) != 0)
101 memset (buf
, 'm', 9);
102 if (fn5 (buf
, q
, &l1
, &l2
) != buf
+ 8 || l1
!= 0 || l2
!= 8
103 || memcmp (buf
, "ABCDEFGH\0zzzz", 14) != 0)
108 /* { dg-final { scan-tree-dump-times "strlen \\(" 4 "strlen" } } */
109 /* { dg-final { scan-tree-dump-times "memcpy \\(" 1 "strlen" } } */
110 /* { dg-final { scan-tree-dump-times "mempcpy \\(" 2 "strlen" } } */
111 /* { dg-final { scan-tree-dump-times "strcpy \\(" 0 "strlen" } } */
112 /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen" } } */
113 /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen" } } */
114 /* { dg-final { scan-tree-dump-times "stpcpy \\(" 2 "strlen" } } */
115 /* { dg-final { cleanup-tree-dump "strlen" } } */