add isl_polyhedron_minimize application
[isl.git] / isl_affine_hull.c
blobce1b3073ca49b6cf9bcc91e41278fb3ad06dc64e
1 #include "isl_ctx.h"
2 #include "isl_seq.h"
3 #include "isl_set.h"
4 #include "isl_lp.h"
5 #include "isl_map.h"
6 #include "isl_map_private.h"
7 #include "isl_equalities.h"
8 #include "isl_sample.h"
9 #include "isl_tab.h"
11 struct isl_basic_map *isl_basic_map_implicit_equalities(
12 struct isl_basic_map *bmap)
14 struct isl_tab *tab;
16 if (!bmap)
17 return bmap;
19 bmap = isl_basic_map_gauss(bmap, NULL);
20 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
21 return bmap;
22 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
23 return bmap;
24 if (bmap->n_ineq <= 1)
25 return bmap;
27 tab = isl_tab_from_basic_map(bmap);
28 tab = isl_tab_detect_equalities(tab);
29 bmap = isl_basic_map_update_from_tab(bmap, tab);
30 isl_tab_free(tab);
31 bmap = isl_basic_map_gauss(bmap, NULL);
32 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
33 return bmap;
36 struct isl_basic_set *isl_basic_set_implicit_equalities(
37 struct isl_basic_set *bset)
39 return (struct isl_basic_set *)
40 isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
43 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
45 int i;
47 if (!map)
48 return map;
50 for (i = 0; i < map->n; ++i) {
51 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
52 if (!map->p[i])
53 goto error;
56 return map;
57 error:
58 isl_map_free(map);
59 return NULL;
62 /* Make eq[row][col] of both bmaps equal so we can add the row
63 * add the column to the common matrix.
64 * Note that because of the echelon form, the columns of row row
65 * after column col are zero.
67 static void set_common_multiple(
68 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
69 unsigned row, unsigned col)
71 isl_int m, c;
73 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
74 return;
76 isl_int_init(c);
77 isl_int_init(m);
78 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
79 isl_int_divexact(c, m, bset1->eq[row][col]);
80 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
81 isl_int_divexact(c, m, bset2->eq[row][col]);
82 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
83 isl_int_clear(c);
84 isl_int_clear(m);
87 /* Delete a given equality, moving all the following equalities one up.
89 static void delete_row(struct isl_basic_set *bset, unsigned row)
91 isl_int *t;
92 int r;
94 t = bset->eq[row];
95 bset->n_eq--;
96 for (r = row; r < bset->n_eq; ++r)
97 bset->eq[r] = bset->eq[r+1];
98 bset->eq[bset->n_eq] = t;
101 /* Make first row entries in column col of bset1 identical to
102 * those of bset2, using the fact that entry bset1->eq[row][col]=a
103 * is non-zero. Initially, these elements of bset1 are all zero.
104 * For each row i < row, we set
105 * A[i] = a * A[i] + B[i][col] * A[row]
106 * B[i] = a * B[i]
107 * so that
108 * A[i][col] = B[i][col] = a * old(B[i][col])
110 static void construct_column(
111 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
112 unsigned row, unsigned col)
114 int r;
115 isl_int a;
116 isl_int b;
117 unsigned total;
119 isl_int_init(a);
120 isl_int_init(b);
121 total = 1 + isl_basic_set_n_dim(bset1);
122 for (r = 0; r < row; ++r) {
123 if (isl_int_is_zero(bset2->eq[r][col]))
124 continue;
125 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
126 isl_int_divexact(a, bset1->eq[row][col], b);
127 isl_int_divexact(b, bset2->eq[r][col], b);
128 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
129 b, bset1->eq[row], total);
130 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
132 isl_int_clear(a);
133 isl_int_clear(b);
134 delete_row(bset1, row);
137 /* Make first row entries in column col of bset1 identical to
138 * those of bset2, using only these entries of the two matrices.
139 * Let t be the last row with different entries.
140 * For each row i < t, we set
141 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
142 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
143 * so that
144 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
146 static int transform_column(
147 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
148 unsigned row, unsigned col)
150 int i, t;
151 isl_int a, b, g;
152 unsigned total;
154 for (t = row-1; t >= 0; --t)
155 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
156 break;
157 if (t < 0)
158 return 0;
160 total = 1 + isl_basic_set_n_dim(bset1);
161 isl_int_init(a);
162 isl_int_init(b);
163 isl_int_init(g);
164 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
165 for (i = 0; i < t; ++i) {
166 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
167 isl_int_gcd(g, a, b);
168 isl_int_divexact(a, a, g);
169 isl_int_divexact(g, b, g);
170 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
171 total);
172 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
173 total);
175 isl_int_clear(a);
176 isl_int_clear(b);
177 isl_int_clear(g);
178 delete_row(bset1, t);
179 delete_row(bset2, t);
180 return 1;
183 /* The implementation is based on Section 5.2 of Michael Karr,
184 * "Affine Relationships Among Variables of a Program",
185 * except that the echelon form we use starts from the last column
186 * and that we are dealing with integer coefficients.
188 static struct isl_basic_set *affine_hull(
189 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
191 unsigned total;
192 int col;
193 int row;
195 total = 1 + isl_basic_set_n_dim(bset1);
197 row = 0;
198 for (col = total-1; col >= 0; --col) {
199 int is_zero1 = row >= bset1->n_eq ||
200 isl_int_is_zero(bset1->eq[row][col]);
201 int is_zero2 = row >= bset2->n_eq ||
202 isl_int_is_zero(bset2->eq[row][col]);
203 if (!is_zero1 && !is_zero2) {
204 set_common_multiple(bset1, bset2, row, col);
205 ++row;
206 } else if (!is_zero1 && is_zero2) {
207 construct_column(bset1, bset2, row, col);
208 } else if (is_zero1 && !is_zero2) {
209 construct_column(bset2, bset1, row, col);
210 } else {
211 if (transform_column(bset1, bset2, row, col))
212 --row;
215 isl_basic_set_free(bset2);
216 isl_assert(ctx, row == bset1->n_eq, goto error);
217 bset1 = isl_basic_set_normalize_constraints(bset1);
218 return bset1;
219 error:
220 isl_basic_set_free(bset1);
221 return NULL;
224 static struct isl_basic_set *isl_basic_set_from_vec(struct isl_vec *vec)
226 int i;
227 int k;
228 struct isl_basic_set *bset = NULL;
229 struct isl_ctx *ctx;
230 unsigned dim;
232 if (!vec)
233 return NULL;
234 ctx = vec->ctx;
235 isl_assert(ctx, vec->size != 0, goto error);
237 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
238 if (!bset)
239 goto error;
240 dim = isl_basic_set_n_dim(bset);
241 for (i = dim - 1; i >= 0; --i) {
242 k = isl_basic_set_alloc_equality(bset);
243 if (k < 0)
244 goto error;
245 isl_seq_clr(bset->eq[k], 1 + dim);
246 isl_int_neg(bset->eq[k][0], vec->el[1 + i]);
247 isl_int_set(bset->eq[k][1 + i], vec->el[0]);
249 isl_vec_free(vec);
251 return bset;
252 error:
253 isl_basic_set_free(bset);
254 isl_vec_free(vec);
255 return NULL;
258 /* Find an integer point in "bset" that lies outside of the equality
259 * "eq" e(x) = 0.
260 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
261 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
262 * The point, if found, is returned as a singleton set.
263 * If no point can be found, the empty set is returned.
265 * Before solving an ILP problem, we first check if simply
266 * adding the normal of the constraint to one of the known
267 * integer points in the basic set yields another point
268 * inside the basic set.
270 * The caller of this function ensures that "bset" is bounded.
272 static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
273 struct isl_basic_set *bset, isl_int *eq, int up)
275 struct isl_basic_set *slice = NULL;
276 struct isl_vec *sample;
277 struct isl_basic_set *point;
278 unsigned dim;
279 int k;
281 dim = isl_basic_set_n_dim(bset);
282 sample = isl_vec_alloc(ctx, 1 + dim);
283 if (!sample)
284 return NULL;
285 isl_int_set_si(sample->block.data[0], 1);
286 isl_seq_combine(sample->block.data + 1,
287 ctx->one, bset->sample->block.data + 1,
288 up ? ctx->one : ctx->negone, eq + 1, dim);
289 if (isl_basic_set_contains(bset, sample))
290 return isl_basic_set_from_vec(sample);
291 isl_vec_free(sample);
292 sample = NULL;
294 slice = isl_basic_set_copy(bset);
295 if (!slice)
296 goto error;
297 slice = isl_basic_set_cow(slice);
298 slice = isl_basic_set_extend(slice, 0, dim, 0, 0, 1);
299 k = isl_basic_set_alloc_inequality(slice);
300 if (k < 0)
301 goto error;
302 if (up)
303 isl_seq_cpy(slice->ineq[k], eq, 1 + dim);
304 else
305 isl_seq_neg(slice->ineq[k], eq, 1 + dim);
306 isl_int_sub_ui(slice->ineq[k][0], slice->ineq[k][0], 1);
308 sample = isl_basic_set_sample_bounded(slice);
309 if (!sample)
310 goto error;
311 if (sample->size == 0) {
312 isl_vec_free(sample);
313 point = isl_basic_set_empty_like(bset);
314 } else
315 point = isl_basic_set_from_vec(sample);
317 return point;
318 error:
319 isl_basic_set_free(slice);
320 return NULL;
323 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
325 int i;
327 bset = isl_basic_set_cow(bset);
328 if (!bset)
329 return NULL;
330 isl_assert(bset->ctx, bset->n_div == 0, goto error);
332 for (i = 0; i < bset->n_eq; ++i)
333 isl_int_set_si(bset->eq[i][0], 0);
335 for (i = 0; i < bset->n_ineq; ++i)
336 isl_int_set_si(bset->ineq[i][0], 0);
338 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
339 return isl_basic_set_implicit_equalities(bset);
340 error:
341 isl_basic_set_free(bset);
342 return NULL;
345 /* Extend an initial (under-)approximation of the affine hull of "bset"
346 * by looking for points that do not satisfy one of the equalities
347 * in the current approximation and adding them to that approximation
348 * until no such points can be found any more.
350 * The caller of this function ensures that "bset" is bounded.
352 static struct isl_basic_set *extend_affine_hull(struct isl_basic_set *bset,
353 struct isl_basic_set *hull)
355 int i, j, k;
356 struct isl_ctx *ctx;
357 unsigned dim;
359 ctx = bset->ctx;
360 dim = isl_basic_set_n_dim(bset);
361 for (i = 0; i < dim; ++i) {
362 struct isl_basic_set *point;
363 for (j = 0; j < hull->n_eq; ++j) {
364 point = outside_point(ctx, bset, hull->eq[j], 1);
365 if (!point)
366 goto error;
367 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
368 break;
369 isl_basic_set_free(point);
370 point = outside_point(ctx, bset, hull->eq[j], 0);
371 if (!point)
372 goto error;
373 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
374 break;
375 isl_basic_set_free(point);
377 bset = isl_basic_set_extend_constraints(bset, 1, 0);
378 k = isl_basic_set_alloc_equality(bset);
379 if (k < 0)
380 goto error;
381 isl_seq_cpy(bset->eq[k], hull->eq[j],
382 1 + isl_basic_set_total_dim(hull));
383 bset = isl_basic_set_gauss(bset, NULL);
384 if (!bset)
385 goto error;
387 if (j == hull->n_eq)
388 break;
389 hull = affine_hull(hull, point);
391 isl_basic_set_free(bset);
393 return hull;
394 error:
395 isl_basic_set_free(bset);
396 isl_basic_set_free(hull);
397 return NULL;
400 /* Drop all constraints in bset that involve any of the dimensions
401 * first to first+n-1.
403 static struct isl_basic_set *drop_constraints_involving
404 (struct isl_basic_set *bset, unsigned first, unsigned n)
406 int i;
408 if (!bset)
409 return NULL;
411 bset = isl_basic_set_cow(bset);
413 for (i = bset->n_eq - 1; i >= 0; --i) {
414 if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
415 continue;
416 isl_basic_set_drop_equality(bset, i);
419 for (i = bset->n_ineq - 1; i >= 0; --i) {
420 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
421 continue;
422 isl_basic_set_drop_inequality(bset, i);
425 return bset;
428 /* Compute the affine hull of "bset", where "hull" is an initial approximation
429 * with only a single point of "bset" and "cone" is the recession cone
430 * of "bset".
432 * We first compute a unimodular transformation that puts the unbounded
433 * directions in the last dimensions. In particular, we take a transformation
434 * that maps all equalities to equalities (in HNF) on the first dimensions.
435 * Let x be the original dimensions and y the transformed, with y_1 bounded
436 * and y_2 unbounded.
438 * [ y_1 ] [ y_1 ] [ Q_1 ]
439 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
441 * Let's call the input basic set S and the initial hull H.
442 * We compute S' = preimage(S, U) and H' = preimage(H, U)
443 * and drop the final dimensions including any constraints involving them.
444 * This results in sets S'' and H''.
445 * Then we extend H'' to the affine hull A'' of S''.
446 * Let F y_1 >= g be the constraint system of A''. In the transformed
447 * space the y_2 are unbounded, so we can add them back without any constraints,
448 * resulting in
450 * [ y_1 ]
451 * [ F 0 ] [ y_2 ] >= g
452 * or
453 * [ Q_1 ]
454 * [ F 0 ] [ Q_2 ] x >= g
455 * or
456 * F Q_1 x >= g
458 * The affine hull in the original space is then obtained as
459 * A = preimage(A'', Q_1).
461 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
462 struct isl_basic_set *hull, struct isl_basic_set *cone)
464 unsigned total;
465 unsigned cone_dim;
466 struct isl_mat *M, *U, *Q;
468 if (!bset || !hull || !cone)
469 goto error;
471 total = isl_basic_set_total_dim(cone);
472 cone_dim = total - cone->n_eq;
474 M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
475 M = isl_mat_left_hermite(M, 0, &U, &Q);
476 if (!M)
477 goto error;
478 isl_mat_free(M);
480 U = isl_mat_lin_to_aff(U);
481 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
482 hull = isl_basic_set_preimage(hull, U);
484 bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
485 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
486 hull = drop_constraints_involving(hull, total - cone_dim, cone_dim);
487 hull = isl_basic_set_drop_dims(hull, total - cone_dim, cone_dim);
489 Q = isl_mat_lin_to_aff(Q);
490 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
492 if (bset && bset->sample)
493 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
495 hull = extend_affine_hull(bset, hull);
497 hull = isl_basic_set_preimage(hull, Q);
499 isl_basic_set_free(cone);
501 return hull;
502 error:
503 isl_basic_set_free(bset);
504 isl_basic_set_free(hull);
505 isl_basic_set_free(cone);
506 return NULL;
509 /* Look for all equalities satisfied by the integer points in bset,
510 * which is assumed not to have any explicit equalities.
512 * The equalities are obtained by successively looking for
513 * a point that is affinely independent of the points found so far.
514 * In particular, for each equality satisfied by the points so far,
515 * we check if there is any point on a hyperplane parallel to the
516 * corresponding hyperplane shifted by at least one (in either direction).
518 * Before looking for any outside points, we first compute the recession
519 * cone. The directions of this recession cone will always be part
520 * of the affine hull, so there is no need for looking for any points
521 * in these directions.
522 * In particular, if the recession cone is full-dimensional, then
523 * the affine hull is simply the whole universe.
525 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
527 struct isl_basic_set *hull = NULL;
528 struct isl_vec *sample = NULL;
529 struct isl_basic_set *cone;
531 if (isl_basic_set_is_empty(bset))
532 return bset;
534 sample = isl_basic_set_sample(isl_basic_set_copy(bset));
535 if (!sample)
536 goto error;
537 if (sample->size == 0) {
538 isl_vec_free(sample);
539 hull = isl_basic_set_empty_like(bset);
540 isl_basic_set_free(bset);
541 return hull;
543 if (sample->size == 1) {
544 isl_vec_free(sample);
545 return bset;
548 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
549 if (!cone)
550 goto error;
551 if (cone->n_eq == 0) {
552 isl_basic_set_free(cone);
553 isl_vec_free(sample);
554 hull = isl_basic_set_universe_like(bset);
555 isl_basic_set_free(bset);
556 return hull;
559 hull = isl_basic_set_from_vec(sample);
560 if (cone->n_eq < isl_basic_set_total_dim(cone))
561 return affine_hull_with_cone(bset, hull, cone);
563 isl_basic_set_free(cone);
564 return extend_affine_hull(bset, hull);
565 error:
566 isl_vec_free(sample);
567 isl_basic_set_free(bset);
568 isl_basic_set_free(hull);
569 return NULL;
572 /* Look for all equalities satisfied by the integer points in bmap
573 * that are independent of the equalities already explicitly available
574 * in bmap.
576 * We first remove all equalities already explicitly available,
577 * then look for additional equalities in the reduced space
578 * and then transform the result to the original space.
579 * The original equalities are _not_ added to this set. This is
580 * the responsibility of the calling function.
581 * The resulting basic set has all meaning about the dimensions removed.
582 * In particular, dimensions that correspond to existential variables
583 * in bmap and that are found to be fixed are not removed.
585 static struct isl_basic_set *equalities_in_underlying_set(
586 struct isl_basic_map *bmap)
588 struct isl_mat *T2 = NULL;
589 struct isl_basic_set *bset = NULL;
590 struct isl_basic_set *hull = NULL;
592 bset = isl_basic_map_underlying_set(bmap);
593 bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
594 if (!bset)
595 goto error;
597 hull = uset_affine_hull(bset);
598 if (T2)
599 hull = isl_basic_set_preimage(hull, T2);
601 return hull;
602 error:
603 isl_mat_free(T2);
604 isl_basic_set_free(bset);
605 isl_basic_set_free(hull);
606 return NULL;
609 /* Detect and make explicit all equalities satisfied by the (integer)
610 * points in bmap.
612 struct isl_basic_map *isl_basic_map_detect_equalities(
613 struct isl_basic_map *bmap)
615 int i, j;
616 struct isl_basic_set *hull = NULL;
618 if (!bmap)
619 return NULL;
620 if (bmap->n_ineq == 0)
621 return bmap;
622 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
623 return bmap;
624 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
625 return bmap;
626 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
627 return isl_basic_map_implicit_equalities(bmap);
629 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
630 if (!hull)
631 goto error;
632 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
633 isl_basic_set_free(hull);
634 return isl_basic_map_set_to_empty(bmap);
636 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
637 hull->n_eq, 0);
638 for (i = 0; i < hull->n_eq; ++i) {
639 j = isl_basic_map_alloc_equality(bmap);
640 if (j < 0)
641 goto error;
642 isl_seq_cpy(bmap->eq[j], hull->eq[i],
643 1 + isl_basic_set_total_dim(hull));
645 isl_basic_set_free(hull);
646 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
647 bmap = isl_basic_map_simplify(bmap);
648 return isl_basic_map_finalize(bmap);
649 error:
650 isl_basic_set_free(hull);
651 isl_basic_map_free(bmap);
652 return NULL;
655 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
657 struct isl_basic_map *bmap;
658 int i;
660 if (!map)
661 return NULL;
663 for (i = 0; i < map->n; ++i) {
664 bmap = isl_basic_map_copy(map->p[i]);
665 bmap = isl_basic_map_detect_equalities(bmap);
666 if (!bmap)
667 goto error;
668 isl_basic_map_free(map->p[i]);
669 map->p[i] = bmap;
672 return map;
673 error:
674 isl_map_free(map);
675 return NULL;
678 /* After computing the rational affine hull (by detecting the implicit
679 * equalities), we compute the additional equalities satisfied by
680 * the integer points (if any) and add the original equalities back in.
682 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
684 struct isl_basic_set *hull = NULL;
686 bmap = isl_basic_map_detect_equalities(bmap);
687 bmap = isl_basic_map_cow(bmap);
688 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
689 return bmap;
692 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
694 return (struct isl_basic_set *)
695 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
698 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
700 int i;
701 struct isl_basic_map *model = NULL;
702 struct isl_basic_map *hull = NULL;
703 struct isl_set *set;
705 if (!map)
706 return NULL;
708 if (map->n == 0) {
709 hull = isl_basic_map_empty_like_map(map);
710 isl_map_free(map);
711 return hull;
714 map = isl_map_detect_equalities(map);
715 map = isl_map_align_divs(map);
716 if (!map)
717 return NULL;
718 model = isl_basic_map_copy(map->p[0]);
719 set = isl_map_underlying_set(map);
720 set = isl_set_cow(set);
721 if (!set)
722 goto error;
724 for (i = 0; i < set->n; ++i) {
725 set->p[i] = isl_basic_set_cow(set->p[i]);
726 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
727 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
728 if (!set->p[i])
729 goto error;
731 set = isl_set_remove_empty_parts(set);
732 if (set->n == 0) {
733 hull = isl_basic_map_empty_like(model);
734 isl_basic_map_free(model);
735 } else {
736 struct isl_basic_set *bset;
737 while (set->n > 1) {
738 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
739 if (!set->p[0])
740 goto error;
742 bset = isl_basic_set_copy(set->p[0]);
743 hull = isl_basic_map_overlying_set(bset, model);
745 isl_set_free(set);
746 hull = isl_basic_map_simplify(hull);
747 return isl_basic_map_finalize(hull);
748 error:
749 isl_basic_map_free(model);
750 isl_set_free(set);
751 return NULL;
754 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
756 return (struct isl_basic_set *)
757 isl_map_affine_hull((struct isl_map *)set);