merge isl_pw_*_from_* implementations
[isl.git] / isl_convex_hull.c
bloba6af5baa729037c031203f235f0ef45c61d4f6d7
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 __isl_give isl_basic_set *uset_convex_hull_wrap_bounded(
31 __isl_take isl_set *set);
33 /* Remove redundant
34 * constraints. If the minimal value along the normal of a constraint
35 * is the same if the constraint is removed, then the constraint is redundant.
37 * Since some constraints may be mutually redundant, sort the constraints
38 * first such that constraints that involve existentially quantified
39 * variables are considered for removal before those that do not.
40 * The sorting is also needed for the use in map_simple_hull.
42 * Note that isl_tab_detect_implicit_equalities may also end up
43 * marking some constraints as redundant. Make sure the constraints
44 * are preserved and undo those marking such that isl_tab_detect_redundant
45 * can consider the constraints in the sorted order.
47 * Alternatively, we could have intersected the basic map with the
48 * corresponding equality and then checked if the dimension was that
49 * of a facet.
51 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
52 __isl_take isl_basic_map *bmap)
54 struct isl_tab *tab;
56 if (!bmap)
57 return NULL;
59 bmap = isl_basic_map_gauss(bmap, NULL);
60 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
61 return bmap;
62 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
63 return bmap;
64 if (bmap->n_ineq <= 1)
65 return bmap;
67 bmap = isl_basic_map_sort_constraints(bmap);
68 tab = isl_tab_from_basic_map(bmap, 0);
69 if (!tab)
70 goto error;
71 tab->preserve = 1;
72 if (isl_tab_detect_implicit_equalities(tab) < 0)
73 goto error;
74 if (isl_tab_restore_redundant(tab) < 0)
75 goto error;
76 tab->preserve = 0;
77 if (isl_tab_detect_redundant(tab) < 0)
78 goto error;
79 bmap = isl_basic_map_update_from_tab(bmap, tab);
80 isl_tab_free(tab);
81 if (!bmap)
82 return NULL;
83 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
84 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
85 return bmap;
86 error:
87 isl_tab_free(tab);
88 isl_basic_map_free(bmap);
89 return NULL;
92 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
93 __isl_take isl_basic_set *bset)
95 return bset_from_bmap(
96 isl_basic_map_remove_redundancies(bset_to_bmap(bset)));
99 /* Remove redundant constraints in each of the basic maps.
101 __isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
103 return isl_map_inline_foreach_basic_map(map,
104 &isl_basic_map_remove_redundancies);
107 __isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
109 return isl_map_remove_redundancies(set);
112 /* Check if the set set is bound in the direction of the affine
113 * constraint c and if so, set the constant term such that the
114 * resulting constraint is a bounding constraint for the set.
116 static isl_bool uset_is_bound(__isl_keep isl_set *set, isl_int *c, unsigned len)
118 int first;
119 int j;
120 isl_int opt;
121 isl_int opt_denom;
123 isl_int_init(opt);
124 isl_int_init(opt_denom);
125 first = 1;
126 for (j = 0; j < set->n; ++j) {
127 enum isl_lp_result res;
129 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
130 continue;
132 res = isl_basic_set_solve_lp(set->p[j],
133 0, c, set->ctx->one, &opt, &opt_denom, NULL);
134 if (res == isl_lp_unbounded)
135 break;
136 if (res == isl_lp_error)
137 goto error;
138 if (res == isl_lp_empty) {
139 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
140 if (!set->p[j])
141 goto error;
142 continue;
144 if (first || isl_int_is_neg(opt)) {
145 if (!isl_int_is_one(opt_denom))
146 isl_seq_scale(c, c, opt_denom, len);
147 isl_int_sub(c[0], c[0], opt);
149 first = 0;
151 isl_int_clear(opt);
152 isl_int_clear(opt_denom);
153 return isl_bool_ok(j >= set->n);
154 error:
155 isl_int_clear(opt);
156 isl_int_clear(opt_denom);
157 return isl_bool_error;
160 static __isl_give isl_set *isl_set_add_basic_set_equality(
161 __isl_take isl_set *set, isl_int *c)
163 int i;
165 set = isl_set_cow(set);
166 if (!set)
167 return NULL;
168 for (i = 0; i < set->n; ++i) {
169 set->p[i] = isl_basic_set_add_eq(set->p[i], c);
170 if (!set->p[i])
171 goto error;
173 return set;
174 error:
175 isl_set_free(set);
176 return NULL;
179 /* Given a union of basic sets, construct the constraints for wrapping
180 * a facet around one of its ridges.
181 * In particular, if each of n the d-dimensional basic sets i in "set"
182 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
183 * and is defined by the constraints
184 * [ 1 ]
185 * A_i [ x ] >= 0
187 * then the resulting set is of dimension n*(1+d) and has as constraints
189 * [ a_i ]
190 * A_i [ x_i ] >= 0
192 * a_i >= 0
194 * \sum_i x_{i,1} = 1
196 static __isl_give isl_basic_set *wrap_constraints(__isl_keep isl_set *set)
198 struct isl_basic_set *lp;
199 unsigned n_eq;
200 unsigned n_ineq;
201 int i, j, k;
202 isl_size dim, lp_dim;
204 dim = isl_set_dim(set, isl_dim_set);
205 if (dim < 0)
206 return NULL;
208 dim += 1;
209 n_eq = 1;
210 n_ineq = set->n;
211 for (i = 0; i < set->n; ++i) {
212 n_eq += set->p[i]->n_eq;
213 n_ineq += set->p[i]->n_ineq;
215 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
216 lp = isl_basic_set_set_rational(lp);
217 if (!lp)
218 return NULL;
219 lp_dim = isl_basic_set_dim(lp, isl_dim_set);
220 if (lp_dim < 0)
221 return isl_basic_set_free(lp);
222 k = isl_basic_set_alloc_equality(lp);
223 isl_int_set_si(lp->eq[k][0], -1);
224 for (i = 0; i < set->n; ++i) {
225 isl_int_set_si(lp->eq[k][1+dim*i], 0);
226 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
227 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
229 for (i = 0; i < set->n; ++i) {
230 k = isl_basic_set_alloc_inequality(lp);
231 isl_seq_clr(lp->ineq[k], 1+lp_dim);
232 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
234 for (j = 0; j < set->p[i]->n_eq; ++j) {
235 k = isl_basic_set_alloc_equality(lp);
236 isl_seq_clr(lp->eq[k], 1+dim*i);
237 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
238 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
241 for (j = 0; j < set->p[i]->n_ineq; ++j) {
242 k = isl_basic_set_alloc_inequality(lp);
243 isl_seq_clr(lp->ineq[k], 1+dim*i);
244 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
245 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
248 return lp;
251 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
252 * of that facet, compute the other facet of the convex hull that contains
253 * the ridge.
255 * We first transform the set such that the facet constraint becomes
257 * x_1 >= 0
259 * I.e., the facet lies in
261 * x_1 = 0
263 * and on that facet, the constraint that defines the ridge is
265 * x_2 >= 0
267 * (This transformation is not strictly needed, all that is needed is
268 * that the ridge contains the origin.)
270 * Since the ridge contains the origin, the cone of the convex hull
271 * will be of the form
273 * x_1 >= 0
274 * x_2 >= a x_1
276 * with this second constraint defining the new facet.
277 * The constant a is obtained by settting x_1 in the cone of the
278 * convex hull to 1 and minimizing x_2.
279 * Now, each element in the cone of the convex hull is the sum
280 * of elements in the cones of the basic sets.
281 * If a_i is the dilation factor of basic set i, then the problem
282 * we need to solve is
284 * min \sum_i x_{i,2}
285 * st
286 * \sum_i x_{i,1} = 1
287 * a_i >= 0
288 * [ a_i ]
289 * A [ x_i ] >= 0
291 * with
292 * [ 1 ]
293 * A_i [ x_i ] >= 0
295 * the constraints of each (transformed) basic set.
296 * If a = n/d, then the constraint defining the new facet (in the transformed
297 * space) is
299 * -n x_1 + d x_2 >= 0
301 * In the original space, we need to take the same combination of the
302 * corresponding constraints "facet" and "ridge".
304 * If a = -infty = "-1/0", then we just return the original facet constraint.
305 * This means that the facet is unbounded, but has a bounded intersection
306 * with the union of sets.
308 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
309 isl_int *facet, isl_int *ridge)
311 int i;
312 isl_ctx *ctx;
313 struct isl_mat *T = NULL;
314 struct isl_basic_set *lp = NULL;
315 struct isl_vec *obj;
316 enum isl_lp_result res;
317 isl_int num, den;
318 isl_size dim;
320 dim = isl_set_dim(set, isl_dim_set);
321 if (dim < 0)
322 return NULL;
323 ctx = set->ctx;
324 set = isl_set_copy(set);
325 set = isl_set_set_rational(set);
327 dim += 1;
328 T = isl_mat_alloc(ctx, 3, dim);
329 if (!T)
330 goto error;
331 isl_int_set_si(T->row[0][0], 1);
332 isl_seq_clr(T->row[0]+1, dim - 1);
333 isl_seq_cpy(T->row[1], facet, dim);
334 isl_seq_cpy(T->row[2], ridge, dim);
335 T = isl_mat_right_inverse(T);
336 set = isl_set_preimage(set, T);
337 T = NULL;
338 if (!set)
339 goto error;
340 lp = wrap_constraints(set);
341 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
342 if (!obj)
343 goto error;
344 isl_int_set_si(obj->block.data[0], 0);
345 for (i = 0; i < set->n; ++i) {
346 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
347 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
348 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
350 isl_int_init(num);
351 isl_int_init(den);
352 res = isl_basic_set_solve_lp(lp, 0,
353 obj->block.data, ctx->one, &num, &den, NULL);
354 if (res == isl_lp_ok) {
355 isl_int_neg(num, num);
356 isl_seq_combine(facet, num, facet, den, ridge, dim);
357 isl_seq_normalize(ctx, facet, dim);
359 isl_int_clear(num);
360 isl_int_clear(den);
361 isl_vec_free(obj);
362 isl_basic_set_free(lp);
363 isl_set_free(set);
364 if (res == isl_lp_error)
365 return NULL;
366 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
367 return NULL);
368 return facet;
369 error:
370 isl_basic_set_free(lp);
371 isl_mat_free(T);
372 isl_set_free(set);
373 return NULL;
376 /* Compute the constraint of a facet of "set".
378 * We first compute the intersection with a bounding constraint
379 * that is orthogonal to one of the coordinate axes.
380 * If the affine hull of this intersection has only one equality,
381 * we have found a facet.
382 * Otherwise, we wrap the current bounding constraint around
383 * one of the equalities of the face (one that is not equal to
384 * the current bounding constraint).
385 * This process continues until we have found a facet.
386 * The dimension of the intersection increases by at least
387 * one on each iteration, so termination is guaranteed.
389 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
391 struct isl_set *slice = NULL;
392 struct isl_basic_set *face = NULL;
393 int i;
394 isl_size dim = isl_set_dim(set, isl_dim_set);
395 isl_bool is_bound;
396 isl_mat *bounds = NULL;
398 if (dim < 0)
399 return NULL;
400 isl_assert(set->ctx, set->n > 0, goto error);
401 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
402 if (!bounds)
403 return NULL;
405 isl_seq_clr(bounds->row[0], dim);
406 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
407 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
408 if (is_bound < 0)
409 goto error;
410 isl_assert(set->ctx, is_bound, goto error);
411 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
412 bounds->n_row = 1;
414 for (;;) {
415 slice = isl_set_copy(set);
416 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
417 face = isl_set_affine_hull(slice);
418 if (!face)
419 goto error;
420 if (face->n_eq == 1) {
421 isl_basic_set_free(face);
422 break;
424 for (i = 0; i < face->n_eq; ++i)
425 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
426 !isl_seq_is_neg(bounds->row[0],
427 face->eq[i], 1 + dim))
428 break;
429 isl_assert(set->ctx, i < face->n_eq, goto error);
430 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
431 goto error;
432 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
433 isl_basic_set_free(face);
436 return bounds;
437 error:
438 isl_basic_set_free(face);
439 isl_mat_free(bounds);
440 return NULL;
443 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
444 * compute a hyperplane description of the facet, i.e., compute the facets
445 * of the facet.
447 * We compute an affine transformation that transforms the constraint
449 * [ 1 ]
450 * c [ x ] = 0
452 * to the constraint
454 * z_1 = 0
456 * by computing the right inverse U of a matrix that starts with the rows
458 * [ 1 0 ]
459 * [ c ]
461 * Then
462 * [ 1 ] [ 1 ]
463 * [ x ] = U [ z ]
464 * and
465 * [ 1 ] [ 1 ]
466 * [ z ] = Q [ x ]
468 * with Q = U^{-1}
469 * Since z_1 is zero, we can drop this variable as well as the corresponding
470 * column of U to obtain
472 * [ 1 ] [ 1 ]
473 * [ x ] = U' [ z' ]
474 * and
475 * [ 1 ] [ 1 ]
476 * [ z' ] = Q' [ x ]
478 * with Q' equal to Q, but without the corresponding row.
479 * After computing the facets of the facet in the z' space,
480 * we convert them back to the x space through Q.
482 static __isl_give isl_basic_set *compute_facet(__isl_keep isl_set *set,
483 isl_int *c)
485 struct isl_mat *m, *U, *Q;
486 struct isl_basic_set *facet = NULL;
487 struct isl_ctx *ctx;
488 isl_size dim;
490 dim = isl_set_dim(set, isl_dim_set);
491 if (dim < 0)
492 return NULL;
493 ctx = set->ctx;
494 set = isl_set_copy(set);
495 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
496 if (!m)
497 goto error;
498 isl_int_set_si(m->row[0][0], 1);
499 isl_seq_clr(m->row[0]+1, dim);
500 isl_seq_cpy(m->row[1], c, 1+dim);
501 U = isl_mat_right_inverse(m);
502 Q = isl_mat_right_inverse(isl_mat_copy(U));
503 U = isl_mat_drop_cols(U, 1, 1);
504 Q = isl_mat_drop_rows(Q, 1, 1);
505 set = isl_set_preimage(set, U);
506 facet = uset_convex_hull_wrap_bounded(set);
507 facet = isl_basic_set_preimage(facet, Q);
508 if (facet && facet->n_eq != 0)
509 isl_die(ctx, isl_error_internal, "unexpected equality",
510 return isl_basic_set_free(facet));
511 return facet;
512 error:
513 isl_basic_set_free(facet);
514 isl_set_free(set);
515 return NULL;
518 /* Given an initial facet constraint, compute the remaining facets.
519 * We do this by running through all facets found so far and computing
520 * the adjacent facets through wrapping, adding those facets that we
521 * hadn't already found before.
523 * For each facet we have found so far, we first compute its facets
524 * in the resulting convex hull. That is, we compute the ridges
525 * of the resulting convex hull contained in the facet.
526 * We also compute the corresponding facet in the current approximation
527 * of the convex hull. There is no need to wrap around the ridges
528 * in this facet since that would result in a facet that is already
529 * present in the current approximation.
531 * This function can still be significantly optimized by checking which of
532 * the facets of the basic sets are also facets of the convex hull and
533 * using all the facets so far to help in constructing the facets of the
534 * facets
535 * and/or
536 * using the technique in section "3.1 Ridge Generation" of
537 * "Extended Convex Hull" by Fukuda et al.
539 static __isl_give isl_basic_set *extend(__isl_take isl_basic_set *hull,
540 __isl_keep isl_set *set)
542 int i, j, f;
543 int k;
544 struct isl_basic_set *facet = NULL;
545 struct isl_basic_set *hull_facet = NULL;
546 isl_size dim;
548 dim = isl_set_dim(set, isl_dim_set);
549 if (dim < 0 || !hull)
550 return isl_basic_set_free(hull);
552 isl_assert(set->ctx, set->n > 0, goto error);
554 for (i = 0; i < hull->n_ineq; ++i) {
555 facet = compute_facet(set, hull->ineq[i]);
556 facet = isl_basic_set_add_eq(facet, hull->ineq[i]);
557 facet = isl_basic_set_gauss(facet, NULL);
558 facet = isl_basic_set_normalize_constraints(facet);
559 hull_facet = isl_basic_set_copy(hull);
560 hull_facet = isl_basic_set_add_eq(hull_facet, hull->ineq[i]);
561 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
562 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
563 if (!facet || !hull_facet)
564 goto error;
565 hull = isl_basic_set_cow(hull);
566 hull = isl_basic_set_extend_space(hull,
567 isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
568 if (!hull)
569 goto error;
570 for (j = 0; j < facet->n_ineq; ++j) {
571 for (f = 0; f < hull_facet->n_ineq; ++f)
572 if (isl_seq_eq(facet->ineq[j],
573 hull_facet->ineq[f], 1 + dim))
574 break;
575 if (f < hull_facet->n_ineq)
576 continue;
577 k = isl_basic_set_alloc_inequality(hull);
578 if (k < 0)
579 goto error;
580 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
581 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
582 goto error;
584 isl_basic_set_free(hull_facet);
585 isl_basic_set_free(facet);
587 hull = isl_basic_set_simplify(hull);
588 hull = isl_basic_set_finalize(hull);
589 return hull;
590 error:
591 isl_basic_set_free(hull_facet);
592 isl_basic_set_free(facet);
593 isl_basic_set_free(hull);
594 return NULL;
597 /* Special case for computing the convex hull of a one dimensional set.
598 * We simply collect the lower and upper bounds of each basic set
599 * and the biggest of those.
601 static __isl_give isl_basic_set *convex_hull_1d(__isl_take isl_set *set)
603 struct isl_mat *c = NULL;
604 isl_int *lower = NULL;
605 isl_int *upper = NULL;
606 int i, j, k;
607 isl_int a, b;
608 struct isl_basic_set *hull;
610 for (i = 0; i < set->n; ++i) {
611 set->p[i] = isl_basic_set_simplify(set->p[i]);
612 if (!set->p[i])
613 goto error;
615 set = isl_set_remove_empty_parts(set);
616 if (!set)
617 goto error;
618 isl_assert(set->ctx, set->n > 0, goto error);
619 c = isl_mat_alloc(set->ctx, 2, 2);
620 if (!c)
621 goto error;
623 if (set->p[0]->n_eq > 0) {
624 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
625 lower = c->row[0];
626 upper = c->row[1];
627 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
628 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
629 isl_seq_neg(upper, set->p[0]->eq[0], 2);
630 } else {
631 isl_seq_neg(lower, set->p[0]->eq[0], 2);
632 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
634 } else {
635 for (j = 0; j < set->p[0]->n_ineq; ++j) {
636 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
637 lower = c->row[0];
638 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
639 } else {
640 upper = c->row[1];
641 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
646 isl_int_init(a);
647 isl_int_init(b);
648 for (i = 0; i < set->n; ++i) {
649 struct isl_basic_set *bset = set->p[i];
650 int has_lower = 0;
651 int has_upper = 0;
653 for (j = 0; j < bset->n_eq; ++j) {
654 has_lower = 1;
655 has_upper = 1;
656 if (lower) {
657 isl_int_mul(a, lower[0], bset->eq[j][1]);
658 isl_int_mul(b, lower[1], bset->eq[j][0]);
659 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
660 isl_seq_cpy(lower, bset->eq[j], 2);
661 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
662 isl_seq_neg(lower, bset->eq[j], 2);
664 if (upper) {
665 isl_int_mul(a, upper[0], bset->eq[j][1]);
666 isl_int_mul(b, upper[1], bset->eq[j][0]);
667 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
668 isl_seq_neg(upper, bset->eq[j], 2);
669 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
670 isl_seq_cpy(upper, bset->eq[j], 2);
673 for (j = 0; j < bset->n_ineq; ++j) {
674 if (isl_int_is_pos(bset->ineq[j][1]))
675 has_lower = 1;
676 if (isl_int_is_neg(bset->ineq[j][1]))
677 has_upper = 1;
678 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
679 isl_int_mul(a, lower[0], bset->ineq[j][1]);
680 isl_int_mul(b, lower[1], bset->ineq[j][0]);
681 if (isl_int_lt(a, b))
682 isl_seq_cpy(lower, bset->ineq[j], 2);
684 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
685 isl_int_mul(a, upper[0], bset->ineq[j][1]);
686 isl_int_mul(b, upper[1], bset->ineq[j][0]);
687 if (isl_int_gt(a, b))
688 isl_seq_cpy(upper, bset->ineq[j], 2);
691 if (!has_lower)
692 lower = NULL;
693 if (!has_upper)
694 upper = NULL;
696 isl_int_clear(a);
697 isl_int_clear(b);
699 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
700 hull = isl_basic_set_set_rational(hull);
701 if (!hull)
702 goto error;
703 if (lower) {
704 k = isl_basic_set_alloc_inequality(hull);
705 isl_seq_cpy(hull->ineq[k], lower, 2);
707 if (upper) {
708 k = isl_basic_set_alloc_inequality(hull);
709 isl_seq_cpy(hull->ineq[k], upper, 2);
711 hull = isl_basic_set_finalize(hull);
712 isl_set_free(set);
713 isl_mat_free(c);
714 return hull;
715 error:
716 isl_set_free(set);
717 isl_mat_free(c);
718 return NULL;
721 static __isl_give isl_basic_set *convex_hull_0d(__isl_take isl_set *set)
723 struct isl_basic_set *convex_hull;
725 if (!set)
726 return NULL;
728 if (isl_set_is_empty(set))
729 convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
730 else
731 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
732 isl_set_free(set);
733 return convex_hull;
736 /* Compute the convex hull of a pair of basic sets without any parameters or
737 * integer divisions using Fourier-Motzkin elimination.
738 * The convex hull is the set of all points that can be written as
739 * the sum of points from both basic sets (in homogeneous coordinates).
740 * We set up the constraints in a space with dimensions for each of
741 * the three sets and then project out the dimensions corresponding
742 * to the two original basic sets, retaining only those corresponding
743 * to the convex hull.
745 static __isl_give isl_basic_set *convex_hull_pair_elim(
746 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
748 int i, j, k;
749 struct isl_basic_set *bset[2];
750 struct isl_basic_set *hull = NULL;
751 isl_size dim;
753 dim = isl_basic_set_dim(bset1, isl_dim_set);
754 if (dim < 0 || !bset2)
755 goto error;
757 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
758 1 + dim + bset1->n_eq + bset2->n_eq,
759 2 + bset1->n_ineq + bset2->n_ineq);
760 bset[0] = bset1;
761 bset[1] = bset2;
762 for (i = 0; i < 2; ++i) {
763 for (j = 0; j < bset[i]->n_eq; ++j) {
764 k = isl_basic_set_alloc_equality(hull);
765 if (k < 0)
766 goto error;
767 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
768 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
769 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
770 1+dim);
772 for (j = 0; j < bset[i]->n_ineq; ++j) {
773 k = isl_basic_set_alloc_inequality(hull);
774 if (k < 0)
775 goto error;
776 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
777 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
778 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
779 bset[i]->ineq[j], 1+dim);
781 k = isl_basic_set_alloc_inequality(hull);
782 if (k < 0)
783 goto error;
784 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
785 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
787 for (j = 0; j < 1+dim; ++j) {
788 k = isl_basic_set_alloc_equality(hull);
789 if (k < 0)
790 goto error;
791 isl_seq_clr(hull->eq[k], 1+2+3*dim);
792 isl_int_set_si(hull->eq[k][j], -1);
793 isl_int_set_si(hull->eq[k][1+dim+j], 1);
794 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
796 hull = isl_basic_set_set_rational(hull);
797 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
798 hull = isl_basic_set_remove_redundancies(hull);
799 isl_basic_set_free(bset1);
800 isl_basic_set_free(bset2);
801 return hull;
802 error:
803 isl_basic_set_free(bset1);
804 isl_basic_set_free(bset2);
805 isl_basic_set_free(hull);
806 return NULL;
809 /* Is the set bounded for each value of the parameters?
811 isl_bool isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
813 struct isl_tab *tab;
814 isl_bool bounded;
816 if (!bset)
817 return isl_bool_error;
818 if (isl_basic_set_plain_is_empty(bset))
819 return isl_bool_true;
821 tab = isl_tab_from_recession_cone(bset, 1);
822 bounded = isl_tab_cone_is_bounded(tab);
823 isl_tab_free(tab);
824 return bounded;
827 /* Is the image bounded for each value of the parameters and
828 * the domain variables?
830 isl_bool isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
832 isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
833 isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
834 isl_bool bounded;
836 if (nparam < 0 || n_in < 0)
837 return isl_bool_error;
839 bmap = isl_basic_map_copy(bmap);
840 bmap = isl_basic_map_cow(bmap);
841 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
842 isl_dim_in, 0, n_in);
843 bounded = isl_basic_set_is_bounded(bset_from_bmap(bmap));
844 isl_basic_map_free(bmap);
846 return bounded;
849 /* Is the set bounded for each value of the parameters?
851 isl_bool isl_set_is_bounded(__isl_keep isl_set *set)
853 int i;
855 if (!set)
856 return isl_bool_error;
858 for (i = 0; i < set->n; ++i) {
859 isl_bool bounded = isl_basic_set_is_bounded(set->p[i]);
860 if (!bounded || bounded < 0)
861 return bounded;
863 return isl_bool_true;
866 /* Compute the lineality space of the convex hull of bset1 and bset2.
868 * We first compute the intersection of the recession cone of bset1
869 * with the negative of the recession cone of bset2 and then compute
870 * the linear hull of the resulting cone.
872 static __isl_give isl_basic_set *induced_lineality_space(
873 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
875 int i, k;
876 struct isl_basic_set *lin = NULL;
877 isl_size dim;
879 dim = isl_basic_set_dim(bset1, isl_dim_all);
880 if (dim < 0 || !bset2)
881 goto error;
883 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
884 bset1->n_eq + bset2->n_eq,
885 bset1->n_ineq + bset2->n_ineq);
886 lin = isl_basic_set_set_rational(lin);
887 if (!lin)
888 goto error;
889 for (i = 0; i < bset1->n_eq; ++i) {
890 k = isl_basic_set_alloc_equality(lin);
891 if (k < 0)
892 goto error;
893 isl_int_set_si(lin->eq[k][0], 0);
894 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
896 for (i = 0; i < bset1->n_ineq; ++i) {
897 k = isl_basic_set_alloc_inequality(lin);
898 if (k < 0)
899 goto error;
900 isl_int_set_si(lin->ineq[k][0], 0);
901 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
903 for (i = 0; i < bset2->n_eq; ++i) {
904 k = isl_basic_set_alloc_equality(lin);
905 if (k < 0)
906 goto error;
907 isl_int_set_si(lin->eq[k][0], 0);
908 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
910 for (i = 0; i < bset2->n_ineq; ++i) {
911 k = isl_basic_set_alloc_inequality(lin);
912 if (k < 0)
913 goto error;
914 isl_int_set_si(lin->ineq[k][0], 0);
915 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
918 isl_basic_set_free(bset1);
919 isl_basic_set_free(bset2);
920 return isl_basic_set_affine_hull(lin);
921 error:
922 isl_basic_set_free(lin);
923 isl_basic_set_free(bset1);
924 isl_basic_set_free(bset2);
925 return NULL;
928 static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set);
930 /* Given a set and a linear space "lin" of dimension n > 0,
931 * project the linear space from the set, compute the convex hull
932 * and then map the set back to the original space.
934 * Let
936 * M x = 0
938 * describe the linear space. We first compute the Hermite normal
939 * form H = M U of M = H Q, to obtain
941 * H Q x = 0
943 * The last n rows of H will be zero, so the last n variables of x' = Q x
944 * are the one we want to project out. We do this by transforming each
945 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
946 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
947 * we transform the hull back to the original space as A' Q_1 x >= b',
948 * with Q_1 all but the last n rows of Q.
950 static __isl_give isl_basic_set *modulo_lineality(__isl_take isl_set *set,
951 __isl_take isl_basic_set *lin)
953 isl_size total = isl_basic_set_dim(lin, isl_dim_all);
954 unsigned lin_dim;
955 struct isl_basic_set *hull;
956 struct isl_mat *M, *U, *Q;
958 if (!set || total < 0)
959 goto error;
960 lin_dim = total - lin->n_eq;
961 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
962 M = isl_mat_left_hermite(M, 0, &U, &Q);
963 if (!M)
964 goto error;
965 isl_mat_free(M);
966 isl_basic_set_free(lin);
968 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
970 U = isl_mat_lin_to_aff(U);
971 Q = isl_mat_lin_to_aff(Q);
973 set = isl_set_preimage(set, U);
974 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
975 hull = uset_convex_hull(set);
976 hull = isl_basic_set_preimage(hull, Q);
978 return hull;
979 error:
980 isl_basic_set_free(lin);
981 isl_set_free(set);
982 return NULL;
985 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
986 * set up an LP for solving
988 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
990 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
991 * The next \alpha{ij} correspond to the equalities and come in pairs.
992 * The final \alpha{ij} correspond to the inequalities.
994 static __isl_give isl_basic_set *valid_direction_lp(
995 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
997 isl_space *dim;
998 struct isl_basic_set *lp;
999 unsigned d;
1000 int n;
1001 int i, j, k;
1002 isl_size total;
1004 total = isl_basic_set_dim(bset1, isl_dim_all);
1005 if (total < 0 || !bset2)
1006 goto error;
1007 d = 1 + total;
1008 n = 2 +
1009 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1010 dim = isl_space_set_alloc(bset1->ctx, 0, n);
1011 lp = isl_basic_set_alloc_space(dim, 0, d, n);
1012 if (!lp)
1013 goto error;
1014 for (i = 0; i < n; ++i) {
1015 k = isl_basic_set_alloc_inequality(lp);
1016 if (k < 0)
1017 goto error;
1018 isl_seq_clr(lp->ineq[k] + 1, n);
1019 isl_int_set_si(lp->ineq[k][0], -1);
1020 isl_int_set_si(lp->ineq[k][1 + i], 1);
1022 for (i = 0; i < d; ++i) {
1023 k = isl_basic_set_alloc_equality(lp);
1024 if (k < 0)
1025 goto error;
1026 n = 0;
1027 isl_int_set_si(lp->eq[k][n], 0); n++;
1028 /* positivity constraint 1 >= 0 */
1029 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1030 for (j = 0; j < bset1->n_eq; ++j) {
1031 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1032 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1034 for (j = 0; j < bset1->n_ineq; ++j) {
1035 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1037 /* positivity constraint 1 >= 0 */
1038 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1039 for (j = 0; j < bset2->n_eq; ++j) {
1040 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1041 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1043 for (j = 0; j < bset2->n_ineq; ++j) {
1044 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1047 lp = isl_basic_set_gauss(lp, NULL);
1048 isl_basic_set_free(bset1);
1049 isl_basic_set_free(bset2);
1050 return lp;
1051 error:
1052 isl_basic_set_free(bset1);
1053 isl_basic_set_free(bset2);
1054 return NULL;
1057 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1058 * for all rays in the homogeneous space of the two cones that correspond
1059 * to the input polyhedra bset1 and bset2.
1061 * We compute s as a vector that satisfies
1063 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1065 * with h_{ij} the normals of the facets of polyhedron i
1066 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1067 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1068 * We first set up an LP with as variables the \alpha{ij}.
1069 * In this formulation, for each polyhedron i,
1070 * the first constraint is the positivity constraint, followed by pairs
1071 * of variables for the equalities, followed by variables for the inequalities.
1072 * We then simply pick a feasible solution and compute s using (*).
1074 * Note that we simply pick any valid direction and make no attempt
1075 * to pick a "good" or even the "best" valid direction.
1077 static __isl_give isl_vec *valid_direction(
1078 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1080 struct isl_basic_set *lp;
1081 struct isl_tab *tab;
1082 struct isl_vec *sample = NULL;
1083 struct isl_vec *dir;
1084 isl_size d;
1085 int i;
1086 int n;
1088 if (!bset1 || !bset2)
1089 goto error;
1090 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1091 isl_basic_set_copy(bset2));
1092 tab = isl_tab_from_basic_set(lp, 0);
1093 sample = isl_tab_get_sample_value(tab);
1094 isl_tab_free(tab);
1095 isl_basic_set_free(lp);
1096 if (!sample)
1097 goto error;
1098 d = isl_basic_set_dim(bset1, isl_dim_all);
1099 if (d < 0)
1100 goto error;
1101 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1102 if (!dir)
1103 goto error;
1104 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1105 n = 1;
1106 /* positivity constraint 1 >= 0 */
1107 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1108 for (i = 0; i < bset1->n_eq; ++i) {
1109 isl_int_sub(sample->block.data[n],
1110 sample->block.data[n], sample->block.data[n+1]);
1111 isl_seq_combine(dir->block.data,
1112 bset1->ctx->one, dir->block.data,
1113 sample->block.data[n], bset1->eq[i], 1 + d);
1115 n += 2;
1117 for (i = 0; i < bset1->n_ineq; ++i)
1118 isl_seq_combine(dir->block.data,
1119 bset1->ctx->one, dir->block.data,
1120 sample->block.data[n++], bset1->ineq[i], 1 + d);
1121 isl_vec_free(sample);
1122 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1123 isl_basic_set_free(bset1);
1124 isl_basic_set_free(bset2);
1125 return dir;
1126 error:
1127 isl_vec_free(sample);
1128 isl_basic_set_free(bset1);
1129 isl_basic_set_free(bset2);
1130 return NULL;
1133 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1134 * compute b_i' + A_i' x' >= 0, with
1136 * [ b_i A_i ] [ y' ] [ y' ]
1137 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1139 * In particular, add the "positivity constraint" and then perform
1140 * the mapping.
1142 static __isl_give isl_basic_set *homogeneous_map(__isl_take isl_basic_set *bset,
1143 __isl_take isl_mat *T)
1145 int k;
1146 isl_size total;
1148 total = isl_basic_set_dim(bset, isl_dim_all);
1149 if (total < 0)
1150 goto error;
1151 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1152 k = isl_basic_set_alloc_inequality(bset);
1153 if (k < 0)
1154 goto error;
1155 isl_seq_clr(bset->ineq[k] + 1, total);
1156 isl_int_set_si(bset->ineq[k][0], 1);
1157 bset = isl_basic_set_preimage(bset, T);
1158 return bset;
1159 error:
1160 isl_mat_free(T);
1161 isl_basic_set_free(bset);
1162 return NULL;
1165 /* Compute the convex hull of a pair of basic sets without any parameters or
1166 * integer divisions, where the convex hull is known to be pointed,
1167 * but the basic sets may be unbounded.
1169 * We turn this problem into the computation of a convex hull of a pair
1170 * _bounded_ polyhedra by "changing the direction of the homogeneous
1171 * dimension". This idea is due to Matthias Koeppe.
1173 * Consider the cones in homogeneous space that correspond to the
1174 * input polyhedra. The rays of these cones are also rays of the
1175 * polyhedra if the coordinate that corresponds to the homogeneous
1176 * dimension is zero. That is, if the inner product of the rays
1177 * with the homogeneous direction is zero.
1178 * The cones in the homogeneous space can also be considered to
1179 * correspond to other pairs of polyhedra by chosing a different
1180 * homogeneous direction. To ensure that both of these polyhedra
1181 * are bounded, we need to make sure that all rays of the cones
1182 * correspond to vertices and not to rays.
1183 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1184 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1185 * The vector s is computed in valid_direction.
1187 * Note that we need to consider _all_ rays of the cones and not just
1188 * the rays that correspond to rays in the polyhedra. If we were to
1189 * only consider those rays and turn them into vertices, then we
1190 * may inadvertently turn some vertices into rays.
1192 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1193 * We therefore transform the two polyhedra such that the selected
1194 * direction is mapped onto this standard direction and then proceed
1195 * with the normal computation.
1196 * Let S be a non-singular square matrix with s as its first row,
1197 * then we want to map the polyhedra to the space
1199 * [ y' ] [ y ] [ y ] [ y' ]
1200 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1202 * We take S to be the unimodular completion of s to limit the growth
1203 * of the coefficients in the following computations.
1205 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1206 * We first move to the homogeneous dimension
1208 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1209 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1211 * Then we change directoin
1213 * [ b_i A_i ] [ y' ] [ y' ]
1214 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1216 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1217 * resulting in b' + A' x' >= 0, which we then convert back
1219 * [ y ] [ y ]
1220 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1222 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1224 static __isl_give isl_basic_set *convex_hull_pair_pointed(
1225 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1227 struct isl_ctx *ctx = NULL;
1228 struct isl_vec *dir = NULL;
1229 struct isl_mat *T = NULL;
1230 struct isl_mat *T2 = NULL;
1231 struct isl_basic_set *hull;
1232 struct isl_set *set;
1234 if (!bset1 || !bset2)
1235 goto error;
1236 ctx = isl_basic_set_get_ctx(bset1);
1237 dir = valid_direction(isl_basic_set_copy(bset1),
1238 isl_basic_set_copy(bset2));
1239 if (!dir)
1240 goto error;
1241 T = isl_mat_alloc(ctx, dir->size, dir->size);
1242 if (!T)
1243 goto error;
1244 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1245 T = isl_mat_unimodular_complete(T, 1);
1246 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1248 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1249 bset2 = homogeneous_map(bset2, T2);
1250 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1251 set = isl_set_add_basic_set(set, bset1);
1252 set = isl_set_add_basic_set(set, bset2);
1253 hull = uset_convex_hull(set);
1254 hull = isl_basic_set_preimage(hull, T);
1256 isl_vec_free(dir);
1258 return hull;
1259 error:
1260 isl_vec_free(dir);
1261 isl_basic_set_free(bset1);
1262 isl_basic_set_free(bset2);
1263 return NULL;
1266 static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set);
1267 static __isl_give isl_basic_set *modulo_affine_hull(
1268 __isl_take isl_set *set, __isl_take isl_basic_set *affine_hull);
1270 /* Compute the convex hull of a pair of basic sets without any parameters or
1271 * integer divisions.
1273 * This function is called from uset_convex_hull_unbounded, which
1274 * means that the complete convex hull is unbounded. Some pairs
1275 * of basic sets may still be bounded, though.
1276 * They may even lie inside a lower dimensional space, in which
1277 * case they need to be handled inside their affine hull since
1278 * the main algorithm assumes that the result is full-dimensional.
1280 * If the convex hull of the two basic sets would have a non-trivial
1281 * lineality space, we first project out this lineality space.
1283 static __isl_give isl_basic_set *convex_hull_pair(
1284 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1286 isl_basic_set *lin, *aff;
1287 isl_bool bounded1, bounded2;
1288 isl_size total;
1290 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1291 return convex_hull_pair_elim(bset1, bset2);
1293 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1294 isl_basic_set_copy(bset2)));
1295 if (!aff)
1296 goto error;
1297 if (aff->n_eq != 0)
1298 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1299 isl_basic_set_free(aff);
1301 bounded1 = isl_basic_set_is_bounded(bset1);
1302 bounded2 = isl_basic_set_is_bounded(bset2);
1304 if (bounded1 < 0 || bounded2 < 0)
1305 goto error;
1307 if (bounded1 && bounded2)
1308 return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1310 if (bounded1 || bounded2)
1311 return convex_hull_pair_pointed(bset1, bset2);
1313 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1314 isl_basic_set_copy(bset2));
1315 if (!lin)
1316 goto error;
1317 if (isl_basic_set_plain_is_universe(lin)) {
1318 isl_basic_set_free(bset1);
1319 isl_basic_set_free(bset2);
1320 return lin;
1322 total = isl_basic_set_dim(lin, isl_dim_all);
1323 if (lin->n_eq < total) {
1324 struct isl_set *set;
1325 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1326 set = isl_set_add_basic_set(set, bset1);
1327 set = isl_set_add_basic_set(set, bset2);
1328 return modulo_lineality(set, lin);
1330 isl_basic_set_free(lin);
1331 if (total < 0)
1332 goto error;
1334 return convex_hull_pair_pointed(bset1, bset2);
1335 error:
1336 isl_basic_set_free(bset1);
1337 isl_basic_set_free(bset2);
1338 return NULL;
1341 /* Compute the lineality space of a basic set.
1342 * We basically just drop the constants and turn every inequality
1343 * into an equality.
1344 * Any explicit representations of local variables are removed
1345 * because they may no longer be valid representations
1346 * in the lineality space.
1348 __isl_give isl_basic_set *isl_basic_set_lineality_space(
1349 __isl_take isl_basic_set *bset)
1351 int i, k;
1352 struct isl_basic_set *lin = NULL;
1353 isl_size n_div, dim;
1355 n_div = isl_basic_set_dim(bset, isl_dim_div);
1356 dim = isl_basic_set_dim(bset, isl_dim_all);
1357 if (n_div < 0 || dim < 0)
1358 return isl_basic_set_free(bset);
1360 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset),
1361 n_div, dim, 0);
1362 for (i = 0; i < n_div; ++i)
1363 if (isl_basic_set_alloc_div(lin) < 0)
1364 goto error;
1365 if (!lin)
1366 goto error;
1367 for (i = 0; i < bset->n_eq; ++i) {
1368 k = isl_basic_set_alloc_equality(lin);
1369 if (k < 0)
1370 goto error;
1371 isl_int_set_si(lin->eq[k][0], 0);
1372 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1374 lin = isl_basic_set_gauss(lin, NULL);
1375 if (!lin)
1376 goto error;
1377 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1378 k = isl_basic_set_alloc_equality(lin);
1379 if (k < 0)
1380 goto error;
1381 isl_int_set_si(lin->eq[k][0], 0);
1382 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1383 lin = isl_basic_set_gauss(lin, NULL);
1384 if (!lin)
1385 goto error;
1387 isl_basic_set_free(bset);
1388 return lin;
1389 error:
1390 isl_basic_set_free(lin);
1391 isl_basic_set_free(bset);
1392 return NULL;
1395 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1396 * set "set".
1398 __isl_give isl_basic_set *isl_set_combined_lineality_space(
1399 __isl_take isl_set *set)
1401 int i;
1402 struct isl_set *lin = NULL;
1404 if (!set)
1405 return NULL;
1406 if (set->n == 0) {
1407 isl_space *space = isl_set_get_space(set);
1408 isl_set_free(set);
1409 return isl_basic_set_empty(space);
1412 lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
1413 for (i = 0; i < set->n; ++i)
1414 lin = isl_set_add_basic_set(lin,
1415 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1416 isl_set_free(set);
1417 return isl_set_affine_hull(lin);
1420 /* Compute the convex hull of a set without any parameters or
1421 * integer divisions.
1422 * In each step, we combined two basic sets until only one
1423 * basic set is left.
1424 * The input basic sets are assumed not to have a non-trivial
1425 * lineality space. If any of the intermediate results has
1426 * a non-trivial lineality space, it is projected out.
1428 static __isl_give isl_basic_set *uset_convex_hull_unbounded(
1429 __isl_take isl_set *set)
1431 isl_basic_set_list *list;
1433 list = isl_set_get_basic_set_list(set);
1434 isl_set_free(set);
1436 while (list) {
1437 isl_size n, total;
1438 struct isl_basic_set *t;
1439 isl_basic_set *bset1, *bset2;
1441 n = isl_basic_set_list_n_basic_set(list);
1442 if (n < 0)
1443 goto error;
1444 if (n < 2)
1445 isl_die(isl_basic_set_list_get_ctx(list),
1446 isl_error_internal,
1447 "expecting at least two elements", goto error);
1448 bset1 = isl_basic_set_list_get_basic_set(list, n - 1);
1449 bset2 = isl_basic_set_list_get_basic_set(list, n - 2);
1450 bset1 = convex_hull_pair(bset1, bset2);
1451 if (n == 2) {
1452 isl_basic_set_list_free(list);
1453 return bset1;
1455 bset1 = isl_basic_set_underlying_set(bset1);
1456 list = isl_basic_set_list_drop(list, n - 2, 2);
1457 list = isl_basic_set_list_add(list, bset1);
1459 t = isl_basic_set_list_get_basic_set(list, n - 2);
1460 t = isl_basic_set_lineality_space(t);
1461 if (!t)
1462 goto error;
1463 if (isl_basic_set_plain_is_universe(t)) {
1464 isl_basic_set_list_free(list);
1465 return t;
1467 total = isl_basic_set_dim(t, isl_dim_all);
1468 if (t->n_eq < total) {
1469 set = isl_basic_set_list_union(list);
1470 return modulo_lineality(set, t);
1472 isl_basic_set_free(t);
1473 if (total < 0)
1474 goto error;
1477 return NULL;
1478 error:
1479 isl_basic_set_list_free(list);
1480 return NULL;
1483 /* Compute an initial hull for wrapping containing a single initial
1484 * facet.
1485 * This function assumes that the given set is bounded.
1487 static __isl_give isl_basic_set *initial_hull(__isl_take isl_basic_set *hull,
1488 __isl_keep isl_set *set)
1490 struct isl_mat *bounds = NULL;
1491 isl_size dim;
1492 int k;
1494 if (!hull)
1495 goto error;
1496 bounds = initial_facet_constraint(set);
1497 if (!bounds)
1498 goto error;
1499 k = isl_basic_set_alloc_inequality(hull);
1500 if (k < 0)
1501 goto error;
1502 dim = isl_set_dim(set, isl_dim_set);
1503 if (dim < 0)
1504 goto error;
1505 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1506 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1507 isl_mat_free(bounds);
1509 return hull;
1510 error:
1511 isl_basic_set_free(hull);
1512 isl_mat_free(bounds);
1513 return NULL;
1516 struct max_constraint {
1517 struct isl_mat *c;
1518 int count;
1519 int ineq;
1522 static isl_bool max_constraint_equal(const void *entry, const void *val)
1524 struct max_constraint *a = (struct max_constraint *)entry;
1525 isl_int *b = (isl_int *)val;
1527 return isl_bool_ok(isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1));
1530 static isl_stat update_constraint(struct isl_ctx *ctx,
1531 struct isl_hash_table *table,
1532 isl_int *con, unsigned len, int n, int ineq)
1534 struct isl_hash_table_entry *entry;
1535 struct max_constraint *c;
1536 uint32_t c_hash;
1538 c_hash = isl_seq_get_hash(con + 1, len);
1539 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1540 con + 1, 0);
1541 if (!entry)
1542 return isl_stat_error;
1543 if (entry == isl_hash_table_entry_none)
1544 return isl_stat_ok;
1545 c = entry->data;
1546 if (c->count < n) {
1547 isl_hash_table_remove(ctx, table, entry);
1548 return isl_stat_ok;
1550 c->count++;
1551 if (isl_int_gt(c->c->row[0][0], con[0]))
1552 return isl_stat_ok;
1553 if (isl_int_eq(c->c->row[0][0], con[0])) {
1554 if (ineq)
1555 c->ineq = ineq;
1556 return isl_stat_ok;
1558 c->c = isl_mat_cow(c->c);
1559 isl_int_set(c->c->row[0][0], con[0]);
1560 c->ineq = ineq;
1562 return isl_stat_ok;
1565 /* Check whether the constraint hash table "table" contains the constraint
1566 * "con".
1568 static isl_bool has_constraint(struct isl_ctx *ctx,
1569 struct isl_hash_table *table, isl_int *con, unsigned len, int n)
1571 struct isl_hash_table_entry *entry;
1572 struct max_constraint *c;
1573 uint32_t c_hash;
1575 c_hash = isl_seq_get_hash(con + 1, len);
1576 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1577 con + 1, 0);
1578 if (!entry)
1579 return isl_bool_error;
1580 if (entry == isl_hash_table_entry_none)
1581 return isl_bool_false;
1582 c = entry->data;
1583 if (c->count < n)
1584 return isl_bool_false;
1585 return isl_bool_ok(isl_int_eq(c->c->row[0][0], con[0]));
1588 /* Are the constraints of "bset" known to be facets?
1589 * If there are any equality constraints, then they are not.
1590 * If there may be redundant constraints, then those
1591 * redundant constraints are not facets.
1593 static isl_bool has_facets(__isl_keep isl_basic_set *bset)
1595 int n_eq;
1597 n_eq = isl_basic_set_n_equality(bset);
1598 if (n_eq < 0)
1599 return isl_bool_error;
1600 if (n_eq != 0)
1601 return isl_bool_false;
1602 return ISL_F_ISSET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1605 /* Check for inequality constraints of a basic set without equalities
1606 * or redundant constraints
1607 * such that the same or more stringent copies of the constraint appear
1608 * in all of the basic sets. Such constraints are necessarily facet
1609 * constraints of the convex hull.
1611 * If the resulting basic set is by chance identical to one of
1612 * the basic sets in "set", then we know that this basic set contains
1613 * all other basic sets and is therefore the convex hull of set.
1614 * In this case we set *is_hull to 1.
1616 static __isl_give isl_basic_set *common_constraints(
1617 __isl_take isl_basic_set *hull, __isl_keep isl_set *set, int *is_hull)
1619 int i, j, s, n;
1620 int min_constraints;
1621 int best;
1622 struct max_constraint *constraints = NULL;
1623 struct isl_hash_table *table = NULL;
1624 isl_size total;
1626 *is_hull = 0;
1628 for (i = 0; i < set->n; ++i) {
1629 isl_bool facets = has_facets(set->p[i]);
1630 if (facets < 0)
1631 return isl_basic_set_free(hull);
1632 if (facets)
1633 break;
1635 if (i >= set->n)
1636 return hull;
1637 min_constraints = set->p[i]->n_ineq;
1638 best = i;
1639 for (i = best + 1; i < set->n; ++i) {
1640 isl_bool facets = has_facets(set->p[i]);
1641 if (facets < 0)
1642 return isl_basic_set_free(hull);
1643 if (!facets)
1644 continue;
1645 if (set->p[i]->n_ineq >= min_constraints)
1646 continue;
1647 min_constraints = set->p[i]->n_ineq;
1648 best = i;
1650 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1651 min_constraints);
1652 if (!constraints)
1653 return hull;
1654 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1655 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1656 goto error;
1658 total = isl_set_dim(set, isl_dim_all);
1659 if (total < 0)
1660 goto error;
1661 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1662 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1663 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1664 if (!constraints[i].c)
1665 goto error;
1666 constraints[i].ineq = 1;
1668 for (i = 0; i < min_constraints; ++i) {
1669 struct isl_hash_table_entry *entry;
1670 uint32_t c_hash;
1671 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1672 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1673 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1674 if (!entry)
1675 goto error;
1676 isl_assert(hull->ctx, !entry->data, goto error);
1677 entry->data = &constraints[i];
1680 n = 0;
1681 for (s = 0; s < set->n; ++s) {
1682 if (s == best)
1683 continue;
1685 for (i = 0; i < set->p[s]->n_eq; ++i) {
1686 isl_int *eq = set->p[s]->eq[i];
1687 for (j = 0; j < 2; ++j) {
1688 isl_seq_neg(eq, eq, 1 + total);
1689 if (update_constraint(hull->ctx, table,
1690 eq, total, n, 0) < 0)
1691 goto error;
1694 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1695 isl_int *ineq = set->p[s]->ineq[i];
1696 if (update_constraint(hull->ctx, table, ineq, total, n,
1697 set->p[s]->n_eq == 0) < 0)
1698 goto error;
1700 ++n;
1703 for (i = 0; i < min_constraints; ++i) {
1704 if (constraints[i].count < n)
1705 continue;
1706 if (!constraints[i].ineq)
1707 continue;
1708 j = isl_basic_set_alloc_inequality(hull);
1709 if (j < 0)
1710 goto error;
1711 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1714 for (s = 0; s < set->n; ++s) {
1715 if (set->p[s]->n_eq)
1716 continue;
1717 if (set->p[s]->n_ineq != hull->n_ineq)
1718 continue;
1719 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1720 isl_bool has;
1721 isl_int *ineq = set->p[s]->ineq[i];
1722 has = has_constraint(hull->ctx, table, ineq, total, n);
1723 if (has < 0)
1724 goto error;
1725 if (!has)
1726 break;
1728 if (i == set->p[s]->n_ineq)
1729 *is_hull = 1;
1732 isl_hash_table_clear(table);
1733 for (i = 0; i < min_constraints; ++i)
1734 isl_mat_free(constraints[i].c);
1735 free(constraints);
1736 free(table);
1737 return hull;
1738 error:
1739 isl_hash_table_clear(table);
1740 free(table);
1741 if (constraints)
1742 for (i = 0; i < min_constraints; ++i)
1743 isl_mat_free(constraints[i].c);
1744 free(constraints);
1745 return hull;
1748 /* Create a template for the convex hull of "set" and fill it up
1749 * obvious facet constraints, if any. If the result happens to
1750 * be the convex hull of "set" then *is_hull is set to 1.
1752 static __isl_give isl_basic_set *proto_hull(__isl_keep isl_set *set,
1753 int *is_hull)
1755 struct isl_basic_set *hull;
1756 unsigned n_ineq;
1757 int i;
1759 n_ineq = 1;
1760 for (i = 0; i < set->n; ++i) {
1761 n_ineq += set->p[i]->n_eq;
1762 n_ineq += set->p[i]->n_ineq;
1764 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1765 hull = isl_basic_set_set_rational(hull);
1766 if (!hull)
1767 return NULL;
1768 return common_constraints(hull, set, is_hull);
1771 static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set)
1773 struct isl_basic_set *hull;
1774 int is_hull;
1776 hull = proto_hull(set, &is_hull);
1777 if (hull && !is_hull) {
1778 if (hull->n_ineq == 0)
1779 hull = initial_hull(hull, set);
1780 hull = extend(hull, set);
1782 isl_set_free(set);
1784 return hull;
1787 /* Compute the convex hull of a set without any parameters or
1788 * integer divisions. Depending on whether the set is bounded,
1789 * we pass control to the wrapping based convex hull or
1790 * the Fourier-Motzkin elimination based convex hull.
1791 * We also handle a few special cases before checking the boundedness.
1793 static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set)
1795 isl_bool bounded;
1796 isl_size dim;
1797 struct isl_basic_set *convex_hull = NULL;
1798 struct isl_basic_set *lin;
1800 dim = isl_set_dim(set, isl_dim_all);
1801 if (dim < 0)
1802 goto error;
1803 if (dim == 0)
1804 return convex_hull_0d(set);
1806 set = isl_set_coalesce(set);
1807 set = isl_set_set_rational(set);
1809 if (!set)
1810 return NULL;
1811 if (set->n == 1) {
1812 convex_hull = isl_basic_set_copy(set->p[0]);
1813 isl_set_free(set);
1814 return convex_hull;
1816 if (dim == 1)
1817 return convex_hull_1d(set);
1819 bounded = isl_set_is_bounded(set);
1820 if (bounded < 0)
1821 goto error;
1822 if (bounded && set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1823 return uset_convex_hull_wrap(set);
1825 lin = isl_set_combined_lineality_space(isl_set_copy(set));
1826 if (!lin)
1827 goto error;
1828 if (isl_basic_set_plain_is_universe(lin)) {
1829 isl_set_free(set);
1830 return lin;
1832 if (lin->n_eq < dim)
1833 return modulo_lineality(set, lin);
1834 isl_basic_set_free(lin);
1836 return uset_convex_hull_unbounded(set);
1837 error:
1838 isl_set_free(set);
1839 isl_basic_set_free(convex_hull);
1840 return NULL;
1843 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1844 * without parameters or divs and where the convex hull of set is
1845 * known to be full-dimensional.
1847 static __isl_give isl_basic_set *uset_convex_hull_wrap_bounded(
1848 __isl_take isl_set *set)
1850 struct isl_basic_set *convex_hull = NULL;
1851 isl_size dim;
1853 dim = isl_set_dim(set, isl_dim_all);
1854 if (dim < 0)
1855 goto error;
1857 if (dim == 0) {
1858 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1859 isl_set_free(set);
1860 convex_hull = isl_basic_set_set_rational(convex_hull);
1861 return convex_hull;
1864 set = isl_set_set_rational(set);
1865 set = isl_set_coalesce(set);
1866 if (!set)
1867 goto error;
1868 if (set->n == 1) {
1869 convex_hull = isl_basic_set_copy(set->p[0]);
1870 isl_set_free(set);
1871 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1872 return convex_hull;
1874 if (dim == 1)
1875 return convex_hull_1d(set);
1877 return uset_convex_hull_wrap(set);
1878 error:
1879 isl_set_free(set);
1880 return NULL;
1883 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1884 * We first remove the equalities (transforming the set), compute the
1885 * convex hull of the transformed set and then add the equalities back
1886 * (after performing the inverse transformation.
1888 static __isl_give isl_basic_set *modulo_affine_hull(
1889 __isl_take isl_set *set, __isl_take isl_basic_set *affine_hull)
1891 struct isl_mat *T;
1892 struct isl_mat *T2;
1893 struct isl_basic_set *dummy;
1894 struct isl_basic_set *convex_hull;
1896 dummy = isl_basic_set_remove_equalities(
1897 isl_basic_set_copy(affine_hull), &T, &T2);
1898 if (!dummy)
1899 goto error;
1900 isl_basic_set_free(dummy);
1901 set = isl_set_preimage(set, T);
1902 convex_hull = uset_convex_hull(set);
1903 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1904 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1905 return convex_hull;
1906 error:
1907 isl_mat_free(T);
1908 isl_mat_free(T2);
1909 isl_basic_set_free(affine_hull);
1910 isl_set_free(set);
1911 return NULL;
1914 /* Return an empty basic map living in the same space as "map".
1916 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1917 __isl_take isl_map *map)
1919 isl_space *space;
1921 space = isl_map_get_space(map);
1922 isl_map_free(map);
1923 return isl_basic_map_empty(space);
1926 /* Compute the convex hull of a map.
1928 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1929 * specifically, the wrapping of facets to obtain new facets.
1931 __isl_give isl_basic_map *isl_map_convex_hull(__isl_take isl_map *map)
1933 struct isl_basic_set *bset;
1934 struct isl_basic_map *model = NULL;
1935 struct isl_basic_set *affine_hull = NULL;
1936 struct isl_basic_map *convex_hull = NULL;
1937 struct isl_set *set = NULL;
1939 map = isl_map_detect_equalities(map);
1940 map = isl_map_align_divs_internal(map);
1941 if (!map)
1942 goto error;
1944 if (map->n == 0)
1945 return replace_map_by_empty_basic_map(map);
1947 model = isl_basic_map_copy(map->p[0]);
1948 set = isl_map_underlying_set(map);
1949 if (!set)
1950 goto error;
1952 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1953 if (!affine_hull)
1954 goto error;
1955 if (affine_hull->n_eq != 0)
1956 bset = modulo_affine_hull(set, affine_hull);
1957 else {
1958 isl_basic_set_free(affine_hull);
1959 bset = uset_convex_hull(set);
1962 convex_hull = isl_basic_map_overlying_set(bset, model);
1963 if (!convex_hull)
1964 return NULL;
1966 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1967 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1968 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1969 return convex_hull;
1970 error:
1971 isl_set_free(set);
1972 isl_basic_map_free(model);
1973 return NULL;
1976 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1978 return bset_from_bmap(isl_map_convex_hull(set_to_map(set)));
1981 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1983 isl_basic_map *hull;
1985 hull = isl_map_convex_hull(map);
1986 return isl_basic_map_remove_divs(hull);
1989 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1991 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set)));
1994 struct sh_data_entry {
1995 struct isl_hash_table *table;
1996 struct isl_tab *tab;
1999 /* Holds the data needed during the simple hull computation.
2000 * In particular,
2001 * n the number of basic sets in the original set
2002 * hull_table a hash table of already computed constraints
2003 * in the simple hull
2004 * p for each basic set,
2005 * table a hash table of the constraints
2006 * tab the tableau corresponding to the basic set
2008 struct sh_data {
2009 struct isl_ctx *ctx;
2010 unsigned n;
2011 struct isl_hash_table *hull_table;
2012 struct sh_data_entry p[1];
2015 static void sh_data_free(struct sh_data *data)
2017 int i;
2019 if (!data)
2020 return;
2021 isl_hash_table_free(data->ctx, data->hull_table);
2022 for (i = 0; i < data->n; ++i) {
2023 isl_hash_table_free(data->ctx, data->p[i].table);
2024 isl_tab_free(data->p[i].tab);
2026 free(data);
2029 struct ineq_cmp_data {
2030 unsigned len;
2031 isl_int *p;
2034 static isl_bool has_ineq(const void *entry, const void *val)
2036 isl_int *row = (isl_int *)entry;
2037 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2039 return isl_bool_ok(isl_seq_eq(row + 1, v->p + 1, v->len) ||
2040 isl_seq_is_neg(row + 1, v->p + 1, v->len));
2043 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2044 isl_int *ineq, unsigned len)
2046 uint32_t c_hash;
2047 struct ineq_cmp_data v;
2048 struct isl_hash_table_entry *entry;
2050 v.len = len;
2051 v.p = ineq;
2052 c_hash = isl_seq_get_hash(ineq + 1, len);
2053 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2054 if (!entry)
2055 return - 1;
2056 entry->data = ineq;
2057 return 0;
2060 /* Fill hash table "table" with the constraints of "bset".
2061 * Equalities are added as two inequalities.
2062 * The value in the hash table is a pointer to the (in)equality of "bset".
2064 static isl_stat hash_basic_set(struct isl_hash_table *table,
2065 __isl_keep isl_basic_set *bset)
2067 int i, j;
2068 isl_size dim = isl_basic_set_dim(bset, isl_dim_all);
2070 if (dim < 0)
2071 return isl_stat_error;
2072 for (i = 0; i < bset->n_eq; ++i) {
2073 for (j = 0; j < 2; ++j) {
2074 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2075 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2076 return isl_stat_error;
2079 for (i = 0; i < bset->n_ineq; ++i) {
2080 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2081 return isl_stat_error;
2083 return isl_stat_ok;
2086 static struct sh_data *sh_data_alloc(__isl_keep isl_set *set, unsigned n_ineq)
2088 struct sh_data *data;
2089 int i;
2091 data = isl_calloc(set->ctx, struct sh_data,
2092 sizeof(struct sh_data) +
2093 (set->n - 1) * sizeof(struct sh_data_entry));
2094 if (!data)
2095 return NULL;
2096 data->ctx = set->ctx;
2097 data->n = set->n;
2098 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2099 if (!data->hull_table)
2100 goto error;
2101 for (i = 0; i < set->n; ++i) {
2102 data->p[i].table = isl_hash_table_alloc(set->ctx,
2103 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2104 if (!data->p[i].table)
2105 goto error;
2106 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2107 goto error;
2109 return data;
2110 error:
2111 sh_data_free(data);
2112 return NULL;
2115 /* Check if inequality "ineq" is a bound for basic set "j" or if
2116 * it can be relaxed (by increasing the constant term) to become
2117 * a bound for that basic set. In the latter case, the constant
2118 * term is updated.
2119 * Relaxation of the constant term is only allowed if "shift" is set.
2121 * Return 1 if "ineq" is a bound
2122 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2123 * -1 if some error occurred
2125 static int is_bound(struct sh_data *data, __isl_keep isl_set *set, int j,
2126 isl_int *ineq, int shift)
2128 enum isl_lp_result res;
2129 isl_int opt;
2131 if (!data->p[j].tab) {
2132 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2133 if (!data->p[j].tab)
2134 return -1;
2137 isl_int_init(opt);
2139 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2140 &opt, NULL, 0);
2141 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2142 if (shift)
2143 isl_int_sub(ineq[0], ineq[0], opt);
2144 else
2145 res = isl_lp_unbounded;
2148 isl_int_clear(opt);
2150 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2151 res == isl_lp_unbounded ? 0 : -1;
2154 /* Set the constant term of "ineq" to the maximum of those of the constraints
2155 * in the basic sets of "set" following "i" that are parallel to "ineq".
2156 * That is, if any of the basic sets of "set" following "i" have a more
2157 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2158 * "c_hash" is the hash value of the linear part of "ineq".
2159 * "v" has been set up for use by has_ineq.
2161 * Note that the two inequality constraints corresponding to an equality are
2162 * represented by the same inequality constraint in data->p[j].table
2163 * (but with different hash values). This means the constraint (or at
2164 * least its constant term) may need to be temporarily negated to get
2165 * the actually hashed constraint.
2167 static isl_stat set_max_constant_term(struct sh_data *data,
2168 __isl_keep isl_set *set,
2169 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2171 int j;
2172 isl_ctx *ctx;
2173 struct isl_hash_table_entry *entry;
2175 ctx = isl_set_get_ctx(set);
2176 for (j = i + 1; j < set->n; ++j) {
2177 int neg;
2178 isl_int *ineq_j;
2180 entry = isl_hash_table_find(ctx, data->p[j].table,
2181 c_hash, &has_ineq, v, 0);
2182 if (!entry)
2183 return isl_stat_error;
2184 if (entry == isl_hash_table_entry_none)
2185 continue;
2187 ineq_j = entry->data;
2188 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2189 if (neg)
2190 isl_int_neg(ineq_j[0], ineq_j[0]);
2191 if (isl_int_gt(ineq_j[0], ineq[0]))
2192 isl_int_set(ineq[0], ineq_j[0]);
2193 if (neg)
2194 isl_int_neg(ineq_j[0], ineq_j[0]);
2197 return isl_stat_ok;
2200 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2201 * become a bound on the whole set. If so, add the (relaxed) inequality
2202 * to "hull". Relaxation is only allowed if "shift" is set.
2204 * We first check if "hull" already contains a translate of the inequality.
2205 * If so, we are done.
2206 * Then, we check if any of the previous basic sets contains a translate
2207 * of the inequality. If so, then we have already considered this
2208 * inequality and we are done.
2209 * Otherwise, for each basic set other than "i", we check if the inequality
2210 * is a bound on the basic set, but first replace the constant term
2211 * by the maximal value of any translate of the inequality in any
2212 * of the following basic sets.
2213 * For previous basic sets, we know that they do not contain a translate
2214 * of the inequality, so we directly call is_bound.
2215 * For following basic sets, we first check if a translate of the
2216 * inequality appears in its description. If so, the constant term
2217 * of the inequality has already been updated with respect to this
2218 * translate and the inequality is therefore known to be a bound
2219 * of this basic set.
2221 static __isl_give isl_basic_set *add_bound(__isl_take isl_basic_set *hull,
2222 struct sh_data *data, __isl_keep isl_set *set, int i, isl_int *ineq,
2223 int shift)
2225 uint32_t c_hash;
2226 struct ineq_cmp_data v;
2227 struct isl_hash_table_entry *entry;
2228 int j, k;
2229 isl_size total;
2231 total = isl_basic_set_dim(hull, isl_dim_all);
2232 if (total < 0)
2233 return isl_basic_set_free(hull);
2235 v.len = total;
2236 v.p = ineq;
2237 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2239 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2240 has_ineq, &v, 0);
2241 if (!entry)
2242 return isl_basic_set_free(hull);
2243 if (entry != isl_hash_table_entry_none)
2244 return hull;
2246 for (j = 0; j < i; ++j) {
2247 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2248 c_hash, has_ineq, &v, 0);
2249 if (!entry)
2250 return isl_basic_set_free(hull);
2251 if (entry != isl_hash_table_entry_none)
2252 break;
2254 if (j < i)
2255 return hull;
2257 k = isl_basic_set_alloc_inequality(hull);
2258 if (k < 0)
2259 goto error;
2260 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2262 if (set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v) < 0)
2263 goto error;
2264 for (j = 0; j < i; ++j) {
2265 int bound;
2266 bound = is_bound(data, set, j, hull->ineq[k], shift);
2267 if (bound < 0)
2268 goto error;
2269 if (!bound)
2270 break;
2272 if (j < i)
2273 return isl_basic_set_free_inequality(hull, 1);
2275 for (j = i + 1; j < set->n; ++j) {
2276 int bound;
2277 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2278 c_hash, has_ineq, &v, 0);
2279 if (!entry)
2280 return isl_basic_set_free(hull);
2281 if (entry != isl_hash_table_entry_none)
2282 continue;
2283 bound = is_bound(data, set, j, hull->ineq[k], shift);
2284 if (bound < 0)
2285 goto error;
2286 if (!bound)
2287 break;
2289 if (j < set->n)
2290 return isl_basic_set_free_inequality(hull, 1);
2292 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2293 has_ineq, &v, 1);
2294 if (!entry)
2295 goto error;
2296 entry->data = hull->ineq[k];
2298 return hull;
2299 error:
2300 isl_basic_set_free(hull);
2301 return NULL;
2304 /* Check if any inequality from basic set "i" is or can be relaxed to
2305 * become a bound on the whole set. If so, add the (relaxed) inequality
2306 * to "hull". Relaxation is only allowed if "shift" is set.
2308 static __isl_give isl_basic_set *add_bounds(__isl_take isl_basic_set *bset,
2309 struct sh_data *data, __isl_keep isl_set *set, int i, int shift)
2311 int j, k;
2312 isl_size dim = isl_basic_set_dim(bset, isl_dim_all);
2314 if (dim < 0)
2315 return isl_basic_set_free(bset);
2317 for (j = 0; j < set->p[i]->n_eq; ++j) {
2318 for (k = 0; k < 2; ++k) {
2319 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2320 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2321 shift);
2324 for (j = 0; j < set->p[i]->n_ineq; ++j)
2325 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2326 return bset;
2329 /* Compute a superset of the convex hull of set that is described
2330 * by only (translates of) the constraints in the constituents of set.
2331 * Translation is only allowed if "shift" is set.
2333 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2334 int shift)
2336 struct sh_data *data = NULL;
2337 struct isl_basic_set *hull = NULL;
2338 unsigned n_ineq;
2339 int i;
2341 if (!set)
2342 return NULL;
2344 n_ineq = 0;
2345 for (i = 0; i < set->n; ++i) {
2346 if (!set->p[i])
2347 goto error;
2348 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2351 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2352 if (!hull)
2353 goto error;
2355 data = sh_data_alloc(set, n_ineq);
2356 if (!data)
2357 goto error;
2359 for (i = 0; i < set->n; ++i)
2360 hull = add_bounds(hull, data, set, i, shift);
2362 sh_data_free(data);
2363 isl_set_free(set);
2365 return hull;
2366 error:
2367 sh_data_free(data);
2368 isl_basic_set_free(hull);
2369 isl_set_free(set);
2370 return NULL;
2373 /* Compute a superset of the convex hull of map that is described
2374 * by only (translates of) the constraints in the constituents of map.
2375 * Handle trivial cases where map is NULL or contains at most one disjunct.
2377 static __isl_give isl_basic_map *map_simple_hull_trivial(
2378 __isl_take isl_map *map)
2380 isl_basic_map *hull;
2382 if (!map)
2383 return NULL;
2384 if (map->n == 0)
2385 return replace_map_by_empty_basic_map(map);
2387 hull = isl_basic_map_copy(map->p[0]);
2388 isl_map_free(map);
2389 return hull;
2392 /* Return a copy of the simple hull cached inside "map".
2393 * "shift" determines whether to return the cached unshifted or shifted
2394 * simple hull.
2396 static __isl_give isl_basic_map *cached_simple_hull(__isl_take isl_map *map,
2397 int shift)
2399 isl_basic_map *hull;
2401 hull = isl_basic_map_copy(map->cached_simple_hull[shift]);
2402 isl_map_free(map);
2404 return hull;
2407 /* Compute a superset of the convex hull of map that is described
2408 * by only (translates of) the constraints in the constituents of map.
2409 * Translation is only allowed if "shift" is set.
2411 * The constraints are sorted while removing redundant constraints
2412 * in order to indicate a preference of which constraints should
2413 * be preserved. In particular, pairs of constraints that are
2414 * sorted together are preferred to either both be preserved
2415 * or both be removed. The sorting is performed inside
2416 * isl_basic_map_remove_redundancies.
2418 * The result of the computation is stored in map->cached_simple_hull[shift]
2419 * such that it can be reused in subsequent calls. The cache is cleared
2420 * whenever the map is modified (in isl_map_cow).
2421 * Note that the results need to be stored in the input map for there
2422 * to be any chance that they may get reused. In particular, they
2423 * are stored in a copy of the input map that is saved before
2424 * the integer division alignment.
2426 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2427 int shift)
2429 struct isl_set *set = NULL;
2430 struct isl_basic_map *model = NULL;
2431 struct isl_basic_map *hull;
2432 struct isl_basic_map *affine_hull;
2433 struct isl_basic_set *bset = NULL;
2434 isl_map *input;
2436 if (!map || map->n <= 1)
2437 return map_simple_hull_trivial(map);
2439 if (map->cached_simple_hull[shift])
2440 return cached_simple_hull(map, shift);
2442 map = isl_map_detect_equalities(map);
2443 if (!map || map->n <= 1)
2444 return map_simple_hull_trivial(map);
2445 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2446 input = isl_map_copy(map);
2447 map = isl_map_align_divs_internal(map);
2448 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2450 set = isl_map_underlying_set(map);
2452 bset = uset_simple_hull(set, shift);
2454 hull = isl_basic_map_overlying_set(bset, model);
2456 hull = isl_basic_map_intersect(hull, affine_hull);
2457 hull = isl_basic_map_remove_redundancies(hull);
2459 if (hull) {
2460 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2461 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2464 hull = isl_basic_map_finalize(hull);
2465 if (input)
2466 input->cached_simple_hull[shift] = isl_basic_map_copy(hull);
2467 isl_map_free(input);
2469 return hull;
2472 /* Compute a superset of the convex hull of map that is described
2473 * by only translates of the constraints in the constituents of map.
2475 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2477 return map_simple_hull(map, 1);
2480 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2482 return bset_from_bmap(isl_map_simple_hull(set_to_map(set)));
2485 /* Compute a superset of the convex hull of map that is described
2486 * by only the constraints in the constituents of map.
2488 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2489 __isl_take isl_map *map)
2491 return map_simple_hull(map, 0);
2494 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2495 __isl_take isl_set *set)
2497 return isl_map_unshifted_simple_hull(set);
2500 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2501 * A constraint that appears with different constant terms
2502 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2503 * (i.e., greatest) constant term.
2504 * "bmap1" and "bmap2" are assumed to have the same (known)
2505 * integer divisions.
2506 * The constraints of both "bmap1" and "bmap2" are assumed
2507 * to have been sorted using isl_basic_map_sort_constraints.
2509 * Run through the inequality constraints of "bmap1" and "bmap2"
2510 * in sorted order.
2511 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2512 * is removed.
2513 * If a match is found, the constraint is kept. If needed, the constant
2514 * term of the constraint is adjusted.
2516 static __isl_give isl_basic_map *select_shared_inequalities(
2517 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2519 int i1, i2;
2521 bmap1 = isl_basic_map_cow(bmap1);
2522 if (!bmap1 || !bmap2)
2523 return isl_basic_map_free(bmap1);
2525 i1 = bmap1->n_ineq - 1;
2526 i2 = bmap2->n_ineq - 1;
2527 while (bmap1 && i1 >= 0 && i2 >= 0) {
2528 int cmp;
2530 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2531 bmap2->ineq[i2]);
2532 if (cmp < 0) {
2533 --i2;
2534 continue;
2536 if (cmp > 0) {
2537 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2538 bmap1 = isl_basic_map_free(bmap1);
2539 --i1;
2540 continue;
2542 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2543 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2544 --i1;
2545 --i2;
2547 for (; i1 >= 0; --i1)
2548 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2549 bmap1 = isl_basic_map_free(bmap1);
2551 return bmap1;
2554 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2555 * "bmap1" and "bmap2" are assumed to have the same (known)
2556 * integer divisions.
2558 * Run through the equality constraints of "bmap1" and "bmap2".
2559 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2560 * is removed.
2562 static __isl_give isl_basic_map *select_shared_equalities(
2563 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2565 int i1, i2;
2566 isl_size total;
2568 bmap1 = isl_basic_map_cow(bmap1);
2569 total = isl_basic_map_dim(bmap1, isl_dim_all);
2570 if (total < 0 || !bmap2)
2571 return isl_basic_map_free(bmap1);
2573 i1 = bmap1->n_eq - 1;
2574 i2 = bmap2->n_eq - 1;
2575 while (bmap1 && i1 >= 0 && i2 >= 0) {
2576 int last1, last2;
2578 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2579 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2580 if (last1 > last2) {
2581 --i2;
2582 continue;
2584 if (last1 < last2) {
2585 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2586 bmap1 = isl_basic_map_free(bmap1);
2587 --i1;
2588 continue;
2590 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2591 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2592 bmap1 = isl_basic_map_free(bmap1);
2594 --i1;
2595 --i2;
2597 for (; i1 >= 0; --i1)
2598 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2599 bmap1 = isl_basic_map_free(bmap1);
2601 return bmap1;
2604 /* Compute a superset of "bmap1" and "bmap2" that is described
2605 * by only the constraints that appear in both "bmap1" and "bmap2".
2607 * First drop constraints that involve unknown integer divisions
2608 * since it is not trivial to check whether two such integer divisions
2609 * in different basic maps are the same.
2610 * Then align the remaining (known) divs and sort the constraints.
2611 * Finally drop all inequalities and equalities from "bmap1" that
2612 * do not also appear in "bmap2".
2614 __isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2615 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2617 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2618 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2619 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2620 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2621 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2622 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2623 bmap1 = isl_basic_map_sort_constraints(bmap1);
2624 bmap2 = isl_basic_map_sort_constraints(bmap2);
2626 bmap1 = select_shared_inequalities(bmap1, bmap2);
2627 bmap1 = select_shared_equalities(bmap1, bmap2);
2629 isl_basic_map_free(bmap2);
2630 bmap1 = isl_basic_map_finalize(bmap1);
2631 return bmap1;
2634 /* Compute a superset of the convex hull of "map" that is described
2635 * by only the constraints in the constituents of "map".
2636 * In particular, the result is composed of constraints that appear
2637 * in each of the basic maps of "map"
2639 * Constraints that involve unknown integer divisions are dropped
2640 * since it is not trivial to check whether two such integer divisions
2641 * in different basic maps are the same.
2643 * The hull is initialized from the first basic map and then
2644 * updated with respect to the other basic maps in turn.
2646 __isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2647 __isl_take isl_map *map)
2649 int i;
2650 isl_basic_map *hull;
2652 if (!map)
2653 return NULL;
2654 if (map->n <= 1)
2655 return map_simple_hull_trivial(map);
2656 map = isl_map_drop_constraint_involving_unknown_divs(map);
2657 hull = isl_basic_map_copy(map->p[0]);
2658 for (i = 1; i < map->n; ++i) {
2659 isl_basic_map *bmap_i;
2661 bmap_i = isl_basic_map_copy(map->p[i]);
2662 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2665 isl_map_free(map);
2666 return hull;
2669 /* Compute a superset of the convex hull of "set" that is described
2670 * by only the constraints in the constituents of "set".
2671 * In particular, the result is composed of constraints that appear
2672 * in each of the basic sets of "set"
2674 __isl_give isl_basic_set *isl_set_plain_unshifted_simple_hull(
2675 __isl_take isl_set *set)
2677 return isl_map_plain_unshifted_simple_hull(set);
2680 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2682 * For each basic set in "set", we first check if the basic set
2683 * contains a translate of "ineq". If this translate is more relaxed,
2684 * then we assume that "ineq" is not a bound on this basic set.
2685 * Otherwise, we know that it is a bound.
2686 * If the basic set does not contain a translate of "ineq", then
2687 * we call is_bound to perform the test.
2689 static __isl_give isl_basic_set *add_bound_from_constraint(
2690 __isl_take isl_basic_set *hull, struct sh_data *data,
2691 __isl_keep isl_set *set, isl_int *ineq)
2693 int i, k;
2694 isl_ctx *ctx;
2695 uint32_t c_hash;
2696 struct ineq_cmp_data v;
2697 isl_size total;
2699 total = isl_basic_set_dim(hull, isl_dim_all);
2700 if (total < 0 || !set)
2701 return isl_basic_set_free(hull);
2703 v.len = total;
2704 v.p = ineq;
2705 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2707 ctx = isl_basic_set_get_ctx(hull);
2708 for (i = 0; i < set->n; ++i) {
2709 int bound;
2710 struct isl_hash_table_entry *entry;
2712 entry = isl_hash_table_find(ctx, data->p[i].table,
2713 c_hash, &has_ineq, &v, 0);
2714 if (!entry)
2715 return isl_basic_set_free(hull);
2716 if (entry != isl_hash_table_entry_none) {
2717 isl_int *ineq_i = entry->data;
2718 int neg, more_relaxed;
2720 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2721 if (neg)
2722 isl_int_neg(ineq_i[0], ineq_i[0]);
2723 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2724 if (neg)
2725 isl_int_neg(ineq_i[0], ineq_i[0]);
2726 if (more_relaxed)
2727 break;
2728 else
2729 continue;
2731 bound = is_bound(data, set, i, ineq, 0);
2732 if (bound < 0)
2733 return isl_basic_set_free(hull);
2734 if (!bound)
2735 break;
2737 if (i < set->n)
2738 return hull;
2740 k = isl_basic_set_alloc_inequality(hull);
2741 if (k < 0)
2742 return isl_basic_set_free(hull);
2743 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2745 return hull;
2748 /* Compute a superset of the convex hull of "set" that is described
2749 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2750 * has no parameters or integer divisions.
2752 * The inequalities in "ineq" are assumed to have been sorted such
2753 * that constraints with the same linear part appear together and
2754 * that among constraints with the same linear part, those with
2755 * smaller constant term appear first.
2757 * We reuse the same data structure that is used by uset_simple_hull,
2758 * but we do not need the hull table since we will not consider the
2759 * same constraint more than once. We therefore allocate it with zero size.
2761 * We run through the constraints and try to add them one by one,
2762 * skipping identical constraints. If we have added a constraint and
2763 * the next constraint is a more relaxed translate, then we skip this
2764 * next constraint as well.
2766 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2767 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2769 int i;
2770 int last_added = 0;
2771 struct sh_data *data = NULL;
2772 isl_basic_set *hull = NULL;
2773 isl_size dim;
2775 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2776 if (!hull)
2777 goto error;
2779 data = sh_data_alloc(set, 0);
2780 if (!data)
2781 goto error;
2783 dim = isl_set_dim(set, isl_dim_set);
2784 if (dim < 0)
2785 goto error;
2786 for (i = 0; i < n_ineq; ++i) {
2787 int hull_n_ineq = hull->n_ineq;
2788 int parallel;
2790 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2791 dim);
2792 if (parallel &&
2793 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2794 continue;
2795 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2796 if (!hull)
2797 goto error;
2798 last_added = hull->n_ineq > hull_n_ineq;
2801 sh_data_free(data);
2802 isl_set_free(set);
2803 return hull;
2804 error:
2805 sh_data_free(data);
2806 isl_set_free(set);
2807 isl_basic_set_free(hull);
2808 return NULL;
2811 /* Collect pointers to all the inequalities in the elements of "list"
2812 * in "ineq". For equalities, store both a pointer to the equality and
2813 * a pointer to its opposite, which is first copied to "mat".
2814 * "ineq" and "mat" are assumed to have been preallocated to the right size
2815 * (the number of inequalities + 2 times the number of equalites and
2816 * the number of equalities, respectively).
2818 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2819 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2821 int i, j, n_eq, n_ineq;
2822 isl_size n;
2824 n = isl_basic_set_list_n_basic_set(list);
2825 if (!mat || n < 0)
2826 return isl_mat_free(mat);
2828 n_eq = 0;
2829 n_ineq = 0;
2830 for (i = 0; i < n; ++i) {
2831 isl_basic_set *bset;
2832 bset = isl_basic_set_list_get_basic_set(list, i);
2833 if (!bset)
2834 return isl_mat_free(mat);
2835 for (j = 0; j < bset->n_eq; ++j) {
2836 ineq[n_ineq++] = mat->row[n_eq];
2837 ineq[n_ineq++] = bset->eq[j];
2838 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2840 for (j = 0; j < bset->n_ineq; ++j)
2841 ineq[n_ineq++] = bset->ineq[j];
2842 isl_basic_set_free(bset);
2845 return mat;
2848 /* Comparison routine for use as an isl_sort callback.
2850 * Constraints with the same linear part are sorted together and
2851 * among constraints with the same linear part, those with smaller
2852 * constant term are sorted first.
2854 static int cmp_ineq(const void *a, const void *b, void *arg)
2856 unsigned dim = *(unsigned *) arg;
2857 isl_int * const *ineq1 = a;
2858 isl_int * const *ineq2 = b;
2859 int cmp;
2861 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2862 if (cmp != 0)
2863 return cmp;
2864 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2867 /* Compute a superset of the convex hull of "set" that is described
2868 * by only constraints in the elements of "list", where "set" has
2869 * no parameters or integer divisions.
2871 * We collect all the constraints in those elements and then
2872 * sort the constraints such that constraints with the same linear part
2873 * are sorted together and that those with smaller constant term are
2874 * sorted first.
2876 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2877 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2879 int i, n_eq, n_ineq;
2880 isl_size n;
2881 isl_size dim;
2882 isl_ctx *ctx;
2883 isl_mat *mat = NULL;
2884 isl_int **ineq = NULL;
2885 isl_basic_set *hull;
2887 n = isl_basic_set_list_n_basic_set(list);
2888 if (!set || n < 0)
2889 goto error;
2890 ctx = isl_set_get_ctx(set);
2892 n_eq = 0;
2893 n_ineq = 0;
2894 for (i = 0; i < n; ++i) {
2895 isl_basic_set *bset;
2896 bset = isl_basic_set_list_get_basic_set(list, i);
2897 if (!bset)
2898 goto error;
2899 n_eq += bset->n_eq;
2900 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2901 isl_basic_set_free(bset);
2904 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2905 if (n_ineq > 0 && !ineq)
2906 goto error;
2908 dim = isl_set_dim(set, isl_dim_set);
2909 if (dim < 0)
2910 goto error;
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_size n;
2948 isl_basic_map *model;
2949 isl_basic_map *hull;
2950 isl_set *set;
2951 isl_basic_set_list *bset_list;
2953 n = isl_basic_map_list_n_basic_map(list);
2954 if (!map || n < 0)
2955 goto error;
2957 if (n == 0) {
2958 isl_space *space;
2960 space = isl_map_get_space(map);
2961 isl_map_free(map);
2962 isl_basic_map_list_free(list);
2963 return isl_basic_map_universe(space);
2965 if (isl_map_plain_is_empty(map)) {
2966 isl_map_free(map);
2967 return isl_basic_map_list_intersect(list);
2970 map = isl_map_align_divs_to_basic_map_list(map, list);
2971 if (!map)
2972 goto error;
2973 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2975 model = isl_basic_map_list_get_basic_map(list, 0);
2977 set = isl_map_underlying_set(map);
2978 bset_list = isl_basic_map_list_underlying_set(list);
2980 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2981 hull = isl_basic_map_overlying_set(hull, model);
2983 return hull;
2984 error:
2985 isl_map_free(map);
2986 isl_basic_map_list_free(list);
2987 return NULL;
2990 /* Return a sequence of the basic maps that make up the maps in "list".
2992 static __isl_give isl_basic_map_list *collect_basic_maps(
2993 __isl_take isl_map_list *list)
2995 int i;
2996 isl_size n;
2997 isl_ctx *ctx;
2998 isl_basic_map_list *bmap_list;
3000 if (!list)
3001 return NULL;
3002 n = isl_map_list_n_map(list);
3003 ctx = isl_map_list_get_ctx(list);
3004 bmap_list = isl_basic_map_list_alloc(ctx, 0);
3005 if (n < 0)
3006 bmap_list = isl_basic_map_list_free(bmap_list);
3008 for (i = 0; i < n; ++i) {
3009 isl_map *map;
3010 isl_basic_map_list *list_i;
3012 map = isl_map_list_get_map(list, i);
3013 map = isl_map_compute_divs(map);
3014 list_i = isl_map_get_basic_map_list(map);
3015 isl_map_free(map);
3016 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
3019 isl_map_list_free(list);
3020 return bmap_list;
3023 /* Compute a superset of the convex hull of "map" that is described
3024 * by only constraints in the elements of "list".
3026 * If "map" is the universe, then the convex hull (and therefore
3027 * any superset of the convexhull) is the universe as well.
3029 * Otherwise, we collect all the basic maps in the map list and
3030 * continue with map_unshifted_simple_hull_from_basic_map_list.
3032 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
3033 __isl_take isl_map *map, __isl_take isl_map_list *list)
3035 isl_basic_map_list *bmap_list;
3036 int is_universe;
3038 is_universe = isl_map_plain_is_universe(map);
3039 if (is_universe < 0)
3040 map = isl_map_free(map);
3041 if (is_universe < 0 || is_universe) {
3042 isl_map_list_free(list);
3043 return isl_map_unshifted_simple_hull(map);
3046 bmap_list = collect_basic_maps(list);
3047 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
3050 /* Compute a superset of the convex hull of "set" that is described
3051 * by only constraints in the elements of "list".
3053 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
3054 __isl_take isl_set *set, __isl_take isl_set_list *list)
3056 return isl_map_unshifted_simple_hull_from_map_list(set, list);
3059 /* Given a set "set", return parametric bounds on the dimension "dim".
3061 static __isl_give isl_basic_set *set_bounds(__isl_keep isl_set *set, int dim)
3063 isl_size set_dim = isl_set_dim(set, isl_dim_set);
3064 if (set_dim < 0)
3065 return NULL;
3066 set = isl_set_copy(set);
3067 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
3068 set = isl_set_eliminate_dims(set, 0, dim);
3069 return isl_set_convex_hull(set);
3072 /* Computes a "simple hull" and then check if each dimension in the
3073 * resulting hull is bounded by a symbolic constant. If not, the
3074 * hull is intersected with the corresponding bounds on the whole set.
3076 __isl_give isl_basic_set *isl_set_bounded_simple_hull(__isl_take isl_set *set)
3078 int i, j;
3079 struct isl_basic_set *hull;
3080 isl_size nparam, dim, total;
3081 unsigned left;
3082 int removed_divs = 0;
3084 hull = isl_set_simple_hull(isl_set_copy(set));
3085 nparam = isl_basic_set_dim(hull, isl_dim_param);
3086 dim = isl_basic_set_dim(hull, isl_dim_set);
3087 total = isl_basic_set_dim(hull, isl_dim_all);
3088 if (nparam < 0 || dim < 0 || total < 0)
3089 goto error;
3091 for (i = 0; i < dim; ++i) {
3092 int lower = 0, upper = 0;
3093 struct isl_basic_set *bounds;
3095 left = total - nparam - i - 1;
3096 for (j = 0; j < hull->n_eq; ++j) {
3097 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3098 continue;
3099 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3100 left) == -1)
3101 break;
3103 if (j < hull->n_eq)
3104 continue;
3106 for (j = 0; j < hull->n_ineq; ++j) {
3107 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3108 continue;
3109 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3110 left) != -1 ||
3111 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3112 i) != -1)
3113 continue;
3114 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3115 lower = 1;
3116 else
3117 upper = 1;
3118 if (lower && upper)
3119 break;
3122 if (lower && upper)
3123 continue;
3125 if (!removed_divs) {
3126 set = isl_set_remove_divs(set);
3127 if (!set)
3128 goto error;
3129 removed_divs = 1;
3131 bounds = set_bounds(set, i);
3132 hull = isl_basic_set_intersect(hull, bounds);
3133 if (!hull)
3134 goto error;
3137 isl_set_free(set);
3138 return hull;
3139 error:
3140 isl_set_free(set);
3141 isl_basic_set_free(hull);
3142 return NULL;