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