Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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
268k
{
35
268k
  int i;
36
268k
37
268k
  isl_seq_cpy(v->el, c, 1 + dim);
38
268k
  isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim));
39
268k
40
304k
  for (i = 0; 
i < n_div304k
;
++i35.7k
) {
41
35.7k
    int pos = 1 + dim + div_map[i];
42
35.7k
    isl_int_add(v->el[pos], v->el[pos], c[1 + dim + i]);
43
35.7k
  }
44
268k
}
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
73.2k
{
52
73.2k
  int i;
53
73.2k
  unsigned dim;
54
73.2k
  unsigned tab_total;
55
73.2k
  unsigned bmap_total;
56
73.2k
  isl_vec *v;
57
73.2k
58
73.2k
  if (
!tab || 73.2k
!bmap73.2k
)
59
0
    return isl_stat_error;
60
73.2k
61
73.2k
  tab_total = isl_basic_map_total_dim(tab->bmap);
62
73.2k
  bmap_total = isl_basic_map_total_dim(bmap);
63
73.2k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
64
73.2k
65
73.2k
  if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
66
0
    return isl_stat_error;
67
73.2k
68
73.2k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
69
73.2k
  if (!v)
70
0
    return isl_stat_error;
71
73.2k
72
99.6k
  
for (i = 0; 73.2k
i < bmap->n_eq99.6k
;
++i26.3k
) {
73
31.3k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
74
31.3k
    if (isl_tab_add_ineq(tab, v->el) < 0)
75
0
      goto error;
76
31.3k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
77
31.3k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
78
31.3k
    if (isl_tab_add_ineq(tab, v->el) < 0)
79
0
      goto error;
80
31.3k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
81
31.3k
    if (tab->empty)
82
4.93k
      break;
83
31.3k
  }
84
73.2k
85
207k
  
for (i = 0; 73.2k
i < bmap->n_ineq207k
;
++i134k
) {
86
162k
    expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
87
162k
    if (isl_tab_add_ineq(tab, v->el) < 0)
88
0
      goto error;
89
162k
    
if (162k
tab->empty162k
)
90
28.2k
      break;
91
162k
  }
92
73.2k
93
73.2k
  isl_vec_free(v);
94
73.2k
  return isl_stat_ok;
95
0
error:
96
0
  isl_vec_free(v);
97
0
  return isl_stat_error;
98
73.2k
}
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
43.2k
{
112
43.2k
  unsigned dim;
113
43.2k
  unsigned tab_total;
114
43.2k
  unsigned bmap_total;
115
43.2k
  isl_vec *v;
116
43.2k
  isl_stat r;
117
43.2k
118
43.2k
  if (
!tab || 43.2k
!bmap43.2k
)
119
0
    return isl_stat_error;
120
43.2k
121
43.2k
  tab_total = isl_basic_map_total_dim(tab->bmap);
122
43.2k
  bmap_total = isl_basic_map_total_dim(bmap);
123
43.2k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
124
43.2k
125
43.2k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
126
43.2k
  if (!v)
127
0
    return isl_stat_error;
128
43.2k
129
43.2k
  
if (43.2k
c < 2 * bmap->n_eq43.2k
) {
130
10.6k
    if ((c % 2) != oppose)
131
5.58k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
132
5.58k
          1 + bmap_total);
133
10.6k
    if (oppose)
134
7.94k
      isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
135
10.6k
    expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
136
10.6k
    r = isl_tab_add_ineq(tab, v->el);
137
10.6k
    if (oppose)
138
7.94k
      isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
139
10.6k
    if ((c % 2) != oppose)
140
5.58k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
141
5.58k
          1 + bmap_total);
142
43.2k
  } else {
143
32.6k
    c -= 2 * bmap->n_eq;
144
32.6k
    if (
oppose32.6k
) {
145
27.2k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
146
27.2k
          1 + bmap_total);
147
27.2k
      isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
148
27.2k
    }
149
32.6k
    expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
150
32.6k
    r = isl_tab_add_ineq(tab, v->el);
151
32.6k
    if (
oppose32.6k
) {
152
27.2k
      isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
153
27.2k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
154
27.2k
          1 + bmap_total);
155
27.2k
    }
156
32.6k
  }
157
43.2k
158
43.2k
  isl_vec_free(v);
159
43.2k
  return r;
160
43.2k
}
161
162
static isl_stat tab_add_divs(struct isl_tab *tab,
163
  __isl_keep isl_basic_map *bmap, int **div_map)
164
73.2k
{
165
73.2k
  int i, j;
166
73.2k
  struct isl_vec *vec;
167
73.2k
  unsigned total;
168
73.2k
  unsigned dim;
169
73.2k
170
73.2k
  if (!bmap)
171
0
    return isl_stat_error;
172
73.2k
  
if (73.2k
!bmap->n_div73.2k
)
173
69.1k
    return isl_stat_ok;
174
4.05k
175
4.05k
  
if (4.05k
!*div_map4.05k
)
176
3.31k
    
*div_map = 3.31k
isl_alloc_array3.31k
(bmap->ctx, int, bmap->n_div);
177
4.05k
  if (!*div_map)
178
0
    return isl_stat_error;
179
4.05k
180
4.05k
  total = isl_basic_map_total_dim(tab->bmap);
181
4.05k
  dim = total - tab->bmap->n_div;
182
4.05k
  vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
183
4.05k
  if (!vec)
184
0
    return isl_stat_error;
185
4.05k
186
9.35k
  
for (i = 0; 4.05k
i < bmap->n_div9.35k
;
++i5.30k
) {
187
5.30k
    isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
188
5.30k
    isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
189
6.78k
    for (j = 0; 
j < i6.78k
;
++j1.48k
)
190
1.48k
      isl_int_set(vec->el[2 + dim + (*div_map)[j]],
191
5.30k
          bmap->div[i][2 + dim + j]);
192
8.23k
    for (j = 0; 
j < tab->bmap->n_div8.23k
;
++j2.93k
)
193
6.88k
      
if (6.88k
isl_seq_eq(tab->bmap->div[j],
194
6.88k
          vec->el, 2 + dim + tab->bmap->n_div))
195
3.94k
        break;
196
5.30k
    (*div_map)[i] = j;
197
5.30k
    if (
j == tab->bmap->n_div5.30k
) {
198
1.35k
      vec->size = 2 + dim + tab->bmap->n_div;
199
1.35k
      if (isl_tab_add_div(tab, vec) < 0)
200
0
        goto error;
201
1.35k
    }
202
5.30k
  }
203
4.05k
204
4.05k
  isl_vec_free(vec);
205
4.05k
206
4.05k
  return isl_stat_ok;
207
0
error:
208
0
  isl_vec_free(vec);
209
0
210
0
  return isl_stat_error;
211
73.2k
}
212
213
/* Freeze all constraints of tableau tab.
214
 */
215
static int tab_freeze_constraints(struct isl_tab *tab)
216
73.2k
{
217
73.2k
  int i;
218
73.2k
219
446k
  for (i = 0; 
i < tab->n_con446k
;
++i373k
)
220
373k
    
if (373k
isl_tab_freeze_constraint(tab, i) < 0373k
)
221
0
      return -1;
222
73.2k
223
73.2k
  return 0;
224
73.2k
}
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
44.1k
{
233
44.1k
  int i, n;
234
44.1k
  int n_test = tab->n_con - offset;
235
44.1k
236
44.1k
  if (isl_tab_detect_redundant(tab) < 0)
237
0
    return -1;
238
44.1k
239
44.1k
  
if (44.1k
n_test == 044.1k
)
240
1.94k
    return 0;
241
42.2k
  
if (42.2k
!*index42.2k
)
242
41.7k
    
*index = 41.7k
isl_alloc_array41.7k
(ctx, int, n_test);
243
42.2k
  if (!*index)
244
0
    return -1;
245
42.2k
246
191k
  
for (n = 0, i = 0; 42.2k
i < n_test191k
;
++i149k
) {
247
149k
    int r;
248
149k
    r = isl_tab_is_redundant(tab, offset + i);
249
149k
    if (r < 0)
250
0
      return -1;
251
149k
    
if (149k
r149k
)
252
106k
      continue;
253
42.8k
    (*index)[n++] = i;
254
42.8k
  }
255
42.2k
256
42.2k
  return n;
257
44.1k
}
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
43.3k
{
300
43.3k
  int i;
301
43.3k
  int modified;
302
43.3k
  int level;
303
43.3k
  int init;
304
43.3k
  isl_bool empty;
305
43.3k
  isl_ctx *ctx;
306
43.3k
  struct isl_tab *tab = NULL;
307
43.3k
  struct isl_tab_undo **snap = NULL;
308
43.3k
  int *k = NULL;
309
43.3k
  int *n = NULL;
310
43.3k
  int **index = NULL;
311
43.3k
  int **div_map = NULL;
312
43.3k
313
43.3k
  empty = isl_basic_map_is_empty(bmap);
314
43.3k
  if (
empty43.3k
) {
315
0
    isl_basic_map_free(bmap);
316
0
    isl_map_free(map);
317
0
    return empty < 0 ? 
isl_stat_error0
:
isl_stat_ok0
;
318
0
  }
319
43.3k
320
43.3k
  bmap = isl_basic_map_cow(bmap);
321
43.3k
  map = isl_map_cow(map);
322
43.3k
323
43.3k
  if (
!bmap || 43.3k
!map43.3k
)
324
0
    goto error;
325
43.3k
326
43.3k
  ctx = map->ctx;
327
43.3k
  snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
328
43.3k
  k = isl_alloc_array(map->ctx, int, map->n);
329
43.3k
  n = isl_alloc_array(map->ctx, int, map->n);
330
43.3k
  index = isl_calloc_array(map->ctx, int *, map->n);
331
43.3k
  div_map = isl_calloc_array(map->ctx, int *, map->n);
332
43.3k
  if (
!snap || 43.3k
!k43.3k
||
!n43.3k
||
!index43.3k
||
!div_map43.3k
)
333
0
    goto error;
334
43.3k
335
43.3k
  bmap = isl_basic_map_order_divs(bmap);
336
43.3k
  map = isl_map_order_divs(map);
337
43.3k
338
43.3k
  tab = isl_tab_from_basic_map(bmap, 1);
339
43.3k
  if (!tab)
340
0
    goto error;
341
43.3k
342
43.3k
  modified = 0;
343
43.3k
  level = 0;
344
43.3k
  init = 1;
345
43.3k
346
177k
  while (
level >= 0177k
) {
347
149k
    if (
level >= map->n149k
) {
348
34.4k
      int empty;
349
34.4k
      struct isl_basic_map *bm;
350
34.4k
      if (
!modified34.4k
) {
351
7.39k
        if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
352
1.28k
          goto error;
353
6.11k
        break;
354
6.11k
      }
355
27.0k
      bm = isl_basic_map_copy(tab->bmap);
356
27.0k
      bm = isl_basic_map_cow(bm);
357
27.0k
      bm = isl_basic_map_update_from_tab(bm, tab);
358
27.0k
      bm = isl_basic_map_simplify(bm);
359
27.0k
      bm = isl_basic_map_finalize(bm);
360
27.0k
      empty = isl_basic_map_is_empty(bm);
361
27.0k
      if (empty)
362
841
        isl_basic_map_free(bm);
363
26.2k
      else 
if (26.2k
dc->add(dc, bm) < 026.2k
)
364
7.49k
        goto error;
365
19.5k
      
if (19.5k
empty < 019.5k
)
366
0
        goto error;
367
19.5k
      level--;
368
19.5k
      init = 0;
369
19.5k
      continue;
370
19.5k
    }
371
114k
    
if (114k
init114k
) {
372
73.2k
      int offset;
373
73.2k
      struct isl_tab_undo *snap2;
374
73.2k
      snap2 = isl_tab_snap(tab);
375
73.2k
      if (tab_add_divs(tab, map->p[level],
376
73.2k
           &div_map[level]) < 0)
377
0
        goto error;
378
73.2k
      offset = tab->n_con;
379
73.2k
      snap[level] = isl_tab_snap(tab);
380
73.2k
      if (tab_freeze_constraints(tab) < 0)
381
0
        goto error;
382
73.2k
      
if (73.2k
tab_add_constraints(tab, map->p[level],
383
73.2k
            div_map[level]) < 0)
384
0
        goto error;
385
73.2k
      k[level] = 0;
386
73.2k
      n[level] = 0;
387
73.2k
      if (
tab->empty73.2k
) {
388
29.0k
        if (isl_tab_rollback(tab, snap2) < 0)
389
0
          goto error;
390
29.0k
        level++;
391
29.0k
        continue;
392
29.0k
      }
393
44.1k
      modified = 1;
394
44.1k
      n[level] = n_non_redundant(ctx, tab, offset,
395
44.1k
                &index[level]);
396
44.1k
      if (n[level] < 0)
397
0
        goto error;
398
44.1k
      
if (44.1k
n[level] == 044.1k
) {
399
16.9k
        level--;
400
16.9k
        init = 0;
401
16.9k
        continue;
402
16.9k
      }
403
27.2k
      
if (27.2k
isl_tab_rollback(tab, snap[level]) < 027.2k
)
404
0
        goto error;
405
27.2k
      
if (27.2k
tab_add_constraint(tab, map->p[level],
406
27.2k
          div_map[level], index[level][0], 1) < 0)
407
0
        goto error;
408
27.2k
      level++;
409
27.2k
      continue;
410
0
    } else {
411
41.4k
      if (
k[level] + 1 >= n[level]41.4k
) {
412
33.4k
        level--;
413
33.4k
        continue;
414
33.4k
      }
415
7.98k
      
if (7.98k
isl_tab_rollback(tab, snap[level]) < 07.98k
)
416
0
        goto error;
417
7.98k
      
if (7.98k
tab_add_constraint(tab, map->p[level],
418
7.98k
            div_map[level],
419
7.98k
            index[level][k[level]], 0) < 0)
420
0
        goto error;
421
7.98k
      snap[level] = isl_tab_snap(tab);
422
7.98k
      k[level]++;
423
7.98k
      if (tab_add_constraint(tab, map->p[level],
424
7.98k
            div_map[level],
425
7.98k
            index[level][k[level]], 1) < 0)
426
0
        goto error;
427
7.98k
      level++;
428
7.98k
      init = 1;
429
7.98k
      continue;
430
7.98k
    }
431
149k
  }
432
43.3k
433
34.6k
  isl_tab_free(tab);
434
34.6k
  free(snap);
435
34.6k
  free(n);
436
34.6k
  free(k);
437
91.4k
  for (i = 0; 
index && 91.4k
i < map->n91.4k
;
++i56.8k
)
438
56.8k
    free(index[i]);
439
34.6k
  free(index);
440
91.4k
  for (i = 0; 
div_map && 91.4k
i < map->n91.4k
;
++i56.8k
)
441
56.8k
    free(div_map[i]);
442
34.6k
  free(div_map);
443
34.6k
444
34.6k
  isl_basic_map_free(bmap);
445
34.6k
  isl_map_free(map);
446
34.6k
447
34.6k
  return isl_stat_ok;
448
8.77k
error:
449
8.77k
  isl_tab_free(tab);
450
8.77k
  free(snap);
451
8.77k
  free(n);
452
8.77k
  free(k);
453
24.4k
  for (i = 0; 
index && 24.4k
i < map->n24.4k
;
++i15.6k
)
454
15.6k
    free(index[i]);
455
8.77k
  free(index);
456
24.4k
  for (i = 0; 
div_map && 24.4k
i < map->n24.4k
;
++i15.6k
)
457
15.6k
    free(div_map[i]);
458
8.77k
  free(div_map);
459
8.77k
  isl_basic_map_free(bmap);
460
8.77k
  isl_map_free(map);
461
8.77k
  return isl_stat_error;
462
43.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
24.8k
{
477
24.8k
  struct isl_subtract_diff_collector *sdc;
478
24.8k
  sdc = (struct isl_subtract_diff_collector *)dc;
479
24.8k
480
24.8k
  sdc->diff = isl_map_union_disjoint(sdc->diff,
481
24.8k
      isl_map_from_basic_map(bmap));
482
24.8k
483
24.8k
  return sdc->diff ? 
isl_stat_ok24.8k
:
isl_stat_error0
;
484
24.8k
}
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
23.4k
{
491
23.4k
  struct isl_subtract_diff_collector sdc;
492
23.4k
  sdc.dc.add = &basic_map_subtract_add;
493
23.4k
  sdc.diff = isl_map_empty(isl_basic_map_get_space(bmap));
494
23.4k
  if (
basic_map_collect_diff(bmap, map, &sdc.dc) < 023.4k
) {
495
0
    isl_map_free(sdc.diff);
496
0
    sdc.diff = NULL;
497
0
  }
498
23.4k
  return sdc.diff;
499
23.4k
}
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.60k
{
506
4.60k
  isl_space *space;
507
4.60k
508
4.60k
  space = isl_map_get_space(map1);
509
4.60k
  isl_map_free(map1);
510
4.60k
  isl_map_free(map2);
511
4.60k
  return isl_map_empty(space);
512
4.60k
}
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
30.9k
{
525
30.9k
  int i;
526
30.9k
  int equal, disjoint;
527
30.9k
  struct isl_map *diff;
528
30.9k
529
30.9k
  if (
!map1 || 30.9k
!map230.9k
)
530
0
    goto error;
531
30.9k
532
30.9k
  
isl_assert30.9k
(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
533
30.9k
534
30.9k
  equal = isl_map_plain_is_equal(map1, map2);
535
30.9k
  if (equal < 0)
536
0
    goto error;
537
30.9k
  
if (30.9k
equal30.9k
)
538
4.60k
    return replace_pair_by_empty(map1, map2);
539
26.3k
540
26.3k
  disjoint = isl_map_is_disjoint(map1, map2);
541
26.3k
  if (disjoint < 0)
542
0
    goto error;
543
26.3k
  
if (26.3k
disjoint26.3k
) {
544
15.1k
    isl_map_free(map2);
545
15.1k
    return map1;
546
15.1k
  }
547
11.1k
548
11.1k
  map1 = isl_map_compute_divs(map1);
549
11.1k
  map2 = isl_map_compute_divs(map2);
550
11.1k
  if (
!map1 || 11.1k
!map211.1k
)
551
0
    goto error;
552
11.1k
553
11.1k
  map1 = isl_map_remove_empty_parts(map1);
554
11.1k
  map2 = isl_map_remove_empty_parts(map2);
555
11.1k
556
11.1k
  diff = isl_map_empty(isl_map_get_space(map1));
557
34.6k
  for (i = 0; 
i < map1->n34.6k
;
++i23.4k
) {
558
23.4k
    struct isl_map *d;
559
23.4k
    d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
560
23.4k
               isl_map_copy(map2));
561
23.4k
    if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
562
8.89k
      diff = isl_map_union_disjoint(diff, d);
563
23.4k
    else
564
14.6k
      diff = isl_map_union(diff, d);
565
23.4k
  }
566
11.1k
567
11.1k
  isl_map_free(map1);
568
11.1k
  isl_map_free(map2);
569
11.1k
570
11.1k
  return diff;
571
0
error:
572
0
  isl_map_free(map1);
573
0
  isl_map_free(map2);
574
0
  return NULL;
575
30.9k
}
576
577
__isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
578
  __isl_take isl_map *map2)
579
30.9k
{
580
30.9k
  return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
581
30.9k
}
582
583
struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
584
20.6k
{
585
20.6k
  return set_from_map(isl_map_subtract(set_to_map(set1),
586
20.6k
              set_to_map(set2)));
587
20.6k
}
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
90
{
594
90
  isl_bool ok;
595
90
  isl_map *ext_dom;
596
90
597
90
  ok = isl_map_compatible_domain(map, dom);
598
90
  if (ok < 0)
599
0
    goto error;
600
90
  
if (90
!ok90
)
601
0
    isl_die(isl_set_get_ctx(dom), isl_error_invalid,
602
90
      "incompatible spaces", goto error);
603
90
  
604
90
  ext_dom = isl_map_universe(isl_map_get_space(map));
605
90
  ext_dom = isl_map_intersect_domain(ext_dom, dom);
606
90
  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
90
}
612
613
__isl_give isl_map *isl_map_subtract_domain(__isl_take isl_map *map,
614
  __isl_take isl_set *dom)
615
90
{
616
90
  return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
617
90
}
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 (2
!ok2
)
631
0
    isl_die(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.77k
{
662
8.77k
  struct isl_is_empty_diff_collector *edc;
663
8.77k
  edc = (struct isl_is_empty_diff_collector *)dc;
664
8.77k
665
8.77k
  edc->empty = 0;
666
8.77k
667
8.77k
  isl_basic_map_free(bmap);
668
8.77k
  return isl_stat_error;
669
8.77k
}
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
18.8k
{
677
18.8k
  isl_bool empty;
678
18.8k
  isl_stat r;
679
18.8k
  struct isl_is_empty_diff_collector edc;
680
18.8k
681
18.8k
  empty = isl_basic_map_plain_is_empty(bmap);
682
18.8k
  if (empty)
683
1
    return empty;
684
18.8k
685
18.8k
  edc.dc.add = &basic_map_is_empty_add;
686
18.8k
  edc.empty = isl_bool_true;
687
18.8k
  r = basic_map_collect_diff(isl_basic_map_copy(bmap),
688
18.8k
           isl_map_copy(map), &edc.dc);
689
18.8k
  if (!edc.empty)
690
8.77k
    return isl_bool_false;
691
10.0k
692
10.0k
  
return r < 0 ? 10.0k
isl_bool_error0
:
isl_bool_true10.0k
;
693
18.8k
}
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
17.6k
{
701
17.6k
  int i;
702
17.6k
  isl_bool is_empty = isl_bool_true;
703
17.6k
704
17.6k
  if (
!map1 || 17.6k
!map217.6k
)
705
0
    return isl_bool_error;
706
17.6k
  
707
27.7k
  
for (i = 0; 17.6k
i < map1->n27.7k
;
++i10.0k
) {
708
18.8k
    is_empty = basic_map_diff_is_empty(map1->p[i], map2);
709
18.8k
    if (
is_empty < 0 || 18.8k
!is_empty18.8k
)
710
8.77k
       break;
711
18.8k
  }
712
17.6k
713
17.6k
  return is_empty;
714
17.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
16.2k
{
720
16.2k
  if (!bmap)
721
0
    return isl_bool_error;
722
16.2k
  
if (16.2k
bmap->n_div16.2k
)
723
1.72k
    return isl_bool_false;
724
14.4k
  
if (14.4k
bmap->n_ineq14.4k
)
725
12.3k
    return isl_bool_false;
726
2.11k
  return bmap->n_eq == isl_basic_map_total_dim(bmap);
727
2.11k
}
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.0k
{
733
18.0k
  if (!map)
734
0
    return isl_bool_error;
735
18.0k
  
if (18.0k
map->n != 118.0k
)
736
1.87k
    return isl_bool_false;
737
16.2k
738
16.2k
  return isl_basic_map_plain_is_singleton(map->p[0]);
739
16.2k
}
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
399
{
747
399
  int j;
748
399
  unsigned dim;
749
399
  struct isl_vec *point;
750
399
  isl_int m;
751
399
752
399
  if (!bmap)
753
0
    return NULL;
754
399
755
399
  dim = isl_basic_map_total_dim(bmap);
756
399
  isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
757
399
  point = isl_vec_alloc(bmap->ctx, 1 + dim);
758
399
  if (!point)
759
0
    return NULL;
760
399
761
399
  
isl_int_init399
(m);
762
399
763
399
  isl_int_set_si(point->el[0], 1);
764
880
  for (j = 0; 
j < bmap->n_eq880
;
++j481
) {
765
481
    int i = dim - 1 - j;
766
481
    isl_assert(bmap->ctx,
767
481
        isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
768
481
        goto error);
769
481
    
isl_assert481
(bmap->ctx,
770
481
        isl_int_is_one(bmap->eq[j][1 + i]) ||
771
481
        isl_int_is_negone(bmap->eq[j][1 + i]),
772
481
        goto error);
773
481
    
isl_assert481
(bmap->ctx,
774
481
        isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
775
481
        goto error);
776
481
777
481
    
isl_int_gcd481
(m, point->el[0], bmap->eq[j][1 + i]);
778
481
    isl_int_divexact(m, bmap->eq[j][1 + i], m);
779
481
    isl_int_abs(m, m);
780
481
    isl_seq_scale(point->el, point->el, m, 1 + i);
781
481
    isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
782
481
    isl_int_neg(m, m);
783
481
    isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
784
481
  }
785
399
786
399
  
isl_int_clear399
(m);
787
399
  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
399
}
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
399
{
801
399
  int i;
802
399
  isl_bool is_subset = isl_bool_false;
803
399
  struct isl_point *point;
804
399
805
399
  if (
!map1 || 399
!map2399
)
806
0
    return isl_bool_error;
807
399
  
if (399
map1->n != 1399
)
808
0
    isl_die(isl_map_get_ctx(map1), isl_error_invalid,
809
399
      "expecting single-disjunct input",
810
399
      return isl_bool_error);
811
399
812
399
  point = singleton_extract_point(map1->p[0]);
813
399
  if (!point)
814
0
    return isl_bool_error;
815
399
816
465
  
for (i = 0; 399
i < map2->n465
;
++i66
) {
817
421
    is_subset = isl_basic_map_contains_point(map2->p[i], point);
818
421
    if (is_subset)
819
355
      break;
820
421
  }
821
399
822
399
  isl_point_free(point);
823
399
  return is_subset;
824
399
}
825
826
static isl_bool map_is_subset(__isl_keep isl_map *map1,
827
  __isl_keep isl_map *map2)
828
41.4k
{
829
41.4k
  isl_bool is_subset = isl_bool_false;
830
41.4k
  isl_bool empty, single;
831
41.4k
  isl_bool rat1, rat2;
832
41.4k
833
41.4k
  if (
!map1 || 41.4k
!map241.4k
)
834
0
    return isl_bool_error;
835
41.4k
836
41.4k
  
if (41.4k
!isl_map_has_equal_space(map1, map2)41.4k
)
837
149
    return isl_bool_false;
838
41.3k
839
41.3k
  empty = isl_map_is_empty(map1);
840
41.3k
  if (empty < 0)
841
0
    return isl_bool_error;
842
41.3k
  
if (41.3k
empty41.3k
)
843
133
    return isl_bool_true;
844
41.1k
845
41.1k
  empty = isl_map_is_empty(map2);
846
41.1k
  if (empty < 0)
847
0
    return isl_bool_error;
848
41.1k
  
if (41.1k
empty41.1k
)
849
16.5k
    return isl_bool_false;
850
24.6k
851
24.6k
  rat1 = isl_map_has_rational(map1);
852
24.6k
  rat2 = isl_map_has_rational(map2);
853
24.6k
  if (
rat1 < 0 || 24.6k
rat2 < 024.6k
)
854
0
    return isl_bool_error;
855
24.6k
  
if (24.6k
rat1 && 24.6k
!rat214
)
856
2
    return isl_bool_false;
857
24.6k
858
24.6k
  
if (24.6k
isl_map_plain_is_universe(map2)24.6k
)
859
6.51k
    return isl_bool_true;
860
18.0k
861
18.0k
  single = isl_map_plain_is_singleton(map1);
862
18.0k
  if (single < 0)
863
0
    return isl_bool_error;
864
18.0k
  map2 = isl_map_compute_divs(isl_map_copy(map2));
865
18.0k
  if (
single18.0k
) {
866
399
    is_subset = map_is_singleton_subset(map1, map2);
867
399
    isl_map_free(map2);
868
399
    return is_subset;
869
399
  }
870
17.6k
  is_subset = map_diff_is_empty(map1, map2);
871
17.6k
  isl_map_free(map2);
872
17.6k
873
17.6k
  return is_subset;
874
17.6k
}
875
876
isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
877
41.4k
{
878
41.4k
  return isl_map_align_params_map_map_and_test(map1, map2,
879
41.4k
              &map_is_subset);
880
41.4k
}
881
882
isl_bool isl_set_is_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
883
23.7k
{
884
23.7k
  return isl_map_is_subset(set_to_map(set1), set_to_map(set2));
885
23.7k
}
886
887
__isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
888
6.56k
{
889
6.56k
  int i;
890
6.56k
  struct isl_subtract_diff_collector sdc;
891
6.56k
  sdc.dc.add = &basic_map_subtract_add;
892
6.56k
893
6.56k
  if (!map)
894
0
    return NULL;
895
6.56k
  
if (6.56k
ISL_F_ISSET6.56k
(map, ISL_MAP_DISJOINT))
896
3.46k
    return map;
897
3.09k
  
if (3.09k
map->n <= 13.09k
)
898
2.73k
    return map;
899
367
900
367
  map = isl_map_compute_divs(map);
901
367
  map = isl_map_remove_empty_parts(map);
902
367
903
367
  if (
!map || 367
map->n <= 1367
)
904
0
    return map;
905
367
906
367
  sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
907
367
908
1.45k
  for (i = 1; 
i < map->n1.45k
;
++i1.08k
) {
909
1.08k
    struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
910
1.08k
    struct isl_map *copy = isl_map_copy(sdc.diff);
911
1.08k
    if (
basic_map_collect_diff(bmap, copy, &sdc.dc) < 01.08k
) {
912
0
      isl_map_free(sdc.diff);
913
0
      sdc.diff = NULL;
914
0
      break;
915
0
    }
916
1.08k
  }
917
6.56k
918
6.56k
  isl_map_free(map);
919
6.56k
920
6.56k
  return sdc.diff;
921
6.56k
}
922
923
__isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
924
6.54k
{
925
6.54k
  return set_from_map(isl_map_make_disjoint(set_to_map(set)));
926
6.54k
}
927
928
__isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
929
6.09k
{
930
6.09k
  isl_map *universe;
931
6.09k
932
6.09k
  if (!map)
933
0
    return NULL;
934
6.09k
935
6.09k
  universe = isl_map_universe(isl_map_get_space(map));
936
6.09k
937
6.09k
  return isl_map_subtract(universe, map);
938
6.09k
}
939
940
__isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
941
6.09k
{
942
6.09k
  return isl_map_complement(set);
943
6.09k
}