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,
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_hash_private.h>
15 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
16 #define FN(TYPE,NAME) xFN(TYPE,NAME)
17 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
18 #define S(TYPE,NAME) xS(TYPE,NAME)
20 /* A union of expressions defined over different domain spaces.
21 * "space" describes the parameters.
22 * The entries of "table" are keyed on the domain space of the entry.
31 struct isl_hash_table table
;
34 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
);
36 isl_ctx
*FN(UNION
,get_ctx
)(__isl_keep UNION
*u
)
38 return u
? u
->space
->ctx
: NULL
;
41 __isl_give isl_space
*FN(UNION
,get_space
)(__isl_keep UNION
*u
)
45 return isl_space_copy(u
->space
);
48 /* Return the number of parameters of "u", where "type"
49 * is required to be set to isl_dim_param.
51 unsigned FN(UNION
,dim
)(__isl_keep UNION
*u
, enum isl_dim_type type
)
56 if (type
!= isl_dim_param
)
57 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
58 "can only reference parameters", return 0);
60 return isl_space_dim(u
->space
, type
);
63 /* Return the position of the parameter with the given name
65 * Return -1 if no such dimension can be found.
67 int FN(UNION
,find_dim_by_name
)(__isl_keep UNION
*u
, enum isl_dim_type type
,
72 return isl_space_find_dim_by_name(u
->space
, type
, name
);
76 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*dim
,
77 enum isl_fold type
, int size
)
79 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*dim
, int size
)
84 dim
= isl_space_params(dim
);
88 u
= isl_calloc_type(dim
->ctx
, UNION
);
97 if (isl_hash_table_init(dim
->ctx
, &u
->table
, size
) < 0)
98 return FN(UNION
,free
)(u
);
107 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
, enum isl_fold type
)
109 return FN(UNION
,alloc
)(dim
, type
, 16);
112 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
)
114 return FN(UNION
,alloc
)(dim
, 16);
118 __isl_give UNION
*FN(UNION
,copy
)(__isl_keep UNION
*u
)
127 /* Return the number of base expressions in "u".
129 int FN(FN(UNION
,n
),PARTS
)(__isl_keep UNION
*u
)
131 return u
? u
->table
.n
: 0;
134 S(UNION
,foreach_data
)
136 isl_stat (*fn
)(__isl_take PART
*part
, void *user
);
140 static isl_stat
FN(UNION
,call_on_copy
)(void **entry
, void *user
)
143 S(UNION
,foreach_data
) *data
= (S(UNION
,foreach_data
) *)user
;
145 return data
->fn(FN(PART
,copy
)(part
), data
->user
);
148 isl_stat
FN(FN(UNION
,foreach
),PARTS
)(__isl_keep UNION
*u
,
149 isl_stat (*fn
)(__isl_take PART
*part
, void *user
), void *user
)
151 S(UNION
,foreach_data
) data
= { fn
, user
};
154 return isl_stat_error
;
156 return isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
157 &FN(UNION
,call_on_copy
), &data
);
160 /* This function is not currently used by isl_aff.c.
162 static int FN(UNION
,has_domain_space
)(const void *entry
, const void *val
)
163 __attribute__ ((unused
));
165 /* Is the domain space of "entry" equal to "space"?
167 static int FN(UNION
,has_domain_space
)(const void *entry
, const void *val
)
169 PART
*part
= (PART
*)entry
;
170 isl_space
*space
= (isl_space
*) val
;
172 if (isl_space_is_params(space
))
173 return isl_space_is_set(part
->dim
);
175 return isl_space_tuple_is_equal(part
->dim
, isl_dim_in
,
179 /* Is the domain space of "entry" equal to the domain of "space"?
181 static int FN(UNION
,has_same_domain_space
)(const void *entry
, const void *val
)
183 PART
*part
= (PART
*)entry
;
184 isl_space
*space
= (isl_space
*) val
;
186 if (isl_space_is_set(space
))
187 return isl_space_is_set(part
->dim
);
189 return isl_space_tuple_is_equal(part
->dim
, isl_dim_in
,
193 /* Return the entry, if any, in "u" that lives in "space".
194 * If "reserve" is set, then an entry is created if it does not exist yet.
195 * Return NULL on error and isl_hash_table_entry_none if no entry was found.
196 * Note that when "reserve" is set, the function will never return
197 * isl_hash_table_entry_none.
199 * First look for the entry (if any) with the same domain space.
200 * If it exists, then check if the range space also matches.
202 static struct isl_hash_table_entry
*FN(UNION
,find_part_entry
)(
203 __isl_keep UNION
*u
, __isl_keep isl_space
*space
, int reserve
)
207 struct isl_hash_table_entry
*entry
;
214 ctx
= FN(UNION
,get_ctx
)(u
);
215 hash
= isl_space_get_domain_hash(space
);
216 entry
= isl_hash_table_find(ctx
, &u
->table
, hash
,
217 &FN(UNION
,has_same_domain_space
), space
, reserve
);
219 return reserve
? NULL
: isl_hash_table_entry_none
;
220 if (reserve
&& !entry
->data
)
223 equal
= isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
230 return isl_hash_table_entry_none
;
231 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
232 "union expression can only contain a single "
233 "expression over a given domain", return NULL
);
236 /* Extract the element of "u" living in "space" (ignoring parameters).
238 * Return the ZERO element if "u" does not contain any element
241 __isl_give PART
*FN(FN(UNION
,extract
),PARTS
)(__isl_keep UNION
*u
,
242 __isl_take isl_space
*space
)
244 struct isl_hash_table_entry
*entry
;
248 if (!isl_space_match(u
->space
, isl_dim_param
, space
, isl_dim_param
)) {
249 space
= isl_space_drop_dims(space
, isl_dim_param
,
250 0, isl_space_dim(space
, isl_dim_param
));
251 space
= isl_space_align_params(space
,
252 FN(UNION
,get_space
)(u
));
257 entry
= FN(UNION
,find_part_entry
)(u
, space
, 0);
260 if (entry
== isl_hash_table_entry_none
)
262 return FN(PART
,ZERO
)(space
, u
->type
);
264 return FN(PART
,ZERO
)(space
);
266 isl_space_free(space
);
267 return FN(PART
,copy
)(entry
->data
);
269 isl_space_free(space
);
273 /* Add "part" to "u".
274 * If "disjoint" is set, then "u" is not allowed to already have
275 * a part that is defined on the same space as "part".
276 * Otherwise, compute the union sum of "part" and the part in "u"
277 * defined on the same space.
279 static __isl_give UNION
*FN(UNION
,add_part_generic
)(__isl_take UNION
*u
,
280 __isl_take PART
*part
, int disjoint
)
283 struct isl_hash_table_entry
*entry
;
288 empty
= FN(PART
,IS_ZERO
)(part
);
296 u
= FN(UNION
,align_params
)(u
, FN(PART
,get_space
)(part
));
297 part
= FN(PART
,align_params
)(part
, FN(UNION
,get_space
)(u
));
299 u
= FN(UNION
,cow
)(u
);
304 entry
= FN(UNION
,find_part_entry
)(u
, part
->dim
, 1);
312 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
313 "additional part should live on separate "
314 "space", goto error
);
315 entry
->data
= FN(PART
,union_add_
)(entry
->data
,
316 FN(PART
,copy
)(part
));
319 empty
= FN(PART
,IS_ZERO
)(part
);
323 FN(PART
,free
)(entry
->data
);
324 isl_hash_table_remove(u
->space
->ctx
, &u
->table
, entry
);
336 /* Add "part" to "u", where "u" is assumed not to already have
337 * a part that is defined on the same space as "part".
339 __isl_give UNION
*FN(FN(UNION
,add
),PARTS
)(__isl_take UNION
*u
,
340 __isl_take PART
*part
)
342 return FN(UNION
,add_part_generic
)(u
, part
, 1);
345 static isl_stat
FN(UNION
,add_part
)(__isl_take PART
*part
, void *user
)
347 UNION
**u
= (UNION
**)user
;
349 *u
= FN(FN(UNION
,add
),PARTS
)(*u
, part
);
354 __isl_give UNION
*FN(UNION
,dup
)(__isl_keep UNION
*u
)
362 dup
= FN(UNION
,ZERO
)(isl_space_copy(u
->space
), u
->type
);
364 dup
= FN(UNION
,ZERO
)(isl_space_copy(u
->space
));
366 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,add_part
), &dup
) < 0)
374 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
)
382 return FN(UNION
,dup
)(u
);
385 static isl_stat
FN(UNION
,free_u_entry
)(void **entry
, void *user
)
392 __isl_null UNION
*FN(UNION
,free
)(__isl_take UNION
*u
)
400 isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
401 &FN(UNION
,free_u_entry
), NULL
);
402 isl_hash_table_clear(&u
->table
);
403 isl_space_free(u
->space
);
413 static isl_stat
FN(UNION
,align_entry
)(__isl_take PART
*part
, void *user
)
416 S(UNION
,align
) *data
= user
;
418 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
419 FN(PART
,get_domain_space
)(part
));
421 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
,
422 FN(PART
,realign_domain
)(part
, exp
));
427 /* Reorder the parameters of "u" according to the given reordering.
429 static __isl_give UNION
*FN(UNION
,realign_domain
)(__isl_take UNION
*u
,
430 __isl_take isl_reordering
*r
)
432 S(UNION
,align
) data
= { NULL
, NULL
};
438 data
.res
= FN(UNION
,alloc
)(isl_space_copy(r
->dim
), u
->type
, u
->table
.n
);
440 data
.res
= FN(UNION
,alloc
)(isl_space_copy(r
->dim
), u
->table
.n
);
443 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,align_entry
), &data
) < 0)
444 data
.res
= FN(UNION
,free
)(data
.res
);
446 isl_reordering_free(data
.exp
);
451 isl_reordering_free(r
);
455 /* Align the parameters of "u" to those of "model".
457 __isl_give UNION
*FN(UNION
,align_params
)(__isl_take UNION
*u
,
458 __isl_take isl_space
*model
)
465 if (isl_space_match(u
->space
, isl_dim_param
, model
, isl_dim_param
)) {
466 isl_space_free(model
);
470 model
= isl_space_params(model
);
471 r
= isl_parameter_alignment_reordering(u
->space
, model
);
472 isl_space_free(model
);
474 return FN(UNION
,realign_domain
)(u
, r
);
476 isl_space_free(model
);
481 /* Add "part" to *u, taking the union sum if "u" already has
482 * a part defined on the same space as "part".
484 static isl_stat
FN(UNION
,union_add_part
)(__isl_take PART
*part
, void *user
)
486 UNION
**u
= (UNION
**)user
;
488 *u
= FN(UNION
,add_part_generic
)(*u
, part
, 0);
493 /* Compute the sum of "u1" and "u2" on the union of their domains,
494 * with the actual sum on the shared domain and
495 * the defined expression on the symmetric difference of the domains.
497 * This is an internal function that is exposed under different
498 * names depending on whether the base expressions have a zero default
500 * If they do, then this function is called "add".
501 * Otherwise, it is called "union_add".
503 static __isl_give UNION
*FN(UNION
,union_add_
)(__isl_take UNION
*u1
,
504 __isl_take UNION
*u2
)
506 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
507 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
509 u1
= FN(UNION
,cow
)(u1
);
514 if (FN(FN(UNION
,foreach
),PARTS
)(u2
, &FN(UNION
,union_add_part
), &u1
) < 0)
526 __isl_give UNION
*FN(FN(UNION
,from
),PARTS
)(__isl_take PART
*part
)
534 dim
= FN(PART
,get_space
)(part
);
535 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
536 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
538 u
= FN(UNION
,ZERO
)(dim
, part
->type
);
540 u
= FN(UNION
,ZERO
)(dim
);
542 u
= FN(FN(UNION
,add
),PARTS
)(u
, part
);
547 S(UNION
,match_bin_data
) {
550 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*);
553 /* Check if data->u2 has an element living in the same space as *entry.
554 * If so, call data->fn on the two elements and add the result to
557 static isl_stat
FN(UNION
,match_bin_entry
)(void **entry
, void *user
)
559 S(UNION
,match_bin_data
) *data
= user
;
560 struct isl_hash_table_entry
*entry2
;
565 space
= FN(PART
,get_space
)(part
);
566 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, space
, 0);
567 isl_space_free(space
);
569 return isl_stat_error
;
570 if (entry2
== isl_hash_table_entry_none
)
573 part2
= entry2
->data
;
574 if (!isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
575 part2
->dim
, isl_dim_out
))
576 isl_die(FN(UNION
,get_ctx
)(data
->u2
), isl_error_invalid
,
577 "entries should have the same range space",
578 return isl_stat_error
);
580 part
= FN(PART
, copy
)(part
);
581 part
= data
->fn(part
, FN(PART
, copy
)(entry2
->data
));
583 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
585 return isl_stat_error
;
590 /* This function is currently only used from isl_polynomial.c
591 * and not from isl_fold.c.
593 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
594 __isl_take UNION
*u2
,
595 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
596 __attribute__ ((unused
));
597 /* For each pair of elements in "u1" and "u2" living in the same space,
598 * call "fn" and collect the results.
600 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
601 __isl_take UNION
*u2
,
602 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
604 S(UNION
,match_bin_data
) data
= { NULL
, NULL
, fn
};
606 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
607 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
614 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u1
->space
), u1
->type
,
617 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u1
->space
), u1
->table
.n
);
619 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
620 &FN(UNION
,match_bin_entry
), &data
) < 0)
629 FN(UNION
,free
)(data
.res
);
633 /* Compute the sum of "u1" and "u2".
635 * If the base expressions have a default zero value, then the sum
636 * is computed on the union of the domains of "u1" and "u2".
637 * Otherwise, it is computed on their shared domains.
639 __isl_give UNION
*FN(UNION
,add
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
642 return FN(UNION
,union_add_
)(u1
, u2
);
644 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,add
));
649 /* Subtract "u2" from "u1" and return the result.
651 __isl_give UNION
*FN(UNION
,sub
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
653 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,sub
));
657 S(UNION
,any_set_data
) {
660 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
663 static isl_stat
FN(UNION
,any_set_entry
)(void **entry
, void *user
)
665 S(UNION
,any_set_data
) *data
= user
;
668 pw
= FN(PW
,copy
)(pw
);
669 pw
= data
->fn(pw
, isl_set_copy(data
->set
));
671 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
673 return isl_stat_error
;
678 /* Update each element of "u" by calling "fn" on the element and "set".
680 static __isl_give UNION
*FN(UNION
,any_set_op
)(__isl_take UNION
*u
,
681 __isl_take isl_set
*set
,
682 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
684 S(UNION
,any_set_data
) data
= { NULL
, NULL
, fn
};
686 u
= FN(UNION
,align_params
)(u
, isl_set_get_space(set
));
687 set
= isl_set_align_params(set
, FN(UNION
,get_space
)(u
));
694 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->type
,
697 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->table
.n
);
699 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
700 &FN(UNION
,any_set_entry
), &data
) < 0)
709 FN(UNION
,free
)(data
.res
);
713 /* Intersect the domain of "u" with the parameter domain "context".
715 __isl_give UNION
*FN(UNION
,intersect_params
)(__isl_take UNION
*u
,
716 __isl_take isl_set
*set
)
718 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,intersect_params
));
721 /* Compute the gist of the domain of "u" with respect to
722 * the parameter domain "context".
724 __isl_give UNION
*FN(UNION
,gist_params
)(__isl_take UNION
*u
,
725 __isl_take isl_set
*set
)
727 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,gist_params
));
730 S(UNION
,match_domain_data
) {
733 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
736 static int FN(UNION
,set_has_dim
)(const void *entry
, const void *val
)
738 isl_set
*set
= (isl_set
*)entry
;
739 isl_space
*dim
= (isl_space
*)val
;
741 return isl_space_is_equal(set
->dim
, dim
);
744 /* Find the set in data->uset that lives in the same space as the domain
745 * of *entry, apply data->fn to *entry and this set (if any), and add
746 * the result to data->res.
748 static isl_stat
FN(UNION
,match_domain_entry
)(void **entry
, void *user
)
750 S(UNION
,match_domain_data
) *data
= user
;
752 struct isl_hash_table_entry
*entry2
;
756 space
= FN(PW
,get_domain_space
)(pw
);
757 hash
= isl_space_get_hash(space
);
758 entry2
= isl_hash_table_find(data
->uset
->dim
->ctx
, &data
->uset
->table
,
759 hash
, &FN(UNION
,set_has_dim
), space
, 0);
760 isl_space_free(space
);
764 pw
= FN(PW
,copy
)(pw
);
765 pw
= data
->fn(pw
, isl_set_copy(entry2
->data
));
767 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
769 return isl_stat_error
;
774 /* Apply fn to each pair of PW in u and set in uset such that
775 * the set lives in the same space as the domain of PW
776 * and collect the results.
778 static __isl_give UNION
*FN(UNION
,match_domain_op
)(__isl_take UNION
*u
,
779 __isl_take isl_union_set
*uset
,
780 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
782 S(UNION
,match_domain_data
) data
= { NULL
, NULL
, fn
};
784 u
= FN(UNION
,align_params
)(u
, isl_union_set_get_space(uset
));
785 uset
= isl_union_set_align_params(uset
, FN(UNION
,get_space
)(u
));
792 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->type
,
795 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->table
.n
);
797 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
798 &FN(UNION
,match_domain_entry
), &data
) < 0)
802 isl_union_set_free(uset
);
806 isl_union_set_free(uset
);
807 FN(UNION
,free
)(data
.res
);
811 /* Intersect the domain of "u" with "uset".
812 * If "uset" is a parameters domain, then intersect the parameter
813 * domain of "u" with this set.
815 __isl_give UNION
*FN(UNION
,intersect_domain
)(__isl_take UNION
*u
,
816 __isl_take isl_union_set
*uset
)
818 if (isl_union_set_is_params(uset
))
819 return FN(UNION
,intersect_params
)(u
,
820 isl_set_from_union_set(uset
));
821 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,intersect_domain
));
824 /* Internal data structure for isl_union_*_subtract_domain.
825 * uset is the set that needs to be removed from the domain.
826 * res collects the results.
828 S(UNION
,subtract_domain_data
) {
833 /* Take the set (which may be empty) in data->uset that lives
834 * in the same space as the domain of "pw", subtract it from the domain
835 * of "pw" and add the result to data->res.
837 static isl_stat
FN(UNION
,subtract_domain_entry
)(__isl_take PW
*pw
, void *user
)
839 S(UNION
,subtract_domain_data
) *data
= user
;
843 space
= FN(PW
,get_domain_space
)(pw
);
844 set
= isl_union_set_extract_set(data
->uset
, space
);
845 pw
= FN(PW
,subtract_domain
)(pw
, set
);
846 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
851 /* Subtract "uset' from the domain of "u".
853 __isl_give UNION
*FN(UNION
,subtract_domain
)(__isl_take UNION
*u
,
854 __isl_take isl_union_set
*uset
)
856 S(UNION
,subtract_domain_data
) data
;
863 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->type
,
866 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->table
.n
);
868 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
869 &FN(UNION
,subtract_domain_entry
), &data
) < 0)
870 data
.res
= FN(UNION
,free
)(data
.res
);
873 isl_union_set_free(uset
);
877 isl_union_set_free(uset
);
881 __isl_give UNION
*FN(UNION
,gist
)(__isl_take UNION
*u
,
882 __isl_take isl_union_set
*uset
)
884 if (isl_union_set_is_params(uset
))
885 return FN(UNION
,gist_params
)(u
, isl_set_from_union_set(uset
));
886 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,gist
));
890 __isl_give isl_val
*FN(UNION
,eval
)(__isl_take UNION
*u
,
891 __isl_take isl_point
*pnt
)
894 struct isl_hash_table_entry
*entry
;
901 space
= isl_space_copy(pnt
->dim
);
904 hash
= isl_space_get_hash(space
);
905 entry
= isl_hash_table_find(u
->space
->ctx
, &u
->table
,
906 hash
, &FN(UNION
,has_domain_space
),
908 isl_space_free(space
);
910 v
= isl_val_zero(isl_point_get_ctx(pnt
));
913 v
= FN(PART
,eval
)(FN(PART
,copy
)(entry
->data
), pnt
);
924 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
925 * Since the UNION may have several references, the entry is only
926 * replaced if the coalescing is successful.
928 static isl_stat
FN(UNION
,coalesce_entry
)(void **entry
, void *user
)
930 PART
**part_p
= (PART
**) entry
;
933 part
= FN(PART
,copy
)(*part_p
);
934 part
= FN(PW
,coalesce
)(part
);
936 return isl_stat_error
;
937 FN(PART
,free
)(*part_p
);
943 __isl_give UNION
*FN(UNION
,coalesce
)(__isl_take UNION
*u
)
948 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
949 &FN(UNION
,coalesce_entry
), NULL
) < 0)
958 static isl_stat
FN(UNION
,domain_entry
)(__isl_take PART
*part
, void *user
)
960 isl_union_set
**uset
= (isl_union_set
**)user
;
962 *uset
= isl_union_set_add_set(*uset
, FN(PART
,domain
)(part
));
967 __isl_give isl_union_set
*FN(UNION
,domain
)(__isl_take UNION
*u
)
971 uset
= isl_union_set_empty(FN(UNION
,get_space
)(u
));
972 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,domain_entry
), &uset
) < 0)
979 isl_union_set_free(uset
);
984 static isl_stat
FN(UNION
,mul_isl_int_entry
)(void **entry
, void *user
)
986 PW
**pw
= (PW
**)entry
;
989 *pw
= FN(PW
,mul_isl_int
)(*pw
, *v
);
991 return isl_stat_error
;
996 __isl_give UNION
*FN(UNION
,mul_isl_int
)(__isl_take UNION
*u
, isl_int v
)
998 if (isl_int_is_one(v
))
1001 if (DEFAULT_IS_ZERO
&& u
&& isl_int_is_zero(v
)) {
1003 isl_space
*dim
= FN(UNION
,get_space
)(u
);
1005 zero
= FN(UNION
,ZERO
)(dim
, u
->type
);
1007 zero
= FN(UNION
,ZERO
)(dim
);
1013 u
= FN(UNION
,cow
)(u
);
1018 if (isl_int_is_neg(v
))
1019 u
->type
= isl_fold_type_negate(u
->type
);
1021 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1022 &FN(UNION
,mul_isl_int_entry
), &v
) < 0)
1031 /* Multiply *entry by the isl_val "user".
1033 * Return 0 on success and -1 on error.
1035 static isl_stat
FN(UNION
,scale_val_entry
)(void **entry
, void *user
)
1037 PW
**pw
= (PW
**)entry
;
1040 *pw
= FN(PW
,scale_val
)(*pw
, isl_val_copy(v
));
1042 return isl_stat_error
;
1047 /* Multiply "u" by "v" and return the result.
1049 __isl_give UNION
*FN(UNION
,scale_val
)(__isl_take UNION
*u
,
1050 __isl_take isl_val
*v
)
1054 if (isl_val_is_one(v
)) {
1059 if (DEFAULT_IS_ZERO
&& u
&& isl_val_is_zero(v
)) {
1061 isl_space
*space
= FN(UNION
,get_space
)(u
);
1063 zero
= FN(UNION
,ZERO
)(space
, u
->type
);
1065 zero
= FN(UNION
,ZERO
)(space
);
1072 if (!isl_val_is_rat(v
))
1073 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1074 "expecting rational factor", goto error
);
1076 u
= FN(UNION
,cow
)(u
);
1081 if (isl_val_is_neg(v
))
1082 u
->type
= isl_fold_type_negate(u
->type
);
1084 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1085 &FN(UNION
,scale_val_entry
), v
) < 0)
1096 /* Divide *entry by the isl_val "user".
1098 * Return 0 on success and -1 on error.
1100 static isl_stat
FN(UNION
,scale_down_val_entry
)(void **entry
, void *user
)
1102 PW
**pw
= (PW
**)entry
;
1105 *pw
= FN(PW
,scale_down_val
)(*pw
, isl_val_copy(v
));
1107 return isl_stat_error
;
1112 /* Divide "u" by "v" and return the result.
1114 __isl_give UNION
*FN(UNION
,scale_down_val
)(__isl_take UNION
*u
,
1115 __isl_take isl_val
*v
)
1119 if (isl_val_is_one(v
)) {
1124 if (!isl_val_is_rat(v
))
1125 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1126 "expecting rational factor", goto error
);
1127 if (isl_val_is_zero(v
))
1128 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1129 "cannot scale down by zero", goto error
);
1131 u
= FN(UNION
,cow
)(u
);
1136 if (isl_val_is_neg(v
))
1137 u
->type
= isl_fold_type_negate(u
->type
);
1139 if (isl_hash_table_foreach(FN(UNION
,get_ctx
)(u
), &u
->table
,
1140 &FN(UNION
,scale_down_val_entry
), v
) < 0)
1151 S(UNION
,plain_is_equal_data
)
1157 static isl_stat
FN(UNION
,plain_is_equal_entry
)(void **entry
, void *user
)
1159 S(UNION
,plain_is_equal_data
) *data
= user
;
1160 struct isl_hash_table_entry
*entry2
;
1163 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, pw
->dim
, 0);
1164 if (!entry2
|| entry2
== isl_hash_table_entry_none
) {
1166 data
->is_equal
= isl_bool_error
;
1168 data
->is_equal
= isl_bool_false
;
1169 return isl_stat_error
;
1172 data
->is_equal
= FN(PW
,plain_is_equal
)(pw
, entry2
->data
);
1173 if (data
->is_equal
< 0 || !data
->is_equal
)
1174 return isl_stat_error
;
1179 isl_bool
FN(UNION
,plain_is_equal
)(__isl_keep UNION
*u1
, __isl_keep UNION
*u2
)
1181 S(UNION
,plain_is_equal_data
) data
= { NULL
, isl_bool_true
};
1184 return isl_bool_error
;
1186 return isl_bool_true
;
1187 if (u1
->table
.n
!= u2
->table
.n
)
1188 return isl_bool_false
;
1190 u1
= FN(UNION
,copy
)(u1
);
1191 u2
= FN(UNION
,copy
)(u2
);
1192 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
1193 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
1198 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
1199 &FN(UNION
,plain_is_equal_entry
), &data
) < 0 &&
1206 return data
.is_equal
;
1210 return isl_bool_error
;
1214 /* Replace *entry by its opposite.
1216 * Return 0 on success and -1 on error.
1218 static isl_stat
FN(UNION
,neg_entry
)(void **entry
, void *user
)
1220 PW
**pw
= (PW
**) entry
;
1222 *pw
= FN(PW
,neg
)(*pw
);
1224 return *pw
? isl_stat_ok
: isl_stat_error
;
1227 /* Return the opposite of "u".
1229 __isl_give UNION
*FN(UNION
,neg
)(__isl_take UNION
*u
)
1231 u
= FN(UNION
,cow
)(u
);
1235 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1236 &FN(UNION
,neg_entry
), NULL
) < 0)
1237 return FN(UNION
,free
)(u
);
1243 /* Internal data structure for isl_union_*_drop_dims.
1244 * type, first and n are passed to isl_*_drop_dims.
1245 * res collects the results.
1247 S(UNION
,drop_dims_data
) {
1248 enum isl_dim_type type
;
1255 /* Drop the parameters specified by "data" from "part" and
1256 * add the results to data->res.
1258 static isl_stat
FN(UNION
,drop_dims_entry
)(__isl_take PART
*part
, void *user
)
1260 S(UNION
,drop_dims_data
) *data
= user
;
1262 part
= FN(PART
,drop_dims
)(part
, data
->type
, data
->first
, data
->n
);
1263 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
1265 return isl_stat_error
;
1270 /* Drop the specified parameters from "u".
1271 * That is, type is required to be isl_dim_param.
1273 __isl_give UNION
*FN(UNION
,drop_dims
)( __isl_take UNION
*u
,
1274 enum isl_dim_type type
, unsigned first
, unsigned n
)
1277 S(UNION
,drop_dims_data
) data
= { type
, first
, n
};
1282 if (type
!= isl_dim_param
)
1283 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1284 "can only project out parameters",
1285 return FN(UNION
,free
)(u
));
1287 space
= FN(UNION
,get_space
)(u
);
1288 space
= isl_space_drop_dims(space
, type
, first
, n
);
1290 data
.res
= FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
1292 data
.res
= FN(UNION
,alloc
)(space
, u
->table
.n
);
1294 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1295 &FN(UNION
,drop_dims_entry
), &data
) < 0)
1296 data
.res
= FN(UNION
,free
)(data
.res
);
1303 /* Internal data structure for isl_union_*_set_dim_name.
1304 * pos is the position of the parameter that needs to be renamed.
1305 * s is the new name.
1306 * res collects the results.
1308 S(UNION
,set_dim_name_data
) {
1315 /* Change the name of the parameter at position data->pos of "part" to data->s
1316 * and add the result to data->res.
1318 static isl_stat
FN(UNION
,set_dim_name_entry
)(__isl_take PART
*part
, void *user
)
1320 S(UNION
,set_dim_name_data
) *data
= user
;
1322 part
= FN(PART
,set_dim_name
)(part
, isl_dim_param
, data
->pos
, data
->s
);
1323 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
1325 return isl_stat_error
;
1330 /* Change the name of the parameter at position "pos" to "s".
1331 * That is, type is required to be isl_dim_param.
1333 __isl_give UNION
*FN(UNION
,set_dim_name
)(__isl_take UNION
*u
,
1334 enum isl_dim_type type
, unsigned pos
, const char *s
)
1336 S(UNION
,set_dim_name_data
) data
= { pos
, s
};
1342 if (type
!= isl_dim_param
)
1343 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1344 "can only set parameter names",
1345 return FN(UNION
,free
)(u
));
1347 space
= FN(UNION
,get_space
)(u
);
1348 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
1350 data
.res
= FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
1352 data
.res
= FN(UNION
,alloc
)(space
, u
->table
.n
);
1355 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1356 &FN(UNION
,set_dim_name_entry
), &data
) < 0)
1357 data
.res
= FN(UNION
,free
)(data
.res
);
1364 /* Reset the user pointer on all identifiers of parameters and tuples
1365 * of the space of "part" and add the result to *res.
1367 static isl_stat
FN(UNION
,reset_user_entry
)(__isl_take PART
*part
, void *user
)
1371 part
= FN(PART
,reset_user
)(part
);
1372 *res
= FN(FN(UNION
,add
),PARTS
)(*res
, part
);
1374 return isl_stat_error
;
1379 /* Reset the user pointer on all identifiers of parameters and tuples
1380 * of the spaces of "u".
1382 __isl_give UNION
*FN(UNION
,reset_user
)(__isl_take UNION
*u
)
1390 space
= FN(UNION
,get_space
)(u
);
1391 space
= isl_space_reset_user(space
);
1393 res
= FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
1395 res
= FN(UNION
,alloc
)(space
, u
->table
.n
);
1397 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1398 &FN(UNION
,reset_user_entry
), &res
) < 0)
1399 res
= FN(UNION
,free
)(res
);