isl_tab_basic_map_partial_lexopt: remove samples that are no longer useful
[isl.git] / isl_affine_hull.c
blobfa987beb2813cad2cd7a4ffec7320543c0d5b315
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_implicit_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(bset1->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 /* Find an integer point in the set represented by "tab"
225 * that lies outside of the equality "eq" e(x) = 0.
226 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
227 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
228 * The point, if found, is returned.
229 * If no point can be found, a zero-length vector is returned.
231 * Before solving an ILP problem, we first check if simply
232 * adding the normal of the constraint to one of the known
233 * integer points in the basic set represented by "tab"
234 * yields another point inside the basic set.
236 * The caller of this function ensures that the tableau is bounded.
238 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
240 struct isl_ctx *ctx;
241 struct isl_vec *sample;
242 struct isl_tab_undo *snap;
243 unsigned dim;
244 int k;
246 if (!tab)
247 return NULL;
248 ctx = tab->mat->ctx;
250 dim = tab->n_var;
251 sample = isl_vec_alloc(ctx, 1 + dim);
252 if (!sample)
253 return NULL;
254 isl_int_set_si(sample->el[0], 1);
255 isl_seq_combine(sample->el + 1,
256 ctx->one, tab->bset->sample->el + 1,
257 up ? ctx->one : ctx->negone, eq + 1, dim);
258 if (isl_basic_set_contains(tab->bset, sample))
259 return sample;
260 isl_vec_free(sample);
261 sample = NULL;
263 snap = isl_tab_snap(tab);
265 if (!up)
266 isl_seq_neg(eq, eq, 1 + dim);
267 isl_int_sub_ui(eq[0], eq[0], 1);
269 if (isl_tab_extend_cons(tab, 1) < 0)
270 goto error;
271 tab = isl_tab_add_ineq(tab, eq);
273 sample = isl_tab_sample(tab);
275 isl_int_add_ui(eq[0], eq[0], 1);
276 if (!up)
277 isl_seq_neg(eq, eq, 1 + dim);
279 if (isl_tab_rollback(tab, snap) < 0)
280 goto error;
282 return sample;
283 error:
284 isl_vec_free(sample);
285 return NULL;
288 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
290 int i;
292 bset = isl_basic_set_cow(bset);
293 if (!bset)
294 return NULL;
295 isl_assert(bset->ctx, bset->n_div == 0, goto error);
297 for (i = 0; i < bset->n_eq; ++i)
298 isl_int_set_si(bset->eq[i][0], 0);
300 for (i = 0; i < bset->n_ineq; ++i)
301 isl_int_set_si(bset->ineq[i][0], 0);
303 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
304 return isl_basic_set_implicit_equalities(bset);
305 error:
306 isl_basic_set_free(bset);
307 return NULL;
310 /* Extend an initial (under-)approximation of the affine hull of basic
311 * set represented by the tableau "tab"
312 * by looking for points that do not satisfy one of the equalities
313 * in the current approximation and adding them to that approximation
314 * until no such points can be found any more.
316 * The caller of this function ensures that "tab" is bounded.
318 static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
319 struct isl_basic_set *hull)
321 int i, j, k;
322 unsigned dim;
324 if (!tab || !hull)
325 goto error;
327 dim = tab->n_var;
329 if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
330 goto error;
332 for (i = 0; i < dim; ++i) {
333 struct isl_vec *sample;
334 struct isl_basic_set *point;
335 for (j = 0; j < hull->n_eq; ++j) {
336 sample = outside_point(tab, hull->eq[j], 1);
337 if (!sample)
338 goto error;
339 if (sample->size > 0)
340 break;
341 isl_vec_free(sample);
342 sample = outside_point(tab, hull->eq[j], 0);
343 if (!sample)
344 goto error;
345 if (sample->size > 0)
346 break;
347 isl_vec_free(sample);
349 tab = isl_tab_add_eq(tab, hull->eq[j]);
350 if (!tab)
351 goto error;
353 if (j == hull->n_eq)
354 break;
355 point = isl_basic_set_from_vec(sample);
356 hull = affine_hull(hull, point);
359 return hull;
360 error:
361 isl_basic_set_free(hull);
362 return NULL;
365 /* Drop all constraints in bset that involve any of the dimensions
366 * first to first+n-1.
368 static struct isl_basic_set *drop_constraints_involving
369 (struct isl_basic_set *bset, unsigned first, unsigned n)
371 int i;
373 if (!bset)
374 return NULL;
376 bset = isl_basic_set_cow(bset);
378 for (i = bset->n_eq - 1; i >= 0; --i) {
379 if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
380 continue;
381 isl_basic_set_drop_equality(bset, i);
384 for (i = bset->n_ineq - 1; i >= 0; --i) {
385 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
386 continue;
387 isl_basic_set_drop_inequality(bset, i);
390 return bset;
393 /* Look for all equalities satisfied by the integer points in bset,
394 * which is assumed to be bounded.
396 * The equalities are obtained by successively looking for
397 * a point that is affinely independent of the points found so far.
398 * In particular, for each equality satisfied by the points so far,
399 * we check if there is any point on a hyperplane parallel to the
400 * corresponding hyperplane shifted by at least one (in either direction).
402 static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
404 struct isl_vec *sample = NULL;
405 struct isl_basic_set *hull;
406 struct isl_tab *tab = NULL;
407 unsigned dim;
409 if (isl_basic_set_fast_is_empty(bset))
410 return bset;
412 dim = isl_basic_set_n_dim(bset);
414 if (bset->sample && bset->sample->size == 1 + dim) {
415 int contains = isl_basic_set_contains(bset, bset->sample);
416 if (contains < 0)
417 goto error;
418 if (contains) {
419 if (dim == 0)
420 return bset;
421 sample = isl_vec_copy(bset->sample);
422 } else {
423 isl_vec_free(bset->sample);
424 bset->sample = NULL;
428 tab = isl_tab_from_basic_set(bset);
429 if (!tab)
430 goto error;
431 tab->bset = isl_basic_set_copy(bset);
433 if (!sample) {
434 struct isl_tab_undo *snap;
435 snap = isl_tab_snap(tab);
436 sample = isl_tab_sample(tab);
437 if (isl_tab_rollback(tab, snap) < 0)
438 goto error;
439 isl_vec_free(tab->bset->sample);
440 tab->bset->sample = isl_vec_copy(sample);
443 if (!sample)
444 goto error;
445 if (sample->size == 0) {
446 isl_tab_free(tab);
447 isl_vec_free(sample);
448 return isl_basic_set_set_to_empty(bset);
451 hull = isl_basic_set_from_vec(sample);
453 isl_basic_set_free(bset);
454 hull = extend_affine_hull(tab, hull);
455 isl_tab_free(tab);
457 return hull;
458 error:
459 isl_vec_free(sample);
460 isl_tab_free(tab);
461 isl_basic_set_free(bset);
462 return NULL;
465 /* Compute the affine hull of "bset", where "cone" is the recession cone
466 * of "bset".
468 * We first compute a unimodular transformation that puts the unbounded
469 * directions in the last dimensions. In particular, we take a transformation
470 * that maps all equalities to equalities (in HNF) on the first dimensions.
471 * Let x be the original dimensions and y the transformed, with y_1 bounded
472 * and y_2 unbounded.
474 * [ y_1 ] [ y_1 ] [ Q_1 ]
475 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
477 * Let's call the input basic set S. We compute S' = preimage(S, U)
478 * and drop the final dimensions including any constraints involving them.
479 * This results in set S''.
480 * Then we compute the affine hull A'' of S''.
481 * Let F y_1 >= g be the constraint system of A''. In the transformed
482 * space the y_2 are unbounded, so we can add them back without any constraints,
483 * resulting in
485 * [ y_1 ]
486 * [ F 0 ] [ y_2 ] >= g
487 * or
488 * [ Q_1 ]
489 * [ F 0 ] [ Q_2 ] x >= g
490 * or
491 * F Q_1 x >= g
493 * The affine hull in the original space is then obtained as
494 * A = preimage(A'', Q_1).
496 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
497 struct isl_basic_set *cone)
499 unsigned total;
500 unsigned cone_dim;
501 struct isl_basic_set *hull;
502 struct isl_mat *M, *U, *Q;
504 if (!bset || !cone)
505 goto error;
507 total = isl_basic_set_total_dim(cone);
508 cone_dim = total - cone->n_eq;
510 M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
511 M = isl_mat_left_hermite(M, 0, &U, &Q);
512 if (!M)
513 goto error;
514 isl_mat_free(M);
516 U = isl_mat_lin_to_aff(U);
517 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
519 bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
520 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
522 Q = isl_mat_lin_to_aff(Q);
523 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
525 if (bset && bset->sample && bset->sample->size == 1 + total)
526 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
528 hull = uset_affine_hull_bounded(bset);
530 if (!hull)
531 isl_mat_free(U);
532 else {
533 struct isl_vec *sample = isl_vec_copy(hull->sample);
534 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
535 if (sample && sample->size > 0)
536 sample = isl_mat_vec_product(U, sample);
537 else
538 isl_mat_free(U);
539 hull = isl_basic_set_preimage(hull, Q);
540 isl_vec_free(hull->sample);
541 hull->sample = sample;
544 isl_basic_set_free(cone);
546 return hull;
547 error:
548 isl_basic_set_free(bset);
549 isl_basic_set_free(cone);
550 return NULL;
553 /* Look for all equalities satisfied by the integer points in bset,
554 * which is assumed not to have any explicit equalities.
556 * The equalities are obtained by successively looking for
557 * a point that is affinely independent of the points found so far.
558 * In particular, for each equality satisfied by the points so far,
559 * we check if there is any point on a hyperplane parallel to the
560 * corresponding hyperplane shifted by at least one (in either direction).
562 * Before looking for any outside points, we first compute the recession
563 * cone. The directions of this recession cone will always be part
564 * of the affine hull, so there is no need for looking for any points
565 * in these directions.
566 * In particular, if the recession cone is full-dimensional, then
567 * the affine hull is simply the whole universe.
569 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
571 struct isl_basic_set *cone;
573 if (isl_basic_set_fast_is_empty(bset))
574 return bset;
576 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
577 if (!cone)
578 goto error;
579 if (cone->n_eq == 0) {
580 struct isl_basic_set *hull;
581 isl_basic_set_free(cone);
582 hull = isl_basic_set_universe_like(bset);
583 isl_basic_set_free(bset);
584 return hull;
587 if (cone->n_eq < isl_basic_set_total_dim(cone))
588 return affine_hull_with_cone(bset, cone);
590 isl_basic_set_free(cone);
591 return uset_affine_hull_bounded(bset);
592 error:
593 isl_basic_set_free(bset);
594 return NULL;
597 /* Look for all equalities satisfied by the integer points in bmap
598 * that are independent of the equalities already explicitly available
599 * in bmap.
601 * We first remove all equalities already explicitly available,
602 * then look for additional equalities in the reduced space
603 * and then transform the result to the original space.
604 * The original equalities are _not_ added to this set. This is
605 * the responsibility of the calling function.
606 * The resulting basic set has all meaning about the dimensions removed.
607 * In particular, dimensions that correspond to existential variables
608 * in bmap and that are found to be fixed are not removed.
610 static struct isl_basic_set *equalities_in_underlying_set(
611 struct isl_basic_map *bmap)
613 struct isl_mat *T1 = NULL;
614 struct isl_mat *T2 = NULL;
615 struct isl_basic_set *bset = NULL;
616 struct isl_basic_set *hull = NULL;
618 bset = isl_basic_map_underlying_set(bmap);
619 if (!bset)
620 return NULL;
621 if (bset->n_eq)
622 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
623 if (!bset)
624 goto error;
626 hull = uset_affine_hull(bset);
627 if (!T2)
628 return hull;
630 if (!hull)
631 isl_mat_free(T1);
632 else {
633 struct isl_vec *sample = isl_vec_copy(hull->sample);
634 if (sample && sample->size > 0)
635 sample = isl_mat_vec_product(T1, sample);
636 else
637 isl_mat_free(T1);
638 hull = isl_basic_set_preimage(hull, T2);
639 isl_vec_free(hull->sample);
640 hull->sample = sample;
643 return hull;
644 error:
645 isl_mat_free(T2);
646 isl_basic_set_free(bset);
647 isl_basic_set_free(hull);
648 return NULL;
651 /* Detect and make explicit all equalities satisfied by the (integer)
652 * points in bmap.
654 struct isl_basic_map *isl_basic_map_detect_equalities(
655 struct isl_basic_map *bmap)
657 int i, j;
658 struct isl_basic_set *hull = NULL;
660 if (!bmap)
661 return NULL;
662 if (bmap->n_ineq == 0)
663 return bmap;
664 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
665 return bmap;
666 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
667 return bmap;
668 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
669 return isl_basic_map_implicit_equalities(bmap);
671 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
672 if (!hull)
673 goto error;
674 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
675 isl_basic_set_free(hull);
676 return isl_basic_map_set_to_empty(bmap);
678 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
679 hull->n_eq, 0);
680 for (i = 0; i < hull->n_eq; ++i) {
681 j = isl_basic_map_alloc_equality(bmap);
682 if (j < 0)
683 goto error;
684 isl_seq_cpy(bmap->eq[j], hull->eq[i],
685 1 + isl_basic_set_total_dim(hull));
687 isl_vec_free(bmap->sample);
688 bmap->sample = isl_vec_copy(hull->sample);
689 isl_basic_set_free(hull);
690 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
691 bmap = isl_basic_map_simplify(bmap);
692 return isl_basic_map_finalize(bmap);
693 error:
694 isl_basic_set_free(hull);
695 isl_basic_map_free(bmap);
696 return NULL;
699 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
700 __isl_take isl_basic_set *bset)
702 return (isl_basic_set *)
703 isl_basic_map_detect_equalities((isl_basic_map *)bset);
706 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
708 struct isl_basic_map *bmap;
709 int i;
711 if (!map)
712 return NULL;
714 for (i = 0; i < map->n; ++i) {
715 bmap = isl_basic_map_copy(map->p[i]);
716 bmap = isl_basic_map_detect_equalities(bmap);
717 if (!bmap)
718 goto error;
719 isl_basic_map_free(map->p[i]);
720 map->p[i] = bmap;
723 return map;
724 error:
725 isl_map_free(map);
726 return NULL;
729 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
731 return (isl_set *)isl_map_detect_equalities((isl_map *)set);
734 /* After computing the rational affine hull (by detecting the implicit
735 * equalities), we compute the additional equalities satisfied by
736 * the integer points (if any) and add the original equalities back in.
738 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
740 bmap = isl_basic_map_detect_equalities(bmap);
741 bmap = isl_basic_map_cow(bmap);
742 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
743 return bmap;
746 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
748 return (struct isl_basic_set *)
749 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
752 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
754 int i;
755 struct isl_basic_map *model = NULL;
756 struct isl_basic_map *hull = NULL;
757 struct isl_set *set;
759 if (!map)
760 return NULL;
762 if (map->n == 0) {
763 hull = isl_basic_map_empty_like_map(map);
764 isl_map_free(map);
765 return hull;
768 map = isl_map_detect_equalities(map);
769 map = isl_map_align_divs(map);
770 if (!map)
771 return NULL;
772 model = isl_basic_map_copy(map->p[0]);
773 set = isl_map_underlying_set(map);
774 set = isl_set_cow(set);
775 if (!set)
776 goto error;
778 for (i = 0; i < set->n; ++i) {
779 set->p[i] = isl_basic_set_cow(set->p[i]);
780 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
781 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
782 if (!set->p[i])
783 goto error;
785 set = isl_set_remove_empty_parts(set);
786 if (set->n == 0) {
787 hull = isl_basic_map_empty_like(model);
788 isl_basic_map_free(model);
789 } else {
790 struct isl_basic_set *bset;
791 while (set->n > 1) {
792 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
793 if (!set->p[0])
794 goto error;
796 bset = isl_basic_set_copy(set->p[0]);
797 hull = isl_basic_map_overlying_set(bset, model);
799 isl_set_free(set);
800 hull = isl_basic_map_simplify(hull);
801 return isl_basic_map_finalize(hull);
802 error:
803 isl_basic_map_free(model);
804 isl_set_free(set);
805 return NULL;
808 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
810 return (struct isl_basic_set *)
811 isl_map_affine_hull((struct isl_map *)set);