2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / testsuite / gcc.dg / strlenopt-11.c
blob0fb327ced021c180f7d5cb61746ff329b82ac86e
1 /* { dg-do run } */
2 /* { dg-options "-O2 -fdump-tree-strlen" } */
4 #include "strlenopt.h"
6 __attribute__((noinline, noclone)) void
7 fn1 (char *p, const char *z, size_t *lp)
9 char *q, *r, *s;
10 char buf[64];
11 size_t l[11];
12 /* The first strlen stays, all the strcpy calls can be optimized
13 into memcpy and most other strlen calls and all strchr calls
14 optimized away. l[6] = strlen (r); and l[9] = strlen (r); need
15 to stay, because we need to invalidate the knowledge about
16 r strlen after strcpy (q, "jklmnopqrst"). */
17 l[0] = strlen (z);
18 strcpy (buf, z);
19 strcpy (p, "abcde");
20 q = strchr (p, '\0');
21 strcpy (q, "efghi");
22 r = strchr (q, '\0');
23 strcpy (r, buf);
24 l[1] = strlen (p);
25 l[2] = strlen (q);
26 l[3] = strlen (r);
27 strcpy (q, "jklmnopqrst");
28 l[4] = strlen (p);
29 l[5] = strlen (q);
30 l[6] = strlen (r);
31 s = strchr (q, '\0');
32 strcpy (s, buf);
33 l[7] = strlen (p);
34 l[8] = strlen (q);
35 l[9] = strlen (r);
36 l[10] = strlen (s);
37 memcpy (lp, l, sizeof l);
40 int
41 main ()
43 char buf[64];
44 size_t l[11];
45 const char *volatile z = "ABCDEFG";
46 memset (buf, '\0', sizeof buf);
47 fn1 (buf, z, l);
48 if (memcmp (buf, "abcdejklmnopqrstABCDEFG", 24) != 0)
49 abort ();
50 if (l[0] != 7)
51 abort ();
52 if (l[1] != 17 || l[2] != 12 || l[3] != 7)
53 abort ();
54 if (l[4] != 16 || l[5] != 11 || l[6] != 6)
55 abort ();
56 if (l[7] != 23 || l[8] != 18 || l[9] != 13 || l[10] != 7)
57 abort ();
58 return 0;
61 /* { dg-final { scan-tree-dump-times "strlen \\(" 3 "strlen" } } */
62 /* avr has BIGGEST_ALIGNMENT 8, allowing fold_builtin_memory_op
63 to expand the memcpy call at the end of fn1. */
64 /* { dg-final { scan-tree-dump-times "memcpy \\(" 7 "strlen" { target { ! avr-*-* } } } } */
65 /* { dg-final { scan-tree-dump-times "memcpy \\(" 6 "strlen" { target { avr-*-* } } } } */
66 /* { dg-final { scan-tree-dump-times "strcpy \\(" 0 "strlen" } } */
67 /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen" } } */
68 /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen" } } */
69 /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen" } } */
70 /* Where the memcpy is expanded, the assignemts to elements of l are
71 propagated. */
72 /* { dg-final { scan-tree-dump-times " _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]* l.0. = " 1 "strlen" { target { ! avr-*-* } } } } */
73 /* { dg-final { scan-tree-dump-times " _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]* l.6. = " 1 "strlen" { target { ! avr-*-* } } } } */
74 /* { dg-final { scan-tree-dump-times " _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]* l.9. = " 1 "strlen" { target { ! avr-*-* } } } } */
75 /* { dg-final { scan-tree-dump-times " _\[0-9\]* = strlen \\(\[^\n\r\]*;" 3 "strlen" { target { avr-*-* } } } } */
76 /* { dg-final { cleanup-tree-dump "strlen" } } */