Coverage Report

Created: 2019-04-21 11:35

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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
59.3k
{
17
507k
  for (; *s; 
s++448k
)
18
448k
    isl_hash_byte(hash, *s);
19
59.3k
  return hash;
20
59.3k
}
21
22
uint32_t isl_hash_mem(uint32_t hash, const void *p, size_t len)
23
10.3k
{
24
10.3k
  int i;
25
10.3k
  const char *s = p;
26
92.9k
  for (i = 0; i < len; 
++i82.6k
)
27
82.6k
    isl_hash_byte(hash, s[i]);
28
10.3k
  return hash;
29
10.3k
}
30
31
static unsigned int round_up(unsigned int v)
32
131k
{
33
131k
  int old_v = v;
34
131k
35
446k
  while (v) {
36
315k
    old_v = v;
37
315k
    v ^= v & -v;
38
315k
  }
39
131k
  return old_v << 1;
40
131k
}
41
42
int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
43
      int min_size)
44
131k
{
45
131k
  size_t size;
46
131k
47
131k
  if (!table)
48
0
    return -1;
49
131k
50
131k
  if (min_size < 2)
51
55.6k
    min_size = 2;
52
131k
  table->bits = ffs(round_up(4 * (min_size + 1) / 3 - 1)) - 1;
53
131k
  table->n = 0;
54
131k
55
131k
  size = 1 << table->bits;
56
131k
  table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
57
131k
            size);
58
131k
  if (!table->entries)
59
3
    return -1;
60
131k
61
131k
  return 0;
62
131k
}
63
64
/* Dummy comparison function that always returns false.
65
 */
66
static int no(const void *entry, const void *val)
67
15.2k
{
68
15.2k
  return 0;
69
15.2k
}
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
1.64k
{
80
1.64k
  int n;
81
1.64k
  size_t old_size, size;
82
1.64k
  struct isl_hash_table_entry *entries;
83
1.64k
  uint32_t h;
84
1.64k
85
1.64k
  entries = table->entries;
86
1.64k
  old_size = 1 << table->bits;
87
1.64k
  size = 2 * old_size;
88
1.64k
  table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
89
1.64k
            size);
90
1.64k
  if (!table->entries) {
91
0
    table->entries = entries;
92
0
    return -1;
93
0
  }
94
1.64k
95
1.64k
  n = table->n;
96
1.64k
  table->n = 0;
97
1.64k
  table->bits++;
98
1.64k
99
17.5k
  for (h = 0; h < old_size; 
++h15.9k
) {
100
15.9k
    struct isl_hash_table_entry *entry;
101
15.9k
102
15.9k
    if (!entries[h].data)
103
3.98k
      continue;
104
11.9k
105
11.9k
    entry = isl_hash_table_find(ctx, table, entries[h].hash,
106
11.9k
              &no, NULL, 1);
107
11.9k
    if (!entry) {
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
11.9k
115
11.9k
    *entry = entries[h];
116
11.9k
  }
117
1.64k
118
1.64k
  free(entries);
119
1.64k
120
1.64k
  return 0;
121
1.64k
}
122
123
struct isl_hash_table *isl_hash_table_alloc(struct isl_ctx *ctx, int min_size)
124
9.51k
{
125
9.51k
  struct isl_hash_table *table = NULL;
126
9.51k
127
9.51k
  table = isl_alloc_type(ctx, struct isl_hash_table);
128
9.51k
  if (isl_hash_table_init(ctx, table, min_size))
129
0
    goto error;
130
9.51k
  return table;
131
0
error:
132
0
  isl_hash_table_free(ctx, table);
133
0
  return NULL;
134
9.51k
}
135
136
void isl_hash_table_clear(struct isl_hash_table *table)
137
131k
{
138
131k
  if (!table)
139
0
    return;
140
131k
  free(table->entries);
141
131k
}
142
143
void isl_hash_table_free(struct isl_ctx *ctx, struct isl_hash_table *table)
144
9.61k
{
145
9.61k
  if (!table)
146
108
    return;
147
9.51k
  isl_hash_table_clear(table);
148
9.51k
  free(table);
149
9.51k
}
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
409k
{
164
409k
  size_t size;
165
409k
  uint32_t h, key_bits;
166
409k
167
409k
  key_bits = isl_hash_bits(key_hash, table->bits);
168
409k
  size = 1 << table->bits;
169
586k
  for (h = key_bits; table->entries[h].data; 
h = (h+1) % size176k
)
170
299k
    if (table->entries[h].hash == key_hash &&
171
299k
        
eq(table->entries[h].data, val)175k
)
172
122k
      return &table->entries[h];
173
409k
174
409k
  
if (286k
!reserve286k
)
175
41.1k
    return NULL;
176
245k
177
245k
  if (4 * table->n >= 3 * size) {
178
1.64k
    if (grow_table(ctx, table) < 0)
179
0
      return NULL;
180
1.64k
    return isl_hash_table_find(ctx, table, key_hash, eq, val, 1);
181
1.64k
  }
182
244k
183
244k
  table->n++;
184
244k
  table->entries[h].hash = key_hash;
185
244k
186
244k
  return &table->entries[h];
187
244k
}
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
311k
{
192
311k
  size_t size;
193
311k
  uint32_t h;
194
311k
195
311k
  if (!table->entries)
196
3
    return isl_stat_error;
197
311k
198
311k
  size = 1 << table->bits;
199
5.64M
  for (h = 0; h < size; 
++ h5.33M
)
200
5.36M
    if (table->entries[h].data &&
201
5.36M
        
fn(&table->entries[h].data, user) < 0400k
)
202
31.2k
      return isl_stat_error;
203
311k
  
204
311k
  
return isl_stat_ok279k
;
205
311k
}
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
26.8k
{
211
26.8k
  int h, h2;
212
26.8k
  size_t size;
213
26.8k
214
26.8k
  if (!table || !entry)
215
0
    return;
216
26.8k
217
26.8k
  size = 1 << table->bits;
218
26.8k
  h = entry - table->entries;
219
26.8k
  isl_assert(ctx, h >= 0 && h < size, return);
220
26.8k
221
80.2k
  
for (h2 = h+1; 26.8k
table->entries[h2 % size].data;
h2++53.3k
) {
222
53.3k
    uint32_t bits = isl_hash_bits(table->entries[h2 % size].hash,
223
53.3k
            table->bits);
224
53.3k
    uint32_t offset = (size + bits - (h+1)) % size;
225
53.3k
    if (offset <= h2 - (h+1))
226
18.1k
      continue;
227
35.1k
    *entry = table->entries[h2 % size];
228
35.1k
    h = h2;
229
35.1k
    entry = &table->entries[h % size];
230
35.1k
  }
231
26.8k
232
26.8k
  entry->hash = 0;
233
26.8k
  entry->data = NULL;
234
26.8k
  table->n--;
235
26.8k
}