2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_aff_private.h>
15 #include <isl_space_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_list_private.h>
19 #include <isl/constraint.h>
22 #include <isl_config.h>
24 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
25 __isl_take isl_vec *v)
32 aff = isl_calloc_type(v->ctx, struct isl_aff);
42 isl_local_space_free(ls);
47 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
56 ctx = isl_local_space_get_ctx(ls);
57 if (!isl_local_space_divs_known(ls))
58 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
60 if (!isl_local_space_is_set(ls))
61 isl_die(ctx, isl_error_invalid,
62 "domain of affine expression should be a set",
65 total = isl_local_space_dim(ls, isl_dim_all);
66 v = isl_vec_alloc(ctx, 1 + 1 + total);
67 return isl_aff_alloc_vec(ls, v);
69 isl_local_space_free(ls);
73 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
77 aff = isl_aff_alloc(ls);
81 isl_int_set_si(aff->v->el[0], 1);
82 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
87 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
96 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
101 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
102 isl_vec_copy(aff->v));
105 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
113 return isl_aff_dup(aff);
116 void *isl_aff_free(__isl_take isl_aff *aff)
124 isl_local_space_free(aff->ls);
125 isl_vec_free(aff->v);
132 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
134 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
137 /* Externally, an isl_aff has a map space, but internally, the
138 * ls field corresponds to the domain of that space.
140 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
144 if (type == isl_dim_out)
146 if (type == isl_dim_in)
148 return isl_local_space_dim(aff->ls, type);
151 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
153 return aff ? isl_local_space_get_space(aff->ls) : NULL;
156 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
161 space = isl_local_space_get_space(aff->ls);
162 space = isl_space_from_domain(space);
163 space = isl_space_add_dims(space, isl_dim_out, 1);
167 __isl_give isl_local_space *isl_aff_get_domain_local_space(
168 __isl_keep isl_aff *aff)
170 return aff ? isl_local_space_copy(aff->ls) : NULL;
173 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
178 ls = isl_local_space_copy(aff->ls);
179 ls = isl_local_space_from_domain(ls);
180 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
184 /* Externally, an isl_aff has a map space, but internally, the
185 * ls field corresponds to the domain of that space.
187 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
188 enum isl_dim_type type, unsigned pos)
192 if (type == isl_dim_out)
194 if (type == isl_dim_in)
196 return isl_local_space_get_dim_name(aff->ls, type, pos);
199 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
200 __isl_take isl_space *dim)
202 aff = isl_aff_cow(aff);
206 aff->ls = isl_local_space_reset_space(aff->ls, dim);
208 return isl_aff_free(aff);
217 /* Reset the space of "aff". This function is called from isl_pw_templ.c
218 * and doesn't know if the space of an element object is represented
219 * directly or through its domain. It therefore passes along both.
221 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
222 __isl_take isl_space *space, __isl_take isl_space *domain)
224 isl_space_free(space);
225 return isl_aff_reset_domain_space(aff, domain);
228 /* Reorder the coefficients of the affine expression based
229 * on the given reodering.
230 * The reordering r is assumed to have been extended with the local
233 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
234 __isl_take isl_reordering *r, int n_div)
242 res = isl_vec_alloc(vec->ctx,
243 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
244 isl_seq_cpy(res->el, vec->el, 2);
245 isl_seq_clr(res->el + 2, res->size - 2);
246 for (i = 0; i < r->len; ++i)
247 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
249 isl_reordering_free(r);
254 isl_reordering_free(r);
258 /* Reorder the dimensions of the domain of "aff" according
259 * to the given reordering.
261 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
262 __isl_take isl_reordering *r)
264 aff = isl_aff_cow(aff);
268 r = isl_reordering_extend(r, aff->ls->div->n_row);
269 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
270 aff->ls->div->n_row);
271 aff->ls = isl_local_space_realign(aff->ls, r);
273 if (!aff->v || !aff->ls)
274 return isl_aff_free(aff);
279 isl_reordering_free(r);
283 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
284 __isl_take isl_space *model)
289 if (!isl_space_match(aff->ls->dim, isl_dim_param,
290 model, isl_dim_param)) {
293 model = isl_space_drop_dims(model, isl_dim_in,
294 0, isl_space_dim(model, isl_dim_in));
295 model = isl_space_drop_dims(model, isl_dim_out,
296 0, isl_space_dim(model, isl_dim_out));
297 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
298 exp = isl_reordering_extend_space(exp,
299 isl_aff_get_domain_space(aff));
300 aff = isl_aff_realign_domain(aff, exp);
303 isl_space_free(model);
306 isl_space_free(model);
311 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
316 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
319 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
326 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
327 if (equal < 0 || !equal)
330 return isl_vec_is_equal(aff1->v, aff2->v);
333 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
337 isl_int_set(*v, aff->v->el[0]);
341 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
345 isl_int_set(*v, aff->v->el[1]);
349 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
350 enum isl_dim_type type, int pos, isl_int *v)
355 if (type == isl_dim_out)
356 isl_die(aff->v->ctx, isl_error_invalid,
357 "output/set dimension does not have a coefficient",
359 if (type == isl_dim_in)
362 if (pos >= isl_local_space_dim(aff->ls, type))
363 isl_die(aff->v->ctx, isl_error_invalid,
364 "position out of bounds", return -1);
366 pos += isl_local_space_offset(aff->ls, type);
367 isl_int_set(*v, aff->v->el[1 + pos]);
372 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
374 aff = isl_aff_cow(aff);
378 aff->v = isl_vec_cow(aff->v);
380 return isl_aff_free(aff);
382 isl_int_set(aff->v->el[0], v);
387 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
389 aff = isl_aff_cow(aff);
393 aff->v = isl_vec_cow(aff->v);
395 return isl_aff_free(aff);
397 isl_int_set(aff->v->el[1], v);
402 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
404 if (isl_int_is_zero(v))
407 aff = isl_aff_cow(aff);
411 aff->v = isl_vec_cow(aff->v);
413 return isl_aff_free(aff);
415 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
420 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
425 isl_int_set_si(t, v);
426 aff = isl_aff_add_constant(aff, t);
432 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
434 aff = isl_aff_cow(aff);
438 aff->v = isl_vec_cow(aff->v);
440 return isl_aff_free(aff);
442 isl_int_set_si(aff->v->el[1], v);
447 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
448 enum isl_dim_type type, int pos, isl_int v)
453 if (type == isl_dim_out)
454 isl_die(aff->v->ctx, isl_error_invalid,
455 "output/set dimension does not have a coefficient",
456 return isl_aff_free(aff));
457 if (type == isl_dim_in)
460 if (pos >= isl_local_space_dim(aff->ls, type))
461 isl_die(aff->v->ctx, isl_error_invalid,
462 "position out of bounds", return isl_aff_free(aff));
464 aff = isl_aff_cow(aff);
468 aff->v = isl_vec_cow(aff->v);
470 return isl_aff_free(aff);
472 pos += isl_local_space_offset(aff->ls, type);
473 isl_int_set(aff->v->el[1 + pos], v);
478 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
479 enum isl_dim_type type, int pos, int v)
484 if (type == isl_dim_out)
485 isl_die(aff->v->ctx, isl_error_invalid,
486 "output/set dimension does not have a coefficient",
487 return isl_aff_free(aff));
488 if (type == isl_dim_in)
491 if (pos >= isl_local_space_dim(aff->ls, type))
492 isl_die(aff->v->ctx, isl_error_invalid,
493 "position out of bounds", return isl_aff_free(aff));
495 aff = isl_aff_cow(aff);
499 aff->v = isl_vec_cow(aff->v);
501 return isl_aff_free(aff);
503 pos += isl_local_space_offset(aff->ls, type);
504 isl_int_set_si(aff->v->el[1 + pos], v);
509 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
510 enum isl_dim_type type, int pos, isl_int v)
515 if (type == isl_dim_out)
516 isl_die(aff->v->ctx, isl_error_invalid,
517 "output/set dimension does not have a coefficient",
518 return isl_aff_free(aff));
519 if (type == isl_dim_in)
522 if (pos >= isl_local_space_dim(aff->ls, type))
523 isl_die(aff->v->ctx, isl_error_invalid,
524 "position out of bounds", return isl_aff_free(aff));
526 aff = isl_aff_cow(aff);
530 aff->v = isl_vec_cow(aff->v);
532 return isl_aff_free(aff);
534 pos += isl_local_space_offset(aff->ls, type);
535 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
540 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
541 enum isl_dim_type type, int pos, int v)
546 isl_int_set_si(t, v);
547 aff = isl_aff_add_coefficient(aff, type, pos, t);
553 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
558 return isl_local_space_get_div(aff->ls, pos);
561 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
563 aff = isl_aff_cow(aff);
566 aff->v = isl_vec_cow(aff->v);
568 return isl_aff_free(aff);
570 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
575 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
579 aff->v = isl_vec_normalize(aff->v);
581 return isl_aff_free(aff);
585 /* Given f, return floor(f).
586 * If f is an integer expression, then just return f.
587 * Otherwise, if f = g/m, write g = q m + r,
588 * create a new div d = [r/m] and return the expression q + d.
589 * The coefficients in r are taken to lie between -m/2 and m/2.
591 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
601 if (isl_int_is_one(aff->v->el[0]))
604 aff = isl_aff_cow(aff);
608 aff->v = isl_vec_cow(aff->v);
609 div = isl_vec_copy(aff->v);
610 div = isl_vec_cow(div);
612 return isl_aff_free(aff);
614 ctx = isl_aff_get_ctx(aff);
615 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
616 for (i = 1; i < aff->v->size; ++i) {
617 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
618 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
619 if (isl_int_gt(div->el[i], aff->v->el[0])) {
620 isl_int_sub(div->el[i], div->el[i], div->el[0]);
621 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
625 aff->ls = isl_local_space_add_div(aff->ls, div);
627 return isl_aff_free(aff);
630 aff->v = isl_vec_extend(aff->v, size + 1);
632 return isl_aff_free(aff);
633 isl_int_set_si(aff->v->el[0], 1);
634 isl_int_set_si(aff->v->el[size], 1);
641 * aff mod m = aff - m * floor(aff/m)
643 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
647 res = isl_aff_copy(aff);
648 aff = isl_aff_scale_down(aff, m);
649 aff = isl_aff_floor(aff);
650 aff = isl_aff_scale(aff, m);
651 res = isl_aff_sub(res, aff);
658 * pwaff mod m = pwaff - m * floor(pwaff/m)
660 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
664 res = isl_pw_aff_copy(pwaff);
665 pwaff = isl_pw_aff_scale_down(pwaff, m);
666 pwaff = isl_pw_aff_floor(pwaff);
667 pwaff = isl_pw_aff_scale(pwaff, m);
668 res = isl_pw_aff_sub(res, pwaff);
673 /* Given f, return ceil(f).
674 * If f is an integer expression, then just return f.
675 * Otherwise, create a new div d = [-f] and return the expression -d.
677 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
682 if (isl_int_is_one(aff->v->el[0]))
685 aff = isl_aff_neg(aff);
686 aff = isl_aff_floor(aff);
687 aff = isl_aff_neg(aff);
692 /* Apply the expansion computed by isl_merge_divs.
693 * The expansion itself is given by "exp" while the resulting
694 * list of divs is given by "div".
696 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
697 __isl_take isl_mat *div, int *exp)
704 aff = isl_aff_cow(aff);
708 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
709 new_n_div = isl_mat_rows(div);
710 if (new_n_div < old_n_div)
711 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
712 "not an expansion", goto error);
714 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
718 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
720 for (i = new_n_div - 1; i >= 0; --i) {
721 if (j >= 0 && exp[j] == i) {
723 isl_int_swap(aff->v->el[offset + i],
724 aff->v->el[offset + j]);
727 isl_int_set_si(aff->v->el[offset + i], 0);
730 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
741 /* Add two affine expressions that live in the same local space.
743 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
744 __isl_take isl_aff *aff2)
748 aff1 = isl_aff_cow(aff1);
752 aff1->v = isl_vec_cow(aff1->v);
758 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
759 isl_int_divexact(f, aff2->v->el[0], gcd);
760 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
761 isl_int_divexact(f, aff1->v->el[0], gcd);
762 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
763 isl_int_divexact(f, aff2->v->el[0], gcd);
764 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
776 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
777 __isl_take isl_aff *aff2)
787 ctx = isl_aff_get_ctx(aff1);
788 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
789 isl_die(ctx, isl_error_invalid,
790 "spaces don't match", goto error);
792 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
793 return add_expanded(aff1, aff2);
795 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
796 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
800 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
801 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
802 aff2 = isl_aff_expand_divs(aff2, div, exp2);
806 return add_expanded(aff1, aff2);
815 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
816 __isl_take isl_aff *aff2)
818 return isl_aff_add(aff1, isl_aff_neg(aff2));
821 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
825 if (isl_int_is_one(f))
828 aff = isl_aff_cow(aff);
831 aff->v = isl_vec_cow(aff->v);
833 return isl_aff_free(aff);
836 isl_int_gcd(gcd, aff->v->el[0], f);
837 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
838 isl_int_divexact(gcd, f, gcd);
839 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
845 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
849 if (isl_int_is_one(f))
852 aff = isl_aff_cow(aff);
855 aff->v = isl_vec_cow(aff->v);
857 return isl_aff_free(aff);
860 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
861 isl_int_gcd(gcd, gcd, f);
862 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
863 isl_int_divexact(gcd, f, gcd);
864 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
870 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
878 isl_int_set_ui(v, f);
879 aff = isl_aff_scale_down(aff, v);
885 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
886 enum isl_dim_type type, unsigned pos, const char *s)
888 aff = isl_aff_cow(aff);
891 if (type == isl_dim_out)
892 isl_die(aff->v->ctx, isl_error_invalid,
893 "cannot set name of output/set dimension",
894 return isl_aff_free(aff));
895 if (type == isl_dim_in)
897 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
899 return isl_aff_free(aff);
904 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
905 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
907 aff = isl_aff_cow(aff);
909 return isl_id_free(id);
910 if (type == isl_dim_out)
911 isl_die(aff->v->ctx, isl_error_invalid,
912 "cannot set name of output/set dimension",
914 if (type == isl_dim_in)
916 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
918 return isl_aff_free(aff);
927 /* Exploit the equalities in "eq" to simplify the affine expression
928 * and the expressions of the integer divisions in the local space.
929 * The integer divisions in this local space are assumed to appear
930 * as regular dimensions in "eq".
932 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
933 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
942 isl_basic_set_free(eq);
946 aff = isl_aff_cow(aff);
950 aff->ls = isl_local_space_substitute_equalities(aff->ls,
951 isl_basic_set_copy(eq));
955 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
957 for (i = 0; i < eq->n_eq; ++i) {
958 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
959 if (j < 0 || j == 0 || j >= total)
962 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
966 isl_basic_set_free(eq);
967 aff = isl_aff_normalize(aff);
970 isl_basic_set_free(eq);
975 /* Exploit the equalities in "eq" to simplify the affine expression
976 * and the expressions of the integer divisions in the local space.
978 static __isl_give isl_aff *isl_aff_substitute_equalities(
979 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
985 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
987 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
988 return isl_aff_substitute_equalities_lifted(aff, eq);
990 isl_basic_set_free(eq);
995 /* Look for equalities among the variables shared by context and aff
996 * and the integer divisions of aff, if any.
997 * The equalities are then used to eliminate coefficients and/or integer
998 * divisions from aff.
1000 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1001 __isl_take isl_set *context)
1003 isl_basic_set *hull;
1008 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1010 isl_basic_set *bset;
1011 isl_local_space *ls;
1012 context = isl_set_add_dims(context, isl_dim_set, n_div);
1013 ls = isl_aff_get_domain_local_space(aff);
1014 bset = isl_basic_set_from_local_space(ls);
1015 bset = isl_basic_set_lift(bset);
1016 bset = isl_basic_set_flatten(bset);
1017 context = isl_set_intersect(context,
1018 isl_set_from_basic_set(bset));
1021 hull = isl_set_affine_hull(context);
1022 return isl_aff_substitute_equalities_lifted(aff, hull);
1025 isl_set_free(context);
1029 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1030 __isl_take isl_set *context)
1032 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1033 dom_context = isl_set_intersect_params(dom_context, context);
1034 return isl_aff_gist(aff, dom_context);
1037 /* Return a basic set containing those elements in the space
1038 * of aff where it is non-negative.
1040 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1042 isl_constraint *ineq;
1043 isl_basic_set *bset;
1045 ineq = isl_inequality_from_aff(aff);
1047 bset = isl_basic_set_from_constraint(ineq);
1048 bset = isl_basic_set_simplify(bset);
1052 /* Return a basic set containing those elements in the space
1053 * of aff where it is zero.
1055 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1057 isl_constraint *ineq;
1058 isl_basic_set *bset;
1060 ineq = isl_equality_from_aff(aff);
1062 bset = isl_basic_set_from_constraint(ineq);
1063 bset = isl_basic_set_simplify(bset);
1067 /* Return a basic set containing those elements in the shared space
1068 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1070 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1071 __isl_take isl_aff *aff2)
1073 aff1 = isl_aff_sub(aff1, aff2);
1075 return isl_aff_nonneg_basic_set(aff1);
1078 /* Return a basic set containing those elements in the shared space
1079 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1081 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1082 __isl_take isl_aff *aff2)
1084 return isl_aff_ge_basic_set(aff2, aff1);
1087 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1088 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1090 aff1 = isl_aff_add(aff1, aff2);
1091 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1095 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1103 /* Check whether the given affine expression has non-zero coefficient
1104 * for any dimension in the given range or if any of these dimensions
1105 * appear with non-zero coefficients in any of the integer divisions
1106 * involved in the affine expression.
1108 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1109 enum isl_dim_type type, unsigned first, unsigned n)
1121 ctx = isl_aff_get_ctx(aff);
1122 if (first + n > isl_aff_dim(aff, type))
1123 isl_die(ctx, isl_error_invalid,
1124 "range out of bounds", return -1);
1126 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1130 first += isl_local_space_offset(aff->ls, type) - 1;
1131 for (i = 0; i < n; ++i)
1132 if (active[first + i]) {
1145 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1146 enum isl_dim_type type, unsigned first, unsigned n)
1152 if (type == isl_dim_out)
1153 isl_die(aff->v->ctx, isl_error_invalid,
1154 "cannot drop output/set dimension",
1155 return isl_aff_free(aff));
1156 if (type == isl_dim_in)
1158 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1161 ctx = isl_aff_get_ctx(aff);
1162 if (first + n > isl_local_space_dim(aff->ls, type))
1163 isl_die(ctx, isl_error_invalid, "range out of bounds",
1164 return isl_aff_free(aff));
1166 aff = isl_aff_cow(aff);
1170 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1172 return isl_aff_free(aff);
1174 first += 1 + isl_local_space_offset(aff->ls, type);
1175 aff->v = isl_vec_drop_els(aff->v, first, n);
1177 return isl_aff_free(aff);
1182 /* Project the domain of the affine expression onto its parameter space.
1183 * The affine expression may not involve any of the domain dimensions.
1185 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1191 n = isl_aff_dim(aff, isl_dim_in);
1192 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1194 return isl_aff_free(aff);
1196 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1197 "affine expression involves some of the domain dimensions",
1198 return isl_aff_free(aff));
1199 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1200 space = isl_aff_get_domain_space(aff);
1201 space = isl_space_params(space);
1202 aff = isl_aff_reset_domain_space(aff, space);
1206 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1207 enum isl_dim_type type, unsigned first, unsigned n)
1213 if (type == isl_dim_out)
1214 isl_die(aff->v->ctx, isl_error_invalid,
1215 "cannot insert output/set dimensions",
1216 return isl_aff_free(aff));
1217 if (type == isl_dim_in)
1219 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1222 ctx = isl_aff_get_ctx(aff);
1223 if (first > isl_local_space_dim(aff->ls, type))
1224 isl_die(ctx, isl_error_invalid, "position out of bounds",
1225 return isl_aff_free(aff));
1227 aff = isl_aff_cow(aff);
1231 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1233 return isl_aff_free(aff);
1235 first += 1 + isl_local_space_offset(aff->ls, type);
1236 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1238 return isl_aff_free(aff);
1243 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1244 enum isl_dim_type type, unsigned n)
1248 pos = isl_aff_dim(aff, type);
1250 return isl_aff_insert_dims(aff, type, pos, n);
1253 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1254 enum isl_dim_type type, unsigned n)
1258 pos = isl_pw_aff_dim(pwaff, type);
1260 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1263 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1265 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1266 return isl_pw_aff_alloc(dom, aff);
1270 #define PW isl_pw_aff
1274 #define EL_IS_ZERO is_empty
1278 #define IS_ZERO is_empty
1281 #undef DEFAULT_IS_ZERO
1282 #define DEFAULT_IS_ZERO 0
1286 #define NO_MOVE_DIMS
1290 #include <isl_pw_templ.c>
1292 static __isl_give isl_set *align_params_pw_pw_set_and(
1293 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1294 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1295 __isl_take isl_pw_aff *pwaff2))
1297 if (!pwaff1 || !pwaff2)
1299 if (isl_space_match(pwaff1->dim, isl_dim_param,
1300 pwaff2->dim, isl_dim_param))
1301 return fn(pwaff1, pwaff2);
1302 if (!isl_space_has_named_params(pwaff1->dim) ||
1303 !isl_space_has_named_params(pwaff2->dim))
1304 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1305 "unaligned unnamed parameters", goto error);
1306 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1307 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1308 return fn(pwaff1, pwaff2);
1310 isl_pw_aff_free(pwaff1);
1311 isl_pw_aff_free(pwaff2);
1315 /* Compute a piecewise quasi-affine expression with a domain that
1316 * is the union of those of pwaff1 and pwaff2 and such that on each
1317 * cell, the quasi-affine expression is the better (according to cmp)
1318 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1319 * is defined on a given cell, then the associated expression
1320 * is the defined one.
1322 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1323 __isl_take isl_pw_aff *pwaff2,
1324 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1325 __isl_take isl_aff *aff2))
1332 if (!pwaff1 || !pwaff2)
1335 ctx = isl_space_get_ctx(pwaff1->dim);
1336 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1337 isl_die(ctx, isl_error_invalid,
1338 "arguments should live in same space", goto error);
1340 if (isl_pw_aff_is_empty(pwaff1)) {
1341 isl_pw_aff_free(pwaff1);
1345 if (isl_pw_aff_is_empty(pwaff2)) {
1346 isl_pw_aff_free(pwaff2);
1350 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1351 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1353 for (i = 0; i < pwaff1->n; ++i) {
1354 set = isl_set_copy(pwaff1->p[i].set);
1355 for (j = 0; j < pwaff2->n; ++j) {
1356 struct isl_set *common;
1359 common = isl_set_intersect(
1360 isl_set_copy(pwaff1->p[i].set),
1361 isl_set_copy(pwaff2->p[j].set));
1362 better = isl_set_from_basic_set(cmp(
1363 isl_aff_copy(pwaff2->p[j].aff),
1364 isl_aff_copy(pwaff1->p[i].aff)));
1365 better = isl_set_intersect(common, better);
1366 if (isl_set_plain_is_empty(better)) {
1367 isl_set_free(better);
1370 set = isl_set_subtract(set, isl_set_copy(better));
1372 res = isl_pw_aff_add_piece(res, better,
1373 isl_aff_copy(pwaff2->p[j].aff));
1375 res = isl_pw_aff_add_piece(res, set,
1376 isl_aff_copy(pwaff1->p[i].aff));
1379 for (j = 0; j < pwaff2->n; ++j) {
1380 set = isl_set_copy(pwaff2->p[j].set);
1381 for (i = 0; i < pwaff1->n; ++i)
1382 set = isl_set_subtract(set,
1383 isl_set_copy(pwaff1->p[i].set));
1384 res = isl_pw_aff_add_piece(res, set,
1385 isl_aff_copy(pwaff2->p[j].aff));
1388 isl_pw_aff_free(pwaff1);
1389 isl_pw_aff_free(pwaff2);
1393 isl_pw_aff_free(pwaff1);
1394 isl_pw_aff_free(pwaff2);
1398 /* Compute a piecewise quasi-affine expression with a domain that
1399 * is the union of those of pwaff1 and pwaff2 and such that on each
1400 * cell, the quasi-affine expression is the maximum of those of pwaff1
1401 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1402 * cell, then the associated expression is the defined one.
1404 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1405 __isl_take isl_pw_aff *pwaff2)
1407 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1410 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1411 __isl_take isl_pw_aff *pwaff2)
1413 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1417 /* Compute a piecewise quasi-affine expression with a domain that
1418 * is the union of those of pwaff1 and pwaff2 and such that on each
1419 * cell, the quasi-affine expression is the minimum of those of pwaff1
1420 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1421 * cell, then the associated expression is the defined one.
1423 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1424 __isl_take isl_pw_aff *pwaff2)
1426 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1429 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1430 __isl_take isl_pw_aff *pwaff2)
1432 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1436 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1437 __isl_take isl_pw_aff *pwaff2, int max)
1440 return isl_pw_aff_union_max(pwaff1, pwaff2);
1442 return isl_pw_aff_union_min(pwaff1, pwaff2);
1445 /* Construct a map with as domain the domain of pwaff and
1446 * one-dimensional range corresponding to the affine expressions.
1448 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1457 dim = isl_pw_aff_get_space(pwaff);
1458 map = isl_map_empty(dim);
1460 for (i = 0; i < pwaff->n; ++i) {
1461 isl_basic_map *bmap;
1464 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1465 map_i = isl_map_from_basic_map(bmap);
1466 map_i = isl_map_intersect_domain(map_i,
1467 isl_set_copy(pwaff->p[i].set));
1468 map = isl_map_union_disjoint(map, map_i);
1471 isl_pw_aff_free(pwaff);
1476 /* Construct a map with as domain the domain of pwaff and
1477 * one-dimensional range corresponding to the affine expressions.
1479 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1483 if (isl_space_is_set(pwaff->dim))
1484 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1485 "space of input is not a map",
1486 return isl_pw_aff_free(pwaff));
1487 return map_from_pw_aff(pwaff);
1490 /* Construct a one-dimensional set with as parameter domain
1491 * the domain of pwaff and the single set dimension
1492 * corresponding to the affine expressions.
1494 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1498 if (!isl_space_is_set(pwaff->dim))
1499 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1500 "space of input is not a set",
1501 return isl_pw_aff_free(pwaff));
1502 return map_from_pw_aff(pwaff);
1505 /* Return a set containing those elements in the domain
1506 * of pwaff where it is non-negative.
1508 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1516 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1518 for (i = 0; i < pwaff->n; ++i) {
1519 isl_basic_set *bset;
1522 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1523 set_i = isl_set_from_basic_set(bset);
1524 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1525 set = isl_set_union_disjoint(set, set_i);
1528 isl_pw_aff_free(pwaff);
1533 /* Return a set containing those elements in the domain
1534 * of pwaff where it is zero (if complement is 0) or not zero
1535 * (if complement is 1).
1537 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1546 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1548 for (i = 0; i < pwaff->n; ++i) {
1549 isl_basic_set *bset;
1550 isl_set *set_i, *zero;
1552 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1553 zero = isl_set_from_basic_set(bset);
1554 set_i = isl_set_copy(pwaff->p[i].set);
1556 set_i = isl_set_subtract(set_i, zero);
1558 set_i = isl_set_intersect(set_i, zero);
1559 set = isl_set_union_disjoint(set, set_i);
1562 isl_pw_aff_free(pwaff);
1567 /* Return a set containing those elements in the domain
1568 * of pwaff where it is zero.
1570 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1572 return pw_aff_zero_set(pwaff, 0);
1575 /* Return a set containing those elements in the domain
1576 * of pwaff where it is not zero.
1578 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1580 return pw_aff_zero_set(pwaff, 1);
1583 /* Return a set containing those elements in the shared domain
1584 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1586 * We compute the difference on the shared domain and then construct
1587 * the set of values where this difference is non-negative.
1588 * If strict is set, we first subtract 1 from the difference.
1589 * If equal is set, we only return the elements where pwaff1 and pwaff2
1592 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1593 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1595 isl_set *set1, *set2;
1597 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1598 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1599 set1 = isl_set_intersect(set1, set2);
1600 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1601 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1602 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1605 isl_space *dim = isl_set_get_space(set1);
1607 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1608 aff = isl_aff_add_constant_si(aff, -1);
1609 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1614 return isl_pw_aff_zero_set(pwaff1);
1615 return isl_pw_aff_nonneg_set(pwaff1);
1618 /* Return a set containing those elements in the shared domain
1619 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1621 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1622 __isl_take isl_pw_aff *pwaff2)
1624 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1627 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1628 __isl_take isl_pw_aff *pwaff2)
1630 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1633 /* Return a set containing those elements in the shared domain
1634 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1636 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1637 __isl_take isl_pw_aff *pwaff2)
1639 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1642 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1643 __isl_take isl_pw_aff *pwaff2)
1645 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1648 /* Return a set containing those elements in the shared domain
1649 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1651 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1652 __isl_take isl_pw_aff *pwaff2)
1654 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1657 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1658 __isl_take isl_pw_aff *pwaff2)
1660 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1663 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1664 __isl_take isl_pw_aff *pwaff2)
1666 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1669 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1670 __isl_take isl_pw_aff *pwaff2)
1672 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1675 /* Return a set containing those elements in the shared domain
1676 * of the elements of list1 and list2 where each element in list1
1677 * has the relation specified by "fn" with each element in list2.
1679 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1680 __isl_take isl_pw_aff_list *list2,
1681 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1682 __isl_take isl_pw_aff *pwaff2))
1688 if (!list1 || !list2)
1691 ctx = isl_pw_aff_list_get_ctx(list1);
1692 if (list1->n < 1 || list2->n < 1)
1693 isl_die(ctx, isl_error_invalid,
1694 "list should contain at least one element", goto error);
1696 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1697 for (i = 0; i < list1->n; ++i)
1698 for (j = 0; j < list2->n; ++j) {
1701 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1702 isl_pw_aff_copy(list2->p[j]));
1703 set = isl_set_intersect(set, set_ij);
1706 isl_pw_aff_list_free(list1);
1707 isl_pw_aff_list_free(list2);
1710 isl_pw_aff_list_free(list1);
1711 isl_pw_aff_list_free(list2);
1715 /* Return a set containing those elements in the shared domain
1716 * of the elements of list1 and list2 where each element in list1
1717 * is equal to each element in list2.
1719 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1720 __isl_take isl_pw_aff_list *list2)
1722 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1725 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1726 __isl_take isl_pw_aff_list *list2)
1728 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1731 /* Return a set containing those elements in the shared domain
1732 * of the elements of list1 and list2 where each element in list1
1733 * is less than or equal to each element in list2.
1735 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1736 __isl_take isl_pw_aff_list *list2)
1738 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1741 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1742 __isl_take isl_pw_aff_list *list2)
1744 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1747 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1748 __isl_take isl_pw_aff_list *list2)
1750 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1753 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1754 __isl_take isl_pw_aff_list *list2)
1756 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1760 /* Return a set containing those elements in the shared domain
1761 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1763 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1764 __isl_take isl_pw_aff *pwaff2)
1766 isl_set *set_lt, *set_gt;
1768 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1769 isl_pw_aff_copy(pwaff2));
1770 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1771 return isl_set_union_disjoint(set_lt, set_gt);
1774 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1775 __isl_take isl_pw_aff *pwaff2)
1777 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1780 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1785 if (isl_int_is_one(v))
1787 if (!isl_int_is_pos(v))
1788 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1789 "factor needs to be positive",
1790 return isl_pw_aff_free(pwaff));
1791 pwaff = isl_pw_aff_cow(pwaff);
1797 for (i = 0; i < pwaff->n; ++i) {
1798 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1799 if (!pwaff->p[i].aff)
1800 return isl_pw_aff_free(pwaff);
1806 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1810 pwaff = isl_pw_aff_cow(pwaff);
1816 for (i = 0; i < pwaff->n; ++i) {
1817 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1818 if (!pwaff->p[i].aff)
1819 return isl_pw_aff_free(pwaff);
1825 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1829 pwaff = isl_pw_aff_cow(pwaff);
1835 for (i = 0; i < pwaff->n; ++i) {
1836 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1837 if (!pwaff->p[i].aff)
1838 return isl_pw_aff_free(pwaff);
1844 /* Return an affine expression that is equal to pwaff_true for elements
1845 * in "cond" and to pwaff_false for elements not in "cond".
1846 * That is, return cond ? pwaff_true : pwaff_false;
1848 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1849 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1853 comp = isl_set_complement(isl_set_copy(cond));
1854 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1855 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1857 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1860 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1865 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1868 /* Check whether pwaff is a piecewise constant.
1870 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1877 for (i = 0; i < pwaff->n; ++i) {
1878 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1879 if (is_cst < 0 || !is_cst)
1886 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1887 __isl_take isl_aff *aff2)
1889 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1890 return isl_aff_mul(aff2, aff1);
1892 if (!isl_aff_is_cst(aff2))
1893 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1894 "at least one affine expression should be constant",
1897 aff1 = isl_aff_cow(aff1);
1901 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1902 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1912 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1913 __isl_take isl_pw_aff *pwaff2)
1915 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1918 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1919 __isl_take isl_pw_aff *pwaff2)
1921 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1924 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1925 __isl_take isl_pw_aff *pwaff2)
1927 return isl_pw_aff_union_add_(pwaff1, pwaff2);
1930 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1931 __isl_take isl_pw_aff *pwaff2)
1933 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
1936 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1937 __isl_take isl_pw_aff *pwaff2)
1939 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1942 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1943 __isl_take isl_pw_aff *pwaff2)
1947 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1948 isl_pw_aff_copy(pwaff2));
1949 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1952 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1953 __isl_take isl_pw_aff *pwaff2)
1955 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1958 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1959 __isl_take isl_pw_aff *pwaff2)
1963 le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1964 isl_pw_aff_copy(pwaff2));
1965 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1968 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1969 __isl_take isl_pw_aff *pwaff2)
1971 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1974 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1975 __isl_take isl_pw_aff_list *list,
1976 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1977 __isl_take isl_pw_aff *pwaff2))
1986 ctx = isl_pw_aff_list_get_ctx(list);
1988 isl_die(ctx, isl_error_invalid,
1989 "list should contain at least one element",
1990 return isl_pw_aff_list_free(list));
1992 res = isl_pw_aff_copy(list->p[0]);
1993 for (i = 1; i < list->n; ++i)
1994 res = fn(res, isl_pw_aff_copy(list->p[i]));
1996 isl_pw_aff_list_free(list);
2000 /* Return an isl_pw_aff that maps each element in the intersection of the
2001 * domains of the elements of list to the minimal corresponding affine
2004 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2006 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2009 /* Return an isl_pw_aff that maps each element in the intersection of the
2010 * domains of the elements of list to the maximal corresponding affine
2013 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2015 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2021 #include <isl_multi_templ.c>
2023 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2024 __isl_take isl_multi_aff *maff2)
2029 maff1 = isl_multi_aff_cow(maff1);
2030 if (!maff1 || !maff2)
2033 ctx = isl_multi_aff_get_ctx(maff1);
2034 if (!isl_space_is_equal(maff1->space, maff2->space))
2035 isl_die(ctx, isl_error_invalid,
2036 "spaces don't match", goto error);
2038 for (i = 0; i < maff1->n; ++i) {
2039 maff1->p[i] = isl_aff_add(maff1->p[i],
2040 isl_aff_copy(maff2->p[i]));
2045 isl_multi_aff_free(maff2);
2048 isl_multi_aff_free(maff1);
2049 isl_multi_aff_free(maff2);
2053 /* Exploit the equalities in "eq" to simplify the affine expressions.
2055 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2056 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2060 maff = isl_multi_aff_cow(maff);
2064 for (i = 0; i < maff->n; ++i) {
2065 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2066 isl_basic_set_copy(eq));
2071 isl_basic_set_free(eq);
2074 isl_basic_set_free(eq);
2075 isl_multi_aff_free(maff);
2079 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2084 maff = isl_multi_aff_cow(maff);
2088 for (i = 0; i < maff->n; ++i) {
2089 maff->p[i] = isl_aff_scale(maff->p[i], f);
2091 return isl_multi_aff_free(maff);
2097 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2098 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2100 maff1 = isl_multi_aff_add(maff1, maff2);
2101 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2105 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2113 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2114 __isl_keep isl_multi_aff *maff2)
2119 if (!maff1 || !maff2)
2121 if (maff1->n != maff2->n)
2123 equal = isl_space_is_equal(maff1->space, maff2->space);
2124 if (equal < 0 || !equal)
2127 for (i = 0; i < maff1->n; ++i) {
2128 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2129 if (equal < 0 || !equal)
2136 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2137 __isl_take isl_multi_aff *maff,
2138 enum isl_dim_type type, unsigned pos, const char *s)
2142 maff = isl_multi_aff_cow(maff);
2146 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2148 return isl_multi_aff_free(maff);
2149 for (i = 0; i < maff->n; ++i) {
2150 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2152 return isl_multi_aff_free(maff);
2158 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2159 enum isl_dim_type type, unsigned first, unsigned n)
2163 maff = isl_multi_aff_cow(maff);
2167 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2169 return isl_multi_aff_free(maff);
2171 if (type == isl_dim_out) {
2172 for (i = 0; i < n; ++i)
2173 isl_aff_free(maff->p[first + i]);
2174 for (i = first; i + n < maff->n; ++i)
2175 maff->p[i] = maff->p[i + n];
2180 for (i = 0; i < maff->n; ++i) {
2181 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2183 return isl_multi_aff_free(maff);
2190 #define PW isl_pw_multi_aff
2192 #define EL isl_multi_aff
2194 #define EL_IS_ZERO is_empty
2198 #define IS_ZERO is_empty
2201 #undef DEFAULT_IS_ZERO
2202 #define DEFAULT_IS_ZERO 0
2207 #define NO_INVOLVES_DIMS
2208 #define NO_MOVE_DIMS
2209 #define NO_INSERT_DIMS
2213 #include <isl_pw_templ.c>
2215 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2216 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2218 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2219 &isl_multi_aff_add);
2222 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2223 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2225 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2229 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2230 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2232 return isl_pw_multi_aff_union_add_(pma1, pma2);
2235 /* Construct a map mapping the domain the piecewise multi-affine expression
2236 * to its range, with each dimension in the range equated to the
2237 * corresponding affine expression on its cell.
2239 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2247 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2249 for (i = 0; i < pma->n; ++i) {
2250 isl_multi_aff *maff;
2251 isl_basic_map *bmap;
2254 maff = isl_multi_aff_copy(pma->p[i].maff);
2255 bmap = isl_basic_map_from_multi_aff(maff);
2256 map_i = isl_map_from_basic_map(bmap);
2257 map_i = isl_map_intersect_domain(map_i,
2258 isl_set_copy(pma->p[i].set));
2259 map = isl_map_union_disjoint(map, map_i);
2262 isl_pw_multi_aff_free(pma);
2266 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2268 if (!isl_space_is_set(pma->dim))
2269 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2270 "isl_pw_multi_aff cannot be converted into an isl_set",
2271 return isl_pw_multi_aff_free(pma));
2273 return isl_map_from_pw_multi_aff(pma);
2276 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2277 * This obivously only works if the input "map" is single-valued.
2278 * If so, we compute the lexicographic minimum of the image in the form
2279 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2280 * to its lexicographic minimum.
2281 * If the input is not single-valued, we produce an error.
2283 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2287 isl_pw_multi_aff *pma;
2292 sv = isl_map_is_single_valued(map);
2296 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2297 "map is not single-valued", goto error);
2298 map = isl_map_make_disjoint(map);
2302 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2304 for (i = 0; i < map->n; ++i) {
2305 isl_pw_multi_aff *pma_i;
2306 isl_basic_map *bmap;
2307 bmap = isl_basic_map_copy(map->p[i]);
2308 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2309 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2319 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2321 return isl_pw_multi_aff_from_map(set);
2324 /* Plug in "subs" for dimension "type", "pos" of "aff".
2326 * Let i be the dimension to replace and let "subs" be of the form
2330 * and "aff" of the form
2336 * floor((a f + d g')/(m d))
2338 * where g' is the result of plugging in "subs" in each of the integer
2341 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2342 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2347 aff = isl_aff_cow(aff);
2349 return isl_aff_free(aff);
2351 ctx = isl_aff_get_ctx(aff);
2352 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2353 isl_die(ctx, isl_error_invalid,
2354 "spaces don't match", return isl_aff_free(aff));
2355 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2356 isl_die(ctx, isl_error_unsupported,
2357 "cannot handle divs yet", return isl_aff_free(aff));
2359 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2361 return isl_aff_free(aff);
2363 aff->v = isl_vec_cow(aff->v);
2365 return isl_aff_free(aff);
2367 pos += isl_local_space_offset(aff->ls, type);
2370 isl_int_set(v, aff->v->el[1 + pos]);
2371 isl_int_set_si(aff->v->el[1 + pos], 0);
2372 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2373 v, subs->v->el + 1, subs->v->size - 1);
2374 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2380 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2381 * expressions in "maff".
2383 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2384 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2385 __isl_keep isl_aff *subs)
2389 maff = isl_multi_aff_cow(maff);
2391 return isl_multi_aff_free(maff);
2393 if (type == isl_dim_in)
2396 for (i = 0; i < maff->n; ++i) {
2397 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2399 return isl_multi_aff_free(maff);
2405 /* Plug in "subs" for dimension "type", "pos" of "pma".
2407 * pma is of the form
2411 * while subs is of the form
2413 * v' = B_j(v) -> S_j
2415 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2416 * has a contribution in the result, in particular
2418 * C_ij(S_j) -> M_i(S_j)
2420 * Note that plugging in S_j in C_ij may also result in an empty set
2421 * and this contribution should simply be discarded.
2423 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2424 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2425 __isl_keep isl_pw_aff *subs)
2428 isl_pw_multi_aff *res;
2431 return isl_pw_multi_aff_free(pma);
2433 n = pma->n * subs->n;
2434 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2436 for (i = 0; i < pma->n; ++i) {
2437 for (j = 0; j < subs->n; ++j) {
2439 isl_multi_aff *res_ij;
2440 common = isl_set_intersect(
2441 isl_set_copy(pma->p[i].set),
2442 isl_set_copy(subs->p[j].set));
2443 common = isl_set_substitute(common,
2444 type, pos, subs->p[j].aff);
2445 if (isl_set_plain_is_empty(common)) {
2446 isl_set_free(common);
2450 res_ij = isl_multi_aff_substitute(
2451 isl_multi_aff_copy(pma->p[i].maff),
2452 type, pos, subs->p[j].aff);
2454 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2458 isl_pw_multi_aff_free(pma);
2462 /* Extend the local space of "dst" to include the divs
2463 * in the local space of "src".
2465 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2466 __isl_keep isl_aff *src)
2474 return isl_aff_free(dst);
2476 ctx = isl_aff_get_ctx(src);
2477 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2478 isl_die(ctx, isl_error_invalid,
2479 "spaces don't match", goto error);
2481 if (src->ls->div->n_row == 0)
2484 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2485 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2489 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2490 dst = isl_aff_expand_divs(dst, div, exp2);
2498 return isl_aff_free(dst);
2501 /* Adjust the local spaces of the affine expressions in "maff"
2502 * such that they all have the save divs.
2504 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2505 __isl_take isl_multi_aff *maff)
2513 maff = isl_multi_aff_cow(maff);
2517 for (i = 1; i < maff->n; ++i)
2518 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2519 for (i = 1; i < maff->n; ++i) {
2520 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2522 return isl_multi_aff_free(maff);
2528 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2530 aff = isl_aff_cow(aff);
2534 aff->ls = isl_local_space_lift(aff->ls);
2536 return isl_aff_free(aff);
2541 /* Lift "maff" to a space with extra dimensions such that the result
2542 * has no more existentially quantified variables.
2543 * If "ls" is not NULL, then *ls is assigned the local space that lies
2544 * at the basis of the lifting applied to "maff".
2546 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2547 __isl_give isl_local_space **ls)
2561 isl_space *space = isl_multi_aff_get_domain_space(maff);
2562 *ls = isl_local_space_from_space(space);
2564 return isl_multi_aff_free(maff);
2569 maff = isl_multi_aff_cow(maff);
2570 maff = isl_multi_aff_align_divs(maff);
2574 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2575 space = isl_multi_aff_get_space(maff);
2576 space = isl_space_lift(isl_space_domain(space), n_div);
2577 space = isl_space_extend_domain_with_range(space,
2578 isl_multi_aff_get_space(maff));
2580 return isl_multi_aff_free(maff);
2581 isl_space_free(maff->space);
2582 maff->space = space;
2585 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2587 return isl_multi_aff_free(maff);
2590 for (i = 0; i < maff->n; ++i) {
2591 maff->p[i] = isl_aff_lift(maff->p[i]);
2599 isl_local_space_free(*ls);
2600 return isl_multi_aff_free(maff);
2604 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2606 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2607 __isl_keep isl_pw_multi_aff *pma, int pos)
2617 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2618 if (pos < 0 || pos >= n_out)
2619 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2620 "index out of bounds", return NULL);
2622 space = isl_pw_multi_aff_get_space(pma);
2623 space = isl_space_drop_dims(space, isl_dim_out,
2624 pos + 1, n_out - pos - 1);
2625 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2627 pa = isl_pw_aff_alloc_size(space, pma->n);
2628 for (i = 0; i < pma->n; ++i) {
2630 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2631 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);