Coverage Report

Created: 2017-04-29 12:21

/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
192k
{
35
192k
  int i;
36
192k
37
192k
  isl_seq_cpy(v->el, c, 1 + dim);
38
192k
  isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim));
39
192k
40
220k
  for (i = 0; 
i < n_div220k
;
++i27.8k
)
{27.8k
41
27.8k
    int pos = 1 + dim + div_map[i];
42
27.8k
    isl_int_add(v->el[pos], v->el[pos], c[1 + dim + i]);
43
27.8k
  }
44
192k
}
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
55.6k
{
52
55.6k
  int i;
53
55.6k
  unsigned dim;
54
55.6k
  unsigned tab_total;
55
55.6k
  unsigned bmap_total;
56
55.6k
  isl_vec *v;
57
55.6k
58
55.6k
  if (
!tab || 55.6k
!bmap55.6k
)
59
0
    return isl_stat_error;
60
55.6k
61
55.6k
  tab_total = isl_basic_map_total_dim(tab->bmap);
62
55.6k
  bmap_total = isl_basic_map_total_dim(bmap);
63
55.6k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
64
55.6k
65
55.6k
  if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
66
0
    return isl_stat_error;
67
55.6k
68
55.6k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
69
55.6k
  if (!v)
70
0
    return isl_stat_error;
71
55.6k
72
73.2k
  
for (i = 0; 55.6k
i < bmap->n_eq73.2k
;
++i17.5k
)
{19.2k
73
19.2k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
74
19.2k
    if (isl_tab_add_ineq(tab, v->el) < 0)
75
0
      goto error;
76
19.2k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
77
19.2k
    expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
78
19.2k
    if (isl_tab_add_ineq(tab, v->el) < 0)
79
0
      goto error;
80
19.2k
    isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
81
19.2k
    if (tab->empty)
82
1.64k
      break;
83
19.2k
  }
84
55.6k
85
152k
  
for (i = 0; 55.6k
i < bmap->n_ineq152k
;
++i97.1k
)
{119k
86
119k
    expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
87
119k
    if (isl_tab_add_ineq(tab, v->el) < 0)
88
0
      goto error;
89
119k
    
if (119k
tab->empty119k
)
90
22.6k
      break;
91
119k
  }
92
55.6k
93
55.6k
  isl_vec_free(v);
94
55.6k
  return isl_stat_ok;
95
0
error:
96
0
  isl_vec_free(v);
97
0
  return isl_stat_error;
98
55.6k
}
99
100
/* Add a specific constraint of bmap (or its opposite) to tab.
101
 * The position of the constraint is specified by "c", where
102
 * the equalities of bmap are counted twice, once for the inequality
103
 * that is equal to the equality, and once for its negation.
104
 *
105
 * Each of these constraints has been added to "tab" before by
106
 * tab_add_constraints (and later removed again), so there should
107
 * already be a row available for the constraint.
108
 */
109
static isl_stat tab_add_constraint(struct isl_tab *tab,
110
  __isl_keep isl_basic_map *bmap, int *div_map, int c, int oppose)
111
34.3k
{
112
34.3k
  unsigned dim;
113
34.3k
  unsigned tab_total;
114
34.3k
  unsigned bmap_total;
115
34.3k
  isl_vec *v;
116
34.3k
  isl_stat r;
117
34.3k
118
34.3k
  if (
!tab || 34.3k
!bmap34.3k
)
119
0
    return isl_stat_error;
120
34.3k
121
34.3k
  tab_total = isl_basic_map_total_dim(tab->bmap);
122
34.3k
  bmap_total = isl_basic_map_total_dim(bmap);
123
34.3k
  dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
124
34.3k
125
34.3k
  v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
126
34.3k
  if (!v)
127
0
    return isl_stat_error;
128
34.3k
129
34.3k
  
if (34.3k
c < 2 * bmap->n_eq34.3k
)
{7.20k
130
7.20k
    if ((c % 2) != oppose)
131
3.73k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
132
3.73k
          1 + bmap_total);
133
7.20k
    if (oppose)
134
5.41k
      isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
135
7.20k
    expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
136
7.20k
    r = isl_tab_add_ineq(tab, v->el);
137
7.20k
    if (oppose)
138
5.41k
      isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
139
7.20k
    if ((c % 2) != oppose)
140
3.73k
      isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
141
3.73k
          1 + bmap_total);
142
27.1k
  } else {
143
27.1k
    c -= 2 * bmap->n_eq;
144
27.1k
    if (
oppose27.1k
)
{22.6k
145
22.6k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
146
22.6k
          1 + bmap_total);
147
22.6k
      isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
148
22.6k
    }
149
27.1k
    expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
150
27.1k
    r = isl_tab_add_ineq(tab, v->el);
151
27.1k
    if (
oppose27.1k
)
{22.6k
152
22.6k
      isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
153
22.6k
      isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
154
22.6k
          1 + bmap_total);
155
22.6k
    }
156
27.1k
  }
157
34.3k
158
34.3k
  isl_vec_free(v);
159
34.3k
  return r;
160
34.3k
}
161
162
static isl_stat tab_add_divs(struct isl_tab *tab,
163
  __isl_keep isl_basic_map *bmap, int **div_map)
164
55.6k
{
165
55.6k
  int i, j;
166
55.6k
  struct isl_vec *vec;
167
55.6k
  unsigned total;
168
55.6k
  unsigned dim;
169
55.6k
170
55.6k
  if (!bmap)
171
0
    return isl_stat_error;
172
55.6k
  
if (55.6k
!bmap->n_div55.6k
)
173
53.4k
    return isl_stat_ok;
174
55.6k
175
2.26k
  
if (2.26k
!*div_map2.26k
)
176
1.79k
    
*div_map = 1.79k
isl_alloc_array1.79k
(bmap->ctx, int, bmap->n_div);
177
2.26k
  if (!*div_map)
178
0
    return isl_stat_error;
179
2.26k
180
2.26k
  total = isl_basic_map_total_dim(tab->bmap);
181
2.26k
  dim = total - tab->bmap->n_div;
182
2.26k
  vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
183
2.26k
  if (!vec)
184
0
    return isl_stat_error;
185
2.26k
186
5.54k
  
for (i = 0; 2.26k
i < bmap->n_div5.54k
;
++i3.28k
)
{3.28k
187
3.28k
    isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
188
3.28k
    isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
189
4.96k
    for (j = 0; 
j < i4.96k
;
++j1.68k
)
190
1.68k
      isl_int_set(vec->el[2 + dim + (*div_map)[j]],
191
3.28k
          bmap->div[i][2 + dim + j]);
192
5.80k
    for (j = 0; 
j < tab->bmap->n_div5.80k
;
++j2.52k
)
193
4.96k
      
if (4.96k
isl_seq_eq(tab->bmap->div[j],4.96k
194
4.96k
          vec->el, 2 + dim + tab->bmap->n_div))
195
2.43k
        break;
196
3.28k
    (*div_map)[i] = j;
197
3.28k
    if (
j == tab->bmap->n_div3.28k
)
{843
198
843
      vec->size = 2 + dim + tab->bmap->n_div;
199
843
      if (isl_tab_add_div(tab, vec) < 0)
200
0
        goto error;
201
843
    }
202
3.28k
  }
203
2.26k
204
2.26k
  isl_vec_free(vec);
205
2.26k
206
2.26k
  return isl_stat_ok;
207
0
error:
208
0
  isl_vec_free(vec);
209
0
210
0
  return isl_stat_error;
211
2.26k
}
212
213
/* Freeze all constraints of tableau tab.
214
 */
215
static int tab_freeze_constraints(struct isl_tab *tab)
216
55.6k
{
217
55.6k
  int i;
218
55.6k
219
328k
  for (i = 0; 
i < tab->n_con328k
;
++i272k
)
220
272k
    
if (272k
isl_tab_freeze_constraint(tab, i) < 0272k
)
221
0
      return -1;
222
55.6k
223
55.6k
  return 0;
224
55.6k
}
225
226
/* Check for redundant constraints starting at offset.
227
 * Put the indices of the redundant constraints in index
228
 * and return the number of redundant constraints.
229
 */
230
static int n_non_redundant(isl_ctx *ctx, struct isl_tab *tab,
231
  int offset, int **index)
232
32.7k
{
233
32.7k
  int i, n;
234
32.7k
  int n_test = tab->n_con - offset;
235
32.7k
236
32.7k
  if (isl_tab_detect_redundant(tab) < 0)
237
0
    return -1;
238
32.7k
239
32.7k
  
if (32.7k
n_test == 032.7k
)
240
1.10k
    return 0;
241
31.6k
  
if (31.6k
!*index31.6k
)
242
31.2k
    
*index = 31.2k
isl_alloc_array31.2k
(ctx, int, n_test);
243
31.6k
  if (!*index)
244
0
    return -1;
245
31.6k
246
135k
  
for (n = 0, i = 0; 31.6k
i < n_test135k
;
++i104k
)
{104k
247
104k
    int r;
248
104k
    r = isl_tab_is_redundant(tab, offset + i);
249
104k
    if (r < 0)
250
0
      return -1;
251
104k
    
if (104k
r104k
)
252
70.2k
      continue;
253
34.0k
    (*index)[n++] = i;
254
34.0k
  }
255
31.6k
256
31.6k
  return n;
257
31.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
32.0k
{
300
32.0k
  int i;
301
32.0k
  int modified;
302
32.0k
  int level;
303
32.0k
  int init;
304
32.0k
  isl_bool empty;
305
32.0k
  isl_ctx *ctx;
306
32.0k
  struct isl_tab *tab = NULL;
307
32.0k
  struct isl_tab_undo **snap = NULL;
308
32.0k
  int *k = NULL;
309
32.0k
  int *n = NULL;
310
32.0k
  int **index = NULL;
311
32.0k
  int **div_map = NULL;
312
32.0k
313
32.0k
  empty = isl_basic_map_is_empty(bmap);
314
32.0k
  if (
empty32.0k
)
{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
32.0k
320
32.0k
  bmap = isl_basic_map_cow(bmap);
321
32.0k
  map = isl_map_cow(map);
322
32.0k
323
32.0k
  if (
!bmap || 32.0k
!map32.0k
)
324
0
    goto error;
325
32.0k
326
32.0k
  ctx = map->ctx;
327
32.0k
  snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
328
32.0k
  k = isl_alloc_array(map->ctx, int, map->n);
329
32.0k
  n = isl_alloc_array(map->ctx, int, map->n);
330
32.0k
  index = isl_calloc_array(map->ctx, int *, map->n);
331
32.0k
  div_map = isl_calloc_array(map->ctx, int *, map->n);
332
32.0k
  if (
!snap || 32.0k
!k32.0k
||
!n32.0k
||
!index32.0k
||
!div_map32.0k
)
333
0
    goto error;
334
32.0k
335
32.0k
  bmap = isl_basic_map_order_divs(bmap);
336
32.0k
  map = isl_map_order_divs(map);
337
32.0k
338
32.0k
  tab = isl_tab_from_basic_map(bmap, 1);
339
32.0k
  if (!tab)
340
0
    goto error;
341
32.0k
342
32.0k
  modified = 0;
343
32.0k
  level = 0;
344
32.0k
  init = 1;
345
32.0k
346
137k
  while (
level >= 0137k
)
{116k
347
116k
    if (
level >= map->n116k
)
{27.2k
348
27.2k
      int empty;
349
27.2k
      struct isl_basic_map *bm;
350
27.2k
      if (
!modified27.2k
)
{5.77k
351
5.77k
        if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
352
1.04k
          goto error;
353
4.73k
        break;
354
5.77k
      }
355
21.5k
      bm = isl_basic_map_copy(tab->bmap);
356
21.5k
      bm = isl_basic_map_cow(bm);
357
21.5k
      bm = isl_basic_map_update_from_tab(bm, tab);
358
21.5k
      bm = isl_basic_map_simplify(bm);
359
21.5k
      bm = isl_basic_map_finalize(bm);
360
21.5k
      empty = isl_basic_map_is_empty(bm);
361
21.5k
      if (empty)
362
570
        isl_basic_map_free(bm);
363
20.9k
      else 
if (20.9k
dc->add(dc, bm) < 020.9k
)
364
5.68k
        goto error;
365
15.8k
      
if (15.8k
empty < 015.8k
)
366
0
        goto error;
367
15.8k
      level--;
368
15.8k
      init = 0;
369
15.8k
      continue;
370
15.8k
    }
371
89.2k
    
if (89.2k
init89.2k
)
{55.6k
372
55.6k
      int offset;
373
55.6k
      struct isl_tab_undo *snap2;
374
55.6k
      snap2 = isl_tab_snap(tab);
375
55.6k
      if (tab_add_divs(tab, map->p[level],
376
55.6k
           &div_map[level]) < 0)
377
0
        goto error;
378
55.6k
      offset = tab->n_con;
379
55.6k
      snap[level] = isl_tab_snap(tab);
380
55.6k
      if (tab_freeze_constraints(tab) < 0)
381
0
        goto error;
382
55.6k
      
if (55.6k
tab_add_constraints(tab, map->p[level],55.6k
383
55.6k
            div_map[level]) < 0)
384
0
        goto error;
385
55.6k
      k[level] = 0;
386
55.6k
      n[level] = 0;
387
55.6k
      if (
tab->empty55.6k
)
{22.9k
388
22.9k
        if (isl_tab_rollback(tab, snap2) < 0)
389
0
          goto error;
390
22.9k
        level++;
391
22.9k
        continue;
392
22.9k
      }
393
32.7k
      modified = 1;
394
32.7k
      n[level] = n_non_redundant(ctx, tab, offset,
395
32.7k
                &index[level]);
396
32.7k
      if (n[level] < 0)
397
0
        goto error;
398
32.7k
      
if (32.7k
n[level] == 032.7k
)
{10.9k
399
10.9k
        level--;
400
10.9k
        init = 0;
401
10.9k
        continue;
402
10.9k
      }
403
21.7k
      
if (21.7k
isl_tab_rollback(tab, snap[level]) < 021.7k
)
404
0
        goto error;
405
21.7k
      
if (21.7k
tab_add_constraint(tab, map->p[level],21.7k
406
21.7k
          div_map[level], index[level][0], 1) < 0)
407
0
        goto error;
408
21.7k
      level++;
409
21.7k
      continue;
410
33.5k
    } else {
411
33.5k
      if (
k[level] + 1 >= n[level]33.5k
)
{27.2k
412
27.2k
        level--;
413
27.2k
        continue;
414
27.2k
      }
415
6.27k
      
if (6.27k
isl_tab_rollback(tab, snap[level]) < 06.27k
)
416
0
        goto error;
417
6.27k
      
if (6.27k
tab_add_constraint(tab, map->p[level],6.27k
418
6.27k
            div_map[level],
419
6.27k
            index[level][k[level]], 0) < 0)
420
0
        goto error;
421
6.27k
      snap[level] = isl_tab_snap(tab);
422
6.27k
      k[level]++;
423
6.27k
      if (tab_add_constraint(tab, map->p[level],
424
6.27k
            div_map[level],
425
6.27k
            index[level][k[level]], 1) < 0)
426
0
        goto error;
427
6.27k
      level++;
428
6.27k
      init = 1;
429
6.27k
      continue;
430
6.27k
    }
431
89.2k
  }
432
32.0k
433
25.2k
  isl_tab_free(tab);
434
25.2k
  free(snap);
435
25.2k
  free(n);
436
25.2k
  free(k);
437
65.9k
  for (i = 0; 
index && 65.9k
i < map->n65.9k
;
++i40.6k
)
438
40.6k
    free(index[i]);
439
25.2k
  free(index);
440
65.9k
  for (i = 0; 
div_map && 65.9k
i < map->n65.9k
;
++i40.6k
)
441
40.6k
    free(div_map[i]);
442
25.2k
  free(div_map);
443
25.2k
444
25.2k
  isl_basic_map_free(bmap);
445
25.2k
  isl_map_free(map);
446
25.2k
447
25.2k
  return isl_stat_ok;
448
6.73k
error:
449
6.73k
  isl_tab_free(tab);
450
6.73k
  free(snap);
451
6.73k
  free(n);
452
6.73k
  free(k);
453
18.9k
  for (i = 0; 
index && 18.9k
i < map->n18.9k
;
++i12.2k
)
454
12.2k
    free(index[i]);
455
6.73k
  free(index);
456
18.9k
  for (i = 0; 
div_map && 18.9k
i < map->n18.9k
;
++i12.2k
)
457
12.2k
    free(div_map[i]);
458
6.73k
  free(div_map);
459
6.73k
  isl_basic_map_free(bmap);
460
6.73k
  isl_map_free(map);
461
6.73k
  return isl_stat_error;
462
32.0k
}
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.9k
{
477
19.9k
  struct isl_subtract_diff_collector *sdc;
478
19.9k
  sdc = (struct isl_subtract_diff_collector *)dc;
479
19.9k
480
19.9k
  sdc->diff = isl_map_union_disjoint(sdc->diff,
481
19.9k
      isl_map_from_basic_map(bmap));
482
19.9k
483
19.9k
  return sdc->diff ? 
isl_stat_ok19.9k
:
isl_stat_error0
;
484
19.9k
}
485
486
/* Return the set difference between bmap and map.
487
 */
488
static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
489
  __isl_take isl_map *map)
490
17.3k
{
491
17.3k
  struct isl_subtract_diff_collector sdc;
492
17.3k
  sdc.dc.add = &basic_map_subtract_add;
493
17.3k
  sdc.diff = isl_map_empty(isl_basic_map_get_space(bmap));
494
17.3k
  if (
basic_map_collect_diff(bmap, map, &sdc.dc) < 017.3k
)
{0
495
0
    isl_map_free(sdc.diff);
496
0
    sdc.diff = NULL;
497
0
  }
498
17.3k
  return sdc.diff;
499
17.3k
}
500
501
/* Return an empty map living in the same space as "map1" and "map2".
502
 */
503
static __isl_give isl_map *replace_pair_by_empty( __isl_take isl_map *map1,
504
  __isl_take isl_map *map2)
505
3.13k
{
506
3.13k
  isl_space *space;
507
3.13k
508
3.13k
  space = isl_map_get_space(map1);
509
3.13k
  isl_map_free(map1);
510
3.13k
  isl_map_free(map2);
511
3.13k
  return isl_map_empty(space);
512
3.13k
}
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
19.7k
{
525
19.7k
  int i;
526
19.7k
  int equal, disjoint;
527
19.7k
  struct isl_map *diff;
528
19.7k
529
19.7k
  if (
!map1 || 19.7k
!map219.7k
)
530
0
    goto error;
531
19.7k
532
19.7k
  
isl_assert19.7k
(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);19.7k
533
19.7k
534
19.7k
  equal = isl_map_plain_is_equal(map1, map2);
535
19.7k
  if (equal < 0)
536
0
    goto error;
537
19.7k
  
if (19.7k
equal19.7k
)
538
3.13k
    return replace_pair_by_empty(map1, map2);
539
19.7k
540
16.6k
  disjoint = isl_map_is_disjoint(map1, map2);
541
16.6k
  if (disjoint < 0)
542
0
    goto error;
543
16.6k
  
if (16.6k
disjoint16.6k
)
{8.55k
544
8.55k
    isl_map_free(map2);
545
8.55k
    return map1;
546
8.55k
  }
547
16.6k
548
8.09k
  map1 = isl_map_compute_divs(map1);
549
8.09k
  map2 = isl_map_compute_divs(map2);
550
8.09k
  if (
!map1 || 8.09k
!map28.09k
)
551
0
    goto error;
552
8.09k
553
8.09k
  map1 = isl_map_remove_empty_parts(map1);
554
8.09k
  map2 = isl_map_remove_empty_parts(map2);
555
8.09k
556
8.09k
  diff = isl_map_empty(isl_map_get_space(map1));
557
25.4k
  for (i = 0; 
i < map1->n25.4k
;
++i17.3k
)
{17.3k
558
17.3k
    struct isl_map *d;
559
17.3k
    d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
560
17.3k
               isl_map_copy(map2));
561
17.3k
    if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
562
6.72k
      diff = isl_map_union_disjoint(diff, d);
563
17.3k
    else
564
10.6k
      diff = isl_map_union(diff, d);
565
17.3k
  }
566
8.09k
567
8.09k
  isl_map_free(map1);
568
8.09k
  isl_map_free(map2);
569
8.09k
570
8.09k
  return diff;
571
0
error:
572
0
  isl_map_free(map1);
573
0
  isl_map_free(map2);
574
0
  return NULL;
575
8.09k
}
576
577
__isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
578
  __isl_take isl_map *map2)
579
19.7k
{
580
19.7k
  return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
581
19.7k
}
582
583
struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
584
13.5k
{
585
13.5k
  return set_from_map(isl_map_subtract(set_to_map(set1),
586
13.5k
              set_to_map(set2)));
587
13.5k
}
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.73k
{
662
6.73k
  struct isl_is_empty_diff_collector *edc;
663
6.73k
  edc = (struct isl_is_empty_diff_collector *)dc;
664
6.73k
665
6.73k
  edc->empty = 0;
666
6.73k
667
6.73k
  isl_basic_map_free(bmap);
668
6.73k
  return isl_stat_error;
669
6.73k
}
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.5k
{
677
13.5k
  isl_bool empty;
678
13.5k
  isl_stat r;
679
13.5k
  struct isl_is_empty_diff_collector edc;
680
13.5k
681
13.5k
  empty = isl_basic_map_plain_is_empty(bmap);
682
13.5k
  if (empty)
683
1
    return empty;
684
13.5k
685
13.5k
  edc.dc.add = &basic_map_is_empty_add;
686
13.5k
  edc.empty = isl_bool_true;
687
13.5k
  r = basic_map_collect_diff(isl_basic_map_copy(bmap),
688
13.5k
           isl_map_copy(map), &edc.dc);
689
13.5k
  if (!edc.empty)
690
6.73k
    return isl_bool_false;
691
13.5k
692
6.86k
  
return r < 0 ? 6.86k
isl_bool_error0
:
isl_bool_true6.86k
;
693
13.5k
}
694
695
/* Check if map1 \ map2 is empty by checking if the set difference is empty
696
 * for each of the basic maps in map1.
697
 */
698
static isl_bool map_diff_is_empty(__isl_keep isl_map *map1,
699
  __isl_keep isl_map *map2)
700
12.7k
{
701
12.7k
  int i;
702
12.7k
  isl_bool is_empty = isl_bool_true;
703
12.7k
704
12.7k
  if (
!map1 || 12.7k
!map212.7k
)
705
0
    return isl_bool_error;
706
12.7k
  
707
19.6k
  
for (i = 0; 12.7k
i < map1->n19.6k
;
++i6.86k
)
{13.5k
708
13.5k
    is_empty = basic_map_diff_is_empty(map1->p[i], map2);
709
13.5k
    if (
is_empty < 0 || 13.5k
!is_empty13.5k
)
710
6.73k
       break;
711
13.5k
  }
712
12.7k
713
12.7k
  return is_empty;
714
12.7k
}
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.7k
{
720
11.7k
  if (!bmap)
721
0
    return isl_bool_error;
722
11.7k
  
if (11.7k
bmap->n_div11.7k
)
723
649
    return isl_bool_false;
724
11.0k
  
if (11.0k
bmap->n_ineq11.0k
)
725
9.20k
    return isl_bool_false;
726
1.87k
  return bmap->n_eq == isl_basic_map_total_dim(bmap);
727
11.0k
}
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
13.1k
{
733
13.1k
  if (!map)
734
0
    return isl_bool_error;
735
13.1k
  
if (13.1k
map->n != 113.1k
)
736
1.40k
    return isl_bool_false;
737
13.1k
738
11.7k
  return isl_basic_map_plain_is_singleton(map->p[0]);
739
13.1k
}
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
365
{
747
365
  int j;
748
365
  unsigned dim;
749
365
  struct isl_vec *point;
750
365
  isl_int m;
751
365
752
365
  if (!bmap)
753
0
    return NULL;
754
365
755
365
  dim = isl_basic_map_total_dim(bmap);
756
365
  isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
757
365
  point = isl_vec_alloc(bmap->ctx, 1 + dim);
758
365
  if (!point)
759
0
    return NULL;
760
365
761
365
  
isl_int_init365
(m);365
762
365
763
365
  isl_int_set_si(point->el[0], 1);
764
800
  for (j = 0; 
j < bmap->n_eq800
;
++j435
)
{435
765
435
    int i = dim - 1 - j;
766
435
    isl_assert(bmap->ctx,
767
435
        isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
768
435
        goto error);
769
435
    
isl_assert435
(bmap->ctx,435
770
435
        isl_int_is_one(bmap->eq[j][1 + i]) ||
771
435
        isl_int_is_negone(bmap->eq[j][1 + i]),
772
435
        goto error);
773
435
    
isl_assert435
(bmap->ctx,435
774
435
        isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
775
435
        goto error);
776
435
777
435
    
isl_int_gcd435
(m, point->el[0], bmap->eq[j][1 + i]);435
778
435
    isl_int_divexact(m, bmap->eq[j][1 + i], m);
779
435
    isl_int_abs(m, m);
780
435
    isl_seq_scale(point->el, point->el, m, 1 + i);
781
435
    isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
782
435
    isl_int_neg(m, m);
783
435
    isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
784
435
  }
785
365
786
365
  
isl_int_clear365
(m);365
787
365
  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
365
}
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
365
{
801
365
  int i;
802
365
  isl_bool is_subset = isl_bool_false;
803
365
  struct isl_point *point;
804
365
805
365
  if (
!map1 || 365
!map2365
)
806
0
    return isl_bool_error;
807
365
  
if (365
map1->n != 1365
)
808
0
    isl_die(isl_map_get_ctx(map1), isl_error_invalid,
809
365
      "expecting single-disjunct input",
810
365
      return isl_bool_error);
811
365
812
365
  point = singleton_extract_point(map1->p[0]);
813
365
  if (!point)
814
0
    return isl_bool_error;
815
365
816
426
  
for (i = 0; 365
i < map2->n426
;
++i61
)
{386
817
386
    is_subset = isl_basic_map_contains_point(map2->p[i], point);
818
386
    if (is_subset)
819
325
      break;
820
386
  }
821
365
822
365
  isl_point_free(point);
823
365
  return is_subset;
824
365
}
825
826
static isl_bool map_is_subset(__isl_keep isl_map *map1,
827
  __isl_keep isl_map *map2)
828
30.9k
{
829
30.9k
  isl_bool is_subset = isl_bool_false;
830
30.9k
  isl_bool empty, single;
831
30.9k
  isl_bool rat1, rat2;
832
30.9k
833
30.9k
  if (
!map1 || 30.9k
!map230.9k
)
834
0
    return isl_bool_error;
835
30.9k
836
30.9k
  
if (30.9k
!isl_map_has_equal_space(map1, map2)30.9k
)
837
98
    return isl_bool_false;
838
30.9k
839
30.8k
  empty = isl_map_is_empty(map1);
840
30.8k
  if (empty < 0)
841
0
    return isl_bool_error;
842
30.8k
  
if (30.8k
empty30.8k
)
843
125
    return isl_bool_true;
844
30.8k
845
30.7k
  empty = isl_map_is_empty(map2);
846
30.7k
  if (empty < 0)
847
0
    return isl_bool_error;
848
30.7k
  
if (30.7k
empty30.7k
)
849
12.5k
    return isl_bool_false;
850
30.7k
851
18.2k
  rat1 = isl_map_has_rational(map1);
852
18.2k
  rat2 = isl_map_has_rational(map2);
853
18.2k
  if (
rat1 < 0 || 18.2k
rat2 < 018.2k
)
854
0
    return isl_bool_error;
855
18.2k
  
if (18.2k
rat1 && 18.2k
!rat214
)
856
2
    return isl_bool_false;
857
18.2k
858
18.2k
  
if (18.2k
isl_map_plain_is_universe(map2)18.2k
)
859
5.12k
    return isl_bool_true;
860
18.2k
861
13.1k
  single = isl_map_plain_is_singleton(map1);
862
13.1k
  if (single < 0)
863
0
    return isl_bool_error;
864
13.1k
  map2 = isl_map_compute_divs(isl_map_copy(map2));
865
13.1k
  if (
single13.1k
)
{365
866
365
    is_subset = map_is_singleton_subset(map1, map2);
867
365
    isl_map_free(map2);
868
365
    return is_subset;
869
365
  }
870
12.7k
  is_subset = map_diff_is_empty(map1, map2);
871
12.7k
  isl_map_free(map2);
872
12.7k
873
12.7k
  return is_subset;
874
13.1k
}
875
876
isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
877
30.9k
{
878
30.9k
  return isl_map_align_params_map_map_and_test(map1, map2,
879
30.9k
              &map_is_subset);
880
30.9k
}
881
882
isl_bool isl_set_is_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
883
17.8k
{
884
17.8k
  return isl_map_is_subset(set_to_map(set1), set_to_map(set2));
885
17.8k
}
886
887
__isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
888
4.95k
{
889
4.95k
  int i;
890
4.95k
  struct isl_subtract_diff_collector sdc;
891
4.95k
  sdc.dc.add = &basic_map_subtract_add;
892
4.95k
893
4.95k
  if (!map)
894
0
    return NULL;
895
4.95k
  
if (4.95k
ISL_F_ISSET4.95k
(map, ISL_MAP_DISJOINT))
896
2.55k
    return map;
897
2.40k
  
if (2.40k
map->n <= 12.40k
)
898
2.06k
    return map;
899
2.40k
900
335
  map = isl_map_compute_divs(map);
901
335
  map = isl_map_remove_empty_parts(map);
902
335
903
335
  if (
!map || 335
map->n <= 1335
)
904
0
    return map;
905
335
906
335
  sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
907
335
908
1.36k
  for (i = 1; 
i < map->n1.36k
;
++i1.02k
)
{1.02k
909
1.02k
    struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
910
1.02k
    struct isl_map *copy = isl_map_copy(sdc.diff);
911
1.02k
    if (
basic_map_collect_diff(bmap, copy, &sdc.dc) < 01.02k
)
{0
912
0
      isl_map_free(sdc.diff);
913
0
      sdc.diff = NULL;
914
0
      break;
915
0
    }
916
1.02k
  }
917
335
918
335
  isl_map_free(map);
919
335
920
335
  return sdc.diff;
921
335
}
922
923
__isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
924
4.94k
{
925
4.94k
  return set_from_map(isl_map_make_disjoint(set_to_map(set)));
926
4.94k
}
927
928
__isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
929
4.89k
{
930
4.89k
  isl_map *universe;
931
4.89k
932
4.89k
  if (!map)
933
0
    return NULL;
934
4.89k
935
4.89k
  universe = isl_map_universe(isl_map_get_space(map));
936
4.89k
937
4.89k
  return isl_map_subtract(universe, map);
938
4.89k
}
939
940
__isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
941
4.89k
{
942
4.89k
  return isl_map_complement(set);
943
4.89k
}