isl_map_subtract.c: tab_add_constraints: avoid NULL pointer dereference
[isl.git] / isl_convex_hull.c
blob362c3789d4a547bc7f46ee0f7db5736de5218370
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_lp.h"
11 #include "isl_map.h"
12 #include "isl_map_private.h"
13 #include "isl_mat.h"
14 #include "isl_set.h"
15 #include "isl_seq.h"
16 #include "isl_equalities.h"
17 #include "isl_tab.h"
19 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
21 static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
23 isl_int *t;
25 if (i != j) {
26 t = bmap->ineq[i];
27 bmap->ineq[i] = bmap->ineq[j];
28 bmap->ineq[j] = t;
32 /* Return 1 if constraint c is redundant with respect to the constraints
33 * in bmap. If c is a lower [upper] bound in some variable and bmap
34 * does not have a lower [upper] bound in that variable, then c cannot
35 * be redundant and we do not need solve any lp.
37 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
38 isl_int *c, isl_int *opt_n, isl_int *opt_d)
40 enum isl_lp_result res;
41 unsigned total;
42 int i, j;
44 if (!bmap)
45 return -1;
47 total = isl_basic_map_total_dim(*bmap);
48 for (i = 0; i < total; ++i) {
49 int sign;
50 if (isl_int_is_zero(c[1+i]))
51 continue;
52 sign = isl_int_sgn(c[1+i]);
53 for (j = 0; j < (*bmap)->n_ineq; ++j)
54 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
55 break;
56 if (j == (*bmap)->n_ineq)
57 break;
59 if (i < total)
60 return 0;
62 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
63 opt_n, opt_d, NULL);
64 if (res == isl_lp_unbounded)
65 return 0;
66 if (res == isl_lp_error)
67 return -1;
68 if (res == isl_lp_empty) {
69 *bmap = isl_basic_map_set_to_empty(*bmap);
70 return 0;
72 return !isl_int_is_neg(*opt_n);
75 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
76 isl_int *c, isl_int *opt_n, isl_int *opt_d)
78 return isl_basic_map_constraint_is_redundant(
79 (struct isl_basic_map **)bset, c, opt_n, opt_d);
82 /* Compute the convex hull of a basic map, by removing the redundant
83 * constraints. If the minimal value along the normal of a constraint
84 * is the same if the constraint is removed, then the constraint is redundant.
86 * Alternatively, we could have intersected the basic map with the
87 * corresponding equality and the checked if the dimension was that
88 * of a facet.
90 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
92 struct isl_tab *tab;
94 if (!bmap)
95 return NULL;
97 bmap = isl_basic_map_gauss(bmap, NULL);
98 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
99 return bmap;
100 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
101 return bmap;
102 if (bmap->n_ineq <= 1)
103 return bmap;
105 tab = isl_tab_from_basic_map(bmap);
106 if (isl_tab_detect_implicit_equalities(tab) < 0)
107 goto error;
108 if (isl_tab_detect_redundant(tab) < 0)
109 goto error;
110 bmap = isl_basic_map_update_from_tab(bmap, tab);
111 isl_tab_free(tab);
112 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
113 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
114 return bmap;
115 error:
116 isl_tab_free(tab);
117 isl_basic_map_free(bmap);
118 return NULL;
121 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
123 return (struct isl_basic_set *)
124 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
127 /* Check if the set set is bound in the direction of the affine
128 * constraint c and if so, set the constant term such that the
129 * resulting constraint is a bounding constraint for the set.
131 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
133 int first;
134 int j;
135 isl_int opt;
136 isl_int opt_denom;
138 isl_int_init(opt);
139 isl_int_init(opt_denom);
140 first = 1;
141 for (j = 0; j < set->n; ++j) {
142 enum isl_lp_result res;
144 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
145 continue;
147 res = isl_basic_set_solve_lp(set->p[j],
148 0, c, set->ctx->one, &opt, &opt_denom, NULL);
149 if (res == isl_lp_unbounded)
150 break;
151 if (res == isl_lp_error)
152 goto error;
153 if (res == isl_lp_empty) {
154 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
155 if (!set->p[j])
156 goto error;
157 continue;
159 if (first || isl_int_is_neg(opt)) {
160 if (!isl_int_is_one(opt_denom))
161 isl_seq_scale(c, c, opt_denom, len);
162 isl_int_sub(c[0], c[0], opt);
164 first = 0;
166 isl_int_clear(opt);
167 isl_int_clear(opt_denom);
168 return j >= set->n;
169 error:
170 isl_int_clear(opt);
171 isl_int_clear(opt_denom);
172 return -1;
175 struct isl_basic_set *isl_basic_set_set_rational(struct isl_basic_set *bset)
177 if (!bset)
178 return NULL;
180 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
181 return bset;
183 bset = isl_basic_set_cow(bset);
184 if (!bset)
185 return NULL;
187 ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
189 return isl_basic_set_finalize(bset);
192 static struct isl_set *isl_set_set_rational(struct isl_set *set)
194 int i;
196 set = isl_set_cow(set);
197 if (!set)
198 return NULL;
199 for (i = 0; i < set->n; ++i) {
200 set->p[i] = isl_basic_set_set_rational(set->p[i]);
201 if (!set->p[i])
202 goto error;
204 return set;
205 error:
206 isl_set_free(set);
207 return NULL;
210 static struct isl_basic_set *isl_basic_set_add_equality(
211 struct isl_basic_set *bset, isl_int *c)
213 int i;
214 unsigned dim;
216 if (!bset)
217 return NULL;
219 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
220 return bset;
222 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
223 isl_assert(bset->ctx, bset->n_div == 0, goto error);
224 dim = isl_basic_set_n_dim(bset);
225 bset = isl_basic_set_cow(bset);
226 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
227 i = isl_basic_set_alloc_equality(bset);
228 if (i < 0)
229 goto error;
230 isl_seq_cpy(bset->eq[i], c, 1 + dim);
231 return bset;
232 error:
233 isl_basic_set_free(bset);
234 return NULL;
237 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
239 int i;
241 set = isl_set_cow(set);
242 if (!set)
243 return NULL;
244 for (i = 0; i < set->n; ++i) {
245 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
246 if (!set->p[i])
247 goto error;
249 return set;
250 error:
251 isl_set_free(set);
252 return NULL;
255 /* Given a union of basic sets, construct the constraints for wrapping
256 * a facet around one of its ridges.
257 * In particular, if each of n the d-dimensional basic sets i in "set"
258 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
259 * and is defined by the constraints
260 * [ 1 ]
261 * A_i [ x ] >= 0
263 * then the resulting set is of dimension n*(1+d) and has as constraints
265 * [ a_i ]
266 * A_i [ x_i ] >= 0
268 * a_i >= 0
270 * \sum_i x_{i,1} = 1
272 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
274 struct isl_basic_set *lp;
275 unsigned n_eq;
276 unsigned n_ineq;
277 int i, j, k;
278 unsigned dim, lp_dim;
280 if (!set)
281 return NULL;
283 dim = 1 + isl_set_n_dim(set);
284 n_eq = 1;
285 n_ineq = set->n;
286 for (i = 0; i < set->n; ++i) {
287 n_eq += set->p[i]->n_eq;
288 n_ineq += set->p[i]->n_ineq;
290 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
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 struct isl_mat *T = NULL;
385 struct isl_basic_set *lp = NULL;
386 struct isl_vec *obj;
387 enum isl_lp_result res;
388 isl_int num, den;
389 unsigned dim;
391 set = isl_set_copy(set);
392 set = isl_set_set_rational(set);
394 dim = 1 + isl_set_n_dim(set);
395 T = isl_mat_alloc(set->ctx, 3, dim);
396 if (!T)
397 goto error;
398 isl_int_set_si(T->row[0][0], 1);
399 isl_seq_clr(T->row[0]+1, dim - 1);
400 isl_seq_cpy(T->row[1], facet, dim);
401 isl_seq_cpy(T->row[2], ridge, dim);
402 T = isl_mat_right_inverse(T);
403 set = isl_set_preimage(set, T);
404 T = NULL;
405 if (!set)
406 goto error;
407 lp = wrap_constraints(set);
408 obj = isl_vec_alloc(set->ctx, 1 + dim*set->n);
409 if (!obj)
410 goto error;
411 isl_int_set_si(obj->block.data[0], 0);
412 for (i = 0; i < set->n; ++i) {
413 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
414 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
415 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
417 isl_int_init(num);
418 isl_int_init(den);
419 res = isl_basic_set_solve_lp(lp, 0,
420 obj->block.data, set->ctx->one, &num, &den, NULL);
421 if (res == isl_lp_ok) {
422 isl_int_neg(num, num);
423 isl_seq_combine(facet, num, facet, den, ridge, dim);
425 isl_int_clear(num);
426 isl_int_clear(den);
427 isl_vec_free(obj);
428 isl_basic_set_free(lp);
429 isl_set_free(set);
430 isl_assert(set->ctx, res == isl_lp_ok || res == isl_lp_unbounded,
431 return NULL);
432 return facet;
433 error:
434 isl_basic_set_free(lp);
435 isl_mat_free(T);
436 isl_set_free(set);
437 return NULL;
440 /* Compute the constraint of a facet of "set".
442 * We first compute the intersection with a bounding constraint
443 * that is orthogonal to one of the coordinate axes.
444 * If the affine hull of this intersection has only one equality,
445 * we have found a facet.
446 * Otherwise, we wrap the current bounding constraint around
447 * one of the equalities of the face (one that is not equal to
448 * the current bounding constraint).
449 * This process continues until we have found a facet.
450 * The dimension of the intersection increases by at least
451 * one on each iteration, so termination is guaranteed.
453 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
455 struct isl_set *slice = NULL;
456 struct isl_basic_set *face = NULL;
457 int i;
458 unsigned dim = isl_set_n_dim(set);
459 int is_bound;
460 isl_mat *bounds;
462 isl_assert(set->ctx, set->n > 0, goto error);
463 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
464 if (!bounds)
465 return NULL;
467 isl_seq_clr(bounds->row[0], dim);
468 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
469 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
470 isl_assert(set->ctx, is_bound == 1, goto error);
471 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
472 bounds->n_row = 1;
474 for (;;) {
475 slice = isl_set_copy(set);
476 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
477 face = isl_set_affine_hull(slice);
478 if (!face)
479 goto error;
480 if (face->n_eq == 1) {
481 isl_basic_set_free(face);
482 break;
484 for (i = 0; i < face->n_eq; ++i)
485 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
486 !isl_seq_is_neg(bounds->row[0],
487 face->eq[i], 1 + dim))
488 break;
489 isl_assert(set->ctx, i < face->n_eq, goto error);
490 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
491 goto error;
492 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
493 isl_basic_set_free(face);
496 return bounds;
497 error:
498 isl_basic_set_free(face);
499 isl_mat_free(bounds);
500 return NULL;
503 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
504 * compute a hyperplane description of the facet, i.e., compute the facets
505 * of the facet.
507 * We compute an affine transformation that transforms the constraint
509 * [ 1 ]
510 * c [ x ] = 0
512 * to the constraint
514 * z_1 = 0
516 * by computing the right inverse U of a matrix that starts with the rows
518 * [ 1 0 ]
519 * [ c ]
521 * Then
522 * [ 1 ] [ 1 ]
523 * [ x ] = U [ z ]
524 * and
525 * [ 1 ] [ 1 ]
526 * [ z ] = Q [ x ]
528 * with Q = U^{-1}
529 * Since z_1 is zero, we can drop this variable as well as the corresponding
530 * column of U to obtain
532 * [ 1 ] [ 1 ]
533 * [ x ] = U' [ z' ]
534 * and
535 * [ 1 ] [ 1 ]
536 * [ z' ] = Q' [ x ]
538 * with Q' equal to Q, but without the corresponding row.
539 * After computing the facets of the facet in the z' space,
540 * we convert them back to the x space through Q.
542 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
544 struct isl_mat *m, *U, *Q;
545 struct isl_basic_set *facet = NULL;
546 struct isl_ctx *ctx;
547 unsigned dim;
549 ctx = set->ctx;
550 set = isl_set_copy(set);
551 dim = isl_set_n_dim(set);
552 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
553 if (!m)
554 goto error;
555 isl_int_set_si(m->row[0][0], 1);
556 isl_seq_clr(m->row[0]+1, dim);
557 isl_seq_cpy(m->row[1], c, 1+dim);
558 U = isl_mat_right_inverse(m);
559 Q = isl_mat_right_inverse(isl_mat_copy(U));
560 U = isl_mat_drop_cols(U, 1, 1);
561 Q = isl_mat_drop_rows(Q, 1, 1);
562 set = isl_set_preimage(set, U);
563 facet = uset_convex_hull_wrap_bounded(set);
564 facet = isl_basic_set_preimage(facet, Q);
565 if (facet)
566 isl_assert(ctx, facet->n_eq == 0, goto error);
567 return facet;
568 error:
569 isl_basic_set_free(facet);
570 isl_set_free(set);
571 return NULL;
574 /* Given an initial facet constraint, compute the remaining facets.
575 * We do this by running through all facets found so far and computing
576 * the adjacent facets through wrapping, adding those facets that we
577 * hadn't already found before.
579 * For each facet we have found so far, we first compute its facets
580 * in the resulting convex hull. That is, we compute the ridges
581 * of the resulting convex hull contained in the facet.
582 * We also compute the corresponding facet in the current approximation
583 * of the convex hull. There is no need to wrap around the ridges
584 * in this facet since that would result in a facet that is already
585 * present in the current approximation.
587 * This function can still be significantly optimized by checking which of
588 * the facets of the basic sets are also facets of the convex hull and
589 * using all the facets so far to help in constructing the facets of the
590 * facets
591 * and/or
592 * using the technique in section "3.1 Ridge Generation" of
593 * "Extended Convex Hull" by Fukuda et al.
595 static struct isl_basic_set *extend(struct isl_basic_set *hull,
596 struct isl_set *set)
598 int i, j, f;
599 int k;
600 struct isl_basic_set *facet = NULL;
601 struct isl_basic_set *hull_facet = NULL;
602 unsigned dim;
604 if (!hull)
605 return NULL;
607 isl_assert(set->ctx, set->n > 0, goto error);
609 dim = isl_set_n_dim(set);
611 for (i = 0; i < hull->n_ineq; ++i) {
612 facet = compute_facet(set, hull->ineq[i]);
613 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
614 facet = isl_basic_set_gauss(facet, NULL);
615 facet = isl_basic_set_normalize_constraints(facet);
616 hull_facet = isl_basic_set_copy(hull);
617 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
618 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
619 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
620 if (!facet)
621 goto error;
622 hull = isl_basic_set_cow(hull);
623 hull = isl_basic_set_extend_dim(hull,
624 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
625 for (j = 0; j < facet->n_ineq; ++j) {
626 for (f = 0; f < hull_facet->n_ineq; ++f)
627 if (isl_seq_eq(facet->ineq[j],
628 hull_facet->ineq[f], 1 + dim))
629 break;
630 if (f < hull_facet->n_ineq)
631 continue;
632 k = isl_basic_set_alloc_inequality(hull);
633 if (k < 0)
634 goto error;
635 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
636 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
637 goto error;
639 isl_basic_set_free(hull_facet);
640 isl_basic_set_free(facet);
642 hull = isl_basic_set_simplify(hull);
643 hull = isl_basic_set_finalize(hull);
644 return hull;
645 error:
646 isl_basic_set_free(hull_facet);
647 isl_basic_set_free(facet);
648 isl_basic_set_free(hull);
649 return NULL;
652 /* Special case for computing the convex hull of a one dimensional set.
653 * We simply collect the lower and upper bounds of each basic set
654 * and the biggest of those.
656 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
658 struct isl_mat *c = NULL;
659 isl_int *lower = NULL;
660 isl_int *upper = NULL;
661 int i, j, k;
662 isl_int a, b;
663 struct isl_basic_set *hull;
665 for (i = 0; i < set->n; ++i) {
666 set->p[i] = isl_basic_set_simplify(set->p[i]);
667 if (!set->p[i])
668 goto error;
670 set = isl_set_remove_empty_parts(set);
671 if (!set)
672 goto error;
673 isl_assert(set->ctx, set->n > 0, goto error);
674 c = isl_mat_alloc(set->ctx, 2, 2);
675 if (!c)
676 goto error;
678 if (set->p[0]->n_eq > 0) {
679 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
680 lower = c->row[0];
681 upper = c->row[1];
682 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
683 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
684 isl_seq_neg(upper, set->p[0]->eq[0], 2);
685 } else {
686 isl_seq_neg(lower, set->p[0]->eq[0], 2);
687 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
689 } else {
690 for (j = 0; j < set->p[0]->n_ineq; ++j) {
691 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
692 lower = c->row[0];
693 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
694 } else {
695 upper = c->row[1];
696 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
701 isl_int_init(a);
702 isl_int_init(b);
703 for (i = 0; i < set->n; ++i) {
704 struct isl_basic_set *bset = set->p[i];
705 int has_lower = 0;
706 int has_upper = 0;
708 for (j = 0; j < bset->n_eq; ++j) {
709 has_lower = 1;
710 has_upper = 1;
711 if (lower) {
712 isl_int_mul(a, lower[0], bset->eq[j][1]);
713 isl_int_mul(b, lower[1], bset->eq[j][0]);
714 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
715 isl_seq_cpy(lower, bset->eq[j], 2);
716 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
717 isl_seq_neg(lower, bset->eq[j], 2);
719 if (upper) {
720 isl_int_mul(a, upper[0], bset->eq[j][1]);
721 isl_int_mul(b, upper[1], bset->eq[j][0]);
722 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
723 isl_seq_neg(upper, bset->eq[j], 2);
724 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
725 isl_seq_cpy(upper, bset->eq[j], 2);
728 for (j = 0; j < bset->n_ineq; ++j) {
729 if (isl_int_is_pos(bset->ineq[j][1]))
730 has_lower = 1;
731 if (isl_int_is_neg(bset->ineq[j][1]))
732 has_upper = 1;
733 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
734 isl_int_mul(a, lower[0], bset->ineq[j][1]);
735 isl_int_mul(b, lower[1], bset->ineq[j][0]);
736 if (isl_int_lt(a, b))
737 isl_seq_cpy(lower, bset->ineq[j], 2);
739 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
740 isl_int_mul(a, upper[0], bset->ineq[j][1]);
741 isl_int_mul(b, upper[1], bset->ineq[j][0]);
742 if (isl_int_gt(a, b))
743 isl_seq_cpy(upper, bset->ineq[j], 2);
746 if (!has_lower)
747 lower = NULL;
748 if (!has_upper)
749 upper = NULL;
751 isl_int_clear(a);
752 isl_int_clear(b);
754 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
755 hull = isl_basic_set_set_rational(hull);
756 if (!hull)
757 goto error;
758 if (lower) {
759 k = isl_basic_set_alloc_inequality(hull);
760 isl_seq_cpy(hull->ineq[k], lower, 2);
762 if (upper) {
763 k = isl_basic_set_alloc_inequality(hull);
764 isl_seq_cpy(hull->ineq[k], upper, 2);
766 hull = isl_basic_set_finalize(hull);
767 isl_set_free(set);
768 isl_mat_free(c);
769 return hull;
770 error:
771 isl_set_free(set);
772 isl_mat_free(c);
773 return NULL;
776 /* Project out final n dimensions using Fourier-Motzkin */
777 static struct isl_set *set_project_out(struct isl_ctx *ctx,
778 struct isl_set *set, unsigned n)
780 return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
783 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
785 struct isl_basic_set *convex_hull;
787 if (!set)
788 return NULL;
790 if (isl_set_is_empty(set))
791 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
792 else
793 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
794 isl_set_free(set);
795 return convex_hull;
798 /* Compute the convex hull of a pair of basic sets without any parameters or
799 * integer divisions using Fourier-Motzkin elimination.
800 * The convex hull is the set of all points that can be written as
801 * the sum of points from both basic sets (in homogeneous coordinates).
802 * We set up the constraints in a space with dimensions for each of
803 * the three sets and then project out the dimensions corresponding
804 * to the two original basic sets, retaining only those corresponding
805 * to the convex hull.
807 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
808 struct isl_basic_set *bset2)
810 int i, j, k;
811 struct isl_basic_set *bset[2];
812 struct isl_basic_set *hull = NULL;
813 unsigned dim;
815 if (!bset1 || !bset2)
816 goto error;
818 dim = isl_basic_set_n_dim(bset1);
819 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
820 1 + dim + bset1->n_eq + bset2->n_eq,
821 2 + bset1->n_ineq + bset2->n_ineq);
822 bset[0] = bset1;
823 bset[1] = bset2;
824 for (i = 0; i < 2; ++i) {
825 for (j = 0; j < bset[i]->n_eq; ++j) {
826 k = isl_basic_set_alloc_equality(hull);
827 if (k < 0)
828 goto error;
829 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
830 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
831 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
832 1+dim);
834 for (j = 0; j < bset[i]->n_ineq; ++j) {
835 k = isl_basic_set_alloc_inequality(hull);
836 if (k < 0)
837 goto error;
838 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
839 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
840 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
841 bset[i]->ineq[j], 1+dim);
843 k = isl_basic_set_alloc_inequality(hull);
844 if (k < 0)
845 goto error;
846 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
847 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
849 for (j = 0; j < 1+dim; ++j) {
850 k = isl_basic_set_alloc_equality(hull);
851 if (k < 0)
852 goto error;
853 isl_seq_clr(hull->eq[k], 1+2+3*dim);
854 isl_int_set_si(hull->eq[k][j], -1);
855 isl_int_set_si(hull->eq[k][1+dim+j], 1);
856 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
858 hull = isl_basic_set_set_rational(hull);
859 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
860 hull = isl_basic_set_convex_hull(hull);
861 isl_basic_set_free(bset1);
862 isl_basic_set_free(bset2);
863 return hull;
864 error:
865 isl_basic_set_free(bset1);
866 isl_basic_set_free(bset2);
867 isl_basic_set_free(hull);
868 return NULL;
871 /* Is the set bounded for each value of the parameters?
873 int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
875 struct isl_tab *tab;
876 int bounded;
878 if (!bset)
879 return -1;
880 if (isl_basic_set_fast_is_empty(bset))
881 return 1;
883 tab = isl_tab_from_recession_cone(bset, 1);
884 bounded = isl_tab_cone_is_bounded(tab);
885 isl_tab_free(tab);
886 return bounded;
889 /* Is the set bounded for each value of the parameters?
891 int isl_set_is_bounded(__isl_keep isl_set *set)
893 int i;
895 if (!set)
896 return -1;
898 for (i = 0; i < set->n; ++i) {
899 int bounded = isl_basic_set_is_bounded(set->p[i]);
900 if (!bounded || bounded < 0)
901 return bounded;
903 return 1;
906 /* Compute the lineality space of the convex hull of bset1 and bset2.
908 * We first compute the intersection of the recession cone of bset1
909 * with the negative of the recession cone of bset2 and then compute
910 * the linear hull of the resulting cone.
912 static struct isl_basic_set *induced_lineality_space(
913 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
915 int i, k;
916 struct isl_basic_set *lin = NULL;
917 unsigned dim;
919 if (!bset1 || !bset2)
920 goto error;
922 dim = isl_basic_set_total_dim(bset1);
923 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1), 0,
924 bset1->n_eq + bset2->n_eq,
925 bset1->n_ineq + bset2->n_ineq);
926 lin = isl_basic_set_set_rational(lin);
927 if (!lin)
928 goto error;
929 for (i = 0; i < bset1->n_eq; ++i) {
930 k = isl_basic_set_alloc_equality(lin);
931 if (k < 0)
932 goto error;
933 isl_int_set_si(lin->eq[k][0], 0);
934 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
936 for (i = 0; i < bset1->n_ineq; ++i) {
937 k = isl_basic_set_alloc_inequality(lin);
938 if (k < 0)
939 goto error;
940 isl_int_set_si(lin->ineq[k][0], 0);
941 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
943 for (i = 0; i < bset2->n_eq; ++i) {
944 k = isl_basic_set_alloc_equality(lin);
945 if (k < 0)
946 goto error;
947 isl_int_set_si(lin->eq[k][0], 0);
948 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
950 for (i = 0; i < bset2->n_ineq; ++i) {
951 k = isl_basic_set_alloc_inequality(lin);
952 if (k < 0)
953 goto error;
954 isl_int_set_si(lin->ineq[k][0], 0);
955 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
958 isl_basic_set_free(bset1);
959 isl_basic_set_free(bset2);
960 return isl_basic_set_affine_hull(lin);
961 error:
962 isl_basic_set_free(lin);
963 isl_basic_set_free(bset1);
964 isl_basic_set_free(bset2);
965 return NULL;
968 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
970 /* Given a set and a linear space "lin" of dimension n > 0,
971 * project the linear space from the set, compute the convex hull
972 * and then map the set back to the original space.
974 * Let
976 * M x = 0
978 * describe the linear space. We first compute the Hermite normal
979 * form H = M U of M = H Q, to obtain
981 * H Q x = 0
983 * The last n rows of H will be zero, so the last n variables of x' = Q x
984 * are the one we want to project out. We do this by transforming each
985 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
986 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
987 * we transform the hull back to the original space as A' Q_1 x >= b',
988 * with Q_1 all but the last n rows of Q.
990 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
991 struct isl_basic_set *lin)
993 unsigned total = isl_basic_set_total_dim(lin);
994 unsigned lin_dim;
995 struct isl_basic_set *hull;
996 struct isl_mat *M, *U, *Q;
998 if (!set || !lin)
999 goto error;
1000 lin_dim = total - lin->n_eq;
1001 M = isl_mat_sub_alloc(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1002 M = isl_mat_left_hermite(M, 0, &U, &Q);
1003 if (!M)
1004 goto error;
1005 isl_mat_free(M);
1006 isl_basic_set_free(lin);
1008 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1010 U = isl_mat_lin_to_aff(U);
1011 Q = isl_mat_lin_to_aff(Q);
1013 set = isl_set_preimage(set, U);
1014 set = isl_set_remove_dims(set, total - lin_dim, lin_dim);
1015 hull = uset_convex_hull(set);
1016 hull = isl_basic_set_preimage(hull, Q);
1018 return hull;
1019 error:
1020 isl_basic_set_free(lin);
1021 isl_set_free(set);
1022 return NULL;
1025 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1026 * set up an LP for solving
1028 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1030 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1031 * The next \alpha{ij} correspond to the equalities and come in pairs.
1032 * The final \alpha{ij} correspond to the inequalities.
1034 static struct isl_basic_set *valid_direction_lp(
1035 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1037 struct isl_dim *dim;
1038 struct isl_basic_set *lp;
1039 unsigned d;
1040 int n;
1041 int i, j, k;
1043 if (!bset1 || !bset2)
1044 goto error;
1045 d = 1 + isl_basic_set_total_dim(bset1);
1046 n = 2 +
1047 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1048 dim = isl_dim_set_alloc(bset1->ctx, 0, n);
1049 lp = isl_basic_set_alloc_dim(dim, 0, d, n);
1050 if (!lp)
1051 goto error;
1052 for (i = 0; i < n; ++i) {
1053 k = isl_basic_set_alloc_inequality(lp);
1054 if (k < 0)
1055 goto error;
1056 isl_seq_clr(lp->ineq[k] + 1, n);
1057 isl_int_set_si(lp->ineq[k][0], -1);
1058 isl_int_set_si(lp->ineq[k][1 + i], 1);
1060 for (i = 0; i < d; ++i) {
1061 k = isl_basic_set_alloc_equality(lp);
1062 if (k < 0)
1063 goto error;
1064 n = 0;
1065 isl_int_set_si(lp->eq[k][n++], 0);
1066 /* positivity constraint 1 >= 0 */
1067 isl_int_set_si(lp->eq[k][n++], i == 0);
1068 for (j = 0; j < bset1->n_eq; ++j) {
1069 isl_int_set(lp->eq[k][n++], bset1->eq[j][i]);
1070 isl_int_neg(lp->eq[k][n++], bset1->eq[j][i]);
1072 for (j = 0; j < bset1->n_ineq; ++j)
1073 isl_int_set(lp->eq[k][n++], bset1->ineq[j][i]);
1074 /* positivity constraint 1 >= 0 */
1075 isl_int_set_si(lp->eq[k][n++], -(i == 0));
1076 for (j = 0; j < bset2->n_eq; ++j) {
1077 isl_int_neg(lp->eq[k][n++], bset2->eq[j][i]);
1078 isl_int_set(lp->eq[k][n++], bset2->eq[j][i]);
1080 for (j = 0; j < bset2->n_ineq; ++j)
1081 isl_int_neg(lp->eq[k][n++], bset2->ineq[j][i]);
1083 lp = isl_basic_set_gauss(lp, NULL);
1084 isl_basic_set_free(bset1);
1085 isl_basic_set_free(bset2);
1086 return lp;
1087 error:
1088 isl_basic_set_free(bset1);
1089 isl_basic_set_free(bset2);
1090 return NULL;
1093 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1094 * for all rays in the homogeneous space of the two cones that correspond
1095 * to the input polyhedra bset1 and bset2.
1097 * We compute s as a vector that satisfies
1099 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1101 * with h_{ij} the normals of the facets of polyhedron i
1102 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1103 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1104 * We first set up an LP with as variables the \alpha{ij}.
1105 * In this formulation, for each polyhedron i,
1106 * the first constraint is the positivity constraint, followed by pairs
1107 * of variables for the equalities, followed by variables for the inequalities.
1108 * We then simply pick a feasible solution and compute s using (*).
1110 * Note that we simply pick any valid direction and make no attempt
1111 * to pick a "good" or even the "best" valid direction.
1113 static struct isl_vec *valid_direction(
1114 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1116 struct isl_basic_set *lp;
1117 struct isl_tab *tab;
1118 struct isl_vec *sample = NULL;
1119 struct isl_vec *dir;
1120 unsigned d;
1121 int i;
1122 int n;
1124 if (!bset1 || !bset2)
1125 goto error;
1126 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1127 isl_basic_set_copy(bset2));
1128 tab = isl_tab_from_basic_set(lp);
1129 sample = isl_tab_get_sample_value(tab);
1130 isl_tab_free(tab);
1131 isl_basic_set_free(lp);
1132 if (!sample)
1133 goto error;
1134 d = isl_basic_set_total_dim(bset1);
1135 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1136 if (!dir)
1137 goto error;
1138 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1139 n = 1;
1140 /* positivity constraint 1 >= 0 */
1141 isl_int_set(dir->block.data[0], sample->block.data[n++]);
1142 for (i = 0; i < bset1->n_eq; ++i) {
1143 isl_int_sub(sample->block.data[n],
1144 sample->block.data[n], sample->block.data[n+1]);
1145 isl_seq_combine(dir->block.data,
1146 bset1->ctx->one, dir->block.data,
1147 sample->block.data[n], bset1->eq[i], 1 + d);
1149 n += 2;
1151 for (i = 0; i < bset1->n_ineq; ++i)
1152 isl_seq_combine(dir->block.data,
1153 bset1->ctx->one, dir->block.data,
1154 sample->block.data[n++], bset1->ineq[i], 1 + d);
1155 isl_vec_free(sample);
1156 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1157 isl_basic_set_free(bset1);
1158 isl_basic_set_free(bset2);
1159 return dir;
1160 error:
1161 isl_vec_free(sample);
1162 isl_basic_set_free(bset1);
1163 isl_basic_set_free(bset2);
1164 return NULL;
1167 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1168 * compute b_i' + A_i' x' >= 0, with
1170 * [ b_i A_i ] [ y' ] [ y' ]
1171 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1173 * In particular, add the "positivity constraint" and then perform
1174 * the mapping.
1176 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1177 struct isl_mat *T)
1179 int k;
1181 if (!bset)
1182 goto error;
1183 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1184 k = isl_basic_set_alloc_inequality(bset);
1185 if (k < 0)
1186 goto error;
1187 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1188 isl_int_set_si(bset->ineq[k][0], 1);
1189 bset = isl_basic_set_preimage(bset, T);
1190 return bset;
1191 error:
1192 isl_mat_free(T);
1193 isl_basic_set_free(bset);
1194 return NULL;
1197 /* Compute the convex hull of a pair of basic sets without any parameters or
1198 * integer divisions, where the convex hull is known to be pointed,
1199 * but the basic sets may be unbounded.
1201 * We turn this problem into the computation of a convex hull of a pair
1202 * _bounded_ polyhedra by "changing the direction of the homogeneous
1203 * dimension". This idea is due to Matthias Koeppe.
1205 * Consider the cones in homogeneous space that correspond to the
1206 * input polyhedra. The rays of these cones are also rays of the
1207 * polyhedra if the coordinate that corresponds to the homogeneous
1208 * dimension is zero. That is, if the inner product of the rays
1209 * with the homogeneous direction is zero.
1210 * The cones in the homogeneous space can also be considered to
1211 * correspond to other pairs of polyhedra by chosing a different
1212 * homogeneous direction. To ensure that both of these polyhedra
1213 * are bounded, we need to make sure that all rays of the cones
1214 * correspond to vertices and not to rays.
1215 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1216 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1217 * The vector s is computed in valid_direction.
1219 * Note that we need to consider _all_ rays of the cones and not just
1220 * the rays that correspond to rays in the polyhedra. If we were to
1221 * only consider those rays and turn them into vertices, then we
1222 * may inadvertently turn some vertices into rays.
1224 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1225 * We therefore transform the two polyhedra such that the selected
1226 * direction is mapped onto this standard direction and then proceed
1227 * with the normal computation.
1228 * Let S be a non-singular square matrix with s as its first row,
1229 * then we want to map the polyhedra to the space
1231 * [ y' ] [ y ] [ y ] [ y' ]
1232 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1234 * We take S to be the unimodular completion of s to limit the growth
1235 * of the coefficients in the following computations.
1237 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1238 * We first move to the homogeneous dimension
1240 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1241 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1243 * Then we change directoin
1245 * [ b_i A_i ] [ y' ] [ y' ]
1246 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1248 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1249 * resulting in b' + A' x' >= 0, which we then convert back
1251 * [ y ] [ y ]
1252 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1254 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1256 static struct isl_basic_set *convex_hull_pair_pointed(
1257 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1259 struct isl_ctx *ctx = NULL;
1260 struct isl_vec *dir = NULL;
1261 struct isl_mat *T = NULL;
1262 struct isl_mat *T2 = NULL;
1263 struct isl_basic_set *hull;
1264 struct isl_set *set;
1266 if (!bset1 || !bset2)
1267 goto error;
1268 ctx = bset1->ctx;
1269 dir = valid_direction(isl_basic_set_copy(bset1),
1270 isl_basic_set_copy(bset2));
1271 if (!dir)
1272 goto error;
1273 T = isl_mat_alloc(bset1->ctx, dir->size, dir->size);
1274 if (!T)
1275 goto error;
1276 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1277 T = isl_mat_unimodular_complete(T, 1);
1278 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1280 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1281 bset2 = homogeneous_map(bset2, T2);
1282 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1283 set = isl_set_add_basic_set(set, bset1);
1284 set = isl_set_add_basic_set(set, bset2);
1285 hull = uset_convex_hull(set);
1286 hull = isl_basic_set_preimage(hull, T);
1288 isl_vec_free(dir);
1290 return hull;
1291 error:
1292 isl_vec_free(dir);
1293 isl_basic_set_free(bset1);
1294 isl_basic_set_free(bset2);
1295 return NULL;
1298 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1299 static struct isl_basic_set *modulo_affine_hull(
1300 struct isl_set *set, struct isl_basic_set *affine_hull);
1302 /* Compute the convex hull of a pair of basic sets without any parameters or
1303 * integer divisions.
1305 * This function is called from uset_convex_hull_unbounded, which
1306 * means that the complete convex hull is unbounded. Some pairs
1307 * of basic sets may still be bounded, though.
1308 * They may even lie inside a lower dimensional space, in which
1309 * case they need to be handled inside their affine hull since
1310 * the main algorithm assumes that the result is full-dimensional.
1312 * If the convex hull of the two basic sets would have a non-trivial
1313 * lineality space, we first project out this lineality space.
1315 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1316 struct isl_basic_set *bset2)
1318 isl_basic_set *lin, *aff;
1319 int bounded1, bounded2;
1321 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1322 isl_basic_set_copy(bset2)));
1323 if (!aff)
1324 goto error;
1325 if (aff->n_eq != 0)
1326 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1327 isl_basic_set_free(aff);
1329 bounded1 = isl_basic_set_is_bounded(bset1);
1330 bounded2 = isl_basic_set_is_bounded(bset2);
1332 if (bounded1 < 0 || bounded2 < 0)
1333 goto error;
1335 if (bounded1 && bounded2)
1336 uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1338 if (bounded1 || bounded2)
1339 return convex_hull_pair_pointed(bset1, bset2);
1341 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1342 isl_basic_set_copy(bset2));
1343 if (!lin)
1344 goto error;
1345 if (isl_basic_set_is_universe(lin)) {
1346 isl_basic_set_free(bset1);
1347 isl_basic_set_free(bset2);
1348 return lin;
1350 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1351 struct isl_set *set;
1352 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1353 set = isl_set_add_basic_set(set, bset1);
1354 set = isl_set_add_basic_set(set, bset2);
1355 return modulo_lineality(set, lin);
1357 isl_basic_set_free(lin);
1359 return convex_hull_pair_pointed(bset1, bset2);
1360 error:
1361 isl_basic_set_free(bset1);
1362 isl_basic_set_free(bset2);
1363 return NULL;
1366 /* Compute the lineality space of a basic set.
1367 * We currently do not allow the basic set to have any divs.
1368 * We basically just drop the constants and turn every inequality
1369 * into an equality.
1371 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1373 int i, k;
1374 struct isl_basic_set *lin = NULL;
1375 unsigned dim;
1377 if (!bset)
1378 goto error;
1379 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1380 dim = isl_basic_set_total_dim(bset);
1382 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset), 0, dim, 0);
1383 if (!lin)
1384 goto error;
1385 for (i = 0; i < bset->n_eq; ++i) {
1386 k = isl_basic_set_alloc_equality(lin);
1387 if (k < 0)
1388 goto error;
1389 isl_int_set_si(lin->eq[k][0], 0);
1390 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1392 lin = isl_basic_set_gauss(lin, NULL);
1393 if (!lin)
1394 goto error;
1395 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1396 k = isl_basic_set_alloc_equality(lin);
1397 if (k < 0)
1398 goto error;
1399 isl_int_set_si(lin->eq[k][0], 0);
1400 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1401 lin = isl_basic_set_gauss(lin, NULL);
1402 if (!lin)
1403 goto error;
1405 isl_basic_set_free(bset);
1406 return lin;
1407 error:
1408 isl_basic_set_free(lin);
1409 isl_basic_set_free(bset);
1410 return NULL;
1413 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1414 * "underlying" set "set".
1416 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1418 int i;
1419 struct isl_set *lin = NULL;
1421 if (!set)
1422 return NULL;
1423 if (set->n == 0) {
1424 struct isl_dim *dim = isl_set_get_dim(set);
1425 isl_set_free(set);
1426 return isl_basic_set_empty(dim);
1429 lin = isl_set_alloc_dim(isl_set_get_dim(set), set->n, 0);
1430 for (i = 0; i < set->n; ++i)
1431 lin = isl_set_add_basic_set(lin,
1432 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1433 isl_set_free(set);
1434 return isl_set_affine_hull(lin);
1437 /* Compute the convex hull of a set without any parameters or
1438 * integer divisions.
1439 * In each step, we combined two basic sets until only one
1440 * basic set is left.
1441 * The input basic sets are assumed not to have a non-trivial
1442 * lineality space. If any of the intermediate results has
1443 * a non-trivial lineality space, it is projected out.
1445 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1447 struct isl_basic_set *convex_hull = NULL;
1449 convex_hull = isl_set_copy_basic_set(set);
1450 set = isl_set_drop_basic_set(set, convex_hull);
1451 if (!set)
1452 goto error;
1453 while (set->n > 0) {
1454 struct isl_basic_set *t;
1455 t = isl_set_copy_basic_set(set);
1456 if (!t)
1457 goto error;
1458 set = isl_set_drop_basic_set(set, t);
1459 if (!set)
1460 goto error;
1461 convex_hull = convex_hull_pair(convex_hull, t);
1462 if (set->n == 0)
1463 break;
1464 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1465 if (!t)
1466 goto error;
1467 if (isl_basic_set_is_universe(t)) {
1468 isl_basic_set_free(convex_hull);
1469 convex_hull = t;
1470 break;
1472 if (t->n_eq < isl_basic_set_total_dim(t)) {
1473 set = isl_set_add_basic_set(set, convex_hull);
1474 return modulo_lineality(set, t);
1476 isl_basic_set_free(t);
1478 isl_set_free(set);
1479 return convex_hull;
1480 error:
1481 isl_set_free(set);
1482 isl_basic_set_free(convex_hull);
1483 return NULL;
1486 /* Compute an initial hull for wrapping containing a single initial
1487 * facet.
1488 * This function assumes that the given set is bounded.
1490 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1491 struct isl_set *set)
1493 struct isl_mat *bounds = NULL;
1494 unsigned dim;
1495 int k;
1497 if (!hull)
1498 goto error;
1499 bounds = initial_facet_constraint(set);
1500 if (!bounds)
1501 goto error;
1502 k = isl_basic_set_alloc_inequality(hull);
1503 if (k < 0)
1504 goto error;
1505 dim = isl_set_n_dim(set);
1506 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1507 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1508 isl_mat_free(bounds);
1510 return hull;
1511 error:
1512 isl_basic_set_free(hull);
1513 isl_mat_free(bounds);
1514 return NULL;
1517 struct max_constraint {
1518 struct isl_mat *c;
1519 int count;
1520 int ineq;
1523 static int max_constraint_equal(const void *entry, const void *val)
1525 struct max_constraint *a = (struct max_constraint *)entry;
1526 isl_int *b = (isl_int *)val;
1528 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1531 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1532 isl_int *con, unsigned len, int n, int ineq)
1534 struct isl_hash_table_entry *entry;
1535 struct max_constraint *c;
1536 uint32_t c_hash;
1538 c_hash = isl_seq_get_hash(con + 1, len);
1539 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1540 con + 1, 0);
1541 if (!entry)
1542 return;
1543 c = entry->data;
1544 if (c->count < n) {
1545 isl_hash_table_remove(ctx, table, entry);
1546 return;
1548 c->count++;
1549 if (isl_int_gt(c->c->row[0][0], con[0]))
1550 return;
1551 if (isl_int_eq(c->c->row[0][0], con[0])) {
1552 if (ineq)
1553 c->ineq = ineq;
1554 return;
1556 c->c = isl_mat_cow(c->c);
1557 isl_int_set(c->c->row[0][0], con[0]);
1558 c->ineq = ineq;
1561 /* Check whether the constraint hash table "table" constains the constraint
1562 * "con".
1564 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1565 isl_int *con, unsigned len, int n)
1567 struct isl_hash_table_entry *entry;
1568 struct max_constraint *c;
1569 uint32_t c_hash;
1571 c_hash = isl_seq_get_hash(con + 1, len);
1572 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1573 con + 1, 0);
1574 if (!entry)
1575 return 0;
1576 c = entry->data;
1577 if (c->count < n)
1578 return 0;
1579 return isl_int_eq(c->c->row[0][0], con[0]);
1582 /* Check for inequality constraints of a basic set without equalities
1583 * such that the same or more stringent copies of the constraint appear
1584 * in all of the basic sets. Such constraints are necessarily facet
1585 * constraints of the convex hull.
1587 * If the resulting basic set is by chance identical to one of
1588 * the basic sets in "set", then we know that this basic set contains
1589 * all other basic sets and is therefore the convex hull of set.
1590 * In this case we set *is_hull to 1.
1592 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1593 struct isl_set *set, int *is_hull)
1595 int i, j, s, n;
1596 int min_constraints;
1597 int best;
1598 struct max_constraint *constraints = NULL;
1599 struct isl_hash_table *table = NULL;
1600 unsigned total;
1602 *is_hull = 0;
1604 for (i = 0; i < set->n; ++i)
1605 if (set->p[i]->n_eq == 0)
1606 break;
1607 if (i >= set->n)
1608 return hull;
1609 min_constraints = set->p[i]->n_ineq;
1610 best = i;
1611 for (i = best + 1; i < set->n; ++i) {
1612 if (set->p[i]->n_eq != 0)
1613 continue;
1614 if (set->p[i]->n_ineq >= min_constraints)
1615 continue;
1616 min_constraints = set->p[i]->n_ineq;
1617 best = i;
1619 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1620 min_constraints);
1621 if (!constraints)
1622 return hull;
1623 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1624 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1625 goto error;
1627 total = isl_dim_total(set->dim);
1628 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1629 constraints[i].c = isl_mat_sub_alloc(hull->ctx,
1630 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1631 if (!constraints[i].c)
1632 goto error;
1633 constraints[i].ineq = 1;
1635 for (i = 0; i < min_constraints; ++i) {
1636 struct isl_hash_table_entry *entry;
1637 uint32_t c_hash;
1638 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1639 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1640 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1641 if (!entry)
1642 goto error;
1643 isl_assert(hull->ctx, !entry->data, goto error);
1644 entry->data = &constraints[i];
1647 n = 0;
1648 for (s = 0; s < set->n; ++s) {
1649 if (s == best)
1650 continue;
1652 for (i = 0; i < set->p[s]->n_eq; ++i) {
1653 isl_int *eq = set->p[s]->eq[i];
1654 for (j = 0; j < 2; ++j) {
1655 isl_seq_neg(eq, eq, 1 + total);
1656 update_constraint(hull->ctx, table,
1657 eq, total, n, 0);
1660 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1661 isl_int *ineq = set->p[s]->ineq[i];
1662 update_constraint(hull->ctx, table, ineq, total, n,
1663 set->p[s]->n_eq == 0);
1665 ++n;
1668 for (i = 0; i < min_constraints; ++i) {
1669 if (constraints[i].count < n)
1670 continue;
1671 if (!constraints[i].ineq)
1672 continue;
1673 j = isl_basic_set_alloc_inequality(hull);
1674 if (j < 0)
1675 goto error;
1676 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1679 for (s = 0; s < set->n; ++s) {
1680 if (set->p[s]->n_eq)
1681 continue;
1682 if (set->p[s]->n_ineq != hull->n_ineq)
1683 continue;
1684 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1685 isl_int *ineq = set->p[s]->ineq[i];
1686 if (!has_constraint(hull->ctx, table, ineq, total, n))
1687 break;
1689 if (i == set->p[s]->n_ineq)
1690 *is_hull = 1;
1693 isl_hash_table_clear(table);
1694 for (i = 0; i < min_constraints; ++i)
1695 isl_mat_free(constraints[i].c);
1696 free(constraints);
1697 free(table);
1698 return hull;
1699 error:
1700 isl_hash_table_clear(table);
1701 free(table);
1702 if (constraints)
1703 for (i = 0; i < min_constraints; ++i)
1704 isl_mat_free(constraints[i].c);
1705 free(constraints);
1706 return hull;
1709 /* Create a template for the convex hull of "set" and fill it up
1710 * obvious facet constraints, if any. If the result happens to
1711 * be the convex hull of "set" then *is_hull is set to 1.
1713 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1715 struct isl_basic_set *hull;
1716 unsigned n_ineq;
1717 int i;
1719 n_ineq = 1;
1720 for (i = 0; i < set->n; ++i) {
1721 n_ineq += set->p[i]->n_eq;
1722 n_ineq += set->p[i]->n_ineq;
1724 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1725 hull = isl_basic_set_set_rational(hull);
1726 if (!hull)
1727 return NULL;
1728 return common_constraints(hull, set, is_hull);
1731 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1733 struct isl_basic_set *hull;
1734 int is_hull;
1736 hull = proto_hull(set, &is_hull);
1737 if (hull && !is_hull) {
1738 if (hull->n_ineq == 0)
1739 hull = initial_hull(hull, set);
1740 hull = extend(hull, set);
1742 isl_set_free(set);
1744 return hull;
1747 /* Compute the convex hull of a set without any parameters or
1748 * integer divisions. Depending on whether the set is bounded,
1749 * we pass control to the wrapping based convex hull or
1750 * the Fourier-Motzkin elimination based convex hull.
1751 * We also handle a few special cases before checking the boundedness.
1753 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1755 struct isl_basic_set *convex_hull = NULL;
1756 struct isl_basic_set *lin;
1758 if (isl_set_n_dim(set) == 0)
1759 return convex_hull_0d(set);
1761 set = isl_set_coalesce(set);
1762 set = isl_set_set_rational(set);
1764 if (!set)
1765 goto error;
1766 if (!set)
1767 return NULL;
1768 if (set->n == 1) {
1769 convex_hull = isl_basic_set_copy(set->p[0]);
1770 isl_set_free(set);
1771 return convex_hull;
1773 if (isl_set_n_dim(set) == 1)
1774 return convex_hull_1d(set);
1776 if (isl_set_is_bounded(set))
1777 return uset_convex_hull_wrap(set);
1779 lin = uset_combined_lineality_space(isl_set_copy(set));
1780 if (!lin)
1781 goto error;
1782 if (isl_basic_set_is_universe(lin)) {
1783 isl_set_free(set);
1784 return lin;
1786 if (lin->n_eq < isl_basic_set_total_dim(lin))
1787 return modulo_lineality(set, lin);
1788 isl_basic_set_free(lin);
1790 return uset_convex_hull_unbounded(set);
1791 error:
1792 isl_set_free(set);
1793 isl_basic_set_free(convex_hull);
1794 return NULL;
1797 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1798 * without parameters or divs and where the convex hull of set is
1799 * known to be full-dimensional.
1801 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1803 struct isl_basic_set *convex_hull = NULL;
1805 if (isl_set_n_dim(set) == 0) {
1806 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1807 isl_set_free(set);
1808 convex_hull = isl_basic_set_set_rational(convex_hull);
1809 return convex_hull;
1812 set = isl_set_set_rational(set);
1814 if (!set)
1815 goto error;
1816 set = isl_set_coalesce(set);
1817 if (!set)
1818 goto error;
1819 if (set->n == 1) {
1820 convex_hull = isl_basic_set_copy(set->p[0]);
1821 isl_set_free(set);
1822 return convex_hull;
1824 if (isl_set_n_dim(set) == 1)
1825 return convex_hull_1d(set);
1827 return uset_convex_hull_wrap(set);
1828 error:
1829 isl_set_free(set);
1830 return NULL;
1833 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1834 * We first remove the equalities (transforming the set), compute the
1835 * convex hull of the transformed set and then add the equalities back
1836 * (after performing the inverse transformation.
1838 static struct isl_basic_set *modulo_affine_hull(
1839 struct isl_set *set, struct isl_basic_set *affine_hull)
1841 struct isl_mat *T;
1842 struct isl_mat *T2;
1843 struct isl_basic_set *dummy;
1844 struct isl_basic_set *convex_hull;
1846 dummy = isl_basic_set_remove_equalities(
1847 isl_basic_set_copy(affine_hull), &T, &T2);
1848 if (!dummy)
1849 goto error;
1850 isl_basic_set_free(dummy);
1851 set = isl_set_preimage(set, T);
1852 convex_hull = uset_convex_hull(set);
1853 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1854 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1855 return convex_hull;
1856 error:
1857 isl_basic_set_free(affine_hull);
1858 isl_set_free(set);
1859 return NULL;
1862 /* Compute the convex hull of a map.
1864 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1865 * specifically, the wrapping of facets to obtain new facets.
1867 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1869 struct isl_basic_set *bset;
1870 struct isl_basic_map *model = NULL;
1871 struct isl_basic_set *affine_hull = NULL;
1872 struct isl_basic_map *convex_hull = NULL;
1873 struct isl_set *set = NULL;
1874 struct isl_ctx *ctx;
1876 if (!map)
1877 goto error;
1879 ctx = map->ctx;
1880 if (map->n == 0) {
1881 convex_hull = isl_basic_map_empty_like_map(map);
1882 isl_map_free(map);
1883 return convex_hull;
1886 map = isl_map_detect_equalities(map);
1887 map = isl_map_align_divs(map);
1888 model = isl_basic_map_copy(map->p[0]);
1889 set = isl_map_underlying_set(map);
1890 if (!set)
1891 goto error;
1893 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1894 if (!affine_hull)
1895 goto error;
1896 if (affine_hull->n_eq != 0)
1897 bset = modulo_affine_hull(set, affine_hull);
1898 else {
1899 isl_basic_set_free(affine_hull);
1900 bset = uset_convex_hull(set);
1903 convex_hull = isl_basic_map_overlying_set(bset, model);
1904 if (!convex_hull)
1905 return NULL;
1907 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1908 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1909 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1910 return convex_hull;
1911 error:
1912 isl_set_free(set);
1913 isl_basic_map_free(model);
1914 return NULL;
1917 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1919 return (struct isl_basic_set *)
1920 isl_map_convex_hull((struct isl_map *)set);
1923 struct sh_data_entry {
1924 struct isl_hash_table *table;
1925 struct isl_tab *tab;
1928 /* Holds the data needed during the simple hull computation.
1929 * In particular,
1930 * n the number of basic sets in the original set
1931 * hull_table a hash table of already computed constraints
1932 * in the simple hull
1933 * p for each basic set,
1934 * table a hash table of the constraints
1935 * tab the tableau corresponding to the basic set
1937 struct sh_data {
1938 struct isl_ctx *ctx;
1939 unsigned n;
1940 struct isl_hash_table *hull_table;
1941 struct sh_data_entry p[1];
1944 static void sh_data_free(struct sh_data *data)
1946 int i;
1948 if (!data)
1949 return;
1950 isl_hash_table_free(data->ctx, data->hull_table);
1951 for (i = 0; i < data->n; ++i) {
1952 isl_hash_table_free(data->ctx, data->p[i].table);
1953 isl_tab_free(data->p[i].tab);
1955 free(data);
1958 struct ineq_cmp_data {
1959 unsigned len;
1960 isl_int *p;
1963 static int has_ineq(const void *entry, const void *val)
1965 isl_int *row = (isl_int *)entry;
1966 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
1968 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
1969 isl_seq_is_neg(row + 1, v->p + 1, v->len);
1972 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
1973 isl_int *ineq, unsigned len)
1975 uint32_t c_hash;
1976 struct ineq_cmp_data v;
1977 struct isl_hash_table_entry *entry;
1979 v.len = len;
1980 v.p = ineq;
1981 c_hash = isl_seq_get_hash(ineq + 1, len);
1982 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
1983 if (!entry)
1984 return - 1;
1985 entry->data = ineq;
1986 return 0;
1989 /* Fill hash table "table" with the constraints of "bset".
1990 * Equalities are added as two inequalities.
1991 * The value in the hash table is a pointer to the (in)equality of "bset".
1993 static int hash_basic_set(struct isl_hash_table *table,
1994 struct isl_basic_set *bset)
1996 int i, j;
1997 unsigned dim = isl_basic_set_total_dim(bset);
1999 for (i = 0; i < bset->n_eq; ++i) {
2000 for (j = 0; j < 2; ++j) {
2001 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2002 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2003 return -1;
2006 for (i = 0; i < bset->n_ineq; ++i) {
2007 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2008 return -1;
2010 return 0;
2013 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2015 struct sh_data *data;
2016 int i;
2018 data = isl_calloc(set->ctx, struct sh_data,
2019 sizeof(struct sh_data) +
2020 (set->n - 1) * sizeof(struct sh_data_entry));
2021 if (!data)
2022 return NULL;
2023 data->ctx = set->ctx;
2024 data->n = set->n;
2025 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2026 if (!data->hull_table)
2027 goto error;
2028 for (i = 0; i < set->n; ++i) {
2029 data->p[i].table = isl_hash_table_alloc(set->ctx,
2030 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2031 if (!data->p[i].table)
2032 goto error;
2033 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2034 goto error;
2036 return data;
2037 error:
2038 sh_data_free(data);
2039 return NULL;
2042 /* Check if inequality "ineq" is a bound for basic set "j" or if
2043 * it can be relaxed (by increasing the constant term) to become
2044 * a bound for that basic set. In the latter case, the constant
2045 * term is updated.
2046 * Return 1 if "ineq" is a bound
2047 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2048 * -1 if some error occurred
2050 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2051 isl_int *ineq)
2053 enum isl_lp_result res;
2054 isl_int opt;
2056 if (!data->p[j].tab) {
2057 data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
2058 if (!data->p[j].tab)
2059 return -1;
2062 isl_int_init(opt);
2064 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2065 &opt, NULL, 0);
2066 if (res == isl_lp_ok && isl_int_is_neg(opt))
2067 isl_int_sub(ineq[0], ineq[0], opt);
2069 isl_int_clear(opt);
2071 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2072 res == isl_lp_unbounded ? 0 : -1;
2075 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2076 * become a bound on the whole set. If so, add the (relaxed) inequality
2077 * to "hull".
2079 * We first check if "hull" already contains a translate of the inequality.
2080 * If so, we are done.
2081 * Then, we check if any of the previous basic sets contains a translate
2082 * of the inequality. If so, then we have already considered this
2083 * inequality and we are done.
2084 * Otherwise, for each basic set other than "i", we check if the inequality
2085 * is a bound on the basic set.
2086 * For previous basic sets, we know that they do not contain a translate
2087 * of the inequality, so we directly call is_bound.
2088 * For following basic sets, we first check if a translate of the
2089 * inequality appears in its description and if so directly update
2090 * the inequality accordingly.
2092 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2093 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
2095 uint32_t c_hash;
2096 struct ineq_cmp_data v;
2097 struct isl_hash_table_entry *entry;
2098 int j, k;
2100 if (!hull)
2101 return NULL;
2103 v.len = isl_basic_set_total_dim(hull);
2104 v.p = ineq;
2105 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2107 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2108 has_ineq, &v, 0);
2109 if (entry)
2110 return hull;
2112 for (j = 0; j < i; ++j) {
2113 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2114 c_hash, has_ineq, &v, 0);
2115 if (entry)
2116 break;
2118 if (j < i)
2119 return hull;
2121 k = isl_basic_set_alloc_inequality(hull);
2122 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2123 if (k < 0)
2124 goto error;
2126 for (j = 0; j < i; ++j) {
2127 int bound;
2128 bound = is_bound(data, set, j, hull->ineq[k]);
2129 if (bound < 0)
2130 goto error;
2131 if (!bound)
2132 break;
2134 if (j < i) {
2135 isl_basic_set_free_inequality(hull, 1);
2136 return hull;
2139 for (j = i + 1; j < set->n; ++j) {
2140 int bound, neg;
2141 isl_int *ineq_j;
2142 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2143 c_hash, has_ineq, &v, 0);
2144 if (entry) {
2145 ineq_j = entry->data;
2146 neg = isl_seq_is_neg(ineq_j + 1,
2147 hull->ineq[k] + 1, v.len);
2148 if (neg)
2149 isl_int_neg(ineq_j[0], ineq_j[0]);
2150 if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
2151 isl_int_set(hull->ineq[k][0], ineq_j[0]);
2152 if (neg)
2153 isl_int_neg(ineq_j[0], ineq_j[0]);
2154 continue;
2156 bound = is_bound(data, set, j, hull->ineq[k]);
2157 if (bound < 0)
2158 goto error;
2159 if (!bound)
2160 break;
2162 if (j < set->n) {
2163 isl_basic_set_free_inequality(hull, 1);
2164 return hull;
2167 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2168 has_ineq, &v, 1);
2169 if (!entry)
2170 goto error;
2171 entry->data = hull->ineq[k];
2173 return hull;
2174 error:
2175 isl_basic_set_free(hull);
2176 return NULL;
2179 /* Check if any inequality from basic set "i" can be relaxed to
2180 * become a bound on the whole set. If so, add the (relaxed) inequality
2181 * to "hull".
2183 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2184 struct sh_data *data, struct isl_set *set, int i)
2186 int j, k;
2187 unsigned dim = isl_basic_set_total_dim(bset);
2189 for (j = 0; j < set->p[i]->n_eq; ++j) {
2190 for (k = 0; k < 2; ++k) {
2191 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2192 bset = add_bound(bset, data, set, i, set->p[i]->eq[j]);
2195 for (j = 0; j < set->p[i]->n_ineq; ++j)
2196 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j]);
2197 return bset;
2200 /* Compute a superset of the convex hull of set that is described
2201 * by only translates of the constraints in the constituents of set.
2203 static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
2205 struct sh_data *data = NULL;
2206 struct isl_basic_set *hull = NULL;
2207 unsigned n_ineq;
2208 int i;
2210 if (!set)
2211 return NULL;
2213 n_ineq = 0;
2214 for (i = 0; i < set->n; ++i) {
2215 if (!set->p[i])
2216 goto error;
2217 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2220 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
2221 if (!hull)
2222 goto error;
2224 data = sh_data_alloc(set, n_ineq);
2225 if (!data)
2226 goto error;
2228 for (i = 0; i < set->n; ++i)
2229 hull = add_bounds(hull, data, set, i);
2231 sh_data_free(data);
2232 isl_set_free(set);
2234 return hull;
2235 error:
2236 sh_data_free(data);
2237 isl_basic_set_free(hull);
2238 isl_set_free(set);
2239 return NULL;
2242 /* Compute a superset of the convex hull of map that is described
2243 * by only translates of the constraints in the constituents of map.
2245 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
2247 struct isl_set *set = NULL;
2248 struct isl_basic_map *model = NULL;
2249 struct isl_basic_map *hull;
2250 struct isl_basic_map *affine_hull;
2251 struct isl_basic_set *bset = NULL;
2253 if (!map)
2254 return NULL;
2255 if (map->n == 0) {
2256 hull = isl_basic_map_empty_like_map(map);
2257 isl_map_free(map);
2258 return hull;
2260 if (map->n == 1) {
2261 hull = isl_basic_map_copy(map->p[0]);
2262 isl_map_free(map);
2263 return hull;
2266 map = isl_map_detect_equalities(map);
2267 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2268 map = isl_map_align_divs(map);
2269 model = isl_basic_map_copy(map->p[0]);
2271 set = isl_map_underlying_set(map);
2273 bset = uset_simple_hull(set);
2275 hull = isl_basic_map_overlying_set(bset, model);
2277 hull = isl_basic_map_intersect(hull, affine_hull);
2278 hull = isl_basic_map_convex_hull(hull);
2279 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2280 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2282 return hull;
2285 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2287 return (struct isl_basic_set *)
2288 isl_map_simple_hull((struct isl_map *)set);
2291 /* Given a set "set", return parametric bounds on the dimension "dim".
2293 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2295 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2296 set = isl_set_copy(set);
2297 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2298 set = isl_set_eliminate_dims(set, 0, dim);
2299 return isl_set_convex_hull(set);
2302 /* Computes a "simple hull" and then check if each dimension in the
2303 * resulting hull is bounded by a symbolic constant. If not, the
2304 * hull is intersected with the corresponding bounds on the whole set.
2306 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
2308 int i, j;
2309 struct isl_basic_set *hull;
2310 unsigned nparam, left;
2311 int removed_divs = 0;
2313 hull = isl_set_simple_hull(isl_set_copy(set));
2314 if (!hull)
2315 goto error;
2317 nparam = isl_basic_set_dim(hull, isl_dim_param);
2318 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
2319 int lower = 0, upper = 0;
2320 struct isl_basic_set *bounds;
2322 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
2323 for (j = 0; j < hull->n_eq; ++j) {
2324 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
2325 continue;
2326 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
2327 left) == -1)
2328 break;
2330 if (j < hull->n_eq)
2331 continue;
2333 for (j = 0; j < hull->n_ineq; ++j) {
2334 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
2335 continue;
2336 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
2337 left) != -1 ||
2338 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
2339 i) != -1)
2340 continue;
2341 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
2342 lower = 1;
2343 else
2344 upper = 1;
2345 if (lower && upper)
2346 break;
2349 if (lower && upper)
2350 continue;
2352 if (!removed_divs) {
2353 set = isl_set_remove_divs(set);
2354 if (!set)
2355 goto error;
2356 removed_divs = 1;
2358 bounds = set_bounds(set, i);
2359 hull = isl_basic_set_intersect(hull, bounds);
2360 if (!hull)
2361 goto error;
2364 isl_set_free(set);
2365 return hull;
2366 error:
2367 isl_set_free(set);
2368 return NULL;