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);
905 aff->v = isl_vec_cow(aff->v);
907 return isl_aff_free(aff);
910 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
911 isl_int_gcd(gcd, gcd, f);
912 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
913 isl_int_divexact(gcd, f, gcd);
914 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
920 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
928 isl_int_set_ui(v, f);
929 aff = isl_aff_scale_down(aff, v);
935 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
936 enum isl_dim_type type, unsigned pos, const char *s)
938 aff = isl_aff_cow(aff);
941 if (type == isl_dim_out)
942 isl_die(aff->v->ctx, isl_error_invalid,
943 "cannot set name of output/set dimension",
944 return isl_aff_free(aff));
945 if (type == isl_dim_in)
947 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
949 return isl_aff_free(aff);
954 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
955 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
957 aff = isl_aff_cow(aff);
959 return isl_id_free(id);
960 if (type == isl_dim_out)
961 isl_die(aff->v->ctx, isl_error_invalid,
962 "cannot set name of output/set dimension",
964 if (type == isl_dim_in)
966 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
968 return isl_aff_free(aff);
977 /* Exploit the equalities in "eq" to simplify the affine expression
978 * and the expressions of the integer divisions in the local space.
979 * The integer divisions in this local space are assumed to appear
980 * as regular dimensions in "eq".
982 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
983 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
992 isl_basic_set_free(eq);
996 aff = isl_aff_cow(aff);
1000 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1001 isl_basic_set_copy(eq));
1005 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1007 for (i = 0; i < eq->n_eq; ++i) {
1008 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1009 if (j < 0 || j == 0 || j >= total)
1012 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1016 isl_basic_set_free(eq);
1017 aff = isl_aff_normalize(aff);
1020 isl_basic_set_free(eq);
1025 /* Exploit the equalities in "eq" to simplify the affine expression
1026 * and the expressions of the integer divisions in the local space.
1028 static __isl_give isl_aff *isl_aff_substitute_equalities(
1029 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1035 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1037 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
1038 return isl_aff_substitute_equalities_lifted(aff, eq);
1040 isl_basic_set_free(eq);
1045 /* Look for equalities among the variables shared by context and aff
1046 * and the integer divisions of aff, if any.
1047 * The equalities are then used to eliminate coefficients and/or integer
1048 * divisions from aff.
1050 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1051 __isl_take isl_set *context)
1053 isl_basic_set *hull;
1058 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1060 isl_basic_set *bset;
1061 isl_local_space *ls;
1062 context = isl_set_add_dims(context, isl_dim_set, n_div);
1063 ls = isl_aff_get_domain_local_space(aff);
1064 bset = isl_basic_set_from_local_space(ls);
1065 bset = isl_basic_set_lift(bset);
1066 bset = isl_basic_set_flatten(bset);
1067 context = isl_set_intersect(context,
1068 isl_set_from_basic_set(bset));
1071 hull = isl_set_affine_hull(context);
1072 return isl_aff_substitute_equalities_lifted(aff, hull);
1075 isl_set_free(context);
1079 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1080 __isl_take isl_set *context)
1082 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1083 dom_context = isl_set_intersect_params(dom_context, context);
1084 return isl_aff_gist(aff, dom_context);
1087 /* Return a basic set containing those elements in the space
1088 * of aff where it is non-negative.
1090 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1092 isl_constraint *ineq;
1093 isl_basic_set *bset;
1095 ineq = isl_inequality_from_aff(aff);
1097 bset = isl_basic_set_from_constraint(ineq);
1098 bset = isl_basic_set_simplify(bset);
1102 /* Return a basic set containing those elements in the space
1103 * of aff where it is zero.
1105 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1107 isl_constraint *ineq;
1108 isl_basic_set *bset;
1110 ineq = isl_equality_from_aff(aff);
1112 bset = isl_basic_set_from_constraint(ineq);
1113 bset = isl_basic_set_simplify(bset);
1117 /* Return a basic set containing those elements in the shared space
1118 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1120 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1121 __isl_take isl_aff *aff2)
1123 aff1 = isl_aff_sub(aff1, aff2);
1125 return isl_aff_nonneg_basic_set(aff1);
1128 /* Return a basic set containing those elements in the shared space
1129 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1131 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1132 __isl_take isl_aff *aff2)
1134 return isl_aff_ge_basic_set(aff2, aff1);
1137 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1138 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1140 aff1 = isl_aff_add(aff1, aff2);
1141 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1145 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1153 /* Check whether the given affine expression has non-zero coefficient
1154 * for any dimension in the given range or if any of these dimensions
1155 * appear with non-zero coefficients in any of the integer divisions
1156 * involved in the affine expression.
1158 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1159 enum isl_dim_type type, unsigned first, unsigned n)
1171 ctx = isl_aff_get_ctx(aff);
1172 if (first + n > isl_aff_dim(aff, type))
1173 isl_die(ctx, isl_error_invalid,
1174 "range out of bounds", return -1);
1176 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1180 first += isl_local_space_offset(aff->ls, type) - 1;
1181 for (i = 0; i < n; ++i)
1182 if (active[first + i]) {
1195 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1196 enum isl_dim_type type, unsigned first, unsigned n)
1202 if (type == isl_dim_out)
1203 isl_die(aff->v->ctx, isl_error_invalid,
1204 "cannot drop output/set dimension",
1205 return isl_aff_free(aff));
1206 if (type == isl_dim_in)
1208 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1211 ctx = isl_aff_get_ctx(aff);
1212 if (first + n > isl_local_space_dim(aff->ls, type))
1213 isl_die(ctx, isl_error_invalid, "range out of bounds",
1214 return isl_aff_free(aff));
1216 aff = isl_aff_cow(aff);
1220 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1222 return isl_aff_free(aff);
1224 first += 1 + isl_local_space_offset(aff->ls, type);
1225 aff->v = isl_vec_drop_els(aff->v, first, n);
1227 return isl_aff_free(aff);
1232 /* Project the domain of the affine expression onto its parameter space.
1233 * The affine expression may not involve any of the domain dimensions.
1235 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1241 n = isl_aff_dim(aff, isl_dim_in);
1242 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1244 return isl_aff_free(aff);
1246 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1247 "affine expression involves some of the domain dimensions",
1248 return isl_aff_free(aff));
1249 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1250 space = isl_aff_get_domain_space(aff);
1251 space = isl_space_params(space);
1252 aff = isl_aff_reset_domain_space(aff, space);
1256 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1257 enum isl_dim_type type, unsigned first, unsigned n)
1263 if (type == isl_dim_out)
1264 isl_die(aff->v->ctx, isl_error_invalid,
1265 "cannot insert output/set dimensions",
1266 return isl_aff_free(aff));
1267 if (type == isl_dim_in)
1269 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1272 ctx = isl_aff_get_ctx(aff);
1273 if (first > isl_local_space_dim(aff->ls, type))
1274 isl_die(ctx, isl_error_invalid, "position out of bounds",
1275 return isl_aff_free(aff));
1277 aff = isl_aff_cow(aff);
1281 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1283 return isl_aff_free(aff);
1285 first += 1 + isl_local_space_offset(aff->ls, type);
1286 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1288 return isl_aff_free(aff);
1293 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1294 enum isl_dim_type type, unsigned n)
1298 pos = isl_aff_dim(aff, type);
1300 return isl_aff_insert_dims(aff, type, pos, n);
1303 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1304 enum isl_dim_type type, unsigned n)
1308 pos = isl_pw_aff_dim(pwaff, type);
1310 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1313 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1315 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1316 return isl_pw_aff_alloc(dom, aff);
1320 #define PW isl_pw_aff
1324 #define EL_IS_ZERO is_empty
1328 #define IS_ZERO is_empty
1331 #undef DEFAULT_IS_ZERO
1332 #define DEFAULT_IS_ZERO 0
1336 #define NO_MOVE_DIMS
1340 #include <isl_pw_templ.c>
1342 static __isl_give isl_set *align_params_pw_pw_set_and(
1343 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1344 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1345 __isl_take isl_pw_aff *pwaff2))
1347 if (!pwaff1 || !pwaff2)
1349 if (isl_space_match(pwaff1->dim, isl_dim_param,
1350 pwaff2->dim, isl_dim_param))
1351 return fn(pwaff1, pwaff2);
1352 if (!isl_space_has_named_params(pwaff1->dim) ||
1353 !isl_space_has_named_params(pwaff2->dim))
1354 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1355 "unaligned unnamed parameters", goto error);
1356 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1357 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1358 return fn(pwaff1, pwaff2);
1360 isl_pw_aff_free(pwaff1);
1361 isl_pw_aff_free(pwaff2);
1365 /* Compute a piecewise quasi-affine expression with a domain that
1366 * is the union of those of pwaff1 and pwaff2 and such that on each
1367 * cell, the quasi-affine expression is the better (according to cmp)
1368 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1369 * is defined on a given cell, then the associated expression
1370 * is the defined one.
1372 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1373 __isl_take isl_pw_aff *pwaff2,
1374 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1375 __isl_take isl_aff *aff2))
1382 if (!pwaff1 || !pwaff2)
1385 ctx = isl_space_get_ctx(pwaff1->dim);
1386 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1387 isl_die(ctx, isl_error_invalid,
1388 "arguments should live in same space", goto error);
1390 if (isl_pw_aff_is_empty(pwaff1)) {
1391 isl_pw_aff_free(pwaff1);
1395 if (isl_pw_aff_is_empty(pwaff2)) {
1396 isl_pw_aff_free(pwaff2);
1400 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1401 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1403 for (i = 0; i < pwaff1->n; ++i) {
1404 set = isl_set_copy(pwaff1->p[i].set);
1405 for (j = 0; j < pwaff2->n; ++j) {
1406 struct isl_set *common;
1409 common = isl_set_intersect(
1410 isl_set_copy(pwaff1->p[i].set),
1411 isl_set_copy(pwaff2->p[j].set));
1412 better = isl_set_from_basic_set(cmp(
1413 isl_aff_copy(pwaff2->p[j].aff),
1414 isl_aff_copy(pwaff1->p[i].aff)));
1415 better = isl_set_intersect(common, better);
1416 if (isl_set_plain_is_empty(better)) {
1417 isl_set_free(better);
1420 set = isl_set_subtract(set, isl_set_copy(better));
1422 res = isl_pw_aff_add_piece(res, better,
1423 isl_aff_copy(pwaff2->p[j].aff));
1425 res = isl_pw_aff_add_piece(res, set,
1426 isl_aff_copy(pwaff1->p[i].aff));
1429 for (j = 0; j < pwaff2->n; ++j) {
1430 set = isl_set_copy(pwaff2->p[j].set);
1431 for (i = 0; i < pwaff1->n; ++i)
1432 set = isl_set_subtract(set,
1433 isl_set_copy(pwaff1->p[i].set));
1434 res = isl_pw_aff_add_piece(res, set,
1435 isl_aff_copy(pwaff2->p[j].aff));
1438 isl_pw_aff_free(pwaff1);
1439 isl_pw_aff_free(pwaff2);
1443 isl_pw_aff_free(pwaff1);
1444 isl_pw_aff_free(pwaff2);
1448 /* Compute a piecewise quasi-affine expression with a domain that
1449 * is the union of those of pwaff1 and pwaff2 and such that on each
1450 * cell, the quasi-affine expression is the maximum of those of pwaff1
1451 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1452 * cell, then the associated expression is the defined one.
1454 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1455 __isl_take isl_pw_aff *pwaff2)
1457 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1460 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1461 __isl_take isl_pw_aff *pwaff2)
1463 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1467 /* Compute a piecewise quasi-affine expression with a domain that
1468 * is the union of those of pwaff1 and pwaff2 and such that on each
1469 * cell, the quasi-affine expression is the minimum of those of pwaff1
1470 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1471 * cell, then the associated expression is the defined one.
1473 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1474 __isl_take isl_pw_aff *pwaff2)
1476 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1479 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1480 __isl_take isl_pw_aff *pwaff2)
1482 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1486 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1487 __isl_take isl_pw_aff *pwaff2, int max)
1490 return isl_pw_aff_union_max(pwaff1, pwaff2);
1492 return isl_pw_aff_union_min(pwaff1, pwaff2);
1495 /* Construct a map with as domain the domain of pwaff and
1496 * one-dimensional range corresponding to the affine expressions.
1498 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1507 dim = isl_pw_aff_get_space(pwaff);
1508 map = isl_map_empty(dim);
1510 for (i = 0; i < pwaff->n; ++i) {
1511 isl_basic_map *bmap;
1514 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1515 map_i = isl_map_from_basic_map(bmap);
1516 map_i = isl_map_intersect_domain(map_i,
1517 isl_set_copy(pwaff->p[i].set));
1518 map = isl_map_union_disjoint(map, map_i);
1521 isl_pw_aff_free(pwaff);
1526 /* Construct a map with as domain the domain of pwaff and
1527 * one-dimensional range corresponding to the affine expressions.
1529 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1533 if (isl_space_is_set(pwaff->dim))
1534 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1535 "space of input is not a map",
1536 return isl_pw_aff_free(pwaff));
1537 return map_from_pw_aff(pwaff);
1540 /* Construct a one-dimensional set with as parameter domain
1541 * the domain of pwaff and the single set dimension
1542 * corresponding to the affine expressions.
1544 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1548 if (!isl_space_is_set(pwaff->dim))
1549 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1550 "space of input is not a set",
1551 return isl_pw_aff_free(pwaff));
1552 return map_from_pw_aff(pwaff);
1555 /* Return a set containing those elements in the domain
1556 * of pwaff where it is non-negative.
1558 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1566 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1568 for (i = 0; i < pwaff->n; ++i) {
1569 isl_basic_set *bset;
1572 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1573 set_i = isl_set_from_basic_set(bset);
1574 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1575 set = isl_set_union_disjoint(set, set_i);
1578 isl_pw_aff_free(pwaff);
1583 /* Return a set containing those elements in the domain
1584 * of pwaff where it is zero (if complement is 0) or not zero
1585 * (if complement is 1).
1587 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1596 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1598 for (i = 0; i < pwaff->n; ++i) {
1599 isl_basic_set *bset;
1600 isl_set *set_i, *zero;
1602 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1603 zero = isl_set_from_basic_set(bset);
1604 set_i = isl_set_copy(pwaff->p[i].set);
1606 set_i = isl_set_subtract(set_i, zero);
1608 set_i = isl_set_intersect(set_i, zero);
1609 set = isl_set_union_disjoint(set, set_i);
1612 isl_pw_aff_free(pwaff);
1617 /* Return a set containing those elements in the domain
1618 * of pwaff where it is zero.
1620 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1622 return pw_aff_zero_set(pwaff, 0);
1625 /* Return a set containing those elements in the domain
1626 * of pwaff where it is not zero.
1628 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1630 return pw_aff_zero_set(pwaff, 1);
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 * We compute the difference on the shared domain and then construct
1637 * the set of values where this difference is non-negative.
1638 * If strict is set, we first subtract 1 from the difference.
1639 * If equal is set, we only return the elements where pwaff1 and pwaff2
1642 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1643 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1645 isl_set *set1, *set2;
1647 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1648 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1649 set1 = isl_set_intersect(set1, set2);
1650 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1651 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1652 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1655 isl_space *dim = isl_set_get_space(set1);
1657 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1658 aff = isl_aff_add_constant_si(aff, -1);
1659 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1664 return isl_pw_aff_zero_set(pwaff1);
1665 return isl_pw_aff_nonneg_set(pwaff1);
1668 /* Return a set containing those elements in the shared domain
1669 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1671 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1672 __isl_take isl_pw_aff *pwaff2)
1674 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1677 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1678 __isl_take isl_pw_aff *pwaff2)
1680 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1683 /* Return a set containing those elements in the shared domain
1684 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1686 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1687 __isl_take isl_pw_aff *pwaff2)
1689 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1692 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1693 __isl_take isl_pw_aff *pwaff2)
1695 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1698 /* Return a set containing those elements in the shared domain
1699 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1701 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1702 __isl_take isl_pw_aff *pwaff2)
1704 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1707 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1708 __isl_take isl_pw_aff *pwaff2)
1710 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1713 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1714 __isl_take isl_pw_aff *pwaff2)
1716 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1719 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1720 __isl_take isl_pw_aff *pwaff2)
1722 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1725 /* Return a set containing those elements in the shared domain
1726 * of the elements of list1 and list2 where each element in list1
1727 * has the relation specified by "fn" with each element in list2.
1729 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1730 __isl_take isl_pw_aff_list *list2,
1731 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1732 __isl_take isl_pw_aff *pwaff2))
1738 if (!list1 || !list2)
1741 ctx = isl_pw_aff_list_get_ctx(list1);
1742 if (list1->n < 1 || list2->n < 1)
1743 isl_die(ctx, isl_error_invalid,
1744 "list should contain at least one element", goto error);
1746 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1747 for (i = 0; i < list1->n; ++i)
1748 for (j = 0; j < list2->n; ++j) {
1751 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1752 isl_pw_aff_copy(list2->p[j]));
1753 set = isl_set_intersect(set, set_ij);
1756 isl_pw_aff_list_free(list1);
1757 isl_pw_aff_list_free(list2);
1760 isl_pw_aff_list_free(list1);
1761 isl_pw_aff_list_free(list2);
1765 /* Return a set containing those elements in the shared domain
1766 * of the elements of list1 and list2 where each element in list1
1767 * is equal to each element in list2.
1769 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1770 __isl_take isl_pw_aff_list *list2)
1772 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1775 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1776 __isl_take isl_pw_aff_list *list2)
1778 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1781 /* Return a set containing those elements in the shared domain
1782 * of the elements of list1 and list2 where each element in list1
1783 * is less than or equal to each element in list2.
1785 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1786 __isl_take isl_pw_aff_list *list2)
1788 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1791 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1792 __isl_take isl_pw_aff_list *list2)
1794 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1797 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1798 __isl_take isl_pw_aff_list *list2)
1800 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1803 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1804 __isl_take isl_pw_aff_list *list2)
1806 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1810 /* Return a set containing those elements in the shared domain
1811 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1813 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1814 __isl_take isl_pw_aff *pwaff2)
1816 isl_set *set_lt, *set_gt;
1818 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1819 isl_pw_aff_copy(pwaff2));
1820 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1821 return isl_set_union_disjoint(set_lt, set_gt);
1824 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1825 __isl_take isl_pw_aff *pwaff2)
1827 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1830 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1835 if (isl_int_is_one(v))
1837 if (!isl_int_is_pos(v))
1838 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1839 "factor needs to be positive",
1840 return isl_pw_aff_free(pwaff));
1841 pwaff = isl_pw_aff_cow(pwaff);
1847 for (i = 0; i < pwaff->n; ++i) {
1848 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1849 if (!pwaff->p[i].aff)
1850 return isl_pw_aff_free(pwaff);
1856 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1860 pwaff = isl_pw_aff_cow(pwaff);
1866 for (i = 0; i < pwaff->n; ++i) {
1867 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1868 if (!pwaff->p[i].aff)
1869 return isl_pw_aff_free(pwaff);
1875 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1879 pwaff = isl_pw_aff_cow(pwaff);
1885 for (i = 0; i < pwaff->n; ++i) {
1886 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1887 if (!pwaff->p[i].aff)
1888 return isl_pw_aff_free(pwaff);
1894 /* Assuming that "cond1" and "cond2" are disjoint,
1895 * return an affine expression that is equal to pwaff1 on cond1
1896 * and to pwaff2 on cond2.
1898 static __isl_give isl_pw_aff *isl_pw_aff_select(
1899 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
1900 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
1902 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
1903 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
1905 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
1908 /* Return an affine expression that is equal to pwaff_true for elements
1909 * where "cond" is non-zero and to pwaff_false for elements where "cond"
1911 * That is, return cond ? pwaff_true : pwaff_false;
1913 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
1914 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1916 isl_set *cond_true, *cond_false;
1918 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1919 cond_false = isl_pw_aff_zero_set(cond);
1920 return isl_pw_aff_select(cond_true, pwaff_true,
1921 cond_false, pwaff_false);
1924 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1929 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1932 /* Check whether pwaff is a piecewise constant.
1934 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1941 for (i = 0; i < pwaff->n; ++i) {
1942 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1943 if (is_cst < 0 || !is_cst)
1950 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1951 __isl_take isl_aff *aff2)
1953 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1954 return isl_aff_mul(aff2, aff1);
1956 if (!isl_aff_is_cst(aff2))
1957 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1958 "at least one affine expression should be constant",
1961 aff1 = isl_aff_cow(aff1);
1965 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1966 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1976 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1977 __isl_take isl_pw_aff *pwaff2)
1979 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1982 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1983 __isl_take isl_pw_aff *pwaff2)
1985 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1988 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1989 __isl_take isl_pw_aff *pwaff2)
1991 return isl_pw_aff_union_add_(pwaff1, pwaff2);
1994 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1995 __isl_take isl_pw_aff *pwaff2)
1997 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2000 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2001 __isl_take isl_pw_aff *pwaff2)
2003 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2006 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2007 __isl_take isl_pw_aff *pwaff2)
2012 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2013 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2014 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2015 isl_pw_aff_copy(pwaff2));
2016 dom = isl_set_subtract(dom, isl_set_copy(le));
2017 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2020 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2021 __isl_take isl_pw_aff *pwaff2)
2023 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2026 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2027 __isl_take isl_pw_aff *pwaff2)
2032 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2033 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2034 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2035 isl_pw_aff_copy(pwaff2));
2036 dom = isl_set_subtract(dom, isl_set_copy(ge));
2037 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2040 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2041 __isl_take isl_pw_aff *pwaff2)
2043 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2046 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2047 __isl_take isl_pw_aff_list *list,
2048 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2049 __isl_take isl_pw_aff *pwaff2))
2058 ctx = isl_pw_aff_list_get_ctx(list);
2060 isl_die(ctx, isl_error_invalid,
2061 "list should contain at least one element",
2062 return isl_pw_aff_list_free(list));
2064 res = isl_pw_aff_copy(list->p[0]);
2065 for (i = 1; i < list->n; ++i)
2066 res = fn(res, isl_pw_aff_copy(list->p[i]));
2068 isl_pw_aff_list_free(list);
2072 /* Return an isl_pw_aff that maps each element in the intersection of the
2073 * domains of the elements of list to the minimal corresponding affine
2076 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2078 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2081 /* Return an isl_pw_aff that maps each element in the intersection of the
2082 * domains of the elements of list to the maximal corresponding affine
2085 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2087 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2093 #include <isl_multi_templ.c>
2095 /* Construct an isl_multi_aff in the given space with value zero in
2096 * each of the output dimensions.
2098 __isl_give isl_multi_aff *isl_multi_aff_zero(__isl_take isl_space *space)
2106 n = isl_space_dim(space , isl_dim_out);
2107 ma = isl_multi_aff_alloc(isl_space_copy(space));
2110 isl_space_free(space);
2113 isl_local_space *ls;
2116 space = isl_space_domain(space);
2117 ls = isl_local_space_from_space(space);
2118 aff = isl_aff_zero_on_domain(ls);
2120 for (i = 0; i < n; ++i)
2121 ma = isl_multi_aff_set_aff(ma, i, isl_aff_copy(aff));
2129 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2132 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2133 __isl_take isl_multi_aff *ma)
2135 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2136 return isl_pw_multi_aff_alloc(dom, ma);
2139 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2140 __isl_take isl_multi_aff *maff2)
2145 maff1 = isl_multi_aff_cow(maff1);
2146 if (!maff1 || !maff2)
2149 ctx = isl_multi_aff_get_ctx(maff1);
2150 if (!isl_space_is_equal(maff1->space, maff2->space))
2151 isl_die(ctx, isl_error_invalid,
2152 "spaces don't match", goto error);
2154 for (i = 0; i < maff1->n; ++i) {
2155 maff1->p[i] = isl_aff_add(maff1->p[i],
2156 isl_aff_copy(maff2->p[i]));
2161 isl_multi_aff_free(maff2);
2164 isl_multi_aff_free(maff1);
2165 isl_multi_aff_free(maff2);
2169 /* Exploit the equalities in "eq" to simplify the affine expressions.
2171 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2172 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2176 maff = isl_multi_aff_cow(maff);
2180 for (i = 0; i < maff->n; ++i) {
2181 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2182 isl_basic_set_copy(eq));
2187 isl_basic_set_free(eq);
2190 isl_basic_set_free(eq);
2191 isl_multi_aff_free(maff);
2195 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2200 maff = isl_multi_aff_cow(maff);
2204 for (i = 0; i < maff->n; ++i) {
2205 maff->p[i] = isl_aff_scale(maff->p[i], f);
2207 return isl_multi_aff_free(maff);
2213 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2214 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2216 maff1 = isl_multi_aff_add(maff1, maff2);
2217 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2221 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2229 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2230 __isl_keep isl_multi_aff *maff2)
2235 if (!maff1 || !maff2)
2237 if (maff1->n != maff2->n)
2239 equal = isl_space_is_equal(maff1->space, maff2->space);
2240 if (equal < 0 || !equal)
2243 for (i = 0; i < maff1->n; ++i) {
2244 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2245 if (equal < 0 || !equal)
2252 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2253 __isl_take isl_multi_aff *maff,
2254 enum isl_dim_type type, unsigned pos, const char *s)
2258 maff = isl_multi_aff_cow(maff);
2262 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2264 return isl_multi_aff_free(maff);
2266 if (type == isl_dim_out)
2268 for (i = 0; i < maff->n; ++i) {
2269 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2271 return isl_multi_aff_free(maff);
2277 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2278 enum isl_dim_type type, unsigned first, unsigned n)
2282 maff = isl_multi_aff_cow(maff);
2286 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2288 return isl_multi_aff_free(maff);
2290 if (type == isl_dim_out) {
2291 for (i = 0; i < n; ++i)
2292 isl_aff_free(maff->p[first + i]);
2293 for (i = first; i + n < maff->n; ++i)
2294 maff->p[i] = maff->p[i + n];
2299 for (i = 0; i < maff->n; ++i) {
2300 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2302 return isl_multi_aff_free(maff);
2309 #define PW isl_pw_multi_aff
2311 #define EL isl_multi_aff
2313 #define EL_IS_ZERO is_empty
2317 #define IS_ZERO is_empty
2320 #undef DEFAULT_IS_ZERO
2321 #define DEFAULT_IS_ZERO 0
2326 #define NO_INVOLVES_DIMS
2327 #define NO_MOVE_DIMS
2328 #define NO_INSERT_DIMS
2332 #include <isl_pw_templ.c>
2335 #define UNION isl_union_pw_multi_aff
2337 #define PART isl_pw_multi_aff
2339 #define PARTS pw_multi_aff
2340 #define ALIGN_DOMAIN
2344 #include <isl_union_templ.c>
2346 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2347 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2349 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2350 &isl_multi_aff_add);
2353 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2354 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2356 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2360 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2361 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2363 return isl_pw_multi_aff_union_add_(pma1, pma2);
2366 /* Construct a map mapping the domain of the piecewise multi-affine expression
2367 * to its range, with each dimension in the range equated to the
2368 * corresponding affine expression on its cell.
2370 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2378 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2380 for (i = 0; i < pma->n; ++i) {
2381 isl_multi_aff *maff;
2382 isl_basic_map *bmap;
2385 maff = isl_multi_aff_copy(pma->p[i].maff);
2386 bmap = isl_basic_map_from_multi_aff(maff);
2387 map_i = isl_map_from_basic_map(bmap);
2388 map_i = isl_map_intersect_domain(map_i,
2389 isl_set_copy(pma->p[i].set));
2390 map = isl_map_union_disjoint(map, map_i);
2393 isl_pw_multi_aff_free(pma);
2397 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2399 if (!isl_space_is_set(pma->dim))
2400 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2401 "isl_pw_multi_aff cannot be converted into an isl_set",
2402 return isl_pw_multi_aff_free(pma));
2404 return isl_map_from_pw_multi_aff(pma);
2407 /* Given a basic map with a single output dimension that is defined
2408 * in terms of the parameters and input dimensions using an equality,
2409 * extract an isl_aff that expresses the output dimension in terms
2410 * of the parameters and input dimensions.
2412 * Since some applications expect the result of isl_pw_multi_aff_from_map
2413 * to only contain integer affine expressions, we compute the floor
2414 * of the expression before returning.
2416 * This function shares some similarities with
2417 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2419 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
2420 __isl_take isl_basic_map *bmap)
2425 isl_local_space *ls;
2430 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
2431 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2432 "basic map should have a single output dimension",
2434 offset = isl_basic_map_offset(bmap, isl_dim_out);
2435 total = isl_basic_map_total_dim(bmap);
2436 for (i = 0; i < bmap->n_eq; ++i) {
2437 if (isl_int_is_zero(bmap->eq[i][offset]))
2439 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
2440 1 + total - (offset + 1)) != -1)
2444 if (i >= bmap->n_eq)
2445 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2446 "unable to find suitable equality", goto error);
2447 ls = isl_basic_map_get_local_space(bmap);
2448 aff = isl_aff_alloc(isl_local_space_domain(ls));
2451 if (isl_int_is_neg(bmap->eq[i][offset]))
2452 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
2454 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
2455 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
2456 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
2457 isl_basic_map_free(bmap);
2459 aff = isl_aff_remove_unused_divs(aff);
2460 aff = isl_aff_floor(aff);
2463 isl_basic_map_free(bmap);
2467 /* Given a basic map where each output dimension is defined
2468 * in terms of the parameters and input dimensions using an equality,
2469 * extract an isl_multi_aff that expresses the output dimensions in terms
2470 * of the parameters and input dimensions.
2472 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
2473 __isl_take isl_basic_map *bmap)
2482 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
2483 n_out = isl_basic_map_dim(bmap, isl_dim_out);
2485 for (i = 0; i < n_out; ++i) {
2486 isl_basic_map *bmap_i;
2489 bmap_i = isl_basic_map_copy(bmap);
2490 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
2491 i + 1, n_out - (1 + i));
2492 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
2493 aff = extract_isl_aff_from_basic_map(bmap_i);
2494 ma = isl_multi_aff_set_aff(ma, i, aff);
2497 isl_basic_map_free(bmap);
2502 /* Create an isl_pw_multi_aff that is equivalent to
2503 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2504 * The given basic map is such that each output dimension is defined
2505 * in terms of the parameters and input dimensions using an equality.
2507 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
2508 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
2512 ma = extract_isl_multi_aff_from_basic_map(bmap);
2513 return isl_pw_multi_aff_alloc(domain, ma);
2516 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2517 * This obivously only works if the input "map" is single-valued.
2518 * If so, we compute the lexicographic minimum of the image in the form
2519 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2520 * to its lexicographic minimum.
2521 * If the input is not single-valued, we produce an error.
2523 * As a special case, we first check if all output dimensions are uniquely
2524 * defined in terms of the parameters and input dimensions over the entire
2525 * domain. If so, we extract the desired isl_pw_multi_aff directly
2526 * from the affine hull of "map" and its domain.
2528 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2532 isl_pw_multi_aff *pma;
2533 isl_basic_map *hull;
2538 hull = isl_map_affine_hull(isl_map_copy(map));
2539 sv = isl_basic_map_plain_is_single_valued(hull);
2541 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
2542 isl_basic_map_free(hull);
2546 sv = isl_map_is_single_valued(map);
2550 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2551 "map is not single-valued", goto error);
2552 map = isl_map_make_disjoint(map);
2556 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2558 for (i = 0; i < map->n; ++i) {
2559 isl_pw_multi_aff *pma_i;
2560 isl_basic_map *bmap;
2561 bmap = isl_basic_map_copy(map->p[i]);
2562 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2563 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2573 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2575 return isl_pw_multi_aff_from_map(set);
2578 /* Return the piecewise affine expression "set ? 1 : 0".
2580 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
2583 isl_space *space = isl_set_get_space(set);
2584 isl_local_space *ls = isl_local_space_from_space(space);
2585 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
2586 isl_aff *one = isl_aff_zero_on_domain(ls);
2588 one = isl_aff_add_constant_si(one, 1);
2589 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
2590 set = isl_set_complement(set);
2591 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
2596 /* Plug in "subs" for dimension "type", "pos" of "aff".
2598 * Let i be the dimension to replace and let "subs" be of the form
2602 * and "aff" of the form
2608 * floor((a f + d g')/(m d))
2610 * where g' is the result of plugging in "subs" in each of the integer
2613 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2614 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2619 aff = isl_aff_cow(aff);
2621 return isl_aff_free(aff);
2623 ctx = isl_aff_get_ctx(aff);
2624 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2625 isl_die(ctx, isl_error_invalid,
2626 "spaces don't match", return isl_aff_free(aff));
2627 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2628 isl_die(ctx, isl_error_unsupported,
2629 "cannot handle divs yet", return isl_aff_free(aff));
2631 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2633 return isl_aff_free(aff);
2635 aff->v = isl_vec_cow(aff->v);
2637 return isl_aff_free(aff);
2639 pos += isl_local_space_offset(aff->ls, type);
2642 isl_int_set(v, aff->v->el[1 + pos]);
2643 isl_int_set_si(aff->v->el[1 + pos], 0);
2644 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2645 v, subs->v->el + 1, subs->v->size - 1);
2646 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2652 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2653 * expressions in "maff".
2655 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2656 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2657 __isl_keep isl_aff *subs)
2661 maff = isl_multi_aff_cow(maff);
2663 return isl_multi_aff_free(maff);
2665 if (type == isl_dim_in)
2668 for (i = 0; i < maff->n; ++i) {
2669 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2671 return isl_multi_aff_free(maff);
2677 /* Plug in "subs" for dimension "type", "pos" of "pma".
2679 * pma is of the form
2683 * while subs is of the form
2685 * v' = B_j(v) -> S_j
2687 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2688 * has a contribution in the result, in particular
2690 * C_ij(S_j) -> M_i(S_j)
2692 * Note that plugging in S_j in C_ij may also result in an empty set
2693 * and this contribution should simply be discarded.
2695 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2696 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2697 __isl_keep isl_pw_aff *subs)
2700 isl_pw_multi_aff *res;
2703 return isl_pw_multi_aff_free(pma);
2705 n = pma->n * subs->n;
2706 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2708 for (i = 0; i < pma->n; ++i) {
2709 for (j = 0; j < subs->n; ++j) {
2711 isl_multi_aff *res_ij;
2712 common = isl_set_intersect(
2713 isl_set_copy(pma->p[i].set),
2714 isl_set_copy(subs->p[j].set));
2715 common = isl_set_substitute(common,
2716 type, pos, subs->p[j].aff);
2717 if (isl_set_plain_is_empty(common)) {
2718 isl_set_free(common);
2722 res_ij = isl_multi_aff_substitute(
2723 isl_multi_aff_copy(pma->p[i].maff),
2724 type, pos, subs->p[j].aff);
2726 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2730 isl_pw_multi_aff_free(pma);
2734 /* Extend the local space of "dst" to include the divs
2735 * in the local space of "src".
2737 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2738 __isl_keep isl_aff *src)
2746 return isl_aff_free(dst);
2748 ctx = isl_aff_get_ctx(src);
2749 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2750 isl_die(ctx, isl_error_invalid,
2751 "spaces don't match", goto error);
2753 if (src->ls->div->n_row == 0)
2756 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2757 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2761 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2762 dst = isl_aff_expand_divs(dst, div, exp2);
2770 return isl_aff_free(dst);
2773 /* Adjust the local spaces of the affine expressions in "maff"
2774 * such that they all have the save divs.
2776 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2777 __isl_take isl_multi_aff *maff)
2785 maff = isl_multi_aff_cow(maff);
2789 for (i = 1; i < maff->n; ++i)
2790 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2791 for (i = 1; i < maff->n; ++i) {
2792 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2794 return isl_multi_aff_free(maff);
2800 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2802 aff = isl_aff_cow(aff);
2806 aff->ls = isl_local_space_lift(aff->ls);
2808 return isl_aff_free(aff);
2813 /* Lift "maff" to a space with extra dimensions such that the result
2814 * has no more existentially quantified variables.
2815 * If "ls" is not NULL, then *ls is assigned the local space that lies
2816 * at the basis of the lifting applied to "maff".
2818 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2819 __isl_give isl_local_space **ls)
2833 isl_space *space = isl_multi_aff_get_domain_space(maff);
2834 *ls = isl_local_space_from_space(space);
2836 return isl_multi_aff_free(maff);
2841 maff = isl_multi_aff_cow(maff);
2842 maff = isl_multi_aff_align_divs(maff);
2846 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2847 space = isl_multi_aff_get_space(maff);
2848 space = isl_space_lift(isl_space_domain(space), n_div);
2849 space = isl_space_extend_domain_with_range(space,
2850 isl_multi_aff_get_space(maff));
2852 return isl_multi_aff_free(maff);
2853 isl_space_free(maff->space);
2854 maff->space = space;
2857 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2859 return isl_multi_aff_free(maff);
2862 for (i = 0; i < maff->n; ++i) {
2863 maff->p[i] = isl_aff_lift(maff->p[i]);
2871 isl_local_space_free(*ls);
2872 return isl_multi_aff_free(maff);
2876 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2878 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2879 __isl_keep isl_pw_multi_aff *pma, int pos)
2889 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2890 if (pos < 0 || pos >= n_out)
2891 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2892 "index out of bounds", return NULL);
2894 space = isl_pw_multi_aff_get_space(pma);
2895 space = isl_space_drop_dims(space, isl_dim_out,
2896 pos + 1, n_out - pos - 1);
2897 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2899 pa = isl_pw_aff_alloc_size(space, pma->n);
2900 for (i = 0; i < pma->n; ++i) {
2902 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2903 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
2909 /* Return an isl_pw_multi_aff with the given "set" as domain and
2910 * an unnamed zero-dimensional range.
2912 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
2913 __isl_take isl_set *set)
2918 space = isl_set_get_space(set);
2919 space = isl_space_from_domain(space);
2920 ma = isl_multi_aff_zero(space);
2921 return isl_pw_multi_aff_alloc(set, ma);
2924 /* Add an isl_pw_multi_aff with the given "set" as domain and
2925 * an unnamed zero-dimensional range to *user.
2927 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
2929 isl_union_pw_multi_aff **upma = user;
2930 isl_pw_multi_aff *pma;
2932 pma = isl_pw_multi_aff_from_domain(set);
2933 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
2938 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
2939 * an unnamed zero-dimensional range.
2941 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
2942 __isl_take isl_union_set *uset)
2945 isl_union_pw_multi_aff *upma;
2950 space = isl_union_set_get_space(uset);
2951 upma = isl_union_pw_multi_aff_empty(space);
2953 if (isl_union_set_foreach_set(uset,
2954 &add_pw_multi_aff_from_domain, &upma) < 0)
2957 isl_union_set_free(uset);
2960 isl_union_set_free(uset);
2961 isl_union_pw_multi_aff_free(upma);
2965 /* Convert "pma" to an isl_map and add it to *umap.
2967 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
2969 isl_union_map **umap = user;
2972 map = isl_map_from_pw_multi_aff(pma);
2973 *umap = isl_union_map_add_map(*umap, map);
2978 /* Construct a union map mapping the domain of the union
2979 * piecewise multi-affine expression to its range, with each dimension
2980 * in the range equated to the corresponding affine expression on its cell.
2982 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
2983 __isl_take isl_union_pw_multi_aff *upma)
2986 isl_union_map *umap;
2991 space = isl_union_pw_multi_aff_get_space(upma);
2992 umap = isl_union_map_empty(space);
2994 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2995 &map_from_pw_multi_aff, &umap) < 0)
2998 isl_union_pw_multi_aff_free(upma);
3001 isl_union_pw_multi_aff_free(upma);
3002 isl_union_map_free(umap);
3006 /* Local data for bin_entry and the callback "fn".
3008 struct isl_union_pw_multi_aff_bin_data {
3009 isl_union_pw_multi_aff *upma2;
3010 isl_union_pw_multi_aff *res;
3011 isl_pw_multi_aff *pma;
3012 int (*fn)(void **entry, void *user);
3015 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
3016 * and call data->fn for each isl_pw_multi_aff in data->upma2.
3018 static int bin_entry(void **entry, void *user)
3020 struct isl_union_pw_multi_aff_bin_data *data = user;
3021 isl_pw_multi_aff *pma = *entry;
3024 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
3025 data->fn, data) < 0)
3031 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
3032 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
3033 * passed as user field) and the isl_pw_multi_aff from upma2 is available
3034 * as *entry. The callback should adjust data->res if desired.
3036 static __isl_give isl_union_pw_multi_aff *bin_op(
3037 __isl_take isl_union_pw_multi_aff *upma1,
3038 __isl_take isl_union_pw_multi_aff *upma2,
3039 int (*fn)(void **entry, void *user))
3042 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
3044 space = isl_union_pw_multi_aff_get_space(upma2);
3045 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
3046 space = isl_union_pw_multi_aff_get_space(upma1);
3047 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
3049 if (!upma1 || !upma2)
3053 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
3055 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
3056 &bin_entry, &data) < 0)
3059 isl_union_pw_multi_aff_free(upma1);
3060 isl_union_pw_multi_aff_free(upma2);
3063 isl_union_pw_multi_aff_free(upma1);
3064 isl_union_pw_multi_aff_free(upma2);
3065 isl_union_pw_multi_aff_free(data.res);
3069 /* Given two isl_multi_affs A -> B and C -> D,
3070 * construct an isl_multi_aff (A * C) -> (B, D).
3072 __isl_give isl_multi_aff *isl_multi_aff_flat_range_product(
3073 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3083 space = isl_space_range_product(isl_multi_aff_get_space(ma1),
3084 isl_multi_aff_get_space(ma2));
3085 space = isl_space_flatten_range(space);
3086 res = isl_multi_aff_alloc(space);
3088 n1 = isl_multi_aff_dim(ma1, isl_dim_out);
3089 n2 = isl_multi_aff_dim(ma2, isl_dim_out);
3091 for (i = 0; i < n1; ++i) {
3092 aff = isl_multi_aff_get_aff(ma1, i);
3093 res = isl_multi_aff_set_aff(res, i, aff);
3096 for (i = 0; i < n2; ++i) {
3097 aff = isl_multi_aff_get_aff(ma2, i);
3098 res = isl_multi_aff_set_aff(res, n1 + i, aff);
3101 isl_multi_aff_free(ma1);
3102 isl_multi_aff_free(ma2);
3105 isl_multi_aff_free(ma1);
3106 isl_multi_aff_free(ma2);
3110 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
3111 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3113 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
3114 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3118 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
3119 isl_pw_multi_aff_get_space(pma2));
3120 space = isl_space_flatten_range(space);
3121 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
3122 &isl_multi_aff_flat_range_product);
3125 /* Given two isl_pw_multi_affs A -> B and C -> D,
3126 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3128 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
3129 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3131 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3132 &pw_multi_aff_flat_range_product);
3135 /* If data->pma and *entry have the same domain space, then compute
3136 * their flat range product and the result to data->res.
3138 static int flat_range_product_entry(void **entry, void *user)
3140 struct isl_union_pw_multi_aff_bin_data *data = user;
3141 isl_pw_multi_aff *pma2 = *entry;
3143 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
3144 pma2->dim, isl_dim_in))
3147 pma2 = isl_pw_multi_aff_flat_range_product(
3148 isl_pw_multi_aff_copy(data->pma),
3149 isl_pw_multi_aff_copy(pma2));
3151 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
3156 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
3157 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
3159 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
3160 __isl_take isl_union_pw_multi_aff *upma1,
3161 __isl_take isl_union_pw_multi_aff *upma2)
3163 return bin_op(upma1, upma2, &flat_range_product_entry);