1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/* { dg-do run } */
/* { dg-options "-O2 -fdump-tree-strlen" } */
#include "strlenopt.h"
char temp[30];
volatile int v;
size_t __attribute__ ((noinline, noclone))
f1 (void)
{
char a[30];
v += 1;
memcpy (a, "1234567", 7);
memcpy (a + 7, "89abcdefg", 9);
memcpy (a + 16, "h", 2);
return strlen (a); // This strlen should be optimized into 17.
}
size_t __attribute__ ((noinline, noclone))
f2 (char *a)
{
v += 2;
memcpy (a, "1234567", 7);
memcpy (a + 7, "89abcdefg", 9);
memcpy (a + 16, "h", 2);
return strlen (a); // This strlen should be optimized into 17.
}
size_t __attribute__ ((noinline, noclone))
f3 (void)
{
char a[30];
v += 3;
a[0] = '1';
memcpy (a + 1, "2345678", 8);
return strlen (a); // This strlen should be optimized into 8.
}
size_t __attribute__ ((noinline, noclone))
f4 (char *a)
{
v += 4;
a[0] = '1';
memcpy (a + 1, "2345678", 8);
return strlen (a); // This strlen should be optimized into 8.
}
size_t __attribute__ ((noinline, noclone))
f5 (void)
{
char a[30];
v += 5;
a[0] = '1';
a[1] = '2';
a[2] = '3';
memcpy (a + 3, "456", 3);
a[6] = '7';
a[7] = 0;
return strlen (a); // This strlen should be optimized into 7.
}
size_t __attribute__ ((noinline, noclone))
f6 (char *a)
{
v += 6;
a[0] = '1';
a[1] = '2';
a[2] = '3';
memcpy (a + 3, "456", 3);
a[6] = '7';
a[7] = 0;
return strlen (a); // This strlen should be optimized into 7.
}
size_t __attribute__ ((noinline, noclone))
f7 (void)
{
char a[30];
v += 7;
strcpy (a, "abcde");
int len1 = strlen (a);
a[2] = '_';
int len2 = strlen (a);
return len1 + len2; // This should be optimized into 10.
}
size_t __attribute__ ((noinline, noclone))
f8 (char *a)
{
v += 8;
strcpy (a, "abcde");
int len1 = strlen (a);
a[2] = '_';
int len2 = strlen (a);
return len1 + len2; // This should be optimized into 10.
}
size_t __attribute__ ((noinline, noclone))
f9 (char b)
{
char a[30];
v += 9;
strcpy (a, "foo.bar");
a[4] = b;
a[3] = 0;
return strlen (a); // This should be optimized into 3.
}
size_t __attribute__ ((noinline, noclone))
f10 (char *a, char b)
{
v += 10;
strcpy (a, "foo.bar");
a[4] = b;
a[3] = 0;
return strlen (a); // This should be optimized into 3.
}
size_t __attribute__ ((noinline, noclone))
f11 (void)
{
char a[30];
v += 11;
strcpy (temp, "123456");
memcpy (a, temp, 7);
return strlen (a); // This should be optimized into 6.
}
size_t __attribute__ ((noinline, noclone))
f12 (char *a)
{
v += 12;
strcpy (temp, "123456");
memcpy (a, temp, 7);
return strlen (a); // This should be optimized into 6.
}
size_t __attribute__ ((noinline, noclone))
f13 (void)
{
char a[30];
v += 13;
strcpy (temp, "1234567");
memcpy (a, temp, 7);
a[7] = 0;
return strlen (a); // This should be optimized into 7.
}
size_t __attribute__ ((noinline, noclone))
f14 (char *a)
{
v += 14;
strcpy (temp, "1234567");
memcpy (a, temp, 7);
a[7] = 0;
return strlen (a); // This should be optimized into 7.
}
size_t __attribute__ ((noinline, noclone))
f15 (void)
{
char a[30];
v += 15;
strcpy (temp, "12345679");
memcpy (a, temp, 7);
a[7] = 0;
return strlen (a); // This should be optimized into 7.
}
size_t __attribute__ ((noinline, noclone))
f16 (char *a)
{
v += 16;
strcpy (temp, "123456789");
memcpy (a, temp, 7);
a[7] = 0;
return strlen (a); // This should be optimized into 7.
}
int
main ()
{
char a[30];
if (f1 () != 17 || f2 (a) != 17 || f3 () != 8 || f4 (a) != 8
|| f5 () != 7 || f6 (a) != 7 || f7 () != 10 || f8 (a) != 10
|| f9 ('_') != 3 || f10 (a, '_') != 3 || f11 () != 6 || f12 (a) != 6
|| f13 () != 7 || f14 (a) != 7 || f15 () != 7 || f16 (a) != 7)
abort ();
return 0;
}
/* { dg-final { scan-tree-dump-times "strlen \\(" 0 "strlen1" } } */
|