Coverage Report

Created: 2018-02-20 23:11

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_map_subtract.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_map_private.h>
11
#include <isl_seq.h>
12
#include <isl/set.h>
13
#include <isl/map.h>
14
#include "isl_tab.h"
15
#include <isl_point_private.h>
16
#include <isl_vec_private.h>
17
18
#include <set_to_map.c>
19
#include <set_from_map.c>
20
21
/* Expand the constraint "c" into "v".  The initial "dim" dimensions
22
 * are the same, but "v" may have more divs than "c" and the divs of "c"
23
 * may appear in different positions in "v".
24
 * The number of divs in "c" is given by "n_div" and the mapping
25
 * of divs in "c" to divs in "v" is given by "div_map".
26
 *
27
 * Although it shouldn't happen in practice, it is theoretically
28
 * possible that two or more divs in "c" are mapped to the same div in "v".
29
 * These divs are then necessarily the same, so we simply add their
30
 * coefficients.
31
 */
32
static void expand_constraint(isl_vec *v, unsigned dim,
33
  isl_int *c, int *div_map, unsigned n_div)
34
251k
{
35
251k
  int i;
36
251k
37
251k
  isl_seq_cpy(v->el, c, 1 + dim);
38
251k
  isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim));
39
251k
40
287k
  for (i = 0; i < n_div; 
++i35.9k
) {
41
35.9k
    int pos = 1 + dim + div_map[i];
42
35.9k
    isl_int_add(v->el[pos], v->el[pos], c[1 + dim + i]);
43
35.9k
  }
44
251k
}
45
46
/* Add all constraints of bmap to tab.  The equalities of bmap
47
 * are added as a pair of inequalities.
48
 */
49
static isl_stat tab_add_constraints(struct isl_tab *tab,
50
  __isl_keep isl_basic_map *bmap, int *div_map)
51
68.0k
{
52
68.0k
  int i;
53
68.0k
  unsigned dim;
54
68.0k
  unsigned tab_total;
55
68.0k
  unsigned bmap_total;
56
68.0k
  isl_vec *v;
57
68.0k
58
68.0k
  if (!tab || !bmap)
59
0
    return isl_stat_error;
60
68.0k
61
68.0k
  tab_total = isl_basic_map_total_dim(tab->bmap);
62
68.0k
  bmap_total = isl_basic_map_total_dim(bmap);
63
68.0k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
64
68.0k
65
68.0k
  if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
66
0
    return isl_stat_error;
67
68.0k
68
68.0k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
69
68.0k
  if (!v)
70
0
    return isl_stat_error;
71
68.0k
72
90.0k
  
for (i = 0; 68.0k
i < bmap->n_eq;
++i21.9k
) {
73
25.5k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
74
25.5k
    if (isl_tab_add_ineq(tab, v->el) < 0)
75
0
      goto error;
76
25.5k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
77
25.5k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
78
25.5k
    if (isl_tab_add_ineq(tab, v->el) < 0)
79
0
      goto error;
80
25.5k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
81
25.5k
    if (tab->empty)
82
3.59k
      break;
83
25.5k
  }
84
68.0k
85
201k
  
for (i = 0; 68.0k
i < bmap->n_ineq;
++i133k
) {
86
157k
    expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
87
157k
    if (isl_tab_add_ineq(tab, v->el) < 0)
88
0
      goto error;
89
157k
    if (tab->empty)
90
24.5k
      break;
91
157k
  }
92
68.0k
93
68.0k
  isl_vec_free(v);
94
68.0k
  return isl_stat_ok;
95
0
error:
96
0
  isl_vec_free(v);
97
0
  return isl_stat_error;
98
68.0k
}
99
100
/* Add a specific constraint of bmap (or its opposite) to tab.
101
 * The position of the constraint is specified by "c", where
102
 * the equalities of bmap are counted twice, once for the inequality
103
 * that is equal to the equality, and once for its negation.
104
 *
105
 * Each of these constraints has been added to "tab" before by
106
 * tab_add_constraints (and later removed again), so there should
107
 * already be a row available for the constraint.
108
 */
109
static isl_stat tab_add_constraint(struct isl_tab *tab,
110
  __isl_keep isl_basic_map *bmap, int *div_map, int c, int oppose)
111
42.8k
{
112
42.8k
  unsigned dim;
113
42.8k
  unsigned tab_total;
114
42.8k
  unsigned bmap_total;
115
42.8k
  isl_vec *v;
116
42.8k
  isl_stat r;
117
42.8k
118
42.8k
  if (!tab || !bmap)
119
0
    return isl_stat_error;
120
42.8k
121
42.8k
  tab_total = isl_basic_map_total_dim(tab->bmap);
122
42.8k
  bmap_total = isl_basic_map_total_dim(bmap);
123
42.8k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
124
42.8k
125
42.8k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
126
42.8k
  if (!v)
127
0
    return isl_stat_error;
128
42.8k
129
42.8k
  if (c < 2 * bmap->n_eq) {
130
10.1k
    if ((c % 2) != oppose)
131
5.50k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
132
5.50k
          1 + bmap_total);
133
10.1k
    if (oppose)
134
10.1k
      
isl_int_sub_ui7.56k
(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
135
10.1k
    expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
136
10.1k
    r = isl_tab_add_ineq(tab, v->el);
137
10.1k
    if (oppose)
138
10.1k
      
isl_int_add_ui7.56k
(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
139
10.1k
    if ((c % 2) != oppose)
140
5.50k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
141
5.50k
          1 + bmap_total);
142
32.7k
  } else {
143
32.7k
    c -= 2 * bmap->n_eq;
144
32.7k
    if (oppose) {
145
27.3k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
146
27.3k
          1 + bmap_total);
147
27.3k
      isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
148
27.3k
    }
149
32.7k
    expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
150
32.7k
    r = isl_tab_add_ineq(tab, v->el);
151
32.7k
    if (oppose) {
152
27.3k
      isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
153
27.3k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
154
27.3k
          1 + bmap_total);
155
27.3k
    }
156
32.7k
  }
157
42.8k
158
42.8k
  isl_vec_free(v);
159
42.8k
  return r;
160
42.8k
}
161
162
static isl_stat tab_add_divs(struct isl_tab *tab,
163
  __isl_keep isl_basic_map *bmap, int **div_map)
164
68.0k
{
165
68.0k
  int i, j;
166
68.0k
  struct isl_vec *vec;
167
68.0k
  unsigned total;
168
68.0k
  unsigned dim;
169
68.0k
170
68.0k
  if (!bmap)
171
0
    return isl_stat_error;
172
68.0k
  if (!bmap->n_div)
173
64.0k
    return isl_stat_ok;
174
4.07k
175
4.07k
  if (!*div_map)
176
3.33k
    *div_map = isl_alloc_array(bmap->ctx, int, bmap->n_div);
177
4.07k
  if (!*div_map)
178
0
    return isl_stat_error;
179
4.07k
180
4.07k
  total = isl_basic_map_total_dim(tab->bmap);
181
4.07k
  dim = total - tab->bmap->n_div;
182
4.07k
  vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
183
4.07k
  if (!vec)
184
0
    return isl_stat_error;
185
4.07k
186
9.42k
  
for (i = 0; 4.07k
i < bmap->n_div;
++i5.34k
) {
187
5.34k
    isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
188
5.34k
    isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
189
6.86k
    for (j = 0; j < i; 
++j1.52k
)
190
5.34k
      
isl_int_set1.52k
(vec->el[2 + dim + (*div_map)[j]],
191
5.34k
          bmap->div[i][2 + dim + j]);
192
8.35k
    for (j = 0; j < tab->bmap->n_div; 
++j3.01k
)
193
6.98k
      if (isl_seq_eq(tab->bmap->div[j],
194
6.98k
          vec->el, 2 + dim + tab->bmap->n_div))
195
3.97k
        break;
196
5.34k
    (*div_map)[i] = j;
197
5.34k
    if (j == tab->bmap->n_div) {
198
1.37k
      vec->size = 2 + dim + tab->bmap->n_div;
199
1.37k
      if (isl_tab_add_div(tab, vec) < 0)
200
0
        goto error;
201
1.37k
    }
202
5.34k
  }
203
4.07k
204
4.07k
  isl_vec_free(vec);
205
4.07k
206
4.07k
  return isl_stat_ok;
207
0
error:
208
0
  isl_vec_free(vec);
209
0
210
0
  return isl_stat_error;
211
4.07k
}
212
213
/* Freeze all constraints of tableau tab.
214
 */
215
static int tab_freeze_constraints(struct isl_tab *tab)
216
68.0k
{
217
68.0k
  int i;
218
68.0k
219
412k
  for (i = 0; i < tab->n_con; 
++i344k
)
220
344k
    if (isl_tab_freeze_constraint(tab, i) < 0)
221
0
      return -1;
222
68.0k
223
68.0k
  return 0;
224
68.0k
}
225
226
/* Check for redundant constraints starting at offset.
227
 * Put the indices of the redundant constraints in index
228
 * and return the number of redundant constraints.
229
 */
230
static int n_non_redundant(isl_ctx *ctx, struct isl_tab *tab,
231
  int offset, int **index)
232
42.9k
{
233
42.9k
  int i, n;
234
42.9k
  int n_test = tab->n_con - offset;
235
42.9k
236
42.9k
  if (isl_tab_detect_redundant(tab) < 0)
237
0
    return -1;
238
42.9k
239
42.9k
  if (n_test == 0)
240
2.27k
    return 0;
241
40.6k
  if (!*index)
242
40.1k
    *index = isl_alloc_array(ctx, int, n_test);
243
40.6k
  if (!*index)
244
0
    return -1;
245
40.6k
246
189k
  
for (n = 0, i = 0; 40.6k
i < n_test;
++i148k
) {
247
148k
    int r;
248
148k
    r = isl_tab_is_redundant(tab, offset + i);
249
148k
    if (r < 0)
250
0
      return -1;
251
148k
    if (r)
252
105k
      continue;
253
42.7k
    (*index)[n++] = i;
254
42.7k
  }
255
40.6k
256
40.6k
  return n;
257
40.6k
}
258
259
/* basic_map_collect_diff calls add on each of the pieces of
260
 * the set difference between bmap and map until the add method
261
 * return a negative value.
262
 */
263
struct isl_diff_collector {
264
  isl_stat (*add)(struct isl_diff_collector *dc,
265
        __isl_take isl_basic_map *bmap);
266
};
267
268
/* Compute the set difference between bmap and map and call
269
 * dc->add on each of the piece until this function returns
270
 * a negative value.
271
 * Return 0 on success and -1 on error.  dc->add returning
272
 * a negative value is treated as an error, but the calling
273
 * function can interpret the results based on the state of dc.
274
 *
275
 * Assumes that map has known divs.
276
 *
277
 * The difference is computed by a backtracking algorithm.
278
 * Each level corresponds to a basic map in "map".
279
 * When a node in entered for the first time, we check
280
 * if the corresonding basic map intersects the current piece
281
 * of "bmap".  If not, we move to the next level.
282
 * Otherwise, we split the current piece into as many
283
 * pieces as there are non-redundant constraints of the current
284
 * basic map in the intersection.  Each of these pieces is
285
 * handled by a child of the current node.
286
 * In particular, if there are n non-redundant constraints,
287
 * then for each 0 <= i < n, a piece is cut off by adding
288
 * constraints 0 <= j < i and adding the opposite of constraint i.
289
 * If there are no non-redundant constraints, meaning that the current
290
 * piece is a subset of the current basic map, then we simply backtrack.
291
 *
292
 * In the leaves, we check if the remaining piece has any integer points
293
 * and if so, pass it along to dc->add.  As a special case, if nothing
294
 * has been removed when we end up in a leaf, we simply pass along
295
 * the original basic map.
296
 */
297
static isl_stat basic_map_collect_diff(__isl_take isl_basic_map *bmap,
298
  __isl_take isl_map *map, struct isl_diff_collector *dc)
299
40.7k
{
300
40.7k
  int i;
301
40.7k
  int modified;
302
40.7k
  int level;
303
40.7k
  int init;
304
40.7k
  isl_bool empty;
305
40.7k
  isl_ctx *ctx;
306
40.7k
  struct isl_tab *tab = NULL;
307
40.7k
  struct isl_tab_undo **snap = NULL;
308
40.7k
  int *k = NULL;
309
40.7k
  int *n = NULL;
310
40.7k
  int **index = NULL;
311
40.7k
  int **div_map = NULL;
312
40.7k
313
40.7k
  empty = isl_basic_map_is_empty(bmap);
314
40.7k
  if (empty) {
315
0
    isl_basic_map_free(bmap);
316
0
    isl_map_free(map);
317
0
    return empty < 0 ? isl_stat_error : isl_stat_ok;
318
0
  }
319
40.7k
320
40.7k
  bmap = isl_basic_map_cow(bmap);
321
40.7k
  map = isl_map_cow(map);
322
40.7k
323
40.7k
  if (!bmap || !map)
324
0
    goto error;
325
40.7k
326
40.7k
  ctx = map->ctx;
327
40.7k
  snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
328
40.7k
  k = isl_alloc_array(map->ctx, int, map->n);
329
40.7k
  n = isl_alloc_array(map->ctx, int, map->n);
330
40.7k
  index = isl_calloc_array(map->ctx, int *, map->n);
331
40.7k
  div_map = isl_calloc_array(map->ctx, int *, map->n);
332
40.7k
  if (!snap || !k || !n || !index || !div_map)
333
0
    goto error;
334
40.7k
335
40.7k
  bmap = isl_basic_map_order_divs(bmap);
336
40.7k
  map = isl_map_order_divs(map);
337
40.7k
338
40.7k
  tab = isl_tab_from_basic_map(bmap, 1);
339
40.7k
  if (!tab)
340
0
    goto error;
341
40.7k
342
40.7k
  modified = 0;
343
40.7k
  level = 0;
344
40.7k
  init = 1;
345
40.7k
346
167k
  while (level >= 0) {
347
140k
    if (level >= map->n) {
348
32.7k
      int empty;
349
32.7k
      struct isl_basic_map *bm;
350
32.7k
      if (!modified) {
351
5.82k
        if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
352
1.32k
          goto error;
353
4.49k
        break;
354
4.49k
      }
355
26.9k
      bm = isl_basic_map_copy(tab->bmap);
356
26.9k
      bm = isl_basic_map_cow(bm);
357
26.9k
      bm = isl_basic_map_update_from_tab(bm, tab);
358
26.9k
      bm = isl_basic_map_simplify(bm);
359
26.9k
      bm = isl_basic_map_finalize(bm);
360
26.9k
      empty = isl_basic_map_is_empty(bm);
361
26.9k
      if (empty)
362
846
        isl_basic_map_free(bm);
363
26.1k
      else if (dc->add(dc, bm) < 0)
364
7.61k
        goto error;
365
19.3k
      if (empty < 0)
366
0
        goto error;
367
19.3k
      level--;
368
19.3k
      init = 0;
369
19.3k
      continue;
370
19.3k
    }
371
107k
    if (init) {
372
68.0k
      int offset;
373
68.0k
      struct isl_tab_undo *snap2;
374
68.0k
      snap2 = isl_tab_snap(tab);
375
68.0k
      if (tab_add_divs(tab, map->p[level],
376
68.0k
           &div_map[level]) < 0)
377
0
        goto error;
378
68.0k
      offset = tab->n_con;
379
68.0k
      snap[level] = isl_tab_snap(tab);
380
68.0k
      if (tab_freeze_constraints(tab) < 0)
381
0
        goto error;
382
68.0k
      if (tab_add_constraints(tab, map->p[level],
383
68.0k
            div_map[level]) < 0)
384
0
        goto error;
385
68.0k
      k[level] = 0;
386
68.0k
      n[level] = 0;
387
68.0k
      if (tab->empty) {
388
25.1k
        if (isl_tab_rollback(tab, snap2) < 0)
389
0
          goto error;
390
25.1k
        level++;
391
25.1k
        continue;
392
25.1k
      }
393
42.9k
      modified = 1;
394
42.9k
      n[level] = n_non_redundant(ctx, tab, offset,
395
42.9k
                &index[level]);
396
42.9k
      if (n[level] < 0)
397
0
        goto error;
398
42.9k
      if (n[level] == 0) {
399
15.9k
        level--;
400
15.9k
        init = 0;
401
15.9k
        continue;
402
15.9k
      }
403
27.0k
      if (isl_tab_rollback(tab, snap[level]) < 0)
404
0
        goto error;
405
27.0k
      if (tab_add_constraint(tab, map->p[level],
406
27.0k
          div_map[level], index[level][0], 1) < 0)
407
0
        goto error;
408
27.0k
      level++;
409
27.0k
      continue;
410
39.5k
    } else {
411
39.5k
      if (k[level] + 1 >= n[level]) {
412
31.5k
        level--;
413
31.5k
        continue;
414
31.5k
      }
415
7.93k
      if (isl_tab_rollback(tab, snap[level]) < 0)
416
0
        goto error;
417
7.93k
      if (tab_add_constraint(tab, map->p[level],
418
7.93k
            div_map[level],
419
7.93k
            index[level][k[level]], 0) < 0)
420
0
        goto error;
421
7.93k
      snap[level] = isl_tab_snap(tab);
422
7.93k
      k[level]++;
423
7.93k
      if (tab_add_constraint(tab, map->p[level],
424
7.93k
            div_map[level],
425
7.93k
            index[level][k[level]], 1) < 0)
426
0
        goto error;
427
7.93k
      level++;
428
7.93k
      init = 1;
429
7.93k
      continue;
430
7.93k
    }
431
107k
  }
432
40.7k
433
40.7k
  isl_tab_free(tab);
434
31.8k
  free(snap);
435
31.8k
  free(n);
436
31.8k
  free(k);
437
81.0k
  for (i = 0; index && i < map->n; 
++i49.2k
)
438
49.2k
    free(index[i]);
439
31.8k
  free(index);
440
81.0k
  for (i = 0; div_map && i < map->n; 
++i49.2k
)
441
49.2k
    free(div_map[i]);
442
31.8k
  free(div_map);
443
31.8k
444
31.8k
  isl_basic_map_free(bmap);
445
31.8k
  isl_map_free(map);
446
31.8k
447
31.8k
  return isl_stat_ok;
448
8.94k
error:
449
8.94k
  isl_tab_free(tab);
450
8.94k
  free(snap);
451
8.94k
  free(n);
452
8.94k
  free(k);
453
24.6k
  for (i = 0; index && i < map->n; 
++i15.7k
)
454
15.7k
    free(index[i]);
455
8.94k
  free(index);
456
24.6k
  for (i = 0; div_map && i < map->n; 
++i15.7k
)
457
15.7k
    free(div_map[i]);
458
8.94k
  free(div_map);
459
8.94k
  isl_basic_map_free(bmap);
460
8.94k
  isl_map_free(map);
461
8.94k
  return isl_stat_error;
462
40.7k
}
463
464
/* A diff collector that actually collects all parts of the
465
 * set difference in the field diff.
466
 */
467
struct isl_subtract_diff_collector {
468
  struct isl_diff_collector dc;
469
  struct isl_map *diff;
470
};
471
472
/* isl_subtract_diff_collector callback.
473
 */
474
static isl_stat basic_map_subtract_add(struct isl_diff_collector *dc,
475
          __isl_take isl_basic_map *bmap)
476
22.9k
{
477
22.9k
  struct isl_subtract_diff_collector *sdc;
478
22.9k
  sdc = (struct isl_subtract_diff_collector *)dc;
479
22.9k
480
22.9k
  sdc->diff = isl_map_union_disjoint(sdc->diff,
481
22.9k
      isl_map_from_basic_map(bmap));
482
22.9k
483
22.9k
  return sdc->diff ? isl_stat_ok : 
isl_stat_error0
;
484
22.9k
}
485
486
/* Return the set difference between bmap and map.
487
 */
488
static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
489
  __isl_take isl_map *map)
490
20.3k
{
491
20.3k
  struct isl_subtract_diff_collector sdc;
492
20.3k
  sdc.dc.add = &basic_map_subtract_add;
493
20.3k
  sdc.diff = isl_map_empty(isl_basic_map_get_space(bmap));
494
20.3k
  if (basic_map_collect_diff(bmap, map, &sdc.dc) < 0) {
495
0
    isl_map_free(sdc.diff);
496
0
    sdc.diff = NULL;
497
0
  }
498
20.3k
  return sdc.diff;
499
20.3k
}
500
501
/* Return an empty map living in the same space as "map1" and "map2".
502
 */
503
static __isl_give isl_map *replace_pair_by_empty( __isl_take isl_map *map1,
504
  __isl_take isl_map *map2)
505
4.74k
{
506
4.74k
  isl_space *space;
507
4.74k
508
4.74k
  space = isl_map_get_space(map1);
509
4.74k
  isl_map_free(map1);
510
4.74k
  isl_map_free(map2);
511
4.74k
  return isl_map_empty(space);
512
4.74k
}
513
514
/* Return the set difference between map1 and map2.
515
 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
516
 *
517
 * If "map1" and "map2" are obviously equal to each other,
518
 * then return an empty map in the same space.
519
 *
520
 * If "map1" and "map2" are disjoint, then simply return "map1".
521
 */
522
static __isl_give isl_map *map_subtract( __isl_take isl_map *map1,
523
  __isl_take isl_map *map2)
524
31.9k
{
525
31.9k
  int i;
526
31.9k
  int equal, disjoint;
527
31.9k
  struct isl_map *diff;
528
31.9k
529
31.9k
  if (!map1 || !map2)
530
0
    goto error;
531
31.9k
532
31.9k
  isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
533
31.9k
534
31.9k
  equal = isl_map_plain_is_equal(map1, map2);
535
31.9k
  if (equal < 0)
536
0
    goto error;
537
31.9k
  if (equal)
538
4.74k
    return replace_pair_by_empty(map1, map2);
539
27.2k
540
27.2k
  disjoint = isl_map_is_disjoint(map1, map2);
541
27.2k
  if (disjoint < 0)
542
0
    goto error;
543
27.2k
  if (disjoint) {
544
15.6k
    isl_map_free(map2);
545
15.6k
    return map1;
546
15.6k
  }
547
11.6k
548
11.6k
  map1 = isl_map_compute_divs(map1);
549
11.6k
  map2 = isl_map_compute_divs(map2);
550
11.6k
  if (!map1 || !map2)
551
0
    goto error;
552
11.6k
553
11.6k
  map1 = isl_map_remove_empty_parts(map1);
554
11.6k
  map2 = isl_map_remove_empty_parts(map2);
555
11.6k
556
11.6k
  diff = isl_map_empty(isl_map_get_space(map1));
557
31.9k
  for (i = 0; i < map1->n; 
++i20.3k
) {
558
20.3k
    struct isl_map *d;
559
20.3k
    d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
560
20.3k
               isl_map_copy(map2));
561
20.3k
    if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
562
20.3k
      
diff = isl_map_union_disjoint(diff, d)9.45k
;
563
10.9k
    else
564
10.9k
      diff = isl_map_union(diff, d);
565
20.3k
  }
566
11.6k
567
11.6k
  isl_map_free(map1);
568
11.6k
  isl_map_free(map2);
569
11.6k
570
11.6k
  return diff;
571
0
error:
572
0
  isl_map_free(map1);
573
0
  isl_map_free(map2);
574
0
  return NULL;
575
11.6k
}
576
577
__isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
578
  __isl_take isl_map *map2)
579
31.9k
{
580
31.9k
  return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
581
31.9k
}
582
583
struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
584
21.2k
{
585
21.2k
  return set_from_map(isl_map_subtract(set_to_map(set1),
586
21.2k
              set_to_map(set2)));
587
21.2k
}
588
589
/* Remove the elements of "dom" from the domain of "map".
590
 */
591
static __isl_give isl_map *map_subtract_domain(__isl_take isl_map *map,
592
  __isl_take isl_set *dom)
593
96
{
594
96
  isl_bool ok;
595
96
  isl_map *ext_dom;
596
96
597
96
  ok = isl_map_compatible_domain(map, dom);
598
96
  if (ok < 0)
599
0
    goto error;
600
96
  if (!ok)
601
96
    
isl_die0
(isl_set_get_ctx(dom), isl_error_invalid,
602
96
      "incompatible spaces", goto error);
603
96
  
604
96
  ext_dom = isl_map_universe(isl_map_get_space(map));
605
96
  ext_dom = isl_map_intersect_domain(ext_dom, dom);
606
96
  return isl_map_subtract(map, ext_dom);
607
0
error:
608
0
  isl_map_free(map);
609
0
  isl_set_free(dom);
610
0
  return NULL;
611
96
}
612
613
__isl_give isl_map *isl_map_subtract_domain(__isl_take isl_map *map,
614
  __isl_take isl_set *dom)
615
96
{
616
96
  return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
617
96
}
618
619
/* Remove the elements of "dom" from the range of "map".
620
 */
621
static __isl_give isl_map *map_subtract_range(__isl_take isl_map *map,
622
  __isl_take isl_set *dom)
623
2
{
624
2
  isl_bool ok;
625
2
  isl_map *ext_dom;
626
2
627
2
  ok = isl_map_compatible_range(map, dom);
628
2
  if (ok < 0)
629
0
    goto error;
630
2
  if (!ok)
631
2
    
isl_die0
(isl_set_get_ctx(dom), isl_error_invalid,
632
2
      "incompatible spaces", goto error);
633
2
  
634
2
  ext_dom = isl_map_universe(isl_map_get_space(map));
635
2
  ext_dom = isl_map_intersect_range(ext_dom, dom);
636
2
  return isl_map_subtract(map, ext_dom);
637
0
error:
638
0
  isl_map_free(map);
639
0
  isl_set_free(dom);
640
0
  return NULL;
641
2
}
642
643
__isl_give isl_map *isl_map_subtract_range(__isl_take isl_map *map,
644
  __isl_take isl_set *dom)
645
2
{
646
2
  return isl_map_align_params_map_map_and(map, dom, &map_subtract_range);
647
2
}
648
649
/* A diff collector that aborts as soon as its add function is called,
650
 * setting empty to 0.
651
 */
652
struct isl_is_empty_diff_collector {
653
  struct isl_diff_collector dc;
654
  isl_bool empty;
655
};
656
657
/* isl_is_empty_diff_collector callback.
658
 */
659
static isl_stat basic_map_is_empty_add(struct isl_diff_collector *dc,
660
          __isl_take isl_basic_map *bmap)
661
8.94k
{
662
8.94k
  struct isl_is_empty_diff_collector *edc;
663
8.94k
  edc = (struct isl_is_empty_diff_collector *)dc;
664
8.94k
665
8.94k
  edc->empty = 0;
666
8.94k
667
8.94k
  isl_basic_map_free(bmap);
668
8.94k
  return isl_stat_error;
669
8.94k
}
670
671
/* Check if bmap \ map is empty by computing this set difference
672
 * and breaking off as soon as the difference is known to be non-empty.
673
 */
674
static isl_bool basic_map_diff_is_empty(__isl_keep isl_basic_map *bmap,
675
  __isl_keep isl_map *map)
676
19.4k
{
677
19.4k
  isl_bool empty;
678
19.4k
  isl_stat r;
679
19.4k
  struct isl_is_empty_diff_collector edc;
680
19.4k
681
19.4k
  empty = isl_basic_map_plain_is_empty(bmap);
682
19.4k
  if (empty)
683
1
    return empty;
684
19.4k
685
19.4k
  edc.dc.add = &basic_map_is_empty_add;
686
19.4k
  edc.empty = isl_bool_true;
687
19.4k
  r = basic_map_collect_diff(isl_basic_map_copy(bmap),
688
19.4k
           isl_map_copy(map), &edc.dc);
689
19.4k
  if (!edc.empty)
690
8.94k
    return isl_bool_false;
691
10.5k
692
10.5k
  return r < 0 ? 
isl_bool_error0
: isl_bool_true;
693
10.5k
}
694
695
/* Check if map1 \ map2 is empty by checking if the set difference is empty
696
 * for each of the basic maps in map1.
697
 */
698
static isl_bool map_diff_is_empty(__isl_keep isl_map *map1,
699
  __isl_keep isl_map *map2)
700
18.3k
{
701
18.3k
  int i;
702
18.3k
  isl_bool is_empty = isl_bool_true;
703
18.3k
704
18.3k
  if (!map1 || !map2)
705
0
    return isl_bool_error;
706
18.3k
  
707
28.8k
  
for (i = 0; 18.3k
i < map1->n;
++i10.5k
) {
708
19.4k
    is_empty = basic_map_diff_is_empty(map1->p[i], map2);
709
19.4k
    if (is_empty < 0 || !is_empty)
710
8.94k
       break;
711
19.4k
  }
712
18.3k
713
18.3k
  return is_empty;
714
18.3k
}
715
716
/* Return true if "bmap" contains a single element.
717
 */
718
isl_bool isl_basic_map_plain_is_singleton(__isl_keep isl_basic_map *bmap)
719
16.7k
{
720
16.7k
  if (!bmap)
721
0
    return isl_bool_error;
722
16.7k
  if (bmap->n_div)
723
1.72k
    return isl_bool_false;
724
15.0k
  if (bmap->n_ineq)
725
12.7k
    return isl_bool_false;
726
2.24k
  return bmap->n_eq == isl_basic_map_total_dim(bmap);
727
2.24k
}
728
729
/* Return true if "map" contains a single element.
730
 */
731
isl_bool isl_map_plain_is_singleton(__isl_keep isl_map *map)
732
18.7k
{
733
18.7k
  if (!map)
734
0
    return isl_bool_error;
735
18.7k
  if (map->n != 1)
736
1.98k
    return isl_bool_false;
737
16.7k
738
16.7k
  return isl_basic_map_plain_is_singleton(map->p[0]);
739
16.7k
}
740
741
/* Given a singleton basic map, extract the single element
742
 * as an isl_point.
743
 */
744
static __isl_give isl_point *singleton_extract_point(
745
  __isl_keep isl_basic_map *bmap)
746
402
{
747
402
  int j;
748
402
  unsigned dim;
749
402
  struct isl_vec *point;
750
402
  isl_int m;
751
402
752
402
  if (!bmap)
753
0
    return NULL;
754
402
755
402
  dim = isl_basic_map_total_dim(bmap);
756
402
  isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
757
402
  point = isl_vec_alloc(bmap->ctx, 1 + dim);
758
402
  if (!point)
759
0
    return NULL;
760
402
761
402
  isl_int_init(m);
762
402
763
402
  isl_int_set_si(point->el[0], 1);
764
886
  for (j = 0; j < bmap->n_eq; 
++j484
) {
765
484
    int i = dim - 1 - j;
766
484
    isl_assert(bmap->ctx,
767
484
        isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
768
484
        goto error);
769
484
    isl_assert(bmap->ctx,
770
484
        isl_int_is_one(bmap->eq[j][1 + i]) ||
771
484
        isl_int_is_negone(bmap->eq[j][1 + i]),
772
484
        goto error);
773
484
    isl_assert(bmap->ctx,
774
484
        isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
775
484
        goto error);
776
484
777
484
    isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
778
484
    isl_int_divexact(m, bmap->eq[j][1 + i], m);
779
484
    isl_int_abs(m, m);
780
484
    isl_seq_scale(point->el, point->el, m, 1 + i);
781
484
    isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
782
484
    isl_int_neg(m, m);
783
484
    isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
784
484
  }
785
402
786
402
  isl_int_clear(m);
787
402
  return isl_point_alloc(isl_basic_map_get_space(bmap), point);
788
0
error:
789
0
  isl_int_clear(m);
790
0
  isl_vec_free(point);
791
0
  return NULL;
792
402
}
793
794
/* Return isl_bool_true if the singleton map "map1" is a subset of "map2",
795
 * i.e., if the single element of "map1" is also an element of "map2".
796
 * Assumes "map2" has known divs.
797
 */
798
static isl_bool map_is_singleton_subset(__isl_keep isl_map *map1,
799
  __isl_keep isl_map *map2)
800
402
{
801
402
  int i;
802
402
  isl_bool is_subset = isl_bool_false;
803
402
  struct isl_point *point;
804
402
805
402
  if (!map1 || !map2)
806
0
    return isl_bool_error;
807
402
  if (map1->n != 1)
808
402
    
isl_die0
(isl_map_get_ctx(map1), isl_error_invalid,
809
402
      "expecting single-disjunct input",
810
402
      return isl_bool_error);
811
402
812
402
  point = singleton_extract_point(map1->p[0]);
813
402
  if (!point)
814
0
    return isl_bool_error;
815
402
816
468
  
for (i = 0; 402
i < map2->n;
++i66
) {
817
424
    is_subset = isl_basic_map_contains_point(map2->p[i], point);
818
424
    if (is_subset)
819
358
      break;
820
424
  }
821
402
822
402
  isl_point_free(point);
823
402
  return is_subset;
824
402
}
825
826
static isl_bool map_is_subset(__isl_keep isl_map *map1,
827
  __isl_keep isl_map *map2)
828
43.1k
{
829
43.1k
  isl_bool is_subset = isl_bool_false;
830
43.1k
  isl_bool empty, single;
831
43.1k
  isl_bool rat1, rat2;
832
43.1k
833
43.1k
  if (!map1 || !map2)
834
0
    return isl_bool_error;
835
43.1k
836
43.1k
  if (!isl_map_has_equal_space(map1, map2))
837
152
    return isl_bool_false;
838
43.0k
839
43.0k
  empty = isl_map_is_empty(map1);
840
43.0k
  if (empty < 0)
841
0
    return isl_bool_error;
842
43.0k
  if (empty)
843
136
    return isl_bool_true;
844
42.8k
845
42.8k
  empty = isl_map_is_empty(map2);
846
42.8k
  if (empty < 0)
847
0
    return isl_bool_error;
848
42.8k
  if (empty)
849
17.3k
    return isl_bool_false;
850
25.5k
851
25.5k
  rat1 = isl_map_has_rational(map1);
852
25.5k
  rat2 = isl_map_has_rational(map2);
853
25.5k
  if (rat1 < 0 || rat2 < 0)
854
0
    return isl_bool_error;
855
25.5k
  if (rat1 && 
!rat214
)
856
2
    return isl_bool_false;
857
25.5k
858
25.5k
  if (isl_map_plain_is_universe(map2))
859
6.82k
    return isl_bool_true;
860
18.7k
861
18.7k
  single = isl_map_plain_is_singleton(map1);
862
18.7k
  if (single < 0)
863
0
    return isl_bool_error;
864
18.7k
  map2 = isl_map_compute_divs(isl_map_copy(map2));
865
18.7k
  if (single) {
866
402
    is_subset = map_is_singleton_subset(map1, map2);
867
402
    isl_map_free(map2);
868
402
    return is_subset;
869
402
  }
870
18.3k
  is_subset = map_diff_is_empty(map1, map2);
871
18.3k
  isl_map_free(map2);
872
18.3k
873
18.3k
  return is_subset;
874
18.3k
}
875
876
isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
877
43.1k
{
878
43.1k
  return isl_map_align_params_map_map_and_test(map1, map2,
879
43.1k
              &map_is_subset);
880
43.1k
}
881
882
isl_bool isl_set_is_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
883
24.4k
{
884
24.4k
  return isl_map_is_subset(set_to_map(set1), set_to_map(set2));
885
24.4k
}
886
887
__isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
888
6.79k
{
889
6.79k
  int i;
890
6.79k
  struct isl_subtract_diff_collector sdc;
891
6.79k
  sdc.dc.add = &basic_map_subtract_add;
892
6.79k
893
6.79k
  if (!map)
894
0
    return NULL;
895
6.79k
  if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
896
6.79k
    
return map3.64k
;
897
3.15k
  if (map->n <= 1)
898
2.79k
    return map;
899
358
900
358
  map = isl_map_compute_divs(map);
901
358
  map = isl_map_remove_empty_parts(map);
902
358
903
358
  if (!map || map->n <= 1)
904
0
    return map;
905
358
906
358
  sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
907
358
908
1.28k
  for (i = 1; i < map->n; 
++i924
) {
909
924
    struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
910
924
    struct isl_map *copy = isl_map_copy(sdc.diff);
911
924
    if (basic_map_collect_diff(bmap, copy, &sdc.dc) < 0) {
912
0
      isl_map_free(sdc.diff);
913
0
      sdc.diff = NULL;
914
0
      break;
915
0
    }
916
924
  }
917
358
918
358
  isl_map_free(map);
919
358
920
358
  return sdc.diff;
921
358
}
922
923
__isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
924
6.78k
{
925
6.78k
  return set_from_map(isl_map_make_disjoint(set_to_map(set)));
926
6.78k
}
927
928
__isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
929
6.36k
{
930
6.36k
  isl_map *universe;
931
6.36k
932
6.36k
  if (!map)
933
0
    return NULL;
934
6.36k
935
6.36k
  universe = isl_map_universe(isl_map_get_space(map));
936
6.36k
937
6.36k
  return isl_map_subtract(universe, map);
938
6.36k
}
939
940
__isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
941
6.36k
{
942
6.36k
  return isl_map_complement(set);
943
6.36k
}