isl_pip: allow existentially quantified variables in the context
[isl.git] / isl_convex_hull.c
blob1c732b39eacb8dec0f829c17684a655cd7bfd6fb
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_lp_private.h>
16 #include <isl/map.h>
17 #include <isl_mat_private.h>
18 #include <isl_vec_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_options_private.h>
22 #include "isl_equalities.h"
23 #include "isl_tab.h"
24 #include <isl_sort.h>
26 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
28 /* Return 1 if constraint c is redundant with respect to the constraints
29 * in bmap. If c is a lower [upper] bound in some variable and bmap
30 * does not have a lower [upper] bound in that variable, then c cannot
31 * be redundant and we do not need solve any lp.
33 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
34 isl_int *c, isl_int *opt_n, isl_int *opt_d)
36 enum isl_lp_result res;
37 unsigned total;
38 int i, j;
40 if (!bmap)
41 return -1;
43 total = isl_basic_map_total_dim(*bmap);
44 for (i = 0; i < total; ++i) {
45 int sign;
46 if (isl_int_is_zero(c[1+i]))
47 continue;
48 sign = isl_int_sgn(c[1+i]);
49 for (j = 0; j < (*bmap)->n_ineq; ++j)
50 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
51 break;
52 if (j == (*bmap)->n_ineq)
53 break;
55 if (i < total)
56 return 0;
58 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
59 opt_n, opt_d, NULL);
60 if (res == isl_lp_unbounded)
61 return 0;
62 if (res == isl_lp_error)
63 return -1;
64 if (res == isl_lp_empty) {
65 *bmap = isl_basic_map_set_to_empty(*bmap);
66 return 0;
68 return !isl_int_is_neg(*opt_n);
71 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
72 isl_int *c, isl_int *opt_n, isl_int *opt_d)
74 return isl_basic_map_constraint_is_redundant(
75 (struct isl_basic_map **)bset, c, opt_n, opt_d);
78 /* Remove redundant
79 * constraints. If the minimal value along the normal of a constraint
80 * is the same if the constraint is removed, then the constraint is redundant.
82 * Since some constraints may be mutually redundant, sort the constraints
83 * first such that constraints that involve existentially quantified
84 * variables are considered for removal before those that do not.
85 * The sorting is also need for the use in map_simple_hull.
87 * Alternatively, we could have intersected the basic map with the
88 * corresponding equality and then checked if the dimension was that
89 * of a facet.
91 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
92 __isl_take isl_basic_map *bmap)
94 struct isl_tab *tab;
96 if (!bmap)
97 return NULL;
99 bmap = isl_basic_map_gauss(bmap, NULL);
100 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
101 return bmap;
102 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
103 return bmap;
104 if (bmap->n_ineq <= 1)
105 return bmap;
107 bmap = isl_basic_map_sort_constraints(bmap);
108 tab = isl_tab_from_basic_map(bmap, 0);
109 if (isl_tab_detect_implicit_equalities(tab) < 0)
110 goto error;
111 if (isl_tab_detect_redundant(tab) < 0)
112 goto error;
113 bmap = isl_basic_map_update_from_tab(bmap, tab);
114 isl_tab_free(tab);
115 if (!bmap)
116 return NULL;
117 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
118 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
119 return bmap;
120 error:
121 isl_tab_free(tab);
122 isl_basic_map_free(bmap);
123 return NULL;
126 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
127 __isl_take isl_basic_set *bset)
129 return (struct isl_basic_set *)
130 isl_basic_map_remove_redundancies((struct isl_basic_map *)bset);
133 /* Remove redundant constraints in each of the basic maps.
135 __isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
137 return isl_map_inline_foreach_basic_map(map,
138 &isl_basic_map_remove_redundancies);
141 __isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
143 return isl_map_remove_redundancies(set);
146 /* Check if the set set is bound in the direction of the affine
147 * constraint c and if so, set the constant term such that the
148 * resulting constraint is a bounding constraint for the set.
150 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
152 int first;
153 int j;
154 isl_int opt;
155 isl_int opt_denom;
157 isl_int_init(opt);
158 isl_int_init(opt_denom);
159 first = 1;
160 for (j = 0; j < set->n; ++j) {
161 enum isl_lp_result res;
163 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
164 continue;
166 res = isl_basic_set_solve_lp(set->p[j],
167 0, c, set->ctx->one, &opt, &opt_denom, NULL);
168 if (res == isl_lp_unbounded)
169 break;
170 if (res == isl_lp_error)
171 goto error;
172 if (res == isl_lp_empty) {
173 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
174 if (!set->p[j])
175 goto error;
176 continue;
178 if (first || isl_int_is_neg(opt)) {
179 if (!isl_int_is_one(opt_denom))
180 isl_seq_scale(c, c, opt_denom, len);
181 isl_int_sub(c[0], c[0], opt);
183 first = 0;
185 isl_int_clear(opt);
186 isl_int_clear(opt_denom);
187 return j >= set->n;
188 error:
189 isl_int_clear(opt);
190 isl_int_clear(opt_denom);
191 return -1;
194 __isl_give isl_basic_map *isl_basic_map_set_rational(
195 __isl_take isl_basic_set *bmap)
197 if (!bmap)
198 return NULL;
200 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
201 return bmap;
203 bmap = isl_basic_map_cow(bmap);
204 if (!bmap)
205 return NULL;
207 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
209 return isl_basic_map_finalize(bmap);
212 __isl_give isl_basic_set *isl_basic_set_set_rational(
213 __isl_take isl_basic_set *bset)
215 return isl_basic_map_set_rational(bset);
218 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
220 int i;
222 map = isl_map_cow(map);
223 if (!map)
224 return NULL;
225 for (i = 0; i < map->n; ++i) {
226 map->p[i] = isl_basic_map_set_rational(map->p[i]);
227 if (!map->p[i])
228 goto error;
230 return map;
231 error:
232 isl_map_free(map);
233 return NULL;
236 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
238 return isl_map_set_rational(set);
241 static struct isl_basic_set *isl_basic_set_add_equality(
242 struct isl_basic_set *bset, isl_int *c)
244 int i;
245 unsigned dim;
247 if (!bset)
248 return NULL;
250 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
251 return bset;
253 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
254 isl_assert(bset->ctx, bset->n_div == 0, goto error);
255 dim = isl_basic_set_n_dim(bset);
256 bset = isl_basic_set_cow(bset);
257 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
258 i = isl_basic_set_alloc_equality(bset);
259 if (i < 0)
260 goto error;
261 isl_seq_cpy(bset->eq[i], c, 1 + dim);
262 return bset;
263 error:
264 isl_basic_set_free(bset);
265 return NULL;
268 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
270 int i;
272 set = isl_set_cow(set);
273 if (!set)
274 return NULL;
275 for (i = 0; i < set->n; ++i) {
276 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
277 if (!set->p[i])
278 goto error;
280 return set;
281 error:
282 isl_set_free(set);
283 return NULL;
286 /* Given a union of basic sets, construct the constraints for wrapping
287 * a facet around one of its ridges.
288 * In particular, if each of n the d-dimensional basic sets i in "set"
289 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
290 * and is defined by the constraints
291 * [ 1 ]
292 * A_i [ x ] >= 0
294 * then the resulting set is of dimension n*(1+d) and has as constraints
296 * [ a_i ]
297 * A_i [ x_i ] >= 0
299 * a_i >= 0
301 * \sum_i x_{i,1} = 1
303 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
305 struct isl_basic_set *lp;
306 unsigned n_eq;
307 unsigned n_ineq;
308 int i, j, k;
309 unsigned dim, lp_dim;
311 if (!set)
312 return NULL;
314 dim = 1 + isl_set_n_dim(set);
315 n_eq = 1;
316 n_ineq = set->n;
317 for (i = 0; i < set->n; ++i) {
318 n_eq += set->p[i]->n_eq;
319 n_ineq += set->p[i]->n_ineq;
321 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
322 lp = isl_basic_set_set_rational(lp);
323 if (!lp)
324 return NULL;
325 lp_dim = isl_basic_set_n_dim(lp);
326 k = isl_basic_set_alloc_equality(lp);
327 isl_int_set_si(lp->eq[k][0], -1);
328 for (i = 0; i < set->n; ++i) {
329 isl_int_set_si(lp->eq[k][1+dim*i], 0);
330 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
331 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
333 for (i = 0; i < set->n; ++i) {
334 k = isl_basic_set_alloc_inequality(lp);
335 isl_seq_clr(lp->ineq[k], 1+lp_dim);
336 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
338 for (j = 0; j < set->p[i]->n_eq; ++j) {
339 k = isl_basic_set_alloc_equality(lp);
340 isl_seq_clr(lp->eq[k], 1+dim*i);
341 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
342 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
345 for (j = 0; j < set->p[i]->n_ineq; ++j) {
346 k = isl_basic_set_alloc_inequality(lp);
347 isl_seq_clr(lp->ineq[k], 1+dim*i);
348 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
349 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
352 return lp;
355 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
356 * of that facet, compute the other facet of the convex hull that contains
357 * the ridge.
359 * We first transform the set such that the facet constraint becomes
361 * x_1 >= 0
363 * I.e., the facet lies in
365 * x_1 = 0
367 * and on that facet, the constraint that defines the ridge is
369 * x_2 >= 0
371 * (This transformation is not strictly needed, all that is needed is
372 * that the ridge contains the origin.)
374 * Since the ridge contains the origin, the cone of the convex hull
375 * will be of the form
377 * x_1 >= 0
378 * x_2 >= a x_1
380 * with this second constraint defining the new facet.
381 * The constant a is obtained by settting x_1 in the cone of the
382 * convex hull to 1 and minimizing x_2.
383 * Now, each element in the cone of the convex hull is the sum
384 * of elements in the cones of the basic sets.
385 * If a_i is the dilation factor of basic set i, then the problem
386 * we need to solve is
388 * min \sum_i x_{i,2}
389 * st
390 * \sum_i x_{i,1} = 1
391 * a_i >= 0
392 * [ a_i ]
393 * A [ x_i ] >= 0
395 * with
396 * [ 1 ]
397 * A_i [ x_i ] >= 0
399 * the constraints of each (transformed) basic set.
400 * If a = n/d, then the constraint defining the new facet (in the transformed
401 * space) is
403 * -n x_1 + d x_2 >= 0
405 * In the original space, we need to take the same combination of the
406 * corresponding constraints "facet" and "ridge".
408 * If a = -infty = "-1/0", then we just return the original facet constraint.
409 * This means that the facet is unbounded, but has a bounded intersection
410 * with the union of sets.
412 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
413 isl_int *facet, isl_int *ridge)
415 int i;
416 isl_ctx *ctx;
417 struct isl_mat *T = NULL;
418 struct isl_basic_set *lp = NULL;
419 struct isl_vec *obj;
420 enum isl_lp_result res;
421 isl_int num, den;
422 unsigned dim;
424 if (!set)
425 return NULL;
426 ctx = set->ctx;
427 set = isl_set_copy(set);
428 set = isl_set_set_rational(set);
430 dim = 1 + isl_set_n_dim(set);
431 T = isl_mat_alloc(ctx, 3, dim);
432 if (!T)
433 goto error;
434 isl_int_set_si(T->row[0][0], 1);
435 isl_seq_clr(T->row[0]+1, dim - 1);
436 isl_seq_cpy(T->row[1], facet, dim);
437 isl_seq_cpy(T->row[2], ridge, dim);
438 T = isl_mat_right_inverse(T);
439 set = isl_set_preimage(set, T);
440 T = NULL;
441 if (!set)
442 goto error;
443 lp = wrap_constraints(set);
444 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
445 if (!obj)
446 goto error;
447 isl_int_set_si(obj->block.data[0], 0);
448 for (i = 0; i < set->n; ++i) {
449 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
450 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
451 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
453 isl_int_init(num);
454 isl_int_init(den);
455 res = isl_basic_set_solve_lp(lp, 0,
456 obj->block.data, ctx->one, &num, &den, NULL);
457 if (res == isl_lp_ok) {
458 isl_int_neg(num, num);
459 isl_seq_combine(facet, num, facet, den, ridge, dim);
460 isl_seq_normalize(ctx, facet, dim);
462 isl_int_clear(num);
463 isl_int_clear(den);
464 isl_vec_free(obj);
465 isl_basic_set_free(lp);
466 isl_set_free(set);
467 if (res == isl_lp_error)
468 return NULL;
469 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
470 return NULL);
471 return facet;
472 error:
473 isl_basic_set_free(lp);
474 isl_mat_free(T);
475 isl_set_free(set);
476 return NULL;
479 /* Compute the constraint of a facet of "set".
481 * We first compute the intersection with a bounding constraint
482 * that is orthogonal to one of the coordinate axes.
483 * If the affine hull of this intersection has only one equality,
484 * we have found a facet.
485 * Otherwise, we wrap the current bounding constraint around
486 * one of the equalities of the face (one that is not equal to
487 * the current bounding constraint).
488 * This process continues until we have found a facet.
489 * The dimension of the intersection increases by at least
490 * one on each iteration, so termination is guaranteed.
492 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
494 struct isl_set *slice = NULL;
495 struct isl_basic_set *face = NULL;
496 int i;
497 unsigned dim = isl_set_n_dim(set);
498 int is_bound;
499 isl_mat *bounds = NULL;
501 isl_assert(set->ctx, set->n > 0, goto error);
502 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
503 if (!bounds)
504 return NULL;
506 isl_seq_clr(bounds->row[0], dim);
507 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
508 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
509 if (is_bound < 0)
510 goto error;
511 isl_assert(set->ctx, is_bound, goto error);
512 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
513 bounds->n_row = 1;
515 for (;;) {
516 slice = isl_set_copy(set);
517 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
518 face = isl_set_affine_hull(slice);
519 if (!face)
520 goto error;
521 if (face->n_eq == 1) {
522 isl_basic_set_free(face);
523 break;
525 for (i = 0; i < face->n_eq; ++i)
526 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
527 !isl_seq_is_neg(bounds->row[0],
528 face->eq[i], 1 + dim))
529 break;
530 isl_assert(set->ctx, i < face->n_eq, goto error);
531 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
532 goto error;
533 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
534 isl_basic_set_free(face);
537 return bounds;
538 error:
539 isl_basic_set_free(face);
540 isl_mat_free(bounds);
541 return NULL;
544 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
545 * compute a hyperplane description of the facet, i.e., compute the facets
546 * of the facet.
548 * We compute an affine transformation that transforms the constraint
550 * [ 1 ]
551 * c [ x ] = 0
553 * to the constraint
555 * z_1 = 0
557 * by computing the right inverse U of a matrix that starts with the rows
559 * [ 1 0 ]
560 * [ c ]
562 * Then
563 * [ 1 ] [ 1 ]
564 * [ x ] = U [ z ]
565 * and
566 * [ 1 ] [ 1 ]
567 * [ z ] = Q [ x ]
569 * with Q = U^{-1}
570 * Since z_1 is zero, we can drop this variable as well as the corresponding
571 * column of U to obtain
573 * [ 1 ] [ 1 ]
574 * [ x ] = U' [ z' ]
575 * and
576 * [ 1 ] [ 1 ]
577 * [ z' ] = Q' [ x ]
579 * with Q' equal to Q, but without the corresponding row.
580 * After computing the facets of the facet in the z' space,
581 * we convert them back to the x space through Q.
583 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
585 struct isl_mat *m, *U, *Q;
586 struct isl_basic_set *facet = NULL;
587 struct isl_ctx *ctx;
588 unsigned dim;
590 ctx = set->ctx;
591 set = isl_set_copy(set);
592 dim = isl_set_n_dim(set);
593 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
594 if (!m)
595 goto error;
596 isl_int_set_si(m->row[0][0], 1);
597 isl_seq_clr(m->row[0]+1, dim);
598 isl_seq_cpy(m->row[1], c, 1+dim);
599 U = isl_mat_right_inverse(m);
600 Q = isl_mat_right_inverse(isl_mat_copy(U));
601 U = isl_mat_drop_cols(U, 1, 1);
602 Q = isl_mat_drop_rows(Q, 1, 1);
603 set = isl_set_preimage(set, U);
604 facet = uset_convex_hull_wrap_bounded(set);
605 facet = isl_basic_set_preimage(facet, Q);
606 if (facet && facet->n_eq != 0)
607 isl_die(ctx, isl_error_internal, "unexpected equality",
608 return isl_basic_set_free(facet));
609 return facet;
610 error:
611 isl_basic_set_free(facet);
612 isl_set_free(set);
613 return NULL;
616 /* Given an initial facet constraint, compute the remaining facets.
617 * We do this by running through all facets found so far and computing
618 * the adjacent facets through wrapping, adding those facets that we
619 * hadn't already found before.
621 * For each facet we have found so far, we first compute its facets
622 * in the resulting convex hull. That is, we compute the ridges
623 * of the resulting convex hull contained in the facet.
624 * We also compute the corresponding facet in the current approximation
625 * of the convex hull. There is no need to wrap around the ridges
626 * in this facet since that would result in a facet that is already
627 * present in the current approximation.
629 * This function can still be significantly optimized by checking which of
630 * the facets of the basic sets are also facets of the convex hull and
631 * using all the facets so far to help in constructing the facets of the
632 * facets
633 * and/or
634 * using the technique in section "3.1 Ridge Generation" of
635 * "Extended Convex Hull" by Fukuda et al.
637 static struct isl_basic_set *extend(struct isl_basic_set *hull,
638 struct isl_set *set)
640 int i, j, f;
641 int k;
642 struct isl_basic_set *facet = NULL;
643 struct isl_basic_set *hull_facet = NULL;
644 unsigned dim;
646 if (!hull)
647 return NULL;
649 isl_assert(set->ctx, set->n > 0, goto error);
651 dim = isl_set_n_dim(set);
653 for (i = 0; i < hull->n_ineq; ++i) {
654 facet = compute_facet(set, hull->ineq[i]);
655 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
656 facet = isl_basic_set_gauss(facet, NULL);
657 facet = isl_basic_set_normalize_constraints(facet);
658 hull_facet = isl_basic_set_copy(hull);
659 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
660 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
661 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
662 if (!facet || !hull_facet)
663 goto error;
664 hull = isl_basic_set_cow(hull);
665 hull = isl_basic_set_extend_space(hull,
666 isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
667 if (!hull)
668 goto error;
669 for (j = 0; j < facet->n_ineq; ++j) {
670 for (f = 0; f < hull_facet->n_ineq; ++f)
671 if (isl_seq_eq(facet->ineq[j],
672 hull_facet->ineq[f], 1 + dim))
673 break;
674 if (f < hull_facet->n_ineq)
675 continue;
676 k = isl_basic_set_alloc_inequality(hull);
677 if (k < 0)
678 goto error;
679 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
680 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
681 goto error;
683 isl_basic_set_free(hull_facet);
684 isl_basic_set_free(facet);
686 hull = isl_basic_set_simplify(hull);
687 hull = isl_basic_set_finalize(hull);
688 return hull;
689 error:
690 isl_basic_set_free(hull_facet);
691 isl_basic_set_free(facet);
692 isl_basic_set_free(hull);
693 return NULL;
696 /* Special case for computing the convex hull of a one dimensional set.
697 * We simply collect the lower and upper bounds of each basic set
698 * and the biggest of those.
700 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
702 struct isl_mat *c = NULL;
703 isl_int *lower = NULL;
704 isl_int *upper = NULL;
705 int i, j, k;
706 isl_int a, b;
707 struct isl_basic_set *hull;
709 for (i = 0; i < set->n; ++i) {
710 set->p[i] = isl_basic_set_simplify(set->p[i]);
711 if (!set->p[i])
712 goto error;
714 set = isl_set_remove_empty_parts(set);
715 if (!set)
716 goto error;
717 isl_assert(set->ctx, set->n > 0, goto error);
718 c = isl_mat_alloc(set->ctx, 2, 2);
719 if (!c)
720 goto error;
722 if (set->p[0]->n_eq > 0) {
723 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
724 lower = c->row[0];
725 upper = c->row[1];
726 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
727 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
728 isl_seq_neg(upper, set->p[0]->eq[0], 2);
729 } else {
730 isl_seq_neg(lower, set->p[0]->eq[0], 2);
731 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
733 } else {
734 for (j = 0; j < set->p[0]->n_ineq; ++j) {
735 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
736 lower = c->row[0];
737 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
738 } else {
739 upper = c->row[1];
740 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
745 isl_int_init(a);
746 isl_int_init(b);
747 for (i = 0; i < set->n; ++i) {
748 struct isl_basic_set *bset = set->p[i];
749 int has_lower = 0;
750 int has_upper = 0;
752 for (j = 0; j < bset->n_eq; ++j) {
753 has_lower = 1;
754 has_upper = 1;
755 if (lower) {
756 isl_int_mul(a, lower[0], bset->eq[j][1]);
757 isl_int_mul(b, lower[1], bset->eq[j][0]);
758 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
759 isl_seq_cpy(lower, bset->eq[j], 2);
760 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
761 isl_seq_neg(lower, bset->eq[j], 2);
763 if (upper) {
764 isl_int_mul(a, upper[0], bset->eq[j][1]);
765 isl_int_mul(b, upper[1], bset->eq[j][0]);
766 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
767 isl_seq_neg(upper, bset->eq[j], 2);
768 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
769 isl_seq_cpy(upper, bset->eq[j], 2);
772 for (j = 0; j < bset->n_ineq; ++j) {
773 if (isl_int_is_pos(bset->ineq[j][1]))
774 has_lower = 1;
775 if (isl_int_is_neg(bset->ineq[j][1]))
776 has_upper = 1;
777 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
778 isl_int_mul(a, lower[0], bset->ineq[j][1]);
779 isl_int_mul(b, lower[1], bset->ineq[j][0]);
780 if (isl_int_lt(a, b))
781 isl_seq_cpy(lower, bset->ineq[j], 2);
783 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
784 isl_int_mul(a, upper[0], bset->ineq[j][1]);
785 isl_int_mul(b, upper[1], bset->ineq[j][0]);
786 if (isl_int_gt(a, b))
787 isl_seq_cpy(upper, bset->ineq[j], 2);
790 if (!has_lower)
791 lower = NULL;
792 if (!has_upper)
793 upper = NULL;
795 isl_int_clear(a);
796 isl_int_clear(b);
798 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
799 hull = isl_basic_set_set_rational(hull);
800 if (!hull)
801 goto error;
802 if (lower) {
803 k = isl_basic_set_alloc_inequality(hull);
804 isl_seq_cpy(hull->ineq[k], lower, 2);
806 if (upper) {
807 k = isl_basic_set_alloc_inequality(hull);
808 isl_seq_cpy(hull->ineq[k], upper, 2);
810 hull = isl_basic_set_finalize(hull);
811 isl_set_free(set);
812 isl_mat_free(c);
813 return hull;
814 error:
815 isl_set_free(set);
816 isl_mat_free(c);
817 return NULL;
820 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
822 struct isl_basic_set *convex_hull;
824 if (!set)
825 return NULL;
827 if (isl_set_is_empty(set))
828 convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
829 else
830 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
831 isl_set_free(set);
832 return convex_hull;
835 /* Compute the convex hull of a pair of basic sets without any parameters or
836 * integer divisions using Fourier-Motzkin elimination.
837 * The convex hull is the set of all points that can be written as
838 * the sum of points from both basic sets (in homogeneous coordinates).
839 * We set up the constraints in a space with dimensions for each of
840 * the three sets and then project out the dimensions corresponding
841 * to the two original basic sets, retaining only those corresponding
842 * to the convex hull.
844 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
845 struct isl_basic_set *bset2)
847 int i, j, k;
848 struct isl_basic_set *bset[2];
849 struct isl_basic_set *hull = NULL;
850 unsigned dim;
852 if (!bset1 || !bset2)
853 goto error;
855 dim = isl_basic_set_n_dim(bset1);
856 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
857 1 + dim + bset1->n_eq + bset2->n_eq,
858 2 + bset1->n_ineq + bset2->n_ineq);
859 bset[0] = bset1;
860 bset[1] = bset2;
861 for (i = 0; i < 2; ++i) {
862 for (j = 0; j < bset[i]->n_eq; ++j) {
863 k = isl_basic_set_alloc_equality(hull);
864 if (k < 0)
865 goto error;
866 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
867 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
868 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
869 1+dim);
871 for (j = 0; j < bset[i]->n_ineq; ++j) {
872 k = isl_basic_set_alloc_inequality(hull);
873 if (k < 0)
874 goto error;
875 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
876 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
877 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
878 bset[i]->ineq[j], 1+dim);
880 k = isl_basic_set_alloc_inequality(hull);
881 if (k < 0)
882 goto error;
883 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
884 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
886 for (j = 0; j < 1+dim; ++j) {
887 k = isl_basic_set_alloc_equality(hull);
888 if (k < 0)
889 goto error;
890 isl_seq_clr(hull->eq[k], 1+2+3*dim);
891 isl_int_set_si(hull->eq[k][j], -1);
892 isl_int_set_si(hull->eq[k][1+dim+j], 1);
893 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
895 hull = isl_basic_set_set_rational(hull);
896 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
897 hull = isl_basic_set_remove_redundancies(hull);
898 isl_basic_set_free(bset1);
899 isl_basic_set_free(bset2);
900 return hull;
901 error:
902 isl_basic_set_free(bset1);
903 isl_basic_set_free(bset2);
904 isl_basic_set_free(hull);
905 return NULL;
908 /* Is the set bounded for each value of the parameters?
910 int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
912 struct isl_tab *tab;
913 int bounded;
915 if (!bset)
916 return -1;
917 if (isl_basic_set_plain_is_empty(bset))
918 return 1;
920 tab = isl_tab_from_recession_cone(bset, 1);
921 bounded = isl_tab_cone_is_bounded(tab);
922 isl_tab_free(tab);
923 return bounded;
926 /* Is the image bounded for each value of the parameters and
927 * the domain variables?
929 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
931 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
932 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
933 int bounded;
935 bmap = isl_basic_map_copy(bmap);
936 bmap = isl_basic_map_cow(bmap);
937 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
938 isl_dim_in, 0, n_in);
939 bounded = isl_basic_set_is_bounded((isl_basic_set *)bmap);
940 isl_basic_map_free(bmap);
942 return bounded;
945 /* Is the set bounded for each value of the parameters?
947 int isl_set_is_bounded(__isl_keep isl_set *set)
949 int i;
951 if (!set)
952 return -1;
954 for (i = 0; i < set->n; ++i) {
955 int bounded = isl_basic_set_is_bounded(set->p[i]);
956 if (!bounded || bounded < 0)
957 return bounded;
959 return 1;
962 /* Compute the lineality space of the convex hull of bset1 and bset2.
964 * We first compute the intersection of the recession cone of bset1
965 * with the negative of the recession cone of bset2 and then compute
966 * the linear hull of the resulting cone.
968 static struct isl_basic_set *induced_lineality_space(
969 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
971 int i, k;
972 struct isl_basic_set *lin = NULL;
973 unsigned dim;
975 if (!bset1 || !bset2)
976 goto error;
978 dim = isl_basic_set_total_dim(bset1);
979 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
980 bset1->n_eq + bset2->n_eq,
981 bset1->n_ineq + bset2->n_ineq);
982 lin = isl_basic_set_set_rational(lin);
983 if (!lin)
984 goto error;
985 for (i = 0; i < bset1->n_eq; ++i) {
986 k = isl_basic_set_alloc_equality(lin);
987 if (k < 0)
988 goto error;
989 isl_int_set_si(lin->eq[k][0], 0);
990 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
992 for (i = 0; i < bset1->n_ineq; ++i) {
993 k = isl_basic_set_alloc_inequality(lin);
994 if (k < 0)
995 goto error;
996 isl_int_set_si(lin->ineq[k][0], 0);
997 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
999 for (i = 0; i < bset2->n_eq; ++i) {
1000 k = isl_basic_set_alloc_equality(lin);
1001 if (k < 0)
1002 goto error;
1003 isl_int_set_si(lin->eq[k][0], 0);
1004 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
1006 for (i = 0; i < bset2->n_ineq; ++i) {
1007 k = isl_basic_set_alloc_inequality(lin);
1008 if (k < 0)
1009 goto error;
1010 isl_int_set_si(lin->ineq[k][0], 0);
1011 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
1014 isl_basic_set_free(bset1);
1015 isl_basic_set_free(bset2);
1016 return isl_basic_set_affine_hull(lin);
1017 error:
1018 isl_basic_set_free(lin);
1019 isl_basic_set_free(bset1);
1020 isl_basic_set_free(bset2);
1021 return NULL;
1024 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1026 /* Given a set and a linear space "lin" of dimension n > 0,
1027 * project the linear space from the set, compute the convex hull
1028 * and then map the set back to the original space.
1030 * Let
1032 * M x = 0
1034 * describe the linear space. We first compute the Hermite normal
1035 * form H = M U of M = H Q, to obtain
1037 * H Q x = 0
1039 * The last n rows of H will be zero, so the last n variables of x' = Q x
1040 * are the one we want to project out. We do this by transforming each
1041 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1042 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1043 * we transform the hull back to the original space as A' Q_1 x >= b',
1044 * with Q_1 all but the last n rows of Q.
1046 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1047 struct isl_basic_set *lin)
1049 unsigned total = isl_basic_set_total_dim(lin);
1050 unsigned lin_dim;
1051 struct isl_basic_set *hull;
1052 struct isl_mat *M, *U, *Q;
1054 if (!set || !lin)
1055 goto error;
1056 lin_dim = total - lin->n_eq;
1057 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1058 M = isl_mat_left_hermite(M, 0, &U, &Q);
1059 if (!M)
1060 goto error;
1061 isl_mat_free(M);
1062 isl_basic_set_free(lin);
1064 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1066 U = isl_mat_lin_to_aff(U);
1067 Q = isl_mat_lin_to_aff(Q);
1069 set = isl_set_preimage(set, U);
1070 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1071 hull = uset_convex_hull(set);
1072 hull = isl_basic_set_preimage(hull, Q);
1074 return hull;
1075 error:
1076 isl_basic_set_free(lin);
1077 isl_set_free(set);
1078 return NULL;
1081 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1082 * set up an LP for solving
1084 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1086 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1087 * The next \alpha{ij} correspond to the equalities and come in pairs.
1088 * The final \alpha{ij} correspond to the inequalities.
1090 static struct isl_basic_set *valid_direction_lp(
1091 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1093 isl_space *dim;
1094 struct isl_basic_set *lp;
1095 unsigned d;
1096 int n;
1097 int i, j, k;
1099 if (!bset1 || !bset2)
1100 goto error;
1101 d = 1 + isl_basic_set_total_dim(bset1);
1102 n = 2 +
1103 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1104 dim = isl_space_set_alloc(bset1->ctx, 0, n);
1105 lp = isl_basic_set_alloc_space(dim, 0, d, n);
1106 if (!lp)
1107 goto error;
1108 for (i = 0; i < n; ++i) {
1109 k = isl_basic_set_alloc_inequality(lp);
1110 if (k < 0)
1111 goto error;
1112 isl_seq_clr(lp->ineq[k] + 1, n);
1113 isl_int_set_si(lp->ineq[k][0], -1);
1114 isl_int_set_si(lp->ineq[k][1 + i], 1);
1116 for (i = 0; i < d; ++i) {
1117 k = isl_basic_set_alloc_equality(lp);
1118 if (k < 0)
1119 goto error;
1120 n = 0;
1121 isl_int_set_si(lp->eq[k][n], 0); n++;
1122 /* positivity constraint 1 >= 0 */
1123 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1124 for (j = 0; j < bset1->n_eq; ++j) {
1125 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1126 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1128 for (j = 0; j < bset1->n_ineq; ++j) {
1129 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1131 /* positivity constraint 1 >= 0 */
1132 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1133 for (j = 0; j < bset2->n_eq; ++j) {
1134 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1135 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1137 for (j = 0; j < bset2->n_ineq; ++j) {
1138 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1141 lp = isl_basic_set_gauss(lp, NULL);
1142 isl_basic_set_free(bset1);
1143 isl_basic_set_free(bset2);
1144 return lp;
1145 error:
1146 isl_basic_set_free(bset1);
1147 isl_basic_set_free(bset2);
1148 return NULL;
1151 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1152 * for all rays in the homogeneous space of the two cones that correspond
1153 * to the input polyhedra bset1 and bset2.
1155 * We compute s as a vector that satisfies
1157 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1159 * with h_{ij} the normals of the facets of polyhedron i
1160 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1161 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1162 * We first set up an LP with as variables the \alpha{ij}.
1163 * In this formulation, for each polyhedron i,
1164 * the first constraint is the positivity constraint, followed by pairs
1165 * of variables for the equalities, followed by variables for the inequalities.
1166 * We then simply pick a feasible solution and compute s using (*).
1168 * Note that we simply pick any valid direction and make no attempt
1169 * to pick a "good" or even the "best" valid direction.
1171 static struct isl_vec *valid_direction(
1172 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1174 struct isl_basic_set *lp;
1175 struct isl_tab *tab;
1176 struct isl_vec *sample = NULL;
1177 struct isl_vec *dir;
1178 unsigned d;
1179 int i;
1180 int n;
1182 if (!bset1 || !bset2)
1183 goto error;
1184 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1185 isl_basic_set_copy(bset2));
1186 tab = isl_tab_from_basic_set(lp, 0);
1187 sample = isl_tab_get_sample_value(tab);
1188 isl_tab_free(tab);
1189 isl_basic_set_free(lp);
1190 if (!sample)
1191 goto error;
1192 d = isl_basic_set_total_dim(bset1);
1193 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1194 if (!dir)
1195 goto error;
1196 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1197 n = 1;
1198 /* positivity constraint 1 >= 0 */
1199 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1200 for (i = 0; i < bset1->n_eq; ++i) {
1201 isl_int_sub(sample->block.data[n],
1202 sample->block.data[n], sample->block.data[n+1]);
1203 isl_seq_combine(dir->block.data,
1204 bset1->ctx->one, dir->block.data,
1205 sample->block.data[n], bset1->eq[i], 1 + d);
1207 n += 2;
1209 for (i = 0; i < bset1->n_ineq; ++i)
1210 isl_seq_combine(dir->block.data,
1211 bset1->ctx->one, dir->block.data,
1212 sample->block.data[n++], bset1->ineq[i], 1 + d);
1213 isl_vec_free(sample);
1214 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1215 isl_basic_set_free(bset1);
1216 isl_basic_set_free(bset2);
1217 return dir;
1218 error:
1219 isl_vec_free(sample);
1220 isl_basic_set_free(bset1);
1221 isl_basic_set_free(bset2);
1222 return NULL;
1225 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1226 * compute b_i' + A_i' x' >= 0, with
1228 * [ b_i A_i ] [ y' ] [ y' ]
1229 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1231 * In particular, add the "positivity constraint" and then perform
1232 * the mapping.
1234 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1235 struct isl_mat *T)
1237 int k;
1239 if (!bset)
1240 goto error;
1241 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1242 k = isl_basic_set_alloc_inequality(bset);
1243 if (k < 0)
1244 goto error;
1245 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1246 isl_int_set_si(bset->ineq[k][0], 1);
1247 bset = isl_basic_set_preimage(bset, T);
1248 return bset;
1249 error:
1250 isl_mat_free(T);
1251 isl_basic_set_free(bset);
1252 return NULL;
1255 /* Compute the convex hull of a pair of basic sets without any parameters or
1256 * integer divisions, where the convex hull is known to be pointed,
1257 * but the basic sets may be unbounded.
1259 * We turn this problem into the computation of a convex hull of a pair
1260 * _bounded_ polyhedra by "changing the direction of the homogeneous
1261 * dimension". This idea is due to Matthias Koeppe.
1263 * Consider the cones in homogeneous space that correspond to the
1264 * input polyhedra. The rays of these cones are also rays of the
1265 * polyhedra if the coordinate that corresponds to the homogeneous
1266 * dimension is zero. That is, if the inner product of the rays
1267 * with the homogeneous direction is zero.
1268 * The cones in the homogeneous space can also be considered to
1269 * correspond to other pairs of polyhedra by chosing a different
1270 * homogeneous direction. To ensure that both of these polyhedra
1271 * are bounded, we need to make sure that all rays of the cones
1272 * correspond to vertices and not to rays.
1273 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1274 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1275 * The vector s is computed in valid_direction.
1277 * Note that we need to consider _all_ rays of the cones and not just
1278 * the rays that correspond to rays in the polyhedra. If we were to
1279 * only consider those rays and turn them into vertices, then we
1280 * may inadvertently turn some vertices into rays.
1282 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1283 * We therefore transform the two polyhedra such that the selected
1284 * direction is mapped onto this standard direction and then proceed
1285 * with the normal computation.
1286 * Let S be a non-singular square matrix with s as its first row,
1287 * then we want to map the polyhedra to the space
1289 * [ y' ] [ y ] [ y ] [ y' ]
1290 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1292 * We take S to be the unimodular completion of s to limit the growth
1293 * of the coefficients in the following computations.
1295 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1296 * We first move to the homogeneous dimension
1298 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1299 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1301 * Then we change directoin
1303 * [ b_i A_i ] [ y' ] [ y' ]
1304 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1306 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1307 * resulting in b' + A' x' >= 0, which we then convert back
1309 * [ y ] [ y ]
1310 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1312 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1314 static struct isl_basic_set *convex_hull_pair_pointed(
1315 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1317 struct isl_ctx *ctx = NULL;
1318 struct isl_vec *dir = NULL;
1319 struct isl_mat *T = NULL;
1320 struct isl_mat *T2 = NULL;
1321 struct isl_basic_set *hull;
1322 struct isl_set *set;
1324 if (!bset1 || !bset2)
1325 goto error;
1326 ctx = isl_basic_set_get_ctx(bset1);
1327 dir = valid_direction(isl_basic_set_copy(bset1),
1328 isl_basic_set_copy(bset2));
1329 if (!dir)
1330 goto error;
1331 T = isl_mat_alloc(ctx, dir->size, dir->size);
1332 if (!T)
1333 goto error;
1334 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1335 T = isl_mat_unimodular_complete(T, 1);
1336 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1338 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1339 bset2 = homogeneous_map(bset2, T2);
1340 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1341 set = isl_set_add_basic_set(set, bset1);
1342 set = isl_set_add_basic_set(set, bset2);
1343 hull = uset_convex_hull(set);
1344 hull = isl_basic_set_preimage(hull, T);
1346 isl_vec_free(dir);
1348 return hull;
1349 error:
1350 isl_vec_free(dir);
1351 isl_basic_set_free(bset1);
1352 isl_basic_set_free(bset2);
1353 return NULL;
1356 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1357 static struct isl_basic_set *modulo_affine_hull(
1358 struct isl_set *set, struct isl_basic_set *affine_hull);
1360 /* Compute the convex hull of a pair of basic sets without any parameters or
1361 * integer divisions.
1363 * This function is called from uset_convex_hull_unbounded, which
1364 * means that the complete convex hull is unbounded. Some pairs
1365 * of basic sets may still be bounded, though.
1366 * They may even lie inside a lower dimensional space, in which
1367 * case they need to be handled inside their affine hull since
1368 * the main algorithm assumes that the result is full-dimensional.
1370 * If the convex hull of the two basic sets would have a non-trivial
1371 * lineality space, we first project out this lineality space.
1373 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1374 struct isl_basic_set *bset2)
1376 isl_basic_set *lin, *aff;
1377 int bounded1, bounded2;
1379 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1380 return convex_hull_pair_elim(bset1, bset2);
1382 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1383 isl_basic_set_copy(bset2)));
1384 if (!aff)
1385 goto error;
1386 if (aff->n_eq != 0)
1387 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1388 isl_basic_set_free(aff);
1390 bounded1 = isl_basic_set_is_bounded(bset1);
1391 bounded2 = isl_basic_set_is_bounded(bset2);
1393 if (bounded1 < 0 || bounded2 < 0)
1394 goto error;
1396 if (bounded1 && bounded2)
1397 return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1399 if (bounded1 || bounded2)
1400 return convex_hull_pair_pointed(bset1, bset2);
1402 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1403 isl_basic_set_copy(bset2));
1404 if (!lin)
1405 goto error;
1406 if (isl_basic_set_plain_is_universe(lin)) {
1407 isl_basic_set_free(bset1);
1408 isl_basic_set_free(bset2);
1409 return lin;
1411 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1412 struct isl_set *set;
1413 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1414 set = isl_set_add_basic_set(set, bset1);
1415 set = isl_set_add_basic_set(set, bset2);
1416 return modulo_lineality(set, lin);
1418 isl_basic_set_free(lin);
1420 return convex_hull_pair_pointed(bset1, bset2);
1421 error:
1422 isl_basic_set_free(bset1);
1423 isl_basic_set_free(bset2);
1424 return NULL;
1427 /* Compute the lineality space of a basic set.
1428 * We currently do not allow the basic set to have any divs.
1429 * We basically just drop the constants and turn every inequality
1430 * into an equality.
1432 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1434 int i, k;
1435 struct isl_basic_set *lin = NULL;
1436 unsigned dim;
1438 if (!bset)
1439 goto error;
1440 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1441 dim = isl_basic_set_total_dim(bset);
1443 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, dim, 0);
1444 if (!lin)
1445 goto error;
1446 for (i = 0; i < bset->n_eq; ++i) {
1447 k = isl_basic_set_alloc_equality(lin);
1448 if (k < 0)
1449 goto error;
1450 isl_int_set_si(lin->eq[k][0], 0);
1451 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1453 lin = isl_basic_set_gauss(lin, NULL);
1454 if (!lin)
1455 goto error;
1456 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1457 k = isl_basic_set_alloc_equality(lin);
1458 if (k < 0)
1459 goto error;
1460 isl_int_set_si(lin->eq[k][0], 0);
1461 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1462 lin = isl_basic_set_gauss(lin, NULL);
1463 if (!lin)
1464 goto error;
1466 isl_basic_set_free(bset);
1467 return lin;
1468 error:
1469 isl_basic_set_free(lin);
1470 isl_basic_set_free(bset);
1471 return NULL;
1474 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1475 * "underlying" set "set".
1477 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1479 int i;
1480 struct isl_set *lin = NULL;
1482 if (!set)
1483 return NULL;
1484 if (set->n == 0) {
1485 isl_space *dim = isl_set_get_space(set);
1486 isl_set_free(set);
1487 return isl_basic_set_empty(dim);
1490 lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
1491 for (i = 0; i < set->n; ++i)
1492 lin = isl_set_add_basic_set(lin,
1493 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1494 isl_set_free(set);
1495 return isl_set_affine_hull(lin);
1498 /* Compute the convex hull of a set without any parameters or
1499 * integer divisions.
1500 * In each step, we combined two basic sets until only one
1501 * basic set is left.
1502 * The input basic sets are assumed not to have a non-trivial
1503 * lineality space. If any of the intermediate results has
1504 * a non-trivial lineality space, it is projected out.
1506 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1508 struct isl_basic_set *convex_hull = NULL;
1510 convex_hull = isl_set_copy_basic_set(set);
1511 set = isl_set_drop_basic_set(set, convex_hull);
1512 if (!set)
1513 goto error;
1514 while (set->n > 0) {
1515 struct isl_basic_set *t;
1516 t = isl_set_copy_basic_set(set);
1517 if (!t)
1518 goto error;
1519 set = isl_set_drop_basic_set(set, t);
1520 if (!set)
1521 goto error;
1522 convex_hull = convex_hull_pair(convex_hull, t);
1523 if (set->n == 0)
1524 break;
1525 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1526 if (!t)
1527 goto error;
1528 if (isl_basic_set_plain_is_universe(t)) {
1529 isl_basic_set_free(convex_hull);
1530 convex_hull = t;
1531 break;
1533 if (t->n_eq < isl_basic_set_total_dim(t)) {
1534 set = isl_set_add_basic_set(set, convex_hull);
1535 return modulo_lineality(set, t);
1537 isl_basic_set_free(t);
1539 isl_set_free(set);
1540 return convex_hull;
1541 error:
1542 isl_set_free(set);
1543 isl_basic_set_free(convex_hull);
1544 return NULL;
1547 /* Compute an initial hull for wrapping containing a single initial
1548 * facet.
1549 * This function assumes that the given set is bounded.
1551 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1552 struct isl_set *set)
1554 struct isl_mat *bounds = NULL;
1555 unsigned dim;
1556 int k;
1558 if (!hull)
1559 goto error;
1560 bounds = initial_facet_constraint(set);
1561 if (!bounds)
1562 goto error;
1563 k = isl_basic_set_alloc_inequality(hull);
1564 if (k < 0)
1565 goto error;
1566 dim = isl_set_n_dim(set);
1567 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1568 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1569 isl_mat_free(bounds);
1571 return hull;
1572 error:
1573 isl_basic_set_free(hull);
1574 isl_mat_free(bounds);
1575 return NULL;
1578 struct max_constraint {
1579 struct isl_mat *c;
1580 int count;
1581 int ineq;
1584 static int max_constraint_equal(const void *entry, const void *val)
1586 struct max_constraint *a = (struct max_constraint *)entry;
1587 isl_int *b = (isl_int *)val;
1589 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1592 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1593 isl_int *con, unsigned len, int n, int ineq)
1595 struct isl_hash_table_entry *entry;
1596 struct max_constraint *c;
1597 uint32_t c_hash;
1599 c_hash = isl_seq_get_hash(con + 1, len);
1600 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1601 con + 1, 0);
1602 if (!entry)
1603 return;
1604 c = entry->data;
1605 if (c->count < n) {
1606 isl_hash_table_remove(ctx, table, entry);
1607 return;
1609 c->count++;
1610 if (isl_int_gt(c->c->row[0][0], con[0]))
1611 return;
1612 if (isl_int_eq(c->c->row[0][0], con[0])) {
1613 if (ineq)
1614 c->ineq = ineq;
1615 return;
1617 c->c = isl_mat_cow(c->c);
1618 isl_int_set(c->c->row[0][0], con[0]);
1619 c->ineq = ineq;
1622 /* Check whether the constraint hash table "table" constains the constraint
1623 * "con".
1625 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1626 isl_int *con, unsigned len, int n)
1628 struct isl_hash_table_entry *entry;
1629 struct max_constraint *c;
1630 uint32_t c_hash;
1632 c_hash = isl_seq_get_hash(con + 1, len);
1633 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1634 con + 1, 0);
1635 if (!entry)
1636 return 0;
1637 c = entry->data;
1638 if (c->count < n)
1639 return 0;
1640 return isl_int_eq(c->c->row[0][0], con[0]);
1643 /* Check for inequality constraints of a basic set without equalities
1644 * such that the same or more stringent copies of the constraint appear
1645 * in all of the basic sets. Such constraints are necessarily facet
1646 * constraints of the convex hull.
1648 * If the resulting basic set is by chance identical to one of
1649 * the basic sets in "set", then we know that this basic set contains
1650 * all other basic sets and is therefore the convex hull of set.
1651 * In this case we set *is_hull to 1.
1653 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1654 struct isl_set *set, int *is_hull)
1656 int i, j, s, n;
1657 int min_constraints;
1658 int best;
1659 struct max_constraint *constraints = NULL;
1660 struct isl_hash_table *table = NULL;
1661 unsigned total;
1663 *is_hull = 0;
1665 for (i = 0; i < set->n; ++i)
1666 if (set->p[i]->n_eq == 0)
1667 break;
1668 if (i >= set->n)
1669 return hull;
1670 min_constraints = set->p[i]->n_ineq;
1671 best = i;
1672 for (i = best + 1; i < set->n; ++i) {
1673 if (set->p[i]->n_eq != 0)
1674 continue;
1675 if (set->p[i]->n_ineq >= min_constraints)
1676 continue;
1677 min_constraints = set->p[i]->n_ineq;
1678 best = i;
1680 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1681 min_constraints);
1682 if (!constraints)
1683 return hull;
1684 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1685 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1686 goto error;
1688 total = isl_space_dim(set->dim, isl_dim_all);
1689 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1690 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1691 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1692 if (!constraints[i].c)
1693 goto error;
1694 constraints[i].ineq = 1;
1696 for (i = 0; i < min_constraints; ++i) {
1697 struct isl_hash_table_entry *entry;
1698 uint32_t c_hash;
1699 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1700 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1701 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1702 if (!entry)
1703 goto error;
1704 isl_assert(hull->ctx, !entry->data, goto error);
1705 entry->data = &constraints[i];
1708 n = 0;
1709 for (s = 0; s < set->n; ++s) {
1710 if (s == best)
1711 continue;
1713 for (i = 0; i < set->p[s]->n_eq; ++i) {
1714 isl_int *eq = set->p[s]->eq[i];
1715 for (j = 0; j < 2; ++j) {
1716 isl_seq_neg(eq, eq, 1 + total);
1717 update_constraint(hull->ctx, table,
1718 eq, total, n, 0);
1721 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1722 isl_int *ineq = set->p[s]->ineq[i];
1723 update_constraint(hull->ctx, table, ineq, total, n,
1724 set->p[s]->n_eq == 0);
1726 ++n;
1729 for (i = 0; i < min_constraints; ++i) {
1730 if (constraints[i].count < n)
1731 continue;
1732 if (!constraints[i].ineq)
1733 continue;
1734 j = isl_basic_set_alloc_inequality(hull);
1735 if (j < 0)
1736 goto error;
1737 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1740 for (s = 0; s < set->n; ++s) {
1741 if (set->p[s]->n_eq)
1742 continue;
1743 if (set->p[s]->n_ineq != hull->n_ineq)
1744 continue;
1745 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1746 isl_int *ineq = set->p[s]->ineq[i];
1747 if (!has_constraint(hull->ctx, table, ineq, total, n))
1748 break;
1750 if (i == set->p[s]->n_ineq)
1751 *is_hull = 1;
1754 isl_hash_table_clear(table);
1755 for (i = 0; i < min_constraints; ++i)
1756 isl_mat_free(constraints[i].c);
1757 free(constraints);
1758 free(table);
1759 return hull;
1760 error:
1761 isl_hash_table_clear(table);
1762 free(table);
1763 if (constraints)
1764 for (i = 0; i < min_constraints; ++i)
1765 isl_mat_free(constraints[i].c);
1766 free(constraints);
1767 return hull;
1770 /* Create a template for the convex hull of "set" and fill it up
1771 * obvious facet constraints, if any. If the result happens to
1772 * be the convex hull of "set" then *is_hull is set to 1.
1774 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1776 struct isl_basic_set *hull;
1777 unsigned n_ineq;
1778 int i;
1780 n_ineq = 1;
1781 for (i = 0; i < set->n; ++i) {
1782 n_ineq += set->p[i]->n_eq;
1783 n_ineq += set->p[i]->n_ineq;
1785 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1786 hull = isl_basic_set_set_rational(hull);
1787 if (!hull)
1788 return NULL;
1789 return common_constraints(hull, set, is_hull);
1792 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1794 struct isl_basic_set *hull;
1795 int is_hull;
1797 hull = proto_hull(set, &is_hull);
1798 if (hull && !is_hull) {
1799 if (hull->n_ineq == 0)
1800 hull = initial_hull(hull, set);
1801 hull = extend(hull, set);
1803 isl_set_free(set);
1805 return hull;
1808 /* Compute the convex hull of a set without any parameters or
1809 * integer divisions. Depending on whether the set is bounded,
1810 * we pass control to the wrapping based convex hull or
1811 * the Fourier-Motzkin elimination based convex hull.
1812 * We also handle a few special cases before checking the boundedness.
1814 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1816 struct isl_basic_set *convex_hull = NULL;
1817 struct isl_basic_set *lin;
1819 if (isl_set_n_dim(set) == 0)
1820 return convex_hull_0d(set);
1822 set = isl_set_coalesce(set);
1823 set = isl_set_set_rational(set);
1825 if (!set)
1826 goto error;
1827 if (!set)
1828 return NULL;
1829 if (set->n == 1) {
1830 convex_hull = isl_basic_set_copy(set->p[0]);
1831 isl_set_free(set);
1832 return convex_hull;
1834 if (isl_set_n_dim(set) == 1)
1835 return convex_hull_1d(set);
1837 if (isl_set_is_bounded(set) &&
1838 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1839 return uset_convex_hull_wrap(set);
1841 lin = uset_combined_lineality_space(isl_set_copy(set));
1842 if (!lin)
1843 goto error;
1844 if (isl_basic_set_plain_is_universe(lin)) {
1845 isl_set_free(set);
1846 return lin;
1848 if (lin->n_eq < isl_basic_set_total_dim(lin))
1849 return modulo_lineality(set, lin);
1850 isl_basic_set_free(lin);
1852 return uset_convex_hull_unbounded(set);
1853 error:
1854 isl_set_free(set);
1855 isl_basic_set_free(convex_hull);
1856 return NULL;
1859 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1860 * without parameters or divs and where the convex hull of set is
1861 * known to be full-dimensional.
1863 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1865 struct isl_basic_set *convex_hull = NULL;
1867 if (!set)
1868 goto error;
1870 if (isl_set_n_dim(set) == 0) {
1871 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1872 isl_set_free(set);
1873 convex_hull = isl_basic_set_set_rational(convex_hull);
1874 return convex_hull;
1877 set = isl_set_set_rational(set);
1878 set = isl_set_coalesce(set);
1879 if (!set)
1880 goto error;
1881 if (set->n == 1) {
1882 convex_hull = isl_basic_set_copy(set->p[0]);
1883 isl_set_free(set);
1884 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1885 return convex_hull;
1887 if (isl_set_n_dim(set) == 1)
1888 return convex_hull_1d(set);
1890 return uset_convex_hull_wrap(set);
1891 error:
1892 isl_set_free(set);
1893 return NULL;
1896 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1897 * We first remove the equalities (transforming the set), compute the
1898 * convex hull of the transformed set and then add the equalities back
1899 * (after performing the inverse transformation.
1901 static struct isl_basic_set *modulo_affine_hull(
1902 struct isl_set *set, struct isl_basic_set *affine_hull)
1904 struct isl_mat *T;
1905 struct isl_mat *T2;
1906 struct isl_basic_set *dummy;
1907 struct isl_basic_set *convex_hull;
1909 dummy = isl_basic_set_remove_equalities(
1910 isl_basic_set_copy(affine_hull), &T, &T2);
1911 if (!dummy)
1912 goto error;
1913 isl_basic_set_free(dummy);
1914 set = isl_set_preimage(set, T);
1915 convex_hull = uset_convex_hull(set);
1916 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1917 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1918 return convex_hull;
1919 error:
1920 isl_basic_set_free(affine_hull);
1921 isl_set_free(set);
1922 return NULL;
1925 /* Return an empty basic map living in the same space as "map".
1927 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1928 __isl_take isl_map *map)
1930 isl_space *space;
1932 space = isl_map_get_space(map);
1933 isl_map_free(map);
1934 return isl_basic_map_empty(space);
1937 /* Compute the convex hull of a map.
1939 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1940 * specifically, the wrapping of facets to obtain new facets.
1942 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1944 struct isl_basic_set *bset;
1945 struct isl_basic_map *model = NULL;
1946 struct isl_basic_set *affine_hull = NULL;
1947 struct isl_basic_map *convex_hull = NULL;
1948 struct isl_set *set = NULL;
1950 map = isl_map_detect_equalities(map);
1951 map = isl_map_align_divs(map);
1952 if (!map)
1953 goto error;
1955 if (map->n == 0)
1956 return replace_map_by_empty_basic_map(map);
1958 model = isl_basic_map_copy(map->p[0]);
1959 set = isl_map_underlying_set(map);
1960 if (!set)
1961 goto error;
1963 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1964 if (!affine_hull)
1965 goto error;
1966 if (affine_hull->n_eq != 0)
1967 bset = modulo_affine_hull(set, affine_hull);
1968 else {
1969 isl_basic_set_free(affine_hull);
1970 bset = uset_convex_hull(set);
1973 convex_hull = isl_basic_map_overlying_set(bset, model);
1974 if (!convex_hull)
1975 return NULL;
1977 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1978 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1979 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1980 return convex_hull;
1981 error:
1982 isl_set_free(set);
1983 isl_basic_map_free(model);
1984 return NULL;
1987 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1989 return (struct isl_basic_set *)
1990 isl_map_convex_hull((struct isl_map *)set);
1993 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1995 isl_basic_map *hull;
1997 hull = isl_map_convex_hull(map);
1998 return isl_basic_map_remove_divs(hull);
2001 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
2003 return (isl_basic_set *)isl_map_polyhedral_hull((isl_map *)set);
2006 struct sh_data_entry {
2007 struct isl_hash_table *table;
2008 struct isl_tab *tab;
2011 /* Holds the data needed during the simple hull computation.
2012 * In particular,
2013 * n the number of basic sets in the original set
2014 * hull_table a hash table of already computed constraints
2015 * in the simple hull
2016 * p for each basic set,
2017 * table a hash table of the constraints
2018 * tab the tableau corresponding to the basic set
2020 struct sh_data {
2021 struct isl_ctx *ctx;
2022 unsigned n;
2023 struct isl_hash_table *hull_table;
2024 struct sh_data_entry p[1];
2027 static void sh_data_free(struct sh_data *data)
2029 int i;
2031 if (!data)
2032 return;
2033 isl_hash_table_free(data->ctx, data->hull_table);
2034 for (i = 0; i < data->n; ++i) {
2035 isl_hash_table_free(data->ctx, data->p[i].table);
2036 isl_tab_free(data->p[i].tab);
2038 free(data);
2041 struct ineq_cmp_data {
2042 unsigned len;
2043 isl_int *p;
2046 static int has_ineq(const void *entry, const void *val)
2048 isl_int *row = (isl_int *)entry;
2049 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2051 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2052 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2055 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2056 isl_int *ineq, unsigned len)
2058 uint32_t c_hash;
2059 struct ineq_cmp_data v;
2060 struct isl_hash_table_entry *entry;
2062 v.len = len;
2063 v.p = ineq;
2064 c_hash = isl_seq_get_hash(ineq + 1, len);
2065 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2066 if (!entry)
2067 return - 1;
2068 entry->data = ineq;
2069 return 0;
2072 /* Fill hash table "table" with the constraints of "bset".
2073 * Equalities are added as two inequalities.
2074 * The value in the hash table is a pointer to the (in)equality of "bset".
2076 static int hash_basic_set(struct isl_hash_table *table,
2077 struct isl_basic_set *bset)
2079 int i, j;
2080 unsigned dim = isl_basic_set_total_dim(bset);
2082 for (i = 0; i < bset->n_eq; ++i) {
2083 for (j = 0; j < 2; ++j) {
2084 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2085 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2086 return -1;
2089 for (i = 0; i < bset->n_ineq; ++i) {
2090 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2091 return -1;
2093 return 0;
2096 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2098 struct sh_data *data;
2099 int i;
2101 data = isl_calloc(set->ctx, struct sh_data,
2102 sizeof(struct sh_data) +
2103 (set->n - 1) * sizeof(struct sh_data_entry));
2104 if (!data)
2105 return NULL;
2106 data->ctx = set->ctx;
2107 data->n = set->n;
2108 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2109 if (!data->hull_table)
2110 goto error;
2111 for (i = 0; i < set->n; ++i) {
2112 data->p[i].table = isl_hash_table_alloc(set->ctx,
2113 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2114 if (!data->p[i].table)
2115 goto error;
2116 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2117 goto error;
2119 return data;
2120 error:
2121 sh_data_free(data);
2122 return NULL;
2125 /* Check if inequality "ineq" is a bound for basic set "j" or if
2126 * it can be relaxed (by increasing the constant term) to become
2127 * a bound for that basic set. In the latter case, the constant
2128 * term is updated.
2129 * Relaxation of the constant term is only allowed if "shift" is set.
2131 * Return 1 if "ineq" is a bound
2132 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2133 * -1 if some error occurred
2135 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2136 isl_int *ineq, int shift)
2138 enum isl_lp_result res;
2139 isl_int opt;
2141 if (!data->p[j].tab) {
2142 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2143 if (!data->p[j].tab)
2144 return -1;
2147 isl_int_init(opt);
2149 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2150 &opt, NULL, 0);
2151 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2152 if (shift)
2153 isl_int_sub(ineq[0], ineq[0], opt);
2154 else
2155 res = isl_lp_unbounded;
2158 isl_int_clear(opt);
2160 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2161 res == isl_lp_unbounded ? 0 : -1;
2164 /* Set the constant term of "ineq" to the maximum of those of the constraints
2165 * in the basic sets of "set" following "i" that are parallel to "ineq".
2166 * That is, if any of the basic sets of "set" following "i" have a more
2167 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2168 * "c_hash" is the hash value of the linear part of "ineq".
2169 * "v" has been set up for use by has_ineq.
2171 * Note that the two inequality constraints corresponding to an equality are
2172 * represented by the same inequality constraint in data->p[j].table
2173 * (but with different hash values). This means the constraint (or at
2174 * least its constant term) may need to be temporarily negated to get
2175 * the actually hashed constraint.
2177 static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
2178 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2180 int j;
2181 isl_ctx *ctx;
2182 struct isl_hash_table_entry *entry;
2184 ctx = isl_set_get_ctx(set);
2185 for (j = i + 1; j < set->n; ++j) {
2186 int neg;
2187 isl_int *ineq_j;
2189 entry = isl_hash_table_find(ctx, data->p[j].table,
2190 c_hash, &has_ineq, v, 0);
2191 if (!entry)
2192 continue;
2194 ineq_j = entry->data;
2195 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2196 if (neg)
2197 isl_int_neg(ineq_j[0], ineq_j[0]);
2198 if (isl_int_gt(ineq_j[0], ineq[0]))
2199 isl_int_set(ineq[0], ineq_j[0]);
2200 if (neg)
2201 isl_int_neg(ineq_j[0], ineq_j[0]);
2205 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2206 * become a bound on the whole set. If so, add the (relaxed) inequality
2207 * to "hull". Relaxation is only allowed if "shift" is set.
2209 * We first check if "hull" already contains a translate of the inequality.
2210 * If so, we are done.
2211 * Then, we check if any of the previous basic sets contains a translate
2212 * of the inequality. If so, then we have already considered this
2213 * inequality and we are done.
2214 * Otherwise, for each basic set other than "i", we check if the inequality
2215 * is a bound on the basic set, but first replace the constant term
2216 * by the maximal value of any translate of the inequality in any
2217 * of the following basic sets.
2218 * For previous basic sets, we know that they do not contain a translate
2219 * of the inequality, so we directly call is_bound.
2220 * For following basic sets, we first check if a translate of the
2221 * inequality appears in its description. If so, the constant term
2222 * of the inequality has already been updated with respect to this
2223 * translate and the inequality is therefore known to be a bound
2224 * of this basic set.
2226 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2227 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
2228 int shift)
2230 uint32_t c_hash;
2231 struct ineq_cmp_data v;
2232 struct isl_hash_table_entry *entry;
2233 int j, k;
2235 if (!hull)
2236 return NULL;
2238 v.len = isl_basic_set_total_dim(hull);
2239 v.p = ineq;
2240 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2242 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2243 has_ineq, &v, 0);
2244 if (entry)
2245 return hull;
2247 for (j = 0; j < i; ++j) {
2248 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2249 c_hash, has_ineq, &v, 0);
2250 if (entry)
2251 break;
2253 if (j < i)
2254 return hull;
2256 k = isl_basic_set_alloc_inequality(hull);
2257 if (k < 0)
2258 goto error;
2259 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2261 set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
2262 for (j = 0; j < i; ++j) {
2263 int bound;
2264 bound = is_bound(data, set, j, hull->ineq[k], shift);
2265 if (bound < 0)
2266 goto error;
2267 if (!bound)
2268 break;
2270 if (j < i) {
2271 isl_basic_set_free_inequality(hull, 1);
2272 return hull;
2275 for (j = i + 1; j < set->n; ++j) {
2276 int bound;
2277 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2278 c_hash, has_ineq, &v, 0);
2279 if (entry)
2280 continue;
2281 bound = is_bound(data, set, j, hull->ineq[k], shift);
2282 if (bound < 0)
2283 goto error;
2284 if (!bound)
2285 break;
2287 if (j < set->n) {
2288 isl_basic_set_free_inequality(hull, 1);
2289 return hull;
2292 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2293 has_ineq, &v, 1);
2294 if (!entry)
2295 goto error;
2296 entry->data = hull->ineq[k];
2298 return hull;
2299 error:
2300 isl_basic_set_free(hull);
2301 return NULL;
2304 /* Check if any inequality from basic set "i" is or can be relaxed to
2305 * become a bound on the whole set. If so, add the (relaxed) inequality
2306 * to "hull". Relaxation is only allowed if "shift" is set.
2308 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2309 struct sh_data *data, struct isl_set *set, int i, int shift)
2311 int j, k;
2312 unsigned dim = isl_basic_set_total_dim(bset);
2314 for (j = 0; j < set->p[i]->n_eq; ++j) {
2315 for (k = 0; k < 2; ++k) {
2316 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2317 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2318 shift);
2321 for (j = 0; j < set->p[i]->n_ineq; ++j)
2322 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2323 return bset;
2326 /* Compute a superset of the convex hull of set that is described
2327 * by only (translates of) the constraints in the constituents of set.
2328 * Translation is only allowed if "shift" is set.
2330 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2331 int shift)
2333 struct sh_data *data = NULL;
2334 struct isl_basic_set *hull = NULL;
2335 unsigned n_ineq;
2336 int i;
2338 if (!set)
2339 return NULL;
2341 n_ineq = 0;
2342 for (i = 0; i < set->n; ++i) {
2343 if (!set->p[i])
2344 goto error;
2345 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2348 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2349 if (!hull)
2350 goto error;
2352 data = sh_data_alloc(set, n_ineq);
2353 if (!data)
2354 goto error;
2356 for (i = 0; i < set->n; ++i)
2357 hull = add_bounds(hull, data, set, i, shift);
2359 sh_data_free(data);
2360 isl_set_free(set);
2362 return hull;
2363 error:
2364 sh_data_free(data);
2365 isl_basic_set_free(hull);
2366 isl_set_free(set);
2367 return NULL;
2370 /* Compute a superset of the convex hull of map that is described
2371 * by only (translates of) the constraints in the constituents of map.
2372 * Handle trivial cases where map is NULL or contains at most one disjunct.
2374 static __isl_give isl_basic_map *map_simple_hull_trivial(
2375 __isl_take isl_map *map)
2377 isl_basic_map *hull;
2379 if (!map)
2380 return NULL;
2381 if (map->n == 0)
2382 return replace_map_by_empty_basic_map(map);
2384 hull = isl_basic_map_copy(map->p[0]);
2385 isl_map_free(map);
2386 return hull;
2389 /* Return a copy of the simple hull cached inside "map".
2390 * "shift" determines whether to return the cached unshifted or shifted
2391 * simple hull.
2393 static __isl_give isl_basic_map *cached_simple_hull(__isl_take isl_map *map,
2394 int shift)
2396 isl_basic_map *hull;
2398 hull = isl_basic_map_copy(map->cached_simple_hull[shift]);
2399 isl_map_free(map);
2401 return hull;
2404 /* Compute a superset of the convex hull of map that is described
2405 * by only (translates of) the constraints in the constituents of map.
2406 * Translation is only allowed if "shift" is set.
2408 * The constraints are sorted while removing redundant constraints
2409 * in order to indicate a preference of which constraints should
2410 * be preserved. In particular, pairs of constraints that are
2411 * sorted together are preferred to either both be preserved
2412 * or both be removed. The sorting is performed inside
2413 * isl_basic_map_remove_redundancies.
2415 * The result of the computation is stored in map->cached_simple_hull[shift]
2416 * such that it can be reused in subsequent calls. The cache is cleared
2417 * whenever the map is modified (in isl_map_cow).
2418 * Note that the results need to be stored in the input map for there
2419 * to be any chance that they may get reused. In particular, they
2420 * are stored in a copy of the input map that is saved before
2421 * the integer division alignment.
2423 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2424 int shift)
2426 struct isl_set *set = NULL;
2427 struct isl_basic_map *model = NULL;
2428 struct isl_basic_map *hull;
2429 struct isl_basic_map *affine_hull;
2430 struct isl_basic_set *bset = NULL;
2431 isl_map *input;
2433 if (!map || map->n <= 1)
2434 return map_simple_hull_trivial(map);
2436 if (map->cached_simple_hull[shift])
2437 return cached_simple_hull(map, shift);
2439 map = isl_map_detect_equalities(map);
2440 if (!map || map->n <= 1)
2441 return map_simple_hull_trivial(map);
2442 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2443 input = isl_map_copy(map);
2444 map = isl_map_align_divs(map);
2445 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2447 set = isl_map_underlying_set(map);
2449 bset = uset_simple_hull(set, shift);
2451 hull = isl_basic_map_overlying_set(bset, model);
2453 hull = isl_basic_map_intersect(hull, affine_hull);
2454 hull = isl_basic_map_remove_redundancies(hull);
2456 if (hull) {
2457 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2458 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2461 hull = isl_basic_map_finalize(hull);
2462 if (input)
2463 input->cached_simple_hull[shift] = isl_basic_map_copy(hull);
2464 isl_map_free(input);
2466 return hull;
2469 /* Compute a superset of the convex hull of map that is described
2470 * by only translates of the constraints in the constituents of map.
2472 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2474 return map_simple_hull(map, 1);
2477 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2479 return (struct isl_basic_set *)
2480 isl_map_simple_hull((struct isl_map *)set);
2483 /* Compute a superset of the convex hull of map that is described
2484 * by only the constraints in the constituents of map.
2486 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2487 __isl_take isl_map *map)
2489 return map_simple_hull(map, 0);
2492 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2493 __isl_take isl_set *set)
2495 return isl_map_unshifted_simple_hull(set);
2498 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2499 * A constraint that appears with different constant terms
2500 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2501 * (i.e., greatest) constant term.
2502 * "bmap1" and "bmap2" are assumed to have the same (known)
2503 * integer divisions.
2504 * The constraints of both "bmap1" and "bmap2" are assumed
2505 * to have been sorted using isl_basic_map_sort_constraints.
2507 * Run through the inequality constraints of "bmap1" and "bmap2"
2508 * in sorted order.
2509 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2510 * is removed.
2511 * If a match is found, the constraint is kept. If needed, the constant
2512 * term of the constraint is adjusted.
2514 static __isl_give isl_basic_map *select_shared_inequalities(
2515 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2517 int i1, i2;
2519 bmap1 = isl_basic_map_cow(bmap1);
2520 if (!bmap1 || !bmap2)
2521 return isl_basic_map_free(bmap1);
2523 i1 = bmap1->n_ineq - 1;
2524 i2 = bmap2->n_ineq - 1;
2525 while (bmap1 && i1 >= 0 && i2 >= 0) {
2526 int cmp;
2528 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2529 bmap2->ineq[i2]);
2530 if (cmp < 0) {
2531 --i2;
2532 continue;
2534 if (cmp > 0) {
2535 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2536 bmap1 = isl_basic_map_free(bmap1);
2537 --i1;
2538 continue;
2540 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2541 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2542 --i1;
2543 --i2;
2545 for (; i1 >= 0; --i1)
2546 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2547 bmap1 = isl_basic_map_free(bmap1);
2549 return bmap1;
2552 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2553 * "bmap1" and "bmap2" are assumed to have the same (known)
2554 * integer divisions.
2556 * Run through the equality constraints of "bmap1" and "bmap2".
2557 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2558 * is removed.
2560 static __isl_give isl_basic_map *select_shared_equalities(
2561 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2563 int i1, i2;
2564 unsigned total;
2566 bmap1 = isl_basic_map_cow(bmap1);
2567 if (!bmap1 || !bmap2)
2568 return isl_basic_map_free(bmap1);
2570 total = isl_basic_map_total_dim(bmap1);
2572 i1 = bmap1->n_eq - 1;
2573 i2 = bmap2->n_eq - 1;
2574 while (bmap1 && i1 >= 0 && i2 >= 0) {
2575 int last1, last2;
2577 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2578 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2579 if (last1 > last2) {
2580 --i2;
2581 continue;
2583 if (last1 < last2) {
2584 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2585 bmap1 = isl_basic_map_free(bmap1);
2586 --i1;
2587 continue;
2589 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2590 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2591 bmap1 = isl_basic_map_free(bmap1);
2593 --i1;
2594 --i2;
2596 for (; i1 >= 0; --i1)
2597 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2598 bmap1 = isl_basic_map_free(bmap1);
2600 return bmap1;
2603 /* Compute a superset of "bmap1" and "bmap2" that is described
2604 * by only the constraints that appear in both "bmap1" and "bmap2".
2606 * First drop constraints that involve unknown integer divisions
2607 * since it is not trivial to check whether two such integer divisions
2608 * in different basic maps are the same.
2609 * Then align the remaining (known) divs and sort the constraints.
2610 * Finally drop all inequalities and equalities from "bmap1" that
2611 * do not also appear in "bmap2".
2613 __isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2614 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2616 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2617 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2618 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2619 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2620 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2621 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2622 bmap1 = isl_basic_map_sort_constraints(bmap1);
2623 bmap2 = isl_basic_map_sort_constraints(bmap2);
2625 bmap1 = select_shared_inequalities(bmap1, bmap2);
2626 bmap1 = select_shared_equalities(bmap1, bmap2);
2628 isl_basic_map_free(bmap2);
2629 bmap1 = isl_basic_map_finalize(bmap1);
2630 return bmap1;
2633 /* Compute a superset of the convex hull of "map" that is described
2634 * by only the constraints in the constituents of "map".
2635 * In particular, the result is composed of constraints that appear
2636 * in each of the basic maps of "map"
2638 * Constraints that involve unknown integer divisions are dropped
2639 * since it is not trivial to check whether two such integer divisions
2640 * in different basic maps are the same.
2642 * The hull is initialized from the first basic map and then
2643 * updated with respect to the other basic maps in turn.
2645 __isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2646 __isl_take isl_map *map)
2648 int i;
2649 isl_basic_map *hull;
2651 if (!map)
2652 return NULL;
2653 if (map->n <= 1)
2654 return map_simple_hull_trivial(map);
2655 map = isl_map_drop_constraint_involving_unknown_divs(map);
2656 hull = isl_basic_map_copy(map->p[0]);
2657 for (i = 1; i < map->n; ++i) {
2658 isl_basic_map *bmap_i;
2660 bmap_i = isl_basic_map_copy(map->p[i]);
2661 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2664 isl_map_free(map);
2665 return hull;
2668 /* Compute a superset of the convex hull of "set" that is described
2669 * by only the constraints in the constituents of "set".
2670 * In particular, the result is composed of constraints that appear
2671 * in each of the basic sets of "set"
2673 __isl_give isl_basic_set *isl_set_plain_unshifted_simple_hull(
2674 __isl_take isl_set *set)
2676 return isl_map_plain_unshifted_simple_hull(set);
2679 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2681 * For each basic set in "set", we first check if the basic set
2682 * contains a translate of "ineq". If this translate is more relaxed,
2683 * then we assume that "ineq" is not a bound on this basic set.
2684 * Otherwise, we know that it is a bound.
2685 * If the basic set does not contain a translate of "ineq", then
2686 * we call is_bound to perform the test.
2688 static __isl_give isl_basic_set *add_bound_from_constraint(
2689 __isl_take isl_basic_set *hull, struct sh_data *data,
2690 __isl_keep isl_set *set, isl_int *ineq)
2692 int i, k;
2693 isl_ctx *ctx;
2694 uint32_t c_hash;
2695 struct ineq_cmp_data v;
2697 if (!hull || !set)
2698 return isl_basic_set_free(hull);
2700 v.len = isl_basic_set_total_dim(hull);
2701 v.p = ineq;
2702 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2704 ctx = isl_basic_set_get_ctx(hull);
2705 for (i = 0; i < set->n; ++i) {
2706 int bound;
2707 struct isl_hash_table_entry *entry;
2709 entry = isl_hash_table_find(ctx, data->p[i].table,
2710 c_hash, &has_ineq, &v, 0);
2711 if (entry) {
2712 isl_int *ineq_i = entry->data;
2713 int neg, more_relaxed;
2715 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2716 if (neg)
2717 isl_int_neg(ineq_i[0], ineq_i[0]);
2718 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2719 if (neg)
2720 isl_int_neg(ineq_i[0], ineq_i[0]);
2721 if (more_relaxed)
2722 break;
2723 else
2724 continue;
2726 bound = is_bound(data, set, i, ineq, 0);
2727 if (bound < 0)
2728 return isl_basic_set_free(hull);
2729 if (!bound)
2730 break;
2732 if (i < set->n)
2733 return hull;
2735 k = isl_basic_set_alloc_inequality(hull);
2736 if (k < 0)
2737 return isl_basic_set_free(hull);
2738 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2740 return hull;
2743 /* Compute a superset of the convex hull of "set" that is described
2744 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2745 * has no parameters or integer divisions.
2747 * The inequalities in "ineq" are assumed to have been sorted such
2748 * that constraints with the same linear part appear together and
2749 * that among constraints with the same linear part, those with
2750 * smaller constant term appear first.
2752 * We reuse the same data structure that is used by uset_simple_hull,
2753 * but we do not need the hull table since we will not consider the
2754 * same constraint more than once. We therefore allocate it with zero size.
2756 * We run through the constraints and try to add them one by one,
2757 * skipping identical constraints. If we have added a constraint and
2758 * the next constraint is a more relaxed translate, then we skip this
2759 * next constraint as well.
2761 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2762 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2764 int i;
2765 int last_added = 0;
2766 struct sh_data *data = NULL;
2767 isl_basic_set *hull = NULL;
2768 unsigned dim;
2770 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2771 if (!hull)
2772 goto error;
2774 data = sh_data_alloc(set, 0);
2775 if (!data)
2776 goto error;
2778 dim = isl_set_dim(set, isl_dim_set);
2779 for (i = 0; i < n_ineq; ++i) {
2780 int hull_n_ineq = hull->n_ineq;
2781 int parallel;
2783 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2784 dim);
2785 if (parallel &&
2786 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2787 continue;
2788 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2789 if (!hull)
2790 goto error;
2791 last_added = hull->n_ineq > hull_n_ineq;
2794 sh_data_free(data);
2795 isl_set_free(set);
2796 return hull;
2797 error:
2798 sh_data_free(data);
2799 isl_set_free(set);
2800 isl_basic_set_free(hull);
2801 return NULL;
2804 /* Collect pointers to all the inequalities in the elements of "list"
2805 * in "ineq". For equalities, store both a pointer to the equality and
2806 * a pointer to its opposite, which is first copied to "mat".
2807 * "ineq" and "mat" are assumed to have been preallocated to the right size
2808 * (the number of inequalities + 2 times the number of equalites and
2809 * the number of equalities, respectively).
2811 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2812 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2814 int i, j, n, n_eq, n_ineq;
2816 if (!mat)
2817 return NULL;
2819 n_eq = 0;
2820 n_ineq = 0;
2821 n = isl_basic_set_list_n_basic_set(list);
2822 for (i = 0; i < n; ++i) {
2823 isl_basic_set *bset;
2824 bset = isl_basic_set_list_get_basic_set(list, i);
2825 if (!bset)
2826 return isl_mat_free(mat);
2827 for (j = 0; j < bset->n_eq; ++j) {
2828 ineq[n_ineq++] = mat->row[n_eq];
2829 ineq[n_ineq++] = bset->eq[j];
2830 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2832 for (j = 0; j < bset->n_ineq; ++j)
2833 ineq[n_ineq++] = bset->ineq[j];
2834 isl_basic_set_free(bset);
2837 return mat;
2840 /* Comparison routine for use as an isl_sort callback.
2842 * Constraints with the same linear part are sorted together and
2843 * among constraints with the same linear part, those with smaller
2844 * constant term are sorted first.
2846 static int cmp_ineq(const void *a, const void *b, void *arg)
2848 unsigned dim = *(unsigned *) arg;
2849 isl_int * const *ineq1 = a;
2850 isl_int * const *ineq2 = b;
2851 int cmp;
2853 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2854 if (cmp != 0)
2855 return cmp;
2856 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2859 /* Compute a superset of the convex hull of "set" that is described
2860 * by only constraints in the elements of "list", where "set" has
2861 * no parameters or integer divisions.
2863 * We collect all the constraints in those elements and then
2864 * sort the constraints such that constraints with the same linear part
2865 * are sorted together and that those with smaller constant term are
2866 * sorted first.
2868 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2869 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2871 int i, n, n_eq, n_ineq;
2872 unsigned dim;
2873 isl_ctx *ctx;
2874 isl_mat *mat = NULL;
2875 isl_int **ineq = NULL;
2876 isl_basic_set *hull;
2878 if (!set)
2879 goto error;
2880 ctx = isl_set_get_ctx(set);
2882 n_eq = 0;
2883 n_ineq = 0;
2884 n = isl_basic_set_list_n_basic_set(list);
2885 for (i = 0; i < n; ++i) {
2886 isl_basic_set *bset;
2887 bset = isl_basic_set_list_get_basic_set(list, i);
2888 if (!bset)
2889 goto error;
2890 n_eq += bset->n_eq;
2891 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2892 isl_basic_set_free(bset);
2895 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2896 if (n_ineq > 0 && !ineq)
2897 goto error;
2899 dim = isl_set_dim(set, isl_dim_set);
2900 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2901 mat = collect_inequalities(mat, list, ineq);
2902 if (!mat)
2903 goto error;
2905 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2906 goto error;
2908 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2910 isl_mat_free(mat);
2911 free(ineq);
2912 isl_basic_set_list_free(list);
2913 return hull;
2914 error:
2915 isl_mat_free(mat);
2916 free(ineq);
2917 isl_set_free(set);
2918 isl_basic_set_list_free(list);
2919 return NULL;
2922 /* Compute a superset of the convex hull of "map" that is described
2923 * by only constraints in the elements of "list".
2925 * If the list is empty, then we can only describe the universe set.
2926 * If the input map is empty, then all constraints are valid, so
2927 * we return the intersection of the elements in "list".
2929 * Otherwise, we align all divs and temporarily treat them
2930 * as regular variables, computing the unshifted simple hull in
2931 * uset_unshifted_simple_hull_from_basic_set_list.
2933 static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2934 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2936 isl_basic_map *model;
2937 isl_basic_map *hull;
2938 isl_set *set;
2939 isl_basic_set_list *bset_list;
2941 if (!map || !list)
2942 goto error;
2944 if (isl_basic_map_list_n_basic_map(list) == 0) {
2945 isl_space *space;
2947 space = isl_map_get_space(map);
2948 isl_map_free(map);
2949 isl_basic_map_list_free(list);
2950 return isl_basic_map_universe(space);
2952 if (isl_map_plain_is_empty(map)) {
2953 isl_map_free(map);
2954 return isl_basic_map_list_intersect(list);
2957 map = isl_map_align_divs_to_basic_map_list(map, list);
2958 if (!map)
2959 goto error;
2960 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2962 model = isl_basic_map_list_get_basic_map(list, 0);
2964 set = isl_map_underlying_set(map);
2965 bset_list = isl_basic_map_list_underlying_set(list);
2967 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2968 hull = isl_basic_map_overlying_set(hull, model);
2970 return hull;
2971 error:
2972 isl_map_free(map);
2973 isl_basic_map_list_free(list);
2974 return NULL;
2977 /* Return a sequence of the basic maps that make up the maps in "list".
2979 static __isl_give isl_basic_set_list *collect_basic_maps(
2980 __isl_take isl_map_list *list)
2982 int i, n;
2983 isl_ctx *ctx;
2984 isl_basic_map_list *bmap_list;
2986 if (!list)
2987 return NULL;
2988 n = isl_map_list_n_map(list);
2989 ctx = isl_map_list_get_ctx(list);
2990 bmap_list = isl_basic_map_list_alloc(ctx, 0);
2992 for (i = 0; i < n; ++i) {
2993 isl_map *map;
2994 isl_basic_map_list *list_i;
2996 map = isl_map_list_get_map(list, i);
2997 map = isl_map_compute_divs(map);
2998 list_i = isl_map_get_basic_map_list(map);
2999 isl_map_free(map);
3000 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
3003 isl_map_list_free(list);
3004 return bmap_list;
3007 /* Compute a superset of the convex hull of "map" that is described
3008 * by only constraints in the elements of "list".
3010 * If "map" is the universe, then the convex hull (and therefore
3011 * any superset of the convexhull) is the universe as well.
3013 * Otherwise, we collect all the basic maps in the map list and
3014 * continue with map_unshifted_simple_hull_from_basic_map_list.
3016 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
3017 __isl_take isl_map *map, __isl_take isl_map_list *list)
3019 isl_basic_map_list *bmap_list;
3020 int is_universe;
3022 is_universe = isl_map_plain_is_universe(map);
3023 if (is_universe < 0)
3024 map = isl_map_free(map);
3025 if (is_universe < 0 || is_universe) {
3026 isl_map_list_free(list);
3027 return isl_map_unshifted_simple_hull(map);
3030 bmap_list = collect_basic_maps(list);
3031 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
3034 /* Compute a superset of the convex hull of "set" that is described
3035 * by only constraints in the elements of "list".
3037 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
3038 __isl_take isl_set *set, __isl_take isl_set_list *list)
3040 return isl_map_unshifted_simple_hull_from_map_list(set, list);
3043 /* Given a set "set", return parametric bounds on the dimension "dim".
3045 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
3047 unsigned set_dim = isl_set_dim(set, isl_dim_set);
3048 set = isl_set_copy(set);
3049 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
3050 set = isl_set_eliminate_dims(set, 0, dim);
3051 return isl_set_convex_hull(set);
3054 /* Computes a "simple hull" and then check if each dimension in the
3055 * resulting hull is bounded by a symbolic constant. If not, the
3056 * hull is intersected with the corresponding bounds on the whole set.
3058 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
3060 int i, j;
3061 struct isl_basic_set *hull;
3062 unsigned nparam, left;
3063 int removed_divs = 0;
3065 hull = isl_set_simple_hull(isl_set_copy(set));
3066 if (!hull)
3067 goto error;
3069 nparam = isl_basic_set_dim(hull, isl_dim_param);
3070 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
3071 int lower = 0, upper = 0;
3072 struct isl_basic_set *bounds;
3074 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
3075 for (j = 0; j < hull->n_eq; ++j) {
3076 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3077 continue;
3078 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3079 left) == -1)
3080 break;
3082 if (j < hull->n_eq)
3083 continue;
3085 for (j = 0; j < hull->n_ineq; ++j) {
3086 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3087 continue;
3088 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3089 left) != -1 ||
3090 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3091 i) != -1)
3092 continue;
3093 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3094 lower = 1;
3095 else
3096 upper = 1;
3097 if (lower && upper)
3098 break;
3101 if (lower && upper)
3102 continue;
3104 if (!removed_divs) {
3105 set = isl_set_remove_divs(set);
3106 if (!set)
3107 goto error;
3108 removed_divs = 1;
3110 bounds = set_bounds(set, i);
3111 hull = isl_basic_set_intersect(hull, bounds);
3112 if (!hull)
3113 goto error;
3116 isl_set_free(set);
3117 return hull;
3118 error:
3119 isl_set_free(set);
3120 return NULL;