Coverage Report

Created: 2018-08-20 19:24

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_union_map.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2010-2011 INRIA Saclay
3
 * Copyright 2013-2014 Ecole Normale Superieure
4
 * Copyright 2014      INRIA Rocquencourt
5
 * Copyright 2016-2017 Sven Verdoolaege
6
 *
7
 * Use of this software is governed by the MIT license
8
 *
9
 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10
 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11
 * 91893 Orsay, France 
12
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13
 * B.P. 105 - 78153 Le Chesnay, France
14
 */
15
16
#include <isl_map_private.h>
17
#include <isl_union_map_private.h>
18
#include <isl/ctx.h>
19
#include <isl/hash.h>
20
#include <isl_aff_private.h>
21
#include <isl/map.h>
22
#include <isl/set.h>
23
#include <isl_space_private.h>
24
#include <isl/union_set.h>
25
#include <isl_maybe_map.h>
26
27
#include <bset_from_bmap.c>
28
#include <set_to_map.c>
29
#include <set_from_map.c>
30
#include <uset_to_umap.c>
31
#include <uset_from_umap.c>
32
#include <set_list_from_map_list_inl.c>
33
34
/* Return the number of parameters of "umap", where "type"
35
 * is required to be set to isl_dim_param.
36
 */
37
unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
38
  enum isl_dim_type type)
39
968
{
40
968
  if (!umap)
41
0
    return 0;
42
968
43
968
  if (type != isl_dim_param)
44
968
    
isl_die0
(isl_union_map_get_ctx(umap), isl_error_invalid,
45
968
      "can only reference parameters", return 0);
46
968
47
968
  return isl_space_dim(umap->dim, type);
48
968
}
49
50
/* Return the number of parameters of "uset", where "type"
51
 * is required to be set to isl_dim_param.
52
 */
53
unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
54
  enum isl_dim_type type)
55
28
{
56
28
  return isl_union_map_dim(uset, type);
57
28
}
58
59
/* Return the id of the specified dimension.
60
 */
61
__isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
62
  enum isl_dim_type type, unsigned pos)
63
0
{
64
0
  if (!umap)
65
0
    return NULL;
66
0
67
0
  if (type != isl_dim_param)
68
0
    isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
69
0
      "can only reference parameters", return NULL);
70
0
71
0
  return isl_space_get_dim_id(umap->dim, type, pos);
72
0
}
73
74
/* Is this union set a parameter domain?
75
 */
76
isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
77
56.4k
{
78
56.4k
  isl_set *set;
79
56.4k
  isl_bool params;
80
56.4k
81
56.4k
  if (!uset)
82
8
    return isl_bool_error;
83
56.4k
  if (uset->table.n != 1)
84
19.0k
    return isl_bool_false;
85
37.3k
86
37.3k
  set = isl_set_from_union_set(isl_union_set_copy(uset));
87
37.3k
  params = isl_set_is_params(set);
88
37.3k
  isl_set_free(set);
89
37.3k
  return params;
90
37.3k
}
91
92
/* Is this union map actually a parameter domain?
93
 * Users should never call this function.  Outside of isl,
94
 * a union map can never be a parameter domain.
95
 */
96
isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
97
1.47k
{
98
1.47k
  return isl_union_set_is_params(uset_from_umap(umap));
99
1.47k
}
100
101
static __isl_give isl_union_map *isl_union_map_alloc(
102
  __isl_take isl_space *space, int size)
103
320k
{
104
320k
  isl_union_map *umap;
105
320k
106
320k
  space = isl_space_params(space);
107
320k
  if (!space)
108
27
    return NULL;
109
320k
110
320k
  umap = isl_calloc_type(space->ctx, isl_union_map);
111
320k
  if (!umap) {
112
42
    isl_space_free(space);
113
42
    return NULL;
114
42
  }
115
320k
116
320k
  umap->ref = 1;
117
320k
  umap->dim = space;
118
320k
  if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
119
8
    return isl_union_map_free(umap);
120
320k
121
320k
  return umap;
122
320k
}
123
124
__isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
125
141k
{
126
141k
  return isl_union_map_alloc(space, 16);
127
141k
}
128
129
__isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
130
25.1k
{
131
25.1k
  return isl_union_map_empty(space);
132
25.1k
}
133
134
isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
135
226k
{
136
226k
  return umap ? umap->dim->ctx : NULL;
137
226k
}
138
139
isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
140
14.2k
{
141
14.2k
  return uset ? uset->dim->ctx : NULL;
142
14.2k
}
143
144
/* Return the space of "umap".
145
 */
146
__isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
147
411k
{
148
411k
  return umap ? 
umap->dim410k
: NULL;
149
411k
}
150
151
__isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
152
410k
{
153
410k
  return isl_space_copy(isl_union_map_peek_space(umap));
154
410k
}
155
156
/* Return the position of the parameter with the given name
157
 * in "umap".
158
 * Return -1 if no such dimension can be found.
159
 */
160
int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
161
  enum isl_dim_type type, const char *name)
162
0
{
163
0
  if (!umap)
164
0
    return -1;
165
0
  return isl_space_find_dim_by_name(umap->dim, type, name);
166
0
}
167
168
__isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
169
24.0k
{
170
24.0k
  return isl_union_map_get_space(uset);
171
24.0k
}
172
173
static isl_stat free_umap_entry(void **entry, void *user)
174
372k
{
175
372k
  isl_map *map = *entry;
176
372k
  isl_map_free(map);
177
372k
  return isl_stat_ok;
178
372k
}
179
180
static isl_stat add_map(__isl_take isl_map *map, void *user)
181
51.1k
{
182
51.1k
  isl_union_map **umap = (isl_union_map **)user;
183
51.1k
184
51.1k
  *umap = isl_union_map_add_map(*umap, map);
185
51.1k
186
51.1k
  return isl_stat_ok;
187
51.1k
}
188
189
__isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
190
26.0k
{
191
26.0k
  isl_union_map *dup;
192
26.0k
193
26.0k
  if (!umap)
194
0
    return NULL;
195
26.0k
196
26.0k
  dup = isl_union_map_empty(isl_space_copy(umap->dim));
197
26.0k
  if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
198
0
    goto error;
199
26.0k
  return dup;
200
0
error:
201
0
  isl_union_map_free(dup);
202
0
  return NULL;
203
26.0k
}
204
205
__isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
206
429k
{
207
429k
  if (!umap)
208
13
    return NULL;
209
429k
210
429k
  if (umap->ref == 1)
211
403k
    return umap;
212
26.0k
  umap->ref--;
213
26.0k
  return isl_union_map_dup(umap);
214
26.0k
}
215
216
struct isl_union_align {
217
  isl_reordering *exp;
218
  isl_union_map *res;
219
};
220
221
static isl_stat align_entry(void **entry, void *user)
222
18.9k
{
223
18.9k
  isl_map *map = *entry;
224
18.9k
  isl_reordering *exp;
225
18.9k
  struct isl_union_align *data = user;
226
18.9k
227
18.9k
  exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
228
18.9k
            isl_map_get_space(map));
229
18.9k
230
18.9k
  data->res = isl_union_map_add_map(data->res,
231
18.9k
          isl_map_realign(isl_map_copy(map), exp));
232
18.9k
233
18.9k
  return isl_stat_ok;
234
18.9k
}
235
236
/* Align the parameters of umap along those of model.
237
 * The result has the parameters of model first, in the same order
238
 * as they appear in model, followed by any remaining parameters of
239
 * umap that do not appear in model.
240
 */
241
__isl_give isl_union_map *isl_union_map_align_params(
242
  __isl_take isl_union_map *umap, __isl_take isl_space *model)
243
282k
{
244
282k
  struct isl_union_align data = { NULL, NULL };
245
282k
  isl_bool equal_params;
246
282k
247
282k
  if (!umap || 
!model282k
)
248
136
    goto error;
249
282k
250
282k
  equal_params = isl_space_has_equal_params(umap->dim, model);
251
282k
  if (equal_params < 0)
252
0
    goto error;
253
282k
  if (equal_params) {
254
267k
    isl_space_free(model);
255
267k
    return umap;
256
267k
  }
257
14.4k
258
14.4k
  data.exp = isl_parameter_alignment_reordering(umap->dim, model);
259
14.4k
  if (!data.exp)
260
0
    goto error;
261
14.4k
262
14.4k
  data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
263
14.4k
          umap->table.n);
264
14.4k
  if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
265
14.4k
          &align_entry, &data) < 0)
266
0
    goto error;
267
14.4k
268
14.4k
  isl_reordering_free(data.exp);
269
14.4k
  isl_union_map_free(umap);
270
14.4k
  isl_space_free(model);
271
14.4k
  return data.res;
272
136
error:
273
136
  isl_reordering_free(data.exp);
274
136
  isl_union_map_free(umap);
275
136
  isl_union_map_free(data.res);
276
136
  isl_space_free(model);
277
136
  return NULL;
278
14.4k
}
279
280
__isl_give isl_union_set *isl_union_set_align_params(
281
  __isl_take isl_union_set *uset, __isl_take isl_space *model)
282
946
{
283
946
  return isl_union_map_align_params(uset, model);
284
946
}
285
286
__isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
287
  __isl_take isl_union_map *umap2)
288
42.9k
{
289
42.9k
  umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
290
42.9k
  umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
291
42.9k
292
42.9k
  umap1 = isl_union_map_cow(umap1);
293
42.9k
294
42.9k
  if (!umap1 || 
!umap242.9k
)
295
19
    goto error;
296
42.9k
297
42.9k
  if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
298
0
    goto error;
299
42.9k
300
42.9k
  isl_union_map_free(umap2);
301
42.9k
302
42.9k
  return umap1;
303
19
error:
304
19
  isl_union_map_free(umap1);
305
19
  isl_union_map_free(umap2);
306
19
  return NULL;
307
42.9k
}
308
309
__isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
310
  __isl_take isl_union_set *uset2)
311
4.64k
{
312
4.64k
  return isl_union_map_union(uset1, uset2);
313
4.64k
}
314
315
__isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
316
398k
{
317
398k
  if (!umap)
318
4.43k
    return NULL;
319
394k
320
394k
  umap->ref++;
321
394k
  return umap;
322
394k
}
323
324
__isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
325
140k
{
326
140k
  return isl_union_map_copy(uset);
327
140k
}
328
329
__isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
330
698k
{
331
698k
  if (!umap)
332
9.84k
    return NULL;
333
688k
334
688k
  if (--umap->ref > 0)
335
368k
    return NULL;
336
320k
337
320k
  isl_hash_table_foreach(umap->dim->ctx, &umap->table,
338
320k
             &free_umap_entry, NULL);
339
320k
  isl_hash_table_clear(&umap->table);
340
320k
  isl_space_free(umap->dim);
341
320k
  free(umap);
342
320k
  return NULL;
343
320k
}
344
345
__isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
346
88.4k
{
347
88.4k
  return isl_union_map_free(uset);
348
88.4k
}
349
350
/* Do "umap" and "space" have the same parameters?
351
 */
352
isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
353
  __isl_keep isl_space *space)
354
494
{
355
494
  isl_space *umap_space;
356
494
357
494
  umap_space = isl_union_map_peek_space(umap);
358
494
  return isl_space_has_equal_params(umap_space, space);
359
494
}
360
361
/* Do "uset" and "space" have the same parameters?
362
 */
363
isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
364
  __isl_keep isl_space *space)
365
17
{
366
17
  return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
367
17
}
368
369
static int has_space(const void *entry, const void *val)
370
76.7k
{
371
76.7k
  isl_map *map = (isl_map *)entry;
372
76.7k
  isl_space *space = (isl_space *) val;
373
76.7k
374
76.7k
  return isl_space_is_equal(map->dim, space);
375
76.7k
}
376
377
__isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
378
  __isl_take isl_map *map)
379
401k
{
380
401k
  uint32_t hash;
381
401k
  struct isl_hash_table_entry *entry;
382
401k
  isl_bool aligned;
383
401k
384
401k
  if (!map || 
!umap401k
)
385
164
    goto error;
386
401k
387
401k
  if (isl_map_plain_is_empty(map)) {
388
15.2k
    isl_map_free(map);
389
15.2k
    return umap;
390
15.2k
  }
391
386k
392
386k
  aligned = isl_map_space_has_equal_params(map, umap->dim);
393
386k
  if (aligned < 0)
394
0
    goto error;
395
386k
  if (!aligned) {
396
7.85k
    umap = isl_union_map_align_params(umap, isl_map_get_space(map));
397
7.85k
    map = isl_map_align_params(map, isl_union_map_get_space(umap));
398
7.85k
  }
399
386k
400
386k
  umap = isl_union_map_cow(umap);
401
386k
402
386k
  if (!map || !umap)
403
0
    goto error;
404
386k
405
386k
  hash = isl_space_get_hash(map->dim);
406
386k
  entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
407
386k
            &has_space, map->dim, 1);
408
386k
  if (!entry)
409
0
    goto error;
410
386k
411
386k
  if (!entry->data)
412
372k
    entry->data = map;
413
14.1k
  else {
414
14.1k
    entry->data = isl_map_union(entry->data, isl_map_copy(map));
415
14.1k
    if (!entry->data)
416
0
      goto error;
417
14.1k
    isl_map_free(map);
418
14.1k
  }
419
386k
420
386k
  return umap;
421
164
error:
422
164
  isl_map_free(map);
423
164
  isl_union_map_free(umap);
424
164
  return NULL;
425
386k
}
426
427
__isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
428
  __isl_take isl_set *set)
429
38.3k
{
430
38.3k
  return isl_union_map_add_map(uset, set_to_map(set));
431
38.3k
}
432
433
__isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
434
47.6k
{
435
47.6k
  isl_space *dim;
436
47.6k
  isl_union_map *umap;
437
47.6k
438
47.6k
  if (!map)
439
11
    return NULL;
440
47.6k
441
47.6k
  dim = isl_map_get_space(map);
442
47.6k
  dim = isl_space_params(dim);
443
47.6k
  umap = isl_union_map_empty(dim);
444
47.6k
  umap = isl_union_map_add_map(umap, map);
445
47.6k
446
47.6k
  return umap;
447
47.6k
}
448
449
__isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
450
15.0k
{
451
15.0k
  return isl_union_map_from_map(set_to_map(set));
452
15.0k
}
453
454
__isl_give isl_union_map *isl_union_map_from_basic_map(
455
  __isl_take isl_basic_map *bmap)
456
5.36k
{
457
5.36k
  return isl_union_map_from_map(isl_map_from_basic_map(bmap));
458
5.36k
}
459
460
__isl_give isl_union_set *isl_union_set_from_basic_set(
461
  __isl_take isl_basic_set *bset)
462
5.36k
{
463
5.36k
  return isl_union_map_from_basic_map(bset);
464
5.36k
}
465
466
struct isl_union_map_foreach_data
467
{
468
  isl_stat (*fn)(__isl_take isl_map *map, void *user);
469
  void *user;
470
};
471
472
static isl_stat call_on_copy(void **entry, void *user)
473
107k
{
474
107k
  isl_map *map = *entry;
475
107k
  struct isl_union_map_foreach_data *data;
476
107k
  data = (struct isl_union_map_foreach_data *)user;
477
107k
478
107k
  return data->fn(isl_map_copy(map), data->user);
479
107k
}
480
481
int isl_union_map_n_map(__isl_keep isl_union_map *umap)
482
28.5k
{
483
28.5k
  return umap ? umap->table.n : 
00
;
484
28.5k
}
485
486
int isl_union_set_n_set(__isl_keep isl_union_set *uset)
487
612
{
488
612
  return uset ? uset->table.n : 
00
;
489
612
}
490
491
isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
492
  isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
493
114k
{
494
114k
  struct isl_union_map_foreach_data data = { fn, user };
495
114k
496
114k
  if (!umap)
497
0
    return isl_stat_error;
498
114k
499
114k
  return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
500
114k
              &call_on_copy, &data);
501
114k
}
502
503
/* Internal data structure for isl_union_map_every_map.
504
 *
505
 * "test" is the user-specified callback function.
506
 * "user" is the user-specified callback function argument.
507
 *
508
 * "failed" is initialized to 0 and set to 1 if "test" fails
509
 * on any map.
510
 */
511
struct isl_union_map_every_data {
512
  isl_bool (*test)(__isl_keep isl_map *map, void *user);
513
  void *user;
514
  int failed;
515
};
516
517
/* Call data->test on "map".
518
 * If this fails, then set data->failed and abort.
519
 */
520
static isl_stat call_every(void **entry, void *user)
521
0
{
522
0
  isl_map *map = *entry;
523
0
  struct isl_union_map_every_data *data = user;
524
0
  isl_bool r;
525
0
526
0
  r = data->test(map, data->user);
527
0
  if (r < 0)
528
0
    return isl_stat_error;
529
0
  if (r)
530
0
    return isl_stat_ok;
531
0
  data->failed = 1;
532
0
  return isl_stat_error;
533
0
}
534
535
/* Does "test" succeed on every map in "umap"?
536
 */
537
isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
538
  isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
539
0
{
540
0
  struct isl_union_map_every_data data = { test, user, 0 };
541
0
  isl_stat r;
542
0
543
0
  if (!umap)
544
0
    return isl_bool_error;
545
0
546
0
  r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
547
0
              &call_every, &data);
548
0
  if (r >= 0)
549
0
    return isl_bool_true;
550
0
  if (data.failed)
551
0
    return isl_bool_false;
552
0
  return isl_bool_error;
553
0
}
554
555
/* Add "map" to "list".
556
 */
557
static isl_stat add_list_map(__isl_take isl_map *map, void *user)
558
11.5k
{
559
11.5k
  isl_map_list **list = user;
560
11.5k
561
11.5k
  *list = isl_map_list_add(*list, map);
562
11.5k
563
11.5k
  if (!*list)
564
9
    return isl_stat_error;
565
11.5k
  return isl_stat_ok;
566
11.5k
}
567
568
/* Return the maps in "umap" as a list.
569
 *
570
 * First construct a list of the appropriate size and then add all the
571
 * elements.
572
 */
573
__isl_give isl_map_list *isl_union_map_get_map_list(
574
  __isl_keep isl_union_map *umap)
575
6.97k
{
576
6.97k
  int n_maps;
577
6.97k
  isl_ctx *ctx;
578
6.97k
  isl_map_list *list;
579
6.97k
580
6.97k
  if (!umap)
581
19
    return NULL;
582
6.95k
  ctx = isl_union_map_get_ctx(umap);
583
6.95k
  n_maps = isl_union_map_n_map(umap);
584
6.95k
  list = isl_map_list_alloc(ctx, n_maps);
585
6.95k
586
6.95k
  if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
587
9
    list = isl_map_list_free(list);
588
6.95k
589
6.95k
  return list;
590
6.95k
}
591
592
/* Return the sets in "uset" as a list.
593
 */
594
__isl_give isl_set_list *isl_union_set_get_set_list(
595
  __isl_keep isl_union_set *uset)
596
2.84k
{
597
2.84k
  return set_list_from_map_list(
598
2.84k
    isl_union_map_get_map_list(uset_to_umap(uset)));
599
2.84k
}
600
601
static isl_stat copy_map(void **entry, void *user)
602
60.3k
{
603
60.3k
  isl_map *map = *entry;
604
60.3k
  isl_map **map_p = user;
605
60.3k
606
60.3k
  *map_p = isl_map_copy(map);
607
60.3k
608
60.3k
  return isl_stat_error;
609
60.3k
}
610
611
__isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
612
60.3k
{
613
60.3k
  isl_ctx *ctx;
614
60.3k
  isl_map *map = NULL;
615
60.3k
616
60.3k
  if (!umap)
617
8
    return NULL;
618
60.3k
  ctx = isl_union_map_get_ctx(umap);
619
60.3k
  if (umap->table.n != 1)
620
60.3k
    
isl_die0
(ctx, isl_error_invalid,
621
60.3k
      "union map needs to contain elements in exactly "
622
60.3k
      "one space", goto error);
623
60.3k
624
60.3k
  isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
625
60.3k
626
60.3k
  isl_union_map_free(umap);
627
60.3k
628
60.3k
  return map;
629
0
error:
630
0
  isl_union_map_free(umap);
631
0
  return NULL;
632
60.3k
}
633
634
__isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
635
57.9k
{
636
57.9k
  return isl_map_from_union_map(uset);
637
57.9k
}
638
639
/* Extract the map in "umap" that lives in the given space (ignoring
640
 * parameters).
641
 */
642
__isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
643
  __isl_take isl_space *space)
644
4.08k
{
645
4.08k
  uint32_t hash;
646
4.08k
  struct isl_hash_table_entry *entry;
647
4.08k
648
4.08k
  space = isl_space_drop_dims(space, isl_dim_param,
649
4.08k
          0, isl_space_dim(space, isl_dim_param));
650
4.08k
  space = isl_space_align_params(space, isl_union_map_get_space(umap));
651
4.08k
  if (!umap || !space)
652
2
    goto error;
653
4.08k
654
4.08k
  hash = isl_space_get_hash(space);
655
4.08k
  entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
656
4.08k
            &has_space, space, 0);
657
4.08k
  if (!entry)
658
1.72k
    return isl_map_empty(space);
659
2.35k
  isl_space_free(space);
660
2.35k
  return isl_map_copy(entry->data);
661
2
error:
662
2
  isl_space_free(space);
663
2
  return NULL;
664
2.35k
}
665
666
__isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
667
  __isl_take isl_space *dim)
668
3.33k
{
669
3.33k
  return set_from_map(isl_union_map_extract_map(uset, dim));
670
3.33k
}
671
672
/* Check if umap contains a map in the given space.
673
 */
674
isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
675
  __isl_keep isl_space *space)
676
275
{
677
275
  uint32_t hash;
678
275
  struct isl_hash_table_entry *entry;
679
275
680
275
  if (!umap || !space)
681
0
    return isl_bool_error;
682
275
683
275
  hash = isl_space_get_hash(space);
684
275
  entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
685
275
            &has_space, space, 0);
686
275
  return !!entry;
687
275
}
688
689
isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
690
  __isl_keep isl_space *space)
691
275
{
692
275
  return isl_union_map_contains(uset, space);
693
275
}
694
695
isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
696
  isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
697
8.08k
{
698
8.08k
  return isl_union_map_foreach_map(uset,
699
8.08k
    (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
700
8.08k
}
701
702
struct isl_union_set_foreach_point_data {
703
  isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
704
  void *user;
705
};
706
707
static isl_stat foreach_point(__isl_take isl_set *set, void *user)
708
0
{
709
0
  struct isl_union_set_foreach_point_data *data = user;
710
0
  isl_stat r;
711
0
712
0
  r = isl_set_foreach_point(set, data->fn, data->user);
713
0
  isl_set_free(set);
714
0
715
0
  return r;
716
0
}
717
718
isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
719
  isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
720
0
{
721
0
  struct isl_union_set_foreach_point_data data = { fn, user };
722
0
  return isl_union_set_foreach_set(uset, &foreach_point, &data);
723
0
}
724
725
/* Data structure that specifies how gen_bin_op should
726
 * construct results from the inputs.
727
 *
728
 * If "subtract" is set, then a map in the first input is copied to the result
729
 * if there is no corresponding map in the second input.
730
 * Otherwise, a map in the first input with no corresponding map
731
 * in the second input is ignored.
732
 * If "filter" is not NULL, then it specifies which maps in the first
733
 * input may have a matching map in the second input.
734
 * In particular, it makes sure that "match_space" can be called
735
 * on the space of the map.
736
 * "match_space" specifies how to transform the space of a map
737
 * in the first input to the space of the corresponding map
738
 * in the second input.
739
 * "fn_map" specifies how the matching maps, one from each input,
740
 * should be combined to form a map in the result.
741
 */
742
struct isl_bin_op_control {
743
  int subtract;
744
  isl_bool (*filter)(__isl_keep isl_map *map);
745
  __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
746
  __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
747
    __isl_take isl_map *map2);
748
};
749
750
/* Internal data structure for gen_bin_op.
751
 * "control" specifies how the maps in the result should be constructed.
752
 * "umap2" is a pointer to the second argument.
753
 * "res" collects the results.
754
 */
755
struct isl_union_map_gen_bin_data {
756
  struct isl_bin_op_control *control;
757
  isl_union_map *umap2;
758
  isl_union_map *res;
759
};
760
761
/* Add a copy of "map" to "res" and return the result.
762
 */
763
static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
764
  __isl_keep isl_map *map)
765
1.62k
{
766
1.62k
  return isl_union_map_add_map(res, isl_map_copy(map));
767
1.62k
}
768
769
/* Combine "map1" and "map2", add the result to "res" and return the result.
770
 * Check whether the result is empty before adding it to "res".
771
 */
772
static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
773
  __isl_keep isl_map *map1, __isl_keep isl_map *map2,
774
  struct isl_union_map_gen_bin_data *data)
775
30.1k
{
776
30.1k
  isl_bool empty;
777
30.1k
  isl_map *map;
778
30.1k
779
30.1k
  map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
780
30.1k
  empty = isl_map_is_empty(map);
781
30.1k
  if (empty < 0 || 
empty30.1k
) {
782
3.51k
    isl_map_free(map);
783
3.51k
    if (empty < 0)
784
1
      return isl_union_map_free(res);
785
3.51k
    return res;
786
3.51k
  }
787
26.6k
  return isl_union_map_add_map(res, map);
788
26.6k
}
789
790
/* Dummy match_space function that simply returns the input space.
791
 */
792
static __isl_give isl_space *identity(__isl_take isl_space *space)
793
0
{
794
0
  return space;
795
0
}
796
797
/* Look for the map in data->umap2 that corresponds to "map", if any.
798
 * Return (isl_bool_true, matching map) if there is one,
799
 * (isl_bool_false, NULL) if there is no matching map and
800
 * (isl_bool_error, NULL) on error.
801
 *
802
 * If not NULL, then data->control->filter specifies whether "map"
803
 * can have any matching map.  If so,
804
 * data->control->match_space specifies which map in data->umap2
805
 * corresponds to "map".
806
 */
807
static __isl_keep isl_maybe_isl_map bin_try_get_match(
808
  struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
809
59.2k
{
810
59.2k
  uint32_t hash;
811
59.2k
  struct isl_hash_table_entry *entry2;
812
59.2k
  isl_space *space;
813
59.2k
  isl_maybe_isl_map res = { isl_bool_error, NULL };
814
59.2k
815
59.2k
  if (data->control->filter) {
816
0
    res.valid = data->control->filter(map);
817
0
    if (res.valid < 0 || !res.valid)
818
0
      return res;
819
0
    res.valid = isl_bool_error;
820
0
  }
821
59.2k
822
59.2k
  space = isl_map_get_space(map);
823
59.2k
  if (data->control->match_space != &identity)
824
56.4k
    space = data->control->match_space(space);
825
59.2k
  if (!space)
826
0
    return res;
827
59.2k
  hash = isl_space_get_hash(space);
828
59.2k
  entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
829
59.2k
             &data->umap2->table, hash,
830
59.2k
             &has_space, space, 0);
831
59.2k
  isl_space_free(space);
832
59.2k
  res.valid = entry2 != NULL;
833
59.2k
  if (entry2)
834
30.1k
    res.value = entry2->data;
835
59.2k
836
59.2k
  return res;
837
59.2k
}
838
839
/* isl_hash_table_foreach callback for gen_bin_op.
840
 * Look for the map in data->umap2 that corresponds
841
 * to the map that "entry" points to, apply the binary operation and
842
 * add the result to data->res.
843
 *
844
 * If no corresponding map can be found, then the effect depends
845
 * on data->control->subtract.  If it is set, then the current map
846
 * is added directly to the result.  Otherwise, it is ignored.
847
 */
848
static isl_stat gen_bin_entry(void **entry, void *user)
849
59.2k
{
850
59.2k
  struct isl_union_map_gen_bin_data *data = user;
851
59.2k
  isl_map *map = *entry;
852
59.2k
  isl_maybe_isl_map m;
853
59.2k
854
59.2k
  m = bin_try_get_match(data, map);
855
59.2k
  if (m.valid < 0)
856
0
    return isl_stat_error;
857
59.2k
  if (!m.valid && 
!data->control->subtract29.0k
)
858
27.4k
    return isl_stat_ok;
859
31.8k
860
31.8k
  if (!m.valid)
861
1.62k
    data->res = bin_add_map(data->res, map);
862
30.1k
  else
863
30.1k
    data->res = bin_add_pair(data->res, map, m.value, data);
864
31.8k
  if (!data->res)
865
1
    return isl_stat_error;
866
31.8k
867
31.8k
  return isl_stat_ok;
868
31.8k
}
869
870
/* Apply a binary operation to "umap1" and "umap2" based on "control".
871
 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
872
 * in gen_bin_entry.
873
 */
874
static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
875
  __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
876
34.2k
{
877
34.2k
  struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
878
34.2k
879
34.2k
  umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
880
34.2k
  umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
881
34.2k
882
34.2k
  if (!umap1 || 
!umap234.2k
)
883
6
    goto error;
884
34.2k
885
34.2k
  data.umap2 = umap2;
886
34.2k
  data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
887
34.2k
               umap1->table.n);
888
34.2k
  if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
889
34.2k
           &gen_bin_entry, &data) < 0)
890
1
    goto error;
891
34.2k
892
34.2k
  isl_union_map_free(umap1);
893
34.2k
  isl_union_map_free(umap2);
894
34.2k
  return data.res;
895
7
error:
896
7
  isl_union_map_free(umap1);
897
7
  isl_union_map_free(umap2);
898
7
  isl_union_map_free(data.res);
899
7
  return NULL;
900
34.2k
}
901
902
__isl_give isl_union_map *isl_union_map_subtract(
903
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
904
3.31k
{
905
3.31k
  struct isl_bin_op_control control = {
906
3.31k
    .subtract = 1,
907
3.31k
    .match_space = &identity,
908
3.31k
    .fn_map = &isl_map_subtract,
909
3.31k
  };
910
3.31k
911
3.31k
  return gen_bin_op(umap1, umap2, &control);
912
3.31k
}
913
914
__isl_give isl_union_set *isl_union_set_subtract(
915
  __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
916
462
{
917
462
  return isl_union_map_subtract(uset1, uset2);
918
462
}
919
920
struct isl_union_map_gen_bin_set_data {
921
  isl_set *set;
922
  isl_union_map *res;
923
};
924
925
static isl_stat intersect_params_entry(void **entry, void *user)
926
14
{
927
14
  struct isl_union_map_gen_bin_set_data *data = user;
928
14
  isl_map *map = *entry;
929
14
  int empty;
930
14
931
14
  map = isl_map_copy(map);
932
14
  map = isl_map_intersect_params(map, isl_set_copy(data->set));
933
14
934
14
  empty = isl_map_is_empty(map);
935
14
  if (empty < 0) {
936
0
    isl_map_free(map);
937
0
    return isl_stat_error;
938
0
  }
939
14
940
14
  data->res = isl_union_map_add_map(data->res, map);
941
14
942
14
  return isl_stat_ok;
943
14
}
944
945
static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
946
  __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
947
1.17k
{
948
1.17k
  struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
949
1.17k
950
1.17k
  umap = isl_union_map_align_params(umap, isl_set_get_space(set));
951
1.17k
  set = isl_set_align_params(set, isl_union_map_get_space(umap));
952
1.17k
953
1.17k
  if (!umap || !set)
954
0
    goto error;
955
1.17k
956
1.17k
  data.set = set;
957
1.17k
  data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
958
1.17k
               umap->table.n);
959
1.17k
  if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
960
1.17k
           fn, &data) < 0)
961
0
    goto error;
962
1.17k
963
1.17k
  isl_union_map_free(umap);
964
1.17k
  isl_set_free(set);
965
1.17k
  return data.res;
966
0
error:
967
0
  isl_union_map_free(umap);
968
0
  isl_set_free(set);
969
0
  isl_union_map_free(data.res);
970
0
  return NULL;
971
1.17k
}
972
973
/* Intersect "umap" with the parameter domain "set".
974
 *
975
 * If "set" does not have any constraints, then we can return immediately.
976
 */
977
__isl_give isl_union_map *isl_union_map_intersect_params(
978
  __isl_take isl_union_map *umap, __isl_take isl_set *set)
979
930
{
980
930
  int is_universe;
981
930
982
930
  is_universe = isl_set_plain_is_universe(set);
983
930
  if (is_universe < 0)
984
8
    goto error;
985
922
  if (is_universe) {
986
891
    isl_set_free(set);
987
891
    return umap;
988
891
  }
989
31
990
31
  return gen_bin_set_op(umap, set, &intersect_params_entry);
991
8
error:
992
8
  isl_union_map_free(umap);
993
8
  isl_set_free(set);
994
8
  return NULL;
995
31
}
996
997
__isl_give isl_union_set *isl_union_set_intersect_params(
998
  __isl_take isl_union_set *uset, __isl_take isl_set *set)
999
155
{
1000
155
  return isl_union_map_intersect_params(uset, set);
1001
155
}
1002
1003
static __isl_give isl_union_map *union_map_intersect_params(
1004
  __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1005
8
{
1006
8
  return isl_union_map_intersect_params(umap,
1007
8
            isl_set_from_union_set(uset));
1008
8
}
1009
1010
static __isl_give isl_union_map *union_map_gist_params(
1011
  __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1012
0
{
1013
0
  return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1014
0
}
1015
1016
struct isl_union_map_match_bin_data {
1017
  isl_union_map *umap2;
1018
  isl_union_map *res;
1019
  __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1020
};
1021
1022
static isl_stat match_bin_entry(void **entry, void *user)
1023
21.5k
{
1024
21.5k
  struct isl_union_map_match_bin_data *data = user;
1025
21.5k
  uint32_t hash;
1026
21.5k
  struct isl_hash_table_entry *entry2;
1027
21.5k
  isl_map *map = *entry;
1028
21.5k
  int empty;
1029
21.5k
1030
21.5k
  hash = isl_space_get_hash(map->dim);
1031
21.5k
  entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1032
21.5k
             hash, &has_space, map->dim, 0);
1033
21.5k
  if (!entry2)
1034
1.88k
    return isl_stat_ok;
1035
19.7k
1036
19.7k
  map = isl_map_copy(map);
1037
19.7k
  map = data->fn(map, isl_map_copy(entry2->data));
1038
19.7k
1039
19.7k
  empty = isl_map_is_empty(map);
1040
19.7k
  if (empty < 0) {
1041
0
    isl_map_free(map);
1042
0
    return isl_stat_error;
1043
0
  }
1044
19.7k
  if (empty) {
1045
341
    isl_map_free(map);
1046
341
    return isl_stat_ok;
1047
341
  }
1048
19.3k
1049
19.3k
  data->res = isl_union_map_add_map(data->res, map);
1050
19.3k
1051
19.3k
  return isl_stat_ok;
1052
19.3k
}
1053
1054
static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1055
  __isl_take isl_union_map *umap2,
1056
  __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1057
15.8k
{
1058
15.8k
  struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1059
15.8k
1060
15.8k
  umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1061
15.8k
  umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1062
15.8k
1063
15.8k
  if (!umap1 || 
!umap215.8k
)
1064
5
    goto error;
1065
15.8k
1066
15.8k
  data.umap2 = umap2;
1067
15.8k
  data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1068
15.8k
               umap1->table.n);
1069
15.8k
  if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1070
15.8k
           &match_bin_entry, &data) < 0)
1071
0
    goto error;
1072
15.8k
1073
15.8k
  isl_union_map_free(umap1);
1074
15.8k
  isl_union_map_free(umap2);
1075
15.8k
  return data.res;
1076
5
error:
1077
5
  isl_union_map_free(umap1);
1078
5
  isl_union_map_free(umap2);
1079
5
  isl_union_map_free(data.res);
1080
5
  return NULL;
1081
15.8k
}
1082
1083
__isl_give isl_union_map *isl_union_map_intersect(
1084
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1085
12.2k
{
1086
12.2k
  return match_bin_op(umap1, umap2, &isl_map_intersect);
1087
12.2k
}
1088
1089
/* Compute the intersection of the two union_sets.
1090
 * As a special case, if exactly one of the two union_sets
1091
 * is a parameter domain, then intersect the parameter domain
1092
 * of the other one with this set.
1093
 */
1094
__isl_give isl_union_set *isl_union_set_intersect(
1095
  __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1096
10.8k
{
1097
10.8k
  int p1, p2;
1098
10.8k
1099
10.8k
  p1 = isl_union_set_is_params(uset1);
1100
10.8k
  p2 = isl_union_set_is_params(uset2);
1101
10.8k
  if (p1 < 0 || p2 < 0)
1102
0
    goto error;
1103
10.8k
  if (!p1 && p2)
1104
0
    return union_map_intersect_params(uset1, uset2);
1105
10.8k
  if (p1 && 
!p20
)
1106
0
    return union_map_intersect_params(uset2, uset1);
1107
10.8k
  return isl_union_map_intersect(uset1, uset2);
1108
0
error:
1109
0
  isl_union_set_free(uset1);
1110
0
  isl_union_set_free(uset2);
1111
0
  return NULL;
1112
10.8k
}
1113
1114
static isl_stat gist_params_entry(void **entry, void *user)
1115
2.28k
{
1116
2.28k
  struct isl_union_map_gen_bin_set_data *data = user;
1117
2.28k
  isl_map *map = *entry;
1118
2.28k
  int empty;
1119
2.28k
1120
2.28k
  map = isl_map_copy(map);
1121
2.28k
  map = isl_map_gist_params(map, isl_set_copy(data->set));
1122
2.28k
1123
2.28k
  empty = isl_map_is_empty(map);
1124
2.28k
  if (empty < 0) {
1125
0
    isl_map_free(map);
1126
0
    return isl_stat_error;
1127
0
  }
1128
2.28k
1129
2.28k
  data->res = isl_union_map_add_map(data->res, map);
1130
2.28k
1131
2.28k
  return isl_stat_ok;
1132
2.28k
}
1133
1134
__isl_give isl_union_map *isl_union_map_gist_params(
1135
  __isl_take isl_union_map *umap, __isl_take isl_set *set)
1136
1.14k
{
1137
1.14k
  return gen_bin_set_op(umap, set, &gist_params_entry);
1138
1.14k
}
1139
1140
__isl_give isl_union_set *isl_union_set_gist_params(
1141
  __isl_take isl_union_set *uset, __isl_take isl_set *set)
1142
1.14k
{
1143
1.14k
  return isl_union_map_gist_params(uset, set);
1144
1.14k
}
1145
1146
__isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1147
  __isl_take isl_union_map *context)
1148
3.41k
{
1149
3.41k
  return match_bin_op(umap, context, &isl_map_gist);
1150
3.41k
}
1151
1152
__isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1153
  __isl_take isl_union_set *context)
1154
3.41k
{
1155
3.41k
  if (isl_union_set_is_params(context))
1156
0
    return union_map_gist_params(uset, context);
1157
3.41k
  return isl_union_map_gist(uset, context);
1158
3.41k
}
1159
1160
/* For each map in "umap", remove the constraints in the corresponding map
1161
 * of "context".
1162
 * Each map in "context" is assumed to consist of a single disjunct and
1163
 * to have explicit representations for all local variables.
1164
 */
1165
__isl_give isl_union_map *isl_union_map_plain_gist(
1166
  __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1167
28
{
1168
28
  return match_bin_op(umap, context, &isl_map_plain_gist);
1169
28
}
1170
1171
/* For each set in "uset", remove the constraints in the corresponding set
1172
 * of "context".
1173
 * Each set in "context" is assumed to consist of a single disjunct and
1174
 * to have explicit representations for all local variables.
1175
 */
1176
__isl_give isl_union_set *isl_union_set_plain_gist(
1177
  __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1178
28
{
1179
28
  return isl_union_map_plain_gist(uset, context);
1180
28
}
1181
1182
static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1183
  __isl_take isl_map *set2)
1184
93
{
1185
93
  return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1186
93
}
1187
1188
static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1189
  __isl_take isl_map *set2)
1190
1
{
1191
1
  return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1192
1
}
1193
1194
__isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1195
  __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1196
1
{
1197
1
  return match_bin_op(uset1, uset2, &lex_lt_set);
1198
1
}
1199
1200
__isl_give isl_union_map *isl_union_set_lex_le_union_set(
1201
  __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1202
73
{
1203
73
  return match_bin_op(uset1, uset2, &lex_le_set);
1204
73
}
1205
1206
__isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1207
  __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1208
1
{
1209
1
  return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1210
1
}
1211
1212
__isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1213
  __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1214
0
{
1215
0
  return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1216
0
}
1217
1218
__isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1219
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1220
0
{
1221
0
  return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1222
0
}
1223
1224
__isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1225
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1226
0
{
1227
0
  return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1228
0
}
1229
1230
/* Intersect the domain of "umap" with "uset".
1231
 */
1232
static __isl_give isl_union_map *union_map_intersect_domain(
1233
  __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1234
29.3k
{
1235
29.3k
  struct isl_bin_op_control control = {
1236
29.3k
    .match_space = &isl_space_domain,
1237
29.3k
    .fn_map = &isl_map_intersect_domain,
1238
29.3k
  };
1239
29.3k
1240
29.3k
  return gen_bin_op(umap, uset, &control);
1241
29.3k
}
1242
1243
/* Intersect the domain of "umap" with "uset".
1244
 * If "uset" is a parameters domain, then intersect the parameter
1245
 * domain of "umap" with this set.
1246
 */
1247
__isl_give isl_union_map *isl_union_map_intersect_domain(
1248
  __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1249
29.4k
{
1250
29.4k
  if (isl_union_set_is_params(uset))
1251
8
    return union_map_intersect_params(umap, uset);
1252
29.3k
  else
1253
29.3k
    return union_map_intersect_domain(umap, uset);
1254
29.4k
}
1255
1256
/* Remove the elements of "uset" from the domain of "umap".
1257
 */
1258
__isl_give isl_union_map *isl_union_map_subtract_domain(
1259
  __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1260
228
{
1261
228
  struct isl_bin_op_control control = {
1262
228
    .subtract = 1,
1263
228
    .match_space = &isl_space_domain,
1264
228
    .fn_map = &isl_map_subtract_domain,
1265
228
  };
1266
228
1267
228
  return gen_bin_op(umap, dom, &control);
1268
228
}
1269
1270
/* Remove the elements of "uset" from the range of "umap".
1271
 */
1272
__isl_give isl_union_map *isl_union_map_subtract_range(
1273
  __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1274
3
{
1275
3
  struct isl_bin_op_control control = {
1276
3
    .subtract = 1,
1277
3
    .match_space = &isl_space_range,
1278
3
    .fn_map = &isl_map_subtract_range,
1279
3
  };
1280
3
1281
3
  return gen_bin_op(umap, dom, &control);
1282
3
}
1283
1284
/* Compute the gist of "umap" with respect to the domain "uset".
1285
 */
1286
static __isl_give isl_union_map *union_map_gist_domain(
1287
  __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1288
107
{
1289
107
  struct isl_bin_op_control control = {
1290
107
    .match_space = &isl_space_domain,
1291
107
    .fn_map = &isl_map_gist_domain,
1292
107
  };
1293
107
1294
107
  return gen_bin_op(umap, uset, &control);
1295
107
}
1296
1297
/* Compute the gist of "umap" with respect to the domain "uset".
1298
 * If "uset" is a parameters domain, then compute the gist
1299
 * with respect to this parameter domain.
1300
 */
1301
__isl_give isl_union_map *isl_union_map_gist_domain(
1302
  __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1303
107
{
1304
107
  if (isl_union_set_is_params(uset))
1305
0
    return union_map_gist_params(umap, uset);
1306
107
  else
1307
107
    return union_map_gist_domain(umap, uset);
1308
107
}
1309
1310
/* Compute the gist of "umap" with respect to the range "uset".
1311
 */
1312
__isl_give isl_union_map *isl_union_map_gist_range(
1313
  __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1314
78
{
1315
78
  struct isl_bin_op_control control = {
1316
78
    .match_space = &isl_space_range,
1317
78
    .fn_map = &isl_map_gist_range,
1318
78
  };
1319
78
1320
78
  return gen_bin_op(umap, uset, &control);
1321
78
}
1322
1323
__isl_give isl_union_map *isl_union_map_intersect_range(
1324
  __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1325
1.08k
{
1326
1.08k
  struct isl_bin_op_control control = {
1327
1.08k
    .match_space = &isl_space_range,
1328
1.08k
    .fn_map = &isl_map_intersect_range,
1329
1.08k
  };
1330
1.08k
1331
1.08k
  return gen_bin_op(umap, uset, &control);
1332
1.08k
}
1333
1334
/* Intersect each map in "umap" in a space A -> [B -> C]
1335
 * with the corresponding map in "factor" in the space A -> C and
1336
 * collect the results.
1337
 */
1338
__isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1339
  __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1340
0
{
1341
0
  struct isl_bin_op_control control = {
1342
0
    .filter = &isl_map_range_is_wrapping,
1343
0
    .match_space = &isl_space_range_factor_range,
1344
0
    .fn_map = &isl_map_intersect_range_factor_range,
1345
0
  };
1346
0
1347
0
  return gen_bin_op(umap, factor, &control);
1348
0
}
1349
1350
struct isl_union_map_bin_data {
1351
  isl_union_map *umap2;
1352
  isl_union_map *res;
1353
  isl_map *map;
1354
  isl_stat (*fn)(void **entry, void *user);
1355
};
1356
1357
static isl_stat apply_range_entry(void **entry, void *user)
1358
48.7k
{
1359
48.7k
  struct isl_union_map_bin_data *data = user;
1360
48.7k
  isl_map *map2 = *entry;
1361
48.7k
  isl_bool empty;
1362
48.7k
1363
48.7k
  if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1364
48.7k
         map2->dim, isl_dim_in))
1365
32.5k
    return isl_stat_ok;
1366
16.2k
1367
16.2k
  map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1368
16.2k
1369
16.2k
  empty = isl_map_is_empty(map2);
1370
16.2k
  if (empty < 0) {
1371
0
    isl_map_free(map2);
1372
0
    return isl_stat_error;
1373
0
  }
1374
16.2k
  if (empty) {
1375
1.12k
    isl_map_free(map2);
1376
1.12k
    return isl_stat_ok;
1377
1.12k
  }
1378
15.0k
1379
15.0k
  data->res = isl_union_map_add_map(data->res, map2);
1380
15.0k
1381
15.0k
  return isl_stat_ok;
1382
15.0k
}
1383
1384
static isl_stat bin_entry(void **entry, void *user)
1385
101k
{
1386
101k
  struct isl_union_map_bin_data *data = user;
1387
101k
  isl_map *map = *entry;
1388
101k
1389
101k
  data->map = map;
1390
101k
  if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1391
101k
           data->fn, data) < 0)
1392
0
    return isl_stat_error;
1393
101k
1394
101k
  return isl_stat_ok;
1395
101k
}
1396
1397
static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1398
  __isl_take isl_union_map *umap2,
1399
  isl_stat (*fn)(void **entry, void *user))
1400
31.4k
{
1401
31.4k
  struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1402
31.4k
1403
31.4k
  umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1404
31.4k
  umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1405
31.4k
1406
31.4k
  if (!umap1 || 
!umap231.3k
)
1407
44
    goto error;
1408
31.3k
1409
31.3k
  data.umap2 = umap2;
1410
31.3k
  data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1411
31.3k
               umap1->table.n);
1412
31.3k
  if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1413
31.3k
           &bin_entry, &data) < 0)
1414
0
    goto error;
1415
31.3k
1416
31.3k
  isl_union_map_free(umap1);
1417
31.3k
  isl_union_map_free(umap2);
1418
31.3k
  return data.res;
1419
44
error:
1420
44
  isl_union_map_free(umap1);
1421
44
  isl_union_map_free(umap2);
1422
44
  isl_union_map_free(data.res);
1423
44
  return NULL;
1424
31.3k
}
1425
1426
__isl_give isl_union_map *isl_union_map_apply_range(
1427
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1428
18.3k
{
1429
18.3k
  return bin_op(umap1, umap2, &apply_range_entry);
1430
18.3k
}
1431
1432
__isl_give isl_union_map *isl_union_map_apply_domain(
1433
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1434
2.75k
{
1435
2.75k
  umap1 = isl_union_map_reverse(umap1);
1436
2.75k
  umap1 = isl_union_map_apply_range(umap1, umap2);
1437
2.75k
  return isl_union_map_reverse(umap1);
1438
2.75k
}
1439
1440
__isl_give isl_union_set *isl_union_set_apply(
1441
  __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1442
13
{
1443
13
  return isl_union_map_apply_range(uset, umap);
1444
13
}
1445
1446
static isl_stat map_lex_lt_entry(void **entry, void *user)
1447
0
{
1448
0
  struct isl_union_map_bin_data *data = user;
1449
0
  isl_map *map2 = *entry;
1450
0
1451
0
  if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1452
0
         map2->dim, isl_dim_out))
1453
0
    return isl_stat_ok;
1454
0
1455
0
  map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1456
0
1457
0
  data->res = isl_union_map_add_map(data->res, map2);
1458
0
1459
0
  return isl_stat_ok;
1460
0
}
1461
1462
__isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1463
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1464
0
{
1465
0
  return bin_op(umap1, umap2, &map_lex_lt_entry);
1466
0
}
1467
1468
static isl_stat map_lex_le_entry(void **entry, void *user)
1469
0
{
1470
0
  struct isl_union_map_bin_data *data = user;
1471
0
  isl_map *map2 = *entry;
1472
0
1473
0
  if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1474
0
         map2->dim, isl_dim_out))
1475
0
    return isl_stat_ok;
1476
0
1477
0
  map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1478
0
1479
0
  data->res = isl_union_map_add_map(data->res, map2);
1480
0
1481
0
  return isl_stat_ok;
1482
0
}
1483
1484
__isl_give isl_union_map *isl_union_map_lex_le_union_map(
1485
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1486
0
{
1487
0
  return bin_op(umap1, umap2, &map_lex_le_entry);
1488
0
}
1489
1490
static isl_stat product_entry(void **entry, void *user)
1491
244
{
1492
244
  struct isl_union_map_bin_data *data = user;
1493
244
  isl_map *map2 = *entry;
1494
244
1495
244
  map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1496
244
1497
244
  data->res = isl_union_map_add_map(data->res, map2);
1498
244
1499
244
  return isl_stat_ok;
1500
244
}
1501
1502
__isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1503
  __isl_take isl_union_map *umap2)
1504
77
{
1505
77
  return bin_op(umap1, umap2, &product_entry);
1506
77
}
1507
1508
static isl_stat set_product_entry(void **entry, void *user)
1509
1
{
1510
1
  struct isl_union_map_bin_data *data = user;
1511
1
  isl_set *set2 = *entry;
1512
1
1513
1
  set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1514
1
1515
1
  data->res = isl_union_set_add_set(data->res, set2);
1516
1
1517
1
  return isl_stat_ok;
1518
1
}
1519
1520
__isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1521
  __isl_take isl_union_set *uset2)
1522
1
{
1523
1
  return bin_op(uset1, uset2, &set_product_entry);
1524
1
}
1525
1526
static isl_stat domain_product_entry(void **entry, void *user)
1527
1.42k
{
1528
1.42k
  struct isl_union_map_bin_data *data = user;
1529
1.42k
  isl_map *map2 = *entry;
1530
1.42k
1531
1.42k
  if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1532
1.42k
         map2->dim, isl_dim_out))
1533
662
    return isl_stat_ok;
1534
758
1535
758
  map2 = isl_map_domain_product(isl_map_copy(data->map),
1536
758
             isl_map_copy(map2));
1537
758
1538
758
  data->res = isl_union_map_add_map(data->res, map2);
1539
758
1540
758
  return isl_stat_ok;
1541
758
}
1542
1543
/* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1544
 */
1545
__isl_give isl_union_map *isl_union_map_domain_product(
1546
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1547
595
{
1548
595
  return bin_op(umap1, umap2, &domain_product_entry);
1549
595
}
1550
1551
static isl_stat range_product_entry(void **entry, void *user)
1552
878
{
1553
878
  struct isl_union_map_bin_data *data = user;
1554
878
  isl_map *map2 = *entry;
1555
878
1556
878
  if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1557
878
         map2->dim, isl_dim_in))
1558
528
    return isl_stat_ok;
1559
350
1560
350
  map2 = isl_map_range_product(isl_map_copy(data->map),
1561
350
             isl_map_copy(map2));
1562
350
1563
350
  data->res = isl_union_map_add_map(data->res, map2);
1564
350
1565
350
  return isl_stat_ok;
1566
350
}
1567
1568
__isl_give isl_union_map *isl_union_map_range_product(
1569
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1570
236
{
1571
236
  return bin_op(umap1, umap2, &range_product_entry);
1572
236
}
1573
1574
/* If data->map A -> B and "map2" C -> D have the same range space,
1575
 * then add (A, C) -> (B * D) to data->res.
1576
 */
1577
static isl_stat flat_domain_product_entry(void **entry, void *user)
1578
0
{
1579
0
  struct isl_union_map_bin_data *data = user;
1580
0
  isl_map *map2 = *entry;
1581
0
1582
0
  if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1583
0
         map2->dim, isl_dim_out))
1584
0
    return isl_stat_ok;
1585
0
1586
0
  map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1587
0
            isl_map_copy(map2));
1588
0
1589
0
  data->res = isl_union_map_add_map(data->res, map2);
1590
0
1591
0
  return isl_stat_ok;
1592
0
}
1593
1594
/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1595
 */
1596
__isl_give isl_union_map *isl_union_map_flat_domain_product(
1597
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1598
0
{
1599
0
  return bin_op(umap1, umap2, &flat_domain_product_entry);
1600
0
}
1601
1602
static isl_stat flat_range_product_entry(void **entry, void *user)
1603
104k
{
1604
104k
  struct isl_union_map_bin_data *data = user;
1605
104k
  isl_map *map2 = *entry;
1606
104k
1607
104k
  if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1608
104k
         map2->dim, isl_dim_in))
1609
88.5k
    return isl_stat_ok;
1610
15.7k
1611
15.7k
  map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1612
15.7k
            isl_map_copy(map2));
1613
15.7k
1614
15.7k
  data->res = isl_union_map_add_map(data->res, map2);
1615
15.7k
1616
15.7k
  return isl_stat_ok;
1617
15.7k
}
1618
1619
__isl_give isl_union_map *isl_union_map_flat_range_product(
1620
  __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1621
12.1k
{
1622
12.1k
  return bin_op(umap1, umap2, &flat_range_product_entry);
1623
12.1k
}
1624
1625
/* Data structure that specifies how un_op should modify
1626
 * the maps in the union map.
1627
 *
1628
 * If "inplace" is set, then the maps in the input union map
1629
 * are modified in place.  This means that "fn_map" should not
1630
 * change the meaning of the map or that the union map only
1631
 * has a single reference.
1632
 * If "total" is set, then all maps need to be modified and
1633
 * the results need to live in the same space.
1634
 * Otherwise, a new union map is constructed to store the results.
1635
 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1636
 * are taken into account.  "filter_user" is passed as the second argument
1637
 * to "filter".  No filter can be set if "inplace" or
1638
 * "total" is set.
1639
 * "fn_map" specifies how the maps (selected by "filter")
1640
 * should be transformed.
1641
 */
1642
struct isl_un_op_control {
1643
  int inplace;
1644
  int total;
1645
  isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1646
  void *filter_user;
1647
  __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1648
};
1649
1650
/* Data structure for wrapping the data for un_op_filter_drop_user.
1651
 * "filter" is the function that is being wrapped.
1652
 */
1653
struct isl_un_op_drop_user_data {
1654
  isl_bool (*filter)(__isl_keep isl_map *map);
1655
};
1656
1657
/* Wrapper for isl_un_op_control filters that do not require
1658
 * a second argument.
1659
 * Simply call data->filter without the second argument.
1660
 */
1661
static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1662
6.61k
{
1663
6.61k
  struct isl_un_op_drop_user_data *data = user;
1664
6.61k
  return data->filter(map);
1665
6.61k
}
1666
1667
/* Internal data structure for "un_op".
1668
 * "control" specifies how the maps in the union map should be modified.
1669
 * "res" collects the results.
1670
 */
1671
struct isl_union_map_un_data {
1672
  struct isl_un_op_control *control;
1673
  isl_union_map *res;
1674
};
1675
1676
/* isl_hash_table_foreach callback for un_op.
1677
 * Handle the map that "entry" points to.
1678
 *
1679
 * If control->filter is set, then check if this map satisfies the filter.
1680
 * If so (or if control->filter is not set), modify the map
1681
 * by calling control->fn_map and either add the result to data->res or
1682
 * replace the original entry by the result (if control->inplace is set).
1683
 */
1684
static isl_stat un_entry(void **entry, void *user)
1685
118k
{
1686
118k
  struct isl_union_map_un_data *data = user;
1687
118k
  isl_map *map = *entry;
1688
118k
1689
118k
  if (data->control->filter) {
1690
6.81k
    isl_bool ok;
1691
6.81k
1692
6.81k
    ok = data->control->filter(map, data->control->filter_user);
1693
6.81k
    if (ok < 0)
1694
0
      return isl_stat_error;
1695
6.81k
    if (!ok)
1696
23
      return isl_stat_ok;
1697
117k
  }
1698
117k
1699
117k
  map = data->control->fn_map(isl_map_copy(map));
1700
117k
  if (!map)
1701
21
    return isl_stat_error;
1702
117k
  if (data->control->inplace) {
1703
13.5k
    isl_map_free(*entry);
1704
13.5k
    *entry = map;
1705
104k
  } else {
1706
104k
    data->res = isl_union_map_add_map(data->res, map);
1707
104k
    if (!data->res)
1708
0
      return isl_stat_error;
1709
117k
  }
1710
117k
1711
117k
  return isl_stat_ok;
1712
117k
}
1713
1714
/* Modify the maps in "umap" based on "control".
1715
 * If control->inplace is set, then modify the maps in "umap" in-place.
1716
 * Otherwise, create a new union map to hold the results.
1717
 * If control->total is set, then perform an inplace computation
1718
 * if "umap" is only referenced once.  Otherwise, create a new union map
1719
 * to store the results.
1720
 */
1721
static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1722
  struct isl_un_op_control *control)
1723
87.2k
{
1724
87.2k
  struct isl_union_map_un_data data = { control };
1725
87.2k
1726
87.2k
  if (!umap)
1727
227
    return NULL;
1728
86.9k
  if ((control->inplace || 
control->total76.3k
) &&
control->filter11.1k
)
1729
86.9k
    
isl_die0
(isl_union_map_get_ctx(umap), isl_error_invalid,
1730
86.9k
      "inplace/total modification cannot be filtered",
1731
86.9k
      return isl_union_map_free(umap));
1732
86.9k
1733
86.9k
  if (control->total && 
umap->ref == 1520
)
1734
60
    control->inplace = 1;
1735
86.9k
  if (control->inplace) {
1736
10.7k
    data.res = umap;
1737
76.2k
  } else {
1738
76.2k
    isl_space *space;
1739
76.2k
1740
76.2k
    space = isl_union_map_get_space(umap);
1741
76.2k
    data.res = isl_union_map_alloc(space, umap->table.n);
1742
76.2k
  }
1743
86.9k
  if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1744
86.9k
            &umap->table, &un_entry, &data) < 0)
1745
21
    data.res = isl_union_map_free(data.res);
1746
86.9k
1747
86.9k
  if (control->inplace)
1748
10.7k
    return data.res;
1749
76.2k
  isl_union_map_free(umap);
1750
76.2k
  return data.res;
1751
76.2k
}
1752
1753
__isl_give isl_union_map *isl_union_map_from_range(
1754
  __isl_take isl_union_set *uset)
1755
3.75k
{
1756
3.75k
  struct isl_un_op_control control = {
1757
3.75k
    .fn_map = &isl_map_from_range,
1758
3.75k
  };
1759
3.75k
  return un_op(uset, &control);
1760
3.75k
}
1761
1762
__isl_give isl_union_map *isl_union_map_from_domain(
1763
  __isl_take isl_union_set *uset)
1764
3.27k
{
1765
3.27k
  return isl_union_map_reverse(isl_union_map_from_range(uset));
1766
3.27k
}
1767
1768
__isl_give isl_union_map *isl_union_map_from_domain_and_range(
1769
  __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1770
481
{
1771
481
  return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1772
481
                 isl_union_map_from_range(range));
1773
481
}
1774
1775
/* Modify the maps in "umap" by applying "fn" on them.
1776
 * "fn" should apply to all maps in "umap" and should not modify the space.
1777
 */
1778
static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1779
  __isl_give isl_map *(*fn)(__isl_take isl_map *))
1780
530
{
1781
530
  struct isl_un_op_control control = {
1782
530
    .total = 1,
1783
530
    .fn_map = fn,
1784
530
  };
1785
530
1786
530
  return un_op(umap, &control);
1787
530
}
1788
1789
/* Compute the affine hull of "map" and return the result as an isl_map.
1790
 */
1791
static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1792
8
{
1793
8
  return isl_map_from_basic_map(isl_map_affine_hull(map));
1794
8
}
1795
1796
__isl_give isl_union_map *isl_union_map_affine_hull(
1797
  __isl_take isl_union_map *umap)
1798
6
{
1799
6
  return total(umap, &isl_map_affine_hull_map);
1800
6
}
1801
1802
__isl_give isl_union_set *isl_union_set_affine_hull(
1803
  __isl_take isl_union_set *uset)
1804
6
{
1805
6
  return isl_union_map_affine_hull(uset);
1806
6
}
1807
1808
/* Wrapper around isl_set_combined_lineality_space
1809
 * that returns the combined lineality space in the form of an isl_set
1810
 * instead of an isl_basic_set.
1811
 */
1812
static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1813
20
{
1814
20
  return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1815
20
}
1816
1817
/* For each set in "uset", compute the (linear) hull
1818
 * of the lineality spaces of its basic sets and
1819
 * collect and return the results.
1820
 */
1821
__isl_give isl_union_set *isl_union_set_combined_lineality_space(
1822
  __isl_take isl_union_set *uset)
1823
28
{
1824
28
  struct isl_un_op_control control = {
1825
28
    .fn_map = &combined_lineality_space,
1826
28
  };
1827
28
  return un_op(uset, &control);
1828
28
}
1829
1830
/* Compute the polyhedral hull of "map" and return the result as an isl_map.
1831
 */
1832
static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1833
0
{
1834
0
  return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1835
0
}
1836
1837
__isl_give isl_union_map *isl_union_map_polyhedral_hull(
1838
  __isl_take isl_union_map *umap)
1839
0
{
1840
0
  return total(umap, &isl_map_polyhedral_hull_map);
1841
0
}
1842
1843
__isl_give isl_union_set *isl_union_set_polyhedral_hull(
1844
  __isl_take isl_union_set *uset)
1845
0
{
1846
0
  return isl_union_map_polyhedral_hull(uset);
1847
0
}
1848
1849
/* Compute a superset of the convex hull of "map" that is described
1850
 * by only translates of the constraints in the constituents of "map" and
1851
 * return the result as an isl_map.
1852
 */
1853
static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1854
0
{
1855
0
  return isl_map_from_basic_map(isl_map_simple_hull(map));
1856
0
}
1857
1858
__isl_give isl_union_map *isl_union_map_simple_hull(
1859
  __isl_take isl_union_map *umap)
1860
0
{
1861
0
  return total(umap, &isl_map_simple_hull_map);
1862
0
}
1863
1864
__isl_give isl_union_set *isl_union_set_simple_hull(
1865
  __isl_take isl_union_set *uset)
1866
0
{
1867
0
  return isl_union_map_simple_hull(uset);
1868
0
}
1869
1870
static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1871
  __isl_give isl_map *(*fn)(__isl_take isl_map *))
1872
10.7k
{
1873
10.7k
  struct isl_un_op_control control = {
1874
10.7k
    .inplace = 1,
1875
10.7k
    .fn_map = fn,
1876
10.7k
  };
1877
10.7k
1878
10.7k
  return un_op(umap, &control);
1879
10.7k
}
1880
1881
/* Remove redundant constraints in each of the basic maps of "umap".
1882
 * Since removing redundant constraints does not change the meaning
1883
 * or the space, the operation can be performed in-place.
1884
 */
1885
__isl_give isl_union_map *isl_union_map_remove_redundancies(
1886
  __isl_take isl_union_map *umap)
1887
9
{
1888
9
  return inplace(umap, &isl_map_remove_redundancies);
1889
9
}
1890
1891
/* Remove redundant constraints in each of the basic sets of "uset".
1892
 */
1893
__isl_give isl_union_set *isl_union_set_remove_redundancies(
1894
  __isl_take isl_union_set *uset)
1895
0
{
1896
0
  return isl_union_map_remove_redundancies(uset);
1897
0
}
1898
1899
__isl_give isl_union_map *isl_union_map_coalesce(
1900
  __isl_take isl_union_map *umap)
1901
8.22k
{
1902
8.22k
  return inplace(umap, &isl_map_coalesce);
1903
8.22k
}
1904
1905
__isl_give isl_union_set *isl_union_set_coalesce(
1906
  __isl_take isl_union_set *uset)
1907
612
{
1908
612
  return isl_union_map_coalesce(uset);
1909
612
}
1910
1911
__isl_give isl_union_map *isl_union_map_detect_equalities(
1912
  __isl_take isl_union_map *umap)
1913
1.79k
{
1914
1.79k
  return inplace(umap, &isl_map_detect_equalities);
1915
1.79k
}
1916
1917
__isl_give isl_union_set *isl_union_set_detect_equalities(
1918
  __isl_take isl_union_set *uset)
1919
84
{
1920
84
  return isl_union_map_detect_equalities(uset);
1921
84
}
1922
1923
__isl_give isl_union_map *isl_union_map_compute_divs(
1924
  __isl_take isl_union_map *umap)
1925
682
{
1926
682
  return inplace(umap, &isl_map_compute_divs);
1927
682
}
1928
1929
__isl_give isl_union_set *isl_union_set_compute_divs(
1930
  __isl_take isl_union_set *uset)
1931
84
{
1932
84
  return isl_union_map_compute_divs(uset);
1933
84
}
1934
1935
__isl_give isl_union_map *isl_union_map_lexmin(
1936
  __isl_take isl_union_map *umap)
1937
212
{
1938
212
  return total(umap, &isl_map_lexmin);
1939
212
}
1940
1941
__isl_give isl_union_set *isl_union_set_lexmin(
1942
  __isl_take isl_union_set *uset)
1943
0
{
1944
0
  return isl_union_map_lexmin(uset);
1945
0
}
1946
1947
__isl_give isl_union_map *isl_union_map_lexmax(
1948
  __isl_take isl_union_map *umap)
1949
252
{
1950
252
  return total(umap, &isl_map_lexmax);
1951
252
}
1952
1953
__isl_give isl_union_set *isl_union_set_lexmax(
1954
  __isl_take isl_union_set *uset)
1955
0
{
1956
0
  return isl_union_map_lexmax(uset);
1957
0
}
1958
1959
/* Return the universe in the space of "map".
1960
 */
1961
static __isl_give isl_map *universe(__isl_take isl_map *map)
1962
17.4k
{
1963
17.4k
  isl_space *space;
1964
17.4k
1965
17.4k
  space = isl_map_get_space(map);
1966
17.4k
  isl_map_free(map);
1967
17.4k
  return isl_map_universe(space);
1968
17.4k
}
1969
1970
__isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1971
13.4k
{
1972
13.4k
  struct isl_un_op_control control = {
1973
13.4k
    .fn_map = &universe,
1974
13.4k
  };
1975
13.4k
  return un_op(umap, &control);
1976
13.4k
}
1977
1978
__isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1979
11.3k
{
1980
11.3k
  return isl_union_map_universe(uset);
1981
11.3k
}
1982
1983
__isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1984
16.0k
{
1985
16.0k
  struct isl_un_op_control control = {
1986
16.0k
    .fn_map = &isl_map_reverse,
1987
16.0k
  };
1988
16.0k
  return un_op(umap, &control);
1989
16.0k
}
1990
1991
/* Compute the parameter domain of the given union map.
1992
 */
1993
__isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1994
10.6k
{
1995
10.6k
  struct isl_un_op_control control = {
1996
10.6k
    .fn_map = &isl_map_params,
1997
10.6k
  };
1998
10.6k
  int empty;
1999
10.6k
2000
10.6k
  empty = isl_union_map_is_empty(umap);
2001
10.6k
  if (empty < 0)
2002
8
    goto error;
2003
10.5k
  if (empty) {
2004
395
    isl_space *space;
2005
395
    space = isl_union_map_get_space(umap);
2006
395
    isl_union_map_free(umap);
2007
395
    return isl_set_empty(space);
2008
395
  }
2009
10.1k
  return isl_set_from_union_set(un_op(umap, &control));
2010
8
error:
2011
8
  isl_union_map_free(umap);
2012
8
  return NULL;
2013
10.1k
}
2014
2015
/* Compute the parameter domain of the given union set.
2016
 */
2017
__isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2018
8.73k
{
2019
8.73k
  return isl_union_map_params(uset);
2020
8.73k
}
2021
2022
__isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2023
16.6k
{
2024
16.6k
  struct isl_un_op_control control = {
2025
16.6k
    .fn_map = &isl_map_domain,
2026
16.6k
  };
2027
16.6k
  return un_op(umap, &control);
2028
16.6k
}
2029
2030
__isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2031
2.16k
{
2032
2.16k
  struct isl_un_op_control control = {
2033
2.16k
    .fn_map = &isl_map_range,
2034
2.16k
  };
2035
2.16k
  return un_op(umap, &control);
2036
2.16k
}
2037
2038
__isl_give isl_union_map *isl_union_map_domain_map(
2039
  __isl_take isl_union_map *umap)
2040
696
{
2041
696
  struct isl_un_op_control control = {
2042
696
    .fn_map = &isl_map_domain_map,
2043
696
  };
2044
696
  return un_op(umap, &control);
2045
696
}
2046
2047
/* Construct an isl_pw_multi_aff that maps "map" to its domain and
2048
 * add the result to "res".
2049
 */
2050
static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2051
380
{
2052
380
  isl_union_pw_multi_aff **res = user;
2053
380
  isl_multi_aff *ma;
2054
380
  isl_pw_multi_aff *pma;
2055
380
2056
380
  ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2057
380
  pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2058
380
  *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2059
380
2060
380
  return *res ? isl_stat_ok : 
isl_stat_error0
;
2061
380
2062
380
}
2063
2064
/* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2065
 * to its domain.
2066
 */
2067
__isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2068
  __isl_take isl_union_map *umap)
2069
147
{
2070
147
  isl_union_pw_multi_aff *res;
2071
147
2072
147
  res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2073
147
  if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2074
0
    res = isl_union_pw_multi_aff_free(res);
2075
147
2076
147
  isl_union_map_free(umap);
2077
147
  return res;
2078
147
}
2079
2080
__isl_give isl_union_map *isl_union_map_range_map(
2081
  __isl_take isl_union_map *umap)
2082
4.56k
{
2083
4.56k
  struct isl_un_op_control control = {
2084
4.56k
    .fn_map = &isl_map_range_map,
2085
4.56k
  };
2086
4.56k
  return un_op(umap, &control);
2087
4.56k
}
2088
2089
/* Given a collection of wrapped maps of the form A[B -> C],
2090
 * return the collection of maps A[B -> C] -> B.
2091
 */
2092
__isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2093
  __isl_take isl_union_set *uset)
2094
0
{
2095
0
  struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2096
0
  struct isl_un_op_control control = {
2097
0
    .filter = &un_op_filter_drop_user,
2098
0
    .filter_user = &data,
2099
0
    .fn_map = &isl_set_wrapped_domain_map,
2100
0
  };
2101
0
  return un_op(uset, &control);
2102
0
}
2103
2104
/* Does "map" relate elements from the same space?
2105
 */
2106
static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2107
201
{
2108
201
  return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2109
201
          map->dim, isl_dim_out);
2110
201
}
2111
2112
__isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2113
228
{
2114
228
  struct isl_un_op_control control = {
2115
228
    .filter = &equal_tuples,
2116
228
    .fn_map = &isl_map_deltas,
2117
228
  };
2118
228
  return un_op(umap, &control);
2119
228
}
2120
2121
__isl_give isl_union_map *isl_union_map_deltas_map(
2122
  __isl_take isl_union_map *umap)
2123
0
{
2124
0
  struct isl_un_op_control control = {
2125
0
    .filter = &equal_tuples,
2126
0
    .fn_map = &isl_map_deltas_map,
2127
0
  };
2128
0
  return un_op(umap, &control);
2129
0
}
2130
2131
__isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2132
108
{
2133
108
  struct isl_un_op_control control = {
2134
108
    .fn_map = &isl_set_identity,
2135
108
  };
2136
108
  return un_op(uset, &control);
2137
108
}
2138
2139
/* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2140
 */
2141
static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2142
21
{
2143
21
  isl_union_pw_multi_aff **res = user;
2144
21
  isl_space *space;
2145
21
  isl_pw_multi_aff *pma;
2146
21
2147
21
  space = isl_space_map_from_set(isl_set_get_space(set));
2148
21
  pma = isl_pw_multi_aff_identity(space);
2149
21
  pma = isl_pw_multi_aff_intersect_domain(pma, set);
2150
21
  *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2151
21
2152
21
  return *res ? isl_stat_ok : 
isl_stat_error0
;
2153
21
}
2154
2155
/* Return an identity function on "uset" in the form
2156
 * of an isl_union_pw_multi_aff.
2157
 */
2158
__isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2159
  __isl_take isl_union_set *uset)
2160
21
{
2161
21
  isl_union_pw_multi_aff *res;
2162
21
2163
21
  res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2164
21
  if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2165
0
    res = isl_union_pw_multi_aff_free(res);
2166
21
2167
21
  isl_union_set_free(uset);
2168
21
  return res;
2169
21
}
2170
2171
/* For each map in "umap" of the form [A -> B] -> C,
2172
 * construct the map A -> C and collect the results.
2173
 */
2174
__isl_give isl_union_map *isl_union_map_domain_factor_domain(
2175
  __isl_take isl_union_map *umap)
2176
735
{
2177
735
  struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2178
735
  struct isl_un_op_control control = {
2179
735
    .filter = &un_op_filter_drop_user,
2180
735
    .filter_user = &data,
2181
735
    .fn_map = &isl_map_domain_factor_domain,
2182
735
  };
2183
735
  return un_op(umap, &control);
2184
735
}
2185
2186
/* For each map in "umap" of the form [A -> B] -> C,
2187
 * construct the map B -> C and collect the results.
2188
 */
2189
__isl_give isl_union_map *isl_union_map_domain_factor_range(
2190
  __isl_take isl_union_map *umap)
2191
202
{
2192
202
  struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2193
202
  struct isl_un_op_control control = {
2194
202
    .filter = &un_op_filter_drop_user,
2195
202
    .filter_user = &data,
2196
202
    .fn_map = &isl_map_domain_factor_range,
2197
202
  };
2198
202
  return un_op(umap, &control);
2199
202
}
2200
2201
/* For each map in "umap" of the form A -> [B -> C],
2202
 * construct the map A -> B and collect the results.
2203
 */
2204
__isl_give isl_union_map *isl_union_map_range_factor_domain(
2205
  __isl_take isl_union_map *umap)
2206
2.47k
{
2207
2.47k
  struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2208
2.47k
  struct isl_un_op_control control = {
2209
2.47k
    .filter = &un_op_filter_drop_user,
2210
2.47k
    .filter_user = &data,
2211
2.47k
    .fn_map = &isl_map_range_factor_domain,
2212
2.47k
  };
2213
2.47k
  return un_op(umap, &control);
2214
2.47k
}
2215
2216
/* For each map in "umap" of the form A -> [B -> C],
2217
 * construct the map A -> C and collect the results.
2218
 */
2219
__isl_give isl_union_map *isl_union_map_range_factor_range(
2220
  __isl_take isl_union_map *umap)
2221
597
{
2222
597
  struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2223
597
  struct isl_un_op_control control = {
2224
597
    .filter = &un_op_filter_drop_user,
2225
597
    .filter_user = &data,
2226
597
    .fn_map = &isl_map_range_factor_range,
2227
597
  };
2228
597
  return un_op(umap, &control);
2229
597
}
2230
2231
/* For each map in "umap" of the form [A -> B] -> [C -> D],
2232
 * construct the map A -> C and collect the results.
2233
 */
2234
__isl_give isl_union_map *isl_union_map_factor_domain(
2235
  __isl_take isl_union_map *umap)
2236
0
{
2237
0
  struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2238
0
  struct isl_un_op_control control = {
2239
0
    .filter = &un_op_filter_drop_user,
2240
0
    .filter_user = &data,
2241
0
    .fn_map = &isl_map_factor_domain,
2242
0
  };
2243
0
  return un_op(umap, &control);
2244
0
}
2245
2246
/* For each map in "umap" of the form [A -> B] -> [C -> D],
2247
 * construct the map B -> D and collect the results.
2248
 */
2249
__isl_give isl_union_map *isl_union_map_factor_range(
2250
  __isl_take isl_union_map *umap)
2251
162
{
2252
162
  struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2253
162
  struct isl_un_op_control control = {
2254
162
    .filter = &un_op_filter_drop_user,
2255
162
    .filter_user = &data,
2256
162
    .fn_map = &isl_map_factor_range,
2257
162
  };
2258
162
  return un_op(umap, &control);
2259
162
}
2260
2261
__isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2262
1.05k
{
2263
1.05k
  struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2264
1.05k
  struct isl_un_op_control control = {
2265
1.05k
    .filter = &un_op_filter_drop_user,
2266
1.05k
    .filter_user = &data,
2267
1.05k
    .fn_map = &isl_set_unwrap,
2268
1.05k
  };
2269
1.05k
  return un_op(uset, &control);
2270
1.05k
}
2271
2272
__isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2273
792
{
2274
792
  struct isl_un_op_control control = {
2275
792
    .fn_map = &isl_map_wrap,
2276
792
  };
2277
792
  return un_op(umap, &control);
2278
792
}
2279
2280
struct isl_union_map_is_subset_data {
2281
  isl_union_map *umap2;
2282
  isl_bool is_subset;
2283
};
2284
2285
static isl_stat is_subset_entry(void **entry, void *user)
2286
11.5k
{
2287
11.5k
  struct isl_union_map_is_subset_data *data = user;
2288
11.5k
  uint32_t hash;
2289
11.5k
  struct isl_hash_table_entry *entry2;
2290
11.5k
  isl_map *map = *entry;
2291
11.5k
2292
11.5k
  hash = isl_space_get_hash(map->dim);
2293
11.5k
  entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2294
11.5k
             hash, &has_space, map->dim, 0);
2295
11.5k
  if (!entry2) {
2296
1.45k
    int empty = isl_map_is_empty(map);
2297
1.45k
    if (empty < 0)
2298
0
      return isl_stat_error;
2299
1.45k
    if (empty)
2300
0
      return isl_stat_ok;
2301
1.45k
    data->is_subset = 0;
2302
1.45k
    return isl_stat_error;
2303
1.45k
  }
2304
10.1k
2305
10.1k
  data->is_subset = isl_map_is_subset(map, entry2->data);
2306
10.1k
  if (data->is_subset < 0 || !data->is_subset)
2307
855
    return isl_stat_error;
2308
9.25k
2309
9.25k
  return isl_stat_ok;
2310
9.25k
}
2311
2312
isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2313
  __isl_keep isl_union_map *umap2)
2314
6.94k
{
2315
6.94k
  struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2316
6.94k
2317
6.94k
  umap1 = isl_union_map_copy(umap1);
2318
6.94k
  umap2 = isl_union_map_copy(umap2);
2319
6.94k
  umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2320
6.94k
  umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2321
6.94k
2322
6.94k
  if (!umap1 || !umap2)
2323
0
    goto error;
2324
6.94k
2325
6.94k
  data.umap2 = umap2;
2326
6.94k
  if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2327
6.94k
           &is_subset_entry, &data) < 0 &&
2328
6.94k
      
data.is_subset2.31k
)
2329
0
    goto error;
2330
6.94k
2331
6.94k
  isl_union_map_free(umap1);
2332
6.94k
  isl_union_map_free(umap2);
2333
6.94k
2334
6.94k
  return data.is_subset;
2335
0
error:
2336
0
  isl_union_map_free(umap1);
2337
0
  isl_union_map_free(umap2);
2338
0
  return isl_bool_error;
2339
6.94k
}
2340
2341
isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2342
  __isl_keep isl_union_set *uset2)
2343
5.54k
{
2344
5.54k
  return isl_union_map_is_subset(uset1, uset2);
2345
5.54k
}
2346
2347
isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2348
  __isl_keep isl_union_map *umap2)
2349
302
{
2350
302
  isl_bool is_subset;
2351
302
2352
302
  if (!umap1 || !umap2)
2353
0
    return isl_bool_error;
2354
302
  is_subset = isl_union_map_is_subset(umap1, umap2);
2355
302
  if (is_subset != isl_bool_true)
2356
31
    return is_subset;
2357
271
  is_subset = isl_union_map_is_subset(umap2, umap1);
2358
271
  return is_subset;
2359
271
}
2360
2361
isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2362
  __isl_keep isl_union_set *uset2)
2363
61
{
2364
61
  return isl_union_map_is_equal(uset1, uset2);
2365
61
}
2366
2367
isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2368
  __isl_keep isl_union_map *umap2)
2369
0
{
2370
0
  isl_bool is_subset;
2371
0
2372
0
  if (!umap1 || !umap2)
2373
0
    return isl_bool_error;
2374
0
  is_subset = isl_union_map_is_subset(umap1, umap2);
2375
0
  if (is_subset != isl_bool_true)
2376
0
    return is_subset;
2377
0
  is_subset = isl_union_map_is_subset(umap2, umap1);
2378
0
  if (is_subset == isl_bool_error)
2379
0
    return is_subset;
2380
0
  return !is_subset;
2381
0
}
2382
2383
isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2384
  __isl_keep isl_union_set *uset2)
2385
0
{
2386
0
  return isl_union_map_is_strict_subset(uset1, uset2);
2387
0
}
2388
2389
/* Internal data structure for isl_union_map_is_disjoint.
2390
 * umap2 is the union map with which we are comparing.
2391
 * is_disjoint is initialized to 1 and is set to 0 as soon
2392
 * as the union maps turn out not to be disjoint.
2393
 */
2394
struct isl_union_map_is_disjoint_data {
2395
  isl_union_map *umap2;
2396
  isl_bool is_disjoint;
2397
};
2398
2399
/* Check if "map" is disjoint from data->umap2 and abort
2400
 * the search if it is not.
2401
 */
2402
static isl_stat is_disjoint_entry(void **entry, void *user)
2403
3.70k
{
2404
3.70k
  struct isl_union_map_is_disjoint_data *data = user;
2405
3.70k
  uint32_t hash;
2406
3.70k
  struct isl_hash_table_entry *entry2;
2407
3.70k
  isl_map *map = *entry;
2408
3.70k
2409
3.70k
  hash = isl_space_get_hash(map->dim);
2410
3.70k
  entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2411
3.70k
             hash, &has_space, map->dim, 0);
2412
3.70k
  if (!entry2)
2413
3.69k
    return isl_stat_ok;
2414
12
2415
12
  data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2416
12
  if (data->is_disjoint < 0 || !data->is_disjoint)
2417
10
    return isl_stat_error;
2418
2
2419
2
  return isl_stat_ok;
2420
2
}
2421
2422
/* Are "umap1" and "umap2" disjoint?
2423
 */
2424
isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2425
  __isl_keep isl_union_map *umap2)
2426
1.44k
{
2427
1.44k
  struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2428
1.44k
2429
1.44k
  umap1 = isl_union_map_copy(umap1);
2430
1.44k
  umap2 = isl_union_map_copy(umap2);
2431
1.44k
  umap1 = isl_union_map_align_params(umap1,
2432
1.44k
            isl_union_map_get_space(umap2));
2433
1.44k
  umap2 = isl_union_map_align_params(umap2,
2434
1.44k
            isl_union_map_get_space(umap1));
2435
1.44k
2436
1.44k
  if (!umap1 || !umap2)
2437
0
    goto error;
2438
1.44k
2439
1.44k
  data.umap2 = umap2;
2440
1.44k
  if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2441
1.44k
           &is_disjoint_entry, &data) < 0 &&
2442
1.44k
      
data.is_disjoint10
)
2443
0
    goto error;
2444
1.44k
2445
1.44k
  isl_union_map_free(umap1);
2446
1.44k
  isl_union_map_free(umap2);
2447
1.44k
2448
1.44k
  return data.is_disjoint;
2449
0
error:
2450
0
  isl_union_map_free(umap1);
2451
0
  isl_union_map_free(umap2);
2452
0
  return isl_bool_error;
2453
1.44k
}
2454
2455
/* Are "uset1" and "uset2" disjoint?
2456
 */
2457
isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2458
  __isl_keep isl_union_set *uset2)
2459
1.23k
{
2460
1.23k
  return isl_union_map_is_disjoint(uset1, uset2);
2461
1.23k
}
2462
2463
static isl_stat sample_entry(void **entry, void *user)
2464
0
{
2465
0
  isl_basic_map **sample = (isl_basic_map **)user;
2466
0
  isl_map *map = *entry;
2467
0
2468
0
  *sample = isl_map_sample(isl_map_copy(map));
2469
0
  if (!*sample)
2470
0
    return isl_stat_error;
2471
0
  if (!isl_basic_map_plain_is_empty(*sample))
2472
0
    return isl_stat_error;
2473
0
  return isl_stat_ok;
2474
0
}
2475
2476
__isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2477
0
{
2478
0
  isl_basic_map *sample = NULL;
2479
0
2480
0
  if (!umap)
2481
0
    return NULL;
2482
0
2483
0
  if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2484
0
           &sample_entry, &sample) < 0 &&
2485
0
      !sample)
2486
0
    goto error;
2487
0
2488
0
  if (!sample)
2489
0
    sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2490
0
2491
0
  isl_union_map_free(umap);
2492
0
2493
0
  return sample;
2494
0
error:
2495
0
  isl_union_map_free(umap);
2496
0
  return NULL;
2497
0
}
2498
2499
__isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2500
0
{
2501
0
  return bset_from_bmap(isl_union_map_sample(uset));
2502
0
}
2503
2504
/* Return an element in "uset" in the form of an isl_point.
2505
 * Return a void isl_point if "uset" is empty.
2506
 */
2507
__isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2508
0
{
2509
0
  return isl_basic_set_sample_point(isl_union_set_sample(uset));
2510
0
}
2511
2512
struct isl_forall_data {
2513
  isl_bool res;
2514
  isl_bool (*fn)(__isl_keep isl_map *map);
2515
};
2516
2517
static isl_stat forall_entry(void **entry, void *user)
2518
30.9k
{
2519
30.9k
  struct isl_forall_data *data = user;
2520
30.9k
  isl_map *map = *entry;
2521
30.9k
2522
30.9k
  data->res = data->fn(map);
2523
30.9k
  if (data->res < 0)
2524
0
    return isl_stat_error;
2525
30.9k
2526
30.9k
  if (!data->res)
2527
30.8k
    return isl_stat_error;
2528
22
2529
22
  return isl_stat_ok;
2530
22
}
2531
2532
static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2533
  isl_bool (*fn)(__isl_keep isl_map *map))
2534
37.9k
{
2535
37.9k
  struct isl_forall_data data = { isl_bool_true, fn };
2536
37.9k
2537
37.9k
  if (!umap)
2538
9
    return isl_bool_error;
2539
37.9k
2540
37.9k
  if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2541
37.9k
           &forall_entry, &data) < 0 && 
data.res30.8k
)
2542
0
    return isl_bool_error;
2543
37.9k
2544
37.9k
  return data.res;
2545
37.9k
}
2546
2547
struct isl_forall_user_data {
2548
  isl_bool res;
2549
  isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2550
  void *user;
2551
};
2552
2553
static isl_stat forall_user_entry(void **entry, void *user)
2554
35
{
2555
35
  struct isl_forall_user_data *data = user;
2556
35
  isl_map *map = *entry;
2557
35
2558
35
  data->res = data->fn(map, data->user);
2559
35
  if (data->res < 0)
2560
0
    return isl_stat_error;
2561
35
2562
35
  if (!data->res)
2563
5
    return isl_stat_error;
2564
30
2565
30
  return isl_stat_ok;
2566
30
}
2567
2568
/* Check if fn(map, user) returns true for all maps "map" in umap.
2569
 */
2570
static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2571
  isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2572
21
{
2573
21
  struct isl_forall_user_data data = { isl_bool_true, fn, user };
2574
21
2575
21
  if (!umap)
2576
0
    return isl_bool_error;
2577
21
2578
21
  if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2579
21
           &forall_user_entry, &data) < 0 && 
data.res5
)
2580
0
    return isl_bool_error;
2581
21
2582
21
  return data.res;
2583
21
}
2584
2585
/* Is "umap" obviously empty?
2586
 */
2587
isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2588
0
{
2589
0
  if (!umap)
2590
0
    return isl_bool_error;
2591
0
  return isl_union_map_n_map(umap) == 0;
2592
0
}
2593
2594
isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2595
37.9k
{
2596
37.9k
  return union_map_forall(umap, &isl_map_is_empty);
2597
37.9k
}
2598
2599
isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2600
2.51k
{
2601
2.51k
  return isl_union_map_is_empty(uset);
2602
2.51k
}
2603
2604
static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2605
3
{
2606
3
  isl_bool is_subset;
2607
3
  isl_space *dim;
2608
3
  isl_map *id;
2609
3
2610
3
  if (!map)
2611
0
    return isl_bool_error;
2612
3
2613
3
  if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2614
3
          map->dim, isl_dim_out))
2615
1
    return isl_bool_false;
2616
2
2617
2
  dim = isl_map_get_space(map);
2618
2
  id = isl_map_identity(dim);
2619
2
2620
2
  is_subset = isl_map_is_subset(map, id);
2621
2
2622
2
  isl_map_free(id);
2623
2
2624
2
  return is_subset;
2625
2
}
2626
2627
/* Given an isl_union_map that consists of a single map, check
2628
 * if it is single-valued.
2629
 */
2630
static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2631
57
{
2632
57
  isl_map *map;
2633
57
  isl_bool sv;
2634
57
2635
57
  umap = isl_union_map_copy(umap);
2636
57
  map = isl_map_from_union_map(umap);
2637
57
  sv = isl_map_is_single_valued(map);
2638
57
  isl_map_free(map);
2639
57
2640
57
  return sv;
2641
57
}
2642
2643
/* Internal data structure for single_valued_on_domain.
2644
 *
2645
 * "umap" is the union map to be tested.
2646
 * "sv" is set to 1 as long as "umap" may still be single-valued.
2647
 */
2648
struct isl_union_map_is_sv_data {
2649
  isl_union_map *umap;
2650
  isl_bool sv;
2651
};
2652
2653
/* Check if the data->umap is single-valued on "set".
2654
 *
2655
 * If data->umap consists of a single map on "set", then test it
2656
 * as an isl_map.
2657
 *
2658
 * Otherwise, compute
2659
 *
2660
 *  M \circ M^-1
2661
 *
2662
 * check if the result is a subset of the identity mapping and
2663
 * store the result in data->sv.
2664
 *
2665
 * Terminate as soon as data->umap has been determined not to
2666
 * be single-valued.
2667
 */
2668
static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2669
11
{
2670
11
  struct isl_union_map_is_sv_data *data = user;
2671
11
  isl_union_map *umap, *test;
2672
11
2673
11
  umap = isl_union_map_copy(data->umap);
2674
11
  umap = isl_union_map_intersect_domain(umap,
2675
11
            isl_union_set_from_set(set));
2676
11
2677
11
  if (isl_union_map_n_map(umap) == 1) {
2678
9
    data->sv = single_map_is_single_valued(umap);
2679
9
    isl_union_map_free(umap);
2680
9
  } else {
2681
2
    test = isl_union_map_reverse(isl_union_map_copy(umap));
2682
2
    test = isl_union_map_apply_range(test, umap);
2683
2
2684
2
    data->sv = union_map_forall(test, &is_subset_of_identity);
2685
2
2686
2
    isl_union_map_free(test);
2687
2
  }
2688
11
2689
11
  if (data->sv < 0 || !data->sv)
2690
3
    return isl_stat_error;
2691
8
  return isl_stat_ok;
2692
8
}
2693
2694
/* Check if the given map is single-valued.
2695
 *
2696
 * If the union map consists of a single map, then test it as an isl_map.
2697
 * Otherwise, check if the union map is single-valued on each of its
2698
 * domain spaces.
2699
 */
2700
isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2701
54
{
2702
54
  isl_union_map *universe;
2703
54
  isl_union_set *domain;
2704
54
  struct isl_union_map_is_sv_data data;
2705
54
2706
54
  if (isl_union_map_n_map(umap) == 1)
2707
48
    return single_map_is_single_valued(umap);
2708
6
2709
6
  universe = isl_union_map_universe(isl_union_map_copy(umap));
2710
6
  domain = isl_union_map_domain(universe);
2711
6
2712
6
  data.sv = isl_bool_true;
2713
6
  data.umap = umap;
2714
6
  if (isl_union_set_foreach_set(domain,
2715
6
          &single_valued_on_domain, &data) < 0 && 
data.sv3
)
2716
0
    data.sv = isl_bool_error;
2717
6
  isl_union_set_free(domain);
2718
6
2719
6
  return data.sv;
2720
6
}
2721
2722
isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2723
0
{
2724
0
  isl_bool in;
2725
0
2726
0
  umap = isl_union_map_copy(umap);
2727
0
  umap = isl_union_map_reverse(umap);
2728
0
  in = isl_union_map_is_single_valued(umap);
2729
0
  isl_union_map_free(umap);
2730
0
2731
0
  return in;
2732
0
}
2733
2734
/* Is "map" obviously not an identity relation because
2735
 * it maps elements from one space to another space?
2736
 * Update *non_identity accordingly.
2737
 *
2738
 * In particular, if the domain and range spaces are the same,
2739
 * then the map is not considered to obviously not be an identity relation.
2740
 * Otherwise, the map is considered to obviously not be an identity relation
2741
 * if it is is non-empty.
2742
 *
2743
 * If "map" is determined to obviously not be an identity relation,
2744
 * then the search is aborted.
2745
 */
2746
static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2747
0
{
2748
0
  isl_bool *non_identity = user;
2749
0
  isl_bool equal;
2750
0
  isl_space *space;
2751
0
2752
0
  space = isl_map_get_space(map);
2753
0
  equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2754
0
  if (equal >= 0 && !equal)
2755
0
    *non_identity = isl_bool_not(isl_map_is_empty(map));
2756
0
  else
2757
0
    *non_identity = isl_bool_not(equal);
2758
0
  isl_space_free(space);
2759
0
  isl_map_free(map);
2760
0
2761
0
  if (*non_identity < 0 || *non_identity)
2762
0
    return isl_stat_error;
2763
0
2764
0
  return isl_stat_ok;
2765
0
}
2766
2767
/* Is "umap" obviously not an identity relation because
2768
 * it maps elements from one space to another space?
2769
 *
2770
 * As soon as a map has been found that maps elements to a different space,
2771
 * non_identity is changed and the search is aborted.
2772
 */
2773
static isl_bool isl_union_map_plain_is_not_identity(
2774
  __isl_keep isl_union_map *umap)
2775
0
{
2776
0
  isl_bool non_identity;
2777
0
2778
0
  non_identity = isl_bool_false;
2779
0
  if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2780
0
          &non_identity) < 0 &&
2781
0
      non_identity == isl_bool_false)
2782
0
    return isl_bool_error;
2783
0
2784
0
  return non_identity;
2785
0
}
2786
2787
/* Does "map" only map elements to themselves?
2788
 * Update *identity accordingly.
2789
 *
2790
 * If "map" is determined not to be an identity relation,
2791
 * then the search is aborted.
2792
 */
2793
static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2794
0
{
2795
0
  isl_bool *identity = user;
2796
0
2797
0
  *identity = isl_map_is_identity(map);
2798
0
  isl_map_free(map);
2799
0
2800
0
  if (*identity < 0 || !*identity)
2801
0
    return isl_stat_error;
2802
0
2803
0
  return isl_stat_ok;
2804
0
}
2805
2806
/* Does "umap" only map elements to themselves?
2807
 *
2808
 * First check if there are any maps that map elements to different spaces.
2809
 * If not, then check that all the maps (between identical spaces)
2810
 * are identity relations.
2811
 */
2812
isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2813
0
{
2814
0
  isl_bool non_identity;
2815
0
  isl_bool identity;
2816
0
2817
0
  non_identity = isl_union_map_plain_is_not_identity(umap);
2818
0
  if (non_identity < 0 || non_identity)
2819
0
    return isl_bool_not(non_identity);
2820
0
2821
0
  identity = isl_bool_true;
2822
0
  if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2823
0
      identity == isl_bool_true)
2824
0
    return isl_bool_error;
2825
0
2826
0
  return identity;
2827
0
}
2828
2829
/* Represents a map that has a fixed value (v) for one of its
2830
 * range dimensions.
2831
 * The map in this structure is not reference counted, so it
2832
 * is only valid while the isl_union_map from which it was
2833
 * obtained is still alive.
2834
 */
2835
struct isl_fixed_map {
2836
  isl_int v;
2837
  isl_map *map;
2838
};
2839
2840
static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2841
  int n)
2842
11
{
2843
11
  int i;
2844
11
  struct isl_fixed_map *v;
2845
11
2846
11
  v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2847
11
  if (!v)
2848
0
    return NULL;
2849
36
  
for (i = 0; 11
i < n;
++i25
)
2850
25
    isl_int_init(v[i].v);
2851
11
  return v;
2852
11
}
2853
2854
static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2855
11
{
2856
11
  int i;
2857
11
2858
11
  if (!v)
2859
0
    return;
2860
36
  
for (i = 0; 11
i < n;
++i25
)
2861
25
    isl_int_clear(v[i].v);
2862
11
  free(v);
2863
11
}
2864
2865
/* Compare the "v" field of two isl_fixed_map structs.
2866
 */
2867
static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2868
14
{
2869
14
  const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2870
14
  const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2871
14
2872
14
  return isl_int_cmp(e1->v, e2->v);
2873
14
}
2874
2875
/* Internal data structure used while checking whether all maps
2876
 * in a union_map have a fixed value for a given output dimension.
2877
 * v is the list of maps, with the fixed value for the dimension
2878
 * n is the number of maps considered so far
2879
 * pos is the output dimension under investigation
2880
 */
2881
struct isl_fixed_dim_data {
2882
  struct isl_fixed_map *v;
2883
  int n;
2884
  int pos;
2885
};
2886
2887
static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2888
25
{
2889
25
  struct isl_fixed_dim_data *data = user;
2890
25
2891
25
  data->v[data->n].map = map;
2892
25
  return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2893
25
              &data->v[data->n++].v);
2894
25
}
2895
2896
static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2897
  int first, int n_range);
2898
2899
/* Given a list of the maps, with their fixed values at output dimension "pos",
2900
 * check whether the ranges of the maps form an obvious partition.
2901
 *
2902
 * We first sort the maps according to their fixed values.
2903
 * If all maps have a different value, then we know the ranges form
2904
 * a partition.
2905
 * Otherwise, we collect the maps with the same fixed value and
2906
 * check whether each such collection is obviously injective
2907
 * based on later dimensions.
2908
 */
2909
static int separates(struct isl_fixed_map *v, int n,
2910
  __isl_take isl_space *dim, int pos, int n_range)
2911
10
{
2912
10
  int i;
2913
10
2914
10
  if (!v)
2915
0
    goto error;
2916
10
2917
10
  qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2918
10
2919
17
  for (i = 0; i + 1 < n; 
++i7
) {
2920
10
    int j, k;
2921
10
    isl_union_map *part;
2922
10
    int injective;
2923
10
2924
16
    for (j = i + 1; j < n; 
++j6
)
2925
13
      if (isl_int_ne(v[i].v, v[j].v))
2926
13
        
break7
;
2927
10
2928
10
    if (j == i + 1)
2929
5
      continue;
2930
5
2931
5
    part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2932
16
    for (k = i; k < j; 
++k11
)
2933
11
      part = isl_union_map_add_map(part,
2934
11
                 isl_map_copy(v[k].map));
2935
5
2936
5
    injective = plain_injective_on_range(part, pos + 1, n_range);
2937
5
    if (injective < 0)
2938
0
      goto error;
2939
5
    if (!injective)
2940
3
      break;
2941
2
2942
2
    i = j - 1;
2943
2
  }
2944
10
2945
10
  isl_space_free(dim);
2946
10
  free_isl_fixed_map_array(v, n);
2947
10
  return i + 1 >= n;
2948
0
error:
2949
0
  isl_space_free(dim);
2950
0
  free_isl_fixed_map_array(v, n);
2951
0
  return -1;
2952
10
}
2953
2954
/* Check whether the maps in umap have obviously distinct ranges.
2955
 * In particular, check for an output dimension in the range
2956
 * [first,n_range) for which all maps have a fixed value
2957
 * and then check if these values, possibly along with fixed values
2958
 * at later dimensions, entail distinct ranges.
2959
 */
2960
static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2961
  int first, int n_range)
2962
15
{
2963
15
  isl_ctx *ctx;
2964
15
  int n;
2965
15
  struct isl_fixed_dim_data data = { NULL };
2966
15
2967
15
  ctx = isl_union_map_get_ctx(umap);
2968
15
2969
15
  n = isl_union_map_n_map(umap);
2970
15
  if (!umap)
2971
0
    goto error;
2972
15
2973
15
  if (n <= 1) {
2974
2
    isl_union_map_free(umap);
2975
2
    return isl_bool_true;
2976
2
  }
2977
13
2978
13
  if (first >= n_range) {
2979
2
    isl_union_map_free(umap);
2980
2
    return isl_bool_false;
2981
2
  }
2982
11
2983
11
  data.v = alloc_isl_fixed_map_array(ctx, n);
2984
11
  if (!data.v)
2985
0
    goto error;
2986
11
2987
13
  
for (data.pos = first; 11
data.pos < n_range;
++data.pos2
) {
2988
12
    isl_bool fixed;
2989
12
    int injective;
2990
12
    isl_space *dim;
2991
12
2992
12
    data.n = 0;
2993
12
    fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2994
12
    if (fixed < 0)
2995
0
      goto error;
2996
12
    if (!fixed)
2997
2
      continue;
2998
10
    dim = isl_union_map_get_space(umap);
2999
10
    injective = separates(data.v, n, dim, data.pos, n_range);
3000
10
    isl_union_map_free(umap);
3001
10
    return injective;
3002
10
  }
3003
11
3004
11
  free_isl_fixed_map_array(data.v, n);
3005
1
  isl_union_map_free(umap);
3006
1
3007
1
  return isl_bool_false;
3008
0
error:
3009
0
  free_isl_fixed_map_array(data.v, n);
3010
0
  isl_union_map_free(umap);
3011
0
  return isl_bool_error;
3012
11
}
3013
3014
/* Check whether the maps in umap that map to subsets of "ran"
3015
 * have obviously distinct ranges.
3016
 */
3017
static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3018
  void *user)
3019
10
{
3020
10
  isl_union_map *umap = user;
3021
10
3022
10
  umap = isl_union_map_copy(umap);
3023
10
  umap = isl_union_map_intersect_range(umap,
3024
10
      isl_union_set_from_set(isl_set_copy(ran)));
3025
10
  return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
3026
10
}
3027
3028
/* Check if the given union_map is obviously injective.
3029
 *
3030
 * In particular, we first check if all individual maps are obviously
3031
 * injective and then check if all the ranges of these maps are
3032
 * obviously disjoint.
3033
 */
3034
isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3035
10
{
3036
10
  isl_bool in;
3037
10
  isl_union_map *univ;
3038
10
  isl_union_set *ran;
3039
10
3040
10
  in = union_map_forall(umap, &isl_map_plain_is_injective);
3041
10
  if (in < 0)
3042
0
    return isl_bool_error;
3043
10
  if (!in)
3044
1
    return isl_bool_false;
3045
9
3046
9
  univ = isl_union_map_universe(isl_union_map_copy(umap));
3047
9
  ran = isl_union_map_range(univ);
3048
9
3049
9
  in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3050
9
3051
9
  isl_union_set_free(ran);
3052
9
3053
9
  return in;
3054
9
}
3055
3056
isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3057
0
{
3058
0
  isl_bool sv;
3059
0
3060
0
  sv = isl_union_map_is_single_valued(umap);
3061
0
  if (sv < 0 || !sv)
3062
0
    return sv;
3063
0
3064
0
  return isl_union_map_is_injective(umap);
3065
0
}
3066
3067
__isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3068
971
{
3069
971
  struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3070
971
  struct isl_un_op_control control = {
3071
971
    .filter = &un_op_filter_drop_user,
3072
971
    .filter_user = &data,
3073
971
    .fn_map = &isl_map_zip,
3074
971
  };
3075
971
  return un_op(umap, &control);
3076
971
}
3077
3078
/* Given a union map, take the maps of the form A -> (B -> C) and
3079
 * return the union of the corresponding maps (A -> B) -> C.
3080
 */
3081
__isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3082
580
{
3083
580
  struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3084
580
  struct isl_un_op_control control = {
3085
580
    .filter = &un_op_filter_drop_user,
3086
580
    .filter_user = &data,
3087
580
    .fn_map = &isl_map_uncurry,
3088
580
  };
3089
580
  return un_op(umap, &control);
3090
580
}
3091
3092
/* Given a union map, take the maps of the form (A -> B) -> C and
3093
 * return the union of the corresponding maps A -> (B -> C).
3094
 */
3095
__isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3096
415
{
3097
415
  struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3098
415
  struct isl_un_op_control control = {
3099
415
    .filter = &un_op_filter_drop_user,
3100
415
    .filter_user = &data,
3101
415
    .fn_map = &isl_map_curry,
3102
415
  };
3103
415
  return un_op(umap, &control);
3104
415
}
3105
3106
/* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3107
 * return the union of the corresponding maps A -> (B -> (C -> D)).
3108
 */
3109
__isl_give isl_union_map *isl_union_map_range_curry(
3110
  __isl_take isl_union_map *umap)
3111
162
{
3112
162
  struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3113
162
  struct isl_un_op_control control = {
3114
162
    .filter = &un_op_filter_drop_user,
3115
162
    .filter_user = &data,
3116
162
    .fn_map = &isl_map_range_curry,
3117
162
  };
3118
162
  return un_op(umap, &control);
3119
162
}
3120
3121
__isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3122
0
{
3123
0
  struct isl_un_op_control control = {
3124
0
    .fn_map = &isl_set_lift,
3125
0
  };
3126
0
  return un_op(uset, &control);
3127
0
}
3128
3129
static isl_stat coefficients_entry(void **entry, void *user)
3130
0
{
3131
0
  isl_set *set = *entry;
3132
0
  isl_union_set **res = user;
3133
0
3134
0
  set = isl_set_copy(set);
3135
0
  set = isl_set_from_basic_set(isl_set_coefficients(set));
3136
0
  *res = isl_union_set_add_set(*res, set);
3137
0
3138
0
  return isl_stat_ok;
3139
0
}
3140
3141
__isl_give isl_union_set *isl_union_set_coefficients(
3142
  __isl_take isl_union_set *uset)
3143
0
{
3144
0
  isl_ctx *ctx;
3145
0
  isl_space *dim;
3146
0
  isl_union_set *res;
3147
0
3148
0
  if (!uset)
3149
0
    return NULL;
3150
0
3151
0
  ctx = isl_union_set_get_ctx(uset);
3152
0
  dim = isl_space_set_alloc(ctx, 0, 0);
3153
0
  res = isl_union_map_alloc(dim, uset->table.n);
3154
0
  if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3155
0
           &coefficients_entry, &res) < 0)
3156
0
    goto error;
3157
0
3158
0
  isl_union_set_free(uset);
3159
0
  return res;
3160
0
error:
3161
0
  isl_union_set_free(uset);
3162
0
  isl_union_set_free(res);
3163
0
  return NULL;
3164
0
}
3165
3166
static isl_stat solutions_entry(void **entry, void *user)
3167
0
{
3168
0
  isl_set *set = *entry;
3169
0
  isl_union_set **res = user;
3170
0
3171
0
  set = isl_set_copy(set);
3172
0
  set = isl_set_from_basic_set(isl_set_solutions(set));
3173
0
  if (!*res)
3174
0
    *res = isl_union_set_from_set(set);
3175
0
  else
3176
0
    *res = isl_union_set_add_set(*res, set);
3177
0
3178
0
  if (!*res)
3179
0
    return isl_stat_error;
3180
0
3181
0
  return isl_stat_ok;
3182
0
}
3183
3184
__isl_give isl_union_set *isl_union_set_solutions(
3185
  __isl_take isl_union_set *uset)
3186
0
{
3187
0
  isl_union_set *res = NULL;
3188
0
3189
0
  if (!uset)
3190
0
    return NULL;
3191
0
3192
0
  if (uset->table.n == 0) {
3193
0
    res = isl_union_set_empty(isl_union_set_get_space(uset));
3194
0
    isl_union_set_free(uset);
3195
0
    return res;
3196
0
  }
3197
0
3198
0
  if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3199
0
           &solutions_entry, &res) < 0)
3200
0
    goto error;
3201
0
3202
0
  isl_union_set_free(uset);
3203
0
  return res;
3204
0
error:
3205
0
  isl_union_set_free(uset);
3206
0
  isl_union_set_free(res);
3207
0
  return NULL;
3208
0
}
3209
3210
/* Is the domain space of "map" equal to "space"?
3211
 */
3212
static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3213
3.23k
{
3214
3.23k
  return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3215
3.23k
          space, isl_dim_out);
3216
3.23k
}
3217
3218
/* Is the range space of "map" equal to "space"?
3219
 */
3220
static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3221
0
{
3222
0
  return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3223
0
          space, isl_dim_out);
3224
0
}
3225
3226
/* Is the set space of "map" equal to "space"?
3227
 */
3228
static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3229
3.60k
{
3230
3.60k
  return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3231
3.60k
          space, isl_dim_out);
3232
3.60k
}
3233
3234
/* Internal data structure for preimage_pw_multi_aff.
3235
 *
3236
 * "pma" is the function under which the preimage should be taken.
3237
 * "space" is the space of "pma".
3238
 * "res" collects the results.
3239
 * "fn" computes the preimage for a given map.
3240
 * "match" returns true if "fn" can be called.
3241
 */
3242
struct isl_union_map_preimage_data {
3243
  isl_space *space;
3244
  isl_pw_multi_aff *pma;
3245
  isl_union_map *res;
3246
  int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3247
  __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3248
    __isl_take isl_pw_multi_aff *pma);
3249
};
3250
3251
/* Call data->fn to compute the preimage of the domain or range of *entry
3252
 * under the function represented by data->pma, provided the domain/range
3253
 * space of *entry matches the target space of data->pma
3254
 * (as given by data->match), and add the result to data->res.
3255
 */
3256
static isl_stat preimage_entry(void **entry, void *user)
3257
6.83k
{
3258
6.83k
  int m;
3259
6.83k
  isl_map *map = *entry;
3260
6.83k
  struct isl_union_map_preimage_data *data = user;
3261
6.83k
  isl_bool empty;
3262
6.83k
3263
6.83k
  m = data->match(map, data->space);
3264
6.83k
  if (m < 0)
3265
0
    return isl_stat_error;
3266
6.83k
  if (!m)
3267
2.53k
    return isl_stat_ok;
3268
4.30k
3269
4.30k
  map = isl_map_copy(map);
3270
4.30k
  map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3271
4.30k
3272
4.30k
  empty = isl_map_is_empty(map);
3273
4.30k
  if (empty < 0 || empty) {
3274
2
    isl_map_free(map);
3275
2
    return empty < 0 ? 
isl_stat_error0
: isl_stat_ok;
3276
2
  }
3277
4.29k
3278
4.29k
  data->res = isl_union_map_add_map(data->res, map);
3279
4.29k
3280
4.29k
  return isl_stat_ok;
3281
4.29k
}
3282
3283
/* Compute the preimage of the domain or range of "umap" under the function
3284
 * represented by "pma".
3285
 * In other words, plug in "pma" in the domain or range of "umap".
3286
 * The function "fn" performs the actual preimage computation on a map,
3287
 * while "match" determines to which maps the function should be applied.
3288
 */
3289
static __isl_give isl_union_map *preimage_pw_multi_aff(
3290
  __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3291
  int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3292
  __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3293
    __isl_take isl_pw_multi_aff *pma))
3294
5.52k
{
3295
5.52k
  isl_ctx *ctx;
3296
5.52k
  isl_space *space;
3297
5.52k
  struct isl_union_map_preimage_data data;
3298
5.52k
3299
5.52k
  umap = isl_union_map_align_params(umap,
3300
5.52k
              isl_pw_multi_aff_get_space(pma));
3301
5.52k
  pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3302
5.52k
3303
5.52k
  if (!umap || !pma)
3304
0
    goto error;
3305
5.52k
3306
5.52k
  ctx = isl_union_map_get_ctx(umap);
3307
5.52k
  space = isl_union_map_get_space(umap);
3308
5.52k
  data.space = isl_pw_multi_aff_get_space(pma);
3309
5.52k
  data.pma = pma;
3310
5.52k
  data.res = isl_union_map_alloc(space, umap->table.n);
3311
5.52k
  data.match = match;
3312
5.52k
  data.fn = fn;
3313
5.52k
  if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3314
5.52k
          &data) < 0)
3315
0
    data.res = isl_union_map_free(data.res);
3316
5.52k
3317
5.52k
  isl_space_free(data.space);
3318
5.52k
  isl_union_map_free(umap);
3319
5.52k
  isl_pw_multi_aff_free(pma);
3320
5.52k
  return data.res;
3321
0
error:
3322
0
  isl_union_map_free(umap);
3323
0
  isl_pw_multi_aff_free(pma);
3324
0
  return NULL;
3325
5.52k
}
3326
3327
/* Compute the preimage of the domain of "umap" under the function
3328
 * represented by "pma".
3329
 * In other words, plug in "pma" in the domain of "umap".
3330
 * The result contains maps that live in the same spaces as the maps of "umap"
3331
 * with domain space equal to the target space of "pma",
3332
 * except that the domain has been replaced by the domain space of "pma".
3333
 */
3334
__isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3335
  __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3336
3.21k
{
3337
3.21k
  return preimage_pw_multi_aff(umap, pma, &domain_match,
3338
3.21k
          &isl_map_preimage_domain_pw_multi_aff);
3339
3.21k
}
3340
3341
/* Compute the preimage of the range of "umap" under the function
3342
 * represented by "pma".
3343
 * In other words, plug in "pma" in the range of "umap".
3344
 * The result contains maps that live in the same spaces as the maps of "umap"
3345
 * with range space equal to the target space of "pma",
3346
 * except that the range has been replaced by the domain space of "pma".
3347
 */
3348
__isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3349
  __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3350
0
{
3351
0
  return preimage_pw_multi_aff(umap, pma, &range_match,
3352
0
          &isl_map_preimage_range_pw_multi_aff);
3353
0
}
3354
3355
/* Compute the preimage of "uset" under the function represented by "pma".
3356
 * In other words, plug in "pma" in "uset".
3357
 * The result contains sets that live in the same spaces as the sets of "uset"
3358
 * with space equal to the target space of "pma",
3359
 * except that the space has been replaced by the domain space of "pma".
3360
 */
3361
__isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3362
  __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3363
2.31k
{
3364
2.31k
  return preimage_pw_multi_aff(uset, pma, &set_match,
3365
2.31k
          &isl_set_preimage_pw_multi_aff);
3366
2.31k
}
3367
3368
/* Compute the preimage of the domain of "umap" under the function
3369
 * represented by "ma".
3370
 * In other words, plug in "ma" in the domain of "umap".
3371
 * The result contains maps that live in the same spaces as the maps of "umap"
3372
 * with domain space equal to the target space of "ma",
3373
 * except that the domain has been replaced by the domain space of "ma".
3374
 */
3375
__isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3376
  __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3377
3.21k
{
3378
3.21k
  return isl_union_map_preimage_domain_pw_multi_aff(umap,
3379
3.21k
          isl_pw_multi_aff_from_multi_aff(ma));
3380
3.21k
}
3381
3382
/* Compute the preimage of the range of "umap" under the function
3383
 * represented by "ma".
3384
 * In other words, plug in "ma" in the range of "umap".
3385
 * The result contains maps that live in the same spaces as the maps of "umap"
3386
 * with range space equal to the target space of "ma",
3387
 * except that the range has been replaced by the domain space of "ma".
3388
 */
3389
__isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3390
  __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3391
0
{
3392
0
  return isl_union_map_preimage_range_pw_multi_aff(umap,
3393
0
          isl_pw_multi_aff_from_multi_aff(ma));
3394
0
}
3395
3396
/* Compute the preimage of "uset" under the function represented by "ma".
3397
 * In other words, plug in "ma" in "uset".
3398
 * The result contains sets that live in the same spaces as the sets of "uset"
3399
 * with space equal to the target space of "ma",
3400
 * except that the space has been replaced by the domain space of "ma".
3401
 */
3402
__isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3403
  __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3404
0
{
3405
0
  return isl_union_set_preimage_pw_multi_aff(uset,
3406
0
          isl_pw_multi_aff_from_multi_aff(ma));
3407
0
}
3408
3409
/* Internal data structure for preimage_multi_pw_aff.
3410
 *
3411
 * "mpa" is the function under which the preimage should be taken.
3412
 * "space" is the space of "mpa".
3413
 * "res" collects the results.
3414
 * "fn" computes the preimage for a given map.
3415
 * "match" returns true if "fn" can be called.
3416
 */
3417
struct isl_union_map_preimage_mpa_data {
3418
  isl_space *space;
3419
  isl_multi_pw_aff *mpa;
3420
  isl_union_map *res;
3421
  int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3422
  __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3423
    __isl_take isl_multi_pw_aff *mpa);
3424
};
3425
3426
/* Call data->fn to compute the preimage of the domain or range of *entry
3427
 * under the function represented by data->mpa, provided the domain/range
3428
 * space of *entry matches the target space of data->mpa
3429
 * (as given by data->match), and add the result to data->res.
3430
 */
3431
static isl_stat preimage_mpa_entry(void **entry, void *user)
3432
0
{
3433
0
  int m;
3434
0
  isl_map *map = *entry;
3435
0
  struct isl_union_map_preimage_mpa_data *data = user;
3436
0
  isl_bool empty;
3437
0
3438
0
  m = data->match(map, data->space);
3439
0
  if (m < 0)
3440
0
    return isl_stat_error;
3441
0
  if (!m)
3442
0
    return isl_stat_ok;
3443
0
3444
0
  map = isl_map_copy(map);
3445
0
  map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3446
0
3447
0
  empty = isl_map_is_empty(map);
3448
0
  if (empty < 0 || empty) {
3449
0
    isl_map_free(map);
3450
0
    return empty < 0 ? isl_stat_error : isl_stat_ok;
3451
0
  }
3452
0
3453
0
  data->res = isl_union_map_add_map(data->res, map);
3454
0
3455
0
  return isl_stat_ok;
3456
0
}
3457
3458
/* Compute the preimage of the domain or range of "umap" under the function
3459
 * represented by "mpa".
3460
 * In other words, plug in "mpa" in the domain or range of "umap".
3461
 * The function "fn" performs the actual preimage computation on a map,
3462
 * while "match" determines to which maps the function should be applied.
3463
 */
3464
static __isl_give isl_union_map *preimage_multi_pw_aff(
3465
  __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3466
  int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3467
  __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3468
    __isl_take isl_multi_pw_aff *mpa))
3469
0
{
3470
0
  isl_ctx *ctx;
3471
0
  isl_space *space;
3472
0
  struct isl_union_map_preimage_mpa_data data;
3473
0
3474
0
  umap = isl_union_map_align_params(umap,
3475
0
              isl_multi_pw_aff_get_space(mpa));
3476
0
  mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3477
0
3478
0
  if (!umap || !mpa)
3479
0
    goto error;
3480
0
3481
0
  ctx = isl_union_map_get_ctx(umap);
3482
0
  space = isl_union_map_get_space(umap);
3483
0
  data.space = isl_multi_pw_aff_get_space(mpa);
3484
0
  data.mpa = mpa;
3485
0
  data.res = isl_union_map_alloc(space, umap->table.n);
3486
0
  data.match = match;
3487
0
  data.fn = fn;
3488
0
  if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3489
0
          &data) < 0)
3490
0
    data.res = isl_union_map_free(data.res);
3491
0
3492
0
  isl_space_free(data.space);
3493
0
  isl_union_map_free(umap);
3494
0
  isl_multi_pw_aff_free(mpa);
3495
0
  return data.res;
3496
0
error:
3497
0
  isl_union_map_free(umap);
3498
0
  isl_multi_pw_aff_free(mpa);
3499
0
  return NULL;
3500
0
}
3501
3502
/* Compute the preimage of the domain of "umap" under the function
3503
 * represented by "mpa".
3504
 * In other words, plug in "mpa" in the domain of "umap".
3505
 * The result contains maps that live in the same spaces as the maps of "umap"
3506
 * with domain space equal to the target space of "mpa",
3507
 * except that the domain has been replaced by the domain space of "mpa".
3508
 */
3509
__isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3510
  __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3511
0
{
3512
0
  return preimage_multi_pw_aff(umap, mpa, &domain_match,
3513
0
          &isl_map_preimage_domain_multi_pw_aff);
3514
0
}
3515
3516
/* Internal data structure for preimage_upma.
3517
 *
3518
 * "umap" is the map of which the preimage should be computed.
3519
 * "res" collects the results.
3520
 * "fn" computes the preimage for a given piecewise multi-affine function.
3521
 */
3522
struct isl_union_map_preimage_upma_data {
3523
  isl_union_map *umap;
3524
  isl_union_map *res;
3525
  __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3526
    __isl_take isl_pw_multi_aff *pma);
3527
};
3528
3529
/* Call data->fn to compute the preimage of the domain or range of data->umap
3530
 * under the function represented by pma and add the result to data->res.
3531
 */
3532
static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3533
2.31k
{
3534
2.31k
  struct isl_union_map_preimage_upma_data *data = user;
3535
2.31k
  isl_union_map *umap;
3536
2.31k
3537
2.31k
  umap = isl_union_map_copy(data->umap);
3538
2.31k
  umap = data->fn(umap, pma);
3539
2.31k
  data->res = isl_union_map_union(data->res, umap);
3540
2.31k
3541
2.31k
  return data->res ? isl_stat_ok : 
isl_stat_error0
;
3542
2.31k
}
3543
3544
/* Compute the preimage of the domain or range of "umap" under the function
3545
 * represented by "upma".
3546
 * In other words, plug in "upma" in the domain or range of "umap".
3547
 * The function "fn" performs the actual preimage computation
3548
 * on a piecewise multi-affine function.
3549
 */
3550
static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3551
  __isl_take isl_union_map *umap,
3552
  __isl_take isl_union_pw_multi_aff *upma,
3553
  __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3554
    __isl_take isl_pw_multi_aff *pma))
3555
404
{
3556
404
  struct isl_union_map_preimage_upma_data data;
3557
404
3558
404
  data.umap = umap;
3559
404
  data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3560
404
  data.fn = fn;
3561
404
  if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3562
404
                &preimage_upma, &data) < 0)
3563
0
    data.res = isl_union_map_free(data.res);
3564
404
3565
404
  isl_union_map_free(umap);
3566
404
  isl_union_pw_multi_aff_free(upma);
3567
404
3568
404
  return data.res;
3569
404
}
3570
3571
/* Compute the preimage of the domain of "umap" under the function
3572
 * represented by "upma".
3573
 * In other words, plug in "upma" in the domain of "umap".
3574
 * The result contains maps that live in the same spaces as the maps of "umap"
3575
 * with domain space equal to one of the target spaces of "upma",
3576
 * except that the domain has been replaced by one of the domain spaces that
3577
 * correspond to that target space of "upma".
3578
 */
3579
__isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3580
  __isl_take isl_union_map *umap,
3581
  __isl_take isl_union_pw_multi_aff *upma)
3582
0
{
3583
0
  return preimage_union_pw_multi_aff(umap, upma,
3584
0
        &isl_union_map_preimage_domain_pw_multi_aff);
3585
0
}
3586
3587
/* Compute the preimage of the range of "umap" under the function
3588
 * represented by "upma".
3589
 * In other words, plug in "upma" in the range of "umap".
3590
 * The result contains maps that live in the same spaces as the maps of "umap"
3591
 * with range space equal to one of the target spaces of "upma",
3592
 * except that the range has been replaced by one of the domain spaces that
3593
 * correspond to that target space of "upma".
3594
 */
3595
__isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3596
  __isl_take isl_union_map *umap,
3597
  __isl_take isl_union_pw_multi_aff *upma)
3598
0
{
3599
0
  return preimage_union_pw_multi_aff(umap, upma,
3600
0
        &isl_union_map_preimage_range_pw_multi_aff);
3601
0
}
3602
3603
/* Compute the preimage of "uset" under the function represented by "upma".
3604
 * In other words, plug in "upma" in the range of "uset".
3605
 * The result contains sets that live in the same spaces as the sets of "uset"
3606
 * with space equal to one of the target spaces of "upma",
3607
 * except that the space has been replaced by one of the domain spaces that
3608
 * correspond to that target space of "upma".
3609
 */
3610
__isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3611
  __isl_take isl_union_set *uset,
3612
  __isl_take isl_union_pw_multi_aff *upma)
3613
404
{
3614
404
  return preimage_union_pw_multi_aff(uset, upma,
3615
404
          &isl_union_set_preimage_pw_multi_aff);
3616
404
}
3617
3618
/* Reset the user pointer on all identifiers of parameters and tuples
3619
 * of the spaces of "umap".
3620
 */
3621
__isl_give isl_union_map *isl_union_map_reset_user(
3622
  __isl_take isl_union_map *umap)
3623
0
{
3624
0
  umap = isl_union_map_cow(umap);
3625
0
  if (!umap)
3626
0
    return NULL;
3627
0
  umap->dim = isl_space_reset_user(umap->dim);
3628
0
  if (!umap->dim)
3629
0
    return isl_union_map_free(umap);
3630
0
  return total(umap, &isl_map_reset_user);
3631
0
}
3632
3633
/* Reset the user pointer on all identifiers of parameters and tuples
3634
 * of the spaces of "uset".
3635
 */
3636
__isl_give isl_union_set *isl_union_set_reset_user(
3637
  __isl_take isl_union_set *uset)
3638
0
{
3639
0
  return isl_union_map_reset_user(uset);
3640
0
}
3641
3642
/* Remove all existentially quantified variables and integer divisions
3643
 * from "umap" using Fourier-Motzkin elimination.
3644
 */
3645
__isl_give isl_union_map *isl_union_map_remove_divs(
3646
  __isl_take isl_union_map *umap)
3647
60
{
3648
60
  return total(umap, &isl_map_remove_divs);
3649
60
}
3650
3651
/* Remove all existentially quantified variables and integer divisions
3652
 * from "uset" using Fourier-Motzkin elimination.
3653
 */
3654
__isl_give isl_union_set *isl_union_set_remove_divs(
3655
  __isl_take isl_union_set *uset)
3656
30
{
3657
30
  return isl_union_map_remove_divs(uset);
3658
30
}
3659
3660
/* Internal data structure for isl_union_map_project_out.
3661
 * "type", "first" and "n" are the arguments for the isl_map_project_out
3662
 * call.
3663
 * "res" collects the results.
3664
 */
3665
struct isl_union_map_project_out_data {
3666
  enum isl_dim_type type;
3667
  unsigned first;
3668
  unsigned n;
3669
3670
  isl_union_map *res;
3671
};
3672
3673
/* Turn the data->n dimensions of type data->type, starting at data->first
3674
 * into existentially quantified variables and add the result to data->res.
3675
 */
3676
static isl_stat project_out(__isl_take isl_map *map, void *user)
3677
20
{
3678
20
  struct isl_union_map_project_out_data *data = user;
3679
20
3680
20
  map = isl_map_project_out(map, data->type, data->first, data->n);
3681
20
  data->res = isl_union_map_add_map(data->res, map);
3682
20
3683
20
  return isl_stat_ok;
3684
20
}
3685
3686
/* Turn the "n" dimensions of type "type", starting at "first"
3687
 * into existentially quantified variables.
3688
 * Since the space of an isl_union_map only contains parameters,
3689
 * type is required to be equal to isl_dim_param.
3690
 */
3691
__isl_give isl_union_map *isl_union_map_project_out(
3692
  __isl_take isl_union_map *umap,
3693
  enum isl_dim_type type, unsigned first, unsigned n)
3694
28
{
3695
28
  isl_space *space;
3696
28
  struct isl_union_map_project_out_data data = { type, first, n };
3697
28
3698
28
  if (!umap)
3699
0
    return NULL;
3700
28
3701
28
  if (type != isl_dim_param)
3702
28
    
isl_die0
(isl_union_map_get_ctx(umap), isl_error_invalid,
3703
28
      "can only project out parameters",
3704
28
      return isl_union_map_free(umap));
3705
28
3706
28
  space = isl_union_map_get_space(umap);
3707
28
  space = isl_space_drop_dims(space, type, first, n);
3708
28
  data.res = isl_union_map_empty(space);
3709
28
  if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3710
0
    data.res = isl_union_map_free(data.res);
3711
28
3712
28
  isl_union_map_free(umap);
3713
28
3714
28
  return data.res;
3715
28
}
3716
3717
/* Project out all parameters from "umap" by existentially quantifying
3718
 * over them.
3719
 */
3720
__isl_give isl_union_map *isl_union_map_project_out_all_params(
3721
  __isl_take isl_union_map *umap)
3722
0
{
3723
0
  unsigned n;
3724
0
3725
0
  if (!umap)
3726
0
    return NULL;
3727
0
  n = isl_union_map_dim(umap, isl_dim_param);
3728
0
  return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3729
0
}
3730
3731
/* Turn the "n" dimensions of type "type", starting at "first"
3732
 * into existentially quantified variables.
3733
 * Since the space of an isl_union_set only contains parameters,
3734
 * "type" is required to be equal to isl_dim_param.
3735
 */
3736
__isl_give isl_union_set *isl_union_set_project_out(
3737
  __isl_take isl_union_set *uset,
3738
  enum isl_dim_type type, unsigned first, unsigned n)
3739
28
{
3740
28
  return isl_union_map_project_out(uset, type, first, n);
3741
28
}
3742
3743
/* Internal data structure for isl_union_map_involves_dims.
3744
 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3745
 */
3746
struct isl_union_map_involves_dims_data {
3747
  unsigned first;
3748
  unsigned n;
3749
};
3750
3751
/* Does "map" _not_ involve the data->n parameters starting at data->first?
3752
 */
3753
static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3754
0
{
3755
0
  struct isl_union_map_involves_dims_data *data = user;
3756
0
  isl_bool involves;
3757
0
3758
0
  involves = isl_map_involves_dims(map,
3759
0
          isl_dim_param, data->first, data->n);
3760
0
  if (involves < 0)
3761
0
    return isl_bool_error;
3762
0
  return !involves;
3763
0
}
3764
3765
/* Does "umap" involve any of the n parameters starting at first?
3766
 * "type" is required to be set to isl_dim_param.
3767
 *
3768
 * "umap" involves any of those parameters if any of its maps
3769
 * involve the parameters.  In other words, "umap" does not
3770
 * involve any of the parameters if all its maps to not
3771
 * involve the parameters.
3772
 */
3773
isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3774
  enum isl_dim_type type, unsigned first, unsigned n)
3775
0
{
3776
0
  struct isl_union_map_involves_dims_data data = { first, n };
3777
0
  isl_bool excludes;
3778
0
3779
0
  if (type != isl_dim_param)
3780
0
    isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3781
0
      "can only reference parameters", return isl_bool_error);
3782
0
3783
0
  excludes = union_map_forall_user(umap, &map_excludes, &data);
3784
0
3785
0
  if (excludes < 0)
3786
0
    return isl_bool_error;
3787
0
3788
0
  return !excludes;
3789
0
}
3790
3791
/* Internal data structure for isl_union_map_reset_range_space.
3792
 * "range" is the space from which to set the range space.
3793
 * "res" collects the results.
3794
 */
3795
struct isl_union_map_reset_range_space_data {
3796
  isl_space *range;
3797
  isl_union_map *res;
3798
};
3799
3800
/* Replace the range space of "map" by the range space of data->range and
3801
 * add the result to data->res.
3802
 */
3803
static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3804
15.1k
{
3805
15.1k
  struct isl_union_map_reset_range_space_data *data = user;
3806
15.1k
  isl_space *space;
3807
15.1k
3808
15.1k
  space = isl_map_get_space(map);
3809
15.1k
  space = isl_space_domain(space);
3810
15.1k
  space = isl_space_extend_domain_with_range(space,
3811
15.1k
            isl_space_copy(data->range));
3812
15.1k
  map = isl_map_reset_space(map, space);
3813
15.1k
  data->res = isl_union_map_add_map(data->res, map);
3814
15.1k
3815
15.1k
  return data->res ? isl_stat_ok : 
isl_stat_error0
;
3816
15.1k
}
3817
3818
/* Replace the range space of all the maps in "umap" by
3819
 * the range space of "space".
3820
 *
3821
 * This assumes that all maps have the same output dimension.
3822
 * This function should therefore not be made publicly available.
3823
 *
3824
 * Since the spaces of the maps change, so do their hash value.
3825
 * We therefore need to create a new isl_union_map.
3826
 */
3827
__isl_give isl_union_map *isl_union_map_reset_range_space(
3828
  __isl_take isl_union_map *umap, __isl_take isl_space *space)
3829
7.33k
{
3830
7.33k
  struct isl_union_map_reset_range_space_data data = { space };
3831
7.33k
3832
7.33k
  data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3833
7.33k
  if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3834
0
    data.res = isl_union_map_free(data.res);
3835
7.33k
3836
7.33k
  isl_space_free(space);
3837
7.33k
  isl_union_map_free(umap);
3838
7.33k
  return data.res;
3839
7.33k
}
3840
3841
/* Check that "umap" and "space" have the same number of parameters.
3842
 */
3843
static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
3844
  __isl_keep isl_space *space)
3845
0
{
3846
0
  unsigned dim1, dim2;
3847
0
3848
0
  if (!umap || !space)
3849
0
    return isl_stat_error;
3850
0
  dim1 = isl_union_map_dim(umap, isl_dim_param);
3851
0
  dim2 = isl_space_dim(space, isl_dim_param);
3852
0
  if (dim1 == dim2)
3853
0
    return isl_stat_ok;
3854
0
  isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3855
0
    "number of parameters does not match", return isl_stat_error);
3856
0
}
3857
3858
/* Internal data structure for isl_union_map_reset_equal_dim_space.
3859
 * "space" is the target space.
3860
 * "res" collects the results.
3861
 */
3862
struct isl_union_map_reset_params_data {
3863
  isl_space *space;
3864
  isl_union_map *res;
3865
};
3866
3867
/* Replace the parameters of "map" by those of data->space and
3868
 * add the result to data->res.
3869
 */
3870
static isl_stat reset_params(__isl_take isl_map *map, void *user)
3871
0
{
3872
0
  struct isl_union_map_reset_params_data *data = user;
3873
0
  isl_space *space;
3874
0
3875
0
  space = isl_map_get_space(map);
3876
0
  space = isl_space_replace_params(space, data->space);
3877
0
  map = isl_map_reset_equal_dim_space(map, space);
3878
0
  data->res = isl_union_map_add_map(data->res, map);
3879
0
3880
0
  return data->res ? isl_stat_ok : isl_stat_error;
3881
0
}
3882
3883
/* Replace the space of "umap" by "space", without modifying
3884
 * the dimension of "umap", i.e., the number of parameters of "umap".
3885
 *
3886
 * Since the hash values of the maps in the union map depend
3887
 * on the parameters, a new union map needs to be constructed.
3888
 */
3889
__isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
3890
  __isl_take isl_union_map *umap, __isl_take isl_space *space)
3891
41
{
3892
41
  struct isl_union_map_reset_params_data data = { space };
3893
41
  isl_bool equal;
3894
41
  isl_space *umap_space;
3895
41
3896
41
  umap_space = isl_union_map_peek_space(umap);
3897
41
  equal = isl_space_is_equal(umap_space, space);
3898
41
  if (equal < 0)
3899
0
    goto error;
3900
41
  if (equal) {
3901
41
    isl_space_free(space);
3902
41
    return umap;
3903
41
  }
3904
0
  if (check_union_map_space_equal_dim(umap, space) < 0)
3905
0
    goto error;
3906
0
3907
0
  data.res = isl_union_map_empty(isl_space_copy(space));
3908
0
  if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
3909
0
    data.res = isl_union_map_free(data.res);
3910
0
3911
0
  isl_space_free(space);
3912
0
  isl_union_map_free(umap);
3913
0
  return data.res;
3914
0
error:
3915
0
  isl_union_map_free(umap);
3916
0
  isl_space_free(space);
3917
0
  return NULL;
3918
0
}
3919
3920
/* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3921
 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3922
 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3923
 * to the domain and the range of each map.
3924
 * "res" collects the results.
3925
 */
3926
struct isl_union_order_at_data {
3927
  isl_multi_union_pw_aff *mupa;
3928
  __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3929
    __isl_take isl_multi_pw_aff *mpa2);
3930
  isl_union_map *res;
3931
};
3932
3933
/* Intersect "map" with the result of applying data->order to
3934
 * the functions in data->mupa that apply to the domain and the range
3935
 * of "map" and add the result to data->res.
3936
 */
3937
static isl_stat order_at(__isl_take isl_map *map, void *user)
3938
14
{
3939
14
  struct isl_union_order_at_data *data = user;
3940
14
  isl_space *space;
3941
14
  isl_multi_pw_aff *mpa1, *mpa2;
3942
14
  isl_map *order;
3943
14
3944
14
  space = isl_space_domain(isl_map_get_space(map));
3945
14
  mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3946
14
  space = isl_space_range(isl_map_get_space(map));
3947
14
  mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3948
14
  order = data->order(mpa1, mpa2);
3949
14
  map = isl_map_intersect(map, order);
3950
14
  data->res = isl_union_map_add_map(data->res, map);
3951
14
3952
14
  return data->res ? isl_stat_ok : 
isl_stat_error0
;
3953
14
}
3954
3955
/* If "mupa" has a non-trivial explicit domain, then intersect
3956
 * domain and range of "umap" with this explicit domain.
3957
 * If the explicit domain only describes constraints on the parameters,
3958
 * then the intersection only needs to be performed once.
3959
 */
3960
static __isl_give isl_union_map *intersect_explicit_domain(
3961
  __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
3962
12
{
3963
12
  isl_bool non_trivial, is_params;
3964
12
  isl_union_set *dom;
3965
12
3966
12
  non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3967
12
  if (non_trivial < 0)
3968
0
    return isl_union_map_free(umap);
3969
12
  if (!non_trivial)
3970
7
    return umap;
3971
5
  mupa = isl_multi_union_pw_aff_copy(mupa);
3972
5
  dom = isl_multi_union_pw_aff_domain(mupa);
3973
5
  is_params = isl_union_set_is_params(dom);
3974
5
  if (is_params < 0) {
3975
0
    isl_union_set_free(dom);
3976
0
    return isl_union_map_free(umap);
3977
0
  }
3978
5
  if (is_params) {
3979
1
    isl_set *set;
3980
1
3981
1
    set = isl_union_set_params(dom);
3982
1
    umap = isl_union_map_intersect_params(umap, set);
3983
1
    return umap;
3984
1
  }
3985
4
  umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
3986
4
  umap = isl_union_map_intersect_range(umap, dom);
3987
4
  return umap;
3988
4
}
3989
3990
/* Intersect each map in "umap" with the result of calling "order"
3991
 * on the functions is "mupa" that apply to the domain and the range
3992
 * of the map.
3993
 */
3994
static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3995
  __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3996
  __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3997
    __isl_take isl_multi_pw_aff *mpa2))
3998
12
{
3999
12
  struct isl_union_order_at_data data;
4000
12
4001
12
  umap = isl_union_map_align_params(umap,
4002
12
        isl_multi_union_pw_aff_get_space(mupa));
4003
12
  mupa = isl_multi_union_pw_aff_align_params(mupa,
4004
12
        isl_union_map_get_space(umap));
4005
12
  umap = intersect_explicit_domain(umap, mupa);
4006
12
  data.mupa = mupa;
4007
12
  data.order = order;
4008
12
  data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4009
12
  if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4010
0
    data.res = isl_union_map_free(data.res);
4011
12
4012
12
  isl_multi_union_pw_aff_free(mupa);
4013
12
  isl_union_map_free(umap);
4014
12
  return data.res;
4015
12
}
4016
4017
/* Return the subset of "umap" where the domain and the range
4018
 * have equal "mupa" values.
4019
 */
4020
__isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4021
  __isl_take isl_union_map *umap,
4022
  __isl_take isl_multi_union_pw_aff *mupa)
4023
10
{
4024
10
  return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4025
10
            &isl_multi_pw_aff_eq_map);
4026
10
}
4027
4028
/* Return the subset of "umap" where the domain has a lexicographically
4029
 * smaller "mupa" value than the range.
4030
 */
4031
__isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
4032
  __isl_take isl_union_map *umap,
4033
  __isl_take isl_multi_union_pw_aff *mupa)
4034
1
{
4035
1
  return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4036
1
            &isl_multi_pw_aff_lex_lt_map);
4037
1
}
4038
4039
/* Return the subset of "umap" where the domain has a lexicographically
4040
 * greater "mupa" value than the range.
4041
 */
4042
__isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
4043
  __isl_take isl_union_map *umap,
4044
  __isl_take isl_multi_union_pw_aff *mupa)
4045
1
{
4046
1
  return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4047
1
            &isl_multi_pw_aff_lex_gt_map);
4048
1
}
4049
4050
/* Return the union of the elements in the list "list".
4051
 */
4052
__isl_give isl_union_set *isl_union_set_list_union(
4053
  __isl_take isl_union_set_list *list)
4054
0
{
4055
0
  int i, n;
4056
0
  isl_ctx *ctx;
4057
0
  isl_space *space;
4058
0
  isl_union_set *res;
4059
0
4060
0
  if (!list)
4061
0
    return NULL;
4062
0
4063
0
  ctx = isl_union_set_list_get_ctx(list);
4064
0
  space = isl_space_params_alloc(ctx, 0);
4065
0
  res = isl_union_set_empty(space);
4066
0
4067
0
  n = isl_union_set_list_n_union_set(list);
4068
0
  for (i = 0; i < n; ++i) {
4069
0
    isl_union_set *uset_i;
4070
0
4071
0
    uset_i = isl_union_set_list_get_union_set(list, i);
4072
0
    res = isl_union_set_union(res, uset_i);
4073
0
  }
4074
0
4075
0
  isl_union_set_list_free(list);
4076
0
  return res;
4077
0
}
4078
4079
/* Update *hash with the hash value of "map".
4080
 */
4081
static isl_stat add_hash(__isl_take isl_map *map, void *user)
4082
0
{
4083
0
  uint32_t *hash = user;
4084
0
  uint32_t map_hash;
4085
0
4086
0
  map_hash = isl_map_get_hash(map);
4087
0
  isl_hash_hash(*hash, map_hash);
4088
0
4089
0
  isl_map_free(map);
4090
0
  return isl_stat_ok;
4091
0
}
4092
4093
/* Return a hash value that digests "umap".
4094
 */
4095
uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4096
0
{
4097
0
  uint32_t hash;
4098
0
4099
0
  if (!umap)
4100
0
    return 0;
4101
0
4102
0
  hash = isl_hash_init();
4103
0
  if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4104
0
    return 0;
4105
0
4106
0
  return hash;
4107
0
}
4108
4109
/* Return a hash value that digests "uset".
4110
 */
4111
uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4112
0
{
4113
0
  return isl_union_map_get_hash(uset);
4114
0
}
4115
4116
/* Add the number of basic sets in "set" to "n".
4117
 */
4118
static isl_stat add_n(__isl_take isl_set *set, void *user)
4119
40
{
4120
40
  int *n = user;
4121
40
4122
40
  *n += isl_set_n_basic_set(set);
4123
40
  isl_set_free(set);
4124
40
4125
40
  return isl_stat_ok;
4126
40
}
4127
4128
/* Return the total number of basic sets in "uset".
4129
 */
4130
int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4131
57
{
4132
57
  int n = 0;
4133
57
4134
57
  if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4135
0
    return -1;
4136
57
4137
57
  return n;
4138
57
}
4139
4140
/* Add the basic sets in "set" to "list".
4141
 */
4142
static isl_stat add_list(__isl_take isl_set *set, void *user)
4143
40
{
4144
40
  isl_basic_set_list **list = user;
4145
40
  isl_basic_set_list *list_i;
4146
40
4147
40
  list_i = isl_set_get_basic_set_list(set);
4148
40
  *list = isl_basic_set_list_concat(*list, list_i);
4149
40
  isl_set_free(set);
4150
40
4151
40
  if (!*list)
4152
0
    return isl_stat_error;
4153
40
  return isl_stat_ok;
4154
40
}
4155
4156
/* Return a list containing all the basic sets in "uset".
4157
 *
4158
 * First construct a list of the appropriate size and
4159
 * then add all the elements.
4160
 */
4161
__isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4162
  __isl_keep isl_union_set *uset)
4163
57
{
4164
57
  int n;
4165
57
  isl_ctx *ctx;
4166
57
  isl_basic_set_list *list;
4167
57
4168
57
  if (!uset)
4169
0
    return NULL;
4170
57
  ctx = isl_union_set_get_ctx(uset);
4171
57
  n = isl_union_set_n_basic_set(uset);
4172
57
  if (n < 0)
4173
0
    return NULL;
4174
57
  list = isl_basic_set_list_alloc(ctx, n);
4175
57
  if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4176
0
    list = isl_basic_set_list_free(list);
4177
57
4178
57
  return list;
4179
57
}
4180
4181
/* Internal data structure for isl_union_map_remove_map_if.
4182
 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4183
 */
4184
struct isl_union_map_remove_map_if_data {
4185
  isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4186
  void *user;
4187
};
4188
4189
/* isl_un_op_control filter that negates the result of data->fn
4190
 * called on "map".
4191
 */
4192
static isl_bool not(__isl_keep isl_map *map, void *user)
4193
0
{
4194
0
  struct isl_union_map_remove_map_if_data *data = user;
4195
0
4196
0
  return isl_bool_not(data->fn(map, data->user));
4197
0
}
4198
4199
/* Dummy isl_un_op_control transformation callback that
4200
 * simply returns the input.
4201
 */
4202
static __isl_give isl_map *map_id(__isl_take isl_map *map)
4203
0
{
4204
0
  return map;
4205
0
}
4206
4207
/* Call "fn" on every map in "umap" and remove those maps
4208
 * for which the callback returns true.
4209
 *
4210
 * Use un_op to keep only those maps that are not filtered out,
4211
 * applying an identity transformation on them.
4212
 */
4213
__isl_give isl_union_map *isl_union_map_remove_map_if(
4214
  __isl_take isl_union_map *umap,
4215
  isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4216
0
{
4217
0
  struct isl_union_map_remove_map_if_data data = { fn, user };
4218
0
  struct isl_un_op_control control = {
4219
0
    .filter = &not,
4220
0
    .filter_user = &data,
4221
0
    .fn_map = &map_id,
4222
0
  };
4223
0
  return un_op(umap, &control);
4224
0
}