Coverage Report

Created: 2018-04-24 22:41

/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
156k
{
17
1.50M
  for (; *s; 
s++1.34M
)
18
1.34M
    isl_hash_byte(hash, *s);
19
156k
  return hash;
20
156k
}
21
22
uint32_t isl_hash_mem(uint32_t hash, const void *p, size_t len)
23
11.6k
{
24
11.6k
  int i;
25
11.6k
  const char *s = p;
26
105k
  for (i = 0; i < len; 
++i93.3k
)
27
93.3k
    isl_hash_byte(hash, s[i]);
28
11.6k
  return hash;
29
11.6k
}
30
31
static unsigned int round_up(unsigned int v)
32
381k
{
33
381k
  int old_v = v;
34
381k
35
1.30M
  while (v) {
36
924k
    old_v = v;
37
924k
    v ^= v & -v;
38
924k
  }
39
381k
  return old_v << 1;
40
381k
}
41
42
int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
43
      int min_size)
44
381k
{
45
381k
  size_t size;
46
381k
47
381k
  if (!table)
48
0
    return -1;
49
381k
50
381k
  if (min_size < 2)
51
164k
    min_size = 2;
52
381k
  table->bits = ffs(round_up(4 * (min_size + 1) / 3 - 1)) - 1;
53
381k
  table->n = 0;
54
381k
55
381k
  size = 1 << table->bits;
56
381k
  table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
57
381k
            size);
58
381k
  if (!table->entries)
59
8
    return -1;
60
381k
61
381k
  return 0;
62
381k
}
63
64
/* Dummy comparison function that always returns false.
65
 */
66
static int no(const void *entry, const void *val)
67
99.4k
{
68
99.4k
  return 0;
69
99.4k
}
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.77k
{
80
4.77k
  int n;
81
4.77k
  size_t old_size, size;
82
4.77k
  struct isl_hash_table_entry *entries;
83
4.77k
  uint32_t h;
84
4.77k
85
4.77k
  entries = table->entries;
86
4.77k
  old_size = 1 << table->bits;
87
4.77k
  size = 2 * old_size;
88
4.77k
  table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
89
4.77k
            size);
90
4.77k
  if (!table->entries) {
91
0
    table->entries = entries;
92
0
    return -1;
93
0
  }
94
4.77k
95
4.77k
  n = table->n;
96
4.77k
  table->n = 0;
97
4.77k
  table->bits++;
98
4.77k
99
48.9k
  for (h = 0; h < old_size; 
++h44.1k
) {
100
44.1k
    struct isl_hash_table_entry *entry;
101
44.1k
102
44.1k
    if (!entries[h].data)
103
11.0k
      continue;
104
33.1k
105
33.1k
    entry = isl_hash_table_find(ctx, table, entries[h].hash,
106
33.1k
              &no, NULL, 1);
107
33.1k
    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
33.1k
115
33.1k
    *entry = entries[h];
116
33.1k
  }
117
4.77k
118
4.77k
  free(entries);
119
4.77k
120
4.77k
  return 0;
121
4.77k
}
122
123
struct isl_hash_table *isl_hash_table_alloc(struct isl_ctx *ctx, int min_size)
124
26.4k
{
125
26.4k
  struct isl_hash_table *table = NULL;
126
26.4k
127
26.4k
  table = isl_alloc_type(ctx, struct isl_hash_table);
128
26.4k
  if (isl_hash_table_init(ctx, table, min_size))
129
0
    goto error;
130
26.4k
  return table;
131
0
error:
132
0
  isl_hash_table_free(ctx, table);
133
0
  return NULL;
134
26.4k
}
135
136
void isl_hash_table_clear(struct isl_hash_table *table)
137
380k
{
138
380k
  if (!table)
139
0
    return;
140
380k
  free(table->entries);
141
380k
}
142
143
void isl_hash_table_free(struct isl_ctx *ctx, struct isl_hash_table *table)
144
26.5k
{
145
26.5k
  if (!table)
146
108
    return;
147
26.4k
  isl_hash_table_clear(table);
148
26.4k
  free(table);
149
26.4k
}
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
1.13M
{
164
1.13M
  size_t size;
165
1.13M
  uint32_t h, key_bits;
166
1.13M
167
1.13M
  key_bits = isl_hash_bits(key_hash, table->bits);
168
1.13M
  size = 1 << table->bits;
169
1.88M
  for (h = key_bits; table->entries[h].data; 
h = (h+1) % size757k
)
170
1.07M
    if (table->entries[h].hash == key_hash &&
171
1.07M
        
eq(table->entries[h].data, val)694k
)
172
322k
      return &table->entries[h];
173
1.13M
174
1.13M
  
if (809k
!reserve809k
)
175
114k
    return NULL;
176
695k
177
695k
  if (4 * table->n >= 3 * size) {
178
4.77k
    if (grow_table(ctx, table) < 0)
179
0
      return NULL;
180
4.77k
    return isl_hash_table_find(ctx, table, key_hash, eq, val, 1);
181
4.77k
  }
182
690k
183
690k
  table->n++;
184
690k
  table->entries[h].hash = key_hash;
185
690k
186
690k
  return &table->entries[h];
187
690k
}
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
921k
{
192
921k
  size_t size;
193
921k
  uint32_t h;
194
921k
195
921k
  if (!table->entries)
196
8
    return isl_stat_error;
197
921k
198
921k
  size = 1 << table->bits;
199
16.2M
  for (h = 0; h < size; 
++ h15.3M
)
200
15.4M
    if (table->entries[h].data &&
201
15.4M
        
fn(&table->entries[h].data, user) < 01.19M
)
202
98.0k
      return isl_stat_error;
203
921k
  
204
921k
  
return isl_stat_ok823k
;
205
921k
}
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
74.1k
{
211
74.1k
  int h, h2;
212
74.1k
  size_t size;
213
74.1k
214
74.1k
  if (!table || !entry)
215
0
    return;
216
74.1k
217
74.1k
  size = 1 << table->bits;
218
74.1k
  h = entry - table->entries;
219
74.1k
  isl_assert(ctx, h >= 0 && h < size, return);
220
74.1k
221
348k
  
for (h2 = h+1; 74.1k
table->entries[h2 % size].data;
h2++274k
) {
222
274k
    uint32_t bits = isl_hash_bits(table->entries[h2 % size].hash,
223
274k
            table->bits);
224
274k
    uint32_t offset = (size + bits - (h+1)) % size;
225
274k
    if (offset <= h2 - (h+1))
226
53.4k
      continue;
227
221k
    *entry = table->entries[h2 % size];
228
221k
    h = h2;
229
221k
    entry = &table->entries[h % size];
230
221k
  }
231
74.1k
232
74.1k
  entry->hash = 0;
233
74.1k
  entry->data = NULL;
234
74.1k
  table->n--;
235
74.1k
}