Coverage Report

Created: 2017-11-21 16:49

/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
98.9k
{
35
98.9k
  int i;
36
98.9k
37
98.9k
  isl_seq_cpy(v->el, c, 1 + dim);
38
98.9k
  isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim));
39
98.9k
40
113k
  for (i = 0; i < n_div; 
++i14.2k
) {
41
14.2k
    int pos = 1 + dim + div_map[i];
42
14.2k
    isl_int_add(v->el[pos], v->el[pos], c[1 + dim + i]);
43
14.2k
  }
44
98.9k
}
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
25.7k
{
52
25.7k
  int i;
53
25.7k
  unsigned dim;
54
25.7k
  unsigned tab_total;
55
25.7k
  unsigned bmap_total;
56
25.7k
  isl_vec *v;
57
25.7k
58
25.7k
  if (!tab || !bmap)
59
0
    return isl_stat_error;
60
25.7k
61
25.7k
  tab_total = isl_basic_map_total_dim(tab->bmap);
62
25.7k
  bmap_total = isl_basic_map_total_dim(bmap);
63
25.7k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
64
25.7k
65
25.7k
  if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
66
0
    return isl_stat_error;
67
25.7k
68
25.7k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
69
25.7k
  if (!v)
70
0
    return isl_stat_error;
71
25.7k
72
34.2k
  
for (i = 0; 25.7k
i < bmap->n_eq;
++i8.53k
) {
73
9.96k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
74
9.96k
    if (isl_tab_add_ineq(tab, v->el) < 0)
75
0
      goto error;
76
9.96k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
77
9.96k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
78
9.96k
    if (isl_tab_add_ineq(tab, v->el) < 0)
79
0
      goto error;
80
9.96k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
81
9.96k
    if (tab->empty)
82
1.42k
      break;
83
9.96k
  }
84
25.7k
85
79.9k
  
for (i = 0; 25.7k
i < bmap->n_ineq;
++i54.2k
) {
86
62.8k
    expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
87
62.8k
    if (isl_tab_add_ineq(tab, v->el) < 0)
88
0
      goto error;
89
62.8k
    if (tab->empty)
90
8.59k
      break;
91
62.8k
  }
92
25.7k
93
25.7k
  isl_vec_free(v);
94
25.7k
  return isl_stat_ok;
95
0
error:
96
0
  isl_vec_free(v);
97
0
  return isl_stat_error;
98
25.7k
}
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
16.1k
{
112
16.1k
  unsigned dim;
113
16.1k
  unsigned tab_total;
114
16.1k
  unsigned bmap_total;
115
16.1k
  isl_vec *v;
116
16.1k
  isl_stat r;
117
16.1k
118
16.1k
  if (!tab || !bmap)
119
0
    return isl_stat_error;
120
16.1k
121
16.1k
  tab_total = isl_basic_map_total_dim(tab->bmap);
122
16.1k
  bmap_total = isl_basic_map_total_dim(bmap);
123
16.1k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
124
16.1k
125
16.1k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
126
16.1k
  if (!v)
127
0
    return isl_stat_error;
128
16.1k
129
16.1k
  if (c < 2 * bmap->n_eq) {
130
3.42k
    if ((c % 2) != oppose)
131
1.84k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
132
1.84k
          1 + bmap_total);
133
3.42k
    if (oppose)
134
3.42k
      
isl_int_sub_ui2.57k
(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
135
3.42k
    expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
136
3.42k
    r = isl_tab_add_ineq(tab, v->el);
137
3.42k
    if (oppose)
138
3.42k
      
isl_int_add_ui2.57k
(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
139
3.42k
    if ((c % 2) != oppose)
140
1.84k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
141
1.84k
          1 + bmap_total);
142
12.7k
  } else {
143
12.7k
    c -= 2 * bmap->n_eq;
144
12.7k
    if (oppose) {
145
10.7k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
146
10.7k
          1 + bmap_total);
147
10.7k
      isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
148
10.7k
    }
149
12.7k
    expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
150
12.7k
    r = isl_tab_add_ineq(tab, v->el);
151
12.7k
    if (oppose) {
152
10.7k
      isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
153
10.7k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
154
10.7k
          1 + bmap_total);
155
10.7k
    }
156
12.7k
  }
157
16.1k
158
16.1k
  isl_vec_free(v);
159
16.1k
  return r;
160
16.1k
}
161
162
static isl_stat tab_add_divs(struct isl_tab *tab,
163
  __isl_keep isl_basic_map *bmap, int **div_map)
164
25.7k
{
165
25.7k
  int i, j;
166
25.7k
  struct isl_vec *vec;
167
25.7k
  unsigned total;
168
25.7k
  unsigned dim;
169
25.7k
170
25.7k
  if (!bmap)
171
0
    return isl_stat_error;
172
25.7k
  if (!bmap->n_div)
173
24.2k
    return isl_stat_ok;
174
1.47k
175
1.47k
  if (!*div_map)
176
1.31k
    *div_map = isl_alloc_array(bmap->ctx, int, bmap->n_div);
177
1.47k
  if (!*div_map)
178
0
    return isl_stat_error;
179
1.47k
180
1.47k
  total = isl_basic_map_total_dim(tab->bmap);
181
1.47k
  dim = total - tab->bmap->n_div;
182
1.47k
  vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
183
1.47k
  if (!vec)
184
0
    return isl_stat_error;
185
1.47k
186
3.42k
  
for (i = 0; 1.47k
i < bmap->n_div;
++i1.95k
) {
187
1.95k
    isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
188
1.95k
    isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
189
2.56k
    for (j = 0; j < i; 
++j612
)
190
1.95k
      
isl_int_set612
(vec->el[2 + dim + (*div_map)[j]],
191
1.95k
          bmap->div[i][2 + dim + j]);
192
3.05k
    for (j = 0; j < tab->bmap->n_div; 
++j1.10k
)
193
2.45k
      if (isl_seq_eq(tab->bmap->div[j],
194
2.45k
          vec->el, 2 + dim + tab->bmap->n_div))
195
1.35k
        break;
196
1.95k
    (*div_map)[i] = j;
197
1.95k
    if (j == tab->bmap->n_div) {
198
597
      vec->size = 2 + dim + tab->bmap->n_div;
199
597
      if (isl_tab_add_div(tab, vec) < 0)
200
0
        goto error;
201
597
    }
202
1.95k
  }
203
1.47k
204
1.47k
  isl_vec_free(vec);
205
1.47k
206
1.47k
  return isl_stat_ok;
207
0
error:
208
0
  isl_vec_free(vec);
209
0
210
0
  return isl_stat_error;
211
25.7k
}
212
213
/* Freeze all constraints of tableau tab.
214
 */
215
static int tab_freeze_constraints(struct isl_tab *tab)
216
25.7k
{
217
25.7k
  int i;
218
25.7k
219
167k
  for (i = 0; i < tab->n_con; 
++i141k
)
220
141k
    if (isl_tab_freeze_constraint(tab, i) < 0)
221
0
      return -1;
222
25.7k
223
25.7k
  return 0;
224
25.7k
}
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
16.8k
{
233
16.8k
  int i, n;
234
16.8k
  int n_test = tab->n_con - offset;
235
16.8k
236
16.8k
  if (isl_tab_detect_redundant(tab) < 0)
237
0
    return -1;
238
16.8k
239
16.8k
  if (n_test == 0)
240
1.10k
    return 0;
241
15.7k
  if (!*index)
242
15.7k
    *index = isl_alloc_array(ctx, int, n_test);
243
15.7k
  if (!*index)
244
0
    return -1;
245
15.7k
246
77.3k
  
for (n = 0, i = 0; 15.7k
i < n_test;
++i61.5k
) {
247
61.5k
    int r;
248
61.5k
    r = isl_tab_is_redundant(tab, offset + i);
249
61.5k
    if (r < 0)
250
0
      return -1;
251
61.5k
    if (r)
252
45.0k
      continue;
253
16.5k
    (*index)[n++] = i;
254
16.5k
  }
255
15.7k
256
15.7k
  return n;
257
16.8k
}
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
18.3k
{
300
18.3k
  int i;
301
18.3k
  int modified;
302
18.3k
  int level;
303
18.3k
  int init;
304
18.3k
  isl_bool empty;
305
18.3k
  isl_ctx *ctx;
306
18.3k
  struct isl_tab *tab = NULL;
307
18.3k
  struct isl_tab_undo **snap = NULL;
308
18.3k
  int *k = NULL;
309
18.3k
  int *n = NULL;
310
18.3k
  int **index = NULL;
311
18.3k
  int **div_map = NULL;
312
18.3k
313
18.3k
  empty = isl_basic_map_is_empty(bmap);
314
18.3k
  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
18.3k
320
18.3k
  bmap = isl_basic_map_cow(bmap);
321
18.3k
  map = isl_map_cow(map);
322
18.3k
323
18.3k
  if (!bmap || !map)
324
0
    goto error;
325
18.3k
326
18.3k
  ctx = map->ctx;
327
18.3k
  snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
328
18.3k
  k = isl_alloc_array(map->ctx, int, map->n);
329
18.3k
  n = isl_alloc_array(map->ctx, int, map->n);
330
18.3k
  index = isl_calloc_array(map->ctx, int *, map->n);
331
18.3k
  div_map = isl_calloc_array(map->ctx, int *, map->n);
332
18.3k
  if (!snap || !k || !n || !index || !div_map)
333
0
    goto error;
334
18.3k
335
18.3k
  bmap = isl_basic_map_order_divs(bmap);
336
18.3k
  map = isl_map_order_divs(map);
337
18.3k
338
18.3k
  tab = isl_tab_from_basic_map(bmap, 1);
339
18.3k
  if (!tab)
340
0
    goto error;
341
18.3k
342
18.3k
  modified = 0;
343
18.3k
  level = 0;
344
18.3k
  init = 1;
345
18.3k
346
65.1k
  while (level >= 0) {
347
53.5k
    if (level >= map->n) {
348
14.7k
      int empty;
349
14.7k
      struct isl_basic_map *bm;
350
14.7k
      if (!modified) {
351
4.19k
        if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
352
394
          goto error;
353
3.80k
        break;
354
3.80k
      }
355
10.5k
      bm = isl_basic_map_copy(tab->bmap);
356
10.5k
      bm = isl_basic_map_cow(bm);
357
10.5k
      bm = isl_basic_map_update_from_tab(bm, tab);
358
10.5k
      bm = isl_basic_map_simplify(bm);
359
10.5k
      bm = isl_basic_map_finalize(bm);
360
10.5k
      empty = isl_basic_map_is_empty(bm);
361
10.5k
      if (empty)
362
283
        isl_basic_map_free(bm);
363
10.2k
      else if (dc->add(dc, bm) < 0)
364
2.48k
        goto error;
365
8.07k
      if (empty < 0)
366
0
        goto error;
367
8.07k
      level--;
368
8.07k
      init = 0;
369
8.07k
      continue;
370
8.07k
    }
371
38.7k
    if (init) {
372
25.7k
      int offset;
373
25.7k
      struct isl_tab_undo *snap2;
374
25.7k
      snap2 = isl_tab_snap(tab);
375
25.7k
      if (tab_add_divs(tab, map->p[level],
376
25.7k
           &div_map[level]) < 0)
377
0
        goto error;
378
25.7k
      offset = tab->n_con;
379
25.7k
      snap[level] = isl_tab_snap(tab);
380
25.7k
      if (tab_freeze_constraints(tab) < 0)
381
0
        goto error;
382
25.7k
      if (tab_add_constraints(tab, map->p[level],
383
25.7k
            div_map[level]) < 0)
384
0
        goto error;
385
25.7k
      k[level] = 0;
386
25.7k
      n[level] = 0;
387
25.7k
      if (tab->empty) {
388
8.86k
        if (isl_tab_rollback(tab, snap2) < 0)
389
0
          goto error;
390
8.86k
        level++;
391
8.86k
        continue;
392
8.86k
      }
393
16.8k
      modified = 1;
394
16.8k
      n[level] = n_non_redundant(ctx, tab, offset,
395
16.8k
                &index[level]);
396
16.8k
      if (n[level] < 0)
397
0
        goto error;
398
16.8k
      if (n[level] == 0) {
399
6.40k
        level--;
400
6.40k
        init = 0;
401
6.40k
        continue;
402
6.40k
      }
403
10.4k
      if (isl_tab_rollback(tab, snap[level]) < 0)
404
0
        goto error;
405
10.4k
      if (tab_add_constraint(tab, map->p[level],
406
10.4k
          div_map[level], index[level][0], 1) < 0)
407
0
        goto error;
408
10.4k
      level++;
409
10.4k
      continue;
410
13.0k
    } else {
411
13.0k
      if (k[level] + 1 >= n[level]) {
412
10.1k
        level--;
413
10.1k
        continue;
414
10.1k
      }
415
2.85k
      if (isl_tab_rollback(tab, snap[level]) < 0)
416
0
        goto error;
417
2.85k
      if (tab_add_constraint(tab, map->p[level],
418
2.85k
            div_map[level],
419
2.85k
            index[level][k[level]], 0) < 0)
420
0
        goto error;
421
2.85k
      snap[level] = isl_tab_snap(tab);
422
2.85k
      k[level]++;
423
2.85k
      if (tab_add_constraint(tab, map->p[level],
424
2.85k
            div_map[level],
425
2.85k
            index[level][k[level]], 1) < 0)
426
0
        goto error;
427
2.85k
      level++;
428
2.85k
      init = 1;
429
2.85k
      continue;
430
2.85k
    }
431
53.5k
  }
432
18.3k
433
18.3k
  isl_tab_free(tab);
434
15.4k
  free(snap);
435
15.4k
  free(n);
436
15.4k
  free(k);
437
35.7k
  for (i = 0; index && i < map->n; 
++i20.3k
)
438
20.3k
    free(index[i]);
439
15.4k
  free(index);
440
35.7k
  for (i = 0; div_map && i < map->n; 
++i20.3k
)
441
20.3k
    free(div_map[i]);
442
15.4k
  free(div_map);
443
15.4k
444
15.4k
  isl_basic_map_free(bmap);
445
15.4k
  isl_map_free(map);
446
15.4k
447
15.4k
  return isl_stat_ok;
448
2.88k
error:
449
2.88k
  isl_tab_free(tab);
450
2.88k
  free(snap);
451
2.88k
  free(n);
452
2.88k
  free(k);
453
7.98k
  for (i = 0; index && i < map->n; 
++i5.10k
)
454
5.10k
    free(index[i]);
455
2.88k
  free(index);
456
7.98k
  for (i = 0; div_map && i < map->n; 
++i5.10k
)
457
5.10k
    free(div_map[i]);
458
2.88k
  free(div_map);
459
2.88k
  isl_basic_map_free(bmap);
460
2.88k
  isl_map_free(map);
461
2.88k
  return isl_stat_error;
462
18.3k
}
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
11.5k
{
477
11.5k
  struct isl_subtract_diff_collector *sdc;
478
11.5k
  sdc = (struct isl_subtract_diff_collector *)dc;
479
11.5k
480
11.5k
  sdc->diff = isl_map_union_disjoint(sdc->diff,
481
11.5k
      isl_map_from_basic_map(bmap));
482
11.5k
483
11.5k
  return sdc->diff ? isl_stat_ok : 
isl_stat_error0
;
484
11.5k
}
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
11.8k
{
491
11.8k
  struct isl_subtract_diff_collector sdc;
492
11.8k
  sdc.dc.add = &basic_map_subtract_add;
493
11.8k
  sdc.diff = isl_map_empty(isl_basic_map_get_space(bmap));
494
11.8k
  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
11.8k
  return sdc.diff;
499
11.8k
}
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
1.75k
{
506
1.75k
  isl_space *space;
507
1.75k
508
1.75k
  space = isl_map_get_space(map1);
509
1.75k
  isl_map_free(map1);
510
1.75k
  isl_map_free(map2);
511
1.75k
  return isl_map_empty(space);
512
1.75k
}
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
11.8k
{
525
11.8k
  int i;
526
11.8k
  int equal, disjoint;
527
11.8k
  struct isl_map *diff;
528
11.8k
529
11.8k
  if (!map1 || !map2)
530
0
    goto error;
531
11.8k
532
11.8k
  isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
533
11.8k
534
11.8k
  equal = isl_map_plain_is_equal(map1, map2);
535
11.8k
  if (equal < 0)
536
0
    goto error;
537
11.8k
  if (equal)
538
1.75k
    return replace_pair_by_empty(map1, map2);
539
10.1k
540
10.1k
  disjoint = isl_map_is_disjoint(map1, map2);
541
10.1k
  if (disjoint < 0)
542
0
    goto error;
543
10.1k
  if (disjoint) {
544
5.68k
    isl_map_free(map2);
545
5.68k
    return map1;
546
5.68k
  }
547
4.44k
548
4.44k
  map1 = isl_map_compute_divs(map1);
549
4.44k
  map2 = isl_map_compute_divs(map2);
550
4.44k
  if (!map1 || !map2)
551
0
    goto error;
552
4.44k
553
4.44k
  map1 = isl_map_remove_empty_parts(map1);
554
4.44k
  map2 = isl_map_remove_empty_parts(map2);
555
4.44k
556
4.44k
  diff = isl_map_empty(isl_map_get_space(map1));
557
16.2k
  for (i = 0; i < map1->n; 
++i11.8k
) {
558
11.8k
    struct isl_map *d;
559
11.8k
    d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
560
11.8k
               isl_map_copy(map2));
561
11.8k
    if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
562
11.8k
      
diff = isl_map_union_disjoint(diff, d)3.27k
;
563
8.55k
    else
564
8.55k
      diff = isl_map_union(diff, d);
565
11.8k
  }
566
4.44k
567
4.44k
  isl_map_free(map1);
568
4.44k
  isl_map_free(map2);
569
4.44k
570
4.44k
  return diff;
571
0
error:
572
0
  isl_map_free(map1);
573
0
  isl_map_free(map2);
574
0
  return NULL;
575
11.8k
}
576
577
__isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
578
  __isl_take isl_map *map2)
579
11.8k
{
580
11.8k
  return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
581
11.8k
}
582
583
struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
584
8.59k
{
585
8.59k
  return set_from_map(isl_map_subtract(set_to_map(set1),
586
8.59k
              set_to_map(set2)));
587
8.59k
}
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
78
{
594
78
  isl_bool ok;
595
78
  isl_map *ext_dom;
596
78
597
78
  ok = isl_map_compatible_domain(map, dom);
598
78
  if (ok < 0)
599
0
    goto error;
600
78
  if (!ok)
601
78
    
isl_die0
(isl_set_get_ctx(dom), isl_error_invalid,
602
78
      "incompatible spaces", goto error);
603
78
  
604
78
  ext_dom = isl_map_universe(isl_map_get_space(map));
605
78
  ext_dom = isl_map_intersect_domain(ext_dom, dom);
606
78
  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
78
}
612
613
__isl_give isl_map *isl_map_subtract_domain(__isl_take isl_map *map,
614
  __isl_take isl_set *dom)
615
78
{
616
78
  return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
617
78
}
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
0
{
624
0
  isl_bool ok;
625
0
  isl_map *ext_dom;
626
0
627
0
  ok = isl_map_compatible_range(map, dom);
628
0
  if (ok < 0)
629
0
    goto error;
630
0
  if (!ok)
631
0
    isl_die(isl_set_get_ctx(dom), isl_error_invalid,
632
0
      "incompatible spaces", goto error);
633
0
  
634
0
  ext_dom = isl_map_universe(isl_map_get_space(map));
635
0
  ext_dom = isl_map_intersect_range(ext_dom, dom);
636
0
  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
0
}
642
643
__isl_give isl_map *isl_map_subtract_range(__isl_take isl_map *map,
644
  __isl_take isl_set *dom)
645
0
{
646
0
  return isl_map_align_params_map_map_and(map, dom, &map_subtract_range);
647
0
}
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
2.88k
{
662
2.88k
  struct isl_is_empty_diff_collector *edc;
663
2.88k
  edc = (struct isl_is_empty_diff_collector *)dc;
664
2.88k
665
2.88k
  edc->empty = 0;
666
2.88k
667
2.88k
  isl_basic_map_free(bmap);
668
2.88k
  return isl_stat_error;
669
2.88k
}
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
6.12k
{
677
6.12k
  isl_bool empty;
678
6.12k
  isl_stat r;
679
6.12k
  struct isl_is_empty_diff_collector edc;
680
6.12k
681
6.12k
  empty = isl_basic_map_plain_is_empty(bmap);
682
6.12k
  if (empty)
683
0
    return empty;
684
6.12k
685
6.12k
  edc.dc.add = &basic_map_is_empty_add;
686
6.12k
  edc.empty = isl_bool_true;
687
6.12k
  r = basic_map_collect_diff(isl_basic_map_copy(bmap),
688
6.12k
           isl_map_copy(map), &edc.dc);
689
6.12k
  if (!edc.empty)
690
2.88k
    return isl_bool_false;
691
3.23k
692
3.23k
  return r < 0 ? 
isl_bool_error0
: isl_bool_true;
693
6.12k
}
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
5.67k
{
701
5.67k
  int i;
702
5.67k
  isl_bool is_empty = isl_bool_true;
703
5.67k
704
5.67k
  if (!map1 || !map2)
705
0
    return isl_bool_error;
706
5.67k
  
707
8.91k
  
for (i = 0; 5.67k
i < map1->n;
++i3.23k
) {
708
6.12k
    is_empty = basic_map_diff_is_empty(map1->p[i], map2);
709
6.12k
    if (is_empty < 0 || !is_empty)
710
2.88k
       break;
711
6.12k
  }
712
5.67k
713
5.67k
  return is_empty;
714
5.67k
}
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
5.08k
{
720
5.08k
  if (!bmap)
721
0
    return isl_bool_error;
722
5.08k
  if (bmap->n_div)
723
358
    return isl_bool_false;
724
4.72k
  if (bmap->n_ineq)
725
4.08k
    return isl_bool_false;
726
648
  return bmap->n_eq == isl_basic_map_total_dim(bmap);
727
648
}
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
5.87k
{
733
5.87k
  if (!map)
734
0
    return isl_bool_error;
735
5.87k
  if (map->n != 1)
736
784
    return isl_bool_false;
737
5.08k
738
5.08k
  return isl_basic_map_plain_is_singleton(map->p[0]);
739
5.08k
}
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
194
{
747
194
  int j;
748
194
  unsigned dim;
749
194
  struct isl_vec *point;
750
194
  isl_int m;
751
194
752
194
  if (!bmap)
753
0
    return NULL;
754
194
755
194
  dim = isl_basic_map_total_dim(bmap);
756
194
  isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
757
194
  point = isl_vec_alloc(bmap->ctx, 1 + dim);
758
194
  if (!point)
759
0
    return NULL;
760
194
761
194
  isl_int_init(m);
762
194
763
194
  isl_int_set_si(point->el[0], 1);
764
415
  for (j = 0; j < bmap->n_eq; 
++j221
) {
765
221
    int i = dim - 1 - j;
766
221
    isl_assert(bmap->ctx,
767
221
        isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
768
221
        goto error);
769
221
    isl_assert(bmap->ctx,
770
221
        isl_int_is_one(bmap->eq[j][1 + i]) ||
771
221
        isl_int_is_negone(bmap->eq[j][1 + i]),
772
221
        goto error);
773
221
    isl_assert(bmap->ctx,
774
221
        isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
775
221
        goto error);
776
221
777
221
    isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
778
221
    isl_int_divexact(m, bmap->eq[j][1 + i], m);
779
221
    isl_int_abs(m, m);
780
221
    isl_seq_scale(point->el, point->el, m, 1 + i);
781
221
    isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
782
221
    isl_int_neg(m, m);
783
221
    isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
784
221
  }
785
194
786
194
  isl_int_clear(m);
787
194
  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
194
}
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
194
{
801
194
  int i;
802
194
  isl_bool is_subset = isl_bool_false;
803
194
  struct isl_point *point;
804
194
805
194
  if (!map1 || !map2)
806
0
    return isl_bool_error;
807
194
  if (map1->n != 1)
808
194
    
isl_die0
(isl_map_get_ctx(map1), isl_error_invalid,
809
194
      "expecting single-disjunct input",
810
194
      return isl_bool_error);
811
194
812
194
  point = singleton_extract_point(map1->p[0]);
813
194
  if (!point)
814
0
    return isl_bool_error;
815
194
816
228
  
for (i = 0; 194
i < map2->n;
++i34
) {
817
206
    is_subset = isl_basic_map_contains_point(map2->p[i], point);
818
206
    if (is_subset)
819
172
      break;
820
206
  }
821
194
822
194
  isl_point_free(point);
823
194
  return is_subset;
824
194
}
825
826
static isl_bool map_is_subset(__isl_keep isl_map *map1,
827
  __isl_keep isl_map *map2)
828
13.5k
{
829
13.5k
  isl_bool is_subset = isl_bool_false;
830
13.5k
  isl_bool empty, single;
831
13.5k
  isl_bool rat1, rat2;
832
13.5k
833
13.5k
  if (!map1 || !map2)
834
0
    return isl_bool_error;
835
13.5k
836
13.5k
  if (!isl_map_has_equal_space(map1, map2))
837
43
    return isl_bool_false;
838
13.4k
839
13.4k
  empty = isl_map_is_empty(map1);
840
13.4k
  if (empty < 0)
841
0
    return isl_bool_error;
842
13.4k
  if (empty)
843
40
    return isl_bool_true;
844
13.4k
845
13.4k
  empty = isl_map_is_empty(map2);
846
13.4k
  if (empty < 0)
847
0
    return isl_bool_error;
848
13.4k
  if (empty)
849
5.56k
    return isl_bool_false;
850
7.85k
851
7.85k
  rat1 = isl_map_has_rational(map1);
852
7.85k
  rat2 = isl_map_has_rational(map2);
853
7.85k
  if (rat1 < 0 || rat2 < 0)
854
0
    return isl_bool_error;
855
7.85k
  if (rat1 && 
!rat20
)
856
0
    return isl_bool_false;
857
7.85k
858
7.85k
  if (isl_map_plain_is_universe(map2))
859
1.98k
    return isl_bool_true;
860
5.87k
861
5.87k
  single = isl_map_plain_is_singleton(map1);
862
5.87k
  if (single < 0)
863
0
    return isl_bool_error;
864
5.87k
  map2 = isl_map_compute_divs(isl_map_copy(map2));
865
5.87k
  if (single) {
866
194
    is_subset = map_is_singleton_subset(map1, map2);
867
194
    isl_map_free(map2);
868
194
    return is_subset;
869
194
  }
870
5.67k
  is_subset = map_diff_is_empty(map1, map2);
871
5.67k
  isl_map_free(map2);
872
5.67k
873
5.67k
  return is_subset;
874
5.67k
}
875
876
isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
877
13.5k
{
878
13.5k
  return isl_map_align_params_map_map_and_test(map1, map2,
879
13.5k
              &map_is_subset);
880
13.5k
}
881
882
isl_bool isl_set_is_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
883
7.72k
{
884
7.72k
  return isl_map_is_subset(set_to_map(set1), set_to_map(set2));
885
7.72k
}
886
887
__isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
888
1.95k
{
889
1.95k
  int i;
890
1.95k
  struct isl_subtract_diff_collector sdc;
891
1.95k
  sdc.dc.add = &basic_map_subtract_add;
892
1.95k
893
1.95k
  if (!map)
894
0
    return NULL;
895
1.95k
  if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
896
1.95k
    
return map966
;
897
984
  if (map->n <= 1)
898
854
    return map;
899
130
900
130
  map = isl_map_compute_divs(map);
901
130
  map = isl_map_remove_empty_parts(map);
902
130
903
130
  if (!map || map->n <= 1)
904
0
    return map;
905
130
906
130
  sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
907
130
908
497
  for (i = 1; i < map->n; 
++i367
) {
909
367
    struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
910
367
    struct isl_map *copy = isl_map_copy(sdc.diff);
911
367
    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
367
  }
917
1.95k
918
1.95k
  isl_map_free(map);
919
1.95k
920
1.95k
  return sdc.diff;
921
1.95k
}
922
923
__isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
924
1.94k
{
925
1.94k
  return set_from_map(isl_map_make_disjoint(set_to_map(set)));
926
1.94k
}
927
928
__isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
929
1.95k
{
930
1.95k
  isl_map *universe;
931
1.95k
932
1.95k
  if (!map)
933
0
    return NULL;
934
1.95k
935
1.95k
  universe = isl_map_universe(isl_map_get_space(map));
936
1.95k
937
1.95k
  return isl_map_subtract(universe, map);
938
1.95k
}
939
940
__isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
941
1.95k
{
942
1.95k
  return isl_map_complement(set);
943
1.95k
}