isl_union_*_add_part_generic: check for overlapping domains instead of spaces
[isl.git] / isl_union_templ.c
blob90776a4fbcfc55beff76ac7fcbc8b0121eb6fe2d
1 /*
2 * Copyright 2010 INRIA Saclay
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_hash_private.h>
14 #include <isl_union_macro.h>
16 /* A union of expressions defined over different domain spaces.
17 * "space" describes the parameters.
18 * The entries of "table" are keyed on the domain space of the entry.
20 struct UNION {
21 int ref;
22 #ifdef HAS_TYPE
23 enum isl_fold type;
24 #endif
25 isl_space *space;
27 struct isl_hash_table table;
30 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u);
32 isl_ctx *FN(UNION,get_ctx)(__isl_keep UNION *u)
34 return u ? u->space->ctx : NULL;
37 __isl_give isl_space *FN(UNION,get_space)(__isl_keep UNION *u)
39 if (!u)
40 return NULL;
41 return isl_space_copy(u->space);
44 /* Return the number of parameters of "u", where "type"
45 * is required to be set to isl_dim_param.
47 unsigned FN(UNION,dim)(__isl_keep UNION *u, enum isl_dim_type type)
49 if (!u)
50 return 0;
52 if (type != isl_dim_param)
53 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
54 "can only reference parameters", return 0);
56 return isl_space_dim(u->space, type);
59 /* Return the position of the parameter with the given name
60 * in "u".
61 * Return -1 if no such dimension can be found.
63 int FN(UNION,find_dim_by_name)(__isl_keep UNION *u, enum isl_dim_type type,
64 const char *name)
66 if (!u)
67 return -1;
68 return isl_space_find_dim_by_name(u->space, type, name);
71 #ifdef HAS_TYPE
72 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim,
73 enum isl_fold type, int size)
74 #else
75 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim, int size)
76 #endif
78 UNION *u;
80 dim = isl_space_params(dim);
81 if (!dim)
82 return NULL;
84 u = isl_calloc_type(dim->ctx, UNION);
85 if (!u)
86 goto error;
88 u->ref = 1;
89 #ifdef HAS_TYPE
90 u->type = type;
91 #endif
92 u->space = dim;
93 if (isl_hash_table_init(dim->ctx, &u->table, size) < 0)
94 return FN(UNION,free)(u);
96 return u;
97 error:
98 isl_space_free(dim);
99 return NULL;
102 #ifdef HAS_TYPE
103 __isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
105 return FN(UNION,alloc)(dim, type, 16);
107 #else
108 __isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim)
110 return FN(UNION,alloc)(dim, 16);
112 #endif
114 __isl_give UNION *FN(UNION,copy)(__isl_keep UNION *u)
116 if (!u)
117 return NULL;
119 u->ref++;
120 return u;
123 /* Return the number of base expressions in "u".
125 int FN(FN(UNION,n),PARTS)(__isl_keep UNION *u)
127 return u ? u->table.n : 0;
130 S(UNION,foreach_data)
132 isl_stat (*fn)(__isl_take PART *part, void *user);
133 void *user;
136 static isl_stat FN(UNION,call_on_copy)(void **entry, void *user)
138 PART *part = *entry;
139 S(UNION,foreach_data) *data = (S(UNION,foreach_data) *)user;
141 part = FN(PART,copy)(part);
142 if (!part)
143 return isl_stat_error;
144 return data->fn(part, data->user);
147 isl_stat FN(FN(UNION,foreach),PARTS)(__isl_keep UNION *u,
148 isl_stat (*fn)(__isl_take PART *part, void *user), void *user)
150 S(UNION,foreach_data) data = { fn, user };
152 if (!u)
153 return isl_stat_error;
155 return isl_hash_table_foreach(u->space->ctx, &u->table,
156 &FN(UNION,call_on_copy), &data);
159 /* Is the domain space of "entry" equal to the domain of "space"?
161 static int FN(UNION,has_same_domain_space)(const void *entry, const void *val)
163 PART *part = (PART *)entry;
164 isl_space *space = (isl_space *) val;
166 if (isl_space_is_set(space))
167 return isl_space_is_set(part->dim);
169 return isl_space_tuple_is_equal(part->dim, isl_dim_in,
170 space, isl_dim_in);
173 /* Return the entry, if any, in "u" that lives in "space".
174 * If "reserve" is set, then an entry is created if it does not exist yet.
175 * Return NULL on error and isl_hash_table_entry_none if no entry was found.
176 * Note that when "reserve" is set, the function will never return
177 * isl_hash_table_entry_none.
179 * First look for the entry (if any) with the same domain space.
180 * If it exists, then check if the range space also matches.
182 static struct isl_hash_table_entry *FN(UNION,find_part_entry)(
183 __isl_keep UNION *u, __isl_keep isl_space *space, int reserve)
185 isl_ctx *ctx;
186 uint32_t hash;
187 struct isl_hash_table_entry *entry;
188 isl_bool equal;
189 PART *part;
191 if (!u || !space)
192 return NULL;
194 ctx = FN(UNION,get_ctx)(u);
195 hash = isl_space_get_domain_hash(space);
196 entry = isl_hash_table_find(ctx, &u->table, hash,
197 &FN(UNION,has_same_domain_space), space, reserve);
198 if (!entry)
199 return reserve ? NULL : isl_hash_table_entry_none;
200 if (reserve && !entry->data)
201 return entry;
202 part = entry->data;
203 equal = isl_space_tuple_is_equal(part->dim, isl_dim_out,
204 space, isl_dim_out);
205 if (equal < 0)
206 return NULL;
207 if (equal)
208 return entry;
209 if (!reserve)
210 return isl_hash_table_entry_none;
211 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
212 "union expression can only contain a single "
213 "expression over a given domain", return NULL);
216 /* Remove "part_entry" from the hash table of "u".
218 static __isl_give UNION *FN(UNION,remove_part_entry)(__isl_take UNION *u,
219 struct isl_hash_table_entry *part_entry)
221 isl_ctx *ctx;
223 if (!u || !part_entry)
224 return FN(UNION,free)(u);
226 ctx = FN(UNION,get_ctx)(u);
227 isl_hash_table_remove(ctx, &u->table, part_entry);
228 FN(PART,free)(part_entry->data);
230 return u;
233 /* Extract the element of "u" living in "space" (ignoring parameters).
235 * Return the ZERO element if "u" does not contain any element
236 * living in "space".
238 __isl_give PART *FN(FN(UNION,extract),PARTS)(__isl_keep UNION *u,
239 __isl_take isl_space *space)
241 struct isl_hash_table_entry *entry;
243 if (!u || !space)
244 goto error;
245 if (!isl_space_match(u->space, isl_dim_param, space, isl_dim_param)) {
246 space = isl_space_drop_dims(space, isl_dim_param,
247 0, isl_space_dim(space, isl_dim_param));
248 space = isl_space_align_params(space,
249 FN(UNION,get_space)(u));
250 if (!space)
251 goto error;
254 entry = FN(UNION,find_part_entry)(u, space, 0);
255 if (!entry)
256 goto error;
257 if (entry == isl_hash_table_entry_none)
258 #ifdef HAS_TYPE
259 return FN(PART,ZERO)(space, u->type);
260 #else
261 return FN(PART,ZERO)(space);
262 #endif
263 isl_space_free(space);
264 return FN(PART,copy)(entry->data);
265 error:
266 isl_space_free(space);
267 return NULL;
270 /* Check that the domain of "part" is disjoint from the domain of the entries
271 * in "u" that are defined on the same domain space, but have a different
272 * target space.
273 * Since a UNION is currently not allowed to contain two entries
274 * with the same domain space, there cannot be any such other entry.
276 static isl_stat FN(UNION,check_disjoint_domain_other)(__isl_keep UNION *u,
277 __isl_keep PART *part)
279 return isl_stat_ok;
282 /* Check that the domain of "part1" is disjoint from the domain of "part2".
283 * This check is performed before "part2" is added to a UNION to ensure
284 * that the UNION expression remains a function.
285 * Since a UNION is currently not allowed to contain two entries
286 * with the same domain space, fail unconditionally.
288 static isl_stat FN(UNION,check_disjoint_domain)(__isl_keep PART *part1,
289 __isl_keep PART *part2)
291 isl_die(FN(PART,get_ctx)(part1), isl_error_invalid,
292 "additional part should live on separate space",
293 return isl_stat_error);
296 /* Add "part" to "u".
297 * If "disjoint" is set, then "u" is not allowed to already have
298 * a part that is defined over a domain that overlaps with the domain
299 * of "part".
300 * Otherwise, compute the union sum of "part" and the part in "u"
301 * defined on the same space.
303 static __isl_give UNION *FN(UNION,add_part_generic)(__isl_take UNION *u,
304 __isl_take PART *part, int disjoint)
306 int empty;
307 struct isl_hash_table_entry *entry;
309 if (!part)
310 goto error;
312 empty = FN(PART,IS_ZERO)(part);
313 if (empty < 0)
314 goto error;
315 if (empty) {
316 FN(PART,free)(part);
317 return u;
320 u = FN(UNION,align_params)(u, FN(PART,get_space)(part));
321 part = FN(PART,align_params)(part, FN(UNION,get_space)(u));
323 u = FN(UNION,cow)(u);
325 if (!u)
326 goto error;
328 if (FN(UNION,check_disjoint_domain_other)(u, part) < 0)
329 goto error;
330 entry = FN(UNION,find_part_entry)(u, part->dim, 1);
331 if (!entry)
332 goto error;
334 if (!entry->data)
335 entry->data = part;
336 else {
337 if (disjoint &&
338 FN(UNION,check_disjoint_domain)(entry->data, part) < 0)
339 goto error;
340 entry->data = FN(PART,union_add_)(entry->data,
341 FN(PART,copy)(part));
342 if (!entry->data)
343 goto error;
344 empty = FN(PART,IS_ZERO)(part);
345 if (empty < 0)
346 goto error;
347 if (empty)
348 u = FN(UNION,remove_part_entry)(u, entry);
349 FN(PART,free)(part);
352 return u;
353 error:
354 FN(PART,free)(part);
355 FN(UNION,free)(u);
356 return NULL;
359 /* Add "part" to "u", where "u" is assumed not to already have
360 * a part that is defined on the same space as "part".
362 __isl_give UNION *FN(FN(UNION,add),PARTS)(__isl_take UNION *u,
363 __isl_take PART *part)
365 return FN(UNION,add_part_generic)(u, part, 1);
368 #ifdef HAS_TYPE
369 /* Allocate a UNION with the same type and the same size as "u" and
370 * with space "space".
372 static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
373 __isl_take isl_space *space)
375 if (!u)
376 space = isl_space_free(space);
377 return FN(UNION,alloc)(space, u->type, u->table.n);
379 #else
380 /* Allocate a UNION with the same size as "u" and with space "space".
382 static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
383 __isl_take isl_space *space)
385 if (!u)
386 space = isl_space_free(space);
387 return FN(UNION,alloc)(space, u->table.n);
389 #endif
391 /* Allocate a UNION with the same space, the same type (if any) and
392 * the same size as "u".
394 static __isl_give UNION *FN(UNION,alloc_same_size)(__isl_keep UNION *u)
396 return FN(UNION,alloc_same_size_on_space)(u, FN(UNION,get_space)(u));
399 /* Call "fn" on each part entry of "u".
401 static isl_stat FN(UNION,foreach_inplace)(__isl_keep UNION *u,
402 isl_stat (*fn)(void **part, void *user), void *user)
404 isl_ctx *ctx;
406 if (!u)
407 return isl_stat_error;
408 ctx = FN(UNION,get_ctx)(u);
409 return isl_hash_table_foreach(ctx, &u->table, fn, user);
412 /* Does "u" have a single reference?
413 * That is, can we change "u" inplace?
415 static isl_bool FN(UNION,has_single_reference)(__isl_keep UNION *u)
417 if (!u)
418 return isl_bool_error;
419 return u->ref == 1;
422 /* Internal data structure for isl_union_*_transform_space.
423 * "fn' is applied to each entry in the input.
424 * "res" collects the results.
426 S(UNION,transform_data)
428 __isl_give PART *(*fn)(__isl_take PART *part, void *user);
429 void *user;
431 UNION *res;
434 /* Apply data->fn to "part" and add the result to data->res.
436 static isl_stat FN(UNION,transform_entry)(__isl_take PART *part, void *user)
438 S(UNION,transform_data) *data = (S(UNION,transform_data) *)user;
440 part = data->fn(part, data->user);
441 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
442 if (!data->res)
443 return isl_stat_error;
445 return isl_stat_ok;
448 /* Return a UNION living in "space" that is obtained by applying "fn"
449 * to each of the entries in "u".
451 static __isl_give UNION *FN(UNION,transform_space)(__isl_take UNION *u,
452 isl_space *space,
453 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
455 S(UNION,transform_data) data = { fn, user };
457 data.res = FN(UNION,alloc_same_size_on_space)(u, space);
458 if (FN(FN(UNION,foreach),PARTS)(u,
459 &FN(UNION,transform_entry), &data) < 0)
460 data.res = FN(UNION,free)(data.res);
461 FN(UNION,free)(u);
462 return data.res;
465 /* Return a UNION that lives in the same space as "u" and that is obtained
466 * by applying "fn" to each of the entries in "u".
468 static __isl_give UNION *FN(UNION,transform)(__isl_take UNION *u,
469 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
471 return FN(UNION,transform_space)(u, FN(UNION,get_space)(u), fn, user);
474 /* Apply data->fn to *part and store the result back into *part.
476 static isl_stat FN(UNION,transform_inplace_entry)(void **part, void *user)
478 S(UNION,transform_data) *data = (S(UNION,transform_data) *) user;
480 *part = data->fn(*part, data->user);
481 if (!*part)
482 return isl_stat_error;
483 return isl_stat_ok;
486 /* Update "u" by applying "fn" to each entry.
487 * This operation is assumed not to change the number of entries nor
488 * the spaces of the entries.
490 * If there is only one reference to "u", then change "u" inplace.
491 * Otherwise, create a new UNION from "u" and discard the original.
493 static __isl_give UNION *FN(UNION,transform_inplace)(__isl_take UNION *u,
494 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
496 isl_bool single_ref;
498 single_ref = FN(UNION,has_single_reference)(u);
499 if (single_ref < 0)
500 return FN(UNION,free)(u);
501 if (single_ref) {
502 S(UNION,transform_data) data = { fn, user };
503 if (FN(UNION,foreach_inplace)(u,
504 &FN(UNION,transform_inplace_entry), &data) < 0)
505 return FN(UNION,free)(u);
506 return u;
508 return FN(UNION,transform)(u, fn, user);
511 /* An isl_union_*_transform callback for use in isl_union_*_dup
512 * that simply returns "part".
514 static __isl_give PART *FN(UNION,copy_part)(__isl_take PART *part, void *user)
516 return part;
519 __isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
521 u = FN(UNION,copy)(u);
522 return FN(UNION,transform)(u, &FN(UNION,copy_part), NULL);
525 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u)
527 if (!u)
528 return NULL;
530 if (u->ref == 1)
531 return u;
532 u->ref--;
533 return FN(UNION,dup)(u);
536 static isl_stat FN(UNION,free_u_entry)(void **entry, void *user)
538 PART *part = *entry;
539 FN(PART,free)(part);
540 return isl_stat_ok;
543 __isl_null UNION *FN(UNION,free)(__isl_take UNION *u)
545 if (!u)
546 return NULL;
548 if (--u->ref > 0)
549 return NULL;
551 isl_hash_table_foreach(u->space->ctx, &u->table,
552 &FN(UNION,free_u_entry), NULL);
553 isl_hash_table_clear(&u->table);
554 isl_space_free(u->space);
555 free(u);
556 return NULL;
559 static __isl_give PART *FN(UNION,align_entry)(__isl_take PART *part, void *user)
561 isl_reordering *exp = user;
563 exp = isl_reordering_extend_space(isl_reordering_copy(exp),
564 FN(PART,get_domain_space)(part));
565 return FN(PART,realign_domain)(part, exp);
568 /* Reorder the parameters of "u" according to the given reordering.
570 static __isl_give UNION *FN(UNION,realign_domain)(__isl_take UNION *u,
571 __isl_take isl_reordering *r)
573 isl_space *space;
575 if (!u || !r)
576 goto error;
578 space = isl_space_copy(r->dim);
579 u = FN(UNION,transform_space)(u, space, &FN(UNION,align_entry), r);
580 isl_reordering_free(r);
581 return u;
582 error:
583 FN(UNION,free)(u);
584 isl_reordering_free(r);
585 return NULL;
588 /* Align the parameters of "u" to those of "model".
590 __isl_give UNION *FN(UNION,align_params)(__isl_take UNION *u,
591 __isl_take isl_space *model)
593 isl_reordering *r;
595 if (!u || !model)
596 goto error;
598 if (isl_space_match(u->space, isl_dim_param, model, isl_dim_param)) {
599 isl_space_free(model);
600 return u;
603 model = isl_space_params(model);
604 r = isl_parameter_alignment_reordering(u->space, model);
605 isl_space_free(model);
607 return FN(UNION,realign_domain)(u, r);
608 error:
609 isl_space_free(model);
610 FN(UNION,free)(u);
611 return NULL;
614 /* Add "part" to *u, taking the union sum if "u" already has
615 * a part defined on the same space as "part".
617 static isl_stat FN(UNION,union_add_part)(__isl_take PART *part, void *user)
619 UNION **u = (UNION **)user;
621 *u = FN(UNION,add_part_generic)(*u, part, 0);
623 return isl_stat_ok;
626 /* Compute the sum of "u1" and "u2" on the union of their domains,
627 * with the actual sum on the shared domain and
628 * the defined expression on the symmetric difference of the domains.
630 * This is an internal function that is exposed under different
631 * names depending on whether the base expressions have a zero default
632 * value.
633 * If they do, then this function is called "add".
634 * Otherwise, it is called "union_add".
636 static __isl_give UNION *FN(UNION,union_add_)(__isl_take UNION *u1,
637 __isl_take UNION *u2)
639 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
640 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
642 u1 = FN(UNION,cow)(u1);
644 if (!u1 || !u2)
645 goto error;
647 if (FN(FN(UNION,foreach),PARTS)(u2, &FN(UNION,union_add_part), &u1) < 0)
648 goto error;
650 FN(UNION,free)(u2);
652 return u1;
653 error:
654 FN(UNION,free)(u1);
655 FN(UNION,free)(u2);
656 return NULL;
659 __isl_give UNION *FN(FN(UNION,from),PARTS)(__isl_take PART *part)
661 isl_space *dim;
662 UNION *u;
664 if (!part)
665 return NULL;
667 dim = FN(PART,get_space)(part);
668 dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
669 dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
670 #ifdef HAS_TYPE
671 u = FN(UNION,ZERO)(dim, part->type);
672 #else
673 u = FN(UNION,ZERO)(dim);
674 #endif
675 u = FN(FN(UNION,add),PARTS)(u, part);
677 return u;
680 S(UNION,match_bin_data) {
681 UNION *u2;
682 UNION *res;
683 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *);
686 /* Check if data->u2 has an element living in the same space as "part".
687 * If so, call data->fn on the two elements and add the result to
688 * data->res.
690 static isl_stat FN(UNION,match_bin_entry)(__isl_take PART *part, void *user)
692 S(UNION,match_bin_data) *data = user;
693 struct isl_hash_table_entry *entry2;
694 isl_space *space;
695 PART *part2;
697 space = FN(PART,get_space)(part);
698 entry2 = FN(UNION,find_part_entry)(data->u2, space, 0);
699 isl_space_free(space);
700 if (!entry2)
701 goto error;
702 if (entry2 == isl_hash_table_entry_none) {
703 FN(PART,free)(part);
704 return isl_stat_ok;
707 part2 = entry2->data;
708 if (!isl_space_tuple_is_equal(part->dim, isl_dim_out,
709 part2->dim, isl_dim_out))
710 isl_die(FN(UNION,get_ctx)(data->u2), isl_error_invalid,
711 "entries should have the same range space",
712 goto error);
714 part = data->fn(part, FN(PART, copy)(entry2->data));
716 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
717 if (!data->res)
718 return isl_stat_error;
720 return isl_stat_ok;
721 error:
722 FN(PART,free)(part);
723 return isl_stat_error;
726 /* This function is currently only used from isl_polynomial.c
727 * and not from isl_fold.c.
729 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
730 __isl_take UNION *u2,
731 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
732 __attribute__ ((unused));
733 /* For each pair of elements in "u1" and "u2" living in the same space,
734 * call "fn" and collect the results.
736 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
737 __isl_take UNION *u2,
738 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
740 S(UNION,match_bin_data) data = { NULL, NULL, fn };
742 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
743 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
745 if (!u1 || !u2)
746 goto error;
748 data.u2 = u2;
749 data.res = FN(UNION,alloc_same_size)(u1);
750 if (FN(FN(UNION,foreach),PARTS)(u1,
751 &FN(UNION,match_bin_entry), &data) < 0)
752 goto error;
754 FN(UNION,free)(u1);
755 FN(UNION,free)(u2);
756 return data.res;
757 error:
758 FN(UNION,free)(u1);
759 FN(UNION,free)(u2);
760 FN(UNION,free)(data.res);
761 return NULL;
764 /* Compute the sum of "u1" and "u2".
766 * If the base expressions have a default zero value, then the sum
767 * is computed on the union of the domains of "u1" and "u2".
768 * Otherwise, it is computed on their shared domains.
770 __isl_give UNION *FN(UNION,add)(__isl_take UNION *u1, __isl_take UNION *u2)
772 #if DEFAULT_IS_ZERO
773 return FN(UNION,union_add_)(u1, u2);
774 #else
775 return FN(UNION,match_bin_op)(u1, u2, &FN(PART,add));
776 #endif
779 #ifndef NO_SUB
780 /* Subtract "u2" from "u1" and return the result.
782 __isl_give UNION *FN(UNION,sub)(__isl_take UNION *u1, __isl_take UNION *u2)
784 return FN(UNION,match_bin_op)(u1, u2, &FN(PART,sub));
786 #endif
788 S(UNION,any_set_data) {
789 isl_set *set;
790 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
793 static __isl_give PART *FN(UNION,any_set_entry)(__isl_take PART *part,
794 void *user)
796 S(UNION,any_set_data) *data = user;
798 return data->fn(part, isl_set_copy(data->set));
801 /* Update each element of "u" by calling "fn" on the element and "set".
803 static __isl_give UNION *FN(UNION,any_set_op)(__isl_take UNION *u,
804 __isl_take isl_set *set,
805 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
807 S(UNION,any_set_data) data = { NULL, fn };
809 u = FN(UNION,align_params)(u, isl_set_get_space(set));
810 set = isl_set_align_params(set, FN(UNION,get_space)(u));
812 if (!u || !set)
813 goto error;
815 data.set = set;
816 u = FN(UNION,transform)(u, &FN(UNION,any_set_entry), &data);
817 isl_set_free(set);
818 return u;
819 error:
820 FN(UNION,free)(u);
821 isl_set_free(set);
822 return NULL;
825 /* Intersect the domain of "u" with the parameter domain "context".
827 __isl_give UNION *FN(UNION,intersect_params)(__isl_take UNION *u,
828 __isl_take isl_set *set)
830 return FN(UNION,any_set_op)(u, set, &FN(PW,intersect_params));
833 /* Compute the gist of the domain of "u" with respect to
834 * the parameter domain "context".
836 __isl_give UNION *FN(UNION,gist_params)(__isl_take UNION *u,
837 __isl_take isl_set *set)
839 return FN(UNION,any_set_op)(u, set, &FN(PW,gist_params));
842 S(UNION,match_domain_data) {
843 isl_union_set *uset;
844 UNION *res;
845 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
848 static int FN(UNION,set_has_dim)(const void *entry, const void *val)
850 isl_set *set = (isl_set *)entry;
851 isl_space *dim = (isl_space *)val;
853 return isl_space_is_equal(set->dim, dim);
856 /* Find the set in data->uset that lives in the same space as the domain
857 * of "part", apply data->fn to *entry and this set (if any), and add
858 * the result to data->res.
860 static isl_stat FN(UNION,match_domain_entry)(__isl_take PART *part, void *user)
862 S(UNION,match_domain_data) *data = user;
863 uint32_t hash;
864 struct isl_hash_table_entry *entry2;
865 isl_space *space;
867 space = FN(PART,get_domain_space)(part);
868 hash = isl_space_get_hash(space);
869 entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
870 hash, &FN(UNION,set_has_dim), space, 0);
871 isl_space_free(space);
872 if (!entry2) {
873 FN(PART,free)(part);
874 return isl_stat_ok;
877 part = data->fn(part, isl_set_copy(entry2->data));
879 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
880 if (!data->res)
881 return isl_stat_error;
883 return isl_stat_ok;
886 /* Apply fn to each pair of PW in u and set in uset such that
887 * the set lives in the same space as the domain of PW
888 * and collect the results.
890 static __isl_give UNION *FN(UNION,match_domain_op)(__isl_take UNION *u,
891 __isl_take isl_union_set *uset,
892 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
894 S(UNION,match_domain_data) data = { NULL, NULL, fn };
896 u = FN(UNION,align_params)(u, isl_union_set_get_space(uset));
897 uset = isl_union_set_align_params(uset, FN(UNION,get_space)(u));
899 if (!u || !uset)
900 goto error;
902 data.uset = uset;
903 data.res = FN(UNION,alloc_same_size)(u);
904 if (FN(FN(UNION,foreach),PARTS)(u,
905 &FN(UNION,match_domain_entry), &data) < 0)
906 goto error;
908 FN(UNION,free)(u);
909 isl_union_set_free(uset);
910 return data.res;
911 error:
912 FN(UNION,free)(u);
913 isl_union_set_free(uset);
914 FN(UNION,free)(data.res);
915 return NULL;
918 /* Intersect the domain of "u" with "uset".
919 * If "uset" is a parameters domain, then intersect the parameter
920 * domain of "u" with this set.
922 __isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
923 __isl_take isl_union_set *uset)
925 if (isl_union_set_is_params(uset))
926 return FN(UNION,intersect_params)(u,
927 isl_set_from_union_set(uset));
928 return FN(UNION,match_domain_op)(u, uset, &FN(PW,intersect_domain));
931 /* Take the set (which may be empty) in data->uset that lives
932 * in the same space as the domain of "pw", subtract it from the domain
933 * of "part" and return the result.
935 static __isl_give PART *FN(UNION,subtract_domain_entry)(__isl_take PART *part,
936 void *user)
938 isl_union_set *uset = user;
939 isl_space *space;
940 isl_set *set;
942 space = FN(PART,get_domain_space)(part);
943 set = isl_union_set_extract_set(uset, space);
944 return FN(PART,subtract_domain)(part, set);
947 /* Subtract "uset' from the domain of "u".
949 __isl_give UNION *FN(UNION,subtract_domain)(__isl_take UNION *u,
950 __isl_take isl_union_set *uset)
952 u = FN(UNION,transform)(u, &FN(UNION,subtract_domain_entry), uset);
953 isl_union_set_free(uset);
954 return u;
957 __isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
958 __isl_take isl_union_set *uset)
960 if (isl_union_set_is_params(uset))
961 return FN(UNION,gist_params)(u, isl_set_from_union_set(uset));
962 return FN(UNION,match_domain_op)(u, uset, &FN(PW,gist));
965 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
966 * Since the UNION may have several references, the entry is only
967 * replaced if the coalescing is successful.
969 static isl_stat FN(UNION,coalesce_entry)(void **entry, void *user)
971 PART **part_p = (PART **) entry;
972 PART *part;
974 part = FN(PART,copy)(*part_p);
975 part = FN(PW,coalesce)(part);
976 if (!part)
977 return isl_stat_error;
978 FN(PART,free)(*part_p);
979 *part_p = part;
981 return isl_stat_ok;
984 __isl_give UNION *FN(UNION,coalesce)(__isl_take UNION *u)
986 if (FN(UNION,foreach_inplace)(u, &FN(UNION,coalesce_entry), NULL) < 0)
987 goto error;
989 return u;
990 error:
991 FN(UNION,free)(u);
992 return NULL;
995 static isl_stat FN(UNION,domain_entry)(__isl_take PART *part, void *user)
997 isl_union_set **uset = (isl_union_set **)user;
999 *uset = isl_union_set_add_set(*uset, FN(PART,domain)(part));
1001 return isl_stat_ok;
1004 __isl_give isl_union_set *FN(UNION,domain)(__isl_take UNION *u)
1006 isl_union_set *uset;
1008 uset = isl_union_set_empty(FN(UNION,get_space)(u));
1009 if (FN(FN(UNION,foreach),PARTS)(u, &FN(UNION,domain_entry), &uset) < 0)
1010 goto error;
1012 FN(UNION,free)(u);
1014 return uset;
1015 error:
1016 isl_union_set_free(uset);
1017 FN(UNION,free)(u);
1018 return NULL;
1021 #ifdef HAS_TYPE
1022 /* Negate the type of "u".
1024 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
1026 u = FN(UNION,cow)(u);
1027 if (!u)
1028 return NULL;
1029 u->type = isl_fold_type_negate(u->type);
1030 return u;
1032 #else
1033 /* Negate the type of "u".
1034 * Since "u" does not have a type, do nothing.
1036 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
1038 return u;
1040 #endif
1042 static __isl_give PART *FN(UNION,mul_isl_int_entry)(__isl_take PART *part,
1043 void *user)
1045 isl_int *v = user;
1047 return FN(PW,mul_isl_int)(part, *v);
1050 __isl_give UNION *FN(UNION,mul_isl_int)(__isl_take UNION *u, isl_int v)
1052 if (isl_int_is_one(v))
1053 return u;
1055 if (DEFAULT_IS_ZERO && u && isl_int_is_zero(v)) {
1056 UNION *zero;
1057 isl_space *dim = FN(UNION,get_space)(u);
1058 #ifdef HAS_TYPE
1059 zero = FN(UNION,ZERO)(dim, u->type);
1060 #else
1061 zero = FN(UNION,ZERO)(dim);
1062 #endif
1063 FN(UNION,free)(u);
1064 return zero;
1067 u = FN(UNION,transform_inplace)(u, &FN(UNION,mul_isl_int_entry), &v);
1068 if (isl_int_is_neg(v))
1069 u = FN(UNION,negate_type)(u);
1071 return u;
1074 /* Multiply "part" by the isl_val "user" and return the result.
1076 static __isl_give PART *FN(UNION,scale_val_entry)(__isl_take PART *part,
1077 void *user)
1079 isl_val *v = user;
1081 return FN(PART,scale_val)(part, isl_val_copy(v));
1084 /* Multiply "u" by "v" and return the result.
1086 __isl_give UNION *FN(UNION,scale_val)(__isl_take UNION *u,
1087 __isl_take isl_val *v)
1089 if (!u || !v)
1090 goto error;
1091 if (isl_val_is_one(v)) {
1092 isl_val_free(v);
1093 return u;
1096 if (DEFAULT_IS_ZERO && u && isl_val_is_zero(v)) {
1097 UNION *zero;
1098 isl_space *space = FN(UNION,get_space)(u);
1099 #ifdef HAS_TYPE
1100 zero = FN(UNION,ZERO)(space, u->type);
1101 #else
1102 zero = FN(UNION,ZERO)(space);
1103 #endif
1104 FN(UNION,free)(u);
1105 isl_val_free(v);
1106 return zero;
1109 if (!isl_val_is_rat(v))
1110 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1111 "expecting rational factor", goto error);
1113 u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_val_entry), v);
1114 if (isl_val_is_neg(v))
1115 u = FN(UNION,negate_type)(u);
1117 isl_val_free(v);
1118 return u;
1119 error:
1120 isl_val_free(v);
1121 FN(UNION,free)(u);
1122 return NULL;
1125 /* Divide "part" by the isl_val "user" and return the result.
1127 static __isl_give PART *FN(UNION,scale_down_val_entry)(__isl_take PART *part,
1128 void *user)
1130 isl_val *v = user;
1132 return FN(PART,scale_down_val)(part, isl_val_copy(v));
1135 /* Divide "u" by "v" and return the result.
1137 __isl_give UNION *FN(UNION,scale_down_val)(__isl_take UNION *u,
1138 __isl_take isl_val *v)
1140 if (!u || !v)
1141 goto error;
1142 if (isl_val_is_one(v)) {
1143 isl_val_free(v);
1144 return u;
1147 if (!isl_val_is_rat(v))
1148 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1149 "expecting rational factor", goto error);
1150 if (isl_val_is_zero(v))
1151 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1152 "cannot scale down by zero", goto error);
1154 u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_down_val_entry), v);
1155 if (isl_val_is_neg(v))
1156 u = FN(UNION,negate_type)(u);
1158 isl_val_free(v);
1159 return u;
1160 error:
1161 isl_val_free(v);
1162 FN(UNION,free)(u);
1163 return NULL;
1166 S(UNION,plain_is_equal_data)
1168 UNION *u2;
1169 isl_bool is_equal;
1172 static isl_stat FN(UNION,plain_is_equal_entry)(void **entry, void *user)
1174 S(UNION,plain_is_equal_data) *data = user;
1175 struct isl_hash_table_entry *entry2;
1176 PW *pw = *entry;
1178 entry2 = FN(UNION,find_part_entry)(data->u2, pw->dim, 0);
1179 if (!entry2 || entry2 == isl_hash_table_entry_none) {
1180 if (!entry2)
1181 data->is_equal = isl_bool_error;
1182 else
1183 data->is_equal = isl_bool_false;
1184 return isl_stat_error;
1187 data->is_equal = FN(PW,plain_is_equal)(pw, entry2->data);
1188 if (data->is_equal < 0 || !data->is_equal)
1189 return isl_stat_error;
1191 return isl_stat_ok;
1194 isl_bool FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
1196 S(UNION,plain_is_equal_data) data = { NULL, isl_bool_true };
1197 int n1, n2;
1199 if (!u1 || !u2)
1200 return isl_bool_error;
1201 if (u1 == u2)
1202 return isl_bool_true;
1203 if (u1->table.n != u2->table.n)
1204 return isl_bool_false;
1205 n1 = FN(FN(UNION,n),PARTS)(u1);
1206 n2 = FN(FN(UNION,n),PARTS)(u2);
1207 if (n1 < 0 || n2 < 0)
1208 return isl_bool_error;
1209 if (n1 != n2)
1210 return isl_bool_false;
1212 u1 = FN(UNION,copy)(u1);
1213 u2 = FN(UNION,copy)(u2);
1214 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
1215 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
1216 if (!u1 || !u2)
1217 goto error;
1219 data.u2 = u2;
1220 if (FN(UNION,foreach_inplace)(u1,
1221 &FN(UNION,plain_is_equal_entry), &data) < 0 &&
1222 data.is_equal)
1223 goto error;
1225 FN(UNION,free)(u1);
1226 FN(UNION,free)(u2);
1228 return data.is_equal;
1229 error:
1230 FN(UNION,free)(u1);
1231 FN(UNION,free)(u2);
1232 return isl_bool_error;
1235 /* Internal data structure for isl_union_*_drop_dims.
1236 * type, first and n are passed to isl_*_drop_dims.
1238 S(UNION,drop_dims_data) {
1239 enum isl_dim_type type;
1240 unsigned first;
1241 unsigned n;
1244 /* Drop the parameters specified by "data" from "part" and return the result.
1246 static __isl_give PART *FN(UNION,drop_dims_entry)(__isl_take PART *part,
1247 void *user)
1249 S(UNION,drop_dims_data) *data = user;
1251 return FN(PART,drop_dims)(part, data->type, data->first, data->n);
1254 /* Drop the specified parameters from "u".
1255 * That is, type is required to be isl_dim_param.
1257 __isl_give UNION *FN(UNION,drop_dims)( __isl_take UNION *u,
1258 enum isl_dim_type type, unsigned first, unsigned n)
1260 isl_space *space;
1261 S(UNION,drop_dims_data) data = { type, first, n };
1263 if (!u)
1264 return NULL;
1266 if (type != isl_dim_param)
1267 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1268 "can only project out parameters",
1269 return FN(UNION,free)(u));
1271 space = FN(UNION,get_space)(u);
1272 space = isl_space_drop_dims(space, type, first, n);
1273 return FN(UNION,transform_space)(u, space, &FN(UNION,drop_dims_entry),
1274 &data);
1277 /* Internal data structure for isl_union_*_set_dim_name.
1278 * pos is the position of the parameter that needs to be renamed.
1279 * s is the new name.
1281 S(UNION,set_dim_name_data) {
1282 unsigned pos;
1283 const char *s;
1286 /* Change the name of the parameter at position data->pos of "part" to data->s
1287 * and return the result.
1289 static __isl_give PART *FN(UNION,set_dim_name_entry)(__isl_take PART *part,
1290 void *user)
1292 S(UNION,set_dim_name_data) *data = user;
1294 return FN(PART,set_dim_name)(part, isl_dim_param, data->pos, data->s);
1297 /* Change the name of the parameter at position "pos" to "s".
1298 * That is, type is required to be isl_dim_param.
1300 __isl_give UNION *FN(UNION,set_dim_name)(__isl_take UNION *u,
1301 enum isl_dim_type type, unsigned pos, const char *s)
1303 S(UNION,set_dim_name_data) data = { pos, s };
1304 isl_space *space;
1306 if (!u)
1307 return NULL;
1309 if (type != isl_dim_param)
1310 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1311 "can only set parameter names",
1312 return FN(UNION,free)(u));
1314 space = FN(UNION,get_space)(u);
1315 space = isl_space_set_dim_name(space, type, pos, s);
1316 return FN(UNION,transform_space)(u, space,
1317 &FN(UNION,set_dim_name_entry), &data);
1320 /* Reset the user pointer on all identifiers of parameters and tuples
1321 * of the space of "part" and return the result.
1323 static __isl_give PART *FN(UNION,reset_user_entry)(__isl_take PART *part,
1324 void *user)
1326 return FN(PART,reset_user)(part);
1329 /* Reset the user pointer on all identifiers of parameters and tuples
1330 * of the spaces of "u".
1332 __isl_give UNION *FN(UNION,reset_user)(__isl_take UNION *u)
1334 isl_space *space;
1336 space = FN(UNION,get_space)(u);
1337 space = isl_space_reset_user(space);
1338 return FN(UNION,transform_space)(u, space, &FN(UNION,reset_user_entry),
1339 NULL);