drop isl_set_drop_redundant_divs
[isl.git] / isl_convex_hull.c
blob636a03dbece3737ddae702204442b7b262b67091
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_lp_private.h>
16 #include <isl/map.h>
17 #include <isl_mat_private.h>
18 #include <isl_vec_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_options_private.h>
22 #include "isl_equalities.h"
23 #include "isl_tab.h"
24 #include <isl_sort.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
30 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
32 /* Remove redundant
33 * constraints. If the minimal value along the normal of a constraint
34 * is the same if the constraint is removed, then the constraint is redundant.
36 * Since some constraints may be mutually redundant, sort the constraints
37 * first such that constraints that involve existentially quantified
38 * variables are considered for removal before those that do not.
39 * The sorting is also needed for the use in map_simple_hull.
41 * Note that isl_tab_detect_implicit_equalities may also end up
42 * marking some constraints as redundant. Make sure the constraints
43 * are preserved and undo those marking such that isl_tab_detect_redundant
44 * can consider the constraints in the sorted order.
46 * Alternatively, we could have intersected the basic map with the
47 * corresponding equality and then checked if the dimension was that
48 * of a facet.
50 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
51 __isl_take isl_basic_map *bmap)
53 struct isl_tab *tab;
55 if (!bmap)
56 return NULL;
58 bmap = isl_basic_map_gauss(bmap, NULL);
59 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
60 return bmap;
61 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
62 return bmap;
63 if (bmap->n_ineq <= 1)
64 return bmap;
66 bmap = isl_basic_map_sort_constraints(bmap);
67 tab = isl_tab_from_basic_map(bmap, 0);
68 if (!tab)
69 goto error;
70 tab->preserve = 1;
71 if (isl_tab_detect_implicit_equalities(tab) < 0)
72 goto error;
73 if (isl_tab_restore_redundant(tab) < 0)
74 goto error;
75 tab->preserve = 0;
76 if (isl_tab_detect_redundant(tab) < 0)
77 goto error;
78 bmap = isl_basic_map_update_from_tab(bmap, tab);
79 isl_tab_free(tab);
80 if (!bmap)
81 return NULL;
82 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
83 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
84 return bmap;
85 error:
86 isl_tab_free(tab);
87 isl_basic_map_free(bmap);
88 return NULL;
91 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
92 __isl_take isl_basic_set *bset)
94 return bset_from_bmap(
95 isl_basic_map_remove_redundancies(bset_to_bmap(bset)));
98 /* Remove redundant constraints in each of the basic maps.
100 __isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
102 return isl_map_inline_foreach_basic_map(map,
103 &isl_basic_map_remove_redundancies);
106 __isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
108 return isl_map_remove_redundancies(set);
111 /* Check if the set set is bound in the direction of the affine
112 * constraint c and if so, set the constant term such that the
113 * resulting constraint is a bounding constraint for the set.
115 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
117 int first;
118 int j;
119 isl_int opt;
120 isl_int opt_denom;
122 isl_int_init(opt);
123 isl_int_init(opt_denom);
124 first = 1;
125 for (j = 0; j < set->n; ++j) {
126 enum isl_lp_result res;
128 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
129 continue;
131 res = isl_basic_set_solve_lp(set->p[j],
132 0, c, set->ctx->one, &opt, &opt_denom, NULL);
133 if (res == isl_lp_unbounded)
134 break;
135 if (res == isl_lp_error)
136 goto error;
137 if (res == isl_lp_empty) {
138 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
139 if (!set->p[j])
140 goto error;
141 continue;
143 if (first || isl_int_is_neg(opt)) {
144 if (!isl_int_is_one(opt_denom))
145 isl_seq_scale(c, c, opt_denom, len);
146 isl_int_sub(c[0], c[0], opt);
148 first = 0;
150 isl_int_clear(opt);
151 isl_int_clear(opt_denom);
152 return j >= set->n;
153 error:
154 isl_int_clear(opt);
155 isl_int_clear(opt_denom);
156 return -1;
159 __isl_give isl_basic_map *isl_basic_map_set_rational(
160 __isl_take isl_basic_map *bmap)
162 if (!bmap)
163 return NULL;
165 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
166 return bmap;
168 bmap = isl_basic_map_cow(bmap);
169 if (!bmap)
170 return NULL;
172 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
174 return isl_basic_map_finalize(bmap);
177 __isl_give isl_basic_set *isl_basic_set_set_rational(
178 __isl_take isl_basic_set *bset)
180 return isl_basic_map_set_rational(bset);
183 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
185 int i;
187 map = isl_map_cow(map);
188 if (!map)
189 return NULL;
190 for (i = 0; i < map->n; ++i) {
191 map->p[i] = isl_basic_map_set_rational(map->p[i]);
192 if (!map->p[i])
193 goto error;
195 return map;
196 error:
197 isl_map_free(map);
198 return NULL;
201 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
203 return isl_map_set_rational(set);
206 static struct isl_basic_set *isl_basic_set_add_equality(
207 struct isl_basic_set *bset, isl_int *c)
209 int i;
210 unsigned dim;
212 if (!bset)
213 return NULL;
215 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
216 return bset;
218 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
219 isl_assert(bset->ctx, bset->n_div == 0, goto error);
220 dim = isl_basic_set_n_dim(bset);
221 bset = isl_basic_set_cow(bset);
222 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
223 i = isl_basic_set_alloc_equality(bset);
224 if (i < 0)
225 goto error;
226 isl_seq_cpy(bset->eq[i], c, 1 + dim);
227 return bset;
228 error:
229 isl_basic_set_free(bset);
230 return NULL;
233 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
235 int i;
237 set = isl_set_cow(set);
238 if (!set)
239 return NULL;
240 for (i = 0; i < set->n; ++i) {
241 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
242 if (!set->p[i])
243 goto error;
245 return set;
246 error:
247 isl_set_free(set);
248 return NULL;
251 /* Given a union of basic sets, construct the constraints for wrapping
252 * a facet around one of its ridges.
253 * In particular, if each of n the d-dimensional basic sets i in "set"
254 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
255 * and is defined by the constraints
256 * [ 1 ]
257 * A_i [ x ] >= 0
259 * then the resulting set is of dimension n*(1+d) and has as constraints
261 * [ a_i ]
262 * A_i [ x_i ] >= 0
264 * a_i >= 0
266 * \sum_i x_{i,1} = 1
268 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
270 struct isl_basic_set *lp;
271 unsigned n_eq;
272 unsigned n_ineq;
273 int i, j, k;
274 unsigned dim, lp_dim;
276 if (!set)
277 return NULL;
279 dim = 1 + isl_set_n_dim(set);
280 n_eq = 1;
281 n_ineq = set->n;
282 for (i = 0; i < set->n; ++i) {
283 n_eq += set->p[i]->n_eq;
284 n_ineq += set->p[i]->n_ineq;
286 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
287 lp = isl_basic_set_set_rational(lp);
288 if (!lp)
289 return NULL;
290 lp_dim = isl_basic_set_n_dim(lp);
291 k = isl_basic_set_alloc_equality(lp);
292 isl_int_set_si(lp->eq[k][0], -1);
293 for (i = 0; i < set->n; ++i) {
294 isl_int_set_si(lp->eq[k][1+dim*i], 0);
295 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
296 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
298 for (i = 0; i < set->n; ++i) {
299 k = isl_basic_set_alloc_inequality(lp);
300 isl_seq_clr(lp->ineq[k], 1+lp_dim);
301 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
303 for (j = 0; j < set->p[i]->n_eq; ++j) {
304 k = isl_basic_set_alloc_equality(lp);
305 isl_seq_clr(lp->eq[k], 1+dim*i);
306 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
307 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
310 for (j = 0; j < set->p[i]->n_ineq; ++j) {
311 k = isl_basic_set_alloc_inequality(lp);
312 isl_seq_clr(lp->ineq[k], 1+dim*i);
313 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
314 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
317 return lp;
320 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
321 * of that facet, compute the other facet of the convex hull that contains
322 * the ridge.
324 * We first transform the set such that the facet constraint becomes
326 * x_1 >= 0
328 * I.e., the facet lies in
330 * x_1 = 0
332 * and on that facet, the constraint that defines the ridge is
334 * x_2 >= 0
336 * (This transformation is not strictly needed, all that is needed is
337 * that the ridge contains the origin.)
339 * Since the ridge contains the origin, the cone of the convex hull
340 * will be of the form
342 * x_1 >= 0
343 * x_2 >= a x_1
345 * with this second constraint defining the new facet.
346 * The constant a is obtained by settting x_1 in the cone of the
347 * convex hull to 1 and minimizing x_2.
348 * Now, each element in the cone of the convex hull is the sum
349 * of elements in the cones of the basic sets.
350 * If a_i is the dilation factor of basic set i, then the problem
351 * we need to solve is
353 * min \sum_i x_{i,2}
354 * st
355 * \sum_i x_{i,1} = 1
356 * a_i >= 0
357 * [ a_i ]
358 * A [ x_i ] >= 0
360 * with
361 * [ 1 ]
362 * A_i [ x_i ] >= 0
364 * the constraints of each (transformed) basic set.
365 * If a = n/d, then the constraint defining the new facet (in the transformed
366 * space) is
368 * -n x_1 + d x_2 >= 0
370 * In the original space, we need to take the same combination of the
371 * corresponding constraints "facet" and "ridge".
373 * If a = -infty = "-1/0", then we just return the original facet constraint.
374 * This means that the facet is unbounded, but has a bounded intersection
375 * with the union of sets.
377 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
378 isl_int *facet, isl_int *ridge)
380 int i;
381 isl_ctx *ctx;
382 struct isl_mat *T = NULL;
383 struct isl_basic_set *lp = NULL;
384 struct isl_vec *obj;
385 enum isl_lp_result res;
386 isl_int num, den;
387 unsigned dim;
389 if (!set)
390 return NULL;
391 ctx = set->ctx;
392 set = isl_set_copy(set);
393 set = isl_set_set_rational(set);
395 dim = 1 + isl_set_n_dim(set);
396 T = isl_mat_alloc(ctx, 3, dim);
397 if (!T)
398 goto error;
399 isl_int_set_si(T->row[0][0], 1);
400 isl_seq_clr(T->row[0]+1, dim - 1);
401 isl_seq_cpy(T->row[1], facet, dim);
402 isl_seq_cpy(T->row[2], ridge, dim);
403 T = isl_mat_right_inverse(T);
404 set = isl_set_preimage(set, T);
405 T = NULL;
406 if (!set)
407 goto error;
408 lp = wrap_constraints(set);
409 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
410 if (!obj)
411 goto error;
412 isl_int_set_si(obj->block.data[0], 0);
413 for (i = 0; i < set->n; ++i) {
414 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
415 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
416 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
418 isl_int_init(num);
419 isl_int_init(den);
420 res = isl_basic_set_solve_lp(lp, 0,
421 obj->block.data, ctx->one, &num, &den, NULL);
422 if (res == isl_lp_ok) {
423 isl_int_neg(num, num);
424 isl_seq_combine(facet, num, facet, den, ridge, dim);
425 isl_seq_normalize(ctx, facet, dim);
427 isl_int_clear(num);
428 isl_int_clear(den);
429 isl_vec_free(obj);
430 isl_basic_set_free(lp);
431 isl_set_free(set);
432 if (res == isl_lp_error)
433 return NULL;
434 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
435 return NULL);
436 return facet;
437 error:
438 isl_basic_set_free(lp);
439 isl_mat_free(T);
440 isl_set_free(set);
441 return NULL;
444 /* Compute the constraint of a facet of "set".
446 * We first compute the intersection with a bounding constraint
447 * that is orthogonal to one of the coordinate axes.
448 * If the affine hull of this intersection has only one equality,
449 * we have found a facet.
450 * Otherwise, we wrap the current bounding constraint around
451 * one of the equalities of the face (one that is not equal to
452 * the current bounding constraint).
453 * This process continues until we have found a facet.
454 * The dimension of the intersection increases by at least
455 * one on each iteration, so termination is guaranteed.
457 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
459 struct isl_set *slice = NULL;
460 struct isl_basic_set *face = NULL;
461 int i;
462 unsigned dim = isl_set_n_dim(set);
463 int is_bound;
464 isl_mat *bounds = NULL;
466 isl_assert(set->ctx, set->n > 0, goto error);
467 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
468 if (!bounds)
469 return NULL;
471 isl_seq_clr(bounds->row[0], dim);
472 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
473 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
474 if (is_bound < 0)
475 goto error;
476 isl_assert(set->ctx, is_bound, goto error);
477 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
478 bounds->n_row = 1;
480 for (;;) {
481 slice = isl_set_copy(set);
482 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
483 face = isl_set_affine_hull(slice);
484 if (!face)
485 goto error;
486 if (face->n_eq == 1) {
487 isl_basic_set_free(face);
488 break;
490 for (i = 0; i < face->n_eq; ++i)
491 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
492 !isl_seq_is_neg(bounds->row[0],
493 face->eq[i], 1 + dim))
494 break;
495 isl_assert(set->ctx, i < face->n_eq, goto error);
496 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
497 goto error;
498 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
499 isl_basic_set_free(face);
502 return bounds;
503 error:
504 isl_basic_set_free(face);
505 isl_mat_free(bounds);
506 return NULL;
509 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
510 * compute a hyperplane description of the facet, i.e., compute the facets
511 * of the facet.
513 * We compute an affine transformation that transforms the constraint
515 * [ 1 ]
516 * c [ x ] = 0
518 * to the constraint
520 * z_1 = 0
522 * by computing the right inverse U of a matrix that starts with the rows
524 * [ 1 0 ]
525 * [ c ]
527 * Then
528 * [ 1 ] [ 1 ]
529 * [ x ] = U [ z ]
530 * and
531 * [ 1 ] [ 1 ]
532 * [ z ] = Q [ x ]
534 * with Q = U^{-1}
535 * Since z_1 is zero, we can drop this variable as well as the corresponding
536 * column of U to obtain
538 * [ 1 ] [ 1 ]
539 * [ x ] = U' [ z' ]
540 * and
541 * [ 1 ] [ 1 ]
542 * [ z' ] = Q' [ x ]
544 * with Q' equal to Q, but without the corresponding row.
545 * After computing the facets of the facet in the z' space,
546 * we convert them back to the x space through Q.
548 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
550 struct isl_mat *m, *U, *Q;
551 struct isl_basic_set *facet = NULL;
552 struct isl_ctx *ctx;
553 unsigned dim;
555 ctx = set->ctx;
556 set = isl_set_copy(set);
557 dim = isl_set_n_dim(set);
558 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
559 if (!m)
560 goto error;
561 isl_int_set_si(m->row[0][0], 1);
562 isl_seq_clr(m->row[0]+1, dim);
563 isl_seq_cpy(m->row[1], c, 1+dim);
564 U = isl_mat_right_inverse(m);
565 Q = isl_mat_right_inverse(isl_mat_copy(U));
566 U = isl_mat_drop_cols(U, 1, 1);
567 Q = isl_mat_drop_rows(Q, 1, 1);
568 set = isl_set_preimage(set, U);
569 facet = uset_convex_hull_wrap_bounded(set);
570 facet = isl_basic_set_preimage(facet, Q);
571 if (facet && facet->n_eq != 0)
572 isl_die(ctx, isl_error_internal, "unexpected equality",
573 return isl_basic_set_free(facet));
574 return facet;
575 error:
576 isl_basic_set_free(facet);
577 isl_set_free(set);
578 return NULL;
581 /* Given an initial facet constraint, compute the remaining facets.
582 * We do this by running through all facets found so far and computing
583 * the adjacent facets through wrapping, adding those facets that we
584 * hadn't already found before.
586 * For each facet we have found so far, we first compute its facets
587 * in the resulting convex hull. That is, we compute the ridges
588 * of the resulting convex hull contained in the facet.
589 * We also compute the corresponding facet in the current approximation
590 * of the convex hull. There is no need to wrap around the ridges
591 * in this facet since that would result in a facet that is already
592 * present in the current approximation.
594 * This function can still be significantly optimized by checking which of
595 * the facets of the basic sets are also facets of the convex hull and
596 * using all the facets so far to help in constructing the facets of the
597 * facets
598 * and/or
599 * using the technique in section "3.1 Ridge Generation" of
600 * "Extended Convex Hull" by Fukuda et al.
602 static struct isl_basic_set *extend(struct isl_basic_set *hull,
603 struct isl_set *set)
605 int i, j, f;
606 int k;
607 struct isl_basic_set *facet = NULL;
608 struct isl_basic_set *hull_facet = NULL;
609 unsigned dim;
611 if (!hull)
612 return NULL;
614 isl_assert(set->ctx, set->n > 0, goto error);
616 dim = isl_set_n_dim(set);
618 for (i = 0; i < hull->n_ineq; ++i) {
619 facet = compute_facet(set, hull->ineq[i]);
620 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
621 facet = isl_basic_set_gauss(facet, NULL);
622 facet = isl_basic_set_normalize_constraints(facet);
623 hull_facet = isl_basic_set_copy(hull);
624 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
625 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
626 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
627 if (!facet || !hull_facet)
628 goto error;
629 hull = isl_basic_set_cow(hull);
630 hull = isl_basic_set_extend_space(hull,
631 isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
632 if (!hull)
633 goto error;
634 for (j = 0; j < facet->n_ineq; ++j) {
635 for (f = 0; f < hull_facet->n_ineq; ++f)
636 if (isl_seq_eq(facet->ineq[j],
637 hull_facet->ineq[f], 1 + dim))
638 break;
639 if (f < hull_facet->n_ineq)
640 continue;
641 k = isl_basic_set_alloc_inequality(hull);
642 if (k < 0)
643 goto error;
644 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
645 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
646 goto error;
648 isl_basic_set_free(hull_facet);
649 isl_basic_set_free(facet);
651 hull = isl_basic_set_simplify(hull);
652 hull = isl_basic_set_finalize(hull);
653 return hull;
654 error:
655 isl_basic_set_free(hull_facet);
656 isl_basic_set_free(facet);
657 isl_basic_set_free(hull);
658 return NULL;
661 /* Special case for computing the convex hull of a one dimensional set.
662 * We simply collect the lower and upper bounds of each basic set
663 * and the biggest of those.
665 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
667 struct isl_mat *c = NULL;
668 isl_int *lower = NULL;
669 isl_int *upper = NULL;
670 int i, j, k;
671 isl_int a, b;
672 struct isl_basic_set *hull;
674 for (i = 0; i < set->n; ++i) {
675 set->p[i] = isl_basic_set_simplify(set->p[i]);
676 if (!set->p[i])
677 goto error;
679 set = isl_set_remove_empty_parts(set);
680 if (!set)
681 goto error;
682 isl_assert(set->ctx, set->n > 0, goto error);
683 c = isl_mat_alloc(set->ctx, 2, 2);
684 if (!c)
685 goto error;
687 if (set->p[0]->n_eq > 0) {
688 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
689 lower = c->row[0];
690 upper = c->row[1];
691 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
692 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
693 isl_seq_neg(upper, set->p[0]->eq[0], 2);
694 } else {
695 isl_seq_neg(lower, set->p[0]->eq[0], 2);
696 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
698 } else {
699 for (j = 0; j < set->p[0]->n_ineq; ++j) {
700 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
701 lower = c->row[0];
702 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
703 } else {
704 upper = c->row[1];
705 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
710 isl_int_init(a);
711 isl_int_init(b);
712 for (i = 0; i < set->n; ++i) {
713 struct isl_basic_set *bset = set->p[i];
714 int has_lower = 0;
715 int has_upper = 0;
717 for (j = 0; j < bset->n_eq; ++j) {
718 has_lower = 1;
719 has_upper = 1;
720 if (lower) {
721 isl_int_mul(a, lower[0], bset->eq[j][1]);
722 isl_int_mul(b, lower[1], bset->eq[j][0]);
723 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
724 isl_seq_cpy(lower, bset->eq[j], 2);
725 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
726 isl_seq_neg(lower, bset->eq[j], 2);
728 if (upper) {
729 isl_int_mul(a, upper[0], bset->eq[j][1]);
730 isl_int_mul(b, upper[1], bset->eq[j][0]);
731 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
732 isl_seq_neg(upper, bset->eq[j], 2);
733 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
734 isl_seq_cpy(upper, bset->eq[j], 2);
737 for (j = 0; j < bset->n_ineq; ++j) {
738 if (isl_int_is_pos(bset->ineq[j][1]))
739 has_lower = 1;
740 if (isl_int_is_neg(bset->ineq[j][1]))
741 has_upper = 1;
742 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
743 isl_int_mul(a, lower[0], bset->ineq[j][1]);
744 isl_int_mul(b, lower[1], bset->ineq[j][0]);
745 if (isl_int_lt(a, b))
746 isl_seq_cpy(lower, bset->ineq[j], 2);
748 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
749 isl_int_mul(a, upper[0], bset->ineq[j][1]);
750 isl_int_mul(b, upper[1], bset->ineq[j][0]);
751 if (isl_int_gt(a, b))
752 isl_seq_cpy(upper, bset->ineq[j], 2);
755 if (!has_lower)
756 lower = NULL;
757 if (!has_upper)
758 upper = NULL;
760 isl_int_clear(a);
761 isl_int_clear(b);
763 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
764 hull = isl_basic_set_set_rational(hull);
765 if (!hull)
766 goto error;
767 if (lower) {
768 k = isl_basic_set_alloc_inequality(hull);
769 isl_seq_cpy(hull->ineq[k], lower, 2);
771 if (upper) {
772 k = isl_basic_set_alloc_inequality(hull);
773 isl_seq_cpy(hull->ineq[k], upper, 2);
775 hull = isl_basic_set_finalize(hull);
776 isl_set_free(set);
777 isl_mat_free(c);
778 return hull;
779 error:
780 isl_set_free(set);
781 isl_mat_free(c);
782 return NULL;
785 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
787 struct isl_basic_set *convex_hull;
789 if (!set)
790 return NULL;
792 if (isl_set_is_empty(set))
793 convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
794 else
795 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
796 isl_set_free(set);
797 return convex_hull;
800 /* Compute the convex hull of a pair of basic sets without any parameters or
801 * integer divisions using Fourier-Motzkin elimination.
802 * The convex hull is the set of all points that can be written as
803 * the sum of points from both basic sets (in homogeneous coordinates).
804 * We set up the constraints in a space with dimensions for each of
805 * the three sets and then project out the dimensions corresponding
806 * to the two original basic sets, retaining only those corresponding
807 * to the convex hull.
809 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
810 struct isl_basic_set *bset2)
812 int i, j, k;
813 struct isl_basic_set *bset[2];
814 struct isl_basic_set *hull = NULL;
815 unsigned dim;
817 if (!bset1 || !bset2)
818 goto error;
820 dim = isl_basic_set_n_dim(bset1);
821 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
822 1 + dim + bset1->n_eq + bset2->n_eq,
823 2 + bset1->n_ineq + bset2->n_ineq);
824 bset[0] = bset1;
825 bset[1] = bset2;
826 for (i = 0; i < 2; ++i) {
827 for (j = 0; j < bset[i]->n_eq; ++j) {
828 k = isl_basic_set_alloc_equality(hull);
829 if (k < 0)
830 goto error;
831 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
832 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
833 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
834 1+dim);
836 for (j = 0; j < bset[i]->n_ineq; ++j) {
837 k = isl_basic_set_alloc_inequality(hull);
838 if (k < 0)
839 goto error;
840 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
841 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
842 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
843 bset[i]->ineq[j], 1+dim);
845 k = isl_basic_set_alloc_inequality(hull);
846 if (k < 0)
847 goto error;
848 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
849 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
851 for (j = 0; j < 1+dim; ++j) {
852 k = isl_basic_set_alloc_equality(hull);
853 if (k < 0)
854 goto error;
855 isl_seq_clr(hull->eq[k], 1+2+3*dim);
856 isl_int_set_si(hull->eq[k][j], -1);
857 isl_int_set_si(hull->eq[k][1+dim+j], 1);
858 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
860 hull = isl_basic_set_set_rational(hull);
861 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
862 hull = isl_basic_set_remove_redundancies(hull);
863 isl_basic_set_free(bset1);
864 isl_basic_set_free(bset2);
865 return hull;
866 error:
867 isl_basic_set_free(bset1);
868 isl_basic_set_free(bset2);
869 isl_basic_set_free(hull);
870 return NULL;
873 /* Is the set bounded for each value of the parameters?
875 isl_bool isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
877 struct isl_tab *tab;
878 isl_bool bounded;
880 if (!bset)
881 return isl_bool_error;
882 if (isl_basic_set_plain_is_empty(bset))
883 return isl_bool_true;
885 tab = isl_tab_from_recession_cone(bset, 1);
886 bounded = isl_tab_cone_is_bounded(tab);
887 isl_tab_free(tab);
888 return bounded;
891 /* Is the image bounded for each value of the parameters and
892 * the domain variables?
894 isl_bool isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
896 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
897 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
898 isl_bool bounded;
900 bmap = isl_basic_map_copy(bmap);
901 bmap = isl_basic_map_cow(bmap);
902 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
903 isl_dim_in, 0, n_in);
904 bounded = isl_basic_set_is_bounded(bset_from_bmap(bmap));
905 isl_basic_map_free(bmap);
907 return bounded;
910 /* Is the set bounded for each value of the parameters?
912 isl_bool isl_set_is_bounded(__isl_keep isl_set *set)
914 int i;
916 if (!set)
917 return isl_bool_error;
919 for (i = 0; i < set->n; ++i) {
920 isl_bool bounded = isl_basic_set_is_bounded(set->p[i]);
921 if (!bounded || bounded < 0)
922 return bounded;
924 return isl_bool_true;
927 /* Compute the lineality space of the convex hull of bset1 and bset2.
929 * We first compute the intersection of the recession cone of bset1
930 * with the negative of the recession cone of bset2 and then compute
931 * the linear hull of the resulting cone.
933 static struct isl_basic_set *induced_lineality_space(
934 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
936 int i, k;
937 struct isl_basic_set *lin = NULL;
938 unsigned dim;
940 if (!bset1 || !bset2)
941 goto error;
943 dim = isl_basic_set_total_dim(bset1);
944 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
945 bset1->n_eq + bset2->n_eq,
946 bset1->n_ineq + bset2->n_ineq);
947 lin = isl_basic_set_set_rational(lin);
948 if (!lin)
949 goto error;
950 for (i = 0; i < bset1->n_eq; ++i) {
951 k = isl_basic_set_alloc_equality(lin);
952 if (k < 0)
953 goto error;
954 isl_int_set_si(lin->eq[k][0], 0);
955 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
957 for (i = 0; i < bset1->n_ineq; ++i) {
958 k = isl_basic_set_alloc_inequality(lin);
959 if (k < 0)
960 goto error;
961 isl_int_set_si(lin->ineq[k][0], 0);
962 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
964 for (i = 0; i < bset2->n_eq; ++i) {
965 k = isl_basic_set_alloc_equality(lin);
966 if (k < 0)
967 goto error;
968 isl_int_set_si(lin->eq[k][0], 0);
969 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
971 for (i = 0; i < bset2->n_ineq; ++i) {
972 k = isl_basic_set_alloc_inequality(lin);
973 if (k < 0)
974 goto error;
975 isl_int_set_si(lin->ineq[k][0], 0);
976 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
979 isl_basic_set_free(bset1);
980 isl_basic_set_free(bset2);
981 return isl_basic_set_affine_hull(lin);
982 error:
983 isl_basic_set_free(lin);
984 isl_basic_set_free(bset1);
985 isl_basic_set_free(bset2);
986 return NULL;
989 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
991 /* Given a set and a linear space "lin" of dimension n > 0,
992 * project the linear space from the set, compute the convex hull
993 * and then map the set back to the original space.
995 * Let
997 * M x = 0
999 * describe the linear space. We first compute the Hermite normal
1000 * form H = M U of M = H Q, to obtain
1002 * H Q x = 0
1004 * The last n rows of H will be zero, so the last n variables of x' = Q x
1005 * are the one we want to project out. We do this by transforming each
1006 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1007 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1008 * we transform the hull back to the original space as A' Q_1 x >= b',
1009 * with Q_1 all but the last n rows of Q.
1011 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1012 struct isl_basic_set *lin)
1014 unsigned total = isl_basic_set_total_dim(lin);
1015 unsigned lin_dim;
1016 struct isl_basic_set *hull;
1017 struct isl_mat *M, *U, *Q;
1019 if (!set || !lin)
1020 goto error;
1021 lin_dim = total - lin->n_eq;
1022 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1023 M = isl_mat_left_hermite(M, 0, &U, &Q);
1024 if (!M)
1025 goto error;
1026 isl_mat_free(M);
1027 isl_basic_set_free(lin);
1029 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1031 U = isl_mat_lin_to_aff(U);
1032 Q = isl_mat_lin_to_aff(Q);
1034 set = isl_set_preimage(set, U);
1035 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1036 hull = uset_convex_hull(set);
1037 hull = isl_basic_set_preimage(hull, Q);
1039 return hull;
1040 error:
1041 isl_basic_set_free(lin);
1042 isl_set_free(set);
1043 return NULL;
1046 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1047 * set up an LP for solving
1049 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1051 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1052 * The next \alpha{ij} correspond to the equalities and come in pairs.
1053 * The final \alpha{ij} correspond to the inequalities.
1055 static struct isl_basic_set *valid_direction_lp(
1056 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1058 isl_space *dim;
1059 struct isl_basic_set *lp;
1060 unsigned d;
1061 int n;
1062 int i, j, k;
1064 if (!bset1 || !bset2)
1065 goto error;
1066 d = 1 + isl_basic_set_total_dim(bset1);
1067 n = 2 +
1068 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1069 dim = isl_space_set_alloc(bset1->ctx, 0, n);
1070 lp = isl_basic_set_alloc_space(dim, 0, d, n);
1071 if (!lp)
1072 goto error;
1073 for (i = 0; i < n; ++i) {
1074 k = isl_basic_set_alloc_inequality(lp);
1075 if (k < 0)
1076 goto error;
1077 isl_seq_clr(lp->ineq[k] + 1, n);
1078 isl_int_set_si(lp->ineq[k][0], -1);
1079 isl_int_set_si(lp->ineq[k][1 + i], 1);
1081 for (i = 0; i < d; ++i) {
1082 k = isl_basic_set_alloc_equality(lp);
1083 if (k < 0)
1084 goto error;
1085 n = 0;
1086 isl_int_set_si(lp->eq[k][n], 0); n++;
1087 /* positivity constraint 1 >= 0 */
1088 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1089 for (j = 0; j < bset1->n_eq; ++j) {
1090 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1091 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1093 for (j = 0; j < bset1->n_ineq; ++j) {
1094 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1096 /* positivity constraint 1 >= 0 */
1097 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1098 for (j = 0; j < bset2->n_eq; ++j) {
1099 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1100 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1102 for (j = 0; j < bset2->n_ineq; ++j) {
1103 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1106 lp = isl_basic_set_gauss(lp, NULL);
1107 isl_basic_set_free(bset1);
1108 isl_basic_set_free(bset2);
1109 return lp;
1110 error:
1111 isl_basic_set_free(bset1);
1112 isl_basic_set_free(bset2);
1113 return NULL;
1116 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1117 * for all rays in the homogeneous space of the two cones that correspond
1118 * to the input polyhedra bset1 and bset2.
1120 * We compute s as a vector that satisfies
1122 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1124 * with h_{ij} the normals of the facets of polyhedron i
1125 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1126 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1127 * We first set up an LP with as variables the \alpha{ij}.
1128 * In this formulation, for each polyhedron i,
1129 * the first constraint is the positivity constraint, followed by pairs
1130 * of variables for the equalities, followed by variables for the inequalities.
1131 * We then simply pick a feasible solution and compute s using (*).
1133 * Note that we simply pick any valid direction and make no attempt
1134 * to pick a "good" or even the "best" valid direction.
1136 static struct isl_vec *valid_direction(
1137 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1139 struct isl_basic_set *lp;
1140 struct isl_tab *tab;
1141 struct isl_vec *sample = NULL;
1142 struct isl_vec *dir;
1143 unsigned d;
1144 int i;
1145 int n;
1147 if (!bset1 || !bset2)
1148 goto error;
1149 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1150 isl_basic_set_copy(bset2));
1151 tab = isl_tab_from_basic_set(lp, 0);
1152 sample = isl_tab_get_sample_value(tab);
1153 isl_tab_free(tab);
1154 isl_basic_set_free(lp);
1155 if (!sample)
1156 goto error;
1157 d = isl_basic_set_total_dim(bset1);
1158 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1159 if (!dir)
1160 goto error;
1161 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1162 n = 1;
1163 /* positivity constraint 1 >= 0 */
1164 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1165 for (i = 0; i < bset1->n_eq; ++i) {
1166 isl_int_sub(sample->block.data[n],
1167 sample->block.data[n], sample->block.data[n+1]);
1168 isl_seq_combine(dir->block.data,
1169 bset1->ctx->one, dir->block.data,
1170 sample->block.data[n], bset1->eq[i], 1 + d);
1172 n += 2;
1174 for (i = 0; i < bset1->n_ineq; ++i)
1175 isl_seq_combine(dir->block.data,
1176 bset1->ctx->one, dir->block.data,
1177 sample->block.data[n++], bset1->ineq[i], 1 + d);
1178 isl_vec_free(sample);
1179 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1180 isl_basic_set_free(bset1);
1181 isl_basic_set_free(bset2);
1182 return dir;
1183 error:
1184 isl_vec_free(sample);
1185 isl_basic_set_free(bset1);
1186 isl_basic_set_free(bset2);
1187 return NULL;
1190 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1191 * compute b_i' + A_i' x' >= 0, with
1193 * [ b_i A_i ] [ y' ] [ y' ]
1194 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1196 * In particular, add the "positivity constraint" and then perform
1197 * the mapping.
1199 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1200 struct isl_mat *T)
1202 int k;
1204 if (!bset)
1205 goto error;
1206 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1207 k = isl_basic_set_alloc_inequality(bset);
1208 if (k < 0)
1209 goto error;
1210 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1211 isl_int_set_si(bset->ineq[k][0], 1);
1212 bset = isl_basic_set_preimage(bset, T);
1213 return bset;
1214 error:
1215 isl_mat_free(T);
1216 isl_basic_set_free(bset);
1217 return NULL;
1220 /* Compute the convex hull of a pair of basic sets without any parameters or
1221 * integer divisions, where the convex hull is known to be pointed,
1222 * but the basic sets may be unbounded.
1224 * We turn this problem into the computation of a convex hull of a pair
1225 * _bounded_ polyhedra by "changing the direction of the homogeneous
1226 * dimension". This idea is due to Matthias Koeppe.
1228 * Consider the cones in homogeneous space that correspond to the
1229 * input polyhedra. The rays of these cones are also rays of the
1230 * polyhedra if the coordinate that corresponds to the homogeneous
1231 * dimension is zero. That is, if the inner product of the rays
1232 * with the homogeneous direction is zero.
1233 * The cones in the homogeneous space can also be considered to
1234 * correspond to other pairs of polyhedra by chosing a different
1235 * homogeneous direction. To ensure that both of these polyhedra
1236 * are bounded, we need to make sure that all rays of the cones
1237 * correspond to vertices and not to rays.
1238 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1239 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1240 * The vector s is computed in valid_direction.
1242 * Note that we need to consider _all_ rays of the cones and not just
1243 * the rays that correspond to rays in the polyhedra. If we were to
1244 * only consider those rays and turn them into vertices, then we
1245 * may inadvertently turn some vertices into rays.
1247 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1248 * We therefore transform the two polyhedra such that the selected
1249 * direction is mapped onto this standard direction and then proceed
1250 * with the normal computation.
1251 * Let S be a non-singular square matrix with s as its first row,
1252 * then we want to map the polyhedra to the space
1254 * [ y' ] [ y ] [ y ] [ y' ]
1255 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1257 * We take S to be the unimodular completion of s to limit the growth
1258 * of the coefficients in the following computations.
1260 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1261 * We first move to the homogeneous dimension
1263 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1264 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1266 * Then we change directoin
1268 * [ b_i A_i ] [ y' ] [ y' ]
1269 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1271 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1272 * resulting in b' + A' x' >= 0, which we then convert back
1274 * [ y ] [ y ]
1275 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1277 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1279 static struct isl_basic_set *convex_hull_pair_pointed(
1280 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1282 struct isl_ctx *ctx = NULL;
1283 struct isl_vec *dir = NULL;
1284 struct isl_mat *T = NULL;
1285 struct isl_mat *T2 = NULL;
1286 struct isl_basic_set *hull;
1287 struct isl_set *set;
1289 if (!bset1 || !bset2)
1290 goto error;
1291 ctx = isl_basic_set_get_ctx(bset1);
1292 dir = valid_direction(isl_basic_set_copy(bset1),
1293 isl_basic_set_copy(bset2));
1294 if (!dir)
1295 goto error;
1296 T = isl_mat_alloc(ctx, dir->size, dir->size);
1297 if (!T)
1298 goto error;
1299 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1300 T = isl_mat_unimodular_complete(T, 1);
1301 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1303 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1304 bset2 = homogeneous_map(bset2, T2);
1305 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1306 set = isl_set_add_basic_set(set, bset1);
1307 set = isl_set_add_basic_set(set, bset2);
1308 hull = uset_convex_hull(set);
1309 hull = isl_basic_set_preimage(hull, T);
1311 isl_vec_free(dir);
1313 return hull;
1314 error:
1315 isl_vec_free(dir);
1316 isl_basic_set_free(bset1);
1317 isl_basic_set_free(bset2);
1318 return NULL;
1321 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1322 static struct isl_basic_set *modulo_affine_hull(
1323 struct isl_set *set, struct isl_basic_set *affine_hull);
1325 /* Compute the convex hull of a pair of basic sets without any parameters or
1326 * integer divisions.
1328 * This function is called from uset_convex_hull_unbounded, which
1329 * means that the complete convex hull is unbounded. Some pairs
1330 * of basic sets may still be bounded, though.
1331 * They may even lie inside a lower dimensional space, in which
1332 * case they need to be handled inside their affine hull since
1333 * the main algorithm assumes that the result is full-dimensional.
1335 * If the convex hull of the two basic sets would have a non-trivial
1336 * lineality space, we first project out this lineality space.
1338 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1339 struct isl_basic_set *bset2)
1341 isl_basic_set *lin, *aff;
1342 int bounded1, bounded2;
1344 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1345 return convex_hull_pair_elim(bset1, bset2);
1347 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1348 isl_basic_set_copy(bset2)));
1349 if (!aff)
1350 goto error;
1351 if (aff->n_eq != 0)
1352 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1353 isl_basic_set_free(aff);
1355 bounded1 = isl_basic_set_is_bounded(bset1);
1356 bounded2 = isl_basic_set_is_bounded(bset2);
1358 if (bounded1 < 0 || bounded2 < 0)
1359 goto error;
1361 if (bounded1 && bounded2)
1362 return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1364 if (bounded1 || bounded2)
1365 return convex_hull_pair_pointed(bset1, bset2);
1367 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1368 isl_basic_set_copy(bset2));
1369 if (!lin)
1370 goto error;
1371 if (isl_basic_set_plain_is_universe(lin)) {
1372 isl_basic_set_free(bset1);
1373 isl_basic_set_free(bset2);
1374 return lin;
1376 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1377 struct isl_set *set;
1378 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1379 set = isl_set_add_basic_set(set, bset1);
1380 set = isl_set_add_basic_set(set, bset2);
1381 return modulo_lineality(set, lin);
1383 isl_basic_set_free(lin);
1385 return convex_hull_pair_pointed(bset1, bset2);
1386 error:
1387 isl_basic_set_free(bset1);
1388 isl_basic_set_free(bset2);
1389 return NULL;
1392 /* Compute the lineality space of a basic set.
1393 * We currently do not allow the basic set to have any divs.
1394 * We basically just drop the constants and turn every inequality
1395 * into an equality.
1397 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1399 int i, k;
1400 struct isl_basic_set *lin = NULL;
1401 unsigned dim;
1403 if (!bset)
1404 goto error;
1405 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1406 dim = isl_basic_set_total_dim(bset);
1408 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, dim, 0);
1409 if (!lin)
1410 goto error;
1411 for (i = 0; i < bset->n_eq; ++i) {
1412 k = isl_basic_set_alloc_equality(lin);
1413 if (k < 0)
1414 goto error;
1415 isl_int_set_si(lin->eq[k][0], 0);
1416 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1418 lin = isl_basic_set_gauss(lin, NULL);
1419 if (!lin)
1420 goto error;
1421 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1422 k = isl_basic_set_alloc_equality(lin);
1423 if (k < 0)
1424 goto error;
1425 isl_int_set_si(lin->eq[k][0], 0);
1426 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1427 lin = isl_basic_set_gauss(lin, NULL);
1428 if (!lin)
1429 goto error;
1431 isl_basic_set_free(bset);
1432 return lin;
1433 error:
1434 isl_basic_set_free(lin);
1435 isl_basic_set_free(bset);
1436 return NULL;
1439 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1440 * "underlying" set "set".
1442 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1444 int i;
1445 struct isl_set *lin = NULL;
1447 if (!set)
1448 return NULL;
1449 if (set->n == 0) {
1450 isl_space *dim = isl_set_get_space(set);
1451 isl_set_free(set);
1452 return isl_basic_set_empty(dim);
1455 lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
1456 for (i = 0; i < set->n; ++i)
1457 lin = isl_set_add_basic_set(lin,
1458 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1459 isl_set_free(set);
1460 return isl_set_affine_hull(lin);
1463 /* Compute the convex hull of a set without any parameters or
1464 * integer divisions.
1465 * In each step, we combined two basic sets until only one
1466 * basic set is left.
1467 * The input basic sets are assumed not to have a non-trivial
1468 * lineality space. If any of the intermediate results has
1469 * a non-trivial lineality space, it is projected out.
1471 static __isl_give isl_basic_set *uset_convex_hull_unbounded(
1472 __isl_take isl_set *set)
1474 isl_basic_set_list *list;
1476 list = isl_set_get_basic_set_list(set);
1477 isl_set_free(set);
1479 while (list) {
1480 int n;
1481 struct isl_basic_set *t;
1482 isl_basic_set *bset1, *bset2;
1484 n = isl_basic_set_list_n_basic_set(list);
1485 if (n < 2)
1486 isl_die(isl_basic_set_list_get_ctx(list),
1487 isl_error_internal,
1488 "expecting at least two elements", goto error);
1489 bset1 = isl_basic_set_list_get_basic_set(list, n - 1);
1490 bset2 = isl_basic_set_list_get_basic_set(list, n - 2);
1491 bset1 = convex_hull_pair(bset1, bset2);
1492 if (n == 2) {
1493 isl_basic_set_list_free(list);
1494 return bset1;
1496 bset1 = isl_basic_set_underlying_set(bset1);
1497 list = isl_basic_set_list_drop(list, n - 2, 2);
1498 list = isl_basic_set_list_add(list, bset1);
1500 t = isl_basic_set_list_get_basic_set(list, n - 2);
1501 t = isl_basic_set_lineality_space(t);
1502 if (!t)
1503 goto error;
1504 if (isl_basic_set_plain_is_universe(t)) {
1505 isl_basic_set_list_free(list);
1506 return t;
1508 if (t->n_eq < isl_basic_set_total_dim(t)) {
1509 set = isl_basic_set_list_union(list);
1510 return modulo_lineality(set, t);
1512 isl_basic_set_free(t);
1515 return NULL;
1516 error:
1517 isl_basic_set_list_free(list);
1518 return NULL;
1521 /* Compute an initial hull for wrapping containing a single initial
1522 * facet.
1523 * This function assumes that the given set is bounded.
1525 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1526 struct isl_set *set)
1528 struct isl_mat *bounds = NULL;
1529 unsigned dim;
1530 int k;
1532 if (!hull)
1533 goto error;
1534 bounds = initial_facet_constraint(set);
1535 if (!bounds)
1536 goto error;
1537 k = isl_basic_set_alloc_inequality(hull);
1538 if (k < 0)
1539 goto error;
1540 dim = isl_set_n_dim(set);
1541 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1542 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1543 isl_mat_free(bounds);
1545 return hull;
1546 error:
1547 isl_basic_set_free(hull);
1548 isl_mat_free(bounds);
1549 return NULL;
1552 struct max_constraint {
1553 struct isl_mat *c;
1554 int count;
1555 int ineq;
1558 static int max_constraint_equal(const void *entry, const void *val)
1560 struct max_constraint *a = (struct max_constraint *)entry;
1561 isl_int *b = (isl_int *)val;
1563 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1566 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1567 isl_int *con, unsigned len, int n, int ineq)
1569 struct isl_hash_table_entry *entry;
1570 struct max_constraint *c;
1571 uint32_t c_hash;
1573 c_hash = isl_seq_get_hash(con + 1, len);
1574 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1575 con + 1, 0);
1576 if (!entry)
1577 return;
1578 c = entry->data;
1579 if (c->count < n) {
1580 isl_hash_table_remove(ctx, table, entry);
1581 return;
1583 c->count++;
1584 if (isl_int_gt(c->c->row[0][0], con[0]))
1585 return;
1586 if (isl_int_eq(c->c->row[0][0], con[0])) {
1587 if (ineq)
1588 c->ineq = ineq;
1589 return;
1591 c->c = isl_mat_cow(c->c);
1592 isl_int_set(c->c->row[0][0], con[0]);
1593 c->ineq = ineq;
1596 /* Check whether the constraint hash table "table" constains the constraint
1597 * "con".
1599 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1600 isl_int *con, unsigned len, int n)
1602 struct isl_hash_table_entry *entry;
1603 struct max_constraint *c;
1604 uint32_t c_hash;
1606 c_hash = isl_seq_get_hash(con + 1, len);
1607 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1608 con + 1, 0);
1609 if (!entry)
1610 return 0;
1611 c = entry->data;
1612 if (c->count < n)
1613 return 0;
1614 return isl_int_eq(c->c->row[0][0], con[0]);
1617 /* Check for inequality constraints of a basic set without equalities
1618 * such that the same or more stringent copies of the constraint appear
1619 * in all of the basic sets. Such constraints are necessarily facet
1620 * constraints of the convex hull.
1622 * If the resulting basic set is by chance identical to one of
1623 * the basic sets in "set", then we know that this basic set contains
1624 * all other basic sets and is therefore the convex hull of set.
1625 * In this case we set *is_hull to 1.
1627 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1628 struct isl_set *set, int *is_hull)
1630 int i, j, s, n;
1631 int min_constraints;
1632 int best;
1633 struct max_constraint *constraints = NULL;
1634 struct isl_hash_table *table = NULL;
1635 unsigned total;
1637 *is_hull = 0;
1639 for (i = 0; i < set->n; ++i)
1640 if (set->p[i]->n_eq == 0)
1641 break;
1642 if (i >= set->n)
1643 return hull;
1644 min_constraints = set->p[i]->n_ineq;
1645 best = i;
1646 for (i = best + 1; i < set->n; ++i) {
1647 if (set->p[i]->n_eq != 0)
1648 continue;
1649 if (set->p[i]->n_ineq >= min_constraints)
1650 continue;
1651 min_constraints = set->p[i]->n_ineq;
1652 best = i;
1654 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1655 min_constraints);
1656 if (!constraints)
1657 return hull;
1658 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1659 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1660 goto error;
1662 total = isl_space_dim(set->dim, isl_dim_all);
1663 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1664 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1665 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1666 if (!constraints[i].c)
1667 goto error;
1668 constraints[i].ineq = 1;
1670 for (i = 0; i < min_constraints; ++i) {
1671 struct isl_hash_table_entry *entry;
1672 uint32_t c_hash;
1673 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1674 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1675 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1676 if (!entry)
1677 goto error;
1678 isl_assert(hull->ctx, !entry->data, goto error);
1679 entry->data = &constraints[i];
1682 n = 0;
1683 for (s = 0; s < set->n; ++s) {
1684 if (s == best)
1685 continue;
1687 for (i = 0; i < set->p[s]->n_eq; ++i) {
1688 isl_int *eq = set->p[s]->eq[i];
1689 for (j = 0; j < 2; ++j) {
1690 isl_seq_neg(eq, eq, 1 + total);
1691 update_constraint(hull->ctx, table,
1692 eq, total, n, 0);
1695 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1696 isl_int *ineq = set->p[s]->ineq[i];
1697 update_constraint(hull->ctx, table, ineq, total, n,
1698 set->p[s]->n_eq == 0);
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_int *ineq = set->p[s]->ineq[i];
1721 if (!has_constraint(hull->ctx, table, ineq, total, n))
1722 break;
1724 if (i == set->p[s]->n_ineq)
1725 *is_hull = 1;
1728 isl_hash_table_clear(table);
1729 for (i = 0; i < min_constraints; ++i)
1730 isl_mat_free(constraints[i].c);
1731 free(constraints);
1732 free(table);
1733 return hull;
1734 error:
1735 isl_hash_table_clear(table);
1736 free(table);
1737 if (constraints)
1738 for (i = 0; i < min_constraints; ++i)
1739 isl_mat_free(constraints[i].c);
1740 free(constraints);
1741 return hull;
1744 /* Create a template for the convex hull of "set" and fill it up
1745 * obvious facet constraints, if any. If the result happens to
1746 * be the convex hull of "set" then *is_hull is set to 1.
1748 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1750 struct isl_basic_set *hull;
1751 unsigned n_ineq;
1752 int i;
1754 n_ineq = 1;
1755 for (i = 0; i < set->n; ++i) {
1756 n_ineq += set->p[i]->n_eq;
1757 n_ineq += set->p[i]->n_ineq;
1759 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1760 hull = isl_basic_set_set_rational(hull);
1761 if (!hull)
1762 return NULL;
1763 return common_constraints(hull, set, is_hull);
1766 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1768 struct isl_basic_set *hull;
1769 int is_hull;
1771 hull = proto_hull(set, &is_hull);
1772 if (hull && !is_hull) {
1773 if (hull->n_ineq == 0)
1774 hull = initial_hull(hull, set);
1775 hull = extend(hull, set);
1777 isl_set_free(set);
1779 return hull;
1782 /* Compute the convex hull of a set without any parameters or
1783 * integer divisions. Depending on whether the set is bounded,
1784 * we pass control to the wrapping based convex hull or
1785 * the Fourier-Motzkin elimination based convex hull.
1786 * We also handle a few special cases before checking the boundedness.
1788 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1790 isl_bool bounded;
1791 struct isl_basic_set *convex_hull = NULL;
1792 struct isl_basic_set *lin;
1794 if (isl_set_n_dim(set) == 0)
1795 return convex_hull_0d(set);
1797 set = isl_set_coalesce(set);
1798 set = isl_set_set_rational(set);
1800 if (!set)
1801 return NULL;
1802 if (set->n == 1) {
1803 convex_hull = isl_basic_set_copy(set->p[0]);
1804 isl_set_free(set);
1805 return convex_hull;
1807 if (isl_set_n_dim(set) == 1)
1808 return convex_hull_1d(set);
1810 bounded = isl_set_is_bounded(set);
1811 if (bounded < 0)
1812 goto error;
1813 if (bounded && set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1814 return uset_convex_hull_wrap(set);
1816 lin = uset_combined_lineality_space(isl_set_copy(set));
1817 if (!lin)
1818 goto error;
1819 if (isl_basic_set_plain_is_universe(lin)) {
1820 isl_set_free(set);
1821 return lin;
1823 if (lin->n_eq < isl_basic_set_total_dim(lin))
1824 return modulo_lineality(set, lin);
1825 isl_basic_set_free(lin);
1827 return uset_convex_hull_unbounded(set);
1828 error:
1829 isl_set_free(set);
1830 isl_basic_set_free(convex_hull);
1831 return NULL;
1834 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1835 * without parameters or divs and where the convex hull of set is
1836 * known to be full-dimensional.
1838 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1840 struct isl_basic_set *convex_hull = NULL;
1842 if (!set)
1843 goto error;
1845 if (isl_set_n_dim(set) == 0) {
1846 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1847 isl_set_free(set);
1848 convex_hull = isl_basic_set_set_rational(convex_hull);
1849 return convex_hull;
1852 set = isl_set_set_rational(set);
1853 set = isl_set_coalesce(set);
1854 if (!set)
1855 goto error;
1856 if (set->n == 1) {
1857 convex_hull = isl_basic_set_copy(set->p[0]);
1858 isl_set_free(set);
1859 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1860 return convex_hull;
1862 if (isl_set_n_dim(set) == 1)
1863 return convex_hull_1d(set);
1865 return uset_convex_hull_wrap(set);
1866 error:
1867 isl_set_free(set);
1868 return NULL;
1871 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1872 * We first remove the equalities (transforming the set), compute the
1873 * convex hull of the transformed set and then add the equalities back
1874 * (after performing the inverse transformation.
1876 static struct isl_basic_set *modulo_affine_hull(
1877 struct isl_set *set, struct isl_basic_set *affine_hull)
1879 struct isl_mat *T;
1880 struct isl_mat *T2;
1881 struct isl_basic_set *dummy;
1882 struct isl_basic_set *convex_hull;
1884 dummy = isl_basic_set_remove_equalities(
1885 isl_basic_set_copy(affine_hull), &T, &T2);
1886 if (!dummy)
1887 goto error;
1888 isl_basic_set_free(dummy);
1889 set = isl_set_preimage(set, T);
1890 convex_hull = uset_convex_hull(set);
1891 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1892 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1893 return convex_hull;
1894 error:
1895 isl_basic_set_free(affine_hull);
1896 isl_set_free(set);
1897 return NULL;
1900 /* Return an empty basic map living in the same space as "map".
1902 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1903 __isl_take isl_map *map)
1905 isl_space *space;
1907 space = isl_map_get_space(map);
1908 isl_map_free(map);
1909 return isl_basic_map_empty(space);
1912 /* Compute the convex hull of a map.
1914 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1915 * specifically, the wrapping of facets to obtain new facets.
1917 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1919 struct isl_basic_set *bset;
1920 struct isl_basic_map *model = NULL;
1921 struct isl_basic_set *affine_hull = NULL;
1922 struct isl_basic_map *convex_hull = NULL;
1923 struct isl_set *set = NULL;
1925 map = isl_map_detect_equalities(map);
1926 map = isl_map_align_divs(map);
1927 if (!map)
1928 goto error;
1930 if (map->n == 0)
1931 return replace_map_by_empty_basic_map(map);
1933 model = isl_basic_map_copy(map->p[0]);
1934 set = isl_map_underlying_set(map);
1935 if (!set)
1936 goto error;
1938 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1939 if (!affine_hull)
1940 goto error;
1941 if (affine_hull->n_eq != 0)
1942 bset = modulo_affine_hull(set, affine_hull);
1943 else {
1944 isl_basic_set_free(affine_hull);
1945 bset = uset_convex_hull(set);
1948 convex_hull = isl_basic_map_overlying_set(bset, model);
1949 if (!convex_hull)
1950 return NULL;
1952 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1953 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1954 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1955 return convex_hull;
1956 error:
1957 isl_set_free(set);
1958 isl_basic_map_free(model);
1959 return NULL;
1962 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1964 return bset_from_bmap(isl_map_convex_hull(set_to_map(set)));
1967 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1969 isl_basic_map *hull;
1971 hull = isl_map_convex_hull(map);
1972 return isl_basic_map_remove_divs(hull);
1975 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1977 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set)));
1980 struct sh_data_entry {
1981 struct isl_hash_table *table;
1982 struct isl_tab *tab;
1985 /* Holds the data needed during the simple hull computation.
1986 * In particular,
1987 * n the number of basic sets in the original set
1988 * hull_table a hash table of already computed constraints
1989 * in the simple hull
1990 * p for each basic set,
1991 * table a hash table of the constraints
1992 * tab the tableau corresponding to the basic set
1994 struct sh_data {
1995 struct isl_ctx *ctx;
1996 unsigned n;
1997 struct isl_hash_table *hull_table;
1998 struct sh_data_entry p[1];
2001 static void sh_data_free(struct sh_data *data)
2003 int i;
2005 if (!data)
2006 return;
2007 isl_hash_table_free(data->ctx, data->hull_table);
2008 for (i = 0; i < data->n; ++i) {
2009 isl_hash_table_free(data->ctx, data->p[i].table);
2010 isl_tab_free(data->p[i].tab);
2012 free(data);
2015 struct ineq_cmp_data {
2016 unsigned len;
2017 isl_int *p;
2020 static int has_ineq(const void *entry, const void *val)
2022 isl_int *row = (isl_int *)entry;
2023 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2025 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2026 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2029 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2030 isl_int *ineq, unsigned len)
2032 uint32_t c_hash;
2033 struct ineq_cmp_data v;
2034 struct isl_hash_table_entry *entry;
2036 v.len = len;
2037 v.p = ineq;
2038 c_hash = isl_seq_get_hash(ineq + 1, len);
2039 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2040 if (!entry)
2041 return - 1;
2042 entry->data = ineq;
2043 return 0;
2046 /* Fill hash table "table" with the constraints of "bset".
2047 * Equalities are added as two inequalities.
2048 * The value in the hash table is a pointer to the (in)equality of "bset".
2050 static int hash_basic_set(struct isl_hash_table *table,
2051 struct isl_basic_set *bset)
2053 int i, j;
2054 unsigned dim = isl_basic_set_total_dim(bset);
2056 for (i = 0; i < bset->n_eq; ++i) {
2057 for (j = 0; j < 2; ++j) {
2058 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2059 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2060 return -1;
2063 for (i = 0; i < bset->n_ineq; ++i) {
2064 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2065 return -1;
2067 return 0;
2070 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2072 struct sh_data *data;
2073 int i;
2075 data = isl_calloc(set->ctx, struct sh_data,
2076 sizeof(struct sh_data) +
2077 (set->n - 1) * sizeof(struct sh_data_entry));
2078 if (!data)
2079 return NULL;
2080 data->ctx = set->ctx;
2081 data->n = set->n;
2082 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2083 if (!data->hull_table)
2084 goto error;
2085 for (i = 0; i < set->n; ++i) {
2086 data->p[i].table = isl_hash_table_alloc(set->ctx,
2087 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2088 if (!data->p[i].table)
2089 goto error;
2090 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2091 goto error;
2093 return data;
2094 error:
2095 sh_data_free(data);
2096 return NULL;
2099 /* Check if inequality "ineq" is a bound for basic set "j" or if
2100 * it can be relaxed (by increasing the constant term) to become
2101 * a bound for that basic set. In the latter case, the constant
2102 * term is updated.
2103 * Relaxation of the constant term is only allowed if "shift" is set.
2105 * Return 1 if "ineq" is a bound
2106 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2107 * -1 if some error occurred
2109 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2110 isl_int *ineq, int shift)
2112 enum isl_lp_result res;
2113 isl_int opt;
2115 if (!data->p[j].tab) {
2116 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2117 if (!data->p[j].tab)
2118 return -1;
2121 isl_int_init(opt);
2123 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2124 &opt, NULL, 0);
2125 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2126 if (shift)
2127 isl_int_sub(ineq[0], ineq[0], opt);
2128 else
2129 res = isl_lp_unbounded;
2132 isl_int_clear(opt);
2134 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2135 res == isl_lp_unbounded ? 0 : -1;
2138 /* Set the constant term of "ineq" to the maximum of those of the constraints
2139 * in the basic sets of "set" following "i" that are parallel to "ineq".
2140 * That is, if any of the basic sets of "set" following "i" have a more
2141 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2142 * "c_hash" is the hash value of the linear part of "ineq".
2143 * "v" has been set up for use by has_ineq.
2145 * Note that the two inequality constraints corresponding to an equality are
2146 * represented by the same inequality constraint in data->p[j].table
2147 * (but with different hash values). This means the constraint (or at
2148 * least its constant term) may need to be temporarily negated to get
2149 * the actually hashed constraint.
2151 static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
2152 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2154 int j;
2155 isl_ctx *ctx;
2156 struct isl_hash_table_entry *entry;
2158 ctx = isl_set_get_ctx(set);
2159 for (j = i + 1; j < set->n; ++j) {
2160 int neg;
2161 isl_int *ineq_j;
2163 entry = isl_hash_table_find(ctx, data->p[j].table,
2164 c_hash, &has_ineq, v, 0);
2165 if (!entry)
2166 continue;
2168 ineq_j = entry->data;
2169 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2170 if (neg)
2171 isl_int_neg(ineq_j[0], ineq_j[0]);
2172 if (isl_int_gt(ineq_j[0], ineq[0]))
2173 isl_int_set(ineq[0], ineq_j[0]);
2174 if (neg)
2175 isl_int_neg(ineq_j[0], ineq_j[0]);
2179 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2180 * become a bound on the whole set. If so, add the (relaxed) inequality
2181 * to "hull". Relaxation is only allowed if "shift" is set.
2183 * We first check if "hull" already contains a translate of the inequality.
2184 * If so, we are done.
2185 * Then, we check if any of the previous basic sets contains a translate
2186 * of the inequality. If so, then we have already considered this
2187 * inequality and we are done.
2188 * Otherwise, for each basic set other than "i", we check if the inequality
2189 * is a bound on the basic set, but first replace the constant term
2190 * by the maximal value of any translate of the inequality in any
2191 * of the following basic sets.
2192 * For previous basic sets, we know that they do not contain a translate
2193 * of the inequality, so we directly call is_bound.
2194 * For following basic sets, we first check if a translate of the
2195 * inequality appears in its description. If so, the constant term
2196 * of the inequality has already been updated with respect to this
2197 * translate and the inequality is therefore known to be a bound
2198 * of this basic set.
2200 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2201 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
2202 int shift)
2204 uint32_t c_hash;
2205 struct ineq_cmp_data v;
2206 struct isl_hash_table_entry *entry;
2207 int j, k;
2209 if (!hull)
2210 return NULL;
2212 v.len = isl_basic_set_total_dim(hull);
2213 v.p = ineq;
2214 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2216 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2217 has_ineq, &v, 0);
2218 if (entry)
2219 return hull;
2221 for (j = 0; j < i; ++j) {
2222 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2223 c_hash, has_ineq, &v, 0);
2224 if (entry)
2225 break;
2227 if (j < i)
2228 return hull;
2230 k = isl_basic_set_alloc_inequality(hull);
2231 if (k < 0)
2232 goto error;
2233 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2235 set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
2236 for (j = 0; j < i; ++j) {
2237 int bound;
2238 bound = is_bound(data, set, j, hull->ineq[k], shift);
2239 if (bound < 0)
2240 goto error;
2241 if (!bound)
2242 break;
2244 if (j < i) {
2245 isl_basic_set_free_inequality(hull, 1);
2246 return hull;
2249 for (j = i + 1; j < set->n; ++j) {
2250 int bound;
2251 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2252 c_hash, has_ineq, &v, 0);
2253 if (entry)
2254 continue;
2255 bound = is_bound(data, set, j, hull->ineq[k], shift);
2256 if (bound < 0)
2257 goto error;
2258 if (!bound)
2259 break;
2261 if (j < set->n) {
2262 isl_basic_set_free_inequality(hull, 1);
2263 return hull;
2266 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2267 has_ineq, &v, 1);
2268 if (!entry)
2269 goto error;
2270 entry->data = hull->ineq[k];
2272 return hull;
2273 error:
2274 isl_basic_set_free(hull);
2275 return NULL;
2278 /* Check if any inequality from basic set "i" is or can be relaxed to
2279 * become a bound on the whole set. If so, add the (relaxed) inequality
2280 * to "hull". Relaxation is only allowed if "shift" is set.
2282 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2283 struct sh_data *data, struct isl_set *set, int i, int shift)
2285 int j, k;
2286 unsigned dim = isl_basic_set_total_dim(bset);
2288 for (j = 0; j < set->p[i]->n_eq; ++j) {
2289 for (k = 0; k < 2; ++k) {
2290 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2291 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2292 shift);
2295 for (j = 0; j < set->p[i]->n_ineq; ++j)
2296 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2297 return bset;
2300 /* Compute a superset of the convex hull of set that is described
2301 * by only (translates of) the constraints in the constituents of set.
2302 * Translation is only allowed if "shift" is set.
2304 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2305 int shift)
2307 struct sh_data *data = NULL;
2308 struct isl_basic_set *hull = NULL;
2309 unsigned n_ineq;
2310 int i;
2312 if (!set)
2313 return NULL;
2315 n_ineq = 0;
2316 for (i = 0; i < set->n; ++i) {
2317 if (!set->p[i])
2318 goto error;
2319 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2322 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2323 if (!hull)
2324 goto error;
2326 data = sh_data_alloc(set, n_ineq);
2327 if (!data)
2328 goto error;
2330 for (i = 0; i < set->n; ++i)
2331 hull = add_bounds(hull, data, set, i, shift);
2333 sh_data_free(data);
2334 isl_set_free(set);
2336 return hull;
2337 error:
2338 sh_data_free(data);
2339 isl_basic_set_free(hull);
2340 isl_set_free(set);
2341 return NULL;
2344 /* Compute a superset of the convex hull of map that is described
2345 * by only (translates of) the constraints in the constituents of map.
2346 * Handle trivial cases where map is NULL or contains at most one disjunct.
2348 static __isl_give isl_basic_map *map_simple_hull_trivial(
2349 __isl_take isl_map *map)
2351 isl_basic_map *hull;
2353 if (!map)
2354 return NULL;
2355 if (map->n == 0)
2356 return replace_map_by_empty_basic_map(map);
2358 hull = isl_basic_map_copy(map->p[0]);
2359 isl_map_free(map);
2360 return hull;
2363 /* Return a copy of the simple hull cached inside "map".
2364 * "shift" determines whether to return the cached unshifted or shifted
2365 * simple hull.
2367 static __isl_give isl_basic_map *cached_simple_hull(__isl_take isl_map *map,
2368 int shift)
2370 isl_basic_map *hull;
2372 hull = isl_basic_map_copy(map->cached_simple_hull[shift]);
2373 isl_map_free(map);
2375 return hull;
2378 /* Compute a superset of the convex hull of map that is described
2379 * by only (translates of) the constraints in the constituents of map.
2380 * Translation is only allowed if "shift" is set.
2382 * The constraints are sorted while removing redundant constraints
2383 * in order to indicate a preference of which constraints should
2384 * be preserved. In particular, pairs of constraints that are
2385 * sorted together are preferred to either both be preserved
2386 * or both be removed. The sorting is performed inside
2387 * isl_basic_map_remove_redundancies.
2389 * The result of the computation is stored in map->cached_simple_hull[shift]
2390 * such that it can be reused in subsequent calls. The cache is cleared
2391 * whenever the map is modified (in isl_map_cow).
2392 * Note that the results need to be stored in the input map for there
2393 * to be any chance that they may get reused. In particular, they
2394 * are stored in a copy of the input map that is saved before
2395 * the integer division alignment.
2397 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2398 int shift)
2400 struct isl_set *set = NULL;
2401 struct isl_basic_map *model = NULL;
2402 struct isl_basic_map *hull;
2403 struct isl_basic_map *affine_hull;
2404 struct isl_basic_set *bset = NULL;
2405 isl_map *input;
2407 if (!map || map->n <= 1)
2408 return map_simple_hull_trivial(map);
2410 if (map->cached_simple_hull[shift])
2411 return cached_simple_hull(map, shift);
2413 map = isl_map_detect_equalities(map);
2414 if (!map || map->n <= 1)
2415 return map_simple_hull_trivial(map);
2416 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2417 input = isl_map_copy(map);
2418 map = isl_map_align_divs(map);
2419 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2421 set = isl_map_underlying_set(map);
2423 bset = uset_simple_hull(set, shift);
2425 hull = isl_basic_map_overlying_set(bset, model);
2427 hull = isl_basic_map_intersect(hull, affine_hull);
2428 hull = isl_basic_map_remove_redundancies(hull);
2430 if (hull) {
2431 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2432 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2435 hull = isl_basic_map_finalize(hull);
2436 if (input)
2437 input->cached_simple_hull[shift] = isl_basic_map_copy(hull);
2438 isl_map_free(input);
2440 return hull;
2443 /* Compute a superset of the convex hull of map that is described
2444 * by only translates of the constraints in the constituents of map.
2446 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2448 return map_simple_hull(map, 1);
2451 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2453 return bset_from_bmap(isl_map_simple_hull(set_to_map(set)));
2456 /* Compute a superset of the convex hull of map that is described
2457 * by only the constraints in the constituents of map.
2459 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2460 __isl_take isl_map *map)
2462 return map_simple_hull(map, 0);
2465 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2466 __isl_take isl_set *set)
2468 return isl_map_unshifted_simple_hull(set);
2471 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2472 * A constraint that appears with different constant terms
2473 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2474 * (i.e., greatest) constant term.
2475 * "bmap1" and "bmap2" are assumed to have the same (known)
2476 * integer divisions.
2477 * The constraints of both "bmap1" and "bmap2" are assumed
2478 * to have been sorted using isl_basic_map_sort_constraints.
2480 * Run through the inequality constraints of "bmap1" and "bmap2"
2481 * in sorted order.
2482 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2483 * is removed.
2484 * If a match is found, the constraint is kept. If needed, the constant
2485 * term of the constraint is adjusted.
2487 static __isl_give isl_basic_map *select_shared_inequalities(
2488 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2490 int i1, i2;
2492 bmap1 = isl_basic_map_cow(bmap1);
2493 if (!bmap1 || !bmap2)
2494 return isl_basic_map_free(bmap1);
2496 i1 = bmap1->n_ineq - 1;
2497 i2 = bmap2->n_ineq - 1;
2498 while (bmap1 && i1 >= 0 && i2 >= 0) {
2499 int cmp;
2501 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2502 bmap2->ineq[i2]);
2503 if (cmp < 0) {
2504 --i2;
2505 continue;
2507 if (cmp > 0) {
2508 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2509 bmap1 = isl_basic_map_free(bmap1);
2510 --i1;
2511 continue;
2513 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2514 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2515 --i1;
2516 --i2;
2518 for (; i1 >= 0; --i1)
2519 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2520 bmap1 = isl_basic_map_free(bmap1);
2522 return bmap1;
2525 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2526 * "bmap1" and "bmap2" are assumed to have the same (known)
2527 * integer divisions.
2529 * Run through the equality constraints of "bmap1" and "bmap2".
2530 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2531 * is removed.
2533 static __isl_give isl_basic_map *select_shared_equalities(
2534 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2536 int i1, i2;
2537 unsigned total;
2539 bmap1 = isl_basic_map_cow(bmap1);
2540 if (!bmap1 || !bmap2)
2541 return isl_basic_map_free(bmap1);
2543 total = isl_basic_map_total_dim(bmap1);
2545 i1 = bmap1->n_eq - 1;
2546 i2 = bmap2->n_eq - 1;
2547 while (bmap1 && i1 >= 0 && i2 >= 0) {
2548 int last1, last2;
2550 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2551 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2552 if (last1 > last2) {
2553 --i2;
2554 continue;
2556 if (last1 < last2) {
2557 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2558 bmap1 = isl_basic_map_free(bmap1);
2559 --i1;
2560 continue;
2562 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2563 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2564 bmap1 = isl_basic_map_free(bmap1);
2566 --i1;
2567 --i2;
2569 for (; i1 >= 0; --i1)
2570 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2571 bmap1 = isl_basic_map_free(bmap1);
2573 return bmap1;
2576 /* Compute a superset of "bmap1" and "bmap2" that is described
2577 * by only the constraints that appear in both "bmap1" and "bmap2".
2579 * First drop constraints that involve unknown integer divisions
2580 * since it is not trivial to check whether two such integer divisions
2581 * in different basic maps are the same.
2582 * Then align the remaining (known) divs and sort the constraints.
2583 * Finally drop all inequalities and equalities from "bmap1" that
2584 * do not also appear in "bmap2".
2586 __isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2587 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2589 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2590 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2591 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2592 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2593 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2594 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2595 bmap1 = isl_basic_map_sort_constraints(bmap1);
2596 bmap2 = isl_basic_map_sort_constraints(bmap2);
2598 bmap1 = select_shared_inequalities(bmap1, bmap2);
2599 bmap1 = select_shared_equalities(bmap1, bmap2);
2601 isl_basic_map_free(bmap2);
2602 bmap1 = isl_basic_map_finalize(bmap1);
2603 return bmap1;
2606 /* Compute a superset of the convex hull of "map" that is described
2607 * by only the constraints in the constituents of "map".
2608 * In particular, the result is composed of constraints that appear
2609 * in each of the basic maps of "map"
2611 * Constraints that involve unknown integer divisions are dropped
2612 * since it is not trivial to check whether two such integer divisions
2613 * in different basic maps are the same.
2615 * The hull is initialized from the first basic map and then
2616 * updated with respect to the other basic maps in turn.
2618 __isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2619 __isl_take isl_map *map)
2621 int i;
2622 isl_basic_map *hull;
2624 if (!map)
2625 return NULL;
2626 if (map->n <= 1)
2627 return map_simple_hull_trivial(map);
2628 map = isl_map_drop_constraint_involving_unknown_divs(map);
2629 hull = isl_basic_map_copy(map->p[0]);
2630 for (i = 1; i < map->n; ++i) {
2631 isl_basic_map *bmap_i;
2633 bmap_i = isl_basic_map_copy(map->p[i]);
2634 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2637 isl_map_free(map);
2638 return hull;
2641 /* Compute a superset of the convex hull of "set" that is described
2642 * by only the constraints in the constituents of "set".
2643 * In particular, the result is composed of constraints that appear
2644 * in each of the basic sets of "set"
2646 __isl_give isl_basic_set *isl_set_plain_unshifted_simple_hull(
2647 __isl_take isl_set *set)
2649 return isl_map_plain_unshifted_simple_hull(set);
2652 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2654 * For each basic set in "set", we first check if the basic set
2655 * contains a translate of "ineq". If this translate is more relaxed,
2656 * then we assume that "ineq" is not a bound on this basic set.
2657 * Otherwise, we know that it is a bound.
2658 * If the basic set does not contain a translate of "ineq", then
2659 * we call is_bound to perform the test.
2661 static __isl_give isl_basic_set *add_bound_from_constraint(
2662 __isl_take isl_basic_set *hull, struct sh_data *data,
2663 __isl_keep isl_set *set, isl_int *ineq)
2665 int i, k;
2666 isl_ctx *ctx;
2667 uint32_t c_hash;
2668 struct ineq_cmp_data v;
2670 if (!hull || !set)
2671 return isl_basic_set_free(hull);
2673 v.len = isl_basic_set_total_dim(hull);
2674 v.p = ineq;
2675 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2677 ctx = isl_basic_set_get_ctx(hull);
2678 for (i = 0; i < set->n; ++i) {
2679 int bound;
2680 struct isl_hash_table_entry *entry;
2682 entry = isl_hash_table_find(ctx, data->p[i].table,
2683 c_hash, &has_ineq, &v, 0);
2684 if (entry) {
2685 isl_int *ineq_i = entry->data;
2686 int neg, more_relaxed;
2688 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2689 if (neg)
2690 isl_int_neg(ineq_i[0], ineq_i[0]);
2691 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2692 if (neg)
2693 isl_int_neg(ineq_i[0], ineq_i[0]);
2694 if (more_relaxed)
2695 break;
2696 else
2697 continue;
2699 bound = is_bound(data, set, i, ineq, 0);
2700 if (bound < 0)
2701 return isl_basic_set_free(hull);
2702 if (!bound)
2703 break;
2705 if (i < set->n)
2706 return hull;
2708 k = isl_basic_set_alloc_inequality(hull);
2709 if (k < 0)
2710 return isl_basic_set_free(hull);
2711 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2713 return hull;
2716 /* Compute a superset of the convex hull of "set" that is described
2717 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2718 * has no parameters or integer divisions.
2720 * The inequalities in "ineq" are assumed to have been sorted such
2721 * that constraints with the same linear part appear together and
2722 * that among constraints with the same linear part, those with
2723 * smaller constant term appear first.
2725 * We reuse the same data structure that is used by uset_simple_hull,
2726 * but we do not need the hull table since we will not consider the
2727 * same constraint more than once. We therefore allocate it with zero size.
2729 * We run through the constraints and try to add them one by one,
2730 * skipping identical constraints. If we have added a constraint and
2731 * the next constraint is a more relaxed translate, then we skip this
2732 * next constraint as well.
2734 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2735 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2737 int i;
2738 int last_added = 0;
2739 struct sh_data *data = NULL;
2740 isl_basic_set *hull = NULL;
2741 unsigned dim;
2743 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2744 if (!hull)
2745 goto error;
2747 data = sh_data_alloc(set, 0);
2748 if (!data)
2749 goto error;
2751 dim = isl_set_dim(set, isl_dim_set);
2752 for (i = 0; i < n_ineq; ++i) {
2753 int hull_n_ineq = hull->n_ineq;
2754 int parallel;
2756 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2757 dim);
2758 if (parallel &&
2759 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2760 continue;
2761 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2762 if (!hull)
2763 goto error;
2764 last_added = hull->n_ineq > hull_n_ineq;
2767 sh_data_free(data);
2768 isl_set_free(set);
2769 return hull;
2770 error:
2771 sh_data_free(data);
2772 isl_set_free(set);
2773 isl_basic_set_free(hull);
2774 return NULL;
2777 /* Collect pointers to all the inequalities in the elements of "list"
2778 * in "ineq". For equalities, store both a pointer to the equality and
2779 * a pointer to its opposite, which is first copied to "mat".
2780 * "ineq" and "mat" are assumed to have been preallocated to the right size
2781 * (the number of inequalities + 2 times the number of equalites and
2782 * the number of equalities, respectively).
2784 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2785 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2787 int i, j, n, n_eq, n_ineq;
2789 if (!mat)
2790 return NULL;
2792 n_eq = 0;
2793 n_ineq = 0;
2794 n = isl_basic_set_list_n_basic_set(list);
2795 for (i = 0; i < n; ++i) {
2796 isl_basic_set *bset;
2797 bset = isl_basic_set_list_get_basic_set(list, i);
2798 if (!bset)
2799 return isl_mat_free(mat);
2800 for (j = 0; j < bset->n_eq; ++j) {
2801 ineq[n_ineq++] = mat->row[n_eq];
2802 ineq[n_ineq++] = bset->eq[j];
2803 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2805 for (j = 0; j < bset->n_ineq; ++j)
2806 ineq[n_ineq++] = bset->ineq[j];
2807 isl_basic_set_free(bset);
2810 return mat;
2813 /* Comparison routine for use as an isl_sort callback.
2815 * Constraints with the same linear part are sorted together and
2816 * among constraints with the same linear part, those with smaller
2817 * constant term are sorted first.
2819 static int cmp_ineq(const void *a, const void *b, void *arg)
2821 unsigned dim = *(unsigned *) arg;
2822 isl_int * const *ineq1 = a;
2823 isl_int * const *ineq2 = b;
2824 int cmp;
2826 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2827 if (cmp != 0)
2828 return cmp;
2829 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2832 /* Compute a superset of the convex hull of "set" that is described
2833 * by only constraints in the elements of "list", where "set" has
2834 * no parameters or integer divisions.
2836 * We collect all the constraints in those elements and then
2837 * sort the constraints such that constraints with the same linear part
2838 * are sorted together and that those with smaller constant term are
2839 * sorted first.
2841 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2842 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2844 int i, n, n_eq, n_ineq;
2845 unsigned dim;
2846 isl_ctx *ctx;
2847 isl_mat *mat = NULL;
2848 isl_int **ineq = NULL;
2849 isl_basic_set *hull;
2851 if (!set)
2852 goto error;
2853 ctx = isl_set_get_ctx(set);
2855 n_eq = 0;
2856 n_ineq = 0;
2857 n = isl_basic_set_list_n_basic_set(list);
2858 for (i = 0; i < n; ++i) {
2859 isl_basic_set *bset;
2860 bset = isl_basic_set_list_get_basic_set(list, i);
2861 if (!bset)
2862 goto error;
2863 n_eq += bset->n_eq;
2864 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2865 isl_basic_set_free(bset);
2868 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2869 if (n_ineq > 0 && !ineq)
2870 goto error;
2872 dim = isl_set_dim(set, isl_dim_set);
2873 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2874 mat = collect_inequalities(mat, list, ineq);
2875 if (!mat)
2876 goto error;
2878 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2879 goto error;
2881 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2883 isl_mat_free(mat);
2884 free(ineq);
2885 isl_basic_set_list_free(list);
2886 return hull;
2887 error:
2888 isl_mat_free(mat);
2889 free(ineq);
2890 isl_set_free(set);
2891 isl_basic_set_list_free(list);
2892 return NULL;
2895 /* Compute a superset of the convex hull of "map" that is described
2896 * by only constraints in the elements of "list".
2898 * If the list is empty, then we can only describe the universe set.
2899 * If the input map is empty, then all constraints are valid, so
2900 * we return the intersection of the elements in "list".
2902 * Otherwise, we align all divs and temporarily treat them
2903 * as regular variables, computing the unshifted simple hull in
2904 * uset_unshifted_simple_hull_from_basic_set_list.
2906 static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2907 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2909 isl_basic_map *model;
2910 isl_basic_map *hull;
2911 isl_set *set;
2912 isl_basic_set_list *bset_list;
2914 if (!map || !list)
2915 goto error;
2917 if (isl_basic_map_list_n_basic_map(list) == 0) {
2918 isl_space *space;
2920 space = isl_map_get_space(map);
2921 isl_map_free(map);
2922 isl_basic_map_list_free(list);
2923 return isl_basic_map_universe(space);
2925 if (isl_map_plain_is_empty(map)) {
2926 isl_map_free(map);
2927 return isl_basic_map_list_intersect(list);
2930 map = isl_map_align_divs_to_basic_map_list(map, list);
2931 if (!map)
2932 goto error;
2933 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2935 model = isl_basic_map_list_get_basic_map(list, 0);
2937 set = isl_map_underlying_set(map);
2938 bset_list = isl_basic_map_list_underlying_set(list);
2940 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2941 hull = isl_basic_map_overlying_set(hull, model);
2943 return hull;
2944 error:
2945 isl_map_free(map);
2946 isl_basic_map_list_free(list);
2947 return NULL;
2950 /* Return a sequence of the basic maps that make up the maps in "list".
2952 static __isl_give isl_basic_map_list *collect_basic_maps(
2953 __isl_take isl_map_list *list)
2955 int i, n;
2956 isl_ctx *ctx;
2957 isl_basic_map_list *bmap_list;
2959 if (!list)
2960 return NULL;
2961 n = isl_map_list_n_map(list);
2962 ctx = isl_map_list_get_ctx(list);
2963 bmap_list = isl_basic_map_list_alloc(ctx, 0);
2965 for (i = 0; i < n; ++i) {
2966 isl_map *map;
2967 isl_basic_map_list *list_i;
2969 map = isl_map_list_get_map(list, i);
2970 map = isl_map_compute_divs(map);
2971 list_i = isl_map_get_basic_map_list(map);
2972 isl_map_free(map);
2973 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
2976 isl_map_list_free(list);
2977 return bmap_list;
2980 /* Compute a superset of the convex hull of "map" that is described
2981 * by only constraints in the elements of "list".
2983 * If "map" is the universe, then the convex hull (and therefore
2984 * any superset of the convexhull) is the universe as well.
2986 * Otherwise, we collect all the basic maps in the map list and
2987 * continue with map_unshifted_simple_hull_from_basic_map_list.
2989 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
2990 __isl_take isl_map *map, __isl_take isl_map_list *list)
2992 isl_basic_map_list *bmap_list;
2993 int is_universe;
2995 is_universe = isl_map_plain_is_universe(map);
2996 if (is_universe < 0)
2997 map = isl_map_free(map);
2998 if (is_universe < 0 || is_universe) {
2999 isl_map_list_free(list);
3000 return isl_map_unshifted_simple_hull(map);
3003 bmap_list = collect_basic_maps(list);
3004 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
3007 /* Compute a superset of the convex hull of "set" that is described
3008 * by only constraints in the elements of "list".
3010 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
3011 __isl_take isl_set *set, __isl_take isl_set_list *list)
3013 return isl_map_unshifted_simple_hull_from_map_list(set, list);
3016 /* Given a set "set", return parametric bounds on the dimension "dim".
3018 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
3020 unsigned set_dim = isl_set_dim(set, isl_dim_set);
3021 set = isl_set_copy(set);
3022 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
3023 set = isl_set_eliminate_dims(set, 0, dim);
3024 return isl_set_convex_hull(set);
3027 /* Computes a "simple hull" and then check if each dimension in the
3028 * resulting hull is bounded by a symbolic constant. If not, the
3029 * hull is intersected with the corresponding bounds on the whole set.
3031 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
3033 int i, j;
3034 struct isl_basic_set *hull;
3035 unsigned nparam, left;
3036 int removed_divs = 0;
3038 hull = isl_set_simple_hull(isl_set_copy(set));
3039 if (!hull)
3040 goto error;
3042 nparam = isl_basic_set_dim(hull, isl_dim_param);
3043 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
3044 int lower = 0, upper = 0;
3045 struct isl_basic_set *bounds;
3047 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
3048 for (j = 0; j < hull->n_eq; ++j) {
3049 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3050 continue;
3051 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3052 left) == -1)
3053 break;
3055 if (j < hull->n_eq)
3056 continue;
3058 for (j = 0; j < hull->n_ineq; ++j) {
3059 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3060 continue;
3061 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3062 left) != -1 ||
3063 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3064 i) != -1)
3065 continue;
3066 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3067 lower = 1;
3068 else
3069 upper = 1;
3070 if (lower && upper)
3071 break;
3074 if (lower && upper)
3075 continue;
3077 if (!removed_divs) {
3078 set = isl_set_remove_divs(set);
3079 if (!set)
3080 goto error;
3081 removed_divs = 1;
3083 bounds = set_bounds(set, i);
3084 hull = isl_basic_set_intersect(hull, bounds);
3085 if (!hull)
3086 goto error;
3089 isl_set_free(set);
3090 return hull;
3091 error:
3092 isl_set_free(set);
3093 isl_basic_set_free(hull);
3094 return NULL;