isl_schedule.c: extract out merge_edge
[isl.git] / isl_multi_templ.c
blobed0ffde70f57061f219cb95741fdda856e2250f5
1 /*
2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012-2013 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
9 */
11 #include <isl_space_private.h>
12 #include <isl/set.h>
13 #include <isl_reordering.h>
15 #define xCAT(A,B) A ## B
16 #define CAT(A,B) xCAT(A,B)
17 #undef EL
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)
45 isl_ctx *ctx;
46 int n;
47 MULTI(BASE) *multi;
49 if (!space)
50 return NULL;
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 *));
56 if (!multi)
57 goto error;
59 multi->space = space;
60 multi->n = n;
61 multi->ref = 1;
62 return multi;
63 error:
64 isl_space_free(space);
65 return NULL;
68 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
70 int i;
71 MULTI(BASE) *dup;
73 if (!multi)
74 return NULL;
76 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
77 if (!dup)
78 return NULL;
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]));
84 return dup;
87 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
89 if (!multi)
90 return NULL;
92 if (multi->ref == 1)
93 return multi;
95 multi->ref--;
96 return FN(MULTI(BASE),dup)(multi);
99 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
101 if (!multi)
102 return NULL;
104 multi->ref++;
105 return multi;
108 void *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
110 int i;
112 if (!multi)
113 return NULL;
115 if (--multi->ref > 0)
116 return NULL;
118 isl_space_free(multi->space);
119 for (i = 0; i < multi->n; ++i)
120 FN(EL,free)(multi->p[i]);
121 free(multi);
123 return NULL;
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)
130 int i;
132 if (!multi)
133 return NULL;
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))
139 return multi;
141 multi = FN(MULTI(BASE),cow)(multi);
142 if (!multi)
143 return NULL;
145 multi->space = isl_space_insert_dims(multi->space, type, first, n);
146 if (!multi->space)
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);
151 if (!multi->p[i])
152 return FN(MULTI(BASE),free)(multi);
155 return multi;
158 __isl_give MULTI(BASE) *FN(MULTI(BASE),add_dims)(__isl_take MULTI(BASE) *multi,
159 enum isl_dim_type type, unsigned n)
161 unsigned pos;
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 /* Return the position of the first dimension of "type" with id "id".
175 * Return -1 if there is no such dimension.
177 int FN(MULTI(BASE),find_dim_by_id)(__isl_keep MULTI(BASE) *multi,
178 enum isl_dim_type type, __isl_keep isl_id *id)
180 if (!multi)
181 return -1;
182 return isl_space_find_dim_by_id(multi->space, type, id);
185 /* Return the id of the given dimension.
187 __isl_give isl_id *FN(MULTI(BASE),get_dim_id)(__isl_keep MULTI(BASE) *multi,
188 enum isl_dim_type type, unsigned pos)
190 return multi ? isl_space_get_dim_id(multi->space, type, pos) : NULL;
193 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
194 __isl_take MULTI(BASE) *multi,
195 enum isl_dim_type type, unsigned pos, const char *s)
197 int i;
199 multi = FN(MULTI(BASE),cow)(multi);
200 if (!multi)
201 return NULL;
203 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
204 if (!multi->space)
205 return FN(MULTI(BASE),free)(multi);
207 if (type == isl_dim_out)
208 return multi;
209 for (i = 0; i < multi->n; ++i) {
210 multi->p[i] = FN(EL,set_dim_name)(multi->p[i], type, pos, s);
211 if (!multi->p[i])
212 return FN(MULTI(BASE),free)(multi);
215 return multi;
218 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
219 enum isl_dim_type type)
221 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
224 /* Does the specified tuple have an id?
226 int FN(MULTI(BASE),has_tuple_id)(__isl_keep MULTI(BASE) *multi,
227 enum isl_dim_type type)
229 return multi ? isl_space_has_tuple_id(multi->space, type) : -1;
232 /* Return the id of the specified tuple.
234 __isl_give isl_id *FN(MULTI(BASE),get_tuple_id)(__isl_keep MULTI(BASE) *multi,
235 enum isl_dim_type type)
237 return multi ? isl_space_get_tuple_id(multi->space, type) : NULL;
240 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
241 int pos)
243 isl_ctx *ctx;
245 if (!multi)
246 return NULL;
247 ctx = FN(MULTI(BASE),get_ctx)(multi);
248 if (pos < 0 || pos >= multi->n)
249 isl_die(ctx, isl_error_invalid,
250 "index out of bounds", return NULL);
251 return FN(EL,copy)(multi->p[pos]);
254 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
255 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
257 isl_space *multi_space = NULL;
258 isl_space *el_space = NULL;
259 int match;
261 multi = FN(MULTI(BASE),cow)(multi);
262 if (!multi || !el)
263 goto error;
265 multi_space = FN(MULTI(BASE),get_space)(multi);
266 match = FN(EL,matching_params)(el, multi_space);
267 if (match < 0)
268 goto error;
269 if (!match) {
270 multi = FN(MULTI(BASE),align_params)(multi,
271 FN(EL,get_space)(el));
272 isl_space_free(multi_space);
273 multi_space = FN(MULTI(BASE),get_space)(multi);
274 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
276 if (FN(EL,check_match_domain_space)(el, multi_space) < 0)
277 goto error;
279 if (pos < 0 || pos >= multi->n)
280 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
281 "index out of bounds", goto error);
283 FN(EL,free)(multi->p[pos]);
284 multi->p[pos] = el;
286 isl_space_free(multi_space);
287 isl_space_free(el_space);
289 return multi;
290 error:
291 FN(MULTI(BASE),free)(multi);
292 FN(EL,free)(el);
293 isl_space_free(multi_space);
294 isl_space_free(el_space);
295 return NULL;
298 /* Reset the space of "multi". This function is called from isl_pw_templ.c
299 * and doesn't know if the space of an element object is represented
300 * directly or through its domain. It therefore passes along both,
301 * which we pass along to the element function since we don't how
302 * that is represented either.
304 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
305 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
306 __isl_take isl_space *domain)
308 int i;
310 multi = FN(MULTI(BASE),cow)(multi);
311 if (!multi || !space || !domain)
312 goto error;
314 for (i = 0; i < multi->n; ++i) {
315 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
316 isl_space_copy(domain));
317 if (!multi->p[i])
318 goto error;
320 isl_space_free(domain);
321 isl_space_free(multi->space);
322 multi->space = space;
324 return multi;
325 error:
326 isl_space_free(domain);
327 isl_space_free(space);
328 FN(MULTI(BASE),free)(multi);
329 return NULL;
332 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
333 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
335 isl_space *space;
337 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
338 isl_space_copy(multi->space));
339 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
342 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
343 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
345 isl_space *domain;
347 domain = isl_space_domain(isl_space_copy(space));
348 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
351 /* Set the id of the given dimension of "multi" to "id".
353 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_id)(
354 __isl_take MULTI(BASE) *multi,
355 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
357 isl_space *space;
359 multi = FN(MULTI(BASE),cow)(multi);
360 if (!multi || !id)
361 goto error;
363 space = FN(MULTI(BASE),get_space)(multi);
364 space = isl_space_set_dim_id(space, type, pos, id);
366 return FN(MULTI(BASE),reset_space)(multi, space);
367 error:
368 isl_id_free(id);
369 FN(MULTI(BASE),free)(multi);
370 return NULL;
373 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
374 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
375 const char *s)
377 isl_space *space;
379 multi = FN(MULTI(BASE),cow)(multi);
380 if (!multi)
381 return NULL;
383 space = FN(MULTI(BASE),get_space)(multi);
384 space = isl_space_set_tuple_name(space, type, s);
386 return FN(MULTI(BASE),reset_space)(multi, space);
389 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
390 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
391 __isl_take isl_id *id)
393 isl_space *space;
395 multi = FN(MULTI(BASE),cow)(multi);
396 if (!multi)
397 return isl_id_free(id);
399 space = FN(MULTI(BASE),get_space)(multi);
400 space = isl_space_set_tuple_id(space, type, id);
402 return FN(MULTI(BASE),reset_space)(multi, space);
405 /* Drop the id on the specified tuple.
407 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
408 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
410 isl_space *space;
412 if (!multi)
413 return NULL;
414 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
415 return multi;
417 multi = FN(MULTI(BASE),cow)(multi);
418 if (!multi)
419 return NULL;
421 space = FN(MULTI(BASE),get_space)(multi);
422 space = isl_space_reset_tuple_id(space, type);
424 return FN(MULTI(BASE),reset_space)(multi, space);
427 /* Reset the user pointer on all identifiers of parameters and tuples
428 * of the space of "multi".
430 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
431 __isl_take MULTI(BASE) *multi)
433 isl_space *space;
435 space = FN(MULTI(BASE),get_space)(multi);
436 space = isl_space_reset_user(space);
438 return FN(MULTI(BASE),reset_space)(multi, space);
441 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
442 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
444 int i;
446 multi = FN(MULTI(BASE),cow)(multi);
447 if (!multi || !exp)
448 goto error;
450 for (i = 0; i < multi->n; ++i) {
451 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
452 isl_reordering_copy(exp));
453 if (!multi->p[i])
454 goto error;
457 multi = FN(MULTI(BASE),reset_domain_space)(multi,
458 isl_space_copy(exp->dim));
460 isl_reordering_free(exp);
461 return multi;
462 error:
463 isl_reordering_free(exp);
464 FN(MULTI(BASE),free)(multi);
465 return NULL;
468 /* Align the parameters of "multi" to those of "model".
470 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
471 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
473 isl_ctx *ctx;
475 if (!multi || !model)
476 goto error;
478 ctx = isl_space_get_ctx(model);
479 if (!isl_space_has_named_params(model))
480 isl_die(ctx, isl_error_invalid,
481 "model has unnamed parameters", goto error);
482 if (!isl_space_has_named_params(multi->space))
483 isl_die(ctx, isl_error_invalid,
484 "input has unnamed parameters", goto error);
485 if (!isl_space_match(multi->space, isl_dim_param,
486 model, isl_dim_param)) {
487 isl_reordering *exp;
489 model = isl_space_params(model);
490 exp = isl_parameter_alignment_reordering(multi->space, model);
491 exp = isl_reordering_extend_space(exp,
492 FN(MULTI(BASE),get_domain_space)(multi));
493 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
496 isl_space_free(model);
497 return multi;
498 error:
499 isl_space_free(model);
500 FN(MULTI(BASE),free)(multi);
501 return NULL;
504 #if !defined(NO_GIST) || !defined(NO_INTERSECT_DOMAIN)
505 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_set_and)(
506 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
507 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
508 __isl_take isl_set *set))
510 isl_ctx *ctx;
512 if (!multi || !set)
513 goto error;
514 if (isl_space_match(multi->space, isl_dim_param,
515 set->dim, isl_dim_param))
516 return fn(multi, set);
517 ctx = FN(MULTI(BASE),get_ctx)(multi);
518 if (!isl_space_has_named_params(multi->space) ||
519 !isl_space_has_named_params(set->dim))
520 isl_die(ctx, isl_error_invalid,
521 "unaligned unnamed parameters", goto error);
522 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
523 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
524 return fn(multi, set);
525 error:
526 FN(MULTI(BASE),free)(multi);
527 isl_set_free(set);
528 return NULL;
530 #endif
532 #ifndef NO_GIST
533 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
534 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
536 int i;
538 multi = FN(MULTI(BASE),cow)(multi);
539 if (!multi || !context)
540 goto error;
542 for (i = 0; i < multi->n; ++i) {
543 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
544 if (!multi->p[i])
545 goto error;
548 isl_set_free(context);
549 return multi;
550 error:
551 isl_set_free(context);
552 FN(MULTI(BASE),free)(multi);
553 return NULL;
556 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
557 __isl_take isl_set *context)
559 return FN(MULTI(BASE),align_params_multi_set_and)(multi, context,
560 &FN(MULTI(BASE),gist_aligned));
563 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
564 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
566 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
567 isl_set *dom_context = isl_set_universe(space);
568 dom_context = isl_set_intersect_params(dom_context, context);
569 return FN(MULTI(BASE),gist)(multi, dom_context);
571 #endif
573 #ifndef NO_INTERSECT_DOMAIN
574 /* Transform the domain of "multi" by combining it with "domain"
575 * using "fn".
577 * The parameters of "multi" and "domain" are assumed to have been aligned.
579 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_aligned)(
580 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain,
581 __isl_give EL *(*fn)(EL *el, __isl_take isl_set *set2))
583 int i;
585 if (!multi || !domain)
586 goto error;
588 if (multi->n == 0) {
589 isl_set_free(domain);
590 return multi;
593 multi = FN(MULTI(BASE),cow)(multi);
594 if (!multi)
595 goto error;
597 for (i = 0; i < multi->n; ++i) {
598 multi->p[i] = fn(multi->p[i], isl_set_copy(domain));
599 if (!multi->p[i])
600 goto error;
603 isl_set_free(domain);
604 return multi;
605 error:
606 isl_set_free(domain);
607 FN(MULTI(BASE),free)(multi);
608 return NULL;
611 /* Intersect the domain of "multi" with "domain".
613 * The parameters of "multi" and "domain" are assumed to have been aligned.
615 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_domain_aligned)(
616 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
618 return FN(MULTI(BASE),intersect_aligned)(multi, domain,
619 &FN(EL,intersect_domain));
622 /* Intersect the domain of "multi" with "domain".
624 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_domain)(
625 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
627 return FN(MULTI(BASE),align_params_multi_set_and)(multi, domain,
628 &FN(MULTI(BASE),intersect_domain_aligned));
631 /* Intersect the parameter domain of "multi" with "domain".
633 * The parameters of "multi" and "domain" are assumed to have been aligned.
635 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_params_aligned)(
636 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
638 return FN(MULTI(BASE),intersect_aligned)(multi, domain,
639 &FN(EL,intersect_params));
642 /* Intersect the parameter domain of "multi" with "domain".
644 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_params)(
645 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
647 return FN(MULTI(BASE),align_params_multi_set_and)(multi, domain,
648 &FN(MULTI(BASE),intersect_params_aligned));
650 #endif
652 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
653 __isl_take isl_space *space, __isl_take LIST(EL) *list)
655 int i;
656 int n;
657 isl_ctx *ctx;
658 MULTI(BASE) *multi;
660 if (!space || !list)
661 goto error;
663 ctx = isl_space_get_ctx(space);
664 n = FN(FN(LIST(EL),n),BASE)(list);
665 if (n != isl_space_dim(space, isl_dim_out))
666 isl_die(ctx, isl_error_invalid,
667 "invalid number of elements in list", goto error);
669 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
670 for (i = 0; i < n; ++i) {
671 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
672 FN(FN(LIST(EL),get),BASE)(list, i));
675 isl_space_free(space);
676 FN(LIST(EL),free)(list);
677 return multi;
678 error:
679 isl_space_free(space);
680 FN(LIST(EL),free)(list);
681 return NULL;
684 #ifndef NO_IDENTITY
685 /* Create a multi expression in the given space that maps each
686 * input dimension to the corresponding output dimension.
688 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
690 int i, n;
691 isl_local_space *ls;
692 MULTI(BASE) *multi;
694 if (!space)
695 return NULL;
697 if (isl_space_is_set(space))
698 isl_die(isl_space_get_ctx(space), isl_error_invalid,
699 "expecting map space", goto error);
701 n = isl_space_dim(space, isl_dim_out);
702 if (n != isl_space_dim(space, isl_dim_in))
703 isl_die(isl_space_get_ctx(space), isl_error_invalid,
704 "number of input and output dimensions needs to be "
705 "the same", goto error);
707 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
709 if (!n) {
710 isl_space_free(space);
711 return multi;
714 space = isl_space_domain(space);
715 ls = isl_local_space_from_space(space);
717 for (i = 0; i < n; ++i) {
718 EL *el;
719 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
720 isl_dim_set, i);
721 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
724 isl_local_space_free(ls);
726 return multi;
727 error:
728 isl_space_free(space);
729 return NULL;
731 #endif
733 /* Construct a multi expression in the given space with value zero in
734 * each of the output dimensions.
736 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
738 int n;
739 MULTI(BASE) *multi;
741 if (!space)
742 return NULL;
744 n = isl_space_dim(space , isl_dim_out);
745 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
747 if (!n)
748 isl_space_free(space);
749 else {
750 int i;
751 isl_local_space *ls;
752 EL *el;
754 space = isl_space_domain(space);
755 ls = isl_local_space_from_space(space);
756 el = FN(EL,zero_on_domain)(ls);
758 for (i = 0; i < n; ++i)
759 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
760 FN(EL,copy)(el));
762 FN(EL,free)(el);
765 return multi;
768 #ifndef NO_FROM_BASE
769 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
771 MULTI(BASE) *multi;
773 multi = FN(MULTI(BASE),alloc)(FN(EL,get_space)(el));
774 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
776 return multi;
778 #endif
780 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
781 __isl_take MULTI(BASE) *multi,
782 enum isl_dim_type type, unsigned first, unsigned n)
784 int i;
785 unsigned dim;
787 multi = FN(MULTI(BASE),cow)(multi);
788 if (!multi)
789 return NULL;
791 dim = FN(MULTI(BASE),dim)(multi, type);
792 if (first + n > dim || first + n < first)
793 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
794 "index out of bounds",
795 return FN(MULTI(BASE),cow)(multi));
797 multi->space = isl_space_drop_dims(multi->space, type, first, n);
798 if (!multi->space)
799 return FN(MULTI(BASE),cow)(multi);
801 if (type == isl_dim_out) {
802 for (i = 0; i < n; ++i)
803 FN(EL,free)(multi->p[first + i]);
804 for (i = first; i + n < multi->n; ++i)
805 multi->p[i] = multi->p[i + n];
806 multi->n -= n;
808 return multi;
811 for (i = 0; i < multi->n; ++i) {
812 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
813 if (!multi->p[i])
814 return FN(MULTI(BASE),cow)(multi);
817 return multi;
820 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
822 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
823 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
824 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
825 __isl_take MULTI(BASE) *multi2))
827 isl_ctx *ctx;
829 if (!multi1 || !multi2)
830 goto error;
831 if (isl_space_match(multi1->space, isl_dim_param,
832 multi2->space, isl_dim_param))
833 return fn(multi1, multi2);
834 ctx = FN(MULTI(BASE),get_ctx)(multi1);
835 if (!isl_space_has_named_params(multi1->space) ||
836 !isl_space_has_named_params(multi2->space))
837 isl_die(ctx, isl_error_invalid,
838 "unaligned unnamed parameters", goto error);
839 multi1 = FN(MULTI(BASE),align_params)(multi1,
840 FN(MULTI(BASE),get_space)(multi2));
841 multi2 = FN(MULTI(BASE),align_params)(multi2,
842 FN(MULTI(BASE),get_space)(multi1));
843 return fn(multi1, multi2);
844 error:
845 FN(MULTI(BASE),free)(multi1);
846 FN(MULTI(BASE),free)(multi2);
847 return NULL;
850 /* Given two MULTI(BASE)s A -> B and C -> D,
851 * construct a MULTI(BASE) (A * C) -> (B, D).
853 * The parameters are assumed to have been aligned.
855 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
856 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
858 int i, n1, n2;
859 EL *el;
860 isl_space *space;
861 MULTI(BASE) *res;
863 if (!multi1 || !multi2)
864 goto error;
866 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
867 FN(MULTI(BASE),get_space)(multi2));
868 res = FN(MULTI(BASE),alloc)(space);
870 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
871 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
873 for (i = 0; i < n1; ++i) {
874 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
875 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
878 for (i = 0; i < n2; ++i) {
879 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
880 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
883 FN(MULTI(BASE),free)(multi1);
884 FN(MULTI(BASE),free)(multi2);
885 return res;
886 error:
887 FN(MULTI(BASE),free)(multi1);
888 FN(MULTI(BASE),free)(multi2);
889 return NULL;
892 /* Given two MULTI(BASE)s A -> B and C -> D,
893 * construct a MULTI(BASE) (A * C) -> (B, D).
895 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
896 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
898 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
899 &FN(MULTI(BASE),range_product_aligned));
902 /* Is the range of "multi" a wrapped relation?
904 int FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
906 if (!multi)
907 return -1;
908 return isl_space_range_is_wrapping(multi->space);
911 /* Given a function A -> [B -> C], extract the function A -> B.
913 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
914 __isl_take MULTI(BASE) *multi)
916 isl_space *space;
917 int total, keep;
919 if (!multi)
920 return NULL;
921 if (!isl_space_range_is_wrapping(multi->space))
922 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
923 "range is not a product",
924 return FN(MULTI(BASE),free)(multi));
926 space = FN(MULTI(BASE),get_space)(multi);
927 total = isl_space_dim(space, isl_dim_out);
928 space = isl_space_range_factor_domain(space);
929 keep = isl_space_dim(space, isl_dim_out);
930 multi = FN(MULTI(BASE),drop_dims)(multi,
931 isl_dim_out, keep, total - keep);
932 multi = FN(MULTI(BASE),reset_space)(multi, space);
934 return multi;
937 /* Given a function A -> [B -> C], extract the function A -> C.
939 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
940 __isl_take MULTI(BASE) *multi)
942 isl_space *space;
943 int total, keep;
945 if (!multi)
946 return NULL;
947 if (!isl_space_range_is_wrapping(multi->space))
948 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
949 "range is not a product",
950 return FN(MULTI(BASE),free)(multi));
952 space = FN(MULTI(BASE),get_space)(multi);
953 total = isl_space_dim(space, isl_dim_out);
954 space = isl_space_range_factor_range(space);
955 keep = isl_space_dim(space, isl_dim_out);
956 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
957 multi = FN(MULTI(BASE),reset_space)(multi, space);
959 return multi;
962 /* Given two MULTI(BASE)s A -> B and C -> D,
963 * construct a MULTI(BASE) [A -> C] -> [B -> D].
965 * The parameters are assumed to have been aligned.
967 __isl_give MULTI(BASE) *FN(MULTI(BASE),product_aligned)(
968 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
970 int i;
971 EL *el;
972 isl_space *space;
973 MULTI(BASE) *res;
974 int in1, in2, out1, out2;
976 in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
977 in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
978 out1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
979 out2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
980 space = isl_space_product(FN(MULTI(BASE),get_space)(multi1),
981 FN(MULTI(BASE),get_space)(multi2));
982 res = FN(MULTI(BASE),alloc)(isl_space_copy(space));
983 space = isl_space_domain(space);
985 for (i = 0; i < out1; ++i) {
986 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
987 el = FN(EL,insert_dims)(el, isl_dim_in, in1, in2);
988 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
989 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
992 for (i = 0; i < out2; ++i) {
993 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
994 el = FN(EL,insert_dims)(el, isl_dim_in, 0, in1);
995 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
996 res = FN(FN(MULTI(BASE),set),BASE)(res, out1 + i, el);
999 isl_space_free(space);
1000 FN(MULTI(BASE),free)(multi1);
1001 FN(MULTI(BASE),free)(multi2);
1002 return res;
1005 /* Given two MULTI(BASE)s A -> B and C -> D,
1006 * construct a MULTI(BASE) [A -> C] -> [B -> D].
1008 __isl_give MULTI(BASE) *FN(MULTI(BASE),product)(
1009 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1011 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1012 &FN(MULTI(BASE),product_aligned));
1015 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
1016 __isl_take MULTI(BASE) *multi)
1018 if (!multi)
1019 return NULL;
1021 if (!multi->space->nested[1])
1022 return multi;
1024 multi = FN(MULTI(BASE),cow)(multi);
1025 if (!multi)
1026 return NULL;
1028 multi->space = isl_space_flatten_range(multi->space);
1029 if (!multi->space)
1030 return FN(MULTI(BASE),free)(multi);
1032 return multi;
1035 /* Given two MULTI(BASE)s A -> B and C -> D,
1036 * construct a MULTI(BASE) (A * C) -> [B -> D].
1038 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
1039 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1041 MULTI(BASE) *multi;
1043 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
1044 multi = FN(MULTI(BASE),flatten_range)(multi);
1045 return multi;
1048 /* Given two multi expressions, "multi1"
1050 * [A] -> [B1 B2]
1052 * where B2 starts at position "pos", and "multi2"
1054 * [A] -> [D]
1056 * return the multi expression
1058 * [A] -> [B1 D B2]
1060 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
1061 __isl_take MULTI(BASE) *multi1, unsigned pos,
1062 __isl_take MULTI(BASE) *multi2)
1064 MULTI(BASE) *res;
1065 unsigned dim;
1067 if (!multi1 || !multi2)
1068 goto error;
1070 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
1071 if (pos > dim)
1072 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1073 "index out of bounds", goto error);
1075 res = FN(MULTI(BASE),copy)(multi1);
1076 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
1077 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
1079 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
1080 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
1082 return res;
1083 error:
1084 FN(MULTI(BASE),free)(multi1);
1085 FN(MULTI(BASE),free)(multi2);
1086 return NULL;
1089 /* Given two multi expressions, "multi1"
1091 * [A1 A2] -> [B1 B2]
1093 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1094 * and "multi2"
1096 * [C] -> [D]
1098 * return the multi expression
1100 * [A1 C A2] -> [B1 D B2]
1102 * We first insert input dimensions to obtain
1104 * [A1 C A2] -> [B1 B2]
1106 * and
1108 * [A1 C A2] -> [D]
1110 * and then apply range_splice.
1112 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
1113 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
1114 __isl_take MULTI(BASE) *multi2)
1116 unsigned n_in1;
1117 unsigned n_in2;
1119 if (!multi1 || !multi2)
1120 goto error;
1122 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
1123 if (in_pos > n_in1)
1124 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1125 "index out of bounds", goto error);
1127 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
1129 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
1130 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
1131 n_in1 - in_pos);
1132 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
1134 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
1135 error:
1136 FN(MULTI(BASE),free)(multi1);
1137 FN(MULTI(BASE),free)(multi2);
1138 return NULL;
1141 /* This function is currently only used from isl_aff.c
1143 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1144 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1145 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1146 __attribute__ ((unused));
1148 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1149 * return the result.
1151 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1152 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1153 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1155 int i;
1156 isl_ctx *ctx;
1158 multi1 = FN(MULTI(BASE),cow)(multi1);
1159 if (!multi1 || !multi2)
1160 goto error;
1162 ctx = FN(MULTI(BASE),get_ctx)(multi1);
1163 if (!isl_space_is_equal(multi1->space, multi2->space))
1164 isl_die(ctx, isl_error_invalid,
1165 "spaces don't match", goto error);
1167 for (i = 0; i < multi1->n; ++i) {
1168 multi1->p[i] = fn(multi1->p[i], FN(EL,copy)(multi2->p[i]));
1169 if (!multi1->p[i])
1170 goto error;
1173 FN(MULTI(BASE),free)(multi2);
1174 return multi1;
1175 error:
1176 FN(MULTI(BASE),free)(multi1);
1177 FN(MULTI(BASE),free)(multi2);
1178 return NULL;
1181 /* Multiply the elements of "multi" by "v" and return the result.
1183 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
1184 __isl_take isl_val *v)
1186 int i;
1188 if (!multi || !v)
1189 goto error;
1191 if (isl_val_is_one(v)) {
1192 isl_val_free(v);
1193 return multi;
1196 if (!isl_val_is_rat(v))
1197 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1198 "expecting rational factor", goto error);
1200 multi = FN(MULTI(BASE),cow)(multi);
1201 if (!multi)
1202 return NULL;
1204 for (i = 0; i < multi->n; ++i) {
1205 multi->p[i] = FN(EL,scale_val)(multi->p[i], isl_val_copy(v));
1206 if (!multi->p[i])
1207 goto error;
1210 isl_val_free(v);
1211 return multi;
1212 error:
1213 isl_val_free(v);
1214 return FN(MULTI(BASE),free)(multi);
1217 /* Multiply the elements of "multi" by the corresponding element of "mv"
1218 * and return the result.
1220 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1221 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1223 int i;
1225 if (!multi || !mv)
1226 goto error;
1228 if (!isl_space_tuple_match(multi->space, isl_dim_out,
1229 mv->space, isl_dim_set))
1230 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1231 "spaces don't match", goto error);
1233 multi = FN(MULTI(BASE),cow)(multi);
1234 if (!multi)
1235 return NULL;
1237 for (i = 0; i < multi->n; ++i) {
1238 isl_val *v;
1240 v = isl_multi_val_get_val(mv, i);
1241 multi->p[i] = FN(EL,scale_val)(multi->p[i], v);
1242 if (!multi->p[i])
1243 goto error;
1246 isl_multi_val_free(mv);
1247 return multi;
1248 error:
1249 isl_multi_val_free(mv);
1250 return FN(MULTI(BASE),free)(multi);
1253 /* Divide the elements of "multi" by the corresponding element of "mv"
1254 * and return the result.
1256 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1257 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1259 int i;
1261 if (!multi || !mv)
1262 goto error;
1264 if (!isl_space_tuple_match(multi->space, isl_dim_out,
1265 mv->space, isl_dim_set))
1266 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1267 "spaces don't match", goto error);
1269 multi = FN(MULTI(BASE),cow)(multi);
1270 if (!multi)
1271 return NULL;
1273 for (i = 0; i < multi->n; ++i) {
1274 isl_val *v;
1276 v = isl_multi_val_get_val(mv, i);
1277 multi->p[i] = FN(EL,scale_down_val)(multi->p[i], v);
1278 if (!multi->p[i])
1279 goto error;
1282 isl_multi_val_free(mv);
1283 return multi;
1284 error:
1285 isl_multi_val_free(mv);
1286 return FN(MULTI(BASE),free)(multi);
1289 #ifndef NO_MOVE_DIMS
1290 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1291 * to dimensions of "dst_type" at "dst_pos".
1293 * We only support moving input dimensions to parameters and vice versa.
1295 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1296 enum isl_dim_type dst_type, unsigned dst_pos,
1297 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1299 int i;
1301 if (!multi)
1302 return NULL;
1304 if (n == 0 &&
1305 !isl_space_is_named_or_nested(multi->space, src_type) &&
1306 !isl_space_is_named_or_nested(multi->space, dst_type))
1307 return multi;
1309 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1310 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1311 "cannot move output/set dimension",
1312 return FN(MULTI(BASE),free)(multi));
1313 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1314 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1315 "cannot move divs",
1316 return FN(MULTI(BASE),free)(multi));
1317 if (src_pos + n > isl_space_dim(multi->space, src_type))
1318 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1319 "range out of bounds",
1320 return FN(MULTI(BASE),free)(multi));
1321 if (dst_type == src_type)
1322 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1323 "moving dims within the same type not supported",
1324 return FN(MULTI(BASE),free)(multi));
1326 multi = FN(MULTI(BASE),cow)(multi);
1327 if (!multi)
1328 return NULL;
1330 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1331 src_type, src_pos, n);
1332 if (!multi->space)
1333 return FN(MULTI(BASE),free)(multi);
1335 for (i = 0; i < multi->n; ++i) {
1336 multi->p[i] = FN(EL,move_dims)(multi->p[i], dst_type, dst_pos,
1337 src_type, src_pos, n);
1338 if (!multi->p[i])
1339 return FN(MULTI(BASE),free)(multi);
1342 return multi;
1344 #endif
1346 /* Convert a multiple expression defined over a parameter domain
1347 * into one that is defined over a zero-dimensional set.
1349 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1350 __isl_take MULTI(BASE) *multi)
1352 isl_space *space;
1354 if (!multi)
1355 return NULL;
1356 if (!isl_space_is_set(multi->space))
1357 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1358 "not living in a set space",
1359 return FN(MULTI(BASE),free)(multi));
1361 space = FN(MULTI(BASE),get_space)(multi);
1362 space = isl_space_from_range(space);
1363 multi = FN(MULTI(BASE),reset_space)(multi, space);
1365 return multi;
1368 /* Are "multi1" and "multi2" obviously equal?
1370 int FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1371 __isl_keep MULTI(BASE) *multi2)
1373 int i;
1374 int equal;
1376 if (!multi1 || !multi2)
1377 return -1;
1378 if (multi1->n != multi2->n)
1379 return 0;
1380 equal = isl_space_is_equal(multi1->space, multi2->space);
1381 if (equal < 0 || !equal)
1382 return equal;
1384 for (i = 0; i < multi1->n; ++i) {
1385 equal = FN(EL,plain_is_equal)(multi1->p[i], multi2->p[i]);
1386 if (equal < 0 || !equal)
1387 return equal;
1390 return 1;
1393 #ifndef NO_DOMAIN
1394 /* Return the shared domain of the elements of "multi".
1396 __isl_give isl_set *FN(MULTI(BASE),domain)(__isl_take MULTI(BASE) *multi)
1398 int i;
1399 isl_set *dom;
1401 if (!multi)
1402 return NULL;
1404 dom = isl_set_universe(FN(MULTI(BASE),get_domain_space)(multi));
1405 for (i = 0; i < multi->n; ++i) {
1406 isl_set *dom_i;
1408 dom_i = FN(EL,domain)(FN(FN(MULTI(BASE),get),BASE)(multi, i));
1409 dom = isl_set_intersect(dom, dom_i);
1412 FN(MULTI(BASE),free)(multi);
1413 return dom;
1415 #endif