Coverage Report

Created: 2018-06-24 14:39

/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
243k
{
35
243k
  int i;
36
243k
37
243k
  isl_seq_cpy(v->el, c, 1 + dim);
38
243k
  isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim));
39
243k
40
287k
  for (i = 0; i < n_div; 
++i44.1k
) {
41
44.1k
    int pos = 1 + dim + div_map[i];
42
44.1k
    isl_int_add(v->el[pos], v->el[pos], c[1 + dim + i]);
43
44.1k
  }
44
243k
}
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.6k
{
52
68.6k
  int i;
53
68.6k
  unsigned dim;
54
68.6k
  unsigned tab_total;
55
68.6k
  unsigned bmap_total;
56
68.6k
  isl_vec *v;
57
68.6k
58
68.6k
  if (!tab || !bmap)
59
0
    return isl_stat_error;
60
68.6k
61
68.6k
  tab_total = isl_basic_map_total_dim(tab->bmap);
62
68.6k
  bmap_total = isl_basic_map_total_dim(bmap);
63
68.6k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
64
68.6k
65
68.6k
  if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
66
0
    return isl_stat_error;
67
68.6k
68
68.6k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
69
68.6k
  if (!v)
70
0
    return isl_stat_error;
71
68.6k
72
90.9k
  
for (i = 0; 68.6k
i < bmap->n_eq;
++i22.3k
) {
73
26.2k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
74
26.2k
    if (isl_tab_add_ineq(tab, v->el) < 0)
75
0
      goto error;
76
26.2k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
77
26.2k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
78
26.2k
    if (isl_tab_add_ineq(tab, v->el) < 0)
79
0
      goto error;
80
26.2k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
81
26.2k
    if (tab->empty)
82
3.96k
      break;
83
26.2k
  }
84
68.6k
85
195k
  
for (i = 0; 68.6k
i < bmap->n_ineq;
++i127k
) {
86
151k
    expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
87
151k
    if (isl_tab_add_ineq(tab, v->el) < 0)
88
0
      goto error;
89
151k
    if (tab->empty)
90
24.0k
      break;
91
151k
  }
92
68.6k
93
68.6k
  isl_vec_free(v);
94
68.6k
  return isl_stat_ok;
95
0
error:
96
0
  isl_vec_free(v);
97
0
  return isl_stat_error;
98
68.6k
}
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
39.2k
{
112
39.2k
  unsigned dim;
113
39.2k
  unsigned tab_total;
114
39.2k
  unsigned bmap_total;
115
39.2k
  isl_vec *v;
116
39.2k
  isl_stat r;
117
39.2k
118
39.2k
  if (!tab || !bmap)
119
0
    return isl_stat_error;
120
39.2k
121
39.2k
  tab_total = isl_basic_map_total_dim(tab->bmap);
122
39.2k
  bmap_total = isl_basic_map_total_dim(bmap);
123
39.2k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
124
39.2k
125
39.2k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
126
39.2k
  if (!v)
127
0
    return isl_stat_error;
128
39.2k
129
39.2k
  if (c < 2 * bmap->n_eq) {
130
10.5k
    if ((c % 2) != oppose)
131
5.74k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
132
5.74k
          1 + bmap_total);
133
10.5k
    if (oppose)
134
10.5k
      
isl_int_sub_ui7.78k
(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
135
10.5k
    expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
136
10.5k
    r = isl_tab_add_ineq(tab, v->el);
137
10.5k
    if (oppose)
138
10.5k
      
isl_int_add_ui7.78k
(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
139
10.5k
    if ((c % 2) != oppose)
140
5.74k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
141
5.74k
          1 + bmap_total);
142
28.7k
  } else {
143
28.7k
    c -= 2 * bmap->n_eq;
144
28.7k
    if (oppose) {
145
25.6k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
146
25.6k
          1 + bmap_total);
147
25.6k
      isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
148
25.6k
    }
149
28.7k
    expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
150
28.7k
    r = isl_tab_add_ineq(tab, v->el);
151
28.7k
    if (oppose) {
152
25.6k
      isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
153
25.6k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
154
25.6k
          1 + bmap_total);
155
25.6k
    }
156
28.7k
  }
157
39.2k
158
39.2k
  isl_vec_free(v);
159
39.2k
  return r;
160
39.2k
}
161
162
static isl_stat tab_add_divs(struct isl_tab *tab,
163
  __isl_keep isl_basic_map *bmap, int **div_map)
164
68.6k
{
165
68.6k
  int i, j;
166
68.6k
  struct isl_vec *vec;
167
68.6k
  unsigned total;
168
68.6k
  unsigned dim;
169
68.6k
170
68.6k
  if (!bmap)
171
0
    return isl_stat_error;
172
68.6k
  if (!bmap->n_div)
173
63.7k
    return isl_stat_ok;
174
4.92k
175
4.92k
  if (!*div_map)
176
4.10k
    *div_map = isl_alloc_array(bmap->ctx, int, bmap->n_div);
177
4.92k
  if (!*div_map)
178
0
    return isl_stat_error;
179
4.92k
180
4.92k
  total = isl_basic_map_total_dim(tab->bmap);
181
4.92k
  dim = total - tab->bmap->n_div;
182
4.92k
  vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
183
4.92k
  if (!vec)
184
0
    return isl_stat_error;
185
4.92k
186
12.6k
  
for (i = 0; 4.92k
i < bmap->n_div;
++i7.68k
) {
187
7.68k
    isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
188
7.68k
    isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
189
11.0k
    for (j = 0; j < i; 
++j3.35k
)
190
7.68k
      
isl_int_set3.35k
(vec->el[2 + dim + (*div_map)[j]],
191
7.68k
          bmap->div[i][2 + dim + j]);
192
14.5k
    for (j = 0; j < tab->bmap->n_div; 
++j6.90k
)
193
11.5k
      if (isl_seq_eq(tab->bmap->div[j],
194
11.5k
          vec->el, 2 + dim + tab->bmap->n_div))
195
4.67k
        break;
196
7.68k
    (*div_map)[i] = j;
197
7.68k
    if (j == tab->bmap->n_div) {
198
3.00k
      vec->size = 2 + dim + tab->bmap->n_div;
199
3.00k
      if (isl_tab_add_div(tab, vec) < 0)
200
0
        goto error;
201
3.00k
    }
202
7.68k
  }
203
4.92k
204
4.92k
  isl_vec_free(vec);
205
4.92k
206
4.92k
  return isl_stat_ok;
207
0
error:
208
0
  isl_vec_free(vec);
209
0
210
0
  return isl_stat_error;
211
4.92k
}
212
213
/* Freeze all constraints of tableau tab.
214
 */
215
static int tab_freeze_constraints(struct isl_tab *tab)
216
68.6k
{
217
68.6k
  int i;
218
68.6k
219
413k
  for (i = 0; i < tab->n_con; 
++i344k
)
220
344k
    if (isl_tab_freeze_constraint(tab, i) < 0)
221
0
      return -1;
222
68.6k
223
68.6k
  return 0;
224
68.6k
}
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
43.9k
{
233
43.9k
  int i, n;
234
43.9k
  int n_test = tab->n_con - offset;
235
43.9k
236
43.9k
  if (isl_tab_detect_redundant(tab) < 0)
237
0
    return -1;
238
43.9k
239
43.9k
  if (n_test == 0)
240
2.66k
    return 0;
241
41.2k
  if (!*index)
242
40.7k
    *index = isl_alloc_array(ctx, int, n_test);
243
41.2k
  if (!*index)
244
0
    return -1;
245
41.2k
246
185k
  
for (n = 0, i = 0; 41.2k
i < n_test;
++i144k
) {
247
144k
    int r;
248
144k
    r = isl_tab_is_redundant(tab, offset + i);
249
144k
    if (r < 0)
250
0
      return -1;
251
144k
    if (r)
252
102k
      continue;
253
41.4k
    (*index)[n++] = i;
254
41.4k
  }
255
41.2k
256
41.2k
  return n;
257
41.2k
}
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
41.8k
{
300
41.8k
  int i;
301
41.8k
  int modified;
302
41.8k
  int level;
303
41.8k
  int init;
304
41.8k
  isl_bool empty;
305
41.8k
  isl_ctx *ctx;
306
41.8k
  struct isl_tab *tab = NULL;
307
41.8k
  struct isl_tab_undo **snap = NULL;
308
41.8k
  int *k = NULL;
309
41.8k
  int *n = NULL;
310
41.8k
  int **index = NULL;
311
41.8k
  int **div_map = NULL;
312
41.8k
313
41.8k
  empty = isl_basic_map_is_empty(bmap);
314
41.8k
  if (empty) {
315
6
    isl_basic_map_free(bmap);
316
6
    isl_map_free(map);
317
6
    return empty < 0 ? 
isl_stat_error0
: isl_stat_ok;
318
6
  }
319
41.8k
320
41.8k
  bmap = isl_basic_map_cow(bmap);
321
41.8k
  map = isl_map_cow(map);
322
41.8k
323
41.8k
  if (!bmap || !map)
324
0
    goto error;
325
41.8k
326
41.8k
  ctx = map->ctx;
327
41.8k
  snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
328
41.8k
  k = isl_alloc_array(map->ctx, int, map->n);
329
41.8k
  n = isl_alloc_array(map->ctx, int, map->n);
330
41.8k
  index = isl_calloc_array(map->ctx, int *, map->n);
331
41.8k
  div_map = isl_calloc_array(map->ctx, int *, map->n);
332
41.8k
  if (!snap || !k || !n || !index || !div_map)
333
0
    goto error;
334
41.8k
335
41.8k
  bmap = isl_basic_map_order_divs(bmap);
336
41.8k
  map = isl_map_order_divs(map);
337
41.8k
338
41.8k
  tab = isl_tab_from_basic_map(bmap, 1);
339
41.8k
  if (!tab)
340
0
    goto error;
341
41.8k
342
41.8k
  modified = 0;
343
41.8k
  level = 0;
344
41.8k
  init = 1;
345
41.8k
346
165k
  while (level >= 0) {
347
137k
    if (level >= map->n) {
348
31.3k
      int empty;
349
31.3k
      struct isl_basic_map *bm;
350
31.3k
      if (!modified) {
351
6.03k
        if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
352
1.34k
          goto error;
353
4.69k
        break;
354
4.69k
      }
355
25.2k
      bm = isl_basic_map_copy(tab->bmap);
356
25.2k
      bm = isl_basic_map_cow(bm);
357
25.2k
      bm = isl_basic_map_update_from_tab(bm, tab);
358
25.2k
      bm = isl_basic_map_simplify(bm);
359
25.2k
      bm = isl_basic_map_finalize(bm);
360
25.2k
      empty = isl_basic_map_is_empty(bm);
361
25.2k
      if (empty)
362
977
        isl_basic_map_free(bm);
363
24.2k
      else if (dc->add(dc, bm) < 0)
364
7.76k
        goto error;
365
17.5k
      if (empty < 0)
366
0
        goto error;
367
17.5k
      level--;
368
17.5k
      init = 0;
369
17.5k
      continue;
370
17.5k
    }
371
105k
    if (init) {
372
68.6k
      int offset;
373
68.6k
      struct isl_tab_undo *snap2;
374
68.6k
      snap2 = isl_tab_snap(tab);
375
68.6k
      if (tab_add_divs(tab, map->p[level],
376
68.6k
           &div_map[level]) < 0)
377
0
        goto error;
378
68.6k
      offset = tab->n_con;
379
68.6k
      snap[level] = isl_tab_snap(tab);
380
68.6k
      if (tab_freeze_constraints(tab) < 0)
381
0
        goto error;
382
68.6k
      if (tab_add_constraints(tab, map->p[level],
383
68.6k
            div_map[level]) < 0)
384
0
        goto error;
385
68.6k
      k[level] = 0;
386
68.6k
      n[level] = 0;
387
68.6k
      if (tab->empty) {
388
24.7k
        if (isl_tab_rollback(tab, snap2) < 0)
389
0
          goto error;
390
24.7k
        level++;
391
24.7k
        continue;
392
24.7k
      }
393
43.9k
      modified = 1;
394
43.9k
      n[level] = n_non_redundant(ctx, tab, offset,
395
43.9k
                &index[level]);
396
43.9k
      if (n[level] < 0)
397
0
        goto error;
398
43.9k
      if (n[level] == 0) {
399
16.3k
        level--;
400
16.3k
        init = 0;
401
16.3k
        continue;
402
16.3k
      }
403
27.5k
      if (isl_tab_rollback(tab, snap[level]) < 0)
404
0
        goto error;
405
27.5k
      if (tab_add_constraint(tab, map->p[level],
406
27.5k
          div_map[level], index[level][0], 1) < 0)
407
0
        goto error;
408
27.5k
      level++;
409
27.5k
      continue;
410
37.0k
    } else {
411
37.0k
      if (k[level] + 1 >= n[level]) {
412
31.1k
        level--;
413
31.1k
        continue;
414
31.1k
      }
415
5.85k
      if (isl_tab_rollback(tab, snap[level]) < 0)
416
0
        goto error;
417
5.85k
      if (tab_add_constraint(tab, map->p[level],
418
5.85k
            div_map[level],
419
5.85k
            index[level][k[level]], 0) < 0)
420
0
        goto error;
421
5.85k
      snap[level] = isl_tab_snap(tab);
422
5.85k
      k[level]++;
423
5.85k
      if (tab_add_constraint(tab, map->p[level],
424
5.85k
            div_map[level],
425
5.85k
            index[level][k[level]], 1) < 0)
426
0
        goto error;
427
5.85k
      level++;
428
5.85k
      init = 1;
429
5.85k
      continue;
430
5.85k
    }
431
105k
  }
432
41.8k
433
41.8k
  isl_tab_free(tab);
434
32.7k
  free(snap);
435
32.7k
  free(n);
436
32.7k
  free(k);
437
83.0k
  for (i = 0; index && i < map->n; 
++i50.3k
)
438
50.3k
    free(index[i]);
439
32.7k
  free(index);
440
83.0k
  for (i = 0; div_map && i < map->n; 
++i50.3k
)
441
50.3k
    free(div_map[i]);
442
32.7k
  free(div_map);
443
32.7k
444
32.7k
  isl_basic_map_free(bmap);
445
32.7k
  isl_map_free(map);
446
32.7k
447
32.7k
  return isl_stat_ok;
448
9.10k
error:
449
9.10k
  isl_tab_free(tab);
450
9.10k
  free(snap);
451
9.10k
  free(n);
452
9.10k
  free(k);
453
25.1k
  for (i = 0; index && i < map->n; 
++i16.0k
)
454
16.0k
    free(index[i]);
455
9.10k
  free(index);
456
25.1k
  for (i = 0; div_map && i < map->n; 
++i16.0k
)
457
16.0k
    free(div_map[i]);
458
9.10k
  free(div_map);
459
9.10k
  isl_basic_map_free(bmap);
460
9.10k
  isl_map_free(map);
461
9.10k
  return isl_stat_error;
462
41.8k
}
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
21.2k
{
477
21.2k
  struct isl_subtract_diff_collector *sdc;
478
21.2k
  sdc = (struct isl_subtract_diff_collector *)dc;
479
21.2k
480
21.2k
  sdc->diff = isl_map_union_disjoint(sdc->diff,
481
21.2k
      isl_map_from_basic_map(bmap));
482
21.2k
483
21.2k
  return sdc->diff ? isl_stat_ok : 
isl_stat_error0
;
484
21.2k
}
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
21.3k
{
491
21.3k
  struct isl_subtract_diff_collector sdc;
492
21.3k
  sdc.dc.add = &basic_map_subtract_add;
493
21.3k
  sdc.diff = isl_map_empty(isl_basic_map_get_space(bmap));
494
21.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
21.3k
  return sdc.diff;
499
21.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.80k
{
506
4.80k
  isl_space *space;
507
4.80k
508
4.80k
  space = isl_map_get_space(map1);
509
4.80k
  isl_map_free(map1);
510
4.80k
  isl_map_free(map2);
511
4.80k
  return isl_map_empty(space);
512
4.80k
}
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
32.7k
{
525
32.7k
  int i;
526
32.7k
  int equal, disjoint;
527
32.7k
  struct isl_map *diff;
528
32.7k
529
32.7k
  if (!map1 || !map2)
530
0
    goto error;
531
32.7k
532
32.7k
  isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
533
32.7k
534
32.7k
  equal = isl_map_plain_is_equal(map1, map2);
535
32.7k
  if (equal < 0)
536
0
    goto error;
537
32.7k
  if (equal)
538
4.80k
    return replace_pair_by_empty(map1, map2);
539
27.9k
540
27.9k
  disjoint = isl_map_is_disjoint(map1, map2);
541
27.9k
  if (disjoint < 0)
542
0
    goto error;
543
27.9k
  if (disjoint) {
544
15.9k
    isl_map_free(map2);
545
15.9k
    return map1;
546
15.9k
  }
547
11.9k
548
11.9k
  map1 = isl_map_compute_divs(map1);
549
11.9k
  map2 = isl_map_compute_divs(map2);
550
11.9k
  if (!map1 || !map2)
551
0
    goto error;
552
11.9k
553
11.9k
  map1 = isl_map_remove_empty_parts(map1);
554
11.9k
  map2 = isl_map_remove_empty_parts(map2);
555
11.9k
556
11.9k
  diff = isl_map_empty(isl_map_get_space(map1));
557
33.3k
  for (i = 0; i < map1->n; 
++i21.3k
) {
558
21.3k
    struct isl_map *d;
559
21.3k
    d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
560
21.3k
               isl_map_copy(map2));
561
21.3k
    if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
562
21.3k
      
diff = isl_map_union_disjoint(diff, d)9.74k
;
563
11.5k
    else
564
11.5k
      diff = isl_map_union(diff, d);
565
21.3k
  }
566
11.9k
567
11.9k
  isl_map_free(map1);
568
11.9k
  isl_map_free(map2);
569
11.9k
570
11.9k
  return diff;
571
0
error:
572
0
  isl_map_free(map1);
573
0
  isl_map_free(map2);
574
0
  return NULL;
575
11.9k
}
576
577
__isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
578
  __isl_take isl_map *map2)
579
32.7k
{
580
32.7k
  return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
581
32.7k
}
582
583
struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
584
21.9k
{
585
21.9k
  return set_from_map(isl_map_subtract(set_to_map(set1),
586
21.9k
              set_to_map(set2)));
587
21.9k
}
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
9.10k
{
662
9.10k
  struct isl_is_empty_diff_collector *edc;
663
9.10k
  edc = (struct isl_is_empty_diff_collector *)dc;
664
9.10k
665
9.10k
  edc->empty = 0;
666
9.10k
667
9.10k
  isl_basic_map_free(bmap);
668
9.10k
  return isl_stat_error;
669
9.10k
}
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.7k
{
677
19.7k
  isl_bool empty;
678
19.7k
  isl_stat r;
679
19.7k
  struct isl_is_empty_diff_collector edc;
680
19.7k
681
19.7k
  empty = isl_basic_map_plain_is_empty(bmap);
682
19.7k
  if (empty)
683
1
    return empty;
684
19.7k
685
19.7k
  edc.dc.add = &basic_map_is_empty_add;
686
19.7k
  edc.empty = isl_bool_true;
687
19.7k
  r = basic_map_collect_diff(isl_basic_map_copy(bmap),
688
19.7k
           isl_map_copy(map), &edc.dc);
689
19.7k
  if (!edc.empty)
690
9.10k
    return isl_bool_false;
691
10.6k
692
10.6k
  return r < 0 ? 
isl_bool_error0
: isl_bool_true;
693
10.6k
}
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.6k
{
701
18.6k
  int i;
702
18.6k
  isl_bool is_empty = isl_bool_true;
703
18.6k
704
18.6k
  if (!map1 || !map2)
705
0
    return isl_bool_error;
706
18.6k
  
707
29.2k
  
for (i = 0; 18.6k
i < map1->n;
++i10.6k
) {
708
19.7k
    is_empty = basic_map_diff_is_empty(map1->p[i], map2);
709
19.7k
    if (is_empty < 0 || !is_empty)
710
9.10k
       break;
711
19.7k
  }
712
18.6k
713
18.6k
  return is_empty;
714
18.6k
}
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
17.0k
{
720
17.0k
  if (!bmap)
721
0
    return isl_bool_error;
722
17.0k
  if (bmap->n_div)
723
1.74k
    return isl_bool_false;
724
15.2k
  if (bmap->n_ineq)
725
12.9k
    return isl_bool_false;
726
2.33k
  return bmap->n_eq == isl_basic_map_total_dim(bmap);
727
2.33k
}
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
19.0k
{
733
19.0k
  if (!map)
734
0
    return isl_bool_error;
735
19.0k
  if (map->n != 1)
736
1.99k
    return isl_bool_false;
737
17.0k
738
17.0k
  return isl_basic_map_plain_is_singleton(map->p[0]);
739
17.0k
}
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
408
{
747
408
  int j;
748
408
  unsigned dim;
749
408
  struct isl_vec *point;
750
408
  isl_int m;
751
408
752
408
  if (!bmap)
753
0
    return NULL;
754
408
755
408
  dim = isl_basic_map_total_dim(bmap);
756
408
  isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
757
408
  point = isl_vec_alloc(bmap->ctx, 1 + dim);
758
408
  if (!point)
759
0
    return NULL;
760
408
761
408
  isl_int_init(m);
762
408
763
408
  isl_int_set_si(point->el[0], 1);
764
900
  for (j = 0; j < bmap->n_eq; 
++j492
) {
765
492
    int i = dim - 1 - j;
766
492
    isl_assert(bmap->ctx,
767
492
        isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
768
492
        goto error);
769
492
    isl_assert(bmap->ctx,
770
492
        isl_int_is_one(bmap->eq[j][1 + i]) ||
771
492
        isl_int_is_negone(bmap->eq[j][1 + i]),
772
492
        goto error);
773
492
    isl_assert(bmap->ctx,
774
492
        isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
775
492
        goto error);
776
492
777
492
    isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
778
492
    isl_int_divexact(m, bmap->eq[j][1 + i], m);
779
492
    isl_int_abs(m, m);
780
492
    isl_seq_scale(point->el, point->el, m, 1 + i);
781
492
    isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
782
492
    isl_int_neg(m, m);
783
492
    isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
784
492
  }
785
408
786
408
  isl_int_clear(m);
787
408
  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
408
}
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
408
{
801
408
  int i;
802
408
  isl_bool is_subset = isl_bool_false;
803
408
  struct isl_point *point;
804
408
805
408
  if (!map1 || !map2)
806
0
    return isl_bool_error;
807
408
  if (map1->n != 1)
808
408
    
isl_die0
(isl_map_get_ctx(map1), isl_error_invalid,
809
408
      "expecting single-disjunct input",
810
408
      return isl_bool_error);
811
408
812
408
  point = singleton_extract_point(map1->p[0]);
813
408
  if (!point)
814
0
    return isl_bool_error;
815
408
816
474
  
for (i = 0; 408
i < map2->n;
++i66
) {
817
430
    is_subset = isl_basic_map_contains_point(map2->p[i], point);
818
430
    if (is_subset)
819
364
      break;
820
430
  }
821
408
822
408
  isl_point_free(point);
823
408
  return is_subset;
824
408
}
825
826
static isl_bool map_is_subset(__isl_keep isl_map *map1,
827
  __isl_keep isl_map *map2)
828
43.5k
{
829
43.5k
  isl_bool is_subset = isl_bool_false;
830
43.5k
  isl_bool empty, single;
831
43.5k
  isl_bool rat1, rat2;
832
43.5k
833
43.5k
  if (!map1 || !map2)
834
0
    return isl_bool_error;
835
43.5k
836
43.5k
  if (!isl_map_has_equal_space(map1, map2))
837
157
    return isl_bool_false;
838
43.3k
839
43.3k
  empty = isl_map_is_empty(map1);
840
43.3k
  if (empty < 0)
841
0
    return isl_bool_error;
842
43.3k
  if (empty)
843
127
    return isl_bool_true;
844
43.2k
845
43.2k
  empty = isl_map_is_empty(map2);
846
43.2k
  if (empty < 0)
847
0
    return isl_bool_error;
848
43.2k
  if (empty)
849
17.3k
    return isl_bool_false;
850
25.9k
851
25.9k
  rat1 = isl_map_has_rational(map1);
852
25.9k
  rat2 = isl_map_has_rational(map2);
853
25.9k
  if (rat1 < 0 || rat2 < 0)
854
0
    return isl_bool_error;
855
25.9k
  if (rat1 && 
!rat214
)
856
2
    return isl_bool_false;
857
25.9k
858
25.9k
  if (isl_map_plain_is_universe(map2))
859
6.90k
    return isl_bool_true;
860
19.0k
861
19.0k
  single = isl_map_plain_is_singleton(map1);
862
19.0k
  if (single < 0)
863
0
    return isl_bool_error;
864
19.0k
  map2 = isl_map_compute_divs(isl_map_copy(map2));
865
19.0k
  if (single) {
866
408
    is_subset = map_is_singleton_subset(map1, map2);
867
408
    isl_map_free(map2);
868
408
    return is_subset;
869
408
  }
870
18.6k
  is_subset = map_diff_is_empty(map1, map2);
871
18.6k
  isl_map_free(map2);
872
18.6k
873
18.6k
  return is_subset;
874
18.6k
}
875
876
isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
877
43.5k
{
878
43.5k
  return isl_map_align_params_map_map_and_test(map1, map2,
879
43.5k
              &map_is_subset);
880
43.5k
}
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.76k
{
889
6.76k
  int i;
890
6.76k
  struct isl_subtract_diff_collector sdc;
891
6.76k
  sdc.dc.add = &basic_map_subtract_add;
892
6.76k
893
6.76k
  if (!map)
894
0
    return NULL;
895
6.76k
  if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
896
6.76k
    
return map3.62k
;
897
3.13k
  if (map->n <= 1)
898
2.82k
    return map;
899
315
900
315
  map = isl_map_compute_divs(map);
901
315
  map = isl_map_remove_empty_parts(map);
902
315
903
315
  if (!map || map->n <= 1)
904
0
    return map;
905
315
906
315
  sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
907
315
908
1.04k
  for (i = 1; i < map->n; 
++i734
) {
909
734
    struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
910
734
    struct isl_map *copy = isl_map_copy(sdc.diff);
911
734
    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
734
  }
917
315
918
315
  isl_map_free(map);
919
315
920
315
  return sdc.diff;
921
315
}
922
923
__isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
924
6.74k
{
925
6.74k
  return set_from_map(isl_map_make_disjoint(set_to_map(set)));
926
6.74k
}
927
928
__isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
929
6.44k
{
930
6.44k
  isl_map *universe;
931
6.44k
932
6.44k
  if (!map)
933
0
    return NULL;
934
6.44k
935
6.44k
  universe = isl_map_universe(isl_map_get_space(map));
936
6.44k
937
6.44k
  return isl_map_subtract(universe, map);
938
6.44k
}
939
940
__isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
941
6.44k
{
942
6.44k
  return isl_map_complement(set);
943
6.44k
}