2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the GNU LGPLv2.1 license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_mat_private.h>
22 #include <isl_list_private.h>
23 #include <isl/constraint.h>
26 #include <isl_config.h>
28 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
29 __isl_take isl_vec *v)
36 aff = isl_calloc_type(v->ctx, struct isl_aff);
46 isl_local_space_free(ls);
51 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
60 ctx = isl_local_space_get_ctx(ls);
61 if (!isl_local_space_divs_known(ls))
62 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
64 if (!isl_local_space_is_set(ls))
65 isl_die(ctx, isl_error_invalid,
66 "domain of affine expression should be a set",
69 total = isl_local_space_dim(ls, isl_dim_all);
70 v = isl_vec_alloc(ctx, 1 + 1 + total);
71 return isl_aff_alloc_vec(ls, v);
73 isl_local_space_free(ls);
77 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
81 aff = isl_aff_alloc(ls);
85 isl_int_set_si(aff->v->el[0], 1);
86 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
91 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
100 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
105 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
106 isl_vec_copy(aff->v));
109 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
117 return isl_aff_dup(aff);
120 void *isl_aff_free(__isl_take isl_aff *aff)
128 isl_local_space_free(aff->ls);
129 isl_vec_free(aff->v);
136 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
138 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
141 /* Externally, an isl_aff has a map space, but internally, the
142 * ls field corresponds to the domain of that space.
144 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
148 if (type == isl_dim_out)
150 if (type == isl_dim_in)
152 return isl_local_space_dim(aff->ls, type);
155 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
157 return aff ? isl_local_space_get_space(aff->ls) : NULL;
160 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
165 space = isl_local_space_get_space(aff->ls);
166 space = isl_space_from_domain(space);
167 space = isl_space_add_dims(space, isl_dim_out, 1);
171 __isl_give isl_local_space *isl_aff_get_domain_local_space(
172 __isl_keep isl_aff *aff)
174 return aff ? isl_local_space_copy(aff->ls) : NULL;
177 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
182 ls = isl_local_space_copy(aff->ls);
183 ls = isl_local_space_from_domain(ls);
184 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
188 /* Externally, an isl_aff has a map space, but internally, the
189 * ls field corresponds to the domain of that space.
191 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
192 enum isl_dim_type type, unsigned pos)
196 if (type == isl_dim_out)
198 if (type == isl_dim_in)
200 return isl_local_space_get_dim_name(aff->ls, type, pos);
203 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
204 __isl_take isl_space *dim)
206 aff = isl_aff_cow(aff);
210 aff->ls = isl_local_space_reset_space(aff->ls, dim);
212 return isl_aff_free(aff);
221 /* Reset the space of "aff". This function is called from isl_pw_templ.c
222 * and doesn't know if the space of an element object is represented
223 * directly or through its domain. It therefore passes along both.
225 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
226 __isl_take isl_space *space, __isl_take isl_space *domain)
228 isl_space_free(space);
229 return isl_aff_reset_domain_space(aff, domain);
232 /* Reorder the coefficients of the affine expression based
233 * on the given reodering.
234 * The reordering r is assumed to have been extended with the local
237 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
238 __isl_take isl_reordering *r, int n_div)
246 res = isl_vec_alloc(vec->ctx,
247 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
248 isl_seq_cpy(res->el, vec->el, 2);
249 isl_seq_clr(res->el + 2, res->size - 2);
250 for (i = 0; i < r->len; ++i)
251 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
253 isl_reordering_free(r);
258 isl_reordering_free(r);
262 /* Reorder the dimensions of the domain of "aff" according
263 * to the given reordering.
265 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
266 __isl_take isl_reordering *r)
268 aff = isl_aff_cow(aff);
272 r = isl_reordering_extend(r, aff->ls->div->n_row);
273 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
274 aff->ls->div->n_row);
275 aff->ls = isl_local_space_realign(aff->ls, r);
277 if (!aff->v || !aff->ls)
278 return isl_aff_free(aff);
283 isl_reordering_free(r);
287 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
288 __isl_take isl_space *model)
293 if (!isl_space_match(aff->ls->dim, isl_dim_param,
294 model, isl_dim_param)) {
297 model = isl_space_drop_dims(model, isl_dim_in,
298 0, isl_space_dim(model, isl_dim_in));
299 model = isl_space_drop_dims(model, isl_dim_out,
300 0, isl_space_dim(model, isl_dim_out));
301 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
302 exp = isl_reordering_extend_space(exp,
303 isl_aff_get_domain_space(aff));
304 aff = isl_aff_realign_domain(aff, exp);
307 isl_space_free(model);
310 isl_space_free(model);
315 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
320 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
323 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
330 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
331 if (equal < 0 || !equal)
334 return isl_vec_is_equal(aff1->v, aff2->v);
337 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
341 isl_int_set(*v, aff->v->el[0]);
345 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
349 isl_int_set(*v, aff->v->el[1]);
353 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
354 enum isl_dim_type type, int pos, isl_int *v)
359 if (type == isl_dim_out)
360 isl_die(aff->v->ctx, isl_error_invalid,
361 "output/set dimension does not have a coefficient",
363 if (type == isl_dim_in)
366 if (pos >= isl_local_space_dim(aff->ls, type))
367 isl_die(aff->v->ctx, isl_error_invalid,
368 "position out of bounds", return -1);
370 pos += isl_local_space_offset(aff->ls, type);
371 isl_int_set(*v, aff->v->el[1 + pos]);
376 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
378 aff = isl_aff_cow(aff);
382 aff->v = isl_vec_cow(aff->v);
384 return isl_aff_free(aff);
386 isl_int_set(aff->v->el[0], v);
391 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
393 aff = isl_aff_cow(aff);
397 aff->v = isl_vec_cow(aff->v);
399 return isl_aff_free(aff);
401 isl_int_set(aff->v->el[1], v);
406 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
408 if (isl_int_is_zero(v))
411 aff = isl_aff_cow(aff);
415 aff->v = isl_vec_cow(aff->v);
417 return isl_aff_free(aff);
419 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
424 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
429 isl_int_set_si(t, v);
430 aff = isl_aff_add_constant(aff, t);
436 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
438 aff = isl_aff_cow(aff);
442 aff->v = isl_vec_cow(aff->v);
444 return isl_aff_free(aff);
446 isl_int_set_si(aff->v->el[1], v);
451 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
452 enum isl_dim_type type, int pos, isl_int v)
457 if (type == isl_dim_out)
458 isl_die(aff->v->ctx, isl_error_invalid,
459 "output/set dimension does not have a coefficient",
460 return isl_aff_free(aff));
461 if (type == isl_dim_in)
464 if (pos >= isl_local_space_dim(aff->ls, type))
465 isl_die(aff->v->ctx, isl_error_invalid,
466 "position out of bounds", return isl_aff_free(aff));
468 aff = isl_aff_cow(aff);
472 aff->v = isl_vec_cow(aff->v);
474 return isl_aff_free(aff);
476 pos += isl_local_space_offset(aff->ls, type);
477 isl_int_set(aff->v->el[1 + pos], v);
482 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
483 enum isl_dim_type type, int pos, int v)
488 if (type == isl_dim_out)
489 isl_die(aff->v->ctx, isl_error_invalid,
490 "output/set dimension does not have a coefficient",
491 return isl_aff_free(aff));
492 if (type == isl_dim_in)
495 if (pos >= isl_local_space_dim(aff->ls, type))
496 isl_die(aff->v->ctx, isl_error_invalid,
497 "position out of bounds", return isl_aff_free(aff));
499 aff = isl_aff_cow(aff);
503 aff->v = isl_vec_cow(aff->v);
505 return isl_aff_free(aff);
507 pos += isl_local_space_offset(aff->ls, type);
508 isl_int_set_si(aff->v->el[1 + pos], v);
513 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
514 enum isl_dim_type type, int pos, isl_int v)
519 if (type == isl_dim_out)
520 isl_die(aff->v->ctx, isl_error_invalid,
521 "output/set dimension does not have a coefficient",
522 return isl_aff_free(aff));
523 if (type == isl_dim_in)
526 if (pos >= isl_local_space_dim(aff->ls, type))
527 isl_die(aff->v->ctx, isl_error_invalid,
528 "position out of bounds", return isl_aff_free(aff));
530 aff = isl_aff_cow(aff);
534 aff->v = isl_vec_cow(aff->v);
536 return isl_aff_free(aff);
538 pos += isl_local_space_offset(aff->ls, type);
539 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
544 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
545 enum isl_dim_type type, int pos, int v)
550 isl_int_set_si(t, v);
551 aff = isl_aff_add_coefficient(aff, type, pos, t);
557 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
562 return isl_local_space_get_div(aff->ls, pos);
565 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
567 aff = isl_aff_cow(aff);
570 aff->v = isl_vec_cow(aff->v);
572 return isl_aff_free(aff);
574 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
579 /* Remove divs from the local space that do not appear in the affine
581 * We currently only remove divs at the end.
582 * Some intermediate divs may also not appear directly in the affine
583 * expression, but we would also need to check that no other divs are
584 * defined in terms of them.
586 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
595 n = isl_local_space_dim(aff->ls, isl_dim_div);
596 off = isl_local_space_offset(aff->ls, isl_dim_div);
598 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
602 aff = isl_aff_cow(aff);
606 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
607 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
608 if (!aff->ls || !aff->v)
609 return isl_aff_free(aff);
614 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
618 aff->v = isl_vec_normalize(aff->v);
620 return isl_aff_free(aff);
621 aff = isl_aff_remove_unused_divs(aff);
625 /* Given f, return floor(f).
626 * If f is an integer expression, then just return f.
627 * If f is a constant, then return the constant floor(f).
628 * Otherwise, if f = g/m, write g = q m + r,
629 * create a new div d = [r/m] and return the expression q + d.
630 * The coefficients in r are taken to lie between -m/2 and m/2.
632 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
642 if (isl_int_is_one(aff->v->el[0]))
645 aff = isl_aff_cow(aff);
649 aff->v = isl_vec_cow(aff->v);
651 return isl_aff_free(aff);
653 if (isl_aff_is_cst(aff)) {
654 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
655 isl_int_set_si(aff->v->el[0], 1);
659 div = isl_vec_copy(aff->v);
660 div = isl_vec_cow(div);
662 return isl_aff_free(aff);
664 ctx = isl_aff_get_ctx(aff);
665 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
666 for (i = 1; i < aff->v->size; ++i) {
667 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
668 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
669 if (isl_int_gt(div->el[i], aff->v->el[0])) {
670 isl_int_sub(div->el[i], div->el[i], div->el[0]);
671 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
675 aff->ls = isl_local_space_add_div(aff->ls, div);
677 return isl_aff_free(aff);
680 aff->v = isl_vec_extend(aff->v, size + 1);
682 return isl_aff_free(aff);
683 isl_int_set_si(aff->v->el[0], 1);
684 isl_int_set_si(aff->v->el[size], 1);
691 * aff mod m = aff - m * floor(aff/m)
693 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
697 res = isl_aff_copy(aff);
698 aff = isl_aff_scale_down(aff, m);
699 aff = isl_aff_floor(aff);
700 aff = isl_aff_scale(aff, m);
701 res = isl_aff_sub(res, aff);
708 * pwaff mod m = pwaff - m * floor(pwaff/m)
710 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
714 res = isl_pw_aff_copy(pwaff);
715 pwaff = isl_pw_aff_scale_down(pwaff, m);
716 pwaff = isl_pw_aff_floor(pwaff);
717 pwaff = isl_pw_aff_scale(pwaff, m);
718 res = isl_pw_aff_sub(res, pwaff);
723 /* Given f, return ceil(f).
724 * If f is an integer expression, then just return f.
725 * Otherwise, create a new div d = [-f] and return the expression -d.
727 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
732 if (isl_int_is_one(aff->v->el[0]))
735 aff = isl_aff_neg(aff);
736 aff = isl_aff_floor(aff);
737 aff = isl_aff_neg(aff);
742 /* Apply the expansion computed by isl_merge_divs.
743 * The expansion itself is given by "exp" while the resulting
744 * list of divs is given by "div".
746 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
747 __isl_take isl_mat *div, int *exp)
754 aff = isl_aff_cow(aff);
758 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
759 new_n_div = isl_mat_rows(div);
760 if (new_n_div < old_n_div)
761 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
762 "not an expansion", goto error);
764 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
768 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
770 for (i = new_n_div - 1; i >= 0; --i) {
771 if (j >= 0 && exp[j] == i) {
773 isl_int_swap(aff->v->el[offset + i],
774 aff->v->el[offset + j]);
777 isl_int_set_si(aff->v->el[offset + i], 0);
780 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
791 /* Add two affine expressions that live in the same local space.
793 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
794 __isl_take isl_aff *aff2)
798 aff1 = isl_aff_cow(aff1);
802 aff1->v = isl_vec_cow(aff1->v);
808 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
809 isl_int_divexact(f, aff2->v->el[0], gcd);
810 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
811 isl_int_divexact(f, aff1->v->el[0], gcd);
812 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
813 isl_int_divexact(f, aff2->v->el[0], gcd);
814 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
826 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
827 __isl_take isl_aff *aff2)
837 ctx = isl_aff_get_ctx(aff1);
838 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
839 isl_die(ctx, isl_error_invalid,
840 "spaces don't match", goto error);
842 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
843 return add_expanded(aff1, aff2);
845 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
846 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
850 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
851 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
852 aff2 = isl_aff_expand_divs(aff2, div, exp2);
856 return add_expanded(aff1, aff2);
865 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
866 __isl_take isl_aff *aff2)
868 return isl_aff_add(aff1, isl_aff_neg(aff2));
871 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
875 if (isl_int_is_one(f))
878 aff = isl_aff_cow(aff);
881 aff->v = isl_vec_cow(aff->v);
883 return isl_aff_free(aff);
886 isl_int_gcd(gcd, aff->v->el[0], f);
887 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
888 isl_int_divexact(gcd, f, gcd);
889 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
895 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
899 if (isl_int_is_one(f))
902 aff = isl_aff_cow(aff);
906 if (isl_int_is_zero(f))
907 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
908 "cannot scale down by zero", return isl_aff_free(aff));
910 aff->v = isl_vec_cow(aff->v);
912 return isl_aff_free(aff);
915 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
916 isl_int_gcd(gcd, gcd, f);
917 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
918 isl_int_divexact(gcd, f, gcd);
919 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
925 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
933 isl_int_set_ui(v, f);
934 aff = isl_aff_scale_down(aff, v);
940 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
941 enum isl_dim_type type, unsigned pos, const char *s)
943 aff = isl_aff_cow(aff);
946 if (type == isl_dim_out)
947 isl_die(aff->v->ctx, isl_error_invalid,
948 "cannot set name of output/set dimension",
949 return isl_aff_free(aff));
950 if (type == isl_dim_in)
952 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
954 return isl_aff_free(aff);
959 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
960 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
962 aff = isl_aff_cow(aff);
964 return isl_id_free(id);
965 if (type == isl_dim_out)
966 isl_die(aff->v->ctx, isl_error_invalid,
967 "cannot set name of output/set dimension",
969 if (type == isl_dim_in)
971 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
973 return isl_aff_free(aff);
982 /* Exploit the equalities in "eq" to simplify the affine expression
983 * and the expressions of the integer divisions in the local space.
984 * The integer divisions in this local space are assumed to appear
985 * as regular dimensions in "eq".
987 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
988 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
997 isl_basic_set_free(eq);
1001 aff = isl_aff_cow(aff);
1005 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1006 isl_basic_set_copy(eq));
1010 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1012 for (i = 0; i < eq->n_eq; ++i) {
1013 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1014 if (j < 0 || j == 0 || j >= total)
1017 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1021 isl_basic_set_free(eq);
1022 aff = isl_aff_normalize(aff);
1025 isl_basic_set_free(eq);
1030 /* Exploit the equalities in "eq" to simplify the affine expression
1031 * and the expressions of the integer divisions in the local space.
1033 static __isl_give isl_aff *isl_aff_substitute_equalities(
1034 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1040 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1042 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
1043 return isl_aff_substitute_equalities_lifted(aff, eq);
1045 isl_basic_set_free(eq);
1050 /* Look for equalities among the variables shared by context and aff
1051 * and the integer divisions of aff, if any.
1052 * The equalities are then used to eliminate coefficients and/or integer
1053 * divisions from aff.
1055 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1056 __isl_take isl_set *context)
1058 isl_basic_set *hull;
1063 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1065 isl_basic_set *bset;
1066 isl_local_space *ls;
1067 context = isl_set_add_dims(context, isl_dim_set, n_div);
1068 ls = isl_aff_get_domain_local_space(aff);
1069 bset = isl_basic_set_from_local_space(ls);
1070 bset = isl_basic_set_lift(bset);
1071 bset = isl_basic_set_flatten(bset);
1072 context = isl_set_intersect(context,
1073 isl_set_from_basic_set(bset));
1076 hull = isl_set_affine_hull(context);
1077 return isl_aff_substitute_equalities_lifted(aff, hull);
1080 isl_set_free(context);
1084 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1085 __isl_take isl_set *context)
1087 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1088 dom_context = isl_set_intersect_params(dom_context, context);
1089 return isl_aff_gist(aff, dom_context);
1092 /* Return a basic set containing those elements in the space
1093 * of aff where it is non-negative.
1095 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1097 isl_constraint *ineq;
1098 isl_basic_set *bset;
1100 ineq = isl_inequality_from_aff(aff);
1102 bset = isl_basic_set_from_constraint(ineq);
1103 bset = isl_basic_set_simplify(bset);
1107 /* Return a basic set containing those elements in the space
1108 * of aff where it is zero.
1110 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1112 isl_constraint *ineq;
1113 isl_basic_set *bset;
1115 ineq = isl_equality_from_aff(aff);
1117 bset = isl_basic_set_from_constraint(ineq);
1118 bset = isl_basic_set_simplify(bset);
1122 /* Return a basic set containing those elements in the shared space
1123 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1125 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1126 __isl_take isl_aff *aff2)
1128 aff1 = isl_aff_sub(aff1, aff2);
1130 return isl_aff_nonneg_basic_set(aff1);
1133 /* Return a basic set containing those elements in the shared space
1134 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1136 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1137 __isl_take isl_aff *aff2)
1139 return isl_aff_ge_basic_set(aff2, aff1);
1142 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1143 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1145 aff1 = isl_aff_add(aff1, aff2);
1146 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1150 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1158 /* Check whether the given affine expression has non-zero coefficient
1159 * for any dimension in the given range or if any of these dimensions
1160 * appear with non-zero coefficients in any of the integer divisions
1161 * involved in the affine expression.
1163 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1164 enum isl_dim_type type, unsigned first, unsigned n)
1176 ctx = isl_aff_get_ctx(aff);
1177 if (first + n > isl_aff_dim(aff, type))
1178 isl_die(ctx, isl_error_invalid,
1179 "range out of bounds", return -1);
1181 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1185 first += isl_local_space_offset(aff->ls, type) - 1;
1186 for (i = 0; i < n; ++i)
1187 if (active[first + i]) {
1200 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1201 enum isl_dim_type type, unsigned first, unsigned n)
1207 if (type == isl_dim_out)
1208 isl_die(aff->v->ctx, isl_error_invalid,
1209 "cannot drop output/set dimension",
1210 return isl_aff_free(aff));
1211 if (type == isl_dim_in)
1213 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1216 ctx = isl_aff_get_ctx(aff);
1217 if (first + n > isl_local_space_dim(aff->ls, type))
1218 isl_die(ctx, isl_error_invalid, "range out of bounds",
1219 return isl_aff_free(aff));
1221 aff = isl_aff_cow(aff);
1225 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1227 return isl_aff_free(aff);
1229 first += 1 + isl_local_space_offset(aff->ls, type);
1230 aff->v = isl_vec_drop_els(aff->v, first, n);
1232 return isl_aff_free(aff);
1237 /* Project the domain of the affine expression onto its parameter space.
1238 * The affine expression may not involve any of the domain dimensions.
1240 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1246 n = isl_aff_dim(aff, isl_dim_in);
1247 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1249 return isl_aff_free(aff);
1251 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1252 "affine expression involves some of the domain dimensions",
1253 return isl_aff_free(aff));
1254 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1255 space = isl_aff_get_domain_space(aff);
1256 space = isl_space_params(space);
1257 aff = isl_aff_reset_domain_space(aff, space);
1261 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1262 enum isl_dim_type type, unsigned first, unsigned n)
1268 if (type == isl_dim_out)
1269 isl_die(aff->v->ctx, isl_error_invalid,
1270 "cannot insert output/set dimensions",
1271 return isl_aff_free(aff));
1272 if (type == isl_dim_in)
1274 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1277 ctx = isl_aff_get_ctx(aff);
1278 if (first > isl_local_space_dim(aff->ls, type))
1279 isl_die(ctx, isl_error_invalid, "position out of bounds",
1280 return isl_aff_free(aff));
1282 aff = isl_aff_cow(aff);
1286 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1288 return isl_aff_free(aff);
1290 first += 1 + isl_local_space_offset(aff->ls, type);
1291 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1293 return isl_aff_free(aff);
1298 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1299 enum isl_dim_type type, unsigned n)
1303 pos = isl_aff_dim(aff, type);
1305 return isl_aff_insert_dims(aff, type, pos, n);
1308 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1309 enum isl_dim_type type, unsigned n)
1313 pos = isl_pw_aff_dim(pwaff, type);
1315 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1318 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1320 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1321 return isl_pw_aff_alloc(dom, aff);
1325 #define PW isl_pw_aff
1329 #define EL_IS_ZERO is_empty
1333 #define IS_ZERO is_empty
1336 #undef DEFAULT_IS_ZERO
1337 #define DEFAULT_IS_ZERO 0
1341 #define NO_MOVE_DIMS
1345 #include <isl_pw_templ.c>
1347 static __isl_give isl_set *align_params_pw_pw_set_and(
1348 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1349 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1350 __isl_take isl_pw_aff *pwaff2))
1352 if (!pwaff1 || !pwaff2)
1354 if (isl_space_match(pwaff1->dim, isl_dim_param,
1355 pwaff2->dim, isl_dim_param))
1356 return fn(pwaff1, pwaff2);
1357 if (!isl_space_has_named_params(pwaff1->dim) ||
1358 !isl_space_has_named_params(pwaff2->dim))
1359 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1360 "unaligned unnamed parameters", goto error);
1361 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1362 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1363 return fn(pwaff1, pwaff2);
1365 isl_pw_aff_free(pwaff1);
1366 isl_pw_aff_free(pwaff2);
1370 /* Compute a piecewise quasi-affine expression with a domain that
1371 * is the union of those of pwaff1 and pwaff2 and such that on each
1372 * cell, the quasi-affine expression is the better (according to cmp)
1373 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1374 * is defined on a given cell, then the associated expression
1375 * is the defined one.
1377 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1378 __isl_take isl_pw_aff *pwaff2,
1379 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1380 __isl_take isl_aff *aff2))
1387 if (!pwaff1 || !pwaff2)
1390 ctx = isl_space_get_ctx(pwaff1->dim);
1391 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1392 isl_die(ctx, isl_error_invalid,
1393 "arguments should live in same space", goto error);
1395 if (isl_pw_aff_is_empty(pwaff1)) {
1396 isl_pw_aff_free(pwaff1);
1400 if (isl_pw_aff_is_empty(pwaff2)) {
1401 isl_pw_aff_free(pwaff2);
1405 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1406 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1408 for (i = 0; i < pwaff1->n; ++i) {
1409 set = isl_set_copy(pwaff1->p[i].set);
1410 for (j = 0; j < pwaff2->n; ++j) {
1411 struct isl_set *common;
1414 common = isl_set_intersect(
1415 isl_set_copy(pwaff1->p[i].set),
1416 isl_set_copy(pwaff2->p[j].set));
1417 better = isl_set_from_basic_set(cmp(
1418 isl_aff_copy(pwaff2->p[j].aff),
1419 isl_aff_copy(pwaff1->p[i].aff)));
1420 better = isl_set_intersect(common, better);
1421 if (isl_set_plain_is_empty(better)) {
1422 isl_set_free(better);
1425 set = isl_set_subtract(set, isl_set_copy(better));
1427 res = isl_pw_aff_add_piece(res, better,
1428 isl_aff_copy(pwaff2->p[j].aff));
1430 res = isl_pw_aff_add_piece(res, set,
1431 isl_aff_copy(pwaff1->p[i].aff));
1434 for (j = 0; j < pwaff2->n; ++j) {
1435 set = isl_set_copy(pwaff2->p[j].set);
1436 for (i = 0; i < pwaff1->n; ++i)
1437 set = isl_set_subtract(set,
1438 isl_set_copy(pwaff1->p[i].set));
1439 res = isl_pw_aff_add_piece(res, set,
1440 isl_aff_copy(pwaff2->p[j].aff));
1443 isl_pw_aff_free(pwaff1);
1444 isl_pw_aff_free(pwaff2);
1448 isl_pw_aff_free(pwaff1);
1449 isl_pw_aff_free(pwaff2);
1453 /* Compute a piecewise quasi-affine expression with a domain that
1454 * is the union of those of pwaff1 and pwaff2 and such that on each
1455 * cell, the quasi-affine expression is the maximum of those of pwaff1
1456 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1457 * cell, then the associated expression is the defined one.
1459 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1460 __isl_take isl_pw_aff *pwaff2)
1462 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1465 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1466 __isl_take isl_pw_aff *pwaff2)
1468 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1472 /* Compute a piecewise quasi-affine expression with a domain that
1473 * is the union of those of pwaff1 and pwaff2 and such that on each
1474 * cell, the quasi-affine expression is the minimum of those of pwaff1
1475 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1476 * cell, then the associated expression is the defined one.
1478 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1479 __isl_take isl_pw_aff *pwaff2)
1481 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1484 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1485 __isl_take isl_pw_aff *pwaff2)
1487 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1491 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1492 __isl_take isl_pw_aff *pwaff2, int max)
1495 return isl_pw_aff_union_max(pwaff1, pwaff2);
1497 return isl_pw_aff_union_min(pwaff1, pwaff2);
1500 /* Construct a map with as domain the domain of pwaff and
1501 * one-dimensional range corresponding to the affine expressions.
1503 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1512 dim = isl_pw_aff_get_space(pwaff);
1513 map = isl_map_empty(dim);
1515 for (i = 0; i < pwaff->n; ++i) {
1516 isl_basic_map *bmap;
1519 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1520 map_i = isl_map_from_basic_map(bmap);
1521 map_i = isl_map_intersect_domain(map_i,
1522 isl_set_copy(pwaff->p[i].set));
1523 map = isl_map_union_disjoint(map, map_i);
1526 isl_pw_aff_free(pwaff);
1531 /* Construct a map with as domain the domain of pwaff and
1532 * one-dimensional range corresponding to the affine expressions.
1534 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1538 if (isl_space_is_set(pwaff->dim))
1539 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1540 "space of input is not a map",
1541 return isl_pw_aff_free(pwaff));
1542 return map_from_pw_aff(pwaff);
1545 /* Construct a one-dimensional set with as parameter domain
1546 * the domain of pwaff and the single set dimension
1547 * corresponding to the affine expressions.
1549 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1553 if (!isl_space_is_set(pwaff->dim))
1554 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1555 "space of input is not a set",
1556 return isl_pw_aff_free(pwaff));
1557 return map_from_pw_aff(pwaff);
1560 /* Return a set containing those elements in the domain
1561 * of pwaff where it is non-negative.
1563 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1571 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1573 for (i = 0; i < pwaff->n; ++i) {
1574 isl_basic_set *bset;
1577 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1578 set_i = isl_set_from_basic_set(bset);
1579 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1580 set = isl_set_union_disjoint(set, set_i);
1583 isl_pw_aff_free(pwaff);
1588 /* Return a set containing those elements in the domain
1589 * of pwaff where it is zero (if complement is 0) or not zero
1590 * (if complement is 1).
1592 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1601 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1603 for (i = 0; i < pwaff->n; ++i) {
1604 isl_basic_set *bset;
1605 isl_set *set_i, *zero;
1607 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1608 zero = isl_set_from_basic_set(bset);
1609 set_i = isl_set_copy(pwaff->p[i].set);
1611 set_i = isl_set_subtract(set_i, zero);
1613 set_i = isl_set_intersect(set_i, zero);
1614 set = isl_set_union_disjoint(set, set_i);
1617 isl_pw_aff_free(pwaff);
1622 /* Return a set containing those elements in the domain
1623 * of pwaff where it is zero.
1625 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1627 return pw_aff_zero_set(pwaff, 0);
1630 /* Return a set containing those elements in the domain
1631 * of pwaff where it is not zero.
1633 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1635 return pw_aff_zero_set(pwaff, 1);
1638 /* Return a set containing those elements in the shared domain
1639 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1641 * We compute the difference on the shared domain and then construct
1642 * the set of values where this difference is non-negative.
1643 * If strict is set, we first subtract 1 from the difference.
1644 * If equal is set, we only return the elements where pwaff1 and pwaff2
1647 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1648 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1650 isl_set *set1, *set2;
1652 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1653 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1654 set1 = isl_set_intersect(set1, set2);
1655 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1656 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1657 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1660 isl_space *dim = isl_set_get_space(set1);
1662 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1663 aff = isl_aff_add_constant_si(aff, -1);
1664 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1669 return isl_pw_aff_zero_set(pwaff1);
1670 return isl_pw_aff_nonneg_set(pwaff1);
1673 /* Return a set containing those elements in the shared domain
1674 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1676 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1677 __isl_take isl_pw_aff *pwaff2)
1679 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1682 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1683 __isl_take isl_pw_aff *pwaff2)
1685 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1688 /* Return a set containing those elements in the shared domain
1689 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1691 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1692 __isl_take isl_pw_aff *pwaff2)
1694 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1697 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1698 __isl_take isl_pw_aff *pwaff2)
1700 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1703 /* Return a set containing those elements in the shared domain
1704 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1706 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1707 __isl_take isl_pw_aff *pwaff2)
1709 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1712 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1713 __isl_take isl_pw_aff *pwaff2)
1715 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1718 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1719 __isl_take isl_pw_aff *pwaff2)
1721 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1724 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1725 __isl_take isl_pw_aff *pwaff2)
1727 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1730 /* Return a set containing those elements in the shared domain
1731 * of the elements of list1 and list2 where each element in list1
1732 * has the relation specified by "fn" with each element in list2.
1734 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1735 __isl_take isl_pw_aff_list *list2,
1736 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1737 __isl_take isl_pw_aff *pwaff2))
1743 if (!list1 || !list2)
1746 ctx = isl_pw_aff_list_get_ctx(list1);
1747 if (list1->n < 1 || list2->n < 1)
1748 isl_die(ctx, isl_error_invalid,
1749 "list should contain at least one element", goto error);
1751 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1752 for (i = 0; i < list1->n; ++i)
1753 for (j = 0; j < list2->n; ++j) {
1756 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1757 isl_pw_aff_copy(list2->p[j]));
1758 set = isl_set_intersect(set, set_ij);
1761 isl_pw_aff_list_free(list1);
1762 isl_pw_aff_list_free(list2);
1765 isl_pw_aff_list_free(list1);
1766 isl_pw_aff_list_free(list2);
1770 /* Return a set containing those elements in the shared domain
1771 * of the elements of list1 and list2 where each element in list1
1772 * is equal to each element in list2.
1774 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1775 __isl_take isl_pw_aff_list *list2)
1777 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1780 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1781 __isl_take isl_pw_aff_list *list2)
1783 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1786 /* Return a set containing those elements in the shared domain
1787 * of the elements of list1 and list2 where each element in list1
1788 * is less than or equal to each element in list2.
1790 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1791 __isl_take isl_pw_aff_list *list2)
1793 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1796 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1797 __isl_take isl_pw_aff_list *list2)
1799 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1802 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1803 __isl_take isl_pw_aff_list *list2)
1805 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1808 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1809 __isl_take isl_pw_aff_list *list2)
1811 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1815 /* Return a set containing those elements in the shared domain
1816 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1818 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1819 __isl_take isl_pw_aff *pwaff2)
1821 isl_set *set_lt, *set_gt;
1823 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1824 isl_pw_aff_copy(pwaff2));
1825 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1826 return isl_set_union_disjoint(set_lt, set_gt);
1829 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1830 __isl_take isl_pw_aff *pwaff2)
1832 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1835 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1840 if (isl_int_is_one(v))
1842 if (!isl_int_is_pos(v))
1843 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1844 "factor needs to be positive",
1845 return isl_pw_aff_free(pwaff));
1846 pwaff = isl_pw_aff_cow(pwaff);
1852 for (i = 0; i < pwaff->n; ++i) {
1853 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1854 if (!pwaff->p[i].aff)
1855 return isl_pw_aff_free(pwaff);
1861 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1865 pwaff = isl_pw_aff_cow(pwaff);
1871 for (i = 0; i < pwaff->n; ++i) {
1872 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1873 if (!pwaff->p[i].aff)
1874 return isl_pw_aff_free(pwaff);
1880 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1884 pwaff = isl_pw_aff_cow(pwaff);
1890 for (i = 0; i < pwaff->n; ++i) {
1891 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1892 if (!pwaff->p[i].aff)
1893 return isl_pw_aff_free(pwaff);
1899 /* Assuming that "cond1" and "cond2" are disjoint,
1900 * return an affine expression that is equal to pwaff1 on cond1
1901 * and to pwaff2 on cond2.
1903 static __isl_give isl_pw_aff *isl_pw_aff_select(
1904 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
1905 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
1907 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
1908 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
1910 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
1913 /* Return an affine expression that is equal to pwaff_true for elements
1914 * where "cond" is non-zero and to pwaff_false for elements where "cond"
1916 * That is, return cond ? pwaff_true : pwaff_false;
1918 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
1919 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1921 isl_set *cond_true, *cond_false;
1923 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1924 cond_false = isl_pw_aff_zero_set(cond);
1925 return isl_pw_aff_select(cond_true, pwaff_true,
1926 cond_false, pwaff_false);
1929 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1934 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1937 /* Check whether pwaff is a piecewise constant.
1939 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1946 for (i = 0; i < pwaff->n; ++i) {
1947 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1948 if (is_cst < 0 || !is_cst)
1955 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1956 __isl_take isl_aff *aff2)
1958 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1959 return isl_aff_mul(aff2, aff1);
1961 if (!isl_aff_is_cst(aff2))
1962 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1963 "at least one affine expression should be constant",
1966 aff1 = isl_aff_cow(aff1);
1970 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1971 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1981 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1982 __isl_take isl_pw_aff *pwaff2)
1984 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1987 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1988 __isl_take isl_pw_aff *pwaff2)
1990 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1993 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1994 __isl_take isl_pw_aff *pwaff2)
1996 return isl_pw_aff_union_add_(pwaff1, pwaff2);
1999 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2000 __isl_take isl_pw_aff *pwaff2)
2002 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2005 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2006 __isl_take isl_pw_aff *pwaff2)
2008 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2011 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2012 __isl_take isl_pw_aff *pwaff2)
2017 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2018 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2019 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2020 isl_pw_aff_copy(pwaff2));
2021 dom = isl_set_subtract(dom, isl_set_copy(le));
2022 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2025 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2026 __isl_take isl_pw_aff *pwaff2)
2028 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2031 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2032 __isl_take isl_pw_aff *pwaff2)
2037 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2038 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2039 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2040 isl_pw_aff_copy(pwaff2));
2041 dom = isl_set_subtract(dom, isl_set_copy(ge));
2042 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2045 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2046 __isl_take isl_pw_aff *pwaff2)
2048 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2051 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2052 __isl_take isl_pw_aff_list *list,
2053 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2054 __isl_take isl_pw_aff *pwaff2))
2063 ctx = isl_pw_aff_list_get_ctx(list);
2065 isl_die(ctx, isl_error_invalid,
2066 "list should contain at least one element",
2067 return isl_pw_aff_list_free(list));
2069 res = isl_pw_aff_copy(list->p[0]);
2070 for (i = 1; i < list->n; ++i)
2071 res = fn(res, isl_pw_aff_copy(list->p[i]));
2073 isl_pw_aff_list_free(list);
2077 /* Return an isl_pw_aff that maps each element in the intersection of the
2078 * domains of the elements of list to the minimal corresponding affine
2081 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2083 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2086 /* Return an isl_pw_aff that maps each element in the intersection of the
2087 * domains of the elements of list to the maximal corresponding affine
2090 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2092 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2098 #include <isl_multi_templ.c>
2100 /* Construct an isl_multi_aff in the given space with value zero in
2101 * each of the output dimensions.
2103 __isl_give isl_multi_aff *isl_multi_aff_zero(__isl_take isl_space *space)
2111 n = isl_space_dim(space , isl_dim_out);
2112 ma = isl_multi_aff_alloc(isl_space_copy(space));
2115 isl_space_free(space);
2118 isl_local_space *ls;
2121 space = isl_space_domain(space);
2122 ls = isl_local_space_from_space(space);
2123 aff = isl_aff_zero_on_domain(ls);
2125 for (i = 0; i < n; ++i)
2126 ma = isl_multi_aff_set_aff(ma, i, isl_aff_copy(aff));
2134 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2137 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2138 __isl_take isl_multi_aff *ma)
2140 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2141 return isl_pw_multi_aff_alloc(dom, ma);
2144 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2145 __isl_take isl_multi_aff *maff2)
2150 maff1 = isl_multi_aff_cow(maff1);
2151 if (!maff1 || !maff2)
2154 ctx = isl_multi_aff_get_ctx(maff1);
2155 if (!isl_space_is_equal(maff1->space, maff2->space))
2156 isl_die(ctx, isl_error_invalid,
2157 "spaces don't match", goto error);
2159 for (i = 0; i < maff1->n; ++i) {
2160 maff1->p[i] = isl_aff_add(maff1->p[i],
2161 isl_aff_copy(maff2->p[i]));
2166 isl_multi_aff_free(maff2);
2169 isl_multi_aff_free(maff1);
2170 isl_multi_aff_free(maff2);
2174 /* Exploit the equalities in "eq" to simplify the affine expressions.
2176 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2177 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2181 maff = isl_multi_aff_cow(maff);
2185 for (i = 0; i < maff->n; ++i) {
2186 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2187 isl_basic_set_copy(eq));
2192 isl_basic_set_free(eq);
2195 isl_basic_set_free(eq);
2196 isl_multi_aff_free(maff);
2200 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2205 maff = isl_multi_aff_cow(maff);
2209 for (i = 0; i < maff->n; ++i) {
2210 maff->p[i] = isl_aff_scale(maff->p[i], f);
2212 return isl_multi_aff_free(maff);
2218 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2219 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2221 maff1 = isl_multi_aff_add(maff1, maff2);
2222 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2226 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2234 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2235 __isl_keep isl_multi_aff *maff2)
2240 if (!maff1 || !maff2)
2242 if (maff1->n != maff2->n)
2244 equal = isl_space_is_equal(maff1->space, maff2->space);
2245 if (equal < 0 || !equal)
2248 for (i = 0; i < maff1->n; ++i) {
2249 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2250 if (equal < 0 || !equal)
2257 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2258 __isl_take isl_multi_aff *maff,
2259 enum isl_dim_type type, unsigned pos, const char *s)
2263 maff = isl_multi_aff_cow(maff);
2267 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2269 return isl_multi_aff_free(maff);
2271 if (type == isl_dim_out)
2273 for (i = 0; i < maff->n; ++i) {
2274 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2276 return isl_multi_aff_free(maff);
2282 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2283 enum isl_dim_type type, unsigned first, unsigned n)
2287 maff = isl_multi_aff_cow(maff);
2291 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2293 return isl_multi_aff_free(maff);
2295 if (type == isl_dim_out) {
2296 for (i = 0; i < n; ++i)
2297 isl_aff_free(maff->p[first + i]);
2298 for (i = first; i + n < maff->n; ++i)
2299 maff->p[i] = maff->p[i + n];
2304 for (i = 0; i < maff->n; ++i) {
2305 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2307 return isl_multi_aff_free(maff);
2314 #define PW isl_pw_multi_aff
2316 #define EL isl_multi_aff
2318 #define EL_IS_ZERO is_empty
2322 #define IS_ZERO is_empty
2325 #undef DEFAULT_IS_ZERO
2326 #define DEFAULT_IS_ZERO 0
2331 #define NO_INVOLVES_DIMS
2332 #define NO_MOVE_DIMS
2333 #define NO_INSERT_DIMS
2337 #include <isl_pw_templ.c>
2340 #define UNION isl_union_pw_multi_aff
2342 #define PART isl_pw_multi_aff
2344 #define PARTS pw_multi_aff
2345 #define ALIGN_DOMAIN
2349 #include <isl_union_templ.c>
2351 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2352 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2354 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2355 &isl_multi_aff_add);
2358 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2359 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2361 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2365 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2366 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2368 return isl_pw_multi_aff_union_add_(pma1, pma2);
2371 /* Construct a map mapping the domain of the piecewise multi-affine expression
2372 * to its range, with each dimension in the range equated to the
2373 * corresponding affine expression on its cell.
2375 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2383 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2385 for (i = 0; i < pma->n; ++i) {
2386 isl_multi_aff *maff;
2387 isl_basic_map *bmap;
2390 maff = isl_multi_aff_copy(pma->p[i].maff);
2391 bmap = isl_basic_map_from_multi_aff(maff);
2392 map_i = isl_map_from_basic_map(bmap);
2393 map_i = isl_map_intersect_domain(map_i,
2394 isl_set_copy(pma->p[i].set));
2395 map = isl_map_union_disjoint(map, map_i);
2398 isl_pw_multi_aff_free(pma);
2402 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2404 if (!isl_space_is_set(pma->dim))
2405 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2406 "isl_pw_multi_aff cannot be converted into an isl_set",
2407 return isl_pw_multi_aff_free(pma));
2409 return isl_map_from_pw_multi_aff(pma);
2412 /* Given a basic map with a single output dimension that is defined
2413 * in terms of the parameters and input dimensions using an equality,
2414 * extract an isl_aff that expresses the output dimension in terms
2415 * of the parameters and input dimensions.
2417 * Since some applications expect the result of isl_pw_multi_aff_from_map
2418 * to only contain integer affine expressions, we compute the floor
2419 * of the expression before returning.
2421 * This function shares some similarities with
2422 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2424 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
2425 __isl_take isl_basic_map *bmap)
2430 isl_local_space *ls;
2435 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
2436 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2437 "basic map should have a single output dimension",
2439 offset = isl_basic_map_offset(bmap, isl_dim_out);
2440 total = isl_basic_map_total_dim(bmap);
2441 for (i = 0; i < bmap->n_eq; ++i) {
2442 if (isl_int_is_zero(bmap->eq[i][offset]))
2444 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
2445 1 + total - (offset + 1)) != -1)
2449 if (i >= bmap->n_eq)
2450 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2451 "unable to find suitable equality", goto error);
2452 ls = isl_basic_map_get_local_space(bmap);
2453 aff = isl_aff_alloc(isl_local_space_domain(ls));
2456 if (isl_int_is_neg(bmap->eq[i][offset]))
2457 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
2459 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
2460 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
2461 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
2462 isl_basic_map_free(bmap);
2464 aff = isl_aff_remove_unused_divs(aff);
2465 aff = isl_aff_floor(aff);
2468 isl_basic_map_free(bmap);
2472 /* Given a basic map where each output dimension is defined
2473 * in terms of the parameters and input dimensions using an equality,
2474 * extract an isl_multi_aff that expresses the output dimensions in terms
2475 * of the parameters and input dimensions.
2477 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
2478 __isl_take isl_basic_map *bmap)
2487 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
2488 n_out = isl_basic_map_dim(bmap, isl_dim_out);
2490 for (i = 0; i < n_out; ++i) {
2491 isl_basic_map *bmap_i;
2494 bmap_i = isl_basic_map_copy(bmap);
2495 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
2496 i + 1, n_out - (1 + i));
2497 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
2498 aff = extract_isl_aff_from_basic_map(bmap_i);
2499 ma = isl_multi_aff_set_aff(ma, i, aff);
2502 isl_basic_map_free(bmap);
2507 /* Create an isl_pw_multi_aff that is equivalent to
2508 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2509 * The given basic map is such that each output dimension is defined
2510 * in terms of the parameters and input dimensions using an equality.
2512 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
2513 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
2517 ma = extract_isl_multi_aff_from_basic_map(bmap);
2518 return isl_pw_multi_aff_alloc(domain, ma);
2521 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2522 * This obivously only works if the input "map" is single-valued.
2523 * If so, we compute the lexicographic minimum of the image in the form
2524 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2525 * to its lexicographic minimum.
2526 * If the input is not single-valued, we produce an error.
2528 * As a special case, we first check if all output dimensions are uniquely
2529 * defined in terms of the parameters and input dimensions over the entire
2530 * domain. If so, we extract the desired isl_pw_multi_aff directly
2531 * from the affine hull of "map" and its domain.
2533 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2537 isl_pw_multi_aff *pma;
2538 isl_basic_map *hull;
2543 hull = isl_map_affine_hull(isl_map_copy(map));
2544 sv = isl_basic_map_plain_is_single_valued(hull);
2546 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
2547 isl_basic_map_free(hull);
2551 sv = isl_map_is_single_valued(map);
2555 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2556 "map is not single-valued", goto error);
2557 map = isl_map_make_disjoint(map);
2561 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2563 for (i = 0; i < map->n; ++i) {
2564 isl_pw_multi_aff *pma_i;
2565 isl_basic_map *bmap;
2566 bmap = isl_basic_map_copy(map->p[i]);
2567 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2568 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2578 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2580 return isl_pw_multi_aff_from_map(set);
2583 /* Return the piecewise affine expression "set ? 1 : 0".
2585 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
2588 isl_space *space = isl_set_get_space(set);
2589 isl_local_space *ls = isl_local_space_from_space(space);
2590 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
2591 isl_aff *one = isl_aff_zero_on_domain(ls);
2593 one = isl_aff_add_constant_si(one, 1);
2594 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
2595 set = isl_set_complement(set);
2596 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
2601 /* Plug in "subs" for dimension "type", "pos" of "aff".
2603 * Let i be the dimension to replace and let "subs" be of the form
2607 * and "aff" of the form
2613 * floor((a f + d g')/(m d))
2615 * where g' is the result of plugging in "subs" in each of the integer
2618 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2619 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2624 aff = isl_aff_cow(aff);
2626 return isl_aff_free(aff);
2628 ctx = isl_aff_get_ctx(aff);
2629 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2630 isl_die(ctx, isl_error_invalid,
2631 "spaces don't match", return isl_aff_free(aff));
2632 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2633 isl_die(ctx, isl_error_unsupported,
2634 "cannot handle divs yet", return isl_aff_free(aff));
2636 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2638 return isl_aff_free(aff);
2640 aff->v = isl_vec_cow(aff->v);
2642 return isl_aff_free(aff);
2644 pos += isl_local_space_offset(aff->ls, type);
2647 isl_int_set(v, aff->v->el[1 + pos]);
2648 isl_int_set_si(aff->v->el[1 + pos], 0);
2649 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2650 v, subs->v->el + 1, subs->v->size - 1);
2651 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2657 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2658 * expressions in "maff".
2660 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2661 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2662 __isl_keep isl_aff *subs)
2666 maff = isl_multi_aff_cow(maff);
2668 return isl_multi_aff_free(maff);
2670 if (type == isl_dim_in)
2673 for (i = 0; i < maff->n; ++i) {
2674 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2676 return isl_multi_aff_free(maff);
2682 /* Plug in "subs" for dimension "type", "pos" of "pma".
2684 * pma is of the form
2688 * while subs is of the form
2690 * v' = B_j(v) -> S_j
2692 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2693 * has a contribution in the result, in particular
2695 * C_ij(S_j) -> M_i(S_j)
2697 * Note that plugging in S_j in C_ij may also result in an empty set
2698 * and this contribution should simply be discarded.
2700 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2701 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2702 __isl_keep isl_pw_aff *subs)
2705 isl_pw_multi_aff *res;
2708 return isl_pw_multi_aff_free(pma);
2710 n = pma->n * subs->n;
2711 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2713 for (i = 0; i < pma->n; ++i) {
2714 for (j = 0; j < subs->n; ++j) {
2716 isl_multi_aff *res_ij;
2717 common = isl_set_intersect(
2718 isl_set_copy(pma->p[i].set),
2719 isl_set_copy(subs->p[j].set));
2720 common = isl_set_substitute(common,
2721 type, pos, subs->p[j].aff);
2722 if (isl_set_plain_is_empty(common)) {
2723 isl_set_free(common);
2727 res_ij = isl_multi_aff_substitute(
2728 isl_multi_aff_copy(pma->p[i].maff),
2729 type, pos, subs->p[j].aff);
2731 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2735 isl_pw_multi_aff_free(pma);
2739 /* Extend the local space of "dst" to include the divs
2740 * in the local space of "src".
2742 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2743 __isl_keep isl_aff *src)
2751 return isl_aff_free(dst);
2753 ctx = isl_aff_get_ctx(src);
2754 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2755 isl_die(ctx, isl_error_invalid,
2756 "spaces don't match", goto error);
2758 if (src->ls->div->n_row == 0)
2761 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2762 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2766 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2767 dst = isl_aff_expand_divs(dst, div, exp2);
2775 return isl_aff_free(dst);
2778 /* Adjust the local spaces of the affine expressions in "maff"
2779 * such that they all have the save divs.
2781 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2782 __isl_take isl_multi_aff *maff)
2790 maff = isl_multi_aff_cow(maff);
2794 for (i = 1; i < maff->n; ++i)
2795 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2796 for (i = 1; i < maff->n; ++i) {
2797 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2799 return isl_multi_aff_free(maff);
2805 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2807 aff = isl_aff_cow(aff);
2811 aff->ls = isl_local_space_lift(aff->ls);
2813 return isl_aff_free(aff);
2818 /* Lift "maff" to a space with extra dimensions such that the result
2819 * has no more existentially quantified variables.
2820 * If "ls" is not NULL, then *ls is assigned the local space that lies
2821 * at the basis of the lifting applied to "maff".
2823 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2824 __isl_give isl_local_space **ls)
2838 isl_space *space = isl_multi_aff_get_domain_space(maff);
2839 *ls = isl_local_space_from_space(space);
2841 return isl_multi_aff_free(maff);
2846 maff = isl_multi_aff_cow(maff);
2847 maff = isl_multi_aff_align_divs(maff);
2851 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2852 space = isl_multi_aff_get_space(maff);
2853 space = isl_space_lift(isl_space_domain(space), n_div);
2854 space = isl_space_extend_domain_with_range(space,
2855 isl_multi_aff_get_space(maff));
2857 return isl_multi_aff_free(maff);
2858 isl_space_free(maff->space);
2859 maff->space = space;
2862 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2864 return isl_multi_aff_free(maff);
2867 for (i = 0; i < maff->n; ++i) {
2868 maff->p[i] = isl_aff_lift(maff->p[i]);
2876 isl_local_space_free(*ls);
2877 return isl_multi_aff_free(maff);
2881 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2883 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2884 __isl_keep isl_pw_multi_aff *pma, int pos)
2894 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2895 if (pos < 0 || pos >= n_out)
2896 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2897 "index out of bounds", return NULL);
2899 space = isl_pw_multi_aff_get_space(pma);
2900 space = isl_space_drop_dims(space, isl_dim_out,
2901 pos + 1, n_out - pos - 1);
2902 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2904 pa = isl_pw_aff_alloc_size(space, pma->n);
2905 for (i = 0; i < pma->n; ++i) {
2907 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2908 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
2914 /* Return an isl_pw_multi_aff with the given "set" as domain and
2915 * an unnamed zero-dimensional range.
2917 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
2918 __isl_take isl_set *set)
2923 space = isl_set_get_space(set);
2924 space = isl_space_from_domain(space);
2925 ma = isl_multi_aff_zero(space);
2926 return isl_pw_multi_aff_alloc(set, ma);
2929 /* Add an isl_pw_multi_aff with the given "set" as domain and
2930 * an unnamed zero-dimensional range to *user.
2932 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
2934 isl_union_pw_multi_aff **upma = user;
2935 isl_pw_multi_aff *pma;
2937 pma = isl_pw_multi_aff_from_domain(set);
2938 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
2943 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
2944 * an unnamed zero-dimensional range.
2946 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
2947 __isl_take isl_union_set *uset)
2950 isl_union_pw_multi_aff *upma;
2955 space = isl_union_set_get_space(uset);
2956 upma = isl_union_pw_multi_aff_empty(space);
2958 if (isl_union_set_foreach_set(uset,
2959 &add_pw_multi_aff_from_domain, &upma) < 0)
2962 isl_union_set_free(uset);
2965 isl_union_set_free(uset);
2966 isl_union_pw_multi_aff_free(upma);
2970 /* Convert "pma" to an isl_map and add it to *umap.
2972 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
2974 isl_union_map **umap = user;
2977 map = isl_map_from_pw_multi_aff(pma);
2978 *umap = isl_union_map_add_map(*umap, map);
2983 /* Construct a union map mapping the domain of the union
2984 * piecewise multi-affine expression to its range, with each dimension
2985 * in the range equated to the corresponding affine expression on its cell.
2987 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
2988 __isl_take isl_union_pw_multi_aff *upma)
2991 isl_union_map *umap;
2996 space = isl_union_pw_multi_aff_get_space(upma);
2997 umap = isl_union_map_empty(space);
2999 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3000 &map_from_pw_multi_aff, &umap) < 0)
3003 isl_union_pw_multi_aff_free(upma);
3006 isl_union_pw_multi_aff_free(upma);
3007 isl_union_map_free(umap);
3011 /* Local data for bin_entry and the callback "fn".
3013 struct isl_union_pw_multi_aff_bin_data {
3014 isl_union_pw_multi_aff *upma2;
3015 isl_union_pw_multi_aff *res;
3016 isl_pw_multi_aff *pma;
3017 int (*fn)(void **entry, void *user);
3020 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
3021 * and call data->fn for each isl_pw_multi_aff in data->upma2.
3023 static int bin_entry(void **entry, void *user)
3025 struct isl_union_pw_multi_aff_bin_data *data = user;
3026 isl_pw_multi_aff *pma = *entry;
3029 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
3030 data->fn, data) < 0)
3036 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
3037 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
3038 * passed as user field) and the isl_pw_multi_aff from upma2 is available
3039 * as *entry. The callback should adjust data->res if desired.
3041 static __isl_give isl_union_pw_multi_aff *bin_op(
3042 __isl_take isl_union_pw_multi_aff *upma1,
3043 __isl_take isl_union_pw_multi_aff *upma2,
3044 int (*fn)(void **entry, void *user))
3047 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
3049 space = isl_union_pw_multi_aff_get_space(upma2);
3050 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
3051 space = isl_union_pw_multi_aff_get_space(upma1);
3052 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
3054 if (!upma1 || !upma2)
3058 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
3060 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
3061 &bin_entry, &data) < 0)
3064 isl_union_pw_multi_aff_free(upma1);
3065 isl_union_pw_multi_aff_free(upma2);
3068 isl_union_pw_multi_aff_free(upma1);
3069 isl_union_pw_multi_aff_free(upma2);
3070 isl_union_pw_multi_aff_free(data.res);
3074 /* Given two isl_multi_affs A -> B and C -> D,
3075 * construct an isl_multi_aff (A * C) -> (B, D).
3077 __isl_give isl_multi_aff *isl_multi_aff_flat_range_product(
3078 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3088 space = isl_space_range_product(isl_multi_aff_get_space(ma1),
3089 isl_multi_aff_get_space(ma2));
3090 space = isl_space_flatten_range(space);
3091 res = isl_multi_aff_alloc(space);
3093 n1 = isl_multi_aff_dim(ma1, isl_dim_out);
3094 n2 = isl_multi_aff_dim(ma2, isl_dim_out);
3096 for (i = 0; i < n1; ++i) {
3097 aff = isl_multi_aff_get_aff(ma1, i);
3098 res = isl_multi_aff_set_aff(res, i, aff);
3101 for (i = 0; i < n2; ++i) {
3102 aff = isl_multi_aff_get_aff(ma2, i);
3103 res = isl_multi_aff_set_aff(res, n1 + i, aff);
3106 isl_multi_aff_free(ma1);
3107 isl_multi_aff_free(ma2);
3110 isl_multi_aff_free(ma1);
3111 isl_multi_aff_free(ma2);
3115 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
3116 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3118 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
3119 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3123 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
3124 isl_pw_multi_aff_get_space(pma2));
3125 space = isl_space_flatten_range(space);
3126 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
3127 &isl_multi_aff_flat_range_product);
3130 /* Given two isl_pw_multi_affs A -> B and C -> D,
3131 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3133 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
3134 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3136 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3137 &pw_multi_aff_flat_range_product);
3140 /* If data->pma and *entry have the same domain space, then compute
3141 * their flat range product and the result to data->res.
3143 static int flat_range_product_entry(void **entry, void *user)
3145 struct isl_union_pw_multi_aff_bin_data *data = user;
3146 isl_pw_multi_aff *pma2 = *entry;
3148 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
3149 pma2->dim, isl_dim_in))
3152 pma2 = isl_pw_multi_aff_flat_range_product(
3153 isl_pw_multi_aff_copy(data->pma),
3154 isl_pw_multi_aff_copy(pma2));
3156 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
3161 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
3162 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
3164 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
3165 __isl_take isl_union_pw_multi_aff *upma1,
3166 __isl_take isl_union_pw_multi_aff *upma2)
3168 return bin_op(upma1, upma2, &flat_range_product_entry);