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>
20 #include <isl_id_private.h>
22 isl_ctx
*isl_morph_get_ctx(__isl_keep isl_morph
*morph
)
26 return isl_basic_set_get_ctx(morph
->dom
);
29 __isl_give isl_morph
*isl_morph_alloc(
30 __isl_take isl_basic_set
*dom
, __isl_take isl_basic_set
*ran
,
31 __isl_take isl_mat
*map
, __isl_take isl_mat
*inv
)
35 if (!dom
|| !ran
|| !map
|| !inv
)
38 morph
= isl_alloc_type(dom
->ctx
, struct isl_morph
);
50 isl_basic_set_free(dom
);
51 isl_basic_set_free(ran
);
57 __isl_give isl_morph
*isl_morph_copy(__isl_keep isl_morph
*morph
)
66 __isl_give isl_morph
*isl_morph_dup(__isl_keep isl_morph
*morph
)
71 return isl_morph_alloc(isl_basic_set_copy(morph
->dom
),
72 isl_basic_set_copy(morph
->ran
),
73 isl_mat_copy(morph
->map
), isl_mat_copy(morph
->inv
));
76 __isl_give isl_morph
*isl_morph_cow(__isl_take isl_morph
*morph
)
84 return isl_morph_dup(morph
);
87 __isl_null isl_morph
*isl_morph_free(__isl_take isl_morph
*morph
)
95 isl_basic_set_free(morph
->dom
);
96 isl_basic_set_free(morph
->ran
);
97 isl_mat_free(morph
->map
);
98 isl_mat_free(morph
->inv
);
104 /* Is "morph" an identity on the parameters?
106 static int identity_on_parameters(__isl_keep isl_morph
*morph
)
112 nparam
= isl_morph_dom_dim(morph
, isl_dim_param
);
113 if (nparam
!= isl_morph_ran_dim(morph
, isl_dim_param
))
117 sub
= isl_mat_sub_alloc(morph
->map
, 0, 1 + nparam
, 0, 1 + nparam
);
118 is_identity
= isl_mat_is_scaled_identity(sub
);
124 /* Return an affine expression of the variables of the range of "morph"
125 * in terms of the parameters and the variables of the domain on "morph".
127 * In order for the space manipulations to make sense, we require
128 * that the parameters are not modified by "morph".
130 __isl_give isl_multi_aff
*isl_morph_get_var_multi_aff(
131 __isl_keep isl_morph
*morph
)
133 isl_space
*dom
, *ran
, *space
;
136 unsigned nparam
, nvar
;
143 is_identity
= identity_on_parameters(morph
);
147 isl_die(isl_morph_get_ctx(morph
), isl_error_invalid
,
148 "cannot handle parameter compression", return NULL
);
150 dom
= isl_morph_get_dom_space(morph
);
151 ls
= isl_local_space_from_space(isl_space_copy(dom
));
152 ran
= isl_morph_get_ran_space(morph
);
153 space
= isl_space_map_from_domain_and_range(dom
, ran
);
154 ma
= isl_multi_aff_zero(space
);
156 nparam
= isl_multi_aff_dim(ma
, isl_dim_param
);
157 nvar
= isl_multi_aff_dim(ma
, isl_dim_out
);
158 for (i
= 0; i
< nvar
; ++i
) {
163 v
= isl_mat_get_row(morph
->map
, 1 + nparam
+ i
);
164 v
= isl_vec_insert_els(v
, 0, 1);
165 val
= isl_mat_get_element_val(morph
->map
, 0, 0);
166 v
= isl_vec_set_element_val(v
, 0, val
);
167 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
168 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
171 isl_local_space_free(ls
);
175 /* Return the domain space of "morph".
177 __isl_give isl_space
*isl_morph_get_dom_space(__isl_keep isl_morph
*morph
)
182 return isl_basic_set_get_space(morph
->dom
);
185 __isl_give isl_space
*isl_morph_get_ran_space(__isl_keep isl_morph
*morph
)
190 return isl_space_copy(morph
->ran
->dim
);
193 unsigned isl_morph_dom_dim(__isl_keep isl_morph
*morph
, enum isl_dim_type type
)
198 return isl_basic_set_dim(morph
->dom
, type
);
201 unsigned isl_morph_ran_dim(__isl_keep isl_morph
*morph
, enum isl_dim_type type
)
206 return isl_basic_set_dim(morph
->ran
, type
);
209 __isl_give isl_morph
*isl_morph_remove_dom_dims(__isl_take isl_morph
*morph
,
210 enum isl_dim_type type
, unsigned first
, unsigned n
)
217 morph
= isl_morph_cow(morph
);
221 dom_offset
= 1 + isl_space_offset(morph
->dom
->dim
, type
);
223 morph
->dom
= isl_basic_set_remove_dims(morph
->dom
, type
, first
, n
);
225 morph
->map
= isl_mat_drop_cols(morph
->map
, dom_offset
+ first
, n
);
227 morph
->inv
= isl_mat_drop_rows(morph
->inv
, dom_offset
+ first
, n
);
229 if (morph
->dom
&& morph
->ran
&& morph
->map
&& morph
->inv
)
232 isl_morph_free(morph
);
236 __isl_give isl_morph
*isl_morph_remove_ran_dims(__isl_take isl_morph
*morph
,
237 enum isl_dim_type type
, unsigned first
, unsigned n
)
244 morph
= isl_morph_cow(morph
);
248 ran_offset
= 1 + isl_space_offset(morph
->ran
->dim
, type
);
250 morph
->ran
= isl_basic_set_remove_dims(morph
->ran
, type
, first
, n
);
252 morph
->map
= isl_mat_drop_rows(morph
->map
, ran_offset
+ first
, n
);
254 morph
->inv
= isl_mat_drop_cols(morph
->inv
, ran_offset
+ first
, n
);
256 if (morph
->dom
&& morph
->ran
&& morph
->map
&& morph
->inv
)
259 isl_morph_free(morph
);
263 /* Project domain of morph onto its parameter domain.
265 __isl_give isl_morph
*isl_morph_dom_params(__isl_take isl_morph
*morph
)
269 morph
= isl_morph_cow(morph
);
272 n
= isl_basic_set_dim(morph
->dom
, isl_dim_set
);
273 morph
= isl_morph_remove_dom_dims(morph
, isl_dim_set
, 0, n
);
276 morph
->dom
= isl_basic_set_params(morph
->dom
);
280 isl_morph_free(morph
);
284 /* Project range of morph onto its parameter domain.
286 __isl_give isl_morph
*isl_morph_ran_params(__isl_take isl_morph
*morph
)
290 morph
= isl_morph_cow(morph
);
293 n
= isl_basic_set_dim(morph
->ran
, isl_dim_set
);
294 morph
= isl_morph_remove_ran_dims(morph
, isl_dim_set
, 0, n
);
297 morph
->ran
= isl_basic_set_params(morph
->ran
);
301 isl_morph_free(morph
);
305 void isl_morph_print_internal(__isl_take isl_morph
*morph
, FILE *out
)
310 isl_basic_set_dump(morph
->dom
);
311 isl_basic_set_dump(morph
->ran
);
312 isl_mat_print_internal(morph
->map
, out
, 4);
313 isl_mat_print_internal(morph
->inv
, out
, 4);
316 void isl_morph_dump(__isl_take isl_morph
*morph
)
318 isl_morph_print_internal(morph
, stderr
);
321 __isl_give isl_morph
*isl_morph_identity(__isl_keep isl_basic_set
*bset
)
324 isl_basic_set
*universe
;
330 total
= isl_basic_set_total_dim(bset
);
331 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
332 universe
= isl_basic_set_universe(isl_space_copy(bset
->dim
));
334 return isl_morph_alloc(universe
, isl_basic_set_copy(universe
),
335 id
, isl_mat_copy(id
));
338 /* Create a(n identity) morphism between empty sets of the same dimension
341 __isl_give isl_morph
*isl_morph_empty(__isl_keep isl_basic_set
*bset
)
344 isl_basic_set
*empty
;
350 total
= isl_basic_set_total_dim(bset
);
351 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
352 empty
= isl_basic_set_empty(isl_space_copy(bset
->dim
));
354 return isl_morph_alloc(empty
, isl_basic_set_copy(empty
),
355 id
, isl_mat_copy(id
));
358 /* Construct a basic set described by the "n" equalities of "bset" starting
361 static __isl_give isl_basic_set
*copy_equalities(__isl_keep isl_basic_set
*bset
,
362 unsigned first
, unsigned n
)
368 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
370 total
= isl_basic_set_total_dim(bset
);
371 eq
= isl_basic_set_alloc_space(isl_space_copy(bset
->dim
), 0, n
, 0);
374 for (i
= 0; i
< n
; ++i
) {
375 k
= isl_basic_set_alloc_equality(eq
);
378 isl_seq_cpy(eq
->eq
[k
], bset
->eq
[first
+ i
], 1 + total
);
383 isl_basic_set_free(eq
);
387 /* Given a basic set, exploit the equalities in the basic set to construct
388 * a morphism that maps the basic set to a lower-dimensional space
389 * with identifier "id".
390 * Specifically, the morphism reduces the number of dimensions of type "type".
392 * We first select the equalities of interest, that is those that involve
393 * variables of type "type" and no later variables.
394 * Denote those equalities as
398 * where C(p) depends on the parameters if type == isl_dim_set and
399 * is a constant if type == isl_dim_param.
401 * Use isl_mat_final_variable_compression to construct a compression
407 * If T is a zero-column matrix, then the set of equality constraints
408 * do not admit a solution. In this case, an empty morphism is returned.
410 * Both matrices are extended to map the full original space to the full
413 __isl_give isl_morph
*isl_basic_set_variable_compression_with_id(
414 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
,
415 __isl_keep isl_id
*id
)
424 isl_basic_set
*dom
, *ran
;
429 if (isl_basic_set_plain_is_empty(bset
))
430 return isl_morph_empty(bset
);
432 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
434 otype
= 1 + isl_space_offset(bset
->dim
, type
);
435 ntype
= isl_basic_set_dim(bset
, type
);
436 orest
= otype
+ ntype
;
437 nrest
= isl_basic_set_total_dim(bset
) - (orest
- 1);
439 for (f_eq
= 0; f_eq
< bset
->n_eq
; ++f_eq
)
440 if (isl_seq_first_non_zero(bset
->eq
[f_eq
] + orest
, nrest
) == -1)
442 for (n_eq
= 0; f_eq
+ n_eq
< bset
->n_eq
; ++n_eq
)
443 if (isl_seq_first_non_zero(bset
->eq
[f_eq
+ n_eq
] + otype
, ntype
) == -1)
446 return isl_morph_identity(bset
);
448 E
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, f_eq
, n_eq
, 0, orest
);
449 C
= isl_mat_final_variable_compression(E
, otype
- 1, &Q
);
452 if (C
&& C
->n_col
== 0) {
455 return isl_morph_empty(bset
);
458 Q
= isl_mat_diagonal(Q
, isl_mat_identity(bset
->ctx
, nrest
));
459 C
= isl_mat_diagonal(C
, isl_mat_identity(bset
->ctx
, nrest
));
461 space
= isl_space_copy(bset
->dim
);
462 space
= isl_space_drop_dims(space
, type
, 0, ntype
);
463 space
= isl_space_add_dims(space
, type
, ntype
- n_eq
);
464 space
= isl_space_set_tuple_id(space
, isl_dim_set
, isl_id_copy(id
));
465 ran
= isl_basic_set_universe(space
);
466 dom
= copy_equalities(bset
, f_eq
, n_eq
);
468 return isl_morph_alloc(dom
, ran
, Q
, C
);
471 /* Given a basic set, exploit the equalities in the basic set to construct
472 * a morphism that maps the basic set to a lower-dimensional space.
473 * Specifically, the morphism reduces the number of dimensions of type "type".
475 __isl_give isl_morph
*isl_basic_set_variable_compression(
476 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
)
478 return isl_basic_set_variable_compression_with_id(bset
, type
,
482 /* Construct a parameter compression for "bset".
483 * We basically just call isl_mat_parameter_compression with the right input
484 * and then extend the resulting matrix to include the variables.
486 * The implementation assumes that "bset" does not have any equalities
487 * that only involve the parameters and that isl_basic_set_gauss has
488 * been applied to "bset".
490 * Let the equalities be given as
494 * We use isl_mat_parameter_compression_ext to compute the compression
498 __isl_give isl_morph
*isl_basic_set_parameter_compression(
499 __isl_keep isl_basic_set
*bset
)
507 isl_basic_set
*dom
, *ran
;
512 if (isl_basic_set_plain_is_empty(bset
))
513 return isl_morph_empty(bset
);
515 return isl_morph_identity(bset
);
518 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
519 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
520 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
522 if (isl_seq_first_non_zero(bset
->eq
[bset
->n_eq
- 1] + 1 + nparam
,
524 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
525 "input not allowed to have parameter equalities",
527 if (n_eq
> nvar
+ n_div
)
528 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
529 "input not gaussed", return NULL
);
531 B
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, 0, n_eq
, 0, 1 + nparam
);
532 H
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
,
533 0, n_eq
, 1 + nparam
, nvar
+ n_div
);
534 inv
= isl_mat_parameter_compression_ext(B
, H
);
535 inv
= isl_mat_diagonal(inv
, isl_mat_identity(bset
->ctx
, nvar
));
536 map
= isl_mat_right_inverse(isl_mat_copy(inv
));
538 dom
= isl_basic_set_universe(isl_space_copy(bset
->dim
));
539 ran
= isl_basic_set_universe(isl_space_copy(bset
->dim
));
541 return isl_morph_alloc(dom
, ran
, map
, inv
);
544 /* Add stride constraints to "bset" based on the inverse mapping
545 * that was plugged in. In particular, if morph maps x' to x,
546 * the the constraints of the original input
550 * have been rewritten to
554 * However, this substitution may loose information on the integrality of x',
555 * so we need to impose that
559 * is integral. If inv = B/d, this means that we need to impose that
565 * exists alpha in Z^m: B x = d alpha
567 * This function is similar to add_strides in isl_affine_hull.c
569 static __isl_give isl_basic_set
*add_strides(__isl_take isl_basic_set
*bset
,
570 __isl_keep isl_morph
*morph
)
575 if (isl_int_is_one(morph
->inv
->row
[0][0]))
580 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
) {
581 isl_seq_gcd(morph
->inv
->row
[1 + i
], morph
->inv
->n_col
, &gcd
);
582 if (isl_int_is_divisible_by(gcd
, morph
->inv
->row
[0][0]))
584 div
= isl_basic_set_alloc_div(bset
);
587 isl_int_set_si(bset
->div
[div
][0], 0);
588 k
= isl_basic_set_alloc_equality(bset
);
591 isl_seq_cpy(bset
->eq
[k
], morph
->inv
->row
[1 + i
],
593 isl_seq_clr(bset
->eq
[k
] + morph
->inv
->n_col
, bset
->n_div
);
594 isl_int_set(bset
->eq
[k
][morph
->inv
->n_col
+ div
],
595 morph
->inv
->row
[0][0]);
603 isl_basic_set_free(bset
);
607 /* Apply the morphism to the basic set.
608 * We basically just compute the preimage of "bset" under the inverse mapping
609 * in morph, add in stride constraints and intersect with the range
612 __isl_give isl_basic_set
*isl_morph_basic_set(__isl_take isl_morph
*morph
,
613 __isl_take isl_basic_set
*bset
)
615 isl_basic_set
*res
= NULL
;
623 isl_assert(bset
->ctx
, isl_space_is_equal(bset
->dim
, morph
->dom
->dim
),
626 max_stride
= morph
->inv
->n_row
- 1;
627 if (isl_int_is_one(morph
->inv
->row
[0][0]))
629 res
= isl_basic_set_alloc_space(isl_space_copy(morph
->ran
->dim
),
630 bset
->n_div
+ max_stride
, bset
->n_eq
+ max_stride
, bset
->n_ineq
);
632 for (i
= 0; i
< bset
->n_div
; ++i
)
633 if (isl_basic_set_alloc_div(res
) < 0)
636 mat
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
,
637 0, morph
->inv
->n_row
);
638 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
641 for (i
= 0; i
< bset
->n_eq
; ++i
) {
642 k
= isl_basic_set_alloc_equality(res
);
645 isl_seq_cpy(res
->eq
[k
], mat
->row
[i
], mat
->n_col
);
646 isl_seq_scale(res
->eq
[k
] + mat
->n_col
, bset
->eq
[i
] + mat
->n_col
,
647 morph
->inv
->row
[0][0], bset
->n_div
);
651 mat
= isl_mat_sub_alloc6(bset
->ctx
, bset
->ineq
, 0, bset
->n_ineq
,
652 0, morph
->inv
->n_row
);
653 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
656 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
657 k
= isl_basic_set_alloc_inequality(res
);
660 isl_seq_cpy(res
->ineq
[k
], mat
->row
[i
], mat
->n_col
);
661 isl_seq_scale(res
->ineq
[k
] + mat
->n_col
,
662 bset
->ineq
[i
] + mat
->n_col
,
663 morph
->inv
->row
[0][0], bset
->n_div
);
667 mat
= isl_mat_sub_alloc6(bset
->ctx
, bset
->div
, 0, bset
->n_div
,
668 1, morph
->inv
->n_row
);
669 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
672 for (i
= 0; i
< bset
->n_div
; ++i
) {
673 isl_int_mul(res
->div
[i
][0],
674 morph
->inv
->row
[0][0], bset
->div
[i
][0]);
675 isl_seq_cpy(res
->div
[i
] + 1, mat
->row
[i
], mat
->n_col
);
676 isl_seq_scale(res
->div
[i
] + 1 + mat
->n_col
,
677 bset
->div
[i
] + 1 + mat
->n_col
,
678 morph
->inv
->row
[0][0], bset
->n_div
);
682 res
= add_strides(res
, morph
);
684 if (isl_basic_set_is_rational(bset
))
685 res
= isl_basic_set_set_rational(res
);
687 res
= isl_basic_set_simplify(res
);
688 res
= isl_basic_set_finalize(res
);
690 res
= isl_basic_set_intersect(res
, isl_basic_set_copy(morph
->ran
));
692 isl_morph_free(morph
);
693 isl_basic_set_free(bset
);
697 isl_morph_free(morph
);
698 isl_basic_set_free(bset
);
699 isl_basic_set_free(res
);
703 /* Apply the morphism to the set.
705 __isl_give isl_set
*isl_morph_set(__isl_take isl_morph
*morph
,
706 __isl_take isl_set
*set
)
713 isl_assert(set
->ctx
, isl_space_is_equal(set
->dim
, morph
->dom
->dim
), goto error
);
715 set
= isl_set_cow(set
);
719 isl_space_free(set
->dim
);
720 set
->dim
= isl_space_copy(morph
->ran
->dim
);
724 for (i
= 0; i
< set
->n
; ++i
) {
725 set
->p
[i
] = isl_morph_basic_set(isl_morph_copy(morph
), set
->p
[i
]);
730 isl_morph_free(morph
);
732 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
737 isl_morph_free(morph
);
741 /* Construct a morphism that first does morph2 and then morph1.
743 __isl_give isl_morph
*isl_morph_compose(__isl_take isl_morph
*morph1
,
744 __isl_take isl_morph
*morph2
)
747 isl_basic_set
*dom
, *ran
;
749 if (!morph1
|| !morph2
)
752 map
= isl_mat_product(isl_mat_copy(morph1
->map
), isl_mat_copy(morph2
->map
));
753 inv
= isl_mat_product(isl_mat_copy(morph2
->inv
), isl_mat_copy(morph1
->inv
));
754 dom
= isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2
)),
755 isl_basic_set_copy(morph1
->dom
));
756 dom
= isl_basic_set_intersect(dom
, isl_basic_set_copy(morph2
->dom
));
757 ran
= isl_morph_basic_set(isl_morph_copy(morph1
),
758 isl_basic_set_copy(morph2
->ran
));
759 ran
= isl_basic_set_intersect(ran
, isl_basic_set_copy(morph1
->ran
));
761 isl_morph_free(morph1
);
762 isl_morph_free(morph2
);
764 return isl_morph_alloc(dom
, ran
, map
, inv
);
766 isl_morph_free(morph1
);
767 isl_morph_free(morph2
);
771 __isl_give isl_morph
*isl_morph_inverse(__isl_take isl_morph
*morph
)
776 morph
= isl_morph_cow(morph
);
781 morph
->dom
= morph
->ran
;
785 morph
->map
= morph
->inv
;
791 /* We detect all the equalities first to avoid implicit equalities
792 * being discovered during the computations. In particular,
793 * the compression on the variables could expose additional stride
794 * constraints on the parameters. This would result in existentially
795 * quantified variables after applying the resulting morph, which
796 * in turn could break invariants of the calling functions.
798 __isl_give isl_morph
*isl_basic_set_full_compression(
799 __isl_keep isl_basic_set
*bset
)
801 isl_morph
*morph
, *morph2
;
803 bset
= isl_basic_set_copy(bset
);
804 bset
= isl_basic_set_detect_equalities(bset
);
806 morph
= isl_basic_set_variable_compression(bset
, isl_dim_param
);
807 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
809 morph2
= isl_basic_set_parameter_compression(bset
);
810 bset
= isl_morph_basic_set(isl_morph_copy(morph2
), bset
);
812 morph
= isl_morph_compose(morph2
, morph
);
814 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
815 isl_basic_set_free(bset
);
817 morph
= isl_morph_compose(morph2
, morph
);
822 __isl_give isl_vec
*isl_morph_vec(__isl_take isl_morph
*morph
,
823 __isl_take isl_vec
*vec
)
828 vec
= isl_mat_vec_product(isl_mat_copy(morph
->map
), vec
);
830 isl_morph_free(morph
);
833 isl_morph_free(morph
);