Coverage Report

Created: 2017-06-28 17:40

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/isl/isl_hash.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2009 Katholieke Universiteit Leuven
3
 *
4
 * Use of this software is governed by the MIT license
5
 *
6
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8
 */
9
10
#include <stdlib.h>
11
#include <isl_hash_private.h>
12
#include <isl/ctx.h>
13
#include "isl_config.h"
14
15
uint32_t isl_hash_string(uint32_t hash, const char *s)
16
130k
{
17
1.15M
  for (; 
*s1.15M
;
s++1.02M
)
18
1.02M
    isl_hash_byte(hash, *s);
19
130k
  return hash;
20
130k
}
21
22
uint32_t isl_hash_mem(uint32_t hash, const void *p, size_t len)
23
13.1k
{
24
13.1k
  int i;
25
13.1k
  const char *s = p;
26
118k
  for (i = 0; 
i < len118k
;
++i104k
)
27
104k
    isl_hash_byte(hash, s[i]);
28
13.1k
  return hash;
29
13.1k
}
30
31
static unsigned int round_up(unsigned int v)
32
274k
{
33
274k
  int old_v = v;
34
274k
35
934k
  while (
v934k
)
{660k
36
660k
    old_v = v;
37
660k
    v ^= v & -v;
38
660k
  }
39
274k
  return old_v << 1;
40
274k
}
41
42
int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
43
      int min_size)
44
274k
{
45
274k
  size_t size;
46
274k
47
274k
  if (!table)
48
0
    return -1;
49
274k
50
274k
  
if (274k
min_size < 2274k
)
51
122k
    min_size = 2;
52
274k
  table->bits = ffs(round_up(4 * (min_size + 1) / 3 - 1)) - 1;
53
274k
  table->n = 0;
54
274k
55
274k
  size = 1 << table->bits;
56
274k
  table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
57
274k
            size);
58
274k
  if (!table->entries)
59
6
    return -1;
60
274k
61
274k
  return 0;
62
274k
}
63
64
/* Dummy comparison function that always returns false.
65
 */
66
static int no(const void *entry, const void *val)
67
19.8k
{
68
19.8k
  return 0;
69
19.8k
}
70
71
/* Extend "table" to twice its size.
72
 * Return 0 on success and -1 on error.
73
 *
74
 * We reuse isl_hash_table_find to create entries in the extended table.
75
 * Since all entries in the original table are assumed to be different,
76
 * there is no need to compare them against each other.
77
 */
78
static int grow_table(struct isl_ctx *ctx, struct isl_hash_table *table)
79
4.01k
{
80
4.01k
  int n;
81
4.01k
  size_t old_size, size;
82
4.01k
  struct isl_hash_table_entry *entries;
83
4.01k
  uint32_t h;
84
4.01k
85
4.01k
  entries = table->entries;
86
4.01k
  old_size = 1 << table->bits;
87
4.01k
  size = 2 * old_size;
88
4.01k
  table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
89
4.01k
            size);
90
4.01k
  if (
!table->entries4.01k
)
{0
91
0
    table->entries = entries;
92
0
    return -1;
93
0
  }
94
4.01k
95
4.01k
  n = table->n;
96
4.01k
  table->n = 0;
97
4.01k
  table->bits++;
98
4.01k
99
39.4k
  for (h = 0; 
h < old_size39.4k
;
++h35.4k
)
{35.4k
100
35.4k
    struct isl_hash_table_entry *entry;
101
35.4k
102
35.4k
    if (!entries[h].data)
103
8.85k
      continue;
104
35.4k
105
26.5k
    entry = isl_hash_table_find(ctx, table, entries[h].hash,
106
26.5k
              &no, NULL, 1);
107
26.5k
    if (
!entry26.5k
)
{0
108
0
      table->bits--;
109
0
      free(table->entries);
110
0
      table->entries = entries;
111
0
      table->n = n;
112
0
      return -1;
113
0
    }
114
26.5k
115
26.5k
    *entry = entries[h];
116
26.5k
  }
117
4.01k
118
4.01k
  free(entries);
119
4.01k
120
4.01k
  return 0;
121
4.01k
}
122
123
struct isl_hash_table *isl_hash_table_alloc(struct isl_ctx *ctx, int min_size)
124
14.3k
{
125
14.3k
  struct isl_hash_table *table = NULL;
126
14.3k
127
14.3k
  table = isl_alloc_type(ctx, struct isl_hash_table);
128
14.3k
  if (isl_hash_table_init(ctx, table, min_size))
129
0
    goto error;
130
14.3k
  return table;
131
0
error:
132
0
  isl_hash_table_free(ctx, table);
133
0
  return NULL;
134
14.3k
}
135
136
void isl_hash_table_clear(struct isl_hash_table *table)
137
274k
{
138
274k
  if (!table)
139
0
    return;
140
274k
  free(table->entries);
141
274k
}
142
143
void isl_hash_table_free(struct isl_ctx *ctx, struct isl_hash_table *table)
144
14.4k
{
145
14.4k
  if (!table)
146
108
    return;
147
14.3k
  isl_hash_table_clear(table);
148
14.3k
  free(table);
149
14.3k
}
150
151
/* A dummy entry that can be used to make a distinction between
152
 * a missing entry and an error condition.
153
 * It is used by isl_union_*_find_part_entry.
154
 */
155
static struct isl_hash_table_entry none = { 0, NULL };
156
struct isl_hash_table_entry *isl_hash_table_entry_none = &none;
157
158
struct isl_hash_table_entry *isl_hash_table_find(struct isl_ctx *ctx,
159
        struct isl_hash_table *table,
160
        uint32_t key_hash,
161
        int (*eq)(const void *entry, const void *val),
162
        const void *val, int reserve)
163
699k
{
164
699k
  size_t size;
165
699k
  uint32_t h, key_bits;
166
699k
167
699k
  key_bits = isl_hash_bits(key_hash, table->bits);
168
699k
  size = 1 << table->bits;
169
1.03M
  for (h = key_bits; 
table->entries[h].data1.03M
;
h = (h+1) % size333k
)
170
522k
    
if (522k
table->entries[h].hash == key_hash &&522k
171
260k
        eq(table->entries[h].data, val))
172
188k
      return &table->entries[h];
173
699k
174
510k
  
if (510k
!reserve510k
)
175
57.7k
    return NULL;
176
510k
177
453k
  
if (453k
4 * table->n >= 3 * size453k
)
{4.01k
178
4.01k
    if (grow_table(ctx, table) < 0)
179
0
      return NULL;
180
4.01k
    return isl_hash_table_find(ctx, table, key_hash, eq, val, 1);
181
4.01k
  }
182
453k
183
449k
  table->n++;
184
449k
  table->entries[h].hash = key_hash;
185
449k
186
449k
  return &table->entries[h];
187
453k
}
188
189
isl_stat isl_hash_table_foreach(isl_ctx *ctx, struct isl_hash_table *table,
190
  isl_stat (*fn)(void **entry, void *user), void *user)
191
698k
{
192
698k
  size_t size;
193
698k
  uint32_t h;
194
698k
195
698k
  if (!table->entries)
196
6
    return isl_stat_error;
197
698k
198
698k
  size = 1 << table->bits;
199
12.8M
  for (h = 0; 
h < size12.8M
;
++ h12.2M
)
200
12.2M
    
if (12.2M
table->entries[h].data &&12.2M
201
893k
        fn(&table->entries[h].data, user) < 0)
202
69.6k
      return isl_stat_error;
203
698k
  
204
628k
  return isl_stat_ok;
205
698k
}
206
207
void isl_hash_table_remove(struct isl_ctx *ctx,
208
        struct isl_hash_table *table,
209
        struct isl_hash_table_entry *entry)
210
63.8k
{
211
63.8k
  int h, h2;
212
63.8k
  size_t size;
213
63.8k
214
63.8k
  if (
!table || 63.8k
!entry63.8k
)
215
0
    return;
216
63.8k
217
63.8k
  size = 1 << table->bits;
218
63.8k
  h = entry - table->entries;
219
63.8k
  isl_assert(ctx, h >= 0 && h < size, return);
220
63.8k
221
167k
  
for (h2 = h+1; 63.8k
table->entries[h2 % size].data167k
;
h2++103k
)
{103k
222
103k
    uint32_t bits = isl_hash_bits(table->entries[h2 % size].hash,
223
103k
            table->bits);
224
103k
    uint32_t offset = (size + bits - (h+1)) % size;
225
103k
    if (offset <= h2 - (h+1))
226
51.2k
      continue;
227
51.9k
    *entry = table->entries[h2 % size];
228
51.9k
    h = h2;
229
51.9k
    entry = &table->entries[h % size];
230
51.9k
  }
231
63.8k
232
63.8k
  entry->hash = 0;
233
63.8k
  entry->data = NULL;
234
63.8k
  table->n--;
235
63.8k
}