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 void 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
);
101 /* Is "morph" an identity on the parameters?
103 static int identity_on_parameters(__isl_keep isl_morph
*morph
)
109 nparam
= isl_morph_dom_dim(morph
, isl_dim_param
);
110 if (nparam
!= isl_morph_ran_dim(morph
, isl_dim_param
))
114 sub
= isl_mat_sub_alloc(morph
->map
, 0, 1 + nparam
, 0, 1 + nparam
);
115 is_identity
= isl_mat_is_scaled_identity(sub
);
121 /* Return an affine expression of the variables of the range of "morph"
122 * in terms of the parameters and the variables of the domain on "morph".
124 * In order for the space manipulations to make sense, we require
125 * that the parameters are not modified by "morph".
127 __isl_give isl_multi_aff
*isl_morph_get_var_multi_aff(
128 __isl_keep isl_morph
*morph
)
130 isl_space
*dom
, *ran
, *space
;
133 unsigned nparam
, nvar
;
140 is_identity
= identity_on_parameters(morph
);
144 isl_die(isl_morph_get_ctx(morph
), isl_error_invalid
,
145 "cannot handle parameter compression", return NULL
);
147 dom
= isl_morph_get_dom_space(morph
);
148 ls
= isl_local_space_from_space(isl_space_copy(dom
));
149 ran
= isl_morph_get_ran_space(morph
);
150 space
= isl_space_map_from_domain_and_range(dom
, ran
);
151 ma
= isl_multi_aff_zero(space
);
153 nparam
= isl_multi_aff_dim(ma
, isl_dim_param
);
154 nvar
= isl_multi_aff_dim(ma
, isl_dim_out
);
155 for (i
= 0; i
< nvar
; ++i
) {
160 v
= isl_mat_get_row(morph
->map
, 1 + nparam
+ i
);
161 v
= isl_vec_insert_els(v
, 0, 1);
162 val
= isl_mat_get_element_val(morph
->map
, 0, 0);
163 v
= isl_vec_set_element_val(v
, 0, val
);
164 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
165 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
168 isl_local_space_free(ls
);
172 /* Return the domain space of "morph".
174 __isl_give isl_space
*isl_morph_get_dom_space(__isl_keep isl_morph
*morph
)
179 return isl_basic_set_get_space(morph
->dom
);
182 __isl_give isl_space
*isl_morph_get_ran_space(__isl_keep isl_morph
*morph
)
187 return isl_space_copy(morph
->ran
->dim
);
190 unsigned isl_morph_dom_dim(__isl_keep isl_morph
*morph
, enum isl_dim_type type
)
195 return isl_basic_set_dim(morph
->dom
, type
);
198 unsigned isl_morph_ran_dim(__isl_keep isl_morph
*morph
, enum isl_dim_type type
)
203 return isl_basic_set_dim(morph
->ran
, type
);
206 __isl_give isl_morph
*isl_morph_remove_dom_dims(__isl_take isl_morph
*morph
,
207 enum isl_dim_type type
, unsigned first
, unsigned n
)
214 morph
= isl_morph_cow(morph
);
218 dom_offset
= 1 + isl_space_offset(morph
->dom
->dim
, type
);
220 morph
->dom
= isl_basic_set_remove_dims(morph
->dom
, type
, first
, n
);
222 morph
->map
= isl_mat_drop_cols(morph
->map
, dom_offset
+ first
, n
);
224 morph
->inv
= isl_mat_drop_rows(morph
->inv
, dom_offset
+ first
, n
);
226 if (morph
->dom
&& morph
->ran
&& morph
->map
&& morph
->inv
)
229 isl_morph_free(morph
);
233 __isl_give isl_morph
*isl_morph_remove_ran_dims(__isl_take isl_morph
*morph
,
234 enum isl_dim_type type
, unsigned first
, unsigned n
)
241 morph
= isl_morph_cow(morph
);
245 ran_offset
= 1 + isl_space_offset(morph
->ran
->dim
, type
);
247 morph
->ran
= isl_basic_set_remove_dims(morph
->ran
, type
, first
, n
);
249 morph
->map
= isl_mat_drop_rows(morph
->map
, ran_offset
+ first
, n
);
251 morph
->inv
= isl_mat_drop_cols(morph
->inv
, ran_offset
+ first
, n
);
253 if (morph
->dom
&& morph
->ran
&& morph
->map
&& morph
->inv
)
256 isl_morph_free(morph
);
260 /* Project domain of morph onto its parameter domain.
262 __isl_give isl_morph
*isl_morph_dom_params(__isl_take isl_morph
*morph
)
266 morph
= isl_morph_cow(morph
);
269 n
= isl_basic_set_dim(morph
->dom
, isl_dim_set
);
270 morph
= isl_morph_remove_dom_dims(morph
, isl_dim_set
, 0, n
);
273 morph
->dom
= isl_basic_set_params(morph
->dom
);
277 isl_morph_free(morph
);
281 /* Project range of morph onto its parameter domain.
283 __isl_give isl_morph
*isl_morph_ran_params(__isl_take isl_morph
*morph
)
287 morph
= isl_morph_cow(morph
);
290 n
= isl_basic_set_dim(morph
->ran
, isl_dim_set
);
291 morph
= isl_morph_remove_ran_dims(morph
, isl_dim_set
, 0, n
);
294 morph
->ran
= isl_basic_set_params(morph
->ran
);
298 isl_morph_free(morph
);
302 void isl_morph_print_internal(__isl_take isl_morph
*morph
, FILE *out
)
307 isl_basic_set_dump(morph
->dom
);
308 isl_basic_set_dump(morph
->ran
);
309 isl_mat_print_internal(morph
->map
, out
, 4);
310 isl_mat_print_internal(morph
->inv
, out
, 4);
313 void isl_morph_dump(__isl_take isl_morph
*morph
)
315 isl_morph_print_internal(morph
, stderr
);
318 __isl_give isl_morph
*isl_morph_identity(__isl_keep isl_basic_set
*bset
)
321 isl_basic_set
*universe
;
327 total
= isl_basic_set_total_dim(bset
);
328 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
329 universe
= isl_basic_set_universe(isl_space_copy(bset
->dim
));
331 return isl_morph_alloc(universe
, isl_basic_set_copy(universe
),
332 id
, isl_mat_copy(id
));
335 /* Create a(n identity) morphism between empty sets of the same dimension
338 __isl_give isl_morph
*isl_morph_empty(__isl_keep isl_basic_set
*bset
)
341 isl_basic_set
*empty
;
347 total
= isl_basic_set_total_dim(bset
);
348 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
349 empty
= isl_basic_set_empty(isl_space_copy(bset
->dim
));
351 return isl_morph_alloc(empty
, isl_basic_set_copy(empty
),
352 id
, isl_mat_copy(id
));
355 /* Construct a basic set described by the "n" equalities of "bset" starting
358 static __isl_give isl_basic_set
*copy_equalities(__isl_keep isl_basic_set
*bset
,
359 unsigned first
, unsigned n
)
365 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
367 total
= isl_basic_set_total_dim(bset
);
368 eq
= isl_basic_set_alloc_space(isl_space_copy(bset
->dim
), 0, n
, 0);
371 for (i
= 0; i
< n
; ++i
) {
372 k
= isl_basic_set_alloc_equality(eq
);
375 isl_seq_cpy(eq
->eq
[k
], bset
->eq
[first
+ i
], 1 + total
);
380 isl_basic_set_free(eq
);
384 /* Given a basic set, exploit the equalties in the basic set to construct
385 * a morphishm that maps the basic set to a lower-dimensional space.
386 * Specifically, the morphism reduces the number of dimensions of type "type".
388 * We first select the equalities of interest, that is those that involve
389 * variables of type "type" and no later variables.
390 * Denote those equalities as
394 * where C(p) depends on the parameters if type == isl_dim_set and
395 * is a constant if type == isl_dim_param.
397 * Use isl_mat_final_variable_compression to construct a compression
403 * If T is a zero-column matrix, then the set of equality constraints
404 * do not admit a solution. In this case, an empty morphism is returned.
406 * Both matrices are extended to map the full original space to the full
409 __isl_give isl_morph
*isl_basic_set_variable_compression(
410 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
)
419 isl_basic_set
*dom
, *ran
;
424 if (isl_basic_set_plain_is_empty(bset
))
425 return isl_morph_empty(bset
);
427 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
429 otype
= 1 + isl_space_offset(bset
->dim
, type
);
430 ntype
= isl_basic_set_dim(bset
, type
);
431 orest
= otype
+ ntype
;
432 nrest
= isl_basic_set_total_dim(bset
) - (orest
- 1);
434 for (f_eq
= 0; f_eq
< bset
->n_eq
; ++f_eq
)
435 if (isl_seq_first_non_zero(bset
->eq
[f_eq
] + orest
, nrest
) == -1)
437 for (n_eq
= 0; f_eq
+ n_eq
< bset
->n_eq
; ++n_eq
)
438 if (isl_seq_first_non_zero(bset
->eq
[f_eq
+ n_eq
] + otype
, ntype
) == -1)
441 return isl_morph_identity(bset
);
443 E
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, f_eq
, n_eq
, 0, orest
);
444 C
= isl_mat_final_variable_compression(E
, otype
- 1, &Q
);
447 if (C
&& C
->n_col
== 0) {
450 return isl_morph_empty(bset
);
453 Q
= isl_mat_diagonal(Q
, isl_mat_identity(bset
->ctx
, nrest
));
454 C
= isl_mat_diagonal(C
, isl_mat_identity(bset
->ctx
, nrest
));
456 dim
= isl_space_copy(bset
->dim
);
457 dim
= isl_space_drop_dims(dim
, type
, 0, ntype
);
458 dim
= isl_space_add_dims(dim
, type
, ntype
- n_eq
);
459 ran
= isl_basic_set_universe(dim
);
460 dom
= copy_equalities(bset
, f_eq
, n_eq
);
462 return isl_morph_alloc(dom
, ran
, Q
, C
);
465 /* Construct a parameter compression for "bset".
466 * We basically just call isl_mat_parameter_compression with the right input
467 * and then extend the resulting matrix to include the variables.
469 * The implementation assumes that "bset" does not have any equalities
470 * that only involve the parameters and that isl_basic_set_gauss has
471 * been applied to "bset".
473 * Let the equalities be given as
477 * We use isl_mat_parameter_compression_ext to compute the compression
481 __isl_give isl_morph
*isl_basic_set_parameter_compression(
482 __isl_keep isl_basic_set
*bset
)
490 isl_basic_set
*dom
, *ran
;
495 if (isl_basic_set_plain_is_empty(bset
))
496 return isl_morph_empty(bset
);
498 return isl_morph_identity(bset
);
501 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
502 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
503 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
505 if (isl_seq_first_non_zero(bset
->eq
[bset
->n_eq
- 1] + 1 + nparam
,
507 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
508 "input not allowed to have parameter equalities",
510 if (n_eq
> nvar
+ n_div
)
511 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
512 "input not gaussed", return NULL
);
514 B
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, 0, n_eq
, 0, 1 + nparam
);
515 H
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
,
516 0, n_eq
, 1 + nparam
, nvar
+ n_div
);
517 inv
= isl_mat_parameter_compression_ext(B
, H
);
518 inv
= isl_mat_diagonal(inv
, isl_mat_identity(bset
->ctx
, nvar
));
519 map
= isl_mat_right_inverse(isl_mat_copy(inv
));
521 dom
= isl_basic_set_universe(isl_space_copy(bset
->dim
));
522 ran
= isl_basic_set_universe(isl_space_copy(bset
->dim
));
524 return isl_morph_alloc(dom
, ran
, map
, inv
);
527 /* Add stride constraints to "bset" based on the inverse mapping
528 * that was plugged in. In particular, if morph maps x' to x,
529 * the the constraints of the original input
533 * have been rewritten to
537 * However, this substitution may loose information on the integrality of x',
538 * so we need to impose that
542 * is integral. If inv = B/d, this means that we need to impose that
548 * exists alpha in Z^m: B x = d alpha
550 * This function is similar to add_strides in isl_affine_hull.c
552 static __isl_give isl_basic_set
*add_strides(__isl_take isl_basic_set
*bset
,
553 __isl_keep isl_morph
*morph
)
558 if (isl_int_is_one(morph
->inv
->row
[0][0]))
563 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
) {
564 isl_seq_gcd(morph
->inv
->row
[1 + i
], morph
->inv
->n_col
, &gcd
);
565 if (isl_int_is_divisible_by(gcd
, morph
->inv
->row
[0][0]))
567 div
= isl_basic_set_alloc_div(bset
);
570 isl_int_set_si(bset
->div
[div
][0], 0);
571 k
= isl_basic_set_alloc_equality(bset
);
574 isl_seq_cpy(bset
->eq
[k
], morph
->inv
->row
[1 + i
],
576 isl_seq_clr(bset
->eq
[k
] + morph
->inv
->n_col
, bset
->n_div
);
577 isl_int_set(bset
->eq
[k
][morph
->inv
->n_col
+ div
],
578 morph
->inv
->row
[0][0]);
586 isl_basic_set_free(bset
);
590 /* Apply the morphism to the basic set.
591 * We basically just compute the preimage of "bset" under the inverse mapping
592 * in morph, add in stride constraints and intersect with the range
595 __isl_give isl_basic_set
*isl_morph_basic_set(__isl_take isl_morph
*morph
,
596 __isl_take isl_basic_set
*bset
)
598 isl_basic_set
*res
= NULL
;
606 isl_assert(bset
->ctx
, isl_space_is_equal(bset
->dim
, morph
->dom
->dim
),
609 max_stride
= morph
->inv
->n_row
- 1;
610 if (isl_int_is_one(morph
->inv
->row
[0][0]))
612 res
= isl_basic_set_alloc_space(isl_space_copy(morph
->ran
->dim
),
613 bset
->n_div
+ max_stride
, bset
->n_eq
+ max_stride
, bset
->n_ineq
);
615 for (i
= 0; i
< bset
->n_div
; ++i
)
616 if (isl_basic_set_alloc_div(res
) < 0)
619 mat
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
,
620 0, morph
->inv
->n_row
);
621 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
624 for (i
= 0; i
< bset
->n_eq
; ++i
) {
625 k
= isl_basic_set_alloc_equality(res
);
628 isl_seq_cpy(res
->eq
[k
], mat
->row
[i
], mat
->n_col
);
629 isl_seq_scale(res
->eq
[k
] + mat
->n_col
, bset
->eq
[i
] + mat
->n_col
,
630 morph
->inv
->row
[0][0], bset
->n_div
);
634 mat
= isl_mat_sub_alloc6(bset
->ctx
, bset
->ineq
, 0, bset
->n_ineq
,
635 0, morph
->inv
->n_row
);
636 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
639 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
640 k
= isl_basic_set_alloc_inequality(res
);
643 isl_seq_cpy(res
->ineq
[k
], mat
->row
[i
], mat
->n_col
);
644 isl_seq_scale(res
->ineq
[k
] + mat
->n_col
,
645 bset
->ineq
[i
] + mat
->n_col
,
646 morph
->inv
->row
[0][0], bset
->n_div
);
650 mat
= isl_mat_sub_alloc6(bset
->ctx
, bset
->div
, 0, bset
->n_div
,
651 1, morph
->inv
->n_row
);
652 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
655 for (i
= 0; i
< bset
->n_div
; ++i
) {
656 isl_int_mul(res
->div
[i
][0],
657 morph
->inv
->row
[0][0], bset
->div
[i
][0]);
658 isl_seq_cpy(res
->div
[i
] + 1, mat
->row
[i
], mat
->n_col
);
659 isl_seq_scale(res
->div
[i
] + 1 + mat
->n_col
,
660 bset
->div
[i
] + 1 + mat
->n_col
,
661 morph
->inv
->row
[0][0], bset
->n_div
);
665 res
= add_strides(res
, morph
);
667 if (isl_basic_set_is_rational(bset
))
668 res
= isl_basic_set_set_rational(res
);
670 res
= isl_basic_set_simplify(res
);
671 res
= isl_basic_set_finalize(res
);
673 res
= isl_basic_set_intersect(res
, isl_basic_set_copy(morph
->ran
));
675 isl_morph_free(morph
);
676 isl_basic_set_free(bset
);
680 isl_morph_free(morph
);
681 isl_basic_set_free(bset
);
682 isl_basic_set_free(res
);
686 /* Apply the morphism to the set.
688 __isl_give isl_set
*isl_morph_set(__isl_take isl_morph
*morph
,
689 __isl_take isl_set
*set
)
696 isl_assert(set
->ctx
, isl_space_is_equal(set
->dim
, morph
->dom
->dim
), goto error
);
698 set
= isl_set_cow(set
);
702 isl_space_free(set
->dim
);
703 set
->dim
= isl_space_copy(morph
->ran
->dim
);
707 for (i
= 0; i
< set
->n
; ++i
) {
708 set
->p
[i
] = isl_morph_basic_set(isl_morph_copy(morph
), set
->p
[i
]);
713 isl_morph_free(morph
);
715 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
720 isl_morph_free(morph
);
724 /* Construct a morphism that first does morph2 and then morph1.
726 __isl_give isl_morph
*isl_morph_compose(__isl_take isl_morph
*morph1
,
727 __isl_take isl_morph
*morph2
)
730 isl_basic_set
*dom
, *ran
;
732 if (!morph1
|| !morph2
)
735 map
= isl_mat_product(isl_mat_copy(morph1
->map
), isl_mat_copy(morph2
->map
));
736 inv
= isl_mat_product(isl_mat_copy(morph2
->inv
), isl_mat_copy(morph1
->inv
));
737 dom
= isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2
)),
738 isl_basic_set_copy(morph1
->dom
));
739 dom
= isl_basic_set_intersect(dom
, isl_basic_set_copy(morph2
->dom
));
740 ran
= isl_morph_basic_set(isl_morph_copy(morph1
),
741 isl_basic_set_copy(morph2
->ran
));
742 ran
= isl_basic_set_intersect(ran
, isl_basic_set_copy(morph1
->ran
));
744 isl_morph_free(morph1
);
745 isl_morph_free(morph2
);
747 return isl_morph_alloc(dom
, ran
, map
, inv
);
749 isl_morph_free(morph1
);
750 isl_morph_free(morph2
);
754 __isl_give isl_morph
*isl_morph_inverse(__isl_take isl_morph
*morph
)
759 morph
= isl_morph_cow(morph
);
764 morph
->dom
= morph
->ran
;
768 morph
->map
= morph
->inv
;
774 /* We detect all the equalities first to avoid implicit equalties
775 * being discovered during the computations. In particular,
776 * the compression on the variables could expose additional stride
777 * constraints on the parameters. This would result in existentially
778 * quantified variables after applying the resulting morph, which
779 * in turn could break invariants of the calling functions.
781 __isl_give isl_morph
*isl_basic_set_full_compression(
782 __isl_keep isl_basic_set
*bset
)
784 isl_morph
*morph
, *morph2
;
786 bset
= isl_basic_set_copy(bset
);
787 bset
= isl_basic_set_detect_equalities(bset
);
789 morph
= isl_basic_set_variable_compression(bset
, isl_dim_param
);
790 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
792 morph2
= isl_basic_set_parameter_compression(bset
);
793 bset
= isl_morph_basic_set(isl_morph_copy(morph2
), bset
);
795 morph
= isl_morph_compose(morph2
, morph
);
797 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
798 isl_basic_set_free(bset
);
800 morph
= isl_morph_compose(morph2
, morph
);
805 __isl_give isl_vec
*isl_morph_vec(__isl_take isl_morph
*morph
,
806 __isl_take isl_vec
*vec
)
811 vec
= isl_mat_vec_product(isl_mat_copy(morph
->map
), vec
);
813 isl_morph_free(morph
);
816 isl_morph_free(morph
);