Coverage Report

Created: 2017-11-21 03:47

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_scan.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 <isl_ctx_private.h>
11
#include <isl_map_private.h>
12
#include "isl_basis_reduction.h"
13
#include "isl_scan.h"
14
#include <isl_seq.h>
15
#include "isl_tab.h"
16
#include <isl_val_private.h>
17
#include <isl_vec_private.h>
18
19
struct isl_counter {
20
  struct isl_scan_callback callback;
21
  isl_int count;
22
  isl_int max;
23
};
24
25
static isl_stat increment_counter(struct isl_scan_callback *cb,
26
  __isl_take isl_vec *sample)
27
0
{
28
0
  struct isl_counter *cnt = (struct isl_counter *)cb;
29
0
30
0
  isl_int_add_ui(cnt->count, cnt->count, 1);
31
0
32
0
  isl_vec_free(sample);
33
0
34
0
  if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
35
0
    return isl_stat_ok;
36
0
  return isl_stat_error;
37
0
}
38
39
static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max)
40
0
{
41
0
  struct isl_counter *cnt = (struct isl_counter *)cb;
42
0
43
0
  isl_int_add(cnt->count, cnt->count, max);
44
0
  isl_int_sub(cnt->count, cnt->count, min);
45
0
  isl_int_add_ui(cnt->count, cnt->count, 1);
46
0
47
0
  if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
48
0
    return 0;
49
0
  isl_int_set(cnt->count, cnt->max);
50
0
  return -1;
51
0
}
52
53
/* Call callback->add with the current sample value of the tableau "tab".
54
 */
55
static int add_solution(struct isl_tab *tab, struct isl_scan_callback *callback)
56
0
{
57
0
  struct isl_vec *sample;
58
0
59
0
  if (!tab)
60
0
    return -1;
61
0
  sample = isl_tab_get_sample_value(tab);
62
0
  if (!sample)
63
0
    return -1;
64
0
65
0
  return callback->add(callback, sample);
66
0
}
67
68
static isl_stat scan_0D(__isl_take isl_basic_set *bset,
69
  struct isl_scan_callback *callback)
70
0
{
71
0
  struct isl_vec *sample;
72
0
73
0
  sample = isl_vec_alloc(bset->ctx, 1);
74
0
  isl_basic_set_free(bset);
75
0
76
0
  if (!sample)
77
0
    return isl_stat_error;
78
0
79
0
  isl_int_set_si(sample->el[0], 1);
80
0
81
0
  return callback->add(callback, sample);
82
0
}
83
84
/* Look for all integer points in "bset", which is assumed to be bounded,
85
 * and call callback->add on each of them.
86
 *
87
 * We first compute a reduced basis for the set and then scan
88
 * the set in the directions of this basis.
89
 * We basically perform a depth first search, where in each level i
90
 * we compute the range in the i-th basis vector direction, given
91
 * fixed values in the directions of the previous basis vector.
92
 * We then add an equality to the tableau fixing the value in the
93
 * direction of the current basis vector to each value in the range
94
 * in turn and then continue to the next level.
95
 *
96
 * The search is implemented iteratively.  "level" identifies the current
97
 * basis vector.  "init" is true if we want the first value at the current
98
 * level and false if we want the next value.
99
 * Solutions are added in the leaves of the search tree, i.e., after
100
 * we have fixed a value in each direction of the basis.
101
 */
102
isl_stat isl_basic_set_scan(__isl_take isl_basic_set *bset,
103
  struct isl_scan_callback *callback)
104
0
{
105
0
  unsigned dim;
106
0
  struct isl_mat *B = NULL;
107
0
  struct isl_tab *tab = NULL;
108
0
  struct isl_vec *min;
109
0
  struct isl_vec *max;
110
0
  struct isl_tab_undo **snap;
111
0
  int level;
112
0
  int init;
113
0
  enum isl_lp_result res;
114
0
115
0
  if (!bset)
116
0
    return isl_stat_error;
117
0
118
0
  dim = isl_basic_set_total_dim(bset);
119
0
  if (dim == 0)
120
0
    return scan_0D(bset, callback);
121
0
122
0
  min = isl_vec_alloc(bset->ctx, dim);
123
0
  max = isl_vec_alloc(bset->ctx, dim);
124
0
  snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim);
125
0
126
0
  if (!min || !max || !snap)
127
0
    goto error;
128
0
129
0
  tab = isl_tab_from_basic_set(bset, 0);
130
0
  if (!tab)
131
0
    goto error;
132
0
  if (isl_tab_extend_cons(tab, dim + 1) < 0)
133
0
    goto error;
134
0
135
0
  tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
136
0
  if (1)
137
0
    tab = isl_tab_compute_reduced_basis(tab);
138
0
  if (!tab)
139
0
    goto error;
140
0
  B = isl_mat_copy(tab->basis);
141
0
  if (!B)
142
0
    goto error;
143
0
144
0
  level = 0;
145
0
  init = 1;
146
0
147
0
  while (level >= 0) {
148
0
    int empty = 0;
149
0
    if (init) {
150
0
      res = isl_tab_min(tab, B->row[1 + level],
151
0
            bset->ctx->one, &min->el[level], NULL, 0);
152
0
      if (res == isl_lp_empty)
153
0
        empty = 1;
154
0
      if (res == isl_lp_error || res == isl_lp_unbounded)
155
0
        goto error;
156
0
      isl_seq_neg(B->row[1 + level] + 1,
157
0
            B->row[1 + level] + 1, dim);
158
0
      res = isl_tab_min(tab, B->row[1 + level],
159
0
            bset->ctx->one, &max->el[level], NULL, 0);
160
0
      isl_seq_neg(B->row[1 + level] + 1,
161
0
            B->row[1 + level] + 1, dim);
162
0
      isl_int_neg(max->el[level], max->el[level]);
163
0
      if (res == isl_lp_empty)
164
0
        empty = 1;
165
0
      if (res == isl_lp_error || res == isl_lp_unbounded)
166
0
        goto error;
167
0
      snap[level] = isl_tab_snap(tab);
168
0
    } else
169
0
      isl_int_add_ui(min->el[level], min->el[level], 1);
170
0
171
0
    if (empty || isl_int_gt(min->el[level], max->el[level])) {
172
0
      level--;
173
0
      init = 0;
174
0
      if (level >= 0)
175
0
        if (isl_tab_rollback(tab, snap[level]) < 0)
176
0
          goto error;
177
0
      continue;
178
0
    }
179
0
    if (level == dim - 1 && callback->add == increment_counter) {
180
0
      if (increment_range(callback,
181
0
              min->el[level], max->el[level]))
182
0
        goto error;
183
0
      level--;
184
0
      init = 0;
185
0
      if (level >= 0)
186
0
        if (isl_tab_rollback(tab, snap[level]) < 0)
187
0
          goto error;
188
0
      continue;
189
0
    }
190
0
    isl_int_neg(B->row[1 + level][0], min->el[level]);
191
0
    if (isl_tab_add_valid_eq(tab, B->row[1 + level]) < 0)
192
0
      goto error;
193
0
    isl_int_set_si(B->row[1 + level][0], 0);
194
0
    if (level < dim - 1) {
195
0
      ++level;
196
0
      init = 1;
197
0
      continue;
198
0
    }
199
0
    if (add_solution(tab, callback) < 0)
200
0
      goto error;
201
0
    init = 0;
202
0
    if (isl_tab_rollback(tab, snap[level]) < 0)
203
0
      goto error;
204
0
  }
205
0
206
0
  isl_tab_free(tab);
207
0
  free(snap);
208
0
  isl_vec_free(min);
209
0
  isl_vec_free(max);
210
0
  isl_basic_set_free(bset);
211
0
  isl_mat_free(B);
212
0
  return isl_stat_ok;
213
0
error:
214
0
  isl_tab_free(tab);
215
0
  free(snap);
216
0
  isl_vec_free(min);
217
0
  isl_vec_free(max);
218
0
  isl_basic_set_free(bset);
219
0
  isl_mat_free(B);
220
0
  return isl_stat_error;
221
0
}
222
223
isl_stat isl_set_scan(__isl_take isl_set *set,
224
  struct isl_scan_callback *callback)
225
0
{
226
0
  int i;
227
0
228
0
  if (!set || !callback)
229
0
    goto error;
230
0
231
0
  set = isl_set_cow(set);
232
0
  set = isl_set_make_disjoint(set);
233
0
  set = isl_set_compute_divs(set);
234
0
  if (!set)
235
0
    goto error;
236
0
237
0
  for (i = 0; i < set->n; ++i)
238
0
    if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
239
0
          callback) < 0)
240
0
      goto error;
241
0
242
0
  isl_set_free(set);
243
0
  return isl_stat_ok;
244
0
error:
245
0
  isl_set_free(set);
246
0
  return isl_stat_error;
247
0
}
248
249
int isl_basic_set_count_upto(__isl_keep isl_basic_set *bset,
250
  isl_int max, isl_int *count)
251
0
{
252
0
  struct isl_counter cnt = { { &increment_counter } };
253
0
254
0
  if (!bset)
255
0
    return -1;
256
0
257
0
  isl_int_init(cnt.count);
258
0
  isl_int_init(cnt.max);
259
0
260
0
  isl_int_set_si(cnt.count, 0);
261
0
  isl_int_set(cnt.max, max);
262
0
  if (isl_basic_set_scan(isl_basic_set_copy(bset), &cnt.callback) < 0 &&
263
0
      isl_int_lt(cnt.count, cnt.max))
264
0
    goto error;
265
0
266
0
  isl_int_set(*count, cnt.count);
267
0
  isl_int_clear(cnt.max);
268
0
  isl_int_clear(cnt.count);
269
0
270
0
  return 0;
271
0
error:
272
0
  isl_int_clear(cnt.count);
273
0
  return -1;
274
0
}
275
276
int isl_set_count_upto(__isl_keep isl_set *set, isl_int max, isl_int *count)
277
0
{
278
0
  struct isl_counter cnt = { { &increment_counter } };
279
0
280
0
  if (!set)
281
0
    return -1;
282
0
283
0
  isl_int_init(cnt.count);
284
0
  isl_int_init(cnt.max);
285
0
286
0
  isl_int_set_si(cnt.count, 0);
287
0
  isl_int_set(cnt.max, max);
288
0
  if (isl_set_scan(isl_set_copy(set), &cnt.callback) < 0 &&
289
0
      isl_int_lt(cnt.count, cnt.max))
290
0
    goto error;
291
0
292
0
  isl_int_set(*count, cnt.count);
293
0
  isl_int_clear(cnt.max);
294
0
  isl_int_clear(cnt.count);
295
0
296
0
  return 0;
297
0
error:
298
0
  isl_int_clear(cnt.count);
299
0
  return -1;
300
0
}
301
302
int isl_set_count(__isl_keep isl_set *set, isl_int *count)
303
0
{
304
0
  if (!set)
305
0
    return -1;
306
0
  return isl_set_count_upto(set, set->ctx->zero, count);
307
0
}
308
309
/* Count the total number of elements in "set" (in an inefficient way) and
310
 * return the result.
311
 */
312
__isl_give isl_val *isl_set_count_val(__isl_keep isl_set *set)
313
0
{
314
0
  isl_val *v;
315
0
316
0
  if (!set)
317
0
    return NULL;
318
0
  v = isl_val_zero(isl_set_get_ctx(set));
319
0
  v = isl_val_cow(v);
320
0
  if (!v)
321
0
    return NULL;
322
0
  if (isl_set_count(set, &v->n) < 0)
323
0
    v = isl_val_free(v);
324
0
  return v;
325
0
}