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 /* Remove "part_entry" from the hash table of "u".
215 static __isl_give UNION
*FN(UNION
,remove_part_entry
)(__isl_take UNION
*u
,
216 struct isl_hash_table_entry
*part_entry
)
220 if (!u
|| !part_entry
)
221 return FN(UNION
,free
)(u
);
223 ctx
= FN(UNION
,get_ctx
)(u
);
224 isl_hash_table_remove(ctx
, &u
->table
, part_entry
);
225 FN(PART
,free
)(part_entry
->data
);
230 /* Extract the element of "u" living in "space" (ignoring parameters).
232 * Return the ZERO element if "u" does not contain any element
235 __isl_give PART
*FN(FN(UNION
,extract
),PARTS
)(__isl_keep UNION
*u
,
236 __isl_take isl_space
*space
)
238 struct isl_hash_table_entry
*entry
;
242 if (!isl_space_match(u
->space
, isl_dim_param
, space
, isl_dim_param
)) {
243 space
= isl_space_drop_dims(space
, isl_dim_param
,
244 0, isl_space_dim(space
, isl_dim_param
));
245 space
= isl_space_align_params(space
,
246 FN(UNION
,get_space
)(u
));
251 entry
= FN(UNION
,find_part_entry
)(u
, space
, 0);
254 if (entry
== isl_hash_table_entry_none
)
256 return FN(PART
,ZERO
)(space
, u
->type
);
258 return FN(PART
,ZERO
)(space
);
260 isl_space_free(space
);
261 return FN(PART
,copy
)(entry
->data
);
263 isl_space_free(space
);
267 /* Add "part" to "u".
268 * If "disjoint" is set, then "u" is not allowed to already have
269 * a part that is defined on the same space as "part".
270 * Otherwise, compute the union sum of "part" and the part in "u"
271 * defined on the same space.
273 static __isl_give UNION
*FN(UNION
,add_part_generic
)(__isl_take UNION
*u
,
274 __isl_take PART
*part
, int disjoint
)
277 struct isl_hash_table_entry
*entry
;
282 empty
= FN(PART
,IS_ZERO
)(part
);
290 u
= FN(UNION
,align_params
)(u
, FN(PART
,get_space
)(part
));
291 part
= FN(PART
,align_params
)(part
, FN(UNION
,get_space
)(u
));
293 u
= FN(UNION
,cow
)(u
);
298 entry
= FN(UNION
,find_part_entry
)(u
, part
->dim
, 1);
306 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
307 "additional part should live on separate "
308 "space", goto error
);
309 entry
->data
= FN(PART
,union_add_
)(entry
->data
,
310 FN(PART
,copy
)(part
));
313 empty
= FN(PART
,IS_ZERO
)(part
);
317 u
= FN(UNION
,remove_part_entry
)(u
, entry
);
328 /* Add "part" to "u", where "u" is assumed not to already have
329 * a part that is defined on the same space as "part".
331 __isl_give UNION
*FN(FN(UNION
,add
),PARTS
)(__isl_take UNION
*u
,
332 __isl_take PART
*part
)
334 return FN(UNION
,add_part_generic
)(u
, part
, 1);
338 /* Allocate a UNION with the same type and the same size as "u" and
339 * with space "space".
341 static __isl_give UNION
*FN(UNION
,alloc_same_size_on_space
)(__isl_keep UNION
*u
,
342 __isl_take isl_space
*space
)
345 space
= isl_space_free(space
);
346 return FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
349 /* Allocate a UNION with the same size as "u" and with space "space".
351 static __isl_give UNION
*FN(UNION
,alloc_same_size_on_space
)(__isl_keep UNION
*u
,
352 __isl_take isl_space
*space
)
355 space
= isl_space_free(space
);
356 return FN(UNION
,alloc
)(space
, u
->table
.n
);
360 /* Allocate a UNION with the same space, the same type (if any) and
361 * the same size as "u".
363 static __isl_give UNION
*FN(UNION
,alloc_same_size
)(__isl_keep UNION
*u
)
365 return FN(UNION
,alloc_same_size_on_space
)(u
, FN(UNION
,get_space
)(u
));
368 /* Call "fn" on each part entry of "u".
370 static isl_stat
FN(UNION
,foreach_inplace
)(__isl_keep UNION
*u
,
371 isl_stat (*fn
)(void **part
, void *user
), void *user
)
376 return isl_stat_error
;
377 ctx
= FN(UNION
,get_ctx
)(u
);
378 return isl_hash_table_foreach(ctx
, &u
->table
, fn
, user
);
381 /* Internal data structure for isl_union_*_transform_space.
382 * "fn' is applied to each entry in the input.
383 * "res" collects the results.
385 S(UNION
,transform_data
)
387 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
);
393 /* Apply data->fn to "part" and add the result to data->res.
395 static isl_stat
FN(UNION
,transform_entry
)(__isl_take PART
*part
, void *user
)
397 S(UNION
,transform_data
) *data
= (S(UNION
,transform_data
) *)user
;
399 part
= data
->fn(part
, data
->user
);
400 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
402 return isl_stat_error
;
407 /* Return a UNION living in "space" that is obtained by applying "fn"
408 * to each of the entries in "u".
410 static __isl_give UNION
*FN(UNION
,transform_space
)(__isl_take UNION
*u
,
412 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
414 S(UNION
,transform_data
) data
= { fn
, user
};
416 data
.res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
417 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
418 &FN(UNION
,transform_entry
), &data
) < 0)
419 data
.res
= FN(UNION
,free
)(data
.res
);
424 static isl_stat
FN(UNION
,add_part
)(__isl_take PART
*part
, void *user
)
426 UNION
**u
= (UNION
**)user
;
428 *u
= FN(FN(UNION
,add
),PARTS
)(*u
, part
);
433 __isl_give UNION
*FN(UNION
,dup
)(__isl_keep UNION
*u
)
440 dup
= FN(UNION
,alloc_same_size
)(u
);
441 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,add_part
), &dup
) < 0)
449 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
)
457 return FN(UNION
,dup
)(u
);
460 static isl_stat
FN(UNION
,free_u_entry
)(void **entry
, void *user
)
467 __isl_null UNION
*FN(UNION
,free
)(__isl_take UNION
*u
)
475 isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
476 &FN(UNION
,free_u_entry
), NULL
);
477 isl_hash_table_clear(&u
->table
);
478 isl_space_free(u
->space
);
483 static __isl_give PART
*FN(UNION
,align_entry
)(__isl_take PART
*part
, void *user
)
485 isl_reordering
*exp
= user
;
487 exp
= isl_reordering_extend_space(isl_reordering_copy(exp
),
488 FN(PART
,get_domain_space
)(part
));
489 return FN(PART
,realign_domain
)(part
, exp
);
492 /* Reorder the parameters of "u" according to the given reordering.
494 static __isl_give UNION
*FN(UNION
,realign_domain
)(__isl_take UNION
*u
,
495 __isl_take isl_reordering
*r
)
502 space
= isl_space_copy(r
->dim
);
503 u
= FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,align_entry
), r
);
504 isl_reordering_free(r
);
508 isl_reordering_free(r
);
512 /* Align the parameters of "u" to those of "model".
514 __isl_give UNION
*FN(UNION
,align_params
)(__isl_take UNION
*u
,
515 __isl_take isl_space
*model
)
522 if (isl_space_match(u
->space
, isl_dim_param
, model
, isl_dim_param
)) {
523 isl_space_free(model
);
527 model
= isl_space_params(model
);
528 r
= isl_parameter_alignment_reordering(u
->space
, model
);
529 isl_space_free(model
);
531 return FN(UNION
,realign_domain
)(u
, r
);
533 isl_space_free(model
);
538 /* Add "part" to *u, taking the union sum if "u" already has
539 * a part defined on the same space as "part".
541 static isl_stat
FN(UNION
,union_add_part
)(__isl_take PART
*part
, void *user
)
543 UNION
**u
= (UNION
**)user
;
545 *u
= FN(UNION
,add_part_generic
)(*u
, part
, 0);
550 /* Compute the sum of "u1" and "u2" on the union of their domains,
551 * with the actual sum on the shared domain and
552 * the defined expression on the symmetric difference of the domains.
554 * This is an internal function that is exposed under different
555 * names depending on whether the base expressions have a zero default
557 * If they do, then this function is called "add".
558 * Otherwise, it is called "union_add".
560 static __isl_give UNION
*FN(UNION
,union_add_
)(__isl_take UNION
*u1
,
561 __isl_take UNION
*u2
)
563 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
564 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
566 u1
= FN(UNION
,cow
)(u1
);
571 if (FN(FN(UNION
,foreach
),PARTS
)(u2
, &FN(UNION
,union_add_part
), &u1
) < 0)
583 __isl_give UNION
*FN(FN(UNION
,from
),PARTS
)(__isl_take PART
*part
)
591 dim
= FN(PART
,get_space
)(part
);
592 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
593 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
595 u
= FN(UNION
,ZERO
)(dim
, part
->type
);
597 u
= FN(UNION
,ZERO
)(dim
);
599 u
= FN(FN(UNION
,add
),PARTS
)(u
, part
);
604 S(UNION
,match_bin_data
) {
607 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*);
610 /* Check if data->u2 has an element living in the same space as *entry.
611 * If so, call data->fn on the two elements and add the result to
614 static isl_stat
FN(UNION
,match_bin_entry
)(void **entry
, void *user
)
616 S(UNION
,match_bin_data
) *data
= user
;
617 struct isl_hash_table_entry
*entry2
;
622 space
= FN(PART
,get_space
)(part
);
623 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, space
, 0);
624 isl_space_free(space
);
626 return isl_stat_error
;
627 if (entry2
== isl_hash_table_entry_none
)
630 part2
= entry2
->data
;
631 if (!isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
632 part2
->dim
, isl_dim_out
))
633 isl_die(FN(UNION
,get_ctx
)(data
->u2
), isl_error_invalid
,
634 "entries should have the same range space",
635 return isl_stat_error
);
637 part
= FN(PART
, copy
)(part
);
638 part
= data
->fn(part
, FN(PART
, copy
)(entry2
->data
));
640 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
642 return isl_stat_error
;
647 /* This function is currently only used from isl_polynomial.c
648 * and not from isl_fold.c.
650 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
651 __isl_take UNION
*u2
,
652 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
653 __attribute__ ((unused
));
654 /* For each pair of elements in "u1" and "u2" living in the same space,
655 * call "fn" and collect the results.
657 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
658 __isl_take UNION
*u2
,
659 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
661 S(UNION
,match_bin_data
) data
= { NULL
, NULL
, fn
};
663 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
664 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
670 data
.res
= FN(UNION
,alloc_same_size
)(u1
);
671 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
672 &FN(UNION
,match_bin_entry
), &data
) < 0)
681 FN(UNION
,free
)(data
.res
);
685 /* Compute the sum of "u1" and "u2".
687 * If the base expressions have a default zero value, then the sum
688 * is computed on the union of the domains of "u1" and "u2".
689 * Otherwise, it is computed on their shared domains.
691 __isl_give UNION
*FN(UNION
,add
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
694 return FN(UNION
,union_add_
)(u1
, u2
);
696 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,add
));
701 /* Subtract "u2" from "u1" and return the result.
703 __isl_give UNION
*FN(UNION
,sub
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
705 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,sub
));
709 S(UNION
,any_set_data
) {
712 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
715 static isl_stat
FN(UNION
,any_set_entry
)(void **entry
, void *user
)
717 S(UNION
,any_set_data
) *data
= user
;
720 pw
= FN(PW
,copy
)(pw
);
721 pw
= data
->fn(pw
, isl_set_copy(data
->set
));
723 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
725 return isl_stat_error
;
730 /* Update each element of "u" by calling "fn" on the element and "set".
732 static __isl_give UNION
*FN(UNION
,any_set_op
)(__isl_take UNION
*u
,
733 __isl_take isl_set
*set
,
734 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
736 S(UNION
,any_set_data
) data
= { NULL
, NULL
, fn
};
738 u
= FN(UNION
,align_params
)(u
, isl_set_get_space(set
));
739 set
= isl_set_align_params(set
, FN(UNION
,get_space
)(u
));
745 data
.res
= FN(UNION
,alloc_same_size
)(u
);
746 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
747 &FN(UNION
,any_set_entry
), &data
) < 0)
756 FN(UNION
,free
)(data
.res
);
760 /* Intersect the domain of "u" with the parameter domain "context".
762 __isl_give UNION
*FN(UNION
,intersect_params
)(__isl_take UNION
*u
,
763 __isl_take isl_set
*set
)
765 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,intersect_params
));
768 /* Compute the gist of the domain of "u" with respect to
769 * the parameter domain "context".
771 __isl_give UNION
*FN(UNION
,gist_params
)(__isl_take UNION
*u
,
772 __isl_take isl_set
*set
)
774 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,gist_params
));
777 S(UNION
,match_domain_data
) {
780 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
783 static int FN(UNION
,set_has_dim
)(const void *entry
, const void *val
)
785 isl_set
*set
= (isl_set
*)entry
;
786 isl_space
*dim
= (isl_space
*)val
;
788 return isl_space_is_equal(set
->dim
, dim
);
791 /* Find the set in data->uset that lives in the same space as the domain
792 * of *entry, apply data->fn to *entry and this set (if any), and add
793 * the result to data->res.
795 static isl_stat
FN(UNION
,match_domain_entry
)(void **entry
, void *user
)
797 S(UNION
,match_domain_data
) *data
= user
;
799 struct isl_hash_table_entry
*entry2
;
803 space
= FN(PW
,get_domain_space
)(pw
);
804 hash
= isl_space_get_hash(space
);
805 entry2
= isl_hash_table_find(data
->uset
->dim
->ctx
, &data
->uset
->table
,
806 hash
, &FN(UNION
,set_has_dim
), space
, 0);
807 isl_space_free(space
);
811 pw
= FN(PW
,copy
)(pw
);
812 pw
= data
->fn(pw
, isl_set_copy(entry2
->data
));
814 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
816 return isl_stat_error
;
821 /* Apply fn to each pair of PW in u and set in uset such that
822 * the set lives in the same space as the domain of PW
823 * and collect the results.
825 static __isl_give UNION
*FN(UNION
,match_domain_op
)(__isl_take UNION
*u
,
826 __isl_take isl_union_set
*uset
,
827 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
829 S(UNION
,match_domain_data
) data
= { NULL
, NULL
, fn
};
831 u
= FN(UNION
,align_params
)(u
, isl_union_set_get_space(uset
));
832 uset
= isl_union_set_align_params(uset
, FN(UNION
,get_space
)(u
));
838 data
.res
= FN(UNION
,alloc_same_size
)(u
);
839 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
840 &FN(UNION
,match_domain_entry
), &data
) < 0)
844 isl_union_set_free(uset
);
848 isl_union_set_free(uset
);
849 FN(UNION
,free
)(data
.res
);
853 /* Intersect the domain of "u" with "uset".
854 * If "uset" is a parameters domain, then intersect the parameter
855 * domain of "u" with this set.
857 __isl_give UNION
*FN(UNION
,intersect_domain
)(__isl_take UNION
*u
,
858 __isl_take isl_union_set
*uset
)
860 if (isl_union_set_is_params(uset
))
861 return FN(UNION
,intersect_params
)(u
,
862 isl_set_from_union_set(uset
));
863 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,intersect_domain
));
866 /* Internal data structure for isl_union_*_subtract_domain.
867 * uset is the set that needs to be removed from the domain.
868 * res collects the results.
870 S(UNION
,subtract_domain_data
) {
875 /* Take the set (which may be empty) in data->uset that lives
876 * in the same space as the domain of "pw", subtract it from the domain
877 * of "pw" and add the result to data->res.
879 static isl_stat
FN(UNION
,subtract_domain_entry
)(__isl_take PW
*pw
, void *user
)
881 S(UNION
,subtract_domain_data
) *data
= user
;
885 space
= FN(PW
,get_domain_space
)(pw
);
886 set
= isl_union_set_extract_set(data
->uset
, space
);
887 pw
= FN(PW
,subtract_domain
)(pw
, set
);
888 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
893 /* Subtract "uset' from the domain of "u".
895 __isl_give UNION
*FN(UNION
,subtract_domain
)(__isl_take UNION
*u
,
896 __isl_take isl_union_set
*uset
)
898 S(UNION
,subtract_domain_data
) data
;
904 data
.res
= FN(UNION
,alloc_same_size
)(u
);
905 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
906 &FN(UNION
,subtract_domain_entry
), &data
) < 0)
907 data
.res
= FN(UNION
,free
)(data
.res
);
910 isl_union_set_free(uset
);
914 isl_union_set_free(uset
);
918 __isl_give UNION
*FN(UNION
,gist
)(__isl_take UNION
*u
,
919 __isl_take isl_union_set
*uset
)
921 if (isl_union_set_is_params(uset
))
922 return FN(UNION
,gist_params
)(u
, isl_set_from_union_set(uset
));
923 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,gist
));
926 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
927 * Since the UNION may have several references, the entry is only
928 * replaced if the coalescing is successful.
930 static isl_stat
FN(UNION
,coalesce_entry
)(void **entry
, void *user
)
932 PART
**part_p
= (PART
**) entry
;
935 part
= FN(PART
,copy
)(*part_p
);
936 part
= FN(PW
,coalesce
)(part
);
938 return isl_stat_error
;
939 FN(PART
,free
)(*part_p
);
945 __isl_give UNION
*FN(UNION
,coalesce
)(__isl_take UNION
*u
)
947 if (FN(UNION
,foreach_inplace
)(u
, &FN(UNION
,coalesce_entry
), NULL
) < 0)
956 static isl_stat
FN(UNION
,domain_entry
)(__isl_take PART
*part
, void *user
)
958 isl_union_set
**uset
= (isl_union_set
**)user
;
960 *uset
= isl_union_set_add_set(*uset
, FN(PART
,domain
)(part
));
965 __isl_give isl_union_set
*FN(UNION
,domain
)(__isl_take UNION
*u
)
969 uset
= isl_union_set_empty(FN(UNION
,get_space
)(u
));
970 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,domain_entry
), &uset
) < 0)
977 isl_union_set_free(uset
);
983 /* Negate the type of "u".
985 static __isl_give UNION
*FN(UNION
,negate_type
)(__isl_take UNION
*u
)
987 u
= FN(UNION
,cow
)(u
);
990 u
->type
= isl_fold_type_negate(u
->type
);
994 /* Negate the type of "u".
995 * Since "u" does not have a type, do nothing.
997 static __isl_give UNION
*FN(UNION
,negate_type
)(__isl_take UNION
*u
)
1003 static isl_stat
FN(UNION
,mul_isl_int_entry
)(void **entry
, void *user
)
1005 PW
**pw
= (PW
**)entry
;
1008 *pw
= FN(PW
,mul_isl_int
)(*pw
, *v
);
1010 return isl_stat_error
;
1015 __isl_give UNION
*FN(UNION
,mul_isl_int
)(__isl_take UNION
*u
, isl_int v
)
1017 if (isl_int_is_one(v
))
1020 if (DEFAULT_IS_ZERO
&& u
&& isl_int_is_zero(v
)) {
1022 isl_space
*dim
= FN(UNION
,get_space
)(u
);
1024 zero
= FN(UNION
,ZERO
)(dim
, u
->type
);
1026 zero
= FN(UNION
,ZERO
)(dim
);
1032 u
= FN(UNION
,cow
)(u
);
1033 if (isl_int_is_neg(v
))
1034 u
= FN(UNION
,negate_type
)(u
);
1038 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1039 &FN(UNION
,mul_isl_int_entry
), &v
) < 0)
1048 /* Multiply *entry by the isl_val "user".
1050 * Return 0 on success and -1 on error.
1052 static isl_stat
FN(UNION
,scale_val_entry
)(void **entry
, void *user
)
1054 PW
**pw
= (PW
**)entry
;
1057 *pw
= FN(PW
,scale_val
)(*pw
, isl_val_copy(v
));
1059 return isl_stat_error
;
1064 /* Multiply "u" by "v" and return the result.
1066 __isl_give UNION
*FN(UNION
,scale_val
)(__isl_take UNION
*u
,
1067 __isl_take isl_val
*v
)
1071 if (isl_val_is_one(v
)) {
1076 if (DEFAULT_IS_ZERO
&& u
&& isl_val_is_zero(v
)) {
1078 isl_space
*space
= FN(UNION
,get_space
)(u
);
1080 zero
= FN(UNION
,ZERO
)(space
, u
->type
);
1082 zero
= FN(UNION
,ZERO
)(space
);
1089 if (!isl_val_is_rat(v
))
1090 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1091 "expecting rational factor", goto error
);
1093 u
= FN(UNION
,cow
)(u
);
1094 if (isl_val_is_neg(v
))
1095 u
= FN(UNION
,negate_type
)(u
);
1099 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1100 &FN(UNION
,scale_val_entry
), v
) < 0)
1111 /* Divide *entry by the isl_val "user".
1113 * Return 0 on success and -1 on error.
1115 static isl_stat
FN(UNION
,scale_down_val_entry
)(void **entry
, void *user
)
1117 PW
**pw
= (PW
**)entry
;
1120 *pw
= FN(PW
,scale_down_val
)(*pw
, isl_val_copy(v
));
1122 return isl_stat_error
;
1127 /* Divide "u" by "v" and return the result.
1129 __isl_give UNION
*FN(UNION
,scale_down_val
)(__isl_take UNION
*u
,
1130 __isl_take isl_val
*v
)
1134 if (isl_val_is_one(v
)) {
1139 if (!isl_val_is_rat(v
))
1140 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1141 "expecting rational factor", goto error
);
1142 if (isl_val_is_zero(v
))
1143 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1144 "cannot scale down by zero", goto error
);
1146 u
= FN(UNION
,cow
)(u
);
1147 if (isl_val_is_neg(v
))
1148 u
= FN(UNION
,negate_type
)(u
);
1152 if (isl_hash_table_foreach(FN(UNION
,get_ctx
)(u
), &u
->table
,
1153 &FN(UNION
,scale_down_val_entry
), v
) < 0)
1164 S(UNION
,plain_is_equal_data
)
1170 static isl_stat
FN(UNION
,plain_is_equal_entry
)(void **entry
, void *user
)
1172 S(UNION
,plain_is_equal_data
) *data
= user
;
1173 struct isl_hash_table_entry
*entry2
;
1176 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, pw
->dim
, 0);
1177 if (!entry2
|| entry2
== isl_hash_table_entry_none
) {
1179 data
->is_equal
= isl_bool_error
;
1181 data
->is_equal
= isl_bool_false
;
1182 return isl_stat_error
;
1185 data
->is_equal
= FN(PW
,plain_is_equal
)(pw
, entry2
->data
);
1186 if (data
->is_equal
< 0 || !data
->is_equal
)
1187 return isl_stat_error
;
1192 isl_bool
FN(UNION
,plain_is_equal
)(__isl_keep UNION
*u1
, __isl_keep UNION
*u2
)
1194 S(UNION
,plain_is_equal_data
) data
= { NULL
, isl_bool_true
};
1197 return isl_bool_error
;
1199 return isl_bool_true
;
1200 if (u1
->table
.n
!= u2
->table
.n
)
1201 return isl_bool_false
;
1203 u1
= FN(UNION
,copy
)(u1
);
1204 u2
= FN(UNION
,copy
)(u2
);
1205 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
1206 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
1211 if (FN(UNION
,foreach_inplace
)(u1
,
1212 &FN(UNION
,plain_is_equal_entry
), &data
) < 0 &&
1219 return data
.is_equal
;
1223 return isl_bool_error
;
1226 /* Internal data structure for isl_union_*_drop_dims.
1227 * type, first and n are passed to isl_*_drop_dims.
1229 S(UNION
,drop_dims_data
) {
1230 enum isl_dim_type type
;
1235 /* Drop the parameters specified by "data" from "part" and return the result.
1237 static __isl_give PART
*FN(UNION
,drop_dims_entry
)(__isl_take PART
*part
,
1240 S(UNION
,drop_dims_data
) *data
= user
;
1242 return FN(PART
,drop_dims
)(part
, data
->type
, data
->first
, data
->n
);
1245 /* Drop the specified parameters from "u".
1246 * That is, type is required to be isl_dim_param.
1248 __isl_give UNION
*FN(UNION
,drop_dims
)( __isl_take UNION
*u
,
1249 enum isl_dim_type type
, unsigned first
, unsigned n
)
1252 S(UNION
,drop_dims_data
) data
= { type
, first
, n
};
1257 if (type
!= isl_dim_param
)
1258 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1259 "can only project out parameters",
1260 return FN(UNION
,free
)(u
));
1262 space
= FN(UNION
,get_space
)(u
);
1263 space
= isl_space_drop_dims(space
, type
, first
, n
);
1264 return FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,drop_dims_entry
),
1268 /* Internal data structure for isl_union_*_set_dim_name.
1269 * pos is the position of the parameter that needs to be renamed.
1270 * s is the new name.
1271 * res collects the results.
1273 S(UNION
,set_dim_name_data
) {
1280 /* Change the name of the parameter at position data->pos of "part" to data->s
1281 * and add the result to data->res.
1283 static isl_stat
FN(UNION
,set_dim_name_entry
)(__isl_take PART
*part
, void *user
)
1285 S(UNION
,set_dim_name_data
) *data
= user
;
1287 part
= FN(PART
,set_dim_name
)(part
, isl_dim_param
, data
->pos
, data
->s
);
1288 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
1290 return isl_stat_error
;
1295 /* Change the name of the parameter at position "pos" to "s".
1296 * That is, type is required to be isl_dim_param.
1298 __isl_give UNION
*FN(UNION
,set_dim_name
)(__isl_take UNION
*u
,
1299 enum isl_dim_type type
, unsigned pos
, const char *s
)
1301 S(UNION
,set_dim_name_data
) data
= { pos
, s
};
1307 if (type
!= isl_dim_param
)
1308 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1309 "can only set parameter names",
1310 return FN(UNION
,free
)(u
));
1312 space
= FN(UNION
,get_space
)(u
);
1313 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
1314 data
.res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
1316 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1317 &FN(UNION
,set_dim_name_entry
), &data
) < 0)
1318 data
.res
= FN(UNION
,free
)(data
.res
);
1325 /* Reset the user pointer on all identifiers of parameters and tuples
1326 * of the space of "part" and add the result to *res.
1328 static isl_stat
FN(UNION
,reset_user_entry
)(__isl_take PART
*part
, void *user
)
1332 part
= FN(PART
,reset_user
)(part
);
1333 *res
= FN(FN(UNION
,add
),PARTS
)(*res
, part
);
1335 return isl_stat_error
;
1340 /* Reset the user pointer on all identifiers of parameters and tuples
1341 * of the spaces of "u".
1343 __isl_give UNION
*FN(UNION
,reset_user
)(__isl_take UNION
*u
)
1348 space
= FN(UNION
,get_space
)(u
);
1349 space
= isl_space_reset_user(space
);
1350 res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
1351 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1352 &FN(UNION
,reset_user_entry
), &res
) < 0)
1353 res
= FN(UNION
,free
)(res
);