isl_schedule_node_get_schedule_depth: return isl_size
[isl.git] / isl_morph.c
blob26a8c9428f2f3ffe8c1e3c05893081782eb1bbae
1 /*
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,
9 * 91893 Orsay, France
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>
16 #include <isl_seq.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)
24 if (!morph)
25 return NULL;
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)
33 isl_morph *morph;
35 if (!dom || !ran || !map || !inv)
36 goto error;
38 morph = isl_alloc_type(dom->ctx, struct isl_morph);
39 if (!morph)
40 goto error;
42 morph->ref = 1;
43 morph->dom = dom;
44 morph->ran = ran;
45 morph->map = map;
46 morph->inv = inv;
48 return morph;
49 error:
50 isl_basic_set_free(dom);
51 isl_basic_set_free(ran);
52 isl_mat_free(map);
53 isl_mat_free(inv);
54 return NULL;
57 __isl_give isl_morph *isl_morph_copy(__isl_keep isl_morph *morph)
59 if (!morph)
60 return NULL;
62 morph->ref++;
63 return morph;
66 __isl_give isl_morph *isl_morph_dup(__isl_keep isl_morph *morph)
68 if (!morph)
69 return NULL;
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)
78 if (!morph)
79 return NULL;
81 if (morph->ref == 1)
82 return morph;
83 morph->ref--;
84 return isl_morph_dup(morph);
87 __isl_null isl_morph *isl_morph_free(__isl_take isl_morph *morph)
89 if (!morph)
90 return NULL;
92 if (--morph->ref > 0)
93 return NULL;
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);
99 free(morph);
101 return NULL;
104 /* Is "morph" an identity on the parameters?
106 static isl_bool identity_on_parameters(__isl_keep isl_morph *morph)
108 isl_bool is_identity;
109 isl_size nparam, nparam_ran;
110 isl_mat *sub;
112 nparam = isl_morph_dom_dim(morph, isl_dim_param);
113 nparam_ran = isl_morph_ran_dim(morph, isl_dim_param);
114 if (nparam < 0 || nparam_ran < 0)
115 return isl_bool_error;
116 if (nparam != nparam_ran)
117 return isl_bool_false;
118 if (nparam == 0)
119 return isl_bool_true;
120 sub = isl_mat_sub_alloc(morph->map, 0, 1 + nparam, 0, 1 + nparam);
121 is_identity = isl_mat_is_scaled_identity(sub);
122 isl_mat_free(sub);
124 return is_identity;
127 /* Return an affine expression of the variables of the range of "morph"
128 * in terms of the parameters and the variables of the domain on "morph".
130 * In order for the space manipulations to make sense, we require
131 * that the parameters are not modified by "morph".
133 __isl_give isl_multi_aff *isl_morph_get_var_multi_aff(
134 __isl_keep isl_morph *morph)
136 isl_space *dom, *ran, *space;
137 isl_local_space *ls;
138 isl_multi_aff *ma;
139 isl_size nparam, nvar;
140 int i;
141 isl_bool is_identity;
143 if (!morph)
144 return NULL;
146 is_identity = identity_on_parameters(morph);
147 if (is_identity < 0)
148 return NULL;
149 if (!is_identity)
150 isl_die(isl_morph_get_ctx(morph), isl_error_invalid,
151 "cannot handle parameter compression", return NULL);
153 dom = isl_morph_get_dom_space(morph);
154 ls = isl_local_space_from_space(isl_space_copy(dom));
155 ran = isl_morph_get_ran_space(morph);
156 space = isl_space_map_from_domain_and_range(dom, ran);
157 ma = isl_multi_aff_zero(space);
159 nparam = isl_multi_aff_dim(ma, isl_dim_param);
160 nvar = isl_multi_aff_dim(ma, isl_dim_out);
161 if (nparam < 0 || nvar < 0)
162 ma = isl_multi_aff_free(ma);
163 for (i = 0; i < nvar; ++i) {
164 isl_val *val;
165 isl_vec *v;
166 isl_aff *aff;
168 v = isl_mat_get_row(morph->map, 1 + nparam + i);
169 v = isl_vec_insert_els(v, 0, 1);
170 val = isl_mat_get_element_val(morph->map, 0, 0);
171 v = isl_vec_set_element_val(v, 0, val);
172 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
173 ma = isl_multi_aff_set_aff(ma, i, aff);
176 isl_local_space_free(ls);
177 return ma;
180 /* Return the domain space of "morph".
182 __isl_give isl_space *isl_morph_get_dom_space(__isl_keep isl_morph *morph)
184 if (!morph)
185 return NULL;
187 return isl_basic_set_get_space(morph->dom);
190 __isl_give isl_space *isl_morph_get_ran_space(__isl_keep isl_morph *morph)
192 if (!morph)
193 return NULL;
195 return isl_space_copy(morph->ran->dim);
198 isl_size isl_morph_dom_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
200 if (!morph)
201 return isl_size_error;
203 return isl_basic_set_dim(morph->dom, type);
206 isl_size isl_morph_ran_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
208 if (!morph)
209 return isl_size_error;
211 return isl_basic_set_dim(morph->ran, type);
214 __isl_give isl_morph *isl_morph_remove_dom_dims(__isl_take isl_morph *morph,
215 enum isl_dim_type type, unsigned first, unsigned n)
217 unsigned dom_offset;
219 if (n == 0)
220 return morph;
222 morph = isl_morph_cow(morph);
223 if (!morph)
224 return NULL;
226 dom_offset = 1 + isl_space_offset(morph->dom->dim, type);
228 morph->dom = isl_basic_set_remove_dims(morph->dom, type, first, n);
230 morph->map = isl_mat_drop_cols(morph->map, dom_offset + first, n);
232 morph->inv = isl_mat_drop_rows(morph->inv, dom_offset + first, n);
234 if (morph->dom && morph->ran && morph->map && morph->inv)
235 return morph;
237 isl_morph_free(morph);
238 return NULL;
241 __isl_give isl_morph *isl_morph_remove_ran_dims(__isl_take isl_morph *morph,
242 enum isl_dim_type type, unsigned first, unsigned n)
244 unsigned ran_offset;
246 if (n == 0)
247 return morph;
249 morph = isl_morph_cow(morph);
250 if (!morph)
251 return NULL;
253 ran_offset = 1 + isl_space_offset(morph->ran->dim, type);
255 morph->ran = isl_basic_set_remove_dims(morph->ran, type, first, n);
257 morph->map = isl_mat_drop_rows(morph->map, ran_offset + first, n);
259 morph->inv = isl_mat_drop_cols(morph->inv, ran_offset + first, n);
261 if (morph->dom && morph->ran && morph->map && morph->inv)
262 return morph;
264 isl_morph_free(morph);
265 return NULL;
268 /* Project domain of morph onto its parameter domain.
270 __isl_give isl_morph *isl_morph_dom_params(__isl_take isl_morph *morph)
272 isl_size n;
274 morph = isl_morph_cow(morph);
275 if (!morph)
276 return NULL;
277 n = isl_basic_set_dim(morph->dom, isl_dim_set);
278 if (n < 0)
279 return isl_morph_free(morph);
280 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, n);
281 if (!morph)
282 return NULL;
283 morph->dom = isl_basic_set_params(morph->dom);
284 if (morph->dom)
285 return morph;
287 isl_morph_free(morph);
288 return NULL;
291 /* Project range of morph onto its parameter domain.
293 __isl_give isl_morph *isl_morph_ran_params(__isl_take isl_morph *morph)
295 isl_size n;
297 morph = isl_morph_cow(morph);
298 if (!morph)
299 return NULL;
300 n = isl_basic_set_dim(morph->ran, isl_dim_set);
301 if (n < 0)
302 return isl_morph_free(morph);
303 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, n);
304 if (!morph)
305 return NULL;
306 morph->ran = isl_basic_set_params(morph->ran);
307 if (morph->ran)
308 return morph;
310 isl_morph_free(morph);
311 return NULL;
314 void isl_morph_print_internal(__isl_take isl_morph *morph, FILE *out)
316 if (!morph)
317 return;
319 isl_basic_set_dump(morph->dom);
320 isl_basic_set_dump(morph->ran);
321 isl_mat_print_internal(morph->map, out, 4);
322 isl_mat_print_internal(morph->inv, out, 4);
325 void isl_morph_dump(__isl_take isl_morph *morph)
327 isl_morph_print_internal(morph, stderr);
330 __isl_give isl_morph *isl_morph_identity(__isl_keep isl_basic_set *bset)
332 isl_mat *id;
333 isl_basic_set *universe;
334 isl_size total;
336 total = isl_basic_set_dim(bset, isl_dim_all);
337 if (total < 0)
338 return NULL;
340 id = isl_mat_identity(bset->ctx, 1 + total);
341 universe = isl_basic_set_universe(isl_space_copy(bset->dim));
343 return isl_morph_alloc(universe, isl_basic_set_copy(universe),
344 id, isl_mat_copy(id));
347 /* Create a(n identity) morphism between empty sets of the same dimension
348 * a "bset".
350 __isl_give isl_morph *isl_morph_empty(__isl_keep isl_basic_set *bset)
352 isl_mat *id;
353 isl_basic_set *empty;
354 isl_size total;
356 total = isl_basic_set_dim(bset, isl_dim_all);
357 if (total < 0)
358 return NULL;
360 id = isl_mat_identity(bset->ctx, 1 + total);
361 empty = isl_basic_set_empty(isl_space_copy(bset->dim));
363 return isl_morph_alloc(empty, isl_basic_set_copy(empty),
364 id, isl_mat_copy(id));
367 /* Construct a basic set described by the "n" equalities of "bset" starting
368 * at "first".
370 static __isl_give isl_basic_set *copy_equalities(__isl_keep isl_basic_set *bset,
371 unsigned first, unsigned n)
373 int i, k;
374 isl_basic_set *eq;
375 isl_size total;
377 total = isl_basic_set_dim(bset, isl_dim_all);
378 if (total < 0 || isl_basic_set_check_no_locals(bset) < 0)
379 return NULL;
381 eq = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, n, 0);
382 if (!eq)
383 return NULL;
384 for (i = 0; i < n; ++i) {
385 k = isl_basic_set_alloc_equality(eq);
386 if (k < 0)
387 goto error;
388 isl_seq_cpy(eq->eq[k], bset->eq[first + i], 1 + total);
391 return eq;
392 error:
393 isl_basic_set_free(eq);
394 return NULL;
397 /* Given a basic set, exploit the equalities in the basic set to construct
398 * a morphism that maps the basic set to a lower-dimensional space
399 * with identifier "id".
400 * Specifically, the morphism reduces the number of dimensions of type "type".
402 * We first select the equalities of interest, that is those that involve
403 * variables of type "type" and no later variables.
404 * Denote those equalities as
406 * -C(p) + M x = 0
408 * where C(p) depends on the parameters if type == isl_dim_set and
409 * is a constant if type == isl_dim_param.
411 * Use isl_mat_final_variable_compression to construct a compression
413 * x = T x'
415 * x' = Q x
417 * If T is a zero-column matrix, then the set of equality constraints
418 * do not admit a solution. In this case, an empty morphism is returned.
420 * Both matrices are extended to map the full original space to the full
421 * compressed space.
423 __isl_give isl_morph *isl_basic_set_variable_compression_with_id(
424 __isl_keep isl_basic_set *bset, enum isl_dim_type type,
425 __isl_keep isl_id *id)
427 unsigned otype;
428 isl_size ntype;
429 unsigned orest;
430 unsigned nrest;
431 isl_size total;
432 int f_eq, n_eq;
433 isl_space *space;
434 isl_mat *E, *Q, *C;
435 isl_basic_set *dom, *ran;
437 if (!bset)
438 return NULL;
440 if (isl_basic_set_plain_is_empty(bset))
441 return isl_morph_empty(bset);
443 if (isl_basic_set_check_no_locals(bset) < 0)
444 return NULL;
446 ntype = isl_basic_set_dim(bset, type);
447 total = isl_basic_set_dim(bset, isl_dim_all);
448 if (ntype < 0 || total < 0)
449 return NULL;
450 otype = isl_basic_set_offset(bset, type);
451 orest = otype + ntype;
452 nrest = total - (orest - 1);
454 for (f_eq = 0; f_eq < bset->n_eq; ++f_eq)
455 if (isl_seq_first_non_zero(bset->eq[f_eq] + orest, nrest) == -1)
456 break;
457 for (n_eq = 0; f_eq + n_eq < bset->n_eq; ++n_eq)
458 if (isl_seq_first_non_zero(bset->eq[f_eq + n_eq] + otype, ntype) == -1)
459 break;
460 if (n_eq == 0)
461 return isl_morph_identity(bset);
463 E = isl_mat_sub_alloc6(bset->ctx, bset->eq, f_eq, n_eq, 0, orest);
464 C = isl_mat_final_variable_compression(E, otype - 1, &Q);
465 if (!Q)
466 C = isl_mat_free(C);
467 if (C && C->n_col == 0) {
468 isl_mat_free(C);
469 isl_mat_free(Q);
470 return isl_morph_empty(bset);
473 Q = isl_mat_diagonal(Q, isl_mat_identity(bset->ctx, nrest));
474 C = isl_mat_diagonal(C, isl_mat_identity(bset->ctx, nrest));
476 space = isl_space_copy(bset->dim);
477 space = isl_space_drop_dims(space, type, 0, ntype);
478 space = isl_space_add_dims(space, type, ntype - n_eq);
479 space = isl_space_set_tuple_id(space, isl_dim_set, isl_id_copy(id));
480 ran = isl_basic_set_universe(space);
481 dom = copy_equalities(bset, f_eq, n_eq);
483 return isl_morph_alloc(dom, ran, Q, C);
486 /* Given a basic set, exploit the equalities in the basic set to construct
487 * a morphism that maps the basic set to a lower-dimensional space.
488 * Specifically, the morphism reduces the number of dimensions of type "type".
490 __isl_give isl_morph *isl_basic_set_variable_compression(
491 __isl_keep isl_basic_set *bset, enum isl_dim_type type)
493 return isl_basic_set_variable_compression_with_id(bset, type,
494 &isl_id_none);
497 /* Construct a parameter compression for "bset".
498 * We basically just call isl_mat_parameter_compression with the right input
499 * and then extend the resulting matrix to include the variables.
501 * The implementation assumes that "bset" does not have any equalities
502 * that only involve the parameters and that isl_basic_set_gauss has
503 * been applied to "bset".
505 * Let the equalities be given as
507 * B(p) + A x = 0.
509 * We use isl_mat_parameter_compression_ext to compute the compression
511 * p = T p'.
513 __isl_give isl_morph *isl_basic_set_parameter_compression(
514 __isl_keep isl_basic_set *bset)
516 isl_size nparam;
517 isl_size nvar;
518 isl_size n_div;
519 int n_eq;
520 isl_mat *H, *B;
521 isl_mat *map, *inv;
522 isl_basic_set *dom, *ran;
524 if (!bset)
525 return NULL;
527 if (isl_basic_set_plain_is_empty(bset))
528 return isl_morph_empty(bset);
529 if (bset->n_eq == 0)
530 return isl_morph_identity(bset);
532 n_eq = bset->n_eq;
533 nparam = isl_basic_set_dim(bset, isl_dim_param);
534 nvar = isl_basic_set_dim(bset, isl_dim_set);
535 n_div = isl_basic_set_dim(bset, isl_dim_div);
536 if (nparam < 0 || nvar < 0 || n_div < 0)
537 return NULL;
539 if (isl_seq_first_non_zero(bset->eq[bset->n_eq - 1] + 1 + nparam,
540 nvar + n_div) == -1)
541 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
542 "input not allowed to have parameter equalities",
543 return NULL);
544 if (n_eq > nvar + n_div)
545 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
546 "input not gaussed", return NULL);
548 B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, n_eq, 0, 1 + nparam);
549 H = isl_mat_sub_alloc6(bset->ctx, bset->eq,
550 0, n_eq, 1 + nparam, nvar + n_div);
551 inv = isl_mat_parameter_compression_ext(B, H);
552 inv = isl_mat_diagonal(inv, isl_mat_identity(bset->ctx, nvar));
553 map = isl_mat_right_inverse(isl_mat_copy(inv));
555 dom = isl_basic_set_universe(isl_space_copy(bset->dim));
556 ran = isl_basic_set_universe(isl_space_copy(bset->dim));
558 return isl_morph_alloc(dom, ran, map, inv);
561 /* Add stride constraints to "bset" based on the inverse mapping
562 * that was plugged in. In particular, if morph maps x' to x,
563 * the constraints of the original input
565 * A x' + b >= 0
567 * have been rewritten to
569 * A inv x + b >= 0
571 * However, this substitution may loose information on the integrality of x',
572 * so we need to impose that
574 * inv x
576 * is integral. If inv = B/d, this means that we need to impose that
578 * B x = 0 mod d
580 * or
582 * exists alpha in Z^m: B x = d alpha
584 * This function is similar to add_strides in isl_affine_hull.c
586 static __isl_give isl_basic_set *add_strides(__isl_take isl_basic_set *bset,
587 __isl_keep isl_morph *morph)
589 int i, div, k;
590 isl_int gcd;
592 if (isl_int_is_one(morph->inv->row[0][0]))
593 return bset;
595 isl_int_init(gcd);
597 for (i = 0; 1 + i < morph->inv->n_row; ++i) {
598 isl_seq_gcd(morph->inv->row[1 + i], morph->inv->n_col, &gcd);
599 if (isl_int_is_divisible_by(gcd, morph->inv->row[0][0]))
600 continue;
601 div = isl_basic_set_alloc_div(bset);
602 if (div < 0)
603 goto error;
604 isl_int_set_si(bset->div[div][0], 0);
605 k = isl_basic_set_alloc_equality(bset);
606 if (k < 0)
607 goto error;
608 isl_seq_cpy(bset->eq[k], morph->inv->row[1 + i],
609 morph->inv->n_col);
610 isl_seq_clr(bset->eq[k] + morph->inv->n_col, bset->n_div);
611 isl_int_set(bset->eq[k][morph->inv->n_col + div],
612 morph->inv->row[0][0]);
615 isl_int_clear(gcd);
617 return bset;
618 error:
619 isl_int_clear(gcd);
620 isl_basic_set_free(bset);
621 return NULL;
624 /* Apply the morphism to the basic set.
625 * We basically just compute the preimage of "bset" under the inverse mapping
626 * in morph, add in stride constraints and intersect with the range
627 * of the morphism.
629 __isl_give isl_basic_set *isl_morph_basic_set(__isl_take isl_morph *morph,
630 __isl_take isl_basic_set *bset)
632 isl_basic_set *res = NULL;
633 isl_mat *mat = NULL;
634 int i, k;
635 int max_stride;
637 if (!morph || !bset)
638 goto error;
640 isl_assert(bset->ctx, isl_space_is_equal(bset->dim, morph->dom->dim),
641 goto error);
643 max_stride = morph->inv->n_row - 1;
644 if (isl_int_is_one(morph->inv->row[0][0]))
645 max_stride = 0;
646 res = isl_basic_set_alloc_space(isl_space_copy(morph->ran->dim),
647 bset->n_div + max_stride, bset->n_eq + max_stride, bset->n_ineq);
649 for (i = 0; i < bset->n_div; ++i)
650 if (isl_basic_set_alloc_div(res) < 0)
651 goto error;
653 mat = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq,
654 0, morph->inv->n_row);
655 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
656 if (!mat)
657 goto error;
658 for (i = 0; i < bset->n_eq; ++i) {
659 k = isl_basic_set_alloc_equality(res);
660 if (k < 0)
661 goto error;
662 isl_seq_cpy(res->eq[k], mat->row[i], mat->n_col);
663 isl_seq_scale(res->eq[k] + mat->n_col, bset->eq[i] + mat->n_col,
664 morph->inv->row[0][0], bset->n_div);
666 isl_mat_free(mat);
668 mat = isl_mat_sub_alloc6(bset->ctx, bset->ineq, 0, bset->n_ineq,
669 0, morph->inv->n_row);
670 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
671 if (!mat)
672 goto error;
673 for (i = 0; i < bset->n_ineq; ++i) {
674 k = isl_basic_set_alloc_inequality(res);
675 if (k < 0)
676 goto error;
677 isl_seq_cpy(res->ineq[k], mat->row[i], mat->n_col);
678 isl_seq_scale(res->ineq[k] + mat->n_col,
679 bset->ineq[i] + mat->n_col,
680 morph->inv->row[0][0], bset->n_div);
682 isl_mat_free(mat);
684 mat = isl_mat_sub_alloc6(bset->ctx, bset->div, 0, bset->n_div,
685 1, morph->inv->n_row);
686 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
687 if (!mat)
688 goto error;
689 for (i = 0; i < bset->n_div; ++i) {
690 isl_int_mul(res->div[i][0],
691 morph->inv->row[0][0], bset->div[i][0]);
692 isl_seq_cpy(res->div[i] + 1, mat->row[i], mat->n_col);
693 isl_seq_scale(res->div[i] + 1 + mat->n_col,
694 bset->div[i] + 1 + mat->n_col,
695 morph->inv->row[0][0], bset->n_div);
697 isl_mat_free(mat);
699 res = add_strides(res, morph);
701 if (isl_basic_set_is_rational(bset))
702 res = isl_basic_set_set_rational(res);
704 res = isl_basic_set_simplify(res);
705 res = isl_basic_set_finalize(res);
707 res = isl_basic_set_intersect(res, isl_basic_set_copy(morph->ran));
709 isl_morph_free(morph);
710 isl_basic_set_free(bset);
711 return res;
712 error:
713 isl_mat_free(mat);
714 isl_morph_free(morph);
715 isl_basic_set_free(bset);
716 isl_basic_set_free(res);
717 return NULL;
720 /* Apply the morphism to the set.
722 __isl_give isl_set *isl_morph_set(__isl_take isl_morph *morph,
723 __isl_take isl_set *set)
725 int i;
727 if (!morph || !set)
728 goto error;
730 isl_assert(set->ctx, isl_space_is_equal(set->dim, morph->dom->dim), goto error);
732 set = isl_set_cow(set);
733 if (!set)
734 goto error;
736 isl_space_free(set->dim);
737 set->dim = isl_space_copy(morph->ran->dim);
738 if (!set->dim)
739 goto error;
741 for (i = 0; i < set->n; ++i) {
742 set->p[i] = isl_morph_basic_set(isl_morph_copy(morph), set->p[i]);
743 if (!set->p[i])
744 goto error;
747 isl_morph_free(morph);
749 ISL_F_CLR(set, ISL_SET_NORMALIZED);
751 return set;
752 error:
753 isl_set_free(set);
754 isl_morph_free(morph);
755 return NULL;
758 /* Construct a morphism that first does morph2 and then morph1.
760 __isl_give isl_morph *isl_morph_compose(__isl_take isl_morph *morph1,
761 __isl_take isl_morph *morph2)
763 isl_mat *map, *inv;
764 isl_basic_set *dom, *ran;
766 if (!morph1 || !morph2)
767 goto error;
769 map = isl_mat_product(isl_mat_copy(morph1->map), isl_mat_copy(morph2->map));
770 inv = isl_mat_product(isl_mat_copy(morph2->inv), isl_mat_copy(morph1->inv));
771 dom = isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2)),
772 isl_basic_set_copy(morph1->dom));
773 dom = isl_basic_set_intersect(dom, isl_basic_set_copy(morph2->dom));
774 ran = isl_morph_basic_set(isl_morph_copy(morph1),
775 isl_basic_set_copy(morph2->ran));
776 ran = isl_basic_set_intersect(ran, isl_basic_set_copy(morph1->ran));
778 isl_morph_free(morph1);
779 isl_morph_free(morph2);
781 return isl_morph_alloc(dom, ran, map, inv);
782 error:
783 isl_morph_free(morph1);
784 isl_morph_free(morph2);
785 return NULL;
788 __isl_give isl_morph *isl_morph_inverse(__isl_take isl_morph *morph)
790 isl_basic_set *bset;
791 isl_mat *mat;
793 morph = isl_morph_cow(morph);
794 if (!morph)
795 return NULL;
797 bset = morph->dom;
798 morph->dom = morph->ran;
799 morph->ran = bset;
801 mat = morph->map;
802 morph->map = morph->inv;
803 morph->inv = mat;
805 return morph;
808 /* We detect all the equalities first to avoid implicit equalities
809 * being discovered during the computations. In particular,
810 * the compression on the variables could expose additional stride
811 * constraints on the parameters. This would result in existentially
812 * quantified variables after applying the resulting morph, which
813 * in turn could break invariants of the calling functions.
815 __isl_give isl_morph *isl_basic_set_full_compression(
816 __isl_keep isl_basic_set *bset)
818 isl_morph *morph, *morph2;
820 bset = isl_basic_set_copy(bset);
821 bset = isl_basic_set_detect_equalities(bset);
823 morph = isl_basic_set_variable_compression(bset, isl_dim_param);
824 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
826 morph2 = isl_basic_set_parameter_compression(bset);
827 bset = isl_morph_basic_set(isl_morph_copy(morph2), bset);
829 morph = isl_morph_compose(morph2, morph);
831 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
832 isl_basic_set_free(bset);
834 morph = isl_morph_compose(morph2, morph);
836 return morph;
839 __isl_give isl_vec *isl_morph_vec(__isl_take isl_morph *morph,
840 __isl_take isl_vec *vec)
842 if (!morph)
843 goto error;
845 vec = isl_mat_vec_product(isl_mat_copy(morph->map), vec);
847 isl_morph_free(morph);
848 return vec;
849 error:
850 isl_morph_free(morph);
851 isl_vec_free(vec);
852 return NULL;