summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/strlenopt-81.c
blob: f493514cfcc4a5970af553dad8960b6fa3654abf (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
/* PR tree-optimization/91996 - fold non-constant strlen relational expressions
   { dg-do run }
   { dg-options "-O2 -Wall -Wno-unused-local-typedefs" } */

typedef __SIZE_TYPE__ size_t;

#define NOIPA   __attribute__ ((noipa))

#define CONCAT(a, b) a ## b
#define CAT(a, b)    CONCAT (a, b)

/* Used in tests where EXPR is expected to be folded to false.  */
#define ELIM(expr)							\
  if (expr) {								\
    extern void								\
      CAT (CAT (test_on_line_, __LINE__), _not_eliminated)(void);	\
    CAT (CAT (test_on_line_, __LINE__), _not_eliminated)();		\
  } typedef void dummy_type


/* Set the alignment for targets that depend on it in order to
   optimize away the ELIM calls.  See pr92128.  */
__attribute__ ((aligned(4))) char a[32], b[32];

void init (void)
{
  __builtin_strncpy (a, "abcdefgh", sizeof a);
  __builtin_strncpy (b, "0123456789", sizeof b);
}

NOIPA void fail (const char *func)
{
  __builtin_printf ("failure in %s\n", func);
  __builtin_abort ();
}

NOIPA void test_global_cpy_4 (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen < 9) return;

  char *d = a;
  __builtin_memcpy (d, b, 4);

  size_t dlen = __builtin_strlen (d);
  if (dlen != 8)   // cannot be eliminated
    fail ("test_global");
}


NOIPA void test_global_cpy_10 (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen < 9) return;

  char *d = a;
  __builtin_memcpy (d, b, 10);

  size_t dlen = __builtin_strlen (d);
  if (dlen != 10)   // cannot be eliminated
    fail ("test_global_cpy_10");
}

NOIPA void test_global_cpy_11 (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen < 9) return;

  char *d = a;
  __builtin_memcpy (d, b, 11);

  size_t dlen = __builtin_strlen (d);
  if (dlen != 10)   // cannot be eliminated
    fail ("test_global_cpy_11");
}

NOIPA void test_global_cpy_20 (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen < 9) return;

  char *d = a;
  __builtin_memcpy (d, b, 20);

  size_t dlen = __builtin_strlen (d);
  if (dlen != 10)   // cannot be eliminated
    fail ("test_global_cpy_20");
}

NOIPA void test_local_cpy_4 (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen < 9) return;

/* Set the alignment for targets that depend on it in order to
   optimize away the ELIM calls.  See pr92128.  */
  __attribute__ ((aligned(4))) char a[10] = "abcdefgh";
  char *d = a;
  __builtin_memcpy (d, b, 4);

  size_t dlen = __builtin_strlen (d);
  ELIM (dlen != 8);
}

NOIPA void test_local_cpy_10 (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen < 9) return;

  char a[32] = "abcdefgh";
  char *d = a;
  __builtin_memcpy (d, b, 10);

  /* B can be longer than 9 and A can initially be longer than 10
     so the test below cannot be eliminated.  */
  size_t dlen = __builtin_strlen (d);
  if (dlen != 10)
    fail ("test_local_cpy_10");
}

NOIPA void test_local_cpy_11 (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen < 9) return;

  char a[32] = "abcdefgh";
  char *d = a;
  __builtin_memcpy (d, b, 11);

  size_t dlen = __builtin_strlen (d);
  if (dlen != 10)
    fail ("test_global_cpy_20");
}

NOIPA void test_local_cpy_20 (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen < 9) return;

  char a[32] = "abcdefgh";
  char *d = a;
  __builtin_memcpy (d, b, 20);

  size_t dlen = __builtin_strlen (d);
  if (dlen != 10)
    fail ("test_global_cpy_20");
}

NOIPA void test_global_length_eq (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen != 10) return;

  size_t alen = __builtin_strlen (a);
  if (alen != 8) return;

  char *d = a;
  __builtin_memcpy (d, b, 4);

  size_t dlen = __builtin_strlen (d);
  ELIM (dlen != 8);
}


NOIPA void test_global_length_gt (void)
{
  size_t blen = __builtin_strlen (b);
  if (blen < 9) return;

  size_t alen = __builtin_strlen (a);
  if (alen < 8) return;

  char *d = a;
  __builtin_memcpy (d, b, 4);

  size_t dlen = __builtin_strlen (d);
  ELIM (dlen < 8);
}

#define TEST(name) do { init (); test_ ## name (); } while (0)

int main (void)
{
  TEST (local_cpy_4);
  TEST (local_cpy_10);
  TEST (local_cpy_11);
  TEST (local_cpy_20);

  TEST (global_cpy_4);
  TEST (global_cpy_10);
  TEST (global_cpy_11);
  TEST (global_cpy_20);
  TEST (global_length_eq);
  TEST (global_length_gt);
}