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 __isl_give isl_basic_map
*isl_basic_map_set_rational(
160 __isl_take isl_basic_map
*bmap
)
165 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
168 bmap
= isl_basic_map_cow(bmap
);
172 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
174 return isl_basic_map_finalize(bmap
);
177 __isl_give isl_basic_set
*isl_basic_set_set_rational(
178 __isl_take isl_basic_set
*bset
)
180 return isl_basic_map_set_rational(bset
);
183 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
187 map
= isl_map_cow(map
);
190 for (i
= 0; i
< map
->n
; ++i
) {
191 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
201 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
203 return isl_map_set_rational(set
);
206 static struct isl_basic_set
*isl_basic_set_add_equality(
207 struct isl_basic_set
*bset
, isl_int
*c
)
215 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
218 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
219 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
220 dim
= isl_basic_set_n_dim(bset
);
221 bset
= isl_basic_set_cow(bset
);
222 bset
= isl_basic_set_extend(bset
, 0, dim
, 0, 1, 0);
223 i
= isl_basic_set_alloc_equality(bset
);
226 isl_seq_cpy(bset
->eq
[i
], c
, 1 + dim
);
229 isl_basic_set_free(bset
);
233 static struct isl_set
*isl_set_add_basic_set_equality(struct isl_set
*set
, isl_int
*c
)
237 set
= isl_set_cow(set
);
240 for (i
= 0; i
< set
->n
; ++i
) {
241 set
->p
[i
] = isl_basic_set_add_equality(set
->p
[i
], c
);
251 /* Given a union of basic sets, construct the constraints for wrapping
252 * a facet around one of its ridges.
253 * In particular, if each of n the d-dimensional basic sets i in "set"
254 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
255 * and is defined by the constraints
259 * then the resulting set is of dimension n*(1+d) and has as constraints
268 static struct isl_basic_set
*wrap_constraints(struct isl_set
*set
)
270 struct isl_basic_set
*lp
;
274 unsigned dim
, lp_dim
;
279 dim
= 1 + isl_set_n_dim(set
);
282 for (i
= 0; i
< set
->n
; ++i
) {
283 n_eq
+= set
->p
[i
]->n_eq
;
284 n_ineq
+= set
->p
[i
]->n_ineq
;
286 lp
= isl_basic_set_alloc(set
->ctx
, 0, dim
* set
->n
, 0, n_eq
, n_ineq
);
287 lp
= isl_basic_set_set_rational(lp
);
290 lp_dim
= isl_basic_set_n_dim(lp
);
291 k
= isl_basic_set_alloc_equality(lp
);
292 isl_int_set_si(lp
->eq
[k
][0], -1);
293 for (i
= 0; i
< set
->n
; ++i
) {
294 isl_int_set_si(lp
->eq
[k
][1+dim
*i
], 0);
295 isl_int_set_si(lp
->eq
[k
][1+dim
*i
+1], 1);
296 isl_seq_clr(lp
->eq
[k
]+1+dim
*i
+2, dim
-2);
298 for (i
= 0; i
< set
->n
; ++i
) {
299 k
= isl_basic_set_alloc_inequality(lp
);
300 isl_seq_clr(lp
->ineq
[k
], 1+lp_dim
);
301 isl_int_set_si(lp
->ineq
[k
][1+dim
*i
], 1);
303 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
304 k
= isl_basic_set_alloc_equality(lp
);
305 isl_seq_clr(lp
->eq
[k
], 1+dim
*i
);
306 isl_seq_cpy(lp
->eq
[k
]+1+dim
*i
, set
->p
[i
]->eq
[j
], dim
);
307 isl_seq_clr(lp
->eq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
310 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
) {
311 k
= isl_basic_set_alloc_inequality(lp
);
312 isl_seq_clr(lp
->ineq
[k
], 1+dim
*i
);
313 isl_seq_cpy(lp
->ineq
[k
]+1+dim
*i
, set
->p
[i
]->ineq
[j
], dim
);
314 isl_seq_clr(lp
->ineq
[k
]+1+dim
*(i
+1), dim
*(set
->n
-i
-1));
320 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
321 * of that facet, compute the other facet of the convex hull that contains
324 * We first transform the set such that the facet constraint becomes
328 * I.e., the facet lies in
332 * and on that facet, the constraint that defines the ridge is
336 * (This transformation is not strictly needed, all that is needed is
337 * that the ridge contains the origin.)
339 * Since the ridge contains the origin, the cone of the convex hull
340 * will be of the form
345 * with this second constraint defining the new facet.
346 * The constant a is obtained by settting x_1 in the cone of the
347 * convex hull to 1 and minimizing x_2.
348 * Now, each element in the cone of the convex hull is the sum
349 * of elements in the cones of the basic sets.
350 * If a_i is the dilation factor of basic set i, then the problem
351 * we need to solve is
364 * the constraints of each (transformed) basic set.
365 * If a = n/d, then the constraint defining the new facet (in the transformed
368 * -n x_1 + d x_2 >= 0
370 * In the original space, we need to take the same combination of the
371 * corresponding constraints "facet" and "ridge".
373 * If a = -infty = "-1/0", then we just return the original facet constraint.
374 * This means that the facet is unbounded, but has a bounded intersection
375 * with the union of sets.
377 isl_int
*isl_set_wrap_facet(__isl_keep isl_set
*set
,
378 isl_int
*facet
, isl_int
*ridge
)
382 struct isl_mat
*T
= NULL
;
383 struct isl_basic_set
*lp
= NULL
;
385 enum isl_lp_result res
;
392 set
= isl_set_copy(set
);
393 set
= isl_set_set_rational(set
);
395 dim
= 1 + isl_set_n_dim(set
);
396 T
= isl_mat_alloc(ctx
, 3, dim
);
399 isl_int_set_si(T
->row
[0][0], 1);
400 isl_seq_clr(T
->row
[0]+1, dim
- 1);
401 isl_seq_cpy(T
->row
[1], facet
, dim
);
402 isl_seq_cpy(T
->row
[2], ridge
, dim
);
403 T
= isl_mat_right_inverse(T
);
404 set
= isl_set_preimage(set
, T
);
408 lp
= wrap_constraints(set
);
409 obj
= isl_vec_alloc(ctx
, 1 + dim
*set
->n
);
412 isl_int_set_si(obj
->block
.data
[0], 0);
413 for (i
= 0; i
< set
->n
; ++i
) {
414 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
, 2);
415 isl_int_set_si(obj
->block
.data
[1 + dim
*i
+2], 1);
416 isl_seq_clr(obj
->block
.data
+ 1 + dim
*i
+3, dim
-3);
420 res
= isl_basic_set_solve_lp(lp
, 0,
421 obj
->block
.data
, ctx
->one
, &num
, &den
, NULL
);
422 if (res
== isl_lp_ok
) {
423 isl_int_neg(num
, num
);
424 isl_seq_combine(facet
, num
, facet
, den
, ridge
, dim
);
425 isl_seq_normalize(ctx
, facet
, dim
);
430 isl_basic_set_free(lp
);
432 if (res
== isl_lp_error
)
434 isl_assert(ctx
, res
== isl_lp_ok
|| res
== isl_lp_unbounded
,
438 isl_basic_set_free(lp
);
444 /* Compute the constraint of a facet of "set".
446 * We first compute the intersection with a bounding constraint
447 * that is orthogonal to one of the coordinate axes.
448 * If the affine hull of this intersection has only one equality,
449 * we have found a facet.
450 * Otherwise, we wrap the current bounding constraint around
451 * one of the equalities of the face (one that is not equal to
452 * the current bounding constraint).
453 * This process continues until we have found a facet.
454 * The dimension of the intersection increases by at least
455 * one on each iteration, so termination is guaranteed.
457 static __isl_give isl_mat
*initial_facet_constraint(__isl_keep isl_set
*set
)
459 struct isl_set
*slice
= NULL
;
460 struct isl_basic_set
*face
= NULL
;
462 unsigned dim
= isl_set_n_dim(set
);
464 isl_mat
*bounds
= NULL
;
466 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
467 bounds
= isl_mat_alloc(set
->ctx
, 1, 1 + dim
);
471 isl_seq_clr(bounds
->row
[0], dim
);
472 isl_int_set_si(bounds
->row
[0][1 + dim
- 1], 1);
473 is_bound
= uset_is_bound(set
, bounds
->row
[0], 1 + dim
);
476 isl_assert(set
->ctx
, is_bound
, goto error
);
477 isl_seq_normalize(set
->ctx
, bounds
->row
[0], 1 + dim
);
481 slice
= isl_set_copy(set
);
482 slice
= isl_set_add_basic_set_equality(slice
, bounds
->row
[0]);
483 face
= isl_set_affine_hull(slice
);
486 if (face
->n_eq
== 1) {
487 isl_basic_set_free(face
);
490 for (i
= 0; i
< face
->n_eq
; ++i
)
491 if (!isl_seq_eq(bounds
->row
[0], face
->eq
[i
], 1 + dim
) &&
492 !isl_seq_is_neg(bounds
->row
[0],
493 face
->eq
[i
], 1 + dim
))
495 isl_assert(set
->ctx
, i
< face
->n_eq
, goto error
);
496 if (!isl_set_wrap_facet(set
, bounds
->row
[0], face
->eq
[i
]))
498 isl_seq_normalize(set
->ctx
, bounds
->row
[0], bounds
->n_col
);
499 isl_basic_set_free(face
);
504 isl_basic_set_free(face
);
505 isl_mat_free(bounds
);
509 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
510 * compute a hyperplane description of the facet, i.e., compute the facets
513 * We compute an affine transformation that transforms the constraint
522 * by computing the right inverse U of a matrix that starts with the rows
535 * Since z_1 is zero, we can drop this variable as well as the corresponding
536 * column of U to obtain
544 * with Q' equal to Q, but without the corresponding row.
545 * After computing the facets of the facet in the z' space,
546 * we convert them back to the x space through Q.
548 static struct isl_basic_set
*compute_facet(struct isl_set
*set
, isl_int
*c
)
550 struct isl_mat
*m
, *U
, *Q
;
551 struct isl_basic_set
*facet
= NULL
;
556 set
= isl_set_copy(set
);
557 dim
= isl_set_n_dim(set
);
558 m
= isl_mat_alloc(set
->ctx
, 2, 1 + dim
);
561 isl_int_set_si(m
->row
[0][0], 1);
562 isl_seq_clr(m
->row
[0]+1, dim
);
563 isl_seq_cpy(m
->row
[1], c
, 1+dim
);
564 U
= isl_mat_right_inverse(m
);
565 Q
= isl_mat_right_inverse(isl_mat_copy(U
));
566 U
= isl_mat_drop_cols(U
, 1, 1);
567 Q
= isl_mat_drop_rows(Q
, 1, 1);
568 set
= isl_set_preimage(set
, U
);
569 facet
= uset_convex_hull_wrap_bounded(set
);
570 facet
= isl_basic_set_preimage(facet
, Q
);
571 if (facet
&& facet
->n_eq
!= 0)
572 isl_die(ctx
, isl_error_internal
, "unexpected equality",
573 return isl_basic_set_free(facet
));
576 isl_basic_set_free(facet
);
581 /* Given an initial facet constraint, compute the remaining facets.
582 * We do this by running through all facets found so far and computing
583 * the adjacent facets through wrapping, adding those facets that we
584 * hadn't already found before.
586 * For each facet we have found so far, we first compute its facets
587 * in the resulting convex hull. That is, we compute the ridges
588 * of the resulting convex hull contained in the facet.
589 * We also compute the corresponding facet in the current approximation
590 * of the convex hull. There is no need to wrap around the ridges
591 * in this facet since that would result in a facet that is already
592 * present in the current approximation.
594 * This function can still be significantly optimized by checking which of
595 * the facets of the basic sets are also facets of the convex hull and
596 * using all the facets so far to help in constructing the facets of the
599 * using the technique in section "3.1 Ridge Generation" of
600 * "Extended Convex Hull" by Fukuda et al.
602 static struct isl_basic_set
*extend(struct isl_basic_set
*hull
,
607 struct isl_basic_set
*facet
= NULL
;
608 struct isl_basic_set
*hull_facet
= NULL
;
614 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
616 dim
= isl_set_n_dim(set
);
618 for (i
= 0; i
< hull
->n_ineq
; ++i
) {
619 facet
= compute_facet(set
, hull
->ineq
[i
]);
620 facet
= isl_basic_set_add_equality(facet
, hull
->ineq
[i
]);
621 facet
= isl_basic_set_gauss(facet
, NULL
);
622 facet
= isl_basic_set_normalize_constraints(facet
);
623 hull_facet
= isl_basic_set_copy(hull
);
624 hull_facet
= isl_basic_set_add_equality(hull_facet
, hull
->ineq
[i
]);
625 hull_facet
= isl_basic_set_gauss(hull_facet
, NULL
);
626 hull_facet
= isl_basic_set_normalize_constraints(hull_facet
);
627 if (!facet
|| !hull_facet
)
629 hull
= isl_basic_set_cow(hull
);
630 hull
= isl_basic_set_extend_space(hull
,
631 isl_space_copy(hull
->dim
), 0, 0, facet
->n_ineq
);
634 for (j
= 0; j
< facet
->n_ineq
; ++j
) {
635 for (f
= 0; f
< hull_facet
->n_ineq
; ++f
)
636 if (isl_seq_eq(facet
->ineq
[j
],
637 hull_facet
->ineq
[f
], 1 + dim
))
639 if (f
< hull_facet
->n_ineq
)
641 k
= isl_basic_set_alloc_inequality(hull
);
644 isl_seq_cpy(hull
->ineq
[k
], hull
->ineq
[i
], 1+dim
);
645 if (!isl_set_wrap_facet(set
, hull
->ineq
[k
], facet
->ineq
[j
]))
648 isl_basic_set_free(hull_facet
);
649 isl_basic_set_free(facet
);
651 hull
= isl_basic_set_simplify(hull
);
652 hull
= isl_basic_set_finalize(hull
);
655 isl_basic_set_free(hull_facet
);
656 isl_basic_set_free(facet
);
657 isl_basic_set_free(hull
);
661 /* Special case for computing the convex hull of a one dimensional set.
662 * We simply collect the lower and upper bounds of each basic set
663 * and the biggest of those.
665 static struct isl_basic_set
*convex_hull_1d(struct isl_set
*set
)
667 struct isl_mat
*c
= NULL
;
668 isl_int
*lower
= NULL
;
669 isl_int
*upper
= NULL
;
672 struct isl_basic_set
*hull
;
674 for (i
= 0; i
< set
->n
; ++i
) {
675 set
->p
[i
] = isl_basic_set_simplify(set
->p
[i
]);
679 set
= isl_set_remove_empty_parts(set
);
682 isl_assert(set
->ctx
, set
->n
> 0, goto error
);
683 c
= isl_mat_alloc(set
->ctx
, 2, 2);
687 if (set
->p
[0]->n_eq
> 0) {
688 isl_assert(set
->ctx
, set
->p
[0]->n_eq
== 1, goto error
);
691 if (isl_int_is_pos(set
->p
[0]->eq
[0][1])) {
692 isl_seq_cpy(lower
, set
->p
[0]->eq
[0], 2);
693 isl_seq_neg(upper
, set
->p
[0]->eq
[0], 2);
695 isl_seq_neg(lower
, set
->p
[0]->eq
[0], 2);
696 isl_seq_cpy(upper
, set
->p
[0]->eq
[0], 2);
699 for (j
= 0; j
< set
->p
[0]->n_ineq
; ++j
) {
700 if (isl_int_is_pos(set
->p
[0]->ineq
[j
][1])) {
702 isl_seq_cpy(lower
, set
->p
[0]->ineq
[j
], 2);
705 isl_seq_cpy(upper
, set
->p
[0]->ineq
[j
], 2);
712 for (i
= 0; i
< set
->n
; ++i
) {
713 struct isl_basic_set
*bset
= set
->p
[i
];
717 for (j
= 0; j
< bset
->n_eq
; ++j
) {
721 isl_int_mul(a
, lower
[0], bset
->eq
[j
][1]);
722 isl_int_mul(b
, lower
[1], bset
->eq
[j
][0]);
723 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
724 isl_seq_cpy(lower
, bset
->eq
[j
], 2);
725 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
726 isl_seq_neg(lower
, bset
->eq
[j
], 2);
729 isl_int_mul(a
, upper
[0], bset
->eq
[j
][1]);
730 isl_int_mul(b
, upper
[1], bset
->eq
[j
][0]);
731 if (isl_int_lt(a
, b
) && isl_int_is_pos(bset
->eq
[j
][1]))
732 isl_seq_neg(upper
, bset
->eq
[j
], 2);
733 if (isl_int_gt(a
, b
) && isl_int_is_neg(bset
->eq
[j
][1]))
734 isl_seq_cpy(upper
, bset
->eq
[j
], 2);
737 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
738 if (isl_int_is_pos(bset
->ineq
[j
][1]))
740 if (isl_int_is_neg(bset
->ineq
[j
][1]))
742 if (lower
&& isl_int_is_pos(bset
->ineq
[j
][1])) {
743 isl_int_mul(a
, lower
[0], bset
->ineq
[j
][1]);
744 isl_int_mul(b
, lower
[1], bset
->ineq
[j
][0]);
745 if (isl_int_lt(a
, b
))
746 isl_seq_cpy(lower
, bset
->ineq
[j
], 2);
748 if (upper
&& isl_int_is_neg(bset
->ineq
[j
][1])) {
749 isl_int_mul(a
, upper
[0], bset
->ineq
[j
][1]);
750 isl_int_mul(b
, upper
[1], bset
->ineq
[j
][0]);
751 if (isl_int_gt(a
, b
))
752 isl_seq_cpy(upper
, bset
->ineq
[j
], 2);
763 hull
= isl_basic_set_alloc(set
->ctx
, 0, 1, 0, 0, 2);
764 hull
= isl_basic_set_set_rational(hull
);
768 k
= isl_basic_set_alloc_inequality(hull
);
769 isl_seq_cpy(hull
->ineq
[k
], lower
, 2);
772 k
= isl_basic_set_alloc_inequality(hull
);
773 isl_seq_cpy(hull
->ineq
[k
], upper
, 2);
775 hull
= isl_basic_set_finalize(hull
);
785 static struct isl_basic_set
*convex_hull_0d(struct isl_set
*set
)
787 struct isl_basic_set
*convex_hull
;
792 if (isl_set_is_empty(set
))
793 convex_hull
= isl_basic_set_empty(isl_space_copy(set
->dim
));
795 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
800 /* Compute the convex hull of a pair of basic sets without any parameters or
801 * integer divisions using Fourier-Motzkin elimination.
802 * The convex hull is the set of all points that can be written as
803 * the sum of points from both basic sets (in homogeneous coordinates).
804 * We set up the constraints in a space with dimensions for each of
805 * the three sets and then project out the dimensions corresponding
806 * to the two original basic sets, retaining only those corresponding
807 * to the convex hull.
809 static struct isl_basic_set
*convex_hull_pair_elim(struct isl_basic_set
*bset1
,
810 struct isl_basic_set
*bset2
)
813 struct isl_basic_set
*bset
[2];
814 struct isl_basic_set
*hull
= NULL
;
817 if (!bset1
|| !bset2
)
820 dim
= isl_basic_set_n_dim(bset1
);
821 hull
= isl_basic_set_alloc(bset1
->ctx
, 0, 2 + 3 * dim
, 0,
822 1 + dim
+ bset1
->n_eq
+ bset2
->n_eq
,
823 2 + bset1
->n_ineq
+ bset2
->n_ineq
);
826 for (i
= 0; i
< 2; ++i
) {
827 for (j
= 0; j
< bset
[i
]->n_eq
; ++j
) {
828 k
= isl_basic_set_alloc_equality(hull
);
831 isl_seq_clr(hull
->eq
[k
], (i
+1) * (1+dim
));
832 isl_seq_clr(hull
->eq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
833 isl_seq_cpy(hull
->eq
[k
]+(i
+1)*(1+dim
), bset
[i
]->eq
[j
],
836 for (j
= 0; j
< bset
[i
]->n_ineq
; ++j
) {
837 k
= isl_basic_set_alloc_inequality(hull
);
840 isl_seq_clr(hull
->ineq
[k
], (i
+1) * (1+dim
));
841 isl_seq_clr(hull
->ineq
[k
]+(i
+2)*(1+dim
), (1-i
)*(1+dim
));
842 isl_seq_cpy(hull
->ineq
[k
]+(i
+1)*(1+dim
),
843 bset
[i
]->ineq
[j
], 1+dim
);
845 k
= isl_basic_set_alloc_inequality(hull
);
848 isl_seq_clr(hull
->ineq
[k
], 1+2+3*dim
);
849 isl_int_set_si(hull
->ineq
[k
][(i
+1)*(1+dim
)], 1);
851 for (j
= 0; j
< 1+dim
; ++j
) {
852 k
= isl_basic_set_alloc_equality(hull
);
855 isl_seq_clr(hull
->eq
[k
], 1+2+3*dim
);
856 isl_int_set_si(hull
->eq
[k
][j
], -1);
857 isl_int_set_si(hull
->eq
[k
][1+dim
+j
], 1);
858 isl_int_set_si(hull
->eq
[k
][2*(1+dim
)+j
], 1);
860 hull
= isl_basic_set_set_rational(hull
);
861 hull
= isl_basic_set_remove_dims(hull
, isl_dim_set
, dim
, 2*(1+dim
));
862 hull
= isl_basic_set_remove_redundancies(hull
);
863 isl_basic_set_free(bset1
);
864 isl_basic_set_free(bset2
);
867 isl_basic_set_free(bset1
);
868 isl_basic_set_free(bset2
);
869 isl_basic_set_free(hull
);
873 /* Is the set bounded for each value of the parameters?
875 isl_bool
isl_basic_set_is_bounded(__isl_keep isl_basic_set
*bset
)
881 return isl_bool_error
;
882 if (isl_basic_set_plain_is_empty(bset
))
883 return isl_bool_true
;
885 tab
= isl_tab_from_recession_cone(bset
, 1);
886 bounded
= isl_tab_cone_is_bounded(tab
);
891 /* Is the image bounded for each value of the parameters and
892 * the domain variables?
894 isl_bool
isl_basic_map_image_is_bounded(__isl_keep isl_basic_map
*bmap
)
896 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
897 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
900 bmap
= isl_basic_map_copy(bmap
);
901 bmap
= isl_basic_map_cow(bmap
);
902 bmap
= isl_basic_map_move_dims(bmap
, isl_dim_param
, nparam
,
903 isl_dim_in
, 0, n_in
);
904 bounded
= isl_basic_set_is_bounded(bset_from_bmap(bmap
));
905 isl_basic_map_free(bmap
);
910 /* Is the set bounded for each value of the parameters?
912 isl_bool
isl_set_is_bounded(__isl_keep isl_set
*set
)
917 return isl_bool_error
;
919 for (i
= 0; i
< set
->n
; ++i
) {
920 isl_bool bounded
= isl_basic_set_is_bounded(set
->p
[i
]);
921 if (!bounded
|| bounded
< 0)
924 return isl_bool_true
;
927 /* Compute the lineality space of the convex hull of bset1 and bset2.
929 * We first compute the intersection of the recession cone of bset1
930 * with the negative of the recession cone of bset2 and then compute
931 * the linear hull of the resulting cone.
933 static struct isl_basic_set
*induced_lineality_space(
934 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
937 struct isl_basic_set
*lin
= NULL
;
940 if (!bset1
|| !bset2
)
943 dim
= isl_basic_set_total_dim(bset1
);
944 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset1
), 0,
945 bset1
->n_eq
+ bset2
->n_eq
,
946 bset1
->n_ineq
+ bset2
->n_ineq
);
947 lin
= isl_basic_set_set_rational(lin
);
950 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
951 k
= isl_basic_set_alloc_equality(lin
);
954 isl_int_set_si(lin
->eq
[k
][0], 0);
955 isl_seq_cpy(lin
->eq
[k
] + 1, bset1
->eq
[i
] + 1, dim
);
957 for (i
= 0; i
< bset1
->n_ineq
; ++i
) {
958 k
= isl_basic_set_alloc_inequality(lin
);
961 isl_int_set_si(lin
->ineq
[k
][0], 0);
962 isl_seq_cpy(lin
->ineq
[k
] + 1, bset1
->ineq
[i
] + 1, dim
);
964 for (i
= 0; i
< bset2
->n_eq
; ++i
) {
965 k
= isl_basic_set_alloc_equality(lin
);
968 isl_int_set_si(lin
->eq
[k
][0], 0);
969 isl_seq_neg(lin
->eq
[k
] + 1, bset2
->eq
[i
] + 1, dim
);
971 for (i
= 0; i
< bset2
->n_ineq
; ++i
) {
972 k
= isl_basic_set_alloc_inequality(lin
);
975 isl_int_set_si(lin
->ineq
[k
][0], 0);
976 isl_seq_neg(lin
->ineq
[k
] + 1, bset2
->ineq
[i
] + 1, dim
);
979 isl_basic_set_free(bset1
);
980 isl_basic_set_free(bset2
);
981 return isl_basic_set_affine_hull(lin
);
983 isl_basic_set_free(lin
);
984 isl_basic_set_free(bset1
);
985 isl_basic_set_free(bset2
);
989 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
);
991 /* Given a set and a linear space "lin" of dimension n > 0,
992 * project the linear space from the set, compute the convex hull
993 * and then map the set back to the original space.
999 * describe the linear space. We first compute the Hermite normal
1000 * form H = M U of M = H Q, to obtain
1004 * The last n rows of H will be zero, so the last n variables of x' = Q x
1005 * are the one we want to project out. We do this by transforming each
1006 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1007 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1008 * we transform the hull back to the original space as A' Q_1 x >= b',
1009 * with Q_1 all but the last n rows of Q.
1011 static struct isl_basic_set
*modulo_lineality(struct isl_set
*set
,
1012 struct isl_basic_set
*lin
)
1014 unsigned total
= isl_basic_set_total_dim(lin
);
1016 struct isl_basic_set
*hull
;
1017 struct isl_mat
*M
, *U
, *Q
;
1021 lin_dim
= total
- lin
->n_eq
;
1022 M
= isl_mat_sub_alloc6(set
->ctx
, lin
->eq
, 0, lin
->n_eq
, 1, total
);
1023 M
= isl_mat_left_hermite(M
, 0, &U
, &Q
);
1027 isl_basic_set_free(lin
);
1029 Q
= isl_mat_drop_rows(Q
, Q
->n_row
- lin_dim
, lin_dim
);
1031 U
= isl_mat_lin_to_aff(U
);
1032 Q
= isl_mat_lin_to_aff(Q
);
1034 set
= isl_set_preimage(set
, U
);
1035 set
= isl_set_remove_dims(set
, isl_dim_set
, total
- lin_dim
, lin_dim
);
1036 hull
= uset_convex_hull(set
);
1037 hull
= isl_basic_set_preimage(hull
, Q
);
1041 isl_basic_set_free(lin
);
1046 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1047 * set up an LP for solving
1049 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1051 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1052 * The next \alpha{ij} correspond to the equalities and come in pairs.
1053 * The final \alpha{ij} correspond to the inequalities.
1055 static struct isl_basic_set
*valid_direction_lp(
1056 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1059 struct isl_basic_set
*lp
;
1064 if (!bset1
|| !bset2
)
1066 d
= 1 + isl_basic_set_total_dim(bset1
);
1068 2 * bset1
->n_eq
+ bset1
->n_ineq
+ 2 * bset2
->n_eq
+ bset2
->n_ineq
;
1069 dim
= isl_space_set_alloc(bset1
->ctx
, 0, n
);
1070 lp
= isl_basic_set_alloc_space(dim
, 0, d
, n
);
1073 for (i
= 0; i
< n
; ++i
) {
1074 k
= isl_basic_set_alloc_inequality(lp
);
1077 isl_seq_clr(lp
->ineq
[k
] + 1, n
);
1078 isl_int_set_si(lp
->ineq
[k
][0], -1);
1079 isl_int_set_si(lp
->ineq
[k
][1 + i
], 1);
1081 for (i
= 0; i
< d
; ++i
) {
1082 k
= isl_basic_set_alloc_equality(lp
);
1086 isl_int_set_si(lp
->eq
[k
][n
], 0); n
++;
1087 /* positivity constraint 1 >= 0 */
1088 isl_int_set_si(lp
->eq
[k
][n
], i
== 0); n
++;
1089 for (j
= 0; j
< bset1
->n_eq
; ++j
) {
1090 isl_int_set(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1091 isl_int_neg(lp
->eq
[k
][n
], bset1
->eq
[j
][i
]); n
++;
1093 for (j
= 0; j
< bset1
->n_ineq
; ++j
) {
1094 isl_int_set(lp
->eq
[k
][n
], bset1
->ineq
[j
][i
]); n
++;
1096 /* positivity constraint 1 >= 0 */
1097 isl_int_set_si(lp
->eq
[k
][n
], -(i
== 0)); n
++;
1098 for (j
= 0; j
< bset2
->n_eq
; ++j
) {
1099 isl_int_neg(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1100 isl_int_set(lp
->eq
[k
][n
], bset2
->eq
[j
][i
]); n
++;
1102 for (j
= 0; j
< bset2
->n_ineq
; ++j
) {
1103 isl_int_neg(lp
->eq
[k
][n
], bset2
->ineq
[j
][i
]); n
++;
1106 lp
= isl_basic_set_gauss(lp
, NULL
);
1107 isl_basic_set_free(bset1
);
1108 isl_basic_set_free(bset2
);
1111 isl_basic_set_free(bset1
);
1112 isl_basic_set_free(bset2
);
1116 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1117 * for all rays in the homogeneous space of the two cones that correspond
1118 * to the input polyhedra bset1 and bset2.
1120 * We compute s as a vector that satisfies
1122 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1124 * with h_{ij} the normals of the facets of polyhedron i
1125 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1126 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1127 * We first set up an LP with as variables the \alpha{ij}.
1128 * In this formulation, for each polyhedron i,
1129 * the first constraint is the positivity constraint, followed by pairs
1130 * of variables for the equalities, followed by variables for the inequalities.
1131 * We then simply pick a feasible solution and compute s using (*).
1133 * Note that we simply pick any valid direction and make no attempt
1134 * to pick a "good" or even the "best" valid direction.
1136 static struct isl_vec
*valid_direction(
1137 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1139 struct isl_basic_set
*lp
;
1140 struct isl_tab
*tab
;
1141 struct isl_vec
*sample
= NULL
;
1142 struct isl_vec
*dir
;
1147 if (!bset1
|| !bset2
)
1149 lp
= valid_direction_lp(isl_basic_set_copy(bset1
),
1150 isl_basic_set_copy(bset2
));
1151 tab
= isl_tab_from_basic_set(lp
, 0);
1152 sample
= isl_tab_get_sample_value(tab
);
1154 isl_basic_set_free(lp
);
1157 d
= isl_basic_set_total_dim(bset1
);
1158 dir
= isl_vec_alloc(bset1
->ctx
, 1 + d
);
1161 isl_seq_clr(dir
->block
.data
+ 1, dir
->size
- 1);
1163 /* positivity constraint 1 >= 0 */
1164 isl_int_set(dir
->block
.data
[0], sample
->block
.data
[n
]); n
++;
1165 for (i
= 0; i
< bset1
->n_eq
; ++i
) {
1166 isl_int_sub(sample
->block
.data
[n
],
1167 sample
->block
.data
[n
], sample
->block
.data
[n
+1]);
1168 isl_seq_combine(dir
->block
.data
,
1169 bset1
->ctx
->one
, dir
->block
.data
,
1170 sample
->block
.data
[n
], bset1
->eq
[i
], 1 + d
);
1174 for (i
= 0; i
< bset1
->n_ineq
; ++i
)
1175 isl_seq_combine(dir
->block
.data
,
1176 bset1
->ctx
->one
, dir
->block
.data
,
1177 sample
->block
.data
[n
++], bset1
->ineq
[i
], 1 + d
);
1178 isl_vec_free(sample
);
1179 isl_seq_normalize(bset1
->ctx
, dir
->el
, dir
->size
);
1180 isl_basic_set_free(bset1
);
1181 isl_basic_set_free(bset2
);
1184 isl_vec_free(sample
);
1185 isl_basic_set_free(bset1
);
1186 isl_basic_set_free(bset2
);
1190 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1191 * compute b_i' + A_i' x' >= 0, with
1193 * [ b_i A_i ] [ y' ] [ y' ]
1194 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1196 * In particular, add the "positivity constraint" and then perform
1199 static struct isl_basic_set
*homogeneous_map(struct isl_basic_set
*bset
,
1206 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1207 k
= isl_basic_set_alloc_inequality(bset
);
1210 isl_seq_clr(bset
->ineq
[k
] + 1, isl_basic_set_total_dim(bset
));
1211 isl_int_set_si(bset
->ineq
[k
][0], 1);
1212 bset
= isl_basic_set_preimage(bset
, T
);
1216 isl_basic_set_free(bset
);
1220 /* Compute the convex hull of a pair of basic sets without any parameters or
1221 * integer divisions, where the convex hull is known to be pointed,
1222 * but the basic sets may be unbounded.
1224 * We turn this problem into the computation of a convex hull of a pair
1225 * _bounded_ polyhedra by "changing the direction of the homogeneous
1226 * dimension". This idea is due to Matthias Koeppe.
1228 * Consider the cones in homogeneous space that correspond to the
1229 * input polyhedra. The rays of these cones are also rays of the
1230 * polyhedra if the coordinate that corresponds to the homogeneous
1231 * dimension is zero. That is, if the inner product of the rays
1232 * with the homogeneous direction is zero.
1233 * The cones in the homogeneous space can also be considered to
1234 * correspond to other pairs of polyhedra by chosing a different
1235 * homogeneous direction. To ensure that both of these polyhedra
1236 * are bounded, we need to make sure that all rays of the cones
1237 * correspond to vertices and not to rays.
1238 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1239 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1240 * The vector s is computed in valid_direction.
1242 * Note that we need to consider _all_ rays of the cones and not just
1243 * the rays that correspond to rays in the polyhedra. If we were to
1244 * only consider those rays and turn them into vertices, then we
1245 * may inadvertently turn some vertices into rays.
1247 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1248 * We therefore transform the two polyhedra such that the selected
1249 * direction is mapped onto this standard direction and then proceed
1250 * with the normal computation.
1251 * Let S be a non-singular square matrix with s as its first row,
1252 * then we want to map the polyhedra to the space
1254 * [ y' ] [ y ] [ y ] [ y' ]
1255 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1257 * We take S to be the unimodular completion of s to limit the growth
1258 * of the coefficients in the following computations.
1260 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1261 * We first move to the homogeneous dimension
1263 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1264 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1266 * Then we change directoin
1268 * [ b_i A_i ] [ y' ] [ y' ]
1269 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1271 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1272 * resulting in b' + A' x' >= 0, which we then convert back
1275 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1277 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1279 static struct isl_basic_set
*convex_hull_pair_pointed(
1280 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1282 struct isl_ctx
*ctx
= NULL
;
1283 struct isl_vec
*dir
= NULL
;
1284 struct isl_mat
*T
= NULL
;
1285 struct isl_mat
*T2
= NULL
;
1286 struct isl_basic_set
*hull
;
1287 struct isl_set
*set
;
1289 if (!bset1
|| !bset2
)
1291 ctx
= isl_basic_set_get_ctx(bset1
);
1292 dir
= valid_direction(isl_basic_set_copy(bset1
),
1293 isl_basic_set_copy(bset2
));
1296 T
= isl_mat_alloc(ctx
, dir
->size
, dir
->size
);
1299 isl_seq_cpy(T
->row
[0], dir
->block
.data
, dir
->size
);
1300 T
= isl_mat_unimodular_complete(T
, 1);
1301 T2
= isl_mat_right_inverse(isl_mat_copy(T
));
1303 bset1
= homogeneous_map(bset1
, isl_mat_copy(T2
));
1304 bset2
= homogeneous_map(bset2
, T2
);
1305 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1306 set
= isl_set_add_basic_set(set
, bset1
);
1307 set
= isl_set_add_basic_set(set
, bset2
);
1308 hull
= uset_convex_hull(set
);
1309 hull
= isl_basic_set_preimage(hull
, T
);
1316 isl_basic_set_free(bset1
);
1317 isl_basic_set_free(bset2
);
1321 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
);
1322 static struct isl_basic_set
*modulo_affine_hull(
1323 struct isl_set
*set
, struct isl_basic_set
*affine_hull
);
1325 /* Compute the convex hull of a pair of basic sets without any parameters or
1326 * integer divisions.
1328 * This function is called from uset_convex_hull_unbounded, which
1329 * means that the complete convex hull is unbounded. Some pairs
1330 * of basic sets may still be bounded, though.
1331 * They may even lie inside a lower dimensional space, in which
1332 * case they need to be handled inside their affine hull since
1333 * the main algorithm assumes that the result is full-dimensional.
1335 * If the convex hull of the two basic sets would have a non-trivial
1336 * lineality space, we first project out this lineality space.
1338 static struct isl_basic_set
*convex_hull_pair(struct isl_basic_set
*bset1
,
1339 struct isl_basic_set
*bset2
)
1341 isl_basic_set
*lin
, *aff
;
1342 int bounded1
, bounded2
;
1344 if (bset1
->ctx
->opt
->convex
== ISL_CONVEX_HULL_FM
)
1345 return convex_hull_pair_elim(bset1
, bset2
);
1347 aff
= isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1
),
1348 isl_basic_set_copy(bset2
)));
1352 return modulo_affine_hull(isl_basic_set_union(bset1
, bset2
), aff
);
1353 isl_basic_set_free(aff
);
1355 bounded1
= isl_basic_set_is_bounded(bset1
);
1356 bounded2
= isl_basic_set_is_bounded(bset2
);
1358 if (bounded1
< 0 || bounded2
< 0)
1361 if (bounded1
&& bounded2
)
1362 return uset_convex_hull_wrap(isl_basic_set_union(bset1
, bset2
));
1364 if (bounded1
|| bounded2
)
1365 return convex_hull_pair_pointed(bset1
, bset2
);
1367 lin
= induced_lineality_space(isl_basic_set_copy(bset1
),
1368 isl_basic_set_copy(bset2
));
1371 if (isl_basic_set_plain_is_universe(lin
)) {
1372 isl_basic_set_free(bset1
);
1373 isl_basic_set_free(bset2
);
1376 if (lin
->n_eq
< isl_basic_set_total_dim(lin
)) {
1377 struct isl_set
*set
;
1378 set
= isl_set_alloc_space(isl_basic_set_get_space(bset1
), 2, 0);
1379 set
= isl_set_add_basic_set(set
, bset1
);
1380 set
= isl_set_add_basic_set(set
, bset2
);
1381 return modulo_lineality(set
, lin
);
1383 isl_basic_set_free(lin
);
1385 return convex_hull_pair_pointed(bset1
, bset2
);
1387 isl_basic_set_free(bset1
);
1388 isl_basic_set_free(bset2
);
1392 /* Compute the lineality space of a basic set.
1393 * We currently do not allow the basic set to have any divs.
1394 * We basically just drop the constants and turn every inequality
1397 struct isl_basic_set
*isl_basic_set_lineality_space(struct isl_basic_set
*bset
)
1400 struct isl_basic_set
*lin
= NULL
;
1405 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
1406 dim
= isl_basic_set_total_dim(bset
);
1408 lin
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset
), 0, dim
, 0);
1411 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1412 k
= isl_basic_set_alloc_equality(lin
);
1415 isl_int_set_si(lin
->eq
[k
][0], 0);
1416 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->eq
[i
] + 1, dim
);
1418 lin
= isl_basic_set_gauss(lin
, NULL
);
1421 for (i
= 0; i
< bset
->n_ineq
&& lin
->n_eq
< dim
; ++i
) {
1422 k
= isl_basic_set_alloc_equality(lin
);
1425 isl_int_set_si(lin
->eq
[k
][0], 0);
1426 isl_seq_cpy(lin
->eq
[k
] + 1, bset
->ineq
[i
] + 1, dim
);
1427 lin
= isl_basic_set_gauss(lin
, NULL
);
1431 isl_basic_set_free(bset
);
1434 isl_basic_set_free(lin
);
1435 isl_basic_set_free(bset
);
1439 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1440 * "underlying" set "set".
1442 static struct isl_basic_set
*uset_combined_lineality_space(struct isl_set
*set
)
1445 struct isl_set
*lin
= NULL
;
1450 isl_space
*dim
= isl_set_get_space(set
);
1452 return isl_basic_set_empty(dim
);
1455 lin
= isl_set_alloc_space(isl_set_get_space(set
), set
->n
, 0);
1456 for (i
= 0; i
< set
->n
; ++i
)
1457 lin
= isl_set_add_basic_set(lin
,
1458 isl_basic_set_lineality_space(isl_basic_set_copy(set
->p
[i
])));
1460 return isl_set_affine_hull(lin
);
1463 /* Compute the convex hull of a set without any parameters or
1464 * integer divisions.
1465 * In each step, we combined two basic sets until only one
1466 * basic set is left.
1467 * The input basic sets are assumed not to have a non-trivial
1468 * lineality space. If any of the intermediate results has
1469 * a non-trivial lineality space, it is projected out.
1471 static __isl_give isl_basic_set
*uset_convex_hull_unbounded(
1472 __isl_take isl_set
*set
)
1474 isl_basic_set_list
*list
;
1476 list
= isl_set_get_basic_set_list(set
);
1481 struct isl_basic_set
*t
;
1482 isl_basic_set
*bset1
, *bset2
;
1484 n
= isl_basic_set_list_n_basic_set(list
);
1486 isl_die(isl_basic_set_list_get_ctx(list
),
1488 "expecting at least two elements", goto error
);
1489 bset1
= isl_basic_set_list_get_basic_set(list
, n
- 1);
1490 bset2
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1491 bset1
= convex_hull_pair(bset1
, bset2
);
1493 isl_basic_set_list_free(list
);
1496 bset1
= isl_basic_set_underlying_set(bset1
);
1497 list
= isl_basic_set_list_drop(list
, n
- 2, 2);
1498 list
= isl_basic_set_list_add(list
, bset1
);
1500 t
= isl_basic_set_list_get_basic_set(list
, n
- 2);
1501 t
= isl_basic_set_lineality_space(t
);
1504 if (isl_basic_set_plain_is_universe(t
)) {
1505 isl_basic_set_list_free(list
);
1508 if (t
->n_eq
< isl_basic_set_total_dim(t
)) {
1509 set
= isl_basic_set_list_union(list
);
1510 return modulo_lineality(set
, t
);
1512 isl_basic_set_free(t
);
1517 isl_basic_set_list_free(list
);
1521 /* Compute an initial hull for wrapping containing a single initial
1523 * This function assumes that the given set is bounded.
1525 static struct isl_basic_set
*initial_hull(struct isl_basic_set
*hull
,
1526 struct isl_set
*set
)
1528 struct isl_mat
*bounds
= NULL
;
1534 bounds
= initial_facet_constraint(set
);
1537 k
= isl_basic_set_alloc_inequality(hull
);
1540 dim
= isl_set_n_dim(set
);
1541 isl_assert(set
->ctx
, 1 + dim
== bounds
->n_col
, goto error
);
1542 isl_seq_cpy(hull
->ineq
[k
], bounds
->row
[0], bounds
->n_col
);
1543 isl_mat_free(bounds
);
1547 isl_basic_set_free(hull
);
1548 isl_mat_free(bounds
);
1552 struct max_constraint
{
1558 static int max_constraint_equal(const void *entry
, const void *val
)
1560 struct max_constraint
*a
= (struct max_constraint
*)entry
;
1561 isl_int
*b
= (isl_int
*)val
;
1563 return isl_seq_eq(a
->c
->row
[0] + 1, b
, a
->c
->n_col
- 1);
1566 static void update_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1567 isl_int
*con
, unsigned len
, int n
, int ineq
)
1569 struct isl_hash_table_entry
*entry
;
1570 struct max_constraint
*c
;
1573 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1574 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1580 isl_hash_table_remove(ctx
, table
, entry
);
1584 if (isl_int_gt(c
->c
->row
[0][0], con
[0]))
1586 if (isl_int_eq(c
->c
->row
[0][0], con
[0])) {
1591 c
->c
= isl_mat_cow(c
->c
);
1592 isl_int_set(c
->c
->row
[0][0], con
[0]);
1596 /* Check whether the constraint hash table "table" constains the constraint
1599 static int has_constraint(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1600 isl_int
*con
, unsigned len
, int n
)
1602 struct isl_hash_table_entry
*entry
;
1603 struct max_constraint
*c
;
1606 c_hash
= isl_seq_get_hash(con
+ 1, len
);
1607 entry
= isl_hash_table_find(ctx
, table
, c_hash
, max_constraint_equal
,
1614 return isl_int_eq(c
->c
->row
[0][0], con
[0]);
1617 /* Check for inequality constraints of a basic set without equalities
1618 * such that the same or more stringent copies of the constraint appear
1619 * in all of the basic sets. Such constraints are necessarily facet
1620 * constraints of the convex hull.
1622 * If the resulting basic set is by chance identical to one of
1623 * the basic sets in "set", then we know that this basic set contains
1624 * all other basic sets and is therefore the convex hull of set.
1625 * In this case we set *is_hull to 1.
1627 static struct isl_basic_set
*common_constraints(struct isl_basic_set
*hull
,
1628 struct isl_set
*set
, int *is_hull
)
1631 int min_constraints
;
1633 struct max_constraint
*constraints
= NULL
;
1634 struct isl_hash_table
*table
= NULL
;
1639 for (i
= 0; i
< set
->n
; ++i
)
1640 if (set
->p
[i
]->n_eq
== 0)
1644 min_constraints
= set
->p
[i
]->n_ineq
;
1646 for (i
= best
+ 1; i
< set
->n
; ++i
) {
1647 if (set
->p
[i
]->n_eq
!= 0)
1649 if (set
->p
[i
]->n_ineq
>= min_constraints
)
1651 min_constraints
= set
->p
[i
]->n_ineq
;
1654 constraints
= isl_calloc_array(hull
->ctx
, struct max_constraint
,
1658 table
= isl_alloc_type(hull
->ctx
, struct isl_hash_table
);
1659 if (isl_hash_table_init(hull
->ctx
, table
, min_constraints
))
1662 total
= isl_space_dim(set
->dim
, isl_dim_all
);
1663 for (i
= 0; i
< set
->p
[best
]->n_ineq
; ++i
) {
1664 constraints
[i
].c
= isl_mat_sub_alloc6(hull
->ctx
,
1665 set
->p
[best
]->ineq
+ i
, 0, 1, 0, 1 + total
);
1666 if (!constraints
[i
].c
)
1668 constraints
[i
].ineq
= 1;
1670 for (i
= 0; i
< min_constraints
; ++i
) {
1671 struct isl_hash_table_entry
*entry
;
1673 c_hash
= isl_seq_get_hash(constraints
[i
].c
->row
[0] + 1, total
);
1674 entry
= isl_hash_table_find(hull
->ctx
, table
, c_hash
,
1675 max_constraint_equal
, constraints
[i
].c
->row
[0] + 1, 1);
1678 isl_assert(hull
->ctx
, !entry
->data
, goto error
);
1679 entry
->data
= &constraints
[i
];
1683 for (s
= 0; s
< set
->n
; ++s
) {
1687 for (i
= 0; i
< set
->p
[s
]->n_eq
; ++i
) {
1688 isl_int
*eq
= set
->p
[s
]->eq
[i
];
1689 for (j
= 0; j
< 2; ++j
) {
1690 isl_seq_neg(eq
, eq
, 1 + total
);
1691 update_constraint(hull
->ctx
, table
,
1695 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1696 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1697 update_constraint(hull
->ctx
, table
, ineq
, total
, n
,
1698 set
->p
[s
]->n_eq
== 0);
1703 for (i
= 0; i
< min_constraints
; ++i
) {
1704 if (constraints
[i
].count
< n
)
1706 if (!constraints
[i
].ineq
)
1708 j
= isl_basic_set_alloc_inequality(hull
);
1711 isl_seq_cpy(hull
->ineq
[j
], constraints
[i
].c
->row
[0], 1 + total
);
1714 for (s
= 0; s
< set
->n
; ++s
) {
1715 if (set
->p
[s
]->n_eq
)
1717 if (set
->p
[s
]->n_ineq
!= hull
->n_ineq
)
1719 for (i
= 0; i
< set
->p
[s
]->n_ineq
; ++i
) {
1720 isl_int
*ineq
= set
->p
[s
]->ineq
[i
];
1721 if (!has_constraint(hull
->ctx
, table
, ineq
, total
, n
))
1724 if (i
== set
->p
[s
]->n_ineq
)
1728 isl_hash_table_clear(table
);
1729 for (i
= 0; i
< min_constraints
; ++i
)
1730 isl_mat_free(constraints
[i
].c
);
1735 isl_hash_table_clear(table
);
1738 for (i
= 0; i
< min_constraints
; ++i
)
1739 isl_mat_free(constraints
[i
].c
);
1744 /* Create a template for the convex hull of "set" and fill it up
1745 * obvious facet constraints, if any. If the result happens to
1746 * be the convex hull of "set" then *is_hull is set to 1.
1748 static struct isl_basic_set
*proto_hull(struct isl_set
*set
, int *is_hull
)
1750 struct isl_basic_set
*hull
;
1755 for (i
= 0; i
< set
->n
; ++i
) {
1756 n_ineq
+= set
->p
[i
]->n_eq
;
1757 n_ineq
+= set
->p
[i
]->n_ineq
;
1759 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
1760 hull
= isl_basic_set_set_rational(hull
);
1763 return common_constraints(hull
, set
, is_hull
);
1766 static struct isl_basic_set
*uset_convex_hull_wrap(struct isl_set
*set
)
1768 struct isl_basic_set
*hull
;
1771 hull
= proto_hull(set
, &is_hull
);
1772 if (hull
&& !is_hull
) {
1773 if (hull
->n_ineq
== 0)
1774 hull
= initial_hull(hull
, set
);
1775 hull
= extend(hull
, set
);
1782 /* Compute the convex hull of a set without any parameters or
1783 * integer divisions. Depending on whether the set is bounded,
1784 * we pass control to the wrapping based convex hull or
1785 * the Fourier-Motzkin elimination based convex hull.
1786 * We also handle a few special cases before checking the boundedness.
1788 static struct isl_basic_set
*uset_convex_hull(struct isl_set
*set
)
1791 struct isl_basic_set
*convex_hull
= NULL
;
1792 struct isl_basic_set
*lin
;
1794 if (isl_set_n_dim(set
) == 0)
1795 return convex_hull_0d(set
);
1797 set
= isl_set_coalesce(set
);
1798 set
= isl_set_set_rational(set
);
1803 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1807 if (isl_set_n_dim(set
) == 1)
1808 return convex_hull_1d(set
);
1810 bounded
= isl_set_is_bounded(set
);
1813 if (bounded
&& set
->ctx
->opt
->convex
== ISL_CONVEX_HULL_WRAP
)
1814 return uset_convex_hull_wrap(set
);
1816 lin
= uset_combined_lineality_space(isl_set_copy(set
));
1819 if (isl_basic_set_plain_is_universe(lin
)) {
1823 if (lin
->n_eq
< isl_basic_set_total_dim(lin
))
1824 return modulo_lineality(set
, lin
);
1825 isl_basic_set_free(lin
);
1827 return uset_convex_hull_unbounded(set
);
1830 isl_basic_set_free(convex_hull
);
1834 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1835 * without parameters or divs and where the convex hull of set is
1836 * known to be full-dimensional.
1838 static struct isl_basic_set
*uset_convex_hull_wrap_bounded(struct isl_set
*set
)
1840 struct isl_basic_set
*convex_hull
= NULL
;
1845 if (isl_set_n_dim(set
) == 0) {
1846 convex_hull
= isl_basic_set_universe(isl_space_copy(set
->dim
));
1848 convex_hull
= isl_basic_set_set_rational(convex_hull
);
1852 set
= isl_set_set_rational(set
);
1853 set
= isl_set_coalesce(set
);
1857 convex_hull
= isl_basic_set_copy(set
->p
[0]);
1859 convex_hull
= isl_basic_map_remove_redundancies(convex_hull
);
1862 if (isl_set_n_dim(set
) == 1)
1863 return convex_hull_1d(set
);
1865 return uset_convex_hull_wrap(set
);
1871 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1872 * We first remove the equalities (transforming the set), compute the
1873 * convex hull of the transformed set and then add the equalities back
1874 * (after performing the inverse transformation.
1876 static struct isl_basic_set
*modulo_affine_hull(
1877 struct isl_set
*set
, struct isl_basic_set
*affine_hull
)
1881 struct isl_basic_set
*dummy
;
1882 struct isl_basic_set
*convex_hull
;
1884 dummy
= isl_basic_set_remove_equalities(
1885 isl_basic_set_copy(affine_hull
), &T
, &T2
);
1888 isl_basic_set_free(dummy
);
1889 set
= isl_set_preimage(set
, T
);
1890 convex_hull
= uset_convex_hull(set
);
1891 convex_hull
= isl_basic_set_preimage(convex_hull
, T2
);
1892 convex_hull
= isl_basic_set_intersect(convex_hull
, affine_hull
);
1895 isl_basic_set_free(affine_hull
);
1900 /* Return an empty basic map living in the same space as "map".
1902 static __isl_give isl_basic_map
*replace_map_by_empty_basic_map(
1903 __isl_take isl_map
*map
)
1907 space
= isl_map_get_space(map
);
1909 return isl_basic_map_empty(space
);
1912 /* Compute the convex hull of a map.
1914 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1915 * specifically, the wrapping of facets to obtain new facets.
1917 struct isl_basic_map
*isl_map_convex_hull(struct isl_map
*map
)
1919 struct isl_basic_set
*bset
;
1920 struct isl_basic_map
*model
= NULL
;
1921 struct isl_basic_set
*affine_hull
= NULL
;
1922 struct isl_basic_map
*convex_hull
= NULL
;
1923 struct isl_set
*set
= NULL
;
1925 map
= isl_map_detect_equalities(map
);
1926 map
= isl_map_align_divs(map
);
1931 return replace_map_by_empty_basic_map(map
);
1933 model
= isl_basic_map_copy(map
->p
[0]);
1934 set
= isl_map_underlying_set(map
);
1938 affine_hull
= isl_set_affine_hull(isl_set_copy(set
));
1941 if (affine_hull
->n_eq
!= 0)
1942 bset
= modulo_affine_hull(set
, affine_hull
);
1944 isl_basic_set_free(affine_hull
);
1945 bset
= uset_convex_hull(set
);
1948 convex_hull
= isl_basic_map_overlying_set(bset
, model
);
1952 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
1953 ISL_F_SET(convex_hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1954 ISL_F_CLR(convex_hull
, ISL_BASIC_MAP_RATIONAL
);
1958 isl_basic_map_free(model
);
1962 struct isl_basic_set
*isl_set_convex_hull(struct isl_set
*set
)
1964 return bset_from_bmap(isl_map_convex_hull(set_to_map(set
)));
1967 __isl_give isl_basic_map
*isl_map_polyhedral_hull(__isl_take isl_map
*map
)
1969 isl_basic_map
*hull
;
1971 hull
= isl_map_convex_hull(map
);
1972 return isl_basic_map_remove_divs(hull
);
1975 __isl_give isl_basic_set
*isl_set_polyhedral_hull(__isl_take isl_set
*set
)
1977 return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set
)));
1980 struct sh_data_entry
{
1981 struct isl_hash_table
*table
;
1982 struct isl_tab
*tab
;
1985 /* Holds the data needed during the simple hull computation.
1987 * n the number of basic sets in the original set
1988 * hull_table a hash table of already computed constraints
1989 * in the simple hull
1990 * p for each basic set,
1991 * table a hash table of the constraints
1992 * tab the tableau corresponding to the basic set
1995 struct isl_ctx
*ctx
;
1997 struct isl_hash_table
*hull_table
;
1998 struct sh_data_entry p
[1];
2001 static void sh_data_free(struct sh_data
*data
)
2007 isl_hash_table_free(data
->ctx
, data
->hull_table
);
2008 for (i
= 0; i
< data
->n
; ++i
) {
2009 isl_hash_table_free(data
->ctx
, data
->p
[i
].table
);
2010 isl_tab_free(data
->p
[i
].tab
);
2015 struct ineq_cmp_data
{
2020 static int has_ineq(const void *entry
, const void *val
)
2022 isl_int
*row
= (isl_int
*)entry
;
2023 struct ineq_cmp_data
*v
= (struct ineq_cmp_data
*)val
;
2025 return isl_seq_eq(row
+ 1, v
->p
+ 1, v
->len
) ||
2026 isl_seq_is_neg(row
+ 1, v
->p
+ 1, v
->len
);
2029 static int hash_ineq(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
2030 isl_int
*ineq
, unsigned len
)
2033 struct ineq_cmp_data v
;
2034 struct isl_hash_table_entry
*entry
;
2038 c_hash
= isl_seq_get_hash(ineq
+ 1, len
);
2039 entry
= isl_hash_table_find(ctx
, table
, c_hash
, has_ineq
, &v
, 1);
2046 /* Fill hash table "table" with the constraints of "bset".
2047 * Equalities are added as two inequalities.
2048 * The value in the hash table is a pointer to the (in)equality of "bset".
2050 static int hash_basic_set(struct isl_hash_table
*table
,
2051 struct isl_basic_set
*bset
)
2054 unsigned dim
= isl_basic_set_total_dim(bset
);
2056 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2057 for (j
= 0; j
< 2; ++j
) {
2058 isl_seq_neg(bset
->eq
[i
], bset
->eq
[i
], 1 + dim
);
2059 if (hash_ineq(bset
->ctx
, table
, bset
->eq
[i
], dim
) < 0)
2063 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2064 if (hash_ineq(bset
->ctx
, table
, bset
->ineq
[i
], dim
) < 0)
2070 static struct sh_data
*sh_data_alloc(struct isl_set
*set
, unsigned n_ineq
)
2072 struct sh_data
*data
;
2075 data
= isl_calloc(set
->ctx
, struct sh_data
,
2076 sizeof(struct sh_data
) +
2077 (set
->n
- 1) * sizeof(struct sh_data_entry
));
2080 data
->ctx
= set
->ctx
;
2082 data
->hull_table
= isl_hash_table_alloc(set
->ctx
, n_ineq
);
2083 if (!data
->hull_table
)
2085 for (i
= 0; i
< set
->n
; ++i
) {
2086 data
->p
[i
].table
= isl_hash_table_alloc(set
->ctx
,
2087 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
);
2088 if (!data
->p
[i
].table
)
2090 if (hash_basic_set(data
->p
[i
].table
, set
->p
[i
]) < 0)
2099 /* Check if inequality "ineq" is a bound for basic set "j" or if
2100 * it can be relaxed (by increasing the constant term) to become
2101 * a bound for that basic set. In the latter case, the constant
2103 * Relaxation of the constant term is only allowed if "shift" is set.
2105 * Return 1 if "ineq" is a bound
2106 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2107 * -1 if some error occurred
2109 static int is_bound(struct sh_data
*data
, struct isl_set
*set
, int j
,
2110 isl_int
*ineq
, int shift
)
2112 enum isl_lp_result res
;
2115 if (!data
->p
[j
].tab
) {
2116 data
->p
[j
].tab
= isl_tab_from_basic_set(set
->p
[j
], 0);
2117 if (!data
->p
[j
].tab
)
2123 res
= isl_tab_min(data
->p
[j
].tab
, ineq
, data
->ctx
->one
,
2125 if (res
== isl_lp_ok
&& isl_int_is_neg(opt
)) {
2127 isl_int_sub(ineq
[0], ineq
[0], opt
);
2129 res
= isl_lp_unbounded
;
2134 return (res
== isl_lp_ok
|| res
== isl_lp_empty
) ? 1 :
2135 res
== isl_lp_unbounded
? 0 : -1;
2138 /* Set the constant term of "ineq" to the maximum of those of the constraints
2139 * in the basic sets of "set" following "i" that are parallel to "ineq".
2140 * That is, if any of the basic sets of "set" following "i" have a more
2141 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
2142 * "c_hash" is the hash value of the linear part of "ineq".
2143 * "v" has been set up for use by has_ineq.
2145 * Note that the two inequality constraints corresponding to an equality are
2146 * represented by the same inequality constraint in data->p[j].table
2147 * (but with different hash values). This means the constraint (or at
2148 * least its constant term) may need to be temporarily negated to get
2149 * the actually hashed constraint.
2151 static void set_max_constant_term(struct sh_data
*data
, __isl_keep isl_set
*set
,
2152 int i
, isl_int
*ineq
, uint32_t c_hash
, struct ineq_cmp_data
*v
)
2156 struct isl_hash_table_entry
*entry
;
2158 ctx
= isl_set_get_ctx(set
);
2159 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2163 entry
= isl_hash_table_find(ctx
, data
->p
[j
].table
,
2164 c_hash
, &has_ineq
, v
, 0);
2168 ineq_j
= entry
->data
;
2169 neg
= isl_seq_is_neg(ineq_j
+ 1, ineq
+ 1, v
->len
);
2171 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2172 if (isl_int_gt(ineq_j
[0], ineq
[0]))
2173 isl_int_set(ineq
[0], ineq_j
[0]);
2175 isl_int_neg(ineq_j
[0], ineq_j
[0]);
2179 /* Check if inequality "ineq" from basic set "i" is or can be relaxed to
2180 * become a bound on the whole set. If so, add the (relaxed) inequality
2181 * to "hull". Relaxation is only allowed if "shift" is set.
2183 * We first check if "hull" already contains a translate of the inequality.
2184 * If so, we are done.
2185 * Then, we check if any of the previous basic sets contains a translate
2186 * of the inequality. If so, then we have already considered this
2187 * inequality and we are done.
2188 * Otherwise, for each basic set other than "i", we check if the inequality
2189 * is a bound on the basic set, but first replace the constant term
2190 * by the maximal value of any translate of the inequality in any
2191 * of the following basic sets.
2192 * For previous basic sets, we know that they do not contain a translate
2193 * of the inequality, so we directly call is_bound.
2194 * For following basic sets, we first check if a translate of the
2195 * inequality appears in its description. If so, the constant term
2196 * of the inequality has already been updated with respect to this
2197 * translate and the inequality is therefore known to be a bound
2198 * of this basic set.
2200 static struct isl_basic_set
*add_bound(struct isl_basic_set
*hull
,
2201 struct sh_data
*data
, struct isl_set
*set
, int i
, isl_int
*ineq
,
2205 struct ineq_cmp_data v
;
2206 struct isl_hash_table_entry
*entry
;
2212 v
.len
= isl_basic_set_total_dim(hull
);
2214 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2216 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2221 for (j
= 0; j
< i
; ++j
) {
2222 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2223 c_hash
, has_ineq
, &v
, 0);
2230 k
= isl_basic_set_alloc_inequality(hull
);
2233 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2235 set_max_constant_term(data
, set
, i
, hull
->ineq
[k
], c_hash
, &v
);
2236 for (j
= 0; j
< i
; ++j
) {
2238 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2245 isl_basic_set_free_inequality(hull
, 1);
2249 for (j
= i
+ 1; j
< set
->n
; ++j
) {
2251 entry
= isl_hash_table_find(hull
->ctx
, data
->p
[j
].table
,
2252 c_hash
, has_ineq
, &v
, 0);
2255 bound
= is_bound(data
, set
, j
, hull
->ineq
[k
], shift
);
2262 isl_basic_set_free_inequality(hull
, 1);
2266 entry
= isl_hash_table_find(hull
->ctx
, data
->hull_table
, c_hash
,
2270 entry
->data
= hull
->ineq
[k
];
2274 isl_basic_set_free(hull
);
2278 /* Check if any inequality from basic set "i" is or can be relaxed to
2279 * become a bound on the whole set. If so, add the (relaxed) inequality
2280 * to "hull". Relaxation is only allowed if "shift" is set.
2282 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
2283 struct sh_data
*data
, struct isl_set
*set
, int i
, int shift
)
2286 unsigned dim
= isl_basic_set_total_dim(bset
);
2288 for (j
= 0; j
< set
->p
[i
]->n_eq
; ++j
) {
2289 for (k
= 0; k
< 2; ++k
) {
2290 isl_seq_neg(set
->p
[i
]->eq
[j
], set
->p
[i
]->eq
[j
], 1+dim
);
2291 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->eq
[j
],
2295 for (j
= 0; j
< set
->p
[i
]->n_ineq
; ++j
)
2296 bset
= add_bound(bset
, data
, set
, i
, set
->p
[i
]->ineq
[j
], shift
);
2300 /* Compute a superset of the convex hull of set that is described
2301 * by only (translates of) the constraints in the constituents of set.
2302 * Translation is only allowed if "shift" is set.
2304 static __isl_give isl_basic_set
*uset_simple_hull(__isl_take isl_set
*set
,
2307 struct sh_data
*data
= NULL
;
2308 struct isl_basic_set
*hull
= NULL
;
2316 for (i
= 0; i
< set
->n
; ++i
) {
2319 n_ineq
+= 2 * set
->p
[i
]->n_eq
+ set
->p
[i
]->n_ineq
;
2322 hull
= isl_basic_set_alloc_space(isl_space_copy(set
->dim
), 0, 0, n_ineq
);
2326 data
= sh_data_alloc(set
, n_ineq
);
2330 for (i
= 0; i
< set
->n
; ++i
)
2331 hull
= add_bounds(hull
, data
, set
, i
, shift
);
2339 isl_basic_set_free(hull
);
2344 /* Compute a superset of the convex hull of map that is described
2345 * by only (translates of) the constraints in the constituents of map.
2346 * Handle trivial cases where map is NULL or contains at most one disjunct.
2348 static __isl_give isl_basic_map
*map_simple_hull_trivial(
2349 __isl_take isl_map
*map
)
2351 isl_basic_map
*hull
;
2356 return replace_map_by_empty_basic_map(map
);
2358 hull
= isl_basic_map_copy(map
->p
[0]);
2363 /* Return a copy of the simple hull cached inside "map".
2364 * "shift" determines whether to return the cached unshifted or shifted
2367 static __isl_give isl_basic_map
*cached_simple_hull(__isl_take isl_map
*map
,
2370 isl_basic_map
*hull
;
2372 hull
= isl_basic_map_copy(map
->cached_simple_hull
[shift
]);
2378 /* Compute a superset of the convex hull of map that is described
2379 * by only (translates of) the constraints in the constituents of map.
2380 * Translation is only allowed if "shift" is set.
2382 * The constraints are sorted while removing redundant constraints
2383 * in order to indicate a preference of which constraints should
2384 * be preserved. In particular, pairs of constraints that are
2385 * sorted together are preferred to either both be preserved
2386 * or both be removed. The sorting is performed inside
2387 * isl_basic_map_remove_redundancies.
2389 * The result of the computation is stored in map->cached_simple_hull[shift]
2390 * such that it can be reused in subsequent calls. The cache is cleared
2391 * whenever the map is modified (in isl_map_cow).
2392 * Note that the results need to be stored in the input map for there
2393 * to be any chance that they may get reused. In particular, they
2394 * are stored in a copy of the input map that is saved before
2395 * the integer division alignment.
2397 static __isl_give isl_basic_map
*map_simple_hull(__isl_take isl_map
*map
,
2400 struct isl_set
*set
= NULL
;
2401 struct isl_basic_map
*model
= NULL
;
2402 struct isl_basic_map
*hull
;
2403 struct isl_basic_map
*affine_hull
;
2404 struct isl_basic_set
*bset
= NULL
;
2407 if (!map
|| map
->n
<= 1)
2408 return map_simple_hull_trivial(map
);
2410 if (map
->cached_simple_hull
[shift
])
2411 return cached_simple_hull(map
, shift
);
2413 map
= isl_map_detect_equalities(map
);
2414 if (!map
|| map
->n
<= 1)
2415 return map_simple_hull_trivial(map
);
2416 affine_hull
= isl_map_affine_hull(isl_map_copy(map
));
2417 input
= isl_map_copy(map
);
2418 map
= isl_map_align_divs(map
);
2419 model
= map
? isl_basic_map_copy(map
->p
[0]) : NULL
;
2421 set
= isl_map_underlying_set(map
);
2423 bset
= uset_simple_hull(set
, shift
);
2425 hull
= isl_basic_map_overlying_set(bset
, model
);
2427 hull
= isl_basic_map_intersect(hull
, affine_hull
);
2428 hull
= isl_basic_map_remove_redundancies(hull
);
2431 ISL_F_SET(hull
, ISL_BASIC_MAP_NO_IMPLICIT
);
2432 ISL_F_SET(hull
, ISL_BASIC_MAP_ALL_EQUALITIES
);
2435 hull
= isl_basic_map_finalize(hull
);
2437 input
->cached_simple_hull
[shift
] = isl_basic_map_copy(hull
);
2438 isl_map_free(input
);
2443 /* Compute a superset of the convex hull of map that is described
2444 * by only translates of the constraints in the constituents of map.
2446 __isl_give isl_basic_map
*isl_map_simple_hull(__isl_take isl_map
*map
)
2448 return map_simple_hull(map
, 1);
2451 struct isl_basic_set
*isl_set_simple_hull(struct isl_set
*set
)
2453 return bset_from_bmap(isl_map_simple_hull(set_to_map(set
)));
2456 /* Compute a superset of the convex hull of map that is described
2457 * by only the constraints in the constituents of map.
2459 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull(
2460 __isl_take isl_map
*map
)
2462 return map_simple_hull(map
, 0);
2465 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull(
2466 __isl_take isl_set
*set
)
2468 return isl_map_unshifted_simple_hull(set
);
2471 /* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
2472 * A constraint that appears with different constant terms
2473 * in "bmap1" and "bmap2" is also kept, with the least restrictive
2474 * (i.e., greatest) constant term.
2475 * "bmap1" and "bmap2" are assumed to have the same (known)
2476 * integer divisions.
2477 * The constraints of both "bmap1" and "bmap2" are assumed
2478 * to have been sorted using isl_basic_map_sort_constraints.
2480 * Run through the inequality constraints of "bmap1" and "bmap2"
2482 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2484 * If a match is found, the constraint is kept. If needed, the constant
2485 * term of the constraint is adjusted.
2487 static __isl_give isl_basic_map
*select_shared_inequalities(
2488 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2492 bmap1
= isl_basic_map_cow(bmap1
);
2493 if (!bmap1
|| !bmap2
)
2494 return isl_basic_map_free(bmap1
);
2496 i1
= bmap1
->n_ineq
- 1;
2497 i2
= bmap2
->n_ineq
- 1;
2498 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2501 cmp
= isl_basic_map_constraint_cmp(bmap1
, bmap1
->ineq
[i1
],
2508 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2509 bmap1
= isl_basic_map_free(bmap1
);
2513 if (isl_int_lt(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]))
2514 isl_int_set(bmap1
->ineq
[i1
][0], bmap2
->ineq
[i2
][0]);
2518 for (; i1
>= 0; --i1
)
2519 if (isl_basic_map_drop_inequality(bmap1
, i1
) < 0)
2520 bmap1
= isl_basic_map_free(bmap1
);
2525 /* Drop all equalities from "bmap1" that do not also appear in "bmap2".
2526 * "bmap1" and "bmap2" are assumed to have the same (known)
2527 * integer divisions.
2529 * Run through the equality constraints of "bmap1" and "bmap2".
2530 * Each constraint of "bmap1" without a matching constraint in "bmap2"
2533 static __isl_give isl_basic_map
*select_shared_equalities(
2534 __isl_take isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
2539 bmap1
= isl_basic_map_cow(bmap1
);
2540 if (!bmap1
|| !bmap2
)
2541 return isl_basic_map_free(bmap1
);
2543 total
= isl_basic_map_total_dim(bmap1
);
2545 i1
= bmap1
->n_eq
- 1;
2546 i2
= bmap2
->n_eq
- 1;
2547 while (bmap1
&& i1
>= 0 && i2
>= 0) {
2550 last1
= isl_seq_last_non_zero(bmap1
->eq
[i1
] + 1, total
);
2551 last2
= isl_seq_last_non_zero(bmap2
->eq
[i2
] + 1, total
);
2552 if (last1
> last2
) {
2556 if (last1
< last2
) {
2557 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2558 bmap1
= isl_basic_map_free(bmap1
);
2562 if (!isl_seq_eq(bmap1
->eq
[i1
], bmap2
->eq
[i2
], 1 + total
)) {
2563 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2564 bmap1
= isl_basic_map_free(bmap1
);
2569 for (; i1
>= 0; --i1
)
2570 if (isl_basic_map_drop_equality(bmap1
, i1
) < 0)
2571 bmap1
= isl_basic_map_free(bmap1
);
2576 /* Compute a superset of "bmap1" and "bmap2" that is described
2577 * by only the constraints that appear in both "bmap1" and "bmap2".
2579 * First drop constraints that involve unknown integer divisions
2580 * since it is not trivial to check whether two such integer divisions
2581 * in different basic maps are the same.
2582 * Then align the remaining (known) divs and sort the constraints.
2583 * Finally drop all inequalities and equalities from "bmap1" that
2584 * do not also appear in "bmap2".
2586 __isl_give isl_basic_map
*isl_basic_map_plain_unshifted_simple_hull(
2587 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
2589 bmap1
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap1
);
2590 bmap2
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap2
);
2591 bmap2
= isl_basic_map_align_divs(bmap2
, bmap1
);
2592 bmap1
= isl_basic_map_align_divs(bmap1
, bmap2
);
2593 bmap1
= isl_basic_map_gauss(bmap1
, NULL
);
2594 bmap2
= isl_basic_map_gauss(bmap2
, NULL
);
2595 bmap1
= isl_basic_map_sort_constraints(bmap1
);
2596 bmap2
= isl_basic_map_sort_constraints(bmap2
);
2598 bmap1
= select_shared_inequalities(bmap1
, bmap2
);
2599 bmap1
= select_shared_equalities(bmap1
, bmap2
);
2601 isl_basic_map_free(bmap2
);
2602 bmap1
= isl_basic_map_finalize(bmap1
);
2606 /* Compute a superset of the convex hull of "map" that is described
2607 * by only the constraints in the constituents of "map".
2608 * In particular, the result is composed of constraints that appear
2609 * in each of the basic maps of "map"
2611 * Constraints that involve unknown integer divisions are dropped
2612 * since it is not trivial to check whether two such integer divisions
2613 * in different basic maps are the same.
2615 * The hull is initialized from the first basic map and then
2616 * updated with respect to the other basic maps in turn.
2618 __isl_give isl_basic_map
*isl_map_plain_unshifted_simple_hull(
2619 __isl_take isl_map
*map
)
2622 isl_basic_map
*hull
;
2627 return map_simple_hull_trivial(map
);
2628 map
= isl_map_drop_constraint_involving_unknown_divs(map
);
2629 hull
= isl_basic_map_copy(map
->p
[0]);
2630 for (i
= 1; i
< map
->n
; ++i
) {
2631 isl_basic_map
*bmap_i
;
2633 bmap_i
= isl_basic_map_copy(map
->p
[i
]);
2634 hull
= isl_basic_map_plain_unshifted_simple_hull(hull
, bmap_i
);
2641 /* Compute a superset of the convex hull of "set" that is described
2642 * by only the constraints in the constituents of "set".
2643 * In particular, the result is composed of constraints that appear
2644 * in each of the basic sets of "set"
2646 __isl_give isl_basic_set
*isl_set_plain_unshifted_simple_hull(
2647 __isl_take isl_set
*set
)
2649 return isl_map_plain_unshifted_simple_hull(set
);
2652 /* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
2654 * For each basic set in "set", we first check if the basic set
2655 * contains a translate of "ineq". If this translate is more relaxed,
2656 * then we assume that "ineq" is not a bound on this basic set.
2657 * Otherwise, we know that it is a bound.
2658 * If the basic set does not contain a translate of "ineq", then
2659 * we call is_bound to perform the test.
2661 static __isl_give isl_basic_set
*add_bound_from_constraint(
2662 __isl_take isl_basic_set
*hull
, struct sh_data
*data
,
2663 __isl_keep isl_set
*set
, isl_int
*ineq
)
2668 struct ineq_cmp_data v
;
2671 return isl_basic_set_free(hull
);
2673 v
.len
= isl_basic_set_total_dim(hull
);
2675 c_hash
= isl_seq_get_hash(ineq
+ 1, v
.len
);
2677 ctx
= isl_basic_set_get_ctx(hull
);
2678 for (i
= 0; i
< set
->n
; ++i
) {
2680 struct isl_hash_table_entry
*entry
;
2682 entry
= isl_hash_table_find(ctx
, data
->p
[i
].table
,
2683 c_hash
, &has_ineq
, &v
, 0);
2685 isl_int
*ineq_i
= entry
->data
;
2686 int neg
, more_relaxed
;
2688 neg
= isl_seq_is_neg(ineq_i
+ 1, ineq
+ 1, v
.len
);
2690 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2691 more_relaxed
= isl_int_gt(ineq_i
[0], ineq
[0]);
2693 isl_int_neg(ineq_i
[0], ineq_i
[0]);
2699 bound
= is_bound(data
, set
, i
, ineq
, 0);
2701 return isl_basic_set_free(hull
);
2708 k
= isl_basic_set_alloc_inequality(hull
);
2710 return isl_basic_set_free(hull
);
2711 isl_seq_cpy(hull
->ineq
[k
], ineq
, 1 + v
.len
);
2716 /* Compute a superset of the convex hull of "set" that is described
2717 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
2718 * has no parameters or integer divisions.
2720 * The inequalities in "ineq" are assumed to have been sorted such
2721 * that constraints with the same linear part appear together and
2722 * that among constraints with the same linear part, those with
2723 * smaller constant term appear first.
2725 * We reuse the same data structure that is used by uset_simple_hull,
2726 * but we do not need the hull table since we will not consider the
2727 * same constraint more than once. We therefore allocate it with zero size.
2729 * We run through the constraints and try to add them one by one,
2730 * skipping identical constraints. If we have added a constraint and
2731 * the next constraint is a more relaxed translate, then we skip this
2732 * next constraint as well.
2734 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_constraints(
2735 __isl_take isl_set
*set
, int n_ineq
, isl_int
**ineq
)
2739 struct sh_data
*data
= NULL
;
2740 isl_basic_set
*hull
= NULL
;
2743 hull
= isl_basic_set_alloc_space(isl_set_get_space(set
), 0, 0, n_ineq
);
2747 data
= sh_data_alloc(set
, 0);
2751 dim
= isl_set_dim(set
, isl_dim_set
);
2752 for (i
= 0; i
< n_ineq
; ++i
) {
2753 int hull_n_ineq
= hull
->n_ineq
;
2756 parallel
= i
> 0 && isl_seq_eq(ineq
[i
- 1] + 1, ineq
[i
] + 1,
2759 (last_added
|| isl_int_eq(ineq
[i
- 1][0], ineq
[i
][0])))
2761 hull
= add_bound_from_constraint(hull
, data
, set
, ineq
[i
]);
2764 last_added
= hull
->n_ineq
> hull_n_ineq
;
2773 isl_basic_set_free(hull
);
2777 /* Collect pointers to all the inequalities in the elements of "list"
2778 * in "ineq". For equalities, store both a pointer to the equality and
2779 * a pointer to its opposite, which is first copied to "mat".
2780 * "ineq" and "mat" are assumed to have been preallocated to the right size
2781 * (the number of inequalities + 2 times the number of equalites and
2782 * the number of equalities, respectively).
2784 static __isl_give isl_mat
*collect_inequalities(__isl_take isl_mat
*mat
,
2785 __isl_keep isl_basic_set_list
*list
, isl_int
**ineq
)
2787 int i
, j
, n
, n_eq
, n_ineq
;
2794 n
= isl_basic_set_list_n_basic_set(list
);
2795 for (i
= 0; i
< n
; ++i
) {
2796 isl_basic_set
*bset
;
2797 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2799 return isl_mat_free(mat
);
2800 for (j
= 0; j
< bset
->n_eq
; ++j
) {
2801 ineq
[n_ineq
++] = mat
->row
[n_eq
];
2802 ineq
[n_ineq
++] = bset
->eq
[j
];
2803 isl_seq_neg(mat
->row
[n_eq
++], bset
->eq
[j
], mat
->n_col
);
2805 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2806 ineq
[n_ineq
++] = bset
->ineq
[j
];
2807 isl_basic_set_free(bset
);
2813 /* Comparison routine for use as an isl_sort callback.
2815 * Constraints with the same linear part are sorted together and
2816 * among constraints with the same linear part, those with smaller
2817 * constant term are sorted first.
2819 static int cmp_ineq(const void *a
, const void *b
, void *arg
)
2821 unsigned dim
= *(unsigned *) arg
;
2822 isl_int
* const *ineq1
= a
;
2823 isl_int
* const *ineq2
= b
;
2826 cmp
= isl_seq_cmp((*ineq1
) + 1, (*ineq2
) + 1, dim
);
2829 return isl_int_cmp((*ineq1
)[0], (*ineq2
)[0]);
2832 /* Compute a superset of the convex hull of "set" that is described
2833 * by only constraints in the elements of "list", where "set" has
2834 * no parameters or integer divisions.
2836 * We collect all the constraints in those elements and then
2837 * sort the constraints such that constraints with the same linear part
2838 * are sorted together and that those with smaller constant term are
2841 static __isl_give isl_basic_set
*uset_unshifted_simple_hull_from_basic_set_list(
2842 __isl_take isl_set
*set
, __isl_take isl_basic_set_list
*list
)
2844 int i
, n
, n_eq
, n_ineq
;
2847 isl_mat
*mat
= NULL
;
2848 isl_int
**ineq
= NULL
;
2849 isl_basic_set
*hull
;
2853 ctx
= isl_set_get_ctx(set
);
2857 n
= isl_basic_set_list_n_basic_set(list
);
2858 for (i
= 0; i
< n
; ++i
) {
2859 isl_basic_set
*bset
;
2860 bset
= isl_basic_set_list_get_basic_set(list
, i
);
2864 n_ineq
+= 2 * bset
->n_eq
+ bset
->n_ineq
;
2865 isl_basic_set_free(bset
);
2868 ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
);
2869 if (n_ineq
> 0 && !ineq
)
2872 dim
= isl_set_dim(set
, isl_dim_set
);
2873 mat
= isl_mat_alloc(ctx
, n_eq
, 1 + dim
);
2874 mat
= collect_inequalities(mat
, list
, ineq
);
2878 if (isl_sort(ineq
, n_ineq
, sizeof(ineq
[0]), &cmp_ineq
, &dim
) < 0)
2881 hull
= uset_unshifted_simple_hull_from_constraints(set
, n_ineq
, ineq
);
2885 isl_basic_set_list_free(list
);
2891 isl_basic_set_list_free(list
);
2895 /* Compute a superset of the convex hull of "map" that is described
2896 * by only constraints in the elements of "list".
2898 * If the list is empty, then we can only describe the universe set.
2899 * If the input map is empty, then all constraints are valid, so
2900 * we return the intersection of the elements in "list".
2902 * Otherwise, we align all divs and temporarily treat them
2903 * as regular variables, computing the unshifted simple hull in
2904 * uset_unshifted_simple_hull_from_basic_set_list.
2906 static __isl_give isl_basic_map
*map_unshifted_simple_hull_from_basic_map_list(
2907 __isl_take isl_map
*map
, __isl_take isl_basic_map_list
*list
)
2909 isl_basic_map
*model
;
2910 isl_basic_map
*hull
;
2912 isl_basic_set_list
*bset_list
;
2917 if (isl_basic_map_list_n_basic_map(list
) == 0) {
2920 space
= isl_map_get_space(map
);
2922 isl_basic_map_list_free(list
);
2923 return isl_basic_map_universe(space
);
2925 if (isl_map_plain_is_empty(map
)) {
2927 return isl_basic_map_list_intersect(list
);
2930 map
= isl_map_align_divs_to_basic_map_list(map
, list
);
2933 list
= isl_basic_map_list_align_divs_to_basic_map(list
, map
->p
[0]);
2935 model
= isl_basic_map_list_get_basic_map(list
, 0);
2937 set
= isl_map_underlying_set(map
);
2938 bset_list
= isl_basic_map_list_underlying_set(list
);
2940 hull
= uset_unshifted_simple_hull_from_basic_set_list(set
, bset_list
);
2941 hull
= isl_basic_map_overlying_set(hull
, model
);
2946 isl_basic_map_list_free(list
);
2950 /* Return a sequence of the basic maps that make up the maps in "list".
2952 static __isl_give isl_basic_map_list
*collect_basic_maps(
2953 __isl_take isl_map_list
*list
)
2957 isl_basic_map_list
*bmap_list
;
2961 n
= isl_map_list_n_map(list
);
2962 ctx
= isl_map_list_get_ctx(list
);
2963 bmap_list
= isl_basic_map_list_alloc(ctx
, 0);
2965 for (i
= 0; i
< n
; ++i
) {
2967 isl_basic_map_list
*list_i
;
2969 map
= isl_map_list_get_map(list
, i
);
2970 map
= isl_map_compute_divs(map
);
2971 list_i
= isl_map_get_basic_map_list(map
);
2973 bmap_list
= isl_basic_map_list_concat(bmap_list
, list_i
);
2976 isl_map_list_free(list
);
2980 /* Compute a superset of the convex hull of "map" that is described
2981 * by only constraints in the elements of "list".
2983 * If "map" is the universe, then the convex hull (and therefore
2984 * any superset of the convexhull) is the universe as well.
2986 * Otherwise, we collect all the basic maps in the map list and
2987 * continue with map_unshifted_simple_hull_from_basic_map_list.
2989 __isl_give isl_basic_map
*isl_map_unshifted_simple_hull_from_map_list(
2990 __isl_take isl_map
*map
, __isl_take isl_map_list
*list
)
2992 isl_basic_map_list
*bmap_list
;
2995 is_universe
= isl_map_plain_is_universe(map
);
2996 if (is_universe
< 0)
2997 map
= isl_map_free(map
);
2998 if (is_universe
< 0 || is_universe
) {
2999 isl_map_list_free(list
);
3000 return isl_map_unshifted_simple_hull(map
);
3003 bmap_list
= collect_basic_maps(list
);
3004 return map_unshifted_simple_hull_from_basic_map_list(map
, bmap_list
);
3007 /* Compute a superset of the convex hull of "set" that is described
3008 * by only constraints in the elements of "list".
3010 __isl_give isl_basic_set
*isl_set_unshifted_simple_hull_from_set_list(
3011 __isl_take isl_set
*set
, __isl_take isl_set_list
*list
)
3013 return isl_map_unshifted_simple_hull_from_map_list(set
, list
);
3016 /* Given a set "set", return parametric bounds on the dimension "dim".
3018 static struct isl_basic_set
*set_bounds(struct isl_set
*set
, int dim
)
3020 unsigned set_dim
= isl_set_dim(set
, isl_dim_set
);
3021 set
= isl_set_copy(set
);
3022 set
= isl_set_eliminate_dims(set
, dim
+ 1, set_dim
- (dim
+ 1));
3023 set
= isl_set_eliminate_dims(set
, 0, dim
);
3024 return isl_set_convex_hull(set
);
3027 /* Computes a "simple hull" and then check if each dimension in the
3028 * resulting hull is bounded by a symbolic constant. If not, the
3029 * hull is intersected with the corresponding bounds on the whole set.
3031 struct isl_basic_set
*isl_set_bounded_simple_hull(struct isl_set
*set
)
3034 struct isl_basic_set
*hull
;
3035 unsigned nparam
, left
;
3036 int removed_divs
= 0;
3038 hull
= isl_set_simple_hull(isl_set_copy(set
));
3042 nparam
= isl_basic_set_dim(hull
, isl_dim_param
);
3043 for (i
= 0; i
< isl_basic_set_dim(hull
, isl_dim_set
); ++i
) {
3044 int lower
= 0, upper
= 0;
3045 struct isl_basic_set
*bounds
;
3047 left
= isl_basic_set_total_dim(hull
) - nparam
- i
- 1;
3048 for (j
= 0; j
< hull
->n_eq
; ++j
) {
3049 if (isl_int_is_zero(hull
->eq
[j
][1 + nparam
+ i
]))
3051 if (isl_seq_first_non_zero(hull
->eq
[j
]+1+nparam
+i
+1,
3058 for (j
= 0; j
< hull
->n_ineq
; ++j
) {
3059 if (isl_int_is_zero(hull
->ineq
[j
][1 + nparam
+ i
]))
3061 if (isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
+i
+1,
3063 isl_seq_first_non_zero(hull
->ineq
[j
]+1+nparam
,
3066 if (isl_int_is_pos(hull
->ineq
[j
][1 + nparam
+ i
]))
3077 if (!removed_divs
) {
3078 set
= isl_set_remove_divs(set
);
3083 bounds
= set_bounds(set
, i
);
3084 hull
= isl_basic_set_intersect(hull
, bounds
);
3093 isl_basic_set_free(hull
);