hide internal isl_basic_map functions
[isl.git] / isl_convex_hull.c
blobb5ca0d9d53e1674b3953ada8d6785c9f6bd51462
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_lp_private.h>
16 #include <isl/map.h>
17 #include <isl_mat_private.h>
18 #include <isl_vec_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_options_private.h>
22 #include "isl_equalities.h"
23 #include "isl_tab.h"
24 #include <isl_sort.h>
26 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
28 /* Return 1 if constraint c is redundant with respect to the constraints
29 * in bmap. If c is a lower [upper] bound in some variable and bmap
30 * does not have a lower [upper] bound in that variable, then c cannot
31 * be redundant and we do not need solve any lp.
33 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
34 isl_int *c, isl_int *opt_n, isl_int *opt_d)
36 enum isl_lp_result res;
37 unsigned total;
38 int i, j;
40 if (!bmap)
41 return -1;
43 total = isl_basic_map_total_dim(*bmap);
44 for (i = 0; i < total; ++i) {
45 int sign;
46 if (isl_int_is_zero(c[1+i]))
47 continue;
48 sign = isl_int_sgn(c[1+i]);
49 for (j = 0; j < (*bmap)->n_ineq; ++j)
50 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
51 break;
52 if (j == (*bmap)->n_ineq)
53 break;
55 if (i < total)
56 return 0;
58 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
59 opt_n, opt_d, NULL);
60 if (res == isl_lp_unbounded)
61 return 0;
62 if (res == isl_lp_error)
63 return -1;
64 if (res == isl_lp_empty) {
65 *bmap = isl_basic_map_set_to_empty(*bmap);
66 return 0;
68 return !isl_int_is_neg(*opt_n);
71 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
72 isl_int *c, isl_int *opt_n, isl_int *opt_d)
74 return isl_basic_map_constraint_is_redundant(
75 (struct isl_basic_map **)bset, c, opt_n, opt_d);
78 /* Remove redundant
79 * constraints. If the minimal value along the normal of a constraint
80 * is the same if the constraint is removed, then the constraint is redundant.
82 * Alternatively, we could have intersected the basic map with the
83 * corresponding equality and the checked if the dimension was that
84 * of a facet.
86 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
87 __isl_take isl_basic_map *bmap)
89 struct isl_tab *tab;
91 if (!bmap)
92 return NULL;
94 bmap = isl_basic_map_gauss(bmap, NULL);
95 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
96 return bmap;
97 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
98 return bmap;
99 if (bmap->n_ineq <= 1)
100 return bmap;
102 tab = isl_tab_from_basic_map(bmap, 0);
103 if (isl_tab_detect_implicit_equalities(tab) < 0)
104 goto error;
105 if (isl_tab_detect_redundant(tab) < 0)
106 goto error;
107 bmap = isl_basic_map_update_from_tab(bmap, tab);
108 isl_tab_free(tab);
109 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
110 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
111 return bmap;
112 error:
113 isl_tab_free(tab);
114 isl_basic_map_free(bmap);
115 return NULL;
118 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
119 __isl_take isl_basic_set *bset)
121 return (struct isl_basic_set *)
122 isl_basic_map_remove_redundancies((struct isl_basic_map *)bset);
125 /* Remove redundant constraints in each of the basic maps.
127 __isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
129 return isl_map_inline_foreach_basic_map(map,
130 &isl_basic_map_remove_redundancies);
133 __isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
135 return isl_map_remove_redundancies(set);
138 /* Check if the set set is bound in the direction of the affine
139 * constraint c and if so, set the constant term such that the
140 * resulting constraint is a bounding constraint for the set.
142 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
144 int first;
145 int j;
146 isl_int opt;
147 isl_int opt_denom;
149 isl_int_init(opt);
150 isl_int_init(opt_denom);
151 first = 1;
152 for (j = 0; j < set->n; ++j) {
153 enum isl_lp_result res;
155 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
156 continue;
158 res = isl_basic_set_solve_lp(set->p[j],
159 0, c, set->ctx->one, &opt, &opt_denom, NULL);
160 if (res == isl_lp_unbounded)
161 break;
162 if (res == isl_lp_error)
163 goto error;
164 if (res == isl_lp_empty) {
165 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
166 if (!set->p[j])
167 goto error;
168 continue;
170 if (first || isl_int_is_neg(opt)) {
171 if (!isl_int_is_one(opt_denom))
172 isl_seq_scale(c, c, opt_denom, len);
173 isl_int_sub(c[0], c[0], opt);
175 first = 0;
177 isl_int_clear(opt);
178 isl_int_clear(opt_denom);
179 return j >= set->n;
180 error:
181 isl_int_clear(opt);
182 isl_int_clear(opt_denom);
183 return -1;
186 __isl_give isl_basic_map *isl_basic_map_set_rational(
187 __isl_take isl_basic_set *bmap)
189 if (!bmap)
190 return NULL;
192 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
193 return bmap;
195 bmap = isl_basic_map_cow(bmap);
196 if (!bmap)
197 return NULL;
199 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
201 return isl_basic_map_finalize(bmap);
204 __isl_give isl_basic_set *isl_basic_set_set_rational(
205 __isl_take isl_basic_set *bset)
207 return isl_basic_map_set_rational(bset);
210 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
212 int i;
214 map = isl_map_cow(map);
215 if (!map)
216 return NULL;
217 for (i = 0; i < map->n; ++i) {
218 map->p[i] = isl_basic_map_set_rational(map->p[i]);
219 if (!map->p[i])
220 goto error;
222 return map;
223 error:
224 isl_map_free(map);
225 return NULL;
228 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
230 return isl_map_set_rational(set);
233 static struct isl_basic_set *isl_basic_set_add_equality(
234 struct isl_basic_set *bset, isl_int *c)
236 int i;
237 unsigned dim;
239 if (!bset)
240 return NULL;
242 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
243 return bset;
245 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
246 isl_assert(bset->ctx, bset->n_div == 0, goto error);
247 dim = isl_basic_set_n_dim(bset);
248 bset = isl_basic_set_cow(bset);
249 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
250 i = isl_basic_set_alloc_equality(bset);
251 if (i < 0)
252 goto error;
253 isl_seq_cpy(bset->eq[i], c, 1 + dim);
254 return bset;
255 error:
256 isl_basic_set_free(bset);
257 return NULL;
260 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
262 int i;
264 set = isl_set_cow(set);
265 if (!set)
266 return NULL;
267 for (i = 0; i < set->n; ++i) {
268 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
269 if (!set->p[i])
270 goto error;
272 return set;
273 error:
274 isl_set_free(set);
275 return NULL;
278 /* Given a union of basic sets, construct the constraints for wrapping
279 * a facet around one of its ridges.
280 * In particular, if each of n the d-dimensional basic sets i in "set"
281 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
282 * and is defined by the constraints
283 * [ 1 ]
284 * A_i [ x ] >= 0
286 * then the resulting set is of dimension n*(1+d) and has as constraints
288 * [ a_i ]
289 * A_i [ x_i ] >= 0
291 * a_i >= 0
293 * \sum_i x_{i,1} = 1
295 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
297 struct isl_basic_set *lp;
298 unsigned n_eq;
299 unsigned n_ineq;
300 int i, j, k;
301 unsigned dim, lp_dim;
303 if (!set)
304 return NULL;
306 dim = 1 + isl_set_n_dim(set);
307 n_eq = 1;
308 n_ineq = set->n;
309 for (i = 0; i < set->n; ++i) {
310 n_eq += set->p[i]->n_eq;
311 n_ineq += set->p[i]->n_ineq;
313 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
314 lp = isl_basic_set_set_rational(lp);
315 if (!lp)
316 return NULL;
317 lp_dim = isl_basic_set_n_dim(lp);
318 k = isl_basic_set_alloc_equality(lp);
319 isl_int_set_si(lp->eq[k][0], -1);
320 for (i = 0; i < set->n; ++i) {
321 isl_int_set_si(lp->eq[k][1+dim*i], 0);
322 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
323 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
325 for (i = 0; i < set->n; ++i) {
326 k = isl_basic_set_alloc_inequality(lp);
327 isl_seq_clr(lp->ineq[k], 1+lp_dim);
328 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
330 for (j = 0; j < set->p[i]->n_eq; ++j) {
331 k = isl_basic_set_alloc_equality(lp);
332 isl_seq_clr(lp->eq[k], 1+dim*i);
333 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
334 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
337 for (j = 0; j < set->p[i]->n_ineq; ++j) {
338 k = isl_basic_set_alloc_inequality(lp);
339 isl_seq_clr(lp->ineq[k], 1+dim*i);
340 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
341 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
344 return lp;
347 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
348 * of that facet, compute the other facet of the convex hull that contains
349 * the ridge.
351 * We first transform the set such that the facet constraint becomes
353 * x_1 >= 0
355 * I.e., the facet lies in
357 * x_1 = 0
359 * and on that facet, the constraint that defines the ridge is
361 * x_2 >= 0
363 * (This transformation is not strictly needed, all that is needed is
364 * that the ridge contains the origin.)
366 * Since the ridge contains the origin, the cone of the convex hull
367 * will be of the form
369 * x_1 >= 0
370 * x_2 >= a x_1
372 * with this second constraint defining the new facet.
373 * The constant a is obtained by settting x_1 in the cone of the
374 * convex hull to 1 and minimizing x_2.
375 * Now, each element in the cone of the convex hull is the sum
376 * of elements in the cones of the basic sets.
377 * If a_i is the dilation factor of basic set i, then the problem
378 * we need to solve is
380 * min \sum_i x_{i,2}
381 * st
382 * \sum_i x_{i,1} = 1
383 * a_i >= 0
384 * [ a_i ]
385 * A [ x_i ] >= 0
387 * with
388 * [ 1 ]
389 * A_i [ x_i ] >= 0
391 * the constraints of each (transformed) basic set.
392 * If a = n/d, then the constraint defining the new facet (in the transformed
393 * space) is
395 * -n x_1 + d x_2 >= 0
397 * In the original space, we need to take the same combination of the
398 * corresponding constraints "facet" and "ridge".
400 * If a = -infty = "-1/0", then we just return the original facet constraint.
401 * This means that the facet is unbounded, but has a bounded intersection
402 * with the union of sets.
404 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
405 isl_int *facet, isl_int *ridge)
407 int i;
408 isl_ctx *ctx;
409 struct isl_mat *T = NULL;
410 struct isl_basic_set *lp = NULL;
411 struct isl_vec *obj;
412 enum isl_lp_result res;
413 isl_int num, den;
414 unsigned dim;
416 if (!set)
417 return NULL;
418 ctx = set->ctx;
419 set = isl_set_copy(set);
420 set = isl_set_set_rational(set);
422 dim = 1 + isl_set_n_dim(set);
423 T = isl_mat_alloc(ctx, 3, dim);
424 if (!T)
425 goto error;
426 isl_int_set_si(T->row[0][0], 1);
427 isl_seq_clr(T->row[0]+1, dim - 1);
428 isl_seq_cpy(T->row[1], facet, dim);
429 isl_seq_cpy(T->row[2], ridge, dim);
430 T = isl_mat_right_inverse(T);
431 set = isl_set_preimage(set, T);
432 T = NULL;
433 if (!set)
434 goto error;
435 lp = wrap_constraints(set);
436 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
437 if (!obj)
438 goto error;
439 isl_int_set_si(obj->block.data[0], 0);
440 for (i = 0; i < set->n; ++i) {
441 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
442 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
443 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
445 isl_int_init(num);
446 isl_int_init(den);
447 res = isl_basic_set_solve_lp(lp, 0,
448 obj->block.data, ctx->one, &num, &den, NULL);
449 if (res == isl_lp_ok) {
450 isl_int_neg(num, num);
451 isl_seq_combine(facet, num, facet, den, ridge, dim);
452 isl_seq_normalize(ctx, facet, dim);
454 isl_int_clear(num);
455 isl_int_clear(den);
456 isl_vec_free(obj);
457 isl_basic_set_free(lp);
458 isl_set_free(set);
459 if (res == isl_lp_error)
460 return NULL;
461 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
462 return NULL);
463 return facet;
464 error:
465 isl_basic_set_free(lp);
466 isl_mat_free(T);
467 isl_set_free(set);
468 return NULL;
471 /* Compute the constraint of a facet of "set".
473 * We first compute the intersection with a bounding constraint
474 * that is orthogonal to one of the coordinate axes.
475 * If the affine hull of this intersection has only one equality,
476 * we have found a facet.
477 * Otherwise, we wrap the current bounding constraint around
478 * one of the equalities of the face (one that is not equal to
479 * the current bounding constraint).
480 * This process continues until we have found a facet.
481 * The dimension of the intersection increases by at least
482 * one on each iteration, so termination is guaranteed.
484 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
486 struct isl_set *slice = NULL;
487 struct isl_basic_set *face = NULL;
488 int i;
489 unsigned dim = isl_set_n_dim(set);
490 int is_bound;
491 isl_mat *bounds = NULL;
493 isl_assert(set->ctx, set->n > 0, goto error);
494 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
495 if (!bounds)
496 return NULL;
498 isl_seq_clr(bounds->row[0], dim);
499 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
500 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
501 if (is_bound < 0)
502 goto error;
503 isl_assert(set->ctx, is_bound, goto error);
504 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
505 bounds->n_row = 1;
507 for (;;) {
508 slice = isl_set_copy(set);
509 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
510 face = isl_set_affine_hull(slice);
511 if (!face)
512 goto error;
513 if (face->n_eq == 1) {
514 isl_basic_set_free(face);
515 break;
517 for (i = 0; i < face->n_eq; ++i)
518 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
519 !isl_seq_is_neg(bounds->row[0],
520 face->eq[i], 1 + dim))
521 break;
522 isl_assert(set->ctx, i < face->n_eq, goto error);
523 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
524 goto error;
525 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
526 isl_basic_set_free(face);
529 return bounds;
530 error:
531 isl_basic_set_free(face);
532 isl_mat_free(bounds);
533 return NULL;
536 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
537 * compute a hyperplane description of the facet, i.e., compute the facets
538 * of the facet.
540 * We compute an affine transformation that transforms the constraint
542 * [ 1 ]
543 * c [ x ] = 0
545 * to the constraint
547 * z_1 = 0
549 * by computing the right inverse U of a matrix that starts with the rows
551 * [ 1 0 ]
552 * [ c ]
554 * Then
555 * [ 1 ] [ 1 ]
556 * [ x ] = U [ z ]
557 * and
558 * [ 1 ] [ 1 ]
559 * [ z ] = Q [ x ]
561 * with Q = U^{-1}
562 * Since z_1 is zero, we can drop this variable as well as the corresponding
563 * column of U to obtain
565 * [ 1 ] [ 1 ]
566 * [ x ] = U' [ z' ]
567 * and
568 * [ 1 ] [ 1 ]
569 * [ z' ] = Q' [ x ]
571 * with Q' equal to Q, but without the corresponding row.
572 * After computing the facets of the facet in the z' space,
573 * we convert them back to the x space through Q.
575 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
577 struct isl_mat *m, *U, *Q;
578 struct isl_basic_set *facet = NULL;
579 struct isl_ctx *ctx;
580 unsigned dim;
582 ctx = set->ctx;
583 set = isl_set_copy(set);
584 dim = isl_set_n_dim(set);
585 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
586 if (!m)
587 goto error;
588 isl_int_set_si(m->row[0][0], 1);
589 isl_seq_clr(m->row[0]+1, dim);
590 isl_seq_cpy(m->row[1], c, 1+dim);
591 U = isl_mat_right_inverse(m);
592 Q = isl_mat_right_inverse(isl_mat_copy(U));
593 U = isl_mat_drop_cols(U, 1, 1);
594 Q = isl_mat_drop_rows(Q, 1, 1);
595 set = isl_set_preimage(set, U);
596 facet = uset_convex_hull_wrap_bounded(set);
597 facet = isl_basic_set_preimage(facet, Q);
598 if (facet && facet->n_eq != 0)
599 isl_die(ctx, isl_error_internal, "unexpected equality",
600 return isl_basic_set_free(facet));
601 return facet;
602 error:
603 isl_basic_set_free(facet);
604 isl_set_free(set);
605 return NULL;
608 /* Given an initial facet constraint, compute the remaining facets.
609 * We do this by running through all facets found so far and computing
610 * the adjacent facets through wrapping, adding those facets that we
611 * hadn't already found before.
613 * For each facet we have found so far, we first compute its facets
614 * in the resulting convex hull. That is, we compute the ridges
615 * of the resulting convex hull contained in the facet.
616 * We also compute the corresponding facet in the current approximation
617 * of the convex hull. There is no need to wrap around the ridges
618 * in this facet since that would result in a facet that is already
619 * present in the current approximation.
621 * This function can still be significantly optimized by checking which of
622 * the facets of the basic sets are also facets of the convex hull and
623 * using all the facets so far to help in constructing the facets of the
624 * facets
625 * and/or
626 * using the technique in section "3.1 Ridge Generation" of
627 * "Extended Convex Hull" by Fukuda et al.
629 static struct isl_basic_set *extend(struct isl_basic_set *hull,
630 struct isl_set *set)
632 int i, j, f;
633 int k;
634 struct isl_basic_set *facet = NULL;
635 struct isl_basic_set *hull_facet = NULL;
636 unsigned dim;
638 if (!hull)
639 return NULL;
641 isl_assert(set->ctx, set->n > 0, goto error);
643 dim = isl_set_n_dim(set);
645 for (i = 0; i < hull->n_ineq; ++i) {
646 facet = compute_facet(set, hull->ineq[i]);
647 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
648 facet = isl_basic_set_gauss(facet, NULL);
649 facet = isl_basic_set_normalize_constraints(facet);
650 hull_facet = isl_basic_set_copy(hull);
651 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
652 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
653 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
654 if (!facet || !hull_facet)
655 goto error;
656 hull = isl_basic_set_cow(hull);
657 hull = isl_basic_set_extend_space(hull,
658 isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
659 if (!hull)
660 goto error;
661 for (j = 0; j < facet->n_ineq; ++j) {
662 for (f = 0; f < hull_facet->n_ineq; ++f)
663 if (isl_seq_eq(facet->ineq[j],
664 hull_facet->ineq[f], 1 + dim))
665 break;
666 if (f < hull_facet->n_ineq)
667 continue;
668 k = isl_basic_set_alloc_inequality(hull);
669 if (k < 0)
670 goto error;
671 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
672 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
673 goto error;
675 isl_basic_set_free(hull_facet);
676 isl_basic_set_free(facet);
678 hull = isl_basic_set_simplify(hull);
679 hull = isl_basic_set_finalize(hull);
680 return hull;
681 error:
682 isl_basic_set_free(hull_facet);
683 isl_basic_set_free(facet);
684 isl_basic_set_free(hull);
685 return NULL;
688 /* Special case for computing the convex hull of a one dimensional set.
689 * We simply collect the lower and upper bounds of each basic set
690 * and the biggest of those.
692 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
694 struct isl_mat *c = NULL;
695 isl_int *lower = NULL;
696 isl_int *upper = NULL;
697 int i, j, k;
698 isl_int a, b;
699 struct isl_basic_set *hull;
701 for (i = 0; i < set->n; ++i) {
702 set->p[i] = isl_basic_set_simplify(set->p[i]);
703 if (!set->p[i])
704 goto error;
706 set = isl_set_remove_empty_parts(set);
707 if (!set)
708 goto error;
709 isl_assert(set->ctx, set->n > 0, goto error);
710 c = isl_mat_alloc(set->ctx, 2, 2);
711 if (!c)
712 goto error;
714 if (set->p[0]->n_eq > 0) {
715 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
716 lower = c->row[0];
717 upper = c->row[1];
718 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
719 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
720 isl_seq_neg(upper, set->p[0]->eq[0], 2);
721 } else {
722 isl_seq_neg(lower, set->p[0]->eq[0], 2);
723 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
725 } else {
726 for (j = 0; j < set->p[0]->n_ineq; ++j) {
727 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
728 lower = c->row[0];
729 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
730 } else {
731 upper = c->row[1];
732 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
737 isl_int_init(a);
738 isl_int_init(b);
739 for (i = 0; i < set->n; ++i) {
740 struct isl_basic_set *bset = set->p[i];
741 int has_lower = 0;
742 int has_upper = 0;
744 for (j = 0; j < bset->n_eq; ++j) {
745 has_lower = 1;
746 has_upper = 1;
747 if (lower) {
748 isl_int_mul(a, lower[0], bset->eq[j][1]);
749 isl_int_mul(b, lower[1], bset->eq[j][0]);
750 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
751 isl_seq_cpy(lower, bset->eq[j], 2);
752 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
753 isl_seq_neg(lower, bset->eq[j], 2);
755 if (upper) {
756 isl_int_mul(a, upper[0], bset->eq[j][1]);
757 isl_int_mul(b, upper[1], bset->eq[j][0]);
758 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
759 isl_seq_neg(upper, bset->eq[j], 2);
760 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
761 isl_seq_cpy(upper, bset->eq[j], 2);
764 for (j = 0; j < bset->n_ineq; ++j) {
765 if (isl_int_is_pos(bset->ineq[j][1]))
766 has_lower = 1;
767 if (isl_int_is_neg(bset->ineq[j][1]))
768 has_upper = 1;
769 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
770 isl_int_mul(a, lower[0], bset->ineq[j][1]);
771 isl_int_mul(b, lower[1], bset->ineq[j][0]);
772 if (isl_int_lt(a, b))
773 isl_seq_cpy(lower, bset->ineq[j], 2);
775 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
776 isl_int_mul(a, upper[0], bset->ineq[j][1]);
777 isl_int_mul(b, upper[1], bset->ineq[j][0]);
778 if (isl_int_gt(a, b))
779 isl_seq_cpy(upper, bset->ineq[j], 2);
782 if (!has_lower)
783 lower = NULL;
784 if (!has_upper)
785 upper = NULL;
787 isl_int_clear(a);
788 isl_int_clear(b);
790 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
791 hull = isl_basic_set_set_rational(hull);
792 if (!hull)
793 goto error;
794 if (lower) {
795 k = isl_basic_set_alloc_inequality(hull);
796 isl_seq_cpy(hull->ineq[k], lower, 2);
798 if (upper) {
799 k = isl_basic_set_alloc_inequality(hull);
800 isl_seq_cpy(hull->ineq[k], upper, 2);
802 hull = isl_basic_set_finalize(hull);
803 isl_set_free(set);
804 isl_mat_free(c);
805 return hull;
806 error:
807 isl_set_free(set);
808 isl_mat_free(c);
809 return NULL;
812 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
814 struct isl_basic_set *convex_hull;
816 if (!set)
817 return NULL;
819 if (isl_set_is_empty(set))
820 convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
821 else
822 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
823 isl_set_free(set);
824 return convex_hull;
827 /* Compute the convex hull of a pair of basic sets without any parameters or
828 * integer divisions using Fourier-Motzkin elimination.
829 * The convex hull is the set of all points that can be written as
830 * the sum of points from both basic sets (in homogeneous coordinates).
831 * We set up the constraints in a space with dimensions for each of
832 * the three sets and then project out the dimensions corresponding
833 * to the two original basic sets, retaining only those corresponding
834 * to the convex hull.
836 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
837 struct isl_basic_set *bset2)
839 int i, j, k;
840 struct isl_basic_set *bset[2];
841 struct isl_basic_set *hull = NULL;
842 unsigned dim;
844 if (!bset1 || !bset2)
845 goto error;
847 dim = isl_basic_set_n_dim(bset1);
848 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
849 1 + dim + bset1->n_eq + bset2->n_eq,
850 2 + bset1->n_ineq + bset2->n_ineq);
851 bset[0] = bset1;
852 bset[1] = bset2;
853 for (i = 0; i < 2; ++i) {
854 for (j = 0; j < bset[i]->n_eq; ++j) {
855 k = isl_basic_set_alloc_equality(hull);
856 if (k < 0)
857 goto error;
858 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
859 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
860 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
861 1+dim);
863 for (j = 0; j < bset[i]->n_ineq; ++j) {
864 k = isl_basic_set_alloc_inequality(hull);
865 if (k < 0)
866 goto error;
867 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
868 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
869 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
870 bset[i]->ineq[j], 1+dim);
872 k = isl_basic_set_alloc_inequality(hull);
873 if (k < 0)
874 goto error;
875 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
876 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
878 for (j = 0; j < 1+dim; ++j) {
879 k = isl_basic_set_alloc_equality(hull);
880 if (k < 0)
881 goto error;
882 isl_seq_clr(hull->eq[k], 1+2+3*dim);
883 isl_int_set_si(hull->eq[k][j], -1);
884 isl_int_set_si(hull->eq[k][1+dim+j], 1);
885 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
887 hull = isl_basic_set_set_rational(hull);
888 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
889 hull = isl_basic_set_remove_redundancies(hull);
890 isl_basic_set_free(bset1);
891 isl_basic_set_free(bset2);
892 return hull;
893 error:
894 isl_basic_set_free(bset1);
895 isl_basic_set_free(bset2);
896 isl_basic_set_free(hull);
897 return NULL;
900 /* Is the set bounded for each value of the parameters?
902 int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
904 struct isl_tab *tab;
905 int bounded;
907 if (!bset)
908 return -1;
909 if (isl_basic_set_plain_is_empty(bset))
910 return 1;
912 tab = isl_tab_from_recession_cone(bset, 1);
913 bounded = isl_tab_cone_is_bounded(tab);
914 isl_tab_free(tab);
915 return bounded;
918 /* Is the image bounded for each value of the parameters and
919 * the domain variables?
921 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
923 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
924 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
925 int bounded;
927 bmap = isl_basic_map_copy(bmap);
928 bmap = isl_basic_map_cow(bmap);
929 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
930 isl_dim_in, 0, n_in);
931 bounded = isl_basic_set_is_bounded((isl_basic_set *)bmap);
932 isl_basic_map_free(bmap);
934 return bounded;
937 /* Is the set bounded for each value of the parameters?
939 int isl_set_is_bounded(__isl_keep isl_set *set)
941 int i;
943 if (!set)
944 return -1;
946 for (i = 0; i < set->n; ++i) {
947 int bounded = isl_basic_set_is_bounded(set->p[i]);
948 if (!bounded || bounded < 0)
949 return bounded;
951 return 1;
954 /* Compute the lineality space of the convex hull of bset1 and bset2.
956 * We first compute the intersection of the recession cone of bset1
957 * with the negative of the recession cone of bset2 and then compute
958 * the linear hull of the resulting cone.
960 static struct isl_basic_set *induced_lineality_space(
961 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
963 int i, k;
964 struct isl_basic_set *lin = NULL;
965 unsigned dim;
967 if (!bset1 || !bset2)
968 goto error;
970 dim = isl_basic_set_total_dim(bset1);
971 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
972 bset1->n_eq + bset2->n_eq,
973 bset1->n_ineq + bset2->n_ineq);
974 lin = isl_basic_set_set_rational(lin);
975 if (!lin)
976 goto error;
977 for (i = 0; i < bset1->n_eq; ++i) {
978 k = isl_basic_set_alloc_equality(lin);
979 if (k < 0)
980 goto error;
981 isl_int_set_si(lin->eq[k][0], 0);
982 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
984 for (i = 0; i < bset1->n_ineq; ++i) {
985 k = isl_basic_set_alloc_inequality(lin);
986 if (k < 0)
987 goto error;
988 isl_int_set_si(lin->ineq[k][0], 0);
989 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
991 for (i = 0; i < bset2->n_eq; ++i) {
992 k = isl_basic_set_alloc_equality(lin);
993 if (k < 0)
994 goto error;
995 isl_int_set_si(lin->eq[k][0], 0);
996 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
998 for (i = 0; i < bset2->n_ineq; ++i) {
999 k = isl_basic_set_alloc_inequality(lin);
1000 if (k < 0)
1001 goto error;
1002 isl_int_set_si(lin->ineq[k][0], 0);
1003 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
1006 isl_basic_set_free(bset1);
1007 isl_basic_set_free(bset2);
1008 return isl_basic_set_affine_hull(lin);
1009 error:
1010 isl_basic_set_free(lin);
1011 isl_basic_set_free(bset1);
1012 isl_basic_set_free(bset2);
1013 return NULL;
1016 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1018 /* Given a set and a linear space "lin" of dimension n > 0,
1019 * project the linear space from the set, compute the convex hull
1020 * and then map the set back to the original space.
1022 * Let
1024 * M x = 0
1026 * describe the linear space. We first compute the Hermite normal
1027 * form H = M U of M = H Q, to obtain
1029 * H Q x = 0
1031 * The last n rows of H will be zero, so the last n variables of x' = Q x
1032 * are the one we want to project out. We do this by transforming each
1033 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1034 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1035 * we transform the hull back to the original space as A' Q_1 x >= b',
1036 * with Q_1 all but the last n rows of Q.
1038 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1039 struct isl_basic_set *lin)
1041 unsigned total = isl_basic_set_total_dim(lin);
1042 unsigned lin_dim;
1043 struct isl_basic_set *hull;
1044 struct isl_mat *M, *U, *Q;
1046 if (!set || !lin)
1047 goto error;
1048 lin_dim = total - lin->n_eq;
1049 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1050 M = isl_mat_left_hermite(M, 0, &U, &Q);
1051 if (!M)
1052 goto error;
1053 isl_mat_free(M);
1054 isl_basic_set_free(lin);
1056 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1058 U = isl_mat_lin_to_aff(U);
1059 Q = isl_mat_lin_to_aff(Q);
1061 set = isl_set_preimage(set, U);
1062 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1063 hull = uset_convex_hull(set);
1064 hull = isl_basic_set_preimage(hull, Q);
1066 return hull;
1067 error:
1068 isl_basic_set_free(lin);
1069 isl_set_free(set);
1070 return NULL;
1073 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1074 * set up an LP for solving
1076 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1078 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1079 * The next \alpha{ij} correspond to the equalities and come in pairs.
1080 * The final \alpha{ij} correspond to the inequalities.
1082 static struct isl_basic_set *valid_direction_lp(
1083 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1085 isl_space *dim;
1086 struct isl_basic_set *lp;
1087 unsigned d;
1088 int n;
1089 int i, j, k;
1091 if (!bset1 || !bset2)
1092 goto error;
1093 d = 1 + isl_basic_set_total_dim(bset1);
1094 n = 2 +
1095 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1096 dim = isl_space_set_alloc(bset1->ctx, 0, n);
1097 lp = isl_basic_set_alloc_space(dim, 0, d, n);
1098 if (!lp)
1099 goto error;
1100 for (i = 0; i < n; ++i) {
1101 k = isl_basic_set_alloc_inequality(lp);
1102 if (k < 0)
1103 goto error;
1104 isl_seq_clr(lp->ineq[k] + 1, n);
1105 isl_int_set_si(lp->ineq[k][0], -1);
1106 isl_int_set_si(lp->ineq[k][1 + i], 1);
1108 for (i = 0; i < d; ++i) {
1109 k = isl_basic_set_alloc_equality(lp);
1110 if (k < 0)
1111 goto error;
1112 n = 0;
1113 isl_int_set_si(lp->eq[k][n], 0); n++;
1114 /* positivity constraint 1 >= 0 */
1115 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1116 for (j = 0; j < bset1->n_eq; ++j) {
1117 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1118 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1120 for (j = 0; j < bset1->n_ineq; ++j) {
1121 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1123 /* positivity constraint 1 >= 0 */
1124 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1125 for (j = 0; j < bset2->n_eq; ++j) {
1126 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1127 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1129 for (j = 0; j < bset2->n_ineq; ++j) {
1130 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1133 lp = isl_basic_set_gauss(lp, NULL);
1134 isl_basic_set_free(bset1);
1135 isl_basic_set_free(bset2);
1136 return lp;
1137 error:
1138 isl_basic_set_free(bset1);
1139 isl_basic_set_free(bset2);
1140 return NULL;
1143 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1144 * for all rays in the homogeneous space of the two cones that correspond
1145 * to the input polyhedra bset1 and bset2.
1147 * We compute s as a vector that satisfies
1149 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1151 * with h_{ij} the normals of the facets of polyhedron i
1152 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1153 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1154 * We first set up an LP with as variables the \alpha{ij}.
1155 * In this formulation, for each polyhedron i,
1156 * the first constraint is the positivity constraint, followed by pairs
1157 * of variables for the equalities, followed by variables for the inequalities.
1158 * We then simply pick a feasible solution and compute s using (*).
1160 * Note that we simply pick any valid direction and make no attempt
1161 * to pick a "good" or even the "best" valid direction.
1163 static struct isl_vec *valid_direction(
1164 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1166 struct isl_basic_set *lp;
1167 struct isl_tab *tab;
1168 struct isl_vec *sample = NULL;
1169 struct isl_vec *dir;
1170 unsigned d;
1171 int i;
1172 int n;
1174 if (!bset1 || !bset2)
1175 goto error;
1176 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1177 isl_basic_set_copy(bset2));
1178 tab = isl_tab_from_basic_set(lp, 0);
1179 sample = isl_tab_get_sample_value(tab);
1180 isl_tab_free(tab);
1181 isl_basic_set_free(lp);
1182 if (!sample)
1183 goto error;
1184 d = isl_basic_set_total_dim(bset1);
1185 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1186 if (!dir)
1187 goto error;
1188 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1189 n = 1;
1190 /* positivity constraint 1 >= 0 */
1191 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1192 for (i = 0; i < bset1->n_eq; ++i) {
1193 isl_int_sub(sample->block.data[n],
1194 sample->block.data[n], sample->block.data[n+1]);
1195 isl_seq_combine(dir->block.data,
1196 bset1->ctx->one, dir->block.data,
1197 sample->block.data[n], bset1->eq[i], 1 + d);
1199 n += 2;
1201 for (i = 0; i < bset1->n_ineq; ++i)
1202 isl_seq_combine(dir->block.data,
1203 bset1->ctx->one, dir->block.data,
1204 sample->block.data[n++], bset1->ineq[i], 1 + d);
1205 isl_vec_free(sample);
1206 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1207 isl_basic_set_free(bset1);
1208 isl_basic_set_free(bset2);
1209 return dir;
1210 error:
1211 isl_vec_free(sample);
1212 isl_basic_set_free(bset1);
1213 isl_basic_set_free(bset2);
1214 return NULL;
1217 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1218 * compute b_i' + A_i' x' >= 0, with
1220 * [ b_i A_i ] [ y' ] [ y' ]
1221 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1223 * In particular, add the "positivity constraint" and then perform
1224 * the mapping.
1226 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1227 struct isl_mat *T)
1229 int k;
1231 if (!bset)
1232 goto error;
1233 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1234 k = isl_basic_set_alloc_inequality(bset);
1235 if (k < 0)
1236 goto error;
1237 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1238 isl_int_set_si(bset->ineq[k][0], 1);
1239 bset = isl_basic_set_preimage(bset, T);
1240 return bset;
1241 error:
1242 isl_mat_free(T);
1243 isl_basic_set_free(bset);
1244 return NULL;
1247 /* Compute the convex hull of a pair of basic sets without any parameters or
1248 * integer divisions, where the convex hull is known to be pointed,
1249 * but the basic sets may be unbounded.
1251 * We turn this problem into the computation of a convex hull of a pair
1252 * _bounded_ polyhedra by "changing the direction of the homogeneous
1253 * dimension". This idea is due to Matthias Koeppe.
1255 * Consider the cones in homogeneous space that correspond to the
1256 * input polyhedra. The rays of these cones are also rays of the
1257 * polyhedra if the coordinate that corresponds to the homogeneous
1258 * dimension is zero. That is, if the inner product of the rays
1259 * with the homogeneous direction is zero.
1260 * The cones in the homogeneous space can also be considered to
1261 * correspond to other pairs of polyhedra by chosing a different
1262 * homogeneous direction. To ensure that both of these polyhedra
1263 * are bounded, we need to make sure that all rays of the cones
1264 * correspond to vertices and not to rays.
1265 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1266 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1267 * The vector s is computed in valid_direction.
1269 * Note that we need to consider _all_ rays of the cones and not just
1270 * the rays that correspond to rays in the polyhedra. If we were to
1271 * only consider those rays and turn them into vertices, then we
1272 * may inadvertently turn some vertices into rays.
1274 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1275 * We therefore transform the two polyhedra such that the selected
1276 * direction is mapped onto this standard direction and then proceed
1277 * with the normal computation.
1278 * Let S be a non-singular square matrix with s as its first row,
1279 * then we want to map the polyhedra to the space
1281 * [ y' ] [ y ] [ y ] [ y' ]
1282 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1284 * We take S to be the unimodular completion of s to limit the growth
1285 * of the coefficients in the following computations.
1287 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1288 * We first move to the homogeneous dimension
1290 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1291 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1293 * Then we change directoin
1295 * [ b_i A_i ] [ y' ] [ y' ]
1296 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1298 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1299 * resulting in b' + A' x' >= 0, which we then convert back
1301 * [ y ] [ y ]
1302 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1304 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1306 static struct isl_basic_set *convex_hull_pair_pointed(
1307 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1309 struct isl_ctx *ctx = NULL;
1310 struct isl_vec *dir = NULL;
1311 struct isl_mat *T = NULL;
1312 struct isl_mat *T2 = NULL;
1313 struct isl_basic_set *hull;
1314 struct isl_set *set;
1316 if (!bset1 || !bset2)
1317 goto error;
1318 ctx = isl_basic_set_get_ctx(bset1);
1319 dir = valid_direction(isl_basic_set_copy(bset1),
1320 isl_basic_set_copy(bset2));
1321 if (!dir)
1322 goto error;
1323 T = isl_mat_alloc(ctx, dir->size, dir->size);
1324 if (!T)
1325 goto error;
1326 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1327 T = isl_mat_unimodular_complete(T, 1);
1328 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1330 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1331 bset2 = homogeneous_map(bset2, T2);
1332 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1333 set = isl_set_add_basic_set(set, bset1);
1334 set = isl_set_add_basic_set(set, bset2);
1335 hull = uset_convex_hull(set);
1336 hull = isl_basic_set_preimage(hull, T);
1338 isl_vec_free(dir);
1340 return hull;
1341 error:
1342 isl_vec_free(dir);
1343 isl_basic_set_free(bset1);
1344 isl_basic_set_free(bset2);
1345 return NULL;
1348 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1349 static struct isl_basic_set *modulo_affine_hull(
1350 struct isl_set *set, struct isl_basic_set *affine_hull);
1352 /* Compute the convex hull of a pair of basic sets without any parameters or
1353 * integer divisions.
1355 * This function is called from uset_convex_hull_unbounded, which
1356 * means that the complete convex hull is unbounded. Some pairs
1357 * of basic sets may still be bounded, though.
1358 * They may even lie inside a lower dimensional space, in which
1359 * case they need to be handled inside their affine hull since
1360 * the main algorithm assumes that the result is full-dimensional.
1362 * If the convex hull of the two basic sets would have a non-trivial
1363 * lineality space, we first project out this lineality space.
1365 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1366 struct isl_basic_set *bset2)
1368 isl_basic_set *lin, *aff;
1369 int bounded1, bounded2;
1371 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1372 return convex_hull_pair_elim(bset1, bset2);
1374 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1375 isl_basic_set_copy(bset2)));
1376 if (!aff)
1377 goto error;
1378 if (aff->n_eq != 0)
1379 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1380 isl_basic_set_free(aff);
1382 bounded1 = isl_basic_set_is_bounded(bset1);
1383 bounded2 = isl_basic_set_is_bounded(bset2);
1385 if (bounded1 < 0 || bounded2 < 0)
1386 goto error;
1388 if (bounded1 && bounded2)
1389 return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1391 if (bounded1 || bounded2)
1392 return convex_hull_pair_pointed(bset1, bset2);
1394 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1395 isl_basic_set_copy(bset2));
1396 if (!lin)
1397 goto error;
1398 if (isl_basic_set_is_universe(lin)) {
1399 isl_basic_set_free(bset1);
1400 isl_basic_set_free(bset2);
1401 return lin;
1403 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1404 struct isl_set *set;
1405 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1406 set = isl_set_add_basic_set(set, bset1);
1407 set = isl_set_add_basic_set(set, bset2);
1408 return modulo_lineality(set, lin);
1410 isl_basic_set_free(lin);
1412 return convex_hull_pair_pointed(bset1, bset2);
1413 error:
1414 isl_basic_set_free(bset1);
1415 isl_basic_set_free(bset2);
1416 return NULL;
1419 /* Compute the lineality space of a basic set.
1420 * We currently do not allow the basic set to have any divs.
1421 * We basically just drop the constants and turn every inequality
1422 * into an equality.
1424 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1426 int i, k;
1427 struct isl_basic_set *lin = NULL;
1428 unsigned dim;
1430 if (!bset)
1431 goto error;
1432 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1433 dim = isl_basic_set_total_dim(bset);
1435 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, dim, 0);
1436 if (!lin)
1437 goto error;
1438 for (i = 0; i < bset->n_eq; ++i) {
1439 k = isl_basic_set_alloc_equality(lin);
1440 if (k < 0)
1441 goto error;
1442 isl_int_set_si(lin->eq[k][0], 0);
1443 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1445 lin = isl_basic_set_gauss(lin, NULL);
1446 if (!lin)
1447 goto error;
1448 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1449 k = isl_basic_set_alloc_equality(lin);
1450 if (k < 0)
1451 goto error;
1452 isl_int_set_si(lin->eq[k][0], 0);
1453 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1454 lin = isl_basic_set_gauss(lin, NULL);
1455 if (!lin)
1456 goto error;
1458 isl_basic_set_free(bset);
1459 return lin;
1460 error:
1461 isl_basic_set_free(lin);
1462 isl_basic_set_free(bset);
1463 return NULL;
1466 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1467 * "underlying" set "set".
1469 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1471 int i;
1472 struct isl_set *lin = NULL;
1474 if (!set)
1475 return NULL;
1476 if (set->n == 0) {
1477 isl_space *dim = isl_set_get_space(set);
1478 isl_set_free(set);
1479 return isl_basic_set_empty(dim);
1482 lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
1483 for (i = 0; i < set->n; ++i)
1484 lin = isl_set_add_basic_set(lin,
1485 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1486 isl_set_free(set);
1487 return isl_set_affine_hull(lin);
1490 /* Compute the convex hull of a set without any parameters or
1491 * integer divisions.
1492 * In each step, we combined two basic sets until only one
1493 * basic set is left.
1494 * The input basic sets are assumed not to have a non-trivial
1495 * lineality space. If any of the intermediate results has
1496 * a non-trivial lineality space, it is projected out.
1498 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1500 struct isl_basic_set *convex_hull = NULL;
1502 convex_hull = isl_set_copy_basic_set(set);
1503 set = isl_set_drop_basic_set(set, convex_hull);
1504 if (!set)
1505 goto error;
1506 while (set->n > 0) {
1507 struct isl_basic_set *t;
1508 t = isl_set_copy_basic_set(set);
1509 if (!t)
1510 goto error;
1511 set = isl_set_drop_basic_set(set, t);
1512 if (!set)
1513 goto error;
1514 convex_hull = convex_hull_pair(convex_hull, t);
1515 if (set->n == 0)
1516 break;
1517 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1518 if (!t)
1519 goto error;
1520 if (isl_basic_set_is_universe(t)) {
1521 isl_basic_set_free(convex_hull);
1522 convex_hull = t;
1523 break;
1525 if (t->n_eq < isl_basic_set_total_dim(t)) {
1526 set = isl_set_add_basic_set(set, convex_hull);
1527 return modulo_lineality(set, t);
1529 isl_basic_set_free(t);
1531 isl_set_free(set);
1532 return convex_hull;
1533 error:
1534 isl_set_free(set);
1535 isl_basic_set_free(convex_hull);
1536 return NULL;
1539 /* Compute an initial hull for wrapping containing a single initial
1540 * facet.
1541 * This function assumes that the given set is bounded.
1543 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1544 struct isl_set *set)
1546 struct isl_mat *bounds = NULL;
1547 unsigned dim;
1548 int k;
1550 if (!hull)
1551 goto error;
1552 bounds = initial_facet_constraint(set);
1553 if (!bounds)
1554 goto error;
1555 k = isl_basic_set_alloc_inequality(hull);
1556 if (k < 0)
1557 goto error;
1558 dim = isl_set_n_dim(set);
1559 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1560 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1561 isl_mat_free(bounds);
1563 return hull;
1564 error:
1565 isl_basic_set_free(hull);
1566 isl_mat_free(bounds);
1567 return NULL;
1570 struct max_constraint {
1571 struct isl_mat *c;
1572 int count;
1573 int ineq;
1576 static int max_constraint_equal(const void *entry, const void *val)
1578 struct max_constraint *a = (struct max_constraint *)entry;
1579 isl_int *b = (isl_int *)val;
1581 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1584 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1585 isl_int *con, unsigned len, int n, int ineq)
1587 struct isl_hash_table_entry *entry;
1588 struct max_constraint *c;
1589 uint32_t c_hash;
1591 c_hash = isl_seq_get_hash(con + 1, len);
1592 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1593 con + 1, 0);
1594 if (!entry)
1595 return;
1596 c = entry->data;
1597 if (c->count < n) {
1598 isl_hash_table_remove(ctx, table, entry);
1599 return;
1601 c->count++;
1602 if (isl_int_gt(c->c->row[0][0], con[0]))
1603 return;
1604 if (isl_int_eq(c->c->row[0][0], con[0])) {
1605 if (ineq)
1606 c->ineq = ineq;
1607 return;
1609 c->c = isl_mat_cow(c->c);
1610 isl_int_set(c->c->row[0][0], con[0]);
1611 c->ineq = ineq;
1614 /* Check whether the constraint hash table "table" constains the constraint
1615 * "con".
1617 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1618 isl_int *con, unsigned len, int n)
1620 struct isl_hash_table_entry *entry;
1621 struct max_constraint *c;
1622 uint32_t c_hash;
1624 c_hash = isl_seq_get_hash(con + 1, len);
1625 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1626 con + 1, 0);
1627 if (!entry)
1628 return 0;
1629 c = entry->data;
1630 if (c->count < n)
1631 return 0;
1632 return isl_int_eq(c->c->row[0][0], con[0]);
1635 /* Check for inequality constraints of a basic set without equalities
1636 * such that the same or more stringent copies of the constraint appear
1637 * in all of the basic sets. Such constraints are necessarily facet
1638 * constraints of the convex hull.
1640 * If the resulting basic set is by chance identical to one of
1641 * the basic sets in "set", then we know that this basic set contains
1642 * all other basic sets and is therefore the convex hull of set.
1643 * In this case we set *is_hull to 1.
1645 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1646 struct isl_set *set, int *is_hull)
1648 int i, j, s, n;
1649 int min_constraints;
1650 int best;
1651 struct max_constraint *constraints = NULL;
1652 struct isl_hash_table *table = NULL;
1653 unsigned total;
1655 *is_hull = 0;
1657 for (i = 0; i < set->n; ++i)
1658 if (set->p[i]->n_eq == 0)
1659 break;
1660 if (i >= set->n)
1661 return hull;
1662 min_constraints = set->p[i]->n_ineq;
1663 best = i;
1664 for (i = best + 1; i < set->n; ++i) {
1665 if (set->p[i]->n_eq != 0)
1666 continue;
1667 if (set->p[i]->n_ineq >= min_constraints)
1668 continue;
1669 min_constraints = set->p[i]->n_ineq;
1670 best = i;
1672 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1673 min_constraints);
1674 if (!constraints)
1675 return hull;
1676 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1677 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1678 goto error;
1680 total = isl_space_dim(set->dim, isl_dim_all);
1681 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1682 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1683 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1684 if (!constraints[i].c)
1685 goto error;
1686 constraints[i].ineq = 1;
1688 for (i = 0; i < min_constraints; ++i) {
1689 struct isl_hash_table_entry *entry;
1690 uint32_t c_hash;
1691 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1692 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1693 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1694 if (!entry)
1695 goto error;
1696 isl_assert(hull->ctx, !entry->data, goto error);
1697 entry->data = &constraints[i];
1700 n = 0;
1701 for (s = 0; s < set->n; ++s) {
1702 if (s == best)
1703 continue;
1705 for (i = 0; i < set->p[s]->n_eq; ++i) {
1706 isl_int *eq = set->p[s]->eq[i];
1707 for (j = 0; j < 2; ++j) {
1708 isl_seq_neg(eq, eq, 1 + total);
1709 update_constraint(hull->ctx, table,
1710 eq, total, n, 0);
1713 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1714 isl_int *ineq = set->p[s]->ineq[i];
1715 update_constraint(hull->ctx, table, ineq, total, n,
1716 set->p[s]->n_eq == 0);
1718 ++n;
1721 for (i = 0; i < min_constraints; ++i) {
1722 if (constraints[i].count < n)
1723 continue;
1724 if (!constraints[i].ineq)
1725 continue;
1726 j = isl_basic_set_alloc_inequality(hull);
1727 if (j < 0)
1728 goto error;
1729 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1732 for (s = 0; s < set->n; ++s) {
1733 if (set->p[s]->n_eq)
1734 continue;
1735 if (set->p[s]->n_ineq != hull->n_ineq)
1736 continue;
1737 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1738 isl_int *ineq = set->p[s]->ineq[i];
1739 if (!has_constraint(hull->ctx, table, ineq, total, n))
1740 break;
1742 if (i == set->p[s]->n_ineq)
1743 *is_hull = 1;
1746 isl_hash_table_clear(table);
1747 for (i = 0; i < min_constraints; ++i)
1748 isl_mat_free(constraints[i].c);
1749 free(constraints);
1750 free(table);
1751 return hull;
1752 error:
1753 isl_hash_table_clear(table);
1754 free(table);
1755 if (constraints)
1756 for (i = 0; i < min_constraints; ++i)
1757 isl_mat_free(constraints[i].c);
1758 free(constraints);
1759 return hull;
1762 /* Create a template for the convex hull of "set" and fill it up
1763 * obvious facet constraints, if any. If the result happens to
1764 * be the convex hull of "set" then *is_hull is set to 1.
1766 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1768 struct isl_basic_set *hull;
1769 unsigned n_ineq;
1770 int i;
1772 n_ineq = 1;
1773 for (i = 0; i < set->n; ++i) {
1774 n_ineq += set->p[i]->n_eq;
1775 n_ineq += set->p[i]->n_ineq;
1777 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1778 hull = isl_basic_set_set_rational(hull);
1779 if (!hull)
1780 return NULL;
1781 return common_constraints(hull, set, is_hull);
1784 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1786 struct isl_basic_set *hull;
1787 int is_hull;
1789 hull = proto_hull(set, &is_hull);
1790 if (hull && !is_hull) {
1791 if (hull->n_ineq == 0)
1792 hull = initial_hull(hull, set);
1793 hull = extend(hull, set);
1795 isl_set_free(set);
1797 return hull;
1800 /* Compute the convex hull of a set without any parameters or
1801 * integer divisions. Depending on whether the set is bounded,
1802 * we pass control to the wrapping based convex hull or
1803 * the Fourier-Motzkin elimination based convex hull.
1804 * We also handle a few special cases before checking the boundedness.
1806 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1808 struct isl_basic_set *convex_hull = NULL;
1809 struct isl_basic_set *lin;
1811 if (isl_set_n_dim(set) == 0)
1812 return convex_hull_0d(set);
1814 set = isl_set_coalesce(set);
1815 set = isl_set_set_rational(set);
1817 if (!set)
1818 goto error;
1819 if (!set)
1820 return NULL;
1821 if (set->n == 1) {
1822 convex_hull = isl_basic_set_copy(set->p[0]);
1823 isl_set_free(set);
1824 return convex_hull;
1826 if (isl_set_n_dim(set) == 1)
1827 return convex_hull_1d(set);
1829 if (isl_set_is_bounded(set) &&
1830 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1831 return uset_convex_hull_wrap(set);
1833 lin = uset_combined_lineality_space(isl_set_copy(set));
1834 if (!lin)
1835 goto error;
1836 if (isl_basic_set_is_universe(lin)) {
1837 isl_set_free(set);
1838 return lin;
1840 if (lin->n_eq < isl_basic_set_total_dim(lin))
1841 return modulo_lineality(set, lin);
1842 isl_basic_set_free(lin);
1844 return uset_convex_hull_unbounded(set);
1845 error:
1846 isl_set_free(set);
1847 isl_basic_set_free(convex_hull);
1848 return NULL;
1851 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1852 * without parameters or divs and where the convex hull of set is
1853 * known to be full-dimensional.
1855 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1857 struct isl_basic_set *convex_hull = NULL;
1859 if (!set)
1860 goto error;
1862 if (isl_set_n_dim(set) == 0) {
1863 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1864 isl_set_free(set);
1865 convex_hull = isl_basic_set_set_rational(convex_hull);
1866 return convex_hull;
1869 set = isl_set_set_rational(set);
1870 set = isl_set_coalesce(set);
1871 if (!set)
1872 goto error;
1873 if (set->n == 1) {
1874 convex_hull = isl_basic_set_copy(set->p[0]);
1875 isl_set_free(set);
1876 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1877 return convex_hull;
1879 if (isl_set_n_dim(set) == 1)
1880 return convex_hull_1d(set);
1882 return uset_convex_hull_wrap(set);
1883 error:
1884 isl_set_free(set);
1885 return NULL;
1888 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1889 * We first remove the equalities (transforming the set), compute the
1890 * convex hull of the transformed set and then add the equalities back
1891 * (after performing the inverse transformation.
1893 static struct isl_basic_set *modulo_affine_hull(
1894 struct isl_set *set, struct isl_basic_set *affine_hull)
1896 struct isl_mat *T;
1897 struct isl_mat *T2;
1898 struct isl_basic_set *dummy;
1899 struct isl_basic_set *convex_hull;
1901 dummy = isl_basic_set_remove_equalities(
1902 isl_basic_set_copy(affine_hull), &T, &T2);
1903 if (!dummy)
1904 goto error;
1905 isl_basic_set_free(dummy);
1906 set = isl_set_preimage(set, T);
1907 convex_hull = uset_convex_hull(set);
1908 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1909 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1910 return convex_hull;
1911 error:
1912 isl_basic_set_free(affine_hull);
1913 isl_set_free(set);
1914 return NULL;
1917 /* Return an empty basic map living in the same space as "map".
1919 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1920 __isl_take isl_map *map)
1922 isl_space *space;
1924 space = isl_map_get_space(map);
1925 isl_map_free(map);
1926 return isl_basic_map_empty(space);
1929 /* Compute the convex hull of a map.
1931 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1932 * specifically, the wrapping of facets to obtain new facets.
1934 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1936 struct isl_basic_set *bset;
1937 struct isl_basic_map *model = NULL;
1938 struct isl_basic_set *affine_hull = NULL;
1939 struct isl_basic_map *convex_hull = NULL;
1940 struct isl_set *set = NULL;
1942 map = isl_map_detect_equalities(map);
1943 map = isl_map_align_divs(map);
1944 if (!map)
1945 goto error;
1947 if (map->n == 0)
1948 return replace_map_by_empty_basic_map(map);
1950 model = isl_basic_map_copy(map->p[0]);
1951 set = isl_map_underlying_set(map);
1952 if (!set)
1953 goto error;
1955 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1956 if (!affine_hull)
1957 goto error;
1958 if (affine_hull->n_eq != 0)
1959 bset = modulo_affine_hull(set, affine_hull);
1960 else {
1961 isl_basic_set_free(affine_hull);
1962 bset = uset_convex_hull(set);
1965 convex_hull = isl_basic_map_overlying_set(bset, model);
1966 if (!convex_hull)
1967 return NULL;
1969 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1970 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1971 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1972 return convex_hull;
1973 error:
1974 isl_set_free(set);
1975 isl_basic_map_free(model);
1976 return NULL;
1979 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1981 return (struct isl_basic_set *)
1982 isl_map_convex_hull((struct isl_map *)set);
1985 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1987 isl_basic_map *hull;
1989 hull = isl_map_convex_hull(map);
1990 return isl_basic_map_remove_divs(hull);
1993 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1995 return (isl_basic_set *)isl_map_polyhedral_hull((isl_map *)set);
1998 struct sh_data_entry {
1999 struct isl_hash_table *table;
2000 struct isl_tab *tab;
2003 /* Holds the data needed during the simple hull computation.
2004 * In particular,
2005 * n the number of basic sets in the original set
2006 * hull_table a hash table of already computed constraints
2007 * in the simple hull
2008 * p for each basic set,
2009 * table a hash table of the constraints
2010 * tab the tableau corresponding to the basic set
2012 struct sh_data {
2013 struct isl_ctx *ctx;
2014 unsigned n;
2015 struct isl_hash_table *hull_table;
2016 struct sh_data_entry p[1];
2019 static void sh_data_free(struct sh_data *data)
2021 int i;
2023 if (!data)
2024 return;
2025 isl_hash_table_free(data->ctx, data->hull_table);
2026 for (i = 0; i < data->n; ++i) {
2027 isl_hash_table_free(data->ctx, data->p[i].table);
2028 isl_tab_free(data->p[i].tab);
2030 free(data);
2033 struct ineq_cmp_data {
2034 unsigned len;
2035 isl_int *p;
2038 static int has_ineq(const void *entry, const void *val)
2040 isl_int *row = (isl_int *)entry;
2041 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2043 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2044 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2047 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2048 isl_int *ineq, unsigned len)
2050 uint32_t c_hash;
2051 struct ineq_cmp_data v;
2052 struct isl_hash_table_entry *entry;
2054 v.len = len;
2055 v.p = ineq;
2056 c_hash = isl_seq_get_hash(ineq + 1, len);
2057 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2058 if (!entry)
2059 return - 1;
2060 entry->data = ineq;
2061 return 0;
2064 /* Fill hash table "table" with the constraints of "bset".
2065 * Equalities are added as two inequalities.
2066 * The value in the hash table is a pointer to the (in)equality of "bset".
2068 static int hash_basic_set(struct isl_hash_table *table,
2069 struct isl_basic_set *bset)
2071 int i, j;
2072 unsigned dim = isl_basic_set_total_dim(bset);
2074 for (i = 0; i < bset->n_eq; ++i) {
2075 for (j = 0; j < 2; ++j) {
2076 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2077 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2078 return -1;
2081 for (i = 0; i < bset->n_ineq; ++i) {
2082 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2083 return -1;
2085 return 0;
2088 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2090 struct sh_data *data;
2091 int i;
2093 data = isl_calloc(set->ctx, struct sh_data,
2094 sizeof(struct sh_data) +
2095 (set->n - 1) * sizeof(struct sh_data_entry));
2096 if (!data)
2097 return NULL;
2098 data->ctx = set->ctx;
2099 data->n = set->n;
2100 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2101 if (!data->hull_table)
2102 goto error;
2103 for (i = 0; i < set->n; ++i) {
2104 data->p[i].table = isl_hash_table_alloc(set->ctx,
2105 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2106 if (!data->p[i].table)
2107 goto error;
2108 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2109 goto error;
2111 return data;
2112 error:
2113 sh_data_free(data);
2114 return NULL;
2117 /* Check if inequality "ineq" is a bound for basic set "j" or if
2118 * it can be relaxed (by increasing the constant term) to become
2119 * a bound for that basic set. In the latter case, the constant
2120 * term is updated.
2121 * Relaxation of the constant term is only allowed if "shift" is set.
2123 * Return 1 if "ineq" is a bound
2124 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2125 * -1 if some error occurred
2127 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2128 isl_int *ineq, int shift)
2130 enum isl_lp_result res;
2131 isl_int opt;
2133 if (!data->p[j].tab) {
2134 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2135 if (!data->p[j].tab)
2136 return -1;
2139 isl_int_init(opt);
2141 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2142 &opt, NULL, 0);
2143 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2144 if (shift)
2145 isl_int_sub(ineq[0], ineq[0], opt);
2146 else
2147 res = isl_lp_unbounded;
2150 isl_int_clear(opt);
2152 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2153 res == isl_lp_unbounded ? 0 : -1;
2156 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2157 * become a bound on the whole set. If so, add the (relaxed) inequality
2158 * to "hull". Relaxation is only allowed if "shift" is set.
2160 * We first check if "hull" already contains a translate of the inequality.
2161 * If so, we are done.
2162 * Then, we check if any of the previous basic sets contains a translate
2163 * of the inequality. If so, then we have already considered this
2164 * inequality and we are done.
2165 * Otherwise, for each basic set other than "i", we check if the inequality
2166 * is a bound on the basic set.
2167 * For previous basic sets, we know that they do not contain a translate
2168 * of the inequality, so we directly call is_bound.
2169 * For following basic sets, we first check if a translate of the
2170 * inequality appears in its description and if so directly update
2171 * the inequality accordingly.
2173 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2174 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
2175 int shift)
2177 uint32_t c_hash;
2178 struct ineq_cmp_data v;
2179 struct isl_hash_table_entry *entry;
2180 int j, k;
2182 if (!hull)
2183 return NULL;
2185 v.len = isl_basic_set_total_dim(hull);
2186 v.p = ineq;
2187 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2189 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2190 has_ineq, &v, 0);
2191 if (entry)
2192 return hull;
2194 for (j = 0; j < i; ++j) {
2195 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2196 c_hash, has_ineq, &v, 0);
2197 if (entry)
2198 break;
2200 if (j < i)
2201 return hull;
2203 k = isl_basic_set_alloc_inequality(hull);
2204 if (k < 0)
2205 goto error;
2206 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2208 for (j = 0; j < i; ++j) {
2209 int bound;
2210 bound = is_bound(data, set, j, hull->ineq[k], shift);
2211 if (bound < 0)
2212 goto error;
2213 if (!bound)
2214 break;
2216 if (j < i) {
2217 isl_basic_set_free_inequality(hull, 1);
2218 return hull;
2221 for (j = i + 1; j < set->n; ++j) {
2222 int bound, neg;
2223 isl_int *ineq_j;
2224 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2225 c_hash, has_ineq, &v, 0);
2226 if (entry) {
2227 ineq_j = entry->data;
2228 neg = isl_seq_is_neg(ineq_j + 1,
2229 hull->ineq[k] + 1, v.len);
2230 if (neg)
2231 isl_int_neg(ineq_j[0], ineq_j[0]);
2232 if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
2233 isl_int_set(hull->ineq[k][0], ineq_j[0]);
2234 if (neg)
2235 isl_int_neg(ineq_j[0], ineq_j[0]);
2236 continue;
2238 bound = is_bound(data, set, j, hull->ineq[k], shift);
2239 if (bound < 0)
2240 goto error;
2241 if (!bound)
2242 break;
2244 if (j < set->n) {
2245 isl_basic_set_free_inequality(hull, 1);
2246 return hull;
2249 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2250 has_ineq, &v, 1);
2251 if (!entry)
2252 goto error;
2253 entry->data = hull->ineq[k];
2255 return hull;
2256 error:
2257 isl_basic_set_free(hull);
2258 return NULL;
2261 /* Check if any inequality from basic set "i" is or can be relaxed to
2262 * become a bound on the whole set. If so, add the (relaxed) inequality
2263 * to "hull". Relaxation is only allowed if "shift" is set.
2265 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2266 struct sh_data *data, struct isl_set *set, int i, int shift)
2268 int j, k;
2269 unsigned dim = isl_basic_set_total_dim(bset);
2271 for (j = 0; j < set->p[i]->n_eq; ++j) {
2272 for (k = 0; k < 2; ++k) {
2273 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2274 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2275 shift);
2278 for (j = 0; j < set->p[i]->n_ineq; ++j)
2279 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2280 return bset;
2283 /* Compute a superset of the convex hull of set that is described
2284 * by only (translates of) the constraints in the constituents of set.
2285 * Translation is only allowed if "shift" is set.
2287 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2288 int shift)
2290 struct sh_data *data = NULL;
2291 struct isl_basic_set *hull = NULL;
2292 unsigned n_ineq;
2293 int i;
2295 if (!set)
2296 return NULL;
2298 n_ineq = 0;
2299 for (i = 0; i < set->n; ++i) {
2300 if (!set->p[i])
2301 goto error;
2302 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2305 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2306 if (!hull)
2307 goto error;
2309 data = sh_data_alloc(set, n_ineq);
2310 if (!data)
2311 goto error;
2313 for (i = 0; i < set->n; ++i)
2314 hull = add_bounds(hull, data, set, i, shift);
2316 sh_data_free(data);
2317 isl_set_free(set);
2319 return hull;
2320 error:
2321 sh_data_free(data);
2322 isl_basic_set_free(hull);
2323 isl_set_free(set);
2324 return NULL;
2327 /* Compute a superset of the convex hull of map that is described
2328 * by only (translates of) the constraints in the constituents of map.
2329 * Translation is only allowed if "shift" is set.
2331 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2332 int shift)
2334 struct isl_set *set = NULL;
2335 struct isl_basic_map *model = NULL;
2336 struct isl_basic_map *hull;
2337 struct isl_basic_map *affine_hull;
2338 struct isl_basic_set *bset = NULL;
2340 if (!map)
2341 return NULL;
2342 if (map->n == 0)
2343 return replace_map_by_empty_basic_map(map);
2344 if (map->n == 1) {
2345 hull = isl_basic_map_copy(map->p[0]);
2346 isl_map_free(map);
2347 return hull;
2350 map = isl_map_detect_equalities(map);
2351 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2352 map = isl_map_align_divs(map);
2353 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2355 set = isl_map_underlying_set(map);
2357 bset = uset_simple_hull(set, shift);
2359 hull = isl_basic_map_overlying_set(bset, model);
2361 hull = isl_basic_map_intersect(hull, affine_hull);
2362 hull = isl_basic_map_remove_redundancies(hull);
2364 if (!hull)
2365 return NULL;
2366 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2367 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2369 hull = isl_basic_map_finalize(hull);
2371 return hull;
2374 /* Compute a superset of the convex hull of map that is described
2375 * by only translates of the constraints in the constituents of map.
2377 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2379 return map_simple_hull(map, 1);
2382 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2384 return (struct isl_basic_set *)
2385 isl_map_simple_hull((struct isl_map *)set);
2388 /* Compute a superset of the convex hull of map that is described
2389 * by only the constraints in the constituents of map.
2391 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2392 __isl_take isl_map *map)
2394 return map_simple_hull(map, 0);
2397 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2398 __isl_take isl_set *set)
2400 return isl_map_unshifted_simple_hull(set);
2403 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2405 * For each basic set in "set", we first check if the basic set
2406 * contains a translate of "ineq". If this translate is more relaxed,
2407 * then we assume that "ineq" is not a bound on this basic set.
2408 * Otherwise, we know that it is a bound.
2409 * If the basic set does not contain a translate of "ineq", then
2410 * we call is_bound to perform the test.
2412 static __isl_give isl_basic_set *add_bound_from_constraint(
2413 __isl_take isl_basic_set *hull, struct sh_data *data,
2414 __isl_keep isl_set *set, isl_int *ineq)
2416 int i, k;
2417 isl_ctx *ctx;
2418 uint32_t c_hash;
2419 struct ineq_cmp_data v;
2421 if (!hull || !set)
2422 return isl_basic_set_free(hull);
2424 v.len = isl_basic_set_total_dim(hull);
2425 v.p = ineq;
2426 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2428 ctx = isl_basic_set_get_ctx(hull);
2429 for (i = 0; i < set->n; ++i) {
2430 int bound;
2431 struct isl_hash_table_entry *entry;
2433 entry = isl_hash_table_find(ctx, data->p[i].table,
2434 c_hash, &has_ineq, &v, 0);
2435 if (entry) {
2436 isl_int *ineq_i = entry->data;
2437 int neg, more_relaxed;
2439 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2440 if (neg)
2441 isl_int_neg(ineq_i[0], ineq_i[0]);
2442 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2443 if (neg)
2444 isl_int_neg(ineq_i[0], ineq_i[0]);
2445 if (more_relaxed)
2446 break;
2447 else
2448 continue;
2450 bound = is_bound(data, set, i, ineq, 0);
2451 if (bound < 0)
2452 return isl_basic_set_free(hull);
2453 if (!bound)
2454 break;
2456 if (i < set->n)
2457 return hull;
2459 k = isl_basic_set_alloc_inequality(hull);
2460 if (k < 0)
2461 return isl_basic_set_free(hull);
2462 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2464 return hull;
2467 /* Compute a superset of the convex hull of "set" that is described
2468 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2469 * has no parameters or integer divisions.
2471 * The inequalities in "ineq" are assumed to have been sorted such
2472 * that constraints with the same linear part appear together and
2473 * that among constraints with the same linear part, those with
2474 * smaller constant term appear first.
2476 * We reuse the same data structure that is used by uset_simple_hull,
2477 * but we do not need the hull table since we will not consider the
2478 * same constraint more than once. We therefore allocate it with zero size.
2480 * We run through the constraints and try to add them one by one,
2481 * skipping identical constraints. If we have added a constraint and
2482 * the next constraint is a more relaxed translate, then we skip this
2483 * next constraint as well.
2485 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2486 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2488 int i;
2489 int last_added = 0;
2490 struct sh_data *data = NULL;
2491 isl_basic_set *hull = NULL;
2492 unsigned dim;
2494 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2495 if (!hull)
2496 goto error;
2498 data = sh_data_alloc(set, 0);
2499 if (!data)
2500 goto error;
2502 dim = isl_set_dim(set, isl_dim_set);
2503 for (i = 0; i < n_ineq; ++i) {
2504 int hull_n_ineq = hull->n_ineq;
2505 int parallel;
2507 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2508 dim);
2509 if (parallel &&
2510 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2511 continue;
2512 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2513 if (!hull)
2514 goto error;
2515 last_added = hull->n_ineq > hull_n_ineq;
2518 sh_data_free(data);
2519 isl_set_free(set);
2520 return hull;
2521 error:
2522 sh_data_free(data);
2523 isl_set_free(set);
2524 isl_basic_set_free(hull);
2525 return NULL;
2528 /* Collect pointers to all the inequalities in the elements of "list"
2529 * in "ineq". For equalities, store both a pointer to the equality and
2530 * a pointer to its opposite, which is first copied to "mat".
2531 * "ineq" and "mat" are assumed to have been preallocated to the right size
2532 * (the number of inequalities + 2 times the number of equalites and
2533 * the number of equalities, respectively).
2535 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2536 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2538 int i, j, n, n_eq, n_ineq;
2540 if (!mat)
2541 return NULL;
2543 n_eq = 0;
2544 n_ineq = 0;
2545 n = isl_basic_set_list_n_basic_set(list);
2546 for (i = 0; i < n; ++i) {
2547 isl_basic_set *bset;
2548 bset = isl_basic_set_list_get_basic_set(list, i);
2549 if (!bset)
2550 return isl_mat_free(mat);
2551 for (j = 0; j < bset->n_eq; ++j) {
2552 ineq[n_ineq++] = mat->row[n_eq];
2553 ineq[n_ineq++] = bset->eq[j];
2554 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2556 for (j = 0; j < bset->n_ineq; ++j)
2557 ineq[n_ineq++] = bset->ineq[j];
2558 isl_basic_set_free(bset);
2561 return mat;
2564 /* Comparison routine for use as an isl_sort callback.
2566 * Constraints with the same linear part are sorted together and
2567 * among constraints with the same linear part, those with smaller
2568 * constant term are sorted first.
2570 static int cmp_ineq(const void *a, const void *b, void *arg)
2572 unsigned dim = *(unsigned *) arg;
2573 isl_int * const *ineq1 = a;
2574 isl_int * const *ineq2 = b;
2575 int cmp;
2577 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2578 if (cmp != 0)
2579 return cmp;
2580 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2583 /* Compute a superset of the convex hull of "set" that is described
2584 * by only constraints in the elements of "list", where "set" has
2585 * no parameters or integer divisions.
2587 * We collect all the constraints in those elements and then
2588 * sort the constraints such that constraints with the same linear part
2589 * are sorted together and that those with smaller constant term are
2590 * sorted first.
2592 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2593 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2595 int i, n, n_eq, n_ineq;
2596 unsigned dim;
2597 isl_ctx *ctx;
2598 isl_mat *mat = NULL;
2599 isl_int **ineq = NULL;
2600 isl_basic_set *hull;
2602 if (!set)
2603 goto error;
2604 ctx = isl_set_get_ctx(set);
2606 n_eq = 0;
2607 n_ineq = 0;
2608 n = isl_basic_set_list_n_basic_set(list);
2609 for (i = 0; i < n; ++i) {
2610 isl_basic_set *bset;
2611 bset = isl_basic_set_list_get_basic_set(list, i);
2612 if (!bset)
2613 goto error;
2614 n_eq += bset->n_eq;
2615 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2616 isl_basic_set_free(bset);
2619 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2620 if (n_ineq > 0 && !ineq)
2621 goto error;
2623 dim = isl_set_dim(set, isl_dim_set);
2624 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2625 mat = collect_inequalities(mat, list, ineq);
2626 if (!mat)
2627 goto error;
2629 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2630 goto error;
2632 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2634 isl_mat_free(mat);
2635 free(ineq);
2636 isl_basic_set_list_free(list);
2637 return hull;
2638 error:
2639 isl_mat_free(mat);
2640 free(ineq);
2641 isl_set_free(set);
2642 isl_basic_set_list_free(list);
2643 return NULL;
2646 /* Compute a superset of the convex hull of "map" that is described
2647 * by only constraints in the elements of "list".
2649 * If the list is empty, then we can only describe the universe set.
2650 * If the input map is empty, then all constraints are valid, so
2651 * we return the intersection of the elements in "list".
2653 * Otherwise, we align all divs and temporarily treat them
2654 * as regular variables, computing the unshifted simple hull in
2655 * uset_unshifted_simple_hull_from_basic_set_list.
2657 static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2658 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2660 isl_basic_map *model;
2661 isl_basic_map *hull;
2662 isl_set *set;
2663 isl_basic_set_list *bset_list;
2665 if (!map || !list)
2666 goto error;
2668 if (isl_basic_map_list_n_basic_map(list) == 0) {
2669 isl_space *space;
2671 space = isl_map_get_space(map);
2672 isl_map_free(map);
2673 isl_basic_map_list_free(list);
2674 return isl_basic_map_universe(space);
2676 if (isl_map_plain_is_empty(map)) {
2677 isl_map_free(map);
2678 return isl_basic_map_list_intersect(list);
2681 map = isl_map_align_divs_to_basic_map_list(map, list);
2682 if (!map)
2683 goto error;
2684 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2686 model = isl_basic_map_list_get_basic_map(list, 0);
2688 set = isl_map_underlying_set(map);
2689 bset_list = isl_basic_map_list_underlying_set(list);
2691 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2692 hull = isl_basic_map_overlying_set(hull, model);
2694 return hull;
2695 error:
2696 isl_map_free(map);
2697 isl_basic_map_list_free(list);
2698 return NULL;
2701 /* Return a sequence of the basic maps that make up the maps in "list".
2703 static __isl_give isl_basic_set_list *collect_basic_maps(
2704 __isl_take isl_map_list *list)
2706 int i, n;
2707 isl_ctx *ctx;
2708 isl_basic_map_list *bmap_list;
2710 if (!list)
2711 return NULL;
2712 n = isl_map_list_n_map(list);
2713 ctx = isl_map_list_get_ctx(list);
2714 bmap_list = isl_basic_map_list_alloc(ctx, 0);
2716 for (i = 0; i < n; ++i) {
2717 isl_map *map;
2718 isl_basic_map_list *list_i;
2720 map = isl_map_list_get_map(list, i);
2721 map = isl_map_compute_divs(map);
2722 list_i = isl_map_get_basic_map_list(map);
2723 isl_map_free(map);
2724 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
2727 isl_map_list_free(list);
2728 return bmap_list;
2731 /* Compute a superset of the convex hull of "map" that is described
2732 * by only constraints in the elements of "list".
2734 * If "map" is the universe, then the convex hull (and therefore
2735 * any superset of the convexhull) is the universe as well.
2737 * Otherwise, we collect all the basic maps in the map list and
2738 * continue with map_unshifted_simple_hull_from_basic_map_list.
2740 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
2741 __isl_take isl_map *map, __isl_take isl_map_list *list)
2743 isl_basic_map_list *bmap_list;
2744 int is_universe;
2746 is_universe = isl_map_plain_is_universe(map);
2747 if (is_universe < 0)
2748 map = isl_map_free(map);
2749 if (is_universe < 0 || is_universe) {
2750 isl_map_list_free(list);
2751 return isl_map_unshifted_simple_hull(map);
2754 bmap_list = collect_basic_maps(list);
2755 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
2758 /* Compute a superset of the convex hull of "set" that is described
2759 * by only constraints in the elements of "list".
2761 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
2762 __isl_take isl_set *set, __isl_take isl_set_list *list)
2764 return isl_map_unshifted_simple_hull_from_map_list(set, list);
2767 /* Given a set "set", return parametric bounds on the dimension "dim".
2769 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2771 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2772 set = isl_set_copy(set);
2773 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2774 set = isl_set_eliminate_dims(set, 0, dim);
2775 return isl_set_convex_hull(set);
2778 /* Computes a "simple hull" and then check if each dimension in the
2779 * resulting hull is bounded by a symbolic constant. If not, the
2780 * hull is intersected with the corresponding bounds on the whole set.
2782 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
2784 int i, j;
2785 struct isl_basic_set *hull;
2786 unsigned nparam, left;
2787 int removed_divs = 0;
2789 hull = isl_set_simple_hull(isl_set_copy(set));
2790 if (!hull)
2791 goto error;
2793 nparam = isl_basic_set_dim(hull, isl_dim_param);
2794 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
2795 int lower = 0, upper = 0;
2796 struct isl_basic_set *bounds;
2798 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
2799 for (j = 0; j < hull->n_eq; ++j) {
2800 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
2801 continue;
2802 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
2803 left) == -1)
2804 break;
2806 if (j < hull->n_eq)
2807 continue;
2809 for (j = 0; j < hull->n_ineq; ++j) {
2810 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
2811 continue;
2812 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
2813 left) != -1 ||
2814 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
2815 i) != -1)
2816 continue;
2817 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
2818 lower = 1;
2819 else
2820 upper = 1;
2821 if (lower && upper)
2822 break;
2825 if (lower && upper)
2826 continue;
2828 if (!removed_divs) {
2829 set = isl_set_remove_divs(set);
2830 if (!set)
2831 goto error;
2832 removed_divs = 1;
2834 bounds = set_bounds(set, i);
2835 hull = isl_basic_set_intersect(hull, bounds);
2836 if (!hull)
2837 goto error;
2840 isl_set_free(set);
2841 return hull;
2842 error:
2843 isl_set_free(set);
2844 return NULL;