extract out shared set_to_map
[isl.git] / isl_convex_hull.c
blob48285dd16bc098585c78ed2ec6e779d5cb0d1611
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 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
30 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
32 /* Return 1 if constraint c is redundant with respect to the constraints
33 * in bmap. If c is a lower [upper] bound in some variable and bmap
34 * does not have a lower [upper] bound in that variable, then c cannot
35 * be redundant and we do not need solve any lp.
37 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
38 isl_int *c, isl_int *opt_n, isl_int *opt_d)
40 enum isl_lp_result res;
41 unsigned total;
42 int i, j;
44 if (!bmap)
45 return -1;
47 total = isl_basic_map_total_dim(*bmap);
48 for (i = 0; i < total; ++i) {
49 int sign;
50 if (isl_int_is_zero(c[1+i]))
51 continue;
52 sign = isl_int_sgn(c[1+i]);
53 for (j = 0; j < (*bmap)->n_ineq; ++j)
54 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
55 break;
56 if (j == (*bmap)->n_ineq)
57 break;
59 if (i < total)
60 return 0;
62 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
63 opt_n, opt_d, NULL);
64 if (res == isl_lp_unbounded)
65 return 0;
66 if (res == isl_lp_error)
67 return -1;
68 if (res == isl_lp_empty) {
69 *bmap = isl_basic_map_set_to_empty(*bmap);
70 return 0;
72 return !isl_int_is_neg(*opt_n);
75 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
76 isl_int *c, isl_int *opt_n, isl_int *opt_d)
78 return isl_basic_map_constraint_is_redundant(
79 (struct isl_basic_map **)bset, c, opt_n, opt_d);
82 /* Remove redundant
83 * constraints. If the minimal value along the normal of a constraint
84 * is the same if the constraint is removed, then the constraint is redundant.
86 * Since some constraints may be mutually redundant, sort the constraints
87 * first such that constraints that involve existentially quantified
88 * variables are considered for removal before those that do not.
89 * The sorting is also need for the use in map_simple_hull.
91 * Alternatively, we could have intersected the basic map with the
92 * corresponding equality and then checked if the dimension was that
93 * of a facet.
95 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
96 __isl_take isl_basic_map *bmap)
98 struct isl_tab *tab;
100 if (!bmap)
101 return NULL;
103 bmap = isl_basic_map_gauss(bmap, NULL);
104 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
105 return bmap;
106 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
107 return bmap;
108 if (bmap->n_ineq <= 1)
109 return bmap;
111 bmap = isl_basic_map_sort_constraints(bmap);
112 tab = isl_tab_from_basic_map(bmap, 0);
113 if (isl_tab_detect_implicit_equalities(tab) < 0)
114 goto error;
115 if (isl_tab_detect_redundant(tab) < 0)
116 goto error;
117 bmap = isl_basic_map_update_from_tab(bmap, tab);
118 isl_tab_free(tab);
119 if (!bmap)
120 return NULL;
121 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
122 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
123 return bmap;
124 error:
125 isl_tab_free(tab);
126 isl_basic_map_free(bmap);
127 return NULL;
130 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
131 __isl_take isl_basic_set *bset)
133 return bset_from_bmap(
134 isl_basic_map_remove_redundancies(bset_to_bmap(bset)));
137 /* Remove redundant constraints in each of the basic maps.
139 __isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
141 return isl_map_inline_foreach_basic_map(map,
142 &isl_basic_map_remove_redundancies);
145 __isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
147 return isl_map_remove_redundancies(set);
150 /* Check if the set set is bound in the direction of the affine
151 * constraint c and if so, set the constant term such that the
152 * resulting constraint is a bounding constraint for the set.
154 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
156 int first;
157 int j;
158 isl_int opt;
159 isl_int opt_denom;
161 isl_int_init(opt);
162 isl_int_init(opt_denom);
163 first = 1;
164 for (j = 0; j < set->n; ++j) {
165 enum isl_lp_result res;
167 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
168 continue;
170 res = isl_basic_set_solve_lp(set->p[j],
171 0, c, set->ctx->one, &opt, &opt_denom, NULL);
172 if (res == isl_lp_unbounded)
173 break;
174 if (res == isl_lp_error)
175 goto error;
176 if (res == isl_lp_empty) {
177 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
178 if (!set->p[j])
179 goto error;
180 continue;
182 if (first || isl_int_is_neg(opt)) {
183 if (!isl_int_is_one(opt_denom))
184 isl_seq_scale(c, c, opt_denom, len);
185 isl_int_sub(c[0], c[0], opt);
187 first = 0;
189 isl_int_clear(opt);
190 isl_int_clear(opt_denom);
191 return j >= set->n;
192 error:
193 isl_int_clear(opt);
194 isl_int_clear(opt_denom);
195 return -1;
198 __isl_give isl_basic_map *isl_basic_map_set_rational(
199 __isl_take isl_basic_set *bmap)
201 if (!bmap)
202 return NULL;
204 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
205 return bmap;
207 bmap = isl_basic_map_cow(bmap);
208 if (!bmap)
209 return NULL;
211 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
213 return isl_basic_map_finalize(bmap);
216 __isl_give isl_basic_set *isl_basic_set_set_rational(
217 __isl_take isl_basic_set *bset)
219 return isl_basic_map_set_rational(bset);
222 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
224 int i;
226 map = isl_map_cow(map);
227 if (!map)
228 return NULL;
229 for (i = 0; i < map->n; ++i) {
230 map->p[i] = isl_basic_map_set_rational(map->p[i]);
231 if (!map->p[i])
232 goto error;
234 return map;
235 error:
236 isl_map_free(map);
237 return NULL;
240 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
242 return isl_map_set_rational(set);
245 static struct isl_basic_set *isl_basic_set_add_equality(
246 struct isl_basic_set *bset, isl_int *c)
248 int i;
249 unsigned dim;
251 if (!bset)
252 return NULL;
254 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
255 return bset;
257 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
258 isl_assert(bset->ctx, bset->n_div == 0, goto error);
259 dim = isl_basic_set_n_dim(bset);
260 bset = isl_basic_set_cow(bset);
261 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
262 i = isl_basic_set_alloc_equality(bset);
263 if (i < 0)
264 goto error;
265 isl_seq_cpy(bset->eq[i], c, 1 + dim);
266 return bset;
267 error:
268 isl_basic_set_free(bset);
269 return NULL;
272 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
274 int i;
276 set = isl_set_cow(set);
277 if (!set)
278 return NULL;
279 for (i = 0; i < set->n; ++i) {
280 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
281 if (!set->p[i])
282 goto error;
284 return set;
285 error:
286 isl_set_free(set);
287 return NULL;
290 /* Given a union of basic sets, construct the constraints for wrapping
291 * a facet around one of its ridges.
292 * In particular, if each of n the d-dimensional basic sets i in "set"
293 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
294 * and is defined by the constraints
295 * [ 1 ]
296 * A_i [ x ] >= 0
298 * then the resulting set is of dimension n*(1+d) and has as constraints
300 * [ a_i ]
301 * A_i [ x_i ] >= 0
303 * a_i >= 0
305 * \sum_i x_{i,1} = 1
307 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
309 struct isl_basic_set *lp;
310 unsigned n_eq;
311 unsigned n_ineq;
312 int i, j, k;
313 unsigned dim, lp_dim;
315 if (!set)
316 return NULL;
318 dim = 1 + isl_set_n_dim(set);
319 n_eq = 1;
320 n_ineq = set->n;
321 for (i = 0; i < set->n; ++i) {
322 n_eq += set->p[i]->n_eq;
323 n_ineq += set->p[i]->n_ineq;
325 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
326 lp = isl_basic_set_set_rational(lp);
327 if (!lp)
328 return NULL;
329 lp_dim = isl_basic_set_n_dim(lp);
330 k = isl_basic_set_alloc_equality(lp);
331 isl_int_set_si(lp->eq[k][0], -1);
332 for (i = 0; i < set->n; ++i) {
333 isl_int_set_si(lp->eq[k][1+dim*i], 0);
334 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
335 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
337 for (i = 0; i < set->n; ++i) {
338 k = isl_basic_set_alloc_inequality(lp);
339 isl_seq_clr(lp->ineq[k], 1+lp_dim);
340 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
342 for (j = 0; j < set->p[i]->n_eq; ++j) {
343 k = isl_basic_set_alloc_equality(lp);
344 isl_seq_clr(lp->eq[k], 1+dim*i);
345 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
346 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
349 for (j = 0; j < set->p[i]->n_ineq; ++j) {
350 k = isl_basic_set_alloc_inequality(lp);
351 isl_seq_clr(lp->ineq[k], 1+dim*i);
352 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
353 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
356 return lp;
359 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
360 * of that facet, compute the other facet of the convex hull that contains
361 * the ridge.
363 * We first transform the set such that the facet constraint becomes
365 * x_1 >= 0
367 * I.e., the facet lies in
369 * x_1 = 0
371 * and on that facet, the constraint that defines the ridge is
373 * x_2 >= 0
375 * (This transformation is not strictly needed, all that is needed is
376 * that the ridge contains the origin.)
378 * Since the ridge contains the origin, the cone of the convex hull
379 * will be of the form
381 * x_1 >= 0
382 * x_2 >= a x_1
384 * with this second constraint defining the new facet.
385 * The constant a is obtained by settting x_1 in the cone of the
386 * convex hull to 1 and minimizing x_2.
387 * Now, each element in the cone of the convex hull is the sum
388 * of elements in the cones of the basic sets.
389 * If a_i is the dilation factor of basic set i, then the problem
390 * we need to solve is
392 * min \sum_i x_{i,2}
393 * st
394 * \sum_i x_{i,1} = 1
395 * a_i >= 0
396 * [ a_i ]
397 * A [ x_i ] >= 0
399 * with
400 * [ 1 ]
401 * A_i [ x_i ] >= 0
403 * the constraints of each (transformed) basic set.
404 * If a = n/d, then the constraint defining the new facet (in the transformed
405 * space) is
407 * -n x_1 + d x_2 >= 0
409 * In the original space, we need to take the same combination of the
410 * corresponding constraints "facet" and "ridge".
412 * If a = -infty = "-1/0", then we just return the original facet constraint.
413 * This means that the facet is unbounded, but has a bounded intersection
414 * with the union of sets.
416 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
417 isl_int *facet, isl_int *ridge)
419 int i;
420 isl_ctx *ctx;
421 struct isl_mat *T = NULL;
422 struct isl_basic_set *lp = NULL;
423 struct isl_vec *obj;
424 enum isl_lp_result res;
425 isl_int num, den;
426 unsigned dim;
428 if (!set)
429 return NULL;
430 ctx = set->ctx;
431 set = isl_set_copy(set);
432 set = isl_set_set_rational(set);
434 dim = 1 + isl_set_n_dim(set);
435 T = isl_mat_alloc(ctx, 3, dim);
436 if (!T)
437 goto error;
438 isl_int_set_si(T->row[0][0], 1);
439 isl_seq_clr(T->row[0]+1, dim - 1);
440 isl_seq_cpy(T->row[1], facet, dim);
441 isl_seq_cpy(T->row[2], ridge, dim);
442 T = isl_mat_right_inverse(T);
443 set = isl_set_preimage(set, T);
444 T = NULL;
445 if (!set)
446 goto error;
447 lp = wrap_constraints(set);
448 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
449 if (!obj)
450 goto error;
451 isl_int_set_si(obj->block.data[0], 0);
452 for (i = 0; i < set->n; ++i) {
453 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
454 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
455 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
457 isl_int_init(num);
458 isl_int_init(den);
459 res = isl_basic_set_solve_lp(lp, 0,
460 obj->block.data, ctx->one, &num, &den, NULL);
461 if (res == isl_lp_ok) {
462 isl_int_neg(num, num);
463 isl_seq_combine(facet, num, facet, den, ridge, dim);
464 isl_seq_normalize(ctx, facet, dim);
466 isl_int_clear(num);
467 isl_int_clear(den);
468 isl_vec_free(obj);
469 isl_basic_set_free(lp);
470 isl_set_free(set);
471 if (res == isl_lp_error)
472 return NULL;
473 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
474 return NULL);
475 return facet;
476 error:
477 isl_basic_set_free(lp);
478 isl_mat_free(T);
479 isl_set_free(set);
480 return NULL;
483 /* Compute the constraint of a facet of "set".
485 * We first compute the intersection with a bounding constraint
486 * that is orthogonal to one of the coordinate axes.
487 * If the affine hull of this intersection has only one equality,
488 * we have found a facet.
489 * Otherwise, we wrap the current bounding constraint around
490 * one of the equalities of the face (one that is not equal to
491 * the current bounding constraint).
492 * This process continues until we have found a facet.
493 * The dimension of the intersection increases by at least
494 * one on each iteration, so termination is guaranteed.
496 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
498 struct isl_set *slice = NULL;
499 struct isl_basic_set *face = NULL;
500 int i;
501 unsigned dim = isl_set_n_dim(set);
502 int is_bound;
503 isl_mat *bounds = NULL;
505 isl_assert(set->ctx, set->n > 0, goto error);
506 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
507 if (!bounds)
508 return NULL;
510 isl_seq_clr(bounds->row[0], dim);
511 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
512 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
513 if (is_bound < 0)
514 goto error;
515 isl_assert(set->ctx, is_bound, goto error);
516 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
517 bounds->n_row = 1;
519 for (;;) {
520 slice = isl_set_copy(set);
521 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
522 face = isl_set_affine_hull(slice);
523 if (!face)
524 goto error;
525 if (face->n_eq == 1) {
526 isl_basic_set_free(face);
527 break;
529 for (i = 0; i < face->n_eq; ++i)
530 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
531 !isl_seq_is_neg(bounds->row[0],
532 face->eq[i], 1 + dim))
533 break;
534 isl_assert(set->ctx, i < face->n_eq, goto error);
535 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
536 goto error;
537 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
538 isl_basic_set_free(face);
541 return bounds;
542 error:
543 isl_basic_set_free(face);
544 isl_mat_free(bounds);
545 return NULL;
548 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
549 * compute a hyperplane description of the facet, i.e., compute the facets
550 * of the facet.
552 * We compute an affine transformation that transforms the constraint
554 * [ 1 ]
555 * c [ x ] = 0
557 * to the constraint
559 * z_1 = 0
561 * by computing the right inverse U of a matrix that starts with the rows
563 * [ 1 0 ]
564 * [ c ]
566 * Then
567 * [ 1 ] [ 1 ]
568 * [ x ] = U [ z ]
569 * and
570 * [ 1 ] [ 1 ]
571 * [ z ] = Q [ x ]
573 * with Q = U^{-1}
574 * Since z_1 is zero, we can drop this variable as well as the corresponding
575 * column of U to obtain
577 * [ 1 ] [ 1 ]
578 * [ x ] = U' [ z' ]
579 * and
580 * [ 1 ] [ 1 ]
581 * [ z' ] = Q' [ x ]
583 * with Q' equal to Q, but without the corresponding row.
584 * After computing the facets of the facet in the z' space,
585 * we convert them back to the x space through Q.
587 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
589 struct isl_mat *m, *U, *Q;
590 struct isl_basic_set *facet = NULL;
591 struct isl_ctx *ctx;
592 unsigned dim;
594 ctx = set->ctx;
595 set = isl_set_copy(set);
596 dim = isl_set_n_dim(set);
597 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
598 if (!m)
599 goto error;
600 isl_int_set_si(m->row[0][0], 1);
601 isl_seq_clr(m->row[0]+1, dim);
602 isl_seq_cpy(m->row[1], c, 1+dim);
603 U = isl_mat_right_inverse(m);
604 Q = isl_mat_right_inverse(isl_mat_copy(U));
605 U = isl_mat_drop_cols(U, 1, 1);
606 Q = isl_mat_drop_rows(Q, 1, 1);
607 set = isl_set_preimage(set, U);
608 facet = uset_convex_hull_wrap_bounded(set);
609 facet = isl_basic_set_preimage(facet, Q);
610 if (facet && facet->n_eq != 0)
611 isl_die(ctx, isl_error_internal, "unexpected equality",
612 return isl_basic_set_free(facet));
613 return facet;
614 error:
615 isl_basic_set_free(facet);
616 isl_set_free(set);
617 return NULL;
620 /* Given an initial facet constraint, compute the remaining facets.
621 * We do this by running through all facets found so far and computing
622 * the adjacent facets through wrapping, adding those facets that we
623 * hadn't already found before.
625 * For each facet we have found so far, we first compute its facets
626 * in the resulting convex hull. That is, we compute the ridges
627 * of the resulting convex hull contained in the facet.
628 * We also compute the corresponding facet in the current approximation
629 * of the convex hull. There is no need to wrap around the ridges
630 * in this facet since that would result in a facet that is already
631 * present in the current approximation.
633 * This function can still be significantly optimized by checking which of
634 * the facets of the basic sets are also facets of the convex hull and
635 * using all the facets so far to help in constructing the facets of the
636 * facets
637 * and/or
638 * using the technique in section "3.1 Ridge Generation" of
639 * "Extended Convex Hull" by Fukuda et al.
641 static struct isl_basic_set *extend(struct isl_basic_set *hull,
642 struct isl_set *set)
644 int i, j, f;
645 int k;
646 struct isl_basic_set *facet = NULL;
647 struct isl_basic_set *hull_facet = NULL;
648 unsigned dim;
650 if (!hull)
651 return NULL;
653 isl_assert(set->ctx, set->n > 0, goto error);
655 dim = isl_set_n_dim(set);
657 for (i = 0; i < hull->n_ineq; ++i) {
658 facet = compute_facet(set, hull->ineq[i]);
659 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
660 facet = isl_basic_set_gauss(facet, NULL);
661 facet = isl_basic_set_normalize_constraints(facet);
662 hull_facet = isl_basic_set_copy(hull);
663 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
664 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
665 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
666 if (!facet || !hull_facet)
667 goto error;
668 hull = isl_basic_set_cow(hull);
669 hull = isl_basic_set_extend_space(hull,
670 isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
671 if (!hull)
672 goto error;
673 for (j = 0; j < facet->n_ineq; ++j) {
674 for (f = 0; f < hull_facet->n_ineq; ++f)
675 if (isl_seq_eq(facet->ineq[j],
676 hull_facet->ineq[f], 1 + dim))
677 break;
678 if (f < hull_facet->n_ineq)
679 continue;
680 k = isl_basic_set_alloc_inequality(hull);
681 if (k < 0)
682 goto error;
683 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
684 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
685 goto error;
687 isl_basic_set_free(hull_facet);
688 isl_basic_set_free(facet);
690 hull = isl_basic_set_simplify(hull);
691 hull = isl_basic_set_finalize(hull);
692 return hull;
693 error:
694 isl_basic_set_free(hull_facet);
695 isl_basic_set_free(facet);
696 isl_basic_set_free(hull);
697 return NULL;
700 /* Special case for computing the convex hull of a one dimensional set.
701 * We simply collect the lower and upper bounds of each basic set
702 * and the biggest of those.
704 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
706 struct isl_mat *c = NULL;
707 isl_int *lower = NULL;
708 isl_int *upper = NULL;
709 int i, j, k;
710 isl_int a, b;
711 struct isl_basic_set *hull;
713 for (i = 0; i < set->n; ++i) {
714 set->p[i] = isl_basic_set_simplify(set->p[i]);
715 if (!set->p[i])
716 goto error;
718 set = isl_set_remove_empty_parts(set);
719 if (!set)
720 goto error;
721 isl_assert(set->ctx, set->n > 0, goto error);
722 c = isl_mat_alloc(set->ctx, 2, 2);
723 if (!c)
724 goto error;
726 if (set->p[0]->n_eq > 0) {
727 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
728 lower = c->row[0];
729 upper = c->row[1];
730 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
731 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
732 isl_seq_neg(upper, set->p[0]->eq[0], 2);
733 } else {
734 isl_seq_neg(lower, set->p[0]->eq[0], 2);
735 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
737 } else {
738 for (j = 0; j < set->p[0]->n_ineq; ++j) {
739 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
740 lower = c->row[0];
741 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
742 } else {
743 upper = c->row[1];
744 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
749 isl_int_init(a);
750 isl_int_init(b);
751 for (i = 0; i < set->n; ++i) {
752 struct isl_basic_set *bset = set->p[i];
753 int has_lower = 0;
754 int has_upper = 0;
756 for (j = 0; j < bset->n_eq; ++j) {
757 has_lower = 1;
758 has_upper = 1;
759 if (lower) {
760 isl_int_mul(a, lower[0], bset->eq[j][1]);
761 isl_int_mul(b, lower[1], bset->eq[j][0]);
762 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
763 isl_seq_cpy(lower, bset->eq[j], 2);
764 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
765 isl_seq_neg(lower, bset->eq[j], 2);
767 if (upper) {
768 isl_int_mul(a, upper[0], bset->eq[j][1]);
769 isl_int_mul(b, upper[1], bset->eq[j][0]);
770 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
771 isl_seq_neg(upper, bset->eq[j], 2);
772 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
773 isl_seq_cpy(upper, bset->eq[j], 2);
776 for (j = 0; j < bset->n_ineq; ++j) {
777 if (isl_int_is_pos(bset->ineq[j][1]))
778 has_lower = 1;
779 if (isl_int_is_neg(bset->ineq[j][1]))
780 has_upper = 1;
781 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
782 isl_int_mul(a, lower[0], bset->ineq[j][1]);
783 isl_int_mul(b, lower[1], bset->ineq[j][0]);
784 if (isl_int_lt(a, b))
785 isl_seq_cpy(lower, bset->ineq[j], 2);
787 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
788 isl_int_mul(a, upper[0], bset->ineq[j][1]);
789 isl_int_mul(b, upper[1], bset->ineq[j][0]);
790 if (isl_int_gt(a, b))
791 isl_seq_cpy(upper, bset->ineq[j], 2);
794 if (!has_lower)
795 lower = NULL;
796 if (!has_upper)
797 upper = NULL;
799 isl_int_clear(a);
800 isl_int_clear(b);
802 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
803 hull = isl_basic_set_set_rational(hull);
804 if (!hull)
805 goto error;
806 if (lower) {
807 k = isl_basic_set_alloc_inequality(hull);
808 isl_seq_cpy(hull->ineq[k], lower, 2);
810 if (upper) {
811 k = isl_basic_set_alloc_inequality(hull);
812 isl_seq_cpy(hull->ineq[k], upper, 2);
814 hull = isl_basic_set_finalize(hull);
815 isl_set_free(set);
816 isl_mat_free(c);
817 return hull;
818 error:
819 isl_set_free(set);
820 isl_mat_free(c);
821 return NULL;
824 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
826 struct isl_basic_set *convex_hull;
828 if (!set)
829 return NULL;
831 if (isl_set_is_empty(set))
832 convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
833 else
834 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
835 isl_set_free(set);
836 return convex_hull;
839 /* Compute the convex hull of a pair of basic sets without any parameters or
840 * integer divisions using Fourier-Motzkin elimination.
841 * The convex hull is the set of all points that can be written as
842 * the sum of points from both basic sets (in homogeneous coordinates).
843 * We set up the constraints in a space with dimensions for each of
844 * the three sets and then project out the dimensions corresponding
845 * to the two original basic sets, retaining only those corresponding
846 * to the convex hull.
848 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
849 struct isl_basic_set *bset2)
851 int i, j, k;
852 struct isl_basic_set *bset[2];
853 struct isl_basic_set *hull = NULL;
854 unsigned dim;
856 if (!bset1 || !bset2)
857 goto error;
859 dim = isl_basic_set_n_dim(bset1);
860 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
861 1 + dim + bset1->n_eq + bset2->n_eq,
862 2 + bset1->n_ineq + bset2->n_ineq);
863 bset[0] = bset1;
864 bset[1] = bset2;
865 for (i = 0; i < 2; ++i) {
866 for (j = 0; j < bset[i]->n_eq; ++j) {
867 k = isl_basic_set_alloc_equality(hull);
868 if (k < 0)
869 goto error;
870 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
871 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
872 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
873 1+dim);
875 for (j = 0; j < bset[i]->n_ineq; ++j) {
876 k = isl_basic_set_alloc_inequality(hull);
877 if (k < 0)
878 goto error;
879 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
880 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
881 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
882 bset[i]->ineq[j], 1+dim);
884 k = isl_basic_set_alloc_inequality(hull);
885 if (k < 0)
886 goto error;
887 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
888 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
890 for (j = 0; j < 1+dim; ++j) {
891 k = isl_basic_set_alloc_equality(hull);
892 if (k < 0)
893 goto error;
894 isl_seq_clr(hull->eq[k], 1+2+3*dim);
895 isl_int_set_si(hull->eq[k][j], -1);
896 isl_int_set_si(hull->eq[k][1+dim+j], 1);
897 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
899 hull = isl_basic_set_set_rational(hull);
900 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
901 hull = isl_basic_set_remove_redundancies(hull);
902 isl_basic_set_free(bset1);
903 isl_basic_set_free(bset2);
904 return hull;
905 error:
906 isl_basic_set_free(bset1);
907 isl_basic_set_free(bset2);
908 isl_basic_set_free(hull);
909 return NULL;
912 /* Is the set bounded for each value of the parameters?
914 int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
916 struct isl_tab *tab;
917 int bounded;
919 if (!bset)
920 return -1;
921 if (isl_basic_set_plain_is_empty(bset))
922 return 1;
924 tab = isl_tab_from_recession_cone(bset, 1);
925 bounded = isl_tab_cone_is_bounded(tab);
926 isl_tab_free(tab);
927 return bounded;
930 /* Is the image bounded for each value of the parameters and
931 * the domain variables?
933 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
935 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
936 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
937 int bounded;
939 bmap = isl_basic_map_copy(bmap);
940 bmap = isl_basic_map_cow(bmap);
941 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
942 isl_dim_in, 0, n_in);
943 bounded = isl_basic_set_is_bounded(bset_from_bmap(bmap));
944 isl_basic_map_free(bmap);
946 return bounded;
949 /* Is the set bounded for each value of the parameters?
951 int isl_set_is_bounded(__isl_keep isl_set *set)
953 int i;
955 if (!set)
956 return -1;
958 for (i = 0; i < set->n; ++i) {
959 int bounded = isl_basic_set_is_bounded(set->p[i]);
960 if (!bounded || bounded < 0)
961 return bounded;
963 return 1;
966 /* Compute the lineality space of the convex hull of bset1 and bset2.
968 * We first compute the intersection of the recession cone of bset1
969 * with the negative of the recession cone of bset2 and then compute
970 * the linear hull of the resulting cone.
972 static struct isl_basic_set *induced_lineality_space(
973 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
975 int i, k;
976 struct isl_basic_set *lin = NULL;
977 unsigned dim;
979 if (!bset1 || !bset2)
980 goto error;
982 dim = isl_basic_set_total_dim(bset1);
983 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
984 bset1->n_eq + bset2->n_eq,
985 bset1->n_ineq + bset2->n_ineq);
986 lin = isl_basic_set_set_rational(lin);
987 if (!lin)
988 goto error;
989 for (i = 0; i < bset1->n_eq; ++i) {
990 k = isl_basic_set_alloc_equality(lin);
991 if (k < 0)
992 goto error;
993 isl_int_set_si(lin->eq[k][0], 0);
994 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
996 for (i = 0; i < bset1->n_ineq; ++i) {
997 k = isl_basic_set_alloc_inequality(lin);
998 if (k < 0)
999 goto error;
1000 isl_int_set_si(lin->ineq[k][0], 0);
1001 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
1003 for (i = 0; i < bset2->n_eq; ++i) {
1004 k = isl_basic_set_alloc_equality(lin);
1005 if (k < 0)
1006 goto error;
1007 isl_int_set_si(lin->eq[k][0], 0);
1008 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
1010 for (i = 0; i < bset2->n_ineq; ++i) {
1011 k = isl_basic_set_alloc_inequality(lin);
1012 if (k < 0)
1013 goto error;
1014 isl_int_set_si(lin->ineq[k][0], 0);
1015 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
1018 isl_basic_set_free(bset1);
1019 isl_basic_set_free(bset2);
1020 return isl_basic_set_affine_hull(lin);
1021 error:
1022 isl_basic_set_free(lin);
1023 isl_basic_set_free(bset1);
1024 isl_basic_set_free(bset2);
1025 return NULL;
1028 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1030 /* Given a set and a linear space "lin" of dimension n > 0,
1031 * project the linear space from the set, compute the convex hull
1032 * and then map the set back to the original space.
1034 * Let
1036 * M x = 0
1038 * describe the linear space. We first compute the Hermite normal
1039 * form H = M U of M = H Q, to obtain
1041 * H Q x = 0
1043 * The last n rows of H will be zero, so the last n variables of x' = Q x
1044 * are the one we want to project out. We do this by transforming each
1045 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1046 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1047 * we transform the hull back to the original space as A' Q_1 x >= b',
1048 * with Q_1 all but the last n rows of Q.
1050 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1051 struct isl_basic_set *lin)
1053 unsigned total = isl_basic_set_total_dim(lin);
1054 unsigned lin_dim;
1055 struct isl_basic_set *hull;
1056 struct isl_mat *M, *U, *Q;
1058 if (!set || !lin)
1059 goto error;
1060 lin_dim = total - lin->n_eq;
1061 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1062 M = isl_mat_left_hermite(M, 0, &U, &Q);
1063 if (!M)
1064 goto error;
1065 isl_mat_free(M);
1066 isl_basic_set_free(lin);
1068 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1070 U = isl_mat_lin_to_aff(U);
1071 Q = isl_mat_lin_to_aff(Q);
1073 set = isl_set_preimage(set, U);
1074 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1075 hull = uset_convex_hull(set);
1076 hull = isl_basic_set_preimage(hull, Q);
1078 return hull;
1079 error:
1080 isl_basic_set_free(lin);
1081 isl_set_free(set);
1082 return NULL;
1085 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1086 * set up an LP for solving
1088 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1090 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1091 * The next \alpha{ij} correspond to the equalities and come in pairs.
1092 * The final \alpha{ij} correspond to the inequalities.
1094 static struct isl_basic_set *valid_direction_lp(
1095 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1097 isl_space *dim;
1098 struct isl_basic_set *lp;
1099 unsigned d;
1100 int n;
1101 int i, j, k;
1103 if (!bset1 || !bset2)
1104 goto error;
1105 d = 1 + isl_basic_set_total_dim(bset1);
1106 n = 2 +
1107 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1108 dim = isl_space_set_alloc(bset1->ctx, 0, n);
1109 lp = isl_basic_set_alloc_space(dim, 0, d, n);
1110 if (!lp)
1111 goto error;
1112 for (i = 0; i < n; ++i) {
1113 k = isl_basic_set_alloc_inequality(lp);
1114 if (k < 0)
1115 goto error;
1116 isl_seq_clr(lp->ineq[k] + 1, n);
1117 isl_int_set_si(lp->ineq[k][0], -1);
1118 isl_int_set_si(lp->ineq[k][1 + i], 1);
1120 for (i = 0; i < d; ++i) {
1121 k = isl_basic_set_alloc_equality(lp);
1122 if (k < 0)
1123 goto error;
1124 n = 0;
1125 isl_int_set_si(lp->eq[k][n], 0); n++;
1126 /* positivity constraint 1 >= 0 */
1127 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1128 for (j = 0; j < bset1->n_eq; ++j) {
1129 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1130 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1132 for (j = 0; j < bset1->n_ineq; ++j) {
1133 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1135 /* positivity constraint 1 >= 0 */
1136 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1137 for (j = 0; j < bset2->n_eq; ++j) {
1138 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1139 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1141 for (j = 0; j < bset2->n_ineq; ++j) {
1142 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1145 lp = isl_basic_set_gauss(lp, NULL);
1146 isl_basic_set_free(bset1);
1147 isl_basic_set_free(bset2);
1148 return lp;
1149 error:
1150 isl_basic_set_free(bset1);
1151 isl_basic_set_free(bset2);
1152 return NULL;
1155 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1156 * for all rays in the homogeneous space of the two cones that correspond
1157 * to the input polyhedra bset1 and bset2.
1159 * We compute s as a vector that satisfies
1161 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1163 * with h_{ij} the normals of the facets of polyhedron i
1164 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1165 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1166 * We first set up an LP with as variables the \alpha{ij}.
1167 * In this formulation, for each polyhedron i,
1168 * the first constraint is the positivity constraint, followed by pairs
1169 * of variables for the equalities, followed by variables for the inequalities.
1170 * We then simply pick a feasible solution and compute s using (*).
1172 * Note that we simply pick any valid direction and make no attempt
1173 * to pick a "good" or even the "best" valid direction.
1175 static struct isl_vec *valid_direction(
1176 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1178 struct isl_basic_set *lp;
1179 struct isl_tab *tab;
1180 struct isl_vec *sample = NULL;
1181 struct isl_vec *dir;
1182 unsigned d;
1183 int i;
1184 int n;
1186 if (!bset1 || !bset2)
1187 goto error;
1188 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1189 isl_basic_set_copy(bset2));
1190 tab = isl_tab_from_basic_set(lp, 0);
1191 sample = isl_tab_get_sample_value(tab);
1192 isl_tab_free(tab);
1193 isl_basic_set_free(lp);
1194 if (!sample)
1195 goto error;
1196 d = isl_basic_set_total_dim(bset1);
1197 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1198 if (!dir)
1199 goto error;
1200 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1201 n = 1;
1202 /* positivity constraint 1 >= 0 */
1203 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1204 for (i = 0; i < bset1->n_eq; ++i) {
1205 isl_int_sub(sample->block.data[n],
1206 sample->block.data[n], sample->block.data[n+1]);
1207 isl_seq_combine(dir->block.data,
1208 bset1->ctx->one, dir->block.data,
1209 sample->block.data[n], bset1->eq[i], 1 + d);
1211 n += 2;
1213 for (i = 0; i < bset1->n_ineq; ++i)
1214 isl_seq_combine(dir->block.data,
1215 bset1->ctx->one, dir->block.data,
1216 sample->block.data[n++], bset1->ineq[i], 1 + d);
1217 isl_vec_free(sample);
1218 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1219 isl_basic_set_free(bset1);
1220 isl_basic_set_free(bset2);
1221 return dir;
1222 error:
1223 isl_vec_free(sample);
1224 isl_basic_set_free(bset1);
1225 isl_basic_set_free(bset2);
1226 return NULL;
1229 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1230 * compute b_i' + A_i' x' >= 0, with
1232 * [ b_i A_i ] [ y' ] [ y' ]
1233 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1235 * In particular, add the "positivity constraint" and then perform
1236 * the mapping.
1238 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1239 struct isl_mat *T)
1241 int k;
1243 if (!bset)
1244 goto error;
1245 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1246 k = isl_basic_set_alloc_inequality(bset);
1247 if (k < 0)
1248 goto error;
1249 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1250 isl_int_set_si(bset->ineq[k][0], 1);
1251 bset = isl_basic_set_preimage(bset, T);
1252 return bset;
1253 error:
1254 isl_mat_free(T);
1255 isl_basic_set_free(bset);
1256 return NULL;
1259 /* Compute the convex hull of a pair of basic sets without any parameters or
1260 * integer divisions, where the convex hull is known to be pointed,
1261 * but the basic sets may be unbounded.
1263 * We turn this problem into the computation of a convex hull of a pair
1264 * _bounded_ polyhedra by "changing the direction of the homogeneous
1265 * dimension". This idea is due to Matthias Koeppe.
1267 * Consider the cones in homogeneous space that correspond to the
1268 * input polyhedra. The rays of these cones are also rays of the
1269 * polyhedra if the coordinate that corresponds to the homogeneous
1270 * dimension is zero. That is, if the inner product of the rays
1271 * with the homogeneous direction is zero.
1272 * The cones in the homogeneous space can also be considered to
1273 * correspond to other pairs of polyhedra by chosing a different
1274 * homogeneous direction. To ensure that both of these polyhedra
1275 * are bounded, we need to make sure that all rays of the cones
1276 * correspond to vertices and not to rays.
1277 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1278 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1279 * The vector s is computed in valid_direction.
1281 * Note that we need to consider _all_ rays of the cones and not just
1282 * the rays that correspond to rays in the polyhedra. If we were to
1283 * only consider those rays and turn them into vertices, then we
1284 * may inadvertently turn some vertices into rays.
1286 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1287 * We therefore transform the two polyhedra such that the selected
1288 * direction is mapped onto this standard direction and then proceed
1289 * with the normal computation.
1290 * Let S be a non-singular square matrix with s as its first row,
1291 * then we want to map the polyhedra to the space
1293 * [ y' ] [ y ] [ y ] [ y' ]
1294 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1296 * We take S to be the unimodular completion of s to limit the growth
1297 * of the coefficients in the following computations.
1299 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1300 * We first move to the homogeneous dimension
1302 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1303 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1305 * Then we change directoin
1307 * [ b_i A_i ] [ y' ] [ y' ]
1308 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1310 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1311 * resulting in b' + A' x' >= 0, which we then convert back
1313 * [ y ] [ y ]
1314 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1316 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1318 static struct isl_basic_set *convex_hull_pair_pointed(
1319 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1321 struct isl_ctx *ctx = NULL;
1322 struct isl_vec *dir = NULL;
1323 struct isl_mat *T = NULL;
1324 struct isl_mat *T2 = NULL;
1325 struct isl_basic_set *hull;
1326 struct isl_set *set;
1328 if (!bset1 || !bset2)
1329 goto error;
1330 ctx = isl_basic_set_get_ctx(bset1);
1331 dir = valid_direction(isl_basic_set_copy(bset1),
1332 isl_basic_set_copy(bset2));
1333 if (!dir)
1334 goto error;
1335 T = isl_mat_alloc(ctx, dir->size, dir->size);
1336 if (!T)
1337 goto error;
1338 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1339 T = isl_mat_unimodular_complete(T, 1);
1340 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1342 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1343 bset2 = homogeneous_map(bset2, T2);
1344 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1345 set = isl_set_add_basic_set(set, bset1);
1346 set = isl_set_add_basic_set(set, bset2);
1347 hull = uset_convex_hull(set);
1348 hull = isl_basic_set_preimage(hull, T);
1350 isl_vec_free(dir);
1352 return hull;
1353 error:
1354 isl_vec_free(dir);
1355 isl_basic_set_free(bset1);
1356 isl_basic_set_free(bset2);
1357 return NULL;
1360 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1361 static struct isl_basic_set *modulo_affine_hull(
1362 struct isl_set *set, struct isl_basic_set *affine_hull);
1364 /* Compute the convex hull of a pair of basic sets without any parameters or
1365 * integer divisions.
1367 * This function is called from uset_convex_hull_unbounded, which
1368 * means that the complete convex hull is unbounded. Some pairs
1369 * of basic sets may still be bounded, though.
1370 * They may even lie inside a lower dimensional space, in which
1371 * case they need to be handled inside their affine hull since
1372 * the main algorithm assumes that the result is full-dimensional.
1374 * If the convex hull of the two basic sets would have a non-trivial
1375 * lineality space, we first project out this lineality space.
1377 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1378 struct isl_basic_set *bset2)
1380 isl_basic_set *lin, *aff;
1381 int bounded1, bounded2;
1383 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1384 return convex_hull_pair_elim(bset1, bset2);
1386 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1387 isl_basic_set_copy(bset2)));
1388 if (!aff)
1389 goto error;
1390 if (aff->n_eq != 0)
1391 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1392 isl_basic_set_free(aff);
1394 bounded1 = isl_basic_set_is_bounded(bset1);
1395 bounded2 = isl_basic_set_is_bounded(bset2);
1397 if (bounded1 < 0 || bounded2 < 0)
1398 goto error;
1400 if (bounded1 && bounded2)
1401 return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1403 if (bounded1 || bounded2)
1404 return convex_hull_pair_pointed(bset1, bset2);
1406 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1407 isl_basic_set_copy(bset2));
1408 if (!lin)
1409 goto error;
1410 if (isl_basic_set_plain_is_universe(lin)) {
1411 isl_basic_set_free(bset1);
1412 isl_basic_set_free(bset2);
1413 return lin;
1415 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1416 struct isl_set *set;
1417 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1418 set = isl_set_add_basic_set(set, bset1);
1419 set = isl_set_add_basic_set(set, bset2);
1420 return modulo_lineality(set, lin);
1422 isl_basic_set_free(lin);
1424 return convex_hull_pair_pointed(bset1, bset2);
1425 error:
1426 isl_basic_set_free(bset1);
1427 isl_basic_set_free(bset2);
1428 return NULL;
1431 /* Compute the lineality space of a basic set.
1432 * We currently do not allow the basic set to have any divs.
1433 * We basically just drop the constants and turn every inequality
1434 * into an equality.
1436 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1438 int i, k;
1439 struct isl_basic_set *lin = NULL;
1440 unsigned dim;
1442 if (!bset)
1443 goto error;
1444 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1445 dim = isl_basic_set_total_dim(bset);
1447 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, dim, 0);
1448 if (!lin)
1449 goto error;
1450 for (i = 0; i < bset->n_eq; ++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->eq[i] + 1, dim);
1457 lin = isl_basic_set_gauss(lin, NULL);
1458 if (!lin)
1459 goto error;
1460 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1461 k = isl_basic_set_alloc_equality(lin);
1462 if (k < 0)
1463 goto error;
1464 isl_int_set_si(lin->eq[k][0], 0);
1465 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1466 lin = isl_basic_set_gauss(lin, NULL);
1467 if (!lin)
1468 goto error;
1470 isl_basic_set_free(bset);
1471 return lin;
1472 error:
1473 isl_basic_set_free(lin);
1474 isl_basic_set_free(bset);
1475 return NULL;
1478 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1479 * "underlying" set "set".
1481 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1483 int i;
1484 struct isl_set *lin = NULL;
1486 if (!set)
1487 return NULL;
1488 if (set->n == 0) {
1489 isl_space *dim = isl_set_get_space(set);
1490 isl_set_free(set);
1491 return isl_basic_set_empty(dim);
1494 lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
1495 for (i = 0; i < set->n; ++i)
1496 lin = isl_set_add_basic_set(lin,
1497 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1498 isl_set_free(set);
1499 return isl_set_affine_hull(lin);
1502 /* Compute the convex hull of a set without any parameters or
1503 * integer divisions.
1504 * In each step, we combined two basic sets until only one
1505 * basic set is left.
1506 * The input basic sets are assumed not to have a non-trivial
1507 * lineality space. If any of the intermediate results has
1508 * a non-trivial lineality space, it is projected out.
1510 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1512 struct isl_basic_set *convex_hull = NULL;
1514 convex_hull = isl_set_copy_basic_set(set);
1515 set = isl_set_drop_basic_set(set, convex_hull);
1516 if (!set)
1517 goto error;
1518 while (set->n > 0) {
1519 struct isl_basic_set *t;
1520 t = isl_set_copy_basic_set(set);
1521 if (!t)
1522 goto error;
1523 set = isl_set_drop_basic_set(set, t);
1524 if (!set)
1525 goto error;
1526 convex_hull = convex_hull_pair(convex_hull, t);
1527 if (set->n == 0)
1528 break;
1529 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1530 if (!t)
1531 goto error;
1532 if (isl_basic_set_plain_is_universe(t)) {
1533 isl_basic_set_free(convex_hull);
1534 convex_hull = t;
1535 break;
1537 if (t->n_eq < isl_basic_set_total_dim(t)) {
1538 convex_hull = isl_basic_set_underlying_set(convex_hull);
1539 set = isl_set_add_basic_set(set, convex_hull);
1540 return modulo_lineality(set, t);
1542 isl_basic_set_free(t);
1544 isl_set_free(set);
1545 return convex_hull;
1546 error:
1547 isl_set_free(set);
1548 isl_basic_set_free(convex_hull);
1549 return NULL;
1552 /* Compute an initial hull for wrapping containing a single initial
1553 * facet.
1554 * This function assumes that the given set is bounded.
1556 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1557 struct isl_set *set)
1559 struct isl_mat *bounds = NULL;
1560 unsigned dim;
1561 int k;
1563 if (!hull)
1564 goto error;
1565 bounds = initial_facet_constraint(set);
1566 if (!bounds)
1567 goto error;
1568 k = isl_basic_set_alloc_inequality(hull);
1569 if (k < 0)
1570 goto error;
1571 dim = isl_set_n_dim(set);
1572 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1573 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1574 isl_mat_free(bounds);
1576 return hull;
1577 error:
1578 isl_basic_set_free(hull);
1579 isl_mat_free(bounds);
1580 return NULL;
1583 struct max_constraint {
1584 struct isl_mat *c;
1585 int count;
1586 int ineq;
1589 static int max_constraint_equal(const void *entry, const void *val)
1591 struct max_constraint *a = (struct max_constraint *)entry;
1592 isl_int *b = (isl_int *)val;
1594 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1597 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1598 isl_int *con, unsigned len, int n, int ineq)
1600 struct isl_hash_table_entry *entry;
1601 struct max_constraint *c;
1602 uint32_t c_hash;
1604 c_hash = isl_seq_get_hash(con + 1, len);
1605 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1606 con + 1, 0);
1607 if (!entry)
1608 return;
1609 c = entry->data;
1610 if (c->count < n) {
1611 isl_hash_table_remove(ctx, table, entry);
1612 return;
1614 c->count++;
1615 if (isl_int_gt(c->c->row[0][0], con[0]))
1616 return;
1617 if (isl_int_eq(c->c->row[0][0], con[0])) {
1618 if (ineq)
1619 c->ineq = ineq;
1620 return;
1622 c->c = isl_mat_cow(c->c);
1623 isl_int_set(c->c->row[0][0], con[0]);
1624 c->ineq = ineq;
1627 /* Check whether the constraint hash table "table" constains the constraint
1628 * "con".
1630 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1631 isl_int *con, unsigned len, int n)
1633 struct isl_hash_table_entry *entry;
1634 struct max_constraint *c;
1635 uint32_t c_hash;
1637 c_hash = isl_seq_get_hash(con + 1, len);
1638 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1639 con + 1, 0);
1640 if (!entry)
1641 return 0;
1642 c = entry->data;
1643 if (c->count < n)
1644 return 0;
1645 return isl_int_eq(c->c->row[0][0], con[0]);
1648 /* Check for inequality constraints of a basic set without equalities
1649 * such that the same or more stringent copies of the constraint appear
1650 * in all of the basic sets. Such constraints are necessarily facet
1651 * constraints of the convex hull.
1653 * If the resulting basic set is by chance identical to one of
1654 * the basic sets in "set", then we know that this basic set contains
1655 * all other basic sets and is therefore the convex hull of set.
1656 * In this case we set *is_hull to 1.
1658 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1659 struct isl_set *set, int *is_hull)
1661 int i, j, s, n;
1662 int min_constraints;
1663 int best;
1664 struct max_constraint *constraints = NULL;
1665 struct isl_hash_table *table = NULL;
1666 unsigned total;
1668 *is_hull = 0;
1670 for (i = 0; i < set->n; ++i)
1671 if (set->p[i]->n_eq == 0)
1672 break;
1673 if (i >= set->n)
1674 return hull;
1675 min_constraints = set->p[i]->n_ineq;
1676 best = i;
1677 for (i = best + 1; i < set->n; ++i) {
1678 if (set->p[i]->n_eq != 0)
1679 continue;
1680 if (set->p[i]->n_ineq >= min_constraints)
1681 continue;
1682 min_constraints = set->p[i]->n_ineq;
1683 best = i;
1685 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1686 min_constraints);
1687 if (!constraints)
1688 return hull;
1689 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1690 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1691 goto error;
1693 total = isl_space_dim(set->dim, isl_dim_all);
1694 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1695 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1696 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1697 if (!constraints[i].c)
1698 goto error;
1699 constraints[i].ineq = 1;
1701 for (i = 0; i < min_constraints; ++i) {
1702 struct isl_hash_table_entry *entry;
1703 uint32_t c_hash;
1704 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1705 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1706 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1707 if (!entry)
1708 goto error;
1709 isl_assert(hull->ctx, !entry->data, goto error);
1710 entry->data = &constraints[i];
1713 n = 0;
1714 for (s = 0; s < set->n; ++s) {
1715 if (s == best)
1716 continue;
1718 for (i = 0; i < set->p[s]->n_eq; ++i) {
1719 isl_int *eq = set->p[s]->eq[i];
1720 for (j = 0; j < 2; ++j) {
1721 isl_seq_neg(eq, eq, 1 + total);
1722 update_constraint(hull->ctx, table,
1723 eq, total, n, 0);
1726 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1727 isl_int *ineq = set->p[s]->ineq[i];
1728 update_constraint(hull->ctx, table, ineq, total, n,
1729 set->p[s]->n_eq == 0);
1731 ++n;
1734 for (i = 0; i < min_constraints; ++i) {
1735 if (constraints[i].count < n)
1736 continue;
1737 if (!constraints[i].ineq)
1738 continue;
1739 j = isl_basic_set_alloc_inequality(hull);
1740 if (j < 0)
1741 goto error;
1742 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1745 for (s = 0; s < set->n; ++s) {
1746 if (set->p[s]->n_eq)
1747 continue;
1748 if (set->p[s]->n_ineq != hull->n_ineq)
1749 continue;
1750 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1751 isl_int *ineq = set->p[s]->ineq[i];
1752 if (!has_constraint(hull->ctx, table, ineq, total, n))
1753 break;
1755 if (i == set->p[s]->n_ineq)
1756 *is_hull = 1;
1759 isl_hash_table_clear(table);
1760 for (i = 0; i < min_constraints; ++i)
1761 isl_mat_free(constraints[i].c);
1762 free(constraints);
1763 free(table);
1764 return hull;
1765 error:
1766 isl_hash_table_clear(table);
1767 free(table);
1768 if (constraints)
1769 for (i = 0; i < min_constraints; ++i)
1770 isl_mat_free(constraints[i].c);
1771 free(constraints);
1772 return hull;
1775 /* Create a template for the convex hull of "set" and fill it up
1776 * obvious facet constraints, if any. If the result happens to
1777 * be the convex hull of "set" then *is_hull is set to 1.
1779 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1781 struct isl_basic_set *hull;
1782 unsigned n_ineq;
1783 int i;
1785 n_ineq = 1;
1786 for (i = 0; i < set->n; ++i) {
1787 n_ineq += set->p[i]->n_eq;
1788 n_ineq += set->p[i]->n_ineq;
1790 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1791 hull = isl_basic_set_set_rational(hull);
1792 if (!hull)
1793 return NULL;
1794 return common_constraints(hull, set, is_hull);
1797 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1799 struct isl_basic_set *hull;
1800 int is_hull;
1802 hull = proto_hull(set, &is_hull);
1803 if (hull && !is_hull) {
1804 if (hull->n_ineq == 0)
1805 hull = initial_hull(hull, set);
1806 hull = extend(hull, set);
1808 isl_set_free(set);
1810 return hull;
1813 /* Compute the convex hull of a set without any parameters or
1814 * integer divisions. Depending on whether the set is bounded,
1815 * we pass control to the wrapping based convex hull or
1816 * the Fourier-Motzkin elimination based convex hull.
1817 * We also handle a few special cases before checking the boundedness.
1819 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1821 struct isl_basic_set *convex_hull = NULL;
1822 struct isl_basic_set *lin;
1824 if (isl_set_n_dim(set) == 0)
1825 return convex_hull_0d(set);
1827 set = isl_set_coalesce(set);
1828 set = isl_set_set_rational(set);
1830 if (!set)
1831 goto error;
1832 if (!set)
1833 return NULL;
1834 if (set->n == 1) {
1835 convex_hull = isl_basic_set_copy(set->p[0]);
1836 isl_set_free(set);
1837 return convex_hull;
1839 if (isl_set_n_dim(set) == 1)
1840 return convex_hull_1d(set);
1842 if (isl_set_is_bounded(set) &&
1843 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1844 return uset_convex_hull_wrap(set);
1846 lin = uset_combined_lineality_space(isl_set_copy(set));
1847 if (!lin)
1848 goto error;
1849 if (isl_basic_set_plain_is_universe(lin)) {
1850 isl_set_free(set);
1851 return lin;
1853 if (lin->n_eq < isl_basic_set_total_dim(lin))
1854 return modulo_lineality(set, lin);
1855 isl_basic_set_free(lin);
1857 return uset_convex_hull_unbounded(set);
1858 error:
1859 isl_set_free(set);
1860 isl_basic_set_free(convex_hull);
1861 return NULL;
1864 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1865 * without parameters or divs and where the convex hull of set is
1866 * known to be full-dimensional.
1868 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1870 struct isl_basic_set *convex_hull = NULL;
1872 if (!set)
1873 goto error;
1875 if (isl_set_n_dim(set) == 0) {
1876 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1877 isl_set_free(set);
1878 convex_hull = isl_basic_set_set_rational(convex_hull);
1879 return convex_hull;
1882 set = isl_set_set_rational(set);
1883 set = isl_set_coalesce(set);
1884 if (!set)
1885 goto error;
1886 if (set->n == 1) {
1887 convex_hull = isl_basic_set_copy(set->p[0]);
1888 isl_set_free(set);
1889 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1890 return convex_hull;
1892 if (isl_set_n_dim(set) == 1)
1893 return convex_hull_1d(set);
1895 return uset_convex_hull_wrap(set);
1896 error:
1897 isl_set_free(set);
1898 return NULL;
1901 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1902 * We first remove the equalities (transforming the set), compute the
1903 * convex hull of the transformed set and then add the equalities back
1904 * (after performing the inverse transformation.
1906 static struct isl_basic_set *modulo_affine_hull(
1907 struct isl_set *set, struct isl_basic_set *affine_hull)
1909 struct isl_mat *T;
1910 struct isl_mat *T2;
1911 struct isl_basic_set *dummy;
1912 struct isl_basic_set *convex_hull;
1914 dummy = isl_basic_set_remove_equalities(
1915 isl_basic_set_copy(affine_hull), &T, &T2);
1916 if (!dummy)
1917 goto error;
1918 isl_basic_set_free(dummy);
1919 set = isl_set_preimage(set, T);
1920 convex_hull = uset_convex_hull(set);
1921 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1922 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1923 return convex_hull;
1924 error:
1925 isl_basic_set_free(affine_hull);
1926 isl_set_free(set);
1927 return NULL;
1930 /* Return an empty basic map living in the same space as "map".
1932 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1933 __isl_take isl_map *map)
1935 isl_space *space;
1937 space = isl_map_get_space(map);
1938 isl_map_free(map);
1939 return isl_basic_map_empty(space);
1942 /* Compute the convex hull of a map.
1944 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1945 * specifically, the wrapping of facets to obtain new facets.
1947 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1949 struct isl_basic_set *bset;
1950 struct isl_basic_map *model = NULL;
1951 struct isl_basic_set *affine_hull = NULL;
1952 struct isl_basic_map *convex_hull = NULL;
1953 struct isl_set *set = NULL;
1955 map = isl_map_detect_equalities(map);
1956 map = isl_map_align_divs(map);
1957 if (!map)
1958 goto error;
1960 if (map->n == 0)
1961 return replace_map_by_empty_basic_map(map);
1963 model = isl_basic_map_copy(map->p[0]);
1964 set = isl_map_underlying_set(map);
1965 if (!set)
1966 goto error;
1968 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1969 if (!affine_hull)
1970 goto error;
1971 if (affine_hull->n_eq != 0)
1972 bset = modulo_affine_hull(set, affine_hull);
1973 else {
1974 isl_basic_set_free(affine_hull);
1975 bset = uset_convex_hull(set);
1978 convex_hull = isl_basic_map_overlying_set(bset, model);
1979 if (!convex_hull)
1980 return NULL;
1982 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1983 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1984 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1985 return convex_hull;
1986 error:
1987 isl_set_free(set);
1988 isl_basic_map_free(model);
1989 return NULL;
1992 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1994 return bset_from_bmap(isl_map_convex_hull(set_to_map(set)));
1997 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1999 isl_basic_map *hull;
2001 hull = isl_map_convex_hull(map);
2002 return isl_basic_map_remove_divs(hull);
2005 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
2007 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set)));
2010 struct sh_data_entry {
2011 struct isl_hash_table *table;
2012 struct isl_tab *tab;
2015 /* Holds the data needed during the simple hull computation.
2016 * In particular,
2017 * n the number of basic sets in the original set
2018 * hull_table a hash table of already computed constraints
2019 * in the simple hull
2020 * p for each basic set,
2021 * table a hash table of the constraints
2022 * tab the tableau corresponding to the basic set
2024 struct sh_data {
2025 struct isl_ctx *ctx;
2026 unsigned n;
2027 struct isl_hash_table *hull_table;
2028 struct sh_data_entry p[1];
2031 static void sh_data_free(struct sh_data *data)
2033 int i;
2035 if (!data)
2036 return;
2037 isl_hash_table_free(data->ctx, data->hull_table);
2038 for (i = 0; i < data->n; ++i) {
2039 isl_hash_table_free(data->ctx, data->p[i].table);
2040 isl_tab_free(data->p[i].tab);
2042 free(data);
2045 struct ineq_cmp_data {
2046 unsigned len;
2047 isl_int *p;
2050 static int has_ineq(const void *entry, const void *val)
2052 isl_int *row = (isl_int *)entry;
2053 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2055 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2056 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2059 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2060 isl_int *ineq, unsigned len)
2062 uint32_t c_hash;
2063 struct ineq_cmp_data v;
2064 struct isl_hash_table_entry *entry;
2066 v.len = len;
2067 v.p = ineq;
2068 c_hash = isl_seq_get_hash(ineq + 1, len);
2069 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2070 if (!entry)
2071 return - 1;
2072 entry->data = ineq;
2073 return 0;
2076 /* Fill hash table "table" with the constraints of "bset".
2077 * Equalities are added as two inequalities.
2078 * The value in the hash table is a pointer to the (in)equality of "bset".
2080 static int hash_basic_set(struct isl_hash_table *table,
2081 struct isl_basic_set *bset)
2083 int i, j;
2084 unsigned dim = isl_basic_set_total_dim(bset);
2086 for (i = 0; i < bset->n_eq; ++i) {
2087 for (j = 0; j < 2; ++j) {
2088 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2089 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2090 return -1;
2093 for (i = 0; i < bset->n_ineq; ++i) {
2094 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2095 return -1;
2097 return 0;
2100 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2102 struct sh_data *data;
2103 int i;
2105 data = isl_calloc(set->ctx, struct sh_data,
2106 sizeof(struct sh_data) +
2107 (set->n - 1) * sizeof(struct sh_data_entry));
2108 if (!data)
2109 return NULL;
2110 data->ctx = set->ctx;
2111 data->n = set->n;
2112 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2113 if (!data->hull_table)
2114 goto error;
2115 for (i = 0; i < set->n; ++i) {
2116 data->p[i].table = isl_hash_table_alloc(set->ctx,
2117 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2118 if (!data->p[i].table)
2119 goto error;
2120 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2121 goto error;
2123 return data;
2124 error:
2125 sh_data_free(data);
2126 return NULL;
2129 /* Check if inequality "ineq" is a bound for basic set "j" or if
2130 * it can be relaxed (by increasing the constant term) to become
2131 * a bound for that basic set. In the latter case, the constant
2132 * term is updated.
2133 * Relaxation of the constant term is only allowed if "shift" is set.
2135 * Return 1 if "ineq" is a bound
2136 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2137 * -1 if some error occurred
2139 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2140 isl_int *ineq, int shift)
2142 enum isl_lp_result res;
2143 isl_int opt;
2145 if (!data->p[j].tab) {
2146 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2147 if (!data->p[j].tab)
2148 return -1;
2151 isl_int_init(opt);
2153 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2154 &opt, NULL, 0);
2155 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2156 if (shift)
2157 isl_int_sub(ineq[0], ineq[0], opt);
2158 else
2159 res = isl_lp_unbounded;
2162 isl_int_clear(opt);
2164 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2165 res == isl_lp_unbounded ? 0 : -1;
2168 /* Set the constant term of "ineq" to the maximum of those of the constraints
2169 * in the basic sets of "set" following "i" that are parallel to "ineq".
2170 * That is, if any of the basic sets of "set" following "i" have a more
2171 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2172 * "c_hash" is the hash value of the linear part of "ineq".
2173 * "v" has been set up for use by has_ineq.
2175 * Note that the two inequality constraints corresponding to an equality are
2176 * represented by the same inequality constraint in data->p[j].table
2177 * (but with different hash values). This means the constraint (or at
2178 * least its constant term) may need to be temporarily negated to get
2179 * the actually hashed constraint.
2181 static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
2182 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2184 int j;
2185 isl_ctx *ctx;
2186 struct isl_hash_table_entry *entry;
2188 ctx = isl_set_get_ctx(set);
2189 for (j = i + 1; j < set->n; ++j) {
2190 int neg;
2191 isl_int *ineq_j;
2193 entry = isl_hash_table_find(ctx, data->p[j].table,
2194 c_hash, &has_ineq, v, 0);
2195 if (!entry)
2196 continue;
2198 ineq_j = entry->data;
2199 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2200 if (neg)
2201 isl_int_neg(ineq_j[0], ineq_j[0]);
2202 if (isl_int_gt(ineq_j[0], ineq[0]))
2203 isl_int_set(ineq[0], ineq_j[0]);
2204 if (neg)
2205 isl_int_neg(ineq_j[0], ineq_j[0]);
2209 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2210 * become a bound on the whole set. If so, add the (relaxed) inequality
2211 * to "hull". Relaxation is only allowed if "shift" is set.
2213 * We first check if "hull" already contains a translate of the inequality.
2214 * If so, we are done.
2215 * Then, we check if any of the previous basic sets contains a translate
2216 * of the inequality. If so, then we have already considered this
2217 * inequality and we are done.
2218 * Otherwise, for each basic set other than "i", we check if the inequality
2219 * is a bound on the basic set, but first replace the constant term
2220 * by the maximal value of any translate of the inequality in any
2221 * of the following basic sets.
2222 * For previous basic sets, we know that they do not contain a translate
2223 * of the inequality, so we directly call is_bound.
2224 * For following basic sets, we first check if a translate of the
2225 * inequality appears in its description. If so, the constant term
2226 * of the inequality has already been updated with respect to this
2227 * translate and the inequality is therefore known to be a bound
2228 * of this basic set.
2230 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2231 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
2232 int shift)
2234 uint32_t c_hash;
2235 struct ineq_cmp_data v;
2236 struct isl_hash_table_entry *entry;
2237 int j, k;
2239 if (!hull)
2240 return NULL;
2242 v.len = isl_basic_set_total_dim(hull);
2243 v.p = ineq;
2244 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2246 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2247 has_ineq, &v, 0);
2248 if (entry)
2249 return hull;
2251 for (j = 0; j < i; ++j) {
2252 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2253 c_hash, has_ineq, &v, 0);
2254 if (entry)
2255 break;
2257 if (j < i)
2258 return hull;
2260 k = isl_basic_set_alloc_inequality(hull);
2261 if (k < 0)
2262 goto error;
2263 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2265 set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
2266 for (j = 0; j < i; ++j) {
2267 int bound;
2268 bound = is_bound(data, set, j, hull->ineq[k], shift);
2269 if (bound < 0)
2270 goto error;
2271 if (!bound)
2272 break;
2274 if (j < i) {
2275 isl_basic_set_free_inequality(hull, 1);
2276 return hull;
2279 for (j = i + 1; j < set->n; ++j) {
2280 int bound;
2281 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2282 c_hash, has_ineq, &v, 0);
2283 if (entry)
2284 continue;
2285 bound = is_bound(data, set, j, hull->ineq[k], shift);
2286 if (bound < 0)
2287 goto error;
2288 if (!bound)
2289 break;
2291 if (j < set->n) {
2292 isl_basic_set_free_inequality(hull, 1);
2293 return hull;
2296 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2297 has_ineq, &v, 1);
2298 if (!entry)
2299 goto error;
2300 entry->data = hull->ineq[k];
2302 return hull;
2303 error:
2304 isl_basic_set_free(hull);
2305 return NULL;
2308 /* Check if any inequality from basic set "i" is or can be relaxed to
2309 * become a bound on the whole set. If so, add the (relaxed) inequality
2310 * to "hull". Relaxation is only allowed if "shift" is set.
2312 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2313 struct sh_data *data, struct isl_set *set, int i, int shift)
2315 int j, k;
2316 unsigned dim = isl_basic_set_total_dim(bset);
2318 for (j = 0; j < set->p[i]->n_eq; ++j) {
2319 for (k = 0; k < 2; ++k) {
2320 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2321 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2322 shift);
2325 for (j = 0; j < set->p[i]->n_ineq; ++j)
2326 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2327 return bset;
2330 /* Compute a superset of the convex hull of set that is described
2331 * by only (translates of) the constraints in the constituents of set.
2332 * Translation is only allowed if "shift" is set.
2334 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2335 int shift)
2337 struct sh_data *data = NULL;
2338 struct isl_basic_set *hull = NULL;
2339 unsigned n_ineq;
2340 int i;
2342 if (!set)
2343 return NULL;
2345 n_ineq = 0;
2346 for (i = 0; i < set->n; ++i) {
2347 if (!set->p[i])
2348 goto error;
2349 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2352 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2353 if (!hull)
2354 goto error;
2356 data = sh_data_alloc(set, n_ineq);
2357 if (!data)
2358 goto error;
2360 for (i = 0; i < set->n; ++i)
2361 hull = add_bounds(hull, data, set, i, shift);
2363 sh_data_free(data);
2364 isl_set_free(set);
2366 return hull;
2367 error:
2368 sh_data_free(data);
2369 isl_basic_set_free(hull);
2370 isl_set_free(set);
2371 return NULL;
2374 /* Compute a superset of the convex hull of map that is described
2375 * by only (translates of) the constraints in the constituents of map.
2376 * Handle trivial cases where map is NULL or contains at most one disjunct.
2378 static __isl_give isl_basic_map *map_simple_hull_trivial(
2379 __isl_take isl_map *map)
2381 isl_basic_map *hull;
2383 if (!map)
2384 return NULL;
2385 if (map->n == 0)
2386 return replace_map_by_empty_basic_map(map);
2388 hull = isl_basic_map_copy(map->p[0]);
2389 isl_map_free(map);
2390 return hull;
2393 /* Return a copy of the simple hull cached inside "map".
2394 * "shift" determines whether to return the cached unshifted or shifted
2395 * simple hull.
2397 static __isl_give isl_basic_map *cached_simple_hull(__isl_take isl_map *map,
2398 int shift)
2400 isl_basic_map *hull;
2402 hull = isl_basic_map_copy(map->cached_simple_hull[shift]);
2403 isl_map_free(map);
2405 return hull;
2408 /* Compute a superset of the convex hull of map that is described
2409 * by only (translates of) the constraints in the constituents of map.
2410 * Translation is only allowed if "shift" is set.
2412 * The constraints are sorted while removing redundant constraints
2413 * in order to indicate a preference of which constraints should
2414 * be preserved. In particular, pairs of constraints that are
2415 * sorted together are preferred to either both be preserved
2416 * or both be removed. The sorting is performed inside
2417 * isl_basic_map_remove_redundancies.
2419 * The result of the computation is stored in map->cached_simple_hull[shift]
2420 * such that it can be reused in subsequent calls. The cache is cleared
2421 * whenever the map is modified (in isl_map_cow).
2422 * Note that the results need to be stored in the input map for there
2423 * to be any chance that they may get reused. In particular, they
2424 * are stored in a copy of the input map that is saved before
2425 * the integer division alignment.
2427 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2428 int shift)
2430 struct isl_set *set = NULL;
2431 struct isl_basic_map *model = NULL;
2432 struct isl_basic_map *hull;
2433 struct isl_basic_map *affine_hull;
2434 struct isl_basic_set *bset = NULL;
2435 isl_map *input;
2437 if (!map || map->n <= 1)
2438 return map_simple_hull_trivial(map);
2440 if (map->cached_simple_hull[shift])
2441 return cached_simple_hull(map, shift);
2443 map = isl_map_detect_equalities(map);
2444 if (!map || map->n <= 1)
2445 return map_simple_hull_trivial(map);
2446 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2447 input = isl_map_copy(map);
2448 map = isl_map_align_divs(map);
2449 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2451 set = isl_map_underlying_set(map);
2453 bset = uset_simple_hull(set, shift);
2455 hull = isl_basic_map_overlying_set(bset, model);
2457 hull = isl_basic_map_intersect(hull, affine_hull);
2458 hull = isl_basic_map_remove_redundancies(hull);
2460 if (hull) {
2461 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2462 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2465 hull = isl_basic_map_finalize(hull);
2466 if (input)
2467 input->cached_simple_hull[shift] = isl_basic_map_copy(hull);
2468 isl_map_free(input);
2470 return hull;
2473 /* Compute a superset of the convex hull of map that is described
2474 * by only translates of the constraints in the constituents of map.
2476 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2478 return map_simple_hull(map, 1);
2481 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2483 return bset_from_bmap(isl_map_simple_hull(set_to_map(set)));
2486 /* Compute a superset of the convex hull of map that is described
2487 * by only the constraints in the constituents of map.
2489 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2490 __isl_take isl_map *map)
2492 return map_simple_hull(map, 0);
2495 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2496 __isl_take isl_set *set)
2498 return isl_map_unshifted_simple_hull(set);
2501 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2502 * A constraint that appears with different constant terms
2503 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2504 * (i.e., greatest) constant term.
2505 * "bmap1" and "bmap2" are assumed to have the same (known)
2506 * integer divisions.
2507 * The constraints of both "bmap1" and "bmap2" are assumed
2508 * to have been sorted using isl_basic_map_sort_constraints.
2510 * Run through the inequality constraints of "bmap1" and "bmap2"
2511 * in sorted order.
2512 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2513 * is removed.
2514 * If a match is found, the constraint is kept. If needed, the constant
2515 * term of the constraint is adjusted.
2517 static __isl_give isl_basic_map *select_shared_inequalities(
2518 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2520 int i1, i2;
2522 bmap1 = isl_basic_map_cow(bmap1);
2523 if (!bmap1 || !bmap2)
2524 return isl_basic_map_free(bmap1);
2526 i1 = bmap1->n_ineq - 1;
2527 i2 = bmap2->n_ineq - 1;
2528 while (bmap1 && i1 >= 0 && i2 >= 0) {
2529 int cmp;
2531 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2532 bmap2->ineq[i2]);
2533 if (cmp < 0) {
2534 --i2;
2535 continue;
2537 if (cmp > 0) {
2538 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2539 bmap1 = isl_basic_map_free(bmap1);
2540 --i1;
2541 continue;
2543 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2544 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2545 --i1;
2546 --i2;
2548 for (; i1 >= 0; --i1)
2549 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2550 bmap1 = isl_basic_map_free(bmap1);
2552 return bmap1;
2555 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2556 * "bmap1" and "bmap2" are assumed to have the same (known)
2557 * integer divisions.
2559 * Run through the equality constraints of "bmap1" and "bmap2".
2560 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2561 * is removed.
2563 static __isl_give isl_basic_map *select_shared_equalities(
2564 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2566 int i1, i2;
2567 unsigned total;
2569 bmap1 = isl_basic_map_cow(bmap1);
2570 if (!bmap1 || !bmap2)
2571 return isl_basic_map_free(bmap1);
2573 total = isl_basic_map_total_dim(bmap1);
2575 i1 = bmap1->n_eq - 1;
2576 i2 = bmap2->n_eq - 1;
2577 while (bmap1 && i1 >= 0 && i2 >= 0) {
2578 int last1, last2;
2580 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2581 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2582 if (last1 > last2) {
2583 --i2;
2584 continue;
2586 if (last1 < last2) {
2587 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2588 bmap1 = isl_basic_map_free(bmap1);
2589 --i1;
2590 continue;
2592 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2593 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2594 bmap1 = isl_basic_map_free(bmap1);
2596 --i1;
2597 --i2;
2599 for (; i1 >= 0; --i1)
2600 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2601 bmap1 = isl_basic_map_free(bmap1);
2603 return bmap1;
2606 /* Compute a superset of "bmap1" and "bmap2" that is described
2607 * by only the constraints that appear in both "bmap1" and "bmap2".
2609 * First drop constraints that involve unknown integer divisions
2610 * since it is not trivial to check whether two such integer divisions
2611 * in different basic maps are the same.
2612 * Then align the remaining (known) divs and sort the constraints.
2613 * Finally drop all inequalities and equalities from "bmap1" that
2614 * do not also appear in "bmap2".
2616 __isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2617 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2619 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2620 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2621 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2622 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2623 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2624 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2625 bmap1 = isl_basic_map_sort_constraints(bmap1);
2626 bmap2 = isl_basic_map_sort_constraints(bmap2);
2628 bmap1 = select_shared_inequalities(bmap1, bmap2);
2629 bmap1 = select_shared_equalities(bmap1, bmap2);
2631 isl_basic_map_free(bmap2);
2632 bmap1 = isl_basic_map_finalize(bmap1);
2633 return bmap1;
2636 /* Compute a superset of the convex hull of "map" that is described
2637 * by only the constraints in the constituents of "map".
2638 * In particular, the result is composed of constraints that appear
2639 * in each of the basic maps of "map"
2641 * Constraints that involve unknown integer divisions are dropped
2642 * since it is not trivial to check whether two such integer divisions
2643 * in different basic maps are the same.
2645 * The hull is initialized from the first basic map and then
2646 * updated with respect to the other basic maps in turn.
2648 __isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2649 __isl_take isl_map *map)
2651 int i;
2652 isl_basic_map *hull;
2654 if (!map)
2655 return NULL;
2656 if (map->n <= 1)
2657 return map_simple_hull_trivial(map);
2658 map = isl_map_drop_constraint_involving_unknown_divs(map);
2659 hull = isl_basic_map_copy(map->p[0]);
2660 for (i = 1; i < map->n; ++i) {
2661 isl_basic_map *bmap_i;
2663 bmap_i = isl_basic_map_copy(map->p[i]);
2664 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2667 isl_map_free(map);
2668 return hull;
2671 /* Compute a superset of the convex hull of "set" that is described
2672 * by only the constraints in the constituents of "set".
2673 * In particular, the result is composed of constraints that appear
2674 * in each of the basic sets of "set"
2676 __isl_give isl_basic_set *isl_set_plain_unshifted_simple_hull(
2677 __isl_take isl_set *set)
2679 return isl_map_plain_unshifted_simple_hull(set);
2682 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2684 * For each basic set in "set", we first check if the basic set
2685 * contains a translate of "ineq". If this translate is more relaxed,
2686 * then we assume that "ineq" is not a bound on this basic set.
2687 * Otherwise, we know that it is a bound.
2688 * If the basic set does not contain a translate of "ineq", then
2689 * we call is_bound to perform the test.
2691 static __isl_give isl_basic_set *add_bound_from_constraint(
2692 __isl_take isl_basic_set *hull, struct sh_data *data,
2693 __isl_keep isl_set *set, isl_int *ineq)
2695 int i, k;
2696 isl_ctx *ctx;
2697 uint32_t c_hash;
2698 struct ineq_cmp_data v;
2700 if (!hull || !set)
2701 return isl_basic_set_free(hull);
2703 v.len = isl_basic_set_total_dim(hull);
2704 v.p = ineq;
2705 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2707 ctx = isl_basic_set_get_ctx(hull);
2708 for (i = 0; i < set->n; ++i) {
2709 int bound;
2710 struct isl_hash_table_entry *entry;
2712 entry = isl_hash_table_find(ctx, data->p[i].table,
2713 c_hash, &has_ineq, &v, 0);
2714 if (entry) {
2715 isl_int *ineq_i = entry->data;
2716 int neg, more_relaxed;
2718 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2719 if (neg)
2720 isl_int_neg(ineq_i[0], ineq_i[0]);
2721 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2722 if (neg)
2723 isl_int_neg(ineq_i[0], ineq_i[0]);
2724 if (more_relaxed)
2725 break;
2726 else
2727 continue;
2729 bound = is_bound(data, set, i, ineq, 0);
2730 if (bound < 0)
2731 return isl_basic_set_free(hull);
2732 if (!bound)
2733 break;
2735 if (i < set->n)
2736 return hull;
2738 k = isl_basic_set_alloc_inequality(hull);
2739 if (k < 0)
2740 return isl_basic_set_free(hull);
2741 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2743 return hull;
2746 /* Compute a superset of the convex hull of "set" that is described
2747 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2748 * has no parameters or integer divisions.
2750 * The inequalities in "ineq" are assumed to have been sorted such
2751 * that constraints with the same linear part appear together and
2752 * that among constraints with the same linear part, those with
2753 * smaller constant term appear first.
2755 * We reuse the same data structure that is used by uset_simple_hull,
2756 * but we do not need the hull table since we will not consider the
2757 * same constraint more than once. We therefore allocate it with zero size.
2759 * We run through the constraints and try to add them one by one,
2760 * skipping identical constraints. If we have added a constraint and
2761 * the next constraint is a more relaxed translate, then we skip this
2762 * next constraint as well.
2764 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2765 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2767 int i;
2768 int last_added = 0;
2769 struct sh_data *data = NULL;
2770 isl_basic_set *hull = NULL;
2771 unsigned dim;
2773 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2774 if (!hull)
2775 goto error;
2777 data = sh_data_alloc(set, 0);
2778 if (!data)
2779 goto error;
2781 dim = isl_set_dim(set, isl_dim_set);
2782 for (i = 0; i < n_ineq; ++i) {
2783 int hull_n_ineq = hull->n_ineq;
2784 int parallel;
2786 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2787 dim);
2788 if (parallel &&
2789 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2790 continue;
2791 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2792 if (!hull)
2793 goto error;
2794 last_added = hull->n_ineq > hull_n_ineq;
2797 sh_data_free(data);
2798 isl_set_free(set);
2799 return hull;
2800 error:
2801 sh_data_free(data);
2802 isl_set_free(set);
2803 isl_basic_set_free(hull);
2804 return NULL;
2807 /* Collect pointers to all the inequalities in the elements of "list"
2808 * in "ineq". For equalities, store both a pointer to the equality and
2809 * a pointer to its opposite, which is first copied to "mat".
2810 * "ineq" and "mat" are assumed to have been preallocated to the right size
2811 * (the number of inequalities + 2 times the number of equalites and
2812 * the number of equalities, respectively).
2814 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2815 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2817 int i, j, n, n_eq, n_ineq;
2819 if (!mat)
2820 return NULL;
2822 n_eq = 0;
2823 n_ineq = 0;
2824 n = isl_basic_set_list_n_basic_set(list);
2825 for (i = 0; i < n; ++i) {
2826 isl_basic_set *bset;
2827 bset = isl_basic_set_list_get_basic_set(list, i);
2828 if (!bset)
2829 return isl_mat_free(mat);
2830 for (j = 0; j < bset->n_eq; ++j) {
2831 ineq[n_ineq++] = mat->row[n_eq];
2832 ineq[n_ineq++] = bset->eq[j];
2833 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2835 for (j = 0; j < bset->n_ineq; ++j)
2836 ineq[n_ineq++] = bset->ineq[j];
2837 isl_basic_set_free(bset);
2840 return mat;
2843 /* Comparison routine for use as an isl_sort callback.
2845 * Constraints with the same linear part are sorted together and
2846 * among constraints with the same linear part, those with smaller
2847 * constant term are sorted first.
2849 static int cmp_ineq(const void *a, const void *b, void *arg)
2851 unsigned dim = *(unsigned *) arg;
2852 isl_int * const *ineq1 = a;
2853 isl_int * const *ineq2 = b;
2854 int cmp;
2856 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2857 if (cmp != 0)
2858 return cmp;
2859 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2862 /* Compute a superset of the convex hull of "set" that is described
2863 * by only constraints in the elements of "list", where "set" has
2864 * no parameters or integer divisions.
2866 * We collect all the constraints in those elements and then
2867 * sort the constraints such that constraints with the same linear part
2868 * are sorted together and that those with smaller constant term are
2869 * sorted first.
2871 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2872 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2874 int i, n, n_eq, n_ineq;
2875 unsigned dim;
2876 isl_ctx *ctx;
2877 isl_mat *mat = NULL;
2878 isl_int **ineq = NULL;
2879 isl_basic_set *hull;
2881 if (!set)
2882 goto error;
2883 ctx = isl_set_get_ctx(set);
2885 n_eq = 0;
2886 n_ineq = 0;
2887 n = isl_basic_set_list_n_basic_set(list);
2888 for (i = 0; i < n; ++i) {
2889 isl_basic_set *bset;
2890 bset = isl_basic_set_list_get_basic_set(list, i);
2891 if (!bset)
2892 goto error;
2893 n_eq += bset->n_eq;
2894 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2895 isl_basic_set_free(bset);
2898 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2899 if (n_ineq > 0 && !ineq)
2900 goto error;
2902 dim = isl_set_dim(set, isl_dim_set);
2903 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2904 mat = collect_inequalities(mat, list, ineq);
2905 if (!mat)
2906 goto error;
2908 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2909 goto error;
2911 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2913 isl_mat_free(mat);
2914 free(ineq);
2915 isl_basic_set_list_free(list);
2916 return hull;
2917 error:
2918 isl_mat_free(mat);
2919 free(ineq);
2920 isl_set_free(set);
2921 isl_basic_set_list_free(list);
2922 return NULL;
2925 /* Compute a superset of the convex hull of "map" that is described
2926 * by only constraints in the elements of "list".
2928 * If the list is empty, then we can only describe the universe set.
2929 * If the input map is empty, then all constraints are valid, so
2930 * we return the intersection of the elements in "list".
2932 * Otherwise, we align all divs and temporarily treat them
2933 * as regular variables, computing the unshifted simple hull in
2934 * uset_unshifted_simple_hull_from_basic_set_list.
2936 static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2937 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2939 isl_basic_map *model;
2940 isl_basic_map *hull;
2941 isl_set *set;
2942 isl_basic_set_list *bset_list;
2944 if (!map || !list)
2945 goto error;
2947 if (isl_basic_map_list_n_basic_map(list) == 0) {
2948 isl_space *space;
2950 space = isl_map_get_space(map);
2951 isl_map_free(map);
2952 isl_basic_map_list_free(list);
2953 return isl_basic_map_universe(space);
2955 if (isl_map_plain_is_empty(map)) {
2956 isl_map_free(map);
2957 return isl_basic_map_list_intersect(list);
2960 map = isl_map_align_divs_to_basic_map_list(map, list);
2961 if (!map)
2962 goto error;
2963 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2965 model = isl_basic_map_list_get_basic_map(list, 0);
2967 set = isl_map_underlying_set(map);
2968 bset_list = isl_basic_map_list_underlying_set(list);
2970 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2971 hull = isl_basic_map_overlying_set(hull, model);
2973 return hull;
2974 error:
2975 isl_map_free(map);
2976 isl_basic_map_list_free(list);
2977 return NULL;
2980 /* Return a sequence of the basic maps that make up the maps in "list".
2982 static __isl_give isl_basic_set_list *collect_basic_maps(
2983 __isl_take isl_map_list *list)
2985 int i, n;
2986 isl_ctx *ctx;
2987 isl_basic_map_list *bmap_list;
2989 if (!list)
2990 return NULL;
2991 n = isl_map_list_n_map(list);
2992 ctx = isl_map_list_get_ctx(list);
2993 bmap_list = isl_basic_map_list_alloc(ctx, 0);
2995 for (i = 0; i < n; ++i) {
2996 isl_map *map;
2997 isl_basic_map_list *list_i;
2999 map = isl_map_list_get_map(list, i);
3000 map = isl_map_compute_divs(map);
3001 list_i = isl_map_get_basic_map_list(map);
3002 isl_map_free(map);
3003 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
3006 isl_map_list_free(list);
3007 return bmap_list;
3010 /* Compute a superset of the convex hull of "map" that is described
3011 * by only constraints in the elements of "list".
3013 * If "map" is the universe, then the convex hull (and therefore
3014 * any superset of the convexhull) is the universe as well.
3016 * Otherwise, we collect all the basic maps in the map list and
3017 * continue with map_unshifted_simple_hull_from_basic_map_list.
3019 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
3020 __isl_take isl_map *map, __isl_take isl_map_list *list)
3022 isl_basic_map_list *bmap_list;
3023 int is_universe;
3025 is_universe = isl_map_plain_is_universe(map);
3026 if (is_universe < 0)
3027 map = isl_map_free(map);
3028 if (is_universe < 0 || is_universe) {
3029 isl_map_list_free(list);
3030 return isl_map_unshifted_simple_hull(map);
3033 bmap_list = collect_basic_maps(list);
3034 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
3037 /* Compute a superset of the convex hull of "set" that is described
3038 * by only constraints in the elements of "list".
3040 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
3041 __isl_take isl_set *set, __isl_take isl_set_list *list)
3043 return isl_map_unshifted_simple_hull_from_map_list(set, list);
3046 /* Given a set "set", return parametric bounds on the dimension "dim".
3048 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
3050 unsigned set_dim = isl_set_dim(set, isl_dim_set);
3051 set = isl_set_copy(set);
3052 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
3053 set = isl_set_eliminate_dims(set, 0, dim);
3054 return isl_set_convex_hull(set);
3057 /* Computes a "simple hull" and then check if each dimension in the
3058 * resulting hull is bounded by a symbolic constant. If not, the
3059 * hull is intersected with the corresponding bounds on the whole set.
3061 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
3063 int i, j;
3064 struct isl_basic_set *hull;
3065 unsigned nparam, left;
3066 int removed_divs = 0;
3068 hull = isl_set_simple_hull(isl_set_copy(set));
3069 if (!hull)
3070 goto error;
3072 nparam = isl_basic_set_dim(hull, isl_dim_param);
3073 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
3074 int lower = 0, upper = 0;
3075 struct isl_basic_set *bounds;
3077 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
3078 for (j = 0; j < hull->n_eq; ++j) {
3079 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3080 continue;
3081 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3082 left) == -1)
3083 break;
3085 if (j < hull->n_eq)
3086 continue;
3088 for (j = 0; j < hull->n_ineq; ++j) {
3089 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3090 continue;
3091 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3092 left) != -1 ||
3093 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3094 i) != -1)
3095 continue;
3096 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3097 lower = 1;
3098 else
3099 upper = 1;
3100 if (lower && upper)
3101 break;
3104 if (lower && upper)
3105 continue;
3107 if (!removed_divs) {
3108 set = isl_set_remove_divs(set);
3109 if (!set)
3110 goto error;
3111 removed_divs = 1;
3113 bounds = set_bounds(set, i);
3114 hull = isl_basic_set_intersect(hull, bounds);
3115 if (!hull)
3116 goto error;
3119 isl_set_free(set);
3120 return hull;
3121 error:
3122 isl_set_free(set);
3123 return NULL;