2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_lp_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_vec_private.h>
21 #include <isl_options_private.h>
22 #include "isl_equalities.h"
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
30 static __isl_give isl_basic_set
*uset_convex_hull_wrap_bounded(
31 __isl_take isl_set
*set
);
34 * constraints. If the minimal value along the normal of a constraint
35 * is the same if the constraint is removed, then the constraint is redundant.
37 * Since some constraints may be mutually redundant, sort the constraints
38 * first such that constraints that involve existentially quantified
39 * variables are considered for removal before those that do not.
40 * The sorting is also needed for the use in map_simple_hull.
42 * Note that isl_tab_detect_implicit_equalities may also end up
43 * marking some constraints as redundant. Make sure the constraints
44 * are preserved and undo those marking such that isl_tab_detect_redundant
45 * can consider the constraints in the sorted order.
47 * Alternatively, we could have intersected the basic map with the
48 * corresponding equality and then checked if the dimension was that
51 __isl_give isl_basic_map
*isl_basic_map_remove_redundancies(
52 __isl_take isl_basic_map
*bmap
)
59 bmap
= isl_basic_map_gauss(bmap
, NULL
);
60 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
62 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
64 if (bmap
->n_ineq
<= 1)
67 bmap
= isl_basic_map_sort_constraints(bmap
);
68 tab
= isl_tab_from_basic_map(bmap
, 0);
72 if (isl_tab_detect_implicit_equalities(tab
) < 0)
74 if (isl_tab_restore_redundant(tab
) < 0)
77 if (isl_tab_detect_redundant(tab
) < 0)
79 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
83 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
84 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
88 isl_basic_map_free(bmap
);
92 __isl_give isl_basic_set
*isl_basic_set_remove_redundancies(
93 __isl_take isl_basic_set
*bset
)
95 return bset_from_bmap(
96 isl_basic_map_remove_redundancies(bset_to_bmap(bset
)));
99 /* Remove redundant constraints in each of the basic maps.
101 __isl_give isl_map
*isl_map_remove_redundancies(__isl_take isl_map
*map
)
103 return isl_map_inline_foreach_basic_map(map
,
104 &isl_basic_map_remove_redundancies
);
107 __isl_give isl_set
*isl_set_remove_redundancies(__isl_take isl_set
*set
)
109 return isl_map_remove_redundancies(set
);
112 /* Check if the set set is bound in the direction of the affine
113 * constraint c and if so, set the constant term such that the
114 * resulting constraint is a bounding constraint for the set.
116 static int uset_is_bound(__isl_keep isl_set
*set
, isl_int
*c
, unsigned len
)
124 isl_int_init(opt_denom
);
126 for (j
= 0; j
< set
->n
; ++j
) {
127 enum isl_lp_result res
;
129 if (ISL_F_ISSET(set
->p
[j
], ISL_BASIC_SET_EMPTY
))
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
)
136 if (res
== isl_lp_error
)
138 if (res
== isl_lp_empty
) {
139 set
->p
[j
] = isl_basic_set_set_to_empty(set
->p
[j
]);
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
);
152 isl_int_clear(opt_denom
);
156 isl_int_clear(opt_denom
);
160 static struct isl_basic_set
*isl_basic_set_add_equality(
161 struct isl_basic_set
*bset
, isl_int
*c
)
169 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
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
);
180 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
183 isl_basic_set_free(bset
);
187 static __isl_give isl_set
*isl_set_add_basic_set_equality(
188 __isl_take isl_set
*set
, isl_int
*c
)
192 set
= isl_set_cow(set
);
195 for (i
= 0; i
< set
->n
; ++i
) {
196 set
->p
[i
] = isl_basic_set_add_equality(set
->p
[i
], c
);
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
214 * then the resulting set is of dimension n*(1+d) and has as constraints
223 static __isl_give isl_basic_set
*wrap_constraints(__isl_keep isl_set
*set
)
225 struct isl_basic_set
*lp
;
229 unsigned dim
, lp_dim
;
234 dim
= 1 + isl_set_n_dim(set
);
237 for (i
= 0; i
< set
->n
; ++i
) {
238 n_eq
+= set
->p
[i
]->n_eq
;
239 n_ineq
+= set
->p
[i
]->n_ineq
;
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
);
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);
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);
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));
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));
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
279 * We first transform the set such that the facet constraint becomes
283 * I.e., the facet lies in
287 * and on that facet, the constraint that defines the ridge is
291 * (This transformation is not strictly needed, all that is needed is
292 * that the ridge contains the origin.)
294 * Since the ridge contains the origin, the cone of the convex hull
295 * will be of the form
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
319 * the constraints of each (transformed) basic set.
320 * If a = n/d, then the constraint defining the new facet (in the transformed
323 * -n x_1 + d x_2 >= 0
325 * In the original space, we need to take the same combination of the
326 * corresponding constraints "facet" and "ridge".
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.
332 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
333 isl_int
*facet
, isl_int
*ridge
)
337 struct isl_mat
*T
= NULL
;
338 struct isl_basic_set
*lp
= NULL
;
340 enum isl_lp_result res
;
347 set
= isl_set_copy(set
);
348 set
= isl_set_set_rational(set
);
350 dim
= 1 + isl_set_n_dim(set
);
351 T
= isl_mat_alloc(ctx
, 3, dim
);
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
);
363 lp
= wrap_constraints(set
);
364 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
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);
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
);
385 isl_basic_set_free(lp
);
387 if (res
== isl_lp_error
)
389 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
393 isl_basic_set_free(lp
);
399 /* Compute the constraint of a facet of "set".
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.
412 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
414 struct isl_set
*slice
= NULL
;
415 struct isl_basic_set
*face
= NULL
;
417 unsigned dim
= isl_set_n_dim(set
);
419 isl_mat
*bounds
= NULL
;
421 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
422 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
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
);
431 isl_assert(set
->ctx
, is_bound
, goto error
);
432 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
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
);
441 if (face
->n_eq
== 1) {
442 isl_basic_set_free(face
);
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
))
450 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
451 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
453 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
454 isl_basic_set_free(face
);
459 isl_basic_set_free(face
);
460 isl_mat_free(bounds
);
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
468 * We compute an affine transformation that transforms the constraint
477 * by computing the right inverse U of a matrix that starts with the rows
490 * Since z_1 is zero, we can drop this variable as well as the corresponding
491 * column of U to obtain
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.
503 static __isl_give isl_basic_set
*compute_facet(__isl_keep isl_set
*set
,
506 struct isl_mat
*m
, *U
, *Q
;
507 struct isl_basic_set
*facet
= NULL
;
512 set
= isl_set_copy(set
);
513 dim
= isl_set_n_dim(set
);
514 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
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
));
532 isl_basic_set_free(facet
);
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.
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.
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
555 * using the technique in section "3.1 Ridge Generation" of
556 * "Extended Convex Hull" by Fukuda et al.
558 static __isl_give isl_basic_set
*extend(__isl_take isl_basic_set
*hull
,
559 __isl_keep isl_set
*set
)
563 struct isl_basic_set
*facet
= NULL
;
564 struct isl_basic_set
*hull_facet
= NULL
;
570 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
572 dim
= isl_set_n_dim(set
);
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
)
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
);
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
))
595 if (f
< hull_facet
->n_ineq
)
597 k
= isl_basic_set_alloc_inequality(hull
);
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
]))
604 isl_basic_set_free(hull_facet
);
605 isl_basic_set_free(facet
);
607 hull
= isl_basic_set_simplify(hull
);
608 hull
= isl_basic_set_finalize(hull
);
611 isl_basic_set_free(hull_facet
);
612 isl_basic_set_free(facet
);
613 isl_basic_set_free(hull
);
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.
621 static __isl_give isl_basic_set
*convex_hull_1d(__isl_take isl_set
*set
)
623 struct isl_mat
*c
= NULL
;
624 isl_int
*lower
= NULL
;
625 isl_int
*upper
= NULL
;
628 struct isl_basic_set
*hull
;
630 for (i
= 0; i
< set
->n
; ++i
) {
631 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
635 set
= isl_set_remove_empty_parts(set
);
638 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
639 c
= isl_mat_alloc(set
->ctx
, 2, 2);
643 if (set
->p
[0]->n_eq
> 0) {
644 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
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);
651 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
652 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
655 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
656 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
658 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
661 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
668 for (i
= 0; i
< set
->n
; ++i
) {
669 struct isl_basic_set
*bset
= set
->p
[i
];
673 for (j
= 0; j
< bset
->n_eq
; ++j
) {
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);
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);
693 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
694 if (isl_int_is_pos(bset
->ineq
[j
][1]))
696 if (isl_int_is_neg(bset
->ineq
[j
][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);
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);
719 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
720 hull
= isl_basic_set_set_rational(hull
);
724 k
= isl_basic_set_alloc_inequality(hull
);
725 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
728 k
= isl_basic_set_alloc_inequality(hull
);
729 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
731 hull
= isl_basic_set_finalize(hull
);
741 static __isl_give isl_basic_set
*convex_hull_0d(__isl_take isl_set
*set
)
743 struct isl_basic_set
*convex_hull
;
748 if (isl_set_is_empty(set
))
749 convex_hull
= isl_basic_set_empty(isl_space_copy(set
->dim
));
751 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
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.
765 static __isl_give isl_basic_set
*convex_hull_pair_elim(
766 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
769 struct isl_basic_set
*bset
[2];
770 struct isl_basic_set
*hull
= NULL
;
773 if (!bset1
|| !bset2
)
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
);
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
);
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
],
792 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
793 k
= isl_basic_set_alloc_inequality(hull
);
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
);
801 k
= isl_basic_set_alloc_inequality(hull
);
804 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
805 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
807 for (j
= 0; j
< 1+dim
; ++j
) {
808 k
= isl_basic_set_alloc_equality(hull
);
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);
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
);
823 isl_basic_set_free(bset1
);
824 isl_basic_set_free(bset2
);
825 isl_basic_set_free(hull
);
829 /* Is the set bounded for each value of the parameters?
831 isl_bool
isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
837 return isl_bool_error
;
838 if (isl_basic_set_plain_is_empty(bset
))
839 return isl_bool_true
;
841 tab
= isl_tab_from_recession_cone(bset
, 1);
842 bounded
= isl_tab_cone_is_bounded(tab
);
847 /* Is the image bounded for each value of the parameters and
848 * the domain variables?
850 isl_bool
isl_basic_map_image_is_bounded(__isl_keep isl_basic_map
*bmap
)
852 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
853 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
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
);
866 /* Is the set bounded for each value of the parameters?
868 isl_bool
isl_set_is_bounded(__isl_keep isl_set
*set
)
873 return isl_bool_error
;
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)
880 return isl_bool_true
;
883 /* Compute the lineality space of the convex hull of bset1 and bset2.
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.
889 static __isl_give isl_basic_set
*induced_lineality_space(
890 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
893 struct isl_basic_set
*lin
= NULL
;
896 if (!bset1
|| !bset2
)
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
);
906 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
907 k
= isl_basic_set_alloc_equality(lin
);
910 isl_int_set_si(lin
->eq
[k
][0], 0);
911 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
913 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
914 k
= isl_basic_set_alloc_inequality(lin
);
917 isl_int_set_si(lin
->ineq
[k
][0], 0);
918 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
920 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
921 k
= isl_basic_set_alloc_equality(lin
);
924 isl_int_set_si(lin
->eq
[k
][0], 0);
925 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
927 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
928 k
= isl_basic_set_alloc_inequality(lin
);
931 isl_int_set_si(lin
->ineq
[k
][0], 0);
932 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
935 isl_basic_set_free(bset1
);
936 isl_basic_set_free(bset2
);
937 return isl_basic_set_affine_hull(lin
);
939 isl_basic_set_free(lin
);
940 isl_basic_set_free(bset1
);
941 isl_basic_set_free(bset2
);
945 static __isl_give isl_basic_set
*uset_convex_hull(__isl_take isl_set
*set
);
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.
955 * describe the linear space. We first compute the Hermite normal
956 * form H = M U of M = H Q, to obtain
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.
967 static __isl_give isl_basic_set
*modulo_lineality(__isl_take isl_set
*set
,
968 __isl_take isl_basic_set
*lin
)
970 unsigned total
= isl_basic_set_total_dim(lin
);
972 struct isl_basic_set
*hull
;
973 struct isl_mat
*M
, *U
, *Q
;
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
);
983 isl_basic_set_free(lin
);
985 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
987 U
= isl_mat_lin_to_aff(U
);
988 Q
= isl_mat_lin_to_aff(Q
);
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
);
997 isl_basic_set_free(lin
);
1002 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1003 * set up an LP for solving
1005 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
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.
1011 static __isl_give isl_basic_set
*valid_direction_lp(
1012 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1015 struct isl_basic_set
*lp
;
1020 if (!bset1
|| !bset2
)
1022 d
= 1 + isl_basic_set_total_dim(bset1
);
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
);
1029 for (i
= 0; i
< n
; ++i
) {
1030 k
= isl_basic_set_alloc_inequality(lp
);
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);
1037 for (i
= 0; i
< d
; ++i
) {
1038 k
= isl_basic_set_alloc_equality(lp
);
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
++;
1049 for (j
= 0; j
< bset1
->n_ineq
; ++j
) {
1050 isl_int_set(lp
->eq
[k
][n
], bset1
->ineq
[j
][i
]); n
++;
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
++;
1058 for (j
= 0; j
< bset2
->n_ineq
; ++j
) {
1059 isl_int_neg(lp
->eq
[k
][n
], bset2
->ineq
[j
][i
]); n
++;
1062 lp
= isl_basic_set_gauss(lp
, NULL
);
1063 isl_basic_set_free(bset1
);
1064 isl_basic_set_free(bset2
);
1067 isl_basic_set_free(bset1
);
1068 isl_basic_set_free(bset2
);
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.
1076 * We compute s as a vector that satisfies
1078 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
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 (*).
1089 * Note that we simply pick any valid direction and make no attempt
1090 * to pick a "good" or even the "best" valid direction.
1092 static __isl_give isl_vec
*valid_direction(
1093 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1095 struct isl_basic_set
*lp
;
1096 struct isl_tab
*tab
;
1097 struct isl_vec
*sample
= NULL
;
1098 struct isl_vec
*dir
;
1103 if (!bset1
|| !bset2
)
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
);
1110 isl_basic_set_free(lp
);
1113 d
= isl_basic_set_total_dim(bset1
);
1114 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1117 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 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
);
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
);
1140 isl_vec_free(sample
);
1141 isl_basic_set_free(bset1
);
1142 isl_basic_set_free(bset2
);
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
1149 * [ b_i A_i ] [ y' ] [ y' ]
1150 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1152 * In particular, add the "positivity constraint" and then perform
1155 static __isl_give isl_basic_set
*homogeneous_map(__isl_take isl_basic_set
*bset
,
1156 __isl_take isl_mat
*T
)
1162 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1163 k
= isl_basic_set_alloc_inequality(bset
);
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
);
1172 isl_basic_set_free(bset
);
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.
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.
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.
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.
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
1210 * [ y' ] [ y ] [ y ] [ y' ]
1211 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1213 * We take S to be the unimodular completion of s to limit the growth
1214 * of the coefficients in the following computations.
1216 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1217 * We first move to the homogeneous dimension
1219 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1220 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1222 * Then we change directoin
1224 * [ b_i A_i ] [ y' ] [ y' ]
1225 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
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
1231 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1233 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1235 static __isl_give isl_basic_set
*convex_hull_pair_pointed(
1236 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
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
;
1245 if (!bset1
|| !bset2
)
1247 ctx
= isl_basic_set_get_ctx(bset1
);
1248 dir
= valid_direction(isl_basic_set_copy(bset1
),
1249 isl_basic_set_copy(bset2
));
1252 T
= isl_mat_alloc(ctx
, dir
->size
, dir
->size
);
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
));
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
);
1272 isl_basic_set_free(bset1
);
1273 isl_basic_set_free(bset2
);
1277 static __isl_give isl_basic_set
*uset_convex_hull_wrap(__isl_take isl_set
*set
);
1278 static __isl_give isl_basic_set
*modulo_affine_hull(
1279 __isl_take isl_set
*set
, __isl_take isl_basic_set
*affine_hull
);
1281 /* Compute the convex hull of a pair of basic sets without any parameters or
1282 * integer divisions.
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.
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.
1294 static __isl_give isl_basic_set
*convex_hull_pair(
1295 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1297 isl_basic_set
*lin
, *aff
;
1298 int bounded1
, bounded2
;
1300 if (bset1
->ctx
->opt
->convex
== ISL_CONVEX_HULL_FM
)
1301 return convex_hull_pair_elim(bset1
, bset2
);
1303 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1304 isl_basic_set_copy(bset2
)));
1308 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1309 isl_basic_set_free(aff
);
1311 bounded1
= isl_basic_set_is_bounded(bset1
);
1312 bounded2
= isl_basic_set_is_bounded(bset2
);
1314 if (bounded1
< 0 || bounded2
< 0)
1317 if (bounded1
&& bounded2
)
1318 return uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1320 if (bounded1
|| bounded2
)
1321 return convex_hull_pair_pointed(bset1
, bset2
);
1323 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1324 isl_basic_set_copy(bset2
));
1327 if (isl_basic_set_plain_is_universe(lin
)) {
1328 isl_basic_set_free(bset1
);
1329 isl_basic_set_free(bset2
);
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
);
1339 isl_basic_set_free(lin
);
1341 return convex_hull_pair_pointed(bset1
, bset2
);
1343 isl_basic_set_free(bset1
);
1344 isl_basic_set_free(bset2
);
1348 /* Compute the lineality space of a basic set.
1349 * We currently do not allow the basic set to have any divs.
1350 * We basically just drop the constants and turn every inequality
1353 __isl_give isl_basic_set
*isl_basic_set_lineality_space(
1354 __isl_take isl_basic_set
*bset
)
1357 struct isl_basic_set
*lin
= NULL
;
1362 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1363 dim
= isl_basic_set_total_dim(bset
);
1365 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset
), 0, dim
, 0);
1368 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1369 k
= isl_basic_set_alloc_equality(lin
);
1372 isl_int_set_si(lin
->eq
[k
][0], 0);
1373 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1375 lin
= isl_basic_set_gauss(lin
, NULL
);
1378 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1379 k
= isl_basic_set_alloc_equality(lin
);
1382 isl_int_set_si(lin
->eq
[k
][0], 0);
1383 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1384 lin
= isl_basic_set_gauss(lin
, NULL
);
1388 isl_basic_set_free(bset
);
1391 isl_basic_set_free(lin
);
1392 isl_basic_set_free(bset
);
1396 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1397 * "underlying" set "set".
1399 static __isl_give isl_basic_set
*uset_combined_lineality_space(
1400 __isl_take isl_set
*set
)
1403 struct isl_set
*lin
= NULL
;
1408 isl_space
*space
= isl_set_get_space(set
);
1410 return isl_basic_set_empty(space
);
1413 lin
= isl_set_alloc_space(isl_set_get_space(set
), set
->n
, 0);
1414 for (i
= 0; i
< set
->n
; ++i
)
1415 lin
= isl_set_add_basic_set(lin
,
1416 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1418 return isl_set_affine_hull(lin
);
1421 /* Compute the convex hull of a set without any parameters or
1422 * integer divisions.
1423 * In each step, we combined two basic sets until only one
1424 * basic set is left.
1425 * The input basic sets are assumed not to have a non-trivial
1426 * lineality space. If any of the intermediate results has
1427 * a non-trivial lineality space, it is projected out.
1429 static __isl_give isl_basic_set
*uset_convex_hull_unbounded(
1430 __isl_take isl_set
*set
)
1432 isl_basic_set_list
*list
;
1434 list
= isl_set_get_basic_set_list(set
);
1439 struct isl_basic_set
*t
;
1440 isl_basic_set
*bset1
, *bset2
;
1442 n
= isl_basic_set_list_n_basic_set(list
);
1444 isl_die(isl_basic_set_list_get_ctx(list
),
1446 "expecting at least two elements", goto error
);
1447 bset1
= isl_basic_set_list_get_basic_set(list
, n
- 1);
1448 bset2
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1449 bset1
= convex_hull_pair(bset1
, bset2
);
1451 isl_basic_set_list_free(list
);
1454 bset1
= isl_basic_set_underlying_set(bset1
);
1455 list
= isl_basic_set_list_drop(list
, n
- 2, 2);
1456 list
= isl_basic_set_list_add(list
, bset1
);
1458 t
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1459 t
= isl_basic_set_lineality_space(t
);
1462 if (isl_basic_set_plain_is_universe(t
)) {
1463 isl_basic_set_list_free(list
);
1466 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1467 set
= isl_basic_set_list_union(list
);
1468 return modulo_lineality(set
, t
);
1470 isl_basic_set_free(t
);
1475 isl_basic_set_list_free(list
);
1479 /* Compute an initial hull for wrapping containing a single initial
1481 * This function assumes that the given set is bounded.
1483 static __isl_give isl_basic_set
*initial_hull(__isl_take isl_basic_set
*hull
,
1484 __isl_keep isl_set
*set
)
1486 struct isl_mat
*bounds
= NULL
;
1492 bounds
= initial_facet_constraint(set
);
1495 k
= isl_basic_set_alloc_inequality(hull
);
1498 dim
= isl_set_n_dim(set
);
1499 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1500 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1501 isl_mat_free(bounds
);
1505 isl_basic_set_free(hull
);
1506 isl_mat_free(bounds
);
1510 struct max_constraint
{
1516 static int max_constraint_equal(const void *entry
, const void *val
)
1518 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1519 isl_int
*b
= (isl_int
*)val
;
1521 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1524 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1525 isl_int
*con
, unsigned len
, int n
, int ineq
)
1527 struct isl_hash_table_entry
*entry
;
1528 struct max_constraint
*c
;
1531 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1532 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1538 isl_hash_table_remove(ctx
, table
, entry
);
1542 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1544 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1549 c
->c
= isl_mat_cow(c
->c
);
1550 isl_int_set(c
->c
->row
[0][0], con
[0]);
1554 /* Check whether the constraint hash table "table" constains the constraint
1557 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1558 isl_int
*con
, unsigned len
, int n
)
1560 struct isl_hash_table_entry
*entry
;
1561 struct max_constraint
*c
;
1564 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1565 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1572 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1575 /* Check for inequality constraints of a basic set without equalities
1576 * such that the same or more stringent copies of the constraint appear
1577 * in all of the basic sets. Such constraints are necessarily facet
1578 * constraints of the convex hull.
1580 * If the resulting basic set is by chance identical to one of
1581 * the basic sets in "set", then we know that this basic set contains
1582 * all other basic sets and is therefore the convex hull of set.
1583 * In this case we set *is_hull to 1.
1585 static __isl_give isl_basic_set
*common_constraints(
1586 __isl_take isl_basic_set
*hull
, __isl_keep isl_set
*set
, int *is_hull
)
1589 int min_constraints
;
1591 struct max_constraint
*constraints
= NULL
;
1592 struct isl_hash_table
*table
= NULL
;
1597 for (i
= 0; i
< set
->n
; ++i
)
1598 if (set
->p
[i
]->n_eq
== 0)
1602 min_constraints
= set
->p
[i
]->n_ineq
;
1604 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1605 if (set
->p
[i
]->n_eq
!= 0)
1607 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1609 min_constraints
= set
->p
[i
]->n_ineq
;
1612 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1616 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1617 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1620 total
= isl_space_dim(set
->dim
, isl_dim_all
);
1621 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1622 constraints
[i
].c
= isl_mat_sub_alloc6(hull
->ctx
,
1623 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1624 if (!constraints
[i
].c
)
1626 constraints
[i
].ineq
= 1;
1628 for (i
= 0; i
< min_constraints
; ++i
) {
1629 struct isl_hash_table_entry
*entry
;
1631 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1632 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1633 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1636 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1637 entry
->data
= &constraints
[i
];
1641 for (s
= 0; s
< set
->n
; ++s
) {
1645 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1646 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1647 for (j
= 0; j
< 2; ++j
) {
1648 isl_seq_neg(eq
, eq
, 1 + total
);
1649 update_constraint(hull
->ctx
, table
,
1653 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1654 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1655 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1656 set
->p
[s
]->n_eq
== 0);
1661 for (i
= 0; i
< min_constraints
; ++i
) {
1662 if (constraints
[i
].count
< n
)
1664 if (!constraints
[i
].ineq
)
1666 j
= isl_basic_set_alloc_inequality(hull
);
1669 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1672 for (s
= 0; s
< set
->n
; ++s
) {
1673 if (set
->p
[s
]->n_eq
)
1675 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1677 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1678 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1679 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1682 if (i
== set
->p
[s
]->n_ineq
)
1686 isl_hash_table_clear(table
);
1687 for (i
= 0; i
< min_constraints
; ++i
)
1688 isl_mat_free(constraints
[i
].c
);
1693 isl_hash_table_clear(table
);
1696 for (i
= 0; i
< min_constraints
; ++i
)
1697 isl_mat_free(constraints
[i
].c
);
1702 /* Create a template for the convex hull of "set" and fill it up
1703 * obvious facet constraints, if any. If the result happens to
1704 * be the convex hull of "set" then *is_hull is set to 1.
1706 static __isl_give isl_basic_set
*proto_hull(__isl_keep isl_set
*set
,
1709 struct isl_basic_set
*hull
;
1714 for (i
= 0; i
< set
->n
; ++i
) {
1715 n_ineq
+= set
->p
[i
]->n_eq
;
1716 n_ineq
+= set
->p
[i
]->n_ineq
;
1718 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
1719 hull
= isl_basic_set_set_rational(hull
);
1722 return common_constraints(hull
, set
, is_hull
);
1725 static __isl_give isl_basic_set
*uset_convex_hull_wrap(__isl_take isl_set
*set
)
1727 struct isl_basic_set
*hull
;
1730 hull
= proto_hull(set
, &is_hull
);
1731 if (hull
&& !is_hull
) {
1732 if (hull
->n_ineq
== 0)
1733 hull
= initial_hull(hull
, set
);
1734 hull
= extend(hull
, set
);
1741 /* Compute the convex hull of a set without any parameters or
1742 * integer divisions. Depending on whether the set is bounded,
1743 * we pass control to the wrapping based convex hull or
1744 * the Fourier-Motzkin elimination based convex hull.
1745 * We also handle a few special cases before checking the boundedness.
1747 static __isl_give isl_basic_set
*uset_convex_hull(__isl_take isl_set
*set
)
1750 struct isl_basic_set
*convex_hull
= NULL
;
1751 struct isl_basic_set
*lin
;
1753 if (isl_set_n_dim(set
) == 0)
1754 return convex_hull_0d(set
);
1756 set
= isl_set_coalesce(set
);
1757 set
= isl_set_set_rational(set
);
1762 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1766 if (isl_set_n_dim(set
) == 1)
1767 return convex_hull_1d(set
);
1769 bounded
= isl_set_is_bounded(set
);
1772 if (bounded
&& set
->ctx
->opt
->convex
== ISL_CONVEX_HULL_WRAP
)
1773 return uset_convex_hull_wrap(set
);
1775 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1778 if (isl_basic_set_plain_is_universe(lin
)) {
1782 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1783 return modulo_lineality(set
, lin
);
1784 isl_basic_set_free(lin
);
1786 return uset_convex_hull_unbounded(set
);
1789 isl_basic_set_free(convex_hull
);
1793 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1794 * without parameters or divs and where the convex hull of set is
1795 * known to be full-dimensional.
1797 static __isl_give isl_basic_set
*uset_convex_hull_wrap_bounded(
1798 __isl_take isl_set
*set
)
1800 struct isl_basic_set
*convex_hull
= NULL
;
1805 if (isl_set_n_dim(set
) == 0) {
1806 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
1808 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1812 set
= isl_set_set_rational(set
);
1813 set
= isl_set_coalesce(set
);
1817 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1819 convex_hull
= isl_basic_map_remove_redundancies(convex_hull
);
1822 if (isl_set_n_dim(set
) == 1)
1823 return convex_hull_1d(set
);
1825 return uset_convex_hull_wrap(set
);
1831 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1832 * We first remove the equalities (transforming the set), compute the
1833 * convex hull of the transformed set and then add the equalities back
1834 * (after performing the inverse transformation.
1836 static __isl_give isl_basic_set
*modulo_affine_hull(
1837 __isl_take isl_set
*set
, __isl_take isl_basic_set
*affine_hull
)
1841 struct isl_basic_set
*dummy
;
1842 struct isl_basic_set
*convex_hull
;
1844 dummy
= isl_basic_set_remove_equalities(
1845 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1848 isl_basic_set_free(dummy
);
1849 set
= isl_set_preimage(set
, T
);
1850 convex_hull
= uset_convex_hull(set
);
1851 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1852 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1855 isl_basic_set_free(affine_hull
);
1860 /* Return an empty basic map living in the same space as "map".
1862 static __isl_give isl_basic_map
*replace_map_by_empty_basic_map(
1863 __isl_take isl_map
*map
)
1867 space
= isl_map_get_space(map
);
1869 return isl_basic_map_empty(space
);
1872 /* Compute the convex hull of a map.
1874 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1875 * specifically, the wrapping of facets to obtain new facets.
1877 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1879 struct isl_basic_set
*bset
;
1880 struct isl_basic_map
*model
= NULL
;
1881 struct isl_basic_set
*affine_hull
= NULL
;
1882 struct isl_basic_map
*convex_hull
= NULL
;
1883 struct isl_set
*set
= NULL
;
1885 map
= isl_map_detect_equalities(map
);
1886 map
= isl_map_align_divs_internal(map
);
1891 return replace_map_by_empty_basic_map(map
);
1893 model
= isl_basic_map_copy(map
->p
[0]);
1894 set
= isl_map_underlying_set(map
);
1898 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1901 if (affine_hull
->n_eq
!= 0)
1902 bset
= modulo_affine_hull(set
, affine_hull
);
1904 isl_basic_set_free(affine_hull
);
1905 bset
= uset_convex_hull(set
);
1908 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1912 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1913 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1914 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1918 isl_basic_map_free(model
);
1922 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1924 return bset_from_bmap(isl_map_convex_hull(set_to_map(set
)));
1927 __isl_give isl_basic_map
*isl_map_polyhedral_hull(__isl_take isl_map
*map
)
1929 isl_basic_map
*hull
;
1931 hull
= isl_map_convex_hull(map
);
1932 return isl_basic_map_remove_divs(hull
);
1935 __isl_give isl_basic_set
*isl_set_polyhedral_hull(__isl_take isl_set
*set
)
1937 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set
)));
1940 struct sh_data_entry
{
1941 struct isl_hash_table
*table
;
1942 struct isl_tab
*tab
;
1945 /* Holds the data needed during the simple hull computation.
1947 * n the number of basic sets in the original set
1948 * hull_table a hash table of already computed constraints
1949 * in the simple hull
1950 * p for each basic set,
1951 * table a hash table of the constraints
1952 * tab the tableau corresponding to the basic set
1955 struct isl_ctx
*ctx
;
1957 struct isl_hash_table
*hull_table
;
1958 struct sh_data_entry p
[1];
1961 static void sh_data_free(struct sh_data
*data
)
1967 isl_hash_table_free(data
->ctx
, data
->hull_table
);
1968 for (i
= 0; i
< data
->n
; ++i
) {
1969 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
1970 isl_tab_free(data
->p
[i
].tab
);
1975 struct ineq_cmp_data
{
1980 static int has_ineq(const void *entry
, const void *val
)
1982 isl_int
*row
= (isl_int
*)entry
;
1983 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
1985 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
1986 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
1989 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1990 isl_int
*ineq
, unsigned len
)
1993 struct ineq_cmp_data v
;
1994 struct isl_hash_table_entry
*entry
;
1998 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
1999 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
2006 /* Fill hash table "table" with the constraints of "bset".
2007 * Equalities are added as two inequalities.
2008 * The value in the hash table is a pointer to the (in)equality of "bset".
2010 static int hash_basic_set(struct isl_hash_table
*table
,
2011 __isl_keep isl_basic_set
*bset
)
2014 unsigned dim
= isl_basic_set_total_dim(bset
);
2016 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2017 for (j
= 0; j
< 2; ++j
) {
2018 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2019 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2023 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2024 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2030 static struct sh_data
*sh_data_alloc(__isl_keep isl_set
*set
, unsigned n_ineq
)
2032 struct sh_data
*data
;
2035 data
= isl_calloc(set
->ctx
, struct sh_data
,
2036 sizeof(struct sh_data
) +
2037 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2040 data
->ctx
= set
->ctx
;
2042 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2043 if (!data
->hull_table
)
2045 for (i
= 0; i
< set
->n
; ++i
) {
2046 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2047 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2048 if (!data
->p
[i
].table
)
2050 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2059 /* Check if inequality "ineq" is a bound for basic set "j" or if
2060 * it can be relaxed (by increasing the constant term) to become
2061 * a bound for that basic set. In the latter case, the constant
2063 * Relaxation of the constant term is only allowed if "shift" is set.
2065 * Return 1 if "ineq" is a bound
2066 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2067 * -1 if some error occurred
2069 static int is_bound(struct sh_data
*data
, __isl_keep isl_set
*set
, int j
,
2070 isl_int
*ineq
, int shift
)
2072 enum isl_lp_result res
;
2075 if (!data
->p
[j
].tab
) {
2076 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
], 0);
2077 if (!data
->p
[j
].tab
)
2083 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2085 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
)) {
2087 isl_int_sub(ineq
[0], ineq
[0], opt
);
2089 res
= isl_lp_unbounded
;
2094 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2095 res
== isl_lp_unbounded
? 0 : -1;
2098 /* Set the constant term of "ineq" to the maximum of those of the constraints
2099 * in the basic sets of "set" following "i" that are parallel to "ineq".
2100 * That is, if any of the basic sets of "set" following "i" have a more
2101 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2102 * "c_hash" is the hash value of the linear part of "ineq".
2103 * "v" has been set up for use by has_ineq.
2105 * Note that the two inequality constraints corresponding to an equality are
2106 * represented by the same inequality constraint in data->p[j].table
2107 * (but with different hash values). This means the constraint (or at
2108 * least its constant term) may need to be temporarily negated to get
2109 * the actually hashed constraint.
2111 static void set_max_constant_term(struct sh_data
*data
, __isl_keep isl_set
*set
,
2112 int i
, isl_int
*ineq
, uint32_t c_hash
, struct ineq_cmp_data
*v
)
2116 struct isl_hash_table_entry
*entry
;
2118 ctx
= isl_set_get_ctx(set
);
2119 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2123 entry
= isl_hash_table_find(ctx
, data
->p
[j
].table
,
2124 c_hash
, &has_ineq
, v
, 0);
2128 ineq_j
= entry
->data
;
2129 neg
= isl_seq_is_neg(ineq_j
+ 1, ineq
+ 1, v
->len
);
2131 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2132 if (isl_int_gt(ineq_j
[0], ineq
[0]))
2133 isl_int_set(ineq
[0], ineq_j
[0]);
2135 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2139 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2140 * become a bound on the whole set. If so, add the (relaxed) inequality
2141 * to "hull". Relaxation is only allowed if "shift" is set.
2143 * We first check if "hull" already contains a translate of the inequality.
2144 * If so, we are done.
2145 * Then, we check if any of the previous basic sets contains a translate
2146 * of the inequality. If so, then we have already considered this
2147 * inequality and we are done.
2148 * Otherwise, for each basic set other than "i", we check if the inequality
2149 * is a bound on the basic set, but first replace the constant term
2150 * by the maximal value of any translate of the inequality in any
2151 * of the following basic sets.
2152 * For previous basic sets, we know that they do not contain a translate
2153 * of the inequality, so we directly call is_bound.
2154 * For following basic sets, we first check if a translate of the
2155 * inequality appears in its description. If so, the constant term
2156 * of the inequality has already been updated with respect to this
2157 * translate and the inequality is therefore known to be a bound
2158 * of this basic set.
2160 static __isl_give isl_basic_set
*add_bound(__isl_take isl_basic_set
*hull
,
2161 struct sh_data
*data
, __isl_keep isl_set
*set
, int i
, isl_int
*ineq
,
2165 struct ineq_cmp_data v
;
2166 struct isl_hash_table_entry
*entry
;
2172 v
.len
= isl_basic_set_total_dim(hull
);
2174 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2176 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2181 for (j
= 0; j
< i
; ++j
) {
2182 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2183 c_hash
, has_ineq
, &v
, 0);
2190 k
= isl_basic_set_alloc_inequality(hull
);
2193 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2195 set_max_constant_term(data
, set
, i
, hull
->ineq
[k
], c_hash
, &v
);
2196 for (j
= 0; j
< i
; ++j
) {
2198 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2205 isl_basic_set_free_inequality(hull
, 1);
2209 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2211 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2212 c_hash
, has_ineq
, &v
, 0);
2215 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2222 isl_basic_set_free_inequality(hull
, 1);
2226 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2230 entry
->data
= hull
->ineq
[k
];
2234 isl_basic_set_free(hull
);
2238 /* Check if any inequality from basic set "i" is or can be relaxed to
2239 * become a bound on the whole set. If so, add the (relaxed) inequality
2240 * to "hull". Relaxation is only allowed if "shift" is set.
2242 static __isl_give isl_basic_set
*add_bounds(__isl_take isl_basic_set
*bset
,
2243 struct sh_data
*data
, __isl_keep isl_set
*set
, int i
, int shift
)
2246 unsigned dim
= isl_basic_set_total_dim(bset
);
2248 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2249 for (k
= 0; k
< 2; ++k
) {
2250 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2251 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
],
2255 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2256 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
], shift
);
2260 /* Compute a superset of the convex hull of set that is described
2261 * by only (translates of) the constraints in the constituents of set.
2262 * Translation is only allowed if "shift" is set.
2264 static __isl_give isl_basic_set
*uset_simple_hull(__isl_take isl_set
*set
,
2267 struct sh_data
*data
= NULL
;
2268 struct isl_basic_set
*hull
= NULL
;
2276 for (i
= 0; i
< set
->n
; ++i
) {
2279 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2282 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
2286 data
= sh_data_alloc(set
, n_ineq
);
2290 for (i
= 0; i
< set
->n
; ++i
)
2291 hull
= add_bounds(hull
, data
, set
, i
, shift
);
2299 isl_basic_set_free(hull
);
2304 /* Compute a superset of the convex hull of map that is described
2305 * by only (translates of) the constraints in the constituents of map.
2306 * Handle trivial cases where map is NULL or contains at most one disjunct.
2308 static __isl_give isl_basic_map
*map_simple_hull_trivial(
2309 __isl_take isl_map
*map
)
2311 isl_basic_map
*hull
;
2316 return replace_map_by_empty_basic_map(map
);
2318 hull
= isl_basic_map_copy(map
->p
[0]);
2323 /* Return a copy of the simple hull cached inside "map".
2324 * "shift" determines whether to return the cached unshifted or shifted
2327 static __isl_give isl_basic_map
*cached_simple_hull(__isl_take isl_map
*map
,
2330 isl_basic_map
*hull
;
2332 hull
= isl_basic_map_copy(map
->cached_simple_hull
[shift
]);
2338 /* Compute a superset of the convex hull of map that is described
2339 * by only (translates of) the constraints in the constituents of map.
2340 * Translation is only allowed if "shift" is set.
2342 * The constraints are sorted while removing redundant constraints
2343 * in order to indicate a preference of which constraints should
2344 * be preserved. In particular, pairs of constraints that are
2345 * sorted together are preferred to either both be preserved
2346 * or both be removed. The sorting is performed inside
2347 * isl_basic_map_remove_redundancies.
2349 * The result of the computation is stored in map->cached_simple_hull[shift]
2350 * such that it can be reused in subsequent calls. The cache is cleared
2351 * whenever the map is modified (in isl_map_cow).
2352 * Note that the results need to be stored in the input map for there
2353 * to be any chance that they may get reused. In particular, they
2354 * are stored in a copy of the input map that is saved before
2355 * the integer division alignment.
2357 static __isl_give isl_basic_map
*map_simple_hull(__isl_take isl_map
*map
,
2360 struct isl_set
*set
= NULL
;
2361 struct isl_basic_map
*model
= NULL
;
2362 struct isl_basic_map
*hull
;
2363 struct isl_basic_map
*affine_hull
;
2364 struct isl_basic_set
*bset
= NULL
;
2367 if (!map
|| map
->n
<= 1)
2368 return map_simple_hull_trivial(map
);
2370 if (map
->cached_simple_hull
[shift
])
2371 return cached_simple_hull(map
, shift
);
2373 map
= isl_map_detect_equalities(map
);
2374 if (!map
|| map
->n
<= 1)
2375 return map_simple_hull_trivial(map
);
2376 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2377 input
= isl_map_copy(map
);
2378 map
= isl_map_align_divs_internal(map
);
2379 model
= map
? isl_basic_map_copy(map
->p
[0]) : NULL
;
2381 set
= isl_map_underlying_set(map
);
2383 bset
= uset_simple_hull(set
, shift
);
2385 hull
= isl_basic_map_overlying_set(bset
, model
);
2387 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2388 hull
= isl_basic_map_remove_redundancies(hull
);
2391 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2392 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2395 hull
= isl_basic_map_finalize(hull
);
2397 input
->cached_simple_hull
[shift
] = isl_basic_map_copy(hull
);
2398 isl_map_free(input
);
2403 /* Compute a superset of the convex hull of map that is described
2404 * by only translates of the constraints in the constituents of map.
2406 __isl_give isl_basic_map
*isl_map_simple_hull(__isl_take isl_map
*map
)
2408 return map_simple_hull(map
, 1);
2411 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2413 return bset_from_bmap(isl_map_simple_hull(set_to_map(set
)));
2416 /* Compute a superset of the convex hull of map that is described
2417 * by only the constraints in the constituents of map.
2419 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull(
2420 __isl_take isl_map
*map
)
2422 return map_simple_hull(map
, 0);
2425 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull(
2426 __isl_take isl_set
*set
)
2428 return isl_map_unshifted_simple_hull(set
);
2431 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2432 * A constraint that appears with different constant terms
2433 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2434 * (i.e., greatest) constant term.
2435 * "bmap1" and "bmap2" are assumed to have the same (known)
2436 * integer divisions.
2437 * The constraints of both "bmap1" and "bmap2" are assumed
2438 * to have been sorted using isl_basic_map_sort_constraints.
2440 * Run through the inequality constraints of "bmap1" and "bmap2"
2442 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2444 * If a match is found, the constraint is kept. If needed, the constant
2445 * term of the constraint is adjusted.
2447 static __isl_give isl_basic_map
*select_shared_inequalities(
2448 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2452 bmap1
= isl_basic_map_cow(bmap1
);
2453 if (!bmap1
|| !bmap2
)
2454 return isl_basic_map_free(bmap1
);
2456 i1
= bmap1
->n_ineq
- 1;
2457 i2
= bmap2
->n_ineq
- 1;
2458 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2461 cmp
= isl_basic_map_constraint_cmp(bmap1
, bmap1
->ineq
[i1
],
2468 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2469 bmap1
= isl_basic_map_free(bmap1
);
2473 if (isl_int_lt(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]))
2474 isl_int_set(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]);
2478 for (; i1
>= 0; --i1
)
2479 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2480 bmap1
= isl_basic_map_free(bmap1
);
2485 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2486 * "bmap1" and "bmap2" are assumed to have the same (known)
2487 * integer divisions.
2489 * Run through the equality constraints of "bmap1" and "bmap2".
2490 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2493 static __isl_give isl_basic_map
*select_shared_equalities(
2494 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2499 bmap1
= isl_basic_map_cow(bmap1
);
2500 if (!bmap1
|| !bmap2
)
2501 return isl_basic_map_free(bmap1
);
2503 total
= isl_basic_map_total_dim(bmap1
);
2505 i1
= bmap1
->n_eq
- 1;
2506 i2
= bmap2
->n_eq
- 1;
2507 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2510 last1
= isl_seq_last_non_zero(bmap1
->eq
[i1
] + 1, total
);
2511 last2
= isl_seq_last_non_zero(bmap2
->eq
[i2
] + 1, total
);
2512 if (last1
> last2
) {
2516 if (last1
< last2
) {
2517 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2518 bmap1
= isl_basic_map_free(bmap1
);
2522 if (!isl_seq_eq(bmap1
->eq
[i1
], bmap2
->eq
[i2
], 1 + total
)) {
2523 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2524 bmap1
= isl_basic_map_free(bmap1
);
2529 for (; i1
>= 0; --i1
)
2530 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2531 bmap1
= isl_basic_map_free(bmap1
);
2536 /* Compute a superset of "bmap1" and "bmap2" that is described
2537 * by only the constraints that appear in both "bmap1" and "bmap2".
2539 * First drop constraints that involve unknown integer divisions
2540 * since it is not trivial to check whether two such integer divisions
2541 * in different basic maps are the same.
2542 * Then align the remaining (known) divs and sort the constraints.
2543 * Finally drop all inequalities and equalities from "bmap1" that
2544 * do not also appear in "bmap2".
2546 __isl_give isl_basic_map
*isl_basic_map_plain_unshifted_simple_hull(
2547 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
2549 bmap1
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap1
);
2550 bmap2
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap2
);
2551 bmap2
= isl_basic_map_align_divs(bmap2
, bmap1
);
2552 bmap1
= isl_basic_map_align_divs(bmap1
, bmap2
);
2553 bmap1
= isl_basic_map_gauss(bmap1
, NULL
);
2554 bmap2
= isl_basic_map_gauss(bmap2
, NULL
);
2555 bmap1
= isl_basic_map_sort_constraints(bmap1
);
2556 bmap2
= isl_basic_map_sort_constraints(bmap2
);
2558 bmap1
= select_shared_inequalities(bmap1
, bmap2
);
2559 bmap1
= select_shared_equalities(bmap1
, bmap2
);
2561 isl_basic_map_free(bmap2
);
2562 bmap1
= isl_basic_map_finalize(bmap1
);
2566 /* Compute a superset of the convex hull of "map" that is described
2567 * by only the constraints in the constituents of "map".
2568 * In particular, the result is composed of constraints that appear
2569 * in each of the basic maps of "map"
2571 * Constraints that involve unknown integer divisions are dropped
2572 * since it is not trivial to check whether two such integer divisions
2573 * in different basic maps are the same.
2575 * The hull is initialized from the first basic map and then
2576 * updated with respect to the other basic maps in turn.
2578 __isl_give isl_basic_map
*isl_map_plain_unshifted_simple_hull(
2579 __isl_take isl_map
*map
)
2582 isl_basic_map
*hull
;
2587 return map_simple_hull_trivial(map
);
2588 map
= isl_map_drop_constraint_involving_unknown_divs(map
);
2589 hull
= isl_basic_map_copy(map
->p
[0]);
2590 for (i
= 1; i
< map
->n
; ++i
) {
2591 isl_basic_map
*bmap_i
;
2593 bmap_i
= isl_basic_map_copy(map
->p
[i
]);
2594 hull
= isl_basic_map_plain_unshifted_simple_hull(hull
, bmap_i
);
2601 /* Compute a superset of the convex hull of "set" that is described
2602 * by only the constraints in the constituents of "set".
2603 * In particular, the result is composed of constraints that appear
2604 * in each of the basic sets of "set"
2606 __isl_give isl_basic_set
*isl_set_plain_unshifted_simple_hull(
2607 __isl_take isl_set
*set
)
2609 return isl_map_plain_unshifted_simple_hull(set
);
2612 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2614 * For each basic set in "set", we first check if the basic set
2615 * contains a translate of "ineq". If this translate is more relaxed,
2616 * then we assume that "ineq" is not a bound on this basic set.
2617 * Otherwise, we know that it is a bound.
2618 * If the basic set does not contain a translate of "ineq", then
2619 * we call is_bound to perform the test.
2621 static __isl_give isl_basic_set
*add_bound_from_constraint(
2622 __isl_take isl_basic_set
*hull
, struct sh_data
*data
,
2623 __isl_keep isl_set
*set
, isl_int
*ineq
)
2628 struct ineq_cmp_data v
;
2631 return isl_basic_set_free(hull
);
2633 v
.len
= isl_basic_set_total_dim(hull
);
2635 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2637 ctx
= isl_basic_set_get_ctx(hull
);
2638 for (i
= 0; i
< set
->n
; ++i
) {
2640 struct isl_hash_table_entry
*entry
;
2642 entry
= isl_hash_table_find(ctx
, data
->p
[i
].table
,
2643 c_hash
, &has_ineq
, &v
, 0);
2645 isl_int
*ineq_i
= entry
->data
;
2646 int neg
, more_relaxed
;
2648 neg
= isl_seq_is_neg(ineq_i
+ 1, ineq
+ 1, v
.len
);
2650 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2651 more_relaxed
= isl_int_gt(ineq_i
[0], ineq
[0]);
2653 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2659 bound
= is_bound(data
, set
, i
, ineq
, 0);
2661 return isl_basic_set_free(hull
);
2668 k
= isl_basic_set_alloc_inequality(hull
);
2670 return isl_basic_set_free(hull
);
2671 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2676 /* Compute a superset of the convex hull of "set" that is described
2677 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2678 * has no parameters or integer divisions.
2680 * The inequalities in "ineq" are assumed to have been sorted such
2681 * that constraints with the same linear part appear together and
2682 * that among constraints with the same linear part, those with
2683 * smaller constant term appear first.
2685 * We reuse the same data structure that is used by uset_simple_hull,
2686 * but we do not need the hull table since we will not consider the
2687 * same constraint more than once. We therefore allocate it with zero size.
2689 * We run through the constraints and try to add them one by one,
2690 * skipping identical constraints. If we have added a constraint and
2691 * the next constraint is a more relaxed translate, then we skip this
2692 * next constraint as well.
2694 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_constraints(
2695 __isl_take isl_set
*set
, int n_ineq
, isl_int
**ineq
)
2699 struct sh_data
*data
= NULL
;
2700 isl_basic_set
*hull
= NULL
;
2703 hull
= isl_basic_set_alloc_space(isl_set_get_space(set
), 0, 0, n_ineq
);
2707 data
= sh_data_alloc(set
, 0);
2711 dim
= isl_set_dim(set
, isl_dim_set
);
2712 for (i
= 0; i
< n_ineq
; ++i
) {
2713 int hull_n_ineq
= hull
->n_ineq
;
2716 parallel
= i
> 0 && isl_seq_eq(ineq
[i
- 1] + 1, ineq
[i
] + 1,
2719 (last_added
|| isl_int_eq(ineq
[i
- 1][0], ineq
[i
][0])))
2721 hull
= add_bound_from_constraint(hull
, data
, set
, ineq
[i
]);
2724 last_added
= hull
->n_ineq
> hull_n_ineq
;
2733 isl_basic_set_free(hull
);
2737 /* Collect pointers to all the inequalities in the elements of "list"
2738 * in "ineq". For equalities, store both a pointer to the equality and
2739 * a pointer to its opposite, which is first copied to "mat".
2740 * "ineq" and "mat" are assumed to have been preallocated to the right size
2741 * (the number of inequalities + 2 times the number of equalites and
2742 * the number of equalities, respectively).
2744 static __isl_give isl_mat
*collect_inequalities(__isl_take isl_mat
*mat
,
2745 __isl_keep isl_basic_set_list
*list
, isl_int
**ineq
)
2747 int i
, j
, n
, n_eq
, n_ineq
;
2754 n
= isl_basic_set_list_n_basic_set(list
);
2755 for (i
= 0; i
< n
; ++i
) {
2756 isl_basic_set
*bset
;
2757 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2759 return isl_mat_free(mat
);
2760 for (j
= 0; j
< bset
->n_eq
; ++j
) {
2761 ineq
[n_ineq
++] = mat
->row
[n_eq
];
2762 ineq
[n_ineq
++] = bset
->eq
[j
];
2763 isl_seq_neg(mat
->row
[n_eq
++], bset
->eq
[j
], mat
->n_col
);
2765 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2766 ineq
[n_ineq
++] = bset
->ineq
[j
];
2767 isl_basic_set_free(bset
);
2773 /* Comparison routine for use as an isl_sort callback.
2775 * Constraints with the same linear part are sorted together and
2776 * among constraints with the same linear part, those with smaller
2777 * constant term are sorted first.
2779 static int cmp_ineq(const void *a
, const void *b
, void *arg
)
2781 unsigned dim
= *(unsigned *) arg
;
2782 isl_int
* const *ineq1
= a
;
2783 isl_int
* const *ineq2
= b
;
2786 cmp
= isl_seq_cmp((*ineq1
) + 1, (*ineq2
) + 1, dim
);
2789 return isl_int_cmp((*ineq1
)[0], (*ineq2
)[0]);
2792 /* Compute a superset of the convex hull of "set" that is described
2793 * by only constraints in the elements of "list", where "set" has
2794 * no parameters or integer divisions.
2796 * We collect all the constraints in those elements and then
2797 * sort the constraints such that constraints with the same linear part
2798 * are sorted together and that those with smaller constant term are
2801 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_basic_set_list(
2802 __isl_take isl_set
*set
, __isl_take isl_basic_set_list
*list
)
2804 int i
, n
, n_eq
, n_ineq
;
2807 isl_mat
*mat
= NULL
;
2808 isl_int
**ineq
= NULL
;
2809 isl_basic_set
*hull
;
2813 ctx
= isl_set_get_ctx(set
);
2817 n
= isl_basic_set_list_n_basic_set(list
);
2818 for (i
= 0; i
< n
; ++i
) {
2819 isl_basic_set
*bset
;
2820 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2824 n_ineq
+= 2 * bset
->n_eq
+ bset
->n_ineq
;
2825 isl_basic_set_free(bset
);
2828 ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
);
2829 if (n_ineq
> 0 && !ineq
)
2832 dim
= isl_set_dim(set
, isl_dim_set
);
2833 mat
= isl_mat_alloc(ctx
, n_eq
, 1 + dim
);
2834 mat
= collect_inequalities(mat
, list
, ineq
);
2838 if (isl_sort(ineq
, n_ineq
, sizeof(ineq
[0]), &cmp_ineq
, &dim
) < 0)
2841 hull
= uset_unshifted_simple_hull_from_constraints(set
, n_ineq
, ineq
);
2845 isl_basic_set_list_free(list
);
2851 isl_basic_set_list_free(list
);
2855 /* Compute a superset of the convex hull of "map" that is described
2856 * by only constraints in the elements of "list".
2858 * If the list is empty, then we can only describe the universe set.
2859 * If the input map is empty, then all constraints are valid, so
2860 * we return the intersection of the elements in "list".
2862 * Otherwise, we align all divs and temporarily treat them
2863 * as regular variables, computing the unshifted simple hull in
2864 * uset_unshifted_simple_hull_from_basic_set_list.
2866 static __isl_give isl_basic_map
*map_unshifted_simple_hull_from_basic_map_list(
2867 __isl_take isl_map
*map
, __isl_take isl_basic_map_list
*list
)
2869 isl_basic_map
*model
;
2870 isl_basic_map
*hull
;
2872 isl_basic_set_list
*bset_list
;
2877 if (isl_basic_map_list_n_basic_map(list
) == 0) {
2880 space
= isl_map_get_space(map
);
2882 isl_basic_map_list_free(list
);
2883 return isl_basic_map_universe(space
);
2885 if (isl_map_plain_is_empty(map
)) {
2887 return isl_basic_map_list_intersect(list
);
2890 map
= isl_map_align_divs_to_basic_map_list(map
, list
);
2893 list
= isl_basic_map_list_align_divs_to_basic_map(list
, map
->p
[0]);
2895 model
= isl_basic_map_list_get_basic_map(list
, 0);
2897 set
= isl_map_underlying_set(map
);
2898 bset_list
= isl_basic_map_list_underlying_set(list
);
2900 hull
= uset_unshifted_simple_hull_from_basic_set_list(set
, bset_list
);
2901 hull
= isl_basic_map_overlying_set(hull
, model
);
2906 isl_basic_map_list_free(list
);
2910 /* Return a sequence of the basic maps that make up the maps in "list".
2912 static __isl_give isl_basic_map_list
*collect_basic_maps(
2913 __isl_take isl_map_list
*list
)
2917 isl_basic_map_list
*bmap_list
;
2921 n
= isl_map_list_n_map(list
);
2922 ctx
= isl_map_list_get_ctx(list
);
2923 bmap_list
= isl_basic_map_list_alloc(ctx
, 0);
2925 for (i
= 0; i
< n
; ++i
) {
2927 isl_basic_map_list
*list_i
;
2929 map
= isl_map_list_get_map(list
, i
);
2930 map
= isl_map_compute_divs(map
);
2931 list_i
= isl_map_get_basic_map_list(map
);
2933 bmap_list
= isl_basic_map_list_concat(bmap_list
, list_i
);
2936 isl_map_list_free(list
);
2940 /* Compute a superset of the convex hull of "map" that is described
2941 * by only constraints in the elements of "list".
2943 * If "map" is the universe, then the convex hull (and therefore
2944 * any superset of the convexhull) is the universe as well.
2946 * Otherwise, we collect all the basic maps in the map list and
2947 * continue with map_unshifted_simple_hull_from_basic_map_list.
2949 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull_from_map_list(
2950 __isl_take isl_map
*map
, __isl_take isl_map_list
*list
)
2952 isl_basic_map_list
*bmap_list
;
2955 is_universe
= isl_map_plain_is_universe(map
);
2956 if (is_universe
< 0)
2957 map
= isl_map_free(map
);
2958 if (is_universe
< 0 || is_universe
) {
2959 isl_map_list_free(list
);
2960 return isl_map_unshifted_simple_hull(map
);
2963 bmap_list
= collect_basic_maps(list
);
2964 return map_unshifted_simple_hull_from_basic_map_list(map
, bmap_list
);
2967 /* Compute a superset of the convex hull of "set" that is described
2968 * by only constraints in the elements of "list".
2970 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull_from_set_list(
2971 __isl_take isl_set
*set
, __isl_take isl_set_list
*list
)
2973 return isl_map_unshifted_simple_hull_from_map_list(set
, list
);
2976 /* Given a set "set", return parametric bounds on the dimension "dim".
2978 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
2980 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
2981 set
= isl_set_copy(set
);
2982 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
2983 set
= isl_set_eliminate_dims(set
, 0, dim
);
2984 return isl_set_convex_hull(set
);
2987 /* Computes a "simple hull" and then check if each dimension in the
2988 * resulting hull is bounded by a symbolic constant. If not, the
2989 * hull is intersected with the corresponding bounds on the whole set.
2991 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
2994 struct isl_basic_set
*hull
;
2995 unsigned nparam
, left
;
2996 int removed_divs
= 0;
2998 hull
= isl_set_simple_hull(isl_set_copy(set
));
3002 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
3003 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
3004 int lower
= 0, upper
= 0;
3005 struct isl_basic_set
*bounds
;
3007 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
3008 for (j
= 0; j
< hull
->n_eq
; ++j
) {
3009 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
3011 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
3018 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
3019 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
3021 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
3023 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
3026 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
3037 if (!removed_divs
) {
3038 set
= isl_set_remove_divs(set
);
3043 bounds
= set_bounds(set
, i
);
3044 hull
= isl_basic_set_intersect(hull
, bounds
);
3053 isl_basic_set_free(hull
);