isl_polynomial.c: reduce_divs: do not assume input is a monomial
[isl.git] / isl_convex_hull.c
blobc81de322e54eabcffc7782553df0bdc730727c41
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_lp_private.h>
16 #include <isl/map.h>
17 #include <isl_mat_private.h>
18 #include <isl_vec_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_options_private.h>
22 #include "isl_equalities.h"
23 #include "isl_tab.h"
24 #include <isl_sort.h>
26 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
28 /* Return 1 if constraint c is redundant with respect to the constraints
29 * in bmap. If c is a lower [upper] bound in some variable and bmap
30 * does not have a lower [upper] bound in that variable, then c cannot
31 * be redundant and we do not need solve any lp.
33 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
34 isl_int *c, isl_int *opt_n, isl_int *opt_d)
36 enum isl_lp_result res;
37 unsigned total;
38 int i, j;
40 if (!bmap)
41 return -1;
43 total = isl_basic_map_total_dim(*bmap);
44 for (i = 0; i < total; ++i) {
45 int sign;
46 if (isl_int_is_zero(c[1+i]))
47 continue;
48 sign = isl_int_sgn(c[1+i]);
49 for (j = 0; j < (*bmap)->n_ineq; ++j)
50 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
51 break;
52 if (j == (*bmap)->n_ineq)
53 break;
55 if (i < total)
56 return 0;
58 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
59 opt_n, opt_d, NULL);
60 if (res == isl_lp_unbounded)
61 return 0;
62 if (res == isl_lp_error)
63 return -1;
64 if (res == isl_lp_empty) {
65 *bmap = isl_basic_map_set_to_empty(*bmap);
66 return 0;
68 return !isl_int_is_neg(*opt_n);
71 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
72 isl_int *c, isl_int *opt_n, isl_int *opt_d)
74 return isl_basic_map_constraint_is_redundant(
75 (struct isl_basic_map **)bset, c, opt_n, opt_d);
78 /* Remove redundant
79 * constraints. If the minimal value along the normal of a constraint
80 * is the same if the constraint is removed, then the constraint is redundant.
82 * Alternatively, we could have intersected the basic map with the
83 * corresponding equality and the checked if the dimension was that
84 * of a facet.
86 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
87 __isl_take isl_basic_map *bmap)
89 struct isl_tab *tab;
91 if (!bmap)
92 return NULL;
94 bmap = isl_basic_map_gauss(bmap, NULL);
95 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
96 return bmap;
97 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
98 return bmap;
99 if (bmap->n_ineq <= 1)
100 return bmap;
102 tab = isl_tab_from_basic_map(bmap, 0);
103 if (isl_tab_detect_implicit_equalities(tab) < 0)
104 goto error;
105 if (isl_tab_detect_redundant(tab) < 0)
106 goto error;
107 bmap = isl_basic_map_update_from_tab(bmap, tab);
108 isl_tab_free(tab);
109 if (!bmap)
110 return NULL;
111 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
112 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
113 return bmap;
114 error:
115 isl_tab_free(tab);
116 isl_basic_map_free(bmap);
117 return NULL;
120 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
121 __isl_take isl_basic_set *bset)
123 return (struct isl_basic_set *)
124 isl_basic_map_remove_redundancies((struct isl_basic_map *)bset);
127 /* Remove redundant constraints in each of the basic maps.
129 __isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
131 return isl_map_inline_foreach_basic_map(map,
132 &isl_basic_map_remove_redundancies);
135 __isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
137 return isl_map_remove_redundancies(set);
140 /* Check if the set set is bound in the direction of the affine
141 * constraint c and if so, set the constant term such that the
142 * resulting constraint is a bounding constraint for the set.
144 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
146 int first;
147 int j;
148 isl_int opt;
149 isl_int opt_denom;
151 isl_int_init(opt);
152 isl_int_init(opt_denom);
153 first = 1;
154 for (j = 0; j < set->n; ++j) {
155 enum isl_lp_result res;
157 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
158 continue;
160 res = isl_basic_set_solve_lp(set->p[j],
161 0, c, set->ctx->one, &opt, &opt_denom, NULL);
162 if (res == isl_lp_unbounded)
163 break;
164 if (res == isl_lp_error)
165 goto error;
166 if (res == isl_lp_empty) {
167 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
168 if (!set->p[j])
169 goto error;
170 continue;
172 if (first || isl_int_is_neg(opt)) {
173 if (!isl_int_is_one(opt_denom))
174 isl_seq_scale(c, c, opt_denom, len);
175 isl_int_sub(c[0], c[0], opt);
177 first = 0;
179 isl_int_clear(opt);
180 isl_int_clear(opt_denom);
181 return j >= set->n;
182 error:
183 isl_int_clear(opt);
184 isl_int_clear(opt_denom);
185 return -1;
188 __isl_give isl_basic_map *isl_basic_map_set_rational(
189 __isl_take isl_basic_set *bmap)
191 if (!bmap)
192 return NULL;
194 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
195 return bmap;
197 bmap = isl_basic_map_cow(bmap);
198 if (!bmap)
199 return NULL;
201 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
203 return isl_basic_map_finalize(bmap);
206 __isl_give isl_basic_set *isl_basic_set_set_rational(
207 __isl_take isl_basic_set *bset)
209 return isl_basic_map_set_rational(bset);
212 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
214 int i;
216 map = isl_map_cow(map);
217 if (!map)
218 return NULL;
219 for (i = 0; i < map->n; ++i) {
220 map->p[i] = isl_basic_map_set_rational(map->p[i]);
221 if (!map->p[i])
222 goto error;
224 return map;
225 error:
226 isl_map_free(map);
227 return NULL;
230 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
232 return isl_map_set_rational(set);
235 static struct isl_basic_set *isl_basic_set_add_equality(
236 struct isl_basic_set *bset, isl_int *c)
238 int i;
239 unsigned dim;
241 if (!bset)
242 return NULL;
244 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
245 return bset;
247 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
248 isl_assert(bset->ctx, bset->n_div == 0, goto error);
249 dim = isl_basic_set_n_dim(bset);
250 bset = isl_basic_set_cow(bset);
251 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
252 i = isl_basic_set_alloc_equality(bset);
253 if (i < 0)
254 goto error;
255 isl_seq_cpy(bset->eq[i], c, 1 + dim);
256 return bset;
257 error:
258 isl_basic_set_free(bset);
259 return NULL;
262 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
264 int i;
266 set = isl_set_cow(set);
267 if (!set)
268 return NULL;
269 for (i = 0; i < set->n; ++i) {
270 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
271 if (!set->p[i])
272 goto error;
274 return set;
275 error:
276 isl_set_free(set);
277 return NULL;
280 /* Given a union of basic sets, construct the constraints for wrapping
281 * a facet around one of its ridges.
282 * In particular, if each of n the d-dimensional basic sets i in "set"
283 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
284 * and is defined by the constraints
285 * [ 1 ]
286 * A_i [ x ] >= 0
288 * then the resulting set is of dimension n*(1+d) and has as constraints
290 * [ a_i ]
291 * A_i [ x_i ] >= 0
293 * a_i >= 0
295 * \sum_i x_{i,1} = 1
297 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
299 struct isl_basic_set *lp;
300 unsigned n_eq;
301 unsigned n_ineq;
302 int i, j, k;
303 unsigned dim, lp_dim;
305 if (!set)
306 return NULL;
308 dim = 1 + isl_set_n_dim(set);
309 n_eq = 1;
310 n_ineq = set->n;
311 for (i = 0; i < set->n; ++i) {
312 n_eq += set->p[i]->n_eq;
313 n_ineq += set->p[i]->n_ineq;
315 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
316 lp = isl_basic_set_set_rational(lp);
317 if (!lp)
318 return NULL;
319 lp_dim = isl_basic_set_n_dim(lp);
320 k = isl_basic_set_alloc_equality(lp);
321 isl_int_set_si(lp->eq[k][0], -1);
322 for (i = 0; i < set->n; ++i) {
323 isl_int_set_si(lp->eq[k][1+dim*i], 0);
324 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
325 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
327 for (i = 0; i < set->n; ++i) {
328 k = isl_basic_set_alloc_inequality(lp);
329 isl_seq_clr(lp->ineq[k], 1+lp_dim);
330 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
332 for (j = 0; j < set->p[i]->n_eq; ++j) {
333 k = isl_basic_set_alloc_equality(lp);
334 isl_seq_clr(lp->eq[k], 1+dim*i);
335 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
336 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
339 for (j = 0; j < set->p[i]->n_ineq; ++j) {
340 k = isl_basic_set_alloc_inequality(lp);
341 isl_seq_clr(lp->ineq[k], 1+dim*i);
342 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
343 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
346 return lp;
349 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
350 * of that facet, compute the other facet of the convex hull that contains
351 * the ridge.
353 * We first transform the set such that the facet constraint becomes
355 * x_1 >= 0
357 * I.e., the facet lies in
359 * x_1 = 0
361 * and on that facet, the constraint that defines the ridge is
363 * x_2 >= 0
365 * (This transformation is not strictly needed, all that is needed is
366 * that the ridge contains the origin.)
368 * Since the ridge contains the origin, the cone of the convex hull
369 * will be of the form
371 * x_1 >= 0
372 * x_2 >= a x_1
374 * with this second constraint defining the new facet.
375 * The constant a is obtained by settting x_1 in the cone of the
376 * convex hull to 1 and minimizing x_2.
377 * Now, each element in the cone of the convex hull is the sum
378 * of elements in the cones of the basic sets.
379 * If a_i is the dilation factor of basic set i, then the problem
380 * we need to solve is
382 * min \sum_i x_{i,2}
383 * st
384 * \sum_i x_{i,1} = 1
385 * a_i >= 0
386 * [ a_i ]
387 * A [ x_i ] >= 0
389 * with
390 * [ 1 ]
391 * A_i [ x_i ] >= 0
393 * the constraints of each (transformed) basic set.
394 * If a = n/d, then the constraint defining the new facet (in the transformed
395 * space) is
397 * -n x_1 + d x_2 >= 0
399 * In the original space, we need to take the same combination of the
400 * corresponding constraints "facet" and "ridge".
402 * If a = -infty = "-1/0", then we just return the original facet constraint.
403 * This means that the facet is unbounded, but has a bounded intersection
404 * with the union of sets.
406 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
407 isl_int *facet, isl_int *ridge)
409 int i;
410 isl_ctx *ctx;
411 struct isl_mat *T = NULL;
412 struct isl_basic_set *lp = NULL;
413 struct isl_vec *obj;
414 enum isl_lp_result res;
415 isl_int num, den;
416 unsigned dim;
418 if (!set)
419 return NULL;
420 ctx = set->ctx;
421 set = isl_set_copy(set);
422 set = isl_set_set_rational(set);
424 dim = 1 + isl_set_n_dim(set);
425 T = isl_mat_alloc(ctx, 3, dim);
426 if (!T)
427 goto error;
428 isl_int_set_si(T->row[0][0], 1);
429 isl_seq_clr(T->row[0]+1, dim - 1);
430 isl_seq_cpy(T->row[1], facet, dim);
431 isl_seq_cpy(T->row[2], ridge, dim);
432 T = isl_mat_right_inverse(T);
433 set = isl_set_preimage(set, T);
434 T = NULL;
435 if (!set)
436 goto error;
437 lp = wrap_constraints(set);
438 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
439 if (!obj)
440 goto error;
441 isl_int_set_si(obj->block.data[0], 0);
442 for (i = 0; i < set->n; ++i) {
443 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
444 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
445 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
447 isl_int_init(num);
448 isl_int_init(den);
449 res = isl_basic_set_solve_lp(lp, 0,
450 obj->block.data, ctx->one, &num, &den, NULL);
451 if (res == isl_lp_ok) {
452 isl_int_neg(num, num);
453 isl_seq_combine(facet, num, facet, den, ridge, dim);
454 isl_seq_normalize(ctx, facet, dim);
456 isl_int_clear(num);
457 isl_int_clear(den);
458 isl_vec_free(obj);
459 isl_basic_set_free(lp);
460 isl_set_free(set);
461 if (res == isl_lp_error)
462 return NULL;
463 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
464 return NULL);
465 return facet;
466 error:
467 isl_basic_set_free(lp);
468 isl_mat_free(T);
469 isl_set_free(set);
470 return NULL;
473 /* Compute the constraint of a facet of "set".
475 * We first compute the intersection with a bounding constraint
476 * that is orthogonal to one of the coordinate axes.
477 * If the affine hull of this intersection has only one equality,
478 * we have found a facet.
479 * Otherwise, we wrap the current bounding constraint around
480 * one of the equalities of the face (one that is not equal to
481 * the current bounding constraint).
482 * This process continues until we have found a facet.
483 * The dimension of the intersection increases by at least
484 * one on each iteration, so termination is guaranteed.
486 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
488 struct isl_set *slice = NULL;
489 struct isl_basic_set *face = NULL;
490 int i;
491 unsigned dim = isl_set_n_dim(set);
492 int is_bound;
493 isl_mat *bounds = NULL;
495 isl_assert(set->ctx, set->n > 0, goto error);
496 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
497 if (!bounds)
498 return NULL;
500 isl_seq_clr(bounds->row[0], dim);
501 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
502 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
503 if (is_bound < 0)
504 goto error;
505 isl_assert(set->ctx, is_bound, goto error);
506 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
507 bounds->n_row = 1;
509 for (;;) {
510 slice = isl_set_copy(set);
511 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
512 face = isl_set_affine_hull(slice);
513 if (!face)
514 goto error;
515 if (face->n_eq == 1) {
516 isl_basic_set_free(face);
517 break;
519 for (i = 0; i < face->n_eq; ++i)
520 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
521 !isl_seq_is_neg(bounds->row[0],
522 face->eq[i], 1 + dim))
523 break;
524 isl_assert(set->ctx, i < face->n_eq, goto error);
525 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
526 goto error;
527 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
528 isl_basic_set_free(face);
531 return bounds;
532 error:
533 isl_basic_set_free(face);
534 isl_mat_free(bounds);
535 return NULL;
538 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
539 * compute a hyperplane description of the facet, i.e., compute the facets
540 * of the facet.
542 * We compute an affine transformation that transforms the constraint
544 * [ 1 ]
545 * c [ x ] = 0
547 * to the constraint
549 * z_1 = 0
551 * by computing the right inverse U of a matrix that starts with the rows
553 * [ 1 0 ]
554 * [ c ]
556 * Then
557 * [ 1 ] [ 1 ]
558 * [ x ] = U [ z ]
559 * and
560 * [ 1 ] [ 1 ]
561 * [ z ] = Q [ x ]
563 * with Q = U^{-1}
564 * Since z_1 is zero, we can drop this variable as well as the corresponding
565 * column of U to obtain
567 * [ 1 ] [ 1 ]
568 * [ x ] = U' [ z' ]
569 * and
570 * [ 1 ] [ 1 ]
571 * [ z' ] = Q' [ x ]
573 * with Q' equal to Q, but without the corresponding row.
574 * After computing the facets of the facet in the z' space,
575 * we convert them back to the x space through Q.
577 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
579 struct isl_mat *m, *U, *Q;
580 struct isl_basic_set *facet = NULL;
581 struct isl_ctx *ctx;
582 unsigned dim;
584 ctx = set->ctx;
585 set = isl_set_copy(set);
586 dim = isl_set_n_dim(set);
587 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
588 if (!m)
589 goto error;
590 isl_int_set_si(m->row[0][0], 1);
591 isl_seq_clr(m->row[0]+1, dim);
592 isl_seq_cpy(m->row[1], c, 1+dim);
593 U = isl_mat_right_inverse(m);
594 Q = isl_mat_right_inverse(isl_mat_copy(U));
595 U = isl_mat_drop_cols(U, 1, 1);
596 Q = isl_mat_drop_rows(Q, 1, 1);
597 set = isl_set_preimage(set, U);
598 facet = uset_convex_hull_wrap_bounded(set);
599 facet = isl_basic_set_preimage(facet, Q);
600 if (facet && facet->n_eq != 0)
601 isl_die(ctx, isl_error_internal, "unexpected equality",
602 return isl_basic_set_free(facet));
603 return facet;
604 error:
605 isl_basic_set_free(facet);
606 isl_set_free(set);
607 return NULL;
610 /* Given an initial facet constraint, compute the remaining facets.
611 * We do this by running through all facets found so far and computing
612 * the adjacent facets through wrapping, adding those facets that we
613 * hadn't already found before.
615 * For each facet we have found so far, we first compute its facets
616 * in the resulting convex hull. That is, we compute the ridges
617 * of the resulting convex hull contained in the facet.
618 * We also compute the corresponding facet in the current approximation
619 * of the convex hull. There is no need to wrap around the ridges
620 * in this facet since that would result in a facet that is already
621 * present in the current approximation.
623 * This function can still be significantly optimized by checking which of
624 * the facets of the basic sets are also facets of the convex hull and
625 * using all the facets so far to help in constructing the facets of the
626 * facets
627 * and/or
628 * using the technique in section "3.1 Ridge Generation" of
629 * "Extended Convex Hull" by Fukuda et al.
631 static struct isl_basic_set *extend(struct isl_basic_set *hull,
632 struct isl_set *set)
634 int i, j, f;
635 int k;
636 struct isl_basic_set *facet = NULL;
637 struct isl_basic_set *hull_facet = NULL;
638 unsigned dim;
640 if (!hull)
641 return NULL;
643 isl_assert(set->ctx, set->n > 0, goto error);
645 dim = isl_set_n_dim(set);
647 for (i = 0; i < hull->n_ineq; ++i) {
648 facet = compute_facet(set, hull->ineq[i]);
649 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
650 facet = isl_basic_set_gauss(facet, NULL);
651 facet = isl_basic_set_normalize_constraints(facet);
652 hull_facet = isl_basic_set_copy(hull);
653 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
654 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
655 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
656 if (!facet || !hull_facet)
657 goto error;
658 hull = isl_basic_set_cow(hull);
659 hull = isl_basic_set_extend_space(hull,
660 isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
661 if (!hull)
662 goto error;
663 for (j = 0; j < facet->n_ineq; ++j) {
664 for (f = 0; f < hull_facet->n_ineq; ++f)
665 if (isl_seq_eq(facet->ineq[j],
666 hull_facet->ineq[f], 1 + dim))
667 break;
668 if (f < hull_facet->n_ineq)
669 continue;
670 k = isl_basic_set_alloc_inequality(hull);
671 if (k < 0)
672 goto error;
673 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
674 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
675 goto error;
677 isl_basic_set_free(hull_facet);
678 isl_basic_set_free(facet);
680 hull = isl_basic_set_simplify(hull);
681 hull = isl_basic_set_finalize(hull);
682 return hull;
683 error:
684 isl_basic_set_free(hull_facet);
685 isl_basic_set_free(facet);
686 isl_basic_set_free(hull);
687 return NULL;
690 /* Special case for computing the convex hull of a one dimensional set.
691 * We simply collect the lower and upper bounds of each basic set
692 * and the biggest of those.
694 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
696 struct isl_mat *c = NULL;
697 isl_int *lower = NULL;
698 isl_int *upper = NULL;
699 int i, j, k;
700 isl_int a, b;
701 struct isl_basic_set *hull;
703 for (i = 0; i < set->n; ++i) {
704 set->p[i] = isl_basic_set_simplify(set->p[i]);
705 if (!set->p[i])
706 goto error;
708 set = isl_set_remove_empty_parts(set);
709 if (!set)
710 goto error;
711 isl_assert(set->ctx, set->n > 0, goto error);
712 c = isl_mat_alloc(set->ctx, 2, 2);
713 if (!c)
714 goto error;
716 if (set->p[0]->n_eq > 0) {
717 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
718 lower = c->row[0];
719 upper = c->row[1];
720 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
721 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
722 isl_seq_neg(upper, set->p[0]->eq[0], 2);
723 } else {
724 isl_seq_neg(lower, set->p[0]->eq[0], 2);
725 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
727 } else {
728 for (j = 0; j < set->p[0]->n_ineq; ++j) {
729 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
730 lower = c->row[0];
731 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
732 } else {
733 upper = c->row[1];
734 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
739 isl_int_init(a);
740 isl_int_init(b);
741 for (i = 0; i < set->n; ++i) {
742 struct isl_basic_set *bset = set->p[i];
743 int has_lower = 0;
744 int has_upper = 0;
746 for (j = 0; j < bset->n_eq; ++j) {
747 has_lower = 1;
748 has_upper = 1;
749 if (lower) {
750 isl_int_mul(a, lower[0], bset->eq[j][1]);
751 isl_int_mul(b, lower[1], bset->eq[j][0]);
752 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
753 isl_seq_cpy(lower, bset->eq[j], 2);
754 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
755 isl_seq_neg(lower, bset->eq[j], 2);
757 if (upper) {
758 isl_int_mul(a, upper[0], bset->eq[j][1]);
759 isl_int_mul(b, upper[1], bset->eq[j][0]);
760 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
761 isl_seq_neg(upper, bset->eq[j], 2);
762 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
763 isl_seq_cpy(upper, bset->eq[j], 2);
766 for (j = 0; j < bset->n_ineq; ++j) {
767 if (isl_int_is_pos(bset->ineq[j][1]))
768 has_lower = 1;
769 if (isl_int_is_neg(bset->ineq[j][1]))
770 has_upper = 1;
771 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
772 isl_int_mul(a, lower[0], bset->ineq[j][1]);
773 isl_int_mul(b, lower[1], bset->ineq[j][0]);
774 if (isl_int_lt(a, b))
775 isl_seq_cpy(lower, bset->ineq[j], 2);
777 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
778 isl_int_mul(a, upper[0], bset->ineq[j][1]);
779 isl_int_mul(b, upper[1], bset->ineq[j][0]);
780 if (isl_int_gt(a, b))
781 isl_seq_cpy(upper, bset->ineq[j], 2);
784 if (!has_lower)
785 lower = NULL;
786 if (!has_upper)
787 upper = NULL;
789 isl_int_clear(a);
790 isl_int_clear(b);
792 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
793 hull = isl_basic_set_set_rational(hull);
794 if (!hull)
795 goto error;
796 if (lower) {
797 k = isl_basic_set_alloc_inequality(hull);
798 isl_seq_cpy(hull->ineq[k], lower, 2);
800 if (upper) {
801 k = isl_basic_set_alloc_inequality(hull);
802 isl_seq_cpy(hull->ineq[k], upper, 2);
804 hull = isl_basic_set_finalize(hull);
805 isl_set_free(set);
806 isl_mat_free(c);
807 return hull;
808 error:
809 isl_set_free(set);
810 isl_mat_free(c);
811 return NULL;
814 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
816 struct isl_basic_set *convex_hull;
818 if (!set)
819 return NULL;
821 if (isl_set_is_empty(set))
822 convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
823 else
824 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
825 isl_set_free(set);
826 return convex_hull;
829 /* Compute the convex hull of a pair of basic sets without any parameters or
830 * integer divisions using Fourier-Motzkin elimination.
831 * The convex hull is the set of all points that can be written as
832 * the sum of points from both basic sets (in homogeneous coordinates).
833 * We set up the constraints in a space with dimensions for each of
834 * the three sets and then project out the dimensions corresponding
835 * to the two original basic sets, retaining only those corresponding
836 * to the convex hull.
838 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
839 struct isl_basic_set *bset2)
841 int i, j, k;
842 struct isl_basic_set *bset[2];
843 struct isl_basic_set *hull = NULL;
844 unsigned dim;
846 if (!bset1 || !bset2)
847 goto error;
849 dim = isl_basic_set_n_dim(bset1);
850 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
851 1 + dim + bset1->n_eq + bset2->n_eq,
852 2 + bset1->n_ineq + bset2->n_ineq);
853 bset[0] = bset1;
854 bset[1] = bset2;
855 for (i = 0; i < 2; ++i) {
856 for (j = 0; j < bset[i]->n_eq; ++j) {
857 k = isl_basic_set_alloc_equality(hull);
858 if (k < 0)
859 goto error;
860 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
861 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
862 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
863 1+dim);
865 for (j = 0; j < bset[i]->n_ineq; ++j) {
866 k = isl_basic_set_alloc_inequality(hull);
867 if (k < 0)
868 goto error;
869 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
870 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
871 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
872 bset[i]->ineq[j], 1+dim);
874 k = isl_basic_set_alloc_inequality(hull);
875 if (k < 0)
876 goto error;
877 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
878 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
880 for (j = 0; j < 1+dim; ++j) {
881 k = isl_basic_set_alloc_equality(hull);
882 if (k < 0)
883 goto error;
884 isl_seq_clr(hull->eq[k], 1+2+3*dim);
885 isl_int_set_si(hull->eq[k][j], -1);
886 isl_int_set_si(hull->eq[k][1+dim+j], 1);
887 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
889 hull = isl_basic_set_set_rational(hull);
890 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
891 hull = isl_basic_set_remove_redundancies(hull);
892 isl_basic_set_free(bset1);
893 isl_basic_set_free(bset2);
894 return hull;
895 error:
896 isl_basic_set_free(bset1);
897 isl_basic_set_free(bset2);
898 isl_basic_set_free(hull);
899 return NULL;
902 /* Is the set bounded for each value of the parameters?
904 int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
906 struct isl_tab *tab;
907 int bounded;
909 if (!bset)
910 return -1;
911 if (isl_basic_set_plain_is_empty(bset))
912 return 1;
914 tab = isl_tab_from_recession_cone(bset, 1);
915 bounded = isl_tab_cone_is_bounded(tab);
916 isl_tab_free(tab);
917 return bounded;
920 /* Is the image bounded for each value of the parameters and
921 * the domain variables?
923 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
925 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
926 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
927 int bounded;
929 bmap = isl_basic_map_copy(bmap);
930 bmap = isl_basic_map_cow(bmap);
931 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
932 isl_dim_in, 0, n_in);
933 bounded = isl_basic_set_is_bounded((isl_basic_set *)bmap);
934 isl_basic_map_free(bmap);
936 return bounded;
939 /* Is the set bounded for each value of the parameters?
941 int isl_set_is_bounded(__isl_keep isl_set *set)
943 int i;
945 if (!set)
946 return -1;
948 for (i = 0; i < set->n; ++i) {
949 int bounded = isl_basic_set_is_bounded(set->p[i]);
950 if (!bounded || bounded < 0)
951 return bounded;
953 return 1;
956 /* Compute the lineality space of the convex hull of bset1 and bset2.
958 * We first compute the intersection of the recession cone of bset1
959 * with the negative of the recession cone of bset2 and then compute
960 * the linear hull of the resulting cone.
962 static struct isl_basic_set *induced_lineality_space(
963 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
965 int i, k;
966 struct isl_basic_set *lin = NULL;
967 unsigned dim;
969 if (!bset1 || !bset2)
970 goto error;
972 dim = isl_basic_set_total_dim(bset1);
973 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
974 bset1->n_eq + bset2->n_eq,
975 bset1->n_ineq + bset2->n_ineq);
976 lin = isl_basic_set_set_rational(lin);
977 if (!lin)
978 goto error;
979 for (i = 0; i < bset1->n_eq; ++i) {
980 k = isl_basic_set_alloc_equality(lin);
981 if (k < 0)
982 goto error;
983 isl_int_set_si(lin->eq[k][0], 0);
984 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
986 for (i = 0; i < bset1->n_ineq; ++i) {
987 k = isl_basic_set_alloc_inequality(lin);
988 if (k < 0)
989 goto error;
990 isl_int_set_si(lin->ineq[k][0], 0);
991 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
993 for (i = 0; i < bset2->n_eq; ++i) {
994 k = isl_basic_set_alloc_equality(lin);
995 if (k < 0)
996 goto error;
997 isl_int_set_si(lin->eq[k][0], 0);
998 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
1000 for (i = 0; i < bset2->n_ineq; ++i) {
1001 k = isl_basic_set_alloc_inequality(lin);
1002 if (k < 0)
1003 goto error;
1004 isl_int_set_si(lin->ineq[k][0], 0);
1005 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
1008 isl_basic_set_free(bset1);
1009 isl_basic_set_free(bset2);
1010 return isl_basic_set_affine_hull(lin);
1011 error:
1012 isl_basic_set_free(lin);
1013 isl_basic_set_free(bset1);
1014 isl_basic_set_free(bset2);
1015 return NULL;
1018 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1020 /* Given a set and a linear space "lin" of dimension n > 0,
1021 * project the linear space from the set, compute the convex hull
1022 * and then map the set back to the original space.
1024 * Let
1026 * M x = 0
1028 * describe the linear space. We first compute the Hermite normal
1029 * form H = M U of M = H Q, to obtain
1031 * H Q x = 0
1033 * The last n rows of H will be zero, so the last n variables of x' = Q x
1034 * are the one we want to project out. We do this by transforming each
1035 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1036 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1037 * we transform the hull back to the original space as A' Q_1 x >= b',
1038 * with Q_1 all but the last n rows of Q.
1040 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1041 struct isl_basic_set *lin)
1043 unsigned total = isl_basic_set_total_dim(lin);
1044 unsigned lin_dim;
1045 struct isl_basic_set *hull;
1046 struct isl_mat *M, *U, *Q;
1048 if (!set || !lin)
1049 goto error;
1050 lin_dim = total - lin->n_eq;
1051 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1052 M = isl_mat_left_hermite(M, 0, &U, &Q);
1053 if (!M)
1054 goto error;
1055 isl_mat_free(M);
1056 isl_basic_set_free(lin);
1058 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1060 U = isl_mat_lin_to_aff(U);
1061 Q = isl_mat_lin_to_aff(Q);
1063 set = isl_set_preimage(set, U);
1064 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1065 hull = uset_convex_hull(set);
1066 hull = isl_basic_set_preimage(hull, Q);
1068 return hull;
1069 error:
1070 isl_basic_set_free(lin);
1071 isl_set_free(set);
1072 return NULL;
1075 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1076 * set up an LP for solving
1078 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1080 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1081 * The next \alpha{ij} correspond to the equalities and come in pairs.
1082 * The final \alpha{ij} correspond to the inequalities.
1084 static struct isl_basic_set *valid_direction_lp(
1085 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1087 isl_space *dim;
1088 struct isl_basic_set *lp;
1089 unsigned d;
1090 int n;
1091 int i, j, k;
1093 if (!bset1 || !bset2)
1094 goto error;
1095 d = 1 + isl_basic_set_total_dim(bset1);
1096 n = 2 +
1097 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1098 dim = isl_space_set_alloc(bset1->ctx, 0, n);
1099 lp = isl_basic_set_alloc_space(dim, 0, d, n);
1100 if (!lp)
1101 goto error;
1102 for (i = 0; i < n; ++i) {
1103 k = isl_basic_set_alloc_inequality(lp);
1104 if (k < 0)
1105 goto error;
1106 isl_seq_clr(lp->ineq[k] + 1, n);
1107 isl_int_set_si(lp->ineq[k][0], -1);
1108 isl_int_set_si(lp->ineq[k][1 + i], 1);
1110 for (i = 0; i < d; ++i) {
1111 k = isl_basic_set_alloc_equality(lp);
1112 if (k < 0)
1113 goto error;
1114 n = 0;
1115 isl_int_set_si(lp->eq[k][n], 0); n++;
1116 /* positivity constraint 1 >= 0 */
1117 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1118 for (j = 0; j < bset1->n_eq; ++j) {
1119 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1120 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1122 for (j = 0; j < bset1->n_ineq; ++j) {
1123 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1125 /* positivity constraint 1 >= 0 */
1126 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1127 for (j = 0; j < bset2->n_eq; ++j) {
1128 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1129 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1131 for (j = 0; j < bset2->n_ineq; ++j) {
1132 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1135 lp = isl_basic_set_gauss(lp, NULL);
1136 isl_basic_set_free(bset1);
1137 isl_basic_set_free(bset2);
1138 return lp;
1139 error:
1140 isl_basic_set_free(bset1);
1141 isl_basic_set_free(bset2);
1142 return NULL;
1145 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1146 * for all rays in the homogeneous space of the two cones that correspond
1147 * to the input polyhedra bset1 and bset2.
1149 * We compute s as a vector that satisfies
1151 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1153 * with h_{ij} the normals of the facets of polyhedron i
1154 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1155 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1156 * We first set up an LP with as variables the \alpha{ij}.
1157 * In this formulation, for each polyhedron i,
1158 * the first constraint is the positivity constraint, followed by pairs
1159 * of variables for the equalities, followed by variables for the inequalities.
1160 * We then simply pick a feasible solution and compute s using (*).
1162 * Note that we simply pick any valid direction and make no attempt
1163 * to pick a "good" or even the "best" valid direction.
1165 static struct isl_vec *valid_direction(
1166 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1168 struct isl_basic_set *lp;
1169 struct isl_tab *tab;
1170 struct isl_vec *sample = NULL;
1171 struct isl_vec *dir;
1172 unsigned d;
1173 int i;
1174 int n;
1176 if (!bset1 || !bset2)
1177 goto error;
1178 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1179 isl_basic_set_copy(bset2));
1180 tab = isl_tab_from_basic_set(lp, 0);
1181 sample = isl_tab_get_sample_value(tab);
1182 isl_tab_free(tab);
1183 isl_basic_set_free(lp);
1184 if (!sample)
1185 goto error;
1186 d = isl_basic_set_total_dim(bset1);
1187 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1188 if (!dir)
1189 goto error;
1190 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1191 n = 1;
1192 /* positivity constraint 1 >= 0 */
1193 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1194 for (i = 0; i < bset1->n_eq; ++i) {
1195 isl_int_sub(sample->block.data[n],
1196 sample->block.data[n], sample->block.data[n+1]);
1197 isl_seq_combine(dir->block.data,
1198 bset1->ctx->one, dir->block.data,
1199 sample->block.data[n], bset1->eq[i], 1 + d);
1201 n += 2;
1203 for (i = 0; i < bset1->n_ineq; ++i)
1204 isl_seq_combine(dir->block.data,
1205 bset1->ctx->one, dir->block.data,
1206 sample->block.data[n++], bset1->ineq[i], 1 + d);
1207 isl_vec_free(sample);
1208 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1209 isl_basic_set_free(bset1);
1210 isl_basic_set_free(bset2);
1211 return dir;
1212 error:
1213 isl_vec_free(sample);
1214 isl_basic_set_free(bset1);
1215 isl_basic_set_free(bset2);
1216 return NULL;
1219 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1220 * compute b_i' + A_i' x' >= 0, with
1222 * [ b_i A_i ] [ y' ] [ y' ]
1223 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1225 * In particular, add the "positivity constraint" and then perform
1226 * the mapping.
1228 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1229 struct isl_mat *T)
1231 int k;
1233 if (!bset)
1234 goto error;
1235 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1236 k = isl_basic_set_alloc_inequality(bset);
1237 if (k < 0)
1238 goto error;
1239 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1240 isl_int_set_si(bset->ineq[k][0], 1);
1241 bset = isl_basic_set_preimage(bset, T);
1242 return bset;
1243 error:
1244 isl_mat_free(T);
1245 isl_basic_set_free(bset);
1246 return NULL;
1249 /* Compute the convex hull of a pair of basic sets without any parameters or
1250 * integer divisions, where the convex hull is known to be pointed,
1251 * but the basic sets may be unbounded.
1253 * We turn this problem into the computation of a convex hull of a pair
1254 * _bounded_ polyhedra by "changing the direction of the homogeneous
1255 * dimension". This idea is due to Matthias Koeppe.
1257 * Consider the cones in homogeneous space that correspond to the
1258 * input polyhedra. The rays of these cones are also rays of the
1259 * polyhedra if the coordinate that corresponds to the homogeneous
1260 * dimension is zero. That is, if the inner product of the rays
1261 * with the homogeneous direction is zero.
1262 * The cones in the homogeneous space can also be considered to
1263 * correspond to other pairs of polyhedra by chosing a different
1264 * homogeneous direction. To ensure that both of these polyhedra
1265 * are bounded, we need to make sure that all rays of the cones
1266 * correspond to vertices and not to rays.
1267 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1268 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1269 * The vector s is computed in valid_direction.
1271 * Note that we need to consider _all_ rays of the cones and not just
1272 * the rays that correspond to rays in the polyhedra. If we were to
1273 * only consider those rays and turn them into vertices, then we
1274 * may inadvertently turn some vertices into rays.
1276 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1277 * We therefore transform the two polyhedra such that the selected
1278 * direction is mapped onto this standard direction and then proceed
1279 * with the normal computation.
1280 * Let S be a non-singular square matrix with s as its first row,
1281 * then we want to map the polyhedra to the space
1283 * [ y' ] [ y ] [ y ] [ y' ]
1284 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1286 * We take S to be the unimodular completion of s to limit the growth
1287 * of the coefficients in the following computations.
1289 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1290 * We first move to the homogeneous dimension
1292 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1293 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1295 * Then we change directoin
1297 * [ b_i A_i ] [ y' ] [ y' ]
1298 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1300 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1301 * resulting in b' + A' x' >= 0, which we then convert back
1303 * [ y ] [ y ]
1304 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1306 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1308 static struct isl_basic_set *convex_hull_pair_pointed(
1309 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1311 struct isl_ctx *ctx = NULL;
1312 struct isl_vec *dir = NULL;
1313 struct isl_mat *T = NULL;
1314 struct isl_mat *T2 = NULL;
1315 struct isl_basic_set *hull;
1316 struct isl_set *set;
1318 if (!bset1 || !bset2)
1319 goto error;
1320 ctx = isl_basic_set_get_ctx(bset1);
1321 dir = valid_direction(isl_basic_set_copy(bset1),
1322 isl_basic_set_copy(bset2));
1323 if (!dir)
1324 goto error;
1325 T = isl_mat_alloc(ctx, dir->size, dir->size);
1326 if (!T)
1327 goto error;
1328 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1329 T = isl_mat_unimodular_complete(T, 1);
1330 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1332 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1333 bset2 = homogeneous_map(bset2, T2);
1334 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1335 set = isl_set_add_basic_set(set, bset1);
1336 set = isl_set_add_basic_set(set, bset2);
1337 hull = uset_convex_hull(set);
1338 hull = isl_basic_set_preimage(hull, T);
1340 isl_vec_free(dir);
1342 return hull;
1343 error:
1344 isl_vec_free(dir);
1345 isl_basic_set_free(bset1);
1346 isl_basic_set_free(bset2);
1347 return NULL;
1350 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1351 static struct isl_basic_set *modulo_affine_hull(
1352 struct isl_set *set, struct isl_basic_set *affine_hull);
1354 /* Compute the convex hull of a pair of basic sets without any parameters or
1355 * integer divisions.
1357 * This function is called from uset_convex_hull_unbounded, which
1358 * means that the complete convex hull is unbounded. Some pairs
1359 * of basic sets may still be bounded, though.
1360 * They may even lie inside a lower dimensional space, in which
1361 * case they need to be handled inside their affine hull since
1362 * the main algorithm assumes that the result is full-dimensional.
1364 * If the convex hull of the two basic sets would have a non-trivial
1365 * lineality space, we first project out this lineality space.
1367 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1368 struct isl_basic_set *bset2)
1370 isl_basic_set *lin, *aff;
1371 int bounded1, bounded2;
1373 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1374 return convex_hull_pair_elim(bset1, bset2);
1376 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1377 isl_basic_set_copy(bset2)));
1378 if (!aff)
1379 goto error;
1380 if (aff->n_eq != 0)
1381 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1382 isl_basic_set_free(aff);
1384 bounded1 = isl_basic_set_is_bounded(bset1);
1385 bounded2 = isl_basic_set_is_bounded(bset2);
1387 if (bounded1 < 0 || bounded2 < 0)
1388 goto error;
1390 if (bounded1 && bounded2)
1391 return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1393 if (bounded1 || bounded2)
1394 return convex_hull_pair_pointed(bset1, bset2);
1396 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1397 isl_basic_set_copy(bset2));
1398 if (!lin)
1399 goto error;
1400 if (isl_basic_set_plain_is_universe(lin)) {
1401 isl_basic_set_free(bset1);
1402 isl_basic_set_free(bset2);
1403 return lin;
1405 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1406 struct isl_set *set;
1407 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1408 set = isl_set_add_basic_set(set, bset1);
1409 set = isl_set_add_basic_set(set, bset2);
1410 return modulo_lineality(set, lin);
1412 isl_basic_set_free(lin);
1414 return convex_hull_pair_pointed(bset1, bset2);
1415 error:
1416 isl_basic_set_free(bset1);
1417 isl_basic_set_free(bset2);
1418 return NULL;
1421 /* Compute the lineality space of a basic set.
1422 * We currently do not allow the basic set to have any divs.
1423 * We basically just drop the constants and turn every inequality
1424 * into an equality.
1426 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1428 int i, k;
1429 struct isl_basic_set *lin = NULL;
1430 unsigned dim;
1432 if (!bset)
1433 goto error;
1434 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1435 dim = isl_basic_set_total_dim(bset);
1437 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, dim, 0);
1438 if (!lin)
1439 goto error;
1440 for (i = 0; i < bset->n_eq; ++i) {
1441 k = isl_basic_set_alloc_equality(lin);
1442 if (k < 0)
1443 goto error;
1444 isl_int_set_si(lin->eq[k][0], 0);
1445 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1447 lin = isl_basic_set_gauss(lin, NULL);
1448 if (!lin)
1449 goto error;
1450 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1451 k = isl_basic_set_alloc_equality(lin);
1452 if (k < 0)
1453 goto error;
1454 isl_int_set_si(lin->eq[k][0], 0);
1455 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1456 lin = isl_basic_set_gauss(lin, NULL);
1457 if (!lin)
1458 goto error;
1460 isl_basic_set_free(bset);
1461 return lin;
1462 error:
1463 isl_basic_set_free(lin);
1464 isl_basic_set_free(bset);
1465 return NULL;
1468 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1469 * "underlying" set "set".
1471 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1473 int i;
1474 struct isl_set *lin = NULL;
1476 if (!set)
1477 return NULL;
1478 if (set->n == 0) {
1479 isl_space *dim = isl_set_get_space(set);
1480 isl_set_free(set);
1481 return isl_basic_set_empty(dim);
1484 lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
1485 for (i = 0; i < set->n; ++i)
1486 lin = isl_set_add_basic_set(lin,
1487 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1488 isl_set_free(set);
1489 return isl_set_affine_hull(lin);
1492 /* Compute the convex hull of a set without any parameters or
1493 * integer divisions.
1494 * In each step, we combined two basic sets until only one
1495 * basic set is left.
1496 * The input basic sets are assumed not to have a non-trivial
1497 * lineality space. If any of the intermediate results has
1498 * a non-trivial lineality space, it is projected out.
1500 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1502 struct isl_basic_set *convex_hull = NULL;
1504 convex_hull = isl_set_copy_basic_set(set);
1505 set = isl_set_drop_basic_set(set, convex_hull);
1506 if (!set)
1507 goto error;
1508 while (set->n > 0) {
1509 struct isl_basic_set *t;
1510 t = isl_set_copy_basic_set(set);
1511 if (!t)
1512 goto error;
1513 set = isl_set_drop_basic_set(set, t);
1514 if (!set)
1515 goto error;
1516 convex_hull = convex_hull_pair(convex_hull, t);
1517 if (set->n == 0)
1518 break;
1519 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1520 if (!t)
1521 goto error;
1522 if (isl_basic_set_plain_is_universe(t)) {
1523 isl_basic_set_free(convex_hull);
1524 convex_hull = t;
1525 break;
1527 if (t->n_eq < isl_basic_set_total_dim(t)) {
1528 convex_hull = isl_basic_set_underlying_set(convex_hull);
1529 set = isl_set_add_basic_set(set, convex_hull);
1530 return modulo_lineality(set, t);
1532 isl_basic_set_free(t);
1534 isl_set_free(set);
1535 return convex_hull;
1536 error:
1537 isl_set_free(set);
1538 isl_basic_set_free(convex_hull);
1539 return NULL;
1542 /* Compute an initial hull for wrapping containing a single initial
1543 * facet.
1544 * This function assumes that the given set is bounded.
1546 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1547 struct isl_set *set)
1549 struct isl_mat *bounds = NULL;
1550 unsigned dim;
1551 int k;
1553 if (!hull)
1554 goto error;
1555 bounds = initial_facet_constraint(set);
1556 if (!bounds)
1557 goto error;
1558 k = isl_basic_set_alloc_inequality(hull);
1559 if (k < 0)
1560 goto error;
1561 dim = isl_set_n_dim(set);
1562 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1563 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1564 isl_mat_free(bounds);
1566 return hull;
1567 error:
1568 isl_basic_set_free(hull);
1569 isl_mat_free(bounds);
1570 return NULL;
1573 struct max_constraint {
1574 struct isl_mat *c;
1575 int count;
1576 int ineq;
1579 static int max_constraint_equal(const void *entry, const void *val)
1581 struct max_constraint *a = (struct max_constraint *)entry;
1582 isl_int *b = (isl_int *)val;
1584 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1587 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1588 isl_int *con, unsigned len, int n, int ineq)
1590 struct isl_hash_table_entry *entry;
1591 struct max_constraint *c;
1592 uint32_t c_hash;
1594 c_hash = isl_seq_get_hash(con + 1, len);
1595 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1596 con + 1, 0);
1597 if (!entry)
1598 return;
1599 c = entry->data;
1600 if (c->count < n) {
1601 isl_hash_table_remove(ctx, table, entry);
1602 return;
1604 c->count++;
1605 if (isl_int_gt(c->c->row[0][0], con[0]))
1606 return;
1607 if (isl_int_eq(c->c->row[0][0], con[0])) {
1608 if (ineq)
1609 c->ineq = ineq;
1610 return;
1612 c->c = isl_mat_cow(c->c);
1613 isl_int_set(c->c->row[0][0], con[0]);
1614 c->ineq = ineq;
1617 /* Check whether the constraint hash table "table" constains the constraint
1618 * "con".
1620 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1621 isl_int *con, unsigned len, int n)
1623 struct isl_hash_table_entry *entry;
1624 struct max_constraint *c;
1625 uint32_t c_hash;
1627 c_hash = isl_seq_get_hash(con + 1, len);
1628 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1629 con + 1, 0);
1630 if (!entry)
1631 return 0;
1632 c = entry->data;
1633 if (c->count < n)
1634 return 0;
1635 return isl_int_eq(c->c->row[0][0], con[0]);
1638 /* Check for inequality constraints of a basic set without equalities
1639 * such that the same or more stringent copies of the constraint appear
1640 * in all of the basic sets. Such constraints are necessarily facet
1641 * constraints of the convex hull.
1643 * If the resulting basic set is by chance identical to one of
1644 * the basic sets in "set", then we know that this basic set contains
1645 * all other basic sets and is therefore the convex hull of set.
1646 * In this case we set *is_hull to 1.
1648 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1649 struct isl_set *set, int *is_hull)
1651 int i, j, s, n;
1652 int min_constraints;
1653 int best;
1654 struct max_constraint *constraints = NULL;
1655 struct isl_hash_table *table = NULL;
1656 unsigned total;
1658 *is_hull = 0;
1660 for (i = 0; i < set->n; ++i)
1661 if (set->p[i]->n_eq == 0)
1662 break;
1663 if (i >= set->n)
1664 return hull;
1665 min_constraints = set->p[i]->n_ineq;
1666 best = i;
1667 for (i = best + 1; i < set->n; ++i) {
1668 if (set->p[i]->n_eq != 0)
1669 continue;
1670 if (set->p[i]->n_ineq >= min_constraints)
1671 continue;
1672 min_constraints = set->p[i]->n_ineq;
1673 best = i;
1675 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1676 min_constraints);
1677 if (!constraints)
1678 return hull;
1679 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1680 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1681 goto error;
1683 total = isl_space_dim(set->dim, isl_dim_all);
1684 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1685 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1686 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1687 if (!constraints[i].c)
1688 goto error;
1689 constraints[i].ineq = 1;
1691 for (i = 0; i < min_constraints; ++i) {
1692 struct isl_hash_table_entry *entry;
1693 uint32_t c_hash;
1694 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1695 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1696 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1697 if (!entry)
1698 goto error;
1699 isl_assert(hull->ctx, !entry->data, goto error);
1700 entry->data = &constraints[i];
1703 n = 0;
1704 for (s = 0; s < set->n; ++s) {
1705 if (s == best)
1706 continue;
1708 for (i = 0; i < set->p[s]->n_eq; ++i) {
1709 isl_int *eq = set->p[s]->eq[i];
1710 for (j = 0; j < 2; ++j) {
1711 isl_seq_neg(eq, eq, 1 + total);
1712 update_constraint(hull->ctx, table,
1713 eq, total, n, 0);
1716 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1717 isl_int *ineq = set->p[s]->ineq[i];
1718 update_constraint(hull->ctx, table, ineq, total, n,
1719 set->p[s]->n_eq == 0);
1721 ++n;
1724 for (i = 0; i < min_constraints; ++i) {
1725 if (constraints[i].count < n)
1726 continue;
1727 if (!constraints[i].ineq)
1728 continue;
1729 j = isl_basic_set_alloc_inequality(hull);
1730 if (j < 0)
1731 goto error;
1732 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1735 for (s = 0; s < set->n; ++s) {
1736 if (set->p[s]->n_eq)
1737 continue;
1738 if (set->p[s]->n_ineq != hull->n_ineq)
1739 continue;
1740 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1741 isl_int *ineq = set->p[s]->ineq[i];
1742 if (!has_constraint(hull->ctx, table, ineq, total, n))
1743 break;
1745 if (i == set->p[s]->n_ineq)
1746 *is_hull = 1;
1749 isl_hash_table_clear(table);
1750 for (i = 0; i < min_constraints; ++i)
1751 isl_mat_free(constraints[i].c);
1752 free(constraints);
1753 free(table);
1754 return hull;
1755 error:
1756 isl_hash_table_clear(table);
1757 free(table);
1758 if (constraints)
1759 for (i = 0; i < min_constraints; ++i)
1760 isl_mat_free(constraints[i].c);
1761 free(constraints);
1762 return hull;
1765 /* Create a template for the convex hull of "set" and fill it up
1766 * obvious facet constraints, if any. If the result happens to
1767 * be the convex hull of "set" then *is_hull is set to 1.
1769 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1771 struct isl_basic_set *hull;
1772 unsigned n_ineq;
1773 int i;
1775 n_ineq = 1;
1776 for (i = 0; i < set->n; ++i) {
1777 n_ineq += set->p[i]->n_eq;
1778 n_ineq += set->p[i]->n_ineq;
1780 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1781 hull = isl_basic_set_set_rational(hull);
1782 if (!hull)
1783 return NULL;
1784 return common_constraints(hull, set, is_hull);
1787 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1789 struct isl_basic_set *hull;
1790 int is_hull;
1792 hull = proto_hull(set, &is_hull);
1793 if (hull && !is_hull) {
1794 if (hull->n_ineq == 0)
1795 hull = initial_hull(hull, set);
1796 hull = extend(hull, set);
1798 isl_set_free(set);
1800 return hull;
1803 /* Compute the convex hull of a set without any parameters or
1804 * integer divisions. Depending on whether the set is bounded,
1805 * we pass control to the wrapping based convex hull or
1806 * the Fourier-Motzkin elimination based convex hull.
1807 * We also handle a few special cases before checking the boundedness.
1809 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1811 struct isl_basic_set *convex_hull = NULL;
1812 struct isl_basic_set *lin;
1814 if (isl_set_n_dim(set) == 0)
1815 return convex_hull_0d(set);
1817 set = isl_set_coalesce(set);
1818 set = isl_set_set_rational(set);
1820 if (!set)
1821 goto error;
1822 if (!set)
1823 return NULL;
1824 if (set->n == 1) {
1825 convex_hull = isl_basic_set_copy(set->p[0]);
1826 isl_set_free(set);
1827 return convex_hull;
1829 if (isl_set_n_dim(set) == 1)
1830 return convex_hull_1d(set);
1832 if (isl_set_is_bounded(set) &&
1833 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1834 return uset_convex_hull_wrap(set);
1836 lin = uset_combined_lineality_space(isl_set_copy(set));
1837 if (!lin)
1838 goto error;
1839 if (isl_basic_set_plain_is_universe(lin)) {
1840 isl_set_free(set);
1841 return lin;
1843 if (lin->n_eq < isl_basic_set_total_dim(lin))
1844 return modulo_lineality(set, lin);
1845 isl_basic_set_free(lin);
1847 return uset_convex_hull_unbounded(set);
1848 error:
1849 isl_set_free(set);
1850 isl_basic_set_free(convex_hull);
1851 return NULL;
1854 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1855 * without parameters or divs and where the convex hull of set is
1856 * known to be full-dimensional.
1858 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1860 struct isl_basic_set *convex_hull = NULL;
1862 if (!set)
1863 goto error;
1865 if (isl_set_n_dim(set) == 0) {
1866 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1867 isl_set_free(set);
1868 convex_hull = isl_basic_set_set_rational(convex_hull);
1869 return convex_hull;
1872 set = isl_set_set_rational(set);
1873 set = isl_set_coalesce(set);
1874 if (!set)
1875 goto error;
1876 if (set->n == 1) {
1877 convex_hull = isl_basic_set_copy(set->p[0]);
1878 isl_set_free(set);
1879 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1880 return convex_hull;
1882 if (isl_set_n_dim(set) == 1)
1883 return convex_hull_1d(set);
1885 return uset_convex_hull_wrap(set);
1886 error:
1887 isl_set_free(set);
1888 return NULL;
1891 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1892 * We first remove the equalities (transforming the set), compute the
1893 * convex hull of the transformed set and then add the equalities back
1894 * (after performing the inverse transformation.
1896 static struct isl_basic_set *modulo_affine_hull(
1897 struct isl_set *set, struct isl_basic_set *affine_hull)
1899 struct isl_mat *T;
1900 struct isl_mat *T2;
1901 struct isl_basic_set *dummy;
1902 struct isl_basic_set *convex_hull;
1904 dummy = isl_basic_set_remove_equalities(
1905 isl_basic_set_copy(affine_hull), &T, &T2);
1906 if (!dummy)
1907 goto error;
1908 isl_basic_set_free(dummy);
1909 set = isl_set_preimage(set, T);
1910 convex_hull = uset_convex_hull(set);
1911 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1912 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1913 return convex_hull;
1914 error:
1915 isl_basic_set_free(affine_hull);
1916 isl_set_free(set);
1917 return NULL;
1920 /* Return an empty basic map living in the same space as "map".
1922 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1923 __isl_take isl_map *map)
1925 isl_space *space;
1927 space = isl_map_get_space(map);
1928 isl_map_free(map);
1929 return isl_basic_map_empty(space);
1932 /* Compute the convex hull of a map.
1934 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1935 * specifically, the wrapping of facets to obtain new facets.
1937 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1939 struct isl_basic_set *bset;
1940 struct isl_basic_map *model = NULL;
1941 struct isl_basic_set *affine_hull = NULL;
1942 struct isl_basic_map *convex_hull = NULL;
1943 struct isl_set *set = NULL;
1945 map = isl_map_detect_equalities(map);
1946 map = isl_map_align_divs(map);
1947 if (!map)
1948 goto error;
1950 if (map->n == 0)
1951 return replace_map_by_empty_basic_map(map);
1953 model = isl_basic_map_copy(map->p[0]);
1954 set = isl_map_underlying_set(map);
1955 if (!set)
1956 goto error;
1958 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1959 if (!affine_hull)
1960 goto error;
1961 if (affine_hull->n_eq != 0)
1962 bset = modulo_affine_hull(set, affine_hull);
1963 else {
1964 isl_basic_set_free(affine_hull);
1965 bset = uset_convex_hull(set);
1968 convex_hull = isl_basic_map_overlying_set(bset, model);
1969 if (!convex_hull)
1970 return NULL;
1972 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1973 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1974 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1975 return convex_hull;
1976 error:
1977 isl_set_free(set);
1978 isl_basic_map_free(model);
1979 return NULL;
1982 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1984 return (struct isl_basic_set *)
1985 isl_map_convex_hull((struct isl_map *)set);
1988 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1990 isl_basic_map *hull;
1992 hull = isl_map_convex_hull(map);
1993 return isl_basic_map_remove_divs(hull);
1996 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1998 return (isl_basic_set *)isl_map_polyhedral_hull((isl_map *)set);
2001 struct sh_data_entry {
2002 struct isl_hash_table *table;
2003 struct isl_tab *tab;
2006 /* Holds the data needed during the simple hull computation.
2007 * In particular,
2008 * n the number of basic sets in the original set
2009 * hull_table a hash table of already computed constraints
2010 * in the simple hull
2011 * p for each basic set,
2012 * table a hash table of the constraints
2013 * tab the tableau corresponding to the basic set
2015 struct sh_data {
2016 struct isl_ctx *ctx;
2017 unsigned n;
2018 struct isl_hash_table *hull_table;
2019 struct sh_data_entry p[1];
2022 static void sh_data_free(struct sh_data *data)
2024 int i;
2026 if (!data)
2027 return;
2028 isl_hash_table_free(data->ctx, data->hull_table);
2029 for (i = 0; i < data->n; ++i) {
2030 isl_hash_table_free(data->ctx, data->p[i].table);
2031 isl_tab_free(data->p[i].tab);
2033 free(data);
2036 struct ineq_cmp_data {
2037 unsigned len;
2038 isl_int *p;
2041 static int has_ineq(const void *entry, const void *val)
2043 isl_int *row = (isl_int *)entry;
2044 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2046 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2047 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2050 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2051 isl_int *ineq, unsigned len)
2053 uint32_t c_hash;
2054 struct ineq_cmp_data v;
2055 struct isl_hash_table_entry *entry;
2057 v.len = len;
2058 v.p = ineq;
2059 c_hash = isl_seq_get_hash(ineq + 1, len);
2060 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2061 if (!entry)
2062 return - 1;
2063 entry->data = ineq;
2064 return 0;
2067 /* Fill hash table "table" with the constraints of "bset".
2068 * Equalities are added as two inequalities.
2069 * The value in the hash table is a pointer to the (in)equality of "bset".
2071 static int hash_basic_set(struct isl_hash_table *table,
2072 struct isl_basic_set *bset)
2074 int i, j;
2075 unsigned dim = isl_basic_set_total_dim(bset);
2077 for (i = 0; i < bset->n_eq; ++i) {
2078 for (j = 0; j < 2; ++j) {
2079 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2080 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2081 return -1;
2084 for (i = 0; i < bset->n_ineq; ++i) {
2085 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2086 return -1;
2088 return 0;
2091 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2093 struct sh_data *data;
2094 int i;
2096 data = isl_calloc(set->ctx, struct sh_data,
2097 sizeof(struct sh_data) +
2098 (set->n - 1) * sizeof(struct sh_data_entry));
2099 if (!data)
2100 return NULL;
2101 data->ctx = set->ctx;
2102 data->n = set->n;
2103 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2104 if (!data->hull_table)
2105 goto error;
2106 for (i = 0; i < set->n; ++i) {
2107 data->p[i].table = isl_hash_table_alloc(set->ctx,
2108 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2109 if (!data->p[i].table)
2110 goto error;
2111 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2112 goto error;
2114 return data;
2115 error:
2116 sh_data_free(data);
2117 return NULL;
2120 /* Check if inequality "ineq" is a bound for basic set "j" or if
2121 * it can be relaxed (by increasing the constant term) to become
2122 * a bound for that basic set. In the latter case, the constant
2123 * term is updated.
2124 * Relaxation of the constant term is only allowed if "shift" is set.
2126 * Return 1 if "ineq" is a bound
2127 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2128 * -1 if some error occurred
2130 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2131 isl_int *ineq, int shift)
2133 enum isl_lp_result res;
2134 isl_int opt;
2136 if (!data->p[j].tab) {
2137 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2138 if (!data->p[j].tab)
2139 return -1;
2142 isl_int_init(opt);
2144 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2145 &opt, NULL, 0);
2146 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2147 if (shift)
2148 isl_int_sub(ineq[0], ineq[0], opt);
2149 else
2150 res = isl_lp_unbounded;
2153 isl_int_clear(opt);
2155 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2156 res == isl_lp_unbounded ? 0 : -1;
2159 /* Set the constant term of "ineq" to the maximum of those of the constraints
2160 * in the basic sets of "set" following "i" that are parallel to "ineq".
2161 * That is, if any of the basic sets of "set" following "i" have a more
2162 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2163 * "c_hash" is the hash value of the linear part of "ineq".
2164 * "v" has been set up for use by has_ineq.
2166 * Note that the two inequality constraints corresponding to an equality are
2167 * represented by the same inequality constraint in data->p[j].table
2168 * (but with different hash values). This means the constraint (or at
2169 * least its constant term) may need to be temporarily negated to get
2170 * the actually hashed constraint.
2172 static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
2173 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2175 int j;
2176 isl_ctx *ctx;
2177 struct isl_hash_table_entry *entry;
2179 ctx = isl_set_get_ctx(set);
2180 for (j = i + 1; j < set->n; ++j) {
2181 int neg;
2182 isl_int *ineq_j;
2184 entry = isl_hash_table_find(ctx, data->p[j].table,
2185 c_hash, &has_ineq, v, 0);
2186 if (!entry)
2187 continue;
2189 ineq_j = entry->data;
2190 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2191 if (neg)
2192 isl_int_neg(ineq_j[0], ineq_j[0]);
2193 if (isl_int_gt(ineq_j[0], ineq[0]))
2194 isl_int_set(ineq[0], ineq_j[0]);
2195 if (neg)
2196 isl_int_neg(ineq_j[0], ineq_j[0]);
2200 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2201 * become a bound on the whole set. If so, add the (relaxed) inequality
2202 * to "hull". Relaxation is only allowed if "shift" is set.
2204 * We first check if "hull" already contains a translate of the inequality.
2205 * If so, we are done.
2206 * Then, we check if any of the previous basic sets contains a translate
2207 * of the inequality. If so, then we have already considered this
2208 * inequality and we are done.
2209 * Otherwise, for each basic set other than "i", we check if the inequality
2210 * is a bound on the basic set, but first replace the constant term
2211 * by the maximal value of any translate of the inequality in any
2212 * of the following basic sets.
2213 * For previous basic sets, we know that they do not contain a translate
2214 * of the inequality, so we directly call is_bound.
2215 * For following basic sets, we first check if a translate of the
2216 * inequality appears in its description. If so, the constant term
2217 * of the inequality has already been updated with respect to this
2218 * translate and the inequality is therefore known to be a bound
2219 * of this basic set.
2221 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2222 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
2223 int shift)
2225 uint32_t c_hash;
2226 struct ineq_cmp_data v;
2227 struct isl_hash_table_entry *entry;
2228 int j, k;
2230 if (!hull)
2231 return NULL;
2233 v.len = isl_basic_set_total_dim(hull);
2234 v.p = ineq;
2235 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2237 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2238 has_ineq, &v, 0);
2239 if (entry)
2240 return hull;
2242 for (j = 0; j < i; ++j) {
2243 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2244 c_hash, has_ineq, &v, 0);
2245 if (entry)
2246 break;
2248 if (j < i)
2249 return hull;
2251 k = isl_basic_set_alloc_inequality(hull);
2252 if (k < 0)
2253 goto error;
2254 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2256 set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
2257 for (j = 0; j < i; ++j) {
2258 int bound;
2259 bound = is_bound(data, set, j, hull->ineq[k], shift);
2260 if (bound < 0)
2261 goto error;
2262 if (!bound)
2263 break;
2265 if (j < i) {
2266 isl_basic_set_free_inequality(hull, 1);
2267 return hull;
2270 for (j = i + 1; j < set->n; ++j) {
2271 int bound;
2272 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2273 c_hash, has_ineq, &v, 0);
2274 if (entry)
2275 continue;
2276 bound = is_bound(data, set, j, hull->ineq[k], shift);
2277 if (bound < 0)
2278 goto error;
2279 if (!bound)
2280 break;
2282 if (j < set->n) {
2283 isl_basic_set_free_inequality(hull, 1);
2284 return hull;
2287 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2288 has_ineq, &v, 1);
2289 if (!entry)
2290 goto error;
2291 entry->data = hull->ineq[k];
2293 return hull;
2294 error:
2295 isl_basic_set_free(hull);
2296 return NULL;
2299 /* Check if any inequality from basic set "i" is or can be relaxed to
2300 * become a bound on the whole set. If so, add the (relaxed) inequality
2301 * to "hull". Relaxation is only allowed if "shift" is set.
2303 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2304 struct sh_data *data, struct isl_set *set, int i, int shift)
2306 int j, k;
2307 unsigned dim = isl_basic_set_total_dim(bset);
2309 for (j = 0; j < set->p[i]->n_eq; ++j) {
2310 for (k = 0; k < 2; ++k) {
2311 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2312 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2313 shift);
2316 for (j = 0; j < set->p[i]->n_ineq; ++j)
2317 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2318 return bset;
2321 /* Compute a superset of the convex hull of set that is described
2322 * by only (translates of) the constraints in the constituents of set.
2323 * Translation is only allowed if "shift" is set.
2325 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2326 int shift)
2328 struct sh_data *data = NULL;
2329 struct isl_basic_set *hull = NULL;
2330 unsigned n_ineq;
2331 int i;
2333 if (!set)
2334 return NULL;
2336 n_ineq = 0;
2337 for (i = 0; i < set->n; ++i) {
2338 if (!set->p[i])
2339 goto error;
2340 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2343 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2344 if (!hull)
2345 goto error;
2347 data = sh_data_alloc(set, n_ineq);
2348 if (!data)
2349 goto error;
2351 for (i = 0; i < set->n; ++i)
2352 hull = add_bounds(hull, data, set, i, shift);
2354 sh_data_free(data);
2355 isl_set_free(set);
2357 return hull;
2358 error:
2359 sh_data_free(data);
2360 isl_basic_set_free(hull);
2361 isl_set_free(set);
2362 return NULL;
2365 /* Compute a superset of the convex hull of map that is described
2366 * by only (translates of) the constraints in the constituents of map.
2367 * Handle trivial cases where map is NULL or contains at most one disjunct.
2369 static __isl_give isl_basic_map *map_simple_hull_trivial(
2370 __isl_take isl_map *map)
2372 isl_basic_map *hull;
2374 if (!map)
2375 return NULL;
2376 if (map->n == 0)
2377 return replace_map_by_empty_basic_map(map);
2379 hull = isl_basic_map_copy(map->p[0]);
2380 isl_map_free(map);
2381 return hull;
2384 /* Compute a superset of the convex hull of map that is described
2385 * by only (translates of) the constraints in the constituents of map.
2386 * Translation is only allowed if "shift" is set.
2388 * Sort the constraints before removing redundant constraints
2389 * in order to indicate a preference of which constraints should
2390 * be preserved. In particular, pairs of constraints that are
2391 * sorted together are preferred to either both be preserved
2392 * or both be removed.
2394 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2395 int shift)
2397 struct isl_set *set = NULL;
2398 struct isl_basic_map *model = NULL;
2399 struct isl_basic_map *hull;
2400 struct isl_basic_map *affine_hull;
2401 struct isl_basic_set *bset = NULL;
2403 if (!map || map->n <= 1)
2404 return map_simple_hull_trivial(map);
2406 map = isl_map_detect_equalities(map);
2407 if (!map || map->n <= 1)
2408 return map_simple_hull_trivial(map);
2409 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2410 map = isl_map_align_divs(map);
2411 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2413 set = isl_map_underlying_set(map);
2415 bset = uset_simple_hull(set, shift);
2417 hull = isl_basic_map_overlying_set(bset, model);
2419 hull = isl_basic_map_intersect(hull, affine_hull);
2420 hull = isl_basic_map_sort_constraints(hull);
2421 hull = isl_basic_map_remove_redundancies(hull);
2423 if (!hull)
2424 return NULL;
2425 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2426 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2428 hull = isl_basic_map_finalize(hull);
2430 return hull;
2433 /* Compute a superset of the convex hull of map that is described
2434 * by only translates of the constraints in the constituents of map.
2436 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2438 return map_simple_hull(map, 1);
2441 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2443 return (struct isl_basic_set *)
2444 isl_map_simple_hull((struct isl_map *)set);
2447 /* Compute a superset of the convex hull of map that is described
2448 * by only the constraints in the constituents of map.
2450 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2451 __isl_take isl_map *map)
2453 return map_simple_hull(map, 0);
2456 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2457 __isl_take isl_set *set)
2459 return isl_map_unshifted_simple_hull(set);
2462 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2463 * A constraint that appears with different constant terms
2464 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2465 * (i.e., greatest) constant term.
2466 * "bmap1" and "bmap2" are assumed to have the same (known)
2467 * integer divisions.
2468 * The constraints of both "bmap1" and "bmap2" are assumed
2469 * to have been sorted using isl_basic_map_sort_constraints.
2471 * Run through the inequality constraints of "bmap1" and "bmap2"
2472 * in sorted order.
2473 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2474 * is removed.
2475 * If a match is found, the constraint is kept. If needed, the constant
2476 * term of the constraint is adjusted.
2478 static __isl_give isl_basic_map *select_shared_inequalities(
2479 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2481 int i1, i2;
2483 bmap1 = isl_basic_map_cow(bmap1);
2484 if (!bmap1 || !bmap2)
2485 return isl_basic_map_free(bmap1);
2487 i1 = bmap1->n_ineq - 1;
2488 i2 = bmap2->n_ineq - 1;
2489 while (bmap1 && i1 >= 0 && i2 >= 0) {
2490 int cmp;
2492 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2493 bmap2->ineq[i2]);
2494 if (cmp < 0) {
2495 --i2;
2496 continue;
2498 if (cmp > 0) {
2499 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2500 bmap1 = isl_basic_map_free(bmap1);
2501 --i1;
2502 continue;
2504 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2505 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2506 --i1;
2507 --i2;
2509 for (; i1 >= 0; --i1)
2510 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2511 bmap1 = isl_basic_map_free(bmap1);
2513 return bmap1;
2516 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2517 * "bmap1" and "bmap2" are assumed to have the same (known)
2518 * integer divisions.
2520 * Run through the equality constraints of "bmap1" and "bmap2".
2521 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2522 * is removed.
2524 static __isl_give isl_basic_map *select_shared_equalities(
2525 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2527 int i1, i2;
2528 unsigned total;
2530 bmap1 = isl_basic_map_cow(bmap1);
2531 if (!bmap1 || !bmap2)
2532 return isl_basic_map_free(bmap1);
2534 total = isl_basic_map_total_dim(bmap1);
2536 i1 = bmap1->n_eq - 1;
2537 i2 = bmap2->n_eq - 1;
2538 while (bmap1 && i1 >= 0 && i2 >= 0) {
2539 int last1, last2;
2541 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2542 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2543 if (last1 > last2) {
2544 --i2;
2545 continue;
2547 if (last1 < last2) {
2548 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2549 bmap1 = isl_basic_map_free(bmap1);
2550 --i1;
2551 continue;
2553 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2554 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2555 bmap1 = isl_basic_map_free(bmap1);
2557 --i1;
2558 --i2;
2560 for (; i1 >= 0; --i1)
2561 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2562 bmap1 = isl_basic_map_free(bmap1);
2564 return bmap1;
2567 /* Compute a superset of "bmap1" and "bmap2" that is described
2568 * by only the constraints that appear in both "bmap1" and "bmap2".
2570 * First drop constraints that involve unknown integer divisions
2571 * since it is not trivial to check whether two such integer divisions
2572 * in different basic maps are the same.
2573 * Then align the remaining (known) divs and sort the constraints.
2574 * Finally drop all inequalities and equalities from "bmap1" that
2575 * do not also appear in "bmap2".
2577 __isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2578 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2580 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2581 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2582 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2583 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2584 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2585 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2586 bmap1 = isl_basic_map_sort_constraints(bmap1);
2587 bmap2 = isl_basic_map_sort_constraints(bmap2);
2589 bmap1 = select_shared_inequalities(bmap1, bmap2);
2590 bmap1 = select_shared_equalities(bmap1, bmap2);
2592 isl_basic_map_free(bmap2);
2593 bmap1 = isl_basic_map_finalize(bmap1);
2594 return bmap1;
2597 /* Compute a superset of the convex hull of "map" that is described
2598 * by only the constraints in the constituents of "map".
2599 * In particular, the result is composed of constraints that appear
2600 * in each of the basic maps of "map"
2602 * Constraints that involve unknown integer divisions are dropped
2603 * since it is not trivial to check whether two such integer divisions
2604 * in different basic maps are the same.
2606 * The hull is initialized from the first basic map and then
2607 * updated with respect to the other basic maps in turn.
2609 __isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2610 __isl_take isl_map *map)
2612 int i;
2613 isl_basic_map *hull;
2615 if (!map)
2616 return NULL;
2617 if (map->n <= 1)
2618 return map_simple_hull_trivial(map);
2619 map = isl_map_drop_constraint_involving_unknown_divs(map);
2620 hull = isl_basic_map_copy(map->p[0]);
2621 for (i = 1; i < map->n; ++i) {
2622 isl_basic_map *bmap_i;
2624 bmap_i = isl_basic_map_copy(map->p[i]);
2625 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2628 isl_map_free(map);
2629 return hull;
2632 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2634 * For each basic set in "set", we first check if the basic set
2635 * contains a translate of "ineq". If this translate is more relaxed,
2636 * then we assume that "ineq" is not a bound on this basic set.
2637 * Otherwise, we know that it is a bound.
2638 * If the basic set does not contain a translate of "ineq", then
2639 * we call is_bound to perform the test.
2641 static __isl_give isl_basic_set *add_bound_from_constraint(
2642 __isl_take isl_basic_set *hull, struct sh_data *data,
2643 __isl_keep isl_set *set, isl_int *ineq)
2645 int i, k;
2646 isl_ctx *ctx;
2647 uint32_t c_hash;
2648 struct ineq_cmp_data v;
2650 if (!hull || !set)
2651 return isl_basic_set_free(hull);
2653 v.len = isl_basic_set_total_dim(hull);
2654 v.p = ineq;
2655 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2657 ctx = isl_basic_set_get_ctx(hull);
2658 for (i = 0; i < set->n; ++i) {
2659 int bound;
2660 struct isl_hash_table_entry *entry;
2662 entry = isl_hash_table_find(ctx, data->p[i].table,
2663 c_hash, &has_ineq, &v, 0);
2664 if (entry) {
2665 isl_int *ineq_i = entry->data;
2666 int neg, more_relaxed;
2668 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2669 if (neg)
2670 isl_int_neg(ineq_i[0], ineq_i[0]);
2671 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2672 if (neg)
2673 isl_int_neg(ineq_i[0], ineq_i[0]);
2674 if (more_relaxed)
2675 break;
2676 else
2677 continue;
2679 bound = is_bound(data, set, i, ineq, 0);
2680 if (bound < 0)
2681 return isl_basic_set_free(hull);
2682 if (!bound)
2683 break;
2685 if (i < set->n)
2686 return hull;
2688 k = isl_basic_set_alloc_inequality(hull);
2689 if (k < 0)
2690 return isl_basic_set_free(hull);
2691 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2693 return hull;
2696 /* Compute a superset of the convex hull of "set" that is described
2697 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2698 * has no parameters or integer divisions.
2700 * The inequalities in "ineq" are assumed to have been sorted such
2701 * that constraints with the same linear part appear together and
2702 * that among constraints with the same linear part, those with
2703 * smaller constant term appear first.
2705 * We reuse the same data structure that is used by uset_simple_hull,
2706 * but we do not need the hull table since we will not consider the
2707 * same constraint more than once. We therefore allocate it with zero size.
2709 * We run through the constraints and try to add them one by one,
2710 * skipping identical constraints. If we have added a constraint and
2711 * the next constraint is a more relaxed translate, then we skip this
2712 * next constraint as well.
2714 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2715 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2717 int i;
2718 int last_added = 0;
2719 struct sh_data *data = NULL;
2720 isl_basic_set *hull = NULL;
2721 unsigned dim;
2723 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2724 if (!hull)
2725 goto error;
2727 data = sh_data_alloc(set, 0);
2728 if (!data)
2729 goto error;
2731 dim = isl_set_dim(set, isl_dim_set);
2732 for (i = 0; i < n_ineq; ++i) {
2733 int hull_n_ineq = hull->n_ineq;
2734 int parallel;
2736 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2737 dim);
2738 if (parallel &&
2739 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2740 continue;
2741 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2742 if (!hull)
2743 goto error;
2744 last_added = hull->n_ineq > hull_n_ineq;
2747 sh_data_free(data);
2748 isl_set_free(set);
2749 return hull;
2750 error:
2751 sh_data_free(data);
2752 isl_set_free(set);
2753 isl_basic_set_free(hull);
2754 return NULL;
2757 /* Collect pointers to all the inequalities in the elements of "list"
2758 * in "ineq". For equalities, store both a pointer to the equality and
2759 * a pointer to its opposite, which is first copied to "mat".
2760 * "ineq" and "mat" are assumed to have been preallocated to the right size
2761 * (the number of inequalities + 2 times the number of equalites and
2762 * the number of equalities, respectively).
2764 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2765 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2767 int i, j, n, n_eq, n_ineq;
2769 if (!mat)
2770 return NULL;
2772 n_eq = 0;
2773 n_ineq = 0;
2774 n = isl_basic_set_list_n_basic_set(list);
2775 for (i = 0; i < n; ++i) {
2776 isl_basic_set *bset;
2777 bset = isl_basic_set_list_get_basic_set(list, i);
2778 if (!bset)
2779 return isl_mat_free(mat);
2780 for (j = 0; j < bset->n_eq; ++j) {
2781 ineq[n_ineq++] = mat->row[n_eq];
2782 ineq[n_ineq++] = bset->eq[j];
2783 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2785 for (j = 0; j < bset->n_ineq; ++j)
2786 ineq[n_ineq++] = bset->ineq[j];
2787 isl_basic_set_free(bset);
2790 return mat;
2793 /* Comparison routine for use as an isl_sort callback.
2795 * Constraints with the same linear part are sorted together and
2796 * among constraints with the same linear part, those with smaller
2797 * constant term are sorted first.
2799 static int cmp_ineq(const void *a, const void *b, void *arg)
2801 unsigned dim = *(unsigned *) arg;
2802 isl_int * const *ineq1 = a;
2803 isl_int * const *ineq2 = b;
2804 int cmp;
2806 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2807 if (cmp != 0)
2808 return cmp;
2809 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2812 /* Compute a superset of the convex hull of "set" that is described
2813 * by only constraints in the elements of "list", where "set" has
2814 * no parameters or integer divisions.
2816 * We collect all the constraints in those elements and then
2817 * sort the constraints such that constraints with the same linear part
2818 * are sorted together and that those with smaller constant term are
2819 * sorted first.
2821 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2822 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2824 int i, n, n_eq, n_ineq;
2825 unsigned dim;
2826 isl_ctx *ctx;
2827 isl_mat *mat = NULL;
2828 isl_int **ineq = NULL;
2829 isl_basic_set *hull;
2831 if (!set)
2832 goto error;
2833 ctx = isl_set_get_ctx(set);
2835 n_eq = 0;
2836 n_ineq = 0;
2837 n = isl_basic_set_list_n_basic_set(list);
2838 for (i = 0; i < n; ++i) {
2839 isl_basic_set *bset;
2840 bset = isl_basic_set_list_get_basic_set(list, i);
2841 if (!bset)
2842 goto error;
2843 n_eq += bset->n_eq;
2844 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2845 isl_basic_set_free(bset);
2848 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2849 if (n_ineq > 0 && !ineq)
2850 goto error;
2852 dim = isl_set_dim(set, isl_dim_set);
2853 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2854 mat = collect_inequalities(mat, list, ineq);
2855 if (!mat)
2856 goto error;
2858 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2859 goto error;
2861 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2863 isl_mat_free(mat);
2864 free(ineq);
2865 isl_basic_set_list_free(list);
2866 return hull;
2867 error:
2868 isl_mat_free(mat);
2869 free(ineq);
2870 isl_set_free(set);
2871 isl_basic_set_list_free(list);
2872 return NULL;
2875 /* Compute a superset of the convex hull of "map" that is described
2876 * by only constraints in the elements of "list".
2878 * If the list is empty, then we can only describe the universe set.
2879 * If the input map is empty, then all constraints are valid, so
2880 * we return the intersection of the elements in "list".
2882 * Otherwise, we align all divs and temporarily treat them
2883 * as regular variables, computing the unshifted simple hull in
2884 * uset_unshifted_simple_hull_from_basic_set_list.
2886 static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2887 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2889 isl_basic_map *model;
2890 isl_basic_map *hull;
2891 isl_set *set;
2892 isl_basic_set_list *bset_list;
2894 if (!map || !list)
2895 goto error;
2897 if (isl_basic_map_list_n_basic_map(list) == 0) {
2898 isl_space *space;
2900 space = isl_map_get_space(map);
2901 isl_map_free(map);
2902 isl_basic_map_list_free(list);
2903 return isl_basic_map_universe(space);
2905 if (isl_map_plain_is_empty(map)) {
2906 isl_map_free(map);
2907 return isl_basic_map_list_intersect(list);
2910 map = isl_map_align_divs_to_basic_map_list(map, list);
2911 if (!map)
2912 goto error;
2913 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2915 model = isl_basic_map_list_get_basic_map(list, 0);
2917 set = isl_map_underlying_set(map);
2918 bset_list = isl_basic_map_list_underlying_set(list);
2920 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2921 hull = isl_basic_map_overlying_set(hull, model);
2923 return hull;
2924 error:
2925 isl_map_free(map);
2926 isl_basic_map_list_free(list);
2927 return NULL;
2930 /* Return a sequence of the basic maps that make up the maps in "list".
2932 static __isl_give isl_basic_set_list *collect_basic_maps(
2933 __isl_take isl_map_list *list)
2935 int i, n;
2936 isl_ctx *ctx;
2937 isl_basic_map_list *bmap_list;
2939 if (!list)
2940 return NULL;
2941 n = isl_map_list_n_map(list);
2942 ctx = isl_map_list_get_ctx(list);
2943 bmap_list = isl_basic_map_list_alloc(ctx, 0);
2945 for (i = 0; i < n; ++i) {
2946 isl_map *map;
2947 isl_basic_map_list *list_i;
2949 map = isl_map_list_get_map(list, i);
2950 map = isl_map_compute_divs(map);
2951 list_i = isl_map_get_basic_map_list(map);
2952 isl_map_free(map);
2953 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
2956 isl_map_list_free(list);
2957 return bmap_list;
2960 /* Compute a superset of the convex hull of "map" that is described
2961 * by only constraints in the elements of "list".
2963 * If "map" is the universe, then the convex hull (and therefore
2964 * any superset of the convexhull) is the universe as well.
2966 * Otherwise, we collect all the basic maps in the map list and
2967 * continue with map_unshifted_simple_hull_from_basic_map_list.
2969 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
2970 __isl_take isl_map *map, __isl_take isl_map_list *list)
2972 isl_basic_map_list *bmap_list;
2973 int is_universe;
2975 is_universe = isl_map_plain_is_universe(map);
2976 if (is_universe < 0)
2977 map = isl_map_free(map);
2978 if (is_universe < 0 || is_universe) {
2979 isl_map_list_free(list);
2980 return isl_map_unshifted_simple_hull(map);
2983 bmap_list = collect_basic_maps(list);
2984 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
2987 /* Compute a superset of the convex hull of "set" that is described
2988 * by only constraints in the elements of "list".
2990 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
2991 __isl_take isl_set *set, __isl_take isl_set_list *list)
2993 return isl_map_unshifted_simple_hull_from_map_list(set, list);
2996 /* Given a set "set", return parametric bounds on the dimension "dim".
2998 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
3000 unsigned set_dim = isl_set_dim(set, isl_dim_set);
3001 set = isl_set_copy(set);
3002 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
3003 set = isl_set_eliminate_dims(set, 0, dim);
3004 return isl_set_convex_hull(set);
3007 /* Computes a "simple hull" and then check if each dimension in the
3008 * resulting hull is bounded by a symbolic constant. If not, the
3009 * hull is intersected with the corresponding bounds on the whole set.
3011 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
3013 int i, j;
3014 struct isl_basic_set *hull;
3015 unsigned nparam, left;
3016 int removed_divs = 0;
3018 hull = isl_set_simple_hull(isl_set_copy(set));
3019 if (!hull)
3020 goto error;
3022 nparam = isl_basic_set_dim(hull, isl_dim_param);
3023 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
3024 int lower = 0, upper = 0;
3025 struct isl_basic_set *bounds;
3027 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
3028 for (j = 0; j < hull->n_eq; ++j) {
3029 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3030 continue;
3031 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3032 left) == -1)
3033 break;
3035 if (j < hull->n_eq)
3036 continue;
3038 for (j = 0; j < hull->n_ineq; ++j) {
3039 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3040 continue;
3041 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3042 left) != -1 ||
3043 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3044 i) != -1)
3045 continue;
3046 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3047 lower = 1;
3048 else
3049 upper = 1;
3050 if (lower && upper)
3051 break;
3054 if (lower && upper)
3055 continue;
3057 if (!removed_divs) {
3058 set = isl_set_remove_divs(set);
3059 if (!set)
3060 goto error;
3061 removed_divs = 1;
3063 bounds = set_bounds(set, i);
3064 hull = isl_basic_set_intersect(hull, bounds);
3065 if (!hull)
3066 goto error;
3069 isl_set_free(set);
3070 return hull;
3071 error:
3072 isl_set_free(set);
3073 return NULL;