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 /* Does "u" have a single reference?
382 * That is, can we change "u" inplace?
384 static isl_bool
FN(UNION
,has_single_reference
)(__isl_keep UNION
*u
)
387 return isl_bool_error
;
391 /* Internal data structure for isl_union_*_transform_space.
392 * "fn' is applied to each entry in the input.
393 * "res" collects the results.
395 S(UNION
,transform_data
)
397 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
);
403 /* Apply data->fn to "part" and add the result to data->res.
405 static isl_stat
FN(UNION
,transform_entry
)(__isl_take PART
*part
, void *user
)
407 S(UNION
,transform_data
) *data
= (S(UNION
,transform_data
) *)user
;
409 part
= data
->fn(part
, data
->user
);
410 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
412 return isl_stat_error
;
417 /* Return a UNION living in "space" that is obtained by applying "fn"
418 * to each of the entries in "u".
420 static __isl_give UNION
*FN(UNION
,transform_space
)(__isl_take UNION
*u
,
422 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
424 S(UNION
,transform_data
) data
= { fn
, user
};
426 data
.res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
427 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
428 &FN(UNION
,transform_entry
), &data
) < 0)
429 data
.res
= FN(UNION
,free
)(data
.res
);
434 /* Return a UNION that lives in the same space as "u" and that is obtained
435 * by applying "fn" to each of the entries in "u".
437 static __isl_give UNION
*FN(UNION
,transform
)(__isl_take UNION
*u
,
438 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
440 return FN(UNION
,transform_space
)(u
, FN(UNION
,get_space
)(u
), fn
, user
);
443 /* Apply data->fn to *part and store the result back into *part.
445 static isl_stat
FN(UNION
,transform_inplace_entry
)(void **part
, void *user
)
447 S(UNION
,transform_data
) *data
= (S(UNION
,transform_data
) *) user
;
449 *part
= data
->fn(*part
, data
->user
);
451 return isl_stat_error
;
455 /* Update "u" by applying "fn" to each entry.
456 * This operation is assumed not to change the number of entries nor
457 * the spaces of the entries.
459 * If there is only one reference to "u", then change "u" inplace.
460 * Otherwise, create a new UNION from "u" and discard the original.
462 static __isl_give UNION
*FN(UNION
,transform_inplace
)(__isl_take UNION
*u
,
463 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
467 single_ref
= FN(UNION
,has_single_reference
)(u
);
469 return FN(UNION
,free
)(u
);
471 S(UNION
,transform_data
) data
= { fn
, user
};
472 if (FN(UNION
,foreach_inplace
)(u
,
473 &FN(UNION
,transform_inplace_entry
), &data
) < 0)
474 return FN(UNION
,free
)(u
);
477 return FN(UNION
,transform
)(u
, fn
, user
);
480 /* An isl_union_*_transform callback for use in isl_union_*_dup
481 * that simply returns "part".
483 static __isl_give PART
*FN(UNION
,copy_part
)(__isl_take PART
*part
, void *user
)
488 __isl_give UNION
*FN(UNION
,dup
)(__isl_keep UNION
*u
)
490 u
= FN(UNION
,copy
)(u
);
491 return FN(UNION
,transform
)(u
, &FN(UNION
,copy_part
), NULL
);
494 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
)
502 return FN(UNION
,dup
)(u
);
505 static isl_stat
FN(UNION
,free_u_entry
)(void **entry
, void *user
)
512 __isl_null UNION
*FN(UNION
,free
)(__isl_take UNION
*u
)
520 isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
521 &FN(UNION
,free_u_entry
), NULL
);
522 isl_hash_table_clear(&u
->table
);
523 isl_space_free(u
->space
);
528 static __isl_give PART
*FN(UNION
,align_entry
)(__isl_take PART
*part
, void *user
)
530 isl_reordering
*exp
= user
;
532 exp
= isl_reordering_extend_space(isl_reordering_copy(exp
),
533 FN(PART
,get_domain_space
)(part
));
534 return FN(PART
,realign_domain
)(part
, exp
);
537 /* Reorder the parameters of "u" according to the given reordering.
539 static __isl_give UNION
*FN(UNION
,realign_domain
)(__isl_take UNION
*u
,
540 __isl_take isl_reordering
*r
)
547 space
= isl_space_copy(r
->dim
);
548 u
= FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,align_entry
), r
);
549 isl_reordering_free(r
);
553 isl_reordering_free(r
);
557 /* Align the parameters of "u" to those of "model".
559 __isl_give UNION
*FN(UNION
,align_params
)(__isl_take UNION
*u
,
560 __isl_take isl_space
*model
)
567 if (isl_space_match(u
->space
, isl_dim_param
, model
, isl_dim_param
)) {
568 isl_space_free(model
);
572 model
= isl_space_params(model
);
573 r
= isl_parameter_alignment_reordering(u
->space
, model
);
574 isl_space_free(model
);
576 return FN(UNION
,realign_domain
)(u
, r
);
578 isl_space_free(model
);
583 /* Add "part" to *u, taking the union sum if "u" already has
584 * a part defined on the same space as "part".
586 static isl_stat
FN(UNION
,union_add_part
)(__isl_take PART
*part
, void *user
)
588 UNION
**u
= (UNION
**)user
;
590 *u
= FN(UNION
,add_part_generic
)(*u
, part
, 0);
595 /* Compute the sum of "u1" and "u2" on the union of their domains,
596 * with the actual sum on the shared domain and
597 * the defined expression on the symmetric difference of the domains.
599 * This is an internal function that is exposed under different
600 * names depending on whether the base expressions have a zero default
602 * If they do, then this function is called "add".
603 * Otherwise, it is called "union_add".
605 static __isl_give UNION
*FN(UNION
,union_add_
)(__isl_take UNION
*u1
,
606 __isl_take UNION
*u2
)
608 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
609 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
611 u1
= FN(UNION
,cow
)(u1
);
616 if (FN(FN(UNION
,foreach
),PARTS
)(u2
, &FN(UNION
,union_add_part
), &u1
) < 0)
628 __isl_give UNION
*FN(FN(UNION
,from
),PARTS
)(__isl_take PART
*part
)
636 dim
= FN(PART
,get_space
)(part
);
637 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
638 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
640 u
= FN(UNION
,ZERO
)(dim
, part
->type
);
642 u
= FN(UNION
,ZERO
)(dim
);
644 u
= FN(FN(UNION
,add
),PARTS
)(u
, part
);
649 S(UNION
,match_bin_data
) {
652 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*);
655 /* Check if data->u2 has an element living in the same space as *entry.
656 * If so, call data->fn on the two elements and add the result to
659 static isl_stat
FN(UNION
,match_bin_entry
)(void **entry
, void *user
)
661 S(UNION
,match_bin_data
) *data
= user
;
662 struct isl_hash_table_entry
*entry2
;
667 space
= FN(PART
,get_space
)(part
);
668 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, space
, 0);
669 isl_space_free(space
);
671 return isl_stat_error
;
672 if (entry2
== isl_hash_table_entry_none
)
675 part2
= entry2
->data
;
676 if (!isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
677 part2
->dim
, isl_dim_out
))
678 isl_die(FN(UNION
,get_ctx
)(data
->u2
), isl_error_invalid
,
679 "entries should have the same range space",
680 return isl_stat_error
);
682 part
= FN(PART
, copy
)(part
);
683 part
= data
->fn(part
, FN(PART
, copy
)(entry2
->data
));
685 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
687 return isl_stat_error
;
692 /* This function is currently only used from isl_polynomial.c
693 * and not from isl_fold.c.
695 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
696 __isl_take UNION
*u2
,
697 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
698 __attribute__ ((unused
));
699 /* For each pair of elements in "u1" and "u2" living in the same space,
700 * call "fn" and collect the results.
702 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
703 __isl_take UNION
*u2
,
704 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
706 S(UNION
,match_bin_data
) data
= { NULL
, NULL
, fn
};
708 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
709 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
715 data
.res
= FN(UNION
,alloc_same_size
)(u1
);
716 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
717 &FN(UNION
,match_bin_entry
), &data
) < 0)
726 FN(UNION
,free
)(data
.res
);
730 /* Compute the sum of "u1" and "u2".
732 * If the base expressions have a default zero value, then the sum
733 * is computed on the union of the domains of "u1" and "u2".
734 * Otherwise, it is computed on their shared domains.
736 __isl_give UNION
*FN(UNION
,add
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
739 return FN(UNION
,union_add_
)(u1
, u2
);
741 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,add
));
746 /* Subtract "u2" from "u1" and return the result.
748 __isl_give UNION
*FN(UNION
,sub
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
750 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,sub
));
754 S(UNION
,any_set_data
) {
756 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
759 static __isl_give PART
*FN(UNION
,any_set_entry
)(__isl_take PART
*part
,
762 S(UNION
,any_set_data
) *data
= user
;
764 return data
->fn(part
, isl_set_copy(data
->set
));
767 /* Update each element of "u" by calling "fn" on the element and "set".
769 static __isl_give UNION
*FN(UNION
,any_set_op
)(__isl_take UNION
*u
,
770 __isl_take isl_set
*set
,
771 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
773 S(UNION
,any_set_data
) data
= { NULL
, fn
};
775 u
= FN(UNION
,align_params
)(u
, isl_set_get_space(set
));
776 set
= isl_set_align_params(set
, FN(UNION
,get_space
)(u
));
782 u
= FN(UNION
,transform
)(u
, &FN(UNION
,any_set_entry
), &data
);
791 /* Intersect the domain of "u" with the parameter domain "context".
793 __isl_give UNION
*FN(UNION
,intersect_params
)(__isl_take UNION
*u
,
794 __isl_take isl_set
*set
)
796 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,intersect_params
));
799 /* Compute the gist of the domain of "u" with respect to
800 * the parameter domain "context".
802 __isl_give UNION
*FN(UNION
,gist_params
)(__isl_take UNION
*u
,
803 __isl_take isl_set
*set
)
805 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,gist_params
));
808 S(UNION
,match_domain_data
) {
811 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
814 static int FN(UNION
,set_has_dim
)(const void *entry
, const void *val
)
816 isl_set
*set
= (isl_set
*)entry
;
817 isl_space
*dim
= (isl_space
*)val
;
819 return isl_space_is_equal(set
->dim
, dim
);
822 /* Find the set in data->uset that lives in the same space as the domain
823 * of *entry, apply data->fn to *entry and this set (if any), and add
824 * the result to data->res.
826 static isl_stat
FN(UNION
,match_domain_entry
)(void **entry
, void *user
)
828 S(UNION
,match_domain_data
) *data
= user
;
830 struct isl_hash_table_entry
*entry2
;
834 space
= FN(PW
,get_domain_space
)(pw
);
835 hash
= isl_space_get_hash(space
);
836 entry2
= isl_hash_table_find(data
->uset
->dim
->ctx
, &data
->uset
->table
,
837 hash
, &FN(UNION
,set_has_dim
), space
, 0);
838 isl_space_free(space
);
842 pw
= FN(PW
,copy
)(pw
);
843 pw
= data
->fn(pw
, isl_set_copy(entry2
->data
));
845 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
847 return isl_stat_error
;
852 /* Apply fn to each pair of PW in u and set in uset such that
853 * the set lives in the same space as the domain of PW
854 * and collect the results.
856 static __isl_give UNION
*FN(UNION
,match_domain_op
)(__isl_take UNION
*u
,
857 __isl_take isl_union_set
*uset
,
858 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
860 S(UNION
,match_domain_data
) data
= { NULL
, NULL
, fn
};
862 u
= FN(UNION
,align_params
)(u
, isl_union_set_get_space(uset
));
863 uset
= isl_union_set_align_params(uset
, FN(UNION
,get_space
)(u
));
869 data
.res
= FN(UNION
,alloc_same_size
)(u
);
870 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
871 &FN(UNION
,match_domain_entry
), &data
) < 0)
875 isl_union_set_free(uset
);
879 isl_union_set_free(uset
);
880 FN(UNION
,free
)(data
.res
);
884 /* Intersect the domain of "u" with "uset".
885 * If "uset" is a parameters domain, then intersect the parameter
886 * domain of "u" with this set.
888 __isl_give UNION
*FN(UNION
,intersect_domain
)(__isl_take UNION
*u
,
889 __isl_take isl_union_set
*uset
)
891 if (isl_union_set_is_params(uset
))
892 return FN(UNION
,intersect_params
)(u
,
893 isl_set_from_union_set(uset
));
894 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,intersect_domain
));
897 /* Take the set (which may be empty) in data->uset that lives
898 * in the same space as the domain of "pw", subtract it from the domain
899 * of "part" and return the result.
901 static __isl_give PART
*FN(UNION
,subtract_domain_entry
)(__isl_take PART
*part
,
904 isl_union_set
*uset
= user
;
908 space
= FN(PART
,get_domain_space
)(part
);
909 set
= isl_union_set_extract_set(uset
, space
);
910 return FN(PART
,subtract_domain
)(part
, set
);
913 /* Subtract "uset' from the domain of "u".
915 __isl_give UNION
*FN(UNION
,subtract_domain
)(__isl_take UNION
*u
,
916 __isl_take isl_union_set
*uset
)
918 u
= FN(UNION
,transform
)(u
, &FN(UNION
,subtract_domain_entry
), uset
);
919 isl_union_set_free(uset
);
923 __isl_give UNION
*FN(UNION
,gist
)(__isl_take UNION
*u
,
924 __isl_take isl_union_set
*uset
)
926 if (isl_union_set_is_params(uset
))
927 return FN(UNION
,gist_params
)(u
, isl_set_from_union_set(uset
));
928 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,gist
));
931 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
932 * Since the UNION may have several references, the entry is only
933 * replaced if the coalescing is successful.
935 static isl_stat
FN(UNION
,coalesce_entry
)(void **entry
, void *user
)
937 PART
**part_p
= (PART
**) entry
;
940 part
= FN(PART
,copy
)(*part_p
);
941 part
= FN(PW
,coalesce
)(part
);
943 return isl_stat_error
;
944 FN(PART
,free
)(*part_p
);
950 __isl_give UNION
*FN(UNION
,coalesce
)(__isl_take UNION
*u
)
952 if (FN(UNION
,foreach_inplace
)(u
, &FN(UNION
,coalesce_entry
), NULL
) < 0)
961 static isl_stat
FN(UNION
,domain_entry
)(__isl_take PART
*part
, void *user
)
963 isl_union_set
**uset
= (isl_union_set
**)user
;
965 *uset
= isl_union_set_add_set(*uset
, FN(PART
,domain
)(part
));
970 __isl_give isl_union_set
*FN(UNION
,domain
)(__isl_take UNION
*u
)
974 uset
= isl_union_set_empty(FN(UNION
,get_space
)(u
));
975 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,domain_entry
), &uset
) < 0)
982 isl_union_set_free(uset
);
988 /* Negate the type of "u".
990 static __isl_give UNION
*FN(UNION
,negate_type
)(__isl_take UNION
*u
)
992 u
= FN(UNION
,cow
)(u
);
995 u
->type
= isl_fold_type_negate(u
->type
);
999 /* Negate the type of "u".
1000 * Since "u" does not have a type, do nothing.
1002 static __isl_give UNION
*FN(UNION
,negate_type
)(__isl_take UNION
*u
)
1008 static __isl_give PART
*FN(UNION
,mul_isl_int_entry
)(__isl_take PART
*part
,
1013 return FN(PW
,mul_isl_int
)(part
, *v
);
1016 __isl_give UNION
*FN(UNION
,mul_isl_int
)(__isl_take UNION
*u
, isl_int v
)
1018 if (isl_int_is_one(v
))
1021 if (DEFAULT_IS_ZERO
&& u
&& isl_int_is_zero(v
)) {
1023 isl_space
*dim
= FN(UNION
,get_space
)(u
);
1025 zero
= FN(UNION
,ZERO
)(dim
, u
->type
);
1027 zero
= FN(UNION
,ZERO
)(dim
);
1033 u
= FN(UNION
,transform_inplace
)(u
, &FN(UNION
,mul_isl_int_entry
), &v
);
1034 if (isl_int_is_neg(v
))
1035 u
= FN(UNION
,negate_type
)(u
);
1040 /* Multiply "part" by the isl_val "user" and return the result.
1042 static __isl_give PART
*FN(UNION
,scale_val_entry
)(__isl_take PART
*part
,
1047 return FN(PART
,scale_val
)(part
, isl_val_copy(v
));
1050 /* Multiply "u" by "v" and return the result.
1052 __isl_give UNION
*FN(UNION
,scale_val
)(__isl_take UNION
*u
,
1053 __isl_take isl_val
*v
)
1057 if (isl_val_is_one(v
)) {
1062 if (DEFAULT_IS_ZERO
&& u
&& isl_val_is_zero(v
)) {
1064 isl_space
*space
= FN(UNION
,get_space
)(u
);
1066 zero
= FN(UNION
,ZERO
)(space
, u
->type
);
1068 zero
= FN(UNION
,ZERO
)(space
);
1075 if (!isl_val_is_rat(v
))
1076 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1077 "expecting rational factor", goto error
);
1079 u
= FN(UNION
,transform_inplace
)(u
, &FN(UNION
,scale_val_entry
), v
);
1080 if (isl_val_is_neg(v
))
1081 u
= FN(UNION
,negate_type
)(u
);
1091 /* Divide *entry by the isl_val "user".
1093 * Return 0 on success and -1 on error.
1095 static isl_stat
FN(UNION
,scale_down_val_entry
)(void **entry
, void *user
)
1097 PW
**pw
= (PW
**)entry
;
1100 *pw
= FN(PW
,scale_down_val
)(*pw
, isl_val_copy(v
));
1102 return isl_stat_error
;
1107 /* Divide "u" by "v" and return the result.
1109 __isl_give UNION
*FN(UNION
,scale_down_val
)(__isl_take UNION
*u
,
1110 __isl_take isl_val
*v
)
1114 if (isl_val_is_one(v
)) {
1119 if (!isl_val_is_rat(v
))
1120 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1121 "expecting rational factor", goto error
);
1122 if (isl_val_is_zero(v
))
1123 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1124 "cannot scale down by zero", goto error
);
1126 u
= FN(UNION
,cow
)(u
);
1127 if (isl_val_is_neg(v
))
1128 u
= FN(UNION
,negate_type
)(u
);
1132 if (isl_hash_table_foreach(FN(UNION
,get_ctx
)(u
), &u
->table
,
1133 &FN(UNION
,scale_down_val_entry
), v
) < 0)
1144 S(UNION
,plain_is_equal_data
)
1150 static isl_stat
FN(UNION
,plain_is_equal_entry
)(void **entry
, void *user
)
1152 S(UNION
,plain_is_equal_data
) *data
= user
;
1153 struct isl_hash_table_entry
*entry2
;
1156 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, pw
->dim
, 0);
1157 if (!entry2
|| entry2
== isl_hash_table_entry_none
) {
1159 data
->is_equal
= isl_bool_error
;
1161 data
->is_equal
= isl_bool_false
;
1162 return isl_stat_error
;
1165 data
->is_equal
= FN(PW
,plain_is_equal
)(pw
, entry2
->data
);
1166 if (data
->is_equal
< 0 || !data
->is_equal
)
1167 return isl_stat_error
;
1172 isl_bool
FN(UNION
,plain_is_equal
)(__isl_keep UNION
*u1
, __isl_keep UNION
*u2
)
1174 S(UNION
,plain_is_equal_data
) data
= { NULL
, isl_bool_true
};
1177 return isl_bool_error
;
1179 return isl_bool_true
;
1180 if (u1
->table
.n
!= u2
->table
.n
)
1181 return isl_bool_false
;
1183 u1
= FN(UNION
,copy
)(u1
);
1184 u2
= FN(UNION
,copy
)(u2
);
1185 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
1186 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
1191 if (FN(UNION
,foreach_inplace
)(u1
,
1192 &FN(UNION
,plain_is_equal_entry
), &data
) < 0 &&
1199 return data
.is_equal
;
1203 return isl_bool_error
;
1206 /* Internal data structure for isl_union_*_drop_dims.
1207 * type, first and n are passed to isl_*_drop_dims.
1209 S(UNION
,drop_dims_data
) {
1210 enum isl_dim_type type
;
1215 /* Drop the parameters specified by "data" from "part" and return the result.
1217 static __isl_give PART
*FN(UNION
,drop_dims_entry
)(__isl_take PART
*part
,
1220 S(UNION
,drop_dims_data
) *data
= user
;
1222 return FN(PART
,drop_dims
)(part
, data
->type
, data
->first
, data
->n
);
1225 /* Drop the specified parameters from "u".
1226 * That is, type is required to be isl_dim_param.
1228 __isl_give UNION
*FN(UNION
,drop_dims
)( __isl_take UNION
*u
,
1229 enum isl_dim_type type
, unsigned first
, unsigned n
)
1232 S(UNION
,drop_dims_data
) data
= { type
, first
, n
};
1237 if (type
!= isl_dim_param
)
1238 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1239 "can only project out parameters",
1240 return FN(UNION
,free
)(u
));
1242 space
= FN(UNION
,get_space
)(u
);
1243 space
= isl_space_drop_dims(space
, type
, first
, n
);
1244 return FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,drop_dims_entry
),
1248 /* Internal data structure for isl_union_*_set_dim_name.
1249 * pos is the position of the parameter that needs to be renamed.
1250 * s is the new name.
1252 S(UNION
,set_dim_name_data
) {
1257 /* Change the name of the parameter at position data->pos of "part" to data->s
1258 * and return the result.
1260 static __isl_give PART
*FN(UNION
,set_dim_name_entry
)(__isl_take PART
*part
,
1263 S(UNION
,set_dim_name_data
) *data
= user
;
1265 return FN(PART
,set_dim_name
)(part
, isl_dim_param
, data
->pos
, data
->s
);
1268 /* Change the name of the parameter at position "pos" to "s".
1269 * That is, type is required to be isl_dim_param.
1271 __isl_give UNION
*FN(UNION
,set_dim_name
)(__isl_take UNION
*u
,
1272 enum isl_dim_type type
, unsigned pos
, const char *s
)
1274 S(UNION
,set_dim_name_data
) data
= { pos
, s
};
1280 if (type
!= isl_dim_param
)
1281 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1282 "can only set parameter names",
1283 return FN(UNION
,free
)(u
));
1285 space
= FN(UNION
,get_space
)(u
);
1286 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
1287 return FN(UNION
,transform_space
)(u
, space
,
1288 &FN(UNION
,set_dim_name_entry
), &data
);
1291 /* Reset the user pointer on all identifiers of parameters and tuples
1292 * of the space of "part" and return the result.
1294 static __isl_give PART
*FN(UNION
,reset_user_entry
)(__isl_take PART
*part
,
1297 return FN(PART
,reset_user
)(part
);
1300 /* Reset the user pointer on all identifiers of parameters and tuples
1301 * of the spaces of "u".
1303 __isl_give UNION
*FN(UNION
,reset_user
)(__isl_take UNION
*u
)
1307 space
= FN(UNION
,get_space
)(u
);
1308 space
= isl_space_reset_user(space
);
1309 return FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,reset_user_entry
),