isl_convex_hull.c: extend: add memory management annotations
[isl.git] / isl_convex_hull.c
blobcc428beb5770feb5ec82c54cdd651769b8505882
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 static struct isl_basic_set *isl_basic_set_add_equality(
160 struct isl_basic_set *bset, isl_int *c)
162 int i;
163 unsigned dim;
165 if (!bset)
166 return NULL;
168 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
169 return bset;
171 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
172 isl_assert(bset->ctx, bset->n_div == 0, goto error);
173 dim = isl_basic_set_n_dim(bset);
174 bset = isl_basic_set_cow(bset);
175 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
176 i = isl_basic_set_alloc_equality(bset);
177 if (i < 0)
178 goto error;
179 isl_seq_cpy(bset->eq[i], c, 1 + dim);
180 return bset;
181 error:
182 isl_basic_set_free(bset);
183 return NULL;
186 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
188 int i;
190 set = isl_set_cow(set);
191 if (!set)
192 return NULL;
193 for (i = 0; i < set->n; ++i) {
194 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
195 if (!set->p[i])
196 goto error;
198 return set;
199 error:
200 isl_set_free(set);
201 return NULL;
204 /* Given a union of basic sets, construct the constraints for wrapping
205 * a facet around one of its ridges.
206 * In particular, if each of n the d-dimensional basic sets i in "set"
207 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
208 * and is defined by the constraints
209 * [ 1 ]
210 * A_i [ x ] >= 0
212 * then the resulting set is of dimension n*(1+d) and has as constraints
214 * [ a_i ]
215 * A_i [ x_i ] >= 0
217 * a_i >= 0
219 * \sum_i x_{i,1} = 1
221 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
223 struct isl_basic_set *lp;
224 unsigned n_eq;
225 unsigned n_ineq;
226 int i, j, k;
227 unsigned dim, lp_dim;
229 if (!set)
230 return NULL;
232 dim = 1 + isl_set_n_dim(set);
233 n_eq = 1;
234 n_ineq = set->n;
235 for (i = 0; i < set->n; ++i) {
236 n_eq += set->p[i]->n_eq;
237 n_ineq += set->p[i]->n_ineq;
239 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
240 lp = isl_basic_set_set_rational(lp);
241 if (!lp)
242 return NULL;
243 lp_dim = isl_basic_set_n_dim(lp);
244 k = isl_basic_set_alloc_equality(lp);
245 isl_int_set_si(lp->eq[k][0], -1);
246 for (i = 0; i < set->n; ++i) {
247 isl_int_set_si(lp->eq[k][1+dim*i], 0);
248 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
249 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
251 for (i = 0; i < set->n; ++i) {
252 k = isl_basic_set_alloc_inequality(lp);
253 isl_seq_clr(lp->ineq[k], 1+lp_dim);
254 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
256 for (j = 0; j < set->p[i]->n_eq; ++j) {
257 k = isl_basic_set_alloc_equality(lp);
258 isl_seq_clr(lp->eq[k], 1+dim*i);
259 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
260 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
263 for (j = 0; j < set->p[i]->n_ineq; ++j) {
264 k = isl_basic_set_alloc_inequality(lp);
265 isl_seq_clr(lp->ineq[k], 1+dim*i);
266 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
267 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
270 return lp;
273 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
274 * of that facet, compute the other facet of the convex hull that contains
275 * the ridge.
277 * We first transform the set such that the facet constraint becomes
279 * x_1 >= 0
281 * I.e., the facet lies in
283 * x_1 = 0
285 * and on that facet, the constraint that defines the ridge is
287 * x_2 >= 0
289 * (This transformation is not strictly needed, all that is needed is
290 * that the ridge contains the origin.)
292 * Since the ridge contains the origin, the cone of the convex hull
293 * will be of the form
295 * x_1 >= 0
296 * x_2 >= a x_1
298 * with this second constraint defining the new facet.
299 * The constant a is obtained by settting x_1 in the cone of the
300 * convex hull to 1 and minimizing x_2.
301 * Now, each element in the cone of the convex hull is the sum
302 * of elements in the cones of the basic sets.
303 * If a_i is the dilation factor of basic set i, then the problem
304 * we need to solve is
306 * min \sum_i x_{i,2}
307 * st
308 * \sum_i x_{i,1} = 1
309 * a_i >= 0
310 * [ a_i ]
311 * A [ x_i ] >= 0
313 * with
314 * [ 1 ]
315 * A_i [ x_i ] >= 0
317 * the constraints of each (transformed) basic set.
318 * If a = n/d, then the constraint defining the new facet (in the transformed
319 * space) is
321 * -n x_1 + d x_2 >= 0
323 * In the original space, we need to take the same combination of the
324 * corresponding constraints "facet" and "ridge".
326 * If a = -infty = "-1/0", then we just return the original facet constraint.
327 * This means that the facet is unbounded, but has a bounded intersection
328 * with the union of sets.
330 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
331 isl_int *facet, isl_int *ridge)
333 int i;
334 isl_ctx *ctx;
335 struct isl_mat *T = NULL;
336 struct isl_basic_set *lp = NULL;
337 struct isl_vec *obj;
338 enum isl_lp_result res;
339 isl_int num, den;
340 unsigned dim;
342 if (!set)
343 return NULL;
344 ctx = set->ctx;
345 set = isl_set_copy(set);
346 set = isl_set_set_rational(set);
348 dim = 1 + isl_set_n_dim(set);
349 T = isl_mat_alloc(ctx, 3, dim);
350 if (!T)
351 goto error;
352 isl_int_set_si(T->row[0][0], 1);
353 isl_seq_clr(T->row[0]+1, dim - 1);
354 isl_seq_cpy(T->row[1], facet, dim);
355 isl_seq_cpy(T->row[2], ridge, dim);
356 T = isl_mat_right_inverse(T);
357 set = isl_set_preimage(set, T);
358 T = NULL;
359 if (!set)
360 goto error;
361 lp = wrap_constraints(set);
362 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
363 if (!obj)
364 goto error;
365 isl_int_set_si(obj->block.data[0], 0);
366 for (i = 0; i < set->n; ++i) {
367 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
368 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
369 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
371 isl_int_init(num);
372 isl_int_init(den);
373 res = isl_basic_set_solve_lp(lp, 0,
374 obj->block.data, ctx->one, &num, &den, NULL);
375 if (res == isl_lp_ok) {
376 isl_int_neg(num, num);
377 isl_seq_combine(facet, num, facet, den, ridge, dim);
378 isl_seq_normalize(ctx, facet, dim);
380 isl_int_clear(num);
381 isl_int_clear(den);
382 isl_vec_free(obj);
383 isl_basic_set_free(lp);
384 isl_set_free(set);
385 if (res == isl_lp_error)
386 return NULL;
387 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
388 return NULL);
389 return facet;
390 error:
391 isl_basic_set_free(lp);
392 isl_mat_free(T);
393 isl_set_free(set);
394 return NULL;
397 /* Compute the constraint of a facet of "set".
399 * We first compute the intersection with a bounding constraint
400 * that is orthogonal to one of the coordinate axes.
401 * If the affine hull of this intersection has only one equality,
402 * we have found a facet.
403 * Otherwise, we wrap the current bounding constraint around
404 * one of the equalities of the face (one that is not equal to
405 * the current bounding constraint).
406 * This process continues until we have found a facet.
407 * The dimension of the intersection increases by at least
408 * one on each iteration, so termination is guaranteed.
410 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
412 struct isl_set *slice = NULL;
413 struct isl_basic_set *face = NULL;
414 int i;
415 unsigned dim = isl_set_n_dim(set);
416 int is_bound;
417 isl_mat *bounds = NULL;
419 isl_assert(set->ctx, set->n > 0, goto error);
420 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
421 if (!bounds)
422 return NULL;
424 isl_seq_clr(bounds->row[0], dim);
425 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
426 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
427 if (is_bound < 0)
428 goto error;
429 isl_assert(set->ctx, is_bound, goto error);
430 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
431 bounds->n_row = 1;
433 for (;;) {
434 slice = isl_set_copy(set);
435 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
436 face = isl_set_affine_hull(slice);
437 if (!face)
438 goto error;
439 if (face->n_eq == 1) {
440 isl_basic_set_free(face);
441 break;
443 for (i = 0; i < face->n_eq; ++i)
444 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
445 !isl_seq_is_neg(bounds->row[0],
446 face->eq[i], 1 + dim))
447 break;
448 isl_assert(set->ctx, i < face->n_eq, goto error);
449 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
450 goto error;
451 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
452 isl_basic_set_free(face);
455 return bounds;
456 error:
457 isl_basic_set_free(face);
458 isl_mat_free(bounds);
459 return NULL;
462 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
463 * compute a hyperplane description of the facet, i.e., compute the facets
464 * of the facet.
466 * We compute an affine transformation that transforms the constraint
468 * [ 1 ]
469 * c [ x ] = 0
471 * to the constraint
473 * z_1 = 0
475 * by computing the right inverse U of a matrix that starts with the rows
477 * [ 1 0 ]
478 * [ c ]
480 * Then
481 * [ 1 ] [ 1 ]
482 * [ x ] = U [ z ]
483 * and
484 * [ 1 ] [ 1 ]
485 * [ z ] = Q [ x ]
487 * with Q = U^{-1}
488 * Since z_1 is zero, we can drop this variable as well as the corresponding
489 * column of U to obtain
491 * [ 1 ] [ 1 ]
492 * [ x ] = U' [ z' ]
493 * and
494 * [ 1 ] [ 1 ]
495 * [ z' ] = Q' [ x ]
497 * with Q' equal to Q, but without the corresponding row.
498 * After computing the facets of the facet in the z' space,
499 * we convert them back to the x space through Q.
501 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
503 struct isl_mat *m, *U, *Q;
504 struct isl_basic_set *facet = NULL;
505 struct isl_ctx *ctx;
506 unsigned dim;
508 ctx = set->ctx;
509 set = isl_set_copy(set);
510 dim = isl_set_n_dim(set);
511 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
512 if (!m)
513 goto error;
514 isl_int_set_si(m->row[0][0], 1);
515 isl_seq_clr(m->row[0]+1, dim);
516 isl_seq_cpy(m->row[1], c, 1+dim);
517 U = isl_mat_right_inverse(m);
518 Q = isl_mat_right_inverse(isl_mat_copy(U));
519 U = isl_mat_drop_cols(U, 1, 1);
520 Q = isl_mat_drop_rows(Q, 1, 1);
521 set = isl_set_preimage(set, U);
522 facet = uset_convex_hull_wrap_bounded(set);
523 facet = isl_basic_set_preimage(facet, Q);
524 if (facet && facet->n_eq != 0)
525 isl_die(ctx, isl_error_internal, "unexpected equality",
526 return isl_basic_set_free(facet));
527 return facet;
528 error:
529 isl_basic_set_free(facet);
530 isl_set_free(set);
531 return NULL;
534 /* Given an initial facet constraint, compute the remaining facets.
535 * We do this by running through all facets found so far and computing
536 * the adjacent facets through wrapping, adding those facets that we
537 * hadn't already found before.
539 * For each facet we have found so far, we first compute its facets
540 * in the resulting convex hull. That is, we compute the ridges
541 * of the resulting convex hull contained in the facet.
542 * We also compute the corresponding facet in the current approximation
543 * of the convex hull. There is no need to wrap around the ridges
544 * in this facet since that would result in a facet that is already
545 * present in the current approximation.
547 * This function can still be significantly optimized by checking which of
548 * the facets of the basic sets are also facets of the convex hull and
549 * using all the facets so far to help in constructing the facets of the
550 * facets
551 * and/or
552 * using the technique in section "3.1 Ridge Generation" of
553 * "Extended Convex Hull" by Fukuda et al.
555 static __isl_give isl_basic_set *extend(__isl_take isl_basic_set *hull,
556 __isl_keep isl_set *set)
558 int i, j, f;
559 int k;
560 struct isl_basic_set *facet = NULL;
561 struct isl_basic_set *hull_facet = NULL;
562 unsigned dim;
564 if (!hull)
565 return NULL;
567 isl_assert(set->ctx, set->n > 0, goto error);
569 dim = isl_set_n_dim(set);
571 for (i = 0; i < hull->n_ineq; ++i) {
572 facet = compute_facet(set, hull->ineq[i]);
573 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
574 facet = isl_basic_set_gauss(facet, NULL);
575 facet = isl_basic_set_normalize_constraints(facet);
576 hull_facet = isl_basic_set_copy(hull);
577 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
578 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
579 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
580 if (!facet || !hull_facet)
581 goto error;
582 hull = isl_basic_set_cow(hull);
583 hull = isl_basic_set_extend_space(hull,
584 isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
585 if (!hull)
586 goto error;
587 for (j = 0; j < facet->n_ineq; ++j) {
588 for (f = 0; f < hull_facet->n_ineq; ++f)
589 if (isl_seq_eq(facet->ineq[j],
590 hull_facet->ineq[f], 1 + dim))
591 break;
592 if (f < hull_facet->n_ineq)
593 continue;
594 k = isl_basic_set_alloc_inequality(hull);
595 if (k < 0)
596 goto error;
597 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
598 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
599 goto error;
601 isl_basic_set_free(hull_facet);
602 isl_basic_set_free(facet);
604 hull = isl_basic_set_simplify(hull);
605 hull = isl_basic_set_finalize(hull);
606 return hull;
607 error:
608 isl_basic_set_free(hull_facet);
609 isl_basic_set_free(facet);
610 isl_basic_set_free(hull);
611 return NULL;
614 /* Special case for computing the convex hull of a one dimensional set.
615 * We simply collect the lower and upper bounds of each basic set
616 * and the biggest of those.
618 static __isl_give isl_basic_set *convex_hull_1d(__isl_take isl_set *set)
620 struct isl_mat *c = NULL;
621 isl_int *lower = NULL;
622 isl_int *upper = NULL;
623 int i, j, k;
624 isl_int a, b;
625 struct isl_basic_set *hull;
627 for (i = 0; i < set->n; ++i) {
628 set->p[i] = isl_basic_set_simplify(set->p[i]);
629 if (!set->p[i])
630 goto error;
632 set = isl_set_remove_empty_parts(set);
633 if (!set)
634 goto error;
635 isl_assert(set->ctx, set->n > 0, goto error);
636 c = isl_mat_alloc(set->ctx, 2, 2);
637 if (!c)
638 goto error;
640 if (set->p[0]->n_eq > 0) {
641 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
642 lower = c->row[0];
643 upper = c->row[1];
644 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
645 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
646 isl_seq_neg(upper, set->p[0]->eq[0], 2);
647 } else {
648 isl_seq_neg(lower, set->p[0]->eq[0], 2);
649 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
651 } else {
652 for (j = 0; j < set->p[0]->n_ineq; ++j) {
653 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
654 lower = c->row[0];
655 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
656 } else {
657 upper = c->row[1];
658 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
663 isl_int_init(a);
664 isl_int_init(b);
665 for (i = 0; i < set->n; ++i) {
666 struct isl_basic_set *bset = set->p[i];
667 int has_lower = 0;
668 int has_upper = 0;
670 for (j = 0; j < bset->n_eq; ++j) {
671 has_lower = 1;
672 has_upper = 1;
673 if (lower) {
674 isl_int_mul(a, lower[0], bset->eq[j][1]);
675 isl_int_mul(b, lower[1], bset->eq[j][0]);
676 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
677 isl_seq_cpy(lower, bset->eq[j], 2);
678 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
679 isl_seq_neg(lower, bset->eq[j], 2);
681 if (upper) {
682 isl_int_mul(a, upper[0], bset->eq[j][1]);
683 isl_int_mul(b, upper[1], bset->eq[j][0]);
684 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
685 isl_seq_neg(upper, bset->eq[j], 2);
686 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
687 isl_seq_cpy(upper, bset->eq[j], 2);
690 for (j = 0; j < bset->n_ineq; ++j) {
691 if (isl_int_is_pos(bset->ineq[j][1]))
692 has_lower = 1;
693 if (isl_int_is_neg(bset->ineq[j][1]))
694 has_upper = 1;
695 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
696 isl_int_mul(a, lower[0], bset->ineq[j][1]);
697 isl_int_mul(b, lower[1], bset->ineq[j][0]);
698 if (isl_int_lt(a, b))
699 isl_seq_cpy(lower, bset->ineq[j], 2);
701 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
702 isl_int_mul(a, upper[0], bset->ineq[j][1]);
703 isl_int_mul(b, upper[1], bset->ineq[j][0]);
704 if (isl_int_gt(a, b))
705 isl_seq_cpy(upper, bset->ineq[j], 2);
708 if (!has_lower)
709 lower = NULL;
710 if (!has_upper)
711 upper = NULL;
713 isl_int_clear(a);
714 isl_int_clear(b);
716 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
717 hull = isl_basic_set_set_rational(hull);
718 if (!hull)
719 goto error;
720 if (lower) {
721 k = isl_basic_set_alloc_inequality(hull);
722 isl_seq_cpy(hull->ineq[k], lower, 2);
724 if (upper) {
725 k = isl_basic_set_alloc_inequality(hull);
726 isl_seq_cpy(hull->ineq[k], upper, 2);
728 hull = isl_basic_set_finalize(hull);
729 isl_set_free(set);
730 isl_mat_free(c);
731 return hull;
732 error:
733 isl_set_free(set);
734 isl_mat_free(c);
735 return NULL;
738 static __isl_give isl_basic_set *convex_hull_0d(__isl_take isl_set *set)
740 struct isl_basic_set *convex_hull;
742 if (!set)
743 return NULL;
745 if (isl_set_is_empty(set))
746 convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
747 else
748 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
749 isl_set_free(set);
750 return convex_hull;
753 /* Compute the convex hull of a pair of basic sets without any parameters or
754 * integer divisions using Fourier-Motzkin elimination.
755 * The convex hull is the set of all points that can be written as
756 * the sum of points from both basic sets (in homogeneous coordinates).
757 * We set up the constraints in a space with dimensions for each of
758 * the three sets and then project out the dimensions corresponding
759 * to the two original basic sets, retaining only those corresponding
760 * to the convex hull.
762 static __isl_give isl_basic_set *convex_hull_pair_elim(
763 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
765 int i, j, k;
766 struct isl_basic_set *bset[2];
767 struct isl_basic_set *hull = NULL;
768 unsigned dim;
770 if (!bset1 || !bset2)
771 goto error;
773 dim = isl_basic_set_n_dim(bset1);
774 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
775 1 + dim + bset1->n_eq + bset2->n_eq,
776 2 + bset1->n_ineq + bset2->n_ineq);
777 bset[0] = bset1;
778 bset[1] = bset2;
779 for (i = 0; i < 2; ++i) {
780 for (j = 0; j < bset[i]->n_eq; ++j) {
781 k = isl_basic_set_alloc_equality(hull);
782 if (k < 0)
783 goto error;
784 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
785 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
786 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
787 1+dim);
789 for (j = 0; j < bset[i]->n_ineq; ++j) {
790 k = isl_basic_set_alloc_inequality(hull);
791 if (k < 0)
792 goto error;
793 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
794 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
795 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
796 bset[i]->ineq[j], 1+dim);
798 k = isl_basic_set_alloc_inequality(hull);
799 if (k < 0)
800 goto error;
801 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
802 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
804 for (j = 0; j < 1+dim; ++j) {
805 k = isl_basic_set_alloc_equality(hull);
806 if (k < 0)
807 goto error;
808 isl_seq_clr(hull->eq[k], 1+2+3*dim);
809 isl_int_set_si(hull->eq[k][j], -1);
810 isl_int_set_si(hull->eq[k][1+dim+j], 1);
811 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
813 hull = isl_basic_set_set_rational(hull);
814 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
815 hull = isl_basic_set_remove_redundancies(hull);
816 isl_basic_set_free(bset1);
817 isl_basic_set_free(bset2);
818 return hull;
819 error:
820 isl_basic_set_free(bset1);
821 isl_basic_set_free(bset2);
822 isl_basic_set_free(hull);
823 return NULL;
826 /* Is the set bounded for each value of the parameters?
828 isl_bool isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
830 struct isl_tab *tab;
831 isl_bool bounded;
833 if (!bset)
834 return isl_bool_error;
835 if (isl_basic_set_plain_is_empty(bset))
836 return isl_bool_true;
838 tab = isl_tab_from_recession_cone(bset, 1);
839 bounded = isl_tab_cone_is_bounded(tab);
840 isl_tab_free(tab);
841 return bounded;
844 /* Is the image bounded for each value of the parameters and
845 * the domain variables?
847 isl_bool isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
849 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
850 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
851 isl_bool bounded;
853 bmap = isl_basic_map_copy(bmap);
854 bmap = isl_basic_map_cow(bmap);
855 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
856 isl_dim_in, 0, n_in);
857 bounded = isl_basic_set_is_bounded(bset_from_bmap(bmap));
858 isl_basic_map_free(bmap);
860 return bounded;
863 /* Is the set bounded for each value of the parameters?
865 isl_bool isl_set_is_bounded(__isl_keep isl_set *set)
867 int i;
869 if (!set)
870 return isl_bool_error;
872 for (i = 0; i < set->n; ++i) {
873 isl_bool bounded = isl_basic_set_is_bounded(set->p[i]);
874 if (!bounded || bounded < 0)
875 return bounded;
877 return isl_bool_true;
880 /* Compute the lineality space of the convex hull of bset1 and bset2.
882 * We first compute the intersection of the recession cone of bset1
883 * with the negative of the recession cone of bset2 and then compute
884 * the linear hull of the resulting cone.
886 static __isl_give isl_basic_set *induced_lineality_space(
887 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
889 int i, k;
890 struct isl_basic_set *lin = NULL;
891 unsigned dim;
893 if (!bset1 || !bset2)
894 goto error;
896 dim = isl_basic_set_total_dim(bset1);
897 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
898 bset1->n_eq + bset2->n_eq,
899 bset1->n_ineq + bset2->n_ineq);
900 lin = isl_basic_set_set_rational(lin);
901 if (!lin)
902 goto error;
903 for (i = 0; i < bset1->n_eq; ++i) {
904 k = isl_basic_set_alloc_equality(lin);
905 if (k < 0)
906 goto error;
907 isl_int_set_si(lin->eq[k][0], 0);
908 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
910 for (i = 0; i < bset1->n_ineq; ++i) {
911 k = isl_basic_set_alloc_inequality(lin);
912 if (k < 0)
913 goto error;
914 isl_int_set_si(lin->ineq[k][0], 0);
915 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
917 for (i = 0; i < bset2->n_eq; ++i) {
918 k = isl_basic_set_alloc_equality(lin);
919 if (k < 0)
920 goto error;
921 isl_int_set_si(lin->eq[k][0], 0);
922 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
924 for (i = 0; i < bset2->n_ineq; ++i) {
925 k = isl_basic_set_alloc_inequality(lin);
926 if (k < 0)
927 goto error;
928 isl_int_set_si(lin->ineq[k][0], 0);
929 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
932 isl_basic_set_free(bset1);
933 isl_basic_set_free(bset2);
934 return isl_basic_set_affine_hull(lin);
935 error:
936 isl_basic_set_free(lin);
937 isl_basic_set_free(bset1);
938 isl_basic_set_free(bset2);
939 return NULL;
942 static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set);
944 /* Given a set and a linear space "lin" of dimension n > 0,
945 * project the linear space from the set, compute the convex hull
946 * and then map the set back to the original space.
948 * Let
950 * M x = 0
952 * describe the linear space. We first compute the Hermite normal
953 * form H = M U of M = H Q, to obtain
955 * H Q x = 0
957 * The last n rows of H will be zero, so the last n variables of x' = Q x
958 * are the one we want to project out. We do this by transforming each
959 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
960 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
961 * we transform the hull back to the original space as A' Q_1 x >= b',
962 * with Q_1 all but the last n rows of Q.
964 static __isl_give isl_basic_set *modulo_lineality(__isl_take isl_set *set,
965 __isl_take isl_basic_set *lin)
967 unsigned total = isl_basic_set_total_dim(lin);
968 unsigned lin_dim;
969 struct isl_basic_set *hull;
970 struct isl_mat *M, *U, *Q;
972 if (!set || !lin)
973 goto error;
974 lin_dim = total - lin->n_eq;
975 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
976 M = isl_mat_left_hermite(M, 0, &U, &Q);
977 if (!M)
978 goto error;
979 isl_mat_free(M);
980 isl_basic_set_free(lin);
982 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
984 U = isl_mat_lin_to_aff(U);
985 Q = isl_mat_lin_to_aff(Q);
987 set = isl_set_preimage(set, U);
988 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
989 hull = uset_convex_hull(set);
990 hull = isl_basic_set_preimage(hull, Q);
992 return hull;
993 error:
994 isl_basic_set_free(lin);
995 isl_set_free(set);
996 return NULL;
999 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1000 * set up an LP for solving
1002 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1004 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1005 * The next \alpha{ij} correspond to the equalities and come in pairs.
1006 * The final \alpha{ij} correspond to the inequalities.
1008 static __isl_give isl_basic_set *valid_direction_lp(
1009 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1011 isl_space *dim;
1012 struct isl_basic_set *lp;
1013 unsigned d;
1014 int n;
1015 int i, j, k;
1017 if (!bset1 || !bset2)
1018 goto error;
1019 d = 1 + isl_basic_set_total_dim(bset1);
1020 n = 2 +
1021 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1022 dim = isl_space_set_alloc(bset1->ctx, 0, n);
1023 lp = isl_basic_set_alloc_space(dim, 0, d, n);
1024 if (!lp)
1025 goto error;
1026 for (i = 0; i < n; ++i) {
1027 k = isl_basic_set_alloc_inequality(lp);
1028 if (k < 0)
1029 goto error;
1030 isl_seq_clr(lp->ineq[k] + 1, n);
1031 isl_int_set_si(lp->ineq[k][0], -1);
1032 isl_int_set_si(lp->ineq[k][1 + i], 1);
1034 for (i = 0; i < d; ++i) {
1035 k = isl_basic_set_alloc_equality(lp);
1036 if (k < 0)
1037 goto error;
1038 n = 0;
1039 isl_int_set_si(lp->eq[k][n], 0); n++;
1040 /* positivity constraint 1 >= 0 */
1041 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1042 for (j = 0; j < bset1->n_eq; ++j) {
1043 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1044 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1046 for (j = 0; j < bset1->n_ineq; ++j) {
1047 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1049 /* positivity constraint 1 >= 0 */
1050 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1051 for (j = 0; j < bset2->n_eq; ++j) {
1052 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1053 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1055 for (j = 0; j < bset2->n_ineq; ++j) {
1056 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1059 lp = isl_basic_set_gauss(lp, NULL);
1060 isl_basic_set_free(bset1);
1061 isl_basic_set_free(bset2);
1062 return lp;
1063 error:
1064 isl_basic_set_free(bset1);
1065 isl_basic_set_free(bset2);
1066 return NULL;
1069 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1070 * for all rays in the homogeneous space of the two cones that correspond
1071 * to the input polyhedra bset1 and bset2.
1073 * We compute s as a vector that satisfies
1075 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1077 * with h_{ij} the normals of the facets of polyhedron i
1078 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1079 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1080 * We first set up an LP with as variables the \alpha{ij}.
1081 * In this formulation, for each polyhedron i,
1082 * the first constraint is the positivity constraint, followed by pairs
1083 * of variables for the equalities, followed by variables for the inequalities.
1084 * We then simply pick a feasible solution and compute s using (*).
1086 * Note that we simply pick any valid direction and make no attempt
1087 * to pick a "good" or even the "best" valid direction.
1089 static __isl_give isl_vec *valid_direction(
1090 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1092 struct isl_basic_set *lp;
1093 struct isl_tab *tab;
1094 struct isl_vec *sample = NULL;
1095 struct isl_vec *dir;
1096 unsigned d;
1097 int i;
1098 int n;
1100 if (!bset1 || !bset2)
1101 goto error;
1102 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1103 isl_basic_set_copy(bset2));
1104 tab = isl_tab_from_basic_set(lp, 0);
1105 sample = isl_tab_get_sample_value(tab);
1106 isl_tab_free(tab);
1107 isl_basic_set_free(lp);
1108 if (!sample)
1109 goto error;
1110 d = isl_basic_set_total_dim(bset1);
1111 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1112 if (!dir)
1113 goto error;
1114 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1115 n = 1;
1116 /* positivity constraint 1 >= 0 */
1117 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1118 for (i = 0; i < bset1->n_eq; ++i) {
1119 isl_int_sub(sample->block.data[n],
1120 sample->block.data[n], sample->block.data[n+1]);
1121 isl_seq_combine(dir->block.data,
1122 bset1->ctx->one, dir->block.data,
1123 sample->block.data[n], bset1->eq[i], 1 + d);
1125 n += 2;
1127 for (i = 0; i < bset1->n_ineq; ++i)
1128 isl_seq_combine(dir->block.data,
1129 bset1->ctx->one, dir->block.data,
1130 sample->block.data[n++], bset1->ineq[i], 1 + d);
1131 isl_vec_free(sample);
1132 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1133 isl_basic_set_free(bset1);
1134 isl_basic_set_free(bset2);
1135 return dir;
1136 error:
1137 isl_vec_free(sample);
1138 isl_basic_set_free(bset1);
1139 isl_basic_set_free(bset2);
1140 return NULL;
1143 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1144 * compute b_i' + A_i' x' >= 0, with
1146 * [ b_i A_i ] [ y' ] [ y' ]
1147 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1149 * In particular, add the "positivity constraint" and then perform
1150 * the mapping.
1152 static __isl_give isl_basic_set *homogeneous_map(__isl_take isl_basic_set *bset,
1153 __isl_take isl_mat *T)
1155 int k;
1157 if (!bset)
1158 goto error;
1159 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1160 k = isl_basic_set_alloc_inequality(bset);
1161 if (k < 0)
1162 goto error;
1163 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1164 isl_int_set_si(bset->ineq[k][0], 1);
1165 bset = isl_basic_set_preimage(bset, T);
1166 return bset;
1167 error:
1168 isl_mat_free(T);
1169 isl_basic_set_free(bset);
1170 return NULL;
1173 /* Compute the convex hull of a pair of basic sets without any parameters or
1174 * integer divisions, where the convex hull is known to be pointed,
1175 * but the basic sets may be unbounded.
1177 * We turn this problem into the computation of a convex hull of a pair
1178 * _bounded_ polyhedra by "changing the direction of the homogeneous
1179 * dimension". This idea is due to Matthias Koeppe.
1181 * Consider the cones in homogeneous space that correspond to the
1182 * input polyhedra. The rays of these cones are also rays of the
1183 * polyhedra if the coordinate that corresponds to the homogeneous
1184 * dimension is zero. That is, if the inner product of the rays
1185 * with the homogeneous direction is zero.
1186 * The cones in the homogeneous space can also be considered to
1187 * correspond to other pairs of polyhedra by chosing a different
1188 * homogeneous direction. To ensure that both of these polyhedra
1189 * are bounded, we need to make sure that all rays of the cones
1190 * correspond to vertices and not to rays.
1191 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1192 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1193 * The vector s is computed in valid_direction.
1195 * Note that we need to consider _all_ rays of the cones and not just
1196 * the rays that correspond to rays in the polyhedra. If we were to
1197 * only consider those rays and turn them into vertices, then we
1198 * may inadvertently turn some vertices into rays.
1200 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1201 * We therefore transform the two polyhedra such that the selected
1202 * direction is mapped onto this standard direction and then proceed
1203 * with the normal computation.
1204 * Let S be a non-singular square matrix with s as its first row,
1205 * then we want to map the polyhedra to the space
1207 * [ y' ] [ y ] [ y ] [ y' ]
1208 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1210 * We take S to be the unimodular completion of s to limit the growth
1211 * of the coefficients in the following computations.
1213 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1214 * We first move to the homogeneous dimension
1216 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1217 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1219 * Then we change directoin
1221 * [ b_i A_i ] [ y' ] [ y' ]
1222 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1224 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1225 * resulting in b' + A' x' >= 0, which we then convert back
1227 * [ y ] [ y ]
1228 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1230 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1232 static __isl_give isl_basic_set *convex_hull_pair_pointed(
1233 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1235 struct isl_ctx *ctx = NULL;
1236 struct isl_vec *dir = NULL;
1237 struct isl_mat *T = NULL;
1238 struct isl_mat *T2 = NULL;
1239 struct isl_basic_set *hull;
1240 struct isl_set *set;
1242 if (!bset1 || !bset2)
1243 goto error;
1244 ctx = isl_basic_set_get_ctx(bset1);
1245 dir = valid_direction(isl_basic_set_copy(bset1),
1246 isl_basic_set_copy(bset2));
1247 if (!dir)
1248 goto error;
1249 T = isl_mat_alloc(ctx, dir->size, dir->size);
1250 if (!T)
1251 goto error;
1252 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1253 T = isl_mat_unimodular_complete(T, 1);
1254 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1256 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1257 bset2 = homogeneous_map(bset2, T2);
1258 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1259 set = isl_set_add_basic_set(set, bset1);
1260 set = isl_set_add_basic_set(set, bset2);
1261 hull = uset_convex_hull(set);
1262 hull = isl_basic_set_preimage(hull, T);
1264 isl_vec_free(dir);
1266 return hull;
1267 error:
1268 isl_vec_free(dir);
1269 isl_basic_set_free(bset1);
1270 isl_basic_set_free(bset2);
1271 return NULL;
1274 static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set);
1275 static __isl_give isl_basic_set *modulo_affine_hull(
1276 __isl_take isl_set *set, __isl_take isl_basic_set *affine_hull);
1278 /* Compute the convex hull of a pair of basic sets without any parameters or
1279 * integer divisions.
1281 * This function is called from uset_convex_hull_unbounded, which
1282 * means that the complete convex hull is unbounded. Some pairs
1283 * of basic sets may still be bounded, though.
1284 * They may even lie inside a lower dimensional space, in which
1285 * case they need to be handled inside their affine hull since
1286 * the main algorithm assumes that the result is full-dimensional.
1288 * If the convex hull of the two basic sets would have a non-trivial
1289 * lineality space, we first project out this lineality space.
1291 static __isl_give isl_basic_set *convex_hull_pair(
1292 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1294 isl_basic_set *lin, *aff;
1295 int bounded1, bounded2;
1297 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1298 return convex_hull_pair_elim(bset1, bset2);
1300 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1301 isl_basic_set_copy(bset2)));
1302 if (!aff)
1303 goto error;
1304 if (aff->n_eq != 0)
1305 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1306 isl_basic_set_free(aff);
1308 bounded1 = isl_basic_set_is_bounded(bset1);
1309 bounded2 = isl_basic_set_is_bounded(bset2);
1311 if (bounded1 < 0 || bounded2 < 0)
1312 goto error;
1314 if (bounded1 && bounded2)
1315 return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1317 if (bounded1 || bounded2)
1318 return convex_hull_pair_pointed(bset1, bset2);
1320 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1321 isl_basic_set_copy(bset2));
1322 if (!lin)
1323 goto error;
1324 if (isl_basic_set_plain_is_universe(lin)) {
1325 isl_basic_set_free(bset1);
1326 isl_basic_set_free(bset2);
1327 return lin;
1329 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1330 struct isl_set *set;
1331 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1332 set = isl_set_add_basic_set(set, bset1);
1333 set = isl_set_add_basic_set(set, bset2);
1334 return modulo_lineality(set, lin);
1336 isl_basic_set_free(lin);
1338 return convex_hull_pair_pointed(bset1, bset2);
1339 error:
1340 isl_basic_set_free(bset1);
1341 isl_basic_set_free(bset2);
1342 return NULL;
1345 /* Compute the lineality space of a basic set.
1346 * We currently do not allow the basic set to have any divs.
1347 * We basically just drop the constants and turn every inequality
1348 * into an equality.
1350 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1352 int i, k;
1353 struct isl_basic_set *lin = NULL;
1354 unsigned dim;
1356 if (!bset)
1357 goto error;
1358 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1359 dim = isl_basic_set_total_dim(bset);
1361 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, dim, 0);
1362 if (!lin)
1363 goto error;
1364 for (i = 0; i < bset->n_eq; ++i) {
1365 k = isl_basic_set_alloc_equality(lin);
1366 if (k < 0)
1367 goto error;
1368 isl_int_set_si(lin->eq[k][0], 0);
1369 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1371 lin = isl_basic_set_gauss(lin, NULL);
1372 if (!lin)
1373 goto error;
1374 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1375 k = isl_basic_set_alloc_equality(lin);
1376 if (k < 0)
1377 goto error;
1378 isl_int_set_si(lin->eq[k][0], 0);
1379 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1380 lin = isl_basic_set_gauss(lin, NULL);
1381 if (!lin)
1382 goto error;
1384 isl_basic_set_free(bset);
1385 return lin;
1386 error:
1387 isl_basic_set_free(lin);
1388 isl_basic_set_free(bset);
1389 return NULL;
1392 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1393 * "underlying" set "set".
1395 static __isl_give isl_basic_set *uset_combined_lineality_space(
1396 __isl_take isl_set *set)
1398 int i;
1399 struct isl_set *lin = NULL;
1401 if (!set)
1402 return NULL;
1403 if (set->n == 0) {
1404 isl_space *space = isl_set_get_space(set);
1405 isl_set_free(set);
1406 return isl_basic_set_empty(space);
1409 lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
1410 for (i = 0; i < set->n; ++i)
1411 lin = isl_set_add_basic_set(lin,
1412 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1413 isl_set_free(set);
1414 return isl_set_affine_hull(lin);
1417 /* Compute the convex hull of a set without any parameters or
1418 * integer divisions.
1419 * In each step, we combined two basic sets until only one
1420 * basic set is left.
1421 * The input basic sets are assumed not to have a non-trivial
1422 * lineality space. If any of the intermediate results has
1423 * a non-trivial lineality space, it is projected out.
1425 static __isl_give isl_basic_set *uset_convex_hull_unbounded(
1426 __isl_take isl_set *set)
1428 isl_basic_set_list *list;
1430 list = isl_set_get_basic_set_list(set);
1431 isl_set_free(set);
1433 while (list) {
1434 int n;
1435 struct isl_basic_set *t;
1436 isl_basic_set *bset1, *bset2;
1438 n = isl_basic_set_list_n_basic_set(list);
1439 if (n < 2)
1440 isl_die(isl_basic_set_list_get_ctx(list),
1441 isl_error_internal,
1442 "expecting at least two elements", goto error);
1443 bset1 = isl_basic_set_list_get_basic_set(list, n - 1);
1444 bset2 = isl_basic_set_list_get_basic_set(list, n - 2);
1445 bset1 = convex_hull_pair(bset1, bset2);
1446 if (n == 2) {
1447 isl_basic_set_list_free(list);
1448 return bset1;
1450 bset1 = isl_basic_set_underlying_set(bset1);
1451 list = isl_basic_set_list_drop(list, n - 2, 2);
1452 list = isl_basic_set_list_add(list, bset1);
1454 t = isl_basic_set_list_get_basic_set(list, n - 2);
1455 t = isl_basic_set_lineality_space(t);
1456 if (!t)
1457 goto error;
1458 if (isl_basic_set_plain_is_universe(t)) {
1459 isl_basic_set_list_free(list);
1460 return t;
1462 if (t->n_eq < isl_basic_set_total_dim(t)) {
1463 set = isl_basic_set_list_union(list);
1464 return modulo_lineality(set, t);
1466 isl_basic_set_free(t);
1469 return NULL;
1470 error:
1471 isl_basic_set_list_free(list);
1472 return NULL;
1475 /* Compute an initial hull for wrapping containing a single initial
1476 * facet.
1477 * This function assumes that the given set is bounded.
1479 static __isl_give isl_basic_set *initial_hull(__isl_take isl_basic_set *hull,
1480 __isl_keep isl_set *set)
1482 struct isl_mat *bounds = NULL;
1483 unsigned dim;
1484 int k;
1486 if (!hull)
1487 goto error;
1488 bounds = initial_facet_constraint(set);
1489 if (!bounds)
1490 goto error;
1491 k = isl_basic_set_alloc_inequality(hull);
1492 if (k < 0)
1493 goto error;
1494 dim = isl_set_n_dim(set);
1495 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1496 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1497 isl_mat_free(bounds);
1499 return hull;
1500 error:
1501 isl_basic_set_free(hull);
1502 isl_mat_free(bounds);
1503 return NULL;
1506 struct max_constraint {
1507 struct isl_mat *c;
1508 int count;
1509 int ineq;
1512 static int max_constraint_equal(const void *entry, const void *val)
1514 struct max_constraint *a = (struct max_constraint *)entry;
1515 isl_int *b = (isl_int *)val;
1517 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1520 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1521 isl_int *con, unsigned len, int n, int ineq)
1523 struct isl_hash_table_entry *entry;
1524 struct max_constraint *c;
1525 uint32_t c_hash;
1527 c_hash = isl_seq_get_hash(con + 1, len);
1528 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1529 con + 1, 0);
1530 if (!entry)
1531 return;
1532 c = entry->data;
1533 if (c->count < n) {
1534 isl_hash_table_remove(ctx, table, entry);
1535 return;
1537 c->count++;
1538 if (isl_int_gt(c->c->row[0][0], con[0]))
1539 return;
1540 if (isl_int_eq(c->c->row[0][0], con[0])) {
1541 if (ineq)
1542 c->ineq = ineq;
1543 return;
1545 c->c = isl_mat_cow(c->c);
1546 isl_int_set(c->c->row[0][0], con[0]);
1547 c->ineq = ineq;
1550 /* Check whether the constraint hash table "table" constains the constraint
1551 * "con".
1553 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1554 isl_int *con, unsigned len, int n)
1556 struct isl_hash_table_entry *entry;
1557 struct max_constraint *c;
1558 uint32_t c_hash;
1560 c_hash = isl_seq_get_hash(con + 1, len);
1561 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1562 con + 1, 0);
1563 if (!entry)
1564 return 0;
1565 c = entry->data;
1566 if (c->count < n)
1567 return 0;
1568 return isl_int_eq(c->c->row[0][0], con[0]);
1571 /* Check for inequality constraints of a basic set without equalities
1572 * such that the same or more stringent copies of the constraint appear
1573 * in all of the basic sets. Such constraints are necessarily facet
1574 * constraints of the convex hull.
1576 * If the resulting basic set is by chance identical to one of
1577 * the basic sets in "set", then we know that this basic set contains
1578 * all other basic sets and is therefore the convex hull of set.
1579 * In this case we set *is_hull to 1.
1581 static __isl_give isl_basic_set *common_constraints(
1582 __isl_take isl_basic_set *hull, __isl_keep isl_set *set, int *is_hull)
1584 int i, j, s, n;
1585 int min_constraints;
1586 int best;
1587 struct max_constraint *constraints = NULL;
1588 struct isl_hash_table *table = NULL;
1589 unsigned total;
1591 *is_hull = 0;
1593 for (i = 0; i < set->n; ++i)
1594 if (set->p[i]->n_eq == 0)
1595 break;
1596 if (i >= set->n)
1597 return hull;
1598 min_constraints = set->p[i]->n_ineq;
1599 best = i;
1600 for (i = best + 1; i < set->n; ++i) {
1601 if (set->p[i]->n_eq != 0)
1602 continue;
1603 if (set->p[i]->n_ineq >= min_constraints)
1604 continue;
1605 min_constraints = set->p[i]->n_ineq;
1606 best = i;
1608 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1609 min_constraints);
1610 if (!constraints)
1611 return hull;
1612 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1613 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1614 goto error;
1616 total = isl_space_dim(set->dim, isl_dim_all);
1617 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1618 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1619 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1620 if (!constraints[i].c)
1621 goto error;
1622 constraints[i].ineq = 1;
1624 for (i = 0; i < min_constraints; ++i) {
1625 struct isl_hash_table_entry *entry;
1626 uint32_t c_hash;
1627 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1628 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1629 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1630 if (!entry)
1631 goto error;
1632 isl_assert(hull->ctx, !entry->data, goto error);
1633 entry->data = &constraints[i];
1636 n = 0;
1637 for (s = 0; s < set->n; ++s) {
1638 if (s == best)
1639 continue;
1641 for (i = 0; i < set->p[s]->n_eq; ++i) {
1642 isl_int *eq = set->p[s]->eq[i];
1643 for (j = 0; j < 2; ++j) {
1644 isl_seq_neg(eq, eq, 1 + total);
1645 update_constraint(hull->ctx, table,
1646 eq, total, n, 0);
1649 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1650 isl_int *ineq = set->p[s]->ineq[i];
1651 update_constraint(hull->ctx, table, ineq, total, n,
1652 set->p[s]->n_eq == 0);
1654 ++n;
1657 for (i = 0; i < min_constraints; ++i) {
1658 if (constraints[i].count < n)
1659 continue;
1660 if (!constraints[i].ineq)
1661 continue;
1662 j = isl_basic_set_alloc_inequality(hull);
1663 if (j < 0)
1664 goto error;
1665 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1668 for (s = 0; s < set->n; ++s) {
1669 if (set->p[s]->n_eq)
1670 continue;
1671 if (set->p[s]->n_ineq != hull->n_ineq)
1672 continue;
1673 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1674 isl_int *ineq = set->p[s]->ineq[i];
1675 if (!has_constraint(hull->ctx, table, ineq, total, n))
1676 break;
1678 if (i == set->p[s]->n_ineq)
1679 *is_hull = 1;
1682 isl_hash_table_clear(table);
1683 for (i = 0; i < min_constraints; ++i)
1684 isl_mat_free(constraints[i].c);
1685 free(constraints);
1686 free(table);
1687 return hull;
1688 error:
1689 isl_hash_table_clear(table);
1690 free(table);
1691 if (constraints)
1692 for (i = 0; i < min_constraints; ++i)
1693 isl_mat_free(constraints[i].c);
1694 free(constraints);
1695 return hull;
1698 /* Create a template for the convex hull of "set" and fill it up
1699 * obvious facet constraints, if any. If the result happens to
1700 * be the convex hull of "set" then *is_hull is set to 1.
1702 static __isl_give isl_basic_set *proto_hull(__isl_keep isl_set *set,
1703 int *is_hull)
1705 struct isl_basic_set *hull;
1706 unsigned n_ineq;
1707 int i;
1709 n_ineq = 1;
1710 for (i = 0; i < set->n; ++i) {
1711 n_ineq += set->p[i]->n_eq;
1712 n_ineq += set->p[i]->n_ineq;
1714 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1715 hull = isl_basic_set_set_rational(hull);
1716 if (!hull)
1717 return NULL;
1718 return common_constraints(hull, set, is_hull);
1721 static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set)
1723 struct isl_basic_set *hull;
1724 int is_hull;
1726 hull = proto_hull(set, &is_hull);
1727 if (hull && !is_hull) {
1728 if (hull->n_ineq == 0)
1729 hull = initial_hull(hull, set);
1730 hull = extend(hull, set);
1732 isl_set_free(set);
1734 return hull;
1737 /* Compute the convex hull of a set without any parameters or
1738 * integer divisions. Depending on whether the set is bounded,
1739 * we pass control to the wrapping based convex hull or
1740 * the Fourier-Motzkin elimination based convex hull.
1741 * We also handle a few special cases before checking the boundedness.
1743 static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set)
1745 isl_bool bounded;
1746 struct isl_basic_set *convex_hull = NULL;
1747 struct isl_basic_set *lin;
1749 if (isl_set_n_dim(set) == 0)
1750 return convex_hull_0d(set);
1752 set = isl_set_coalesce(set);
1753 set = isl_set_set_rational(set);
1755 if (!set)
1756 return NULL;
1757 if (set->n == 1) {
1758 convex_hull = isl_basic_set_copy(set->p[0]);
1759 isl_set_free(set);
1760 return convex_hull;
1762 if (isl_set_n_dim(set) == 1)
1763 return convex_hull_1d(set);
1765 bounded = isl_set_is_bounded(set);
1766 if (bounded < 0)
1767 goto error;
1768 if (bounded && set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1769 return uset_convex_hull_wrap(set);
1771 lin = uset_combined_lineality_space(isl_set_copy(set));
1772 if (!lin)
1773 goto error;
1774 if (isl_basic_set_plain_is_universe(lin)) {
1775 isl_set_free(set);
1776 return lin;
1778 if (lin->n_eq < isl_basic_set_total_dim(lin))
1779 return modulo_lineality(set, lin);
1780 isl_basic_set_free(lin);
1782 return uset_convex_hull_unbounded(set);
1783 error:
1784 isl_set_free(set);
1785 isl_basic_set_free(convex_hull);
1786 return NULL;
1789 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1790 * without parameters or divs and where the convex hull of set is
1791 * known to be full-dimensional.
1793 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1795 struct isl_basic_set *convex_hull = NULL;
1797 if (!set)
1798 goto error;
1800 if (isl_set_n_dim(set) == 0) {
1801 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1802 isl_set_free(set);
1803 convex_hull = isl_basic_set_set_rational(convex_hull);
1804 return convex_hull;
1807 set = isl_set_set_rational(set);
1808 set = isl_set_coalesce(set);
1809 if (!set)
1810 goto error;
1811 if (set->n == 1) {
1812 convex_hull = isl_basic_set_copy(set->p[0]);
1813 isl_set_free(set);
1814 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1815 return convex_hull;
1817 if (isl_set_n_dim(set) == 1)
1818 return convex_hull_1d(set);
1820 return uset_convex_hull_wrap(set);
1821 error:
1822 isl_set_free(set);
1823 return NULL;
1826 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1827 * We first remove the equalities (transforming the set), compute the
1828 * convex hull of the transformed set and then add the equalities back
1829 * (after performing the inverse transformation.
1831 static __isl_give isl_basic_set *modulo_affine_hull(
1832 __isl_take isl_set *set, __isl_take isl_basic_set *affine_hull)
1834 struct isl_mat *T;
1835 struct isl_mat *T2;
1836 struct isl_basic_set *dummy;
1837 struct isl_basic_set *convex_hull;
1839 dummy = isl_basic_set_remove_equalities(
1840 isl_basic_set_copy(affine_hull), &T, &T2);
1841 if (!dummy)
1842 goto error;
1843 isl_basic_set_free(dummy);
1844 set = isl_set_preimage(set, T);
1845 convex_hull = uset_convex_hull(set);
1846 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1847 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1848 return convex_hull;
1849 error:
1850 isl_basic_set_free(affine_hull);
1851 isl_set_free(set);
1852 return NULL;
1855 /* Return an empty basic map living in the same space as "map".
1857 static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1858 __isl_take isl_map *map)
1860 isl_space *space;
1862 space = isl_map_get_space(map);
1863 isl_map_free(map);
1864 return isl_basic_map_empty(space);
1867 /* Compute the convex hull of a map.
1869 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1870 * specifically, the wrapping of facets to obtain new facets.
1872 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1874 struct isl_basic_set *bset;
1875 struct isl_basic_map *model = NULL;
1876 struct isl_basic_set *affine_hull = NULL;
1877 struct isl_basic_map *convex_hull = NULL;
1878 struct isl_set *set = NULL;
1880 map = isl_map_detect_equalities(map);
1881 map = isl_map_align_divs_internal(map);
1882 if (!map)
1883 goto error;
1885 if (map->n == 0)
1886 return replace_map_by_empty_basic_map(map);
1888 model = isl_basic_map_copy(map->p[0]);
1889 set = isl_map_underlying_set(map);
1890 if (!set)
1891 goto error;
1893 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1894 if (!affine_hull)
1895 goto error;
1896 if (affine_hull->n_eq != 0)
1897 bset = modulo_affine_hull(set, affine_hull);
1898 else {
1899 isl_basic_set_free(affine_hull);
1900 bset = uset_convex_hull(set);
1903 convex_hull = isl_basic_map_overlying_set(bset, model);
1904 if (!convex_hull)
1905 return NULL;
1907 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1908 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1909 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1910 return convex_hull;
1911 error:
1912 isl_set_free(set);
1913 isl_basic_map_free(model);
1914 return NULL;
1917 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1919 return bset_from_bmap(isl_map_convex_hull(set_to_map(set)));
1922 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1924 isl_basic_map *hull;
1926 hull = isl_map_convex_hull(map);
1927 return isl_basic_map_remove_divs(hull);
1930 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1932 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set)));
1935 struct sh_data_entry {
1936 struct isl_hash_table *table;
1937 struct isl_tab *tab;
1940 /* Holds the data needed during the simple hull computation.
1941 * In particular,
1942 * n the number of basic sets in the original set
1943 * hull_table a hash table of already computed constraints
1944 * in the simple hull
1945 * p for each basic set,
1946 * table a hash table of the constraints
1947 * tab the tableau corresponding to the basic set
1949 struct sh_data {
1950 struct isl_ctx *ctx;
1951 unsigned n;
1952 struct isl_hash_table *hull_table;
1953 struct sh_data_entry p[1];
1956 static void sh_data_free(struct sh_data *data)
1958 int i;
1960 if (!data)
1961 return;
1962 isl_hash_table_free(data->ctx, data->hull_table);
1963 for (i = 0; i < data->n; ++i) {
1964 isl_hash_table_free(data->ctx, data->p[i].table);
1965 isl_tab_free(data->p[i].tab);
1967 free(data);
1970 struct ineq_cmp_data {
1971 unsigned len;
1972 isl_int *p;
1975 static int has_ineq(const void *entry, const void *val)
1977 isl_int *row = (isl_int *)entry;
1978 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
1980 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
1981 isl_seq_is_neg(row + 1, v->p + 1, v->len);
1984 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
1985 isl_int *ineq, unsigned len)
1987 uint32_t c_hash;
1988 struct ineq_cmp_data v;
1989 struct isl_hash_table_entry *entry;
1991 v.len = len;
1992 v.p = ineq;
1993 c_hash = isl_seq_get_hash(ineq + 1, len);
1994 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
1995 if (!entry)
1996 return - 1;
1997 entry->data = ineq;
1998 return 0;
2001 /* Fill hash table "table" with the constraints of "bset".
2002 * Equalities are added as two inequalities.
2003 * The value in the hash table is a pointer to the (in)equality of "bset".
2005 static int hash_basic_set(struct isl_hash_table *table,
2006 struct isl_basic_set *bset)
2008 int i, j;
2009 unsigned dim = isl_basic_set_total_dim(bset);
2011 for (i = 0; i < bset->n_eq; ++i) {
2012 for (j = 0; j < 2; ++j) {
2013 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2014 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2015 return -1;
2018 for (i = 0; i < bset->n_ineq; ++i) {
2019 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2020 return -1;
2022 return 0;
2025 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2027 struct sh_data *data;
2028 int i;
2030 data = isl_calloc(set->ctx, struct sh_data,
2031 sizeof(struct sh_data) +
2032 (set->n - 1) * sizeof(struct sh_data_entry));
2033 if (!data)
2034 return NULL;
2035 data->ctx = set->ctx;
2036 data->n = set->n;
2037 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2038 if (!data->hull_table)
2039 goto error;
2040 for (i = 0; i < set->n; ++i) {
2041 data->p[i].table = isl_hash_table_alloc(set->ctx,
2042 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2043 if (!data->p[i].table)
2044 goto error;
2045 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2046 goto error;
2048 return data;
2049 error:
2050 sh_data_free(data);
2051 return NULL;
2054 /* Check if inequality "ineq" is a bound for basic set "j" or if
2055 * it can be relaxed (by increasing the constant term) to become
2056 * a bound for that basic set. In the latter case, the constant
2057 * term is updated.
2058 * Relaxation of the constant term is only allowed if "shift" is set.
2060 * Return 1 if "ineq" is a bound
2061 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2062 * -1 if some error occurred
2064 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2065 isl_int *ineq, int shift)
2067 enum isl_lp_result res;
2068 isl_int opt;
2070 if (!data->p[j].tab) {
2071 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2072 if (!data->p[j].tab)
2073 return -1;
2076 isl_int_init(opt);
2078 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2079 &opt, NULL, 0);
2080 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2081 if (shift)
2082 isl_int_sub(ineq[0], ineq[0], opt);
2083 else
2084 res = isl_lp_unbounded;
2087 isl_int_clear(opt);
2089 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2090 res == isl_lp_unbounded ? 0 : -1;
2093 /* Set the constant term of "ineq" to the maximum of those of the constraints
2094 * in the basic sets of "set" following "i" that are parallel to "ineq".
2095 * That is, if any of the basic sets of "set" following "i" have a more
2096 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2097 * "c_hash" is the hash value of the linear part of "ineq".
2098 * "v" has been set up for use by has_ineq.
2100 * Note that the two inequality constraints corresponding to an equality are
2101 * represented by the same inequality constraint in data->p[j].table
2102 * (but with different hash values). This means the constraint (or at
2103 * least its constant term) may need to be temporarily negated to get
2104 * the actually hashed constraint.
2106 static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
2107 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2109 int j;
2110 isl_ctx *ctx;
2111 struct isl_hash_table_entry *entry;
2113 ctx = isl_set_get_ctx(set);
2114 for (j = i + 1; j < set->n; ++j) {
2115 int neg;
2116 isl_int *ineq_j;
2118 entry = isl_hash_table_find(ctx, data->p[j].table,
2119 c_hash, &has_ineq, v, 0);
2120 if (!entry)
2121 continue;
2123 ineq_j = entry->data;
2124 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2125 if (neg)
2126 isl_int_neg(ineq_j[0], ineq_j[0]);
2127 if (isl_int_gt(ineq_j[0], ineq[0]))
2128 isl_int_set(ineq[0], ineq_j[0]);
2129 if (neg)
2130 isl_int_neg(ineq_j[0], ineq_j[0]);
2134 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2135 * become a bound on the whole set. If so, add the (relaxed) inequality
2136 * to "hull". Relaxation is only allowed if "shift" is set.
2138 * We first check if "hull" already contains a translate of the inequality.
2139 * If so, we are done.
2140 * Then, we check if any of the previous basic sets contains a translate
2141 * of the inequality. If so, then we have already considered this
2142 * inequality and we are done.
2143 * Otherwise, for each basic set other than "i", we check if the inequality
2144 * is a bound on the basic set, but first replace the constant term
2145 * by the maximal value of any translate of the inequality in any
2146 * of the following basic sets.
2147 * For previous basic sets, we know that they do not contain a translate
2148 * of the inequality, so we directly call is_bound.
2149 * For following basic sets, we first check if a translate of the
2150 * inequality appears in its description. If so, the constant term
2151 * of the inequality has already been updated with respect to this
2152 * translate and the inequality is therefore known to be a bound
2153 * of this basic set.
2155 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2156 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
2157 int shift)
2159 uint32_t c_hash;
2160 struct ineq_cmp_data v;
2161 struct isl_hash_table_entry *entry;
2162 int j, k;
2164 if (!hull)
2165 return NULL;
2167 v.len = isl_basic_set_total_dim(hull);
2168 v.p = ineq;
2169 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2171 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2172 has_ineq, &v, 0);
2173 if (entry)
2174 return hull;
2176 for (j = 0; j < i; ++j) {
2177 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2178 c_hash, has_ineq, &v, 0);
2179 if (entry)
2180 break;
2182 if (j < i)
2183 return hull;
2185 k = isl_basic_set_alloc_inequality(hull);
2186 if (k < 0)
2187 goto error;
2188 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2190 set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
2191 for (j = 0; j < i; ++j) {
2192 int bound;
2193 bound = is_bound(data, set, j, hull->ineq[k], shift);
2194 if (bound < 0)
2195 goto error;
2196 if (!bound)
2197 break;
2199 if (j < i) {
2200 isl_basic_set_free_inequality(hull, 1);
2201 return hull;
2204 for (j = i + 1; j < set->n; ++j) {
2205 int bound;
2206 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2207 c_hash, has_ineq, &v, 0);
2208 if (entry)
2209 continue;
2210 bound = is_bound(data, set, j, hull->ineq[k], shift);
2211 if (bound < 0)
2212 goto error;
2213 if (!bound)
2214 break;
2216 if (j < set->n) {
2217 isl_basic_set_free_inequality(hull, 1);
2218 return hull;
2221 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2222 has_ineq, &v, 1);
2223 if (!entry)
2224 goto error;
2225 entry->data = hull->ineq[k];
2227 return hull;
2228 error:
2229 isl_basic_set_free(hull);
2230 return NULL;
2233 /* Check if any inequality from basic set "i" is or can be relaxed to
2234 * become a bound on the whole set. If so, add the (relaxed) inequality
2235 * to "hull". Relaxation is only allowed if "shift" is set.
2237 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2238 struct sh_data *data, struct isl_set *set, int i, int shift)
2240 int j, k;
2241 unsigned dim = isl_basic_set_total_dim(bset);
2243 for (j = 0; j < set->p[i]->n_eq; ++j) {
2244 for (k = 0; k < 2; ++k) {
2245 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2246 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2247 shift);
2250 for (j = 0; j < set->p[i]->n_ineq; ++j)
2251 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2252 return bset;
2255 /* Compute a superset of the convex hull of set that is described
2256 * by only (translates of) the constraints in the constituents of set.
2257 * Translation is only allowed if "shift" is set.
2259 static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2260 int shift)
2262 struct sh_data *data = NULL;
2263 struct isl_basic_set *hull = NULL;
2264 unsigned n_ineq;
2265 int i;
2267 if (!set)
2268 return NULL;
2270 n_ineq = 0;
2271 for (i = 0; i < set->n; ++i) {
2272 if (!set->p[i])
2273 goto error;
2274 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2277 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2278 if (!hull)
2279 goto error;
2281 data = sh_data_alloc(set, n_ineq);
2282 if (!data)
2283 goto error;
2285 for (i = 0; i < set->n; ++i)
2286 hull = add_bounds(hull, data, set, i, shift);
2288 sh_data_free(data);
2289 isl_set_free(set);
2291 return hull;
2292 error:
2293 sh_data_free(data);
2294 isl_basic_set_free(hull);
2295 isl_set_free(set);
2296 return NULL;
2299 /* Compute a superset of the convex hull of map that is described
2300 * by only (translates of) the constraints in the constituents of map.
2301 * Handle trivial cases where map is NULL or contains at most one disjunct.
2303 static __isl_give isl_basic_map *map_simple_hull_trivial(
2304 __isl_take isl_map *map)
2306 isl_basic_map *hull;
2308 if (!map)
2309 return NULL;
2310 if (map->n == 0)
2311 return replace_map_by_empty_basic_map(map);
2313 hull = isl_basic_map_copy(map->p[0]);
2314 isl_map_free(map);
2315 return hull;
2318 /* Return a copy of the simple hull cached inside "map".
2319 * "shift" determines whether to return the cached unshifted or shifted
2320 * simple hull.
2322 static __isl_give isl_basic_map *cached_simple_hull(__isl_take isl_map *map,
2323 int shift)
2325 isl_basic_map *hull;
2327 hull = isl_basic_map_copy(map->cached_simple_hull[shift]);
2328 isl_map_free(map);
2330 return hull;
2333 /* Compute a superset of the convex hull of map that is described
2334 * by only (translates of) the constraints in the constituents of map.
2335 * Translation is only allowed if "shift" is set.
2337 * The constraints are sorted while removing redundant constraints
2338 * in order to indicate a preference of which constraints should
2339 * be preserved. In particular, pairs of constraints that are
2340 * sorted together are preferred to either both be preserved
2341 * or both be removed. The sorting is performed inside
2342 * isl_basic_map_remove_redundancies.
2344 * The result of the computation is stored in map->cached_simple_hull[shift]
2345 * such that it can be reused in subsequent calls. The cache is cleared
2346 * whenever the map is modified (in isl_map_cow).
2347 * Note that the results need to be stored in the input map for there
2348 * to be any chance that they may get reused. In particular, they
2349 * are stored in a copy of the input map that is saved before
2350 * the integer division alignment.
2352 static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2353 int shift)
2355 struct isl_set *set = NULL;
2356 struct isl_basic_map *model = NULL;
2357 struct isl_basic_map *hull;
2358 struct isl_basic_map *affine_hull;
2359 struct isl_basic_set *bset = NULL;
2360 isl_map *input;
2362 if (!map || map->n <= 1)
2363 return map_simple_hull_trivial(map);
2365 if (map->cached_simple_hull[shift])
2366 return cached_simple_hull(map, shift);
2368 map = isl_map_detect_equalities(map);
2369 if (!map || map->n <= 1)
2370 return map_simple_hull_trivial(map);
2371 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2372 input = isl_map_copy(map);
2373 map = isl_map_align_divs_internal(map);
2374 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2376 set = isl_map_underlying_set(map);
2378 bset = uset_simple_hull(set, shift);
2380 hull = isl_basic_map_overlying_set(bset, model);
2382 hull = isl_basic_map_intersect(hull, affine_hull);
2383 hull = isl_basic_map_remove_redundancies(hull);
2385 if (hull) {
2386 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2387 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2390 hull = isl_basic_map_finalize(hull);
2391 if (input)
2392 input->cached_simple_hull[shift] = isl_basic_map_copy(hull);
2393 isl_map_free(input);
2395 return hull;
2398 /* Compute a superset of the convex hull of map that is described
2399 * by only translates of the constraints in the constituents of map.
2401 __isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2403 return map_simple_hull(map, 1);
2406 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2408 return bset_from_bmap(isl_map_simple_hull(set_to_map(set)));
2411 /* Compute a superset of the convex hull of map that is described
2412 * by only the constraints in the constituents of map.
2414 __isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2415 __isl_take isl_map *map)
2417 return map_simple_hull(map, 0);
2420 __isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2421 __isl_take isl_set *set)
2423 return isl_map_unshifted_simple_hull(set);
2426 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2427 * A constraint that appears with different constant terms
2428 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2429 * (i.e., greatest) constant term.
2430 * "bmap1" and "bmap2" are assumed to have the same (known)
2431 * integer divisions.
2432 * The constraints of both "bmap1" and "bmap2" are assumed
2433 * to have been sorted using isl_basic_map_sort_constraints.
2435 * Run through the inequality constraints of "bmap1" and "bmap2"
2436 * in sorted order.
2437 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2438 * is removed.
2439 * If a match is found, the constraint is kept. If needed, the constant
2440 * term of the constraint is adjusted.
2442 static __isl_give isl_basic_map *select_shared_inequalities(
2443 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2445 int i1, i2;
2447 bmap1 = isl_basic_map_cow(bmap1);
2448 if (!bmap1 || !bmap2)
2449 return isl_basic_map_free(bmap1);
2451 i1 = bmap1->n_ineq - 1;
2452 i2 = bmap2->n_ineq - 1;
2453 while (bmap1 && i1 >= 0 && i2 >= 0) {
2454 int cmp;
2456 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2457 bmap2->ineq[i2]);
2458 if (cmp < 0) {
2459 --i2;
2460 continue;
2462 if (cmp > 0) {
2463 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2464 bmap1 = isl_basic_map_free(bmap1);
2465 --i1;
2466 continue;
2468 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2469 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2470 --i1;
2471 --i2;
2473 for (; i1 >= 0; --i1)
2474 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2475 bmap1 = isl_basic_map_free(bmap1);
2477 return bmap1;
2480 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2481 * "bmap1" and "bmap2" are assumed to have the same (known)
2482 * integer divisions.
2484 * Run through the equality constraints of "bmap1" and "bmap2".
2485 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2486 * is removed.
2488 static __isl_give isl_basic_map *select_shared_equalities(
2489 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2491 int i1, i2;
2492 unsigned total;
2494 bmap1 = isl_basic_map_cow(bmap1);
2495 if (!bmap1 || !bmap2)
2496 return isl_basic_map_free(bmap1);
2498 total = isl_basic_map_total_dim(bmap1);
2500 i1 = bmap1->n_eq - 1;
2501 i2 = bmap2->n_eq - 1;
2502 while (bmap1 && i1 >= 0 && i2 >= 0) {
2503 int last1, last2;
2505 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2506 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2507 if (last1 > last2) {
2508 --i2;
2509 continue;
2511 if (last1 < last2) {
2512 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2513 bmap1 = isl_basic_map_free(bmap1);
2514 --i1;
2515 continue;
2517 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2518 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2519 bmap1 = isl_basic_map_free(bmap1);
2521 --i1;
2522 --i2;
2524 for (; i1 >= 0; --i1)
2525 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2526 bmap1 = isl_basic_map_free(bmap1);
2528 return bmap1;
2531 /* Compute a superset of "bmap1" and "bmap2" that is described
2532 * by only the constraints that appear in both "bmap1" and "bmap2".
2534 * First drop constraints that involve unknown integer divisions
2535 * since it is not trivial to check whether two such integer divisions
2536 * in different basic maps are the same.
2537 * Then align the remaining (known) divs and sort the constraints.
2538 * Finally drop all inequalities and equalities from "bmap1" that
2539 * do not also appear in "bmap2".
2541 __isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2542 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2544 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2545 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2546 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2547 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2548 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2549 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2550 bmap1 = isl_basic_map_sort_constraints(bmap1);
2551 bmap2 = isl_basic_map_sort_constraints(bmap2);
2553 bmap1 = select_shared_inequalities(bmap1, bmap2);
2554 bmap1 = select_shared_equalities(bmap1, bmap2);
2556 isl_basic_map_free(bmap2);
2557 bmap1 = isl_basic_map_finalize(bmap1);
2558 return bmap1;
2561 /* Compute a superset of the convex hull of "map" that is described
2562 * by only the constraints in the constituents of "map".
2563 * In particular, the result is composed of constraints that appear
2564 * in each of the basic maps of "map"
2566 * Constraints that involve unknown integer divisions are dropped
2567 * since it is not trivial to check whether two such integer divisions
2568 * in different basic maps are the same.
2570 * The hull is initialized from the first basic map and then
2571 * updated with respect to the other basic maps in turn.
2573 __isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2574 __isl_take isl_map *map)
2576 int i;
2577 isl_basic_map *hull;
2579 if (!map)
2580 return NULL;
2581 if (map->n <= 1)
2582 return map_simple_hull_trivial(map);
2583 map = isl_map_drop_constraint_involving_unknown_divs(map);
2584 hull = isl_basic_map_copy(map->p[0]);
2585 for (i = 1; i < map->n; ++i) {
2586 isl_basic_map *bmap_i;
2588 bmap_i = isl_basic_map_copy(map->p[i]);
2589 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2592 isl_map_free(map);
2593 return hull;
2596 /* Compute a superset of the convex hull of "set" that is described
2597 * by only the constraints in the constituents of "set".
2598 * In particular, the result is composed of constraints that appear
2599 * in each of the basic sets of "set"
2601 __isl_give isl_basic_set *isl_set_plain_unshifted_simple_hull(
2602 __isl_take isl_set *set)
2604 return isl_map_plain_unshifted_simple_hull(set);
2607 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2609 * For each basic set in "set", we first check if the basic set
2610 * contains a translate of "ineq". If this translate is more relaxed,
2611 * then we assume that "ineq" is not a bound on this basic set.
2612 * Otherwise, we know that it is a bound.
2613 * If the basic set does not contain a translate of "ineq", then
2614 * we call is_bound to perform the test.
2616 static __isl_give isl_basic_set *add_bound_from_constraint(
2617 __isl_take isl_basic_set *hull, struct sh_data *data,
2618 __isl_keep isl_set *set, isl_int *ineq)
2620 int i, k;
2621 isl_ctx *ctx;
2622 uint32_t c_hash;
2623 struct ineq_cmp_data v;
2625 if (!hull || !set)
2626 return isl_basic_set_free(hull);
2628 v.len = isl_basic_set_total_dim(hull);
2629 v.p = ineq;
2630 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2632 ctx = isl_basic_set_get_ctx(hull);
2633 for (i = 0; i < set->n; ++i) {
2634 int bound;
2635 struct isl_hash_table_entry *entry;
2637 entry = isl_hash_table_find(ctx, data->p[i].table,
2638 c_hash, &has_ineq, &v, 0);
2639 if (entry) {
2640 isl_int *ineq_i = entry->data;
2641 int neg, more_relaxed;
2643 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2644 if (neg)
2645 isl_int_neg(ineq_i[0], ineq_i[0]);
2646 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2647 if (neg)
2648 isl_int_neg(ineq_i[0], ineq_i[0]);
2649 if (more_relaxed)
2650 break;
2651 else
2652 continue;
2654 bound = is_bound(data, set, i, ineq, 0);
2655 if (bound < 0)
2656 return isl_basic_set_free(hull);
2657 if (!bound)
2658 break;
2660 if (i < set->n)
2661 return hull;
2663 k = isl_basic_set_alloc_inequality(hull);
2664 if (k < 0)
2665 return isl_basic_set_free(hull);
2666 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2668 return hull;
2671 /* Compute a superset of the convex hull of "set" that is described
2672 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2673 * has no parameters or integer divisions.
2675 * The inequalities in "ineq" are assumed to have been sorted such
2676 * that constraints with the same linear part appear together and
2677 * that among constraints with the same linear part, those with
2678 * smaller constant term appear first.
2680 * We reuse the same data structure that is used by uset_simple_hull,
2681 * but we do not need the hull table since we will not consider the
2682 * same constraint more than once. We therefore allocate it with zero size.
2684 * We run through the constraints and try to add them one by one,
2685 * skipping identical constraints. If we have added a constraint and
2686 * the next constraint is a more relaxed translate, then we skip this
2687 * next constraint as well.
2689 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2690 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2692 int i;
2693 int last_added = 0;
2694 struct sh_data *data = NULL;
2695 isl_basic_set *hull = NULL;
2696 unsigned dim;
2698 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2699 if (!hull)
2700 goto error;
2702 data = sh_data_alloc(set, 0);
2703 if (!data)
2704 goto error;
2706 dim = isl_set_dim(set, isl_dim_set);
2707 for (i = 0; i < n_ineq; ++i) {
2708 int hull_n_ineq = hull->n_ineq;
2709 int parallel;
2711 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2712 dim);
2713 if (parallel &&
2714 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2715 continue;
2716 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2717 if (!hull)
2718 goto error;
2719 last_added = hull->n_ineq > hull_n_ineq;
2722 sh_data_free(data);
2723 isl_set_free(set);
2724 return hull;
2725 error:
2726 sh_data_free(data);
2727 isl_set_free(set);
2728 isl_basic_set_free(hull);
2729 return NULL;
2732 /* Collect pointers to all the inequalities in the elements of "list"
2733 * in "ineq". For equalities, store both a pointer to the equality and
2734 * a pointer to its opposite, which is first copied to "mat".
2735 * "ineq" and "mat" are assumed to have been preallocated to the right size
2736 * (the number of inequalities + 2 times the number of equalites and
2737 * the number of equalities, respectively).
2739 static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2740 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2742 int i, j, n, n_eq, n_ineq;
2744 if (!mat)
2745 return NULL;
2747 n_eq = 0;
2748 n_ineq = 0;
2749 n = isl_basic_set_list_n_basic_set(list);
2750 for (i = 0; i < n; ++i) {
2751 isl_basic_set *bset;
2752 bset = isl_basic_set_list_get_basic_set(list, i);
2753 if (!bset)
2754 return isl_mat_free(mat);
2755 for (j = 0; j < bset->n_eq; ++j) {
2756 ineq[n_ineq++] = mat->row[n_eq];
2757 ineq[n_ineq++] = bset->eq[j];
2758 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2760 for (j = 0; j < bset->n_ineq; ++j)
2761 ineq[n_ineq++] = bset->ineq[j];
2762 isl_basic_set_free(bset);
2765 return mat;
2768 /* Comparison routine for use as an isl_sort callback.
2770 * Constraints with the same linear part are sorted together and
2771 * among constraints with the same linear part, those with smaller
2772 * constant term are sorted first.
2774 static int cmp_ineq(const void *a, const void *b, void *arg)
2776 unsigned dim = *(unsigned *) arg;
2777 isl_int * const *ineq1 = a;
2778 isl_int * const *ineq2 = b;
2779 int cmp;
2781 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2782 if (cmp != 0)
2783 return cmp;
2784 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2787 /* Compute a superset of the convex hull of "set" that is described
2788 * by only constraints in the elements of "list", where "set" has
2789 * no parameters or integer divisions.
2791 * We collect all the constraints in those elements and then
2792 * sort the constraints such that constraints with the same linear part
2793 * are sorted together and that those with smaller constant term are
2794 * sorted first.
2796 static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2797 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2799 int i, n, n_eq, n_ineq;
2800 unsigned dim;
2801 isl_ctx *ctx;
2802 isl_mat *mat = NULL;
2803 isl_int **ineq = NULL;
2804 isl_basic_set *hull;
2806 if (!set)
2807 goto error;
2808 ctx = isl_set_get_ctx(set);
2810 n_eq = 0;
2811 n_ineq = 0;
2812 n = isl_basic_set_list_n_basic_set(list);
2813 for (i = 0; i < n; ++i) {
2814 isl_basic_set *bset;
2815 bset = isl_basic_set_list_get_basic_set(list, i);
2816 if (!bset)
2817 goto error;
2818 n_eq += bset->n_eq;
2819 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2820 isl_basic_set_free(bset);
2823 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2824 if (n_ineq > 0 && !ineq)
2825 goto error;
2827 dim = isl_set_dim(set, isl_dim_set);
2828 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2829 mat = collect_inequalities(mat, list, ineq);
2830 if (!mat)
2831 goto error;
2833 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2834 goto error;
2836 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2838 isl_mat_free(mat);
2839 free(ineq);
2840 isl_basic_set_list_free(list);
2841 return hull;
2842 error:
2843 isl_mat_free(mat);
2844 free(ineq);
2845 isl_set_free(set);
2846 isl_basic_set_list_free(list);
2847 return NULL;
2850 /* Compute a superset of the convex hull of "map" that is described
2851 * by only constraints in the elements of "list".
2853 * If the list is empty, then we can only describe the universe set.
2854 * If the input map is empty, then all constraints are valid, so
2855 * we return the intersection of the elements in "list".
2857 * Otherwise, we align all divs and temporarily treat them
2858 * as regular variables, computing the unshifted simple hull in
2859 * uset_unshifted_simple_hull_from_basic_set_list.
2861 static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2862 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2864 isl_basic_map *model;
2865 isl_basic_map *hull;
2866 isl_set *set;
2867 isl_basic_set_list *bset_list;
2869 if (!map || !list)
2870 goto error;
2872 if (isl_basic_map_list_n_basic_map(list) == 0) {
2873 isl_space *space;
2875 space = isl_map_get_space(map);
2876 isl_map_free(map);
2877 isl_basic_map_list_free(list);
2878 return isl_basic_map_universe(space);
2880 if (isl_map_plain_is_empty(map)) {
2881 isl_map_free(map);
2882 return isl_basic_map_list_intersect(list);
2885 map = isl_map_align_divs_to_basic_map_list(map, list);
2886 if (!map)
2887 goto error;
2888 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2890 model = isl_basic_map_list_get_basic_map(list, 0);
2892 set = isl_map_underlying_set(map);
2893 bset_list = isl_basic_map_list_underlying_set(list);
2895 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2896 hull = isl_basic_map_overlying_set(hull, model);
2898 return hull;
2899 error:
2900 isl_map_free(map);
2901 isl_basic_map_list_free(list);
2902 return NULL;
2905 /* Return a sequence of the basic maps that make up the maps in "list".
2907 static __isl_give isl_basic_map_list *collect_basic_maps(
2908 __isl_take isl_map_list *list)
2910 int i, n;
2911 isl_ctx *ctx;
2912 isl_basic_map_list *bmap_list;
2914 if (!list)
2915 return NULL;
2916 n = isl_map_list_n_map(list);
2917 ctx = isl_map_list_get_ctx(list);
2918 bmap_list = isl_basic_map_list_alloc(ctx, 0);
2920 for (i = 0; i < n; ++i) {
2921 isl_map *map;
2922 isl_basic_map_list *list_i;
2924 map = isl_map_list_get_map(list, i);
2925 map = isl_map_compute_divs(map);
2926 list_i = isl_map_get_basic_map_list(map);
2927 isl_map_free(map);
2928 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
2931 isl_map_list_free(list);
2932 return bmap_list;
2935 /* Compute a superset of the convex hull of "map" that is described
2936 * by only constraints in the elements of "list".
2938 * If "map" is the universe, then the convex hull (and therefore
2939 * any superset of the convexhull) is the universe as well.
2941 * Otherwise, we collect all the basic maps in the map list and
2942 * continue with map_unshifted_simple_hull_from_basic_map_list.
2944 __isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
2945 __isl_take isl_map *map, __isl_take isl_map_list *list)
2947 isl_basic_map_list *bmap_list;
2948 int is_universe;
2950 is_universe = isl_map_plain_is_universe(map);
2951 if (is_universe < 0)
2952 map = isl_map_free(map);
2953 if (is_universe < 0 || is_universe) {
2954 isl_map_list_free(list);
2955 return isl_map_unshifted_simple_hull(map);
2958 bmap_list = collect_basic_maps(list);
2959 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
2962 /* Compute a superset of the convex hull of "set" that is described
2963 * by only constraints in the elements of "list".
2965 __isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
2966 __isl_take isl_set *set, __isl_take isl_set_list *list)
2968 return isl_map_unshifted_simple_hull_from_map_list(set, list);
2971 /* Given a set "set", return parametric bounds on the dimension "dim".
2973 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2975 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2976 set = isl_set_copy(set);
2977 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2978 set = isl_set_eliminate_dims(set, 0, dim);
2979 return isl_set_convex_hull(set);
2982 /* Computes a "simple hull" and then check if each dimension in the
2983 * resulting hull is bounded by a symbolic constant. If not, the
2984 * hull is intersected with the corresponding bounds on the whole set.
2986 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
2988 int i, j;
2989 struct isl_basic_set *hull;
2990 unsigned nparam, left;
2991 int removed_divs = 0;
2993 hull = isl_set_simple_hull(isl_set_copy(set));
2994 if (!hull)
2995 goto error;
2997 nparam = isl_basic_set_dim(hull, isl_dim_param);
2998 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
2999 int lower = 0, upper = 0;
3000 struct isl_basic_set *bounds;
3002 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
3003 for (j = 0; j < hull->n_eq; ++j) {
3004 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3005 continue;
3006 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3007 left) == -1)
3008 break;
3010 if (j < hull->n_eq)
3011 continue;
3013 for (j = 0; j < hull->n_ineq; ++j) {
3014 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3015 continue;
3016 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3017 left) != -1 ||
3018 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3019 i) != -1)
3020 continue;
3021 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3022 lower = 1;
3023 else
3024 upper = 1;
3025 if (lower && upper)
3026 break;
3029 if (lower && upper)
3030 continue;
3032 if (!removed_divs) {
3033 set = isl_set_remove_divs(set);
3034 if (!set)
3035 goto error;
3036 removed_divs = 1;
3038 bounds = set_bounds(set, i);
3039 hull = isl_basic_set_intersect(hull, bounds);
3040 if (!hull)
3041 goto error;
3044 isl_set_free(set);
3045 return hull;
3046 error:
3047 isl_set_free(set);
3048 isl_basic_set_free(hull);
3049 return NULL;