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 struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
);
33 * constraints. If the minimal value along the normal of a constraint
34 * is the same if the constraint is removed, then the constraint is redundant.
36 * Since some constraints may be mutually redundant, sort the constraints
37 * first such that constraints that involve existentially quantified
38 * variables are considered for removal before those that do not.
39 * The sorting is also needed for the use in map_simple_hull.
41 * Note that isl_tab_detect_implicit_equalities may also end up
42 * marking some constraints as redundant. Make sure the constraints
43 * are preserved and undo those marking such that isl_tab_detect_redundant
44 * can consider the constraints in the sorted order.
46 * Alternatively, we could have intersected the basic map with the
47 * corresponding equality and then checked if the dimension was that
50 __isl_give isl_basic_map
*isl_basic_map_remove_redundancies(
51 __isl_take isl_basic_map
*bmap
)
58 bmap
= isl_basic_map_gauss(bmap
, NULL
);
59 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
61 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
))
63 if (bmap
->n_ineq
<= 1)
66 bmap
= isl_basic_map_sort_constraints(bmap
);
67 tab
= isl_tab_from_basic_map(bmap
, 0);
71 if (isl_tab_detect_implicit_equalities(tab
) < 0)
73 if (isl_tab_restore_redundant(tab
) < 0)
76 if (isl_tab_detect_redundant(tab
) < 0)
78 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
82 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
83 ISL_F_SET(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
87 isl_basic_map_free(bmap
);
91 __isl_give isl_basic_set
*isl_basic_set_remove_redundancies(
92 __isl_take isl_basic_set
*bset
)
94 return bset_from_bmap(
95 isl_basic_map_remove_redundancies(bset_to_bmap(bset
)));
98 /* Remove redundant constraints in each of the basic maps.
100 __isl_give isl_map
*isl_map_remove_redundancies(__isl_take isl_map
*map
)
102 return isl_map_inline_foreach_basic_map(map
,
103 &isl_basic_map_remove_redundancies
);
106 __isl_give isl_set
*isl_set_remove_redundancies(__isl_take isl_set
*set
)
108 return isl_map_remove_redundancies(set
);
111 /* Check if the set set is bound in the direction of the affine
112 * constraint c and if so, set the constant term such that the
113 * resulting constraint is a bounding constraint for the set.
115 static int uset_is_bound(struct isl_set
*set
, isl_int
*c
, unsigned len
)
123 isl_int_init(opt_denom
);
125 for (j
= 0; j
< set
->n
; ++j
) {
126 enum isl_lp_result res
;
128 if (ISL_F_ISSET(set
->p
[j
], ISL_BASIC_SET_EMPTY
))
131 res
= isl_basic_set_solve_lp(set
->p
[j
],
132 0, c
, set
->ctx
->one
, &opt
, &opt_denom
, NULL
);
133 if (res
== isl_lp_unbounded
)
135 if (res
== isl_lp_error
)
137 if (res
== isl_lp_empty
) {
138 set
->p
[j
] = isl_basic_set_set_to_empty(set
->p
[j
]);
143 if (first
|| isl_int_is_neg(opt
)) {
144 if (!isl_int_is_one(opt_denom
))
145 isl_seq_scale(c
, c
, opt_denom
, len
);
146 isl_int_sub(c
[0], c
[0], opt
);
151 isl_int_clear(opt_denom
);
155 isl_int_clear(opt_denom
);
159 static struct isl_basic_set
*isl_basic_set_add_equality(
160 struct isl_basic_set
*bset
, isl_int
*c
)
168 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
171 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
172 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
173 dim
= isl_basic_set_n_dim(bset
);
174 bset
= isl_basic_set_cow(bset
);
175 bset
= isl_basic_set_extend(bset
, 0, dim
, 0, 1, 0);
176 i
= isl_basic_set_alloc_equality(bset
);
179 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
182 isl_basic_set_free(bset
);
186 static struct isl_set
*isl_set_add_basic_set_equality(struct isl_set
*set
, isl_int
*c
)
190 set
= isl_set_cow(set
);
193 for (i
= 0; i
< set
->n
; ++i
) {
194 set
->p
[i
] = isl_basic_set_add_equality(set
->p
[i
], c
);
204 /* Given a union of basic sets, construct the constraints for wrapping
205 * a facet around one of its ridges.
206 * In particular, if each of n the d-dimensional basic sets i in "set"
207 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
208 * and is defined by the constraints
212 * then the resulting set is of dimension n*(1+d) and has as constraints
221 static struct isl_basic_set
*wrap_constraints(struct isl_set
*set
)
223 struct isl_basic_set
*lp
;
227 unsigned dim
, lp_dim
;
232 dim
= 1 + isl_set_n_dim(set
);
235 for (i
= 0; i
< set
->n
; ++i
) {
236 n_eq
+= set
->p
[i
]->n_eq
;
237 n_ineq
+= set
->p
[i
]->n_ineq
;
239 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
240 lp
= isl_basic_set_set_rational(lp
);
243 lp_dim
= isl_basic_set_n_dim(lp
);
244 k
= isl_basic_set_alloc_equality(lp
);
245 isl_int_set_si(lp
->eq
[k
][0], -1);
246 for (i
= 0; i
< set
->n
; ++i
) {
247 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
248 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
249 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
251 for (i
= 0; i
< set
->n
; ++i
) {
252 k
= isl_basic_set_alloc_inequality(lp
);
253 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
254 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
256 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
257 k
= isl_basic_set_alloc_equality(lp
);
258 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
259 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
260 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
263 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
264 k
= isl_basic_set_alloc_inequality(lp
);
265 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
266 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
267 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
273 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
274 * of that facet, compute the other facet of the convex hull that contains
277 * We first transform the set such that the facet constraint becomes
281 * I.e., the facet lies in
285 * and on that facet, the constraint that defines the ridge is
289 * (This transformation is not strictly needed, all that is needed is
290 * that the ridge contains the origin.)
292 * Since the ridge contains the origin, the cone of the convex hull
293 * will be of the form
298 * with this second constraint defining the new facet.
299 * The constant a is obtained by settting x_1 in the cone of the
300 * convex hull to 1 and minimizing x_2.
301 * Now, each element in the cone of the convex hull is the sum
302 * of elements in the cones of the basic sets.
303 * If a_i is the dilation factor of basic set i, then the problem
304 * we need to solve is
317 * the constraints of each (transformed) basic set.
318 * If a = n/d, then the constraint defining the new facet (in the transformed
321 * -n x_1 + d x_2 >= 0
323 * In the original space, we need to take the same combination of the
324 * corresponding constraints "facet" and "ridge".
326 * If a = -infty = "-1/0", then we just return the original facet constraint.
327 * This means that the facet is unbounded, but has a bounded intersection
328 * with the union of sets.
330 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
331 isl_int
*facet
, isl_int
*ridge
)
335 struct isl_mat
*T
= NULL
;
336 struct isl_basic_set
*lp
= NULL
;
338 enum isl_lp_result res
;
345 set
= isl_set_copy(set
);
346 set
= isl_set_set_rational(set
);
348 dim
= 1 + isl_set_n_dim(set
);
349 T
= isl_mat_alloc(ctx
, 3, dim
);
352 isl_int_set_si(T
->row
[0][0], 1);
353 isl_seq_clr(T
->row
[0]+1, dim
- 1);
354 isl_seq_cpy(T
->row
[1], facet
, dim
);
355 isl_seq_cpy(T
->row
[2], ridge
, dim
);
356 T
= isl_mat_right_inverse(T
);
357 set
= isl_set_preimage(set
, T
);
361 lp
= wrap_constraints(set
);
362 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
365 isl_int_set_si(obj
->block
.data
[0], 0);
366 for (i
= 0; i
< set
->n
; ++i
) {
367 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
368 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
369 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
373 res
= isl_basic_set_solve_lp(lp
, 0,
374 obj
->block
.data
, ctx
->one
, &num
, &den
, NULL
);
375 if (res
== isl_lp_ok
) {
376 isl_int_neg(num
, num
);
377 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
378 isl_seq_normalize(ctx
, facet
, dim
);
383 isl_basic_set_free(lp
);
385 if (res
== isl_lp_error
)
387 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
391 isl_basic_set_free(lp
);
397 /* Compute the constraint of a facet of "set".
399 * We first compute the intersection with a bounding constraint
400 * that is orthogonal to one of the coordinate axes.
401 * If the affine hull of this intersection has only one equality,
402 * we have found a facet.
403 * Otherwise, we wrap the current bounding constraint around
404 * one of the equalities of the face (one that is not equal to
405 * the current bounding constraint).
406 * This process continues until we have found a facet.
407 * The dimension of the intersection increases by at least
408 * one on each iteration, so termination is guaranteed.
410 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
412 struct isl_set
*slice
= NULL
;
413 struct isl_basic_set
*face
= NULL
;
415 unsigned dim
= isl_set_n_dim(set
);
417 isl_mat
*bounds
= NULL
;
419 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
420 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
424 isl_seq_clr(bounds
->row
[0], dim
);
425 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
426 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
429 isl_assert(set
->ctx
, is_bound
, goto error
);
430 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
434 slice
= isl_set_copy(set
);
435 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
436 face
= isl_set_affine_hull(slice
);
439 if (face
->n_eq
== 1) {
440 isl_basic_set_free(face
);
443 for (i
= 0; i
< face
->n_eq
; ++i
)
444 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
445 !isl_seq_is_neg(bounds
->row
[0],
446 face
->eq
[i
], 1 + dim
))
448 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
449 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
451 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
452 isl_basic_set_free(face
);
457 isl_basic_set_free(face
);
458 isl_mat_free(bounds
);
462 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
463 * compute a hyperplane description of the facet, i.e., compute the facets
466 * We compute an affine transformation that transforms the constraint
475 * by computing the right inverse U of a matrix that starts with the rows
488 * Since z_1 is zero, we can drop this variable as well as the corresponding
489 * column of U to obtain
497 * with Q' equal to Q, but without the corresponding row.
498 * After computing the facets of the facet in the z' space,
499 * we convert them back to the x space through Q.
501 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
503 struct isl_mat
*m
, *U
, *Q
;
504 struct isl_basic_set
*facet
= NULL
;
509 set
= isl_set_copy(set
);
510 dim
= isl_set_n_dim(set
);
511 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
514 isl_int_set_si(m
->row
[0][0], 1);
515 isl_seq_clr(m
->row
[0]+1, dim
);
516 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
517 U
= isl_mat_right_inverse(m
);
518 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
519 U
= isl_mat_drop_cols(U
, 1, 1);
520 Q
= isl_mat_drop_rows(Q
, 1, 1);
521 set
= isl_set_preimage(set
, U
);
522 facet
= uset_convex_hull_wrap_bounded(set
);
523 facet
= isl_basic_set_preimage(facet
, Q
);
524 if (facet
&& facet
->n_eq
!= 0)
525 isl_die(ctx
, isl_error_internal
, "unexpected equality",
526 return isl_basic_set_free(facet
));
529 isl_basic_set_free(facet
);
534 /* Given an initial facet constraint, compute the remaining facets.
535 * We do this by running through all facets found so far and computing
536 * the adjacent facets through wrapping, adding those facets that we
537 * hadn't already found before.
539 * For each facet we have found so far, we first compute its facets
540 * in the resulting convex hull. That is, we compute the ridges
541 * of the resulting convex hull contained in the facet.
542 * We also compute the corresponding facet in the current approximation
543 * of the convex hull. There is no need to wrap around the ridges
544 * in this facet since that would result in a facet that is already
545 * present in the current approximation.
547 * This function can still be significantly optimized by checking which of
548 * the facets of the basic sets are also facets of the convex hull and
549 * using all the facets so far to help in constructing the facets of the
552 * using the technique in section "3.1 Ridge Generation" of
553 * "Extended Convex Hull" by Fukuda et al.
555 static __isl_give isl_basic_set
*extend(__isl_take isl_basic_set
*hull
,
556 __isl_keep isl_set
*set
)
560 struct isl_basic_set
*facet
= NULL
;
561 struct isl_basic_set
*hull_facet
= NULL
;
567 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
569 dim
= isl_set_n_dim(set
);
571 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
572 facet
= compute_facet(set
, hull
->ineq
[i
]);
573 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
574 facet
= isl_basic_set_gauss(facet
, NULL
);
575 facet
= isl_basic_set_normalize_constraints(facet
);
576 hull_facet
= isl_basic_set_copy(hull
);
577 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
578 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
579 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
580 if (!facet
|| !hull_facet
)
582 hull
= isl_basic_set_cow(hull
);
583 hull
= isl_basic_set_extend_space(hull
,
584 isl_space_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
587 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
588 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
589 if (isl_seq_eq(facet
->ineq
[j
],
590 hull_facet
->ineq
[f
], 1 + dim
))
592 if (f
< hull_facet
->n_ineq
)
594 k
= isl_basic_set_alloc_inequality(hull
);
597 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
598 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
601 isl_basic_set_free(hull_facet
);
602 isl_basic_set_free(facet
);
604 hull
= isl_basic_set_simplify(hull
);
605 hull
= isl_basic_set_finalize(hull
);
608 isl_basic_set_free(hull_facet
);
609 isl_basic_set_free(facet
);
610 isl_basic_set_free(hull
);
614 /* Special case for computing the convex hull of a one dimensional set.
615 * We simply collect the lower and upper bounds of each basic set
616 * and the biggest of those.
618 static __isl_give isl_basic_set
*convex_hull_1d(__isl_take isl_set
*set
)
620 struct isl_mat
*c
= NULL
;
621 isl_int
*lower
= NULL
;
622 isl_int
*upper
= NULL
;
625 struct isl_basic_set
*hull
;
627 for (i
= 0; i
< set
->n
; ++i
) {
628 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
632 set
= isl_set_remove_empty_parts(set
);
635 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
636 c
= isl_mat_alloc(set
->ctx
, 2, 2);
640 if (set
->p
[0]->n_eq
> 0) {
641 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
644 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
645 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
646 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
648 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
649 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
652 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
653 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
655 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
658 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
665 for (i
= 0; i
< set
->n
; ++i
) {
666 struct isl_basic_set
*bset
= set
->p
[i
];
670 for (j
= 0; j
< bset
->n_eq
; ++j
) {
674 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
675 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
676 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
677 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
678 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
679 isl_seq_neg(lower
, bset
->eq
[j
], 2);
682 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
683 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
684 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
685 isl_seq_neg(upper
, bset
->eq
[j
], 2);
686 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
687 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
690 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
691 if (isl_int_is_pos(bset
->ineq
[j
][1]))
693 if (isl_int_is_neg(bset
->ineq
[j
][1]))
695 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
696 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
697 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
698 if (isl_int_lt(a
, b
))
699 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
701 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
702 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
703 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
704 if (isl_int_gt(a
, b
))
705 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
716 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
717 hull
= isl_basic_set_set_rational(hull
);
721 k
= isl_basic_set_alloc_inequality(hull
);
722 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
725 k
= isl_basic_set_alloc_inequality(hull
);
726 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
728 hull
= isl_basic_set_finalize(hull
);
738 static __isl_give isl_basic_set
*convex_hull_0d(__isl_take isl_set
*set
)
740 struct isl_basic_set
*convex_hull
;
745 if (isl_set_is_empty(set
))
746 convex_hull
= isl_basic_set_empty(isl_space_copy(set
->dim
));
748 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
753 /* Compute the convex hull of a pair of basic sets without any parameters or
754 * integer divisions using Fourier-Motzkin elimination.
755 * The convex hull is the set of all points that can be written as
756 * the sum of points from both basic sets (in homogeneous coordinates).
757 * We set up the constraints in a space with dimensions for each of
758 * the three sets and then project out the dimensions corresponding
759 * to the two original basic sets, retaining only those corresponding
760 * to the convex hull.
762 static __isl_give isl_basic_set
*convex_hull_pair_elim(
763 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
766 struct isl_basic_set
*bset
[2];
767 struct isl_basic_set
*hull
= NULL
;
770 if (!bset1
|| !bset2
)
773 dim
= isl_basic_set_n_dim(bset1
);
774 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
775 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
776 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
779 for (i
= 0; i
< 2; ++i
) {
780 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
781 k
= isl_basic_set_alloc_equality(hull
);
784 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
785 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
786 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
789 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
790 k
= isl_basic_set_alloc_inequality(hull
);
793 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
794 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
795 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
796 bset
[i
]->ineq
[j
], 1+dim
);
798 k
= isl_basic_set_alloc_inequality(hull
);
801 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
802 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
804 for (j
= 0; j
< 1+dim
; ++j
) {
805 k
= isl_basic_set_alloc_equality(hull
);
808 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
809 isl_int_set_si(hull
->eq
[k
][j
], -1);
810 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
811 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
813 hull
= isl_basic_set_set_rational(hull
);
814 hull
= isl_basic_set_remove_dims(hull
, isl_dim_set
, dim
, 2*(1+dim
));
815 hull
= isl_basic_set_remove_redundancies(hull
);
816 isl_basic_set_free(bset1
);
817 isl_basic_set_free(bset2
);
820 isl_basic_set_free(bset1
);
821 isl_basic_set_free(bset2
);
822 isl_basic_set_free(hull
);
826 /* Is the set bounded for each value of the parameters?
828 isl_bool
isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
834 return isl_bool_error
;
835 if (isl_basic_set_plain_is_empty(bset
))
836 return isl_bool_true
;
838 tab
= isl_tab_from_recession_cone(bset
, 1);
839 bounded
= isl_tab_cone_is_bounded(tab
);
844 /* Is the image bounded for each value of the parameters and
845 * the domain variables?
847 isl_bool
isl_basic_map_image_is_bounded(__isl_keep isl_basic_map
*bmap
)
849 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
850 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
853 bmap
= isl_basic_map_copy(bmap
);
854 bmap
= isl_basic_map_cow(bmap
);
855 bmap
= isl_basic_map_move_dims(bmap
, isl_dim_param
, nparam
,
856 isl_dim_in
, 0, n_in
);
857 bounded
= isl_basic_set_is_bounded(bset_from_bmap(bmap
));
858 isl_basic_map_free(bmap
);
863 /* Is the set bounded for each value of the parameters?
865 isl_bool
isl_set_is_bounded(__isl_keep isl_set
*set
)
870 return isl_bool_error
;
872 for (i
= 0; i
< set
->n
; ++i
) {
873 isl_bool bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
874 if (!bounded
|| bounded
< 0)
877 return isl_bool_true
;
880 /* Compute the lineality space of the convex hull of bset1 and bset2.
882 * We first compute the intersection of the recession cone of bset1
883 * with the negative of the recession cone of bset2 and then compute
884 * the linear hull of the resulting cone.
886 static __isl_give isl_basic_set
*induced_lineality_space(
887 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
890 struct isl_basic_set
*lin
= NULL
;
893 if (!bset1
|| !bset2
)
896 dim
= isl_basic_set_total_dim(bset1
);
897 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset1
), 0,
898 bset1
->n_eq
+ bset2
->n_eq
,
899 bset1
->n_ineq
+ bset2
->n_ineq
);
900 lin
= isl_basic_set_set_rational(lin
);
903 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
904 k
= isl_basic_set_alloc_equality(lin
);
907 isl_int_set_si(lin
->eq
[k
][0], 0);
908 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
910 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
911 k
= isl_basic_set_alloc_inequality(lin
);
914 isl_int_set_si(lin
->ineq
[k
][0], 0);
915 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
917 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
918 k
= isl_basic_set_alloc_equality(lin
);
921 isl_int_set_si(lin
->eq
[k
][0], 0);
922 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
924 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
925 k
= isl_basic_set_alloc_inequality(lin
);
928 isl_int_set_si(lin
->ineq
[k
][0], 0);
929 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
932 isl_basic_set_free(bset1
);
933 isl_basic_set_free(bset2
);
934 return isl_basic_set_affine_hull(lin
);
936 isl_basic_set_free(lin
);
937 isl_basic_set_free(bset1
);
938 isl_basic_set_free(bset2
);
942 static __isl_give isl_basic_set
*uset_convex_hull(__isl_take isl_set
*set
);
944 /* Given a set and a linear space "lin" of dimension n > 0,
945 * project the linear space from the set, compute the convex hull
946 * and then map the set back to the original space.
952 * describe the linear space. We first compute the Hermite normal
953 * form H = M U of M = H Q, to obtain
957 * The last n rows of H will be zero, so the last n variables of x' = Q x
958 * are the one we want to project out. We do this by transforming each
959 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
960 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
961 * we transform the hull back to the original space as A' Q_1 x >= b',
962 * with Q_1 all but the last n rows of Q.
964 static __isl_give isl_basic_set
*modulo_lineality(__isl_take isl_set
*set
,
965 __isl_take isl_basic_set
*lin
)
967 unsigned total
= isl_basic_set_total_dim(lin
);
969 struct isl_basic_set
*hull
;
970 struct isl_mat
*M
, *U
, *Q
;
974 lin_dim
= total
- lin
->n_eq
;
975 M
= isl_mat_sub_alloc6(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
976 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
980 isl_basic_set_free(lin
);
982 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
984 U
= isl_mat_lin_to_aff(U
);
985 Q
= isl_mat_lin_to_aff(Q
);
987 set
= isl_set_preimage(set
, U
);
988 set
= isl_set_remove_dims(set
, isl_dim_set
, total
- lin_dim
, lin_dim
);
989 hull
= uset_convex_hull(set
);
990 hull
= isl_basic_set_preimage(hull
, Q
);
994 isl_basic_set_free(lin
);
999 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1000 * set up an LP for solving
1002 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1004 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1005 * The next \alpha{ij} correspond to the equalities and come in pairs.
1006 * The final \alpha{ij} correspond to the inequalities.
1008 static __isl_give isl_basic_set
*valid_direction_lp(
1009 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1012 struct isl_basic_set
*lp
;
1017 if (!bset1
|| !bset2
)
1019 d
= 1 + isl_basic_set_total_dim(bset1
);
1021 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1022 dim
= isl_space_set_alloc(bset1
->ctx
, 0, n
);
1023 lp
= isl_basic_set_alloc_space(dim
, 0, d
, n
);
1026 for (i
= 0; i
< n
; ++i
) {
1027 k
= isl_basic_set_alloc_inequality(lp
);
1030 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1031 isl_int_set_si(lp
->ineq
[k
][0], -1);
1032 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1034 for (i
= 0; i
< d
; ++i
) {
1035 k
= isl_basic_set_alloc_equality(lp
);
1039 isl_int_set_si(lp
->eq
[k
][n
], 0); n
++;
1040 /* positivity constraint 1 >= 0 */
1041 isl_int_set_si(lp
->eq
[k
][n
], i
== 0); n
++;
1042 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1043 isl_int_set(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1044 isl_int_neg(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1046 for (j
= 0; j
< bset1
->n_ineq
; ++j
) {
1047 isl_int_set(lp
->eq
[k
][n
], bset1
->ineq
[j
][i
]); n
++;
1049 /* positivity constraint 1 >= 0 */
1050 isl_int_set_si(lp
->eq
[k
][n
], -(i
== 0)); n
++;
1051 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1052 isl_int_neg(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1053 isl_int_set(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1055 for (j
= 0; j
< bset2
->n_ineq
; ++j
) {
1056 isl_int_neg(lp
->eq
[k
][n
], bset2
->ineq
[j
][i
]); n
++;
1059 lp
= isl_basic_set_gauss(lp
, NULL
);
1060 isl_basic_set_free(bset1
);
1061 isl_basic_set_free(bset2
);
1064 isl_basic_set_free(bset1
);
1065 isl_basic_set_free(bset2
);
1069 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1070 * for all rays in the homogeneous space of the two cones that correspond
1071 * to the input polyhedra bset1 and bset2.
1073 * We compute s as a vector that satisfies
1075 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1077 * with h_{ij} the normals of the facets of polyhedron i
1078 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1079 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1080 * We first set up an LP with as variables the \alpha{ij}.
1081 * In this formulation, for each polyhedron i,
1082 * the first constraint is the positivity constraint, followed by pairs
1083 * of variables for the equalities, followed by variables for the inequalities.
1084 * We then simply pick a feasible solution and compute s using (*).
1086 * Note that we simply pick any valid direction and make no attempt
1087 * to pick a "good" or even the "best" valid direction.
1089 static __isl_give isl_vec
*valid_direction(
1090 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1092 struct isl_basic_set
*lp
;
1093 struct isl_tab
*tab
;
1094 struct isl_vec
*sample
= NULL
;
1095 struct isl_vec
*dir
;
1100 if (!bset1
|| !bset2
)
1102 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1103 isl_basic_set_copy(bset2
));
1104 tab
= isl_tab_from_basic_set(lp
, 0);
1105 sample
= isl_tab_get_sample_value(tab
);
1107 isl_basic_set_free(lp
);
1110 d
= isl_basic_set_total_dim(bset1
);
1111 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1114 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1116 /* positivity constraint 1 >= 0 */
1117 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
]); n
++;
1118 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1119 isl_int_sub(sample
->block
.data
[n
],
1120 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1121 isl_seq_combine(dir
->block
.data
,
1122 bset1
->ctx
->one
, dir
->block
.data
,
1123 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1127 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1128 isl_seq_combine(dir
->block
.data
,
1129 bset1
->ctx
->one
, dir
->block
.data
,
1130 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1131 isl_vec_free(sample
);
1132 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1133 isl_basic_set_free(bset1
);
1134 isl_basic_set_free(bset2
);
1137 isl_vec_free(sample
);
1138 isl_basic_set_free(bset1
);
1139 isl_basic_set_free(bset2
);
1143 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1144 * compute b_i' + A_i' x' >= 0, with
1146 * [ b_i A_i ] [ y' ] [ y' ]
1147 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1149 * In particular, add the "positivity constraint" and then perform
1152 static __isl_give isl_basic_set
*homogeneous_map(__isl_take isl_basic_set
*bset
,
1153 __isl_take isl_mat
*T
)
1159 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1160 k
= isl_basic_set_alloc_inequality(bset
);
1163 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1164 isl_int_set_si(bset
->ineq
[k
][0], 1);
1165 bset
= isl_basic_set_preimage(bset
, T
);
1169 isl_basic_set_free(bset
);
1173 /* Compute the convex hull of a pair of basic sets without any parameters or
1174 * integer divisions, where the convex hull is known to be pointed,
1175 * but the basic sets may be unbounded.
1177 * We turn this problem into the computation of a convex hull of a pair
1178 * _bounded_ polyhedra by "changing the direction of the homogeneous
1179 * dimension". This idea is due to Matthias Koeppe.
1181 * Consider the cones in homogeneous space that correspond to the
1182 * input polyhedra. The rays of these cones are also rays of the
1183 * polyhedra if the coordinate that corresponds to the homogeneous
1184 * dimension is zero. That is, if the inner product of the rays
1185 * with the homogeneous direction is zero.
1186 * The cones in the homogeneous space can also be considered to
1187 * correspond to other pairs of polyhedra by chosing a different
1188 * homogeneous direction. To ensure that both of these polyhedra
1189 * are bounded, we need to make sure that all rays of the cones
1190 * correspond to vertices and not to rays.
1191 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1192 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1193 * The vector s is computed in valid_direction.
1195 * Note that we need to consider _all_ rays of the cones and not just
1196 * the rays that correspond to rays in the polyhedra. If we were to
1197 * only consider those rays and turn them into vertices, then we
1198 * may inadvertently turn some vertices into rays.
1200 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1201 * We therefore transform the two polyhedra such that the selected
1202 * direction is mapped onto this standard direction and then proceed
1203 * with the normal computation.
1204 * Let S be a non-singular square matrix with s as its first row,
1205 * then we want to map the polyhedra to the space
1207 * [ y' ] [ y ] [ y ] [ y' ]
1208 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1210 * We take S to be the unimodular completion of s to limit the growth
1211 * of the coefficients in the following computations.
1213 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1214 * We first move to the homogeneous dimension
1216 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1217 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1219 * Then we change directoin
1221 * [ b_i A_i ] [ y' ] [ y' ]
1222 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1224 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1225 * resulting in b' + A' x' >= 0, which we then convert back
1228 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1230 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1232 static __isl_give isl_basic_set
*convex_hull_pair_pointed(
1233 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1235 struct isl_ctx
*ctx
= NULL
;
1236 struct isl_vec
*dir
= NULL
;
1237 struct isl_mat
*T
= NULL
;
1238 struct isl_mat
*T2
= NULL
;
1239 struct isl_basic_set
*hull
;
1240 struct isl_set
*set
;
1242 if (!bset1
|| !bset2
)
1244 ctx
= isl_basic_set_get_ctx(bset1
);
1245 dir
= valid_direction(isl_basic_set_copy(bset1
),
1246 isl_basic_set_copy(bset2
));
1249 T
= isl_mat_alloc(ctx
, dir
->size
, dir
->size
);
1252 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1253 T
= isl_mat_unimodular_complete(T
, 1);
1254 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1256 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1257 bset2
= homogeneous_map(bset2
, T2
);
1258 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1259 set
= isl_set_add_basic_set(set
, bset1
);
1260 set
= isl_set_add_basic_set(set
, bset2
);
1261 hull
= uset_convex_hull(set
);
1262 hull
= isl_basic_set_preimage(hull
, T
);
1269 isl_basic_set_free(bset1
);
1270 isl_basic_set_free(bset2
);
1274 static __isl_give isl_basic_set
*uset_convex_hull_wrap(__isl_take isl_set
*set
);
1275 static __isl_give isl_basic_set
*modulo_affine_hull(
1276 __isl_take isl_set
*set
, __isl_take isl_basic_set
*affine_hull
);
1278 /* Compute the convex hull of a pair of basic sets without any parameters or
1279 * integer divisions.
1281 * This function is called from uset_convex_hull_unbounded, which
1282 * means that the complete convex hull is unbounded. Some pairs
1283 * of basic sets may still be bounded, though.
1284 * They may even lie inside a lower dimensional space, in which
1285 * case they need to be handled inside their affine hull since
1286 * the main algorithm assumes that the result is full-dimensional.
1288 * If the convex hull of the two basic sets would have a non-trivial
1289 * lineality space, we first project out this lineality space.
1291 static __isl_give isl_basic_set
*convex_hull_pair(
1292 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
1294 isl_basic_set
*lin
, *aff
;
1295 int bounded1
, bounded2
;
1297 if (bset1
->ctx
->opt
->convex
== ISL_CONVEX_HULL_FM
)
1298 return convex_hull_pair_elim(bset1
, bset2
);
1300 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1301 isl_basic_set_copy(bset2
)));
1305 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1306 isl_basic_set_free(aff
);
1308 bounded1
= isl_basic_set_is_bounded(bset1
);
1309 bounded2
= isl_basic_set_is_bounded(bset2
);
1311 if (bounded1
< 0 || bounded2
< 0)
1314 if (bounded1
&& bounded2
)
1315 return uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1317 if (bounded1
|| bounded2
)
1318 return convex_hull_pair_pointed(bset1
, bset2
);
1320 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1321 isl_basic_set_copy(bset2
));
1324 if (isl_basic_set_plain_is_universe(lin
)) {
1325 isl_basic_set_free(bset1
);
1326 isl_basic_set_free(bset2
);
1329 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1330 struct isl_set
*set
;
1331 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1332 set
= isl_set_add_basic_set(set
, bset1
);
1333 set
= isl_set_add_basic_set(set
, bset2
);
1334 return modulo_lineality(set
, lin
);
1336 isl_basic_set_free(lin
);
1338 return convex_hull_pair_pointed(bset1
, bset2
);
1340 isl_basic_set_free(bset1
);
1341 isl_basic_set_free(bset2
);
1345 /* Compute the lineality space of a basic set.
1346 * We currently do not allow the basic set to have any divs.
1347 * We basically just drop the constants and turn every inequality
1350 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1353 struct isl_basic_set
*lin
= NULL
;
1358 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1359 dim
= isl_basic_set_total_dim(bset
);
1361 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset
), 0, dim
, 0);
1364 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1365 k
= isl_basic_set_alloc_equality(lin
);
1368 isl_int_set_si(lin
->eq
[k
][0], 0);
1369 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1371 lin
= isl_basic_set_gauss(lin
, NULL
);
1374 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1375 k
= isl_basic_set_alloc_equality(lin
);
1378 isl_int_set_si(lin
->eq
[k
][0], 0);
1379 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1380 lin
= isl_basic_set_gauss(lin
, NULL
);
1384 isl_basic_set_free(bset
);
1387 isl_basic_set_free(lin
);
1388 isl_basic_set_free(bset
);
1392 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1393 * "underlying" set "set".
1395 static __isl_give isl_basic_set
*uset_combined_lineality_space(
1396 __isl_take isl_set
*set
)
1399 struct isl_set
*lin
= NULL
;
1404 isl_space
*space
= isl_set_get_space(set
);
1406 return isl_basic_set_empty(space
);
1409 lin
= isl_set_alloc_space(isl_set_get_space(set
), set
->n
, 0);
1410 for (i
= 0; i
< set
->n
; ++i
)
1411 lin
= isl_set_add_basic_set(lin
,
1412 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1414 return isl_set_affine_hull(lin
);
1417 /* Compute the convex hull of a set without any parameters or
1418 * integer divisions.
1419 * In each step, we combined two basic sets until only one
1420 * basic set is left.
1421 * The input basic sets are assumed not to have a non-trivial
1422 * lineality space. If any of the intermediate results has
1423 * a non-trivial lineality space, it is projected out.
1425 static __isl_give isl_basic_set
*uset_convex_hull_unbounded(
1426 __isl_take isl_set
*set
)
1428 isl_basic_set_list
*list
;
1430 list
= isl_set_get_basic_set_list(set
);
1435 struct isl_basic_set
*t
;
1436 isl_basic_set
*bset1
, *bset2
;
1438 n
= isl_basic_set_list_n_basic_set(list
);
1440 isl_die(isl_basic_set_list_get_ctx(list
),
1442 "expecting at least two elements", goto error
);
1443 bset1
= isl_basic_set_list_get_basic_set(list
, n
- 1);
1444 bset2
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1445 bset1
= convex_hull_pair(bset1
, bset2
);
1447 isl_basic_set_list_free(list
);
1450 bset1
= isl_basic_set_underlying_set(bset1
);
1451 list
= isl_basic_set_list_drop(list
, n
- 2, 2);
1452 list
= isl_basic_set_list_add(list
, bset1
);
1454 t
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1455 t
= isl_basic_set_lineality_space(t
);
1458 if (isl_basic_set_plain_is_universe(t
)) {
1459 isl_basic_set_list_free(list
);
1462 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1463 set
= isl_basic_set_list_union(list
);
1464 return modulo_lineality(set
, t
);
1466 isl_basic_set_free(t
);
1471 isl_basic_set_list_free(list
);
1475 /* Compute an initial hull for wrapping containing a single initial
1477 * This function assumes that the given set is bounded.
1479 static __isl_give isl_basic_set
*initial_hull(__isl_take isl_basic_set
*hull
,
1480 __isl_keep isl_set
*set
)
1482 struct isl_mat
*bounds
= NULL
;
1488 bounds
= initial_facet_constraint(set
);
1491 k
= isl_basic_set_alloc_inequality(hull
);
1494 dim
= isl_set_n_dim(set
);
1495 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1496 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1497 isl_mat_free(bounds
);
1501 isl_basic_set_free(hull
);
1502 isl_mat_free(bounds
);
1506 struct max_constraint
{
1512 static int max_constraint_equal(const void *entry
, const void *val
)
1514 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1515 isl_int
*b
= (isl_int
*)val
;
1517 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1520 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1521 isl_int
*con
, unsigned len
, int n
, int ineq
)
1523 struct isl_hash_table_entry
*entry
;
1524 struct max_constraint
*c
;
1527 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1528 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1534 isl_hash_table_remove(ctx
, table
, entry
);
1538 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1540 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1545 c
->c
= isl_mat_cow(c
->c
);
1546 isl_int_set(c
->c
->row
[0][0], con
[0]);
1550 /* Check whether the constraint hash table "table" constains the constraint
1553 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1554 isl_int
*con
, unsigned len
, int n
)
1556 struct isl_hash_table_entry
*entry
;
1557 struct max_constraint
*c
;
1560 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1561 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1568 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1571 /* Check for inequality constraints of a basic set without equalities
1572 * such that the same or more stringent copies of the constraint appear
1573 * in all of the basic sets. Such constraints are necessarily facet
1574 * constraints of the convex hull.
1576 * If the resulting basic set is by chance identical to one of
1577 * the basic sets in "set", then we know that this basic set contains
1578 * all other basic sets and is therefore the convex hull of set.
1579 * In this case we set *is_hull to 1.
1581 static __isl_give isl_basic_set
*common_constraints(
1582 __isl_take isl_basic_set
*hull
, __isl_keep isl_set
*set
, int *is_hull
)
1585 int min_constraints
;
1587 struct max_constraint
*constraints
= NULL
;
1588 struct isl_hash_table
*table
= NULL
;
1593 for (i
= 0; i
< set
->n
; ++i
)
1594 if (set
->p
[i
]->n_eq
== 0)
1598 min_constraints
= set
->p
[i
]->n_ineq
;
1600 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1601 if (set
->p
[i
]->n_eq
!= 0)
1603 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1605 min_constraints
= set
->p
[i
]->n_ineq
;
1608 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1612 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1613 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1616 total
= isl_space_dim(set
->dim
, isl_dim_all
);
1617 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1618 constraints
[i
].c
= isl_mat_sub_alloc6(hull
->ctx
,
1619 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1620 if (!constraints
[i
].c
)
1622 constraints
[i
].ineq
= 1;
1624 for (i
= 0; i
< min_constraints
; ++i
) {
1625 struct isl_hash_table_entry
*entry
;
1627 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1628 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1629 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1632 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1633 entry
->data
= &constraints
[i
];
1637 for (s
= 0; s
< set
->n
; ++s
) {
1641 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1642 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1643 for (j
= 0; j
< 2; ++j
) {
1644 isl_seq_neg(eq
, eq
, 1 + total
);
1645 update_constraint(hull
->ctx
, table
,
1649 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1650 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1651 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1652 set
->p
[s
]->n_eq
== 0);
1657 for (i
= 0; i
< min_constraints
; ++i
) {
1658 if (constraints
[i
].count
< n
)
1660 if (!constraints
[i
].ineq
)
1662 j
= isl_basic_set_alloc_inequality(hull
);
1665 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1668 for (s
= 0; s
< set
->n
; ++s
) {
1669 if (set
->p
[s
]->n_eq
)
1671 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1673 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1674 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1675 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1678 if (i
== set
->p
[s
]->n_ineq
)
1682 isl_hash_table_clear(table
);
1683 for (i
= 0; i
< min_constraints
; ++i
)
1684 isl_mat_free(constraints
[i
].c
);
1689 isl_hash_table_clear(table
);
1692 for (i
= 0; i
< min_constraints
; ++i
)
1693 isl_mat_free(constraints
[i
].c
);
1698 /* Create a template for the convex hull of "set" and fill it up
1699 * obvious facet constraints, if any. If the result happens to
1700 * be the convex hull of "set" then *is_hull is set to 1.
1702 static __isl_give isl_basic_set
*proto_hull(__isl_keep isl_set
*set
,
1705 struct isl_basic_set
*hull
;
1710 for (i
= 0; i
< set
->n
; ++i
) {
1711 n_ineq
+= set
->p
[i
]->n_eq
;
1712 n_ineq
+= set
->p
[i
]->n_ineq
;
1714 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
1715 hull
= isl_basic_set_set_rational(hull
);
1718 return common_constraints(hull
, set
, is_hull
);
1721 static __isl_give isl_basic_set
*uset_convex_hull_wrap(__isl_take isl_set
*set
)
1723 struct isl_basic_set
*hull
;
1726 hull
= proto_hull(set
, &is_hull
);
1727 if (hull
&& !is_hull
) {
1728 if (hull
->n_ineq
== 0)
1729 hull
= initial_hull(hull
, set
);
1730 hull
= extend(hull
, set
);
1737 /* Compute the convex hull of a set without any parameters or
1738 * integer divisions. Depending on whether the set is bounded,
1739 * we pass control to the wrapping based convex hull or
1740 * the Fourier-Motzkin elimination based convex hull.
1741 * We also handle a few special cases before checking the boundedness.
1743 static __isl_give isl_basic_set
*uset_convex_hull(__isl_take isl_set
*set
)
1746 struct isl_basic_set
*convex_hull
= NULL
;
1747 struct isl_basic_set
*lin
;
1749 if (isl_set_n_dim(set
) == 0)
1750 return convex_hull_0d(set
);
1752 set
= isl_set_coalesce(set
);
1753 set
= isl_set_set_rational(set
);
1758 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1762 if (isl_set_n_dim(set
) == 1)
1763 return convex_hull_1d(set
);
1765 bounded
= isl_set_is_bounded(set
);
1768 if (bounded
&& set
->ctx
->opt
->convex
== ISL_CONVEX_HULL_WRAP
)
1769 return uset_convex_hull_wrap(set
);
1771 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1774 if (isl_basic_set_plain_is_universe(lin
)) {
1778 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1779 return modulo_lineality(set
, lin
);
1780 isl_basic_set_free(lin
);
1782 return uset_convex_hull_unbounded(set
);
1785 isl_basic_set_free(convex_hull
);
1789 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1790 * without parameters or divs and where the convex hull of set is
1791 * known to be full-dimensional.
1793 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1795 struct isl_basic_set
*convex_hull
= NULL
;
1800 if (isl_set_n_dim(set
) == 0) {
1801 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
1803 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1807 set
= isl_set_set_rational(set
);
1808 set
= isl_set_coalesce(set
);
1812 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1814 convex_hull
= isl_basic_map_remove_redundancies(convex_hull
);
1817 if (isl_set_n_dim(set
) == 1)
1818 return convex_hull_1d(set
);
1820 return uset_convex_hull_wrap(set
);
1826 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1827 * We first remove the equalities (transforming the set), compute the
1828 * convex hull of the transformed set and then add the equalities back
1829 * (after performing the inverse transformation.
1831 static __isl_give isl_basic_set
*modulo_affine_hull(
1832 __isl_take isl_set
*set
, __isl_take isl_basic_set
*affine_hull
)
1836 struct isl_basic_set
*dummy
;
1837 struct isl_basic_set
*convex_hull
;
1839 dummy
= isl_basic_set_remove_equalities(
1840 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1843 isl_basic_set_free(dummy
);
1844 set
= isl_set_preimage(set
, T
);
1845 convex_hull
= uset_convex_hull(set
);
1846 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1847 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1850 isl_basic_set_free(affine_hull
);
1855 /* Return an empty basic map living in the same space as "map".
1857 static __isl_give isl_basic_map
*replace_map_by_empty_basic_map(
1858 __isl_take isl_map
*map
)
1862 space
= isl_map_get_space(map
);
1864 return isl_basic_map_empty(space
);
1867 /* Compute the convex hull of a map.
1869 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1870 * specifically, the wrapping of facets to obtain new facets.
1872 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1874 struct isl_basic_set
*bset
;
1875 struct isl_basic_map
*model
= NULL
;
1876 struct isl_basic_set
*affine_hull
= NULL
;
1877 struct isl_basic_map
*convex_hull
= NULL
;
1878 struct isl_set
*set
= NULL
;
1880 map
= isl_map_detect_equalities(map
);
1881 map
= isl_map_align_divs_internal(map
);
1886 return replace_map_by_empty_basic_map(map
);
1888 model
= isl_basic_map_copy(map
->p
[0]);
1889 set
= isl_map_underlying_set(map
);
1893 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1896 if (affine_hull
->n_eq
!= 0)
1897 bset
= modulo_affine_hull(set
, affine_hull
);
1899 isl_basic_set_free(affine_hull
);
1900 bset
= uset_convex_hull(set
);
1903 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1907 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1908 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1909 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1913 isl_basic_map_free(model
);
1917 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1919 return bset_from_bmap(isl_map_convex_hull(set_to_map(set
)));
1922 __isl_give isl_basic_map
*isl_map_polyhedral_hull(__isl_take isl_map
*map
)
1924 isl_basic_map
*hull
;
1926 hull
= isl_map_convex_hull(map
);
1927 return isl_basic_map_remove_divs(hull
);
1930 __isl_give isl_basic_set
*isl_set_polyhedral_hull(__isl_take isl_set
*set
)
1932 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set
)));
1935 struct sh_data_entry
{
1936 struct isl_hash_table
*table
;
1937 struct isl_tab
*tab
;
1940 /* Holds the data needed during the simple hull computation.
1942 * n the number of basic sets in the original set
1943 * hull_table a hash table of already computed constraints
1944 * in the simple hull
1945 * p for each basic set,
1946 * table a hash table of the constraints
1947 * tab the tableau corresponding to the basic set
1950 struct isl_ctx
*ctx
;
1952 struct isl_hash_table
*hull_table
;
1953 struct sh_data_entry p
[1];
1956 static void sh_data_free(struct sh_data
*data
)
1962 isl_hash_table_free(data
->ctx
, data
->hull_table
);
1963 for (i
= 0; i
< data
->n
; ++i
) {
1964 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
1965 isl_tab_free(data
->p
[i
].tab
);
1970 struct ineq_cmp_data
{
1975 static int has_ineq(const void *entry
, const void *val
)
1977 isl_int
*row
= (isl_int
*)entry
;
1978 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
1980 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
1981 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
1984 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1985 isl_int
*ineq
, unsigned len
)
1988 struct ineq_cmp_data v
;
1989 struct isl_hash_table_entry
*entry
;
1993 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
1994 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
2001 /* Fill hash table "table" with the constraints of "bset".
2002 * Equalities are added as two inequalities.
2003 * The value in the hash table is a pointer to the (in)equality of "bset".
2005 static int hash_basic_set(struct isl_hash_table
*table
,
2006 struct isl_basic_set
*bset
)
2009 unsigned dim
= isl_basic_set_total_dim(bset
);
2011 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2012 for (j
= 0; j
< 2; ++j
) {
2013 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2014 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2018 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2019 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2025 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
2027 struct sh_data
*data
;
2030 data
= isl_calloc(set
->ctx
, struct sh_data
,
2031 sizeof(struct sh_data
) +
2032 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2035 data
->ctx
= set
->ctx
;
2037 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2038 if (!data
->hull_table
)
2040 for (i
= 0; i
< set
->n
; ++i
) {
2041 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2042 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2043 if (!data
->p
[i
].table
)
2045 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2054 /* Check if inequality "ineq" is a bound for basic set "j" or if
2055 * it can be relaxed (by increasing the constant term) to become
2056 * a bound for that basic set. In the latter case, the constant
2058 * Relaxation of the constant term is only allowed if "shift" is set.
2060 * Return 1 if "ineq" is a bound
2061 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2062 * -1 if some error occurred
2064 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2065 isl_int
*ineq
, int shift
)
2067 enum isl_lp_result res
;
2070 if (!data
->p
[j
].tab
) {
2071 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
], 0);
2072 if (!data
->p
[j
].tab
)
2078 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2080 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
)) {
2082 isl_int_sub(ineq
[0], ineq
[0], opt
);
2084 res
= isl_lp_unbounded
;
2089 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2090 res
== isl_lp_unbounded
? 0 : -1;
2093 /* Set the constant term of "ineq" to the maximum of those of the constraints
2094 * in the basic sets of "set" following "i" that are parallel to "ineq".
2095 * That is, if any of the basic sets of "set" following "i" have a more
2096 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2097 * "c_hash" is the hash value of the linear part of "ineq".
2098 * "v" has been set up for use by has_ineq.
2100 * Note that the two inequality constraints corresponding to an equality are
2101 * represented by the same inequality constraint in data->p[j].table
2102 * (but with different hash values). This means the constraint (or at
2103 * least its constant term) may need to be temporarily negated to get
2104 * the actually hashed constraint.
2106 static void set_max_constant_term(struct sh_data
*data
, __isl_keep isl_set
*set
,
2107 int i
, isl_int
*ineq
, uint32_t c_hash
, struct ineq_cmp_data
*v
)
2111 struct isl_hash_table_entry
*entry
;
2113 ctx
= isl_set_get_ctx(set
);
2114 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2118 entry
= isl_hash_table_find(ctx
, data
->p
[j
].table
,
2119 c_hash
, &has_ineq
, v
, 0);
2123 ineq_j
= entry
->data
;
2124 neg
= isl_seq_is_neg(ineq_j
+ 1, ineq
+ 1, v
->len
);
2126 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2127 if (isl_int_gt(ineq_j
[0], ineq
[0]))
2128 isl_int_set(ineq
[0], ineq_j
[0]);
2130 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2134 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2135 * become a bound on the whole set. If so, add the (relaxed) inequality
2136 * to "hull". Relaxation is only allowed if "shift" is set.
2138 * We first check if "hull" already contains a translate of the inequality.
2139 * If so, we are done.
2140 * Then, we check if any of the previous basic sets contains a translate
2141 * of the inequality. If so, then we have already considered this
2142 * inequality and we are done.
2143 * Otherwise, for each basic set other than "i", we check if the inequality
2144 * is a bound on the basic set, but first replace the constant term
2145 * by the maximal value of any translate of the inequality in any
2146 * of the following basic sets.
2147 * For previous basic sets, we know that they do not contain a translate
2148 * of the inequality, so we directly call is_bound.
2149 * For following basic sets, we first check if a translate of the
2150 * inequality appears in its description. If so, the constant term
2151 * of the inequality has already been updated with respect to this
2152 * translate and the inequality is therefore known to be a bound
2153 * of this basic set.
2155 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2156 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
,
2160 struct ineq_cmp_data v
;
2161 struct isl_hash_table_entry
*entry
;
2167 v
.len
= isl_basic_set_total_dim(hull
);
2169 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2171 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2176 for (j
= 0; j
< i
; ++j
) {
2177 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2178 c_hash
, has_ineq
, &v
, 0);
2185 k
= isl_basic_set_alloc_inequality(hull
);
2188 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2190 set_max_constant_term(data
, set
, i
, hull
->ineq
[k
], c_hash
, &v
);
2191 for (j
= 0; j
< i
; ++j
) {
2193 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2200 isl_basic_set_free_inequality(hull
, 1);
2204 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2206 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2207 c_hash
, has_ineq
, &v
, 0);
2210 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2217 isl_basic_set_free_inequality(hull
, 1);
2221 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2225 entry
->data
= hull
->ineq
[k
];
2229 isl_basic_set_free(hull
);
2233 /* Check if any inequality from basic set "i" is or can be relaxed to
2234 * become a bound on the whole set. If so, add the (relaxed) inequality
2235 * to "hull". Relaxation is only allowed if "shift" is set.
2237 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2238 struct sh_data
*data
, struct isl_set
*set
, int i
, int shift
)
2241 unsigned dim
= isl_basic_set_total_dim(bset
);
2243 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2244 for (k
= 0; k
< 2; ++k
) {
2245 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2246 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
],
2250 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2251 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
], shift
);
2255 /* Compute a superset of the convex hull of set that is described
2256 * by only (translates of) the constraints in the constituents of set.
2257 * Translation is only allowed if "shift" is set.
2259 static __isl_give isl_basic_set
*uset_simple_hull(__isl_take isl_set
*set
,
2262 struct sh_data
*data
= NULL
;
2263 struct isl_basic_set
*hull
= NULL
;
2271 for (i
= 0; i
< set
->n
; ++i
) {
2274 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2277 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
2281 data
= sh_data_alloc(set
, n_ineq
);
2285 for (i
= 0; i
< set
->n
; ++i
)
2286 hull
= add_bounds(hull
, data
, set
, i
, shift
);
2294 isl_basic_set_free(hull
);
2299 /* Compute a superset of the convex hull of map that is described
2300 * by only (translates of) the constraints in the constituents of map.
2301 * Handle trivial cases where map is NULL or contains at most one disjunct.
2303 static __isl_give isl_basic_map
*map_simple_hull_trivial(
2304 __isl_take isl_map
*map
)
2306 isl_basic_map
*hull
;
2311 return replace_map_by_empty_basic_map(map
);
2313 hull
= isl_basic_map_copy(map
->p
[0]);
2318 /* Return a copy of the simple hull cached inside "map".
2319 * "shift" determines whether to return the cached unshifted or shifted
2322 static __isl_give isl_basic_map
*cached_simple_hull(__isl_take isl_map
*map
,
2325 isl_basic_map
*hull
;
2327 hull
= isl_basic_map_copy(map
->cached_simple_hull
[shift
]);
2333 /* Compute a superset of the convex hull of map that is described
2334 * by only (translates of) the constraints in the constituents of map.
2335 * Translation is only allowed if "shift" is set.
2337 * The constraints are sorted while removing redundant constraints
2338 * in order to indicate a preference of which constraints should
2339 * be preserved. In particular, pairs of constraints that are
2340 * sorted together are preferred to either both be preserved
2341 * or both be removed. The sorting is performed inside
2342 * isl_basic_map_remove_redundancies.
2344 * The result of the computation is stored in map->cached_simple_hull[shift]
2345 * such that it can be reused in subsequent calls. The cache is cleared
2346 * whenever the map is modified (in isl_map_cow).
2347 * Note that the results need to be stored in the input map for there
2348 * to be any chance that they may get reused. In particular, they
2349 * are stored in a copy of the input map that is saved before
2350 * the integer division alignment.
2352 static __isl_give isl_basic_map
*map_simple_hull(__isl_take isl_map
*map
,
2355 struct isl_set
*set
= NULL
;
2356 struct isl_basic_map
*model
= NULL
;
2357 struct isl_basic_map
*hull
;
2358 struct isl_basic_map
*affine_hull
;
2359 struct isl_basic_set
*bset
= NULL
;
2362 if (!map
|| map
->n
<= 1)
2363 return map_simple_hull_trivial(map
);
2365 if (map
->cached_simple_hull
[shift
])
2366 return cached_simple_hull(map
, shift
);
2368 map
= isl_map_detect_equalities(map
);
2369 if (!map
|| map
->n
<= 1)
2370 return map_simple_hull_trivial(map
);
2371 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2372 input
= isl_map_copy(map
);
2373 map
= isl_map_align_divs_internal(map
);
2374 model
= map
? isl_basic_map_copy(map
->p
[0]) : NULL
;
2376 set
= isl_map_underlying_set(map
);
2378 bset
= uset_simple_hull(set
, shift
);
2380 hull
= isl_basic_map_overlying_set(bset
, model
);
2382 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2383 hull
= isl_basic_map_remove_redundancies(hull
);
2386 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2387 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2390 hull
= isl_basic_map_finalize(hull
);
2392 input
->cached_simple_hull
[shift
] = isl_basic_map_copy(hull
);
2393 isl_map_free(input
);
2398 /* Compute a superset of the convex hull of map that is described
2399 * by only translates of the constraints in the constituents of map.
2401 __isl_give isl_basic_map
*isl_map_simple_hull(__isl_take isl_map
*map
)
2403 return map_simple_hull(map
, 1);
2406 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2408 return bset_from_bmap(isl_map_simple_hull(set_to_map(set
)));
2411 /* Compute a superset of the convex hull of map that is described
2412 * by only the constraints in the constituents of map.
2414 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull(
2415 __isl_take isl_map
*map
)
2417 return map_simple_hull(map
, 0);
2420 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull(
2421 __isl_take isl_set
*set
)
2423 return isl_map_unshifted_simple_hull(set
);
2426 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2427 * A constraint that appears with different constant terms
2428 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2429 * (i.e., greatest) constant term.
2430 * "bmap1" and "bmap2" are assumed to have the same (known)
2431 * integer divisions.
2432 * The constraints of both "bmap1" and "bmap2" are assumed
2433 * to have been sorted using isl_basic_map_sort_constraints.
2435 * Run through the inequality constraints of "bmap1" and "bmap2"
2437 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2439 * If a match is found, the constraint is kept. If needed, the constant
2440 * term of the constraint is adjusted.
2442 static __isl_give isl_basic_map
*select_shared_inequalities(
2443 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2447 bmap1
= isl_basic_map_cow(bmap1
);
2448 if (!bmap1
|| !bmap2
)
2449 return isl_basic_map_free(bmap1
);
2451 i1
= bmap1
->n_ineq
- 1;
2452 i2
= bmap2
->n_ineq
- 1;
2453 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2456 cmp
= isl_basic_map_constraint_cmp(bmap1
, bmap1
->ineq
[i1
],
2463 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2464 bmap1
= isl_basic_map_free(bmap1
);
2468 if (isl_int_lt(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]))
2469 isl_int_set(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]);
2473 for (; i1
>= 0; --i1
)
2474 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2475 bmap1
= isl_basic_map_free(bmap1
);
2480 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2481 * "bmap1" and "bmap2" are assumed to have the same (known)
2482 * integer divisions.
2484 * Run through the equality constraints of "bmap1" and "bmap2".
2485 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2488 static __isl_give isl_basic_map
*select_shared_equalities(
2489 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2494 bmap1
= isl_basic_map_cow(bmap1
);
2495 if (!bmap1
|| !bmap2
)
2496 return isl_basic_map_free(bmap1
);
2498 total
= isl_basic_map_total_dim(bmap1
);
2500 i1
= bmap1
->n_eq
- 1;
2501 i2
= bmap2
->n_eq
- 1;
2502 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2505 last1
= isl_seq_last_non_zero(bmap1
->eq
[i1
] + 1, total
);
2506 last2
= isl_seq_last_non_zero(bmap2
->eq
[i2
] + 1, total
);
2507 if (last1
> last2
) {
2511 if (last1
< last2
) {
2512 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2513 bmap1
= isl_basic_map_free(bmap1
);
2517 if (!isl_seq_eq(bmap1
->eq
[i1
], bmap2
->eq
[i2
], 1 + total
)) {
2518 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2519 bmap1
= isl_basic_map_free(bmap1
);
2524 for (; i1
>= 0; --i1
)
2525 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2526 bmap1
= isl_basic_map_free(bmap1
);
2531 /* Compute a superset of "bmap1" and "bmap2" that is described
2532 * by only the constraints that appear in both "bmap1" and "bmap2".
2534 * First drop constraints that involve unknown integer divisions
2535 * since it is not trivial to check whether two such integer divisions
2536 * in different basic maps are the same.
2537 * Then align the remaining (known) divs and sort the constraints.
2538 * Finally drop all inequalities and equalities from "bmap1" that
2539 * do not also appear in "bmap2".
2541 __isl_give isl_basic_map
*isl_basic_map_plain_unshifted_simple_hull(
2542 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
2544 bmap1
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap1
);
2545 bmap2
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap2
);
2546 bmap2
= isl_basic_map_align_divs(bmap2
, bmap1
);
2547 bmap1
= isl_basic_map_align_divs(bmap1
, bmap2
);
2548 bmap1
= isl_basic_map_gauss(bmap1
, NULL
);
2549 bmap2
= isl_basic_map_gauss(bmap2
, NULL
);
2550 bmap1
= isl_basic_map_sort_constraints(bmap1
);
2551 bmap2
= isl_basic_map_sort_constraints(bmap2
);
2553 bmap1
= select_shared_inequalities(bmap1
, bmap2
);
2554 bmap1
= select_shared_equalities(bmap1
, bmap2
);
2556 isl_basic_map_free(bmap2
);
2557 bmap1
= isl_basic_map_finalize(bmap1
);
2561 /* Compute a superset of the convex hull of "map" that is described
2562 * by only the constraints in the constituents of "map".
2563 * In particular, the result is composed of constraints that appear
2564 * in each of the basic maps of "map"
2566 * Constraints that involve unknown integer divisions are dropped
2567 * since it is not trivial to check whether two such integer divisions
2568 * in different basic maps are the same.
2570 * The hull is initialized from the first basic map and then
2571 * updated with respect to the other basic maps in turn.
2573 __isl_give isl_basic_map
*isl_map_plain_unshifted_simple_hull(
2574 __isl_take isl_map
*map
)
2577 isl_basic_map
*hull
;
2582 return map_simple_hull_trivial(map
);
2583 map
= isl_map_drop_constraint_involving_unknown_divs(map
);
2584 hull
= isl_basic_map_copy(map
->p
[0]);
2585 for (i
= 1; i
< map
->n
; ++i
) {
2586 isl_basic_map
*bmap_i
;
2588 bmap_i
= isl_basic_map_copy(map
->p
[i
]);
2589 hull
= isl_basic_map_plain_unshifted_simple_hull(hull
, bmap_i
);
2596 /* Compute a superset of the convex hull of "set" that is described
2597 * by only the constraints in the constituents of "set".
2598 * In particular, the result is composed of constraints that appear
2599 * in each of the basic sets of "set"
2601 __isl_give isl_basic_set
*isl_set_plain_unshifted_simple_hull(
2602 __isl_take isl_set
*set
)
2604 return isl_map_plain_unshifted_simple_hull(set
);
2607 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2609 * For each basic set in "set", we first check if the basic set
2610 * contains a translate of "ineq". If this translate is more relaxed,
2611 * then we assume that "ineq" is not a bound on this basic set.
2612 * Otherwise, we know that it is a bound.
2613 * If the basic set does not contain a translate of "ineq", then
2614 * we call is_bound to perform the test.
2616 static __isl_give isl_basic_set
*add_bound_from_constraint(
2617 __isl_take isl_basic_set
*hull
, struct sh_data
*data
,
2618 __isl_keep isl_set
*set
, isl_int
*ineq
)
2623 struct ineq_cmp_data v
;
2626 return isl_basic_set_free(hull
);
2628 v
.len
= isl_basic_set_total_dim(hull
);
2630 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2632 ctx
= isl_basic_set_get_ctx(hull
);
2633 for (i
= 0; i
< set
->n
; ++i
) {
2635 struct isl_hash_table_entry
*entry
;
2637 entry
= isl_hash_table_find(ctx
, data
->p
[i
].table
,
2638 c_hash
, &has_ineq
, &v
, 0);
2640 isl_int
*ineq_i
= entry
->data
;
2641 int neg
, more_relaxed
;
2643 neg
= isl_seq_is_neg(ineq_i
+ 1, ineq
+ 1, v
.len
);
2645 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2646 more_relaxed
= isl_int_gt(ineq_i
[0], ineq
[0]);
2648 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2654 bound
= is_bound(data
, set
, i
, ineq
, 0);
2656 return isl_basic_set_free(hull
);
2663 k
= isl_basic_set_alloc_inequality(hull
);
2665 return isl_basic_set_free(hull
);
2666 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2671 /* Compute a superset of the convex hull of "set" that is described
2672 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2673 * has no parameters or integer divisions.
2675 * The inequalities in "ineq" are assumed to have been sorted such
2676 * that constraints with the same linear part appear together and
2677 * that among constraints with the same linear part, those with
2678 * smaller constant term appear first.
2680 * We reuse the same data structure that is used by uset_simple_hull,
2681 * but we do not need the hull table since we will not consider the
2682 * same constraint more than once. We therefore allocate it with zero size.
2684 * We run through the constraints and try to add them one by one,
2685 * skipping identical constraints. If we have added a constraint and
2686 * the next constraint is a more relaxed translate, then we skip this
2687 * next constraint as well.
2689 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_constraints(
2690 __isl_take isl_set
*set
, int n_ineq
, isl_int
**ineq
)
2694 struct sh_data
*data
= NULL
;
2695 isl_basic_set
*hull
= NULL
;
2698 hull
= isl_basic_set_alloc_space(isl_set_get_space(set
), 0, 0, n_ineq
);
2702 data
= sh_data_alloc(set
, 0);
2706 dim
= isl_set_dim(set
, isl_dim_set
);
2707 for (i
= 0; i
< n_ineq
; ++i
) {
2708 int hull_n_ineq
= hull
->n_ineq
;
2711 parallel
= i
> 0 && isl_seq_eq(ineq
[i
- 1] + 1, ineq
[i
] + 1,
2714 (last_added
|| isl_int_eq(ineq
[i
- 1][0], ineq
[i
][0])))
2716 hull
= add_bound_from_constraint(hull
, data
, set
, ineq
[i
]);
2719 last_added
= hull
->n_ineq
> hull_n_ineq
;
2728 isl_basic_set_free(hull
);
2732 /* Collect pointers to all the inequalities in the elements of "list"
2733 * in "ineq". For equalities, store both a pointer to the equality and
2734 * a pointer to its opposite, which is first copied to "mat".
2735 * "ineq" and "mat" are assumed to have been preallocated to the right size
2736 * (the number of inequalities + 2 times the number of equalites and
2737 * the number of equalities, respectively).
2739 static __isl_give isl_mat
*collect_inequalities(__isl_take isl_mat
*mat
,
2740 __isl_keep isl_basic_set_list
*list
, isl_int
**ineq
)
2742 int i
, j
, n
, n_eq
, n_ineq
;
2749 n
= isl_basic_set_list_n_basic_set(list
);
2750 for (i
= 0; i
< n
; ++i
) {
2751 isl_basic_set
*bset
;
2752 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2754 return isl_mat_free(mat
);
2755 for (j
= 0; j
< bset
->n_eq
; ++j
) {
2756 ineq
[n_ineq
++] = mat
->row
[n_eq
];
2757 ineq
[n_ineq
++] = bset
->eq
[j
];
2758 isl_seq_neg(mat
->row
[n_eq
++], bset
->eq
[j
], mat
->n_col
);
2760 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2761 ineq
[n_ineq
++] = bset
->ineq
[j
];
2762 isl_basic_set_free(bset
);
2768 /* Comparison routine for use as an isl_sort callback.
2770 * Constraints with the same linear part are sorted together and
2771 * among constraints with the same linear part, those with smaller
2772 * constant term are sorted first.
2774 static int cmp_ineq(const void *a
, const void *b
, void *arg
)
2776 unsigned dim
= *(unsigned *) arg
;
2777 isl_int
* const *ineq1
= a
;
2778 isl_int
* const *ineq2
= b
;
2781 cmp
= isl_seq_cmp((*ineq1
) + 1, (*ineq2
) + 1, dim
);
2784 return isl_int_cmp((*ineq1
)[0], (*ineq2
)[0]);
2787 /* Compute a superset of the convex hull of "set" that is described
2788 * by only constraints in the elements of "list", where "set" has
2789 * no parameters or integer divisions.
2791 * We collect all the constraints in those elements and then
2792 * sort the constraints such that constraints with the same linear part
2793 * are sorted together and that those with smaller constant term are
2796 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_basic_set_list(
2797 __isl_take isl_set
*set
, __isl_take isl_basic_set_list
*list
)
2799 int i
, n
, n_eq
, n_ineq
;
2802 isl_mat
*mat
= NULL
;
2803 isl_int
**ineq
= NULL
;
2804 isl_basic_set
*hull
;
2808 ctx
= isl_set_get_ctx(set
);
2812 n
= isl_basic_set_list_n_basic_set(list
);
2813 for (i
= 0; i
< n
; ++i
) {
2814 isl_basic_set
*bset
;
2815 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2819 n_ineq
+= 2 * bset
->n_eq
+ bset
->n_ineq
;
2820 isl_basic_set_free(bset
);
2823 ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
);
2824 if (n_ineq
> 0 && !ineq
)
2827 dim
= isl_set_dim(set
, isl_dim_set
);
2828 mat
= isl_mat_alloc(ctx
, n_eq
, 1 + dim
);
2829 mat
= collect_inequalities(mat
, list
, ineq
);
2833 if (isl_sort(ineq
, n_ineq
, sizeof(ineq
[0]), &cmp_ineq
, &dim
) < 0)
2836 hull
= uset_unshifted_simple_hull_from_constraints(set
, n_ineq
, ineq
);
2840 isl_basic_set_list_free(list
);
2846 isl_basic_set_list_free(list
);
2850 /* Compute a superset of the convex hull of "map" that is described
2851 * by only constraints in the elements of "list".
2853 * If the list is empty, then we can only describe the universe set.
2854 * If the input map is empty, then all constraints are valid, so
2855 * we return the intersection of the elements in "list".
2857 * Otherwise, we align all divs and temporarily treat them
2858 * as regular variables, computing the unshifted simple hull in
2859 * uset_unshifted_simple_hull_from_basic_set_list.
2861 static __isl_give isl_basic_map
*map_unshifted_simple_hull_from_basic_map_list(
2862 __isl_take isl_map
*map
, __isl_take isl_basic_map_list
*list
)
2864 isl_basic_map
*model
;
2865 isl_basic_map
*hull
;
2867 isl_basic_set_list
*bset_list
;
2872 if (isl_basic_map_list_n_basic_map(list
) == 0) {
2875 space
= isl_map_get_space(map
);
2877 isl_basic_map_list_free(list
);
2878 return isl_basic_map_universe(space
);
2880 if (isl_map_plain_is_empty(map
)) {
2882 return isl_basic_map_list_intersect(list
);
2885 map
= isl_map_align_divs_to_basic_map_list(map
, list
);
2888 list
= isl_basic_map_list_align_divs_to_basic_map(list
, map
->p
[0]);
2890 model
= isl_basic_map_list_get_basic_map(list
, 0);
2892 set
= isl_map_underlying_set(map
);
2893 bset_list
= isl_basic_map_list_underlying_set(list
);
2895 hull
= uset_unshifted_simple_hull_from_basic_set_list(set
, bset_list
);
2896 hull
= isl_basic_map_overlying_set(hull
, model
);
2901 isl_basic_map_list_free(list
);
2905 /* Return a sequence of the basic maps that make up the maps in "list".
2907 static __isl_give isl_basic_map_list
*collect_basic_maps(
2908 __isl_take isl_map_list
*list
)
2912 isl_basic_map_list
*bmap_list
;
2916 n
= isl_map_list_n_map(list
);
2917 ctx
= isl_map_list_get_ctx(list
);
2918 bmap_list
= isl_basic_map_list_alloc(ctx
, 0);
2920 for (i
= 0; i
< n
; ++i
) {
2922 isl_basic_map_list
*list_i
;
2924 map
= isl_map_list_get_map(list
, i
);
2925 map
= isl_map_compute_divs(map
);
2926 list_i
= isl_map_get_basic_map_list(map
);
2928 bmap_list
= isl_basic_map_list_concat(bmap_list
, list_i
);
2931 isl_map_list_free(list
);
2935 /* Compute a superset of the convex hull of "map" that is described
2936 * by only constraints in the elements of "list".
2938 * If "map" is the universe, then the convex hull (and therefore
2939 * any superset of the convexhull) is the universe as well.
2941 * Otherwise, we collect all the basic maps in the map list and
2942 * continue with map_unshifted_simple_hull_from_basic_map_list.
2944 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull_from_map_list(
2945 __isl_take isl_map
*map
, __isl_take isl_map_list
*list
)
2947 isl_basic_map_list
*bmap_list
;
2950 is_universe
= isl_map_plain_is_universe(map
);
2951 if (is_universe
< 0)
2952 map
= isl_map_free(map
);
2953 if (is_universe
< 0 || is_universe
) {
2954 isl_map_list_free(list
);
2955 return isl_map_unshifted_simple_hull(map
);
2958 bmap_list
= collect_basic_maps(list
);
2959 return map_unshifted_simple_hull_from_basic_map_list(map
, bmap_list
);
2962 /* Compute a superset of the convex hull of "set" that is described
2963 * by only constraints in the elements of "list".
2965 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull_from_set_list(
2966 __isl_take isl_set
*set
, __isl_take isl_set_list
*list
)
2968 return isl_map_unshifted_simple_hull_from_map_list(set
, list
);
2971 /* Given a set "set", return parametric bounds on the dimension "dim".
2973 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
2975 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
2976 set
= isl_set_copy(set
);
2977 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
2978 set
= isl_set_eliminate_dims(set
, 0, dim
);
2979 return isl_set_convex_hull(set
);
2982 /* Computes a "simple hull" and then check if each dimension in the
2983 * resulting hull is bounded by a symbolic constant. If not, the
2984 * hull is intersected with the corresponding bounds on the whole set.
2986 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
2989 struct isl_basic_set
*hull
;
2990 unsigned nparam
, left
;
2991 int removed_divs
= 0;
2993 hull
= isl_set_simple_hull(isl_set_copy(set
));
2997 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
2998 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
2999 int lower
= 0, upper
= 0;
3000 struct isl_basic_set
*bounds
;
3002 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
3003 for (j
= 0; j
< hull
->n_eq
; ++j
) {
3004 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
3006 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
3013 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
3014 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
3016 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
3018 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
3021 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
3032 if (!removed_divs
) {
3033 set
= isl_set_remove_divs(set
);
3038 bounds
= set_bounds(set
, i
);
3039 hull
= isl_basic_set_intersect(hull
, bounds
);
3048 isl_basic_set_free(hull
);