summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/strlenopt-86.c
blob: d2029443556843d51b5be585927e9fdf9f128834 (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
/* PR tree-optimization/83821 - local aggregate initialization defeats
   strlen optimization
   Verify that a strlen() call is not eliminated for a pointer to a region
   of memory allocated by calloc() if a byte is written into the region
   that isn't known to be nul.
   { dg-do compile }
   { dg-options "-O2 -fdump-tree-optimized" } */

unsigned n0, n1;

void*
keep_strlen_calloc_store_cst_memset (int i, unsigned a, unsigned b)
{
  char *p = __builtin_calloc (a, 1);

  p[i] = 'x';

  __builtin_memset (p, 0, b);

  n0 = __builtin_strlen (p);

  return p;
}

void*
keep_strlen_calloc_store_var_memset (int i, int x, unsigned a, unsigned b)
{
  char *p = __builtin_calloc (a, 1);

  p[i] = x;

  __builtin_memset (p, 0, b);

  n0 = __builtin_strlen (p);

  return p;
}

void*
keep_strlen_calloc_store_memset_2 (int i, int x, unsigned a, unsigned b, unsigned c)
{
  char *p = __builtin_calloc (a, 1);

  p[i] = x;
  __builtin_memset (p, 0, b);

  n0 = __builtin_strlen (p);

  p[3] = x;
  __builtin_memset (p, 0, c);

  n1 = __builtin_strlen (p);

  return p;
}

/* { dg-final { scan-tree-dump-times "__builtin_strlen" 4 "optimized" } } */