isl_stream: keep track of textual representation of tokens for better error reporting
[isl.git] / isl_convex_hull.c
bloba6442015588e8dbae781deff58de77d2005d2416
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_map_private.h>
11 #include <isl/lp.h>
12 #include <isl/map.h>
13 #include <isl_mat_private.h>
14 #include <isl/set.h>
15 #include <isl/seq.h>
16 #include "isl_equalities.h"
17 #include "isl_tab.h"
19 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
21 static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
23 isl_int *t;
25 if (i != j) {
26 t = bmap->ineq[i];
27 bmap->ineq[i] = bmap->ineq[j];
28 bmap->ineq[j] = t;
32 /* Return 1 if constraint c is redundant with respect to the constraints
33 * in bmap. If c is a lower [upper] bound in some variable and bmap
34 * does not have a lower [upper] bound in that variable, then c cannot
35 * be redundant and we do not need solve any lp.
37 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
38 isl_int *c, isl_int *opt_n, isl_int *opt_d)
40 enum isl_lp_result res;
41 unsigned total;
42 int i, j;
44 if (!bmap)
45 return -1;
47 total = isl_basic_map_total_dim(*bmap);
48 for (i = 0; i < total; ++i) {
49 int sign;
50 if (isl_int_is_zero(c[1+i]))
51 continue;
52 sign = isl_int_sgn(c[1+i]);
53 for (j = 0; j < (*bmap)->n_ineq; ++j)
54 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
55 break;
56 if (j == (*bmap)->n_ineq)
57 break;
59 if (i < total)
60 return 0;
62 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
63 opt_n, opt_d, NULL);
64 if (res == isl_lp_unbounded)
65 return 0;
66 if (res == isl_lp_error)
67 return -1;
68 if (res == isl_lp_empty) {
69 *bmap = isl_basic_map_set_to_empty(*bmap);
70 return 0;
72 return !isl_int_is_neg(*opt_n);
75 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
76 isl_int *c, isl_int *opt_n, isl_int *opt_d)
78 return isl_basic_map_constraint_is_redundant(
79 (struct isl_basic_map **)bset, c, opt_n, opt_d);
82 /* Remove redundant
83 * constraints. If the minimal value along the normal of a constraint
84 * is the same if the constraint is removed, then the constraint is redundant.
86 * Alternatively, we could have intersected the basic map with the
87 * corresponding equality and the checked if the dimension was that
88 * of a facet.
90 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
91 __isl_take isl_basic_map *bmap)
93 struct isl_tab *tab;
95 if (!bmap)
96 return NULL;
98 bmap = isl_basic_map_gauss(bmap, NULL);
99 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
100 return bmap;
101 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
102 return bmap;
103 if (bmap->n_ineq <= 1)
104 return bmap;
106 tab = isl_tab_from_basic_map(bmap);
107 if (isl_tab_detect_implicit_equalities(tab) < 0)
108 goto error;
109 if (isl_tab_detect_redundant(tab) < 0)
110 goto error;
111 bmap = isl_basic_map_update_from_tab(bmap, tab);
112 isl_tab_free(tab);
113 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
114 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
115 return bmap;
116 error:
117 isl_tab_free(tab);
118 isl_basic_map_free(bmap);
119 return NULL;
122 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
123 __isl_take isl_basic_set *bset)
125 return (struct isl_basic_set *)
126 isl_basic_map_remove_redundancies((struct isl_basic_map *)bset);
129 /* Check if the set set is bound in the direction of the affine
130 * constraint c and if so, set the constant term such that the
131 * resulting constraint is a bounding constraint for the set.
133 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
135 int first;
136 int j;
137 isl_int opt;
138 isl_int opt_denom;
140 isl_int_init(opt);
141 isl_int_init(opt_denom);
142 first = 1;
143 for (j = 0; j < set->n; ++j) {
144 enum isl_lp_result res;
146 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
147 continue;
149 res = isl_basic_set_solve_lp(set->p[j],
150 0, c, set->ctx->one, &opt, &opt_denom, NULL);
151 if (res == isl_lp_unbounded)
152 break;
153 if (res == isl_lp_error)
154 goto error;
155 if (res == isl_lp_empty) {
156 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
157 if (!set->p[j])
158 goto error;
159 continue;
161 if (first || isl_int_is_neg(opt)) {
162 if (!isl_int_is_one(opt_denom))
163 isl_seq_scale(c, c, opt_denom, len);
164 isl_int_sub(c[0], c[0], opt);
166 first = 0;
168 isl_int_clear(opt);
169 isl_int_clear(opt_denom);
170 return j >= set->n;
171 error:
172 isl_int_clear(opt);
173 isl_int_clear(opt_denom);
174 return -1;
177 struct isl_basic_set *isl_basic_set_set_rational(struct isl_basic_set *bset)
179 if (!bset)
180 return NULL;
182 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
183 return bset;
185 bset = isl_basic_set_cow(bset);
186 if (!bset)
187 return NULL;
189 ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
191 return isl_basic_set_finalize(bset);
194 static struct isl_set *isl_set_set_rational(struct isl_set *set)
196 int i;
198 set = isl_set_cow(set);
199 if (!set)
200 return NULL;
201 for (i = 0; i < set->n; ++i) {
202 set->p[i] = isl_basic_set_set_rational(set->p[i]);
203 if (!set->p[i])
204 goto error;
206 return set;
207 error:
208 isl_set_free(set);
209 return NULL;
212 static struct isl_basic_set *isl_basic_set_add_equality(
213 struct isl_basic_set *bset, isl_int *c)
215 int i;
216 unsigned dim;
218 if (!bset)
219 return NULL;
221 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
222 return bset;
224 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
225 isl_assert(bset->ctx, bset->n_div == 0, goto error);
226 dim = isl_basic_set_n_dim(bset);
227 bset = isl_basic_set_cow(bset);
228 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
229 i = isl_basic_set_alloc_equality(bset);
230 if (i < 0)
231 goto error;
232 isl_seq_cpy(bset->eq[i], c, 1 + dim);
233 return bset;
234 error:
235 isl_basic_set_free(bset);
236 return NULL;
239 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
241 int i;
243 set = isl_set_cow(set);
244 if (!set)
245 return NULL;
246 for (i = 0; i < set->n; ++i) {
247 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
248 if (!set->p[i])
249 goto error;
251 return set;
252 error:
253 isl_set_free(set);
254 return NULL;
257 /* Given a union of basic sets, construct the constraints for wrapping
258 * a facet around one of its ridges.
259 * In particular, if each of n the d-dimensional basic sets i in "set"
260 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
261 * and is defined by the constraints
262 * [ 1 ]
263 * A_i [ x ] >= 0
265 * then the resulting set is of dimension n*(1+d) and has as constraints
267 * [ a_i ]
268 * A_i [ x_i ] >= 0
270 * a_i >= 0
272 * \sum_i x_{i,1} = 1
274 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
276 struct isl_basic_set *lp;
277 unsigned n_eq;
278 unsigned n_ineq;
279 int i, j, k;
280 unsigned dim, lp_dim;
282 if (!set)
283 return NULL;
285 dim = 1 + isl_set_n_dim(set);
286 n_eq = 1;
287 n_ineq = set->n;
288 for (i = 0; i < set->n; ++i) {
289 n_eq += set->p[i]->n_eq;
290 n_ineq += set->p[i]->n_ineq;
292 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
293 lp = isl_basic_set_set_rational(lp);
294 if (!lp)
295 return NULL;
296 lp_dim = isl_basic_set_n_dim(lp);
297 k = isl_basic_set_alloc_equality(lp);
298 isl_int_set_si(lp->eq[k][0], -1);
299 for (i = 0; i < set->n; ++i) {
300 isl_int_set_si(lp->eq[k][1+dim*i], 0);
301 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
302 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
304 for (i = 0; i < set->n; ++i) {
305 k = isl_basic_set_alloc_inequality(lp);
306 isl_seq_clr(lp->ineq[k], 1+lp_dim);
307 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
309 for (j = 0; j < set->p[i]->n_eq; ++j) {
310 k = isl_basic_set_alloc_equality(lp);
311 isl_seq_clr(lp->eq[k], 1+dim*i);
312 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
313 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
316 for (j = 0; j < set->p[i]->n_ineq; ++j) {
317 k = isl_basic_set_alloc_inequality(lp);
318 isl_seq_clr(lp->ineq[k], 1+dim*i);
319 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
320 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
323 return lp;
326 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
327 * of that facet, compute the other facet of the convex hull that contains
328 * the ridge.
330 * We first transform the set such that the facet constraint becomes
332 * x_1 >= 0
334 * I.e., the facet lies in
336 * x_1 = 0
338 * and on that facet, the constraint that defines the ridge is
340 * x_2 >= 0
342 * (This transformation is not strictly needed, all that is needed is
343 * that the ridge contains the origin.)
345 * Since the ridge contains the origin, the cone of the convex hull
346 * will be of the form
348 * x_1 >= 0
349 * x_2 >= a x_1
351 * with this second constraint defining the new facet.
352 * The constant a is obtained by settting x_1 in the cone of the
353 * convex hull to 1 and minimizing x_2.
354 * Now, each element in the cone of the convex hull is the sum
355 * of elements in the cones of the basic sets.
356 * If a_i is the dilation factor of basic set i, then the problem
357 * we need to solve is
359 * min \sum_i x_{i,2}
360 * st
361 * \sum_i x_{i,1} = 1
362 * a_i >= 0
363 * [ a_i ]
364 * A [ x_i ] >= 0
366 * with
367 * [ 1 ]
368 * A_i [ x_i ] >= 0
370 * the constraints of each (transformed) basic set.
371 * If a = n/d, then the constraint defining the new facet (in the transformed
372 * space) is
374 * -n x_1 + d x_2 >= 0
376 * In the original space, we need to take the same combination of the
377 * corresponding constraints "facet" and "ridge".
379 * If a = -infty = "-1/0", then we just return the original facet constraint.
380 * This means that the facet is unbounded, but has a bounded intersection
381 * with the union of sets.
383 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
384 isl_int *facet, isl_int *ridge)
386 int i;
387 isl_ctx *ctx;
388 struct isl_mat *T = NULL;
389 struct isl_basic_set *lp = NULL;
390 struct isl_vec *obj;
391 enum isl_lp_result res;
392 isl_int num, den;
393 unsigned dim;
395 if (!set)
396 return NULL;
397 ctx = set->ctx;
398 set = isl_set_copy(set);
399 set = isl_set_set_rational(set);
401 dim = 1 + isl_set_n_dim(set);
402 T = isl_mat_alloc(ctx, 3, dim);
403 if (!T)
404 goto error;
405 isl_int_set_si(T->row[0][0], 1);
406 isl_seq_clr(T->row[0]+1, dim - 1);
407 isl_seq_cpy(T->row[1], facet, dim);
408 isl_seq_cpy(T->row[2], ridge, dim);
409 T = isl_mat_right_inverse(T);
410 set = isl_set_preimage(set, T);
411 T = NULL;
412 if (!set)
413 goto error;
414 lp = wrap_constraints(set);
415 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
416 if (!obj)
417 goto error;
418 isl_int_set_si(obj->block.data[0], 0);
419 for (i = 0; i < set->n; ++i) {
420 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
421 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
422 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
424 isl_int_init(num);
425 isl_int_init(den);
426 res = isl_basic_set_solve_lp(lp, 0,
427 obj->block.data, ctx->one, &num, &den, NULL);
428 if (res == isl_lp_ok) {
429 isl_int_neg(num, num);
430 isl_seq_combine(facet, num, facet, den, ridge, dim);
431 isl_seq_normalize(ctx, facet, dim);
433 isl_int_clear(num);
434 isl_int_clear(den);
435 isl_vec_free(obj);
436 isl_basic_set_free(lp);
437 isl_set_free(set);
438 if (res == isl_lp_error)
439 return NULL;
440 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
441 return NULL);
442 return facet;
443 error:
444 isl_basic_set_free(lp);
445 isl_mat_free(T);
446 isl_set_free(set);
447 return NULL;
450 /* Compute the constraint of a facet of "set".
452 * We first compute the intersection with a bounding constraint
453 * that is orthogonal to one of the coordinate axes.
454 * If the affine hull of this intersection has only one equality,
455 * we have found a facet.
456 * Otherwise, we wrap the current bounding constraint around
457 * one of the equalities of the face (one that is not equal to
458 * the current bounding constraint).
459 * This process continues until we have found a facet.
460 * The dimension of the intersection increases by at least
461 * one on each iteration, so termination is guaranteed.
463 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
465 struct isl_set *slice = NULL;
466 struct isl_basic_set *face = NULL;
467 int i;
468 unsigned dim = isl_set_n_dim(set);
469 int is_bound;
470 isl_mat *bounds;
472 isl_assert(set->ctx, set->n > 0, goto error);
473 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
474 if (!bounds)
475 return NULL;
477 isl_seq_clr(bounds->row[0], dim);
478 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
479 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
480 if (is_bound < 0)
481 goto error;
482 isl_assert(set->ctx, is_bound, goto error);
483 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
484 bounds->n_row = 1;
486 for (;;) {
487 slice = isl_set_copy(set);
488 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
489 face = isl_set_affine_hull(slice);
490 if (!face)
491 goto error;
492 if (face->n_eq == 1) {
493 isl_basic_set_free(face);
494 break;
496 for (i = 0; i < face->n_eq; ++i)
497 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
498 !isl_seq_is_neg(bounds->row[0],
499 face->eq[i], 1 + dim))
500 break;
501 isl_assert(set->ctx, i < face->n_eq, goto error);
502 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
503 goto error;
504 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
505 isl_basic_set_free(face);
508 return bounds;
509 error:
510 isl_basic_set_free(face);
511 isl_mat_free(bounds);
512 return NULL;
515 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
516 * compute a hyperplane description of the facet, i.e., compute the facets
517 * of the facet.
519 * We compute an affine transformation that transforms the constraint
521 * [ 1 ]
522 * c [ x ] = 0
524 * to the constraint
526 * z_1 = 0
528 * by computing the right inverse U of a matrix that starts with the rows
530 * [ 1 0 ]
531 * [ c ]
533 * Then
534 * [ 1 ] [ 1 ]
535 * [ x ] = U [ z ]
536 * and
537 * [ 1 ] [ 1 ]
538 * [ z ] = Q [ x ]
540 * with Q = U^{-1}
541 * Since z_1 is zero, we can drop this variable as well as the corresponding
542 * column of U to obtain
544 * [ 1 ] [ 1 ]
545 * [ x ] = U' [ z' ]
546 * and
547 * [ 1 ] [ 1 ]
548 * [ z' ] = Q' [ x ]
550 * with Q' equal to Q, but without the corresponding row.
551 * After computing the facets of the facet in the z' space,
552 * we convert them back to the x space through Q.
554 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
556 struct isl_mat *m, *U, *Q;
557 struct isl_basic_set *facet = NULL;
558 struct isl_ctx *ctx;
559 unsigned dim;
561 ctx = set->ctx;
562 set = isl_set_copy(set);
563 dim = isl_set_n_dim(set);
564 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
565 if (!m)
566 goto error;
567 isl_int_set_si(m->row[0][0], 1);
568 isl_seq_clr(m->row[0]+1, dim);
569 isl_seq_cpy(m->row[1], c, 1+dim);
570 U = isl_mat_right_inverse(m);
571 Q = isl_mat_right_inverse(isl_mat_copy(U));
572 U = isl_mat_drop_cols(U, 1, 1);
573 Q = isl_mat_drop_rows(Q, 1, 1);
574 set = isl_set_preimage(set, U);
575 facet = uset_convex_hull_wrap_bounded(set);
576 facet = isl_basic_set_preimage(facet, Q);
577 if (facet)
578 isl_assert(ctx, facet->n_eq == 0, goto error);
579 return facet;
580 error:
581 isl_basic_set_free(facet);
582 isl_set_free(set);
583 return NULL;
586 /* Given an initial facet constraint, compute the remaining facets.
587 * We do this by running through all facets found so far and computing
588 * the adjacent facets through wrapping, adding those facets that we
589 * hadn't already found before.
591 * For each facet we have found so far, we first compute its facets
592 * in the resulting convex hull. That is, we compute the ridges
593 * of the resulting convex hull contained in the facet.
594 * We also compute the corresponding facet in the current approximation
595 * of the convex hull. There is no need to wrap around the ridges
596 * in this facet since that would result in a facet that is already
597 * present in the current approximation.
599 * This function can still be significantly optimized by checking which of
600 * the facets of the basic sets are also facets of the convex hull and
601 * using all the facets so far to help in constructing the facets of the
602 * facets
603 * and/or
604 * using the technique in section "3.1 Ridge Generation" of
605 * "Extended Convex Hull" by Fukuda et al.
607 static struct isl_basic_set *extend(struct isl_basic_set *hull,
608 struct isl_set *set)
610 int i, j, f;
611 int k;
612 struct isl_basic_set *facet = NULL;
613 struct isl_basic_set *hull_facet = NULL;
614 unsigned dim;
616 if (!hull)
617 return NULL;
619 isl_assert(set->ctx, set->n > 0, goto error);
621 dim = isl_set_n_dim(set);
623 for (i = 0; i < hull->n_ineq; ++i) {
624 facet = compute_facet(set, hull->ineq[i]);
625 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
626 facet = isl_basic_set_gauss(facet, NULL);
627 facet = isl_basic_set_normalize_constraints(facet);
628 hull_facet = isl_basic_set_copy(hull);
629 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
630 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
631 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
632 if (!facet || !hull_facet)
633 goto error;
634 hull = isl_basic_set_cow(hull);
635 hull = isl_basic_set_extend_dim(hull,
636 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
637 if (!hull)
638 goto error;
639 for (j = 0; j < facet->n_ineq; ++j) {
640 for (f = 0; f < hull_facet->n_ineq; ++f)
641 if (isl_seq_eq(facet->ineq[j],
642 hull_facet->ineq[f], 1 + dim))
643 break;
644 if (f < hull_facet->n_ineq)
645 continue;
646 k = isl_basic_set_alloc_inequality(hull);
647 if (k < 0)
648 goto error;
649 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
650 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
651 goto error;
653 isl_basic_set_free(hull_facet);
654 isl_basic_set_free(facet);
656 hull = isl_basic_set_simplify(hull);
657 hull = isl_basic_set_finalize(hull);
658 return hull;
659 error:
660 isl_basic_set_free(hull_facet);
661 isl_basic_set_free(facet);
662 isl_basic_set_free(hull);
663 return NULL;
666 /* Special case for computing the convex hull of a one dimensional set.
667 * We simply collect the lower and upper bounds of each basic set
668 * and the biggest of those.
670 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
672 struct isl_mat *c = NULL;
673 isl_int *lower = NULL;
674 isl_int *upper = NULL;
675 int i, j, k;
676 isl_int a, b;
677 struct isl_basic_set *hull;
679 for (i = 0; i < set->n; ++i) {
680 set->p[i] = isl_basic_set_simplify(set->p[i]);
681 if (!set->p[i])
682 goto error;
684 set = isl_set_remove_empty_parts(set);
685 if (!set)
686 goto error;
687 isl_assert(set->ctx, set->n > 0, goto error);
688 c = isl_mat_alloc(set->ctx, 2, 2);
689 if (!c)
690 goto error;
692 if (set->p[0]->n_eq > 0) {
693 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
694 lower = c->row[0];
695 upper = c->row[1];
696 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
697 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
698 isl_seq_neg(upper, set->p[0]->eq[0], 2);
699 } else {
700 isl_seq_neg(lower, set->p[0]->eq[0], 2);
701 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
703 } else {
704 for (j = 0; j < set->p[0]->n_ineq; ++j) {
705 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
706 lower = c->row[0];
707 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
708 } else {
709 upper = c->row[1];
710 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
715 isl_int_init(a);
716 isl_int_init(b);
717 for (i = 0; i < set->n; ++i) {
718 struct isl_basic_set *bset = set->p[i];
719 int has_lower = 0;
720 int has_upper = 0;
722 for (j = 0; j < bset->n_eq; ++j) {
723 has_lower = 1;
724 has_upper = 1;
725 if (lower) {
726 isl_int_mul(a, lower[0], bset->eq[j][1]);
727 isl_int_mul(b, lower[1], bset->eq[j][0]);
728 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
729 isl_seq_cpy(lower, bset->eq[j], 2);
730 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
731 isl_seq_neg(lower, bset->eq[j], 2);
733 if (upper) {
734 isl_int_mul(a, upper[0], bset->eq[j][1]);
735 isl_int_mul(b, upper[1], bset->eq[j][0]);
736 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
737 isl_seq_neg(upper, bset->eq[j], 2);
738 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
739 isl_seq_cpy(upper, bset->eq[j], 2);
742 for (j = 0; j < bset->n_ineq; ++j) {
743 if (isl_int_is_pos(bset->ineq[j][1]))
744 has_lower = 1;
745 if (isl_int_is_neg(bset->ineq[j][1]))
746 has_upper = 1;
747 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
748 isl_int_mul(a, lower[0], bset->ineq[j][1]);
749 isl_int_mul(b, lower[1], bset->ineq[j][0]);
750 if (isl_int_lt(a, b))
751 isl_seq_cpy(lower, bset->ineq[j], 2);
753 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
754 isl_int_mul(a, upper[0], bset->ineq[j][1]);
755 isl_int_mul(b, upper[1], bset->ineq[j][0]);
756 if (isl_int_gt(a, b))
757 isl_seq_cpy(upper, bset->ineq[j], 2);
760 if (!has_lower)
761 lower = NULL;
762 if (!has_upper)
763 upper = NULL;
765 isl_int_clear(a);
766 isl_int_clear(b);
768 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
769 hull = isl_basic_set_set_rational(hull);
770 if (!hull)
771 goto error;
772 if (lower) {
773 k = isl_basic_set_alloc_inequality(hull);
774 isl_seq_cpy(hull->ineq[k], lower, 2);
776 if (upper) {
777 k = isl_basic_set_alloc_inequality(hull);
778 isl_seq_cpy(hull->ineq[k], upper, 2);
780 hull = isl_basic_set_finalize(hull);
781 isl_set_free(set);
782 isl_mat_free(c);
783 return hull;
784 error:
785 isl_set_free(set);
786 isl_mat_free(c);
787 return NULL;
790 /* Project out final n dimensions using Fourier-Motzkin */
791 static struct isl_set *set_project_out(struct isl_ctx *ctx,
792 struct isl_set *set, unsigned n)
794 return isl_set_remove_dims(set, isl_dim_set, isl_set_n_dim(set) - n, n);
797 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
799 struct isl_basic_set *convex_hull;
801 if (!set)
802 return NULL;
804 if (isl_set_is_empty(set))
805 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
806 else
807 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
808 isl_set_free(set);
809 return convex_hull;
812 /* Compute the convex hull of a pair of basic sets without any parameters or
813 * integer divisions using Fourier-Motzkin elimination.
814 * The convex hull is the set of all points that can be written as
815 * the sum of points from both basic sets (in homogeneous coordinates).
816 * We set up the constraints in a space with dimensions for each of
817 * the three sets and then project out the dimensions corresponding
818 * to the two original basic sets, retaining only those corresponding
819 * to the convex hull.
821 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
822 struct isl_basic_set *bset2)
824 int i, j, k;
825 struct isl_basic_set *bset[2];
826 struct isl_basic_set *hull = NULL;
827 unsigned dim;
829 if (!bset1 || !bset2)
830 goto error;
832 dim = isl_basic_set_n_dim(bset1);
833 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
834 1 + dim + bset1->n_eq + bset2->n_eq,
835 2 + bset1->n_ineq + bset2->n_ineq);
836 bset[0] = bset1;
837 bset[1] = bset2;
838 for (i = 0; i < 2; ++i) {
839 for (j = 0; j < bset[i]->n_eq; ++j) {
840 k = isl_basic_set_alloc_equality(hull);
841 if (k < 0)
842 goto error;
843 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
844 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
845 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
846 1+dim);
848 for (j = 0; j < bset[i]->n_ineq; ++j) {
849 k = isl_basic_set_alloc_inequality(hull);
850 if (k < 0)
851 goto error;
852 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
853 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
854 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
855 bset[i]->ineq[j], 1+dim);
857 k = isl_basic_set_alloc_inequality(hull);
858 if (k < 0)
859 goto error;
860 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
861 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
863 for (j = 0; j < 1+dim; ++j) {
864 k = isl_basic_set_alloc_equality(hull);
865 if (k < 0)
866 goto error;
867 isl_seq_clr(hull->eq[k], 1+2+3*dim);
868 isl_int_set_si(hull->eq[k][j], -1);
869 isl_int_set_si(hull->eq[k][1+dim+j], 1);
870 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
872 hull = isl_basic_set_set_rational(hull);
873 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
874 hull = isl_basic_set_remove_redundancies(hull);
875 isl_basic_set_free(bset1);
876 isl_basic_set_free(bset2);
877 return hull;
878 error:
879 isl_basic_set_free(bset1);
880 isl_basic_set_free(bset2);
881 isl_basic_set_free(hull);
882 return NULL;
885 /* Is the set bounded for each value of the parameters?
887 int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
889 struct isl_tab *tab;
890 int bounded;
892 if (!bset)
893 return -1;
894 if (isl_basic_set_fast_is_empty(bset))
895 return 1;
897 tab = isl_tab_from_recession_cone(bset, 1);
898 bounded = isl_tab_cone_is_bounded(tab);
899 isl_tab_free(tab);
900 return bounded;
903 /* Is the image bounded for each value of the parameters and
904 * the domain variables?
906 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
908 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
909 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
910 int bounded;
912 bmap = isl_basic_map_copy(bmap);
913 bmap = isl_basic_map_cow(bmap);
914 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
915 isl_dim_in, 0, n_in);
916 bounded = isl_basic_set_is_bounded((isl_basic_set *)bmap);
917 isl_basic_map_free(bmap);
919 return bounded;
922 /* Is the set bounded for each value of the parameters?
924 int isl_set_is_bounded(__isl_keep isl_set *set)
926 int i;
928 if (!set)
929 return -1;
931 for (i = 0; i < set->n; ++i) {
932 int bounded = isl_basic_set_is_bounded(set->p[i]);
933 if (!bounded || bounded < 0)
934 return bounded;
936 return 1;
939 /* Compute the lineality space of the convex hull of bset1 and bset2.
941 * We first compute the intersection of the recession cone of bset1
942 * with the negative of the recession cone of bset2 and then compute
943 * the linear hull of the resulting cone.
945 static struct isl_basic_set *induced_lineality_space(
946 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
948 int i, k;
949 struct isl_basic_set *lin = NULL;
950 unsigned dim;
952 if (!bset1 || !bset2)
953 goto error;
955 dim = isl_basic_set_total_dim(bset1);
956 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1), 0,
957 bset1->n_eq + bset2->n_eq,
958 bset1->n_ineq + bset2->n_ineq);
959 lin = isl_basic_set_set_rational(lin);
960 if (!lin)
961 goto error;
962 for (i = 0; i < bset1->n_eq; ++i) {
963 k = isl_basic_set_alloc_equality(lin);
964 if (k < 0)
965 goto error;
966 isl_int_set_si(lin->eq[k][0], 0);
967 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
969 for (i = 0; i < bset1->n_ineq; ++i) {
970 k = isl_basic_set_alloc_inequality(lin);
971 if (k < 0)
972 goto error;
973 isl_int_set_si(lin->ineq[k][0], 0);
974 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
976 for (i = 0; i < bset2->n_eq; ++i) {
977 k = isl_basic_set_alloc_equality(lin);
978 if (k < 0)
979 goto error;
980 isl_int_set_si(lin->eq[k][0], 0);
981 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
983 for (i = 0; i < bset2->n_ineq; ++i) {
984 k = isl_basic_set_alloc_inequality(lin);
985 if (k < 0)
986 goto error;
987 isl_int_set_si(lin->ineq[k][0], 0);
988 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
991 isl_basic_set_free(bset1);
992 isl_basic_set_free(bset2);
993 return isl_basic_set_affine_hull(lin);
994 error:
995 isl_basic_set_free(lin);
996 isl_basic_set_free(bset1);
997 isl_basic_set_free(bset2);
998 return NULL;
1001 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1003 /* Given a set and a linear space "lin" of dimension n > 0,
1004 * project the linear space from the set, compute the convex hull
1005 * and then map the set back to the original space.
1007 * Let
1009 * M x = 0
1011 * describe the linear space. We first compute the Hermite normal
1012 * form H = M U of M = H Q, to obtain
1014 * H Q x = 0
1016 * The last n rows of H will be zero, so the last n variables of x' = Q x
1017 * are the one we want to project out. We do this by transforming each
1018 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1019 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1020 * we transform the hull back to the original space as A' Q_1 x >= b',
1021 * with Q_1 all but the last n rows of Q.
1023 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1024 struct isl_basic_set *lin)
1026 unsigned total = isl_basic_set_total_dim(lin);
1027 unsigned lin_dim;
1028 struct isl_basic_set *hull;
1029 struct isl_mat *M, *U, *Q;
1031 if (!set || !lin)
1032 goto error;
1033 lin_dim = total - lin->n_eq;
1034 M = isl_mat_sub_alloc(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1035 M = isl_mat_left_hermite(M, 0, &U, &Q);
1036 if (!M)
1037 goto error;
1038 isl_mat_free(M);
1039 isl_basic_set_free(lin);
1041 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1043 U = isl_mat_lin_to_aff(U);
1044 Q = isl_mat_lin_to_aff(Q);
1046 set = isl_set_preimage(set, U);
1047 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1048 hull = uset_convex_hull(set);
1049 hull = isl_basic_set_preimage(hull, Q);
1051 return hull;
1052 error:
1053 isl_basic_set_free(lin);
1054 isl_set_free(set);
1055 return NULL;
1058 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1059 * set up an LP for solving
1061 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1063 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1064 * The next \alpha{ij} correspond to the equalities and come in pairs.
1065 * The final \alpha{ij} correspond to the inequalities.
1067 static struct isl_basic_set *valid_direction_lp(
1068 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1070 struct isl_dim *dim;
1071 struct isl_basic_set *lp;
1072 unsigned d;
1073 int n;
1074 int i, j, k;
1076 if (!bset1 || !bset2)
1077 goto error;
1078 d = 1 + isl_basic_set_total_dim(bset1);
1079 n = 2 +
1080 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1081 dim = isl_dim_set_alloc(bset1->ctx, 0, n);
1082 lp = isl_basic_set_alloc_dim(dim, 0, d, n);
1083 if (!lp)
1084 goto error;
1085 for (i = 0; i < n; ++i) {
1086 k = isl_basic_set_alloc_inequality(lp);
1087 if (k < 0)
1088 goto error;
1089 isl_seq_clr(lp->ineq[k] + 1, n);
1090 isl_int_set_si(lp->ineq[k][0], -1);
1091 isl_int_set_si(lp->ineq[k][1 + i], 1);
1093 for (i = 0; i < d; ++i) {
1094 k = isl_basic_set_alloc_equality(lp);
1095 if (k < 0)
1096 goto error;
1097 n = 0;
1098 isl_int_set_si(lp->eq[k][n], 0); n++;
1099 /* positivity constraint 1 >= 0 */
1100 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1101 for (j = 0; j < bset1->n_eq; ++j) {
1102 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1103 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1105 for (j = 0; j < bset1->n_ineq; ++j) {
1106 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1108 /* positivity constraint 1 >= 0 */
1109 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1110 for (j = 0; j < bset2->n_eq; ++j) {
1111 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1112 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1114 for (j = 0; j < bset2->n_ineq; ++j) {
1115 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1118 lp = isl_basic_set_gauss(lp, NULL);
1119 isl_basic_set_free(bset1);
1120 isl_basic_set_free(bset2);
1121 return lp;
1122 error:
1123 isl_basic_set_free(bset1);
1124 isl_basic_set_free(bset2);
1125 return NULL;
1128 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1129 * for all rays in the homogeneous space of the two cones that correspond
1130 * to the input polyhedra bset1 and bset2.
1132 * We compute s as a vector that satisfies
1134 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1136 * with h_{ij} the normals of the facets of polyhedron i
1137 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1138 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1139 * We first set up an LP with as variables the \alpha{ij}.
1140 * In this formulation, for each polyhedron i,
1141 * the first constraint is the positivity constraint, followed by pairs
1142 * of variables for the equalities, followed by variables for the inequalities.
1143 * We then simply pick a feasible solution and compute s using (*).
1145 * Note that we simply pick any valid direction and make no attempt
1146 * to pick a "good" or even the "best" valid direction.
1148 static struct isl_vec *valid_direction(
1149 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1151 struct isl_basic_set *lp;
1152 struct isl_tab *tab;
1153 struct isl_vec *sample = NULL;
1154 struct isl_vec *dir;
1155 unsigned d;
1156 int i;
1157 int n;
1159 if (!bset1 || !bset2)
1160 goto error;
1161 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1162 isl_basic_set_copy(bset2));
1163 tab = isl_tab_from_basic_set(lp);
1164 sample = isl_tab_get_sample_value(tab);
1165 isl_tab_free(tab);
1166 isl_basic_set_free(lp);
1167 if (!sample)
1168 goto error;
1169 d = isl_basic_set_total_dim(bset1);
1170 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1171 if (!dir)
1172 goto error;
1173 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1174 n = 1;
1175 /* positivity constraint 1 >= 0 */
1176 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1177 for (i = 0; i < bset1->n_eq; ++i) {
1178 isl_int_sub(sample->block.data[n],
1179 sample->block.data[n], sample->block.data[n+1]);
1180 isl_seq_combine(dir->block.data,
1181 bset1->ctx->one, dir->block.data,
1182 sample->block.data[n], bset1->eq[i], 1 + d);
1184 n += 2;
1186 for (i = 0; i < bset1->n_ineq; ++i)
1187 isl_seq_combine(dir->block.data,
1188 bset1->ctx->one, dir->block.data,
1189 sample->block.data[n++], bset1->ineq[i], 1 + d);
1190 isl_vec_free(sample);
1191 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1192 isl_basic_set_free(bset1);
1193 isl_basic_set_free(bset2);
1194 return dir;
1195 error:
1196 isl_vec_free(sample);
1197 isl_basic_set_free(bset1);
1198 isl_basic_set_free(bset2);
1199 return NULL;
1202 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1203 * compute b_i' + A_i' x' >= 0, with
1205 * [ b_i A_i ] [ y' ] [ y' ]
1206 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1208 * In particular, add the "positivity constraint" and then perform
1209 * the mapping.
1211 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1212 struct isl_mat *T)
1214 int k;
1216 if (!bset)
1217 goto error;
1218 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1219 k = isl_basic_set_alloc_inequality(bset);
1220 if (k < 0)
1221 goto error;
1222 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1223 isl_int_set_si(bset->ineq[k][0], 1);
1224 bset = isl_basic_set_preimage(bset, T);
1225 return bset;
1226 error:
1227 isl_mat_free(T);
1228 isl_basic_set_free(bset);
1229 return NULL;
1232 /* Compute the convex hull of a pair of basic sets without any parameters or
1233 * integer divisions, where the convex hull is known to be pointed,
1234 * but the basic sets may be unbounded.
1236 * We turn this problem into the computation of a convex hull of a pair
1237 * _bounded_ polyhedra by "changing the direction of the homogeneous
1238 * dimension". This idea is due to Matthias Koeppe.
1240 * Consider the cones in homogeneous space that correspond to the
1241 * input polyhedra. The rays of these cones are also rays of the
1242 * polyhedra if the coordinate that corresponds to the homogeneous
1243 * dimension is zero. That is, if the inner product of the rays
1244 * with the homogeneous direction is zero.
1245 * The cones in the homogeneous space can also be considered to
1246 * correspond to other pairs of polyhedra by chosing a different
1247 * homogeneous direction. To ensure that both of these polyhedra
1248 * are bounded, we need to make sure that all rays of the cones
1249 * correspond to vertices and not to rays.
1250 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1251 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1252 * The vector s is computed in valid_direction.
1254 * Note that we need to consider _all_ rays of the cones and not just
1255 * the rays that correspond to rays in the polyhedra. If we were to
1256 * only consider those rays and turn them into vertices, then we
1257 * may inadvertently turn some vertices into rays.
1259 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1260 * We therefore transform the two polyhedra such that the selected
1261 * direction is mapped onto this standard direction and then proceed
1262 * with the normal computation.
1263 * Let S be a non-singular square matrix with s as its first row,
1264 * then we want to map the polyhedra to the space
1266 * [ y' ] [ y ] [ y ] [ y' ]
1267 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1269 * We take S to be the unimodular completion of s to limit the growth
1270 * of the coefficients in the following computations.
1272 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1273 * We first move to the homogeneous dimension
1275 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1276 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1278 * Then we change directoin
1280 * [ b_i A_i ] [ y' ] [ y' ]
1281 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1283 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1284 * resulting in b' + A' x' >= 0, which we then convert back
1286 * [ y ] [ y ]
1287 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1289 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1291 static struct isl_basic_set *convex_hull_pair_pointed(
1292 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1294 struct isl_ctx *ctx = NULL;
1295 struct isl_vec *dir = NULL;
1296 struct isl_mat *T = NULL;
1297 struct isl_mat *T2 = NULL;
1298 struct isl_basic_set *hull;
1299 struct isl_set *set;
1301 if (!bset1 || !bset2)
1302 goto error;
1303 ctx = bset1->ctx;
1304 dir = valid_direction(isl_basic_set_copy(bset1),
1305 isl_basic_set_copy(bset2));
1306 if (!dir)
1307 goto error;
1308 T = isl_mat_alloc(bset1->ctx, dir->size, dir->size);
1309 if (!T)
1310 goto error;
1311 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1312 T = isl_mat_unimodular_complete(T, 1);
1313 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1315 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1316 bset2 = homogeneous_map(bset2, T2);
1317 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1318 set = isl_set_add_basic_set(set, bset1);
1319 set = isl_set_add_basic_set(set, bset2);
1320 hull = uset_convex_hull(set);
1321 hull = isl_basic_set_preimage(hull, T);
1323 isl_vec_free(dir);
1325 return hull;
1326 error:
1327 isl_vec_free(dir);
1328 isl_basic_set_free(bset1);
1329 isl_basic_set_free(bset2);
1330 return NULL;
1333 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1334 static struct isl_basic_set *modulo_affine_hull(
1335 struct isl_set *set, struct isl_basic_set *affine_hull);
1337 /* Compute the convex hull of a pair of basic sets without any parameters or
1338 * integer divisions.
1340 * This function is called from uset_convex_hull_unbounded, which
1341 * means that the complete convex hull is unbounded. Some pairs
1342 * of basic sets may still be bounded, though.
1343 * They may even lie inside a lower dimensional space, in which
1344 * case they need to be handled inside their affine hull since
1345 * the main algorithm assumes that the result is full-dimensional.
1347 * If the convex hull of the two basic sets would have a non-trivial
1348 * lineality space, we first project out this lineality space.
1350 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1351 struct isl_basic_set *bset2)
1353 isl_basic_set *lin, *aff;
1354 int bounded1, bounded2;
1356 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1357 return convex_hull_pair_elim(bset1, bset2);
1359 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1360 isl_basic_set_copy(bset2)));
1361 if (!aff)
1362 goto error;
1363 if (aff->n_eq != 0)
1364 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1365 isl_basic_set_free(aff);
1367 bounded1 = isl_basic_set_is_bounded(bset1);
1368 bounded2 = isl_basic_set_is_bounded(bset2);
1370 if (bounded1 < 0 || bounded2 < 0)
1371 goto error;
1373 if (bounded1 && bounded2)
1374 uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1376 if (bounded1 || bounded2)
1377 return convex_hull_pair_pointed(bset1, bset2);
1379 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1380 isl_basic_set_copy(bset2));
1381 if (!lin)
1382 goto error;
1383 if (isl_basic_set_is_universe(lin)) {
1384 isl_basic_set_free(bset1);
1385 isl_basic_set_free(bset2);
1386 return lin;
1388 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1389 struct isl_set *set;
1390 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1391 set = isl_set_add_basic_set(set, bset1);
1392 set = isl_set_add_basic_set(set, bset2);
1393 return modulo_lineality(set, lin);
1395 isl_basic_set_free(lin);
1397 return convex_hull_pair_pointed(bset1, bset2);
1398 error:
1399 isl_basic_set_free(bset1);
1400 isl_basic_set_free(bset2);
1401 return NULL;
1404 /* Compute the lineality space of a basic set.
1405 * We currently do not allow the basic set to have any divs.
1406 * We basically just drop the constants and turn every inequality
1407 * into an equality.
1409 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1411 int i, k;
1412 struct isl_basic_set *lin = NULL;
1413 unsigned dim;
1415 if (!bset)
1416 goto error;
1417 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1418 dim = isl_basic_set_total_dim(bset);
1420 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset), 0, dim, 0);
1421 if (!lin)
1422 goto error;
1423 for (i = 0; i < bset->n_eq; ++i) {
1424 k = isl_basic_set_alloc_equality(lin);
1425 if (k < 0)
1426 goto error;
1427 isl_int_set_si(lin->eq[k][0], 0);
1428 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1430 lin = isl_basic_set_gauss(lin, NULL);
1431 if (!lin)
1432 goto error;
1433 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1434 k = isl_basic_set_alloc_equality(lin);
1435 if (k < 0)
1436 goto error;
1437 isl_int_set_si(lin->eq[k][0], 0);
1438 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1439 lin = isl_basic_set_gauss(lin, NULL);
1440 if (!lin)
1441 goto error;
1443 isl_basic_set_free(bset);
1444 return lin;
1445 error:
1446 isl_basic_set_free(lin);
1447 isl_basic_set_free(bset);
1448 return NULL;
1451 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1452 * "underlying" set "set".
1454 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1456 int i;
1457 struct isl_set *lin = NULL;
1459 if (!set)
1460 return NULL;
1461 if (set->n == 0) {
1462 struct isl_dim *dim = isl_set_get_dim(set);
1463 isl_set_free(set);
1464 return isl_basic_set_empty(dim);
1467 lin = isl_set_alloc_dim(isl_set_get_dim(set), set->n, 0);
1468 for (i = 0; i < set->n; ++i)
1469 lin = isl_set_add_basic_set(lin,
1470 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1471 isl_set_free(set);
1472 return isl_set_affine_hull(lin);
1475 /* Compute the convex hull of a set without any parameters or
1476 * integer divisions.
1477 * In each step, we combined two basic sets until only one
1478 * basic set is left.
1479 * The input basic sets are assumed not to have a non-trivial
1480 * lineality space. If any of the intermediate results has
1481 * a non-trivial lineality space, it is projected out.
1483 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1485 struct isl_basic_set *convex_hull = NULL;
1487 convex_hull = isl_set_copy_basic_set(set);
1488 set = isl_set_drop_basic_set(set, convex_hull);
1489 if (!set)
1490 goto error;
1491 while (set->n > 0) {
1492 struct isl_basic_set *t;
1493 t = isl_set_copy_basic_set(set);
1494 if (!t)
1495 goto error;
1496 set = isl_set_drop_basic_set(set, t);
1497 if (!set)
1498 goto error;
1499 convex_hull = convex_hull_pair(convex_hull, t);
1500 if (set->n == 0)
1501 break;
1502 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1503 if (!t)
1504 goto error;
1505 if (isl_basic_set_is_universe(t)) {
1506 isl_basic_set_free(convex_hull);
1507 convex_hull = t;
1508 break;
1510 if (t->n_eq < isl_basic_set_total_dim(t)) {
1511 set = isl_set_add_basic_set(set, convex_hull);
1512 return modulo_lineality(set, t);
1514 isl_basic_set_free(t);
1516 isl_set_free(set);
1517 return convex_hull;
1518 error:
1519 isl_set_free(set);
1520 isl_basic_set_free(convex_hull);
1521 return NULL;
1524 /* Compute an initial hull for wrapping containing a single initial
1525 * facet.
1526 * This function assumes that the given set is bounded.
1528 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1529 struct isl_set *set)
1531 struct isl_mat *bounds = NULL;
1532 unsigned dim;
1533 int k;
1535 if (!hull)
1536 goto error;
1537 bounds = initial_facet_constraint(set);
1538 if (!bounds)
1539 goto error;
1540 k = isl_basic_set_alloc_inequality(hull);
1541 if (k < 0)
1542 goto error;
1543 dim = isl_set_n_dim(set);
1544 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1545 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1546 isl_mat_free(bounds);
1548 return hull;
1549 error:
1550 isl_basic_set_free(hull);
1551 isl_mat_free(bounds);
1552 return NULL;
1555 struct max_constraint {
1556 struct isl_mat *c;
1557 int count;
1558 int ineq;
1561 static int max_constraint_equal(const void *entry, const void *val)
1563 struct max_constraint *a = (struct max_constraint *)entry;
1564 isl_int *b = (isl_int *)val;
1566 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1569 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1570 isl_int *con, unsigned len, int n, int ineq)
1572 struct isl_hash_table_entry *entry;
1573 struct max_constraint *c;
1574 uint32_t c_hash;
1576 c_hash = isl_seq_get_hash(con + 1, len);
1577 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1578 con + 1, 0);
1579 if (!entry)
1580 return;
1581 c = entry->data;
1582 if (c->count < n) {
1583 isl_hash_table_remove(ctx, table, entry);
1584 return;
1586 c->count++;
1587 if (isl_int_gt(c->c->row[0][0], con[0]))
1588 return;
1589 if (isl_int_eq(c->c->row[0][0], con[0])) {
1590 if (ineq)
1591 c->ineq = ineq;
1592 return;
1594 c->c = isl_mat_cow(c->c);
1595 isl_int_set(c->c->row[0][0], con[0]);
1596 c->ineq = ineq;
1599 /* Check whether the constraint hash table "table" constains the constraint
1600 * "con".
1602 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1603 isl_int *con, unsigned len, int n)
1605 struct isl_hash_table_entry *entry;
1606 struct max_constraint *c;
1607 uint32_t c_hash;
1609 c_hash = isl_seq_get_hash(con + 1, len);
1610 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1611 con + 1, 0);
1612 if (!entry)
1613 return 0;
1614 c = entry->data;
1615 if (c->count < n)
1616 return 0;
1617 return isl_int_eq(c->c->row[0][0], con[0]);
1620 /* Check for inequality constraints of a basic set without equalities
1621 * such that the same or more stringent copies of the constraint appear
1622 * in all of the basic sets. Such constraints are necessarily facet
1623 * constraints of the convex hull.
1625 * If the resulting basic set is by chance identical to one of
1626 * the basic sets in "set", then we know that this basic set contains
1627 * all other basic sets and is therefore the convex hull of set.
1628 * In this case we set *is_hull to 1.
1630 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1631 struct isl_set *set, int *is_hull)
1633 int i, j, s, n;
1634 int min_constraints;
1635 int best;
1636 struct max_constraint *constraints = NULL;
1637 struct isl_hash_table *table = NULL;
1638 unsigned total;
1640 *is_hull = 0;
1642 for (i = 0; i < set->n; ++i)
1643 if (set->p[i]->n_eq == 0)
1644 break;
1645 if (i >= set->n)
1646 return hull;
1647 min_constraints = set->p[i]->n_ineq;
1648 best = i;
1649 for (i = best + 1; i < set->n; ++i) {
1650 if (set->p[i]->n_eq != 0)
1651 continue;
1652 if (set->p[i]->n_ineq >= min_constraints)
1653 continue;
1654 min_constraints = set->p[i]->n_ineq;
1655 best = i;
1657 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1658 min_constraints);
1659 if (!constraints)
1660 return hull;
1661 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1662 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1663 goto error;
1665 total = isl_dim_total(set->dim);
1666 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1667 constraints[i].c = isl_mat_sub_alloc(hull->ctx,
1668 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1669 if (!constraints[i].c)
1670 goto error;
1671 constraints[i].ineq = 1;
1673 for (i = 0; i < min_constraints; ++i) {
1674 struct isl_hash_table_entry *entry;
1675 uint32_t c_hash;
1676 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1677 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1678 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1679 if (!entry)
1680 goto error;
1681 isl_assert(hull->ctx, !entry->data, goto error);
1682 entry->data = &constraints[i];
1685 n = 0;
1686 for (s = 0; s < set->n; ++s) {
1687 if (s == best)
1688 continue;
1690 for (i = 0; i < set->p[s]->n_eq; ++i) {
1691 isl_int *eq = set->p[s]->eq[i];
1692 for (j = 0; j < 2; ++j) {
1693 isl_seq_neg(eq, eq, 1 + total);
1694 update_constraint(hull->ctx, table,
1695 eq, total, n, 0);
1698 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1699 isl_int *ineq = set->p[s]->ineq[i];
1700 update_constraint(hull->ctx, table, ineq, total, n,
1701 set->p[s]->n_eq == 0);
1703 ++n;
1706 for (i = 0; i < min_constraints; ++i) {
1707 if (constraints[i].count < n)
1708 continue;
1709 if (!constraints[i].ineq)
1710 continue;
1711 j = isl_basic_set_alloc_inequality(hull);
1712 if (j < 0)
1713 goto error;
1714 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1717 for (s = 0; s < set->n; ++s) {
1718 if (set->p[s]->n_eq)
1719 continue;
1720 if (set->p[s]->n_ineq != hull->n_ineq)
1721 continue;
1722 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1723 isl_int *ineq = set->p[s]->ineq[i];
1724 if (!has_constraint(hull->ctx, table, ineq, total, n))
1725 break;
1727 if (i == set->p[s]->n_ineq)
1728 *is_hull = 1;
1731 isl_hash_table_clear(table);
1732 for (i = 0; i < min_constraints; ++i)
1733 isl_mat_free(constraints[i].c);
1734 free(constraints);
1735 free(table);
1736 return hull;
1737 error:
1738 isl_hash_table_clear(table);
1739 free(table);
1740 if (constraints)
1741 for (i = 0; i < min_constraints; ++i)
1742 isl_mat_free(constraints[i].c);
1743 free(constraints);
1744 return hull;
1747 /* Create a template for the convex hull of "set" and fill it up
1748 * obvious facet constraints, if any. If the result happens to
1749 * be the convex hull of "set" then *is_hull is set to 1.
1751 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1753 struct isl_basic_set *hull;
1754 unsigned n_ineq;
1755 int i;
1757 n_ineq = 1;
1758 for (i = 0; i < set->n; ++i) {
1759 n_ineq += set->p[i]->n_eq;
1760 n_ineq += set->p[i]->n_ineq;
1762 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1763 hull = isl_basic_set_set_rational(hull);
1764 if (!hull)
1765 return NULL;
1766 return common_constraints(hull, set, is_hull);
1769 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1771 struct isl_basic_set *hull;
1772 int is_hull;
1774 hull = proto_hull(set, &is_hull);
1775 if (hull && !is_hull) {
1776 if (hull->n_ineq == 0)
1777 hull = initial_hull(hull, set);
1778 hull = extend(hull, set);
1780 isl_set_free(set);
1782 return hull;
1785 /* Compute the convex hull of a set without any parameters or
1786 * integer divisions. Depending on whether the set is bounded,
1787 * we pass control to the wrapping based convex hull or
1788 * the Fourier-Motzkin elimination based convex hull.
1789 * We also handle a few special cases before checking the boundedness.
1791 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1793 struct isl_basic_set *convex_hull = NULL;
1794 struct isl_basic_set *lin;
1796 if (isl_set_n_dim(set) == 0)
1797 return convex_hull_0d(set);
1799 set = isl_set_coalesce(set);
1800 set = isl_set_set_rational(set);
1802 if (!set)
1803 goto error;
1804 if (!set)
1805 return NULL;
1806 if (set->n == 1) {
1807 convex_hull = isl_basic_set_copy(set->p[0]);
1808 isl_set_free(set);
1809 return convex_hull;
1811 if (isl_set_n_dim(set) == 1)
1812 return convex_hull_1d(set);
1814 if (isl_set_is_bounded(set) &&
1815 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1816 return uset_convex_hull_wrap(set);
1818 lin = uset_combined_lineality_space(isl_set_copy(set));
1819 if (!lin)
1820 goto error;
1821 if (isl_basic_set_is_universe(lin)) {
1822 isl_set_free(set);
1823 return lin;
1825 if (lin->n_eq < isl_basic_set_total_dim(lin))
1826 return modulo_lineality(set, lin);
1827 isl_basic_set_free(lin);
1829 return uset_convex_hull_unbounded(set);
1830 error:
1831 isl_set_free(set);
1832 isl_basic_set_free(convex_hull);
1833 return NULL;
1836 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1837 * without parameters or divs and where the convex hull of set is
1838 * known to be full-dimensional.
1840 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1842 struct isl_basic_set *convex_hull = NULL;
1844 if (!set)
1845 goto error;
1847 if (isl_set_n_dim(set) == 0) {
1848 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1849 isl_set_free(set);
1850 convex_hull = isl_basic_set_set_rational(convex_hull);
1851 return convex_hull;
1854 set = isl_set_set_rational(set);
1855 set = isl_set_coalesce(set);
1856 if (!set)
1857 goto error;
1858 if (set->n == 1) {
1859 convex_hull = isl_basic_set_copy(set->p[0]);
1860 isl_set_free(set);
1861 return convex_hull;
1863 if (isl_set_n_dim(set) == 1)
1864 return convex_hull_1d(set);
1866 return uset_convex_hull_wrap(set);
1867 error:
1868 isl_set_free(set);
1869 return NULL;
1872 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1873 * We first remove the equalities (transforming the set), compute the
1874 * convex hull of the transformed set and then add the equalities back
1875 * (after performing the inverse transformation.
1877 static struct isl_basic_set *modulo_affine_hull(
1878 struct isl_set *set, struct isl_basic_set *affine_hull)
1880 struct isl_mat *T;
1881 struct isl_mat *T2;
1882 struct isl_basic_set *dummy;
1883 struct isl_basic_set *convex_hull;
1885 dummy = isl_basic_set_remove_equalities(
1886 isl_basic_set_copy(affine_hull), &T, &T2);
1887 if (!dummy)
1888 goto error;
1889 isl_basic_set_free(dummy);
1890 set = isl_set_preimage(set, T);
1891 convex_hull = uset_convex_hull(set);
1892 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1893 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1894 return convex_hull;
1895 error:
1896 isl_basic_set_free(affine_hull);
1897 isl_set_free(set);
1898 return NULL;
1901 /* Compute the convex hull of a map.
1903 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1904 * specifically, the wrapping of facets to obtain new facets.
1906 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1908 struct isl_basic_set *bset;
1909 struct isl_basic_map *model = NULL;
1910 struct isl_basic_set *affine_hull = NULL;
1911 struct isl_basic_map *convex_hull = NULL;
1912 struct isl_set *set = NULL;
1913 struct isl_ctx *ctx;
1915 if (!map)
1916 goto error;
1918 ctx = map->ctx;
1919 if (map->n == 0) {
1920 convex_hull = isl_basic_map_empty_like_map(map);
1921 isl_map_free(map);
1922 return convex_hull;
1925 map = isl_map_detect_equalities(map);
1926 map = isl_map_align_divs(map);
1927 if (!map)
1928 goto error;
1929 model = isl_basic_map_copy(map->p[0]);
1930 set = isl_map_underlying_set(map);
1931 if (!set)
1932 goto error;
1934 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1935 if (!affine_hull)
1936 goto error;
1937 if (affine_hull->n_eq != 0)
1938 bset = modulo_affine_hull(set, affine_hull);
1939 else {
1940 isl_basic_set_free(affine_hull);
1941 bset = uset_convex_hull(set);
1944 convex_hull = isl_basic_map_overlying_set(bset, model);
1945 if (!convex_hull)
1946 return NULL;
1948 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1949 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1950 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1951 return convex_hull;
1952 error:
1953 isl_set_free(set);
1954 isl_basic_map_free(model);
1955 return NULL;
1958 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1960 return (struct isl_basic_set *)
1961 isl_map_convex_hull((struct isl_map *)set);
1964 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1966 isl_basic_map *hull;
1968 hull = isl_map_convex_hull(map);
1969 return isl_basic_map_remove_divs(hull);
1972 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1974 return (isl_basic_set *)isl_map_polyhedral_hull((isl_map *)set);
1977 struct sh_data_entry {
1978 struct isl_hash_table *table;
1979 struct isl_tab *tab;
1982 /* Holds the data needed during the simple hull computation.
1983 * In particular,
1984 * n the number of basic sets in the original set
1985 * hull_table a hash table of already computed constraints
1986 * in the simple hull
1987 * p for each basic set,
1988 * table a hash table of the constraints
1989 * tab the tableau corresponding to the basic set
1991 struct sh_data {
1992 struct isl_ctx *ctx;
1993 unsigned n;
1994 struct isl_hash_table *hull_table;
1995 struct sh_data_entry p[1];
1998 static void sh_data_free(struct sh_data *data)
2000 int i;
2002 if (!data)
2003 return;
2004 isl_hash_table_free(data->ctx, data->hull_table);
2005 for (i = 0; i < data->n; ++i) {
2006 isl_hash_table_free(data->ctx, data->p[i].table);
2007 isl_tab_free(data->p[i].tab);
2009 free(data);
2012 struct ineq_cmp_data {
2013 unsigned len;
2014 isl_int *p;
2017 static int has_ineq(const void *entry, const void *val)
2019 isl_int *row = (isl_int *)entry;
2020 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2022 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2023 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2026 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2027 isl_int *ineq, unsigned len)
2029 uint32_t c_hash;
2030 struct ineq_cmp_data v;
2031 struct isl_hash_table_entry *entry;
2033 v.len = len;
2034 v.p = ineq;
2035 c_hash = isl_seq_get_hash(ineq + 1, len);
2036 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2037 if (!entry)
2038 return - 1;
2039 entry->data = ineq;
2040 return 0;
2043 /* Fill hash table "table" with the constraints of "bset".
2044 * Equalities are added as two inequalities.
2045 * The value in the hash table is a pointer to the (in)equality of "bset".
2047 static int hash_basic_set(struct isl_hash_table *table,
2048 struct isl_basic_set *bset)
2050 int i, j;
2051 unsigned dim = isl_basic_set_total_dim(bset);
2053 for (i = 0; i < bset->n_eq; ++i) {
2054 for (j = 0; j < 2; ++j) {
2055 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2056 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2057 return -1;
2060 for (i = 0; i < bset->n_ineq; ++i) {
2061 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2062 return -1;
2064 return 0;
2067 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2069 struct sh_data *data;
2070 int i;
2072 data = isl_calloc(set->ctx, struct sh_data,
2073 sizeof(struct sh_data) +
2074 (set->n - 1) * sizeof(struct sh_data_entry));
2075 if (!data)
2076 return NULL;
2077 data->ctx = set->ctx;
2078 data->n = set->n;
2079 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2080 if (!data->hull_table)
2081 goto error;
2082 for (i = 0; i < set->n; ++i) {
2083 data->p[i].table = isl_hash_table_alloc(set->ctx,
2084 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2085 if (!data->p[i].table)
2086 goto error;
2087 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2088 goto error;
2090 return data;
2091 error:
2092 sh_data_free(data);
2093 return NULL;
2096 /* Check if inequality "ineq" is a bound for basic set "j" or if
2097 * it can be relaxed (by increasing the constant term) to become
2098 * a bound for that basic set. In the latter case, the constant
2099 * term is updated.
2100 * Return 1 if "ineq" is a bound
2101 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2102 * -1 if some error occurred
2104 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2105 isl_int *ineq)
2107 enum isl_lp_result res;
2108 isl_int opt;
2110 if (!data->p[j].tab) {
2111 data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
2112 if (!data->p[j].tab)
2113 return -1;
2116 isl_int_init(opt);
2118 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2119 &opt, NULL, 0);
2120 if (res == isl_lp_ok && isl_int_is_neg(opt))
2121 isl_int_sub(ineq[0], ineq[0], opt);
2123 isl_int_clear(opt);
2125 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2126 res == isl_lp_unbounded ? 0 : -1;
2129 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2130 * become a bound on the whole set. If so, add the (relaxed) inequality
2131 * to "hull".
2133 * We first check if "hull" already contains a translate of the inequality.
2134 * If so, we are done.
2135 * Then, we check if any of the previous basic sets contains a translate
2136 * of the inequality. If so, then we have already considered this
2137 * inequality and we are done.
2138 * Otherwise, for each basic set other than "i", we check if the inequality
2139 * is a bound on the basic set.
2140 * For previous basic sets, we know that they do not contain a translate
2141 * of the inequality, so we directly call is_bound.
2142 * For following basic sets, we first check if a translate of the
2143 * inequality appears in its description and if so directly update
2144 * the inequality accordingly.
2146 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2147 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
2149 uint32_t c_hash;
2150 struct ineq_cmp_data v;
2151 struct isl_hash_table_entry *entry;
2152 int j, k;
2154 if (!hull)
2155 return NULL;
2157 v.len = isl_basic_set_total_dim(hull);
2158 v.p = ineq;
2159 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2161 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2162 has_ineq, &v, 0);
2163 if (entry)
2164 return hull;
2166 for (j = 0; j < i; ++j) {
2167 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2168 c_hash, has_ineq, &v, 0);
2169 if (entry)
2170 break;
2172 if (j < i)
2173 return hull;
2175 k = isl_basic_set_alloc_inequality(hull);
2176 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2177 if (k < 0)
2178 goto error;
2180 for (j = 0; j < i; ++j) {
2181 int bound;
2182 bound = is_bound(data, set, j, hull->ineq[k]);
2183 if (bound < 0)
2184 goto error;
2185 if (!bound)
2186 break;
2188 if (j < i) {
2189 isl_basic_set_free_inequality(hull, 1);
2190 return hull;
2193 for (j = i + 1; j < set->n; ++j) {
2194 int bound, neg;
2195 isl_int *ineq_j;
2196 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2197 c_hash, has_ineq, &v, 0);
2198 if (entry) {
2199 ineq_j = entry->data;
2200 neg = isl_seq_is_neg(ineq_j + 1,
2201 hull->ineq[k] + 1, v.len);
2202 if (neg)
2203 isl_int_neg(ineq_j[0], ineq_j[0]);
2204 if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
2205 isl_int_set(hull->ineq[k][0], ineq_j[0]);
2206 if (neg)
2207 isl_int_neg(ineq_j[0], ineq_j[0]);
2208 continue;
2210 bound = is_bound(data, set, j, hull->ineq[k]);
2211 if (bound < 0)
2212 goto error;
2213 if (!bound)
2214 break;
2216 if (j < set->n) {
2217 isl_basic_set_free_inequality(hull, 1);
2218 return hull;
2221 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2222 has_ineq, &v, 1);
2223 if (!entry)
2224 goto error;
2225 entry->data = hull->ineq[k];
2227 return hull;
2228 error:
2229 isl_basic_set_free(hull);
2230 return NULL;
2233 /* Check if any inequality from basic set "i" can be relaxed to
2234 * become a bound on the whole set. If so, add the (relaxed) inequality
2235 * to "hull".
2237 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2238 struct sh_data *data, struct isl_set *set, int i)
2240 int j, k;
2241 unsigned dim = isl_basic_set_total_dim(bset);
2243 for (j = 0; j < set->p[i]->n_eq; ++j) {
2244 for (k = 0; k < 2; ++k) {
2245 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2246 bset = add_bound(bset, data, set, i, set->p[i]->eq[j]);
2249 for (j = 0; j < set->p[i]->n_ineq; ++j)
2250 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j]);
2251 return bset;
2254 /* Compute a superset of the convex hull of set that is described
2255 * by only translates of the constraints in the constituents of set.
2257 static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
2259 struct sh_data *data = NULL;
2260 struct isl_basic_set *hull = NULL;
2261 unsigned n_ineq;
2262 int i;
2264 if (!set)
2265 return NULL;
2267 n_ineq = 0;
2268 for (i = 0; i < set->n; ++i) {
2269 if (!set->p[i])
2270 goto error;
2271 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2274 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
2275 if (!hull)
2276 goto error;
2278 data = sh_data_alloc(set, n_ineq);
2279 if (!data)
2280 goto error;
2282 for (i = 0; i < set->n; ++i)
2283 hull = add_bounds(hull, data, set, i);
2285 sh_data_free(data);
2286 isl_set_free(set);
2288 return hull;
2289 error:
2290 sh_data_free(data);
2291 isl_basic_set_free(hull);
2292 isl_set_free(set);
2293 return NULL;
2296 /* Compute a superset of the convex hull of map that is described
2297 * by only translates of the constraints in the constituents of map.
2299 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
2301 struct isl_set *set = NULL;
2302 struct isl_basic_map *model = NULL;
2303 struct isl_basic_map *hull;
2304 struct isl_basic_map *affine_hull;
2305 struct isl_basic_set *bset = NULL;
2307 if (!map)
2308 return NULL;
2309 if (map->n == 0) {
2310 hull = isl_basic_map_empty_like_map(map);
2311 isl_map_free(map);
2312 return hull;
2314 if (map->n == 1) {
2315 hull = isl_basic_map_copy(map->p[0]);
2316 isl_map_free(map);
2317 return hull;
2320 map = isl_map_detect_equalities(map);
2321 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2322 map = isl_map_align_divs(map);
2323 model = isl_basic_map_copy(map->p[0]);
2325 set = isl_map_underlying_set(map);
2327 bset = uset_simple_hull(set);
2329 hull = isl_basic_map_overlying_set(bset, model);
2331 hull = isl_basic_map_intersect(hull, affine_hull);
2332 hull = isl_basic_map_remove_redundancies(hull);
2333 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2334 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2336 return hull;
2339 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2341 return (struct isl_basic_set *)
2342 isl_map_simple_hull((struct isl_map *)set);
2345 /* Given a set "set", return parametric bounds on the dimension "dim".
2347 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2349 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2350 set = isl_set_copy(set);
2351 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2352 set = isl_set_eliminate_dims(set, 0, dim);
2353 return isl_set_convex_hull(set);
2356 /* Computes a "simple hull" and then check if each dimension in the
2357 * resulting hull is bounded by a symbolic constant. If not, the
2358 * hull is intersected with the corresponding bounds on the whole set.
2360 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
2362 int i, j;
2363 struct isl_basic_set *hull;
2364 unsigned nparam, left;
2365 int removed_divs = 0;
2367 hull = isl_set_simple_hull(isl_set_copy(set));
2368 if (!hull)
2369 goto error;
2371 nparam = isl_basic_set_dim(hull, isl_dim_param);
2372 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
2373 int lower = 0, upper = 0;
2374 struct isl_basic_set *bounds;
2376 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
2377 for (j = 0; j < hull->n_eq; ++j) {
2378 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
2379 continue;
2380 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
2381 left) == -1)
2382 break;
2384 if (j < hull->n_eq)
2385 continue;
2387 for (j = 0; j < hull->n_ineq; ++j) {
2388 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
2389 continue;
2390 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
2391 left) != -1 ||
2392 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
2393 i) != -1)
2394 continue;
2395 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
2396 lower = 1;
2397 else
2398 upper = 1;
2399 if (lower && upper)
2400 break;
2403 if (lower && upper)
2404 continue;
2406 if (!removed_divs) {
2407 set = isl_set_remove_divs(set);
2408 if (!set)
2409 goto error;
2410 removed_divs = 1;
2412 bounds = set_bounds(set, i);
2413 hull = isl_basic_set_intersect(hull, bounds);
2414 if (!hull)
2415 goto error;
2418 isl_set_free(set);
2419 return hull;
2420 error:
2421 isl_set_free(set);
2422 return NULL;