summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/strlenopt-82.c
blob: 8070f6ccf30c8a1898ddcdc54748a4823c6215d6 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* PR tree-optimization/83821 - local aggregate initialization defeats
   strlen optimization

   Avoid exercising targets other than x86_64 in LP64 mode due to PR 83543
   - strlen of a local array member not optimized on some targets
   { dg-do compile { target { { i?86-*-* x86_64-*-* } && lp64 } } }

   { dg-options "-O2 -Wall -fdump-tree-optimized" } */

#include "strlenopt.h"

#define CAT(x, y) x ## y
#define CONCAT(x, y) CAT (x, y)
#define FAILNAME(name) CONCAT (call_ ## name ##_on_line_, __LINE__)

#define FAIL(name) do {				\
    extern void FAILNAME (name) (void);		\
    FAILNAME (name)();				\
  } while (0)

/* Macro to emit a call to function named
     call_in_true_branch_not_eliminated_on_line_NNN()
   for each call that's expected to be eliminated.  The dg-final
   scan-tree-dump-time directive at the bottom of the test verifies
   that no such call appears in output.  */
#define ELIM(expr) \
  if (!(expr)) FAIL (in_true_branch_not_eliminated); else (void)0

/* Macro to emit a call to a function named
     call_made_in_{true,false}_branch_on_line_NNN()
   for each call that's expected to be retained.  The dg-final
   scan-tree-dump-time directive at the bottom of the test verifies
   that the expected number of both kinds of calls appears in output
   (a pair for each line with the invocation of the KEEP() macro.  */
#define KEEP(expr)				\
  if (expr)					\
    FAIL (made_in_true_branch);			\
  else						\
    FAIL (made_in_false_branch)

#define STR10 "0123456789"
#define STR20 STR10 STR10
#define STR30 STR20 STR10
#define STR40 STR20 STR20

void elim_char_array_init_consecutive (void)
{
  char a[][10] = { "1", "12", "123", "1234", "12345", "12345" };

  ELIM (strlen (a[0]) == 1);
  ELIM (strlen (a[1]) == 2);
  ELIM (strlen (a[2]) == 3);
  ELIM (strlen (a[3]) == 4);
  ELIM (strlen (a[4]) == 5);
}

void elim_char_array_cpy_consecutive (void)
{
  char a[5][10];

  strcpy (a[0], "12345");
  strcpy (a[1], "1234");
  strcpy (a[2], "123");
  strcpy (a[3], "12");
  strcpy (a[4], "1");

  ELIM (strlen (a[0]) == 5);
  ELIM (strlen (a[1]) == 4);
  ELIM (strlen (a[2]) == 3);
  ELIM (strlen (a[3]) == 2);
  ELIM (strlen (a[4]) == 1);
}

void elim_clear_char_array_cpy_consecutive (void)
{
  char a[5][10] = { };

  strcpy (a[0], "12345");
  strcpy (a[1], "1234");
  strcpy (a[2], "123");
  strcpy (a[3], "12");
  strcpy (a[4], "1");

  ELIM (strlen (a[0]) == 5);
  ELIM (strlen (a[1]) == 4);
  ELIM (strlen (a[2]) == 3);
  ELIM (strlen (a[3]) == 2);
  ELIM (strlen (a[4]) == 1);
}

struct Consec
{
  char s1[sizeof STR40];
  char s2[sizeof STR40];
  const char *p1;
  const char *p2;
};

void elim_struct_init_consecutive (void)
{
  struct Consec a = { STR10, STR10, STR10, STR10 };

  ELIM (strlen (a.s1) == sizeof STR10 - 1);
  ELIM (strlen (a.s2) == sizeof STR10 - 1);
  ELIM (strlen (a.p1) == sizeof STR10 - 1);
  ELIM (strlen (a.p2) == sizeof STR10 - 1);
}

void elim_struct_array_init_consecutive (void)
{
  struct Consec a[2] = {
    { STR10, STR20, STR30, STR40 },
    { STR40, STR30, STR20, STR10 }
  };

  ELIM (strlen (a[0].s1) == sizeof STR10 - 1);
  ELIM (strlen (a[0].s2) == sizeof STR20 - 1);
  ELIM (strlen (a[0].p1) == sizeof STR30 - 1);
  ELIM (strlen (a[0].p2) == sizeof STR40 - 1);

  ELIM (strlen (a[1].s1) == sizeof STR40 - 1);
  ELIM (strlen (a[1].s2) == sizeof STR30 - 1);
  ELIM (strlen (a[1].p1) == sizeof STR20 - 1);
  ELIM (strlen (a[1].p2) == sizeof STR10 - 1);
}

struct NonConsec
{
  char s1[sizeof STR40];
  int i1;
  char s2[sizeof STR40];
  int i2;
  const char *p1;
  int i3;
  const char *p2;
  int i4;
};

void elim_struct_init_nonconsecutive (void)
{
  struct NonConsec b = { STR10, 123, STR20, 456, b.s1, 789, b.s2, 123 };

  ELIM (strlen (b.s1) == sizeof STR10 - 1);
  ELIM (strlen (b.s2) == sizeof STR20 - 1);
  ELIM (strlen (b.p1) == sizeof STR10 - 1);
  ELIM (strlen (b.p2) == sizeof STR20 - 1);
}

void elim_struct_assign_tmp_nonconsecutive (void)
{
  struct NonConsec b = { "a", 1, "b", 2, "c", 3, "d", 4 };

  b = (struct NonConsec){ STR10, 123, STR20, 456, STR30, 789, STR40, 123 };

  ELIM (strlen (b.s1) == sizeof STR10 - 1);
  ELIM (strlen (b.s2) == sizeof STR20 - 1);
  ELIM (strlen (b.p1) == sizeof STR30 - 1);
  ELIM (strlen (b.p2) == sizeof STR40 - 1);
}

const struct NonConsec bcst = {
  STR40, -1, STR30, -2, STR20, -3, STR10, -4
};

void elim_struct_assign_cst_nonconsecutive (void)
{
  struct NonConsec b = { "a", 1, "b", 2, "c", 3, "d" };

  b = bcst;

  ELIM (strlen (b.s1) == sizeof STR40 - 1);
  ELIM (strlen (b.s2) == sizeof STR30 - 1);
  ELIM (strlen (b.p1) == sizeof STR20 - 1);
  ELIM (strlen (b.p2) == sizeof STR10 - 1);
}

void elim_struct_copy_cst_nonconsecutive (void)
{
  struct NonConsec b = { "a", 1, "b", 2, "c", 3, "d" };
  memcpy (&b, &bcst, sizeof b);

  /* ELIM (strlen (b.s1) == sizeof STR40 - 1);
     ELIM (strlen (b.s2) == sizeof STR30 - 1); */
  ELIM (strlen (b.p1) == sizeof STR20 - 1);
  ELIM (strlen (b.p2) == sizeof STR10 - 1);
}


#line 1000

int sink (void*);

void keep_init_nonconsecutive (void)
{
  struct NonConsec b = {
    STR10, 123, STR20, 456, b.s1, 789, b.s2,
    sink (&b)
  };

  KEEP (strlen (b.s1) == sizeof STR10 - 1);
  KEEP (strlen (b.s2) == sizeof STR10 - 1);
  KEEP (strlen (b.p1) == sizeof STR10 - 1);
  KEEP (strlen (b.p2) == sizeof STR10 - 1);
}

void keep_assign_tmp_nonconsecutive (void)
{
  struct NonConsec b = { "a", 1, "b", 2, "c", 3, "d", 4 };

  b = (struct NonConsec){
    STR10, 123, STR20, 456, STR30, 789, STR40,
    sink (&b)
  };

  KEEP (strlen (b.s1) == sizeof STR10 - 1);
  KEEP (strlen (b.s2) == sizeof STR20 - 1);
  KEEP (strlen (b.p1) == sizeof STR30 - 1);
  KEEP (strlen (b.p2) == sizeof STR40 - 1);
}

/* { dg-final { scan-tree-dump-times "call_in_true_branch_not_eliminated_" 0 "optimized" } }

   { dg-final { scan-tree-dump-times "call_made_in_true_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 8 "optimized" } }
   { dg-final { scan-tree-dump-times "call_made_in_false_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 8 "optimized" } } */