isl_union_*_plain_is_equal: also check total number of parts
[isl.git] / isl_union_templ.c
blob9aa59749900d2ca8185e7fdf7dedbf36819e1aaf
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 /* Add "part" to "u".
271 * If "disjoint" is set, then "u" is not allowed to already have
272 * a part that is defined on the same space as "part".
273 * Otherwise, compute the union sum of "part" and the part in "u"
274 * defined on the same space.
276 static __isl_give UNION *FN(UNION,add_part_generic)(__isl_take UNION *u,
277 __isl_take PART *part, int disjoint)
279 int empty;
280 struct isl_hash_table_entry *entry;
282 if (!part)
283 goto error;
285 empty = FN(PART,IS_ZERO)(part);
286 if (empty < 0)
287 goto error;
288 if (empty) {
289 FN(PART,free)(part);
290 return u;
293 u = FN(UNION,align_params)(u, FN(PART,get_space)(part));
294 part = FN(PART,align_params)(part, FN(UNION,get_space)(u));
296 u = FN(UNION,cow)(u);
298 if (!u)
299 goto error;
301 entry = FN(UNION,find_part_entry)(u, part->dim, 1);
302 if (!entry)
303 goto error;
305 if (!entry->data)
306 entry->data = part;
307 else {
308 if (disjoint)
309 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
310 "additional part should live on separate "
311 "space", goto error);
312 entry->data = FN(PART,union_add_)(entry->data,
313 FN(PART,copy)(part));
314 if (!entry->data)
315 goto error;
316 empty = FN(PART,IS_ZERO)(part);
317 if (empty < 0)
318 goto error;
319 if (empty)
320 u = FN(UNION,remove_part_entry)(u, entry);
321 FN(PART,free)(part);
324 return u;
325 error:
326 FN(PART,free)(part);
327 FN(UNION,free)(u);
328 return NULL;
331 /* Add "part" to "u", where "u" is assumed not to already have
332 * a part that is defined on the same space as "part".
334 __isl_give UNION *FN(FN(UNION,add),PARTS)(__isl_take UNION *u,
335 __isl_take PART *part)
337 return FN(UNION,add_part_generic)(u, part, 1);
340 #ifdef HAS_TYPE
341 /* Allocate a UNION with the same type and the same size as "u" and
342 * with space "space".
344 static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
345 __isl_take isl_space *space)
347 if (!u)
348 space = isl_space_free(space);
349 return FN(UNION,alloc)(space, u->type, u->table.n);
351 #else
352 /* Allocate a UNION with the same size as "u" and with space "space".
354 static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
355 __isl_take isl_space *space)
357 if (!u)
358 space = isl_space_free(space);
359 return FN(UNION,alloc)(space, u->table.n);
361 #endif
363 /* Allocate a UNION with the same space, the same type (if any) and
364 * the same size as "u".
366 static __isl_give UNION *FN(UNION,alloc_same_size)(__isl_keep UNION *u)
368 return FN(UNION,alloc_same_size_on_space)(u, FN(UNION,get_space)(u));
371 /* Call "fn" on each part entry of "u".
373 static isl_stat FN(UNION,foreach_inplace)(__isl_keep UNION *u,
374 isl_stat (*fn)(void **part, void *user), void *user)
376 isl_ctx *ctx;
378 if (!u)
379 return isl_stat_error;
380 ctx = FN(UNION,get_ctx)(u);
381 return isl_hash_table_foreach(ctx, &u->table, fn, user);
384 /* Does "u" have a single reference?
385 * That is, can we change "u" inplace?
387 static isl_bool FN(UNION,has_single_reference)(__isl_keep UNION *u)
389 if (!u)
390 return isl_bool_error;
391 return u->ref == 1;
394 /* Internal data structure for isl_union_*_transform_space.
395 * "fn' is applied to each entry in the input.
396 * "res" collects the results.
398 S(UNION,transform_data)
400 __isl_give PART *(*fn)(__isl_take PART *part, void *user);
401 void *user;
403 UNION *res;
406 /* Apply data->fn to "part" and add the result to data->res.
408 static isl_stat FN(UNION,transform_entry)(__isl_take PART *part, void *user)
410 S(UNION,transform_data) *data = (S(UNION,transform_data) *)user;
412 part = data->fn(part, data->user);
413 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
414 if (!data->res)
415 return isl_stat_error;
417 return isl_stat_ok;
420 /* Return a UNION living in "space" that is obtained by applying "fn"
421 * to each of the entries in "u".
423 static __isl_give UNION *FN(UNION,transform_space)(__isl_take UNION *u,
424 isl_space *space,
425 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
427 S(UNION,transform_data) data = { fn, user };
429 data.res = FN(UNION,alloc_same_size_on_space)(u, space);
430 if (FN(FN(UNION,foreach),PARTS)(u,
431 &FN(UNION,transform_entry), &data) < 0)
432 data.res = FN(UNION,free)(data.res);
433 FN(UNION,free)(u);
434 return data.res;
437 /* Return a UNION that lives in the same space as "u" and that is obtained
438 * by applying "fn" to each of the entries in "u".
440 static __isl_give UNION *FN(UNION,transform)(__isl_take UNION *u,
441 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
443 return FN(UNION,transform_space)(u, FN(UNION,get_space)(u), fn, user);
446 /* Apply data->fn to *part and store the result back into *part.
448 static isl_stat FN(UNION,transform_inplace_entry)(void **part, void *user)
450 S(UNION,transform_data) *data = (S(UNION,transform_data) *) user;
452 *part = data->fn(*part, data->user);
453 if (!*part)
454 return isl_stat_error;
455 return isl_stat_ok;
458 /* Update "u" by applying "fn" to each entry.
459 * This operation is assumed not to change the number of entries nor
460 * the spaces of the entries.
462 * If there is only one reference to "u", then change "u" inplace.
463 * Otherwise, create a new UNION from "u" and discard the original.
465 static __isl_give UNION *FN(UNION,transform_inplace)(__isl_take UNION *u,
466 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
468 isl_bool single_ref;
470 single_ref = FN(UNION,has_single_reference)(u);
471 if (single_ref < 0)
472 return FN(UNION,free)(u);
473 if (single_ref) {
474 S(UNION,transform_data) data = { fn, user };
475 if (FN(UNION,foreach_inplace)(u,
476 &FN(UNION,transform_inplace_entry), &data) < 0)
477 return FN(UNION,free)(u);
478 return u;
480 return FN(UNION,transform)(u, fn, user);
483 /* An isl_union_*_transform callback for use in isl_union_*_dup
484 * that simply returns "part".
486 static __isl_give PART *FN(UNION,copy_part)(__isl_take PART *part, void *user)
488 return part;
491 __isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
493 u = FN(UNION,copy)(u);
494 return FN(UNION,transform)(u, &FN(UNION,copy_part), NULL);
497 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u)
499 if (!u)
500 return NULL;
502 if (u->ref == 1)
503 return u;
504 u->ref--;
505 return FN(UNION,dup)(u);
508 static isl_stat FN(UNION,free_u_entry)(void **entry, void *user)
510 PART *part = *entry;
511 FN(PART,free)(part);
512 return isl_stat_ok;
515 __isl_null UNION *FN(UNION,free)(__isl_take UNION *u)
517 if (!u)
518 return NULL;
520 if (--u->ref > 0)
521 return NULL;
523 isl_hash_table_foreach(u->space->ctx, &u->table,
524 &FN(UNION,free_u_entry), NULL);
525 isl_hash_table_clear(&u->table);
526 isl_space_free(u->space);
527 free(u);
528 return NULL;
531 static __isl_give PART *FN(UNION,align_entry)(__isl_take PART *part, void *user)
533 isl_reordering *exp = user;
535 exp = isl_reordering_extend_space(isl_reordering_copy(exp),
536 FN(PART,get_domain_space)(part));
537 return FN(PART,realign_domain)(part, exp);
540 /* Reorder the parameters of "u" according to the given reordering.
542 static __isl_give UNION *FN(UNION,realign_domain)(__isl_take UNION *u,
543 __isl_take isl_reordering *r)
545 isl_space *space;
547 if (!u || !r)
548 goto error;
550 space = isl_space_copy(r->dim);
551 u = FN(UNION,transform_space)(u, space, &FN(UNION,align_entry), r);
552 isl_reordering_free(r);
553 return u;
554 error:
555 FN(UNION,free)(u);
556 isl_reordering_free(r);
557 return NULL;
560 /* Align the parameters of "u" to those of "model".
562 __isl_give UNION *FN(UNION,align_params)(__isl_take UNION *u,
563 __isl_take isl_space *model)
565 isl_reordering *r;
567 if (!u || !model)
568 goto error;
570 if (isl_space_match(u->space, isl_dim_param, model, isl_dim_param)) {
571 isl_space_free(model);
572 return u;
575 model = isl_space_params(model);
576 r = isl_parameter_alignment_reordering(u->space, model);
577 isl_space_free(model);
579 return FN(UNION,realign_domain)(u, r);
580 error:
581 isl_space_free(model);
582 FN(UNION,free)(u);
583 return NULL;
586 /* Add "part" to *u, taking the union sum if "u" already has
587 * a part defined on the same space as "part".
589 static isl_stat FN(UNION,union_add_part)(__isl_take PART *part, void *user)
591 UNION **u = (UNION **)user;
593 *u = FN(UNION,add_part_generic)(*u, part, 0);
595 return isl_stat_ok;
598 /* Compute the sum of "u1" and "u2" on the union of their domains,
599 * with the actual sum on the shared domain and
600 * the defined expression on the symmetric difference of the domains.
602 * This is an internal function that is exposed under different
603 * names depending on whether the base expressions have a zero default
604 * value.
605 * If they do, then this function is called "add".
606 * Otherwise, it is called "union_add".
608 static __isl_give UNION *FN(UNION,union_add_)(__isl_take UNION *u1,
609 __isl_take UNION *u2)
611 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
612 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
614 u1 = FN(UNION,cow)(u1);
616 if (!u1 || !u2)
617 goto error;
619 if (FN(FN(UNION,foreach),PARTS)(u2, &FN(UNION,union_add_part), &u1) < 0)
620 goto error;
622 FN(UNION,free)(u2);
624 return u1;
625 error:
626 FN(UNION,free)(u1);
627 FN(UNION,free)(u2);
628 return NULL;
631 __isl_give UNION *FN(FN(UNION,from),PARTS)(__isl_take PART *part)
633 isl_space *dim;
634 UNION *u;
636 if (!part)
637 return NULL;
639 dim = FN(PART,get_space)(part);
640 dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
641 dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
642 #ifdef HAS_TYPE
643 u = FN(UNION,ZERO)(dim, part->type);
644 #else
645 u = FN(UNION,ZERO)(dim);
646 #endif
647 u = FN(FN(UNION,add),PARTS)(u, part);
649 return u;
652 S(UNION,match_bin_data) {
653 UNION *u2;
654 UNION *res;
655 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *);
658 /* Check if data->u2 has an element living in the same space as "part".
659 * If so, call data->fn on the two elements and add the result to
660 * data->res.
662 static isl_stat FN(UNION,match_bin_entry)(__isl_take PART *part, void *user)
664 S(UNION,match_bin_data) *data = user;
665 struct isl_hash_table_entry *entry2;
666 isl_space *space;
667 PART *part2;
669 space = FN(PART,get_space)(part);
670 entry2 = FN(UNION,find_part_entry)(data->u2, space, 0);
671 isl_space_free(space);
672 if (!entry2)
673 goto error;
674 if (entry2 == isl_hash_table_entry_none) {
675 FN(PART,free)(part);
676 return isl_stat_ok;
679 part2 = entry2->data;
680 if (!isl_space_tuple_is_equal(part->dim, isl_dim_out,
681 part2->dim, isl_dim_out))
682 isl_die(FN(UNION,get_ctx)(data->u2), isl_error_invalid,
683 "entries should have the same range space",
684 goto error);
686 part = data->fn(part, FN(PART, copy)(entry2->data));
688 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
689 if (!data->res)
690 return isl_stat_error;
692 return isl_stat_ok;
693 error:
694 FN(PART,free)(part);
695 return isl_stat_error;
698 /* This function is currently only used from isl_polynomial.c
699 * and not from isl_fold.c.
701 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
702 __isl_take UNION *u2,
703 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
704 __attribute__ ((unused));
705 /* For each pair of elements in "u1" and "u2" living in the same space,
706 * call "fn" and collect the results.
708 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
709 __isl_take UNION *u2,
710 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
712 S(UNION,match_bin_data) data = { NULL, NULL, fn };
714 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
715 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
717 if (!u1 || !u2)
718 goto error;
720 data.u2 = u2;
721 data.res = FN(UNION,alloc_same_size)(u1);
722 if (FN(FN(UNION,foreach),PARTS)(u1,
723 &FN(UNION,match_bin_entry), &data) < 0)
724 goto error;
726 FN(UNION,free)(u1);
727 FN(UNION,free)(u2);
728 return data.res;
729 error:
730 FN(UNION,free)(u1);
731 FN(UNION,free)(u2);
732 FN(UNION,free)(data.res);
733 return NULL;
736 /* Compute the sum of "u1" and "u2".
738 * If the base expressions have a default zero value, then the sum
739 * is computed on the union of the domains of "u1" and "u2".
740 * Otherwise, it is computed on their shared domains.
742 __isl_give UNION *FN(UNION,add)(__isl_take UNION *u1, __isl_take UNION *u2)
744 #if DEFAULT_IS_ZERO
745 return FN(UNION,union_add_)(u1, u2);
746 #else
747 return FN(UNION,match_bin_op)(u1, u2, &FN(PART,add));
748 #endif
751 #ifndef NO_SUB
752 /* Subtract "u2" from "u1" and return the result.
754 __isl_give UNION *FN(UNION,sub)(__isl_take UNION *u1, __isl_take UNION *u2)
756 return FN(UNION,match_bin_op)(u1, u2, &FN(PART,sub));
758 #endif
760 S(UNION,any_set_data) {
761 isl_set *set;
762 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
765 static __isl_give PART *FN(UNION,any_set_entry)(__isl_take PART *part,
766 void *user)
768 S(UNION,any_set_data) *data = user;
770 return data->fn(part, isl_set_copy(data->set));
773 /* Update each element of "u" by calling "fn" on the element and "set".
775 static __isl_give UNION *FN(UNION,any_set_op)(__isl_take UNION *u,
776 __isl_take isl_set *set,
777 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
779 S(UNION,any_set_data) data = { NULL, fn };
781 u = FN(UNION,align_params)(u, isl_set_get_space(set));
782 set = isl_set_align_params(set, FN(UNION,get_space)(u));
784 if (!u || !set)
785 goto error;
787 data.set = set;
788 u = FN(UNION,transform)(u, &FN(UNION,any_set_entry), &data);
789 isl_set_free(set);
790 return u;
791 error:
792 FN(UNION,free)(u);
793 isl_set_free(set);
794 return NULL;
797 /* Intersect the domain of "u" with the parameter domain "context".
799 __isl_give UNION *FN(UNION,intersect_params)(__isl_take UNION *u,
800 __isl_take isl_set *set)
802 return FN(UNION,any_set_op)(u, set, &FN(PW,intersect_params));
805 /* Compute the gist of the domain of "u" with respect to
806 * the parameter domain "context".
808 __isl_give UNION *FN(UNION,gist_params)(__isl_take UNION *u,
809 __isl_take isl_set *set)
811 return FN(UNION,any_set_op)(u, set, &FN(PW,gist_params));
814 S(UNION,match_domain_data) {
815 isl_union_set *uset;
816 UNION *res;
817 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
820 static int FN(UNION,set_has_dim)(const void *entry, const void *val)
822 isl_set *set = (isl_set *)entry;
823 isl_space *dim = (isl_space *)val;
825 return isl_space_is_equal(set->dim, dim);
828 /* Find the set in data->uset that lives in the same space as the domain
829 * of "part", apply data->fn to *entry and this set (if any), and add
830 * the result to data->res.
832 static isl_stat FN(UNION,match_domain_entry)(__isl_take PART *part, void *user)
834 S(UNION,match_domain_data) *data = user;
835 uint32_t hash;
836 struct isl_hash_table_entry *entry2;
837 isl_space *space;
839 space = FN(PART,get_domain_space)(part);
840 hash = isl_space_get_hash(space);
841 entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
842 hash, &FN(UNION,set_has_dim), space, 0);
843 isl_space_free(space);
844 if (!entry2) {
845 FN(PART,free)(part);
846 return isl_stat_ok;
849 part = data->fn(part, isl_set_copy(entry2->data));
851 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
852 if (!data->res)
853 return isl_stat_error;
855 return isl_stat_ok;
858 /* Apply fn to each pair of PW in u and set in uset such that
859 * the set lives in the same space as the domain of PW
860 * and collect the results.
862 static __isl_give UNION *FN(UNION,match_domain_op)(__isl_take UNION *u,
863 __isl_take isl_union_set *uset,
864 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
866 S(UNION,match_domain_data) data = { NULL, NULL, fn };
868 u = FN(UNION,align_params)(u, isl_union_set_get_space(uset));
869 uset = isl_union_set_align_params(uset, FN(UNION,get_space)(u));
871 if (!u || !uset)
872 goto error;
874 data.uset = uset;
875 data.res = FN(UNION,alloc_same_size)(u);
876 if (FN(FN(UNION,foreach),PARTS)(u,
877 &FN(UNION,match_domain_entry), &data) < 0)
878 goto error;
880 FN(UNION,free)(u);
881 isl_union_set_free(uset);
882 return data.res;
883 error:
884 FN(UNION,free)(u);
885 isl_union_set_free(uset);
886 FN(UNION,free)(data.res);
887 return NULL;
890 /* Intersect the domain of "u" with "uset".
891 * If "uset" is a parameters domain, then intersect the parameter
892 * domain of "u" with this set.
894 __isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
895 __isl_take isl_union_set *uset)
897 if (isl_union_set_is_params(uset))
898 return FN(UNION,intersect_params)(u,
899 isl_set_from_union_set(uset));
900 return FN(UNION,match_domain_op)(u, uset, &FN(PW,intersect_domain));
903 /* Take the set (which may be empty) in data->uset that lives
904 * in the same space as the domain of "pw", subtract it from the domain
905 * of "part" and return the result.
907 static __isl_give PART *FN(UNION,subtract_domain_entry)(__isl_take PART *part,
908 void *user)
910 isl_union_set *uset = user;
911 isl_space *space;
912 isl_set *set;
914 space = FN(PART,get_domain_space)(part);
915 set = isl_union_set_extract_set(uset, space);
916 return FN(PART,subtract_domain)(part, set);
919 /* Subtract "uset' from the domain of "u".
921 __isl_give UNION *FN(UNION,subtract_domain)(__isl_take UNION *u,
922 __isl_take isl_union_set *uset)
924 u = FN(UNION,transform)(u, &FN(UNION,subtract_domain_entry), uset);
925 isl_union_set_free(uset);
926 return u;
929 __isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
930 __isl_take isl_union_set *uset)
932 if (isl_union_set_is_params(uset))
933 return FN(UNION,gist_params)(u, isl_set_from_union_set(uset));
934 return FN(UNION,match_domain_op)(u, uset, &FN(PW,gist));
937 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
938 * Since the UNION may have several references, the entry is only
939 * replaced if the coalescing is successful.
941 static isl_stat FN(UNION,coalesce_entry)(void **entry, void *user)
943 PART **part_p = (PART **) entry;
944 PART *part;
946 part = FN(PART,copy)(*part_p);
947 part = FN(PW,coalesce)(part);
948 if (!part)
949 return isl_stat_error;
950 FN(PART,free)(*part_p);
951 *part_p = part;
953 return isl_stat_ok;
956 __isl_give UNION *FN(UNION,coalesce)(__isl_take UNION *u)
958 if (FN(UNION,foreach_inplace)(u, &FN(UNION,coalesce_entry), NULL) < 0)
959 goto error;
961 return u;
962 error:
963 FN(UNION,free)(u);
964 return NULL;
967 static isl_stat FN(UNION,domain_entry)(__isl_take PART *part, void *user)
969 isl_union_set **uset = (isl_union_set **)user;
971 *uset = isl_union_set_add_set(*uset, FN(PART,domain)(part));
973 return isl_stat_ok;
976 __isl_give isl_union_set *FN(UNION,domain)(__isl_take UNION *u)
978 isl_union_set *uset;
980 uset = isl_union_set_empty(FN(UNION,get_space)(u));
981 if (FN(FN(UNION,foreach),PARTS)(u, &FN(UNION,domain_entry), &uset) < 0)
982 goto error;
984 FN(UNION,free)(u);
986 return uset;
987 error:
988 isl_union_set_free(uset);
989 FN(UNION,free)(u);
990 return NULL;
993 #ifdef HAS_TYPE
994 /* Negate the type of "u".
996 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
998 u = FN(UNION,cow)(u);
999 if (!u)
1000 return NULL;
1001 u->type = isl_fold_type_negate(u->type);
1002 return u;
1004 #else
1005 /* Negate the type of "u".
1006 * Since "u" does not have a type, do nothing.
1008 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
1010 return u;
1012 #endif
1014 static __isl_give PART *FN(UNION,mul_isl_int_entry)(__isl_take PART *part,
1015 void *user)
1017 isl_int *v = user;
1019 return FN(PW,mul_isl_int)(part, *v);
1022 __isl_give UNION *FN(UNION,mul_isl_int)(__isl_take UNION *u, isl_int v)
1024 if (isl_int_is_one(v))
1025 return u;
1027 if (DEFAULT_IS_ZERO && u && isl_int_is_zero(v)) {
1028 UNION *zero;
1029 isl_space *dim = FN(UNION,get_space)(u);
1030 #ifdef HAS_TYPE
1031 zero = FN(UNION,ZERO)(dim, u->type);
1032 #else
1033 zero = FN(UNION,ZERO)(dim);
1034 #endif
1035 FN(UNION,free)(u);
1036 return zero;
1039 u = FN(UNION,transform_inplace)(u, &FN(UNION,mul_isl_int_entry), &v);
1040 if (isl_int_is_neg(v))
1041 u = FN(UNION,negate_type)(u);
1043 return u;
1046 /* Multiply "part" by the isl_val "user" and return the result.
1048 static __isl_give PART *FN(UNION,scale_val_entry)(__isl_take PART *part,
1049 void *user)
1051 isl_val *v = user;
1053 return FN(PART,scale_val)(part, isl_val_copy(v));
1056 /* Multiply "u" by "v" and return the result.
1058 __isl_give UNION *FN(UNION,scale_val)(__isl_take UNION *u,
1059 __isl_take isl_val *v)
1061 if (!u || !v)
1062 goto error;
1063 if (isl_val_is_one(v)) {
1064 isl_val_free(v);
1065 return u;
1068 if (DEFAULT_IS_ZERO && u && isl_val_is_zero(v)) {
1069 UNION *zero;
1070 isl_space *space = FN(UNION,get_space)(u);
1071 #ifdef HAS_TYPE
1072 zero = FN(UNION,ZERO)(space, u->type);
1073 #else
1074 zero = FN(UNION,ZERO)(space);
1075 #endif
1076 FN(UNION,free)(u);
1077 isl_val_free(v);
1078 return zero;
1081 if (!isl_val_is_rat(v))
1082 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1083 "expecting rational factor", goto error);
1085 u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_val_entry), v);
1086 if (isl_val_is_neg(v))
1087 u = FN(UNION,negate_type)(u);
1089 isl_val_free(v);
1090 return u;
1091 error:
1092 isl_val_free(v);
1093 FN(UNION,free)(u);
1094 return NULL;
1097 /* Divide "part" by the isl_val "user" and return the result.
1099 static __isl_give PART *FN(UNION,scale_down_val_entry)(__isl_take PART *part,
1100 void *user)
1102 isl_val *v = user;
1104 return FN(PART,scale_down_val)(part, isl_val_copy(v));
1107 /* Divide "u" by "v" and return the result.
1109 __isl_give UNION *FN(UNION,scale_down_val)(__isl_take UNION *u,
1110 __isl_take isl_val *v)
1112 if (!u || !v)
1113 goto error;
1114 if (isl_val_is_one(v)) {
1115 isl_val_free(v);
1116 return u;
1119 if (!isl_val_is_rat(v))
1120 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1121 "expecting rational factor", goto error);
1122 if (isl_val_is_zero(v))
1123 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1124 "cannot scale down by zero", goto error);
1126 u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_down_val_entry), v);
1127 if (isl_val_is_neg(v))
1128 u = FN(UNION,negate_type)(u);
1130 isl_val_free(v);
1131 return u;
1132 error:
1133 isl_val_free(v);
1134 FN(UNION,free)(u);
1135 return NULL;
1138 S(UNION,plain_is_equal_data)
1140 UNION *u2;
1141 isl_bool is_equal;
1144 static isl_stat FN(UNION,plain_is_equal_entry)(void **entry, void *user)
1146 S(UNION,plain_is_equal_data) *data = user;
1147 struct isl_hash_table_entry *entry2;
1148 PW *pw = *entry;
1150 entry2 = FN(UNION,find_part_entry)(data->u2, pw->dim, 0);
1151 if (!entry2 || entry2 == isl_hash_table_entry_none) {
1152 if (!entry2)
1153 data->is_equal = isl_bool_error;
1154 else
1155 data->is_equal = isl_bool_false;
1156 return isl_stat_error;
1159 data->is_equal = FN(PW,plain_is_equal)(pw, entry2->data);
1160 if (data->is_equal < 0 || !data->is_equal)
1161 return isl_stat_error;
1163 return isl_stat_ok;
1166 isl_bool FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
1168 S(UNION,plain_is_equal_data) data = { NULL, isl_bool_true };
1169 int n1, n2;
1171 if (!u1 || !u2)
1172 return isl_bool_error;
1173 if (u1 == u2)
1174 return isl_bool_true;
1175 if (u1->table.n != u2->table.n)
1176 return isl_bool_false;
1177 n1 = FN(FN(UNION,n),PARTS)(u1);
1178 n2 = FN(FN(UNION,n),PARTS)(u2);
1179 if (n1 < 0 || n2 < 0)
1180 return isl_bool_error;
1181 if (n1 != n2)
1182 return isl_bool_false;
1184 u1 = FN(UNION,copy)(u1);
1185 u2 = FN(UNION,copy)(u2);
1186 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
1187 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
1188 if (!u1 || !u2)
1189 goto error;
1191 data.u2 = u2;
1192 if (FN(UNION,foreach_inplace)(u1,
1193 &FN(UNION,plain_is_equal_entry), &data) < 0 &&
1194 data.is_equal)
1195 goto error;
1197 FN(UNION,free)(u1);
1198 FN(UNION,free)(u2);
1200 return data.is_equal;
1201 error:
1202 FN(UNION,free)(u1);
1203 FN(UNION,free)(u2);
1204 return isl_bool_error;
1207 /* Internal data structure for isl_union_*_drop_dims.
1208 * type, first and n are passed to isl_*_drop_dims.
1210 S(UNION,drop_dims_data) {
1211 enum isl_dim_type type;
1212 unsigned first;
1213 unsigned n;
1216 /* Drop the parameters specified by "data" from "part" and return the result.
1218 static __isl_give PART *FN(UNION,drop_dims_entry)(__isl_take PART *part,
1219 void *user)
1221 S(UNION,drop_dims_data) *data = user;
1223 return FN(PART,drop_dims)(part, data->type, data->first, data->n);
1226 /* Drop the specified parameters from "u".
1227 * That is, type is required to be isl_dim_param.
1229 __isl_give UNION *FN(UNION,drop_dims)( __isl_take UNION *u,
1230 enum isl_dim_type type, unsigned first, unsigned n)
1232 isl_space *space;
1233 S(UNION,drop_dims_data) data = { type, first, n };
1235 if (!u)
1236 return NULL;
1238 if (type != isl_dim_param)
1239 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1240 "can only project out parameters",
1241 return FN(UNION,free)(u));
1243 space = FN(UNION,get_space)(u);
1244 space = isl_space_drop_dims(space, type, first, n);
1245 return FN(UNION,transform_space)(u, space, &FN(UNION,drop_dims_entry),
1246 &data);
1249 /* Internal data structure for isl_union_*_set_dim_name.
1250 * pos is the position of the parameter that needs to be renamed.
1251 * s is the new name.
1253 S(UNION,set_dim_name_data) {
1254 unsigned pos;
1255 const char *s;
1258 /* Change the name of the parameter at position data->pos of "part" to data->s
1259 * and return the result.
1261 static __isl_give PART *FN(UNION,set_dim_name_entry)(__isl_take PART *part,
1262 void *user)
1264 S(UNION,set_dim_name_data) *data = user;
1266 return FN(PART,set_dim_name)(part, isl_dim_param, data->pos, data->s);
1269 /* Change the name of the parameter at position "pos" to "s".
1270 * That is, type is required to be isl_dim_param.
1272 __isl_give UNION *FN(UNION,set_dim_name)(__isl_take UNION *u,
1273 enum isl_dim_type type, unsigned pos, const char *s)
1275 S(UNION,set_dim_name_data) data = { pos, s };
1276 isl_space *space;
1278 if (!u)
1279 return NULL;
1281 if (type != isl_dim_param)
1282 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1283 "can only set parameter names",
1284 return FN(UNION,free)(u));
1286 space = FN(UNION,get_space)(u);
1287 space = isl_space_set_dim_name(space, type, pos, s);
1288 return FN(UNION,transform_space)(u, space,
1289 &FN(UNION,set_dim_name_entry), &data);
1292 /* Reset the user pointer on all identifiers of parameters and tuples
1293 * of the space of "part" and return the result.
1295 static __isl_give PART *FN(UNION,reset_user_entry)(__isl_take PART *part,
1296 void *user)
1298 return FN(PART,reset_user)(part);
1301 /* Reset the user pointer on all identifiers of parameters and tuples
1302 * of the spaces of "u".
1304 __isl_give UNION *FN(UNION,reset_user)(__isl_take UNION *u)
1306 isl_space *space;
1308 space = FN(UNION,get_space)(u);
1309 space = isl_space_reset_user(space);
1310 return FN(UNION,transform_space)(u, space, &FN(UNION,reset_user_entry),
1311 NULL);