Coverage Report

Created: 2017-04-27 19:33

/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
122k
{
17
1.16M
  for (; 
*s1.16M
;
s++1.04M
)
18
1.04M
    isl_hash_byte(hash, *s);
19
122k
  return hash;
20
122k
}
21
22
uint32_t isl_hash_mem(uint32_t hash, const void *p, size_t len)
23
12.8k
{
24
12.8k
  int i;
25
12.8k
  const char *s = p;
26
115k
  for (i = 0; 
i < len115k
;
++i102k
)
27
102k
    isl_hash_byte(hash, s[i]);
28
12.8k
  return hash;
29
12.8k
}
30
31
static unsigned int round_up(unsigned int v)
32
236k
{
33
236k
  int old_v = v;
34
236k
35
807k
  while (
v807k
)
{570k
36
570k
    old_v = v;
37
570k
    v ^= v & -v;
38
570k
  }
39
236k
  return old_v << 1;
40
236k
}
41
42
int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
43
      int min_size)
44
236k
{
45
236k
  size_t size;
46
236k
47
236k
  if (!table)
48
0
    return -1;
49
236k
50
236k
  
if (236k
min_size < 2236k
)
51
106k
    min_size = 2;
52
236k
  table->bits = ffs(round_up(4 * (min_size + 1) / 3 - 1)) - 1;
53
236k
  table->n = 0;
54
236k
55
236k
  size = 1 << table->bits;
56
236k
  table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
57
236k
            size);
58
236k
  if (!table->entries)
59
6
    return -1;
60
236k
61
236k
  return 0;
62
236k
}
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
3.32k
{
80
3.32k
  int n;
81
3.32k
  size_t old_size, size;
82
3.32k
  struct isl_hash_table_entry *entries;
83
3.32k
  uint32_t h;
84
3.32k
85
3.32k
  entries = table->entries;
86
3.32k
  old_size = 1 << table->bits;
87
3.32k
  size = 2 * old_size;
88
3.32k
  table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
89
3.32k
            size);
90
3.32k
  if (
!table->entries3.32k
)
{0
91
0
    table->entries = entries;
92
0
    return -1;
93
0
  }
94
3.32k
95
3.32k
  n = table->n;
96
3.32k
  table->n = 0;
97
3.32k
  table->bits++;
98
3.32k
99
30.9k
  for (h = 0; 
h < old_size30.9k
;
++h27.6k
)
{27.6k
100
27.6k
    struct isl_hash_table_entry *entry;
101
27.6k
102
27.6k
    if (!entries[h].data)
103
6.91k
      continue;
104
27.6k
105
20.7k
    entry = isl_hash_table_find(ctx, table, entries[h].hash,
106
20.7k
              &no, NULL, 1);
107
20.7k
    if (
!entry20.7k
)
{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
20.7k
115
20.7k
    *entry = entries[h];
116
20.7k
  }
117
3.32k
118
3.32k
  free(entries);
119
3.32k
120
3.32k
  return 0;
121
3.32k
}
122
123
struct isl_hash_table *isl_hash_table_alloc(struct isl_ctx *ctx, int min_size)
124
13.1k
{
125
13.1k
  struct isl_hash_table *table = NULL;
126
13.1k
127
13.1k
  table = isl_alloc_type(ctx, struct isl_hash_table);
128
13.1k
  if (isl_hash_table_init(ctx, table, min_size))
129
0
    goto error;
130
13.1k
  return table;
131
0
error:
132
0
  isl_hash_table_free(ctx, table);
133
0
  return NULL;
134
13.1k
}
135
136
void isl_hash_table_clear(struct isl_hash_table *table)
137
236k
{
138
236k
  if (!table)
139
0
    return;
140
236k
  free(table->entries);
141
236k
}
142
143
void isl_hash_table_free(struct isl_ctx *ctx, struct isl_hash_table *table)
144
13.2k
{
145
13.2k
  if (!table)
146
114
    return;
147
13.1k
  isl_hash_table_clear(table);
148
13.1k
  free(table);
149
13.1k
}
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
617k
{
164
617k
  size_t size;
165
617k
  uint32_t h, key_bits;
166
617k
167
617k
  key_bits = isl_hash_bits(key_hash, table->bits);
168
617k
  size = 1 << table->bits;
169
905k
  for (h = key_bits; 
table->entries[h].data905k
;
h = (h+1) % size288k
)
170
462k
    
if (462k
table->entries[h].hash == key_hash &&462k
171
245k
        eq(table->entries[h].data, val))
172
174k
      return &table->entries[h];
173
617k
174
443k
  
if (443k
!reserve443k
)
175
49.2k
    return NULL;
176
443k
177
393k
  
if (393k
4 * table->n >= 3 * size393k
)
{3.32k
178
3.32k
    if (grow_table(ctx, table) < 0)
179
0
      return NULL;
180
3.32k
    return isl_hash_table_find(ctx, table, key_hash, eq, val, 1);
181
3.32k
  }
182
393k
183
390k
  table->n++;
184
390k
  table->entries[h].hash = key_hash;
185
390k
186
390k
  return &table->entries[h];
187
393k
}
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
581k
{
192
581k
  size_t size;
193
581k
  uint32_t h;
194
581k
195
581k
  if (!table->entries)
196
6
    return isl_stat_error;
197
581k
198
581k
  size = 1 << table->bits;
199
10.4M
  for (h = 0; 
h < size10.4M
;
++ h9.91M
)
200
9.97M
    
if (9.97M
table->entries[h].data &&9.97M
201
721k
        fn(&table->entries[h].data, user) < 0)
202
61.5k
      return isl_stat_error;
203
581k
  
204
519k
  return isl_stat_ok;
205
581k
}
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
59.8k
{
211
59.8k
  int h, h2;
212
59.8k
  size_t size;
213
59.8k
214
59.8k
  if (
!table || 59.8k
!entry59.8k
)
215
0
    return;
216
59.8k
217
59.8k
  size = 1 << table->bits;
218
59.8k
  h = entry - table->entries;
219
59.8k
  isl_assert(ctx, h >= 0 && h < size, return);
220
59.8k
221
146k
  
for (h2 = h+1; 59.8k
table->entries[h2 % size].data146k
;
h2++87.0k
)
{87.0k
222
87.0k
    uint32_t bits = isl_hash_bits(table->entries[h2 % size].hash,
223
87.0k
            table->bits);
224
87.0k
    uint32_t offset = (size + bits - (h+1)) % size;
225
87.0k
    if (offset <= h2 - (h+1))
226
35.5k
      continue;
227
51.5k
    *entry = table->entries[h2 % size];
228
51.5k
    h = h2;
229
51.5k
    entry = &table->entries[h % size];
230
51.5k
  }
231
59.8k
232
59.8k
  entry->hash = 0;
233
59.8k
  entry->data = NULL;
234
59.8k
  table->n--;
235
59.8k
}