isl_map_simplify.c: uset_gist_full: drop unused variable
[isl.git] / isl_convex_hull.c
blobc85cee4067c4dec79777465f125ebfa12fc78410
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 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
30 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
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 * Since some constraints may be mutually redundant, sort the constraints
87 * first such that constraints that involve existentially quantified
88 * variables are considered for removal before those that do not.
89 * The sorting is also needed for the use in map_simple_hull.
91 * Note that isl_tab_detect_implicit_equalities may also end up
92 * marking some constraints as redundant. Make sure the constraints
93 * are preserved and undo those marking such that isl_tab_detect_redundant
94 * can consider the constraints in the sorted order.
96 * Alternatively, we could have intersected the basic map with the
97 * corresponding equality and then checked if the dimension was that
98 * of a facet.
100 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
101 __isl_take isl_basic_map *bmap)
103 struct isl_tab *tab;
105 if (!bmap)
106 return NULL;
108 bmap = isl_basic_map_gauss(bmap, NULL);
109 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
110 return bmap;
111 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
112 return bmap;
113 if (bmap->n_ineq <= 1)
114 return bmap;
116 bmap = isl_basic_map_sort_constraints(bmap);
117 tab = isl_tab_from_basic_map(bmap, 0);
118 if (!tab)
119 goto error;
120 tab->preserve = 1;
121 if (isl_tab_detect_implicit_equalities(tab) < 0)
122 goto error;
123 if (isl_tab_restore_redundant(tab) < 0)
124 goto error;
125 tab->preserve = 0;
126 if (isl_tab_detect_redundant(tab) < 0)
127 goto error;
128 bmap = isl_basic_map_update_from_tab(bmap, tab);
129 isl_tab_free(tab);
130 if (!bmap)
131 return NULL;
132 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
133 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
134 return bmap;
135 error:
136 isl_tab_free(tab);
137 isl_basic_map_free(bmap);
138 return NULL;
141 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
142 __isl_take isl_basic_set *bset)
144 return bset_from_bmap(
145 isl_basic_map_remove_redundancies(bset_to_bmap(bset)));
148 /* Remove redundant constraints in each of the basic maps.
150 __isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
152 return isl_map_inline_foreach_basic_map(map,
153 &isl_basic_map_remove_redundancies);
156 __isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
158 return isl_map_remove_redundancies(set);
161 /* Check if the set set is bound in the direction of the affine
162 * constraint c and if so, set the constant term such that the
163 * resulting constraint is a bounding constraint for the set.
165 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
167 int first;
168 int j;
169 isl_int opt;
170 isl_int opt_denom;
172 isl_int_init(opt);
173 isl_int_init(opt_denom);
174 first = 1;
175 for (j = 0; j < set->n; ++j) {
176 enum isl_lp_result res;
178 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
179 continue;
181 res = isl_basic_set_solve_lp(set->p[j],
182 0, c, set->ctx->one, &opt, &opt_denom, NULL);
183 if (res == isl_lp_unbounded)
184 break;
185 if (res == isl_lp_error)
186 goto error;
187 if (res == isl_lp_empty) {
188 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
189 if (!set->p[j])
190 goto error;
191 continue;
193 if (first || isl_int_is_neg(opt)) {
194 if (!isl_int_is_one(opt_denom))
195 isl_seq_scale(c, c, opt_denom, len);
196 isl_int_sub(c[0], c[0], opt);
198 first = 0;
200 isl_int_clear(opt);
201 isl_int_clear(opt_denom);
202 return j >= set->n;
203 error:
204 isl_int_clear(opt);
205 isl_int_clear(opt_denom);
206 return -1;
209 __isl_give isl_basic_map *isl_basic_map_set_rational(
210 __isl_take isl_basic_map *bmap)
212 if (!bmap)
213 return NULL;
215 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
216 return bmap;
218 bmap = isl_basic_map_cow(bmap);
219 if (!bmap)
220 return NULL;
222 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
224 return isl_basic_map_finalize(bmap);
227 __isl_give isl_basic_set *isl_basic_set_set_rational(
228 __isl_take isl_basic_set *bset)
230 return isl_basic_map_set_rational(bset);
233 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
235 int i;
237 map = isl_map_cow(map);
238 if (!map)
239 return NULL;
240 for (i = 0; i < map->n; ++i) {
241 map->p[i] = isl_basic_map_set_rational(map->p[i]);
242 if (!map->p[i])
243 goto error;
245 return map;
246 error:
247 isl_map_free(map);
248 return NULL;
251 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
253 return isl_map_set_rational(set);
256 static struct isl_basic_set *isl_basic_set_add_equality(
257 struct isl_basic_set *bset, isl_int *c)
259 int i;
260 unsigned dim;
262 if (!bset)
263 return NULL;
265 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
266 return bset;
268 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
269 isl_assert(bset->ctx, bset->n_div == 0, goto error);
270 dim = isl_basic_set_n_dim(bset);
271 bset = isl_basic_set_cow(bset);
272 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
273 i = isl_basic_set_alloc_equality(bset);
274 if (i < 0)
275 goto error;
276 isl_seq_cpy(bset->eq[i], c, 1 + dim);
277 return bset;
278 error:
279 isl_basic_set_free(bset);
280 return NULL;
283 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
285 int i;
287 set = isl_set_cow(set);
288 if (!set)
289 return NULL;
290 for (i = 0; i < set->n; ++i) {
291 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
292 if (!set->p[i])
293 goto error;
295 return set;
296 error:
297 isl_set_free(set);
298 return NULL;
301 /* Given a union of basic sets, construct the constraints for wrapping
302 * a facet around one of its ridges.
303 * In particular, if each of n the d-dimensional basic sets i in "set"
304 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
305 * and is defined by the constraints
306 * [ 1 ]
307 * A_i [ x ] >= 0
309 * then the resulting set is of dimension n*(1+d) and has as constraints
311 * [ a_i ]
312 * A_i [ x_i ] >= 0
314 * a_i >= 0
316 * \sum_i x_{i,1} = 1
318 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
320 struct isl_basic_set *lp;
321 unsigned n_eq;
322 unsigned n_ineq;
323 int i, j, k;
324 unsigned dim, lp_dim;
326 if (!set)
327 return NULL;
329 dim = 1 + isl_set_n_dim(set);
330 n_eq = 1;
331 n_ineq = set->n;
332 for (i = 0; i < set->n; ++i) {
333 n_eq += set->p[i]->n_eq;
334 n_ineq += set->p[i]->n_ineq;
336 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
337 lp = isl_basic_set_set_rational(lp);
338 if (!lp)
339 return NULL;
340 lp_dim = isl_basic_set_n_dim(lp);
341 k = isl_basic_set_alloc_equality(lp);
342 isl_int_set_si(lp->eq[k][0], -1);
343 for (i = 0; i < set->n; ++i) {
344 isl_int_set_si(lp->eq[k][1+dim*i], 0);
345 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
346 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
348 for (i = 0; i < set->n; ++i) {
349 k = isl_basic_set_alloc_inequality(lp);
350 isl_seq_clr(lp->ineq[k], 1+lp_dim);
351 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
353 for (j = 0; j < set->p[i]->n_eq; ++j) {
354 k = isl_basic_set_alloc_equality(lp);
355 isl_seq_clr(lp->eq[k], 1+dim*i);
356 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
357 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
360 for (j = 0; j < set->p[i]->n_ineq; ++j) {
361 k = isl_basic_set_alloc_inequality(lp);
362 isl_seq_clr(lp->ineq[k], 1+dim*i);
363 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
364 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
367 return lp;
370 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
371 * of that facet, compute the other facet of the convex hull that contains
372 * the ridge.
374 * We first transform the set such that the facet constraint becomes
376 * x_1 >= 0
378 * I.e., the facet lies in
380 * x_1 = 0
382 * and on that facet, the constraint that defines the ridge is
384 * x_2 >= 0
386 * (This transformation is not strictly needed, all that is needed is
387 * that the ridge contains the origin.)
389 * Since the ridge contains the origin, the cone of the convex hull
390 * will be of the form
392 * x_1 >= 0
393 * x_2 >= a x_1
395 * with this second constraint defining the new facet.
396 * The constant a is obtained by settting x_1 in the cone of the
397 * convex hull to 1 and minimizing x_2.
398 * Now, each element in the cone of the convex hull is the sum
399 * of elements in the cones of the basic sets.
400 * If a_i is the dilation factor of basic set i, then the problem
401 * we need to solve is
403 * min \sum_i x_{i,2}
404 * st
405 * \sum_i x_{i,1} = 1
406 * a_i >= 0
407 * [ a_i ]
408 * A [ x_i ] >= 0
410 * with
411 * [ 1 ]
412 * A_i [ x_i ] >= 0
414 * the constraints of each (transformed) basic set.
415 * If a = n/d, then the constraint defining the new facet (in the transformed
416 * space) is
418 * -n x_1 + d x_2 >= 0
420 * In the original space, we need to take the same combination of the
421 * corresponding constraints "facet" and "ridge".
423 * If a = -infty = "-1/0", then we just return the original facet constraint.
424 * This means that the facet is unbounded, but has a bounded intersection
425 * with the union of sets.
427 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
428 isl_int *facet, isl_int *ridge)
430 int i;
431 isl_ctx *ctx;
432 struct isl_mat *T = NULL;
433 struct isl_basic_set *lp = NULL;
434 struct isl_vec *obj;
435 enum isl_lp_result res;
436 isl_int num, den;
437 unsigned dim;
439 if (!set)
440 return NULL;
441 ctx = set->ctx;
442 set = isl_set_copy(set);
443 set = isl_set_set_rational(set);
445 dim = 1 + isl_set_n_dim(set);
446 T = isl_mat_alloc(ctx, 3, dim);
447 if (!T)
448 goto error;
449 isl_int_set_si(T->row[0][0], 1);
450 isl_seq_clr(T->row[0]+1, dim - 1);
451 isl_seq_cpy(T->row[1], facet, dim);
452 isl_seq_cpy(T->row[2], ridge, dim);
453 T = isl_mat_right_inverse(T);
454 set = isl_set_preimage(set, T);
455 T = NULL;
456 if (!set)
457 goto error;
458 lp = wrap_constraints(set);
459 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
460 if (!obj)
461 goto error;
462 isl_int_set_si(obj->block.data[0], 0);
463 for (i = 0; i < set->n; ++i) {
464 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
465 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
466 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
468 isl_int_init(num);
469 isl_int_init(den);
470 res = isl_basic_set_solve_lp(lp, 0,
471 obj->block.data, ctx->one, &num, &den, NULL);
472 if (res == isl_lp_ok) {
473 isl_int_neg(num, num);
474 isl_seq_combine(facet, num, facet, den, ridge, dim);
475 isl_seq_normalize(ctx, facet, dim);
477 isl_int_clear(num);
478 isl_int_clear(den);
479 isl_vec_free(obj);
480 isl_basic_set_free(lp);
481 isl_set_free(set);
482 if (res == isl_lp_error)
483 return NULL;
484 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
485 return NULL);
486 return facet;
487 error:
488 isl_basic_set_free(lp);
489 isl_mat_free(T);
490 isl_set_free(set);
491 return NULL;
494 /* Compute the constraint of a facet of "set".
496 * We first compute the intersection with a bounding constraint
497 * that is orthogonal to one of the coordinate axes.
498 * If the affine hull of this intersection has only one equality,
499 * we have found a facet.
500 * Otherwise, we wrap the current bounding constraint around
501 * one of the equalities of the face (one that is not equal to
502 * the current bounding constraint).
503 * This process continues until we have found a facet.
504 * The dimension of the intersection increases by at least
505 * one on each iteration, so termination is guaranteed.
507 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
509 struct isl_set *slice = NULL;
510 struct isl_basic_set *face = NULL;
511 int i;
512 unsigned dim = isl_set_n_dim(set);
513 int is_bound;
514 isl_mat *bounds = NULL;
516 isl_assert(set->ctx, set->n > 0, goto error);
517 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
518 if (!bounds)
519 return NULL;
521 isl_seq_clr(bounds->row[0], dim);
522 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
523 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
524 if (is_bound < 0)
525 goto error;
526 isl_assert(set->ctx, is_bound, goto error);
527 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
528 bounds->n_row = 1;
530 for (;;) {
531 slice = isl_set_copy(set);
532 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
533 face = isl_set_affine_hull(slice);
534 if (!face)
535 goto error;
536 if (face->n_eq == 1) {
537 isl_basic_set_free(face);
538 break;
540 for (i = 0; i < face->n_eq; ++i)
541 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
542 !isl_seq_is_neg(bounds->row[0],
543 face->eq[i], 1 + dim))
544 break;
545 isl_assert(set->ctx, i < face->n_eq, goto error);
546 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
547 goto error;
548 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
549 isl_basic_set_free(face);
552 return bounds;
553 error:
554 isl_basic_set_free(face);
555 isl_mat_free(bounds);
556 return NULL;
559 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
560 * compute a hyperplane description of the facet, i.e., compute the facets
561 * of the facet.
563 * We compute an affine transformation that transforms the constraint
565 * [ 1 ]
566 * c [ x ] = 0
568 * to the constraint
570 * z_1 = 0
572 * by computing the right inverse U of a matrix that starts with the rows
574 * [ 1 0 ]
575 * [ c ]
577 * Then
578 * [ 1 ] [ 1 ]
579 * [ x ] = U [ z ]
580 * and
581 * [ 1 ] [ 1 ]
582 * [ z ] = Q [ x ]
584 * with Q = U^{-1}
585 * Since z_1 is zero, we can drop this variable as well as the corresponding
586 * column of U to obtain
588 * [ 1 ] [ 1 ]
589 * [ x ] = U' [ z' ]
590 * and
591 * [ 1 ] [ 1 ]
592 * [ z' ] = Q' [ x ]
594 * with Q' equal to Q, but without the corresponding row.
595 * After computing the facets of the facet in the z' space,
596 * we convert them back to the x space through Q.
598 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
600 struct isl_mat *m, *U, *Q;
601 struct isl_basic_set *facet = NULL;
602 struct isl_ctx *ctx;
603 unsigned dim;
605 ctx = set->ctx;
606 set = isl_set_copy(set);
607 dim = isl_set_n_dim(set);
608 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
609 if (!m)
610 goto error;
611 isl_int_set_si(m->row[0][0], 1);
612 isl_seq_clr(m->row[0]+1, dim);
613 isl_seq_cpy(m->row[1], c, 1+dim);
614 U = isl_mat_right_inverse(m);
615 Q = isl_mat_right_inverse(isl_mat_copy(U));
616 U = isl_mat_drop_cols(U, 1, 1);
617 Q = isl_mat_drop_rows(Q, 1, 1);
618 set = isl_set_preimage(set, U);
619 facet = uset_convex_hull_wrap_bounded(set);
620 facet = isl_basic_set_preimage(facet, Q);
621 if (facet && facet->n_eq != 0)
622 isl_die(ctx, isl_error_internal, "unexpected equality",
623 return isl_basic_set_free(facet));
624 return facet;
625 error:
626 isl_basic_set_free(facet);
627 isl_set_free(set);
628 return NULL;
631 /* Given an initial facet constraint, compute the remaining facets.
632 * We do this by running through all facets found so far and computing
633 * the adjacent facets through wrapping, adding those facets that we
634 * hadn't already found before.
636 * For each facet we have found so far, we first compute its facets
637 * in the resulting convex hull. That is, we compute the ridges
638 * of the resulting convex hull contained in the facet.
639 * We also compute the corresponding facet in the current approximation
640 * of the convex hull. There is no need to wrap around the ridges
641 * in this facet since that would result in a facet that is already
642 * present in the current approximation.
644 * This function can still be significantly optimized by checking which of
645 * the facets of the basic sets are also facets of the convex hull and
646 * using all the facets so far to help in constructing the facets of the
647 * facets
648 * and/or
649 * using the technique in section "3.1 Ridge Generation" of
650 * "Extended Convex Hull" by Fukuda et al.
652 static struct isl_basic_set *extend(struct isl_basic_set *hull,
653 struct isl_set *set)
655 int i, j, f;
656 int k;
657 struct isl_basic_set *facet = NULL;
658 struct isl_basic_set *hull_facet = NULL;
659 unsigned dim;
661 if (!hull)
662 return NULL;
664 isl_assert(set->ctx, set->n > 0, goto error);
666 dim = isl_set_n_dim(set);
668 for (i = 0; i < hull->n_ineq; ++i) {
669 facet = compute_facet(set, hull->ineq[i]);
670 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
671 facet = isl_basic_set_gauss(facet, NULL);
672 facet = isl_basic_set_normalize_constraints(facet);
673 hull_facet = isl_basic_set_copy(hull);
674 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
675 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
676 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
677 if (!facet || !hull_facet)
678 goto error;
679 hull = isl_basic_set_cow(hull);
680 hull = isl_basic_set_extend_space(hull,
681 isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
682 if (!hull)
683 goto error;
684 for (j = 0; j < facet->n_ineq; ++j) {
685 for (f = 0; f < hull_facet->n_ineq; ++f)
686 if (isl_seq_eq(facet->ineq[j],
687 hull_facet->ineq[f], 1 + dim))
688 break;
689 if (f < hull_facet->n_ineq)
690 continue;
691 k = isl_basic_set_alloc_inequality(hull);
692 if (k < 0)
693 goto error;
694 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
695 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
696 goto error;
698 isl_basic_set_free(hull_facet);
699 isl_basic_set_free(facet);
701 hull = isl_basic_set_simplify(hull);
702 hull = isl_basic_set_finalize(hull);
703 return hull;
704 error:
705 isl_basic_set_free(hull_facet);
706 isl_basic_set_free(facet);
707 isl_basic_set_free(hull);
708 return NULL;
711 /* Special case for computing the convex hull of a one dimensional set.
712 * We simply collect the lower and upper bounds of each basic set
713 * and the biggest of those.
715 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
717 struct isl_mat *c = NULL;
718 isl_int *lower = NULL;
719 isl_int *upper = NULL;
720 int i, j, k;
721 isl_int a, b;
722 struct isl_basic_set *hull;
724 for (i = 0; i < set->n; ++i) {
725 set->p[i] = isl_basic_set_simplify(set->p[i]);
726 if (!set->p[i])
727 goto error;
729 set = isl_set_remove_empty_parts(set);
730 if (!set)
731 goto error;
732 isl_assert(set->ctx, set->n > 0, goto error);
733 c = isl_mat_alloc(set->ctx, 2, 2);
734 if (!c)
735 goto error;
737 if (set->p[0]->n_eq > 0) {
738 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
739 lower = c->row[0];
740 upper = c->row[1];
741 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
742 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
743 isl_seq_neg(upper, set->p[0]->eq[0], 2);
744 } else {
745 isl_seq_neg(lower, set->p[0]->eq[0], 2);
746 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
748 } else {
749 for (j = 0; j < set->p[0]->n_ineq; ++j) {
750 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
751 lower = c->row[0];
752 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
753 } else {
754 upper = c->row[1];
755 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
760 isl_int_init(a);
761 isl_int_init(b);
762 for (i = 0; i < set->n; ++i) {
763 struct isl_basic_set *bset = set->p[i];
764 int has_lower = 0;
765 int has_upper = 0;
767 for (j = 0; j < bset->n_eq; ++j) {
768 has_lower = 1;
769 has_upper = 1;
770 if (lower) {
771 isl_int_mul(a, lower[0], bset->eq[j][1]);
772 isl_int_mul(b, lower[1], bset->eq[j][0]);
773 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
774 isl_seq_cpy(lower, bset->eq[j], 2);
775 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
776 isl_seq_neg(lower, bset->eq[j], 2);
778 if (upper) {
779 isl_int_mul(a, upper[0], bset->eq[j][1]);
780 isl_int_mul(b, upper[1], bset->eq[j][0]);
781 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
782 isl_seq_neg(upper, bset->eq[j], 2);
783 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
784 isl_seq_cpy(upper, bset->eq[j], 2);
787 for (j = 0; j < bset->n_ineq; ++j) {
788 if (isl_int_is_pos(bset->ineq[j][1]))
789 has_lower = 1;
790 if (isl_int_is_neg(bset->ineq[j][1]))
791 has_upper = 1;
792 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
793 isl_int_mul(a, lower[0], bset->ineq[j][1]);
794 isl_int_mul(b, lower[1], bset->ineq[j][0]);
795 if (isl_int_lt(a, b))
796 isl_seq_cpy(lower, bset->ineq[j], 2);
798 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
799 isl_int_mul(a, upper[0], bset->ineq[j][1]);
800 isl_int_mul(b, upper[1], bset->ineq[j][0]);
801 if (isl_int_gt(a, b))
802 isl_seq_cpy(upper, bset->ineq[j], 2);
805 if (!has_lower)
806 lower = NULL;
807 if (!has_upper)
808 upper = NULL;
810 isl_int_clear(a);
811 isl_int_clear(b);
813 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
814 hull = isl_basic_set_set_rational(hull);
815 if (!hull)
816 goto error;
817 if (lower) {
818 k = isl_basic_set_alloc_inequality(hull);
819 isl_seq_cpy(hull->ineq[k], lower, 2);
821 if (upper) {
822 k = isl_basic_set_alloc_inequality(hull);
823 isl_seq_cpy(hull->ineq[k], upper, 2);
825 hull = isl_basic_set_finalize(hull);
826 isl_set_free(set);
827 isl_mat_free(c);
828 return hull;
829 error:
830 isl_set_free(set);
831 isl_mat_free(c);
832 return NULL;
835 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
837 struct isl_basic_set *convex_hull;
839 if (!set)
840 return NULL;
842 if (isl_set_is_empty(set))
843 convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
844 else
845 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
846 isl_set_free(set);
847 return convex_hull;
850 /* Compute the convex hull of a pair of basic sets without any parameters or
851 * integer divisions using Fourier-Motzkin elimination.
852 * The convex hull is the set of all points that can be written as
853 * the sum of points from both basic sets (in homogeneous coordinates).
854 * We set up the constraints in a space with dimensions for each of
855 * the three sets and then project out the dimensions corresponding
856 * to the two original basic sets, retaining only those corresponding
857 * to the convex hull.
859 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
860 struct isl_basic_set *bset2)
862 int i, j, k;
863 struct isl_basic_set *bset[2];
864 struct isl_basic_set *hull = NULL;
865 unsigned dim;
867 if (!bset1 || !bset2)
868 goto error;
870 dim = isl_basic_set_n_dim(bset1);
871 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
872 1 + dim + bset1->n_eq + bset2->n_eq,
873 2 + bset1->n_ineq + bset2->n_ineq);
874 bset[0] = bset1;
875 bset[1] = bset2;
876 for (i = 0; i < 2; ++i) {
877 for (j = 0; j < bset[i]->n_eq; ++j) {
878 k = isl_basic_set_alloc_equality(hull);
879 if (k < 0)
880 goto error;
881 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
882 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
883 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
884 1+dim);
886 for (j = 0; j < bset[i]->n_ineq; ++j) {
887 k = isl_basic_set_alloc_inequality(hull);
888 if (k < 0)
889 goto error;
890 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
891 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
892 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
893 bset[i]->ineq[j], 1+dim);
895 k = isl_basic_set_alloc_inequality(hull);
896 if (k < 0)
897 goto error;
898 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
899 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
901 for (j = 0; j < 1+dim; ++j) {
902 k = isl_basic_set_alloc_equality(hull);
903 if (k < 0)
904 goto error;
905 isl_seq_clr(hull->eq[k], 1+2+3*dim);
906 isl_int_set_si(hull->eq[k][j], -1);
907 isl_int_set_si(hull->eq[k][1+dim+j], 1);
908 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
910 hull = isl_basic_set_set_rational(hull);
911 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
912 hull = isl_basic_set_remove_redundancies(hull);
913 isl_basic_set_free(bset1);
914 isl_basic_set_free(bset2);
915 return hull;
916 error:
917 isl_basic_set_free(bset1);
918 isl_basic_set_free(bset2);
919 isl_basic_set_free(hull);
920 return NULL;
923 /* Is the set bounded for each value of the parameters?
925 isl_bool isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
927 struct isl_tab *tab;
928 isl_bool bounded;
930 if (!bset)
931 return isl_bool_error;
932 if (isl_basic_set_plain_is_empty(bset))
933 return isl_bool_true;
935 tab = isl_tab_from_recession_cone(bset, 1);
936 bounded = isl_tab_cone_is_bounded(tab);
937 isl_tab_free(tab);
938 return bounded;
941 /* Is the image bounded for each value of the parameters and
942 * the domain variables?
944 isl_bool isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
946 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
947 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
948 isl_bool bounded;
950 bmap = isl_basic_map_copy(bmap);
951 bmap = isl_basic_map_cow(bmap);
952 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
953 isl_dim_in, 0, n_in);
954 bounded = isl_basic_set_is_bounded(bset_from_bmap(bmap));
955 isl_basic_map_free(bmap);
957 return bounded;
960 /* Is the set bounded for each value of the parameters?
962 isl_bool isl_set_is_bounded(__isl_keep isl_set *set)
964 int i;
966 if (!set)
967 return isl_bool_error;
969 for (i = 0; i < set->n; ++i) {
970 isl_bool bounded = isl_basic_set_is_bounded(set->p[i]);
971 if (!bounded || bounded < 0)
972 return bounded;
974 return isl_bool_true;
977 /* Compute the lineality space of the convex hull of bset1 and bset2.
979 * We first compute the intersection of the recession cone of bset1
980 * with the negative of the recession cone of bset2 and then compute
981 * the linear hull of the resulting cone.
983 static struct isl_basic_set *induced_lineality_space(
984 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
986 int i, k;
987 struct isl_basic_set *lin = NULL;
988 unsigned dim;
990 if (!bset1 || !bset2)
991 goto error;
993 dim = isl_basic_set_total_dim(bset1);
994 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
995 bset1->n_eq + bset2->n_eq,
996 bset1->n_ineq + bset2->n_ineq);
997 lin = isl_basic_set_set_rational(lin);
998 if (!lin)
999 goto error;
1000 for (i = 0; i < bset1->n_eq; ++i) {
1001 k = isl_basic_set_alloc_equality(lin);
1002 if (k < 0)
1003 goto error;
1004 isl_int_set_si(lin->eq[k][0], 0);
1005 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
1007 for (i = 0; i < bset1->n_ineq; ++i) {
1008 k = isl_basic_set_alloc_inequality(lin);
1009 if (k < 0)
1010 goto error;
1011 isl_int_set_si(lin->ineq[k][0], 0);
1012 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
1014 for (i = 0; i < bset2->n_eq; ++i) {
1015 k = isl_basic_set_alloc_equality(lin);
1016 if (k < 0)
1017 goto error;
1018 isl_int_set_si(lin->eq[k][0], 0);
1019 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
1021 for (i = 0; i < bset2->n_ineq; ++i) {
1022 k = isl_basic_set_alloc_inequality(lin);
1023 if (k < 0)
1024 goto error;
1025 isl_int_set_si(lin->ineq[k][0], 0);
1026 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
1029 isl_basic_set_free(bset1);
1030 isl_basic_set_free(bset2);
1031 return isl_basic_set_affine_hull(lin);
1032 error:
1033 isl_basic_set_free(lin);
1034 isl_basic_set_free(bset1);
1035 isl_basic_set_free(bset2);
1036 return NULL;
1039 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1041 /* Given a set and a linear space "lin" of dimension n > 0,
1042 * project the linear space from the set, compute the convex hull
1043 * and then map the set back to the original space.
1045 * Let
1047 * M x = 0
1049 * describe the linear space. We first compute the Hermite normal
1050 * form H = M U of M = H Q, to obtain
1052 * H Q x = 0
1054 * The last n rows of H will be zero, so the last n variables of x' = Q x
1055 * are the one we want to project out. We do this by transforming each
1056 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1057 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1058 * we transform the hull back to the original space as A' Q_1 x >= b',
1059 * with Q_1 all but the last n rows of Q.
1061 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1062 struct isl_basic_set *lin)
1064 unsigned total = isl_basic_set_total_dim(lin);
1065 unsigned lin_dim;
1066 struct isl_basic_set *hull;
1067 struct isl_mat *M, *U, *Q;
1069 if (!set || !lin)
1070 goto error;
1071 lin_dim = total - lin->n_eq;
1072 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1073 M = isl_mat_left_hermite(M, 0, &U, &Q);
1074 if (!M)
1075 goto error;
1076 isl_mat_free(M);
1077 isl_basic_set_free(lin);
1079 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1081 U = isl_mat_lin_to_aff(U);
1082 Q = isl_mat_lin_to_aff(Q);
1084 set = isl_set_preimage(set, U);
1085 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1086 hull = uset_convex_hull(set);
1087 hull = isl_basic_set_preimage(hull, Q);
1089 return hull;
1090 error:
1091 isl_basic_set_free(lin);
1092 isl_set_free(set);
1093 return NULL;
1096 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1097 * set up an LP for solving
1099 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1101 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1102 * The next \alpha{ij} correspond to the equalities and come in pairs.
1103 * The final \alpha{ij} correspond to the inequalities.
1105 static struct isl_basic_set *valid_direction_lp(
1106 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1108 isl_space *dim;
1109 struct isl_basic_set *lp;
1110 unsigned d;
1111 int n;
1112 int i, j, k;
1114 if (!bset1 || !bset2)
1115 goto error;
1116 d = 1 + isl_basic_set_total_dim(bset1);
1117 n = 2 +
1118 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1119 dim = isl_space_set_alloc(bset1->ctx, 0, n);
1120 lp = isl_basic_set_alloc_space(dim, 0, d, n);
1121 if (!lp)
1122 goto error;
1123 for (i = 0; i < n; ++i) {
1124 k = isl_basic_set_alloc_inequality(lp);
1125 if (k < 0)
1126 goto error;
1127 isl_seq_clr(lp->ineq[k] + 1, n);
1128 isl_int_set_si(lp->ineq[k][0], -1);
1129 isl_int_set_si(lp->ineq[k][1 + i], 1);
1131 for (i = 0; i < d; ++i) {
1132 k = isl_basic_set_alloc_equality(lp);
1133 if (k < 0)
1134 goto error;
1135 n = 0;
1136 isl_int_set_si(lp->eq[k][n], 0); n++;
1137 /* positivity constraint 1 >= 0 */
1138 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1139 for (j = 0; j < bset1->n_eq; ++j) {
1140 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1141 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1143 for (j = 0; j < bset1->n_ineq; ++j) {
1144 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1146 /* positivity constraint 1 >= 0 */
1147 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1148 for (j = 0; j < bset2->n_eq; ++j) {
1149 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1150 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1152 for (j = 0; j < bset2->n_ineq; ++j) {
1153 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1156 lp = isl_basic_set_gauss(lp, NULL);
1157 isl_basic_set_free(bset1);
1158 isl_basic_set_free(bset2);
1159 return lp;
1160 error:
1161 isl_basic_set_free(bset1);
1162 isl_basic_set_free(bset2);
1163 return NULL;
1166 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1167 * for all rays in the homogeneous space of the two cones that correspond
1168 * to the input polyhedra bset1 and bset2.
1170 * We compute s as a vector that satisfies
1172 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1174 * with h_{ij} the normals of the facets of polyhedron i
1175 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1176 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1177 * We first set up an LP with as variables the \alpha{ij}.
1178 * In this formulation, for each polyhedron i,
1179 * the first constraint is the positivity constraint, followed by pairs
1180 * of variables for the equalities, followed by variables for the inequalities.
1181 * We then simply pick a feasible solution and compute s using (*).
1183 * Note that we simply pick any valid direction and make no attempt
1184 * to pick a "good" or even the "best" valid direction.
1186 static struct isl_vec *valid_direction(
1187 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1189 struct isl_basic_set *lp;
1190 struct isl_tab *tab;
1191 struct isl_vec *sample = NULL;
1192 struct isl_vec *dir;
1193 unsigned d;
1194 int i;
1195 int n;
1197 if (!bset1 || !bset2)
1198 goto error;
1199 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1200 isl_basic_set_copy(bset2));
1201 tab = isl_tab_from_basic_set(lp, 0);
1202 sample = isl_tab_get_sample_value(tab);
1203 isl_tab_free(tab);
1204 isl_basic_set_free(lp);
1205 if (!sample)
1206 goto error;
1207 d = isl_basic_set_total_dim(bset1);
1208 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1209 if (!dir)
1210 goto error;
1211 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1212 n = 1;
1213 /* positivity constraint 1 >= 0 */
1214 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1215 for (i = 0; i < bset1->n_eq; ++i) {
1216 isl_int_sub(sample->block.data[n],
1217 sample->block.data[n], sample->block.data[n+1]);
1218 isl_seq_combine(dir->block.data,
1219 bset1->ctx->one, dir->block.data,
1220 sample->block.data[n], bset1->eq[i], 1 + d);
1222 n += 2;
1224 for (i = 0; i < bset1->n_ineq; ++i)
1225 isl_seq_combine(dir->block.data,
1226 bset1->ctx->one, dir->block.data,
1227 sample->block.data[n++], bset1->ineq[i], 1 + d);
1228 isl_vec_free(sample);
1229 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1230 isl_basic_set_free(bset1);
1231 isl_basic_set_free(bset2);
1232 return dir;
1233 error:
1234 isl_vec_free(sample);
1235 isl_basic_set_free(bset1);
1236 isl_basic_set_free(bset2);
1237 return NULL;
1240 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1241 * compute b_i' + A_i' x' >= 0, with
1243 * [ b_i A_i ] [ y' ] [ y' ]
1244 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1246 * In particular, add the "positivity constraint" and then perform
1247 * the mapping.
1249 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1250 struct isl_mat *T)
1252 int k;
1254 if (!bset)
1255 goto error;
1256 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1257 k = isl_basic_set_alloc_inequality(bset);
1258 if (k < 0)
1259 goto error;
1260 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1261 isl_int_set_si(bset->ineq[k][0], 1);
1262 bset = isl_basic_set_preimage(bset, T);
1263 return bset;
1264 error:
1265 isl_mat_free(T);
1266 isl_basic_set_free(bset);
1267 return NULL;
1270 /* Compute the convex hull of a pair of basic sets without any parameters or
1271 * integer divisions, where the convex hull is known to be pointed,
1272 * but the basic sets may be unbounded.
1274 * We turn this problem into the computation of a convex hull of a pair
1275 * _bounded_ polyhedra by "changing the direction of the homogeneous
1276 * dimension". This idea is due to Matthias Koeppe.
1278 * Consider the cones in homogeneous space that correspond to the
1279 * input polyhedra. The rays of these cones are also rays of the
1280 * polyhedra if the coordinate that corresponds to the homogeneous
1281 * dimension is zero. That is, if the inner product of the rays
1282 * with the homogeneous direction is zero.
1283 * The cones in the homogeneous space can also be considered to
1284 * correspond to other pairs of polyhedra by chosing a different
1285 * homogeneous direction. To ensure that both of these polyhedra
1286 * are bounded, we need to make sure that all rays of the cones
1287 * correspond to vertices and not to rays.
1288 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1289 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1290 * The vector s is computed in valid_direction.
1292 * Note that we need to consider _all_ rays of the cones and not just
1293 * the rays that correspond to rays in the polyhedra. If we were to
1294 * only consider those rays and turn them into vertices, then we
1295 * may inadvertently turn some vertices into rays.
1297 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1298 * We therefore transform the two polyhedra such that the selected
1299 * direction is mapped onto this standard direction and then proceed
1300 * with the normal computation.
1301 * Let S be a non-singular square matrix with s as its first row,
1302 * then we want to map the polyhedra to the space
1304 * [ y' ] [ y ] [ y ] [ y' ]
1305 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1307 * We take S to be the unimodular completion of s to limit the growth
1308 * of the coefficients in the following computations.
1310 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1311 * We first move to the homogeneous dimension
1313 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1314 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1316 * Then we change directoin
1318 * [ b_i A_i ] [ y' ] [ y' ]
1319 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1321 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1322 * resulting in b' + A' x' >= 0, which we then convert back
1324 * [ y ] [ y ]
1325 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1327 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1329 static struct isl_basic_set *convex_hull_pair_pointed(
1330 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1332 struct isl_ctx *ctx = NULL;
1333 struct isl_vec *dir = NULL;
1334 struct isl_mat *T = NULL;
1335 struct isl_mat *T2 = NULL;
1336 struct isl_basic_set *hull;
1337 struct isl_set *set;
1339 if (!bset1 || !bset2)
1340 goto error;
1341 ctx = isl_basic_set_get_ctx(bset1);
1342 dir = valid_direction(isl_basic_set_copy(bset1),
1343 isl_basic_set_copy(bset2));
1344 if (!dir)
1345 goto error;
1346 T = isl_mat_alloc(ctx, dir->size, dir->size);
1347 if (!T)
1348 goto error;
1349 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1350 T = isl_mat_unimodular_complete(T, 1);
1351 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1353 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1354 bset2 = homogeneous_map(bset2, T2);
1355 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1356 set = isl_set_add_basic_set(set, bset1);
1357 set = isl_set_add_basic_set(set, bset2);
1358 hull = uset_convex_hull(set);
1359 hull = isl_basic_set_preimage(hull, T);
1361 isl_vec_free(dir);
1363 return hull;
1364 error:
1365 isl_vec_free(dir);
1366 isl_basic_set_free(bset1);
1367 isl_basic_set_free(bset2);
1368 return NULL;
1371 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1372 static struct isl_basic_set *modulo_affine_hull(
1373 struct isl_set *set, struct isl_basic_set *affine_hull);
1375 /* Compute the convex hull of a pair of basic sets without any parameters or
1376 * integer divisions.
1378 * This function is called from uset_convex_hull_unbounded, which
1379 * means that the complete convex hull is unbounded. Some pairs
1380 * of basic sets may still be bounded, though.
1381 * They may even lie inside a lower dimensional space, in which
1382 * case they need to be handled inside their affine hull since
1383 * the main algorithm assumes that the result is full-dimensional.
1385 * If the convex hull of the two basic sets would have a non-trivial
1386 * lineality space, we first project out this lineality space.
1388 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1389 struct isl_basic_set *bset2)
1391 isl_basic_set *lin, *aff;
1392 int bounded1, bounded2;
1394 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1395 return convex_hull_pair_elim(bset1, bset2);
1397 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1398 isl_basic_set_copy(bset2)));
1399 if (!aff)
1400 goto error;
1401 if (aff->n_eq != 0)
1402 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1403 isl_basic_set_free(aff);
1405 bounded1 = isl_basic_set_is_bounded(bset1);
1406 bounded2 = isl_basic_set_is_bounded(bset2);
1408 if (bounded1 < 0 || bounded2 < 0)
1409 goto error;
1411 if (bounded1 && bounded2)
1412 return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1414 if (bounded1 || bounded2)
1415 return convex_hull_pair_pointed(bset1, bset2);
1417 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1418 isl_basic_set_copy(bset2));
1419 if (!lin)
1420 goto error;
1421 if (isl_basic_set_plain_is_universe(lin)) {
1422 isl_basic_set_free(bset1);
1423 isl_basic_set_free(bset2);
1424 return lin;
1426 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1427 struct isl_set *set;
1428 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1429 set = isl_set_add_basic_set(set, bset1);
1430 set = isl_set_add_basic_set(set, bset2);
1431 return modulo_lineality(set, lin);
1433 isl_basic_set_free(lin);
1435 return convex_hull_pair_pointed(bset1, bset2);
1436 error:
1437 isl_basic_set_free(bset1);
1438 isl_basic_set_free(bset2);
1439 return NULL;
1442 /* Compute the lineality space of a basic set.
1443 * We currently do not allow the basic set to have any divs.
1444 * We basically just drop the constants and turn every inequality
1445 * into an equality.
1447 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1449 int i, k;
1450 struct isl_basic_set *lin = NULL;
1451 unsigned dim;
1453 if (!bset)
1454 goto error;
1455 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1456 dim = isl_basic_set_total_dim(bset);
1458 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, dim, 0);
1459 if (!lin)
1460 goto error;
1461 for (i = 0; i < bset->n_eq; ++i) {
1462 k = isl_basic_set_alloc_equality(lin);
1463 if (k < 0)
1464 goto error;
1465 isl_int_set_si(lin->eq[k][0], 0);
1466 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1468 lin = isl_basic_set_gauss(lin, NULL);
1469 if (!lin)
1470 goto error;
1471 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1472 k = isl_basic_set_alloc_equality(lin);
1473 if (k < 0)
1474 goto error;
1475 isl_int_set_si(lin->eq[k][0], 0);
1476 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1477 lin = isl_basic_set_gauss(lin, NULL);
1478 if (!lin)
1479 goto error;
1481 isl_basic_set_free(bset);
1482 return lin;
1483 error:
1484 isl_basic_set_free(lin);
1485 isl_basic_set_free(bset);
1486 return NULL;
1489 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1490 * "underlying" set "set".
1492 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1494 int i;
1495 struct isl_set *lin = NULL;
1497 if (!set)
1498 return NULL;
1499 if (set->n == 0) {
1500 isl_space *dim = isl_set_get_space(set);
1501 isl_set_free(set);
1502 return isl_basic_set_empty(dim);
1505 lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
1506 for (i = 0; i < set->n; ++i)
1507 lin = isl_set_add_basic_set(lin,
1508 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1509 isl_set_free(set);
1510 return isl_set_affine_hull(lin);
1513 /* Compute the convex hull of a set without any parameters or
1514 * integer divisions.
1515 * In each step, we combined two basic sets until only one
1516 * basic set is left.
1517 * The input basic sets are assumed not to have a non-trivial
1518 * lineality space. If any of the intermediate results has
1519 * a non-trivial lineality space, it is projected out.
1521 static __isl_give isl_basic_set *uset_convex_hull_unbounded(
1522 __isl_take isl_set *set)
1524 isl_basic_set_list *list;
1526 list = isl_set_get_basic_set_list(set);
1527 isl_set_free(set);
1529 while (list) {
1530 int n;
1531 struct isl_basic_set *t;
1532 isl_basic_set *bset1, *bset2;
1534 n = isl_basic_set_list_n_basic_set(list);
1535 if (n < 2)
1536 isl_die(isl_basic_set_list_get_ctx(list),
1537 isl_error_internal,
1538 "expecting at least two elements", goto error);
1539 bset1 = isl_basic_set_list_get_basic_set(list, n - 1);
1540 bset2 = isl_basic_set_list_get_basic_set(list, n - 2);
1541 bset1 = convex_hull_pair(bset1, bset2);
1542 if (n == 2) {
1543 isl_basic_set_list_free(list);
1544 return bset1;
1546 bset1 = isl_basic_set_underlying_set(bset1);
1547 list = isl_basic_set_list_drop(list, n - 2, 2);
1548 list = isl_basic_set_list_add(list, bset1);
1550 t = isl_basic_set_list_get_basic_set(list, n - 2);
1551 t = isl_basic_set_lineality_space(t);
1552 if (!t)
1553 goto error;
1554 if (isl_basic_set_plain_is_universe(t)) {
1555 isl_basic_set_list_free(list);
1556 return t;
1558 if (t->n_eq < isl_basic_set_total_dim(t)) {
1559 set = isl_basic_set_list_union(list);
1560 return modulo_lineality(set, t);
1562 isl_basic_set_free(t);
1565 return NULL;
1566 error:
1567 isl_basic_set_list_free(list);
1568 return NULL;
1571 /* Compute an initial hull for wrapping containing a single initial
1572 * facet.
1573 * This function assumes that the given set is bounded.
1575 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1576 struct isl_set *set)
1578 struct isl_mat *bounds = NULL;
1579 unsigned dim;
1580 int k;
1582 if (!hull)
1583 goto error;
1584 bounds = initial_facet_constraint(set);
1585 if (!bounds)
1586 goto error;
1587 k = isl_basic_set_alloc_inequality(hull);
1588 if (k < 0)
1589 goto error;
1590 dim = isl_set_n_dim(set);
1591 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1592 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1593 isl_mat_free(bounds);
1595 return hull;
1596 error:
1597 isl_basic_set_free(hull);
1598 isl_mat_free(bounds);
1599 return NULL;
1602 struct max_constraint {
1603 struct isl_mat *c;
1604 int count;
1605 int ineq;
1608 static int max_constraint_equal(const void *entry, const void *val)
1610 struct max_constraint *a = (struct max_constraint *)entry;
1611 isl_int *b = (isl_int *)val;
1613 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1616 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1617 isl_int *con, unsigned len, int n, int ineq)
1619 struct isl_hash_table_entry *entry;
1620 struct max_constraint *c;
1621 uint32_t c_hash;
1623 c_hash = isl_seq_get_hash(con + 1, len);
1624 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1625 con + 1, 0);
1626 if (!entry)
1627 return;
1628 c = entry->data;
1629 if (c->count < n) {
1630 isl_hash_table_remove(ctx, table, entry);
1631 return;
1633 c->count++;
1634 if (isl_int_gt(c->c->row[0][0], con[0]))
1635 return;
1636 if (isl_int_eq(c->c->row[0][0], con[0])) {
1637 if (ineq)
1638 c->ineq = ineq;
1639 return;
1641 c->c = isl_mat_cow(c->c);
1642 isl_int_set(c->c->row[0][0], con[0]);
1643 c->ineq = ineq;
1646 /* Check whether the constraint hash table "table" constains the constraint
1647 * "con".
1649 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1650 isl_int *con, unsigned len, int n)
1652 struct isl_hash_table_entry *entry;
1653 struct max_constraint *c;
1654 uint32_t c_hash;
1656 c_hash = isl_seq_get_hash(con + 1, len);
1657 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1658 con + 1, 0);
1659 if (!entry)
1660 return 0;
1661 c = entry->data;
1662 if (c->count < n)
1663 return 0;
1664 return isl_int_eq(c->c->row[0][0], con[0]);
1667 /* Check for inequality constraints of a basic set without equalities
1668 * such that the same or more stringent copies of the constraint appear
1669 * in all of the basic sets. Such constraints are necessarily facet
1670 * constraints of the convex hull.
1672 * If the resulting basic set is by chance identical to one of
1673 * the basic sets in "set", then we know that this basic set contains
1674 * all other basic sets and is therefore the convex hull of set.
1675 * In this case we set *is_hull to 1.
1677 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1678 struct isl_set *set, int *is_hull)
1680 int i, j, s, n;
1681 int min_constraints;
1682 int best;
1683 struct max_constraint *constraints = NULL;
1684 struct isl_hash_table *table = NULL;
1685 unsigned total;
1687 *is_hull = 0;
1689 for (i = 0; i < set->n; ++i)
1690 if (set->p[i]->n_eq == 0)
1691 break;
1692 if (i >= set->n)
1693 return hull;
1694 min_constraints = set->p[i]->n_ineq;
1695 best = i;
1696 for (i = best + 1; i < set->n; ++i) {
1697 if (set->p[i]->n_eq != 0)
1698 continue;
1699 if (set->p[i]->n_ineq >= min_constraints)
1700 continue;
1701 min_constraints = set->p[i]->n_ineq;
1702 best = i;
1704 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1705 min_constraints);
1706 if (!constraints)
1707 return hull;
1708 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1709 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1710 goto error;
1712 total = isl_space_dim(set->dim, isl_dim_all);
1713 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1714 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1715 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1716 if (!constraints[i].c)
1717 goto error;
1718 constraints[i].ineq = 1;
1720 for (i = 0; i < min_constraints; ++i) {
1721 struct isl_hash_table_entry *entry;
1722 uint32_t c_hash;
1723 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1724 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1725 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1726 if (!entry)
1727 goto error;
1728 isl_assert(hull->ctx, !entry->data, goto error);
1729 entry->data = &constraints[i];
1732 n = 0;
1733 for (s = 0; s < set->n; ++s) {
1734 if (s == best)
1735 continue;
1737 for (i = 0; i < set->p[s]->n_eq; ++i) {
1738 isl_int *eq = set->p[s]->eq[i];
1739 for (j = 0; j < 2; ++j) {
1740 isl_seq_neg(eq, eq, 1 + total);
1741 update_constraint(hull->ctx, table,
1742 eq, total, n, 0);
1745 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1746 isl_int *ineq = set->p[s]->ineq[i];
1747 update_constraint(hull->ctx, table, ineq, total, n,
1748 set->p[s]->n_eq == 0);
1750 ++n;
1753 for (i = 0; i < min_constraints; ++i) {
1754 if (constraints[i].count < n)
1755 continue;
1756 if (!constraints[i].ineq)
1757 continue;
1758 j = isl_basic_set_alloc_inequality(hull);
1759 if (j < 0)
1760 goto error;
1761 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1764 for (s = 0; s < set->n; ++s) {
1765 if (set->p[s]->n_eq)
1766 continue;
1767 if (set->p[s]->n_ineq != hull->n_ineq)
1768 continue;
1769 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1770 isl_int *ineq = set->p[s]->ineq[i];
1771 if (!has_constraint(hull->ctx, table, ineq, total, n))
1772 break;
1774 if (i == set->p[s]->n_ineq)
1775 *is_hull = 1;
1778 isl_hash_table_clear(table);
1779 for (i = 0; i < min_constraints; ++i)
1780 isl_mat_free(constraints[i].c);
1781 free(constraints);
1782 free(table);
1783 return hull;
1784 error:
1785 isl_hash_table_clear(table);
1786 free(table);
1787 if (constraints)
1788 for (i = 0; i < min_constraints; ++i)
1789 isl_mat_free(constraints[i].c);
1790 free(constraints);
1791 return hull;
1794 /* Create a template for the convex hull of "set" and fill it up
1795 * obvious facet constraints, if any. If the result happens to
1796 * be the convex hull of "set" then *is_hull is set to 1.
1798 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1800 struct isl_basic_set *hull;
1801 unsigned n_ineq;
1802 int i;
1804 n_ineq = 1;
1805 for (i = 0; i < set->n; ++i) {
1806 n_ineq += set->p[i]->n_eq;
1807 n_ineq += set->p[i]->n_ineq;
1809 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1810 hull = isl_basic_set_set_rational(hull);
1811 if (!hull)
1812 return NULL;
1813 return common_constraints(hull, set, is_hull);
1816 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1818 struct isl_basic_set *hull;
1819 int is_hull;
1821 hull = proto_hull(set, &is_hull);
1822 if (hull && !is_hull) {
1823 if (hull->n_ineq == 0)
1824 hull = initial_hull(hull, set);
1825 hull = extend(hull, set);
1827 isl_set_free(set);
1829 return hull;
1832 /* Compute the convex hull of a set without any parameters or
1833 * integer divisions. Depending on whether the set is bounded,
1834 * we pass control to the wrapping based convex hull or
1835 * the Fourier-Motzkin elimination based convex hull.
1836 * We also handle a few special cases before checking the boundedness.
1838 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1840 isl_bool bounded;
1841 struct isl_basic_set *convex_hull = NULL;
1842 struct isl_basic_set *lin;
1844 if (isl_set_n_dim(set) == 0)
1845 return convex_hull_0d(set);
1847 set = isl_set_coalesce(set);
1848 set = isl_set_set_rational(set);
1850 if (!set)
1851 goto error;
1852 if (!set)
1853 return NULL;
1854 if (set->n == 1) {
1855 convex_hull = isl_basic_set_copy(set->p[0]);
1856 isl_set_free(set);
1857 return convex_hull;
1859 if (isl_set_n_dim(set) == 1)
1860 return convex_hull_1d(set);
1862 bounded = isl_set_is_bounded(set);
1863 if (bounded < 0)
1864 goto error;
1865 if (bounded && set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1866 return uset_convex_hull_wrap(set);
1868 lin = uset_combined_lineality_space(isl_set_copy(set));
1869 if (!lin)
1870 goto error;
1871 if (isl_basic_set_plain_is_universe(lin)) {
1872 isl_set_free(set);
1873 return lin;
1875 if (lin->n_eq < isl_basic_set_total_dim(lin))
1876 return modulo_lineality(set, lin);
1877 isl_basic_set_free(lin);
1879 return uset_convex_hull_unbounded(set);
1880 error:
1881 isl_set_free(set);
1882 isl_basic_set_free(convex_hull);
1883 return NULL;
1886 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1887 * without parameters or divs and where the convex hull of set is
1888 * known to be full-dimensional.
1890 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1892 struct isl_basic_set *convex_hull = NULL;
1894 if (!set)
1895 goto error;
1897 if (isl_set_n_dim(set) == 0) {
1898 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1899 isl_set_free(set);
1900 convex_hull = isl_basic_set_set_rational(convex_hull);
1901 return convex_hull;
1904 set = isl_set_set_rational(set);
1905 set = isl_set_coalesce(set);
1906 if (!set)
1907 goto error;
1908 if (set->n == 1) {
1909 convex_hull = isl_basic_set_copy(set->p[0]);
1910 isl_set_free(set);
1911 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1912 return convex_hull;
1914 if (isl_set_n_dim(set) == 1)
1915 return convex_hull_1d(set);
1917 return uset_convex_hull_wrap(set);
1918 error:
1919 isl_set_free(set);
1920 return NULL;
1923 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1924 * We first remove the equalities (transforming the set), compute the
1925 * convex hull of the transformed set and then add the equalities back
1926 * (after performing the inverse transformation.
1928 static struct isl_basic_set *modulo_affine_hull(
1929 struct isl_set *set, struct isl_basic_set *affine_hull)
1931 struct isl_mat *T;
1932 struct isl_mat *T2;
1933 struct isl_basic_set *dummy;
1934 struct isl_basic_set *convex_hull;
1936 dummy = isl_basic_set_remove_equalities(
1937 isl_basic_set_copy(affine_hull), &T, &T2);
1938 if (!dummy)
1939 goto error;
1940 isl_basic_set_free(dummy);
1941 set = isl_set_preimage(set, T);
1942 convex_hull = uset_convex_hull(set);
1943 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1944 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1945 return convex_hull;
1946 error:
1947 isl_basic_set_free(affine_hull);
1948 isl_set_free(set);
1949 return NULL;
1952 /* Return an empty basic map living in the same space as "map".
1954 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1955 __isl_take isl_map *map)
1957 isl_space *space;
1959 space = isl_map_get_space(map);
1960 isl_map_free(map);
1961 return isl_basic_map_empty(space);
1964 /* Compute the convex hull of a map.
1966 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1967 * specifically, the wrapping of facets to obtain new facets.
1969 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1971 struct isl_basic_set *bset;
1972 struct isl_basic_map *model = NULL;
1973 struct isl_basic_set *affine_hull = NULL;
1974 struct isl_basic_map *convex_hull = NULL;
1975 struct isl_set *set = NULL;
1977 map = isl_map_detect_equalities(map);
1978 map = isl_map_align_divs(map);
1979 if (!map)
1980 goto error;
1982 if (map->n == 0)
1983 return replace_map_by_empty_basic_map(map);
1985 model = isl_basic_map_copy(map->p[0]);
1986 set = isl_map_underlying_set(map);
1987 if (!set)
1988 goto error;
1990 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1991 if (!affine_hull)
1992 goto error;
1993 if (affine_hull->n_eq != 0)
1994 bset = modulo_affine_hull(set, affine_hull);
1995 else {
1996 isl_basic_set_free(affine_hull);
1997 bset = uset_convex_hull(set);
2000 convex_hull = isl_basic_map_overlying_set(bset, model);
2001 if (!convex_hull)
2002 return NULL;
2004 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
2005 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2006 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
2007 return convex_hull;
2008 error:
2009 isl_set_free(set);
2010 isl_basic_map_free(model);
2011 return NULL;
2014 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
2016 return bset_from_bmap(isl_map_convex_hull(set_to_map(set)));
2019 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
2021 isl_basic_map *hull;
2023 hull = isl_map_convex_hull(map);
2024 return isl_basic_map_remove_divs(hull);
2027 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
2029 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set)));
2032 struct sh_data_entry {
2033 struct isl_hash_table *table;
2034 struct isl_tab *tab;
2037 /* Holds the data needed during the simple hull computation.
2038 * In particular,
2039 * n the number of basic sets in the original set
2040 * hull_table a hash table of already computed constraints
2041 * in the simple hull
2042 * p for each basic set,
2043 * table a hash table of the constraints
2044 * tab the tableau corresponding to the basic set
2046 struct sh_data {
2047 struct isl_ctx *ctx;
2048 unsigned n;
2049 struct isl_hash_table *hull_table;
2050 struct sh_data_entry p[1];
2053 static void sh_data_free(struct sh_data *data)
2055 int i;
2057 if (!data)
2058 return;
2059 isl_hash_table_free(data->ctx, data->hull_table);
2060 for (i = 0; i < data->n; ++i) {
2061 isl_hash_table_free(data->ctx, data->p[i].table);
2062 isl_tab_free(data->p[i].tab);
2064 free(data);
2067 struct ineq_cmp_data {
2068 unsigned len;
2069 isl_int *p;
2072 static int has_ineq(const void *entry, const void *val)
2074 isl_int *row = (isl_int *)entry;
2075 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2077 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2078 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2081 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2082 isl_int *ineq, unsigned len)
2084 uint32_t c_hash;
2085 struct ineq_cmp_data v;
2086 struct isl_hash_table_entry *entry;
2088 v.len = len;
2089 v.p = ineq;
2090 c_hash = isl_seq_get_hash(ineq + 1, len);
2091 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2092 if (!entry)
2093 return - 1;
2094 entry->data = ineq;
2095 return 0;
2098 /* Fill hash table "table" with the constraints of "bset".
2099 * Equalities are added as two inequalities.
2100 * The value in the hash table is a pointer to the (in)equality of "bset".
2102 static int hash_basic_set(struct isl_hash_table *table,
2103 struct isl_basic_set *bset)
2105 int i, j;
2106 unsigned dim = isl_basic_set_total_dim(bset);
2108 for (i = 0; i < bset->n_eq; ++i) {
2109 for (j = 0; j < 2; ++j) {
2110 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2111 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2112 return -1;
2115 for (i = 0; i < bset->n_ineq; ++i) {
2116 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2117 return -1;
2119 return 0;
2122 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2124 struct sh_data *data;
2125 int i;
2127 data = isl_calloc(set->ctx, struct sh_data,
2128 sizeof(struct sh_data) +
2129 (set->n - 1) * sizeof(struct sh_data_entry));
2130 if (!data)
2131 return NULL;
2132 data->ctx = set->ctx;
2133 data->n = set->n;
2134 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2135 if (!data->hull_table)
2136 goto error;
2137 for (i = 0; i < set->n; ++i) {
2138 data->p[i].table = isl_hash_table_alloc(set->ctx,
2139 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2140 if (!data->p[i].table)
2141 goto error;
2142 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2143 goto error;
2145 return data;
2146 error:
2147 sh_data_free(data);
2148 return NULL;
2151 /* Check if inequality "ineq" is a bound for basic set "j" or if
2152 * it can be relaxed (by increasing the constant term) to become
2153 * a bound for that basic set. In the latter case, the constant
2154 * term is updated.
2155 * Relaxation of the constant term is only allowed if "shift" is set.
2157 * Return 1 if "ineq" is a bound
2158 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2159 * -1 if some error occurred
2161 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2162 isl_int *ineq, int shift)
2164 enum isl_lp_result res;
2165 isl_int opt;
2167 if (!data->p[j].tab) {
2168 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2169 if (!data->p[j].tab)
2170 return -1;
2173 isl_int_init(opt);
2175 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2176 &opt, NULL, 0);
2177 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2178 if (shift)
2179 isl_int_sub(ineq[0], ineq[0], opt);
2180 else
2181 res = isl_lp_unbounded;
2184 isl_int_clear(opt);
2186 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2187 res == isl_lp_unbounded ? 0 : -1;
2190 /* Set the constant term of "ineq" to the maximum of those of the constraints
2191 * in the basic sets of "set" following "i" that are parallel to "ineq".
2192 * That is, if any of the basic sets of "set" following "i" have a more
2193 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2194 * "c_hash" is the hash value of the linear part of "ineq".
2195 * "v" has been set up for use by has_ineq.
2197 * Note that the two inequality constraints corresponding to an equality are
2198 * represented by the same inequality constraint in data->p[j].table
2199 * (but with different hash values). This means the constraint (or at
2200 * least its constant term) may need to be temporarily negated to get
2201 * the actually hashed constraint.
2203 static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
2204 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2206 int j;
2207 isl_ctx *ctx;
2208 struct isl_hash_table_entry *entry;
2210 ctx = isl_set_get_ctx(set);
2211 for (j = i + 1; j < set->n; ++j) {
2212 int neg;
2213 isl_int *ineq_j;
2215 entry = isl_hash_table_find(ctx, data->p[j].table,
2216 c_hash, &has_ineq, v, 0);
2217 if (!entry)
2218 continue;
2220 ineq_j = entry->data;
2221 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2222 if (neg)
2223 isl_int_neg(ineq_j[0], ineq_j[0]);
2224 if (isl_int_gt(ineq_j[0], ineq[0]))
2225 isl_int_set(ineq[0], ineq_j[0]);
2226 if (neg)
2227 isl_int_neg(ineq_j[0], ineq_j[0]);
2231 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2232 * become a bound on the whole set. If so, add the (relaxed) inequality
2233 * to "hull". Relaxation is only allowed if "shift" is set.
2235 * We first check if "hull" already contains a translate of the inequality.
2236 * If so, we are done.
2237 * Then, we check if any of the previous basic sets contains a translate
2238 * of the inequality. If so, then we have already considered this
2239 * inequality and we are done.
2240 * Otherwise, for each basic set other than "i", we check if the inequality
2241 * is a bound on the basic set, but first replace the constant term
2242 * by the maximal value of any translate of the inequality in any
2243 * of the following basic sets.
2244 * For previous basic sets, we know that they do not contain a translate
2245 * of the inequality, so we directly call is_bound.
2246 * For following basic sets, we first check if a translate of the
2247 * inequality appears in its description. If so, the constant term
2248 * of the inequality has already been updated with respect to this
2249 * translate and the inequality is therefore known to be a bound
2250 * of this basic set.
2252 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2253 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
2254 int shift)
2256 uint32_t c_hash;
2257 struct ineq_cmp_data v;
2258 struct isl_hash_table_entry *entry;
2259 int j, k;
2261 if (!hull)
2262 return NULL;
2264 v.len = isl_basic_set_total_dim(hull);
2265 v.p = ineq;
2266 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2268 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2269 has_ineq, &v, 0);
2270 if (entry)
2271 return hull;
2273 for (j = 0; j < i; ++j) {
2274 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2275 c_hash, has_ineq, &v, 0);
2276 if (entry)
2277 break;
2279 if (j < i)
2280 return hull;
2282 k = isl_basic_set_alloc_inequality(hull);
2283 if (k < 0)
2284 goto error;
2285 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2287 set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
2288 for (j = 0; j < i; ++j) {
2289 int bound;
2290 bound = is_bound(data, set, j, hull->ineq[k], shift);
2291 if (bound < 0)
2292 goto error;
2293 if (!bound)
2294 break;
2296 if (j < i) {
2297 isl_basic_set_free_inequality(hull, 1);
2298 return hull;
2301 for (j = i + 1; j < set->n; ++j) {
2302 int bound;
2303 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2304 c_hash, has_ineq, &v, 0);
2305 if (entry)
2306 continue;
2307 bound = is_bound(data, set, j, hull->ineq[k], shift);
2308 if (bound < 0)
2309 goto error;
2310 if (!bound)
2311 break;
2313 if (j < set->n) {
2314 isl_basic_set_free_inequality(hull, 1);
2315 return hull;
2318 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2319 has_ineq, &v, 1);
2320 if (!entry)
2321 goto error;
2322 entry->data = hull->ineq[k];
2324 return hull;
2325 error:
2326 isl_basic_set_free(hull);
2327 return NULL;
2330 /* Check if any inequality from basic set "i" is or can be relaxed to
2331 * become a bound on the whole set. If so, add the (relaxed) inequality
2332 * to "hull". Relaxation is only allowed if "shift" is set.
2334 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2335 struct sh_data *data, struct isl_set *set, int i, int shift)
2337 int j, k;
2338 unsigned dim = isl_basic_set_total_dim(bset);
2340 for (j = 0; j < set->p[i]->n_eq; ++j) {
2341 for (k = 0; k < 2; ++k) {
2342 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2343 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2344 shift);
2347 for (j = 0; j < set->p[i]->n_ineq; ++j)
2348 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2349 return bset;
2352 /* Compute a superset of the convex hull of set that is described
2353 * by only (translates of) the constraints in the constituents of set.
2354 * Translation is only allowed if "shift" is set.
2356 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2357 int shift)
2359 struct sh_data *data = NULL;
2360 struct isl_basic_set *hull = NULL;
2361 unsigned n_ineq;
2362 int i;
2364 if (!set)
2365 return NULL;
2367 n_ineq = 0;
2368 for (i = 0; i < set->n; ++i) {
2369 if (!set->p[i])
2370 goto error;
2371 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2374 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2375 if (!hull)
2376 goto error;
2378 data = sh_data_alloc(set, n_ineq);
2379 if (!data)
2380 goto error;
2382 for (i = 0; i < set->n; ++i)
2383 hull = add_bounds(hull, data, set, i, shift);
2385 sh_data_free(data);
2386 isl_set_free(set);
2388 return hull;
2389 error:
2390 sh_data_free(data);
2391 isl_basic_set_free(hull);
2392 isl_set_free(set);
2393 return NULL;
2396 /* Compute a superset of the convex hull of map that is described
2397 * by only (translates of) the constraints in the constituents of map.
2398 * Handle trivial cases where map is NULL or contains at most one disjunct.
2400 static __isl_give isl_basic_map *map_simple_hull_trivial(
2401 __isl_take isl_map *map)
2403 isl_basic_map *hull;
2405 if (!map)
2406 return NULL;
2407 if (map->n == 0)
2408 return replace_map_by_empty_basic_map(map);
2410 hull = isl_basic_map_copy(map->p[0]);
2411 isl_map_free(map);
2412 return hull;
2415 /* Return a copy of the simple hull cached inside "map".
2416 * "shift" determines whether to return the cached unshifted or shifted
2417 * simple hull.
2419 static __isl_give isl_basic_map *cached_simple_hull(__isl_take isl_map *map,
2420 int shift)
2422 isl_basic_map *hull;
2424 hull = isl_basic_map_copy(map->cached_simple_hull[shift]);
2425 isl_map_free(map);
2427 return hull;
2430 /* Compute a superset of the convex hull of map that is described
2431 * by only (translates of) the constraints in the constituents of map.
2432 * Translation is only allowed if "shift" is set.
2434 * The constraints are sorted while removing redundant constraints
2435 * in order to indicate a preference of which constraints should
2436 * be preserved. In particular, pairs of constraints that are
2437 * sorted together are preferred to either both be preserved
2438 * or both be removed. The sorting is performed inside
2439 * isl_basic_map_remove_redundancies.
2441 * The result of the computation is stored in map->cached_simple_hull[shift]
2442 * such that it can be reused in subsequent calls. The cache is cleared
2443 * whenever the map is modified (in isl_map_cow).
2444 * Note that the results need to be stored in the input map for there
2445 * to be any chance that they may get reused. In particular, they
2446 * are stored in a copy of the input map that is saved before
2447 * the integer division alignment.
2449 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2450 int shift)
2452 struct isl_set *set = NULL;
2453 struct isl_basic_map *model = NULL;
2454 struct isl_basic_map *hull;
2455 struct isl_basic_map *affine_hull;
2456 struct isl_basic_set *bset = NULL;
2457 isl_map *input;
2459 if (!map || map->n <= 1)
2460 return map_simple_hull_trivial(map);
2462 if (map->cached_simple_hull[shift])
2463 return cached_simple_hull(map, shift);
2465 map = isl_map_detect_equalities(map);
2466 if (!map || map->n <= 1)
2467 return map_simple_hull_trivial(map);
2468 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2469 input = isl_map_copy(map);
2470 map = isl_map_align_divs(map);
2471 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2473 set = isl_map_underlying_set(map);
2475 bset = uset_simple_hull(set, shift);
2477 hull = isl_basic_map_overlying_set(bset, model);
2479 hull = isl_basic_map_intersect(hull, affine_hull);
2480 hull = isl_basic_map_remove_redundancies(hull);
2482 if (hull) {
2483 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2484 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2487 hull = isl_basic_map_finalize(hull);
2488 if (input)
2489 input->cached_simple_hull[shift] = isl_basic_map_copy(hull);
2490 isl_map_free(input);
2492 return hull;
2495 /* Compute a superset of the convex hull of map that is described
2496 * by only translates of the constraints in the constituents of map.
2498 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2500 return map_simple_hull(map, 1);
2503 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2505 return bset_from_bmap(isl_map_simple_hull(set_to_map(set)));
2508 /* Compute a superset of the convex hull of map that is described
2509 * by only the constraints in the constituents of map.
2511 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2512 __isl_take isl_map *map)
2514 return map_simple_hull(map, 0);
2517 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2518 __isl_take isl_set *set)
2520 return isl_map_unshifted_simple_hull(set);
2523 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2524 * A constraint that appears with different constant terms
2525 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2526 * (i.e., greatest) constant term.
2527 * "bmap1" and "bmap2" are assumed to have the same (known)
2528 * integer divisions.
2529 * The constraints of both "bmap1" and "bmap2" are assumed
2530 * to have been sorted using isl_basic_map_sort_constraints.
2532 * Run through the inequality constraints of "bmap1" and "bmap2"
2533 * in sorted order.
2534 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2535 * is removed.
2536 * If a match is found, the constraint is kept. If needed, the constant
2537 * term of the constraint is adjusted.
2539 static __isl_give isl_basic_map *select_shared_inequalities(
2540 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2542 int i1, i2;
2544 bmap1 = isl_basic_map_cow(bmap1);
2545 if (!bmap1 || !bmap2)
2546 return isl_basic_map_free(bmap1);
2548 i1 = bmap1->n_ineq - 1;
2549 i2 = bmap2->n_ineq - 1;
2550 while (bmap1 && i1 >= 0 && i2 >= 0) {
2551 int cmp;
2553 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2554 bmap2->ineq[i2]);
2555 if (cmp < 0) {
2556 --i2;
2557 continue;
2559 if (cmp > 0) {
2560 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2561 bmap1 = isl_basic_map_free(bmap1);
2562 --i1;
2563 continue;
2565 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2566 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2567 --i1;
2568 --i2;
2570 for (; i1 >= 0; --i1)
2571 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2572 bmap1 = isl_basic_map_free(bmap1);
2574 return bmap1;
2577 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2578 * "bmap1" and "bmap2" are assumed to have the same (known)
2579 * integer divisions.
2581 * Run through the equality constraints of "bmap1" and "bmap2".
2582 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2583 * is removed.
2585 static __isl_give isl_basic_map *select_shared_equalities(
2586 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2588 int i1, i2;
2589 unsigned total;
2591 bmap1 = isl_basic_map_cow(bmap1);
2592 if (!bmap1 || !bmap2)
2593 return isl_basic_map_free(bmap1);
2595 total = isl_basic_map_total_dim(bmap1);
2597 i1 = bmap1->n_eq - 1;
2598 i2 = bmap2->n_eq - 1;
2599 while (bmap1 && i1 >= 0 && i2 >= 0) {
2600 int last1, last2;
2602 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2603 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2604 if (last1 > last2) {
2605 --i2;
2606 continue;
2608 if (last1 < last2) {
2609 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2610 bmap1 = isl_basic_map_free(bmap1);
2611 --i1;
2612 continue;
2614 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2615 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2616 bmap1 = isl_basic_map_free(bmap1);
2618 --i1;
2619 --i2;
2621 for (; i1 >= 0; --i1)
2622 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2623 bmap1 = isl_basic_map_free(bmap1);
2625 return bmap1;
2628 /* Compute a superset of "bmap1" and "bmap2" that is described
2629 * by only the constraints that appear in both "bmap1" and "bmap2".
2631 * First drop constraints that involve unknown integer divisions
2632 * since it is not trivial to check whether two such integer divisions
2633 * in different basic maps are the same.
2634 * Then align the remaining (known) divs and sort the constraints.
2635 * Finally drop all inequalities and equalities from "bmap1" that
2636 * do not also appear in "bmap2".
2638 __isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2639 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2641 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2642 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2643 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2644 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2645 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2646 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2647 bmap1 = isl_basic_map_sort_constraints(bmap1);
2648 bmap2 = isl_basic_map_sort_constraints(bmap2);
2650 bmap1 = select_shared_inequalities(bmap1, bmap2);
2651 bmap1 = select_shared_equalities(bmap1, bmap2);
2653 isl_basic_map_free(bmap2);
2654 bmap1 = isl_basic_map_finalize(bmap1);
2655 return bmap1;
2658 /* Compute a superset of the convex hull of "map" that is described
2659 * by only the constraints in the constituents of "map".
2660 * In particular, the result is composed of constraints that appear
2661 * in each of the basic maps of "map"
2663 * Constraints that involve unknown integer divisions are dropped
2664 * since it is not trivial to check whether two such integer divisions
2665 * in different basic maps are the same.
2667 * The hull is initialized from the first basic map and then
2668 * updated with respect to the other basic maps in turn.
2670 __isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2671 __isl_take isl_map *map)
2673 int i;
2674 isl_basic_map *hull;
2676 if (!map)
2677 return NULL;
2678 if (map->n <= 1)
2679 return map_simple_hull_trivial(map);
2680 map = isl_map_drop_constraint_involving_unknown_divs(map);
2681 hull = isl_basic_map_copy(map->p[0]);
2682 for (i = 1; i < map->n; ++i) {
2683 isl_basic_map *bmap_i;
2685 bmap_i = isl_basic_map_copy(map->p[i]);
2686 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2689 isl_map_free(map);
2690 return hull;
2693 /* Compute a superset of the convex hull of "set" that is described
2694 * by only the constraints in the constituents of "set".
2695 * In particular, the result is composed of constraints that appear
2696 * in each of the basic sets of "set"
2698 __isl_give isl_basic_set *isl_set_plain_unshifted_simple_hull(
2699 __isl_take isl_set *set)
2701 return isl_map_plain_unshifted_simple_hull(set);
2704 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2706 * For each basic set in "set", we first check if the basic set
2707 * contains a translate of "ineq". If this translate is more relaxed,
2708 * then we assume that "ineq" is not a bound on this basic set.
2709 * Otherwise, we know that it is a bound.
2710 * If the basic set does not contain a translate of "ineq", then
2711 * we call is_bound to perform the test.
2713 static __isl_give isl_basic_set *add_bound_from_constraint(
2714 __isl_take isl_basic_set *hull, struct sh_data *data,
2715 __isl_keep isl_set *set, isl_int *ineq)
2717 int i, k;
2718 isl_ctx *ctx;
2719 uint32_t c_hash;
2720 struct ineq_cmp_data v;
2722 if (!hull || !set)
2723 return isl_basic_set_free(hull);
2725 v.len = isl_basic_set_total_dim(hull);
2726 v.p = ineq;
2727 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2729 ctx = isl_basic_set_get_ctx(hull);
2730 for (i = 0; i < set->n; ++i) {
2731 int bound;
2732 struct isl_hash_table_entry *entry;
2734 entry = isl_hash_table_find(ctx, data->p[i].table,
2735 c_hash, &has_ineq, &v, 0);
2736 if (entry) {
2737 isl_int *ineq_i = entry->data;
2738 int neg, more_relaxed;
2740 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2741 if (neg)
2742 isl_int_neg(ineq_i[0], ineq_i[0]);
2743 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2744 if (neg)
2745 isl_int_neg(ineq_i[0], ineq_i[0]);
2746 if (more_relaxed)
2747 break;
2748 else
2749 continue;
2751 bound = is_bound(data, set, i, ineq, 0);
2752 if (bound < 0)
2753 return isl_basic_set_free(hull);
2754 if (!bound)
2755 break;
2757 if (i < set->n)
2758 return hull;
2760 k = isl_basic_set_alloc_inequality(hull);
2761 if (k < 0)
2762 return isl_basic_set_free(hull);
2763 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2765 return hull;
2768 /* Compute a superset of the convex hull of "set" that is described
2769 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2770 * has no parameters or integer divisions.
2772 * The inequalities in "ineq" are assumed to have been sorted such
2773 * that constraints with the same linear part appear together and
2774 * that among constraints with the same linear part, those with
2775 * smaller constant term appear first.
2777 * We reuse the same data structure that is used by uset_simple_hull,
2778 * but we do not need the hull table since we will not consider the
2779 * same constraint more than once. We therefore allocate it with zero size.
2781 * We run through the constraints and try to add them one by one,
2782 * skipping identical constraints. If we have added a constraint and
2783 * the next constraint is a more relaxed translate, then we skip this
2784 * next constraint as well.
2786 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2787 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2789 int i;
2790 int last_added = 0;
2791 struct sh_data *data = NULL;
2792 isl_basic_set *hull = NULL;
2793 unsigned dim;
2795 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2796 if (!hull)
2797 goto error;
2799 data = sh_data_alloc(set, 0);
2800 if (!data)
2801 goto error;
2803 dim = isl_set_dim(set, isl_dim_set);
2804 for (i = 0; i < n_ineq; ++i) {
2805 int hull_n_ineq = hull->n_ineq;
2806 int parallel;
2808 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2809 dim);
2810 if (parallel &&
2811 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2812 continue;
2813 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2814 if (!hull)
2815 goto error;
2816 last_added = hull->n_ineq > hull_n_ineq;
2819 sh_data_free(data);
2820 isl_set_free(set);
2821 return hull;
2822 error:
2823 sh_data_free(data);
2824 isl_set_free(set);
2825 isl_basic_set_free(hull);
2826 return NULL;
2829 /* Collect pointers to all the inequalities in the elements of "list"
2830 * in "ineq". For equalities, store both a pointer to the equality and
2831 * a pointer to its opposite, which is first copied to "mat".
2832 * "ineq" and "mat" are assumed to have been preallocated to the right size
2833 * (the number of inequalities + 2 times the number of equalites and
2834 * the number of equalities, respectively).
2836 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2837 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2839 int i, j, n, n_eq, n_ineq;
2841 if (!mat)
2842 return NULL;
2844 n_eq = 0;
2845 n_ineq = 0;
2846 n = isl_basic_set_list_n_basic_set(list);
2847 for (i = 0; i < n; ++i) {
2848 isl_basic_set *bset;
2849 bset = isl_basic_set_list_get_basic_set(list, i);
2850 if (!bset)
2851 return isl_mat_free(mat);
2852 for (j = 0; j < bset->n_eq; ++j) {
2853 ineq[n_ineq++] = mat->row[n_eq];
2854 ineq[n_ineq++] = bset->eq[j];
2855 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2857 for (j = 0; j < bset->n_ineq; ++j)
2858 ineq[n_ineq++] = bset->ineq[j];
2859 isl_basic_set_free(bset);
2862 return mat;
2865 /* Comparison routine for use as an isl_sort callback.
2867 * Constraints with the same linear part are sorted together and
2868 * among constraints with the same linear part, those with smaller
2869 * constant term are sorted first.
2871 static int cmp_ineq(const void *a, const void *b, void *arg)
2873 unsigned dim = *(unsigned *) arg;
2874 isl_int * const *ineq1 = a;
2875 isl_int * const *ineq2 = b;
2876 int cmp;
2878 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2879 if (cmp != 0)
2880 return cmp;
2881 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2884 /* Compute a superset of the convex hull of "set" that is described
2885 * by only constraints in the elements of "list", where "set" has
2886 * no parameters or integer divisions.
2888 * We collect all the constraints in those elements and then
2889 * sort the constraints such that constraints with the same linear part
2890 * are sorted together and that those with smaller constant term are
2891 * sorted first.
2893 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2894 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2896 int i, n, n_eq, n_ineq;
2897 unsigned dim;
2898 isl_ctx *ctx;
2899 isl_mat *mat = NULL;
2900 isl_int **ineq = NULL;
2901 isl_basic_set *hull;
2903 if (!set)
2904 goto error;
2905 ctx = isl_set_get_ctx(set);
2907 n_eq = 0;
2908 n_ineq = 0;
2909 n = isl_basic_set_list_n_basic_set(list);
2910 for (i = 0; i < n; ++i) {
2911 isl_basic_set *bset;
2912 bset = isl_basic_set_list_get_basic_set(list, i);
2913 if (!bset)
2914 goto error;
2915 n_eq += bset->n_eq;
2916 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2917 isl_basic_set_free(bset);
2920 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2921 if (n_ineq > 0 && !ineq)
2922 goto error;
2924 dim = isl_set_dim(set, isl_dim_set);
2925 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2926 mat = collect_inequalities(mat, list, ineq);
2927 if (!mat)
2928 goto error;
2930 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2931 goto error;
2933 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2935 isl_mat_free(mat);
2936 free(ineq);
2937 isl_basic_set_list_free(list);
2938 return hull;
2939 error:
2940 isl_mat_free(mat);
2941 free(ineq);
2942 isl_set_free(set);
2943 isl_basic_set_list_free(list);
2944 return NULL;
2947 /* Compute a superset of the convex hull of "map" that is described
2948 * by only constraints in the elements of "list".
2950 * If the list is empty, then we can only describe the universe set.
2951 * If the input map is empty, then all constraints are valid, so
2952 * we return the intersection of the elements in "list".
2954 * Otherwise, we align all divs and temporarily treat them
2955 * as regular variables, computing the unshifted simple hull in
2956 * uset_unshifted_simple_hull_from_basic_set_list.
2958 static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2959 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2961 isl_basic_map *model;
2962 isl_basic_map *hull;
2963 isl_set *set;
2964 isl_basic_set_list *bset_list;
2966 if (!map || !list)
2967 goto error;
2969 if (isl_basic_map_list_n_basic_map(list) == 0) {
2970 isl_space *space;
2972 space = isl_map_get_space(map);
2973 isl_map_free(map);
2974 isl_basic_map_list_free(list);
2975 return isl_basic_map_universe(space);
2977 if (isl_map_plain_is_empty(map)) {
2978 isl_map_free(map);
2979 return isl_basic_map_list_intersect(list);
2982 map = isl_map_align_divs_to_basic_map_list(map, list);
2983 if (!map)
2984 goto error;
2985 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2987 model = isl_basic_map_list_get_basic_map(list, 0);
2989 set = isl_map_underlying_set(map);
2990 bset_list = isl_basic_map_list_underlying_set(list);
2992 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2993 hull = isl_basic_map_overlying_set(hull, model);
2995 return hull;
2996 error:
2997 isl_map_free(map);
2998 isl_basic_map_list_free(list);
2999 return NULL;
3002 /* Return a sequence of the basic maps that make up the maps in "list".
3004 static __isl_give isl_basic_map_list *collect_basic_maps(
3005 __isl_take isl_map_list *list)
3007 int i, n;
3008 isl_ctx *ctx;
3009 isl_basic_map_list *bmap_list;
3011 if (!list)
3012 return NULL;
3013 n = isl_map_list_n_map(list);
3014 ctx = isl_map_list_get_ctx(list);
3015 bmap_list = isl_basic_map_list_alloc(ctx, 0);
3017 for (i = 0; i < n; ++i) {
3018 isl_map *map;
3019 isl_basic_map_list *list_i;
3021 map = isl_map_list_get_map(list, i);
3022 map = isl_map_compute_divs(map);
3023 list_i = isl_map_get_basic_map_list(map);
3024 isl_map_free(map);
3025 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
3028 isl_map_list_free(list);
3029 return bmap_list;
3032 /* Compute a superset of the convex hull of "map" that is described
3033 * by only constraints in the elements of "list".
3035 * If "map" is the universe, then the convex hull (and therefore
3036 * any superset of the convexhull) is the universe as well.
3038 * Otherwise, we collect all the basic maps in the map list and
3039 * continue with map_unshifted_simple_hull_from_basic_map_list.
3041 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
3042 __isl_take isl_map *map, __isl_take isl_map_list *list)
3044 isl_basic_map_list *bmap_list;
3045 int is_universe;
3047 is_universe = isl_map_plain_is_universe(map);
3048 if (is_universe < 0)
3049 map = isl_map_free(map);
3050 if (is_universe < 0 || is_universe) {
3051 isl_map_list_free(list);
3052 return isl_map_unshifted_simple_hull(map);
3055 bmap_list = collect_basic_maps(list);
3056 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
3059 /* Compute a superset of the convex hull of "set" that is described
3060 * by only constraints in the elements of "list".
3062 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
3063 __isl_take isl_set *set, __isl_take isl_set_list *list)
3065 return isl_map_unshifted_simple_hull_from_map_list(set, list);
3068 /* Given a set "set", return parametric bounds on the dimension "dim".
3070 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
3072 unsigned set_dim = isl_set_dim(set, isl_dim_set);
3073 set = isl_set_copy(set);
3074 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
3075 set = isl_set_eliminate_dims(set, 0, dim);
3076 return isl_set_convex_hull(set);
3079 /* Computes a "simple hull" and then check if each dimension in the
3080 * resulting hull is bounded by a symbolic constant. If not, the
3081 * hull is intersected with the corresponding bounds on the whole set.
3083 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
3085 int i, j;
3086 struct isl_basic_set *hull;
3087 unsigned nparam, left;
3088 int removed_divs = 0;
3090 hull = isl_set_simple_hull(isl_set_copy(set));
3091 if (!hull)
3092 goto error;
3094 nparam = isl_basic_set_dim(hull, isl_dim_param);
3095 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
3096 int lower = 0, upper = 0;
3097 struct isl_basic_set *bounds;
3099 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
3100 for (j = 0; j < hull->n_eq; ++j) {
3101 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3102 continue;
3103 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3104 left) == -1)
3105 break;
3107 if (j < hull->n_eq)
3108 continue;
3110 for (j = 0; j < hull->n_ineq; ++j) {
3111 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3112 continue;
3113 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3114 left) != -1 ||
3115 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3116 i) != -1)
3117 continue;
3118 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3119 lower = 1;
3120 else
3121 upper = 1;
3122 if (lower && upper)
3123 break;
3126 if (lower && upper)
3127 continue;
3129 if (!removed_divs) {
3130 set = isl_set_remove_divs(set);
3131 if (!set)
3132 goto error;
3133 removed_divs = 1;
3135 bounds = set_bounds(set, i);
3136 hull = isl_basic_set_intersect(hull, bounds);
3137 if (!hull)
3138 goto error;
3141 isl_set_free(set);
3142 return hull;
3143 error:
3144 isl_set_free(set);
3145 isl_basic_set_free(hull);
3146 return NULL;