python: derive all classes from 'object'
[isl.git] / isl_convex_hull.c
blobe61139f2b674207188074f56954e412cef7451be
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_map *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 __isl_give isl_basic_set *uset_convex_hull_unbounded(
1511 __isl_take isl_set *set)
1513 isl_basic_set_list *list;
1515 list = isl_set_get_basic_set_list(set);
1516 isl_set_free(set);
1518 while (list) {
1519 int n;
1520 struct isl_basic_set *t;
1521 isl_basic_set *bset1, *bset2;
1523 n = isl_basic_set_list_n_basic_set(list);
1524 if (n < 2)
1525 isl_die(isl_basic_set_list_get_ctx(list),
1526 isl_error_internal,
1527 "expecting at least two elements", goto error);
1528 bset1 = isl_basic_set_list_get_basic_set(list, n - 1);
1529 bset2 = isl_basic_set_list_get_basic_set(list, n - 2);
1530 bset1 = convex_hull_pair(bset1, bset2);
1531 if (n == 2) {
1532 isl_basic_set_list_free(list);
1533 return bset1;
1535 bset1 = isl_basic_set_underlying_set(bset1);
1536 list = isl_basic_set_list_drop(list, n - 2, 2);
1537 list = isl_basic_set_list_add(list, bset1);
1539 t = isl_basic_set_list_get_basic_set(list, n - 2);
1540 t = isl_basic_set_lineality_space(t);
1541 if (!t)
1542 goto error;
1543 if (isl_basic_set_plain_is_universe(t)) {
1544 isl_basic_set_list_free(list);
1545 return t;
1547 if (t->n_eq < isl_basic_set_total_dim(t)) {
1548 set = isl_basic_set_list_union(list);
1549 return modulo_lineality(set, t);
1551 isl_basic_set_free(t);
1554 return NULL;
1555 error:
1556 isl_basic_set_list_free(list);
1557 return NULL;
1560 /* Compute an initial hull for wrapping containing a single initial
1561 * facet.
1562 * This function assumes that the given set is bounded.
1564 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1565 struct isl_set *set)
1567 struct isl_mat *bounds = NULL;
1568 unsigned dim;
1569 int k;
1571 if (!hull)
1572 goto error;
1573 bounds = initial_facet_constraint(set);
1574 if (!bounds)
1575 goto error;
1576 k = isl_basic_set_alloc_inequality(hull);
1577 if (k < 0)
1578 goto error;
1579 dim = isl_set_n_dim(set);
1580 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1581 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1582 isl_mat_free(bounds);
1584 return hull;
1585 error:
1586 isl_basic_set_free(hull);
1587 isl_mat_free(bounds);
1588 return NULL;
1591 struct max_constraint {
1592 struct isl_mat *c;
1593 int count;
1594 int ineq;
1597 static int max_constraint_equal(const void *entry, const void *val)
1599 struct max_constraint *a = (struct max_constraint *)entry;
1600 isl_int *b = (isl_int *)val;
1602 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1605 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1606 isl_int *con, unsigned len, int n, int ineq)
1608 struct isl_hash_table_entry *entry;
1609 struct max_constraint *c;
1610 uint32_t c_hash;
1612 c_hash = isl_seq_get_hash(con + 1, len);
1613 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1614 con + 1, 0);
1615 if (!entry)
1616 return;
1617 c = entry->data;
1618 if (c->count < n) {
1619 isl_hash_table_remove(ctx, table, entry);
1620 return;
1622 c->count++;
1623 if (isl_int_gt(c->c->row[0][0], con[0]))
1624 return;
1625 if (isl_int_eq(c->c->row[0][0], con[0])) {
1626 if (ineq)
1627 c->ineq = ineq;
1628 return;
1630 c->c = isl_mat_cow(c->c);
1631 isl_int_set(c->c->row[0][0], con[0]);
1632 c->ineq = ineq;
1635 /* Check whether the constraint hash table "table" constains the constraint
1636 * "con".
1638 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1639 isl_int *con, unsigned len, int n)
1641 struct isl_hash_table_entry *entry;
1642 struct max_constraint *c;
1643 uint32_t c_hash;
1645 c_hash = isl_seq_get_hash(con + 1, len);
1646 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1647 con + 1, 0);
1648 if (!entry)
1649 return 0;
1650 c = entry->data;
1651 if (c->count < n)
1652 return 0;
1653 return isl_int_eq(c->c->row[0][0], con[0]);
1656 /* Check for inequality constraints of a basic set without equalities
1657 * such that the same or more stringent copies of the constraint appear
1658 * in all of the basic sets. Such constraints are necessarily facet
1659 * constraints of the convex hull.
1661 * If the resulting basic set is by chance identical to one of
1662 * the basic sets in "set", then we know that this basic set contains
1663 * all other basic sets and is therefore the convex hull of set.
1664 * In this case we set *is_hull to 1.
1666 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1667 struct isl_set *set, int *is_hull)
1669 int i, j, s, n;
1670 int min_constraints;
1671 int best;
1672 struct max_constraint *constraints = NULL;
1673 struct isl_hash_table *table = NULL;
1674 unsigned total;
1676 *is_hull = 0;
1678 for (i = 0; i < set->n; ++i)
1679 if (set->p[i]->n_eq == 0)
1680 break;
1681 if (i >= set->n)
1682 return hull;
1683 min_constraints = set->p[i]->n_ineq;
1684 best = i;
1685 for (i = best + 1; i < set->n; ++i) {
1686 if (set->p[i]->n_eq != 0)
1687 continue;
1688 if (set->p[i]->n_ineq >= min_constraints)
1689 continue;
1690 min_constraints = set->p[i]->n_ineq;
1691 best = i;
1693 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1694 min_constraints);
1695 if (!constraints)
1696 return hull;
1697 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1698 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1699 goto error;
1701 total = isl_space_dim(set->dim, isl_dim_all);
1702 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1703 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1704 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1705 if (!constraints[i].c)
1706 goto error;
1707 constraints[i].ineq = 1;
1709 for (i = 0; i < min_constraints; ++i) {
1710 struct isl_hash_table_entry *entry;
1711 uint32_t c_hash;
1712 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1713 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1714 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1715 if (!entry)
1716 goto error;
1717 isl_assert(hull->ctx, !entry->data, goto error);
1718 entry->data = &constraints[i];
1721 n = 0;
1722 for (s = 0; s < set->n; ++s) {
1723 if (s == best)
1724 continue;
1726 for (i = 0; i < set->p[s]->n_eq; ++i) {
1727 isl_int *eq = set->p[s]->eq[i];
1728 for (j = 0; j < 2; ++j) {
1729 isl_seq_neg(eq, eq, 1 + total);
1730 update_constraint(hull->ctx, table,
1731 eq, total, n, 0);
1734 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1735 isl_int *ineq = set->p[s]->ineq[i];
1736 update_constraint(hull->ctx, table, ineq, total, n,
1737 set->p[s]->n_eq == 0);
1739 ++n;
1742 for (i = 0; i < min_constraints; ++i) {
1743 if (constraints[i].count < n)
1744 continue;
1745 if (!constraints[i].ineq)
1746 continue;
1747 j = isl_basic_set_alloc_inequality(hull);
1748 if (j < 0)
1749 goto error;
1750 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1753 for (s = 0; s < set->n; ++s) {
1754 if (set->p[s]->n_eq)
1755 continue;
1756 if (set->p[s]->n_ineq != hull->n_ineq)
1757 continue;
1758 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1759 isl_int *ineq = set->p[s]->ineq[i];
1760 if (!has_constraint(hull->ctx, table, ineq, total, n))
1761 break;
1763 if (i == set->p[s]->n_ineq)
1764 *is_hull = 1;
1767 isl_hash_table_clear(table);
1768 for (i = 0; i < min_constraints; ++i)
1769 isl_mat_free(constraints[i].c);
1770 free(constraints);
1771 free(table);
1772 return hull;
1773 error:
1774 isl_hash_table_clear(table);
1775 free(table);
1776 if (constraints)
1777 for (i = 0; i < min_constraints; ++i)
1778 isl_mat_free(constraints[i].c);
1779 free(constraints);
1780 return hull;
1783 /* Create a template for the convex hull of "set" and fill it up
1784 * obvious facet constraints, if any. If the result happens to
1785 * be the convex hull of "set" then *is_hull is set to 1.
1787 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1789 struct isl_basic_set *hull;
1790 unsigned n_ineq;
1791 int i;
1793 n_ineq = 1;
1794 for (i = 0; i < set->n; ++i) {
1795 n_ineq += set->p[i]->n_eq;
1796 n_ineq += set->p[i]->n_ineq;
1798 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1799 hull = isl_basic_set_set_rational(hull);
1800 if (!hull)
1801 return NULL;
1802 return common_constraints(hull, set, is_hull);
1805 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1807 struct isl_basic_set *hull;
1808 int is_hull;
1810 hull = proto_hull(set, &is_hull);
1811 if (hull && !is_hull) {
1812 if (hull->n_ineq == 0)
1813 hull = initial_hull(hull, set);
1814 hull = extend(hull, set);
1816 isl_set_free(set);
1818 return hull;
1821 /* Compute the convex hull of a set without any parameters or
1822 * integer divisions. Depending on whether the set is bounded,
1823 * we pass control to the wrapping based convex hull or
1824 * the Fourier-Motzkin elimination based convex hull.
1825 * We also handle a few special cases before checking the boundedness.
1827 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1829 struct isl_basic_set *convex_hull = NULL;
1830 struct isl_basic_set *lin;
1832 if (isl_set_n_dim(set) == 0)
1833 return convex_hull_0d(set);
1835 set = isl_set_coalesce(set);
1836 set = isl_set_set_rational(set);
1838 if (!set)
1839 goto error;
1840 if (!set)
1841 return NULL;
1842 if (set->n == 1) {
1843 convex_hull = isl_basic_set_copy(set->p[0]);
1844 isl_set_free(set);
1845 return convex_hull;
1847 if (isl_set_n_dim(set) == 1)
1848 return convex_hull_1d(set);
1850 if (isl_set_is_bounded(set) &&
1851 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1852 return uset_convex_hull_wrap(set);
1854 lin = uset_combined_lineality_space(isl_set_copy(set));
1855 if (!lin)
1856 goto error;
1857 if (isl_basic_set_plain_is_universe(lin)) {
1858 isl_set_free(set);
1859 return lin;
1861 if (lin->n_eq < isl_basic_set_total_dim(lin))
1862 return modulo_lineality(set, lin);
1863 isl_basic_set_free(lin);
1865 return uset_convex_hull_unbounded(set);
1866 error:
1867 isl_set_free(set);
1868 isl_basic_set_free(convex_hull);
1869 return NULL;
1872 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1873 * without parameters or divs and where the convex hull of set is
1874 * known to be full-dimensional.
1876 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1878 struct isl_basic_set *convex_hull = NULL;
1880 if (!set)
1881 goto error;
1883 if (isl_set_n_dim(set) == 0) {
1884 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1885 isl_set_free(set);
1886 convex_hull = isl_basic_set_set_rational(convex_hull);
1887 return convex_hull;
1890 set = isl_set_set_rational(set);
1891 set = isl_set_coalesce(set);
1892 if (!set)
1893 goto error;
1894 if (set->n == 1) {
1895 convex_hull = isl_basic_set_copy(set->p[0]);
1896 isl_set_free(set);
1897 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1898 return convex_hull;
1900 if (isl_set_n_dim(set) == 1)
1901 return convex_hull_1d(set);
1903 return uset_convex_hull_wrap(set);
1904 error:
1905 isl_set_free(set);
1906 return NULL;
1909 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1910 * We first remove the equalities (transforming the set), compute the
1911 * convex hull of the transformed set and then add the equalities back
1912 * (after performing the inverse transformation.
1914 static struct isl_basic_set *modulo_affine_hull(
1915 struct isl_set *set, struct isl_basic_set *affine_hull)
1917 struct isl_mat *T;
1918 struct isl_mat *T2;
1919 struct isl_basic_set *dummy;
1920 struct isl_basic_set *convex_hull;
1922 dummy = isl_basic_set_remove_equalities(
1923 isl_basic_set_copy(affine_hull), &T, &T2);
1924 if (!dummy)
1925 goto error;
1926 isl_basic_set_free(dummy);
1927 set = isl_set_preimage(set, T);
1928 convex_hull = uset_convex_hull(set);
1929 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1930 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1931 return convex_hull;
1932 error:
1933 isl_basic_set_free(affine_hull);
1934 isl_set_free(set);
1935 return NULL;
1938 /* Return an empty basic map living in the same space as "map".
1940 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1941 __isl_take isl_map *map)
1943 isl_space *space;
1945 space = isl_map_get_space(map);
1946 isl_map_free(map);
1947 return isl_basic_map_empty(space);
1950 /* Compute the convex hull of a map.
1952 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1953 * specifically, the wrapping of facets to obtain new facets.
1955 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1957 struct isl_basic_set *bset;
1958 struct isl_basic_map *model = NULL;
1959 struct isl_basic_set *affine_hull = NULL;
1960 struct isl_basic_map *convex_hull = NULL;
1961 struct isl_set *set = NULL;
1963 map = isl_map_detect_equalities(map);
1964 map = isl_map_align_divs(map);
1965 if (!map)
1966 goto error;
1968 if (map->n == 0)
1969 return replace_map_by_empty_basic_map(map);
1971 model = isl_basic_map_copy(map->p[0]);
1972 set = isl_map_underlying_set(map);
1973 if (!set)
1974 goto error;
1976 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1977 if (!affine_hull)
1978 goto error;
1979 if (affine_hull->n_eq != 0)
1980 bset = modulo_affine_hull(set, affine_hull);
1981 else {
1982 isl_basic_set_free(affine_hull);
1983 bset = uset_convex_hull(set);
1986 convex_hull = isl_basic_map_overlying_set(bset, model);
1987 if (!convex_hull)
1988 return NULL;
1990 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1991 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1992 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1993 return convex_hull;
1994 error:
1995 isl_set_free(set);
1996 isl_basic_map_free(model);
1997 return NULL;
2000 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
2002 return bset_from_bmap(isl_map_convex_hull(set_to_map(set)));
2005 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
2007 isl_basic_map *hull;
2009 hull = isl_map_convex_hull(map);
2010 return isl_basic_map_remove_divs(hull);
2013 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
2015 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set)));
2018 struct sh_data_entry {
2019 struct isl_hash_table *table;
2020 struct isl_tab *tab;
2023 /* Holds the data needed during the simple hull computation.
2024 * In particular,
2025 * n the number of basic sets in the original set
2026 * hull_table a hash table of already computed constraints
2027 * in the simple hull
2028 * p for each basic set,
2029 * table a hash table of the constraints
2030 * tab the tableau corresponding to the basic set
2032 struct sh_data {
2033 struct isl_ctx *ctx;
2034 unsigned n;
2035 struct isl_hash_table *hull_table;
2036 struct sh_data_entry p[1];
2039 static void sh_data_free(struct sh_data *data)
2041 int i;
2043 if (!data)
2044 return;
2045 isl_hash_table_free(data->ctx, data->hull_table);
2046 for (i = 0; i < data->n; ++i) {
2047 isl_hash_table_free(data->ctx, data->p[i].table);
2048 isl_tab_free(data->p[i].tab);
2050 free(data);
2053 struct ineq_cmp_data {
2054 unsigned len;
2055 isl_int *p;
2058 static int has_ineq(const void *entry, const void *val)
2060 isl_int *row = (isl_int *)entry;
2061 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2063 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2064 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2067 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2068 isl_int *ineq, unsigned len)
2070 uint32_t c_hash;
2071 struct ineq_cmp_data v;
2072 struct isl_hash_table_entry *entry;
2074 v.len = len;
2075 v.p = ineq;
2076 c_hash = isl_seq_get_hash(ineq + 1, len);
2077 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2078 if (!entry)
2079 return - 1;
2080 entry->data = ineq;
2081 return 0;
2084 /* Fill hash table "table" with the constraints of "bset".
2085 * Equalities are added as two inequalities.
2086 * The value in the hash table is a pointer to the (in)equality of "bset".
2088 static int hash_basic_set(struct isl_hash_table *table,
2089 struct isl_basic_set *bset)
2091 int i, j;
2092 unsigned dim = isl_basic_set_total_dim(bset);
2094 for (i = 0; i < bset->n_eq; ++i) {
2095 for (j = 0; j < 2; ++j) {
2096 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2097 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2098 return -1;
2101 for (i = 0; i < bset->n_ineq; ++i) {
2102 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2103 return -1;
2105 return 0;
2108 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2110 struct sh_data *data;
2111 int i;
2113 data = isl_calloc(set->ctx, struct sh_data,
2114 sizeof(struct sh_data) +
2115 (set->n - 1) * sizeof(struct sh_data_entry));
2116 if (!data)
2117 return NULL;
2118 data->ctx = set->ctx;
2119 data->n = set->n;
2120 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2121 if (!data->hull_table)
2122 goto error;
2123 for (i = 0; i < set->n; ++i) {
2124 data->p[i].table = isl_hash_table_alloc(set->ctx,
2125 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2126 if (!data->p[i].table)
2127 goto error;
2128 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2129 goto error;
2131 return data;
2132 error:
2133 sh_data_free(data);
2134 return NULL;
2137 /* Check if inequality "ineq" is a bound for basic set "j" or if
2138 * it can be relaxed (by increasing the constant term) to become
2139 * a bound for that basic set. In the latter case, the constant
2140 * term is updated.
2141 * Relaxation of the constant term is only allowed if "shift" is set.
2143 * Return 1 if "ineq" is a bound
2144 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2145 * -1 if some error occurred
2147 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2148 isl_int *ineq, int shift)
2150 enum isl_lp_result res;
2151 isl_int opt;
2153 if (!data->p[j].tab) {
2154 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2155 if (!data->p[j].tab)
2156 return -1;
2159 isl_int_init(opt);
2161 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2162 &opt, NULL, 0);
2163 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2164 if (shift)
2165 isl_int_sub(ineq[0], ineq[0], opt);
2166 else
2167 res = isl_lp_unbounded;
2170 isl_int_clear(opt);
2172 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2173 res == isl_lp_unbounded ? 0 : -1;
2176 /* Set the constant term of "ineq" to the maximum of those of the constraints
2177 * in the basic sets of "set" following "i" that are parallel to "ineq".
2178 * That is, if any of the basic sets of "set" following "i" have a more
2179 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2180 * "c_hash" is the hash value of the linear part of "ineq".
2181 * "v" has been set up for use by has_ineq.
2183 * Note that the two inequality constraints corresponding to an equality are
2184 * represented by the same inequality constraint in data->p[j].table
2185 * (but with different hash values). This means the constraint (or at
2186 * least its constant term) may need to be temporarily negated to get
2187 * the actually hashed constraint.
2189 static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
2190 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2192 int j;
2193 isl_ctx *ctx;
2194 struct isl_hash_table_entry *entry;
2196 ctx = isl_set_get_ctx(set);
2197 for (j = i + 1; j < set->n; ++j) {
2198 int neg;
2199 isl_int *ineq_j;
2201 entry = isl_hash_table_find(ctx, data->p[j].table,
2202 c_hash, &has_ineq, v, 0);
2203 if (!entry)
2204 continue;
2206 ineq_j = entry->data;
2207 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2208 if (neg)
2209 isl_int_neg(ineq_j[0], ineq_j[0]);
2210 if (isl_int_gt(ineq_j[0], ineq[0]))
2211 isl_int_set(ineq[0], ineq_j[0]);
2212 if (neg)
2213 isl_int_neg(ineq_j[0], ineq_j[0]);
2217 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2218 * become a bound on the whole set. If so, add the (relaxed) inequality
2219 * to "hull". Relaxation is only allowed if "shift" is set.
2221 * We first check if "hull" already contains a translate of the inequality.
2222 * If so, we are done.
2223 * Then, we check if any of the previous basic sets contains a translate
2224 * of the inequality. If so, then we have already considered this
2225 * inequality and we are done.
2226 * Otherwise, for each basic set other than "i", we check if the inequality
2227 * is a bound on the basic set, but first replace the constant term
2228 * by the maximal value of any translate of the inequality in any
2229 * of the following basic sets.
2230 * For previous basic sets, we know that they do not contain a translate
2231 * of the inequality, so we directly call is_bound.
2232 * For following basic sets, we first check if a translate of the
2233 * inequality appears in its description. If so, the constant term
2234 * of the inequality has already been updated with respect to this
2235 * translate and the inequality is therefore known to be a bound
2236 * of this basic set.
2238 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2239 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
2240 int shift)
2242 uint32_t c_hash;
2243 struct ineq_cmp_data v;
2244 struct isl_hash_table_entry *entry;
2245 int j, k;
2247 if (!hull)
2248 return NULL;
2250 v.len = isl_basic_set_total_dim(hull);
2251 v.p = ineq;
2252 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2254 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2255 has_ineq, &v, 0);
2256 if (entry)
2257 return hull;
2259 for (j = 0; j < i; ++j) {
2260 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2261 c_hash, has_ineq, &v, 0);
2262 if (entry)
2263 break;
2265 if (j < i)
2266 return hull;
2268 k = isl_basic_set_alloc_inequality(hull);
2269 if (k < 0)
2270 goto error;
2271 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2273 set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
2274 for (j = 0; j < i; ++j) {
2275 int bound;
2276 bound = is_bound(data, set, j, hull->ineq[k], shift);
2277 if (bound < 0)
2278 goto error;
2279 if (!bound)
2280 break;
2282 if (j < i) {
2283 isl_basic_set_free_inequality(hull, 1);
2284 return hull;
2287 for (j = i + 1; j < set->n; ++j) {
2288 int bound;
2289 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2290 c_hash, has_ineq, &v, 0);
2291 if (entry)
2292 continue;
2293 bound = is_bound(data, set, j, hull->ineq[k], shift);
2294 if (bound < 0)
2295 goto error;
2296 if (!bound)
2297 break;
2299 if (j < set->n) {
2300 isl_basic_set_free_inequality(hull, 1);
2301 return hull;
2304 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2305 has_ineq, &v, 1);
2306 if (!entry)
2307 goto error;
2308 entry->data = hull->ineq[k];
2310 return hull;
2311 error:
2312 isl_basic_set_free(hull);
2313 return NULL;
2316 /* Check if any inequality from basic set "i" is or can be relaxed to
2317 * become a bound on the whole set. If so, add the (relaxed) inequality
2318 * to "hull". Relaxation is only allowed if "shift" is set.
2320 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2321 struct sh_data *data, struct isl_set *set, int i, int shift)
2323 int j, k;
2324 unsigned dim = isl_basic_set_total_dim(bset);
2326 for (j = 0; j < set->p[i]->n_eq; ++j) {
2327 for (k = 0; k < 2; ++k) {
2328 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2329 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2330 shift);
2333 for (j = 0; j < set->p[i]->n_ineq; ++j)
2334 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2335 return bset;
2338 /* Compute a superset of the convex hull of set that is described
2339 * by only (translates of) the constraints in the constituents of set.
2340 * Translation is only allowed if "shift" is set.
2342 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2343 int shift)
2345 struct sh_data *data = NULL;
2346 struct isl_basic_set *hull = NULL;
2347 unsigned n_ineq;
2348 int i;
2350 if (!set)
2351 return NULL;
2353 n_ineq = 0;
2354 for (i = 0; i < set->n; ++i) {
2355 if (!set->p[i])
2356 goto error;
2357 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2360 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2361 if (!hull)
2362 goto error;
2364 data = sh_data_alloc(set, n_ineq);
2365 if (!data)
2366 goto error;
2368 for (i = 0; i < set->n; ++i)
2369 hull = add_bounds(hull, data, set, i, shift);
2371 sh_data_free(data);
2372 isl_set_free(set);
2374 return hull;
2375 error:
2376 sh_data_free(data);
2377 isl_basic_set_free(hull);
2378 isl_set_free(set);
2379 return NULL;
2382 /* Compute a superset of the convex hull of map that is described
2383 * by only (translates of) the constraints in the constituents of map.
2384 * Handle trivial cases where map is NULL or contains at most one disjunct.
2386 static __isl_give isl_basic_map *map_simple_hull_trivial(
2387 __isl_take isl_map *map)
2389 isl_basic_map *hull;
2391 if (!map)
2392 return NULL;
2393 if (map->n == 0)
2394 return replace_map_by_empty_basic_map(map);
2396 hull = isl_basic_map_copy(map->p[0]);
2397 isl_map_free(map);
2398 return hull;
2401 /* Return a copy of the simple hull cached inside "map".
2402 * "shift" determines whether to return the cached unshifted or shifted
2403 * simple hull.
2405 static __isl_give isl_basic_map *cached_simple_hull(__isl_take isl_map *map,
2406 int shift)
2408 isl_basic_map *hull;
2410 hull = isl_basic_map_copy(map->cached_simple_hull[shift]);
2411 isl_map_free(map);
2413 return hull;
2416 /* Compute a superset of the convex hull of map that is described
2417 * by only (translates of) the constraints in the constituents of map.
2418 * Translation is only allowed if "shift" is set.
2420 * The constraints are sorted while removing redundant constraints
2421 * in order to indicate a preference of which constraints should
2422 * be preserved. In particular, pairs of constraints that are
2423 * sorted together are preferred to either both be preserved
2424 * or both be removed. The sorting is performed inside
2425 * isl_basic_map_remove_redundancies.
2427 * The result of the computation is stored in map->cached_simple_hull[shift]
2428 * such that it can be reused in subsequent calls. The cache is cleared
2429 * whenever the map is modified (in isl_map_cow).
2430 * Note that the results need to be stored in the input map for there
2431 * to be any chance that they may get reused. In particular, they
2432 * are stored in a copy of the input map that is saved before
2433 * the integer division alignment.
2435 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2436 int shift)
2438 struct isl_set *set = NULL;
2439 struct isl_basic_map *model = NULL;
2440 struct isl_basic_map *hull;
2441 struct isl_basic_map *affine_hull;
2442 struct isl_basic_set *bset = NULL;
2443 isl_map *input;
2445 if (!map || map->n <= 1)
2446 return map_simple_hull_trivial(map);
2448 if (map->cached_simple_hull[shift])
2449 return cached_simple_hull(map, shift);
2451 map = isl_map_detect_equalities(map);
2452 if (!map || map->n <= 1)
2453 return map_simple_hull_trivial(map);
2454 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2455 input = isl_map_copy(map);
2456 map = isl_map_align_divs(map);
2457 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2459 set = isl_map_underlying_set(map);
2461 bset = uset_simple_hull(set, shift);
2463 hull = isl_basic_map_overlying_set(bset, model);
2465 hull = isl_basic_map_intersect(hull, affine_hull);
2466 hull = isl_basic_map_remove_redundancies(hull);
2468 if (hull) {
2469 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2470 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2473 hull = isl_basic_map_finalize(hull);
2474 if (input)
2475 input->cached_simple_hull[shift] = isl_basic_map_copy(hull);
2476 isl_map_free(input);
2478 return hull;
2481 /* Compute a superset of the convex hull of map that is described
2482 * by only translates of the constraints in the constituents of map.
2484 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2486 return map_simple_hull(map, 1);
2489 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2491 return bset_from_bmap(isl_map_simple_hull(set_to_map(set)));
2494 /* Compute a superset of the convex hull of map that is described
2495 * by only the constraints in the constituents of map.
2497 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2498 __isl_take isl_map *map)
2500 return map_simple_hull(map, 0);
2503 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2504 __isl_take isl_set *set)
2506 return isl_map_unshifted_simple_hull(set);
2509 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2510 * A constraint that appears with different constant terms
2511 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2512 * (i.e., greatest) constant term.
2513 * "bmap1" and "bmap2" are assumed to have the same (known)
2514 * integer divisions.
2515 * The constraints of both "bmap1" and "bmap2" are assumed
2516 * to have been sorted using isl_basic_map_sort_constraints.
2518 * Run through the inequality constraints of "bmap1" and "bmap2"
2519 * in sorted order.
2520 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2521 * is removed.
2522 * If a match is found, the constraint is kept. If needed, the constant
2523 * term of the constraint is adjusted.
2525 static __isl_give isl_basic_map *select_shared_inequalities(
2526 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2528 int i1, i2;
2530 bmap1 = isl_basic_map_cow(bmap1);
2531 if (!bmap1 || !bmap2)
2532 return isl_basic_map_free(bmap1);
2534 i1 = bmap1->n_ineq - 1;
2535 i2 = bmap2->n_ineq - 1;
2536 while (bmap1 && i1 >= 0 && i2 >= 0) {
2537 int cmp;
2539 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2540 bmap2->ineq[i2]);
2541 if (cmp < 0) {
2542 --i2;
2543 continue;
2545 if (cmp > 0) {
2546 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2547 bmap1 = isl_basic_map_free(bmap1);
2548 --i1;
2549 continue;
2551 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2552 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2553 --i1;
2554 --i2;
2556 for (; i1 >= 0; --i1)
2557 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2558 bmap1 = isl_basic_map_free(bmap1);
2560 return bmap1;
2563 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2564 * "bmap1" and "bmap2" are assumed to have the same (known)
2565 * integer divisions.
2567 * Run through the equality constraints of "bmap1" and "bmap2".
2568 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2569 * is removed.
2571 static __isl_give isl_basic_map *select_shared_equalities(
2572 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2574 int i1, i2;
2575 unsigned total;
2577 bmap1 = isl_basic_map_cow(bmap1);
2578 if (!bmap1 || !bmap2)
2579 return isl_basic_map_free(bmap1);
2581 total = isl_basic_map_total_dim(bmap1);
2583 i1 = bmap1->n_eq - 1;
2584 i2 = bmap2->n_eq - 1;
2585 while (bmap1 && i1 >= 0 && i2 >= 0) {
2586 int last1, last2;
2588 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2589 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2590 if (last1 > last2) {
2591 --i2;
2592 continue;
2594 if (last1 < last2) {
2595 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2596 bmap1 = isl_basic_map_free(bmap1);
2597 --i1;
2598 continue;
2600 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2601 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2602 bmap1 = isl_basic_map_free(bmap1);
2604 --i1;
2605 --i2;
2607 for (; i1 >= 0; --i1)
2608 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2609 bmap1 = isl_basic_map_free(bmap1);
2611 return bmap1;
2614 /* Compute a superset of "bmap1" and "bmap2" that is described
2615 * by only the constraints that appear in both "bmap1" and "bmap2".
2617 * First drop constraints that involve unknown integer divisions
2618 * since it is not trivial to check whether two such integer divisions
2619 * in different basic maps are the same.
2620 * Then align the remaining (known) divs and sort the constraints.
2621 * Finally drop all inequalities and equalities from "bmap1" that
2622 * do not also appear in "bmap2".
2624 __isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2625 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2627 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2628 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2629 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2630 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2631 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2632 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2633 bmap1 = isl_basic_map_sort_constraints(bmap1);
2634 bmap2 = isl_basic_map_sort_constraints(bmap2);
2636 bmap1 = select_shared_inequalities(bmap1, bmap2);
2637 bmap1 = select_shared_equalities(bmap1, bmap2);
2639 isl_basic_map_free(bmap2);
2640 bmap1 = isl_basic_map_finalize(bmap1);
2641 return bmap1;
2644 /* Compute a superset of the convex hull of "map" that is described
2645 * by only the constraints in the constituents of "map".
2646 * In particular, the result is composed of constraints that appear
2647 * in each of the basic maps of "map"
2649 * Constraints that involve unknown integer divisions are dropped
2650 * since it is not trivial to check whether two such integer divisions
2651 * in different basic maps are the same.
2653 * The hull is initialized from the first basic map and then
2654 * updated with respect to the other basic maps in turn.
2656 __isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2657 __isl_take isl_map *map)
2659 int i;
2660 isl_basic_map *hull;
2662 if (!map)
2663 return NULL;
2664 if (map->n <= 1)
2665 return map_simple_hull_trivial(map);
2666 map = isl_map_drop_constraint_involving_unknown_divs(map);
2667 hull = isl_basic_map_copy(map->p[0]);
2668 for (i = 1; i < map->n; ++i) {
2669 isl_basic_map *bmap_i;
2671 bmap_i = isl_basic_map_copy(map->p[i]);
2672 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2675 isl_map_free(map);
2676 return hull;
2679 /* Compute a superset of the convex hull of "set" that is described
2680 * by only the constraints in the constituents of "set".
2681 * In particular, the result is composed of constraints that appear
2682 * in each of the basic sets of "set"
2684 __isl_give isl_basic_set *isl_set_plain_unshifted_simple_hull(
2685 __isl_take isl_set *set)
2687 return isl_map_plain_unshifted_simple_hull(set);
2690 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2692 * For each basic set in "set", we first check if the basic set
2693 * contains a translate of "ineq". If this translate is more relaxed,
2694 * then we assume that "ineq" is not a bound on this basic set.
2695 * Otherwise, we know that it is a bound.
2696 * If the basic set does not contain a translate of "ineq", then
2697 * we call is_bound to perform the test.
2699 static __isl_give isl_basic_set *add_bound_from_constraint(
2700 __isl_take isl_basic_set *hull, struct sh_data *data,
2701 __isl_keep isl_set *set, isl_int *ineq)
2703 int i, k;
2704 isl_ctx *ctx;
2705 uint32_t c_hash;
2706 struct ineq_cmp_data v;
2708 if (!hull || !set)
2709 return isl_basic_set_free(hull);
2711 v.len = isl_basic_set_total_dim(hull);
2712 v.p = ineq;
2713 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2715 ctx = isl_basic_set_get_ctx(hull);
2716 for (i = 0; i < set->n; ++i) {
2717 int bound;
2718 struct isl_hash_table_entry *entry;
2720 entry = isl_hash_table_find(ctx, data->p[i].table,
2721 c_hash, &has_ineq, &v, 0);
2722 if (entry) {
2723 isl_int *ineq_i = entry->data;
2724 int neg, more_relaxed;
2726 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2727 if (neg)
2728 isl_int_neg(ineq_i[0], ineq_i[0]);
2729 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2730 if (neg)
2731 isl_int_neg(ineq_i[0], ineq_i[0]);
2732 if (more_relaxed)
2733 break;
2734 else
2735 continue;
2737 bound = is_bound(data, set, i, ineq, 0);
2738 if (bound < 0)
2739 return isl_basic_set_free(hull);
2740 if (!bound)
2741 break;
2743 if (i < set->n)
2744 return hull;
2746 k = isl_basic_set_alloc_inequality(hull);
2747 if (k < 0)
2748 return isl_basic_set_free(hull);
2749 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2751 return hull;
2754 /* Compute a superset of the convex hull of "set" that is described
2755 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2756 * has no parameters or integer divisions.
2758 * The inequalities in "ineq" are assumed to have been sorted such
2759 * that constraints with the same linear part appear together and
2760 * that among constraints with the same linear part, those with
2761 * smaller constant term appear first.
2763 * We reuse the same data structure that is used by uset_simple_hull,
2764 * but we do not need the hull table since we will not consider the
2765 * same constraint more than once. We therefore allocate it with zero size.
2767 * We run through the constraints and try to add them one by one,
2768 * skipping identical constraints. If we have added a constraint and
2769 * the next constraint is a more relaxed translate, then we skip this
2770 * next constraint as well.
2772 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2773 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2775 int i;
2776 int last_added = 0;
2777 struct sh_data *data = NULL;
2778 isl_basic_set *hull = NULL;
2779 unsigned dim;
2781 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2782 if (!hull)
2783 goto error;
2785 data = sh_data_alloc(set, 0);
2786 if (!data)
2787 goto error;
2789 dim = isl_set_dim(set, isl_dim_set);
2790 for (i = 0; i < n_ineq; ++i) {
2791 int hull_n_ineq = hull->n_ineq;
2792 int parallel;
2794 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2795 dim);
2796 if (parallel &&
2797 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2798 continue;
2799 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2800 if (!hull)
2801 goto error;
2802 last_added = hull->n_ineq > hull_n_ineq;
2805 sh_data_free(data);
2806 isl_set_free(set);
2807 return hull;
2808 error:
2809 sh_data_free(data);
2810 isl_set_free(set);
2811 isl_basic_set_free(hull);
2812 return NULL;
2815 /* Collect pointers to all the inequalities in the elements of "list"
2816 * in "ineq". For equalities, store both a pointer to the equality and
2817 * a pointer to its opposite, which is first copied to "mat".
2818 * "ineq" and "mat" are assumed to have been preallocated to the right size
2819 * (the number of inequalities + 2 times the number of equalites and
2820 * the number of equalities, respectively).
2822 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2823 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2825 int i, j, n, n_eq, n_ineq;
2827 if (!mat)
2828 return NULL;
2830 n_eq = 0;
2831 n_ineq = 0;
2832 n = isl_basic_set_list_n_basic_set(list);
2833 for (i = 0; i < n; ++i) {
2834 isl_basic_set *bset;
2835 bset = isl_basic_set_list_get_basic_set(list, i);
2836 if (!bset)
2837 return isl_mat_free(mat);
2838 for (j = 0; j < bset->n_eq; ++j) {
2839 ineq[n_ineq++] = mat->row[n_eq];
2840 ineq[n_ineq++] = bset->eq[j];
2841 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2843 for (j = 0; j < bset->n_ineq; ++j)
2844 ineq[n_ineq++] = bset->ineq[j];
2845 isl_basic_set_free(bset);
2848 return mat;
2851 /* Comparison routine for use as an isl_sort callback.
2853 * Constraints with the same linear part are sorted together and
2854 * among constraints with the same linear part, those with smaller
2855 * constant term are sorted first.
2857 static int cmp_ineq(const void *a, const void *b, void *arg)
2859 unsigned dim = *(unsigned *) arg;
2860 isl_int * const *ineq1 = a;
2861 isl_int * const *ineq2 = b;
2862 int cmp;
2864 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2865 if (cmp != 0)
2866 return cmp;
2867 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2870 /* Compute a superset of the convex hull of "set" that is described
2871 * by only constraints in the elements of "list", where "set" has
2872 * no parameters or integer divisions.
2874 * We collect all the constraints in those elements and then
2875 * sort the constraints such that constraints with the same linear part
2876 * are sorted together and that those with smaller constant term are
2877 * sorted first.
2879 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2880 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2882 int i, n, n_eq, n_ineq;
2883 unsigned dim;
2884 isl_ctx *ctx;
2885 isl_mat *mat = NULL;
2886 isl_int **ineq = NULL;
2887 isl_basic_set *hull;
2889 if (!set)
2890 goto error;
2891 ctx = isl_set_get_ctx(set);
2893 n_eq = 0;
2894 n_ineq = 0;
2895 n = isl_basic_set_list_n_basic_set(list);
2896 for (i = 0; i < n; ++i) {
2897 isl_basic_set *bset;
2898 bset = isl_basic_set_list_get_basic_set(list, i);
2899 if (!bset)
2900 goto error;
2901 n_eq += bset->n_eq;
2902 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2903 isl_basic_set_free(bset);
2906 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2907 if (n_ineq > 0 && !ineq)
2908 goto error;
2910 dim = isl_set_dim(set, isl_dim_set);
2911 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2912 mat = collect_inequalities(mat, list, ineq);
2913 if (!mat)
2914 goto error;
2916 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2917 goto error;
2919 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2921 isl_mat_free(mat);
2922 free(ineq);
2923 isl_basic_set_list_free(list);
2924 return hull;
2925 error:
2926 isl_mat_free(mat);
2927 free(ineq);
2928 isl_set_free(set);
2929 isl_basic_set_list_free(list);
2930 return NULL;
2933 /* Compute a superset of the convex hull of "map" that is described
2934 * by only constraints in the elements of "list".
2936 * If the list is empty, then we can only describe the universe set.
2937 * If the input map is empty, then all constraints are valid, so
2938 * we return the intersection of the elements in "list".
2940 * Otherwise, we align all divs and temporarily treat them
2941 * as regular variables, computing the unshifted simple hull in
2942 * uset_unshifted_simple_hull_from_basic_set_list.
2944 static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2945 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2947 isl_basic_map *model;
2948 isl_basic_map *hull;
2949 isl_set *set;
2950 isl_basic_set_list *bset_list;
2952 if (!map || !list)
2953 goto error;
2955 if (isl_basic_map_list_n_basic_map(list) == 0) {
2956 isl_space *space;
2958 space = isl_map_get_space(map);
2959 isl_map_free(map);
2960 isl_basic_map_list_free(list);
2961 return isl_basic_map_universe(space);
2963 if (isl_map_plain_is_empty(map)) {
2964 isl_map_free(map);
2965 return isl_basic_map_list_intersect(list);
2968 map = isl_map_align_divs_to_basic_map_list(map, list);
2969 if (!map)
2970 goto error;
2971 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2973 model = isl_basic_map_list_get_basic_map(list, 0);
2975 set = isl_map_underlying_set(map);
2976 bset_list = isl_basic_map_list_underlying_set(list);
2978 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2979 hull = isl_basic_map_overlying_set(hull, model);
2981 return hull;
2982 error:
2983 isl_map_free(map);
2984 isl_basic_map_list_free(list);
2985 return NULL;
2988 /* Return a sequence of the basic maps that make up the maps in "list".
2990 static __isl_give isl_basic_map_list *collect_basic_maps(
2991 __isl_take isl_map_list *list)
2993 int i, n;
2994 isl_ctx *ctx;
2995 isl_basic_map_list *bmap_list;
2997 if (!list)
2998 return NULL;
2999 n = isl_map_list_n_map(list);
3000 ctx = isl_map_list_get_ctx(list);
3001 bmap_list = isl_basic_map_list_alloc(ctx, 0);
3003 for (i = 0; i < n; ++i) {
3004 isl_map *map;
3005 isl_basic_map_list *list_i;
3007 map = isl_map_list_get_map(list, i);
3008 map = isl_map_compute_divs(map);
3009 list_i = isl_map_get_basic_map_list(map);
3010 isl_map_free(map);
3011 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
3014 isl_map_list_free(list);
3015 return bmap_list;
3018 /* Compute a superset of the convex hull of "map" that is described
3019 * by only constraints in the elements of "list".
3021 * If "map" is the universe, then the convex hull (and therefore
3022 * any superset of the convexhull) is the universe as well.
3024 * Otherwise, we collect all the basic maps in the map list and
3025 * continue with map_unshifted_simple_hull_from_basic_map_list.
3027 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
3028 __isl_take isl_map *map, __isl_take isl_map_list *list)
3030 isl_basic_map_list *bmap_list;
3031 int is_universe;
3033 is_universe = isl_map_plain_is_universe(map);
3034 if (is_universe < 0)
3035 map = isl_map_free(map);
3036 if (is_universe < 0 || is_universe) {
3037 isl_map_list_free(list);
3038 return isl_map_unshifted_simple_hull(map);
3041 bmap_list = collect_basic_maps(list);
3042 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
3045 /* Compute a superset of the convex hull of "set" that is described
3046 * by only constraints in the elements of "list".
3048 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
3049 __isl_take isl_set *set, __isl_take isl_set_list *list)
3051 return isl_map_unshifted_simple_hull_from_map_list(set, list);
3054 /* Given a set "set", return parametric bounds on the dimension "dim".
3056 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
3058 unsigned set_dim = isl_set_dim(set, isl_dim_set);
3059 set = isl_set_copy(set);
3060 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
3061 set = isl_set_eliminate_dims(set, 0, dim);
3062 return isl_set_convex_hull(set);
3065 /* Computes a "simple hull" and then check if each dimension in the
3066 * resulting hull is bounded by a symbolic constant. If not, the
3067 * hull is intersected with the corresponding bounds on the whole set.
3069 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
3071 int i, j;
3072 struct isl_basic_set *hull;
3073 unsigned nparam, left;
3074 int removed_divs = 0;
3076 hull = isl_set_simple_hull(isl_set_copy(set));
3077 if (!hull)
3078 goto error;
3080 nparam = isl_basic_set_dim(hull, isl_dim_param);
3081 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
3082 int lower = 0, upper = 0;
3083 struct isl_basic_set *bounds;
3085 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
3086 for (j = 0; j < hull->n_eq; ++j) {
3087 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3088 continue;
3089 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3090 left) == -1)
3091 break;
3093 if (j < hull->n_eq)
3094 continue;
3096 for (j = 0; j < hull->n_ineq; ++j) {
3097 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3098 continue;
3099 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3100 left) != -1 ||
3101 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3102 i) != -1)
3103 continue;
3104 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3105 lower = 1;
3106 else
3107 upper = 1;
3108 if (lower && upper)
3109 break;
3112 if (lower && upper)
3113 continue;
3115 if (!removed_divs) {
3116 set = isl_set_remove_divs(set);
3117 if (!set)
3118 goto error;
3119 removed_divs = 1;
3121 bounds = set_bounds(set, i);
3122 hull = isl_basic_set_intersect(hull, bounds);
3123 if (!hull)
3124 goto error;
3127 isl_set_free(set);
3128 return hull;
3129 error:
3130 isl_set_free(set);
3131 return NULL;