Coverage Report

Created: 2017-03-27 23:01

/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
179k
{
35
179k
  int i;
36
179k
37
179k
  isl_seq_cpy(v->el, c, 1 + dim);
38
179k
  isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim));
39
179k
40
205k
  for (i = 0; 
i < n_div205k
;
++i25.3k
)
{25.3k
41
25.3k
    int pos = 1 + dim + div_map[i];
42
25.3k
    isl_int_add(v->el[pos], v->el[pos], c[1 + dim + i]);
43
25.3k
  }
44
179k
}
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
53.2k
{
52
53.2k
  int i;
53
53.2k
  unsigned dim;
54
53.2k
  unsigned tab_total;
55
53.2k
  unsigned bmap_total;
56
53.2k
  isl_vec *v;
57
53.2k
58
53.2k
  if (
!tab || 53.2k
!bmap53.2k
)
59
0
    return isl_stat_error;
60
53.2k
61
53.2k
  tab_total = isl_basic_map_total_dim(tab->bmap);
62
53.2k
  bmap_total = isl_basic_map_total_dim(bmap);
63
53.2k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
64
53.2k
65
53.2k
  if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
66
0
    return isl_stat_error;
67
53.2k
68
53.2k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
69
53.2k
  if (!v)
70
0
    return isl_stat_error;
71
53.2k
72
66.8k
  
for (i = 0; 53.2k
i < bmap->n_eq66.8k
;
++i13.6k
)
{15.2k
73
15.2k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
74
15.2k
    if (isl_tab_add_ineq(tab, v->el) < 0)
75
0
      goto error;
76
15.2k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
77
15.2k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
78
15.2k
    if (isl_tab_add_ineq(tab, v->el) < 0)
79
0
      goto error;
80
15.2k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
81
15.2k
    if (tab->empty)
82
1.59k
      break;
83
15.2k
  }
84
53.2k
85
147k
  
for (i = 0; 53.2k
i < bmap->n_ineq147k
;
++i94.4k
)
{115k
86
115k
    expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
87
115k
    if (isl_tab_add_ineq(tab, v->el) < 0)
88
0
      goto error;
89
115k
    
if (115k
tab->empty115k
)
90
21.4k
      break;
91
115k
  }
92
53.2k
93
53.2k
  isl_vec_free(v);
94
53.2k
  return isl_stat_ok;
95
0
error:
96
0
  isl_vec_free(v);
97
0
  return isl_stat_error;
98
53.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
33.2k
{
112
33.2k
  unsigned dim;
113
33.2k
  unsigned tab_total;
114
33.2k
  unsigned bmap_total;
115
33.2k
  isl_vec *v;
116
33.2k
  isl_stat r;
117
33.2k
118
33.2k
  if (
!tab || 33.2k
!bmap33.2k
)
119
0
    return isl_stat_error;
120
33.2k
121
33.2k
  tab_total = isl_basic_map_total_dim(tab->bmap);
122
33.2k
  bmap_total = isl_basic_map_total_dim(bmap);
123
33.2k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
124
33.2k
125
33.2k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
126
33.2k
  if (!v)
127
0
    return isl_stat_error;
128
33.2k
129
33.2k
  
if (33.2k
c < 2 * bmap->n_eq33.2k
)
{6.58k
130
6.58k
    if ((c % 2) != oppose)
131
3.50k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
132
3.50k
          1 + bmap_total);
133
6.58k
    if (oppose)
134
4.97k
      isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
135
6.58k
    expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
136
6.58k
    r = isl_tab_add_ineq(tab, v->el);
137
6.58k
    if (oppose)
138
4.97k
      isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
139
6.58k
    if ((c % 2) != oppose)
140
3.50k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
141
3.50k
          1 + bmap_total);
142
26.6k
  } else {
143
26.6k
    c -= 2 * bmap->n_eq;
144
26.6k
    if (
oppose26.6k
)
{22.2k
145
22.2k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
146
22.2k
          1 + bmap_total);
147
22.2k
      isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
148
22.2k
    }
149
26.6k
    expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
150
26.6k
    r = isl_tab_add_ineq(tab, v->el);
151
26.6k
    if (
oppose26.6k
)
{22.2k
152
22.2k
      isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
153
22.2k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
154
22.2k
          1 + bmap_total);
155
22.2k
    }
156
26.6k
  }
157
33.2k
158
33.2k
  isl_vec_free(v);
159
33.2k
  return r;
160
33.2k
}
161
162
static isl_stat tab_add_divs(struct isl_tab *tab,
163
  __isl_keep isl_basic_map *bmap, int **div_map)
164
53.2k
{
165
53.2k
  int i, j;
166
53.2k
  struct isl_vec *vec;
167
53.2k
  unsigned total;
168
53.2k
  unsigned dim;
169
53.2k
170
53.2k
  if (!bmap)
171
0
    return isl_stat_error;
172
53.2k
  
if (53.2k
!bmap->n_div53.2k
)
173
51.3k
    return isl_stat_ok;
174
53.2k
175
1.92k
  
if (1.92k
!*div_map1.92k
)
176
1.44k
    
*div_map = 1.44k
isl_alloc_array1.44k
(bmap->ctx, int, bmap->n_div);
177
1.92k
  if (!*div_map)
178
0
    return isl_stat_error;
179
1.92k
180
1.92k
  total = isl_basic_map_total_dim(tab->bmap);
181
1.92k
  dim = total - tab->bmap->n_div;
182
1.92k
  vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
183
1.92k
  if (!vec)
184
0
    return isl_stat_error;
185
1.92k
186
4.87k
  
for (i = 0; 1.92k
i < bmap->n_div4.87k
;
++i2.95k
)
{2.95k
187
2.95k
    isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
188
2.95k
    isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
189
4.63k
    for (j = 0; 
j < i4.63k
;
++j1.68k
)
190
1.68k
      isl_int_set(vec->el[2 + dim + (*div_map)[j]],
191
2.95k
          bmap->div[i][2 + dim + j]);
192
5.52k
    for (j = 0; 
j < tab->bmap->n_div5.52k
;
++j2.57k
)
193
4.72k
      
if (4.72k
isl_seq_eq(tab->bmap->div[j],4.72k
194
4.72k
          vec->el, 2 + dim + tab->bmap->n_div))
195
2.15k
        break;
196
2.95k
    (*div_map)[i] = j;
197
2.95k
    if (
j == tab->bmap->n_div2.95k
)
{795
198
795
      vec->size = 2 + dim + tab->bmap->n_div;
199
795
      if (isl_tab_add_div(tab, vec) < 0)
200
0
        goto error;
201
795
    }
202
2.95k
  }
203
1.92k
204
1.92k
  isl_vec_free(vec);
205
1.92k
206
1.92k
  return isl_stat_ok;
207
0
error:
208
0
  isl_vec_free(vec);
209
0
210
0
  return isl_stat_error;
211
1.92k
}
212
213
/* Freeze all constraints of tableau tab.
214
 */
215
static int tab_freeze_constraints(struct isl_tab *tab)
216
53.2k
{
217
53.2k
  int i;
218
53.2k
219
312k
  for (i = 0; 
i < tab->n_con312k
;
++i259k
)
220
259k
    
if (259k
isl_tab_freeze_constraint(tab, i) < 0259k
)
221
0
      return -1;
222
53.2k
223
53.2k
  return 0;
224
53.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
31.4k
{
233
31.4k
  int i, n;
234
31.4k
  int n_test = tab->n_con - offset;
235
31.4k
236
31.4k
  if (isl_tab_detect_redundant(tab) < 0)
237
0
    return -1;
238
31.4k
239
31.4k
  
if (31.4k
n_test == 031.4k
)
240
801
    return 0;
241
30.6k
  
if (30.6k
!*index30.6k
)
242
30.2k
    
*index = 30.2k
isl_alloc_array30.2k
(ctx, int, n_test);
243
30.6k
  if (!*index)
244
0
    return -1;
245
30.6k
246
128k
  
for (n = 0, i = 0; 30.6k
i < n_test128k
;
++i97.9k
)
{97.9k
247
97.9k
    int r;
248
97.9k
    r = isl_tab_is_redundant(tab, offset + i);
249
97.9k
    if (r < 0)
250
0
      return -1;
251
97.9k
    
if (97.9k
r97.9k
)
252
65.3k
      continue;
253
32.5k
    (*index)[n++] = i;
254
32.5k
  }
255
30.6k
256
30.6k
  return n;
257
30.6k
}
258
259
/* basic_map_collect_diff calls add on each of the pieces of
260
 * the set difference between bmap and map until the add method
261
 * return a negative value.
262
 */
263
struct isl_diff_collector {
264
  isl_stat (*add)(struct isl_diff_collector *dc,
265
        __isl_take isl_basic_map *bmap);
266
};
267
268
/* Compute the set difference between bmap and map and call
269
 * dc->add on each of the piece until this function returns
270
 * a negative value.
271
 * Return 0 on success and -1 on error.  dc->add returning
272
 * a negative value is treated as an error, but the calling
273
 * function can interpret the results based on the state of dc.
274
 *
275
 * Assumes that map has known divs.
276
 *
277
 * The difference is computed by a backtracking algorithm.
278
 * Each level corresponds to a basic map in "map".
279
 * When a node in entered for the first time, we check
280
 * if the corresonding basic map intersects the current piece
281
 * of "bmap".  If not, we move to the next level.
282
 * Otherwise, we split the current piece into as many
283
 * pieces as there are non-redundant constraints of the current
284
 * basic map in the intersection.  Each of these pieces is
285
 * handled by a child of the current node.
286
 * In particular, if there are n non-redundant constraints,
287
 * then for each 0 <= i < n, a piece is cut off by adding
288
 * constraints 0 <= j < i and adding the opposite of constraint i.
289
 * If there are no non-redundant constraints, meaning that the current
290
 * piece is a subset of the current basic map, then we simply backtrack.
291
 *
292
 * In the leaves, we check if the remaining piece has any integer points
293
 * and if so, pass it along to dc->add.  As a special case, if nothing
294
 * has been removed when we end up in a leaf, we simply pass along
295
 * the original basic map.
296
 */
297
static isl_stat basic_map_collect_diff(__isl_take isl_basic_map *bmap,
298
  __isl_take isl_map *map, struct isl_diff_collector *dc)
299
30.6k
{
300
30.6k
  int i;
301
30.6k
  int modified;
302
30.6k
  int level;
303
30.6k
  int init;
304
30.6k
  isl_bool empty;
305
30.6k
  isl_ctx *ctx;
306
30.6k
  struct isl_tab *tab = NULL;
307
30.6k
  struct isl_tab_undo **snap = NULL;
308
30.6k
  int *k = NULL;
309
30.6k
  int *n = NULL;
310
30.6k
  int **index = NULL;
311
30.6k
  int **div_map = NULL;
312
30.6k
313
30.6k
  empty = isl_basic_map_is_empty(bmap);
314
30.6k
  if (
empty30.6k
)
{0
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
30.6k
320
30.6k
  bmap = isl_basic_map_cow(bmap);
321
30.6k
  map = isl_map_cow(map);
322
30.6k
323
30.6k
  if (
!bmap || 30.6k
!map30.6k
)
324
0
    goto error;
325
30.6k
326
30.6k
  ctx = map->ctx;
327
30.6k
  snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
328
30.6k
  k = isl_alloc_array(map->ctx, int, map->n);
329
30.6k
  n = isl_alloc_array(map->ctx, int, map->n);
330
30.6k
  index = isl_calloc_array(map->ctx, int *, map->n);
331
30.6k
  div_map = isl_calloc_array(map->ctx, int *, map->n);
332
30.6k
  if (
!snap || 30.6k
!k30.6k
||
!n30.6k
||
!index30.6k
||
!div_map30.6k
)
333
0
    goto error;
334
30.6k
335
30.6k
  bmap = isl_basic_map_order_divs(bmap);
336
30.6k
  map = isl_map_order_divs(map);
337
30.6k
338
30.6k
  tab = isl_tab_from_basic_map(bmap, 1);
339
30.6k
  if (!tab)
340
0
    goto error;
341
30.6k
342
30.6k
  modified = 0;
343
30.6k
  level = 0;
344
30.6k
  init = 1;
345
30.6k
346
131k
  while (
level >= 0131k
)
{111k
347
111k
    if (
level >= map->n111k
)
{26.3k
348
26.3k
      int empty;
349
26.3k
      struct isl_basic_map *bm;
350
26.3k
      if (
!modified26.3k
)
{5.51k
351
5.51k
        if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
352
1.04k
          goto error;
353
4.47k
        break;
354
5.51k
      }
355
20.8k
      bm = isl_basic_map_copy(tab->bmap);
356
20.8k
      bm = isl_basic_map_cow(bm);
357
20.8k
      bm = isl_basic_map_update_from_tab(bm, tab);
358
20.8k
      bm = isl_basic_map_simplify(bm);
359
20.8k
      bm = isl_basic_map_finalize(bm);
360
20.8k
      empty = isl_basic_map_is_empty(bm);
361
20.8k
      if (empty)
362
569
        isl_basic_map_free(bm);
363
20.2k
      else 
if (20.2k
dc->add(dc, bm) < 020.2k
)
364
5.65k
        goto error;
365
15.2k
      
if (15.2k
empty < 015.2k
)
366
0
        goto error;
367
15.2k
      level--;
368
15.2k
      init = 0;
369
15.2k
      continue;
370
15.2k
    }
371
85.2k
    
if (85.2k
init85.2k
)
{53.2k
372
53.2k
      int offset;
373
53.2k
      struct isl_tab_undo *snap2;
374
53.2k
      snap2 = isl_tab_snap(tab);
375
53.2k
      if (tab_add_divs(tab, map->p[level],
376
53.2k
           &div_map[level]) < 0)
377
0
        goto error;
378
53.2k
      offset = tab->n_con;
379
53.2k
      snap[level] = isl_tab_snap(tab);
380
53.2k
      if (tab_freeze_constraints(tab) < 0)
381
0
        goto error;
382
53.2k
      
if (53.2k
tab_add_constraints(tab, map->p[level],53.2k
383
53.2k
            div_map[level]) < 0)
384
0
        goto error;
385
53.2k
      k[level] = 0;
386
53.2k
      n[level] = 0;
387
53.2k
      if (
tab->empty53.2k
)
{21.7k
388
21.7k
        if (isl_tab_rollback(tab, snap2) < 0)
389
0
          goto error;
390
21.7k
        level++;
391
21.7k
        continue;
392
21.7k
      }
393
31.4k
      modified = 1;
394
31.4k
      n[level] = n_non_redundant(ctx, tab, offset,
395
31.4k
                &index[level]);
396
31.4k
      if (n[level] < 0)
397
0
        goto error;
398
31.4k
      
if (31.4k
n[level] == 031.4k
)
{10.2k
399
10.2k
        level--;
400
10.2k
        init = 0;
401
10.2k
        continue;
402
10.2k
      }
403
21.2k
      
if (21.2k
isl_tab_rollback(tab, snap[level]) < 021.2k
)
404
0
        goto error;
405
21.2k
      
if (21.2k
tab_add_constraint(tab, map->p[level],21.2k
406
21.2k
          div_map[level], index[level][0], 1) < 0)
407
0
        goto error;
408
21.2k
      level++;
409
21.2k
      continue;
410
32.0k
    } else {
411
32.0k
      if (
k[level] + 1 >= n[level]32.0k
)
{26.0k
412
26.0k
        level--;
413
26.0k
        continue;
414
26.0k
      }
415
5.99k
      
if (5.99k
isl_tab_rollback(tab, snap[level]) < 05.99k
)
416
0
        goto error;
417
5.99k
      
if (5.99k
tab_add_constraint(tab, map->p[level],5.99k
418
5.99k
            div_map[level],
419
5.99k
            index[level][k[level]], 0) < 0)
420
0
        goto error;
421
5.99k
      snap[level] = isl_tab_snap(tab);
422
5.99k
      k[level]++;
423
5.99k
      if (tab_add_constraint(tab, map->p[level],
424
5.99k
            div_map[level],
425
5.99k
            index[level][k[level]], 1) < 0)
426
0
        goto error;
427
5.99k
      level++;
428
5.99k
      init = 1;
429
5.99k
      continue;
430
5.99k
    }
431
85.2k
  }
432
30.6k
433
23.9k
  isl_tab_free(tab);
434
23.9k
  free(snap);
435
23.9k
  free(n);
436
23.9k
  free(k);
437
62.2k
  for (i = 0; 
index && 62.2k
i < map->n62.2k
;
++i38.2k
)
438
38.2k
    free(index[i]);
439
23.9k
  free(index);
440
62.2k
  for (i = 0; 
div_map && 62.2k
i < map->n62.2k
;
++i38.2k
)
441
38.2k
    free(div_map[i]);
442
23.9k
  free(div_map);
443
23.9k
444
23.9k
  isl_basic_map_free(bmap);
445
23.9k
  isl_map_free(map);
446
23.9k
447
23.9k
  return isl_stat_ok;
448
6.69k
error:
449
6.69k
  isl_tab_free(tab);
450
6.69k
  free(snap);
451
6.69k
  free(n);
452
6.69k
  free(k);
453
18.8k
  for (i = 0; 
index && 18.8k
i < map->n18.8k
;
++i12.1k
)
454
12.1k
    free(index[i]);
455
6.69k
  free(index);
456
18.8k
  for (i = 0; 
div_map && 18.8k
i < map->n18.8k
;
++i12.1k
)
457
12.1k
    free(div_map[i]);
458
6.69k
  free(div_map);
459
6.69k
  isl_basic_map_free(bmap);
460
6.69k
  isl_map_free(map);
461
6.69k
  return isl_stat_error;
462
30.6k
}
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
19.1k
{
477
19.1k
  struct isl_subtract_diff_collector *sdc;
478
19.1k
  sdc = (struct isl_subtract_diff_collector *)dc;
479
19.1k
480
19.1k
  sdc->diff = isl_map_union_disjoint(sdc->diff,
481
19.1k
      isl_map_from_basic_map(bmap));
482
19.1k
483
19.1k
  return sdc->diff ? 
isl_stat_ok19.1k
:
isl_stat_error0
;
484
19.1k
}
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
16.5k
{
491
16.5k
  struct isl_subtract_diff_collector sdc;
492
16.5k
  sdc.dc.add = &basic_map_subtract_add;
493
16.5k
  sdc.diff = isl_map_empty(isl_basic_map_get_space(bmap));
494
16.5k
  if (
basic_map_collect_diff(bmap, map, &sdc.dc) < 016.5k
)
{0
495
0
    isl_map_free(sdc.diff);
496
0
    sdc.diff = NULL;
497
0
  }
498
16.5k
  return sdc.diff;
499
16.5k
}
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
2.92k
{
506
2.92k
  isl_space *space;
507
2.92k
508
2.92k
  space = isl_map_get_space(map1);
509
2.92k
  isl_map_free(map1);
510
2.92k
  isl_map_free(map2);
511
2.92k
  return isl_map_empty(space);
512
2.92k
}
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
18.6k
{
525
18.6k
  int i;
526
18.6k
  int equal, disjoint;
527
18.6k
  struct isl_map *diff;
528
18.6k
529
18.6k
  if (
!map1 || 18.6k
!map218.6k
)
530
0
    goto error;
531
18.6k
532
18.6k
  
isl_assert18.6k
(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);18.6k
533
18.6k
534
18.6k
  equal = isl_map_plain_is_equal(map1, map2);
535
18.6k
  if (equal < 0)
536
0
    goto error;
537
18.6k
  
if (18.6k
equal18.6k
)
538
2.92k
    return replace_pair_by_empty(map1, map2);
539
18.6k
540
15.7k
  disjoint = isl_map_is_disjoint(map1, map2);
541
15.7k
  if (disjoint < 0)
542
0
    goto error;
543
15.7k
  
if (15.7k
disjoint15.7k
)
{8.06k
544
8.06k
    isl_map_free(map2);
545
8.06k
    return map1;
546
8.06k
  }
547
15.7k
548
7.65k
  map1 = isl_map_compute_divs(map1);
549
7.65k
  map2 = isl_map_compute_divs(map2);
550
7.65k
  if (
!map1 || 7.65k
!map27.65k
)
551
0
    goto error;
552
7.65k
553
7.65k
  map1 = isl_map_remove_empty_parts(map1);
554
7.65k
  map2 = isl_map_remove_empty_parts(map2);
555
7.65k
556
7.65k
  diff = isl_map_empty(isl_map_get_space(map1));
557
24.2k
  for (i = 0; 
i < map1->n24.2k
;
++i16.5k
)
{16.5k
558
16.5k
    struct isl_map *d;
559
16.5k
    d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
560
16.5k
               isl_map_copy(map2));
561
16.5k
    if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
562
6.45k
      diff = isl_map_union_disjoint(diff, d);
563
16.5k
    else
564
10.1k
      diff = isl_map_union(diff, d);
565
16.5k
  }
566
7.65k
567
7.65k
  isl_map_free(map1);
568
7.65k
  isl_map_free(map2);
569
7.65k
570
7.65k
  return diff;
571
0
error:
572
0
  isl_map_free(map1);
573
0
  isl_map_free(map2);
574
0
  return NULL;
575
7.65k
}
576
577
__isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
578
  __isl_take isl_map *map2)
579
18.6k
{
580
18.6k
  return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
581
18.6k
}
582
583
struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
584
12.8k
{
585
12.8k
  return set_from_map(isl_map_subtract(set_to_map(set1),
586
12.8k
              set_to_map(set2)));
587
12.8k
}
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
2
{
594
2
  isl_bool ok;
595
2
  isl_map *ext_dom;
596
2
597
2
  ok = isl_map_compatible_domain(map, dom);
598
2
  if (ok < 0)
599
0
    goto error;
600
2
  
if (2
!ok2
)
601
0
    isl_die(isl_set_get_ctx(dom), isl_error_invalid,
602
2
      "incompatible spaces", goto error);
603
2
  
604
2
  ext_dom = isl_map_universe(isl_map_get_space(map));
605
2
  ext_dom = isl_map_intersect_domain(ext_dom, dom);
606
2
  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
2
}
612
613
__isl_give isl_map *isl_map_subtract_domain(__isl_take isl_map *map,
614
  __isl_take isl_set *dom)
615
2
{
616
2
  return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
617
2
}
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
6.69k
{
662
6.69k
  struct isl_is_empty_diff_collector *edc;
663
6.69k
  edc = (struct isl_is_empty_diff_collector *)dc;
664
6.69k
665
6.69k
  edc->empty = 0;
666
6.69k
667
6.69k
  isl_basic_map_free(bmap);
668
6.69k
  return isl_stat_error;
669
6.69k
}
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
13.4k
{
677
13.4k
  isl_bool empty;
678
13.4k
  isl_stat r;
679
13.4k
  struct isl_is_empty_diff_collector edc;
680
13.4k
681
13.4k
  empty = isl_basic_map_plain_is_empty(bmap);
682
13.4k
  if (empty)
683
1
    return empty;
684
13.4k
685
13.4k
  edc.dc.add = &basic_map_is_empty_add;
686
13.4k
  edc.empty = isl_bool_true;
687
13.4k
  r = basic_map_collect_diff(isl_basic_map_copy(bmap),
688
13.4k
           isl_map_copy(map), &edc.dc);
689
13.4k
  if (!edc.empty)
690
6.69k
    return isl_bool_false;
691
13.4k
692
6.75k
  
return r < 0 ? 6.75k
isl_bool_error0
:
isl_bool_true6.75k
;
693
13.4k
}
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
12.6k
{
701
12.6k
  int i;
702
12.6k
  isl_bool is_empty = isl_bool_true;
703
12.6k
704
12.6k
  if (
!map1 || 12.6k
!map212.6k
)
705
0
    return isl_bool_error;
706
12.6k
  
707
19.3k
  
for (i = 0; 12.6k
i < map1->n19.3k
;
++i6.75k
)
{13.4k
708
13.4k
    is_empty = basic_map_diff_is_empty(map1->p[i], map2);
709
13.4k
    if (
is_empty < 0 || 13.4k
!is_empty13.4k
)
710
6.69k
       break;
711
13.4k
  }
712
12.6k
713
12.6k
  return is_empty;
714
12.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
11.5k
{
720
11.5k
  if (!bmap)
721
0
    return isl_bool_error;
722
11.5k
  
if (11.5k
bmap->n_div11.5k
)
723
649
    return isl_bool_false;
724
10.8k
  
if (10.8k
bmap->n_ineq10.8k
)
725
9.06k
    return isl_bool_false;
726
1.81k
  return bmap->n_eq == isl_basic_map_total_dim(bmap);
727
10.8k
}
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
12.9k
{
733
12.9k
  if (!map)
734
0
    return isl_bool_error;
735
12.9k
  
if (12.9k
map->n != 112.9k
)
736
1.40k
    return isl_bool_false;
737
12.9k
738
11.5k
  return isl_basic_map_plain_is_singleton(map->p[0]);
739
12.9k
}
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
317
{
747
317
  int j;
748
317
  unsigned dim;
749
317
  struct isl_vec *point;
750
317
  isl_int m;
751
317
752
317
  if (!bmap)
753
0
    return NULL;
754
317
755
317
  dim = isl_basic_map_total_dim(bmap);
756
317
  isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
757
317
  point = isl_vec_alloc(bmap->ctx, 1 + dim);
758
317
  if (!point)
759
0
    return NULL;
760
317
761
317
  
isl_int_init317
(m);317
762
317
763
317
  isl_int_set_si(point->el[0], 1);
764
704
  for (j = 0; 
j < bmap->n_eq704
;
++j387
)
{387
765
387
    int i = dim - 1 - j;
766
387
    isl_assert(bmap->ctx,
767
387
        isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
768
387
        goto error);
769
387
    
isl_assert387
(bmap->ctx,387
770
387
        isl_int_is_one(bmap->eq[j][1 + i]) ||
771
387
        isl_int_is_negone(bmap->eq[j][1 + i]),
772
387
        goto error);
773
387
    
isl_assert387
(bmap->ctx,387
774
387
        isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
775
387
        goto error);
776
387
777
387
    
isl_int_gcd387
(m, point->el[0], bmap->eq[j][1 + i]);387
778
387
    isl_int_divexact(m, bmap->eq[j][1 + i], m);
779
387
    isl_int_abs(m, m);
780
387
    isl_seq_scale(point->el, point->el, m, 1 + i);
781
387
    isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
782
387
    isl_int_neg(m, m);
783
387
    isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
784
387
  }
785
317
786
317
  
isl_int_clear317
(m);317
787
317
  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
317
}
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
317
{
801
317
  int i;
802
317
  isl_bool is_subset = isl_bool_false;
803
317
  struct isl_point *point;
804
317
805
317
  if (
!map1 || 317
!map2317
)
806
0
    return isl_bool_error;
807
317
  
if (317
map1->n != 1317
)
808
0
    isl_die(isl_map_get_ctx(map1), isl_error_invalid,
809
317
      "expecting single-disjunct input",
810
317
      return isl_bool_error);
811
317
812
317
  point = singleton_extract_point(map1->p[0]);
813
317
  if (!point)
814
0
    return isl_bool_error;
815
317
816
378
  
for (i = 0; 317
i < map2->n378
;
++i61
)
{338
817
338
    is_subset = isl_basic_map_contains_point(map2->p[i], point);
818
338
    if (is_subset)
819
277
      break;
820
338
  }
821
317
822
317
  isl_point_free(point);
823
317
  return is_subset;
824
317
}
825
826
static isl_bool map_is_subset(__isl_keep isl_map *map1,
827
  __isl_keep isl_map *map2)
828
30.5k
{
829
30.5k
  isl_bool is_subset = isl_bool_false;
830
30.5k
  isl_bool empty, single;
831
30.5k
  isl_bool rat1, rat2;
832
30.5k
833
30.5k
  if (
!map1 || 30.5k
!map230.5k
)
834
0
    return isl_bool_error;
835
30.5k
836
30.5k
  
if (30.5k
!isl_map_has_equal_space(map1, map2)30.5k
)
837
98
    return isl_bool_false;
838
30.5k
839
30.4k
  empty = isl_map_is_empty(map1);
840
30.4k
  if (empty < 0)
841
0
    return isl_bool_error;
842
30.4k
  
if (30.4k
empty30.4k
)
843
125
    return isl_bool_true;
844
30.4k
845
30.3k
  empty = isl_map_is_empty(map2);
846
30.3k
  if (empty < 0)
847
0
    return isl_bool_error;
848
30.3k
  
if (30.3k
empty30.3k
)
849
12.4k
    return isl_bool_false;
850
30.3k
851
17.9k
  rat1 = isl_map_has_rational(map1);
852
17.9k
  rat2 = isl_map_has_rational(map2);
853
17.9k
  if (
rat1 < 0 || 17.9k
rat2 < 017.9k
)
854
0
    return isl_bool_error;
855
17.9k
  
if (17.9k
rat1 && 17.9k
!rat214
)
856
2
    return isl_bool_false;
857
17.9k
858
17.9k
  
if (17.9k
isl_map_plain_is_universe(map2)17.9k
)
859
5.00k
    return isl_bool_true;
860
17.9k
861
12.9k
  single = isl_map_plain_is_singleton(map1);
862
12.9k
  if (single < 0)
863
0
    return isl_bool_error;
864
12.9k
  map2 = isl_map_compute_divs(isl_map_copy(map2));
865
12.9k
  if (
single12.9k
)
{317
866
317
    is_subset = map_is_singleton_subset(map1, map2);
867
317
    isl_map_free(map2);
868
317
    return is_subset;
869
317
  }
870
12.6k
  is_subset = map_diff_is_empty(map1, map2);
871
12.6k
  isl_map_free(map2);
872
12.6k
873
12.6k
  return is_subset;
874
12.9k
}
875
876
isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
877
30.5k
{
878
30.5k
  return isl_map_align_params_map_map_and_test(map1, map2,
879
30.5k
              &map_is_subset);
880
30.5k
}
881
882
isl_bool isl_set_is_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
883
17.6k
{
884
17.6k
  return isl_map_is_subset(set_to_map(set1), set_to_map(set2));
885
17.6k
}
886
887
__isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
888
2.93k
{
889
2.93k
  int i;
890
2.93k
  struct isl_subtract_diff_collector sdc;
891
2.93k
  sdc.dc.add = &basic_map_subtract_add;
892
2.93k
893
2.93k
  if (!map)
894
0
    return NULL;
895
2.93k
  
if (2.93k
ISL_F_ISSET2.93k
(map, ISL_MAP_DISJOINT))
896
1.32k
    return map;
897
1.60k
  
if (1.60k
map->n <= 11.60k
)
898
1.42k
    return map;
899
1.60k
900
185
  map = isl_map_compute_divs(map);
901
185
  map = isl_map_remove_empty_parts(map);
902
185
903
185
  if (
!map || 185
map->n <= 1185
)
904
0
    return map;
905
185
906
185
  sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
907
185
908
766
  for (i = 1; 
i < map->n766
;
++i581
)
{581
909
581
    struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
910
581
    struct isl_map *copy = isl_map_copy(sdc.diff);
911
581
    if (
basic_map_collect_diff(bmap, copy, &sdc.dc) < 0581
)
{0
912
0
      isl_map_free(sdc.diff);
913
0
      sdc.diff = NULL;
914
0
      break;
915
0
    }
916
581
  }
917
185
918
185
  isl_map_free(map);
919
185
920
185
  return sdc.diff;
921
185
}
922
923
__isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
924
2.92k
{
925
2.92k
  return set_from_map(isl_map_make_disjoint(set_to_map(set)));
926
2.92k
}
927
928
__isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
929
4.82k
{
930
4.82k
  isl_map *universe;
931
4.82k
932
4.82k
  if (!map)
933
0
    return NULL;
934
4.82k
935
4.82k
  universe = isl_map_universe(isl_map_get_space(map));
936
4.82k
937
4.82k
  return isl_map_subtract(universe, map);
938
4.82k
}
939
940
__isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
941
4.82k
{
942
4.82k
  return isl_map_complement(set);
943
4.82k
}