2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_map_private.h>
14 #include <isl_aff_private.h>
15 #include <isl_morph.h>
17 #include <isl_mat_private.h>
18 #include <isl_space_private.h>
19 #include <isl_equalities.h>
21 isl_ctx
*isl_morph_get_ctx(__isl_keep isl_morph
*morph
)
25 return isl_basic_set_get_ctx(morph
->dom
);
28 __isl_give isl_morph
*isl_morph_alloc(
29 __isl_take isl_basic_set
*dom
, __isl_take isl_basic_set
*ran
,
30 __isl_take isl_mat
*map
, __isl_take isl_mat
*inv
)
34 if (!dom
|| !ran
|| !map
|| !inv
)
37 morph
= isl_alloc_type(dom
->ctx
, struct isl_morph
);
49 isl_basic_set_free(dom
);
50 isl_basic_set_free(ran
);
56 __isl_give isl_morph
*isl_morph_copy(__isl_keep isl_morph
*morph
)
65 __isl_give isl_morph
*isl_morph_dup(__isl_keep isl_morph
*morph
)
70 return isl_morph_alloc(isl_basic_set_copy(morph
->dom
),
71 isl_basic_set_copy(morph
->ran
),
72 isl_mat_copy(morph
->map
), isl_mat_copy(morph
->inv
));
75 __isl_give isl_morph
*isl_morph_cow(__isl_take isl_morph
*morph
)
83 return isl_morph_dup(morph
);
86 __isl_null isl_morph
*isl_morph_free(__isl_take isl_morph
*morph
)
94 isl_basic_set_free(morph
->dom
);
95 isl_basic_set_free(morph
->ran
);
96 isl_mat_free(morph
->map
);
97 isl_mat_free(morph
->inv
);
103 /* Is "morph" an identity on the parameters?
105 static int identity_on_parameters(__isl_keep isl_morph
*morph
)
111 nparam
= isl_morph_dom_dim(morph
, isl_dim_param
);
112 if (nparam
!= isl_morph_ran_dim(morph
, isl_dim_param
))
116 sub
= isl_mat_sub_alloc(morph
->map
, 0, 1 + nparam
, 0, 1 + nparam
);
117 is_identity
= isl_mat_is_scaled_identity(sub
);
123 /* Return an affine expression of the variables of the range of "morph"
124 * in terms of the parameters and the variables of the domain on "morph".
126 * In order for the space manipulations to make sense, we require
127 * that the parameters are not modified by "morph".
129 __isl_give isl_multi_aff
*isl_morph_get_var_multi_aff(
130 __isl_keep isl_morph
*morph
)
132 isl_space
*dom
, *ran
, *space
;
135 unsigned nparam
, nvar
;
142 is_identity
= identity_on_parameters(morph
);
146 isl_die(isl_morph_get_ctx(morph
), isl_error_invalid
,
147 "cannot handle parameter compression", return NULL
);
149 dom
= isl_morph_get_dom_space(morph
);
150 ls
= isl_local_space_from_space(isl_space_copy(dom
));
151 ran
= isl_morph_get_ran_space(morph
);
152 space
= isl_space_map_from_domain_and_range(dom
, ran
);
153 ma
= isl_multi_aff_zero(space
);
155 nparam
= isl_multi_aff_dim(ma
, isl_dim_param
);
156 nvar
= isl_multi_aff_dim(ma
, isl_dim_out
);
157 for (i
= 0; i
< nvar
; ++i
) {
162 v
= isl_mat_get_row(morph
->map
, 1 + nparam
+ i
);
163 v
= isl_vec_insert_els(v
, 0, 1);
164 val
= isl_mat_get_element_val(morph
->map
, 0, 0);
165 v
= isl_vec_set_element_val(v
, 0, val
);
166 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
167 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
170 isl_local_space_free(ls
);
174 /* Return the domain space of "morph".
176 __isl_give isl_space
*isl_morph_get_dom_space(__isl_keep isl_morph
*morph
)
181 return isl_basic_set_get_space(morph
->dom
);
184 __isl_give isl_space
*isl_morph_get_ran_space(__isl_keep isl_morph
*morph
)
189 return isl_space_copy(morph
->ran
->dim
);
192 unsigned isl_morph_dom_dim(__isl_keep isl_morph
*morph
, enum isl_dim_type type
)
197 return isl_basic_set_dim(morph
->dom
, type
);
200 unsigned isl_morph_ran_dim(__isl_keep isl_morph
*morph
, enum isl_dim_type type
)
205 return isl_basic_set_dim(morph
->ran
, type
);
208 __isl_give isl_morph
*isl_morph_remove_dom_dims(__isl_take isl_morph
*morph
,
209 enum isl_dim_type type
, unsigned first
, unsigned n
)
216 morph
= isl_morph_cow(morph
);
220 dom_offset
= 1 + isl_space_offset(morph
->dom
->dim
, type
);
222 morph
->dom
= isl_basic_set_remove_dims(morph
->dom
, type
, first
, n
);
224 morph
->map
= isl_mat_drop_cols(morph
->map
, dom_offset
+ first
, n
);
226 morph
->inv
= isl_mat_drop_rows(morph
->inv
, dom_offset
+ first
, n
);
228 if (morph
->dom
&& morph
->ran
&& morph
->map
&& morph
->inv
)
231 isl_morph_free(morph
);
235 __isl_give isl_morph
*isl_morph_remove_ran_dims(__isl_take isl_morph
*morph
,
236 enum isl_dim_type type
, unsigned first
, unsigned n
)
243 morph
= isl_morph_cow(morph
);
247 ran_offset
= 1 + isl_space_offset(morph
->ran
->dim
, type
);
249 morph
->ran
= isl_basic_set_remove_dims(morph
->ran
, type
, first
, n
);
251 morph
->map
= isl_mat_drop_rows(morph
->map
, ran_offset
+ first
, n
);
253 morph
->inv
= isl_mat_drop_cols(morph
->inv
, ran_offset
+ first
, n
);
255 if (morph
->dom
&& morph
->ran
&& morph
->map
&& morph
->inv
)
258 isl_morph_free(morph
);
262 /* Project domain of morph onto its parameter domain.
264 __isl_give isl_morph
*isl_morph_dom_params(__isl_take isl_morph
*morph
)
268 morph
= isl_morph_cow(morph
);
271 n
= isl_basic_set_dim(morph
->dom
, isl_dim_set
);
272 morph
= isl_morph_remove_dom_dims(morph
, isl_dim_set
, 0, n
);
275 morph
->dom
= isl_basic_set_params(morph
->dom
);
279 isl_morph_free(morph
);
283 /* Project range of morph onto its parameter domain.
285 __isl_give isl_morph
*isl_morph_ran_params(__isl_take isl_morph
*morph
)
289 morph
= isl_morph_cow(morph
);
292 n
= isl_basic_set_dim(morph
->ran
, isl_dim_set
);
293 morph
= isl_morph_remove_ran_dims(morph
, isl_dim_set
, 0, n
);
296 morph
->ran
= isl_basic_set_params(morph
->ran
);
300 isl_morph_free(morph
);
304 void isl_morph_print_internal(__isl_take isl_morph
*morph
, FILE *out
)
309 isl_basic_set_dump(morph
->dom
);
310 isl_basic_set_dump(morph
->ran
);
311 isl_mat_print_internal(morph
->map
, out
, 4);
312 isl_mat_print_internal(morph
->inv
, out
, 4);
315 void isl_morph_dump(__isl_take isl_morph
*morph
)
317 isl_morph_print_internal(morph
, stderr
);
320 __isl_give isl_morph
*isl_morph_identity(__isl_keep isl_basic_set
*bset
)
323 isl_basic_set
*universe
;
329 total
= isl_basic_set_total_dim(bset
);
330 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
331 universe
= isl_basic_set_universe(isl_space_copy(bset
->dim
));
333 return isl_morph_alloc(universe
, isl_basic_set_copy(universe
),
334 id
, isl_mat_copy(id
));
337 /* Create a(n identity) morphism between empty sets of the same dimension
340 __isl_give isl_morph
*isl_morph_empty(__isl_keep isl_basic_set
*bset
)
343 isl_basic_set
*empty
;
349 total
= isl_basic_set_total_dim(bset
);
350 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
351 empty
= isl_basic_set_empty(isl_space_copy(bset
->dim
));
353 return isl_morph_alloc(empty
, isl_basic_set_copy(empty
),
354 id
, isl_mat_copy(id
));
357 /* Construct a basic set described by the "n" equalities of "bset" starting
360 static __isl_give isl_basic_set
*copy_equalities(__isl_keep isl_basic_set
*bset
,
361 unsigned first
, unsigned n
)
367 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
369 total
= isl_basic_set_total_dim(bset
);
370 eq
= isl_basic_set_alloc_space(isl_space_copy(bset
->dim
), 0, n
, 0);
373 for (i
= 0; i
< n
; ++i
) {
374 k
= isl_basic_set_alloc_equality(eq
);
377 isl_seq_cpy(eq
->eq
[k
], bset
->eq
[first
+ i
], 1 + total
);
382 isl_basic_set_free(eq
);
386 /* Given a basic set, exploit the equalities in the basic set to construct
387 * a morphism that maps the basic set to a lower-dimensional space.
388 * Specifically, the morphism reduces the number of dimensions of type "type".
390 * We first select the equalities of interest, that is those that involve
391 * variables of type "type" and no later variables.
392 * Denote those equalities as
396 * where C(p) depends on the parameters if type == isl_dim_set and
397 * is a constant if type == isl_dim_param.
399 * Use isl_mat_final_variable_compression to construct a compression
405 * If T is a zero-column matrix, then the set of equality constraints
406 * do not admit a solution. In this case, an empty morphism is returned.
408 * Both matrices are extended to map the full original space to the full
411 __isl_give isl_morph
*isl_basic_set_variable_compression(
412 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
)
421 isl_basic_set
*dom
, *ran
;
426 if (isl_basic_set_plain_is_empty(bset
))
427 return isl_morph_empty(bset
);
429 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
431 otype
= 1 + isl_space_offset(bset
->dim
, type
);
432 ntype
= isl_basic_set_dim(bset
, type
);
433 orest
= otype
+ ntype
;
434 nrest
= isl_basic_set_total_dim(bset
) - (orest
- 1);
436 for (f_eq
= 0; f_eq
< bset
->n_eq
; ++f_eq
)
437 if (isl_seq_first_non_zero(bset
->eq
[f_eq
] + orest
, nrest
) == -1)
439 for (n_eq
= 0; f_eq
+ n_eq
< bset
->n_eq
; ++n_eq
)
440 if (isl_seq_first_non_zero(bset
->eq
[f_eq
+ n_eq
] + otype
, ntype
) == -1)
443 return isl_morph_identity(bset
);
445 E
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, f_eq
, n_eq
, 0, orest
);
446 C
= isl_mat_final_variable_compression(E
, otype
- 1, &Q
);
449 if (C
&& C
->n_col
== 0) {
452 return isl_morph_empty(bset
);
455 Q
= isl_mat_diagonal(Q
, isl_mat_identity(bset
->ctx
, nrest
));
456 C
= isl_mat_diagonal(C
, isl_mat_identity(bset
->ctx
, nrest
));
458 dim
= isl_space_copy(bset
->dim
);
459 dim
= isl_space_drop_dims(dim
, type
, 0, ntype
);
460 dim
= isl_space_add_dims(dim
, type
, ntype
- n_eq
);
461 ran
= isl_basic_set_universe(dim
);
462 dom
= copy_equalities(bset
, f_eq
, n_eq
);
464 return isl_morph_alloc(dom
, ran
, Q
, C
);
467 /* Construct a parameter compression for "bset".
468 * We basically just call isl_mat_parameter_compression with the right input
469 * and then extend the resulting matrix to include the variables.
471 * The implementation assumes that "bset" does not have any equalities
472 * that only involve the parameters and that isl_basic_set_gauss has
473 * been applied to "bset".
475 * Let the equalities be given as
479 * We use isl_mat_parameter_compression_ext to compute the compression
483 __isl_give isl_morph
*isl_basic_set_parameter_compression(
484 __isl_keep isl_basic_set
*bset
)
492 isl_basic_set
*dom
, *ran
;
497 if (isl_basic_set_plain_is_empty(bset
))
498 return isl_morph_empty(bset
);
500 return isl_morph_identity(bset
);
503 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
504 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
505 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
507 if (isl_seq_first_non_zero(bset
->eq
[bset
->n_eq
- 1] + 1 + nparam
,
509 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
510 "input not allowed to have parameter equalities",
512 if (n_eq
> nvar
+ n_div
)
513 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
514 "input not gaussed", return NULL
);
516 B
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, 0, n_eq
, 0, 1 + nparam
);
517 H
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
,
518 0, n_eq
, 1 + nparam
, nvar
+ n_div
);
519 inv
= isl_mat_parameter_compression_ext(B
, H
);
520 inv
= isl_mat_diagonal(inv
, isl_mat_identity(bset
->ctx
, nvar
));
521 map
= isl_mat_right_inverse(isl_mat_copy(inv
));
523 dom
= isl_basic_set_universe(isl_space_copy(bset
->dim
));
524 ran
= isl_basic_set_universe(isl_space_copy(bset
->dim
));
526 return isl_morph_alloc(dom
, ran
, map
, inv
);
529 /* Add stride constraints to "bset" based on the inverse mapping
530 * that was plugged in. In particular, if morph maps x' to x,
531 * the the constraints of the original input
535 * have been rewritten to
539 * However, this substitution may loose information on the integrality of x',
540 * so we need to impose that
544 * is integral. If inv = B/d, this means that we need to impose that
550 * exists alpha in Z^m: B x = d alpha
552 * This function is similar to add_strides in isl_affine_hull.c
554 static __isl_give isl_basic_set
*add_strides(__isl_take isl_basic_set
*bset
,
555 __isl_keep isl_morph
*morph
)
560 if (isl_int_is_one(morph
->inv
->row
[0][0]))
565 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
) {
566 isl_seq_gcd(morph
->inv
->row
[1 + i
], morph
->inv
->n_col
, &gcd
);
567 if (isl_int_is_divisible_by(gcd
, morph
->inv
->row
[0][0]))
569 div
= isl_basic_set_alloc_div(bset
);
572 isl_int_set_si(bset
->div
[div
][0], 0);
573 k
= isl_basic_set_alloc_equality(bset
);
576 isl_seq_cpy(bset
->eq
[k
], morph
->inv
->row
[1 + i
],
578 isl_seq_clr(bset
->eq
[k
] + morph
->inv
->n_col
, bset
->n_div
);
579 isl_int_set(bset
->eq
[k
][morph
->inv
->n_col
+ div
],
580 morph
->inv
->row
[0][0]);
588 isl_basic_set_free(bset
);
592 /* Apply the morphism to the basic set.
593 * We basically just compute the preimage of "bset" under the inverse mapping
594 * in morph, add in stride constraints and intersect with the range
597 __isl_give isl_basic_set
*isl_morph_basic_set(__isl_take isl_morph
*morph
,
598 __isl_take isl_basic_set
*bset
)
600 isl_basic_set
*res
= NULL
;
608 isl_assert(bset
->ctx
, isl_space_is_equal(bset
->dim
, morph
->dom
->dim
),
611 max_stride
= morph
->inv
->n_row
- 1;
612 if (isl_int_is_one(morph
->inv
->row
[0][0]))
614 res
= isl_basic_set_alloc_space(isl_space_copy(morph
->ran
->dim
),
615 bset
->n_div
+ max_stride
, bset
->n_eq
+ max_stride
, bset
->n_ineq
);
617 for (i
= 0; i
< bset
->n_div
; ++i
)
618 if (isl_basic_set_alloc_div(res
) < 0)
621 mat
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
,
622 0, morph
->inv
->n_row
);
623 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
626 for (i
= 0; i
< bset
->n_eq
; ++i
) {
627 k
= isl_basic_set_alloc_equality(res
);
630 isl_seq_cpy(res
->eq
[k
], mat
->row
[i
], mat
->n_col
);
631 isl_seq_scale(res
->eq
[k
] + mat
->n_col
, bset
->eq
[i
] + mat
->n_col
,
632 morph
->inv
->row
[0][0], bset
->n_div
);
636 mat
= isl_mat_sub_alloc6(bset
->ctx
, bset
->ineq
, 0, bset
->n_ineq
,
637 0, morph
->inv
->n_row
);
638 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
641 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
642 k
= isl_basic_set_alloc_inequality(res
);
645 isl_seq_cpy(res
->ineq
[k
], mat
->row
[i
], mat
->n_col
);
646 isl_seq_scale(res
->ineq
[k
] + mat
->n_col
,
647 bset
->ineq
[i
] + mat
->n_col
,
648 morph
->inv
->row
[0][0], bset
->n_div
);
652 mat
= isl_mat_sub_alloc6(bset
->ctx
, bset
->div
, 0, bset
->n_div
,
653 1, morph
->inv
->n_row
);
654 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
657 for (i
= 0; i
< bset
->n_div
; ++i
) {
658 isl_int_mul(res
->div
[i
][0],
659 morph
->inv
->row
[0][0], bset
->div
[i
][0]);
660 isl_seq_cpy(res
->div
[i
] + 1, mat
->row
[i
], mat
->n_col
);
661 isl_seq_scale(res
->div
[i
] + 1 + mat
->n_col
,
662 bset
->div
[i
] + 1 + mat
->n_col
,
663 morph
->inv
->row
[0][0], bset
->n_div
);
667 res
= add_strides(res
, morph
);
669 if (isl_basic_set_is_rational(bset
))
670 res
= isl_basic_set_set_rational(res
);
672 res
= isl_basic_set_simplify(res
);
673 res
= isl_basic_set_finalize(res
);
675 res
= isl_basic_set_intersect(res
, isl_basic_set_copy(morph
->ran
));
677 isl_morph_free(morph
);
678 isl_basic_set_free(bset
);
682 isl_morph_free(morph
);
683 isl_basic_set_free(bset
);
684 isl_basic_set_free(res
);
688 /* Apply the morphism to the set.
690 __isl_give isl_set
*isl_morph_set(__isl_take isl_morph
*morph
,
691 __isl_take isl_set
*set
)
698 isl_assert(set
->ctx
, isl_space_is_equal(set
->dim
, morph
->dom
->dim
), goto error
);
700 set
= isl_set_cow(set
);
704 isl_space_free(set
->dim
);
705 set
->dim
= isl_space_copy(morph
->ran
->dim
);
709 for (i
= 0; i
< set
->n
; ++i
) {
710 set
->p
[i
] = isl_morph_basic_set(isl_morph_copy(morph
), set
->p
[i
]);
715 isl_morph_free(morph
);
717 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
722 isl_morph_free(morph
);
726 /* Construct a morphism that first does morph2 and then morph1.
728 __isl_give isl_morph
*isl_morph_compose(__isl_take isl_morph
*morph1
,
729 __isl_take isl_morph
*morph2
)
732 isl_basic_set
*dom
, *ran
;
734 if (!morph1
|| !morph2
)
737 map
= isl_mat_product(isl_mat_copy(morph1
->map
), isl_mat_copy(morph2
->map
));
738 inv
= isl_mat_product(isl_mat_copy(morph2
->inv
), isl_mat_copy(morph1
->inv
));
739 dom
= isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2
)),
740 isl_basic_set_copy(morph1
->dom
));
741 dom
= isl_basic_set_intersect(dom
, isl_basic_set_copy(morph2
->dom
));
742 ran
= isl_morph_basic_set(isl_morph_copy(morph1
),
743 isl_basic_set_copy(morph2
->ran
));
744 ran
= isl_basic_set_intersect(ran
, isl_basic_set_copy(morph1
->ran
));
746 isl_morph_free(morph1
);
747 isl_morph_free(morph2
);
749 return isl_morph_alloc(dom
, ran
, map
, inv
);
751 isl_morph_free(morph1
);
752 isl_morph_free(morph2
);
756 __isl_give isl_morph
*isl_morph_inverse(__isl_take isl_morph
*morph
)
761 morph
= isl_morph_cow(morph
);
766 morph
->dom
= morph
->ran
;
770 morph
->map
= morph
->inv
;
776 /* We detect all the equalities first to avoid implicit equalities
777 * being discovered during the computations. In particular,
778 * the compression on the variables could expose additional stride
779 * constraints on the parameters. This would result in existentially
780 * quantified variables after applying the resulting morph, which
781 * in turn could break invariants of the calling functions.
783 __isl_give isl_morph
*isl_basic_set_full_compression(
784 __isl_keep isl_basic_set
*bset
)
786 isl_morph
*morph
, *morph2
;
788 bset
= isl_basic_set_copy(bset
);
789 bset
= isl_basic_set_detect_equalities(bset
);
791 morph
= isl_basic_set_variable_compression(bset
, isl_dim_param
);
792 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
794 morph2
= isl_basic_set_parameter_compression(bset
);
795 bset
= isl_morph_basic_set(isl_morph_copy(morph2
), bset
);
797 morph
= isl_morph_compose(morph2
, morph
);
799 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
800 isl_basic_set_free(bset
);
802 morph
= isl_morph_compose(morph2
, morph
);
807 __isl_give isl_vec
*isl_morph_vec(__isl_take isl_morph
*morph
,
808 __isl_take isl_vec
*vec
)
813 vec
= isl_mat_vec_product(isl_mat_copy(morph
->map
), vec
);
815 isl_morph_free(morph
);
818 isl_morph_free(morph
);