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>
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.
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
)
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
)
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
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
,
68 return isl_space_find_dim_by_name(u
->space
, type
, name
);
72 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*dim
,
73 enum isl_fold type
, int size
)
75 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*dim
, int size
)
80 dim
= isl_space_params(dim
);
84 u
= isl_calloc_type(dim
->ctx
, UNION
);
93 if (isl_hash_table_init(dim
->ctx
, &u
->table
, size
) < 0)
94 return FN(UNION
,free
)(u
);
103 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
, enum isl_fold type
)
105 return FN(UNION
,alloc
)(dim
, type
, 16);
108 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
)
110 return FN(UNION
,alloc
)(dim
, 16);
114 __isl_give UNION
*FN(UNION
,copy
)(__isl_keep UNION
*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
);
136 static isl_stat
FN(UNION
,call_on_copy
)(void **entry
, void *user
)
139 S(UNION
,foreach_data
) *data
= (S(UNION
,foreach_data
) *)user
;
141 return data
->fn(FN(PART
,copy
)(part
), data
->user
);
144 isl_stat
FN(FN(UNION
,foreach
),PARTS
)(__isl_keep UNION
*u
,
145 isl_stat (*fn
)(__isl_take PART
*part
, void *user
), void *user
)
147 S(UNION
,foreach_data
) data
= { fn
, user
};
150 return isl_stat_error
;
152 return isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
153 &FN(UNION
,call_on_copy
), &data
);
156 /* Is the domain space of "entry" equal to the domain of "space"?
158 static int FN(UNION
,has_same_domain_space
)(const void *entry
, const void *val
)
160 PART
*part
= (PART
*)entry
;
161 isl_space
*space
= (isl_space
*) val
;
163 if (isl_space_is_set(space
))
164 return isl_space_is_set(part
->dim
);
166 return isl_space_tuple_is_equal(part
->dim
, isl_dim_in
,
170 /* Return the entry, if any, in "u" that lives in "space".
171 * If "reserve" is set, then an entry is created if it does not exist yet.
172 * Return NULL on error and isl_hash_table_entry_none if no entry was found.
173 * Note that when "reserve" is set, the function will never return
174 * isl_hash_table_entry_none.
176 * First look for the entry (if any) with the same domain space.
177 * If it exists, then check if the range space also matches.
179 static struct isl_hash_table_entry
*FN(UNION
,find_part_entry
)(
180 __isl_keep UNION
*u
, __isl_keep isl_space
*space
, int reserve
)
184 struct isl_hash_table_entry
*entry
;
191 ctx
= FN(UNION
,get_ctx
)(u
);
192 hash
= isl_space_get_domain_hash(space
);
193 entry
= isl_hash_table_find(ctx
, &u
->table
, hash
,
194 &FN(UNION
,has_same_domain_space
), space
, reserve
);
196 return reserve
? NULL
: isl_hash_table_entry_none
;
197 if (reserve
&& !entry
->data
)
200 equal
= isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
207 return isl_hash_table_entry_none
;
208 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
209 "union expression can only contain a single "
210 "expression over a given domain", return NULL
);
213 /* Extract the element of "u" living in "space" (ignoring parameters).
215 * Return the ZERO element if "u" does not contain any element
218 __isl_give PART
*FN(FN(UNION
,extract
),PARTS
)(__isl_keep UNION
*u
,
219 __isl_take isl_space
*space
)
221 struct isl_hash_table_entry
*entry
;
225 if (!isl_space_match(u
->space
, isl_dim_param
, space
, isl_dim_param
)) {
226 space
= isl_space_drop_dims(space
, isl_dim_param
,
227 0, isl_space_dim(space
, isl_dim_param
));
228 space
= isl_space_align_params(space
,
229 FN(UNION
,get_space
)(u
));
234 entry
= FN(UNION
,find_part_entry
)(u
, space
, 0);
237 if (entry
== isl_hash_table_entry_none
)
239 return FN(PART
,ZERO
)(space
, u
->type
);
241 return FN(PART
,ZERO
)(space
);
243 isl_space_free(space
);
244 return FN(PART
,copy
)(entry
->data
);
246 isl_space_free(space
);
250 /* Add "part" to "u".
251 * If "disjoint" is set, then "u" is not allowed to already have
252 * a part that is defined on the same space as "part".
253 * Otherwise, compute the union sum of "part" and the part in "u"
254 * defined on the same space.
256 static __isl_give UNION
*FN(UNION
,add_part_generic
)(__isl_take UNION
*u
,
257 __isl_take PART
*part
, int disjoint
)
260 struct isl_hash_table_entry
*entry
;
265 empty
= FN(PART
,IS_ZERO
)(part
);
273 u
= FN(UNION
,align_params
)(u
, FN(PART
,get_space
)(part
));
274 part
= FN(PART
,align_params
)(part
, FN(UNION
,get_space
)(u
));
276 u
= FN(UNION
,cow
)(u
);
281 entry
= FN(UNION
,find_part_entry
)(u
, part
->dim
, 1);
289 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
290 "additional part should live on separate "
291 "space", goto error
);
292 entry
->data
= FN(PART
,union_add_
)(entry
->data
,
293 FN(PART
,copy
)(part
));
296 empty
= FN(PART
,IS_ZERO
)(part
);
300 FN(PART
,free
)(entry
->data
);
301 isl_hash_table_remove(u
->space
->ctx
, &u
->table
, entry
);
313 /* Add "part" to "u", where "u" is assumed not to already have
314 * a part that is defined on the same space as "part".
316 __isl_give UNION
*FN(FN(UNION
,add
),PARTS
)(__isl_take UNION
*u
,
317 __isl_take PART
*part
)
319 return FN(UNION
,add_part_generic
)(u
, part
, 1);
323 /* Allocate a UNION with the same type and the same size as "u" and
324 * with space "space".
326 static __isl_give UNION
*FN(UNION
,alloc_same_size_on_space
)(__isl_keep UNION
*u
,
327 __isl_take isl_space
*space
)
330 space
= isl_space_free(space
);
331 return FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
334 /* Allocate a UNION with the same size as "u" and with space "space".
336 static __isl_give UNION
*FN(UNION
,alloc_same_size_on_space
)(__isl_keep UNION
*u
,
337 __isl_take isl_space
*space
)
340 space
= isl_space_free(space
);
341 return FN(UNION
,alloc
)(space
, u
->table
.n
);
345 /* Allocate a UNION with the same space, the same type (if any) and
346 * the same size as "u".
348 static __isl_give UNION
*FN(UNION
,alloc_same_size
)(__isl_keep UNION
*u
)
350 return FN(UNION
,alloc_same_size_on_space
)(u
, FN(UNION
,get_space
)(u
));
353 static isl_stat
FN(UNION
,add_part
)(__isl_take PART
*part
, void *user
)
355 UNION
**u
= (UNION
**)user
;
357 *u
= FN(FN(UNION
,add
),PARTS
)(*u
, part
);
362 __isl_give UNION
*FN(UNION
,dup
)(__isl_keep UNION
*u
)
369 dup
= FN(UNION
,alloc_same_size
)(u
);
370 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,add_part
), &dup
) < 0)
378 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
)
386 return FN(UNION
,dup
)(u
);
389 static isl_stat
FN(UNION
,free_u_entry
)(void **entry
, void *user
)
396 __isl_null UNION
*FN(UNION
,free
)(__isl_take UNION
*u
)
404 isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
405 &FN(UNION
,free_u_entry
), NULL
);
406 isl_hash_table_clear(&u
->table
);
407 isl_space_free(u
->space
);
417 static isl_stat
FN(UNION
,align_entry
)(__isl_take PART
*part
, void *user
)
420 S(UNION
,align
) *data
= user
;
422 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
423 FN(PART
,get_domain_space
)(part
));
425 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
,
426 FN(PART
,realign_domain
)(part
, exp
));
431 /* Reorder the parameters of "u" according to the given reordering.
433 static __isl_give UNION
*FN(UNION
,realign_domain
)(__isl_take UNION
*u
,
434 __isl_take isl_reordering
*r
)
436 S(UNION
,align
) data
= { NULL
, NULL
};
442 space
= isl_space_copy(r
->dim
);
443 data
.res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
445 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,align_entry
), &data
) < 0)
446 data
.res
= FN(UNION
,free
)(data
.res
);
448 isl_reordering_free(data
.exp
);
453 isl_reordering_free(r
);
457 /* Align the parameters of "u" to those of "model".
459 __isl_give UNION
*FN(UNION
,align_params
)(__isl_take UNION
*u
,
460 __isl_take isl_space
*model
)
467 if (isl_space_match(u
->space
, isl_dim_param
, model
, isl_dim_param
)) {
468 isl_space_free(model
);
472 model
= isl_space_params(model
);
473 r
= isl_parameter_alignment_reordering(u
->space
, model
);
474 isl_space_free(model
);
476 return FN(UNION
,realign_domain
)(u
, r
);
478 isl_space_free(model
);
483 /* Add "part" to *u, taking the union sum if "u" already has
484 * a part defined on the same space as "part".
486 static isl_stat
FN(UNION
,union_add_part
)(__isl_take PART
*part
, void *user
)
488 UNION
**u
= (UNION
**)user
;
490 *u
= FN(UNION
,add_part_generic
)(*u
, part
, 0);
495 /* Compute the sum of "u1" and "u2" on the union of their domains,
496 * with the actual sum on the shared domain and
497 * the defined expression on the symmetric difference of the domains.
499 * This is an internal function that is exposed under different
500 * names depending on whether the base expressions have a zero default
502 * If they do, then this function is called "add".
503 * Otherwise, it is called "union_add".
505 static __isl_give UNION
*FN(UNION
,union_add_
)(__isl_take UNION
*u1
,
506 __isl_take UNION
*u2
)
508 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
509 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
511 u1
= FN(UNION
,cow
)(u1
);
516 if (FN(FN(UNION
,foreach
),PARTS
)(u2
, &FN(UNION
,union_add_part
), &u1
) < 0)
528 __isl_give UNION
*FN(FN(UNION
,from
),PARTS
)(__isl_take PART
*part
)
536 dim
= FN(PART
,get_space
)(part
);
537 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
538 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
540 u
= FN(UNION
,ZERO
)(dim
, part
->type
);
542 u
= FN(UNION
,ZERO
)(dim
);
544 u
= FN(FN(UNION
,add
),PARTS
)(u
, part
);
549 S(UNION
,match_bin_data
) {
552 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*);
555 /* Check if data->u2 has an element living in the same space as *entry.
556 * If so, call data->fn on the two elements and add the result to
559 static isl_stat
FN(UNION
,match_bin_entry
)(void **entry
, void *user
)
561 S(UNION
,match_bin_data
) *data
= user
;
562 struct isl_hash_table_entry
*entry2
;
567 space
= FN(PART
,get_space
)(part
);
568 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, space
, 0);
569 isl_space_free(space
);
571 return isl_stat_error
;
572 if (entry2
== isl_hash_table_entry_none
)
575 part2
= entry2
->data
;
576 if (!isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
577 part2
->dim
, isl_dim_out
))
578 isl_die(FN(UNION
,get_ctx
)(data
->u2
), isl_error_invalid
,
579 "entries should have the same range space",
580 return isl_stat_error
);
582 part
= FN(PART
, copy
)(part
);
583 part
= data
->fn(part
, FN(PART
, copy
)(entry2
->data
));
585 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
587 return isl_stat_error
;
592 /* This function is currently only used from isl_polynomial.c
593 * and not from isl_fold.c.
595 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
596 __isl_take UNION
*u2
,
597 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
598 __attribute__ ((unused
));
599 /* For each pair of elements in "u1" and "u2" living in the same space,
600 * call "fn" and collect the results.
602 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
603 __isl_take UNION
*u2
,
604 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
606 S(UNION
,match_bin_data
) data
= { NULL
, NULL
, fn
};
608 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
609 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
615 data
.res
= FN(UNION
,alloc_same_size
)(u1
);
616 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
617 &FN(UNION
,match_bin_entry
), &data
) < 0)
626 FN(UNION
,free
)(data
.res
);
630 /* Compute the sum of "u1" and "u2".
632 * If the base expressions have a default zero value, then the sum
633 * is computed on the union of the domains of "u1" and "u2".
634 * Otherwise, it is computed on their shared domains.
636 __isl_give UNION
*FN(UNION
,add
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
639 return FN(UNION
,union_add_
)(u1
, u2
);
641 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,add
));
646 /* Subtract "u2" from "u1" and return the result.
648 __isl_give UNION
*FN(UNION
,sub
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
650 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,sub
));
654 S(UNION
,any_set_data
) {
657 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
660 static isl_stat
FN(UNION
,any_set_entry
)(void **entry
, void *user
)
662 S(UNION
,any_set_data
) *data
= user
;
665 pw
= FN(PW
,copy
)(pw
);
666 pw
= data
->fn(pw
, isl_set_copy(data
->set
));
668 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
670 return isl_stat_error
;
675 /* Update each element of "u" by calling "fn" on the element and "set".
677 static __isl_give UNION
*FN(UNION
,any_set_op
)(__isl_take UNION
*u
,
678 __isl_take isl_set
*set
,
679 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
681 S(UNION
,any_set_data
) data
= { NULL
, NULL
, fn
};
683 u
= FN(UNION
,align_params
)(u
, isl_set_get_space(set
));
684 set
= isl_set_align_params(set
, FN(UNION
,get_space
)(u
));
690 data
.res
= FN(UNION
,alloc_same_size
)(u
);
691 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
692 &FN(UNION
,any_set_entry
), &data
) < 0)
701 FN(UNION
,free
)(data
.res
);
705 /* Intersect the domain of "u" with the parameter domain "context".
707 __isl_give UNION
*FN(UNION
,intersect_params
)(__isl_take UNION
*u
,
708 __isl_take isl_set
*set
)
710 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,intersect_params
));
713 /* Compute the gist of the domain of "u" with respect to
714 * the parameter domain "context".
716 __isl_give UNION
*FN(UNION
,gist_params
)(__isl_take UNION
*u
,
717 __isl_take isl_set
*set
)
719 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,gist_params
));
722 S(UNION
,match_domain_data
) {
725 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
728 static int FN(UNION
,set_has_dim
)(const void *entry
, const void *val
)
730 isl_set
*set
= (isl_set
*)entry
;
731 isl_space
*dim
= (isl_space
*)val
;
733 return isl_space_is_equal(set
->dim
, dim
);
736 /* Find the set in data->uset that lives in the same space as the domain
737 * of *entry, apply data->fn to *entry and this set (if any), and add
738 * the result to data->res.
740 static isl_stat
FN(UNION
,match_domain_entry
)(void **entry
, void *user
)
742 S(UNION
,match_domain_data
) *data
= user
;
744 struct isl_hash_table_entry
*entry2
;
748 space
= FN(PW
,get_domain_space
)(pw
);
749 hash
= isl_space_get_hash(space
);
750 entry2
= isl_hash_table_find(data
->uset
->dim
->ctx
, &data
->uset
->table
,
751 hash
, &FN(UNION
,set_has_dim
), space
, 0);
752 isl_space_free(space
);
756 pw
= FN(PW
,copy
)(pw
);
757 pw
= data
->fn(pw
, isl_set_copy(entry2
->data
));
759 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
761 return isl_stat_error
;
766 /* Apply fn to each pair of PW in u and set in uset such that
767 * the set lives in the same space as the domain of PW
768 * and collect the results.
770 static __isl_give UNION
*FN(UNION
,match_domain_op
)(__isl_take UNION
*u
,
771 __isl_take isl_union_set
*uset
,
772 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
774 S(UNION
,match_domain_data
) data
= { NULL
, NULL
, fn
};
776 u
= FN(UNION
,align_params
)(u
, isl_union_set_get_space(uset
));
777 uset
= isl_union_set_align_params(uset
, FN(UNION
,get_space
)(u
));
783 data
.res
= FN(UNION
,alloc_same_size
)(u
);
784 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
785 &FN(UNION
,match_domain_entry
), &data
) < 0)
789 isl_union_set_free(uset
);
793 isl_union_set_free(uset
);
794 FN(UNION
,free
)(data
.res
);
798 /* Intersect the domain of "u" with "uset".
799 * If "uset" is a parameters domain, then intersect the parameter
800 * domain of "u" with this set.
802 __isl_give UNION
*FN(UNION
,intersect_domain
)(__isl_take UNION
*u
,
803 __isl_take isl_union_set
*uset
)
805 if (isl_union_set_is_params(uset
))
806 return FN(UNION
,intersect_params
)(u
,
807 isl_set_from_union_set(uset
));
808 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,intersect_domain
));
811 /* Internal data structure for isl_union_*_subtract_domain.
812 * uset is the set that needs to be removed from the domain.
813 * res collects the results.
815 S(UNION
,subtract_domain_data
) {
820 /* Take the set (which may be empty) in data->uset that lives
821 * in the same space as the domain of "pw", subtract it from the domain
822 * of "pw" and add the result to data->res.
824 static isl_stat
FN(UNION
,subtract_domain_entry
)(__isl_take PW
*pw
, void *user
)
826 S(UNION
,subtract_domain_data
) *data
= user
;
830 space
= FN(PW
,get_domain_space
)(pw
);
831 set
= isl_union_set_extract_set(data
->uset
, space
);
832 pw
= FN(PW
,subtract_domain
)(pw
, set
);
833 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
838 /* Subtract "uset' from the domain of "u".
840 __isl_give UNION
*FN(UNION
,subtract_domain
)(__isl_take UNION
*u
,
841 __isl_take isl_union_set
*uset
)
843 S(UNION
,subtract_domain_data
) data
;
849 data
.res
= FN(UNION
,alloc_same_size
)(u
);
850 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
851 &FN(UNION
,subtract_domain_entry
), &data
) < 0)
852 data
.res
= FN(UNION
,free
)(data
.res
);
855 isl_union_set_free(uset
);
859 isl_union_set_free(uset
);
863 __isl_give UNION
*FN(UNION
,gist
)(__isl_take UNION
*u
,
864 __isl_take isl_union_set
*uset
)
866 if (isl_union_set_is_params(uset
))
867 return FN(UNION
,gist_params
)(u
, isl_set_from_union_set(uset
));
868 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,gist
));
871 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
872 * Since the UNION may have several references, the entry is only
873 * replaced if the coalescing is successful.
875 static isl_stat
FN(UNION
,coalesce_entry
)(void **entry
, void *user
)
877 PART
**part_p
= (PART
**) entry
;
880 part
= FN(PART
,copy
)(*part_p
);
881 part
= FN(PW
,coalesce
)(part
);
883 return isl_stat_error
;
884 FN(PART
,free
)(*part_p
);
890 __isl_give UNION
*FN(UNION
,coalesce
)(__isl_take UNION
*u
)
895 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
896 &FN(UNION
,coalesce_entry
), NULL
) < 0)
905 static isl_stat
FN(UNION
,domain_entry
)(__isl_take PART
*part
, void *user
)
907 isl_union_set
**uset
= (isl_union_set
**)user
;
909 *uset
= isl_union_set_add_set(*uset
, FN(PART
,domain
)(part
));
914 __isl_give isl_union_set
*FN(UNION
,domain
)(__isl_take UNION
*u
)
918 uset
= isl_union_set_empty(FN(UNION
,get_space
)(u
));
919 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,domain_entry
), &uset
) < 0)
926 isl_union_set_free(uset
);
931 static isl_stat
FN(UNION
,mul_isl_int_entry
)(void **entry
, void *user
)
933 PW
**pw
= (PW
**)entry
;
936 *pw
= FN(PW
,mul_isl_int
)(*pw
, *v
);
938 return isl_stat_error
;
943 __isl_give UNION
*FN(UNION
,mul_isl_int
)(__isl_take UNION
*u
, isl_int v
)
945 if (isl_int_is_one(v
))
948 if (DEFAULT_IS_ZERO
&& u
&& isl_int_is_zero(v
)) {
950 isl_space
*dim
= FN(UNION
,get_space
)(u
);
952 zero
= FN(UNION
,ZERO
)(dim
, u
->type
);
954 zero
= FN(UNION
,ZERO
)(dim
);
960 u
= FN(UNION
,cow
)(u
);
965 if (isl_int_is_neg(v
))
966 u
->type
= isl_fold_type_negate(u
->type
);
968 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
969 &FN(UNION
,mul_isl_int_entry
), &v
) < 0)
978 /* Multiply *entry by the isl_val "user".
980 * Return 0 on success and -1 on error.
982 static isl_stat
FN(UNION
,scale_val_entry
)(void **entry
, void *user
)
984 PW
**pw
= (PW
**)entry
;
987 *pw
= FN(PW
,scale_val
)(*pw
, isl_val_copy(v
));
989 return isl_stat_error
;
994 /* Multiply "u" by "v" and return the result.
996 __isl_give UNION
*FN(UNION
,scale_val
)(__isl_take UNION
*u
,
997 __isl_take isl_val
*v
)
1001 if (isl_val_is_one(v
)) {
1006 if (DEFAULT_IS_ZERO
&& u
&& isl_val_is_zero(v
)) {
1008 isl_space
*space
= FN(UNION
,get_space
)(u
);
1010 zero
= FN(UNION
,ZERO
)(space
, u
->type
);
1012 zero
= FN(UNION
,ZERO
)(space
);
1019 if (!isl_val_is_rat(v
))
1020 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1021 "expecting rational factor", goto error
);
1023 u
= FN(UNION
,cow
)(u
);
1028 if (isl_val_is_neg(v
))
1029 u
->type
= isl_fold_type_negate(u
->type
);
1031 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1032 &FN(UNION
,scale_val_entry
), v
) < 0)
1043 /* Divide *entry by the isl_val "user".
1045 * Return 0 on success and -1 on error.
1047 static isl_stat
FN(UNION
,scale_down_val_entry
)(void **entry
, void *user
)
1049 PW
**pw
= (PW
**)entry
;
1052 *pw
= FN(PW
,scale_down_val
)(*pw
, isl_val_copy(v
));
1054 return isl_stat_error
;
1059 /* Divide "u" by "v" and return the result.
1061 __isl_give UNION
*FN(UNION
,scale_down_val
)(__isl_take UNION
*u
,
1062 __isl_take isl_val
*v
)
1066 if (isl_val_is_one(v
)) {
1071 if (!isl_val_is_rat(v
))
1072 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1073 "expecting rational factor", goto error
);
1074 if (isl_val_is_zero(v
))
1075 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1076 "cannot scale down by zero", goto error
);
1078 u
= FN(UNION
,cow
)(u
);
1083 if (isl_val_is_neg(v
))
1084 u
->type
= isl_fold_type_negate(u
->type
);
1086 if (isl_hash_table_foreach(FN(UNION
,get_ctx
)(u
), &u
->table
,
1087 &FN(UNION
,scale_down_val_entry
), v
) < 0)
1098 S(UNION
,plain_is_equal_data
)
1104 static isl_stat
FN(UNION
,plain_is_equal_entry
)(void **entry
, void *user
)
1106 S(UNION
,plain_is_equal_data
) *data
= user
;
1107 struct isl_hash_table_entry
*entry2
;
1110 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, pw
->dim
, 0);
1111 if (!entry2
|| entry2
== isl_hash_table_entry_none
) {
1113 data
->is_equal
= isl_bool_error
;
1115 data
->is_equal
= isl_bool_false
;
1116 return isl_stat_error
;
1119 data
->is_equal
= FN(PW
,plain_is_equal
)(pw
, entry2
->data
);
1120 if (data
->is_equal
< 0 || !data
->is_equal
)
1121 return isl_stat_error
;
1126 isl_bool
FN(UNION
,plain_is_equal
)(__isl_keep UNION
*u1
, __isl_keep UNION
*u2
)
1128 S(UNION
,plain_is_equal_data
) data
= { NULL
, isl_bool_true
};
1131 return isl_bool_error
;
1133 return isl_bool_true
;
1134 if (u1
->table
.n
!= u2
->table
.n
)
1135 return isl_bool_false
;
1137 u1
= FN(UNION
,copy
)(u1
);
1138 u2
= FN(UNION
,copy
)(u2
);
1139 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
1140 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
1145 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
1146 &FN(UNION
,plain_is_equal_entry
), &data
) < 0 &&
1153 return data
.is_equal
;
1157 return isl_bool_error
;
1160 /* Internal data structure for isl_union_*_drop_dims.
1161 * type, first and n are passed to isl_*_drop_dims.
1162 * res collects the results.
1164 S(UNION
,drop_dims_data
) {
1165 enum isl_dim_type type
;
1172 /* Drop the parameters specified by "data" from "part" and
1173 * add the results to data->res.
1175 static isl_stat
FN(UNION
,drop_dims_entry
)(__isl_take PART
*part
, void *user
)
1177 S(UNION
,drop_dims_data
) *data
= user
;
1179 part
= FN(PART
,drop_dims
)(part
, data
->type
, data
->first
, data
->n
);
1180 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
1182 return isl_stat_error
;
1187 /* Drop the specified parameters from "u".
1188 * That is, type is required to be isl_dim_param.
1190 __isl_give UNION
*FN(UNION
,drop_dims
)( __isl_take UNION
*u
,
1191 enum isl_dim_type type
, unsigned first
, unsigned n
)
1194 S(UNION
,drop_dims_data
) data
= { type
, first
, n
};
1199 if (type
!= isl_dim_param
)
1200 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1201 "can only project out parameters",
1202 return FN(UNION
,free
)(u
));
1204 space
= FN(UNION
,get_space
)(u
);
1205 space
= isl_space_drop_dims(space
, type
, first
, n
);
1206 data
.res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
1207 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1208 &FN(UNION
,drop_dims_entry
), &data
) < 0)
1209 data
.res
= FN(UNION
,free
)(data
.res
);
1216 /* Internal data structure for isl_union_*_set_dim_name.
1217 * pos is the position of the parameter that needs to be renamed.
1218 * s is the new name.
1219 * res collects the results.
1221 S(UNION
,set_dim_name_data
) {
1228 /* Change the name of the parameter at position data->pos of "part" to data->s
1229 * and add the result to data->res.
1231 static isl_stat
FN(UNION
,set_dim_name_entry
)(__isl_take PART
*part
, void *user
)
1233 S(UNION
,set_dim_name_data
) *data
= user
;
1235 part
= FN(PART
,set_dim_name
)(part
, isl_dim_param
, data
->pos
, data
->s
);
1236 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
1238 return isl_stat_error
;
1243 /* Change the name of the parameter at position "pos" to "s".
1244 * That is, type is required to be isl_dim_param.
1246 __isl_give UNION
*FN(UNION
,set_dim_name
)(__isl_take UNION
*u
,
1247 enum isl_dim_type type
, unsigned pos
, const char *s
)
1249 S(UNION
,set_dim_name_data
) data
= { pos
, s
};
1255 if (type
!= isl_dim_param
)
1256 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1257 "can only set parameter names",
1258 return FN(UNION
,free
)(u
));
1260 space
= FN(UNION
,get_space
)(u
);
1261 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
1262 data
.res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
1264 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1265 &FN(UNION
,set_dim_name_entry
), &data
) < 0)
1266 data
.res
= FN(UNION
,free
)(data
.res
);
1273 /* Reset the user pointer on all identifiers of parameters and tuples
1274 * of the space of "part" and add the result to *res.
1276 static isl_stat
FN(UNION
,reset_user_entry
)(__isl_take PART
*part
, void *user
)
1280 part
= FN(PART
,reset_user
)(part
);
1281 *res
= FN(FN(UNION
,add
),PARTS
)(*res
, part
);
1283 return isl_stat_error
;
1288 /* Reset the user pointer on all identifiers of parameters and tuples
1289 * of the spaces of "u".
1291 __isl_give UNION
*FN(UNION
,reset_user
)(__isl_take UNION
*u
)
1296 space
= FN(UNION
,get_space
)(u
);
1297 space
= isl_space_reset_user(space
);
1298 res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
1299 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1300 &FN(UNION
,reset_user_entry
), &res
) < 0)
1301 res
= FN(UNION
,free
)(res
);