isl_map_convex_hull: remove lineality space if any before computing convex hull
[isl.git] / isl_convex_hull.c
blob20a601cf09b71e9020e7eb1f29941c286a07131f
1 #include "isl_lp.h"
2 #include "isl_map.h"
3 #include "isl_map_private.h"
4 #include "isl_mat.h"
5 #include "isl_set.h"
6 #include "isl_seq.h"
7 #include "isl_equalities.h"
8 #include "isl_tab.h"
10 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
12 static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
14 isl_int *t;
16 if (i != j) {
17 t = bmap->ineq[i];
18 bmap->ineq[i] = bmap->ineq[j];
19 bmap->ineq[j] = t;
23 /* Return 1 if constraint c is redundant with respect to the constraints
24 * in bmap. If c is a lower [upper] bound in some variable and bmap
25 * does not have a lower [upper] bound in that variable, then c cannot
26 * be redundant and we do not need solve any lp.
28 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
29 isl_int *c, isl_int *opt_n, isl_int *opt_d)
31 enum isl_lp_result res;
32 unsigned total;
33 int i, j;
35 if (!bmap)
36 return -1;
38 total = isl_basic_map_total_dim(*bmap);
39 for (i = 0; i < total; ++i) {
40 int sign;
41 if (isl_int_is_zero(c[1+i]))
42 continue;
43 sign = isl_int_sgn(c[1+i]);
44 for (j = 0; j < (*bmap)->n_ineq; ++j)
45 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
46 break;
47 if (j == (*bmap)->n_ineq)
48 break;
50 if (i < total)
51 return 0;
53 res = isl_solve_lp(*bmap, 0, c, (*bmap)->ctx->one, opt_n, opt_d);
54 if (res == isl_lp_unbounded)
55 return 0;
56 if (res == isl_lp_error)
57 return -1;
58 if (res == isl_lp_empty) {
59 *bmap = isl_basic_map_set_to_empty(*bmap);
60 return 0;
62 return !isl_int_is_neg(*opt_n);
65 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
66 isl_int *c, isl_int *opt_n, isl_int *opt_d)
68 return isl_basic_map_constraint_is_redundant(
69 (struct isl_basic_map **)bset, c, opt_n, opt_d);
72 /* Compute the convex hull of a basic map, by removing the redundant
73 * constraints. If the minimal value along the normal of a constraint
74 * is the same if the constraint is removed, then the constraint is redundant.
76 * Alternatively, we could have intersected the basic map with the
77 * corresponding equality and the checked if the dimension was that
78 * of a facet.
80 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
82 struct isl_tab *tab;
84 if (!bmap)
85 return NULL;
87 bmap = isl_basic_map_gauss(bmap, NULL);
88 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
89 return bmap;
90 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
91 return bmap;
92 if (bmap->n_ineq <= 1)
93 return bmap;
95 tab = isl_tab_from_basic_map(bmap);
96 tab = isl_tab_detect_equalities(bmap->ctx, tab);
97 tab = isl_tab_detect_redundant(bmap->ctx, tab);
98 bmap = isl_basic_map_update_from_tab(bmap, tab);
99 isl_tab_free(bmap->ctx, tab);
100 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
101 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
102 return bmap;
105 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
107 return (struct isl_basic_set *)
108 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
111 /* Check if the set set is bound in the direction of the affine
112 * constraint c and if so, set the constant term such that the
113 * resulting constraint is a bounding constraint for the set.
115 static int uset_is_bound(struct isl_ctx *ctx, struct isl_set *set,
116 isl_int *c, unsigned len)
118 int first;
119 int j;
120 isl_int opt;
121 isl_int opt_denom;
123 isl_int_init(opt);
124 isl_int_init(opt_denom);
125 first = 1;
126 for (j = 0; j < set->n; ++j) {
127 enum isl_lp_result res;
129 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
130 continue;
132 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
133 0, c, ctx->one, &opt, &opt_denom);
134 if (res == isl_lp_unbounded)
135 break;
136 if (res == isl_lp_error)
137 goto error;
138 if (res == isl_lp_empty) {
139 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
140 if (!set->p[j])
141 goto error;
142 continue;
144 if (!isl_int_is_one(opt_denom))
145 isl_seq_scale(c, c, opt_denom, len);
146 if (first || isl_int_is_neg(opt))
147 isl_int_sub(c[0], c[0], opt);
148 first = 0;
150 isl_int_clear(opt);
151 isl_int_clear(opt_denom);
152 return j >= set->n;
153 error:
154 isl_int_clear(opt);
155 isl_int_clear(opt_denom);
156 return -1;
159 /* Check if "c" is a direction that is independent of the previously found "n"
160 * bounds in "dirs".
161 * If so, add it to the list, with the negative of the lower bound
162 * in the constant position, i.e., such that c corresponds to a bounding
163 * hyperplane (but not necessarily a facet).
164 * Assumes set "set" is bounded.
166 static int is_independent_bound(struct isl_ctx *ctx,
167 struct isl_set *set, isl_int *c,
168 struct isl_mat *dirs, int n)
170 int is_bound;
171 int i = 0;
173 isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
174 if (n != 0) {
175 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
176 if (pos < 0)
177 return 0;
178 for (i = 0; i < n; ++i) {
179 int pos_i;
180 pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
181 if (pos_i < pos)
182 continue;
183 if (pos_i > pos)
184 break;
185 isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
186 dirs->n_col-1, NULL);
187 pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
188 if (pos < 0)
189 return 0;
193 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
194 if (is_bound != 1)
195 return is_bound;
196 if (i < n) {
197 int k;
198 isl_int *t = dirs->row[n];
199 for (k = n; k > i; --k)
200 dirs->row[k] = dirs->row[k-1];
201 dirs->row[i] = t;
203 return 1;
206 /* Compute and return a maximal set of linearly independent bounds
207 * on the set "set", based on the constraints of the basic sets
208 * in "set".
210 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
211 struct isl_set *set)
213 int i, j, n;
214 struct isl_mat *dirs = NULL;
215 unsigned dim = isl_set_n_dim(set);
217 dirs = isl_mat_alloc(ctx, dim, 1+dim);
218 if (!dirs)
219 goto error;
221 n = 0;
222 for (i = 0; n < dim && i < set->n; ++i) {
223 int f;
224 struct isl_basic_set *bset = set->p[i];
226 for (j = 0; n < dim && j < bset->n_eq; ++j) {
227 f = is_independent_bound(ctx, set, bset->eq[j],
228 dirs, n);
229 if (f < 0)
230 goto error;
231 if (f)
232 ++n;
234 for (j = 0; n < dim && j < bset->n_ineq; ++j) {
235 f = is_independent_bound(ctx, set, bset->ineq[j],
236 dirs, n);
237 if (f < 0)
238 goto error;
239 if (f)
240 ++n;
243 dirs->n_row = n;
244 return dirs;
245 error:
246 isl_mat_free(ctx, dirs);
247 return NULL;
250 static struct isl_basic_set *isl_basic_set_set_rational(
251 struct isl_basic_set *bset)
253 if (!bset)
254 return NULL;
256 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
257 return bset;
259 bset = isl_basic_set_cow(bset);
260 if (!bset)
261 return NULL;
263 ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
265 return isl_basic_set_finalize(bset);
268 static struct isl_set *isl_set_set_rational(struct isl_set *set)
270 int i;
272 set = isl_set_cow(set);
273 if (!set)
274 return NULL;
275 for (i = 0; i < set->n; ++i) {
276 set->p[i] = isl_basic_set_set_rational(set->p[i]);
277 if (!set->p[i])
278 goto error;
280 return set;
281 error:
282 isl_set_free(set);
283 return NULL;
286 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
287 struct isl_basic_set *bset, isl_int *c)
289 int i;
290 unsigned total;
291 unsigned dim;
293 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
294 return bset;
296 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
297 isl_assert(ctx, bset->n_div == 0, goto error);
298 dim = isl_basic_set_n_dim(bset);
299 bset = isl_basic_set_cow(bset);
300 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
301 i = isl_basic_set_alloc_equality(bset);
302 if (i < 0)
303 goto error;
304 isl_seq_cpy(bset->eq[i], c, 1 + dim);
305 return bset;
306 error:
307 isl_basic_set_free(bset);
308 return NULL;
311 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
312 struct isl_set *set, isl_int *c)
314 int i;
316 set = isl_set_cow(set);
317 if (!set)
318 return NULL;
319 for (i = 0; i < set->n; ++i) {
320 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
321 if (!set->p[i])
322 goto error;
324 return set;
325 error:
326 isl_set_free(set);
327 return NULL;
330 /* Given a union of basic sets, construct the constraints for wrapping
331 * a facet around one of its ridges.
332 * In particular, if each of n the d-dimensional basic sets i in "set"
333 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
334 * and is defined by the constraints
335 * [ 1 ]
336 * A_i [ x ] >= 0
338 * then the resulting set is of dimension n*(1+d) and has as constraints
340 * [ a_i ]
341 * A_i [ x_i ] >= 0
343 * a_i >= 0
345 * \sum_i x_{i,1} = 1
347 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
349 struct isl_basic_set *lp;
350 unsigned n_eq;
351 unsigned n_ineq;
352 int i, j, k;
353 unsigned dim, lp_dim;
355 if (!set)
356 return NULL;
358 dim = 1 + isl_set_n_dim(set);
359 n_eq = 1;
360 n_ineq = set->n;
361 for (i = 0; i < set->n; ++i) {
362 n_eq += set->p[i]->n_eq;
363 n_ineq += set->p[i]->n_ineq;
365 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
366 if (!lp)
367 return NULL;
368 lp_dim = isl_basic_set_n_dim(lp);
369 k = isl_basic_set_alloc_equality(lp);
370 isl_int_set_si(lp->eq[k][0], -1);
371 for (i = 0; i < set->n; ++i) {
372 isl_int_set_si(lp->eq[k][1+dim*i], 0);
373 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
374 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
376 for (i = 0; i < set->n; ++i) {
377 k = isl_basic_set_alloc_inequality(lp);
378 isl_seq_clr(lp->ineq[k], 1+lp_dim);
379 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
381 for (j = 0; j < set->p[i]->n_eq; ++j) {
382 k = isl_basic_set_alloc_equality(lp);
383 isl_seq_clr(lp->eq[k], 1+dim*i);
384 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
385 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
388 for (j = 0; j < set->p[i]->n_ineq; ++j) {
389 k = isl_basic_set_alloc_inequality(lp);
390 isl_seq_clr(lp->ineq[k], 1+dim*i);
391 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
392 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
395 return lp;
398 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
399 * of that facet, compute the other facet of the convex hull that contains
400 * the ridge.
402 * We first transform the set such that the facet constraint becomes
404 * x_1 >= 0
406 * I.e., the facet lies in
408 * x_1 = 0
410 * and on that facet, the constraint that defines the ridge is
412 * x_2 >= 0
414 * (This transformation is not strictly needed, all that is needed is
415 * that the ridge contains the origin.)
417 * Since the ridge contains the origin, the cone of the convex hull
418 * will be of the form
420 * x_1 >= 0
421 * x_2 >= a x_1
423 * with this second constraint defining the new facet.
424 * The constant a is obtained by settting x_1 in the cone of the
425 * convex hull to 1 and minimizing x_2.
426 * Now, each element in the cone of the convex hull is the sum
427 * of elements in the cones of the basic sets.
428 * If a_i is the dilation factor of basic set i, then the problem
429 * we need to solve is
431 * min \sum_i x_{i,2}
432 * st
433 * \sum_i x_{i,1} = 1
434 * a_i >= 0
435 * [ a_i ]
436 * A [ x_i ] >= 0
438 * with
439 * [ 1 ]
440 * A_i [ x_i ] >= 0
442 * the constraints of each (transformed) basic set.
443 * If a = n/d, then the constraint defining the new facet (in the transformed
444 * space) is
446 * -n x_1 + d x_2 >= 0
448 * In the original space, we need to take the same combination of the
449 * corresponding constraints "facet" and "ridge".
451 * Note that a is always finite, since we only apply the wrapping
452 * technique to a union of polytopes.
454 static isl_int *wrap_facet(struct isl_set *set, isl_int *facet, isl_int *ridge)
456 int i;
457 struct isl_mat *T = NULL;
458 struct isl_basic_set *lp = NULL;
459 struct isl_vec *obj;
460 enum isl_lp_result res;
461 isl_int num, den;
462 unsigned dim;
464 set = isl_set_copy(set);
466 dim = 1 + isl_set_n_dim(set);
467 T = isl_mat_alloc(set->ctx, 3, dim);
468 if (!T)
469 goto error;
470 isl_int_set_si(T->row[0][0], 1);
471 isl_seq_clr(T->row[0]+1, dim - 1);
472 isl_seq_cpy(T->row[1], facet, dim);
473 isl_seq_cpy(T->row[2], ridge, dim);
474 T = isl_mat_right_inverse(set->ctx, T);
475 set = isl_set_preimage(set, T);
476 T = NULL;
477 if (!set)
478 goto error;
479 lp = wrap_constraints(set);
480 obj = isl_vec_alloc(set->ctx, 1 + dim*set->n);
481 if (!obj)
482 goto error;
483 isl_int_set_si(obj->block.data[0], 0);
484 for (i = 0; i < set->n; ++i) {
485 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
486 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
487 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
489 isl_int_init(num);
490 isl_int_init(den);
491 res = isl_solve_lp((struct isl_basic_map *)lp, 0,
492 obj->block.data, set->ctx->one, &num, &den);
493 if (res == isl_lp_ok) {
494 isl_int_neg(num, num);
495 isl_seq_combine(facet, num, facet, den, ridge, dim);
497 isl_int_clear(num);
498 isl_int_clear(den);
499 isl_vec_free(set->ctx, obj);
500 isl_basic_set_free(lp);
501 isl_set_free(set);
502 isl_assert(set->ctx, res == isl_lp_ok, return NULL);
503 return facet;
504 error:
505 isl_basic_set_free(lp);
506 isl_mat_free(set->ctx, T);
507 isl_set_free(set);
508 return NULL;
511 /* Given a set of d linearly independent bounding constraints of the
512 * convex hull of "set", compute the constraint of a facet of "set".
514 * We first compute the intersection with the first bounding hyperplane
515 * and remove the component corresponding to this hyperplane from
516 * other bounds (in homogeneous space).
517 * We then wrap around one of the remaining bounding constraints
518 * and continue the process until all bounding constraints have been
519 * taken into account.
520 * The resulting linear combination of the bounding constraints will
521 * correspond to a facet of the convex hull.
523 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
524 struct isl_set *set, struct isl_mat *bounds)
526 struct isl_set *slice = NULL;
527 struct isl_basic_set *face = NULL;
528 struct isl_mat *m, *U, *Q;
529 int i;
530 unsigned dim = isl_set_n_dim(set);
532 isl_assert(ctx, set->n > 0, goto error);
533 isl_assert(ctx, bounds->n_row == dim, goto error);
535 while (bounds->n_row > 1) {
536 slice = isl_set_copy(set);
537 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
538 face = isl_set_affine_hull(slice);
539 if (!face)
540 goto error;
541 if (face->n_eq == 1) {
542 isl_basic_set_free(face);
543 break;
545 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + dim);
546 if (!m)
547 goto error;
548 isl_int_set_si(m->row[0][0], 1);
549 isl_seq_clr(m->row[0]+1, dim);
550 for (i = 0; i < face->n_eq; ++i)
551 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + dim);
552 U = isl_mat_right_inverse(ctx, m);
553 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
554 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
555 dim - face->n_eq);
556 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
557 dim - face->n_eq);
558 U = isl_mat_drop_cols(ctx, U, 0, 1);
559 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
560 bounds = isl_mat_product(ctx, bounds, U);
561 bounds = isl_mat_product(ctx, bounds, Q);
562 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
563 bounds->n_col) == -1) {
564 bounds->n_row--;
565 isl_assert(ctx, bounds->n_row > 1, goto error);
567 if (!wrap_facet(set, bounds->row[0],
568 bounds->row[bounds->n_row-1]))
569 goto error;
570 isl_basic_set_free(face);
571 bounds->n_row--;
573 return bounds;
574 error:
575 isl_basic_set_free(face);
576 isl_mat_free(ctx, bounds);
577 return NULL;
580 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
581 * compute a hyperplane description of the facet, i.e., compute the facets
582 * of the facet.
584 * We compute an affine transformation that transforms the constraint
586 * [ 1 ]
587 * c [ x ] = 0
589 * to the constraint
591 * z_1 = 0
593 * by computing the right inverse U of a matrix that starts with the rows
595 * [ 1 0 ]
596 * [ c ]
598 * Then
599 * [ 1 ] [ 1 ]
600 * [ x ] = U [ z ]
601 * and
602 * [ 1 ] [ 1 ]
603 * [ z ] = Q [ x ]
605 * with Q = U^{-1}
606 * Since z_1 is zero, we can drop this variable as well as the corresponding
607 * column of U to obtain
609 * [ 1 ] [ 1 ]
610 * [ x ] = U' [ z' ]
611 * and
612 * [ 1 ] [ 1 ]
613 * [ z' ] = Q' [ x ]
615 * with Q' equal to Q, but without the corresponding row.
616 * After computing the facets of the facet in the z' space,
617 * we convert them back to the x space through Q.
619 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
621 struct isl_mat *m, *U, *Q;
622 struct isl_basic_set *facet = NULL;
623 struct isl_ctx *ctx;
624 unsigned dim;
626 ctx = set->ctx;
627 set = isl_set_copy(set);
628 dim = isl_set_n_dim(set);
629 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
630 if (!m)
631 goto error;
632 isl_int_set_si(m->row[0][0], 1);
633 isl_seq_clr(m->row[0]+1, dim);
634 isl_seq_cpy(m->row[1], c, 1+dim);
635 U = isl_mat_right_inverse(set->ctx, m);
636 Q = isl_mat_right_inverse(set->ctx, isl_mat_copy(set->ctx, U));
637 U = isl_mat_drop_cols(set->ctx, U, 1, 1);
638 Q = isl_mat_drop_rows(set->ctx, Q, 1, 1);
639 set = isl_set_preimage(set, U);
640 facet = uset_convex_hull_wrap_bounded(set);
641 facet = isl_basic_set_preimage(facet, Q);
642 isl_assert(ctx, facet->n_eq == 0, goto error);
643 return facet;
644 error:
645 isl_basic_set_free(facet);
646 isl_set_free(set);
647 return NULL;
650 /* Given an initial facet constraint, compute the remaining facets.
651 * We do this by running through all facets found so far and computing
652 * the adjacent facets through wrapping, adding those facets that we
653 * hadn't already found before.
655 * For each facet we have found so far, we first compute its facets
656 * in the resulting convex hull. That is, we compute the ridges
657 * of the resulting convex hull contained in the facet.
658 * We also compute the corresponding facet in the current approximation
659 * of the convex hull. There is no need to wrap around the ridges
660 * in this facet since that would result in a facet that is already
661 * present in the current approximation.
663 * This function can still be significantly optimized by checking which of
664 * the facets of the basic sets are also facets of the convex hull and
665 * using all the facets so far to help in constructing the facets of the
666 * facets
667 * and/or
668 * using the technique in section "3.1 Ridge Generation" of
669 * "Extended Convex Hull" by Fukuda et al.
671 static struct isl_basic_set *extend(struct isl_basic_set *hull,
672 struct isl_set *set)
674 int i, j, f;
675 int k;
676 struct isl_basic_set *facet = NULL;
677 struct isl_basic_set *hull_facet = NULL;
678 unsigned total;
679 unsigned dim;
681 isl_assert(set->ctx, set->n > 0, goto error);
683 dim = isl_set_n_dim(set);
685 for (i = 0; i < hull->n_ineq; ++i) {
686 facet = compute_facet(set, hull->ineq[i]);
687 facet = isl_basic_set_add_equality(facet->ctx, facet, hull->ineq[i]);
688 facet = isl_basic_set_gauss(facet, NULL);
689 facet = isl_basic_set_normalize_constraints(facet);
690 hull_facet = isl_basic_set_copy(hull);
691 hull_facet = isl_basic_set_add_equality(hull_facet->ctx, hull_facet, hull->ineq[i]);
692 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
693 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
694 if (!facet)
695 goto error;
696 hull = isl_basic_set_cow(hull);
697 hull = isl_basic_set_extend_dim(hull,
698 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
699 for (j = 0; j < facet->n_ineq; ++j) {
700 for (f = 0; f < hull_facet->n_ineq; ++f)
701 if (isl_seq_eq(facet->ineq[j],
702 hull_facet->ineq[f], 1 + dim))
703 break;
704 if (f < hull_facet->n_ineq)
705 continue;
706 k = isl_basic_set_alloc_inequality(hull);
707 if (k < 0)
708 goto error;
709 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
710 if (!wrap_facet(set, hull->ineq[k], facet->ineq[j]))
711 goto error;
713 isl_basic_set_free(hull_facet);
714 isl_basic_set_free(facet);
716 hull = isl_basic_set_simplify(hull);
717 hull = isl_basic_set_finalize(hull);
718 return hull;
719 error:
720 isl_basic_set_free(hull_facet);
721 isl_basic_set_free(facet);
722 isl_basic_set_free(hull);
723 return NULL;
726 /* Special case for computing the convex hull of a one dimensional set.
727 * We simply collect the lower and upper bounds of each basic set
728 * and the biggest of those.
730 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
731 struct isl_set *set)
733 struct isl_mat *c = NULL;
734 isl_int *lower = NULL;
735 isl_int *upper = NULL;
736 int i, j, k;
737 isl_int a, b;
738 struct isl_basic_set *hull;
740 for (i = 0; i < set->n; ++i) {
741 set->p[i] = isl_basic_set_simplify(set->p[i]);
742 if (!set->p[i])
743 goto error;
745 set = isl_set_remove_empty_parts(set);
746 if (!set)
747 goto error;
748 isl_assert(ctx, set->n > 0, goto error);
749 c = isl_mat_alloc(ctx, 2, 2);
750 if (!c)
751 goto error;
753 if (set->p[0]->n_eq > 0) {
754 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
755 lower = c->row[0];
756 upper = c->row[1];
757 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
758 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
759 isl_seq_neg(upper, set->p[0]->eq[0], 2);
760 } else {
761 isl_seq_neg(lower, set->p[0]->eq[0], 2);
762 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
764 } else {
765 for (j = 0; j < set->p[0]->n_ineq; ++j) {
766 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
767 lower = c->row[0];
768 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
769 } else {
770 upper = c->row[1];
771 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
776 isl_int_init(a);
777 isl_int_init(b);
778 for (i = 0; i < set->n; ++i) {
779 struct isl_basic_set *bset = set->p[i];
780 int has_lower = 0;
781 int has_upper = 0;
783 for (j = 0; j < bset->n_eq; ++j) {
784 has_lower = 1;
785 has_upper = 1;
786 if (lower) {
787 isl_int_mul(a, lower[0], bset->eq[j][1]);
788 isl_int_mul(b, lower[1], bset->eq[j][0]);
789 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
790 isl_seq_cpy(lower, bset->eq[j], 2);
791 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
792 isl_seq_neg(lower, bset->eq[j], 2);
794 if (upper) {
795 isl_int_mul(a, upper[0], bset->eq[j][1]);
796 isl_int_mul(b, upper[1], bset->eq[j][0]);
797 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
798 isl_seq_neg(upper, bset->eq[j], 2);
799 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
800 isl_seq_cpy(upper, bset->eq[j], 2);
803 for (j = 0; j < bset->n_ineq; ++j) {
804 if (isl_int_is_pos(bset->ineq[j][1]))
805 has_lower = 1;
806 if (isl_int_is_neg(bset->ineq[j][1]))
807 has_upper = 1;
808 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
809 isl_int_mul(a, lower[0], bset->ineq[j][1]);
810 isl_int_mul(b, lower[1], bset->ineq[j][0]);
811 if (isl_int_lt(a, b))
812 isl_seq_cpy(lower, bset->ineq[j], 2);
814 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
815 isl_int_mul(a, upper[0], bset->ineq[j][1]);
816 isl_int_mul(b, upper[1], bset->ineq[j][0]);
817 if (isl_int_gt(a, b))
818 isl_seq_cpy(upper, bset->ineq[j], 2);
821 if (!has_lower)
822 lower = NULL;
823 if (!has_upper)
824 upper = NULL;
826 isl_int_clear(a);
827 isl_int_clear(b);
829 hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
830 hull = isl_basic_set_set_rational(hull);
831 if (!hull)
832 goto error;
833 if (lower) {
834 k = isl_basic_set_alloc_inequality(hull);
835 isl_seq_cpy(hull->ineq[k], lower, 2);
837 if (upper) {
838 k = isl_basic_set_alloc_inequality(hull);
839 isl_seq_cpy(hull->ineq[k], upper, 2);
841 hull = isl_basic_set_finalize(hull);
842 isl_set_free(set);
843 isl_mat_free(ctx, c);
844 return hull;
845 error:
846 isl_set_free(set);
847 isl_mat_free(ctx, c);
848 return NULL;
851 /* Project out final n dimensions using Fourier-Motzkin */
852 static struct isl_set *set_project_out(struct isl_ctx *ctx,
853 struct isl_set *set, unsigned n)
855 return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
858 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
860 struct isl_basic_set *convex_hull;
862 if (!set)
863 return NULL;
865 if (isl_set_is_empty(set))
866 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
867 else
868 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
869 isl_set_free(set);
870 return convex_hull;
873 /* Compute the convex hull of a pair of basic sets without any parameters or
874 * integer divisions using Fourier-Motzkin elimination.
875 * The convex hull is the set of all points that can be written as
876 * the sum of points from both basic sets (in homogeneous coordinates).
877 * We set up the constraints in a space with dimensions for each of
878 * the three sets and then project out the dimensions corresponding
879 * to the two original basic sets, retaining only those corresponding
880 * to the convex hull.
882 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
883 struct isl_basic_set *bset2)
885 int i, j, k;
886 struct isl_basic_set *bset[2];
887 struct isl_basic_set *hull = NULL;
888 unsigned dim;
890 if (!bset1 || !bset2)
891 goto error;
893 dim = isl_basic_set_n_dim(bset1);
894 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
895 1 + dim + bset1->n_eq + bset2->n_eq,
896 2 + bset1->n_ineq + bset2->n_ineq);
897 bset[0] = bset1;
898 bset[1] = bset2;
899 for (i = 0; i < 2; ++i) {
900 for (j = 0; j < bset[i]->n_eq; ++j) {
901 k = isl_basic_set_alloc_equality(hull);
902 if (k < 0)
903 goto error;
904 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
905 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
906 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
907 1+dim);
909 for (j = 0; j < bset[i]->n_ineq; ++j) {
910 k = isl_basic_set_alloc_inequality(hull);
911 if (k < 0)
912 goto error;
913 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
914 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
915 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
916 bset[i]->ineq[j], 1+dim);
918 k = isl_basic_set_alloc_inequality(hull);
919 if (k < 0)
920 goto error;
921 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
922 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
924 for (j = 0; j < 1+dim; ++j) {
925 k = isl_basic_set_alloc_equality(hull);
926 if (k < 0)
927 goto error;
928 isl_seq_clr(hull->eq[k], 1+2+3*dim);
929 isl_int_set_si(hull->eq[k][j], -1);
930 isl_int_set_si(hull->eq[k][1+dim+j], 1);
931 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
933 hull = isl_basic_set_set_rational(hull);
934 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
935 hull = isl_basic_set_convex_hull(hull);
936 isl_basic_set_free(bset1);
937 isl_basic_set_free(bset2);
938 return hull;
939 error:
940 isl_basic_set_free(bset1);
941 isl_basic_set_free(bset2);
942 isl_basic_set_free(hull);
943 return NULL;
946 /* Compute the lineality space of an "underlying" basic set.
947 * We basically just drop the constants and turn every inequality
948 * into an equality.
950 static struct isl_basic_set *ubasic_set_lineality_space(
951 struct isl_basic_set *bset)
953 int i, k;
954 struct isl_basic_set *lin;
955 unsigned dim = isl_basic_set_total_dim(bset);
957 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset), 0, dim, 0);
958 if (!lin)
959 goto error;
960 for (i = 0; i < bset->n_eq; ++i) {
961 k = isl_basic_set_alloc_equality(lin);
962 if (k < 0)
963 goto error;
964 isl_int_set_si(lin->eq[k][0], 0);
965 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
967 lin = isl_basic_set_gauss(lin, NULL);
968 if (!lin)
969 goto error;
970 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
971 k = isl_basic_set_alloc_equality(lin);
972 if (k < 0)
973 goto error;
974 isl_int_set_si(lin->eq[k][0], 0);
975 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
976 lin = isl_basic_set_gauss(lin, NULL);
977 if (!lin)
978 goto error;
980 isl_basic_set_free(bset);
981 return lin;
982 error:
983 isl_basic_set_free(lin);
984 isl_basic_set_free(bset);
985 return NULL;
988 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
989 * "underlying" set "set".
991 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
993 int i;
994 struct isl_set *lin = NULL;
996 if (!set)
997 return NULL;
998 if (set->n == 0) {
999 struct isl_dim *dim = isl_set_get_dim(set);
1000 isl_set_free(set);
1001 return isl_basic_set_empty(dim);
1004 lin = isl_set_alloc_dim(isl_set_get_dim(set), set->n, 0);
1005 for (i = 0; i < set->n; ++i)
1006 lin = isl_set_add(lin,
1007 ubasic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1008 isl_set_free(set);
1009 return isl_set_affine_hull(lin);
1012 /* Compute the convex hull of a set without any parameters or
1013 * integer divisions using Fourier-Motzkin elimination.
1014 * In each step, we combined two basic sets until only one
1015 * basic set is left.
1017 static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
1019 struct isl_basic_set *convex_hull = NULL;
1021 convex_hull = isl_set_copy_basic_set(set);
1022 set = isl_set_drop_basic_set(set, convex_hull);
1023 if (!set)
1024 goto error;
1025 while (set->n > 0) {
1026 struct isl_basic_set *t;
1027 t = isl_set_copy_basic_set(set);
1028 if (!t)
1029 goto error;
1030 set = isl_set_drop_basic_set(set, t);
1031 if (!set)
1032 goto error;
1033 convex_hull = convex_hull_pair(convex_hull, t);
1035 isl_set_free(set);
1036 return convex_hull;
1037 error:
1038 isl_set_free(set);
1039 isl_basic_set_free(convex_hull);
1040 return NULL;
1043 /* Compute an initial hull for wrapping containing a single initial
1044 * facet by first computing bounds on the set and then using these
1045 * bounds to construct an initial facet.
1046 * This function is a remnant of an older implementation where the
1047 * bounds were also used to check whether the set was bounded.
1048 * Since this function will now only be called when we know the
1049 * set to be bounded, the initial facet should probably be constructed
1050 * by simply using the coordinate directions instead.
1052 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1053 struct isl_set *set)
1055 struct isl_mat *bounds = NULL;
1056 unsigned dim;
1057 int k;
1059 if (!hull)
1060 goto error;
1061 bounds = independent_bounds(set->ctx, set);
1062 if (!bounds)
1063 goto error;
1064 isl_assert(set->ctx, bounds->n_row == isl_set_n_dim(set), goto error);
1065 bounds = initial_facet_constraint(set->ctx, set, bounds);
1066 if (!bounds)
1067 goto error;
1068 k = isl_basic_set_alloc_inequality(hull);
1069 if (k < 0)
1070 goto error;
1071 dim = isl_set_n_dim(set);
1072 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1073 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1074 isl_mat_free(set->ctx, bounds);
1076 return hull;
1077 error:
1078 isl_basic_set_free(hull);
1079 isl_mat_free(set->ctx, bounds);
1080 return NULL;
1083 struct max_constraint {
1084 struct isl_mat *c;
1085 int count;
1086 int ineq;
1089 static int max_constraint_equal(const void *entry, const void *val)
1091 struct max_constraint *a = (struct max_constraint *)entry;
1092 isl_int *b = (isl_int *)val;
1094 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1097 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1098 isl_int *con, unsigned len, int n, int ineq)
1100 struct isl_hash_table_entry *entry;
1101 struct max_constraint *c;
1102 uint32_t c_hash;
1104 c_hash = isl_seq_hash(con + 1, len, isl_hash_init());
1105 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1106 con + 1, 0);
1107 if (!entry)
1108 return;
1109 c = entry->data;
1110 if (c->count < n) {
1111 isl_hash_table_remove(ctx, table, entry);
1112 return;
1114 c->count++;
1115 if (isl_int_gt(c->c->row[0][0], con[0]))
1116 return;
1117 if (isl_int_eq(c->c->row[0][0], con[0])) {
1118 if (ineq)
1119 c->ineq = ineq;
1120 return;
1122 c->c = isl_mat_cow(ctx, c->c);
1123 isl_int_set(c->c->row[0][0], con[0]);
1124 c->ineq = ineq;
1127 /* Check whether the constraint hash table "table" constains the constraint
1128 * "con".
1130 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1131 isl_int *con, unsigned len, int n)
1133 struct isl_hash_table_entry *entry;
1134 struct max_constraint *c;
1135 uint32_t c_hash;
1137 c_hash = isl_seq_hash(con + 1, len, isl_hash_init());
1138 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1139 con + 1, 0);
1140 if (!entry)
1141 return 0;
1142 c = entry->data;
1143 if (c->count < n)
1144 return 0;
1145 return isl_int_eq(c->c->row[0][0], con[0]);
1148 /* Check for inequality constraints of a basic set without equalities
1149 * such that the same or more stringent copies of the constraint appear
1150 * in all of the basic sets. Such constraints are necessarily facet
1151 * constraints of the convex hull.
1153 * If the resulting basic set is by chance identical to one of
1154 * the basic sets in "set", then we know that this basic set contains
1155 * all other basic sets and is therefore the convex hull of set.
1156 * In this case we set *is_hull to 1.
1158 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1159 struct isl_set *set, int *is_hull)
1161 int i, j, s, n;
1162 int min_constraints;
1163 int best;
1164 struct max_constraint *constraints = NULL;
1165 struct isl_hash_table *table = NULL;
1166 unsigned total;
1168 *is_hull = 0;
1170 for (i = 0; i < set->n; ++i)
1171 if (set->p[i]->n_eq == 0)
1172 break;
1173 if (i >= set->n)
1174 return hull;
1175 min_constraints = set->p[i]->n_ineq;
1176 best = i;
1177 for (i = best + 1; i < set->n; ++i) {
1178 if (set->p[i]->n_eq != 0)
1179 continue;
1180 if (set->p[i]->n_ineq >= min_constraints)
1181 continue;
1182 min_constraints = set->p[i]->n_ineq;
1183 best = i;
1185 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1186 min_constraints);
1187 if (!constraints)
1188 return hull;
1189 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1190 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1191 goto error;
1193 total = isl_dim_total(set->dim);
1194 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1195 constraints[i].c = isl_mat_sub_alloc(hull->ctx,
1196 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1197 if (!constraints[i].c)
1198 goto error;
1199 constraints[i].ineq = 1;
1201 for (i = 0; i < min_constraints; ++i) {
1202 struct isl_hash_table_entry *entry;
1203 uint32_t c_hash;
1204 c_hash = isl_seq_hash(constraints[i].c->row[0] + 1, total,
1205 isl_hash_init());
1206 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1207 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1208 if (!entry)
1209 goto error;
1210 isl_assert(hull->ctx, !entry->data, goto error);
1211 entry->data = &constraints[i];
1214 n = 0;
1215 for (s = 0; s < set->n; ++s) {
1216 if (s == best)
1217 continue;
1219 for (i = 0; i < set->p[s]->n_eq; ++i) {
1220 isl_int *eq = set->p[s]->eq[i];
1221 for (j = 0; j < 2; ++j) {
1222 isl_seq_neg(eq, eq, 1 + total);
1223 update_constraint(hull->ctx, table,
1224 eq, total, n, 0);
1227 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1228 isl_int *ineq = set->p[s]->ineq[i];
1229 update_constraint(hull->ctx, table, ineq, total, n,
1230 set->p[s]->n_eq == 0);
1232 ++n;
1235 for (i = 0; i < min_constraints; ++i) {
1236 if (constraints[i].count < n)
1237 continue;
1238 if (!constraints[i].ineq)
1239 continue;
1240 j = isl_basic_set_alloc_inequality(hull);
1241 if (j < 0)
1242 goto error;
1243 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1246 for (s = 0; s < set->n; ++s) {
1247 if (set->p[s]->n_eq)
1248 continue;
1249 if (set->p[s]->n_ineq != hull->n_ineq)
1250 continue;
1251 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1252 isl_int *ineq = set->p[s]->ineq[i];
1253 if (!has_constraint(hull->ctx, table, ineq, total, n))
1254 break;
1256 if (i == set->p[s]->n_ineq)
1257 *is_hull = 1;
1260 isl_hash_table_clear(table);
1261 for (i = 0; i < min_constraints; ++i)
1262 isl_mat_free(hull->ctx, constraints[i].c);
1263 free(constraints);
1264 free(table);
1265 return hull;
1266 error:
1267 isl_hash_table_clear(table);
1268 free(table);
1269 if (constraints)
1270 for (i = 0; i < min_constraints; ++i)
1271 isl_mat_free(hull->ctx, constraints[i].c);
1272 free(constraints);
1273 return hull;
1276 /* Create a template for the convex hull of "set" and fill it up
1277 * obvious facet constraints, if any. If the result happens to
1278 * be the convex hull of "set" then *is_hull is set to 1.
1280 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1282 struct isl_basic_set *hull;
1283 unsigned n_ineq;
1284 int i;
1286 n_ineq = 1;
1287 for (i = 0; i < set->n; ++i) {
1288 n_ineq += set->p[i]->n_eq;
1289 n_ineq += set->p[i]->n_ineq;
1291 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1292 hull = isl_basic_set_set_rational(hull);
1293 if (!hull)
1294 return NULL;
1295 return common_constraints(hull, set, is_hull);
1298 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1300 struct isl_basic_set *hull;
1301 int is_hull;
1303 hull = proto_hull(set, &is_hull);
1304 if (hull && !is_hull) {
1305 if (hull->n_ineq == 0)
1306 hull = initial_hull(hull, set);
1307 hull = extend(hull, set);
1309 isl_set_free(set);
1311 return hull;
1314 static int isl_basic_set_is_bounded(struct isl_basic_set *bset)
1316 struct isl_tab *tab;
1317 int bounded;
1319 tab = isl_tab_from_recession_cone((struct isl_basic_map *)bset);
1320 bounded = isl_tab_cone_is_bounded(bset->ctx, tab);
1321 isl_tab_free(bset->ctx, tab);
1322 return bounded;
1325 static int isl_set_is_bounded(struct isl_set *set)
1327 int i;
1329 for (i = 0; i < set->n; ++i) {
1330 int bounded = isl_basic_set_is_bounded(set->p[i]);
1331 if (!bounded || bounded < 0)
1332 return bounded;
1334 return 1;
1337 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1339 /* Given a set and a linear space "lin" of dimension n > 0,
1340 * project the linear space from the set, compute the convex hull
1341 * and then map the set back to the original space.
1343 * Let
1345 * M x = 0
1347 * describe the linear space. We first compute the Hermite normal
1348 * form H = M U of M = H Q, to obtain
1350 * H Q x = 0
1352 * The last n rows of H will be zero, so the last n variables of x' = Q x
1353 * are the one we want to project out. We do this by transforming each
1354 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1355 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1356 * we transform the hull back to the original space as A' Q_1 x >= b',
1357 * with Q_1 all but the last n rows of Q.
1359 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1360 struct isl_basic_set *lin)
1362 unsigned total = isl_basic_set_total_dim(lin);
1363 unsigned lin_dim;
1364 struct isl_basic_set *hull;
1365 struct isl_mat *M, *U, *Q;
1367 if (!set || !lin)
1368 goto error;
1369 lin_dim = total - lin->n_eq;
1370 M = isl_mat_sub_alloc(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1371 M = isl_mat_left_hermite(set->ctx, M, 0, &U, &Q);
1372 if (!M)
1373 goto error;
1374 isl_mat_free(set->ctx, M);
1375 isl_basic_set_free(lin);
1377 isl_mat_drop_rows(set->ctx, Q, Q->n_row - lin_dim, lin_dim);
1379 U = isl_mat_lin_to_aff(set->ctx, U);
1380 Q = isl_mat_lin_to_aff(set->ctx, Q);
1382 set = isl_set_preimage(set, U);
1383 set = isl_set_remove_dims(set, total - lin_dim, lin_dim);
1384 hull = uset_convex_hull(set);
1385 hull = isl_basic_set_preimage(hull, Q);
1387 return hull;
1388 error:
1389 isl_basic_set_free(lin);
1390 isl_set_free(set);
1391 return NULL;
1394 /* Compute the convex hull of a set without any parameters or
1395 * integer divisions. Depending on whether the set is bounded,
1396 * we pass control to the wrapping based convex hull or
1397 * the Fourier-Motzkin elimination based convex hull.
1398 * We also handle a few special cases before checking the boundedness.
1400 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1402 int i;
1403 struct isl_basic_set *convex_hull = NULL;
1404 struct isl_basic_set *lin;
1406 if (isl_set_n_dim(set) == 0)
1407 return convex_hull_0d(set);
1409 set = isl_set_coalesce(set);
1410 set = isl_set_set_rational(set);
1412 if (!set)
1413 goto error;
1414 if (!set)
1415 return NULL;
1416 if (set->n == 1) {
1417 convex_hull = isl_basic_set_copy(set->p[0]);
1418 isl_set_free(set);
1419 return convex_hull;
1421 if (isl_set_n_dim(set) == 1)
1422 return convex_hull_1d(set->ctx, set);
1424 if (isl_set_is_bounded(set))
1425 return uset_convex_hull_wrap(set);
1427 lin = uset_combined_lineality_space(isl_set_copy(set));
1428 if (!lin)
1429 goto error;
1430 if (isl_basic_set_is_universe(lin)) {
1431 isl_set_free(set);
1432 return lin;
1434 if (lin->n_eq < isl_basic_set_total_dim(lin))
1435 return modulo_lineality(set, lin);
1436 isl_basic_set_free(lin);
1438 return uset_convex_hull_elim(set);
1439 error:
1440 isl_set_free(set);
1441 isl_basic_set_free(convex_hull);
1442 return NULL;
1445 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1446 * without parameters or divs and where the convex hull of set is
1447 * known to be full-dimensional.
1449 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1451 int i;
1452 struct isl_basic_set *convex_hull = NULL;
1454 if (isl_set_n_dim(set) == 0) {
1455 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1456 isl_set_free(set);
1457 convex_hull = isl_basic_set_set_rational(convex_hull);
1458 return convex_hull;
1461 set = isl_set_set_rational(set);
1463 if (!set)
1464 goto error;
1465 set = isl_set_normalize(set);
1466 if (!set)
1467 goto error;
1468 if (set->n == 1) {
1469 convex_hull = isl_basic_set_copy(set->p[0]);
1470 isl_set_free(set);
1471 return convex_hull;
1473 if (isl_set_n_dim(set) == 1)
1474 return convex_hull_1d(set->ctx, set);
1476 return uset_convex_hull_wrap(set);
1477 error:
1478 isl_set_free(set);
1479 return NULL;
1482 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1483 * We first remove the equalities (transforming the set), compute the
1484 * convex hull of the transformed set and then add the equalities back
1485 * (after performing the inverse transformation.
1487 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1488 struct isl_set *set, struct isl_basic_set *affine_hull)
1490 struct isl_mat *T;
1491 struct isl_mat *T2;
1492 struct isl_basic_set *dummy;
1493 struct isl_basic_set *convex_hull;
1495 dummy = isl_basic_set_remove_equalities(
1496 isl_basic_set_copy(affine_hull), &T, &T2);
1497 if (!dummy)
1498 goto error;
1499 isl_basic_set_free(dummy);
1500 set = isl_set_preimage(set, T);
1501 convex_hull = uset_convex_hull(set);
1502 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1503 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1504 return convex_hull;
1505 error:
1506 isl_basic_set_free(affine_hull);
1507 isl_set_free(set);
1508 return NULL;
1511 /* Compute the convex hull of a map.
1513 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1514 * specifically, the wrapping of facets to obtain new facets.
1516 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1518 struct isl_basic_set *bset;
1519 struct isl_basic_map *model = NULL;
1520 struct isl_basic_set *affine_hull = NULL;
1521 struct isl_basic_map *convex_hull = NULL;
1522 struct isl_set *set = NULL;
1523 struct isl_ctx *ctx;
1525 if (!map)
1526 goto error;
1528 ctx = map->ctx;
1529 if (map->n == 0) {
1530 convex_hull = isl_basic_map_empty_like_map(map);
1531 isl_map_free(map);
1532 return convex_hull;
1535 map = isl_map_detect_equalities(map);
1536 map = isl_map_align_divs(map);
1537 model = isl_basic_map_copy(map->p[0]);
1538 set = isl_map_underlying_set(map);
1539 if (!set)
1540 goto error;
1542 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1543 if (!affine_hull)
1544 goto error;
1545 if (affine_hull->n_eq != 0)
1546 bset = modulo_affine_hull(ctx, set, affine_hull);
1547 else {
1548 isl_basic_set_free(affine_hull);
1549 bset = uset_convex_hull(set);
1552 convex_hull = isl_basic_map_overlying_set(bset, model);
1554 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1555 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1556 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1557 return convex_hull;
1558 error:
1559 isl_set_free(set);
1560 isl_basic_map_free(model);
1561 return NULL;
1564 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1566 return (struct isl_basic_set *)
1567 isl_map_convex_hull((struct isl_map *)set);
1570 struct sh_data_entry {
1571 struct isl_hash_table *table;
1572 struct isl_tab *tab;
1575 /* Holds the data needed during the simple hull computation.
1576 * In particular,
1577 * n the number of basic sets in the original set
1578 * hull_table a hash table of already computed constraints
1579 * in the simple hull
1580 * p for each basic set,
1581 * table a hash table of the constraints
1582 * tab the tableau corresponding to the basic set
1584 struct sh_data {
1585 struct isl_ctx *ctx;
1586 unsigned n;
1587 struct isl_hash_table *hull_table;
1588 struct sh_data_entry p[0];
1591 static void sh_data_free(struct sh_data *data)
1593 int i;
1595 if (!data)
1596 return;
1597 isl_hash_table_free(data->ctx, data->hull_table);
1598 for (i = 0; i < data->n; ++i) {
1599 isl_hash_table_free(data->ctx, data->p[i].table);
1600 isl_tab_free(data->ctx, data->p[i].tab);
1602 free(data);
1605 struct ineq_cmp_data {
1606 unsigned len;
1607 isl_int *p;
1610 static int has_ineq(const void *entry, const void *val)
1612 isl_int *row = (isl_int *)entry;
1613 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
1615 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
1616 isl_seq_is_neg(row + 1, v->p + 1, v->len);
1619 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
1620 isl_int *ineq, unsigned len)
1622 uint32_t c_hash;
1623 struct ineq_cmp_data v;
1624 struct isl_hash_table_entry *entry;
1626 v.len = len;
1627 v.p = ineq;
1628 c_hash = isl_seq_hash(ineq + 1, len, isl_hash_init());
1629 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
1630 if (!entry)
1631 return - 1;
1632 entry->data = ineq;
1633 return 0;
1636 /* Fill hash table "table" with the constraints of "bset".
1637 * Equalities are added as two inequalities.
1638 * The value in the hash table is a pointer to the (in)equality of "bset".
1640 static int hash_basic_set(struct isl_hash_table *table,
1641 struct isl_basic_set *bset)
1643 int i, j;
1644 unsigned dim = isl_basic_set_total_dim(bset);
1646 for (i = 0; i < bset->n_eq; ++i) {
1647 for (j = 0; j < 2; ++j) {
1648 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
1649 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
1650 return -1;
1653 for (i = 0; i < bset->n_ineq; ++i) {
1654 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
1655 return -1;
1657 return 0;
1660 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
1662 struct sh_data *data;
1663 int i;
1665 data = isl_calloc(set->ctx, struct sh_data,
1666 sizeof(struct sh_data) + set->n * sizeof(struct sh_data_entry));
1667 if (!data)
1668 return NULL;
1669 data->ctx = set->ctx;
1670 data->n = set->n;
1671 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
1672 if (!data->hull_table)
1673 goto error;
1674 for (i = 0; i < set->n; ++i) {
1675 data->p[i].table = isl_hash_table_alloc(set->ctx,
1676 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
1677 if (!data->p[i].table)
1678 goto error;
1679 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
1680 goto error;
1682 return data;
1683 error:
1684 sh_data_free(data);
1685 return NULL;
1688 /* Check if inequality "ineq" is a bound for basic set "j" or if
1689 * it can be relaxed (by increasing the constant term) to become
1690 * a bound for that basic set. In the latter case, the constant
1691 * term is updated.
1692 * Return 1 if "ineq" is a bound
1693 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
1694 * -1 if some error occurred
1696 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
1697 isl_int *ineq)
1699 enum isl_lp_result res;
1700 isl_int opt;
1702 if (!data->p[j].tab) {
1703 data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
1704 if (!data->p[j].tab)
1705 return -1;
1708 isl_int_init(opt);
1710 res = isl_tab_min(data->ctx, data->p[j].tab, ineq, data->ctx->one,
1711 &opt, NULL);
1712 if (res == isl_lp_ok && isl_int_is_neg(opt))
1713 isl_int_sub(ineq[0], ineq[0], opt);
1715 isl_int_clear(opt);
1717 return res == isl_lp_ok ? 1 :
1718 res == isl_lp_unbounded ? 0 : -1;
1721 /* Check if inequality "ineq" from basic set "i" can be relaxed to
1722 * become a bound on the whole set. If so, add the (relaxed) inequality
1723 * to "hull".
1725 * We first check if "hull" already contains a translate of the inequality.
1726 * If so, we are done.
1727 * Then, we check if any of the previous basic sets contains a translate
1728 * of the inequality. If so, then we have already considered this
1729 * inequality and we are done.
1730 * Otherwise, for each basic set other than "i", we check if the inequality
1731 * is a bound on the basic set.
1732 * For previous basic sets, we know that they do not contain a translate
1733 * of the inequality, so we directly call is_bound.
1734 * For following basic sets, we first check if a translate of the
1735 * inequality appears in its description and if so directly update
1736 * the inequality accordingly.
1738 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
1739 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
1741 uint32_t c_hash;
1742 struct ineq_cmp_data v;
1743 struct isl_hash_table_entry *entry;
1744 int j, k;
1746 if (!hull)
1747 return NULL;
1749 v.len = isl_basic_set_total_dim(hull);
1750 v.p = ineq;
1751 c_hash = isl_seq_hash(ineq + 1, v.len, isl_hash_init());
1753 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
1754 has_ineq, &v, 0);
1755 if (entry)
1756 return hull;
1758 for (j = 0; j < i; ++j) {
1759 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
1760 c_hash, has_ineq, &v, 0);
1761 if (entry)
1762 break;
1764 if (j < i)
1765 return hull;
1767 k = isl_basic_set_alloc_inequality(hull);
1768 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
1769 if (k < 0)
1770 goto error;
1772 for (j = 0; j < i; ++j) {
1773 int bound;
1774 bound = is_bound(data, set, j, hull->ineq[k]);
1775 if (bound < 0)
1776 goto error;
1777 if (!bound)
1778 break;
1780 if (j < i) {
1781 isl_basic_set_free_inequality(hull, 1);
1782 return hull;
1785 for (j = i + 1; j < set->n; ++j) {
1786 int bound, neg;
1787 isl_int *ineq_j;
1788 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
1789 c_hash, has_ineq, &v, 0);
1790 if (entry) {
1791 ineq_j = entry->data;
1792 neg = isl_seq_is_neg(ineq_j + 1,
1793 hull->ineq[k] + 1, v.len);
1794 if (neg)
1795 isl_int_neg(ineq_j[0], ineq_j[0]);
1796 if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
1797 isl_int_set(hull->ineq[k][0], ineq_j[0]);
1798 if (neg)
1799 isl_int_neg(ineq_j[0], ineq_j[0]);
1800 continue;
1802 bound = is_bound(data, set, j, hull->ineq[k]);
1803 if (bound < 0)
1804 goto error;
1805 if (!bound)
1806 break;
1808 if (j < set->n) {
1809 isl_basic_set_free_inequality(hull, 1);
1810 return hull;
1813 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
1814 has_ineq, &v, 1);
1815 if (!entry)
1816 goto error;
1817 entry->data = hull->ineq[k];
1819 return hull;
1820 error:
1821 isl_basic_set_free(hull);
1822 return NULL;
1825 /* Check if any inequality from basic set "i" can be relaxed to
1826 * become a bound on the whole set. If so, add the (relaxed) inequality
1827 * to "hull".
1829 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
1830 struct sh_data *data, struct isl_set *set, int i)
1832 int j, k;
1833 unsigned dim = isl_basic_set_total_dim(bset);
1835 for (j = 0; j < set->p[i]->n_eq; ++j) {
1836 for (k = 0; k < 2; ++k) {
1837 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
1838 add_bound(bset, data, set, i, set->p[i]->eq[j]);
1841 for (j = 0; j < set->p[i]->n_ineq; ++j)
1842 add_bound(bset, data, set, i, set->p[i]->ineq[j]);
1843 return bset;
1846 /* Compute a superset of the convex hull of set that is described
1847 * by only translates of the constraints in the constituents of set.
1849 static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
1851 struct sh_data *data = NULL;
1852 struct isl_basic_set *hull = NULL;
1853 unsigned n_ineq;
1854 int i, j;
1856 if (!set)
1857 return NULL;
1859 n_ineq = 0;
1860 for (i = 0; i < set->n; ++i) {
1861 if (!set->p[i])
1862 goto error;
1863 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
1866 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1867 if (!hull)
1868 goto error;
1870 data = sh_data_alloc(set, n_ineq);
1871 if (!data)
1872 goto error;
1874 for (i = 0; i < set->n; ++i)
1875 hull = add_bounds(hull, data, set, i);
1877 sh_data_free(data);
1878 isl_set_free(set);
1880 return hull;
1881 error:
1882 sh_data_free(data);
1883 isl_basic_set_free(hull);
1884 isl_set_free(set);
1885 return NULL;
1888 /* Compute a superset of the convex hull of map that is described
1889 * by only translates of the constraints in the constituents of map.
1891 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
1893 struct isl_set *set = NULL;
1894 struct isl_basic_map *model = NULL;
1895 struct isl_basic_map *hull;
1896 struct isl_basic_map *affine_hull;
1897 struct isl_basic_set *bset = NULL;
1899 if (!map)
1900 return NULL;
1901 if (map->n == 0) {
1902 hull = isl_basic_map_empty_like_map(map);
1903 isl_map_free(map);
1904 return hull;
1906 if (map->n == 1) {
1907 hull = isl_basic_map_copy(map->p[0]);
1908 isl_map_free(map);
1909 return hull;
1912 map = isl_map_detect_equalities(map);
1913 affine_hull = isl_map_affine_hull(isl_map_copy(map));
1914 map = isl_map_align_divs(map);
1915 model = isl_basic_map_copy(map->p[0]);
1917 set = isl_map_underlying_set(map);
1919 bset = uset_simple_hull(set);
1921 hull = isl_basic_map_overlying_set(bset, model);
1923 hull = isl_basic_map_intersect(hull, affine_hull);
1924 hull = isl_basic_map_convex_hull(hull);
1925 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
1926 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1928 return hull;
1931 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
1933 return (struct isl_basic_set *)
1934 isl_map_simple_hull((struct isl_map *)set);
1937 /* Given a set "set", return parametric bounds on the dimension "dim".
1939 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
1941 unsigned set_dim = isl_set_dim(set, isl_dim_set);
1942 set = isl_set_copy(set);
1943 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
1944 set = isl_set_eliminate_dims(set, 0, dim);
1945 return isl_set_convex_hull(set);
1948 /* Computes a "simple hull" and then check if each dimension in the
1949 * resulting hull is bounded by a symbolic constant. If not, the
1950 * hull is intersected with the corresponding bounds on the whole set.
1952 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
1954 int i, j;
1955 struct isl_basic_set *hull;
1956 unsigned nparam, left;
1957 int removed_divs = 0;
1959 hull = isl_set_simple_hull(isl_set_copy(set));
1960 if (!hull)
1961 goto error;
1963 nparam = isl_basic_set_dim(hull, isl_dim_param);
1964 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
1965 int lower = 0, upper = 0;
1966 struct isl_basic_set *bounds;
1968 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
1969 for (j = 0; j < hull->n_eq; ++j) {
1970 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
1971 continue;
1972 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
1973 left) == -1)
1974 break;
1976 if (j < hull->n_eq)
1977 continue;
1979 for (j = 0; j < hull->n_ineq; ++j) {
1980 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
1981 continue;
1982 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
1983 left) != -1 ||
1984 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
1985 i) != -1)
1986 continue;
1987 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
1988 lower = 1;
1989 else
1990 upper = 1;
1991 if (lower && upper)
1992 break;
1995 if (lower && upper)
1996 continue;
1998 if (!removed_divs) {
1999 set = isl_set_remove_divs(set);
2000 if (!set)
2001 goto error;
2002 removed_divs = 1;
2004 bounds = set_bounds(set, i);
2005 hull = isl_basic_set_intersect(hull, bounds);
2006 if (!hull)
2007 goto error;
2010 isl_set_free(set);
2011 return hull;
2012 error:
2013 isl_set_free(set);
2014 return NULL;