PR middle-end/77357 - strlen of constant strings not folded
[official-gcc.git] / gcc / testsuite / gcc.dg / strlenopt-51.c
blobcbed11bbf588bf556a1cf78b5fdc3618ee7ddca4
1 /* PR tree-optimization/77357 - strlen of constant strings not folded
2 { dg-do compile }
3 { dg-options "-O2 -Wall -fdump-tree-gimple -fdump-tree-optimized" } */
5 #include "strlenopt.h"
7 #define CONCAT(x, y) x ## y
8 #define CAT(x, y) CONCAT (x, y)
9 #define FAILNAME(name) CAT (call_ ## name ##_on_line_, __LINE__)
11 #define FAIL(name) do { \
12 extern void FAILNAME (name) (void); \
13 FAILNAME (name)(); \
14 } while (0)
16 /* Macro to emit a call to funcation named
17 call_in_true_branch_not_eliminated_on_line_NNN()
18 for each call that's expected to be eliminated. The dg-final
19 scan-tree-dump-time directive at the bottom of the test verifies
20 that no such call appears in output. */
21 #define ELIM(expr) \
22 if (!(expr)) FAIL (in_true_branch_not_eliminated); else (void)0
24 /* Macro to emit a call to a function named
25 call_made_in_{true,false}_branch_on_line_NNN()
26 for each call that's expected to be retained. The dg-final
27 scan-tree-dump-time directive at the bottom of the test verifies
28 that the expected number of both kinds of calls appears in output
29 (a pair for each line with the invocation of the KEEP() macro. */
30 #define KEEP(expr) \
31 if (expr) \
32 FAIL (made_in_true_branch); \
33 else \
34 FAIL (made_in_false_branch)
36 #define T(s, n) ELIM (strlen (s) == n)
39 struct S
41 char a1[1], a2[2], a3[3], a4[4], a5[5], a6[6], a7[7], a8[8], a9[9];
44 #define S0 ""
45 #define S1 "1"
46 #define S2 "12"
47 #define S3 "123"
48 #define S4 "1234"
49 #define S5 "12345"
50 #define S6 "123456"
51 #define S7 "1234567"
52 #define S8 "12345678"
54 const char a9[][9] = { S0, S1, S2, S3, S4, S5, S6, S7, S8 };
56 void test_elim_a9 (int i)
58 ELIM (strlen (&a9[0][i]) > 0);
59 ELIM (strlen (&a9[1][i]) > 1);
60 ELIM (strlen (&a9[2][i]) > 2);
61 ELIM (strlen (&a9[3][i]) > 3);
62 ELIM (strlen (&a9[4][i]) > 4);
63 ELIM (strlen (&a9[5][i]) > 5);
64 ELIM (strlen (&a9[6][i]) > 6);
65 ELIM (strlen (&a9[7][i]) > 7);
66 ELIM (strlen (&a9[8][i]) > 8);
69 const char a9_9[][9][9] = {
70 { S0, S1, S2, S3, S4, S5, S6, S7, S8 },
71 { S1, S2, S3, S4, S5, S6, S7, S8, S0 },
72 { S2, S3, S4, S5, S6, S7, S8, S0, S1 },
73 { S3, S4, S5, S6, S7, S8, S0, S1, S2 },
74 { S4, S5, S6, S7, S8, S0, S1, S2, S3 },
75 { S5, S6, S7, S8, S0, S1, S2, S3, S4 },
76 { S6, S7, S8, S0, S1, S2, S3, S4, S5 },
77 { S7, S8, S0, S1, S2, S3, S4, S5, S6 },
78 { S8, S0, S2, S2, S3, S4, S5, S6, S7 }
81 void test_elim_a9_9 (int i)
83 #undef T
84 #define T(I) \
85 ELIM (strlen (&a9_9[I][0][i]) > (0 + I) % 9); \
86 ELIM (strlen (&a9_9[I][1][i]) > (1 + I) % 9); \
87 ELIM (strlen (&a9_9[I][2][i]) > (2 + i) % 9); \
88 ELIM (strlen (&a9_9[I][3][i]) > (3 + I) % 9); \
89 ELIM (strlen (&a9_9[I][4][i]) > (4 + I) % 9); \
90 ELIM (strlen (&a9_9[I][5][i]) > (5 + I) % 9); \
91 ELIM (strlen (&a9_9[I][6][i]) > (6 + I) % 9); \
92 ELIM (strlen (&a9_9[I][7][i]) > (7 + I) % 9); \
93 ELIM (strlen (&a9_9[I][8][i]) > (8 + I) % 9)
95 T (0); T (1); T (2); T (3); T (4); T (5); T (6); T (7); T (8);
98 #line 1000
100 void test_keep_a9_9 (int i)
102 #undef T
103 #define T(I) \
104 KEEP (strlen (&a9_9[i][I][0]) > (1 + I) % 9); \
105 KEEP (strlen (&a9_9[i][I][1]) > (1 + I) % 9); \
106 KEEP (strlen (&a9_9[i][I][2]) > (2 + I) % 9); \
107 KEEP (strlen (&a9_9[i][I][3]) > (3 + I) % 9); \
108 KEEP (strlen (&a9_9[i][I][4]) > (4 + I) % 9); \
109 KEEP (strlen (&a9_9[i][I][5]) > (5 + I) % 9); \
110 KEEP (strlen (&a9_9[i][I][6]) > (6 + I) % 9); \
111 KEEP (strlen (&a9_9[i][I][7]) > (7 + I) % 9); \
112 KEEP (strlen (&a9_9[i][I][8]) > (8 + I) % 9)
114 T (0); T (1); T (2); T (3); T (4); T (5); T (6); T (7); T (8);
117 /* { dg-final { scan-tree-dump-times "strlen" 72 "gimple" } }
118 { dg-final { scan-tree-dump-times "strlen" 63 "optimized" } }
120 { dg-final { scan-tree-dump-times "call_made_in_true_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 72 "optimized" } }
121 { dg-final { scan-tree-dump-times "call_made_in_false_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 81 "optimized" } } */