add isl_aff_scale_down_ui
[isl.git] / isl_convex_hull.c
blobfec85e6b1da3b1e48ec726cd78c28900bea5ae05
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
12 #include <isl/lp.h>
13 #include <isl/map.h>
14 #include <isl_mat_private.h>
15 #include <isl/set.h>
16 #include <isl/seq.h>
17 #include "isl_equalities.h"
18 #include "isl_tab.h"
20 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
22 /* Return 1 if constraint c is redundant with respect to the constraints
23 * in bmap. If c is a lower [upper] bound in some variable and bmap
24 * does not have a lower [upper] bound in that variable, then c cannot
25 * be redundant and we do not need solve any lp.
27 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
28 isl_int *c, isl_int *opt_n, isl_int *opt_d)
30 enum isl_lp_result res;
31 unsigned total;
32 int i, j;
34 if (!bmap)
35 return -1;
37 total = isl_basic_map_total_dim(*bmap);
38 for (i = 0; i < total; ++i) {
39 int sign;
40 if (isl_int_is_zero(c[1+i]))
41 continue;
42 sign = isl_int_sgn(c[1+i]);
43 for (j = 0; j < (*bmap)->n_ineq; ++j)
44 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
45 break;
46 if (j == (*bmap)->n_ineq)
47 break;
49 if (i < total)
50 return 0;
52 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
53 opt_n, opt_d, NULL);
54 if (res == isl_lp_unbounded)
55 return 0;
56 if (res == isl_lp_error)
57 return -1;
58 if (res == isl_lp_empty) {
59 *bmap = isl_basic_map_set_to_empty(*bmap);
60 return 0;
62 return !isl_int_is_neg(*opt_n);
65 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
66 isl_int *c, isl_int *opt_n, isl_int *opt_d)
68 return isl_basic_map_constraint_is_redundant(
69 (struct isl_basic_map **)bset, c, opt_n, opt_d);
72 /* Remove redundant
73 * constraints. If the minimal value along the normal of a constraint
74 * is the same if the constraint is removed, then the constraint is redundant.
76 * Alternatively, we could have intersected the basic map with the
77 * corresponding equality and the checked if the dimension was that
78 * of a facet.
80 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
81 __isl_take isl_basic_map *bmap)
83 struct isl_tab *tab;
85 if (!bmap)
86 return NULL;
88 bmap = isl_basic_map_gauss(bmap, NULL);
89 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
90 return bmap;
91 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
92 return bmap;
93 if (bmap->n_ineq <= 1)
94 return bmap;
96 tab = isl_tab_from_basic_map(bmap);
97 if (isl_tab_detect_implicit_equalities(tab) < 0)
98 goto error;
99 if (isl_tab_detect_redundant(tab) < 0)
100 goto error;
101 bmap = isl_basic_map_update_from_tab(bmap, tab);
102 isl_tab_free(tab);
103 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
104 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
105 return bmap;
106 error:
107 isl_tab_free(tab);
108 isl_basic_map_free(bmap);
109 return NULL;
112 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
113 __isl_take isl_basic_set *bset)
115 return (struct isl_basic_set *)
116 isl_basic_map_remove_redundancies((struct isl_basic_map *)bset);
119 /* Check if the set set is bound in the direction of the affine
120 * constraint c and if so, set the constant term such that the
121 * resulting constraint is a bounding constraint for the set.
123 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
125 int first;
126 int j;
127 isl_int opt;
128 isl_int opt_denom;
130 isl_int_init(opt);
131 isl_int_init(opt_denom);
132 first = 1;
133 for (j = 0; j < set->n; ++j) {
134 enum isl_lp_result res;
136 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
137 continue;
139 res = isl_basic_set_solve_lp(set->p[j],
140 0, c, set->ctx->one, &opt, &opt_denom, NULL);
141 if (res == isl_lp_unbounded)
142 break;
143 if (res == isl_lp_error)
144 goto error;
145 if (res == isl_lp_empty) {
146 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
147 if (!set->p[j])
148 goto error;
149 continue;
151 if (first || isl_int_is_neg(opt)) {
152 if (!isl_int_is_one(opt_denom))
153 isl_seq_scale(c, c, opt_denom, len);
154 isl_int_sub(c[0], c[0], opt);
156 first = 0;
158 isl_int_clear(opt);
159 isl_int_clear(opt_denom);
160 return j >= set->n;
161 error:
162 isl_int_clear(opt);
163 isl_int_clear(opt_denom);
164 return -1;
167 __isl_give isl_basic_map *isl_basic_map_set_rational(
168 __isl_take isl_basic_set *bmap)
170 if (!bmap)
171 return NULL;
173 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
174 return bmap;
176 bmap = isl_basic_map_cow(bmap);
177 if (!bmap)
178 return NULL;
180 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
182 return isl_basic_map_finalize(bmap);
185 __isl_give isl_basic_set *isl_basic_set_set_rational(
186 __isl_take isl_basic_set *bset)
188 return isl_basic_map_set_rational(bset);
191 static struct isl_set *isl_set_set_rational(struct isl_set *set)
193 int i;
195 set = isl_set_cow(set);
196 if (!set)
197 return NULL;
198 for (i = 0; i < set->n; ++i) {
199 set->p[i] = isl_basic_set_set_rational(set->p[i]);
200 if (!set->p[i])
201 goto error;
203 return set;
204 error:
205 isl_set_free(set);
206 return NULL;
209 static struct isl_basic_set *isl_basic_set_add_equality(
210 struct isl_basic_set *bset, isl_int *c)
212 int i;
213 unsigned dim;
215 if (!bset)
216 return NULL;
218 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
219 return bset;
221 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
222 isl_assert(bset->ctx, bset->n_div == 0, goto error);
223 dim = isl_basic_set_n_dim(bset);
224 bset = isl_basic_set_cow(bset);
225 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
226 i = isl_basic_set_alloc_equality(bset);
227 if (i < 0)
228 goto error;
229 isl_seq_cpy(bset->eq[i], c, 1 + dim);
230 return bset;
231 error:
232 isl_basic_set_free(bset);
233 return NULL;
236 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
238 int i;
240 set = isl_set_cow(set);
241 if (!set)
242 return NULL;
243 for (i = 0; i < set->n; ++i) {
244 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
245 if (!set->p[i])
246 goto error;
248 return set;
249 error:
250 isl_set_free(set);
251 return NULL;
254 /* Given a union of basic sets, construct the constraints for wrapping
255 * a facet around one of its ridges.
256 * In particular, if each of n the d-dimensional basic sets i in "set"
257 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
258 * and is defined by the constraints
259 * [ 1 ]
260 * A_i [ x ] >= 0
262 * then the resulting set is of dimension n*(1+d) and has as constraints
264 * [ a_i ]
265 * A_i [ x_i ] >= 0
267 * a_i >= 0
269 * \sum_i x_{i,1} = 1
271 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
273 struct isl_basic_set *lp;
274 unsigned n_eq;
275 unsigned n_ineq;
276 int i, j, k;
277 unsigned dim, lp_dim;
279 if (!set)
280 return NULL;
282 dim = 1 + isl_set_n_dim(set);
283 n_eq = 1;
284 n_ineq = set->n;
285 for (i = 0; i < set->n; ++i) {
286 n_eq += set->p[i]->n_eq;
287 n_ineq += set->p[i]->n_ineq;
289 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
290 lp = isl_basic_set_set_rational(lp);
291 if (!lp)
292 return NULL;
293 lp_dim = isl_basic_set_n_dim(lp);
294 k = isl_basic_set_alloc_equality(lp);
295 isl_int_set_si(lp->eq[k][0], -1);
296 for (i = 0; i < set->n; ++i) {
297 isl_int_set_si(lp->eq[k][1+dim*i], 0);
298 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
299 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
301 for (i = 0; i < set->n; ++i) {
302 k = isl_basic_set_alloc_inequality(lp);
303 isl_seq_clr(lp->ineq[k], 1+lp_dim);
304 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
306 for (j = 0; j < set->p[i]->n_eq; ++j) {
307 k = isl_basic_set_alloc_equality(lp);
308 isl_seq_clr(lp->eq[k], 1+dim*i);
309 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
310 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
313 for (j = 0; j < set->p[i]->n_ineq; ++j) {
314 k = isl_basic_set_alloc_inequality(lp);
315 isl_seq_clr(lp->ineq[k], 1+dim*i);
316 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
317 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
320 return lp;
323 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
324 * of that facet, compute the other facet of the convex hull that contains
325 * the ridge.
327 * We first transform the set such that the facet constraint becomes
329 * x_1 >= 0
331 * I.e., the facet lies in
333 * x_1 = 0
335 * and on that facet, the constraint that defines the ridge is
337 * x_2 >= 0
339 * (This transformation is not strictly needed, all that is needed is
340 * that the ridge contains the origin.)
342 * Since the ridge contains the origin, the cone of the convex hull
343 * will be of the form
345 * x_1 >= 0
346 * x_2 >= a x_1
348 * with this second constraint defining the new facet.
349 * The constant a is obtained by settting x_1 in the cone of the
350 * convex hull to 1 and minimizing x_2.
351 * Now, each element in the cone of the convex hull is the sum
352 * of elements in the cones of the basic sets.
353 * If a_i is the dilation factor of basic set i, then the problem
354 * we need to solve is
356 * min \sum_i x_{i,2}
357 * st
358 * \sum_i x_{i,1} = 1
359 * a_i >= 0
360 * [ a_i ]
361 * A [ x_i ] >= 0
363 * with
364 * [ 1 ]
365 * A_i [ x_i ] >= 0
367 * the constraints of each (transformed) basic set.
368 * If a = n/d, then the constraint defining the new facet (in the transformed
369 * space) is
371 * -n x_1 + d x_2 >= 0
373 * In the original space, we need to take the same combination of the
374 * corresponding constraints "facet" and "ridge".
376 * If a = -infty = "-1/0", then we just return the original facet constraint.
377 * This means that the facet is unbounded, but has a bounded intersection
378 * with the union of sets.
380 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
381 isl_int *facet, isl_int *ridge)
383 int i;
384 isl_ctx *ctx;
385 struct isl_mat *T = NULL;
386 struct isl_basic_set *lp = NULL;
387 struct isl_vec *obj;
388 enum isl_lp_result res;
389 isl_int num, den;
390 unsigned dim;
392 if (!set)
393 return NULL;
394 ctx = set->ctx;
395 set = isl_set_copy(set);
396 set = isl_set_set_rational(set);
398 dim = 1 + isl_set_n_dim(set);
399 T = isl_mat_alloc(ctx, 3, dim);
400 if (!T)
401 goto error;
402 isl_int_set_si(T->row[0][0], 1);
403 isl_seq_clr(T->row[0]+1, dim - 1);
404 isl_seq_cpy(T->row[1], facet, dim);
405 isl_seq_cpy(T->row[2], ridge, dim);
406 T = isl_mat_right_inverse(T);
407 set = isl_set_preimage(set, T);
408 T = NULL;
409 if (!set)
410 goto error;
411 lp = wrap_constraints(set);
412 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
413 if (!obj)
414 goto error;
415 isl_int_set_si(obj->block.data[0], 0);
416 for (i = 0; i < set->n; ++i) {
417 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
418 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
419 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
421 isl_int_init(num);
422 isl_int_init(den);
423 res = isl_basic_set_solve_lp(lp, 0,
424 obj->block.data, ctx->one, &num, &den, NULL);
425 if (res == isl_lp_ok) {
426 isl_int_neg(num, num);
427 isl_seq_combine(facet, num, facet, den, ridge, dim);
428 isl_seq_normalize(ctx, facet, dim);
430 isl_int_clear(num);
431 isl_int_clear(den);
432 isl_vec_free(obj);
433 isl_basic_set_free(lp);
434 isl_set_free(set);
435 if (res == isl_lp_error)
436 return NULL;
437 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
438 return NULL);
439 return facet;
440 error:
441 isl_basic_set_free(lp);
442 isl_mat_free(T);
443 isl_set_free(set);
444 return NULL;
447 /* Compute the constraint of a facet of "set".
449 * We first compute the intersection with a bounding constraint
450 * that is orthogonal to one of the coordinate axes.
451 * If the affine hull of this intersection has only one equality,
452 * we have found a facet.
453 * Otherwise, we wrap the current bounding constraint around
454 * one of the equalities of the face (one that is not equal to
455 * the current bounding constraint).
456 * This process continues until we have found a facet.
457 * The dimension of the intersection increases by at least
458 * one on each iteration, so termination is guaranteed.
460 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
462 struct isl_set *slice = NULL;
463 struct isl_basic_set *face = NULL;
464 int i;
465 unsigned dim = isl_set_n_dim(set);
466 int is_bound;
467 isl_mat *bounds;
469 isl_assert(set->ctx, set->n > 0, goto error);
470 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
471 if (!bounds)
472 return NULL;
474 isl_seq_clr(bounds->row[0], dim);
475 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
476 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
477 if (is_bound < 0)
478 goto error;
479 isl_assert(set->ctx, is_bound, goto error);
480 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
481 bounds->n_row = 1;
483 for (;;) {
484 slice = isl_set_copy(set);
485 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
486 face = isl_set_affine_hull(slice);
487 if (!face)
488 goto error;
489 if (face->n_eq == 1) {
490 isl_basic_set_free(face);
491 break;
493 for (i = 0; i < face->n_eq; ++i)
494 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
495 !isl_seq_is_neg(bounds->row[0],
496 face->eq[i], 1 + dim))
497 break;
498 isl_assert(set->ctx, i < face->n_eq, goto error);
499 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
500 goto error;
501 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
502 isl_basic_set_free(face);
505 return bounds;
506 error:
507 isl_basic_set_free(face);
508 isl_mat_free(bounds);
509 return NULL;
512 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
513 * compute a hyperplane description of the facet, i.e., compute the facets
514 * of the facet.
516 * We compute an affine transformation that transforms the constraint
518 * [ 1 ]
519 * c [ x ] = 0
521 * to the constraint
523 * z_1 = 0
525 * by computing the right inverse U of a matrix that starts with the rows
527 * [ 1 0 ]
528 * [ c ]
530 * Then
531 * [ 1 ] [ 1 ]
532 * [ x ] = U [ z ]
533 * and
534 * [ 1 ] [ 1 ]
535 * [ z ] = Q [ x ]
537 * with Q = U^{-1}
538 * Since z_1 is zero, we can drop this variable as well as the corresponding
539 * column of U to obtain
541 * [ 1 ] [ 1 ]
542 * [ x ] = U' [ z' ]
543 * and
544 * [ 1 ] [ 1 ]
545 * [ z' ] = Q' [ x ]
547 * with Q' equal to Q, but without the corresponding row.
548 * After computing the facets of the facet in the z' space,
549 * we convert them back to the x space through Q.
551 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
553 struct isl_mat *m, *U, *Q;
554 struct isl_basic_set *facet = NULL;
555 struct isl_ctx *ctx;
556 unsigned dim;
558 ctx = set->ctx;
559 set = isl_set_copy(set);
560 dim = isl_set_n_dim(set);
561 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
562 if (!m)
563 goto error;
564 isl_int_set_si(m->row[0][0], 1);
565 isl_seq_clr(m->row[0]+1, dim);
566 isl_seq_cpy(m->row[1], c, 1+dim);
567 U = isl_mat_right_inverse(m);
568 Q = isl_mat_right_inverse(isl_mat_copy(U));
569 U = isl_mat_drop_cols(U, 1, 1);
570 Q = isl_mat_drop_rows(Q, 1, 1);
571 set = isl_set_preimage(set, U);
572 facet = uset_convex_hull_wrap_bounded(set);
573 facet = isl_basic_set_preimage(facet, Q);
574 if (facet)
575 isl_assert(ctx, facet->n_eq == 0, goto error);
576 return facet;
577 error:
578 isl_basic_set_free(facet);
579 isl_set_free(set);
580 return NULL;
583 /* Given an initial facet constraint, compute the remaining facets.
584 * We do this by running through all facets found so far and computing
585 * the adjacent facets through wrapping, adding those facets that we
586 * hadn't already found before.
588 * For each facet we have found so far, we first compute its facets
589 * in the resulting convex hull. That is, we compute the ridges
590 * of the resulting convex hull contained in the facet.
591 * We also compute the corresponding facet in the current approximation
592 * of the convex hull. There is no need to wrap around the ridges
593 * in this facet since that would result in a facet that is already
594 * present in the current approximation.
596 * This function can still be significantly optimized by checking which of
597 * the facets of the basic sets are also facets of the convex hull and
598 * using all the facets so far to help in constructing the facets of the
599 * facets
600 * and/or
601 * using the technique in section "3.1 Ridge Generation" of
602 * "Extended Convex Hull" by Fukuda et al.
604 static struct isl_basic_set *extend(struct isl_basic_set *hull,
605 struct isl_set *set)
607 int i, j, f;
608 int k;
609 struct isl_basic_set *facet = NULL;
610 struct isl_basic_set *hull_facet = NULL;
611 unsigned dim;
613 if (!hull)
614 return NULL;
616 isl_assert(set->ctx, set->n > 0, goto error);
618 dim = isl_set_n_dim(set);
620 for (i = 0; i < hull->n_ineq; ++i) {
621 facet = compute_facet(set, hull->ineq[i]);
622 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
623 facet = isl_basic_set_gauss(facet, NULL);
624 facet = isl_basic_set_normalize_constraints(facet);
625 hull_facet = isl_basic_set_copy(hull);
626 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
627 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
628 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
629 if (!facet || !hull_facet)
630 goto error;
631 hull = isl_basic_set_cow(hull);
632 hull = isl_basic_set_extend_dim(hull,
633 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
634 if (!hull)
635 goto error;
636 for (j = 0; j < facet->n_ineq; ++j) {
637 for (f = 0; f < hull_facet->n_ineq; ++f)
638 if (isl_seq_eq(facet->ineq[j],
639 hull_facet->ineq[f], 1 + dim))
640 break;
641 if (f < hull_facet->n_ineq)
642 continue;
643 k = isl_basic_set_alloc_inequality(hull);
644 if (k < 0)
645 goto error;
646 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
647 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
648 goto error;
650 isl_basic_set_free(hull_facet);
651 isl_basic_set_free(facet);
653 hull = isl_basic_set_simplify(hull);
654 hull = isl_basic_set_finalize(hull);
655 return hull;
656 error:
657 isl_basic_set_free(hull_facet);
658 isl_basic_set_free(facet);
659 isl_basic_set_free(hull);
660 return NULL;
663 /* Special case for computing the convex hull of a one dimensional set.
664 * We simply collect the lower and upper bounds of each basic set
665 * and the biggest of those.
667 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
669 struct isl_mat *c = NULL;
670 isl_int *lower = NULL;
671 isl_int *upper = NULL;
672 int i, j, k;
673 isl_int a, b;
674 struct isl_basic_set *hull;
676 for (i = 0; i < set->n; ++i) {
677 set->p[i] = isl_basic_set_simplify(set->p[i]);
678 if (!set->p[i])
679 goto error;
681 set = isl_set_remove_empty_parts(set);
682 if (!set)
683 goto error;
684 isl_assert(set->ctx, set->n > 0, goto error);
685 c = isl_mat_alloc(set->ctx, 2, 2);
686 if (!c)
687 goto error;
689 if (set->p[0]->n_eq > 0) {
690 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
691 lower = c->row[0];
692 upper = c->row[1];
693 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
694 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
695 isl_seq_neg(upper, set->p[0]->eq[0], 2);
696 } else {
697 isl_seq_neg(lower, set->p[0]->eq[0], 2);
698 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
700 } else {
701 for (j = 0; j < set->p[0]->n_ineq; ++j) {
702 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
703 lower = c->row[0];
704 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
705 } else {
706 upper = c->row[1];
707 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
712 isl_int_init(a);
713 isl_int_init(b);
714 for (i = 0; i < set->n; ++i) {
715 struct isl_basic_set *bset = set->p[i];
716 int has_lower = 0;
717 int has_upper = 0;
719 for (j = 0; j < bset->n_eq; ++j) {
720 has_lower = 1;
721 has_upper = 1;
722 if (lower) {
723 isl_int_mul(a, lower[0], bset->eq[j][1]);
724 isl_int_mul(b, lower[1], bset->eq[j][0]);
725 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
726 isl_seq_cpy(lower, bset->eq[j], 2);
727 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
728 isl_seq_neg(lower, bset->eq[j], 2);
730 if (upper) {
731 isl_int_mul(a, upper[0], bset->eq[j][1]);
732 isl_int_mul(b, upper[1], bset->eq[j][0]);
733 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
734 isl_seq_neg(upper, bset->eq[j], 2);
735 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
736 isl_seq_cpy(upper, bset->eq[j], 2);
739 for (j = 0; j < bset->n_ineq; ++j) {
740 if (isl_int_is_pos(bset->ineq[j][1]))
741 has_lower = 1;
742 if (isl_int_is_neg(bset->ineq[j][1]))
743 has_upper = 1;
744 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
745 isl_int_mul(a, lower[0], bset->ineq[j][1]);
746 isl_int_mul(b, lower[1], bset->ineq[j][0]);
747 if (isl_int_lt(a, b))
748 isl_seq_cpy(lower, bset->ineq[j], 2);
750 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
751 isl_int_mul(a, upper[0], bset->ineq[j][1]);
752 isl_int_mul(b, upper[1], bset->ineq[j][0]);
753 if (isl_int_gt(a, b))
754 isl_seq_cpy(upper, bset->ineq[j], 2);
757 if (!has_lower)
758 lower = NULL;
759 if (!has_upper)
760 upper = NULL;
762 isl_int_clear(a);
763 isl_int_clear(b);
765 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
766 hull = isl_basic_set_set_rational(hull);
767 if (!hull)
768 goto error;
769 if (lower) {
770 k = isl_basic_set_alloc_inequality(hull);
771 isl_seq_cpy(hull->ineq[k], lower, 2);
773 if (upper) {
774 k = isl_basic_set_alloc_inequality(hull);
775 isl_seq_cpy(hull->ineq[k], upper, 2);
777 hull = isl_basic_set_finalize(hull);
778 isl_set_free(set);
779 isl_mat_free(c);
780 return hull;
781 error:
782 isl_set_free(set);
783 isl_mat_free(c);
784 return NULL;
787 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
789 struct isl_basic_set *convex_hull;
791 if (!set)
792 return NULL;
794 if (isl_set_is_empty(set))
795 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
796 else
797 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
798 isl_set_free(set);
799 return convex_hull;
802 /* Compute the convex hull of a pair of basic sets without any parameters or
803 * integer divisions using Fourier-Motzkin elimination.
804 * The convex hull is the set of all points that can be written as
805 * the sum of points from both basic sets (in homogeneous coordinates).
806 * We set up the constraints in a space with dimensions for each of
807 * the three sets and then project out the dimensions corresponding
808 * to the two original basic sets, retaining only those corresponding
809 * to the convex hull.
811 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
812 struct isl_basic_set *bset2)
814 int i, j, k;
815 struct isl_basic_set *bset[2];
816 struct isl_basic_set *hull = NULL;
817 unsigned dim;
819 if (!bset1 || !bset2)
820 goto error;
822 dim = isl_basic_set_n_dim(bset1);
823 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
824 1 + dim + bset1->n_eq + bset2->n_eq,
825 2 + bset1->n_ineq + bset2->n_ineq);
826 bset[0] = bset1;
827 bset[1] = bset2;
828 for (i = 0; i < 2; ++i) {
829 for (j = 0; j < bset[i]->n_eq; ++j) {
830 k = isl_basic_set_alloc_equality(hull);
831 if (k < 0)
832 goto error;
833 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
834 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
835 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
836 1+dim);
838 for (j = 0; j < bset[i]->n_ineq; ++j) {
839 k = isl_basic_set_alloc_inequality(hull);
840 if (k < 0)
841 goto error;
842 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
843 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
844 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
845 bset[i]->ineq[j], 1+dim);
847 k = isl_basic_set_alloc_inequality(hull);
848 if (k < 0)
849 goto error;
850 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
851 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
853 for (j = 0; j < 1+dim; ++j) {
854 k = isl_basic_set_alloc_equality(hull);
855 if (k < 0)
856 goto error;
857 isl_seq_clr(hull->eq[k], 1+2+3*dim);
858 isl_int_set_si(hull->eq[k][j], -1);
859 isl_int_set_si(hull->eq[k][1+dim+j], 1);
860 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
862 hull = isl_basic_set_set_rational(hull);
863 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
864 hull = isl_basic_set_remove_redundancies(hull);
865 isl_basic_set_free(bset1);
866 isl_basic_set_free(bset2);
867 return hull;
868 error:
869 isl_basic_set_free(bset1);
870 isl_basic_set_free(bset2);
871 isl_basic_set_free(hull);
872 return NULL;
875 /* Is the set bounded for each value of the parameters?
877 int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
879 struct isl_tab *tab;
880 int bounded;
882 if (!bset)
883 return -1;
884 if (isl_basic_set_plain_is_empty(bset))
885 return 1;
887 tab = isl_tab_from_recession_cone(bset, 1);
888 bounded = isl_tab_cone_is_bounded(tab);
889 isl_tab_free(tab);
890 return bounded;
893 /* Is the image bounded for each value of the parameters and
894 * the domain variables?
896 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
898 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
899 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
900 int bounded;
902 bmap = isl_basic_map_copy(bmap);
903 bmap = isl_basic_map_cow(bmap);
904 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
905 isl_dim_in, 0, n_in);
906 bounded = isl_basic_set_is_bounded((isl_basic_set *)bmap);
907 isl_basic_map_free(bmap);
909 return bounded;
912 /* Is the set bounded for each value of the parameters?
914 int isl_set_is_bounded(__isl_keep isl_set *set)
916 int i;
918 if (!set)
919 return -1;
921 for (i = 0; i < set->n; ++i) {
922 int bounded = isl_basic_set_is_bounded(set->p[i]);
923 if (!bounded || bounded < 0)
924 return bounded;
926 return 1;
929 /* Compute the lineality space of the convex hull of bset1 and bset2.
931 * We first compute the intersection of the recession cone of bset1
932 * with the negative of the recession cone of bset2 and then compute
933 * the linear hull of the resulting cone.
935 static struct isl_basic_set *induced_lineality_space(
936 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
938 int i, k;
939 struct isl_basic_set *lin = NULL;
940 unsigned dim;
942 if (!bset1 || !bset2)
943 goto error;
945 dim = isl_basic_set_total_dim(bset1);
946 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1), 0,
947 bset1->n_eq + bset2->n_eq,
948 bset1->n_ineq + bset2->n_ineq);
949 lin = isl_basic_set_set_rational(lin);
950 if (!lin)
951 goto error;
952 for (i = 0; i < bset1->n_eq; ++i) {
953 k = isl_basic_set_alloc_equality(lin);
954 if (k < 0)
955 goto error;
956 isl_int_set_si(lin->eq[k][0], 0);
957 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
959 for (i = 0; i < bset1->n_ineq; ++i) {
960 k = isl_basic_set_alloc_inequality(lin);
961 if (k < 0)
962 goto error;
963 isl_int_set_si(lin->ineq[k][0], 0);
964 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
966 for (i = 0; i < bset2->n_eq; ++i) {
967 k = isl_basic_set_alloc_equality(lin);
968 if (k < 0)
969 goto error;
970 isl_int_set_si(lin->eq[k][0], 0);
971 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
973 for (i = 0; i < bset2->n_ineq; ++i) {
974 k = isl_basic_set_alloc_inequality(lin);
975 if (k < 0)
976 goto error;
977 isl_int_set_si(lin->ineq[k][0], 0);
978 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
981 isl_basic_set_free(bset1);
982 isl_basic_set_free(bset2);
983 return isl_basic_set_affine_hull(lin);
984 error:
985 isl_basic_set_free(lin);
986 isl_basic_set_free(bset1);
987 isl_basic_set_free(bset2);
988 return NULL;
991 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
993 /* Given a set and a linear space "lin" of dimension n > 0,
994 * project the linear space from the set, compute the convex hull
995 * and then map the set back to the original space.
997 * Let
999 * M x = 0
1001 * describe the linear space. We first compute the Hermite normal
1002 * form H = M U of M = H Q, to obtain
1004 * H Q x = 0
1006 * The last n rows of H will be zero, so the last n variables of x' = Q x
1007 * are the one we want to project out. We do this by transforming each
1008 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1009 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1010 * we transform the hull back to the original space as A' Q_1 x >= b',
1011 * with Q_1 all but the last n rows of Q.
1013 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1014 struct isl_basic_set *lin)
1016 unsigned total = isl_basic_set_total_dim(lin);
1017 unsigned lin_dim;
1018 struct isl_basic_set *hull;
1019 struct isl_mat *M, *U, *Q;
1021 if (!set || !lin)
1022 goto error;
1023 lin_dim = total - lin->n_eq;
1024 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1025 M = isl_mat_left_hermite(M, 0, &U, &Q);
1026 if (!M)
1027 goto error;
1028 isl_mat_free(M);
1029 isl_basic_set_free(lin);
1031 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1033 U = isl_mat_lin_to_aff(U);
1034 Q = isl_mat_lin_to_aff(Q);
1036 set = isl_set_preimage(set, U);
1037 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1038 hull = uset_convex_hull(set);
1039 hull = isl_basic_set_preimage(hull, Q);
1041 return hull;
1042 error:
1043 isl_basic_set_free(lin);
1044 isl_set_free(set);
1045 return NULL;
1048 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1049 * set up an LP for solving
1051 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1053 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1054 * The next \alpha{ij} correspond to the equalities and come in pairs.
1055 * The final \alpha{ij} correspond to the inequalities.
1057 static struct isl_basic_set *valid_direction_lp(
1058 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1060 struct isl_dim *dim;
1061 struct isl_basic_set *lp;
1062 unsigned d;
1063 int n;
1064 int i, j, k;
1066 if (!bset1 || !bset2)
1067 goto error;
1068 d = 1 + isl_basic_set_total_dim(bset1);
1069 n = 2 +
1070 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1071 dim = isl_dim_set_alloc(bset1->ctx, 0, n);
1072 lp = isl_basic_set_alloc_dim(dim, 0, d, n);
1073 if (!lp)
1074 goto error;
1075 for (i = 0; i < n; ++i) {
1076 k = isl_basic_set_alloc_inequality(lp);
1077 if (k < 0)
1078 goto error;
1079 isl_seq_clr(lp->ineq[k] + 1, n);
1080 isl_int_set_si(lp->ineq[k][0], -1);
1081 isl_int_set_si(lp->ineq[k][1 + i], 1);
1083 for (i = 0; i < d; ++i) {
1084 k = isl_basic_set_alloc_equality(lp);
1085 if (k < 0)
1086 goto error;
1087 n = 0;
1088 isl_int_set_si(lp->eq[k][n], 0); n++;
1089 /* positivity constraint 1 >= 0 */
1090 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1091 for (j = 0; j < bset1->n_eq; ++j) {
1092 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1093 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1095 for (j = 0; j < bset1->n_ineq; ++j) {
1096 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1098 /* positivity constraint 1 >= 0 */
1099 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1100 for (j = 0; j < bset2->n_eq; ++j) {
1101 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1102 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1104 for (j = 0; j < bset2->n_ineq; ++j) {
1105 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1108 lp = isl_basic_set_gauss(lp, NULL);
1109 isl_basic_set_free(bset1);
1110 isl_basic_set_free(bset2);
1111 return lp;
1112 error:
1113 isl_basic_set_free(bset1);
1114 isl_basic_set_free(bset2);
1115 return NULL;
1118 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1119 * for all rays in the homogeneous space of the two cones that correspond
1120 * to the input polyhedra bset1 and bset2.
1122 * We compute s as a vector that satisfies
1124 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1126 * with h_{ij} the normals of the facets of polyhedron i
1127 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1128 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1129 * We first set up an LP with as variables the \alpha{ij}.
1130 * In this formulation, for each polyhedron i,
1131 * the first constraint is the positivity constraint, followed by pairs
1132 * of variables for the equalities, followed by variables for the inequalities.
1133 * We then simply pick a feasible solution and compute s using (*).
1135 * Note that we simply pick any valid direction and make no attempt
1136 * to pick a "good" or even the "best" valid direction.
1138 static struct isl_vec *valid_direction(
1139 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1141 struct isl_basic_set *lp;
1142 struct isl_tab *tab;
1143 struct isl_vec *sample = NULL;
1144 struct isl_vec *dir;
1145 unsigned d;
1146 int i;
1147 int n;
1149 if (!bset1 || !bset2)
1150 goto error;
1151 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1152 isl_basic_set_copy(bset2));
1153 tab = isl_tab_from_basic_set(lp);
1154 sample = isl_tab_get_sample_value(tab);
1155 isl_tab_free(tab);
1156 isl_basic_set_free(lp);
1157 if (!sample)
1158 goto error;
1159 d = isl_basic_set_total_dim(bset1);
1160 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1161 if (!dir)
1162 goto error;
1163 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1164 n = 1;
1165 /* positivity constraint 1 >= 0 */
1166 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1167 for (i = 0; i < bset1->n_eq; ++i) {
1168 isl_int_sub(sample->block.data[n],
1169 sample->block.data[n], sample->block.data[n+1]);
1170 isl_seq_combine(dir->block.data,
1171 bset1->ctx->one, dir->block.data,
1172 sample->block.data[n], bset1->eq[i], 1 + d);
1174 n += 2;
1176 for (i = 0; i < bset1->n_ineq; ++i)
1177 isl_seq_combine(dir->block.data,
1178 bset1->ctx->one, dir->block.data,
1179 sample->block.data[n++], bset1->ineq[i], 1 + d);
1180 isl_vec_free(sample);
1181 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1182 isl_basic_set_free(bset1);
1183 isl_basic_set_free(bset2);
1184 return dir;
1185 error:
1186 isl_vec_free(sample);
1187 isl_basic_set_free(bset1);
1188 isl_basic_set_free(bset2);
1189 return NULL;
1192 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1193 * compute b_i' + A_i' x' >= 0, with
1195 * [ b_i A_i ] [ y' ] [ y' ]
1196 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1198 * In particular, add the "positivity constraint" and then perform
1199 * the mapping.
1201 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1202 struct isl_mat *T)
1204 int k;
1206 if (!bset)
1207 goto error;
1208 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1209 k = isl_basic_set_alloc_inequality(bset);
1210 if (k < 0)
1211 goto error;
1212 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1213 isl_int_set_si(bset->ineq[k][0], 1);
1214 bset = isl_basic_set_preimage(bset, T);
1215 return bset;
1216 error:
1217 isl_mat_free(T);
1218 isl_basic_set_free(bset);
1219 return NULL;
1222 /* Compute the convex hull of a pair of basic sets without any parameters or
1223 * integer divisions, where the convex hull is known to be pointed,
1224 * but the basic sets may be unbounded.
1226 * We turn this problem into the computation of a convex hull of a pair
1227 * _bounded_ polyhedra by "changing the direction of the homogeneous
1228 * dimension". This idea is due to Matthias Koeppe.
1230 * Consider the cones in homogeneous space that correspond to the
1231 * input polyhedra. The rays of these cones are also rays of the
1232 * polyhedra if the coordinate that corresponds to the homogeneous
1233 * dimension is zero. That is, if the inner product of the rays
1234 * with the homogeneous direction is zero.
1235 * The cones in the homogeneous space can also be considered to
1236 * correspond to other pairs of polyhedra by chosing a different
1237 * homogeneous direction. To ensure that both of these polyhedra
1238 * are bounded, we need to make sure that all rays of the cones
1239 * correspond to vertices and not to rays.
1240 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1241 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1242 * The vector s is computed in valid_direction.
1244 * Note that we need to consider _all_ rays of the cones and not just
1245 * the rays that correspond to rays in the polyhedra. If we were to
1246 * only consider those rays and turn them into vertices, then we
1247 * may inadvertently turn some vertices into rays.
1249 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1250 * We therefore transform the two polyhedra such that the selected
1251 * direction is mapped onto this standard direction and then proceed
1252 * with the normal computation.
1253 * Let S be a non-singular square matrix with s as its first row,
1254 * then we want to map the polyhedra to the space
1256 * [ y' ] [ y ] [ y ] [ y' ]
1257 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1259 * We take S to be the unimodular completion of s to limit the growth
1260 * of the coefficients in the following computations.
1262 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1263 * We first move to the homogeneous dimension
1265 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1266 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1268 * Then we change directoin
1270 * [ b_i A_i ] [ y' ] [ y' ]
1271 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1273 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1274 * resulting in b' + A' x' >= 0, which we then convert back
1276 * [ y ] [ y ]
1277 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1279 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1281 static struct isl_basic_set *convex_hull_pair_pointed(
1282 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1284 struct isl_ctx *ctx = NULL;
1285 struct isl_vec *dir = NULL;
1286 struct isl_mat *T = NULL;
1287 struct isl_mat *T2 = NULL;
1288 struct isl_basic_set *hull;
1289 struct isl_set *set;
1291 if (!bset1 || !bset2)
1292 goto error;
1293 ctx = bset1->ctx;
1294 dir = valid_direction(isl_basic_set_copy(bset1),
1295 isl_basic_set_copy(bset2));
1296 if (!dir)
1297 goto error;
1298 T = isl_mat_alloc(bset1->ctx, dir->size, dir->size);
1299 if (!T)
1300 goto error;
1301 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1302 T = isl_mat_unimodular_complete(T, 1);
1303 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1305 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1306 bset2 = homogeneous_map(bset2, T2);
1307 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1308 set = isl_set_add_basic_set(set, bset1);
1309 set = isl_set_add_basic_set(set, bset2);
1310 hull = uset_convex_hull(set);
1311 hull = isl_basic_set_preimage(hull, T);
1313 isl_vec_free(dir);
1315 return hull;
1316 error:
1317 isl_vec_free(dir);
1318 isl_basic_set_free(bset1);
1319 isl_basic_set_free(bset2);
1320 return NULL;
1323 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1324 static struct isl_basic_set *modulo_affine_hull(
1325 struct isl_set *set, struct isl_basic_set *affine_hull);
1327 /* Compute the convex hull of a pair of basic sets without any parameters or
1328 * integer divisions.
1330 * This function is called from uset_convex_hull_unbounded, which
1331 * means that the complete convex hull is unbounded. Some pairs
1332 * of basic sets may still be bounded, though.
1333 * They may even lie inside a lower dimensional space, in which
1334 * case they need to be handled inside their affine hull since
1335 * the main algorithm assumes that the result is full-dimensional.
1337 * If the convex hull of the two basic sets would have a non-trivial
1338 * lineality space, we first project out this lineality space.
1340 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1341 struct isl_basic_set *bset2)
1343 isl_basic_set *lin, *aff;
1344 int bounded1, bounded2;
1346 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1347 return convex_hull_pair_elim(bset1, bset2);
1349 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1350 isl_basic_set_copy(bset2)));
1351 if (!aff)
1352 goto error;
1353 if (aff->n_eq != 0)
1354 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1355 isl_basic_set_free(aff);
1357 bounded1 = isl_basic_set_is_bounded(bset1);
1358 bounded2 = isl_basic_set_is_bounded(bset2);
1360 if (bounded1 < 0 || bounded2 < 0)
1361 goto error;
1363 if (bounded1 && bounded2)
1364 uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1366 if (bounded1 || bounded2)
1367 return convex_hull_pair_pointed(bset1, bset2);
1369 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1370 isl_basic_set_copy(bset2));
1371 if (!lin)
1372 goto error;
1373 if (isl_basic_set_is_universe(lin)) {
1374 isl_basic_set_free(bset1);
1375 isl_basic_set_free(bset2);
1376 return lin;
1378 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1379 struct isl_set *set;
1380 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1381 set = isl_set_add_basic_set(set, bset1);
1382 set = isl_set_add_basic_set(set, bset2);
1383 return modulo_lineality(set, lin);
1385 isl_basic_set_free(lin);
1387 return convex_hull_pair_pointed(bset1, bset2);
1388 error:
1389 isl_basic_set_free(bset1);
1390 isl_basic_set_free(bset2);
1391 return NULL;
1394 /* Compute the lineality space of a basic set.
1395 * We currently do not allow the basic set to have any divs.
1396 * We basically just drop the constants and turn every inequality
1397 * into an equality.
1399 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1401 int i, k;
1402 struct isl_basic_set *lin = NULL;
1403 unsigned dim;
1405 if (!bset)
1406 goto error;
1407 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1408 dim = isl_basic_set_total_dim(bset);
1410 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset), 0, dim, 0);
1411 if (!lin)
1412 goto error;
1413 for (i = 0; i < bset->n_eq; ++i) {
1414 k = isl_basic_set_alloc_equality(lin);
1415 if (k < 0)
1416 goto error;
1417 isl_int_set_si(lin->eq[k][0], 0);
1418 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1420 lin = isl_basic_set_gauss(lin, NULL);
1421 if (!lin)
1422 goto error;
1423 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1424 k = isl_basic_set_alloc_equality(lin);
1425 if (k < 0)
1426 goto error;
1427 isl_int_set_si(lin->eq[k][0], 0);
1428 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1429 lin = isl_basic_set_gauss(lin, NULL);
1430 if (!lin)
1431 goto error;
1433 isl_basic_set_free(bset);
1434 return lin;
1435 error:
1436 isl_basic_set_free(lin);
1437 isl_basic_set_free(bset);
1438 return NULL;
1441 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1442 * "underlying" set "set".
1444 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1446 int i;
1447 struct isl_set *lin = NULL;
1449 if (!set)
1450 return NULL;
1451 if (set->n == 0) {
1452 struct isl_dim *dim = isl_set_get_dim(set);
1453 isl_set_free(set);
1454 return isl_basic_set_empty(dim);
1457 lin = isl_set_alloc_dim(isl_set_get_dim(set), set->n, 0);
1458 for (i = 0; i < set->n; ++i)
1459 lin = isl_set_add_basic_set(lin,
1460 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1461 isl_set_free(set);
1462 return isl_set_affine_hull(lin);
1465 /* Compute the convex hull of a set without any parameters or
1466 * integer divisions.
1467 * In each step, we combined two basic sets until only one
1468 * basic set is left.
1469 * The input basic sets are assumed not to have a non-trivial
1470 * lineality space. If any of the intermediate results has
1471 * a non-trivial lineality space, it is projected out.
1473 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1475 struct isl_basic_set *convex_hull = NULL;
1477 convex_hull = isl_set_copy_basic_set(set);
1478 set = isl_set_drop_basic_set(set, convex_hull);
1479 if (!set)
1480 goto error;
1481 while (set->n > 0) {
1482 struct isl_basic_set *t;
1483 t = isl_set_copy_basic_set(set);
1484 if (!t)
1485 goto error;
1486 set = isl_set_drop_basic_set(set, t);
1487 if (!set)
1488 goto error;
1489 convex_hull = convex_hull_pair(convex_hull, t);
1490 if (set->n == 0)
1491 break;
1492 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1493 if (!t)
1494 goto error;
1495 if (isl_basic_set_is_universe(t)) {
1496 isl_basic_set_free(convex_hull);
1497 convex_hull = t;
1498 break;
1500 if (t->n_eq < isl_basic_set_total_dim(t)) {
1501 set = isl_set_add_basic_set(set, convex_hull);
1502 return modulo_lineality(set, t);
1504 isl_basic_set_free(t);
1506 isl_set_free(set);
1507 return convex_hull;
1508 error:
1509 isl_set_free(set);
1510 isl_basic_set_free(convex_hull);
1511 return NULL;
1514 /* Compute an initial hull for wrapping containing a single initial
1515 * facet.
1516 * This function assumes that the given set is bounded.
1518 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1519 struct isl_set *set)
1521 struct isl_mat *bounds = NULL;
1522 unsigned dim;
1523 int k;
1525 if (!hull)
1526 goto error;
1527 bounds = initial_facet_constraint(set);
1528 if (!bounds)
1529 goto error;
1530 k = isl_basic_set_alloc_inequality(hull);
1531 if (k < 0)
1532 goto error;
1533 dim = isl_set_n_dim(set);
1534 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1535 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1536 isl_mat_free(bounds);
1538 return hull;
1539 error:
1540 isl_basic_set_free(hull);
1541 isl_mat_free(bounds);
1542 return NULL;
1545 struct max_constraint {
1546 struct isl_mat *c;
1547 int count;
1548 int ineq;
1551 static int max_constraint_equal(const void *entry, const void *val)
1553 struct max_constraint *a = (struct max_constraint *)entry;
1554 isl_int *b = (isl_int *)val;
1556 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1559 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1560 isl_int *con, unsigned len, int n, int ineq)
1562 struct isl_hash_table_entry *entry;
1563 struct max_constraint *c;
1564 uint32_t c_hash;
1566 c_hash = isl_seq_get_hash(con + 1, len);
1567 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1568 con + 1, 0);
1569 if (!entry)
1570 return;
1571 c = entry->data;
1572 if (c->count < n) {
1573 isl_hash_table_remove(ctx, table, entry);
1574 return;
1576 c->count++;
1577 if (isl_int_gt(c->c->row[0][0], con[0]))
1578 return;
1579 if (isl_int_eq(c->c->row[0][0], con[0])) {
1580 if (ineq)
1581 c->ineq = ineq;
1582 return;
1584 c->c = isl_mat_cow(c->c);
1585 isl_int_set(c->c->row[0][0], con[0]);
1586 c->ineq = ineq;
1589 /* Check whether the constraint hash table "table" constains the constraint
1590 * "con".
1592 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1593 isl_int *con, unsigned len, int n)
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 0;
1604 c = entry->data;
1605 if (c->count < n)
1606 return 0;
1607 return isl_int_eq(c->c->row[0][0], con[0]);
1610 /* Check for inequality constraints of a basic set without equalities
1611 * such that the same or more stringent copies of the constraint appear
1612 * in all of the basic sets. Such constraints are necessarily facet
1613 * constraints of the convex hull.
1615 * If the resulting basic set is by chance identical to one of
1616 * the basic sets in "set", then we know that this basic set contains
1617 * all other basic sets and is therefore the convex hull of set.
1618 * In this case we set *is_hull to 1.
1620 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1621 struct isl_set *set, int *is_hull)
1623 int i, j, s, n;
1624 int min_constraints;
1625 int best;
1626 struct max_constraint *constraints = NULL;
1627 struct isl_hash_table *table = NULL;
1628 unsigned total;
1630 *is_hull = 0;
1632 for (i = 0; i < set->n; ++i)
1633 if (set->p[i]->n_eq == 0)
1634 break;
1635 if (i >= set->n)
1636 return hull;
1637 min_constraints = set->p[i]->n_ineq;
1638 best = i;
1639 for (i = best + 1; i < set->n; ++i) {
1640 if (set->p[i]->n_eq != 0)
1641 continue;
1642 if (set->p[i]->n_ineq >= min_constraints)
1643 continue;
1644 min_constraints = set->p[i]->n_ineq;
1645 best = i;
1647 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1648 min_constraints);
1649 if (!constraints)
1650 return hull;
1651 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1652 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1653 goto error;
1655 total = isl_dim_total(set->dim);
1656 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1657 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1658 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1659 if (!constraints[i].c)
1660 goto error;
1661 constraints[i].ineq = 1;
1663 for (i = 0; i < min_constraints; ++i) {
1664 struct isl_hash_table_entry *entry;
1665 uint32_t c_hash;
1666 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1667 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1668 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1669 if (!entry)
1670 goto error;
1671 isl_assert(hull->ctx, !entry->data, goto error);
1672 entry->data = &constraints[i];
1675 n = 0;
1676 for (s = 0; s < set->n; ++s) {
1677 if (s == best)
1678 continue;
1680 for (i = 0; i < set->p[s]->n_eq; ++i) {
1681 isl_int *eq = set->p[s]->eq[i];
1682 for (j = 0; j < 2; ++j) {
1683 isl_seq_neg(eq, eq, 1 + total);
1684 update_constraint(hull->ctx, table,
1685 eq, total, n, 0);
1688 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1689 isl_int *ineq = set->p[s]->ineq[i];
1690 update_constraint(hull->ctx, table, ineq, total, n,
1691 set->p[s]->n_eq == 0);
1693 ++n;
1696 for (i = 0; i < min_constraints; ++i) {
1697 if (constraints[i].count < n)
1698 continue;
1699 if (!constraints[i].ineq)
1700 continue;
1701 j = isl_basic_set_alloc_inequality(hull);
1702 if (j < 0)
1703 goto error;
1704 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1707 for (s = 0; s < set->n; ++s) {
1708 if (set->p[s]->n_eq)
1709 continue;
1710 if (set->p[s]->n_ineq != hull->n_ineq)
1711 continue;
1712 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1713 isl_int *ineq = set->p[s]->ineq[i];
1714 if (!has_constraint(hull->ctx, table, ineq, total, n))
1715 break;
1717 if (i == set->p[s]->n_ineq)
1718 *is_hull = 1;
1721 isl_hash_table_clear(table);
1722 for (i = 0; i < min_constraints; ++i)
1723 isl_mat_free(constraints[i].c);
1724 free(constraints);
1725 free(table);
1726 return hull;
1727 error:
1728 isl_hash_table_clear(table);
1729 free(table);
1730 if (constraints)
1731 for (i = 0; i < min_constraints; ++i)
1732 isl_mat_free(constraints[i].c);
1733 free(constraints);
1734 return hull;
1737 /* Create a template for the convex hull of "set" and fill it up
1738 * obvious facet constraints, if any. If the result happens to
1739 * be the convex hull of "set" then *is_hull is set to 1.
1741 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1743 struct isl_basic_set *hull;
1744 unsigned n_ineq;
1745 int i;
1747 n_ineq = 1;
1748 for (i = 0; i < set->n; ++i) {
1749 n_ineq += set->p[i]->n_eq;
1750 n_ineq += set->p[i]->n_ineq;
1752 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1753 hull = isl_basic_set_set_rational(hull);
1754 if (!hull)
1755 return NULL;
1756 return common_constraints(hull, set, is_hull);
1759 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1761 struct isl_basic_set *hull;
1762 int is_hull;
1764 hull = proto_hull(set, &is_hull);
1765 if (hull && !is_hull) {
1766 if (hull->n_ineq == 0)
1767 hull = initial_hull(hull, set);
1768 hull = extend(hull, set);
1770 isl_set_free(set);
1772 return hull;
1775 /* Compute the convex hull of a set without any parameters or
1776 * integer divisions. Depending on whether the set is bounded,
1777 * we pass control to the wrapping based convex hull or
1778 * the Fourier-Motzkin elimination based convex hull.
1779 * We also handle a few special cases before checking the boundedness.
1781 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1783 struct isl_basic_set *convex_hull = NULL;
1784 struct isl_basic_set *lin;
1786 if (isl_set_n_dim(set) == 0)
1787 return convex_hull_0d(set);
1789 set = isl_set_coalesce(set);
1790 set = isl_set_set_rational(set);
1792 if (!set)
1793 goto error;
1794 if (!set)
1795 return NULL;
1796 if (set->n == 1) {
1797 convex_hull = isl_basic_set_copy(set->p[0]);
1798 isl_set_free(set);
1799 return convex_hull;
1801 if (isl_set_n_dim(set) == 1)
1802 return convex_hull_1d(set);
1804 if (isl_set_is_bounded(set) &&
1805 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1806 return uset_convex_hull_wrap(set);
1808 lin = uset_combined_lineality_space(isl_set_copy(set));
1809 if (!lin)
1810 goto error;
1811 if (isl_basic_set_is_universe(lin)) {
1812 isl_set_free(set);
1813 return lin;
1815 if (lin->n_eq < isl_basic_set_total_dim(lin))
1816 return modulo_lineality(set, lin);
1817 isl_basic_set_free(lin);
1819 return uset_convex_hull_unbounded(set);
1820 error:
1821 isl_set_free(set);
1822 isl_basic_set_free(convex_hull);
1823 return NULL;
1826 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1827 * without parameters or divs and where the convex hull of set is
1828 * known to be full-dimensional.
1830 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1832 struct isl_basic_set *convex_hull = NULL;
1834 if (!set)
1835 goto error;
1837 if (isl_set_n_dim(set) == 0) {
1838 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1839 isl_set_free(set);
1840 convex_hull = isl_basic_set_set_rational(convex_hull);
1841 return convex_hull;
1844 set = isl_set_set_rational(set);
1845 set = isl_set_coalesce(set);
1846 if (!set)
1847 goto error;
1848 if (set->n == 1) {
1849 convex_hull = isl_basic_set_copy(set->p[0]);
1850 isl_set_free(set);
1851 return convex_hull;
1853 if (isl_set_n_dim(set) == 1)
1854 return convex_hull_1d(set);
1856 return uset_convex_hull_wrap(set);
1857 error:
1858 isl_set_free(set);
1859 return NULL;
1862 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1863 * We first remove the equalities (transforming the set), compute the
1864 * convex hull of the transformed set and then add the equalities back
1865 * (after performing the inverse transformation.
1867 static struct isl_basic_set *modulo_affine_hull(
1868 struct isl_set *set, struct isl_basic_set *affine_hull)
1870 struct isl_mat *T;
1871 struct isl_mat *T2;
1872 struct isl_basic_set *dummy;
1873 struct isl_basic_set *convex_hull;
1875 dummy = isl_basic_set_remove_equalities(
1876 isl_basic_set_copy(affine_hull), &T, &T2);
1877 if (!dummy)
1878 goto error;
1879 isl_basic_set_free(dummy);
1880 set = isl_set_preimage(set, T);
1881 convex_hull = uset_convex_hull(set);
1882 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1883 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1884 return convex_hull;
1885 error:
1886 isl_basic_set_free(affine_hull);
1887 isl_set_free(set);
1888 return NULL;
1891 /* Compute the convex hull of a map.
1893 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1894 * specifically, the wrapping of facets to obtain new facets.
1896 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1898 struct isl_basic_set *bset;
1899 struct isl_basic_map *model = NULL;
1900 struct isl_basic_set *affine_hull = NULL;
1901 struct isl_basic_map *convex_hull = NULL;
1902 struct isl_set *set = NULL;
1903 struct isl_ctx *ctx;
1905 if (!map)
1906 goto error;
1908 ctx = map->ctx;
1909 if (map->n == 0) {
1910 convex_hull = isl_basic_map_empty_like_map(map);
1911 isl_map_free(map);
1912 return convex_hull;
1915 map = isl_map_detect_equalities(map);
1916 map = isl_map_align_divs(map);
1917 if (!map)
1918 goto error;
1919 model = isl_basic_map_copy(map->p[0]);
1920 set = isl_map_underlying_set(map);
1921 if (!set)
1922 goto error;
1924 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1925 if (!affine_hull)
1926 goto error;
1927 if (affine_hull->n_eq != 0)
1928 bset = modulo_affine_hull(set, affine_hull);
1929 else {
1930 isl_basic_set_free(affine_hull);
1931 bset = uset_convex_hull(set);
1934 convex_hull = isl_basic_map_overlying_set(bset, model);
1935 if (!convex_hull)
1936 return NULL;
1938 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1939 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1940 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1941 return convex_hull;
1942 error:
1943 isl_set_free(set);
1944 isl_basic_map_free(model);
1945 return NULL;
1948 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1950 return (struct isl_basic_set *)
1951 isl_map_convex_hull((struct isl_map *)set);
1954 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1956 isl_basic_map *hull;
1958 hull = isl_map_convex_hull(map);
1959 return isl_basic_map_remove_divs(hull);
1962 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1964 return (isl_basic_set *)isl_map_polyhedral_hull((isl_map *)set);
1967 struct sh_data_entry {
1968 struct isl_hash_table *table;
1969 struct isl_tab *tab;
1972 /* Holds the data needed during the simple hull computation.
1973 * In particular,
1974 * n the number of basic sets in the original set
1975 * hull_table a hash table of already computed constraints
1976 * in the simple hull
1977 * p for each basic set,
1978 * table a hash table of the constraints
1979 * tab the tableau corresponding to the basic set
1981 struct sh_data {
1982 struct isl_ctx *ctx;
1983 unsigned n;
1984 struct isl_hash_table *hull_table;
1985 struct sh_data_entry p[1];
1988 static void sh_data_free(struct sh_data *data)
1990 int i;
1992 if (!data)
1993 return;
1994 isl_hash_table_free(data->ctx, data->hull_table);
1995 for (i = 0; i < data->n; ++i) {
1996 isl_hash_table_free(data->ctx, data->p[i].table);
1997 isl_tab_free(data->p[i].tab);
1999 free(data);
2002 struct ineq_cmp_data {
2003 unsigned len;
2004 isl_int *p;
2007 static int has_ineq(const void *entry, const void *val)
2009 isl_int *row = (isl_int *)entry;
2010 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2012 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2013 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2016 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2017 isl_int *ineq, unsigned len)
2019 uint32_t c_hash;
2020 struct ineq_cmp_data v;
2021 struct isl_hash_table_entry *entry;
2023 v.len = len;
2024 v.p = ineq;
2025 c_hash = isl_seq_get_hash(ineq + 1, len);
2026 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2027 if (!entry)
2028 return - 1;
2029 entry->data = ineq;
2030 return 0;
2033 /* Fill hash table "table" with the constraints of "bset".
2034 * Equalities are added as two inequalities.
2035 * The value in the hash table is a pointer to the (in)equality of "bset".
2037 static int hash_basic_set(struct isl_hash_table *table,
2038 struct isl_basic_set *bset)
2040 int i, j;
2041 unsigned dim = isl_basic_set_total_dim(bset);
2043 for (i = 0; i < bset->n_eq; ++i) {
2044 for (j = 0; j < 2; ++j) {
2045 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2046 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2047 return -1;
2050 for (i = 0; i < bset->n_ineq; ++i) {
2051 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2052 return -1;
2054 return 0;
2057 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2059 struct sh_data *data;
2060 int i;
2062 data = isl_calloc(set->ctx, struct sh_data,
2063 sizeof(struct sh_data) +
2064 (set->n - 1) * sizeof(struct sh_data_entry));
2065 if (!data)
2066 return NULL;
2067 data->ctx = set->ctx;
2068 data->n = set->n;
2069 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2070 if (!data->hull_table)
2071 goto error;
2072 for (i = 0; i < set->n; ++i) {
2073 data->p[i].table = isl_hash_table_alloc(set->ctx,
2074 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2075 if (!data->p[i].table)
2076 goto error;
2077 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2078 goto error;
2080 return data;
2081 error:
2082 sh_data_free(data);
2083 return NULL;
2086 /* Check if inequality "ineq" is a bound for basic set "j" or if
2087 * it can be relaxed (by increasing the constant term) to become
2088 * a bound for that basic set. In the latter case, the constant
2089 * term is updated.
2090 * Return 1 if "ineq" is a bound
2091 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2092 * -1 if some error occurred
2094 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2095 isl_int *ineq)
2097 enum isl_lp_result res;
2098 isl_int opt;
2100 if (!data->p[j].tab) {
2101 data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
2102 if (!data->p[j].tab)
2103 return -1;
2106 isl_int_init(opt);
2108 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2109 &opt, NULL, 0);
2110 if (res == isl_lp_ok && isl_int_is_neg(opt))
2111 isl_int_sub(ineq[0], ineq[0], opt);
2113 isl_int_clear(opt);
2115 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2116 res == isl_lp_unbounded ? 0 : -1;
2119 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2120 * become a bound on the whole set. If so, add the (relaxed) inequality
2121 * to "hull".
2123 * We first check if "hull" already contains a translate of the inequality.
2124 * If so, we are done.
2125 * Then, we check if any of the previous basic sets contains a translate
2126 * of the inequality. If so, then we have already considered this
2127 * inequality and we are done.
2128 * Otherwise, for each basic set other than "i", we check if the inequality
2129 * is a bound on the basic set.
2130 * For previous basic sets, we know that they do not contain a translate
2131 * of the inequality, so we directly call is_bound.
2132 * For following basic sets, we first check if a translate of the
2133 * inequality appears in its description and if so directly update
2134 * the inequality accordingly.
2136 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2137 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
2139 uint32_t c_hash;
2140 struct ineq_cmp_data v;
2141 struct isl_hash_table_entry *entry;
2142 int j, k;
2144 if (!hull)
2145 return NULL;
2147 v.len = isl_basic_set_total_dim(hull);
2148 v.p = ineq;
2149 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2151 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2152 has_ineq, &v, 0);
2153 if (entry)
2154 return hull;
2156 for (j = 0; j < i; ++j) {
2157 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2158 c_hash, has_ineq, &v, 0);
2159 if (entry)
2160 break;
2162 if (j < i)
2163 return hull;
2165 k = isl_basic_set_alloc_inequality(hull);
2166 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2167 if (k < 0)
2168 goto error;
2170 for (j = 0; j < i; ++j) {
2171 int bound;
2172 bound = is_bound(data, set, j, hull->ineq[k]);
2173 if (bound < 0)
2174 goto error;
2175 if (!bound)
2176 break;
2178 if (j < i) {
2179 isl_basic_set_free_inequality(hull, 1);
2180 return hull;
2183 for (j = i + 1; j < set->n; ++j) {
2184 int bound, neg;
2185 isl_int *ineq_j;
2186 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2187 c_hash, has_ineq, &v, 0);
2188 if (entry) {
2189 ineq_j = entry->data;
2190 neg = isl_seq_is_neg(ineq_j + 1,
2191 hull->ineq[k] + 1, v.len);
2192 if (neg)
2193 isl_int_neg(ineq_j[0], ineq_j[0]);
2194 if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
2195 isl_int_set(hull->ineq[k][0], ineq_j[0]);
2196 if (neg)
2197 isl_int_neg(ineq_j[0], ineq_j[0]);
2198 continue;
2200 bound = is_bound(data, set, j, hull->ineq[k]);
2201 if (bound < 0)
2202 goto error;
2203 if (!bound)
2204 break;
2206 if (j < set->n) {
2207 isl_basic_set_free_inequality(hull, 1);
2208 return hull;
2211 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2212 has_ineq, &v, 1);
2213 if (!entry)
2214 goto error;
2215 entry->data = hull->ineq[k];
2217 return hull;
2218 error:
2219 isl_basic_set_free(hull);
2220 return NULL;
2223 /* Check if any inequality from basic set "i" can be relaxed to
2224 * become a bound on the whole set. If so, add the (relaxed) inequality
2225 * to "hull".
2227 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2228 struct sh_data *data, struct isl_set *set, int i)
2230 int j, k;
2231 unsigned dim = isl_basic_set_total_dim(bset);
2233 for (j = 0; j < set->p[i]->n_eq; ++j) {
2234 for (k = 0; k < 2; ++k) {
2235 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2236 bset = add_bound(bset, data, set, i, set->p[i]->eq[j]);
2239 for (j = 0; j < set->p[i]->n_ineq; ++j)
2240 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j]);
2241 return bset;
2244 /* Compute a superset of the convex hull of set that is described
2245 * by only translates of the constraints in the constituents of set.
2247 static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
2249 struct sh_data *data = NULL;
2250 struct isl_basic_set *hull = NULL;
2251 unsigned n_ineq;
2252 int i;
2254 if (!set)
2255 return NULL;
2257 n_ineq = 0;
2258 for (i = 0; i < set->n; ++i) {
2259 if (!set->p[i])
2260 goto error;
2261 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2264 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
2265 if (!hull)
2266 goto error;
2268 data = sh_data_alloc(set, n_ineq);
2269 if (!data)
2270 goto error;
2272 for (i = 0; i < set->n; ++i)
2273 hull = add_bounds(hull, data, set, i);
2275 sh_data_free(data);
2276 isl_set_free(set);
2278 return hull;
2279 error:
2280 sh_data_free(data);
2281 isl_basic_set_free(hull);
2282 isl_set_free(set);
2283 return NULL;
2286 /* Compute a superset of the convex hull of map that is described
2287 * by only translates of the constraints in the constituents of map.
2289 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
2291 struct isl_set *set = NULL;
2292 struct isl_basic_map *model = NULL;
2293 struct isl_basic_map *hull;
2294 struct isl_basic_map *affine_hull;
2295 struct isl_basic_set *bset = NULL;
2297 if (!map)
2298 return NULL;
2299 if (map->n == 0) {
2300 hull = isl_basic_map_empty_like_map(map);
2301 isl_map_free(map);
2302 return hull;
2304 if (map->n == 1) {
2305 hull = isl_basic_map_copy(map->p[0]);
2306 isl_map_free(map);
2307 return hull;
2310 map = isl_map_detect_equalities(map);
2311 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2312 map = isl_map_align_divs(map);
2313 model = isl_basic_map_copy(map->p[0]);
2315 set = isl_map_underlying_set(map);
2317 bset = uset_simple_hull(set);
2319 hull = isl_basic_map_overlying_set(bset, model);
2321 hull = isl_basic_map_intersect(hull, affine_hull);
2322 hull = isl_basic_map_remove_redundancies(hull);
2323 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2324 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2326 return hull;
2329 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2331 return (struct isl_basic_set *)
2332 isl_map_simple_hull((struct isl_map *)set);
2335 /* Given a set "set", return parametric bounds on the dimension "dim".
2337 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2339 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2340 set = isl_set_copy(set);
2341 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2342 set = isl_set_eliminate_dims(set, 0, dim);
2343 return isl_set_convex_hull(set);
2346 /* Computes a "simple hull" and then check if each dimension in the
2347 * resulting hull is bounded by a symbolic constant. If not, the
2348 * hull is intersected with the corresponding bounds on the whole set.
2350 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
2352 int i, j;
2353 struct isl_basic_set *hull;
2354 unsigned nparam, left;
2355 int removed_divs = 0;
2357 hull = isl_set_simple_hull(isl_set_copy(set));
2358 if (!hull)
2359 goto error;
2361 nparam = isl_basic_set_dim(hull, isl_dim_param);
2362 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
2363 int lower = 0, upper = 0;
2364 struct isl_basic_set *bounds;
2366 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
2367 for (j = 0; j < hull->n_eq; ++j) {
2368 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
2369 continue;
2370 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
2371 left) == -1)
2372 break;
2374 if (j < hull->n_eq)
2375 continue;
2377 for (j = 0; j < hull->n_ineq; ++j) {
2378 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
2379 continue;
2380 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
2381 left) != -1 ||
2382 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
2383 i) != -1)
2384 continue;
2385 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
2386 lower = 1;
2387 else
2388 upper = 1;
2389 if (lower && upper)
2390 break;
2393 if (lower && upper)
2394 continue;
2396 if (!removed_divs) {
2397 set = isl_set_remove_divs(set);
2398 if (!set)
2399 goto error;
2400 removed_divs = 1;
2402 bounds = set_bounds(set, i);
2403 hull = isl_basic_set_intersect(hull, bounds);
2404 if (!hull)
2405 goto error;
2408 isl_set_free(set);
2409 return hull;
2410 error:
2411 isl_set_free(set);
2412 return NULL;