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
16 #include "has_single_reference_templ.c"
18 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
);
20 isl_ctx
*FN(UNION
,get_ctx
)(__isl_keep UNION
*u
)
22 return u
? u
->space
->ctx
: NULL
;
25 /* Return the space of "u".
27 static __isl_keep isl_space
*FN(UNION
,peek_space
)(__isl_keep UNION
*u
)
34 /* Return a copy of the space of "u".
36 __isl_give isl_space
*FN(UNION
,get_space
)(__isl_keep UNION
*u
)
38 return isl_space_copy(FN(UNION
,peek_space
)(u
));
41 /* Return the number of parameters of "u", where "type"
42 * is required to be set to isl_dim_param.
44 isl_size
FN(UNION
,dim
)(__isl_keep UNION
*u
, enum isl_dim_type type
)
47 return isl_size_error
;
49 if (type
!= isl_dim_param
)
50 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
51 "can only reference parameters", return isl_size_error
);
53 return isl_space_dim(u
->space
, type
);
56 /* Return the position of the parameter with the given name
58 * Return -1 if no such dimension can be found.
60 int FN(UNION
,find_dim_by_name
)(__isl_keep UNION
*u
, enum isl_dim_type type
,
65 return isl_space_find_dim_by_name(u
->space
, type
, name
);
69 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*space
,
70 enum isl_fold type
, int size
)
72 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*space
, int size
)
77 space
= isl_space_params(space
);
81 u
= isl_calloc_type(space
->ctx
, UNION
);
90 if (isl_hash_table_init(space
->ctx
, &u
->table
, size
) < 0)
91 return FN(UNION
,free
)(u
);
95 isl_space_free(space
);
100 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
, enum isl_fold type
)
102 return FN(UNION
,alloc
)(dim
, type
, 16);
105 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
)
107 return FN(UNION
,alloc
)(dim
, 16);
111 __isl_give UNION
*FN(UNION
,copy
)(__isl_keep UNION
*u
)
120 /* Extract the element of "u" living in "space" (ignoring parameters).
122 * Return the ZERO element if "u" does not contain any element
125 __isl_give PART
*FN(FN(UNION
,extract
),BASE
)(__isl_keep UNION
*u
,
126 __isl_take isl_space
*space
)
128 struct isl_hash_table_entry
*entry
;
130 space
= isl_space_replace_params(space
, FN(UNION
,peek_space
)(u
));
132 entry
= FN(UNION
,find_part_entry
)(u
, space
, 0);
135 if (entry
== isl_hash_table_entry_none
)
137 return FN(PART
,ZERO
)(space
, u
->type
);
139 return FN(PART
,ZERO
)(space
);
141 isl_space_free(space
);
142 return FN(PART
,copy
)(entry
->data
);
144 isl_space_free(space
);
148 /* Add "part" to "u".
149 * If "disjoint" is set, then "u" is not allowed to already have
150 * a part that is defined over a domain that overlaps with the domain
152 * Otherwise, compute the union sum of "part" and the part in "u"
153 * defined on the same space.
155 static __isl_give UNION
*FN(UNION
,add_part_generic
)(__isl_take UNION
*u
,
156 __isl_take PART
*part
, int disjoint
)
159 struct isl_hash_table_entry
*entry
;
164 empty
= FN(PART
,IS_ZERO
)(part
);
172 u
= FN(UNION
,align_params
)(u
, FN(PART
,get_space
)(part
));
173 part
= FN(PART
,align_params
)(part
, FN(UNION
,get_space
)(u
));
175 u
= FN(UNION
,cow
)(u
);
180 if (FN(UNION
,check_disjoint_domain_other
)(u
, part
) < 0)
182 entry
= FN(UNION
,find_part_entry
)(u
, part
->dim
, 1);
190 FN(UNION
,check_disjoint_domain
)(entry
->data
, part
) < 0)
192 entry
->data
= FN(PART
,union_add_
)(entry
->data
,
193 FN(PART
,copy
)(part
));
196 empty
= FN(PART
,IS_ZERO
)(part
);
200 u
= FN(UNION
,remove_part_entry
)(u
, entry
);
211 /* Add "part" to "u", where "u" is assumed not to already have
212 * a part that is defined on the same space as "part".
214 __isl_give UNION
*FN(FN(UNION
,add
),BASE
)(__isl_take UNION
*u
,
215 __isl_take PART
*part
)
217 return FN(UNION
,add_part_generic
)(u
, part
, 1);
221 /* Allocate a UNION with the same type and the same size as "u" and
222 * with space "space".
224 static __isl_give UNION
*FN(UNION
,alloc_same_size_on_space
)(__isl_keep UNION
*u
,
225 __isl_take isl_space
*space
)
229 return FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
231 isl_space_free(space
);
235 /* Allocate a UNION with the same size as "u" and with space "space".
237 static __isl_give UNION
*FN(UNION
,alloc_same_size_on_space
)(__isl_keep UNION
*u
,
238 __isl_take isl_space
*space
)
242 return FN(UNION
,alloc
)(space
, u
->table
.n
);
244 isl_space_free(space
);
249 /* Allocate a UNION with the same space, the same type (if any) and
250 * the same size as "u".
252 static __isl_give UNION
*FN(UNION
,alloc_same_size
)(__isl_keep UNION
*u
)
254 return FN(UNION
,alloc_same_size_on_space
)(u
, FN(UNION
,get_space
)(u
));
257 /* Internal data structure for isl_union_*_transform_space.
258 * "fn' is applied to each entry in the input.
259 * "res" collects the results.
261 S(UNION
,transform_data
)
263 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
);
269 /* Apply data->fn to "part" and add the result to data->res.
271 static isl_stat
FN(UNION
,transform_entry
)(__isl_take PART
*part
, void *user
)
273 S(UNION
,transform_data
) *data
= (S(UNION
,transform_data
) *)user
;
275 part
= data
->fn(part
, data
->user
);
276 data
->res
= FN(FN(UNION
,add
),BASE
)(data
->res
, part
);
278 return isl_stat_error
;
283 /* Return a UNION living in "space" that is obtained by applying "fn"
284 * to each of the entries in "u".
286 static __isl_give UNION
*FN(UNION
,transform_space
)(__isl_take UNION
*u
,
288 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
290 S(UNION
,transform_data
) data
= { fn
, user
};
292 data
.res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
293 if (FN(FN(UNION
,foreach
),BASE
)(u
,
294 &FN(UNION
,transform_entry
), &data
) < 0)
295 data
.res
= FN(UNION
,free
)(data
.res
);
300 /* Return a UNION that lives in the same space as "u" and that is obtained
301 * by applying "fn" to each of the entries in "u".
303 static __isl_give UNION
*FN(UNION
,transform
)(__isl_take UNION
*u
,
304 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
306 return FN(UNION
,transform_space
)(u
, FN(UNION
,get_space
)(u
), fn
, user
);
309 /* Apply data->fn to *part and store the result back into *part.
311 static isl_stat
FN(UNION
,transform_inplace_entry
)(void **part
, void *user
)
313 S(UNION
,transform_data
) *data
= (S(UNION
,transform_data
) *) user
;
315 *part
= data
->fn(*part
, data
->user
);
317 return isl_stat_error
;
321 /* Update "u" by applying "fn" to each entry.
322 * This operation is assumed not to change the number of entries nor
323 * the spaces of the entries.
325 * If there is only one reference to "u", then change "u" inplace.
326 * Otherwise, create a new UNION from "u" and discard the original.
328 static __isl_give UNION
*FN(UNION
,transform_inplace
)(__isl_take UNION
*u
,
329 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
333 single_ref
= FN(UNION
,has_single_reference
)(u
);
335 return FN(UNION
,free
)(u
);
337 S(UNION
,transform_data
) data
= { fn
, user
};
338 if (FN(UNION
,foreach_inplace
)(u
,
339 &FN(UNION
,transform_inplace_entry
), &data
) < 0)
340 return FN(UNION
,free
)(u
);
343 return FN(UNION
,transform
)(u
, fn
, user
);
346 /* An isl_union_*_transform callback for use in isl_union_*_dup
347 * that simply returns "part".
349 static __isl_give PART
*FN(UNION
,copy_part
)(__isl_take PART
*part
, void *user
)
354 __isl_give UNION
*FN(UNION
,dup
)(__isl_keep UNION
*u
)
356 u
= FN(UNION
,copy
)(u
);
357 return FN(UNION
,transform
)(u
, &FN(UNION
,copy_part
), NULL
);
360 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
)
368 return FN(UNION
,dup
)(u
);
371 __isl_null UNION
*FN(UNION
,free
)(__isl_take UNION
*u
)
379 isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
380 &FN(UNION
,free_u_entry
), NULL
);
381 isl_hash_table_clear(&u
->table
);
382 isl_space_free(u
->space
);
387 static __isl_give PART
*FN(UNION
,align_entry
)(__isl_take PART
*part
, void *user
)
389 isl_reordering
*exp
= user
;
391 exp
= isl_reordering_extend_space(isl_reordering_copy(exp
),
392 FN(PART
,get_domain_space
)(part
));
393 return FN(PART
,realign_domain
)(part
, exp
);
396 /* Reorder the parameters of "u" according to the given reordering.
398 static __isl_give UNION
*FN(UNION
,realign_domain
)(__isl_take UNION
*u
,
399 __isl_take isl_reordering
*r
)
406 space
= isl_reordering_get_space(r
);
407 u
= FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,align_entry
), r
);
408 isl_reordering_free(r
);
412 isl_reordering_free(r
);
416 /* Align the parameters of "u" to those of "model".
418 __isl_give UNION
*FN(UNION
,align_params
)(__isl_take UNION
*u
,
419 __isl_take isl_space
*model
)
421 isl_bool equal_params
;
427 equal_params
= isl_space_has_equal_params(u
->space
, model
);
428 if (equal_params
< 0)
431 isl_space_free(model
);
435 r
= isl_parameter_alignment_reordering(u
->space
, model
);
436 isl_space_free(model
);
438 return FN(UNION
,realign_domain
)(u
, r
);
440 isl_space_free(model
);
445 /* Add "part" to *u, taking the union sum if "u" already has
446 * a part defined on the same space as "part".
448 static isl_stat
FN(UNION
,union_add_part
)(__isl_take PART
*part
, void *user
)
450 UNION
**u
= (UNION
**)user
;
452 *u
= FN(UNION
,add_part_generic
)(*u
, part
, 0);
457 /* Compute the sum of "u1" and "u2" on the union of their domains,
458 * with the actual sum on the shared domain and
459 * the defined expression on the symmetric difference of the domains.
461 * This is an internal function that is exposed under different
462 * names depending on whether the base expressions have a zero default
464 * If they do, then this function is called "add".
465 * Otherwise, it is called "union_add".
467 static __isl_give UNION
*FN(UNION
,union_add_
)(__isl_take UNION
*u1
,
468 __isl_take UNION
*u2
)
470 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
471 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
473 u1
= FN(UNION
,cow
)(u1
);
478 if (FN(FN(UNION
,foreach
),BASE
)(u2
, &FN(UNION
,union_add_part
), &u1
) < 0)
490 __isl_give UNION
*FN(FN(UNION
,from
),BASE
)(__isl_take PART
*part
)
498 space
= FN(PART
,get_space
)(part
);
499 space
= isl_space_drop_dims(space
, isl_dim_in
, 0,
500 isl_space_dim(space
, isl_dim_in
));
501 space
= isl_space_drop_dims(space
, isl_dim_out
, 0,
502 isl_space_dim(space
, isl_dim_out
));
504 u
= FN(UNION
,ZERO
)(space
, part
->type
);
506 u
= FN(UNION
,ZERO
)(space
);
508 u
= FN(FN(UNION
,add
),BASE
)(u
, part
);
513 S(UNION
,match_bin_data
) {
516 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*);
519 /* Check if data->u2 has an element living in the same space as "part".
520 * If so, call data->fn on the two elements and add the result to
523 static isl_stat
FN(UNION
,match_bin_entry
)(__isl_take PART
*part
, void *user
)
525 S(UNION
,match_bin_data
) *data
= user
;
526 struct isl_hash_table_entry
*entry2
;
530 space
= FN(PART
,get_space
)(part
);
531 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, space
, 0);
532 isl_space_free(space
);
535 if (entry2
== isl_hash_table_entry_none
) {
540 part2
= entry2
->data
;
541 if (!isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
542 part2
->dim
, isl_dim_out
))
543 isl_die(FN(UNION
,get_ctx
)(data
->u2
), isl_error_invalid
,
544 "entries should have the same range space",
547 part
= data
->fn(part
, FN(PART
, copy
)(entry2
->data
));
549 data
->res
= FN(FN(UNION
,add
),BASE
)(data
->res
, part
);
551 return isl_stat_error
;
556 return isl_stat_error
;
559 /* This function is currently only used from isl_polynomial.c
560 * and not from isl_fold.c.
562 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
563 __isl_take UNION
*u2
,
564 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
565 __attribute__ ((unused
));
566 /* For each pair of elements in "u1" and "u2" living in the same space,
567 * call "fn" and collect the results.
569 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
570 __isl_take UNION
*u2
,
571 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
573 S(UNION
,match_bin_data
) data
= { NULL
, NULL
, fn
};
575 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
576 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
582 data
.res
= FN(UNION
,alloc_same_size
)(u1
);
583 if (FN(FN(UNION
,foreach
),BASE
)(u1
,
584 &FN(UNION
,match_bin_entry
), &data
) < 0)
593 FN(UNION
,free
)(data
.res
);
597 /* Compute the sum of "u1" and "u2".
599 * If the base expressions have a default zero value, then the sum
600 * is computed on the union of the domains of "u1" and "u2".
601 * Otherwise, it is computed on their shared domains.
603 __isl_give UNION
*FN(UNION
,add
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
606 return FN(UNION
,union_add_
)(u1
, u2
);
608 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,add
));
613 /* Subtract "u2" from "u1" and return the result.
615 __isl_give UNION
*FN(UNION
,sub
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
617 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,sub
));
621 S(UNION
,any_set_data
) {
623 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
626 static __isl_give PART
*FN(UNION
,any_set_entry
)(__isl_take PART
*part
,
629 S(UNION
,any_set_data
) *data
= user
;
631 return data
->fn(part
, isl_set_copy(data
->set
));
634 /* Update each element of "u" by calling "fn" on the element and "set".
636 static __isl_give UNION
*FN(UNION
,any_set_op
)(__isl_take UNION
*u
,
637 __isl_take isl_set
*set
,
638 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
640 S(UNION
,any_set_data
) data
= { NULL
, fn
};
642 u
= FN(UNION
,align_params
)(u
, isl_set_get_space(set
));
643 set
= isl_set_align_params(set
, FN(UNION
,get_space
)(u
));
649 u
= FN(UNION
,transform
)(u
, &FN(UNION
,any_set_entry
), &data
);
658 /* Intersect the domain of "u" with the parameter domain "context".
660 __isl_give UNION
*FN(UNION
,intersect_params
)(__isl_take UNION
*u
,
661 __isl_take isl_set
*set
)
663 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,intersect_params
));
666 /* Compute the gist of the domain of "u" with respect to
667 * the parameter domain "context".
669 __isl_give UNION
*FN(UNION
,gist_params
)(__isl_take UNION
*u
,
670 __isl_take isl_set
*set
)
672 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,gist_params
));
675 S(UNION
,match_domain_data
) {
678 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
681 static int FN(UNION
,set_has_space
)(const void *entry
, const void *val
)
683 isl_set
*set
= (isl_set
*)entry
;
684 isl_space
*space
= (isl_space
*)val
;
686 return isl_space_is_equal(set
->dim
, space
);
689 /* Find the set in data->uset that lives in the same space as the domain
690 * of "part", apply data->fn to *entry and this set (if any), and add
691 * the result to data->res.
693 static isl_stat
FN(UNION
,match_domain_entry
)(__isl_take PART
*part
, void *user
)
695 S(UNION
,match_domain_data
) *data
= user
;
697 struct isl_hash_table_entry
*entry2
;
700 space
= FN(PART
,get_domain_space
)(part
);
701 hash
= isl_space_get_hash(space
);
702 entry2
= isl_hash_table_find(data
->uset
->dim
->ctx
, &data
->uset
->table
,
703 hash
, &FN(UNION
,set_has_space
), space
, 0);
704 isl_space_free(space
);
710 part
= data
->fn(part
, isl_set_copy(entry2
->data
));
712 data
->res
= FN(FN(UNION
,add
),BASE
)(data
->res
, part
);
714 return isl_stat_error
;
719 /* Apply fn to each pair of PW in u and set in uset such that
720 * the set lives in the same space as the domain of PW
721 * and collect the results.
723 static __isl_give UNION
*FN(UNION
,match_domain_op
)(__isl_take UNION
*u
,
724 __isl_take isl_union_set
*uset
,
725 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
727 S(UNION
,match_domain_data
) data
= { NULL
, NULL
, fn
};
729 u
= FN(UNION
,align_params
)(u
, isl_union_set_get_space(uset
));
730 uset
= isl_union_set_align_params(uset
, FN(UNION
,get_space
)(u
));
736 data
.res
= FN(UNION
,alloc_same_size
)(u
);
737 if (FN(FN(UNION
,foreach
),BASE
)(u
,
738 &FN(UNION
,match_domain_entry
), &data
) < 0)
742 isl_union_set_free(uset
);
746 isl_union_set_free(uset
);
747 FN(UNION
,free
)(data
.res
);
751 /* Intersect the domain of "u" with "uset".
752 * If "uset" is a parameters domain, then intersect the parameter
753 * domain of "u" with this set.
755 __isl_give UNION
*FN(UNION
,intersect_domain
)(__isl_take UNION
*u
,
756 __isl_take isl_union_set
*uset
)
758 if (isl_union_set_is_params(uset
))
759 return FN(UNION
,intersect_params
)(u
,
760 isl_set_from_union_set(uset
));
761 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,intersect_domain
));
764 /* Take the set (which may be empty) in data->uset that lives
765 * in the same space as the domain of "pw", subtract it from the domain
766 * of "part" and return the result.
768 static __isl_give PART
*FN(UNION
,subtract_domain_entry
)(__isl_take PART
*part
,
771 isl_union_set
*uset
= user
;
775 space
= FN(PART
,get_domain_space
)(part
);
776 set
= isl_union_set_extract_set(uset
, space
);
777 return FN(PART
,subtract_domain
)(part
, set
);
780 /* Subtract "uset' from the domain of "u".
782 __isl_give UNION
*FN(UNION
,subtract_domain
)(__isl_take UNION
*u
,
783 __isl_take isl_union_set
*uset
)
785 u
= FN(UNION
,transform
)(u
, &FN(UNION
,subtract_domain_entry
), uset
);
786 isl_union_set_free(uset
);
790 __isl_give UNION
*FN(UNION
,gist
)(__isl_take UNION
*u
,
791 __isl_take isl_union_set
*uset
)
793 if (isl_union_set_is_params(uset
))
794 return FN(UNION
,gist_params
)(u
, isl_set_from_union_set(uset
));
795 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,gist
));
798 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
799 * Since the UNION may have several references, the entry is only
800 * replaced if the coalescing is successful.
802 static isl_stat
FN(UNION
,coalesce_entry
)(void **entry
, void *user
)
804 PART
**part_p
= (PART
**) entry
;
807 part
= FN(PART
,copy
)(*part_p
);
808 part
= FN(PW
,coalesce
)(part
);
810 return isl_stat_error
;
811 FN(PART
,free
)(*part_p
);
817 __isl_give UNION
*FN(UNION
,coalesce
)(__isl_take UNION
*u
)
819 if (FN(UNION
,foreach_inplace
)(u
, &FN(UNION
,coalesce_entry
), NULL
) < 0)
828 static isl_stat
FN(UNION
,domain_entry
)(__isl_take PART
*part
, void *user
)
830 isl_union_set
**uset
= (isl_union_set
**)user
;
832 *uset
= isl_union_set_add_set(*uset
, FN(PART
,domain
)(part
));
837 __isl_give isl_union_set
*FN(UNION
,domain
)(__isl_take UNION
*u
)
841 uset
= isl_union_set_empty(FN(UNION
,get_space
)(u
));
842 if (FN(FN(UNION
,foreach
),BASE
)(u
, &FN(UNION
,domain_entry
), &uset
) < 0)
849 isl_union_set_free(uset
);
855 /* Negate the type of "u".
857 static __isl_give UNION
*FN(UNION
,negate_type
)(__isl_take UNION
*u
)
859 u
= FN(UNION
,cow
)(u
);
862 u
->type
= isl_fold_type_negate(u
->type
);
866 /* Negate the type of "u".
867 * Since "u" does not have a type, do nothing.
869 static __isl_give UNION
*FN(UNION
,negate_type
)(__isl_take UNION
*u
)
875 /* Multiply "part" by the isl_val "user" and return the result.
877 static __isl_give PART
*FN(UNION
,scale_val_entry
)(__isl_take PART
*part
,
882 return FN(PART
,scale_val
)(part
, isl_val_copy(v
));
885 /* Multiply "u" by "v" and return the result.
887 __isl_give UNION
*FN(UNION
,scale_val
)(__isl_take UNION
*u
,
888 __isl_take isl_val
*v
)
892 if (isl_val_is_one(v
)) {
897 if (DEFAULT_IS_ZERO
&& u
&& isl_val_is_zero(v
)) {
899 isl_space
*space
= FN(UNION
,get_space
)(u
);
901 zero
= FN(UNION
,ZERO
)(space
, u
->type
);
903 zero
= FN(UNION
,ZERO
)(space
);
910 if (!isl_val_is_rat(v
))
911 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
912 "expecting rational factor", goto error
);
914 u
= FN(UNION
,transform_inplace
)(u
, &FN(UNION
,scale_val_entry
), v
);
915 if (isl_val_is_neg(v
))
916 u
= FN(UNION
,negate_type
)(u
);
926 /* Divide "part" by the isl_val "user" and return the result.
928 static __isl_give PART
*FN(UNION
,scale_down_val_entry
)(__isl_take PART
*part
,
933 return FN(PART
,scale_down_val
)(part
, isl_val_copy(v
));
936 /* Divide "u" by "v" and return the result.
938 __isl_give UNION
*FN(UNION
,scale_down_val
)(__isl_take UNION
*u
,
939 __isl_take isl_val
*v
)
943 if (isl_val_is_one(v
)) {
948 if (!isl_val_is_rat(v
))
949 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
950 "expecting rational factor", goto error
);
951 if (isl_val_is_zero(v
))
952 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
953 "cannot scale down by zero", goto error
);
955 u
= FN(UNION
,transform_inplace
)(u
, &FN(UNION
,scale_down_val_entry
), v
);
956 if (isl_val_is_neg(v
))
957 u
= FN(UNION
,negate_type
)(u
);
967 S(UNION
,plain_is_equal_data
)
973 static isl_stat
FN(UNION
,plain_is_equal_entry
)(void **entry
, void *user
)
975 S(UNION
,plain_is_equal_data
) *data
= user
;
976 struct isl_hash_table_entry
*entry2
;
979 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, pw
->dim
, 0);
980 if (!entry2
|| entry2
== isl_hash_table_entry_none
) {
982 data
->is_equal
= isl_bool_error
;
984 data
->is_equal
= isl_bool_false
;
985 return isl_stat_error
;
988 data
->is_equal
= FN(PW
,plain_is_equal
)(pw
, entry2
->data
);
989 if (data
->is_equal
< 0 || !data
->is_equal
)
990 return isl_stat_error
;
995 isl_bool
FN(UNION
,plain_is_equal
)(__isl_keep UNION
*u1
, __isl_keep UNION
*u2
)
997 S(UNION
,plain_is_equal_data
) data
= { NULL
, isl_bool_true
};
1001 return isl_bool_error
;
1003 return isl_bool_true
;
1004 if (u1
->table
.n
!= u2
->table
.n
)
1005 return isl_bool_false
;
1006 n1
= FN(FN(UNION
,n
),BASE
)(u1
);
1007 n2
= FN(FN(UNION
,n
),BASE
)(u2
);
1008 if (n1
< 0 || n2
< 0)
1009 return isl_bool_error
;
1011 return isl_bool_false
;
1013 u1
= FN(UNION
,copy
)(u1
);
1014 u2
= FN(UNION
,copy
)(u2
);
1015 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
1016 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
1021 if (FN(UNION
,foreach_inplace
)(u1
,
1022 &FN(UNION
,plain_is_equal_entry
), &data
) < 0 &&
1029 return data
.is_equal
;
1033 return isl_bool_error
;
1036 /* Check whether the element that "entry" points to involves any NaNs and
1037 * store the result in *nan.
1038 * Abort as soon as one such element has been found.
1040 static isl_stat
FN(UNION
,involves_nan_entry
)(void **entry
, void *user
)
1042 isl_bool
*nan
= user
;
1045 *nan
= FN(PW
,involves_nan
)(pw
);
1046 if (*nan
< 0 || !nan
)
1047 return isl_stat_error
;
1052 /* Does "u" involve any NaNs?
1054 isl_bool
FN(UNION
,involves_nan
)(__isl_keep UNION
*u
)
1056 isl_bool nan
= isl_bool_false
;
1059 return isl_bool_error
;
1061 if (FN(UNION
,foreach_inplace
)(u
,
1062 &FN(UNION
,involves_nan_entry
), &nan
) < 0 &&
1064 return isl_bool_error
;
1069 /* Internal data structure for isl_union_*_drop_dims.
1070 * type, first and n are passed to isl_*_drop_dims.
1072 S(UNION
,drop_dims_data
) {
1073 enum isl_dim_type type
;
1078 /* Drop the parameters specified by "data" from "part" and return the result.
1080 static __isl_give PART
*FN(UNION
,drop_dims_entry
)(__isl_take PART
*part
,
1083 S(UNION
,drop_dims_data
) *data
= user
;
1085 return FN(PART
,drop_dims
)(part
, data
->type
, data
->first
, data
->n
);
1088 /* Drop the specified parameters from "u".
1089 * That is, type is required to be isl_dim_param.
1091 __isl_give UNION
*FN(UNION
,drop_dims
)( __isl_take UNION
*u
,
1092 enum isl_dim_type type
, unsigned first
, unsigned n
)
1095 S(UNION
,drop_dims_data
) data
= { type
, first
, n
};
1100 if (type
!= isl_dim_param
)
1101 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1102 "can only project out parameters",
1103 return FN(UNION
,free
)(u
));
1105 space
= FN(UNION
,get_space
)(u
);
1106 space
= isl_space_drop_dims(space
, type
, first
, n
);
1107 return FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,drop_dims_entry
),
1111 /* Internal data structure for isl_union_*_set_dim_name.
1112 * pos is the position of the parameter that needs to be renamed.
1113 * s is the new name.
1115 S(UNION
,set_dim_name_data
) {
1120 /* Change the name of the parameter at position data->pos of "part" to data->s
1121 * and return the result.
1123 static __isl_give PART
*FN(UNION
,set_dim_name_entry
)(__isl_take PART
*part
,
1126 S(UNION
,set_dim_name_data
) *data
= user
;
1128 return FN(PART
,set_dim_name
)(part
, isl_dim_param
, data
->pos
, data
->s
);
1131 /* Change the name of the parameter at position "pos" to "s".
1132 * That is, type is required to be isl_dim_param.
1134 __isl_give UNION
*FN(UNION
,set_dim_name
)(__isl_take UNION
*u
,
1135 enum isl_dim_type type
, unsigned pos
, const char *s
)
1137 S(UNION
,set_dim_name_data
) data
= { pos
, s
};
1143 if (type
!= isl_dim_param
)
1144 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1145 "can only set parameter names",
1146 return FN(UNION
,free
)(u
));
1148 space
= FN(UNION
,get_space
)(u
);
1149 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
1150 return FN(UNION
,transform_space
)(u
, space
,
1151 &FN(UNION
,set_dim_name_entry
), &data
);
1154 /* Reset the user pointer on all identifiers of parameters and tuples
1155 * of the space of "part" and return the result.
1157 static __isl_give PART
*FN(UNION
,reset_user_entry
)(__isl_take PART
*part
,
1160 return FN(PART
,reset_user
)(part
);
1163 /* Reset the user pointer on all identifiers of parameters and tuples
1164 * of the spaces of "u".
1166 __isl_give UNION
*FN(UNION
,reset_user
)(__isl_take UNION
*u
)
1170 space
= FN(UNION
,get_space
)(u
);
1171 space
= isl_space_reset_user(space
);
1172 return FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,reset_user_entry
),
1176 /* Add the base expression held by "entry" to "list".
1178 static isl_stat
FN(UNION
,add_to_list
)(void **entry
, void *user
)
1181 LIST(PART
) **list
= user
;
1183 *list
= FN(LIST(PART
),add
)(*list
, FN(PART
,copy
)(pw
));
1185 return isl_stat_error
;
1190 /* Return a list containing all the base expressions in "u".
1192 * First construct a list of the appropriate size and
1193 * then add all the elements.
1195 __isl_give
LIST(PART
) *FN(FN(UNION
,get
),LIST(BASE
))(__isl_keep UNION
*u
)
1202 n
= FN(FN(UNION
,n
),BASE
)(u
);
1205 list
= FN(LIST(PART
),alloc
)(FN(UNION
,get_ctx(u
)), n
);
1206 if (FN(UNION
,foreach_inplace
)(u
, &FN(UNION
,add_to_list
), &list
) < 0)
1207 return FN(LIST(PART
),free
)(list
);