isl_map_simplify.c: drop_more_redundant_divs: use isl_basic_map_dim
[isl.git] / isl_convex_hull.c
blobc7ebd5edfcef6239b02703f9fd99688a6c4a54a0
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 then 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 set = isl_set_add_basic_set(set, convex_hull);
1529 return modulo_lineality(set, t);
1531 isl_basic_set_free(t);
1533 isl_set_free(set);
1534 return convex_hull;
1535 error:
1536 isl_set_free(set);
1537 isl_basic_set_free(convex_hull);
1538 return NULL;
1541 /* Compute an initial hull for wrapping containing a single initial
1542 * facet.
1543 * This function assumes that the given set is bounded.
1545 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1546 struct isl_set *set)
1548 struct isl_mat *bounds = NULL;
1549 unsigned dim;
1550 int k;
1552 if (!hull)
1553 goto error;
1554 bounds = initial_facet_constraint(set);
1555 if (!bounds)
1556 goto error;
1557 k = isl_basic_set_alloc_inequality(hull);
1558 if (k < 0)
1559 goto error;
1560 dim = isl_set_n_dim(set);
1561 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1562 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1563 isl_mat_free(bounds);
1565 return hull;
1566 error:
1567 isl_basic_set_free(hull);
1568 isl_mat_free(bounds);
1569 return NULL;
1572 struct max_constraint {
1573 struct isl_mat *c;
1574 int count;
1575 int ineq;
1578 static int max_constraint_equal(const void *entry, const void *val)
1580 struct max_constraint *a = (struct max_constraint *)entry;
1581 isl_int *b = (isl_int *)val;
1583 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1586 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1587 isl_int *con, unsigned len, int n, int ineq)
1589 struct isl_hash_table_entry *entry;
1590 struct max_constraint *c;
1591 uint32_t c_hash;
1593 c_hash = isl_seq_get_hash(con + 1, len);
1594 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1595 con + 1, 0);
1596 if (!entry)
1597 return;
1598 c = entry->data;
1599 if (c->count < n) {
1600 isl_hash_table_remove(ctx, table, entry);
1601 return;
1603 c->count++;
1604 if (isl_int_gt(c->c->row[0][0], con[0]))
1605 return;
1606 if (isl_int_eq(c->c->row[0][0], con[0])) {
1607 if (ineq)
1608 c->ineq = ineq;
1609 return;
1611 c->c = isl_mat_cow(c->c);
1612 isl_int_set(c->c->row[0][0], con[0]);
1613 c->ineq = ineq;
1616 /* Check whether the constraint hash table "table" constains the constraint
1617 * "con".
1619 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1620 isl_int *con, unsigned len, int n)
1622 struct isl_hash_table_entry *entry;
1623 struct max_constraint *c;
1624 uint32_t c_hash;
1626 c_hash = isl_seq_get_hash(con + 1, len);
1627 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1628 con + 1, 0);
1629 if (!entry)
1630 return 0;
1631 c = entry->data;
1632 if (c->count < n)
1633 return 0;
1634 return isl_int_eq(c->c->row[0][0], con[0]);
1637 /* Check for inequality constraints of a basic set without equalities
1638 * such that the same or more stringent copies of the constraint appear
1639 * in all of the basic sets. Such constraints are necessarily facet
1640 * constraints of the convex hull.
1642 * If the resulting basic set is by chance identical to one of
1643 * the basic sets in "set", then we know that this basic set contains
1644 * all other basic sets and is therefore the convex hull of set.
1645 * In this case we set *is_hull to 1.
1647 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1648 struct isl_set *set, int *is_hull)
1650 int i, j, s, n;
1651 int min_constraints;
1652 int best;
1653 struct max_constraint *constraints = NULL;
1654 struct isl_hash_table *table = NULL;
1655 unsigned total;
1657 *is_hull = 0;
1659 for (i = 0; i < set->n; ++i)
1660 if (set->p[i]->n_eq == 0)
1661 break;
1662 if (i >= set->n)
1663 return hull;
1664 min_constraints = set->p[i]->n_ineq;
1665 best = i;
1666 for (i = best + 1; i < set->n; ++i) {
1667 if (set->p[i]->n_eq != 0)
1668 continue;
1669 if (set->p[i]->n_ineq >= min_constraints)
1670 continue;
1671 min_constraints = set->p[i]->n_ineq;
1672 best = i;
1674 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1675 min_constraints);
1676 if (!constraints)
1677 return hull;
1678 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1679 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1680 goto error;
1682 total = isl_space_dim(set->dim, isl_dim_all);
1683 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1684 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1685 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1686 if (!constraints[i].c)
1687 goto error;
1688 constraints[i].ineq = 1;
1690 for (i = 0; i < min_constraints; ++i) {
1691 struct isl_hash_table_entry *entry;
1692 uint32_t c_hash;
1693 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1694 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1695 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1696 if (!entry)
1697 goto error;
1698 isl_assert(hull->ctx, !entry->data, goto error);
1699 entry->data = &constraints[i];
1702 n = 0;
1703 for (s = 0; s < set->n; ++s) {
1704 if (s == best)
1705 continue;
1707 for (i = 0; i < set->p[s]->n_eq; ++i) {
1708 isl_int *eq = set->p[s]->eq[i];
1709 for (j = 0; j < 2; ++j) {
1710 isl_seq_neg(eq, eq, 1 + total);
1711 update_constraint(hull->ctx, table,
1712 eq, total, n, 0);
1715 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1716 isl_int *ineq = set->p[s]->ineq[i];
1717 update_constraint(hull->ctx, table, ineq, total, n,
1718 set->p[s]->n_eq == 0);
1720 ++n;
1723 for (i = 0; i < min_constraints; ++i) {
1724 if (constraints[i].count < n)
1725 continue;
1726 if (!constraints[i].ineq)
1727 continue;
1728 j = isl_basic_set_alloc_inequality(hull);
1729 if (j < 0)
1730 goto error;
1731 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1734 for (s = 0; s < set->n; ++s) {
1735 if (set->p[s]->n_eq)
1736 continue;
1737 if (set->p[s]->n_ineq != hull->n_ineq)
1738 continue;
1739 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1740 isl_int *ineq = set->p[s]->ineq[i];
1741 if (!has_constraint(hull->ctx, table, ineq, total, n))
1742 break;
1744 if (i == set->p[s]->n_ineq)
1745 *is_hull = 1;
1748 isl_hash_table_clear(table);
1749 for (i = 0; i < min_constraints; ++i)
1750 isl_mat_free(constraints[i].c);
1751 free(constraints);
1752 free(table);
1753 return hull;
1754 error:
1755 isl_hash_table_clear(table);
1756 free(table);
1757 if (constraints)
1758 for (i = 0; i < min_constraints; ++i)
1759 isl_mat_free(constraints[i].c);
1760 free(constraints);
1761 return hull;
1764 /* Create a template for the convex hull of "set" and fill it up
1765 * obvious facet constraints, if any. If the result happens to
1766 * be the convex hull of "set" then *is_hull is set to 1.
1768 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1770 struct isl_basic_set *hull;
1771 unsigned n_ineq;
1772 int i;
1774 n_ineq = 1;
1775 for (i = 0; i < set->n; ++i) {
1776 n_ineq += set->p[i]->n_eq;
1777 n_ineq += set->p[i]->n_ineq;
1779 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1780 hull = isl_basic_set_set_rational(hull);
1781 if (!hull)
1782 return NULL;
1783 return common_constraints(hull, set, is_hull);
1786 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1788 struct isl_basic_set *hull;
1789 int is_hull;
1791 hull = proto_hull(set, &is_hull);
1792 if (hull && !is_hull) {
1793 if (hull->n_ineq == 0)
1794 hull = initial_hull(hull, set);
1795 hull = extend(hull, set);
1797 isl_set_free(set);
1799 return hull;
1802 /* Compute the convex hull of a set without any parameters or
1803 * integer divisions. Depending on whether the set is bounded,
1804 * we pass control to the wrapping based convex hull or
1805 * the Fourier-Motzkin elimination based convex hull.
1806 * We also handle a few special cases before checking the boundedness.
1808 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1810 struct isl_basic_set *convex_hull = NULL;
1811 struct isl_basic_set *lin;
1813 if (isl_set_n_dim(set) == 0)
1814 return convex_hull_0d(set);
1816 set = isl_set_coalesce(set);
1817 set = isl_set_set_rational(set);
1819 if (!set)
1820 goto error;
1821 if (!set)
1822 return NULL;
1823 if (set->n == 1) {
1824 convex_hull = isl_basic_set_copy(set->p[0]);
1825 isl_set_free(set);
1826 return convex_hull;
1828 if (isl_set_n_dim(set) == 1)
1829 return convex_hull_1d(set);
1831 if (isl_set_is_bounded(set) &&
1832 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1833 return uset_convex_hull_wrap(set);
1835 lin = uset_combined_lineality_space(isl_set_copy(set));
1836 if (!lin)
1837 goto error;
1838 if (isl_basic_set_plain_is_universe(lin)) {
1839 isl_set_free(set);
1840 return lin;
1842 if (lin->n_eq < isl_basic_set_total_dim(lin))
1843 return modulo_lineality(set, lin);
1844 isl_basic_set_free(lin);
1846 return uset_convex_hull_unbounded(set);
1847 error:
1848 isl_set_free(set);
1849 isl_basic_set_free(convex_hull);
1850 return NULL;
1853 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1854 * without parameters or divs and where the convex hull of set is
1855 * known to be full-dimensional.
1857 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1859 struct isl_basic_set *convex_hull = NULL;
1861 if (!set)
1862 goto error;
1864 if (isl_set_n_dim(set) == 0) {
1865 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1866 isl_set_free(set);
1867 convex_hull = isl_basic_set_set_rational(convex_hull);
1868 return convex_hull;
1871 set = isl_set_set_rational(set);
1872 set = isl_set_coalesce(set);
1873 if (!set)
1874 goto error;
1875 if (set->n == 1) {
1876 convex_hull = isl_basic_set_copy(set->p[0]);
1877 isl_set_free(set);
1878 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1879 return convex_hull;
1881 if (isl_set_n_dim(set) == 1)
1882 return convex_hull_1d(set);
1884 return uset_convex_hull_wrap(set);
1885 error:
1886 isl_set_free(set);
1887 return NULL;
1890 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1891 * We first remove the equalities (transforming the set), compute the
1892 * convex hull of the transformed set and then add the equalities back
1893 * (after performing the inverse transformation.
1895 static struct isl_basic_set *modulo_affine_hull(
1896 struct isl_set *set, struct isl_basic_set *affine_hull)
1898 struct isl_mat *T;
1899 struct isl_mat *T2;
1900 struct isl_basic_set *dummy;
1901 struct isl_basic_set *convex_hull;
1903 dummy = isl_basic_set_remove_equalities(
1904 isl_basic_set_copy(affine_hull), &T, &T2);
1905 if (!dummy)
1906 goto error;
1907 isl_basic_set_free(dummy);
1908 set = isl_set_preimage(set, T);
1909 convex_hull = uset_convex_hull(set);
1910 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1911 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1912 return convex_hull;
1913 error:
1914 isl_basic_set_free(affine_hull);
1915 isl_set_free(set);
1916 return NULL;
1919 /* Return an empty basic map living in the same space as "map".
1921 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1922 __isl_take isl_map *map)
1924 isl_space *space;
1926 space = isl_map_get_space(map);
1927 isl_map_free(map);
1928 return isl_basic_map_empty(space);
1931 /* Compute the convex hull of a map.
1933 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1934 * specifically, the wrapping of facets to obtain new facets.
1936 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1938 struct isl_basic_set *bset;
1939 struct isl_basic_map *model = NULL;
1940 struct isl_basic_set *affine_hull = NULL;
1941 struct isl_basic_map *convex_hull = NULL;
1942 struct isl_set *set = NULL;
1944 map = isl_map_detect_equalities(map);
1945 map = isl_map_align_divs(map);
1946 if (!map)
1947 goto error;
1949 if (map->n == 0)
1950 return replace_map_by_empty_basic_map(map);
1952 model = isl_basic_map_copy(map->p[0]);
1953 set = isl_map_underlying_set(map);
1954 if (!set)
1955 goto error;
1957 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1958 if (!affine_hull)
1959 goto error;
1960 if (affine_hull->n_eq != 0)
1961 bset = modulo_affine_hull(set, affine_hull);
1962 else {
1963 isl_basic_set_free(affine_hull);
1964 bset = uset_convex_hull(set);
1967 convex_hull = isl_basic_map_overlying_set(bset, model);
1968 if (!convex_hull)
1969 return NULL;
1971 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1972 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1973 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1974 return convex_hull;
1975 error:
1976 isl_set_free(set);
1977 isl_basic_map_free(model);
1978 return NULL;
1981 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1983 return (struct isl_basic_set *)
1984 isl_map_convex_hull((struct isl_map *)set);
1987 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1989 isl_basic_map *hull;
1991 hull = isl_map_convex_hull(map);
1992 return isl_basic_map_remove_divs(hull);
1995 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1997 return (isl_basic_set *)isl_map_polyhedral_hull((isl_map *)set);
2000 struct sh_data_entry {
2001 struct isl_hash_table *table;
2002 struct isl_tab *tab;
2005 /* Holds the data needed during the simple hull computation.
2006 * In particular,
2007 * n the number of basic sets in the original set
2008 * hull_table a hash table of already computed constraints
2009 * in the simple hull
2010 * p for each basic set,
2011 * table a hash table of the constraints
2012 * tab the tableau corresponding to the basic set
2014 struct sh_data {
2015 struct isl_ctx *ctx;
2016 unsigned n;
2017 struct isl_hash_table *hull_table;
2018 struct sh_data_entry p[1];
2021 static void sh_data_free(struct sh_data *data)
2023 int i;
2025 if (!data)
2026 return;
2027 isl_hash_table_free(data->ctx, data->hull_table);
2028 for (i = 0; i < data->n; ++i) {
2029 isl_hash_table_free(data->ctx, data->p[i].table);
2030 isl_tab_free(data->p[i].tab);
2032 free(data);
2035 struct ineq_cmp_data {
2036 unsigned len;
2037 isl_int *p;
2040 static int has_ineq(const void *entry, const void *val)
2042 isl_int *row = (isl_int *)entry;
2043 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2045 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2046 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2049 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2050 isl_int *ineq, unsigned len)
2052 uint32_t c_hash;
2053 struct ineq_cmp_data v;
2054 struct isl_hash_table_entry *entry;
2056 v.len = len;
2057 v.p = ineq;
2058 c_hash = isl_seq_get_hash(ineq + 1, len);
2059 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2060 if (!entry)
2061 return - 1;
2062 entry->data = ineq;
2063 return 0;
2066 /* Fill hash table "table" with the constraints of "bset".
2067 * Equalities are added as two inequalities.
2068 * The value in the hash table is a pointer to the (in)equality of "bset".
2070 static int hash_basic_set(struct isl_hash_table *table,
2071 struct isl_basic_set *bset)
2073 int i, j;
2074 unsigned dim = isl_basic_set_total_dim(bset);
2076 for (i = 0; i < bset->n_eq; ++i) {
2077 for (j = 0; j < 2; ++j) {
2078 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2079 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2080 return -1;
2083 for (i = 0; i < bset->n_ineq; ++i) {
2084 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2085 return -1;
2087 return 0;
2090 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2092 struct sh_data *data;
2093 int i;
2095 data = isl_calloc(set->ctx, struct sh_data,
2096 sizeof(struct sh_data) +
2097 (set->n - 1) * sizeof(struct sh_data_entry));
2098 if (!data)
2099 return NULL;
2100 data->ctx = set->ctx;
2101 data->n = set->n;
2102 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2103 if (!data->hull_table)
2104 goto error;
2105 for (i = 0; i < set->n; ++i) {
2106 data->p[i].table = isl_hash_table_alloc(set->ctx,
2107 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2108 if (!data->p[i].table)
2109 goto error;
2110 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2111 goto error;
2113 return data;
2114 error:
2115 sh_data_free(data);
2116 return NULL;
2119 /* Check if inequality "ineq" is a bound for basic set "j" or if
2120 * it can be relaxed (by increasing the constant term) to become
2121 * a bound for that basic set. In the latter case, the constant
2122 * term is updated.
2123 * Relaxation of the constant term is only allowed if "shift" is set.
2125 * Return 1 if "ineq" is a bound
2126 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2127 * -1 if some error occurred
2129 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2130 isl_int *ineq, int shift)
2132 enum isl_lp_result res;
2133 isl_int opt;
2135 if (!data->p[j].tab) {
2136 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2137 if (!data->p[j].tab)
2138 return -1;
2141 isl_int_init(opt);
2143 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2144 &opt, NULL, 0);
2145 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2146 if (shift)
2147 isl_int_sub(ineq[0], ineq[0], opt);
2148 else
2149 res = isl_lp_unbounded;
2152 isl_int_clear(opt);
2154 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2155 res == isl_lp_unbounded ? 0 : -1;
2158 /* Set the constant term of "ineq" to the maximum of those of the constraints
2159 * in the basic sets of "set" following "i" that are parallel to "ineq".
2160 * That is, if any of the basic sets of "set" following "i" have a more
2161 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2162 * "c_hash" is the hash value of the linear part of "ineq".
2163 * "v" has been set up for use by has_ineq.
2165 * Note that the two inequality constraints corresponding to an equality are
2166 * represented by the same inequality constraint in data->p[j].table
2167 * (but with different hash values). This means the constraint (or at
2168 * least its constant term) may need to be temporarily negated to get
2169 * the actually hashed constraint.
2171 static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
2172 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2174 int j;
2175 isl_ctx *ctx;
2176 struct isl_hash_table_entry *entry;
2178 ctx = isl_set_get_ctx(set);
2179 for (j = i + 1; j < set->n; ++j) {
2180 int neg;
2181 isl_int *ineq_j;
2183 entry = isl_hash_table_find(ctx, data->p[j].table,
2184 c_hash, &has_ineq, v, 0);
2185 if (!entry)
2186 continue;
2188 ineq_j = entry->data;
2189 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2190 if (neg)
2191 isl_int_neg(ineq_j[0], ineq_j[0]);
2192 if (isl_int_gt(ineq_j[0], ineq[0]))
2193 isl_int_set(ineq[0], ineq_j[0]);
2194 if (neg)
2195 isl_int_neg(ineq_j[0], ineq_j[0]);
2199 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2200 * become a bound on the whole set. If so, add the (relaxed) inequality
2201 * to "hull". Relaxation is only allowed if "shift" is set.
2203 * We first check if "hull" already contains a translate of the inequality.
2204 * If so, we are done.
2205 * Then, we check if any of the previous basic sets contains a translate
2206 * of the inequality. If so, then we have already considered this
2207 * inequality and we are done.
2208 * Otherwise, for each basic set other than "i", we check if the inequality
2209 * is a bound on the basic set, but first replace the constant term
2210 * by the maximal value of any translate of the inequality in any
2211 * of the following basic sets.
2212 * For previous basic sets, we know that they do not contain a translate
2213 * of the inequality, so we directly call is_bound.
2214 * For following basic sets, we first check if a translate of the
2215 * inequality appears in its description. If so, the constant term
2216 * of the inequality has already been updated with respect to this
2217 * translate and the inequality is therefore known to be a bound
2218 * of this basic set.
2220 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2221 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
2222 int shift)
2224 uint32_t c_hash;
2225 struct ineq_cmp_data v;
2226 struct isl_hash_table_entry *entry;
2227 int j, k;
2229 if (!hull)
2230 return NULL;
2232 v.len = isl_basic_set_total_dim(hull);
2233 v.p = ineq;
2234 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2236 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2237 has_ineq, &v, 0);
2238 if (entry)
2239 return hull;
2241 for (j = 0; j < i; ++j) {
2242 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2243 c_hash, has_ineq, &v, 0);
2244 if (entry)
2245 break;
2247 if (j < i)
2248 return hull;
2250 k = isl_basic_set_alloc_inequality(hull);
2251 if (k < 0)
2252 goto error;
2253 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2255 set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
2256 for (j = 0; j < i; ++j) {
2257 int bound;
2258 bound = is_bound(data, set, j, hull->ineq[k], shift);
2259 if (bound < 0)
2260 goto error;
2261 if (!bound)
2262 break;
2264 if (j < i) {
2265 isl_basic_set_free_inequality(hull, 1);
2266 return hull;
2269 for (j = i + 1; j < set->n; ++j) {
2270 int bound;
2271 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2272 c_hash, has_ineq, &v, 0);
2273 if (entry)
2274 continue;
2275 bound = is_bound(data, set, j, hull->ineq[k], shift);
2276 if (bound < 0)
2277 goto error;
2278 if (!bound)
2279 break;
2281 if (j < set->n) {
2282 isl_basic_set_free_inequality(hull, 1);
2283 return hull;
2286 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2287 has_ineq, &v, 1);
2288 if (!entry)
2289 goto error;
2290 entry->data = hull->ineq[k];
2292 return hull;
2293 error:
2294 isl_basic_set_free(hull);
2295 return NULL;
2298 /* Check if any inequality from basic set "i" is or can be relaxed to
2299 * become a bound on the whole set. If so, add the (relaxed) inequality
2300 * to "hull". Relaxation is only allowed if "shift" is set.
2302 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2303 struct sh_data *data, struct isl_set *set, int i, int shift)
2305 int j, k;
2306 unsigned dim = isl_basic_set_total_dim(bset);
2308 for (j = 0; j < set->p[i]->n_eq; ++j) {
2309 for (k = 0; k < 2; ++k) {
2310 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2311 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2312 shift);
2315 for (j = 0; j < set->p[i]->n_ineq; ++j)
2316 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2317 return bset;
2320 /* Compute a superset of the convex hull of set that is described
2321 * by only (translates of) the constraints in the constituents of set.
2322 * Translation is only allowed if "shift" is set.
2324 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2325 int shift)
2327 struct sh_data *data = NULL;
2328 struct isl_basic_set *hull = NULL;
2329 unsigned n_ineq;
2330 int i;
2332 if (!set)
2333 return NULL;
2335 n_ineq = 0;
2336 for (i = 0; i < set->n; ++i) {
2337 if (!set->p[i])
2338 goto error;
2339 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2342 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2343 if (!hull)
2344 goto error;
2346 data = sh_data_alloc(set, n_ineq);
2347 if (!data)
2348 goto error;
2350 for (i = 0; i < set->n; ++i)
2351 hull = add_bounds(hull, data, set, i, shift);
2353 sh_data_free(data);
2354 isl_set_free(set);
2356 return hull;
2357 error:
2358 sh_data_free(data);
2359 isl_basic_set_free(hull);
2360 isl_set_free(set);
2361 return NULL;
2364 /* Compute a superset of the convex hull of map that is described
2365 * by only (translates of) the constraints in the constituents of map.
2366 * Handle trivial cases where map is NULL or contains at most one disjunct.
2368 static __isl_give isl_basic_map *map_simple_hull_trivial(
2369 __isl_take isl_map *map)
2371 isl_basic_map *hull;
2373 if (!map)
2374 return NULL;
2375 if (map->n == 0)
2376 return replace_map_by_empty_basic_map(map);
2378 hull = isl_basic_map_copy(map->p[0]);
2379 isl_map_free(map);
2380 return hull;
2383 /* Compute a superset of the convex hull of map that is described
2384 * by only (translates of) the constraints in the constituents of map.
2385 * Translation is only allowed if "shift" is set.
2387 * Sort the constraints before removing redundant constraints
2388 * in order to indicate a preference of which constraints should
2389 * be preserved. In particular, pairs of constraints that are
2390 * sorted together are preferred to either both be preserved
2391 * or both be removed.
2393 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2394 int shift)
2396 struct isl_set *set = NULL;
2397 struct isl_basic_map *model = NULL;
2398 struct isl_basic_map *hull;
2399 struct isl_basic_map *affine_hull;
2400 struct isl_basic_set *bset = NULL;
2402 if (!map || map->n <= 1)
2403 return map_simple_hull_trivial(map);
2405 map = isl_map_detect_equalities(map);
2406 if (!map || map->n <= 1)
2407 return map_simple_hull_trivial(map);
2408 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2409 map = isl_map_align_divs(map);
2410 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2412 set = isl_map_underlying_set(map);
2414 bset = uset_simple_hull(set, shift);
2416 hull = isl_basic_map_overlying_set(bset, model);
2418 hull = isl_basic_map_intersect(hull, affine_hull);
2419 hull = isl_basic_map_sort_constraints(hull);
2420 hull = isl_basic_map_remove_redundancies(hull);
2422 if (!hull)
2423 return NULL;
2424 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2425 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2427 hull = isl_basic_map_finalize(hull);
2429 return hull;
2432 /* Compute a superset of the convex hull of map that is described
2433 * by only translates of the constraints in the constituents of map.
2435 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2437 return map_simple_hull(map, 1);
2440 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2442 return (struct isl_basic_set *)
2443 isl_map_simple_hull((struct isl_map *)set);
2446 /* Compute a superset of the convex hull of map that is described
2447 * by only the constraints in the constituents of map.
2449 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2450 __isl_take isl_map *map)
2452 return map_simple_hull(map, 0);
2455 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2456 __isl_take isl_set *set)
2458 return isl_map_unshifted_simple_hull(set);
2461 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2462 * A constraint that appears with different constant terms
2463 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2464 * (i.e., greatest) constant term.
2465 * "bmap1" and "bmap2" are assumed to have the same (known)
2466 * integer divisions.
2467 * The constraints of both "bmap1" and "bmap2" are assumed
2468 * to have been sorted using isl_basic_map_sort_constraints.
2470 * Run through the inequality constraints of "bmap1" and "bmap2"
2471 * in sorted order.
2472 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2473 * is removed.
2474 * If a match is found, the constraint is kept. If needed, the constant
2475 * term of the constraint is adjusted.
2477 static __isl_give isl_basic_map *select_shared_inequalities(
2478 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2480 int i1, i2;
2482 bmap1 = isl_basic_map_cow(bmap1);
2483 if (!bmap1 || !bmap2)
2484 return isl_basic_map_free(bmap1);
2486 i1 = bmap1->n_ineq - 1;
2487 i2 = bmap2->n_ineq - 1;
2488 while (bmap1 && i1 >= 0 && i2 >= 0) {
2489 int cmp;
2491 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2492 bmap2->ineq[i2]);
2493 if (cmp < 0) {
2494 --i2;
2495 continue;
2497 if (cmp > 0) {
2498 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2499 bmap1 = isl_basic_map_free(bmap1);
2500 --i1;
2501 continue;
2503 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2504 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2505 --i1;
2506 --i2;
2508 for (; i1 >= 0; --i1)
2509 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2510 bmap1 = isl_basic_map_free(bmap1);
2512 return bmap1;
2515 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2516 * "bmap1" and "bmap2" are assumed to have the same (known)
2517 * integer divisions.
2519 * Run through the equality constraints of "bmap1" and "bmap2".
2520 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2521 * is removed.
2523 static __isl_give isl_basic_map *select_shared_equalities(
2524 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2526 int i1, i2;
2527 unsigned total;
2529 bmap1 = isl_basic_map_cow(bmap1);
2530 if (!bmap1 || !bmap2)
2531 return isl_basic_map_free(bmap1);
2533 total = isl_basic_map_total_dim(bmap1);
2535 i1 = bmap1->n_eq - 1;
2536 i2 = bmap2->n_eq - 1;
2537 while (bmap1 && i1 >= 0 && i2 >= 0) {
2538 int last1, last2;
2540 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2541 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2542 if (last1 > last2) {
2543 --i2;
2544 continue;
2546 if (last1 < last2) {
2547 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2548 bmap1 = isl_basic_map_free(bmap1);
2549 --i1;
2550 continue;
2552 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2553 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2554 bmap1 = isl_basic_map_free(bmap1);
2556 --i1;
2557 --i2;
2559 for (; i1 >= 0; --i1)
2560 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2561 bmap1 = isl_basic_map_free(bmap1);
2563 return bmap1;
2566 /* Compute a superset of "bmap1" and "bmap2" that is described
2567 * by only the constraints that appear in both "bmap1" and "bmap2".
2569 * First drop constraints that involve unknown integer divisions
2570 * since it is not trivial to check whether two such integer divisions
2571 * in different basic maps are the same.
2572 * Then align the remaining (known) divs and sort the constraints.
2573 * Finally drop all inequalities and equalities from "bmap1" that
2574 * do not also appear in "bmap2".
2576 __isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2577 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2579 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2580 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2581 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2582 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2583 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2584 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2585 bmap1 = isl_basic_map_sort_constraints(bmap1);
2586 bmap2 = isl_basic_map_sort_constraints(bmap2);
2588 bmap1 = select_shared_inequalities(bmap1, bmap2);
2589 bmap1 = select_shared_equalities(bmap1, bmap2);
2591 isl_basic_map_free(bmap2);
2592 bmap1 = isl_basic_map_finalize(bmap1);
2593 return bmap1;
2596 /* Compute a superset of the convex hull of "map" that is described
2597 * by only the constraints in the constituents of "map".
2598 * In particular, the result is composed of constraints that appear
2599 * in each of the basic maps of "map"
2601 * Constraints that involve unknown integer divisions are dropped
2602 * since it is not trivial to check whether two such integer divisions
2603 * in different basic maps are the same.
2605 * The hull is initialized from the first basic map and then
2606 * updated with respect to the other basic maps in turn.
2608 __isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2609 __isl_take isl_map *map)
2611 int i;
2612 isl_basic_map *hull;
2614 if (!map)
2615 return NULL;
2616 if (map->n <= 1)
2617 return map_simple_hull_trivial(map);
2618 map = isl_map_drop_constraint_involving_unknown_divs(map);
2619 hull = isl_basic_map_copy(map->p[0]);
2620 for (i = 1; i < map->n; ++i) {
2621 isl_basic_map *bmap_i;
2623 bmap_i = isl_basic_map_copy(map->p[i]);
2624 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2627 isl_map_free(map);
2628 return hull;
2631 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2633 * For each basic set in "set", we first check if the basic set
2634 * contains a translate of "ineq". If this translate is more relaxed,
2635 * then we assume that "ineq" is not a bound on this basic set.
2636 * Otherwise, we know that it is a bound.
2637 * If the basic set does not contain a translate of "ineq", then
2638 * we call is_bound to perform the test.
2640 static __isl_give isl_basic_set *add_bound_from_constraint(
2641 __isl_take isl_basic_set *hull, struct sh_data *data,
2642 __isl_keep isl_set *set, isl_int *ineq)
2644 int i, k;
2645 isl_ctx *ctx;
2646 uint32_t c_hash;
2647 struct ineq_cmp_data v;
2649 if (!hull || !set)
2650 return isl_basic_set_free(hull);
2652 v.len = isl_basic_set_total_dim(hull);
2653 v.p = ineq;
2654 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2656 ctx = isl_basic_set_get_ctx(hull);
2657 for (i = 0; i < set->n; ++i) {
2658 int bound;
2659 struct isl_hash_table_entry *entry;
2661 entry = isl_hash_table_find(ctx, data->p[i].table,
2662 c_hash, &has_ineq, &v, 0);
2663 if (entry) {
2664 isl_int *ineq_i = entry->data;
2665 int neg, more_relaxed;
2667 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2668 if (neg)
2669 isl_int_neg(ineq_i[0], ineq_i[0]);
2670 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2671 if (neg)
2672 isl_int_neg(ineq_i[0], ineq_i[0]);
2673 if (more_relaxed)
2674 break;
2675 else
2676 continue;
2678 bound = is_bound(data, set, i, ineq, 0);
2679 if (bound < 0)
2680 return isl_basic_set_free(hull);
2681 if (!bound)
2682 break;
2684 if (i < set->n)
2685 return hull;
2687 k = isl_basic_set_alloc_inequality(hull);
2688 if (k < 0)
2689 return isl_basic_set_free(hull);
2690 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2692 return hull;
2695 /* Compute a superset of the convex hull of "set" that is described
2696 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2697 * has no parameters or integer divisions.
2699 * The inequalities in "ineq" are assumed to have been sorted such
2700 * that constraints with the same linear part appear together and
2701 * that among constraints with the same linear part, those with
2702 * smaller constant term appear first.
2704 * We reuse the same data structure that is used by uset_simple_hull,
2705 * but we do not need the hull table since we will not consider the
2706 * same constraint more than once. We therefore allocate it with zero size.
2708 * We run through the constraints and try to add them one by one,
2709 * skipping identical constraints. If we have added a constraint and
2710 * the next constraint is a more relaxed translate, then we skip this
2711 * next constraint as well.
2713 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2714 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2716 int i;
2717 int last_added = 0;
2718 struct sh_data *data = NULL;
2719 isl_basic_set *hull = NULL;
2720 unsigned dim;
2722 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2723 if (!hull)
2724 goto error;
2726 data = sh_data_alloc(set, 0);
2727 if (!data)
2728 goto error;
2730 dim = isl_set_dim(set, isl_dim_set);
2731 for (i = 0; i < n_ineq; ++i) {
2732 int hull_n_ineq = hull->n_ineq;
2733 int parallel;
2735 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2736 dim);
2737 if (parallel &&
2738 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2739 continue;
2740 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2741 if (!hull)
2742 goto error;
2743 last_added = hull->n_ineq > hull_n_ineq;
2746 sh_data_free(data);
2747 isl_set_free(set);
2748 return hull;
2749 error:
2750 sh_data_free(data);
2751 isl_set_free(set);
2752 isl_basic_set_free(hull);
2753 return NULL;
2756 /* Collect pointers to all the inequalities in the elements of "list"
2757 * in "ineq". For equalities, store both a pointer to the equality and
2758 * a pointer to its opposite, which is first copied to "mat".
2759 * "ineq" and "mat" are assumed to have been preallocated to the right size
2760 * (the number of inequalities + 2 times the number of equalites and
2761 * the number of equalities, respectively).
2763 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2764 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2766 int i, j, n, n_eq, n_ineq;
2768 if (!mat)
2769 return NULL;
2771 n_eq = 0;
2772 n_ineq = 0;
2773 n = isl_basic_set_list_n_basic_set(list);
2774 for (i = 0; i < n; ++i) {
2775 isl_basic_set *bset;
2776 bset = isl_basic_set_list_get_basic_set(list, i);
2777 if (!bset)
2778 return isl_mat_free(mat);
2779 for (j = 0; j < bset->n_eq; ++j) {
2780 ineq[n_ineq++] = mat->row[n_eq];
2781 ineq[n_ineq++] = bset->eq[j];
2782 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2784 for (j = 0; j < bset->n_ineq; ++j)
2785 ineq[n_ineq++] = bset->ineq[j];
2786 isl_basic_set_free(bset);
2789 return mat;
2792 /* Comparison routine for use as an isl_sort callback.
2794 * Constraints with the same linear part are sorted together and
2795 * among constraints with the same linear part, those with smaller
2796 * constant term are sorted first.
2798 static int cmp_ineq(const void *a, const void *b, void *arg)
2800 unsigned dim = *(unsigned *) arg;
2801 isl_int * const *ineq1 = a;
2802 isl_int * const *ineq2 = b;
2803 int cmp;
2805 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2806 if (cmp != 0)
2807 return cmp;
2808 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2811 /* Compute a superset of the convex hull of "set" that is described
2812 * by only constraints in the elements of "list", where "set" has
2813 * no parameters or integer divisions.
2815 * We collect all the constraints in those elements and then
2816 * sort the constraints such that constraints with the same linear part
2817 * are sorted together and that those with smaller constant term are
2818 * sorted first.
2820 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2821 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2823 int i, n, n_eq, n_ineq;
2824 unsigned dim;
2825 isl_ctx *ctx;
2826 isl_mat *mat = NULL;
2827 isl_int **ineq = NULL;
2828 isl_basic_set *hull;
2830 if (!set)
2831 goto error;
2832 ctx = isl_set_get_ctx(set);
2834 n_eq = 0;
2835 n_ineq = 0;
2836 n = isl_basic_set_list_n_basic_set(list);
2837 for (i = 0; i < n; ++i) {
2838 isl_basic_set *bset;
2839 bset = isl_basic_set_list_get_basic_set(list, i);
2840 if (!bset)
2841 goto error;
2842 n_eq += bset->n_eq;
2843 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2844 isl_basic_set_free(bset);
2847 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2848 if (n_ineq > 0 && !ineq)
2849 goto error;
2851 dim = isl_set_dim(set, isl_dim_set);
2852 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2853 mat = collect_inequalities(mat, list, ineq);
2854 if (!mat)
2855 goto error;
2857 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2858 goto error;
2860 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2862 isl_mat_free(mat);
2863 free(ineq);
2864 isl_basic_set_list_free(list);
2865 return hull;
2866 error:
2867 isl_mat_free(mat);
2868 free(ineq);
2869 isl_set_free(set);
2870 isl_basic_set_list_free(list);
2871 return NULL;
2874 /* Compute a superset of the convex hull of "map" that is described
2875 * by only constraints in the elements of "list".
2877 * If the list is empty, then we can only describe the universe set.
2878 * If the input map is empty, then all constraints are valid, so
2879 * we return the intersection of the elements in "list".
2881 * Otherwise, we align all divs and temporarily treat them
2882 * as regular variables, computing the unshifted simple hull in
2883 * uset_unshifted_simple_hull_from_basic_set_list.
2885 static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2886 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2888 isl_basic_map *model;
2889 isl_basic_map *hull;
2890 isl_set *set;
2891 isl_basic_set_list *bset_list;
2893 if (!map || !list)
2894 goto error;
2896 if (isl_basic_map_list_n_basic_map(list) == 0) {
2897 isl_space *space;
2899 space = isl_map_get_space(map);
2900 isl_map_free(map);
2901 isl_basic_map_list_free(list);
2902 return isl_basic_map_universe(space);
2904 if (isl_map_plain_is_empty(map)) {
2905 isl_map_free(map);
2906 return isl_basic_map_list_intersect(list);
2909 map = isl_map_align_divs_to_basic_map_list(map, list);
2910 if (!map)
2911 goto error;
2912 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2914 model = isl_basic_map_list_get_basic_map(list, 0);
2916 set = isl_map_underlying_set(map);
2917 bset_list = isl_basic_map_list_underlying_set(list);
2919 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2920 hull = isl_basic_map_overlying_set(hull, model);
2922 return hull;
2923 error:
2924 isl_map_free(map);
2925 isl_basic_map_list_free(list);
2926 return NULL;
2929 /* Return a sequence of the basic maps that make up the maps in "list".
2931 static __isl_give isl_basic_set_list *collect_basic_maps(
2932 __isl_take isl_map_list *list)
2934 int i, n;
2935 isl_ctx *ctx;
2936 isl_basic_map_list *bmap_list;
2938 if (!list)
2939 return NULL;
2940 n = isl_map_list_n_map(list);
2941 ctx = isl_map_list_get_ctx(list);
2942 bmap_list = isl_basic_map_list_alloc(ctx, 0);
2944 for (i = 0; i < n; ++i) {
2945 isl_map *map;
2946 isl_basic_map_list *list_i;
2948 map = isl_map_list_get_map(list, i);
2949 map = isl_map_compute_divs(map);
2950 list_i = isl_map_get_basic_map_list(map);
2951 isl_map_free(map);
2952 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
2955 isl_map_list_free(list);
2956 return bmap_list;
2959 /* Compute a superset of the convex hull of "map" that is described
2960 * by only constraints in the elements of "list".
2962 * If "map" is the universe, then the convex hull (and therefore
2963 * any superset of the convexhull) is the universe as well.
2965 * Otherwise, we collect all the basic maps in the map list and
2966 * continue with map_unshifted_simple_hull_from_basic_map_list.
2968 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
2969 __isl_take isl_map *map, __isl_take isl_map_list *list)
2971 isl_basic_map_list *bmap_list;
2972 int is_universe;
2974 is_universe = isl_map_plain_is_universe(map);
2975 if (is_universe < 0)
2976 map = isl_map_free(map);
2977 if (is_universe < 0 || is_universe) {
2978 isl_map_list_free(list);
2979 return isl_map_unshifted_simple_hull(map);
2982 bmap_list = collect_basic_maps(list);
2983 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
2986 /* Compute a superset of the convex hull of "set" that is described
2987 * by only constraints in the elements of "list".
2989 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
2990 __isl_take isl_set *set, __isl_take isl_set_list *list)
2992 return isl_map_unshifted_simple_hull_from_map_list(set, list);
2995 /* Given a set "set", return parametric bounds on the dimension "dim".
2997 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2999 unsigned set_dim = isl_set_dim(set, isl_dim_set);
3000 set = isl_set_copy(set);
3001 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
3002 set = isl_set_eliminate_dims(set, 0, dim);
3003 return isl_set_convex_hull(set);
3006 /* Computes a "simple hull" and then check if each dimension in the
3007 * resulting hull is bounded by a symbolic constant. If not, the
3008 * hull is intersected with the corresponding bounds on the whole set.
3010 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
3012 int i, j;
3013 struct isl_basic_set *hull;
3014 unsigned nparam, left;
3015 int removed_divs = 0;
3017 hull = isl_set_simple_hull(isl_set_copy(set));
3018 if (!hull)
3019 goto error;
3021 nparam = isl_basic_set_dim(hull, isl_dim_param);
3022 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
3023 int lower = 0, upper = 0;
3024 struct isl_basic_set *bounds;
3026 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
3027 for (j = 0; j < hull->n_eq; ++j) {
3028 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3029 continue;
3030 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3031 left) == -1)
3032 break;
3034 if (j < hull->n_eq)
3035 continue;
3037 for (j = 0; j < hull->n_ineq; ++j) {
3038 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3039 continue;
3040 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3041 left) != -1 ||
3042 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3043 i) != -1)
3044 continue;
3045 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3046 lower = 1;
3047 else
3048 upper = 1;
3049 if (lower && upper)
3050 break;
3053 if (lower && upper)
3054 continue;
3056 if (!removed_divs) {
3057 set = isl_set_remove_divs(set);
3058 if (!set)
3059 goto error;
3060 removed_divs = 1;
3062 bounds = set_bounds(set, i);
3063 hull = isl_basic_set_intersect(hull, bounds);
3064 if (!hull)
3065 goto error;
3068 isl_set_free(set);
3069 return hull;
3070 error:
3071 isl_set_free(set);
3072 return NULL;