isl_coalesce.c: extend: allow multiple relaxed constraints
[isl.git] / isl_affine_hull.c
blobcb95b5c8b462a70a5c3154b3e18cef03f282fd3b
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_seq.h>
18 #include <isl/set.h>
19 #include <isl/lp.h>
20 #include <isl/map.h>
21 #include "isl_equalities.h"
22 #include "isl_sample.h"
23 #include "isl_tab.h"
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
27 #include <bset_to_bmap.c>
28 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
32 struct isl_basic_map *isl_basic_map_implicit_equalities(
33 struct isl_basic_map *bmap)
35 struct isl_tab *tab;
37 if (!bmap)
38 return bmap;
40 bmap = isl_basic_map_gauss(bmap, NULL);
41 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
42 return bmap;
43 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
44 return bmap;
45 if (bmap->n_ineq <= 1)
46 return bmap;
48 tab = isl_tab_from_basic_map(bmap, 0);
49 if (isl_tab_detect_implicit_equalities(tab) < 0)
50 goto error;
51 bmap = isl_basic_map_update_from_tab(bmap, tab);
52 isl_tab_free(tab);
53 bmap = isl_basic_map_gauss(bmap, NULL);
54 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
55 return bmap;
56 error:
57 isl_tab_free(tab);
58 isl_basic_map_free(bmap);
59 return NULL;
62 struct isl_basic_set *isl_basic_set_implicit_equalities(
63 struct isl_basic_set *bset)
65 return bset_from_bmap(
66 isl_basic_map_implicit_equalities(bset_to_bmap(bset)));
69 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
71 int i;
73 if (!map)
74 return map;
76 for (i = 0; i < map->n; ++i) {
77 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
78 if (!map->p[i])
79 goto error;
82 return map;
83 error:
84 isl_map_free(map);
85 return NULL;
88 /* Make eq[row][col] of both bmaps equal so we can add the row
89 * add the column to the common matrix.
90 * Note that because of the echelon form, the columns of row row
91 * after column col are zero.
93 static void set_common_multiple(
94 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
95 unsigned row, unsigned col)
97 isl_int m, c;
99 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
100 return;
102 isl_int_init(c);
103 isl_int_init(m);
104 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
105 isl_int_divexact(c, m, bset1->eq[row][col]);
106 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
107 isl_int_divexact(c, m, bset2->eq[row][col]);
108 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
109 isl_int_clear(c);
110 isl_int_clear(m);
113 /* Delete a given equality, moving all the following equalities one up.
115 static void delete_row(struct isl_basic_set *bset, unsigned row)
117 isl_int *t;
118 int r;
120 t = bset->eq[row];
121 bset->n_eq--;
122 for (r = row; r < bset->n_eq; ++r)
123 bset->eq[r] = bset->eq[r+1];
124 bset->eq[bset->n_eq] = t;
127 /* Make first row entries in column col of bset1 identical to
128 * those of bset2, using the fact that entry bset1->eq[row][col]=a
129 * is non-zero. Initially, these elements of bset1 are all zero.
130 * For each row i < row, we set
131 * A[i] = a * A[i] + B[i][col] * A[row]
132 * B[i] = a * B[i]
133 * so that
134 * A[i][col] = B[i][col] = a * old(B[i][col])
136 static void construct_column(
137 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
138 unsigned row, unsigned col)
140 int r;
141 isl_int a;
142 isl_int b;
143 unsigned total;
145 isl_int_init(a);
146 isl_int_init(b);
147 total = 1 + isl_basic_set_n_dim(bset1);
148 for (r = 0; r < row; ++r) {
149 if (isl_int_is_zero(bset2->eq[r][col]))
150 continue;
151 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
152 isl_int_divexact(a, bset1->eq[row][col], b);
153 isl_int_divexact(b, bset2->eq[r][col], b);
154 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
155 b, bset1->eq[row], total);
156 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
158 isl_int_clear(a);
159 isl_int_clear(b);
160 delete_row(bset1, row);
163 /* Make first row entries in column col of bset1 identical to
164 * those of bset2, using only these entries of the two matrices.
165 * Let t be the last row with different entries.
166 * For each row i < t, we set
167 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
168 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
169 * so that
170 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
172 static int transform_column(
173 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
174 unsigned row, unsigned col)
176 int i, t;
177 isl_int a, b, g;
178 unsigned total;
180 for (t = row-1; t >= 0; --t)
181 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
182 break;
183 if (t < 0)
184 return 0;
186 total = 1 + isl_basic_set_n_dim(bset1);
187 isl_int_init(a);
188 isl_int_init(b);
189 isl_int_init(g);
190 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
191 for (i = 0; i < t; ++i) {
192 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
193 isl_int_gcd(g, a, b);
194 isl_int_divexact(a, a, g);
195 isl_int_divexact(g, b, g);
196 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
197 total);
198 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
199 total);
201 isl_int_clear(a);
202 isl_int_clear(b);
203 isl_int_clear(g);
204 delete_row(bset1, t);
205 delete_row(bset2, t);
206 return 1;
209 /* The implementation is based on Section 5.2 of Michael Karr,
210 * "Affine Relationships Among Variables of a Program",
211 * except that the echelon form we use starts from the last column
212 * and that we are dealing with integer coefficients.
214 static struct isl_basic_set *affine_hull(
215 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
217 unsigned total;
218 int col;
219 int row;
221 if (!bset1 || !bset2)
222 goto error;
224 total = 1 + isl_basic_set_n_dim(bset1);
226 row = 0;
227 for (col = total-1; col >= 0; --col) {
228 int is_zero1 = row >= bset1->n_eq ||
229 isl_int_is_zero(bset1->eq[row][col]);
230 int is_zero2 = row >= bset2->n_eq ||
231 isl_int_is_zero(bset2->eq[row][col]);
232 if (!is_zero1 && !is_zero2) {
233 set_common_multiple(bset1, bset2, row, col);
234 ++row;
235 } else if (!is_zero1 && is_zero2) {
236 construct_column(bset1, bset2, row, col);
237 } else if (is_zero1 && !is_zero2) {
238 construct_column(bset2, bset1, row, col);
239 } else {
240 if (transform_column(bset1, bset2, row, col))
241 --row;
244 isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
245 isl_basic_set_free(bset2);
246 bset1 = isl_basic_set_normalize_constraints(bset1);
247 return bset1;
248 error:
249 isl_basic_set_free(bset1);
250 isl_basic_set_free(bset2);
251 return NULL;
254 /* Find an integer point in the set represented by "tab"
255 * that lies outside of the equality "eq" e(x) = 0.
256 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
257 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
258 * The point, if found, is returned.
259 * If no point can be found, a zero-length vector is returned.
261 * Before solving an ILP problem, we first check if simply
262 * adding the normal of the constraint to one of the known
263 * integer points in the basic set represented by "tab"
264 * yields another point inside the basic set.
266 * The caller of this function ensures that the tableau is bounded or
267 * that tab->basis and tab->n_unbounded have been set appropriately.
269 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
271 struct isl_ctx *ctx;
272 struct isl_vec *sample = NULL;
273 struct isl_tab_undo *snap;
274 unsigned dim;
276 if (!tab)
277 return NULL;
278 ctx = tab->mat->ctx;
280 dim = tab->n_var;
281 sample = isl_vec_alloc(ctx, 1 + dim);
282 if (!sample)
283 return NULL;
284 isl_int_set_si(sample->el[0], 1);
285 isl_seq_combine(sample->el + 1,
286 ctx->one, tab->bmap->sample->el + 1,
287 up ? ctx->one : ctx->negone, eq + 1, dim);
288 if (isl_basic_map_contains(tab->bmap, sample))
289 return sample;
290 isl_vec_free(sample);
291 sample = NULL;
293 snap = isl_tab_snap(tab);
295 if (!up)
296 isl_seq_neg(eq, eq, 1 + dim);
297 isl_int_sub_ui(eq[0], eq[0], 1);
299 if (isl_tab_extend_cons(tab, 1) < 0)
300 goto error;
301 if (isl_tab_add_ineq(tab, eq) < 0)
302 goto error;
304 sample = isl_tab_sample(tab);
306 isl_int_add_ui(eq[0], eq[0], 1);
307 if (!up)
308 isl_seq_neg(eq, eq, 1 + dim);
310 if (sample && isl_tab_rollback(tab, snap) < 0)
311 goto error;
313 return sample;
314 error:
315 isl_vec_free(sample);
316 return NULL;
319 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
321 int i;
323 bset = isl_basic_set_cow(bset);
324 if (!bset)
325 return NULL;
326 isl_assert(bset->ctx, bset->n_div == 0, goto error);
328 for (i = 0; i < bset->n_eq; ++i)
329 isl_int_set_si(bset->eq[i][0], 0);
331 for (i = 0; i < bset->n_ineq; ++i)
332 isl_int_set_si(bset->ineq[i][0], 0);
334 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
335 return isl_basic_set_implicit_equalities(bset);
336 error:
337 isl_basic_set_free(bset);
338 return NULL;
341 __isl_give isl_set *isl_set_recession_cone(__isl_take isl_set *set)
343 int i;
345 if (!set)
346 return NULL;
347 if (set->n == 0)
348 return set;
350 set = isl_set_remove_divs(set);
351 set = isl_set_cow(set);
352 if (!set)
353 return NULL;
355 for (i = 0; i < set->n; ++i) {
356 set->p[i] = isl_basic_set_recession_cone(set->p[i]);
357 if (!set->p[i])
358 goto error;
361 return set;
362 error:
363 isl_set_free(set);
364 return NULL;
367 /* Move "sample" to a point that is one up (or down) from the original
368 * point in dimension "pos".
370 static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
372 if (up)
373 isl_int_add_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
374 else
375 isl_int_sub_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
378 /* Check if any points that are adjacent to "sample" also belong to "bset".
379 * If so, add them to "hull" and return the updated hull.
381 * Before checking whether and adjacent point belongs to "bset", we first
382 * check whether it already belongs to "hull" as this test is typically
383 * much cheaper.
385 static __isl_give isl_basic_set *add_adjacent_points(
386 __isl_take isl_basic_set *hull, __isl_take isl_vec *sample,
387 __isl_keep isl_basic_set *bset)
389 int i, up;
390 int dim;
392 if (!sample)
393 goto error;
395 dim = isl_basic_set_dim(hull, isl_dim_set);
397 for (i = 0; i < dim; ++i) {
398 for (up = 0; up <= 1; ++up) {
399 int contains;
400 isl_basic_set *point;
402 adjacent_point(sample, i, up);
403 contains = isl_basic_set_contains(hull, sample);
404 if (contains < 0)
405 goto error;
406 if (contains) {
407 adjacent_point(sample, i, !up);
408 continue;
410 contains = isl_basic_set_contains(bset, sample);
411 if (contains < 0)
412 goto error;
413 if (contains) {
414 point = isl_basic_set_from_vec(
415 isl_vec_copy(sample));
416 hull = affine_hull(hull, point);
418 adjacent_point(sample, i, !up);
419 if (contains)
420 break;
424 isl_vec_free(sample);
426 return hull;
427 error:
428 isl_vec_free(sample);
429 isl_basic_set_free(hull);
430 return NULL;
433 /* Extend an initial (under-)approximation of the affine hull of basic
434 * set represented by the tableau "tab"
435 * by looking for points that do not satisfy one of the equalities
436 * in the current approximation and adding them to that approximation
437 * until no such points can be found any more.
439 * The caller of this function ensures that "tab" is bounded or
440 * that tab->basis and tab->n_unbounded have been set appropriately.
442 * "bset" may be either NULL or the basic set represented by "tab".
443 * If "bset" is not NULL, we check for any point we find if any
444 * of its adjacent points also belong to "bset".
446 static __isl_give isl_basic_set *extend_affine_hull(struct isl_tab *tab,
447 __isl_take isl_basic_set *hull, __isl_keep isl_basic_set *bset)
449 int i, j;
450 unsigned dim;
452 if (!tab || !hull)
453 goto error;
455 dim = tab->n_var;
457 if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
458 goto error;
460 for (i = 0; i < dim; ++i) {
461 struct isl_vec *sample;
462 struct isl_basic_set *point;
463 for (j = 0; j < hull->n_eq; ++j) {
464 sample = outside_point(tab, hull->eq[j], 1);
465 if (!sample)
466 goto error;
467 if (sample->size > 0)
468 break;
469 isl_vec_free(sample);
470 sample = outside_point(tab, hull->eq[j], 0);
471 if (!sample)
472 goto error;
473 if (sample->size > 0)
474 break;
475 isl_vec_free(sample);
477 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
478 goto error;
480 if (j == hull->n_eq)
481 break;
482 if (tab->samples &&
483 isl_tab_add_sample(tab, isl_vec_copy(sample)) < 0)
484 hull = isl_basic_set_free(hull);
485 if (bset)
486 hull = add_adjacent_points(hull, isl_vec_copy(sample),
487 bset);
488 point = isl_basic_set_from_vec(sample);
489 hull = affine_hull(hull, point);
490 if (!hull)
491 return NULL;
494 return hull;
495 error:
496 isl_basic_set_free(hull);
497 return NULL;
500 /* Drop all constraints in bmap that involve any of the dimensions
501 * first to first+n-1.
503 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
504 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
506 int i;
508 if (n == 0)
509 return bmap;
511 bmap = isl_basic_map_cow(bmap);
513 if (!bmap)
514 return NULL;
516 for (i = bmap->n_eq - 1; i >= 0; --i) {
517 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
518 continue;
519 isl_basic_map_drop_equality(bmap, i);
522 for (i = bmap->n_ineq - 1; i >= 0; --i) {
523 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
524 continue;
525 isl_basic_map_drop_inequality(bmap, i);
528 bmap = isl_basic_map_add_known_div_constraints(bmap);
529 return bmap;
532 /* Drop all constraints in bset that involve any of the dimensions
533 * first to first+n-1.
535 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
536 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
538 return isl_basic_map_drop_constraints_involving(bset, first, n);
541 /* Drop all constraints in bmap that do not involve any of the dimensions
542 * first to first + n - 1 of the given type.
544 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
545 __isl_take isl_basic_map *bmap,
546 enum isl_dim_type type, unsigned first, unsigned n)
548 int i;
549 unsigned dim;
551 if (n == 0) {
552 isl_space *space = isl_basic_map_get_space(bmap);
553 isl_basic_map_free(bmap);
554 return isl_basic_map_universe(space);
556 bmap = isl_basic_map_cow(bmap);
557 if (!bmap)
558 return NULL;
560 dim = isl_basic_map_dim(bmap, type);
561 if (first + n > dim || first + n < first)
562 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
563 "index out of bounds", return isl_basic_map_free(bmap));
565 first += isl_basic_map_offset(bmap, type) - 1;
567 for (i = bmap->n_eq - 1; i >= 0; --i) {
568 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
569 continue;
570 isl_basic_map_drop_equality(bmap, i);
573 for (i = bmap->n_ineq - 1; i >= 0; --i) {
574 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
575 continue;
576 isl_basic_map_drop_inequality(bmap, i);
579 bmap = isl_basic_map_add_known_div_constraints(bmap);
580 return bmap;
583 /* Drop all constraints in bset that do not involve any of the dimensions
584 * first to first + n - 1 of the given type.
586 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
587 __isl_take isl_basic_set *bset,
588 enum isl_dim_type type, unsigned first, unsigned n)
590 return isl_basic_map_drop_constraints_not_involving_dims(bset,
591 type, first, n);
594 /* Drop all constraints in bmap that involve any of the dimensions
595 * first to first + n - 1 of the given type.
597 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
598 __isl_take isl_basic_map *bmap,
599 enum isl_dim_type type, unsigned first, unsigned n)
601 unsigned dim;
603 if (!bmap)
604 return NULL;
605 if (n == 0)
606 return bmap;
608 dim = isl_basic_map_dim(bmap, type);
609 if (first + n > dim || first + n < first)
610 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
611 "index out of bounds", return isl_basic_map_free(bmap));
613 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
614 first += isl_basic_map_offset(bmap, type) - 1;
615 return isl_basic_map_drop_constraints_involving(bmap, first, n);
618 /* Drop all constraints in bset that involve any of the dimensions
619 * first to first + n - 1 of the given type.
621 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
622 __isl_take isl_basic_set *bset,
623 enum isl_dim_type type, unsigned first, unsigned n)
625 return isl_basic_map_drop_constraints_involving_dims(bset,
626 type, first, n);
629 /* Drop constraints from "map" by applying "drop" to each basic map.
631 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
632 enum isl_dim_type type, unsigned first, unsigned n,
633 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
634 enum isl_dim_type type, unsigned first, unsigned n))
636 int i;
637 unsigned dim;
639 if (!map)
640 return NULL;
642 dim = isl_map_dim(map, type);
643 if (first + n > dim || first + n < first)
644 isl_die(isl_map_get_ctx(map), isl_error_invalid,
645 "index out of bounds", return isl_map_free(map));
647 map = isl_map_cow(map);
648 if (!map)
649 return NULL;
651 for (i = 0; i < map->n; ++i) {
652 map->p[i] = drop(map->p[i], type, first, n);
653 if (!map->p[i])
654 return isl_map_free(map);
657 if (map->n > 1)
658 ISL_F_CLR(map, ISL_MAP_DISJOINT);
660 return map;
663 /* Drop all constraints in map that involve any of the dimensions
664 * first to first + n - 1 of the given type.
666 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
667 __isl_take isl_map *map,
668 enum isl_dim_type type, unsigned first, unsigned n)
670 if (n == 0)
671 return map;
672 return drop_constraints(map, type, first, n,
673 &isl_basic_map_drop_constraints_involving_dims);
676 /* Drop all constraints in "map" that do not involve any of the dimensions
677 * first to first + n - 1 of the given type.
679 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
680 __isl_take isl_map *map,
681 enum isl_dim_type type, unsigned first, unsigned n)
683 if (n == 0) {
684 isl_space *space = isl_map_get_space(map);
685 isl_map_free(map);
686 return isl_map_universe(space);
688 return drop_constraints(map, type, first, n,
689 &isl_basic_map_drop_constraints_not_involving_dims);
692 /* Drop all constraints in set that involve any of the dimensions
693 * first to first + n - 1 of the given type.
695 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
696 __isl_take isl_set *set,
697 enum isl_dim_type type, unsigned first, unsigned n)
699 return isl_map_drop_constraints_involving_dims(set, type, first, n);
702 /* Drop all constraints in "set" that do not involve any of the dimensions
703 * first to first + n - 1 of the given type.
705 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
706 __isl_take isl_set *set,
707 enum isl_dim_type type, unsigned first, unsigned n)
709 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
712 /* Construct an initial underapproximation of the hull of "bset"
713 * from "sample" and any of its adjacent points that also belong to "bset".
715 static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
716 __isl_take isl_vec *sample)
718 isl_basic_set *hull;
720 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
721 hull = add_adjacent_points(hull, sample, bset);
723 return hull;
726 /* Look for all equalities satisfied by the integer points in bset,
727 * which is assumed to be bounded.
729 * The equalities are obtained by successively looking for
730 * a point that is affinely independent of the points found so far.
731 * In particular, for each equality satisfied by the points so far,
732 * we check if there is any point on a hyperplane parallel to the
733 * corresponding hyperplane shifted by at least one (in either direction).
735 static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
737 struct isl_vec *sample = NULL;
738 struct isl_basic_set *hull;
739 struct isl_tab *tab = NULL;
740 unsigned dim;
742 if (isl_basic_set_plain_is_empty(bset))
743 return bset;
745 dim = isl_basic_set_n_dim(bset);
747 if (bset->sample && bset->sample->size == 1 + dim) {
748 int contains = isl_basic_set_contains(bset, bset->sample);
749 if (contains < 0)
750 goto error;
751 if (contains) {
752 if (dim == 0)
753 return bset;
754 sample = isl_vec_copy(bset->sample);
755 } else {
756 isl_vec_free(bset->sample);
757 bset->sample = NULL;
761 tab = isl_tab_from_basic_set(bset, 1);
762 if (!tab)
763 goto error;
764 if (tab->empty) {
765 isl_tab_free(tab);
766 isl_vec_free(sample);
767 return isl_basic_set_set_to_empty(bset);
770 if (!sample) {
771 struct isl_tab_undo *snap;
772 snap = isl_tab_snap(tab);
773 sample = isl_tab_sample(tab);
774 if (isl_tab_rollback(tab, snap) < 0)
775 goto error;
776 isl_vec_free(tab->bmap->sample);
777 tab->bmap->sample = isl_vec_copy(sample);
780 if (!sample)
781 goto error;
782 if (sample->size == 0) {
783 isl_tab_free(tab);
784 isl_vec_free(sample);
785 return isl_basic_set_set_to_empty(bset);
788 hull = initialize_hull(bset, sample);
790 hull = extend_affine_hull(tab, hull, bset);
791 isl_basic_set_free(bset);
792 isl_tab_free(tab);
794 return hull;
795 error:
796 isl_vec_free(sample);
797 isl_tab_free(tab);
798 isl_basic_set_free(bset);
799 return NULL;
802 /* Given an unbounded tableau and an integer point satisfying the tableau,
803 * construct an initial affine hull containing the recession cone
804 * shifted to the given point.
806 * The unbounded directions are taken from the last rows of the basis,
807 * which is assumed to have been initialized appropriately.
809 static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
810 __isl_take isl_vec *vec)
812 int i;
813 int k;
814 struct isl_basic_set *bset = NULL;
815 struct isl_ctx *ctx;
816 unsigned dim;
818 if (!vec || !tab)
819 return NULL;
820 ctx = vec->ctx;
821 isl_assert(ctx, vec->size != 0, goto error);
823 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
824 if (!bset)
825 goto error;
826 dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
827 for (i = 0; i < dim; ++i) {
828 k = isl_basic_set_alloc_equality(bset);
829 if (k < 0)
830 goto error;
831 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
832 vec->size - 1);
833 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
834 vec->size - 1, &bset->eq[k][0]);
835 isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
837 bset->sample = vec;
838 bset = isl_basic_set_gauss(bset, NULL);
840 return bset;
841 error:
842 isl_basic_set_free(bset);
843 isl_vec_free(vec);
844 return NULL;
847 /* Given a tableau of a set and a tableau of the corresponding
848 * recession cone, detect and add all equalities to the tableau.
849 * If the tableau is bounded, then we can simply keep the
850 * tableau in its state after the return from extend_affine_hull.
851 * However, if the tableau is unbounded, then
852 * isl_tab_set_initial_basis_with_cone will add some additional
853 * constraints to the tableau that have to be removed again.
854 * In this case, we therefore rollback to the state before
855 * any constraints were added and then add the equalities back in.
857 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
858 struct isl_tab *tab_cone)
860 int j;
861 struct isl_vec *sample;
862 struct isl_basic_set *hull = NULL;
863 struct isl_tab_undo *snap;
865 if (!tab || !tab_cone)
866 goto error;
868 snap = isl_tab_snap(tab);
870 isl_mat_free(tab->basis);
871 tab->basis = NULL;
873 isl_assert(tab->mat->ctx, tab->bmap, goto error);
874 isl_assert(tab->mat->ctx, tab->samples, goto error);
875 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
876 isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
878 if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
879 goto error;
881 sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
882 if (!sample)
883 goto error;
885 isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
887 isl_vec_free(tab->bmap->sample);
888 tab->bmap->sample = isl_vec_copy(sample);
890 if (tab->n_unbounded == 0)
891 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
892 else
893 hull = initial_hull(tab, isl_vec_copy(sample));
895 for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
896 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
897 hull = affine_hull(hull,
898 isl_basic_set_from_vec(isl_vec_copy(sample)));
901 isl_vec_free(sample);
903 hull = extend_affine_hull(tab, hull, NULL);
904 if (!hull)
905 goto error;
907 if (tab->n_unbounded == 0) {
908 isl_basic_set_free(hull);
909 return tab;
912 if (isl_tab_rollback(tab, snap) < 0)
913 goto error;
915 if (hull->n_eq > tab->n_zero) {
916 for (j = 0; j < hull->n_eq; ++j) {
917 isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
918 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
919 goto error;
923 isl_basic_set_free(hull);
925 return tab;
926 error:
927 isl_basic_set_free(hull);
928 isl_tab_free(tab);
929 return NULL;
932 /* Compute the affine hull of "bset", where "cone" is the recession cone
933 * of "bset".
935 * We first compute a unimodular transformation that puts the unbounded
936 * directions in the last dimensions. In particular, we take a transformation
937 * that maps all equalities to equalities (in HNF) on the first dimensions.
938 * Let x be the original dimensions and y the transformed, with y_1 bounded
939 * and y_2 unbounded.
941 * [ y_1 ] [ y_1 ] [ Q_1 ]
942 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
944 * Let's call the input basic set S. We compute S' = preimage(S, U)
945 * and drop the final dimensions including any constraints involving them.
946 * This results in set S''.
947 * Then we compute the affine hull A'' of S''.
948 * Let F y_1 >= g be the constraint system of A''. In the transformed
949 * space the y_2 are unbounded, so we can add them back without any constraints,
950 * resulting in
952 * [ y_1 ]
953 * [ F 0 ] [ y_2 ] >= g
954 * or
955 * [ Q_1 ]
956 * [ F 0 ] [ Q_2 ] x >= g
957 * or
958 * F Q_1 x >= g
960 * The affine hull in the original space is then obtained as
961 * A = preimage(A'', Q_1).
963 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
964 struct isl_basic_set *cone)
966 unsigned total;
967 unsigned cone_dim;
968 struct isl_basic_set *hull;
969 struct isl_mat *M, *U, *Q;
971 if (!bset || !cone)
972 goto error;
974 total = isl_basic_set_total_dim(cone);
975 cone_dim = total - cone->n_eq;
977 M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
978 M = isl_mat_left_hermite(M, 0, &U, &Q);
979 if (!M)
980 goto error;
981 isl_mat_free(M);
983 U = isl_mat_lin_to_aff(U);
984 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
986 bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
987 cone_dim);
988 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
990 Q = isl_mat_lin_to_aff(Q);
991 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
993 if (bset && bset->sample && bset->sample->size == 1 + total)
994 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
996 hull = uset_affine_hull_bounded(bset);
998 if (!hull) {
999 isl_mat_free(Q);
1000 isl_mat_free(U);
1001 } else {
1002 struct isl_vec *sample = isl_vec_copy(hull->sample);
1003 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
1004 if (sample && sample->size > 0)
1005 sample = isl_mat_vec_product(U, sample);
1006 else
1007 isl_mat_free(U);
1008 hull = isl_basic_set_preimage(hull, Q);
1009 if (hull) {
1010 isl_vec_free(hull->sample);
1011 hull->sample = sample;
1012 } else
1013 isl_vec_free(sample);
1016 isl_basic_set_free(cone);
1018 return hull;
1019 error:
1020 isl_basic_set_free(bset);
1021 isl_basic_set_free(cone);
1022 return NULL;
1025 /* Look for all equalities satisfied by the integer points in bset,
1026 * which is assumed not to have any explicit equalities.
1028 * The equalities are obtained by successively looking for
1029 * a point that is affinely independent of the points found so far.
1030 * In particular, for each equality satisfied by the points so far,
1031 * we check if there is any point on a hyperplane parallel to the
1032 * corresponding hyperplane shifted by at least one (in either direction).
1034 * Before looking for any outside points, we first compute the recession
1035 * cone. The directions of this recession cone will always be part
1036 * of the affine hull, so there is no need for looking for any points
1037 * in these directions.
1038 * In particular, if the recession cone is full-dimensional, then
1039 * the affine hull is simply the whole universe.
1041 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
1043 struct isl_basic_set *cone;
1045 if (isl_basic_set_plain_is_empty(bset))
1046 return bset;
1048 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
1049 if (!cone)
1050 goto error;
1051 if (cone->n_eq == 0) {
1052 isl_space *space;
1053 space = isl_basic_set_get_space(bset);
1054 isl_basic_set_free(cone);
1055 isl_basic_set_free(bset);
1056 return isl_basic_set_universe(space);
1059 if (cone->n_eq < isl_basic_set_total_dim(cone))
1060 return affine_hull_with_cone(bset, cone);
1062 isl_basic_set_free(cone);
1063 return uset_affine_hull_bounded(bset);
1064 error:
1065 isl_basic_set_free(bset);
1066 return NULL;
1069 /* Look for all equalities satisfied by the integer points in bmap
1070 * that are independent of the equalities already explicitly available
1071 * in bmap.
1073 * We first remove all equalities already explicitly available,
1074 * then look for additional equalities in the reduced space
1075 * and then transform the result to the original space.
1076 * The original equalities are _not_ added to this set. This is
1077 * the responsibility of the calling function.
1078 * The resulting basic set has all meaning about the dimensions removed.
1079 * In particular, dimensions that correspond to existential variables
1080 * in bmap and that are found to be fixed are not removed.
1082 static struct isl_basic_set *equalities_in_underlying_set(
1083 struct isl_basic_map *bmap)
1085 struct isl_mat *T1 = NULL;
1086 struct isl_mat *T2 = NULL;
1087 struct isl_basic_set *bset = NULL;
1088 struct isl_basic_set *hull = NULL;
1090 bset = isl_basic_map_underlying_set(bmap);
1091 if (!bset)
1092 return NULL;
1093 if (bset->n_eq)
1094 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
1095 if (!bset)
1096 goto error;
1098 hull = uset_affine_hull(bset);
1099 if (!T2)
1100 return hull;
1102 if (!hull) {
1103 isl_mat_free(T1);
1104 isl_mat_free(T2);
1105 } else {
1106 struct isl_vec *sample = isl_vec_copy(hull->sample);
1107 if (sample && sample->size > 0)
1108 sample = isl_mat_vec_product(T1, sample);
1109 else
1110 isl_mat_free(T1);
1111 hull = isl_basic_set_preimage(hull, T2);
1112 if (hull) {
1113 isl_vec_free(hull->sample);
1114 hull->sample = sample;
1115 } else
1116 isl_vec_free(sample);
1119 return hull;
1120 error:
1121 isl_mat_free(T1);
1122 isl_mat_free(T2);
1123 isl_basic_set_free(bset);
1124 isl_basic_set_free(hull);
1125 return NULL;
1128 /* Detect and make explicit all equalities satisfied by the (integer)
1129 * points in bmap.
1131 struct isl_basic_map *isl_basic_map_detect_equalities(
1132 struct isl_basic_map *bmap)
1134 int i, j;
1135 struct isl_basic_set *hull = NULL;
1137 if (!bmap)
1138 return NULL;
1139 if (bmap->n_ineq == 0)
1140 return bmap;
1141 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1142 return bmap;
1143 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
1144 return bmap;
1145 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1146 return isl_basic_map_implicit_equalities(bmap);
1148 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
1149 if (!hull)
1150 goto error;
1151 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
1152 isl_basic_set_free(hull);
1153 return isl_basic_map_set_to_empty(bmap);
1155 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
1156 hull->n_eq, 0);
1157 for (i = 0; i < hull->n_eq; ++i) {
1158 j = isl_basic_map_alloc_equality(bmap);
1159 if (j < 0)
1160 goto error;
1161 isl_seq_cpy(bmap->eq[j], hull->eq[i],
1162 1 + isl_basic_set_total_dim(hull));
1164 isl_vec_free(bmap->sample);
1165 bmap->sample = isl_vec_copy(hull->sample);
1166 isl_basic_set_free(hull);
1167 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
1168 bmap = isl_basic_map_simplify(bmap);
1169 return isl_basic_map_finalize(bmap);
1170 error:
1171 isl_basic_set_free(hull);
1172 isl_basic_map_free(bmap);
1173 return NULL;
1176 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
1177 __isl_take isl_basic_set *bset)
1179 return bset_from_bmap(
1180 isl_basic_map_detect_equalities(bset_to_bmap(bset)));
1183 __isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
1185 return isl_map_inline_foreach_basic_map(map,
1186 &isl_basic_map_detect_equalities);
1189 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
1191 return set_from_map(isl_map_detect_equalities(set_to_map(set)));
1194 /* Return the superset of "bmap" described by the equalities
1195 * satisfied by "bmap" that are already known.
1197 __isl_give isl_basic_map *isl_basic_map_plain_affine_hull(
1198 __isl_take isl_basic_map *bmap)
1200 bmap = isl_basic_map_cow(bmap);
1201 if (bmap)
1202 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1203 bmap = isl_basic_map_finalize(bmap);
1204 return bmap;
1207 /* Return the superset of "bset" described by the equalities
1208 * satisfied by "bset" that are already known.
1210 __isl_give isl_basic_set *isl_basic_set_plain_affine_hull(
1211 __isl_take isl_basic_set *bset)
1213 return isl_basic_map_plain_affine_hull(bset);
1216 /* After computing the rational affine hull (by detecting the implicit
1217 * equalities), we compute the additional equalities satisfied by
1218 * the integer points (if any) and add the original equalities back in.
1220 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
1222 bmap = isl_basic_map_detect_equalities(bmap);
1223 bmap = isl_basic_map_plain_affine_hull(bmap);
1224 return bmap;
1227 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
1229 return bset_from_bmap(isl_basic_map_affine_hull(bset_to_bmap(bset)));
1232 /* Given a rational affine matrix "M", add stride constraints to "bmap"
1233 * that ensure that
1235 * M(x)
1237 * is an integer vector. The variables x include all the variables
1238 * of "bmap" except the unknown divs.
1240 * If d is the common denominator of M, then we need to impose that
1242 * d M(x) = 0 mod d
1244 * or
1246 * exists alpha : d M(x) = d alpha
1248 * This function is similar to add_strides in isl_morph.c
1250 static __isl_give isl_basic_map *add_strides(__isl_take isl_basic_map *bmap,
1251 __isl_keep isl_mat *M, int n_known)
1253 int i, div, k;
1254 isl_int gcd;
1256 if (isl_int_is_one(M->row[0][0]))
1257 return bmap;
1259 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1260 M->n_row - 1, M->n_row - 1, 0);
1262 isl_int_init(gcd);
1263 for (i = 1; i < M->n_row; ++i) {
1264 isl_seq_gcd(M->row[i], M->n_col, &gcd);
1265 if (isl_int_is_divisible_by(gcd, M->row[0][0]))
1266 continue;
1267 div = isl_basic_map_alloc_div(bmap);
1268 if (div < 0)
1269 goto error;
1270 isl_int_set_si(bmap->div[div][0], 0);
1271 k = isl_basic_map_alloc_equality(bmap);
1272 if (k < 0)
1273 goto error;
1274 isl_seq_cpy(bmap->eq[k], M->row[i], M->n_col);
1275 isl_seq_clr(bmap->eq[k] + M->n_col, bmap->n_div - n_known);
1276 isl_int_set(bmap->eq[k][M->n_col - n_known + div],
1277 M->row[0][0]);
1279 isl_int_clear(gcd);
1281 return bmap;
1282 error:
1283 isl_int_clear(gcd);
1284 isl_basic_map_free(bmap);
1285 return NULL;
1288 /* If there are any equalities that involve (multiple) unknown divs,
1289 * then extract the stride information encoded by those equalities
1290 * and make it explicitly available in "bmap".
1292 * We first sort the divs so that the unknown divs appear last and
1293 * then we count how many equalities involve these divs.
1295 * Let these equalities be of the form
1297 * A(x) + B y = 0
1299 * where y represents the unknown divs and x the remaining variables.
1300 * Let [H 0] be the Hermite Normal Form of B, i.e.,
1302 * B = [H 0] Q
1304 * Then x is a solution of the equalities iff
1306 * H^-1 A(x) (= - [I 0] Q y)
1308 * is an integer vector. Let d be the common denominator of H^-1.
1309 * We impose
1311 * d H^-1 A(x) = d alpha
1313 * in add_strides, with alpha fresh existentially quantified variables.
1315 static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
1316 __isl_take isl_basic_map *bmap)
1318 int known;
1319 int n_known;
1320 int n, n_col;
1321 int total;
1322 isl_ctx *ctx;
1323 isl_mat *A, *B, *M;
1325 known = isl_basic_map_divs_known(bmap);
1326 if (known < 0)
1327 return isl_basic_map_free(bmap);
1328 if (known)
1329 return bmap;
1330 bmap = isl_basic_map_sort_divs(bmap);
1331 bmap = isl_basic_map_gauss(bmap, NULL);
1332 if (!bmap)
1333 return NULL;
1335 for (n_known = 0; n_known < bmap->n_div; ++n_known)
1336 if (isl_int_is_zero(bmap->div[n_known][0]))
1337 break;
1338 ctx = isl_basic_map_get_ctx(bmap);
1339 total = isl_space_dim(bmap->dim, isl_dim_all);
1340 for (n = 0; n < bmap->n_eq; ++n)
1341 if (isl_seq_first_non_zero(bmap->eq[n] + 1 + total + n_known,
1342 bmap->n_div - n_known) == -1)
1343 break;
1344 if (n == 0)
1345 return bmap;
1346 B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + total + n_known);
1347 n_col = bmap->n_div - n_known;
1348 A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + total + n_known, n_col);
1349 A = isl_mat_left_hermite(A, 0, NULL, NULL);
1350 A = isl_mat_drop_cols(A, n, n_col - n);
1351 A = isl_mat_lin_to_aff(A);
1352 A = isl_mat_right_inverse(A);
1353 B = isl_mat_insert_zero_rows(B, 0, 1);
1354 B = isl_mat_set_element_si(B, 0, 0, 1);
1355 M = isl_mat_product(A, B);
1356 if (!M)
1357 return isl_basic_map_free(bmap);
1358 bmap = add_strides(bmap, M, n_known);
1359 bmap = isl_basic_map_gauss(bmap, NULL);
1360 isl_mat_free(M);
1362 return bmap;
1365 /* Compute the affine hull of each basic map in "map" separately
1366 * and make all stride information explicit so that we can remove
1367 * all unknown divs without losing this information.
1368 * The result is also guaranteed to be gaussed.
1370 * In simple cases where a div is determined by an equality,
1371 * calling isl_basic_map_gauss is enough to make the stride information
1372 * explicit, as it will derive an explicit representation for the div
1373 * from the equality. If, however, the stride information
1374 * is encoded through multiple unknown divs then we need to make
1375 * some extra effort in isl_basic_map_make_strides_explicit.
1377 static __isl_give isl_map *isl_map_local_affine_hull(__isl_take isl_map *map)
1379 int i;
1381 map = isl_map_cow(map);
1382 if (!map)
1383 return NULL;
1385 for (i = 0; i < map->n; ++i) {
1386 map->p[i] = isl_basic_map_affine_hull(map->p[i]);
1387 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1388 map->p[i] = isl_basic_map_make_strides_explicit(map->p[i]);
1389 if (!map->p[i])
1390 return isl_map_free(map);
1393 return map;
1396 static __isl_give isl_set *isl_set_local_affine_hull(__isl_take isl_set *set)
1398 return isl_map_local_affine_hull(set);
1401 /* Return an empty basic map living in the same space as "map".
1403 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1404 __isl_take isl_map *map)
1406 isl_space *space;
1408 space = isl_map_get_space(map);
1409 isl_map_free(map);
1410 return isl_basic_map_empty(space);
1413 /* Compute the affine hull of "map".
1415 * We first compute the affine hull of each basic map separately.
1416 * Then we align the divs and recompute the affine hulls of the basic
1417 * maps since some of them may now have extra divs.
1418 * In order to avoid performing parametric integer programming to
1419 * compute explicit expressions for the divs, possible leading to
1420 * an explosion in the number of basic maps, we first drop all unknown
1421 * divs before aligning the divs. Note that isl_map_local_affine_hull tries
1422 * to make sure that all stride information is explicitly available
1423 * in terms of known divs. This involves calling isl_basic_set_gauss,
1424 * which is also needed because affine_hull assumes its input has been gaussed,
1425 * while isl_map_affine_hull may be called on input that has not been gaussed,
1426 * in particular from initial_facet_constraint.
1427 * Similarly, align_divs may reorder some divs so that we need to
1428 * gauss the result again.
1429 * Finally, we combine the individual affine hulls into a single
1430 * affine hull.
1432 __isl_give isl_basic_map *isl_map_affine_hull(__isl_take isl_map *map)
1434 struct isl_basic_map *model = NULL;
1435 struct isl_basic_map *hull = NULL;
1436 struct isl_set *set;
1437 isl_basic_set *bset;
1439 map = isl_map_detect_equalities(map);
1440 map = isl_map_local_affine_hull(map);
1441 map = isl_map_remove_empty_parts(map);
1442 map = isl_map_remove_unknown_divs(map);
1443 map = isl_map_align_divs(map);
1445 if (!map)
1446 return NULL;
1448 if (map->n == 0)
1449 return replace_map_by_empty_basic_map(map);
1451 model = isl_basic_map_copy(map->p[0]);
1452 set = isl_map_underlying_set(map);
1453 set = isl_set_cow(set);
1454 set = isl_set_local_affine_hull(set);
1455 if (!set)
1456 goto error;
1458 while (set->n > 1)
1459 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
1461 bset = isl_basic_set_copy(set->p[0]);
1462 hull = isl_basic_map_overlying_set(bset, model);
1463 isl_set_free(set);
1464 hull = isl_basic_map_simplify(hull);
1465 return isl_basic_map_finalize(hull);
1466 error:
1467 isl_basic_map_free(model);
1468 isl_set_free(set);
1469 return NULL;
1472 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
1474 return bset_from_bmap(isl_map_affine_hull(set_to_map(set)));