048fc72f0b726b1237b6411d77bd8f853c2b0d66
[platform/upstream/isl.git] / isl_morph.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France 
9  */
10
11 #include <isl_morph.h>
12 #include <isl_seq.h>
13 #include <isl_map_private.h>
14 #include <isl_dim_private.h>
15 #include <isl_equalities.h>
16
17 __isl_give isl_morph *isl_morph_alloc(
18         __isl_take isl_basic_set *dom, __isl_take isl_basic_set *ran,
19         __isl_take isl_mat *map, __isl_take isl_mat *inv)
20 {
21         isl_morph *morph;
22
23         if (!dom || !ran || !map || !inv)
24                 goto error;
25
26         morph = isl_alloc_type(in_dim->ctx, struct isl_morph);
27         if (!morph)
28                 goto error;
29
30         morph->ref = 1;
31         morph->dom = dom;
32         morph->ran = ran;
33         morph->map = map;
34         morph->inv = inv;
35
36         return morph;
37 error:
38         isl_basic_set_free(dom);
39         isl_basic_set_free(ran);
40         isl_mat_free(map);
41         isl_mat_free(inv);
42         return NULL;
43 }
44
45 __isl_give isl_morph *isl_morph_copy(__isl_keep isl_morph *morph)
46 {
47         if (!morph)
48                 return NULL;
49
50         morph->ref++;
51         return morph;
52 }
53
54 __isl_give isl_morph *isl_morph_dup(__isl_keep isl_morph *morph)
55 {
56         if (!morph)
57                 return NULL;
58
59         return isl_morph_alloc(isl_basic_set_copy(morph->dom),
60                 isl_basic_set_copy(morph->ran),
61                 isl_mat_copy(morph->map), isl_mat_copy(morph->inv));
62 }
63
64 __isl_give isl_morph *isl_morph_cow(__isl_take isl_morph *morph)
65 {
66         if (!morph)
67                 return NULL;
68
69         if (morph->ref == 1)
70                 return morph;
71         morph->ref--;
72         return isl_morph_dup(morph);
73 }
74
75 void isl_morph_free(__isl_take isl_morph *morph)
76 {
77         if (!morph)
78                 return;
79
80         if (--morph->ref > 0)
81                 return;
82
83         isl_basic_set_free(morph->dom);
84         isl_basic_set_free(morph->ran);
85         isl_mat_free(morph->map);
86         isl_mat_free(morph->inv);
87         free(morph);
88 }
89
90 __isl_give isl_dim *isl_morph_get_ran_dim(__isl_keep isl_morph *morph)
91 {
92         if (!morph)
93                 return NULL;
94         
95         return isl_dim_copy(morph->ran->dim);
96 }
97
98 __isl_give isl_morph *isl_morph_remove_dom_dims(__isl_take isl_morph *morph,
99         enum isl_dim_type type, unsigned first, unsigned n)
100 {
101         unsigned dom_offset;
102
103         if (n == 0)
104                 return morph;
105
106         morph = isl_morph_cow(morph);
107         if (!morph)
108                 return NULL;
109
110         dom_offset = 1 + isl_dim_offset(morph->dom->dim, type);
111
112         morph->dom = isl_basic_set_remove(morph->dom, type, first, n);
113
114         morph->map = isl_mat_drop_cols(morph->map, dom_offset + first, n);
115
116         morph->inv = isl_mat_drop_rows(morph->inv, dom_offset + first, n);
117
118         if (morph->dom && morph->ran && morph->map && morph->inv)
119                 return morph;
120
121         isl_morph_free(morph);
122         return NULL;
123 }
124
125 __isl_give isl_morph *isl_morph_remove_ran_dims(__isl_take isl_morph *morph,
126         enum isl_dim_type type, unsigned first, unsigned n)
127 {
128         unsigned ran_offset;
129
130         if (n == 0)
131                 return morph;
132
133         morph = isl_morph_cow(morph);
134         if (!morph)
135                 return NULL;
136
137         ran_offset = 1 + isl_dim_offset(morph->ran->dim, type);
138
139         morph->ran = isl_basic_set_remove(morph->ran, type, first, n);
140
141         morph->map = isl_mat_drop_rows(morph->map, ran_offset + first, n);
142
143         morph->inv = isl_mat_drop_cols(morph->inv, ran_offset + first, n);
144
145         if (morph->dom && morph->ran && morph->map && morph->inv)
146                 return morph;
147
148         isl_morph_free(morph);
149         return NULL;
150 }
151
152 void isl_morph_dump(__isl_take isl_morph *morph, FILE *out)
153 {
154         if (!morph)
155                 return;
156
157         isl_basic_set_print(morph->dom, out, 0, "", "", ISL_FORMAT_ISL);
158         isl_basic_set_print(morph->ran, out, 0, "", "", ISL_FORMAT_ISL);
159         isl_mat_dump(morph->map, out, 4);
160         isl_mat_dump(morph->inv, out, 4);
161 }
162
163 __isl_give isl_morph *isl_morph_identity(__isl_keep isl_basic_set *bset)
164 {
165         isl_mat *id;
166         isl_basic_set *universe;
167         unsigned total;
168
169         if (!bset)
170                 return NULL;
171
172         total = isl_basic_set_total_dim(bset);
173         id = isl_mat_identity(bset->ctx, 1 + total);
174         universe = isl_basic_set_universe(isl_dim_copy(bset->dim));
175
176         return isl_morph_alloc(universe, isl_basic_set_copy(universe),
177                 id, isl_mat_copy(id));
178 }
179
180 /* Create a(n identity) morphism between empty sets of the same dimension
181  * a "bset".
182  */
183 __isl_give isl_morph *isl_morph_empty(__isl_keep isl_basic_set *bset)
184 {
185         isl_mat *id;
186         isl_basic_set *empty;
187         unsigned total;
188
189         if (!bset)
190                 return NULL;
191
192         total = isl_basic_set_total_dim(bset);
193         id = isl_mat_identity(bset->ctx, 1 + total);
194         empty = isl_basic_set_empty(isl_dim_copy(bset->dim));
195
196         return isl_morph_alloc(empty, isl_basic_set_copy(empty),
197                 id, isl_mat_copy(id));
198 }
199
200 /* Given a matrix that maps a (possibly) parametric domain to
201  * a parametric domain, add in rows that map the "nparam" parameters onto
202  * themselves.
203  */
204 static __isl_give isl_mat *insert_parameter_rows(__isl_take isl_mat *mat,
205         unsigned nparam)
206 {
207         int i;
208
209         if (nparam == 0)
210                 return mat;
211         if (!mat)
212                 return NULL;
213
214         mat = isl_mat_insert_rows(mat, 1, nparam);
215         if (!mat)
216                 return NULL;
217
218         for (i = 0; i < nparam; ++i) {
219                 isl_seq_clr(mat->row[1 + i], mat->n_col);
220                 isl_int_set(mat->row[1 + i][1 + i], mat->row[0][0]);
221         }
222
223         return mat;
224 }
225
226 /* Construct a basic set described by the "n" equalities of "bset" starting
227  * at "first".
228  */
229 static __isl_give isl_basic_set *copy_equalities(__isl_keep isl_basic_set *bset,
230         unsigned first, unsigned n)
231 {
232         int i, k;
233         isl_basic_set *eq;
234         unsigned total;
235
236         isl_assert(bset->ctx, bset->n_div == 0, return NULL);
237
238         total = isl_basic_set_total_dim(bset);
239         eq = isl_basic_set_alloc_dim(isl_dim_copy(bset->dim), 0, n, 0);
240         if (!eq)
241                 return NULL;
242         for (i = 0; i < n; ++i) {
243                 k = isl_basic_set_alloc_equality(eq);
244                 if (k < 0)
245                         goto error;
246                 isl_seq_cpy(eq->eq[k], bset->eq[first + k], 1 + total);
247         }
248
249         return eq;
250 error:
251         isl_basic_set_free(eq);
252         return NULL;
253 }
254
255 /* Given a basic set, exploit the equalties in the a basic set to construct
256  * a morphishm that maps the basic set to a lower-dimensional space.
257  * Specifically, the morphism reduces the number of dimensions of type "type".
258  *
259  * This function is a slight generalization of isl_mat_variable_compression
260  * in that it allows the input to be parametric and that it allows for the
261  * compression of either parameters or set variables.
262  *
263  * We first select the equalities of interest, that is those that involve
264  * variables of type "type" and no later variables.
265  * Denote those equalities as
266  *
267  *              -C(p) + M x = 0
268  *
269  * where C(p) depends on the parameters if type == isl_dim_set and
270  * is a constant if type == isl_dim_param.
271  *
272  * First compute the (left) Hermite normal form of M,
273  *
274  *              M [U1 U2] = M U = H = [H1 0]
275  * or
276  *                            M = H Q = [H1 0] [Q1]
277  *                                             [Q2]
278  *
279  * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
280  * Define the transformed variables as
281  *
282  *              x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
283  *                          [ x2' ]           [Q2]
284  *
285  * The equalities then become
286  *
287  *              -C(p) + H1 x1' = 0   or   x1' = H1^{-1} C(p) = C'(p)
288  *
289  * If the denominator of the constant term does not divide the
290  * the common denominator of the parametric terms, then every
291  * integer point is mapped to a non-integer point and then the original set has no
292  * integer solutions (since the x' are a unimodular transformation
293  * of the x).  In this case, an empty morphism is returned.
294  * Otherwise, the transformation is given by
295  *
296  *              x = U1 H1^{-1} C(p) + U2 x2'
297  *
298  * The inverse transformation is simply
299  *
300  *              x2' = Q2 x
301  *
302  * Both matrices are extended to map the full original space to the full
303  * compressed space.
304  */
305 __isl_give isl_morph *isl_basic_set_variable_compression(
306         __isl_keep isl_basic_set *bset, enum isl_dim_type type)
307 {
308         unsigned otype;
309         unsigned ntype;
310         unsigned orest;
311         unsigned nrest;
312         unsigned total;
313         int f_eq, n_eq;
314         isl_dim *dim;
315         isl_mat *H, *U, *Q, *C = NULL, *H1, *U1, *U2;
316         isl_basic_set *dom, *ran;
317
318         if (!bset)
319                 return NULL;
320
321         if (isl_basic_set_fast_is_empty(bset))
322                 return isl_morph_empty(bset);
323
324         isl_assert(bset->ctx, bset->n_div == 0, return NULL);
325
326         otype = 1 + isl_dim_offset(bset->dim, type);
327         ntype = isl_basic_set_dim(bset, type);
328         orest = otype + ntype;
329         nrest = isl_basic_set_total_dim(bset) - (orest - 1);
330
331         for (f_eq = 0; f_eq < bset->n_eq; ++f_eq)
332                 if (isl_seq_first_non_zero(bset->eq[f_eq] + orest, nrest) == -1)
333                         break;
334         for (n_eq = 0; f_eq + n_eq < bset->n_eq; ++n_eq)
335                 if (isl_seq_first_non_zero(bset->eq[f_eq + n_eq] + otype, ntype) == -1)
336                         break;
337         if (n_eq == 0)
338                 return isl_morph_identity(bset);
339
340         H = isl_mat_sub_alloc(bset->ctx, bset->eq, f_eq, n_eq, otype, ntype);
341         H = isl_mat_left_hermite(H, 0, &U, &Q);
342         if (!H || !U || !Q)
343                 goto error;
344         Q = isl_mat_drop_rows(Q, 0, n_eq);
345         Q = isl_mat_diagonal(isl_mat_identity(bset->ctx, otype), Q);
346         Q = isl_mat_diagonal(Q, isl_mat_identity(bset->ctx, nrest));
347         C = isl_mat_alloc(bset->ctx, 1 + n_eq, otype);
348         if (!C)
349                 goto error;
350         isl_int_set_si(C->row[0][0], 1);
351         isl_seq_clr(C->row[0] + 1, otype - 1);
352         isl_mat_sub_neg(C->ctx, C->row + 1, bset->eq + f_eq, n_eq, 0, 0, otype);
353         H1 = isl_mat_sub_alloc(H->ctx, H->row, 0, H->n_row, 0, H->n_row);
354         H1 = isl_mat_lin_to_aff(H1);
355         C = isl_mat_inverse_product(H1, C);
356         if (!C)
357                 goto error;
358         isl_mat_free(H);
359
360         if (!isl_int_is_one(C->row[0][0])) {
361                 int i;
362                 isl_int g;
363
364                 isl_int_init(g);
365                 for (i = 0; i < n_eq; ++i) {
366                         isl_seq_gcd(C->row[1 + i] + 1, otype - 1, &g);
367                         isl_int_gcd(g, g, C->row[0][0]);
368                         if (!isl_int_is_divisible_by(C->row[1 + i][0], g))
369                                 break;
370                 }
371                 isl_int_clear(g);
372
373                 if (i < n_eq) {
374                         isl_mat_free(C);
375                         isl_mat_free(U);
376                         isl_mat_free(Q);
377                         return isl_morph_empty(bset);
378                 }
379
380                 C = isl_mat_normalize(C);
381         }
382
383         U1 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row, 0, n_eq);
384         U1 = isl_mat_lin_to_aff(U1);
385         U2 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row, n_eq, U->n_row - n_eq);
386         U2 = isl_mat_lin_to_aff(U2);
387         isl_mat_free(U);
388
389         C = isl_mat_product(U1, C);
390         C = isl_mat_aff_direct_sum(C, U2);
391         C = insert_parameter_rows(C, otype - 1);
392         C = isl_mat_diagonal(C, isl_mat_identity(bset->ctx, nrest));
393
394         dim = isl_dim_copy(bset->dim);
395         dim = isl_dim_drop(dim, type, 0, ntype);
396         dim = isl_dim_add(dim, type, ntype - n_eq);
397         ran = isl_basic_set_universe(dim);
398         dom = copy_equalities(bset, f_eq, n_eq);
399
400         return isl_morph_alloc(dom, ran, Q, C);
401 error:
402         isl_mat_free(C);
403         isl_mat_free(H);
404         isl_mat_free(U);
405         isl_mat_free(Q);
406         return NULL;
407 }
408
409 /* Construct a parameter compression for "bset".
410  * We basically just call isl_mat_parameter_compression with the right input
411  * and then extend the resulting matrix to include the variables.
412  *
413  * Let the equalities be given as
414  *
415  *      B(p) + A x = 0
416  *
417  * and let [H 0] be the Hermite Normal Form of A, then
418  *
419  *      H^-1 B(p)
420  *
421  * needs to be integer, so we impose that each row is divisible by
422  * the denominator.
423  */
424 __isl_give isl_morph *isl_basic_set_parameter_compression(
425         __isl_keep isl_basic_set *bset)
426 {
427         unsigned nparam;
428         unsigned nvar;
429         int n_eq;
430         isl_mat *H, *B;
431         isl_vec *d;
432         isl_mat *map, *inv;
433         isl_basic_set *dom, *ran;
434
435         if (!bset)
436                 return NULL;
437
438         if (isl_basic_set_fast_is_empty(bset))
439                 return isl_morph_empty(bset);
440         if (bset->n_eq == 0)
441                 return isl_morph_identity(bset);
442
443         isl_assert(bset->ctx, bset->n_div == 0, return NULL);
444
445         n_eq = bset->n_eq;
446         nparam = isl_basic_set_dim(bset, isl_dim_param);
447         nvar = isl_basic_set_dim(bset, isl_dim_set);
448
449         isl_assert(bset->ctx, n_eq <= nvar, return NULL);
450
451         d = isl_vec_alloc(bset->ctx, n_eq);
452         B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, n_eq, 0, 1 + nparam);
453         H = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, n_eq, 1 + nparam, nvar);
454         H = isl_mat_left_hermite(H, 0, NULL, NULL);
455         H = isl_mat_drop_cols(H, n_eq, nvar - n_eq);
456         H = isl_mat_lin_to_aff(H);
457         H = isl_mat_right_inverse(H);
458         if (!H || !d)
459                 goto error;
460         isl_seq_set(d->el, H->row[0][0], d->size);
461         H = isl_mat_drop_rows(H, 0, 1);
462         H = isl_mat_drop_cols(H, 0, 1);
463         B = isl_mat_product(H, B);
464         inv = isl_mat_parameter_compression(B, d);
465         inv = isl_mat_diagonal(inv, isl_mat_identity(bset->ctx, nvar));
466         map = isl_mat_right_inverse(isl_mat_copy(inv));
467
468         dom = isl_basic_set_universe(isl_dim_copy(bset->dim));
469         ran = isl_basic_set_universe(isl_dim_copy(bset->dim));
470
471         return isl_morph_alloc(dom, ran, map, inv);
472 error:
473         isl_mat_free(H);
474         isl_mat_free(B);
475         isl_vec_free(d);
476         return NULL;
477 }
478
479 /* Add stride constraints to "bset" based on the inverse mapping
480  * that was plugged in.  In particular, if morph maps x' to x,
481  * the the constraints of the original input
482  *
483  *      A x' + b >= 0
484  *
485  * have been rewritten to
486  *
487  *      A inv x + b >= 0
488  *
489  * However, this substitution may loose information on the integrality of x',
490  * so we need to impose that
491  *
492  *      inv x
493  *
494  * is integral.  If inv = B/d, this means that we need to impose that
495  *
496  *      B x = 0         mod d
497  *
498  * or
499  *
500  *      exists alpha in Z^m: B x = d alpha
501  *
502  */
503 static __isl_give isl_basic_set *add_strides(__isl_take isl_basic_set *bset,
504         __isl_keep isl_morph *morph)
505 {
506         int i, div, k;
507         isl_int gcd;
508
509         if (isl_int_is_one(morph->inv->row[0][0]))
510                 return bset;
511
512         isl_int_init(gcd);
513
514         for (i = 0; 1 + i < morph->inv->n_row; ++i) {
515                 isl_seq_gcd(morph->inv->row[1 + i], morph->inv->n_col, &gcd);
516                 if (isl_int_is_divisible_by(gcd, morph->inv->row[0][0]))
517                         continue;
518                 div = isl_basic_set_alloc_div(bset);
519                 if (div < 0)
520                         goto error;
521                 k = isl_basic_set_alloc_equality(bset);
522                 if (k < 0)
523                         goto error;
524                 isl_seq_cpy(bset->eq[k], morph->inv->row[1 + i],
525                             morph->inv->n_col);
526                 isl_seq_clr(bset->eq[k] + morph->inv->n_col, bset->n_div);
527                 isl_int_set(bset->eq[k][morph->inv->n_col + div],
528                             morph->inv->row[0][0]);
529         }
530
531         isl_int_clear(gcd);
532
533         return bset;
534 error:
535         isl_int_clear(gcd);
536         isl_basic_set_free(bset);
537         return NULL;
538 }
539
540 /* Apply the morphism to the basic set.
541  * We basically just compute the preimage of "bset" under the inverse mapping
542  * in morph, add in stride constraints and intersect with the range
543  * of the morphism.
544  */
545 __isl_give isl_basic_set *isl_morph_basic_set(__isl_take isl_morph *morph,
546         __isl_take isl_basic_set *bset)
547 {
548         isl_basic_set *res = NULL;
549         isl_mat *mat = NULL;
550         int i, k;
551         int max_stride;
552
553         if (!morph || !bset)
554                 goto error;
555
556         isl_assert(bset->ctx, isl_dim_equal(bset->dim, morph->dom->dim),
557                     goto error);
558
559         max_stride = morph->inv->n_row - 1;
560         if (isl_int_is_one(morph->inv->row[0][0]))
561                 max_stride = 0;
562         res = isl_basic_set_alloc_dim(isl_dim_copy(morph->ran->dim),
563                 bset->n_div + max_stride, bset->n_eq + max_stride, bset->n_ineq);
564
565         for (i = 0; i < bset->n_div; ++i)
566                 if (isl_basic_set_alloc_div(res) < 0)
567                         goto error;
568
569         mat = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq,
570                                         0, morph->inv->n_row);
571         mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
572         if (!mat)
573                 goto error;
574         for (i = 0; i < bset->n_eq; ++i) {
575                 k = isl_basic_set_alloc_equality(res);
576                 if (k < 0)
577                         goto error;
578                 isl_seq_cpy(res->eq[k], mat->row[i], mat->n_col);
579                 isl_seq_scale(res->eq[k] + mat->n_col, bset->eq[i] + mat->n_col,
580                                 morph->inv->row[0][0], bset->n_div);
581         }
582         isl_mat_free(mat);
583
584         mat = isl_mat_sub_alloc(bset->ctx, bset->ineq, 0, bset->n_ineq,
585                                         0, morph->inv->n_row);
586         mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
587         if (!mat)
588                 goto error;
589         for (i = 0; i < bset->n_ineq; ++i) {
590                 k = isl_basic_set_alloc_inequality(res);
591                 if (k < 0)
592                         goto error;
593                 isl_seq_cpy(res->ineq[k], mat->row[i], mat->n_col);
594                 isl_seq_scale(res->ineq[k] + mat->n_col,
595                                 bset->ineq[i] + mat->n_col,
596                                 morph->inv->row[0][0], bset->n_div);
597         }
598         isl_mat_free(mat);
599
600         mat = isl_mat_sub_alloc(bset->ctx, bset->div, 0, bset->n_div,
601                                         1, morph->inv->n_row);
602         mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
603         if (!mat)
604                 goto error;
605         for (i = 0; i < bset->n_div; ++i) {
606                 isl_int_mul(res->div[i][0],
607                                 morph->inv->row[0][0], bset->div[i][0]);
608                 isl_seq_cpy(res->div[i] + 1, mat->row[i], mat->n_col);
609                 isl_seq_scale(res->div[i] + 1 + mat->n_col,
610                                 bset->div[i] + 1 + mat->n_col,
611                                 morph->inv->row[0][0], bset->n_div);
612         }
613         isl_mat_free(mat);
614
615         res = add_strides(res, morph);
616
617         res = isl_basic_set_simplify(res);
618         res = isl_basic_set_finalize(res);
619
620         res = isl_basic_set_intersect(res, isl_basic_set_copy(morph->ran));
621
622         isl_morph_free(morph);
623         isl_basic_set_free(bset);
624         return res;
625 error:
626         isl_mat_free(mat);
627         isl_morph_free(morph);
628         isl_basic_set_free(bset);
629         isl_basic_set_free(res);
630         return NULL;
631 }
632
633 /* Apply the morphism to the set.
634  */
635 __isl_give isl_set *isl_morph_set(__isl_take isl_morph *morph,
636         __isl_take isl_set *set)
637 {
638         int i;
639
640         if (!morph || !set)
641                 goto error;
642
643         isl_assert(set->ctx, isl_dim_equal(set->dim, morph->dom->dim), goto error);
644
645         set = isl_set_cow(set);
646         if (!set)
647                 goto error;
648
649         isl_dim_free(set->dim);
650         set->dim = isl_dim_copy(morph->ran->dim);
651         if (!set->dim)
652                 goto error;
653
654         for (i = 0; i < set->n; ++i) {
655                 set->p[i] = isl_morph_basic_set(isl_morph_copy(morph), set->p[i]);
656                 if (!set->p[i])
657                         goto error;
658         }
659
660         isl_morph_free(morph);
661
662         ISL_F_CLR(set, ISL_SET_NORMALIZED);
663
664         return set;
665 error:
666         isl_set_free(set);
667         isl_morph_free(morph);
668         return NULL;
669 }
670
671 /* Construct a morphism that first does morph2 and then morph1.
672  */
673 __isl_give isl_morph *isl_morph_compose(__isl_take isl_morph *morph1,
674         __isl_take isl_morph *morph2)
675 {
676         isl_mat *map, *inv;
677         isl_basic_set *dom, *ran;
678
679         if (!morph1 || !morph2)
680                 goto error;
681
682         map = isl_mat_product(isl_mat_copy(morph1->map), isl_mat_copy(morph2->map));
683         inv = isl_mat_product(isl_mat_copy(morph2->inv), isl_mat_copy(morph1->inv));
684         dom = isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2)),
685                                   isl_basic_set_copy(morph1->dom));
686         dom = isl_basic_set_intersect(dom, isl_basic_set_copy(morph2->dom));
687         ran = isl_morph_basic_set(isl_morph_copy(morph1),
688                                   isl_basic_set_copy(morph2->ran));
689         ran = isl_basic_set_intersect(ran, isl_basic_set_copy(morph1->ran));
690
691         isl_morph_free(morph1);
692         isl_morph_free(morph2);
693
694         return isl_morph_alloc(dom, ran, map, inv);
695 error:
696         isl_morph_free(morph1);
697         isl_morph_free(morph2);
698         return NULL;
699 }
700
701 __isl_give isl_morph *isl_morph_inverse(__isl_take isl_morph *morph)
702 {
703         isl_basic_set *bset;
704         isl_mat *mat;
705
706         morph = isl_morph_cow(morph);
707         if (!morph)
708                 return NULL;
709
710         bset = morph->dom;
711         morph->dom = morph->ran;
712         morph->ran = bset;
713
714         mat = morph->map;
715         morph->map = morph->inv;
716         morph->inv = mat;
717
718         return morph;
719 }