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 /* Add "v" to the numerator of the constant term of "aff".
438 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
440 if (isl_int_is_zero(v))
443 aff = isl_aff_cow(aff);
447 aff->v = isl_vec_cow(aff->v);
449 return isl_aff_free(aff);
451 isl_int_add(aff->v->el[1], aff->v->el[1], v);
456 /* Add "v" to the numerator of the constant term of "aff".
458 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
466 isl_int_set_si(t, v);
467 aff = isl_aff_add_constant_num(aff, t);
473 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
475 aff = isl_aff_cow(aff);
479 aff->v = isl_vec_cow(aff->v);
481 return isl_aff_free(aff);
483 isl_int_set_si(aff->v->el[1], v);
488 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
489 enum isl_dim_type type, int pos, isl_int v)
494 if (type == isl_dim_out)
495 isl_die(aff->v->ctx, isl_error_invalid,
496 "output/set dimension does not have a coefficient",
497 return isl_aff_free(aff));
498 if (type == isl_dim_in)
501 if (pos >= isl_local_space_dim(aff->ls, type))
502 isl_die(aff->v->ctx, isl_error_invalid,
503 "position out of bounds", return isl_aff_free(aff));
505 aff = isl_aff_cow(aff);
509 aff->v = isl_vec_cow(aff->v);
511 return isl_aff_free(aff);
513 pos += isl_local_space_offset(aff->ls, type);
514 isl_int_set(aff->v->el[1 + pos], v);
519 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
520 enum isl_dim_type type, int pos, int v)
525 if (type == isl_dim_out)
526 isl_die(aff->v->ctx, isl_error_invalid,
527 "output/set dimension does not have a coefficient",
528 return isl_aff_free(aff));
529 if (type == isl_dim_in)
532 if (pos >= isl_local_space_dim(aff->ls, type))
533 isl_die(aff->v->ctx, isl_error_invalid,
534 "position out of bounds", return isl_aff_free(aff));
536 aff = isl_aff_cow(aff);
540 aff->v = isl_vec_cow(aff->v);
542 return isl_aff_free(aff);
544 pos += isl_local_space_offset(aff->ls, type);
545 isl_int_set_si(aff->v->el[1 + pos], v);
550 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
551 enum isl_dim_type type, int pos, isl_int v)
556 if (type == isl_dim_out)
557 isl_die(aff->v->ctx, isl_error_invalid,
558 "output/set dimension does not have a coefficient",
559 return isl_aff_free(aff));
560 if (type == isl_dim_in)
563 if (pos >= isl_local_space_dim(aff->ls, type))
564 isl_die(aff->v->ctx, isl_error_invalid,
565 "position out of bounds", return isl_aff_free(aff));
567 aff = isl_aff_cow(aff);
571 aff->v = isl_vec_cow(aff->v);
573 return isl_aff_free(aff);
575 pos += isl_local_space_offset(aff->ls, type);
576 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
581 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
582 enum isl_dim_type type, int pos, int v)
587 isl_int_set_si(t, v);
588 aff = isl_aff_add_coefficient(aff, type, pos, t);
594 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
599 return isl_local_space_get_div(aff->ls, pos);
602 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
604 aff = isl_aff_cow(aff);
607 aff->v = isl_vec_cow(aff->v);
609 return isl_aff_free(aff);
611 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
616 /* Remove divs from the local space that do not appear in the affine
618 * We currently only remove divs at the end.
619 * Some intermediate divs may also not appear directly in the affine
620 * expression, but we would also need to check that no other divs are
621 * defined in terms of them.
623 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
632 n = isl_local_space_dim(aff->ls, isl_dim_div);
633 off = isl_local_space_offset(aff->ls, isl_dim_div);
635 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
639 aff = isl_aff_cow(aff);
643 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
644 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
645 if (!aff->ls || !aff->v)
646 return isl_aff_free(aff);
651 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
655 aff->v = isl_vec_normalize(aff->v);
657 return isl_aff_free(aff);
658 aff = isl_aff_remove_unused_divs(aff);
662 /* Given f, return floor(f).
663 * If f is an integer expression, then just return f.
664 * If f is a constant, then return the constant floor(f).
665 * Otherwise, if f = g/m, write g = q m + r,
666 * create a new div d = [r/m] and return the expression q + d.
667 * The coefficients in r are taken to lie between -m/2 and m/2.
669 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
679 if (isl_int_is_one(aff->v->el[0]))
682 aff = isl_aff_cow(aff);
686 aff->v = isl_vec_cow(aff->v);
688 return isl_aff_free(aff);
690 if (isl_aff_is_cst(aff)) {
691 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
692 isl_int_set_si(aff->v->el[0], 1);
696 div = isl_vec_copy(aff->v);
697 div = isl_vec_cow(div);
699 return isl_aff_free(aff);
701 ctx = isl_aff_get_ctx(aff);
702 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
703 for (i = 1; i < aff->v->size; ++i) {
704 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
705 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
706 if (isl_int_gt(div->el[i], aff->v->el[0])) {
707 isl_int_sub(div->el[i], div->el[i], div->el[0]);
708 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
712 aff->ls = isl_local_space_add_div(aff->ls, div);
714 return isl_aff_free(aff);
717 aff->v = isl_vec_extend(aff->v, size + 1);
719 return isl_aff_free(aff);
720 isl_int_set_si(aff->v->el[0], 1);
721 isl_int_set_si(aff->v->el[size], 1);
728 * aff mod m = aff - m * floor(aff/m)
730 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
734 res = isl_aff_copy(aff);
735 aff = isl_aff_scale_down(aff, m);
736 aff = isl_aff_floor(aff);
737 aff = isl_aff_scale(aff, m);
738 res = isl_aff_sub(res, aff);
745 * pwaff mod m = pwaff - m * floor(pwaff/m)
747 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
751 res = isl_pw_aff_copy(pwaff);
752 pwaff = isl_pw_aff_scale_down(pwaff, m);
753 pwaff = isl_pw_aff_floor(pwaff);
754 pwaff = isl_pw_aff_scale(pwaff, m);
755 res = isl_pw_aff_sub(res, pwaff);
760 /* Given f, return ceil(f).
761 * If f is an integer expression, then just return f.
762 * Otherwise, create a new div d = [-f] and return the expression -d.
764 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
769 if (isl_int_is_one(aff->v->el[0]))
772 aff = isl_aff_neg(aff);
773 aff = isl_aff_floor(aff);
774 aff = isl_aff_neg(aff);
779 /* Apply the expansion computed by isl_merge_divs.
780 * The expansion itself is given by "exp" while the resulting
781 * list of divs is given by "div".
783 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
784 __isl_take isl_mat *div, int *exp)
791 aff = isl_aff_cow(aff);
795 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
796 new_n_div = isl_mat_rows(div);
797 if (new_n_div < old_n_div)
798 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
799 "not an expansion", goto error);
801 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
805 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
807 for (i = new_n_div - 1; i >= 0; --i) {
808 if (j >= 0 && exp[j] == i) {
810 isl_int_swap(aff->v->el[offset + i],
811 aff->v->el[offset + j]);
814 isl_int_set_si(aff->v->el[offset + i], 0);
817 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
828 /* Add two affine expressions that live in the same local space.
830 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
831 __isl_take isl_aff *aff2)
835 aff1 = isl_aff_cow(aff1);
839 aff1->v = isl_vec_cow(aff1->v);
845 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
846 isl_int_divexact(f, aff2->v->el[0], gcd);
847 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
848 isl_int_divexact(f, aff1->v->el[0], gcd);
849 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
850 isl_int_divexact(f, aff2->v->el[0], gcd);
851 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
863 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
864 __isl_take isl_aff *aff2)
874 ctx = isl_aff_get_ctx(aff1);
875 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
876 isl_die(ctx, isl_error_invalid,
877 "spaces don't match", goto error);
879 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
880 return add_expanded(aff1, aff2);
882 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
883 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
887 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
888 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
889 aff2 = isl_aff_expand_divs(aff2, div, exp2);
893 return add_expanded(aff1, aff2);
902 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
903 __isl_take isl_aff *aff2)
905 return isl_aff_add(aff1, isl_aff_neg(aff2));
908 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
912 if (isl_int_is_one(f))
915 aff = isl_aff_cow(aff);
918 aff->v = isl_vec_cow(aff->v);
920 return isl_aff_free(aff);
923 isl_int_gcd(gcd, aff->v->el[0], f);
924 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
925 isl_int_divexact(gcd, f, gcd);
926 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
932 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
936 if (isl_int_is_one(f))
939 aff = isl_aff_cow(aff);
942 aff->v = isl_vec_cow(aff->v);
944 return isl_aff_free(aff);
947 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
948 isl_int_gcd(gcd, gcd, f);
949 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
950 isl_int_divexact(gcd, f, gcd);
951 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
957 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
965 isl_int_set_ui(v, f);
966 aff = isl_aff_scale_down(aff, v);
972 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
973 enum isl_dim_type type, unsigned pos, const char *s)
975 aff = isl_aff_cow(aff);
978 if (type == isl_dim_out)
979 isl_die(aff->v->ctx, isl_error_invalid,
980 "cannot set name of output/set dimension",
981 return isl_aff_free(aff));
982 if (type == isl_dim_in)
984 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
986 return isl_aff_free(aff);
991 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
992 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
994 aff = isl_aff_cow(aff);
996 return isl_id_free(id);
997 if (type == isl_dim_out)
998 isl_die(aff->v->ctx, isl_error_invalid,
999 "cannot set name of output/set dimension",
1001 if (type == isl_dim_in)
1003 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1005 return isl_aff_free(aff);
1014 /* Exploit the equalities in "eq" to simplify the affine expression
1015 * and the expressions of the integer divisions in the local space.
1016 * The integer divisions in this local space are assumed to appear
1017 * as regular dimensions in "eq".
1019 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1020 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1028 if (eq->n_eq == 0) {
1029 isl_basic_set_free(eq);
1033 aff = isl_aff_cow(aff);
1037 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1038 isl_basic_set_copy(eq));
1042 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1044 for (i = 0; i < eq->n_eq; ++i) {
1045 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1046 if (j < 0 || j == 0 || j >= total)
1049 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1053 isl_basic_set_free(eq);
1054 aff = isl_aff_normalize(aff);
1057 isl_basic_set_free(eq);
1062 /* Exploit the equalities in "eq" to simplify the affine expression
1063 * and the expressions of the integer divisions in the local space.
1065 static __isl_give isl_aff *isl_aff_substitute_equalities(
1066 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1072 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1074 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
1075 return isl_aff_substitute_equalities_lifted(aff, eq);
1077 isl_basic_set_free(eq);
1082 /* Look for equalities among the variables shared by context and aff
1083 * and the integer divisions of aff, if any.
1084 * The equalities are then used to eliminate coefficients and/or integer
1085 * divisions from aff.
1087 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1088 __isl_take isl_set *context)
1090 isl_basic_set *hull;
1095 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1097 isl_basic_set *bset;
1098 isl_local_space *ls;
1099 context = isl_set_add_dims(context, isl_dim_set, n_div);
1100 ls = isl_aff_get_domain_local_space(aff);
1101 bset = isl_basic_set_from_local_space(ls);
1102 bset = isl_basic_set_lift(bset);
1103 bset = isl_basic_set_flatten(bset);
1104 context = isl_set_intersect(context,
1105 isl_set_from_basic_set(bset));
1108 hull = isl_set_affine_hull(context);
1109 return isl_aff_substitute_equalities_lifted(aff, hull);
1112 isl_set_free(context);
1116 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1117 __isl_take isl_set *context)
1119 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1120 dom_context = isl_set_intersect_params(dom_context, context);
1121 return isl_aff_gist(aff, dom_context);
1124 /* Return a basic set containing those elements in the space
1125 * of aff where it is non-negative.
1127 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1129 isl_constraint *ineq;
1130 isl_basic_set *bset;
1132 ineq = isl_inequality_from_aff(aff);
1134 bset = isl_basic_set_from_constraint(ineq);
1135 bset = isl_basic_set_simplify(bset);
1139 /* Return a basic set containing those elements in the space
1140 * of aff where it is zero.
1142 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1144 isl_constraint *ineq;
1145 isl_basic_set *bset;
1147 ineq = isl_equality_from_aff(aff);
1149 bset = isl_basic_set_from_constraint(ineq);
1150 bset = isl_basic_set_simplify(bset);
1154 /* Return a basic set containing those elements in the shared space
1155 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1157 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1158 __isl_take isl_aff *aff2)
1160 aff1 = isl_aff_sub(aff1, aff2);
1162 return isl_aff_nonneg_basic_set(aff1);
1165 /* Return a basic set containing those elements in the shared space
1166 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1168 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1169 __isl_take isl_aff *aff2)
1171 return isl_aff_ge_basic_set(aff2, aff1);
1174 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1175 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1177 aff1 = isl_aff_add(aff1, aff2);
1178 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1182 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1190 /* Check whether the given affine expression has non-zero coefficient
1191 * for any dimension in the given range or if any of these dimensions
1192 * appear with non-zero coefficients in any of the integer divisions
1193 * involved in the affine expression.
1195 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1196 enum isl_dim_type type, unsigned first, unsigned n)
1208 ctx = isl_aff_get_ctx(aff);
1209 if (first + n > isl_aff_dim(aff, type))
1210 isl_die(ctx, isl_error_invalid,
1211 "range out of bounds", return -1);
1213 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1217 first += isl_local_space_offset(aff->ls, type) - 1;
1218 for (i = 0; i < n; ++i)
1219 if (active[first + i]) {
1232 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1233 enum isl_dim_type type, unsigned first, unsigned n)
1239 if (type == isl_dim_out)
1240 isl_die(aff->v->ctx, isl_error_invalid,
1241 "cannot drop output/set dimension",
1242 return isl_aff_free(aff));
1243 if (type == isl_dim_in)
1245 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1248 ctx = isl_aff_get_ctx(aff);
1249 if (first + n > isl_local_space_dim(aff->ls, type))
1250 isl_die(ctx, isl_error_invalid, "range out of bounds",
1251 return isl_aff_free(aff));
1253 aff = isl_aff_cow(aff);
1257 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1259 return isl_aff_free(aff);
1261 first += 1 + isl_local_space_offset(aff->ls, type);
1262 aff->v = isl_vec_drop_els(aff->v, first, n);
1264 return isl_aff_free(aff);
1269 /* Project the domain of the affine expression onto its parameter space.
1270 * The affine expression may not involve any of the domain dimensions.
1272 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1278 n = isl_aff_dim(aff, isl_dim_in);
1279 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1281 return isl_aff_free(aff);
1283 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1284 "affine expression involves some of the domain dimensions",
1285 return isl_aff_free(aff));
1286 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1287 space = isl_aff_get_domain_space(aff);
1288 space = isl_space_params(space);
1289 aff = isl_aff_reset_domain_space(aff, space);
1293 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1294 enum isl_dim_type type, unsigned first, unsigned n)
1300 if (type == isl_dim_out)
1301 isl_die(aff->v->ctx, isl_error_invalid,
1302 "cannot insert output/set dimensions",
1303 return isl_aff_free(aff));
1304 if (type == isl_dim_in)
1306 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1309 ctx = isl_aff_get_ctx(aff);
1310 if (first > isl_local_space_dim(aff->ls, type))
1311 isl_die(ctx, isl_error_invalid, "position out of bounds",
1312 return isl_aff_free(aff));
1314 aff = isl_aff_cow(aff);
1318 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1320 return isl_aff_free(aff);
1322 first += 1 + isl_local_space_offset(aff->ls, type);
1323 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1325 return isl_aff_free(aff);
1330 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1331 enum isl_dim_type type, unsigned n)
1335 pos = isl_aff_dim(aff, type);
1337 return isl_aff_insert_dims(aff, type, pos, n);
1340 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1341 enum isl_dim_type type, unsigned n)
1345 pos = isl_pw_aff_dim(pwaff, type);
1347 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1350 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1352 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1353 return isl_pw_aff_alloc(dom, aff);
1357 #define PW isl_pw_aff
1361 #define EL_IS_ZERO is_empty
1365 #define IS_ZERO is_empty
1368 #undef DEFAULT_IS_ZERO
1369 #define DEFAULT_IS_ZERO 0
1373 #define NO_MOVE_DIMS
1377 #include <isl_pw_templ.c>
1379 static __isl_give isl_set *align_params_pw_pw_set_and(
1380 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1381 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1382 __isl_take isl_pw_aff *pwaff2))
1384 if (!pwaff1 || !pwaff2)
1386 if (isl_space_match(pwaff1->dim, isl_dim_param,
1387 pwaff2->dim, isl_dim_param))
1388 return fn(pwaff1, pwaff2);
1389 if (!isl_space_has_named_params(pwaff1->dim) ||
1390 !isl_space_has_named_params(pwaff2->dim))
1391 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1392 "unaligned unnamed parameters", goto error);
1393 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1394 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1395 return fn(pwaff1, pwaff2);
1397 isl_pw_aff_free(pwaff1);
1398 isl_pw_aff_free(pwaff2);
1402 /* Compute a piecewise quasi-affine expression with a domain that
1403 * is the union of those of pwaff1 and pwaff2 and such that on each
1404 * cell, the quasi-affine expression is the better (according to cmp)
1405 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1406 * is defined on a given cell, then the associated expression
1407 * is the defined one.
1409 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1410 __isl_take isl_pw_aff *pwaff2,
1411 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1412 __isl_take isl_aff *aff2))
1419 if (!pwaff1 || !pwaff2)
1422 ctx = isl_space_get_ctx(pwaff1->dim);
1423 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1424 isl_die(ctx, isl_error_invalid,
1425 "arguments should live in same space", goto error);
1427 if (isl_pw_aff_is_empty(pwaff1)) {
1428 isl_pw_aff_free(pwaff1);
1432 if (isl_pw_aff_is_empty(pwaff2)) {
1433 isl_pw_aff_free(pwaff2);
1437 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1438 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1440 for (i = 0; i < pwaff1->n; ++i) {
1441 set = isl_set_copy(pwaff1->p[i].set);
1442 for (j = 0; j < pwaff2->n; ++j) {
1443 struct isl_set *common;
1446 common = isl_set_intersect(
1447 isl_set_copy(pwaff1->p[i].set),
1448 isl_set_copy(pwaff2->p[j].set));
1449 better = isl_set_from_basic_set(cmp(
1450 isl_aff_copy(pwaff2->p[j].aff),
1451 isl_aff_copy(pwaff1->p[i].aff)));
1452 better = isl_set_intersect(common, better);
1453 if (isl_set_plain_is_empty(better)) {
1454 isl_set_free(better);
1457 set = isl_set_subtract(set, isl_set_copy(better));
1459 res = isl_pw_aff_add_piece(res, better,
1460 isl_aff_copy(pwaff2->p[j].aff));
1462 res = isl_pw_aff_add_piece(res, set,
1463 isl_aff_copy(pwaff1->p[i].aff));
1466 for (j = 0; j < pwaff2->n; ++j) {
1467 set = isl_set_copy(pwaff2->p[j].set);
1468 for (i = 0; i < pwaff1->n; ++i)
1469 set = isl_set_subtract(set,
1470 isl_set_copy(pwaff1->p[i].set));
1471 res = isl_pw_aff_add_piece(res, set,
1472 isl_aff_copy(pwaff2->p[j].aff));
1475 isl_pw_aff_free(pwaff1);
1476 isl_pw_aff_free(pwaff2);
1480 isl_pw_aff_free(pwaff1);
1481 isl_pw_aff_free(pwaff2);
1485 /* Compute a piecewise quasi-affine expression with a domain that
1486 * is the union of those of pwaff1 and pwaff2 and such that on each
1487 * cell, the quasi-affine expression is the maximum of those of pwaff1
1488 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1489 * cell, then the associated expression is the defined one.
1491 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1492 __isl_take isl_pw_aff *pwaff2)
1494 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1497 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1498 __isl_take isl_pw_aff *pwaff2)
1500 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1504 /* Compute a piecewise quasi-affine expression with a domain that
1505 * is the union of those of pwaff1 and pwaff2 and such that on each
1506 * cell, the quasi-affine expression is the minimum of those of pwaff1
1507 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1508 * cell, then the associated expression is the defined one.
1510 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1511 __isl_take isl_pw_aff *pwaff2)
1513 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1516 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1517 __isl_take isl_pw_aff *pwaff2)
1519 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1523 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1524 __isl_take isl_pw_aff *pwaff2, int max)
1527 return isl_pw_aff_union_max(pwaff1, pwaff2);
1529 return isl_pw_aff_union_min(pwaff1, pwaff2);
1532 /* Construct a map with as domain the domain of pwaff and
1533 * one-dimensional range corresponding to the affine expressions.
1535 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1544 dim = isl_pw_aff_get_space(pwaff);
1545 map = isl_map_empty(dim);
1547 for (i = 0; i < pwaff->n; ++i) {
1548 isl_basic_map *bmap;
1551 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1552 map_i = isl_map_from_basic_map(bmap);
1553 map_i = isl_map_intersect_domain(map_i,
1554 isl_set_copy(pwaff->p[i].set));
1555 map = isl_map_union_disjoint(map, map_i);
1558 isl_pw_aff_free(pwaff);
1563 /* Construct a map with as domain the domain of pwaff and
1564 * one-dimensional range corresponding to the affine expressions.
1566 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1570 if (isl_space_is_set(pwaff->dim))
1571 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1572 "space of input is not a map",
1573 return isl_pw_aff_free(pwaff));
1574 return map_from_pw_aff(pwaff);
1577 /* Construct a one-dimensional set with as parameter domain
1578 * the domain of pwaff and the single set dimension
1579 * corresponding to the affine expressions.
1581 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1585 if (!isl_space_is_set(pwaff->dim))
1586 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1587 "space of input is not a set",
1588 return isl_pw_aff_free(pwaff));
1589 return map_from_pw_aff(pwaff);
1592 /* Return a set containing those elements in the domain
1593 * of pwaff where it is non-negative.
1595 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1603 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1605 for (i = 0; i < pwaff->n; ++i) {
1606 isl_basic_set *bset;
1609 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1610 set_i = isl_set_from_basic_set(bset);
1611 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1612 set = isl_set_union_disjoint(set, set_i);
1615 isl_pw_aff_free(pwaff);
1620 /* Return a set containing those elements in the domain
1621 * of pwaff where it is zero (if complement is 0) or not zero
1622 * (if complement is 1).
1624 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1633 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1635 for (i = 0; i < pwaff->n; ++i) {
1636 isl_basic_set *bset;
1637 isl_set *set_i, *zero;
1639 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1640 zero = isl_set_from_basic_set(bset);
1641 set_i = isl_set_copy(pwaff->p[i].set);
1643 set_i = isl_set_subtract(set_i, zero);
1645 set_i = isl_set_intersect(set_i, zero);
1646 set = isl_set_union_disjoint(set, set_i);
1649 isl_pw_aff_free(pwaff);
1654 /* Return a set containing those elements in the domain
1655 * of pwaff where it is zero.
1657 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1659 return pw_aff_zero_set(pwaff, 0);
1662 /* Return a set containing those elements in the domain
1663 * of pwaff where it is not zero.
1665 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1667 return pw_aff_zero_set(pwaff, 1);
1670 /* Return a set containing those elements in the shared domain
1671 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1673 * We compute the difference on the shared domain and then construct
1674 * the set of values where this difference is non-negative.
1675 * If strict is set, we first subtract 1 from the difference.
1676 * If equal is set, we only return the elements where pwaff1 and pwaff2
1679 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1680 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1682 isl_set *set1, *set2;
1684 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1685 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1686 set1 = isl_set_intersect(set1, set2);
1687 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1688 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1689 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1692 isl_space *dim = isl_set_get_space(set1);
1694 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1695 aff = isl_aff_add_constant_si(aff, -1);
1696 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1701 return isl_pw_aff_zero_set(pwaff1);
1702 return isl_pw_aff_nonneg_set(pwaff1);
1705 /* Return a set containing those elements in the shared domain
1706 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1708 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1709 __isl_take isl_pw_aff *pwaff2)
1711 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1714 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1715 __isl_take isl_pw_aff *pwaff2)
1717 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1720 /* Return a set containing those elements in the shared domain
1721 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1723 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1724 __isl_take isl_pw_aff *pwaff2)
1726 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1729 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1730 __isl_take isl_pw_aff *pwaff2)
1732 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1735 /* Return a set containing those elements in the shared domain
1736 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1738 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1739 __isl_take isl_pw_aff *pwaff2)
1741 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1744 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1745 __isl_take isl_pw_aff *pwaff2)
1747 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1750 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1751 __isl_take isl_pw_aff *pwaff2)
1753 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1756 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1757 __isl_take isl_pw_aff *pwaff2)
1759 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1762 /* Return a set containing those elements in the shared domain
1763 * of the elements of list1 and list2 where each element in list1
1764 * has the relation specified by "fn" with each element in list2.
1766 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1767 __isl_take isl_pw_aff_list *list2,
1768 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1769 __isl_take isl_pw_aff *pwaff2))
1775 if (!list1 || !list2)
1778 ctx = isl_pw_aff_list_get_ctx(list1);
1779 if (list1->n < 1 || list2->n < 1)
1780 isl_die(ctx, isl_error_invalid,
1781 "list should contain at least one element", goto error);
1783 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1784 for (i = 0; i < list1->n; ++i)
1785 for (j = 0; j < list2->n; ++j) {
1788 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1789 isl_pw_aff_copy(list2->p[j]));
1790 set = isl_set_intersect(set, set_ij);
1793 isl_pw_aff_list_free(list1);
1794 isl_pw_aff_list_free(list2);
1797 isl_pw_aff_list_free(list1);
1798 isl_pw_aff_list_free(list2);
1802 /* Return a set containing those elements in the shared domain
1803 * of the elements of list1 and list2 where each element in list1
1804 * is equal to each element in list2.
1806 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1807 __isl_take isl_pw_aff_list *list2)
1809 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1812 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1813 __isl_take isl_pw_aff_list *list2)
1815 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1818 /* Return a set containing those elements in the shared domain
1819 * of the elements of list1 and list2 where each element in list1
1820 * is less than or equal to each element in list2.
1822 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1823 __isl_take isl_pw_aff_list *list2)
1825 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1828 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1829 __isl_take isl_pw_aff_list *list2)
1831 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1834 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1835 __isl_take isl_pw_aff_list *list2)
1837 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1840 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1841 __isl_take isl_pw_aff_list *list2)
1843 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1847 /* Return a set containing those elements in the shared domain
1848 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1850 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1851 __isl_take isl_pw_aff *pwaff2)
1853 isl_set *set_lt, *set_gt;
1855 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1856 isl_pw_aff_copy(pwaff2));
1857 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1858 return isl_set_union_disjoint(set_lt, set_gt);
1861 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1862 __isl_take isl_pw_aff *pwaff2)
1864 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1867 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1872 if (isl_int_is_one(v))
1874 if (!isl_int_is_pos(v))
1875 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1876 "factor needs to be positive",
1877 return isl_pw_aff_free(pwaff));
1878 pwaff = isl_pw_aff_cow(pwaff);
1884 for (i = 0; i < pwaff->n; ++i) {
1885 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1886 if (!pwaff->p[i].aff)
1887 return isl_pw_aff_free(pwaff);
1893 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1897 pwaff = isl_pw_aff_cow(pwaff);
1903 for (i = 0; i < pwaff->n; ++i) {
1904 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1905 if (!pwaff->p[i].aff)
1906 return isl_pw_aff_free(pwaff);
1912 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1916 pwaff = isl_pw_aff_cow(pwaff);
1922 for (i = 0; i < pwaff->n; ++i) {
1923 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1924 if (!pwaff->p[i].aff)
1925 return isl_pw_aff_free(pwaff);
1931 /* Assuming that "cond1" and "cond2" are disjoint,
1932 * return an affine expression that is equal to pwaff1 on cond1
1933 * and to pwaff2 on cond2.
1935 static __isl_give isl_pw_aff *isl_pw_aff_select(
1936 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
1937 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
1939 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
1940 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
1942 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
1945 /* Return an affine expression that is equal to pwaff_true for elements
1946 * where "cond" is non-zero and to pwaff_false for elements where "cond"
1948 * That is, return cond ? pwaff_true : pwaff_false;
1950 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
1951 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1953 isl_set *cond_true, *cond_false;
1955 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1956 cond_false = isl_pw_aff_zero_set(cond);
1957 return isl_pw_aff_select(cond_true, pwaff_true,
1958 cond_false, pwaff_false);
1961 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1966 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1969 /* Check whether pwaff is a piecewise constant.
1971 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1978 for (i = 0; i < pwaff->n; ++i) {
1979 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1980 if (is_cst < 0 || !is_cst)
1987 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1988 __isl_take isl_aff *aff2)
1990 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1991 return isl_aff_mul(aff2, aff1);
1993 if (!isl_aff_is_cst(aff2))
1994 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1995 "at least one affine expression should be constant",
1998 aff1 = isl_aff_cow(aff1);
2002 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2003 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2013 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2014 __isl_take isl_pw_aff *pwaff2)
2016 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2019 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2020 __isl_take isl_pw_aff *pwaff2)
2022 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2025 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2026 __isl_take isl_pw_aff *pwaff2)
2028 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2031 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2032 __isl_take isl_pw_aff *pwaff2)
2034 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2037 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2038 __isl_take isl_pw_aff *pwaff2)
2040 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2043 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2044 __isl_take isl_pw_aff *pwaff2)
2049 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2050 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2051 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2052 isl_pw_aff_copy(pwaff2));
2053 dom = isl_set_subtract(dom, isl_set_copy(le));
2054 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2057 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2058 __isl_take isl_pw_aff *pwaff2)
2060 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2063 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2064 __isl_take isl_pw_aff *pwaff2)
2069 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2070 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2071 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2072 isl_pw_aff_copy(pwaff2));
2073 dom = isl_set_subtract(dom, isl_set_copy(ge));
2074 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2077 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2078 __isl_take isl_pw_aff *pwaff2)
2080 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2083 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2084 __isl_take isl_pw_aff_list *list,
2085 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2086 __isl_take isl_pw_aff *pwaff2))
2095 ctx = isl_pw_aff_list_get_ctx(list);
2097 isl_die(ctx, isl_error_invalid,
2098 "list should contain at least one element",
2099 return isl_pw_aff_list_free(list));
2101 res = isl_pw_aff_copy(list->p[0]);
2102 for (i = 1; i < list->n; ++i)
2103 res = fn(res, isl_pw_aff_copy(list->p[i]));
2105 isl_pw_aff_list_free(list);
2109 /* Return an isl_pw_aff that maps each element in the intersection of the
2110 * domains of the elements of list to the minimal corresponding affine
2113 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2115 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2118 /* Return an isl_pw_aff that maps each element in the intersection of the
2119 * domains of the elements of list to the maximal corresponding affine
2122 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2124 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2130 #include <isl_multi_templ.c>
2132 /* Construct an isl_multi_aff in the given space with value zero in
2133 * each of the output dimensions.
2135 __isl_give isl_multi_aff *isl_multi_aff_zero(__isl_take isl_space *space)
2143 n = isl_space_dim(space , isl_dim_out);
2144 ma = isl_multi_aff_alloc(isl_space_copy(space));
2147 isl_space_free(space);
2150 isl_local_space *ls;
2153 space = isl_space_domain(space);
2154 ls = isl_local_space_from_space(space);
2155 aff = isl_aff_zero_on_domain(ls);
2157 for (i = 0; i < n; ++i)
2158 ma = isl_multi_aff_set_aff(ma, i, isl_aff_copy(aff));
2166 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2169 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2170 __isl_take isl_multi_aff *ma)
2172 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2173 return isl_pw_multi_aff_alloc(dom, ma);
2176 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2177 __isl_take isl_multi_aff *maff2)
2182 maff1 = isl_multi_aff_cow(maff1);
2183 if (!maff1 || !maff2)
2186 ctx = isl_multi_aff_get_ctx(maff1);
2187 if (!isl_space_is_equal(maff1->space, maff2->space))
2188 isl_die(ctx, isl_error_invalid,
2189 "spaces don't match", goto error);
2191 for (i = 0; i < maff1->n; ++i) {
2192 maff1->p[i] = isl_aff_add(maff1->p[i],
2193 isl_aff_copy(maff2->p[i]));
2198 isl_multi_aff_free(maff2);
2201 isl_multi_aff_free(maff1);
2202 isl_multi_aff_free(maff2);
2206 /* Exploit the equalities in "eq" to simplify the affine expressions.
2208 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2209 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2213 maff = isl_multi_aff_cow(maff);
2217 for (i = 0; i < maff->n; ++i) {
2218 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2219 isl_basic_set_copy(eq));
2224 isl_basic_set_free(eq);
2227 isl_basic_set_free(eq);
2228 isl_multi_aff_free(maff);
2232 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2237 maff = isl_multi_aff_cow(maff);
2241 for (i = 0; i < maff->n; ++i) {
2242 maff->p[i] = isl_aff_scale(maff->p[i], f);
2244 return isl_multi_aff_free(maff);
2250 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2251 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2253 maff1 = isl_multi_aff_add(maff1, maff2);
2254 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2258 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2266 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2267 __isl_keep isl_multi_aff *maff2)
2272 if (!maff1 || !maff2)
2274 if (maff1->n != maff2->n)
2276 equal = isl_space_is_equal(maff1->space, maff2->space);
2277 if (equal < 0 || !equal)
2280 for (i = 0; i < maff1->n; ++i) {
2281 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2282 if (equal < 0 || !equal)
2289 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2290 __isl_take isl_multi_aff *maff,
2291 enum isl_dim_type type, unsigned pos, const char *s)
2295 maff = isl_multi_aff_cow(maff);
2299 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2301 return isl_multi_aff_free(maff);
2303 if (type == isl_dim_out)
2305 for (i = 0; i < maff->n; ++i) {
2306 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2308 return isl_multi_aff_free(maff);
2314 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2315 enum isl_dim_type type, unsigned first, unsigned n)
2319 maff = isl_multi_aff_cow(maff);
2323 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2325 return isl_multi_aff_free(maff);
2327 if (type == isl_dim_out) {
2328 for (i = 0; i < n; ++i)
2329 isl_aff_free(maff->p[first + i]);
2330 for (i = first; i + n < maff->n; ++i)
2331 maff->p[i] = maff->p[i + n];
2336 for (i = 0; i < maff->n; ++i) {
2337 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2339 return isl_multi_aff_free(maff);
2346 #define PW isl_pw_multi_aff
2348 #define EL isl_multi_aff
2350 #define EL_IS_ZERO is_empty
2354 #define IS_ZERO is_empty
2357 #undef DEFAULT_IS_ZERO
2358 #define DEFAULT_IS_ZERO 0
2363 #define NO_INVOLVES_DIMS
2364 #define NO_MOVE_DIMS
2365 #define NO_INSERT_DIMS
2369 #include <isl_pw_templ.c>
2372 #define UNION isl_union_pw_multi_aff
2374 #define PART isl_pw_multi_aff
2376 #define PARTS pw_multi_aff
2377 #define ALIGN_DOMAIN
2381 #include <isl_union_templ.c>
2383 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2384 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2386 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2387 &isl_multi_aff_add);
2390 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2391 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2393 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2397 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2398 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2400 return isl_pw_multi_aff_union_add_(pma1, pma2);
2403 /* Construct a map mapping the domain of the piecewise multi-affine expression
2404 * to its range, with each dimension in the range equated to the
2405 * corresponding affine expression on its cell.
2407 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2415 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2417 for (i = 0; i < pma->n; ++i) {
2418 isl_multi_aff *maff;
2419 isl_basic_map *bmap;
2422 maff = isl_multi_aff_copy(pma->p[i].maff);
2423 bmap = isl_basic_map_from_multi_aff(maff);
2424 map_i = isl_map_from_basic_map(bmap);
2425 map_i = isl_map_intersect_domain(map_i,
2426 isl_set_copy(pma->p[i].set));
2427 map = isl_map_union_disjoint(map, map_i);
2430 isl_pw_multi_aff_free(pma);
2434 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2436 if (!isl_space_is_set(pma->dim))
2437 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2438 "isl_pw_multi_aff cannot be converted into an isl_set",
2439 return isl_pw_multi_aff_free(pma));
2441 return isl_map_from_pw_multi_aff(pma);
2444 /* Given a basic map with a single output dimension that is defined
2445 * in terms of the parameters and input dimensions using an equality,
2446 * extract an isl_aff that expresses the output dimension in terms
2447 * of the parameters and input dimensions.
2449 * Since some applications expect the result of isl_pw_multi_aff_from_map
2450 * to only contain integer affine expressions, we compute the floor
2451 * of the expression before returning.
2453 * This function shares some similarities with
2454 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2456 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
2457 __isl_take isl_basic_map *bmap)
2462 isl_local_space *ls;
2467 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
2468 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2469 "basic map should have a single output dimension",
2471 offset = isl_basic_map_offset(bmap, isl_dim_out);
2472 total = isl_basic_map_total_dim(bmap);
2473 for (i = 0; i < bmap->n_eq; ++i) {
2474 if (isl_int_is_zero(bmap->eq[i][offset]))
2476 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
2477 1 + total - (offset + 1)) != -1)
2481 if (i >= bmap->n_eq)
2482 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2483 "unable to find suitable equality", goto error);
2484 ls = isl_basic_map_get_local_space(bmap);
2485 aff = isl_aff_alloc(isl_local_space_domain(ls));
2488 if (isl_int_is_neg(bmap->eq[i][offset]))
2489 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
2491 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
2492 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
2493 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
2494 isl_basic_map_free(bmap);
2496 aff = isl_aff_remove_unused_divs(aff);
2497 aff = isl_aff_floor(aff);
2500 isl_basic_map_free(bmap);
2504 /* Given a basic map where each output dimension is defined
2505 * in terms of the parameters and input dimensions using an equality,
2506 * extract an isl_multi_aff that expresses the output dimensions in terms
2507 * of the parameters and input dimensions.
2509 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
2510 __isl_take isl_basic_map *bmap)
2519 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
2520 n_out = isl_basic_map_dim(bmap, isl_dim_out);
2522 for (i = 0; i < n_out; ++i) {
2523 isl_basic_map *bmap_i;
2526 bmap_i = isl_basic_map_copy(bmap);
2527 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
2528 i + 1, n_out - (1 + i));
2529 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
2530 aff = extract_isl_aff_from_basic_map(bmap_i);
2531 ma = isl_multi_aff_set_aff(ma, i, aff);
2534 isl_basic_map_free(bmap);
2539 /* Create an isl_pw_multi_aff that is equivalent to
2540 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2541 * The given basic map is such that each output dimension is defined
2542 * in terms of the parameters and input dimensions using an equality.
2544 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
2545 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
2549 ma = extract_isl_multi_aff_from_basic_map(bmap);
2550 return isl_pw_multi_aff_alloc(domain, ma);
2553 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2554 * This obivously only works if the input "map" is single-valued.
2555 * If so, we compute the lexicographic minimum of the image in the form
2556 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2557 * to its lexicographic minimum.
2558 * If the input is not single-valued, we produce an error.
2560 * As a special case, we first check if all output dimensions are uniquely
2561 * defined in terms of the parameters and input dimensions over the entire
2562 * domain. If so, we extract the desired isl_pw_multi_aff directly
2563 * from the affine hull of "map" and its domain.
2565 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2569 isl_pw_multi_aff *pma;
2570 isl_basic_map *hull;
2575 hull = isl_map_affine_hull(isl_map_copy(map));
2576 sv = isl_basic_map_plain_is_single_valued(hull);
2578 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
2579 isl_basic_map_free(hull);
2583 sv = isl_map_is_single_valued(map);
2587 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2588 "map is not single-valued", goto error);
2589 map = isl_map_make_disjoint(map);
2593 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2595 for (i = 0; i < map->n; ++i) {
2596 isl_pw_multi_aff *pma_i;
2597 isl_basic_map *bmap;
2598 bmap = isl_basic_map_copy(map->p[i]);
2599 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2600 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2610 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2612 return isl_pw_multi_aff_from_map(set);
2615 /* Return the piecewise affine expression "set ? 1 : 0".
2617 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
2620 isl_space *space = isl_set_get_space(set);
2621 isl_local_space *ls = isl_local_space_from_space(space);
2622 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
2623 isl_aff *one = isl_aff_zero_on_domain(ls);
2625 one = isl_aff_add_constant_si(one, 1);
2626 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
2627 set = isl_set_complement(set);
2628 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
2633 /* Plug in "subs" for dimension "type", "pos" of "aff".
2635 * Let i be the dimension to replace and let "subs" be of the form
2639 * and "aff" of the form
2645 * floor((a f + d g')/(m d))
2647 * where g' is the result of plugging in "subs" in each of the integer
2650 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2651 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2656 aff = isl_aff_cow(aff);
2658 return isl_aff_free(aff);
2660 ctx = isl_aff_get_ctx(aff);
2661 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2662 isl_die(ctx, isl_error_invalid,
2663 "spaces don't match", return isl_aff_free(aff));
2664 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2665 isl_die(ctx, isl_error_unsupported,
2666 "cannot handle divs yet", return isl_aff_free(aff));
2668 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2670 return isl_aff_free(aff);
2672 aff->v = isl_vec_cow(aff->v);
2674 return isl_aff_free(aff);
2676 pos += isl_local_space_offset(aff->ls, type);
2679 isl_int_set(v, aff->v->el[1 + pos]);
2680 isl_int_set_si(aff->v->el[1 + pos], 0);
2681 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2682 v, subs->v->el + 1, subs->v->size - 1);
2683 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2689 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2690 * expressions in "maff".
2692 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2693 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2694 __isl_keep isl_aff *subs)
2698 maff = isl_multi_aff_cow(maff);
2700 return isl_multi_aff_free(maff);
2702 if (type == isl_dim_in)
2705 for (i = 0; i < maff->n; ++i) {
2706 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2708 return isl_multi_aff_free(maff);
2714 /* Plug in "subs" for dimension "type", "pos" of "pma".
2716 * pma is of the form
2720 * while subs is of the form
2722 * v' = B_j(v) -> S_j
2724 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2725 * has a contribution in the result, in particular
2727 * C_ij(S_j) -> M_i(S_j)
2729 * Note that plugging in S_j in C_ij may also result in an empty set
2730 * and this contribution should simply be discarded.
2732 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2733 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2734 __isl_keep isl_pw_aff *subs)
2737 isl_pw_multi_aff *res;
2740 return isl_pw_multi_aff_free(pma);
2742 n = pma->n * subs->n;
2743 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2745 for (i = 0; i < pma->n; ++i) {
2746 for (j = 0; j < subs->n; ++j) {
2748 isl_multi_aff *res_ij;
2749 common = isl_set_intersect(
2750 isl_set_copy(pma->p[i].set),
2751 isl_set_copy(subs->p[j].set));
2752 common = isl_set_substitute(common,
2753 type, pos, subs->p[j].aff);
2754 if (isl_set_plain_is_empty(common)) {
2755 isl_set_free(common);
2759 res_ij = isl_multi_aff_substitute(
2760 isl_multi_aff_copy(pma->p[i].maff),
2761 type, pos, subs->p[j].aff);
2763 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2767 isl_pw_multi_aff_free(pma);
2771 /* Extend the local space of "dst" to include the divs
2772 * in the local space of "src".
2774 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2775 __isl_keep isl_aff *src)
2783 return isl_aff_free(dst);
2785 ctx = isl_aff_get_ctx(src);
2786 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2787 isl_die(ctx, isl_error_invalid,
2788 "spaces don't match", goto error);
2790 if (src->ls->div->n_row == 0)
2793 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2794 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2798 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2799 dst = isl_aff_expand_divs(dst, div, exp2);
2807 return isl_aff_free(dst);
2810 /* Adjust the local spaces of the affine expressions in "maff"
2811 * such that they all have the save divs.
2813 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2814 __isl_take isl_multi_aff *maff)
2822 maff = isl_multi_aff_cow(maff);
2826 for (i = 1; i < maff->n; ++i)
2827 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2828 for (i = 1; i < maff->n; ++i) {
2829 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2831 return isl_multi_aff_free(maff);
2837 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2839 aff = isl_aff_cow(aff);
2843 aff->ls = isl_local_space_lift(aff->ls);
2845 return isl_aff_free(aff);
2850 /* Lift "maff" to a space with extra dimensions such that the result
2851 * has no more existentially quantified variables.
2852 * If "ls" is not NULL, then *ls is assigned the local space that lies
2853 * at the basis of the lifting applied to "maff".
2855 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2856 __isl_give isl_local_space **ls)
2870 isl_space *space = isl_multi_aff_get_domain_space(maff);
2871 *ls = isl_local_space_from_space(space);
2873 return isl_multi_aff_free(maff);
2878 maff = isl_multi_aff_cow(maff);
2879 maff = isl_multi_aff_align_divs(maff);
2883 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2884 space = isl_multi_aff_get_space(maff);
2885 space = isl_space_lift(isl_space_domain(space), n_div);
2886 space = isl_space_extend_domain_with_range(space,
2887 isl_multi_aff_get_space(maff));
2889 return isl_multi_aff_free(maff);
2890 isl_space_free(maff->space);
2891 maff->space = space;
2894 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2896 return isl_multi_aff_free(maff);
2899 for (i = 0; i < maff->n; ++i) {
2900 maff->p[i] = isl_aff_lift(maff->p[i]);
2908 isl_local_space_free(*ls);
2909 return isl_multi_aff_free(maff);
2913 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2915 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2916 __isl_keep isl_pw_multi_aff *pma, int pos)
2926 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2927 if (pos < 0 || pos >= n_out)
2928 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2929 "index out of bounds", return NULL);
2931 space = isl_pw_multi_aff_get_space(pma);
2932 space = isl_space_drop_dims(space, isl_dim_out,
2933 pos + 1, n_out - pos - 1);
2934 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2936 pa = isl_pw_aff_alloc_size(space, pma->n);
2937 for (i = 0; i < pma->n; ++i) {
2939 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2940 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
2946 /* Return an isl_pw_multi_aff with the given "set" as domain and
2947 * an unnamed zero-dimensional range.
2949 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
2950 __isl_take isl_set *set)
2955 space = isl_set_get_space(set);
2956 space = isl_space_from_domain(space);
2957 ma = isl_multi_aff_zero(space);
2958 return isl_pw_multi_aff_alloc(set, ma);
2961 /* Add an isl_pw_multi_aff with the given "set" as domain and
2962 * an unnamed zero-dimensional range to *user.
2964 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
2966 isl_union_pw_multi_aff **upma = user;
2967 isl_pw_multi_aff *pma;
2969 pma = isl_pw_multi_aff_from_domain(set);
2970 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
2975 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
2976 * an unnamed zero-dimensional range.
2978 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
2979 __isl_take isl_union_set *uset)
2982 isl_union_pw_multi_aff *upma;
2987 space = isl_union_set_get_space(uset);
2988 upma = isl_union_pw_multi_aff_empty(space);
2990 if (isl_union_set_foreach_set(uset,
2991 &add_pw_multi_aff_from_domain, &upma) < 0)
2994 isl_union_set_free(uset);
2997 isl_union_set_free(uset);
2998 isl_union_pw_multi_aff_free(upma);
3002 /* Convert "pma" to an isl_map and add it to *umap.
3004 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
3006 isl_union_map **umap = user;
3009 map = isl_map_from_pw_multi_aff(pma);
3010 *umap = isl_union_map_add_map(*umap, map);
3015 /* Construct a union map mapping the domain of the union
3016 * piecewise multi-affine expression to its range, with each dimension
3017 * in the range equated to the corresponding affine expression on its cell.
3019 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
3020 __isl_take isl_union_pw_multi_aff *upma)
3023 isl_union_map *umap;
3028 space = isl_union_pw_multi_aff_get_space(upma);
3029 umap = isl_union_map_empty(space);
3031 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3032 &map_from_pw_multi_aff, &umap) < 0)
3035 isl_union_pw_multi_aff_free(upma);
3038 isl_union_pw_multi_aff_free(upma);
3039 isl_union_map_free(umap);
3043 /* Local data for bin_entry and the callback "fn".
3045 struct isl_union_pw_multi_aff_bin_data {
3046 isl_union_pw_multi_aff *upma2;
3047 isl_union_pw_multi_aff *res;
3048 isl_pw_multi_aff *pma;
3049 int (*fn)(void **entry, void *user);
3052 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
3053 * and call data->fn for each isl_pw_multi_aff in data->upma2.
3055 static int bin_entry(void **entry, void *user)
3057 struct isl_union_pw_multi_aff_bin_data *data = user;
3058 isl_pw_multi_aff *pma = *entry;
3061 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
3062 data->fn, data) < 0)
3068 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
3069 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
3070 * passed as user field) and the isl_pw_multi_aff from upma2 is available
3071 * as *entry. The callback should adjust data->res if desired.
3073 static __isl_give isl_union_pw_multi_aff *bin_op(
3074 __isl_take isl_union_pw_multi_aff *upma1,
3075 __isl_take isl_union_pw_multi_aff *upma2,
3076 int (*fn)(void **entry, void *user))
3079 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
3081 space = isl_union_pw_multi_aff_get_space(upma2);
3082 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
3083 space = isl_union_pw_multi_aff_get_space(upma1);
3084 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
3086 if (!upma1 || !upma2)
3090 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
3092 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
3093 &bin_entry, &data) < 0)
3096 isl_union_pw_multi_aff_free(upma1);
3097 isl_union_pw_multi_aff_free(upma2);
3100 isl_union_pw_multi_aff_free(upma1);
3101 isl_union_pw_multi_aff_free(upma2);
3102 isl_union_pw_multi_aff_free(data.res);
3106 /* Given two isl_multi_affs A -> B and C -> D,
3107 * construct an isl_multi_aff (A * C) -> (B, D).
3109 __isl_give isl_multi_aff *isl_multi_aff_flat_range_product(
3110 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3120 space = isl_space_range_product(isl_multi_aff_get_space(ma1),
3121 isl_multi_aff_get_space(ma2));
3122 space = isl_space_flatten_range(space);
3123 res = isl_multi_aff_alloc(space);
3125 n1 = isl_multi_aff_dim(ma1, isl_dim_out);
3126 n2 = isl_multi_aff_dim(ma2, isl_dim_out);
3128 for (i = 0; i < n1; ++i) {
3129 aff = isl_multi_aff_get_aff(ma1, i);
3130 res = isl_multi_aff_set_aff(res, i, aff);
3133 for (i = 0; i < n2; ++i) {
3134 aff = isl_multi_aff_get_aff(ma2, i);
3135 res = isl_multi_aff_set_aff(res, n1 + i, aff);
3138 isl_multi_aff_free(ma1);
3139 isl_multi_aff_free(ma2);
3142 isl_multi_aff_free(ma1);
3143 isl_multi_aff_free(ma2);
3147 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
3148 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3150 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
3151 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3155 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
3156 isl_pw_multi_aff_get_space(pma2));
3157 space = isl_space_flatten_range(space);
3158 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
3159 &isl_multi_aff_flat_range_product);
3162 /* Given two isl_pw_multi_affs A -> B and C -> D,
3163 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3165 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
3166 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3168 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3169 &pw_multi_aff_flat_range_product);
3172 /* If data->pma and *entry have the same domain space, then compute
3173 * their flat range product and the result to data->res.
3175 static int flat_range_product_entry(void **entry, void *user)
3177 struct isl_union_pw_multi_aff_bin_data *data = user;
3178 isl_pw_multi_aff *pma2 = *entry;
3180 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
3181 pma2->dim, isl_dim_in))
3184 pma2 = isl_pw_multi_aff_flat_range_product(
3185 isl_pw_multi_aff_copy(data->pma),
3186 isl_pw_multi_aff_copy(pma2));
3188 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
3193 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
3194 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
3196 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
3197 __isl_take isl_union_pw_multi_aff *upma1,
3198 __isl_take isl_union_pw_multi_aff *upma2)
3200 return bin_op(upma1, upma2, &flat_range_product_entry);