2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_map_private.h>
12 #include <isl_morph.h>
14 #include <isl_mat_private.h>
15 #include <isl_dim_private.h>
16 #include <isl_equalities.h>
18 __isl_give isl_morph *isl_morph_alloc(
19 __isl_take isl_basic_set *dom, __isl_take isl_basic_set *ran,
20 __isl_take isl_mat *map, __isl_take isl_mat *inv)
24 if (!dom || !ran || !map || !inv)
27 morph = isl_alloc_type(dom->ctx, struct isl_morph);
39 isl_basic_set_free(dom);
40 isl_basic_set_free(ran);
46 __isl_give isl_morph *isl_morph_copy(__isl_keep isl_morph *morph)
55 __isl_give isl_morph *isl_morph_dup(__isl_keep isl_morph *morph)
60 return isl_morph_alloc(isl_basic_set_copy(morph->dom),
61 isl_basic_set_copy(morph->ran),
62 isl_mat_copy(morph->map), isl_mat_copy(morph->inv));
65 __isl_give isl_morph *isl_morph_cow(__isl_take isl_morph *morph)
73 return isl_morph_dup(morph);
76 void isl_morph_free(__isl_take isl_morph *morph)
84 isl_basic_set_free(morph->dom);
85 isl_basic_set_free(morph->ran);
86 isl_mat_free(morph->map);
87 isl_mat_free(morph->inv);
91 __isl_give isl_dim *isl_morph_get_ran_dim(__isl_keep isl_morph *morph)
96 return isl_dim_copy(morph->ran->dim);
99 unsigned isl_morph_dom_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
104 return isl_basic_set_dim(morph->dom, type);
107 unsigned isl_morph_ran_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
112 return isl_basic_set_dim(morph->ran, type);
115 __isl_give isl_morph *isl_morph_remove_dom_dims(__isl_take isl_morph *morph,
116 enum isl_dim_type type, unsigned first, unsigned n)
123 morph = isl_morph_cow(morph);
127 dom_offset = 1 + isl_dim_offset(morph->dom->dim, type);
129 morph->dom = isl_basic_set_remove_dims(morph->dom, type, first, n);
131 morph->map = isl_mat_drop_cols(morph->map, dom_offset + first, n);
133 morph->inv = isl_mat_drop_rows(morph->inv, dom_offset + first, n);
135 if (morph->dom && morph->ran && morph->map && morph->inv)
138 isl_morph_free(morph);
142 __isl_give isl_morph *isl_morph_remove_ran_dims(__isl_take isl_morph *morph,
143 enum isl_dim_type type, unsigned first, unsigned n)
150 morph = isl_morph_cow(morph);
154 ran_offset = 1 + isl_dim_offset(morph->ran->dim, type);
156 morph->ran = isl_basic_set_remove_dims(morph->ran, type, first, n);
158 morph->map = isl_mat_drop_rows(morph->map, ran_offset + first, n);
160 morph->inv = isl_mat_drop_cols(morph->inv, ran_offset + first, n);
162 if (morph->dom && morph->ran && morph->map && morph->inv)
165 isl_morph_free(morph);
169 void isl_morph_dump(__isl_take isl_morph *morph, FILE *out)
174 isl_basic_set_print(morph->dom, out, 0, "", "", ISL_FORMAT_ISL);
175 isl_basic_set_print(morph->ran, out, 0, "", "", ISL_FORMAT_ISL);
176 isl_mat_print_internal(morph->map, out, 4);
177 isl_mat_print_internal(morph->inv, out, 4);
180 __isl_give isl_morph *isl_morph_identity(__isl_keep isl_basic_set *bset)
183 isl_basic_set *universe;
189 total = isl_basic_set_total_dim(bset);
190 id = isl_mat_identity(bset->ctx, 1 + total);
191 universe = isl_basic_set_universe(isl_dim_copy(bset->dim));
193 return isl_morph_alloc(universe, isl_basic_set_copy(universe),
194 id, isl_mat_copy(id));
197 /* Create a(n identity) morphism between empty sets of the same dimension
200 __isl_give isl_morph *isl_morph_empty(__isl_keep isl_basic_set *bset)
203 isl_basic_set *empty;
209 total = isl_basic_set_total_dim(bset);
210 id = isl_mat_identity(bset->ctx, 1 + total);
211 empty = isl_basic_set_empty(isl_dim_copy(bset->dim));
213 return isl_morph_alloc(empty, isl_basic_set_copy(empty),
214 id, isl_mat_copy(id));
217 /* Given a matrix that maps a (possibly) parametric domain to
218 * a parametric domain, add in rows that map the "nparam" parameters onto
221 static __isl_give isl_mat *insert_parameter_rows(__isl_take isl_mat *mat,
231 mat = isl_mat_insert_rows(mat, 1, nparam);
235 for (i = 0; i < nparam; ++i) {
236 isl_seq_clr(mat->row[1 + i], mat->n_col);
237 isl_int_set(mat->row[1 + i][1 + i], mat->row[0][0]);
243 /* Construct a basic set described by the "n" equalities of "bset" starting
246 static __isl_give isl_basic_set *copy_equalities(__isl_keep isl_basic_set *bset,
247 unsigned first, unsigned n)
253 isl_assert(bset->ctx, bset->n_div == 0, return NULL);
255 total = isl_basic_set_total_dim(bset);
256 eq = isl_basic_set_alloc_dim(isl_dim_copy(bset->dim), 0, n, 0);
259 for (i = 0; i < n; ++i) {
260 k = isl_basic_set_alloc_equality(eq);
263 isl_seq_cpy(eq->eq[k], bset->eq[first + k], 1 + total);
268 isl_basic_set_free(eq);
272 /* Given a basic set, exploit the equalties in the a basic set to construct
273 * a morphishm that maps the basic set to a lower-dimensional space.
274 * Specifically, the morphism reduces the number of dimensions of type "type".
276 * This function is a slight generalization of isl_mat_variable_compression
277 * in that it allows the input to be parametric and that it allows for the
278 * compression of either parameters or set variables.
280 * We first select the equalities of interest, that is those that involve
281 * variables of type "type" and no later variables.
282 * Denote those equalities as
286 * where C(p) depends on the parameters if type == isl_dim_set and
287 * is a constant if type == isl_dim_param.
289 * First compute the (left) Hermite normal form of M,
291 * M [U1 U2] = M U = H = [H1 0]
293 * M = H Q = [H1 0] [Q1]
296 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
297 * Define the transformed variables as
299 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
302 * The equalities then become
304 * -C(p) + H1 x1' = 0 or x1' = H1^{-1} C(p) = C'(p)
306 * If the denominator of the constant term does not divide the
307 * the common denominator of the parametric terms, then every
308 * integer point is mapped to a non-integer point and then the original set has no
309 * integer solutions (since the x' are a unimodular transformation
310 * of the x). In this case, an empty morphism is returned.
311 * Otherwise, the transformation is given by
313 * x = U1 H1^{-1} C(p) + U2 x2'
315 * The inverse transformation is simply
319 * Both matrices are extended to map the full original space to the full
322 __isl_give isl_morph *isl_basic_set_variable_compression(
323 __isl_keep isl_basic_set *bset, enum isl_dim_type type)
331 isl_mat *H, *U, *Q, *C = NULL, *H1, *U1, *U2;
332 isl_basic_set *dom, *ran;
337 if (isl_basic_set_plain_is_empty(bset))
338 return isl_morph_empty(bset);
340 isl_assert(bset->ctx, bset->n_div == 0, return NULL);
342 otype = 1 + isl_dim_offset(bset->dim, type);
343 ntype = isl_basic_set_dim(bset, type);
344 orest = otype + ntype;
345 nrest = isl_basic_set_total_dim(bset) - (orest - 1);
347 for (f_eq = 0; f_eq < bset->n_eq; ++f_eq)
348 if (isl_seq_first_non_zero(bset->eq[f_eq] + orest, nrest) == -1)
350 for (n_eq = 0; f_eq + n_eq < bset->n_eq; ++n_eq)
351 if (isl_seq_first_non_zero(bset->eq[f_eq + n_eq] + otype, ntype) == -1)
354 return isl_morph_identity(bset);
356 H = isl_mat_sub_alloc6(bset->ctx, bset->eq, f_eq, n_eq, otype, ntype);
357 H = isl_mat_left_hermite(H, 0, &U, &Q);
360 Q = isl_mat_drop_rows(Q, 0, n_eq);
361 Q = isl_mat_diagonal(isl_mat_identity(bset->ctx, otype), Q);
362 Q = isl_mat_diagonal(Q, isl_mat_identity(bset->ctx, nrest));
363 C = isl_mat_alloc(bset->ctx, 1 + n_eq, otype);
366 isl_int_set_si(C->row[0][0], 1);
367 isl_seq_clr(C->row[0] + 1, otype - 1);
368 isl_mat_sub_neg(C->ctx, C->row + 1, bset->eq + f_eq, n_eq, 0, 0, otype);
369 H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row);
370 H1 = isl_mat_lin_to_aff(H1);
371 C = isl_mat_inverse_product(H1, C);
376 if (!isl_int_is_one(C->row[0][0])) {
381 for (i = 0; i < n_eq; ++i) {
382 isl_seq_gcd(C->row[1 + i] + 1, otype - 1, &g);
383 isl_int_gcd(g, g, C->row[0][0]);
384 if (!isl_int_is_divisible_by(C->row[1 + i][0], g))
393 return isl_morph_empty(bset);
396 C = isl_mat_normalize(C);
399 U1 = isl_mat_sub_alloc(U, 0, U->n_row, 0, n_eq);
400 U1 = isl_mat_lin_to_aff(U1);
401 U2 = isl_mat_sub_alloc(U, 0, U->n_row, n_eq, U->n_row - n_eq);
402 U2 = isl_mat_lin_to_aff(U2);
405 C = isl_mat_product(U1, C);
406 C = isl_mat_aff_direct_sum(C, U2);
407 C = insert_parameter_rows(C, otype - 1);
408 C = isl_mat_diagonal(C, isl_mat_identity(bset->ctx, nrest));
410 dim = isl_dim_copy(bset->dim);
411 dim = isl_dim_drop(dim, type, 0, ntype);
412 dim = isl_dim_add(dim, type, ntype - n_eq);
413 ran = isl_basic_set_universe(dim);
414 dom = copy_equalities(bset, f_eq, n_eq);
416 return isl_morph_alloc(dom, ran, Q, C);
425 /* Construct a parameter compression for "bset".
426 * We basically just call isl_mat_parameter_compression with the right input
427 * and then extend the resulting matrix to include the variables.
429 * Let the equalities be given as
433 * and let [H 0] be the Hermite Normal Form of A, then
437 * needs to be integer, so we impose that each row is divisible by
440 __isl_give isl_morph *isl_basic_set_parameter_compression(
441 __isl_keep isl_basic_set *bset)
449 isl_basic_set *dom, *ran;
454 if (isl_basic_set_plain_is_empty(bset))
455 return isl_morph_empty(bset);
457 return isl_morph_identity(bset);
459 isl_assert(bset->ctx, bset->n_div == 0, return NULL);
462 nparam = isl_basic_set_dim(bset, isl_dim_param);
463 nvar = isl_basic_set_dim(bset, isl_dim_set);
465 isl_assert(bset->ctx, n_eq <= nvar, return NULL);
467 d = isl_vec_alloc(bset->ctx, n_eq);
468 B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, n_eq, 0, 1 + nparam);
469 H = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, n_eq, 1 + nparam, nvar);
470 H = isl_mat_left_hermite(H, 0, NULL, NULL);
471 H = isl_mat_drop_cols(H, n_eq, nvar - n_eq);
472 H = isl_mat_lin_to_aff(H);
473 H = isl_mat_right_inverse(H);
476 isl_seq_set(d->el, H->row[0][0], d->size);
477 H = isl_mat_drop_rows(H, 0, 1);
478 H = isl_mat_drop_cols(H, 0, 1);
479 B = isl_mat_product(H, B);
480 inv = isl_mat_parameter_compression(B, d);
481 inv = isl_mat_diagonal(inv, isl_mat_identity(bset->ctx, nvar));
482 map = isl_mat_right_inverse(isl_mat_copy(inv));
484 dom = isl_basic_set_universe(isl_dim_copy(bset->dim));
485 ran = isl_basic_set_universe(isl_dim_copy(bset->dim));
487 return isl_morph_alloc(dom, ran, map, inv);
495 /* Add stride constraints to "bset" based on the inverse mapping
496 * that was plugged in. In particular, if morph maps x' to x,
497 * the the constraints of the original input
501 * have been rewritten to
505 * However, this substitution may loose information on the integrality of x',
506 * so we need to impose that
510 * is integral. If inv = B/d, this means that we need to impose that
516 * exists alpha in Z^m: B x = d alpha
519 static __isl_give isl_basic_set *add_strides(__isl_take isl_basic_set *bset,
520 __isl_keep isl_morph *morph)
525 if (isl_int_is_one(morph->inv->row[0][0]))
530 for (i = 0; 1 + i < morph->inv->n_row; ++i) {
531 isl_seq_gcd(morph->inv->row[1 + i], morph->inv->n_col, &gcd);
532 if (isl_int_is_divisible_by(gcd, morph->inv->row[0][0]))
534 div = isl_basic_set_alloc_div(bset);
537 k = isl_basic_set_alloc_equality(bset);
540 isl_seq_cpy(bset->eq[k], morph->inv->row[1 + i],
542 isl_seq_clr(bset->eq[k] + morph->inv->n_col, bset->n_div);
543 isl_int_set(bset->eq[k][morph->inv->n_col + div],
544 morph->inv->row[0][0]);
552 isl_basic_set_free(bset);
556 /* Apply the morphism to the basic set.
557 * We basically just compute the preimage of "bset" under the inverse mapping
558 * in morph, add in stride constraints and intersect with the range
561 __isl_give isl_basic_set *isl_morph_basic_set(__isl_take isl_morph *morph,
562 __isl_take isl_basic_set *bset)
564 isl_basic_set *res = NULL;
572 isl_assert(bset->ctx, isl_dim_equal(bset->dim, morph->dom->dim),
575 max_stride = morph->inv->n_row - 1;
576 if (isl_int_is_one(morph->inv->row[0][0]))
578 res = isl_basic_set_alloc_dim(isl_dim_copy(morph->ran->dim),
579 bset->n_div + max_stride, bset->n_eq + max_stride, bset->n_ineq);
581 for (i = 0; i < bset->n_div; ++i)
582 if (isl_basic_set_alloc_div(res) < 0)
585 mat = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq,
586 0, morph->inv->n_row);
587 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
590 for (i = 0; i < bset->n_eq; ++i) {
591 k = isl_basic_set_alloc_equality(res);
594 isl_seq_cpy(res->eq[k], mat->row[i], mat->n_col);
595 isl_seq_scale(res->eq[k] + mat->n_col, bset->eq[i] + mat->n_col,
596 morph->inv->row[0][0], bset->n_div);
600 mat = isl_mat_sub_alloc6(bset->ctx, bset->ineq, 0, bset->n_ineq,
601 0, morph->inv->n_row);
602 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
605 for (i = 0; i < bset->n_ineq; ++i) {
606 k = isl_basic_set_alloc_inequality(res);
609 isl_seq_cpy(res->ineq[k], mat->row[i], mat->n_col);
610 isl_seq_scale(res->ineq[k] + mat->n_col,
611 bset->ineq[i] + mat->n_col,
612 morph->inv->row[0][0], bset->n_div);
616 mat = isl_mat_sub_alloc6(bset->ctx, bset->div, 0, bset->n_div,
617 1, morph->inv->n_row);
618 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
621 for (i = 0; i < bset->n_div; ++i) {
622 isl_int_mul(res->div[i][0],
623 morph->inv->row[0][0], bset->div[i][0]);
624 isl_seq_cpy(res->div[i] + 1, mat->row[i], mat->n_col);
625 isl_seq_scale(res->div[i] + 1 + mat->n_col,
626 bset->div[i] + 1 + mat->n_col,
627 morph->inv->row[0][0], bset->n_div);
631 res = add_strides(res, morph);
633 if (isl_basic_set_is_rational(bset))
634 res = isl_basic_set_set_rational(res);
636 res = isl_basic_set_simplify(res);
637 res = isl_basic_set_finalize(res);
639 res = isl_basic_set_intersect(res, isl_basic_set_copy(morph->ran));
641 isl_morph_free(morph);
642 isl_basic_set_free(bset);
646 isl_morph_free(morph);
647 isl_basic_set_free(bset);
648 isl_basic_set_free(res);
652 /* Apply the morphism to the set.
654 __isl_give isl_set *isl_morph_set(__isl_take isl_morph *morph,
655 __isl_take isl_set *set)
662 isl_assert(set->ctx, isl_dim_equal(set->dim, morph->dom->dim), goto error);
664 set = isl_set_cow(set);
668 isl_dim_free(set->dim);
669 set->dim = isl_dim_copy(morph->ran->dim);
673 for (i = 0; i < set->n; ++i) {
674 set->p[i] = isl_morph_basic_set(isl_morph_copy(morph), set->p[i]);
679 isl_morph_free(morph);
681 ISL_F_CLR(set, ISL_SET_NORMALIZED);
686 isl_morph_free(morph);
690 /* Construct a morphism that first does morph2 and then morph1.
692 __isl_give isl_morph *isl_morph_compose(__isl_take isl_morph *morph1,
693 __isl_take isl_morph *morph2)
696 isl_basic_set *dom, *ran;
698 if (!morph1 || !morph2)
701 map = isl_mat_product(isl_mat_copy(morph1->map), isl_mat_copy(morph2->map));
702 inv = isl_mat_product(isl_mat_copy(morph2->inv), isl_mat_copy(morph1->inv));
703 dom = isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2)),
704 isl_basic_set_copy(morph1->dom));
705 dom = isl_basic_set_intersect(dom, isl_basic_set_copy(morph2->dom));
706 ran = isl_morph_basic_set(isl_morph_copy(morph1),
707 isl_basic_set_copy(morph2->ran));
708 ran = isl_basic_set_intersect(ran, isl_basic_set_copy(morph1->ran));
710 isl_morph_free(morph1);
711 isl_morph_free(morph2);
713 return isl_morph_alloc(dom, ran, map, inv);
715 isl_morph_free(morph1);
716 isl_morph_free(morph2);
720 __isl_give isl_morph *isl_morph_inverse(__isl_take isl_morph *morph)
725 morph = isl_morph_cow(morph);
730 morph->dom = morph->ran;
734 morph->map = morph->inv;
740 __isl_give isl_morph *isl_basic_set_full_compression(
741 __isl_keep isl_basic_set *bset)
743 isl_morph *morph, *morph2;
745 bset = isl_basic_set_copy(bset);
747 morph = isl_basic_set_variable_compression(bset, isl_dim_param);
748 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
750 morph2 = isl_basic_set_parameter_compression(bset);
751 bset = isl_morph_basic_set(isl_morph_copy(morph2), bset);
753 morph = isl_morph_compose(morph2, morph);
755 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
756 isl_basic_set_free(bset);
758 morph = isl_morph_compose(morph2, morph);
763 __isl_give isl_vec *isl_morph_vec(__isl_take isl_morph *morph,
764 __isl_take isl_vec *vec)
769 vec = isl_mat_vec_product(isl_mat_copy(morph->map), vec);
771 isl_morph_free(morph);
774 isl_morph_free(morph);