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 isl_bool
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
);
157 return isl_bool_error
;
160 static __isl_give isl_set
*isl_set_add_basic_set_equality(
161 __isl_take isl_set
*set
, isl_int
*c
)
165 set
= isl_set_cow(set
);
168 for (i
= 0; i
< set
->n
; ++i
) {
169 set
->p
[i
] = isl_basic_set_add_eq(set
->p
[i
], c
);
179 /* Given a union of basic sets, construct the constraints for wrapping
180 * a facet around one of its ridges.
181 * In particular, if each of n the d-dimensional basic sets i in "set"
182 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
183 * and is defined by the constraints
187 * then the resulting set is of dimension n*(1+d) and has as constraints
196 static __isl_give isl_basic_set
*wrap_constraints(__isl_keep isl_set
*set
)
198 struct isl_basic_set
*lp
;
202 unsigned dim
, lp_dim
;
207 dim
= 1 + isl_set_n_dim(set
);
210 for (i
= 0; i
< set
->n
; ++i
) {
211 n_eq
+= set
->p
[i
]->n_eq
;
212 n_ineq
+= set
->p
[i
]->n_ineq
;
214 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
215 lp
= isl_basic_set_set_rational(lp
);
218 lp_dim
= isl_basic_set_n_dim(lp
);
219 k
= isl_basic_set_alloc_equality(lp
);
220 isl_int_set_si(lp
->eq
[k
][0], -1);
221 for (i
= 0; i
< set
->n
; ++i
) {
222 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
223 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
224 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
226 for (i
= 0; i
< set
->n
; ++i
) {
227 k
= isl_basic_set_alloc_inequality(lp
);
228 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
229 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
231 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
232 k
= isl_basic_set_alloc_equality(lp
);
233 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
234 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
235 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
238 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
239 k
= isl_basic_set_alloc_inequality(lp
);
240 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
241 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
242 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
248 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
249 * of that facet, compute the other facet of the convex hull that contains
252 * We first transform the set such that the facet constraint becomes
256 * I.e., the facet lies in
260 * and on that facet, the constraint that defines the ridge is
264 * (This transformation is not strictly needed, all that is needed is
265 * that the ridge contains the origin.)
267 * Since the ridge contains the origin, the cone of the convex hull
268 * will be of the form
273 * with this second constraint defining the new facet.
274 * The constant a is obtained by settting x_1 in the cone of the
275 * convex hull to 1 and minimizing x_2.
276 * Now, each element in the cone of the convex hull is the sum
277 * of elements in the cones of the basic sets.
278 * If a_i is the dilation factor of basic set i, then the problem
279 * we need to solve is
292 * the constraints of each (transformed) basic set.
293 * If a = n/d, then the constraint defining the new facet (in the transformed
296 * -n x_1 + d x_2 >= 0
298 * In the original space, we need to take the same combination of the
299 * corresponding constraints "facet" and "ridge".
301 * If a = -infty = "-1/0", then we just return the original facet constraint.
302 * This means that the facet is unbounded, but has a bounded intersection
303 * with the union of sets.
305 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
306 isl_int
*facet
, isl_int
*ridge
)
310 struct isl_mat
*T
= NULL
;
311 struct isl_basic_set
*lp
= NULL
;
313 enum isl_lp_result res
;
320 set
= isl_set_copy(set
);
321 set
= isl_set_set_rational(set
);
323 dim
= 1 + isl_set_n_dim(set
);
324 T
= isl_mat_alloc(ctx
, 3, dim
);
327 isl_int_set_si(T
->row
[0][0], 1);
328 isl_seq_clr(T
->row
[0]+1, dim
- 1);
329 isl_seq_cpy(T
->row
[1], facet
, dim
);
330 isl_seq_cpy(T
->row
[2], ridge
, dim
);
331 T
= isl_mat_right_inverse(T
);
332 set
= isl_set_preimage(set
, T
);
336 lp
= wrap_constraints(set
);
337 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
340 isl_int_set_si(obj
->block
.data
[0], 0);
341 for (i
= 0; i
< set
->n
; ++i
) {
342 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
343 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
344 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
348 res
= isl_basic_set_solve_lp(lp
, 0,
349 obj
->block
.data
, ctx
->one
, &num
, &den
, NULL
);
350 if (res
== isl_lp_ok
) {
351 isl_int_neg(num
, num
);
352 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
353 isl_seq_normalize(ctx
, facet
, dim
);
358 isl_basic_set_free(lp
);
360 if (res
== isl_lp_error
)
362 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
366 isl_basic_set_free(lp
);
372 /* Compute the constraint of a facet of "set".
374 * We first compute the intersection with a bounding constraint
375 * that is orthogonal to one of the coordinate axes.
376 * If the affine hull of this intersection has only one equality,
377 * we have found a facet.
378 * Otherwise, we wrap the current bounding constraint around
379 * one of the equalities of the face (one that is not equal to
380 * the current bounding constraint).
381 * This process continues until we have found a facet.
382 * The dimension of the intersection increases by at least
383 * one on each iteration, so termination is guaranteed.
385 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
387 struct isl_set
*slice
= NULL
;
388 struct isl_basic_set
*face
= NULL
;
390 unsigned dim
= isl_set_n_dim(set
);
392 isl_mat
*bounds
= NULL
;
394 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
395 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
399 isl_seq_clr(bounds
->row
[0], dim
);
400 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
401 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
404 isl_assert(set
->ctx
, is_bound
, goto error
);
405 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
409 slice
= isl_set_copy(set
);
410 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
411 face
= isl_set_affine_hull(slice
);
414 if (face
->n_eq
== 1) {
415 isl_basic_set_free(face
);
418 for (i
= 0; i
< face
->n_eq
; ++i
)
419 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
420 !isl_seq_is_neg(bounds
->row
[0],
421 face
->eq
[i
], 1 + dim
))
423 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
424 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
426 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
427 isl_basic_set_free(face
);
432 isl_basic_set_free(face
);
433 isl_mat_free(bounds
);
437 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
438 * compute a hyperplane description of the facet, i.e., compute the facets
441 * We compute an affine transformation that transforms the constraint
450 * by computing the right inverse U of a matrix that starts with the rows
463 * Since z_1 is zero, we can drop this variable as well as the corresponding
464 * column of U to obtain
472 * with Q' equal to Q, but without the corresponding row.
473 * After computing the facets of the facet in the z' space,
474 * we convert them back to the x space through Q.
476 static __isl_give isl_basic_set
*compute_facet(__isl_keep isl_set
*set
,
479 struct isl_mat
*m
, *U
, *Q
;
480 struct isl_basic_set
*facet
= NULL
;
485 set
= isl_set_copy(set
);
486 dim
= isl_set_n_dim(set
);
487 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
490 isl_int_set_si(m
->row
[0][0], 1);
491 isl_seq_clr(m
->row
[0]+1, dim
);
492 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
493 U
= isl_mat_right_inverse(m
);
494 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
495 U
= isl_mat_drop_cols(U
, 1, 1);
496 Q
= isl_mat_drop_rows(Q
, 1, 1);
497 set
= isl_set_preimage(set
, U
);
498 facet
= uset_convex_hull_wrap_bounded(set
);
499 facet
= isl_basic_set_preimage(facet
, Q
);
500 if (facet
&& facet
->n_eq
!= 0)
501 isl_die(ctx
, isl_error_internal
, "unexpected equality",
502 return isl_basic_set_free(facet
));
505 isl_basic_set_free(facet
);
510 /* Given an initial facet constraint, compute the remaining facets.
511 * We do this by running through all facets found so far and computing
512 * the adjacent facets through wrapping, adding those facets that we
513 * hadn't already found before.
515 * For each facet we have found so far, we first compute its facets
516 * in the resulting convex hull. That is, we compute the ridges
517 * of the resulting convex hull contained in the facet.
518 * We also compute the corresponding facet in the current approximation
519 * of the convex hull. There is no need to wrap around the ridges
520 * in this facet since that would result in a facet that is already
521 * present in the current approximation.
523 * This function can still be significantly optimized by checking which of
524 * the facets of the basic sets are also facets of the convex hull and
525 * using all the facets so far to help in constructing the facets of the
528 * using the technique in section "3.1 Ridge Generation" of
529 * "Extended Convex Hull" by Fukuda et al.
531 static __isl_give isl_basic_set
*extend(__isl_take isl_basic_set
*hull
,
532 __isl_keep isl_set
*set
)
536 struct isl_basic_set
*facet
= NULL
;
537 struct isl_basic_set
*hull_facet
= NULL
;
543 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
545 dim
= isl_set_n_dim(set
);
547 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
548 facet
= compute_facet(set
, hull
->ineq
[i
]);
549 facet
= isl_basic_set_add_eq(facet
, hull
->ineq
[i
]);
550 facet
= isl_basic_set_gauss(facet
, NULL
);
551 facet
= isl_basic_set_normalize_constraints(facet
);
552 hull_facet
= isl_basic_set_copy(hull
);
553 hull_facet
= isl_basic_set_add_eq(hull_facet
, hull
->ineq
[i
]);
554 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
555 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
556 if (!facet
|| !hull_facet
)
558 hull
= isl_basic_set_cow(hull
);
559 hull
= isl_basic_set_extend_space(hull
,
560 isl_space_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
563 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
564 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
565 if (isl_seq_eq(facet
->ineq
[j
],
566 hull_facet
->ineq
[f
], 1 + dim
))
568 if (f
< hull_facet
->n_ineq
)
570 k
= isl_basic_set_alloc_inequality(hull
);
573 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
574 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
577 isl_basic_set_free(hull_facet
);
578 isl_basic_set_free(facet
);
580 hull
= isl_basic_set_simplify(hull
);
581 hull
= isl_basic_set_finalize(hull
);
584 isl_basic_set_free(hull_facet
);
585 isl_basic_set_free(facet
);
586 isl_basic_set_free(hull
);
590 /* Special case for computing the convex hull of a one dimensional set.
591 * We simply collect the lower and upper bounds of each basic set
592 * and the biggest of those.
594 static __isl_give isl_basic_set
*convex_hull_1d(__isl_take isl_set
*set
)
596 struct isl_mat
*c
= NULL
;
597 isl_int
*lower
= NULL
;
598 isl_int
*upper
= NULL
;
601 struct isl_basic_set
*hull
;
603 for (i
= 0; i
< set
->n
; ++i
) {
604 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
608 set
= isl_set_remove_empty_parts(set
);
611 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
612 c
= isl_mat_alloc(set
->ctx
, 2, 2);
616 if (set
->p
[0]->n_eq
> 0) {
617 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
620 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
621 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
622 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
624 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
625 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
628 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
629 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
631 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
634 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
641 for (i
= 0; i
< set
->n
; ++i
) {
642 struct isl_basic_set
*bset
= set
->p
[i
];
646 for (j
= 0; j
< bset
->n_eq
; ++j
) {
650 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
651 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
652 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
653 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
654 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
655 isl_seq_neg(lower
, bset
->eq
[j
], 2);
658 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
659 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
660 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
661 isl_seq_neg(upper
, bset
->eq
[j
], 2);
662 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
663 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
666 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
667 if (isl_int_is_pos(bset
->ineq
[j
][1]))
669 if (isl_int_is_neg(bset
->ineq
[j
][1]))
671 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
672 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
673 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
674 if (isl_int_lt(a
, b
))
675 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
677 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
678 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
679 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
680 if (isl_int_gt(a
, b
))
681 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
692 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
693 hull
= isl_basic_set_set_rational(hull
);
697 k
= isl_basic_set_alloc_inequality(hull
);
698 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
701 k
= isl_basic_set_alloc_inequality(hull
);
702 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
704 hull
= isl_basic_set_finalize(hull
);
714 static __isl_give isl_basic_set
*convex_hull_0d(__isl_take isl_set
*set
)
716 struct isl_basic_set
*convex_hull
;
721 if (isl_set_is_empty(set
))
722 convex_hull
= isl_basic_set_empty(isl_space_copy(set
->dim
));
724 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
729 /* Compute the convex hull of a pair of basic sets without any parameters or
730 * integer divisions using Fourier-Motzkin elimination.
731 * The convex hull is the set of all points that can be written as
732 * the sum of points from both basic sets (in homogeneous coordinates).
733 * We set up the constraints in a space with dimensions for each of
734 * the three sets and then project out the dimensions corresponding
735 * to the two original basic sets, retaining only those corresponding
736 * to the convex hull.
738 static __isl_give isl_basic_set
*convex_hull_pair_elim(
739 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
742 struct isl_basic_set
*bset
[2];
743 struct isl_basic_set
*hull
= NULL
;
746 if (!bset1
|| !bset2
)
749 dim
= isl_basic_set_n_dim(bset1
);
750 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
751 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
752 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
755 for (i
= 0; i
< 2; ++i
) {
756 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
757 k
= isl_basic_set_alloc_equality(hull
);
760 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
761 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
762 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
765 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
766 k
= isl_basic_set_alloc_inequality(hull
);
769 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
770 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
771 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
772 bset
[i
]->ineq
[j
], 1+dim
);
774 k
= isl_basic_set_alloc_inequality(hull
);
777 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
778 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
780 for (j
= 0; j
< 1+dim
; ++j
) {
781 k
= isl_basic_set_alloc_equality(hull
);
784 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
785 isl_int_set_si(hull
->eq
[k
][j
], -1);
786 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
787 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
789 hull
= isl_basic_set_set_rational(hull
);
790 hull
= isl_basic_set_remove_dims(hull
, isl_dim_set
, dim
, 2*(1+dim
));
791 hull
= isl_basic_set_remove_redundancies(hull
);
792 isl_basic_set_free(bset1
);
793 isl_basic_set_free(bset2
);
796 isl_basic_set_free(bset1
);
797 isl_basic_set_free(bset2
);
798 isl_basic_set_free(hull
);
802 /* Is the set bounded for each value of the parameters?
804 isl_bool
isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
810 return isl_bool_error
;
811 if (isl_basic_set_plain_is_empty(bset
))
812 return isl_bool_true
;
814 tab
= isl_tab_from_recession_cone(bset
, 1);
815 bounded
= isl_tab_cone_is_bounded(tab
);
820 /* Is the image bounded for each value of the parameters and
821 * the domain variables?
823 isl_bool
isl_basic_map_image_is_bounded(__isl_keep isl_basic_map
*bmap
)
825 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
826 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
829 bmap
= isl_basic_map_copy(bmap
);
830 bmap
= isl_basic_map_cow(bmap
);
831 bmap
= isl_basic_map_move_dims(bmap
, isl_dim_param
, nparam
,
832 isl_dim_in
, 0, n_in
);
833 bounded
= isl_basic_set_is_bounded(bset_from_bmap(bmap
));
834 isl_basic_map_free(bmap
);
839 /* Is the set bounded for each value of the parameters?
841 isl_bool
isl_set_is_bounded(__isl_keep isl_set
*set
)
846 return isl_bool_error
;
848 for (i
= 0; i
< set
->n
; ++i
) {
849 isl_bool bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
850 if (!bounded
|| bounded
< 0)
853 return isl_bool_true
;
856 /* Compute the lineality space of the convex hull of bset1 and bset2.
858 * We first compute the intersection of the recession cone of bset1
859 * with the negative of the recession cone of bset2 and then compute
860 * the linear hull of the resulting cone.
862 static __isl_give isl_basic_set
*induced_lineality_space(
863 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
866 struct isl_basic_set
*lin
= NULL
;
869 if (!bset1
|| !bset2
)
872 dim
= isl_basic_set_total_dim(bset1
);
873 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset1
), 0,
874 bset1
->n_eq
+ bset2
->n_eq
,
875 bset1
->n_ineq
+ bset2
->n_ineq
);
876 lin
= isl_basic_set_set_rational(lin
);
879 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
880 k
= isl_basic_set_alloc_equality(lin
);
883 isl_int_set_si(lin
->eq
[k
][0], 0);
884 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
886 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
887 k
= isl_basic_set_alloc_inequality(lin
);
890 isl_int_set_si(lin
->ineq
[k
][0], 0);
891 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
893 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
894 k
= isl_basic_set_alloc_equality(lin
);
897 isl_int_set_si(lin
->eq
[k
][0], 0);
898 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
900 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
901 k
= isl_basic_set_alloc_inequality(lin
);
904 isl_int_set_si(lin
->ineq
[k
][0], 0);
905 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
908 isl_basic_set_free(bset1
);
909 isl_basic_set_free(bset2
);
910 return isl_basic_set_affine_hull(lin
);
912 isl_basic_set_free(lin
);
913 isl_basic_set_free(bset1
);
914 isl_basic_set_free(bset2
);
918 static __isl_give isl_basic_set
*uset_convex_hull(__isl_take isl_set
*set
);
920 /* Given a set and a linear space "lin" of dimension n > 0,
921 * project the linear space from the set, compute the convex hull
922 * and then map the set back to the original space.
928 * describe the linear space. We first compute the Hermite normal
929 * form H = M U of M = H Q, to obtain
933 * The last n rows of H will be zero, so the last n variables of x' = Q x
934 * are the one we want to project out. We do this by transforming each
935 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
936 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
937 * we transform the hull back to the original space as A' Q_1 x >= b',
938 * with Q_1 all but the last n rows of Q.
940 static __isl_give isl_basic_set
*modulo_lineality(__isl_take isl_set
*set
,
941 __isl_take isl_basic_set
*lin
)
943 unsigned total
= isl_basic_set_total_dim(lin
);
945 struct isl_basic_set
*hull
;
946 struct isl_mat
*M
, *U
, *Q
;
950 lin_dim
= total
- lin
->n_eq
;
951 M
= isl_mat_sub_alloc6(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
952 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
956 isl_basic_set_free(lin
);
958 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
960 U
= isl_mat_lin_to_aff(U
);
961 Q
= isl_mat_lin_to_aff(Q
);
963 set
= isl_set_preimage(set
, U
);
964 set
= isl_set_remove_dims(set
, isl_dim_set
, total
- lin_dim
, lin_dim
);
965 hull
= uset_convex_hull(set
);
966 hull
= isl_basic_set_preimage(hull
, Q
);
970 isl_basic_set_free(lin
);
975 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
976 * set up an LP for solving
978 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
980 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
981 * The next \alpha{ij} correspond to the equalities and come in pairs.
982 * The final \alpha{ij} correspond to the inequalities.
984 static __isl_give isl_basic_set
*valid_direction_lp(
985 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
988 struct isl_basic_set
*lp
;
993 if (!bset1
|| !bset2
)
995 d
= 1 + isl_basic_set_total_dim(bset1
);
997 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
998 dim
= isl_space_set_alloc(bset1
->ctx
, 0, n
);
999 lp
= isl_basic_set_alloc_space(dim
, 0, d
, n
);
1002 for (i
= 0; i
< n
; ++i
) {
1003 k
= isl_basic_set_alloc_inequality(lp
);
1006 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1007 isl_int_set_si(lp
->ineq
[k
][0], -1);
1008 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1010 for (i
= 0; i
< d
; ++i
) {
1011 k
= isl_basic_set_alloc_equality(lp
);
1015 isl_int_set_si(lp
->eq
[k
][n
], 0); n
++;
1016 /* positivity constraint 1 >= 0 */
1017 isl_int_set_si(lp
->eq
[k
][n
], i
== 0); n
++;
1018 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1019 isl_int_set(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1020 isl_int_neg(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1022 for (j
= 0; j
< bset1
->n_ineq
; ++j
) {
1023 isl_int_set(lp
->eq
[k
][n
], bset1
->ineq
[j
][i
]); n
++;
1025 /* positivity constraint 1 >= 0 */
1026 isl_int_set_si(lp
->eq
[k
][n
], -(i
== 0)); n
++;
1027 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1028 isl_int_neg(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1029 isl_int_set(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1031 for (j
= 0; j
< bset2
->n_ineq
; ++j
) {
1032 isl_int_neg(lp
->eq
[k
][n
], bset2
->ineq
[j
][i
]); n
++;
1035 lp
= isl_basic_set_gauss(lp
, NULL
);
1036 isl_basic_set_free(bset1
);
1037 isl_basic_set_free(bset2
);
1040 isl_basic_set_free(bset1
);
1041 isl_basic_set_free(bset2
);
1045 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1046 * for all rays in the homogeneous space of the two cones that correspond
1047 * to the input polyhedra bset1 and bset2.
1049 * We compute s as a vector that satisfies
1051 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1053 * with h_{ij} the normals of the facets of polyhedron i
1054 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1055 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1056 * We first set up an LP with as variables the \alpha{ij}.
1057 * In this formulation, for each polyhedron i,
1058 * the first constraint is the positivity constraint, followed by pairs
1059 * of variables for the equalities, followed by variables for the inequalities.
1060 * We then simply pick a feasible solution and compute s using (*).
1062 * Note that we simply pick any valid direction and make no attempt
1063 * to pick a "good" or even the "best" valid direction.
1065 static __isl_give isl_vec
*valid_direction(
1066 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1068 struct isl_basic_set
*lp
;
1069 struct isl_tab
*tab
;
1070 struct isl_vec
*sample
= NULL
;
1071 struct isl_vec
*dir
;
1076 if (!bset1
|| !bset2
)
1078 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1079 isl_basic_set_copy(bset2
));
1080 tab
= isl_tab_from_basic_set(lp
, 0);
1081 sample
= isl_tab_get_sample_value(tab
);
1083 isl_basic_set_free(lp
);
1086 d
= isl_basic_set_total_dim(bset1
);
1087 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1090 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1092 /* positivity constraint 1 >= 0 */
1093 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
]); n
++;
1094 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1095 isl_int_sub(sample
->block
.data
[n
],
1096 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1097 isl_seq_combine(dir
->block
.data
,
1098 bset1
->ctx
->one
, dir
->block
.data
,
1099 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1103 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1104 isl_seq_combine(dir
->block
.data
,
1105 bset1
->ctx
->one
, dir
->block
.data
,
1106 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1107 isl_vec_free(sample
);
1108 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1109 isl_basic_set_free(bset1
);
1110 isl_basic_set_free(bset2
);
1113 isl_vec_free(sample
);
1114 isl_basic_set_free(bset1
);
1115 isl_basic_set_free(bset2
);
1119 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1120 * compute b_i' + A_i' x' >= 0, with
1122 * [ b_i A_i ] [ y' ] [ y' ]
1123 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1125 * In particular, add the "positivity constraint" and then perform
1128 static __isl_give isl_basic_set
*homogeneous_map(__isl_take isl_basic_set
*bset
,
1129 __isl_take isl_mat
*T
)
1135 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1136 k
= isl_basic_set_alloc_inequality(bset
);
1139 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1140 isl_int_set_si(bset
->ineq
[k
][0], 1);
1141 bset
= isl_basic_set_preimage(bset
, T
);
1145 isl_basic_set_free(bset
);
1149 /* Compute the convex hull of a pair of basic sets without any parameters or
1150 * integer divisions, where the convex hull is known to be pointed,
1151 * but the basic sets may be unbounded.
1153 * We turn this problem into the computation of a convex hull of a pair
1154 * _bounded_ polyhedra by "changing the direction of the homogeneous
1155 * dimension". This idea is due to Matthias Koeppe.
1157 * Consider the cones in homogeneous space that correspond to the
1158 * input polyhedra. The rays of these cones are also rays of the
1159 * polyhedra if the coordinate that corresponds to the homogeneous
1160 * dimension is zero. That is, if the inner product of the rays
1161 * with the homogeneous direction is zero.
1162 * The cones in the homogeneous space can also be considered to
1163 * correspond to other pairs of polyhedra by chosing a different
1164 * homogeneous direction. To ensure that both of these polyhedra
1165 * are bounded, we need to make sure that all rays of the cones
1166 * correspond to vertices and not to rays.
1167 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1168 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1169 * The vector s is computed in valid_direction.
1171 * Note that we need to consider _all_ rays of the cones and not just
1172 * the rays that correspond to rays in the polyhedra. If we were to
1173 * only consider those rays and turn them into vertices, then we
1174 * may inadvertently turn some vertices into rays.
1176 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1177 * We therefore transform the two polyhedra such that the selected
1178 * direction is mapped onto this standard direction and then proceed
1179 * with the normal computation.
1180 * Let S be a non-singular square matrix with s as its first row,
1181 * then we want to map the polyhedra to the space
1183 * [ y' ] [ y ] [ y ] [ y' ]
1184 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1186 * We take S to be the unimodular completion of s to limit the growth
1187 * of the coefficients in the following computations.
1189 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1190 * We first move to the homogeneous dimension
1192 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1193 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1195 * Then we change directoin
1197 * [ b_i A_i ] [ y' ] [ y' ]
1198 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1200 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1201 * resulting in b' + A' x' >= 0, which we then convert back
1204 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1206 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1208 static __isl_give isl_basic_set
*convex_hull_pair_pointed(
1209 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1211 struct isl_ctx
*ctx
= NULL
;
1212 struct isl_vec
*dir
= NULL
;
1213 struct isl_mat
*T
= NULL
;
1214 struct isl_mat
*T2
= NULL
;
1215 struct isl_basic_set
*hull
;
1216 struct isl_set
*set
;
1218 if (!bset1
|| !bset2
)
1220 ctx
= isl_basic_set_get_ctx(bset1
);
1221 dir
= valid_direction(isl_basic_set_copy(bset1
),
1222 isl_basic_set_copy(bset2
));
1225 T
= isl_mat_alloc(ctx
, dir
->size
, dir
->size
);
1228 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1229 T
= isl_mat_unimodular_complete(T
, 1);
1230 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1232 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1233 bset2
= homogeneous_map(bset2
, T2
);
1234 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1235 set
= isl_set_add_basic_set(set
, bset1
);
1236 set
= isl_set_add_basic_set(set
, bset2
);
1237 hull
= uset_convex_hull(set
);
1238 hull
= isl_basic_set_preimage(hull
, T
);
1245 isl_basic_set_free(bset1
);
1246 isl_basic_set_free(bset2
);
1250 static __isl_give isl_basic_set
*uset_convex_hull_wrap(__isl_take isl_set
*set
);
1251 static __isl_give isl_basic_set
*modulo_affine_hull(
1252 __isl_take isl_set
*set
, __isl_take isl_basic_set
*affine_hull
);
1254 /* Compute the convex hull of a pair of basic sets without any parameters or
1255 * integer divisions.
1257 * This function is called from uset_convex_hull_unbounded, which
1258 * means that the complete convex hull is unbounded. Some pairs
1259 * of basic sets may still be bounded, though.
1260 * They may even lie inside a lower dimensional space, in which
1261 * case they need to be handled inside their affine hull since
1262 * the main algorithm assumes that the result is full-dimensional.
1264 * If the convex hull of the two basic sets would have a non-trivial
1265 * lineality space, we first project out this lineality space.
1267 static __isl_give isl_basic_set
*convex_hull_pair(
1268 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1270 isl_basic_set
*lin
, *aff
;
1271 isl_bool bounded1
, bounded2
;
1273 if (bset1
->ctx
->opt
->convex
== ISL_CONVEX_HULL_FM
)
1274 return convex_hull_pair_elim(bset1
, bset2
);
1276 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1277 isl_basic_set_copy(bset2
)));
1281 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1282 isl_basic_set_free(aff
);
1284 bounded1
= isl_basic_set_is_bounded(bset1
);
1285 bounded2
= isl_basic_set_is_bounded(bset2
);
1287 if (bounded1
< 0 || bounded2
< 0)
1290 if (bounded1
&& bounded2
)
1291 return uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1293 if (bounded1
|| bounded2
)
1294 return convex_hull_pair_pointed(bset1
, bset2
);
1296 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1297 isl_basic_set_copy(bset2
));
1300 if (isl_basic_set_plain_is_universe(lin
)) {
1301 isl_basic_set_free(bset1
);
1302 isl_basic_set_free(bset2
);
1305 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1306 struct isl_set
*set
;
1307 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1308 set
= isl_set_add_basic_set(set
, bset1
);
1309 set
= isl_set_add_basic_set(set
, bset2
);
1310 return modulo_lineality(set
, lin
);
1312 isl_basic_set_free(lin
);
1314 return convex_hull_pair_pointed(bset1
, bset2
);
1316 isl_basic_set_free(bset1
);
1317 isl_basic_set_free(bset2
);
1321 /* Compute the lineality space of a basic set.
1322 * We basically just drop the constants and turn every inequality
1324 * Any explicit representations of local variables are removed
1325 * because they may no longer be valid representations
1326 * in the lineality space.
1328 __isl_give isl_basic_set
*isl_basic_set_lineality_space(
1329 __isl_take isl_basic_set
*bset
)
1332 struct isl_basic_set
*lin
= NULL
;
1333 unsigned n_div
, dim
;
1337 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1338 dim
= isl_basic_set_total_dim(bset
);
1340 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset
),
1342 for (i
= 0; i
< n_div
; ++i
)
1343 if (isl_basic_set_alloc_div(lin
) < 0)
1347 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1348 k
= isl_basic_set_alloc_equality(lin
);
1351 isl_int_set_si(lin
->eq
[k
][0], 0);
1352 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1354 lin
= isl_basic_set_gauss(lin
, NULL
);
1357 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1358 k
= isl_basic_set_alloc_equality(lin
);
1361 isl_int_set_si(lin
->eq
[k
][0], 0);
1362 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1363 lin
= isl_basic_set_gauss(lin
, NULL
);
1367 isl_basic_set_free(bset
);
1370 isl_basic_set_free(lin
);
1371 isl_basic_set_free(bset
);
1375 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1378 __isl_give isl_basic_set
*isl_set_combined_lineality_space(
1379 __isl_take isl_set
*set
)
1382 struct isl_set
*lin
= NULL
;
1387 isl_space
*space
= isl_set_get_space(set
);
1389 return isl_basic_set_empty(space
);
1392 lin
= isl_set_alloc_space(isl_set_get_space(set
), set
->n
, 0);
1393 for (i
= 0; i
< set
->n
; ++i
)
1394 lin
= isl_set_add_basic_set(lin
,
1395 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1397 return isl_set_affine_hull(lin
);
1400 /* Compute the convex hull of a set without any parameters or
1401 * integer divisions.
1402 * In each step, we combined two basic sets until only one
1403 * basic set is left.
1404 * The input basic sets are assumed not to have a non-trivial
1405 * lineality space. If any of the intermediate results has
1406 * a non-trivial lineality space, it is projected out.
1408 static __isl_give isl_basic_set
*uset_convex_hull_unbounded(
1409 __isl_take isl_set
*set
)
1411 isl_basic_set_list
*list
;
1413 list
= isl_set_get_basic_set_list(set
);
1418 struct isl_basic_set
*t
;
1419 isl_basic_set
*bset1
, *bset2
;
1421 n
= isl_basic_set_list_n_basic_set(list
);
1423 isl_die(isl_basic_set_list_get_ctx(list
),
1425 "expecting at least two elements", goto error
);
1426 bset1
= isl_basic_set_list_get_basic_set(list
, n
- 1);
1427 bset2
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1428 bset1
= convex_hull_pair(bset1
, bset2
);
1430 isl_basic_set_list_free(list
);
1433 bset1
= isl_basic_set_underlying_set(bset1
);
1434 list
= isl_basic_set_list_drop(list
, n
- 2, 2);
1435 list
= isl_basic_set_list_add(list
, bset1
);
1437 t
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1438 t
= isl_basic_set_lineality_space(t
);
1441 if (isl_basic_set_plain_is_universe(t
)) {
1442 isl_basic_set_list_free(list
);
1445 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1446 set
= isl_basic_set_list_union(list
);
1447 return modulo_lineality(set
, t
);
1449 isl_basic_set_free(t
);
1454 isl_basic_set_list_free(list
);
1458 /* Compute an initial hull for wrapping containing a single initial
1460 * This function assumes that the given set is bounded.
1462 static __isl_give isl_basic_set
*initial_hull(__isl_take isl_basic_set
*hull
,
1463 __isl_keep isl_set
*set
)
1465 struct isl_mat
*bounds
= NULL
;
1471 bounds
= initial_facet_constraint(set
);
1474 k
= isl_basic_set_alloc_inequality(hull
);
1477 dim
= isl_set_n_dim(set
);
1478 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1479 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1480 isl_mat_free(bounds
);
1484 isl_basic_set_free(hull
);
1485 isl_mat_free(bounds
);
1489 struct max_constraint
{
1495 static int max_constraint_equal(const void *entry
, const void *val
)
1497 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1498 isl_int
*b
= (isl_int
*)val
;
1500 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1503 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1504 isl_int
*con
, unsigned len
, int n
, int ineq
)
1506 struct isl_hash_table_entry
*entry
;
1507 struct max_constraint
*c
;
1510 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1511 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1517 isl_hash_table_remove(ctx
, table
, entry
);
1521 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1523 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1528 c
->c
= isl_mat_cow(c
->c
);
1529 isl_int_set(c
->c
->row
[0][0], con
[0]);
1533 /* Check whether the constraint hash table "table" contains the constraint
1536 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1537 isl_int
*con
, unsigned len
, int n
)
1539 struct isl_hash_table_entry
*entry
;
1540 struct max_constraint
*c
;
1543 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1544 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1551 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1554 /* Check for inequality constraints of a basic set without equalities
1555 * such that the same or more stringent copies of the constraint appear
1556 * in all of the basic sets. Such constraints are necessarily facet
1557 * constraints of the convex hull.
1559 * If the resulting basic set is by chance identical to one of
1560 * the basic sets in "set", then we know that this basic set contains
1561 * all other basic sets and is therefore the convex hull of set.
1562 * In this case we set *is_hull to 1.
1564 static __isl_give isl_basic_set
*common_constraints(
1565 __isl_take isl_basic_set
*hull
, __isl_keep isl_set
*set
, int *is_hull
)
1568 int min_constraints
;
1570 struct max_constraint
*constraints
= NULL
;
1571 struct isl_hash_table
*table
= NULL
;
1576 for (i
= 0; i
< set
->n
; ++i
)
1577 if (set
->p
[i
]->n_eq
== 0)
1581 min_constraints
= set
->p
[i
]->n_ineq
;
1583 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1584 if (set
->p
[i
]->n_eq
!= 0)
1586 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1588 min_constraints
= set
->p
[i
]->n_ineq
;
1591 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1595 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1596 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1599 total
= isl_set_dim(set
, isl_dim_all
);
1600 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1601 constraints
[i
].c
= isl_mat_sub_alloc6(hull
->ctx
,
1602 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1603 if (!constraints
[i
].c
)
1605 constraints
[i
].ineq
= 1;
1607 for (i
= 0; i
< min_constraints
; ++i
) {
1608 struct isl_hash_table_entry
*entry
;
1610 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1611 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1612 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1615 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1616 entry
->data
= &constraints
[i
];
1620 for (s
= 0; s
< set
->n
; ++s
) {
1624 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1625 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1626 for (j
= 0; j
< 2; ++j
) {
1627 isl_seq_neg(eq
, eq
, 1 + total
);
1628 update_constraint(hull
->ctx
, table
,
1632 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1633 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1634 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1635 set
->p
[s
]->n_eq
== 0);
1640 for (i
= 0; i
< min_constraints
; ++i
) {
1641 if (constraints
[i
].count
< n
)
1643 if (!constraints
[i
].ineq
)
1645 j
= isl_basic_set_alloc_inequality(hull
);
1648 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1651 for (s
= 0; s
< set
->n
; ++s
) {
1652 if (set
->p
[s
]->n_eq
)
1654 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1656 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1657 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1658 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1661 if (i
== set
->p
[s
]->n_ineq
)
1665 isl_hash_table_clear(table
);
1666 for (i
= 0; i
< min_constraints
; ++i
)
1667 isl_mat_free(constraints
[i
].c
);
1672 isl_hash_table_clear(table
);
1675 for (i
= 0; i
< min_constraints
; ++i
)
1676 isl_mat_free(constraints
[i
].c
);
1681 /* Create a template for the convex hull of "set" and fill it up
1682 * obvious facet constraints, if any. If the result happens to
1683 * be the convex hull of "set" then *is_hull is set to 1.
1685 static __isl_give isl_basic_set
*proto_hull(__isl_keep isl_set
*set
,
1688 struct isl_basic_set
*hull
;
1693 for (i
= 0; i
< set
->n
; ++i
) {
1694 n_ineq
+= set
->p
[i
]->n_eq
;
1695 n_ineq
+= set
->p
[i
]->n_ineq
;
1697 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
1698 hull
= isl_basic_set_set_rational(hull
);
1701 return common_constraints(hull
, set
, is_hull
);
1704 static __isl_give isl_basic_set
*uset_convex_hull_wrap(__isl_take isl_set
*set
)
1706 struct isl_basic_set
*hull
;
1709 hull
= proto_hull(set
, &is_hull
);
1710 if (hull
&& !is_hull
) {
1711 if (hull
->n_ineq
== 0)
1712 hull
= initial_hull(hull
, set
);
1713 hull
= extend(hull
, set
);
1720 /* Compute the convex hull of a set without any parameters or
1721 * integer divisions. Depending on whether the set is bounded,
1722 * we pass control to the wrapping based convex hull or
1723 * the Fourier-Motzkin elimination based convex hull.
1724 * We also handle a few special cases before checking the boundedness.
1726 static __isl_give isl_basic_set
*uset_convex_hull(__isl_take isl_set
*set
)
1729 struct isl_basic_set
*convex_hull
= NULL
;
1730 struct isl_basic_set
*lin
;
1732 if (isl_set_n_dim(set
) == 0)
1733 return convex_hull_0d(set
);
1735 set
= isl_set_coalesce(set
);
1736 set
= isl_set_set_rational(set
);
1741 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1745 if (isl_set_n_dim(set
) == 1)
1746 return convex_hull_1d(set
);
1748 bounded
= isl_set_is_bounded(set
);
1751 if (bounded
&& set
->ctx
->opt
->convex
== ISL_CONVEX_HULL_WRAP
)
1752 return uset_convex_hull_wrap(set
);
1754 lin
= isl_set_combined_lineality_space(isl_set_copy(set
));
1757 if (isl_basic_set_plain_is_universe(lin
)) {
1761 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1762 return modulo_lineality(set
, lin
);
1763 isl_basic_set_free(lin
);
1765 return uset_convex_hull_unbounded(set
);
1768 isl_basic_set_free(convex_hull
);
1772 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1773 * without parameters or divs and where the convex hull of set is
1774 * known to be full-dimensional.
1776 static __isl_give isl_basic_set
*uset_convex_hull_wrap_bounded(
1777 __isl_take isl_set
*set
)
1779 struct isl_basic_set
*convex_hull
= NULL
;
1784 if (isl_set_n_dim(set
) == 0) {
1785 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
1787 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1791 set
= isl_set_set_rational(set
);
1792 set
= isl_set_coalesce(set
);
1796 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1798 convex_hull
= isl_basic_map_remove_redundancies(convex_hull
);
1801 if (isl_set_n_dim(set
) == 1)
1802 return convex_hull_1d(set
);
1804 return uset_convex_hull_wrap(set
);
1810 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1811 * We first remove the equalities (transforming the set), compute the
1812 * convex hull of the transformed set and then add the equalities back
1813 * (after performing the inverse transformation.
1815 static __isl_give isl_basic_set
*modulo_affine_hull(
1816 __isl_take isl_set
*set
, __isl_take isl_basic_set
*affine_hull
)
1820 struct isl_basic_set
*dummy
;
1821 struct isl_basic_set
*convex_hull
;
1823 dummy
= isl_basic_set_remove_equalities(
1824 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1827 isl_basic_set_free(dummy
);
1828 set
= isl_set_preimage(set
, T
);
1829 convex_hull
= uset_convex_hull(set
);
1830 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1831 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1836 isl_basic_set_free(affine_hull
);
1841 /* Return an empty basic map living in the same space as "map".
1843 static __isl_give isl_basic_map
*replace_map_by_empty_basic_map(
1844 __isl_take isl_map
*map
)
1848 space
= isl_map_get_space(map
);
1850 return isl_basic_map_empty(space
);
1853 /* Compute the convex hull of a map.
1855 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1856 * specifically, the wrapping of facets to obtain new facets.
1858 __isl_give isl_basic_map
*isl_map_convex_hull(__isl_take isl_map
*map
)
1860 struct isl_basic_set
*bset
;
1861 struct isl_basic_map
*model
= NULL
;
1862 struct isl_basic_set
*affine_hull
= NULL
;
1863 struct isl_basic_map
*convex_hull
= NULL
;
1864 struct isl_set
*set
= NULL
;
1866 map
= isl_map_detect_equalities(map
);
1867 map
= isl_map_align_divs_internal(map
);
1872 return replace_map_by_empty_basic_map(map
);
1874 model
= isl_basic_map_copy(map
->p
[0]);
1875 set
= isl_map_underlying_set(map
);
1879 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1882 if (affine_hull
->n_eq
!= 0)
1883 bset
= modulo_affine_hull(set
, affine_hull
);
1885 isl_basic_set_free(affine_hull
);
1886 bset
= uset_convex_hull(set
);
1889 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1893 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1894 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1895 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1899 isl_basic_map_free(model
);
1903 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1905 return bset_from_bmap(isl_map_convex_hull(set_to_map(set
)));
1908 __isl_give isl_basic_map
*isl_map_polyhedral_hull(__isl_take isl_map
*map
)
1910 isl_basic_map
*hull
;
1912 hull
= isl_map_convex_hull(map
);
1913 return isl_basic_map_remove_divs(hull
);
1916 __isl_give isl_basic_set
*isl_set_polyhedral_hull(__isl_take isl_set
*set
)
1918 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set
)));
1921 struct sh_data_entry
{
1922 struct isl_hash_table
*table
;
1923 struct isl_tab
*tab
;
1926 /* Holds the data needed during the simple hull computation.
1928 * n the number of basic sets in the original set
1929 * hull_table a hash table of already computed constraints
1930 * in the simple hull
1931 * p for each basic set,
1932 * table a hash table of the constraints
1933 * tab the tableau corresponding to the basic set
1936 struct isl_ctx
*ctx
;
1938 struct isl_hash_table
*hull_table
;
1939 struct sh_data_entry p
[1];
1942 static void sh_data_free(struct sh_data
*data
)
1948 isl_hash_table_free(data
->ctx
, data
->hull_table
);
1949 for (i
= 0; i
< data
->n
; ++i
) {
1950 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
1951 isl_tab_free(data
->p
[i
].tab
);
1956 struct ineq_cmp_data
{
1961 static int has_ineq(const void *entry
, const void *val
)
1963 isl_int
*row
= (isl_int
*)entry
;
1964 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
1966 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
1967 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
1970 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1971 isl_int
*ineq
, unsigned len
)
1974 struct ineq_cmp_data v
;
1975 struct isl_hash_table_entry
*entry
;
1979 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
1980 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
1987 /* Fill hash table "table" with the constraints of "bset".
1988 * Equalities are added as two inequalities.
1989 * The value in the hash table is a pointer to the (in)equality of "bset".
1991 static isl_stat
hash_basic_set(struct isl_hash_table
*table
,
1992 __isl_keep isl_basic_set
*bset
)
1995 unsigned dim
= isl_basic_set_total_dim(bset
);
1997 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1998 for (j
= 0; j
< 2; ++j
) {
1999 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2000 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2001 return isl_stat_error
;
2004 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2005 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2006 return isl_stat_error
;
2011 static struct sh_data
*sh_data_alloc(__isl_keep isl_set
*set
, unsigned n_ineq
)
2013 struct sh_data
*data
;
2016 data
= isl_calloc(set
->ctx
, struct sh_data
,
2017 sizeof(struct sh_data
) +
2018 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2021 data
->ctx
= set
->ctx
;
2023 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2024 if (!data
->hull_table
)
2026 for (i
= 0; i
< set
->n
; ++i
) {
2027 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2028 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2029 if (!data
->p
[i
].table
)
2031 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2040 /* Check if inequality "ineq" is a bound for basic set "j" or if
2041 * it can be relaxed (by increasing the constant term) to become
2042 * a bound for that basic set. In the latter case, the constant
2044 * Relaxation of the constant term is only allowed if "shift" is set.
2046 * Return 1 if "ineq" is a bound
2047 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2048 * -1 if some error occurred
2050 static int is_bound(struct sh_data
*data
, __isl_keep isl_set
*set
, int j
,
2051 isl_int
*ineq
, int shift
)
2053 enum isl_lp_result res
;
2056 if (!data
->p
[j
].tab
) {
2057 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
], 0);
2058 if (!data
->p
[j
].tab
)
2064 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2066 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
)) {
2068 isl_int_sub(ineq
[0], ineq
[0], opt
);
2070 res
= isl_lp_unbounded
;
2075 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2076 res
== isl_lp_unbounded
? 0 : -1;
2079 /* Set the constant term of "ineq" to the maximum of those of the constraints
2080 * in the basic sets of "set" following "i" that are parallel to "ineq".
2081 * That is, if any of the basic sets of "set" following "i" have a more
2082 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2083 * "c_hash" is the hash value of the linear part of "ineq".
2084 * "v" has been set up for use by has_ineq.
2086 * Note that the two inequality constraints corresponding to an equality are
2087 * represented by the same inequality constraint in data->p[j].table
2088 * (but with different hash values). This means the constraint (or at
2089 * least its constant term) may need to be temporarily negated to get
2090 * the actually hashed constraint.
2092 static void set_max_constant_term(struct sh_data
*data
, __isl_keep isl_set
*set
,
2093 int i
, isl_int
*ineq
, uint32_t c_hash
, struct ineq_cmp_data
*v
)
2097 struct isl_hash_table_entry
*entry
;
2099 ctx
= isl_set_get_ctx(set
);
2100 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2104 entry
= isl_hash_table_find(ctx
, data
->p
[j
].table
,
2105 c_hash
, &has_ineq
, v
, 0);
2109 ineq_j
= entry
->data
;
2110 neg
= isl_seq_is_neg(ineq_j
+ 1, ineq
+ 1, v
->len
);
2112 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2113 if (isl_int_gt(ineq_j
[0], ineq
[0]))
2114 isl_int_set(ineq
[0], ineq_j
[0]);
2116 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2120 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2121 * become a bound on the whole set. If so, add the (relaxed) inequality
2122 * to "hull". Relaxation is only allowed if "shift" is set.
2124 * We first check if "hull" already contains a translate of the inequality.
2125 * If so, we are done.
2126 * Then, we check if any of the previous basic sets contains a translate
2127 * of the inequality. If so, then we have already considered this
2128 * inequality and we are done.
2129 * Otherwise, for each basic set other than "i", we check if the inequality
2130 * is a bound on the basic set, but first replace the constant term
2131 * by the maximal value of any translate of the inequality in any
2132 * of the following basic sets.
2133 * For previous basic sets, we know that they do not contain a translate
2134 * of the inequality, so we directly call is_bound.
2135 * For following basic sets, we first check if a translate of the
2136 * inequality appears in its description. If so, the constant term
2137 * of the inequality has already been updated with respect to this
2138 * translate and the inequality is therefore known to be a bound
2139 * of this basic set.
2141 static __isl_give isl_basic_set
*add_bound(__isl_take isl_basic_set
*hull
,
2142 struct sh_data
*data
, __isl_keep isl_set
*set
, int i
, isl_int
*ineq
,
2146 struct ineq_cmp_data v
;
2147 struct isl_hash_table_entry
*entry
;
2153 v
.len
= isl_basic_set_total_dim(hull
);
2155 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2157 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2162 for (j
= 0; j
< i
; ++j
) {
2163 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2164 c_hash
, has_ineq
, &v
, 0);
2171 k
= isl_basic_set_alloc_inequality(hull
);
2174 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2176 set_max_constant_term(data
, set
, i
, hull
->ineq
[k
], c_hash
, &v
);
2177 for (j
= 0; j
< i
; ++j
) {
2179 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2186 isl_basic_set_free_inequality(hull
, 1);
2190 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2192 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2193 c_hash
, has_ineq
, &v
, 0);
2196 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2203 isl_basic_set_free_inequality(hull
, 1);
2207 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2211 entry
->data
= hull
->ineq
[k
];
2215 isl_basic_set_free(hull
);
2219 /* Check if any inequality from basic set "i" is or can be relaxed to
2220 * become a bound on the whole set. If so, add the (relaxed) inequality
2221 * to "hull". Relaxation is only allowed if "shift" is set.
2223 static __isl_give isl_basic_set
*add_bounds(__isl_take isl_basic_set
*bset
,
2224 struct sh_data
*data
, __isl_keep isl_set
*set
, int i
, int shift
)
2227 unsigned dim
= isl_basic_set_total_dim(bset
);
2229 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2230 for (k
= 0; k
< 2; ++k
) {
2231 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2232 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
],
2236 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2237 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
], shift
);
2241 /* Compute a superset of the convex hull of set that is described
2242 * by only (translates of) the constraints in the constituents of set.
2243 * Translation is only allowed if "shift" is set.
2245 static __isl_give isl_basic_set
*uset_simple_hull(__isl_take isl_set
*set
,
2248 struct sh_data
*data
= NULL
;
2249 struct isl_basic_set
*hull
= NULL
;
2257 for (i
= 0; i
< set
->n
; ++i
) {
2260 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2263 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
2267 data
= sh_data_alloc(set
, n_ineq
);
2271 for (i
= 0; i
< set
->n
; ++i
)
2272 hull
= add_bounds(hull
, data
, set
, i
, shift
);
2280 isl_basic_set_free(hull
);
2285 /* Compute a superset of the convex hull of map that is described
2286 * by only (translates of) the constraints in the constituents of map.
2287 * Handle trivial cases where map is NULL or contains at most one disjunct.
2289 static __isl_give isl_basic_map
*map_simple_hull_trivial(
2290 __isl_take isl_map
*map
)
2292 isl_basic_map
*hull
;
2297 return replace_map_by_empty_basic_map(map
);
2299 hull
= isl_basic_map_copy(map
->p
[0]);
2304 /* Return a copy of the simple hull cached inside "map".
2305 * "shift" determines whether to return the cached unshifted or shifted
2308 static __isl_give isl_basic_map
*cached_simple_hull(__isl_take isl_map
*map
,
2311 isl_basic_map
*hull
;
2313 hull
= isl_basic_map_copy(map
->cached_simple_hull
[shift
]);
2319 /* Compute a superset of the convex hull of map that is described
2320 * by only (translates of) the constraints in the constituents of map.
2321 * Translation is only allowed if "shift" is set.
2323 * The constraints are sorted while removing redundant constraints
2324 * in order to indicate a preference of which constraints should
2325 * be preserved. In particular, pairs of constraints that are
2326 * sorted together are preferred to either both be preserved
2327 * or both be removed. The sorting is performed inside
2328 * isl_basic_map_remove_redundancies.
2330 * The result of the computation is stored in map->cached_simple_hull[shift]
2331 * such that it can be reused in subsequent calls. The cache is cleared
2332 * whenever the map is modified (in isl_map_cow).
2333 * Note that the results need to be stored in the input map for there
2334 * to be any chance that they may get reused. In particular, they
2335 * are stored in a copy of the input map that is saved before
2336 * the integer division alignment.
2338 static __isl_give isl_basic_map
*map_simple_hull(__isl_take isl_map
*map
,
2341 struct isl_set
*set
= NULL
;
2342 struct isl_basic_map
*model
= NULL
;
2343 struct isl_basic_map
*hull
;
2344 struct isl_basic_map
*affine_hull
;
2345 struct isl_basic_set
*bset
= NULL
;
2348 if (!map
|| map
->n
<= 1)
2349 return map_simple_hull_trivial(map
);
2351 if (map
->cached_simple_hull
[shift
])
2352 return cached_simple_hull(map
, shift
);
2354 map
= isl_map_detect_equalities(map
);
2355 if (!map
|| map
->n
<= 1)
2356 return map_simple_hull_trivial(map
);
2357 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2358 input
= isl_map_copy(map
);
2359 map
= isl_map_align_divs_internal(map
);
2360 model
= map
? isl_basic_map_copy(map
->p
[0]) : NULL
;
2362 set
= isl_map_underlying_set(map
);
2364 bset
= uset_simple_hull(set
, shift
);
2366 hull
= isl_basic_map_overlying_set(bset
, model
);
2368 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2369 hull
= isl_basic_map_remove_redundancies(hull
);
2372 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2373 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2376 hull
= isl_basic_map_finalize(hull
);
2378 input
->cached_simple_hull
[shift
] = isl_basic_map_copy(hull
);
2379 isl_map_free(input
);
2384 /* Compute a superset of the convex hull of map that is described
2385 * by only translates of the constraints in the constituents of map.
2387 __isl_give isl_basic_map
*isl_map_simple_hull(__isl_take isl_map
*map
)
2389 return map_simple_hull(map
, 1);
2392 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2394 return bset_from_bmap(isl_map_simple_hull(set_to_map(set
)));
2397 /* Compute a superset of the convex hull of map that is described
2398 * by only the constraints in the constituents of map.
2400 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull(
2401 __isl_take isl_map
*map
)
2403 return map_simple_hull(map
, 0);
2406 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull(
2407 __isl_take isl_set
*set
)
2409 return isl_map_unshifted_simple_hull(set
);
2412 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2413 * A constraint that appears with different constant terms
2414 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2415 * (i.e., greatest) constant term.
2416 * "bmap1" and "bmap2" are assumed to have the same (known)
2417 * integer divisions.
2418 * The constraints of both "bmap1" and "bmap2" are assumed
2419 * to have been sorted using isl_basic_map_sort_constraints.
2421 * Run through the inequality constraints of "bmap1" and "bmap2"
2423 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2425 * If a match is found, the constraint is kept. If needed, the constant
2426 * term of the constraint is adjusted.
2428 static __isl_give isl_basic_map
*select_shared_inequalities(
2429 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2433 bmap1
= isl_basic_map_cow(bmap1
);
2434 if (!bmap1
|| !bmap2
)
2435 return isl_basic_map_free(bmap1
);
2437 i1
= bmap1
->n_ineq
- 1;
2438 i2
= bmap2
->n_ineq
- 1;
2439 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2442 cmp
= isl_basic_map_constraint_cmp(bmap1
, bmap1
->ineq
[i1
],
2449 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2450 bmap1
= isl_basic_map_free(bmap1
);
2454 if (isl_int_lt(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]))
2455 isl_int_set(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]);
2459 for (; i1
>= 0; --i1
)
2460 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2461 bmap1
= isl_basic_map_free(bmap1
);
2466 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2467 * "bmap1" and "bmap2" are assumed to have the same (known)
2468 * integer divisions.
2470 * Run through the equality constraints of "bmap1" and "bmap2".
2471 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2474 static __isl_give isl_basic_map
*select_shared_equalities(
2475 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2480 bmap1
= isl_basic_map_cow(bmap1
);
2481 if (!bmap1
|| !bmap2
)
2482 return isl_basic_map_free(bmap1
);
2484 total
= isl_basic_map_total_dim(bmap1
);
2486 i1
= bmap1
->n_eq
- 1;
2487 i2
= bmap2
->n_eq
- 1;
2488 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2491 last1
= isl_seq_last_non_zero(bmap1
->eq
[i1
] + 1, total
);
2492 last2
= isl_seq_last_non_zero(bmap2
->eq
[i2
] + 1, total
);
2493 if (last1
> last2
) {
2497 if (last1
< last2
) {
2498 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2499 bmap1
= isl_basic_map_free(bmap1
);
2503 if (!isl_seq_eq(bmap1
->eq
[i1
], bmap2
->eq
[i2
], 1 + total
)) {
2504 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2505 bmap1
= isl_basic_map_free(bmap1
);
2510 for (; i1
>= 0; --i1
)
2511 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2512 bmap1
= isl_basic_map_free(bmap1
);
2517 /* Compute a superset of "bmap1" and "bmap2" that is described
2518 * by only the constraints that appear in both "bmap1" and "bmap2".
2520 * First drop constraints that involve unknown integer divisions
2521 * since it is not trivial to check whether two such integer divisions
2522 * in different basic maps are the same.
2523 * Then align the remaining (known) divs and sort the constraints.
2524 * Finally drop all inequalities and equalities from "bmap1" that
2525 * do not also appear in "bmap2".
2527 __isl_give isl_basic_map
*isl_basic_map_plain_unshifted_simple_hull(
2528 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
2530 bmap1
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap1
);
2531 bmap2
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap2
);
2532 bmap2
= isl_basic_map_align_divs(bmap2
, bmap1
);
2533 bmap1
= isl_basic_map_align_divs(bmap1
, bmap2
);
2534 bmap1
= isl_basic_map_gauss(bmap1
, NULL
);
2535 bmap2
= isl_basic_map_gauss(bmap2
, NULL
);
2536 bmap1
= isl_basic_map_sort_constraints(bmap1
);
2537 bmap2
= isl_basic_map_sort_constraints(bmap2
);
2539 bmap1
= select_shared_inequalities(bmap1
, bmap2
);
2540 bmap1
= select_shared_equalities(bmap1
, bmap2
);
2542 isl_basic_map_free(bmap2
);
2543 bmap1
= isl_basic_map_finalize(bmap1
);
2547 /* Compute a superset of the convex hull of "map" that is described
2548 * by only the constraints in the constituents of "map".
2549 * In particular, the result is composed of constraints that appear
2550 * in each of the basic maps of "map"
2552 * Constraints that involve unknown integer divisions are dropped
2553 * since it is not trivial to check whether two such integer divisions
2554 * in different basic maps are the same.
2556 * The hull is initialized from the first basic map and then
2557 * updated with respect to the other basic maps in turn.
2559 __isl_give isl_basic_map
*isl_map_plain_unshifted_simple_hull(
2560 __isl_take isl_map
*map
)
2563 isl_basic_map
*hull
;
2568 return map_simple_hull_trivial(map
);
2569 map
= isl_map_drop_constraint_involving_unknown_divs(map
);
2570 hull
= isl_basic_map_copy(map
->p
[0]);
2571 for (i
= 1; i
< map
->n
; ++i
) {
2572 isl_basic_map
*bmap_i
;
2574 bmap_i
= isl_basic_map_copy(map
->p
[i
]);
2575 hull
= isl_basic_map_plain_unshifted_simple_hull(hull
, bmap_i
);
2582 /* Compute a superset of the convex hull of "set" that is described
2583 * by only the constraints in the constituents of "set".
2584 * In particular, the result is composed of constraints that appear
2585 * in each of the basic sets of "set"
2587 __isl_give isl_basic_set
*isl_set_plain_unshifted_simple_hull(
2588 __isl_take isl_set
*set
)
2590 return isl_map_plain_unshifted_simple_hull(set
);
2593 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2595 * For each basic set in "set", we first check if the basic set
2596 * contains a translate of "ineq". If this translate is more relaxed,
2597 * then we assume that "ineq" is not a bound on this basic set.
2598 * Otherwise, we know that it is a bound.
2599 * If the basic set does not contain a translate of "ineq", then
2600 * we call is_bound to perform the test.
2602 static __isl_give isl_basic_set
*add_bound_from_constraint(
2603 __isl_take isl_basic_set
*hull
, struct sh_data
*data
,
2604 __isl_keep isl_set
*set
, isl_int
*ineq
)
2609 struct ineq_cmp_data v
;
2612 return isl_basic_set_free(hull
);
2614 v
.len
= isl_basic_set_total_dim(hull
);
2616 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2618 ctx
= isl_basic_set_get_ctx(hull
);
2619 for (i
= 0; i
< set
->n
; ++i
) {
2621 struct isl_hash_table_entry
*entry
;
2623 entry
= isl_hash_table_find(ctx
, data
->p
[i
].table
,
2624 c_hash
, &has_ineq
, &v
, 0);
2626 isl_int
*ineq_i
= entry
->data
;
2627 int neg
, more_relaxed
;
2629 neg
= isl_seq_is_neg(ineq_i
+ 1, ineq
+ 1, v
.len
);
2631 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2632 more_relaxed
= isl_int_gt(ineq_i
[0], ineq
[0]);
2634 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2640 bound
= is_bound(data
, set
, i
, ineq
, 0);
2642 return isl_basic_set_free(hull
);
2649 k
= isl_basic_set_alloc_inequality(hull
);
2651 return isl_basic_set_free(hull
);
2652 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2657 /* Compute a superset of the convex hull of "set" that is described
2658 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2659 * has no parameters or integer divisions.
2661 * The inequalities in "ineq" are assumed to have been sorted such
2662 * that constraints with the same linear part appear together and
2663 * that among constraints with the same linear part, those with
2664 * smaller constant term appear first.
2666 * We reuse the same data structure that is used by uset_simple_hull,
2667 * but we do not need the hull table since we will not consider the
2668 * same constraint more than once. We therefore allocate it with zero size.
2670 * We run through the constraints and try to add them one by one,
2671 * skipping identical constraints. If we have added a constraint and
2672 * the next constraint is a more relaxed translate, then we skip this
2673 * next constraint as well.
2675 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_constraints(
2676 __isl_take isl_set
*set
, int n_ineq
, isl_int
**ineq
)
2680 struct sh_data
*data
= NULL
;
2681 isl_basic_set
*hull
= NULL
;
2684 hull
= isl_basic_set_alloc_space(isl_set_get_space(set
), 0, 0, n_ineq
);
2688 data
= sh_data_alloc(set
, 0);
2692 dim
= isl_set_dim(set
, isl_dim_set
);
2693 for (i
= 0; i
< n_ineq
; ++i
) {
2694 int hull_n_ineq
= hull
->n_ineq
;
2697 parallel
= i
> 0 && isl_seq_eq(ineq
[i
- 1] + 1, ineq
[i
] + 1,
2700 (last_added
|| isl_int_eq(ineq
[i
- 1][0], ineq
[i
][0])))
2702 hull
= add_bound_from_constraint(hull
, data
, set
, ineq
[i
]);
2705 last_added
= hull
->n_ineq
> hull_n_ineq
;
2714 isl_basic_set_free(hull
);
2718 /* Collect pointers to all the inequalities in the elements of "list"
2719 * in "ineq". For equalities, store both a pointer to the equality and
2720 * a pointer to its opposite, which is first copied to "mat".
2721 * "ineq" and "mat" are assumed to have been preallocated to the right size
2722 * (the number of inequalities + 2 times the number of equalites and
2723 * the number of equalities, respectively).
2725 static __isl_give isl_mat
*collect_inequalities(__isl_take isl_mat
*mat
,
2726 __isl_keep isl_basic_set_list
*list
, isl_int
**ineq
)
2728 int i
, j
, n
, n_eq
, n_ineq
;
2735 n
= isl_basic_set_list_n_basic_set(list
);
2736 for (i
= 0; i
< n
; ++i
) {
2737 isl_basic_set
*bset
;
2738 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2740 return isl_mat_free(mat
);
2741 for (j
= 0; j
< bset
->n_eq
; ++j
) {
2742 ineq
[n_ineq
++] = mat
->row
[n_eq
];
2743 ineq
[n_ineq
++] = bset
->eq
[j
];
2744 isl_seq_neg(mat
->row
[n_eq
++], bset
->eq
[j
], mat
->n_col
);
2746 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2747 ineq
[n_ineq
++] = bset
->ineq
[j
];
2748 isl_basic_set_free(bset
);
2754 /* Comparison routine for use as an isl_sort callback.
2756 * Constraints with the same linear part are sorted together and
2757 * among constraints with the same linear part, those with smaller
2758 * constant term are sorted first.
2760 static int cmp_ineq(const void *a
, const void *b
, void *arg
)
2762 unsigned dim
= *(unsigned *) arg
;
2763 isl_int
* const *ineq1
= a
;
2764 isl_int
* const *ineq2
= b
;
2767 cmp
= isl_seq_cmp((*ineq1
) + 1, (*ineq2
) + 1, dim
);
2770 return isl_int_cmp((*ineq1
)[0], (*ineq2
)[0]);
2773 /* Compute a superset of the convex hull of "set" that is described
2774 * by only constraints in the elements of "list", where "set" has
2775 * no parameters or integer divisions.
2777 * We collect all the constraints in those elements and then
2778 * sort the constraints such that constraints with the same linear part
2779 * are sorted together and that those with smaller constant term are
2782 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_basic_set_list(
2783 __isl_take isl_set
*set
, __isl_take isl_basic_set_list
*list
)
2785 int i
, n
, n_eq
, n_ineq
;
2788 isl_mat
*mat
= NULL
;
2789 isl_int
**ineq
= NULL
;
2790 isl_basic_set
*hull
;
2794 ctx
= isl_set_get_ctx(set
);
2798 n
= isl_basic_set_list_n_basic_set(list
);
2799 for (i
= 0; i
< n
; ++i
) {
2800 isl_basic_set
*bset
;
2801 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2805 n_ineq
+= 2 * bset
->n_eq
+ bset
->n_ineq
;
2806 isl_basic_set_free(bset
);
2809 ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
);
2810 if (n_ineq
> 0 && !ineq
)
2813 dim
= isl_set_dim(set
, isl_dim_set
);
2814 mat
= isl_mat_alloc(ctx
, n_eq
, 1 + dim
);
2815 mat
= collect_inequalities(mat
, list
, ineq
);
2819 if (isl_sort(ineq
, n_ineq
, sizeof(ineq
[0]), &cmp_ineq
, &dim
) < 0)
2822 hull
= uset_unshifted_simple_hull_from_constraints(set
, n_ineq
, ineq
);
2826 isl_basic_set_list_free(list
);
2832 isl_basic_set_list_free(list
);
2836 /* Compute a superset of the convex hull of "map" that is described
2837 * by only constraints in the elements of "list".
2839 * If the list is empty, then we can only describe the universe set.
2840 * If the input map is empty, then all constraints are valid, so
2841 * we return the intersection of the elements in "list".
2843 * Otherwise, we align all divs and temporarily treat them
2844 * as regular variables, computing the unshifted simple hull in
2845 * uset_unshifted_simple_hull_from_basic_set_list.
2847 static __isl_give isl_basic_map
*map_unshifted_simple_hull_from_basic_map_list(
2848 __isl_take isl_map
*map
, __isl_take isl_basic_map_list
*list
)
2850 isl_basic_map
*model
;
2851 isl_basic_map
*hull
;
2853 isl_basic_set_list
*bset_list
;
2858 if (isl_basic_map_list_n_basic_map(list
) == 0) {
2861 space
= isl_map_get_space(map
);
2863 isl_basic_map_list_free(list
);
2864 return isl_basic_map_universe(space
);
2866 if (isl_map_plain_is_empty(map
)) {
2868 return isl_basic_map_list_intersect(list
);
2871 map
= isl_map_align_divs_to_basic_map_list(map
, list
);
2874 list
= isl_basic_map_list_align_divs_to_basic_map(list
, map
->p
[0]);
2876 model
= isl_basic_map_list_get_basic_map(list
, 0);
2878 set
= isl_map_underlying_set(map
);
2879 bset_list
= isl_basic_map_list_underlying_set(list
);
2881 hull
= uset_unshifted_simple_hull_from_basic_set_list(set
, bset_list
);
2882 hull
= isl_basic_map_overlying_set(hull
, model
);
2887 isl_basic_map_list_free(list
);
2891 /* Return a sequence of the basic maps that make up the maps in "list".
2893 static __isl_give isl_basic_map_list
*collect_basic_maps(
2894 __isl_take isl_map_list
*list
)
2898 isl_basic_map_list
*bmap_list
;
2902 n
= isl_map_list_n_map(list
);
2903 ctx
= isl_map_list_get_ctx(list
);
2904 bmap_list
= isl_basic_map_list_alloc(ctx
, 0);
2906 for (i
= 0; i
< n
; ++i
) {
2908 isl_basic_map_list
*list_i
;
2910 map
= isl_map_list_get_map(list
, i
);
2911 map
= isl_map_compute_divs(map
);
2912 list_i
= isl_map_get_basic_map_list(map
);
2914 bmap_list
= isl_basic_map_list_concat(bmap_list
, list_i
);
2917 isl_map_list_free(list
);
2921 /* Compute a superset of the convex hull of "map" that is described
2922 * by only constraints in the elements of "list".
2924 * If "map" is the universe, then the convex hull (and therefore
2925 * any superset of the convexhull) is the universe as well.
2927 * Otherwise, we collect all the basic maps in the map list and
2928 * continue with map_unshifted_simple_hull_from_basic_map_list.
2930 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull_from_map_list(
2931 __isl_take isl_map
*map
, __isl_take isl_map_list
*list
)
2933 isl_basic_map_list
*bmap_list
;
2936 is_universe
= isl_map_plain_is_universe(map
);
2937 if (is_universe
< 0)
2938 map
= isl_map_free(map
);
2939 if (is_universe
< 0 || is_universe
) {
2940 isl_map_list_free(list
);
2941 return isl_map_unshifted_simple_hull(map
);
2944 bmap_list
= collect_basic_maps(list
);
2945 return map_unshifted_simple_hull_from_basic_map_list(map
, bmap_list
);
2948 /* Compute a superset of the convex hull of "set" that is described
2949 * by only constraints in the elements of "list".
2951 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull_from_set_list(
2952 __isl_take isl_set
*set
, __isl_take isl_set_list
*list
)
2954 return isl_map_unshifted_simple_hull_from_map_list(set
, list
);
2957 /* Given a set "set", return parametric bounds on the dimension "dim".
2959 static __isl_give isl_basic_set
*set_bounds(__isl_keep isl_set
*set
, int dim
)
2961 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
2962 set
= isl_set_copy(set
);
2963 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
2964 set
= isl_set_eliminate_dims(set
, 0, dim
);
2965 return isl_set_convex_hull(set
);
2968 /* Computes a "simple hull" and then check if each dimension in the
2969 * resulting hull is bounded by a symbolic constant. If not, the
2970 * hull is intersected with the corresponding bounds on the whole set.
2972 __isl_give isl_basic_set
*isl_set_bounded_simple_hull(__isl_take isl_set
*set
)
2975 struct isl_basic_set
*hull
;
2976 unsigned nparam
, left
;
2977 int removed_divs
= 0;
2979 hull
= isl_set_simple_hull(isl_set_copy(set
));
2983 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
2984 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
2985 int lower
= 0, upper
= 0;
2986 struct isl_basic_set
*bounds
;
2988 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
2989 for (j
= 0; j
< hull
->n_eq
; ++j
) {
2990 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
2992 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
2999 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
3000 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
3002 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
3004 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
3007 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
3018 if (!removed_divs
) {
3019 set
= isl_set_remove_divs(set
);
3024 bounds
= set_bounds(set
, i
);
3025 hull
= isl_basic_set_intersect(hull
, bounds
);
3034 isl_basic_set_free(hull
);