2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11 #include <isl_space_private.h>
13 #include <isl_reordering.h>
15 #define xCAT(A,B) A ## B
16 #define CAT(A,B) xCAT(A,B)
18 #define EL CAT(isl_,BASE)
19 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
20 #define FN(TYPE,NAME) xFN(TYPE,NAME)
21 #define xMULTI(BASE) isl_multi_ ## BASE
22 #define MULTI(BASE) xMULTI(BASE)
23 #define MULTI_NAME(BASE) "isl_multi_" #BASE
24 #define xLIST(EL) EL ## _list
25 #define LIST(EL) xLIST(EL)
27 isl_ctx *FN(MULTI(BASE),get_ctx)(__isl_keep MULTI(BASE) *multi)
29 return multi ? isl_space_get_ctx(multi->space) : NULL;
32 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
34 return multi ? isl_space_copy(multi->space) : NULL;
37 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
38 __isl_keep MULTI(BASE) *multi)
40 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
43 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
52 ctx = isl_space_get_ctx(space);
53 n = isl_space_dim(space, isl_dim_out);
54 multi = isl_calloc(ctx, MULTI(BASE),
55 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
64 isl_space_free(space);
68 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
76 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
80 for (i = 0; i < multi->n; ++i)
81 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
82 FN(EL,copy)(multi->p[i]));
87 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
96 return FN(MULTI(BASE),dup)(multi);
99 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
108 void *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
115 if (--multi->ref > 0)
118 isl_space_free(multi->space);
119 for (i = 0; i < multi->n; ++i)
120 FN(EL,free)(multi->p[i]);
126 __isl_give MULTI(BASE) *FN(MULTI(BASE),insert_dims)(
127 __isl_take MULTI(BASE) *multi,
128 enum isl_dim_type type, unsigned first, unsigned n)
134 if (type == isl_dim_out)
135 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
136 "cannot insert output/set dimensions",
137 return FN(MULTI(BASE),free)(multi));
138 if (n == 0 && !isl_space_is_named_or_nested(multi->space, type))
141 multi = FN(MULTI(BASE),cow)(multi);
145 multi->space = isl_space_insert_dims(multi->space, type, first, n);
147 return FN(MULTI(BASE),free)(multi);
149 for (i = 0; i < multi->n; ++i) {
150 multi->p[i] = FN(EL,insert_dims)(multi->p[i], type, first, n);
152 return FN(MULTI(BASE),free)(multi);
158 __isl_give MULTI(BASE) *FN(MULTI(BASE),add_dims)(__isl_take MULTI(BASE) *multi,
159 enum isl_dim_type type, unsigned n)
163 pos = FN(MULTI(BASE),dim)(multi, type);
165 return FN(MULTI(BASE),insert_dims)(multi, type, pos, n);
168 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
169 enum isl_dim_type type)
171 return multi ? isl_space_dim(multi->space, type) : 0;
174 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
175 __isl_take MULTI(BASE) *multi,
176 enum isl_dim_type type, unsigned pos, const char *s)
180 multi = FN(MULTI(BASE),cow)(multi);
184 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
186 return FN(MULTI(BASE),free)(multi);
188 if (type == isl_dim_out)
190 for (i = 0; i < multi->n; ++i) {
191 multi->p[i] = FN(EL,set_dim_name)(multi->p[i], type, pos, s);
193 return FN(MULTI(BASE),free)(multi);
199 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
200 enum isl_dim_type type)
202 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
205 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
212 ctx = FN(MULTI(BASE),get_ctx)(multi);
213 if (pos < 0 || pos >= multi->n)
214 isl_die(ctx, isl_error_invalid,
215 "index out of bounds", return NULL);
216 return FN(EL,copy)(multi->p[pos]);
219 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
220 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
222 isl_space *multi_space = NULL;
223 isl_space *el_space = NULL;
225 multi = FN(MULTI(BASE),cow)(multi);
229 multi_space = FN(MULTI(BASE),get_space)(multi);
230 if (FN(EL,check_match_domain_space)(el, multi_space) < 0)
233 if (pos < 0 || pos >= multi->n)
234 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
235 "index out of bounds", goto error);
237 FN(EL,free)(multi->p[pos]);
240 isl_space_free(multi_space);
241 isl_space_free(el_space);
245 FN(MULTI(BASE),free)(multi);
247 isl_space_free(multi_space);
248 isl_space_free(el_space);
252 /* Reset the space of "multi". This function is called from isl_pw_templ.c
253 * and doesn't know if the space of an element object is represented
254 * directly or through its domain. It therefore passes along both,
255 * which we pass along to the element function since we don't how
256 * that is represented either.
258 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
259 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
260 __isl_take isl_space *domain)
264 multi = FN(MULTI(BASE),cow)(multi);
265 if (!multi || !space || !domain)
268 for (i = 0; i < multi->n; ++i) {
269 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
270 isl_space_copy(domain));
274 isl_space_free(domain);
275 isl_space_free(multi->space);
276 multi->space = space;
280 isl_space_free(domain);
281 isl_space_free(space);
282 FN(MULTI(BASE),free)(multi);
286 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
287 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
291 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
292 isl_space_copy(multi->space));
293 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
296 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
297 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
301 domain = isl_space_domain(isl_space_copy(space));
302 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
305 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
306 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
311 multi = FN(MULTI(BASE),cow)(multi);
315 space = FN(MULTI(BASE),get_space)(multi);
316 space = isl_space_set_tuple_name(space, type, s);
318 return FN(MULTI(BASE),reset_space)(multi, space);
321 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
322 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
323 __isl_take isl_id *id)
327 multi = FN(MULTI(BASE),cow)(multi);
329 return isl_id_free(id);
331 space = FN(MULTI(BASE),get_space)(multi);
332 space = isl_space_set_tuple_id(space, type, id);
334 return FN(MULTI(BASE),reset_space)(multi, space);
337 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
338 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
342 multi = FN(MULTI(BASE),cow)(multi);
346 for (i = 0; i < multi->n; ++i) {
347 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
348 isl_reordering_copy(exp));
353 multi = FN(MULTI(BASE),reset_domain_space)(multi,
354 isl_space_copy(exp->dim));
356 isl_reordering_free(exp);
359 isl_reordering_free(exp);
360 FN(MULTI(BASE),free)(multi);
364 /* Align the parameters of "multi" to those of "model".
366 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
367 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
371 if (!multi || !model)
374 ctx = isl_space_get_ctx(model);
375 if (!isl_space_has_named_params(model))
376 isl_die(ctx, isl_error_invalid,
377 "model has unnamed parameters", goto error);
378 if (!isl_space_has_named_params(multi->space))
379 isl_die(ctx, isl_error_invalid,
380 "input has unnamed parameters", goto error);
381 if (!isl_space_match(multi->space, isl_dim_param,
382 model, isl_dim_param)) {
385 model = isl_space_params(model);
386 exp = isl_parameter_alignment_reordering(multi->space, model);
387 exp = isl_reordering_extend_space(exp,
388 FN(MULTI(BASE),get_domain_space)(multi));
389 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
392 isl_space_free(model);
395 isl_space_free(model);
396 FN(MULTI(BASE),free)(multi);
401 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_set_and)(
402 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
403 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
404 __isl_take isl_set *set))
410 if (isl_space_match(multi->space, isl_dim_param,
411 set->dim, isl_dim_param))
412 return fn(multi, set);
413 ctx = FN(MULTI(BASE),get_ctx)(multi);
414 if (!isl_space_has_named_params(multi->space) ||
415 !isl_space_has_named_params(set->dim))
416 isl_die(ctx, isl_error_invalid,
417 "unaligned unnamed parameters", goto error);
418 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
419 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
420 return fn(multi, set);
422 FN(MULTI(BASE),free)(multi);
427 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
428 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
432 if (!multi || !context)
435 for (i = 0; i < multi->n; ++i) {
436 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
441 isl_set_free(context);
444 isl_set_free(context);
445 FN(MULTI(BASE),free)(multi);
449 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
450 __isl_take isl_set *context)
452 return FN(MULTI(BASE),align_params_multi_set_and)(multi, context,
453 &FN(MULTI(BASE),gist_aligned));
456 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
457 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
459 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
460 isl_set *dom_context = isl_set_universe(space);
461 dom_context = isl_set_intersect_params(dom_context, context);
462 return FN(MULTI(BASE),gist)(multi, dom_context);
466 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
467 __isl_take isl_space *space, __isl_take LIST(EL) *list)
477 ctx = isl_space_get_ctx(space);
478 n = FN(FN(LIST(EL),n),BASE)(list);
479 if (n != isl_space_dim(space, isl_dim_out))
480 isl_die(ctx, isl_error_invalid,
481 "invalid number of elements in list", goto error);
483 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
484 for (i = 0; i < n; ++i) {
485 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
486 FN(FN(LIST(EL),get),BASE)(list, i));
489 isl_space_free(space);
490 FN(LIST(EL),free)(list);
493 isl_space_free(space);
494 FN(LIST(EL),free)(list);
499 /* Create a multi expression in the given space that maps each
500 * input dimension to the corresponding output dimension.
502 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
511 if (isl_space_is_set(space))
512 isl_die(isl_space_get_ctx(space), isl_error_invalid,
513 "expecting map space", goto error);
515 n = isl_space_dim(space, isl_dim_out);
516 if (n != isl_space_dim(space, isl_dim_in))
517 isl_die(isl_space_get_ctx(space), isl_error_invalid,
518 "number of input and output dimensions needs to be "
519 "the same", goto error);
521 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
524 isl_space_free(space);
528 space = isl_space_domain(space);
529 ls = isl_local_space_from_space(space);
531 for (i = 0; i < n; ++i) {
533 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
535 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
538 isl_local_space_free(ls);
542 isl_space_free(space);
547 /* Construct a multi expression in the given space with value zero in
548 * each of the output dimensions.
550 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
558 n = isl_space_dim(space , isl_dim_out);
559 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
562 isl_space_free(space);
568 space = isl_space_domain(space);
569 ls = isl_local_space_from_space(space);
570 el = FN(EL,zero_on_domain)(ls);
572 for (i = 0; i < n; ++i)
573 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
583 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
587 multi = FN(MULTI(BASE),alloc)(FN(EL,get_space)(el));
588 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
594 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
595 __isl_take MULTI(BASE) *multi,
596 enum isl_dim_type type, unsigned first, unsigned n)
601 multi = FN(MULTI(BASE),cow)(multi);
605 dim = FN(MULTI(BASE),dim)(multi, type);
606 if (first + n > dim || first + n < first)
607 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
608 "index out of bounds",
609 return FN(MULTI(BASE),cow)(multi));
611 multi->space = isl_space_drop_dims(multi->space, type, first, n);
613 return FN(MULTI(BASE),cow)(multi);
615 if (type == isl_dim_out) {
616 for (i = 0; i < n; ++i)
617 FN(EL,free)(multi->p[first + i]);
618 for (i = first; i + n < multi->n; ++i)
619 multi->p[i] = multi->p[i + n];
625 for (i = 0; i < multi->n; ++i) {
626 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
628 return FN(MULTI(BASE),cow)(multi);
634 /* Given two MULTI(BASE)s A -> B and C -> D,
635 * construct a MULTI(BASE) (A * C) -> (B, D).
637 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
638 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
645 if (!multi1 || !multi2)
648 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
649 FN(MULTI(BASE),get_space)(multi2));
650 res = FN(MULTI(BASE),alloc)(space);
652 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
653 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
655 for (i = 0; i < n1; ++i) {
656 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
657 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
660 for (i = 0; i < n2; ++i) {
661 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
662 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
665 FN(MULTI(BASE),free)(multi1);
666 FN(MULTI(BASE),free)(multi2);
669 FN(MULTI(BASE),free)(multi1);
670 FN(MULTI(BASE),free)(multi2);
674 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
675 __isl_take MULTI(BASE) *multi)
680 if (!multi->space->nested[1])
683 multi = FN(MULTI(BASE),cow)(multi);
687 multi->space = isl_space_flatten_range(multi->space);
689 return FN(MULTI(BASE),free)(multi);
694 /* Given two MULTI(BASE)s A -> B and C -> D,
695 * construct a MULTI(BASE) (A * C) -> [B -> D].
697 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
698 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
702 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
703 multi = FN(MULTI(BASE),flatten_range)(multi);
707 /* Given two multi expressions, "multi1"
711 * where B2 starts at position "pos", and "multi2"
715 * return the multi expression
719 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
720 __isl_take MULTI(BASE) *multi1, unsigned pos,
721 __isl_take MULTI(BASE) *multi2)
726 if (!multi1 || !multi2)
729 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
731 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
732 "index out of bounds", goto error);
734 res = FN(MULTI(BASE),copy)(multi1);
735 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
736 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
738 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
739 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
743 FN(MULTI(BASE),free)(multi1);
744 FN(MULTI(BASE),free)(multi2);
748 /* Given two multi expressions, "multi1"
752 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
757 * return the multi expression
759 * [A1 C A2] -> [B1 D B2]
761 * We first insert input dimensions to obtain
763 * [A1 C A2] -> [B1 B2]
769 * and then apply range_splice.
771 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
772 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
773 __isl_take MULTI(BASE) *multi2)
778 if (!multi1 || !multi2)
781 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
783 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
784 "index out of bounds", goto error);
786 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
788 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
789 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
791 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
793 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
795 FN(MULTI(BASE),free)(multi1);
796 FN(MULTI(BASE),free)(multi2);
800 /* This function is currently only used from isl_aff.c
802 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
803 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
804 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
805 __attribute__ ((unused));
807 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
810 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
811 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
812 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
817 multi1 = FN(MULTI(BASE),cow)(multi1);
818 if (!multi1 || !multi2)
821 ctx = FN(MULTI(BASE),get_ctx)(multi1);
822 if (!isl_space_is_equal(multi1->space, multi2->space))
823 isl_die(ctx, isl_error_invalid,
824 "spaces don't match", goto error);
826 for (i = 0; i < multi1->n; ++i) {
827 multi1->p[i] = fn(multi1->p[i], FN(EL,copy)(multi2->p[i]));
832 FN(MULTI(BASE),free)(multi2);
835 FN(MULTI(BASE),free)(multi1);
836 FN(MULTI(BASE),free)(multi2);