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>
15 #include <isl_map_private.h>
16 #include <isl_aff_private.h>
17 #include <isl_space_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_list_private.h>
21 #include <isl/constraint.h>
24 #include <isl_config.h>
26 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
27 __isl_take isl_vec *v)
34 aff = isl_calloc_type(v->ctx, struct isl_aff);
44 isl_local_space_free(ls);
49 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
58 ctx = isl_local_space_get_ctx(ls);
59 if (!isl_local_space_divs_known(ls))
60 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
62 if (!isl_local_space_is_set(ls))
63 isl_die(ctx, isl_error_invalid,
64 "domain of affine expression should be a set",
67 total = isl_local_space_dim(ls, isl_dim_all);
68 v = isl_vec_alloc(ctx, 1 + 1 + total);
69 return isl_aff_alloc_vec(ls, v);
71 isl_local_space_free(ls);
75 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
79 aff = isl_aff_alloc(ls);
83 isl_int_set_si(aff->v->el[0], 1);
84 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
89 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
98 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
103 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
104 isl_vec_copy(aff->v));
107 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
115 return isl_aff_dup(aff);
118 void *isl_aff_free(__isl_take isl_aff *aff)
126 isl_local_space_free(aff->ls);
127 isl_vec_free(aff->v);
134 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
136 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
139 /* Externally, an isl_aff has a map space, but internally, the
140 * ls field corresponds to the domain of that space.
142 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
146 if (type == isl_dim_out)
148 if (type == isl_dim_in)
150 return isl_local_space_dim(aff->ls, type);
153 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
155 return aff ? isl_local_space_get_space(aff->ls) : NULL;
158 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
163 space = isl_local_space_get_space(aff->ls);
164 space = isl_space_from_domain(space);
165 space = isl_space_add_dims(space, isl_dim_out, 1);
169 __isl_give isl_local_space *isl_aff_get_domain_local_space(
170 __isl_keep isl_aff *aff)
172 return aff ? isl_local_space_copy(aff->ls) : NULL;
175 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
180 ls = isl_local_space_copy(aff->ls);
181 ls = isl_local_space_from_domain(ls);
182 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
186 /* Externally, an isl_aff has a map space, but internally, the
187 * ls field corresponds to the domain of that space.
189 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
190 enum isl_dim_type type, unsigned pos)
194 if (type == isl_dim_out)
196 if (type == isl_dim_in)
198 return isl_local_space_get_dim_name(aff->ls, type, pos);
201 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
202 __isl_take isl_space *dim)
204 aff = isl_aff_cow(aff);
208 aff->ls = isl_local_space_reset_space(aff->ls, dim);
210 return isl_aff_free(aff);
219 /* Reset the space of "aff". This function is called from isl_pw_templ.c
220 * and doesn't know if the space of an element object is represented
221 * directly or through its domain. It therefore passes along both.
223 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
224 __isl_take isl_space *space, __isl_take isl_space *domain)
226 isl_space_free(space);
227 return isl_aff_reset_domain_space(aff, domain);
230 /* Reorder the coefficients of the affine expression based
231 * on the given reodering.
232 * The reordering r is assumed to have been extended with the local
235 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
236 __isl_take isl_reordering *r, int n_div)
244 res = isl_vec_alloc(vec->ctx,
245 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
246 isl_seq_cpy(res->el, vec->el, 2);
247 isl_seq_clr(res->el + 2, res->size - 2);
248 for (i = 0; i < r->len; ++i)
249 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
251 isl_reordering_free(r);
256 isl_reordering_free(r);
260 /* Reorder the dimensions of the domain of "aff" according
261 * to the given reordering.
263 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
264 __isl_take isl_reordering *r)
266 aff = isl_aff_cow(aff);
270 r = isl_reordering_extend(r, aff->ls->div->n_row);
271 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
272 aff->ls->div->n_row);
273 aff->ls = isl_local_space_realign(aff->ls, r);
275 if (!aff->v || !aff->ls)
276 return isl_aff_free(aff);
281 isl_reordering_free(r);
285 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
286 __isl_take isl_space *model)
291 if (!isl_space_match(aff->ls->dim, isl_dim_param,
292 model, isl_dim_param)) {
295 model = isl_space_drop_dims(model, isl_dim_in,
296 0, isl_space_dim(model, isl_dim_in));
297 model = isl_space_drop_dims(model, isl_dim_out,
298 0, isl_space_dim(model, isl_dim_out));
299 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
300 exp = isl_reordering_extend_space(exp,
301 isl_aff_get_domain_space(aff));
302 aff = isl_aff_realign_domain(aff, exp);
305 isl_space_free(model);
308 isl_space_free(model);
313 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
318 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
321 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
328 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
329 if (equal < 0 || !equal)
332 return isl_vec_is_equal(aff1->v, aff2->v);
335 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
339 isl_int_set(*v, aff->v->el[0]);
343 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
347 isl_int_set(*v, aff->v->el[1]);
351 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
352 enum isl_dim_type type, int pos, isl_int *v)
357 if (type == isl_dim_out)
358 isl_die(aff->v->ctx, isl_error_invalid,
359 "output/set dimension does not have a coefficient",
361 if (type == isl_dim_in)
364 if (pos >= isl_local_space_dim(aff->ls, type))
365 isl_die(aff->v->ctx, isl_error_invalid,
366 "position out of bounds", return -1);
368 pos += isl_local_space_offset(aff->ls, type);
369 isl_int_set(*v, aff->v->el[1 + pos]);
374 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
376 aff = isl_aff_cow(aff);
380 aff->v = isl_vec_cow(aff->v);
382 return isl_aff_free(aff);
384 isl_int_set(aff->v->el[0], v);
389 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
391 aff = isl_aff_cow(aff);
395 aff->v = isl_vec_cow(aff->v);
397 return isl_aff_free(aff);
399 isl_int_set(aff->v->el[1], v);
404 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
406 if (isl_int_is_zero(v))
409 aff = isl_aff_cow(aff);
413 aff->v = isl_vec_cow(aff->v);
415 return isl_aff_free(aff);
417 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
422 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
427 isl_int_set_si(t, v);
428 aff = isl_aff_add_constant(aff, t);
434 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
436 aff = isl_aff_cow(aff);
440 aff->v = isl_vec_cow(aff->v);
442 return isl_aff_free(aff);
444 isl_int_set_si(aff->v->el[1], v);
449 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
450 enum isl_dim_type type, int pos, isl_int v)
455 if (type == isl_dim_out)
456 isl_die(aff->v->ctx, isl_error_invalid,
457 "output/set dimension does not have a coefficient",
458 return isl_aff_free(aff));
459 if (type == isl_dim_in)
462 if (pos >= isl_local_space_dim(aff->ls, type))
463 isl_die(aff->v->ctx, isl_error_invalid,
464 "position out of bounds", return isl_aff_free(aff));
466 aff = isl_aff_cow(aff);
470 aff->v = isl_vec_cow(aff->v);
472 return isl_aff_free(aff);
474 pos += isl_local_space_offset(aff->ls, type);
475 isl_int_set(aff->v->el[1 + pos], v);
480 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
481 enum isl_dim_type type, int pos, int v)
486 if (type == isl_dim_out)
487 isl_die(aff->v->ctx, isl_error_invalid,
488 "output/set dimension does not have a coefficient",
489 return isl_aff_free(aff));
490 if (type == isl_dim_in)
493 if (pos >= isl_local_space_dim(aff->ls, type))
494 isl_die(aff->v->ctx, isl_error_invalid,
495 "position out of bounds", return isl_aff_free(aff));
497 aff = isl_aff_cow(aff);
501 aff->v = isl_vec_cow(aff->v);
503 return isl_aff_free(aff);
505 pos += isl_local_space_offset(aff->ls, type);
506 isl_int_set_si(aff->v->el[1 + pos], v);
511 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
512 enum isl_dim_type type, int pos, isl_int v)
517 if (type == isl_dim_out)
518 isl_die(aff->v->ctx, isl_error_invalid,
519 "output/set dimension does not have a coefficient",
520 return isl_aff_free(aff));
521 if (type == isl_dim_in)
524 if (pos >= isl_local_space_dim(aff->ls, type))
525 isl_die(aff->v->ctx, isl_error_invalid,
526 "position out of bounds", return isl_aff_free(aff));
528 aff = isl_aff_cow(aff);
532 aff->v = isl_vec_cow(aff->v);
534 return isl_aff_free(aff);
536 pos += isl_local_space_offset(aff->ls, type);
537 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
542 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
543 enum isl_dim_type type, int pos, int v)
548 isl_int_set_si(t, v);
549 aff = isl_aff_add_coefficient(aff, type, pos, t);
555 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
560 return isl_local_space_get_div(aff->ls, pos);
563 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
565 aff = isl_aff_cow(aff);
568 aff->v = isl_vec_cow(aff->v);
570 return isl_aff_free(aff);
572 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
577 /* Remove divs from the local space that do not appear in the affine
579 * We currently only remove divs at the end.
580 * Some intermediate divs may also not appear directly in the affine
581 * expression, but we would also need to check that no other divs are
582 * defined in terms of them.
584 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
593 n = isl_local_space_dim(aff->ls, isl_dim_div);
594 off = isl_local_space_offset(aff->ls, isl_dim_div);
596 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
600 aff = isl_aff_cow(aff);
604 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
605 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
606 if (!aff->ls || !aff->v)
607 return isl_aff_free(aff);
612 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
616 aff->v = isl_vec_normalize(aff->v);
618 return isl_aff_free(aff);
622 /* Given f, return floor(f).
623 * If f is an integer expression, then just return f.
624 * Otherwise, if f = g/m, write g = q m + r,
625 * create a new div d = [r/m] and return the expression q + d.
626 * The coefficients in r are taken to lie between -m/2 and m/2.
628 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
638 if (isl_int_is_one(aff->v->el[0]))
641 aff = isl_aff_cow(aff);
645 aff->v = isl_vec_cow(aff->v);
646 div = isl_vec_copy(aff->v);
647 div = isl_vec_cow(div);
649 return isl_aff_free(aff);
651 ctx = isl_aff_get_ctx(aff);
652 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
653 for (i = 1; i < aff->v->size; ++i) {
654 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
655 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
656 if (isl_int_gt(div->el[i], aff->v->el[0])) {
657 isl_int_sub(div->el[i], div->el[i], div->el[0]);
658 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
662 aff->ls = isl_local_space_add_div(aff->ls, div);
664 return isl_aff_free(aff);
667 aff->v = isl_vec_extend(aff->v, size + 1);
669 return isl_aff_free(aff);
670 isl_int_set_si(aff->v->el[0], 1);
671 isl_int_set_si(aff->v->el[size], 1);
678 * aff mod m = aff - m * floor(aff/m)
680 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
684 res = isl_aff_copy(aff);
685 aff = isl_aff_scale_down(aff, m);
686 aff = isl_aff_floor(aff);
687 aff = isl_aff_scale(aff, m);
688 res = isl_aff_sub(res, aff);
695 * pwaff mod m = pwaff - m * floor(pwaff/m)
697 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
701 res = isl_pw_aff_copy(pwaff);
702 pwaff = isl_pw_aff_scale_down(pwaff, m);
703 pwaff = isl_pw_aff_floor(pwaff);
704 pwaff = isl_pw_aff_scale(pwaff, m);
705 res = isl_pw_aff_sub(res, pwaff);
710 /* Given f, return ceil(f).
711 * If f is an integer expression, then just return f.
712 * Otherwise, create a new div d = [-f] and return the expression -d.
714 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
719 if (isl_int_is_one(aff->v->el[0]))
722 aff = isl_aff_neg(aff);
723 aff = isl_aff_floor(aff);
724 aff = isl_aff_neg(aff);
729 /* Apply the expansion computed by isl_merge_divs.
730 * The expansion itself is given by "exp" while the resulting
731 * list of divs is given by "div".
733 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
734 __isl_take isl_mat *div, int *exp)
741 aff = isl_aff_cow(aff);
745 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
746 new_n_div = isl_mat_rows(div);
747 if (new_n_div < old_n_div)
748 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
749 "not an expansion", goto error);
751 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
755 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
757 for (i = new_n_div - 1; i >= 0; --i) {
758 if (j >= 0 && exp[j] == i) {
760 isl_int_swap(aff->v->el[offset + i],
761 aff->v->el[offset + j]);
764 isl_int_set_si(aff->v->el[offset + i], 0);
767 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
778 /* Add two affine expressions that live in the same local space.
780 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
781 __isl_take isl_aff *aff2)
785 aff1 = isl_aff_cow(aff1);
789 aff1->v = isl_vec_cow(aff1->v);
795 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
796 isl_int_divexact(f, aff2->v->el[0], gcd);
797 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
798 isl_int_divexact(f, aff1->v->el[0], gcd);
799 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
800 isl_int_divexact(f, aff2->v->el[0], gcd);
801 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
813 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
814 __isl_take isl_aff *aff2)
824 ctx = isl_aff_get_ctx(aff1);
825 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
826 isl_die(ctx, isl_error_invalid,
827 "spaces don't match", goto error);
829 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
830 return add_expanded(aff1, aff2);
832 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
833 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
837 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
838 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
839 aff2 = isl_aff_expand_divs(aff2, div, exp2);
843 return add_expanded(aff1, aff2);
852 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
853 __isl_take isl_aff *aff2)
855 return isl_aff_add(aff1, isl_aff_neg(aff2));
858 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
862 if (isl_int_is_one(f))
865 aff = isl_aff_cow(aff);
868 aff->v = isl_vec_cow(aff->v);
870 return isl_aff_free(aff);
873 isl_int_gcd(gcd, aff->v->el[0], f);
874 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
875 isl_int_divexact(gcd, f, gcd);
876 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
882 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
886 if (isl_int_is_one(f))
889 aff = isl_aff_cow(aff);
892 aff->v = isl_vec_cow(aff->v);
894 return isl_aff_free(aff);
897 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
898 isl_int_gcd(gcd, gcd, f);
899 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
900 isl_int_divexact(gcd, f, gcd);
901 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
907 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
915 isl_int_set_ui(v, f);
916 aff = isl_aff_scale_down(aff, v);
922 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
923 enum isl_dim_type type, unsigned pos, const char *s)
925 aff = isl_aff_cow(aff);
928 if (type == isl_dim_out)
929 isl_die(aff->v->ctx, isl_error_invalid,
930 "cannot set name of output/set dimension",
931 return isl_aff_free(aff));
932 if (type == isl_dim_in)
934 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
936 return isl_aff_free(aff);
941 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
942 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
944 aff = isl_aff_cow(aff);
946 return isl_id_free(id);
947 if (type == isl_dim_out)
948 isl_die(aff->v->ctx, isl_error_invalid,
949 "cannot set name of output/set dimension",
951 if (type == isl_dim_in)
953 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
955 return isl_aff_free(aff);
964 /* Exploit the equalities in "eq" to simplify the affine expression
965 * and the expressions of the integer divisions in the local space.
966 * The integer divisions in this local space are assumed to appear
967 * as regular dimensions in "eq".
969 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
970 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
979 isl_basic_set_free(eq);
983 aff = isl_aff_cow(aff);
987 aff->ls = isl_local_space_substitute_equalities(aff->ls,
988 isl_basic_set_copy(eq));
992 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
994 for (i = 0; i < eq->n_eq; ++i) {
995 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
996 if (j < 0 || j == 0 || j >= total)
999 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1003 isl_basic_set_free(eq);
1004 aff = isl_aff_normalize(aff);
1007 isl_basic_set_free(eq);
1012 /* Exploit the equalities in "eq" to simplify the affine expression
1013 * and the expressions of the integer divisions in the local space.
1015 static __isl_give isl_aff *isl_aff_substitute_equalities(
1016 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1022 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1024 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
1025 return isl_aff_substitute_equalities_lifted(aff, eq);
1027 isl_basic_set_free(eq);
1032 /* Look for equalities among the variables shared by context and aff
1033 * and the integer divisions of aff, if any.
1034 * The equalities are then used to eliminate coefficients and/or integer
1035 * divisions from aff.
1037 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1038 __isl_take isl_set *context)
1040 isl_basic_set *hull;
1045 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1047 isl_basic_set *bset;
1048 isl_local_space *ls;
1049 context = isl_set_add_dims(context, isl_dim_set, n_div);
1050 ls = isl_aff_get_domain_local_space(aff);
1051 bset = isl_basic_set_from_local_space(ls);
1052 bset = isl_basic_set_lift(bset);
1053 bset = isl_basic_set_flatten(bset);
1054 context = isl_set_intersect(context,
1055 isl_set_from_basic_set(bset));
1058 hull = isl_set_affine_hull(context);
1059 return isl_aff_substitute_equalities_lifted(aff, hull);
1062 isl_set_free(context);
1066 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1067 __isl_take isl_set *context)
1069 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1070 dom_context = isl_set_intersect_params(dom_context, context);
1071 return isl_aff_gist(aff, dom_context);
1074 /* Return a basic set containing those elements in the space
1075 * of aff where it is non-negative.
1077 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1079 isl_constraint *ineq;
1080 isl_basic_set *bset;
1082 ineq = isl_inequality_from_aff(aff);
1084 bset = isl_basic_set_from_constraint(ineq);
1085 bset = isl_basic_set_simplify(bset);
1089 /* Return a basic set containing those elements in the space
1090 * of aff where it is zero.
1092 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1094 isl_constraint *ineq;
1095 isl_basic_set *bset;
1097 ineq = isl_equality_from_aff(aff);
1099 bset = isl_basic_set_from_constraint(ineq);
1100 bset = isl_basic_set_simplify(bset);
1104 /* Return a basic set containing those elements in the shared space
1105 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1107 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1108 __isl_take isl_aff *aff2)
1110 aff1 = isl_aff_sub(aff1, aff2);
1112 return isl_aff_nonneg_basic_set(aff1);
1115 /* Return a basic set containing those elements in the shared space
1116 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1118 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1119 __isl_take isl_aff *aff2)
1121 return isl_aff_ge_basic_set(aff2, aff1);
1124 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1125 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1127 aff1 = isl_aff_add(aff1, aff2);
1128 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1132 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1140 /* Check whether the given affine expression has non-zero coefficient
1141 * for any dimension in the given range or if any of these dimensions
1142 * appear with non-zero coefficients in any of the integer divisions
1143 * involved in the affine expression.
1145 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1146 enum isl_dim_type type, unsigned first, unsigned n)
1158 ctx = isl_aff_get_ctx(aff);
1159 if (first + n > isl_aff_dim(aff, type))
1160 isl_die(ctx, isl_error_invalid,
1161 "range out of bounds", return -1);
1163 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1167 first += isl_local_space_offset(aff->ls, type) - 1;
1168 for (i = 0; i < n; ++i)
1169 if (active[first + i]) {
1182 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1183 enum isl_dim_type type, unsigned first, unsigned n)
1189 if (type == isl_dim_out)
1190 isl_die(aff->v->ctx, isl_error_invalid,
1191 "cannot drop output/set dimension",
1192 return isl_aff_free(aff));
1193 if (type == isl_dim_in)
1195 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1198 ctx = isl_aff_get_ctx(aff);
1199 if (first + n > isl_local_space_dim(aff->ls, type))
1200 isl_die(ctx, isl_error_invalid, "range out of bounds",
1201 return isl_aff_free(aff));
1203 aff = isl_aff_cow(aff);
1207 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1209 return isl_aff_free(aff);
1211 first += 1 + isl_local_space_offset(aff->ls, type);
1212 aff->v = isl_vec_drop_els(aff->v, first, n);
1214 return isl_aff_free(aff);
1219 /* Project the domain of the affine expression onto its parameter space.
1220 * The affine expression may not involve any of the domain dimensions.
1222 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1228 n = isl_aff_dim(aff, isl_dim_in);
1229 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1231 return isl_aff_free(aff);
1233 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1234 "affine expression involves some of the domain dimensions",
1235 return isl_aff_free(aff));
1236 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1237 space = isl_aff_get_domain_space(aff);
1238 space = isl_space_params(space);
1239 aff = isl_aff_reset_domain_space(aff, space);
1243 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1244 enum isl_dim_type type, unsigned first, unsigned n)
1250 if (type == isl_dim_out)
1251 isl_die(aff->v->ctx, isl_error_invalid,
1252 "cannot insert output/set dimensions",
1253 return isl_aff_free(aff));
1254 if (type == isl_dim_in)
1256 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1259 ctx = isl_aff_get_ctx(aff);
1260 if (first > isl_local_space_dim(aff->ls, type))
1261 isl_die(ctx, isl_error_invalid, "position out of bounds",
1262 return isl_aff_free(aff));
1264 aff = isl_aff_cow(aff);
1268 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1270 return isl_aff_free(aff);
1272 first += 1 + isl_local_space_offset(aff->ls, type);
1273 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1275 return isl_aff_free(aff);
1280 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1281 enum isl_dim_type type, unsigned n)
1285 pos = isl_aff_dim(aff, type);
1287 return isl_aff_insert_dims(aff, type, pos, n);
1290 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1291 enum isl_dim_type type, unsigned n)
1295 pos = isl_pw_aff_dim(pwaff, type);
1297 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1300 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1302 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1303 return isl_pw_aff_alloc(dom, aff);
1307 #define PW isl_pw_aff
1311 #define EL_IS_ZERO is_empty
1315 #define IS_ZERO is_empty
1318 #undef DEFAULT_IS_ZERO
1319 #define DEFAULT_IS_ZERO 0
1323 #define NO_MOVE_DIMS
1327 #include <isl_pw_templ.c>
1329 static __isl_give isl_set *align_params_pw_pw_set_and(
1330 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1331 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1332 __isl_take isl_pw_aff *pwaff2))
1334 if (!pwaff1 || !pwaff2)
1336 if (isl_space_match(pwaff1->dim, isl_dim_param,
1337 pwaff2->dim, isl_dim_param))
1338 return fn(pwaff1, pwaff2);
1339 if (!isl_space_has_named_params(pwaff1->dim) ||
1340 !isl_space_has_named_params(pwaff2->dim))
1341 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1342 "unaligned unnamed parameters", goto error);
1343 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1344 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1345 return fn(pwaff1, pwaff2);
1347 isl_pw_aff_free(pwaff1);
1348 isl_pw_aff_free(pwaff2);
1352 /* Compute a piecewise quasi-affine expression with a domain that
1353 * is the union of those of pwaff1 and pwaff2 and such that on each
1354 * cell, the quasi-affine expression is the better (according to cmp)
1355 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1356 * is defined on a given cell, then the associated expression
1357 * is the defined one.
1359 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1360 __isl_take isl_pw_aff *pwaff2,
1361 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1362 __isl_take isl_aff *aff2))
1369 if (!pwaff1 || !pwaff2)
1372 ctx = isl_space_get_ctx(pwaff1->dim);
1373 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1374 isl_die(ctx, isl_error_invalid,
1375 "arguments should live in same space", goto error);
1377 if (isl_pw_aff_is_empty(pwaff1)) {
1378 isl_pw_aff_free(pwaff1);
1382 if (isl_pw_aff_is_empty(pwaff2)) {
1383 isl_pw_aff_free(pwaff2);
1387 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1388 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1390 for (i = 0; i < pwaff1->n; ++i) {
1391 set = isl_set_copy(pwaff1->p[i].set);
1392 for (j = 0; j < pwaff2->n; ++j) {
1393 struct isl_set *common;
1396 common = isl_set_intersect(
1397 isl_set_copy(pwaff1->p[i].set),
1398 isl_set_copy(pwaff2->p[j].set));
1399 better = isl_set_from_basic_set(cmp(
1400 isl_aff_copy(pwaff2->p[j].aff),
1401 isl_aff_copy(pwaff1->p[i].aff)));
1402 better = isl_set_intersect(common, better);
1403 if (isl_set_plain_is_empty(better)) {
1404 isl_set_free(better);
1407 set = isl_set_subtract(set, isl_set_copy(better));
1409 res = isl_pw_aff_add_piece(res, better,
1410 isl_aff_copy(pwaff2->p[j].aff));
1412 res = isl_pw_aff_add_piece(res, set,
1413 isl_aff_copy(pwaff1->p[i].aff));
1416 for (j = 0; j < pwaff2->n; ++j) {
1417 set = isl_set_copy(pwaff2->p[j].set);
1418 for (i = 0; i < pwaff1->n; ++i)
1419 set = isl_set_subtract(set,
1420 isl_set_copy(pwaff1->p[i].set));
1421 res = isl_pw_aff_add_piece(res, set,
1422 isl_aff_copy(pwaff2->p[j].aff));
1425 isl_pw_aff_free(pwaff1);
1426 isl_pw_aff_free(pwaff2);
1430 isl_pw_aff_free(pwaff1);
1431 isl_pw_aff_free(pwaff2);
1435 /* Compute a piecewise quasi-affine expression with a domain that
1436 * is the union of those of pwaff1 and pwaff2 and such that on each
1437 * cell, the quasi-affine expression is the maximum of those of pwaff1
1438 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1439 * cell, then the associated expression is the defined one.
1441 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1442 __isl_take isl_pw_aff *pwaff2)
1444 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1447 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1448 __isl_take isl_pw_aff *pwaff2)
1450 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1454 /* Compute a piecewise quasi-affine expression with a domain that
1455 * is the union of those of pwaff1 and pwaff2 and such that on each
1456 * cell, the quasi-affine expression is the minimum of those of pwaff1
1457 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1458 * cell, then the associated expression is the defined one.
1460 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1461 __isl_take isl_pw_aff *pwaff2)
1463 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1466 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1467 __isl_take isl_pw_aff *pwaff2)
1469 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1473 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1474 __isl_take isl_pw_aff *pwaff2, int max)
1477 return isl_pw_aff_union_max(pwaff1, pwaff2);
1479 return isl_pw_aff_union_min(pwaff1, pwaff2);
1482 /* Construct a map with as domain the domain of pwaff and
1483 * one-dimensional range corresponding to the affine expressions.
1485 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1494 dim = isl_pw_aff_get_space(pwaff);
1495 map = isl_map_empty(dim);
1497 for (i = 0; i < pwaff->n; ++i) {
1498 isl_basic_map *bmap;
1501 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1502 map_i = isl_map_from_basic_map(bmap);
1503 map_i = isl_map_intersect_domain(map_i,
1504 isl_set_copy(pwaff->p[i].set));
1505 map = isl_map_union_disjoint(map, map_i);
1508 isl_pw_aff_free(pwaff);
1513 /* Construct a map with as domain the domain of pwaff and
1514 * one-dimensional range corresponding to the affine expressions.
1516 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1520 if (isl_space_is_set(pwaff->dim))
1521 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1522 "space of input is not a map",
1523 return isl_pw_aff_free(pwaff));
1524 return map_from_pw_aff(pwaff);
1527 /* Construct a one-dimensional set with as parameter domain
1528 * the domain of pwaff and the single set dimension
1529 * corresponding to the affine expressions.
1531 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1535 if (!isl_space_is_set(pwaff->dim))
1536 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1537 "space of input is not a set",
1538 return isl_pw_aff_free(pwaff));
1539 return map_from_pw_aff(pwaff);
1542 /* Return a set containing those elements in the domain
1543 * of pwaff where it is non-negative.
1545 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1553 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1555 for (i = 0; i < pwaff->n; ++i) {
1556 isl_basic_set *bset;
1559 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1560 set_i = isl_set_from_basic_set(bset);
1561 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1562 set = isl_set_union_disjoint(set, set_i);
1565 isl_pw_aff_free(pwaff);
1570 /* Return a set containing those elements in the domain
1571 * of pwaff where it is zero (if complement is 0) or not zero
1572 * (if complement is 1).
1574 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1583 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1585 for (i = 0; i < pwaff->n; ++i) {
1586 isl_basic_set *bset;
1587 isl_set *set_i, *zero;
1589 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1590 zero = isl_set_from_basic_set(bset);
1591 set_i = isl_set_copy(pwaff->p[i].set);
1593 set_i = isl_set_subtract(set_i, zero);
1595 set_i = isl_set_intersect(set_i, zero);
1596 set = isl_set_union_disjoint(set, set_i);
1599 isl_pw_aff_free(pwaff);
1604 /* Return a set containing those elements in the domain
1605 * of pwaff where it is zero.
1607 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1609 return pw_aff_zero_set(pwaff, 0);
1612 /* Return a set containing those elements in the domain
1613 * of pwaff where it is not zero.
1615 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1617 return pw_aff_zero_set(pwaff, 1);
1620 /* Return a set containing those elements in the shared domain
1621 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1623 * We compute the difference on the shared domain and then construct
1624 * the set of values where this difference is non-negative.
1625 * If strict is set, we first subtract 1 from the difference.
1626 * If equal is set, we only return the elements where pwaff1 and pwaff2
1629 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1630 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1632 isl_set *set1, *set2;
1634 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1635 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1636 set1 = isl_set_intersect(set1, set2);
1637 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1638 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1639 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1642 isl_space *dim = isl_set_get_space(set1);
1644 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1645 aff = isl_aff_add_constant_si(aff, -1);
1646 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1651 return isl_pw_aff_zero_set(pwaff1);
1652 return isl_pw_aff_nonneg_set(pwaff1);
1655 /* Return a set containing those elements in the shared domain
1656 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1658 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1659 __isl_take isl_pw_aff *pwaff2)
1661 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1664 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1665 __isl_take isl_pw_aff *pwaff2)
1667 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
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 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1674 __isl_take isl_pw_aff *pwaff2)
1676 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1679 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1680 __isl_take isl_pw_aff *pwaff2)
1682 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1685 /* Return a set containing those elements in the shared domain
1686 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1688 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1689 __isl_take isl_pw_aff *pwaff2)
1691 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1694 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1695 __isl_take isl_pw_aff *pwaff2)
1697 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1700 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1701 __isl_take isl_pw_aff *pwaff2)
1703 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1706 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1707 __isl_take isl_pw_aff *pwaff2)
1709 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1712 /* Return a set containing those elements in the shared domain
1713 * of the elements of list1 and list2 where each element in list1
1714 * has the relation specified by "fn" with each element in list2.
1716 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1717 __isl_take isl_pw_aff_list *list2,
1718 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1719 __isl_take isl_pw_aff *pwaff2))
1725 if (!list1 || !list2)
1728 ctx = isl_pw_aff_list_get_ctx(list1);
1729 if (list1->n < 1 || list2->n < 1)
1730 isl_die(ctx, isl_error_invalid,
1731 "list should contain at least one element", goto error);
1733 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1734 for (i = 0; i < list1->n; ++i)
1735 for (j = 0; j < list2->n; ++j) {
1738 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1739 isl_pw_aff_copy(list2->p[j]));
1740 set = isl_set_intersect(set, set_ij);
1743 isl_pw_aff_list_free(list1);
1744 isl_pw_aff_list_free(list2);
1747 isl_pw_aff_list_free(list1);
1748 isl_pw_aff_list_free(list2);
1752 /* Return a set containing those elements in the shared domain
1753 * of the elements of list1 and list2 where each element in list1
1754 * is equal to each element in list2.
1756 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1757 __isl_take isl_pw_aff_list *list2)
1759 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1762 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1763 __isl_take isl_pw_aff_list *list2)
1765 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1768 /* Return a set containing those elements in the shared domain
1769 * of the elements of list1 and list2 where each element in list1
1770 * is less than or equal to each element in list2.
1772 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1773 __isl_take isl_pw_aff_list *list2)
1775 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1778 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1779 __isl_take isl_pw_aff_list *list2)
1781 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1784 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1785 __isl_take isl_pw_aff_list *list2)
1787 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1790 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1791 __isl_take isl_pw_aff_list *list2)
1793 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1797 /* Return a set containing those elements in the shared domain
1798 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1800 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1801 __isl_take isl_pw_aff *pwaff2)
1803 isl_set *set_lt, *set_gt;
1805 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1806 isl_pw_aff_copy(pwaff2));
1807 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1808 return isl_set_union_disjoint(set_lt, set_gt);
1811 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1812 __isl_take isl_pw_aff *pwaff2)
1814 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1817 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1822 if (isl_int_is_one(v))
1824 if (!isl_int_is_pos(v))
1825 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1826 "factor needs to be positive",
1827 return isl_pw_aff_free(pwaff));
1828 pwaff = isl_pw_aff_cow(pwaff);
1834 for (i = 0; i < pwaff->n; ++i) {
1835 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1836 if (!pwaff->p[i].aff)
1837 return isl_pw_aff_free(pwaff);
1843 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1847 pwaff = isl_pw_aff_cow(pwaff);
1853 for (i = 0; i < pwaff->n; ++i) {
1854 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1855 if (!pwaff->p[i].aff)
1856 return isl_pw_aff_free(pwaff);
1862 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1866 pwaff = isl_pw_aff_cow(pwaff);
1872 for (i = 0; i < pwaff->n; ++i) {
1873 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1874 if (!pwaff->p[i].aff)
1875 return isl_pw_aff_free(pwaff);
1881 /* Return an affine expression that is equal to pwaff_true for elements
1882 * in "cond" and to pwaff_false for elements not in "cond".
1883 * That is, return cond ? pwaff_true : pwaff_false;
1885 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1886 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1890 comp = isl_set_complement(isl_set_copy(cond));
1891 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1892 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1894 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1897 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1902 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1905 /* Check whether pwaff is a piecewise constant.
1907 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1914 for (i = 0; i < pwaff->n; ++i) {
1915 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1916 if (is_cst < 0 || !is_cst)
1923 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1924 __isl_take isl_aff *aff2)
1926 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1927 return isl_aff_mul(aff2, aff1);
1929 if (!isl_aff_is_cst(aff2))
1930 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1931 "at least one affine expression should be constant",
1934 aff1 = isl_aff_cow(aff1);
1938 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1939 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1949 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1950 __isl_take isl_pw_aff *pwaff2)
1952 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1955 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1956 __isl_take isl_pw_aff *pwaff2)
1958 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1961 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1962 __isl_take isl_pw_aff *pwaff2)
1964 return isl_pw_aff_union_add_(pwaff1, pwaff2);
1967 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1968 __isl_take isl_pw_aff *pwaff2)
1970 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
1973 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1974 __isl_take isl_pw_aff *pwaff2)
1976 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1979 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1980 __isl_take isl_pw_aff *pwaff2)
1984 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1985 isl_pw_aff_copy(pwaff2));
1986 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1989 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1990 __isl_take isl_pw_aff *pwaff2)
1992 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1995 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1996 __isl_take isl_pw_aff *pwaff2)
2000 le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2001 isl_pw_aff_copy(pwaff2));
2002 return isl_pw_aff_cond(le, pwaff1, pwaff2);
2005 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2006 __isl_take isl_pw_aff *pwaff2)
2008 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2011 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2012 __isl_take isl_pw_aff_list *list,
2013 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2014 __isl_take isl_pw_aff *pwaff2))
2023 ctx = isl_pw_aff_list_get_ctx(list);
2025 isl_die(ctx, isl_error_invalid,
2026 "list should contain at least one element",
2027 return isl_pw_aff_list_free(list));
2029 res = isl_pw_aff_copy(list->p[0]);
2030 for (i = 1; i < list->n; ++i)
2031 res = fn(res, isl_pw_aff_copy(list->p[i]));
2033 isl_pw_aff_list_free(list);
2037 /* Return an isl_pw_aff that maps each element in the intersection of the
2038 * domains of the elements of list to the minimal corresponding affine
2041 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2043 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2046 /* Return an isl_pw_aff that maps each element in the intersection of the
2047 * domains of the elements of list to the maximal corresponding affine
2050 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2052 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2058 #include <isl_multi_templ.c>
2060 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2061 __isl_take isl_multi_aff *maff2)
2066 maff1 = isl_multi_aff_cow(maff1);
2067 if (!maff1 || !maff2)
2070 ctx = isl_multi_aff_get_ctx(maff1);
2071 if (!isl_space_is_equal(maff1->space, maff2->space))
2072 isl_die(ctx, isl_error_invalid,
2073 "spaces don't match", goto error);
2075 for (i = 0; i < maff1->n; ++i) {
2076 maff1->p[i] = isl_aff_add(maff1->p[i],
2077 isl_aff_copy(maff2->p[i]));
2082 isl_multi_aff_free(maff2);
2085 isl_multi_aff_free(maff1);
2086 isl_multi_aff_free(maff2);
2090 /* Exploit the equalities in "eq" to simplify the affine expressions.
2092 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2093 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2097 maff = isl_multi_aff_cow(maff);
2101 for (i = 0; i < maff->n; ++i) {
2102 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2103 isl_basic_set_copy(eq));
2108 isl_basic_set_free(eq);
2111 isl_basic_set_free(eq);
2112 isl_multi_aff_free(maff);
2116 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2121 maff = isl_multi_aff_cow(maff);
2125 for (i = 0; i < maff->n; ++i) {
2126 maff->p[i] = isl_aff_scale(maff->p[i], f);
2128 return isl_multi_aff_free(maff);
2134 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2135 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2137 maff1 = isl_multi_aff_add(maff1, maff2);
2138 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2142 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2150 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2151 __isl_keep isl_multi_aff *maff2)
2156 if (!maff1 || !maff2)
2158 if (maff1->n != maff2->n)
2160 equal = isl_space_is_equal(maff1->space, maff2->space);
2161 if (equal < 0 || !equal)
2164 for (i = 0; i < maff1->n; ++i) {
2165 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2166 if (equal < 0 || !equal)
2173 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2174 __isl_take isl_multi_aff *maff,
2175 enum isl_dim_type type, unsigned pos, const char *s)
2179 maff = isl_multi_aff_cow(maff);
2183 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2185 return isl_multi_aff_free(maff);
2186 for (i = 0; i < maff->n; ++i) {
2187 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2189 return isl_multi_aff_free(maff);
2195 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2196 enum isl_dim_type type, unsigned first, unsigned n)
2200 maff = isl_multi_aff_cow(maff);
2204 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2206 return isl_multi_aff_free(maff);
2208 if (type == isl_dim_out) {
2209 for (i = 0; i < n; ++i)
2210 isl_aff_free(maff->p[first + i]);
2211 for (i = first; i + n < maff->n; ++i)
2212 maff->p[i] = maff->p[i + n];
2217 for (i = 0; i < maff->n; ++i) {
2218 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2220 return isl_multi_aff_free(maff);
2227 #define PW isl_pw_multi_aff
2229 #define EL isl_multi_aff
2231 #define EL_IS_ZERO is_empty
2235 #define IS_ZERO is_empty
2238 #undef DEFAULT_IS_ZERO
2239 #define DEFAULT_IS_ZERO 0
2244 #define NO_INVOLVES_DIMS
2245 #define NO_MOVE_DIMS
2246 #define NO_INSERT_DIMS
2250 #include <isl_pw_templ.c>
2252 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2253 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2255 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2256 &isl_multi_aff_add);
2259 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2260 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2262 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2266 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2267 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2269 return isl_pw_multi_aff_union_add_(pma1, pma2);
2272 /* Construct a map mapping the domain the piecewise multi-affine expression
2273 * to its range, with each dimension in the range equated to the
2274 * corresponding affine expression on its cell.
2276 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2284 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2286 for (i = 0; i < pma->n; ++i) {
2287 isl_multi_aff *maff;
2288 isl_basic_map *bmap;
2291 maff = isl_multi_aff_copy(pma->p[i].maff);
2292 bmap = isl_basic_map_from_multi_aff(maff);
2293 map_i = isl_map_from_basic_map(bmap);
2294 map_i = isl_map_intersect_domain(map_i,
2295 isl_set_copy(pma->p[i].set));
2296 map = isl_map_union_disjoint(map, map_i);
2299 isl_pw_multi_aff_free(pma);
2303 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2305 if (!isl_space_is_set(pma->dim))
2306 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2307 "isl_pw_multi_aff cannot be converted into an isl_set",
2308 return isl_pw_multi_aff_free(pma));
2310 return isl_map_from_pw_multi_aff(pma);
2313 /* Given a basic map with a single output dimension that is defined
2314 * in terms of the parameters and input dimensions using an equality,
2315 * extract an isl_aff that expresses the output dimension in terms
2316 * of the parameters and input dimensions.
2318 * Since some applications expect the result of isl_pw_multi_aff_from_map
2319 * to only contain integer affine expressions, we compute the floor
2320 * of the expression before returning.
2322 * This function shares some similarities with
2323 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2325 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
2326 __isl_take isl_basic_map *bmap)
2331 isl_local_space *ls;
2336 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
2337 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2338 "basic map should have a single output dimension",
2340 offset = isl_basic_map_offset(bmap, isl_dim_out);
2341 total = isl_basic_map_total_dim(bmap);
2342 for (i = 0; i < bmap->n_eq; ++i) {
2343 if (isl_int_is_zero(bmap->eq[i][offset]))
2345 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
2346 1 + total - (offset + 1)) != -1)
2350 if (i >= bmap->n_eq)
2351 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2352 "unable to find suitable equality", goto error);
2353 ls = isl_basic_map_get_local_space(bmap);
2354 aff = isl_aff_alloc(isl_local_space_domain(ls));
2357 if (isl_int_is_neg(bmap->eq[i][offset]))
2358 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
2360 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
2361 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
2362 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
2363 isl_basic_map_free(bmap);
2365 aff = isl_aff_remove_unused_divs(aff);
2366 aff = isl_aff_floor(aff);
2369 isl_basic_map_free(bmap);
2373 /* Given a basic map where each output dimension is defined
2374 * in terms of the parameters and input dimensions using an equality,
2375 * extract an isl_multi_aff that expresses the output dimensions in terms
2376 * of the parameters and input dimensions.
2378 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
2379 __isl_take isl_basic_map *bmap)
2388 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
2389 n_out = isl_basic_map_dim(bmap, isl_dim_out);
2391 for (i = 0; i < n_out; ++i) {
2392 isl_basic_map *bmap_i;
2395 bmap_i = isl_basic_map_copy(bmap);
2396 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
2397 i + 1, n_out - (1 + i));
2398 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
2399 aff = extract_isl_aff_from_basic_map(bmap_i);
2400 ma = isl_multi_aff_set_aff(ma, i, aff);
2403 isl_basic_map_free(bmap);
2408 /* Create an isl_pw_multi_aff that is equivalent to
2409 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2410 * The given basic map is such that each output dimension is defined
2411 * in terms of the parameters and input dimensions using an equality.
2413 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
2414 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
2418 ma = extract_isl_multi_aff_from_basic_map(bmap);
2419 return isl_pw_multi_aff_alloc(domain, ma);
2422 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2423 * This obivously only works if the input "map" is single-valued.
2424 * If so, we compute the lexicographic minimum of the image in the form
2425 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2426 * to its lexicographic minimum.
2427 * If the input is not single-valued, we produce an error.
2429 * As a special case, we first check if all output dimensions are uniquely
2430 * defined in terms of the parameters and input dimensions over the entire
2431 * domain. If so, we extract the desired isl_pw_multi_aff directly
2432 * from the affine hull of "map" and its domain.
2434 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2438 isl_pw_multi_aff *pma;
2439 isl_basic_map *hull;
2444 hull = isl_map_affine_hull(isl_map_copy(map));
2445 sv = isl_basic_map_plain_is_single_valued(hull);
2447 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
2448 isl_basic_map_free(hull);
2452 sv = isl_map_is_single_valued(map);
2456 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2457 "map is not single-valued", goto error);
2458 map = isl_map_make_disjoint(map);
2462 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2464 for (i = 0; i < map->n; ++i) {
2465 isl_pw_multi_aff *pma_i;
2466 isl_basic_map *bmap;
2467 bmap = isl_basic_map_copy(map->p[i]);
2468 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2469 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2479 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2481 return isl_pw_multi_aff_from_map(set);
2484 /* Plug in "subs" for dimension "type", "pos" of "aff".
2486 * Let i be the dimension to replace and let "subs" be of the form
2490 * and "aff" of the form
2496 * floor((a f + d g')/(m d))
2498 * where g' is the result of plugging in "subs" in each of the integer
2501 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2502 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2507 aff = isl_aff_cow(aff);
2509 return isl_aff_free(aff);
2511 ctx = isl_aff_get_ctx(aff);
2512 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2513 isl_die(ctx, isl_error_invalid,
2514 "spaces don't match", return isl_aff_free(aff));
2515 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2516 isl_die(ctx, isl_error_unsupported,
2517 "cannot handle divs yet", return isl_aff_free(aff));
2519 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2521 return isl_aff_free(aff);
2523 aff->v = isl_vec_cow(aff->v);
2525 return isl_aff_free(aff);
2527 pos += isl_local_space_offset(aff->ls, type);
2530 isl_int_set(v, aff->v->el[1 + pos]);
2531 isl_int_set_si(aff->v->el[1 + pos], 0);
2532 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2533 v, subs->v->el + 1, subs->v->size - 1);
2534 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2540 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2541 * expressions in "maff".
2543 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2544 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2545 __isl_keep isl_aff *subs)
2549 maff = isl_multi_aff_cow(maff);
2551 return isl_multi_aff_free(maff);
2553 if (type == isl_dim_in)
2556 for (i = 0; i < maff->n; ++i) {
2557 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2559 return isl_multi_aff_free(maff);
2565 /* Plug in "subs" for dimension "type", "pos" of "pma".
2567 * pma is of the form
2571 * while subs is of the form
2573 * v' = B_j(v) -> S_j
2575 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2576 * has a contribution in the result, in particular
2578 * C_ij(S_j) -> M_i(S_j)
2580 * Note that plugging in S_j in C_ij may also result in an empty set
2581 * and this contribution should simply be discarded.
2583 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2584 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2585 __isl_keep isl_pw_aff *subs)
2588 isl_pw_multi_aff *res;
2591 return isl_pw_multi_aff_free(pma);
2593 n = pma->n * subs->n;
2594 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2596 for (i = 0; i < pma->n; ++i) {
2597 for (j = 0; j < subs->n; ++j) {
2599 isl_multi_aff *res_ij;
2600 common = isl_set_intersect(
2601 isl_set_copy(pma->p[i].set),
2602 isl_set_copy(subs->p[j].set));
2603 common = isl_set_substitute(common,
2604 type, pos, subs->p[j].aff);
2605 if (isl_set_plain_is_empty(common)) {
2606 isl_set_free(common);
2610 res_ij = isl_multi_aff_substitute(
2611 isl_multi_aff_copy(pma->p[i].maff),
2612 type, pos, subs->p[j].aff);
2614 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2618 isl_pw_multi_aff_free(pma);
2622 /* Extend the local space of "dst" to include the divs
2623 * in the local space of "src".
2625 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2626 __isl_keep isl_aff *src)
2634 return isl_aff_free(dst);
2636 ctx = isl_aff_get_ctx(src);
2637 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2638 isl_die(ctx, isl_error_invalid,
2639 "spaces don't match", goto error);
2641 if (src->ls->div->n_row == 0)
2644 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2645 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2649 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2650 dst = isl_aff_expand_divs(dst, div, exp2);
2658 return isl_aff_free(dst);
2661 /* Adjust the local spaces of the affine expressions in "maff"
2662 * such that they all have the save divs.
2664 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2665 __isl_take isl_multi_aff *maff)
2673 maff = isl_multi_aff_cow(maff);
2677 for (i = 1; i < maff->n; ++i)
2678 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2679 for (i = 1; i < maff->n; ++i) {
2680 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2682 return isl_multi_aff_free(maff);
2688 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2690 aff = isl_aff_cow(aff);
2694 aff->ls = isl_local_space_lift(aff->ls);
2696 return isl_aff_free(aff);
2701 /* Lift "maff" to a space with extra dimensions such that the result
2702 * has no more existentially quantified variables.
2703 * If "ls" is not NULL, then *ls is assigned the local space that lies
2704 * at the basis of the lifting applied to "maff".
2706 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2707 __isl_give isl_local_space **ls)
2721 isl_space *space = isl_multi_aff_get_domain_space(maff);
2722 *ls = isl_local_space_from_space(space);
2724 return isl_multi_aff_free(maff);
2729 maff = isl_multi_aff_cow(maff);
2730 maff = isl_multi_aff_align_divs(maff);
2734 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2735 space = isl_multi_aff_get_space(maff);
2736 space = isl_space_lift(isl_space_domain(space), n_div);
2737 space = isl_space_extend_domain_with_range(space,
2738 isl_multi_aff_get_space(maff));
2740 return isl_multi_aff_free(maff);
2741 isl_space_free(maff->space);
2742 maff->space = space;
2745 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2747 return isl_multi_aff_free(maff);
2750 for (i = 0; i < maff->n; ++i) {
2751 maff->p[i] = isl_aff_lift(maff->p[i]);
2759 isl_local_space_free(*ls);
2760 return isl_multi_aff_free(maff);
2764 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2766 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2767 __isl_keep isl_pw_multi_aff *pma, int pos)
2777 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2778 if (pos < 0 || pos >= n_out)
2779 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2780 "index out of bounds", return NULL);
2782 space = isl_pw_multi_aff_get_space(pma);
2783 space = isl_space_drop_dims(space, isl_dim_out,
2784 pos + 1, n_out - pos - 1);
2785 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2787 pa = isl_pw_aff_alloc_size(space, pma->n);
2788 for (i = 0; i < pma->n; ++i) {
2790 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2791 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);