1 /* This test needs runtime that provides stpcpy and mempcpy functions. */
2 /* { dg-do run { target *-*-linux* } } */
3 /* { dg-options "-O2 -fdump-tree-strlen" } */
4
5 #define USE_GNU
6 #include "strlenopt.h"
7
8 __attribute__((noinline, noclone)) char *
fn1(char * p,size_t * l1,size_t * l2)9 fn1 (char *p, size_t *l1, size_t *l2)
10 {
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);
16 *l1 = la;
17 *l2 = lp;
18 return a;
19 }
20
21 __attribute__((noinline, noclone)) char *
fn2(char * p,const char * q,size_t * l1,size_t * l2,size_t * l3)22 fn2 (char *p, const char *q, size_t *l1, size_t *l2, size_t *l3)
23 {
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);
31 *l1 = lq;
32 *l2 = la;
33 *l3 = lp;
34 return a;
35 }
36
37 __attribute__((noinline, noclone)) char *
fn3(char * p,size_t * l1,size_t * l2)38 fn3 (char *p, size_t *l1, size_t *l2)
39 {
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);
45 *l1 = la;
46 *l2 = lp;
47 return a;
48 }
49
50 __attribute__((noinline, noclone)) char *
fn4(char * p,const char * q,size_t * l1,size_t * l2,size_t * l3)51 fn4 (char *p, const char *q, size_t *l1, size_t *l2, size_t *l3)
52 {
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);
60 *l1 = lq;
61 *l2 = la;
62 *l3 = lp;
63 return a;
64 }
65
66 __attribute__((noinline, noclone)) char *
fn5(char * p,const char * q,size_t * l1,size_t * l2)67 fn5 (char *p, const char *q, size_t *l1, size_t *l2)
68 {
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);
74 *l1 = la;
75 *l2 = lp;
76 return a;
77 }
78
79 int
main()80 main ()
81 {
82 char buf[64];
83 const char *volatile q = "ABCDEFGH";
84 size_t l1, l2, l3;
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)
89 abort ();
90 if (fn2 (buf, q, &l1, &l2, &l3) != buf + 9 || l1 != 8 || l2 != 4 || l3 != 8
91 || memcmp (buf, "ABCDEFGH\0zzzz", 14) != 0)
92 abort ();
93 if (fn3 (buf, &l1, &l2) != buf + 5 || l1 != 0 || l2 != 5
94 || memcmp (buf, "abcde\0GH\0zzzz", 14) != 0)
95 abort ();
96 l3 = 0;
97 memset (buf, 'n', 9);
98 if (fn4 (buf, q, &l1, &l2, &l3) != buf + 8 || l1 != 8 || l2 != 0 || l3 != 8
99 || memcmp (buf, "ABCDEFGH\0zzzz", 14) != 0)
100 abort ();
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)
104 abort ();
105 return 0;
106 }
107
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" } } */
116