blob: 76a844bfa052acad4f9ba6d02982f976382b0fe3 [file] [log] [blame]
Andrew Top61a84952019-04-30 15:07:33 -07001/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 INRIA Rocquencourt
4 *
5 * Use of this software is governed by the MIT license
6 *
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
11 */
12
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>
25
26#include <bset_to_bmap.c>
27#include <bset_from_bmap.c>
28#include <set_to_map.c>
29
30static __isl_give isl_basic_set *uset_convex_hull_wrap_bounded(
31 __isl_take isl_set *set);
32
33/* Remove redundant
34 * constraints. If the minimal value along the normal of a constraint
35 * is the same if the constraint is removed, then the constraint is redundant.
36 *
37 * Since some constraints may be mutually redundant, sort the constraints
38 * first such that constraints that involve existentially quantified
39 * variables are considered for removal before those that do not.
40 * The sorting is also needed for the use in map_simple_hull.
41 *
42 * Note that isl_tab_detect_implicit_equalities may also end up
43 * marking some constraints as redundant. Make sure the constraints
44 * are preserved and undo those marking such that isl_tab_detect_redundant
45 * can consider the constraints in the sorted order.
46 *
47 * Alternatively, we could have intersected the basic map with the
48 * corresponding equality and then checked if the dimension was that
49 * of a facet.
50 */
51__isl_give isl_basic_map *isl_basic_map_remove_redundancies(
52 __isl_take isl_basic_map *bmap)
53{
54 struct isl_tab *tab;
55
56 if (!bmap)
57 return NULL;
58
59 bmap = isl_basic_map_gauss(bmap, NULL);
60 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
61 return bmap;
62 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
63 return bmap;
64 if (bmap->n_ineq <= 1)
65 return bmap;
66
67 bmap = isl_basic_map_sort_constraints(bmap);
68 tab = isl_tab_from_basic_map(bmap, 0);
69 if (!tab)
70 goto error;
71 tab->preserve = 1;
72 if (isl_tab_detect_implicit_equalities(tab) < 0)
73 goto error;
74 if (isl_tab_restore_redundant(tab) < 0)
75 goto error;
76 tab->preserve = 0;
77 if (isl_tab_detect_redundant(tab) < 0)
78 goto error;
79 bmap = isl_basic_map_update_from_tab(bmap, tab);
80 isl_tab_free(tab);
81 if (!bmap)
82 return NULL;
83 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
84 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
85 return bmap;
86error:
87 isl_tab_free(tab);
88 isl_basic_map_free(bmap);
89 return NULL;
90}
91
92__isl_give isl_basic_set *isl_basic_set_remove_redundancies(
93 __isl_take isl_basic_set *bset)
94{
95 return bset_from_bmap(
96 isl_basic_map_remove_redundancies(bset_to_bmap(bset)));
97}
98
99/* Remove redundant constraints in each of the basic maps.
100 */
101__isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
102{
103 return isl_map_inline_foreach_basic_map(map,
104 &isl_basic_map_remove_redundancies);
105}
106
107__isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
108{
109 return isl_map_remove_redundancies(set);
110}
111
112/* Check if the set set is bound in the direction of the affine
113 * constraint c and if so, set the constant term such that the
114 * resulting constraint is a bounding constraint for the set.
115 */
116static int uset_is_bound(__isl_keep isl_set *set, isl_int *c, unsigned len)
117{
118 int first;
119 int j;
120 isl_int opt;
121 isl_int opt_denom;
122
123 isl_int_init(opt);
124 isl_int_init(opt_denom);
125 first = 1;
126 for (j = 0; j < set->n; ++j) {
127 enum isl_lp_result res;
128
129 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
130 continue;
131
132 res = isl_basic_set_solve_lp(set->p[j],
133 0, c, set->ctx->one, &opt, &opt_denom, NULL);
134 if (res == isl_lp_unbounded)
135 break;
136 if (res == isl_lp_error)
137 goto error;
138 if (res == isl_lp_empty) {
139 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
140 if (!set->p[j])
141 goto error;
142 continue;
143 }
144 if (first || isl_int_is_neg(opt)) {
145 if (!isl_int_is_one(opt_denom))
146 isl_seq_scale(c, c, opt_denom, len);
147 isl_int_sub(c[0], c[0], opt);
148 }
149 first = 0;
150 }
151 isl_int_clear(opt);
152 isl_int_clear(opt_denom);
153 return j >= set->n;
154error:
155 isl_int_clear(opt);
156 isl_int_clear(opt_denom);
157 return -1;
158}
159
160static struct isl_basic_set *isl_basic_set_add_equality(
161 struct isl_basic_set *bset, isl_int *c)
162{
163 int i;
164 unsigned dim;
165
166 if (!bset)
167 return NULL;
168
169 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
170 return bset;
171
172 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
173 isl_assert(bset->ctx, bset->n_div == 0, goto error);
174 dim = isl_basic_set_n_dim(bset);
175 bset = isl_basic_set_cow(bset);
176 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
177 i = isl_basic_set_alloc_equality(bset);
178 if (i < 0)
179 goto error;
180 isl_seq_cpy(bset->eq[i], c, 1 + dim);
181 return bset;
182error:
183 isl_basic_set_free(bset);
184 return NULL;
185}
186
187static __isl_give isl_set *isl_set_add_basic_set_equality(
188 __isl_take isl_set *set, isl_int *c)
189{
190 int i;
191
192 set = isl_set_cow(set);
193 if (!set)
194 return NULL;
195 for (i = 0; i < set->n; ++i) {
196 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
197 if (!set->p[i])
198 goto error;
199 }
200 return set;
201error:
202 isl_set_free(set);
203 return NULL;
204}
205
206/* Given a union of basic sets, construct the constraints for wrapping
207 * a facet around one of its ridges.
208 * In particular, if each of n the d-dimensional basic sets i in "set"
209 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
210 * and is defined by the constraints
211 * [ 1 ]
212 * A_i [ x ] >= 0
213 *
214 * then the resulting set is of dimension n*(1+d) and has as constraints
215 *
216 * [ a_i ]
217 * A_i [ x_i ] >= 0
218 *
219 * a_i >= 0
220 *
221 * \sum_i x_{i,1} = 1
222 */
223static __isl_give isl_basic_set *wrap_constraints(__isl_keep isl_set *set)
224{
225 struct isl_basic_set *lp;
226 unsigned n_eq;
227 unsigned n_ineq;
228 int i, j, k;
229 unsigned dim, lp_dim;
230
231 if (!set)
232 return NULL;
233
234 dim = 1 + isl_set_n_dim(set);
235 n_eq = 1;
236 n_ineq = set->n;
237 for (i = 0; i < set->n; ++i) {
238 n_eq += set->p[i]->n_eq;
239 n_ineq += set->p[i]->n_ineq;
240 }
241 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
242 lp = isl_basic_set_set_rational(lp);
243 if (!lp)
244 return NULL;
245 lp_dim = isl_basic_set_n_dim(lp);
246 k = isl_basic_set_alloc_equality(lp);
247 isl_int_set_si(lp->eq[k][0], -1);
248 for (i = 0; i < set->n; ++i) {
249 isl_int_set_si(lp->eq[k][1+dim*i], 0);
250 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
251 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
252 }
253 for (i = 0; i < set->n; ++i) {
254 k = isl_basic_set_alloc_inequality(lp);
255 isl_seq_clr(lp->ineq[k], 1+lp_dim);
256 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
257
258 for (j = 0; j < set->p[i]->n_eq; ++j) {
259 k = isl_basic_set_alloc_equality(lp);
260 isl_seq_clr(lp->eq[k], 1+dim*i);
261 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
262 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
263 }
264
265 for (j = 0; j < set->p[i]->n_ineq; ++j) {
266 k = isl_basic_set_alloc_inequality(lp);
267 isl_seq_clr(lp->ineq[k], 1+dim*i);
268 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
269 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
270 }
271 }
272 return lp;
273}
274
275/* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
276 * of that facet, compute the other facet of the convex hull that contains
277 * the ridge.
278 *
279 * We first transform the set such that the facet constraint becomes
280 *
281 * x_1 >= 0
282 *
283 * I.e., the facet lies in
284 *
285 * x_1 = 0
286 *
287 * and on that facet, the constraint that defines the ridge is
288 *
289 * x_2 >= 0
290 *
291 * (This transformation is not strictly needed, all that is needed is
292 * that the ridge contains the origin.)
293 *
294 * Since the ridge contains the origin, the cone of the convex hull
295 * will be of the form
296 *
297 * x_1 >= 0
298 * x_2 >= a x_1
299 *
300 * with this second constraint defining the new facet.
301 * The constant a is obtained by settting x_1 in the cone of the
302 * convex hull to 1 and minimizing x_2.
303 * Now, each element in the cone of the convex hull is the sum
304 * of elements in the cones of the basic sets.
305 * If a_i is the dilation factor of basic set i, then the problem
306 * we need to solve is
307 *
308 * min \sum_i x_{i,2}
309 * st
310 * \sum_i x_{i,1} = 1
311 * a_i >= 0
312 * [ a_i ]
313 * A [ x_i ] >= 0
314 *
315 * with
316 * [ 1 ]
317 * A_i [ x_i ] >= 0
318 *
319 * the constraints of each (transformed) basic set.
320 * If a = n/d, then the constraint defining the new facet (in the transformed
321 * space) is
322 *
323 * -n x_1 + d x_2 >= 0
324 *
325 * In the original space, we need to take the same combination of the
326 * corresponding constraints "facet" and "ridge".
327 *
328 * If a = -infty = "-1/0", then we just return the original facet constraint.
329 * This means that the facet is unbounded, but has a bounded intersection
330 * with the union of sets.
331 */
332isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
333 isl_int *facet, isl_int *ridge)
334{
335 int i;
336 isl_ctx *ctx;
337 struct isl_mat *T = NULL;
338 struct isl_basic_set *lp = NULL;
339 struct isl_vec *obj;
340 enum isl_lp_result res;
341 isl_int num, den;
342 unsigned dim;
343
344 if (!set)
345 return NULL;
346 ctx = set->ctx;
347 set = isl_set_copy(set);
348 set = isl_set_set_rational(set);
349
350 dim = 1 + isl_set_n_dim(set);
351 T = isl_mat_alloc(ctx, 3, dim);
352 if (!T)
353 goto error;
354 isl_int_set_si(T->row[0][0], 1);
355 isl_seq_clr(T->row[0]+1, dim - 1);
356 isl_seq_cpy(T->row[1], facet, dim);
357 isl_seq_cpy(T->row[2], ridge, dim);
358 T = isl_mat_right_inverse(T);
359 set = isl_set_preimage(set, T);
360 T = NULL;
361 if (!set)
362 goto error;
363 lp = wrap_constraints(set);
364 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
365 if (!obj)
366 goto error;
367 isl_int_set_si(obj->block.data[0], 0);
368 for (i = 0; i < set->n; ++i) {
369 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
370 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
371 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
372 }
373 isl_int_init(num);
374 isl_int_init(den);
375 res = isl_basic_set_solve_lp(lp, 0,
376 obj->block.data, ctx->one, &num, &den, NULL);
377 if (res == isl_lp_ok) {
378 isl_int_neg(num, num);
379 isl_seq_combine(facet, num, facet, den, ridge, dim);
380 isl_seq_normalize(ctx, facet, dim);
381 }
382 isl_int_clear(num);
383 isl_int_clear(den);
384 isl_vec_free(obj);
385 isl_basic_set_free(lp);
386 isl_set_free(set);
387 if (res == isl_lp_error)
388 return NULL;
389 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
390 return NULL);
391 return facet;
392error:
393 isl_basic_set_free(lp);
394 isl_mat_free(T);
395 isl_set_free(set);
396 return NULL;
397}
398
399/* Compute the constraint of a facet of "set".
400 *
401 * We first compute the intersection with a bounding constraint
402 * that is orthogonal to one of the coordinate axes.
403 * If the affine hull of this intersection has only one equality,
404 * we have found a facet.
405 * Otherwise, we wrap the current bounding constraint around
406 * one of the equalities of the face (one that is not equal to
407 * the current bounding constraint).
408 * This process continues until we have found a facet.
409 * The dimension of the intersection increases by at least
410 * one on each iteration, so termination is guaranteed.
411 */
412static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
413{
414 struct isl_set *slice = NULL;
415 struct isl_basic_set *face = NULL;
416 int i;
417 unsigned dim = isl_set_n_dim(set);
418 int is_bound;
419 isl_mat *bounds = NULL;
420
421 isl_assert(set->ctx, set->n > 0, goto error);
422 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
423 if (!bounds)
424 return NULL;
425
426 isl_seq_clr(bounds->row[0], dim);
427 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
428 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
429 if (is_bound < 0)
430 goto error;
431 isl_assert(set->ctx, is_bound, goto error);
432 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
433 bounds->n_row = 1;
434
435 for (;;) {
436 slice = isl_set_copy(set);
437 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
438 face = isl_set_affine_hull(slice);
439 if (!face)
440 goto error;
441 if (face->n_eq == 1) {
442 isl_basic_set_free(face);
443 break;
444 }
445 for (i = 0; i < face->n_eq; ++i)
446 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
447 !isl_seq_is_neg(bounds->row[0],
448 face->eq[i], 1 + dim))
449 break;
450 isl_assert(set->ctx, i < face->n_eq, goto error);
451 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
452 goto error;
453 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
454 isl_basic_set_free(face);
455 }
456
457 return bounds;
458error:
459 isl_basic_set_free(face);
460 isl_mat_free(bounds);
461 return NULL;
462}
463
464/* Given the bounding constraint "c" of a facet of the convex hull of "set",
465 * compute a hyperplane description of the facet, i.e., compute the facets
466 * of the facet.
467 *
468 * We compute an affine transformation that transforms the constraint
469 *
470 * [ 1 ]
471 * c [ x ] = 0
472 *
473 * to the constraint
474 *
475 * z_1 = 0
476 *
477 * by computing the right inverse U of a matrix that starts with the rows
478 *
479 * [ 1 0 ]
480 * [ c ]
481 *
482 * Then
483 * [ 1 ] [ 1 ]
484 * [ x ] = U [ z ]
485 * and
486 * [ 1 ] [ 1 ]
487 * [ z ] = Q [ x ]
488 *
489 * with Q = U^{-1}
490 * Since z_1 is zero, we can drop this variable as well as the corresponding
491 * column of U to obtain
492 *
493 * [ 1 ] [ 1 ]
494 * [ x ] = U' [ z' ]
495 * and
496 * [ 1 ] [ 1 ]
497 * [ z' ] = Q' [ x ]
498 *
499 * with Q' equal to Q, but without the corresponding row.
500 * After computing the facets of the facet in the z' space,
501 * we convert them back to the x space through Q.
502 */
503static __isl_give isl_basic_set *compute_facet(__isl_keep isl_set *set,
504 isl_int *c)
505{
506 struct isl_mat *m, *U, *Q;
507 struct isl_basic_set *facet = NULL;
508 struct isl_ctx *ctx;
509 unsigned dim;
510
511 ctx = set->ctx;
512 set = isl_set_copy(set);
513 dim = isl_set_n_dim(set);
514 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
515 if (!m)
516 goto error;
517 isl_int_set_si(m->row[0][0], 1);
518 isl_seq_clr(m->row[0]+1, dim);
519 isl_seq_cpy(m->row[1], c, 1+dim);
520 U = isl_mat_right_inverse(m);
521 Q = isl_mat_right_inverse(isl_mat_copy(U));
522 U = isl_mat_drop_cols(U, 1, 1);
523 Q = isl_mat_drop_rows(Q, 1, 1);
524 set = isl_set_preimage(set, U);
525 facet = uset_convex_hull_wrap_bounded(set);
526 facet = isl_basic_set_preimage(facet, Q);
527 if (facet && facet->n_eq != 0)
528 isl_die(ctx, isl_error_internal, "unexpected equality",
529 return isl_basic_set_free(facet));
530 return facet;
531error:
532 isl_basic_set_free(facet);
533 isl_set_free(set);
534 return NULL;
535}
536
537/* Given an initial facet constraint, compute the remaining facets.
538 * We do this by running through all facets found so far and computing
539 * the adjacent facets through wrapping, adding those facets that we
540 * hadn't already found before.
541 *
542 * For each facet we have found so far, we first compute its facets
543 * in the resulting convex hull. That is, we compute the ridges
544 * of the resulting convex hull contained in the facet.
545 * We also compute the corresponding facet in the current approximation
546 * of the convex hull. There is no need to wrap around the ridges
547 * in this facet since that would result in a facet that is already
548 * present in the current approximation.
549 *
550 * This function can still be significantly optimized by checking which of
551 * the facets of the basic sets are also facets of the convex hull and
552 * using all the facets so far to help in constructing the facets of the
553 * facets
554 * and/or
555 * using the technique in section "3.1 Ridge Generation" of
556 * "Extended Convex Hull" by Fukuda et al.
557 */
558static __isl_give isl_basic_set *extend(__isl_take isl_basic_set *hull,
559 __isl_keep isl_set *set)
560{
561 int i, j, f;
562 int k;
563 struct isl_basic_set *facet = NULL;
564 struct isl_basic_set *hull_facet = NULL;
565 unsigned dim;
566
567 if (!hull)
568 return NULL;
569
570 isl_assert(set->ctx, set->n > 0, goto error);
571
572 dim = isl_set_n_dim(set);
573
574 for (i = 0; i < hull->n_ineq; ++i) {
575 facet = compute_facet(set, hull->ineq[i]);
576 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
577 facet = isl_basic_set_gauss(facet, NULL);
578 facet = isl_basic_set_normalize_constraints(facet);
579 hull_facet = isl_basic_set_copy(hull);
580 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
581 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
582 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
583 if (!facet || !hull_facet)
584 goto error;
585 hull = isl_basic_set_cow(hull);
586 hull = isl_basic_set_extend_space(hull,
587 isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
588 if (!hull)
589 goto error;
590 for (j = 0; j < facet->n_ineq; ++j) {
591 for (f = 0; f < hull_facet->n_ineq; ++f)
592 if (isl_seq_eq(facet->ineq[j],
593 hull_facet->ineq[f], 1 + dim))
594 break;
595 if (f < hull_facet->n_ineq)
596 continue;
597 k = isl_basic_set_alloc_inequality(hull);
598 if (k < 0)
599 goto error;
600 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
601 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
602 goto error;
603 }
604 isl_basic_set_free(hull_facet);
605 isl_basic_set_free(facet);
606 }
607 hull = isl_basic_set_simplify(hull);
608 hull = isl_basic_set_finalize(hull);
609 return hull;
610error:
611 isl_basic_set_free(hull_facet);
612 isl_basic_set_free(facet);
613 isl_basic_set_free(hull);
614 return NULL;
615}
616
617/* Special case for computing the convex hull of a one dimensional set.
618 * We simply collect the lower and upper bounds of each basic set
619 * and the biggest of those.
620 */
621static __isl_give isl_basic_set *convex_hull_1d(__isl_take isl_set *set)
622{
623 struct isl_mat *c = NULL;
624 isl_int *lower = NULL;
625 isl_int *upper = NULL;
626 int i, j, k;
627 isl_int a, b;
628 struct isl_basic_set *hull;
629
630 for (i = 0; i < set->n; ++i) {
631 set->p[i] = isl_basic_set_simplify(set->p[i]);
632 if (!set->p[i])
633 goto error;
634 }
635 set = isl_set_remove_empty_parts(set);
636 if (!set)
637 goto error;
638 isl_assert(set->ctx, set->n > 0, goto error);
639 c = isl_mat_alloc(set->ctx, 2, 2);
640 if (!c)
641 goto error;
642
643 if (set->p[0]->n_eq > 0) {
644 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
645 lower = c->row[0];
646 upper = c->row[1];
647 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
648 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
649 isl_seq_neg(upper, set->p[0]->eq[0], 2);
650 } else {
651 isl_seq_neg(lower, set->p[0]->eq[0], 2);
652 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
653 }
654 } else {
655 for (j = 0; j < set->p[0]->n_ineq; ++j) {
656 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
657 lower = c->row[0];
658 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
659 } else {
660 upper = c->row[1];
661 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
662 }
663 }
664 }
665
666 isl_int_init(a);
667 isl_int_init(b);
668 for (i = 0; i < set->n; ++i) {
669 struct isl_basic_set *bset = set->p[i];
670 int has_lower = 0;
671 int has_upper = 0;
672
673 for (j = 0; j < bset->n_eq; ++j) {
674 has_lower = 1;
675 has_upper = 1;
676 if (lower) {
677 isl_int_mul(a, lower[0], bset->eq[j][1]);
678 isl_int_mul(b, lower[1], bset->eq[j][0]);
679 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
680 isl_seq_cpy(lower, bset->eq[j], 2);
681 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
682 isl_seq_neg(lower, bset->eq[j], 2);
683 }
684 if (upper) {
685 isl_int_mul(a, upper[0], bset->eq[j][1]);
686 isl_int_mul(b, upper[1], bset->eq[j][0]);
687 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
688 isl_seq_neg(upper, bset->eq[j], 2);
689 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
690 isl_seq_cpy(upper, bset->eq[j], 2);
691 }
692 }
693 for (j = 0; j < bset->n_ineq; ++j) {
694 if (isl_int_is_pos(bset->ineq[j][1]))
695 has_lower = 1;
696 if (isl_int_is_neg(bset->ineq[j][1]))
697 has_upper = 1;
698 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
699 isl_int_mul(a, lower[0], bset->ineq[j][1]);
700 isl_int_mul(b, lower[1], bset->ineq[j][0]);
701 if (isl_int_lt(a, b))
702 isl_seq_cpy(lower, bset->ineq[j], 2);
703 }
704 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
705 isl_int_mul(a, upper[0], bset->ineq[j][1]);
706 isl_int_mul(b, upper[1], bset->ineq[j][0]);
707 if (isl_int_gt(a, b))
708 isl_seq_cpy(upper, bset->ineq[j], 2);
709 }
710 }
711 if (!has_lower)
712 lower = NULL;
713 if (!has_upper)
714 upper = NULL;
715 }
716 isl_int_clear(a);
717 isl_int_clear(b);
718
719 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
720 hull = isl_basic_set_set_rational(hull);
721 if (!hull)
722 goto error;
723 if (lower) {
724 k = isl_basic_set_alloc_inequality(hull);
725 isl_seq_cpy(hull->ineq[k], lower, 2);
726 }
727 if (upper) {
728 k = isl_basic_set_alloc_inequality(hull);
729 isl_seq_cpy(hull->ineq[k], upper, 2);
730 }
731 hull = isl_basic_set_finalize(hull);
732 isl_set_free(set);
733 isl_mat_free(c);
734 return hull;
735error:
736 isl_set_free(set);
737 isl_mat_free(c);
738 return NULL;
739}
740
741static __isl_give isl_basic_set *convex_hull_0d(__isl_take isl_set *set)
742{
743 struct isl_basic_set *convex_hull;
744
745 if (!set)
746 return NULL;
747
748 if (isl_set_is_empty(set))
749 convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
750 else
751 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
752 isl_set_free(set);
753 return convex_hull;
754}
755
756/* Compute the convex hull of a pair of basic sets without any parameters or
757 * integer divisions using Fourier-Motzkin elimination.
758 * The convex hull is the set of all points that can be written as
759 * the sum of points from both basic sets (in homogeneous coordinates).
760 * We set up the constraints in a space with dimensions for each of
761 * the three sets and then project out the dimensions corresponding
762 * to the two original basic sets, retaining only those corresponding
763 * to the convex hull.
764 */
765static __isl_give isl_basic_set *convex_hull_pair_elim(
766 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
767{
768 int i, j, k;
769 struct isl_basic_set *bset[2];
770 struct isl_basic_set *hull = NULL;
771 unsigned dim;
772
773 if (!bset1 || !bset2)
774 goto error;
775
776 dim = isl_basic_set_n_dim(bset1);
777 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
778 1 + dim + bset1->n_eq + bset2->n_eq,
779 2 + bset1->n_ineq + bset2->n_ineq);
780 bset[0] = bset1;
781 bset[1] = bset2;
782 for (i = 0; i < 2; ++i) {
783 for (j = 0; j < bset[i]->n_eq; ++j) {
784 k = isl_basic_set_alloc_equality(hull);
785 if (k < 0)
786 goto error;
787 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
788 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
789 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
790 1+dim);
791 }
792 for (j = 0; j < bset[i]->n_ineq; ++j) {
793 k = isl_basic_set_alloc_inequality(hull);
794 if (k < 0)
795 goto error;
796 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
797 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
798 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
799 bset[i]->ineq[j], 1+dim);
800 }
801 k = isl_basic_set_alloc_inequality(hull);
802 if (k < 0)
803 goto error;
804 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
805 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
806 }
807 for (j = 0; j < 1+dim; ++j) {
808 k = isl_basic_set_alloc_equality(hull);
809 if (k < 0)
810 goto error;
811 isl_seq_clr(hull->eq[k], 1+2+3*dim);
812 isl_int_set_si(hull->eq[k][j], -1);
813 isl_int_set_si(hull->eq[k][1+dim+j], 1);
814 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
815 }
816 hull = isl_basic_set_set_rational(hull);
817 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
818 hull = isl_basic_set_remove_redundancies(hull);
819 isl_basic_set_free(bset1);
820 isl_basic_set_free(bset2);
821 return hull;
822error:
823 isl_basic_set_free(bset1);
824 isl_basic_set_free(bset2);
825 isl_basic_set_free(hull);
826 return NULL;
827}
828
829/* Is the set bounded for each value of the parameters?
830 */
831isl_bool isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
832{
833 struct isl_tab *tab;
834 isl_bool bounded;
835
836 if (!bset)
837 return isl_bool_error;
838 if (isl_basic_set_plain_is_empty(bset))
839 return isl_bool_true;
840
841 tab = isl_tab_from_recession_cone(bset, 1);
842 bounded = isl_tab_cone_is_bounded(tab);
843 isl_tab_free(tab);
844 return bounded;
845}
846
847/* Is the image bounded for each value of the parameters and
848 * the domain variables?
849 */
850isl_bool isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
851{
852 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
853 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
854 isl_bool bounded;
855
856 bmap = isl_basic_map_copy(bmap);
857 bmap = isl_basic_map_cow(bmap);
858 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
859 isl_dim_in, 0, n_in);
860 bounded = isl_basic_set_is_bounded(bset_from_bmap(bmap));
861 isl_basic_map_free(bmap);
862
863 return bounded;
864}
865
866/* Is the set bounded for each value of the parameters?
867 */
868isl_bool isl_set_is_bounded(__isl_keep isl_set *set)
869{
870 int i;
871
872 if (!set)
873 return isl_bool_error;
874
875 for (i = 0; i < set->n; ++i) {
876 isl_bool bounded = isl_basic_set_is_bounded(set->p[i]);
877 if (!bounded || bounded < 0)
878 return bounded;
879 }
880 return isl_bool_true;
881}
882
883/* Compute the lineality space of the convex hull of bset1 and bset2.
884 *
885 * We first compute the intersection of the recession cone of bset1
886 * with the negative of the recession cone of bset2 and then compute
887 * the linear hull of the resulting cone.
888 */
889static __isl_give isl_basic_set *induced_lineality_space(
890 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
891{
892 int i, k;
893 struct isl_basic_set *lin = NULL;
894 unsigned dim;
895
896 if (!bset1 || !bset2)
897 goto error;
898
899 dim = isl_basic_set_total_dim(bset1);
900 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
901 bset1->n_eq + bset2->n_eq,
902 bset1->n_ineq + bset2->n_ineq);
903 lin = isl_basic_set_set_rational(lin);
904 if (!lin)
905 goto error;
906 for (i = 0; i < bset1->n_eq; ++i) {
907 k = isl_basic_set_alloc_equality(lin);
908 if (k < 0)
909 goto error;
910 isl_int_set_si(lin->eq[k][0], 0);
911 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
912 }
913 for (i = 0; i < bset1->n_ineq; ++i) {
914 k = isl_basic_set_alloc_inequality(lin);
915 if (k < 0)
916 goto error;
917 isl_int_set_si(lin->ineq[k][0], 0);
918 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
919 }
920 for (i = 0; i < bset2->n_eq; ++i) {
921 k = isl_basic_set_alloc_equality(lin);
922 if (k < 0)
923 goto error;
924 isl_int_set_si(lin->eq[k][0], 0);
925 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
926 }
927 for (i = 0; i < bset2->n_ineq; ++i) {
928 k = isl_basic_set_alloc_inequality(lin);
929 if (k < 0)
930 goto error;
931 isl_int_set_si(lin->ineq[k][0], 0);
932 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
933 }
934
935 isl_basic_set_free(bset1);
936 isl_basic_set_free(bset2);
937 return isl_basic_set_affine_hull(lin);
938error:
939 isl_basic_set_free(lin);
940 isl_basic_set_free(bset1);
941 isl_basic_set_free(bset2);
942 return NULL;
943}
944
945static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set);
946
947/* Given a set and a linear space "lin" of dimension n > 0,
948 * project the linear space from the set, compute the convex hull
949 * and then map the set back to the original space.
950 *
951 * Let
952 *
953 * M x = 0
954 *
955 * describe the linear space. We first compute the Hermite normal
956 * form H = M U of M = H Q, to obtain
957 *
958 * H Q x = 0
959 *
960 * The last n rows of H will be zero, so the last n variables of x' = Q x
961 * are the one we want to project out. We do this by transforming each
962 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
963 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
964 * we transform the hull back to the original space as A' Q_1 x >= b',
965 * with Q_1 all but the last n rows of Q.
966 */
967static __isl_give isl_basic_set *modulo_lineality(__isl_take isl_set *set,
968 __isl_take isl_basic_set *lin)
969{
970 unsigned total = isl_basic_set_total_dim(lin);
971 unsigned lin_dim;
972 struct isl_basic_set *hull;
973 struct isl_mat *M, *U, *Q;
974
975 if (!set || !lin)
976 goto error;
977 lin_dim = total - lin->n_eq;
978 M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
979 M = isl_mat_left_hermite(M, 0, &U, &Q);
980 if (!M)
981 goto error;
982 isl_mat_free(M);
983 isl_basic_set_free(lin);
984
985 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
986
987 U = isl_mat_lin_to_aff(U);
988 Q = isl_mat_lin_to_aff(Q);
989
990 set = isl_set_preimage(set, U);
991 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
992 hull = uset_convex_hull(set);
993 hull = isl_basic_set_preimage(hull, Q);
994
995 return hull;
996error:
997 isl_basic_set_free(lin);
998 isl_set_free(set);
999 return NULL;
1000}
1001
1002/* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1003 * set up an LP for solving
1004 *
1005 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1006 *
1007 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1008 * The next \alpha{ij} correspond to the equalities and come in pairs.
1009 * The final \alpha{ij} correspond to the inequalities.
1010 */
1011static __isl_give isl_basic_set *valid_direction_lp(
1012 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1013{
1014 isl_space *dim;
1015 struct isl_basic_set *lp;
1016 unsigned d;
1017 int n;
1018 int i, j, k;
1019
1020 if (!bset1 || !bset2)
1021 goto error;
1022 d = 1 + isl_basic_set_total_dim(bset1);
1023 n = 2 +
1024 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1025 dim = isl_space_set_alloc(bset1->ctx, 0, n);
1026 lp = isl_basic_set_alloc_space(dim, 0, d, n);
1027 if (!lp)
1028 goto error;
1029 for (i = 0; i < n; ++i) {
1030 k = isl_basic_set_alloc_inequality(lp);
1031 if (k < 0)
1032 goto error;
1033 isl_seq_clr(lp->ineq[k] + 1, n);
1034 isl_int_set_si(lp->ineq[k][0], -1);
1035 isl_int_set_si(lp->ineq[k][1 + i], 1);
1036 }
1037 for (i = 0; i < d; ++i) {
1038 k = isl_basic_set_alloc_equality(lp);
1039 if (k < 0)
1040 goto error;
1041 n = 0;
1042 isl_int_set_si(lp->eq[k][n], 0); n++;
1043 /* positivity constraint 1 >= 0 */
1044 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1045 for (j = 0; j < bset1->n_eq; ++j) {
1046 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1047 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1048 }
1049 for (j = 0; j < bset1->n_ineq; ++j) {
1050 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1051 }
1052 /* positivity constraint 1 >= 0 */
1053 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1054 for (j = 0; j < bset2->n_eq; ++j) {
1055 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1056 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1057 }
1058 for (j = 0; j < bset2->n_ineq; ++j) {
1059 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1060 }
1061 }
1062 lp = isl_basic_set_gauss(lp, NULL);
1063 isl_basic_set_free(bset1);
1064 isl_basic_set_free(bset2);
1065 return lp;
1066error:
1067 isl_basic_set_free(bset1);
1068 isl_basic_set_free(bset2);
1069 return NULL;
1070}
1071
1072/* Compute a vector s in the homogeneous space such that <s, r> > 0
1073 * for all rays in the homogeneous space of the two cones that correspond
1074 * to the input polyhedra bset1 and bset2.
1075 *
1076 * We compute s as a vector that satisfies
1077 *
1078 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1079 *
1080 * with h_{ij} the normals of the facets of polyhedron i
1081 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1082 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1083 * We first set up an LP with as variables the \alpha{ij}.
1084 * In this formulation, for each polyhedron i,
1085 * the first constraint is the positivity constraint, followed by pairs
1086 * of variables for the equalities, followed by variables for the inequalities.
1087 * We then simply pick a feasible solution and compute s using (*).
1088 *
1089 * Note that we simply pick any valid direction and make no attempt
1090 * to pick a "good" or even the "best" valid direction.
1091 */
1092static __isl_give isl_vec *valid_direction(
1093 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1094{
1095 struct isl_basic_set *lp;
1096 struct isl_tab *tab;
1097 struct isl_vec *sample = NULL;
1098 struct isl_vec *dir;
1099 unsigned d;
1100 int i;
1101 int n;
1102
1103 if (!bset1 || !bset2)
1104 goto error;
1105 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1106 isl_basic_set_copy(bset2));
1107 tab = isl_tab_from_basic_set(lp, 0);
1108 sample = isl_tab_get_sample_value(tab);
1109 isl_tab_free(tab);
1110 isl_basic_set_free(lp);
1111 if (!sample)
1112 goto error;
1113 d = isl_basic_set_total_dim(bset1);
1114 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1115 if (!dir)
1116 goto error;
1117 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1118 n = 1;
1119 /* positivity constraint 1 >= 0 */
1120 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1121 for (i = 0; i < bset1->n_eq; ++i) {
1122 isl_int_sub(sample->block.data[n],
1123 sample->block.data[n], sample->block.data[n+1]);
1124 isl_seq_combine(dir->block.data,
1125 bset1->ctx->one, dir->block.data,
1126 sample->block.data[n], bset1->eq[i], 1 + d);
1127
1128 n += 2;
1129 }
1130 for (i = 0; i < bset1->n_ineq; ++i)
1131 isl_seq_combine(dir->block.data,
1132 bset1->ctx->one, dir->block.data,
1133 sample->block.data[n++], bset1->ineq[i], 1 + d);
1134 isl_vec_free(sample);
1135 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1136 isl_basic_set_free(bset1);
1137 isl_basic_set_free(bset2);
1138 return dir;
1139error:
1140 isl_vec_free(sample);
1141 isl_basic_set_free(bset1);
1142 isl_basic_set_free(bset2);
1143 return NULL;
1144}
1145
1146/* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1147 * compute b_i' + A_i' x' >= 0, with
1148 *
1149 * [ b_i A_i ] [ y' ] [ y' ]
1150 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1151 *
1152 * In particular, add the "positivity constraint" and then perform
1153 * the mapping.
1154 */
1155static __isl_give isl_basic_set *homogeneous_map(__isl_take isl_basic_set *bset,
1156 __isl_take isl_mat *T)
1157{
1158 int k;
1159
1160 if (!bset)
1161 goto error;
1162 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1163 k = isl_basic_set_alloc_inequality(bset);
1164 if (k < 0)
1165 goto error;
1166 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1167 isl_int_set_si(bset->ineq[k][0], 1);
1168 bset = isl_basic_set_preimage(bset, T);
1169 return bset;
1170error:
1171 isl_mat_free(T);
1172 isl_basic_set_free(bset);
1173 return NULL;
1174}
1175
1176/* Compute the convex hull of a pair of basic sets without any parameters or
1177 * integer divisions, where the convex hull is known to be pointed,
1178 * but the basic sets may be unbounded.
1179 *
1180 * We turn this problem into the computation of a convex hull of a pair
1181 * _bounded_ polyhedra by "changing the direction of the homogeneous
1182 * dimension". This idea is due to Matthias Koeppe.
1183 *
1184 * Consider the cones in homogeneous space that correspond to the
1185 * input polyhedra. The rays of these cones are also rays of the
1186 * polyhedra if the coordinate that corresponds to the homogeneous
1187 * dimension is zero. That is, if the inner product of the rays
1188 * with the homogeneous direction is zero.
1189 * The cones in the homogeneous space can also be considered to
1190 * correspond to other pairs of polyhedra by chosing a different
1191 * homogeneous direction. To ensure that both of these polyhedra
1192 * are bounded, we need to make sure that all rays of the cones
1193 * correspond to vertices and not to rays.
1194 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1195 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1196 * The vector s is computed in valid_direction.
1197 *
1198 * Note that we need to consider _all_ rays of the cones and not just
1199 * the rays that correspond to rays in the polyhedra. If we were to
1200 * only consider those rays and turn them into vertices, then we
1201 * may inadvertently turn some vertices into rays.
1202 *
1203 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1204 * We therefore transform the two polyhedra such that the selected
1205 * direction is mapped onto this standard direction and then proceed
1206 * with the normal computation.
1207 * Let S be a non-singular square matrix with s as its first row,
1208 * then we want to map the polyhedra to the space
1209 *
1210 * [ y' ] [ y ] [ y ] [ y' ]
1211 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1212 *
1213 * We take S to be the unimodular completion of s to limit the growth
1214 * of the coefficients in the following computations.
1215 *
1216 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1217 * We first move to the homogeneous dimension
1218 *
1219 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1220 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1221 *
1222 * Then we change directoin
1223 *
1224 * [ b_i A_i ] [ y' ] [ y' ]
1225 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1226 *
1227 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1228 * resulting in b' + A' x' >= 0, which we then convert back
1229 *
1230 * [ y ] [ y ]
1231 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1232 *
1233 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1234 */
1235static __isl_give isl_basic_set *convex_hull_pair_pointed(
1236 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1237{
1238 struct isl_ctx *ctx = NULL;
1239 struct isl_vec *dir = NULL;
1240 struct isl_mat *T = NULL;
1241 struct isl_mat *T2 = NULL;
1242 struct isl_basic_set *hull;
1243 struct isl_set *set;
1244
1245 if (!bset1 || !bset2)
1246 goto error;
1247 ctx = isl_basic_set_get_ctx(bset1);
1248 dir = valid_direction(isl_basic_set_copy(bset1),
1249 isl_basic_set_copy(bset2));
1250 if (!dir)
1251 goto error;
1252 T = isl_mat_alloc(ctx, dir->size, dir->size);
1253 if (!T)
1254 goto error;
1255 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1256 T = isl_mat_unimodular_complete(T, 1);
1257 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1258
1259 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1260 bset2 = homogeneous_map(bset2, T2);
1261 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1262 set = isl_set_add_basic_set(set, bset1);
1263 set = isl_set_add_basic_set(set, bset2);
1264 hull = uset_convex_hull(set);
1265 hull = isl_basic_set_preimage(hull, T);
1266
1267 isl_vec_free(dir);
1268
1269 return hull;
1270error:
1271 isl_vec_free(dir);
1272 isl_basic_set_free(bset1);
1273 isl_basic_set_free(bset2);
1274 return NULL;
1275}
1276
1277static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set);
1278static __isl_give isl_basic_set *modulo_affine_hull(
1279 __isl_take isl_set *set, __isl_take isl_basic_set *affine_hull);
1280
1281/* Compute the convex hull of a pair of basic sets without any parameters or
1282 * integer divisions.
1283 *
1284 * This function is called from uset_convex_hull_unbounded, which
1285 * means that the complete convex hull is unbounded. Some pairs
1286 * of basic sets may still be bounded, though.
1287 * They may even lie inside a lower dimensional space, in which
1288 * case they need to be handled inside their affine hull since
1289 * the main algorithm assumes that the result is full-dimensional.
1290 *
1291 * If the convex hull of the two basic sets would have a non-trivial
1292 * lineality space, we first project out this lineality space.
1293 */
1294static __isl_give isl_basic_set *convex_hull_pair(
1295 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
1296{
1297 isl_basic_set *lin, *aff;
1298 int bounded1, bounded2;
1299
1300 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1301 return convex_hull_pair_elim(bset1, bset2);
1302
1303 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1304 isl_basic_set_copy(bset2)));
1305 if (!aff)
1306 goto error;
1307 if (aff->n_eq != 0)
1308 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1309 isl_basic_set_free(aff);
1310
1311 bounded1 = isl_basic_set_is_bounded(bset1);
1312 bounded2 = isl_basic_set_is_bounded(bset2);
1313
1314 if (bounded1 < 0 || bounded2 < 0)
1315 goto error;
1316
1317 if (bounded1 && bounded2)
1318 return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1319
1320 if (bounded1 || bounded2)
1321 return convex_hull_pair_pointed(bset1, bset2);
1322
1323 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1324 isl_basic_set_copy(bset2));
1325 if (!lin)
1326 goto error;
1327 if (isl_basic_set_plain_is_universe(lin)) {
1328 isl_basic_set_free(bset1);
1329 isl_basic_set_free(bset2);
1330 return lin;
1331 }
1332 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1333 struct isl_set *set;
1334 set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
1335 set = isl_set_add_basic_set(set, bset1);
1336 set = isl_set_add_basic_set(set, bset2);
1337 return modulo_lineality(set, lin);
1338 }
1339 isl_basic_set_free(lin);
1340
1341 return convex_hull_pair_pointed(bset1, bset2);
1342error:
1343 isl_basic_set_free(bset1);
1344 isl_basic_set_free(bset2);
1345 return NULL;
1346}
1347
1348/* Compute the lineality space of a basic set.
1349 * We basically just drop the constants and turn every inequality
1350 * into an equality.
1351 * Any explicit representations of local variables are removed
1352 * because they may no longer be valid representations
1353 * in the lineality space.
1354 */
1355__isl_give isl_basic_set *isl_basic_set_lineality_space(
1356 __isl_take isl_basic_set *bset)
1357{
1358 int i, k;
1359 struct isl_basic_set *lin = NULL;
1360 unsigned n_div, dim;
1361
1362 if (!bset)
1363 goto error;
1364 n_div = isl_basic_set_dim(bset, isl_dim_div);
1365 dim = isl_basic_set_total_dim(bset);
1366
1367 lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset),
1368 n_div, dim, 0);
1369 for (i = 0; i < n_div; ++i)
1370 if (isl_basic_set_alloc_div(lin) < 0)
1371 goto error;
1372 if (!lin)
1373 goto error;
1374 for (i = 0; i < bset->n_eq; ++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->eq[i] + 1, dim);
1380 }
1381 lin = isl_basic_set_gauss(lin, NULL);
1382 if (!lin)
1383 goto error;
1384 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1385 k = isl_basic_set_alloc_equality(lin);
1386 if (k < 0)
1387 goto error;
1388 isl_int_set_si(lin->eq[k][0], 0);
1389 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1390 lin = isl_basic_set_gauss(lin, NULL);
1391 if (!lin)
1392 goto error;
1393 }
1394 isl_basic_set_free(bset);
1395 return lin;
1396error:
1397 isl_basic_set_free(lin);
1398 isl_basic_set_free(bset);
1399 return NULL;
1400}
1401
1402/* Compute the (linear) hull of the lineality spaces of the basic sets in the
1403 * set "set".
1404 */
1405__isl_give isl_basic_set *isl_set_combined_lineality_space(
1406 __isl_take isl_set *set)
1407{
1408 int i;
1409 struct isl_set *lin = NULL;
1410
1411 if (!set)
1412 return NULL;
1413 if (set->n == 0) {
1414 isl_space *space = isl_set_get_space(set);
1415 isl_set_free(set);
1416 return isl_basic_set_empty(space);
1417 }
1418
1419 lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
1420 for (i = 0; i < set->n; ++i)
1421 lin = isl_set_add_basic_set(lin,
1422 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1423 isl_set_free(set);
1424 return isl_set_affine_hull(lin);
1425}
1426
1427/* Compute the convex hull of a set without any parameters or
1428 * integer divisions.
1429 * In each step, we combined two basic sets until only one
1430 * basic set is left.
1431 * The input basic sets are assumed not to have a non-trivial
1432 * lineality space. If any of the intermediate results has
1433 * a non-trivial lineality space, it is projected out.
1434 */
1435static __isl_give isl_basic_set *uset_convex_hull_unbounded(
1436 __isl_take isl_set *set)
1437{
1438 isl_basic_set_list *list;
1439
1440 list = isl_set_get_basic_set_list(set);
1441 isl_set_free(set);
1442
1443 while (list) {
1444 int n;
1445 struct isl_basic_set *t;
1446 isl_basic_set *bset1, *bset2;
1447
1448 n = isl_basic_set_list_n_basic_set(list);
1449 if (n < 2)
1450 isl_die(isl_basic_set_list_get_ctx(list),
1451 isl_error_internal,
1452 "expecting at least two elements", goto error);
1453 bset1 = isl_basic_set_list_get_basic_set(list, n - 1);
1454 bset2 = isl_basic_set_list_get_basic_set(list, n - 2);
1455 bset1 = convex_hull_pair(bset1, bset2);
1456 if (n == 2) {
1457 isl_basic_set_list_free(list);
1458 return bset1;
1459 }
1460 bset1 = isl_basic_set_underlying_set(bset1);
1461 list = isl_basic_set_list_drop(list, n - 2, 2);
1462 list = isl_basic_set_list_add(list, bset1);
1463
1464 t = isl_basic_set_list_get_basic_set(list, n - 2);
1465 t = isl_basic_set_lineality_space(t);
1466 if (!t)
1467 goto error;
1468 if (isl_basic_set_plain_is_universe(t)) {
1469 isl_basic_set_list_free(list);
1470 return t;
1471 }
1472 if (t->n_eq < isl_basic_set_total_dim(t)) {
1473 set = isl_basic_set_list_union(list);
1474 return modulo_lineality(set, t);
1475 }
1476 isl_basic_set_free(t);
1477 }
1478
1479 return NULL;
1480error:
1481 isl_basic_set_list_free(list);
1482 return NULL;
1483}
1484
1485/* Compute an initial hull for wrapping containing a single initial
1486 * facet.
1487 * This function assumes that the given set is bounded.
1488 */
1489static __isl_give isl_basic_set *initial_hull(__isl_take isl_basic_set *hull,
1490 __isl_keep isl_set *set)
1491{
1492 struct isl_mat *bounds = NULL;
1493 unsigned dim;
1494 int k;
1495
1496 if (!hull)
1497 goto error;
1498 bounds = initial_facet_constraint(set);
1499 if (!bounds)
1500 goto error;
1501 k = isl_basic_set_alloc_inequality(hull);
1502 if (k < 0)
1503 goto error;
1504 dim = isl_set_n_dim(set);
1505 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1506 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1507 isl_mat_free(bounds);
1508
1509 return hull;
1510error:
1511 isl_basic_set_free(hull);
1512 isl_mat_free(bounds);
1513 return NULL;
1514}
1515
1516struct max_constraint {
1517 struct isl_mat *c;
1518 int count;
1519 int ineq;
1520};
1521
1522static int max_constraint_equal(const void *entry, const void *val)
1523{
1524 struct max_constraint *a = (struct max_constraint *)entry;
1525 isl_int *b = (isl_int *)val;
1526
1527 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1528}
1529
1530static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1531 isl_int *con, unsigned len, int n, int ineq)
1532{
1533 struct isl_hash_table_entry *entry;
1534 struct max_constraint *c;
1535 uint32_t c_hash;
1536
1537 c_hash = isl_seq_get_hash(con + 1, len);
1538 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1539 con + 1, 0);
1540 if (!entry)
1541 return;
1542 c = entry->data;
1543 if (c->count < n) {
1544 isl_hash_table_remove(ctx, table, entry);
1545 return;
1546 }
1547 c->count++;
1548 if (isl_int_gt(c->c->row[0][0], con[0]))
1549 return;
1550 if (isl_int_eq(c->c->row[0][0], con[0])) {
1551 if (ineq)
1552 c->ineq = ineq;
1553 return;
1554 }
1555 c->c = isl_mat_cow(c->c);
1556 isl_int_set(c->c->row[0][0], con[0]);
1557 c->ineq = ineq;
1558}
1559
1560/* Check whether the constraint hash table "table" contains the constraint
1561 * "con".
1562 */
1563static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1564 isl_int *con, unsigned len, int n)
1565{
1566 struct isl_hash_table_entry *entry;
1567 struct max_constraint *c;
1568 uint32_t c_hash;
1569
1570 c_hash = isl_seq_get_hash(con + 1, len);
1571 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1572 con + 1, 0);
1573 if (!entry)
1574 return 0;
1575 c = entry->data;
1576 if (c->count < n)
1577 return 0;
1578 return isl_int_eq(c->c->row[0][0], con[0]);
1579}
1580
1581/* Check for inequality constraints of a basic set without equalities
1582 * such that the same or more stringent copies of the constraint appear
1583 * in all of the basic sets. Such constraints are necessarily facet
1584 * constraints of the convex hull.
1585 *
1586 * If the resulting basic set is by chance identical to one of
1587 * the basic sets in "set", then we know that this basic set contains
1588 * all other basic sets and is therefore the convex hull of set.
1589 * In this case we set *is_hull to 1.
1590 */
1591static __isl_give isl_basic_set *common_constraints(
1592 __isl_take isl_basic_set *hull, __isl_keep isl_set *set, int *is_hull)
1593{
1594 int i, j, s, n;
1595 int min_constraints;
1596 int best;
1597 struct max_constraint *constraints = NULL;
1598 struct isl_hash_table *table = NULL;
1599 unsigned total;
1600
1601 *is_hull = 0;
1602
1603 for (i = 0; i < set->n; ++i)
1604 if (set->p[i]->n_eq == 0)
1605 break;
1606 if (i >= set->n)
1607 return hull;
1608 min_constraints = set->p[i]->n_ineq;
1609 best = i;
1610 for (i = best + 1; i < set->n; ++i) {
1611 if (set->p[i]->n_eq != 0)
1612 continue;
1613 if (set->p[i]->n_ineq >= min_constraints)
1614 continue;
1615 min_constraints = set->p[i]->n_ineq;
1616 best = i;
1617 }
1618 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1619 min_constraints);
1620 if (!constraints)
1621 return hull;
1622 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1623 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1624 goto error;
1625
1626 total = isl_space_dim(set->dim, isl_dim_all);
1627 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1628 constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
1629 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1630 if (!constraints[i].c)
1631 goto error;
1632 constraints[i].ineq = 1;
1633 }
1634 for (i = 0; i < min_constraints; ++i) {
1635 struct isl_hash_table_entry *entry;
1636 uint32_t c_hash;
1637 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1638 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1639 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1640 if (!entry)
1641 goto error;
1642 isl_assert(hull->ctx, !entry->data, goto error);
1643 entry->data = &constraints[i];
1644 }
1645
1646 n = 0;
1647 for (s = 0; s < set->n; ++s) {
1648 if (s == best)
1649 continue;
1650
1651 for (i = 0; i < set->p[s]->n_eq; ++i) {
1652 isl_int *eq = set->p[s]->eq[i];
1653 for (j = 0; j < 2; ++j) {
1654 isl_seq_neg(eq, eq, 1 + total);
1655 update_constraint(hull->ctx, table,
1656 eq, total, n, 0);
1657 }
1658 }
1659 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1660 isl_int *ineq = set->p[s]->ineq[i];
1661 update_constraint(hull->ctx, table, ineq, total, n,
1662 set->p[s]->n_eq == 0);
1663 }
1664 ++n;
1665 }
1666
1667 for (i = 0; i < min_constraints; ++i) {
1668 if (constraints[i].count < n)
1669 continue;
1670 if (!constraints[i].ineq)
1671 continue;
1672 j = isl_basic_set_alloc_inequality(hull);
1673 if (j < 0)
1674 goto error;
1675 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1676 }
1677
1678 for (s = 0; s < set->n; ++s) {
1679 if (set->p[s]->n_eq)
1680 continue;
1681 if (set->p[s]->n_ineq != hull->n_ineq)
1682 continue;
1683 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1684 isl_int *ineq = set->p[s]->ineq[i];
1685 if (!has_constraint(hull->ctx, table, ineq, total, n))
1686 break;
1687 }
1688 if (i == set->p[s]->n_ineq)
1689 *is_hull = 1;
1690 }
1691
1692 isl_hash_table_clear(table);
1693 for (i = 0; i < min_constraints; ++i)
1694 isl_mat_free(constraints[i].c);
1695 free(constraints);
1696 free(table);
1697 return hull;
1698error:
1699 isl_hash_table_clear(table);
1700 free(table);
1701 if (constraints)
1702 for (i = 0; i < min_constraints; ++i)
1703 isl_mat_free(constraints[i].c);
1704 free(constraints);
1705 return hull;
1706}
1707
1708/* Create a template for the convex hull of "set" and fill it up
1709 * obvious facet constraints, if any. If the result happens to
1710 * be the convex hull of "set" then *is_hull is set to 1.
1711 */
1712static __isl_give isl_basic_set *proto_hull(__isl_keep isl_set *set,
1713 int *is_hull)
1714{
1715 struct isl_basic_set *hull;
1716 unsigned n_ineq;
1717 int i;
1718
1719 n_ineq = 1;
1720 for (i = 0; i < set->n; ++i) {
1721 n_ineq += set->p[i]->n_eq;
1722 n_ineq += set->p[i]->n_ineq;
1723 }
1724 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
1725 hull = isl_basic_set_set_rational(hull);
1726 if (!hull)
1727 return NULL;
1728 return common_constraints(hull, set, is_hull);
1729}
1730
1731static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set)
1732{
1733 struct isl_basic_set *hull;
1734 int is_hull;
1735
1736 hull = proto_hull(set, &is_hull);
1737 if (hull && !is_hull) {
1738 if (hull->n_ineq == 0)
1739 hull = initial_hull(hull, set);
1740 hull = extend(hull, set);
1741 }
1742 isl_set_free(set);
1743
1744 return hull;
1745}
1746
1747/* Compute the convex hull of a set without any parameters or
1748 * integer divisions. Depending on whether the set is bounded,
1749 * we pass control to the wrapping based convex hull or
1750 * the Fourier-Motzkin elimination based convex hull.
1751 * We also handle a few special cases before checking the boundedness.
1752 */
1753static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set)
1754{
1755 isl_bool bounded;
1756 struct isl_basic_set *convex_hull = NULL;
1757 struct isl_basic_set *lin;
1758
1759 if (isl_set_n_dim(set) == 0)
1760 return convex_hull_0d(set);
1761
1762 set = isl_set_coalesce(set);
1763 set = isl_set_set_rational(set);
1764
1765 if (!set)
1766 return NULL;
1767 if (set->n == 1) {
1768 convex_hull = isl_basic_set_copy(set->p[0]);
1769 isl_set_free(set);
1770 return convex_hull;
1771 }
1772 if (isl_set_n_dim(set) == 1)
1773 return convex_hull_1d(set);
1774
1775 bounded = isl_set_is_bounded(set);
1776 if (bounded < 0)
1777 goto error;
1778 if (bounded && set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1779 return uset_convex_hull_wrap(set);
1780
1781 lin = isl_set_combined_lineality_space(isl_set_copy(set));
1782 if (!lin)
1783 goto error;
1784 if (isl_basic_set_plain_is_universe(lin)) {
1785 isl_set_free(set);
1786 return lin;
1787 }
1788 if (lin->n_eq < isl_basic_set_total_dim(lin))
1789 return modulo_lineality(set, lin);
1790 isl_basic_set_free(lin);
1791
1792 return uset_convex_hull_unbounded(set);
1793error:
1794 isl_set_free(set);
1795 isl_basic_set_free(convex_hull);
1796 return NULL;
1797}
1798
1799/* This is the core procedure, where "set" is a "pure" set, i.e.,
1800 * without parameters or divs and where the convex hull of set is
1801 * known to be full-dimensional.
1802 */
1803static __isl_give isl_basic_set *uset_convex_hull_wrap_bounded(
1804 __isl_take isl_set *set)
1805{
1806 struct isl_basic_set *convex_hull = NULL;
1807
1808 if (!set)
1809 goto error;
1810
1811 if (isl_set_n_dim(set) == 0) {
1812 convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
1813 isl_set_free(set);
1814 convex_hull = isl_basic_set_set_rational(convex_hull);
1815 return convex_hull;
1816 }
1817
1818 set = isl_set_set_rational(set);
1819 set = isl_set_coalesce(set);
1820 if (!set)
1821 goto error;
1822 if (set->n == 1) {
1823 convex_hull = isl_basic_set_copy(set->p[0]);
1824 isl_set_free(set);
1825 convex_hull = isl_basic_map_remove_redundancies(convex_hull);
1826 return convex_hull;
1827 }
1828 if (isl_set_n_dim(set) == 1)
1829 return convex_hull_1d(set);
1830
1831 return uset_convex_hull_wrap(set);
1832error:
1833 isl_set_free(set);
1834 return NULL;
1835}
1836
1837/* Compute the convex hull of set "set" with affine hull "affine_hull",
1838 * We first remove the equalities (transforming the set), compute the
1839 * convex hull of the transformed set and then add the equalities back
1840 * (after performing the inverse transformation.
1841 */
1842static __isl_give isl_basic_set *modulo_affine_hull(
1843 __isl_take isl_set *set, __isl_take isl_basic_set *affine_hull)
1844{
1845 struct isl_mat *T;
1846 struct isl_mat *T2;
1847 struct isl_basic_set *dummy;
1848 struct isl_basic_set *convex_hull;
1849
1850 dummy = isl_basic_set_remove_equalities(
1851 isl_basic_set_copy(affine_hull), &T, &T2);
1852 if (!dummy)
1853 goto error;
1854 isl_basic_set_free(dummy);
1855 set = isl_set_preimage(set, T);
1856 convex_hull = uset_convex_hull(set);
1857 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1858 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1859 return convex_hull;
1860error:
1861 isl_mat_free(T);
1862 isl_mat_free(T2);
1863 isl_basic_set_free(affine_hull);
1864 isl_set_free(set);
1865 return NULL;
1866}
1867
1868/* Return an empty basic map living in the same space as "map".
1869 */
1870static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1871 __isl_take isl_map *map)
1872{
1873 isl_space *space;
1874
1875 space = isl_map_get_space(map);
1876 isl_map_free(map);
1877 return isl_basic_map_empty(space);
1878}
1879
1880/* Compute the convex hull of a map.
1881 *
1882 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1883 * specifically, the wrapping of facets to obtain new facets.
1884 */
1885__isl_give isl_basic_map *isl_map_convex_hull(__isl_take isl_map *map)
1886{
1887 struct isl_basic_set *bset;
1888 struct isl_basic_map *model = NULL;
1889 struct isl_basic_set *affine_hull = NULL;
1890 struct isl_basic_map *convex_hull = NULL;
1891 struct isl_set *set = NULL;
1892
1893 map = isl_map_detect_equalities(map);
1894 map = isl_map_align_divs_internal(map);
1895 if (!map)
1896 goto error;
1897
1898 if (map->n == 0)
1899 return replace_map_by_empty_basic_map(map);
1900
1901 model = isl_basic_map_copy(map->p[0]);
1902 set = isl_map_underlying_set(map);
1903 if (!set)
1904 goto error;
1905
1906 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1907 if (!affine_hull)
1908 goto error;
1909 if (affine_hull->n_eq != 0)
1910 bset = modulo_affine_hull(set, affine_hull);
1911 else {
1912 isl_basic_set_free(affine_hull);
1913 bset = uset_convex_hull(set);
1914 }
1915
1916 convex_hull = isl_basic_map_overlying_set(bset, model);
1917 if (!convex_hull)
1918 return NULL;
1919
1920 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1921 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1922 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1923 return convex_hull;
1924error:
1925 isl_set_free(set);
1926 isl_basic_map_free(model);
1927 return NULL;
1928}
1929
1930struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1931{
1932 return bset_from_bmap(isl_map_convex_hull(set_to_map(set)));
1933}
1934
1935__isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1936{
1937 isl_basic_map *hull;
1938
1939 hull = isl_map_convex_hull(map);
1940 return isl_basic_map_remove_divs(hull);
1941}
1942
1943__isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1944{
1945 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set)));
1946}
1947
1948struct sh_data_entry {
1949 struct isl_hash_table *table;
1950 struct isl_tab *tab;
1951};
1952
1953/* Holds the data needed during the simple hull computation.
1954 * In particular,
1955 * n the number of basic sets in the original set
1956 * hull_table a hash table of already computed constraints
1957 * in the simple hull
1958 * p for each basic set,
1959 * table a hash table of the constraints
1960 * tab the tableau corresponding to the basic set
1961 */
1962struct sh_data {
1963 struct isl_ctx *ctx;
1964 unsigned n;
1965 struct isl_hash_table *hull_table;
1966 struct sh_data_entry p[1];
1967};
1968
1969static void sh_data_free(struct sh_data *data)
1970{
1971 int i;
1972
1973 if (!data)
1974 return;
1975 isl_hash_table_free(data->ctx, data->hull_table);
1976 for (i = 0; i < data->n; ++i) {
1977 isl_hash_table_free(data->ctx, data->p[i].table);
1978 isl_tab_free(data->p[i].tab);
1979 }
1980 free(data);
1981}
1982
1983struct ineq_cmp_data {
1984 unsigned len;
1985 isl_int *p;
1986};
1987
1988static int has_ineq(const void *entry, const void *val)
1989{
1990 isl_int *row = (isl_int *)entry;
1991 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
1992
1993 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
1994 isl_seq_is_neg(row + 1, v->p + 1, v->len);
1995}
1996
1997static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
1998 isl_int *ineq, unsigned len)
1999{
2000 uint32_t c_hash;
2001 struct ineq_cmp_data v;
2002 struct isl_hash_table_entry *entry;
2003
2004 v.len = len;
2005 v.p = ineq;
2006 c_hash = isl_seq_get_hash(ineq + 1, len);
2007 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2008 if (!entry)
2009 return - 1;
2010 entry->data = ineq;
2011 return 0;
2012}
2013
2014/* Fill hash table "table" with the constraints of "bset".
2015 * Equalities are added as two inequalities.
2016 * The value in the hash table is a pointer to the (in)equality of "bset".
2017 */
2018static int hash_basic_set(struct isl_hash_table *table,
2019 __isl_keep isl_basic_set *bset)
2020{
2021 int i, j;
2022 unsigned dim = isl_basic_set_total_dim(bset);
2023
2024 for (i = 0; i < bset->n_eq; ++i) {
2025 for (j = 0; j < 2; ++j) {
2026 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2027 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2028 return -1;
2029 }
2030 }
2031 for (i = 0; i < bset->n_ineq; ++i) {
2032 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2033 return -1;
2034 }
2035 return 0;
2036}
2037
2038static struct sh_data *sh_data_alloc(__isl_keep isl_set *set, unsigned n_ineq)
2039{
2040 struct sh_data *data;
2041 int i;
2042
2043 data = isl_calloc(set->ctx, struct sh_data,
2044 sizeof(struct sh_data) +
2045 (set->n - 1) * sizeof(struct sh_data_entry));
2046 if (!data)
2047 return NULL;
2048 data->ctx = set->ctx;
2049 data->n = set->n;
2050 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2051 if (!data->hull_table)
2052 goto error;
2053 for (i = 0; i < set->n; ++i) {
2054 data->p[i].table = isl_hash_table_alloc(set->ctx,
2055 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2056 if (!data->p[i].table)
2057 goto error;
2058 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2059 goto error;
2060 }
2061 return data;
2062error:
2063 sh_data_free(data);
2064 return NULL;
2065}
2066
2067/* Check if inequality "ineq" is a bound for basic set "j" or if
2068 * it can be relaxed (by increasing the constant term) to become
2069 * a bound for that basic set. In the latter case, the constant
2070 * term is updated.
2071 * Relaxation of the constant term is only allowed if "shift" is set.
2072 *
2073 * Return 1 if "ineq" is a bound
2074 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2075 * -1 if some error occurred
2076 */
2077static int is_bound(struct sh_data *data, __isl_keep isl_set *set, int j,
2078 isl_int *ineq, int shift)
2079{
2080 enum isl_lp_result res;
2081 isl_int opt;
2082
2083 if (!data->p[j].tab) {
2084 data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
2085 if (!data->p[j].tab)
2086 return -1;
2087 }
2088
2089 isl_int_init(opt);
2090
2091 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2092 &opt, NULL, 0);
2093 if (res == isl_lp_ok && isl_int_is_neg(opt)) {
2094 if (shift)
2095 isl_int_sub(ineq[0], ineq[0], opt);
2096 else
2097 res = isl_lp_unbounded;
2098 }
2099
2100 isl_int_clear(opt);
2101
2102 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2103 res == isl_lp_unbounded ? 0 : -1;
2104}
2105
2106/* Set the constant term of "ineq" to the maximum of those of the constraints
2107 * in the basic sets of "set" following "i" that are parallel to "ineq".
2108 * That is, if any of the basic sets of "set" following "i" have a more
2109 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2110 * "c_hash" is the hash value of the linear part of "ineq".
2111 * "v" has been set up for use by has_ineq.
2112 *
2113 * Note that the two inequality constraints corresponding to an equality are
2114 * represented by the same inequality constraint in data->p[j].table
2115 * (but with different hash values). This means the constraint (or at
2116 * least its constant term) may need to be temporarily negated to get
2117 * the actually hashed constraint.
2118 */
2119static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
2120 int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
2121{
2122 int j;
2123 isl_ctx *ctx;
2124 struct isl_hash_table_entry *entry;
2125
2126 ctx = isl_set_get_ctx(set);
2127 for (j = i + 1; j < set->n; ++j) {
2128 int neg;
2129 isl_int *ineq_j;
2130
2131 entry = isl_hash_table_find(ctx, data->p[j].table,
2132 c_hash, &has_ineq, v, 0);
2133 if (!entry)
2134 continue;
2135
2136 ineq_j = entry->data;
2137 neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
2138 if (neg)
2139 isl_int_neg(ineq_j[0], ineq_j[0]);
2140 if (isl_int_gt(ineq_j[0], ineq[0]))
2141 isl_int_set(ineq[0], ineq_j[0]);
2142 if (neg)
2143 isl_int_neg(ineq_j[0], ineq_j[0]);
2144 }
2145}
2146
2147/* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2148 * become a bound on the whole set. If so, add the (relaxed) inequality
2149 * to "hull". Relaxation is only allowed if "shift" is set.
2150 *
2151 * We first check if "hull" already contains a translate of the inequality.
2152 * If so, we are done.
2153 * Then, we check if any of the previous basic sets contains a translate
2154 * of the inequality. If so, then we have already considered this
2155 * inequality and we are done.
2156 * Otherwise, for each basic set other than "i", we check if the inequality
2157 * is a bound on the basic set, but first replace the constant term
2158 * by the maximal value of any translate of the inequality in any
2159 * of the following basic sets.
2160 * For previous basic sets, we know that they do not contain a translate
2161 * of the inequality, so we directly call is_bound.
2162 * For following basic sets, we first check if a translate of the
2163 * inequality appears in its description. If so, the constant term
2164 * of the inequality has already been updated with respect to this
2165 * translate and the inequality is therefore known to be a bound
2166 * of this basic set.
2167 */
2168static __isl_give isl_basic_set *add_bound(__isl_take isl_basic_set *hull,
2169 struct sh_data *data, __isl_keep isl_set *set, int i, isl_int *ineq,
2170 int shift)
2171{
2172 uint32_t c_hash;
2173 struct ineq_cmp_data v;
2174 struct isl_hash_table_entry *entry;
2175 int j, k;
2176
2177 if (!hull)
2178 return NULL;
2179
2180 v.len = isl_basic_set_total_dim(hull);
2181 v.p = ineq;
2182 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2183
2184 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2185 has_ineq, &v, 0);
2186 if (entry)
2187 return hull;
2188
2189 for (j = 0; j < i; ++j) {
2190 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2191 c_hash, has_ineq, &v, 0);
2192 if (entry)
2193 break;
2194 }
2195 if (j < i)
2196 return hull;
2197
2198 k = isl_basic_set_alloc_inequality(hull);
2199 if (k < 0)
2200 goto error;
2201 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2202
2203 set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
2204 for (j = 0; j < i; ++j) {
2205 int bound;
2206 bound = is_bound(data, set, j, hull->ineq[k], shift);
2207 if (bound < 0)
2208 goto error;
2209 if (!bound)
2210 break;
2211 }
2212 if (j < i) {
2213 isl_basic_set_free_inequality(hull, 1);
2214 return hull;
2215 }
2216
2217 for (j = i + 1; j < set->n; ++j) {
2218 int bound;
2219 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2220 c_hash, has_ineq, &v, 0);
2221 if (entry)
2222 continue;
2223 bound = is_bound(data, set, j, hull->ineq[k], shift);
2224 if (bound < 0)
2225 goto error;
2226 if (!bound)
2227 break;
2228 }
2229 if (j < set->n) {
2230 isl_basic_set_free_inequality(hull, 1);
2231 return hull;
2232 }
2233
2234 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2235 has_ineq, &v, 1);
2236 if (!entry)
2237 goto error;
2238 entry->data = hull->ineq[k];
2239
2240 return hull;
2241error:
2242 isl_basic_set_free(hull);
2243 return NULL;
2244}
2245
2246/* Check if any inequality from basic set "i" is or can be relaxed to
2247 * become a bound on the whole set. If so, add the (relaxed) inequality
2248 * to "hull". Relaxation is only allowed if "shift" is set.
2249 */
2250static __isl_give isl_basic_set *add_bounds(__isl_take isl_basic_set *bset,
2251 struct sh_data *data, __isl_keep isl_set *set, int i, int shift)
2252{
2253 int j, k;
2254 unsigned dim = isl_basic_set_total_dim(bset);
2255
2256 for (j = 0; j < set->p[i]->n_eq; ++j) {
2257 for (k = 0; k < 2; ++k) {
2258 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2259 bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
2260 shift);
2261 }
2262 }
2263 for (j = 0; j < set->p[i]->n_ineq; ++j)
2264 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
2265 return bset;
2266}
2267
2268/* Compute a superset of the convex hull of set that is described
2269 * by only (translates of) the constraints in the constituents of set.
2270 * Translation is only allowed if "shift" is set.
2271 */
2272static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
2273 int shift)
2274{
2275 struct sh_data *data = NULL;
2276 struct isl_basic_set *hull = NULL;
2277 unsigned n_ineq;
2278 int i;
2279
2280 if (!set)
2281 return NULL;
2282
2283 n_ineq = 0;
2284 for (i = 0; i < set->n; ++i) {
2285 if (!set->p[i])
2286 goto error;
2287 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2288 }
2289
2290 hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
2291 if (!hull)
2292 goto error;
2293
2294 data = sh_data_alloc(set, n_ineq);
2295 if (!data)
2296 goto error;
2297
2298 for (i = 0; i < set->n; ++i)
2299 hull = add_bounds(hull, data, set, i, shift);
2300
2301 sh_data_free(data);
2302 isl_set_free(set);
2303
2304 return hull;
2305error:
2306 sh_data_free(data);
2307 isl_basic_set_free(hull);
2308 isl_set_free(set);
2309 return NULL;
2310}
2311
2312/* Compute a superset of the convex hull of map that is described
2313 * by only (translates of) the constraints in the constituents of map.
2314 * Handle trivial cases where map is NULL or contains at most one disjunct.
2315 */
2316static __isl_give isl_basic_map *map_simple_hull_trivial(
2317 __isl_take isl_map *map)
2318{
2319 isl_basic_map *hull;
2320
2321 if (!map)
2322 return NULL;
2323 if (map->n == 0)
2324 return replace_map_by_empty_basic_map(map);
2325
2326 hull = isl_basic_map_copy(map->p[0]);
2327 isl_map_free(map);
2328 return hull;
2329}
2330
2331/* Return a copy of the simple hull cached inside "map".
2332 * "shift" determines whether to return the cached unshifted or shifted
2333 * simple hull.
2334 */
2335static __isl_give isl_basic_map *cached_simple_hull(__isl_take isl_map *map,
2336 int shift)
2337{
2338 isl_basic_map *hull;
2339
2340 hull = isl_basic_map_copy(map->cached_simple_hull[shift]);
2341 isl_map_free(map);
2342
2343 return hull;
2344}
2345
2346/* Compute a superset of the convex hull of map that is described
2347 * by only (translates of) the constraints in the constituents of map.
2348 * Translation is only allowed if "shift" is set.
2349 *
2350 * The constraints are sorted while removing redundant constraints
2351 * in order to indicate a preference of which constraints should
2352 * be preserved. In particular, pairs of constraints that are
2353 * sorted together are preferred to either both be preserved
2354 * or both be removed. The sorting is performed inside
2355 * isl_basic_map_remove_redundancies.
2356 *
2357 * The result of the computation is stored in map->cached_simple_hull[shift]
2358 * such that it can be reused in subsequent calls. The cache is cleared
2359 * whenever the map is modified (in isl_map_cow).
2360 * Note that the results need to be stored in the input map for there
2361 * to be any chance that they may get reused. In particular, they
2362 * are stored in a copy of the input map that is saved before
2363 * the integer division alignment.
2364 */
2365static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
2366 int shift)
2367{
2368 struct isl_set *set = NULL;
2369 struct isl_basic_map *model = NULL;
2370 struct isl_basic_map *hull;
2371 struct isl_basic_map *affine_hull;
2372 struct isl_basic_set *bset = NULL;
2373 isl_map *input;
2374
2375 if (!map || map->n <= 1)
2376 return map_simple_hull_trivial(map);
2377
2378 if (map->cached_simple_hull[shift])
2379 return cached_simple_hull(map, shift);
2380
2381 map = isl_map_detect_equalities(map);
2382 if (!map || map->n <= 1)
2383 return map_simple_hull_trivial(map);
2384 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2385 input = isl_map_copy(map);
2386 map = isl_map_align_divs_internal(map);
2387 model = map ? isl_basic_map_copy(map->p[0]) : NULL;
2388
2389 set = isl_map_underlying_set(map);
2390
2391 bset = uset_simple_hull(set, shift);
2392
2393 hull = isl_basic_map_overlying_set(bset, model);
2394
2395 hull = isl_basic_map_intersect(hull, affine_hull);
2396 hull = isl_basic_map_remove_redundancies(hull);
2397
2398 if (hull) {
2399 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2400 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2401 }
2402
2403 hull = isl_basic_map_finalize(hull);
2404 if (input)
2405 input->cached_simple_hull[shift] = isl_basic_map_copy(hull);
2406 isl_map_free(input);
2407
2408 return hull;
2409}
2410
2411/* Compute a superset of the convex hull of map that is described
2412 * by only translates of the constraints in the constituents of map.
2413 */
2414__isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
2415{
2416 return map_simple_hull(map, 1);
2417}
2418
2419struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2420{
2421 return bset_from_bmap(isl_map_simple_hull(set_to_map(set)));
2422}
2423
2424/* Compute a superset of the convex hull of map that is described
2425 * by only the constraints in the constituents of map.
2426 */
2427__isl_give isl_basic_map *isl_map_unshifted_simple_hull(
2428 __isl_take isl_map *map)
2429{
2430 return map_simple_hull(map, 0);
2431}
2432
2433__isl_give isl_basic_set *isl_set_unshifted_simple_hull(
2434 __isl_take isl_set *set)
2435{
2436 return isl_map_unshifted_simple_hull(set);
2437}
2438
2439/* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2440 * A constraint that appears with different constant terms
2441 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2442 * (i.e., greatest) constant term.
2443 * "bmap1" and "bmap2" are assumed to have the same (known)
2444 * integer divisions.
2445 * The constraints of both "bmap1" and "bmap2" are assumed
2446 * to have been sorted using isl_basic_map_sort_constraints.
2447 *
2448 * Run through the inequality constraints of "bmap1" and "bmap2"
2449 * in sorted order.
2450 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2451 * is removed.
2452 * If a match is found, the constraint is kept. If needed, the constant
2453 * term of the constraint is adjusted.
2454 */
2455static __isl_give isl_basic_map *select_shared_inequalities(
2456 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2457{
2458 int i1, i2;
2459
2460 bmap1 = isl_basic_map_cow(bmap1);
2461 if (!bmap1 || !bmap2)
2462 return isl_basic_map_free(bmap1);
2463
2464 i1 = bmap1->n_ineq - 1;
2465 i2 = bmap2->n_ineq - 1;
2466 while (bmap1 && i1 >= 0 && i2 >= 0) {
2467 int cmp;
2468
2469 cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
2470 bmap2->ineq[i2]);
2471 if (cmp < 0) {
2472 --i2;
2473 continue;
2474 }
2475 if (cmp > 0) {
2476 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2477 bmap1 = isl_basic_map_free(bmap1);
2478 --i1;
2479 continue;
2480 }
2481 if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
2482 isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
2483 --i1;
2484 --i2;
2485 }
2486 for (; i1 >= 0; --i1)
2487 if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
2488 bmap1 = isl_basic_map_free(bmap1);
2489
2490 return bmap1;
2491}
2492
2493/* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2494 * "bmap1" and "bmap2" are assumed to have the same (known)
2495 * integer divisions.
2496 *
2497 * Run through the equality constraints of "bmap1" and "bmap2".
2498 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2499 * is removed.
2500 */
2501static __isl_give isl_basic_map *select_shared_equalities(
2502 __isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
2503{
2504 int i1, i2;
2505 unsigned total;
2506
2507 bmap1 = isl_basic_map_cow(bmap1);
2508 if (!bmap1 || !bmap2)
2509 return isl_basic_map_free(bmap1);
2510
2511 total = isl_basic_map_total_dim(bmap1);
2512
2513 i1 = bmap1->n_eq - 1;
2514 i2 = bmap2->n_eq - 1;
2515 while (bmap1 && i1 >= 0 && i2 >= 0) {
2516 int last1, last2;
2517
2518 last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
2519 last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
2520 if (last1 > last2) {
2521 --i2;
2522 continue;
2523 }
2524 if (last1 < last2) {
2525 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2526 bmap1 = isl_basic_map_free(bmap1);
2527 --i1;
2528 continue;
2529 }
2530 if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
2531 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2532 bmap1 = isl_basic_map_free(bmap1);
2533 }
2534 --i1;
2535 --i2;
2536 }
2537 for (; i1 >= 0; --i1)
2538 if (isl_basic_map_drop_equality(bmap1, i1) < 0)
2539 bmap1 = isl_basic_map_free(bmap1);
2540
2541 return bmap1;
2542}
2543
2544/* Compute a superset of "bmap1" and "bmap2" that is described
2545 * by only the constraints that appear in both "bmap1" and "bmap2".
2546 *
2547 * First drop constraints that involve unknown integer divisions
2548 * since it is not trivial to check whether two such integer divisions
2549 * in different basic maps are the same.
2550 * Then align the remaining (known) divs and sort the constraints.
2551 * Finally drop all inequalities and equalities from "bmap1" that
2552 * do not also appear in "bmap2".
2553 */
2554__isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
2555 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
2556{
2557 bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
2558 bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
2559 bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
2560 bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
2561 bmap1 = isl_basic_map_gauss(bmap1, NULL);
2562 bmap2 = isl_basic_map_gauss(bmap2, NULL);
2563 bmap1 = isl_basic_map_sort_constraints(bmap1);
2564 bmap2 = isl_basic_map_sort_constraints(bmap2);
2565
2566 bmap1 = select_shared_inequalities(bmap1, bmap2);
2567 bmap1 = select_shared_equalities(bmap1, bmap2);
2568
2569 isl_basic_map_free(bmap2);
2570 bmap1 = isl_basic_map_finalize(bmap1);
2571 return bmap1;
2572}
2573
2574/* Compute a superset of the convex hull of "map" that is described
2575 * by only the constraints in the constituents of "map".
2576 * In particular, the result is composed of constraints that appear
2577 * in each of the basic maps of "map"
2578 *
2579 * Constraints that involve unknown integer divisions are dropped
2580 * since it is not trivial to check whether two such integer divisions
2581 * in different basic maps are the same.
2582 *
2583 * The hull is initialized from the first basic map and then
2584 * updated with respect to the other basic maps in turn.
2585 */
2586__isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
2587 __isl_take isl_map *map)
2588{
2589 int i;
2590 isl_basic_map *hull;
2591
2592 if (!map)
2593 return NULL;
2594 if (map->n <= 1)
2595 return map_simple_hull_trivial(map);
2596 map = isl_map_drop_constraint_involving_unknown_divs(map);
2597 hull = isl_basic_map_copy(map->p[0]);
2598 for (i = 1; i < map->n; ++i) {
2599 isl_basic_map *bmap_i;
2600
2601 bmap_i = isl_basic_map_copy(map->p[i]);
2602 hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
2603 }
2604
2605 isl_map_free(map);
2606 return hull;
2607}
2608
2609/* Compute a superset of the convex hull of "set" that is described
2610 * by only the constraints in the constituents of "set".
2611 * In particular, the result is composed of constraints that appear
2612 * in each of the basic sets of "set"
2613 */
2614__isl_give isl_basic_set *isl_set_plain_unshifted_simple_hull(
2615 __isl_take isl_set *set)
2616{
2617 return isl_map_plain_unshifted_simple_hull(set);
2618}
2619
2620/* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2621 *
2622 * For each basic set in "set", we first check if the basic set
2623 * contains a translate of "ineq". If this translate is more relaxed,
2624 * then we assume that "ineq" is not a bound on this basic set.
2625 * Otherwise, we know that it is a bound.
2626 * If the basic set does not contain a translate of "ineq", then
2627 * we call is_bound to perform the test.
2628 */
2629static __isl_give isl_basic_set *add_bound_from_constraint(
2630 __isl_take isl_basic_set *hull, struct sh_data *data,
2631 __isl_keep isl_set *set, isl_int *ineq)
2632{
2633 int i, k;
2634 isl_ctx *ctx;
2635 uint32_t c_hash;
2636 struct ineq_cmp_data v;
2637
2638 if (!hull || !set)
2639 return isl_basic_set_free(hull);
2640
2641 v.len = isl_basic_set_total_dim(hull);
2642 v.p = ineq;
2643 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2644
2645 ctx = isl_basic_set_get_ctx(hull);
2646 for (i = 0; i < set->n; ++i) {
2647 int bound;
2648 struct isl_hash_table_entry *entry;
2649
2650 entry = isl_hash_table_find(ctx, data->p[i].table,
2651 c_hash, &has_ineq, &v, 0);
2652 if (entry) {
2653 isl_int *ineq_i = entry->data;
2654 int neg, more_relaxed;
2655
2656 neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
2657 if (neg)
2658 isl_int_neg(ineq_i[0], ineq_i[0]);
2659 more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
2660 if (neg)
2661 isl_int_neg(ineq_i[0], ineq_i[0]);
2662 if (more_relaxed)
2663 break;
2664 else
2665 continue;
2666 }
2667 bound = is_bound(data, set, i, ineq, 0);
2668 if (bound < 0)
2669 return isl_basic_set_free(hull);
2670 if (!bound)
2671 break;
2672 }
2673 if (i < set->n)
2674 return hull;
2675
2676 k = isl_basic_set_alloc_inequality(hull);
2677 if (k < 0)
2678 return isl_basic_set_free(hull);
2679 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2680
2681 return hull;
2682}
2683
2684/* Compute a superset of the convex hull of "set" that is described
2685 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2686 * has no parameters or integer divisions.
2687 *
2688 * The inequalities in "ineq" are assumed to have been sorted such
2689 * that constraints with the same linear part appear together and
2690 * that among constraints with the same linear part, those with
2691 * smaller constant term appear first.
2692 *
2693 * We reuse the same data structure that is used by uset_simple_hull,
2694 * but we do not need the hull table since we will not consider the
2695 * same constraint more than once. We therefore allocate it with zero size.
2696 *
2697 * We run through the constraints and try to add them one by one,
2698 * skipping identical constraints. If we have added a constraint and
2699 * the next constraint is a more relaxed translate, then we skip this
2700 * next constraint as well.
2701 */
2702static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
2703 __isl_take isl_set *set, int n_ineq, isl_int **ineq)
2704{
2705 int i;
2706 int last_added = 0;
2707 struct sh_data *data = NULL;
2708 isl_basic_set *hull = NULL;
2709 unsigned dim;
2710
2711 hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
2712 if (!hull)
2713 goto error;
2714
2715 data = sh_data_alloc(set, 0);
2716 if (!data)
2717 goto error;
2718
2719 dim = isl_set_dim(set, isl_dim_set);
2720 for (i = 0; i < n_ineq; ++i) {
2721 int hull_n_ineq = hull->n_ineq;
2722 int parallel;
2723
2724 parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
2725 dim);
2726 if (parallel &&
2727 (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
2728 continue;
2729 hull = add_bound_from_constraint(hull, data, set, ineq[i]);
2730 if (!hull)
2731 goto error;
2732 last_added = hull->n_ineq > hull_n_ineq;
2733 }
2734
2735 sh_data_free(data);
2736 isl_set_free(set);
2737 return hull;
2738error:
2739 sh_data_free(data);
2740 isl_set_free(set);
2741 isl_basic_set_free(hull);
2742 return NULL;
2743}
2744
2745/* Collect pointers to all the inequalities in the elements of "list"
2746 * in "ineq". For equalities, store both a pointer to the equality and
2747 * a pointer to its opposite, which is first copied to "mat".
2748 * "ineq" and "mat" are assumed to have been preallocated to the right size
2749 * (the number of inequalities + 2 times the number of equalites and
2750 * the number of equalities, respectively).
2751 */
2752static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
2753 __isl_keep isl_basic_set_list *list, isl_int **ineq)
2754{
2755 int i, j, n, n_eq, n_ineq;
2756
2757 if (!mat)
2758 return NULL;
2759
2760 n_eq = 0;
2761 n_ineq = 0;
2762 n = isl_basic_set_list_n_basic_set(list);
2763 for (i = 0; i < n; ++i) {
2764 isl_basic_set *bset;
2765 bset = isl_basic_set_list_get_basic_set(list, i);
2766 if (!bset)
2767 return isl_mat_free(mat);
2768 for (j = 0; j < bset->n_eq; ++j) {
2769 ineq[n_ineq++] = mat->row[n_eq];
2770 ineq[n_ineq++] = bset->eq[j];
2771 isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
2772 }
2773 for (j = 0; j < bset->n_ineq; ++j)
2774 ineq[n_ineq++] = bset->ineq[j];
2775 isl_basic_set_free(bset);
2776 }
2777
2778 return mat;
2779}
2780
2781/* Comparison routine for use as an isl_sort callback.
2782 *
2783 * Constraints with the same linear part are sorted together and
2784 * among constraints with the same linear part, those with smaller
2785 * constant term are sorted first.
2786 */
2787static int cmp_ineq(const void *a, const void *b, void *arg)
2788{
2789 unsigned dim = *(unsigned *) arg;
2790 isl_int * const *ineq1 = a;
2791 isl_int * const *ineq2 = b;
2792 int cmp;
2793
2794 cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
2795 if (cmp != 0)
2796 return cmp;
2797 return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
2798}
2799
2800/* Compute a superset of the convex hull of "set" that is described
2801 * by only constraints in the elements of "list", where "set" has
2802 * no parameters or integer divisions.
2803 *
2804 * We collect all the constraints in those elements and then
2805 * sort the constraints such that constraints with the same linear part
2806 * are sorted together and that those with smaller constant term are
2807 * sorted first.
2808 */
2809static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
2810 __isl_take isl_set *set, __isl_take isl_basic_set_list *list)
2811{
2812 int i, n, n_eq, n_ineq;
2813 unsigned dim;
2814 isl_ctx *ctx;
2815 isl_mat *mat = NULL;
2816 isl_int **ineq = NULL;
2817 isl_basic_set *hull;
2818
2819 if (!set)
2820 goto error;
2821 ctx = isl_set_get_ctx(set);
2822
2823 n_eq = 0;
2824 n_ineq = 0;
2825 n = isl_basic_set_list_n_basic_set(list);
2826 for (i = 0; i < n; ++i) {
2827 isl_basic_set *bset;
2828 bset = isl_basic_set_list_get_basic_set(list, i);
2829 if (!bset)
2830 goto error;
2831 n_eq += bset->n_eq;
2832 n_ineq += 2 * bset->n_eq + bset->n_ineq;
2833 isl_basic_set_free(bset);
2834 }
2835
2836 ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
2837 if (n_ineq > 0 && !ineq)
2838 goto error;
2839
2840 dim = isl_set_dim(set, isl_dim_set);
2841 mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
2842 mat = collect_inequalities(mat, list, ineq);
2843 if (!mat)
2844 goto error;
2845
2846 if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
2847 goto error;
2848
2849 hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);
2850
2851 isl_mat_free(mat);
2852 free(ineq);
2853 isl_basic_set_list_free(list);
2854 return hull;
2855error:
2856 isl_mat_free(mat);
2857 free(ineq);
2858 isl_set_free(set);
2859 isl_basic_set_list_free(list);
2860 return NULL;
2861}
2862
2863/* Compute a superset of the convex hull of "map" that is described
2864 * by only constraints in the elements of "list".
2865 *
2866 * If the list is empty, then we can only describe the universe set.
2867 * If the input map is empty, then all constraints are valid, so
2868 * we return the intersection of the elements in "list".
2869 *
2870 * Otherwise, we align all divs and temporarily treat them
2871 * as regular variables, computing the unshifted simple hull in
2872 * uset_unshifted_simple_hull_from_basic_set_list.
2873 */
2874static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
2875 __isl_take isl_map *map, __isl_take isl_basic_map_list *list)
2876{
2877 isl_basic_map *model;
2878 isl_basic_map *hull;
2879 isl_set *set;
2880 isl_basic_set_list *bset_list;
2881
2882 if (!map || !list)
2883 goto error;
2884
2885 if (isl_basic_map_list_n_basic_map(list) == 0) {
2886 isl_space *space;
2887
2888 space = isl_map_get_space(map);
2889 isl_map_free(map);
2890 isl_basic_map_list_free(list);
2891 return isl_basic_map_universe(space);
2892 }
2893 if (isl_map_plain_is_empty(map)) {
2894 isl_map_free(map);
2895 return isl_basic_map_list_intersect(list);
2896 }
2897
2898 map = isl_map_align_divs_to_basic_map_list(map, list);
2899 if (!map)
2900 goto error;
2901 list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);
2902
2903 model = isl_basic_map_list_get_basic_map(list, 0);
2904
2905 set = isl_map_underlying_set(map);
2906 bset_list = isl_basic_map_list_underlying_set(list);
2907
2908 hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
2909 hull = isl_basic_map_overlying_set(hull, model);
2910
2911 return hull;
2912error:
2913 isl_map_free(map);
2914 isl_basic_map_list_free(list);
2915 return NULL;
2916}
2917
2918/* Return a sequence of the basic maps that make up the maps in "list".
2919 */
2920static __isl_give isl_basic_map_list *collect_basic_maps(
2921 __isl_take isl_map_list *list)
2922{
2923 int i, n;
2924 isl_ctx *ctx;
2925 isl_basic_map_list *bmap_list;
2926
2927 if (!list)
2928 return NULL;
2929 n = isl_map_list_n_map(list);
2930 ctx = isl_map_list_get_ctx(list);
2931 bmap_list = isl_basic_map_list_alloc(ctx, 0);
2932
2933 for (i = 0; i < n; ++i) {
2934 isl_map *map;
2935 isl_basic_map_list *list_i;
2936
2937 map = isl_map_list_get_map(list, i);
2938 map = isl_map_compute_divs(map);
2939 list_i = isl_map_get_basic_map_list(map);
2940 isl_map_free(map);
2941 bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
2942 }
2943
2944 isl_map_list_free(list);
2945 return bmap_list;
2946}
2947
2948/* Compute a superset of the convex hull of "map" that is described
2949 * by only constraints in the elements of "list".
2950 *
2951 * If "map" is the universe, then the convex hull (and therefore
2952 * any superset of the convexhull) is the universe as well.
2953 *
2954 * Otherwise, we collect all the basic maps in the map list and
2955 * continue with map_unshifted_simple_hull_from_basic_map_list.
2956 */
2957__isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
2958 __isl_take isl_map *map, __isl_take isl_map_list *list)
2959{
2960 isl_basic_map_list *bmap_list;
2961 int is_universe;
2962
2963 is_universe = isl_map_plain_is_universe(map);
2964 if (is_universe < 0)
2965 map = isl_map_free(map);
2966 if (is_universe < 0 || is_universe) {
2967 isl_map_list_free(list);
2968 return isl_map_unshifted_simple_hull(map);
2969 }
2970
2971 bmap_list = collect_basic_maps(list);
2972 return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
2973}
2974
2975/* Compute a superset of the convex hull of "set" that is described
2976 * by only constraints in the elements of "list".
2977 */
2978__isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
2979 __isl_take isl_set *set, __isl_take isl_set_list *list)
2980{
2981 return isl_map_unshifted_simple_hull_from_map_list(set, list);
2982}
2983
2984/* Given a set "set", return parametric bounds on the dimension "dim".
2985 */
2986static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2987{
2988 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2989 set = isl_set_copy(set);
2990 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2991 set = isl_set_eliminate_dims(set, 0, dim);
2992 return isl_set_convex_hull(set);
2993}
2994
2995/* Computes a "simple hull" and then check if each dimension in the
2996 * resulting hull is bounded by a symbolic constant. If not, the
2997 * hull is intersected with the corresponding bounds on the whole set.
2998 */
2999__isl_give isl_basic_set *isl_set_bounded_simple_hull(__isl_take isl_set *set)
3000{
3001 int i, j;
3002 struct isl_basic_set *hull;
3003 unsigned nparam, left;
3004 int removed_divs = 0;
3005
3006 hull = isl_set_simple_hull(isl_set_copy(set));
3007 if (!hull)
3008 goto error;
3009
3010 nparam = isl_basic_set_dim(hull, isl_dim_param);
3011 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
3012 int lower = 0, upper = 0;
3013 struct isl_basic_set *bounds;
3014
3015 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
3016 for (j = 0; j < hull->n_eq; ++j) {
3017 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
3018 continue;
3019 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
3020 left) == -1)
3021 break;
3022 }
3023 if (j < hull->n_eq)
3024 continue;
3025
3026 for (j = 0; j < hull->n_ineq; ++j) {
3027 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
3028 continue;
3029 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
3030 left) != -1 ||
3031 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
3032 i) != -1)
3033 continue;
3034 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
3035 lower = 1;
3036 else
3037 upper = 1;
3038 if (lower && upper)
3039 break;
3040 }
3041
3042 if (lower && upper)
3043 continue;
3044
3045 if (!removed_divs) {
3046 set = isl_set_remove_divs(set);
3047 if (!set)
3048 goto error;
3049 removed_divs = 1;
3050 }
3051 bounds = set_bounds(set, i);
3052 hull = isl_basic_set_intersect(hull, bounds);
3053 if (!hull)
3054 goto error;
3055 }
3056
3057 isl_set_free(set);
3058 return hull;
3059error:
3060 isl_set_free(set);
3061 isl_basic_set_free(hull);
3062 return NULL;
3063}