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
;
215 ctx
= FN(UNION
,get_ctx
)(u
);
216 domain
= isl_space_domain(isl_space_copy(space
));
219 hash
= isl_space_get_hash(domain
);
220 isl_space_free(domain
);
221 entry
= isl_hash_table_find(ctx
, &u
->table
, hash
,
222 &FN(UNION
,has_same_domain_space
), space
, reserve
);
224 return reserve
? NULL
: isl_hash_table_entry_none
;
225 if (reserve
&& !entry
->data
)
228 equal
= isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
235 return isl_hash_table_entry_none
;
236 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
237 "union expression can only contain a single "
238 "expression over a given domain", return NULL
);
241 /* Extract the element of "u" living in "space" (ignoring parameters).
243 * Return the ZERO element if "u" does not contain any element
246 __isl_give PART
*FN(FN(UNION
,extract
),PARTS
)(__isl_keep UNION
*u
,
247 __isl_take isl_space
*space
)
249 struct isl_hash_table_entry
*entry
;
253 if (!isl_space_match(u
->space
, isl_dim_param
, space
, isl_dim_param
)) {
254 space
= isl_space_drop_dims(space
, isl_dim_param
,
255 0, isl_space_dim(space
, isl_dim_param
));
256 space
= isl_space_align_params(space
,
257 FN(UNION
,get_space
)(u
));
262 entry
= FN(UNION
,find_part_entry
)(u
, space
, 0);
265 if (entry
== isl_hash_table_entry_none
)
267 return FN(PART
,ZERO
)(space
, u
->type
);
269 return FN(PART
,ZERO
)(space
);
271 isl_space_free(space
);
272 return FN(PART
,copy
)(entry
->data
);
274 isl_space_free(space
);
278 /* Add "part" to "u".
279 * If "disjoint" is set, then "u" is not allowed to already have
280 * a part that is defined on the same space as "part".
281 * Otherwise, compute the union sum of "part" and the part in "u"
282 * defined on the same space.
284 static __isl_give UNION
*FN(UNION
,add_part_generic
)(__isl_take UNION
*u
,
285 __isl_take PART
*part
, int disjoint
)
288 struct isl_hash_table_entry
*entry
;
293 empty
= FN(PART
,IS_ZERO
)(part
);
301 u
= FN(UNION
,align_params
)(u
, FN(PART
,get_space
)(part
));
302 part
= FN(PART
,align_params
)(part
, FN(UNION
,get_space
)(u
));
304 u
= FN(UNION
,cow
)(u
);
309 entry
= FN(UNION
,find_part_entry
)(u
, part
->dim
, 1);
317 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
318 "additional part should live on separate "
319 "space", goto error
);
320 entry
->data
= FN(PART
,union_add_
)(entry
->data
,
321 FN(PART
,copy
)(part
));
324 empty
= FN(PART
,IS_ZERO
)(part
);
328 FN(PART
,free
)(entry
->data
);
329 isl_hash_table_remove(u
->space
->ctx
, &u
->table
, entry
);
341 /* Add "part" to "u", where "u" is assumed not to already have
342 * a part that is defined on the same space as "part".
344 __isl_give UNION
*FN(FN(UNION
,add
),PARTS
)(__isl_take UNION
*u
,
345 __isl_take PART
*part
)
347 return FN(UNION
,add_part_generic
)(u
, part
, 1);
350 static isl_stat
FN(UNION
,add_part
)(__isl_take PART
*part
, void *user
)
352 UNION
**u
= (UNION
**)user
;
354 *u
= FN(FN(UNION
,add
),PARTS
)(*u
, part
);
359 __isl_give UNION
*FN(UNION
,dup
)(__isl_keep UNION
*u
)
367 dup
= FN(UNION
,ZERO
)(isl_space_copy(u
->space
), u
->type
);
369 dup
= FN(UNION
,ZERO
)(isl_space_copy(u
->space
));
371 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,add_part
), &dup
) < 0)
379 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
)
387 return FN(UNION
,dup
)(u
);
390 static isl_stat
FN(UNION
,free_u_entry
)(void **entry
, void *user
)
397 __isl_null UNION
*FN(UNION
,free
)(__isl_take UNION
*u
)
405 isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
406 &FN(UNION
,free_u_entry
), NULL
);
407 isl_hash_table_clear(&u
->table
);
408 isl_space_free(u
->space
);
418 static isl_stat
FN(UNION
,align_entry
)(__isl_take PART
*part
, void *user
)
421 S(UNION
,align
) *data
= user
;
423 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
424 FN(PART
,get_domain_space
)(part
));
426 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
,
427 FN(PART
,realign_domain
)(part
, exp
));
432 /* Reorder the parameters of "u" according to the given reordering.
434 static __isl_give UNION
*FN(UNION
,realign_domain
)(__isl_take UNION
*u
,
435 __isl_take isl_reordering
*r
)
437 S(UNION
,align
) data
= { NULL
, NULL
};
443 data
.res
= FN(UNION
,alloc
)(isl_space_copy(r
->dim
), u
->type
, u
->table
.n
);
445 data
.res
= FN(UNION
,alloc
)(isl_space_copy(r
->dim
), u
->table
.n
);
448 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,align_entry
), &data
) < 0)
449 data
.res
= FN(UNION
,free
)(data
.res
);
451 isl_reordering_free(data
.exp
);
456 isl_reordering_free(r
);
460 /* Align the parameters of "u" to those of "model".
462 __isl_give UNION
*FN(UNION
,align_params
)(__isl_take UNION
*u
,
463 __isl_take isl_space
*model
)
470 if (isl_space_match(u
->space
, isl_dim_param
, model
, isl_dim_param
)) {
471 isl_space_free(model
);
475 model
= isl_space_params(model
);
476 r
= isl_parameter_alignment_reordering(u
->space
, model
);
477 isl_space_free(model
);
479 return FN(UNION
,realign_domain
)(u
, r
);
481 isl_space_free(model
);
486 /* Add "part" to *u, taking the union sum if "u" already has
487 * a part defined on the same space as "part".
489 static isl_stat
FN(UNION
,union_add_part
)(__isl_take PART
*part
, void *user
)
491 UNION
**u
= (UNION
**)user
;
493 *u
= FN(UNION
,add_part_generic
)(*u
, part
, 0);
498 /* Compute the sum of "u1" and "u2" on the union of their domains,
499 * with the actual sum on the shared domain and
500 * the defined expression on the symmetric difference of the domains.
502 * This is an internal function that is exposed under different
503 * names depending on whether the base expressions have a zero default
505 * If they do, then this function is called "add".
506 * Otherwise, it is called "union_add".
508 static __isl_give UNION
*FN(UNION
,union_add_
)(__isl_take UNION
*u1
,
509 __isl_take UNION
*u2
)
511 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
512 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
514 u1
= FN(UNION
,cow
)(u1
);
519 if (FN(FN(UNION
,foreach
),PARTS
)(u2
, &FN(UNION
,union_add_part
), &u1
) < 0)
531 __isl_give UNION
*FN(FN(UNION
,from
),PARTS
)(__isl_take PART
*part
)
539 dim
= FN(PART
,get_space
)(part
);
540 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
541 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
543 u
= FN(UNION
,ZERO
)(dim
, part
->type
);
545 u
= FN(UNION
,ZERO
)(dim
);
547 u
= FN(FN(UNION
,add
),PARTS
)(u
, part
);
552 S(UNION
,match_bin_data
) {
555 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*);
558 /* Check if data->u2 has an element living in the same space as *entry.
559 * If so, call data->fn on the two elements and add the result to
562 static isl_stat
FN(UNION
,match_bin_entry
)(void **entry
, void *user
)
564 S(UNION
,match_bin_data
) *data
= user
;
565 struct isl_hash_table_entry
*entry2
;
570 space
= FN(PART
,get_space
)(part
);
571 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, space
, 0);
572 isl_space_free(space
);
574 return isl_stat_error
;
575 if (entry2
== isl_hash_table_entry_none
)
578 part2
= entry2
->data
;
579 if (!isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
580 part2
->dim
, isl_dim_out
))
581 isl_die(FN(UNION
,get_ctx
)(data
->u2
), isl_error_invalid
,
582 "entries should have the same range space",
583 return isl_stat_error
);
585 part
= FN(PART
, copy
)(part
);
586 part
= data
->fn(part
, FN(PART
, copy
)(entry2
->data
));
588 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
590 return isl_stat_error
;
595 /* This function is currently only used from isl_polynomial.c
596 * and not from isl_fold.c.
598 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
599 __isl_take UNION
*u2
,
600 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
601 __attribute__ ((unused
));
602 /* For each pair of elements in "u1" and "u2" living in the same space,
603 * call "fn" and collect the results.
605 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
606 __isl_take UNION
*u2
,
607 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
609 S(UNION
,match_bin_data
) data
= { NULL
, NULL
, fn
};
611 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
612 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
619 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u1
->space
), u1
->type
,
622 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u1
->space
), u1
->table
.n
);
624 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
625 &FN(UNION
,match_bin_entry
), &data
) < 0)
634 FN(UNION
,free
)(data
.res
);
638 /* Compute the sum of "u1" and "u2".
640 * If the base expressions have a default zero value, then the sum
641 * is computed on the union of the domains of "u1" and "u2".
642 * Otherwise, it is computed on their shared domains.
644 __isl_give UNION
*FN(UNION
,add
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
647 return FN(UNION
,union_add_
)(u1
, u2
);
649 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,add
));
654 /* Subtract "u2" from "u1" and return the result.
656 __isl_give UNION
*FN(UNION
,sub
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
658 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,sub
));
662 S(UNION
,any_set_data
) {
665 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
668 static isl_stat
FN(UNION
,any_set_entry
)(void **entry
, void *user
)
670 S(UNION
,any_set_data
) *data
= user
;
673 pw
= FN(PW
,copy
)(pw
);
674 pw
= data
->fn(pw
, isl_set_copy(data
->set
));
676 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
678 return isl_stat_error
;
683 /* Update each element of "u" by calling "fn" on the element and "set".
685 static __isl_give UNION
*FN(UNION
,any_set_op
)(__isl_take UNION
*u
,
686 __isl_take isl_set
*set
,
687 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
689 S(UNION
,any_set_data
) data
= { NULL
, NULL
, fn
};
691 u
= FN(UNION
,align_params
)(u
, isl_set_get_space(set
));
692 set
= isl_set_align_params(set
, FN(UNION
,get_space
)(u
));
699 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->type
,
702 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->table
.n
);
704 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
705 &FN(UNION
,any_set_entry
), &data
) < 0)
714 FN(UNION
,free
)(data
.res
);
718 /* Intersect the domain of "u" with the parameter domain "context".
720 __isl_give UNION
*FN(UNION
,intersect_params
)(__isl_take UNION
*u
,
721 __isl_take isl_set
*set
)
723 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,intersect_params
));
726 /* Compute the gist of the domain of "u" with respect to
727 * the parameter domain "context".
729 __isl_give UNION
*FN(UNION
,gist_params
)(__isl_take UNION
*u
,
730 __isl_take isl_set
*set
)
732 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,gist_params
));
735 S(UNION
,match_domain_data
) {
738 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
741 static int FN(UNION
,set_has_dim
)(const void *entry
, const void *val
)
743 isl_set
*set
= (isl_set
*)entry
;
744 isl_space
*dim
= (isl_space
*)val
;
746 return isl_space_is_equal(set
->dim
, dim
);
749 /* Find the set in data->uset that lives in the same space as the domain
750 * of *entry, apply data->fn to *entry and this set (if any), and add
751 * the result to data->res.
753 static isl_stat
FN(UNION
,match_domain_entry
)(void **entry
, void *user
)
755 S(UNION
,match_domain_data
) *data
= user
;
757 struct isl_hash_table_entry
*entry2
;
761 space
= FN(PW
,get_domain_space
)(pw
);
762 hash
= isl_space_get_hash(space
);
763 entry2
= isl_hash_table_find(data
->uset
->dim
->ctx
, &data
->uset
->table
,
764 hash
, &FN(UNION
,set_has_dim
), space
, 0);
765 isl_space_free(space
);
769 pw
= FN(PW
,copy
)(pw
);
770 pw
= data
->fn(pw
, isl_set_copy(entry2
->data
));
772 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
774 return isl_stat_error
;
779 /* Apply fn to each pair of PW in u and set in uset such that
780 * the set lives in the same space as the domain of PW
781 * and collect the results.
783 static __isl_give UNION
*FN(UNION
,match_domain_op
)(__isl_take UNION
*u
,
784 __isl_take isl_union_set
*uset
,
785 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
787 S(UNION
,match_domain_data
) data
= { NULL
, NULL
, fn
};
789 u
= FN(UNION
,align_params
)(u
, isl_union_set_get_space(uset
));
790 uset
= isl_union_set_align_params(uset
, FN(UNION
,get_space
)(u
));
797 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->type
,
800 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->table
.n
);
802 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
803 &FN(UNION
,match_domain_entry
), &data
) < 0)
807 isl_union_set_free(uset
);
811 isl_union_set_free(uset
);
812 FN(UNION
,free
)(data
.res
);
816 /* Intersect the domain of "u" with "uset".
817 * If "uset" is a parameters domain, then intersect the parameter
818 * domain of "u" with this set.
820 __isl_give UNION
*FN(UNION
,intersect_domain
)(__isl_take UNION
*u
,
821 __isl_take isl_union_set
*uset
)
823 if (isl_union_set_is_params(uset
))
824 return FN(UNION
,intersect_params
)(u
,
825 isl_set_from_union_set(uset
));
826 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,intersect_domain
));
829 /* Internal data structure for isl_union_*_subtract_domain.
830 * uset is the set that needs to be removed from the domain.
831 * res collects the results.
833 S(UNION
,subtract_domain_data
) {
838 /* Take the set (which may be empty) in data->uset that lives
839 * in the same space as the domain of "pw", subtract it from the domain
840 * of "pw" and add the result to data->res.
842 static isl_stat
FN(UNION
,subtract_domain_entry
)(__isl_take PW
*pw
, void *user
)
844 S(UNION
,subtract_domain_data
) *data
= user
;
848 space
= FN(PW
,get_domain_space
)(pw
);
849 set
= isl_union_set_extract_set(data
->uset
, space
);
850 pw
= FN(PW
,subtract_domain
)(pw
, set
);
851 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
856 /* Subtract "uset' from the domain of "u".
858 __isl_give UNION
*FN(UNION
,subtract_domain
)(__isl_take UNION
*u
,
859 __isl_take isl_union_set
*uset
)
861 S(UNION
,subtract_domain_data
) data
;
868 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->type
,
871 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->table
.n
);
873 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
874 &FN(UNION
,subtract_domain_entry
), &data
) < 0)
875 data
.res
= FN(UNION
,free
)(data
.res
);
878 isl_union_set_free(uset
);
882 isl_union_set_free(uset
);
886 __isl_give UNION
*FN(UNION
,gist
)(__isl_take UNION
*u
,
887 __isl_take isl_union_set
*uset
)
889 if (isl_union_set_is_params(uset
))
890 return FN(UNION
,gist_params
)(u
, isl_set_from_union_set(uset
));
891 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,gist
));
895 __isl_give isl_val
*FN(UNION
,eval
)(__isl_take UNION
*u
,
896 __isl_take isl_point
*pnt
)
899 struct isl_hash_table_entry
*entry
;
906 space
= isl_space_copy(pnt
->dim
);
909 hash
= isl_space_get_hash(space
);
910 entry
= isl_hash_table_find(u
->space
->ctx
, &u
->table
,
911 hash
, &FN(UNION
,has_domain_space
),
913 isl_space_free(space
);
915 v
= isl_val_zero(isl_point_get_ctx(pnt
));
918 v
= FN(PART
,eval
)(FN(PART
,copy
)(entry
->data
), pnt
);
929 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
930 * Since the UNION may have several references, the entry is only
931 * replaced if the coalescing is successful.
933 static isl_stat
FN(UNION
,coalesce_entry
)(void **entry
, void *user
)
935 PART
**part_p
= (PART
**) entry
;
938 part
= FN(PART
,copy
)(*part_p
);
939 part
= FN(PW
,coalesce
)(part
);
941 return isl_stat_error
;
942 FN(PART
,free
)(*part_p
);
948 __isl_give UNION
*FN(UNION
,coalesce
)(__isl_take UNION
*u
)
953 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
954 &FN(UNION
,coalesce_entry
), NULL
) < 0)
963 static isl_stat
FN(UNION
,domain_entry
)(__isl_take PART
*part
, void *user
)
965 isl_union_set
**uset
= (isl_union_set
**)user
;
967 *uset
= isl_union_set_add_set(*uset
, FN(PART
,domain
)(part
));
972 __isl_give isl_union_set
*FN(UNION
,domain
)(__isl_take UNION
*u
)
976 uset
= isl_union_set_empty(FN(UNION
,get_space
)(u
));
977 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,domain_entry
), &uset
) < 0)
984 isl_union_set_free(uset
);
989 static isl_stat
FN(UNION
,mul_isl_int_entry
)(void **entry
, void *user
)
991 PW
**pw
= (PW
**)entry
;
994 *pw
= FN(PW
,mul_isl_int
)(*pw
, *v
);
996 return isl_stat_error
;
1001 __isl_give UNION
*FN(UNION
,mul_isl_int
)(__isl_take UNION
*u
, isl_int v
)
1003 if (isl_int_is_one(v
))
1006 if (DEFAULT_IS_ZERO
&& u
&& isl_int_is_zero(v
)) {
1008 isl_space
*dim
= FN(UNION
,get_space
)(u
);
1010 zero
= FN(UNION
,ZERO
)(dim
, u
->type
);
1012 zero
= FN(UNION
,ZERO
)(dim
);
1018 u
= FN(UNION
,cow
)(u
);
1023 if (isl_int_is_neg(v
))
1024 u
->type
= isl_fold_type_negate(u
->type
);
1026 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1027 &FN(UNION
,mul_isl_int_entry
), &v
) < 0)
1036 /* Multiply *entry by the isl_val "user".
1038 * Return 0 on success and -1 on error.
1040 static isl_stat
FN(UNION
,scale_val_entry
)(void **entry
, void *user
)
1042 PW
**pw
= (PW
**)entry
;
1045 *pw
= FN(PW
,scale_val
)(*pw
, isl_val_copy(v
));
1047 return isl_stat_error
;
1052 /* Multiply "u" by "v" and return the result.
1054 __isl_give UNION
*FN(UNION
,scale_val
)(__isl_take UNION
*u
,
1055 __isl_take isl_val
*v
)
1059 if (isl_val_is_one(v
)) {
1064 if (DEFAULT_IS_ZERO
&& u
&& isl_val_is_zero(v
)) {
1066 isl_space
*space
= FN(UNION
,get_space
)(u
);
1068 zero
= FN(UNION
,ZERO
)(space
, u
->type
);
1070 zero
= FN(UNION
,ZERO
)(space
);
1077 if (!isl_val_is_rat(v
))
1078 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1079 "expecting rational factor", goto error
);
1081 u
= FN(UNION
,cow
)(u
);
1086 if (isl_val_is_neg(v
))
1087 u
->type
= isl_fold_type_negate(u
->type
);
1089 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1090 &FN(UNION
,scale_val_entry
), v
) < 0)
1101 /* Divide *entry by the isl_val "user".
1103 * Return 0 on success and -1 on error.
1105 static isl_stat
FN(UNION
,scale_down_val_entry
)(void **entry
, void *user
)
1107 PW
**pw
= (PW
**)entry
;
1110 *pw
= FN(PW
,scale_down_val
)(*pw
, isl_val_copy(v
));
1112 return isl_stat_error
;
1117 /* Divide "u" by "v" and return the result.
1119 __isl_give UNION
*FN(UNION
,scale_down_val
)(__isl_take UNION
*u
,
1120 __isl_take isl_val
*v
)
1124 if (isl_val_is_one(v
)) {
1129 if (!isl_val_is_rat(v
))
1130 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1131 "expecting rational factor", goto error
);
1132 if (isl_val_is_zero(v
))
1133 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1134 "cannot scale down by zero", goto error
);
1136 u
= FN(UNION
,cow
)(u
);
1141 if (isl_val_is_neg(v
))
1142 u
->type
= isl_fold_type_negate(u
->type
);
1144 if (isl_hash_table_foreach(FN(UNION
,get_ctx
)(u
), &u
->table
,
1145 &FN(UNION
,scale_down_val_entry
), v
) < 0)
1156 S(UNION
,plain_is_equal_data
)
1162 static isl_stat
FN(UNION
,plain_is_equal_entry
)(void **entry
, void *user
)
1164 S(UNION
,plain_is_equal_data
) *data
= user
;
1165 struct isl_hash_table_entry
*entry2
;
1168 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, pw
->dim
, 0);
1169 if (!entry2
|| entry2
== isl_hash_table_entry_none
) {
1171 data
->is_equal
= isl_bool_error
;
1173 data
->is_equal
= isl_bool_false
;
1174 return isl_stat_error
;
1177 data
->is_equal
= FN(PW
,plain_is_equal
)(pw
, entry2
->data
);
1178 if (data
->is_equal
< 0 || !data
->is_equal
)
1179 return isl_stat_error
;
1184 isl_bool
FN(UNION
,plain_is_equal
)(__isl_keep UNION
*u1
, __isl_keep UNION
*u2
)
1186 S(UNION
,plain_is_equal_data
) data
= { NULL
, isl_bool_true
};
1189 return isl_bool_error
;
1191 return isl_bool_true
;
1192 if (u1
->table
.n
!= u2
->table
.n
)
1193 return isl_bool_false
;
1195 u1
= FN(UNION
,copy
)(u1
);
1196 u2
= FN(UNION
,copy
)(u2
);
1197 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
1198 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
1203 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
1204 &FN(UNION
,plain_is_equal_entry
), &data
) < 0 &&
1211 return data
.is_equal
;
1215 return isl_bool_error
;
1219 /* Replace *entry by its opposite.
1221 * Return 0 on success and -1 on error.
1223 static isl_stat
FN(UNION
,neg_entry
)(void **entry
, void *user
)
1225 PW
**pw
= (PW
**) entry
;
1227 *pw
= FN(PW
,neg
)(*pw
);
1229 return *pw
? isl_stat_ok
: isl_stat_error
;
1232 /* Return the opposite of "u".
1234 __isl_give UNION
*FN(UNION
,neg
)(__isl_take UNION
*u
)
1236 u
= FN(UNION
,cow
)(u
);
1240 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1241 &FN(UNION
,neg_entry
), NULL
) < 0)
1242 return FN(UNION
,free
)(u
);
1248 /* Internal data structure for isl_union_*_drop_dims.
1249 * type, first and n are passed to isl_*_drop_dims.
1250 * res collects the results.
1252 S(UNION
,drop_dims_data
) {
1253 enum isl_dim_type type
;
1260 /* Drop the parameters specified by "data" from "part" and
1261 * add the results to data->res.
1263 static isl_stat
FN(UNION
,drop_dims_entry
)(__isl_take PART
*part
, void *user
)
1265 S(UNION
,drop_dims_data
) *data
= user
;
1267 part
= FN(PART
,drop_dims
)(part
, data
->type
, data
->first
, data
->n
);
1268 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
1270 return isl_stat_error
;
1275 /* Drop the specified parameters from "u".
1276 * That is, type is required to be isl_dim_param.
1278 __isl_give UNION
*FN(UNION
,drop_dims
)( __isl_take UNION
*u
,
1279 enum isl_dim_type type
, unsigned first
, unsigned n
)
1282 S(UNION
,drop_dims_data
) data
= { type
, first
, n
};
1287 if (type
!= isl_dim_param
)
1288 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1289 "can only project out parameters",
1290 return FN(UNION
,free
)(u
));
1292 space
= FN(UNION
,get_space
)(u
);
1293 space
= isl_space_drop_dims(space
, type
, first
, n
);
1295 data
.res
= FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
1297 data
.res
= FN(UNION
,alloc
)(space
, u
->table
.n
);
1299 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1300 &FN(UNION
,drop_dims_entry
), &data
) < 0)
1301 data
.res
= FN(UNION
,free
)(data
.res
);
1308 /* Internal data structure for isl_union_*_set_dim_name.
1309 * pos is the position of the parameter that needs to be renamed.
1310 * s is the new name.
1311 * res collects the results.
1313 S(UNION
,set_dim_name_data
) {
1320 /* Change the name of the parameter at position data->pos of "part" to data->s
1321 * and add the result to data->res.
1323 static isl_stat
FN(UNION
,set_dim_name_entry
)(__isl_take PART
*part
, void *user
)
1325 S(UNION
,set_dim_name_data
) *data
= user
;
1327 part
= FN(PART
,set_dim_name
)(part
, isl_dim_param
, data
->pos
, data
->s
);
1328 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
1330 return isl_stat_error
;
1335 /* Change the name of the parameter at position "pos" to "s".
1336 * That is, type is required to be isl_dim_param.
1338 __isl_give UNION
*FN(UNION
,set_dim_name
)(__isl_take UNION
*u
,
1339 enum isl_dim_type type
, unsigned pos
, const char *s
)
1341 S(UNION
,set_dim_name_data
) data
= { pos
, s
};
1347 if (type
!= isl_dim_param
)
1348 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1349 "can only set parameter names",
1350 return FN(UNION
,free
)(u
));
1352 space
= FN(UNION
,get_space
)(u
);
1353 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
1355 data
.res
= FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
1357 data
.res
= FN(UNION
,alloc
)(space
, u
->table
.n
);
1360 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1361 &FN(UNION
,set_dim_name_entry
), &data
) < 0)
1362 data
.res
= FN(UNION
,free
)(data
.res
);
1369 /* Reset the user pointer on all identifiers of parameters and tuples
1370 * of the space of "part" and add the result to *res.
1372 static isl_stat
FN(UNION
,reset_user_entry
)(__isl_take PART
*part
, void *user
)
1376 part
= FN(PART
,reset_user
)(part
);
1377 *res
= FN(FN(UNION
,add
),PARTS
)(*res
, part
);
1379 return isl_stat_error
;
1384 /* Reset the user pointer on all identifiers of parameters and tuples
1385 * of the spaces of "u".
1387 __isl_give UNION
*FN(UNION
,reset_user
)(__isl_take UNION
*u
)
1395 space
= FN(UNION
,get_space
)(u
);
1396 space
= isl_space_reset_user(space
);
1398 res
= FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
1400 res
= FN(UNION
,alloc
)(space
, u
->table
.n
);
1402 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1403 &FN(UNION
,reset_user_entry
), &res
) < 0)
1404 res
= FN(UNION
,free
)(res
);