Coverage Report

Created: 2018-02-20 23:11

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